Daily bump.
[official-gcc.git] / gcc / ada / sem_ch4.adb
blob03364dade9f63c76c126531e415ec4f41198beb8
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-2024, 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 Style; use Style;
69 with Tbuild; use Tbuild;
70 with Uintp; use Uintp;
71 with Warnsw; use Warnsw;
73 package body Sem_Ch4 is
75 -- Tables which speed up the identification of dangerous calls to Ada 2012
76 -- functions with writable actuals (AI05-0144).
78 -- The following table enumerates the Ada constructs which may evaluate in
79 -- arbitrary order. It does not cover all the language constructs which can
80 -- be evaluated in arbitrary order but the subset needed for AI05-0144.
82 Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean :=
83 (N_Aggregate => True,
84 N_Assignment_Statement => True,
85 N_Entry_Call_Statement => True,
86 N_Extension_Aggregate => True,
87 N_Full_Type_Declaration => True,
88 N_Indexed_Component => True,
89 N_Object_Declaration => True,
90 N_Pragma => True,
91 N_Range => True,
92 N_Slice => True,
93 N_Array_Type_Definition => True,
94 N_Membership_Test => True,
95 N_Binary_Op => True,
96 N_Subprogram_Call => True,
97 others => False);
99 -- The following table enumerates the nodes on which we stop climbing when
100 -- locating the outermost Ada construct that can be evaluated in arbitrary
101 -- order.
103 Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean :=
104 (N_Aggregate => True,
105 N_Assignment_Statement => True,
106 N_Entry_Call_Statement => True,
107 N_Extended_Return_Statement => True,
108 N_Extension_Aggregate => True,
109 N_Full_Type_Declaration => True,
110 N_Object_Declaration => True,
111 N_Object_Renaming_Declaration => True,
112 N_Package_Specification => True,
113 N_Pragma => True,
114 N_Procedure_Call_Statement => True,
115 N_Simple_Return_Statement => True,
116 N_Has_Condition => True,
117 others => False);
119 -----------------------
120 -- Local Subprograms --
121 -----------------------
123 procedure Analyze_Concatenation_Rest (N : Node_Id);
124 -- Does the "rest" of the work of Analyze_Concatenation, after the left
125 -- operand has been analyzed. See Analyze_Concatenation for details.
127 procedure Analyze_Expression (N : Node_Id);
128 -- For expressions that are not names, this is just a call to analyze. If
129 -- the expression is a name, it may be a call to a parameterless function,
130 -- and if so must be converted into an explicit call node and analyzed as
131 -- such. This deproceduring must be done during the first pass of overload
132 -- resolution, because otherwise a procedure call with overloaded actuals
133 -- may fail to resolve.
135 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
136 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an
137 -- operator name or an expanded name whose selector is an operator name,
138 -- and one possible interpretation is as a predefined operator.
140 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
141 -- If the prefix of a selected_component is overloaded, the proper
142 -- interpretation that yields a record type with the proper selector
143 -- name must be selected.
145 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
146 -- Procedure to analyze a user defined binary operator, which is resolved
147 -- like a function, but instead of a list of actuals it is presented
148 -- with the left and right operands of an operator node.
150 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
151 -- Procedure to analyze a user defined unary operator, which is resolved
152 -- like a function, but instead of a list of actuals, it is presented with
153 -- the operand of the operator node.
155 procedure Analyze_One_Call
156 (N : Node_Id;
157 Nam : Entity_Id;
158 Report : Boolean;
159 Success : out Boolean;
160 Skip_First : Boolean := False);
161 -- Check one interpretation of an overloaded subprogram name for
162 -- compatibility with the types of the actuals in a call. If there is a
163 -- single interpretation which does not match, post error if Report is
164 -- set to True.
166 -- Nam is the entity that provides the formals against which the actuals
167 -- are checked. Nam is either the name of a subprogram, or the internal
168 -- subprogram type constructed for an access_to_subprogram. If the actuals
169 -- are compatible with Nam, then Nam is added to the list of candidate
170 -- interpretations for N, and Success is set to True.
172 -- The flag Skip_First is used when analyzing a call that was rewritten
173 -- from object notation. In this case the first actual may have to receive
174 -- an explicit dereference, depending on the first formal of the operation
175 -- being called. The caller will have verified that the object is legal
176 -- for the call. If the remaining parameters match, the first parameter
177 -- will rewritten as a dereference if needed, prior to completing analysis.
179 procedure Check_Misspelled_Selector
180 (Prefix : Entity_Id;
181 Sel : Node_Id);
182 -- Give possible misspelling message if Sel seems likely to be a mis-
183 -- spelling of one of the selectors of the Prefix. This is called by
184 -- Analyze_Selected_Component after producing an invalid selector error
185 -- message.
187 procedure Find_Arithmetic_Types
188 (L, R : Node_Id;
189 Op_Id : Entity_Id;
190 N : Node_Id);
191 -- L and R are the operands of an arithmetic operator. Find consistent
192 -- pairs of interpretations for L and R that have a numeric type consistent
193 -- with the semantics of the operator.
195 procedure Find_Comparison_Equality_Types
196 (L, R : Node_Id;
197 Op_Id : Entity_Id;
198 N : Node_Id);
199 -- L and R are operands of a comparison or equality operator. Find valid
200 -- pairs of interpretations for L and R.
202 procedure Find_Concatenation_Types
203 (L, R : Node_Id;
204 Op_Id : Entity_Id;
205 N : Node_Id);
206 -- For the four varieties of concatenation
208 procedure Find_Boolean_Types
209 (L, R : Node_Id;
210 Op_Id : Entity_Id;
211 N : Node_Id);
212 -- Ditto for binary logical operations
214 procedure Find_Negation_Types
215 (R : Node_Id;
216 Op_Id : Entity_Id;
217 N : Node_Id);
218 -- Find consistent interpretation for operand of negation operator
220 function Find_Primitive_Operation (N : Node_Id) return Boolean;
221 -- Find candidate interpretations for the name Obj.Proc when it appears in
222 -- a subprogram renaming declaration.
224 procedure Find_Unary_Types
225 (R : Node_Id;
226 Op_Id : Entity_Id;
227 N : Node_Id);
228 -- Unary arithmetic types: plus, minus, abs
230 procedure Check_Arithmetic_Pair
231 (T1, T2 : Entity_Id;
232 Op_Id : Entity_Id;
233 N : Node_Id);
234 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
235 -- for left and right operand. Determine whether they constitute a valid
236 -- pair for the given operator, and record the corresponding interpretation
237 -- of the operator node. The node N may be an operator node (the usual
238 -- case) or a function call whose prefix is an operator designator. In
239 -- both cases Op_Id is the operator name itself.
241 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
242 -- Give detailed information on overloaded call where none of the
243 -- interpretations match. N is the call node, Nam the designator for
244 -- the overloaded entity being called.
246 function Junk_Operand (N : Node_Id) return Boolean;
247 -- Test for an operand that is an inappropriate entity (e.g. a package
248 -- name or a label). If so, issue an error message and return True. If
249 -- the operand is not an inappropriate entity kind, return False.
251 procedure Operator_Check (N : Node_Id);
252 -- Verify that an operator has received some valid interpretation. If none
253 -- was found, determine whether a use clause would make the operation
254 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
255 -- every type compatible with the operator, even if the operator for the
256 -- type is not directly visible. The routine uses this type to emit a more
257 -- informative message.
259 function Has_Possible_User_Defined_Literal (N : Node_Id) return Boolean;
260 -- Ada 2022: if an operand is a literal, it may be subject to an
261 -- implicit conversion to a type for which a user-defined literal
262 -- function exists. During the first pass of type resolution we do
263 -- not know the context imposed on the literal, so we assume that
264 -- the literal type is a valid candidate and rely on the second pass
265 -- of resolution to find the type with the proper aspect. We only
266 -- add this interpretation if no other one was found, which may be
267 -- too restrictive but seems sufficient to handle most proper uses
268 -- of the new aspect. It is unclear whether a full implementation of
269 -- these aspects can be achieved without larger modifications to the
270 -- two-pass resolution algorithm.
272 function Possible_Type_For_Conditional_Expression
273 (T1, T2 : Entity_Id) return Entity_Id;
274 -- Given two types T1 and T2 that are _not_ compatible, return a type that
275 -- may still be used as the possible type of a conditional expression whose
276 -- dependent expressions, or part thereof, have type T1 and T2 respectively
277 -- during the first phase of type resolution, or Empty if such a type does
278 -- not exist.
280 -- The typical example is an if_expression whose then_expression is of a
281 -- tagged type and whose else_expresssion is of an extension of this type:
282 -- the types are not compatible but such an if_expression can be legal if
283 -- its expected type is the 'Class of the tagged type, so the function will
284 -- return the tagged type in this case. If the expected type turns out to
285 -- be something else, including the tagged type itself, then an error will
286 -- be given during the second phase of type resolution.
288 procedure Remove_Abstract_Operations (N : Node_Id);
289 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
290 -- operation is not a candidate interpretation.
292 function Try_Container_Indexing
293 (N : Node_Id;
294 Prefix : Node_Id;
295 Exprs : List_Id) return Boolean;
296 -- AI05-0139: Generalized indexing to support iterators over containers
297 -- ??? Need to provide a more detailed spec of what this function does
299 function Try_Indexed_Call
300 (N : Node_Id;
301 Nam : Entity_Id;
302 Typ : Entity_Id;
303 Skip_First : Boolean) return Boolean;
304 -- If a function has defaults for all its actuals, a call to it may in fact
305 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
306 -- interpretation as an indexing, prior to analysis as a call. If both are
307 -- possible, the node is overloaded with both interpretations (same symbol
308 -- but two different types). If the call is written in prefix form, the
309 -- prefix becomes the first parameter in the call, and only the remaining
310 -- actuals must be checked for the presence of defaults.
312 function Try_Indirect_Call
313 (N : Node_Id;
314 Nam : Entity_Id;
315 Typ : Entity_Id) return Boolean;
316 -- Similarly, a function F that needs no actuals can return an access to a
317 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
318 -- the call may be overloaded with both interpretations.
320 procedure wpo (T : Entity_Id);
321 pragma Warnings (Off, wpo);
322 -- Used for debugging: obtain list of primitive operations even if
323 -- type is not frozen and dispatch table is not built yet.
325 ------------------------
326 -- Ambiguous_Operands --
327 ------------------------
329 procedure Ambiguous_Operands (N : Node_Id) is
330 procedure List_Operand_Interps (Opnd : Node_Id);
332 --------------------------
333 -- List_Operand_Interps --
334 --------------------------
336 procedure List_Operand_Interps (Opnd : Node_Id) is
337 Nam : Node_Id := Empty;
338 Err : Node_Id := N;
340 begin
341 if Is_Overloaded (Opnd) then
342 if Nkind (Opnd) in N_Op then
343 Nam := Opnd;
345 elsif Nkind (Opnd) = N_Function_Call then
346 Nam := Name (Opnd);
348 elsif Ada_Version >= Ada_2012 then
349 declare
350 It : Interp;
351 I : Interp_Index;
353 begin
354 Get_First_Interp (Opnd, I, It);
355 while Present (It.Nam) loop
356 if Has_Implicit_Dereference (It.Typ) then
357 Error_Msg_N
358 ("can be interpreted as implicit dereference", Opnd);
359 return;
360 end if;
362 Get_Next_Interp (I, It);
363 end loop;
364 end;
366 return;
367 end if;
369 else
370 return;
371 end if;
373 if Opnd = Left_Opnd (N) then
374 Error_Msg_N
375 ("\left operand has the following interpretations", N);
376 else
377 Error_Msg_N
378 ("\right operand has the following interpretations", N);
379 Err := Opnd;
380 end if;
382 List_Interps (Nam, Err);
383 end List_Operand_Interps;
385 -- Start of processing for Ambiguous_Operands
387 begin
388 if Nkind (N) in N_Membership_Test then
389 Error_Msg_N ("ambiguous operands for membership", N);
391 elsif Nkind (N) in N_Op_Eq | N_Op_Ne then
392 Error_Msg_N ("ambiguous operands for equality", N);
394 else
395 Error_Msg_N ("ambiguous operands for comparison", N);
396 end if;
398 if All_Errors_Mode then
399 List_Operand_Interps (Left_Opnd (N));
400 List_Operand_Interps (Right_Opnd (N));
401 else
402 Error_Msg_N ("\use -gnatf switch for details", N);
403 end if;
404 end Ambiguous_Operands;
406 -----------------------
407 -- Analyze_Aggregate --
408 -----------------------
410 -- Most of the analysis of Aggregates requires that the type be known, and
411 -- is therefore put off until resolution of the context. Delta aggregates
412 -- have a base component that determines the enclosing aggregate type so
413 -- its type can be ascertained earlier. This also allows delta aggregates
414 -- to appear in the context of a record type with a private extension, as
415 -- per the latest update of AI12-0127.
417 procedure Analyze_Aggregate (N : Node_Id) is
418 begin
419 if No (Etype (N)) then
420 if Nkind (N) = N_Delta_Aggregate then
421 declare
422 Base : constant Node_Id := Expression (N);
424 I : Interp_Index;
425 It : Interp;
427 begin
428 Analyze (Base);
430 -- If the base is overloaded, propagate interpretations to the
431 -- enclosing aggregate.
433 if Is_Overloaded (Base) then
434 Get_First_Interp (Base, I, It);
435 Set_Etype (N, Any_Type);
437 while Present (It.Nam) loop
438 Add_One_Interp (N, It.Typ, It.Typ);
439 Get_Next_Interp (I, It);
440 end loop;
442 else
443 Set_Etype (N, Etype (Base));
444 end if;
445 end;
447 else
448 Set_Etype (N, Any_Composite);
449 end if;
450 end if;
451 end Analyze_Aggregate;
453 -----------------------
454 -- Analyze_Allocator --
455 -----------------------
457 procedure Analyze_Allocator (N : Node_Id) is
458 Loc : constant Source_Ptr := Sloc (N);
459 Sav_Errs : constant Nat := Serious_Errors_Detected;
460 E : Node_Id := Expression (N);
461 Acc_Type : Entity_Id;
462 Type_Id : Entity_Id;
463 P : Node_Id;
464 C : Node_Id;
465 Onode : Node_Id;
467 begin
468 -- Deal with allocator restrictions
470 -- In accordance with H.4(7), the No_Allocators restriction only applies
471 -- to user-written allocators. The same consideration applies to the
472 -- No_Standard_Allocators_Before_Elaboration restriction.
474 if Comes_From_Source (N) then
475 Check_Restriction (No_Allocators, N);
477 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
478 -- look at enclosing context, checking task/main subprogram case.
480 C := N;
481 P := Parent (C);
482 while Present (P) loop
484 -- For the task case we need a handled sequence of statements,
485 -- where the occurrence of the allocator is within the statements
486 -- and the parent is a task body
488 if Nkind (P) = N_Handled_Sequence_Of_Statements
489 and then Is_List_Member (C)
490 and then List_Containing (C) = Statements (P)
491 then
492 Onode := Original_Node (Parent (P));
494 -- Check for allocator within task body, this is a definite
495 -- violation of No_Allocators_After_Elaboration we can detect
496 -- at compile time.
498 if Nkind (Onode) = N_Task_Body then
499 Check_Restriction
500 (No_Standard_Allocators_After_Elaboration, N);
501 exit;
502 end if;
503 end if;
505 -- The other case is appearance in a subprogram body. This is
506 -- a violation if this is a library level subprogram with no
507 -- parameters. Note that this is now a static error even if the
508 -- subprogram is not the main program (this is a change, in an
509 -- earlier version only the main program was affected, and the
510 -- check had to be done in the binder).
512 if Nkind (P) = N_Subprogram_Body
513 and then Nkind (Parent (P)) = N_Compilation_Unit
514 and then No (Parameter_Specifications (Specification (P)))
515 then
516 Check_Restriction
517 (No_Standard_Allocators_After_Elaboration, N);
518 end if;
520 C := P;
521 P := Parent (C);
522 end loop;
523 end if;
525 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
526 -- any. The expected type for the name is any type. A non-overloading
527 -- rule then requires it to be of a type descended from
528 -- System.Storage_Pools.Subpools.Subpool_Handle.
530 -- This isn't exactly what the AI says, but it seems to be the right
531 -- rule. The AI should be fixed.???
533 declare
534 Subpool : constant Node_Id := Subpool_Handle_Name (N);
536 begin
537 if Present (Subpool) then
538 Analyze (Subpool);
540 if Is_Overloaded (Subpool) then
541 Error_Msg_N ("ambiguous subpool handle", Subpool);
542 end if;
544 -- Check that Etype (Subpool) is descended from Subpool_Handle
546 Resolve (Subpool);
547 end if;
548 end;
550 -- Analyze the qualified expression or subtype indication
552 if Nkind (E) = N_Qualified_Expression then
553 Acc_Type := Create_Itype (E_Allocator_Type, N);
554 Set_Etype (Acc_Type, Acc_Type);
555 Find_Type (Subtype_Mark (E));
557 -- Analyze the qualified expression, and apply the name resolution
558 -- rule given in 4.7(3).
560 Analyze (E);
561 Type_Id := Etype (E);
562 Set_Directly_Designated_Type (Acc_Type, Type_Id);
564 -- A qualified expression requires an exact match of the type,
565 -- class-wide matching is not allowed.
567 -- if Is_Class_Wide_Type (Type_Id)
568 -- and then Base_Type
569 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
570 -- then
571 -- Wrong_Type (Expression (E), Type_Id);
572 -- end if;
574 -- We don't analyze the qualified expression itself because it's
575 -- part of the allocator. It is fully analyzed and resolved when
576 -- the allocator is resolved with the context type.
578 Set_Etype (E, Type_Id);
580 -- Case where allocator has a subtype indication
582 else
583 -- If the allocator includes a N_Subtype_Indication then a
584 -- constraint is present, otherwise the node is a subtype mark.
585 -- Introduce an explicit subtype declaration into the tree
586 -- defining some anonymous subtype and rewrite the allocator to
587 -- use this subtype rather than the subtype indication.
589 -- It is important to introduce the explicit subtype declaration
590 -- so that the bounds of the subtype indication are attached to
591 -- the tree in case the allocator is inside a generic unit.
593 -- Finally, if there is no subtype indication and the type is
594 -- a tagged unconstrained type with discriminants, the designated
595 -- object is constrained by their default values, and it is
596 -- simplest to introduce an explicit constraint now. In some cases
597 -- this is done during expansion, but freeze actions are certain
598 -- to be emitted in the proper order if constraint is explicit.
600 if Is_Entity_Name (E) and then Expander_Active then
601 Find_Type (E);
602 Type_Id := Entity (E);
604 if Is_Tagged_Type (Type_Id)
605 and then Has_Defaulted_Discriminants (Type_Id)
606 and then not Is_Constrained (Type_Id)
607 then
608 declare
609 Constr : constant List_Id := New_List;
610 Loc : constant Source_Ptr := Sloc (E);
611 Discr : Entity_Id := First_Discriminant (Type_Id);
613 begin
614 while Present (Discr) loop
615 Append (Discriminant_Default_Value (Discr), Constr);
616 Next_Discriminant (Discr);
617 end loop;
619 Rewrite (E,
620 Make_Subtype_Indication (Loc,
621 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
622 Constraint =>
623 Make_Index_Or_Discriminant_Constraint (Loc,
624 Constraints => Constr)));
625 end;
626 end if;
627 end if;
629 if Nkind (E) = N_Subtype_Indication then
630 declare
631 Def_Id : Entity_Id;
632 Base_Typ : Entity_Id;
634 begin
635 -- A constraint is only allowed for a composite type in Ada
636 -- 95. In Ada 83, a constraint is also allowed for an
637 -- access-to-composite type, but the constraint is ignored.
639 Find_Type (Subtype_Mark (E));
640 Base_Typ := Entity (Subtype_Mark (E));
642 if Is_Elementary_Type (Base_Typ) then
643 if not (Ada_Version = Ada_83
644 and then Is_Access_Type (Base_Typ))
645 then
646 Error_Msg_N ("constraint not allowed here", E);
648 if Nkind (Constraint (E)) =
649 N_Index_Or_Discriminant_Constraint
650 then
651 Error_Msg_N -- CODEFIX
652 ("\if qualified expression was meant, " &
653 "use apostrophe", Constraint (E));
654 end if;
655 end if;
657 -- Get rid of the bogus constraint:
659 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
660 Analyze_Allocator (N);
661 return;
662 end if;
664 -- In GNATprove mode we need to preserve the link between
665 -- the original subtype indication and the anonymous subtype,
666 -- to extend proofs to constrained access types. We only do
667 -- that outside of spec expressions, otherwise the declaration
668 -- cannot be inserted and analyzed. In such a case, GNATprove
669 -- later rejects the allocator as it is not used here in
670 -- a non-interfering context (SPARK 4.8(2) and 7.1.3(10)).
672 if Expander_Active
673 or else (GNATprove_Mode and then not In_Spec_Expression)
674 then
675 Def_Id := Make_Temporary (Loc, 'S');
677 declare
678 Subtype_Decl : constant Node_Id :=
679 Make_Subtype_Declaration (Loc,
680 Defining_Identifier => Def_Id,
681 Subtype_Indication => Relocate_Node (E));
682 begin
683 Insert_Action (E, Subtype_Decl);
685 -- Handle unusual case where Insert_Action does not
686 -- analyze the declaration. Subtype_Decl must be
687 -- preanalyzed before call to Process_Subtype below.
688 Preanalyze (Subtype_Decl);
689 end;
691 if Sav_Errs /= Serious_Errors_Detected
692 and then Nkind (Constraint (E)) =
693 N_Index_Or_Discriminant_Constraint
694 then
695 Error_Msg_N -- CODEFIX
696 ("if qualified expression was meant, use apostrophe!",
697 Constraint (E));
698 end if;
700 E := New_Occurrence_Of (Def_Id, Loc);
701 Rewrite (Expression (N), E);
702 end if;
703 end;
704 end if;
706 Type_Id := Process_Subtype (E, N);
707 Acc_Type := Create_Itype (E_Allocator_Type, N);
708 Set_Etype (Acc_Type, Acc_Type);
709 Set_Directly_Designated_Type (Acc_Type, Type_Id);
710 Check_Fully_Declared (Type_Id, N);
712 -- Ada 2005 (AI-231): If the designated type is itself an access
713 -- type that excludes null, its default initialization will
714 -- be a null object, and we can insert an unconditional raise
715 -- before the allocator.
717 -- Ada 2012 (AI-104): A not null indication here is altogether
718 -- illegal.
720 if Can_Never_Be_Null (Type_Id) then
721 if Expander_Active then
722 Apply_Compile_Time_Constraint_Error
723 (N, "null value not allowed here??", CE_Null_Not_Allowed);
725 elsif Warn_On_Ada_2012_Compatibility then
726 Error_Msg_N
727 ("null value not allowed here in Ada 2012?y?", E);
728 end if;
729 end if;
731 -- Check for missing initialization. Skip this check if the allocator
732 -- is made for a special return object or if we already had errors on
733 -- analyzing the allocator since, in that case, these are very likely
734 -- cascaded errors.
736 if not Is_Definite_Subtype (Type_Id)
737 and then not For_Special_Return_Object (N)
738 and then Serious_Errors_Detected = Sav_Errs
739 then
740 if Is_Class_Wide_Type (Type_Id) then
741 Error_Msg_N
742 ("initialization required in class-wide allocation", N);
744 else
745 if Ada_Version < Ada_2005
746 and then Is_Limited_Type (Type_Id)
747 then
748 Error_Msg_N ("unconstrained allocation not allowed", N);
750 if Is_Array_Type (Type_Id) then
751 Error_Msg_N
752 ("\constraint with array bounds required", N);
754 elsif Has_Unknown_Discriminants (Type_Id) then
755 null;
757 else pragma Assert (Has_Discriminants (Type_Id));
758 Error_Msg_N
759 ("\constraint with discriminant values required", N);
760 end if;
762 -- Limited Ada 2005 and general nonlimited case.
763 -- This is an error, except in the case of an
764 -- uninitialized allocator that is generated
765 -- for a build-in-place function return of a
766 -- discriminated but compile-time-known-size
767 -- type.
769 else
770 if Is_Rewrite_Substitution (N)
771 and then Nkind (Original_Node (N)) = N_Allocator
772 then
773 declare
774 Qual : constant Node_Id :=
775 Expression (Original_Node (N));
776 pragma Assert
777 (Nkind (Qual) = N_Qualified_Expression);
778 Call : constant Node_Id := Expression (Qual);
779 pragma Assert
780 (Is_Expanded_Build_In_Place_Call (Call));
781 begin
782 null;
783 end;
785 else
786 Error_Msg_N
787 ("uninitialized unconstrained allocation not "
788 & "allowed", N);
790 if Is_Array_Type (Type_Id) then
791 Error_Msg_N
792 ("\qualified expression or constraint with "
793 & "array bounds required", N);
795 elsif Has_Unknown_Discriminants (Type_Id) then
796 Error_Msg_N ("\qualified expression required", N);
798 else pragma Assert (Has_Discriminants (Type_Id));
799 Error_Msg_N
800 ("\qualified expression or constraint with "
801 & "discriminant values required", N);
802 end if;
803 end if;
804 end if;
805 end if;
806 end if;
807 end if;
809 if Is_Abstract_Type (Type_Id) then
810 Error_Msg_N ("cannot allocate abstract object", E);
811 end if;
813 Set_Etype (N, Acc_Type);
815 -- If this is an allocator for the return stack, then no restriction may
816 -- be violated since it's just a low-level access to the primary stack.
818 if Nkind (Parent (N)) = N_Object_Declaration
819 and then Is_Entity_Name (Object_Definition (Parent (N)))
820 and then Is_Access_Type (Entity (Object_Definition (Parent (N))))
821 then
822 declare
823 Pool : constant Entity_Id :=
824 Associated_Storage_Pool
825 (Root_Type (Entity (Object_Definition (Parent (N)))));
827 begin
828 if Present (Pool) and then Is_RTE (Pool, RE_RS_Pool) then
829 goto Leave;
830 end if;
831 end;
832 end if;
834 if Has_Task (Designated_Type (Acc_Type)) then
835 Check_Restriction (No_Tasking, N);
836 Check_Restriction (Max_Tasks, N);
837 Check_Restriction (No_Task_Allocators, N);
838 end if;
840 -- Check restriction against dynamically allocated protected objects
842 if Has_Protected (Designated_Type (Acc_Type)) then
843 Check_Restriction (No_Protected_Type_Allocators, N);
844 end if;
846 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
847 -- type is nested, and the designated type needs finalization. The rule
848 -- is conservative in that class-wide types need finalization.
850 if Needs_Finalization (Designated_Type (Acc_Type))
851 and then not Is_Library_Level_Entity (Acc_Type)
852 then
853 Check_Restriction (No_Nested_Finalization, N);
854 end if;
856 -- Check that an allocator of a nested access type doesn't create a
857 -- protected object when restriction No_Local_Protected_Objects applies.
859 if Has_Protected (Designated_Type (Acc_Type))
860 and then not Is_Library_Level_Entity (Acc_Type)
861 then
862 Check_Restriction (No_Local_Protected_Objects, N);
863 end if;
865 -- Likewise for No_Local_Timing_Events
867 if Has_Timing_Event (Designated_Type (Acc_Type))
868 and then not Is_Library_Level_Entity (Acc_Type)
869 then
870 Check_Restriction (No_Local_Timing_Events, N);
871 end if;
873 -- If the No_Streams restriction is set, check that the type of the
874 -- object is not, and does not contain, any subtype derived from
875 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
876 -- Has_Stream just for efficiency reasons. There is no point in
877 -- spending time on a Has_Stream check if the restriction is not set.
879 if Restriction_Check_Required (No_Streams) then
880 if Has_Stream (Designated_Type (Acc_Type)) then
881 Check_Restriction (No_Streams, N);
882 end if;
883 end if;
885 if not Is_Library_Level_Entity (Acc_Type) then
886 Check_Restriction (No_Local_Allocators, N);
887 end if;
889 <<Leave>>
890 if Serious_Errors_Detected > Sav_Errs then
891 Set_Error_Posted (N);
892 Set_Etype (N, Any_Type);
893 end if;
894 end Analyze_Allocator;
896 ---------------------------
897 -- Analyze_Arithmetic_Op --
898 ---------------------------
900 procedure Analyze_Arithmetic_Op (N : Node_Id) is
901 L : constant Node_Id := Left_Opnd (N);
902 R : constant Node_Id := Right_Opnd (N);
904 Op_Id : Entity_Id;
906 begin
907 Set_Etype (N, Any_Type);
908 Candidate_Type := Empty;
910 Analyze_Expression (L);
911 Analyze_Expression (R);
913 -- If the entity is already set, the node is the instantiation of a
914 -- generic node with a non-local reference, or was manufactured by a
915 -- call to Make_Op_xxx. In either case the entity is known to be valid,
916 -- and we do not need to collect interpretations, instead we just get
917 -- the single possible interpretation.
919 if Present (Entity (N)) then
920 Op_Id := Entity (N);
922 if Ekind (Op_Id) = E_Operator then
923 Find_Arithmetic_Types (L, R, Op_Id, N);
924 else
925 Add_One_Interp (N, Op_Id, Etype (Op_Id));
926 end if;
928 -- Entity is not already set, so we do need to collect interpretations
930 else
931 Op_Id := Get_Name_Entity_Id (Chars (N));
932 while Present (Op_Id) loop
933 if Ekind (Op_Id) = E_Operator
934 and then Present (Next_Entity (First_Entity (Op_Id)))
935 then
936 Find_Arithmetic_Types (L, R, Op_Id, N);
938 -- The following may seem superfluous, because an operator cannot
939 -- be generic, but this ignores the cleverness of the author of
940 -- ACVC bc1013a.
942 elsif Is_Overloadable (Op_Id) then
943 Analyze_User_Defined_Binary_Op (N, Op_Id);
944 end if;
946 Op_Id := Homonym (Op_Id);
947 end loop;
948 end if;
950 Operator_Check (N);
951 Check_Function_Writable_Actuals (N);
952 end Analyze_Arithmetic_Op;
954 ------------------
955 -- Analyze_Call --
956 ------------------
958 -- Function, procedure, and entry calls are checked here. The Name in
959 -- the call may be overloaded. The actuals have been analyzed and may
960 -- themselves be overloaded. On exit from this procedure, the node N
961 -- may have zero, one or more interpretations. In the first case an
962 -- error message is produced. In the last case, the node is flagged
963 -- as overloaded and the interpretations are collected in All_Interp.
965 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
966 -- the type-checking is similar to that of other calls.
968 procedure Analyze_Call (N : Node_Id) is
969 Actuals : constant List_Id := Parameter_Associations (N);
970 Loc : constant Source_Ptr := Sloc (N);
971 Nam : Node_Id;
972 X : Interp_Index;
973 It : Interp;
974 Nam_Ent : Entity_Id := Empty;
975 Success : Boolean := False;
977 Deref : Boolean := False;
978 -- Flag indicates whether an interpretation of the prefix is a
979 -- parameterless call that returns an access_to_subprogram.
981 procedure Check_Writable_Actuals (N : Node_Id);
982 -- If the call has out or in-out parameters then mark its outermost
983 -- enclosing construct as a node on which the writable actuals check
984 -- must be performed.
986 function Name_Denotes_Function return Boolean;
987 -- If the type of the name is an access to subprogram, this may be the
988 -- type of a name, or the return type of the function being called. If
989 -- the name is not an entity then it can denote a protected function.
990 -- Until we distinguish Etype from Return_Type, we must use this routine
991 -- to resolve the meaning of the name in the call.
993 procedure No_Interpretation;
994 -- Output error message when no valid interpretation exists
996 ----------------------------
997 -- Check_Writable_Actuals --
998 ----------------------------
1000 -- The identification of conflicts in calls to functions with writable
1001 -- actuals is performed in the analysis phase of the front end to ensure
1002 -- that it reports exactly the same errors compiling with and without
1003 -- expansion enabled. It is performed in two stages:
1005 -- 1) When a call to a function with out-mode parameters is found,
1006 -- we climb to the outermost enclosing construct that can be
1007 -- evaluated in arbitrary order and we mark it with the flag
1008 -- Check_Actuals.
1010 -- 2) When the analysis of the marked node is complete, we traverse
1011 -- its decorated subtree searching for conflicts (see function
1012 -- Sem_Util.Check_Function_Writable_Actuals).
1014 -- The unique exception to this general rule is for aggregates, since
1015 -- their analysis is performed by the front end in the resolution
1016 -- phase. For aggregates we do not climb to their enclosing construct:
1017 -- we restrict the analysis to the subexpressions initializing the
1018 -- aggregate components.
1020 -- This implies that the analysis of expressions containing aggregates
1021 -- is not complete, since there may be conflicts on writable actuals
1022 -- involving subexpressions of the enclosing logical or arithmetic
1023 -- expressions. However, we cannot wait and perform the analysis when
1024 -- the whole subtree is resolved, since the subtrees may be transformed,
1025 -- thus adding extra complexity and computation cost to identify and
1026 -- report exactly the same errors compiling with and without expansion
1027 -- enabled.
1029 procedure Check_Writable_Actuals (N : Node_Id) is
1030 begin
1031 if Comes_From_Source (N)
1032 and then Present (Get_Subprogram_Entity (N))
1033 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1034 then
1035 -- For procedures and entries there is no need to climb since
1036 -- we only need to check if the actuals of this call invoke
1037 -- functions whose out-mode parameters overlap.
1039 if Nkind (N) /= N_Function_Call then
1040 Set_Check_Actuals (N);
1042 -- For calls to functions we climb to the outermost enclosing
1043 -- construct where the out-mode actuals of this function may
1044 -- introduce conflicts.
1046 else
1047 declare
1048 Outermost : Node_Id := Empty; -- init to avoid warning
1049 P : Node_Id := N;
1051 begin
1052 while Present (P) loop
1053 -- For object declarations we can climb to the node from
1054 -- its object definition branch or from its initializing
1055 -- expression. We prefer to mark the child node as the
1056 -- outermost construct to avoid adding further complexity
1057 -- to the routine that will later take care of
1058 -- performing the writable actuals check.
1060 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1061 and then Nkind (P) not in
1062 N_Assignment_Statement | N_Object_Declaration
1063 then
1064 Outermost := P;
1065 end if;
1067 -- Avoid climbing more than needed
1069 exit when Stop_Subtree_Climbing (Nkind (P))
1070 or else (Nkind (P) = N_Range
1071 and then
1072 Nkind (Parent (P)) not in N_In | N_Not_In);
1074 P := Parent (P);
1075 end loop;
1077 Set_Check_Actuals (Outermost);
1078 end;
1079 end if;
1080 end if;
1081 end Check_Writable_Actuals;
1083 ---------------------------
1084 -- Name_Denotes_Function --
1085 ---------------------------
1087 function Name_Denotes_Function return Boolean is
1088 begin
1089 if Is_Entity_Name (Nam) then
1090 return Ekind (Entity (Nam)) = E_Function;
1091 elsif Nkind (Nam) = N_Selected_Component then
1092 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1093 else
1094 return False;
1095 end if;
1096 end Name_Denotes_Function;
1098 -----------------------
1099 -- No_Interpretation --
1100 -----------------------
1102 procedure No_Interpretation is
1103 L : constant Boolean := Is_List_Member (N);
1104 K : constant Node_Kind := Nkind (Parent (N));
1106 begin
1107 -- If the node is in a list whose parent is not an expression then it
1108 -- must be an attempted procedure call.
1110 if L and then K not in N_Subexpr then
1111 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1112 Error_Msg_NE
1113 ("must instantiate generic procedure& before call",
1114 Nam, Entity (Nam));
1115 else
1116 Error_Msg_N ("procedure or entry name expected", Nam);
1117 end if;
1119 -- Check for tasking cases where only an entry call will do
1121 elsif not L
1122 and then K in N_Entry_Call_Alternative | N_Triggering_Alternative
1123 then
1124 Error_Msg_N ("entry name expected", Nam);
1126 -- Otherwise give general error message
1128 else
1129 Error_Msg_N ("invalid prefix in call", Nam);
1130 end if;
1131 end No_Interpretation;
1133 -- Start of processing for Analyze_Call
1135 begin
1136 -- Initialize the type of the result of the call to the error type,
1137 -- which will be reset if the type is successfully resolved.
1139 Set_Etype (N, Any_Type);
1141 Nam := Name (N);
1143 if not Is_Overloaded (Nam) then
1145 -- Only one interpretation to check
1147 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1148 Nam_Ent := Etype (Nam);
1150 -- If the prefix is an access_to_subprogram, this may be an indirect
1151 -- call. This is the case if the name in the call is not an entity
1152 -- name, or if it is a function name in the context of a procedure
1153 -- call. In this latter case, we have a call to a parameterless
1154 -- function that returns a pointer_to_procedure which is the entity
1155 -- being called. Finally, F (X) may be a call to a parameterless
1156 -- function that returns a pointer to a function with parameters.
1157 -- Note that if F returns an access-to-subprogram whose designated
1158 -- type is an array, F (X) cannot be interpreted as an indirect call
1159 -- through the result of the call to F.
1161 elsif Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
1162 and then
1163 (not Name_Denotes_Function
1164 or else Nkind (N) = N_Procedure_Call_Statement
1165 or else
1166 (Nkind (Parent (N)) /= N_Explicit_Dereference
1167 and then Is_Entity_Name (Nam)
1168 and then No (First_Formal (Entity (Nam)))
1169 and then not
1170 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1171 and then Present (Actuals)))
1172 then
1173 Nam_Ent := Designated_Type (Etype (Nam));
1174 Insert_Explicit_Dereference (Nam);
1176 -- Selected component case. Simple entry or protected operation,
1177 -- where the entry name is given by the selector name.
1179 elsif Nkind (Nam) = N_Selected_Component then
1180 Nam_Ent := Entity (Selector_Name (Nam));
1182 if Ekind (Nam_Ent) not in E_Entry
1183 | E_Entry_Family
1184 | E_Function
1185 | E_Procedure
1186 then
1187 Error_Msg_N ("name in call is not a callable entity", Nam);
1188 Set_Etype (N, Any_Type);
1189 return;
1190 end if;
1192 -- If the name is an Indexed component, it can be a call to a member
1193 -- of an entry family. The prefix must be a selected component whose
1194 -- selector is the entry. Analyze_Procedure_Call normalizes several
1195 -- kinds of call into this form.
1197 elsif Nkind (Nam) = N_Indexed_Component then
1198 if Nkind (Prefix (Nam)) = N_Selected_Component then
1199 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1200 else
1201 Error_Msg_N ("name in call is not a callable entity", Nam);
1202 Set_Etype (N, Any_Type);
1203 return;
1204 end if;
1206 elsif not Is_Entity_Name (Nam) then
1207 Error_Msg_N ("name in call is not a callable entity", Nam);
1208 Set_Etype (N, Any_Type);
1209 return;
1211 else
1212 Nam_Ent := Entity (Nam);
1214 -- If not overloadable, this may be a generalized indexing
1215 -- operation with named associations. Rewrite again as an
1216 -- indexed component and analyze as container indexing.
1218 if not Is_Overloadable (Nam_Ent) then
1219 if Present
1220 (Find_Value_Of_Aspect
1221 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1222 then
1223 Replace (N,
1224 Make_Indexed_Component (Sloc (N),
1225 Prefix => Nam,
1226 Expressions => Parameter_Associations (N)));
1228 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1229 return;
1230 else
1231 No_Interpretation;
1232 end if;
1234 else
1235 No_Interpretation;
1236 end if;
1238 return;
1239 end if;
1240 end if;
1242 -- Operations generated for RACW stub types are called only through
1243 -- dispatching, and can never be the static interpretation of a call.
1245 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1246 No_Interpretation;
1247 return;
1248 end if;
1250 Analyze_One_Call (N, Nam_Ent, True, Success);
1252 -- If the nonoverloaded interpretation is a call to an abstract
1253 -- nondispatching operation, then flag an error and return.
1255 if Is_Overloadable (Nam_Ent)
1256 and then Is_Abstract_Subprogram (Nam_Ent)
1257 and then not Is_Dispatching_Operation (Nam_Ent)
1258 then
1259 Nondispatching_Call_To_Abstract_Operation (N, Nam_Ent);
1260 return;
1261 end if;
1263 -- If this is an indirect call, the return type of the access_to
1264 -- subprogram may be an incomplete type. At the point of the call,
1265 -- use the full type if available, and at the same time update the
1266 -- return type of the access_to_subprogram.
1268 if Success
1269 and then Nkind (Nam) = N_Explicit_Dereference
1270 and then Ekind (Etype (N)) = E_Incomplete_Type
1271 and then Present (Full_View (Etype (N)))
1272 then
1273 Set_Etype (N, Full_View (Etype (N)));
1274 Set_Etype (Nam_Ent, Etype (N));
1275 end if;
1277 -- Overloaded call
1279 else
1280 -- An overloaded selected component must denote overloaded operations
1281 -- of a concurrent type. The interpretations are attached to the
1282 -- simple name of those operations.
1284 if Nkind (Nam) = N_Selected_Component then
1285 Nam := Selector_Name (Nam);
1286 end if;
1288 Get_First_Interp (Nam, X, It);
1289 while Present (It.Nam) loop
1290 Nam_Ent := It.Nam;
1291 Deref := False;
1293 -- Name may be call that returns an access to subprogram, or more
1294 -- generally an overloaded expression one of whose interpretations
1295 -- yields an access to subprogram. If the name is an entity, we do
1296 -- not dereference, because the node is a call that returns the
1297 -- access type: note difference between f(x), where the call may
1298 -- return an access subprogram type, and f(x)(y), where the type
1299 -- returned by the call to f is implicitly dereferenced to analyze
1300 -- the outer call.
1302 if Is_Access_Type (Nam_Ent) then
1303 Nam_Ent := Designated_Type (Nam_Ent);
1305 elsif Is_Access_Type (Etype (Nam_Ent))
1306 and then
1307 (not Is_Entity_Name (Nam)
1308 or else Nkind (N) = N_Procedure_Call_Statement)
1309 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1310 = E_Subprogram_Type
1311 then
1312 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1314 if Is_Entity_Name (Nam) then
1315 Deref := True;
1316 end if;
1317 end if;
1319 -- If the call has been rewritten from a prefixed call, the first
1320 -- parameter has been analyzed, but may need a subsequent
1321 -- dereference, so skip its analysis now.
1323 if Is_Rewrite_Substitution (N)
1324 and then Nkind (Original_Node (N)) = Nkind (N)
1325 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1326 and then Present (Parameter_Associations (N))
1327 and then Present (Etype (First (Parameter_Associations (N))))
1328 then
1329 Analyze_One_Call
1330 (N, Nam_Ent, False, Success, Skip_First => True);
1331 else
1332 Analyze_One_Call (N, Nam_Ent, False, Success);
1333 end if;
1335 -- If the interpretation succeeds, mark the proper type of the
1336 -- prefix (any valid candidate will do). If not, remove the
1337 -- candidate interpretation. If this is a parameterless call
1338 -- on an anonymous access to subprogram, X is a variable with
1339 -- an access discriminant D, the entity in the interpretation is
1340 -- D, so rewrite X as X.D.all.
1342 if Success then
1343 if Deref
1344 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1345 then
1346 if Ekind (It.Nam) = E_Discriminant
1347 and then Has_Implicit_Dereference (It.Nam)
1348 then
1349 Rewrite (Name (N),
1350 Make_Explicit_Dereference (Loc,
1351 Prefix =>
1352 Make_Selected_Component (Loc,
1353 Prefix =>
1354 New_Occurrence_Of (Entity (Nam), Loc),
1355 Selector_Name =>
1356 New_Occurrence_Of (It.Nam, Loc))));
1358 Analyze (N);
1359 return;
1361 else
1362 Set_Entity (Nam, It.Nam);
1363 Insert_Explicit_Dereference (Nam);
1364 Set_Etype (Nam, Nam_Ent);
1365 end if;
1367 else
1368 Set_Etype (Nam, It.Typ);
1369 end if;
1371 elsif Nkind (Name (N)) in N_Function_Call | N_Selected_Component
1372 then
1373 Remove_Interp (X);
1374 end if;
1376 Get_Next_Interp (X, It);
1377 end loop;
1379 -- If the name is the result of a function call, it can only be a
1380 -- call to a function returning an access to subprogram. Insert
1381 -- explicit dereference.
1383 if Nkind (Nam) = N_Function_Call then
1384 Insert_Explicit_Dereference (Nam);
1385 end if;
1387 if Etype (N) = Any_Type then
1389 -- None of the interpretations is compatible with the actuals
1391 Diagnose_Call (N, Nam);
1393 -- Special checks for uninstantiated put routines
1395 if Nkind (N) = N_Procedure_Call_Statement
1396 and then Is_Entity_Name (Nam)
1397 and then Chars (Nam) = Name_Put
1398 and then List_Length (Actuals) = 1
1399 then
1400 declare
1401 Arg : constant Node_Id := First (Actuals);
1402 Typ : Entity_Id;
1404 begin
1405 if Nkind (Arg) = N_Parameter_Association then
1406 Typ := Etype (Explicit_Actual_Parameter (Arg));
1407 else
1408 Typ := Etype (Arg);
1409 end if;
1411 if Is_Signed_Integer_Type (Typ) then
1412 Error_Msg_N
1413 ("possible missing instantiation of "
1414 & "'Text_'I'O.'Integer_'I'O!", Nam);
1416 elsif Is_Modular_Integer_Type (Typ) then
1417 Error_Msg_N
1418 ("possible missing instantiation of "
1419 & "'Text_'I'O.'Modular_'I'O!", Nam);
1421 elsif Is_Floating_Point_Type (Typ) then
1422 Error_Msg_N
1423 ("possible missing instantiation of "
1424 & "'Text_'I'O.'Float_'I'O!", Nam);
1426 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1427 Error_Msg_N
1428 ("possible missing instantiation of "
1429 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1431 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1432 Error_Msg_N
1433 ("possible missing instantiation of "
1434 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1436 elsif Is_Enumeration_Type (Typ) then
1437 Error_Msg_N
1438 ("possible missing instantiation of "
1439 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1440 end if;
1441 end;
1442 end if;
1444 elsif not Is_Overloaded (N)
1445 and then Is_Entity_Name (Nam)
1446 then
1447 -- Resolution yields a single interpretation. Verify that the
1448 -- reference has capitalization consistent with the declaration.
1450 Set_Entity_With_Checks (Nam, Entity (Nam));
1451 Generate_Reference (Entity (Nam), Nam);
1453 Set_Etype (Nam, Etype (Entity (Nam)));
1454 else
1455 Remove_Abstract_Operations (N);
1456 end if;
1457 end if;
1459 -- Check the accessibility level for actuals for explicitly aliased
1460 -- formals when a function call appears within a return statement.
1461 -- This is only checked if the enclosing subprogram Comes_From_Source,
1462 -- to avoid issuing errors on calls occurring in wrapper subprograms
1463 -- (for example, where the call is part of an expression of an aspect
1464 -- associated with a wrapper, such as Pre'Class).
1466 if Nkind (N) = N_Function_Call
1467 and then Comes_From_Source (N)
1468 and then Present (Nam_Ent)
1469 and then In_Return_Value (N)
1470 and then Comes_From_Source (Current_Subprogram)
1471 then
1472 declare
1473 Form : Node_Id;
1474 Act : Node_Id;
1475 begin
1476 Act := First_Actual (N);
1477 Form := First_Formal (Nam_Ent);
1479 while Present (Form) and then Present (Act) loop
1480 -- Check whether the formal is aliased and if the accessibility
1481 -- level of the actual is deeper than the accessibility level
1482 -- of the enclosing subprogram to which the current return
1483 -- statement applies.
1485 -- Should we be checking Is_Entity_Name on Act? Won't this miss
1486 -- other cases ???
1488 if Is_Explicitly_Aliased (Form)
1489 and then Is_Entity_Name (Act)
1490 and then Static_Accessibility_Level
1491 (Act, Zero_On_Dynamic_Level)
1492 > Subprogram_Access_Level (Current_Subprogram)
1493 then
1494 Error_Msg_N ("actual for explicitly aliased formal is too"
1495 & " short lived", Act);
1496 end if;
1498 Next_Formal (Form);
1499 Next_Actual (Act);
1500 end loop;
1501 end;
1502 end if;
1504 if Ada_Version >= Ada_2012 then
1506 -- Check if the call contains a function with writable actuals
1508 Check_Writable_Actuals (N);
1510 -- If found and the outermost construct that can be evaluated in
1511 -- an arbitrary order is precisely this call, then check all its
1512 -- actuals.
1514 Check_Function_Writable_Actuals (N);
1516 -- The return type of the function may be incomplete. This can be
1517 -- the case if the type is a generic formal, or a limited view. It
1518 -- can also happen when the function declaration appears before the
1519 -- full view of the type (which is legal in Ada 2012) and the call
1520 -- appears in a different unit, in which case the incomplete view
1521 -- must be replaced with the full view (or the nonlimited view)
1522 -- to prevent subsequent type errors. Note that the usual install/
1523 -- removal of limited_with clauses is not sufficient to handle this
1524 -- case, because the limited view may have been captured in another
1525 -- compilation unit that defines the current function.
1527 if Is_Incomplete_Type (Etype (N)) then
1528 if Present (Full_View (Etype (N))) then
1529 if Is_Entity_Name (Nam) then
1530 Set_Etype (Nam, Full_View (Etype (N)));
1531 Set_Etype (Entity (Nam), Full_View (Etype (N)));
1532 end if;
1534 Set_Etype (N, Full_View (Etype (N)));
1536 -- If the call is within a thunk, the nonlimited view should be
1537 -- analyzed eventually (see also Analyze_Return_Type).
1539 elsif From_Limited_With (Etype (N))
1540 and then Present (Non_Limited_View (Etype (N)))
1541 and then
1542 (Ekind (Non_Limited_View (Etype (N))) /= E_Incomplete_Type
1543 or else Is_Thunk (Current_Scope))
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 -- compare type if it is present, typically because it is a global type
2054 -- in a generic instance, or with the common type that must be present,
2055 -- or with the type of the formal of the user-defined function.
2057 if Present (Entity (N)) then
2058 Op_Id := Entity (N);
2060 if Ekind (Op_Id) = E_Operator then
2061 Add_One_Interp (N, Op_Id, Standard_Boolean);
2062 else
2063 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2064 end if;
2066 if Is_Overloaded (L) then
2067 if Ekind (Op_Id) = E_Operator then
2068 Set_Etype (L,
2069 (if Present (Compare_Type (N))
2070 then Compare_Type (N)
2071 else Intersect_Types (L, R)));
2072 else
2073 Set_Etype (L, Etype (First_Formal (Op_Id)));
2074 end if;
2075 end if;
2077 if Is_Overloaded (R) then
2078 if Ekind (Op_Id) = E_Operator then
2079 Set_Etype (R,
2080 (if Present (Compare_Type (N))
2081 then Compare_Type (N)
2082 else Intersect_Types (L, R)));
2083 else
2084 Set_Etype (R, Etype (Next_Formal (First_Formal (Op_Id))));
2085 end if;
2086 end if;
2088 else
2089 Op_Id := Get_Name_Entity_Id (Chars (N));
2091 while Present (Op_Id) loop
2092 if Ekind (Op_Id) = E_Operator then
2093 Find_Comparison_Equality_Types (L, R, Op_Id, N);
2094 else
2095 Analyze_User_Defined_Binary_Op (N, Op_Id);
2096 end if;
2098 Op_Id := Homonym (Op_Id);
2099 end loop;
2100 end if;
2102 -- If there was no match and the operator is inequality, this may be
2103 -- a case where inequality has not been made explicit, as for tagged
2104 -- types. Analyze the node as the negation of an equality operation.
2105 -- This cannot be done earlier because, before analysis, we cannot rule
2106 -- out the presence of an explicit inequality.
2108 if Etype (N) = Any_Type and then Nkind (N) = N_Op_Ne then
2109 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
2111 while Present (Op_Id) loop
2112 if Ekind (Op_Id) = E_Operator then
2113 Find_Comparison_Equality_Types (L, R, Op_Id, N);
2114 else
2115 Analyze_User_Defined_Binary_Op (N, Op_Id);
2116 end if;
2118 Op_Id := Homonym (Op_Id);
2119 end loop;
2121 if Etype (N) /= Any_Type then
2122 Op_Id := Entity (N);
2124 Rewrite (N,
2125 Make_Op_Not (Loc,
2126 Right_Opnd =>
2127 Make_Op_Eq (Loc,
2128 Left_Opnd => Left_Opnd (N),
2129 Right_Opnd => Right_Opnd (N))));
2131 Set_Entity (Right_Opnd (N), Op_Id);
2132 Analyze (N);
2133 end if;
2134 end if;
2136 Operator_Check (N);
2137 Check_Function_Writable_Actuals (N);
2138 end Analyze_Comparison_Equality_Op;
2140 ----------------------------------
2141 -- Analyze_Explicit_Dereference --
2142 ----------------------------------
2144 procedure Analyze_Explicit_Dereference (N : Node_Id) is
2145 Loc : constant Source_Ptr := Sloc (N);
2146 P : constant Node_Id := Prefix (N);
2147 T : Entity_Id;
2148 I : Interp_Index;
2149 It : Interp;
2150 New_N : Node_Id;
2152 function Is_Function_Type return Boolean;
2153 -- Check whether node may be interpreted as an implicit function call
2155 ----------------------
2156 -- Is_Function_Type --
2157 ----------------------
2159 function Is_Function_Type return Boolean is
2160 I : Interp_Index;
2161 It : Interp;
2163 begin
2164 if not Is_Overloaded (N) then
2165 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
2166 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
2168 else
2169 Get_First_Interp (N, I, It);
2170 while Present (It.Nam) loop
2171 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
2172 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
2173 then
2174 return False;
2175 end if;
2177 Get_Next_Interp (I, It);
2178 end loop;
2180 return True;
2181 end if;
2182 end Is_Function_Type;
2184 -- Start of processing for Analyze_Explicit_Dereference
2186 begin
2187 -- In formal verification mode, keep track of all reads and writes
2188 -- through explicit dereferences.
2190 if GNATprove_Mode then
2191 SPARK_Specific.Generate_Dereference (N);
2192 end if;
2194 Analyze (P);
2195 Set_Etype (N, Any_Type);
2197 -- Test for remote access to subprogram type, and if so return
2198 -- after rewriting the original tree.
2200 if Remote_AST_E_Dereference (P) then
2201 return;
2202 end if;
2204 -- Normal processing for other than remote access to subprogram type
2206 if not Is_Overloaded (P) then
2207 if Is_Access_Type (Etype (P)) then
2209 -- Set the Etype
2211 declare
2212 DT : constant Entity_Id := Designated_Type (Etype (P));
2214 begin
2215 -- An explicit dereference is a legal occurrence of an
2216 -- incomplete type imported through a limited_with clause, if
2217 -- the full view is visible, or if we are within an instance
2218 -- body, where the enclosing body has a regular with_clause
2219 -- on the unit.
2221 if From_Limited_With (DT)
2222 and then not From_Limited_With (Scope (DT))
2223 and then
2224 (Is_Immediately_Visible (Scope (DT))
2225 or else
2226 (Is_Child_Unit (Scope (DT))
2227 and then Is_Visible_Lib_Unit (Scope (DT)))
2228 or else In_Instance_Body)
2229 then
2230 Set_Etype (N, Available_View (DT));
2232 else
2233 Set_Etype (N, DT);
2234 end if;
2235 end;
2237 elsif Etype (P) /= Any_Type then
2238 Error_Msg_N ("prefix of dereference must be an access type", N);
2239 return;
2240 end if;
2242 else
2243 Get_First_Interp (P, I, It);
2244 while Present (It.Nam) loop
2245 T := It.Typ;
2247 if Is_Access_Type (T) then
2248 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2249 end if;
2251 Get_Next_Interp (I, It);
2252 end loop;
2254 -- Error if no interpretation of the prefix has an access type
2256 if Etype (N) = Any_Type then
2257 Error_Msg_N
2258 ("access type required in prefix of explicit dereference", P);
2259 Set_Etype (N, Any_Type);
2260 return;
2261 end if;
2262 end if;
2264 if Is_Function_Type
2265 and then Nkind (Parent (N)) /= N_Indexed_Component
2267 and then (Nkind (Parent (N)) /= N_Function_Call
2268 or else N /= Name (Parent (N)))
2270 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2271 or else N /= Name (Parent (N)))
2273 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2274 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2275 or else
2276 (Attribute_Name (Parent (N)) /= Name_Address
2277 and then
2278 Attribute_Name (Parent (N)) /= Name_Access))
2279 then
2280 -- Name is a function call with no actuals, in a context that
2281 -- requires deproceduring (including as an actual in an enclosing
2282 -- function or procedure call). There are some pathological cases
2283 -- where the prefix might include functions that return access to
2284 -- subprograms and others that return a regular type. Disambiguation
2285 -- of those has to take place in Resolve.
2287 New_N :=
2288 Make_Function_Call (Loc,
2289 Name => Make_Explicit_Dereference (Loc, P),
2290 Parameter_Associations => New_List);
2292 -- If the prefix is overloaded, remove operations that have formals,
2293 -- we know that this is a parameterless call.
2295 if Is_Overloaded (P) then
2296 Get_First_Interp (P, I, It);
2297 while Present (It.Nam) loop
2298 T := It.Typ;
2300 if Is_Access_Type (T)
2301 and then No (First_Formal (Base_Type (Designated_Type (T))))
2302 then
2303 Set_Etype (P, T);
2304 else
2305 Remove_Interp (I);
2306 end if;
2308 Get_Next_Interp (I, It);
2309 end loop;
2310 end if;
2312 Rewrite (N, New_N);
2313 Analyze (N);
2315 elsif not Is_Function_Type
2316 and then Is_Overloaded (N)
2317 then
2318 -- The prefix may include access to subprograms and other access
2319 -- types. If the context selects the interpretation that is a
2320 -- function call (not a procedure call) we cannot rewrite the node
2321 -- yet, but we include the result of the call interpretation.
2323 Get_First_Interp (N, I, It);
2324 while Present (It.Nam) loop
2325 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2326 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2327 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2328 then
2329 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2330 end if;
2332 Get_Next_Interp (I, It);
2333 end loop;
2334 end if;
2336 -- A value of remote access-to-class-wide must not be dereferenced
2337 -- (RM E.2.2(16)).
2339 Validate_Remote_Access_To_Class_Wide_Type (N);
2340 end Analyze_Explicit_Dereference;
2342 ------------------------
2343 -- Analyze_Expression --
2344 ------------------------
2346 procedure Analyze_Expression (N : Node_Id) is
2347 begin
2348 -- If the expression is an indexed component that will be rewritten
2349 -- as a container indexing, it has already been analyzed.
2351 if Nkind (N) = N_Indexed_Component
2352 and then Present (Generalized_Indexing (N))
2353 then
2354 null;
2356 else
2357 Analyze (N);
2358 Check_Parameterless_Call (N);
2359 end if;
2360 end Analyze_Expression;
2362 -------------------------------------
2363 -- Analyze_Expression_With_Actions --
2364 -------------------------------------
2366 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2368 procedure Check_Action_OK (A : Node_Id);
2369 -- Check that the action A is allowed as a declare_item of a declare
2370 -- expression if N and A come from source.
2372 ---------------------
2373 -- Check_Action_OK --
2374 ---------------------
2376 procedure Check_Action_OK (A : Node_Id) is
2377 begin
2378 if not Comes_From_Source (N) or else not Comes_From_Source (A) then
2380 -- If, for example, an (illegal) expression function is
2381 -- transformed into a "vanilla" function then we don't want to
2382 -- allow it just because Comes_From_Source is now False. So look
2383 -- at the Original_Node.
2385 if Is_Rewrite_Substitution (A) then
2386 Check_Action_OK (Original_Node (A));
2387 end if;
2389 return; -- Allow anything in generated code
2390 end if;
2392 case Nkind (A) is
2393 when N_Object_Declaration =>
2394 if Nkind (Object_Definition (A)) = N_Access_Definition then
2395 Error_Msg_N
2396 ("anonymous access type not allowed in declare_expression",
2397 Object_Definition (A));
2398 end if;
2400 if Aliased_Present (A) then
2401 Error_Msg_N ("ALIASED not allowed in declare_expression", A);
2402 end if;
2404 if Constant_Present (A)
2405 and then not Is_Limited_Type (Etype (Defining_Identifier (A)))
2406 then
2407 return; -- nonlimited constants are OK
2408 end if;
2410 when N_Object_Renaming_Declaration =>
2411 if Present (Access_Definition (A)) then
2412 Error_Msg_N
2413 ("anonymous access type not allowed in declare_expression",
2414 Access_Definition (A));
2415 end if;
2417 if not Is_Limited_Type (Etype (Defining_Identifier (A))) then
2418 return; -- ???For now; the RM rule is a bit more complicated
2419 end if;
2421 when N_Pragma =>
2422 declare
2423 -- See AI22-0045 pragma categorization.
2424 subtype Executable_Pragma_Id is Pragma_Id
2425 with Predicate => Executable_Pragma_Id in
2426 -- language-defined executable pragmas
2427 Pragma_Assert | Pragma_Inspection_Point
2429 -- GNAT-defined executable pragmas
2430 | Pragma_Assume | Pragma_Debug;
2431 begin
2432 if Get_Pragma_Id (A) in Executable_Pragma_Id then
2433 return;
2434 end if;
2435 end;
2437 when others =>
2438 null; -- Nothing else allowed
2439 end case;
2441 -- We could mention pragmas in the message text; let's not.
2442 Error_Msg_N ("object renaming or constant declaration expected", A);
2443 end Check_Action_OK;
2445 A : Node_Id;
2446 EWA_Scop : Entity_Id;
2448 -- Start of processing for Analyze_Expression_With_Actions
2450 begin
2451 -- Create a scope, which is needed to provide proper visibility of the
2452 -- declare_items.
2454 EWA_Scop := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
2455 Set_Etype (EWA_Scop, Standard_Void_Type);
2456 Set_Scope (EWA_Scop, Current_Scope);
2457 Set_Parent (EWA_Scop, N);
2458 Push_Scope (EWA_Scop);
2460 -- If this Expression_With_Actions node comes from source, then it
2461 -- represents a declare_expression; increment the counter to take note
2462 -- of that.
2464 if Comes_From_Source (N) then
2465 In_Declare_Expr := In_Declare_Expr + 1;
2466 end if;
2468 A := First (Actions (N));
2469 while Present (A) loop
2470 Analyze (A);
2471 Check_Action_OK (A);
2472 Next (A);
2473 end loop;
2475 Analyze_Expression (Expression (N));
2476 Set_Etype (N, Etype (Expression (N)));
2477 End_Scope;
2479 if Comes_From_Source (N) then
2480 In_Declare_Expr := In_Declare_Expr - 1;
2481 end if;
2482 end Analyze_Expression_With_Actions;
2484 ---------------------------
2485 -- Analyze_If_Expression --
2486 ---------------------------
2488 procedure Analyze_If_Expression (N : Node_Id) is
2489 Condition : constant Node_Id := First (Expressions (N));
2491 Then_Expr : Node_Id;
2492 Else_Expr : Node_Id;
2494 procedure Check_Else_Expression (T : Entity_Id);
2495 -- Check one interpretation of the THEN expression with type T
2497 procedure Check_Expression_Pair (T1, T2 : Entity_Id);
2498 -- Check THEN expression with type T1 and ELSE expression with type T2
2500 ---------------------------
2501 -- Check_Else_Expression --
2502 ---------------------------
2504 procedure Check_Else_Expression (T : Entity_Id) is
2505 I : Interp_Index;
2506 It : Interp;
2508 begin
2509 -- Loop through the interpretations of the ELSE expression
2511 if not Is_Overloaded (Else_Expr) then
2512 Check_Expression_Pair (T, Etype (Else_Expr));
2514 else
2515 Get_First_Interp (Else_Expr, I, It);
2516 while Present (It.Typ) loop
2517 Check_Expression_Pair (T, It.Typ);
2518 Get_Next_Interp (I, It);
2519 end loop;
2520 end if;
2521 end Check_Else_Expression;
2523 ---------------------------
2524 -- Check_Expression_Pair --
2525 ---------------------------
2527 procedure Check_Expression_Pair (T1, T2 : Entity_Id) is
2528 T : Entity_Id;
2530 begin
2531 if Covers (T1 => T1, T2 => T2)
2532 or else Covers (T1 => T2, T2 => T1)
2533 then
2534 T := Specific_Type (T1, T2);
2536 elsif Is_User_Defined_Literal (Then_Expr, T2) then
2537 T := T2;
2539 elsif Is_User_Defined_Literal (Else_Expr, T1) then
2540 T := T1;
2542 else
2543 T := Possible_Type_For_Conditional_Expression (T1, T2);
2545 if No (T) then
2546 return;
2547 end if;
2548 end if;
2550 Add_One_Interp (N, T, T);
2551 end Check_Expression_Pair;
2553 -- Local variables
2555 I : Interp_Index;
2556 It : Interp;
2558 -- Start of processing for Analyze_If_Expression
2560 begin
2561 -- Defend against error of missing expressions from previous error
2563 if No (Condition) then
2564 Check_Error_Detected;
2565 return;
2566 end if;
2568 Set_Etype (N, Any_Type);
2570 Then_Expr := Next (Condition);
2572 if No (Then_Expr) then
2573 Check_Error_Detected;
2574 return;
2575 end if;
2577 Else_Expr := Next (Then_Expr);
2579 -- Analyze and resolve the condition. We need to resolve this now so
2580 -- that it gets folded to True/False if possible, before we analyze
2581 -- the THEN/ELSE branches, because when analyzing these branches, we
2582 -- may call Is_Statically_Unevaluated, which expects the condition of
2583 -- an enclosing IF to have been analyze/resolved/evaluated.
2585 Analyze_Expression (Condition);
2586 Resolve (Condition, Any_Boolean);
2588 -- Analyze the THEN expression and (if present) the ELSE expression. For
2589 -- them we delay resolution in the normal manner because of overloading.
2591 Analyze_Expression (Then_Expr);
2593 if Present (Else_Expr) then
2594 Analyze_Expression (Else_Expr);
2595 end if;
2597 -- RM 4.5.7(10/3): If the if_expression is the operand of a type
2598 -- conversion, the type of the if_expression is the target type
2599 -- of the conversion.
2601 if Nkind (Parent (N)) = N_Type_Conversion then
2602 Set_Etype (N, Etype (Parent (N)));
2603 return;
2604 end if;
2606 -- Loop through the interpretations of the THEN expression and check the
2607 -- ELSE expression if present.
2609 if not Is_Overloaded (Then_Expr) then
2610 if Present (Else_Expr) then
2611 Check_Else_Expression (Etype (Then_Expr));
2612 else
2613 Set_Etype (N, Etype (Then_Expr));
2614 end if;
2616 else
2617 Get_First_Interp (Then_Expr, I, It);
2618 while Present (It.Typ) loop
2619 if Present (Else_Expr) then
2620 Check_Else_Expression (It.Typ);
2621 else
2622 Add_One_Interp (N, It.Typ, It.Typ);
2623 end if;
2625 Get_Next_Interp (I, It);
2626 end loop;
2627 end if;
2629 -- If no possible interpretation has been found, the type of the
2630 -- ELSE expression does not match any interpretation of the THEN
2631 -- expression.
2633 if Etype (N) = Any_Type then
2634 if Is_Overloaded (Then_Expr) then
2635 if Is_Overloaded (Else_Expr) then
2636 Error_Msg_N
2637 ("no interpretation compatible with those of THEN expression",
2638 Else_Expr);
2639 else
2640 Error_Msg_N
2641 ("type of ELSE incompatible with interpretations of THEN "
2642 & "expression",
2643 Else_Expr);
2644 Error_Msg_NE
2645 ("\ELSE expression has}!", Else_Expr, Etype (Else_Expr));
2646 end if;
2648 elsif Present (Else_Expr) then
2649 if Is_Overloaded (Else_Expr) then
2650 Error_Msg_N
2651 ("no interpretation compatible with type of THEN expression",
2652 Else_Expr);
2653 Error_Msg_NE
2654 ("\THEN expression has}!", Else_Expr, Etype (Then_Expr));
2655 else
2656 Error_Msg_N
2657 ("type of ELSE incompatible with that of THEN expression",
2658 Else_Expr);
2659 Error_Msg_NE
2660 ("\THEN expression has}!", Else_Expr, Etype (Then_Expr));
2661 Error_Msg_NE
2662 ("\ELSE expression has}!", Else_Expr, Etype (Else_Expr));
2663 end if;
2664 end if;
2665 end if;
2666 end Analyze_If_Expression;
2668 ------------------------------------
2669 -- Analyze_Indexed_Component_Form --
2670 ------------------------------------
2672 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2673 P : constant Node_Id := Prefix (N);
2674 Exprs : constant List_Id := Expressions (N);
2675 Exp : Node_Id;
2676 P_T : Entity_Id;
2677 E : Node_Id;
2678 U_N : Entity_Id;
2680 procedure Process_Function_Call;
2681 -- Prefix in indexed component form is an overloadable entity, so the
2682 -- node is very likely a function call; reformat it as such. The only
2683 -- exception is a call to a parameterless function that returns an
2684 -- array type, or an access type thereof, in which case this will be
2685 -- undone later by Resolve_Call or Resolve_Entry_Call.
2687 procedure Process_Indexed_Component;
2688 -- Prefix in indexed component form is actually an indexed component.
2689 -- This routine processes it, knowing that the prefix is already
2690 -- resolved.
2692 procedure Process_Indexed_Component_Or_Slice;
2693 -- An indexed component with a single index may designate a slice if
2694 -- the index is a subtype mark. This routine disambiguates these two
2695 -- cases by resolving the prefix to see if it is a subtype mark.
2697 procedure Process_Overloaded_Indexed_Component;
2698 -- If the prefix of an indexed component is overloaded, the proper
2699 -- interpretation is selected by the index types and the context.
2701 ---------------------------
2702 -- Process_Function_Call --
2703 ---------------------------
2705 procedure Process_Function_Call is
2706 Loc : constant Source_Ptr := Sloc (N);
2707 Actual : Node_Id;
2709 begin
2710 Change_Node (N, N_Function_Call);
2711 Set_Name (N, P);
2712 Set_Parameter_Associations (N, Exprs);
2714 -- Analyze actuals prior to analyzing the call itself
2716 Actual := First (Parameter_Associations (N));
2717 while Present (Actual) loop
2718 Analyze (Actual);
2719 Check_Parameterless_Call (Actual);
2721 -- Move to next actual. Note that we use Next, not Next_Actual
2722 -- here. The reason for this is a bit subtle. If a function call
2723 -- includes named associations, the parser recognizes the node
2724 -- as a call, and it is analyzed as such. If all associations are
2725 -- positional, the parser builds an indexed_component node, and
2726 -- it is only after analysis of the prefix that the construct
2727 -- is recognized as a call, in which case Process_Function_Call
2728 -- rewrites the node and analyzes the actuals. If the list of
2729 -- actuals is malformed, the parser may leave the node as an
2730 -- indexed component (despite the presence of named associations).
2731 -- The iterator Next_Actual is equivalent to Next if the list is
2732 -- positional, but follows the normalized chain of actuals when
2733 -- named associations are present. In this case normalization has
2734 -- not taken place, and actuals remain unanalyzed, which leads to
2735 -- subsequent crashes or loops if there is an attempt to continue
2736 -- analysis of the program.
2738 -- IF there is a single actual and it is a type name, the node
2739 -- can only be interpreted as a slice of a parameterless call.
2740 -- Rebuild the node as such and analyze.
2742 if No (Next (Actual))
2743 and then Is_Entity_Name (Actual)
2744 and then Is_Type (Entity (Actual))
2745 and then Is_Discrete_Type (Entity (Actual))
2746 and then not Is_Current_Instance (Actual)
2747 then
2748 Replace (N,
2749 Make_Slice (Loc,
2750 Prefix => P,
2751 Discrete_Range =>
2752 New_Occurrence_Of (Entity (Actual), Loc)));
2753 Analyze (N);
2754 return;
2756 else
2757 Next (Actual);
2758 end if;
2759 end loop;
2761 Analyze_Call (N);
2762 end Process_Function_Call;
2764 -------------------------------
2765 -- Process_Indexed_Component --
2766 -------------------------------
2768 procedure Process_Indexed_Component is
2769 Exp : Node_Id;
2770 Array_Type : Entity_Id;
2771 Index : Node_Id;
2772 Pent : Entity_Id := Empty;
2774 begin
2775 Exp := First (Exprs);
2777 if Is_Overloaded (P) then
2778 Process_Overloaded_Indexed_Component;
2780 else
2781 Array_Type := Etype (P);
2783 if Is_Entity_Name (P) then
2784 Pent := Entity (P);
2785 elsif Nkind (P) = N_Selected_Component
2786 and then Is_Entity_Name (Selector_Name (P))
2787 then
2788 Pent := Entity (Selector_Name (P));
2789 end if;
2791 -- Prefix must be appropriate for an array type, taking into
2792 -- account a possible implicit dereference.
2794 if Is_Access_Type (Array_Type) then
2795 Error_Msg_NW
2796 (Warn_On_Dereference, "?d?implicit dereference", N);
2797 Array_Type := Implicitly_Designated_Type (Array_Type);
2798 end if;
2800 if Is_Array_Type (Array_Type) then
2802 -- In order to correctly access First_Index component later,
2803 -- replace string literal subtype by its parent type.
2805 if Ekind (Array_Type) = E_String_Literal_Subtype then
2806 Array_Type := Etype (Array_Type);
2807 end if;
2809 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2810 Analyze (Exp);
2811 Set_Etype (N, Any_Type);
2813 if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then
2814 Error_Msg_N ("invalid index type in entry name", N);
2816 elsif Present (Next (Exp)) then
2817 Error_Msg_N ("too many subscripts in entry reference", N);
2819 else
2820 Set_Etype (N, Etype (P));
2821 end if;
2823 return;
2825 elsif Is_Record_Type (Array_Type)
2826 and then Remote_AST_I_Dereference (P)
2827 then
2828 return;
2830 elsif Try_Container_Indexing (N, P, Exprs) then
2831 return;
2833 elsif Array_Type = Any_Type then
2834 Set_Etype (N, Any_Type);
2836 -- In most cases the analysis of the prefix will have emitted
2837 -- an error already, but if the prefix may be interpreted as a
2838 -- call in prefixed notation, the report is left to the caller.
2839 -- To prevent cascaded errors, report only if no previous ones.
2841 if Serious_Errors_Detected = 0 then
2842 Error_Msg_N ("invalid prefix in indexed component", P);
2844 if Nkind (P) = N_Expanded_Name then
2845 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2846 end if;
2847 end if;
2849 return;
2851 -- Here we definitely have a bad indexing
2853 else
2854 if Nkind (Parent (N)) = N_Requeue_Statement
2855 and then Present (Pent) and then Ekind (Pent) = E_Entry
2856 then
2857 Error_Msg_N
2858 ("REQUEUE does not permit parameters", First (Exprs));
2860 elsif Is_Entity_Name (P)
2861 and then Etype (P) = Standard_Void_Type
2862 then
2863 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2865 else
2866 Error_Msg_N ("array type required in indexed component", P);
2867 end if;
2869 Set_Etype (N, Any_Type);
2870 return;
2871 end if;
2873 Index := First_Index (Array_Type);
2874 while Present (Index) and then Present (Exp) loop
2875 if not Has_Compatible_Type (Exp, Etype (Index)) then
2876 Wrong_Type (Exp, Etype (Index));
2877 Set_Etype (N, Any_Type);
2878 return;
2879 end if;
2881 Next_Index (Index);
2882 Next (Exp);
2883 end loop;
2885 Set_Etype (N, Component_Type (Array_Type));
2886 Check_Implicit_Dereference (N, Etype (N));
2888 if Present (Index) then
2889 Error_Msg_N
2890 ("too few subscripts in array reference", First (Exprs));
2892 elsif Present (Exp) then
2893 Error_Msg_N ("too many subscripts in array reference", Exp);
2894 end if;
2895 end if;
2896 end Process_Indexed_Component;
2898 ----------------------------------------
2899 -- Process_Indexed_Component_Or_Slice --
2900 ----------------------------------------
2902 procedure Process_Indexed_Component_Or_Slice is
2903 begin
2904 Exp := First (Exprs);
2905 while Present (Exp) loop
2906 Analyze_Expression (Exp);
2907 Next (Exp);
2908 end loop;
2910 Exp := First (Exprs);
2912 -- If one index is present, and it is a subtype name, then the node
2913 -- denotes a slice (note that the case of an explicit range for a
2914 -- slice was already built as an N_Slice node in the first place,
2915 -- so that case is not handled here).
2917 -- We use a replace rather than a rewrite here because this is one
2918 -- of the cases in which the tree built by the parser is plain wrong.
2920 if No (Next (Exp))
2921 and then Is_Entity_Name (Exp)
2922 and then Is_Type (Entity (Exp))
2923 then
2924 Replace (N,
2925 Make_Slice (Sloc (N),
2926 Prefix => P,
2927 Discrete_Range => New_Copy (Exp)));
2928 Analyze (N);
2930 -- Otherwise (more than one index present, or single index is not
2931 -- a subtype name), then we have the indexed component case.
2933 else
2934 Process_Indexed_Component;
2935 end if;
2936 end Process_Indexed_Component_Or_Slice;
2938 ------------------------------------------
2939 -- Process_Overloaded_Indexed_Component --
2940 ------------------------------------------
2942 procedure Process_Overloaded_Indexed_Component is
2943 Exp : Node_Id;
2944 I : Interp_Index;
2945 It : Interp;
2946 Typ : Entity_Id;
2947 Index : Node_Id;
2948 Found : Boolean;
2950 begin
2951 Set_Etype (N, Any_Type);
2953 Get_First_Interp (P, I, It);
2954 while Present (It.Nam) loop
2955 Typ := It.Typ;
2957 if Is_Access_Type (Typ) then
2958 Typ := Designated_Type (Typ);
2959 Error_Msg_NW
2960 (Warn_On_Dereference, "?d?implicit dereference", N);
2961 end if;
2963 if Is_Array_Type (Typ) then
2965 -- Got a candidate: verify that index types are compatible
2967 Index := First_Index (Typ);
2968 Found := True;
2969 Exp := First (Exprs);
2970 while Present (Index) and then Present (Exp) loop
2971 if Has_Compatible_Type (Exp, Etype (Index)) then
2972 null;
2973 else
2974 Found := False;
2975 Remove_Interp (I);
2976 exit;
2977 end if;
2979 Next_Index (Index);
2980 Next (Exp);
2981 end loop;
2983 if Found and then No (Index) and then No (Exp) then
2984 declare
2985 CT : constant Entity_Id :=
2986 Base_Type (Component_Type (Typ));
2987 begin
2988 Add_One_Interp (N, CT, CT);
2989 Check_Implicit_Dereference (N, CT);
2990 end;
2991 end if;
2993 elsif Try_Container_Indexing (N, P, Exprs) then
2994 return;
2996 end if;
2998 Get_Next_Interp (I, It);
2999 end loop;
3001 if Etype (N) = Any_Type then
3002 Error_Msg_N ("no legal interpretation for indexed component", N);
3003 Set_Is_Overloaded (N, False);
3004 end if;
3005 end Process_Overloaded_Indexed_Component;
3007 -- Start of processing for Analyze_Indexed_Component_Form
3009 begin
3010 -- Get name of array, function or type
3012 Analyze (P);
3014 -- If P is an explicit dereference whose prefix is of a remote access-
3015 -- to-subprogram type, then N has already been rewritten as a subprogram
3016 -- call and analyzed.
3018 if Nkind (N) in N_Subprogram_Call then
3019 return;
3021 -- When the prefix is attribute 'Loop_Entry and the sole expression of
3022 -- the indexed component denotes a loop name, the indexed form is turned
3023 -- into an attribute reference.
3025 elsif Nkind (N) = N_Attribute_Reference
3026 and then Attribute_Name (N) = Name_Loop_Entry
3027 then
3028 return;
3029 end if;
3031 pragma Assert (Nkind (N) = N_Indexed_Component);
3033 P_T := Base_Type (Etype (P));
3035 if Is_Entity_Name (P) and then Present (Entity (P)) then
3036 U_N := Entity (P);
3038 if Is_Type (U_N) then
3040 -- Reformat node as a type conversion
3042 E := Remove_Head (Exprs);
3044 if Present (First (Exprs)) then
3045 Error_Msg_N
3046 ("argument of type conversion must be single expression", N);
3047 end if;
3049 Change_Node (N, N_Type_Conversion);
3050 Set_Subtype_Mark (N, P);
3051 Set_Etype (N, U_N);
3052 Set_Expression (N, E);
3054 -- After changing the node, call for the specific Analysis
3055 -- routine directly, to avoid a double call to the expander.
3057 Analyze_Type_Conversion (N);
3058 return;
3059 end if;
3061 if Is_Overloadable (U_N) then
3062 Process_Function_Call;
3064 elsif Ekind (Etype (P)) = E_Subprogram_Type
3065 or else (Is_Access_Type (Etype (P))
3066 and then
3067 Ekind (Designated_Type (Etype (P))) =
3068 E_Subprogram_Type)
3069 then
3070 -- Call to access_to-subprogram with possible implicit dereference
3072 Process_Function_Call;
3074 elsif Is_Generic_Subprogram (U_N) then
3076 -- A common beginner's (or C++ templates fan) error
3078 Error_Msg_N ("generic subprogram cannot be called", N);
3079 Set_Etype (N, Any_Type);
3080 return;
3082 else
3083 Process_Indexed_Component_Or_Slice;
3084 end if;
3086 -- If not an entity name, prefix is an expression that may denote
3087 -- an array or an access-to-subprogram.
3089 else
3090 if Ekind (P_T) = E_Subprogram_Type
3091 or else (Is_Access_Type (P_T)
3092 and then
3093 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
3094 then
3095 Process_Function_Call;
3097 elsif Nkind (P) = N_Selected_Component
3098 and then Present (Entity (Selector_Name (P)))
3099 and then Is_Overloadable (Entity (Selector_Name (P)))
3100 then
3101 Process_Function_Call;
3102 else
3103 -- Indexed component, slice, or a call to a member of a family
3104 -- entry, which will be converted to an entry call later.
3106 Process_Indexed_Component_Or_Slice;
3107 end if;
3108 end if;
3110 Analyze_Dimension (N);
3111 end Analyze_Indexed_Component_Form;
3113 ------------------------
3114 -- Analyze_Logical_Op --
3115 ------------------------
3117 procedure Analyze_Logical_Op (N : Node_Id) is
3118 L : constant Node_Id := Left_Opnd (N);
3119 R : constant Node_Id := Right_Opnd (N);
3121 Op_Id : Entity_Id;
3123 begin
3124 Set_Etype (N, Any_Type);
3125 Candidate_Type := Empty;
3127 Analyze_Expression (L);
3128 Analyze_Expression (R);
3130 -- If the entity is already set, the node is the instantiation of a
3131 -- generic node with a non-local reference, or was manufactured by a
3132 -- call to Make_Op_xxx. In either case the entity is known to be valid,
3133 -- and we do not need to collect interpretations, instead we just get
3134 -- the single possible interpretation.
3136 if Present (Entity (N)) then
3137 Op_Id := Entity (N);
3139 if Ekind (Op_Id) = E_Operator then
3140 Find_Boolean_Types (L, R, Op_Id, N);
3141 else
3142 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3143 end if;
3145 -- Entity is not already set, so we do need to collect interpretations
3147 else
3148 Op_Id := Get_Name_Entity_Id (Chars (N));
3149 while Present (Op_Id) loop
3150 if Ekind (Op_Id) = E_Operator then
3151 Find_Boolean_Types (L, R, Op_Id, N);
3152 else
3153 Analyze_User_Defined_Binary_Op (N, Op_Id);
3154 end if;
3156 Op_Id := Homonym (Op_Id);
3157 end loop;
3158 end if;
3160 Operator_Check (N);
3161 Check_Function_Writable_Actuals (N);
3163 if Style_Check then
3164 if Nkind (L) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
3165 and then Is_Boolean_Type (Etype (L))
3166 then
3167 Check_Xtra_Parens_Precedence (L);
3168 end if;
3170 if Nkind (R) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
3171 and then Is_Boolean_Type (Etype (R))
3172 then
3173 Check_Xtra_Parens_Precedence (R);
3174 end if;
3175 end if;
3176 end Analyze_Logical_Op;
3178 ---------------------------
3179 -- Analyze_Membership_Op --
3180 ---------------------------
3182 procedure Analyze_Membership_Op (N : Node_Id) is
3183 Loc : constant Source_Ptr := Sloc (N);
3184 L : constant Node_Id := Left_Opnd (N);
3185 R : constant Node_Id := Right_Opnd (N);
3187 procedure Analyze_Set_Membership;
3188 -- If a set of alternatives is present, analyze each and find the
3189 -- common type to which they must all resolve.
3191 function Find_Interp return Boolean;
3192 -- Find a valid interpretation of the test. Note that the context of the
3193 -- operation plays no role in resolving the operands, so that if there
3194 -- is more than one interpretation of the operands that is compatible
3195 -- with the test, the operation is ambiguous.
3197 function Try_Left_Interp (T : Entity_Id) return Boolean;
3198 -- Try an interpretation of the left operand with type T. Return true if
3199 -- one interpretation (at least) of the right operand making up a valid
3200 -- operand pair exists, otherwise false if no such pair exists.
3202 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean;
3203 -- Return true if T1 and T2 constitute a valid pair of operand types for
3204 -- L and R respectively.
3206 ----------------------------
3207 -- Analyze_Set_Membership --
3208 ----------------------------
3210 procedure Analyze_Set_Membership is
3211 Alt : Node_Id;
3212 Index : Interp_Index;
3213 It : Interp;
3214 Candidate_Interps : Node_Id;
3215 Common_Type : Entity_Id := Empty;
3217 begin
3218 Analyze (L);
3219 Candidate_Interps := L;
3221 if not Is_Overloaded (L) then
3222 Common_Type := Etype (L);
3224 Alt := First (Alternatives (N));
3225 while Present (Alt) loop
3226 Analyze (Alt);
3228 if not Has_Compatible_Type (Alt, Common_Type) then
3229 Wrong_Type (Alt, Common_Type);
3230 end if;
3232 Next (Alt);
3233 end loop;
3235 else
3236 Alt := First (Alternatives (N));
3237 while Present (Alt) loop
3238 Analyze (Alt);
3239 if not Is_Overloaded (Alt) then
3240 Common_Type := Etype (Alt);
3242 else
3243 Get_First_Interp (Alt, Index, It);
3244 while Present (It.Typ) loop
3245 if not
3246 Has_Compatible_Type (Candidate_Interps, It.Typ)
3247 then
3248 Remove_Interp (Index);
3249 end if;
3251 Get_Next_Interp (Index, It);
3252 end loop;
3254 Get_First_Interp (Alt, Index, It);
3256 if No (It.Typ) then
3257 Error_Msg_N ("alternative has no legal type", Alt);
3258 return;
3259 end if;
3261 -- If alternative is not overloaded, we have a unique type
3262 -- for all of them.
3264 Set_Etype (Alt, It.Typ);
3266 -- If the alternative is an enumeration literal, use the one
3267 -- for this interpretation.
3269 if Is_Entity_Name (Alt) then
3270 Set_Entity (Alt, It.Nam);
3271 end if;
3273 Get_Next_Interp (Index, It);
3275 if No (It.Typ) then
3276 Set_Is_Overloaded (Alt, False);
3277 Common_Type := Etype (Alt);
3278 end if;
3280 Candidate_Interps := Alt;
3281 end if;
3283 Next (Alt);
3284 end loop;
3285 end if;
3287 if Present (Common_Type) then
3288 Set_Etype (L, Common_Type);
3290 -- The left operand may still be overloaded, to be resolved using
3291 -- the Common_Type.
3293 else
3294 Error_Msg_N ("cannot resolve membership operation", N);
3295 end if;
3296 end Analyze_Set_Membership;
3298 -----------------
3299 -- Find_Interp --
3300 -----------------
3302 function Find_Interp return Boolean is
3303 Found : Boolean;
3304 I : Interp_Index;
3305 It : Interp;
3306 L_Typ : Entity_Id;
3307 Valid_I : Interp_Index;
3309 begin
3310 -- Loop through the interpretations of the left operand
3312 if not Is_Overloaded (L) then
3313 Found := Try_Left_Interp (Etype (L));
3315 else
3316 Found := False;
3317 L_Typ := Empty;
3318 Valid_I := 0;
3320 Get_First_Interp (L, I, It);
3321 while Present (It.Typ) loop
3322 if Try_Left_Interp (It.Typ) then
3323 -- If several interpretations are possible, disambiguate
3325 if Present (L_Typ)
3326 and then Base_Type (It.Typ) /= Base_Type (L_Typ)
3327 then
3328 It := Disambiguate (L, Valid_I, I, Any_Type);
3330 if It = No_Interp then
3331 Ambiguous_Operands (N);
3332 Set_Etype (L, Any_Type);
3333 return True;
3334 end if;
3336 else
3337 Valid_I := I;
3338 end if;
3340 L_Typ := It.Typ;
3341 Set_Etype (L, L_Typ);
3342 Found := True;
3343 end if;
3345 Get_Next_Interp (I, It);
3346 end loop;
3347 end if;
3349 return Found;
3350 end Find_Interp;
3352 ---------------------
3353 -- Try_Left_Interp --
3354 ---------------------
3356 function Try_Left_Interp (T : Entity_Id) return Boolean is
3357 Found : Boolean;
3358 I : Interp_Index;
3359 It : Interp;
3360 R_Typ : Entity_Id;
3361 Valid_I : Interp_Index;
3363 begin
3364 -- Defend against previous error
3366 if Nkind (R) = N_Error then
3367 Found := False;
3369 -- Loop through the interpretations of the right operand
3371 elsif not Is_Overloaded (R) then
3372 Found := Is_Valid_Pair (T, Etype (R));
3374 else
3375 Found := False;
3376 R_Typ := Empty;
3377 Valid_I := 0;
3379 Get_First_Interp (R, I, It);
3380 while Present (It.Typ) loop
3381 if Is_Valid_Pair (T, It.Typ) then
3382 -- If several interpretations are possible, disambiguate
3384 if Present (R_Typ)
3385 and then Base_Type (It.Typ) /= Base_Type (R_Typ)
3386 then
3387 It := Disambiguate (R, Valid_I, I, Any_Type);
3389 if It = No_Interp then
3390 Ambiguous_Operands (N);
3391 Set_Etype (R, Any_Type);
3392 return True;
3393 end if;
3395 else
3396 Valid_I := I;
3397 end if;
3399 R_Typ := It.Typ;
3400 Found := True;
3401 end if;
3403 Get_Next_Interp (I, It);
3404 end loop;
3405 end if;
3407 return Found;
3408 end Try_Left_Interp;
3410 -------------------
3411 -- Is_Valid_Pair --
3412 -------------------
3414 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean is
3415 begin
3416 return Covers (T1 => T1, T2 => T2)
3417 or else Covers (T1 => T2, T2 => T1)
3418 or else Is_User_Defined_Literal (L, T2)
3419 or else Is_User_Defined_Literal (R, T1);
3420 end Is_Valid_Pair;
3422 -- Local variables
3424 Dummy : Boolean;
3425 Op : Node_Id;
3427 -- Start of processing for Analyze_Membership_Op
3429 begin
3430 Analyze_Expression (L);
3432 if No (R) then
3433 pragma Assert (Ada_Version >= Ada_2012);
3435 Analyze_Set_Membership;
3437 declare
3438 Alt : Node_Id;
3439 begin
3440 Alt := First (Alternatives (N));
3441 while Present (Alt) loop
3442 if Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)) then
3443 Check_Fully_Declared (Entity (Alt), Alt);
3445 if Has_Ghost_Predicate_Aspect (Entity (Alt)) then
3446 Error_Msg_NE
3447 ("subtype& has ghost predicate, "
3448 & "not allowed in membership test",
3449 Alt, Entity (Alt));
3450 end if;
3451 end if;
3453 Next (Alt);
3454 end loop;
3455 end;
3457 elsif Nkind (R) = N_Range
3458 or else (Nkind (R) = N_Attribute_Reference
3459 and then Attribute_Name (R) = Name_Range)
3460 then
3461 Analyze_Expression (R);
3463 Dummy := Find_Interp;
3465 -- If not a range, it can be a subtype mark, or else it is a degenerate
3466 -- membership test with a singleton value, i.e. a test for equality,
3467 -- if the types are compatible.
3469 else
3470 Analyze_Expression (R);
3472 if Is_Entity_Name (R) and then Is_Type (Entity (R)) then
3473 Find_Type (R);
3474 Check_Fully_Declared (Entity (R), R);
3476 if Has_Ghost_Predicate_Aspect (Entity (R)) then
3477 Error_Msg_NE
3478 ("subtype& has ghost predicate, "
3479 & "not allowed in membership test",
3480 R, Entity (R));
3481 end if;
3483 elsif Ada_Version >= Ada_2012 and then Find_Interp then
3484 Op := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
3485 Resolve_Membership_Equality (Op, Etype (L));
3487 if Nkind (N) = N_Not_In then
3488 Op := Make_Op_Not (Loc, Op);
3489 end if;
3491 Rewrite (N, Op);
3492 Analyze (N);
3493 return;
3495 else
3496 -- In all versions of the language, if we reach this point there
3497 -- is a previous error that will be diagnosed below.
3499 Find_Type (R);
3500 end if;
3501 end if;
3503 -- Compatibility between expression and subtype mark or range is
3504 -- checked during resolution. The result of the operation is Boolean
3505 -- in any case.
3507 Set_Etype (N, Standard_Boolean);
3509 if Comes_From_Source (N)
3510 and then Present (Right_Opnd (N))
3511 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3512 then
3513 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3514 end if;
3516 Check_Function_Writable_Actuals (N);
3517 end Analyze_Membership_Op;
3519 -----------------
3520 -- Analyze_Mod --
3521 -----------------
3523 procedure Analyze_Mod (N : Node_Id) is
3524 begin
3525 -- A special warning check, if we have an expression of the form:
3526 -- expr mod 2 * literal
3527 -- where literal is 128 or less, then probably what was meant was
3528 -- expr mod 2 ** literal
3529 -- so issue an appropriate warning.
3531 if Warn_On_Suspicious_Modulus_Value
3532 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3533 and then Intval (Right_Opnd (N)) = Uint_2
3534 and then Nkind (Parent (N)) = N_Op_Multiply
3535 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3536 and then Intval (Right_Opnd (Parent (N))) <= Uint_128
3537 then
3538 Error_Msg_N
3539 ("suspicious MOD value, was '*'* intended'??.m?", Parent (N));
3540 end if;
3542 -- Remaining processing is same as for other arithmetic operators
3544 Analyze_Arithmetic_Op (N);
3545 end Analyze_Mod;
3547 ----------------------
3548 -- Analyze_Negation --
3549 ----------------------
3551 procedure Analyze_Negation (N : Node_Id) is
3552 R : constant Node_Id := Right_Opnd (N);
3554 Op_Id : Entity_Id;
3556 begin
3557 Set_Etype (N, Any_Type);
3558 Candidate_Type := Empty;
3560 Analyze_Expression (R);
3562 -- If the entity is already set, the node is the instantiation of a
3563 -- generic node with a non-local reference, or was manufactured by a
3564 -- call to Make_Op_xxx. In either case the entity is known to be valid,
3565 -- and we do not need to collect interpretations, instead we just get
3566 -- the single possible interpretation.
3568 if Present (Entity (N)) then
3569 Op_Id := Entity (N);
3571 if Ekind (Op_Id) = E_Operator then
3572 Find_Negation_Types (R, Op_Id, N);
3573 else
3574 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3575 end if;
3577 else
3578 Op_Id := Get_Name_Entity_Id (Chars (N));
3579 while Present (Op_Id) loop
3580 if Ekind (Op_Id) = E_Operator then
3581 Find_Negation_Types (R, Op_Id, N);
3582 else
3583 Analyze_User_Defined_Unary_Op (N, Op_Id);
3584 end if;
3586 Op_Id := Homonym (Op_Id);
3587 end loop;
3588 end if;
3590 Operator_Check (N);
3591 end Analyze_Negation;
3593 ------------------
3594 -- Analyze_Null --
3595 ------------------
3597 procedure Analyze_Null (N : Node_Id) is
3598 begin
3599 Set_Etype (N, Universal_Access);
3600 end Analyze_Null;
3602 ----------------------
3603 -- Analyze_One_Call --
3604 ----------------------
3606 procedure Analyze_One_Call
3607 (N : Node_Id;
3608 Nam : Entity_Id;
3609 Report : Boolean;
3610 Success : out Boolean;
3611 Skip_First : Boolean := False)
3613 Actuals : constant List_Id := Parameter_Associations (N);
3614 Prev_T : constant Entity_Id := Etype (N);
3616 -- Recognize cases of prefixed calls that have been rewritten in
3617 -- various ways. The simplest case is a rewritten selected component,
3618 -- but it can also be an already-examined indexed component, or a
3619 -- prefix that is itself a rewritten prefixed call that is in turn
3620 -- an indexed call (the syntactic ambiguity involving the indexing of
3621 -- a function with defaulted parameters that returns an array).
3622 -- A flag Maybe_Indexed_Call might be useful here ???
3624 Must_Skip : constant Boolean := Skip_First
3625 or else Nkind (Original_Node (N)) = N_Selected_Component
3626 or else
3627 (Nkind (Original_Node (N)) = N_Indexed_Component
3628 and then Nkind (Prefix (Original_Node (N))) =
3629 N_Selected_Component)
3630 or else
3631 (Nkind (Parent (N)) = N_Function_Call
3632 and then Is_Array_Type (Etype (Name (N)))
3633 and then Etype (Original_Node (N)) =
3634 Component_Type (Etype (Name (N)))
3635 and then Nkind (Original_Node (Parent (N))) =
3636 N_Selected_Component);
3638 -- The first formal must be omitted from the match when trying to find
3639 -- a primitive operation that is a possible interpretation, and also
3640 -- after the call has been rewritten, because the corresponding actual
3641 -- is already known to be compatible, and because this may be an
3642 -- indexing of a call with default parameters.
3644 First_Form : Entity_Id;
3645 Formal : Entity_Id;
3646 Actual : Node_Id;
3647 Is_Indexed : Boolean := False;
3648 Is_Indirect : Boolean := False;
3649 Subp_Type : constant Entity_Id := Etype (Nam);
3650 Norm_OK : Boolean;
3652 function Compatible_Types_In_Predicate
3653 (T1 : Entity_Id;
3654 T2 : Entity_Id) return Boolean;
3655 -- For an Ada 2012 predicate or invariant, a call may mention an
3656 -- incomplete type, while resolution of the corresponding predicate
3657 -- function may see the full view, as a consequence of the delayed
3658 -- resolution of the corresponding expressions. This may occur in
3659 -- the body of a predicate function, or in a call to such. Anomalies
3660 -- involving private and full views can also happen. In each case,
3661 -- rewrite node or add conversions to remove spurious type errors.
3663 procedure Indicate_Name_And_Type;
3664 -- If candidate interpretation matches, indicate name and type of result
3665 -- on call node.
3667 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3668 -- There may be a user-defined operator that hides the current
3669 -- interpretation. We must check for this independently of the
3670 -- analysis of the call with the user-defined operation, because
3671 -- the parameter names may be wrong and yet the hiding takes place.
3672 -- This fixes a problem with ACATS test B34014O.
3674 -- When the type Address is a visible integer type, and the DEC
3675 -- system extension is visible, the predefined operator may be
3676 -- hidden as well, by one of the address operations in auxdec.
3677 -- Finally, the abstract operations on address do not hide the
3678 -- predefined operator (this is the purpose of making them abstract).
3680 -----------------------------------
3681 -- Compatible_Types_In_Predicate --
3682 -----------------------------------
3684 function Compatible_Types_In_Predicate
3685 (T1 : Entity_Id;
3686 T2 : Entity_Id) return Boolean
3688 function Common_Type (T : Entity_Id) return Entity_Id;
3689 -- Find non-private underlying full view if any, without going to
3690 -- ancestor type (as opposed to Underlying_Type).
3692 -----------------
3693 -- Common_Type --
3694 -----------------
3696 function Common_Type (T : Entity_Id) return Entity_Id is
3697 CT : Entity_Id;
3699 begin
3700 CT := T;
3702 if Is_Private_Type (CT) and then Present (Full_View (CT)) then
3703 CT := Full_View (CT);
3704 end if;
3706 if Is_Private_Type (CT)
3707 and then Present (Underlying_Full_View (CT))
3708 then
3709 CT := Underlying_Full_View (CT);
3710 end if;
3712 return Base_Type (CT);
3713 end Common_Type;
3715 -- Start of processing for Compatible_Types_In_Predicate
3717 begin
3718 if (Ekind (Current_Scope) = E_Function
3719 and then Is_Predicate_Function (Current_Scope))
3720 or else
3721 (Ekind (Nam) = E_Function
3722 and then Is_Predicate_Function (Nam))
3723 then
3724 if Is_Incomplete_Type (T1)
3725 and then Present (Full_View (T1))
3726 and then Full_View (T1) = T2
3727 then
3728 Set_Etype (Formal, Etype (Actual));
3729 return True;
3731 elsif Common_Type (T1) = Common_Type (T2) then
3732 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3733 return True;
3735 else
3736 return False;
3737 end if;
3739 else
3740 return False;
3741 end if;
3742 end Compatible_Types_In_Predicate;
3744 ----------------------------
3745 -- Indicate_Name_And_Type --
3746 ----------------------------
3748 procedure Indicate_Name_And_Type is
3749 begin
3750 Add_One_Interp (N, Nam, Etype (Nam));
3751 Check_Implicit_Dereference (N, Etype (Nam));
3752 Success := True;
3754 -- If the prefix of the call is a name, indicate the entity
3755 -- being called. If it is not a name, it is an expression that
3756 -- denotes an access to subprogram or else an entry or family. In
3757 -- the latter case, the name is a selected component, and the entity
3758 -- being called is noted on the selector.
3760 if not Is_Type (Nam) then
3761 if Is_Entity_Name (Name (N)) then
3762 Set_Entity (Name (N), Nam);
3763 Set_Etype (Name (N), Etype (Nam));
3765 elsif Nkind (Name (N)) = N_Selected_Component then
3766 Set_Entity (Selector_Name (Name (N)), Nam);
3767 end if;
3768 end if;
3770 if Debug_Flag_E and not Report then
3771 Write_Str (" Overloaded call ");
3772 Write_Int (Int (N));
3773 Write_Str (" compatible with ");
3774 Write_Int (Int (Nam));
3775 Write_Eol;
3776 end if;
3777 end Indicate_Name_And_Type;
3779 ------------------------
3780 -- Operator_Hidden_By --
3781 ------------------------
3783 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3784 Act1 : constant Node_Id := First_Actual (N);
3785 Act2 : constant Node_Id := Next_Actual (Act1);
3786 Form1 : constant Entity_Id := First_Formal (Fun);
3787 Form2 : constant Entity_Id := Next_Formal (Form1);
3789 begin
3790 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3791 return False;
3793 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3794 return False;
3796 elsif Present (Form2) then
3797 if No (Act2)
3798 or else not Has_Compatible_Type (Act2, Etype (Form2))
3799 then
3800 return False;
3801 end if;
3803 elsif Present (Act2) then
3804 return False;
3805 end if;
3807 -- Now we know that the arity of the operator matches the function,
3808 -- and the function call is a valid interpretation. The function
3809 -- hides the operator if it has the right signature, or if one of
3810 -- its operands is a non-abstract operation on Address when this is
3811 -- a visible integer type.
3813 return Hides_Op (Fun, Nam)
3814 or else Is_Descendant_Of_Address (Etype (Form1))
3815 or else
3816 (Present (Form2)
3817 and then Is_Descendant_Of_Address (Etype (Form2)));
3818 end Operator_Hidden_By;
3820 -- Start of processing for Analyze_One_Call
3822 begin
3823 Success := False;
3825 -- If the subprogram has no formals or if all the formals have defaults,
3826 -- and the return type is an array type, the node may denote an indexing
3827 -- of the result of a parameterless call. In Ada 2005, the subprogram
3828 -- may have one non-defaulted formal, and the call may have been written
3829 -- in prefix notation, so that the rebuilt parameter list has more than
3830 -- one actual.
3832 if not Is_Overloadable (Nam)
3833 and then Ekind (Nam) /= E_Subprogram_Type
3834 and then Ekind (Nam) /= E_Entry_Family
3835 then
3836 return;
3837 end if;
3839 -- An indexing requires at least one actual. The name of the call cannot
3840 -- be an implicit indirect call, so it cannot be a generated explicit
3841 -- dereference.
3843 if not Is_Empty_List (Actuals)
3844 and then
3845 (Needs_No_Actuals (Nam)
3846 or else
3847 (Needs_One_Actual (Nam)
3848 and then Present (Next_Actual (First (Actuals)))))
3849 then
3850 if Is_Array_Type (Subp_Type)
3851 and then
3852 (Nkind (Name (N)) /= N_Explicit_Dereference
3853 or else Comes_From_Source (Name (N)))
3854 then
3855 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3857 elsif Is_Access_Type (Subp_Type)
3858 and then Is_Array_Type (Designated_Type (Subp_Type))
3859 then
3860 Is_Indexed :=
3861 Try_Indexed_Call
3862 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3864 -- The prefix can also be a parameterless function that returns an
3865 -- access to subprogram, in which case this is an indirect call.
3866 -- If this succeeds, an explicit dereference is added later on,
3867 -- in Analyze_Call or Resolve_Call.
3869 elsif Is_Access_Type (Subp_Type)
3870 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3871 then
3872 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3873 end if;
3875 end if;
3877 -- If the call has been transformed into a slice, it is of the form
3878 -- F (Subtype) where F is parameterless. The node has been rewritten in
3879 -- Try_Indexed_Call and there is nothing else to do.
3881 if Is_Indexed
3882 and then Nkind (N) = N_Slice
3883 then
3884 return;
3885 end if;
3887 Normalize_Actuals
3888 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3890 if not Norm_OK then
3892 -- If an indirect call is a possible interpretation, indicate
3893 -- success to the caller. This may be an indexing of an explicit
3894 -- dereference of a call that returns an access type (see above).
3896 if Is_Indirect
3897 or else (Is_Indexed
3898 and then Nkind (Name (N)) = N_Explicit_Dereference
3899 and then Comes_From_Source (Name (N)))
3900 then
3901 Success := True;
3902 return;
3904 -- Mismatch in number or names of parameters
3906 elsif Debug_Flag_E then
3907 Write_Str (" normalization fails in call ");
3908 Write_Int (Int (N));
3909 Write_Str (" with subprogram ");
3910 Write_Int (Int (Nam));
3911 Write_Eol;
3912 end if;
3914 -- If the context expects a function call, discard any interpretation
3915 -- that is a procedure. If the node is not overloaded, leave as is for
3916 -- better error reporting when type mismatch is found.
3918 elsif Nkind (N) = N_Function_Call
3919 and then Is_Overloaded (Name (N))
3920 and then Ekind (Nam) = E_Procedure
3921 then
3922 return;
3924 -- Ditto for function calls in a procedure context
3926 elsif Nkind (N) = N_Procedure_Call_Statement
3927 and then Is_Overloaded (Name (N))
3928 and then Etype (Nam) /= Standard_Void_Type
3929 then
3930 return;
3932 elsif No (Actuals) then
3934 -- If Normalize succeeds, then there are default parameters for
3935 -- all formals.
3937 Indicate_Name_And_Type;
3939 elsif Ekind (Nam) = E_Operator then
3940 if Nkind (N) = N_Procedure_Call_Statement then
3941 return;
3942 end if;
3944 -- This occurs when the prefix of the call is an operator name
3945 -- or an expanded name whose selector is an operator name.
3947 Analyze_Operator_Call (N, Nam);
3949 if Etype (N) /= Prev_T then
3951 -- Check that operator is not hidden by a function interpretation
3953 if Is_Overloaded (Name (N)) then
3954 declare
3955 I : Interp_Index;
3956 It : Interp;
3958 begin
3959 Get_First_Interp (Name (N), I, It);
3960 while Present (It.Nam) loop
3961 if Operator_Hidden_By (It.Nam) then
3962 Set_Etype (N, Prev_T);
3963 return;
3964 end if;
3966 Get_Next_Interp (I, It);
3967 end loop;
3968 end;
3969 end if;
3971 -- If operator matches formals, record its name on the call.
3972 -- If the operator is overloaded, Resolve will select the
3973 -- correct one from the list of interpretations. The call
3974 -- node itself carries the first candidate.
3976 Set_Entity (Name (N), Nam);
3977 Success := True;
3979 elsif Report and then Etype (N) = Any_Type then
3980 Error_Msg_N ("incompatible arguments for operator", N);
3981 end if;
3983 else
3984 -- Normalize_Actuals has chained the named associations in the
3985 -- correct order of the formals.
3987 Actual := First_Actual (N);
3988 Formal := First_Formal (Nam);
3989 First_Form := Formal;
3991 -- If we are analyzing a call rewritten from object notation, skip
3992 -- first actual, which may be rewritten later as an explicit
3993 -- dereference.
3995 if Must_Skip then
3996 Next_Actual (Actual);
3997 Next_Formal (Formal);
3998 end if;
4000 while Present (Actual) and then Present (Formal) loop
4001 if Nkind (Parent (Actual)) /= N_Parameter_Association
4002 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
4003 then
4004 -- The actual can be compatible with the formal, but we must
4005 -- also check that the context is not an address type that is
4006 -- visibly an integer type. In this case the use of literals is
4007 -- illegal, except in the body of descendants of system, where
4008 -- arithmetic operations on address are of course used.
4010 if Has_Compatible_Type (Actual, Etype (Formal))
4011 and then
4012 (Etype (Actual) /= Universal_Integer
4013 or else not Is_Descendant_Of_Address (Etype (Formal))
4014 or else In_Predefined_Unit (N))
4015 then
4016 Next_Actual (Actual);
4017 Next_Formal (Formal);
4019 -- In Allow_Integer_Address mode, we allow an actual integer to
4020 -- match a formal address type and vice versa. We only do this
4021 -- if we are certain that an error will otherwise be issued
4023 elsif Address_Integer_Convert_OK
4024 (Etype (Actual), Etype (Formal))
4025 and then (Report and not Is_Indexed and not Is_Indirect)
4026 then
4027 -- Handle this case by introducing an unchecked conversion
4029 Rewrite (Actual,
4030 Unchecked_Convert_To (Etype (Formal),
4031 Relocate_Node (Actual)));
4032 Analyze_And_Resolve (Actual, Etype (Formal));
4033 Next_Actual (Actual);
4034 Next_Formal (Formal);
4036 -- Under relaxed RM semantics silently replace occurrences of
4037 -- null by System.Address_Null. We only do this if we know that
4038 -- an error will otherwise be issued.
4040 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
4041 and then (Report and not Is_Indexed and not Is_Indirect)
4042 then
4043 Replace_Null_By_Null_Address (Actual);
4044 Analyze_And_Resolve (Actual, Etype (Formal));
4045 Next_Actual (Actual);
4046 Next_Formal (Formal);
4048 elsif Compatible_Types_In_Predicate
4049 (Etype (Formal), Etype (Actual))
4050 then
4051 Next_Actual (Actual);
4052 Next_Formal (Formal);
4054 -- A current instance used as an actual of a function,
4055 -- whose body has not been seen, may include a formal
4056 -- whose type is an incomplete view of an enclosing
4057 -- type declaration containing the current call (e.g.
4058 -- in the Expression for a component declaration).
4060 -- In this case, update the signature of the subprogram
4061 -- so the formal has the type of the full view.
4063 elsif Inside_Init_Proc
4064 and then Nkind (Actual) = N_Identifier
4065 and then Ekind (Etype (Formal)) = E_Incomplete_Type
4066 and then Etype (Actual) = Full_View (Etype (Formal))
4067 then
4068 Set_Etype (Formal, Etype (Actual));
4069 Next_Actual (Actual);
4070 Next_Formal (Formal);
4072 -- Handle failed type check
4074 else
4075 if Debug_Flag_E then
4076 Write_Str (" type checking fails in call ");
4077 Write_Int (Int (N));
4078 Write_Str (" with formal ");
4079 Write_Int (Int (Formal));
4080 Write_Str (" in subprogram ");
4081 Write_Int (Int (Nam));
4082 Write_Eol;
4083 end if;
4085 -- Comment needed on the following test???
4087 if Report and not Is_Indexed and not Is_Indirect then
4089 -- Ada 2005 (AI-251): Complete the error notification
4090 -- to help new Ada 2005 users.
4092 if Is_Class_Wide_Type (Etype (Formal))
4093 and then Is_Interface (Etype (Etype (Formal)))
4094 and then not Interface_Present_In_Ancestor
4095 (Typ => Etype (Actual),
4096 Iface => Etype (Etype (Formal)))
4097 then
4098 Error_Msg_NE
4099 ("(Ada 2005) does not implement interface }",
4100 Actual, Etype (Etype (Formal)));
4101 end if;
4103 -- If we are going to output a secondary error message
4104 -- below, we need to have Wrong_Type output the main one.
4106 Wrong_Type
4107 (Actual, Etype (Formal), Multiple => All_Errors_Mode);
4109 if Nkind (Actual) = N_Op_Eq
4110 and then Nkind (Left_Opnd (Actual)) = N_Identifier
4111 then
4112 Formal := First_Formal (Nam);
4113 while Present (Formal) loop
4114 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
4115 Error_Msg_N -- CODEFIX
4116 ("possible misspelling of `='>`!", Actual);
4117 exit;
4118 end if;
4120 Next_Formal (Formal);
4121 end loop;
4122 end if;
4124 if All_Errors_Mode then
4125 Error_Msg_Sloc := Sloc (Nam);
4127 if Etype (Formal) = Any_Type then
4128 Error_Msg_N
4129 ("there is no legal actual parameter", Actual);
4130 end if;
4132 if Is_Overloadable (Nam)
4133 and then Present (Alias (Nam))
4134 and then not Comes_From_Source (Nam)
4135 then
4136 Error_Msg_NE
4137 ("\\ =='> in call to inherited operation & #!",
4138 Actual, Nam);
4140 elsif Ekind (Nam) = E_Subprogram_Type then
4141 declare
4142 Access_To_Subprogram_Typ :
4143 constant Entity_Id :=
4144 Defining_Identifier
4145 (Associated_Node_For_Itype (Nam));
4146 begin
4147 Error_Msg_NE
4148 ("\\ =='> in call to dereference of &#!",
4149 Actual, Access_To_Subprogram_Typ);
4150 end;
4152 else
4153 Error_Msg_NE
4154 ("\\ =='> in call to &#!", Actual, Nam);
4156 end if;
4157 end if;
4158 end if;
4160 return;
4161 end if;
4163 else
4164 -- Normalize_Actuals has verified that a default value exists
4165 -- for this formal. Current actual names a subsequent formal.
4167 Next_Formal (Formal);
4168 end if;
4169 end loop;
4171 -- Due to our current model of controlled type expansion we may
4172 -- have resolved a user call to a non-visible controlled primitive
4173 -- since these inherited subprograms may be generated in the current
4174 -- scope. This is a side effect of the need for the expander to be
4175 -- able to resolve internally generated calls.
4177 -- Specifically, the issue appears when predefined controlled
4178 -- operations get called on a type extension whose parent is a
4179 -- private extension completed with a controlled extension - see
4180 -- below:
4182 -- package X is
4183 -- type Par_Typ is tagged private;
4184 -- private
4185 -- type Par_Typ is new Controlled with null record;
4186 -- end;
4187 -- ...
4188 -- procedure Main is
4189 -- type Ext_Typ is new Par_Typ with null record;
4190 -- Obj : Ext_Typ;
4191 -- begin
4192 -- Finalize (Obj); -- Will improperly resolve
4193 -- end;
4195 -- To avoid breaking privacy, Is_Hidden gets set elsewhere on such
4196 -- primitives, but we still need to verify that Nam is indeed a
4197 -- non-visible controlled subprogram. So, we do that here and issue
4198 -- the appropriate error.
4200 if Is_Hidden (Nam)
4201 and then not In_Instance
4202 and then not Comes_From_Source (Nam)
4203 and then Comes_From_Source (N)
4205 -- Verify Nam is a non-visible controlled primitive
4207 and then Chars (Nam) in Name_Adjust
4208 | Name_Finalize
4209 | Name_Initialize
4210 and then Ekind (Nam) = E_Procedure
4211 and then Is_Controlled (Etype (First_Form))
4212 and then No (Next_Formal (First_Form))
4213 and then not Is_Visibly_Controlled (Etype (First_Form))
4214 then
4215 Error_Msg_Node_2 := Etype (First_Form);
4216 Error_Msg_NE ("call to non-visible controlled primitive & on type"
4217 & " &", N, Nam);
4218 end if;
4220 -- On exit, all actuals match
4222 Indicate_Name_And_Type;
4223 end if;
4224 end Analyze_One_Call;
4226 ---------------------------
4227 -- Analyze_Operator_Call --
4228 ---------------------------
4230 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
4231 Op_Name : constant Name_Id := Chars (Op_Id);
4232 Act1 : constant Node_Id := First_Actual (N);
4233 Act2 : constant Node_Id := Next_Actual (Act1);
4235 begin
4236 -- Binary operator case
4238 if Present (Act2) then
4240 -- If more than two operands, then not binary operator after all
4242 if Present (Next_Actual (Act2)) then
4243 return;
4244 end if;
4246 -- Otherwise action depends on operator
4248 case Op_Name is
4249 when Name_Op_Add
4250 | Name_Op_Divide
4251 | Name_Op_Expon
4252 | Name_Op_Mod
4253 | Name_Op_Multiply
4254 | Name_Op_Rem
4255 | Name_Op_Subtract
4257 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
4259 when Name_Op_And
4260 | Name_Op_Or
4261 | Name_Op_Xor
4263 Find_Boolean_Types (Act1, Act2, Op_Id, N);
4265 when Name_Op_Eq
4266 | Name_Op_Ge
4267 | Name_Op_Gt
4268 | Name_Op_Le
4269 | Name_Op_Lt
4270 | Name_Op_Ne
4272 Find_Comparison_Equality_Types (Act1, Act2, Op_Id, N);
4274 when Name_Op_Concat =>
4275 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
4277 -- Is this when others, or should it be an abort???
4279 when others =>
4280 null;
4281 end case;
4283 -- Unary operator case
4285 else
4286 case Op_Name is
4287 when Name_Op_Abs
4288 | Name_Op_Add
4289 | Name_Op_Subtract
4291 Find_Unary_Types (Act1, Op_Id, N);
4293 when Name_Op_Not =>
4294 Find_Negation_Types (Act1, Op_Id, N);
4296 -- Is this when others correct, or should it be an abort???
4298 when others =>
4299 null;
4300 end case;
4301 end if;
4302 end Analyze_Operator_Call;
4304 -------------------------------------------
4305 -- Analyze_Overloaded_Selected_Component --
4306 -------------------------------------------
4308 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
4309 Nam : constant Node_Id := Prefix (N);
4310 Sel : constant Node_Id := Selector_Name (N);
4311 Comp : Entity_Id;
4312 I : Interp_Index;
4313 It : Interp;
4314 T : Entity_Id;
4316 begin
4317 Set_Etype (Sel, Any_Type);
4319 Get_First_Interp (Nam, I, It);
4320 while Present (It.Typ) loop
4321 if Is_Access_Type (It.Typ) then
4322 T := Designated_Type (It.Typ);
4323 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4324 else
4325 T := It.Typ;
4326 end if;
4328 -- Locate the component. For a private prefix the selector can denote
4329 -- a discriminant.
4331 if Is_Record_Type (T) or else Is_Private_Type (T) then
4333 -- If the prefix is a class-wide type, the visible components are
4334 -- those of the base type.
4336 if Is_Class_Wide_Type (T) then
4337 T := Etype (T);
4338 end if;
4340 Comp := First_Entity (T);
4341 while Present (Comp) loop
4342 if Chars (Comp) = Chars (Sel)
4343 and then Is_Visible_Component (Comp, Sel)
4344 then
4346 -- AI05-105: if the context is an object renaming with
4347 -- an anonymous access type, the expected type of the
4348 -- object must be anonymous. This is a name resolution rule.
4350 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
4351 or else No (Access_Definition (Parent (N)))
4352 or else Is_Anonymous_Access_Type (Etype (Comp))
4353 then
4354 Set_Entity (Sel, Comp);
4355 Set_Etype (Sel, Etype (Comp));
4356 Add_One_Interp (N, Etype (Comp), Etype (Comp));
4357 Check_Implicit_Dereference (N, Etype (Comp));
4359 -- This also specifies a candidate to resolve the name.
4360 -- Further overloading will be resolved from context.
4361 -- The selector name itself does not carry overloading
4362 -- information.
4364 Set_Etype (Nam, It.Typ);
4366 else
4367 -- Named access type in the context of a renaming
4368 -- declaration with an access definition. Remove
4369 -- inapplicable candidate.
4371 Remove_Interp (I);
4372 end if;
4373 end if;
4375 Next_Entity (Comp);
4376 end loop;
4378 elsif Is_Concurrent_Type (T) then
4379 Comp := First_Entity (T);
4380 while Present (Comp)
4381 and then Comp /= First_Private_Entity (T)
4382 loop
4383 if Chars (Comp) = Chars (Sel) then
4384 if Is_Overloadable (Comp) then
4385 Add_One_Interp (Sel, Comp, Etype (Comp));
4386 else
4387 Set_Entity_With_Checks (Sel, Comp);
4388 Generate_Reference (Comp, Sel);
4389 end if;
4391 Set_Etype (Sel, Etype (Comp));
4392 Set_Etype (N, Etype (Comp));
4393 Set_Etype (Nam, It.Typ);
4394 end if;
4396 Next_Entity (Comp);
4397 end loop;
4399 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4400 end if;
4402 Get_Next_Interp (I, It);
4403 end loop;
4405 if Etype (N) = Any_Type
4406 and then not Try_Object_Operation (N)
4407 then
4408 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
4409 Set_Entity (Sel, Any_Id);
4410 Set_Etype (Sel, Any_Type);
4411 end if;
4412 end Analyze_Overloaded_Selected_Component;
4414 ----------------------------------
4415 -- Analyze_Qualified_Expression --
4416 ----------------------------------
4418 procedure Analyze_Qualified_Expression (N : Node_Id) is
4419 Expr : constant Node_Id := Expression (N);
4420 Mark : constant Entity_Id := Subtype_Mark (N);
4422 I : Interp_Index;
4423 It : Interp;
4424 T : Entity_Id;
4426 begin
4427 Find_Type (Mark);
4428 T := Entity (Mark);
4430 if Nkind (Enclosing_Declaration (N)) in
4431 N_Formal_Type_Declaration |
4432 N_Full_Type_Declaration |
4433 N_Incomplete_Type_Declaration |
4434 N_Protected_Type_Declaration |
4435 N_Private_Extension_Declaration |
4436 N_Private_Type_Declaration |
4437 N_Subtype_Declaration |
4438 N_Task_Type_Declaration
4439 and then T = Defining_Identifier (Enclosing_Declaration (N))
4440 then
4441 Error_Msg_N ("current instance not allowed", Mark);
4442 T := Any_Type;
4443 end if;
4445 Set_Etype (N, T);
4447 Analyze_Expression (Expr);
4449 if T = Any_Type then
4450 return;
4451 end if;
4453 Check_Fully_Declared (T, N);
4455 -- If expected type is class-wide, check for exact match before
4456 -- expansion, because if the expression is a dispatching call it
4457 -- may be rewritten as explicit dereference with class-wide result.
4458 -- If expression is overloaded, retain only interpretations that
4459 -- will yield exact matches.
4461 if Is_Class_Wide_Type (T) then
4462 if not Is_Overloaded (Expr) then
4463 if Base_Type (Etype (Expr)) /= Base_Type (T)
4464 and then Etype (Expr) /= Raise_Type
4465 then
4466 if Nkind (Expr) = N_Aggregate then
4467 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
4468 else
4469 Wrong_Type (Expr, T);
4470 end if;
4471 end if;
4473 else
4474 Get_First_Interp (Expr, I, It);
4476 while Present (It.Nam) loop
4477 if Base_Type (It.Typ) /= Base_Type (T) then
4478 Remove_Interp (I);
4479 end if;
4481 Get_Next_Interp (I, It);
4482 end loop;
4483 end if;
4484 end if;
4485 end Analyze_Qualified_Expression;
4487 -----------------------------------
4488 -- Analyze_Quantified_Expression --
4489 -----------------------------------
4491 procedure Analyze_Quantified_Expression (N : Node_Id) is
4492 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
4493 -- Return True if the iterator is part of a quantified expression and
4494 -- the range is known to be statically empty.
4496 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
4497 -- Determine whether if expression If_Expr lacks an else part or if it
4498 -- has one, it evaluates to True.
4500 --------------------
4501 -- Is_Empty_Range --
4502 --------------------
4504 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4505 begin
4506 return Is_Array_Type (Typ)
4507 and then Compile_Time_Known_Bounds (Typ)
4508 and then
4509 Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4510 Expr_Value (Type_High_Bound (Etype (First_Index (Typ))));
4511 end Is_Empty_Range;
4513 -----------------------------
4514 -- No_Else_Or_Trivial_True --
4515 -----------------------------
4517 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4518 Else_Expr : constant Node_Id :=
4519 Next (Next (First (Expressions (If_Expr))));
4520 begin
4521 return
4522 No (Else_Expr)
4523 or else (Compile_Time_Known_Value (Else_Expr)
4524 and then Is_True (Expr_Value (Else_Expr)));
4525 end No_Else_Or_Trivial_True;
4527 -- Local variables
4529 Cond : constant Node_Id := Condition (N);
4530 Loc : constant Source_Ptr := Sloc (N);
4531 Loop_Id : Entity_Id;
4532 QE_Scop : Entity_Id;
4534 -- Start of processing for Analyze_Quantified_Expression
4536 begin
4537 -- Create a scope to emulate the loop-like behavior of the quantified
4538 -- expression. The scope is needed to provide proper visibility of the
4539 -- loop variable.
4541 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
4542 Set_Etype (QE_Scop, Standard_Void_Type);
4543 Set_Scope (QE_Scop, Current_Scope);
4544 Set_Parent (QE_Scop, N);
4546 Push_Scope (QE_Scop);
4548 -- All constituents are preanalyzed and resolved to avoid untimely
4549 -- generation of various temporaries and types. Full analysis and
4550 -- expansion is carried out when the quantified expression is
4551 -- transformed into an expression with actions.
4553 if Present (Iterator_Specification (N)) then
4554 Preanalyze (Iterator_Specification (N));
4556 -- Do not proceed with the analysis when the range of iteration is
4557 -- empty.
4559 if Is_Entity_Name (Name (Iterator_Specification (N)))
4560 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4561 then
4562 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4563 End_Scope;
4565 -- Emit a warning and replace expression with its static value
4567 if All_Present (N) then
4568 Error_Msg_N
4569 ("??quantified expression with ALL "
4570 & "over a null range has value True", N);
4571 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4573 else
4574 Error_Msg_N
4575 ("??quantified expression with SOME "
4576 & "over a null range has value False", N);
4577 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4578 end if;
4580 Analyze (N);
4581 return;
4582 end if;
4584 else pragma Assert (Present (Loop_Parameter_Specification (N)));
4585 declare
4586 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4588 begin
4589 Preanalyze (Loop_Par);
4591 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
4592 and then Parent (Loop_Par) /= N
4593 then
4594 -- The parser cannot distinguish between a loop specification
4595 -- and an iterator specification. If after preanalysis the
4596 -- proper form has been recognized, rewrite the expression to
4597 -- reflect the right kind. This is needed for proper ASIS
4598 -- navigation. If expansion is enabled, the transformation is
4599 -- performed when the expression is rewritten as a loop.
4600 -- Is this still needed???
4602 Set_Iterator_Specification (N,
4603 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
4605 Set_Defining_Identifier (Iterator_Specification (N),
4606 Relocate_Node (Defining_Identifier (Loop_Par)));
4607 Set_Name (Iterator_Specification (N),
4608 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4609 Set_Comes_From_Source (Iterator_Specification (N),
4610 Comes_From_Source (Loop_Parameter_Specification (N)));
4611 Set_Loop_Parameter_Specification (N, Empty);
4612 end if;
4613 end;
4614 end if;
4616 Preanalyze_And_Resolve (Cond, Standard_Boolean);
4618 End_Scope;
4619 Set_Etype (N, Standard_Boolean);
4621 -- Verify that the loop variable is used within the condition of the
4622 -- quantified expression.
4624 if Present (Iterator_Specification (N)) then
4625 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4626 else
4627 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4628 end if;
4630 declare
4631 type Subexpr_Kind is (Full, Conjunct, Disjunct);
4633 procedure Check_Subexpr (Expr : Node_Id; Kind : Subexpr_Kind);
4634 -- Check that the quantified variable appears in every sub-expression
4635 -- of the quantified expression. If Kind is Full, Expr is the full
4636 -- expression. If Kind is Conjunct (resp. Disjunct), Expr is a
4637 -- conjunct (resp. disjunct) of the full expression.
4639 -------------------
4640 -- Check_Subexpr --
4641 -------------------
4643 procedure Check_Subexpr (Expr : Node_Id; Kind : Subexpr_Kind) is
4644 begin
4645 if Nkind (Expr) in N_Op_And | N_And_Then
4646 and then Kind /= Disjunct
4647 then
4648 Check_Subexpr (Left_Opnd (Expr), Conjunct);
4649 Check_Subexpr (Right_Opnd (Expr), Conjunct);
4651 elsif Nkind (Expr) in N_Op_Or | N_Or_Else
4652 and then Kind /= Conjunct
4653 then
4654 Check_Subexpr (Left_Opnd (Expr), Disjunct);
4655 Check_Subexpr (Right_Opnd (Expr), Disjunct);
4657 elsif Kind /= Full
4658 and then not Referenced (Loop_Id, Expr)
4659 then
4660 declare
4661 Sub : constant String :=
4662 (if Kind = Conjunct then "conjunct" else "disjunct");
4663 begin
4664 Error_Msg_NE
4665 ("?.t?unused variable & in " & Sub, Expr, Loop_Id);
4666 Error_Msg_NE
4667 ("\consider extracting " & Sub & " from quantified "
4668 & "expression", Expr, Loop_Id);
4669 end;
4670 end if;
4671 end Check_Subexpr;
4673 begin
4674 if Warn_On_Suspicious_Contract
4675 and then not Is_Internal_Name (Chars (Loop_Id))
4677 -- Generating C, this check causes spurious warnings on inlined
4678 -- postconditions; we can safely disable it because this check
4679 -- was previously performed when analyzing the internally built
4680 -- postconditions procedure.
4682 and then not (Modify_Tree_For_C and In_Inlined_Body)
4683 then
4684 if not Referenced (Loop_Id, Cond) then
4685 Error_Msg_N ("?.t?unused variable &", Loop_Id);
4686 else
4687 Check_Subexpr (Cond, Kind => Full);
4688 end if;
4689 end if;
4690 end;
4692 -- Diagnose a possible misuse of the SOME existential quantifier. When
4693 -- we have a quantified expression of the form:
4695 -- for some X => (if P then Q [else True])
4697 -- any value for X that makes P False results in the if expression being
4698 -- trivially True, and so also results in the quantified expression
4699 -- being trivially True.
4701 if Warn_On_Suspicious_Contract
4702 and then not All_Present (N)
4703 and then Nkind (Cond) = N_If_Expression
4704 and then No_Else_Or_Trivial_True (Cond)
4705 then
4706 Error_Msg_N ("?.t?suspicious expression", N);
4707 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4708 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4709 end if;
4710 end Analyze_Quantified_Expression;
4712 -------------------
4713 -- Analyze_Range --
4714 -------------------
4716 procedure Analyze_Range (N : Node_Id) is
4717 L : constant Node_Id := Low_Bound (N);
4718 H : constant Node_Id := High_Bound (N);
4719 I1, I2 : Interp_Index;
4720 It1, It2 : Interp;
4722 procedure Check_Common_Type (T1, T2 : Entity_Id);
4723 -- Verify the compatibility of two types, and choose the
4724 -- non universal one if the other is universal.
4726 procedure Check_High_Bound (T : Entity_Id);
4727 -- Test one interpretation of the low bound against all those
4728 -- of the high bound.
4730 procedure Check_Universal_Expression (N : Node_Id);
4731 -- In Ada 83, reject bounds of a universal range that are not literals
4732 -- or entity names.
4734 -----------------------
4735 -- Check_Common_Type --
4736 -----------------------
4738 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4739 begin
4740 if Covers (T1 => T1, T2 => T2)
4741 or else
4742 Covers (T1 => T2, T2 => T1)
4743 then
4744 if Is_Universal_Numeric_Type (T1)
4745 or else T1 = Any_Character
4746 then
4747 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4749 elsif T1 = T2 then
4750 Add_One_Interp (N, T1, T1);
4752 else
4753 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4754 end if;
4755 end if;
4756 end Check_Common_Type;
4758 ----------------------
4759 -- Check_High_Bound --
4760 ----------------------
4762 procedure Check_High_Bound (T : Entity_Id) is
4763 begin
4764 if not Is_Overloaded (H) then
4765 Check_Common_Type (T, Etype (H));
4766 else
4767 Get_First_Interp (H, I2, It2);
4768 while Present (It2.Typ) loop
4769 Check_Common_Type (T, It2.Typ);
4770 Get_Next_Interp (I2, It2);
4771 end loop;
4772 end if;
4773 end Check_High_Bound;
4775 --------------------------------
4776 -- Check_Universal_Expression --
4777 --------------------------------
4779 procedure Check_Universal_Expression (N : Node_Id) is
4780 begin
4781 if Etype (N) = Universal_Integer
4782 and then Nkind (N) /= N_Integer_Literal
4783 and then not Is_Entity_Name (N)
4784 and then Nkind (N) /= N_Attribute_Reference
4785 then
4786 Error_Msg_N ("illegal bound in discrete range", N);
4787 end if;
4788 end Check_Universal_Expression;
4790 -- Start of processing for Analyze_Range
4792 begin
4793 Set_Etype (N, Any_Type);
4794 Analyze_Expression (L);
4795 Analyze_Expression (H);
4797 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4798 return;
4800 else
4801 if not Is_Overloaded (L) then
4802 Check_High_Bound (Etype (L));
4803 else
4804 Get_First_Interp (L, I1, It1);
4805 while Present (It1.Typ) loop
4806 Check_High_Bound (It1.Typ);
4807 Get_Next_Interp (I1, It1);
4808 end loop;
4809 end if;
4811 -- If result is Any_Type, then we did not find a compatible pair
4813 if Etype (N) = Any_Type then
4814 Error_Msg_N ("incompatible types in range", N);
4815 end if;
4816 end if;
4818 if Ada_Version = Ada_83
4819 and then
4820 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4821 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4822 then
4823 Check_Universal_Expression (L);
4824 Check_Universal_Expression (H);
4825 end if;
4827 Check_Function_Writable_Actuals (N);
4828 end Analyze_Range;
4830 -----------------------
4831 -- Analyze_Reference --
4832 -----------------------
4834 procedure Analyze_Reference (N : Node_Id) is
4835 P : constant Node_Id := Prefix (N);
4836 E : Entity_Id;
4837 T : Entity_Id;
4838 Acc_Type : Entity_Id;
4840 begin
4841 Analyze (P);
4843 -- An interesting error check, if we take the 'Ref of an object for
4844 -- which a pragma Atomic or Volatile has been given, and the type of the
4845 -- object is not Atomic or Volatile, then we are in trouble. The problem
4846 -- is that no trace of the atomic/volatile status will remain for the
4847 -- backend to respect when it deals with the resulting pointer, since
4848 -- the pointer type will not be marked atomic (it is a pointer to the
4849 -- base type of the object).
4851 -- It is not clear if that can ever occur, but in case it does, we will
4852 -- generate an error message. Not clear if this message can ever be
4853 -- generated, and pretty clear that it represents a bug if it is, still
4854 -- seems worth checking, except in CodePeer mode where we do not really
4855 -- care and don't want to bother the user.
4857 T := Etype (P);
4859 if Is_Entity_Name (P)
4860 and then Is_Object_Reference (P)
4861 and then not CodePeer_Mode
4862 then
4863 E := Entity (P);
4864 T := Etype (P);
4866 if (Has_Atomic_Components (E)
4867 and then not Has_Atomic_Components (T))
4868 or else
4869 (Has_Volatile_Components (E)
4870 and then not Has_Volatile_Components (T))
4871 or else (Is_Atomic (E) and then not Is_Atomic (T))
4872 or else (Is_Volatile (E) and then not Is_Volatile (T))
4873 then
4874 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4875 end if;
4876 end if;
4878 -- Carry on with normal processing
4880 Acc_Type := Create_Itype (E_Allocator_Type, N);
4881 Set_Etype (Acc_Type, Acc_Type);
4882 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4883 Set_Etype (N, Acc_Type);
4884 end Analyze_Reference;
4886 --------------------------------
4887 -- Analyze_Selected_Component --
4888 --------------------------------
4890 -- Prefix is a record type or a task or protected type. In the latter case,
4891 -- the selector must denote a visible entry.
4893 procedure Analyze_Selected_Component (N : Node_Id) is
4894 Name : constant Node_Id := Prefix (N);
4895 Sel : constant Node_Id := Selector_Name (N);
4896 Act_Decl : Node_Id;
4897 Comp : Entity_Id := Empty;
4898 Has_Candidate : Boolean := False;
4899 Hidden_Comp : Entity_Id;
4900 In_Scope : Boolean;
4901 Is_Private_Op : Boolean;
4902 Parent_N : Node_Id;
4903 Prefix_Type : Entity_Id;
4905 Type_To_Use : Entity_Id;
4906 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4907 -- a class-wide type, we use its root type, whose components are
4908 -- present in the class-wide type.
4910 Is_Single_Concurrent_Object : Boolean;
4911 -- Set True if the prefix is a single task or a single protected object
4913 function Constraint_Has_Unprefixed_Discriminant_Reference
4914 (Typ : Entity_Id) return Boolean;
4915 -- Given a subtype that is subject to a discriminant-dependent
4916 -- constraint, returns True if any of the values of the constraint
4917 -- (i.e., any of the index values for an index constraint, any of
4918 -- the discriminant values for a discriminant constraint)
4919 -- are unprefixed discriminant names.
4921 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4922 -- It is known that the parent of N denotes a subprogram call. Comp
4923 -- is an overloadable component of the concurrent type of the prefix.
4924 -- Determine whether all formals of the parent of N and Comp are mode
4925 -- conformant. If the parent node is not analyzed yet it may be an
4926 -- indexed component rather than a function call.
4928 function Has_Dereference (Nod : Node_Id) return Boolean;
4929 -- Check whether prefix includes a dereference, explicit or implicit,
4930 -- at any recursive level.
4932 function Try_By_Protected_Procedure_Prefixed_View return Boolean;
4933 -- Return True if N is an access attribute whose prefix is a prefixed
4934 -- class-wide (synchronized or protected) interface view for which some
4935 -- interpretation is a procedure with synchronization kind By_Protected
4936 -- _Procedure, and collect all its interpretations (since it may be an
4937 -- overloaded interface primitive); otherwise return False.
4939 function Try_Selected_Component_In_Instance
4940 (Typ : Entity_Id) return Boolean;
4941 -- If Typ is the actual for a formal derived type, or a derived type
4942 -- thereof, the component inherited from the generic parent may not
4943 -- be visible in the actual, but the selected component is legal. Climb
4944 -- up the derivation chain of the generic parent type and return True if
4945 -- we find the proper ancestor type; otherwise return False.
4947 ------------------------------------------------------
4948 -- Constraint_Has_Unprefixed_Discriminant_Reference --
4949 ------------------------------------------------------
4951 function Constraint_Has_Unprefixed_Discriminant_Reference
4952 (Typ : Entity_Id) return Boolean
4954 function Is_Discriminant_Name (N : Node_Id) return Boolean is
4955 (Nkind (N) = N_Identifier
4956 and then Ekind (Entity (N)) = E_Discriminant);
4957 begin
4958 if Is_Array_Type (Typ) then
4959 declare
4960 Index : Node_Id := First_Index (Typ);
4961 Rng : Node_Id;
4962 begin
4963 while Present (Index) loop
4964 Rng := Index;
4965 if Nkind (Rng) = N_Subtype_Indication then
4966 Rng := Range_Expression (Constraint (Rng));
4967 end if;
4969 if Nkind (Rng) = N_Range then
4970 if Is_Discriminant_Name (Low_Bound (Rng))
4971 or else Is_Discriminant_Name (High_Bound (Rng))
4972 then
4973 return True;
4974 end if;
4975 end if;
4977 Next_Index (Index);
4978 end loop;
4979 end;
4980 else
4981 declare
4982 Elmt : Elmt_Id := First_Elmt (Discriminant_Constraint (Typ));
4983 begin
4984 while Present (Elmt) loop
4985 if Is_Discriminant_Name (Node (Elmt)) then
4986 return True;
4987 end if;
4988 Next_Elmt (Elmt);
4989 end loop;
4990 end;
4991 end if;
4993 return False;
4994 end Constraint_Has_Unprefixed_Discriminant_Reference;
4996 ------------------------------
4997 -- Has_Mode_Conformant_Spec --
4998 ------------------------------
5000 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
5001 Comp_Param : Entity_Id;
5002 Param : Node_Id;
5003 Param_Typ : Entity_Id;
5005 begin
5006 Comp_Param := First_Formal (Comp);
5008 if Nkind (Parent (N)) = N_Indexed_Component then
5009 Param := First (Expressions (Parent (N)));
5010 else
5011 Param := First (Parameter_Associations (Parent (N)));
5012 end if;
5014 while Present (Comp_Param)
5015 and then Present (Param)
5016 loop
5017 Param_Typ := Find_Parameter_Type (Param);
5019 if Present (Param_Typ)
5020 and then
5021 not Conforming_Types
5022 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
5023 then
5024 return False;
5025 end if;
5027 Next_Formal (Comp_Param);
5028 Next (Param);
5029 end loop;
5031 -- One of the specs has additional formals; there is no match, unless
5032 -- this may be an indexing of a parameterless call.
5034 -- Note that when expansion is disabled, the corresponding record
5035 -- type of synchronized types is not constructed, so that there is
5036 -- no point is attempting an interpretation as a prefixed call, as
5037 -- this is bound to fail because the primitive operations will not
5038 -- be properly located.
5040 if Present (Comp_Param) or else Present (Param) then
5041 if Needs_No_Actuals (Comp)
5042 and then Is_Array_Type (Etype (Comp))
5043 and then not Expander_Active
5044 then
5045 return True;
5046 else
5047 return False;
5048 end if;
5049 end if;
5051 return True;
5052 end Has_Mode_Conformant_Spec;
5054 ---------------------
5055 -- Has_Dereference --
5056 ---------------------
5058 function Has_Dereference (Nod : Node_Id) return Boolean is
5059 begin
5060 if Nkind (Nod) = N_Explicit_Dereference then
5061 return True;
5063 elsif Is_Access_Type (Etype (Nod)) then
5064 return True;
5066 elsif Nkind (Nod) in N_Indexed_Component | N_Selected_Component then
5067 return Has_Dereference (Prefix (Nod));
5069 else
5070 return False;
5071 end if;
5072 end Has_Dereference;
5074 ----------------------------------------------
5075 -- Try_By_Protected_Procedure_Prefixed_View --
5076 ----------------------------------------------
5078 function Try_By_Protected_Procedure_Prefixed_View return Boolean is
5079 Candidate : Node_Id := Empty;
5080 Elmt : Elmt_Id;
5081 Prim : Node_Id;
5083 begin
5084 if Nkind (Parent (N)) = N_Attribute_Reference
5085 and then Attribute_Name (Parent (N)) in
5086 Name_Access
5087 | Name_Unchecked_Access
5088 | Name_Unrestricted_Access
5089 and then Is_Class_Wide_Type (Prefix_Type)
5090 and then (Is_Synchronized_Interface (Prefix_Type)
5091 or else Is_Protected_Interface (Prefix_Type))
5092 then
5093 -- If we have not found yet any interpretation then mark this
5094 -- one as the first interpretation (cf. Add_One_Interp).
5096 if No (Etype (Sel)) then
5097 Set_Etype (Sel, Any_Type);
5098 end if;
5100 Elmt := First_Elmt (Primitive_Operations (Etype (Prefix_Type)));
5101 while Present (Elmt) loop
5102 Prim := Node (Elmt);
5104 if Chars (Prim) = Chars (Sel)
5105 and then Is_By_Protected_Procedure (Prim)
5106 then
5107 Candidate := New_Copy (Prim);
5109 -- Skip the controlling formal; required to check type
5110 -- conformance of the target access to protected type
5111 -- (see Conforming_Types).
5113 Set_First_Entity (Candidate,
5114 Next_Entity (First_Entity (Prim)));
5116 Add_One_Interp (Sel, Candidate, Etype (Prim));
5117 Set_Etype (N, Etype (Prim));
5118 end if;
5120 Next_Elmt (Elmt);
5121 end loop;
5122 end if;
5124 -- Propagate overloaded attribute
5126 if Present (Candidate) and then Is_Overloaded (Sel) then
5127 Set_Is_Overloaded (N);
5128 end if;
5130 return Present (Candidate);
5131 end Try_By_Protected_Procedure_Prefixed_View;
5133 ----------------------------------------
5134 -- Try_Selected_Component_In_Instance --
5135 ----------------------------------------
5137 function Try_Selected_Component_In_Instance
5138 (Typ : Entity_Id) return Boolean
5140 procedure Find_Component_In_Instance (Rec : Entity_Id);
5141 -- In an instance, a component of a private extension may not be
5142 -- visible while it was visible in the generic. Search candidate
5143 -- scope for a component with the proper identifier. If a match is
5144 -- found, the Etype of both N and Sel are set from this component,
5145 -- and the entity of Sel is set to reference this component. If no
5146 -- match is found, Entity (Sel) remains unset. For a derived type
5147 -- that is an actual of the instance, the desired component may be
5148 -- found in any ancestor.
5150 --------------------------------
5151 -- Find_Component_In_Instance --
5152 --------------------------------
5154 procedure Find_Component_In_Instance (Rec : Entity_Id) is
5155 Comp : Entity_Id;
5156 Typ : Entity_Id;
5158 begin
5159 Typ := Rec;
5160 while Present (Typ) loop
5161 Comp := First_Component (Typ);
5162 while Present (Comp) loop
5163 if Chars (Comp) = Chars (Sel) then
5164 Set_Entity_With_Checks (Sel, Comp);
5165 Set_Etype (Sel, Etype (Comp));
5166 Set_Etype (N, Etype (Comp));
5167 return;
5168 end if;
5170 Next_Component (Comp);
5171 end loop;
5173 -- If not found, the component may be declared in the parent
5174 -- type or its full view, if any.
5176 if Is_Derived_Type (Typ) then
5177 Typ := Etype (Typ);
5179 if Is_Private_Type (Typ) then
5180 Typ := Full_View (Typ);
5181 end if;
5183 else
5184 return;
5185 end if;
5186 end loop;
5188 -- If we fall through, no match, so no changes made
5190 return;
5191 end Find_Component_In_Instance;
5193 -- Local variables
5195 Par : Entity_Id;
5197 -- Start of processing for Try_Selected_Component_In_Instance
5199 begin
5200 pragma Assert (In_Instance and then Is_Tagged_Type (Typ));
5201 pragma Assert (Etype (N) = Any_Type);
5203 -- Climb up derivation chain to generic actual subtype
5205 Par := Typ;
5206 while not Is_Generic_Actual_Type (Par) loop
5207 if Ekind (Par) = E_Record_Type then
5208 Par := Parent_Subtype (Par);
5209 exit when No (Par);
5210 else
5211 exit when Par = Etype (Par);
5212 Par := Etype (Par);
5213 end if;
5214 end loop;
5216 -- If Par is a generic actual, look for component in ancestor types.
5217 -- Skip this if we have no Declaration_Node, as is the case for
5218 -- itypes.
5220 if Present (Par)
5221 and then Is_Generic_Actual_Type (Par)
5222 and then Present (Declaration_Node (Par))
5223 then
5224 Par := Generic_Parent_Type (Declaration_Node (Par));
5225 loop
5226 Find_Component_In_Instance (Par);
5227 exit when Present (Entity (Sel))
5228 or else Par = Etype (Par);
5229 Par := Etype (Par);
5230 end loop;
5232 -- Another special case: the type is an extension of a private
5233 -- type T, either is an actual in an instance or is immediately
5234 -- visible, and we are in the body of the instance, which means
5235 -- the generic body had a full view of the type declaration for
5236 -- T or some ancestor that defines the component in question.
5237 -- This happens because Is_Visible_Component returned False on
5238 -- this component, as T or the ancestor is still private since
5239 -- the Has_Private_View mechanism is bypassed because T or the
5240 -- ancestor is not directly referenced in the generic body.
5242 elsif Is_Derived_Type (Typ)
5243 and then (Used_As_Generic_Actual (Typ)
5244 or else Is_Immediately_Visible (Typ))
5245 and then In_Instance_Body
5246 then
5247 Find_Component_In_Instance (Parent_Subtype (Typ));
5248 end if;
5250 return Etype (N) /= Any_Type;
5251 end Try_Selected_Component_In_Instance;
5253 -- Start of processing for Analyze_Selected_Component
5255 begin
5256 Set_Etype (N, Any_Type);
5258 if Is_Overloaded (Name) then
5259 Analyze_Overloaded_Selected_Component (N);
5260 return;
5262 elsif Etype (Name) = Any_Type then
5263 Set_Entity (Sel, Any_Id);
5264 Set_Etype (Sel, Any_Type);
5265 return;
5267 else
5268 Prefix_Type := Etype (Name);
5269 end if;
5271 if Is_Access_Type (Prefix_Type) then
5273 -- A RACW object can never be used as prefix of a selected component
5274 -- since that means it is dereferenced without being a controlling
5275 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
5276 -- reporting an error, we must check whether this is actually a
5277 -- dispatching call in prefix form.
5279 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
5280 and then Comes_From_Source (N)
5281 then
5282 if Try_Object_Operation (N) then
5283 return;
5284 else
5285 Error_Msg_N
5286 ("invalid dereference of a remote access-to-class-wide value",
5288 end if;
5290 -- Normal case of selected component applied to access type
5292 else
5293 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5294 Prefix_Type := Implicitly_Designated_Type (Prefix_Type);
5295 end if;
5297 -- If we have an explicit dereference of a remote access-to-class-wide
5298 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
5299 -- have to check for the case of a prefix that is a controlling operand
5300 -- of a prefixed dispatching call, as the dereference is legal in that
5301 -- case. Normally this condition is checked in Validate_Remote_Access_
5302 -- To_Class_Wide_Type, but we have to defer the checking for selected
5303 -- component prefixes because of the prefixed dispatching call case.
5304 -- Note that implicit dereferences are checked for this just above.
5306 elsif Nkind (Name) = N_Explicit_Dereference
5307 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
5308 and then Comes_From_Source (N)
5309 then
5310 if Try_Object_Operation (N) then
5311 return;
5312 else
5313 Error_Msg_N
5314 ("invalid dereference of a remote access-to-class-wide value",
5316 end if;
5317 end if;
5319 -- (Ada 2005): if the prefix is the limited view of a type, and
5320 -- the context already includes the full view, use the full view
5321 -- in what follows, either to retrieve a component of to find
5322 -- a primitive operation. If the prefix is an explicit dereference,
5323 -- set the type of the prefix to reflect this transformation.
5324 -- If the nonlimited view is itself an incomplete type, get the
5325 -- full view if available.
5327 if From_Limited_With (Prefix_Type)
5328 and then Has_Non_Limited_View (Prefix_Type)
5329 then
5330 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
5332 if Nkind (N) = N_Explicit_Dereference then
5333 Set_Etype (Prefix (N), Prefix_Type);
5334 end if;
5335 end if;
5337 if Ekind (Prefix_Type) = E_Private_Subtype then
5338 Prefix_Type := Base_Type (Prefix_Type);
5339 end if;
5341 Type_To_Use := Prefix_Type;
5343 -- For class-wide types, use the entity list of the root type. This
5344 -- indirection is specially important for private extensions because
5345 -- only the root type get switched (not the class-wide type).
5347 if Is_Class_Wide_Type (Prefix_Type) then
5348 Type_To_Use := Root_Type (Prefix_Type);
5349 end if;
5351 -- If the prefix is a single concurrent object, use its name in error
5352 -- messages, rather than that of its anonymous type.
5354 Is_Single_Concurrent_Object :=
5355 Is_Concurrent_Type (Prefix_Type)
5356 and then Is_Internal_Name (Chars (Prefix_Type))
5357 and then not Is_Derived_Type (Prefix_Type)
5358 and then Is_Entity_Name (Name);
5360 -- Avoid initializing Comp if that initialization is not needed
5361 -- (and, more importantly, if the call to First_Entity could fail).
5363 if Has_Discriminants (Type_To_Use)
5364 or else Is_Record_Type (Type_To_Use)
5365 or else Is_Private_Type (Type_To_Use)
5366 or else Is_Concurrent_Type (Type_To_Use)
5367 then
5368 Comp := First_Entity (Type_To_Use);
5369 end if;
5371 -- If the selector has an original discriminant, the node appears in
5372 -- an instance. Replace the discriminant with the corresponding one
5373 -- in the current discriminated type. For nested generics, this must
5374 -- be done transitively, so note the new original discriminant.
5376 if Nkind (Sel) = N_Identifier
5377 and then In_Instance
5378 and then Present (Original_Discriminant (Sel))
5379 then
5380 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
5382 -- Mark entity before rewriting, for completeness and because
5383 -- subsequent semantic checks might examine the original node.
5385 Set_Entity (Sel, Comp);
5386 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
5387 Set_Original_Discriminant (Selector_Name (N), Comp);
5388 Set_Etype (N, Etype (Comp));
5389 Check_Implicit_Dereference (N, Etype (Comp));
5391 elsif Is_Record_Type (Prefix_Type) then
5393 -- Find a component with the given name. If the node is a prefixed
5394 -- call, do not examine components whose visibility may be
5395 -- accidental.
5397 while Present (Comp)
5398 and then not Is_Prefixed_Call (N)
5400 -- When the selector has been resolved to a function then we may be
5401 -- looking at a prefixed call which has been preanalyzed already as
5402 -- part of a class condition. In such cases it is possible for a
5403 -- derived type to declare a component which has the same name as
5404 -- a primitive used in a parent's class condition.
5406 -- Avoid seeing components as possible interpretations of the
5407 -- selected component when this is true.
5409 and then not (Inside_Class_Condition_Preanalysis
5410 and then Present (Entity (Sel))
5411 and then Ekind (Entity (Sel)) = E_Function)
5412 loop
5413 if Chars (Comp) = Chars (Sel)
5414 and then Is_Visible_Component (Comp, N)
5415 then
5416 Set_Entity_With_Checks (Sel, Comp);
5417 Set_Etype (Sel, Etype (Comp));
5419 if Ekind (Comp) = E_Discriminant then
5420 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
5421 Error_Msg_N
5422 ("cannot reference discriminant of unchecked union",
5423 Sel);
5424 end if;
5426 if Is_Generic_Type (Prefix_Type)
5427 or else
5428 Is_Generic_Type (Root_Type (Prefix_Type))
5429 then
5430 Set_Original_Discriminant (Sel, Comp);
5431 end if;
5432 end if;
5434 -- Resolve the prefix early otherwise it is not possible to
5435 -- build the actual subtype of the component: it may need
5436 -- to duplicate this prefix and duplication is only allowed
5437 -- on fully resolved expressions.
5439 Resolve (Name);
5441 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
5442 -- subtypes in a package specification.
5443 -- Example:
5445 -- limited with Pkg;
5446 -- package Pkg is
5447 -- type Acc_Inc is access Pkg.T;
5448 -- X : Acc_Inc;
5449 -- N : Natural := X.all.Comp; -- ERROR, limited view
5450 -- end Pkg; -- Comp is not visible
5452 if Nkind (Name) = N_Explicit_Dereference
5453 and then From_Limited_With (Etype (Prefix (Name)))
5454 and then not Is_Potentially_Use_Visible (Etype (Name))
5455 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
5456 N_Package_Specification
5457 then
5458 Error_Msg_NE
5459 ("premature usage of incomplete}", Prefix (Name),
5460 Etype (Prefix (Name)));
5461 end if;
5463 -- We never need an actual subtype for the case of a selection
5464 -- for a indexed component of a non-packed array, since in
5465 -- this case gigi generates all the checks and can find the
5466 -- necessary bounds information.
5468 -- We also do not need an actual subtype for the case of a
5469 -- first, last, length, or range attribute applied to a
5470 -- non-packed array, since gigi can again get the bounds in
5471 -- these cases (gigi cannot handle the packed case, since it
5472 -- has the bounds of the packed array type, not the original
5473 -- bounds of the type). However, if the prefix is itself a
5474 -- selected component, as in a.b.c (i), gigi may regard a.b.c
5475 -- as a dynamic-sized temporary, so we do generate an actual
5476 -- subtype for this case.
5478 Parent_N := Parent (N);
5480 if not Is_Packed (Etype (Comp))
5481 and then
5482 ((Nkind (Parent_N) = N_Indexed_Component
5483 and then Nkind (Name) /= N_Selected_Component)
5484 or else
5485 (Nkind (Parent_N) = N_Attribute_Reference
5486 and then
5487 Attribute_Name (Parent_N) in Name_First
5488 | Name_Last
5489 | Name_Length
5490 | Name_Range))
5491 then
5492 Set_Etype (N, Etype (Comp));
5494 -- If full analysis is not enabled, we do not generate an
5495 -- actual subtype, because in the absence of expansion
5496 -- reference to a formal of a protected type, for example,
5497 -- will not be properly transformed, and will lead to
5498 -- out-of-scope references in gigi.
5500 -- In all other cases, we currently build an actual subtype.
5501 -- It seems likely that many of these cases can be avoided,
5502 -- but right now, the front end makes direct references to the
5503 -- bounds (e.g. in generating a length check), and if we do
5504 -- not make an actual subtype, we end up getting a direct
5505 -- reference to a discriminant, which will not do.
5507 elsif Full_Analysis then
5508 Act_Decl :=
5509 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
5510 Insert_Action (N, Act_Decl);
5512 if No (Act_Decl) then
5513 Set_Etype (N, Etype (Comp));
5515 else
5516 -- If discriminants were present in the component
5517 -- declaration, they have been replaced by the
5518 -- actual values in the prefix object.
5520 declare
5521 Subt : constant Entity_Id :=
5522 Defining_Identifier (Act_Decl);
5523 begin
5524 Set_Etype (Subt, Base_Type (Etype (Comp)));
5525 Set_Etype (N, Subt);
5526 end;
5527 end if;
5529 -- If Etype (Comp) is an access type whose designated subtype
5530 -- is constrained by an unprefixed discriminant value,
5531 -- then ideally we would build a new subtype with an
5532 -- appropriately prefixed discriminant value and use that
5533 -- instead, as is done in Build_Actual_Subtype_Of_Component.
5534 -- That turns out to be difficult in this context (with
5535 -- Full_Analysis = False, we could be processing a selected
5536 -- component that occurs in a Postcondition pragma;
5537 -- PPC pragmas are odd because they can contain references
5538 -- to formal parameters that occur outside the subprogram).
5539 -- So instead we punt on building a new subtype and we
5540 -- use the base type instead. This might introduce
5541 -- correctness problems if N were the target of an
5542 -- assignment (because a required check might be omitted);
5543 -- fortunately, that's impossible because a reference to the
5544 -- current instance of a type does not denote a variable view
5545 -- when the reference occurs within an aspect_specification.
5546 -- GNAT's Precondition and Postcondition pragmas follow the
5547 -- same rules as a Pre or Post aspect_specification.
5549 elsif Has_Discriminant_Dependent_Constraint (Comp)
5550 and then Ekind (Etype (Comp)) = E_Access_Subtype
5551 and then Constraint_Has_Unprefixed_Discriminant_Reference
5552 (Designated_Type (Etype (Comp)))
5553 then
5554 Set_Etype (N, Base_Type (Etype (Comp)));
5556 -- If Full_Analysis not enabled, just set the Etype
5558 else
5559 Set_Etype (N, Etype (Comp));
5560 end if;
5562 Check_Implicit_Dereference (N, Etype (N));
5563 return;
5564 end if;
5566 -- If the prefix is a private extension, check only the visible
5567 -- components of the partial view. This must include the tag,
5568 -- which can appear in expanded code in a tag check.
5570 if Ekind (Type_To_Use) = E_Record_Type_With_Private
5571 and then Chars (Selector_Name (N)) /= Name_uTag
5572 then
5573 exit when Comp = Last_Entity (Type_To_Use);
5574 end if;
5576 Next_Entity (Comp);
5577 end loop;
5579 -- Ada 2005 (AI-252): The selected component can be interpreted as
5580 -- a prefixed view of a subprogram. Depending on the context, this is
5581 -- either a name that can appear in a renaming declaration, or part
5582 -- of an enclosing call given in prefix form.
5584 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
5585 -- selected component should resolve to a name.
5587 -- Extension feature: Also support calls with prefixed views for
5588 -- untagged record types.
5590 if Ada_Version >= Ada_2005
5591 and then (Is_Tagged_Type (Prefix_Type)
5592 or else Core_Extensions_Allowed)
5593 and then not Is_Concurrent_Type (Prefix_Type)
5594 then
5595 if Nkind (Parent (N)) = N_Generic_Association
5596 or else Nkind (Parent (N)) = N_Requeue_Statement
5597 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
5598 then
5599 if Find_Primitive_Operation (N) then
5600 return;
5601 end if;
5603 elsif Try_By_Protected_Procedure_Prefixed_View then
5604 return;
5606 -- If the prefix type is the actual for a formal derived type,
5607 -- or a derived type thereof, the component inherited from the
5608 -- generic parent may not be visible in the actual, but the
5609 -- selected component is legal. This case must be handled before
5610 -- trying the object.operation notation to avoid reporting
5611 -- spurious errors, but must be skipped when Is_Prefixed_Call has
5612 -- been set (because that means that this node was resolved to an
5613 -- Object.Operation call when the generic unit was analyzed).
5615 elsif In_Instance
5616 and then not Is_Prefixed_Call (N)
5617 and then Is_Tagged_Type (Prefix_Type)
5618 and then Try_Selected_Component_In_Instance (Type_To_Use)
5619 then
5620 return;
5622 elsif Try_Object_Operation (N) then
5623 return;
5624 end if;
5626 -- If the transformation fails, it will be necessary to redo the
5627 -- analysis with all errors enabled, to indicate candidate
5628 -- interpretations and reasons for each failure ???
5630 end if;
5632 elsif Is_Private_Type (Prefix_Type) then
5634 -- Allow access only to discriminants of the type. If the type has
5635 -- no full view, gigi uses the parent type for the components, so we
5636 -- do the same here.
5638 if No (Full_View (Prefix_Type)) then
5639 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
5640 Comp := First_Entity (Type_To_Use);
5641 end if;
5643 while Present (Comp) loop
5644 if Chars (Comp) = Chars (Sel) then
5645 if Ekind (Comp) = E_Discriminant then
5646 Set_Entity_With_Checks (Sel, Comp);
5647 Generate_Reference (Comp, Sel);
5649 Set_Etype (Sel, Etype (Comp));
5650 Set_Etype (N, Etype (Comp));
5651 Check_Implicit_Dereference (N, Etype (N));
5653 if Is_Generic_Type (Prefix_Type)
5654 or else Is_Generic_Type (Root_Type (Prefix_Type))
5655 then
5656 Set_Original_Discriminant (Sel, Comp);
5657 end if;
5659 -- Before declaring an error, check whether this is tagged
5660 -- private type and a call to a primitive operation.
5662 elsif Ada_Version >= Ada_2005
5663 and then Is_Tagged_Type (Prefix_Type)
5664 and then Try_Object_Operation (N)
5665 then
5666 return;
5668 else
5669 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5670 Error_Msg_NE ("invisible selector& for }", N, Sel);
5671 Set_Entity (Sel, Any_Id);
5672 Set_Etype (N, Any_Type);
5673 end if;
5675 return;
5676 end if;
5678 Next_Entity (Comp);
5679 end loop;
5681 -- Extension feature: Also support calls with prefixed views for
5682 -- untagged private types.
5684 if Core_Extensions_Allowed then
5685 if Try_Object_Operation (N) then
5686 return;
5687 end if;
5688 end if;
5690 elsif Is_Concurrent_Type (Prefix_Type) then
5692 -- Find visible operation with given name. For a protected type,
5693 -- the possible candidates are discriminants, entries or protected
5694 -- subprograms. For a task type, the set can only include entries or
5695 -- discriminants if the task type is not an enclosing scope. If it
5696 -- is an enclosing scope (e.g. in an inner task) then all entities
5697 -- are visible, but the prefix must denote the enclosing scope, i.e.
5698 -- can only be a direct name or an expanded name.
5700 Set_Etype (Sel, Any_Type);
5701 Hidden_Comp := Empty;
5702 In_Scope := In_Open_Scopes (Prefix_Type);
5703 Is_Private_Op := False;
5705 while Present (Comp) loop
5707 -- Do not examine private operations of the type if not within
5708 -- its scope.
5710 if Chars (Comp) = Chars (Sel) then
5711 if Is_Overloadable (Comp)
5712 and then (In_Scope
5713 or else Comp /= First_Private_Entity (Type_To_Use))
5714 then
5715 Add_One_Interp (Sel, Comp, Etype (Comp));
5716 if Comp = First_Private_Entity (Type_To_Use) then
5717 Is_Private_Op := True;
5718 end if;
5720 -- If the prefix is tagged, the correct interpretation may
5721 -- lie in the primitive or class-wide operations of the
5722 -- type. Perform a simple conformance check to determine
5723 -- whether Try_Object_Operation should be invoked even if
5724 -- a visible entity is found.
5726 if Is_Tagged_Type (Prefix_Type)
5727 and then Nkind (Parent (N)) in N_Function_Call
5728 | N_Indexed_Component
5729 | N_Procedure_Call_Statement
5730 and then Has_Mode_Conformant_Spec (Comp)
5731 then
5732 Has_Candidate := True;
5733 end if;
5735 -- Note: a selected component may not denote a component of a
5736 -- protected type (4.1.3(7)).
5738 elsif Ekind (Comp) in E_Discriminant | E_Entry_Family
5739 or else (In_Scope
5740 and then not Is_Protected_Type (Prefix_Type)
5741 and then Is_Entity_Name (Name))
5742 then
5743 Set_Entity_With_Checks (Sel, Comp);
5744 Generate_Reference (Comp, Sel);
5746 -- The selector is not overloadable, so we have a candidate
5747 -- interpretation.
5749 Has_Candidate := True;
5751 else
5752 if Ekind (Comp) = E_Component then
5753 Hidden_Comp := Comp;
5754 end if;
5756 goto Next_Comp;
5757 end if;
5759 Set_Etype (Sel, Etype (Comp));
5760 Set_Etype (N, Etype (Comp));
5762 if Ekind (Comp) = E_Discriminant then
5763 Set_Original_Discriminant (Sel, Comp);
5764 end if;
5765 end if;
5767 <<Next_Comp>>
5768 if Comp = First_Private_Entity (Type_To_Use) then
5769 if Etype (Sel) /= Any_Type then
5771 -- If the first private entity's name matches, then treat
5772 -- it as a private op: needed for the error check for
5773 -- illegal selection of private entities further below.
5775 if Chars (Comp) = Chars (Sel) then
5776 Is_Private_Op := True;
5777 end if;
5779 -- We have a candidate, so exit the loop
5781 exit;
5783 else
5784 -- Indicate that subsequent operations are private,
5785 -- for better error reporting.
5787 Is_Private_Op := True;
5788 end if;
5789 end if;
5791 -- Do not examine private operations if not within scope of
5792 -- the synchronized type.
5794 exit when not In_Scope
5795 and then
5796 Comp = First_Private_Entity (Base_Type (Prefix_Type));
5797 Next_Entity (Comp);
5798 end loop;
5800 -- If the scope is a current instance, the prefix cannot be an
5801 -- expression of the same type, unless the selector designates a
5802 -- public operation (otherwise that would represent an attempt to
5803 -- reach an internal entity of another synchronized object).
5805 -- This is legal if prefix is an access to such type and there is
5806 -- a dereference, or is a component with a dereferenced prefix.
5807 -- It is also legal if the prefix is a component of a task type,
5808 -- and the selector is one of the task operations.
5810 if In_Scope
5811 and then not Is_Entity_Name (Name)
5812 and then not Has_Dereference (Name)
5813 then
5814 if Is_Task_Type (Prefix_Type)
5815 and then Present (Entity (Sel))
5816 and then Is_Entry (Entity (Sel))
5817 then
5818 null;
5820 elsif Is_Protected_Type (Prefix_Type)
5821 and then Is_Overloadable (Entity (Sel))
5822 and then not Is_Private_Op
5823 then
5824 null;
5826 else
5827 Error_Msg_NE
5828 ("invalid reference to internal operation of some object of "
5829 & "type &", N, Type_To_Use);
5830 Set_Entity (Sel, Any_Id);
5831 Set_Etype (Sel, Any_Type);
5832 return;
5833 end if;
5835 -- Another special case: the prefix may denote an object of the type
5836 -- (but not a type) in which case this is an external call and the
5837 -- operation must be public.
5839 elsif In_Scope
5840 and then Is_Object_Reference (Original_Node (Prefix (N)))
5841 and then Comes_From_Source (N)
5842 and then Is_Private_Op
5843 then
5844 if Present (Hidden_Comp) then
5845 Error_Msg_NE
5846 ("invalid reference to private component of object of type "
5847 & "&", N, Type_To_Use);
5849 else
5850 Error_Msg_NE
5851 ("invalid reference to private operation of some object of "
5852 & "type &", N, Type_To_Use);
5853 end if;
5855 Set_Entity (Sel, Any_Id);
5856 Set_Etype (Sel, Any_Type);
5857 return;
5858 end if;
5860 -- If there is no visible entity with the given name or none of the
5861 -- visible entities are plausible interpretations, check whether
5862 -- there is some other primitive operation with that name.
5864 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
5865 if (Etype (N) = Any_Type
5866 or else not Has_Candidate)
5867 and then Try_Object_Operation (N)
5868 then
5869 return;
5871 -- If the context is not syntactically a procedure call, it
5872 -- may be a call to a primitive function declared outside of
5873 -- the synchronized type.
5875 -- If the context is a procedure call, there might still be
5876 -- an overloading between an entry and a primitive procedure
5877 -- declared outside of the synchronized type, called in prefix
5878 -- notation. This is harder to disambiguate because in one case
5879 -- the controlling formal is implicit ???
5881 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
5882 and then Nkind (Parent (N)) /= N_Indexed_Component
5883 and then Try_Object_Operation (N)
5884 then
5885 return;
5886 end if;
5888 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5889 -- entry or procedure of a tagged concurrent type we must check
5890 -- if there are class-wide subprograms covering the primitive. If
5891 -- true then Try_Object_Operation reports the error.
5893 if Has_Candidate
5894 and then Is_Concurrent_Type (Prefix_Type)
5895 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
5896 then
5897 -- Duplicate the call. This is required to avoid problems with
5898 -- the tree transformations performed by Try_Object_Operation.
5899 -- Set properly the parent of the copied call, because it is
5900 -- about to be reanalyzed.
5902 declare
5903 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5905 begin
5906 Set_Parent (Par, Parent (Parent (N)));
5908 if Try_Object_Operation
5909 (Sinfo.Nodes.Name (Par), CW_Test_Only => True)
5910 then
5911 return;
5912 end if;
5913 end;
5914 end if;
5915 end if;
5917 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
5919 -- Case of a prefix of a protected type: selector might denote
5920 -- an invisible private component.
5922 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5923 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5924 Next_Entity (Comp);
5925 end loop;
5927 if Present (Comp) then
5928 if Is_Single_Concurrent_Object then
5929 Error_Msg_Node_2 := Entity (Name);
5930 Error_Msg_NE ("invisible selector& for &", N, Sel);
5932 else
5933 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5934 Error_Msg_NE ("invisible selector& for }", N, Sel);
5935 end if;
5936 return;
5937 end if;
5938 end if;
5940 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5942 -- Extension feature: Also support calls with prefixed views for
5943 -- untagged types.
5945 elsif Core_Extensions_Allowed
5946 and then Try_Object_Operation (N)
5947 then
5948 return;
5950 else
5951 -- Invalid prefix
5953 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5954 end if;
5956 -- If N still has no type, the component is not defined in the prefix
5958 if Etype (N) = Any_Type then
5960 if Is_Single_Concurrent_Object then
5961 Error_Msg_Node_2 := Entity (Name);
5962 Error_Msg_NE ("no selector& for&", N, Sel);
5964 Check_Misspelled_Selector (Type_To_Use, Sel);
5966 -- If this is a derived formal type, the parent may have different
5967 -- visibility at this point. Try for an inherited component before
5968 -- reporting an error.
5970 elsif Is_Generic_Type (Prefix_Type)
5971 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
5972 and then Prefix_Type /= Etype (Prefix_Type)
5973 and then Is_Record_Type (Etype (Prefix_Type))
5974 then
5975 Set_Etype (Prefix (N), Etype (Prefix_Type));
5976 Analyze_Selected_Component (N);
5977 return;
5979 -- Similarly, if this is the actual for a formal derived type, or
5980 -- a derived type thereof, the component inherited from the generic
5981 -- parent may not be visible in the actual, but the selected
5982 -- component is legal.
5984 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5986 -- Climb up the derivation chain of the generic parent type until
5987 -- we find the proper ancestor type.
5989 if Try_Selected_Component_In_Instance (Type_To_Use) then
5990 return;
5992 -- The search above must have eventually succeeded, since the
5993 -- selected component was legal in the generic.
5995 else
5996 raise Program_Error;
5997 end if;
5999 -- Component not found, specialize error message when appropriate
6001 else
6002 if Ekind (Prefix_Type) = E_Record_Subtype then
6004 -- Check whether this is a component of the base type which
6005 -- is absent from a statically constrained subtype. This will
6006 -- raise constraint error at run time, but is not a compile-
6007 -- time error. When the selector is illegal for base type as
6008 -- well fall through and generate a compilation error anyway.
6010 Comp := First_Component (Base_Type (Prefix_Type));
6011 while Present (Comp) loop
6012 if Chars (Comp) = Chars (Sel)
6013 and then Is_Visible_Component (Comp, Sel)
6014 then
6015 Set_Entity_With_Checks (Sel, Comp);
6016 Generate_Reference (Comp, Sel);
6017 Set_Etype (Sel, Etype (Comp));
6018 Set_Etype (N, Etype (Comp));
6020 -- Emit appropriate message. The node will be replaced
6021 -- by an appropriate raise statement.
6023 -- Note that in GNATprove mode, as with all calls to
6024 -- apply a compile time constraint error, this will be
6025 -- made into an error to simplify the processing of the
6026 -- formal verification backend.
6028 Apply_Compile_Time_Constraint_Error
6029 (N, "component not present in }??",
6030 CE_Discriminant_Check_Failed,
6031 Ent => Prefix_Type,
6032 Emit_Message =>
6033 GNATprove_Mode or not In_Instance_Not_Visible);
6034 return;
6035 end if;
6037 Next_Component (Comp);
6038 end loop;
6040 end if;
6042 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
6043 Error_Msg_NE ("no selector& for}", N, Sel);
6045 -- Add information in the case of an incomplete prefix
6047 if Is_Incomplete_Type (Type_To_Use) then
6048 declare
6049 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
6051 begin
6052 if From_Limited_With (Scope (Type_To_Use)) then
6053 Error_Msg_NE
6054 ("\limited view of& has no components", N, Inc);
6056 else
6057 Error_Msg_NE
6058 ("\premature usage of incomplete type&", N, Inc);
6060 if Nkind (Parent (Inc)) =
6061 N_Incomplete_Type_Declaration
6062 then
6063 -- Record location of premature use in entity so that
6064 -- a continuation message is generated when the
6065 -- completion is seen.
6067 Set_Premature_Use (Parent (Inc), N);
6068 end if;
6069 end if;
6070 end;
6071 end if;
6073 Check_Misspelled_Selector (Type_To_Use, Sel);
6074 end if;
6076 Set_Entity (Sel, Any_Id);
6077 Set_Etype (Sel, Any_Type);
6078 end if;
6079 end Analyze_Selected_Component;
6081 ---------------------------
6082 -- Analyze_Short_Circuit --
6083 ---------------------------
6085 procedure Analyze_Short_Circuit (N : Node_Id) is
6086 L : constant Node_Id := Left_Opnd (N);
6087 R : constant Node_Id := Right_Opnd (N);
6088 Ind : Interp_Index;
6089 It : Interp;
6091 begin
6092 Set_Etype (N, Any_Type);
6093 Analyze_Expression (L);
6094 Analyze_Expression (R);
6096 if not Is_Overloaded (L) then
6097 if Root_Type (Etype (L)) = Standard_Boolean
6098 and then Has_Compatible_Type (R, Etype (L))
6099 then
6100 Add_One_Interp (N, Etype (L), Etype (L));
6101 end if;
6103 else
6104 Get_First_Interp (L, Ind, It);
6105 while Present (It.Typ) loop
6106 if Root_Type (It.Typ) = Standard_Boolean
6107 and then Has_Compatible_Type (R, It.Typ)
6108 then
6109 Add_One_Interp (N, It.Typ, It.Typ);
6110 end if;
6112 Get_Next_Interp (Ind, It);
6113 end loop;
6114 end if;
6116 -- Here we have failed to find an interpretation. Clearly we know that
6117 -- it is not the case that both operands can have an interpretation of
6118 -- Boolean, but this is by far the most likely intended interpretation.
6119 -- So we simply resolve both operands as Booleans, and at least one of
6120 -- these resolutions will generate an error message, and we do not need
6121 -- to give another error message on the short circuit operation itself.
6123 if Etype (N) = Any_Type then
6124 Resolve (L, Standard_Boolean);
6125 Resolve (R, Standard_Boolean);
6126 Set_Etype (N, Standard_Boolean);
6127 end if;
6129 if Style_Check then
6130 if Nkind (L) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
6131 then
6132 Check_Xtra_Parens_Precedence (L);
6133 end if;
6135 if Nkind (R) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
6136 then
6137 Check_Xtra_Parens_Precedence (R);
6138 end if;
6139 end if;
6140 end Analyze_Short_Circuit;
6142 -------------------
6143 -- Analyze_Slice --
6144 -------------------
6146 procedure Analyze_Slice (N : Node_Id) is
6147 D : constant Node_Id := Discrete_Range (N);
6148 P : constant Node_Id := Prefix (N);
6149 Array_Type : Entity_Id;
6150 Index_Type : Entity_Id;
6152 procedure Analyze_Overloaded_Slice;
6153 -- If the prefix is overloaded, select those interpretations that
6154 -- yield a one-dimensional array type.
6156 ------------------------------
6157 -- Analyze_Overloaded_Slice --
6158 ------------------------------
6160 procedure Analyze_Overloaded_Slice is
6161 I : Interp_Index;
6162 It : Interp;
6163 Typ : Entity_Id;
6165 begin
6166 Set_Etype (N, Any_Type);
6168 Get_First_Interp (P, I, It);
6169 while Present (It.Nam) loop
6170 Typ := It.Typ;
6172 if Is_Access_Type (Typ) then
6173 Typ := Designated_Type (Typ);
6174 Error_Msg_NW
6175 (Warn_On_Dereference, "?d?implicit dereference", N);
6176 end if;
6178 if Is_Array_Type (Typ)
6179 and then Number_Dimensions (Typ) = 1
6180 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
6181 then
6182 Add_One_Interp (N, Typ, Typ);
6183 end if;
6185 Get_Next_Interp (I, It);
6186 end loop;
6188 if Etype (N) = Any_Type then
6189 Error_Msg_N ("expect array type in prefix of slice", N);
6190 end if;
6191 end Analyze_Overloaded_Slice;
6193 -- Start of processing for Analyze_Slice
6195 begin
6196 Analyze (P);
6197 Analyze (D);
6199 if Is_Overloaded (P) then
6200 Analyze_Overloaded_Slice;
6202 else
6203 Array_Type := Etype (P);
6204 Set_Etype (N, Any_Type);
6206 if Is_Access_Type (Array_Type) then
6207 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
6208 Array_Type := Implicitly_Designated_Type (Array_Type);
6209 end if;
6211 if not Is_Array_Type (Array_Type) then
6212 Wrong_Type (P, Any_Array);
6214 elsif Number_Dimensions (Array_Type) > 1 then
6215 Error_Msg_N
6216 ("type is not one-dimensional array in slice prefix", N);
6218 else
6219 if Ekind (Array_Type) = E_String_Literal_Subtype then
6220 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
6221 else
6222 Index_Type := Etype (First_Index (Array_Type));
6223 end if;
6225 if not Has_Compatible_Type (D, Index_Type) then
6226 Wrong_Type (D, Index_Type);
6227 else
6228 Set_Etype (N, Array_Type);
6229 end if;
6230 end if;
6231 end if;
6232 end Analyze_Slice;
6234 -----------------------------
6235 -- Analyze_Type_Conversion --
6236 -----------------------------
6238 procedure Analyze_Type_Conversion (N : Node_Id) is
6239 Expr : constant Node_Id := Expression (N);
6240 Mark : constant Entity_Id := Subtype_Mark (N);
6242 Typ : Entity_Id;
6244 begin
6245 -- If Conversion_OK is set, then the Etype is already set, and the only
6246 -- processing required is to analyze the expression. This is used to
6247 -- construct certain "illegal" conversions which are not allowed by Ada
6248 -- semantics, but can be handled by Gigi, see Sinfo for further details.
6250 if Conversion_OK (N) then
6251 Analyze (Expr);
6252 return;
6253 end if;
6255 -- Otherwise full type analysis is required, as well as some semantic
6256 -- checks to make sure the argument of the conversion is appropriate.
6258 Find_Type (Mark);
6259 Typ := Entity (Mark);
6260 Set_Etype (N, Typ);
6262 Analyze_Expression (Expr);
6264 Check_Fully_Declared (Typ, N);
6265 Validate_Remote_Type_Type_Conversion (N);
6267 -- Only remaining step is validity checks on the argument. These
6268 -- are skipped if the conversion does not come from the source.
6270 if not Comes_From_Source (N) then
6271 return;
6273 -- If there was an error in a generic unit, no need to replicate the
6274 -- error message. Conversely, constant-folding in the generic may
6275 -- transform the argument of a conversion into a string literal, which
6276 -- is legal. Therefore the following tests are not performed in an
6277 -- instance. The same applies to an inlined body.
6279 elsif In_Instance or In_Inlined_Body then
6280 return;
6282 elsif Nkind (Expr) = N_Null then
6283 Error_Msg_N ("argument of conversion cannot be null", N);
6284 Error_Msg_N ("\use qualified expression instead", N);
6285 Set_Etype (N, Any_Type);
6287 elsif Nkind (Expr) = N_Aggregate then
6288 Error_Msg_N ("argument of conversion cannot be aggregate", N);
6289 Error_Msg_N ("\use qualified expression instead", N);
6291 elsif Nkind (Expr) = N_Allocator then
6292 Error_Msg_N ("argument of conversion cannot be allocator", N);
6293 Error_Msg_N ("\use qualified expression instead", N);
6295 elsif Nkind (Expr) = N_String_Literal then
6296 Error_Msg_N ("argument of conversion cannot be string literal", N);
6297 Error_Msg_N ("\use qualified expression instead", N);
6299 elsif Nkind (Expr) = N_Character_Literal then
6300 if Ada_Version = Ada_83 then
6301 Resolve (Expr, Typ);
6302 else
6303 Error_Msg_N
6304 ("argument of conversion cannot be character literal", N);
6305 Error_Msg_N ("\use qualified expression instead", N);
6306 end if;
6308 elsif Nkind (Expr) = N_Attribute_Reference
6309 and then Attribute_Name (Expr) in Name_Access
6310 | Name_Unchecked_Access
6311 | Name_Unrestricted_Access
6312 then
6313 Error_Msg_N
6314 ("argument of conversion cannot be access attribute", N);
6315 Error_Msg_N ("\use qualified expression instead", N);
6316 end if;
6318 -- A formal parameter of a specific tagged type whose related subprogram
6319 -- is subject to pragma Extensions_Visible with value "False" cannot
6320 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
6321 -- internally generated expressions.
6323 if Is_Class_Wide_Type (Typ)
6324 and then Comes_From_Source (Expr)
6325 and then Is_EVF_Expression (Expr)
6326 then
6327 Error_Msg_N
6328 ("formal parameter cannot be converted to class-wide type when "
6329 & "Extensions_Visible is False", Expr);
6330 end if;
6331 end Analyze_Type_Conversion;
6333 ----------------------
6334 -- Analyze_Unary_Op --
6335 ----------------------
6337 procedure Analyze_Unary_Op (N : Node_Id) is
6338 R : constant Node_Id := Right_Opnd (N);
6340 Op_Id : Entity_Id;
6342 begin
6343 Set_Etype (N, Any_Type);
6344 Candidate_Type := Empty;
6346 Analyze_Expression (R);
6348 -- If the entity is already set, the node is the instantiation of a
6349 -- generic node with a non-local reference, or was manufactured by a
6350 -- call to Make_Op_xxx. In either case the entity is known to be valid,
6351 -- and we do not need to collect interpretations, instead we just get
6352 -- the single possible interpretation.
6354 if Present (Entity (N)) then
6355 Op_Id := Entity (N);
6357 if Ekind (Op_Id) = E_Operator then
6358 Find_Unary_Types (R, Op_Id, N);
6359 else
6360 Add_One_Interp (N, Op_Id, Etype (Op_Id));
6361 end if;
6363 else
6364 Op_Id := Get_Name_Entity_Id (Chars (N));
6365 while Present (Op_Id) loop
6366 if Ekind (Op_Id) = E_Operator then
6367 if No (Next_Entity (First_Entity (Op_Id))) then
6368 Find_Unary_Types (R, Op_Id, N);
6369 end if;
6371 elsif Is_Overloadable (Op_Id) then
6372 Analyze_User_Defined_Unary_Op (N, Op_Id);
6373 end if;
6375 Op_Id := Homonym (Op_Id);
6376 end loop;
6377 end if;
6379 Operator_Check (N);
6380 end Analyze_Unary_Op;
6382 ----------------------------------
6383 -- Analyze_Unchecked_Expression --
6384 ----------------------------------
6386 procedure Analyze_Unchecked_Expression (N : Node_Id) is
6387 Expr : constant Node_Id := Expression (N);
6389 begin
6390 Analyze (Expr, Suppress => All_Checks);
6391 Set_Etype (N, Etype (Expr));
6392 Save_Interps (Expr, N);
6393 end Analyze_Unchecked_Expression;
6395 ---------------------------------------
6396 -- Analyze_Unchecked_Type_Conversion --
6397 ---------------------------------------
6399 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
6400 Expr : constant Node_Id := Expression (N);
6401 Mark : constant Entity_Id := Subtype_Mark (N);
6403 begin
6404 Find_Type (Mark);
6405 Set_Etype (N, Entity (Mark));
6406 Analyze_Expression (Expr);
6407 end Analyze_Unchecked_Type_Conversion;
6409 ------------------------------------
6410 -- Analyze_User_Defined_Binary_Op --
6411 ------------------------------------
6413 procedure Analyze_User_Defined_Binary_Op
6414 (N : Node_Id;
6415 Op_Id : Entity_Id) is
6416 begin
6417 declare
6418 F1 : constant Entity_Id := First_Formal (Op_Id);
6419 F2 : constant Entity_Id := Next_Formal (F1);
6421 begin
6422 -- Verify that Op_Id is a visible binary function. Note that since
6423 -- we know Op_Id is overloaded, potentially use visible means use
6424 -- visible for sure (RM 9.4(11)). Be prepared for previous errors.
6426 if Ekind (Op_Id) = E_Function
6427 and then Present (F2)
6428 and then (Is_Immediately_Visible (Op_Id)
6429 or else Is_Potentially_Use_Visible (Op_Id))
6430 and then (Has_Compatible_Type (Left_Opnd (N), Etype (F1))
6431 or else Etype (F1) = Any_Type)
6432 and then (Has_Compatible_Type (Right_Opnd (N), Etype (F2))
6433 or else Etype (F2) = Any_Type)
6434 then
6435 Add_One_Interp (N, Op_Id, Base_Type (Etype (Op_Id)));
6437 -- If the operands are overloaded, indicate that the current
6438 -- type is a viable candidate. This is redundant in most cases,
6439 -- but for equality and comparison operators where the context
6440 -- does not impose a type on the operands, setting the proper
6441 -- type is necessary to avoid subsequent ambiguities during
6442 -- resolution, when both user-defined and predefined operators
6443 -- may be candidates.
6445 if Is_Overloaded (Left_Opnd (N)) then
6446 Set_Etype (Left_Opnd (N), Etype (F1));
6447 end if;
6449 if Is_Overloaded (Right_Opnd (N)) then
6450 Set_Etype (Right_Opnd (N), Etype (F2));
6451 end if;
6453 if Debug_Flag_E then
6454 Write_Str ("user defined operator ");
6455 Write_Name (Chars (Op_Id));
6456 Write_Str (" on node ");
6457 Write_Int (Int (N));
6458 Write_Eol;
6459 end if;
6460 end if;
6461 end;
6462 end Analyze_User_Defined_Binary_Op;
6464 -----------------------------------
6465 -- Analyze_User_Defined_Unary_Op --
6466 -----------------------------------
6468 procedure Analyze_User_Defined_Unary_Op
6469 (N : Node_Id;
6470 Op_Id : Entity_Id)
6472 begin
6473 -- Only do analysis if the operator Comes_From_Source, since otherwise
6474 -- the operator was generated by the expander, and all such operators
6475 -- always refer to the operators in package Standard.
6477 if Comes_From_Source (N) then
6478 declare
6479 F : constant Entity_Id := First_Formal (Op_Id);
6481 begin
6482 -- Verify that Op_Id is a visible unary function. Note that since
6483 -- we know Op_Id is overloaded, potentially use visible means use
6484 -- visible for sure (RM 9.4(11)).
6486 if Ekind (Op_Id) = E_Function
6487 and then No (Next_Formal (F))
6488 and then (Is_Immediately_Visible (Op_Id)
6489 or else Is_Potentially_Use_Visible (Op_Id))
6490 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
6491 then
6492 Add_One_Interp (N, Op_Id, Etype (Op_Id));
6493 end if;
6494 end;
6495 end if;
6496 end Analyze_User_Defined_Unary_Op;
6498 ---------------------------
6499 -- Check_Arithmetic_Pair --
6500 ---------------------------
6502 procedure Check_Arithmetic_Pair
6503 (T1, T2 : Entity_Id;
6504 Op_Id : Entity_Id;
6505 N : Node_Id)
6507 Op_Name : constant Name_Id := Chars (Op_Id);
6509 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
6510 -- Check whether the fixed-point type Typ has a user-defined operator
6511 -- (multiplication or division) that should hide the corresponding
6512 -- predefined operator. Used to implement Ada 2005 AI-264, to make
6513 -- such operators more visible and therefore useful.
6515 -- If the name of the operation is an expanded name with prefix
6516 -- Standard, the predefined universal fixed operator is available,
6517 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
6519 ------------------
6520 -- Has_Fixed_Op --
6521 ------------------
6523 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
6524 Bas : constant Entity_Id := Base_Type (Typ);
6525 Ent : Entity_Id;
6526 F1 : Entity_Id;
6527 F2 : Entity_Id;
6529 begin
6530 -- If the universal_fixed operation is given explicitly the rule
6531 -- concerning primitive operations of the type do not apply.
6533 if Nkind (N) = N_Function_Call
6534 and then Nkind (Name (N)) = N_Expanded_Name
6535 and then Entity (Prefix (Name (N))) = Standard_Standard
6536 then
6537 return False;
6538 end if;
6540 -- The operation is treated as primitive if it is declared in the
6541 -- same scope as the type, and therefore on the same entity chain.
6543 Ent := Next_Entity (Typ);
6544 while Present (Ent) loop
6545 if Chars (Ent) = Chars (Op) then
6546 F1 := First_Formal (Ent);
6547 F2 := Next_Formal (F1);
6549 -- The operation counts as primitive if either operand or
6550 -- result are of the given base type, and both operands are
6551 -- fixed point types.
6553 if (Base_Type (Etype (F1)) = Bas
6554 and then Is_Fixed_Point_Type (Etype (F2)))
6556 or else
6557 (Base_Type (Etype (F2)) = Bas
6558 and then Is_Fixed_Point_Type (Etype (F1)))
6560 or else
6561 (Base_Type (Etype (Ent)) = Bas
6562 and then Is_Fixed_Point_Type (Etype (F1))
6563 and then Is_Fixed_Point_Type (Etype (F2)))
6564 then
6565 return True;
6566 end if;
6567 end if;
6569 Next_Entity (Ent);
6570 end loop;
6572 return False;
6573 end Has_Fixed_Op;
6575 -- Start of processing for Check_Arithmetic_Pair
6577 begin
6578 if Op_Name in Name_Op_Add | Name_Op_Subtract then
6579 if Is_Numeric_Type (T1)
6580 and then Is_Numeric_Type (T2)
6581 and then (Covers (T1 => T1, T2 => T2)
6582 or else
6583 Covers (T1 => T2, T2 => T1))
6584 then
6585 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6586 end if;
6588 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide then
6589 if Is_Fixed_Point_Type (T1)
6590 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
6591 then
6592 -- Add one interpretation with universal fixed result
6594 if not Has_Fixed_Op (T1, Op_Id)
6595 or else Nkind (Parent (N)) = N_Type_Conversion
6596 then
6597 Add_One_Interp (N, Op_Id, Universal_Fixed);
6598 end if;
6600 elsif Is_Fixed_Point_Type (T2)
6601 and then T1 = Universal_Real
6602 and then
6603 (not Has_Fixed_Op (T1, Op_Id)
6604 or else Nkind (Parent (N)) = N_Type_Conversion)
6605 then
6606 Add_One_Interp (N, Op_Id, Universal_Fixed);
6608 elsif Is_Numeric_Type (T1)
6609 and then Is_Numeric_Type (T2)
6610 and then (Covers (T1 => T1, T2 => T2)
6611 or else
6612 Covers (T1 => T2, T2 => T1))
6613 then
6614 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6616 elsif Is_Fixed_Point_Type (T1)
6617 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6618 or else T2 = Universal_Integer)
6619 then
6620 Add_One_Interp (N, Op_Id, T1);
6622 elsif T2 = Universal_Real
6623 and then Base_Type (T1) = Base_Type (Standard_Integer)
6624 and then Op_Name = Name_Op_Multiply
6625 then
6626 Add_One_Interp (N, Op_Id, Any_Fixed);
6628 elsif T1 = Universal_Real
6629 and then Base_Type (T2) = Base_Type (Standard_Integer)
6630 then
6631 Add_One_Interp (N, Op_Id, Any_Fixed);
6633 elsif Is_Fixed_Point_Type (T2)
6634 and then (Base_Type (T1) = Base_Type (Standard_Integer)
6635 or else T1 = Universal_Integer)
6636 and then Op_Name = Name_Op_Multiply
6637 then
6638 Add_One_Interp (N, Op_Id, T2);
6640 elsif T1 = Universal_Real and then T2 = Universal_Integer then
6641 Add_One_Interp (N, Op_Id, T1);
6643 elsif T2 = Universal_Real
6644 and then T1 = Universal_Integer
6645 and then Op_Name = Name_Op_Multiply
6646 then
6647 Add_One_Interp (N, Op_Id, T2);
6648 end if;
6650 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
6652 if Is_Integer_Type (T1)
6653 and then (Covers (T1 => T1, T2 => T2)
6654 or else
6655 Covers (T1 => T2, T2 => T1))
6656 then
6657 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6658 end if;
6660 elsif Op_Name = Name_Op_Expon then
6661 if Is_Numeric_Type (T1)
6662 and then not Is_Fixed_Point_Type (T1)
6663 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6664 or else T2 = Universal_Integer)
6665 then
6666 Add_One_Interp (N, Op_Id, Base_Type (T1));
6667 end if;
6669 else pragma Assert (Nkind (N) in N_Op_Shift);
6671 -- If not one of the predefined operators, the node may be one
6672 -- of the intrinsic functions. Its kind is always specific, and
6673 -- we can use it directly, rather than the name of the operation.
6675 if Is_Integer_Type (T1)
6676 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6677 or else T2 = Universal_Integer)
6678 then
6679 Add_One_Interp (N, Op_Id, Base_Type (T1));
6680 end if;
6681 end if;
6682 end Check_Arithmetic_Pair;
6684 -------------------------------
6685 -- Check_Misspelled_Selector --
6686 -------------------------------
6688 procedure Check_Misspelled_Selector
6689 (Prefix : Entity_Id;
6690 Sel : Node_Id)
6692 Max_Suggestions : constant := 2;
6693 Nr_Of_Suggestions : Natural := 0;
6695 Suggestion_1 : Entity_Id := Empty;
6696 Suggestion_2 : Entity_Id := Empty;
6698 Comp : Entity_Id;
6700 begin
6701 -- All the components of the prefix of selector Sel are matched against
6702 -- Sel and a count is maintained of possible misspellings. When at
6703 -- the end of the analysis there are one or two (not more) possible
6704 -- misspellings, these misspellings will be suggested as possible
6705 -- correction.
6707 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
6709 -- Concurrent types should be handled as well ???
6711 return;
6712 end if;
6714 Comp := First_Entity (Prefix);
6715 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
6716 if Is_Visible_Component (Comp, Sel) then
6717 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
6718 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
6720 case Nr_Of_Suggestions is
6721 when 1 => Suggestion_1 := Comp;
6722 when 2 => Suggestion_2 := Comp;
6723 when others => null;
6724 end case;
6725 end if;
6726 end if;
6728 Next_Entity (Comp);
6729 end loop;
6731 -- Report at most two suggestions
6733 if Nr_Of_Suggestions = 1 then
6734 Error_Msg_NE -- CODEFIX
6735 ("\possible misspelling of&", Sel, Suggestion_1);
6737 elsif Nr_Of_Suggestions = 2 then
6738 Error_Msg_Node_2 := Suggestion_2;
6739 Error_Msg_NE -- CODEFIX
6740 ("\possible misspelling of& or&", Sel, Suggestion_1);
6741 end if;
6742 end Check_Misspelled_Selector;
6744 -------------------
6745 -- Diagnose_Call --
6746 -------------------
6748 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
6749 Actual : Node_Id;
6750 X : Interp_Index;
6751 It : Interp;
6752 Err_Mode : Boolean;
6753 New_Nam : Node_Id;
6754 Num_Actuals : Natural;
6755 Num_Interps : Natural;
6756 Void_Interp_Seen : Boolean := False;
6758 Success : Boolean;
6759 pragma Warnings (Off, Boolean);
6761 begin
6762 Num_Actuals := 0;
6763 Actual := First_Actual (N);
6765 while Present (Actual) loop
6766 -- Ada 2005 (AI-50217): Post an error in case of premature
6767 -- usage of an entity from the limited view.
6769 if not Analyzed (Etype (Actual))
6770 and then From_Limited_With (Etype (Actual))
6771 and then Ada_Version >= Ada_2005
6772 then
6773 Error_Msg_Qual_Level := 1;
6774 Error_Msg_NE
6775 ("missing with_clause for scope of imported type&",
6776 Actual, Etype (Actual));
6777 Error_Msg_Qual_Level := 0;
6778 end if;
6780 Num_Actuals := Num_Actuals + 1;
6781 Next_Actual (Actual);
6782 end loop;
6784 -- Before listing the possible candidates, check whether this is
6785 -- a prefix of a selected component that has been rewritten as a
6786 -- parameterless function call because there is a callable candidate
6787 -- interpretation. If there is a hidden package in the list of homonyms
6788 -- of the function name (bad programming style in any case) suggest that
6789 -- this is the intended entity.
6791 if No (Parameter_Associations (N))
6792 and then Nkind (Parent (N)) = N_Selected_Component
6793 and then Nkind (Parent (Parent (N))) in N_Declaration
6794 and then Is_Overloaded (Nam)
6795 then
6796 declare
6797 Ent : Entity_Id;
6799 begin
6800 Ent := Current_Entity (Nam);
6801 while Present (Ent) loop
6802 if Ekind (Ent) = E_Package then
6803 Error_Msg_N
6804 ("no legal interpretations as function call,!", Nam);
6805 Error_Msg_NE ("\package& is not visible", N, Ent);
6807 Rewrite (Parent (N),
6808 New_Occurrence_Of (Any_Type, Sloc (N)));
6809 return;
6810 end if;
6812 Ent := Homonym (Ent);
6813 end loop;
6814 end;
6815 end if;
6817 -- If this is a call to an operation of a concurrent type, the failed
6818 -- interpretations have been removed from the name. Recover them now
6819 -- in order to provide full diagnostics.
6821 if Nkind (Parent (Nam)) = N_Selected_Component then
6822 Set_Entity (Nam, Empty);
6823 New_Nam := New_Copy_Tree (Parent (Nam));
6824 Set_Is_Overloaded (New_Nam, False);
6825 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6826 Set_Parent (New_Nam, Parent (Parent (Nam)));
6827 Analyze_Selected_Component (New_Nam);
6828 Get_First_Interp (Selector_Name (New_Nam), X, It);
6829 else
6830 Get_First_Interp (Nam, X, It);
6831 end if;
6833 -- If the number of actuals is 2, then remove interpretations involving
6834 -- a unary "+" operator as they might yield confusing errors downstream.
6836 if Num_Actuals = 2
6837 and then Nkind (Parent (Nam)) /= N_Selected_Component
6838 then
6839 Num_Interps := 0;
6841 while Present (It.Nam) loop
6842 if Ekind (It.Nam) = E_Operator
6843 and then Chars (It.Nam) = Name_Op_Add
6844 and then (No (First_Formal (It.Nam))
6845 or else No (Next_Formal (First_Formal (It.Nam))))
6846 then
6847 Remove_Interp (X);
6848 else
6849 Num_Interps := Num_Interps + 1;
6850 end if;
6852 Get_Next_Interp (X, It);
6853 end loop;
6855 if Num_Interps = 0 then
6856 Error_Msg_N ("!too many arguments in call to&", Nam);
6857 return;
6858 end if;
6860 Get_First_Interp (Nam, X, It);
6862 else
6863 Num_Interps := 2; -- at least
6864 end if;
6866 -- Analyze each candidate call again with full error reporting for each
6868 if Num_Interps > 1 then
6869 Error_Msg_N ("!no candidate interpretations match the actuals:", Nam);
6870 end if;
6872 Err_Mode := All_Errors_Mode;
6873 All_Errors_Mode := True;
6875 while Present (It.Nam) loop
6876 if Etype (It.Nam) = Standard_Void_Type then
6877 Void_Interp_Seen := True;
6878 end if;
6880 Analyze_One_Call (N, It.Nam, True, Success);
6881 Get_Next_Interp (X, It);
6882 end loop;
6884 if Nkind (N) = N_Function_Call then
6885 Get_First_Interp (Nam, X, It);
6887 if No (It.Typ)
6888 and then Ekind (Entity (Name (N))) = E_Function
6889 and then Present (Homonym (Entity (Name (N))))
6890 then
6891 -- A name may appear overloaded if it has a homonym, even if that
6892 -- homonym is non-overloadable, in which case the overload list is
6893 -- in fact empty. This specialized case deserves a special message
6894 -- if the homonym is a child package.
6896 declare
6897 Nam : constant Node_Id := Name (N);
6898 H : constant Entity_Id := Homonym (Entity (Nam));
6900 begin
6901 if Ekind (H) = E_Package and then Is_Child_Unit (H) then
6902 Error_Msg_Qual_Level := 2;
6903 Error_Msg_NE ("if an entity in package& is meant, ", Nam, H);
6904 Error_Msg_NE ("\use a fully qualified name", Nam, H);
6905 Error_Msg_Qual_Level := 0;
6906 end if;
6907 end;
6909 else
6910 while Present (It.Nam) loop
6911 if Ekind (It.Nam) in E_Function | E_Operator then
6912 return;
6913 else
6914 Get_Next_Interp (X, It);
6915 end if;
6916 end loop;
6918 -- If all interpretations are procedures, this deserves a more
6919 -- precise message. Ditto if this appears as the prefix of a
6920 -- selected component, which may be a lexical error.
6922 Error_Msg_N
6923 ("\context requires function call, found procedure name", Nam);
6925 if Nkind (Parent (N)) = N_Selected_Component
6926 and then N = Prefix (Parent (N))
6927 then
6928 Error_Msg_N -- CODEFIX
6929 ("\period should probably be semicolon", Parent (N));
6930 end if;
6931 end if;
6933 elsif Nkind (N) = N_Procedure_Call_Statement
6934 and then not Void_Interp_Seen
6935 then
6936 Error_Msg_N ("\function name found in procedure call", Nam);
6937 end if;
6939 All_Errors_Mode := Err_Mode;
6940 end Diagnose_Call;
6942 ---------------------------
6943 -- Find_Arithmetic_Types --
6944 ---------------------------
6946 procedure Find_Arithmetic_Types
6947 (L, R : Node_Id;
6948 Op_Id : Entity_Id;
6949 N : Node_Id)
6951 procedure Check_Right_Argument (T : Entity_Id);
6952 -- Check right operand of operator
6954 --------------------------
6955 -- Check_Right_Argument --
6956 --------------------------
6958 procedure Check_Right_Argument (T : Entity_Id) is
6959 I : Interp_Index;
6960 It : Interp;
6962 begin
6963 if not Is_Overloaded (R) then
6964 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6966 else
6967 Get_First_Interp (R, I, It);
6968 while Present (It.Typ) loop
6969 Check_Arithmetic_Pair (T, It.Typ, Op_Id, N);
6970 Get_Next_Interp (I, It);
6971 end loop;
6972 end if;
6973 end Check_Right_Argument;
6975 -- Local variables
6977 I : Interp_Index;
6978 It : Interp;
6980 -- Start of processing for Find_Arithmetic_Types
6982 begin
6983 if not Is_Overloaded (L) then
6984 Check_Right_Argument (Etype (L));
6986 else
6987 Get_First_Interp (L, I, It);
6988 while Present (It.Typ) loop
6989 Check_Right_Argument (It.Typ);
6990 Get_Next_Interp (I, It);
6991 end loop;
6992 end if;
6993 end Find_Arithmetic_Types;
6995 ------------------------
6996 -- Find_Boolean_Types --
6997 ------------------------
6999 procedure Find_Boolean_Types
7000 (L, R : Node_Id;
7001 Op_Id : Entity_Id;
7002 N : Node_Id)
7004 procedure Check_Boolean_Pair (T1, T2 : Entity_Id);
7005 -- Check operand pair of operator
7007 procedure Check_Right_Argument (T : Entity_Id);
7008 -- Check right operand of operator
7010 ------------------------
7011 -- Check_Boolean_Pair --
7012 ------------------------
7014 procedure Check_Boolean_Pair (T1, T2 : Entity_Id) is
7015 T : Entity_Id;
7017 begin
7018 if Valid_Boolean_Arg (T1)
7019 and then Valid_Boolean_Arg (T2)
7020 and then (Covers (T1 => T1, T2 => T2)
7021 or else Covers (T1 => T2, T2 => T1))
7022 then
7023 T := Specific_Type (T1, T2);
7025 if T = Universal_Integer then
7026 T := Any_Modular;
7027 end if;
7029 Add_One_Interp (N, Op_Id, T);
7030 end if;
7031 end Check_Boolean_Pair;
7033 --------------------------
7034 -- Check_Right_Argument --
7035 --------------------------
7037 procedure Check_Right_Argument (T : Entity_Id) is
7038 I : Interp_Index;
7039 It : Interp;
7041 begin
7042 -- Defend against previous error
7044 if Nkind (R) = N_Error then
7045 null;
7047 elsif not Is_Overloaded (R) then
7048 Check_Boolean_Pair (T, Etype (R));
7050 else
7051 Get_First_Interp (R, I, It);
7052 while Present (It.Typ) loop
7053 Check_Boolean_Pair (T, It.Typ);
7054 Get_Next_Interp (I, It);
7055 end loop;
7056 end if;
7057 end Check_Right_Argument;
7059 -- Local variables
7061 I : Interp_Index;
7062 It : Interp;
7064 -- Start of processing for Find_Boolean_Types
7066 begin
7067 if not Is_Overloaded (L) then
7068 Check_Right_Argument (Etype (L));
7070 else
7071 Get_First_Interp (L, I, It);
7072 while Present (It.Typ) loop
7073 Check_Right_Argument (It.Typ);
7074 Get_Next_Interp (I, It);
7075 end loop;
7076 end if;
7077 end Find_Boolean_Types;
7079 ------------------------------------
7080 -- Find_Comparison_Equality_Types --
7081 ------------------------------------
7083 -- The context of the operator plays no role in resolving the operands,
7084 -- so that if there is more than one interpretation of the operands that
7085 -- is compatible with the comparison or equality, then the operation is
7086 -- ambiguous, but this cannot be reported at this point because there is
7087 -- no guarantee that the operation will be resolved to this operator yet.
7089 procedure Find_Comparison_Equality_Types
7090 (L, R : Node_Id;
7091 Op_Id : Entity_Id;
7092 N : Node_Id)
7094 Op_Name : constant Name_Id := Chars (Op_Id);
7095 Op_Typ : Entity_Id renames Standard_Boolean;
7097 function Try_Left_Interp (T : Entity_Id) return Entity_Id;
7098 -- Try an interpretation of the left operand with type T. Return the
7099 -- type of the interpretation of the right operand making up a valid
7100 -- operand pair, or else Any_Type if the right operand is ambiguous,
7101 -- otherwise Empty if no such pair exists.
7103 function Is_Valid_Comparison_Type (T : Entity_Id) return Boolean;
7104 -- Return true if T is a valid comparison type
7106 function Is_Valid_Equality_Type
7107 (T : Entity_Id;
7108 Anon_Access : Boolean) return Boolean;
7109 -- Return true if T is a valid equality type
7111 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean;
7112 -- Return true if T1 and T2 constitute a valid pair of operand types for
7113 -- L and R respectively.
7115 ---------------------
7116 -- Try_Left_Interp --
7117 ---------------------
7119 function Try_Left_Interp (T : Entity_Id) return Entity_Id is
7120 I : Interp_Index;
7121 It : Interp;
7122 R_Typ : Entity_Id;
7123 Valid_I : Interp_Index;
7125 begin
7126 -- Defend against previous error
7128 if Nkind (R) = N_Error then
7129 null;
7131 -- Loop through the interpretations of the right operand
7133 elsif not Is_Overloaded (R) then
7134 if Is_Valid_Pair (T, Etype (R)) then
7135 return Etype (R);
7136 end if;
7138 else
7139 R_Typ := Empty;
7140 Valid_I := 0;
7142 Get_First_Interp (R, I, It);
7143 while Present (It.Typ) loop
7144 if Is_Valid_Pair (T, It.Typ) then
7145 -- If several interpretations are possible, disambiguate
7147 if Present (R_Typ)
7148 and then Base_Type (It.Typ) /= Base_Type (R_Typ)
7149 then
7150 It := Disambiguate (R, Valid_I, I, Any_Type);
7152 if It = No_Interp then
7153 R_Typ := Any_Type;
7154 exit;
7155 end if;
7157 else
7158 Valid_I := I;
7159 end if;
7161 R_Typ := It.Typ;
7162 end if;
7164 Get_Next_Interp (I, It);
7165 end loop;
7167 if Present (R_Typ) then
7168 return R_Typ;
7169 end if;
7170 end if;
7172 return Empty;
7173 end Try_Left_Interp;
7175 ------------------------------
7176 -- Is_Valid_Comparison_Type --
7177 ------------------------------
7179 function Is_Valid_Comparison_Type (T : Entity_Id) return Boolean is
7180 begin
7181 -- The operation must be performed in a context where the operators
7182 -- of the base type are visible.
7184 if Is_Visible_Operator (N, Base_Type (T)) then
7185 null;
7187 -- Save candidate type for subsequent error message, if any
7189 else
7190 if Valid_Comparison_Arg (T) then
7191 Candidate_Type := T;
7192 end if;
7194 return False;
7195 end if;
7197 -- Defer to the common implementation for the rest
7199 return Valid_Comparison_Arg (T);
7200 end Is_Valid_Comparison_Type;
7202 ----------------------------
7203 -- Is_Valid_Equality_Type --
7204 ----------------------------
7206 function Is_Valid_Equality_Type
7207 (T : Entity_Id;
7208 Anon_Access : Boolean) return Boolean
7210 begin
7211 -- The operation must be performed in a context where the operators
7212 -- of the base type are visible. Deal with special types used with
7213 -- access types before type resolution is done.
7215 if Ekind (T) = E_Access_Attribute_Type
7216 or else (Ekind (T) in E_Access_Subprogram_Type
7217 | E_Access_Protected_Subprogram_Type
7218 and then
7219 Ekind (Designated_Type (T)) /= E_Subprogram_Type)
7220 or else Is_Visible_Operator (N, Base_Type (T))
7221 then
7222 null;
7224 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
7225 -- anonymous access types in universal_access equality operators.
7227 elsif Anon_Access then
7228 if Ada_Version < Ada_2005 then
7229 return False;
7230 end if;
7232 -- Save candidate type for subsequent error message, if any
7234 else
7235 if Valid_Equality_Arg (T) then
7236 Candidate_Type := T;
7237 end if;
7239 return False;
7240 end if;
7242 -- For the use of a "/=" operator on a tagged type, several possible
7243 -- interpretations of equality need to be considered, we don't want
7244 -- the default inequality declared in Standard to be chosen, and the
7245 -- "/=" operator will be rewritten as a negation of "=" (see the end
7246 -- of Analyze_Comparison_Equality_Op). This ensures the rewriting
7247 -- occurs during analysis rather than being delayed until expansion.
7248 -- Note that, if the node is N_Op_Ne but Op_Id is Name_Op_Eq, then we
7249 -- still proceed with the interpretation, because this indicates
7250 -- the aforementioned rewriting case where the interpretation to be
7251 -- considered is actually that of the "=" operator.
7253 if Nkind (N) = N_Op_Ne
7254 and then Op_Name /= Name_Op_Eq
7255 and then Is_Tagged_Type (T)
7256 then
7257 return False;
7259 -- Defer to the common implementation for the rest
7261 else
7262 return Valid_Equality_Arg (T);
7263 end if;
7264 end Is_Valid_Equality_Type;
7266 -------------------
7267 -- Is_Valid_Pair --
7268 -------------------
7270 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean is
7271 begin
7272 if Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne then
7273 declare
7274 Anon_Access : constant Boolean :=
7275 Is_Anonymous_Access_Type (T1)
7276 or else Is_Anonymous_Access_Type (T2);
7277 -- RM 4.5.2(9.1/2): At least one of the operands of an equality
7278 -- operator for universal_access shall be of specific anonymous
7279 -- access type.
7281 begin
7282 if not Is_Valid_Equality_Type (T1, Anon_Access)
7283 or else not Is_Valid_Equality_Type (T2, Anon_Access)
7284 then
7285 return False;
7286 end if;
7287 end;
7289 else
7290 if not Is_Valid_Comparison_Type (T1)
7291 or else not Is_Valid_Comparison_Type (T2)
7292 then
7293 return False;
7294 end if;
7295 end if;
7297 return Covers (T1 => T1, T2 => T2)
7298 or else Covers (T1 => T2, T2 => T1)
7299 or else Is_User_Defined_Literal (L, T2)
7300 or else Is_User_Defined_Literal (R, T1);
7301 end Is_Valid_Pair;
7303 -- Local variables
7305 I : Interp_Index;
7306 It : Interp;
7307 L_Typ : Entity_Id;
7308 R_Typ : Entity_Id;
7309 T : Entity_Id;
7310 Valid_I : Interp_Index;
7312 -- Start of processing for Find_Comparison_Equality_Types
7314 begin
7315 -- Loop through the interpretations of the left operand
7317 if not Is_Overloaded (L) then
7318 T := Try_Left_Interp (Etype (L));
7320 if Present (T) then
7321 Set_Etype (R, T);
7322 Add_One_Interp (N, Op_Id, Op_Typ, Find_Unique_Type (L, R));
7323 end if;
7325 else
7326 L_Typ := Empty;
7327 R_Typ := Empty;
7328 Valid_I := 0;
7330 Get_First_Interp (L, I, It);
7331 while Present (It.Typ) loop
7332 T := Try_Left_Interp (It.Typ);
7334 if Present (T) then
7335 -- If several interpretations are possible, disambiguate
7337 if Present (L_Typ)
7338 and then Base_Type (It.Typ) /= Base_Type (L_Typ)
7339 then
7340 It := Disambiguate (L, Valid_I, I, Any_Type);
7342 if It = No_Interp then
7343 L_Typ := Any_Type;
7344 R_Typ := T;
7345 exit;
7346 end if;
7348 else
7349 Valid_I := I;
7350 end if;
7352 L_Typ := It.Typ;
7353 R_Typ := T;
7354 end if;
7356 Get_Next_Interp (I, It);
7357 end loop;
7359 if Present (L_Typ) then
7360 Set_Etype (L, L_Typ);
7361 Set_Etype (R, R_Typ);
7362 Add_One_Interp (N, Op_Id, Op_Typ, Find_Unique_Type (L, R));
7363 end if;
7364 end if;
7365 end Find_Comparison_Equality_Types;
7367 ------------------------------
7368 -- Find_Concatenation_Types --
7369 ------------------------------
7371 procedure Find_Concatenation_Types
7372 (L, R : Node_Id;
7373 Op_Id : Entity_Id;
7374 N : Node_Id)
7376 Is_String : constant Boolean := Nkind (L) = N_String_Literal
7377 or else
7378 Nkind (R) = N_String_Literal;
7379 Op_Type : constant Entity_Id := Etype (Op_Id);
7381 begin
7382 if Is_Array_Type (Op_Type)
7384 -- Small but very effective optimization: if at least one operand is a
7385 -- string literal, then the type of the operator must be either array
7386 -- of characters or array of strings.
7388 and then (not Is_String
7389 or else
7390 Is_Character_Type (Component_Type (Op_Type))
7391 or else
7392 Is_String_Type (Component_Type (Op_Type)))
7394 and then not Is_Limited_Type (Op_Type)
7396 and then (Has_Compatible_Type (L, Op_Type)
7397 or else
7398 Has_Compatible_Type (L, Component_Type (Op_Type)))
7400 and then (Has_Compatible_Type (R, Op_Type)
7401 or else
7402 Has_Compatible_Type (R, Component_Type (Op_Type)))
7403 then
7404 Add_One_Interp (N, Op_Id, Op_Type);
7405 end if;
7406 end Find_Concatenation_Types;
7408 -------------------------
7409 -- Find_Negation_Types --
7410 -------------------------
7412 procedure Find_Negation_Types
7413 (R : Node_Id;
7414 Op_Id : Entity_Id;
7415 N : Node_Id)
7417 Index : Interp_Index;
7418 It : Interp;
7420 begin
7421 if not Is_Overloaded (R) then
7422 if Etype (R) = Universal_Integer then
7423 Add_One_Interp (N, Op_Id, Any_Modular);
7424 elsif Valid_Boolean_Arg (Etype (R)) then
7425 Add_One_Interp (N, Op_Id, Etype (R));
7426 end if;
7428 else
7429 Get_First_Interp (R, Index, It);
7430 while Present (It.Typ) loop
7431 if Valid_Boolean_Arg (It.Typ) then
7432 Add_One_Interp (N, Op_Id, It.Typ);
7433 end if;
7435 Get_Next_Interp (Index, It);
7436 end loop;
7437 end if;
7438 end Find_Negation_Types;
7440 ------------------------------
7441 -- Find_Primitive_Operation --
7442 ------------------------------
7444 function Find_Primitive_Operation (N : Node_Id) return Boolean is
7445 Obj : constant Node_Id := Prefix (N);
7446 Op : constant Node_Id := Selector_Name (N);
7448 Prim : Elmt_Id;
7449 Prims : Elist_Id;
7450 Typ : Entity_Id;
7452 begin
7453 Set_Etype (Op, Any_Type);
7455 if Is_Access_Type (Etype (Obj)) then
7456 Typ := Designated_Type (Etype (Obj));
7457 else
7458 Typ := Etype (Obj);
7459 end if;
7461 if Is_Class_Wide_Type (Typ) then
7462 Typ := Root_Type (Typ);
7463 end if;
7465 Prims := Primitive_Operations (Typ);
7467 Prim := First_Elmt (Prims);
7468 while Present (Prim) loop
7469 if Chars (Node (Prim)) = Chars (Op) then
7470 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
7471 Set_Etype (N, Etype (Node (Prim)));
7472 end if;
7474 Next_Elmt (Prim);
7475 end loop;
7477 -- Now look for class-wide operations of the type or any of its
7478 -- ancestors by iterating over the homonyms of the selector.
7480 declare
7481 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
7482 Hom : Entity_Id;
7484 begin
7485 Hom := Current_Entity (Op);
7486 while Present (Hom) loop
7487 if (Ekind (Hom) = E_Procedure
7488 or else
7489 Ekind (Hom) = E_Function)
7490 and then Scope (Hom) = Scope (Typ)
7491 and then Present (First_Formal (Hom))
7492 and then
7493 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7494 or else
7495 (Is_Access_Type (Etype (First_Formal (Hom)))
7496 and then
7497 Ekind (Etype (First_Formal (Hom))) =
7498 E_Anonymous_Access_Type
7499 and then
7500 Base_Type
7501 (Designated_Type (Etype (First_Formal (Hom)))) =
7502 Cls_Type))
7503 then
7504 Add_One_Interp (Op, Hom, Etype (Hom));
7505 Set_Etype (N, Etype (Hom));
7506 end if;
7508 Hom := Homonym (Hom);
7509 end loop;
7510 end;
7512 return Etype (Op) /= Any_Type;
7513 end Find_Primitive_Operation;
7515 ----------------------
7516 -- Find_Unary_Types --
7517 ----------------------
7519 procedure Find_Unary_Types
7520 (R : Node_Id;
7521 Op_Id : Entity_Id;
7522 N : Node_Id)
7524 Index : Interp_Index;
7525 It : Interp;
7527 begin
7528 if not Is_Overloaded (R) then
7529 if Is_Numeric_Type (Etype (R)) then
7531 -- In an instance a generic actual may be a numeric type even if
7532 -- the formal in the generic unit was not. In that case, the
7533 -- predefined operator was not a possible interpretation in the
7534 -- generic, and cannot be one in the instance, unless the operator
7535 -- is an actual of an instance.
7537 if In_Instance
7538 and then
7539 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
7540 then
7541 null;
7542 else
7543 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
7544 end if;
7545 end if;
7547 else
7548 Get_First_Interp (R, Index, It);
7549 while Present (It.Typ) loop
7550 if Is_Numeric_Type (It.Typ) then
7551 if In_Instance
7552 and then
7553 not Is_Numeric_Type
7554 (Corresponding_Generic_Type (Etype (It.Typ)))
7555 then
7556 null;
7558 else
7559 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
7560 end if;
7561 end if;
7563 Get_Next_Interp (Index, It);
7564 end loop;
7565 end if;
7566 end Find_Unary_Types;
7568 ------------------
7569 -- Junk_Operand --
7570 ------------------
7572 function Junk_Operand (N : Node_Id) return Boolean is
7573 Enode : Node_Id;
7575 begin
7576 if Error_Posted (N) then
7577 return False;
7578 end if;
7580 -- Get entity to be tested
7582 if Is_Entity_Name (N)
7583 and then Present (Entity (N))
7584 then
7585 Enode := N;
7587 -- An odd case, a procedure name gets converted to a very peculiar
7588 -- function call, and here is where we detect this happening.
7590 elsif Nkind (N) = N_Function_Call
7591 and then Is_Entity_Name (Name (N))
7592 and then Present (Entity (Name (N)))
7593 then
7594 Enode := Name (N);
7596 -- Another odd case, there are at least some cases of selected
7597 -- components where the selected component is not marked as having
7598 -- an entity, even though the selector does have an entity
7600 elsif Nkind (N) = N_Selected_Component
7601 and then Present (Entity (Selector_Name (N)))
7602 then
7603 Enode := Selector_Name (N);
7605 else
7606 return False;
7607 end if;
7609 -- Now test the entity we got to see if it is a bad case
7611 case Ekind (Entity (Enode)) is
7612 when E_Package =>
7613 Error_Msg_N
7614 ("package name cannot be used as operand", Enode);
7616 when Generic_Unit_Kind =>
7617 Error_Msg_N
7618 ("generic unit name cannot be used as operand", Enode);
7620 when Type_Kind =>
7621 Error_Msg_N
7622 ("subtype name cannot be used as operand", Enode);
7624 when Entry_Kind =>
7625 Error_Msg_N
7626 ("entry name cannot be used as operand", Enode);
7628 when E_Procedure =>
7629 Error_Msg_N
7630 ("procedure name cannot be used as operand", Enode);
7632 when E_Exception =>
7633 Error_Msg_N
7634 ("exception name cannot be used as operand", Enode);
7636 when E_Block
7637 | E_Label
7638 | E_Loop
7640 Error_Msg_N
7641 ("label name cannot be used as operand", Enode);
7643 when others =>
7644 return False;
7645 end case;
7647 return True;
7648 end Junk_Operand;
7650 --------------------
7651 -- Operator_Check --
7652 --------------------
7654 procedure Operator_Check (N : Node_Id) is
7655 begin
7656 Remove_Abstract_Operations (N);
7658 -- Test for case of no interpretation found for operator
7660 if Etype (N) = Any_Type then
7661 declare
7662 L : constant Node_Id :=
7663 (if Nkind (N) in N_Binary_Op then Left_Opnd (N) else Empty);
7664 R : constant Node_Id := Right_Opnd (N);
7666 begin
7667 -- If either operand has no type, then don't complain further,
7668 -- since this simply means that we have a propagated error.
7670 if R = Error
7671 or else Etype (R) = Any_Type
7672 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
7673 then
7674 -- For the rather unusual case where one of the operands is
7675 -- a Raise_Expression, whose initial type is Any_Type, use
7676 -- the type of the other operand.
7678 if Nkind (L) = N_Raise_Expression then
7679 Set_Etype (L, Etype (R));
7680 Set_Etype (N, Etype (R));
7682 elsif Nkind (R) = N_Raise_Expression then
7683 Set_Etype (R, Etype (L));
7684 Set_Etype (N, Etype (L));
7685 end if;
7687 return;
7689 -- We explicitly check for the case of concatenation of component
7690 -- with component to avoid reporting spurious matching array types
7691 -- that might happen to be lurking in distant packages (such as
7692 -- run-time packages). This also prevents inconsistencies in the
7693 -- messages for certain ACVC B tests, which can vary depending on
7694 -- types declared in run-time interfaces. Another improvement when
7695 -- aggregates are present is to look for a well-typed operand.
7697 elsif Present (Candidate_Type)
7698 and then (Nkind (N) /= N_Op_Concat
7699 or else Is_Array_Type (Etype (L))
7700 or else Is_Array_Type (Etype (R)))
7701 then
7702 if Nkind (N) = N_Op_Concat then
7703 if Etype (L) /= Any_Composite
7704 and then Is_Array_Type (Etype (L))
7705 then
7706 Candidate_Type := Etype (L);
7708 elsif Etype (R) /= Any_Composite
7709 and then Is_Array_Type (Etype (R))
7710 then
7711 Candidate_Type := Etype (R);
7712 end if;
7713 end if;
7715 Error_Msg_NE -- CODEFIX
7716 ("operator for} is not directly visible!",
7717 N, First_Subtype (Candidate_Type));
7719 declare
7720 U : constant Node_Id :=
7721 Cunit (Get_Source_Unit (Candidate_Type));
7722 begin
7723 if Unit_Is_Visible (U) then
7724 Error_Msg_N -- CODEFIX
7725 ("use clause would make operation legal!", N);
7726 else
7727 Error_Msg_NE -- CODEFIX
7728 ("add with_clause and use_clause for&!",
7729 N, Defining_Entity (Unit (U)));
7730 end if;
7731 end;
7732 return;
7734 -- If either operand is a junk operand (e.g. package name), then
7735 -- post appropriate error messages, but do not complain further.
7737 -- Note that the use of OR in this test instead of OR ELSE is
7738 -- quite deliberate, we may as well check both operands in the
7739 -- binary operator case.
7741 elsif Junk_Operand (R)
7742 or -- really mean OR here and not OR ELSE, see above
7743 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
7744 then
7745 return;
7747 -- The handling of user-defined literals is deferred to the second
7748 -- pass of resolution.
7750 elsif Has_Possible_User_Defined_Literal (N) then
7751 return;
7753 -- If we have a logical operator, one of whose operands is
7754 -- Boolean, then we know that the other operand cannot resolve to
7755 -- Boolean (since we got no interpretations), but in that case we
7756 -- pretty much know that the other operand should be Boolean, so
7757 -- resolve it that way (generating an error).
7759 elsif Nkind (N) in N_Op_And | N_Op_Or | N_Op_Xor then
7760 if Etype (L) = Standard_Boolean then
7761 Resolve (R, Standard_Boolean);
7762 return;
7763 elsif Etype (R) = Standard_Boolean then
7764 Resolve (L, Standard_Boolean);
7765 return;
7766 end if;
7768 -- For an arithmetic operator or comparison operator, if one
7769 -- of the operands is numeric, then we know the other operand
7770 -- is not the same numeric type. If it is a non-numeric type,
7771 -- then probably it is intended to match the other operand.
7773 elsif Nkind (N) in N_Op_Add
7774 | N_Op_Divide
7775 | N_Op_Ge
7776 | N_Op_Gt
7777 | N_Op_Le
7778 | N_Op_Lt
7779 | N_Op_Mod
7780 | N_Op_Multiply
7781 | N_Op_Rem
7782 | N_Op_Subtract
7783 then
7784 -- If Allow_Integer_Address is active, check whether the
7785 -- operation becomes legal after converting an operand.
7787 if Is_Numeric_Type (Etype (L))
7788 and then not Is_Numeric_Type (Etype (R))
7789 then
7790 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7791 Rewrite (L,
7792 Unchecked_Convert_To (
7793 Standard_Address, Relocate_Node (L)));
7794 Rewrite (R,
7795 Unchecked_Convert_To (
7796 Standard_Address, Relocate_Node (R)));
7798 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7799 Analyze_Comparison_Equality_Op (N);
7800 else
7801 Analyze_Arithmetic_Op (N);
7802 end if;
7803 else
7804 Resolve (R, Etype (L));
7805 end if;
7807 return;
7809 elsif Is_Numeric_Type (Etype (R))
7810 and then not Is_Numeric_Type (Etype (L))
7811 then
7812 if Address_Integer_Convert_OK (Etype (L), Etype (R)) 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)));
7820 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7821 Analyze_Comparison_Equality_Op (N);
7822 else
7823 Analyze_Arithmetic_Op (N);
7824 end if;
7826 return;
7828 else
7829 Resolve (L, Etype (R));
7830 end if;
7832 return;
7834 elsif Allow_Integer_Address
7835 and then Is_Descendant_Of_Address (Etype (L))
7836 and then Is_Descendant_Of_Address (Etype (R))
7837 and then not Error_Posted (N)
7838 then
7839 declare
7840 Addr_Type : constant Entity_Id := Etype (L);
7842 begin
7843 Rewrite (L,
7844 Unchecked_Convert_To (
7845 Standard_Address, Relocate_Node (L)));
7846 Rewrite (R,
7847 Unchecked_Convert_To (
7848 Standard_Address, Relocate_Node (R)));
7850 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7851 Analyze_Comparison_Equality_Op (N);
7852 else
7853 Analyze_Arithmetic_Op (N);
7854 end if;
7856 -- If this is an operand in an enclosing arithmetic
7857 -- operation, Convert the result as an address so that
7858 -- arithmetic folding of address can continue.
7860 if Nkind (Parent (N)) in N_Op then
7861 Rewrite (N,
7862 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7863 end if;
7865 return;
7866 end;
7868 -- Under relaxed RM semantics silently replace occurrences of
7869 -- null by System.Address_Null.
7871 elsif Null_To_Null_Address_Convert_OK (N) then
7872 Replace_Null_By_Null_Address (N);
7874 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7875 Analyze_Comparison_Equality_Op (N);
7876 else
7877 Analyze_Arithmetic_Op (N);
7878 end if;
7880 return;
7881 end if;
7883 -- Comparisons on A'Access are common enough to deserve a
7884 -- special message.
7886 elsif Nkind (N) in N_Op_Eq | N_Op_Ne
7887 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7888 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7889 then
7890 Error_Msg_N
7891 ("two access attributes cannot be compared directly", N);
7892 Error_Msg_N
7893 ("\use qualified expression for one of the operands",
7895 return;
7897 -- Another one for C programmers
7899 elsif Nkind (N) = N_Op_Concat
7900 and then Valid_Boolean_Arg (Etype (L))
7901 and then Valid_Boolean_Arg (Etype (R))
7902 then
7903 Error_Msg_N ("invalid operands for concatenation", N);
7904 Error_Msg_N -- CODEFIX
7905 ("\maybe AND was meant", N);
7906 return;
7908 -- A special case for comparison of access parameter with null
7910 elsif Nkind (N) = N_Op_Eq
7911 and then Is_Entity_Name (L)
7912 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7913 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7914 N_Access_Definition
7915 and then Nkind (R) = N_Null
7916 then
7917 Error_Msg_N ("access parameter is not allowed to be null", L);
7918 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7919 return;
7921 -- Another special case for exponentiation, where the right
7922 -- operand must be Natural, independently of the base.
7924 elsif Nkind (N) = N_Op_Expon
7925 and then Is_Numeric_Type (Etype (L))
7926 and then not Is_Overloaded (R)
7927 and then
7928 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7929 and then Base_Type (Etype (R)) /= Universal_Integer
7930 then
7931 if Ada_Version >= Ada_2012
7932 and then Has_Dimension_System (Etype (L))
7933 then
7934 Error_Msg_NE
7935 ("exponent for dimensioned type must be a rational" &
7936 ", found}", R, Etype (R));
7937 else
7938 Error_Msg_NE
7939 ("exponent must be of type Natural, found}", R, Etype (R));
7940 end if;
7942 return;
7944 elsif Nkind (N) in N_Op_Eq | N_Op_Ne then
7945 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7946 Rewrite (L,
7947 Unchecked_Convert_To (
7948 Standard_Address, Relocate_Node (L)));
7949 Rewrite (R,
7950 Unchecked_Convert_To (
7951 Standard_Address, Relocate_Node (R)));
7952 Analyze_Comparison_Equality_Op (N);
7953 return;
7955 -- Under relaxed RM semantics silently replace occurrences of
7956 -- null by System.Address_Null.
7958 elsif Null_To_Null_Address_Convert_OK (N) then
7959 Replace_Null_By_Null_Address (N);
7960 Analyze_Comparison_Equality_Op (N);
7961 return;
7962 end if;
7963 end if;
7965 -- If we fall through then just give general message
7967 Unresolved_Operator (N);
7968 end;
7969 end if;
7970 end Operator_Check;
7972 ---------------------------------------
7973 -- Has_Possible_User_Defined_Literal --
7974 ---------------------------------------
7976 function Has_Possible_User_Defined_Literal (N : Node_Id) return Boolean is
7977 R : constant Node_Id := Right_Opnd (N);
7979 procedure Check_Literal_Opnd (Opnd : Node_Id);
7980 -- If an operand is a literal to which an aspect may apply,
7981 -- add the corresponding type to operator node.
7983 ------------------------
7984 -- Check_Literal_Opnd --
7985 ------------------------
7987 procedure Check_Literal_Opnd (Opnd : Node_Id) is
7988 begin
7989 if Nkind (Opnd) in N_Numeric_Or_String_Literal
7990 or else (Is_Entity_Name (Opnd)
7991 and then Present (Entity (Opnd))
7992 and then Is_Named_Number (Entity (Opnd)))
7993 then
7994 Add_One_Interp (N, Etype (Opnd), Etype (Opnd));
7995 end if;
7996 end Check_Literal_Opnd;
7998 -- Start of processing for Has_Possible_User_Defined_Literal
8000 begin
8001 if Ada_Version < Ada_2022 then
8002 return False;
8003 end if;
8005 Check_Literal_Opnd (R);
8007 -- Check left operand only if right one did not provide a
8008 -- possible interpretation. Note that literal types are not
8009 -- overloadable, in the sense that there is no overloadable
8010 -- entity name whose several interpretations can be used to
8011 -- indicate possible resulting types, so there is no way to
8012 -- provide more than one interpretation to the operator node.
8013 -- The choice of one operand over the other is arbitrary at
8014 -- this point, and may lead to spurious resolution when both
8015 -- operands are literals of different kinds, but the second
8016 -- pass of resolution will examine anew both operands to
8017 -- determine whether a user-defined literal may apply to
8018 -- either or both.
8020 if Nkind (N) in N_Binary_Op and then Etype (N) = Any_Type then
8021 Check_Literal_Opnd (Left_Opnd (N));
8022 end if;
8024 return Etype (N) /= Any_Type;
8025 end Has_Possible_User_Defined_Literal;
8027 -----------------------------------------------
8028 -- Nondispatching_Call_To_Abstract_Operation --
8029 -----------------------------------------------
8031 procedure Nondispatching_Call_To_Abstract_Operation
8032 (N : Node_Id;
8033 Abstract_Op : Entity_Id)
8035 Typ : constant Entity_Id := Etype (N);
8037 begin
8038 -- In an instance body, this is a runtime check, but one we know will
8039 -- fail, so give an appropriate warning. As usual this kind of warning
8040 -- is an error in SPARK mode.
8042 Error_Msg_Sloc := Sloc (Abstract_Op);
8044 if In_Instance_Body and then SPARK_Mode /= On then
8045 Error_Msg_NE
8046 ("??cannot call abstract operation& declared#",
8047 N, Abstract_Op);
8048 Error_Msg_N ("\Program_Error [??", N);
8049 Rewrite (N,
8050 Make_Raise_Program_Error (Sloc (N),
8051 Reason => PE_Explicit_Raise));
8052 Analyze (N);
8053 Set_Etype (N, Typ);
8055 else
8056 Error_Msg_NE
8057 ("cannot call abstract operation& declared#",
8058 N, Abstract_Op);
8059 Set_Etype (N, Any_Type);
8060 end if;
8061 end Nondispatching_Call_To_Abstract_Operation;
8063 ----------------------------------------------
8064 -- Possible_Type_For_Conditional_Expression --
8065 ----------------------------------------------
8067 function Possible_Type_For_Conditional_Expression
8068 (T1, T2 : Entity_Id) return Entity_Id
8070 function Is_Access_Protected_Subprogram_Attribute
8071 (T : Entity_Id) return Boolean;
8072 -- Return true if T is the type of an access-to-protected-subprogram
8073 -- attribute.
8075 function Is_Access_Subprogram_Attribute (T : Entity_Id) return Boolean;
8076 -- Return true if T is the type of an access-to-subprogram attribute
8078 ----------------------------------------------
8079 -- Is_Access_Protected_Subprogram_Attribute --
8080 ----------------------------------------------
8082 function Is_Access_Protected_Subprogram_Attribute
8083 (T : Entity_Id) return Boolean
8085 begin
8086 return Ekind (T) = E_Access_Protected_Subprogram_Type
8087 and then Ekind (Designated_Type (T)) /= E_Subprogram_Type;
8088 end Is_Access_Protected_Subprogram_Attribute;
8090 ------------------------------------
8091 -- Is_Access_Subprogram_Attribute --
8092 ------------------------------------
8094 function Is_Access_Subprogram_Attribute (T : Entity_Id) return Boolean is
8095 begin
8096 return Ekind (T) = E_Access_Subprogram_Type
8097 and then Ekind (Designated_Type (T)) /= E_Subprogram_Type;
8098 end Is_Access_Subprogram_Attribute;
8100 -- Start of processing for Possible_Type_For_Conditional_Expression
8102 begin
8103 -- If both types are those of similar access attributes or allocators,
8104 -- pick one of them, for example the first.
8106 if Ekind (T1) in E_Access_Attribute_Type | E_Allocator_Type
8107 and then Ekind (T2) in E_Access_Attribute_Type | E_Allocator_Type
8108 then
8109 return T1;
8111 elsif Is_Access_Subprogram_Attribute (T1)
8112 and then Is_Access_Subprogram_Attribute (T2)
8113 and then
8114 Subtype_Conformant (Designated_Type (T1), Designated_Type (T2))
8115 then
8116 return T1;
8118 elsif Is_Access_Protected_Subprogram_Attribute (T1)
8119 and then Is_Access_Protected_Subprogram_Attribute (T2)
8120 and then
8121 Subtype_Conformant (Designated_Type (T1), Designated_Type (T2))
8122 then
8123 return T1;
8125 -- The other case to be considered is a pair of tagged types
8127 elsif Is_Tagged_Type (T1) and then Is_Tagged_Type (T2) then
8128 -- Covers performs the same checks when T1 or T2 are a CW type, so
8129 -- we don't need to do them again here.
8131 if not Is_Class_Wide_Type (T1) and then Is_Ancestor (T1, T2) then
8132 return T1;
8134 elsif not Is_Class_Wide_Type (T2) and then Is_Ancestor (T2, T1) then
8135 return T2;
8137 -- Neither type is an ancestor of the other, but they may have one in
8138 -- common, so we pick the first type as above. We could perform here
8139 -- the computation of the nearest common ancestors of T1 and T2, but
8140 -- this would require a significant amount of work and the practical
8141 -- benefit would very likely be negligible.
8143 else
8144 return T1;
8145 end if;
8147 -- Otherwise no type is possible
8149 else
8150 return Empty;
8151 end if;
8152 end Possible_Type_For_Conditional_Expression;
8154 --------------------------------
8155 -- Remove_Abstract_Operations --
8156 --------------------------------
8158 procedure Remove_Abstract_Operations (N : Node_Id) is
8159 Abstract_Op : Entity_Id := Empty;
8160 Address_Descendant : Boolean := False;
8161 I : Interp_Index;
8162 It : Interp;
8164 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
8165 -- activate this if either extensions are enabled, or if the abstract
8166 -- operation in question comes from a predefined file. This latter test
8167 -- allows us to use abstract to make operations invisible to users. In
8168 -- particular, if type Address is non-private and abstract subprograms
8169 -- are used to hide its operators, they will be truly hidden.
8171 type Operand_Position is (First_Op, Second_Op);
8172 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
8174 procedure Remove_Address_Interpretations (Op : Operand_Position);
8175 -- Ambiguities may arise when the operands are literal and the address
8176 -- operations in s-auxdec are visible. In that case, remove the
8177 -- interpretation of a literal as Address, to retain the semantics
8178 -- of Address as a private type.
8180 ------------------------------------
8181 -- Remove_Address_Interpretations --
8182 ------------------------------------
8184 procedure Remove_Address_Interpretations (Op : Operand_Position) is
8185 Formal : Entity_Id;
8187 begin
8188 if Is_Overloaded (N) then
8189 Get_First_Interp (N, I, It);
8190 while Present (It.Nam) loop
8191 Formal := First_Entity (It.Nam);
8193 if Op = Second_Op then
8194 Next_Entity (Formal);
8195 end if;
8197 if Is_Descendant_Of_Address (Etype (Formal)) then
8198 Address_Descendant := True;
8199 Remove_Interp (I);
8200 end if;
8202 Get_Next_Interp (I, It);
8203 end loop;
8204 end if;
8205 end Remove_Address_Interpretations;
8207 -- Start of processing for Remove_Abstract_Operations
8209 begin
8210 if Is_Overloaded (N) then
8211 if Debug_Flag_V then
8212 Write_Line ("Remove_Abstract_Operations: ");
8213 Write_Overloads (N);
8214 end if;
8216 Get_First_Interp (N, I, It);
8218 while Present (It.Nam) loop
8219 if Is_Overloadable (It.Nam)
8220 and then Is_Abstract_Subprogram (It.Nam)
8221 and then not Is_Dispatching_Operation (It.Nam)
8222 then
8223 Abstract_Op := It.Nam;
8225 if Is_Descendant_Of_Address (It.Typ) then
8226 Address_Descendant := True;
8227 Remove_Interp (I);
8228 exit;
8230 -- In Ada 2005, this operation does not participate in overload
8231 -- resolution. If the operation is defined in a predefined
8232 -- unit, it is one of the operations declared abstract in some
8233 -- variants of System, and it must be removed as well.
8235 elsif Ada_Version >= Ada_2005
8236 or else In_Predefined_Unit (It.Nam)
8237 then
8238 Remove_Interp (I);
8239 exit;
8240 end if;
8241 end if;
8243 Get_Next_Interp (I, It);
8244 end loop;
8246 if No (Abstract_Op) then
8248 -- If some interpretation yields an integer type, it is still
8249 -- possible that there are address interpretations. Remove them
8250 -- if one operand is a literal, to avoid spurious ambiguities
8251 -- on systems where Address is a visible integer type.
8253 if Is_Overloaded (N)
8254 and then Nkind (N) in N_Op
8255 and then Is_Integer_Type (Etype (N))
8256 then
8257 if Nkind (N) in N_Binary_Op then
8258 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
8259 Remove_Address_Interpretations (Second_Op);
8261 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
8262 Remove_Address_Interpretations (First_Op);
8263 end if;
8264 end if;
8265 end if;
8267 elsif Nkind (N) in N_Op then
8269 -- Remove interpretations that treat literals as addresses. This
8270 -- is never appropriate, even when Address is defined as a visible
8271 -- Integer type. The reason is that we would really prefer Address
8272 -- to behave as a private type, even in this case. If Address is a
8273 -- visible integer type, we get lots of overload ambiguities.
8275 if Nkind (N) in N_Binary_Op then
8276 declare
8277 U1 : constant Boolean :=
8278 Present (Universal_Interpretation (Right_Opnd (N)));
8279 U2 : constant Boolean :=
8280 Present (Universal_Interpretation (Left_Opnd (N)));
8282 begin
8283 if U1 then
8284 Remove_Address_Interpretations (Second_Op);
8285 end if;
8287 if U2 then
8288 Remove_Address_Interpretations (First_Op);
8289 end if;
8291 if not (U1 and U2) then
8293 -- Remove corresponding predefined operator, which is
8294 -- always added to the overload set.
8296 Get_First_Interp (N, I, It);
8297 while Present (It.Nam) loop
8298 if Scope (It.Nam) = Standard_Standard
8299 and then Base_Type (It.Typ) =
8300 Base_Type (Etype (Abstract_Op))
8301 then
8302 Remove_Interp (I);
8303 end if;
8305 Get_Next_Interp (I, It);
8306 end loop;
8308 elsif Is_Overloaded (N)
8309 and then Present (Univ_Type)
8310 then
8311 -- If both operands have a universal interpretation,
8312 -- it is still necessary to remove interpretations that
8313 -- yield Address. Any remaining ambiguities will be
8314 -- removed in Disambiguate.
8316 Get_First_Interp (N, I, It);
8317 while Present (It.Nam) loop
8318 if Is_Descendant_Of_Address (It.Typ) then
8319 Remove_Interp (I);
8321 elsif not Is_Type (It.Nam) then
8322 Set_Entity (N, It.Nam);
8323 end if;
8325 Get_Next_Interp (I, It);
8326 end loop;
8327 end if;
8328 end;
8329 end if;
8331 elsif Nkind (N) = N_Function_Call
8332 and then
8333 (Nkind (Name (N)) = N_Operator_Symbol
8334 or else
8335 (Nkind (Name (N)) = N_Expanded_Name
8336 and then
8337 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
8338 then
8340 declare
8341 Arg1 : constant Node_Id := First (Parameter_Associations (N));
8342 U1 : constant Boolean :=
8343 Present (Universal_Interpretation (Arg1));
8344 U2 : constant Boolean :=
8345 Present (Next (Arg1)) and then
8346 Present (Universal_Interpretation (Next (Arg1)));
8348 begin
8349 if U1 then
8350 Remove_Address_Interpretations (First_Op);
8351 end if;
8353 if U2 then
8354 Remove_Address_Interpretations (Second_Op);
8355 end if;
8357 if not (U1 and U2) then
8358 Get_First_Interp (N, I, It);
8359 while Present (It.Nam) loop
8360 if Scope (It.Nam) = Standard_Standard
8361 and then It.Typ = Base_Type (Etype (Abstract_Op))
8362 then
8363 Remove_Interp (I);
8364 end if;
8366 Get_Next_Interp (I, It);
8367 end loop;
8368 end if;
8369 end;
8370 end if;
8372 -- If the removal has left no valid interpretations, emit an error
8373 -- message now and label node as illegal.
8375 if Present (Abstract_Op) then
8376 Get_First_Interp (N, I, It);
8378 if No (It.Nam) then
8380 -- Removal of abstract operation left no viable candidate
8382 Nondispatching_Call_To_Abstract_Operation (N, Abstract_Op);
8384 -- In Ada 2005, an abstract operation may disable predefined
8385 -- operators. Since the context is not yet known, we mark the
8386 -- predefined operators as potentially hidden. Do not include
8387 -- predefined operators when addresses are involved since this
8388 -- case is handled separately.
8390 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
8391 while Present (It.Nam) loop
8392 if Is_Numeric_Type (It.Typ)
8393 and then Scope (It.Typ) = Standard_Standard
8394 and then Ekind (It.Nam) = E_Operator
8395 then
8396 Set_Abstract_Op (I, Abstract_Op);
8397 end if;
8399 Get_Next_Interp (I, It);
8400 end loop;
8401 end if;
8402 end if;
8404 if Debug_Flag_V then
8405 Write_Line ("Remove_Abstract_Operations done: ");
8406 Write_Overloads (N);
8407 end if;
8408 end if;
8409 end Remove_Abstract_Operations;
8411 ----------------------------
8412 -- Try_Container_Indexing --
8413 ----------------------------
8415 function Try_Container_Indexing
8416 (N : Node_Id;
8417 Prefix : Node_Id;
8418 Exprs : List_Id) return Boolean
8420 Pref_Typ : Entity_Id := Etype (Prefix);
8422 function Constant_Indexing_OK return Boolean;
8423 -- Constant_Indexing is legal if there is no Variable_Indexing defined
8424 -- for the type, or else node not a target of assignment, or an actual
8425 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
8427 function Expr_Matches_In_Formal
8428 (Subp : Entity_Id;
8429 Par : Node_Id) return Boolean;
8430 -- Find formal corresponding to given indexed component that is an
8431 -- actual in a call. Note that the enclosing subprogram call has not
8432 -- been analyzed yet, and the parameter list is not normalized, so
8433 -- that if the argument is a parameter association we must match it
8434 -- by name and not by position.
8436 function Find_Indexing_Operations
8437 (T : Entity_Id;
8438 Nam : Name_Id;
8439 Is_Constant : Boolean) return Node_Id;
8440 -- Return a reference to the primitive operation of type T denoted by
8441 -- name Nam. If the operation is overloaded, the reference carries all
8442 -- interpretations. Flag Is_Constant should be set when the context is
8443 -- constant indexing.
8445 --------------------------
8446 -- Constant_Indexing_OK --
8447 --------------------------
8449 function Constant_Indexing_OK return Boolean is
8450 Par : Node_Id;
8452 begin
8453 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
8454 return True;
8456 elsif not Is_Variable (Prefix) then
8457 return True;
8458 end if;
8460 Par := N;
8461 while Present (Par) loop
8462 if Nkind (Parent (Par)) = N_Assignment_Statement
8463 and then Par = Name (Parent (Par))
8464 then
8465 return False;
8467 -- The call may be overloaded, in which case we assume that its
8468 -- resolution does not depend on the type of the parameter that
8469 -- includes the indexing operation.
8471 elsif Nkind (Parent (Par)) in N_Subprogram_Call then
8473 if not Is_Entity_Name (Name (Parent (Par))) then
8475 -- ??? We don't know what to do with an N_Selected_Component
8476 -- node for a prefixed-notation call to AA.BB where AA's
8477 -- type is known, but BB has not yet been resolved. In that
8478 -- case, the preceding Is_Entity_Name call returns False.
8479 -- Incorrectly returning False here will usually work
8480 -- better than incorrectly returning True, so that's what
8481 -- we do for now.
8483 return False;
8484 end if;
8486 declare
8487 Proc : Entity_Id;
8489 begin
8490 -- We should look for an interpretation with the proper
8491 -- number of formals, and determine whether it is an
8492 -- In_Parameter, but for now we examine the formal that
8493 -- corresponds to the indexing, and assume that variable
8494 -- indexing is required if some interpretation has an
8495 -- assignable formal at that position. Still does not
8496 -- cover the most complex cases ???
8498 if Is_Overloaded (Name (Parent (Par))) then
8499 declare
8500 Proc : constant Node_Id := Name (Parent (Par));
8501 I : Interp_Index;
8502 It : Interp;
8504 begin
8505 Get_First_Interp (Proc, I, It);
8506 while Present (It.Nam) loop
8507 if not Expr_Matches_In_Formal (It.Nam, Par) then
8508 return False;
8509 end if;
8511 Get_Next_Interp (I, It);
8512 end loop;
8513 end;
8515 -- All interpretations have a matching in-mode formal
8517 return True;
8519 else
8520 Proc := Entity (Name (Parent (Par)));
8522 -- If this is an indirect call, get formals from
8523 -- designated type.
8525 if Is_Access_Subprogram_Type (Etype (Proc)) then
8526 Proc := Designated_Type (Etype (Proc));
8527 end if;
8528 end if;
8530 return Expr_Matches_In_Formal (Proc, Par);
8531 end;
8533 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
8534 return False;
8536 -- If the indexed component is a prefix it may be the first actual
8537 -- of a prefixed call. Retrieve the called entity, if any, and
8538 -- check its first formal. Determine if the context is a procedure
8539 -- or function call.
8541 elsif Nkind (Parent (Par)) = N_Selected_Component then
8542 declare
8543 Sel : constant Node_Id := Selector_Name (Parent (Par));
8544 Nam : constant Entity_Id := Current_Entity (Sel);
8546 begin
8547 if Present (Nam) and then Is_Overloadable (Nam) then
8548 if Nkind (Parent (Parent (Par))) =
8549 N_Procedure_Call_Statement
8550 then
8551 return False;
8553 elsif Ekind (Nam) = E_Function
8554 and then Present (First_Formal (Nam))
8555 then
8556 return Ekind (First_Formal (Nam)) = E_In_Parameter;
8557 end if;
8558 end if;
8559 end;
8561 elsif Nkind (Par) in N_Op then
8562 return True;
8563 end if;
8565 Par := Parent (Par);
8566 end loop;
8568 -- In all other cases, constant indexing is legal
8570 return True;
8571 end Constant_Indexing_OK;
8573 ----------------------------
8574 -- Expr_Matches_In_Formal --
8575 ----------------------------
8577 function Expr_Matches_In_Formal
8578 (Subp : Entity_Id;
8579 Par : Node_Id) return Boolean
8581 Actual : Node_Id;
8582 Formal : Node_Id;
8584 begin
8585 Formal := First_Formal (Subp);
8586 Actual := First (Parameter_Associations ((Parent (Par))));
8588 if Nkind (Par) /= N_Parameter_Association then
8590 -- Match by position
8592 while Present (Actual) and then Present (Formal) loop
8593 exit when Actual = Par;
8594 Next (Actual);
8596 if Present (Formal) then
8597 Next_Formal (Formal);
8599 -- Otherwise this is a parameter mismatch, the error is
8600 -- reported elsewhere, or else variable indexing is implied.
8602 else
8603 return False;
8604 end if;
8605 end loop;
8607 else
8608 -- Match by name
8610 while Present (Formal) loop
8611 exit when Chars (Formal) = Chars (Selector_Name (Par));
8612 Next_Formal (Formal);
8614 if No (Formal) then
8615 return False;
8616 end if;
8617 end loop;
8618 end if;
8620 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
8621 end Expr_Matches_In_Formal;
8623 ------------------------------
8624 -- Find_Indexing_Operations --
8625 ------------------------------
8627 function Find_Indexing_Operations
8628 (T : Entity_Id;
8629 Nam : Name_Id;
8630 Is_Constant : Boolean) return Node_Id
8632 procedure Inspect_Declarations
8633 (Typ : Entity_Id;
8634 Ref : in out Node_Id);
8635 -- Traverse the declarative list where type Typ resides and collect
8636 -- all suitable interpretations in node Ref.
8638 procedure Inspect_Primitives
8639 (Typ : Entity_Id;
8640 Ref : in out Node_Id);
8641 -- Traverse the list of primitive operations of type Typ and collect
8642 -- all suitable interpretations in node Ref.
8644 function Is_OK_Candidate
8645 (Subp_Id : Entity_Id;
8646 Typ : Entity_Id) return Boolean;
8647 -- Determine whether subprogram Subp_Id is a suitable indexing
8648 -- operation for type Typ. To qualify as such, the subprogram must
8649 -- be a function, have at least two parameters, and the type of the
8650 -- first parameter must be either Typ, or Typ'Class, or access [to
8651 -- constant] with designated type Typ or Typ'Class.
8653 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
8654 -- Store subprogram Subp_Id as an interpretation in node Ref
8656 --------------------------
8657 -- Inspect_Declarations --
8658 --------------------------
8660 procedure Inspect_Declarations
8661 (Typ : Entity_Id;
8662 Ref : in out Node_Id)
8664 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
8665 Decl : Node_Id;
8666 Subp_Id : Entity_Id;
8668 begin
8669 -- Ensure that the routine is not called with itypes, which lack a
8670 -- declarative node.
8672 pragma Assert (Present (Typ_Decl));
8673 pragma Assert (Is_List_Member (Typ_Decl));
8675 Decl := First (List_Containing (Typ_Decl));
8676 while Present (Decl) loop
8677 if Nkind (Decl) = N_Subprogram_Declaration then
8678 Subp_Id := Defining_Entity (Decl);
8680 if Is_OK_Candidate (Subp_Id, Typ) then
8681 Record_Interp (Subp_Id, Ref);
8682 end if;
8683 end if;
8685 Next (Decl);
8686 end loop;
8687 end Inspect_Declarations;
8689 ------------------------
8690 -- Inspect_Primitives --
8691 ------------------------
8693 procedure Inspect_Primitives
8694 (Typ : Entity_Id;
8695 Ref : in out Node_Id)
8697 Prim_Elmt : Elmt_Id;
8698 Prim_Id : Entity_Id;
8700 begin
8701 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
8702 while Present (Prim_Elmt) loop
8703 Prim_Id := Node (Prim_Elmt);
8705 if Is_OK_Candidate (Prim_Id, Typ) then
8706 Record_Interp (Prim_Id, Ref);
8707 end if;
8709 Next_Elmt (Prim_Elmt);
8710 end loop;
8711 end Inspect_Primitives;
8713 ---------------------
8714 -- Is_OK_Candidate --
8715 ---------------------
8717 function Is_OK_Candidate
8718 (Subp_Id : Entity_Id;
8719 Typ : Entity_Id) return Boolean
8721 Formal : Entity_Id;
8722 Formal_Typ : Entity_Id;
8723 Param_Typ : Node_Id;
8725 begin
8726 -- To classify as a suitable candidate, the subprogram must be a
8727 -- function whose name matches the argument of aspect Constant or
8728 -- Variable_Indexing.
8730 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
8731 Formal := First_Formal (Subp_Id);
8733 -- The candidate requires at least two parameters
8735 if Present (Formal) and then Present (Next_Formal (Formal)) then
8736 Formal_Typ := Empty;
8737 Param_Typ := Parameter_Type (Parent (Formal));
8739 -- Use the designated type when the first parameter is of an
8740 -- access type.
8742 if Nkind (Param_Typ) = N_Access_Definition
8743 and then Present (Subtype_Mark (Param_Typ))
8744 then
8745 -- When the context is a constant indexing, the access
8746 -- definition must be access-to-constant. This does not
8747 -- apply to variable indexing.
8749 if not Is_Constant
8750 or else Constant_Present (Param_Typ)
8751 then
8752 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
8753 end if;
8755 -- Otherwise use the parameter type
8757 else
8758 Formal_Typ := Etype (Param_Typ);
8759 end if;
8761 if Present (Formal_Typ) then
8763 -- Use the specific type when the parameter type is
8764 -- class-wide.
8766 if Is_Class_Wide_Type (Formal_Typ) then
8767 Formal_Typ := Etype (Base_Type (Formal_Typ));
8768 end if;
8770 -- Use the full view when the parameter type is private
8771 -- or incomplete.
8773 if Is_Incomplete_Or_Private_Type (Formal_Typ)
8774 and then Present (Full_View (Formal_Typ))
8775 then
8776 Formal_Typ := Full_View (Formal_Typ);
8777 end if;
8779 -- The type of the first parameter must denote the type
8780 -- of the container or acts as its ancestor type.
8782 return
8783 Formal_Typ = Typ
8784 or else Is_Ancestor (Formal_Typ, Typ);
8785 end if;
8786 end if;
8787 end if;
8789 return False;
8790 end Is_OK_Candidate;
8792 -------------------
8793 -- Record_Interp --
8794 -------------------
8796 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8797 begin
8798 if Present (Ref) then
8799 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8801 -- Otherwise this is the first interpretation. Create a reference
8802 -- where all remaining interpretations will be collected.
8804 else
8805 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8806 end if;
8807 end Record_Interp;
8809 -- Local variables
8811 Ref : Node_Id;
8812 Typ : Entity_Id;
8814 -- Start of processing for Find_Indexing_Operations
8816 begin
8817 Typ := T;
8819 -- Use the specific type when the parameter type is class-wide
8821 if Is_Class_Wide_Type (Typ) then
8822 Typ := Root_Type (Typ);
8823 end if;
8825 Ref := Empty;
8826 Typ := Underlying_Type (Base_Type (Typ));
8828 Inspect_Primitives (Typ, Ref);
8830 -- Now look for explicit declarations of an indexing operation.
8831 -- If the type is private the operation may be declared in the
8832 -- visible part that contains the partial view.
8834 if Is_Private_Type (T) then
8835 Inspect_Declarations (T, Ref);
8836 end if;
8838 Inspect_Declarations (Typ, Ref);
8840 return Ref;
8841 end Find_Indexing_Operations;
8843 -- Local variables
8845 Loc : constant Source_Ptr := Sloc (N);
8846 Assoc : List_Id;
8847 C_Type : Entity_Id;
8848 Func : Entity_Id;
8849 Func_Name : Node_Id;
8850 Indexing : Node_Id;
8852 Is_Constant_Indexing : Boolean := False;
8853 -- This flag reflects the nature of the container indexing. Note that
8854 -- the context may be suited for constant indexing, but the type may
8855 -- lack a Constant_Indexing annotation.
8857 -- Start of processing for Try_Container_Indexing
8859 begin
8860 -- Node may have been analyzed already when testing for a prefixed
8861 -- call, in which case do not redo analysis.
8863 if Present (Generalized_Indexing (N)) then
8864 return True;
8865 end if;
8867 -- An explicit dereference needs to be created in the case of a prefix
8868 -- that's an access.
8870 -- It seems that this should be done elsewhere, but not clear where that
8871 -- should happen. Normally Insert_Explicit_Dereference is called via
8872 -- Resolve_Implicit_Dereference, called from Resolve_Indexed_Component,
8873 -- but that won't be called in this case because we transform the
8874 -- indexing to a call. Resolve_Call.Check_Prefixed_Call takes care of
8875 -- implicit dereferencing and referencing on prefixed calls, but that
8876 -- would be too late, even if we expanded to a prefix call, because
8877 -- Process_Indexed_Component will flag an error before the resolution
8878 -- happens. ???
8880 if Is_Access_Type (Pref_Typ) then
8881 Pref_Typ := Implicitly_Designated_Type (Pref_Typ);
8882 Insert_Explicit_Dereference (Prefix);
8883 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
8884 end if;
8886 C_Type := Pref_Typ;
8888 -- If indexing a class-wide container, obtain indexing primitive from
8889 -- specific type.
8891 if Is_Class_Wide_Type (C_Type) then
8892 C_Type := Etype (Base_Type (C_Type));
8893 end if;
8895 -- Check whether the type has a specified indexing aspect
8897 Func_Name := Empty;
8899 -- The context is suitable for constant indexing, so obtain the name of
8900 -- the indexing function from aspect Constant_Indexing.
8902 if Constant_Indexing_OK then
8903 Func_Name :=
8904 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
8905 end if;
8907 if Present (Func_Name) then
8908 Is_Constant_Indexing := True;
8910 -- Otherwise attempt variable indexing
8912 else
8913 Func_Name :=
8914 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
8915 end if;
8917 -- The type is not subject to either form of indexing, therefore the
8918 -- indexed component does not denote container indexing. If this is a
8919 -- true error, it is diagnosed by the caller.
8921 if No (Func_Name) then
8923 -- The prefix itself may be an indexing of a container. Rewrite it
8924 -- as such and retry.
8926 if Has_Implicit_Dereference (Pref_Typ) then
8927 Build_Explicit_Dereference
8928 (Prefix, Get_Reference_Discriminant (Pref_Typ));
8929 return Try_Container_Indexing (N, Prefix, Exprs);
8931 -- Otherwise this is definitely not container indexing
8933 else
8934 return False;
8935 end if;
8937 -- If the container type is derived from another container type, the
8938 -- value of the inherited aspect is the Reference operation declared
8939 -- for the parent type.
8941 -- However, Reference is also a primitive operation of the type, and the
8942 -- inherited operation has a different signature. We retrieve the right
8943 -- ones (the function may be overloaded) from the list of primitive
8944 -- operations of the derived type.
8946 -- Note that predefined containers are typically all derived from one of
8947 -- the Controlled types. The code below is motivated by containers that
8948 -- are derived from other types with a Reference aspect.
8949 -- Note as well that we need to examine the base type, given that
8950 -- the container object may be a constrained subtype or itype that
8951 -- does not have an explicit declaration.
8953 elsif Is_Derived_Type (C_Type)
8954 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
8955 then
8956 Func_Name :=
8957 Find_Indexing_Operations
8958 (T => Base_Type (C_Type),
8959 Nam => Chars (Func_Name),
8960 Is_Constant => Is_Constant_Indexing);
8961 end if;
8963 Assoc := New_List (Relocate_Node (Prefix));
8965 -- A generalized indexing may have nore than one index expression, so
8966 -- transfer all of them to the argument list to be used in the call.
8967 -- Note that there may be named associations, in which case the node
8968 -- was rewritten earlier as a call, and has been transformed back into
8969 -- an indexed expression to share the following processing.
8971 -- The generalized indexing node is the one on which analysis and
8972 -- resolution take place. Before expansion the original node is replaced
8973 -- with the generalized indexing node, which is a call, possibly with a
8974 -- dereference operation.
8976 -- Create argument list for function call that represents generalized
8977 -- indexing. Note that indices (i.e. actuals) may themselves be
8978 -- overloaded.
8980 declare
8981 Arg : Node_Id;
8982 New_Arg : Node_Id;
8984 begin
8985 Arg := First (Exprs);
8986 while Present (Arg) loop
8987 New_Arg := Relocate_Node (Arg);
8989 -- The arguments can be parameter associations, in which case the
8990 -- explicit actual parameter carries the overloadings.
8992 if Nkind (New_Arg) /= N_Parameter_Association then
8993 Save_Interps (Arg, New_Arg);
8994 end if;
8996 Append (New_Arg, Assoc);
8997 Next (Arg);
8998 end loop;
8999 end;
9001 if not Is_Overloaded (Func_Name) then
9002 Func := Entity (Func_Name);
9004 -- Can happen in case of e.g. cascaded errors
9006 if No (Func) then
9007 return False;
9008 end if;
9010 Indexing :=
9011 Make_Function_Call (Loc,
9012 Name => New_Occurrence_Of (Func, Loc),
9013 Parameter_Associations => Assoc);
9015 Set_Parent (Indexing, Parent (N));
9016 Set_Generalized_Indexing (N, Indexing);
9017 Analyze (Indexing);
9018 Set_Etype (N, Etype (Indexing));
9020 -- If the return type of the indexing function is a reference type,
9021 -- add the dereference as a possible interpretation. Note that the
9022 -- indexing aspect may be a function that returns the element type
9023 -- with no intervening implicit dereference, and that the reference
9024 -- discriminant is not the first discriminant.
9026 if Has_Discriminants (Etype (Func)) then
9027 Check_Implicit_Dereference (N, Etype (Func));
9028 end if;
9030 else
9031 -- If there are multiple indexing functions, build a function call
9032 -- and analyze it for each of the possible interpretations.
9034 Indexing :=
9035 Make_Function_Call (Loc,
9036 Name =>
9037 Make_Identifier (Loc, Chars (Func_Name)),
9038 Parameter_Associations => Assoc);
9039 Set_Parent (Indexing, Parent (N));
9040 Set_Generalized_Indexing (N, Indexing);
9041 Set_Etype (N, Any_Type);
9042 Set_Etype (Name (Indexing), Any_Type);
9044 declare
9045 I : Interp_Index;
9046 It : Interp;
9047 Success : Boolean;
9049 begin
9050 Get_First_Interp (Func_Name, I, It);
9051 Set_Etype (Indexing, Any_Type);
9053 -- Analyze each candidate function with the given actuals
9055 while Present (It.Nam) loop
9056 Analyze_One_Call (Indexing, It.Nam, False, Success);
9057 Get_Next_Interp (I, It);
9058 end loop;
9060 -- If there are several successful candidates, resolution will
9061 -- be by result. Mark the interpretations of the function name
9062 -- itself.
9064 if Is_Overloaded (Indexing) then
9065 Get_First_Interp (Indexing, I, It);
9067 while Present (It.Nam) loop
9068 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
9069 Get_Next_Interp (I, It);
9070 end loop;
9072 else
9073 Set_Etype (Name (Indexing), Etype (Indexing));
9074 end if;
9076 -- Now add the candidate interpretations to the indexing node
9077 -- itself, to be replaced later by the function call.
9079 if Is_Overloaded (Name (Indexing)) then
9080 Get_First_Interp (Name (Indexing), I, It);
9082 while Present (It.Nam) loop
9083 Add_One_Interp (N, It.Nam, It.Typ);
9085 -- Add dereference interpretation if the result type has
9086 -- implicit reference discriminants.
9088 if Has_Discriminants (Etype (It.Nam)) then
9089 Check_Implicit_Dereference (N, Etype (It.Nam));
9090 end if;
9092 Get_Next_Interp (I, It);
9093 end loop;
9095 else
9096 Set_Etype (N, Etype (Name (Indexing)));
9097 if Has_Discriminants (Etype (N)) then
9098 Check_Implicit_Dereference (N, Etype (N));
9099 end if;
9100 end if;
9101 end;
9102 end if;
9104 if Etype (Indexing) = Any_Type then
9105 Error_Msg_NE
9106 ("container cannot be indexed with&", N, Etype (First (Exprs)));
9107 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
9108 end if;
9110 return True;
9111 end Try_Container_Indexing;
9113 -----------------------
9114 -- Try_Indirect_Call --
9115 -----------------------
9117 function Try_Indirect_Call
9118 (N : Node_Id;
9119 Nam : Entity_Id;
9120 Typ : Entity_Id) return Boolean
9122 Actual : Node_Id;
9123 Formal : Entity_Id;
9125 Call_OK : Boolean;
9126 pragma Warnings (Off, Call_OK);
9128 begin
9129 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
9131 Actual := First_Actual (N);
9132 Formal := First_Formal (Designated_Type (Typ));
9133 while Present (Actual) and then Present (Formal) loop
9134 if not Has_Compatible_Type (Actual, Etype (Formal)) then
9135 return False;
9136 end if;
9138 Next (Actual);
9139 Next_Formal (Formal);
9140 end loop;
9142 if No (Actual) and then No (Formal) then
9143 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
9145 -- Nam is a candidate interpretation for the name in the call,
9146 -- if it is not an indirect call.
9148 if not Is_Type (Nam)
9149 and then Is_Entity_Name (Name (N))
9150 then
9151 Set_Entity (Name (N), Nam);
9152 end if;
9154 return True;
9156 else
9157 return False;
9158 end if;
9159 end Try_Indirect_Call;
9161 ----------------------
9162 -- Try_Indexed_Call --
9163 ----------------------
9165 function Try_Indexed_Call
9166 (N : Node_Id;
9167 Nam : Entity_Id;
9168 Typ : Entity_Id;
9169 Skip_First : Boolean) return Boolean
9171 Loc : constant Source_Ptr := Sloc (N);
9172 Actuals : constant List_Id := Parameter_Associations (N);
9173 Actual : Node_Id;
9174 Index : Entity_Id;
9176 begin
9177 Actual := First (Actuals);
9179 -- If the call was originally written in prefix form, skip the first
9180 -- actual, which is obviously not defaulted.
9182 if Skip_First then
9183 Next (Actual);
9184 end if;
9186 Index := First_Index (Typ);
9187 while Present (Actual) and then Present (Index) loop
9189 -- If the parameter list has a named association, the expression
9190 -- is definitely a call and not an indexed component.
9192 if Nkind (Actual) = N_Parameter_Association then
9193 return False;
9194 end if;
9196 if Is_Entity_Name (Actual)
9197 and then Is_Type (Entity (Actual))
9198 and then No (Next (Actual))
9199 then
9200 -- A single actual that is a type name indicates a slice if the
9201 -- type is discrete, and an error otherwise.
9203 if Is_Discrete_Type (Entity (Actual)) then
9204 Rewrite (N,
9205 Make_Slice (Loc,
9206 Prefix =>
9207 Make_Function_Call (Loc,
9208 Name => Relocate_Node (Name (N))),
9209 Discrete_Range =>
9210 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
9212 Analyze (N);
9214 else
9215 Error_Msg_N ("invalid use of type in expression", Actual);
9216 Set_Etype (N, Any_Type);
9217 end if;
9219 return True;
9221 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
9222 return False;
9223 end if;
9225 Next (Actual);
9226 Next_Index (Index);
9227 end loop;
9229 if No (Actual) and then No (Index) then
9230 Add_One_Interp (N, Nam, Component_Type (Typ));
9232 -- Nam is a candidate interpretation for the name in the call,
9233 -- if it is not an indirect call.
9235 if not Is_Type (Nam)
9236 and then Is_Entity_Name (Name (N))
9237 then
9238 Set_Entity (Name (N), Nam);
9239 end if;
9241 return True;
9242 else
9243 return False;
9244 end if;
9245 end Try_Indexed_Call;
9247 --------------------------
9248 -- Try_Object_Operation --
9249 --------------------------
9251 function Try_Object_Operation
9252 (N : Node_Id;
9253 CW_Test_Only : Boolean := False;
9254 Allow_Extensions : Boolean := False) return Boolean
9256 K : constant Node_Kind := Nkind (Parent (N));
9257 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
9258 Loc : constant Source_Ptr := Sloc (N);
9259 Obj : constant Node_Id := Prefix (N);
9261 Subprog : constant Node_Id :=
9262 Make_Identifier (Sloc (Selector_Name (N)),
9263 Chars => Chars (Selector_Name (N)));
9264 -- Identifier on which possible interpretations will be collected
9266 Report_Error : Boolean := False;
9267 -- If no candidate interpretation matches the context, redo analysis
9268 -- with Report_Error True to provide additional information.
9270 Actual : Node_Id;
9271 Candidate : Entity_Id := Empty;
9272 New_Call_Node : Node_Id := Empty;
9273 Node_To_Replace : Node_Id;
9274 Obj_Type : Entity_Id := Etype (Obj);
9275 Success : Boolean := False;
9277 procedure Complete_Object_Operation
9278 (Call_Node : Node_Id;
9279 Node_To_Replace : Node_Id);
9280 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
9281 -- Call_Node, insert the object (or its dereference) as the first actual
9282 -- in the call, and complete the analysis of the call.
9284 procedure Report_Ambiguity (Op : Entity_Id);
9285 -- If a prefixed procedure call is ambiguous, indicate whether the call
9286 -- includes an implicit dereference or an implicit 'Access.
9288 procedure Transform_Object_Operation
9289 (Call_Node : out Node_Id;
9290 Node_To_Replace : out Node_Id);
9291 -- Transform Obj.Operation (X, Y, ...) into Operation (Obj, X, Y ...).
9292 -- Call_Node is the resulting subprogram call, Node_To_Replace is
9293 -- either N or the parent of N, and Subprog is a reference to the
9294 -- subprogram we are trying to match. Note that the transformation
9295 -- may be partially destructive for the parent of N, so it needs to
9296 -- be undone in the case where Try_Object_Operation returns false.
9298 function Try_Class_Wide_Operation
9299 (Call_Node : Node_Id;
9300 Node_To_Replace : Node_Id) return Boolean;
9301 -- Traverse all ancestor types looking for a class-wide subprogram for
9302 -- which the current operation is a valid non-dispatching call.
9304 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
9305 -- If prefix is overloaded, its interpretation may include different
9306 -- tagged types, and we must examine the primitive operations and the
9307 -- class-wide operations of each in order to find candidate
9308 -- interpretations for the call as a whole.
9310 function Try_Primitive_Operation
9311 (Call_Node : Node_Id;
9312 Node_To_Replace : Node_Id) return Boolean;
9313 -- Traverse the list of primitive subprograms looking for a dispatching
9314 -- operation for which the current node is a valid call.
9316 function Valid_Candidate
9317 (Success : Boolean;
9318 Call : Node_Id;
9319 Subp : Entity_Id) return Entity_Id;
9320 -- If the subprogram is a valid interpretation, record it, and add to
9321 -- the list of interpretations of Subprog. Otherwise return Empty.
9323 -------------------------------
9324 -- Complete_Object_Operation --
9325 -------------------------------
9327 procedure Complete_Object_Operation
9328 (Call_Node : Node_Id;
9329 Node_To_Replace : Node_Id)
9331 Control : constant Entity_Id := First_Formal (Entity (Subprog));
9332 Formal_Type : constant Entity_Id := Etype (Control);
9333 First_Actual : Node_Id;
9335 begin
9336 -- Place the name of the operation, with its interpretations,
9337 -- on the rewritten call.
9339 Set_Name (Call_Node, Subprog);
9341 First_Actual := First (Parameter_Associations (Call_Node));
9343 -- For cross-reference purposes, treat the new node as being in the
9344 -- source if the original one is. Set entity and type, even though
9345 -- they may be overwritten during resolution if overloaded.
9347 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
9348 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
9350 if Nkind (N) = N_Selected_Component
9351 and then not Inside_A_Generic
9352 then
9353 Set_Entity (Selector_Name (N), Entity (Subprog));
9354 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
9355 end if;
9357 -- If need be, rewrite first actual as an explicit dereference. If
9358 -- the call is overloaded, the rewriting can only be done once the
9359 -- primitive operation is identified.
9361 if Is_Overloaded (Subprog) then
9363 -- The prefix itself may be overloaded, and its interpretations
9364 -- must be propagated to the new actual in the call.
9366 if Is_Overloaded (Obj) then
9367 Save_Interps (Obj, First_Actual);
9368 end if;
9370 Rewrite (First_Actual, Obj);
9372 elsif not Is_Access_Type (Formal_Type)
9373 and then Is_Access_Type (Etype (Obj))
9374 then
9375 Rewrite (First_Actual,
9376 Make_Explicit_Dereference (Sloc (Obj), Obj));
9377 Analyze (First_Actual);
9379 -- If we need to introduce an explicit dereference, verify that
9380 -- the resulting actual is compatible with the mode of the formal.
9382 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
9383 and then Is_Access_Constant (Etype (Obj))
9384 then
9385 Error_Msg_NE
9386 ("expect variable in call to&", Prefix (N), Entity (Subprog));
9387 end if;
9389 -- Conversely, if the formal is an access parameter and the object is
9390 -- not an access type or a reference type (i.e. a type with the
9391 -- Implicit_Dereference aspect specified), replace the actual with a
9392 -- 'Access reference. Its analysis will check that the object is
9393 -- aliased.
9395 elsif Is_Access_Type (Formal_Type)
9396 and then not Is_Access_Type (Etype (Obj))
9397 and then
9398 (not Has_Implicit_Dereference (Etype (Obj))
9399 or else
9400 not Is_Access_Type (Designated_Type (Etype
9401 (Get_Reference_Discriminant (Etype (Obj))))))
9402 then
9403 -- A special case: A.all'Access is illegal if A is an access to a
9404 -- constant and the context requires an access to a variable.
9406 if not Is_Access_Constant (Formal_Type) then
9407 if (Nkind (Obj) = N_Explicit_Dereference
9408 and then Is_Access_Constant (Etype (Prefix (Obj))))
9409 or else not Is_Variable (Obj)
9410 then
9411 Error_Msg_NE
9412 ("actual for & must be a variable", Obj, Control);
9413 end if;
9414 end if;
9416 Rewrite (First_Actual,
9417 Make_Attribute_Reference (Loc,
9418 Attribute_Name => Name_Access,
9419 Prefix => Relocate_Node (Obj)));
9421 -- If the object is not overloaded verify that taking access of
9422 -- it is legal. Otherwise check is made during resolution.
9424 if not Is_Overloaded (Obj)
9425 and then not Is_Aliased_View (Obj)
9426 then
9427 Error_Msg_NE
9428 ("object in prefixed call to & must be aliased "
9429 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
9430 end if;
9432 Analyze (First_Actual);
9434 else
9435 if Is_Overloaded (Obj) then
9436 Save_Interps (Obj, First_Actual);
9437 end if;
9439 Rewrite (First_Actual, Obj);
9440 end if;
9442 if In_Extended_Main_Source_Unit (Current_Scope) then
9443 -- The operation is obtained from the dispatch table and not by
9444 -- visibility, and may be declared in a unit that is not
9445 -- explicitly referenced in the source, but is nevertheless
9446 -- required in the context of the current unit. Indicate that
9447 -- operation and its scope are referenced, to prevent spurious and
9448 -- misleading warnings. If the operation is overloaded, all
9449 -- primitives are in the same scope and we can use any of them.
9450 -- Don't do that outside the main unit since otherwise this will
9451 -- e.g. prevent the detection of some unused with clauses.
9453 Set_Referenced (Entity (Subprog), True);
9454 Set_Referenced (Scope (Entity (Subprog)), True);
9455 end if;
9457 Rewrite (Node_To_Replace, Call_Node);
9459 -- Propagate the interpretations collected in subprog to the new
9460 -- function call node, to be resolved from context.
9462 if Is_Overloaded (Subprog) then
9463 Save_Interps (Subprog, Node_To_Replace);
9465 else
9466 Analyze (Node_To_Replace);
9468 -- If the operation has been rewritten into a call, which may get
9469 -- subsequently an explicit dereference, preserve the type on the
9470 -- original node (selected component or indexed component) for
9471 -- subsequent legality tests, e.g. Is_Variable. which examines
9472 -- the original node.
9474 if Nkind (Node_To_Replace) = N_Function_Call then
9475 Set_Etype
9476 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
9477 end if;
9478 end if;
9479 end Complete_Object_Operation;
9481 ----------------------
9482 -- Report_Ambiguity --
9483 ----------------------
9485 procedure Report_Ambiguity (Op : Entity_Id) is
9486 Access_Actual : constant Boolean :=
9487 Is_Access_Type (Etype (Prefix (N)));
9488 Access_Formal : Boolean := False;
9490 begin
9491 Error_Msg_Sloc := Sloc (Op);
9493 if Present (First_Formal (Op)) then
9494 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
9495 end if;
9497 if Access_Formal and then not Access_Actual then
9498 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9499 Error_Msg_N
9500 ("\possible interpretation "
9501 & "(inherited, with implicit 'Access) #", N);
9502 else
9503 Error_Msg_N
9504 ("\possible interpretation (with implicit 'Access) #", N);
9505 end if;
9507 elsif not Access_Formal and then Access_Actual then
9508 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9509 Error_Msg_N
9510 ("\possible interpretation "
9511 & "(inherited, with implicit dereference) #", N);
9512 else
9513 Error_Msg_N
9514 ("\possible interpretation (with implicit dereference) #", N);
9515 end if;
9517 else
9518 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9519 Error_Msg_N ("\possible interpretation (inherited)#", N);
9520 else
9521 Error_Msg_N -- CODEFIX
9522 ("\possible interpretation#", N);
9523 end if;
9524 end if;
9525 end Report_Ambiguity;
9527 --------------------------------
9528 -- Transform_Object_Operation --
9529 --------------------------------
9531 procedure Transform_Object_Operation
9532 (Call_Node : out Node_Id;
9533 Node_To_Replace : out Node_Id)
9535 Dummy : constant Node_Id := New_Copy (Obj);
9536 -- Placeholder used as a first parameter in the call, replaced
9537 -- eventually by the proper object.
9539 Parent_Node : constant Node_Id := Parent (N);
9541 Actual : Node_Id;
9542 Actuals : List_Id;
9544 begin
9545 -- Common case covering 1) Call to a procedure and 2) Call to a
9546 -- function that has some additional actuals.
9548 if Nkind (Parent_Node) in N_Subprogram_Call
9550 -- N is a selected component node containing the name of the
9551 -- subprogram. If N is not the name of the parent node we must
9552 -- not replace the parent node by the new construct. This case
9553 -- occurs when N is a parameterless call to a subprogram that
9554 -- is an actual parameter of a call to another subprogram. For
9555 -- example:
9556 -- Some_Subprogram (..., Obj.Operation, ...)
9558 and then N = Name (Parent_Node)
9559 then
9560 Node_To_Replace := Parent_Node;
9562 Actuals := Parameter_Associations (Parent_Node);
9564 if Present (Actuals) then
9565 Prepend (Dummy, Actuals);
9566 else
9567 Actuals := New_List (Dummy);
9568 end if;
9570 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
9571 Call_Node :=
9572 Make_Procedure_Call_Statement (Loc,
9573 Name => New_Copy (Subprog),
9574 Parameter_Associations => Actuals);
9576 else
9577 Call_Node :=
9578 Make_Function_Call (Loc,
9579 Name => New_Copy (Subprog),
9580 Parameter_Associations => Actuals);
9581 end if;
9583 -- Before analysis, a function call appears as an indexed component
9584 -- if there are no named associations.
9586 elsif Nkind (Parent_Node) = N_Indexed_Component
9587 and then N = Prefix (Parent_Node)
9588 then
9589 Node_To_Replace := Parent_Node;
9590 Actuals := Expressions (Parent_Node);
9592 Actual := First (Actuals);
9593 while Present (Actual) loop
9594 Analyze (Actual);
9595 Next (Actual);
9596 end loop;
9598 Prepend (Dummy, Actuals);
9600 Call_Node :=
9601 Make_Function_Call (Loc,
9602 Name => New_Copy (Subprog),
9603 Parameter_Associations => Actuals);
9605 -- Parameterless call: Obj.F is rewritten as F (Obj)
9607 else
9608 Node_To_Replace := N;
9610 Call_Node :=
9611 Make_Function_Call (Loc,
9612 Name => New_Copy (Subprog),
9613 Parameter_Associations => New_List (Dummy));
9614 end if;
9615 end Transform_Object_Operation;
9617 ------------------------------
9618 -- Try_Class_Wide_Operation --
9619 ------------------------------
9621 function Try_Class_Wide_Operation
9622 (Call_Node : Node_Id;
9623 Node_To_Replace : Node_Id) return Boolean
9625 Anc_Type : Entity_Id;
9626 Matching_Op : Entity_Id := Empty;
9627 Error : Boolean;
9629 procedure Traverse_Homonyms
9630 (Anc_Type : Entity_Id;
9631 Error : out Boolean);
9632 -- Traverse the homonym chain of the subprogram searching for those
9633 -- homonyms whose first formal has the Anc_Type's class-wide type,
9634 -- or an anonymous access type designating the class-wide type. If
9635 -- an ambiguity is detected, then Error is set to True.
9637 procedure Traverse_Interfaces
9638 (Anc_Type : Entity_Id;
9639 Error : out Boolean);
9640 -- Traverse the list of interfaces, if any, associated with Anc_Type
9641 -- and search for acceptable class-wide homonyms associated with each
9642 -- interface. If an ambiguity is detected, then Error is set to True.
9644 -----------------------
9645 -- Traverse_Homonyms --
9646 -----------------------
9648 procedure Traverse_Homonyms
9649 (Anc_Type : Entity_Id;
9650 Error : out Boolean)
9652 function First_Formal_Match
9653 (Subp_Id : Entity_Id;
9654 Typ : Entity_Id) return Boolean;
9655 -- Predicate to verify that the first foramal of class-wide
9656 -- subprogram Subp_Id matches type Typ of the prefix.
9658 ------------------------
9659 -- First_Formal_Match --
9660 ------------------------
9662 function First_Formal_Match
9663 (Subp_Id : Entity_Id;
9664 Typ : Entity_Id) return Boolean
9666 Ctrl : constant Entity_Id := First_Formal (Subp_Id);
9668 begin
9669 return
9670 Present (Ctrl)
9671 and then
9672 (Base_Type (Etype (Ctrl)) = Typ
9673 or else
9674 (Ekind (Etype (Ctrl)) = E_Anonymous_Access_Type
9675 and then
9676 Base_Type (Designated_Type (Etype (Ctrl))) =
9677 Typ));
9678 end First_Formal_Match;
9680 -- Local variables
9682 CW_Typ : constant Entity_Id := Class_Wide_Type (Anc_Type);
9684 Candidate : Entity_Id;
9685 -- If homonym is a renaming, examine the renamed program
9687 Hom : Entity_Id;
9688 Hom_Ref : Node_Id;
9689 Success : Boolean;
9691 -- Start of processing for Traverse_Homonyms
9693 begin
9694 Error := False;
9696 -- Find a non-hidden operation whose first parameter is of the
9697 -- class-wide type, a subtype thereof, or an anonymous access
9698 -- to same. If in an instance, the operation can be considered
9699 -- even if hidden (it may be hidden because the instantiation
9700 -- is expanded after the containing package has been analyzed).
9701 -- If the subprogram is a generic actual in an enclosing instance,
9702 -- it appears as a renaming that is a candidate interpretation as
9703 -- well.
9705 Hom := Current_Entity (Subprog);
9706 while Present (Hom) loop
9707 if Ekind (Hom) in E_Procedure | E_Function
9708 and then Present (Renamed_Entity (Hom))
9709 and then Is_Generic_Actual_Subprogram (Hom)
9710 and then In_Open_Scopes (Scope (Hom))
9711 then
9712 Candidate := Renamed_Entity (Hom);
9713 else
9714 Candidate := Hom;
9715 end if;
9717 if Ekind (Candidate) in E_Function | E_Procedure
9718 and then (not Is_Hidden (Candidate) or else In_Instance)
9719 and then Scope (Candidate) = Scope (Base_Type (Anc_Type))
9720 and then First_Formal_Match (Candidate, CW_Typ)
9721 then
9722 -- If the context is a procedure call, ignore functions
9723 -- in the name of the call.
9725 if Ekind (Candidate) = E_Function
9726 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
9727 and then N = Name (Parent (N))
9728 then
9729 goto Next_Hom;
9731 -- If the context is a function call, ignore procedures
9732 -- in the name of the call.
9734 elsif Ekind (Candidate) = E_Procedure
9735 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
9736 then
9737 goto Next_Hom;
9738 end if;
9740 Set_Etype (Call_Node, Any_Type);
9741 Set_Is_Overloaded (Call_Node, False);
9742 Success := False;
9744 if No (Matching_Op) then
9745 Hom_Ref := New_Occurrence_Of (Candidate, Sloc (Subprog));
9747 Set_Etype (Call_Node, Any_Type);
9748 Set_Name (Call_Node, Hom_Ref);
9749 Set_Parent (Call_Node, Parent (Node_To_Replace));
9751 Analyze_One_Call
9752 (N => Call_Node,
9753 Nam => Candidate,
9754 Report => Report_Error,
9755 Success => Success,
9756 Skip_First => True);
9758 Matching_Op :=
9759 Valid_Candidate (Success, Call_Node, Candidate);
9761 else
9762 Analyze_One_Call
9763 (N => Call_Node,
9764 Nam => Candidate,
9765 Report => Report_Error,
9766 Success => Success,
9767 Skip_First => True);
9769 -- The same operation may be encountered on two homonym
9770 -- traversals, before and after looking at interfaces.
9771 -- Check for this case before reporting a real ambiguity.
9773 if Present
9774 (Valid_Candidate (Success, Call_Node, Candidate))
9775 and then Nkind (Call_Node) /= N_Function_Call
9776 and then Candidate /= Matching_Op
9777 then
9778 Error_Msg_NE ("ambiguous call to&", N, Hom);
9779 Report_Ambiguity (Matching_Op);
9780 Report_Ambiguity (Hom);
9781 Check_Ambiguous_Aggregate (New_Call_Node);
9782 Error := True;
9783 return;
9784 end if;
9785 end if;
9786 end if;
9788 <<Next_Hom>>
9789 Hom := Homonym (Hom);
9790 end loop;
9791 end Traverse_Homonyms;
9793 -------------------------
9794 -- Traverse_Interfaces --
9795 -------------------------
9797 procedure Traverse_Interfaces
9798 (Anc_Type : Entity_Id;
9799 Error : out Boolean)
9801 Intface_List : constant List_Id :=
9802 Abstract_Interface_List (Anc_Type);
9803 Intface : Node_Id;
9805 begin
9806 Error := False;
9808 Intface := First (Intface_List);
9809 while Present (Intface) loop
9811 -- Look for acceptable class-wide homonyms associated with the
9812 -- interface.
9814 Traverse_Homonyms (Etype (Intface), Error);
9816 if Error then
9817 return;
9818 end if;
9820 -- Continue the search by looking at each of the interface's
9821 -- associated interface ancestors.
9823 Traverse_Interfaces (Etype (Intface), Error);
9825 if Error then
9826 return;
9827 end if;
9829 Next (Intface);
9830 end loop;
9831 end Traverse_Interfaces;
9833 -- Start of processing for Try_Class_Wide_Operation
9835 begin
9836 -- If we are searching only for conflicting class-wide subprograms
9837 -- then initialize directly Matching_Op with the target entity.
9839 if CW_Test_Only then
9840 Matching_Op := Entity (Selector_Name (N));
9841 end if;
9843 -- Loop through ancestor types (including interfaces), traversing
9844 -- the homonym chain of the subprogram, trying out those homonyms
9845 -- whose first formal has the class-wide type of the ancestor, or
9846 -- an anonymous access type designating the class-wide type.
9848 Anc_Type := Obj_Type;
9849 loop
9850 -- Look for a match among homonyms associated with the ancestor
9852 Traverse_Homonyms (Anc_Type, Error);
9854 if Error then
9855 return True;
9856 end if;
9858 -- Continue the search for matches among homonyms associated with
9859 -- any interfaces implemented by the ancestor.
9861 Traverse_Interfaces (Anc_Type, Error);
9863 if Error then
9864 return True;
9865 end if;
9867 exit when Etype (Anc_Type) = Anc_Type;
9868 Anc_Type := Etype (Anc_Type);
9869 end loop;
9871 if Present (Matching_Op) then
9872 Set_Etype (Call_Node, Etype (Matching_Op));
9873 end if;
9875 return Present (Matching_Op);
9876 end Try_Class_Wide_Operation;
9878 -----------------------------------
9879 -- Try_One_Prefix_Interpretation --
9880 -----------------------------------
9882 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
9883 Prev_Obj_Type : constant Entity_Id := Obj_Type;
9884 -- If the interpretation does not have a valid candidate type,
9885 -- preserve current value of Obj_Type for subsequent errors.
9887 begin
9888 Obj_Type := T;
9890 if Is_Access_Type (Obj_Type) then
9891 Obj_Type := Designated_Type (Obj_Type);
9892 end if;
9894 if Ekind (Obj_Type)
9895 in E_Private_Subtype | E_Record_Subtype_With_Private
9896 then
9897 Obj_Type := Base_Type (Obj_Type);
9898 end if;
9900 if Is_Class_Wide_Type (Obj_Type) then
9901 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9902 end if;
9904 -- The type may have be obtained through a limited_with clause,
9905 -- in which case the primitive operations are available on its
9906 -- nonlimited view. If still incomplete, retrieve full view.
9908 if Ekind (Obj_Type) = E_Incomplete_Type
9909 and then From_Limited_With (Obj_Type)
9910 and then Has_Non_Limited_View (Obj_Type)
9911 then
9912 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
9913 end if;
9915 -- If the object is not tagged, or the type is still an incomplete
9916 -- type, this is not a prefixed call. Restore the previous type as
9917 -- the current one is not a legal candidate.
9919 -- Extension feature: Calls with prefixed views are also supported
9920 -- for untagged types, so skip the early return when extensions are
9921 -- enabled, unless the type doesn't have a primitive operations list
9922 -- (such as in the case of predefined types).
9924 if (not Is_Tagged_Type (Obj_Type)
9925 and then
9926 (not (Core_Extensions_Allowed or Allow_Extensions)
9927 or else No (Primitive_Operations (Obj_Type))))
9928 or else Is_Incomplete_Type (Obj_Type)
9929 then
9930 Obj_Type := Prev_Obj_Type;
9931 return;
9932 end if;
9934 declare
9935 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
9936 Ignore : Boolean;
9937 Prim_Result : Boolean := False;
9939 begin
9940 if not CW_Test_Only then
9941 Prim_Result :=
9942 Try_Primitive_Operation
9943 (Call_Node => New_Call_Node,
9944 Node_To_Replace => Node_To_Replace);
9946 -- Extension feature: In the case where the prefix is of an
9947 -- access type, and a primitive wasn't found for the designated
9948 -- type, then if the access type has primitives we attempt a
9949 -- prefixed call using one of its primitives. (It seems that
9950 -- this isn't quite right to give preference to the designated
9951 -- type in the case where both the access and designated types
9952 -- have homographic prefixed-view operations that could result
9953 -- in an ambiguity, but handling properly may be tricky. ???)
9955 if (Core_Extensions_Allowed or Allow_Extensions)
9956 and then not Prim_Result
9957 and then Is_Named_Access_Type (Prev_Obj_Type)
9958 and then Present (Direct_Primitive_Operations (Prev_Obj_Type))
9959 then
9960 -- Temporarily reset Obj_Type to the original access type
9962 Obj_Type := Prev_Obj_Type;
9964 Prim_Result :=
9965 Try_Primitive_Operation
9966 (Call_Node => New_Call_Node,
9967 Node_To_Replace => Node_To_Replace);
9969 -- Restore Obj_Type to the designated type (is this really
9970 -- necessary, or should it only be done when Prim_Result is
9971 -- still False?).
9973 Obj_Type := Designated_Type (Obj_Type);
9974 end if;
9975 end if;
9977 -- Check if there is a class-wide subprogram covering the
9978 -- primitive. This check must be done even if a candidate
9979 -- was found in order to report ambiguous calls.
9981 if not Prim_Result then
9982 Ignore :=
9983 Try_Class_Wide_Operation
9984 (Call_Node => New_Call_Node,
9985 Node_To_Replace => Node_To_Replace);
9987 -- If we found a primitive we search for class-wide subprograms
9988 -- using a duplicate of the call node (done to avoid missing its
9989 -- decoration if there is no ambiguity).
9991 else
9992 Ignore :=
9993 Try_Class_Wide_Operation
9994 (Call_Node => Dup_Call_Node,
9995 Node_To_Replace => Node_To_Replace);
9996 end if;
9997 end;
9998 end Try_One_Prefix_Interpretation;
10000 -----------------------------
10001 -- Try_Primitive_Operation --
10002 -----------------------------
10004 function Try_Primitive_Operation
10005 (Call_Node : Node_Id;
10006 Node_To_Replace : Node_Id) return Boolean
10008 Elmt : Elmt_Id;
10009 Prim_Op : Entity_Id;
10010 Matching_Op : Entity_Id := Empty;
10011 Prim_Op_Ref : Node_Id := Empty;
10013 Corr_Type : Entity_Id := Empty;
10014 -- If the prefix is a synchronized type, the controlling type of
10015 -- the primitive operation is the corresponding record type, else
10016 -- this is the object type itself.
10018 Success : Boolean := False;
10020 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
10021 -- For tagged types the candidate interpretations are found in
10022 -- the list of primitive operations of the type and its ancestors.
10023 -- For formal tagged types we have to find the operations declared
10024 -- in the same scope as the type (including in the generic formal
10025 -- part) because the type itself carries no primitive operations,
10026 -- except for formal derived types that inherit the operations of
10027 -- the parent and progenitors.
10029 -- If the context is a generic subprogram body, the generic formals
10030 -- are visible by name, but are not in the entity list of the
10031 -- subprogram because that list starts with the subprogram formals.
10032 -- We retrieve the candidate operations from the generic declaration.
10034 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
10035 -- Prefix notation can also be used on operations that are not
10036 -- primitives of the type, but are declared in the same immediate
10037 -- declarative part, which can only mean the corresponding package
10038 -- body (see RM 4.1.3 (9.2/3)). If we are in that body we extend the
10039 -- list of primitives with body operations with the same name that
10040 -- may be candidates, so that Try_Primitive_Operations can examine
10041 -- them if no real primitive is found.
10043 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
10044 -- An operation that overrides an inherited operation in the private
10045 -- part of its package may be hidden, but if the inherited operation
10046 -- is visible a direct call to it will dispatch to the private one,
10047 -- which is therefore a valid candidate.
10049 function Names_Match
10050 (Obj_Type : Entity_Id;
10051 Prim_Op : Entity_Id;
10052 Subprog : Entity_Id) return Boolean;
10053 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
10054 -- is a protected type then compare also the original name of Prim_Op
10055 -- with the name of Subprog (since the expander may have added a
10056 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
10058 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
10059 -- Verify that the prefix, dereferenced if need be, is a valid
10060 -- controlling argument in a call to Op. The remaining actuals
10061 -- are checked in the subsequent call to Analyze_One_Call.
10063 ------------------------------
10064 -- Collect_Generic_Type_Ops --
10065 ------------------------------
10067 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
10068 Bas : constant Entity_Id := Base_Type (T);
10069 Candidates : constant Elist_Id := New_Elmt_List;
10070 Subp : Entity_Id;
10071 Formal : Entity_Id;
10073 procedure Check_Candidate;
10074 -- The operation is a candidate if its first parameter is a
10075 -- controlling operand of the desired type.
10077 -----------------------
10078 -- Check_Candidate; --
10079 -----------------------
10081 procedure Check_Candidate is
10082 begin
10083 Formal := First_Formal (Subp);
10085 if Present (Formal)
10086 and then Is_Controlling_Formal (Formal)
10087 and then
10088 (Base_Type (Etype (Formal)) = Bas
10089 or else
10090 (Is_Access_Type (Etype (Formal))
10091 and then Designated_Type (Etype (Formal)) = Bas))
10092 then
10093 Append_Elmt (Subp, Candidates);
10094 end if;
10095 end Check_Candidate;
10097 -- Start of processing for Collect_Generic_Type_Ops
10099 begin
10100 if Is_Derived_Type (T) then
10101 return Primitive_Operations (T);
10103 elsif Ekind (Scope (T)) in E_Procedure | E_Function then
10105 -- Scan the list of generic formals to find subprograms
10106 -- that may have a first controlling formal of the type.
10108 if Nkind (Unit_Declaration_Node (Scope (T))) =
10109 N_Generic_Subprogram_Declaration
10110 then
10111 declare
10112 Decl : Node_Id;
10114 begin
10115 Decl :=
10116 First (Generic_Formal_Declarations
10117 (Unit_Declaration_Node (Scope (T))));
10118 while Present (Decl) loop
10119 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
10120 Subp := Defining_Entity (Decl);
10121 Check_Candidate;
10122 end if;
10124 Next (Decl);
10125 end loop;
10126 end;
10127 end if;
10128 return Candidates;
10130 else
10131 -- Scan the list of entities declared in the same scope as
10132 -- the type. In general this will be an open scope, given that
10133 -- the call we are analyzing can only appear within a generic
10134 -- declaration or body (either the one that declares T, or a
10135 -- child unit).
10137 -- For a subtype representing a generic actual type, go to the
10138 -- base type.
10140 if Is_Generic_Actual_Type (T) then
10141 Subp := First_Entity (Scope (Base_Type (T)));
10142 else
10143 Subp := First_Entity (Scope (T));
10144 end if;
10146 while Present (Subp) loop
10147 if Is_Overloadable (Subp) then
10148 Check_Candidate;
10149 end if;
10151 Next_Entity (Subp);
10152 end loop;
10154 return Candidates;
10155 end if;
10156 end Collect_Generic_Type_Ops;
10158 ----------------------------
10159 -- Extended_Primitive_Ops --
10160 ----------------------------
10162 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
10163 Type_Scope : constant Entity_Id := Scope (T);
10164 Op_List : Elist_Id := Primitive_Operations (T);
10165 begin
10166 if Is_Package_Or_Generic_Package (Type_Scope)
10167 and then ((In_Package_Body (Type_Scope)
10168 and then In_Open_Scopes (Type_Scope)) or else In_Instance_Body)
10169 then
10170 -- Retrieve list of declarations of package body if possible
10172 declare
10173 The_Body : constant Node_Id :=
10174 Corresponding_Body (Unit_Declaration_Node (Type_Scope));
10175 begin
10176 if Present (The_Body) then
10177 declare
10178 Body_Decls : constant List_Id :=
10179 Declarations (Unit_Declaration_Node (The_Body));
10180 Op_Found : Boolean := False;
10181 Op : Entity_Id := Current_Entity (Subprog);
10182 begin
10183 while Present (Op) loop
10184 if Comes_From_Source (Op)
10185 and then Is_Overloadable (Op)
10187 -- Exclude overriding primitive operations of a
10188 -- type extension declared in the package body,
10189 -- to prevent duplicates in extended list.
10191 and then not Is_Primitive (Op)
10192 and then Is_List_Member
10193 (Unit_Declaration_Node (Op))
10194 and then List_Containing
10195 (Unit_Declaration_Node (Op)) = Body_Decls
10196 then
10197 if not Op_Found then
10198 -- Copy list of primitives so it is not
10199 -- affected for other uses.
10201 Op_List := New_Copy_Elist (Op_List);
10202 Op_Found := True;
10203 end if;
10205 Append_Elmt (Op, Op_List);
10206 end if;
10208 Op := Homonym (Op);
10209 end loop;
10210 end;
10211 end if;
10212 end;
10213 end if;
10215 return Op_List;
10216 end Extended_Primitive_Ops;
10218 ---------------------------
10219 -- Is_Private_Overriding --
10220 ---------------------------
10222 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
10223 Visible_Op : Entity_Id;
10225 begin
10226 -- The subprogram may be overloaded with both visible and private
10227 -- entities with the same name. We have to scan the chain of
10228 -- homonyms to determine whether there is a previous implicit
10229 -- declaration in the same scope that is overridden by the
10230 -- private candidate.
10232 Visible_Op := Homonym (Op);
10233 while Present (Visible_Op) loop
10234 if Scope (Op) /= Scope (Visible_Op) then
10235 return False;
10237 elsif not Comes_From_Source (Visible_Op)
10238 and then Alias (Visible_Op) = Op
10239 then
10240 -- If Visible_Op or what it overrides is not hidden, then we
10241 -- have found what we're looking for.
10243 if not Is_Hidden (Visible_Op)
10244 or else not Is_Hidden (Overridden_Operation (Op))
10245 then
10246 return True;
10247 end if;
10248 end if;
10250 Visible_Op := Homonym (Visible_Op);
10251 end loop;
10253 return False;
10254 end Is_Private_Overriding;
10256 -----------------
10257 -- Names_Match --
10258 -----------------
10260 function Names_Match
10261 (Obj_Type : Entity_Id;
10262 Prim_Op : Entity_Id;
10263 Subprog : Entity_Id) return Boolean is
10264 begin
10265 -- Common case: exact match
10267 if Chars (Prim_Op) = Chars (Subprog) then
10268 return True;
10270 -- For protected type primitives the expander may have built the
10271 -- name of the dispatching primitive prepending the type name to
10272 -- avoid conflicts with the name of the protected subprogram (see
10273 -- Exp_Ch9.Build_Selected_Name).
10275 elsif Is_Protected_Type (Obj_Type) then
10276 return
10277 Present (Original_Protected_Subprogram (Prim_Op))
10278 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
10279 Chars (Subprog);
10281 -- In an instance, the selector name may be a generic actual that
10282 -- renames a primitive operation of the type of the prefix.
10284 elsif In_Instance and then Present (Current_Entity (Subprog)) then
10285 declare
10286 Subp : constant Entity_Id := Current_Entity (Subprog);
10287 begin
10288 if Present (Subp)
10289 and then Is_Subprogram (Subp)
10290 and then Present (Renamed_Entity (Subp))
10291 and then Is_Generic_Actual_Subprogram (Subp)
10292 and then Chars (Renamed_Entity (Subp)) = Chars (Prim_Op)
10293 then
10294 return True;
10295 end if;
10296 end;
10297 end if;
10299 return False;
10300 end Names_Match;
10302 -----------------------------
10303 -- Valid_First_Argument_Of --
10304 -----------------------------
10306 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
10307 Typ : Entity_Id := Etype (First_Formal (Op));
10309 begin
10310 if Is_Concurrent_Type (Typ)
10311 and then Present (Corresponding_Record_Type (Typ))
10312 then
10313 Typ := Corresponding_Record_Type (Typ);
10314 end if;
10316 -- Simple case. Object may be a subtype of the tagged type or may
10317 -- be the corresponding record of a synchronized type.
10319 return Obj_Type = Typ
10320 or else Base_Type (Obj_Type) = Base_Type (Typ)
10321 or else Corr_Type = Typ
10323 -- Object may be of a derived type whose parent has unknown
10324 -- discriminants, in which case the type matches the underlying
10325 -- record view of its base.
10327 or else
10328 (Has_Unknown_Discriminants (Typ)
10329 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
10331 -- Prefix can be dereferenced
10333 or else
10334 (Is_Access_Type (Corr_Type)
10335 and then Designated_Type (Corr_Type) = Typ)
10337 -- Formal is an access parameter, for which the object can
10338 -- provide an access.
10340 or else
10341 (Ekind (Typ) = E_Anonymous_Access_Type
10342 and then
10343 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
10344 end Valid_First_Argument_Of;
10346 -- Start of processing for Try_Primitive_Operation
10348 begin
10349 -- Look for subprograms in the list of primitive operations. The name
10350 -- must be identical, and the kind of call indicates the expected
10351 -- kind of operation (function or procedure). If the type is a
10352 -- (tagged) synchronized type, the primitive ops are attached to the
10353 -- corresponding record (base) type.
10355 if Is_Concurrent_Type (Obj_Type) then
10356 if Present (Corresponding_Record_Type (Obj_Type)) then
10357 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
10358 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
10359 else
10360 Corr_Type := Obj_Type;
10361 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
10362 end if;
10364 elsif not Is_Generic_Type (Obj_Type) then
10365 Corr_Type := Obj_Type;
10366 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
10368 else
10369 Corr_Type := Obj_Type;
10370 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
10371 end if;
10373 while Present (Elmt) loop
10374 Prim_Op := Node (Elmt);
10376 if Names_Match (Obj_Type, Prim_Op, Subprog)
10377 and then Present (First_Formal (Prim_Op))
10378 and then Valid_First_Argument_Of (Prim_Op)
10379 and then
10380 (Nkind (Call_Node) = N_Function_Call)
10382 (Ekind (Prim_Op) = E_Function)
10383 then
10384 -- Ada 2005 (AI-251): If this primitive operation corresponds
10385 -- to an immediate ancestor interface there is no need to add
10386 -- it to the list of interpretations; the corresponding aliased
10387 -- primitive is also in this list of primitive operations and
10388 -- will be used instead.
10390 if (Present (Interface_Alias (Prim_Op))
10391 and then Is_Ancestor (Find_Dispatching_Type
10392 (Alias (Prim_Op)), Corr_Type))
10394 -- Do not consider hidden primitives unless the type is in an
10395 -- open scope or we are within an instance, where visibility
10396 -- is known to be correct, or else if this is an overriding
10397 -- operation in the private part for an inherited operation.
10399 or else (Is_Hidden (Prim_Op)
10400 and then not Is_Immediately_Visible (Obj_Type)
10401 and then not In_Instance
10402 and then not Is_Private_Overriding (Prim_Op))
10403 then
10404 goto Continue;
10405 end if;
10407 Set_Etype (Call_Node, Any_Type);
10408 Set_Is_Overloaded (Call_Node, False);
10410 if No (Matching_Op) then
10411 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
10412 Candidate := Prim_Op;
10414 Set_Parent (Call_Node, Parent (Node_To_Replace));
10416 Set_Name (Call_Node, Prim_Op_Ref);
10417 Success := False;
10419 Analyze_One_Call
10420 (N => Call_Node,
10421 Nam => Prim_Op,
10422 Report => Report_Error,
10423 Success => Success,
10424 Skip_First => True);
10426 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
10428 -- More than one interpretation, collect for subsequent
10429 -- disambiguation. If this is a procedure call and there
10430 -- is another match, report ambiguity now.
10432 else
10433 Analyze_One_Call
10434 (N => Call_Node,
10435 Nam => Prim_Op,
10436 Report => Report_Error,
10437 Success => Success,
10438 Skip_First => True);
10440 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
10441 and then Nkind (Call_Node) /= N_Function_Call
10442 then
10443 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
10444 Report_Ambiguity (Matching_Op);
10445 Report_Ambiguity (Prim_Op);
10446 Check_Ambiguous_Aggregate (Call_Node);
10447 return True;
10448 end if;
10449 end if;
10450 end if;
10452 <<Continue>>
10453 Next_Elmt (Elmt);
10454 end loop;
10456 if Present (Matching_Op) then
10457 Set_Etype (Call_Node, Etype (Matching_Op));
10458 end if;
10460 return Present (Matching_Op);
10461 end Try_Primitive_Operation;
10463 ---------------------
10464 -- Valid_Candidate --
10465 ---------------------
10467 function Valid_Candidate
10468 (Success : Boolean;
10469 Call : Node_Id;
10470 Subp : Entity_Id) return Entity_Id
10472 Arr_Type : Entity_Id;
10473 Comp_Type : Entity_Id;
10475 begin
10476 -- If the subprogram is a valid interpretation, record it in global
10477 -- variable Subprog, to collect all possible overloadings.
10479 if Success then
10480 if Subp /= Entity (Subprog) then
10481 Add_One_Interp (Subprog, Subp, Etype (Subp));
10482 end if;
10483 end if;
10485 -- If the call may be an indexed call, retrieve component type of
10486 -- resulting expression, and add possible interpretation.
10488 Arr_Type := Empty;
10489 Comp_Type := Empty;
10491 if Nkind (Call) = N_Function_Call
10492 and then Nkind (Parent (N)) = N_Indexed_Component
10493 and then Needs_One_Actual (Subp)
10494 then
10495 if Is_Array_Type (Etype (Subp)) then
10496 Arr_Type := Etype (Subp);
10498 elsif Is_Access_Type (Etype (Subp))
10499 and then Is_Array_Type (Designated_Type (Etype (Subp)))
10500 then
10501 Arr_Type := Designated_Type (Etype (Subp));
10502 end if;
10503 end if;
10505 if Present (Arr_Type) then
10507 -- Verify that the actuals (excluding the object) match the types
10508 -- of the indexes.
10510 declare
10511 Actual : Node_Id;
10512 Index : Node_Id;
10514 begin
10515 Actual := Next (First_Actual (Call));
10516 Index := First_Index (Arr_Type);
10517 while Present (Actual) and then Present (Index) loop
10518 if not Has_Compatible_Type (Actual, Etype (Index)) then
10519 Arr_Type := Empty;
10520 exit;
10521 end if;
10523 Next_Actual (Actual);
10524 Next_Index (Index);
10525 end loop;
10527 if No (Actual)
10528 and then No (Index)
10529 and then Present (Arr_Type)
10530 then
10531 Comp_Type := Component_Type (Arr_Type);
10532 end if;
10533 end;
10535 if Present (Comp_Type)
10536 and then Etype (Subprog) /= Comp_Type
10537 then
10538 Add_One_Interp (Subprog, Subp, Comp_Type);
10539 end if;
10540 end if;
10542 if Etype (Call) /= Any_Type then
10543 return Subp;
10544 else
10545 return Empty;
10546 end if;
10547 end Valid_Candidate;
10549 -- Start of processing for Try_Object_Operation
10551 begin
10552 Analyze_Expression (Obj);
10554 -- Analyze the actuals if node is known to be a subprogram call
10556 if Is_Subprg_Call and then N = Name (Parent (N)) then
10557 Actual := First (Parameter_Associations (Parent (N)));
10558 while Present (Actual) loop
10559 Analyze_Expression (Actual);
10560 Next (Actual);
10561 end loop;
10562 end if;
10564 -- Build a subprogram call node, using a copy of Obj as its first
10565 -- actual. This is a placeholder, to be replaced by an explicit
10566 -- dereference when needed.
10568 Transform_Object_Operation
10569 (Call_Node => New_Call_Node,
10570 Node_To_Replace => Node_To_Replace);
10572 Set_Etype (New_Call_Node, Any_Type);
10573 Set_Etype (Subprog, Any_Type);
10574 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
10576 if not Is_Overloaded (Obj) then
10577 Try_One_Prefix_Interpretation (Obj_Type);
10579 else
10580 declare
10581 I : Interp_Index;
10582 It : Interp;
10583 begin
10584 Get_First_Interp (Obj, I, It);
10585 while Present (It.Nam) loop
10586 Try_One_Prefix_Interpretation (It.Typ);
10587 Get_Next_Interp (I, It);
10588 end loop;
10589 end;
10590 end if;
10592 if Etype (New_Call_Node) /= Any_Type then
10594 -- No need to complete the tree transformations if we are only
10595 -- searching for conflicting class-wide subprograms
10597 if CW_Test_Only then
10598 return False;
10599 else
10600 Complete_Object_Operation
10601 (Call_Node => New_Call_Node,
10602 Node_To_Replace => Node_To_Replace);
10603 return True;
10604 end if;
10606 elsif Present (Candidate) then
10608 -- The argument list is not type correct. Re-analyze with error
10609 -- reporting enabled, and use one of the possible candidates.
10610 -- In All_Errors_Mode, re-analyze all failed interpretations.
10612 if All_Errors_Mode then
10613 Report_Error := True;
10614 if Try_Primitive_Operation
10615 (Call_Node => New_Call_Node,
10616 Node_To_Replace => Node_To_Replace)
10618 or else
10619 Try_Class_Wide_Operation
10620 (Call_Node => New_Call_Node,
10621 Node_To_Replace => Node_To_Replace)
10622 then
10623 null;
10624 end if;
10626 else
10627 Analyze_One_Call
10628 (N => New_Call_Node,
10629 Nam => Candidate,
10630 Report => True,
10631 Success => Success,
10632 Skip_First => True);
10634 -- The error may hot have been reported yet for overloaded
10635 -- prefixed calls, depending on the non-matching candidate,
10636 -- in which case provide a concise error now.
10638 if Serious_Errors_Detected = 0 then
10639 Error_Msg_NE
10640 ("cannot resolve prefixed call to primitive operation of&",
10641 N, Entity (Obj));
10642 end if;
10643 end if;
10645 -- No need for further errors
10647 return True;
10649 else
10650 -- There was no candidate operation, but Analyze_Selected_Component
10651 -- may continue the analysis so we need to undo the change possibly
10652 -- made to the Parent of N earlier by Transform_Object_Operation.
10654 declare
10655 Parent_Node : constant Node_Id := Parent (N);
10657 begin
10658 if Node_To_Replace = Parent_Node then
10659 Remove (First (Parameter_Associations (New_Call_Node)));
10660 Set_Parent
10661 (Parameter_Associations (New_Call_Node), Parent_Node);
10662 end if;
10663 end;
10665 return False;
10666 end if;
10667 end Try_Object_Operation;
10669 -------------------------
10670 -- Unresolved_Operator --
10671 -------------------------
10673 procedure Unresolved_Operator (N : Node_Id) is
10674 L : constant Node_Id :=
10675 (if Nkind (N) in N_Binary_Op then Left_Opnd (N) else Empty);
10676 R : constant Node_Id := Right_Opnd (N);
10678 Op_Id : Entity_Id;
10680 begin
10681 -- Note that in the following messages, if the operand is overloaded we
10682 -- choose an arbitrary type to complain about, but that is probably more
10683 -- useful than not giving a type at all.
10685 if Nkind (N) in N_Unary_Op then
10686 Error_Msg_Node_2 := Etype (R);
10687 Error_Msg_N ("operator& not defined for}", N);
10689 elsif Nkind (N) in N_Binary_Op then
10690 if not Is_Overloaded (L)
10691 and then not Is_Overloaded (R)
10692 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
10693 then
10694 Error_Msg_Node_2 := First_Subtype (Etype (R));
10695 Error_Msg_N ("there is no applicable operator& for}", N);
10697 else
10698 -- Another attempt to find a fix: one of the candidate
10699 -- interpretations may not be use-visible. This has
10700 -- already been checked for predefined operators, so
10701 -- we examine only user-defined functions.
10703 Op_Id := Get_Name_Entity_Id (Chars (N));
10705 while Present (Op_Id) loop
10706 if Ekind (Op_Id) /= E_Operator
10707 and then Is_Overloadable (Op_Id)
10708 and then not Is_Immediately_Visible (Op_Id)
10709 and then not In_Use (Scope (Op_Id))
10710 and then not Is_Abstract_Subprogram (Op_Id)
10711 and then not Is_Hidden (Op_Id)
10712 and then Ekind (Scope (Op_Id)) = E_Package
10713 and then Has_Compatible_Type (L, Etype (First_Formal (Op_Id)))
10714 and then Present (Next_Formal (First_Formal (Op_Id)))
10715 and then
10716 Has_Compatible_Type
10717 (R, Etype (Next_Formal (First_Formal (Op_Id))))
10718 then
10719 Error_Msg_N ("no legal interpretation for operator&", N);
10720 Error_Msg_NE ("\use clause on& would make operation legal",
10721 N, Scope (Op_Id));
10722 exit;
10723 end if;
10725 Op_Id := Homonym (Op_Id);
10726 end loop;
10728 if No (Op_Id) then
10729 Error_Msg_N ("invalid operand types for operator&", N);
10731 if Nkind (N) /= N_Op_Concat then
10732 Error_Msg_NE ("\left operand has}!", N, Etype (L));
10733 Error_Msg_NE ("\right operand has}!", N, Etype (R));
10735 -- For multiplication and division operators with
10736 -- a fixed-point operand and an integer operand,
10737 -- indicate that the integer operand should be of
10738 -- type Integer.
10740 if Nkind (N) in N_Op_Multiply | N_Op_Divide
10741 and then Is_Fixed_Point_Type (Etype (L))
10742 and then Is_Integer_Type (Etype (R))
10743 then
10744 Error_Msg_N ("\convert right operand to `Integer`", N);
10746 elsif Nkind (N) = N_Op_Multiply
10747 and then Is_Fixed_Point_Type (Etype (R))
10748 and then Is_Integer_Type (Etype (L))
10749 then
10750 Error_Msg_N ("\convert left operand to `Integer`", N);
10751 end if;
10753 -- For concatenation operators it is more difficult to
10754 -- determine which is the wrong operand. It is worth
10755 -- flagging explicitly an access type, for those who
10756 -- might think that a dereference happens here.
10758 elsif Is_Access_Type (Etype (L)) then
10759 Error_Msg_N ("\left operand is access type", N);
10761 elsif Is_Access_Type (Etype (R)) then
10762 Error_Msg_N ("\right operand is access type", N);
10763 end if;
10764 end if;
10765 end if;
10766 end if;
10767 end Unresolved_Operator;
10769 ---------
10770 -- wpo --
10771 ---------
10773 procedure wpo (T : Entity_Id) is
10774 Op : Entity_Id;
10775 E : Elmt_Id;
10777 begin
10778 if not Is_Tagged_Type (T) then
10779 return;
10780 end if;
10782 E := First_Elmt (Primitive_Operations (Base_Type (T)));
10783 while Present (E) loop
10784 Op := Node (E);
10785 Write_Int (Int (Op));
10786 Write_Str (" === ");
10787 Write_Name (Chars (Op));
10788 Write_Str (" in ");
10789 Write_Name (Chars (Scope (Op)));
10790 Next_Elmt (E);
10791 Write_Eol;
10792 end loop;
10793 end wpo;
10795 end Sem_Ch4;