Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_ch4.adb
blob153a63586ca5d9c92508199c346e6972df7ed8a6
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-2023, 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 Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Util; use Exp_Util;
36 with Itypes; use Itypes;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Dim; use Sem_Dim;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.Nodes; use Sinfo.Nodes;
66 with Sinfo.Utils; use Sinfo.Utils;
67 with Snames; use Snames;
68 with Tbuild; use Tbuild;
69 with Uintp; use Uintp;
70 with Warnsw; use Warnsw;
72 package body Sem_Ch4 is
74 -- Tables which speed up the identification of dangerous calls to Ada 2012
75 -- functions with writable actuals (AI05-0144).
77 -- The following table enumerates the Ada constructs which may evaluate in
78 -- arbitrary order. It does not cover all the language constructs which can
79 -- be evaluated in arbitrary order but the subset needed for AI05-0144.
81 Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean :=
82 (N_Aggregate => True,
83 N_Assignment_Statement => True,
84 N_Entry_Call_Statement => True,
85 N_Extension_Aggregate => True,
86 N_Full_Type_Declaration => True,
87 N_Indexed_Component => True,
88 N_Object_Declaration => True,
89 N_Pragma => True,
90 N_Range => True,
91 N_Slice => True,
92 N_Array_Type_Definition => True,
93 N_Membership_Test => True,
94 N_Binary_Op => True,
95 N_Subprogram_Call => True,
96 others => False);
98 -- The following table enumerates the nodes on which we stop climbing when
99 -- locating the outermost Ada construct that can be evaluated in arbitrary
100 -- order.
102 Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean :=
103 (N_Aggregate => True,
104 N_Assignment_Statement => True,
105 N_Entry_Call_Statement => True,
106 N_Extended_Return_Statement => True,
107 N_Extension_Aggregate => True,
108 N_Full_Type_Declaration => True,
109 N_Object_Declaration => True,
110 N_Object_Renaming_Declaration => True,
111 N_Package_Specification => True,
112 N_Pragma => True,
113 N_Procedure_Call_Statement => True,
114 N_Simple_Return_Statement => True,
115 N_Has_Condition => True,
116 others => False);
118 -----------------------
119 -- Local Subprograms --
120 -----------------------
122 procedure Analyze_Concatenation_Rest (N : Node_Id);
123 -- Does the "rest" of the work of Analyze_Concatenation, after the left
124 -- operand has been analyzed. See Analyze_Concatenation for details.
126 procedure Analyze_Expression (N : Node_Id);
127 -- For expressions that are not names, this is just a call to analyze. If
128 -- the expression is a name, it may be a call to a parameterless function,
129 -- and if so must be converted into an explicit call node and analyzed as
130 -- such. This deproceduring must be done during the first pass of overload
131 -- resolution, because otherwise a procedure call with overloaded actuals
132 -- may fail to resolve.
134 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
135 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an
136 -- operator name or an expanded name whose selector is an operator name,
137 -- and one possible interpretation is as a predefined operator.
139 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
140 -- If the prefix of a selected_component is overloaded, the proper
141 -- interpretation that yields a record type with the proper selector
142 -- name must be selected.
144 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
145 -- Procedure to analyze a user defined binary operator, which is resolved
146 -- like a function, but instead of a list of actuals it is presented
147 -- with the left and right operands of an operator node.
149 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
150 -- Procedure to analyze a user defined unary operator, which is resolved
151 -- like a function, but instead of a list of actuals, it is presented with
152 -- the operand of the operator node.
154 procedure Analyze_One_Call
155 (N : Node_Id;
156 Nam : Entity_Id;
157 Report : Boolean;
158 Success : out Boolean;
159 Skip_First : Boolean := False);
160 -- Check one interpretation of an overloaded subprogram name for
161 -- compatibility with the types of the actuals in a call. If there is a
162 -- single interpretation which does not match, post error if Report is
163 -- set to True.
165 -- Nam is the entity that provides the formals against which the actuals
166 -- are checked. Nam is either the name of a subprogram, or the internal
167 -- subprogram type constructed for an access_to_subprogram. If the actuals
168 -- are compatible with Nam, then Nam is added to the list of candidate
169 -- interpretations for N, and Success is set to True.
171 -- The flag Skip_First is used when analyzing a call that was rewritten
172 -- from object notation. In this case the first actual may have to receive
173 -- an explicit dereference, depending on the first formal of the operation
174 -- being called. The caller will have verified that the object is legal
175 -- for the call. If the remaining parameters match, the first parameter
176 -- will rewritten as a dereference if needed, prior to completing analysis.
178 procedure Check_Misspelled_Selector
179 (Prefix : Entity_Id;
180 Sel : Node_Id);
181 -- Give possible misspelling message if Sel seems likely to be a mis-
182 -- spelling of one of the selectors of the Prefix. This is called by
183 -- Analyze_Selected_Component after producing an invalid selector error
184 -- message.
186 procedure Find_Arithmetic_Types
187 (L, R : Node_Id;
188 Op_Id : Entity_Id;
189 N : Node_Id);
190 -- L and R are the operands of an arithmetic operator. Find consistent
191 -- pairs of interpretations for L and R that have a numeric type consistent
192 -- with the semantics of the operator.
194 procedure Find_Comparison_Equality_Types
195 (L, R : Node_Id;
196 Op_Id : Entity_Id;
197 N : Node_Id);
198 -- L and R are operands of a comparison or equality operator. Find valid
199 -- pairs of interpretations for L and R.
201 procedure Find_Concatenation_Types
202 (L, R : Node_Id;
203 Op_Id : Entity_Id;
204 N : Node_Id);
205 -- For the four varieties of concatenation
207 procedure Find_Boolean_Types
208 (L, R : Node_Id;
209 Op_Id : Entity_Id;
210 N : Node_Id);
211 -- Ditto for binary logical operations
213 procedure Find_Negation_Types
214 (R : Node_Id;
215 Op_Id : Entity_Id;
216 N : Node_Id);
217 -- Find consistent interpretation for operand of negation operator
219 function Find_Primitive_Operation (N : Node_Id) return Boolean;
220 -- Find candidate interpretations for the name Obj.Proc when it appears in
221 -- a subprogram renaming declaration.
223 procedure Find_Unary_Types
224 (R : Node_Id;
225 Op_Id : Entity_Id;
226 N : Node_Id);
227 -- Unary arithmetic types: plus, minus, abs
229 procedure Check_Arithmetic_Pair
230 (T1, T2 : Entity_Id;
231 Op_Id : Entity_Id;
232 N : Node_Id);
233 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
234 -- for left and right operand. Determine whether they constitute a valid
235 -- pair for the given operator, and record the corresponding interpretation
236 -- of the operator node. The node N may be an operator node (the usual
237 -- case) or a function call whose prefix is an operator designator. In
238 -- both cases Op_Id is the operator name itself.
240 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
241 -- Give detailed information on overloaded call where none of the
242 -- interpretations match. N is the call node, Nam the designator for
243 -- the overloaded entity being called.
245 function Junk_Operand (N : Node_Id) return Boolean;
246 -- Test for an operand that is an inappropriate entity (e.g. a package
247 -- name or a label). If so, issue an error message and return True. If
248 -- the operand is not an inappropriate entity kind, return False.
250 procedure Operator_Check (N : Node_Id);
251 -- Verify that an operator has received some valid interpretation. If none
252 -- was found, determine whether a use clause would make the operation
253 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
254 -- every type compatible with the operator, even if the operator for the
255 -- type is not directly visible. The routine uses this type to emit a more
256 -- informative message.
258 function Has_Possible_Literal_Aspects (N : Node_Id) return Boolean;
259 -- Ada_2022: if an operand is a literal it may be subject to an
260 -- implicit conversion to a type for which a user-defined literal
261 -- function exists. During the first pass of type resolution we do
262 -- not know the context imposed on the literal, so we assume that
263 -- the literal type is a valid candidate and rely on the second pass
264 -- of resolution to find the type with the proper aspect. We only
265 -- add this interpretation if no other one was found, which may be
266 -- too restrictive but seems sufficient to handle most proper uses
267 -- of the new aspect. It is unclear whether a full implementation of
268 -- these aspects can be achieved without larger modifications to the
269 -- two-pass resolution algorithm.
271 function Possible_Type_For_Conditional_Expression
272 (T1, T2 : Entity_Id) return Entity_Id;
273 -- Given two types T1 and T2 that are _not_ compatible, return a type that
274 -- may still be used as the possible type of a conditional expression whose
275 -- dependent expressions, or part thereof, have type T1 and T2 respectively
276 -- during the first phase of type resolution, or Empty if such a type does
277 -- not exist.
279 -- The typical example is an if_expression whose then_expression is of a
280 -- tagged type and whose else_expresssion is of an extension of this type:
281 -- the types are not compatible but such an if_expression can be legal if
282 -- its expected type is the 'Class of the tagged type, so the function will
283 -- return the tagged type in this case. If the expected type turns out to
284 -- be something else, including the tagged type itself, then an error will
285 -- be given during the second phase of type resolution.
287 procedure Remove_Abstract_Operations (N : Node_Id);
288 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
289 -- operation is not a candidate interpretation.
291 function Try_Container_Indexing
292 (N : Node_Id;
293 Prefix : Node_Id;
294 Exprs : List_Id) return Boolean;
295 -- AI05-0139: Generalized indexing to support iterators over containers
296 -- ??? Need to provide a more detailed spec of what this function does
298 function Try_Indexed_Call
299 (N : Node_Id;
300 Nam : Entity_Id;
301 Typ : Entity_Id;
302 Skip_First : Boolean) return Boolean;
303 -- If a function has defaults for all its actuals, a call to it may in fact
304 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
305 -- interpretation as an indexing, prior to analysis as a call. If both are
306 -- possible, the node is overloaded with both interpretations (same symbol
307 -- but two different types). If the call is written in prefix form, the
308 -- prefix becomes the first parameter in the call, and only the remaining
309 -- actuals must be checked for the presence of defaults.
311 function Try_Indirect_Call
312 (N : Node_Id;
313 Nam : Entity_Id;
314 Typ : Entity_Id) return Boolean;
315 -- Similarly, a function F that needs no actuals can return an access to a
316 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
317 -- the call may be overloaded with both interpretations.
319 procedure wpo (T : Entity_Id);
320 pragma Warnings (Off, wpo);
321 -- Used for debugging: obtain list of primitive operations even if
322 -- type is not frozen and dispatch table is not built yet.
324 ------------------------
325 -- Ambiguous_Operands --
326 ------------------------
328 procedure Ambiguous_Operands (N : Node_Id) is
329 procedure List_Operand_Interps (Opnd : Node_Id);
331 --------------------------
332 -- List_Operand_Interps --
333 --------------------------
335 procedure List_Operand_Interps (Opnd : Node_Id) is
336 Nam : Node_Id := Empty;
337 Err : Node_Id := N;
339 begin
340 if Is_Overloaded (Opnd) then
341 if Nkind (Opnd) in N_Op then
342 Nam := Opnd;
344 elsif Nkind (Opnd) = N_Function_Call then
345 Nam := Name (Opnd);
347 elsif Ada_Version >= Ada_2012 then
348 declare
349 It : Interp;
350 I : Interp_Index;
352 begin
353 Get_First_Interp (Opnd, I, It);
354 while Present (It.Nam) loop
355 if Has_Implicit_Dereference (It.Typ) then
356 Error_Msg_N
357 ("can be interpreted as implicit dereference", Opnd);
358 return;
359 end if;
361 Get_Next_Interp (I, It);
362 end loop;
363 end;
365 return;
366 end if;
368 else
369 return;
370 end if;
372 if Opnd = Left_Opnd (N) then
373 Error_Msg_N
374 ("\left operand has the following interpretations", N);
375 else
376 Error_Msg_N
377 ("\right operand has the following interpretations", N);
378 Err := Opnd;
379 end if;
381 List_Interps (Nam, Err);
382 end List_Operand_Interps;
384 -- Start of processing for Ambiguous_Operands
386 begin
387 if Nkind (N) in N_Membership_Test then
388 Error_Msg_N ("ambiguous operands for membership", N);
390 elsif Nkind (N) in N_Op_Eq | N_Op_Ne then
391 Error_Msg_N ("ambiguous operands for equality", N);
393 else
394 Error_Msg_N ("ambiguous operands for comparison", N);
395 end if;
397 if All_Errors_Mode then
398 List_Operand_Interps (Left_Opnd (N));
399 List_Operand_Interps (Right_Opnd (N));
400 else
401 Error_Msg_N ("\use -gnatf switch for details", N);
402 end if;
403 end Ambiguous_Operands;
405 -----------------------
406 -- Analyze_Aggregate --
407 -----------------------
409 -- Most of the analysis of Aggregates requires that the type be known, and
410 -- is therefore put off until resolution of the context. Delta aggregates
411 -- have a base component that determines the enclosing aggregate type so
412 -- its type can be ascertained earlier. This also allows delta aggregates
413 -- to appear in the context of a record type with a private extension, as
414 -- per the latest update of AI12-0127.
416 procedure Analyze_Aggregate (N : Node_Id) is
417 begin
418 if No (Etype (N)) then
419 if Nkind (N) = N_Delta_Aggregate then
420 declare
421 Base : constant Node_Id := Expression (N);
423 I : Interp_Index;
424 It : Interp;
426 begin
427 Analyze (Base);
429 -- If the base is overloaded, propagate interpretations to the
430 -- enclosing aggregate.
432 if Is_Overloaded (Base) then
433 Get_First_Interp (Base, I, It);
434 Set_Etype (N, Any_Type);
436 while Present (It.Nam) loop
437 Add_One_Interp (N, It.Typ, It.Typ);
438 Get_Next_Interp (I, It);
439 end loop;
441 else
442 Set_Etype (N, Etype (Base));
443 end if;
444 end;
446 else
447 Set_Etype (N, Any_Composite);
448 end if;
449 end if;
450 end Analyze_Aggregate;
452 -----------------------
453 -- Analyze_Allocator --
454 -----------------------
456 procedure Analyze_Allocator (N : Node_Id) is
457 Loc : constant Source_Ptr := Sloc (N);
458 Sav_Errs : constant Nat := Serious_Errors_Detected;
459 E : Node_Id := Expression (N);
460 Acc_Type : Entity_Id;
461 Type_Id : Entity_Id;
462 P : Node_Id;
463 C : Node_Id;
464 Onode : Node_Id;
466 begin
467 -- Deal with allocator restrictions
469 -- In accordance with H.4(7), the No_Allocators restriction only applies
470 -- to user-written allocators. The same consideration applies to the
471 -- No_Standard_Allocators_Before_Elaboration restriction.
473 if Comes_From_Source (N) then
474 Check_Restriction (No_Allocators, N);
476 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
477 -- look at enclosing context, checking task/main subprogram case.
479 C := N;
480 P := Parent (C);
481 while Present (P) loop
483 -- For the task case we need a handled sequence of statements,
484 -- where the occurrence of the allocator is within the statements
485 -- and the parent is a task body
487 if Nkind (P) = N_Handled_Sequence_Of_Statements
488 and then Is_List_Member (C)
489 and then List_Containing (C) = Statements (P)
490 then
491 Onode := Original_Node (Parent (P));
493 -- Check for allocator within task body, this is a definite
494 -- violation of No_Allocators_After_Elaboration we can detect
495 -- at compile time.
497 if Nkind (Onode) = N_Task_Body then
498 Check_Restriction
499 (No_Standard_Allocators_After_Elaboration, N);
500 exit;
501 end if;
502 end if;
504 -- The other case is appearance in a subprogram body. This is
505 -- a violation if this is a library level subprogram with no
506 -- parameters. Note that this is now a static error even if the
507 -- subprogram is not the main program (this is a change, in an
508 -- earlier version only the main program was affected, and the
509 -- check had to be done in the binder).
511 if Nkind (P) = N_Subprogram_Body
512 and then Nkind (Parent (P)) = N_Compilation_Unit
513 and then No (Parameter_Specifications (Specification (P)))
514 then
515 Check_Restriction
516 (No_Standard_Allocators_After_Elaboration, N);
517 end if;
519 C := P;
520 P := Parent (C);
521 end loop;
522 end if;
524 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
525 -- any. The expected type for the name is any type. A non-overloading
526 -- rule then requires it to be of a type descended from
527 -- System.Storage_Pools.Subpools.Subpool_Handle.
529 -- This isn't exactly what the AI says, but it seems to be the right
530 -- rule. The AI should be fixed.???
532 declare
533 Subpool : constant Node_Id := Subpool_Handle_Name (N);
535 begin
536 if Present (Subpool) then
537 Analyze (Subpool);
539 if Is_Overloaded (Subpool) then
540 Error_Msg_N ("ambiguous subpool handle", Subpool);
541 end if;
543 -- Check that Etype (Subpool) is descended from Subpool_Handle
545 Resolve (Subpool);
546 end if;
547 end;
549 -- Analyze the qualified expression or subtype indication
551 if Nkind (E) = N_Qualified_Expression then
552 Acc_Type := Create_Itype (E_Allocator_Type, N);
553 Set_Etype (Acc_Type, Acc_Type);
554 Find_Type (Subtype_Mark (E));
556 -- Analyze the qualified expression, and apply the name resolution
557 -- rule given in 4.7(3).
559 Analyze (E);
560 Type_Id := Etype (E);
561 Set_Directly_Designated_Type (Acc_Type, Type_Id);
563 -- A qualified expression requires an exact match of the type,
564 -- class-wide matching is not allowed.
566 -- if Is_Class_Wide_Type (Type_Id)
567 -- and then Base_Type
568 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
569 -- then
570 -- Wrong_Type (Expression (E), Type_Id);
571 -- end if;
573 -- We don't analyze the qualified expression itself because it's
574 -- part of the allocator. It is fully analyzed and resolved when
575 -- the allocator is resolved with the context type.
577 Set_Etype (E, Type_Id);
579 -- Case where allocator has a subtype indication
581 else
582 -- If the allocator includes a N_Subtype_Indication then a
583 -- constraint is present, otherwise the node is a subtype mark.
584 -- Introduce an explicit subtype declaration into the tree
585 -- defining some anonymous subtype and rewrite the allocator to
586 -- use this subtype rather than the subtype indication.
588 -- It is important to introduce the explicit subtype declaration
589 -- so that the bounds of the subtype indication are attached to
590 -- the tree in case the allocator is inside a generic unit.
592 -- Finally, if there is no subtype indication and the type is
593 -- a tagged unconstrained type with discriminants, the designated
594 -- object is constrained by their default values, and it is
595 -- simplest to introduce an explicit constraint now. In some cases
596 -- this is done during expansion, but freeze actions are certain
597 -- to be emitted in the proper order if constraint is explicit.
599 if Is_Entity_Name (E) and then Expander_Active then
600 Find_Type (E);
601 Type_Id := Entity (E);
603 if Is_Tagged_Type (Type_Id)
604 and then Has_Defaulted_Discriminants (Type_Id)
605 and then not Is_Constrained (Type_Id)
606 then
607 declare
608 Constr : constant List_Id := New_List;
609 Loc : constant Source_Ptr := Sloc (E);
610 Discr : Entity_Id := First_Discriminant (Type_Id);
612 begin
613 while Present (Discr) loop
614 Append (Discriminant_Default_Value (Discr), Constr);
615 Next_Discriminant (Discr);
616 end loop;
618 Rewrite (E,
619 Make_Subtype_Indication (Loc,
620 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
621 Constraint =>
622 Make_Index_Or_Discriminant_Constraint (Loc,
623 Constraints => Constr)));
624 end;
625 end if;
626 end if;
628 if Nkind (E) = N_Subtype_Indication then
629 declare
630 Def_Id : Entity_Id;
631 Base_Typ : Entity_Id;
633 begin
634 -- A constraint is only allowed for a composite type in Ada
635 -- 95. In Ada 83, a constraint is also allowed for an
636 -- access-to-composite type, but the constraint is ignored.
638 Find_Type (Subtype_Mark (E));
639 Base_Typ := Entity (Subtype_Mark (E));
641 if Is_Elementary_Type (Base_Typ) then
642 if not (Ada_Version = Ada_83
643 and then Is_Access_Type (Base_Typ))
644 then
645 Error_Msg_N ("constraint not allowed here", E);
647 if Nkind (Constraint (E)) =
648 N_Index_Or_Discriminant_Constraint
649 then
650 Error_Msg_N -- CODEFIX
651 ("\if qualified expression was meant, " &
652 "use apostrophe", Constraint (E));
653 end if;
654 end if;
656 -- Get rid of the bogus constraint:
658 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
659 Analyze_Allocator (N);
660 return;
661 end if;
663 -- In GNATprove mode we need to preserve the link between
664 -- the original subtype indication and the anonymous subtype,
665 -- to extend proofs to constrained access types. We only do
666 -- that outside of spec expressions, otherwise the declaration
667 -- cannot be inserted and analyzed. In such a case, GNATprove
668 -- later rejects the allocator as it is not used here in
669 -- a non-interfering context (SPARK 4.8(2) and 7.1.3(10)).
671 if Expander_Active
672 or else (GNATprove_Mode and then not In_Spec_Expression)
673 then
674 Def_Id := Make_Temporary (Loc, 'S');
676 declare
677 Subtype_Decl : constant Node_Id :=
678 Make_Subtype_Declaration (Loc,
679 Defining_Identifier => Def_Id,
680 Subtype_Indication => Relocate_Node (E));
681 begin
682 Insert_Action (E, Subtype_Decl);
684 -- Handle unusual case where Insert_Action does not
685 -- analyze the declaration. Subtype_Decl must be
686 -- preanalyzed before call to Process_Subtype below.
687 Preanalyze (Subtype_Decl);
688 end;
690 if Sav_Errs /= Serious_Errors_Detected
691 and then Nkind (Constraint (E)) =
692 N_Index_Or_Discriminant_Constraint
693 then
694 Error_Msg_N -- CODEFIX
695 ("if qualified expression was meant, use apostrophe!",
696 Constraint (E));
697 end if;
699 E := New_Occurrence_Of (Def_Id, Loc);
700 Rewrite (Expression (N), E);
701 end if;
702 end;
703 end if;
705 Type_Id := Process_Subtype (E, N);
706 Acc_Type := Create_Itype (E_Allocator_Type, N);
707 Set_Etype (Acc_Type, Acc_Type);
708 Set_Directly_Designated_Type (Acc_Type, Type_Id);
709 Check_Fully_Declared (Type_Id, N);
711 -- Ada 2005 (AI-231): If the designated type is itself an access
712 -- type that excludes null, its default initialization will
713 -- be a null object, and we can insert an unconditional raise
714 -- before the allocator.
716 -- Ada 2012 (AI-104): A not null indication here is altogether
717 -- illegal.
719 if Can_Never_Be_Null (Type_Id) then
720 declare
721 Not_Null_Check : constant Node_Id :=
722 Make_Raise_Constraint_Error (Sloc (E),
723 Reason => CE_Null_Not_Allowed);
725 begin
726 if Expander_Active then
727 Insert_Action (N, Not_Null_Check);
728 Analyze (Not_Null_Check);
730 elsif Warn_On_Ada_2012_Compatibility then
731 Error_Msg_N
732 ("null value not allowed here in Ada 2012?y?", E);
733 end if;
734 end;
735 end if;
737 -- Check for missing initialization. Skip this check if the allocator
738 -- is made for a special return object or if we already had errors on
739 -- analyzing the allocator since, in that case, these are very likely
740 -- cascaded errors.
742 if not Is_Definite_Subtype (Type_Id)
743 and then not For_Special_Return_Object (N)
744 and then Serious_Errors_Detected = Sav_Errs
745 then
746 if Is_Class_Wide_Type (Type_Id) then
747 Error_Msg_N
748 ("initialization required in class-wide allocation", N);
750 else
751 if Ada_Version < Ada_2005
752 and then Is_Limited_Type (Type_Id)
753 then
754 Error_Msg_N ("unconstrained allocation not allowed", N);
756 if Is_Array_Type (Type_Id) then
757 Error_Msg_N
758 ("\constraint with array bounds required", N);
760 elsif Has_Unknown_Discriminants (Type_Id) then
761 null;
763 else pragma Assert (Has_Discriminants (Type_Id));
764 Error_Msg_N
765 ("\constraint with discriminant values required", N);
766 end if;
768 -- Limited Ada 2005 and general nonlimited case.
769 -- This is an error, except in the case of an
770 -- uninitialized allocator that is generated
771 -- for a build-in-place function return of a
772 -- discriminated but compile-time-known-size
773 -- type.
775 else
776 if Is_Rewrite_Substitution (N)
777 and then Nkind (Original_Node (N)) = N_Allocator
778 then
779 declare
780 Qual : constant Node_Id :=
781 Expression (Original_Node (N));
782 pragma Assert
783 (Nkind (Qual) = N_Qualified_Expression);
784 Call : constant Node_Id := Expression (Qual);
785 pragma Assert
786 (Is_Expanded_Build_In_Place_Call (Call));
787 begin
788 null;
789 end;
791 else
792 Error_Msg_N
793 ("uninitialized unconstrained allocation not "
794 & "allowed", N);
796 if Is_Array_Type (Type_Id) then
797 Error_Msg_N
798 ("\qualified expression or constraint with "
799 & "array bounds required", N);
801 elsif Has_Unknown_Discriminants (Type_Id) then
802 Error_Msg_N ("\qualified expression required", N);
804 else pragma Assert (Has_Discriminants (Type_Id));
805 Error_Msg_N
806 ("\qualified expression or constraint with "
807 & "discriminant values required", N);
808 end if;
809 end if;
810 end if;
811 end if;
812 end if;
813 end if;
815 if Is_Abstract_Type (Type_Id) then
816 Error_Msg_N ("cannot allocate abstract object", E);
817 end if;
819 Set_Etype (N, Acc_Type);
821 -- If this is an allocator for the return stack, then no restriction may
822 -- be violated since it's just a low-level access to the primary stack.
824 if Nkind (Parent (N)) = N_Object_Declaration
825 and then Is_Entity_Name (Object_Definition (Parent (N)))
826 and then Is_Access_Type (Entity (Object_Definition (Parent (N))))
827 then
828 declare
829 Pool : constant Entity_Id :=
830 Associated_Storage_Pool
831 (Root_Type (Entity (Object_Definition (Parent (N)))));
833 begin
834 if Present (Pool) and then Is_RTE (Pool, RE_RS_Pool) then
835 goto Leave;
836 end if;
837 end;
838 end if;
840 if Has_Task (Designated_Type (Acc_Type)) then
841 Check_Restriction (No_Tasking, N);
842 Check_Restriction (Max_Tasks, N);
843 Check_Restriction (No_Task_Allocators, N);
844 end if;
846 -- Check restriction against dynamically allocated protected objects
848 if Has_Protected (Designated_Type (Acc_Type)) then
849 Check_Restriction (No_Protected_Type_Allocators, N);
850 end if;
852 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
853 -- type is nested, and the designated type needs finalization. The rule
854 -- is conservative in that class-wide types need finalization.
856 if Needs_Finalization (Designated_Type (Acc_Type))
857 and then not Is_Library_Level_Entity (Acc_Type)
858 then
859 Check_Restriction (No_Nested_Finalization, N);
860 end if;
862 -- Check that an allocator of a nested access type doesn't create a
863 -- protected object when restriction No_Local_Protected_Objects applies.
865 if Has_Protected (Designated_Type (Acc_Type))
866 and then not Is_Library_Level_Entity (Acc_Type)
867 then
868 Check_Restriction (No_Local_Protected_Objects, N);
869 end if;
871 -- Likewise for No_Local_Timing_Events
873 if Has_Timing_Event (Designated_Type (Acc_Type))
874 and then not Is_Library_Level_Entity (Acc_Type)
875 then
876 Check_Restriction (No_Local_Timing_Events, N);
877 end if;
879 -- If the No_Streams restriction is set, check that the type of the
880 -- object is not, and does not contain, any subtype derived from
881 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
882 -- Has_Stream just for efficiency reasons. There is no point in
883 -- spending time on a Has_Stream check if the restriction is not set.
885 if Restriction_Check_Required (No_Streams) then
886 if Has_Stream (Designated_Type (Acc_Type)) then
887 Check_Restriction (No_Streams, N);
888 end if;
889 end if;
891 if not Is_Library_Level_Entity (Acc_Type) then
892 Check_Restriction (No_Local_Allocators, N);
893 end if;
895 <<Leave>>
896 if Serious_Errors_Detected > Sav_Errs then
897 Set_Error_Posted (N);
898 Set_Etype (N, Any_Type);
899 end if;
900 end Analyze_Allocator;
902 ---------------------------
903 -- Analyze_Arithmetic_Op --
904 ---------------------------
906 procedure Analyze_Arithmetic_Op (N : Node_Id) is
907 L : constant Node_Id := Left_Opnd (N);
908 R : constant Node_Id := Right_Opnd (N);
910 Op_Id : Entity_Id;
912 begin
913 Set_Etype (N, Any_Type);
914 Candidate_Type := Empty;
916 Analyze_Expression (L);
917 Analyze_Expression (R);
919 -- If the entity is already set, the node is the instantiation of a
920 -- generic node with a non-local reference, or was manufactured by a
921 -- call to Make_Op_xxx. In either case the entity is known to be valid,
922 -- and we do not need to collect interpretations, instead we just get
923 -- the single possible interpretation.
925 if Present (Entity (N)) then
926 Op_Id := Entity (N);
928 if Ekind (Op_Id) = E_Operator then
929 Find_Arithmetic_Types (L, R, Op_Id, N);
930 else
931 Add_One_Interp (N, Op_Id, Etype (Op_Id));
932 end if;
934 -- Entity is not already set, so we do need to collect interpretations
936 else
937 Op_Id := Get_Name_Entity_Id (Chars (N));
938 while Present (Op_Id) loop
939 if Ekind (Op_Id) = E_Operator
940 and then Present (Next_Entity (First_Entity (Op_Id)))
941 then
942 Find_Arithmetic_Types (L, R, Op_Id, N);
944 -- The following may seem superfluous, because an operator cannot
945 -- be generic, but this ignores the cleverness of the author of
946 -- ACVC bc1013a.
948 elsif Is_Overloadable (Op_Id) then
949 Analyze_User_Defined_Binary_Op (N, Op_Id);
950 end if;
952 Op_Id := Homonym (Op_Id);
953 end loop;
954 end if;
956 Operator_Check (N);
957 Check_Function_Writable_Actuals (N);
958 end Analyze_Arithmetic_Op;
960 ------------------
961 -- Analyze_Call --
962 ------------------
964 -- Function, procedure, and entry calls are checked here. The Name in
965 -- the call may be overloaded. The actuals have been analyzed and may
966 -- themselves be overloaded. On exit from this procedure, the node N
967 -- may have zero, one or more interpretations. In the first case an
968 -- error message is produced. In the last case, the node is flagged
969 -- as overloaded and the interpretations are collected in All_Interp.
971 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
972 -- the type-checking is similar to that of other calls.
974 procedure Analyze_Call (N : Node_Id) is
975 Actuals : constant List_Id := Parameter_Associations (N);
976 Loc : constant Source_Ptr := Sloc (N);
977 Nam : Node_Id;
978 X : Interp_Index;
979 It : Interp;
980 Nam_Ent : Entity_Id := Empty;
981 Success : Boolean := False;
983 Deref : Boolean := False;
984 -- Flag indicates whether an interpretation of the prefix is a
985 -- parameterless call that returns an access_to_subprogram.
987 procedure Check_Writable_Actuals (N : Node_Id);
988 -- If the call has out or in-out parameters then mark its outermost
989 -- enclosing construct as a node on which the writable actuals check
990 -- must be performed.
992 function Name_Denotes_Function return Boolean;
993 -- If the type of the name is an access to subprogram, this may be the
994 -- type of a name, or the return type of the function being called. If
995 -- the name is not an entity then it can denote a protected function.
996 -- Until we distinguish Etype from Return_Type, we must use this routine
997 -- to resolve the meaning of the name in the call.
999 procedure No_Interpretation;
1000 -- Output error message when no valid interpretation exists
1002 ----------------------------
1003 -- Check_Writable_Actuals --
1004 ----------------------------
1006 -- The identification of conflicts in calls to functions with writable
1007 -- actuals is performed in the analysis phase of the front end to ensure
1008 -- that it reports exactly the same errors compiling with and without
1009 -- expansion enabled. It is performed in two stages:
1011 -- 1) When a call to a function with out-mode parameters is found,
1012 -- we climb to the outermost enclosing construct that can be
1013 -- evaluated in arbitrary order and we mark it with the flag
1014 -- Check_Actuals.
1016 -- 2) When the analysis of the marked node is complete, we traverse
1017 -- its decorated subtree searching for conflicts (see function
1018 -- Sem_Util.Check_Function_Writable_Actuals).
1020 -- The unique exception to this general rule is for aggregates, since
1021 -- their analysis is performed by the front end in the resolution
1022 -- phase. For aggregates we do not climb to their enclosing construct:
1023 -- we restrict the analysis to the subexpressions initializing the
1024 -- aggregate components.
1026 -- This implies that the analysis of expressions containing aggregates
1027 -- is not complete, since there may be conflicts on writable actuals
1028 -- involving subexpressions of the enclosing logical or arithmetic
1029 -- expressions. However, we cannot wait and perform the analysis when
1030 -- the whole subtree is resolved, since the subtrees may be transformed,
1031 -- thus adding extra complexity and computation cost to identify and
1032 -- report exactly the same errors compiling with and without expansion
1033 -- enabled.
1035 procedure Check_Writable_Actuals (N : Node_Id) is
1036 begin
1037 if Comes_From_Source (N)
1038 and then Present (Get_Subprogram_Entity (N))
1039 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1040 then
1041 -- For procedures and entries there is no need to climb since
1042 -- we only need to check if the actuals of this call invoke
1043 -- functions whose out-mode parameters overlap.
1045 if Nkind (N) /= N_Function_Call then
1046 Set_Check_Actuals (N);
1048 -- For calls to functions we climb to the outermost enclosing
1049 -- construct where the out-mode actuals of this function may
1050 -- introduce conflicts.
1052 else
1053 declare
1054 Outermost : Node_Id := Empty; -- init to avoid warning
1055 P : Node_Id := N;
1057 begin
1058 while Present (P) loop
1059 -- For object declarations we can climb to the node from
1060 -- its object definition branch or from its initializing
1061 -- expression. We prefer to mark the child node as the
1062 -- outermost construct to avoid adding further complexity
1063 -- to the routine that will later take care of
1064 -- performing the writable actuals check.
1066 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1067 and then Nkind (P) not in
1068 N_Assignment_Statement | N_Object_Declaration
1069 then
1070 Outermost := P;
1071 end if;
1073 -- Avoid climbing more than needed
1075 exit when Stop_Subtree_Climbing (Nkind (P))
1076 or else (Nkind (P) = N_Range
1077 and then
1078 Nkind (Parent (P)) not in N_In | N_Not_In);
1080 P := Parent (P);
1081 end loop;
1083 Set_Check_Actuals (Outermost);
1084 end;
1085 end if;
1086 end if;
1087 end Check_Writable_Actuals;
1089 ---------------------------
1090 -- Name_Denotes_Function --
1091 ---------------------------
1093 function Name_Denotes_Function return Boolean is
1094 begin
1095 if Is_Entity_Name (Nam) then
1096 return Ekind (Entity (Nam)) = E_Function;
1097 elsif Nkind (Nam) = N_Selected_Component then
1098 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1099 else
1100 return False;
1101 end if;
1102 end Name_Denotes_Function;
1104 -----------------------
1105 -- No_Interpretation --
1106 -----------------------
1108 procedure No_Interpretation is
1109 L : constant Boolean := Is_List_Member (N);
1110 K : constant Node_Kind := Nkind (Parent (N));
1112 begin
1113 -- If the node is in a list whose parent is not an expression then it
1114 -- must be an attempted procedure call.
1116 if L and then K not in N_Subexpr then
1117 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1118 Error_Msg_NE
1119 ("must instantiate generic procedure& before call",
1120 Nam, Entity (Nam));
1121 else
1122 Error_Msg_N ("procedure or entry name expected", Nam);
1123 end if;
1125 -- Check for tasking cases where only an entry call will do
1127 elsif not L
1128 and then K in N_Entry_Call_Alternative | N_Triggering_Alternative
1129 then
1130 Error_Msg_N ("entry name expected", Nam);
1132 -- Otherwise give general error message
1134 else
1135 Error_Msg_N ("invalid prefix in call", Nam);
1136 end if;
1137 end No_Interpretation;
1139 -- Start of processing for Analyze_Call
1141 begin
1142 -- Initialize the type of the result of the call to the error type,
1143 -- which will be reset if the type is successfully resolved.
1145 Set_Etype (N, Any_Type);
1147 Nam := Name (N);
1149 if not Is_Overloaded (Nam) then
1151 -- Only one interpretation to check
1153 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1154 Nam_Ent := Etype (Nam);
1156 -- If the prefix is an access_to_subprogram, this may be an indirect
1157 -- call. This is the case if the name in the call is not an entity
1158 -- name, or if it is a function name in the context of a procedure
1159 -- call. In this latter case, we have a call to a parameterless
1160 -- function that returns a pointer_to_procedure which is the entity
1161 -- being called. Finally, F (X) may be a call to a parameterless
1162 -- function that returns a pointer to a function with parameters.
1163 -- Note that if F returns an access-to-subprogram whose designated
1164 -- type is an array, F (X) cannot be interpreted as an indirect call
1165 -- through the result of the call to F.
1167 elsif Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
1168 and then
1169 (not Name_Denotes_Function
1170 or else Nkind (N) = N_Procedure_Call_Statement
1171 or else
1172 (Nkind (Parent (N)) /= N_Explicit_Dereference
1173 and then Is_Entity_Name (Nam)
1174 and then No (First_Formal (Entity (Nam)))
1175 and then not
1176 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1177 and then Present (Actuals)))
1178 then
1179 Nam_Ent := Designated_Type (Etype (Nam));
1180 Insert_Explicit_Dereference (Nam);
1182 -- Selected component case. Simple entry or protected operation,
1183 -- where the entry name is given by the selector name.
1185 elsif Nkind (Nam) = N_Selected_Component then
1186 Nam_Ent := Entity (Selector_Name (Nam));
1188 if Ekind (Nam_Ent) not in E_Entry
1189 | E_Entry_Family
1190 | E_Function
1191 | E_Procedure
1192 then
1193 Error_Msg_N ("name in call is not a callable entity", Nam);
1194 Set_Etype (N, Any_Type);
1195 return;
1196 end if;
1198 -- If the name is an Indexed component, it can be a call to a member
1199 -- of an entry family. The prefix must be a selected component whose
1200 -- selector is the entry. Analyze_Procedure_Call normalizes several
1201 -- kinds of call into this form.
1203 elsif Nkind (Nam) = N_Indexed_Component then
1204 if Nkind (Prefix (Nam)) = N_Selected_Component then
1205 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1206 else
1207 Error_Msg_N ("name in call is not a callable entity", Nam);
1208 Set_Etype (N, Any_Type);
1209 return;
1210 end if;
1212 elsif not Is_Entity_Name (Nam) then
1213 Error_Msg_N ("name in call is not a callable entity", Nam);
1214 Set_Etype (N, Any_Type);
1215 return;
1217 else
1218 Nam_Ent := Entity (Nam);
1220 -- If not overloadable, this may be a generalized indexing
1221 -- operation with named associations. Rewrite again as an
1222 -- indexed component and analyze as container indexing.
1224 if not Is_Overloadable (Nam_Ent) then
1225 if Present
1226 (Find_Value_Of_Aspect
1227 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1228 then
1229 Replace (N,
1230 Make_Indexed_Component (Sloc (N),
1231 Prefix => Nam,
1232 Expressions => Parameter_Associations (N)));
1234 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1235 return;
1236 else
1237 No_Interpretation;
1238 end if;
1240 else
1241 No_Interpretation;
1242 end if;
1244 return;
1245 end if;
1246 end if;
1248 -- Operations generated for RACW stub types are called only through
1249 -- dispatching, and can never be the static interpretation of a call.
1251 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1252 No_Interpretation;
1253 return;
1254 end if;
1256 Analyze_One_Call (N, Nam_Ent, True, Success);
1258 -- If the nonoverloaded interpretation is a call to an abstract
1259 -- nondispatching operation, then flag an error and return.
1261 if Is_Overloadable (Nam_Ent)
1262 and then Is_Abstract_Subprogram (Nam_Ent)
1263 and then not Is_Dispatching_Operation (Nam_Ent)
1264 then
1265 Nondispatching_Call_To_Abstract_Operation (N, Nam_Ent);
1266 return;
1267 end if;
1269 -- If this is an indirect call, the return type of the access_to
1270 -- subprogram may be an incomplete type. At the point of the call,
1271 -- use the full type if available, and at the same time update the
1272 -- return type of the access_to_subprogram.
1274 if Success
1275 and then Nkind (Nam) = N_Explicit_Dereference
1276 and then Ekind (Etype (N)) = E_Incomplete_Type
1277 and then Present (Full_View (Etype (N)))
1278 then
1279 Set_Etype (N, Full_View (Etype (N)));
1280 Set_Etype (Nam_Ent, Etype (N));
1281 end if;
1283 -- Overloaded call
1285 else
1286 -- An overloaded selected component must denote overloaded operations
1287 -- of a concurrent type. The interpretations are attached to the
1288 -- simple name of those operations.
1290 if Nkind (Nam) = N_Selected_Component then
1291 Nam := Selector_Name (Nam);
1292 end if;
1294 Get_First_Interp (Nam, X, It);
1295 while Present (It.Nam) loop
1296 Nam_Ent := It.Nam;
1297 Deref := False;
1299 -- Name may be call that returns an access to subprogram, or more
1300 -- generally an overloaded expression one of whose interpretations
1301 -- yields an access to subprogram. If the name is an entity, we do
1302 -- not dereference, because the node is a call that returns the
1303 -- access type: note difference between f(x), where the call may
1304 -- return an access subprogram type, and f(x)(y), where the type
1305 -- returned by the call to f is implicitly dereferenced to analyze
1306 -- the outer call.
1308 if Is_Access_Type (Nam_Ent) then
1309 Nam_Ent := Designated_Type (Nam_Ent);
1311 elsif Is_Access_Type (Etype (Nam_Ent))
1312 and then
1313 (not Is_Entity_Name (Nam)
1314 or else Nkind (N) = N_Procedure_Call_Statement)
1315 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1316 = E_Subprogram_Type
1317 then
1318 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1320 if Is_Entity_Name (Nam) then
1321 Deref := True;
1322 end if;
1323 end if;
1325 -- If the call has been rewritten from a prefixed call, the first
1326 -- parameter has been analyzed, but may need a subsequent
1327 -- dereference, so skip its analysis now.
1329 if Is_Rewrite_Substitution (N)
1330 and then Nkind (Original_Node (N)) = Nkind (N)
1331 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1332 and then Present (Parameter_Associations (N))
1333 and then Present (Etype (First (Parameter_Associations (N))))
1334 then
1335 Analyze_One_Call
1336 (N, Nam_Ent, False, Success, Skip_First => True);
1337 else
1338 Analyze_One_Call (N, Nam_Ent, False, Success);
1339 end if;
1341 -- If the interpretation succeeds, mark the proper type of the
1342 -- prefix (any valid candidate will do). If not, remove the
1343 -- candidate interpretation. If this is a parameterless call
1344 -- on an anonymous access to subprogram, X is a variable with
1345 -- an access discriminant D, the entity in the interpretation is
1346 -- D, so rewrite X as X.D.all.
1348 if Success then
1349 if Deref
1350 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1351 then
1352 if Ekind (It.Nam) = E_Discriminant
1353 and then Has_Implicit_Dereference (It.Nam)
1354 then
1355 Rewrite (Name (N),
1356 Make_Explicit_Dereference (Loc,
1357 Prefix =>
1358 Make_Selected_Component (Loc,
1359 Prefix =>
1360 New_Occurrence_Of (Entity (Nam), Loc),
1361 Selector_Name =>
1362 New_Occurrence_Of (It.Nam, Loc))));
1364 Analyze (N);
1365 return;
1367 else
1368 Set_Entity (Nam, It.Nam);
1369 Insert_Explicit_Dereference (Nam);
1370 Set_Etype (Nam, Nam_Ent);
1371 end if;
1373 else
1374 Set_Etype (Nam, It.Typ);
1375 end if;
1377 elsif Nkind (Name (N)) in N_Function_Call | N_Selected_Component
1378 then
1379 Remove_Interp (X);
1380 end if;
1382 Get_Next_Interp (X, It);
1383 end loop;
1385 -- If the name is the result of a function call, it can only be a
1386 -- call to a function returning an access to subprogram. Insert
1387 -- explicit dereference.
1389 if Nkind (Nam) = N_Function_Call then
1390 Insert_Explicit_Dereference (Nam);
1391 end if;
1393 if Etype (N) = Any_Type then
1395 -- None of the interpretations is compatible with the actuals
1397 Diagnose_Call (N, Nam);
1399 -- Special checks for uninstantiated put routines
1401 if Nkind (N) = N_Procedure_Call_Statement
1402 and then Is_Entity_Name (Nam)
1403 and then Chars (Nam) = Name_Put
1404 and then List_Length (Actuals) = 1
1405 then
1406 declare
1407 Arg : constant Node_Id := First (Actuals);
1408 Typ : Entity_Id;
1410 begin
1411 if Nkind (Arg) = N_Parameter_Association then
1412 Typ := Etype (Explicit_Actual_Parameter (Arg));
1413 else
1414 Typ := Etype (Arg);
1415 end if;
1417 if Is_Signed_Integer_Type (Typ) then
1418 Error_Msg_N
1419 ("possible missing instantiation of "
1420 & "'Text_'I'O.'Integer_'I'O!", Nam);
1422 elsif Is_Modular_Integer_Type (Typ) then
1423 Error_Msg_N
1424 ("possible missing instantiation of "
1425 & "'Text_'I'O.'Modular_'I'O!", Nam);
1427 elsif Is_Floating_Point_Type (Typ) then
1428 Error_Msg_N
1429 ("possible missing instantiation of "
1430 & "'Text_'I'O.'Float_'I'O!", Nam);
1432 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1433 Error_Msg_N
1434 ("possible missing instantiation of "
1435 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1437 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1438 Error_Msg_N
1439 ("possible missing instantiation of "
1440 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1442 elsif Is_Enumeration_Type (Typ) then
1443 Error_Msg_N
1444 ("possible missing instantiation of "
1445 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1446 end if;
1447 end;
1448 end if;
1450 elsif not Is_Overloaded (N)
1451 and then Is_Entity_Name (Nam)
1452 then
1453 -- Resolution yields a single interpretation. Verify that the
1454 -- reference has capitalization consistent with the declaration.
1456 Set_Entity_With_Checks (Nam, Entity (Nam));
1457 Generate_Reference (Entity (Nam), Nam);
1459 Set_Etype (Nam, Etype (Entity (Nam)));
1460 else
1461 Remove_Abstract_Operations (N);
1462 end if;
1463 end if;
1465 -- Check the accessibility level for actuals for explicitly aliased
1466 -- formals when a function call appears within a return statement.
1467 -- This is only checked if the enclosing subprogram Comes_From_Source,
1468 -- to avoid issuing errors on calls occurring in wrapper subprograms
1469 -- (for example, where the call is part of an expression of an aspect
1470 -- associated with a wrapper, such as Pre'Class).
1472 if Nkind (N) = N_Function_Call
1473 and then Comes_From_Source (N)
1474 and then Present (Nam_Ent)
1475 and then In_Return_Value (N)
1476 and then Comes_From_Source (Current_Subprogram)
1477 then
1478 declare
1479 Form : Node_Id;
1480 Act : Node_Id;
1481 begin
1482 Act := First_Actual (N);
1483 Form := First_Formal (Nam_Ent);
1485 while Present (Form) and then Present (Act) loop
1486 -- Check whether the formal is aliased and if the accessibility
1487 -- level of the actual is deeper than the accessibility level
1488 -- of the enclosing subprogram to which the current return
1489 -- statement applies.
1491 -- Should we be checking Is_Entity_Name on Act? Won't this miss
1492 -- other cases ???
1494 if Is_Explicitly_Aliased (Form)
1495 and then Is_Entity_Name (Act)
1496 and then Static_Accessibility_Level
1497 (Act, Zero_On_Dynamic_Level)
1498 > Subprogram_Access_Level (Current_Subprogram)
1499 then
1500 Error_Msg_N ("actual for explicitly aliased formal is too"
1501 & " short lived", Act);
1502 end if;
1504 Next_Formal (Form);
1505 Next_Actual (Act);
1506 end loop;
1507 end;
1508 end if;
1510 if Ada_Version >= Ada_2012 then
1512 -- Check if the call contains a function with writable actuals
1514 Check_Writable_Actuals (N);
1516 -- If found and the outermost construct that can be evaluated in
1517 -- an arbitrary order is precisely this call, then check all its
1518 -- actuals.
1520 Check_Function_Writable_Actuals (N);
1522 -- The return type of the function may be incomplete. This can be
1523 -- the case if the type is a generic formal, or a limited view. It
1524 -- can also happen when the function declaration appears before the
1525 -- full view of the type (which is legal in Ada 2012) and the call
1526 -- appears in a different unit, in which case the incomplete view
1527 -- must be replaced with the full view (or the nonlimited view)
1528 -- to prevent subsequent type errors. Note that the usual install/
1529 -- removal of limited_with clauses is not sufficient to handle this
1530 -- case, because the limited view may have been captured in another
1531 -- compilation unit that defines the current function.
1533 if Is_Incomplete_Type (Etype (N)) then
1534 if Present (Full_View (Etype (N))) then
1535 if Is_Entity_Name (Nam) then
1536 Set_Etype (Nam, Full_View (Etype (N)));
1537 Set_Etype (Entity (Nam), Full_View (Etype (N)));
1538 end if;
1540 Set_Etype (N, Full_View (Etype (N)));
1542 elsif From_Limited_With (Etype (N))
1543 and then Present (Non_Limited_View (Etype (N)))
1544 then
1545 Set_Etype (N, Non_Limited_View (Etype (N)));
1547 -- If there is no completion for the type, this may be because
1548 -- there is only a limited view of it and there is nothing in
1549 -- the context of the current unit that has required a regular
1550 -- compilation of the unit containing the type. We recognize
1551 -- this unusual case by the fact that unit is not analyzed.
1552 -- Note that the call being analyzed is in a different unit from
1553 -- the function declaration, and nothing indicates that the type
1554 -- is a limited view.
1556 elsif Ekind (Scope (Etype (N))) = E_Package
1557 and then Present (Limited_View (Scope (Etype (N))))
1558 and then not Analyzed (Unit_Declaration_Node (Scope (Etype (N))))
1559 then
1560 Error_Msg_NE
1561 ("cannot call function that returns limited view of}",
1562 N, Etype (N));
1564 Error_Msg_NE
1565 ("\there must be a regular with_clause for package & in the "
1566 & "current unit, or in some unit in its context",
1567 N, Scope (Etype (N)));
1569 Set_Etype (N, Any_Type);
1570 end if;
1571 end if;
1572 end if;
1573 end Analyze_Call;
1575 -----------------------------
1576 -- Analyze_Case_Expression --
1577 -----------------------------
1579 procedure Analyze_Case_Expression (N : Node_Id) is
1580 Expr : constant Node_Id := Expression (N);
1581 First_Alt : constant Node_Id := First (Alternatives (N));
1583 First_Expr : Node_Id := Empty;
1584 -- First expression in the case where there is some type information
1585 -- available, i.e. there is not Any_Type everywhere, which can happen
1586 -- because of some error.
1588 Second_Expr : Node_Id := Empty;
1589 -- Second expression as above
1591 Wrong_Alt : Node_Id := Empty;
1592 -- For error reporting
1594 procedure Non_Static_Choice_Error (Choice : Node_Id);
1595 -- Error routine invoked by the generic instantiation below when
1596 -- the case expression has a non static choice.
1598 procedure Check_Next_Expression (T : Entity_Id; Alt : Node_Id);
1599 -- Check one interpretation of the next expression with type T
1601 procedure Check_Expression_Pair (T1, T2 : Entity_Id; Alt : Node_Id);
1602 -- Check first expression with type T1 and next expression with type T2
1604 package Case_Choices_Analysis is new
1605 Generic_Analyze_Choices
1606 (Process_Associated_Node => No_OP);
1607 use Case_Choices_Analysis;
1609 package Case_Choices_Checking is new
1610 Generic_Check_Choices
1611 (Process_Empty_Choice => No_OP,
1612 Process_Non_Static_Choice => Non_Static_Choice_Error,
1613 Process_Associated_Node => No_OP);
1614 use Case_Choices_Checking;
1616 -----------------------------
1617 -- Non_Static_Choice_Error --
1618 -----------------------------
1620 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1621 begin
1622 Flag_Non_Static_Expr
1623 ("choice given in case expression is not static!", Choice);
1624 end Non_Static_Choice_Error;
1626 ---------------------------
1627 -- Check_Next_Expression --
1628 ---------------------------
1630 procedure Check_Next_Expression (T : Entity_Id; Alt : Node_Id) is
1631 Next_Expr : constant Node_Id := Expression (Alt);
1633 I : Interp_Index;
1634 It : Interp;
1636 begin
1637 if Next_Expr = First_Expr then
1638 Check_Next_Expression (T, Next (Alt));
1639 return;
1640 end if;
1642 -- Loop through the interpretations of the next expression
1644 if not Is_Overloaded (Next_Expr) then
1645 Check_Expression_Pair (T, Etype (Next_Expr), Alt);
1647 else
1648 Get_First_Interp (Next_Expr, I, It);
1649 while Present (It.Typ) loop
1650 Check_Expression_Pair (T, It.Typ, Alt);
1651 Get_Next_Interp (I, It);
1652 end loop;
1653 end if;
1654 end Check_Next_Expression;
1656 ---------------------------
1657 -- Check_Expression_Pair --
1658 ---------------------------
1660 procedure Check_Expression_Pair (T1, T2 : Entity_Id; Alt : Node_Id) is
1661 Next_Expr : constant Node_Id := Expression (Alt);
1663 T : Entity_Id;
1665 begin
1666 if Covers (T1 => T1, T2 => T2)
1667 or else Covers (T1 => T2, T2 => T1)
1668 then
1669 T := Specific_Type (T1, T2);
1671 elsif Is_User_Defined_Literal (First_Expr, T2) then
1672 T := T2;
1674 elsif Is_User_Defined_Literal (Next_Expr, T1) then
1675 T := T1;
1677 else
1678 T := Possible_Type_For_Conditional_Expression (T1, T2);
1680 if No (T) then
1681 Wrong_Alt := Alt;
1682 return;
1683 end if;
1684 end if;
1686 if Present (Next (Alt)) then
1687 Check_Next_Expression (T, Next (Alt));
1688 else
1689 Add_One_Interp (N, T, T);
1690 end if;
1691 end Check_Expression_Pair;
1693 -- Local variables
1695 Alt : Node_Id;
1696 Exp_Type : Entity_Id;
1697 Exp_Btype : Entity_Id;
1698 I : Interp_Index;
1699 It : Interp;
1700 Others_Present : Boolean;
1702 -- Start of processing for Analyze_Case_Expression
1704 begin
1705 Analyze_And_Resolve (Expr, Any_Discrete);
1706 Check_Unset_Reference (Expr);
1707 Exp_Type := Etype (Expr);
1708 Exp_Btype := Base_Type (Exp_Type);
1710 Set_Etype (N, Any_Type);
1712 Alt := First_Alt;
1713 while Present (Alt) loop
1714 if Error_Posted (Expression (Alt)) then
1715 return;
1716 end if;
1718 Analyze_Expression (Expression (Alt));
1720 if Etype (Expression (Alt)) /= Any_Type then
1721 if No (First_Expr) then
1722 First_Expr := Expression (Alt);
1724 elsif No (Second_Expr) then
1725 Second_Expr := Expression (Alt);
1726 end if;
1727 end if;
1729 Next (Alt);
1730 end loop;
1732 -- Get our initial type from the first expression for which we got some
1733 -- useful type information from the expression.
1735 if No (First_Expr) then
1736 return;
1737 end if;
1739 -- The expression must be of a discrete type which must be determinable
1740 -- independently of the context in which the expression occurs, but
1741 -- using the fact that the expression must be of a discrete type.
1742 -- Moreover, the type this expression must not be a character literal
1743 -- (which is always ambiguous).
1745 -- If error already reported by Resolve, nothing more to do
1747 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1748 return;
1750 -- Special case message for character literal
1752 elsif Exp_Btype = Any_Character then
1753 Error_Msg_N
1754 ("character literal as case expression is ambiguous", Expr);
1755 return;
1756 end if;
1758 -- If the case expression is a formal object of mode in out, then
1759 -- treat it as having a nonstatic subtype by forcing use of the base
1760 -- type (which has to get passed to Check_Case_Choices below). Also
1761 -- use base type when the case expression is parenthesized.
1763 if Paren_Count (Expr) > 0
1764 or else (Is_Entity_Name (Expr)
1765 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1766 then
1767 Exp_Type := Exp_Btype;
1768 end if;
1770 -- The case expression alternatives cover the range of a static subtype
1771 -- subject to aspect Static_Predicate. Do not check the choices when the
1772 -- case expression has not been fully analyzed yet because this may lead
1773 -- to bogus errors.
1775 if Is_OK_Static_Subtype (Exp_Type)
1776 and then Has_Static_Predicate_Aspect (Exp_Type)
1777 and then In_Spec_Expression
1778 then
1779 null;
1781 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1783 else
1784 Analyze_Choices (Alternatives (N), Exp_Type);
1785 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1787 if Exp_Type = Universal_Integer and then not Others_Present then
1788 Error_Msg_N
1789 ("case on universal integer requires OTHERS choice", Expr);
1790 return;
1791 end if;
1792 end if;
1794 -- RM 4.5.7(10/3): If the case_expression is the operand of a type
1795 -- conversion, the type of the case_expression is the target type
1796 -- of the conversion.
1798 if Nkind (Parent (N)) = N_Type_Conversion then
1799 Set_Etype (N, Etype (Parent (N)));
1800 return;
1801 end if;
1803 -- Loop through the interpretations of the first expression and check
1804 -- the other expressions if present.
1806 if not Is_Overloaded (First_Expr) then
1807 if Present (Second_Expr) then
1808 Check_Next_Expression (Etype (First_Expr), First_Alt);
1809 else
1810 Set_Etype (N, Etype (First_Expr));
1811 end if;
1813 else
1814 Get_First_Interp (First_Expr, I, It);
1815 while Present (It.Typ) loop
1816 if Present (Second_Expr) then
1817 Check_Next_Expression (It.Typ, First_Alt);
1818 else
1819 Add_One_Interp (N, It.Typ, It.Typ);
1820 end if;
1822 Get_Next_Interp (I, It);
1823 end loop;
1824 end if;
1826 -- If no possible interpretation has been found, the type of the wrong
1827 -- alternative doesn't match any interpretation of the FIRST expression.
1829 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1830 Second_Expr := Expression (Wrong_Alt);
1832 if Is_Overloaded (First_Expr) then
1833 if Is_Overloaded (Second_Expr) then
1834 Error_Msg_N
1835 ("no interpretation compatible with those of previous "
1836 & "alternative",
1837 Second_Expr);
1838 else
1839 Error_Msg_N
1840 ("type incompatible with interpretations of previous "
1841 & "alternative",
1842 Second_Expr);
1843 Error_Msg_NE
1844 ("\this alternative has}!",
1845 Second_Expr,
1846 Etype (Second_Expr));
1847 end if;
1849 else
1850 if Is_Overloaded (Second_Expr) then
1851 Error_Msg_N
1852 ("no interpretation compatible with type of previous "
1853 & "alternative",
1854 Second_Expr);
1855 Error_Msg_NE
1856 ("\previous alternative has}!",
1857 Second_Expr,
1858 Etype (First_Expr));
1859 else
1860 Error_Msg_N
1861 ("type incompatible with that of previous alternative",
1862 Second_Expr);
1863 Error_Msg_NE
1864 ("\previous alternative has}!",
1865 Second_Expr,
1866 Etype (First_Expr));
1867 Error_Msg_NE
1868 ("\this alternative has}!",
1869 Second_Expr,
1870 Etype (Second_Expr));
1871 end if;
1872 end if;
1873 end if;
1874 end Analyze_Case_Expression;
1876 ---------------------------
1877 -- Analyze_Concatenation --
1878 ---------------------------
1880 procedure Analyze_Concatenation (N : Node_Id) is
1882 -- We wish to avoid deep recursion, because concatenations are often
1883 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1884 -- operands nonrecursively until we find something that is not a
1885 -- concatenation (A in this case), or has already been analyzed. We
1886 -- analyze that, and then walk back up the tree following Parent
1887 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1888 -- work at each level. The Parent pointers allow us to avoid recursion,
1889 -- and thus avoid running out of memory.
1891 NN : Node_Id := N;
1892 L : Node_Id;
1894 begin
1895 Candidate_Type := Empty;
1897 -- The following code is equivalent to:
1899 -- Set_Etype (N, Any_Type);
1900 -- Analyze_Expression (Left_Opnd (N));
1901 -- Analyze_Concatenation_Rest (N);
1903 -- where the Analyze_Expression call recurses back here if the left
1904 -- operand is a concatenation.
1906 -- Walk down left operands
1908 loop
1909 Set_Etype (NN, Any_Type);
1910 L := Left_Opnd (NN);
1911 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1912 NN := L;
1913 end loop;
1915 -- Now (given the above example) NN is A&B and L is A
1917 -- First analyze L ...
1919 Analyze_Expression (L);
1921 -- ... then walk NN back up until we reach N (where we started), calling
1922 -- Analyze_Concatenation_Rest along the way.
1924 loop
1925 Analyze_Concatenation_Rest (NN);
1926 exit when NN = N;
1927 NN := Parent (NN);
1928 end loop;
1929 end Analyze_Concatenation;
1931 --------------------------------
1932 -- Analyze_Concatenation_Rest --
1933 --------------------------------
1935 -- If the only one-dimensional array type in scope is String,
1936 -- this is the resulting type of the operation. Otherwise there
1937 -- will be a concatenation operation defined for each user-defined
1938 -- one-dimensional array.
1940 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1941 L : constant Node_Id := Left_Opnd (N);
1942 R : constant Node_Id := Right_Opnd (N);
1943 Op_Id : Entity_Id := Entity (N);
1944 LT : Entity_Id;
1945 RT : Entity_Id;
1947 begin
1948 Analyze_Expression (R);
1950 -- If the entity is present, the node appears in an instance, and
1951 -- denotes a predefined concatenation operation. The resulting type is
1952 -- obtained from the arguments when possible. If the arguments are
1953 -- aggregates, the array type and the concatenation type must be
1954 -- visible.
1956 if Present (Op_Id) then
1957 if Ekind (Op_Id) = E_Operator then
1958 LT := Base_Type (Etype (L));
1959 RT := Base_Type (Etype (R));
1961 if Is_Array_Type (LT)
1962 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1963 then
1964 Add_One_Interp (N, Op_Id, LT);
1966 elsif Is_Array_Type (RT)
1967 and then LT = Base_Type (Component_Type (RT))
1968 then
1969 Add_One_Interp (N, Op_Id, RT);
1971 -- If one operand is a string type or a user-defined array type,
1972 -- and the other is a literal, result is of the specific type.
1974 elsif
1975 (Root_Type (LT) = Standard_String
1976 or else Scope (LT) /= Standard_Standard)
1977 and then Etype (R) = Any_String
1978 then
1979 Add_One_Interp (N, Op_Id, LT);
1981 elsif
1982 (Root_Type (RT) = Standard_String
1983 or else Scope (RT) /= Standard_Standard)
1984 and then Etype (L) = Any_String
1985 then
1986 Add_One_Interp (N, Op_Id, RT);
1988 elsif not Is_Generic_Type (Etype (Op_Id)) then
1989 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1991 else
1992 -- Type and its operations must be visible
1994 Set_Entity (N, Empty);
1995 Analyze_Concatenation (N);
1996 end if;
1998 else
1999 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2000 end if;
2002 else
2003 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
2004 while Present (Op_Id) loop
2005 if Ekind (Op_Id) = E_Operator then
2007 -- Do not consider operators declared in dead code, they
2008 -- cannot be part of the resolution.
2010 if Is_Eliminated (Op_Id) then
2011 null;
2012 else
2013 Find_Concatenation_Types (L, R, Op_Id, N);
2014 end if;
2016 else
2017 Analyze_User_Defined_Binary_Op (N, Op_Id);
2018 end if;
2020 Op_Id := Homonym (Op_Id);
2021 end loop;
2022 end if;
2024 Operator_Check (N);
2025 end Analyze_Concatenation_Rest;
2027 ------------------------------------
2028 -- Analyze_Comparison_Equality_Op --
2029 ------------------------------------
2031 procedure Analyze_Comparison_Equality_Op (N : Node_Id) is
2032 Loc : constant Source_Ptr := Sloc (N);
2033 L : constant Node_Id := Left_Opnd (N);
2034 R : constant Node_Id := Right_Opnd (N);
2036 Op_Id : Entity_Id;
2038 begin
2039 Set_Etype (N, Any_Type);
2040 Candidate_Type := Empty;
2042 Analyze_Expression (L);
2043 Analyze_Expression (R);
2045 -- If the entity is set, the node is a generic instance with a non-local
2046 -- reference to the predefined operator or to a user-defined function.
2047 -- It can also be an inequality that is expanded into the negation of a
2048 -- call to a user-defined equality operator.
2050 -- For the predefined case, the result is Boolean, regardless of the
2051 -- type of the operands. The operands may even be limited, if they are
2052 -- generic actuals. If they are overloaded, label the operands with the
2053 -- common type that must be present, or with the type of the formal of
2054 -- the user-defined function.
2056 if Present (Entity (N)) then
2057 Op_Id := Entity (N);
2059 if Ekind (Op_Id) = E_Operator then
2060 Add_One_Interp (N, Op_Id, Standard_Boolean);
2061 else
2062 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2063 end if;
2065 if Is_Overloaded (L) then
2066 if Ekind (Op_Id) = E_Operator then
2067 Set_Etype (L, Intersect_Types (L, R));
2068 else
2069 Set_Etype (L, Etype (First_Formal (Op_Id)));
2070 end if;
2071 end if;
2073 if Is_Overloaded (R) then
2074 if Ekind (Op_Id) = E_Operator then
2075 Set_Etype (R, Intersect_Types (L, R));
2076 else
2077 Set_Etype (R, Etype (Next_Formal (First_Formal (Op_Id))));
2078 end if;
2079 end if;
2081 else
2082 Op_Id := Get_Name_Entity_Id (Chars (N));
2084 while Present (Op_Id) loop
2085 if Ekind (Op_Id) = E_Operator then
2086 Find_Comparison_Equality_Types (L, R, Op_Id, N);
2087 else
2088 Analyze_User_Defined_Binary_Op (N, Op_Id);
2089 end if;
2091 Op_Id := Homonym (Op_Id);
2092 end loop;
2093 end if;
2095 -- If there was no match, and the operator is inequality, this may be
2096 -- a case where inequality has not been made explicit, as for tagged
2097 -- types. Analyze the node as the negation of an equality operation.
2098 -- This cannot be done earlier, because before analysis we cannot rule
2099 -- out the presence of an explicit inequality.
2101 if Etype (N) = Any_Type
2102 and then Nkind (N) = N_Op_Ne
2103 then
2104 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
2105 while Present (Op_Id) loop
2106 if Ekind (Op_Id) = E_Operator then
2107 Find_Comparison_Equality_Types (L, R, Op_Id, N);
2108 else
2109 Analyze_User_Defined_Binary_Op (N, Op_Id);
2110 end if;
2112 Op_Id := Homonym (Op_Id);
2113 end loop;
2115 if Etype (N) /= Any_Type then
2116 Op_Id := Entity (N);
2118 Rewrite (N,
2119 Make_Op_Not (Loc,
2120 Right_Opnd =>
2121 Make_Op_Eq (Loc,
2122 Left_Opnd => Left_Opnd (N),
2123 Right_Opnd => Right_Opnd (N))));
2125 Set_Entity (Right_Opnd (N), Op_Id);
2126 Analyze (N);
2127 end if;
2128 end if;
2130 Operator_Check (N);
2131 Check_Function_Writable_Actuals (N);
2132 end Analyze_Comparison_Equality_Op;
2134 ----------------------------------
2135 -- Analyze_Explicit_Dereference --
2136 ----------------------------------
2138 procedure Analyze_Explicit_Dereference (N : Node_Id) is
2139 Loc : constant Source_Ptr := Sloc (N);
2140 P : constant Node_Id := Prefix (N);
2141 T : Entity_Id;
2142 I : Interp_Index;
2143 It : Interp;
2144 New_N : Node_Id;
2146 function Is_Function_Type return Boolean;
2147 -- Check whether node may be interpreted as an implicit function call
2149 ----------------------
2150 -- Is_Function_Type --
2151 ----------------------
2153 function Is_Function_Type return Boolean is
2154 I : Interp_Index;
2155 It : Interp;
2157 begin
2158 if not Is_Overloaded (N) then
2159 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
2160 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
2162 else
2163 Get_First_Interp (N, I, It);
2164 while Present (It.Nam) loop
2165 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
2166 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
2167 then
2168 return False;
2169 end if;
2171 Get_Next_Interp (I, It);
2172 end loop;
2174 return True;
2175 end if;
2176 end Is_Function_Type;
2178 -- Start of processing for Analyze_Explicit_Dereference
2180 begin
2181 -- In formal verification mode, keep track of all reads and writes
2182 -- through explicit dereferences.
2184 if GNATprove_Mode then
2185 SPARK_Specific.Generate_Dereference (N);
2186 end if;
2188 Analyze (P);
2189 Set_Etype (N, Any_Type);
2191 -- Test for remote access to subprogram type, and if so return
2192 -- after rewriting the original tree.
2194 if Remote_AST_E_Dereference (P) then
2195 return;
2196 end if;
2198 -- Normal processing for other than remote access to subprogram type
2200 if not Is_Overloaded (P) then
2201 if Is_Access_Type (Etype (P)) then
2203 -- Set the Etype
2205 declare
2206 DT : constant Entity_Id := Designated_Type (Etype (P));
2208 begin
2209 -- An explicit dereference is a legal occurrence of an
2210 -- incomplete type imported through a limited_with clause, if
2211 -- the full view is visible, or if we are within an instance
2212 -- body, where the enclosing body has a regular with_clause
2213 -- on the unit.
2215 if From_Limited_With (DT)
2216 and then not From_Limited_With (Scope (DT))
2217 and then
2218 (Is_Immediately_Visible (Scope (DT))
2219 or else
2220 (Is_Child_Unit (Scope (DT))
2221 and then Is_Visible_Lib_Unit (Scope (DT)))
2222 or else In_Instance_Body)
2223 then
2224 Set_Etype (N, Available_View (DT));
2226 else
2227 Set_Etype (N, DT);
2228 end if;
2229 end;
2231 elsif Etype (P) /= Any_Type then
2232 Error_Msg_N ("prefix of dereference must be an access type", N);
2233 return;
2234 end if;
2236 else
2237 Get_First_Interp (P, I, It);
2238 while Present (It.Nam) loop
2239 T := It.Typ;
2241 if Is_Access_Type (T) then
2242 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2243 end if;
2245 Get_Next_Interp (I, It);
2246 end loop;
2248 -- Error if no interpretation of the prefix has an access type
2250 if Etype (N) = Any_Type then
2251 Error_Msg_N
2252 ("access type required in prefix of explicit dereference", P);
2253 Set_Etype (N, Any_Type);
2254 return;
2255 end if;
2256 end if;
2258 if Is_Function_Type
2259 and then Nkind (Parent (N)) /= N_Indexed_Component
2261 and then (Nkind (Parent (N)) /= N_Function_Call
2262 or else N /= Name (Parent (N)))
2264 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2265 or else N /= Name (Parent (N)))
2267 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2268 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2269 or else
2270 (Attribute_Name (Parent (N)) /= Name_Address
2271 and then
2272 Attribute_Name (Parent (N)) /= Name_Access))
2273 then
2274 -- Name is a function call with no actuals, in a context that
2275 -- requires deproceduring (including as an actual in an enclosing
2276 -- function or procedure call). There are some pathological cases
2277 -- where the prefix might include functions that return access to
2278 -- subprograms and others that return a regular type. Disambiguation
2279 -- of those has to take place in Resolve.
2281 New_N :=
2282 Make_Function_Call (Loc,
2283 Name => Make_Explicit_Dereference (Loc, P),
2284 Parameter_Associations => New_List);
2286 -- If the prefix is overloaded, remove operations that have formals,
2287 -- we know that this is a parameterless call.
2289 if Is_Overloaded (P) then
2290 Get_First_Interp (P, I, It);
2291 while Present (It.Nam) loop
2292 T := It.Typ;
2294 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2295 Set_Etype (P, T);
2296 else
2297 Remove_Interp (I);
2298 end if;
2300 Get_Next_Interp (I, It);
2301 end loop;
2302 end if;
2304 Rewrite (N, New_N);
2305 Analyze (N);
2307 elsif not Is_Function_Type
2308 and then Is_Overloaded (N)
2309 then
2310 -- The prefix may include access to subprograms and other access
2311 -- types. If the context selects the interpretation that is a
2312 -- function call (not a procedure call) we cannot rewrite the node
2313 -- yet, but we include the result of the call interpretation.
2315 Get_First_Interp (N, I, It);
2316 while Present (It.Nam) loop
2317 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2318 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2319 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2320 then
2321 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2322 end if;
2324 Get_Next_Interp (I, It);
2325 end loop;
2326 end if;
2328 -- A value of remote access-to-class-wide must not be dereferenced
2329 -- (RM E.2.2(16)).
2331 Validate_Remote_Access_To_Class_Wide_Type (N);
2332 end Analyze_Explicit_Dereference;
2334 ------------------------
2335 -- Analyze_Expression --
2336 ------------------------
2338 procedure Analyze_Expression (N : Node_Id) is
2339 begin
2340 -- If the expression is an indexed component that will be rewritten
2341 -- as a container indexing, it has already been analyzed.
2343 if Nkind (N) = N_Indexed_Component
2344 and then Present (Generalized_Indexing (N))
2345 then
2346 null;
2348 else
2349 Analyze (N);
2350 Check_Parameterless_Call (N);
2351 end if;
2352 end Analyze_Expression;
2354 -------------------------------------
2355 -- Analyze_Expression_With_Actions --
2356 -------------------------------------
2358 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2360 procedure Check_Action_OK (A : Node_Id);
2361 -- Check that the action A is allowed as a declare_item of a declare
2362 -- expression if N and A come from source.
2364 ---------------------
2365 -- Check_Action_OK --
2366 ---------------------
2368 procedure Check_Action_OK (A : Node_Id) is
2369 begin
2370 if not Comes_From_Source (N) or else not Comes_From_Source (A) then
2371 return; -- Allow anything in generated code
2372 end if;
2374 case Nkind (A) is
2375 when N_Object_Declaration =>
2376 if Nkind (Object_Definition (A)) = N_Access_Definition then
2377 Error_Msg_N
2378 ("anonymous access type not allowed in declare_expression",
2379 Object_Definition (A));
2380 end if;
2382 if Aliased_Present (A) then
2383 Error_Msg_N ("ALIASED not allowed in declare_expression", A);
2384 end if;
2386 if Constant_Present (A)
2387 and then not Is_Limited_Type (Etype (Defining_Identifier (A)))
2388 then
2389 return; -- nonlimited constants are OK
2390 end if;
2392 when N_Object_Renaming_Declaration =>
2393 if Present (Access_Definition (A)) then
2394 Error_Msg_N
2395 ("anonymous access type not allowed in declare_expression",
2396 Access_Definition (A));
2397 end if;
2399 if not Is_Limited_Type (Etype (Defining_Identifier (A))) then
2400 return; -- ???For now; the RM rule is a bit more complicated
2401 end if;
2403 when others =>
2404 null; -- Nothing else allowed, not even pragmas
2405 end case;
2407 Error_Msg_N ("object renaming or constant declaration expected", A);
2408 end Check_Action_OK;
2410 A : Node_Id;
2411 EWA_Scop : Entity_Id;
2413 -- Start of processing for Analyze_Expression_With_Actions
2415 begin
2416 -- Create a scope, which is needed to provide proper visibility of the
2417 -- declare_items.
2419 EWA_Scop := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
2420 Set_Etype (EWA_Scop, Standard_Void_Type);
2421 Set_Scope (EWA_Scop, Current_Scope);
2422 Set_Parent (EWA_Scop, N);
2423 Push_Scope (EWA_Scop);
2425 -- If this Expression_With_Actions node comes from source, then it
2426 -- represents a declare_expression; increment the counter to take note
2427 -- of that.
2429 if Comes_From_Source (N) then
2430 In_Declare_Expr := In_Declare_Expr + 1;
2431 end if;
2433 A := First (Actions (N));
2434 while Present (A) loop
2435 Analyze (A);
2436 Check_Action_OK (A);
2437 Next (A);
2438 end loop;
2440 Analyze_Expression (Expression (N));
2441 Set_Etype (N, Etype (Expression (N)));
2442 End_Scope;
2444 if Comes_From_Source (N) then
2445 In_Declare_Expr := In_Declare_Expr - 1;
2446 end if;
2447 end Analyze_Expression_With_Actions;
2449 ---------------------------
2450 -- Analyze_If_Expression --
2451 ---------------------------
2453 procedure Analyze_If_Expression (N : Node_Id) is
2454 Condition : constant Node_Id := First (Expressions (N));
2456 Then_Expr : Node_Id;
2457 Else_Expr : Node_Id;
2459 procedure Check_Else_Expression (T : Entity_Id);
2460 -- Check one interpretation of the THEN expression with type T
2462 procedure Check_Expression_Pair (T1, T2 : Entity_Id);
2463 -- Check THEN expression with type T1 and ELSE expression with type T2
2465 ---------------------------
2466 -- Check_Else_Expression --
2467 ---------------------------
2469 procedure Check_Else_Expression (T : Entity_Id) is
2470 I : Interp_Index;
2471 It : Interp;
2473 begin
2474 -- Loop through the interpretations of the ELSE expression
2476 if not Is_Overloaded (Else_Expr) then
2477 Check_Expression_Pair (T, Etype (Else_Expr));
2479 else
2480 Get_First_Interp (Else_Expr, I, It);
2481 while Present (It.Typ) loop
2482 Check_Expression_Pair (T, It.Typ);
2483 Get_Next_Interp (I, It);
2484 end loop;
2485 end if;
2486 end Check_Else_Expression;
2488 ---------------------------
2489 -- Check_Expression_Pair --
2490 ---------------------------
2492 procedure Check_Expression_Pair (T1, T2 : Entity_Id) is
2493 T : Entity_Id;
2495 begin
2496 if Covers (T1 => T1, T2 => T2)
2497 or else Covers (T1 => T2, T2 => T1)
2498 then
2499 T := Specific_Type (T1, T2);
2501 elsif Is_User_Defined_Literal (Then_Expr, T2) then
2502 T := T2;
2504 elsif Is_User_Defined_Literal (Else_Expr, T1) then
2505 T := T1;
2507 else
2508 T := Possible_Type_For_Conditional_Expression (T1, T2);
2510 if No (T) then
2511 return;
2512 end if;
2513 end if;
2515 Add_One_Interp (N, T, T);
2516 end Check_Expression_Pair;
2518 -- Local variables
2520 I : Interp_Index;
2521 It : Interp;
2523 -- Start of processing for Analyze_If_Expression
2525 begin
2526 -- Defend against error of missing expressions from previous error
2528 if No (Condition) then
2529 Check_Error_Detected;
2530 return;
2531 end if;
2533 Set_Etype (N, Any_Type);
2535 Then_Expr := Next (Condition);
2537 if No (Then_Expr) then
2538 Check_Error_Detected;
2539 return;
2540 end if;
2542 Else_Expr := Next (Then_Expr);
2544 -- Analyze and resolve the condition. We need to resolve this now so
2545 -- that it gets folded to True/False if possible, before we analyze
2546 -- the THEN/ELSE branches, because when analyzing these branches, we
2547 -- may call Is_Statically_Unevaluated, which expects the condition of
2548 -- an enclosing IF to have been analyze/resolved/evaluated.
2550 Analyze_Expression (Condition);
2551 Resolve (Condition, Any_Boolean);
2553 -- Analyze the THEN expression and (if present) the ELSE expression. For
2554 -- them we delay resolution in the normal manner because of overloading.
2556 Analyze_Expression (Then_Expr);
2558 if Present (Else_Expr) then
2559 Analyze_Expression (Else_Expr);
2560 end if;
2562 -- RM 4.5.7(10/3): If the if_expression is the operand of a type
2563 -- conversion, the type of the if_expression is the target type
2564 -- of the conversion.
2566 if Nkind (Parent (N)) = N_Type_Conversion then
2567 Set_Etype (N, Etype (Parent (N)));
2568 return;
2569 end if;
2571 -- Loop through the interpretations of the THEN expression and check the
2572 -- ELSE expression if present.
2574 if not Is_Overloaded (Then_Expr) then
2575 if Present (Else_Expr) then
2576 Check_Else_Expression (Etype (Then_Expr));
2577 else
2578 Set_Etype (N, Etype (Then_Expr));
2579 end if;
2581 else
2582 Get_First_Interp (Then_Expr, I, It);
2583 while Present (It.Typ) loop
2584 if Present (Else_Expr) then
2585 Check_Else_Expression (It.Typ);
2586 else
2587 Add_One_Interp (N, It.Typ, It.Typ);
2588 end if;
2590 Get_Next_Interp (I, It);
2591 end loop;
2592 end if;
2594 -- If no possible interpretation has been found, the type of the
2595 -- ELSE expression does not match any interpretation of the THEN
2596 -- expression.
2598 if Etype (N) = Any_Type then
2599 if Is_Overloaded (Then_Expr) then
2600 if Is_Overloaded (Else_Expr) then
2601 Error_Msg_N
2602 ("no interpretation compatible with those of THEN expression",
2603 Else_Expr);
2604 else
2605 Error_Msg_N
2606 ("type of ELSE incompatible with interpretations of THEN "
2607 & "expression",
2608 Else_Expr);
2609 Error_Msg_NE
2610 ("\ELSE expression has}!", Else_Expr, Etype (Else_Expr));
2611 end if;
2613 else
2614 if Is_Overloaded (Else_Expr) then
2615 Error_Msg_N
2616 ("no interpretation compatible with type of THEN expression",
2617 Else_Expr);
2618 Error_Msg_NE
2619 ("\THEN expression has}!", Else_Expr, Etype (Then_Expr));
2620 else
2621 Error_Msg_N
2622 ("type of ELSE incompatible with that of THEN expression",
2623 Else_Expr);
2624 Error_Msg_NE
2625 ("\THEN expression has}!", Else_Expr, Etype (Then_Expr));
2626 Error_Msg_NE
2627 ("\ELSE expression has}!", Else_Expr, Etype (Else_Expr));
2628 end if;
2629 end if;
2630 end if;
2631 end Analyze_If_Expression;
2633 ------------------------------------
2634 -- Analyze_Indexed_Component_Form --
2635 ------------------------------------
2637 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2638 P : constant Node_Id := Prefix (N);
2639 Exprs : constant List_Id := Expressions (N);
2640 Exp : Node_Id;
2641 P_T : Entity_Id;
2642 E : Node_Id;
2643 U_N : Entity_Id;
2645 procedure Process_Function_Call;
2646 -- Prefix in indexed component form is an overloadable entity, so the
2647 -- node is very likely a function call; reformat it as such. The only
2648 -- exception is a call to a parameterless function that returns an
2649 -- array type, or an access type thereof, in which case this will be
2650 -- undone later by Resolve_Call or Resolve_Entry_Call.
2652 procedure Process_Indexed_Component;
2653 -- Prefix in indexed component form is actually an indexed component.
2654 -- This routine processes it, knowing that the prefix is already
2655 -- resolved.
2657 procedure Process_Indexed_Component_Or_Slice;
2658 -- An indexed component with a single index may designate a slice if
2659 -- the index is a subtype mark. This routine disambiguates these two
2660 -- cases by resolving the prefix to see if it is a subtype mark.
2662 procedure Process_Overloaded_Indexed_Component;
2663 -- If the prefix of an indexed component is overloaded, the proper
2664 -- interpretation is selected by the index types and the context.
2666 ---------------------------
2667 -- Process_Function_Call --
2668 ---------------------------
2670 procedure Process_Function_Call is
2671 Loc : constant Source_Ptr := Sloc (N);
2672 Actual : Node_Id;
2674 begin
2675 Change_Node (N, N_Function_Call);
2676 Set_Name (N, P);
2677 Set_Parameter_Associations (N, Exprs);
2679 -- Analyze actuals prior to analyzing the call itself
2681 Actual := First (Parameter_Associations (N));
2682 while Present (Actual) loop
2683 Analyze (Actual);
2684 Check_Parameterless_Call (Actual);
2686 -- Move to next actual. Note that we use Next, not Next_Actual
2687 -- here. The reason for this is a bit subtle. If a function call
2688 -- includes named associations, the parser recognizes the node
2689 -- as a call, and it is analyzed as such. If all associations are
2690 -- positional, the parser builds an indexed_component node, and
2691 -- it is only after analysis of the prefix that the construct
2692 -- is recognized as a call, in which case Process_Function_Call
2693 -- rewrites the node and analyzes the actuals. If the list of
2694 -- actuals is malformed, the parser may leave the node as an
2695 -- indexed component (despite the presence of named associations).
2696 -- The iterator Next_Actual is equivalent to Next if the list is
2697 -- positional, but follows the normalized chain of actuals when
2698 -- named associations are present. In this case normalization has
2699 -- not taken place, and actuals remain unanalyzed, which leads to
2700 -- subsequent crashes or loops if there is an attempt to continue
2701 -- analysis of the program.
2703 -- IF there is a single actual and it is a type name, the node
2704 -- can only be interpreted as a slice of a parameterless call.
2705 -- Rebuild the node as such and analyze.
2707 if No (Next (Actual))
2708 and then Is_Entity_Name (Actual)
2709 and then Is_Type (Entity (Actual))
2710 and then Is_Discrete_Type (Entity (Actual))
2711 and then not Is_Current_Instance (Actual)
2712 then
2713 Replace (N,
2714 Make_Slice (Loc,
2715 Prefix => P,
2716 Discrete_Range =>
2717 New_Occurrence_Of (Entity (Actual), Loc)));
2718 Analyze (N);
2719 return;
2721 else
2722 Next (Actual);
2723 end if;
2724 end loop;
2726 Analyze_Call (N);
2727 end Process_Function_Call;
2729 -------------------------------
2730 -- Process_Indexed_Component --
2731 -------------------------------
2733 procedure Process_Indexed_Component is
2734 Exp : Node_Id;
2735 Array_Type : Entity_Id;
2736 Index : Node_Id;
2737 Pent : Entity_Id := Empty;
2739 begin
2740 Exp := First (Exprs);
2742 if Is_Overloaded (P) then
2743 Process_Overloaded_Indexed_Component;
2745 else
2746 Array_Type := Etype (P);
2748 if Is_Entity_Name (P) then
2749 Pent := Entity (P);
2750 elsif Nkind (P) = N_Selected_Component
2751 and then Is_Entity_Name (Selector_Name (P))
2752 then
2753 Pent := Entity (Selector_Name (P));
2754 end if;
2756 -- Prefix must be appropriate for an array type, taking into
2757 -- account a possible implicit dereference.
2759 if Is_Access_Type (Array_Type) then
2760 Error_Msg_NW
2761 (Warn_On_Dereference, "?d?implicit dereference", N);
2762 Array_Type := Implicitly_Designated_Type (Array_Type);
2763 end if;
2765 if Is_Array_Type (Array_Type) then
2767 -- In order to correctly access First_Index component later,
2768 -- replace string literal subtype by its parent type.
2770 if Ekind (Array_Type) = E_String_Literal_Subtype then
2771 Array_Type := Etype (Array_Type);
2772 end if;
2774 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2775 Analyze (Exp);
2776 Set_Etype (N, Any_Type);
2778 if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then
2779 Error_Msg_N ("invalid index type in entry name", N);
2781 elsif Present (Next (Exp)) then
2782 Error_Msg_N ("too many subscripts in entry reference", N);
2784 else
2785 Set_Etype (N, Etype (P));
2786 end if;
2788 return;
2790 elsif Is_Record_Type (Array_Type)
2791 and then Remote_AST_I_Dereference (P)
2792 then
2793 return;
2795 elsif Try_Container_Indexing (N, P, Exprs) then
2796 return;
2798 elsif Array_Type = Any_Type then
2799 Set_Etype (N, Any_Type);
2801 -- In most cases the analysis of the prefix will have emitted
2802 -- an error already, but if the prefix may be interpreted as a
2803 -- call in prefixed notation, the report is left to the caller.
2804 -- To prevent cascaded errors, report only if no previous ones.
2806 if Serious_Errors_Detected = 0 then
2807 Error_Msg_N ("invalid prefix in indexed component", P);
2809 if Nkind (P) = N_Expanded_Name then
2810 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2811 end if;
2812 end if;
2814 return;
2816 -- Here we definitely have a bad indexing
2818 else
2819 if Nkind (Parent (N)) = N_Requeue_Statement
2820 and then Present (Pent) and then Ekind (Pent) = E_Entry
2821 then
2822 Error_Msg_N
2823 ("REQUEUE does not permit parameters", First (Exprs));
2825 elsif Is_Entity_Name (P)
2826 and then Etype (P) = Standard_Void_Type
2827 then
2828 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2830 else
2831 Error_Msg_N ("array type required in indexed component", P);
2832 end if;
2834 Set_Etype (N, Any_Type);
2835 return;
2836 end if;
2838 Index := First_Index (Array_Type);
2839 while Present (Index) and then Present (Exp) loop
2840 if not Has_Compatible_Type (Exp, Etype (Index)) then
2841 Wrong_Type (Exp, Etype (Index));
2842 Set_Etype (N, Any_Type);
2843 return;
2844 end if;
2846 Next_Index (Index);
2847 Next (Exp);
2848 end loop;
2850 Set_Etype (N, Component_Type (Array_Type));
2851 Check_Implicit_Dereference (N, Etype (N));
2853 if Present (Index) then
2854 Error_Msg_N
2855 ("too few subscripts in array reference", First (Exprs));
2857 elsif Present (Exp) then
2858 Error_Msg_N ("too many subscripts in array reference", Exp);
2859 end if;
2860 end if;
2861 end Process_Indexed_Component;
2863 ----------------------------------------
2864 -- Process_Indexed_Component_Or_Slice --
2865 ----------------------------------------
2867 procedure Process_Indexed_Component_Or_Slice is
2868 begin
2869 Exp := First (Exprs);
2870 while Present (Exp) loop
2871 Analyze_Expression (Exp);
2872 Next (Exp);
2873 end loop;
2875 Exp := First (Exprs);
2877 -- If one index is present, and it is a subtype name, then the node
2878 -- denotes a slice (note that the case of an explicit range for a
2879 -- slice was already built as an N_Slice node in the first place,
2880 -- so that case is not handled here).
2882 -- We use a replace rather than a rewrite here because this is one
2883 -- of the cases in which the tree built by the parser is plain wrong.
2885 if No (Next (Exp))
2886 and then Is_Entity_Name (Exp)
2887 and then Is_Type (Entity (Exp))
2888 then
2889 Replace (N,
2890 Make_Slice (Sloc (N),
2891 Prefix => P,
2892 Discrete_Range => New_Copy (Exp)));
2893 Analyze (N);
2895 -- Otherwise (more than one index present, or single index is not
2896 -- a subtype name), then we have the indexed component case.
2898 else
2899 Process_Indexed_Component;
2900 end if;
2901 end Process_Indexed_Component_Or_Slice;
2903 ------------------------------------------
2904 -- Process_Overloaded_Indexed_Component --
2905 ------------------------------------------
2907 procedure Process_Overloaded_Indexed_Component is
2908 Exp : Node_Id;
2909 I : Interp_Index;
2910 It : Interp;
2911 Typ : Entity_Id;
2912 Index : Node_Id;
2913 Found : Boolean;
2915 begin
2916 Set_Etype (N, Any_Type);
2918 Get_First_Interp (P, I, It);
2919 while Present (It.Nam) loop
2920 Typ := It.Typ;
2922 if Is_Access_Type (Typ) then
2923 Typ := Designated_Type (Typ);
2924 Error_Msg_NW
2925 (Warn_On_Dereference, "?d?implicit dereference", N);
2926 end if;
2928 if Is_Array_Type (Typ) then
2930 -- Got a candidate: verify that index types are compatible
2932 Index := First_Index (Typ);
2933 Found := True;
2934 Exp := First (Exprs);
2935 while Present (Index) and then Present (Exp) loop
2936 if Has_Compatible_Type (Exp, Etype (Index)) then
2937 null;
2938 else
2939 Found := False;
2940 Remove_Interp (I);
2941 exit;
2942 end if;
2944 Next_Index (Index);
2945 Next (Exp);
2946 end loop;
2948 if Found and then No (Index) and then No (Exp) then
2949 declare
2950 CT : constant Entity_Id :=
2951 Base_Type (Component_Type (Typ));
2952 begin
2953 Add_One_Interp (N, CT, CT);
2954 Check_Implicit_Dereference (N, CT);
2955 end;
2956 end if;
2958 elsif Try_Container_Indexing (N, P, Exprs) then
2959 return;
2961 end if;
2963 Get_Next_Interp (I, It);
2964 end loop;
2966 if Etype (N) = Any_Type then
2967 Error_Msg_N ("no legal interpretation for indexed component", N);
2968 Set_Is_Overloaded (N, False);
2969 end if;
2970 end Process_Overloaded_Indexed_Component;
2972 -- Start of processing for Analyze_Indexed_Component_Form
2974 begin
2975 -- Get name of array, function or type
2977 Analyze (P);
2979 -- If P is an explicit dereference whose prefix is of a remote access-
2980 -- to-subprogram type, then N has already been rewritten as a subprogram
2981 -- call and analyzed.
2983 if Nkind (N) in N_Subprogram_Call then
2984 return;
2986 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2987 -- the indexed component denotes a loop name, the indexed form is turned
2988 -- into an attribute reference.
2990 elsif Nkind (N) = N_Attribute_Reference
2991 and then Attribute_Name (N) = Name_Loop_Entry
2992 then
2993 return;
2994 end if;
2996 pragma Assert (Nkind (N) = N_Indexed_Component);
2998 P_T := Base_Type (Etype (P));
3000 if Is_Entity_Name (P) and then Present (Entity (P)) then
3001 U_N := Entity (P);
3003 if Is_Type (U_N) then
3005 -- Reformat node as a type conversion
3007 E := Remove_Head (Exprs);
3009 if Present (First (Exprs)) then
3010 Error_Msg_N
3011 ("argument of type conversion must be single expression", N);
3012 end if;
3014 Change_Node (N, N_Type_Conversion);
3015 Set_Subtype_Mark (N, P);
3016 Set_Etype (N, U_N);
3017 Set_Expression (N, E);
3019 -- After changing the node, call for the specific Analysis
3020 -- routine directly, to avoid a double call to the expander.
3022 Analyze_Type_Conversion (N);
3023 return;
3024 end if;
3026 if Is_Overloadable (U_N) then
3027 Process_Function_Call;
3029 elsif Ekind (Etype (P)) = E_Subprogram_Type
3030 or else (Is_Access_Type (Etype (P))
3031 and then
3032 Ekind (Designated_Type (Etype (P))) =
3033 E_Subprogram_Type)
3034 then
3035 -- Call to access_to-subprogram with possible implicit dereference
3037 Process_Function_Call;
3039 elsif Is_Generic_Subprogram (U_N) then
3041 -- A common beginner's (or C++ templates fan) error
3043 Error_Msg_N ("generic subprogram cannot be called", N);
3044 Set_Etype (N, Any_Type);
3045 return;
3047 else
3048 Process_Indexed_Component_Or_Slice;
3049 end if;
3051 -- If not an entity name, prefix is an expression that may denote
3052 -- an array or an access-to-subprogram.
3054 else
3055 if Ekind (P_T) = E_Subprogram_Type
3056 or else (Is_Access_Type (P_T)
3057 and then
3058 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
3059 then
3060 Process_Function_Call;
3062 elsif Nkind (P) = N_Selected_Component
3063 and then Present (Entity (Selector_Name (P)))
3064 and then Is_Overloadable (Entity (Selector_Name (P)))
3065 then
3066 Process_Function_Call;
3067 else
3068 -- Indexed component, slice, or a call to a member of a family
3069 -- entry, which will be converted to an entry call later.
3071 Process_Indexed_Component_Or_Slice;
3072 end if;
3073 end if;
3075 Analyze_Dimension (N);
3076 end Analyze_Indexed_Component_Form;
3078 ------------------------
3079 -- Analyze_Logical_Op --
3080 ------------------------
3082 procedure Analyze_Logical_Op (N : Node_Id) is
3083 L : constant Node_Id := Left_Opnd (N);
3084 R : constant Node_Id := Right_Opnd (N);
3086 Op_Id : Entity_Id;
3088 begin
3089 Set_Etype (N, Any_Type);
3090 Candidate_Type := Empty;
3092 Analyze_Expression (L);
3093 Analyze_Expression (R);
3095 -- If the entity is already set, the node is the instantiation of a
3096 -- generic node with a non-local reference, or was manufactured by a
3097 -- call to Make_Op_xxx. In either case the entity is known to be valid,
3098 -- and we do not need to collect interpretations, instead we just get
3099 -- the single possible interpretation.
3101 if Present (Entity (N)) then
3102 Op_Id := Entity (N);
3104 if Ekind (Op_Id) = E_Operator then
3105 Find_Boolean_Types (L, R, Op_Id, N);
3106 else
3107 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3108 end if;
3110 -- Entity is not already set, so we do need to collect interpretations
3112 else
3113 Op_Id := Get_Name_Entity_Id (Chars (N));
3114 while Present (Op_Id) loop
3115 if Ekind (Op_Id) = E_Operator then
3116 Find_Boolean_Types (L, R, Op_Id, N);
3117 else
3118 Analyze_User_Defined_Binary_Op (N, Op_Id);
3119 end if;
3121 Op_Id := Homonym (Op_Id);
3122 end loop;
3123 end if;
3125 Operator_Check (N);
3126 Check_Function_Writable_Actuals (N);
3127 end Analyze_Logical_Op;
3129 ---------------------------
3130 -- Analyze_Membership_Op --
3131 ---------------------------
3133 procedure Analyze_Membership_Op (N : Node_Id) is
3134 Loc : constant Source_Ptr := Sloc (N);
3135 L : constant Node_Id := Left_Opnd (N);
3136 R : constant Node_Id := Right_Opnd (N);
3138 procedure Analyze_Set_Membership;
3139 -- If a set of alternatives is present, analyze each and find the
3140 -- common type to which they must all resolve.
3142 function Find_Interp return Boolean;
3143 -- Find a valid interpretation of the test. Note that the context of the
3144 -- operation plays no role in resolving the operands, so that if there
3145 -- is more than one interpretation of the operands that is compatible
3146 -- with the test, the operation is ambiguous.
3148 function Try_Left_Interp (T : Entity_Id) return Boolean;
3149 -- Try an interpretation of the left operand with type T. Return true if
3150 -- one interpretation (at least) of the right operand making up a valid
3151 -- operand pair exists, otherwise false if no such pair exists.
3153 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean;
3154 -- Return true if T1 and T2 constitute a valid pair of operand types for
3155 -- L and R respectively.
3157 ----------------------------
3158 -- Analyze_Set_Membership --
3159 ----------------------------
3161 procedure Analyze_Set_Membership is
3162 Alt : Node_Id;
3163 Index : Interp_Index;
3164 It : Interp;
3165 Candidate_Interps : Node_Id;
3166 Common_Type : Entity_Id := Empty;
3168 begin
3169 Analyze (L);
3170 Candidate_Interps := L;
3172 if not Is_Overloaded (L) then
3173 Common_Type := Etype (L);
3175 Alt := First (Alternatives (N));
3176 while Present (Alt) loop
3177 Analyze (Alt);
3179 if not Has_Compatible_Type (Alt, Common_Type) then
3180 Wrong_Type (Alt, Common_Type);
3181 end if;
3183 Next (Alt);
3184 end loop;
3186 else
3187 Alt := First (Alternatives (N));
3188 while Present (Alt) loop
3189 Analyze (Alt);
3190 if not Is_Overloaded (Alt) then
3191 Common_Type := Etype (Alt);
3193 else
3194 Get_First_Interp (Alt, Index, It);
3195 while Present (It.Typ) loop
3196 if not
3197 Has_Compatible_Type (Candidate_Interps, It.Typ)
3198 then
3199 Remove_Interp (Index);
3200 end if;
3202 Get_Next_Interp (Index, It);
3203 end loop;
3205 Get_First_Interp (Alt, Index, It);
3207 if No (It.Typ) then
3208 Error_Msg_N ("alternative has no legal type", Alt);
3209 return;
3210 end if;
3212 -- If alternative is not overloaded, we have a unique type
3213 -- for all of them.
3215 Set_Etype (Alt, It.Typ);
3217 -- If the alternative is an enumeration literal, use the one
3218 -- for this interpretation.
3220 if Is_Entity_Name (Alt) then
3221 Set_Entity (Alt, It.Nam);
3222 end if;
3224 Get_Next_Interp (Index, It);
3226 if No (It.Typ) then
3227 Set_Is_Overloaded (Alt, False);
3228 Common_Type := Etype (Alt);
3229 end if;
3231 Candidate_Interps := Alt;
3232 end if;
3234 Next (Alt);
3235 end loop;
3236 end if;
3238 if Present (Common_Type) then
3239 Set_Etype (L, Common_Type);
3241 -- The left operand may still be overloaded, to be resolved using
3242 -- the Common_Type.
3244 else
3245 Error_Msg_N ("cannot resolve membership operation", N);
3246 end if;
3247 end Analyze_Set_Membership;
3249 -----------------
3250 -- Find_Interp --
3251 -----------------
3253 function Find_Interp return Boolean is
3254 Found : Boolean;
3255 I : Interp_Index;
3256 It : Interp;
3257 L_Typ : Entity_Id;
3258 Valid_I : Interp_Index;
3260 begin
3261 -- Loop through the interpretations of the left operand
3263 if not Is_Overloaded (L) then
3264 Found := Try_Left_Interp (Etype (L));
3266 else
3267 Found := False;
3268 L_Typ := Empty;
3269 Valid_I := 0;
3271 Get_First_Interp (L, I, It);
3272 while Present (It.Typ) loop
3273 if Try_Left_Interp (It.Typ) then
3274 -- If several interpretations are possible, disambiguate
3276 if Present (L_Typ)
3277 and then Base_Type (It.Typ) /= Base_Type (L_Typ)
3278 then
3279 It := Disambiguate (L, Valid_I, I, Any_Type);
3281 if It = No_Interp then
3282 Ambiguous_Operands (N);
3283 Set_Etype (L, Any_Type);
3284 return True;
3285 end if;
3287 else
3288 Valid_I := I;
3289 end if;
3291 L_Typ := It.Typ;
3292 Set_Etype (L, L_Typ);
3293 Found := True;
3294 end if;
3296 Get_Next_Interp (I, It);
3297 end loop;
3298 end if;
3300 return Found;
3301 end Find_Interp;
3303 ---------------------
3304 -- Try_Left_Interp --
3305 ---------------------
3307 function Try_Left_Interp (T : Entity_Id) return Boolean is
3308 Found : Boolean;
3309 I : Interp_Index;
3310 It : Interp;
3311 R_Typ : Entity_Id;
3312 Valid_I : Interp_Index;
3314 begin
3315 -- Defend against previous error
3317 if Nkind (R) = N_Error then
3318 Found := False;
3320 -- Loop through the interpretations of the right operand
3322 elsif not Is_Overloaded (R) then
3323 Found := Is_Valid_Pair (T, Etype (R));
3325 else
3326 Found := False;
3327 R_Typ := Empty;
3328 Valid_I := 0;
3330 Get_First_Interp (R, I, It);
3331 while Present (It.Typ) loop
3332 if Is_Valid_Pair (T, It.Typ) then
3333 -- If several interpretations are possible, disambiguate
3335 if Present (R_Typ)
3336 and then Base_Type (It.Typ) /= Base_Type (R_Typ)
3337 then
3338 It := Disambiguate (R, Valid_I, I, Any_Type);
3340 if It = No_Interp then
3341 Ambiguous_Operands (N);
3342 Set_Etype (R, Any_Type);
3343 return True;
3344 end if;
3346 else
3347 Valid_I := I;
3348 end if;
3350 R_Typ := It.Typ;
3351 Found := True;
3352 end if;
3354 Get_Next_Interp (I, It);
3355 end loop;
3356 end if;
3358 return Found;
3359 end Try_Left_Interp;
3361 -------------------
3362 -- Is_Valid_Pair --
3363 -------------------
3365 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean is
3366 begin
3367 return Covers (T1 => T1, T2 => T2)
3368 or else Covers (T1 => T2, T2 => T1)
3369 or else Is_User_Defined_Literal (L, T2)
3370 or else Is_User_Defined_Literal (R, T1);
3371 end Is_Valid_Pair;
3373 -- Local variables
3375 Dummy : Boolean;
3376 Op : Node_Id;
3378 -- Start of processing for Analyze_Membership_Op
3380 begin
3381 Analyze_Expression (L);
3383 if No (R) then
3384 pragma Assert (Ada_Version >= Ada_2012);
3386 Analyze_Set_Membership;
3388 elsif Nkind (R) = N_Range
3389 or else (Nkind (R) = N_Attribute_Reference
3390 and then Attribute_Name (R) = Name_Range)
3391 then
3392 Analyze_Expression (R);
3394 Dummy := Find_Interp;
3396 -- If not a range, it can be a subtype mark, or else it is a degenerate
3397 -- membership test with a singleton value, i.e. a test for equality,
3398 -- if the types are compatible.
3400 else
3401 Analyze_Expression (R);
3403 if Is_Entity_Name (R) and then Is_Type (Entity (R)) then
3404 Find_Type (R);
3405 Check_Fully_Declared (Entity (R), R);
3407 elsif Ada_Version >= Ada_2012 and then Find_Interp then
3408 Op := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
3409 Resolve_Membership_Equality (Op, Etype (L));
3411 if Nkind (N) = N_Not_In then
3412 Op := Make_Op_Not (Loc, Op);
3413 end if;
3415 Rewrite (N, Op);
3416 Analyze (N);
3417 return;
3419 else
3420 -- In all versions of the language, if we reach this point there
3421 -- is a previous error that will be diagnosed below.
3423 Find_Type (R);
3424 end if;
3425 end if;
3427 -- Compatibility between expression and subtype mark or range is
3428 -- checked during resolution. The result of the operation is Boolean
3429 -- in any case.
3431 Set_Etype (N, Standard_Boolean);
3433 if Comes_From_Source (N)
3434 and then Present (Right_Opnd (N))
3435 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3436 then
3437 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3438 end if;
3440 Check_Function_Writable_Actuals (N);
3441 end Analyze_Membership_Op;
3443 -----------------
3444 -- Analyze_Mod --
3445 -----------------
3447 procedure Analyze_Mod (N : Node_Id) is
3448 begin
3449 -- A special warning check, if we have an expression of the form:
3450 -- expr mod 2 * literal
3451 -- where literal is 128 or less, then probably what was meant was
3452 -- expr mod 2 ** literal
3453 -- so issue an appropriate warning.
3455 if Warn_On_Suspicious_Modulus_Value
3456 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3457 and then Intval (Right_Opnd (N)) = Uint_2
3458 and then Nkind (Parent (N)) = N_Op_Multiply
3459 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3460 and then Intval (Right_Opnd (Parent (N))) <= Uint_128
3461 then
3462 Error_Msg_N
3463 ("suspicious MOD value, was '*'* intended'??.m?", Parent (N));
3464 end if;
3466 -- Remaining processing is same as for other arithmetic operators
3468 Analyze_Arithmetic_Op (N);
3469 end Analyze_Mod;
3471 ----------------------
3472 -- Analyze_Negation --
3473 ----------------------
3475 procedure Analyze_Negation (N : Node_Id) is
3476 R : constant Node_Id := Right_Opnd (N);
3478 Op_Id : Entity_Id;
3480 begin
3481 Set_Etype (N, Any_Type);
3482 Candidate_Type := Empty;
3484 Analyze_Expression (R);
3486 -- If the entity is already set, the node is the instantiation of a
3487 -- generic node with a non-local reference, or was manufactured by a
3488 -- call to Make_Op_xxx. In either case the entity is known to be valid,
3489 -- and we do not need to collect interpretations, instead we just get
3490 -- the single possible interpretation.
3492 if Present (Entity (N)) then
3493 Op_Id := Entity (N);
3495 if Ekind (Op_Id) = E_Operator then
3496 Find_Negation_Types (R, Op_Id, N);
3497 else
3498 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3499 end if;
3501 else
3502 Op_Id := Get_Name_Entity_Id (Chars (N));
3503 while Present (Op_Id) loop
3504 if Ekind (Op_Id) = E_Operator then
3505 Find_Negation_Types (R, Op_Id, N);
3506 else
3507 Analyze_User_Defined_Unary_Op (N, Op_Id);
3508 end if;
3510 Op_Id := Homonym (Op_Id);
3511 end loop;
3512 end if;
3514 Operator_Check (N);
3515 end Analyze_Negation;
3517 ------------------
3518 -- Analyze_Null --
3519 ------------------
3521 procedure Analyze_Null (N : Node_Id) is
3522 begin
3523 Set_Etype (N, Universal_Access);
3524 end Analyze_Null;
3526 ----------------------
3527 -- Analyze_One_Call --
3528 ----------------------
3530 procedure Analyze_One_Call
3531 (N : Node_Id;
3532 Nam : Entity_Id;
3533 Report : Boolean;
3534 Success : out Boolean;
3535 Skip_First : Boolean := False)
3537 Actuals : constant List_Id := Parameter_Associations (N);
3538 Prev_T : constant Entity_Id := Etype (N);
3540 -- Recognize cases of prefixed calls that have been rewritten in
3541 -- various ways. The simplest case is a rewritten selected component,
3542 -- but it can also be an already-examined indexed component, or a
3543 -- prefix that is itself a rewritten prefixed call that is in turn
3544 -- an indexed call (the syntactic ambiguity involving the indexing of
3545 -- a function with defaulted parameters that returns an array).
3546 -- A flag Maybe_Indexed_Call might be useful here ???
3548 Must_Skip : constant Boolean := Skip_First
3549 or else Nkind (Original_Node (N)) = N_Selected_Component
3550 or else
3551 (Nkind (Original_Node (N)) = N_Indexed_Component
3552 and then Nkind (Prefix (Original_Node (N))) =
3553 N_Selected_Component)
3554 or else
3555 (Nkind (Parent (N)) = N_Function_Call
3556 and then Is_Array_Type (Etype (Name (N)))
3557 and then Etype (Original_Node (N)) =
3558 Component_Type (Etype (Name (N)))
3559 and then Nkind (Original_Node (Parent (N))) =
3560 N_Selected_Component);
3562 -- The first formal must be omitted from the match when trying to find
3563 -- a primitive operation that is a possible interpretation, and also
3564 -- after the call has been rewritten, because the corresponding actual
3565 -- is already known to be compatible, and because this may be an
3566 -- indexing of a call with default parameters.
3568 First_Form : Entity_Id;
3569 Formal : Entity_Id;
3570 Actual : Node_Id;
3571 Is_Indexed : Boolean := False;
3572 Is_Indirect : Boolean := False;
3573 Subp_Type : constant Entity_Id := Etype (Nam);
3574 Norm_OK : Boolean;
3576 function Compatible_Types_In_Predicate
3577 (T1 : Entity_Id;
3578 T2 : Entity_Id) return Boolean;
3579 -- For an Ada 2012 predicate or invariant, a call may mention an
3580 -- incomplete type, while resolution of the corresponding predicate
3581 -- function may see the full view, as a consequence of the delayed
3582 -- resolution of the corresponding expressions. This may occur in
3583 -- the body of a predicate function, or in a call to such. Anomalies
3584 -- involving private and full views can also happen. In each case,
3585 -- rewrite node or add conversions to remove spurious type errors.
3587 procedure Indicate_Name_And_Type;
3588 -- If candidate interpretation matches, indicate name and type of result
3589 -- on call node.
3591 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3592 -- There may be a user-defined operator that hides the current
3593 -- interpretation. We must check for this independently of the
3594 -- analysis of the call with the user-defined operation, because
3595 -- the parameter names may be wrong and yet the hiding takes place.
3596 -- This fixes a problem with ACATS test B34014O.
3598 -- When the type Address is a visible integer type, and the DEC
3599 -- system extension is visible, the predefined operator may be
3600 -- hidden as well, by one of the address operations in auxdec.
3601 -- Finally, the abstract operations on address do not hide the
3602 -- predefined operator (this is the purpose of making them abstract).
3604 -----------------------------------
3605 -- Compatible_Types_In_Predicate --
3606 -----------------------------------
3608 function Compatible_Types_In_Predicate
3609 (T1 : Entity_Id;
3610 T2 : Entity_Id) return Boolean
3612 function Common_Type (T : Entity_Id) return Entity_Id;
3613 -- Find non-private underlying full view if any, without going to
3614 -- ancestor type (as opposed to Underlying_Type).
3616 -----------------
3617 -- Common_Type --
3618 -----------------
3620 function Common_Type (T : Entity_Id) return Entity_Id is
3621 CT : Entity_Id;
3623 begin
3624 CT := T;
3626 if Is_Private_Type (CT) and then Present (Full_View (CT)) then
3627 CT := Full_View (CT);
3628 end if;
3630 if Is_Private_Type (CT)
3631 and then Present (Underlying_Full_View (CT))
3632 then
3633 CT := Underlying_Full_View (CT);
3634 end if;
3636 return Base_Type (CT);
3637 end Common_Type;
3639 -- Start of processing for Compatible_Types_In_Predicate
3641 begin
3642 if (Ekind (Current_Scope) = E_Function
3643 and then Is_Predicate_Function (Current_Scope))
3644 or else
3645 (Ekind (Nam) = E_Function
3646 and then Is_Predicate_Function (Nam))
3647 then
3648 if Is_Incomplete_Type (T1)
3649 and then Present (Full_View (T1))
3650 and then Full_View (T1) = T2
3651 then
3652 Set_Etype (Formal, Etype (Actual));
3653 return True;
3655 elsif Common_Type (T1) = Common_Type (T2) then
3656 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3657 return True;
3659 else
3660 return False;
3661 end if;
3663 else
3664 return False;
3665 end if;
3666 end Compatible_Types_In_Predicate;
3668 ----------------------------
3669 -- Indicate_Name_And_Type --
3670 ----------------------------
3672 procedure Indicate_Name_And_Type is
3673 begin
3674 Add_One_Interp (N, Nam, Etype (Nam));
3675 Check_Implicit_Dereference (N, Etype (Nam));
3676 Success := True;
3678 -- If the prefix of the call is a name, indicate the entity
3679 -- being called. If it is not a name, it is an expression that
3680 -- denotes an access to subprogram or else an entry or family. In
3681 -- the latter case, the name is a selected component, and the entity
3682 -- being called is noted on the selector.
3684 if not Is_Type (Nam) then
3685 if Is_Entity_Name (Name (N)) then
3686 Set_Entity (Name (N), Nam);
3687 Set_Etype (Name (N), Etype (Nam));
3689 elsif Nkind (Name (N)) = N_Selected_Component then
3690 Set_Entity (Selector_Name (Name (N)), Nam);
3691 end if;
3692 end if;
3694 if Debug_Flag_E and not Report then
3695 Write_Str (" Overloaded call ");
3696 Write_Int (Int (N));
3697 Write_Str (" compatible with ");
3698 Write_Int (Int (Nam));
3699 Write_Eol;
3700 end if;
3701 end Indicate_Name_And_Type;
3703 ------------------------
3704 -- Operator_Hidden_By --
3705 ------------------------
3707 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3708 Act1 : constant Node_Id := First_Actual (N);
3709 Act2 : constant Node_Id := Next_Actual (Act1);
3710 Form1 : constant Entity_Id := First_Formal (Fun);
3711 Form2 : constant Entity_Id := Next_Formal (Form1);
3713 begin
3714 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3715 return False;
3717 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3718 return False;
3720 elsif Present (Form2) then
3721 if No (Act2)
3722 or else not Has_Compatible_Type (Act2, Etype (Form2))
3723 then
3724 return False;
3725 end if;
3727 elsif Present (Act2) then
3728 return False;
3729 end if;
3731 -- Now we know that the arity of the operator matches the function,
3732 -- and the function call is a valid interpretation. The function
3733 -- hides the operator if it has the right signature, or if one of
3734 -- its operands is a non-abstract operation on Address when this is
3735 -- a visible integer type.
3737 return Hides_Op (Fun, Nam)
3738 or else Is_Descendant_Of_Address (Etype (Form1))
3739 or else
3740 (Present (Form2)
3741 and then Is_Descendant_Of_Address (Etype (Form2)));
3742 end Operator_Hidden_By;
3744 -- Start of processing for Analyze_One_Call
3746 begin
3747 Success := False;
3749 -- If the subprogram has no formals or if all the formals have defaults,
3750 -- and the return type is an array type, the node may denote an indexing
3751 -- of the result of a parameterless call. In Ada 2005, the subprogram
3752 -- may have one non-defaulted formal, and the call may have been written
3753 -- in prefix notation, so that the rebuilt parameter list has more than
3754 -- one actual.
3756 if not Is_Overloadable (Nam)
3757 and then Ekind (Nam) /= E_Subprogram_Type
3758 and then Ekind (Nam) /= E_Entry_Family
3759 then
3760 return;
3761 end if;
3763 -- An indexing requires at least one actual. The name of the call cannot
3764 -- be an implicit indirect call, so it cannot be a generated explicit
3765 -- dereference.
3767 if not Is_Empty_List (Actuals)
3768 and then
3769 (Needs_No_Actuals (Nam)
3770 or else
3771 (Needs_One_Actual (Nam)
3772 and then Present (Next_Actual (First (Actuals)))))
3773 then
3774 if Is_Array_Type (Subp_Type)
3775 and then
3776 (Nkind (Name (N)) /= N_Explicit_Dereference
3777 or else Comes_From_Source (Name (N)))
3778 then
3779 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3781 elsif Is_Access_Type (Subp_Type)
3782 and then Is_Array_Type (Designated_Type (Subp_Type))
3783 then
3784 Is_Indexed :=
3785 Try_Indexed_Call
3786 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3788 -- The prefix can also be a parameterless function that returns an
3789 -- access to subprogram, in which case this is an indirect call.
3790 -- If this succeeds, an explicit dereference is added later on,
3791 -- in Analyze_Call or Resolve_Call.
3793 elsif Is_Access_Type (Subp_Type)
3794 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3795 then
3796 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3797 end if;
3799 end if;
3801 -- If the call has been transformed into a slice, it is of the form
3802 -- F (Subtype) where F is parameterless. The node has been rewritten in
3803 -- Try_Indexed_Call and there is nothing else to do.
3805 if Is_Indexed
3806 and then Nkind (N) = N_Slice
3807 then
3808 return;
3809 end if;
3811 Normalize_Actuals
3812 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3814 if not Norm_OK then
3816 -- If an indirect call is a possible interpretation, indicate
3817 -- success to the caller. This may be an indexing of an explicit
3818 -- dereference of a call that returns an access type (see above).
3820 if Is_Indirect
3821 or else (Is_Indexed
3822 and then Nkind (Name (N)) = N_Explicit_Dereference
3823 and then Comes_From_Source (Name (N)))
3824 then
3825 Success := True;
3826 return;
3828 -- Mismatch in number or names of parameters
3830 elsif Debug_Flag_E then
3831 Write_Str (" normalization fails in call ");
3832 Write_Int (Int (N));
3833 Write_Str (" with subprogram ");
3834 Write_Int (Int (Nam));
3835 Write_Eol;
3836 end if;
3838 -- If the context expects a function call, discard any interpretation
3839 -- that is a procedure. If the node is not overloaded, leave as is for
3840 -- better error reporting when type mismatch is found.
3842 elsif Nkind (N) = N_Function_Call
3843 and then Is_Overloaded (Name (N))
3844 and then Ekind (Nam) = E_Procedure
3845 then
3846 return;
3848 -- Ditto for function calls in a procedure context
3850 elsif Nkind (N) = N_Procedure_Call_Statement
3851 and then Is_Overloaded (Name (N))
3852 and then Etype (Nam) /= Standard_Void_Type
3853 then
3854 return;
3856 elsif No (Actuals) then
3858 -- If Normalize succeeds, then there are default parameters for
3859 -- all formals.
3861 Indicate_Name_And_Type;
3863 elsif Ekind (Nam) = E_Operator then
3864 if Nkind (N) = N_Procedure_Call_Statement then
3865 return;
3866 end if;
3868 -- This occurs when the prefix of the call is an operator name
3869 -- or an expanded name whose selector is an operator name.
3871 Analyze_Operator_Call (N, Nam);
3873 if Etype (N) /= Prev_T then
3875 -- Check that operator is not hidden by a function interpretation
3877 if Is_Overloaded (Name (N)) then
3878 declare
3879 I : Interp_Index;
3880 It : Interp;
3882 begin
3883 Get_First_Interp (Name (N), I, It);
3884 while Present (It.Nam) loop
3885 if Operator_Hidden_By (It.Nam) then
3886 Set_Etype (N, Prev_T);
3887 return;
3888 end if;
3890 Get_Next_Interp (I, It);
3891 end loop;
3892 end;
3893 end if;
3895 -- If operator matches formals, record its name on the call.
3896 -- If the operator is overloaded, Resolve will select the
3897 -- correct one from the list of interpretations. The call
3898 -- node itself carries the first candidate.
3900 Set_Entity (Name (N), Nam);
3901 Success := True;
3903 elsif Report and then Etype (N) = Any_Type then
3904 Error_Msg_N ("incompatible arguments for operator", N);
3905 end if;
3907 else
3908 -- Normalize_Actuals has chained the named associations in the
3909 -- correct order of the formals.
3911 Actual := First_Actual (N);
3912 Formal := First_Formal (Nam);
3913 First_Form := Formal;
3915 -- If we are analyzing a call rewritten from object notation, skip
3916 -- first actual, which may be rewritten later as an explicit
3917 -- dereference.
3919 if Must_Skip then
3920 Next_Actual (Actual);
3921 Next_Formal (Formal);
3922 end if;
3924 while Present (Actual) and then Present (Formal) loop
3925 if Nkind (Parent (Actual)) /= N_Parameter_Association
3926 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3927 then
3928 -- The actual can be compatible with the formal, but we must
3929 -- also check that the context is not an address type that is
3930 -- visibly an integer type. In this case the use of literals is
3931 -- illegal, except in the body of descendants of system, where
3932 -- arithmetic operations on address are of course used.
3934 if Has_Compatible_Type (Actual, Etype (Formal))
3935 and then
3936 (Etype (Actual) /= Universal_Integer
3937 or else not Is_Descendant_Of_Address (Etype (Formal))
3938 or else In_Predefined_Unit (N))
3939 then
3940 Next_Actual (Actual);
3941 Next_Formal (Formal);
3943 -- In Allow_Integer_Address mode, we allow an actual integer to
3944 -- match a formal address type and vice versa. We only do this
3945 -- if we are certain that an error will otherwise be issued
3947 elsif Address_Integer_Convert_OK
3948 (Etype (Actual), Etype (Formal))
3949 and then (Report and not Is_Indexed and not Is_Indirect)
3950 then
3951 -- Handle this case by introducing an unchecked conversion
3953 Rewrite (Actual,
3954 Unchecked_Convert_To (Etype (Formal),
3955 Relocate_Node (Actual)));
3956 Analyze_And_Resolve (Actual, Etype (Formal));
3957 Next_Actual (Actual);
3958 Next_Formal (Formal);
3960 -- Under relaxed RM semantics silently replace occurrences of
3961 -- null by System.Address_Null. We only do this if we know that
3962 -- an error will otherwise be issued.
3964 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3965 and then (Report and not Is_Indexed and not Is_Indirect)
3966 then
3967 Replace_Null_By_Null_Address (Actual);
3968 Analyze_And_Resolve (Actual, Etype (Formal));
3969 Next_Actual (Actual);
3970 Next_Formal (Formal);
3972 elsif Compatible_Types_In_Predicate
3973 (Etype (Formal), Etype (Actual))
3974 then
3975 Next_Actual (Actual);
3976 Next_Formal (Formal);
3978 -- A current instance used as an actual of a function,
3979 -- whose body has not been seen, may include a formal
3980 -- whose type is an incomplete view of an enclosing
3981 -- type declaration containing the current call (e.g.
3982 -- in the Expression for a component declaration).
3984 -- In this case, update the signature of the subprogram
3985 -- so the formal has the type of the full view.
3987 elsif Inside_Init_Proc
3988 and then Nkind (Actual) = N_Identifier
3989 and then Ekind (Etype (Formal)) = E_Incomplete_Type
3990 and then Etype (Actual) = Full_View (Etype (Formal))
3991 then
3992 Set_Etype (Formal, Etype (Actual));
3993 Next_Actual (Actual);
3994 Next_Formal (Formal);
3996 -- Handle failed type check
3998 else
3999 if Debug_Flag_E then
4000 Write_Str (" type checking fails in call ");
4001 Write_Int (Int (N));
4002 Write_Str (" with formal ");
4003 Write_Int (Int (Formal));
4004 Write_Str (" in subprogram ");
4005 Write_Int (Int (Nam));
4006 Write_Eol;
4007 end if;
4009 -- Comment needed on the following test???
4011 if Report and not Is_Indexed and not Is_Indirect then
4013 -- Ada 2005 (AI-251): Complete the error notification
4014 -- to help new Ada 2005 users.
4016 if Is_Class_Wide_Type (Etype (Formal))
4017 and then Is_Interface (Etype (Etype (Formal)))
4018 and then not Interface_Present_In_Ancestor
4019 (Typ => Etype (Actual),
4020 Iface => Etype (Etype (Formal)))
4021 then
4022 Error_Msg_NE
4023 ("(Ada 2005) does not implement interface }",
4024 Actual, Etype (Etype (Formal)));
4025 end if;
4027 Wrong_Type (Actual, Etype (Formal));
4029 if Nkind (Actual) = N_Op_Eq
4030 and then Nkind (Left_Opnd (Actual)) = N_Identifier
4031 then
4032 Formal := First_Formal (Nam);
4033 while Present (Formal) loop
4034 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
4035 Error_Msg_N -- CODEFIX
4036 ("possible misspelling of `='>`!", Actual);
4037 exit;
4038 end if;
4040 Next_Formal (Formal);
4041 end loop;
4042 end if;
4044 if All_Errors_Mode then
4045 Error_Msg_Sloc := Sloc (Nam);
4047 if Etype (Formal) = Any_Type then
4048 Error_Msg_N
4049 ("there is no legal actual parameter", Actual);
4050 end if;
4052 if Is_Overloadable (Nam)
4053 and then Present (Alias (Nam))
4054 and then not Comes_From_Source (Nam)
4055 then
4056 Error_Msg_NE
4057 ("\\ =='> in call to inherited operation & #!",
4058 Actual, Nam);
4060 elsif Ekind (Nam) = E_Subprogram_Type then
4061 declare
4062 Access_To_Subprogram_Typ :
4063 constant Entity_Id :=
4064 Defining_Identifier
4065 (Associated_Node_For_Itype (Nam));
4066 begin
4067 Error_Msg_NE
4068 ("\\ =='> in call to dereference of &#!",
4069 Actual, Access_To_Subprogram_Typ);
4070 end;
4072 else
4073 Error_Msg_NE
4074 ("\\ =='> in call to &#!", Actual, Nam);
4076 end if;
4077 end if;
4078 end if;
4080 return;
4081 end if;
4083 else
4084 -- Normalize_Actuals has verified that a default value exists
4085 -- for this formal. Current actual names a subsequent formal.
4087 Next_Formal (Formal);
4088 end if;
4089 end loop;
4091 -- Due to our current model of controlled type expansion we may
4092 -- have resolved a user call to a non-visible controlled primitive
4093 -- since these inherited subprograms may be generated in the current
4094 -- scope. This is a side effect of the need for the expander to be
4095 -- able to resolve internally generated calls.
4097 -- Specifically, the issue appears when predefined controlled
4098 -- operations get called on a type extension whose parent is a
4099 -- private extension completed with a controlled extension - see
4100 -- below:
4102 -- package X is
4103 -- type Par_Typ is tagged private;
4104 -- private
4105 -- type Par_Typ is new Controlled with null record;
4106 -- end;
4107 -- ...
4108 -- procedure Main is
4109 -- type Ext_Typ is new Par_Typ with null record;
4110 -- Obj : Ext_Typ;
4111 -- begin
4112 -- Finalize (Obj); -- Will improperly resolve
4113 -- end;
4115 -- To avoid breaking privacy, Is_Hidden gets set elsewhere on such
4116 -- primitives, but we still need to verify that Nam is indeed a
4117 -- non-visible controlled subprogram. So, we do that here and issue
4118 -- the appropriate error.
4120 if Is_Hidden (Nam)
4121 and then not In_Instance
4122 and then not Comes_From_Source (Nam)
4123 and then Comes_From_Source (N)
4125 -- Verify Nam is a non-visible controlled primitive
4127 and then Chars (Nam) in Name_Adjust
4128 | Name_Finalize
4129 | Name_Initialize
4130 and then Ekind (Nam) = E_Procedure
4131 and then Is_Controlled (Etype (First_Form))
4132 and then No (Next_Formal (First_Form))
4133 and then not Is_Visibly_Controlled (Etype (First_Form))
4134 then
4135 Error_Msg_Node_2 := Etype (First_Form);
4136 Error_Msg_NE ("call to non-visible controlled primitive & on type"
4137 & " &", N, Nam);
4138 end if;
4140 -- On exit, all actuals match
4142 Indicate_Name_And_Type;
4143 end if;
4144 end Analyze_One_Call;
4146 ---------------------------
4147 -- Analyze_Operator_Call --
4148 ---------------------------
4150 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
4151 Op_Name : constant Name_Id := Chars (Op_Id);
4152 Act1 : constant Node_Id := First_Actual (N);
4153 Act2 : constant Node_Id := Next_Actual (Act1);
4155 begin
4156 -- Binary operator case
4158 if Present (Act2) then
4160 -- If more than two operands, then not binary operator after all
4162 if Present (Next_Actual (Act2)) then
4163 return;
4164 end if;
4166 -- Otherwise action depends on operator
4168 case Op_Name is
4169 when Name_Op_Add
4170 | Name_Op_Divide
4171 | Name_Op_Expon
4172 | Name_Op_Mod
4173 | Name_Op_Multiply
4174 | Name_Op_Rem
4175 | Name_Op_Subtract
4177 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
4179 when Name_Op_And
4180 | Name_Op_Or
4181 | Name_Op_Xor
4183 Find_Boolean_Types (Act1, Act2, Op_Id, N);
4185 when Name_Op_Eq
4186 | Name_Op_Ge
4187 | Name_Op_Gt
4188 | Name_Op_Le
4189 | Name_Op_Lt
4190 | Name_Op_Ne
4192 Find_Comparison_Equality_Types (Act1, Act2, Op_Id, N);
4194 when Name_Op_Concat =>
4195 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
4197 -- Is this when others, or should it be an abort???
4199 when others =>
4200 null;
4201 end case;
4203 -- Unary operator case
4205 else
4206 case Op_Name is
4207 when Name_Op_Abs
4208 | Name_Op_Add
4209 | Name_Op_Subtract
4211 Find_Unary_Types (Act1, Op_Id, N);
4213 when Name_Op_Not =>
4214 Find_Negation_Types (Act1, Op_Id, N);
4216 -- Is this when others correct, or should it be an abort???
4218 when others =>
4219 null;
4220 end case;
4221 end if;
4222 end Analyze_Operator_Call;
4224 -------------------------------------------
4225 -- Analyze_Overloaded_Selected_Component --
4226 -------------------------------------------
4228 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
4229 Nam : constant Node_Id := Prefix (N);
4230 Sel : constant Node_Id := Selector_Name (N);
4231 Comp : Entity_Id;
4232 I : Interp_Index;
4233 It : Interp;
4234 T : Entity_Id;
4236 begin
4237 Set_Etype (Sel, Any_Type);
4239 Get_First_Interp (Nam, I, It);
4240 while Present (It.Typ) loop
4241 if Is_Access_Type (It.Typ) then
4242 T := Designated_Type (It.Typ);
4243 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4244 else
4245 T := It.Typ;
4246 end if;
4248 -- Locate the component. For a private prefix the selector can denote
4249 -- a discriminant.
4251 if Is_Record_Type (T) or else Is_Private_Type (T) then
4253 -- If the prefix is a class-wide type, the visible components are
4254 -- those of the base type.
4256 if Is_Class_Wide_Type (T) then
4257 T := Etype (T);
4258 end if;
4260 Comp := First_Entity (T);
4261 while Present (Comp) loop
4262 if Chars (Comp) = Chars (Sel)
4263 and then Is_Visible_Component (Comp, Sel)
4264 then
4266 -- AI05-105: if the context is an object renaming with
4267 -- an anonymous access type, the expected type of the
4268 -- object must be anonymous. This is a name resolution rule.
4270 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
4271 or else No (Access_Definition (Parent (N)))
4272 or else Is_Anonymous_Access_Type (Etype (Comp))
4273 then
4274 Set_Entity (Sel, Comp);
4275 Set_Etype (Sel, Etype (Comp));
4276 Add_One_Interp (N, Etype (Comp), Etype (Comp));
4277 Check_Implicit_Dereference (N, Etype (Comp));
4279 -- This also specifies a candidate to resolve the name.
4280 -- Further overloading will be resolved from context.
4281 -- The selector name itself does not carry overloading
4282 -- information.
4284 Set_Etype (Nam, It.Typ);
4286 else
4287 -- Named access type in the context of a renaming
4288 -- declaration with an access definition. Remove
4289 -- inapplicable candidate.
4291 Remove_Interp (I);
4292 end if;
4293 end if;
4295 Next_Entity (Comp);
4296 end loop;
4298 elsif Is_Concurrent_Type (T) then
4299 Comp := First_Entity (T);
4300 while Present (Comp)
4301 and then Comp /= First_Private_Entity (T)
4302 loop
4303 if Chars (Comp) = Chars (Sel) then
4304 if Is_Overloadable (Comp) then
4305 Add_One_Interp (Sel, Comp, Etype (Comp));
4306 else
4307 Set_Entity_With_Checks (Sel, Comp);
4308 Generate_Reference (Comp, Sel);
4309 end if;
4311 Set_Etype (Sel, Etype (Comp));
4312 Set_Etype (N, Etype (Comp));
4313 Set_Etype (Nam, It.Typ);
4314 end if;
4316 Next_Entity (Comp);
4317 end loop;
4319 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4320 end if;
4322 Get_Next_Interp (I, It);
4323 end loop;
4325 if Etype (N) = Any_Type
4326 and then not Try_Object_Operation (N)
4327 then
4328 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
4329 Set_Entity (Sel, Any_Id);
4330 Set_Etype (Sel, Any_Type);
4331 end if;
4332 end Analyze_Overloaded_Selected_Component;
4334 ----------------------------------
4335 -- Analyze_Qualified_Expression --
4336 ----------------------------------
4338 procedure Analyze_Qualified_Expression (N : Node_Id) is
4339 Expr : constant Node_Id := Expression (N);
4340 Mark : constant Entity_Id := Subtype_Mark (N);
4342 I : Interp_Index;
4343 It : Interp;
4344 T : Entity_Id;
4346 begin
4347 Find_Type (Mark);
4348 T := Entity (Mark);
4350 if Nkind (Enclosing_Declaration (N)) in
4351 N_Formal_Type_Declaration |
4352 N_Full_Type_Declaration |
4353 N_Incomplete_Type_Declaration |
4354 N_Protected_Type_Declaration |
4355 N_Private_Extension_Declaration |
4356 N_Private_Type_Declaration |
4357 N_Subtype_Declaration |
4358 N_Task_Type_Declaration
4359 and then T = Defining_Identifier (Enclosing_Declaration (N))
4360 then
4361 Error_Msg_N ("current instance not allowed", Mark);
4362 T := Any_Type;
4363 end if;
4365 Set_Etype (N, T);
4367 Analyze_Expression (Expr);
4369 if T = Any_Type then
4370 return;
4371 end if;
4373 Check_Fully_Declared (T, N);
4375 -- If expected type is class-wide, check for exact match before
4376 -- expansion, because if the expression is a dispatching call it
4377 -- may be rewritten as explicit dereference with class-wide result.
4378 -- If expression is overloaded, retain only interpretations that
4379 -- will yield exact matches.
4381 if Is_Class_Wide_Type (T) then
4382 if not Is_Overloaded (Expr) then
4383 if Base_Type (Etype (Expr)) /= Base_Type (T)
4384 and then Etype (Expr) /= Raise_Type
4385 then
4386 if Nkind (Expr) = N_Aggregate then
4387 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
4388 else
4389 Wrong_Type (Expr, T);
4390 end if;
4391 end if;
4393 else
4394 Get_First_Interp (Expr, I, It);
4396 while Present (It.Nam) loop
4397 if Base_Type (It.Typ) /= Base_Type (T) then
4398 Remove_Interp (I);
4399 end if;
4401 Get_Next_Interp (I, It);
4402 end loop;
4403 end if;
4404 end if;
4405 end Analyze_Qualified_Expression;
4407 -----------------------------------
4408 -- Analyze_Quantified_Expression --
4409 -----------------------------------
4411 procedure Analyze_Quantified_Expression (N : Node_Id) is
4412 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
4413 -- Return True if the iterator is part of a quantified expression and
4414 -- the range is known to be statically empty.
4416 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
4417 -- Determine whether if expression If_Expr lacks an else part or if it
4418 -- has one, it evaluates to True.
4420 --------------------
4421 -- Is_Empty_Range --
4422 --------------------
4424 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4425 begin
4426 return Is_Array_Type (Typ)
4427 and then Compile_Time_Known_Bounds (Typ)
4428 and then
4429 Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4430 Expr_Value (Type_High_Bound (Etype (First_Index (Typ))));
4431 end Is_Empty_Range;
4433 -----------------------------
4434 -- No_Else_Or_Trivial_True --
4435 -----------------------------
4437 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4438 Else_Expr : constant Node_Id :=
4439 Next (Next (First (Expressions (If_Expr))));
4440 begin
4441 return
4442 No (Else_Expr)
4443 or else (Compile_Time_Known_Value (Else_Expr)
4444 and then Is_True (Expr_Value (Else_Expr)));
4445 end No_Else_Or_Trivial_True;
4447 -- Local variables
4449 Cond : constant Node_Id := Condition (N);
4450 Loc : constant Source_Ptr := Sloc (N);
4451 Loop_Id : Entity_Id;
4452 QE_Scop : Entity_Id;
4454 -- Start of processing for Analyze_Quantified_Expression
4456 begin
4457 -- Create a scope to emulate the loop-like behavior of the quantified
4458 -- expression. The scope is needed to provide proper visibility of the
4459 -- loop variable.
4461 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
4462 Set_Etype (QE_Scop, Standard_Void_Type);
4463 Set_Scope (QE_Scop, Current_Scope);
4464 Set_Parent (QE_Scop, N);
4466 Push_Scope (QE_Scop);
4468 -- All constituents are preanalyzed and resolved to avoid untimely
4469 -- generation of various temporaries and types. Full analysis and
4470 -- expansion is carried out when the quantified expression is
4471 -- transformed into an expression with actions.
4473 if Present (Iterator_Specification (N)) then
4474 Preanalyze (Iterator_Specification (N));
4476 -- Do not proceed with the analysis when the range of iteration is
4477 -- empty.
4479 if Is_Entity_Name (Name (Iterator_Specification (N)))
4480 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4481 then
4482 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4483 End_Scope;
4485 -- Emit a warning and replace expression with its static value
4487 if All_Present (N) then
4488 Error_Msg_N
4489 ("??quantified expression with ALL "
4490 & "over a null range has value True", N);
4491 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4493 else
4494 Error_Msg_N
4495 ("??quantified expression with SOME "
4496 & "over a null range has value False", N);
4497 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4498 end if;
4500 Analyze (N);
4501 return;
4502 end if;
4504 else pragma Assert (Present (Loop_Parameter_Specification (N)));
4505 declare
4506 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4508 begin
4509 Preanalyze (Loop_Par);
4511 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
4512 and then Parent (Loop_Par) /= N
4513 then
4514 -- The parser cannot distinguish between a loop specification
4515 -- and an iterator specification. If after preanalysis the
4516 -- proper form has been recognized, rewrite the expression to
4517 -- reflect the right kind. This is needed for proper ASIS
4518 -- navigation. If expansion is enabled, the transformation is
4519 -- performed when the expression is rewritten as a loop.
4520 -- Is this still needed???
4522 Set_Iterator_Specification (N,
4523 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
4525 Set_Defining_Identifier (Iterator_Specification (N),
4526 Relocate_Node (Defining_Identifier (Loop_Par)));
4527 Set_Name (Iterator_Specification (N),
4528 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4529 Set_Comes_From_Source (Iterator_Specification (N),
4530 Comes_From_Source (Loop_Parameter_Specification (N)));
4531 Set_Loop_Parameter_Specification (N, Empty);
4532 end if;
4533 end;
4534 end if;
4536 Preanalyze_And_Resolve (Cond, Standard_Boolean);
4538 End_Scope;
4539 Set_Etype (N, Standard_Boolean);
4541 -- Verify that the loop variable is used within the condition of the
4542 -- quantified expression.
4544 if Present (Iterator_Specification (N)) then
4545 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4546 else
4547 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4548 end if;
4550 declare
4551 type Subexpr_Kind is (Full, Conjunct, Disjunct);
4553 procedure Check_Subexpr (Expr : Node_Id; Kind : Subexpr_Kind);
4554 -- Check that the quantified variable appears in every sub-expression
4555 -- of the quantified expression. If Kind is Full, Expr is the full
4556 -- expression. If Kind is Conjunct (resp. Disjunct), Expr is a
4557 -- conjunct (resp. disjunct) of the full expression.
4559 -------------------
4560 -- Check_Subexpr --
4561 -------------------
4563 procedure Check_Subexpr (Expr : Node_Id; Kind : Subexpr_Kind) is
4564 begin
4565 if Nkind (Expr) in N_Op_And | N_And_Then
4566 and then Kind /= Disjunct
4567 then
4568 Check_Subexpr (Left_Opnd (Expr), Conjunct);
4569 Check_Subexpr (Right_Opnd (Expr), Conjunct);
4571 elsif Nkind (Expr) in N_Op_Or | N_Or_Else
4572 and then Kind /= Conjunct
4573 then
4574 Check_Subexpr (Left_Opnd (Expr), Disjunct);
4575 Check_Subexpr (Right_Opnd (Expr), Disjunct);
4577 elsif Kind /= Full
4578 and then not Referenced (Loop_Id, Expr)
4579 then
4580 declare
4581 Sub : constant String :=
4582 (if Kind = Conjunct then "conjunct" else "disjunct");
4583 begin
4584 Error_Msg_NE
4585 ("?.t?unused variable & in " & Sub, Expr, Loop_Id);
4586 Error_Msg_NE
4587 ("\consider extracting " & Sub & " from quantified "
4588 & "expression", Expr, Loop_Id);
4589 end;
4590 end if;
4591 end Check_Subexpr;
4593 begin
4594 if Warn_On_Suspicious_Contract
4595 and then not Is_Internal_Name (Chars (Loop_Id))
4597 -- Generating C, this check causes spurious warnings on inlined
4598 -- postconditions; we can safely disable it because this check
4599 -- was previously performed when analyzing the internally built
4600 -- postconditions procedure.
4602 and then not (Modify_Tree_For_C and In_Inlined_Body)
4603 then
4604 if not Referenced (Loop_Id, Cond) then
4605 Error_Msg_N ("?.t?unused variable &", Loop_Id);
4606 else
4607 Check_Subexpr (Cond, Kind => Full);
4608 end if;
4609 end if;
4610 end;
4612 -- Diagnose a possible misuse of the SOME existential quantifier. When
4613 -- we have a quantified expression of the form:
4615 -- for some X => (if P then Q [else True])
4617 -- any value for X that makes P False results in the if expression being
4618 -- trivially True, and so also results in the quantified expression
4619 -- being trivially True.
4621 if Warn_On_Suspicious_Contract
4622 and then not All_Present (N)
4623 and then Nkind (Cond) = N_If_Expression
4624 and then No_Else_Or_Trivial_True (Cond)
4625 then
4626 Error_Msg_N ("?.t?suspicious expression", N);
4627 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4628 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4629 end if;
4630 end Analyze_Quantified_Expression;
4632 -------------------
4633 -- Analyze_Range --
4634 -------------------
4636 procedure Analyze_Range (N : Node_Id) is
4637 L : constant Node_Id := Low_Bound (N);
4638 H : constant Node_Id := High_Bound (N);
4639 I1, I2 : Interp_Index;
4640 It1, It2 : Interp;
4642 procedure Check_Common_Type (T1, T2 : Entity_Id);
4643 -- Verify the compatibility of two types, and choose the
4644 -- non universal one if the other is universal.
4646 procedure Check_High_Bound (T : Entity_Id);
4647 -- Test one interpretation of the low bound against all those
4648 -- of the high bound.
4650 procedure Check_Universal_Expression (N : Node_Id);
4651 -- In Ada 83, reject bounds of a universal range that are not literals
4652 -- or entity names.
4654 -----------------------
4655 -- Check_Common_Type --
4656 -----------------------
4658 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4659 begin
4660 if Covers (T1 => T1, T2 => T2)
4661 or else
4662 Covers (T1 => T2, T2 => T1)
4663 then
4664 if Is_Universal_Numeric_Type (T1)
4665 or else T1 = Any_Character
4666 then
4667 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4669 elsif T1 = T2 then
4670 Add_One_Interp (N, T1, T1);
4672 else
4673 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4674 end if;
4675 end if;
4676 end Check_Common_Type;
4678 ----------------------
4679 -- Check_High_Bound --
4680 ----------------------
4682 procedure Check_High_Bound (T : Entity_Id) is
4683 begin
4684 if not Is_Overloaded (H) then
4685 Check_Common_Type (T, Etype (H));
4686 else
4687 Get_First_Interp (H, I2, It2);
4688 while Present (It2.Typ) loop
4689 Check_Common_Type (T, It2.Typ);
4690 Get_Next_Interp (I2, It2);
4691 end loop;
4692 end if;
4693 end Check_High_Bound;
4695 --------------------------------
4696 -- Check_Universal_Expression --
4697 --------------------------------
4699 procedure Check_Universal_Expression (N : Node_Id) is
4700 begin
4701 if Etype (N) = Universal_Integer
4702 and then Nkind (N) /= N_Integer_Literal
4703 and then not Is_Entity_Name (N)
4704 and then Nkind (N) /= N_Attribute_Reference
4705 then
4706 Error_Msg_N ("illegal bound in discrete range", N);
4707 end if;
4708 end Check_Universal_Expression;
4710 -- Start of processing for Analyze_Range
4712 begin
4713 Set_Etype (N, Any_Type);
4714 Analyze_Expression (L);
4715 Analyze_Expression (H);
4717 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4718 return;
4720 else
4721 if not Is_Overloaded (L) then
4722 Check_High_Bound (Etype (L));
4723 else
4724 Get_First_Interp (L, I1, It1);
4725 while Present (It1.Typ) loop
4726 Check_High_Bound (It1.Typ);
4727 Get_Next_Interp (I1, It1);
4728 end loop;
4729 end if;
4731 -- If result is Any_Type, then we did not find a compatible pair
4733 if Etype (N) = Any_Type then
4734 Error_Msg_N ("incompatible types in range", N);
4735 end if;
4736 end if;
4738 if Ada_Version = Ada_83
4739 and then
4740 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4741 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4742 then
4743 Check_Universal_Expression (L);
4744 Check_Universal_Expression (H);
4745 end if;
4747 Check_Function_Writable_Actuals (N);
4748 end Analyze_Range;
4750 -----------------------
4751 -- Analyze_Reference --
4752 -----------------------
4754 procedure Analyze_Reference (N : Node_Id) is
4755 P : constant Node_Id := Prefix (N);
4756 E : Entity_Id;
4757 T : Entity_Id;
4758 Acc_Type : Entity_Id;
4760 begin
4761 Analyze (P);
4763 -- An interesting error check, if we take the 'Ref of an object for
4764 -- which a pragma Atomic or Volatile has been given, and the type of the
4765 -- object is not Atomic or Volatile, then we are in trouble. The problem
4766 -- is that no trace of the atomic/volatile status will remain for the
4767 -- backend to respect when it deals with the resulting pointer, since
4768 -- the pointer type will not be marked atomic (it is a pointer to the
4769 -- base type of the object).
4771 -- It is not clear if that can ever occur, but in case it does, we will
4772 -- generate an error message. Not clear if this message can ever be
4773 -- generated, and pretty clear that it represents a bug if it is, still
4774 -- seems worth checking, except in CodePeer mode where we do not really
4775 -- care and don't want to bother the user.
4777 T := Etype (P);
4779 if Is_Entity_Name (P)
4780 and then Is_Object_Reference (P)
4781 and then not CodePeer_Mode
4782 then
4783 E := Entity (P);
4784 T := Etype (P);
4786 if (Has_Atomic_Components (E)
4787 and then not Has_Atomic_Components (T))
4788 or else
4789 (Has_Volatile_Components (E)
4790 and then not Has_Volatile_Components (T))
4791 or else (Is_Atomic (E) and then not Is_Atomic (T))
4792 or else (Is_Volatile (E) and then not Is_Volatile (T))
4793 then
4794 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4795 end if;
4796 end if;
4798 -- Carry on with normal processing
4800 Acc_Type := Create_Itype (E_Allocator_Type, N);
4801 Set_Etype (Acc_Type, Acc_Type);
4802 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4803 Set_Etype (N, Acc_Type);
4804 end Analyze_Reference;
4806 --------------------------------
4807 -- Analyze_Selected_Component --
4808 --------------------------------
4810 -- Prefix is a record type or a task or protected type. In the latter case,
4811 -- the selector must denote a visible entry.
4813 procedure Analyze_Selected_Component (N : Node_Id) is
4814 Name : constant Node_Id := Prefix (N);
4815 Sel : constant Node_Id := Selector_Name (N);
4816 Act_Decl : Node_Id;
4817 Comp : Entity_Id := Empty;
4818 Has_Candidate : Boolean := False;
4819 Hidden_Comp : Entity_Id;
4820 In_Scope : Boolean;
4821 Is_Private_Op : Boolean;
4822 Parent_N : Node_Id;
4823 Prefix_Type : Entity_Id;
4825 Type_To_Use : Entity_Id;
4826 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4827 -- a class-wide type, we use its root type, whose components are
4828 -- present in the class-wide type.
4830 Is_Single_Concurrent_Object : Boolean;
4831 -- Set True if the prefix is a single task or a single protected object
4833 function Constraint_Has_Unprefixed_Discriminant_Reference
4834 (Typ : Entity_Id) return Boolean;
4835 -- Given a subtype that is subject to a discriminant-dependent
4836 -- constraint, returns True if any of the values of the constraint
4837 -- (i.e., any of the index values for an index constraint, any of
4838 -- the discriminant values for a discriminant constraint)
4839 -- are unprefixed discriminant names.
4841 procedure Find_Component_In_Instance (Rec : Entity_Id);
4842 -- In an instance, a component of a private extension may not be visible
4843 -- while it was visible in the generic. Search candidate scope for a
4844 -- component with the proper identifier. This is only done if all other
4845 -- searches have failed. If a match is found, the Etype of both N and
4846 -- Sel are set from this component, and the entity of Sel is set to
4847 -- reference this component. If no match is found, Entity (Sel) remains
4848 -- unset. For a derived type that is an actual of the instance, the
4849 -- desired component may be found in any ancestor.
4851 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4852 -- It is known that the parent of N denotes a subprogram call. Comp
4853 -- is an overloadable component of the concurrent type of the prefix.
4854 -- Determine whether all formals of the parent of N and Comp are mode
4855 -- conformant. If the parent node is not analyzed yet it may be an
4856 -- indexed component rather than a function call.
4858 function Has_Dereference (Nod : Node_Id) return Boolean;
4859 -- Check whether prefix includes a dereference, explicit or implicit,
4860 -- at any recursive level.
4862 function Try_By_Protected_Procedure_Prefixed_View return Boolean;
4863 -- Return True if N is an access attribute whose prefix is a prefixed
4864 -- class-wide (synchronized or protected) interface view for which some
4865 -- interpretation is a procedure with synchronization kind By_Protected
4866 -- _Procedure, and collect all its interpretations (since it may be an
4867 -- overloaded interface primitive); otherwise return False.
4869 ------------------------------------------------------
4870 -- Constraint_Has_Unprefixed_Discriminant_Reference --
4871 ------------------------------------------------------
4873 function Constraint_Has_Unprefixed_Discriminant_Reference
4874 (Typ : Entity_Id) return Boolean
4877 function Is_Discriminant_Name (N : Node_Id) return Boolean is
4878 ((Nkind (N) = N_Identifier)
4879 and then (Ekind (Entity (N)) = E_Discriminant));
4880 begin
4881 if Is_Array_Type (Typ) then
4882 declare
4883 Index : Node_Id := First_Index (Typ);
4884 Rng : Node_Id;
4885 begin
4886 while Present (Index) loop
4887 Rng := Index;
4888 if Nkind (Rng) = N_Subtype_Indication then
4889 Rng := Range_Expression (Constraint (Rng));
4890 end if;
4892 if Nkind (Rng) = N_Range then
4893 if Is_Discriminant_Name (Low_Bound (Rng))
4894 or else Is_Discriminant_Name (High_Bound (Rng))
4895 then
4896 return True;
4897 end if;
4898 end if;
4900 Next_Index (Index);
4901 end loop;
4902 end;
4903 else
4904 declare
4905 Elmt : Elmt_Id := First_Elmt (Discriminant_Constraint (Typ));
4906 begin
4907 while Present (Elmt) loop
4908 if Is_Discriminant_Name (Node (Elmt)) then
4909 return True;
4910 end if;
4911 Next_Elmt (Elmt);
4912 end loop;
4913 end;
4914 end if;
4916 return False;
4917 end Constraint_Has_Unprefixed_Discriminant_Reference;
4919 --------------------------------
4920 -- Find_Component_In_Instance --
4921 --------------------------------
4923 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4924 Comp : Entity_Id;
4925 Typ : Entity_Id;
4927 begin
4928 Typ := Rec;
4929 while Present (Typ) loop
4930 Comp := First_Component (Typ);
4931 while Present (Comp) loop
4932 if Chars (Comp) = Chars (Sel) then
4933 Set_Entity_With_Checks (Sel, Comp);
4934 Set_Etype (Sel, Etype (Comp));
4935 Set_Etype (N, Etype (Comp));
4936 return;
4937 end if;
4939 Next_Component (Comp);
4940 end loop;
4942 -- If not found, the component may be declared in the parent
4943 -- type or its full view, if any.
4945 if Is_Derived_Type (Typ) then
4946 Typ := Etype (Typ);
4948 if Is_Private_Type (Typ) then
4949 Typ := Full_View (Typ);
4950 end if;
4952 else
4953 return;
4954 end if;
4955 end loop;
4957 -- If we fall through, no match, so no changes made
4959 return;
4960 end Find_Component_In_Instance;
4962 ------------------------------
4963 -- Has_Mode_Conformant_Spec --
4964 ------------------------------
4966 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4967 Comp_Param : Entity_Id;
4968 Param : Node_Id;
4969 Param_Typ : Entity_Id;
4971 begin
4972 Comp_Param := First_Formal (Comp);
4974 if Nkind (Parent (N)) = N_Indexed_Component then
4975 Param := First (Expressions (Parent (N)));
4976 else
4977 Param := First (Parameter_Associations (Parent (N)));
4978 end if;
4980 while Present (Comp_Param)
4981 and then Present (Param)
4982 loop
4983 Param_Typ := Find_Parameter_Type (Param);
4985 if Present (Param_Typ)
4986 and then
4987 not Conforming_Types
4988 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4989 then
4990 return False;
4991 end if;
4993 Next_Formal (Comp_Param);
4994 Next (Param);
4995 end loop;
4997 -- One of the specs has additional formals; there is no match, unless
4998 -- this may be an indexing of a parameterless call.
5000 -- Note that when expansion is disabled, the corresponding record
5001 -- type of synchronized types is not constructed, so that there is
5002 -- no point is attempting an interpretation as a prefixed call, as
5003 -- this is bound to fail because the primitive operations will not
5004 -- be properly located.
5006 if Present (Comp_Param) or else Present (Param) then
5007 if Needs_No_Actuals (Comp)
5008 and then Is_Array_Type (Etype (Comp))
5009 and then not Expander_Active
5010 then
5011 return True;
5012 else
5013 return False;
5014 end if;
5015 end if;
5017 return True;
5018 end Has_Mode_Conformant_Spec;
5020 ---------------------
5021 -- Has_Dereference --
5022 ---------------------
5024 function Has_Dereference (Nod : Node_Id) return Boolean is
5025 begin
5026 if Nkind (Nod) = N_Explicit_Dereference then
5027 return True;
5029 elsif Is_Access_Type (Etype (Nod)) then
5030 return True;
5032 elsif Nkind (Nod) in N_Indexed_Component | N_Selected_Component then
5033 return Has_Dereference (Prefix (Nod));
5035 else
5036 return False;
5037 end if;
5038 end Has_Dereference;
5040 ----------------------------------------------
5041 -- Try_By_Protected_Procedure_Prefixed_View --
5042 ----------------------------------------------
5044 function Try_By_Protected_Procedure_Prefixed_View return Boolean is
5045 Candidate : Node_Id := Empty;
5046 Elmt : Elmt_Id;
5047 Prim : Node_Id;
5049 begin
5050 if Nkind (Parent (N)) = N_Attribute_Reference
5051 and then Attribute_Name (Parent (N)) in
5052 Name_Access
5053 | Name_Unchecked_Access
5054 | Name_Unrestricted_Access
5055 and then Is_Class_Wide_Type (Prefix_Type)
5056 and then (Is_Synchronized_Interface (Prefix_Type)
5057 or else Is_Protected_Interface (Prefix_Type))
5058 then
5059 -- If we have not found yet any interpretation then mark this
5060 -- one as the first interpretation (cf. Add_One_Interp).
5062 if No (Etype (Sel)) then
5063 Set_Etype (Sel, Any_Type);
5064 end if;
5066 Elmt := First_Elmt (Primitive_Operations (Etype (Prefix_Type)));
5067 while Present (Elmt) loop
5068 Prim := Node (Elmt);
5070 if Chars (Prim) = Chars (Sel)
5071 and then Is_By_Protected_Procedure (Prim)
5072 then
5073 Candidate := New_Copy (Prim);
5075 -- Skip the controlling formal; required to check type
5076 -- conformance of the target access to protected type
5077 -- (see Conforming_Types).
5079 Set_First_Entity (Candidate,
5080 Next_Entity (First_Entity (Prim)));
5082 Add_One_Interp (Sel, Candidate, Etype (Prim));
5083 Set_Etype (N, Etype (Prim));
5084 end if;
5086 Next_Elmt (Elmt);
5087 end loop;
5088 end if;
5090 -- Propagate overloaded attribute
5092 if Present (Candidate) and then Is_Overloaded (Sel) then
5093 Set_Is_Overloaded (N);
5094 end if;
5096 return Present (Candidate);
5097 end Try_By_Protected_Procedure_Prefixed_View;
5099 -- Start of processing for Analyze_Selected_Component
5101 begin
5102 Set_Etype (N, Any_Type);
5104 if Is_Overloaded (Name) then
5105 Analyze_Overloaded_Selected_Component (N);
5106 return;
5108 elsif Etype (Name) = Any_Type then
5109 Set_Entity (Sel, Any_Id);
5110 Set_Etype (Sel, Any_Type);
5111 return;
5113 else
5114 Prefix_Type := Etype (Name);
5115 end if;
5117 if Is_Access_Type (Prefix_Type) then
5119 -- A RACW object can never be used as prefix of a selected component
5120 -- since that means it is dereferenced without being a controlling
5121 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
5122 -- reporting an error, we must check whether this is actually a
5123 -- dispatching call in prefix form.
5125 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
5126 and then Comes_From_Source (N)
5127 then
5128 if Try_Object_Operation (N) then
5129 return;
5130 else
5131 Error_Msg_N
5132 ("invalid dereference of a remote access-to-class-wide value",
5134 end if;
5136 -- Normal case of selected component applied to access type
5138 else
5139 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5140 Prefix_Type := Implicitly_Designated_Type (Prefix_Type);
5141 end if;
5143 -- If we have an explicit dereference of a remote access-to-class-wide
5144 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
5145 -- have to check for the case of a prefix that is a controlling operand
5146 -- of a prefixed dispatching call, as the dereference is legal in that
5147 -- case. Normally this condition is checked in Validate_Remote_Access_
5148 -- To_Class_Wide_Type, but we have to defer the checking for selected
5149 -- component prefixes because of the prefixed dispatching call case.
5150 -- Note that implicit dereferences are checked for this just above.
5152 elsif Nkind (Name) = N_Explicit_Dereference
5153 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
5154 and then Comes_From_Source (N)
5155 then
5156 if Try_Object_Operation (N) then
5157 return;
5158 else
5159 Error_Msg_N
5160 ("invalid dereference of a remote access-to-class-wide value",
5162 end if;
5163 end if;
5165 -- (Ada 2005): if the prefix is the limited view of a type, and
5166 -- the context already includes the full view, use the full view
5167 -- in what follows, either to retrieve a component of to find
5168 -- a primitive operation. If the prefix is an explicit dereference,
5169 -- set the type of the prefix to reflect this transformation.
5170 -- If the nonlimited view is itself an incomplete type, get the
5171 -- full view if available.
5173 if From_Limited_With (Prefix_Type)
5174 and then Has_Non_Limited_View (Prefix_Type)
5175 then
5176 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
5178 if Nkind (N) = N_Explicit_Dereference then
5179 Set_Etype (Prefix (N), Prefix_Type);
5180 end if;
5181 end if;
5183 if Ekind (Prefix_Type) = E_Private_Subtype then
5184 Prefix_Type := Base_Type (Prefix_Type);
5185 end if;
5187 Type_To_Use := Prefix_Type;
5189 -- For class-wide types, use the entity list of the root type. This
5190 -- indirection is specially important for private extensions because
5191 -- only the root type get switched (not the class-wide type).
5193 if Is_Class_Wide_Type (Prefix_Type) then
5194 Type_To_Use := Root_Type (Prefix_Type);
5195 end if;
5197 -- If the prefix is a single concurrent object, use its name in error
5198 -- messages, rather than that of its anonymous type.
5200 Is_Single_Concurrent_Object :=
5201 Is_Concurrent_Type (Prefix_Type)
5202 and then Is_Internal_Name (Chars (Prefix_Type))
5203 and then not Is_Derived_Type (Prefix_Type)
5204 and then Is_Entity_Name (Name);
5206 -- Avoid initializing Comp if that initialization is not needed
5207 -- (and, more importantly, if the call to First_Entity could fail).
5209 if Has_Discriminants (Type_To_Use)
5210 or else Is_Record_Type (Type_To_Use)
5211 or else Is_Private_Type (Type_To_Use)
5212 or else Is_Concurrent_Type (Type_To_Use)
5213 then
5214 Comp := First_Entity (Type_To_Use);
5215 end if;
5217 -- If the selector has an original discriminant, the node appears in
5218 -- an instance. Replace the discriminant with the corresponding one
5219 -- in the current discriminated type. For nested generics, this must
5220 -- be done transitively, so note the new original discriminant.
5222 if Nkind (Sel) = N_Identifier
5223 and then In_Instance
5224 and then Present (Original_Discriminant (Sel))
5225 then
5226 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
5228 -- Mark entity before rewriting, for completeness and because
5229 -- subsequent semantic checks might examine the original node.
5231 Set_Entity (Sel, Comp);
5232 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
5233 Set_Original_Discriminant (Selector_Name (N), Comp);
5234 Set_Etype (N, Etype (Comp));
5235 Check_Implicit_Dereference (N, Etype (Comp));
5237 elsif Is_Record_Type (Prefix_Type) then
5239 -- Find a component with the given name. If the node is a prefixed
5240 -- call, do not examine components whose visibility may be
5241 -- accidental.
5243 while Present (Comp)
5244 and then not Is_Prefixed_Call (N)
5246 -- When the selector has been resolved to a function then we may be
5247 -- looking at a prefixed call which has been preanalyzed already as
5248 -- part of a class condition. In such cases it is possible for a
5249 -- derived type to declare a component which has the same name as
5250 -- a primitive used in a parent's class condition.
5252 -- Avoid seeing components as possible interpretations of the
5253 -- selected component when this is true.
5255 and then not (Inside_Class_Condition_Preanalysis
5256 and then Present (Entity (Sel))
5257 and then Ekind (Entity (Sel)) = E_Function)
5258 loop
5259 if Chars (Comp) = Chars (Sel)
5260 and then Is_Visible_Component (Comp, N)
5261 then
5262 Set_Entity_With_Checks (Sel, Comp);
5263 Set_Etype (Sel, Etype (Comp));
5265 if Ekind (Comp) = E_Discriminant then
5266 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
5267 Error_Msg_N
5268 ("cannot reference discriminant of unchecked union",
5269 Sel);
5270 end if;
5272 if Is_Generic_Type (Prefix_Type)
5273 or else
5274 Is_Generic_Type (Root_Type (Prefix_Type))
5275 then
5276 Set_Original_Discriminant (Sel, Comp);
5277 end if;
5278 end if;
5280 -- Resolve the prefix early otherwise it is not possible to
5281 -- build the actual subtype of the component: it may need
5282 -- to duplicate this prefix and duplication is only allowed
5283 -- on fully resolved expressions.
5285 Resolve (Name);
5287 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
5288 -- subtypes in a package specification.
5289 -- Example:
5291 -- limited with Pkg;
5292 -- package Pkg is
5293 -- type Acc_Inc is access Pkg.T;
5294 -- X : Acc_Inc;
5295 -- N : Natural := X.all.Comp; -- ERROR, limited view
5296 -- end Pkg; -- Comp is not visible
5298 if Nkind (Name) = N_Explicit_Dereference
5299 and then From_Limited_With (Etype (Prefix (Name)))
5300 and then not Is_Potentially_Use_Visible (Etype (Name))
5301 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
5302 N_Package_Specification
5303 then
5304 Error_Msg_NE
5305 ("premature usage of incomplete}", Prefix (Name),
5306 Etype (Prefix (Name)));
5307 end if;
5309 -- We never need an actual subtype for the case of a selection
5310 -- for a indexed component of a non-packed array, since in
5311 -- this case gigi generates all the checks and can find the
5312 -- necessary bounds information.
5314 -- We also do not need an actual subtype for the case of a
5315 -- first, last, length, or range attribute applied to a
5316 -- non-packed array, since gigi can again get the bounds in
5317 -- these cases (gigi cannot handle the packed case, since it
5318 -- has the bounds of the packed array type, not the original
5319 -- bounds of the type). However, if the prefix is itself a
5320 -- selected component, as in a.b.c (i), gigi may regard a.b.c
5321 -- as a dynamic-sized temporary, so we do generate an actual
5322 -- subtype for this case.
5324 Parent_N := Parent (N);
5326 if not Is_Packed (Etype (Comp))
5327 and then
5328 ((Nkind (Parent_N) = N_Indexed_Component
5329 and then Nkind (Name) /= N_Selected_Component)
5330 or else
5331 (Nkind (Parent_N) = N_Attribute_Reference
5332 and then
5333 Attribute_Name (Parent_N) in Name_First
5334 | Name_Last
5335 | Name_Length
5336 | Name_Range))
5337 then
5338 Set_Etype (N, Etype (Comp));
5340 -- If full analysis is not enabled, we do not generate an
5341 -- actual subtype, because in the absence of expansion
5342 -- reference to a formal of a protected type, for example,
5343 -- will not be properly transformed, and will lead to
5344 -- out-of-scope references in gigi.
5346 -- In all other cases, we currently build an actual subtype.
5347 -- It seems likely that many of these cases can be avoided,
5348 -- but right now, the front end makes direct references to the
5349 -- bounds (e.g. in generating a length check), and if we do
5350 -- not make an actual subtype, we end up getting a direct
5351 -- reference to a discriminant, which will not do.
5353 elsif Full_Analysis then
5354 Act_Decl :=
5355 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
5356 Insert_Action (N, Act_Decl);
5358 if No (Act_Decl) then
5359 Set_Etype (N, Etype (Comp));
5361 else
5362 -- If discriminants were present in the component
5363 -- declaration, they have been replaced by the
5364 -- actual values in the prefix object.
5366 declare
5367 Subt : constant Entity_Id :=
5368 Defining_Identifier (Act_Decl);
5369 begin
5370 Set_Etype (Subt, Base_Type (Etype (Comp)));
5371 Set_Etype (N, Subt);
5372 end;
5373 end if;
5375 -- If Etype (Comp) is an access type whose designated subtype
5376 -- is constrained by an unprefixed discriminant value,
5377 -- then ideally we would build a new subtype with an
5378 -- appropriately prefixed discriminant value and use that
5379 -- instead, as is done in Build_Actual_Subtype_Of_Component.
5380 -- That turns out to be difficult in this context (with
5381 -- Full_Analysis = False, we could be processing a selected
5382 -- component that occurs in a Postcondition pragma;
5383 -- PPC pragmas are odd because they can contain references
5384 -- to formal parameters that occur outside the subprogram).
5385 -- So instead we punt on building a new subtype and we
5386 -- use the base type instead. This might introduce
5387 -- correctness problems if N were the target of an
5388 -- assignment (because a required check might be omitted);
5389 -- fortunately, that's impossible because a reference to the
5390 -- current instance of a type does not denote a variable view
5391 -- when the reference occurs within an aspect_specification.
5392 -- GNAT's Precondition and Postcondition pragmas follow the
5393 -- same rules as a Pre or Post aspect_specification.
5395 elsif Has_Discriminant_Dependent_Constraint (Comp)
5396 and then Ekind (Etype (Comp)) = E_Access_Subtype
5397 and then Constraint_Has_Unprefixed_Discriminant_Reference
5398 (Designated_Type (Etype (Comp)))
5399 then
5400 Set_Etype (N, Base_Type (Etype (Comp)));
5402 -- If Full_Analysis not enabled, just set the Etype
5404 else
5405 Set_Etype (N, Etype (Comp));
5406 end if;
5408 Check_Implicit_Dereference (N, Etype (N));
5409 return;
5410 end if;
5412 -- If the prefix is a private extension, check only the visible
5413 -- components of the partial view. This must include the tag,
5414 -- which can appear in expanded code in a tag check.
5416 if Ekind (Type_To_Use) = E_Record_Type_With_Private
5417 and then Chars (Selector_Name (N)) /= Name_uTag
5418 then
5419 exit when Comp = Last_Entity (Type_To_Use);
5420 end if;
5422 Next_Entity (Comp);
5423 end loop;
5425 -- Ada 2005 (AI-252): The selected component can be interpreted as
5426 -- a prefixed view of a subprogram. Depending on the context, this is
5427 -- either a name that can appear in a renaming declaration, or part
5428 -- of an enclosing call given in prefix form.
5430 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
5431 -- selected component should resolve to a name.
5433 -- Extension feature: Also support calls with prefixed views for
5434 -- untagged record types.
5436 if Ada_Version >= Ada_2005
5437 and then (Is_Tagged_Type (Prefix_Type)
5438 or else Core_Extensions_Allowed)
5439 and then not Is_Concurrent_Type (Prefix_Type)
5440 then
5441 if Nkind (Parent (N)) = N_Generic_Association
5442 or else Nkind (Parent (N)) = N_Requeue_Statement
5443 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
5444 then
5445 if Find_Primitive_Operation (N) then
5446 return;
5447 end if;
5449 elsif Try_By_Protected_Procedure_Prefixed_View then
5450 return;
5452 elsif Try_Object_Operation (N) then
5453 return;
5454 end if;
5456 -- If the transformation fails, it will be necessary to redo the
5457 -- analysis with all errors enabled, to indicate candidate
5458 -- interpretations and reasons for each failure ???
5460 end if;
5462 elsif Is_Private_Type (Prefix_Type) then
5464 -- Allow access only to discriminants of the type. If the type has
5465 -- no full view, gigi uses the parent type for the components, so we
5466 -- do the same here.
5468 if No (Full_View (Prefix_Type)) then
5469 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
5470 Comp := First_Entity (Type_To_Use);
5471 end if;
5473 while Present (Comp) loop
5474 if Chars (Comp) = Chars (Sel) then
5475 if Ekind (Comp) = E_Discriminant then
5476 Set_Entity_With_Checks (Sel, Comp);
5477 Generate_Reference (Comp, Sel);
5479 Set_Etype (Sel, Etype (Comp));
5480 Set_Etype (N, Etype (Comp));
5481 Check_Implicit_Dereference (N, Etype (N));
5483 if Is_Generic_Type (Prefix_Type)
5484 or else Is_Generic_Type (Root_Type (Prefix_Type))
5485 then
5486 Set_Original_Discriminant (Sel, Comp);
5487 end if;
5489 -- Before declaring an error, check whether this is tagged
5490 -- private type and a call to a primitive operation.
5492 elsif Ada_Version >= Ada_2005
5493 and then Is_Tagged_Type (Prefix_Type)
5494 and then Try_Object_Operation (N)
5495 then
5496 return;
5498 else
5499 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5500 Error_Msg_NE ("invisible selector& for }", N, Sel);
5501 Set_Entity (Sel, Any_Id);
5502 Set_Etype (N, Any_Type);
5503 end if;
5505 return;
5506 end if;
5508 Next_Entity (Comp);
5509 end loop;
5511 -- Extension feature: Also support calls with prefixed views for
5512 -- untagged private types.
5514 if Core_Extensions_Allowed then
5515 if Try_Object_Operation (N) then
5516 return;
5517 end if;
5518 end if;
5520 elsif Is_Concurrent_Type (Prefix_Type) then
5522 -- Find visible operation with given name. For a protected type,
5523 -- the possible candidates are discriminants, entries or protected
5524 -- subprograms. For a task type, the set can only include entries or
5525 -- discriminants if the task type is not an enclosing scope. If it
5526 -- is an enclosing scope (e.g. in an inner task) then all entities
5527 -- are visible, but the prefix must denote the enclosing scope, i.e.
5528 -- can only be a direct name or an expanded name.
5530 Set_Etype (Sel, Any_Type);
5531 Hidden_Comp := Empty;
5532 In_Scope := In_Open_Scopes (Prefix_Type);
5533 Is_Private_Op := False;
5535 while Present (Comp) loop
5537 -- Do not examine private operations of the type if not within
5538 -- its scope.
5540 if Chars (Comp) = Chars (Sel) then
5541 if Is_Overloadable (Comp)
5542 and then (In_Scope
5543 or else Comp /= First_Private_Entity (Type_To_Use))
5544 then
5545 Add_One_Interp (Sel, Comp, Etype (Comp));
5546 if Comp = First_Private_Entity (Type_To_Use) then
5547 Is_Private_Op := True;
5548 end if;
5550 -- If the prefix is tagged, the correct interpretation may
5551 -- lie in the primitive or class-wide operations of the
5552 -- type. Perform a simple conformance check to determine
5553 -- whether Try_Object_Operation should be invoked even if
5554 -- a visible entity is found.
5556 if Is_Tagged_Type (Prefix_Type)
5557 and then Nkind (Parent (N)) in N_Function_Call
5558 | N_Indexed_Component
5559 | N_Procedure_Call_Statement
5560 and then Has_Mode_Conformant_Spec (Comp)
5561 then
5562 Has_Candidate := True;
5563 end if;
5565 -- Note: a selected component may not denote a component of a
5566 -- protected type (4.1.3(7)).
5568 elsif Ekind (Comp) in E_Discriminant | E_Entry_Family
5569 or else (In_Scope
5570 and then not Is_Protected_Type (Prefix_Type)
5571 and then Is_Entity_Name (Name))
5572 then
5573 Set_Entity_With_Checks (Sel, Comp);
5574 Generate_Reference (Comp, Sel);
5576 -- The selector is not overloadable, so we have a candidate
5577 -- interpretation.
5579 Has_Candidate := True;
5581 else
5582 if Ekind (Comp) = E_Component then
5583 Hidden_Comp := Comp;
5584 end if;
5586 goto Next_Comp;
5587 end if;
5589 Set_Etype (Sel, Etype (Comp));
5590 Set_Etype (N, Etype (Comp));
5592 if Ekind (Comp) = E_Discriminant then
5593 Set_Original_Discriminant (Sel, Comp);
5594 end if;
5595 end if;
5597 <<Next_Comp>>
5598 if Comp = First_Private_Entity (Type_To_Use) then
5599 if Etype (Sel) /= Any_Type then
5601 -- If the first private entity's name matches, then treat
5602 -- it as a private op: needed for the error check for
5603 -- illegal selection of private entities further below.
5605 if Chars (Comp) = Chars (Sel) then
5606 Is_Private_Op := True;
5607 end if;
5609 -- We have a candidate, so exit the loop
5611 exit;
5613 else
5614 -- Indicate that subsequent operations are private,
5615 -- for better error reporting.
5617 Is_Private_Op := True;
5618 end if;
5619 end if;
5621 -- Do not examine private operations if not within scope of
5622 -- the synchronized type.
5624 exit when not In_Scope
5625 and then
5626 Comp = First_Private_Entity (Base_Type (Prefix_Type));
5627 Next_Entity (Comp);
5628 end loop;
5630 -- If the scope is a current instance, the prefix cannot be an
5631 -- expression of the same type, unless the selector designates a
5632 -- public operation (otherwise that would represent an attempt to
5633 -- reach an internal entity of another synchronized object).
5635 -- This is legal if prefix is an access to such type and there is
5636 -- a dereference, or is a component with a dereferenced prefix.
5637 -- It is also legal if the prefix is a component of a task type,
5638 -- and the selector is one of the task operations.
5640 if In_Scope
5641 and then not Is_Entity_Name (Name)
5642 and then not Has_Dereference (Name)
5643 then
5644 if Is_Task_Type (Prefix_Type)
5645 and then Present (Entity (Sel))
5646 and then Is_Entry (Entity (Sel))
5647 then
5648 null;
5650 elsif Is_Protected_Type (Prefix_Type)
5651 and then Is_Overloadable (Entity (Sel))
5652 and then not Is_Private_Op
5653 then
5654 null;
5656 else
5657 Error_Msg_NE
5658 ("invalid reference to internal operation of some object of "
5659 & "type &", N, Type_To_Use);
5660 Set_Entity (Sel, Any_Id);
5661 Set_Etype (Sel, Any_Type);
5662 return;
5663 end if;
5665 -- Another special case: the prefix may denote an object of the type
5666 -- (but not a type) in which case this is an external call and the
5667 -- operation must be public.
5669 elsif In_Scope
5670 and then Is_Object_Reference (Original_Node (Prefix (N)))
5671 and then Comes_From_Source (N)
5672 and then Is_Private_Op
5673 then
5674 if Present (Hidden_Comp) then
5675 Error_Msg_NE
5676 ("invalid reference to private component of object of type "
5677 & "&", N, Type_To_Use);
5679 else
5680 Error_Msg_NE
5681 ("invalid reference to private operation of some object of "
5682 & "type &", N, Type_To_Use);
5683 end if;
5685 Set_Entity (Sel, Any_Id);
5686 Set_Etype (Sel, Any_Type);
5687 return;
5688 end if;
5690 -- If there is no visible entity with the given name or none of the
5691 -- visible entities are plausible interpretations, check whether
5692 -- there is some other primitive operation with that name.
5694 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
5695 if (Etype (N) = Any_Type
5696 or else not Has_Candidate)
5697 and then Try_Object_Operation (N)
5698 then
5699 return;
5701 -- If the context is not syntactically a procedure call, it
5702 -- may be a call to a primitive function declared outside of
5703 -- the synchronized type.
5705 -- If the context is a procedure call, there might still be
5706 -- an overloading between an entry and a primitive procedure
5707 -- declared outside of the synchronized type, called in prefix
5708 -- notation. This is harder to disambiguate because in one case
5709 -- the controlling formal is implicit ???
5711 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
5712 and then Nkind (Parent (N)) /= N_Indexed_Component
5713 and then Try_Object_Operation (N)
5714 then
5715 return;
5716 end if;
5718 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5719 -- entry or procedure of a tagged concurrent type we must check
5720 -- if there are class-wide subprograms covering the primitive. If
5721 -- true then Try_Object_Operation reports the error.
5723 if Has_Candidate
5724 and then Is_Concurrent_Type (Prefix_Type)
5725 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
5726 then
5727 -- Duplicate the call. This is required to avoid problems with
5728 -- the tree transformations performed by Try_Object_Operation.
5729 -- Set properly the parent of the copied call, because it is
5730 -- about to be reanalyzed.
5732 declare
5733 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5735 begin
5736 Set_Parent (Par, Parent (Parent (N)));
5738 if Try_Object_Operation
5739 (Sinfo.Nodes.Name (Par), CW_Test_Only => True)
5740 then
5741 return;
5742 end if;
5743 end;
5744 end if;
5745 end if;
5747 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
5749 -- Case of a prefix of a protected type: selector might denote
5750 -- an invisible private component.
5752 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5753 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5754 Next_Entity (Comp);
5755 end loop;
5757 if Present (Comp) then
5758 if Is_Single_Concurrent_Object then
5759 Error_Msg_Node_2 := Entity (Name);
5760 Error_Msg_NE ("invisible selector& for &", N, Sel);
5762 else
5763 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5764 Error_Msg_NE ("invisible selector& for }", N, Sel);
5765 end if;
5766 return;
5767 end if;
5768 end if;
5770 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5772 -- Extension feature: Also support calls with prefixed views for
5773 -- untagged types.
5775 elsif Core_Extensions_Allowed
5776 and then Try_Object_Operation (N)
5777 then
5778 return;
5780 else
5781 -- Invalid prefix
5783 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5784 end if;
5786 -- If N still has no type, the component is not defined in the prefix
5788 if Etype (N) = Any_Type then
5790 if Is_Single_Concurrent_Object then
5791 Error_Msg_Node_2 := Entity (Name);
5792 Error_Msg_NE ("no selector& for&", N, Sel);
5794 Check_Misspelled_Selector (Type_To_Use, Sel);
5796 -- If this is a derived formal type, the parent may have different
5797 -- visibility at this point. Try for an inherited component before
5798 -- reporting an error.
5800 elsif Is_Generic_Type (Prefix_Type)
5801 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
5802 and then Prefix_Type /= Etype (Prefix_Type)
5803 and then Is_Record_Type (Etype (Prefix_Type))
5804 then
5805 Set_Etype (Prefix (N), Etype (Prefix_Type));
5806 Analyze_Selected_Component (N);
5807 return;
5809 -- Similarly, if this is the actual for a formal derived type, or
5810 -- a derived type thereof, the component inherited from the generic
5811 -- parent may not be visible in the actual, but the selected
5812 -- component is legal. Climb up the derivation chain of the generic
5813 -- parent type until we find the proper ancestor type.
5815 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5816 declare
5817 Par : Entity_Id := Prefix_Type;
5818 begin
5819 -- Climb up derivation chain to generic actual subtype
5821 while not Is_Generic_Actual_Type (Par) loop
5822 if Ekind (Par) = E_Record_Type then
5823 Par := Parent_Subtype (Par);
5824 exit when No (Par);
5825 else
5826 exit when Par = Etype (Par);
5827 Par := Etype (Par);
5828 end if;
5829 end loop;
5831 if Present (Par) and then Is_Generic_Actual_Type (Par) then
5833 -- Now look for component in ancestor types
5835 Par := Generic_Parent_Type (Declaration_Node (Par));
5836 loop
5837 Find_Component_In_Instance (Par);
5838 exit when Present (Entity (Sel))
5839 or else Par = Etype (Par);
5840 Par := Etype (Par);
5841 end loop;
5843 -- Another special case: the type is an extension of a private
5844 -- type T, either is an actual in an instance or is immediately
5845 -- visible, and we are in the body of the instance, which means
5846 -- the generic body had a full view of the type declaration for
5847 -- T or some ancestor that defines the component in question.
5848 -- This happens because Is_Visible_Component returned False on
5849 -- this component, as T or the ancestor is still private since
5850 -- the Has_Private_View mechanism is bypassed because T or the
5851 -- ancestor is not directly referenced in the generic body.
5853 elsif Is_Derived_Type (Type_To_Use)
5854 and then (Used_As_Generic_Actual (Type_To_Use)
5855 or else Is_Immediately_Visible (Type_To_Use))
5856 and then In_Instance_Body
5857 then
5858 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
5859 end if;
5860 end;
5862 -- The search above must have eventually succeeded, since the
5863 -- selected component was legal in the generic.
5865 if No (Entity (Sel)) then
5866 raise Program_Error;
5867 end if;
5869 return;
5871 -- Component not found, specialize error message when appropriate
5873 else
5874 if Ekind (Prefix_Type) = E_Record_Subtype then
5876 -- Check whether this is a component of the base type which
5877 -- is absent from a statically constrained subtype. This will
5878 -- raise constraint error at run time, but is not a compile-
5879 -- time error. When the selector is illegal for base type as
5880 -- well fall through and generate a compilation error anyway.
5882 Comp := First_Component (Base_Type (Prefix_Type));
5883 while Present (Comp) loop
5884 if Chars (Comp) = Chars (Sel)
5885 and then Is_Visible_Component (Comp, Sel)
5886 then
5887 Set_Entity_With_Checks (Sel, Comp);
5888 Generate_Reference (Comp, Sel);
5889 Set_Etype (Sel, Etype (Comp));
5890 Set_Etype (N, Etype (Comp));
5892 -- Emit appropriate message. The node will be replaced
5893 -- by an appropriate raise statement.
5895 -- Note that in SPARK mode, as with all calls to apply a
5896 -- compile time constraint error, this will be made into
5897 -- an error to simplify the processing of the formal
5898 -- verification backend.
5900 Apply_Compile_Time_Constraint_Error
5901 (N, "component not present in }??",
5902 CE_Discriminant_Check_Failed,
5903 Ent => Prefix_Type,
5904 Emit_Message =>
5905 SPARK_Mode = On or not In_Instance_Not_Visible);
5906 return;
5907 end if;
5909 Next_Component (Comp);
5910 end loop;
5912 end if;
5914 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5915 Error_Msg_NE ("no selector& for}", N, Sel);
5917 -- Add information in the case of an incomplete prefix
5919 if Is_Incomplete_Type (Type_To_Use) then
5920 declare
5921 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5923 begin
5924 if From_Limited_With (Scope (Type_To_Use)) then
5925 Error_Msg_NE
5926 ("\limited view of& has no components", N, Inc);
5928 else
5929 Error_Msg_NE
5930 ("\premature usage of incomplete type&", N, Inc);
5932 if Nkind (Parent (Inc)) =
5933 N_Incomplete_Type_Declaration
5934 then
5935 -- Record location of premature use in entity so that
5936 -- a continuation message is generated when the
5937 -- completion is seen.
5939 Set_Premature_Use (Parent (Inc), N);
5940 end if;
5941 end if;
5942 end;
5943 end if;
5945 Check_Misspelled_Selector (Type_To_Use, Sel);
5946 end if;
5948 Set_Entity (Sel, Any_Id);
5949 Set_Etype (Sel, Any_Type);
5950 end if;
5951 end Analyze_Selected_Component;
5953 ---------------------------
5954 -- Analyze_Short_Circuit --
5955 ---------------------------
5957 procedure Analyze_Short_Circuit (N : Node_Id) is
5958 L : constant Node_Id := Left_Opnd (N);
5959 R : constant Node_Id := Right_Opnd (N);
5960 Ind : Interp_Index;
5961 It : Interp;
5963 begin
5964 Set_Etype (N, Any_Type);
5965 Analyze_Expression (L);
5966 Analyze_Expression (R);
5968 if not Is_Overloaded (L) then
5969 if Root_Type (Etype (L)) = Standard_Boolean
5970 and then Has_Compatible_Type (R, Etype (L))
5971 then
5972 Add_One_Interp (N, Etype (L), Etype (L));
5973 end if;
5975 else
5976 Get_First_Interp (L, Ind, It);
5977 while Present (It.Typ) loop
5978 if Root_Type (It.Typ) = Standard_Boolean
5979 and then Has_Compatible_Type (R, It.Typ)
5980 then
5981 Add_One_Interp (N, It.Typ, It.Typ);
5982 end if;
5984 Get_Next_Interp (Ind, It);
5985 end loop;
5986 end if;
5988 -- Here we have failed to find an interpretation. Clearly we know that
5989 -- it is not the case that both operands can have an interpretation of
5990 -- Boolean, but this is by far the most likely intended interpretation.
5991 -- So we simply resolve both operands as Booleans, and at least one of
5992 -- these resolutions will generate an error message, and we do not need
5993 -- to give another error message on the short circuit operation itself.
5995 if Etype (N) = Any_Type then
5996 Resolve (L, Standard_Boolean);
5997 Resolve (R, Standard_Boolean);
5998 Set_Etype (N, Standard_Boolean);
5999 end if;
6000 end Analyze_Short_Circuit;
6002 -------------------
6003 -- Analyze_Slice --
6004 -------------------
6006 procedure Analyze_Slice (N : Node_Id) is
6007 D : constant Node_Id := Discrete_Range (N);
6008 P : constant Node_Id := Prefix (N);
6009 Array_Type : Entity_Id;
6010 Index_Type : Entity_Id;
6012 procedure Analyze_Overloaded_Slice;
6013 -- If the prefix is overloaded, select those interpretations that
6014 -- yield a one-dimensional array type.
6016 ------------------------------
6017 -- Analyze_Overloaded_Slice --
6018 ------------------------------
6020 procedure Analyze_Overloaded_Slice is
6021 I : Interp_Index;
6022 It : Interp;
6023 Typ : Entity_Id;
6025 begin
6026 Set_Etype (N, Any_Type);
6028 Get_First_Interp (P, I, It);
6029 while Present (It.Nam) loop
6030 Typ := It.Typ;
6032 if Is_Access_Type (Typ) then
6033 Typ := Designated_Type (Typ);
6034 Error_Msg_NW
6035 (Warn_On_Dereference, "?d?implicit dereference", N);
6036 end if;
6038 if Is_Array_Type (Typ)
6039 and then Number_Dimensions (Typ) = 1
6040 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
6041 then
6042 Add_One_Interp (N, Typ, Typ);
6043 end if;
6045 Get_Next_Interp (I, It);
6046 end loop;
6048 if Etype (N) = Any_Type then
6049 Error_Msg_N ("expect array type in prefix of slice", N);
6050 end if;
6051 end Analyze_Overloaded_Slice;
6053 -- Start of processing for Analyze_Slice
6055 begin
6056 Analyze (P);
6057 Analyze (D);
6059 if Is_Overloaded (P) then
6060 Analyze_Overloaded_Slice;
6062 else
6063 Array_Type := Etype (P);
6064 Set_Etype (N, Any_Type);
6066 if Is_Access_Type (Array_Type) then
6067 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
6068 Array_Type := Implicitly_Designated_Type (Array_Type);
6069 end if;
6071 if not Is_Array_Type (Array_Type) then
6072 Wrong_Type (P, Any_Array);
6074 elsif Number_Dimensions (Array_Type) > 1 then
6075 Error_Msg_N
6076 ("type is not one-dimensional array in slice prefix", N);
6078 else
6079 if Ekind (Array_Type) = E_String_Literal_Subtype then
6080 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
6081 else
6082 Index_Type := Etype (First_Index (Array_Type));
6083 end if;
6085 if not Has_Compatible_Type (D, Index_Type) then
6086 Wrong_Type (D, Index_Type);
6087 else
6088 Set_Etype (N, Array_Type);
6089 end if;
6090 end if;
6091 end if;
6092 end Analyze_Slice;
6094 -----------------------------
6095 -- Analyze_Type_Conversion --
6096 -----------------------------
6098 procedure Analyze_Type_Conversion (N : Node_Id) is
6099 Expr : constant Node_Id := Expression (N);
6100 Mark : constant Entity_Id := Subtype_Mark (N);
6102 Typ : Entity_Id;
6104 begin
6105 -- If Conversion_OK is set, then the Etype is already set, and the only
6106 -- processing required is to analyze the expression. This is used to
6107 -- construct certain "illegal" conversions which are not allowed by Ada
6108 -- semantics, but can be handled by Gigi, see Sinfo for further details.
6110 if Conversion_OK (N) then
6111 Analyze (Expr);
6112 return;
6113 end if;
6115 -- Otherwise full type analysis is required, as well as some semantic
6116 -- checks to make sure the argument of the conversion is appropriate.
6118 Find_Type (Mark);
6119 Typ := Entity (Mark);
6120 Set_Etype (N, Typ);
6122 Analyze_Expression (Expr);
6124 Check_Fully_Declared (Typ, N);
6125 Validate_Remote_Type_Type_Conversion (N);
6127 -- Only remaining step is validity checks on the argument. These
6128 -- are skipped if the conversion does not come from the source.
6130 if not Comes_From_Source (N) then
6131 return;
6133 -- If there was an error in a generic unit, no need to replicate the
6134 -- error message. Conversely, constant-folding in the generic may
6135 -- transform the argument of a conversion into a string literal, which
6136 -- is legal. Therefore the following tests are not performed in an
6137 -- instance. The same applies to an inlined body.
6139 elsif In_Instance or In_Inlined_Body then
6140 return;
6142 elsif Nkind (Expr) = N_Null then
6143 Error_Msg_N ("argument of conversion cannot be null", N);
6144 Error_Msg_N ("\use qualified expression instead", N);
6145 Set_Etype (N, Any_Type);
6147 elsif Nkind (Expr) = N_Aggregate then
6148 Error_Msg_N ("argument of conversion cannot be aggregate", N);
6149 Error_Msg_N ("\use qualified expression instead", N);
6151 elsif Nkind (Expr) = N_Allocator then
6152 Error_Msg_N ("argument of conversion cannot be allocator", N);
6153 Error_Msg_N ("\use qualified expression instead", N);
6155 elsif Nkind (Expr) = N_String_Literal then
6156 Error_Msg_N ("argument of conversion cannot be string literal", N);
6157 Error_Msg_N ("\use qualified expression instead", N);
6159 elsif Nkind (Expr) = N_Character_Literal then
6160 if Ada_Version = Ada_83 then
6161 Resolve (Expr, Typ);
6162 else
6163 Error_Msg_N
6164 ("argument of conversion cannot be character literal", N);
6165 Error_Msg_N ("\use qualified expression instead", N);
6166 end if;
6168 elsif Nkind (Expr) = N_Attribute_Reference
6169 and then Attribute_Name (Expr) in Name_Access
6170 | Name_Unchecked_Access
6171 | Name_Unrestricted_Access
6172 then
6173 Error_Msg_N
6174 ("argument of conversion cannot be access attribute", N);
6175 Error_Msg_N ("\use qualified expression instead", N);
6176 end if;
6178 -- A formal parameter of a specific tagged type whose related subprogram
6179 -- is subject to pragma Extensions_Visible with value "False" cannot
6180 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
6181 -- internally generated expressions.
6183 if Is_Class_Wide_Type (Typ)
6184 and then Comes_From_Source (Expr)
6185 and then Is_EVF_Expression (Expr)
6186 then
6187 Error_Msg_N
6188 ("formal parameter cannot be converted to class-wide type when "
6189 & "Extensions_Visible is False", Expr);
6190 end if;
6191 end Analyze_Type_Conversion;
6193 ----------------------
6194 -- Analyze_Unary_Op --
6195 ----------------------
6197 procedure Analyze_Unary_Op (N : Node_Id) is
6198 R : constant Node_Id := Right_Opnd (N);
6200 Op_Id : Entity_Id;
6202 begin
6203 Set_Etype (N, Any_Type);
6204 Candidate_Type := Empty;
6206 Analyze_Expression (R);
6208 -- If the entity is already set, the node is the instantiation of a
6209 -- generic node with a non-local reference, or was manufactured by a
6210 -- call to Make_Op_xxx. In either case the entity is known to be valid,
6211 -- and we do not need to collect interpretations, instead we just get
6212 -- the single possible interpretation.
6214 if Present (Entity (N)) then
6215 Op_Id := Entity (N);
6217 if Ekind (Op_Id) = E_Operator then
6218 Find_Unary_Types (R, Op_Id, N);
6219 else
6220 Add_One_Interp (N, Op_Id, Etype (Op_Id));
6221 end if;
6223 else
6224 Op_Id := Get_Name_Entity_Id (Chars (N));
6225 while Present (Op_Id) loop
6226 if Ekind (Op_Id) = E_Operator then
6227 if No (Next_Entity (First_Entity (Op_Id))) then
6228 Find_Unary_Types (R, Op_Id, N);
6229 end if;
6231 elsif Is_Overloadable (Op_Id) then
6232 Analyze_User_Defined_Unary_Op (N, Op_Id);
6233 end if;
6235 Op_Id := Homonym (Op_Id);
6236 end loop;
6237 end if;
6239 Operator_Check (N);
6240 end Analyze_Unary_Op;
6242 ----------------------------------
6243 -- Analyze_Unchecked_Expression --
6244 ----------------------------------
6246 procedure Analyze_Unchecked_Expression (N : Node_Id) is
6247 Expr : constant Node_Id := Expression (N);
6249 begin
6250 Analyze (Expr, Suppress => All_Checks);
6251 Set_Etype (N, Etype (Expr));
6252 Save_Interps (Expr, N);
6253 end Analyze_Unchecked_Expression;
6255 ---------------------------------------
6256 -- Analyze_Unchecked_Type_Conversion --
6257 ---------------------------------------
6259 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
6260 Expr : constant Node_Id := Expression (N);
6261 Mark : constant Entity_Id := Subtype_Mark (N);
6263 begin
6264 Find_Type (Mark);
6265 Set_Etype (N, Entity (Mark));
6266 Analyze_Expression (Expr);
6267 end Analyze_Unchecked_Type_Conversion;
6269 ------------------------------------
6270 -- Analyze_User_Defined_Binary_Op --
6271 ------------------------------------
6273 procedure Analyze_User_Defined_Binary_Op
6274 (N : Node_Id;
6275 Op_Id : Entity_Id) is
6276 begin
6277 declare
6278 F1 : constant Entity_Id := First_Formal (Op_Id);
6279 F2 : constant Entity_Id := Next_Formal (F1);
6281 begin
6282 -- Verify that Op_Id is a visible binary function. Note that since
6283 -- we know Op_Id is overloaded, potentially use visible means use
6284 -- visible for sure (RM 9.4(11)). Be prepared for previous errors.
6286 if Ekind (Op_Id) = E_Function
6287 and then Present (F2)
6288 and then (Is_Immediately_Visible (Op_Id)
6289 or else Is_Potentially_Use_Visible (Op_Id))
6290 and then (Has_Compatible_Type (Left_Opnd (N), Etype (F1))
6291 or else Etype (F1) = Any_Type)
6292 and then (Has_Compatible_Type (Right_Opnd (N), Etype (F2))
6293 or else Etype (F2) = Any_Type)
6294 then
6295 Add_One_Interp (N, Op_Id, Base_Type (Etype (Op_Id)));
6297 -- If the operands are overloaded, indicate that the current
6298 -- type is a viable candidate. This is redundant in most cases,
6299 -- but for equality and comparison operators where the context
6300 -- does not impose a type on the operands, setting the proper
6301 -- type is necessary to avoid subsequent ambiguities during
6302 -- resolution, when both user-defined and predefined operators
6303 -- may be candidates.
6305 if Is_Overloaded (Left_Opnd (N)) then
6306 Set_Etype (Left_Opnd (N), Etype (F1));
6307 end if;
6309 if Is_Overloaded (Right_Opnd (N)) then
6310 Set_Etype (Right_Opnd (N), Etype (F2));
6311 end if;
6313 if Debug_Flag_E then
6314 Write_Str ("user defined operator ");
6315 Write_Name (Chars (Op_Id));
6316 Write_Str (" on node ");
6317 Write_Int (Int (N));
6318 Write_Eol;
6319 end if;
6320 end if;
6321 end;
6322 end Analyze_User_Defined_Binary_Op;
6324 -----------------------------------
6325 -- Analyze_User_Defined_Unary_Op --
6326 -----------------------------------
6328 procedure Analyze_User_Defined_Unary_Op
6329 (N : Node_Id;
6330 Op_Id : Entity_Id)
6332 begin
6333 -- Only do analysis if the operator Comes_From_Source, since otherwise
6334 -- the operator was generated by the expander, and all such operators
6335 -- always refer to the operators in package Standard.
6337 if Comes_From_Source (N) then
6338 declare
6339 F : constant Entity_Id := First_Formal (Op_Id);
6341 begin
6342 -- Verify that Op_Id is a visible unary function. Note that since
6343 -- we know Op_Id is overloaded, potentially use visible means use
6344 -- visible for sure (RM 9.4(11)).
6346 if Ekind (Op_Id) = E_Function
6347 and then No (Next_Formal (F))
6348 and then (Is_Immediately_Visible (Op_Id)
6349 or else Is_Potentially_Use_Visible (Op_Id))
6350 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
6351 then
6352 Add_One_Interp (N, Op_Id, Etype (Op_Id));
6353 end if;
6354 end;
6355 end if;
6356 end Analyze_User_Defined_Unary_Op;
6358 ---------------------------
6359 -- Check_Arithmetic_Pair --
6360 ---------------------------
6362 procedure Check_Arithmetic_Pair
6363 (T1, T2 : Entity_Id;
6364 Op_Id : Entity_Id;
6365 N : Node_Id)
6367 Op_Name : constant Name_Id := Chars (Op_Id);
6369 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
6370 -- Check whether the fixed-point type Typ has a user-defined operator
6371 -- (multiplication or division) that should hide the corresponding
6372 -- predefined operator. Used to implement Ada 2005 AI-264, to make
6373 -- such operators more visible and therefore useful.
6375 -- If the name of the operation is an expanded name with prefix
6376 -- Standard, the predefined universal fixed operator is available,
6377 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
6379 ------------------
6380 -- Has_Fixed_Op --
6381 ------------------
6383 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
6384 Bas : constant Entity_Id := Base_Type (Typ);
6385 Ent : Entity_Id;
6386 F1 : Entity_Id;
6387 F2 : Entity_Id;
6389 begin
6390 -- If the universal_fixed operation is given explicitly the rule
6391 -- concerning primitive operations of the type do not apply.
6393 if Nkind (N) = N_Function_Call
6394 and then Nkind (Name (N)) = N_Expanded_Name
6395 and then Entity (Prefix (Name (N))) = Standard_Standard
6396 then
6397 return False;
6398 end if;
6400 -- The operation is treated as primitive if it is declared in the
6401 -- same scope as the type, and therefore on the same entity chain.
6403 Ent := Next_Entity (Typ);
6404 while Present (Ent) loop
6405 if Chars (Ent) = Chars (Op) then
6406 F1 := First_Formal (Ent);
6407 F2 := Next_Formal (F1);
6409 -- The operation counts as primitive if either operand or
6410 -- result are of the given base type, and both operands are
6411 -- fixed point types.
6413 if (Base_Type (Etype (F1)) = Bas
6414 and then Is_Fixed_Point_Type (Etype (F2)))
6416 or else
6417 (Base_Type (Etype (F2)) = Bas
6418 and then Is_Fixed_Point_Type (Etype (F1)))
6420 or else
6421 (Base_Type (Etype (Ent)) = Bas
6422 and then Is_Fixed_Point_Type (Etype (F1))
6423 and then Is_Fixed_Point_Type (Etype (F2)))
6424 then
6425 return True;
6426 end if;
6427 end if;
6429 Next_Entity (Ent);
6430 end loop;
6432 return False;
6433 end Has_Fixed_Op;
6435 -- Start of processing for Check_Arithmetic_Pair
6437 begin
6438 if Op_Name in Name_Op_Add | Name_Op_Subtract then
6439 if Is_Numeric_Type (T1)
6440 and then Is_Numeric_Type (T2)
6441 and then (Covers (T1 => T1, T2 => T2)
6442 or else
6443 Covers (T1 => T2, T2 => T1))
6444 then
6445 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6446 end if;
6448 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide then
6449 if Is_Fixed_Point_Type (T1)
6450 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
6451 then
6452 -- Add one interpretation with universal fixed result
6454 if not Has_Fixed_Op (T1, Op_Id)
6455 or else Nkind (Parent (N)) = N_Type_Conversion
6456 then
6457 Add_One_Interp (N, Op_Id, Universal_Fixed);
6458 end if;
6460 elsif Is_Fixed_Point_Type (T2)
6461 and then T1 = Universal_Real
6462 and then
6463 (not Has_Fixed_Op (T1, Op_Id)
6464 or else Nkind (Parent (N)) = N_Type_Conversion)
6465 then
6466 Add_One_Interp (N, Op_Id, Universal_Fixed);
6468 elsif Is_Numeric_Type (T1)
6469 and then Is_Numeric_Type (T2)
6470 and then (Covers (T1 => T1, T2 => T2)
6471 or else
6472 Covers (T1 => T2, T2 => T1))
6473 then
6474 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6476 elsif Is_Fixed_Point_Type (T1)
6477 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6478 or else T2 = Universal_Integer)
6479 then
6480 Add_One_Interp (N, Op_Id, T1);
6482 elsif T2 = Universal_Real
6483 and then Base_Type (T1) = Base_Type (Standard_Integer)
6484 and then Op_Name = Name_Op_Multiply
6485 then
6486 Add_One_Interp (N, Op_Id, Any_Fixed);
6488 elsif T1 = Universal_Real
6489 and then Base_Type (T2) = Base_Type (Standard_Integer)
6490 then
6491 Add_One_Interp (N, Op_Id, Any_Fixed);
6493 elsif Is_Fixed_Point_Type (T2)
6494 and then (Base_Type (T1) = Base_Type (Standard_Integer)
6495 or else T1 = Universal_Integer)
6496 and then Op_Name = Name_Op_Multiply
6497 then
6498 Add_One_Interp (N, Op_Id, T2);
6500 elsif T1 = Universal_Real and then T2 = Universal_Integer then
6501 Add_One_Interp (N, Op_Id, T1);
6503 elsif T2 = Universal_Real
6504 and then T1 = Universal_Integer
6505 and then Op_Name = Name_Op_Multiply
6506 then
6507 Add_One_Interp (N, Op_Id, T2);
6508 end if;
6510 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
6512 if Is_Integer_Type (T1)
6513 and then (Covers (T1 => T1, T2 => T2)
6514 or else
6515 Covers (T1 => T2, T2 => T1))
6516 then
6517 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6518 end if;
6520 elsif Op_Name = Name_Op_Expon then
6521 if Is_Numeric_Type (T1)
6522 and then not Is_Fixed_Point_Type (T1)
6523 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6524 or else T2 = Universal_Integer)
6525 then
6526 Add_One_Interp (N, Op_Id, Base_Type (T1));
6527 end if;
6529 else pragma Assert (Nkind (N) in N_Op_Shift);
6531 -- If not one of the predefined operators, the node may be one
6532 -- of the intrinsic functions. Its kind is always specific, and
6533 -- we can use it directly, rather than the name of the operation.
6535 if Is_Integer_Type (T1)
6536 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6537 or else T2 = Universal_Integer)
6538 then
6539 Add_One_Interp (N, Op_Id, Base_Type (T1));
6540 end if;
6541 end if;
6542 end Check_Arithmetic_Pair;
6544 -------------------------------
6545 -- Check_Misspelled_Selector --
6546 -------------------------------
6548 procedure Check_Misspelled_Selector
6549 (Prefix : Entity_Id;
6550 Sel : Node_Id)
6552 Max_Suggestions : constant := 2;
6553 Nr_Of_Suggestions : Natural := 0;
6555 Suggestion_1 : Entity_Id := Empty;
6556 Suggestion_2 : Entity_Id := Empty;
6558 Comp : Entity_Id;
6560 begin
6561 -- All the components of the prefix of selector Sel are matched against
6562 -- Sel and a count is maintained of possible misspellings. When at
6563 -- the end of the analysis there are one or two (not more) possible
6564 -- misspellings, these misspellings will be suggested as possible
6565 -- correction.
6567 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
6569 -- Concurrent types should be handled as well ???
6571 return;
6572 end if;
6574 Comp := First_Entity (Prefix);
6575 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
6576 if Is_Visible_Component (Comp, Sel) then
6577 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
6578 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
6580 case Nr_Of_Suggestions is
6581 when 1 => Suggestion_1 := Comp;
6582 when 2 => Suggestion_2 := Comp;
6583 when others => null;
6584 end case;
6585 end if;
6586 end if;
6588 Next_Entity (Comp);
6589 end loop;
6591 -- Report at most two suggestions
6593 if Nr_Of_Suggestions = 1 then
6594 Error_Msg_NE -- CODEFIX
6595 ("\possible misspelling of&", Sel, Suggestion_1);
6597 elsif Nr_Of_Suggestions = 2 then
6598 Error_Msg_Node_2 := Suggestion_2;
6599 Error_Msg_NE -- CODEFIX
6600 ("\possible misspelling of& or&", Sel, Suggestion_1);
6601 end if;
6602 end Check_Misspelled_Selector;
6604 -------------------
6605 -- Diagnose_Call --
6606 -------------------
6608 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
6609 Actual : Node_Id;
6610 X : Interp_Index;
6611 It : Interp;
6612 Err_Mode : Boolean;
6613 New_Nam : Node_Id;
6614 Num_Actuals : Natural;
6615 Num_Interps : Natural;
6616 Void_Interp_Seen : Boolean := False;
6618 Success : Boolean;
6619 pragma Warnings (Off, Boolean);
6621 begin
6622 Num_Actuals := 0;
6623 Actual := First_Actual (N);
6625 while Present (Actual) loop
6626 -- Ada 2005 (AI-50217): Post an error in case of premature
6627 -- usage of an entity from the limited view.
6629 if not Analyzed (Etype (Actual))
6630 and then From_Limited_With (Etype (Actual))
6631 and then Ada_Version >= Ada_2005
6632 then
6633 Error_Msg_Qual_Level := 1;
6634 Error_Msg_NE
6635 ("missing with_clause for scope of imported type&",
6636 Actual, Etype (Actual));
6637 Error_Msg_Qual_Level := 0;
6638 end if;
6640 Num_Actuals := Num_Actuals + 1;
6641 Next_Actual (Actual);
6642 end loop;
6644 -- Before listing the possible candidates, check whether this is
6645 -- a prefix of a selected component that has been rewritten as a
6646 -- parameterless function call because there is a callable candidate
6647 -- interpretation. If there is a hidden package in the list of homonyms
6648 -- of the function name (bad programming style in any case) suggest that
6649 -- this is the intended entity.
6651 if No (Parameter_Associations (N))
6652 and then Nkind (Parent (N)) = N_Selected_Component
6653 and then Nkind (Parent (Parent (N))) in N_Declaration
6654 and then Is_Overloaded (Nam)
6655 then
6656 declare
6657 Ent : Entity_Id;
6659 begin
6660 Ent := Current_Entity (Nam);
6661 while Present (Ent) loop
6662 if Ekind (Ent) = E_Package then
6663 Error_Msg_N
6664 ("no legal interpretations as function call,!", Nam);
6665 Error_Msg_NE ("\package& is not visible", N, Ent);
6667 Rewrite (Parent (N),
6668 New_Occurrence_Of (Any_Type, Sloc (N)));
6669 return;
6670 end if;
6672 Ent := Homonym (Ent);
6673 end loop;
6674 end;
6675 end if;
6677 -- If this is a call to an operation of a concurrent type, the failed
6678 -- interpretations have been removed from the name. Recover them now
6679 -- in order to provide full diagnostics.
6681 if Nkind (Parent (Nam)) = N_Selected_Component then
6682 Set_Entity (Nam, Empty);
6683 New_Nam := New_Copy_Tree (Parent (Nam));
6684 Set_Is_Overloaded (New_Nam, False);
6685 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6686 Set_Parent (New_Nam, Parent (Parent (Nam)));
6687 Analyze_Selected_Component (New_Nam);
6688 Get_First_Interp (Selector_Name (New_Nam), X, It);
6689 else
6690 Get_First_Interp (Nam, X, It);
6691 end if;
6693 -- If the number of actuals is 2, then remove interpretations involving
6694 -- a unary "+" operator as they might yield confusing errors downstream.
6696 if Num_Actuals = 2
6697 and then Nkind (Parent (Nam)) /= N_Selected_Component
6698 then
6699 Num_Interps := 0;
6701 while Present (It.Nam) loop
6702 if Ekind (It.Nam) = E_Operator
6703 and then Chars (It.Nam) = Name_Op_Add
6704 and then (No (First_Formal (It.Nam))
6705 or else No (Next_Formal (First_Formal (It.Nam))))
6706 then
6707 Remove_Interp (X);
6708 else
6709 Num_Interps := Num_Interps + 1;
6710 end if;
6712 Get_Next_Interp (X, It);
6713 end loop;
6715 if Num_Interps = 0 then
6716 Error_Msg_N ("!too many arguments in call to&", Nam);
6717 return;
6718 end if;
6720 Get_First_Interp (Nam, X, It);
6722 else
6723 Num_Interps := 2; -- at least
6724 end if;
6726 -- Analyze each candidate call again with full error reporting for each
6728 if Num_Interps > 1 then
6729 Error_Msg_N ("!no candidate interpretations match the actuals:", Nam);
6730 end if;
6732 Err_Mode := All_Errors_Mode;
6733 All_Errors_Mode := True;
6735 while Present (It.Nam) loop
6736 if Etype (It.Nam) = Standard_Void_Type then
6737 Void_Interp_Seen := True;
6738 end if;
6740 Analyze_One_Call (N, It.Nam, True, Success);
6741 Get_Next_Interp (X, It);
6742 end loop;
6744 if Nkind (N) = N_Function_Call then
6745 Get_First_Interp (Nam, X, It);
6747 if No (It.Typ)
6748 and then Ekind (Entity (Name (N))) = E_Function
6749 and then Present (Homonym (Entity (Name (N))))
6750 then
6751 -- A name may appear overloaded if it has a homonym, even if that
6752 -- homonym is non-overloadable, in which case the overload list is
6753 -- in fact empty. This specialized case deserves a special message
6754 -- if the homonym is a child package.
6756 declare
6757 Nam : constant Node_Id := Name (N);
6758 H : constant Entity_Id := Homonym (Entity (Nam));
6760 begin
6761 if Ekind (H) = E_Package and then Is_Child_Unit (H) then
6762 Error_Msg_Qual_Level := 2;
6763 Error_Msg_NE ("if an entity in package& is meant, ", Nam, H);
6764 Error_Msg_NE ("\use a fully qualified name", Nam, H);
6765 Error_Msg_Qual_Level := 0;
6766 end if;
6767 end;
6769 else
6770 while Present (It.Nam) loop
6771 if Ekind (It.Nam) in E_Function | E_Operator then
6772 return;
6773 else
6774 Get_Next_Interp (X, It);
6775 end if;
6776 end loop;
6778 -- If all interpretations are procedures, this deserves a more
6779 -- precise message. Ditto if this appears as the prefix of a
6780 -- selected component, which may be a lexical error.
6782 Error_Msg_N
6783 ("\context requires function call, found procedure name", Nam);
6785 if Nkind (Parent (N)) = N_Selected_Component
6786 and then N = Prefix (Parent (N))
6787 then
6788 Error_Msg_N -- CODEFIX
6789 ("\period should probably be semicolon", Parent (N));
6790 end if;
6791 end if;
6793 elsif Nkind (N) = N_Procedure_Call_Statement
6794 and then not Void_Interp_Seen
6795 then
6796 Error_Msg_N ("\function name found in procedure call", Nam);
6797 end if;
6799 All_Errors_Mode := Err_Mode;
6800 end Diagnose_Call;
6802 ---------------------------
6803 -- Find_Arithmetic_Types --
6804 ---------------------------
6806 procedure Find_Arithmetic_Types
6807 (L, R : Node_Id;
6808 Op_Id : Entity_Id;
6809 N : Node_Id)
6811 procedure Check_Right_Argument (T : Entity_Id);
6812 -- Check right operand of operator
6814 --------------------------
6815 -- Check_Right_Argument --
6816 --------------------------
6818 procedure Check_Right_Argument (T : Entity_Id) is
6819 I : Interp_Index;
6820 It : Interp;
6822 begin
6823 if not Is_Overloaded (R) then
6824 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6826 else
6827 Get_First_Interp (R, I, It);
6828 while Present (It.Typ) loop
6829 Check_Arithmetic_Pair (T, It.Typ, Op_Id, N);
6830 Get_Next_Interp (I, It);
6831 end loop;
6832 end if;
6833 end Check_Right_Argument;
6835 -- Local variables
6837 I : Interp_Index;
6838 It : Interp;
6840 -- Start of processing for Find_Arithmetic_Types
6842 begin
6843 if not Is_Overloaded (L) then
6844 Check_Right_Argument (Etype (L));
6846 else
6847 Get_First_Interp (L, I, It);
6848 while Present (It.Typ) loop
6849 Check_Right_Argument (It.Typ);
6850 Get_Next_Interp (I, It);
6851 end loop;
6852 end if;
6853 end Find_Arithmetic_Types;
6855 ------------------------
6856 -- Find_Boolean_Types --
6857 ------------------------
6859 procedure Find_Boolean_Types
6860 (L, R : Node_Id;
6861 Op_Id : Entity_Id;
6862 N : Node_Id)
6864 procedure Check_Boolean_Pair (T1, T2 : Entity_Id);
6865 -- Check operand pair of operator
6867 procedure Check_Right_Argument (T : Entity_Id);
6868 -- Check right operand of operator
6870 ------------------------
6871 -- Check_Boolean_Pair --
6872 ------------------------
6874 procedure Check_Boolean_Pair (T1, T2 : Entity_Id) is
6875 T : Entity_Id;
6877 begin
6878 if Valid_Boolean_Arg (T1)
6879 and then Valid_Boolean_Arg (T2)
6880 and then (Covers (T1 => T1, T2 => T2)
6881 or else Covers (T1 => T2, T2 => T1))
6882 then
6883 T := Specific_Type (T1, T2);
6885 if T = Universal_Integer then
6886 T := Any_Modular;
6887 end if;
6889 Add_One_Interp (N, Op_Id, T);
6890 end if;
6891 end Check_Boolean_Pair;
6893 --------------------------
6894 -- Check_Right_Argument --
6895 --------------------------
6897 procedure Check_Right_Argument (T : Entity_Id) is
6898 I : Interp_Index;
6899 It : Interp;
6901 begin
6902 -- Defend against previous error
6904 if Nkind (R) = N_Error then
6905 null;
6907 elsif not Is_Overloaded (R) then
6908 Check_Boolean_Pair (T, Etype (R));
6910 else
6911 Get_First_Interp (R, I, It);
6912 while Present (It.Typ) loop
6913 Check_Boolean_Pair (T, It.Typ);
6914 Get_Next_Interp (I, It);
6915 end loop;
6916 end if;
6917 end Check_Right_Argument;
6919 -- Local variables
6921 I : Interp_Index;
6922 It : Interp;
6924 -- Start of processing for Find_Boolean_Types
6926 begin
6927 if not Is_Overloaded (L) then
6928 Check_Right_Argument (Etype (L));
6930 else
6931 Get_First_Interp (L, I, It);
6932 while Present (It.Typ) loop
6933 Check_Right_Argument (It.Typ);
6934 Get_Next_Interp (I, It);
6935 end loop;
6936 end if;
6937 end Find_Boolean_Types;
6939 ------------------------------------
6940 -- Find_Comparison_Equality_Types --
6941 ------------------------------------
6943 -- The context of the operator plays no role in resolving the operands,
6944 -- so that if there is more than one interpretation of the operands that
6945 -- is compatible with the comparison or equality, then the operation is
6946 -- ambiguous, but this cannot be reported at this point because there is
6947 -- no guarantee that the operation will be resolved to this operator yet.
6949 procedure Find_Comparison_Equality_Types
6950 (L, R : Node_Id;
6951 Op_Id : Entity_Id;
6952 N : Node_Id)
6954 Op_Name : constant Name_Id := Chars (Op_Id);
6955 Op_Typ : Entity_Id renames Standard_Boolean;
6957 function Try_Left_Interp (T : Entity_Id) return Entity_Id;
6958 -- Try an interpretation of the left operand with type T. Return the
6959 -- type of the interpretation of the right operand making up a valid
6960 -- operand pair, or else Any_Type if the right operand is ambiguous,
6961 -- otherwise Empty if no such pair exists.
6963 function Is_Valid_Comparison_Type (T : Entity_Id) return Boolean;
6964 -- Return true if T is a valid comparison type
6966 function Is_Valid_Equality_Type
6967 (T : Entity_Id;
6968 Anon_Access : Boolean) return Boolean;
6969 -- Return true if T is a valid equality type
6971 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean;
6972 -- Return true if T1 and T2 constitute a valid pair of operand types for
6973 -- L and R respectively.
6975 ---------------------
6976 -- Try_Left_Interp --
6977 ---------------------
6979 function Try_Left_Interp (T : Entity_Id) return Entity_Id is
6980 I : Interp_Index;
6981 It : Interp;
6982 R_Typ : Entity_Id;
6983 Valid_I : Interp_Index;
6985 begin
6986 -- Defend against previous error
6988 if Nkind (R) = N_Error then
6989 null;
6991 -- Loop through the interpretations of the right operand
6993 elsif not Is_Overloaded (R) then
6994 if Is_Valid_Pair (T, Etype (R)) then
6995 return Etype (R);
6996 end if;
6998 else
6999 R_Typ := Empty;
7000 Valid_I := 0;
7002 Get_First_Interp (R, I, It);
7003 while Present (It.Typ) loop
7004 if Is_Valid_Pair (T, It.Typ) then
7005 -- If several interpretations are possible, disambiguate
7007 if Present (R_Typ)
7008 and then Base_Type (It.Typ) /= Base_Type (R_Typ)
7009 then
7010 It := Disambiguate (R, Valid_I, I, Any_Type);
7012 if It = No_Interp then
7013 R_Typ := Any_Type;
7014 exit;
7015 end if;
7017 else
7018 Valid_I := I;
7019 end if;
7021 R_Typ := It.Typ;
7022 end if;
7024 Get_Next_Interp (I, It);
7025 end loop;
7027 if Present (R_Typ) then
7028 return R_Typ;
7029 end if;
7030 end if;
7032 return Empty;
7033 end Try_Left_Interp;
7035 ------------------------------
7036 -- Is_Valid_Comparison_Type --
7037 ------------------------------
7039 function Is_Valid_Comparison_Type (T : Entity_Id) return Boolean is
7040 begin
7041 -- The operation must be performed in a context where the operators
7042 -- of the base type are visible.
7044 if Is_Visible_Operator (N, Base_Type (T)) then
7045 null;
7047 -- Save candidate type for subsequent error message, if any
7049 else
7050 if Valid_Comparison_Arg (T) then
7051 Candidate_Type := T;
7052 end if;
7054 return False;
7055 end if;
7057 -- Defer to the common implementation for the rest
7059 return Valid_Comparison_Arg (T);
7060 end Is_Valid_Comparison_Type;
7062 ----------------------------
7063 -- Is_Valid_Equality_Type --
7064 ----------------------------
7066 function Is_Valid_Equality_Type
7067 (T : Entity_Id;
7068 Anon_Access : Boolean) return Boolean
7070 begin
7071 -- The operation must be performed in a context where the operators
7072 -- of the base type are visible. Deal with special types used with
7073 -- access types before type resolution is done.
7075 if Ekind (T) = E_Access_Attribute_Type
7076 or else (Ekind (T) in E_Access_Subprogram_Type
7077 | E_Access_Protected_Subprogram_Type
7078 and then
7079 Ekind (Designated_Type (T)) /= E_Subprogram_Type)
7080 or else Is_Visible_Operator (N, Base_Type (T))
7081 then
7082 null;
7084 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
7085 -- anonymous access types in universal_access equality operators.
7087 elsif Anon_Access then
7088 if Ada_Version < Ada_2005 then
7089 return False;
7090 end if;
7092 -- Save candidate type for subsequent error message, if any
7094 else
7095 if Valid_Equality_Arg (T) then
7096 Candidate_Type := T;
7097 end if;
7099 return False;
7100 end if;
7102 -- For the use of a "/=" operator on a tagged type, several possible
7103 -- interpretations of equality need to be considered, we don't want
7104 -- the default inequality declared in Standard to be chosen, and the
7105 -- "/=" operator will be rewritten as a negation of "=" (see the end
7106 -- of Analyze_Comparison_Equality_Op). This ensures the rewriting
7107 -- occurs during analysis rather than being delayed until expansion.
7108 -- Note that, if the node is N_Op_Ne but Op_Id is Name_Op_Eq, then we
7109 -- still proceed with the interpretation, because this indicates
7110 -- the aforementioned rewriting case where the interpretation to be
7111 -- considered is actually that of the "=" operator.
7113 if Nkind (N) = N_Op_Ne
7114 and then Op_Name /= Name_Op_Eq
7115 and then Is_Tagged_Type (T)
7116 then
7117 return False;
7119 -- Defer to the common implementation for the rest
7121 else
7122 return Valid_Equality_Arg (T);
7123 end if;
7124 end Is_Valid_Equality_Type;
7126 -------------------
7127 -- Is_Valid_Pair --
7128 -------------------
7130 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean is
7131 begin
7132 if Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne then
7133 declare
7134 Anon_Access : constant Boolean :=
7135 Is_Anonymous_Access_Type (T1)
7136 or else Is_Anonymous_Access_Type (T2);
7137 -- RM 4.5.2(9.1/2): At least one of the operands of an equality
7138 -- operator for universal_access shall be of specific anonymous
7139 -- access type.
7141 begin
7142 if not Is_Valid_Equality_Type (T1, Anon_Access)
7143 or else not Is_Valid_Equality_Type (T2, Anon_Access)
7144 then
7145 return False;
7146 end if;
7147 end;
7149 else
7150 if not Is_Valid_Comparison_Type (T1)
7151 or else not Is_Valid_Comparison_Type (T2)
7152 then
7153 return False;
7154 end if;
7155 end if;
7157 return Covers (T1 => T1, T2 => T2)
7158 or else Covers (T1 => T2, T2 => T1)
7159 or else Is_User_Defined_Literal (L, T2)
7160 or else Is_User_Defined_Literal (R, T1);
7161 end Is_Valid_Pair;
7163 -- Local variables
7165 I : Interp_Index;
7166 It : Interp;
7167 L_Typ : Entity_Id;
7168 R_Typ : Entity_Id;
7169 T : Entity_Id;
7170 Valid_I : Interp_Index;
7172 -- Start of processing for Find_Comparison_Equality_Types
7174 begin
7175 -- Loop through the interpretations of the left operand
7177 if not Is_Overloaded (L) then
7178 T := Try_Left_Interp (Etype (L));
7180 if Present (T) then
7181 Set_Etype (R, T);
7182 Add_One_Interp (N, Op_Id, Op_Typ, Find_Unique_Type (L, R));
7183 end if;
7185 else
7186 L_Typ := Empty;
7187 R_Typ := Empty;
7188 Valid_I := 0;
7190 Get_First_Interp (L, I, It);
7191 while Present (It.Typ) loop
7192 T := Try_Left_Interp (It.Typ);
7194 if Present (T) then
7195 -- If several interpretations are possible, disambiguate
7197 if Present (L_Typ)
7198 and then Base_Type (It.Typ) /= Base_Type (L_Typ)
7199 then
7200 It := Disambiguate (L, Valid_I, I, Any_Type);
7202 if It = No_Interp then
7203 L_Typ := Any_Type;
7204 R_Typ := T;
7205 exit;
7206 end if;
7208 else
7209 Valid_I := I;
7210 end if;
7212 L_Typ := It.Typ;
7213 R_Typ := T;
7214 end if;
7216 Get_Next_Interp (I, It);
7217 end loop;
7219 if Present (L_Typ) then
7220 Set_Etype (L, L_Typ);
7221 Set_Etype (R, R_Typ);
7222 Add_One_Interp (N, Op_Id, Op_Typ, Find_Unique_Type (L, R));
7223 end if;
7224 end if;
7225 end Find_Comparison_Equality_Types;
7227 ------------------------------
7228 -- Find_Concatenation_Types --
7229 ------------------------------
7231 procedure Find_Concatenation_Types
7232 (L, R : Node_Id;
7233 Op_Id : Entity_Id;
7234 N : Node_Id)
7236 Is_String : constant Boolean := Nkind (L) = N_String_Literal
7237 or else
7238 Nkind (R) = N_String_Literal;
7239 Op_Type : constant Entity_Id := Etype (Op_Id);
7241 begin
7242 if Is_Array_Type (Op_Type)
7244 -- Small but very effective optimization: if at least one operand is a
7245 -- string literal, then the type of the operator must be either array
7246 -- of characters or array of strings.
7248 and then (not Is_String
7249 or else
7250 Is_Character_Type (Component_Type (Op_Type))
7251 or else
7252 Is_String_Type (Component_Type (Op_Type)))
7254 and then not Is_Limited_Type (Op_Type)
7256 and then (Has_Compatible_Type (L, Op_Type)
7257 or else
7258 Has_Compatible_Type (L, Component_Type (Op_Type)))
7260 and then (Has_Compatible_Type (R, Op_Type)
7261 or else
7262 Has_Compatible_Type (R, Component_Type (Op_Type)))
7263 then
7264 Add_One_Interp (N, Op_Id, Op_Type);
7265 end if;
7266 end Find_Concatenation_Types;
7268 -------------------------
7269 -- Find_Negation_Types --
7270 -------------------------
7272 procedure Find_Negation_Types
7273 (R : Node_Id;
7274 Op_Id : Entity_Id;
7275 N : Node_Id)
7277 Index : Interp_Index;
7278 It : Interp;
7280 begin
7281 if not Is_Overloaded (R) then
7282 if Etype (R) = Universal_Integer then
7283 Add_One_Interp (N, Op_Id, Any_Modular);
7284 elsif Valid_Boolean_Arg (Etype (R)) then
7285 Add_One_Interp (N, Op_Id, Etype (R));
7286 end if;
7288 else
7289 Get_First_Interp (R, Index, It);
7290 while Present (It.Typ) loop
7291 if Valid_Boolean_Arg (It.Typ) then
7292 Add_One_Interp (N, Op_Id, It.Typ);
7293 end if;
7295 Get_Next_Interp (Index, It);
7296 end loop;
7297 end if;
7298 end Find_Negation_Types;
7300 ------------------------------
7301 -- Find_Primitive_Operation --
7302 ------------------------------
7304 function Find_Primitive_Operation (N : Node_Id) return Boolean is
7305 Obj : constant Node_Id := Prefix (N);
7306 Op : constant Node_Id := Selector_Name (N);
7308 Prim : Elmt_Id;
7309 Prims : Elist_Id;
7310 Typ : Entity_Id;
7312 begin
7313 Set_Etype (Op, Any_Type);
7315 if Is_Access_Type (Etype (Obj)) then
7316 Typ := Designated_Type (Etype (Obj));
7317 else
7318 Typ := Etype (Obj);
7319 end if;
7321 if Is_Class_Wide_Type (Typ) then
7322 Typ := Root_Type (Typ);
7323 end if;
7325 Prims := Primitive_Operations (Typ);
7327 Prim := First_Elmt (Prims);
7328 while Present (Prim) loop
7329 if Chars (Node (Prim)) = Chars (Op) then
7330 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
7331 Set_Etype (N, Etype (Node (Prim)));
7332 end if;
7334 Next_Elmt (Prim);
7335 end loop;
7337 -- Now look for class-wide operations of the type or any of its
7338 -- ancestors by iterating over the homonyms of the selector.
7340 declare
7341 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
7342 Hom : Entity_Id;
7344 begin
7345 Hom := Current_Entity (Op);
7346 while Present (Hom) loop
7347 if (Ekind (Hom) = E_Procedure
7348 or else
7349 Ekind (Hom) = E_Function)
7350 and then Scope (Hom) = Scope (Typ)
7351 and then Present (First_Formal (Hom))
7352 and then
7353 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7354 or else
7355 (Is_Access_Type (Etype (First_Formal (Hom)))
7356 and then
7357 Ekind (Etype (First_Formal (Hom))) =
7358 E_Anonymous_Access_Type
7359 and then
7360 Base_Type
7361 (Designated_Type (Etype (First_Formal (Hom)))) =
7362 Cls_Type))
7363 then
7364 Add_One_Interp (Op, Hom, Etype (Hom));
7365 Set_Etype (N, Etype (Hom));
7366 end if;
7368 Hom := Homonym (Hom);
7369 end loop;
7370 end;
7372 return Etype (Op) /= Any_Type;
7373 end Find_Primitive_Operation;
7375 ----------------------
7376 -- Find_Unary_Types --
7377 ----------------------
7379 procedure Find_Unary_Types
7380 (R : Node_Id;
7381 Op_Id : Entity_Id;
7382 N : Node_Id)
7384 Index : Interp_Index;
7385 It : Interp;
7387 begin
7388 if not Is_Overloaded (R) then
7389 if Is_Numeric_Type (Etype (R)) then
7391 -- In an instance a generic actual may be a numeric type even if
7392 -- the formal in the generic unit was not. In that case, the
7393 -- predefined operator was not a possible interpretation in the
7394 -- generic, and cannot be one in the instance, unless the operator
7395 -- is an actual of an instance.
7397 if In_Instance
7398 and then
7399 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
7400 then
7401 null;
7402 else
7403 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
7404 end if;
7405 end if;
7407 else
7408 Get_First_Interp (R, Index, It);
7409 while Present (It.Typ) loop
7410 if Is_Numeric_Type (It.Typ) then
7411 if In_Instance
7412 and then
7413 not Is_Numeric_Type
7414 (Corresponding_Generic_Type (Etype (It.Typ)))
7415 then
7416 null;
7418 else
7419 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
7420 end if;
7421 end if;
7423 Get_Next_Interp (Index, It);
7424 end loop;
7425 end if;
7426 end Find_Unary_Types;
7428 ------------------
7429 -- Junk_Operand --
7430 ------------------
7432 function Junk_Operand (N : Node_Id) return Boolean is
7433 Enode : Node_Id;
7435 begin
7436 if Error_Posted (N) then
7437 return False;
7438 end if;
7440 -- Get entity to be tested
7442 if Is_Entity_Name (N)
7443 and then Present (Entity (N))
7444 then
7445 Enode := N;
7447 -- An odd case, a procedure name gets converted to a very peculiar
7448 -- function call, and here is where we detect this happening.
7450 elsif Nkind (N) = N_Function_Call
7451 and then Is_Entity_Name (Name (N))
7452 and then Present (Entity (Name (N)))
7453 then
7454 Enode := Name (N);
7456 -- Another odd case, there are at least some cases of selected
7457 -- components where the selected component is not marked as having
7458 -- an entity, even though the selector does have an entity
7460 elsif Nkind (N) = N_Selected_Component
7461 and then Present (Entity (Selector_Name (N)))
7462 then
7463 Enode := Selector_Name (N);
7465 else
7466 return False;
7467 end if;
7469 -- Now test the entity we got to see if it is a bad case
7471 case Ekind (Entity (Enode)) is
7472 when E_Package =>
7473 Error_Msg_N
7474 ("package name cannot be used as operand", Enode);
7476 when Generic_Unit_Kind =>
7477 Error_Msg_N
7478 ("generic unit name cannot be used as operand", Enode);
7480 when Type_Kind =>
7481 Error_Msg_N
7482 ("subtype name cannot be used as operand", Enode);
7484 when Entry_Kind =>
7485 Error_Msg_N
7486 ("entry name cannot be used as operand", Enode);
7488 when E_Procedure =>
7489 Error_Msg_N
7490 ("procedure name cannot be used as operand", Enode);
7492 when E_Exception =>
7493 Error_Msg_N
7494 ("exception name cannot be used as operand", Enode);
7496 when E_Block
7497 | E_Label
7498 | E_Loop
7500 Error_Msg_N
7501 ("label name cannot be used as operand", Enode);
7503 when others =>
7504 return False;
7505 end case;
7507 return True;
7508 end Junk_Operand;
7510 --------------------
7511 -- Operator_Check --
7512 --------------------
7514 procedure Operator_Check (N : Node_Id) is
7515 begin
7516 Remove_Abstract_Operations (N);
7518 -- Test for case of no interpretation found for operator
7520 if Etype (N) = Any_Type then
7521 declare
7522 L : Node_Id;
7523 R : Node_Id;
7524 Op_Id : Entity_Id := Empty;
7526 begin
7527 R := Right_Opnd (N);
7529 if Nkind (N) in N_Binary_Op then
7530 L := Left_Opnd (N);
7531 else
7532 L := Empty;
7533 end if;
7535 -- If either operand has no type, then don't complain further,
7536 -- since this simply means that we have a propagated error.
7538 if R = Error
7539 or else Etype (R) = Any_Type
7540 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
7541 then
7542 -- For the rather unusual case where one of the operands is
7543 -- a Raise_Expression, whose initial type is Any_Type, use
7544 -- the type of the other operand.
7546 if Nkind (L) = N_Raise_Expression then
7547 Set_Etype (L, Etype (R));
7548 Set_Etype (N, Etype (R));
7550 elsif Nkind (R) = N_Raise_Expression then
7551 Set_Etype (R, Etype (L));
7552 Set_Etype (N, Etype (L));
7553 end if;
7555 return;
7557 -- We explicitly check for the case of concatenation of component
7558 -- with component to avoid reporting spurious matching array types
7559 -- that might happen to be lurking in distant packages (such as
7560 -- run-time packages). This also prevents inconsistencies in the
7561 -- messages for certain ACVC B tests, which can vary depending on
7562 -- types declared in run-time interfaces. Another improvement when
7563 -- aggregates are present is to look for a well-typed operand.
7565 elsif Present (Candidate_Type)
7566 and then (Nkind (N) /= N_Op_Concat
7567 or else Is_Array_Type (Etype (L))
7568 or else Is_Array_Type (Etype (R)))
7569 then
7570 if Nkind (N) = N_Op_Concat then
7571 if Etype (L) /= Any_Composite
7572 and then Is_Array_Type (Etype (L))
7573 then
7574 Candidate_Type := Etype (L);
7576 elsif Etype (R) /= Any_Composite
7577 and then Is_Array_Type (Etype (R))
7578 then
7579 Candidate_Type := Etype (R);
7580 end if;
7581 end if;
7583 Error_Msg_NE -- CODEFIX
7584 ("operator for} is not directly visible!",
7585 N, First_Subtype (Candidate_Type));
7587 declare
7588 U : constant Node_Id :=
7589 Cunit (Get_Source_Unit (Candidate_Type));
7590 begin
7591 if Unit_Is_Visible (U) then
7592 Error_Msg_N -- CODEFIX
7593 ("use clause would make operation legal!", N);
7594 else
7595 Error_Msg_NE -- CODEFIX
7596 ("add with_clause and use_clause for&!",
7597 N, Defining_Entity (Unit (U)));
7598 end if;
7599 end;
7600 return;
7602 -- If either operand is a junk operand (e.g. package name), then
7603 -- post appropriate error messages, but do not complain further.
7605 -- Note that the use of OR in this test instead of OR ELSE is
7606 -- quite deliberate, we may as well check both operands in the
7607 -- binary operator case.
7609 elsif Junk_Operand (R)
7610 or -- really mean OR here and not OR ELSE, see above
7611 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
7612 then
7613 return;
7615 elsif Present (Entity (N))
7616 and then Has_Possible_Literal_Aspects (N)
7617 then
7618 return;
7620 -- If we have a logical operator, one of whose operands is
7621 -- Boolean, then we know that the other operand cannot resolve to
7622 -- Boolean (since we got no interpretations), but in that case we
7623 -- pretty much know that the other operand should be Boolean, so
7624 -- resolve it that way (generating an error).
7626 elsif Nkind (N) in N_Op_And | N_Op_Or | N_Op_Xor then
7627 if Etype (L) = Standard_Boolean then
7628 Resolve (R, Standard_Boolean);
7629 return;
7630 elsif Etype (R) = Standard_Boolean then
7631 Resolve (L, Standard_Boolean);
7632 return;
7633 end if;
7635 -- For an arithmetic operator or comparison operator, if one
7636 -- of the operands is numeric, then we know the other operand
7637 -- is not the same numeric type. If it is a non-numeric type,
7638 -- then probably it is intended to match the other operand.
7640 elsif Nkind (N) in N_Op_Add
7641 | N_Op_Divide
7642 | N_Op_Ge
7643 | N_Op_Gt
7644 | N_Op_Le
7645 | N_Op_Lt
7646 | N_Op_Mod
7647 | N_Op_Multiply
7648 | N_Op_Rem
7649 | N_Op_Subtract
7650 then
7651 -- If Allow_Integer_Address is active, check whether the
7652 -- operation becomes legal after converting an operand.
7654 if Is_Numeric_Type (Etype (L))
7655 and then not Is_Numeric_Type (Etype (R))
7656 then
7657 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7658 Rewrite (L,
7659 Unchecked_Convert_To (
7660 Standard_Address, Relocate_Node (L)));
7661 Rewrite (R,
7662 Unchecked_Convert_To (
7663 Standard_Address, Relocate_Node (R)));
7665 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7666 Analyze_Comparison_Equality_Op (N);
7667 else
7668 Analyze_Arithmetic_Op (N);
7669 end if;
7670 else
7671 Resolve (R, Etype (L));
7672 end if;
7674 return;
7676 elsif Is_Numeric_Type (Etype (R))
7677 and then not Is_Numeric_Type (Etype (L))
7678 then
7679 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
7680 Rewrite (L,
7681 Unchecked_Convert_To (
7682 Standard_Address, Relocate_Node (L)));
7683 Rewrite (R,
7684 Unchecked_Convert_To (
7685 Standard_Address, Relocate_Node (R)));
7687 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7688 Analyze_Comparison_Equality_Op (N);
7689 else
7690 Analyze_Arithmetic_Op (N);
7691 end if;
7693 return;
7695 else
7696 Resolve (L, Etype (R));
7697 end if;
7699 return;
7701 elsif Allow_Integer_Address
7702 and then Is_Descendant_Of_Address (Etype (L))
7703 and then Is_Descendant_Of_Address (Etype (R))
7704 and then not Error_Posted (N)
7705 then
7706 declare
7707 Addr_Type : constant Entity_Id := Etype (L);
7709 begin
7710 Rewrite (L,
7711 Unchecked_Convert_To (
7712 Standard_Address, Relocate_Node (L)));
7713 Rewrite (R,
7714 Unchecked_Convert_To (
7715 Standard_Address, Relocate_Node (R)));
7717 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7718 Analyze_Comparison_Equality_Op (N);
7719 else
7720 Analyze_Arithmetic_Op (N);
7721 end if;
7723 -- If this is an operand in an enclosing arithmetic
7724 -- operation, Convert the result as an address so that
7725 -- arithmetic folding of address can continue.
7727 if Nkind (Parent (N)) in N_Op then
7728 Rewrite (N,
7729 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7730 end if;
7732 return;
7733 end;
7735 -- Under relaxed RM semantics silently replace occurrences of
7736 -- null by System.Address_Null.
7738 elsif Null_To_Null_Address_Convert_OK (N) then
7739 Replace_Null_By_Null_Address (N);
7741 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7742 Analyze_Comparison_Equality_Op (N);
7743 else
7744 Analyze_Arithmetic_Op (N);
7745 end if;
7747 return;
7748 end if;
7750 -- Comparisons on A'Access are common enough to deserve a
7751 -- special message.
7753 elsif Nkind (N) in N_Op_Eq | N_Op_Ne
7754 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7755 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7756 then
7757 Error_Msg_N
7758 ("two access attributes cannot be compared directly", N);
7759 Error_Msg_N
7760 ("\use qualified expression for one of the operands",
7762 return;
7764 -- Another one for C programmers
7766 elsif Nkind (N) = N_Op_Concat
7767 and then Valid_Boolean_Arg (Etype (L))
7768 and then Valid_Boolean_Arg (Etype (R))
7769 then
7770 Error_Msg_N ("invalid operands for concatenation", N);
7771 Error_Msg_N -- CODEFIX
7772 ("\maybe AND was meant", N);
7773 return;
7775 -- A special case for comparison of access parameter with null
7777 elsif Nkind (N) = N_Op_Eq
7778 and then Is_Entity_Name (L)
7779 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7780 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7781 N_Access_Definition
7782 and then Nkind (R) = N_Null
7783 then
7784 Error_Msg_N ("access parameter is not allowed to be null", L);
7785 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7786 return;
7788 -- Another special case for exponentiation, where the right
7789 -- operand must be Natural, independently of the base.
7791 elsif Nkind (N) = N_Op_Expon
7792 and then Is_Numeric_Type (Etype (L))
7793 and then not Is_Overloaded (R)
7794 and then
7795 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7796 and then Base_Type (Etype (R)) /= Universal_Integer
7797 then
7798 if Ada_Version >= Ada_2012
7799 and then Has_Dimension_System (Etype (L))
7800 then
7801 Error_Msg_NE
7802 ("exponent for dimensioned type must be a rational" &
7803 ", found}", R, Etype (R));
7804 else
7805 Error_Msg_NE
7806 ("exponent must be of type Natural, found}", R, Etype (R));
7807 end if;
7809 return;
7811 elsif Nkind (N) in N_Op_Eq | N_Op_Ne then
7812 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7813 Rewrite (L,
7814 Unchecked_Convert_To (
7815 Standard_Address, Relocate_Node (L)));
7816 Rewrite (R,
7817 Unchecked_Convert_To (
7818 Standard_Address, Relocate_Node (R)));
7819 Analyze_Comparison_Equality_Op (N);
7820 return;
7822 -- Under relaxed RM semantics silently replace occurrences of
7823 -- null by System.Address_Null.
7825 elsif Null_To_Null_Address_Convert_OK (N) then
7826 Replace_Null_By_Null_Address (N);
7827 Analyze_Comparison_Equality_Op (N);
7828 return;
7829 end if;
7830 end if;
7832 -- If we fall through then just give general message. Note that in
7833 -- the following messages, if the operand is overloaded we choose
7834 -- an arbitrary type to complain about, but that is probably more
7835 -- useful than not giving a type at all.
7837 if Nkind (N) in N_Unary_Op then
7838 Error_Msg_Node_2 := Etype (R);
7839 Error_Msg_N ("operator& not defined for}", N);
7840 return;
7842 else
7843 if Nkind (N) in N_Binary_Op then
7844 if not Is_Overloaded (L)
7845 and then not Is_Overloaded (R)
7846 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7847 then
7848 Error_Msg_Node_2 := First_Subtype (Etype (R));
7849 Error_Msg_N ("there is no applicable operator& for}", N);
7851 else
7852 -- Another attempt to find a fix: one of the candidate
7853 -- interpretations may not be use-visible. This has
7854 -- already been checked for predefined operators, so
7855 -- we examine only user-defined functions.
7857 Op_Id := Get_Name_Entity_Id (Chars (N));
7859 while Present (Op_Id) loop
7860 if Ekind (Op_Id) /= E_Operator
7861 and then Is_Overloadable (Op_Id)
7862 then
7863 if not Is_Immediately_Visible (Op_Id)
7864 and then not In_Use (Scope (Op_Id))
7865 and then not Is_Abstract_Subprogram (Op_Id)
7866 and then not Is_Hidden (Op_Id)
7867 and then Ekind (Scope (Op_Id)) = E_Package
7868 and then
7869 Has_Compatible_Type
7870 (L, Etype (First_Formal (Op_Id)))
7871 and then Present
7872 (Next_Formal (First_Formal (Op_Id)))
7873 and then
7874 Has_Compatible_Type
7876 Etype (Next_Formal (First_Formal (Op_Id))))
7877 then
7878 Error_Msg_N
7879 ("no legal interpretation for operator&", N);
7880 Error_Msg_NE
7881 ("\use clause on& would make operation legal",
7882 N, Scope (Op_Id));
7883 exit;
7884 end if;
7885 end if;
7887 Op_Id := Homonym (Op_Id);
7888 end loop;
7890 if No (Op_Id) then
7891 Error_Msg_N ("invalid operand types for operator&", N);
7893 if Nkind (N) /= N_Op_Concat then
7894 Error_Msg_NE ("\left operand has}!", N, Etype (L));
7895 Error_Msg_NE ("\right operand has}!", N, Etype (R));
7897 -- For multiplication and division operators with
7898 -- a fixed-point operand and an integer operand,
7899 -- indicate that the integer operand should be of
7900 -- type Integer.
7902 if Nkind (N) in N_Op_Multiply | N_Op_Divide
7903 and then Is_Fixed_Point_Type (Etype (L))
7904 and then Is_Integer_Type (Etype (R))
7905 then
7906 Error_Msg_N
7907 ("\convert right operand to `Integer`", N);
7909 elsif Nkind (N) = N_Op_Multiply
7910 and then Is_Fixed_Point_Type (Etype (R))
7911 and then Is_Integer_Type (Etype (L))
7912 then
7913 Error_Msg_N
7914 ("\convert left operand to `Integer`", N);
7915 end if;
7917 -- For concatenation operators it is more difficult to
7918 -- determine which is the wrong operand. It is worth
7919 -- flagging explicitly an access type, for those who
7920 -- might think that a dereference happens here.
7922 elsif Is_Access_Type (Etype (L)) then
7923 Error_Msg_N ("\left operand is access type", N);
7925 elsif Is_Access_Type (Etype (R)) then
7926 Error_Msg_N ("\right operand is access type", N);
7927 end if;
7928 end if;
7929 end if;
7930 end if;
7931 end if;
7932 end;
7933 end if;
7934 end Operator_Check;
7936 ----------------------------------
7937 -- Has_Possible_Literal_Aspects --
7938 ----------------------------------
7940 function Has_Possible_Literal_Aspects (N : Node_Id) return Boolean is
7941 R : constant Node_Id := Right_Opnd (N);
7942 L : Node_Id := Empty;
7944 procedure Check_Literal_Opnd (Opnd : Node_Id);
7945 -- If an operand is a literal to which an aspect may apply,
7946 -- add the corresponding type to operator node.
7948 ------------------------
7949 -- Check_Literal_Opnd --
7950 ------------------------
7952 procedure Check_Literal_Opnd (Opnd : Node_Id) is
7953 begin
7954 if Nkind (Opnd) in N_Numeric_Or_String_Literal
7955 or else (Is_Entity_Name (Opnd)
7956 and then Present (Entity (Opnd))
7957 and then Is_Named_Number (Entity (Opnd)))
7958 then
7959 Add_One_Interp (N, Etype (Opnd), Etype (Opnd));
7960 end if;
7961 end Check_Literal_Opnd;
7963 -- Start of processing for Has_Possible_Literal_Aspects
7965 begin
7966 if Ada_Version < Ada_2022 then
7967 return False;
7968 end if;
7970 if Nkind (N) in N_Binary_Op then
7971 L := Left_Opnd (N);
7972 else
7973 L := Empty;
7974 end if;
7975 Check_Literal_Opnd (R);
7977 -- Check left operand only if right one did not provide a
7978 -- possible interpretation. Note that literal types are not
7979 -- overloadable, in the sense that there is no overloadable
7980 -- entity name whose several interpretations can be used to
7981 -- indicate possible resulting types, so there is no way to
7982 -- provide more than one interpretation to the operator node.
7983 -- The choice of one operand over the other is arbitrary at
7984 -- this point, and may lead to spurious resolution when both
7985 -- operands are literals of different kinds, but the second
7986 -- pass of resolution will examine anew both operands to
7987 -- determine whether a user-defined literal may apply to
7988 -- either or both.
7990 if Present (L)
7991 and then Etype (N) = Any_Type
7992 then
7993 Check_Literal_Opnd (L);
7994 end if;
7996 return Etype (N) /= Any_Type;
7997 end Has_Possible_Literal_Aspects;
7999 -----------------------------------------------
8000 -- Nondispatching_Call_To_Abstract_Operation --
8001 -----------------------------------------------
8003 procedure Nondispatching_Call_To_Abstract_Operation
8004 (N : Node_Id;
8005 Abstract_Op : Entity_Id)
8007 Typ : constant Entity_Id := Etype (N);
8009 begin
8010 -- In an instance body, this is a runtime check, but one we know will
8011 -- fail, so give an appropriate warning. As usual this kind of warning
8012 -- is an error in SPARK mode.
8014 Error_Msg_Sloc := Sloc (Abstract_Op);
8016 if In_Instance_Body and then SPARK_Mode /= On then
8017 Error_Msg_NE
8018 ("??cannot call abstract operation& declared#",
8019 N, Abstract_Op);
8020 Error_Msg_N ("\Program_Error [??", N);
8021 Rewrite (N,
8022 Make_Raise_Program_Error (Sloc (N),
8023 Reason => PE_Explicit_Raise));
8024 Analyze (N);
8025 Set_Etype (N, Typ);
8027 else
8028 Error_Msg_NE
8029 ("cannot call abstract operation& declared#",
8030 N, Abstract_Op);
8031 Set_Etype (N, Any_Type);
8032 end if;
8033 end Nondispatching_Call_To_Abstract_Operation;
8035 ----------------------------------------------
8036 -- Possible_Type_For_Conditional_Expression --
8037 ----------------------------------------------
8039 function Possible_Type_For_Conditional_Expression
8040 (T1, T2 : Entity_Id) return Entity_Id
8042 function Is_Access_Protected_Subprogram_Attribute
8043 (T : Entity_Id) return Boolean;
8044 -- Return true if T is the type of an access-to-protected-subprogram
8045 -- attribute.
8047 function Is_Access_Subprogram_Attribute (T : Entity_Id) return Boolean;
8048 -- Return true if T is the type of an access-to-subprogram attribute
8050 ----------------------------------------------
8051 -- Is_Access_Protected_Subprogram_Attribute --
8052 ----------------------------------------------
8054 function Is_Access_Protected_Subprogram_Attribute
8055 (T : Entity_Id) return Boolean
8057 begin
8058 return Ekind (T) = E_Access_Protected_Subprogram_Type
8059 and then Ekind (Designated_Type (T)) /= E_Subprogram_Type;
8060 end Is_Access_Protected_Subprogram_Attribute;
8062 ------------------------------------
8063 -- Is_Access_Subprogram_Attribute --
8064 ------------------------------------
8066 function Is_Access_Subprogram_Attribute (T : Entity_Id) return Boolean is
8067 begin
8068 return Ekind (T) = E_Access_Subprogram_Type
8069 and then Ekind (Designated_Type (T)) /= E_Subprogram_Type;
8070 end Is_Access_Subprogram_Attribute;
8072 -- Start of processing for Possible_Type_For_Conditional_Expression
8074 begin
8075 -- If both types are those of similar access attributes or allocators,
8076 -- pick one of them, for example the first.
8078 if Ekind (T1) in E_Access_Attribute_Type | E_Allocator_Type
8079 and then Ekind (T2) in E_Access_Attribute_Type | E_Allocator_Type
8080 then
8081 return T1;
8083 elsif Is_Access_Subprogram_Attribute (T1)
8084 and then Is_Access_Subprogram_Attribute (T2)
8085 and then
8086 Subtype_Conformant (Designated_Type (T1), Designated_Type (T2))
8087 then
8088 return T1;
8090 elsif Is_Access_Protected_Subprogram_Attribute (T1)
8091 and then Is_Access_Protected_Subprogram_Attribute (T2)
8092 and then
8093 Subtype_Conformant (Designated_Type (T1), Designated_Type (T2))
8094 then
8095 return T1;
8097 -- The other case to be considered is a pair of tagged types
8099 elsif Is_Tagged_Type (T1) and then Is_Tagged_Type (T2) then
8100 -- Covers performs the same checks when T1 or T2 are a CW type, so
8101 -- we don't need to do them again here.
8103 if not Is_Class_Wide_Type (T1) and then Is_Ancestor (T1, T2) then
8104 return T1;
8106 elsif not Is_Class_Wide_Type (T2) and then Is_Ancestor (T2, T1) then
8107 return T2;
8109 -- Neither type is an ancestor of the other, but they may have one in
8110 -- common, so we pick the first type as above. We could perform here
8111 -- the computation of the nearest common ancestors of T1 and T2, but
8112 -- this would require a significant amount of work and the practical
8113 -- benefit would very likely be negligible.
8115 else
8116 return T1;
8117 end if;
8119 -- Otherwise no type is possible
8121 else
8122 return Empty;
8123 end if;
8124 end Possible_Type_For_Conditional_Expression;
8126 --------------------------------
8127 -- Remove_Abstract_Operations --
8128 --------------------------------
8130 procedure Remove_Abstract_Operations (N : Node_Id) is
8131 Abstract_Op : Entity_Id := Empty;
8132 Address_Descendant : Boolean := False;
8133 I : Interp_Index;
8134 It : Interp;
8136 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
8137 -- activate this if either extensions are enabled, or if the abstract
8138 -- operation in question comes from a predefined file. This latter test
8139 -- allows us to use abstract to make operations invisible to users. In
8140 -- particular, if type Address is non-private and abstract subprograms
8141 -- are used to hide its operators, they will be truly hidden.
8143 type Operand_Position is (First_Op, Second_Op);
8144 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
8146 procedure Remove_Address_Interpretations (Op : Operand_Position);
8147 -- Ambiguities may arise when the operands are literal and the address
8148 -- operations in s-auxdec are visible. In that case, remove the
8149 -- interpretation of a literal as Address, to retain the semantics
8150 -- of Address as a private type.
8152 ------------------------------------
8153 -- Remove_Address_Interpretations --
8154 ------------------------------------
8156 procedure Remove_Address_Interpretations (Op : Operand_Position) is
8157 Formal : Entity_Id;
8159 begin
8160 if Is_Overloaded (N) then
8161 Get_First_Interp (N, I, It);
8162 while Present (It.Nam) loop
8163 Formal := First_Entity (It.Nam);
8165 if Op = Second_Op then
8166 Next_Entity (Formal);
8167 end if;
8169 if Is_Descendant_Of_Address (Etype (Formal)) then
8170 Address_Descendant := True;
8171 Remove_Interp (I);
8172 end if;
8174 Get_Next_Interp (I, It);
8175 end loop;
8176 end if;
8177 end Remove_Address_Interpretations;
8179 -- Start of processing for Remove_Abstract_Operations
8181 begin
8182 if Is_Overloaded (N) then
8183 if Debug_Flag_V then
8184 Write_Line ("Remove_Abstract_Operations: ");
8185 Write_Overloads (N);
8186 end if;
8188 Get_First_Interp (N, I, It);
8190 while Present (It.Nam) loop
8191 if Is_Overloadable (It.Nam)
8192 and then Is_Abstract_Subprogram (It.Nam)
8193 and then not Is_Dispatching_Operation (It.Nam)
8194 then
8195 Abstract_Op := It.Nam;
8197 if Is_Descendant_Of_Address (It.Typ) then
8198 Address_Descendant := True;
8199 Remove_Interp (I);
8200 exit;
8202 -- In Ada 2005, this operation does not participate in overload
8203 -- resolution. If the operation is defined in a predefined
8204 -- unit, it is one of the operations declared abstract in some
8205 -- variants of System, and it must be removed as well.
8207 elsif Ada_Version >= Ada_2005
8208 or else In_Predefined_Unit (It.Nam)
8209 then
8210 Remove_Interp (I);
8211 exit;
8212 end if;
8213 end if;
8215 Get_Next_Interp (I, It);
8216 end loop;
8218 if No (Abstract_Op) then
8220 -- If some interpretation yields an integer type, it is still
8221 -- possible that there are address interpretations. Remove them
8222 -- if one operand is a literal, to avoid spurious ambiguities
8223 -- on systems where Address is a visible integer type.
8225 if Is_Overloaded (N)
8226 and then Nkind (N) in N_Op
8227 and then Is_Integer_Type (Etype (N))
8228 then
8229 if Nkind (N) in N_Binary_Op then
8230 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
8231 Remove_Address_Interpretations (Second_Op);
8233 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
8234 Remove_Address_Interpretations (First_Op);
8235 end if;
8236 end if;
8237 end if;
8239 elsif Nkind (N) in N_Op then
8241 -- Remove interpretations that treat literals as addresses. This
8242 -- is never appropriate, even when Address is defined as a visible
8243 -- Integer type. The reason is that we would really prefer Address
8244 -- to behave as a private type, even in this case. If Address is a
8245 -- visible integer type, we get lots of overload ambiguities.
8247 if Nkind (N) in N_Binary_Op then
8248 declare
8249 U1 : constant Boolean :=
8250 Present (Universal_Interpretation (Right_Opnd (N)));
8251 U2 : constant Boolean :=
8252 Present (Universal_Interpretation (Left_Opnd (N)));
8254 begin
8255 if U1 then
8256 Remove_Address_Interpretations (Second_Op);
8257 end if;
8259 if U2 then
8260 Remove_Address_Interpretations (First_Op);
8261 end if;
8263 if not (U1 and U2) then
8265 -- Remove corresponding predefined operator, which is
8266 -- always added to the overload set.
8268 Get_First_Interp (N, I, It);
8269 while Present (It.Nam) loop
8270 if Scope (It.Nam) = Standard_Standard
8271 and then Base_Type (It.Typ) =
8272 Base_Type (Etype (Abstract_Op))
8273 then
8274 Remove_Interp (I);
8275 end if;
8277 Get_Next_Interp (I, It);
8278 end loop;
8280 elsif Is_Overloaded (N)
8281 and then Present (Univ_Type)
8282 then
8283 -- If both operands have a universal interpretation,
8284 -- it is still necessary to remove interpretations that
8285 -- yield Address. Any remaining ambiguities will be
8286 -- removed in Disambiguate.
8288 Get_First_Interp (N, I, It);
8289 while Present (It.Nam) loop
8290 if Is_Descendant_Of_Address (It.Typ) then
8291 Remove_Interp (I);
8293 elsif not Is_Type (It.Nam) then
8294 Set_Entity (N, It.Nam);
8295 end if;
8297 Get_Next_Interp (I, It);
8298 end loop;
8299 end if;
8300 end;
8301 end if;
8303 elsif Nkind (N) = N_Function_Call
8304 and then
8305 (Nkind (Name (N)) = N_Operator_Symbol
8306 or else
8307 (Nkind (Name (N)) = N_Expanded_Name
8308 and then
8309 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
8310 then
8312 declare
8313 Arg1 : constant Node_Id := First (Parameter_Associations (N));
8314 U1 : constant Boolean :=
8315 Present (Universal_Interpretation (Arg1));
8316 U2 : constant Boolean :=
8317 Present (Next (Arg1)) and then
8318 Present (Universal_Interpretation (Next (Arg1)));
8320 begin
8321 if U1 then
8322 Remove_Address_Interpretations (First_Op);
8323 end if;
8325 if U2 then
8326 Remove_Address_Interpretations (Second_Op);
8327 end if;
8329 if not (U1 and U2) then
8330 Get_First_Interp (N, I, It);
8331 while Present (It.Nam) loop
8332 if Scope (It.Nam) = Standard_Standard
8333 and then It.Typ = Base_Type (Etype (Abstract_Op))
8334 then
8335 Remove_Interp (I);
8336 end if;
8338 Get_Next_Interp (I, It);
8339 end loop;
8340 end if;
8341 end;
8342 end if;
8344 -- If the removal has left no valid interpretations, emit an error
8345 -- message now and label node as illegal.
8347 if Present (Abstract_Op) then
8348 Get_First_Interp (N, I, It);
8350 if No (It.Nam) then
8352 -- Removal of abstract operation left no viable candidate
8354 Nondispatching_Call_To_Abstract_Operation (N, Abstract_Op);
8356 -- In Ada 2005, an abstract operation may disable predefined
8357 -- operators. Since the context is not yet known, we mark the
8358 -- predefined operators as potentially hidden. Do not include
8359 -- predefined operators when addresses are involved since this
8360 -- case is handled separately.
8362 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
8363 while Present (It.Nam) loop
8364 if Is_Numeric_Type (It.Typ)
8365 and then Scope (It.Typ) = Standard_Standard
8366 and then Ekind (It.Nam) = E_Operator
8367 then
8368 Set_Abstract_Op (I, Abstract_Op);
8369 end if;
8371 Get_Next_Interp (I, It);
8372 end loop;
8373 end if;
8374 end if;
8376 if Debug_Flag_V then
8377 Write_Line ("Remove_Abstract_Operations done: ");
8378 Write_Overloads (N);
8379 end if;
8380 end if;
8381 end Remove_Abstract_Operations;
8383 ----------------------------
8384 -- Try_Container_Indexing --
8385 ----------------------------
8387 function Try_Container_Indexing
8388 (N : Node_Id;
8389 Prefix : Node_Id;
8390 Exprs : List_Id) return Boolean
8392 Pref_Typ : Entity_Id := Etype (Prefix);
8394 function Constant_Indexing_OK return Boolean;
8395 -- Constant_Indexing is legal if there is no Variable_Indexing defined
8396 -- for the type, or else node not a target of assignment, or an actual
8397 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
8399 function Expr_Matches_In_Formal
8400 (Subp : Entity_Id;
8401 Par : Node_Id) return Boolean;
8402 -- Find formal corresponding to given indexed component that is an
8403 -- actual in a call. Note that the enclosing subprogram call has not
8404 -- been analyzed yet, and the parameter list is not normalized, so
8405 -- that if the argument is a parameter association we must match it
8406 -- by name and not by position.
8408 function Find_Indexing_Operations
8409 (T : Entity_Id;
8410 Nam : Name_Id;
8411 Is_Constant : Boolean) return Node_Id;
8412 -- Return a reference to the primitive operation of type T denoted by
8413 -- name Nam. If the operation is overloaded, the reference carries all
8414 -- interpretations. Flag Is_Constant should be set when the context is
8415 -- constant indexing.
8417 --------------------------
8418 -- Constant_Indexing_OK --
8419 --------------------------
8421 function Constant_Indexing_OK return Boolean is
8422 Par : Node_Id;
8424 begin
8425 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
8426 return True;
8428 elsif not Is_Variable (Prefix) then
8429 return True;
8430 end if;
8432 Par := N;
8433 while Present (Par) loop
8434 if Nkind (Parent (Par)) = N_Assignment_Statement
8435 and then Par = Name (Parent (Par))
8436 then
8437 return False;
8439 -- The call may be overloaded, in which case we assume that its
8440 -- resolution does not depend on the type of the parameter that
8441 -- includes the indexing operation.
8443 elsif Nkind (Parent (Par)) in N_Subprogram_Call
8444 and then Is_Entity_Name (Name (Parent (Par)))
8445 then
8446 declare
8447 Proc : Entity_Id;
8449 begin
8450 -- We should look for an interpretation with the proper
8451 -- number of formals, and determine whether it is an
8452 -- In_Parameter, but for now we examine the formal that
8453 -- corresponds to the indexing, and assume that variable
8454 -- indexing is required if some interpretation has an
8455 -- assignable formal at that position. Still does not
8456 -- cover the most complex cases ???
8458 if Is_Overloaded (Name (Parent (Par))) then
8459 declare
8460 Proc : constant Node_Id := Name (Parent (Par));
8461 I : Interp_Index;
8462 It : Interp;
8464 begin
8465 Get_First_Interp (Proc, I, It);
8466 while Present (It.Nam) loop
8467 if not Expr_Matches_In_Formal (It.Nam, Par) then
8468 return False;
8469 end if;
8471 Get_Next_Interp (I, It);
8472 end loop;
8473 end;
8475 -- All interpretations have a matching in-mode formal
8477 return True;
8479 else
8480 Proc := Entity (Name (Parent (Par)));
8482 -- If this is an indirect call, get formals from
8483 -- designated type.
8485 if Is_Access_Subprogram_Type (Etype (Proc)) then
8486 Proc := Designated_Type (Etype (Proc));
8487 end if;
8488 end if;
8490 return Expr_Matches_In_Formal (Proc, Par);
8491 end;
8493 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
8494 return False;
8496 -- If the indexed component is a prefix it may be the first actual
8497 -- of a prefixed call. Retrieve the called entity, if any, and
8498 -- check its first formal. Determine if the context is a procedure
8499 -- or function call.
8501 elsif Nkind (Parent (Par)) = N_Selected_Component then
8502 declare
8503 Sel : constant Node_Id := Selector_Name (Parent (Par));
8504 Nam : constant Entity_Id := Current_Entity (Sel);
8506 begin
8507 if Present (Nam) and then Is_Overloadable (Nam) then
8508 if Nkind (Parent (Parent (Par))) =
8509 N_Procedure_Call_Statement
8510 then
8511 return False;
8513 elsif Ekind (Nam) = E_Function
8514 and then Present (First_Formal (Nam))
8515 then
8516 return Ekind (First_Formal (Nam)) = E_In_Parameter;
8517 end if;
8518 end if;
8519 end;
8521 elsif Nkind (Par) in N_Op then
8522 return True;
8523 end if;
8525 Par := Parent (Par);
8526 end loop;
8528 -- In all other cases, constant indexing is legal
8530 return True;
8531 end Constant_Indexing_OK;
8533 ----------------------------
8534 -- Expr_Matches_In_Formal --
8535 ----------------------------
8537 function Expr_Matches_In_Formal
8538 (Subp : Entity_Id;
8539 Par : Node_Id) return Boolean
8541 Actual : Node_Id;
8542 Formal : Node_Id;
8544 begin
8545 Formal := First_Formal (Subp);
8546 Actual := First (Parameter_Associations ((Parent (Par))));
8548 if Nkind (Par) /= N_Parameter_Association then
8550 -- Match by position
8552 while Present (Actual) and then Present (Formal) loop
8553 exit when Actual = Par;
8554 Next (Actual);
8556 if Present (Formal) then
8557 Next_Formal (Formal);
8559 -- Otherwise this is a parameter mismatch, the error is
8560 -- reported elsewhere, or else variable indexing is implied.
8562 else
8563 return False;
8564 end if;
8565 end loop;
8567 else
8568 -- Match by name
8570 while Present (Formal) loop
8571 exit when Chars (Formal) = Chars (Selector_Name (Par));
8572 Next_Formal (Formal);
8574 if No (Formal) then
8575 return False;
8576 end if;
8577 end loop;
8578 end if;
8580 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
8581 end Expr_Matches_In_Formal;
8583 ------------------------------
8584 -- Find_Indexing_Operations --
8585 ------------------------------
8587 function Find_Indexing_Operations
8588 (T : Entity_Id;
8589 Nam : Name_Id;
8590 Is_Constant : Boolean) return Node_Id
8592 procedure Inspect_Declarations
8593 (Typ : Entity_Id;
8594 Ref : in out Node_Id);
8595 -- Traverse the declarative list where type Typ resides and collect
8596 -- all suitable interpretations in node Ref.
8598 procedure Inspect_Primitives
8599 (Typ : Entity_Id;
8600 Ref : in out Node_Id);
8601 -- Traverse the list of primitive operations of type Typ and collect
8602 -- all suitable interpretations in node Ref.
8604 function Is_OK_Candidate
8605 (Subp_Id : Entity_Id;
8606 Typ : Entity_Id) return Boolean;
8607 -- Determine whether subprogram Subp_Id is a suitable indexing
8608 -- operation for type Typ. To qualify as such, the subprogram must
8609 -- be a function, have at least two parameters, and the type of the
8610 -- first parameter must be either Typ, or Typ'Class, or access [to
8611 -- constant] with designated type Typ or Typ'Class.
8613 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
8614 -- Store subprogram Subp_Id as an interpretation in node Ref
8616 --------------------------
8617 -- Inspect_Declarations --
8618 --------------------------
8620 procedure Inspect_Declarations
8621 (Typ : Entity_Id;
8622 Ref : in out Node_Id)
8624 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
8625 Decl : Node_Id;
8626 Subp_Id : Entity_Id;
8628 begin
8629 -- Ensure that the routine is not called with itypes, which lack a
8630 -- declarative node.
8632 pragma Assert (Present (Typ_Decl));
8633 pragma Assert (Is_List_Member (Typ_Decl));
8635 Decl := First (List_Containing (Typ_Decl));
8636 while Present (Decl) loop
8637 if Nkind (Decl) = N_Subprogram_Declaration then
8638 Subp_Id := Defining_Entity (Decl);
8640 if Is_OK_Candidate (Subp_Id, Typ) then
8641 Record_Interp (Subp_Id, Ref);
8642 end if;
8643 end if;
8645 Next (Decl);
8646 end loop;
8647 end Inspect_Declarations;
8649 ------------------------
8650 -- Inspect_Primitives --
8651 ------------------------
8653 procedure Inspect_Primitives
8654 (Typ : Entity_Id;
8655 Ref : in out Node_Id)
8657 Prim_Elmt : Elmt_Id;
8658 Prim_Id : Entity_Id;
8660 begin
8661 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
8662 while Present (Prim_Elmt) loop
8663 Prim_Id := Node (Prim_Elmt);
8665 if Is_OK_Candidate (Prim_Id, Typ) then
8666 Record_Interp (Prim_Id, Ref);
8667 end if;
8669 Next_Elmt (Prim_Elmt);
8670 end loop;
8671 end Inspect_Primitives;
8673 ---------------------
8674 -- Is_OK_Candidate --
8675 ---------------------
8677 function Is_OK_Candidate
8678 (Subp_Id : Entity_Id;
8679 Typ : Entity_Id) return Boolean
8681 Formal : Entity_Id;
8682 Formal_Typ : Entity_Id;
8683 Param_Typ : Node_Id;
8685 begin
8686 -- To classify as a suitable candidate, the subprogram must be a
8687 -- function whose name matches the argument of aspect Constant or
8688 -- Variable_Indexing.
8690 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
8691 Formal := First_Formal (Subp_Id);
8693 -- The candidate requires at least two parameters
8695 if Present (Formal) and then Present (Next_Formal (Formal)) then
8696 Formal_Typ := Empty;
8697 Param_Typ := Parameter_Type (Parent (Formal));
8699 -- Use the designated type when the first parameter is of an
8700 -- access type.
8702 if Nkind (Param_Typ) = N_Access_Definition
8703 and then Present (Subtype_Mark (Param_Typ))
8704 then
8705 -- When the context is a constant indexing, the access
8706 -- definition must be access-to-constant. This does not
8707 -- apply to variable indexing.
8709 if not Is_Constant
8710 or else Constant_Present (Param_Typ)
8711 then
8712 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
8713 end if;
8715 -- Otherwise use the parameter type
8717 else
8718 Formal_Typ := Etype (Param_Typ);
8719 end if;
8721 if Present (Formal_Typ) then
8723 -- Use the specific type when the parameter type is
8724 -- class-wide.
8726 if Is_Class_Wide_Type (Formal_Typ) then
8727 Formal_Typ := Etype (Base_Type (Formal_Typ));
8728 end if;
8730 -- Use the full view when the parameter type is private
8731 -- or incomplete.
8733 if Is_Incomplete_Or_Private_Type (Formal_Typ)
8734 and then Present (Full_View (Formal_Typ))
8735 then
8736 Formal_Typ := Full_View (Formal_Typ);
8737 end if;
8739 -- The type of the first parameter must denote the type
8740 -- of the container or acts as its ancestor type.
8742 return
8743 Formal_Typ = Typ
8744 or else Is_Ancestor (Formal_Typ, Typ);
8745 end if;
8746 end if;
8747 end if;
8749 return False;
8750 end Is_OK_Candidate;
8752 -------------------
8753 -- Record_Interp --
8754 -------------------
8756 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8757 begin
8758 if Present (Ref) then
8759 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8761 -- Otherwise this is the first interpretation. Create a reference
8762 -- where all remaining interpretations will be collected.
8764 else
8765 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8766 end if;
8767 end Record_Interp;
8769 -- Local variables
8771 Ref : Node_Id;
8772 Typ : Entity_Id;
8774 -- Start of processing for Find_Indexing_Operations
8776 begin
8777 Typ := T;
8779 -- Use the specific type when the parameter type is class-wide
8781 if Is_Class_Wide_Type (Typ) then
8782 Typ := Root_Type (Typ);
8783 end if;
8785 Ref := Empty;
8786 Typ := Underlying_Type (Base_Type (Typ));
8788 Inspect_Primitives (Typ, Ref);
8790 -- Now look for explicit declarations of an indexing operation.
8791 -- If the type is private the operation may be declared in the
8792 -- visible part that contains the partial view.
8794 if Is_Private_Type (T) then
8795 Inspect_Declarations (T, Ref);
8796 end if;
8798 Inspect_Declarations (Typ, Ref);
8800 return Ref;
8801 end Find_Indexing_Operations;
8803 -- Local variables
8805 Loc : constant Source_Ptr := Sloc (N);
8806 Assoc : List_Id;
8807 C_Type : Entity_Id;
8808 Func : Entity_Id;
8809 Func_Name : Node_Id;
8810 Indexing : Node_Id;
8812 Is_Constant_Indexing : Boolean := False;
8813 -- This flag reflects the nature of the container indexing. Note that
8814 -- the context may be suited for constant indexing, but the type may
8815 -- lack a Constant_Indexing annotation.
8817 -- Start of processing for Try_Container_Indexing
8819 begin
8820 -- Node may have been analyzed already when testing for a prefixed
8821 -- call, in which case do not redo analysis.
8823 if Present (Generalized_Indexing (N)) then
8824 return True;
8825 end if;
8827 -- An explicit dereference needs to be created in the case of a prefix
8828 -- that's an access.
8830 -- It seems that this should be done elsewhere, but not clear where that
8831 -- should happen. Normally Insert_Explicit_Dereference is called via
8832 -- Resolve_Implicit_Dereference, called from Resolve_Indexed_Component,
8833 -- but that won't be called in this case because we transform the
8834 -- indexing to a call. Resolve_Call.Check_Prefixed_Call takes care of
8835 -- implicit dereferencing and referencing on prefixed calls, but that
8836 -- would be too late, even if we expanded to a prefix call, because
8837 -- Process_Indexed_Component will flag an error before the resolution
8838 -- happens. ???
8840 if Is_Access_Type (Pref_Typ) then
8841 Pref_Typ := Implicitly_Designated_Type (Pref_Typ);
8842 Insert_Explicit_Dereference (Prefix);
8843 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
8844 end if;
8846 C_Type := Pref_Typ;
8848 -- If indexing a class-wide container, obtain indexing primitive from
8849 -- specific type.
8851 if Is_Class_Wide_Type (C_Type) then
8852 C_Type := Etype (Base_Type (C_Type));
8853 end if;
8855 -- Check whether the type has a specified indexing aspect
8857 Func_Name := Empty;
8859 -- The context is suitable for constant indexing, so obtain the name of
8860 -- the indexing function from aspect Constant_Indexing.
8862 if Constant_Indexing_OK then
8863 Func_Name :=
8864 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
8865 end if;
8867 if Present (Func_Name) then
8868 Is_Constant_Indexing := True;
8870 -- Otherwise attempt variable indexing
8872 else
8873 Func_Name :=
8874 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
8875 end if;
8877 -- The type is not subject to either form of indexing, therefore the
8878 -- indexed component does not denote container indexing. If this is a
8879 -- true error, it is diagnosed by the caller.
8881 if No (Func_Name) then
8883 -- The prefix itself may be an indexing of a container. Rewrite it
8884 -- as such and retry.
8886 if Has_Implicit_Dereference (Pref_Typ) then
8887 Build_Explicit_Dereference
8888 (Prefix, Get_Reference_Discriminant (Pref_Typ));
8889 return Try_Container_Indexing (N, Prefix, Exprs);
8891 -- Otherwise this is definitely not container indexing
8893 else
8894 return False;
8895 end if;
8897 -- If the container type is derived from another container type, the
8898 -- value of the inherited aspect is the Reference operation declared
8899 -- for the parent type.
8901 -- However, Reference is also a primitive operation of the type, and the
8902 -- inherited operation has a different signature. We retrieve the right
8903 -- ones (the function may be overloaded) from the list of primitive
8904 -- operations of the derived type.
8906 -- Note that predefined containers are typically all derived from one of
8907 -- the Controlled types. The code below is motivated by containers that
8908 -- are derived from other types with a Reference aspect.
8909 -- Note as well that we need to examine the base type, given that
8910 -- the container object may be a constrained subtype or itype that
8911 -- does not have an explicit declaration.
8913 elsif Is_Derived_Type (C_Type)
8914 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
8915 then
8916 Func_Name :=
8917 Find_Indexing_Operations
8918 (T => Base_Type (C_Type),
8919 Nam => Chars (Func_Name),
8920 Is_Constant => Is_Constant_Indexing);
8921 end if;
8923 Assoc := New_List (Relocate_Node (Prefix));
8925 -- A generalized indexing may have nore than one index expression, so
8926 -- transfer all of them to the argument list to be used in the call.
8927 -- Note that there may be named associations, in which case the node
8928 -- was rewritten earlier as a call, and has been transformed back into
8929 -- an indexed expression to share the following processing.
8931 -- The generalized indexing node is the one on which analysis and
8932 -- resolution take place. Before expansion the original node is replaced
8933 -- with the generalized indexing node, which is a call, possibly with a
8934 -- dereference operation.
8936 -- Create argument list for function call that represents generalized
8937 -- indexing. Note that indices (i.e. actuals) may themselves be
8938 -- overloaded.
8940 declare
8941 Arg : Node_Id;
8942 New_Arg : Node_Id;
8944 begin
8945 Arg := First (Exprs);
8946 while Present (Arg) loop
8947 New_Arg := Relocate_Node (Arg);
8949 -- The arguments can be parameter associations, in which case the
8950 -- explicit actual parameter carries the overloadings.
8952 if Nkind (New_Arg) /= N_Parameter_Association then
8953 Save_Interps (Arg, New_Arg);
8954 end if;
8956 Append (New_Arg, Assoc);
8957 Next (Arg);
8958 end loop;
8959 end;
8961 if not Is_Overloaded (Func_Name) then
8962 Func := Entity (Func_Name);
8964 -- Can happen in case of e.g. cascaded errors
8966 if No (Func) then
8967 return False;
8968 end if;
8970 Indexing :=
8971 Make_Function_Call (Loc,
8972 Name => New_Occurrence_Of (Func, Loc),
8973 Parameter_Associations => Assoc);
8975 Set_Parent (Indexing, Parent (N));
8976 Set_Generalized_Indexing (N, Indexing);
8977 Analyze (Indexing);
8978 Set_Etype (N, Etype (Indexing));
8980 -- If the return type of the indexing function is a reference type,
8981 -- add the dereference as a possible interpretation. Note that the
8982 -- indexing aspect may be a function that returns the element type
8983 -- with no intervening implicit dereference, and that the reference
8984 -- discriminant is not the first discriminant.
8986 if Has_Discriminants (Etype (Func)) then
8987 Check_Implicit_Dereference (N, Etype (Func));
8988 end if;
8990 else
8991 -- If there are multiple indexing functions, build a function call
8992 -- and analyze it for each of the possible interpretations.
8994 Indexing :=
8995 Make_Function_Call (Loc,
8996 Name =>
8997 Make_Identifier (Loc, Chars (Func_Name)),
8998 Parameter_Associations => Assoc);
8999 Set_Parent (Indexing, Parent (N));
9000 Set_Generalized_Indexing (N, Indexing);
9001 Set_Etype (N, Any_Type);
9002 Set_Etype (Name (Indexing), Any_Type);
9004 declare
9005 I : Interp_Index;
9006 It : Interp;
9007 Success : Boolean;
9009 begin
9010 Get_First_Interp (Func_Name, I, It);
9011 Set_Etype (Indexing, Any_Type);
9013 -- Analyze each candidate function with the given actuals
9015 while Present (It.Nam) loop
9016 Analyze_One_Call (Indexing, It.Nam, False, Success);
9017 Get_Next_Interp (I, It);
9018 end loop;
9020 -- If there are several successful candidates, resolution will
9021 -- be by result. Mark the interpretations of the function name
9022 -- itself.
9024 if Is_Overloaded (Indexing) then
9025 Get_First_Interp (Indexing, I, It);
9027 while Present (It.Nam) loop
9028 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
9029 Get_Next_Interp (I, It);
9030 end loop;
9032 else
9033 Set_Etype (Name (Indexing), Etype (Indexing));
9034 end if;
9036 -- Now add the candidate interpretations to the indexing node
9037 -- itself, to be replaced later by the function call.
9039 if Is_Overloaded (Name (Indexing)) then
9040 Get_First_Interp (Name (Indexing), I, It);
9042 while Present (It.Nam) loop
9043 Add_One_Interp (N, It.Nam, It.Typ);
9045 -- Add dereference interpretation if the result type has
9046 -- implicit reference discriminants.
9048 if Has_Discriminants (Etype (It.Nam)) then
9049 Check_Implicit_Dereference (N, Etype (It.Nam));
9050 end if;
9052 Get_Next_Interp (I, It);
9053 end loop;
9055 else
9056 Set_Etype (N, Etype (Name (Indexing)));
9057 if Has_Discriminants (Etype (N)) then
9058 Check_Implicit_Dereference (N, Etype (N));
9059 end if;
9060 end if;
9061 end;
9062 end if;
9064 if Etype (Indexing) = Any_Type then
9065 Error_Msg_NE
9066 ("container cannot be indexed with&", N, Etype (First (Exprs)));
9067 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
9068 end if;
9070 return True;
9071 end Try_Container_Indexing;
9073 -----------------------
9074 -- Try_Indirect_Call --
9075 -----------------------
9077 function Try_Indirect_Call
9078 (N : Node_Id;
9079 Nam : Entity_Id;
9080 Typ : Entity_Id) return Boolean
9082 Actual : Node_Id;
9083 Formal : Entity_Id;
9085 Call_OK : Boolean;
9086 pragma Warnings (Off, Call_OK);
9088 begin
9089 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
9091 Actual := First_Actual (N);
9092 Formal := First_Formal (Designated_Type (Typ));
9093 while Present (Actual) and then Present (Formal) loop
9094 if not Has_Compatible_Type (Actual, Etype (Formal)) then
9095 return False;
9096 end if;
9098 Next (Actual);
9099 Next_Formal (Formal);
9100 end loop;
9102 if No (Actual) and then No (Formal) then
9103 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
9105 -- Nam is a candidate interpretation for the name in the call,
9106 -- if it is not an indirect call.
9108 if not Is_Type (Nam)
9109 and then Is_Entity_Name (Name (N))
9110 then
9111 Set_Entity (Name (N), Nam);
9112 end if;
9114 return True;
9116 else
9117 return False;
9118 end if;
9119 end Try_Indirect_Call;
9121 ----------------------
9122 -- Try_Indexed_Call --
9123 ----------------------
9125 function Try_Indexed_Call
9126 (N : Node_Id;
9127 Nam : Entity_Id;
9128 Typ : Entity_Id;
9129 Skip_First : Boolean) return Boolean
9131 Loc : constant Source_Ptr := Sloc (N);
9132 Actuals : constant List_Id := Parameter_Associations (N);
9133 Actual : Node_Id;
9134 Index : Entity_Id;
9136 begin
9137 Actual := First (Actuals);
9139 -- If the call was originally written in prefix form, skip the first
9140 -- actual, which is obviously not defaulted.
9142 if Skip_First then
9143 Next (Actual);
9144 end if;
9146 Index := First_Index (Typ);
9147 while Present (Actual) and then Present (Index) loop
9149 -- If the parameter list has a named association, the expression
9150 -- is definitely a call and not an indexed component.
9152 if Nkind (Actual) = N_Parameter_Association then
9153 return False;
9154 end if;
9156 if Is_Entity_Name (Actual)
9157 and then Is_Type (Entity (Actual))
9158 and then No (Next (Actual))
9159 then
9160 -- A single actual that is a type name indicates a slice if the
9161 -- type is discrete, and an error otherwise.
9163 if Is_Discrete_Type (Entity (Actual)) then
9164 Rewrite (N,
9165 Make_Slice (Loc,
9166 Prefix =>
9167 Make_Function_Call (Loc,
9168 Name => Relocate_Node (Name (N))),
9169 Discrete_Range =>
9170 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
9172 Analyze (N);
9174 else
9175 Error_Msg_N ("invalid use of type in expression", Actual);
9176 Set_Etype (N, Any_Type);
9177 end if;
9179 return True;
9181 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
9182 return False;
9183 end if;
9185 Next (Actual);
9186 Next_Index (Index);
9187 end loop;
9189 if No (Actual) and then No (Index) then
9190 Add_One_Interp (N, Nam, Component_Type (Typ));
9192 -- Nam is a candidate interpretation for the name in the call,
9193 -- if it is not an indirect call.
9195 if not Is_Type (Nam)
9196 and then Is_Entity_Name (Name (N))
9197 then
9198 Set_Entity (Name (N), Nam);
9199 end if;
9201 return True;
9202 else
9203 return False;
9204 end if;
9205 end Try_Indexed_Call;
9207 --------------------------
9208 -- Try_Object_Operation --
9209 --------------------------
9211 function Try_Object_Operation
9212 (N : Node_Id;
9213 CW_Test_Only : Boolean := False;
9214 Allow_Extensions : Boolean := False) return Boolean
9216 K : constant Node_Kind := Nkind (Parent (N));
9217 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
9218 Loc : constant Source_Ptr := Sloc (N);
9219 Obj : constant Node_Id := Prefix (N);
9221 Subprog : constant Node_Id :=
9222 Make_Identifier (Sloc (Selector_Name (N)),
9223 Chars => Chars (Selector_Name (N)));
9224 -- Identifier on which possible interpretations will be collected
9226 Report_Error : Boolean := False;
9227 -- If no candidate interpretation matches the context, redo analysis
9228 -- with Report_Error True to provide additional information.
9230 Actual : Node_Id;
9231 Candidate : Entity_Id := Empty;
9232 New_Call_Node : Node_Id := Empty;
9233 Node_To_Replace : Node_Id;
9234 Obj_Type : Entity_Id := Etype (Obj);
9235 Success : Boolean := False;
9237 procedure Complete_Object_Operation
9238 (Call_Node : Node_Id;
9239 Node_To_Replace : Node_Id);
9240 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
9241 -- Call_Node, insert the object (or its dereference) as the first actual
9242 -- in the call, and complete the analysis of the call.
9244 procedure Report_Ambiguity (Op : Entity_Id);
9245 -- If a prefixed procedure call is ambiguous, indicate whether the call
9246 -- includes an implicit dereference or an implicit 'Access.
9248 procedure Transform_Object_Operation
9249 (Call_Node : out Node_Id;
9250 Node_To_Replace : out Node_Id);
9251 -- Transform Obj.Operation (X, Y, ...) into Operation (Obj, X, Y ...).
9252 -- Call_Node is the resulting subprogram call, Node_To_Replace is
9253 -- either N or the parent of N, and Subprog is a reference to the
9254 -- subprogram we are trying to match. Note that the transformation
9255 -- may be partially destructive for the parent of N, so it needs to
9256 -- be undone in the case where Try_Object_Operation returns false.
9258 function Try_Class_Wide_Operation
9259 (Call_Node : Node_Id;
9260 Node_To_Replace : Node_Id) return Boolean;
9261 -- Traverse all ancestor types looking for a class-wide subprogram for
9262 -- which the current operation is a valid non-dispatching call.
9264 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
9265 -- If prefix is overloaded, its interpretation may include different
9266 -- tagged types, and we must examine the primitive operations and the
9267 -- class-wide operations of each in order to find candidate
9268 -- interpretations for the call as a whole.
9270 function Try_Primitive_Operation
9271 (Call_Node : Node_Id;
9272 Node_To_Replace : Node_Id) return Boolean;
9273 -- Traverse the list of primitive subprograms looking for a dispatching
9274 -- operation for which the current node is a valid call.
9276 function Valid_Candidate
9277 (Success : Boolean;
9278 Call : Node_Id;
9279 Subp : Entity_Id) return Entity_Id;
9280 -- If the subprogram is a valid interpretation, record it, and add to
9281 -- the list of interpretations of Subprog. Otherwise return Empty.
9283 -------------------------------
9284 -- Complete_Object_Operation --
9285 -------------------------------
9287 procedure Complete_Object_Operation
9288 (Call_Node : Node_Id;
9289 Node_To_Replace : Node_Id)
9291 Control : constant Entity_Id := First_Formal (Entity (Subprog));
9292 Formal_Type : constant Entity_Id := Etype (Control);
9293 First_Actual : Node_Id;
9295 begin
9296 -- Place the name of the operation, with its interpretations,
9297 -- on the rewritten call.
9299 Set_Name (Call_Node, Subprog);
9301 First_Actual := First (Parameter_Associations (Call_Node));
9303 -- For cross-reference purposes, treat the new node as being in the
9304 -- source if the original one is. Set entity and type, even though
9305 -- they may be overwritten during resolution if overloaded.
9307 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
9308 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
9310 if Nkind (N) = N_Selected_Component
9311 and then not Inside_A_Generic
9312 then
9313 Set_Entity (Selector_Name (N), Entity (Subprog));
9314 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
9315 end if;
9317 -- If need be, rewrite first actual as an explicit dereference. If
9318 -- the call is overloaded, the rewriting can only be done once the
9319 -- primitive operation is identified.
9321 if Is_Overloaded (Subprog) then
9323 -- The prefix itself may be overloaded, and its interpretations
9324 -- must be propagated to the new actual in the call.
9326 if Is_Overloaded (Obj) then
9327 Save_Interps (Obj, First_Actual);
9328 end if;
9330 Rewrite (First_Actual, Obj);
9332 elsif not Is_Access_Type (Formal_Type)
9333 and then Is_Access_Type (Etype (Obj))
9334 then
9335 Rewrite (First_Actual,
9336 Make_Explicit_Dereference (Sloc (Obj), Obj));
9337 Analyze (First_Actual);
9339 -- If we need to introduce an explicit dereference, verify that
9340 -- the resulting actual is compatible with the mode of the formal.
9342 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
9343 and then Is_Access_Constant (Etype (Obj))
9344 then
9345 Error_Msg_NE
9346 ("expect variable in call to&", Prefix (N), Entity (Subprog));
9347 end if;
9349 -- Conversely, if the formal is an access parameter and the object is
9350 -- not an access type or a reference type (i.e. a type with the
9351 -- Implicit_Dereference aspect specified), replace the actual with a
9352 -- 'Access reference. Its analysis will check that the object is
9353 -- aliased.
9355 elsif Is_Access_Type (Formal_Type)
9356 and then not Is_Access_Type (Etype (Obj))
9357 and then
9358 (not Has_Implicit_Dereference (Etype (Obj))
9359 or else
9360 not Is_Access_Type (Designated_Type (Etype
9361 (Get_Reference_Discriminant (Etype (Obj))))))
9362 then
9363 -- A special case: A.all'Access is illegal if A is an access to a
9364 -- constant and the context requires an access to a variable.
9366 if not Is_Access_Constant (Formal_Type) then
9367 if (Nkind (Obj) = N_Explicit_Dereference
9368 and then Is_Access_Constant (Etype (Prefix (Obj))))
9369 or else not Is_Variable (Obj)
9370 then
9371 Error_Msg_NE
9372 ("actual for & must be a variable", Obj, Control);
9373 end if;
9374 end if;
9376 Rewrite (First_Actual,
9377 Make_Attribute_Reference (Loc,
9378 Attribute_Name => Name_Access,
9379 Prefix => Relocate_Node (Obj)));
9381 -- If the object is not overloaded verify that taking access of
9382 -- it is legal. Otherwise check is made during resolution.
9384 if not Is_Overloaded (Obj)
9385 and then not Is_Aliased_View (Obj)
9386 then
9387 Error_Msg_NE
9388 ("object in prefixed call to & must be aliased "
9389 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
9390 end if;
9392 Analyze (First_Actual);
9394 else
9395 if Is_Overloaded (Obj) then
9396 Save_Interps (Obj, First_Actual);
9397 end if;
9399 Rewrite (First_Actual, Obj);
9400 end if;
9402 if In_Extended_Main_Source_Unit (Current_Scope) then
9403 -- The operation is obtained from the dispatch table and not by
9404 -- visibility, and may be declared in a unit that is not
9405 -- explicitly referenced in the source, but is nevertheless
9406 -- required in the context of the current unit. Indicate that
9407 -- operation and its scope are referenced, to prevent spurious and
9408 -- misleading warnings. If the operation is overloaded, all
9409 -- primitives are in the same scope and we can use any of them.
9410 -- Don't do that outside the main unit since otherwise this will
9411 -- e.g. prevent the detection of some unused with clauses.
9413 Set_Referenced (Entity (Subprog), True);
9414 Set_Referenced (Scope (Entity (Subprog)), True);
9415 end if;
9417 Rewrite (Node_To_Replace, Call_Node);
9419 -- Propagate the interpretations collected in subprog to the new
9420 -- function call node, to be resolved from context.
9422 if Is_Overloaded (Subprog) then
9423 Save_Interps (Subprog, Node_To_Replace);
9425 else
9426 Analyze (Node_To_Replace);
9428 -- If the operation has been rewritten into a call, which may get
9429 -- subsequently an explicit dereference, preserve the type on the
9430 -- original node (selected component or indexed component) for
9431 -- subsequent legality tests, e.g. Is_Variable. which examines
9432 -- the original node.
9434 if Nkind (Node_To_Replace) = N_Function_Call then
9435 Set_Etype
9436 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
9437 end if;
9438 end if;
9439 end Complete_Object_Operation;
9441 ----------------------
9442 -- Report_Ambiguity --
9443 ----------------------
9445 procedure Report_Ambiguity (Op : Entity_Id) is
9446 Access_Actual : constant Boolean :=
9447 Is_Access_Type (Etype (Prefix (N)));
9448 Access_Formal : Boolean := False;
9450 begin
9451 Error_Msg_Sloc := Sloc (Op);
9453 if Present (First_Formal (Op)) then
9454 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
9455 end if;
9457 if Access_Formal and then not Access_Actual then
9458 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9459 Error_Msg_N
9460 ("\possible interpretation "
9461 & "(inherited, with implicit 'Access) #", N);
9462 else
9463 Error_Msg_N
9464 ("\possible interpretation (with implicit 'Access) #", N);
9465 end if;
9467 elsif not Access_Formal and then Access_Actual then
9468 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9469 Error_Msg_N
9470 ("\possible interpretation "
9471 & "(inherited, with implicit dereference) #", N);
9472 else
9473 Error_Msg_N
9474 ("\possible interpretation (with implicit dereference) #", N);
9475 end if;
9477 else
9478 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9479 Error_Msg_N ("\possible interpretation (inherited)#", N);
9480 else
9481 Error_Msg_N -- CODEFIX
9482 ("\possible interpretation#", N);
9483 end if;
9484 end if;
9485 end Report_Ambiguity;
9487 --------------------------------
9488 -- Transform_Object_Operation --
9489 --------------------------------
9491 procedure Transform_Object_Operation
9492 (Call_Node : out Node_Id;
9493 Node_To_Replace : out Node_Id)
9495 Dummy : constant Node_Id := New_Copy (Obj);
9496 -- Placeholder used as a first parameter in the call, replaced
9497 -- eventually by the proper object.
9499 Parent_Node : constant Node_Id := Parent (N);
9501 Actual : Node_Id;
9502 Actuals : List_Id;
9504 begin
9505 -- Common case covering 1) Call to a procedure and 2) Call to a
9506 -- function that has some additional actuals.
9508 if Nkind (Parent_Node) in N_Subprogram_Call
9510 -- N is a selected component node containing the name of the
9511 -- subprogram. If N is not the name of the parent node we must
9512 -- not replace the parent node by the new construct. This case
9513 -- occurs when N is a parameterless call to a subprogram that
9514 -- is an actual parameter of a call to another subprogram. For
9515 -- example:
9516 -- Some_Subprogram (..., Obj.Operation, ...)
9518 and then N = Name (Parent_Node)
9519 then
9520 Node_To_Replace := Parent_Node;
9522 Actuals := Parameter_Associations (Parent_Node);
9524 if Present (Actuals) then
9525 Prepend (Dummy, Actuals);
9526 else
9527 Actuals := New_List (Dummy);
9528 end if;
9530 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
9531 Call_Node :=
9532 Make_Procedure_Call_Statement (Loc,
9533 Name => New_Copy (Subprog),
9534 Parameter_Associations => Actuals);
9536 else
9537 Call_Node :=
9538 Make_Function_Call (Loc,
9539 Name => New_Copy (Subprog),
9540 Parameter_Associations => Actuals);
9541 end if;
9543 -- Before analysis, a function call appears as an indexed component
9544 -- if there are no named associations.
9546 elsif Nkind (Parent_Node) = N_Indexed_Component
9547 and then N = Prefix (Parent_Node)
9548 then
9549 Node_To_Replace := Parent_Node;
9550 Actuals := Expressions (Parent_Node);
9552 Actual := First (Actuals);
9553 while Present (Actual) loop
9554 Analyze (Actual);
9555 Next (Actual);
9556 end loop;
9558 Prepend (Dummy, Actuals);
9560 Call_Node :=
9561 Make_Function_Call (Loc,
9562 Name => New_Copy (Subprog),
9563 Parameter_Associations => Actuals);
9565 -- Parameterless call: Obj.F is rewritten as F (Obj)
9567 else
9568 Node_To_Replace := N;
9570 Call_Node :=
9571 Make_Function_Call (Loc,
9572 Name => New_Copy (Subprog),
9573 Parameter_Associations => New_List (Dummy));
9574 end if;
9575 end Transform_Object_Operation;
9577 ------------------------------
9578 -- Try_Class_Wide_Operation --
9579 ------------------------------
9581 function Try_Class_Wide_Operation
9582 (Call_Node : Node_Id;
9583 Node_To_Replace : Node_Id) return Boolean
9585 Anc_Type : Entity_Id;
9586 Matching_Op : Entity_Id := Empty;
9587 Error : Boolean;
9589 procedure Traverse_Homonyms
9590 (Anc_Type : Entity_Id;
9591 Error : out Boolean);
9592 -- Traverse the homonym chain of the subprogram searching for those
9593 -- homonyms whose first formal has the Anc_Type's class-wide type,
9594 -- or an anonymous access type designating the class-wide type. If
9595 -- an ambiguity is detected, then Error is set to True.
9597 procedure Traverse_Interfaces
9598 (Anc_Type : Entity_Id;
9599 Error : out Boolean);
9600 -- Traverse the list of interfaces, if any, associated with Anc_Type
9601 -- and search for acceptable class-wide homonyms associated with each
9602 -- interface. If an ambiguity is detected, then Error is set to True.
9604 -----------------------
9605 -- Traverse_Homonyms --
9606 -----------------------
9608 procedure Traverse_Homonyms
9609 (Anc_Type : Entity_Id;
9610 Error : out Boolean)
9612 function First_Formal_Match
9613 (Subp_Id : Entity_Id;
9614 Typ : Entity_Id) return Boolean;
9615 -- Predicate to verify that the first foramal of class-wide
9616 -- subprogram Subp_Id matches type Typ of the prefix.
9618 ------------------------
9619 -- First_Formal_Match --
9620 ------------------------
9622 function First_Formal_Match
9623 (Subp_Id : Entity_Id;
9624 Typ : Entity_Id) return Boolean
9626 Ctrl : constant Entity_Id := First_Formal (Subp_Id);
9628 begin
9629 return
9630 Present (Ctrl)
9631 and then
9632 (Base_Type (Etype (Ctrl)) = Typ
9633 or else
9634 (Ekind (Etype (Ctrl)) = E_Anonymous_Access_Type
9635 and then
9636 Base_Type (Designated_Type (Etype (Ctrl))) =
9637 Typ));
9638 end First_Formal_Match;
9640 -- Local variables
9642 CW_Typ : constant Entity_Id := Class_Wide_Type (Anc_Type);
9644 Candidate : Entity_Id;
9645 -- If homonym is a renaming, examine the renamed program
9647 Hom : Entity_Id;
9648 Hom_Ref : Node_Id;
9649 Success : Boolean;
9651 -- Start of processing for Traverse_Homonyms
9653 begin
9654 Error := False;
9656 -- Find a non-hidden operation whose first parameter is of the
9657 -- class-wide type, a subtype thereof, or an anonymous access
9658 -- to same. If in an instance, the operation can be considered
9659 -- even if hidden (it may be hidden because the instantiation
9660 -- is expanded after the containing package has been analyzed).
9661 -- If the subprogram is a generic actual in an enclosing instance,
9662 -- it appears as a renaming that is a candidate interpretation as
9663 -- well.
9665 Hom := Current_Entity (Subprog);
9666 while Present (Hom) loop
9667 if Ekind (Hom) in E_Procedure | E_Function
9668 and then Present (Renamed_Entity (Hom))
9669 and then Is_Generic_Actual_Subprogram (Hom)
9670 and then In_Open_Scopes (Scope (Hom))
9671 then
9672 Candidate := Renamed_Entity (Hom);
9673 else
9674 Candidate := Hom;
9675 end if;
9677 if Ekind (Candidate) in E_Function | E_Procedure
9678 and then (not Is_Hidden (Candidate) or else In_Instance)
9679 and then Scope (Candidate) = Scope (Base_Type (Anc_Type))
9680 and then First_Formal_Match (Candidate, CW_Typ)
9681 then
9682 -- If the context is a procedure call, ignore functions
9683 -- in the name of the call.
9685 if Ekind (Candidate) = E_Function
9686 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
9687 and then N = Name (Parent (N))
9688 then
9689 goto Next_Hom;
9691 -- If the context is a function call, ignore procedures
9692 -- in the name of the call.
9694 elsif Ekind (Candidate) = E_Procedure
9695 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
9696 then
9697 goto Next_Hom;
9698 end if;
9700 Set_Etype (Call_Node, Any_Type);
9701 Set_Is_Overloaded (Call_Node, False);
9702 Success := False;
9704 if No (Matching_Op) then
9705 Hom_Ref := New_Occurrence_Of (Candidate, Sloc (Subprog));
9707 Set_Etype (Call_Node, Any_Type);
9708 Set_Name (Call_Node, Hom_Ref);
9709 Set_Parent (Call_Node, Parent (Node_To_Replace));
9711 Analyze_One_Call
9712 (N => Call_Node,
9713 Nam => Candidate,
9714 Report => Report_Error,
9715 Success => Success,
9716 Skip_First => True);
9718 Matching_Op :=
9719 Valid_Candidate (Success, Call_Node, Candidate);
9721 else
9722 Analyze_One_Call
9723 (N => Call_Node,
9724 Nam => Candidate,
9725 Report => Report_Error,
9726 Success => Success,
9727 Skip_First => True);
9729 -- The same operation may be encountered on two homonym
9730 -- traversals, before and after looking at interfaces.
9731 -- Check for this case before reporting a real ambiguity.
9733 if Present
9734 (Valid_Candidate (Success, Call_Node, Candidate))
9735 and then Nkind (Call_Node) /= N_Function_Call
9736 and then Candidate /= Matching_Op
9737 then
9738 Error_Msg_NE ("ambiguous call to&", N, Hom);
9739 Report_Ambiguity (Matching_Op);
9740 Report_Ambiguity (Hom);
9741 Check_Ambiguous_Aggregate (New_Call_Node);
9742 Error := True;
9743 return;
9744 end if;
9745 end if;
9746 end if;
9748 <<Next_Hom>>
9749 Hom := Homonym (Hom);
9750 end loop;
9751 end Traverse_Homonyms;
9753 -------------------------
9754 -- Traverse_Interfaces --
9755 -------------------------
9757 procedure Traverse_Interfaces
9758 (Anc_Type : Entity_Id;
9759 Error : out Boolean)
9761 Intface_List : constant List_Id :=
9762 Abstract_Interface_List (Anc_Type);
9763 Intface : Node_Id;
9765 begin
9766 Error := False;
9768 Intface := First (Intface_List);
9769 while Present (Intface) loop
9771 -- Look for acceptable class-wide homonyms associated with the
9772 -- interface.
9774 Traverse_Homonyms (Etype (Intface), Error);
9776 if Error then
9777 return;
9778 end if;
9780 -- Continue the search by looking at each of the interface's
9781 -- associated interface ancestors.
9783 Traverse_Interfaces (Etype (Intface), Error);
9785 if Error then
9786 return;
9787 end if;
9789 Next (Intface);
9790 end loop;
9791 end Traverse_Interfaces;
9793 -- Start of processing for Try_Class_Wide_Operation
9795 begin
9796 -- If we are searching only for conflicting class-wide subprograms
9797 -- then initialize directly Matching_Op with the target entity.
9799 if CW_Test_Only then
9800 Matching_Op := Entity (Selector_Name (N));
9801 end if;
9803 -- Loop through ancestor types (including interfaces), traversing
9804 -- the homonym chain of the subprogram, trying out those homonyms
9805 -- whose first formal has the class-wide type of the ancestor, or
9806 -- an anonymous access type designating the class-wide type.
9808 Anc_Type := Obj_Type;
9809 loop
9810 -- Look for a match among homonyms associated with the ancestor
9812 Traverse_Homonyms (Anc_Type, Error);
9814 if Error then
9815 return True;
9816 end if;
9818 -- Continue the search for matches among homonyms associated with
9819 -- any interfaces implemented by the ancestor.
9821 Traverse_Interfaces (Anc_Type, Error);
9823 if Error then
9824 return True;
9825 end if;
9827 exit when Etype (Anc_Type) = Anc_Type;
9828 Anc_Type := Etype (Anc_Type);
9829 end loop;
9831 if Present (Matching_Op) then
9832 Set_Etype (Call_Node, Etype (Matching_Op));
9833 end if;
9835 return Present (Matching_Op);
9836 end Try_Class_Wide_Operation;
9838 -----------------------------------
9839 -- Try_One_Prefix_Interpretation --
9840 -----------------------------------
9842 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
9843 Prev_Obj_Type : constant Entity_Id := Obj_Type;
9844 -- If the interpretation does not have a valid candidate type,
9845 -- preserve current value of Obj_Type for subsequent errors.
9847 begin
9848 Obj_Type := T;
9850 if Is_Access_Type (Obj_Type) then
9851 Obj_Type := Designated_Type (Obj_Type);
9852 end if;
9854 if Ekind (Obj_Type)
9855 in E_Private_Subtype | E_Record_Subtype_With_Private
9856 then
9857 Obj_Type := Base_Type (Obj_Type);
9858 end if;
9860 if Is_Class_Wide_Type (Obj_Type) then
9861 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9862 end if;
9864 -- The type may have be obtained through a limited_with clause,
9865 -- in which case the primitive operations are available on its
9866 -- nonlimited view. If still incomplete, retrieve full view.
9868 if Ekind (Obj_Type) = E_Incomplete_Type
9869 and then From_Limited_With (Obj_Type)
9870 and then Has_Non_Limited_View (Obj_Type)
9871 then
9872 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
9873 end if;
9875 -- If the object is not tagged, or the type is still an incomplete
9876 -- type, this is not a prefixed call. Restore the previous type as
9877 -- the current one is not a legal candidate.
9879 -- Extension feature: Calls with prefixed views are also supported
9880 -- for untagged types, so skip the early return when extensions are
9881 -- enabled, unless the type doesn't have a primitive operations list
9882 -- (such as in the case of predefined types).
9884 if (not Is_Tagged_Type (Obj_Type)
9885 and then
9886 (not (Core_Extensions_Allowed or Allow_Extensions)
9887 or else not Present (Primitive_Operations (Obj_Type))))
9888 or else Is_Incomplete_Type (Obj_Type)
9889 then
9890 Obj_Type := Prev_Obj_Type;
9891 return;
9892 end if;
9894 declare
9895 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
9896 Ignore : Boolean;
9897 Prim_Result : Boolean := False;
9899 begin
9900 if not CW_Test_Only then
9901 Prim_Result :=
9902 Try_Primitive_Operation
9903 (Call_Node => New_Call_Node,
9904 Node_To_Replace => Node_To_Replace);
9906 -- Extension feature: In the case where the prefix is of an
9907 -- access type, and a primitive wasn't found for the designated
9908 -- type, then if the access type has primitives we attempt a
9909 -- prefixed call using one of its primitives. (It seems that
9910 -- this isn't quite right to give preference to the designated
9911 -- type in the case where both the access and designated types
9912 -- have homographic prefixed-view operations that could result
9913 -- in an ambiguity, but handling properly may be tricky. ???)
9915 if (Core_Extensions_Allowed or Allow_Extensions)
9916 and then not Prim_Result
9917 and then Is_Named_Access_Type (Prev_Obj_Type)
9918 and then Present (Direct_Primitive_Operations (Prev_Obj_Type))
9919 then
9920 -- Temporarily reset Obj_Type to the original access type
9922 Obj_Type := Prev_Obj_Type;
9924 Prim_Result :=
9925 Try_Primitive_Operation
9926 (Call_Node => New_Call_Node,
9927 Node_To_Replace => Node_To_Replace);
9929 -- Restore Obj_Type to the designated type (is this really
9930 -- necessary, or should it only be done when Prim_Result is
9931 -- still False?).
9933 Obj_Type := Designated_Type (Obj_Type);
9934 end if;
9935 end if;
9937 -- Check if there is a class-wide subprogram covering the
9938 -- primitive. This check must be done even if a candidate
9939 -- was found in order to report ambiguous calls.
9941 if not Prim_Result then
9942 Ignore :=
9943 Try_Class_Wide_Operation
9944 (Call_Node => New_Call_Node,
9945 Node_To_Replace => Node_To_Replace);
9947 -- If we found a primitive we search for class-wide subprograms
9948 -- using a duplicate of the call node (done to avoid missing its
9949 -- decoration if there is no ambiguity).
9951 else
9952 Ignore :=
9953 Try_Class_Wide_Operation
9954 (Call_Node => Dup_Call_Node,
9955 Node_To_Replace => Node_To_Replace);
9956 end if;
9957 end;
9958 end Try_One_Prefix_Interpretation;
9960 -----------------------------
9961 -- Try_Primitive_Operation --
9962 -----------------------------
9964 function Try_Primitive_Operation
9965 (Call_Node : Node_Id;
9966 Node_To_Replace : Node_Id) return Boolean
9968 Elmt : Elmt_Id;
9969 Prim_Op : Entity_Id;
9970 Matching_Op : Entity_Id := Empty;
9971 Prim_Op_Ref : Node_Id := Empty;
9973 Corr_Type : Entity_Id := Empty;
9974 -- If the prefix is a synchronized type, the controlling type of
9975 -- the primitive operation is the corresponding record type, else
9976 -- this is the object type itself.
9978 Success : Boolean := False;
9980 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
9981 -- For tagged types the candidate interpretations are found in
9982 -- the list of primitive operations of the type and its ancestors.
9983 -- For formal tagged types we have to find the operations declared
9984 -- in the same scope as the type (including in the generic formal
9985 -- part) because the type itself carries no primitive operations,
9986 -- except for formal derived types that inherit the operations of
9987 -- the parent and progenitors.
9989 -- If the context is a generic subprogram body, the generic formals
9990 -- are visible by name, but are not in the entity list of the
9991 -- subprogram because that list starts with the subprogram formals.
9992 -- We retrieve the candidate operations from the generic declaration.
9994 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
9995 -- Prefix notation can also be used on operations that are not
9996 -- primitives of the type, but are declared in the same immediate
9997 -- declarative part, which can only mean the corresponding package
9998 -- body (see RM 4.1.3 (9.2/3)). If we are in that body we extend the
9999 -- list of primitives with body operations with the same name that
10000 -- may be candidates, so that Try_Primitive_Operations can examine
10001 -- them if no real primitive is found.
10003 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
10004 -- An operation that overrides an inherited operation in the private
10005 -- part of its package may be hidden, but if the inherited operation
10006 -- is visible a direct call to it will dispatch to the private one,
10007 -- which is therefore a valid candidate.
10009 function Names_Match
10010 (Obj_Type : Entity_Id;
10011 Prim_Op : Entity_Id;
10012 Subprog : Entity_Id) return Boolean;
10013 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
10014 -- is a protected type then compare also the original name of Prim_Op
10015 -- with the name of Subprog (since the expander may have added a
10016 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
10018 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
10019 -- Verify that the prefix, dereferenced if need be, is a valid
10020 -- controlling argument in a call to Op. The remaining actuals
10021 -- are checked in the subsequent call to Analyze_One_Call.
10023 ------------------------------
10024 -- Collect_Generic_Type_Ops --
10025 ------------------------------
10027 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
10028 Bas : constant Entity_Id := Base_Type (T);
10029 Candidates : constant Elist_Id := New_Elmt_List;
10030 Subp : Entity_Id;
10031 Formal : Entity_Id;
10033 procedure Check_Candidate;
10034 -- The operation is a candidate if its first parameter is a
10035 -- controlling operand of the desired type.
10037 -----------------------
10038 -- Check_Candidate; --
10039 -----------------------
10041 procedure Check_Candidate is
10042 begin
10043 Formal := First_Formal (Subp);
10045 if Present (Formal)
10046 and then Is_Controlling_Formal (Formal)
10047 and then
10048 (Base_Type (Etype (Formal)) = Bas
10049 or else
10050 (Is_Access_Type (Etype (Formal))
10051 and then Designated_Type (Etype (Formal)) = Bas))
10052 then
10053 Append_Elmt (Subp, Candidates);
10054 end if;
10055 end Check_Candidate;
10057 -- Start of processing for Collect_Generic_Type_Ops
10059 begin
10060 if Is_Derived_Type (T) then
10061 return Primitive_Operations (T);
10063 elsif Ekind (Scope (T)) in E_Procedure | E_Function then
10065 -- Scan the list of generic formals to find subprograms
10066 -- that may have a first controlling formal of the type.
10068 if Nkind (Unit_Declaration_Node (Scope (T))) =
10069 N_Generic_Subprogram_Declaration
10070 then
10071 declare
10072 Decl : Node_Id;
10074 begin
10075 Decl :=
10076 First (Generic_Formal_Declarations
10077 (Unit_Declaration_Node (Scope (T))));
10078 while Present (Decl) loop
10079 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
10080 Subp := Defining_Entity (Decl);
10081 Check_Candidate;
10082 end if;
10084 Next (Decl);
10085 end loop;
10086 end;
10087 end if;
10088 return Candidates;
10090 else
10091 -- Scan the list of entities declared in the same scope as
10092 -- the type. In general this will be an open scope, given that
10093 -- the call we are analyzing can only appear within a generic
10094 -- declaration or body (either the one that declares T, or a
10095 -- child unit).
10097 -- For a subtype representing a generic actual type, go to the
10098 -- base type.
10100 if Is_Generic_Actual_Type (T) then
10101 Subp := First_Entity (Scope (Base_Type (T)));
10102 else
10103 Subp := First_Entity (Scope (T));
10104 end if;
10106 while Present (Subp) loop
10107 if Is_Overloadable (Subp) then
10108 Check_Candidate;
10109 end if;
10111 Next_Entity (Subp);
10112 end loop;
10114 return Candidates;
10115 end if;
10116 end Collect_Generic_Type_Ops;
10118 ----------------------------
10119 -- Extended_Primitive_Ops --
10120 ----------------------------
10122 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
10123 Type_Scope : constant Entity_Id := Scope (T);
10124 Op_List : Elist_Id := Primitive_Operations (T);
10125 begin
10126 if Is_Package_Or_Generic_Package (Type_Scope)
10127 and then ((In_Package_Body (Type_Scope)
10128 and then In_Open_Scopes (Type_Scope)) or else In_Instance_Body)
10129 then
10130 -- Retrieve list of declarations of package body if possible
10132 declare
10133 The_Body : constant Node_Id :=
10134 Corresponding_Body (Unit_Declaration_Node (Type_Scope));
10135 begin
10136 if Present (The_Body) then
10137 declare
10138 Body_Decls : constant List_Id :=
10139 Declarations (Unit_Declaration_Node (The_Body));
10140 Op_Found : Boolean := False;
10141 Op : Entity_Id := Current_Entity (Subprog);
10142 begin
10143 while Present (Op) loop
10144 if Comes_From_Source (Op)
10145 and then Is_Overloadable (Op)
10147 -- Exclude overriding primitive operations of a
10148 -- type extension declared in the package body,
10149 -- to prevent duplicates in extended list.
10151 and then not Is_Primitive (Op)
10152 and then Is_List_Member
10153 (Unit_Declaration_Node (Op))
10154 and then List_Containing
10155 (Unit_Declaration_Node (Op)) = Body_Decls
10156 then
10157 if not Op_Found then
10158 -- Copy list of primitives so it is not
10159 -- affected for other uses.
10161 Op_List := New_Copy_Elist (Op_List);
10162 Op_Found := True;
10163 end if;
10165 Append_Elmt (Op, Op_List);
10166 end if;
10168 Op := Homonym (Op);
10169 end loop;
10170 end;
10171 end if;
10172 end;
10173 end if;
10175 return Op_List;
10176 end Extended_Primitive_Ops;
10178 ---------------------------
10179 -- Is_Private_Overriding --
10180 ---------------------------
10182 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
10183 Visible_Op : Entity_Id;
10185 begin
10186 -- The subprogram may be overloaded with both visible and private
10187 -- entities with the same name. We have to scan the chain of
10188 -- homonyms to determine whether there is a previous implicit
10189 -- declaration in the same scope that is overridden by the
10190 -- private candidate.
10192 Visible_Op := Homonym (Op);
10193 while Present (Visible_Op) loop
10194 if Scope (Op) /= Scope (Visible_Op) then
10195 return False;
10197 elsif not Comes_From_Source (Visible_Op)
10198 and then Alias (Visible_Op) = Op
10199 and then not Is_Hidden (Visible_Op)
10200 then
10201 return True;
10202 end if;
10204 Visible_Op := Homonym (Visible_Op);
10205 end loop;
10207 return False;
10208 end Is_Private_Overriding;
10210 -----------------
10211 -- Names_Match --
10212 -----------------
10214 function Names_Match
10215 (Obj_Type : Entity_Id;
10216 Prim_Op : Entity_Id;
10217 Subprog : Entity_Id) return Boolean is
10218 begin
10219 -- Common case: exact match
10221 if Chars (Prim_Op) = Chars (Subprog) then
10222 return True;
10224 -- For protected type primitives the expander may have built the
10225 -- name of the dispatching primitive prepending the type name to
10226 -- avoid conflicts with the name of the protected subprogram (see
10227 -- Exp_Ch9.Build_Selected_Name).
10229 elsif Is_Protected_Type (Obj_Type) then
10230 return
10231 Present (Original_Protected_Subprogram (Prim_Op))
10232 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
10233 Chars (Subprog);
10235 -- In an instance, the selector name may be a generic actual that
10236 -- renames a primitive operation of the type of the prefix.
10238 elsif In_Instance and then Present (Current_Entity (Subprog)) then
10239 declare
10240 Subp : constant Entity_Id := Current_Entity (Subprog);
10241 begin
10242 if Present (Subp)
10243 and then Is_Subprogram (Subp)
10244 and then Present (Renamed_Entity (Subp))
10245 and then Is_Generic_Actual_Subprogram (Subp)
10246 and then Chars (Renamed_Entity (Subp)) = Chars (Prim_Op)
10247 then
10248 return True;
10249 end if;
10250 end;
10251 end if;
10253 return False;
10254 end Names_Match;
10256 -----------------------------
10257 -- Valid_First_Argument_Of --
10258 -----------------------------
10260 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
10261 Typ : Entity_Id := Etype (First_Formal (Op));
10263 begin
10264 if Is_Concurrent_Type (Typ)
10265 and then Present (Corresponding_Record_Type (Typ))
10266 then
10267 Typ := Corresponding_Record_Type (Typ);
10268 end if;
10270 -- Simple case. Object may be a subtype of the tagged type or may
10271 -- be the corresponding record of a synchronized type.
10273 return Obj_Type = Typ
10274 or else Base_Type (Obj_Type) = Base_Type (Typ)
10275 or else Corr_Type = Typ
10277 -- Object may be of a derived type whose parent has unknown
10278 -- discriminants, in which case the type matches the underlying
10279 -- record view of its base.
10281 or else
10282 (Has_Unknown_Discriminants (Typ)
10283 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
10285 -- Prefix can be dereferenced
10287 or else
10288 (Is_Access_Type (Corr_Type)
10289 and then Designated_Type (Corr_Type) = Typ)
10291 -- Formal is an access parameter, for which the object can
10292 -- provide an access.
10294 or else
10295 (Ekind (Typ) = E_Anonymous_Access_Type
10296 and then
10297 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
10298 end Valid_First_Argument_Of;
10300 -- Start of processing for Try_Primitive_Operation
10302 begin
10303 -- Look for subprograms in the list of primitive operations. The name
10304 -- must be identical, and the kind of call indicates the expected
10305 -- kind of operation (function or procedure). If the type is a
10306 -- (tagged) synchronized type, the primitive ops are attached to the
10307 -- corresponding record (base) type.
10309 if Is_Concurrent_Type (Obj_Type) then
10310 if Present (Corresponding_Record_Type (Obj_Type)) then
10311 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
10312 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
10313 else
10314 Corr_Type := Obj_Type;
10315 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
10316 end if;
10318 elsif not Is_Generic_Type (Obj_Type) then
10319 Corr_Type := Obj_Type;
10320 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
10322 else
10323 Corr_Type := Obj_Type;
10324 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
10325 end if;
10327 while Present (Elmt) loop
10328 Prim_Op := Node (Elmt);
10330 if Names_Match (Obj_Type, Prim_Op, Subprog)
10331 and then Present (First_Formal (Prim_Op))
10332 and then Valid_First_Argument_Of (Prim_Op)
10333 and then
10334 (Nkind (Call_Node) = N_Function_Call)
10336 (Ekind (Prim_Op) = E_Function)
10337 then
10338 -- Ada 2005 (AI-251): If this primitive operation corresponds
10339 -- to an immediate ancestor interface there is no need to add
10340 -- it to the list of interpretations; the corresponding aliased
10341 -- primitive is also in this list of primitive operations and
10342 -- will be used instead.
10344 if (Present (Interface_Alias (Prim_Op))
10345 and then Is_Ancestor (Find_Dispatching_Type
10346 (Alias (Prim_Op)), Corr_Type))
10348 -- Do not consider hidden primitives unless the type is in an
10349 -- open scope or we are within an instance, where visibility
10350 -- is known to be correct, or else if this is an overriding
10351 -- operation in the private part for an inherited operation.
10353 or else (Is_Hidden (Prim_Op)
10354 and then not Is_Immediately_Visible (Obj_Type)
10355 and then not In_Instance
10356 and then not Is_Private_Overriding (Prim_Op))
10357 then
10358 goto Continue;
10359 end if;
10361 Set_Etype (Call_Node, Any_Type);
10362 Set_Is_Overloaded (Call_Node, False);
10364 if No (Matching_Op) then
10365 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
10366 Candidate := Prim_Op;
10368 Set_Parent (Call_Node, Parent (Node_To_Replace));
10370 Set_Name (Call_Node, Prim_Op_Ref);
10371 Success := False;
10373 Analyze_One_Call
10374 (N => Call_Node,
10375 Nam => Prim_Op,
10376 Report => Report_Error,
10377 Success => Success,
10378 Skip_First => True);
10380 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
10382 -- More than one interpretation, collect for subsequent
10383 -- disambiguation. If this is a procedure call and there
10384 -- is another match, report ambiguity now.
10386 else
10387 Analyze_One_Call
10388 (N => Call_Node,
10389 Nam => Prim_Op,
10390 Report => Report_Error,
10391 Success => Success,
10392 Skip_First => True);
10394 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
10395 and then Nkind (Call_Node) /= N_Function_Call
10396 then
10397 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
10398 Report_Ambiguity (Matching_Op);
10399 Report_Ambiguity (Prim_Op);
10400 Check_Ambiguous_Aggregate (Call_Node);
10401 return True;
10402 end if;
10403 end if;
10404 end if;
10406 <<Continue>>
10407 Next_Elmt (Elmt);
10408 end loop;
10410 if Present (Matching_Op) then
10411 Set_Etype (Call_Node, Etype (Matching_Op));
10412 end if;
10414 return Present (Matching_Op);
10415 end Try_Primitive_Operation;
10417 ---------------------
10418 -- Valid_Candidate --
10419 ---------------------
10421 function Valid_Candidate
10422 (Success : Boolean;
10423 Call : Node_Id;
10424 Subp : Entity_Id) return Entity_Id
10426 Arr_Type : Entity_Id;
10427 Comp_Type : Entity_Id;
10429 begin
10430 -- If the subprogram is a valid interpretation, record it in global
10431 -- variable Subprog, to collect all possible overloadings.
10433 if Success then
10434 if Subp /= Entity (Subprog) then
10435 Add_One_Interp (Subprog, Subp, Etype (Subp));
10436 end if;
10437 end if;
10439 -- If the call may be an indexed call, retrieve component type of
10440 -- resulting expression, and add possible interpretation.
10442 Arr_Type := Empty;
10443 Comp_Type := Empty;
10445 if Nkind (Call) = N_Function_Call
10446 and then Nkind (Parent (N)) = N_Indexed_Component
10447 and then Needs_One_Actual (Subp)
10448 then
10449 if Is_Array_Type (Etype (Subp)) then
10450 Arr_Type := Etype (Subp);
10452 elsif Is_Access_Type (Etype (Subp))
10453 and then Is_Array_Type (Designated_Type (Etype (Subp)))
10454 then
10455 Arr_Type := Designated_Type (Etype (Subp));
10456 end if;
10457 end if;
10459 if Present (Arr_Type) then
10461 -- Verify that the actuals (excluding the object) match the types
10462 -- of the indexes.
10464 declare
10465 Actual : Node_Id;
10466 Index : Node_Id;
10468 begin
10469 Actual := Next (First_Actual (Call));
10470 Index := First_Index (Arr_Type);
10471 while Present (Actual) and then Present (Index) loop
10472 if not Has_Compatible_Type (Actual, Etype (Index)) then
10473 Arr_Type := Empty;
10474 exit;
10475 end if;
10477 Next_Actual (Actual);
10478 Next_Index (Index);
10479 end loop;
10481 if No (Actual)
10482 and then No (Index)
10483 and then Present (Arr_Type)
10484 then
10485 Comp_Type := Component_Type (Arr_Type);
10486 end if;
10487 end;
10489 if Present (Comp_Type)
10490 and then Etype (Subprog) /= Comp_Type
10491 then
10492 Add_One_Interp (Subprog, Subp, Comp_Type);
10493 end if;
10494 end if;
10496 if Etype (Call) /= Any_Type then
10497 return Subp;
10498 else
10499 return Empty;
10500 end if;
10501 end Valid_Candidate;
10503 -- Start of processing for Try_Object_Operation
10505 begin
10506 Analyze_Expression (Obj);
10508 -- Analyze the actuals if node is known to be a subprogram call
10510 if Is_Subprg_Call and then N = Name (Parent (N)) then
10511 Actual := First (Parameter_Associations (Parent (N)));
10512 while Present (Actual) loop
10513 Analyze_Expression (Actual);
10514 Next (Actual);
10515 end loop;
10516 end if;
10518 -- Build a subprogram call node, using a copy of Obj as its first
10519 -- actual. This is a placeholder, to be replaced by an explicit
10520 -- dereference when needed.
10522 Transform_Object_Operation
10523 (Call_Node => New_Call_Node,
10524 Node_To_Replace => Node_To_Replace);
10526 Set_Etype (New_Call_Node, Any_Type);
10527 Set_Etype (Subprog, Any_Type);
10528 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
10530 if not Is_Overloaded (Obj) then
10531 Try_One_Prefix_Interpretation (Obj_Type);
10533 else
10534 declare
10535 I : Interp_Index;
10536 It : Interp;
10537 begin
10538 Get_First_Interp (Obj, I, It);
10539 while Present (It.Nam) loop
10540 Try_One_Prefix_Interpretation (It.Typ);
10541 Get_Next_Interp (I, It);
10542 end loop;
10543 end;
10544 end if;
10546 if Etype (New_Call_Node) /= Any_Type then
10548 -- No need to complete the tree transformations if we are only
10549 -- searching for conflicting class-wide subprograms
10551 if CW_Test_Only then
10552 return False;
10553 else
10554 Complete_Object_Operation
10555 (Call_Node => New_Call_Node,
10556 Node_To_Replace => Node_To_Replace);
10557 return True;
10558 end if;
10560 elsif Present (Candidate) then
10562 -- The argument list is not type correct. Re-analyze with error
10563 -- reporting enabled, and use one of the possible candidates.
10564 -- In All_Errors_Mode, re-analyze all failed interpretations.
10566 if All_Errors_Mode then
10567 Report_Error := True;
10568 if Try_Primitive_Operation
10569 (Call_Node => New_Call_Node,
10570 Node_To_Replace => Node_To_Replace)
10572 or else
10573 Try_Class_Wide_Operation
10574 (Call_Node => New_Call_Node,
10575 Node_To_Replace => Node_To_Replace)
10576 then
10577 null;
10578 end if;
10580 else
10581 Analyze_One_Call
10582 (N => New_Call_Node,
10583 Nam => Candidate,
10584 Report => True,
10585 Success => Success,
10586 Skip_First => True);
10588 -- The error may hot have been reported yet for overloaded
10589 -- prefixed calls, depending on the non-matching candidate,
10590 -- in which case provide a concise error now.
10592 if Serious_Errors_Detected = 0 then
10593 Error_Msg_NE
10594 ("cannot resolve prefixed call to primitive operation of&",
10595 N, Entity (Obj));
10596 end if;
10597 end if;
10599 -- No need for further errors
10601 return True;
10603 else
10604 -- There was no candidate operation, but Analyze_Selected_Component
10605 -- may continue the analysis so we need to undo the change possibly
10606 -- made to the Parent of N earlier by Transform_Object_Operation.
10608 declare
10609 Parent_Node : constant Node_Id := Parent (N);
10611 begin
10612 if Node_To_Replace = Parent_Node then
10613 Remove (First (Parameter_Associations (New_Call_Node)));
10614 Set_Parent
10615 (Parameter_Associations (New_Call_Node), Parent_Node);
10616 end if;
10617 end;
10619 return False;
10620 end if;
10621 end Try_Object_Operation;
10623 ---------
10624 -- wpo --
10625 ---------
10627 procedure wpo (T : Entity_Id) is
10628 Op : Entity_Id;
10629 E : Elmt_Id;
10631 begin
10632 if not Is_Tagged_Type (T) then
10633 return;
10634 end if;
10636 E := First_Elmt (Primitive_Operations (Base_Type (T)));
10637 while Present (E) loop
10638 Op := Node (E);
10639 Write_Int (Int (Op));
10640 Write_Str (" === ");
10641 Write_Name (Chars (Op));
10642 Write_Str (" in ");
10643 Write_Name (Chars (Scope (Op)));
10644 Next_Elmt (E);
10645 Write_Eol;
10646 end loop;
10647 end wpo;
10649 end Sem_Ch4;