ada: Simplify "not Present" with "No"
[official-gcc.git] / gcc / ada / sem_ch4.adb
blob50ba6c9c847c408e66298159b91ec50baed43d02
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Util; use Exp_Util;
36 with Itypes; use Itypes;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Dim; use Sem_Dim;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.Nodes; use Sinfo.Nodes;
66 with Sinfo.Utils; use Sinfo.Utils;
67 with Snames; use Snames;
68 with 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 declare
722 Not_Null_Check : constant Node_Id :=
723 Make_Raise_Constraint_Error (Sloc (E),
724 Reason => CE_Null_Not_Allowed);
726 begin
727 if Expander_Active then
728 Insert_Action (N, Not_Null_Check);
729 Analyze (Not_Null_Check);
731 elsif Warn_On_Ada_2012_Compatibility then
732 Error_Msg_N
733 ("null value not allowed here in Ada 2012?y?", E);
734 end if;
735 end;
736 end if;
738 -- Check for missing initialization. Skip this check if the allocator
739 -- is made for a special return object or if we already had errors on
740 -- analyzing the allocator since, in that case, these are very likely
741 -- cascaded errors.
743 if not Is_Definite_Subtype (Type_Id)
744 and then not For_Special_Return_Object (N)
745 and then Serious_Errors_Detected = Sav_Errs
746 then
747 if Is_Class_Wide_Type (Type_Id) then
748 Error_Msg_N
749 ("initialization required in class-wide allocation", N);
751 else
752 if Ada_Version < Ada_2005
753 and then Is_Limited_Type (Type_Id)
754 then
755 Error_Msg_N ("unconstrained allocation not allowed", N);
757 if Is_Array_Type (Type_Id) then
758 Error_Msg_N
759 ("\constraint with array bounds required", N);
761 elsif Has_Unknown_Discriminants (Type_Id) then
762 null;
764 else pragma Assert (Has_Discriminants (Type_Id));
765 Error_Msg_N
766 ("\constraint with discriminant values required", N);
767 end if;
769 -- Limited Ada 2005 and general nonlimited case.
770 -- This is an error, except in the case of an
771 -- uninitialized allocator that is generated
772 -- for a build-in-place function return of a
773 -- discriminated but compile-time-known-size
774 -- type.
776 else
777 if Is_Rewrite_Substitution (N)
778 and then Nkind (Original_Node (N)) = N_Allocator
779 then
780 declare
781 Qual : constant Node_Id :=
782 Expression (Original_Node (N));
783 pragma Assert
784 (Nkind (Qual) = N_Qualified_Expression);
785 Call : constant Node_Id := Expression (Qual);
786 pragma Assert
787 (Is_Expanded_Build_In_Place_Call (Call));
788 begin
789 null;
790 end;
792 else
793 Error_Msg_N
794 ("uninitialized unconstrained allocation not "
795 & "allowed", N);
797 if Is_Array_Type (Type_Id) then
798 Error_Msg_N
799 ("\qualified expression or constraint with "
800 & "array bounds required", N);
802 elsif Has_Unknown_Discriminants (Type_Id) then
803 Error_Msg_N ("\qualified expression required", N);
805 else pragma Assert (Has_Discriminants (Type_Id));
806 Error_Msg_N
807 ("\qualified expression or constraint with "
808 & "discriminant values required", N);
809 end if;
810 end if;
811 end if;
812 end if;
813 end if;
814 end if;
816 if Is_Abstract_Type (Type_Id) then
817 Error_Msg_N ("cannot allocate abstract object", E);
818 end if;
820 Set_Etype (N, Acc_Type);
822 -- If this is an allocator for the return stack, then no restriction may
823 -- be violated since it's just a low-level access to the primary stack.
825 if Nkind (Parent (N)) = N_Object_Declaration
826 and then Is_Entity_Name (Object_Definition (Parent (N)))
827 and then Is_Access_Type (Entity (Object_Definition (Parent (N))))
828 then
829 declare
830 Pool : constant Entity_Id :=
831 Associated_Storage_Pool
832 (Root_Type (Entity (Object_Definition (Parent (N)))));
834 begin
835 if Present (Pool) and then Is_RTE (Pool, RE_RS_Pool) then
836 goto Leave;
837 end if;
838 end;
839 end if;
841 if Has_Task (Designated_Type (Acc_Type)) then
842 Check_Restriction (No_Tasking, N);
843 Check_Restriction (Max_Tasks, N);
844 Check_Restriction (No_Task_Allocators, N);
845 end if;
847 -- Check restriction against dynamically allocated protected objects
849 if Has_Protected (Designated_Type (Acc_Type)) then
850 Check_Restriction (No_Protected_Type_Allocators, N);
851 end if;
853 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
854 -- type is nested, and the designated type needs finalization. The rule
855 -- is conservative in that class-wide types need finalization.
857 if Needs_Finalization (Designated_Type (Acc_Type))
858 and then not Is_Library_Level_Entity (Acc_Type)
859 then
860 Check_Restriction (No_Nested_Finalization, N);
861 end if;
863 -- Check that an allocator of a nested access type doesn't create a
864 -- protected object when restriction No_Local_Protected_Objects applies.
866 if Has_Protected (Designated_Type (Acc_Type))
867 and then not Is_Library_Level_Entity (Acc_Type)
868 then
869 Check_Restriction (No_Local_Protected_Objects, N);
870 end if;
872 -- Likewise for No_Local_Timing_Events
874 if Has_Timing_Event (Designated_Type (Acc_Type))
875 and then not Is_Library_Level_Entity (Acc_Type)
876 then
877 Check_Restriction (No_Local_Timing_Events, N);
878 end if;
880 -- If the No_Streams restriction is set, check that the type of the
881 -- object is not, and does not contain, any subtype derived from
882 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
883 -- Has_Stream just for efficiency reasons. There is no point in
884 -- spending time on a Has_Stream check if the restriction is not set.
886 if Restriction_Check_Required (No_Streams) then
887 if Has_Stream (Designated_Type (Acc_Type)) then
888 Check_Restriction (No_Streams, N);
889 end if;
890 end if;
892 if not Is_Library_Level_Entity (Acc_Type) then
893 Check_Restriction (No_Local_Allocators, N);
894 end if;
896 <<Leave>>
897 if Serious_Errors_Detected > Sav_Errs then
898 Set_Error_Posted (N);
899 Set_Etype (N, Any_Type);
900 end if;
901 end Analyze_Allocator;
903 ---------------------------
904 -- Analyze_Arithmetic_Op --
905 ---------------------------
907 procedure Analyze_Arithmetic_Op (N : Node_Id) is
908 L : constant Node_Id := Left_Opnd (N);
909 R : constant Node_Id := Right_Opnd (N);
911 Op_Id : Entity_Id;
913 begin
914 Set_Etype (N, Any_Type);
915 Candidate_Type := Empty;
917 Analyze_Expression (L);
918 Analyze_Expression (R);
920 -- If the entity is already set, the node is the instantiation of a
921 -- generic node with a non-local reference, or was manufactured by a
922 -- call to Make_Op_xxx. In either case the entity is known to be valid,
923 -- and we do not need to collect interpretations, instead we just get
924 -- the single possible interpretation.
926 if Present (Entity (N)) then
927 Op_Id := Entity (N);
929 if Ekind (Op_Id) = E_Operator then
930 Find_Arithmetic_Types (L, R, Op_Id, N);
931 else
932 Add_One_Interp (N, Op_Id, Etype (Op_Id));
933 end if;
935 -- Entity is not already set, so we do need to collect interpretations
937 else
938 Op_Id := Get_Name_Entity_Id (Chars (N));
939 while Present (Op_Id) loop
940 if Ekind (Op_Id) = E_Operator
941 and then Present (Next_Entity (First_Entity (Op_Id)))
942 then
943 Find_Arithmetic_Types (L, R, Op_Id, N);
945 -- The following may seem superfluous, because an operator cannot
946 -- be generic, but this ignores the cleverness of the author of
947 -- ACVC bc1013a.
949 elsif Is_Overloadable (Op_Id) then
950 Analyze_User_Defined_Binary_Op (N, Op_Id);
951 end if;
953 Op_Id := Homonym (Op_Id);
954 end loop;
955 end if;
957 Operator_Check (N);
958 Check_Function_Writable_Actuals (N);
959 end Analyze_Arithmetic_Op;
961 ------------------
962 -- Analyze_Call --
963 ------------------
965 -- Function, procedure, and entry calls are checked here. The Name in
966 -- the call may be overloaded. The actuals have been analyzed and may
967 -- themselves be overloaded. On exit from this procedure, the node N
968 -- may have zero, one or more interpretations. In the first case an
969 -- error message is produced. In the last case, the node is flagged
970 -- as overloaded and the interpretations are collected in All_Interp.
972 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
973 -- the type-checking is similar to that of other calls.
975 procedure Analyze_Call (N : Node_Id) is
976 Actuals : constant List_Id := Parameter_Associations (N);
977 Loc : constant Source_Ptr := Sloc (N);
978 Nam : Node_Id;
979 X : Interp_Index;
980 It : Interp;
981 Nam_Ent : Entity_Id := Empty;
982 Success : Boolean := False;
984 Deref : Boolean := False;
985 -- Flag indicates whether an interpretation of the prefix is a
986 -- parameterless call that returns an access_to_subprogram.
988 procedure Check_Writable_Actuals (N : Node_Id);
989 -- If the call has out or in-out parameters then mark its outermost
990 -- enclosing construct as a node on which the writable actuals check
991 -- must be performed.
993 function Name_Denotes_Function return Boolean;
994 -- If the type of the name is an access to subprogram, this may be the
995 -- type of a name, or the return type of the function being called. If
996 -- the name is not an entity then it can denote a protected function.
997 -- Until we distinguish Etype from Return_Type, we must use this routine
998 -- to resolve the meaning of the name in the call.
1000 procedure No_Interpretation;
1001 -- Output error message when no valid interpretation exists
1003 ----------------------------
1004 -- Check_Writable_Actuals --
1005 ----------------------------
1007 -- The identification of conflicts in calls to functions with writable
1008 -- actuals is performed in the analysis phase of the front end to ensure
1009 -- that it reports exactly the same errors compiling with and without
1010 -- expansion enabled. It is performed in two stages:
1012 -- 1) When a call to a function with out-mode parameters is found,
1013 -- we climb to the outermost enclosing construct that can be
1014 -- evaluated in arbitrary order and we mark it with the flag
1015 -- Check_Actuals.
1017 -- 2) When the analysis of the marked node is complete, we traverse
1018 -- its decorated subtree searching for conflicts (see function
1019 -- Sem_Util.Check_Function_Writable_Actuals).
1021 -- The unique exception to this general rule is for aggregates, since
1022 -- their analysis is performed by the front end in the resolution
1023 -- phase. For aggregates we do not climb to their enclosing construct:
1024 -- we restrict the analysis to the subexpressions initializing the
1025 -- aggregate components.
1027 -- This implies that the analysis of expressions containing aggregates
1028 -- is not complete, since there may be conflicts on writable actuals
1029 -- involving subexpressions of the enclosing logical or arithmetic
1030 -- expressions. However, we cannot wait and perform the analysis when
1031 -- the whole subtree is resolved, since the subtrees may be transformed,
1032 -- thus adding extra complexity and computation cost to identify and
1033 -- report exactly the same errors compiling with and without expansion
1034 -- enabled.
1036 procedure Check_Writable_Actuals (N : Node_Id) is
1037 begin
1038 if Comes_From_Source (N)
1039 and then Present (Get_Subprogram_Entity (N))
1040 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1041 then
1042 -- For procedures and entries there is no need to climb since
1043 -- we only need to check if the actuals of this call invoke
1044 -- functions whose out-mode parameters overlap.
1046 if Nkind (N) /= N_Function_Call then
1047 Set_Check_Actuals (N);
1049 -- For calls to functions we climb to the outermost enclosing
1050 -- construct where the out-mode actuals of this function may
1051 -- introduce conflicts.
1053 else
1054 declare
1055 Outermost : Node_Id := Empty; -- init to avoid warning
1056 P : Node_Id := N;
1058 begin
1059 while Present (P) loop
1060 -- For object declarations we can climb to the node from
1061 -- its object definition branch or from its initializing
1062 -- expression. We prefer to mark the child node as the
1063 -- outermost construct to avoid adding further complexity
1064 -- to the routine that will later take care of
1065 -- performing the writable actuals check.
1067 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1068 and then Nkind (P) not in
1069 N_Assignment_Statement | N_Object_Declaration
1070 then
1071 Outermost := P;
1072 end if;
1074 -- Avoid climbing more than needed
1076 exit when Stop_Subtree_Climbing (Nkind (P))
1077 or else (Nkind (P) = N_Range
1078 and then
1079 Nkind (Parent (P)) not in N_In | N_Not_In);
1081 P := Parent (P);
1082 end loop;
1084 Set_Check_Actuals (Outermost);
1085 end;
1086 end if;
1087 end if;
1088 end Check_Writable_Actuals;
1090 ---------------------------
1091 -- Name_Denotes_Function --
1092 ---------------------------
1094 function Name_Denotes_Function return Boolean is
1095 begin
1096 if Is_Entity_Name (Nam) then
1097 return Ekind (Entity (Nam)) = E_Function;
1098 elsif Nkind (Nam) = N_Selected_Component then
1099 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1100 else
1101 return False;
1102 end if;
1103 end Name_Denotes_Function;
1105 -----------------------
1106 -- No_Interpretation --
1107 -----------------------
1109 procedure No_Interpretation is
1110 L : constant Boolean := Is_List_Member (N);
1111 K : constant Node_Kind := Nkind (Parent (N));
1113 begin
1114 -- If the node is in a list whose parent is not an expression then it
1115 -- must be an attempted procedure call.
1117 if L and then K not in N_Subexpr then
1118 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1119 Error_Msg_NE
1120 ("must instantiate generic procedure& before call",
1121 Nam, Entity (Nam));
1122 else
1123 Error_Msg_N ("procedure or entry name expected", Nam);
1124 end if;
1126 -- Check for tasking cases where only an entry call will do
1128 elsif not L
1129 and then K in N_Entry_Call_Alternative | N_Triggering_Alternative
1130 then
1131 Error_Msg_N ("entry name expected", Nam);
1133 -- Otherwise give general error message
1135 else
1136 Error_Msg_N ("invalid prefix in call", Nam);
1137 end if;
1138 end No_Interpretation;
1140 -- Start of processing for Analyze_Call
1142 begin
1143 -- Initialize the type of the result of the call to the error type,
1144 -- which will be reset if the type is successfully resolved.
1146 Set_Etype (N, Any_Type);
1148 Nam := Name (N);
1150 if not Is_Overloaded (Nam) then
1152 -- Only one interpretation to check
1154 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1155 Nam_Ent := Etype (Nam);
1157 -- If the prefix is an access_to_subprogram, this may be an indirect
1158 -- call. This is the case if the name in the call is not an entity
1159 -- name, or if it is a function name in the context of a procedure
1160 -- call. In this latter case, we have a call to a parameterless
1161 -- function that returns a pointer_to_procedure which is the entity
1162 -- being called. Finally, F (X) may be a call to a parameterless
1163 -- function that returns a pointer to a function with parameters.
1164 -- Note that if F returns an access-to-subprogram whose designated
1165 -- type is an array, F (X) cannot be interpreted as an indirect call
1166 -- through the result of the call to F.
1168 elsif Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
1169 and then
1170 (not Name_Denotes_Function
1171 or else Nkind (N) = N_Procedure_Call_Statement
1172 or else
1173 (Nkind (Parent (N)) /= N_Explicit_Dereference
1174 and then Is_Entity_Name (Nam)
1175 and then No (First_Formal (Entity (Nam)))
1176 and then not
1177 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1178 and then Present (Actuals)))
1179 then
1180 Nam_Ent := Designated_Type (Etype (Nam));
1181 Insert_Explicit_Dereference (Nam);
1183 -- Selected component case. Simple entry or protected operation,
1184 -- where the entry name is given by the selector name.
1186 elsif Nkind (Nam) = N_Selected_Component then
1187 Nam_Ent := Entity (Selector_Name (Nam));
1189 if Ekind (Nam_Ent) not in E_Entry
1190 | E_Entry_Family
1191 | E_Function
1192 | E_Procedure
1193 then
1194 Error_Msg_N ("name in call is not a callable entity", Nam);
1195 Set_Etype (N, Any_Type);
1196 return;
1197 end if;
1199 -- If the name is an Indexed component, it can be a call to a member
1200 -- of an entry family. The prefix must be a selected component whose
1201 -- selector is the entry. Analyze_Procedure_Call normalizes several
1202 -- kinds of call into this form.
1204 elsif Nkind (Nam) = N_Indexed_Component then
1205 if Nkind (Prefix (Nam)) = N_Selected_Component then
1206 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1207 else
1208 Error_Msg_N ("name in call is not a callable entity", Nam);
1209 Set_Etype (N, Any_Type);
1210 return;
1211 end if;
1213 elsif not Is_Entity_Name (Nam) then
1214 Error_Msg_N ("name in call is not a callable entity", Nam);
1215 Set_Etype (N, Any_Type);
1216 return;
1218 else
1219 Nam_Ent := Entity (Nam);
1221 -- If not overloadable, this may be a generalized indexing
1222 -- operation with named associations. Rewrite again as an
1223 -- indexed component and analyze as container indexing.
1225 if not Is_Overloadable (Nam_Ent) then
1226 if Present
1227 (Find_Value_Of_Aspect
1228 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1229 then
1230 Replace (N,
1231 Make_Indexed_Component (Sloc (N),
1232 Prefix => Nam,
1233 Expressions => Parameter_Associations (N)));
1235 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1236 return;
1237 else
1238 No_Interpretation;
1239 end if;
1241 else
1242 No_Interpretation;
1243 end if;
1245 return;
1246 end if;
1247 end if;
1249 -- Operations generated for RACW stub types are called only through
1250 -- dispatching, and can never be the static interpretation of a call.
1252 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1253 No_Interpretation;
1254 return;
1255 end if;
1257 Analyze_One_Call (N, Nam_Ent, True, Success);
1259 -- If the nonoverloaded interpretation is a call to an abstract
1260 -- nondispatching operation, then flag an error and return.
1262 if Is_Overloadable (Nam_Ent)
1263 and then Is_Abstract_Subprogram (Nam_Ent)
1264 and then not Is_Dispatching_Operation (Nam_Ent)
1265 then
1266 Nondispatching_Call_To_Abstract_Operation (N, Nam_Ent);
1267 return;
1268 end if;
1270 -- If this is an indirect call, the return type of the access_to
1271 -- subprogram may be an incomplete type. At the point of the call,
1272 -- use the full type if available, and at the same time update the
1273 -- return type of the access_to_subprogram.
1275 if Success
1276 and then Nkind (Nam) = N_Explicit_Dereference
1277 and then Ekind (Etype (N)) = E_Incomplete_Type
1278 and then Present (Full_View (Etype (N)))
1279 then
1280 Set_Etype (N, Full_View (Etype (N)));
1281 Set_Etype (Nam_Ent, Etype (N));
1282 end if;
1284 -- Overloaded call
1286 else
1287 -- An overloaded selected component must denote overloaded operations
1288 -- of a concurrent type. The interpretations are attached to the
1289 -- simple name of those operations.
1291 if Nkind (Nam) = N_Selected_Component then
1292 Nam := Selector_Name (Nam);
1293 end if;
1295 Get_First_Interp (Nam, X, It);
1296 while Present (It.Nam) loop
1297 Nam_Ent := It.Nam;
1298 Deref := False;
1300 -- Name may be call that returns an access to subprogram, or more
1301 -- generally an overloaded expression one of whose interpretations
1302 -- yields an access to subprogram. If the name is an entity, we do
1303 -- not dereference, because the node is a call that returns the
1304 -- access type: note difference between f(x), where the call may
1305 -- return an access subprogram type, and f(x)(y), where the type
1306 -- returned by the call to f is implicitly dereferenced to analyze
1307 -- the outer call.
1309 if Is_Access_Type (Nam_Ent) then
1310 Nam_Ent := Designated_Type (Nam_Ent);
1312 elsif Is_Access_Type (Etype (Nam_Ent))
1313 and then
1314 (not Is_Entity_Name (Nam)
1315 or else Nkind (N) = N_Procedure_Call_Statement)
1316 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1317 = E_Subprogram_Type
1318 then
1319 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1321 if Is_Entity_Name (Nam) then
1322 Deref := True;
1323 end if;
1324 end if;
1326 -- If the call has been rewritten from a prefixed call, the first
1327 -- parameter has been analyzed, but may need a subsequent
1328 -- dereference, so skip its analysis now.
1330 if Is_Rewrite_Substitution (N)
1331 and then Nkind (Original_Node (N)) = Nkind (N)
1332 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1333 and then Present (Parameter_Associations (N))
1334 and then Present (Etype (First (Parameter_Associations (N))))
1335 then
1336 Analyze_One_Call
1337 (N, Nam_Ent, False, Success, Skip_First => True);
1338 else
1339 Analyze_One_Call (N, Nam_Ent, False, Success);
1340 end if;
1342 -- If the interpretation succeeds, mark the proper type of the
1343 -- prefix (any valid candidate will do). If not, remove the
1344 -- candidate interpretation. If this is a parameterless call
1345 -- on an anonymous access to subprogram, X is a variable with
1346 -- an access discriminant D, the entity in the interpretation is
1347 -- D, so rewrite X as X.D.all.
1349 if Success then
1350 if Deref
1351 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1352 then
1353 if Ekind (It.Nam) = E_Discriminant
1354 and then Has_Implicit_Dereference (It.Nam)
1355 then
1356 Rewrite (Name (N),
1357 Make_Explicit_Dereference (Loc,
1358 Prefix =>
1359 Make_Selected_Component (Loc,
1360 Prefix =>
1361 New_Occurrence_Of (Entity (Nam), Loc),
1362 Selector_Name =>
1363 New_Occurrence_Of (It.Nam, Loc))));
1365 Analyze (N);
1366 return;
1368 else
1369 Set_Entity (Nam, It.Nam);
1370 Insert_Explicit_Dereference (Nam);
1371 Set_Etype (Nam, Nam_Ent);
1372 end if;
1374 else
1375 Set_Etype (Nam, It.Typ);
1376 end if;
1378 elsif Nkind (Name (N)) in N_Function_Call | N_Selected_Component
1379 then
1380 Remove_Interp (X);
1381 end if;
1383 Get_Next_Interp (X, It);
1384 end loop;
1386 -- If the name is the result of a function call, it can only be a
1387 -- call to a function returning an access to subprogram. Insert
1388 -- explicit dereference.
1390 if Nkind (Nam) = N_Function_Call then
1391 Insert_Explicit_Dereference (Nam);
1392 end if;
1394 if Etype (N) = Any_Type then
1396 -- None of the interpretations is compatible with the actuals
1398 Diagnose_Call (N, Nam);
1400 -- Special checks for uninstantiated put routines
1402 if Nkind (N) = N_Procedure_Call_Statement
1403 and then Is_Entity_Name (Nam)
1404 and then Chars (Nam) = Name_Put
1405 and then List_Length (Actuals) = 1
1406 then
1407 declare
1408 Arg : constant Node_Id := First (Actuals);
1409 Typ : Entity_Id;
1411 begin
1412 if Nkind (Arg) = N_Parameter_Association then
1413 Typ := Etype (Explicit_Actual_Parameter (Arg));
1414 else
1415 Typ := Etype (Arg);
1416 end if;
1418 if Is_Signed_Integer_Type (Typ) then
1419 Error_Msg_N
1420 ("possible missing instantiation of "
1421 & "'Text_'I'O.'Integer_'I'O!", Nam);
1423 elsif Is_Modular_Integer_Type (Typ) then
1424 Error_Msg_N
1425 ("possible missing instantiation of "
1426 & "'Text_'I'O.'Modular_'I'O!", Nam);
1428 elsif Is_Floating_Point_Type (Typ) then
1429 Error_Msg_N
1430 ("possible missing instantiation of "
1431 & "'Text_'I'O.'Float_'I'O!", Nam);
1433 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1434 Error_Msg_N
1435 ("possible missing instantiation of "
1436 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1438 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1439 Error_Msg_N
1440 ("possible missing instantiation of "
1441 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1443 elsif Is_Enumeration_Type (Typ) then
1444 Error_Msg_N
1445 ("possible missing instantiation of "
1446 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1447 end if;
1448 end;
1449 end if;
1451 elsif not Is_Overloaded (N)
1452 and then Is_Entity_Name (Nam)
1453 then
1454 -- Resolution yields a single interpretation. Verify that the
1455 -- reference has capitalization consistent with the declaration.
1457 Set_Entity_With_Checks (Nam, Entity (Nam));
1458 Generate_Reference (Entity (Nam), Nam);
1460 Set_Etype (Nam, Etype (Entity (Nam)));
1461 else
1462 Remove_Abstract_Operations (N);
1463 end if;
1464 end if;
1466 -- Check the accessibility level for actuals for explicitly aliased
1467 -- formals when a function call appears within a return statement.
1468 -- This is only checked if the enclosing subprogram Comes_From_Source,
1469 -- to avoid issuing errors on calls occurring in wrapper subprograms
1470 -- (for example, where the call is part of an expression of an aspect
1471 -- associated with a wrapper, such as Pre'Class).
1473 if Nkind (N) = N_Function_Call
1474 and then Comes_From_Source (N)
1475 and then Present (Nam_Ent)
1476 and then In_Return_Value (N)
1477 and then Comes_From_Source (Current_Subprogram)
1478 then
1479 declare
1480 Form : Node_Id;
1481 Act : Node_Id;
1482 begin
1483 Act := First_Actual (N);
1484 Form := First_Formal (Nam_Ent);
1486 while Present (Form) and then Present (Act) loop
1487 -- Check whether the formal is aliased and if the accessibility
1488 -- level of the actual is deeper than the accessibility level
1489 -- of the enclosing subprogram to which the current return
1490 -- statement applies.
1492 -- Should we be checking Is_Entity_Name on Act? Won't this miss
1493 -- other cases ???
1495 if Is_Explicitly_Aliased (Form)
1496 and then Is_Entity_Name (Act)
1497 and then Static_Accessibility_Level
1498 (Act, Zero_On_Dynamic_Level)
1499 > Subprogram_Access_Level (Current_Subprogram)
1500 then
1501 Error_Msg_N ("actual for explicitly aliased formal is too"
1502 & " short lived", Act);
1503 end if;
1505 Next_Formal (Form);
1506 Next_Actual (Act);
1507 end loop;
1508 end;
1509 end if;
1511 if Ada_Version >= Ada_2012 then
1513 -- Check if the call contains a function with writable actuals
1515 Check_Writable_Actuals (N);
1517 -- If found and the outermost construct that can be evaluated in
1518 -- an arbitrary order is precisely this call, then check all its
1519 -- actuals.
1521 Check_Function_Writable_Actuals (N);
1523 -- The return type of the function may be incomplete. This can be
1524 -- the case if the type is a generic formal, or a limited view. It
1525 -- can also happen when the function declaration appears before the
1526 -- full view of the type (which is legal in Ada 2012) and the call
1527 -- appears in a different unit, in which case the incomplete view
1528 -- must be replaced with the full view (or the nonlimited view)
1529 -- to prevent subsequent type errors. Note that the usual install/
1530 -- removal of limited_with clauses is not sufficient to handle this
1531 -- case, because the limited view may have been captured in another
1532 -- compilation unit that defines the current function.
1534 if Is_Incomplete_Type (Etype (N)) then
1535 if Present (Full_View (Etype (N))) then
1536 if Is_Entity_Name (Nam) then
1537 Set_Etype (Nam, Full_View (Etype (N)));
1538 Set_Etype (Entity (Nam), Full_View (Etype (N)));
1539 end if;
1541 Set_Etype (N, Full_View (Etype (N)));
1543 -- If the call is within a thunk, the nonlimited view should be
1544 -- analyzed eventually (see also Analyze_Return_Type).
1546 elsif From_Limited_With (Etype (N))
1547 and then Present (Non_Limited_View (Etype (N)))
1548 and then
1549 (Ekind (Non_Limited_View (Etype (N))) /= E_Incomplete_Type
1550 or else Is_Thunk (Current_Scope))
1551 then
1552 Set_Etype (N, Non_Limited_View (Etype (N)));
1554 -- If there is no completion for the type, this may be because
1555 -- there is only a limited view of it and there is nothing in
1556 -- the context of the current unit that has required a regular
1557 -- compilation of the unit containing the type. We recognize
1558 -- this unusual case by the fact that unit is not analyzed.
1559 -- Note that the call being analyzed is in a different unit from
1560 -- the function declaration, and nothing indicates that the type
1561 -- is a limited view.
1563 elsif Ekind (Scope (Etype (N))) = E_Package
1564 and then Present (Limited_View (Scope (Etype (N))))
1565 and then not Analyzed (Unit_Declaration_Node (Scope (Etype (N))))
1566 then
1567 Error_Msg_NE
1568 ("cannot call function that returns limited view of}",
1569 N, Etype (N));
1571 Error_Msg_NE
1572 ("\there must be a regular with_clause for package & in the "
1573 & "current unit, or in some unit in its context",
1574 N, Scope (Etype (N)));
1576 Set_Etype (N, Any_Type);
1577 end if;
1578 end if;
1579 end if;
1580 end Analyze_Call;
1582 -----------------------------
1583 -- Analyze_Case_Expression --
1584 -----------------------------
1586 procedure Analyze_Case_Expression (N : Node_Id) is
1587 Expr : constant Node_Id := Expression (N);
1588 First_Alt : constant Node_Id := First (Alternatives (N));
1590 First_Expr : Node_Id := Empty;
1591 -- First expression in the case where there is some type information
1592 -- available, i.e. there is not Any_Type everywhere, which can happen
1593 -- because of some error.
1595 Second_Expr : Node_Id := Empty;
1596 -- Second expression as above
1598 Wrong_Alt : Node_Id := Empty;
1599 -- For error reporting
1601 procedure Non_Static_Choice_Error (Choice : Node_Id);
1602 -- Error routine invoked by the generic instantiation below when
1603 -- the case expression has a non static choice.
1605 procedure Check_Next_Expression (T : Entity_Id; Alt : Node_Id);
1606 -- Check one interpretation of the next expression with type T
1608 procedure Check_Expression_Pair (T1, T2 : Entity_Id; Alt : Node_Id);
1609 -- Check first expression with type T1 and next expression with type T2
1611 package Case_Choices_Analysis is new
1612 Generic_Analyze_Choices
1613 (Process_Associated_Node => No_OP);
1614 use Case_Choices_Analysis;
1616 package Case_Choices_Checking is new
1617 Generic_Check_Choices
1618 (Process_Empty_Choice => No_OP,
1619 Process_Non_Static_Choice => Non_Static_Choice_Error,
1620 Process_Associated_Node => No_OP);
1621 use Case_Choices_Checking;
1623 -----------------------------
1624 -- Non_Static_Choice_Error --
1625 -----------------------------
1627 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1628 begin
1629 Flag_Non_Static_Expr
1630 ("choice given in case expression is not static!", Choice);
1631 end Non_Static_Choice_Error;
1633 ---------------------------
1634 -- Check_Next_Expression --
1635 ---------------------------
1637 procedure Check_Next_Expression (T : Entity_Id; Alt : Node_Id) is
1638 Next_Expr : constant Node_Id := Expression (Alt);
1640 I : Interp_Index;
1641 It : Interp;
1643 begin
1644 if Next_Expr = First_Expr then
1645 Check_Next_Expression (T, Next (Alt));
1646 return;
1647 end if;
1649 -- Loop through the interpretations of the next expression
1651 if not Is_Overloaded (Next_Expr) then
1652 Check_Expression_Pair (T, Etype (Next_Expr), Alt);
1654 else
1655 Get_First_Interp (Next_Expr, I, It);
1656 while Present (It.Typ) loop
1657 Check_Expression_Pair (T, It.Typ, Alt);
1658 Get_Next_Interp (I, It);
1659 end loop;
1660 end if;
1661 end Check_Next_Expression;
1663 ---------------------------
1664 -- Check_Expression_Pair --
1665 ---------------------------
1667 procedure Check_Expression_Pair (T1, T2 : Entity_Id; Alt : Node_Id) is
1668 Next_Expr : constant Node_Id := Expression (Alt);
1670 T : Entity_Id;
1672 begin
1673 if Covers (T1 => T1, T2 => T2)
1674 or else Covers (T1 => T2, T2 => T1)
1675 then
1676 T := Specific_Type (T1, T2);
1678 elsif Is_User_Defined_Literal (First_Expr, T2) then
1679 T := T2;
1681 elsif Is_User_Defined_Literal (Next_Expr, T1) then
1682 T := T1;
1684 else
1685 T := Possible_Type_For_Conditional_Expression (T1, T2);
1687 if No (T) then
1688 Wrong_Alt := Alt;
1689 return;
1690 end if;
1691 end if;
1693 if Present (Next (Alt)) then
1694 Check_Next_Expression (T, Next (Alt));
1695 else
1696 Add_One_Interp (N, T, T);
1697 end if;
1698 end Check_Expression_Pair;
1700 -- Local variables
1702 Alt : Node_Id;
1703 Exp_Type : Entity_Id;
1704 Exp_Btype : Entity_Id;
1705 I : Interp_Index;
1706 It : Interp;
1707 Others_Present : Boolean;
1709 -- Start of processing for Analyze_Case_Expression
1711 begin
1712 Analyze_And_Resolve (Expr, Any_Discrete);
1713 Check_Unset_Reference (Expr);
1714 Exp_Type := Etype (Expr);
1715 Exp_Btype := Base_Type (Exp_Type);
1717 Set_Etype (N, Any_Type);
1719 Alt := First_Alt;
1720 while Present (Alt) loop
1721 if Error_Posted (Expression (Alt)) then
1722 return;
1723 end if;
1725 Analyze_Expression (Expression (Alt));
1727 if Etype (Expression (Alt)) /= Any_Type then
1728 if No (First_Expr) then
1729 First_Expr := Expression (Alt);
1731 elsif No (Second_Expr) then
1732 Second_Expr := Expression (Alt);
1733 end if;
1734 end if;
1736 Next (Alt);
1737 end loop;
1739 -- Get our initial type from the first expression for which we got some
1740 -- useful type information from the expression.
1742 if No (First_Expr) then
1743 return;
1744 end if;
1746 -- The expression must be of a discrete type which must be determinable
1747 -- independently of the context in which the expression occurs, but
1748 -- using the fact that the expression must be of a discrete type.
1749 -- Moreover, the type this expression must not be a character literal
1750 -- (which is always ambiguous).
1752 -- If error already reported by Resolve, nothing more to do
1754 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1755 return;
1757 -- Special case message for character literal
1759 elsif Exp_Btype = Any_Character then
1760 Error_Msg_N
1761 ("character literal as case expression is ambiguous", Expr);
1762 return;
1763 end if;
1765 -- If the case expression is a formal object of mode in out, then
1766 -- treat it as having a nonstatic subtype by forcing use of the base
1767 -- type (which has to get passed to Check_Case_Choices below). Also
1768 -- use base type when the case expression is parenthesized.
1770 if Paren_Count (Expr) > 0
1771 or else (Is_Entity_Name (Expr)
1772 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1773 then
1774 Exp_Type := Exp_Btype;
1775 end if;
1777 -- The case expression alternatives cover the range of a static subtype
1778 -- subject to aspect Static_Predicate. Do not check the choices when the
1779 -- case expression has not been fully analyzed yet because this may lead
1780 -- to bogus errors.
1782 if Is_OK_Static_Subtype (Exp_Type)
1783 and then Has_Static_Predicate_Aspect (Exp_Type)
1784 and then In_Spec_Expression
1785 then
1786 null;
1788 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1790 else
1791 Analyze_Choices (Alternatives (N), Exp_Type);
1792 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1794 if Exp_Type = Universal_Integer and then not Others_Present then
1795 Error_Msg_N
1796 ("case on universal integer requires OTHERS choice", Expr);
1797 return;
1798 end if;
1799 end if;
1801 -- RM 4.5.7(10/3): If the case_expression is the operand of a type
1802 -- conversion, the type of the case_expression is the target type
1803 -- of the conversion.
1805 if Nkind (Parent (N)) = N_Type_Conversion then
1806 Set_Etype (N, Etype (Parent (N)));
1807 return;
1808 end if;
1810 -- Loop through the interpretations of the first expression and check
1811 -- the other expressions if present.
1813 if not Is_Overloaded (First_Expr) then
1814 if Present (Second_Expr) then
1815 Check_Next_Expression (Etype (First_Expr), First_Alt);
1816 else
1817 Set_Etype (N, Etype (First_Expr));
1818 end if;
1820 else
1821 Get_First_Interp (First_Expr, I, It);
1822 while Present (It.Typ) loop
1823 if Present (Second_Expr) then
1824 Check_Next_Expression (It.Typ, First_Alt);
1825 else
1826 Add_One_Interp (N, It.Typ, It.Typ);
1827 end if;
1829 Get_Next_Interp (I, It);
1830 end loop;
1831 end if;
1833 -- If no possible interpretation has been found, the type of the wrong
1834 -- alternative doesn't match any interpretation of the FIRST expression.
1836 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1837 Second_Expr := Expression (Wrong_Alt);
1839 if Is_Overloaded (First_Expr) then
1840 if Is_Overloaded (Second_Expr) then
1841 Error_Msg_N
1842 ("no interpretation compatible with those of previous "
1843 & "alternative",
1844 Second_Expr);
1845 else
1846 Error_Msg_N
1847 ("type incompatible with interpretations of previous "
1848 & "alternative",
1849 Second_Expr);
1850 Error_Msg_NE
1851 ("\this alternative has}!",
1852 Second_Expr,
1853 Etype (Second_Expr));
1854 end if;
1856 else
1857 if Is_Overloaded (Second_Expr) then
1858 Error_Msg_N
1859 ("no interpretation compatible with type of previous "
1860 & "alternative",
1861 Second_Expr);
1862 Error_Msg_NE
1863 ("\previous alternative has}!",
1864 Second_Expr,
1865 Etype (First_Expr));
1866 else
1867 Error_Msg_N
1868 ("type incompatible with that of previous alternative",
1869 Second_Expr);
1870 Error_Msg_NE
1871 ("\previous alternative has}!",
1872 Second_Expr,
1873 Etype (First_Expr));
1874 Error_Msg_NE
1875 ("\this alternative has}!",
1876 Second_Expr,
1877 Etype (Second_Expr));
1878 end if;
1879 end if;
1880 end if;
1881 end Analyze_Case_Expression;
1883 ---------------------------
1884 -- Analyze_Concatenation --
1885 ---------------------------
1887 procedure Analyze_Concatenation (N : Node_Id) is
1889 -- We wish to avoid deep recursion, because concatenations are often
1890 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1891 -- operands nonrecursively until we find something that is not a
1892 -- concatenation (A in this case), or has already been analyzed. We
1893 -- analyze that, and then walk back up the tree following Parent
1894 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1895 -- work at each level. The Parent pointers allow us to avoid recursion,
1896 -- and thus avoid running out of memory.
1898 NN : Node_Id := N;
1899 L : Node_Id;
1901 begin
1902 Candidate_Type := Empty;
1904 -- The following code is equivalent to:
1906 -- Set_Etype (N, Any_Type);
1907 -- Analyze_Expression (Left_Opnd (N));
1908 -- Analyze_Concatenation_Rest (N);
1910 -- where the Analyze_Expression call recurses back here if the left
1911 -- operand is a concatenation.
1913 -- Walk down left operands
1915 loop
1916 Set_Etype (NN, Any_Type);
1917 L := Left_Opnd (NN);
1918 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1919 NN := L;
1920 end loop;
1922 -- Now (given the above example) NN is A&B and L is A
1924 -- First analyze L ...
1926 Analyze_Expression (L);
1928 -- ... then walk NN back up until we reach N (where we started), calling
1929 -- Analyze_Concatenation_Rest along the way.
1931 loop
1932 Analyze_Concatenation_Rest (NN);
1933 exit when NN = N;
1934 NN := Parent (NN);
1935 end loop;
1936 end Analyze_Concatenation;
1938 --------------------------------
1939 -- Analyze_Concatenation_Rest --
1940 --------------------------------
1942 -- If the only one-dimensional array type in scope is String,
1943 -- this is the resulting type of the operation. Otherwise there
1944 -- will be a concatenation operation defined for each user-defined
1945 -- one-dimensional array.
1947 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1948 L : constant Node_Id := Left_Opnd (N);
1949 R : constant Node_Id := Right_Opnd (N);
1950 Op_Id : Entity_Id := Entity (N);
1951 LT : Entity_Id;
1952 RT : Entity_Id;
1954 begin
1955 Analyze_Expression (R);
1957 -- If the entity is present, the node appears in an instance, and
1958 -- denotes a predefined concatenation operation. The resulting type is
1959 -- obtained from the arguments when possible. If the arguments are
1960 -- aggregates, the array type and the concatenation type must be
1961 -- visible.
1963 if Present (Op_Id) then
1964 if Ekind (Op_Id) = E_Operator then
1965 LT := Base_Type (Etype (L));
1966 RT := Base_Type (Etype (R));
1968 if Is_Array_Type (LT)
1969 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1970 then
1971 Add_One_Interp (N, Op_Id, LT);
1973 elsif Is_Array_Type (RT)
1974 and then LT = Base_Type (Component_Type (RT))
1975 then
1976 Add_One_Interp (N, Op_Id, RT);
1978 -- If one operand is a string type or a user-defined array type,
1979 -- and the other is a literal, result is of the specific type.
1981 elsif
1982 (Root_Type (LT) = Standard_String
1983 or else Scope (LT) /= Standard_Standard)
1984 and then Etype (R) = Any_String
1985 then
1986 Add_One_Interp (N, Op_Id, LT);
1988 elsif
1989 (Root_Type (RT) = Standard_String
1990 or else Scope (RT) /= Standard_Standard)
1991 and then Etype (L) = Any_String
1992 then
1993 Add_One_Interp (N, Op_Id, RT);
1995 elsif not Is_Generic_Type (Etype (Op_Id)) then
1996 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1998 else
1999 -- Type and its operations must be visible
2001 Set_Entity (N, Empty);
2002 Analyze_Concatenation (N);
2003 end if;
2005 else
2006 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2007 end if;
2009 else
2010 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
2011 while Present (Op_Id) loop
2012 if Ekind (Op_Id) = E_Operator then
2014 -- Do not consider operators declared in dead code, they
2015 -- cannot be part of the resolution.
2017 if Is_Eliminated (Op_Id) then
2018 null;
2019 else
2020 Find_Concatenation_Types (L, R, Op_Id, N);
2021 end if;
2023 else
2024 Analyze_User_Defined_Binary_Op (N, Op_Id);
2025 end if;
2027 Op_Id := Homonym (Op_Id);
2028 end loop;
2029 end if;
2031 Operator_Check (N);
2032 end Analyze_Concatenation_Rest;
2034 ------------------------------------
2035 -- Analyze_Comparison_Equality_Op --
2036 ------------------------------------
2038 procedure Analyze_Comparison_Equality_Op (N : Node_Id) is
2039 Loc : constant Source_Ptr := Sloc (N);
2040 L : constant Node_Id := Left_Opnd (N);
2041 R : constant Node_Id := Right_Opnd (N);
2043 Op_Id : Entity_Id;
2045 begin
2046 Set_Etype (N, Any_Type);
2047 Candidate_Type := Empty;
2049 Analyze_Expression (L);
2050 Analyze_Expression (R);
2052 -- If the entity is set, the node is a generic instance with a non-local
2053 -- reference to the predefined operator or to a user-defined function.
2054 -- It can also be an inequality that is expanded into the negation of a
2055 -- call to a user-defined equality operator.
2057 -- For the predefined case, the result is Boolean, regardless of the
2058 -- type of the operands. The operands may even be limited, if they are
2059 -- generic actuals. If they are overloaded, label the operands with the
2060 -- common type that must be present, or with the type of the formal of
2061 -- the user-defined function.
2063 if Present (Entity (N)) then
2064 Op_Id := Entity (N);
2066 if Ekind (Op_Id) = E_Operator then
2067 Add_One_Interp (N, Op_Id, Standard_Boolean);
2068 else
2069 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2070 end if;
2072 if Is_Overloaded (L) then
2073 if Ekind (Op_Id) = E_Operator then
2074 Set_Etype (L, Intersect_Types (L, R));
2075 else
2076 Set_Etype (L, Etype (First_Formal (Op_Id)));
2077 end if;
2078 end if;
2080 if Is_Overloaded (R) then
2081 if Ekind (Op_Id) = E_Operator then
2082 Set_Etype (R, 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
2109 and then Nkind (N) = N_Op_Ne
2110 then
2111 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
2112 while Present (Op_Id) loop
2113 if Ekind (Op_Id) = E_Operator then
2114 Find_Comparison_Equality_Types (L, R, Op_Id, N);
2115 else
2116 Analyze_User_Defined_Binary_Op (N, Op_Id);
2117 end if;
2119 Op_Id := Homonym (Op_Id);
2120 end loop;
2122 if Etype (N) /= Any_Type then
2123 Op_Id := Entity (N);
2125 Rewrite (N,
2126 Make_Op_Not (Loc,
2127 Right_Opnd =>
2128 Make_Op_Eq (Loc,
2129 Left_Opnd => Left_Opnd (N),
2130 Right_Opnd => Right_Opnd (N))));
2132 Set_Entity (Right_Opnd (N), Op_Id);
2133 Analyze (N);
2134 end if;
2135 end if;
2137 Operator_Check (N);
2138 Check_Function_Writable_Actuals (N);
2139 end Analyze_Comparison_Equality_Op;
2141 ----------------------------------
2142 -- Analyze_Explicit_Dereference --
2143 ----------------------------------
2145 procedure Analyze_Explicit_Dereference (N : Node_Id) is
2146 Loc : constant Source_Ptr := Sloc (N);
2147 P : constant Node_Id := Prefix (N);
2148 T : Entity_Id;
2149 I : Interp_Index;
2150 It : Interp;
2151 New_N : Node_Id;
2153 function Is_Function_Type return Boolean;
2154 -- Check whether node may be interpreted as an implicit function call
2156 ----------------------
2157 -- Is_Function_Type --
2158 ----------------------
2160 function Is_Function_Type return Boolean is
2161 I : Interp_Index;
2162 It : Interp;
2164 begin
2165 if not Is_Overloaded (N) then
2166 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
2167 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
2169 else
2170 Get_First_Interp (N, I, It);
2171 while Present (It.Nam) loop
2172 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
2173 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
2174 then
2175 return False;
2176 end if;
2178 Get_Next_Interp (I, It);
2179 end loop;
2181 return True;
2182 end if;
2183 end Is_Function_Type;
2185 -- Start of processing for Analyze_Explicit_Dereference
2187 begin
2188 -- In formal verification mode, keep track of all reads and writes
2189 -- through explicit dereferences.
2191 if GNATprove_Mode then
2192 SPARK_Specific.Generate_Dereference (N);
2193 end if;
2195 Analyze (P);
2196 Set_Etype (N, Any_Type);
2198 -- Test for remote access to subprogram type, and if so return
2199 -- after rewriting the original tree.
2201 if Remote_AST_E_Dereference (P) then
2202 return;
2203 end if;
2205 -- Normal processing for other than remote access to subprogram type
2207 if not Is_Overloaded (P) then
2208 if Is_Access_Type (Etype (P)) then
2210 -- Set the Etype
2212 declare
2213 DT : constant Entity_Id := Designated_Type (Etype (P));
2215 begin
2216 -- An explicit dereference is a legal occurrence of an
2217 -- incomplete type imported through a limited_with clause, if
2218 -- the full view is visible, or if we are within an instance
2219 -- body, where the enclosing body has a regular with_clause
2220 -- on the unit.
2222 if From_Limited_With (DT)
2223 and then not From_Limited_With (Scope (DT))
2224 and then
2225 (Is_Immediately_Visible (Scope (DT))
2226 or else
2227 (Is_Child_Unit (Scope (DT))
2228 and then Is_Visible_Lib_Unit (Scope (DT)))
2229 or else In_Instance_Body)
2230 then
2231 Set_Etype (N, Available_View (DT));
2233 else
2234 Set_Etype (N, DT);
2235 end if;
2236 end;
2238 elsif Etype (P) /= Any_Type then
2239 Error_Msg_N ("prefix of dereference must be an access type", N);
2240 return;
2241 end if;
2243 else
2244 Get_First_Interp (P, I, It);
2245 while Present (It.Nam) loop
2246 T := It.Typ;
2248 if Is_Access_Type (T) then
2249 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2250 end if;
2252 Get_Next_Interp (I, It);
2253 end loop;
2255 -- Error if no interpretation of the prefix has an access type
2257 if Etype (N) = Any_Type then
2258 Error_Msg_N
2259 ("access type required in prefix of explicit dereference", P);
2260 Set_Etype (N, Any_Type);
2261 return;
2262 end if;
2263 end if;
2265 if Is_Function_Type
2266 and then Nkind (Parent (N)) /= N_Indexed_Component
2268 and then (Nkind (Parent (N)) /= N_Function_Call
2269 or else N /= Name (Parent (N)))
2271 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2272 or else N /= Name (Parent (N)))
2274 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2275 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2276 or else
2277 (Attribute_Name (Parent (N)) /= Name_Address
2278 and then
2279 Attribute_Name (Parent (N)) /= Name_Access))
2280 then
2281 -- Name is a function call with no actuals, in a context that
2282 -- requires deproceduring (including as an actual in an enclosing
2283 -- function or procedure call). There are some pathological cases
2284 -- where the prefix might include functions that return access to
2285 -- subprograms and others that return a regular type. Disambiguation
2286 -- of those has to take place in Resolve.
2288 New_N :=
2289 Make_Function_Call (Loc,
2290 Name => Make_Explicit_Dereference (Loc, P),
2291 Parameter_Associations => New_List);
2293 -- If the prefix is overloaded, remove operations that have formals,
2294 -- we know that this is a parameterless call.
2296 if Is_Overloaded (P) then
2297 Get_First_Interp (P, I, It);
2298 while Present (It.Nam) loop
2299 T := It.Typ;
2301 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2302 Set_Etype (P, T);
2303 else
2304 Remove_Interp (I);
2305 end if;
2307 Get_Next_Interp (I, It);
2308 end loop;
2309 end if;
2311 Rewrite (N, New_N);
2312 Analyze (N);
2314 elsif not Is_Function_Type
2315 and then Is_Overloaded (N)
2316 then
2317 -- The prefix may include access to subprograms and other access
2318 -- types. If the context selects the interpretation that is a
2319 -- function call (not a procedure call) we cannot rewrite the node
2320 -- yet, but we include the result of the call interpretation.
2322 Get_First_Interp (N, I, It);
2323 while Present (It.Nam) loop
2324 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2325 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2326 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2327 then
2328 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2329 end if;
2331 Get_Next_Interp (I, It);
2332 end loop;
2333 end if;
2335 -- A value of remote access-to-class-wide must not be dereferenced
2336 -- (RM E.2.2(16)).
2338 Validate_Remote_Access_To_Class_Wide_Type (N);
2339 end Analyze_Explicit_Dereference;
2341 ------------------------
2342 -- Analyze_Expression --
2343 ------------------------
2345 procedure Analyze_Expression (N : Node_Id) is
2346 begin
2347 -- If the expression is an indexed component that will be rewritten
2348 -- as a container indexing, it has already been analyzed.
2350 if Nkind (N) = N_Indexed_Component
2351 and then Present (Generalized_Indexing (N))
2352 then
2353 null;
2355 else
2356 Analyze (N);
2357 Check_Parameterless_Call (N);
2358 end if;
2359 end Analyze_Expression;
2361 -------------------------------------
2362 -- Analyze_Expression_With_Actions --
2363 -------------------------------------
2365 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2367 procedure Check_Action_OK (A : Node_Id);
2368 -- Check that the action A is allowed as a declare_item of a declare
2369 -- expression if N and A come from source.
2371 ---------------------
2372 -- Check_Action_OK --
2373 ---------------------
2375 procedure Check_Action_OK (A : Node_Id) is
2376 begin
2377 if not Comes_From_Source (N) or else not Comes_From_Source (A) then
2379 -- If, for example, an (illegal) expression function is
2380 -- transformed into a "vanilla" function then we don't want to
2381 -- allow it just because Comes_From_Source is now False. So look
2382 -- at the Original_Node.
2384 if Is_Rewrite_Substitution (A) then
2385 Check_Action_OK (Original_Node (A));
2386 end if;
2388 return; -- Allow anything in generated code
2389 end if;
2391 case Nkind (A) is
2392 when N_Object_Declaration =>
2393 if Nkind (Object_Definition (A)) = N_Access_Definition then
2394 Error_Msg_N
2395 ("anonymous access type not allowed in declare_expression",
2396 Object_Definition (A));
2397 end if;
2399 if Aliased_Present (A) then
2400 Error_Msg_N ("ALIASED not allowed in declare_expression", A);
2401 end if;
2403 if Constant_Present (A)
2404 and then not Is_Limited_Type (Etype (Defining_Identifier (A)))
2405 then
2406 return; -- nonlimited constants are OK
2407 end if;
2409 when N_Object_Renaming_Declaration =>
2410 if Present (Access_Definition (A)) then
2411 Error_Msg_N
2412 ("anonymous access type not allowed in declare_expression",
2413 Access_Definition (A));
2414 end if;
2416 if not Is_Limited_Type (Etype (Defining_Identifier (A))) then
2417 return; -- ???For now; the RM rule is a bit more complicated
2418 end if;
2420 when N_Pragma =>
2421 declare
2422 -- See AI22-0045 pragma categorization.
2423 subtype Executable_Pragma_Id is Pragma_Id
2424 with Predicate => Executable_Pragma_Id in
2425 -- language-defined executable pragmas
2426 Pragma_Assert | Pragma_Inspection_Point
2428 -- GNAT-defined executable pragmas
2429 | Pragma_Assume | Pragma_Debug;
2430 begin
2431 if Get_Pragma_Id (A) in Executable_Pragma_Id then
2432 return;
2433 end if;
2434 end;
2436 when others =>
2437 null; -- Nothing else allowed
2438 end case;
2440 -- We could mention pragmas in the message text; let's not.
2441 Error_Msg_N ("object renaming or constant declaration expected", A);
2442 end Check_Action_OK;
2444 A : Node_Id;
2445 EWA_Scop : Entity_Id;
2447 -- Start of processing for Analyze_Expression_With_Actions
2449 begin
2450 -- Create a scope, which is needed to provide proper visibility of the
2451 -- declare_items.
2453 EWA_Scop := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
2454 Set_Etype (EWA_Scop, Standard_Void_Type);
2455 Set_Scope (EWA_Scop, Current_Scope);
2456 Set_Parent (EWA_Scop, N);
2457 Push_Scope (EWA_Scop);
2459 -- If this Expression_With_Actions node comes from source, then it
2460 -- represents a declare_expression; increment the counter to take note
2461 -- of that.
2463 if Comes_From_Source (N) then
2464 In_Declare_Expr := In_Declare_Expr + 1;
2465 end if;
2467 A := First (Actions (N));
2468 while Present (A) loop
2469 Analyze (A);
2470 Check_Action_OK (A);
2471 Next (A);
2472 end loop;
2474 Analyze_Expression (Expression (N));
2475 Set_Etype (N, Etype (Expression (N)));
2476 End_Scope;
2478 if Comes_From_Source (N) then
2479 In_Declare_Expr := In_Declare_Expr - 1;
2480 end if;
2481 end Analyze_Expression_With_Actions;
2483 ---------------------------
2484 -- Analyze_If_Expression --
2485 ---------------------------
2487 procedure Analyze_If_Expression (N : Node_Id) is
2488 Condition : constant Node_Id := First (Expressions (N));
2490 Then_Expr : Node_Id;
2491 Else_Expr : Node_Id;
2493 procedure Check_Else_Expression (T : Entity_Id);
2494 -- Check one interpretation of the THEN expression with type T
2496 procedure Check_Expression_Pair (T1, T2 : Entity_Id);
2497 -- Check THEN expression with type T1 and ELSE expression with type T2
2499 ---------------------------
2500 -- Check_Else_Expression --
2501 ---------------------------
2503 procedure Check_Else_Expression (T : Entity_Id) is
2504 I : Interp_Index;
2505 It : Interp;
2507 begin
2508 -- Loop through the interpretations of the ELSE expression
2510 if not Is_Overloaded (Else_Expr) then
2511 Check_Expression_Pair (T, Etype (Else_Expr));
2513 else
2514 Get_First_Interp (Else_Expr, I, It);
2515 while Present (It.Typ) loop
2516 Check_Expression_Pair (T, It.Typ);
2517 Get_Next_Interp (I, It);
2518 end loop;
2519 end if;
2520 end Check_Else_Expression;
2522 ---------------------------
2523 -- Check_Expression_Pair --
2524 ---------------------------
2526 procedure Check_Expression_Pair (T1, T2 : Entity_Id) is
2527 T : Entity_Id;
2529 begin
2530 if Covers (T1 => T1, T2 => T2)
2531 or else Covers (T1 => T2, T2 => T1)
2532 then
2533 T := Specific_Type (T1, T2);
2535 elsif Is_User_Defined_Literal (Then_Expr, T2) then
2536 T := T2;
2538 elsif Is_User_Defined_Literal (Else_Expr, T1) then
2539 T := T1;
2541 else
2542 T := Possible_Type_For_Conditional_Expression (T1, T2);
2544 if No (T) then
2545 return;
2546 end if;
2547 end if;
2549 Add_One_Interp (N, T, T);
2550 end Check_Expression_Pair;
2552 -- Local variables
2554 I : Interp_Index;
2555 It : Interp;
2557 -- Start of processing for Analyze_If_Expression
2559 begin
2560 -- Defend against error of missing expressions from previous error
2562 if No (Condition) then
2563 Check_Error_Detected;
2564 return;
2565 end if;
2567 Set_Etype (N, Any_Type);
2569 Then_Expr := Next (Condition);
2571 if No (Then_Expr) then
2572 Check_Error_Detected;
2573 return;
2574 end if;
2576 Else_Expr := Next (Then_Expr);
2578 -- Analyze and resolve the condition. We need to resolve this now so
2579 -- that it gets folded to True/False if possible, before we analyze
2580 -- the THEN/ELSE branches, because when analyzing these branches, we
2581 -- may call Is_Statically_Unevaluated, which expects the condition of
2582 -- an enclosing IF to have been analyze/resolved/evaluated.
2584 Analyze_Expression (Condition);
2585 Resolve (Condition, Any_Boolean);
2587 -- Analyze the THEN expression and (if present) the ELSE expression. For
2588 -- them we delay resolution in the normal manner because of overloading.
2590 Analyze_Expression (Then_Expr);
2592 if Present (Else_Expr) then
2593 Analyze_Expression (Else_Expr);
2594 end if;
2596 -- RM 4.5.7(10/3): If the if_expression is the operand of a type
2597 -- conversion, the type of the if_expression is the target type
2598 -- of the conversion.
2600 if Nkind (Parent (N)) = N_Type_Conversion then
2601 Set_Etype (N, Etype (Parent (N)));
2602 return;
2603 end if;
2605 -- Loop through the interpretations of the THEN expression and check the
2606 -- ELSE expression if present.
2608 if not Is_Overloaded (Then_Expr) then
2609 if Present (Else_Expr) then
2610 Check_Else_Expression (Etype (Then_Expr));
2611 else
2612 Set_Etype (N, Etype (Then_Expr));
2613 end if;
2615 else
2616 Get_First_Interp (Then_Expr, I, It);
2617 while Present (It.Typ) loop
2618 if Present (Else_Expr) then
2619 Check_Else_Expression (It.Typ);
2620 else
2621 Add_One_Interp (N, It.Typ, It.Typ);
2622 end if;
2624 Get_Next_Interp (I, It);
2625 end loop;
2626 end if;
2628 -- If no possible interpretation has been found, the type of the
2629 -- ELSE expression does not match any interpretation of the THEN
2630 -- expression.
2632 if Etype (N) = Any_Type then
2633 if Is_Overloaded (Then_Expr) then
2634 if Is_Overloaded (Else_Expr) then
2635 Error_Msg_N
2636 ("no interpretation compatible with those of THEN expression",
2637 Else_Expr);
2638 else
2639 Error_Msg_N
2640 ("type of ELSE incompatible with interpretations of THEN "
2641 & "expression",
2642 Else_Expr);
2643 Error_Msg_NE
2644 ("\ELSE expression has}!", Else_Expr, Etype (Else_Expr));
2645 end if;
2647 else
2648 if Is_Overloaded (Else_Expr) then
2649 Error_Msg_N
2650 ("no interpretation compatible with type of THEN expression",
2651 Else_Expr);
2652 Error_Msg_NE
2653 ("\THEN expression has}!", Else_Expr, Etype (Then_Expr));
2654 else
2655 Error_Msg_N
2656 ("type of ELSE incompatible with that of THEN expression",
2657 Else_Expr);
2658 Error_Msg_NE
2659 ("\THEN expression has}!", Else_Expr, Etype (Then_Expr));
2660 Error_Msg_NE
2661 ("\ELSE expression has}!", Else_Expr, Etype (Else_Expr));
2662 end if;
2663 end if;
2664 end if;
2665 end Analyze_If_Expression;
2667 ------------------------------------
2668 -- Analyze_Indexed_Component_Form --
2669 ------------------------------------
2671 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2672 P : constant Node_Id := Prefix (N);
2673 Exprs : constant List_Id := Expressions (N);
2674 Exp : Node_Id;
2675 P_T : Entity_Id;
2676 E : Node_Id;
2677 U_N : Entity_Id;
2679 procedure Process_Function_Call;
2680 -- Prefix in indexed component form is an overloadable entity, so the
2681 -- node is very likely a function call; reformat it as such. The only
2682 -- exception is a call to a parameterless function that returns an
2683 -- array type, or an access type thereof, in which case this will be
2684 -- undone later by Resolve_Call or Resolve_Entry_Call.
2686 procedure Process_Indexed_Component;
2687 -- Prefix in indexed component form is actually an indexed component.
2688 -- This routine processes it, knowing that the prefix is already
2689 -- resolved.
2691 procedure Process_Indexed_Component_Or_Slice;
2692 -- An indexed component with a single index may designate a slice if
2693 -- the index is a subtype mark. This routine disambiguates these two
2694 -- cases by resolving the prefix to see if it is a subtype mark.
2696 procedure Process_Overloaded_Indexed_Component;
2697 -- If the prefix of an indexed component is overloaded, the proper
2698 -- interpretation is selected by the index types and the context.
2700 ---------------------------
2701 -- Process_Function_Call --
2702 ---------------------------
2704 procedure Process_Function_Call is
2705 Loc : constant Source_Ptr := Sloc (N);
2706 Actual : Node_Id;
2708 begin
2709 Change_Node (N, N_Function_Call);
2710 Set_Name (N, P);
2711 Set_Parameter_Associations (N, Exprs);
2713 -- Analyze actuals prior to analyzing the call itself
2715 Actual := First (Parameter_Associations (N));
2716 while Present (Actual) loop
2717 Analyze (Actual);
2718 Check_Parameterless_Call (Actual);
2720 -- Move to next actual. Note that we use Next, not Next_Actual
2721 -- here. The reason for this is a bit subtle. If a function call
2722 -- includes named associations, the parser recognizes the node
2723 -- as a call, and it is analyzed as such. If all associations are
2724 -- positional, the parser builds an indexed_component node, and
2725 -- it is only after analysis of the prefix that the construct
2726 -- is recognized as a call, in which case Process_Function_Call
2727 -- rewrites the node and analyzes the actuals. If the list of
2728 -- actuals is malformed, the parser may leave the node as an
2729 -- indexed component (despite the presence of named associations).
2730 -- The iterator Next_Actual is equivalent to Next if the list is
2731 -- positional, but follows the normalized chain of actuals when
2732 -- named associations are present. In this case normalization has
2733 -- not taken place, and actuals remain unanalyzed, which leads to
2734 -- subsequent crashes or loops if there is an attempt to continue
2735 -- analysis of the program.
2737 -- IF there is a single actual and it is a type name, the node
2738 -- can only be interpreted as a slice of a parameterless call.
2739 -- Rebuild the node as such and analyze.
2741 if No (Next (Actual))
2742 and then Is_Entity_Name (Actual)
2743 and then Is_Type (Entity (Actual))
2744 and then Is_Discrete_Type (Entity (Actual))
2745 and then not Is_Current_Instance (Actual)
2746 then
2747 Replace (N,
2748 Make_Slice (Loc,
2749 Prefix => P,
2750 Discrete_Range =>
2751 New_Occurrence_Of (Entity (Actual), Loc)));
2752 Analyze (N);
2753 return;
2755 else
2756 Next (Actual);
2757 end if;
2758 end loop;
2760 Analyze_Call (N);
2761 end Process_Function_Call;
2763 -------------------------------
2764 -- Process_Indexed_Component --
2765 -------------------------------
2767 procedure Process_Indexed_Component is
2768 Exp : Node_Id;
2769 Array_Type : Entity_Id;
2770 Index : Node_Id;
2771 Pent : Entity_Id := Empty;
2773 begin
2774 Exp := First (Exprs);
2776 if Is_Overloaded (P) then
2777 Process_Overloaded_Indexed_Component;
2779 else
2780 Array_Type := Etype (P);
2782 if Is_Entity_Name (P) then
2783 Pent := Entity (P);
2784 elsif Nkind (P) = N_Selected_Component
2785 and then Is_Entity_Name (Selector_Name (P))
2786 then
2787 Pent := Entity (Selector_Name (P));
2788 end if;
2790 -- Prefix must be appropriate for an array type, taking into
2791 -- account a possible implicit dereference.
2793 if Is_Access_Type (Array_Type) then
2794 Error_Msg_NW
2795 (Warn_On_Dereference, "?d?implicit dereference", N);
2796 Array_Type := Implicitly_Designated_Type (Array_Type);
2797 end if;
2799 if Is_Array_Type (Array_Type) then
2801 -- In order to correctly access First_Index component later,
2802 -- replace string literal subtype by its parent type.
2804 if Ekind (Array_Type) = E_String_Literal_Subtype then
2805 Array_Type := Etype (Array_Type);
2806 end if;
2808 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2809 Analyze (Exp);
2810 Set_Etype (N, Any_Type);
2812 if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then
2813 Error_Msg_N ("invalid index type in entry name", N);
2815 elsif Present (Next (Exp)) then
2816 Error_Msg_N ("too many subscripts in entry reference", N);
2818 else
2819 Set_Etype (N, Etype (P));
2820 end if;
2822 return;
2824 elsif Is_Record_Type (Array_Type)
2825 and then Remote_AST_I_Dereference (P)
2826 then
2827 return;
2829 elsif Try_Container_Indexing (N, P, Exprs) then
2830 return;
2832 elsif Array_Type = Any_Type then
2833 Set_Etype (N, Any_Type);
2835 -- In most cases the analysis of the prefix will have emitted
2836 -- an error already, but if the prefix may be interpreted as a
2837 -- call in prefixed notation, the report is left to the caller.
2838 -- To prevent cascaded errors, report only if no previous ones.
2840 if Serious_Errors_Detected = 0 then
2841 Error_Msg_N ("invalid prefix in indexed component", P);
2843 if Nkind (P) = N_Expanded_Name then
2844 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2845 end if;
2846 end if;
2848 return;
2850 -- Here we definitely have a bad indexing
2852 else
2853 if Nkind (Parent (N)) = N_Requeue_Statement
2854 and then Present (Pent) and then Ekind (Pent) = E_Entry
2855 then
2856 Error_Msg_N
2857 ("REQUEUE does not permit parameters", First (Exprs));
2859 elsif Is_Entity_Name (P)
2860 and then Etype (P) = Standard_Void_Type
2861 then
2862 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2864 else
2865 Error_Msg_N ("array type required in indexed component", P);
2866 end if;
2868 Set_Etype (N, Any_Type);
2869 return;
2870 end if;
2872 Index := First_Index (Array_Type);
2873 while Present (Index) and then Present (Exp) loop
2874 if not Has_Compatible_Type (Exp, Etype (Index)) then
2875 Wrong_Type (Exp, Etype (Index));
2876 Set_Etype (N, Any_Type);
2877 return;
2878 end if;
2880 Next_Index (Index);
2881 Next (Exp);
2882 end loop;
2884 Set_Etype (N, Component_Type (Array_Type));
2885 Check_Implicit_Dereference (N, Etype (N));
2887 if Present (Index) then
2888 Error_Msg_N
2889 ("too few subscripts in array reference", First (Exprs));
2891 elsif Present (Exp) then
2892 Error_Msg_N ("too many subscripts in array reference", Exp);
2893 end if;
2894 end if;
2895 end Process_Indexed_Component;
2897 ----------------------------------------
2898 -- Process_Indexed_Component_Or_Slice --
2899 ----------------------------------------
2901 procedure Process_Indexed_Component_Or_Slice is
2902 begin
2903 Exp := First (Exprs);
2904 while Present (Exp) loop
2905 Analyze_Expression (Exp);
2906 Next (Exp);
2907 end loop;
2909 Exp := First (Exprs);
2911 -- If one index is present, and it is a subtype name, then the node
2912 -- denotes a slice (note that the case of an explicit range for a
2913 -- slice was already built as an N_Slice node in the first place,
2914 -- so that case is not handled here).
2916 -- We use a replace rather than a rewrite here because this is one
2917 -- of the cases in which the tree built by the parser is plain wrong.
2919 if No (Next (Exp))
2920 and then Is_Entity_Name (Exp)
2921 and then Is_Type (Entity (Exp))
2922 then
2923 Replace (N,
2924 Make_Slice (Sloc (N),
2925 Prefix => P,
2926 Discrete_Range => New_Copy (Exp)));
2927 Analyze (N);
2929 -- Otherwise (more than one index present, or single index is not
2930 -- a subtype name), then we have the indexed component case.
2932 else
2933 Process_Indexed_Component;
2934 end if;
2935 end Process_Indexed_Component_Or_Slice;
2937 ------------------------------------------
2938 -- Process_Overloaded_Indexed_Component --
2939 ------------------------------------------
2941 procedure Process_Overloaded_Indexed_Component is
2942 Exp : Node_Id;
2943 I : Interp_Index;
2944 It : Interp;
2945 Typ : Entity_Id;
2946 Index : Node_Id;
2947 Found : Boolean;
2949 begin
2950 Set_Etype (N, Any_Type);
2952 Get_First_Interp (P, I, It);
2953 while Present (It.Nam) loop
2954 Typ := It.Typ;
2956 if Is_Access_Type (Typ) then
2957 Typ := Designated_Type (Typ);
2958 Error_Msg_NW
2959 (Warn_On_Dereference, "?d?implicit dereference", N);
2960 end if;
2962 if Is_Array_Type (Typ) then
2964 -- Got a candidate: verify that index types are compatible
2966 Index := First_Index (Typ);
2967 Found := True;
2968 Exp := First (Exprs);
2969 while Present (Index) and then Present (Exp) loop
2970 if Has_Compatible_Type (Exp, Etype (Index)) then
2971 null;
2972 else
2973 Found := False;
2974 Remove_Interp (I);
2975 exit;
2976 end if;
2978 Next_Index (Index);
2979 Next (Exp);
2980 end loop;
2982 if Found and then No (Index) and then No (Exp) then
2983 declare
2984 CT : constant Entity_Id :=
2985 Base_Type (Component_Type (Typ));
2986 begin
2987 Add_One_Interp (N, CT, CT);
2988 Check_Implicit_Dereference (N, CT);
2989 end;
2990 end if;
2992 elsif Try_Container_Indexing (N, P, Exprs) then
2993 return;
2995 end if;
2997 Get_Next_Interp (I, It);
2998 end loop;
3000 if Etype (N) = Any_Type then
3001 Error_Msg_N ("no legal interpretation for indexed component", N);
3002 Set_Is_Overloaded (N, False);
3003 end if;
3004 end Process_Overloaded_Indexed_Component;
3006 -- Start of processing for Analyze_Indexed_Component_Form
3008 begin
3009 -- Get name of array, function or type
3011 Analyze (P);
3013 -- If P is an explicit dereference whose prefix is of a remote access-
3014 -- to-subprogram type, then N has already been rewritten as a subprogram
3015 -- call and analyzed.
3017 if Nkind (N) in N_Subprogram_Call then
3018 return;
3020 -- When the prefix is attribute 'Loop_Entry and the sole expression of
3021 -- the indexed component denotes a loop name, the indexed form is turned
3022 -- into an attribute reference.
3024 elsif Nkind (N) = N_Attribute_Reference
3025 and then Attribute_Name (N) = Name_Loop_Entry
3026 then
3027 return;
3028 end if;
3030 pragma Assert (Nkind (N) = N_Indexed_Component);
3032 P_T := Base_Type (Etype (P));
3034 if Is_Entity_Name (P) and then Present (Entity (P)) then
3035 U_N := Entity (P);
3037 if Is_Type (U_N) then
3039 -- Reformat node as a type conversion
3041 E := Remove_Head (Exprs);
3043 if Present (First (Exprs)) then
3044 Error_Msg_N
3045 ("argument of type conversion must be single expression", N);
3046 end if;
3048 Change_Node (N, N_Type_Conversion);
3049 Set_Subtype_Mark (N, P);
3050 Set_Etype (N, U_N);
3051 Set_Expression (N, E);
3053 -- After changing the node, call for the specific Analysis
3054 -- routine directly, to avoid a double call to the expander.
3056 Analyze_Type_Conversion (N);
3057 return;
3058 end if;
3060 if Is_Overloadable (U_N) then
3061 Process_Function_Call;
3063 elsif Ekind (Etype (P)) = E_Subprogram_Type
3064 or else (Is_Access_Type (Etype (P))
3065 and then
3066 Ekind (Designated_Type (Etype (P))) =
3067 E_Subprogram_Type)
3068 then
3069 -- Call to access_to-subprogram with possible implicit dereference
3071 Process_Function_Call;
3073 elsif Is_Generic_Subprogram (U_N) then
3075 -- A common beginner's (or C++ templates fan) error
3077 Error_Msg_N ("generic subprogram cannot be called", N);
3078 Set_Etype (N, Any_Type);
3079 return;
3081 else
3082 Process_Indexed_Component_Or_Slice;
3083 end if;
3085 -- If not an entity name, prefix is an expression that may denote
3086 -- an array or an access-to-subprogram.
3088 else
3089 if Ekind (P_T) = E_Subprogram_Type
3090 or else (Is_Access_Type (P_T)
3091 and then
3092 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
3093 then
3094 Process_Function_Call;
3096 elsif Nkind (P) = N_Selected_Component
3097 and then Present (Entity (Selector_Name (P)))
3098 and then Is_Overloadable (Entity (Selector_Name (P)))
3099 then
3100 Process_Function_Call;
3101 else
3102 -- Indexed component, slice, or a call to a member of a family
3103 -- entry, which will be converted to an entry call later.
3105 Process_Indexed_Component_Or_Slice;
3106 end if;
3107 end if;
3109 Analyze_Dimension (N);
3110 end Analyze_Indexed_Component_Form;
3112 ------------------------
3113 -- Analyze_Logical_Op --
3114 ------------------------
3116 procedure Analyze_Logical_Op (N : Node_Id) is
3117 L : constant Node_Id := Left_Opnd (N);
3118 R : constant Node_Id := Right_Opnd (N);
3120 Op_Id : Entity_Id;
3122 begin
3123 Set_Etype (N, Any_Type);
3124 Candidate_Type := Empty;
3126 Analyze_Expression (L);
3127 Analyze_Expression (R);
3129 -- If the entity is already set, the node is the instantiation of a
3130 -- generic node with a non-local reference, or was manufactured by a
3131 -- call to Make_Op_xxx. In either case the entity is known to be valid,
3132 -- and we do not need to collect interpretations, instead we just get
3133 -- the single possible interpretation.
3135 if Present (Entity (N)) then
3136 Op_Id := Entity (N);
3138 if Ekind (Op_Id) = E_Operator then
3139 Find_Boolean_Types (L, R, Op_Id, N);
3140 else
3141 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3142 end if;
3144 -- Entity is not already set, so we do need to collect interpretations
3146 else
3147 Op_Id := Get_Name_Entity_Id (Chars (N));
3148 while Present (Op_Id) loop
3149 if Ekind (Op_Id) = E_Operator then
3150 Find_Boolean_Types (L, R, Op_Id, N);
3151 else
3152 Analyze_User_Defined_Binary_Op (N, Op_Id);
3153 end if;
3155 Op_Id := Homonym (Op_Id);
3156 end loop;
3157 end if;
3159 Operator_Check (N);
3160 Check_Function_Writable_Actuals (N);
3162 if Style_Check then
3163 if Nkind (L) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
3164 and then Is_Boolean_Type (Etype (L))
3165 then
3166 Check_Xtra_Parens_Precedence (L);
3167 end if;
3169 if Nkind (R) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
3170 and then Is_Boolean_Type (Etype (R))
3171 then
3172 Check_Xtra_Parens_Precedence (R);
3173 end if;
3174 end if;
3175 end Analyze_Logical_Op;
3177 ---------------------------
3178 -- Analyze_Membership_Op --
3179 ---------------------------
3181 procedure Analyze_Membership_Op (N : Node_Id) is
3182 Loc : constant Source_Ptr := Sloc (N);
3183 L : constant Node_Id := Left_Opnd (N);
3184 R : constant Node_Id := Right_Opnd (N);
3186 procedure Analyze_Set_Membership;
3187 -- If a set of alternatives is present, analyze each and find the
3188 -- common type to which they must all resolve.
3190 function Find_Interp return Boolean;
3191 -- Find a valid interpretation of the test. Note that the context of the
3192 -- operation plays no role in resolving the operands, so that if there
3193 -- is more than one interpretation of the operands that is compatible
3194 -- with the test, the operation is ambiguous.
3196 function Try_Left_Interp (T : Entity_Id) return Boolean;
3197 -- Try an interpretation of the left operand with type T. Return true if
3198 -- one interpretation (at least) of the right operand making up a valid
3199 -- operand pair exists, otherwise false if no such pair exists.
3201 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean;
3202 -- Return true if T1 and T2 constitute a valid pair of operand types for
3203 -- L and R respectively.
3205 ----------------------------
3206 -- Analyze_Set_Membership --
3207 ----------------------------
3209 procedure Analyze_Set_Membership is
3210 Alt : Node_Id;
3211 Index : Interp_Index;
3212 It : Interp;
3213 Candidate_Interps : Node_Id;
3214 Common_Type : Entity_Id := Empty;
3216 begin
3217 Analyze (L);
3218 Candidate_Interps := L;
3220 if not Is_Overloaded (L) then
3221 Common_Type := Etype (L);
3223 Alt := First (Alternatives (N));
3224 while Present (Alt) loop
3225 Analyze (Alt);
3227 if not Has_Compatible_Type (Alt, Common_Type) then
3228 Wrong_Type (Alt, Common_Type);
3229 end if;
3231 Next (Alt);
3232 end loop;
3234 else
3235 Alt := First (Alternatives (N));
3236 while Present (Alt) loop
3237 Analyze (Alt);
3238 if not Is_Overloaded (Alt) then
3239 Common_Type := Etype (Alt);
3241 else
3242 Get_First_Interp (Alt, Index, It);
3243 while Present (It.Typ) loop
3244 if not
3245 Has_Compatible_Type (Candidate_Interps, It.Typ)
3246 then
3247 Remove_Interp (Index);
3248 end if;
3250 Get_Next_Interp (Index, It);
3251 end loop;
3253 Get_First_Interp (Alt, Index, It);
3255 if No (It.Typ) then
3256 Error_Msg_N ("alternative has no legal type", Alt);
3257 return;
3258 end if;
3260 -- If alternative is not overloaded, we have a unique type
3261 -- for all of them.
3263 Set_Etype (Alt, It.Typ);
3265 -- If the alternative is an enumeration literal, use the one
3266 -- for this interpretation.
3268 if Is_Entity_Name (Alt) then
3269 Set_Entity (Alt, It.Nam);
3270 end if;
3272 Get_Next_Interp (Index, It);
3274 if No (It.Typ) then
3275 Set_Is_Overloaded (Alt, False);
3276 Common_Type := Etype (Alt);
3277 end if;
3279 Candidate_Interps := Alt;
3280 end if;
3282 Next (Alt);
3283 end loop;
3284 end if;
3286 if Present (Common_Type) then
3287 Set_Etype (L, Common_Type);
3289 -- The left operand may still be overloaded, to be resolved using
3290 -- the Common_Type.
3292 else
3293 Error_Msg_N ("cannot resolve membership operation", N);
3294 end if;
3295 end Analyze_Set_Membership;
3297 -----------------
3298 -- Find_Interp --
3299 -----------------
3301 function Find_Interp return Boolean is
3302 Found : Boolean;
3303 I : Interp_Index;
3304 It : Interp;
3305 L_Typ : Entity_Id;
3306 Valid_I : Interp_Index;
3308 begin
3309 -- Loop through the interpretations of the left operand
3311 if not Is_Overloaded (L) then
3312 Found := Try_Left_Interp (Etype (L));
3314 else
3315 Found := False;
3316 L_Typ := Empty;
3317 Valid_I := 0;
3319 Get_First_Interp (L, I, It);
3320 while Present (It.Typ) loop
3321 if Try_Left_Interp (It.Typ) then
3322 -- If several interpretations are possible, disambiguate
3324 if Present (L_Typ)
3325 and then Base_Type (It.Typ) /= Base_Type (L_Typ)
3326 then
3327 It := Disambiguate (L, Valid_I, I, Any_Type);
3329 if It = No_Interp then
3330 Ambiguous_Operands (N);
3331 Set_Etype (L, Any_Type);
3332 return True;
3333 end if;
3335 else
3336 Valid_I := I;
3337 end if;
3339 L_Typ := It.Typ;
3340 Set_Etype (L, L_Typ);
3341 Found := True;
3342 end if;
3344 Get_Next_Interp (I, It);
3345 end loop;
3346 end if;
3348 return Found;
3349 end Find_Interp;
3351 ---------------------
3352 -- Try_Left_Interp --
3353 ---------------------
3355 function Try_Left_Interp (T : Entity_Id) return Boolean is
3356 Found : Boolean;
3357 I : Interp_Index;
3358 It : Interp;
3359 R_Typ : Entity_Id;
3360 Valid_I : Interp_Index;
3362 begin
3363 -- Defend against previous error
3365 if Nkind (R) = N_Error then
3366 Found := False;
3368 -- Loop through the interpretations of the right operand
3370 elsif not Is_Overloaded (R) then
3371 Found := Is_Valid_Pair (T, Etype (R));
3373 else
3374 Found := False;
3375 R_Typ := Empty;
3376 Valid_I := 0;
3378 Get_First_Interp (R, I, It);
3379 while Present (It.Typ) loop
3380 if Is_Valid_Pair (T, It.Typ) then
3381 -- If several interpretations are possible, disambiguate
3383 if Present (R_Typ)
3384 and then Base_Type (It.Typ) /= Base_Type (R_Typ)
3385 then
3386 It := Disambiguate (R, Valid_I, I, Any_Type);
3388 if It = No_Interp then
3389 Ambiguous_Operands (N);
3390 Set_Etype (R, Any_Type);
3391 return True;
3392 end if;
3394 else
3395 Valid_I := I;
3396 end if;
3398 R_Typ := It.Typ;
3399 Found := True;
3400 end if;
3402 Get_Next_Interp (I, It);
3403 end loop;
3404 end if;
3406 return Found;
3407 end Try_Left_Interp;
3409 -------------------
3410 -- Is_Valid_Pair --
3411 -------------------
3413 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean is
3414 begin
3415 return Covers (T1 => T1, T2 => T2)
3416 or else Covers (T1 => T2, T2 => T1)
3417 or else Is_User_Defined_Literal (L, T2)
3418 or else Is_User_Defined_Literal (R, T1);
3419 end Is_Valid_Pair;
3421 -- Local variables
3423 Dummy : Boolean;
3424 Op : Node_Id;
3426 -- Start of processing for Analyze_Membership_Op
3428 begin
3429 Analyze_Expression (L);
3431 if No (R) then
3432 pragma Assert (Ada_Version >= Ada_2012);
3434 Analyze_Set_Membership;
3436 declare
3437 Alt : Node_Id;
3438 begin
3439 Alt := First (Alternatives (N));
3440 while Present (Alt) loop
3441 if Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)) then
3442 Check_Fully_Declared (Entity (Alt), Alt);
3444 if Has_Ghost_Predicate_Aspect (Entity (Alt)) then
3445 Error_Msg_NE
3446 ("subtype& has ghost predicate, "
3447 & "not allowed in membership test",
3448 Alt, Entity (Alt));
3449 end if;
3450 end if;
3452 Next (Alt);
3453 end loop;
3454 end;
3456 elsif Nkind (R) = N_Range
3457 or else (Nkind (R) = N_Attribute_Reference
3458 and then Attribute_Name (R) = Name_Range)
3459 then
3460 Analyze_Expression (R);
3462 Dummy := Find_Interp;
3464 -- If not a range, it can be a subtype mark, or else it is a degenerate
3465 -- membership test with a singleton value, i.e. a test for equality,
3466 -- if the types are compatible.
3468 else
3469 Analyze_Expression (R);
3471 if Is_Entity_Name (R) and then Is_Type (Entity (R)) then
3472 Find_Type (R);
3473 Check_Fully_Declared (Entity (R), R);
3475 if Has_Ghost_Predicate_Aspect (Entity (R)) then
3476 Error_Msg_NE
3477 ("subtype& has ghost predicate, "
3478 & "not allowed in membership test",
3479 R, Entity (R));
3480 end if;
3482 elsif Ada_Version >= Ada_2012 and then Find_Interp then
3483 Op := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
3484 Resolve_Membership_Equality (Op, Etype (L));
3486 if Nkind (N) = N_Not_In then
3487 Op := Make_Op_Not (Loc, Op);
3488 end if;
3490 Rewrite (N, Op);
3491 Analyze (N);
3492 return;
3494 else
3495 -- In all versions of the language, if we reach this point there
3496 -- is a previous error that will be diagnosed below.
3498 Find_Type (R);
3499 end if;
3500 end if;
3502 -- Compatibility between expression and subtype mark or range is
3503 -- checked during resolution. The result of the operation is Boolean
3504 -- in any case.
3506 Set_Etype (N, Standard_Boolean);
3508 if Comes_From_Source (N)
3509 and then Present (Right_Opnd (N))
3510 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3511 then
3512 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3513 end if;
3515 Check_Function_Writable_Actuals (N);
3516 end Analyze_Membership_Op;
3518 -----------------
3519 -- Analyze_Mod --
3520 -----------------
3522 procedure Analyze_Mod (N : Node_Id) is
3523 begin
3524 -- A special warning check, if we have an expression of the form:
3525 -- expr mod 2 * literal
3526 -- where literal is 128 or less, then probably what was meant was
3527 -- expr mod 2 ** literal
3528 -- so issue an appropriate warning.
3530 if Warn_On_Suspicious_Modulus_Value
3531 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3532 and then Intval (Right_Opnd (N)) = Uint_2
3533 and then Nkind (Parent (N)) = N_Op_Multiply
3534 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3535 and then Intval (Right_Opnd (Parent (N))) <= Uint_128
3536 then
3537 Error_Msg_N
3538 ("suspicious MOD value, was '*'* intended'??.m?", Parent (N));
3539 end if;
3541 -- Remaining processing is same as for other arithmetic operators
3543 Analyze_Arithmetic_Op (N);
3544 end Analyze_Mod;
3546 ----------------------
3547 -- Analyze_Negation --
3548 ----------------------
3550 procedure Analyze_Negation (N : Node_Id) is
3551 R : constant Node_Id := Right_Opnd (N);
3553 Op_Id : Entity_Id;
3555 begin
3556 Set_Etype (N, Any_Type);
3557 Candidate_Type := Empty;
3559 Analyze_Expression (R);
3561 -- If the entity is already set, the node is the instantiation of a
3562 -- generic node with a non-local reference, or was manufactured by a
3563 -- call to Make_Op_xxx. In either case the entity is known to be valid,
3564 -- and we do not need to collect interpretations, instead we just get
3565 -- the single possible interpretation.
3567 if Present (Entity (N)) then
3568 Op_Id := Entity (N);
3570 if Ekind (Op_Id) = E_Operator then
3571 Find_Negation_Types (R, Op_Id, N);
3572 else
3573 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3574 end if;
3576 else
3577 Op_Id := Get_Name_Entity_Id (Chars (N));
3578 while Present (Op_Id) loop
3579 if Ekind (Op_Id) = E_Operator then
3580 Find_Negation_Types (R, Op_Id, N);
3581 else
3582 Analyze_User_Defined_Unary_Op (N, Op_Id);
3583 end if;
3585 Op_Id := Homonym (Op_Id);
3586 end loop;
3587 end if;
3589 Operator_Check (N);
3590 end Analyze_Negation;
3592 ------------------
3593 -- Analyze_Null --
3594 ------------------
3596 procedure Analyze_Null (N : Node_Id) is
3597 begin
3598 Set_Etype (N, Universal_Access);
3599 end Analyze_Null;
3601 ----------------------
3602 -- Analyze_One_Call --
3603 ----------------------
3605 procedure Analyze_One_Call
3606 (N : Node_Id;
3607 Nam : Entity_Id;
3608 Report : Boolean;
3609 Success : out Boolean;
3610 Skip_First : Boolean := False)
3612 Actuals : constant List_Id := Parameter_Associations (N);
3613 Prev_T : constant Entity_Id := Etype (N);
3615 -- Recognize cases of prefixed calls that have been rewritten in
3616 -- various ways. The simplest case is a rewritten selected component,
3617 -- but it can also be an already-examined indexed component, or a
3618 -- prefix that is itself a rewritten prefixed call that is in turn
3619 -- an indexed call (the syntactic ambiguity involving the indexing of
3620 -- a function with defaulted parameters that returns an array).
3621 -- A flag Maybe_Indexed_Call might be useful here ???
3623 Must_Skip : constant Boolean := Skip_First
3624 or else Nkind (Original_Node (N)) = N_Selected_Component
3625 or else
3626 (Nkind (Original_Node (N)) = N_Indexed_Component
3627 and then Nkind (Prefix (Original_Node (N))) =
3628 N_Selected_Component)
3629 or else
3630 (Nkind (Parent (N)) = N_Function_Call
3631 and then Is_Array_Type (Etype (Name (N)))
3632 and then Etype (Original_Node (N)) =
3633 Component_Type (Etype (Name (N)))
3634 and then Nkind (Original_Node (Parent (N))) =
3635 N_Selected_Component);
3637 -- The first formal must be omitted from the match when trying to find
3638 -- a primitive operation that is a possible interpretation, and also
3639 -- after the call has been rewritten, because the corresponding actual
3640 -- is already known to be compatible, and because this may be an
3641 -- indexing of a call with default parameters.
3643 First_Form : Entity_Id;
3644 Formal : Entity_Id;
3645 Actual : Node_Id;
3646 Is_Indexed : Boolean := False;
3647 Is_Indirect : Boolean := False;
3648 Subp_Type : constant Entity_Id := Etype (Nam);
3649 Norm_OK : Boolean;
3651 function Compatible_Types_In_Predicate
3652 (T1 : Entity_Id;
3653 T2 : Entity_Id) return Boolean;
3654 -- For an Ada 2012 predicate or invariant, a call may mention an
3655 -- incomplete type, while resolution of the corresponding predicate
3656 -- function may see the full view, as a consequence of the delayed
3657 -- resolution of the corresponding expressions. This may occur in
3658 -- the body of a predicate function, or in a call to such. Anomalies
3659 -- involving private and full views can also happen. In each case,
3660 -- rewrite node or add conversions to remove spurious type errors.
3662 procedure Indicate_Name_And_Type;
3663 -- If candidate interpretation matches, indicate name and type of result
3664 -- on call node.
3666 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3667 -- There may be a user-defined operator that hides the current
3668 -- interpretation. We must check for this independently of the
3669 -- analysis of the call with the user-defined operation, because
3670 -- the parameter names may be wrong and yet the hiding takes place.
3671 -- This fixes a problem with ACATS test B34014O.
3673 -- When the type Address is a visible integer type, and the DEC
3674 -- system extension is visible, the predefined operator may be
3675 -- hidden as well, by one of the address operations in auxdec.
3676 -- Finally, the abstract operations on address do not hide the
3677 -- predefined operator (this is the purpose of making them abstract).
3679 -----------------------------------
3680 -- Compatible_Types_In_Predicate --
3681 -----------------------------------
3683 function Compatible_Types_In_Predicate
3684 (T1 : Entity_Id;
3685 T2 : Entity_Id) return Boolean
3687 function Common_Type (T : Entity_Id) return Entity_Id;
3688 -- Find non-private underlying full view if any, without going to
3689 -- ancestor type (as opposed to Underlying_Type).
3691 -----------------
3692 -- Common_Type --
3693 -----------------
3695 function Common_Type (T : Entity_Id) return Entity_Id is
3696 CT : Entity_Id;
3698 begin
3699 CT := T;
3701 if Is_Private_Type (CT) and then Present (Full_View (CT)) then
3702 CT := Full_View (CT);
3703 end if;
3705 if Is_Private_Type (CT)
3706 and then Present (Underlying_Full_View (CT))
3707 then
3708 CT := Underlying_Full_View (CT);
3709 end if;
3711 return Base_Type (CT);
3712 end Common_Type;
3714 -- Start of processing for Compatible_Types_In_Predicate
3716 begin
3717 if (Ekind (Current_Scope) = E_Function
3718 and then Is_Predicate_Function (Current_Scope))
3719 or else
3720 (Ekind (Nam) = E_Function
3721 and then Is_Predicate_Function (Nam))
3722 then
3723 if Is_Incomplete_Type (T1)
3724 and then Present (Full_View (T1))
3725 and then Full_View (T1) = T2
3726 then
3727 Set_Etype (Formal, Etype (Actual));
3728 return True;
3730 elsif Common_Type (T1) = Common_Type (T2) then
3731 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3732 return True;
3734 else
3735 return False;
3736 end if;
3738 else
3739 return False;
3740 end if;
3741 end Compatible_Types_In_Predicate;
3743 ----------------------------
3744 -- Indicate_Name_And_Type --
3745 ----------------------------
3747 procedure Indicate_Name_And_Type is
3748 begin
3749 Add_One_Interp (N, Nam, Etype (Nam));
3750 Check_Implicit_Dereference (N, Etype (Nam));
3751 Success := True;
3753 -- If the prefix of the call is a name, indicate the entity
3754 -- being called. If it is not a name, it is an expression that
3755 -- denotes an access to subprogram or else an entry or family. In
3756 -- the latter case, the name is a selected component, and the entity
3757 -- being called is noted on the selector.
3759 if not Is_Type (Nam) then
3760 if Is_Entity_Name (Name (N)) then
3761 Set_Entity (Name (N), Nam);
3762 Set_Etype (Name (N), Etype (Nam));
3764 elsif Nkind (Name (N)) = N_Selected_Component then
3765 Set_Entity (Selector_Name (Name (N)), Nam);
3766 end if;
3767 end if;
3769 if Debug_Flag_E and not Report then
3770 Write_Str (" Overloaded call ");
3771 Write_Int (Int (N));
3772 Write_Str (" compatible with ");
3773 Write_Int (Int (Nam));
3774 Write_Eol;
3775 end if;
3776 end Indicate_Name_And_Type;
3778 ------------------------
3779 -- Operator_Hidden_By --
3780 ------------------------
3782 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3783 Act1 : constant Node_Id := First_Actual (N);
3784 Act2 : constant Node_Id := Next_Actual (Act1);
3785 Form1 : constant Entity_Id := First_Formal (Fun);
3786 Form2 : constant Entity_Id := Next_Formal (Form1);
3788 begin
3789 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3790 return False;
3792 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3793 return False;
3795 elsif Present (Form2) then
3796 if No (Act2)
3797 or else not Has_Compatible_Type (Act2, Etype (Form2))
3798 then
3799 return False;
3800 end if;
3802 elsif Present (Act2) then
3803 return False;
3804 end if;
3806 -- Now we know that the arity of the operator matches the function,
3807 -- and the function call is a valid interpretation. The function
3808 -- hides the operator if it has the right signature, or if one of
3809 -- its operands is a non-abstract operation on Address when this is
3810 -- a visible integer type.
3812 return Hides_Op (Fun, Nam)
3813 or else Is_Descendant_Of_Address (Etype (Form1))
3814 or else
3815 (Present (Form2)
3816 and then Is_Descendant_Of_Address (Etype (Form2)));
3817 end Operator_Hidden_By;
3819 -- Start of processing for Analyze_One_Call
3821 begin
3822 Success := False;
3824 -- If the subprogram has no formals or if all the formals have defaults,
3825 -- and the return type is an array type, the node may denote an indexing
3826 -- of the result of a parameterless call. In Ada 2005, the subprogram
3827 -- may have one non-defaulted formal, and the call may have been written
3828 -- in prefix notation, so that the rebuilt parameter list has more than
3829 -- one actual.
3831 if not Is_Overloadable (Nam)
3832 and then Ekind (Nam) /= E_Subprogram_Type
3833 and then Ekind (Nam) /= E_Entry_Family
3834 then
3835 return;
3836 end if;
3838 -- An indexing requires at least one actual. The name of the call cannot
3839 -- be an implicit indirect call, so it cannot be a generated explicit
3840 -- dereference.
3842 if not Is_Empty_List (Actuals)
3843 and then
3844 (Needs_No_Actuals (Nam)
3845 or else
3846 (Needs_One_Actual (Nam)
3847 and then Present (Next_Actual (First (Actuals)))))
3848 then
3849 if Is_Array_Type (Subp_Type)
3850 and then
3851 (Nkind (Name (N)) /= N_Explicit_Dereference
3852 or else Comes_From_Source (Name (N)))
3853 then
3854 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3856 elsif Is_Access_Type (Subp_Type)
3857 and then Is_Array_Type (Designated_Type (Subp_Type))
3858 then
3859 Is_Indexed :=
3860 Try_Indexed_Call
3861 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3863 -- The prefix can also be a parameterless function that returns an
3864 -- access to subprogram, in which case this is an indirect call.
3865 -- If this succeeds, an explicit dereference is added later on,
3866 -- in Analyze_Call or Resolve_Call.
3868 elsif Is_Access_Type (Subp_Type)
3869 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3870 then
3871 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3872 end if;
3874 end if;
3876 -- If the call has been transformed into a slice, it is of the form
3877 -- F (Subtype) where F is parameterless. The node has been rewritten in
3878 -- Try_Indexed_Call and there is nothing else to do.
3880 if Is_Indexed
3881 and then Nkind (N) = N_Slice
3882 then
3883 return;
3884 end if;
3886 Normalize_Actuals
3887 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3889 if not Norm_OK then
3891 -- If an indirect call is a possible interpretation, indicate
3892 -- success to the caller. This may be an indexing of an explicit
3893 -- dereference of a call that returns an access type (see above).
3895 if Is_Indirect
3896 or else (Is_Indexed
3897 and then Nkind (Name (N)) = N_Explicit_Dereference
3898 and then Comes_From_Source (Name (N)))
3899 then
3900 Success := True;
3901 return;
3903 -- Mismatch in number or names of parameters
3905 elsif Debug_Flag_E then
3906 Write_Str (" normalization fails in call ");
3907 Write_Int (Int (N));
3908 Write_Str (" with subprogram ");
3909 Write_Int (Int (Nam));
3910 Write_Eol;
3911 end if;
3913 -- If the context expects a function call, discard any interpretation
3914 -- that is a procedure. If the node is not overloaded, leave as is for
3915 -- better error reporting when type mismatch is found.
3917 elsif Nkind (N) = N_Function_Call
3918 and then Is_Overloaded (Name (N))
3919 and then Ekind (Nam) = E_Procedure
3920 then
3921 return;
3923 -- Ditto for function calls in a procedure context
3925 elsif Nkind (N) = N_Procedure_Call_Statement
3926 and then Is_Overloaded (Name (N))
3927 and then Etype (Nam) /= Standard_Void_Type
3928 then
3929 return;
3931 elsif No (Actuals) then
3933 -- If Normalize succeeds, then there are default parameters for
3934 -- all formals.
3936 Indicate_Name_And_Type;
3938 elsif Ekind (Nam) = E_Operator then
3939 if Nkind (N) = N_Procedure_Call_Statement then
3940 return;
3941 end if;
3943 -- This occurs when the prefix of the call is an operator name
3944 -- or an expanded name whose selector is an operator name.
3946 Analyze_Operator_Call (N, Nam);
3948 if Etype (N) /= Prev_T then
3950 -- Check that operator is not hidden by a function interpretation
3952 if Is_Overloaded (Name (N)) then
3953 declare
3954 I : Interp_Index;
3955 It : Interp;
3957 begin
3958 Get_First_Interp (Name (N), I, It);
3959 while Present (It.Nam) loop
3960 if Operator_Hidden_By (It.Nam) then
3961 Set_Etype (N, Prev_T);
3962 return;
3963 end if;
3965 Get_Next_Interp (I, It);
3966 end loop;
3967 end;
3968 end if;
3970 -- If operator matches formals, record its name on the call.
3971 -- If the operator is overloaded, Resolve will select the
3972 -- correct one from the list of interpretations. The call
3973 -- node itself carries the first candidate.
3975 Set_Entity (Name (N), Nam);
3976 Success := True;
3978 elsif Report and then Etype (N) = Any_Type then
3979 Error_Msg_N ("incompatible arguments for operator", N);
3980 end if;
3982 else
3983 -- Normalize_Actuals has chained the named associations in the
3984 -- correct order of the formals.
3986 Actual := First_Actual (N);
3987 Formal := First_Formal (Nam);
3988 First_Form := Formal;
3990 -- If we are analyzing a call rewritten from object notation, skip
3991 -- first actual, which may be rewritten later as an explicit
3992 -- dereference.
3994 if Must_Skip then
3995 Next_Actual (Actual);
3996 Next_Formal (Formal);
3997 end if;
3999 while Present (Actual) and then Present (Formal) loop
4000 if Nkind (Parent (Actual)) /= N_Parameter_Association
4001 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
4002 then
4003 -- The actual can be compatible with the formal, but we must
4004 -- also check that the context is not an address type that is
4005 -- visibly an integer type. In this case the use of literals is
4006 -- illegal, except in the body of descendants of system, where
4007 -- arithmetic operations on address are of course used.
4009 if Has_Compatible_Type (Actual, Etype (Formal))
4010 and then
4011 (Etype (Actual) /= Universal_Integer
4012 or else not Is_Descendant_Of_Address (Etype (Formal))
4013 or else In_Predefined_Unit (N))
4014 then
4015 Next_Actual (Actual);
4016 Next_Formal (Formal);
4018 -- In Allow_Integer_Address mode, we allow an actual integer to
4019 -- match a formal address type and vice versa. We only do this
4020 -- if we are certain that an error will otherwise be issued
4022 elsif Address_Integer_Convert_OK
4023 (Etype (Actual), Etype (Formal))
4024 and then (Report and not Is_Indexed and not Is_Indirect)
4025 then
4026 -- Handle this case by introducing an unchecked conversion
4028 Rewrite (Actual,
4029 Unchecked_Convert_To (Etype (Formal),
4030 Relocate_Node (Actual)));
4031 Analyze_And_Resolve (Actual, Etype (Formal));
4032 Next_Actual (Actual);
4033 Next_Formal (Formal);
4035 -- Under relaxed RM semantics silently replace occurrences of
4036 -- null by System.Address_Null. We only do this if we know that
4037 -- an error will otherwise be issued.
4039 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
4040 and then (Report and not Is_Indexed and not Is_Indirect)
4041 then
4042 Replace_Null_By_Null_Address (Actual);
4043 Analyze_And_Resolve (Actual, Etype (Formal));
4044 Next_Actual (Actual);
4045 Next_Formal (Formal);
4047 elsif Compatible_Types_In_Predicate
4048 (Etype (Formal), Etype (Actual))
4049 then
4050 Next_Actual (Actual);
4051 Next_Formal (Formal);
4053 -- A current instance used as an actual of a function,
4054 -- whose body has not been seen, may include a formal
4055 -- whose type is an incomplete view of an enclosing
4056 -- type declaration containing the current call (e.g.
4057 -- in the Expression for a component declaration).
4059 -- In this case, update the signature of the subprogram
4060 -- so the formal has the type of the full view.
4062 elsif Inside_Init_Proc
4063 and then Nkind (Actual) = N_Identifier
4064 and then Ekind (Etype (Formal)) = E_Incomplete_Type
4065 and then Etype (Actual) = Full_View (Etype (Formal))
4066 then
4067 Set_Etype (Formal, Etype (Actual));
4068 Next_Actual (Actual);
4069 Next_Formal (Formal);
4071 -- Handle failed type check
4073 else
4074 if Debug_Flag_E then
4075 Write_Str (" type checking fails in call ");
4076 Write_Int (Int (N));
4077 Write_Str (" with formal ");
4078 Write_Int (Int (Formal));
4079 Write_Str (" in subprogram ");
4080 Write_Int (Int (Nam));
4081 Write_Eol;
4082 end if;
4084 -- Comment needed on the following test???
4086 if Report and not Is_Indexed and not Is_Indirect then
4088 -- Ada 2005 (AI-251): Complete the error notification
4089 -- to help new Ada 2005 users.
4091 if Is_Class_Wide_Type (Etype (Formal))
4092 and then Is_Interface (Etype (Etype (Formal)))
4093 and then not Interface_Present_In_Ancestor
4094 (Typ => Etype (Actual),
4095 Iface => Etype (Etype (Formal)))
4096 then
4097 Error_Msg_NE
4098 ("(Ada 2005) does not implement interface }",
4099 Actual, Etype (Etype (Formal)));
4100 end if;
4102 -- If we are going to output a secondary error message
4103 -- below, we need to have Wrong_Type output the main one.
4105 Wrong_Type
4106 (Actual, Etype (Formal), Multiple => All_Errors_Mode);
4108 if Nkind (Actual) = N_Op_Eq
4109 and then Nkind (Left_Opnd (Actual)) = N_Identifier
4110 then
4111 Formal := First_Formal (Nam);
4112 while Present (Formal) loop
4113 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
4114 Error_Msg_N -- CODEFIX
4115 ("possible misspelling of `='>`!", Actual);
4116 exit;
4117 end if;
4119 Next_Formal (Formal);
4120 end loop;
4121 end if;
4123 if All_Errors_Mode then
4124 Error_Msg_Sloc := Sloc (Nam);
4126 if Etype (Formal) = Any_Type then
4127 Error_Msg_N
4128 ("there is no legal actual parameter", Actual);
4129 end if;
4131 if Is_Overloadable (Nam)
4132 and then Present (Alias (Nam))
4133 and then not Comes_From_Source (Nam)
4134 then
4135 Error_Msg_NE
4136 ("\\ =='> in call to inherited operation & #!",
4137 Actual, Nam);
4139 elsif Ekind (Nam) = E_Subprogram_Type then
4140 declare
4141 Access_To_Subprogram_Typ :
4142 constant Entity_Id :=
4143 Defining_Identifier
4144 (Associated_Node_For_Itype (Nam));
4145 begin
4146 Error_Msg_NE
4147 ("\\ =='> in call to dereference of &#!",
4148 Actual, Access_To_Subprogram_Typ);
4149 end;
4151 else
4152 Error_Msg_NE
4153 ("\\ =='> in call to &#!", Actual, Nam);
4155 end if;
4156 end if;
4157 end if;
4159 return;
4160 end if;
4162 else
4163 -- Normalize_Actuals has verified that a default value exists
4164 -- for this formal. Current actual names a subsequent formal.
4166 Next_Formal (Formal);
4167 end if;
4168 end loop;
4170 -- Due to our current model of controlled type expansion we may
4171 -- have resolved a user call to a non-visible controlled primitive
4172 -- since these inherited subprograms may be generated in the current
4173 -- scope. This is a side effect of the need for the expander to be
4174 -- able to resolve internally generated calls.
4176 -- Specifically, the issue appears when predefined controlled
4177 -- operations get called on a type extension whose parent is a
4178 -- private extension completed with a controlled extension - see
4179 -- below:
4181 -- package X is
4182 -- type Par_Typ is tagged private;
4183 -- private
4184 -- type Par_Typ is new Controlled with null record;
4185 -- end;
4186 -- ...
4187 -- procedure Main is
4188 -- type Ext_Typ is new Par_Typ with null record;
4189 -- Obj : Ext_Typ;
4190 -- begin
4191 -- Finalize (Obj); -- Will improperly resolve
4192 -- end;
4194 -- To avoid breaking privacy, Is_Hidden gets set elsewhere on such
4195 -- primitives, but we still need to verify that Nam is indeed a
4196 -- non-visible controlled subprogram. So, we do that here and issue
4197 -- the appropriate error.
4199 if Is_Hidden (Nam)
4200 and then not In_Instance
4201 and then not Comes_From_Source (Nam)
4202 and then Comes_From_Source (N)
4204 -- Verify Nam is a non-visible controlled primitive
4206 and then Chars (Nam) in Name_Adjust
4207 | Name_Finalize
4208 | Name_Initialize
4209 and then Ekind (Nam) = E_Procedure
4210 and then Is_Controlled (Etype (First_Form))
4211 and then No (Next_Formal (First_Form))
4212 and then not Is_Visibly_Controlled (Etype (First_Form))
4213 then
4214 Error_Msg_Node_2 := Etype (First_Form);
4215 Error_Msg_NE ("call to non-visible controlled primitive & on type"
4216 & " &", N, Nam);
4217 end if;
4219 -- On exit, all actuals match
4221 Indicate_Name_And_Type;
4222 end if;
4223 end Analyze_One_Call;
4225 ---------------------------
4226 -- Analyze_Operator_Call --
4227 ---------------------------
4229 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
4230 Op_Name : constant Name_Id := Chars (Op_Id);
4231 Act1 : constant Node_Id := First_Actual (N);
4232 Act2 : constant Node_Id := Next_Actual (Act1);
4234 begin
4235 -- Binary operator case
4237 if Present (Act2) then
4239 -- If more than two operands, then not binary operator after all
4241 if Present (Next_Actual (Act2)) then
4242 return;
4243 end if;
4245 -- Otherwise action depends on operator
4247 case Op_Name is
4248 when Name_Op_Add
4249 | Name_Op_Divide
4250 | Name_Op_Expon
4251 | Name_Op_Mod
4252 | Name_Op_Multiply
4253 | Name_Op_Rem
4254 | Name_Op_Subtract
4256 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
4258 when Name_Op_And
4259 | Name_Op_Or
4260 | Name_Op_Xor
4262 Find_Boolean_Types (Act1, Act2, Op_Id, N);
4264 when Name_Op_Eq
4265 | Name_Op_Ge
4266 | Name_Op_Gt
4267 | Name_Op_Le
4268 | Name_Op_Lt
4269 | Name_Op_Ne
4271 Find_Comparison_Equality_Types (Act1, Act2, Op_Id, N);
4273 when Name_Op_Concat =>
4274 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
4276 -- Is this when others, or should it be an abort???
4278 when others =>
4279 null;
4280 end case;
4282 -- Unary operator case
4284 else
4285 case Op_Name is
4286 when Name_Op_Abs
4287 | Name_Op_Add
4288 | Name_Op_Subtract
4290 Find_Unary_Types (Act1, Op_Id, N);
4292 when Name_Op_Not =>
4293 Find_Negation_Types (Act1, Op_Id, N);
4295 -- Is this when others correct, or should it be an abort???
4297 when others =>
4298 null;
4299 end case;
4300 end if;
4301 end Analyze_Operator_Call;
4303 -------------------------------------------
4304 -- Analyze_Overloaded_Selected_Component --
4305 -------------------------------------------
4307 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
4308 Nam : constant Node_Id := Prefix (N);
4309 Sel : constant Node_Id := Selector_Name (N);
4310 Comp : Entity_Id;
4311 I : Interp_Index;
4312 It : Interp;
4313 T : Entity_Id;
4315 begin
4316 Set_Etype (Sel, Any_Type);
4318 Get_First_Interp (Nam, I, It);
4319 while Present (It.Typ) loop
4320 if Is_Access_Type (It.Typ) then
4321 T := Designated_Type (It.Typ);
4322 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4323 else
4324 T := It.Typ;
4325 end if;
4327 -- Locate the component. For a private prefix the selector can denote
4328 -- a discriminant.
4330 if Is_Record_Type (T) or else Is_Private_Type (T) then
4332 -- If the prefix is a class-wide type, the visible components are
4333 -- those of the base type.
4335 if Is_Class_Wide_Type (T) then
4336 T := Etype (T);
4337 end if;
4339 Comp := First_Entity (T);
4340 while Present (Comp) loop
4341 if Chars (Comp) = Chars (Sel)
4342 and then Is_Visible_Component (Comp, Sel)
4343 then
4345 -- AI05-105: if the context is an object renaming with
4346 -- an anonymous access type, the expected type of the
4347 -- object must be anonymous. This is a name resolution rule.
4349 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
4350 or else No (Access_Definition (Parent (N)))
4351 or else Is_Anonymous_Access_Type (Etype (Comp))
4352 then
4353 Set_Entity (Sel, Comp);
4354 Set_Etype (Sel, Etype (Comp));
4355 Add_One_Interp (N, Etype (Comp), Etype (Comp));
4356 Check_Implicit_Dereference (N, Etype (Comp));
4358 -- This also specifies a candidate to resolve the name.
4359 -- Further overloading will be resolved from context.
4360 -- The selector name itself does not carry overloading
4361 -- information.
4363 Set_Etype (Nam, It.Typ);
4365 else
4366 -- Named access type in the context of a renaming
4367 -- declaration with an access definition. Remove
4368 -- inapplicable candidate.
4370 Remove_Interp (I);
4371 end if;
4372 end if;
4374 Next_Entity (Comp);
4375 end loop;
4377 elsif Is_Concurrent_Type (T) then
4378 Comp := First_Entity (T);
4379 while Present (Comp)
4380 and then Comp /= First_Private_Entity (T)
4381 loop
4382 if Chars (Comp) = Chars (Sel) then
4383 if Is_Overloadable (Comp) then
4384 Add_One_Interp (Sel, Comp, Etype (Comp));
4385 else
4386 Set_Entity_With_Checks (Sel, Comp);
4387 Generate_Reference (Comp, Sel);
4388 end if;
4390 Set_Etype (Sel, Etype (Comp));
4391 Set_Etype (N, Etype (Comp));
4392 Set_Etype (Nam, It.Typ);
4393 end if;
4395 Next_Entity (Comp);
4396 end loop;
4398 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4399 end if;
4401 Get_Next_Interp (I, It);
4402 end loop;
4404 if Etype (N) = Any_Type
4405 and then not Try_Object_Operation (N)
4406 then
4407 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
4408 Set_Entity (Sel, Any_Id);
4409 Set_Etype (Sel, Any_Type);
4410 end if;
4411 end Analyze_Overloaded_Selected_Component;
4413 ----------------------------------
4414 -- Analyze_Qualified_Expression --
4415 ----------------------------------
4417 procedure Analyze_Qualified_Expression (N : Node_Id) is
4418 Expr : constant Node_Id := Expression (N);
4419 Mark : constant Entity_Id := Subtype_Mark (N);
4421 I : Interp_Index;
4422 It : Interp;
4423 T : Entity_Id;
4425 begin
4426 Find_Type (Mark);
4427 T := Entity (Mark);
4429 if Nkind (Enclosing_Declaration (N)) in
4430 N_Formal_Type_Declaration |
4431 N_Full_Type_Declaration |
4432 N_Incomplete_Type_Declaration |
4433 N_Protected_Type_Declaration |
4434 N_Private_Extension_Declaration |
4435 N_Private_Type_Declaration |
4436 N_Subtype_Declaration |
4437 N_Task_Type_Declaration
4438 and then T = Defining_Identifier (Enclosing_Declaration (N))
4439 then
4440 Error_Msg_N ("current instance not allowed", Mark);
4441 T := Any_Type;
4442 end if;
4444 Set_Etype (N, T);
4446 Analyze_Expression (Expr);
4448 if T = Any_Type then
4449 return;
4450 end if;
4452 Check_Fully_Declared (T, N);
4454 -- If expected type is class-wide, check for exact match before
4455 -- expansion, because if the expression is a dispatching call it
4456 -- may be rewritten as explicit dereference with class-wide result.
4457 -- If expression is overloaded, retain only interpretations that
4458 -- will yield exact matches.
4460 if Is_Class_Wide_Type (T) then
4461 if not Is_Overloaded (Expr) then
4462 if Base_Type (Etype (Expr)) /= Base_Type (T)
4463 and then Etype (Expr) /= Raise_Type
4464 then
4465 if Nkind (Expr) = N_Aggregate then
4466 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
4467 else
4468 Wrong_Type (Expr, T);
4469 end if;
4470 end if;
4472 else
4473 Get_First_Interp (Expr, I, It);
4475 while Present (It.Nam) loop
4476 if Base_Type (It.Typ) /= Base_Type (T) then
4477 Remove_Interp (I);
4478 end if;
4480 Get_Next_Interp (I, It);
4481 end loop;
4482 end if;
4483 end if;
4484 end Analyze_Qualified_Expression;
4486 -----------------------------------
4487 -- Analyze_Quantified_Expression --
4488 -----------------------------------
4490 procedure Analyze_Quantified_Expression (N : Node_Id) is
4491 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
4492 -- Return True if the iterator is part of a quantified expression and
4493 -- the range is known to be statically empty.
4495 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
4496 -- Determine whether if expression If_Expr lacks an else part or if it
4497 -- has one, it evaluates to True.
4499 --------------------
4500 -- Is_Empty_Range --
4501 --------------------
4503 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4504 begin
4505 return Is_Array_Type (Typ)
4506 and then Compile_Time_Known_Bounds (Typ)
4507 and then
4508 Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4509 Expr_Value (Type_High_Bound (Etype (First_Index (Typ))));
4510 end Is_Empty_Range;
4512 -----------------------------
4513 -- No_Else_Or_Trivial_True --
4514 -----------------------------
4516 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4517 Else_Expr : constant Node_Id :=
4518 Next (Next (First (Expressions (If_Expr))));
4519 begin
4520 return
4521 No (Else_Expr)
4522 or else (Compile_Time_Known_Value (Else_Expr)
4523 and then Is_True (Expr_Value (Else_Expr)));
4524 end No_Else_Or_Trivial_True;
4526 -- Local variables
4528 Cond : constant Node_Id := Condition (N);
4529 Loc : constant Source_Ptr := Sloc (N);
4530 Loop_Id : Entity_Id;
4531 QE_Scop : Entity_Id;
4533 -- Start of processing for Analyze_Quantified_Expression
4535 begin
4536 -- Create a scope to emulate the loop-like behavior of the quantified
4537 -- expression. The scope is needed to provide proper visibility of the
4538 -- loop variable.
4540 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
4541 Set_Etype (QE_Scop, Standard_Void_Type);
4542 Set_Scope (QE_Scop, Current_Scope);
4543 Set_Parent (QE_Scop, N);
4545 Push_Scope (QE_Scop);
4547 -- All constituents are preanalyzed and resolved to avoid untimely
4548 -- generation of various temporaries and types. Full analysis and
4549 -- expansion is carried out when the quantified expression is
4550 -- transformed into an expression with actions.
4552 if Present (Iterator_Specification (N)) then
4553 Preanalyze (Iterator_Specification (N));
4555 -- Do not proceed with the analysis when the range of iteration is
4556 -- empty.
4558 if Is_Entity_Name (Name (Iterator_Specification (N)))
4559 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4560 then
4561 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4562 End_Scope;
4564 -- Emit a warning and replace expression with its static value
4566 if All_Present (N) then
4567 Error_Msg_N
4568 ("??quantified expression with ALL "
4569 & "over a null range has value True", N);
4570 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4572 else
4573 Error_Msg_N
4574 ("??quantified expression with SOME "
4575 & "over a null range has value False", N);
4576 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4577 end if;
4579 Analyze (N);
4580 return;
4581 end if;
4583 else pragma Assert (Present (Loop_Parameter_Specification (N)));
4584 declare
4585 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4587 begin
4588 Preanalyze (Loop_Par);
4590 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
4591 and then Parent (Loop_Par) /= N
4592 then
4593 -- The parser cannot distinguish between a loop specification
4594 -- and an iterator specification. If after preanalysis the
4595 -- proper form has been recognized, rewrite the expression to
4596 -- reflect the right kind. This is needed for proper ASIS
4597 -- navigation. If expansion is enabled, the transformation is
4598 -- performed when the expression is rewritten as a loop.
4599 -- Is this still needed???
4601 Set_Iterator_Specification (N,
4602 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
4604 Set_Defining_Identifier (Iterator_Specification (N),
4605 Relocate_Node (Defining_Identifier (Loop_Par)));
4606 Set_Name (Iterator_Specification (N),
4607 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4608 Set_Comes_From_Source (Iterator_Specification (N),
4609 Comes_From_Source (Loop_Parameter_Specification (N)));
4610 Set_Loop_Parameter_Specification (N, Empty);
4611 end if;
4612 end;
4613 end if;
4615 Preanalyze_And_Resolve (Cond, Standard_Boolean);
4617 End_Scope;
4618 Set_Etype (N, Standard_Boolean);
4620 -- Verify that the loop variable is used within the condition of the
4621 -- quantified expression.
4623 if Present (Iterator_Specification (N)) then
4624 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4625 else
4626 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4627 end if;
4629 declare
4630 type Subexpr_Kind is (Full, Conjunct, Disjunct);
4632 procedure Check_Subexpr (Expr : Node_Id; Kind : Subexpr_Kind);
4633 -- Check that the quantified variable appears in every sub-expression
4634 -- of the quantified expression. If Kind is Full, Expr is the full
4635 -- expression. If Kind is Conjunct (resp. Disjunct), Expr is a
4636 -- conjunct (resp. disjunct) of the full expression.
4638 -------------------
4639 -- Check_Subexpr --
4640 -------------------
4642 procedure Check_Subexpr (Expr : Node_Id; Kind : Subexpr_Kind) is
4643 begin
4644 if Nkind (Expr) in N_Op_And | N_And_Then
4645 and then Kind /= Disjunct
4646 then
4647 Check_Subexpr (Left_Opnd (Expr), Conjunct);
4648 Check_Subexpr (Right_Opnd (Expr), Conjunct);
4650 elsif Nkind (Expr) in N_Op_Or | N_Or_Else
4651 and then Kind /= Conjunct
4652 then
4653 Check_Subexpr (Left_Opnd (Expr), Disjunct);
4654 Check_Subexpr (Right_Opnd (Expr), Disjunct);
4656 elsif Kind /= Full
4657 and then not Referenced (Loop_Id, Expr)
4658 then
4659 declare
4660 Sub : constant String :=
4661 (if Kind = Conjunct then "conjunct" else "disjunct");
4662 begin
4663 Error_Msg_NE
4664 ("?.t?unused variable & in " & Sub, Expr, Loop_Id);
4665 Error_Msg_NE
4666 ("\consider extracting " & Sub & " from quantified "
4667 & "expression", Expr, Loop_Id);
4668 end;
4669 end if;
4670 end Check_Subexpr;
4672 begin
4673 if Warn_On_Suspicious_Contract
4674 and then not Is_Internal_Name (Chars (Loop_Id))
4676 -- Generating C, this check causes spurious warnings on inlined
4677 -- postconditions; we can safely disable it because this check
4678 -- was previously performed when analyzing the internally built
4679 -- postconditions procedure.
4681 and then not (Modify_Tree_For_C and In_Inlined_Body)
4682 then
4683 if not Referenced (Loop_Id, Cond) then
4684 Error_Msg_N ("?.t?unused variable &", Loop_Id);
4685 else
4686 Check_Subexpr (Cond, Kind => Full);
4687 end if;
4688 end if;
4689 end;
4691 -- Diagnose a possible misuse of the SOME existential quantifier. When
4692 -- we have a quantified expression of the form:
4694 -- for some X => (if P then Q [else True])
4696 -- any value for X that makes P False results in the if expression being
4697 -- trivially True, and so also results in the quantified expression
4698 -- being trivially True.
4700 if Warn_On_Suspicious_Contract
4701 and then not All_Present (N)
4702 and then Nkind (Cond) = N_If_Expression
4703 and then No_Else_Or_Trivial_True (Cond)
4704 then
4705 Error_Msg_N ("?.t?suspicious expression", N);
4706 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4707 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4708 end if;
4709 end Analyze_Quantified_Expression;
4711 -------------------
4712 -- Analyze_Range --
4713 -------------------
4715 procedure Analyze_Range (N : Node_Id) is
4716 L : constant Node_Id := Low_Bound (N);
4717 H : constant Node_Id := High_Bound (N);
4718 I1, I2 : Interp_Index;
4719 It1, It2 : Interp;
4721 procedure Check_Common_Type (T1, T2 : Entity_Id);
4722 -- Verify the compatibility of two types, and choose the
4723 -- non universal one if the other is universal.
4725 procedure Check_High_Bound (T : Entity_Id);
4726 -- Test one interpretation of the low bound against all those
4727 -- of the high bound.
4729 procedure Check_Universal_Expression (N : Node_Id);
4730 -- In Ada 83, reject bounds of a universal range that are not literals
4731 -- or entity names.
4733 -----------------------
4734 -- Check_Common_Type --
4735 -----------------------
4737 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4738 begin
4739 if Covers (T1 => T1, T2 => T2)
4740 or else
4741 Covers (T1 => T2, T2 => T1)
4742 then
4743 if Is_Universal_Numeric_Type (T1)
4744 or else T1 = Any_Character
4745 then
4746 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4748 elsif T1 = T2 then
4749 Add_One_Interp (N, T1, T1);
4751 else
4752 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4753 end if;
4754 end if;
4755 end Check_Common_Type;
4757 ----------------------
4758 -- Check_High_Bound --
4759 ----------------------
4761 procedure Check_High_Bound (T : Entity_Id) is
4762 begin
4763 if not Is_Overloaded (H) then
4764 Check_Common_Type (T, Etype (H));
4765 else
4766 Get_First_Interp (H, I2, It2);
4767 while Present (It2.Typ) loop
4768 Check_Common_Type (T, It2.Typ);
4769 Get_Next_Interp (I2, It2);
4770 end loop;
4771 end if;
4772 end Check_High_Bound;
4774 --------------------------------
4775 -- Check_Universal_Expression --
4776 --------------------------------
4778 procedure Check_Universal_Expression (N : Node_Id) is
4779 begin
4780 if Etype (N) = Universal_Integer
4781 and then Nkind (N) /= N_Integer_Literal
4782 and then not Is_Entity_Name (N)
4783 and then Nkind (N) /= N_Attribute_Reference
4784 then
4785 Error_Msg_N ("illegal bound in discrete range", N);
4786 end if;
4787 end Check_Universal_Expression;
4789 -- Start of processing for Analyze_Range
4791 begin
4792 Set_Etype (N, Any_Type);
4793 Analyze_Expression (L);
4794 Analyze_Expression (H);
4796 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4797 return;
4799 else
4800 if not Is_Overloaded (L) then
4801 Check_High_Bound (Etype (L));
4802 else
4803 Get_First_Interp (L, I1, It1);
4804 while Present (It1.Typ) loop
4805 Check_High_Bound (It1.Typ);
4806 Get_Next_Interp (I1, It1);
4807 end loop;
4808 end if;
4810 -- If result is Any_Type, then we did not find a compatible pair
4812 if Etype (N) = Any_Type then
4813 Error_Msg_N ("incompatible types in range", N);
4814 end if;
4815 end if;
4817 if Ada_Version = Ada_83
4818 and then
4819 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4820 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4821 then
4822 Check_Universal_Expression (L);
4823 Check_Universal_Expression (H);
4824 end if;
4826 Check_Function_Writable_Actuals (N);
4827 end Analyze_Range;
4829 -----------------------
4830 -- Analyze_Reference --
4831 -----------------------
4833 procedure Analyze_Reference (N : Node_Id) is
4834 P : constant Node_Id := Prefix (N);
4835 E : Entity_Id;
4836 T : Entity_Id;
4837 Acc_Type : Entity_Id;
4839 begin
4840 Analyze (P);
4842 -- An interesting error check, if we take the 'Ref of an object for
4843 -- which a pragma Atomic or Volatile has been given, and the type of the
4844 -- object is not Atomic or Volatile, then we are in trouble. The problem
4845 -- is that no trace of the atomic/volatile status will remain for the
4846 -- backend to respect when it deals with the resulting pointer, since
4847 -- the pointer type will not be marked atomic (it is a pointer to the
4848 -- base type of the object).
4850 -- It is not clear if that can ever occur, but in case it does, we will
4851 -- generate an error message. Not clear if this message can ever be
4852 -- generated, and pretty clear that it represents a bug if it is, still
4853 -- seems worth checking, except in CodePeer mode where we do not really
4854 -- care and don't want to bother the user.
4856 T := Etype (P);
4858 if Is_Entity_Name (P)
4859 and then Is_Object_Reference (P)
4860 and then not CodePeer_Mode
4861 then
4862 E := Entity (P);
4863 T := Etype (P);
4865 if (Has_Atomic_Components (E)
4866 and then not Has_Atomic_Components (T))
4867 or else
4868 (Has_Volatile_Components (E)
4869 and then not Has_Volatile_Components (T))
4870 or else (Is_Atomic (E) and then not Is_Atomic (T))
4871 or else (Is_Volatile (E) and then not Is_Volatile (T))
4872 then
4873 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4874 end if;
4875 end if;
4877 -- Carry on with normal processing
4879 Acc_Type := Create_Itype (E_Allocator_Type, N);
4880 Set_Etype (Acc_Type, Acc_Type);
4881 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4882 Set_Etype (N, Acc_Type);
4883 end Analyze_Reference;
4885 --------------------------------
4886 -- Analyze_Selected_Component --
4887 --------------------------------
4889 -- Prefix is a record type or a task or protected type. In the latter case,
4890 -- the selector must denote a visible entry.
4892 procedure Analyze_Selected_Component (N : Node_Id) is
4893 Name : constant Node_Id := Prefix (N);
4894 Sel : constant Node_Id := Selector_Name (N);
4895 Act_Decl : Node_Id;
4896 Comp : Entity_Id := Empty;
4897 Has_Candidate : Boolean := False;
4898 Hidden_Comp : Entity_Id;
4899 In_Scope : Boolean;
4900 Is_Private_Op : Boolean;
4901 Parent_N : Node_Id;
4902 Prefix_Type : Entity_Id;
4904 Type_To_Use : Entity_Id;
4905 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4906 -- a class-wide type, we use its root type, whose components are
4907 -- present in the class-wide type.
4909 Is_Single_Concurrent_Object : Boolean;
4910 -- Set True if the prefix is a single task or a single protected object
4912 function Constraint_Has_Unprefixed_Discriminant_Reference
4913 (Typ : Entity_Id) return Boolean;
4914 -- Given a subtype that is subject to a discriminant-dependent
4915 -- constraint, returns True if any of the values of the constraint
4916 -- (i.e., any of the index values for an index constraint, any of
4917 -- the discriminant values for a discriminant constraint)
4918 -- are unprefixed discriminant names.
4920 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4921 -- It is known that the parent of N denotes a subprogram call. Comp
4922 -- is an overloadable component of the concurrent type of the prefix.
4923 -- Determine whether all formals of the parent of N and Comp are mode
4924 -- conformant. If the parent node is not analyzed yet it may be an
4925 -- indexed component rather than a function call.
4927 function Has_Dereference (Nod : Node_Id) return Boolean;
4928 -- Check whether prefix includes a dereference, explicit or implicit,
4929 -- at any recursive level.
4931 function Try_By_Protected_Procedure_Prefixed_View return Boolean;
4932 -- Return True if N is an access attribute whose prefix is a prefixed
4933 -- class-wide (synchronized or protected) interface view for which some
4934 -- interpretation is a procedure with synchronization kind By_Protected
4935 -- _Procedure, and collect all its interpretations (since it may be an
4936 -- overloaded interface primitive); otherwise return False.
4938 function Try_Selected_Component_In_Instance
4939 (Typ : Entity_Id) return Boolean;
4940 -- If Typ is the actual for a formal derived type, or a derived type
4941 -- thereof, the component inherited from the generic parent may not
4942 -- be visible in the actual, but the selected component is legal. Climb
4943 -- up the derivation chain of the generic parent type and return True if
4944 -- we find the proper ancestor type; otherwise return False.
4946 ------------------------------------------------------
4947 -- Constraint_Has_Unprefixed_Discriminant_Reference --
4948 ------------------------------------------------------
4950 function Constraint_Has_Unprefixed_Discriminant_Reference
4951 (Typ : Entity_Id) return Boolean
4953 function Is_Discriminant_Name (N : Node_Id) return Boolean is
4954 (Nkind (N) = N_Identifier
4955 and then Ekind (Entity (N)) = E_Discriminant);
4956 begin
4957 if Is_Array_Type (Typ) then
4958 declare
4959 Index : Node_Id := First_Index (Typ);
4960 Rng : Node_Id;
4961 begin
4962 while Present (Index) loop
4963 Rng := Index;
4964 if Nkind (Rng) = N_Subtype_Indication then
4965 Rng := Range_Expression (Constraint (Rng));
4966 end if;
4968 if Nkind (Rng) = N_Range then
4969 if Is_Discriminant_Name (Low_Bound (Rng))
4970 or else Is_Discriminant_Name (High_Bound (Rng))
4971 then
4972 return True;
4973 end if;
4974 end if;
4976 Next_Index (Index);
4977 end loop;
4978 end;
4979 else
4980 declare
4981 Elmt : Elmt_Id := First_Elmt (Discriminant_Constraint (Typ));
4982 begin
4983 while Present (Elmt) loop
4984 if Is_Discriminant_Name (Node (Elmt)) then
4985 return True;
4986 end if;
4987 Next_Elmt (Elmt);
4988 end loop;
4989 end;
4990 end if;
4992 return False;
4993 end Constraint_Has_Unprefixed_Discriminant_Reference;
4995 ------------------------------
4996 -- Has_Mode_Conformant_Spec --
4997 ------------------------------
4999 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
5000 Comp_Param : Entity_Id;
5001 Param : Node_Id;
5002 Param_Typ : Entity_Id;
5004 begin
5005 Comp_Param := First_Formal (Comp);
5007 if Nkind (Parent (N)) = N_Indexed_Component then
5008 Param := First (Expressions (Parent (N)));
5009 else
5010 Param := First (Parameter_Associations (Parent (N)));
5011 end if;
5013 while Present (Comp_Param)
5014 and then Present (Param)
5015 loop
5016 Param_Typ := Find_Parameter_Type (Param);
5018 if Present (Param_Typ)
5019 and then
5020 not Conforming_Types
5021 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
5022 then
5023 return False;
5024 end if;
5026 Next_Formal (Comp_Param);
5027 Next (Param);
5028 end loop;
5030 -- One of the specs has additional formals; there is no match, unless
5031 -- this may be an indexing of a parameterless call.
5033 -- Note that when expansion is disabled, the corresponding record
5034 -- type of synchronized types is not constructed, so that there is
5035 -- no point is attempting an interpretation as a prefixed call, as
5036 -- this is bound to fail because the primitive operations will not
5037 -- be properly located.
5039 if Present (Comp_Param) or else Present (Param) then
5040 if Needs_No_Actuals (Comp)
5041 and then Is_Array_Type (Etype (Comp))
5042 and then not Expander_Active
5043 then
5044 return True;
5045 else
5046 return False;
5047 end if;
5048 end if;
5050 return True;
5051 end Has_Mode_Conformant_Spec;
5053 ---------------------
5054 -- Has_Dereference --
5055 ---------------------
5057 function Has_Dereference (Nod : Node_Id) return Boolean is
5058 begin
5059 if Nkind (Nod) = N_Explicit_Dereference then
5060 return True;
5062 elsif Is_Access_Type (Etype (Nod)) then
5063 return True;
5065 elsif Nkind (Nod) in N_Indexed_Component | N_Selected_Component then
5066 return Has_Dereference (Prefix (Nod));
5068 else
5069 return False;
5070 end if;
5071 end Has_Dereference;
5073 ----------------------------------------------
5074 -- Try_By_Protected_Procedure_Prefixed_View --
5075 ----------------------------------------------
5077 function Try_By_Protected_Procedure_Prefixed_View return Boolean is
5078 Candidate : Node_Id := Empty;
5079 Elmt : Elmt_Id;
5080 Prim : Node_Id;
5082 begin
5083 if Nkind (Parent (N)) = N_Attribute_Reference
5084 and then Attribute_Name (Parent (N)) in
5085 Name_Access
5086 | Name_Unchecked_Access
5087 | Name_Unrestricted_Access
5088 and then Is_Class_Wide_Type (Prefix_Type)
5089 and then (Is_Synchronized_Interface (Prefix_Type)
5090 or else Is_Protected_Interface (Prefix_Type))
5091 then
5092 -- If we have not found yet any interpretation then mark this
5093 -- one as the first interpretation (cf. Add_One_Interp).
5095 if No (Etype (Sel)) then
5096 Set_Etype (Sel, Any_Type);
5097 end if;
5099 Elmt := First_Elmt (Primitive_Operations (Etype (Prefix_Type)));
5100 while Present (Elmt) loop
5101 Prim := Node (Elmt);
5103 if Chars (Prim) = Chars (Sel)
5104 and then Is_By_Protected_Procedure (Prim)
5105 then
5106 Candidate := New_Copy (Prim);
5108 -- Skip the controlling formal; required to check type
5109 -- conformance of the target access to protected type
5110 -- (see Conforming_Types).
5112 Set_First_Entity (Candidate,
5113 Next_Entity (First_Entity (Prim)));
5115 Add_One_Interp (Sel, Candidate, Etype (Prim));
5116 Set_Etype (N, Etype (Prim));
5117 end if;
5119 Next_Elmt (Elmt);
5120 end loop;
5121 end if;
5123 -- Propagate overloaded attribute
5125 if Present (Candidate) and then Is_Overloaded (Sel) then
5126 Set_Is_Overloaded (N);
5127 end if;
5129 return Present (Candidate);
5130 end Try_By_Protected_Procedure_Prefixed_View;
5132 ----------------------------------------
5133 -- Try_Selected_Component_In_Instance --
5134 ----------------------------------------
5136 function Try_Selected_Component_In_Instance
5137 (Typ : Entity_Id) return Boolean
5139 procedure Find_Component_In_Instance (Rec : Entity_Id);
5140 -- In an instance, a component of a private extension may not be
5141 -- visible while it was visible in the generic. Search candidate
5142 -- scope for a component with the proper identifier. If a match is
5143 -- found, the Etype of both N and Sel are set from this component,
5144 -- and the entity of Sel is set to reference this component. If no
5145 -- match is found, Entity (Sel) remains unset. For a derived type
5146 -- that is an actual of the instance, the desired component may be
5147 -- found in any ancestor.
5149 --------------------------------
5150 -- Find_Component_In_Instance --
5151 --------------------------------
5153 procedure Find_Component_In_Instance (Rec : Entity_Id) is
5154 Comp : Entity_Id;
5155 Typ : Entity_Id;
5157 begin
5158 Typ := Rec;
5159 while Present (Typ) loop
5160 Comp := First_Component (Typ);
5161 while Present (Comp) loop
5162 if Chars (Comp) = Chars (Sel) then
5163 Set_Entity_With_Checks (Sel, Comp);
5164 Set_Etype (Sel, Etype (Comp));
5165 Set_Etype (N, Etype (Comp));
5166 return;
5167 end if;
5169 Next_Component (Comp);
5170 end loop;
5172 -- If not found, the component may be declared in the parent
5173 -- type or its full view, if any.
5175 if Is_Derived_Type (Typ) then
5176 Typ := Etype (Typ);
5178 if Is_Private_Type (Typ) then
5179 Typ := Full_View (Typ);
5180 end if;
5182 else
5183 return;
5184 end if;
5185 end loop;
5187 -- If we fall through, no match, so no changes made
5189 return;
5190 end Find_Component_In_Instance;
5192 -- Local variables
5194 Par : Entity_Id;
5196 -- Start of processing for Try_Selected_Component_In_Instance
5198 begin
5199 pragma Assert (In_Instance and then Is_Tagged_Type (Typ));
5200 pragma Assert (Etype (N) = Any_Type);
5202 -- Climb up derivation chain to generic actual subtype
5204 Par := Typ;
5205 while not Is_Generic_Actual_Type (Par) loop
5206 if Ekind (Par) = E_Record_Type then
5207 Par := Parent_Subtype (Par);
5208 exit when No (Par);
5209 else
5210 exit when Par = Etype (Par);
5211 Par := Etype (Par);
5212 end if;
5213 end loop;
5215 -- If Par is a generic actual, look for component in ancestor types.
5216 -- Skip this if we have no Declaration_Node, as is the case for
5217 -- itypes.
5219 if Present (Par)
5220 and then Is_Generic_Actual_Type (Par)
5221 and then Present (Declaration_Node (Par))
5222 then
5223 Par := Generic_Parent_Type (Declaration_Node (Par));
5224 loop
5225 Find_Component_In_Instance (Par);
5226 exit when Present (Entity (Sel))
5227 or else Par = Etype (Par);
5228 Par := Etype (Par);
5229 end loop;
5231 -- Another special case: the type is an extension of a private
5232 -- type T, either is an actual in an instance or is immediately
5233 -- visible, and we are in the body of the instance, which means
5234 -- the generic body had a full view of the type declaration for
5235 -- T or some ancestor that defines the component in question.
5236 -- This happens because Is_Visible_Component returned False on
5237 -- this component, as T or the ancestor is still private since
5238 -- the Has_Private_View mechanism is bypassed because T or the
5239 -- ancestor is not directly referenced in the generic body.
5241 elsif Is_Derived_Type (Typ)
5242 and then (Used_As_Generic_Actual (Typ)
5243 or else Is_Immediately_Visible (Typ))
5244 and then In_Instance_Body
5245 then
5246 Find_Component_In_Instance (Parent_Subtype (Typ));
5247 end if;
5249 return Etype (N) /= Any_Type;
5250 end Try_Selected_Component_In_Instance;
5252 -- Start of processing for Analyze_Selected_Component
5254 begin
5255 Set_Etype (N, Any_Type);
5257 if Is_Overloaded (Name) then
5258 Analyze_Overloaded_Selected_Component (N);
5259 return;
5261 elsif Etype (Name) = Any_Type then
5262 Set_Entity (Sel, Any_Id);
5263 Set_Etype (Sel, Any_Type);
5264 return;
5266 else
5267 Prefix_Type := Etype (Name);
5268 end if;
5270 if Is_Access_Type (Prefix_Type) then
5272 -- A RACW object can never be used as prefix of a selected component
5273 -- since that means it is dereferenced without being a controlling
5274 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
5275 -- reporting an error, we must check whether this is actually a
5276 -- dispatching call in prefix form.
5278 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
5279 and then Comes_From_Source (N)
5280 then
5281 if Try_Object_Operation (N) then
5282 return;
5283 else
5284 Error_Msg_N
5285 ("invalid dereference of a remote access-to-class-wide value",
5287 end if;
5289 -- Normal case of selected component applied to access type
5291 else
5292 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5293 Prefix_Type := Implicitly_Designated_Type (Prefix_Type);
5294 end if;
5296 -- If we have an explicit dereference of a remote access-to-class-wide
5297 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
5298 -- have to check for the case of a prefix that is a controlling operand
5299 -- of a prefixed dispatching call, as the dereference is legal in that
5300 -- case. Normally this condition is checked in Validate_Remote_Access_
5301 -- To_Class_Wide_Type, but we have to defer the checking for selected
5302 -- component prefixes because of the prefixed dispatching call case.
5303 -- Note that implicit dereferences are checked for this just above.
5305 elsif Nkind (Name) = N_Explicit_Dereference
5306 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
5307 and then Comes_From_Source (N)
5308 then
5309 if Try_Object_Operation (N) then
5310 return;
5311 else
5312 Error_Msg_N
5313 ("invalid dereference of a remote access-to-class-wide value",
5315 end if;
5316 end if;
5318 -- (Ada 2005): if the prefix is the limited view of a type, and
5319 -- the context already includes the full view, use the full view
5320 -- in what follows, either to retrieve a component of to find
5321 -- a primitive operation. If the prefix is an explicit dereference,
5322 -- set the type of the prefix to reflect this transformation.
5323 -- If the nonlimited view is itself an incomplete type, get the
5324 -- full view if available.
5326 if From_Limited_With (Prefix_Type)
5327 and then Has_Non_Limited_View (Prefix_Type)
5328 then
5329 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
5331 if Nkind (N) = N_Explicit_Dereference then
5332 Set_Etype (Prefix (N), Prefix_Type);
5333 end if;
5334 end if;
5336 if Ekind (Prefix_Type) = E_Private_Subtype then
5337 Prefix_Type := Base_Type (Prefix_Type);
5338 end if;
5340 Type_To_Use := Prefix_Type;
5342 -- For class-wide types, use the entity list of the root type. This
5343 -- indirection is specially important for private extensions because
5344 -- only the root type get switched (not the class-wide type).
5346 if Is_Class_Wide_Type (Prefix_Type) then
5347 Type_To_Use := Root_Type (Prefix_Type);
5348 end if;
5350 -- If the prefix is a single concurrent object, use its name in error
5351 -- messages, rather than that of its anonymous type.
5353 Is_Single_Concurrent_Object :=
5354 Is_Concurrent_Type (Prefix_Type)
5355 and then Is_Internal_Name (Chars (Prefix_Type))
5356 and then not Is_Derived_Type (Prefix_Type)
5357 and then Is_Entity_Name (Name);
5359 -- Avoid initializing Comp if that initialization is not needed
5360 -- (and, more importantly, if the call to First_Entity could fail).
5362 if Has_Discriminants (Type_To_Use)
5363 or else Is_Record_Type (Type_To_Use)
5364 or else Is_Private_Type (Type_To_Use)
5365 or else Is_Concurrent_Type (Type_To_Use)
5366 then
5367 Comp := First_Entity (Type_To_Use);
5368 end if;
5370 -- If the selector has an original discriminant, the node appears in
5371 -- an instance. Replace the discriminant with the corresponding one
5372 -- in the current discriminated type. For nested generics, this must
5373 -- be done transitively, so note the new original discriminant.
5375 if Nkind (Sel) = N_Identifier
5376 and then In_Instance
5377 and then Present (Original_Discriminant (Sel))
5378 then
5379 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
5381 -- Mark entity before rewriting, for completeness and because
5382 -- subsequent semantic checks might examine the original node.
5384 Set_Entity (Sel, Comp);
5385 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
5386 Set_Original_Discriminant (Selector_Name (N), Comp);
5387 Set_Etype (N, Etype (Comp));
5388 Check_Implicit_Dereference (N, Etype (Comp));
5390 elsif Is_Record_Type (Prefix_Type) then
5392 -- Find a component with the given name. If the node is a prefixed
5393 -- call, do not examine components whose visibility may be
5394 -- accidental.
5396 while Present (Comp)
5397 and then not Is_Prefixed_Call (N)
5399 -- When the selector has been resolved to a function then we may be
5400 -- looking at a prefixed call which has been preanalyzed already as
5401 -- part of a class condition. In such cases it is possible for a
5402 -- derived type to declare a component which has the same name as
5403 -- a primitive used in a parent's class condition.
5405 -- Avoid seeing components as possible interpretations of the
5406 -- selected component when this is true.
5408 and then not (Inside_Class_Condition_Preanalysis
5409 and then Present (Entity (Sel))
5410 and then Ekind (Entity (Sel)) = E_Function)
5411 loop
5412 if Chars (Comp) = Chars (Sel)
5413 and then Is_Visible_Component (Comp, N)
5414 then
5415 Set_Entity_With_Checks (Sel, Comp);
5416 Set_Etype (Sel, Etype (Comp));
5418 if Ekind (Comp) = E_Discriminant then
5419 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
5420 Error_Msg_N
5421 ("cannot reference discriminant of unchecked union",
5422 Sel);
5423 end if;
5425 if Is_Generic_Type (Prefix_Type)
5426 or else
5427 Is_Generic_Type (Root_Type (Prefix_Type))
5428 then
5429 Set_Original_Discriminant (Sel, Comp);
5430 end if;
5431 end if;
5433 -- Resolve the prefix early otherwise it is not possible to
5434 -- build the actual subtype of the component: it may need
5435 -- to duplicate this prefix and duplication is only allowed
5436 -- on fully resolved expressions.
5438 Resolve (Name);
5440 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
5441 -- subtypes in a package specification.
5442 -- Example:
5444 -- limited with Pkg;
5445 -- package Pkg is
5446 -- type Acc_Inc is access Pkg.T;
5447 -- X : Acc_Inc;
5448 -- N : Natural := X.all.Comp; -- ERROR, limited view
5449 -- end Pkg; -- Comp is not visible
5451 if Nkind (Name) = N_Explicit_Dereference
5452 and then From_Limited_With (Etype (Prefix (Name)))
5453 and then not Is_Potentially_Use_Visible (Etype (Name))
5454 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
5455 N_Package_Specification
5456 then
5457 Error_Msg_NE
5458 ("premature usage of incomplete}", Prefix (Name),
5459 Etype (Prefix (Name)));
5460 end if;
5462 -- We never need an actual subtype for the case of a selection
5463 -- for a indexed component of a non-packed array, since in
5464 -- this case gigi generates all the checks and can find the
5465 -- necessary bounds information.
5467 -- We also do not need an actual subtype for the case of a
5468 -- first, last, length, or range attribute applied to a
5469 -- non-packed array, since gigi can again get the bounds in
5470 -- these cases (gigi cannot handle the packed case, since it
5471 -- has the bounds of the packed array type, not the original
5472 -- bounds of the type). However, if the prefix is itself a
5473 -- selected component, as in a.b.c (i), gigi may regard a.b.c
5474 -- as a dynamic-sized temporary, so we do generate an actual
5475 -- subtype for this case.
5477 Parent_N := Parent (N);
5479 if not Is_Packed (Etype (Comp))
5480 and then
5481 ((Nkind (Parent_N) = N_Indexed_Component
5482 and then Nkind (Name) /= N_Selected_Component)
5483 or else
5484 (Nkind (Parent_N) = N_Attribute_Reference
5485 and then
5486 Attribute_Name (Parent_N) in Name_First
5487 | Name_Last
5488 | Name_Length
5489 | Name_Range))
5490 then
5491 Set_Etype (N, Etype (Comp));
5493 -- If full analysis is not enabled, we do not generate an
5494 -- actual subtype, because in the absence of expansion
5495 -- reference to a formal of a protected type, for example,
5496 -- will not be properly transformed, and will lead to
5497 -- out-of-scope references in gigi.
5499 -- In all other cases, we currently build an actual subtype.
5500 -- It seems likely that many of these cases can be avoided,
5501 -- but right now, the front end makes direct references to the
5502 -- bounds (e.g. in generating a length check), and if we do
5503 -- not make an actual subtype, we end up getting a direct
5504 -- reference to a discriminant, which will not do.
5506 elsif Full_Analysis then
5507 Act_Decl :=
5508 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
5509 Insert_Action (N, Act_Decl);
5511 if No (Act_Decl) then
5512 Set_Etype (N, Etype (Comp));
5514 else
5515 -- If discriminants were present in the component
5516 -- declaration, they have been replaced by the
5517 -- actual values in the prefix object.
5519 declare
5520 Subt : constant Entity_Id :=
5521 Defining_Identifier (Act_Decl);
5522 begin
5523 Set_Etype (Subt, Base_Type (Etype (Comp)));
5524 Set_Etype (N, Subt);
5525 end;
5526 end if;
5528 -- If Etype (Comp) is an access type whose designated subtype
5529 -- is constrained by an unprefixed discriminant value,
5530 -- then ideally we would build a new subtype with an
5531 -- appropriately prefixed discriminant value and use that
5532 -- instead, as is done in Build_Actual_Subtype_Of_Component.
5533 -- That turns out to be difficult in this context (with
5534 -- Full_Analysis = False, we could be processing a selected
5535 -- component that occurs in a Postcondition pragma;
5536 -- PPC pragmas are odd because they can contain references
5537 -- to formal parameters that occur outside the subprogram).
5538 -- So instead we punt on building a new subtype and we
5539 -- use the base type instead. This might introduce
5540 -- correctness problems if N were the target of an
5541 -- assignment (because a required check might be omitted);
5542 -- fortunately, that's impossible because a reference to the
5543 -- current instance of a type does not denote a variable view
5544 -- when the reference occurs within an aspect_specification.
5545 -- GNAT's Precondition and Postcondition pragmas follow the
5546 -- same rules as a Pre or Post aspect_specification.
5548 elsif Has_Discriminant_Dependent_Constraint (Comp)
5549 and then Ekind (Etype (Comp)) = E_Access_Subtype
5550 and then Constraint_Has_Unprefixed_Discriminant_Reference
5551 (Designated_Type (Etype (Comp)))
5552 then
5553 Set_Etype (N, Base_Type (Etype (Comp)));
5555 -- If Full_Analysis not enabled, just set the Etype
5557 else
5558 Set_Etype (N, Etype (Comp));
5559 end if;
5561 Check_Implicit_Dereference (N, Etype (N));
5562 return;
5563 end if;
5565 -- If the prefix is a private extension, check only the visible
5566 -- components of the partial view. This must include the tag,
5567 -- which can appear in expanded code in a tag check.
5569 if Ekind (Type_To_Use) = E_Record_Type_With_Private
5570 and then Chars (Selector_Name (N)) /= Name_uTag
5571 then
5572 exit when Comp = Last_Entity (Type_To_Use);
5573 end if;
5575 Next_Entity (Comp);
5576 end loop;
5578 -- Ada 2005 (AI-252): The selected component can be interpreted as
5579 -- a prefixed view of a subprogram. Depending on the context, this is
5580 -- either a name that can appear in a renaming declaration, or part
5581 -- of an enclosing call given in prefix form.
5583 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
5584 -- selected component should resolve to a name.
5586 -- Extension feature: Also support calls with prefixed views for
5587 -- untagged record types.
5589 if Ada_Version >= Ada_2005
5590 and then (Is_Tagged_Type (Prefix_Type)
5591 or else Core_Extensions_Allowed)
5592 and then not Is_Concurrent_Type (Prefix_Type)
5593 then
5594 if Nkind (Parent (N)) = N_Generic_Association
5595 or else Nkind (Parent (N)) = N_Requeue_Statement
5596 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
5597 then
5598 if Find_Primitive_Operation (N) then
5599 return;
5600 end if;
5602 elsif Try_By_Protected_Procedure_Prefixed_View then
5603 return;
5605 -- If the prefix type is the actual for a formal derived type,
5606 -- or a derived type thereof, the component inherited from the
5607 -- generic parent may not be visible in the actual, but the
5608 -- selected component is legal. This case must be handled before
5609 -- trying the object.operation notation to avoid reporting
5610 -- spurious errors, but must be skipped when Is_Prefixed_Call has
5611 -- been set (because that means that this node was resolved to an
5612 -- Object.Operation call when the generic unit was analyzed).
5614 elsif In_Instance
5615 and then not Is_Prefixed_Call (N)
5616 and then Is_Tagged_Type (Prefix_Type)
5617 and then Try_Selected_Component_In_Instance (Type_To_Use)
5618 then
5619 return;
5621 elsif Try_Object_Operation (N) then
5622 return;
5623 end if;
5625 -- If the transformation fails, it will be necessary to redo the
5626 -- analysis with all errors enabled, to indicate candidate
5627 -- interpretations and reasons for each failure ???
5629 end if;
5631 elsif Is_Private_Type (Prefix_Type) then
5633 -- Allow access only to discriminants of the type. If the type has
5634 -- no full view, gigi uses the parent type for the components, so we
5635 -- do the same here.
5637 if No (Full_View (Prefix_Type)) then
5638 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
5639 Comp := First_Entity (Type_To_Use);
5640 end if;
5642 while Present (Comp) loop
5643 if Chars (Comp) = Chars (Sel) then
5644 if Ekind (Comp) = E_Discriminant then
5645 Set_Entity_With_Checks (Sel, Comp);
5646 Generate_Reference (Comp, Sel);
5648 Set_Etype (Sel, Etype (Comp));
5649 Set_Etype (N, Etype (Comp));
5650 Check_Implicit_Dereference (N, Etype (N));
5652 if Is_Generic_Type (Prefix_Type)
5653 or else Is_Generic_Type (Root_Type (Prefix_Type))
5654 then
5655 Set_Original_Discriminant (Sel, Comp);
5656 end if;
5658 -- Before declaring an error, check whether this is tagged
5659 -- private type and a call to a primitive operation.
5661 elsif Ada_Version >= Ada_2005
5662 and then Is_Tagged_Type (Prefix_Type)
5663 and then Try_Object_Operation (N)
5664 then
5665 return;
5667 else
5668 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5669 Error_Msg_NE ("invisible selector& for }", N, Sel);
5670 Set_Entity (Sel, Any_Id);
5671 Set_Etype (N, Any_Type);
5672 end if;
5674 return;
5675 end if;
5677 Next_Entity (Comp);
5678 end loop;
5680 -- Extension feature: Also support calls with prefixed views for
5681 -- untagged private types.
5683 if Core_Extensions_Allowed then
5684 if Try_Object_Operation (N) then
5685 return;
5686 end if;
5687 end if;
5689 elsif Is_Concurrent_Type (Prefix_Type) then
5691 -- Find visible operation with given name. For a protected type,
5692 -- the possible candidates are discriminants, entries or protected
5693 -- subprograms. For a task type, the set can only include entries or
5694 -- discriminants if the task type is not an enclosing scope. If it
5695 -- is an enclosing scope (e.g. in an inner task) then all entities
5696 -- are visible, but the prefix must denote the enclosing scope, i.e.
5697 -- can only be a direct name or an expanded name.
5699 Set_Etype (Sel, Any_Type);
5700 Hidden_Comp := Empty;
5701 In_Scope := In_Open_Scopes (Prefix_Type);
5702 Is_Private_Op := False;
5704 while Present (Comp) loop
5706 -- Do not examine private operations of the type if not within
5707 -- its scope.
5709 if Chars (Comp) = Chars (Sel) then
5710 if Is_Overloadable (Comp)
5711 and then (In_Scope
5712 or else Comp /= First_Private_Entity (Type_To_Use))
5713 then
5714 Add_One_Interp (Sel, Comp, Etype (Comp));
5715 if Comp = First_Private_Entity (Type_To_Use) then
5716 Is_Private_Op := True;
5717 end if;
5719 -- If the prefix is tagged, the correct interpretation may
5720 -- lie in the primitive or class-wide operations of the
5721 -- type. Perform a simple conformance check to determine
5722 -- whether Try_Object_Operation should be invoked even if
5723 -- a visible entity is found.
5725 if Is_Tagged_Type (Prefix_Type)
5726 and then Nkind (Parent (N)) in N_Function_Call
5727 | N_Indexed_Component
5728 | N_Procedure_Call_Statement
5729 and then Has_Mode_Conformant_Spec (Comp)
5730 then
5731 Has_Candidate := True;
5732 end if;
5734 -- Note: a selected component may not denote a component of a
5735 -- protected type (4.1.3(7)).
5737 elsif Ekind (Comp) in E_Discriminant | E_Entry_Family
5738 or else (In_Scope
5739 and then not Is_Protected_Type (Prefix_Type)
5740 and then Is_Entity_Name (Name))
5741 then
5742 Set_Entity_With_Checks (Sel, Comp);
5743 Generate_Reference (Comp, Sel);
5745 -- The selector is not overloadable, so we have a candidate
5746 -- interpretation.
5748 Has_Candidate := True;
5750 else
5751 if Ekind (Comp) = E_Component then
5752 Hidden_Comp := Comp;
5753 end if;
5755 goto Next_Comp;
5756 end if;
5758 Set_Etype (Sel, Etype (Comp));
5759 Set_Etype (N, Etype (Comp));
5761 if Ekind (Comp) = E_Discriminant then
5762 Set_Original_Discriminant (Sel, Comp);
5763 end if;
5764 end if;
5766 <<Next_Comp>>
5767 if Comp = First_Private_Entity (Type_To_Use) then
5768 if Etype (Sel) /= Any_Type then
5770 -- If the first private entity's name matches, then treat
5771 -- it as a private op: needed for the error check for
5772 -- illegal selection of private entities further below.
5774 if Chars (Comp) = Chars (Sel) then
5775 Is_Private_Op := True;
5776 end if;
5778 -- We have a candidate, so exit the loop
5780 exit;
5782 else
5783 -- Indicate that subsequent operations are private,
5784 -- for better error reporting.
5786 Is_Private_Op := True;
5787 end if;
5788 end if;
5790 -- Do not examine private operations if not within scope of
5791 -- the synchronized type.
5793 exit when not In_Scope
5794 and then
5795 Comp = First_Private_Entity (Base_Type (Prefix_Type));
5796 Next_Entity (Comp);
5797 end loop;
5799 -- If the scope is a current instance, the prefix cannot be an
5800 -- expression of the same type, unless the selector designates a
5801 -- public operation (otherwise that would represent an attempt to
5802 -- reach an internal entity of another synchronized object).
5804 -- This is legal if prefix is an access to such type and there is
5805 -- a dereference, or is a component with a dereferenced prefix.
5806 -- It is also legal if the prefix is a component of a task type,
5807 -- and the selector is one of the task operations.
5809 if In_Scope
5810 and then not Is_Entity_Name (Name)
5811 and then not Has_Dereference (Name)
5812 then
5813 if Is_Task_Type (Prefix_Type)
5814 and then Present (Entity (Sel))
5815 and then Is_Entry (Entity (Sel))
5816 then
5817 null;
5819 elsif Is_Protected_Type (Prefix_Type)
5820 and then Is_Overloadable (Entity (Sel))
5821 and then not Is_Private_Op
5822 then
5823 null;
5825 else
5826 Error_Msg_NE
5827 ("invalid reference to internal operation of some object of "
5828 & "type &", N, Type_To_Use);
5829 Set_Entity (Sel, Any_Id);
5830 Set_Etype (Sel, Any_Type);
5831 return;
5832 end if;
5834 -- Another special case: the prefix may denote an object of the type
5835 -- (but not a type) in which case this is an external call and the
5836 -- operation must be public.
5838 elsif In_Scope
5839 and then Is_Object_Reference (Original_Node (Prefix (N)))
5840 and then Comes_From_Source (N)
5841 and then Is_Private_Op
5842 then
5843 if Present (Hidden_Comp) then
5844 Error_Msg_NE
5845 ("invalid reference to private component of object of type "
5846 & "&", N, Type_To_Use);
5848 else
5849 Error_Msg_NE
5850 ("invalid reference to private operation of some object of "
5851 & "type &", N, Type_To_Use);
5852 end if;
5854 Set_Entity (Sel, Any_Id);
5855 Set_Etype (Sel, Any_Type);
5856 return;
5857 end if;
5859 -- If there is no visible entity with the given name or none of the
5860 -- visible entities are plausible interpretations, check whether
5861 -- there is some other primitive operation with that name.
5863 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
5864 if (Etype (N) = Any_Type
5865 or else not Has_Candidate)
5866 and then Try_Object_Operation (N)
5867 then
5868 return;
5870 -- If the context is not syntactically a procedure call, it
5871 -- may be a call to a primitive function declared outside of
5872 -- the synchronized type.
5874 -- If the context is a procedure call, there might still be
5875 -- an overloading between an entry and a primitive procedure
5876 -- declared outside of the synchronized type, called in prefix
5877 -- notation. This is harder to disambiguate because in one case
5878 -- the controlling formal is implicit ???
5880 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
5881 and then Nkind (Parent (N)) /= N_Indexed_Component
5882 and then Try_Object_Operation (N)
5883 then
5884 return;
5885 end if;
5887 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5888 -- entry or procedure of a tagged concurrent type we must check
5889 -- if there are class-wide subprograms covering the primitive. If
5890 -- true then Try_Object_Operation reports the error.
5892 if Has_Candidate
5893 and then Is_Concurrent_Type (Prefix_Type)
5894 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
5895 then
5896 -- Duplicate the call. This is required to avoid problems with
5897 -- the tree transformations performed by Try_Object_Operation.
5898 -- Set properly the parent of the copied call, because it is
5899 -- about to be reanalyzed.
5901 declare
5902 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5904 begin
5905 Set_Parent (Par, Parent (Parent (N)));
5907 if Try_Object_Operation
5908 (Sinfo.Nodes.Name (Par), CW_Test_Only => True)
5909 then
5910 return;
5911 end if;
5912 end;
5913 end if;
5914 end if;
5916 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
5918 -- Case of a prefix of a protected type: selector might denote
5919 -- an invisible private component.
5921 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5922 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5923 Next_Entity (Comp);
5924 end loop;
5926 if Present (Comp) then
5927 if Is_Single_Concurrent_Object then
5928 Error_Msg_Node_2 := Entity (Name);
5929 Error_Msg_NE ("invisible selector& for &", N, Sel);
5931 else
5932 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5933 Error_Msg_NE ("invisible selector& for }", N, Sel);
5934 end if;
5935 return;
5936 end if;
5937 end if;
5939 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5941 -- Extension feature: Also support calls with prefixed views for
5942 -- untagged types.
5944 elsif Core_Extensions_Allowed
5945 and then Try_Object_Operation (N)
5946 then
5947 return;
5949 else
5950 -- Invalid prefix
5952 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5953 end if;
5955 -- If N still has no type, the component is not defined in the prefix
5957 if Etype (N) = Any_Type then
5959 if Is_Single_Concurrent_Object then
5960 Error_Msg_Node_2 := Entity (Name);
5961 Error_Msg_NE ("no selector& for&", N, Sel);
5963 Check_Misspelled_Selector (Type_To_Use, Sel);
5965 -- If this is a derived formal type, the parent may have different
5966 -- visibility at this point. Try for an inherited component before
5967 -- reporting an error.
5969 elsif Is_Generic_Type (Prefix_Type)
5970 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
5971 and then Prefix_Type /= Etype (Prefix_Type)
5972 and then Is_Record_Type (Etype (Prefix_Type))
5973 then
5974 Set_Etype (Prefix (N), Etype (Prefix_Type));
5975 Analyze_Selected_Component (N);
5976 return;
5978 -- Similarly, if this is the actual for a formal derived type, or
5979 -- a derived type thereof, the component inherited from the generic
5980 -- parent may not be visible in the actual, but the selected
5981 -- component is legal.
5983 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5985 -- Climb up the derivation chain of the generic parent type until
5986 -- we find the proper ancestor type.
5988 if Try_Selected_Component_In_Instance (Type_To_Use) then
5989 return;
5991 -- The search above must have eventually succeeded, since the
5992 -- selected component was legal in the generic.
5994 else
5995 raise Program_Error;
5996 end if;
5998 -- Component not found, specialize error message when appropriate
6000 else
6001 if Ekind (Prefix_Type) = E_Record_Subtype then
6003 -- Check whether this is a component of the base type which
6004 -- is absent from a statically constrained subtype. This will
6005 -- raise constraint error at run time, but is not a compile-
6006 -- time error. When the selector is illegal for base type as
6007 -- well fall through and generate a compilation error anyway.
6009 Comp := First_Component (Base_Type (Prefix_Type));
6010 while Present (Comp) loop
6011 if Chars (Comp) = Chars (Sel)
6012 and then Is_Visible_Component (Comp, Sel)
6013 then
6014 Set_Entity_With_Checks (Sel, Comp);
6015 Generate_Reference (Comp, Sel);
6016 Set_Etype (Sel, Etype (Comp));
6017 Set_Etype (N, Etype (Comp));
6019 -- Emit appropriate message. The node will be replaced
6020 -- by an appropriate raise statement.
6022 -- Note that in SPARK mode, as with all calls to apply a
6023 -- compile time constraint error, this will be made into
6024 -- an error to simplify the processing of the formal
6025 -- verification backend.
6027 Apply_Compile_Time_Constraint_Error
6028 (N, "component not present in }??",
6029 CE_Discriminant_Check_Failed,
6030 Ent => Prefix_Type,
6031 Emit_Message =>
6032 SPARK_Mode = On or not In_Instance_Not_Visible);
6033 return;
6034 end if;
6036 Next_Component (Comp);
6037 end loop;
6039 end if;
6041 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
6042 Error_Msg_NE ("no selector& for}", N, Sel);
6044 -- Add information in the case of an incomplete prefix
6046 if Is_Incomplete_Type (Type_To_Use) then
6047 declare
6048 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
6050 begin
6051 if From_Limited_With (Scope (Type_To_Use)) then
6052 Error_Msg_NE
6053 ("\limited view of& has no components", N, Inc);
6055 else
6056 Error_Msg_NE
6057 ("\premature usage of incomplete type&", N, Inc);
6059 if Nkind (Parent (Inc)) =
6060 N_Incomplete_Type_Declaration
6061 then
6062 -- Record location of premature use in entity so that
6063 -- a continuation message is generated when the
6064 -- completion is seen.
6066 Set_Premature_Use (Parent (Inc), N);
6067 end if;
6068 end if;
6069 end;
6070 end if;
6072 Check_Misspelled_Selector (Type_To_Use, Sel);
6073 end if;
6075 Set_Entity (Sel, Any_Id);
6076 Set_Etype (Sel, Any_Type);
6077 end if;
6078 end Analyze_Selected_Component;
6080 ---------------------------
6081 -- Analyze_Short_Circuit --
6082 ---------------------------
6084 procedure Analyze_Short_Circuit (N : Node_Id) is
6085 L : constant Node_Id := Left_Opnd (N);
6086 R : constant Node_Id := Right_Opnd (N);
6087 Ind : Interp_Index;
6088 It : Interp;
6090 begin
6091 Set_Etype (N, Any_Type);
6092 Analyze_Expression (L);
6093 Analyze_Expression (R);
6095 if not Is_Overloaded (L) then
6096 if Root_Type (Etype (L)) = Standard_Boolean
6097 and then Has_Compatible_Type (R, Etype (L))
6098 then
6099 Add_One_Interp (N, Etype (L), Etype (L));
6100 end if;
6102 else
6103 Get_First_Interp (L, Ind, It);
6104 while Present (It.Typ) loop
6105 if Root_Type (It.Typ) = Standard_Boolean
6106 and then Has_Compatible_Type (R, It.Typ)
6107 then
6108 Add_One_Interp (N, It.Typ, It.Typ);
6109 end if;
6111 Get_Next_Interp (Ind, It);
6112 end loop;
6113 end if;
6115 -- Here we have failed to find an interpretation. Clearly we know that
6116 -- it is not the case that both operands can have an interpretation of
6117 -- Boolean, but this is by far the most likely intended interpretation.
6118 -- So we simply resolve both operands as Booleans, and at least one of
6119 -- these resolutions will generate an error message, and we do not need
6120 -- to give another error message on the short circuit operation itself.
6122 if Etype (N) = Any_Type then
6123 Resolve (L, Standard_Boolean);
6124 Resolve (R, Standard_Boolean);
6125 Set_Etype (N, Standard_Boolean);
6126 end if;
6128 if Style_Check then
6129 if Nkind (L) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
6130 then
6131 Check_Xtra_Parens_Precedence (L);
6132 end if;
6134 if Nkind (R) not in N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor
6135 then
6136 Check_Xtra_Parens_Precedence (R);
6137 end if;
6138 end if;
6139 end Analyze_Short_Circuit;
6141 -------------------
6142 -- Analyze_Slice --
6143 -------------------
6145 procedure Analyze_Slice (N : Node_Id) is
6146 D : constant Node_Id := Discrete_Range (N);
6147 P : constant Node_Id := Prefix (N);
6148 Array_Type : Entity_Id;
6149 Index_Type : Entity_Id;
6151 procedure Analyze_Overloaded_Slice;
6152 -- If the prefix is overloaded, select those interpretations that
6153 -- yield a one-dimensional array type.
6155 ------------------------------
6156 -- Analyze_Overloaded_Slice --
6157 ------------------------------
6159 procedure Analyze_Overloaded_Slice is
6160 I : Interp_Index;
6161 It : Interp;
6162 Typ : Entity_Id;
6164 begin
6165 Set_Etype (N, Any_Type);
6167 Get_First_Interp (P, I, It);
6168 while Present (It.Nam) loop
6169 Typ := It.Typ;
6171 if Is_Access_Type (Typ) then
6172 Typ := Designated_Type (Typ);
6173 Error_Msg_NW
6174 (Warn_On_Dereference, "?d?implicit dereference", N);
6175 end if;
6177 if Is_Array_Type (Typ)
6178 and then Number_Dimensions (Typ) = 1
6179 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
6180 then
6181 Add_One_Interp (N, Typ, Typ);
6182 end if;
6184 Get_Next_Interp (I, It);
6185 end loop;
6187 if Etype (N) = Any_Type then
6188 Error_Msg_N ("expect array type in prefix of slice", N);
6189 end if;
6190 end Analyze_Overloaded_Slice;
6192 -- Start of processing for Analyze_Slice
6194 begin
6195 Analyze (P);
6196 Analyze (D);
6198 if Is_Overloaded (P) then
6199 Analyze_Overloaded_Slice;
6201 else
6202 Array_Type := Etype (P);
6203 Set_Etype (N, Any_Type);
6205 if Is_Access_Type (Array_Type) then
6206 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
6207 Array_Type := Implicitly_Designated_Type (Array_Type);
6208 end if;
6210 if not Is_Array_Type (Array_Type) then
6211 Wrong_Type (P, Any_Array);
6213 elsif Number_Dimensions (Array_Type) > 1 then
6214 Error_Msg_N
6215 ("type is not one-dimensional array in slice prefix", N);
6217 else
6218 if Ekind (Array_Type) = E_String_Literal_Subtype then
6219 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
6220 else
6221 Index_Type := Etype (First_Index (Array_Type));
6222 end if;
6224 if not Has_Compatible_Type (D, Index_Type) then
6225 Wrong_Type (D, Index_Type);
6226 else
6227 Set_Etype (N, Array_Type);
6228 end if;
6229 end if;
6230 end if;
6231 end Analyze_Slice;
6233 -----------------------------
6234 -- Analyze_Type_Conversion --
6235 -----------------------------
6237 procedure Analyze_Type_Conversion (N : Node_Id) is
6238 Expr : constant Node_Id := Expression (N);
6239 Mark : constant Entity_Id := Subtype_Mark (N);
6241 Typ : Entity_Id;
6243 begin
6244 -- If Conversion_OK is set, then the Etype is already set, and the only
6245 -- processing required is to analyze the expression. This is used to
6246 -- construct certain "illegal" conversions which are not allowed by Ada
6247 -- semantics, but can be handled by Gigi, see Sinfo for further details.
6249 if Conversion_OK (N) then
6250 Analyze (Expr);
6251 return;
6252 end if;
6254 -- Otherwise full type analysis is required, as well as some semantic
6255 -- checks to make sure the argument of the conversion is appropriate.
6257 Find_Type (Mark);
6258 Typ := Entity (Mark);
6259 Set_Etype (N, Typ);
6261 Analyze_Expression (Expr);
6263 Check_Fully_Declared (Typ, N);
6264 Validate_Remote_Type_Type_Conversion (N);
6266 -- Only remaining step is validity checks on the argument. These
6267 -- are skipped if the conversion does not come from the source.
6269 if not Comes_From_Source (N) then
6270 return;
6272 -- If there was an error in a generic unit, no need to replicate the
6273 -- error message. Conversely, constant-folding in the generic may
6274 -- transform the argument of a conversion into a string literal, which
6275 -- is legal. Therefore the following tests are not performed in an
6276 -- instance. The same applies to an inlined body.
6278 elsif In_Instance or In_Inlined_Body then
6279 return;
6281 elsif Nkind (Expr) = N_Null then
6282 Error_Msg_N ("argument of conversion cannot be null", N);
6283 Error_Msg_N ("\use qualified expression instead", N);
6284 Set_Etype (N, Any_Type);
6286 elsif Nkind (Expr) = N_Aggregate then
6287 Error_Msg_N ("argument of conversion cannot be aggregate", N);
6288 Error_Msg_N ("\use qualified expression instead", N);
6290 elsif Nkind (Expr) = N_Allocator then
6291 Error_Msg_N ("argument of conversion cannot be allocator", N);
6292 Error_Msg_N ("\use qualified expression instead", N);
6294 elsif Nkind (Expr) = N_String_Literal then
6295 Error_Msg_N ("argument of conversion cannot be string literal", N);
6296 Error_Msg_N ("\use qualified expression instead", N);
6298 elsif Nkind (Expr) = N_Character_Literal then
6299 if Ada_Version = Ada_83 then
6300 Resolve (Expr, Typ);
6301 else
6302 Error_Msg_N
6303 ("argument of conversion cannot be character literal", N);
6304 Error_Msg_N ("\use qualified expression instead", N);
6305 end if;
6307 elsif Nkind (Expr) = N_Attribute_Reference
6308 and then Attribute_Name (Expr) in Name_Access
6309 | Name_Unchecked_Access
6310 | Name_Unrestricted_Access
6311 then
6312 Error_Msg_N
6313 ("argument of conversion cannot be access attribute", N);
6314 Error_Msg_N ("\use qualified expression instead", N);
6315 end if;
6317 -- A formal parameter of a specific tagged type whose related subprogram
6318 -- is subject to pragma Extensions_Visible with value "False" cannot
6319 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
6320 -- internally generated expressions.
6322 if Is_Class_Wide_Type (Typ)
6323 and then Comes_From_Source (Expr)
6324 and then Is_EVF_Expression (Expr)
6325 then
6326 Error_Msg_N
6327 ("formal parameter cannot be converted to class-wide type when "
6328 & "Extensions_Visible is False", Expr);
6329 end if;
6330 end Analyze_Type_Conversion;
6332 ----------------------
6333 -- Analyze_Unary_Op --
6334 ----------------------
6336 procedure Analyze_Unary_Op (N : Node_Id) is
6337 R : constant Node_Id := Right_Opnd (N);
6339 Op_Id : Entity_Id;
6341 begin
6342 Set_Etype (N, Any_Type);
6343 Candidate_Type := Empty;
6345 Analyze_Expression (R);
6347 -- If the entity is already set, the node is the instantiation of a
6348 -- generic node with a non-local reference, or was manufactured by a
6349 -- call to Make_Op_xxx. In either case the entity is known to be valid,
6350 -- and we do not need to collect interpretations, instead we just get
6351 -- the single possible interpretation.
6353 if Present (Entity (N)) then
6354 Op_Id := Entity (N);
6356 if Ekind (Op_Id) = E_Operator then
6357 Find_Unary_Types (R, Op_Id, N);
6358 else
6359 Add_One_Interp (N, Op_Id, Etype (Op_Id));
6360 end if;
6362 else
6363 Op_Id := Get_Name_Entity_Id (Chars (N));
6364 while Present (Op_Id) loop
6365 if Ekind (Op_Id) = E_Operator then
6366 if No (Next_Entity (First_Entity (Op_Id))) then
6367 Find_Unary_Types (R, Op_Id, N);
6368 end if;
6370 elsif Is_Overloadable (Op_Id) then
6371 Analyze_User_Defined_Unary_Op (N, Op_Id);
6372 end if;
6374 Op_Id := Homonym (Op_Id);
6375 end loop;
6376 end if;
6378 Operator_Check (N);
6379 end Analyze_Unary_Op;
6381 ----------------------------------
6382 -- Analyze_Unchecked_Expression --
6383 ----------------------------------
6385 procedure Analyze_Unchecked_Expression (N : Node_Id) is
6386 Expr : constant Node_Id := Expression (N);
6388 begin
6389 Analyze (Expr, Suppress => All_Checks);
6390 Set_Etype (N, Etype (Expr));
6391 Save_Interps (Expr, N);
6392 end Analyze_Unchecked_Expression;
6394 ---------------------------------------
6395 -- Analyze_Unchecked_Type_Conversion --
6396 ---------------------------------------
6398 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
6399 Expr : constant Node_Id := Expression (N);
6400 Mark : constant Entity_Id := Subtype_Mark (N);
6402 begin
6403 Find_Type (Mark);
6404 Set_Etype (N, Entity (Mark));
6405 Analyze_Expression (Expr);
6406 end Analyze_Unchecked_Type_Conversion;
6408 ------------------------------------
6409 -- Analyze_User_Defined_Binary_Op --
6410 ------------------------------------
6412 procedure Analyze_User_Defined_Binary_Op
6413 (N : Node_Id;
6414 Op_Id : Entity_Id) is
6415 begin
6416 declare
6417 F1 : constant Entity_Id := First_Formal (Op_Id);
6418 F2 : constant Entity_Id := Next_Formal (F1);
6420 begin
6421 -- Verify that Op_Id is a visible binary function. Note that since
6422 -- we know Op_Id is overloaded, potentially use visible means use
6423 -- visible for sure (RM 9.4(11)). Be prepared for previous errors.
6425 if Ekind (Op_Id) = E_Function
6426 and then Present (F2)
6427 and then (Is_Immediately_Visible (Op_Id)
6428 or else Is_Potentially_Use_Visible (Op_Id))
6429 and then (Has_Compatible_Type (Left_Opnd (N), Etype (F1))
6430 or else Etype (F1) = Any_Type)
6431 and then (Has_Compatible_Type (Right_Opnd (N), Etype (F2))
6432 or else Etype (F2) = Any_Type)
6433 then
6434 Add_One_Interp (N, Op_Id, Base_Type (Etype (Op_Id)));
6436 -- If the operands are overloaded, indicate that the current
6437 -- type is a viable candidate. This is redundant in most cases,
6438 -- but for equality and comparison operators where the context
6439 -- does not impose a type on the operands, setting the proper
6440 -- type is necessary to avoid subsequent ambiguities during
6441 -- resolution, when both user-defined and predefined operators
6442 -- may be candidates.
6444 if Is_Overloaded (Left_Opnd (N)) then
6445 Set_Etype (Left_Opnd (N), Etype (F1));
6446 end if;
6448 if Is_Overloaded (Right_Opnd (N)) then
6449 Set_Etype (Right_Opnd (N), Etype (F2));
6450 end if;
6452 if Debug_Flag_E then
6453 Write_Str ("user defined operator ");
6454 Write_Name (Chars (Op_Id));
6455 Write_Str (" on node ");
6456 Write_Int (Int (N));
6457 Write_Eol;
6458 end if;
6459 end if;
6460 end;
6461 end Analyze_User_Defined_Binary_Op;
6463 -----------------------------------
6464 -- Analyze_User_Defined_Unary_Op --
6465 -----------------------------------
6467 procedure Analyze_User_Defined_Unary_Op
6468 (N : Node_Id;
6469 Op_Id : Entity_Id)
6471 begin
6472 -- Only do analysis if the operator Comes_From_Source, since otherwise
6473 -- the operator was generated by the expander, and all such operators
6474 -- always refer to the operators in package Standard.
6476 if Comes_From_Source (N) then
6477 declare
6478 F : constant Entity_Id := First_Formal (Op_Id);
6480 begin
6481 -- Verify that Op_Id is a visible unary function. Note that since
6482 -- we know Op_Id is overloaded, potentially use visible means use
6483 -- visible for sure (RM 9.4(11)).
6485 if Ekind (Op_Id) = E_Function
6486 and then No (Next_Formal (F))
6487 and then (Is_Immediately_Visible (Op_Id)
6488 or else Is_Potentially_Use_Visible (Op_Id))
6489 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
6490 then
6491 Add_One_Interp (N, Op_Id, Etype (Op_Id));
6492 end if;
6493 end;
6494 end if;
6495 end Analyze_User_Defined_Unary_Op;
6497 ---------------------------
6498 -- Check_Arithmetic_Pair --
6499 ---------------------------
6501 procedure Check_Arithmetic_Pair
6502 (T1, T2 : Entity_Id;
6503 Op_Id : Entity_Id;
6504 N : Node_Id)
6506 Op_Name : constant Name_Id := Chars (Op_Id);
6508 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
6509 -- Check whether the fixed-point type Typ has a user-defined operator
6510 -- (multiplication or division) that should hide the corresponding
6511 -- predefined operator. Used to implement Ada 2005 AI-264, to make
6512 -- such operators more visible and therefore useful.
6514 -- If the name of the operation is an expanded name with prefix
6515 -- Standard, the predefined universal fixed operator is available,
6516 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
6518 ------------------
6519 -- Has_Fixed_Op --
6520 ------------------
6522 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
6523 Bas : constant Entity_Id := Base_Type (Typ);
6524 Ent : Entity_Id;
6525 F1 : Entity_Id;
6526 F2 : Entity_Id;
6528 begin
6529 -- If the universal_fixed operation is given explicitly the rule
6530 -- concerning primitive operations of the type do not apply.
6532 if Nkind (N) = N_Function_Call
6533 and then Nkind (Name (N)) = N_Expanded_Name
6534 and then Entity (Prefix (Name (N))) = Standard_Standard
6535 then
6536 return False;
6537 end if;
6539 -- The operation is treated as primitive if it is declared in the
6540 -- same scope as the type, and therefore on the same entity chain.
6542 Ent := Next_Entity (Typ);
6543 while Present (Ent) loop
6544 if Chars (Ent) = Chars (Op) then
6545 F1 := First_Formal (Ent);
6546 F2 := Next_Formal (F1);
6548 -- The operation counts as primitive if either operand or
6549 -- result are of the given base type, and both operands are
6550 -- fixed point types.
6552 if (Base_Type (Etype (F1)) = Bas
6553 and then Is_Fixed_Point_Type (Etype (F2)))
6555 or else
6556 (Base_Type (Etype (F2)) = Bas
6557 and then Is_Fixed_Point_Type (Etype (F1)))
6559 or else
6560 (Base_Type (Etype (Ent)) = Bas
6561 and then Is_Fixed_Point_Type (Etype (F1))
6562 and then Is_Fixed_Point_Type (Etype (F2)))
6563 then
6564 return True;
6565 end if;
6566 end if;
6568 Next_Entity (Ent);
6569 end loop;
6571 return False;
6572 end Has_Fixed_Op;
6574 -- Start of processing for Check_Arithmetic_Pair
6576 begin
6577 if Op_Name in Name_Op_Add | Name_Op_Subtract then
6578 if Is_Numeric_Type (T1)
6579 and then Is_Numeric_Type (T2)
6580 and then (Covers (T1 => T1, T2 => T2)
6581 or else
6582 Covers (T1 => T2, T2 => T1))
6583 then
6584 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6585 end if;
6587 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide then
6588 if Is_Fixed_Point_Type (T1)
6589 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
6590 then
6591 -- Add one interpretation with universal fixed result
6593 if not Has_Fixed_Op (T1, Op_Id)
6594 or else Nkind (Parent (N)) = N_Type_Conversion
6595 then
6596 Add_One_Interp (N, Op_Id, Universal_Fixed);
6597 end if;
6599 elsif Is_Fixed_Point_Type (T2)
6600 and then T1 = Universal_Real
6601 and then
6602 (not Has_Fixed_Op (T1, Op_Id)
6603 or else Nkind (Parent (N)) = N_Type_Conversion)
6604 then
6605 Add_One_Interp (N, Op_Id, Universal_Fixed);
6607 elsif Is_Numeric_Type (T1)
6608 and then Is_Numeric_Type (T2)
6609 and then (Covers (T1 => T1, T2 => T2)
6610 or else
6611 Covers (T1 => T2, T2 => T1))
6612 then
6613 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6615 elsif Is_Fixed_Point_Type (T1)
6616 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6617 or else T2 = Universal_Integer)
6618 then
6619 Add_One_Interp (N, Op_Id, T1);
6621 elsif T2 = Universal_Real
6622 and then Base_Type (T1) = Base_Type (Standard_Integer)
6623 and then Op_Name = Name_Op_Multiply
6624 then
6625 Add_One_Interp (N, Op_Id, Any_Fixed);
6627 elsif T1 = Universal_Real
6628 and then Base_Type (T2) = Base_Type (Standard_Integer)
6629 then
6630 Add_One_Interp (N, Op_Id, Any_Fixed);
6632 elsif Is_Fixed_Point_Type (T2)
6633 and then (Base_Type (T1) = Base_Type (Standard_Integer)
6634 or else T1 = Universal_Integer)
6635 and then Op_Name = Name_Op_Multiply
6636 then
6637 Add_One_Interp (N, Op_Id, T2);
6639 elsif T1 = Universal_Real and then T2 = Universal_Integer then
6640 Add_One_Interp (N, Op_Id, T1);
6642 elsif T2 = Universal_Real
6643 and then T1 = Universal_Integer
6644 and then Op_Name = Name_Op_Multiply
6645 then
6646 Add_One_Interp (N, Op_Id, T2);
6647 end if;
6649 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
6651 if Is_Integer_Type (T1)
6652 and then (Covers (T1 => T1, T2 => T2)
6653 or else
6654 Covers (T1 => T2, T2 => T1))
6655 then
6656 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
6657 end if;
6659 elsif Op_Name = Name_Op_Expon then
6660 if Is_Numeric_Type (T1)
6661 and then not Is_Fixed_Point_Type (T1)
6662 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6663 or else T2 = Universal_Integer)
6664 then
6665 Add_One_Interp (N, Op_Id, Base_Type (T1));
6666 end if;
6668 else pragma Assert (Nkind (N) in N_Op_Shift);
6670 -- If not one of the predefined operators, the node may be one
6671 -- of the intrinsic functions. Its kind is always specific, and
6672 -- we can use it directly, rather than the name of the operation.
6674 if Is_Integer_Type (T1)
6675 and then (Base_Type (T2) = Base_Type (Standard_Integer)
6676 or else T2 = Universal_Integer)
6677 then
6678 Add_One_Interp (N, Op_Id, Base_Type (T1));
6679 end if;
6680 end if;
6681 end Check_Arithmetic_Pair;
6683 -------------------------------
6684 -- Check_Misspelled_Selector --
6685 -------------------------------
6687 procedure Check_Misspelled_Selector
6688 (Prefix : Entity_Id;
6689 Sel : Node_Id)
6691 Max_Suggestions : constant := 2;
6692 Nr_Of_Suggestions : Natural := 0;
6694 Suggestion_1 : Entity_Id := Empty;
6695 Suggestion_2 : Entity_Id := Empty;
6697 Comp : Entity_Id;
6699 begin
6700 -- All the components of the prefix of selector Sel are matched against
6701 -- Sel and a count is maintained of possible misspellings. When at
6702 -- the end of the analysis there are one or two (not more) possible
6703 -- misspellings, these misspellings will be suggested as possible
6704 -- correction.
6706 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
6708 -- Concurrent types should be handled as well ???
6710 return;
6711 end if;
6713 Comp := First_Entity (Prefix);
6714 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
6715 if Is_Visible_Component (Comp, Sel) then
6716 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
6717 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
6719 case Nr_Of_Suggestions is
6720 when 1 => Suggestion_1 := Comp;
6721 when 2 => Suggestion_2 := Comp;
6722 when others => null;
6723 end case;
6724 end if;
6725 end if;
6727 Next_Entity (Comp);
6728 end loop;
6730 -- Report at most two suggestions
6732 if Nr_Of_Suggestions = 1 then
6733 Error_Msg_NE -- CODEFIX
6734 ("\possible misspelling of&", Sel, Suggestion_1);
6736 elsif Nr_Of_Suggestions = 2 then
6737 Error_Msg_Node_2 := Suggestion_2;
6738 Error_Msg_NE -- CODEFIX
6739 ("\possible misspelling of& or&", Sel, Suggestion_1);
6740 end if;
6741 end Check_Misspelled_Selector;
6743 -------------------
6744 -- Diagnose_Call --
6745 -------------------
6747 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
6748 Actual : Node_Id;
6749 X : Interp_Index;
6750 It : Interp;
6751 Err_Mode : Boolean;
6752 New_Nam : Node_Id;
6753 Num_Actuals : Natural;
6754 Num_Interps : Natural;
6755 Void_Interp_Seen : Boolean := False;
6757 Success : Boolean;
6758 pragma Warnings (Off, Boolean);
6760 begin
6761 Num_Actuals := 0;
6762 Actual := First_Actual (N);
6764 while Present (Actual) loop
6765 -- Ada 2005 (AI-50217): Post an error in case of premature
6766 -- usage of an entity from the limited view.
6768 if not Analyzed (Etype (Actual))
6769 and then From_Limited_With (Etype (Actual))
6770 and then Ada_Version >= Ada_2005
6771 then
6772 Error_Msg_Qual_Level := 1;
6773 Error_Msg_NE
6774 ("missing with_clause for scope of imported type&",
6775 Actual, Etype (Actual));
6776 Error_Msg_Qual_Level := 0;
6777 end if;
6779 Num_Actuals := Num_Actuals + 1;
6780 Next_Actual (Actual);
6781 end loop;
6783 -- Before listing the possible candidates, check whether this is
6784 -- a prefix of a selected component that has been rewritten as a
6785 -- parameterless function call because there is a callable candidate
6786 -- interpretation. If there is a hidden package in the list of homonyms
6787 -- of the function name (bad programming style in any case) suggest that
6788 -- this is the intended entity.
6790 if No (Parameter_Associations (N))
6791 and then Nkind (Parent (N)) = N_Selected_Component
6792 and then Nkind (Parent (Parent (N))) in N_Declaration
6793 and then Is_Overloaded (Nam)
6794 then
6795 declare
6796 Ent : Entity_Id;
6798 begin
6799 Ent := Current_Entity (Nam);
6800 while Present (Ent) loop
6801 if Ekind (Ent) = E_Package then
6802 Error_Msg_N
6803 ("no legal interpretations as function call,!", Nam);
6804 Error_Msg_NE ("\package& is not visible", N, Ent);
6806 Rewrite (Parent (N),
6807 New_Occurrence_Of (Any_Type, Sloc (N)));
6808 return;
6809 end if;
6811 Ent := Homonym (Ent);
6812 end loop;
6813 end;
6814 end if;
6816 -- If this is a call to an operation of a concurrent type, the failed
6817 -- interpretations have been removed from the name. Recover them now
6818 -- in order to provide full diagnostics.
6820 if Nkind (Parent (Nam)) = N_Selected_Component then
6821 Set_Entity (Nam, Empty);
6822 New_Nam := New_Copy_Tree (Parent (Nam));
6823 Set_Is_Overloaded (New_Nam, False);
6824 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6825 Set_Parent (New_Nam, Parent (Parent (Nam)));
6826 Analyze_Selected_Component (New_Nam);
6827 Get_First_Interp (Selector_Name (New_Nam), X, It);
6828 else
6829 Get_First_Interp (Nam, X, It);
6830 end if;
6832 -- If the number of actuals is 2, then remove interpretations involving
6833 -- a unary "+" operator as they might yield confusing errors downstream.
6835 if Num_Actuals = 2
6836 and then Nkind (Parent (Nam)) /= N_Selected_Component
6837 then
6838 Num_Interps := 0;
6840 while Present (It.Nam) loop
6841 if Ekind (It.Nam) = E_Operator
6842 and then Chars (It.Nam) = Name_Op_Add
6843 and then (No (First_Formal (It.Nam))
6844 or else No (Next_Formal (First_Formal (It.Nam))))
6845 then
6846 Remove_Interp (X);
6847 else
6848 Num_Interps := Num_Interps + 1;
6849 end if;
6851 Get_Next_Interp (X, It);
6852 end loop;
6854 if Num_Interps = 0 then
6855 Error_Msg_N ("!too many arguments in call to&", Nam);
6856 return;
6857 end if;
6859 Get_First_Interp (Nam, X, It);
6861 else
6862 Num_Interps := 2; -- at least
6863 end if;
6865 -- Analyze each candidate call again with full error reporting for each
6867 if Num_Interps > 1 then
6868 Error_Msg_N ("!no candidate interpretations match the actuals:", Nam);
6869 end if;
6871 Err_Mode := All_Errors_Mode;
6872 All_Errors_Mode := True;
6874 while Present (It.Nam) loop
6875 if Etype (It.Nam) = Standard_Void_Type then
6876 Void_Interp_Seen := True;
6877 end if;
6879 Analyze_One_Call (N, It.Nam, True, Success);
6880 Get_Next_Interp (X, It);
6881 end loop;
6883 if Nkind (N) = N_Function_Call then
6884 Get_First_Interp (Nam, X, It);
6886 if No (It.Typ)
6887 and then Ekind (Entity (Name (N))) = E_Function
6888 and then Present (Homonym (Entity (Name (N))))
6889 then
6890 -- A name may appear overloaded if it has a homonym, even if that
6891 -- homonym is non-overloadable, in which case the overload list is
6892 -- in fact empty. This specialized case deserves a special message
6893 -- if the homonym is a child package.
6895 declare
6896 Nam : constant Node_Id := Name (N);
6897 H : constant Entity_Id := Homonym (Entity (Nam));
6899 begin
6900 if Ekind (H) = E_Package and then Is_Child_Unit (H) then
6901 Error_Msg_Qual_Level := 2;
6902 Error_Msg_NE ("if an entity in package& is meant, ", Nam, H);
6903 Error_Msg_NE ("\use a fully qualified name", Nam, H);
6904 Error_Msg_Qual_Level := 0;
6905 end if;
6906 end;
6908 else
6909 while Present (It.Nam) loop
6910 if Ekind (It.Nam) in E_Function | E_Operator then
6911 return;
6912 else
6913 Get_Next_Interp (X, It);
6914 end if;
6915 end loop;
6917 -- If all interpretations are procedures, this deserves a more
6918 -- precise message. Ditto if this appears as the prefix of a
6919 -- selected component, which may be a lexical error.
6921 Error_Msg_N
6922 ("\context requires function call, found procedure name", Nam);
6924 if Nkind (Parent (N)) = N_Selected_Component
6925 and then N = Prefix (Parent (N))
6926 then
6927 Error_Msg_N -- CODEFIX
6928 ("\period should probably be semicolon", Parent (N));
6929 end if;
6930 end if;
6932 elsif Nkind (N) = N_Procedure_Call_Statement
6933 and then not Void_Interp_Seen
6934 then
6935 Error_Msg_N ("\function name found in procedure call", Nam);
6936 end if;
6938 All_Errors_Mode := Err_Mode;
6939 end Diagnose_Call;
6941 ---------------------------
6942 -- Find_Arithmetic_Types --
6943 ---------------------------
6945 procedure Find_Arithmetic_Types
6946 (L, R : Node_Id;
6947 Op_Id : Entity_Id;
6948 N : Node_Id)
6950 procedure Check_Right_Argument (T : Entity_Id);
6951 -- Check right operand of operator
6953 --------------------------
6954 -- Check_Right_Argument --
6955 --------------------------
6957 procedure Check_Right_Argument (T : Entity_Id) is
6958 I : Interp_Index;
6959 It : Interp;
6961 begin
6962 if not Is_Overloaded (R) then
6963 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6965 else
6966 Get_First_Interp (R, I, It);
6967 while Present (It.Typ) loop
6968 Check_Arithmetic_Pair (T, It.Typ, Op_Id, N);
6969 Get_Next_Interp (I, It);
6970 end loop;
6971 end if;
6972 end Check_Right_Argument;
6974 -- Local variables
6976 I : Interp_Index;
6977 It : Interp;
6979 -- Start of processing for Find_Arithmetic_Types
6981 begin
6982 if not Is_Overloaded (L) then
6983 Check_Right_Argument (Etype (L));
6985 else
6986 Get_First_Interp (L, I, It);
6987 while Present (It.Typ) loop
6988 Check_Right_Argument (It.Typ);
6989 Get_Next_Interp (I, It);
6990 end loop;
6991 end if;
6992 end Find_Arithmetic_Types;
6994 ------------------------
6995 -- Find_Boolean_Types --
6996 ------------------------
6998 procedure Find_Boolean_Types
6999 (L, R : Node_Id;
7000 Op_Id : Entity_Id;
7001 N : Node_Id)
7003 procedure Check_Boolean_Pair (T1, T2 : Entity_Id);
7004 -- Check operand pair of operator
7006 procedure Check_Right_Argument (T : Entity_Id);
7007 -- Check right operand of operator
7009 ------------------------
7010 -- Check_Boolean_Pair --
7011 ------------------------
7013 procedure Check_Boolean_Pair (T1, T2 : Entity_Id) is
7014 T : Entity_Id;
7016 begin
7017 if Valid_Boolean_Arg (T1)
7018 and then Valid_Boolean_Arg (T2)
7019 and then (Covers (T1 => T1, T2 => T2)
7020 or else Covers (T1 => T2, T2 => T1))
7021 then
7022 T := Specific_Type (T1, T2);
7024 if T = Universal_Integer then
7025 T := Any_Modular;
7026 end if;
7028 Add_One_Interp (N, Op_Id, T);
7029 end if;
7030 end Check_Boolean_Pair;
7032 --------------------------
7033 -- Check_Right_Argument --
7034 --------------------------
7036 procedure Check_Right_Argument (T : Entity_Id) is
7037 I : Interp_Index;
7038 It : Interp;
7040 begin
7041 -- Defend against previous error
7043 if Nkind (R) = N_Error then
7044 null;
7046 elsif not Is_Overloaded (R) then
7047 Check_Boolean_Pair (T, Etype (R));
7049 else
7050 Get_First_Interp (R, I, It);
7051 while Present (It.Typ) loop
7052 Check_Boolean_Pair (T, It.Typ);
7053 Get_Next_Interp (I, It);
7054 end loop;
7055 end if;
7056 end Check_Right_Argument;
7058 -- Local variables
7060 I : Interp_Index;
7061 It : Interp;
7063 -- Start of processing for Find_Boolean_Types
7065 begin
7066 if not Is_Overloaded (L) then
7067 Check_Right_Argument (Etype (L));
7069 else
7070 Get_First_Interp (L, I, It);
7071 while Present (It.Typ) loop
7072 Check_Right_Argument (It.Typ);
7073 Get_Next_Interp (I, It);
7074 end loop;
7075 end if;
7076 end Find_Boolean_Types;
7078 ------------------------------------
7079 -- Find_Comparison_Equality_Types --
7080 ------------------------------------
7082 -- The context of the operator plays no role in resolving the operands,
7083 -- so that if there is more than one interpretation of the operands that
7084 -- is compatible with the comparison or equality, then the operation is
7085 -- ambiguous, but this cannot be reported at this point because there is
7086 -- no guarantee that the operation will be resolved to this operator yet.
7088 procedure Find_Comparison_Equality_Types
7089 (L, R : Node_Id;
7090 Op_Id : Entity_Id;
7091 N : Node_Id)
7093 Op_Name : constant Name_Id := Chars (Op_Id);
7094 Op_Typ : Entity_Id renames Standard_Boolean;
7096 function Try_Left_Interp (T : Entity_Id) return Entity_Id;
7097 -- Try an interpretation of the left operand with type T. Return the
7098 -- type of the interpretation of the right operand making up a valid
7099 -- operand pair, or else Any_Type if the right operand is ambiguous,
7100 -- otherwise Empty if no such pair exists.
7102 function Is_Valid_Comparison_Type (T : Entity_Id) return Boolean;
7103 -- Return true if T is a valid comparison type
7105 function Is_Valid_Equality_Type
7106 (T : Entity_Id;
7107 Anon_Access : Boolean) return Boolean;
7108 -- Return true if T is a valid equality type
7110 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean;
7111 -- Return true if T1 and T2 constitute a valid pair of operand types for
7112 -- L and R respectively.
7114 ---------------------
7115 -- Try_Left_Interp --
7116 ---------------------
7118 function Try_Left_Interp (T : Entity_Id) return Entity_Id is
7119 I : Interp_Index;
7120 It : Interp;
7121 R_Typ : Entity_Id;
7122 Valid_I : Interp_Index;
7124 begin
7125 -- Defend against previous error
7127 if Nkind (R) = N_Error then
7128 null;
7130 -- Loop through the interpretations of the right operand
7132 elsif not Is_Overloaded (R) then
7133 if Is_Valid_Pair (T, Etype (R)) then
7134 return Etype (R);
7135 end if;
7137 else
7138 R_Typ := Empty;
7139 Valid_I := 0;
7141 Get_First_Interp (R, I, It);
7142 while Present (It.Typ) loop
7143 if Is_Valid_Pair (T, It.Typ) then
7144 -- If several interpretations are possible, disambiguate
7146 if Present (R_Typ)
7147 and then Base_Type (It.Typ) /= Base_Type (R_Typ)
7148 then
7149 It := Disambiguate (R, Valid_I, I, Any_Type);
7151 if It = No_Interp then
7152 R_Typ := Any_Type;
7153 exit;
7154 end if;
7156 else
7157 Valid_I := I;
7158 end if;
7160 R_Typ := It.Typ;
7161 end if;
7163 Get_Next_Interp (I, It);
7164 end loop;
7166 if Present (R_Typ) then
7167 return R_Typ;
7168 end if;
7169 end if;
7171 return Empty;
7172 end Try_Left_Interp;
7174 ------------------------------
7175 -- Is_Valid_Comparison_Type --
7176 ------------------------------
7178 function Is_Valid_Comparison_Type (T : Entity_Id) return Boolean is
7179 begin
7180 -- The operation must be performed in a context where the operators
7181 -- of the base type are visible.
7183 if Is_Visible_Operator (N, Base_Type (T)) then
7184 null;
7186 -- Save candidate type for subsequent error message, if any
7188 else
7189 if Valid_Comparison_Arg (T) then
7190 Candidate_Type := T;
7191 end if;
7193 return False;
7194 end if;
7196 -- Defer to the common implementation for the rest
7198 return Valid_Comparison_Arg (T);
7199 end Is_Valid_Comparison_Type;
7201 ----------------------------
7202 -- Is_Valid_Equality_Type --
7203 ----------------------------
7205 function Is_Valid_Equality_Type
7206 (T : Entity_Id;
7207 Anon_Access : Boolean) return Boolean
7209 begin
7210 -- The operation must be performed in a context where the operators
7211 -- of the base type are visible. Deal with special types used with
7212 -- access types before type resolution is done.
7214 if Ekind (T) = E_Access_Attribute_Type
7215 or else (Ekind (T) in E_Access_Subprogram_Type
7216 | E_Access_Protected_Subprogram_Type
7217 and then
7218 Ekind (Designated_Type (T)) /= E_Subprogram_Type)
7219 or else Is_Visible_Operator (N, Base_Type (T))
7220 then
7221 null;
7223 -- AI95-0230: Keep restriction imposed by Ada 83 and 95, do not allow
7224 -- anonymous access types in universal_access equality operators.
7226 elsif Anon_Access then
7227 if Ada_Version < Ada_2005 then
7228 return False;
7229 end if;
7231 -- Save candidate type for subsequent error message, if any
7233 else
7234 if Valid_Equality_Arg (T) then
7235 Candidate_Type := T;
7236 end if;
7238 return False;
7239 end if;
7241 -- For the use of a "/=" operator on a tagged type, several possible
7242 -- interpretations of equality need to be considered, we don't want
7243 -- the default inequality declared in Standard to be chosen, and the
7244 -- "/=" operator will be rewritten as a negation of "=" (see the end
7245 -- of Analyze_Comparison_Equality_Op). This ensures the rewriting
7246 -- occurs during analysis rather than being delayed until expansion.
7247 -- Note that, if the node is N_Op_Ne but Op_Id is Name_Op_Eq, then we
7248 -- still proceed with the interpretation, because this indicates
7249 -- the aforementioned rewriting case where the interpretation to be
7250 -- considered is actually that of the "=" operator.
7252 if Nkind (N) = N_Op_Ne
7253 and then Op_Name /= Name_Op_Eq
7254 and then Is_Tagged_Type (T)
7255 then
7256 return False;
7258 -- Defer to the common implementation for the rest
7260 else
7261 return Valid_Equality_Arg (T);
7262 end if;
7263 end Is_Valid_Equality_Type;
7265 -------------------
7266 -- Is_Valid_Pair --
7267 -------------------
7269 function Is_Valid_Pair (T1, T2 : Entity_Id) return Boolean is
7270 begin
7271 if Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne then
7272 declare
7273 Anon_Access : constant Boolean :=
7274 Is_Anonymous_Access_Type (T1)
7275 or else Is_Anonymous_Access_Type (T2);
7276 -- RM 4.5.2(9.1/2): At least one of the operands of an equality
7277 -- operator for universal_access shall be of specific anonymous
7278 -- access type.
7280 begin
7281 if not Is_Valid_Equality_Type (T1, Anon_Access)
7282 or else not Is_Valid_Equality_Type (T2, Anon_Access)
7283 then
7284 return False;
7285 end if;
7286 end;
7288 else
7289 if not Is_Valid_Comparison_Type (T1)
7290 or else not Is_Valid_Comparison_Type (T2)
7291 then
7292 return False;
7293 end if;
7294 end if;
7296 return Covers (T1 => T1, T2 => T2)
7297 or else Covers (T1 => T2, T2 => T1)
7298 or else Is_User_Defined_Literal (L, T2)
7299 or else Is_User_Defined_Literal (R, T1);
7300 end Is_Valid_Pair;
7302 -- Local variables
7304 I : Interp_Index;
7305 It : Interp;
7306 L_Typ : Entity_Id;
7307 R_Typ : Entity_Id;
7308 T : Entity_Id;
7309 Valid_I : Interp_Index;
7311 -- Start of processing for Find_Comparison_Equality_Types
7313 begin
7314 -- Loop through the interpretations of the left operand
7316 if not Is_Overloaded (L) then
7317 T := Try_Left_Interp (Etype (L));
7319 if Present (T) then
7320 Set_Etype (R, T);
7321 Add_One_Interp (N, Op_Id, Op_Typ, Find_Unique_Type (L, R));
7322 end if;
7324 else
7325 L_Typ := Empty;
7326 R_Typ := Empty;
7327 Valid_I := 0;
7329 Get_First_Interp (L, I, It);
7330 while Present (It.Typ) loop
7331 T := Try_Left_Interp (It.Typ);
7333 if Present (T) then
7334 -- If several interpretations are possible, disambiguate
7336 if Present (L_Typ)
7337 and then Base_Type (It.Typ) /= Base_Type (L_Typ)
7338 then
7339 It := Disambiguate (L, Valid_I, I, Any_Type);
7341 if It = No_Interp then
7342 L_Typ := Any_Type;
7343 R_Typ := T;
7344 exit;
7345 end if;
7347 else
7348 Valid_I := I;
7349 end if;
7351 L_Typ := It.Typ;
7352 R_Typ := T;
7353 end if;
7355 Get_Next_Interp (I, It);
7356 end loop;
7358 if Present (L_Typ) then
7359 Set_Etype (L, L_Typ);
7360 Set_Etype (R, R_Typ);
7361 Add_One_Interp (N, Op_Id, Op_Typ, Find_Unique_Type (L, R));
7362 end if;
7363 end if;
7364 end Find_Comparison_Equality_Types;
7366 ------------------------------
7367 -- Find_Concatenation_Types --
7368 ------------------------------
7370 procedure Find_Concatenation_Types
7371 (L, R : Node_Id;
7372 Op_Id : Entity_Id;
7373 N : Node_Id)
7375 Is_String : constant Boolean := Nkind (L) = N_String_Literal
7376 or else
7377 Nkind (R) = N_String_Literal;
7378 Op_Type : constant Entity_Id := Etype (Op_Id);
7380 begin
7381 if Is_Array_Type (Op_Type)
7383 -- Small but very effective optimization: if at least one operand is a
7384 -- string literal, then the type of the operator must be either array
7385 -- of characters or array of strings.
7387 and then (not Is_String
7388 or else
7389 Is_Character_Type (Component_Type (Op_Type))
7390 or else
7391 Is_String_Type (Component_Type (Op_Type)))
7393 and then not Is_Limited_Type (Op_Type)
7395 and then (Has_Compatible_Type (L, Op_Type)
7396 or else
7397 Has_Compatible_Type (L, Component_Type (Op_Type)))
7399 and then (Has_Compatible_Type (R, Op_Type)
7400 or else
7401 Has_Compatible_Type (R, Component_Type (Op_Type)))
7402 then
7403 Add_One_Interp (N, Op_Id, Op_Type);
7404 end if;
7405 end Find_Concatenation_Types;
7407 -------------------------
7408 -- Find_Negation_Types --
7409 -------------------------
7411 procedure Find_Negation_Types
7412 (R : Node_Id;
7413 Op_Id : Entity_Id;
7414 N : Node_Id)
7416 Index : Interp_Index;
7417 It : Interp;
7419 begin
7420 if not Is_Overloaded (R) then
7421 if Etype (R) = Universal_Integer then
7422 Add_One_Interp (N, Op_Id, Any_Modular);
7423 elsif Valid_Boolean_Arg (Etype (R)) then
7424 Add_One_Interp (N, Op_Id, Etype (R));
7425 end if;
7427 else
7428 Get_First_Interp (R, Index, It);
7429 while Present (It.Typ) loop
7430 if Valid_Boolean_Arg (It.Typ) then
7431 Add_One_Interp (N, Op_Id, It.Typ);
7432 end if;
7434 Get_Next_Interp (Index, It);
7435 end loop;
7436 end if;
7437 end Find_Negation_Types;
7439 ------------------------------
7440 -- Find_Primitive_Operation --
7441 ------------------------------
7443 function Find_Primitive_Operation (N : Node_Id) return Boolean is
7444 Obj : constant Node_Id := Prefix (N);
7445 Op : constant Node_Id := Selector_Name (N);
7447 Prim : Elmt_Id;
7448 Prims : Elist_Id;
7449 Typ : Entity_Id;
7451 begin
7452 Set_Etype (Op, Any_Type);
7454 if Is_Access_Type (Etype (Obj)) then
7455 Typ := Designated_Type (Etype (Obj));
7456 else
7457 Typ := Etype (Obj);
7458 end if;
7460 if Is_Class_Wide_Type (Typ) then
7461 Typ := Root_Type (Typ);
7462 end if;
7464 Prims := Primitive_Operations (Typ);
7466 Prim := First_Elmt (Prims);
7467 while Present (Prim) loop
7468 if Chars (Node (Prim)) = Chars (Op) then
7469 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
7470 Set_Etype (N, Etype (Node (Prim)));
7471 end if;
7473 Next_Elmt (Prim);
7474 end loop;
7476 -- Now look for class-wide operations of the type or any of its
7477 -- ancestors by iterating over the homonyms of the selector.
7479 declare
7480 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
7481 Hom : Entity_Id;
7483 begin
7484 Hom := Current_Entity (Op);
7485 while Present (Hom) loop
7486 if (Ekind (Hom) = E_Procedure
7487 or else
7488 Ekind (Hom) = E_Function)
7489 and then Scope (Hom) = Scope (Typ)
7490 and then Present (First_Formal (Hom))
7491 and then
7492 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7493 or else
7494 (Is_Access_Type (Etype (First_Formal (Hom)))
7495 and then
7496 Ekind (Etype (First_Formal (Hom))) =
7497 E_Anonymous_Access_Type
7498 and then
7499 Base_Type
7500 (Designated_Type (Etype (First_Formal (Hom)))) =
7501 Cls_Type))
7502 then
7503 Add_One_Interp (Op, Hom, Etype (Hom));
7504 Set_Etype (N, Etype (Hom));
7505 end if;
7507 Hom := Homonym (Hom);
7508 end loop;
7509 end;
7511 return Etype (Op) /= Any_Type;
7512 end Find_Primitive_Operation;
7514 ----------------------
7515 -- Find_Unary_Types --
7516 ----------------------
7518 procedure Find_Unary_Types
7519 (R : Node_Id;
7520 Op_Id : Entity_Id;
7521 N : Node_Id)
7523 Index : Interp_Index;
7524 It : Interp;
7526 begin
7527 if not Is_Overloaded (R) then
7528 if Is_Numeric_Type (Etype (R)) then
7530 -- In an instance a generic actual may be a numeric type even if
7531 -- the formal in the generic unit was not. In that case, the
7532 -- predefined operator was not a possible interpretation in the
7533 -- generic, and cannot be one in the instance, unless the operator
7534 -- is an actual of an instance.
7536 if In_Instance
7537 and then
7538 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
7539 then
7540 null;
7541 else
7542 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
7543 end if;
7544 end if;
7546 else
7547 Get_First_Interp (R, Index, It);
7548 while Present (It.Typ) loop
7549 if Is_Numeric_Type (It.Typ) then
7550 if In_Instance
7551 and then
7552 not Is_Numeric_Type
7553 (Corresponding_Generic_Type (Etype (It.Typ)))
7554 then
7555 null;
7557 else
7558 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
7559 end if;
7560 end if;
7562 Get_Next_Interp (Index, It);
7563 end loop;
7564 end if;
7565 end Find_Unary_Types;
7567 ------------------
7568 -- Junk_Operand --
7569 ------------------
7571 function Junk_Operand (N : Node_Id) return Boolean is
7572 Enode : Node_Id;
7574 begin
7575 if Error_Posted (N) then
7576 return False;
7577 end if;
7579 -- Get entity to be tested
7581 if Is_Entity_Name (N)
7582 and then Present (Entity (N))
7583 then
7584 Enode := N;
7586 -- An odd case, a procedure name gets converted to a very peculiar
7587 -- function call, and here is where we detect this happening.
7589 elsif Nkind (N) = N_Function_Call
7590 and then Is_Entity_Name (Name (N))
7591 and then Present (Entity (Name (N)))
7592 then
7593 Enode := Name (N);
7595 -- Another odd case, there are at least some cases of selected
7596 -- components where the selected component is not marked as having
7597 -- an entity, even though the selector does have an entity
7599 elsif Nkind (N) = N_Selected_Component
7600 and then Present (Entity (Selector_Name (N)))
7601 then
7602 Enode := Selector_Name (N);
7604 else
7605 return False;
7606 end if;
7608 -- Now test the entity we got to see if it is a bad case
7610 case Ekind (Entity (Enode)) is
7611 when E_Package =>
7612 Error_Msg_N
7613 ("package name cannot be used as operand", Enode);
7615 when Generic_Unit_Kind =>
7616 Error_Msg_N
7617 ("generic unit name cannot be used as operand", Enode);
7619 when Type_Kind =>
7620 Error_Msg_N
7621 ("subtype name cannot be used as operand", Enode);
7623 when Entry_Kind =>
7624 Error_Msg_N
7625 ("entry name cannot be used as operand", Enode);
7627 when E_Procedure =>
7628 Error_Msg_N
7629 ("procedure name cannot be used as operand", Enode);
7631 when E_Exception =>
7632 Error_Msg_N
7633 ("exception name cannot be used as operand", Enode);
7635 when E_Block
7636 | E_Label
7637 | E_Loop
7639 Error_Msg_N
7640 ("label name cannot be used as operand", Enode);
7642 when others =>
7643 return False;
7644 end case;
7646 return True;
7647 end Junk_Operand;
7649 --------------------
7650 -- Operator_Check --
7651 --------------------
7653 procedure Operator_Check (N : Node_Id) is
7654 begin
7655 Remove_Abstract_Operations (N);
7657 -- Test for case of no interpretation found for operator
7659 if Etype (N) = Any_Type then
7660 declare
7661 L : constant Node_Id :=
7662 (if Nkind (N) in N_Binary_Op then Left_Opnd (N) else Empty);
7663 R : constant Node_Id := Right_Opnd (N);
7665 begin
7666 -- If either operand has no type, then don't complain further,
7667 -- since this simply means that we have a propagated error.
7669 if R = Error
7670 or else Etype (R) = Any_Type
7671 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
7672 then
7673 -- For the rather unusual case where one of the operands is
7674 -- a Raise_Expression, whose initial type is Any_Type, use
7675 -- the type of the other operand.
7677 if Nkind (L) = N_Raise_Expression then
7678 Set_Etype (L, Etype (R));
7679 Set_Etype (N, Etype (R));
7681 elsif Nkind (R) = N_Raise_Expression then
7682 Set_Etype (R, Etype (L));
7683 Set_Etype (N, Etype (L));
7684 end if;
7686 return;
7688 -- We explicitly check for the case of concatenation of component
7689 -- with component to avoid reporting spurious matching array types
7690 -- that might happen to be lurking in distant packages (such as
7691 -- run-time packages). This also prevents inconsistencies in the
7692 -- messages for certain ACVC B tests, which can vary depending on
7693 -- types declared in run-time interfaces. Another improvement when
7694 -- aggregates are present is to look for a well-typed operand.
7696 elsif Present (Candidate_Type)
7697 and then (Nkind (N) /= N_Op_Concat
7698 or else Is_Array_Type (Etype (L))
7699 or else Is_Array_Type (Etype (R)))
7700 then
7701 if Nkind (N) = N_Op_Concat then
7702 if Etype (L) /= Any_Composite
7703 and then Is_Array_Type (Etype (L))
7704 then
7705 Candidate_Type := Etype (L);
7707 elsif Etype (R) /= Any_Composite
7708 and then Is_Array_Type (Etype (R))
7709 then
7710 Candidate_Type := Etype (R);
7711 end if;
7712 end if;
7714 Error_Msg_NE -- CODEFIX
7715 ("operator for} is not directly visible!",
7716 N, First_Subtype (Candidate_Type));
7718 declare
7719 U : constant Node_Id :=
7720 Cunit (Get_Source_Unit (Candidate_Type));
7721 begin
7722 if Unit_Is_Visible (U) then
7723 Error_Msg_N -- CODEFIX
7724 ("use clause would make operation legal!", N);
7725 else
7726 Error_Msg_NE -- CODEFIX
7727 ("add with_clause and use_clause for&!",
7728 N, Defining_Entity (Unit (U)));
7729 end if;
7730 end;
7731 return;
7733 -- If either operand is a junk operand (e.g. package name), then
7734 -- post appropriate error messages, but do not complain further.
7736 -- Note that the use of OR in this test instead of OR ELSE is
7737 -- quite deliberate, we may as well check both operands in the
7738 -- binary operator case.
7740 elsif Junk_Operand (R)
7741 or -- really mean OR here and not OR ELSE, see above
7742 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
7743 then
7744 return;
7746 -- The handling of user-defined literals is deferred to the second
7747 -- pass of resolution.
7749 elsif Has_Possible_User_Defined_Literal (N) then
7750 return;
7752 -- If we have a logical operator, one of whose operands is
7753 -- Boolean, then we know that the other operand cannot resolve to
7754 -- Boolean (since we got no interpretations), but in that case we
7755 -- pretty much know that the other operand should be Boolean, so
7756 -- resolve it that way (generating an error).
7758 elsif Nkind (N) in N_Op_And | N_Op_Or | N_Op_Xor then
7759 if Etype (L) = Standard_Boolean then
7760 Resolve (R, Standard_Boolean);
7761 return;
7762 elsif Etype (R) = Standard_Boolean then
7763 Resolve (L, Standard_Boolean);
7764 return;
7765 end if;
7767 -- For an arithmetic operator or comparison operator, if one
7768 -- of the operands is numeric, then we know the other operand
7769 -- is not the same numeric type. If it is a non-numeric type,
7770 -- then probably it is intended to match the other operand.
7772 elsif Nkind (N) in N_Op_Add
7773 | N_Op_Divide
7774 | N_Op_Ge
7775 | N_Op_Gt
7776 | N_Op_Le
7777 | N_Op_Lt
7778 | N_Op_Mod
7779 | N_Op_Multiply
7780 | N_Op_Rem
7781 | N_Op_Subtract
7782 then
7783 -- If Allow_Integer_Address is active, check whether the
7784 -- operation becomes legal after converting an operand.
7786 if Is_Numeric_Type (Etype (L))
7787 and then not Is_Numeric_Type (Etype (R))
7788 then
7789 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7790 Rewrite (L,
7791 Unchecked_Convert_To (
7792 Standard_Address, Relocate_Node (L)));
7793 Rewrite (R,
7794 Unchecked_Convert_To (
7795 Standard_Address, Relocate_Node (R)));
7797 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7798 Analyze_Comparison_Equality_Op (N);
7799 else
7800 Analyze_Arithmetic_Op (N);
7801 end if;
7802 else
7803 Resolve (R, Etype (L));
7804 end if;
7806 return;
7808 elsif Is_Numeric_Type (Etype (R))
7809 and then not Is_Numeric_Type (Etype (L))
7810 then
7811 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
7812 Rewrite (L,
7813 Unchecked_Convert_To (
7814 Standard_Address, Relocate_Node (L)));
7815 Rewrite (R,
7816 Unchecked_Convert_To (
7817 Standard_Address, Relocate_Node (R)));
7819 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7820 Analyze_Comparison_Equality_Op (N);
7821 else
7822 Analyze_Arithmetic_Op (N);
7823 end if;
7825 return;
7827 else
7828 Resolve (L, Etype (R));
7829 end if;
7831 return;
7833 elsif Allow_Integer_Address
7834 and then Is_Descendant_Of_Address (Etype (L))
7835 and then Is_Descendant_Of_Address (Etype (R))
7836 and then not Error_Posted (N)
7837 then
7838 declare
7839 Addr_Type : constant Entity_Id := Etype (L);
7841 begin
7842 Rewrite (L,
7843 Unchecked_Convert_To (
7844 Standard_Address, Relocate_Node (L)));
7845 Rewrite (R,
7846 Unchecked_Convert_To (
7847 Standard_Address, Relocate_Node (R)));
7849 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7850 Analyze_Comparison_Equality_Op (N);
7851 else
7852 Analyze_Arithmetic_Op (N);
7853 end if;
7855 -- If this is an operand in an enclosing arithmetic
7856 -- operation, Convert the result as an address so that
7857 -- arithmetic folding of address can continue.
7859 if Nkind (Parent (N)) in N_Op then
7860 Rewrite (N,
7861 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7862 end if;
7864 return;
7865 end;
7867 -- Under relaxed RM semantics silently replace occurrences of
7868 -- null by System.Address_Null.
7870 elsif Null_To_Null_Address_Convert_OK (N) then
7871 Replace_Null_By_Null_Address (N);
7873 if Nkind (N) in N_Op_Ge | N_Op_Gt | N_Op_Le | N_Op_Lt then
7874 Analyze_Comparison_Equality_Op (N);
7875 else
7876 Analyze_Arithmetic_Op (N);
7877 end if;
7879 return;
7880 end if;
7882 -- Comparisons on A'Access are common enough to deserve a
7883 -- special message.
7885 elsif Nkind (N) in N_Op_Eq | N_Op_Ne
7886 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7887 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7888 then
7889 Error_Msg_N
7890 ("two access attributes cannot be compared directly", N);
7891 Error_Msg_N
7892 ("\use qualified expression for one of the operands",
7894 return;
7896 -- Another one for C programmers
7898 elsif Nkind (N) = N_Op_Concat
7899 and then Valid_Boolean_Arg (Etype (L))
7900 and then Valid_Boolean_Arg (Etype (R))
7901 then
7902 Error_Msg_N ("invalid operands for concatenation", N);
7903 Error_Msg_N -- CODEFIX
7904 ("\maybe AND was meant", N);
7905 return;
7907 -- A special case for comparison of access parameter with null
7909 elsif Nkind (N) = N_Op_Eq
7910 and then Is_Entity_Name (L)
7911 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7912 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7913 N_Access_Definition
7914 and then Nkind (R) = N_Null
7915 then
7916 Error_Msg_N ("access parameter is not allowed to be null", L);
7917 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7918 return;
7920 -- Another special case for exponentiation, where the right
7921 -- operand must be Natural, independently of the base.
7923 elsif Nkind (N) = N_Op_Expon
7924 and then Is_Numeric_Type (Etype (L))
7925 and then not Is_Overloaded (R)
7926 and then
7927 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7928 and then Base_Type (Etype (R)) /= Universal_Integer
7929 then
7930 if Ada_Version >= Ada_2012
7931 and then Has_Dimension_System (Etype (L))
7932 then
7933 Error_Msg_NE
7934 ("exponent for dimensioned type must be a rational" &
7935 ", found}", R, Etype (R));
7936 else
7937 Error_Msg_NE
7938 ("exponent must be of type Natural, found}", R, Etype (R));
7939 end if;
7941 return;
7943 elsif Nkind (N) in N_Op_Eq | N_Op_Ne then
7944 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7945 Rewrite (L,
7946 Unchecked_Convert_To (
7947 Standard_Address, Relocate_Node (L)));
7948 Rewrite (R,
7949 Unchecked_Convert_To (
7950 Standard_Address, Relocate_Node (R)));
7951 Analyze_Comparison_Equality_Op (N);
7952 return;
7954 -- Under relaxed RM semantics silently replace occurrences of
7955 -- null by System.Address_Null.
7957 elsif Null_To_Null_Address_Convert_OK (N) then
7958 Replace_Null_By_Null_Address (N);
7959 Analyze_Comparison_Equality_Op (N);
7960 return;
7961 end if;
7962 end if;
7964 -- If we fall through then just give general message
7966 Unresolved_Operator (N);
7967 end;
7968 end if;
7969 end Operator_Check;
7971 ---------------------------------------
7972 -- Has_Possible_User_Defined_Literal --
7973 ---------------------------------------
7975 function Has_Possible_User_Defined_Literal (N : Node_Id) return Boolean is
7976 R : constant Node_Id := Right_Opnd (N);
7978 procedure Check_Literal_Opnd (Opnd : Node_Id);
7979 -- If an operand is a literal to which an aspect may apply,
7980 -- add the corresponding type to operator node.
7982 ------------------------
7983 -- Check_Literal_Opnd --
7984 ------------------------
7986 procedure Check_Literal_Opnd (Opnd : Node_Id) is
7987 begin
7988 if Nkind (Opnd) in N_Numeric_Or_String_Literal
7989 or else (Is_Entity_Name (Opnd)
7990 and then Present (Entity (Opnd))
7991 and then Is_Named_Number (Entity (Opnd)))
7992 then
7993 Add_One_Interp (N, Etype (Opnd), Etype (Opnd));
7994 end if;
7995 end Check_Literal_Opnd;
7997 -- Start of processing for Has_Possible_User_Defined_Literal
7999 begin
8000 if Ada_Version < Ada_2022 then
8001 return False;
8002 end if;
8004 Check_Literal_Opnd (R);
8006 -- Check left operand only if right one did not provide a
8007 -- possible interpretation. Note that literal types are not
8008 -- overloadable, in the sense that there is no overloadable
8009 -- entity name whose several interpretations can be used to
8010 -- indicate possible resulting types, so there is no way to
8011 -- provide more than one interpretation to the operator node.
8012 -- The choice of one operand over the other is arbitrary at
8013 -- this point, and may lead to spurious resolution when both
8014 -- operands are literals of different kinds, but the second
8015 -- pass of resolution will examine anew both operands to
8016 -- determine whether a user-defined literal may apply to
8017 -- either or both.
8019 if Nkind (N) in N_Binary_Op and then Etype (N) = Any_Type then
8020 Check_Literal_Opnd (Left_Opnd (N));
8021 end if;
8023 return Etype (N) /= Any_Type;
8024 end Has_Possible_User_Defined_Literal;
8026 -----------------------------------------------
8027 -- Nondispatching_Call_To_Abstract_Operation --
8028 -----------------------------------------------
8030 procedure Nondispatching_Call_To_Abstract_Operation
8031 (N : Node_Id;
8032 Abstract_Op : Entity_Id)
8034 Typ : constant Entity_Id := Etype (N);
8036 begin
8037 -- In an instance body, this is a runtime check, but one we know will
8038 -- fail, so give an appropriate warning. As usual this kind of warning
8039 -- is an error in SPARK mode.
8041 Error_Msg_Sloc := Sloc (Abstract_Op);
8043 if In_Instance_Body and then SPARK_Mode /= On then
8044 Error_Msg_NE
8045 ("??cannot call abstract operation& declared#",
8046 N, Abstract_Op);
8047 Error_Msg_N ("\Program_Error [??", N);
8048 Rewrite (N,
8049 Make_Raise_Program_Error (Sloc (N),
8050 Reason => PE_Explicit_Raise));
8051 Analyze (N);
8052 Set_Etype (N, Typ);
8054 else
8055 Error_Msg_NE
8056 ("cannot call abstract operation& declared#",
8057 N, Abstract_Op);
8058 Set_Etype (N, Any_Type);
8059 end if;
8060 end Nondispatching_Call_To_Abstract_Operation;
8062 ----------------------------------------------
8063 -- Possible_Type_For_Conditional_Expression --
8064 ----------------------------------------------
8066 function Possible_Type_For_Conditional_Expression
8067 (T1, T2 : Entity_Id) return Entity_Id
8069 function Is_Access_Protected_Subprogram_Attribute
8070 (T : Entity_Id) return Boolean;
8071 -- Return true if T is the type of an access-to-protected-subprogram
8072 -- attribute.
8074 function Is_Access_Subprogram_Attribute (T : Entity_Id) return Boolean;
8075 -- Return true if T is the type of an access-to-subprogram attribute
8077 ----------------------------------------------
8078 -- Is_Access_Protected_Subprogram_Attribute --
8079 ----------------------------------------------
8081 function Is_Access_Protected_Subprogram_Attribute
8082 (T : Entity_Id) return Boolean
8084 begin
8085 return Ekind (T) = E_Access_Protected_Subprogram_Type
8086 and then Ekind (Designated_Type (T)) /= E_Subprogram_Type;
8087 end Is_Access_Protected_Subprogram_Attribute;
8089 ------------------------------------
8090 -- Is_Access_Subprogram_Attribute --
8091 ------------------------------------
8093 function Is_Access_Subprogram_Attribute (T : Entity_Id) return Boolean is
8094 begin
8095 return Ekind (T) = E_Access_Subprogram_Type
8096 and then Ekind (Designated_Type (T)) /= E_Subprogram_Type;
8097 end Is_Access_Subprogram_Attribute;
8099 -- Start of processing for Possible_Type_For_Conditional_Expression
8101 begin
8102 -- If both types are those of similar access attributes or allocators,
8103 -- pick one of them, for example the first.
8105 if Ekind (T1) in E_Access_Attribute_Type | E_Allocator_Type
8106 and then Ekind (T2) in E_Access_Attribute_Type | E_Allocator_Type
8107 then
8108 return T1;
8110 elsif Is_Access_Subprogram_Attribute (T1)
8111 and then Is_Access_Subprogram_Attribute (T2)
8112 and then
8113 Subtype_Conformant (Designated_Type (T1), Designated_Type (T2))
8114 then
8115 return T1;
8117 elsif Is_Access_Protected_Subprogram_Attribute (T1)
8118 and then Is_Access_Protected_Subprogram_Attribute (T2)
8119 and then
8120 Subtype_Conformant (Designated_Type (T1), Designated_Type (T2))
8121 then
8122 return T1;
8124 -- The other case to be considered is a pair of tagged types
8126 elsif Is_Tagged_Type (T1) and then Is_Tagged_Type (T2) then
8127 -- Covers performs the same checks when T1 or T2 are a CW type, so
8128 -- we don't need to do them again here.
8130 if not Is_Class_Wide_Type (T1) and then Is_Ancestor (T1, T2) then
8131 return T1;
8133 elsif not Is_Class_Wide_Type (T2) and then Is_Ancestor (T2, T1) then
8134 return T2;
8136 -- Neither type is an ancestor of the other, but they may have one in
8137 -- common, so we pick the first type as above. We could perform here
8138 -- the computation of the nearest common ancestors of T1 and T2, but
8139 -- this would require a significant amount of work and the practical
8140 -- benefit would very likely be negligible.
8142 else
8143 return T1;
8144 end if;
8146 -- Otherwise no type is possible
8148 else
8149 return Empty;
8150 end if;
8151 end Possible_Type_For_Conditional_Expression;
8153 --------------------------------
8154 -- Remove_Abstract_Operations --
8155 --------------------------------
8157 procedure Remove_Abstract_Operations (N : Node_Id) is
8158 Abstract_Op : Entity_Id := Empty;
8159 Address_Descendant : Boolean := False;
8160 I : Interp_Index;
8161 It : Interp;
8163 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
8164 -- activate this if either extensions are enabled, or if the abstract
8165 -- operation in question comes from a predefined file. This latter test
8166 -- allows us to use abstract to make operations invisible to users. In
8167 -- particular, if type Address is non-private and abstract subprograms
8168 -- are used to hide its operators, they will be truly hidden.
8170 type Operand_Position is (First_Op, Second_Op);
8171 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
8173 procedure Remove_Address_Interpretations (Op : Operand_Position);
8174 -- Ambiguities may arise when the operands are literal and the address
8175 -- operations in s-auxdec are visible. In that case, remove the
8176 -- interpretation of a literal as Address, to retain the semantics
8177 -- of Address as a private type.
8179 ------------------------------------
8180 -- Remove_Address_Interpretations --
8181 ------------------------------------
8183 procedure Remove_Address_Interpretations (Op : Operand_Position) is
8184 Formal : Entity_Id;
8186 begin
8187 if Is_Overloaded (N) then
8188 Get_First_Interp (N, I, It);
8189 while Present (It.Nam) loop
8190 Formal := First_Entity (It.Nam);
8192 if Op = Second_Op then
8193 Next_Entity (Formal);
8194 end if;
8196 if Is_Descendant_Of_Address (Etype (Formal)) then
8197 Address_Descendant := True;
8198 Remove_Interp (I);
8199 end if;
8201 Get_Next_Interp (I, It);
8202 end loop;
8203 end if;
8204 end Remove_Address_Interpretations;
8206 -- Start of processing for Remove_Abstract_Operations
8208 begin
8209 if Is_Overloaded (N) then
8210 if Debug_Flag_V then
8211 Write_Line ("Remove_Abstract_Operations: ");
8212 Write_Overloads (N);
8213 end if;
8215 Get_First_Interp (N, I, It);
8217 while Present (It.Nam) loop
8218 if Is_Overloadable (It.Nam)
8219 and then Is_Abstract_Subprogram (It.Nam)
8220 and then not Is_Dispatching_Operation (It.Nam)
8221 then
8222 Abstract_Op := It.Nam;
8224 if Is_Descendant_Of_Address (It.Typ) then
8225 Address_Descendant := True;
8226 Remove_Interp (I);
8227 exit;
8229 -- In Ada 2005, this operation does not participate in overload
8230 -- resolution. If the operation is defined in a predefined
8231 -- unit, it is one of the operations declared abstract in some
8232 -- variants of System, and it must be removed as well.
8234 elsif Ada_Version >= Ada_2005
8235 or else In_Predefined_Unit (It.Nam)
8236 then
8237 Remove_Interp (I);
8238 exit;
8239 end if;
8240 end if;
8242 Get_Next_Interp (I, It);
8243 end loop;
8245 if No (Abstract_Op) then
8247 -- If some interpretation yields an integer type, it is still
8248 -- possible that there are address interpretations. Remove them
8249 -- if one operand is a literal, to avoid spurious ambiguities
8250 -- on systems where Address is a visible integer type.
8252 if Is_Overloaded (N)
8253 and then Nkind (N) in N_Op
8254 and then Is_Integer_Type (Etype (N))
8255 then
8256 if Nkind (N) in N_Binary_Op then
8257 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
8258 Remove_Address_Interpretations (Second_Op);
8260 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
8261 Remove_Address_Interpretations (First_Op);
8262 end if;
8263 end if;
8264 end if;
8266 elsif Nkind (N) in N_Op then
8268 -- Remove interpretations that treat literals as addresses. This
8269 -- is never appropriate, even when Address is defined as a visible
8270 -- Integer type. The reason is that we would really prefer Address
8271 -- to behave as a private type, even in this case. If Address is a
8272 -- visible integer type, we get lots of overload ambiguities.
8274 if Nkind (N) in N_Binary_Op then
8275 declare
8276 U1 : constant Boolean :=
8277 Present (Universal_Interpretation (Right_Opnd (N)));
8278 U2 : constant Boolean :=
8279 Present (Universal_Interpretation (Left_Opnd (N)));
8281 begin
8282 if U1 then
8283 Remove_Address_Interpretations (Second_Op);
8284 end if;
8286 if U2 then
8287 Remove_Address_Interpretations (First_Op);
8288 end if;
8290 if not (U1 and U2) then
8292 -- Remove corresponding predefined operator, which is
8293 -- always added to the overload set.
8295 Get_First_Interp (N, I, It);
8296 while Present (It.Nam) loop
8297 if Scope (It.Nam) = Standard_Standard
8298 and then Base_Type (It.Typ) =
8299 Base_Type (Etype (Abstract_Op))
8300 then
8301 Remove_Interp (I);
8302 end if;
8304 Get_Next_Interp (I, It);
8305 end loop;
8307 elsif Is_Overloaded (N)
8308 and then Present (Univ_Type)
8309 then
8310 -- If both operands have a universal interpretation,
8311 -- it is still necessary to remove interpretations that
8312 -- yield Address. Any remaining ambiguities will be
8313 -- removed in Disambiguate.
8315 Get_First_Interp (N, I, It);
8316 while Present (It.Nam) loop
8317 if Is_Descendant_Of_Address (It.Typ) then
8318 Remove_Interp (I);
8320 elsif not Is_Type (It.Nam) then
8321 Set_Entity (N, It.Nam);
8322 end if;
8324 Get_Next_Interp (I, It);
8325 end loop;
8326 end if;
8327 end;
8328 end if;
8330 elsif Nkind (N) = N_Function_Call
8331 and then
8332 (Nkind (Name (N)) = N_Operator_Symbol
8333 or else
8334 (Nkind (Name (N)) = N_Expanded_Name
8335 and then
8336 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
8337 then
8339 declare
8340 Arg1 : constant Node_Id := First (Parameter_Associations (N));
8341 U1 : constant Boolean :=
8342 Present (Universal_Interpretation (Arg1));
8343 U2 : constant Boolean :=
8344 Present (Next (Arg1)) and then
8345 Present (Universal_Interpretation (Next (Arg1)));
8347 begin
8348 if U1 then
8349 Remove_Address_Interpretations (First_Op);
8350 end if;
8352 if U2 then
8353 Remove_Address_Interpretations (Second_Op);
8354 end if;
8356 if not (U1 and U2) then
8357 Get_First_Interp (N, I, It);
8358 while Present (It.Nam) loop
8359 if Scope (It.Nam) = Standard_Standard
8360 and then It.Typ = Base_Type (Etype (Abstract_Op))
8361 then
8362 Remove_Interp (I);
8363 end if;
8365 Get_Next_Interp (I, It);
8366 end loop;
8367 end if;
8368 end;
8369 end if;
8371 -- If the removal has left no valid interpretations, emit an error
8372 -- message now and label node as illegal.
8374 if Present (Abstract_Op) then
8375 Get_First_Interp (N, I, It);
8377 if No (It.Nam) then
8379 -- Removal of abstract operation left no viable candidate
8381 Nondispatching_Call_To_Abstract_Operation (N, Abstract_Op);
8383 -- In Ada 2005, an abstract operation may disable predefined
8384 -- operators. Since the context is not yet known, we mark the
8385 -- predefined operators as potentially hidden. Do not include
8386 -- predefined operators when addresses are involved since this
8387 -- case is handled separately.
8389 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
8390 while Present (It.Nam) loop
8391 if Is_Numeric_Type (It.Typ)
8392 and then Scope (It.Typ) = Standard_Standard
8393 and then Ekind (It.Nam) = E_Operator
8394 then
8395 Set_Abstract_Op (I, Abstract_Op);
8396 end if;
8398 Get_Next_Interp (I, It);
8399 end loop;
8400 end if;
8401 end if;
8403 if Debug_Flag_V then
8404 Write_Line ("Remove_Abstract_Operations done: ");
8405 Write_Overloads (N);
8406 end if;
8407 end if;
8408 end Remove_Abstract_Operations;
8410 ----------------------------
8411 -- Try_Container_Indexing --
8412 ----------------------------
8414 function Try_Container_Indexing
8415 (N : Node_Id;
8416 Prefix : Node_Id;
8417 Exprs : List_Id) return Boolean
8419 Pref_Typ : Entity_Id := Etype (Prefix);
8421 function Constant_Indexing_OK return Boolean;
8422 -- Constant_Indexing is legal if there is no Variable_Indexing defined
8423 -- for the type, or else node not a target of assignment, or an actual
8424 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
8426 function Expr_Matches_In_Formal
8427 (Subp : Entity_Id;
8428 Par : Node_Id) return Boolean;
8429 -- Find formal corresponding to given indexed component that is an
8430 -- actual in a call. Note that the enclosing subprogram call has not
8431 -- been analyzed yet, and the parameter list is not normalized, so
8432 -- that if the argument is a parameter association we must match it
8433 -- by name and not by position.
8435 function Find_Indexing_Operations
8436 (T : Entity_Id;
8437 Nam : Name_Id;
8438 Is_Constant : Boolean) return Node_Id;
8439 -- Return a reference to the primitive operation of type T denoted by
8440 -- name Nam. If the operation is overloaded, the reference carries all
8441 -- interpretations. Flag Is_Constant should be set when the context is
8442 -- constant indexing.
8444 --------------------------
8445 -- Constant_Indexing_OK --
8446 --------------------------
8448 function Constant_Indexing_OK return Boolean is
8449 Par : Node_Id;
8451 begin
8452 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
8453 return True;
8455 elsif not Is_Variable (Prefix) then
8456 return True;
8457 end if;
8459 Par := N;
8460 while Present (Par) loop
8461 if Nkind (Parent (Par)) = N_Assignment_Statement
8462 and then Par = Name (Parent (Par))
8463 then
8464 return False;
8466 -- The call may be overloaded, in which case we assume that its
8467 -- resolution does not depend on the type of the parameter that
8468 -- includes the indexing operation.
8470 elsif Nkind (Parent (Par)) in N_Subprogram_Call
8471 and then Is_Entity_Name (Name (Parent (Par)))
8472 then
8473 declare
8474 Proc : Entity_Id;
8476 begin
8477 -- We should look for an interpretation with the proper
8478 -- number of formals, and determine whether it is an
8479 -- In_Parameter, but for now we examine the formal that
8480 -- corresponds to the indexing, and assume that variable
8481 -- indexing is required if some interpretation has an
8482 -- assignable formal at that position. Still does not
8483 -- cover the most complex cases ???
8485 if Is_Overloaded (Name (Parent (Par))) then
8486 declare
8487 Proc : constant Node_Id := Name (Parent (Par));
8488 I : Interp_Index;
8489 It : Interp;
8491 begin
8492 Get_First_Interp (Proc, I, It);
8493 while Present (It.Nam) loop
8494 if not Expr_Matches_In_Formal (It.Nam, Par) then
8495 return False;
8496 end if;
8498 Get_Next_Interp (I, It);
8499 end loop;
8500 end;
8502 -- All interpretations have a matching in-mode formal
8504 return True;
8506 else
8507 Proc := Entity (Name (Parent (Par)));
8509 -- If this is an indirect call, get formals from
8510 -- designated type.
8512 if Is_Access_Subprogram_Type (Etype (Proc)) then
8513 Proc := Designated_Type (Etype (Proc));
8514 end if;
8515 end if;
8517 return Expr_Matches_In_Formal (Proc, Par);
8518 end;
8520 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
8521 return False;
8523 -- If the indexed component is a prefix it may be the first actual
8524 -- of a prefixed call. Retrieve the called entity, if any, and
8525 -- check its first formal. Determine if the context is a procedure
8526 -- or function call.
8528 elsif Nkind (Parent (Par)) = N_Selected_Component then
8529 declare
8530 Sel : constant Node_Id := Selector_Name (Parent (Par));
8531 Nam : constant Entity_Id := Current_Entity (Sel);
8533 begin
8534 if Present (Nam) and then Is_Overloadable (Nam) then
8535 if Nkind (Parent (Parent (Par))) =
8536 N_Procedure_Call_Statement
8537 then
8538 return False;
8540 elsif Ekind (Nam) = E_Function
8541 and then Present (First_Formal (Nam))
8542 then
8543 return Ekind (First_Formal (Nam)) = E_In_Parameter;
8544 end if;
8545 end if;
8546 end;
8548 elsif Nkind (Par) in N_Op then
8549 return True;
8550 end if;
8552 Par := Parent (Par);
8553 end loop;
8555 -- In all other cases, constant indexing is legal
8557 return True;
8558 end Constant_Indexing_OK;
8560 ----------------------------
8561 -- Expr_Matches_In_Formal --
8562 ----------------------------
8564 function Expr_Matches_In_Formal
8565 (Subp : Entity_Id;
8566 Par : Node_Id) return Boolean
8568 Actual : Node_Id;
8569 Formal : Node_Id;
8571 begin
8572 Formal := First_Formal (Subp);
8573 Actual := First (Parameter_Associations ((Parent (Par))));
8575 if Nkind (Par) /= N_Parameter_Association then
8577 -- Match by position
8579 while Present (Actual) and then Present (Formal) loop
8580 exit when Actual = Par;
8581 Next (Actual);
8583 if Present (Formal) then
8584 Next_Formal (Formal);
8586 -- Otherwise this is a parameter mismatch, the error is
8587 -- reported elsewhere, or else variable indexing is implied.
8589 else
8590 return False;
8591 end if;
8592 end loop;
8594 else
8595 -- Match by name
8597 while Present (Formal) loop
8598 exit when Chars (Formal) = Chars (Selector_Name (Par));
8599 Next_Formal (Formal);
8601 if No (Formal) then
8602 return False;
8603 end if;
8604 end loop;
8605 end if;
8607 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
8608 end Expr_Matches_In_Formal;
8610 ------------------------------
8611 -- Find_Indexing_Operations --
8612 ------------------------------
8614 function Find_Indexing_Operations
8615 (T : Entity_Id;
8616 Nam : Name_Id;
8617 Is_Constant : Boolean) return Node_Id
8619 procedure Inspect_Declarations
8620 (Typ : Entity_Id;
8621 Ref : in out Node_Id);
8622 -- Traverse the declarative list where type Typ resides and collect
8623 -- all suitable interpretations in node Ref.
8625 procedure Inspect_Primitives
8626 (Typ : Entity_Id;
8627 Ref : in out Node_Id);
8628 -- Traverse the list of primitive operations of type Typ and collect
8629 -- all suitable interpretations in node Ref.
8631 function Is_OK_Candidate
8632 (Subp_Id : Entity_Id;
8633 Typ : Entity_Id) return Boolean;
8634 -- Determine whether subprogram Subp_Id is a suitable indexing
8635 -- operation for type Typ. To qualify as such, the subprogram must
8636 -- be a function, have at least two parameters, and the type of the
8637 -- first parameter must be either Typ, or Typ'Class, or access [to
8638 -- constant] with designated type Typ or Typ'Class.
8640 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
8641 -- Store subprogram Subp_Id as an interpretation in node Ref
8643 --------------------------
8644 -- Inspect_Declarations --
8645 --------------------------
8647 procedure Inspect_Declarations
8648 (Typ : Entity_Id;
8649 Ref : in out Node_Id)
8651 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
8652 Decl : Node_Id;
8653 Subp_Id : Entity_Id;
8655 begin
8656 -- Ensure that the routine is not called with itypes, which lack a
8657 -- declarative node.
8659 pragma Assert (Present (Typ_Decl));
8660 pragma Assert (Is_List_Member (Typ_Decl));
8662 Decl := First (List_Containing (Typ_Decl));
8663 while Present (Decl) loop
8664 if Nkind (Decl) = N_Subprogram_Declaration then
8665 Subp_Id := Defining_Entity (Decl);
8667 if Is_OK_Candidate (Subp_Id, Typ) then
8668 Record_Interp (Subp_Id, Ref);
8669 end if;
8670 end if;
8672 Next (Decl);
8673 end loop;
8674 end Inspect_Declarations;
8676 ------------------------
8677 -- Inspect_Primitives --
8678 ------------------------
8680 procedure Inspect_Primitives
8681 (Typ : Entity_Id;
8682 Ref : in out Node_Id)
8684 Prim_Elmt : Elmt_Id;
8685 Prim_Id : Entity_Id;
8687 begin
8688 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
8689 while Present (Prim_Elmt) loop
8690 Prim_Id := Node (Prim_Elmt);
8692 if Is_OK_Candidate (Prim_Id, Typ) then
8693 Record_Interp (Prim_Id, Ref);
8694 end if;
8696 Next_Elmt (Prim_Elmt);
8697 end loop;
8698 end Inspect_Primitives;
8700 ---------------------
8701 -- Is_OK_Candidate --
8702 ---------------------
8704 function Is_OK_Candidate
8705 (Subp_Id : Entity_Id;
8706 Typ : Entity_Id) return Boolean
8708 Formal : Entity_Id;
8709 Formal_Typ : Entity_Id;
8710 Param_Typ : Node_Id;
8712 begin
8713 -- To classify as a suitable candidate, the subprogram must be a
8714 -- function whose name matches the argument of aspect Constant or
8715 -- Variable_Indexing.
8717 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
8718 Formal := First_Formal (Subp_Id);
8720 -- The candidate requires at least two parameters
8722 if Present (Formal) and then Present (Next_Formal (Formal)) then
8723 Formal_Typ := Empty;
8724 Param_Typ := Parameter_Type (Parent (Formal));
8726 -- Use the designated type when the first parameter is of an
8727 -- access type.
8729 if Nkind (Param_Typ) = N_Access_Definition
8730 and then Present (Subtype_Mark (Param_Typ))
8731 then
8732 -- When the context is a constant indexing, the access
8733 -- definition must be access-to-constant. This does not
8734 -- apply to variable indexing.
8736 if not Is_Constant
8737 or else Constant_Present (Param_Typ)
8738 then
8739 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
8740 end if;
8742 -- Otherwise use the parameter type
8744 else
8745 Formal_Typ := Etype (Param_Typ);
8746 end if;
8748 if Present (Formal_Typ) then
8750 -- Use the specific type when the parameter type is
8751 -- class-wide.
8753 if Is_Class_Wide_Type (Formal_Typ) then
8754 Formal_Typ := Etype (Base_Type (Formal_Typ));
8755 end if;
8757 -- Use the full view when the parameter type is private
8758 -- or incomplete.
8760 if Is_Incomplete_Or_Private_Type (Formal_Typ)
8761 and then Present (Full_View (Formal_Typ))
8762 then
8763 Formal_Typ := Full_View (Formal_Typ);
8764 end if;
8766 -- The type of the first parameter must denote the type
8767 -- of the container or acts as its ancestor type.
8769 return
8770 Formal_Typ = Typ
8771 or else Is_Ancestor (Formal_Typ, Typ);
8772 end if;
8773 end if;
8774 end if;
8776 return False;
8777 end Is_OK_Candidate;
8779 -------------------
8780 -- Record_Interp --
8781 -------------------
8783 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8784 begin
8785 if Present (Ref) then
8786 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8788 -- Otherwise this is the first interpretation. Create a reference
8789 -- where all remaining interpretations will be collected.
8791 else
8792 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8793 end if;
8794 end Record_Interp;
8796 -- Local variables
8798 Ref : Node_Id;
8799 Typ : Entity_Id;
8801 -- Start of processing for Find_Indexing_Operations
8803 begin
8804 Typ := T;
8806 -- Use the specific type when the parameter type is class-wide
8808 if Is_Class_Wide_Type (Typ) then
8809 Typ := Root_Type (Typ);
8810 end if;
8812 Ref := Empty;
8813 Typ := Underlying_Type (Base_Type (Typ));
8815 Inspect_Primitives (Typ, Ref);
8817 -- Now look for explicit declarations of an indexing operation.
8818 -- If the type is private the operation may be declared in the
8819 -- visible part that contains the partial view.
8821 if Is_Private_Type (T) then
8822 Inspect_Declarations (T, Ref);
8823 end if;
8825 Inspect_Declarations (Typ, Ref);
8827 return Ref;
8828 end Find_Indexing_Operations;
8830 -- Local variables
8832 Loc : constant Source_Ptr := Sloc (N);
8833 Assoc : List_Id;
8834 C_Type : Entity_Id;
8835 Func : Entity_Id;
8836 Func_Name : Node_Id;
8837 Indexing : Node_Id;
8839 Is_Constant_Indexing : Boolean := False;
8840 -- This flag reflects the nature of the container indexing. Note that
8841 -- the context may be suited for constant indexing, but the type may
8842 -- lack a Constant_Indexing annotation.
8844 -- Start of processing for Try_Container_Indexing
8846 begin
8847 -- Node may have been analyzed already when testing for a prefixed
8848 -- call, in which case do not redo analysis.
8850 if Present (Generalized_Indexing (N)) then
8851 return True;
8852 end if;
8854 -- An explicit dereference needs to be created in the case of a prefix
8855 -- that's an access.
8857 -- It seems that this should be done elsewhere, but not clear where that
8858 -- should happen. Normally Insert_Explicit_Dereference is called via
8859 -- Resolve_Implicit_Dereference, called from Resolve_Indexed_Component,
8860 -- but that won't be called in this case because we transform the
8861 -- indexing to a call. Resolve_Call.Check_Prefixed_Call takes care of
8862 -- implicit dereferencing and referencing on prefixed calls, but that
8863 -- would be too late, even if we expanded to a prefix call, because
8864 -- Process_Indexed_Component will flag an error before the resolution
8865 -- happens. ???
8867 if Is_Access_Type (Pref_Typ) then
8868 Pref_Typ := Implicitly_Designated_Type (Pref_Typ);
8869 Insert_Explicit_Dereference (Prefix);
8870 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
8871 end if;
8873 C_Type := Pref_Typ;
8875 -- If indexing a class-wide container, obtain indexing primitive from
8876 -- specific type.
8878 if Is_Class_Wide_Type (C_Type) then
8879 C_Type := Etype (Base_Type (C_Type));
8880 end if;
8882 -- Check whether the type has a specified indexing aspect
8884 Func_Name := Empty;
8886 -- The context is suitable for constant indexing, so obtain the name of
8887 -- the indexing function from aspect Constant_Indexing.
8889 if Constant_Indexing_OK then
8890 Func_Name :=
8891 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
8892 end if;
8894 if Present (Func_Name) then
8895 Is_Constant_Indexing := True;
8897 -- Otherwise attempt variable indexing
8899 else
8900 Func_Name :=
8901 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
8902 end if;
8904 -- The type is not subject to either form of indexing, therefore the
8905 -- indexed component does not denote container indexing. If this is a
8906 -- true error, it is diagnosed by the caller.
8908 if No (Func_Name) then
8910 -- The prefix itself may be an indexing of a container. Rewrite it
8911 -- as such and retry.
8913 if Has_Implicit_Dereference (Pref_Typ) then
8914 Build_Explicit_Dereference
8915 (Prefix, Get_Reference_Discriminant (Pref_Typ));
8916 return Try_Container_Indexing (N, Prefix, Exprs);
8918 -- Otherwise this is definitely not container indexing
8920 else
8921 return False;
8922 end if;
8924 -- If the container type is derived from another container type, the
8925 -- value of the inherited aspect is the Reference operation declared
8926 -- for the parent type.
8928 -- However, Reference is also a primitive operation of the type, and the
8929 -- inherited operation has a different signature. We retrieve the right
8930 -- ones (the function may be overloaded) from the list of primitive
8931 -- operations of the derived type.
8933 -- Note that predefined containers are typically all derived from one of
8934 -- the Controlled types. The code below is motivated by containers that
8935 -- are derived from other types with a Reference aspect.
8936 -- Note as well that we need to examine the base type, given that
8937 -- the container object may be a constrained subtype or itype that
8938 -- does not have an explicit declaration.
8940 elsif Is_Derived_Type (C_Type)
8941 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
8942 then
8943 Func_Name :=
8944 Find_Indexing_Operations
8945 (T => Base_Type (C_Type),
8946 Nam => Chars (Func_Name),
8947 Is_Constant => Is_Constant_Indexing);
8948 end if;
8950 Assoc := New_List (Relocate_Node (Prefix));
8952 -- A generalized indexing may have nore than one index expression, so
8953 -- transfer all of them to the argument list to be used in the call.
8954 -- Note that there may be named associations, in which case the node
8955 -- was rewritten earlier as a call, and has been transformed back into
8956 -- an indexed expression to share the following processing.
8958 -- The generalized indexing node is the one on which analysis and
8959 -- resolution take place. Before expansion the original node is replaced
8960 -- with the generalized indexing node, which is a call, possibly with a
8961 -- dereference operation.
8963 -- Create argument list for function call that represents generalized
8964 -- indexing. Note that indices (i.e. actuals) may themselves be
8965 -- overloaded.
8967 declare
8968 Arg : Node_Id;
8969 New_Arg : Node_Id;
8971 begin
8972 Arg := First (Exprs);
8973 while Present (Arg) loop
8974 New_Arg := Relocate_Node (Arg);
8976 -- The arguments can be parameter associations, in which case the
8977 -- explicit actual parameter carries the overloadings.
8979 if Nkind (New_Arg) /= N_Parameter_Association then
8980 Save_Interps (Arg, New_Arg);
8981 end if;
8983 Append (New_Arg, Assoc);
8984 Next (Arg);
8985 end loop;
8986 end;
8988 if not Is_Overloaded (Func_Name) then
8989 Func := Entity (Func_Name);
8991 -- Can happen in case of e.g. cascaded errors
8993 if No (Func) then
8994 return False;
8995 end if;
8997 Indexing :=
8998 Make_Function_Call (Loc,
8999 Name => New_Occurrence_Of (Func, Loc),
9000 Parameter_Associations => Assoc);
9002 Set_Parent (Indexing, Parent (N));
9003 Set_Generalized_Indexing (N, Indexing);
9004 Analyze (Indexing);
9005 Set_Etype (N, Etype (Indexing));
9007 -- If the return type of the indexing function is a reference type,
9008 -- add the dereference as a possible interpretation. Note that the
9009 -- indexing aspect may be a function that returns the element type
9010 -- with no intervening implicit dereference, and that the reference
9011 -- discriminant is not the first discriminant.
9013 if Has_Discriminants (Etype (Func)) then
9014 Check_Implicit_Dereference (N, Etype (Func));
9015 end if;
9017 else
9018 -- If there are multiple indexing functions, build a function call
9019 -- and analyze it for each of the possible interpretations.
9021 Indexing :=
9022 Make_Function_Call (Loc,
9023 Name =>
9024 Make_Identifier (Loc, Chars (Func_Name)),
9025 Parameter_Associations => Assoc);
9026 Set_Parent (Indexing, Parent (N));
9027 Set_Generalized_Indexing (N, Indexing);
9028 Set_Etype (N, Any_Type);
9029 Set_Etype (Name (Indexing), Any_Type);
9031 declare
9032 I : Interp_Index;
9033 It : Interp;
9034 Success : Boolean;
9036 begin
9037 Get_First_Interp (Func_Name, I, It);
9038 Set_Etype (Indexing, Any_Type);
9040 -- Analyze each candidate function with the given actuals
9042 while Present (It.Nam) loop
9043 Analyze_One_Call (Indexing, It.Nam, False, Success);
9044 Get_Next_Interp (I, It);
9045 end loop;
9047 -- If there are several successful candidates, resolution will
9048 -- be by result. Mark the interpretations of the function name
9049 -- itself.
9051 if Is_Overloaded (Indexing) then
9052 Get_First_Interp (Indexing, I, It);
9054 while Present (It.Nam) loop
9055 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
9056 Get_Next_Interp (I, It);
9057 end loop;
9059 else
9060 Set_Etype (Name (Indexing), Etype (Indexing));
9061 end if;
9063 -- Now add the candidate interpretations to the indexing node
9064 -- itself, to be replaced later by the function call.
9066 if Is_Overloaded (Name (Indexing)) then
9067 Get_First_Interp (Name (Indexing), I, It);
9069 while Present (It.Nam) loop
9070 Add_One_Interp (N, It.Nam, It.Typ);
9072 -- Add dereference interpretation if the result type has
9073 -- implicit reference discriminants.
9075 if Has_Discriminants (Etype (It.Nam)) then
9076 Check_Implicit_Dereference (N, Etype (It.Nam));
9077 end if;
9079 Get_Next_Interp (I, It);
9080 end loop;
9082 else
9083 Set_Etype (N, Etype (Name (Indexing)));
9084 if Has_Discriminants (Etype (N)) then
9085 Check_Implicit_Dereference (N, Etype (N));
9086 end if;
9087 end if;
9088 end;
9089 end if;
9091 if Etype (Indexing) = Any_Type then
9092 Error_Msg_NE
9093 ("container cannot be indexed with&", N, Etype (First (Exprs)));
9094 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
9095 end if;
9097 return True;
9098 end Try_Container_Indexing;
9100 -----------------------
9101 -- Try_Indirect_Call --
9102 -----------------------
9104 function Try_Indirect_Call
9105 (N : Node_Id;
9106 Nam : Entity_Id;
9107 Typ : Entity_Id) return Boolean
9109 Actual : Node_Id;
9110 Formal : Entity_Id;
9112 Call_OK : Boolean;
9113 pragma Warnings (Off, Call_OK);
9115 begin
9116 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
9118 Actual := First_Actual (N);
9119 Formal := First_Formal (Designated_Type (Typ));
9120 while Present (Actual) and then Present (Formal) loop
9121 if not Has_Compatible_Type (Actual, Etype (Formal)) then
9122 return False;
9123 end if;
9125 Next (Actual);
9126 Next_Formal (Formal);
9127 end loop;
9129 if No (Actual) and then No (Formal) then
9130 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
9132 -- Nam is a candidate interpretation for the name in the call,
9133 -- if it is not an indirect call.
9135 if not Is_Type (Nam)
9136 and then Is_Entity_Name (Name (N))
9137 then
9138 Set_Entity (Name (N), Nam);
9139 end if;
9141 return True;
9143 else
9144 return False;
9145 end if;
9146 end Try_Indirect_Call;
9148 ----------------------
9149 -- Try_Indexed_Call --
9150 ----------------------
9152 function Try_Indexed_Call
9153 (N : Node_Id;
9154 Nam : Entity_Id;
9155 Typ : Entity_Id;
9156 Skip_First : Boolean) return Boolean
9158 Loc : constant Source_Ptr := Sloc (N);
9159 Actuals : constant List_Id := Parameter_Associations (N);
9160 Actual : Node_Id;
9161 Index : Entity_Id;
9163 begin
9164 Actual := First (Actuals);
9166 -- If the call was originally written in prefix form, skip the first
9167 -- actual, which is obviously not defaulted.
9169 if Skip_First then
9170 Next (Actual);
9171 end if;
9173 Index := First_Index (Typ);
9174 while Present (Actual) and then Present (Index) loop
9176 -- If the parameter list has a named association, the expression
9177 -- is definitely a call and not an indexed component.
9179 if Nkind (Actual) = N_Parameter_Association then
9180 return False;
9181 end if;
9183 if Is_Entity_Name (Actual)
9184 and then Is_Type (Entity (Actual))
9185 and then No (Next (Actual))
9186 then
9187 -- A single actual that is a type name indicates a slice if the
9188 -- type is discrete, and an error otherwise.
9190 if Is_Discrete_Type (Entity (Actual)) then
9191 Rewrite (N,
9192 Make_Slice (Loc,
9193 Prefix =>
9194 Make_Function_Call (Loc,
9195 Name => Relocate_Node (Name (N))),
9196 Discrete_Range =>
9197 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
9199 Analyze (N);
9201 else
9202 Error_Msg_N ("invalid use of type in expression", Actual);
9203 Set_Etype (N, Any_Type);
9204 end if;
9206 return True;
9208 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
9209 return False;
9210 end if;
9212 Next (Actual);
9213 Next_Index (Index);
9214 end loop;
9216 if No (Actual) and then No (Index) then
9217 Add_One_Interp (N, Nam, Component_Type (Typ));
9219 -- Nam is a candidate interpretation for the name in the call,
9220 -- if it is not an indirect call.
9222 if not Is_Type (Nam)
9223 and then Is_Entity_Name (Name (N))
9224 then
9225 Set_Entity (Name (N), Nam);
9226 end if;
9228 return True;
9229 else
9230 return False;
9231 end if;
9232 end Try_Indexed_Call;
9234 --------------------------
9235 -- Try_Object_Operation --
9236 --------------------------
9238 function Try_Object_Operation
9239 (N : Node_Id;
9240 CW_Test_Only : Boolean := False;
9241 Allow_Extensions : Boolean := False) return Boolean
9243 K : constant Node_Kind := Nkind (Parent (N));
9244 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
9245 Loc : constant Source_Ptr := Sloc (N);
9246 Obj : constant Node_Id := Prefix (N);
9248 Subprog : constant Node_Id :=
9249 Make_Identifier (Sloc (Selector_Name (N)),
9250 Chars => Chars (Selector_Name (N)));
9251 -- Identifier on which possible interpretations will be collected
9253 Report_Error : Boolean := False;
9254 -- If no candidate interpretation matches the context, redo analysis
9255 -- with Report_Error True to provide additional information.
9257 Actual : Node_Id;
9258 Candidate : Entity_Id := Empty;
9259 New_Call_Node : Node_Id := Empty;
9260 Node_To_Replace : Node_Id;
9261 Obj_Type : Entity_Id := Etype (Obj);
9262 Success : Boolean := False;
9264 procedure Complete_Object_Operation
9265 (Call_Node : Node_Id;
9266 Node_To_Replace : Node_Id);
9267 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
9268 -- Call_Node, insert the object (or its dereference) as the first actual
9269 -- in the call, and complete the analysis of the call.
9271 procedure Report_Ambiguity (Op : Entity_Id);
9272 -- If a prefixed procedure call is ambiguous, indicate whether the call
9273 -- includes an implicit dereference or an implicit 'Access.
9275 procedure Transform_Object_Operation
9276 (Call_Node : out Node_Id;
9277 Node_To_Replace : out Node_Id);
9278 -- Transform Obj.Operation (X, Y, ...) into Operation (Obj, X, Y ...).
9279 -- Call_Node is the resulting subprogram call, Node_To_Replace is
9280 -- either N or the parent of N, and Subprog is a reference to the
9281 -- subprogram we are trying to match. Note that the transformation
9282 -- may be partially destructive for the parent of N, so it needs to
9283 -- be undone in the case where Try_Object_Operation returns false.
9285 function Try_Class_Wide_Operation
9286 (Call_Node : Node_Id;
9287 Node_To_Replace : Node_Id) return Boolean;
9288 -- Traverse all ancestor types looking for a class-wide subprogram for
9289 -- which the current operation is a valid non-dispatching call.
9291 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
9292 -- If prefix is overloaded, its interpretation may include different
9293 -- tagged types, and we must examine the primitive operations and the
9294 -- class-wide operations of each in order to find candidate
9295 -- interpretations for the call as a whole.
9297 function Try_Primitive_Operation
9298 (Call_Node : Node_Id;
9299 Node_To_Replace : Node_Id) return Boolean;
9300 -- Traverse the list of primitive subprograms looking for a dispatching
9301 -- operation for which the current node is a valid call.
9303 function Valid_Candidate
9304 (Success : Boolean;
9305 Call : Node_Id;
9306 Subp : Entity_Id) return Entity_Id;
9307 -- If the subprogram is a valid interpretation, record it, and add to
9308 -- the list of interpretations of Subprog. Otherwise return Empty.
9310 -------------------------------
9311 -- Complete_Object_Operation --
9312 -------------------------------
9314 procedure Complete_Object_Operation
9315 (Call_Node : Node_Id;
9316 Node_To_Replace : Node_Id)
9318 Control : constant Entity_Id := First_Formal (Entity (Subprog));
9319 Formal_Type : constant Entity_Id := Etype (Control);
9320 First_Actual : Node_Id;
9322 begin
9323 -- Place the name of the operation, with its interpretations,
9324 -- on the rewritten call.
9326 Set_Name (Call_Node, Subprog);
9328 First_Actual := First (Parameter_Associations (Call_Node));
9330 -- For cross-reference purposes, treat the new node as being in the
9331 -- source if the original one is. Set entity and type, even though
9332 -- they may be overwritten during resolution if overloaded.
9334 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
9335 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
9337 if Nkind (N) = N_Selected_Component
9338 and then not Inside_A_Generic
9339 then
9340 Set_Entity (Selector_Name (N), Entity (Subprog));
9341 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
9342 end if;
9344 -- If need be, rewrite first actual as an explicit dereference. If
9345 -- the call is overloaded, the rewriting can only be done once the
9346 -- primitive operation is identified.
9348 if Is_Overloaded (Subprog) then
9350 -- The prefix itself may be overloaded, and its interpretations
9351 -- must be propagated to the new actual in the call.
9353 if Is_Overloaded (Obj) then
9354 Save_Interps (Obj, First_Actual);
9355 end if;
9357 Rewrite (First_Actual, Obj);
9359 elsif not Is_Access_Type (Formal_Type)
9360 and then Is_Access_Type (Etype (Obj))
9361 then
9362 Rewrite (First_Actual,
9363 Make_Explicit_Dereference (Sloc (Obj), Obj));
9364 Analyze (First_Actual);
9366 -- If we need to introduce an explicit dereference, verify that
9367 -- the resulting actual is compatible with the mode of the formal.
9369 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
9370 and then Is_Access_Constant (Etype (Obj))
9371 then
9372 Error_Msg_NE
9373 ("expect variable in call to&", Prefix (N), Entity (Subprog));
9374 end if;
9376 -- Conversely, if the formal is an access parameter and the object is
9377 -- not an access type or a reference type (i.e. a type with the
9378 -- Implicit_Dereference aspect specified), replace the actual with a
9379 -- 'Access reference. Its analysis will check that the object is
9380 -- aliased.
9382 elsif Is_Access_Type (Formal_Type)
9383 and then not Is_Access_Type (Etype (Obj))
9384 and then
9385 (not Has_Implicit_Dereference (Etype (Obj))
9386 or else
9387 not Is_Access_Type (Designated_Type (Etype
9388 (Get_Reference_Discriminant (Etype (Obj))))))
9389 then
9390 -- A special case: A.all'Access is illegal if A is an access to a
9391 -- constant and the context requires an access to a variable.
9393 if not Is_Access_Constant (Formal_Type) then
9394 if (Nkind (Obj) = N_Explicit_Dereference
9395 and then Is_Access_Constant (Etype (Prefix (Obj))))
9396 or else not Is_Variable (Obj)
9397 then
9398 Error_Msg_NE
9399 ("actual for & must be a variable", Obj, Control);
9400 end if;
9401 end if;
9403 Rewrite (First_Actual,
9404 Make_Attribute_Reference (Loc,
9405 Attribute_Name => Name_Access,
9406 Prefix => Relocate_Node (Obj)));
9408 -- If the object is not overloaded verify that taking access of
9409 -- it is legal. Otherwise check is made during resolution.
9411 if not Is_Overloaded (Obj)
9412 and then not Is_Aliased_View (Obj)
9413 then
9414 Error_Msg_NE
9415 ("object in prefixed call to & must be aliased "
9416 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
9417 end if;
9419 Analyze (First_Actual);
9421 else
9422 if Is_Overloaded (Obj) then
9423 Save_Interps (Obj, First_Actual);
9424 end if;
9426 Rewrite (First_Actual, Obj);
9427 end if;
9429 if In_Extended_Main_Source_Unit (Current_Scope) then
9430 -- The operation is obtained from the dispatch table and not by
9431 -- visibility, and may be declared in a unit that is not
9432 -- explicitly referenced in the source, but is nevertheless
9433 -- required in the context of the current unit. Indicate that
9434 -- operation and its scope are referenced, to prevent spurious and
9435 -- misleading warnings. If the operation is overloaded, all
9436 -- primitives are in the same scope and we can use any of them.
9437 -- Don't do that outside the main unit since otherwise this will
9438 -- e.g. prevent the detection of some unused with clauses.
9440 Set_Referenced (Entity (Subprog), True);
9441 Set_Referenced (Scope (Entity (Subprog)), True);
9442 end if;
9444 Rewrite (Node_To_Replace, Call_Node);
9446 -- Propagate the interpretations collected in subprog to the new
9447 -- function call node, to be resolved from context.
9449 if Is_Overloaded (Subprog) then
9450 Save_Interps (Subprog, Node_To_Replace);
9452 else
9453 Analyze (Node_To_Replace);
9455 -- If the operation has been rewritten into a call, which may get
9456 -- subsequently an explicit dereference, preserve the type on the
9457 -- original node (selected component or indexed component) for
9458 -- subsequent legality tests, e.g. Is_Variable. which examines
9459 -- the original node.
9461 if Nkind (Node_To_Replace) = N_Function_Call then
9462 Set_Etype
9463 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
9464 end if;
9465 end if;
9466 end Complete_Object_Operation;
9468 ----------------------
9469 -- Report_Ambiguity --
9470 ----------------------
9472 procedure Report_Ambiguity (Op : Entity_Id) is
9473 Access_Actual : constant Boolean :=
9474 Is_Access_Type (Etype (Prefix (N)));
9475 Access_Formal : Boolean := False;
9477 begin
9478 Error_Msg_Sloc := Sloc (Op);
9480 if Present (First_Formal (Op)) then
9481 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
9482 end if;
9484 if Access_Formal and then not Access_Actual then
9485 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9486 Error_Msg_N
9487 ("\possible interpretation "
9488 & "(inherited, with implicit 'Access) #", N);
9489 else
9490 Error_Msg_N
9491 ("\possible interpretation (with implicit 'Access) #", N);
9492 end if;
9494 elsif not Access_Formal and then Access_Actual then
9495 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9496 Error_Msg_N
9497 ("\possible interpretation "
9498 & "(inherited, with implicit dereference) #", N);
9499 else
9500 Error_Msg_N
9501 ("\possible interpretation (with implicit dereference) #", N);
9502 end if;
9504 else
9505 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
9506 Error_Msg_N ("\possible interpretation (inherited)#", N);
9507 else
9508 Error_Msg_N -- CODEFIX
9509 ("\possible interpretation#", N);
9510 end if;
9511 end if;
9512 end Report_Ambiguity;
9514 --------------------------------
9515 -- Transform_Object_Operation --
9516 --------------------------------
9518 procedure Transform_Object_Operation
9519 (Call_Node : out Node_Id;
9520 Node_To_Replace : out Node_Id)
9522 Dummy : constant Node_Id := New_Copy (Obj);
9523 -- Placeholder used as a first parameter in the call, replaced
9524 -- eventually by the proper object.
9526 Parent_Node : constant Node_Id := Parent (N);
9528 Actual : Node_Id;
9529 Actuals : List_Id;
9531 begin
9532 -- Common case covering 1) Call to a procedure and 2) Call to a
9533 -- function that has some additional actuals.
9535 if Nkind (Parent_Node) in N_Subprogram_Call
9537 -- N is a selected component node containing the name of the
9538 -- subprogram. If N is not the name of the parent node we must
9539 -- not replace the parent node by the new construct. This case
9540 -- occurs when N is a parameterless call to a subprogram that
9541 -- is an actual parameter of a call to another subprogram. For
9542 -- example:
9543 -- Some_Subprogram (..., Obj.Operation, ...)
9545 and then N = Name (Parent_Node)
9546 then
9547 Node_To_Replace := Parent_Node;
9549 Actuals := Parameter_Associations (Parent_Node);
9551 if Present (Actuals) then
9552 Prepend (Dummy, Actuals);
9553 else
9554 Actuals := New_List (Dummy);
9555 end if;
9557 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
9558 Call_Node :=
9559 Make_Procedure_Call_Statement (Loc,
9560 Name => New_Copy (Subprog),
9561 Parameter_Associations => Actuals);
9563 else
9564 Call_Node :=
9565 Make_Function_Call (Loc,
9566 Name => New_Copy (Subprog),
9567 Parameter_Associations => Actuals);
9568 end if;
9570 -- Before analysis, a function call appears as an indexed component
9571 -- if there are no named associations.
9573 elsif Nkind (Parent_Node) = N_Indexed_Component
9574 and then N = Prefix (Parent_Node)
9575 then
9576 Node_To_Replace := Parent_Node;
9577 Actuals := Expressions (Parent_Node);
9579 Actual := First (Actuals);
9580 while Present (Actual) loop
9581 Analyze (Actual);
9582 Next (Actual);
9583 end loop;
9585 Prepend (Dummy, Actuals);
9587 Call_Node :=
9588 Make_Function_Call (Loc,
9589 Name => New_Copy (Subprog),
9590 Parameter_Associations => Actuals);
9592 -- Parameterless call: Obj.F is rewritten as F (Obj)
9594 else
9595 Node_To_Replace := N;
9597 Call_Node :=
9598 Make_Function_Call (Loc,
9599 Name => New_Copy (Subprog),
9600 Parameter_Associations => New_List (Dummy));
9601 end if;
9602 end Transform_Object_Operation;
9604 ------------------------------
9605 -- Try_Class_Wide_Operation --
9606 ------------------------------
9608 function Try_Class_Wide_Operation
9609 (Call_Node : Node_Id;
9610 Node_To_Replace : Node_Id) return Boolean
9612 Anc_Type : Entity_Id;
9613 Matching_Op : Entity_Id := Empty;
9614 Error : Boolean;
9616 procedure Traverse_Homonyms
9617 (Anc_Type : Entity_Id;
9618 Error : out Boolean);
9619 -- Traverse the homonym chain of the subprogram searching for those
9620 -- homonyms whose first formal has the Anc_Type's class-wide type,
9621 -- or an anonymous access type designating the class-wide type. If
9622 -- an ambiguity is detected, then Error is set to True.
9624 procedure Traverse_Interfaces
9625 (Anc_Type : Entity_Id;
9626 Error : out Boolean);
9627 -- Traverse the list of interfaces, if any, associated with Anc_Type
9628 -- and search for acceptable class-wide homonyms associated with each
9629 -- interface. If an ambiguity is detected, then Error is set to True.
9631 -----------------------
9632 -- Traverse_Homonyms --
9633 -----------------------
9635 procedure Traverse_Homonyms
9636 (Anc_Type : Entity_Id;
9637 Error : out Boolean)
9639 function First_Formal_Match
9640 (Subp_Id : Entity_Id;
9641 Typ : Entity_Id) return Boolean;
9642 -- Predicate to verify that the first foramal of class-wide
9643 -- subprogram Subp_Id matches type Typ of the prefix.
9645 ------------------------
9646 -- First_Formal_Match --
9647 ------------------------
9649 function First_Formal_Match
9650 (Subp_Id : Entity_Id;
9651 Typ : Entity_Id) return Boolean
9653 Ctrl : constant Entity_Id := First_Formal (Subp_Id);
9655 begin
9656 return
9657 Present (Ctrl)
9658 and then
9659 (Base_Type (Etype (Ctrl)) = Typ
9660 or else
9661 (Ekind (Etype (Ctrl)) = E_Anonymous_Access_Type
9662 and then
9663 Base_Type (Designated_Type (Etype (Ctrl))) =
9664 Typ));
9665 end First_Formal_Match;
9667 -- Local variables
9669 CW_Typ : constant Entity_Id := Class_Wide_Type (Anc_Type);
9671 Candidate : Entity_Id;
9672 -- If homonym is a renaming, examine the renamed program
9674 Hom : Entity_Id;
9675 Hom_Ref : Node_Id;
9676 Success : Boolean;
9678 -- Start of processing for Traverse_Homonyms
9680 begin
9681 Error := False;
9683 -- Find a non-hidden operation whose first parameter is of the
9684 -- class-wide type, a subtype thereof, or an anonymous access
9685 -- to same. If in an instance, the operation can be considered
9686 -- even if hidden (it may be hidden because the instantiation
9687 -- is expanded after the containing package has been analyzed).
9688 -- If the subprogram is a generic actual in an enclosing instance,
9689 -- it appears as a renaming that is a candidate interpretation as
9690 -- well.
9692 Hom := Current_Entity (Subprog);
9693 while Present (Hom) loop
9694 if Ekind (Hom) in E_Procedure | E_Function
9695 and then Present (Renamed_Entity (Hom))
9696 and then Is_Generic_Actual_Subprogram (Hom)
9697 and then In_Open_Scopes (Scope (Hom))
9698 then
9699 Candidate := Renamed_Entity (Hom);
9700 else
9701 Candidate := Hom;
9702 end if;
9704 if Ekind (Candidate) in E_Function | E_Procedure
9705 and then (not Is_Hidden (Candidate) or else In_Instance)
9706 and then Scope (Candidate) = Scope (Base_Type (Anc_Type))
9707 and then First_Formal_Match (Candidate, CW_Typ)
9708 then
9709 -- If the context is a procedure call, ignore functions
9710 -- in the name of the call.
9712 if Ekind (Candidate) = E_Function
9713 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
9714 and then N = Name (Parent (N))
9715 then
9716 goto Next_Hom;
9718 -- If the context is a function call, ignore procedures
9719 -- in the name of the call.
9721 elsif Ekind (Candidate) = E_Procedure
9722 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
9723 then
9724 goto Next_Hom;
9725 end if;
9727 Set_Etype (Call_Node, Any_Type);
9728 Set_Is_Overloaded (Call_Node, False);
9729 Success := False;
9731 if No (Matching_Op) then
9732 Hom_Ref := New_Occurrence_Of (Candidate, Sloc (Subprog));
9734 Set_Etype (Call_Node, Any_Type);
9735 Set_Name (Call_Node, Hom_Ref);
9736 Set_Parent (Call_Node, Parent (Node_To_Replace));
9738 Analyze_One_Call
9739 (N => Call_Node,
9740 Nam => Candidate,
9741 Report => Report_Error,
9742 Success => Success,
9743 Skip_First => True);
9745 Matching_Op :=
9746 Valid_Candidate (Success, Call_Node, Candidate);
9748 else
9749 Analyze_One_Call
9750 (N => Call_Node,
9751 Nam => Candidate,
9752 Report => Report_Error,
9753 Success => Success,
9754 Skip_First => True);
9756 -- The same operation may be encountered on two homonym
9757 -- traversals, before and after looking at interfaces.
9758 -- Check for this case before reporting a real ambiguity.
9760 if Present
9761 (Valid_Candidate (Success, Call_Node, Candidate))
9762 and then Nkind (Call_Node) /= N_Function_Call
9763 and then Candidate /= Matching_Op
9764 then
9765 Error_Msg_NE ("ambiguous call to&", N, Hom);
9766 Report_Ambiguity (Matching_Op);
9767 Report_Ambiguity (Hom);
9768 Check_Ambiguous_Aggregate (New_Call_Node);
9769 Error := True;
9770 return;
9771 end if;
9772 end if;
9773 end if;
9775 <<Next_Hom>>
9776 Hom := Homonym (Hom);
9777 end loop;
9778 end Traverse_Homonyms;
9780 -------------------------
9781 -- Traverse_Interfaces --
9782 -------------------------
9784 procedure Traverse_Interfaces
9785 (Anc_Type : Entity_Id;
9786 Error : out Boolean)
9788 Intface_List : constant List_Id :=
9789 Abstract_Interface_List (Anc_Type);
9790 Intface : Node_Id;
9792 begin
9793 Error := False;
9795 Intface := First (Intface_List);
9796 while Present (Intface) loop
9798 -- Look for acceptable class-wide homonyms associated with the
9799 -- interface.
9801 Traverse_Homonyms (Etype (Intface), Error);
9803 if Error then
9804 return;
9805 end if;
9807 -- Continue the search by looking at each of the interface's
9808 -- associated interface ancestors.
9810 Traverse_Interfaces (Etype (Intface), Error);
9812 if Error then
9813 return;
9814 end if;
9816 Next (Intface);
9817 end loop;
9818 end Traverse_Interfaces;
9820 -- Start of processing for Try_Class_Wide_Operation
9822 begin
9823 -- If we are searching only for conflicting class-wide subprograms
9824 -- then initialize directly Matching_Op with the target entity.
9826 if CW_Test_Only then
9827 Matching_Op := Entity (Selector_Name (N));
9828 end if;
9830 -- Loop through ancestor types (including interfaces), traversing
9831 -- the homonym chain of the subprogram, trying out those homonyms
9832 -- whose first formal has the class-wide type of the ancestor, or
9833 -- an anonymous access type designating the class-wide type.
9835 Anc_Type := Obj_Type;
9836 loop
9837 -- Look for a match among homonyms associated with the ancestor
9839 Traverse_Homonyms (Anc_Type, Error);
9841 if Error then
9842 return True;
9843 end if;
9845 -- Continue the search for matches among homonyms associated with
9846 -- any interfaces implemented by the ancestor.
9848 Traverse_Interfaces (Anc_Type, Error);
9850 if Error then
9851 return True;
9852 end if;
9854 exit when Etype (Anc_Type) = Anc_Type;
9855 Anc_Type := Etype (Anc_Type);
9856 end loop;
9858 if Present (Matching_Op) then
9859 Set_Etype (Call_Node, Etype (Matching_Op));
9860 end if;
9862 return Present (Matching_Op);
9863 end Try_Class_Wide_Operation;
9865 -----------------------------------
9866 -- Try_One_Prefix_Interpretation --
9867 -----------------------------------
9869 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
9870 Prev_Obj_Type : constant Entity_Id := Obj_Type;
9871 -- If the interpretation does not have a valid candidate type,
9872 -- preserve current value of Obj_Type for subsequent errors.
9874 begin
9875 Obj_Type := T;
9877 if Is_Access_Type (Obj_Type) then
9878 Obj_Type := Designated_Type (Obj_Type);
9879 end if;
9881 if Ekind (Obj_Type)
9882 in E_Private_Subtype | E_Record_Subtype_With_Private
9883 then
9884 Obj_Type := Base_Type (Obj_Type);
9885 end if;
9887 if Is_Class_Wide_Type (Obj_Type) then
9888 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9889 end if;
9891 -- The type may have be obtained through a limited_with clause,
9892 -- in which case the primitive operations are available on its
9893 -- nonlimited view. If still incomplete, retrieve full view.
9895 if Ekind (Obj_Type) = E_Incomplete_Type
9896 and then From_Limited_With (Obj_Type)
9897 and then Has_Non_Limited_View (Obj_Type)
9898 then
9899 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
9900 end if;
9902 -- If the object is not tagged, or the type is still an incomplete
9903 -- type, this is not a prefixed call. Restore the previous type as
9904 -- the current one is not a legal candidate.
9906 -- Extension feature: Calls with prefixed views are also supported
9907 -- for untagged types, so skip the early return when extensions are
9908 -- enabled, unless the type doesn't have a primitive operations list
9909 -- (such as in the case of predefined types).
9911 if (not Is_Tagged_Type (Obj_Type)
9912 and then
9913 (not (Core_Extensions_Allowed or Allow_Extensions)
9914 or else not Present (Primitive_Operations (Obj_Type))))
9915 or else Is_Incomplete_Type (Obj_Type)
9916 then
9917 Obj_Type := Prev_Obj_Type;
9918 return;
9919 end if;
9921 declare
9922 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
9923 Ignore : Boolean;
9924 Prim_Result : Boolean := False;
9926 begin
9927 if not CW_Test_Only then
9928 Prim_Result :=
9929 Try_Primitive_Operation
9930 (Call_Node => New_Call_Node,
9931 Node_To_Replace => Node_To_Replace);
9933 -- Extension feature: In the case where the prefix is of an
9934 -- access type, and a primitive wasn't found for the designated
9935 -- type, then if the access type has primitives we attempt a
9936 -- prefixed call using one of its primitives. (It seems that
9937 -- this isn't quite right to give preference to the designated
9938 -- type in the case where both the access and designated types
9939 -- have homographic prefixed-view operations that could result
9940 -- in an ambiguity, but handling properly may be tricky. ???)
9942 if (Core_Extensions_Allowed or Allow_Extensions)
9943 and then not Prim_Result
9944 and then Is_Named_Access_Type (Prev_Obj_Type)
9945 and then Present (Direct_Primitive_Operations (Prev_Obj_Type))
9946 then
9947 -- Temporarily reset Obj_Type to the original access type
9949 Obj_Type := Prev_Obj_Type;
9951 Prim_Result :=
9952 Try_Primitive_Operation
9953 (Call_Node => New_Call_Node,
9954 Node_To_Replace => Node_To_Replace);
9956 -- Restore Obj_Type to the designated type (is this really
9957 -- necessary, or should it only be done when Prim_Result is
9958 -- still False?).
9960 Obj_Type := Designated_Type (Obj_Type);
9961 end if;
9962 end if;
9964 -- Check if there is a class-wide subprogram covering the
9965 -- primitive. This check must be done even if a candidate
9966 -- was found in order to report ambiguous calls.
9968 if not Prim_Result then
9969 Ignore :=
9970 Try_Class_Wide_Operation
9971 (Call_Node => New_Call_Node,
9972 Node_To_Replace => Node_To_Replace);
9974 -- If we found a primitive we search for class-wide subprograms
9975 -- using a duplicate of the call node (done to avoid missing its
9976 -- decoration if there is no ambiguity).
9978 else
9979 Ignore :=
9980 Try_Class_Wide_Operation
9981 (Call_Node => Dup_Call_Node,
9982 Node_To_Replace => Node_To_Replace);
9983 end if;
9984 end;
9985 end Try_One_Prefix_Interpretation;
9987 -----------------------------
9988 -- Try_Primitive_Operation --
9989 -----------------------------
9991 function Try_Primitive_Operation
9992 (Call_Node : Node_Id;
9993 Node_To_Replace : Node_Id) return Boolean
9995 Elmt : Elmt_Id;
9996 Prim_Op : Entity_Id;
9997 Matching_Op : Entity_Id := Empty;
9998 Prim_Op_Ref : Node_Id := Empty;
10000 Corr_Type : Entity_Id := Empty;
10001 -- If the prefix is a synchronized type, the controlling type of
10002 -- the primitive operation is the corresponding record type, else
10003 -- this is the object type itself.
10005 Success : Boolean := False;
10007 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
10008 -- For tagged types the candidate interpretations are found in
10009 -- the list of primitive operations of the type and its ancestors.
10010 -- For formal tagged types we have to find the operations declared
10011 -- in the same scope as the type (including in the generic formal
10012 -- part) because the type itself carries no primitive operations,
10013 -- except for formal derived types that inherit the operations of
10014 -- the parent and progenitors.
10016 -- If the context is a generic subprogram body, the generic formals
10017 -- are visible by name, but are not in the entity list of the
10018 -- subprogram because that list starts with the subprogram formals.
10019 -- We retrieve the candidate operations from the generic declaration.
10021 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
10022 -- Prefix notation can also be used on operations that are not
10023 -- primitives of the type, but are declared in the same immediate
10024 -- declarative part, which can only mean the corresponding package
10025 -- body (see RM 4.1.3 (9.2/3)). If we are in that body we extend the
10026 -- list of primitives with body operations with the same name that
10027 -- may be candidates, so that Try_Primitive_Operations can examine
10028 -- them if no real primitive is found.
10030 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
10031 -- An operation that overrides an inherited operation in the private
10032 -- part of its package may be hidden, but if the inherited operation
10033 -- is visible a direct call to it will dispatch to the private one,
10034 -- which is therefore a valid candidate.
10036 function Names_Match
10037 (Obj_Type : Entity_Id;
10038 Prim_Op : Entity_Id;
10039 Subprog : Entity_Id) return Boolean;
10040 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
10041 -- is a protected type then compare also the original name of Prim_Op
10042 -- with the name of Subprog (since the expander may have added a
10043 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
10045 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
10046 -- Verify that the prefix, dereferenced if need be, is a valid
10047 -- controlling argument in a call to Op. The remaining actuals
10048 -- are checked in the subsequent call to Analyze_One_Call.
10050 ------------------------------
10051 -- Collect_Generic_Type_Ops --
10052 ------------------------------
10054 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
10055 Bas : constant Entity_Id := Base_Type (T);
10056 Candidates : constant Elist_Id := New_Elmt_List;
10057 Subp : Entity_Id;
10058 Formal : Entity_Id;
10060 procedure Check_Candidate;
10061 -- The operation is a candidate if its first parameter is a
10062 -- controlling operand of the desired type.
10064 -----------------------
10065 -- Check_Candidate; --
10066 -----------------------
10068 procedure Check_Candidate is
10069 begin
10070 Formal := First_Formal (Subp);
10072 if Present (Formal)
10073 and then Is_Controlling_Formal (Formal)
10074 and then
10075 (Base_Type (Etype (Formal)) = Bas
10076 or else
10077 (Is_Access_Type (Etype (Formal))
10078 and then Designated_Type (Etype (Formal)) = Bas))
10079 then
10080 Append_Elmt (Subp, Candidates);
10081 end if;
10082 end Check_Candidate;
10084 -- Start of processing for Collect_Generic_Type_Ops
10086 begin
10087 if Is_Derived_Type (T) then
10088 return Primitive_Operations (T);
10090 elsif Ekind (Scope (T)) in E_Procedure | E_Function then
10092 -- Scan the list of generic formals to find subprograms
10093 -- that may have a first controlling formal of the type.
10095 if Nkind (Unit_Declaration_Node (Scope (T))) =
10096 N_Generic_Subprogram_Declaration
10097 then
10098 declare
10099 Decl : Node_Id;
10101 begin
10102 Decl :=
10103 First (Generic_Formal_Declarations
10104 (Unit_Declaration_Node (Scope (T))));
10105 while Present (Decl) loop
10106 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
10107 Subp := Defining_Entity (Decl);
10108 Check_Candidate;
10109 end if;
10111 Next (Decl);
10112 end loop;
10113 end;
10114 end if;
10115 return Candidates;
10117 else
10118 -- Scan the list of entities declared in the same scope as
10119 -- the type. In general this will be an open scope, given that
10120 -- the call we are analyzing can only appear within a generic
10121 -- declaration or body (either the one that declares T, or a
10122 -- child unit).
10124 -- For a subtype representing a generic actual type, go to the
10125 -- base type.
10127 if Is_Generic_Actual_Type (T) then
10128 Subp := First_Entity (Scope (Base_Type (T)));
10129 else
10130 Subp := First_Entity (Scope (T));
10131 end if;
10133 while Present (Subp) loop
10134 if Is_Overloadable (Subp) then
10135 Check_Candidate;
10136 end if;
10138 Next_Entity (Subp);
10139 end loop;
10141 return Candidates;
10142 end if;
10143 end Collect_Generic_Type_Ops;
10145 ----------------------------
10146 -- Extended_Primitive_Ops --
10147 ----------------------------
10149 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
10150 Type_Scope : constant Entity_Id := Scope (T);
10151 Op_List : Elist_Id := Primitive_Operations (T);
10152 begin
10153 if Is_Package_Or_Generic_Package (Type_Scope)
10154 and then ((In_Package_Body (Type_Scope)
10155 and then In_Open_Scopes (Type_Scope)) or else In_Instance_Body)
10156 then
10157 -- Retrieve list of declarations of package body if possible
10159 declare
10160 The_Body : constant Node_Id :=
10161 Corresponding_Body (Unit_Declaration_Node (Type_Scope));
10162 begin
10163 if Present (The_Body) then
10164 declare
10165 Body_Decls : constant List_Id :=
10166 Declarations (Unit_Declaration_Node (The_Body));
10167 Op_Found : Boolean := False;
10168 Op : Entity_Id := Current_Entity (Subprog);
10169 begin
10170 while Present (Op) loop
10171 if Comes_From_Source (Op)
10172 and then Is_Overloadable (Op)
10174 -- Exclude overriding primitive operations of a
10175 -- type extension declared in the package body,
10176 -- to prevent duplicates in extended list.
10178 and then not Is_Primitive (Op)
10179 and then Is_List_Member
10180 (Unit_Declaration_Node (Op))
10181 and then List_Containing
10182 (Unit_Declaration_Node (Op)) = Body_Decls
10183 then
10184 if not Op_Found then
10185 -- Copy list of primitives so it is not
10186 -- affected for other uses.
10188 Op_List := New_Copy_Elist (Op_List);
10189 Op_Found := True;
10190 end if;
10192 Append_Elmt (Op, Op_List);
10193 end if;
10195 Op := Homonym (Op);
10196 end loop;
10197 end;
10198 end if;
10199 end;
10200 end if;
10202 return Op_List;
10203 end Extended_Primitive_Ops;
10205 ---------------------------
10206 -- Is_Private_Overriding --
10207 ---------------------------
10209 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
10210 Visible_Op : Entity_Id;
10212 begin
10213 -- The subprogram may be overloaded with both visible and private
10214 -- entities with the same name. We have to scan the chain of
10215 -- homonyms to determine whether there is a previous implicit
10216 -- declaration in the same scope that is overridden by the
10217 -- private candidate.
10219 Visible_Op := Homonym (Op);
10220 while Present (Visible_Op) loop
10221 if Scope (Op) /= Scope (Visible_Op) then
10222 return False;
10224 elsif not Comes_From_Source (Visible_Op)
10225 and then Alias (Visible_Op) = Op
10226 and then not Is_Hidden (Visible_Op)
10227 then
10228 return True;
10229 end if;
10231 Visible_Op := Homonym (Visible_Op);
10232 end loop;
10234 return False;
10235 end Is_Private_Overriding;
10237 -----------------
10238 -- Names_Match --
10239 -----------------
10241 function Names_Match
10242 (Obj_Type : Entity_Id;
10243 Prim_Op : Entity_Id;
10244 Subprog : Entity_Id) return Boolean is
10245 begin
10246 -- Common case: exact match
10248 if Chars (Prim_Op) = Chars (Subprog) then
10249 return True;
10251 -- For protected type primitives the expander may have built the
10252 -- name of the dispatching primitive prepending the type name to
10253 -- avoid conflicts with the name of the protected subprogram (see
10254 -- Exp_Ch9.Build_Selected_Name).
10256 elsif Is_Protected_Type (Obj_Type) then
10257 return
10258 Present (Original_Protected_Subprogram (Prim_Op))
10259 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
10260 Chars (Subprog);
10262 -- In an instance, the selector name may be a generic actual that
10263 -- renames a primitive operation of the type of the prefix.
10265 elsif In_Instance and then Present (Current_Entity (Subprog)) then
10266 declare
10267 Subp : constant Entity_Id := Current_Entity (Subprog);
10268 begin
10269 if Present (Subp)
10270 and then Is_Subprogram (Subp)
10271 and then Present (Renamed_Entity (Subp))
10272 and then Is_Generic_Actual_Subprogram (Subp)
10273 and then Chars (Renamed_Entity (Subp)) = Chars (Prim_Op)
10274 then
10275 return True;
10276 end if;
10277 end;
10278 end if;
10280 return False;
10281 end Names_Match;
10283 -----------------------------
10284 -- Valid_First_Argument_Of --
10285 -----------------------------
10287 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
10288 Typ : Entity_Id := Etype (First_Formal (Op));
10290 begin
10291 if Is_Concurrent_Type (Typ)
10292 and then Present (Corresponding_Record_Type (Typ))
10293 then
10294 Typ := Corresponding_Record_Type (Typ);
10295 end if;
10297 -- Simple case. Object may be a subtype of the tagged type or may
10298 -- be the corresponding record of a synchronized type.
10300 return Obj_Type = Typ
10301 or else Base_Type (Obj_Type) = Base_Type (Typ)
10302 or else Corr_Type = Typ
10304 -- Object may be of a derived type whose parent has unknown
10305 -- discriminants, in which case the type matches the underlying
10306 -- record view of its base.
10308 or else
10309 (Has_Unknown_Discriminants (Typ)
10310 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
10312 -- Prefix can be dereferenced
10314 or else
10315 (Is_Access_Type (Corr_Type)
10316 and then Designated_Type (Corr_Type) = Typ)
10318 -- Formal is an access parameter, for which the object can
10319 -- provide an access.
10321 or else
10322 (Ekind (Typ) = E_Anonymous_Access_Type
10323 and then
10324 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
10325 end Valid_First_Argument_Of;
10327 -- Start of processing for Try_Primitive_Operation
10329 begin
10330 -- Look for subprograms in the list of primitive operations. The name
10331 -- must be identical, and the kind of call indicates the expected
10332 -- kind of operation (function or procedure). If the type is a
10333 -- (tagged) synchronized type, the primitive ops are attached to the
10334 -- corresponding record (base) type.
10336 if Is_Concurrent_Type (Obj_Type) then
10337 if Present (Corresponding_Record_Type (Obj_Type)) then
10338 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
10339 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
10340 else
10341 Corr_Type := Obj_Type;
10342 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
10343 end if;
10345 elsif not Is_Generic_Type (Obj_Type) then
10346 Corr_Type := Obj_Type;
10347 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
10349 else
10350 Corr_Type := Obj_Type;
10351 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
10352 end if;
10354 while Present (Elmt) loop
10355 Prim_Op := Node (Elmt);
10357 if Names_Match (Obj_Type, Prim_Op, Subprog)
10358 and then Present (First_Formal (Prim_Op))
10359 and then Valid_First_Argument_Of (Prim_Op)
10360 and then
10361 (Nkind (Call_Node) = N_Function_Call)
10363 (Ekind (Prim_Op) = E_Function)
10364 then
10365 -- Ada 2005 (AI-251): If this primitive operation corresponds
10366 -- to an immediate ancestor interface there is no need to add
10367 -- it to the list of interpretations; the corresponding aliased
10368 -- primitive is also in this list of primitive operations and
10369 -- will be used instead.
10371 if (Present (Interface_Alias (Prim_Op))
10372 and then Is_Ancestor (Find_Dispatching_Type
10373 (Alias (Prim_Op)), Corr_Type))
10375 -- Do not consider hidden primitives unless the type is in an
10376 -- open scope or we are within an instance, where visibility
10377 -- is known to be correct, or else if this is an overriding
10378 -- operation in the private part for an inherited operation.
10380 or else (Is_Hidden (Prim_Op)
10381 and then not Is_Immediately_Visible (Obj_Type)
10382 and then not In_Instance
10383 and then not Is_Private_Overriding (Prim_Op))
10384 then
10385 goto Continue;
10386 end if;
10388 Set_Etype (Call_Node, Any_Type);
10389 Set_Is_Overloaded (Call_Node, False);
10391 if No (Matching_Op) then
10392 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
10393 Candidate := Prim_Op;
10395 Set_Parent (Call_Node, Parent (Node_To_Replace));
10397 Set_Name (Call_Node, Prim_Op_Ref);
10398 Success := False;
10400 Analyze_One_Call
10401 (N => Call_Node,
10402 Nam => Prim_Op,
10403 Report => Report_Error,
10404 Success => Success,
10405 Skip_First => True);
10407 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
10409 -- More than one interpretation, collect for subsequent
10410 -- disambiguation. If this is a procedure call and there
10411 -- is another match, report ambiguity now.
10413 else
10414 Analyze_One_Call
10415 (N => Call_Node,
10416 Nam => Prim_Op,
10417 Report => Report_Error,
10418 Success => Success,
10419 Skip_First => True);
10421 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
10422 and then Nkind (Call_Node) /= N_Function_Call
10423 then
10424 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
10425 Report_Ambiguity (Matching_Op);
10426 Report_Ambiguity (Prim_Op);
10427 Check_Ambiguous_Aggregate (Call_Node);
10428 return True;
10429 end if;
10430 end if;
10431 end if;
10433 <<Continue>>
10434 Next_Elmt (Elmt);
10435 end loop;
10437 if Present (Matching_Op) then
10438 Set_Etype (Call_Node, Etype (Matching_Op));
10439 end if;
10441 return Present (Matching_Op);
10442 end Try_Primitive_Operation;
10444 ---------------------
10445 -- Valid_Candidate --
10446 ---------------------
10448 function Valid_Candidate
10449 (Success : Boolean;
10450 Call : Node_Id;
10451 Subp : Entity_Id) return Entity_Id
10453 Arr_Type : Entity_Id;
10454 Comp_Type : Entity_Id;
10456 begin
10457 -- If the subprogram is a valid interpretation, record it in global
10458 -- variable Subprog, to collect all possible overloadings.
10460 if Success then
10461 if Subp /= Entity (Subprog) then
10462 Add_One_Interp (Subprog, Subp, Etype (Subp));
10463 end if;
10464 end if;
10466 -- If the call may be an indexed call, retrieve component type of
10467 -- resulting expression, and add possible interpretation.
10469 Arr_Type := Empty;
10470 Comp_Type := Empty;
10472 if Nkind (Call) = N_Function_Call
10473 and then Nkind (Parent (N)) = N_Indexed_Component
10474 and then Needs_One_Actual (Subp)
10475 then
10476 if Is_Array_Type (Etype (Subp)) then
10477 Arr_Type := Etype (Subp);
10479 elsif Is_Access_Type (Etype (Subp))
10480 and then Is_Array_Type (Designated_Type (Etype (Subp)))
10481 then
10482 Arr_Type := Designated_Type (Etype (Subp));
10483 end if;
10484 end if;
10486 if Present (Arr_Type) then
10488 -- Verify that the actuals (excluding the object) match the types
10489 -- of the indexes.
10491 declare
10492 Actual : Node_Id;
10493 Index : Node_Id;
10495 begin
10496 Actual := Next (First_Actual (Call));
10497 Index := First_Index (Arr_Type);
10498 while Present (Actual) and then Present (Index) loop
10499 if not Has_Compatible_Type (Actual, Etype (Index)) then
10500 Arr_Type := Empty;
10501 exit;
10502 end if;
10504 Next_Actual (Actual);
10505 Next_Index (Index);
10506 end loop;
10508 if No (Actual)
10509 and then No (Index)
10510 and then Present (Arr_Type)
10511 then
10512 Comp_Type := Component_Type (Arr_Type);
10513 end if;
10514 end;
10516 if Present (Comp_Type)
10517 and then Etype (Subprog) /= Comp_Type
10518 then
10519 Add_One_Interp (Subprog, Subp, Comp_Type);
10520 end if;
10521 end if;
10523 if Etype (Call) /= Any_Type then
10524 return Subp;
10525 else
10526 return Empty;
10527 end if;
10528 end Valid_Candidate;
10530 -- Start of processing for Try_Object_Operation
10532 begin
10533 Analyze_Expression (Obj);
10535 -- Analyze the actuals if node is known to be a subprogram call
10537 if Is_Subprg_Call and then N = Name (Parent (N)) then
10538 Actual := First (Parameter_Associations (Parent (N)));
10539 while Present (Actual) loop
10540 Analyze_Expression (Actual);
10541 Next (Actual);
10542 end loop;
10543 end if;
10545 -- Build a subprogram call node, using a copy of Obj as its first
10546 -- actual. This is a placeholder, to be replaced by an explicit
10547 -- dereference when needed.
10549 Transform_Object_Operation
10550 (Call_Node => New_Call_Node,
10551 Node_To_Replace => Node_To_Replace);
10553 Set_Etype (New_Call_Node, Any_Type);
10554 Set_Etype (Subprog, Any_Type);
10555 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
10557 if not Is_Overloaded (Obj) then
10558 Try_One_Prefix_Interpretation (Obj_Type);
10560 else
10561 declare
10562 I : Interp_Index;
10563 It : Interp;
10564 begin
10565 Get_First_Interp (Obj, I, It);
10566 while Present (It.Nam) loop
10567 Try_One_Prefix_Interpretation (It.Typ);
10568 Get_Next_Interp (I, It);
10569 end loop;
10570 end;
10571 end if;
10573 if Etype (New_Call_Node) /= Any_Type then
10575 -- No need to complete the tree transformations if we are only
10576 -- searching for conflicting class-wide subprograms
10578 if CW_Test_Only then
10579 return False;
10580 else
10581 Complete_Object_Operation
10582 (Call_Node => New_Call_Node,
10583 Node_To_Replace => Node_To_Replace);
10584 return True;
10585 end if;
10587 elsif Present (Candidate) then
10589 -- The argument list is not type correct. Re-analyze with error
10590 -- reporting enabled, and use one of the possible candidates.
10591 -- In All_Errors_Mode, re-analyze all failed interpretations.
10593 if All_Errors_Mode then
10594 Report_Error := True;
10595 if Try_Primitive_Operation
10596 (Call_Node => New_Call_Node,
10597 Node_To_Replace => Node_To_Replace)
10599 or else
10600 Try_Class_Wide_Operation
10601 (Call_Node => New_Call_Node,
10602 Node_To_Replace => Node_To_Replace)
10603 then
10604 null;
10605 end if;
10607 else
10608 Analyze_One_Call
10609 (N => New_Call_Node,
10610 Nam => Candidate,
10611 Report => True,
10612 Success => Success,
10613 Skip_First => True);
10615 -- The error may hot have been reported yet for overloaded
10616 -- prefixed calls, depending on the non-matching candidate,
10617 -- in which case provide a concise error now.
10619 if Serious_Errors_Detected = 0 then
10620 Error_Msg_NE
10621 ("cannot resolve prefixed call to primitive operation of&",
10622 N, Entity (Obj));
10623 end if;
10624 end if;
10626 -- No need for further errors
10628 return True;
10630 else
10631 -- There was no candidate operation, but Analyze_Selected_Component
10632 -- may continue the analysis so we need to undo the change possibly
10633 -- made to the Parent of N earlier by Transform_Object_Operation.
10635 declare
10636 Parent_Node : constant Node_Id := Parent (N);
10638 begin
10639 if Node_To_Replace = Parent_Node then
10640 Remove (First (Parameter_Associations (New_Call_Node)));
10641 Set_Parent
10642 (Parameter_Associations (New_Call_Node), Parent_Node);
10643 end if;
10644 end;
10646 return False;
10647 end if;
10648 end Try_Object_Operation;
10650 -------------------------
10651 -- Unresolved_Operator --
10652 -------------------------
10654 procedure Unresolved_Operator (N : Node_Id) is
10655 L : constant Node_Id :=
10656 (if Nkind (N) in N_Binary_Op then Left_Opnd (N) else Empty);
10657 R : constant Node_Id := Right_Opnd (N);
10659 Op_Id : Entity_Id;
10661 begin
10662 -- Note that in the following messages, if the operand is overloaded we
10663 -- choose an arbitrary type to complain about, but that is probably more
10664 -- useful than not giving a type at all.
10666 if Nkind (N) in N_Unary_Op then
10667 Error_Msg_Node_2 := Etype (R);
10668 Error_Msg_N ("operator& not defined for}", N);
10670 elsif Nkind (N) in N_Binary_Op then
10671 if not Is_Overloaded (L)
10672 and then not Is_Overloaded (R)
10673 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
10674 then
10675 Error_Msg_Node_2 := First_Subtype (Etype (R));
10676 Error_Msg_N ("there is no applicable operator& for}", N);
10678 else
10679 -- Another attempt to find a fix: one of the candidate
10680 -- interpretations may not be use-visible. This has
10681 -- already been checked for predefined operators, so
10682 -- we examine only user-defined functions.
10684 Op_Id := Get_Name_Entity_Id (Chars (N));
10686 while Present (Op_Id) loop
10687 if Ekind (Op_Id) /= E_Operator
10688 and then Is_Overloadable (Op_Id)
10689 and then not Is_Immediately_Visible (Op_Id)
10690 and then not In_Use (Scope (Op_Id))
10691 and then not Is_Abstract_Subprogram (Op_Id)
10692 and then not Is_Hidden (Op_Id)
10693 and then Ekind (Scope (Op_Id)) = E_Package
10694 and then Has_Compatible_Type (L, Etype (First_Formal (Op_Id)))
10695 and then Present (Next_Formal (First_Formal (Op_Id)))
10696 and then
10697 Has_Compatible_Type
10698 (R, Etype (Next_Formal (First_Formal (Op_Id))))
10699 then
10700 Error_Msg_N ("no legal interpretation for operator&", N);
10701 Error_Msg_NE ("\use clause on& would make operation legal",
10702 N, Scope (Op_Id));
10703 exit;
10704 end if;
10706 Op_Id := Homonym (Op_Id);
10707 end loop;
10709 if No (Op_Id) then
10710 Error_Msg_N ("invalid operand types for operator&", N);
10712 if Nkind (N) /= N_Op_Concat then
10713 Error_Msg_NE ("\left operand has}!", N, Etype (L));
10714 Error_Msg_NE ("\right operand has}!", N, Etype (R));
10716 -- For multiplication and division operators with
10717 -- a fixed-point operand and an integer operand,
10718 -- indicate that the integer operand should be of
10719 -- type Integer.
10721 if Nkind (N) in N_Op_Multiply | N_Op_Divide
10722 and then Is_Fixed_Point_Type (Etype (L))
10723 and then Is_Integer_Type (Etype (R))
10724 then
10725 Error_Msg_N ("\convert right operand to `Integer`", N);
10727 elsif Nkind (N) = N_Op_Multiply
10728 and then Is_Fixed_Point_Type (Etype (R))
10729 and then Is_Integer_Type (Etype (L))
10730 then
10731 Error_Msg_N ("\convert left operand to `Integer`", N);
10732 end if;
10734 -- For concatenation operators it is more difficult to
10735 -- determine which is the wrong operand. It is worth
10736 -- flagging explicitly an access type, for those who
10737 -- might think that a dereference happens here.
10739 elsif Is_Access_Type (Etype (L)) then
10740 Error_Msg_N ("\left operand is access type", N);
10742 elsif Is_Access_Type (Etype (R)) then
10743 Error_Msg_N ("\right operand is access type", N);
10744 end if;
10745 end if;
10746 end if;
10747 end if;
10748 end Unresolved_Operator;
10750 ---------
10751 -- wpo --
10752 ---------
10754 procedure wpo (T : Entity_Id) is
10755 Op : Entity_Id;
10756 E : Elmt_Id;
10758 begin
10759 if not Is_Tagged_Type (T) then
10760 return;
10761 end if;
10763 E := First_Elmt (Primitive_Operations (Base_Type (T)));
10764 while Present (E) loop
10765 Op := Node (E);
10766 Write_Int (Int (Op));
10767 Write_Str (" === ");
10768 Write_Name (Chars (Op));
10769 Write_Str (" in ");
10770 Write_Name (Chars (Scope (Op)));
10771 Next_Elmt (E);
10772 Write_Eol;
10773 end loop;
10774 end wpo;
10776 end Sem_Ch4;