]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch4.adb
16614edd98584e843fbece134ad8ee734768a2c3
[thirdparty/gcc.git] / gcc / ada / sem_ch4.adb
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-2019, 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 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Itypes; use Itypes;
34 with Lib; use Lib;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Namet.Sp; use Namet.Sp;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Output; use Output;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Case; use Sem_Case;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Dist; use Sem_Dist;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Type; use Sem_Type;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
64
65 package body Sem_Ch4 is
66
67 -- Tables which speed up the identification of dangerous calls to Ada 2012
68 -- functions with writable actuals (AI05-0144).
69
70 -- The following table enumerates the Ada constructs which may evaluate in
71 -- arbitrary order. It does not cover all the language constructs which can
72 -- be evaluated in arbitrary order but the subset needed for AI05-0144.
73
74 Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean :=
75 (N_Aggregate => True,
76 N_Assignment_Statement => True,
77 N_Entry_Call_Statement => True,
78 N_Extension_Aggregate => True,
79 N_Full_Type_Declaration => True,
80 N_Indexed_Component => True,
81 N_Object_Declaration => True,
82 N_Pragma => True,
83 N_Range => True,
84 N_Slice => True,
85 N_Array_Type_Definition => True,
86 N_Membership_Test => True,
87 N_Binary_Op => True,
88 N_Subprogram_Call => True,
89 others => False);
90
91 -- The following table enumerates the nodes on which we stop climbing when
92 -- locating the outermost Ada construct that can be evaluated in arbitrary
93 -- order.
94
95 Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean :=
96 (N_Aggregate => True,
97 N_Assignment_Statement => True,
98 N_Entry_Call_Statement => True,
99 N_Extended_Return_Statement => True,
100 N_Extension_Aggregate => True,
101 N_Full_Type_Declaration => True,
102 N_Object_Declaration => True,
103 N_Object_Renaming_Declaration => True,
104 N_Package_Specification => True,
105 N_Pragma => True,
106 N_Procedure_Call_Statement => True,
107 N_Simple_Return_Statement => True,
108 N_Has_Condition => True,
109 others => False);
110
111 -----------------------
112 -- Local Subprograms --
113 -----------------------
114
115 procedure Analyze_Concatenation_Rest (N : Node_Id);
116 -- Does the "rest" of the work of Analyze_Concatenation, after the left
117 -- operand has been analyzed. See Analyze_Concatenation for details.
118
119 procedure Analyze_Expression (N : Node_Id);
120 -- For expressions that are not names, this is just a call to analyze. If
121 -- the expression is a name, it may be a call to a parameterless function,
122 -- and if so must be converted into an explicit call node and analyzed as
123 -- such. This deproceduring must be done during the first pass of overload
124 -- resolution, because otherwise a procedure call with overloaded actuals
125 -- may fail to resolve.
126
127 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
128 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an
129 -- operator name or an expanded name whose selector is an operator name,
130 -- and one possible interpretation is as a predefined operator.
131
132 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
133 -- If the prefix of a selected_component is overloaded, the proper
134 -- interpretation that yields a record type with the proper selector
135 -- name must be selected.
136
137 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
138 -- Procedure to analyze a user defined binary operator, which is resolved
139 -- like a function, but instead of a list of actuals it is presented
140 -- with the left and right operands of an operator node.
141
142 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
143 -- Procedure to analyze a user defined unary operator, which is resolved
144 -- like a function, but instead of a list of actuals, it is presented with
145 -- the operand of the operator node.
146
147 procedure Ambiguous_Operands (N : Node_Id);
148 -- For equality, membership, and comparison operators with overloaded
149 -- arguments, list possible interpretations.
150
151 procedure Analyze_One_Call
152 (N : Node_Id;
153 Nam : Entity_Id;
154 Report : Boolean;
155 Success : out Boolean;
156 Skip_First : Boolean := False);
157 -- Check one interpretation of an overloaded subprogram name for
158 -- compatibility with the types of the actuals in a call. If there is a
159 -- single interpretation which does not match, post error if Report is
160 -- set to True.
161 --
162 -- Nam is the entity that provides the formals against which the actuals
163 -- are checked. Nam is either the name of a subprogram, or the internal
164 -- subprogram type constructed for an access_to_subprogram. If the actuals
165 -- are compatible with Nam, then Nam is added to the list of candidate
166 -- interpretations for N, and Success is set to True.
167 --
168 -- The flag Skip_First is used when analyzing a call that was rewritten
169 -- from object notation. In this case the first actual may have to receive
170 -- an explicit dereference, depending on the first formal of the operation
171 -- being called. The caller will have verified that the object is legal
172 -- for the call. If the remaining parameters match, the first parameter
173 -- will rewritten as a dereference if needed, prior to completing analysis.
174 procedure Check_Misspelled_Selector
175 (Prefix : Entity_Id;
176 Sel : Node_Id);
177 -- Give possible misspelling message if Sel seems likely to be a mis-
178 -- spelling of one of the selectors of the Prefix. This is called by
179 -- Analyze_Selected_Component after producing an invalid selector error
180 -- message.
181
182 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
183 -- Verify that type T is declared in scope S. Used to find interpretations
184 -- for operators given by expanded names. This is abstracted as a separate
185 -- function to handle extensions to System, where S is System, but T is
186 -- declared in the extension.
187
188 procedure Find_Arithmetic_Types
189 (L, R : Node_Id;
190 Op_Id : Entity_Id;
191 N : Node_Id);
192 -- L and R are the operands of an arithmetic operator. Find consistent
193 -- pairs of interpretations for L and R that have a numeric type consistent
194 -- with the semantics of the operator.
195
196 procedure Find_Comparison_Types
197 (L, R : Node_Id;
198 Op_Id : Entity_Id;
199 N : Node_Id);
200 -- L and R are operands of a comparison operator. Find consistent pairs of
201 -- interpretations for L and R.
202
203 procedure Find_Concatenation_Types
204 (L, R : Node_Id;
205 Op_Id : Entity_Id;
206 N : Node_Id);
207 -- For the four varieties of concatenation
208
209 procedure Find_Equality_Types
210 (L, R : Node_Id;
211 Op_Id : Entity_Id;
212 N : Node_Id);
213 -- Ditto for equality operators
214
215 procedure Find_Boolean_Types
216 (L, R : Node_Id;
217 Op_Id : Entity_Id;
218 N : Node_Id);
219 -- Ditto for binary logical operations
220
221 procedure Find_Negation_Types
222 (R : Node_Id;
223 Op_Id : Entity_Id;
224 N : Node_Id);
225 -- Find consistent interpretation for operand of negation operator
226
227 procedure Find_Non_Universal_Interpretations
228 (N : Node_Id;
229 R : Node_Id;
230 Op_Id : Entity_Id;
231 T1 : Entity_Id);
232 -- For equality and comparison operators, the result is always boolean, and
233 -- the legality of the operation is determined from the visibility of the
234 -- operand types. If one of the operands has a universal interpretation,
235 -- the legality check uses some compatible non-universal interpretation of
236 -- the other operand. N can be an operator node, or a function call whose
237 -- name is an operator designator. Any_Access, which is the initial type of
238 -- the literal NULL, is a universal type for the purpose of this routine.
239
240 function Find_Primitive_Operation (N : Node_Id) return Boolean;
241 -- Find candidate interpretations for the name Obj.Proc when it appears in
242 -- a subprogram renaming declaration.
243
244 procedure Find_Unary_Types
245 (R : Node_Id;
246 Op_Id : Entity_Id;
247 N : Node_Id);
248 -- Unary arithmetic types: plus, minus, abs
249
250 procedure Check_Arithmetic_Pair
251 (T1, T2 : Entity_Id;
252 Op_Id : Entity_Id;
253 N : Node_Id);
254 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
255 -- for left and right operand. Determine whether they constitute a valid
256 -- pair for the given operator, and record the corresponding interpretation
257 -- of the operator node. The node N may be an operator node (the usual
258 -- case) or a function call whose prefix is an operator designator. In
259 -- both cases Op_Id is the operator name itself.
260
261 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
262 -- Give detailed information on overloaded call where none of the
263 -- interpretations match. N is the call node, Nam the designator for
264 -- the overloaded entity being called.
265
266 function Junk_Operand (N : Node_Id) return Boolean;
267 -- Test for an operand that is an inappropriate entity (e.g. a package
268 -- name or a label). If so, issue an error message and return True. If
269 -- the operand is not an inappropriate entity kind, return False.
270
271 procedure Operator_Check (N : Node_Id);
272 -- Verify that an operator has received some valid interpretation. If none
273 -- was found, determine whether a use clause would make the operation
274 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
275 -- every type compatible with the operator, even if the operator for the
276 -- type is not directly visible. The routine uses this type to emit a more
277 -- informative message.
278
279 function Process_Implicit_Dereference_Prefix
280 (E : Entity_Id;
281 P : Node_Id) return Entity_Id;
282 -- Called when P is the prefix of an implicit dereference, denoting an
283 -- object E. The function returns the designated type of the prefix, taking
284 -- into account that the designated type of an anonymous access type may be
285 -- a limited view, when the nonlimited view is visible.
286 --
287 -- If in semantics only mode (-gnatc or generic), the function also records
288 -- that the prefix is a reference to E, if any. Normally, such a reference
289 -- is generated only when the implicit dereference is expanded into an
290 -- explicit one, but for consistency we must generate the reference when
291 -- expansion is disabled as well.
292
293 procedure Remove_Abstract_Operations (N : Node_Id);
294 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
295 -- operation is not a candidate interpretation.
296
297 function Try_Container_Indexing
298 (N : Node_Id;
299 Prefix : Node_Id;
300 Exprs : List_Id) return Boolean;
301 -- AI05-0139: Generalized indexing to support iterators over containers
302
303 function Try_Indexed_Call
304 (N : Node_Id;
305 Nam : Entity_Id;
306 Typ : Entity_Id;
307 Skip_First : Boolean) return Boolean;
308 -- If a function has defaults for all its actuals, a call to it may in fact
309 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
310 -- interpretation as an indexing, prior to analysis as a call. If both are
311 -- possible, the node is overloaded with both interpretations (same symbol
312 -- but two different types). If the call is written in prefix form, the
313 -- prefix becomes the first parameter in the call, and only the remaining
314 -- actuals must be checked for the presence of defaults.
315
316 function Try_Indirect_Call
317 (N : Node_Id;
318 Nam : Entity_Id;
319 Typ : Entity_Id) return Boolean;
320 -- Similarly, a function F that needs no actuals can return an access to a
321 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
322 -- the call may be overloaded with both interpretations.
323
324 procedure wpo (T : Entity_Id);
325 pragma Warnings (Off, wpo);
326 -- Used for debugging: obtain list of primitive operations even if
327 -- type is not frozen and dispatch table is not built yet.
328
329 ------------------------
330 -- Ambiguous_Operands --
331 ------------------------
332
333 procedure Ambiguous_Operands (N : Node_Id) is
334 procedure List_Operand_Interps (Opnd : Node_Id);
335
336 --------------------------
337 -- List_Operand_Interps --
338 --------------------------
339
340 procedure List_Operand_Interps (Opnd : Node_Id) is
341 Nam : Node_Id := Empty;
342 Err : Node_Id := N;
343
344 begin
345 if Is_Overloaded (Opnd) then
346 if Nkind (Opnd) in N_Op then
347 Nam := Opnd;
348
349 elsif Nkind (Opnd) = N_Function_Call then
350 Nam := Name (Opnd);
351
352 elsif Ada_Version >= Ada_2012 then
353 declare
354 It : Interp;
355 I : Interp_Index;
356
357 begin
358 Get_First_Interp (Opnd, I, It);
359 while Present (It.Nam) loop
360 if Has_Implicit_Dereference (It.Typ) then
361 Error_Msg_N
362 ("can be interpreted as implicit dereference", Opnd);
363 return;
364 end if;
365
366 Get_Next_Interp (I, It);
367 end loop;
368 end;
369
370 return;
371 end if;
372
373 else
374 return;
375 end if;
376
377 if Opnd = Left_Opnd (N) then
378 Error_Msg_N
379 ("\left operand has the following interpretations", N);
380 else
381 Error_Msg_N
382 ("\right operand has the following interpretations", N);
383 Err := Opnd;
384 end if;
385
386 List_Interps (Nam, Err);
387 end List_Operand_Interps;
388
389 -- Start of processing for Ambiguous_Operands
390
391 begin
392 if Nkind (N) in N_Membership_Test then
393 Error_Msg_N ("ambiguous operands for membership", N);
394
395 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
396 Error_Msg_N ("ambiguous operands for equality", N);
397
398 else
399 Error_Msg_N ("ambiguous operands for comparison", N);
400 end if;
401
402 if All_Errors_Mode then
403 List_Operand_Interps (Left_Opnd (N));
404 List_Operand_Interps (Right_Opnd (N));
405 else
406 Error_Msg_N ("\use -gnatf switch for details", N);
407 end if;
408 end Ambiguous_Operands;
409
410 -----------------------
411 -- Analyze_Aggregate --
412 -----------------------
413
414 -- Most of the analysis of Aggregates requires that the type be known, and
415 -- is therefore put off until resolution of the context. Delta aggregates
416 -- have a base component that determines the enclosing aggregate type so
417 -- its type can be ascertained earlier. This also allows delta aggregates
418 -- to appear in the context of a record type with a private extension, as
419 -- per the latest update of AI12-0127.
420
421 procedure Analyze_Aggregate (N : Node_Id) is
422 begin
423 if No (Etype (N)) then
424 if Nkind (N) = N_Delta_Aggregate then
425 declare
426 Base : constant Node_Id := Expression (N);
427
428 I : Interp_Index;
429 It : Interp;
430
431 begin
432 Analyze (Base);
433
434 -- If the base is overloaded, propagate interpretations to the
435 -- enclosing aggregate.
436
437 if Is_Overloaded (Base) then
438 Get_First_Interp (Base, I, It);
439 Set_Etype (N, Any_Type);
440
441 while Present (It.Nam) loop
442 Add_One_Interp (N, It.Typ, It.Typ);
443 Get_Next_Interp (I, It);
444 end loop;
445
446 else
447 Set_Etype (N, Etype (Base));
448 end if;
449 end;
450
451 else
452 Set_Etype (N, Any_Composite);
453 end if;
454 end if;
455 end Analyze_Aggregate;
456
457 -----------------------
458 -- Analyze_Allocator --
459 -----------------------
460
461 procedure Analyze_Allocator (N : Node_Id) is
462 Loc : constant Source_Ptr := Sloc (N);
463 Sav_Errs : constant Nat := Serious_Errors_Detected;
464 E : Node_Id := Expression (N);
465 Acc_Type : Entity_Id;
466 Type_Id : Entity_Id;
467 P : Node_Id;
468 C : Node_Id;
469 Onode : Node_Id;
470
471 begin
472 Check_SPARK_05_Restriction ("allocator is not allowed", N);
473
474 -- Deal with allocator restrictions
475
476 -- In accordance with H.4(7), the No_Allocators restriction only applies
477 -- to user-written allocators. The same consideration applies to the
478 -- No_Standard_Allocators_Before_Elaboration restriction.
479
480 if Comes_From_Source (N) then
481 Check_Restriction (No_Allocators, N);
482
483 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
484 -- look at enclosing context, checking task/main subprogram case.
485
486 C := N;
487 P := Parent (C);
488 while Present (P) loop
489
490 -- For the task case we need a handled sequence of statements,
491 -- where the occurrence of the allocator is within the statements
492 -- and the parent is a task body
493
494 if Nkind (P) = N_Handled_Sequence_Of_Statements
495 and then Is_List_Member (C)
496 and then List_Containing (C) = Statements (P)
497 then
498 Onode := Original_Node (Parent (P));
499
500 -- Check for allocator within task body, this is a definite
501 -- violation of No_Allocators_After_Elaboration we can detect
502 -- at compile time.
503
504 if Nkind (Onode) = N_Task_Body then
505 Check_Restriction
506 (No_Standard_Allocators_After_Elaboration, N);
507 exit;
508 end if;
509 end if;
510
511 -- The other case is appearance in a subprogram body. This is
512 -- a violation if this is a library level subprogram with no
513 -- parameters. Note that this is now a static error even if the
514 -- subprogram is not the main program (this is a change, in an
515 -- earlier version only the main program was affected, and the
516 -- check had to be done in the binder.
517
518 if Nkind (P) = N_Subprogram_Body
519 and then Nkind (Parent (P)) = N_Compilation_Unit
520 and then No (Parameter_Specifications (Specification (P)))
521 then
522 Check_Restriction
523 (No_Standard_Allocators_After_Elaboration, N);
524 end if;
525
526 C := P;
527 P := Parent (C);
528 end loop;
529 end if;
530
531 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
532 -- any. The expected type for the name is any type. A non-overloading
533 -- rule then requires it to be of a type descended from
534 -- System.Storage_Pools.Subpools.Subpool_Handle.
535
536 -- This isn't exactly what the AI says, but it seems to be the right
537 -- rule. The AI should be fixed.???
538
539 declare
540 Subpool : constant Node_Id := Subpool_Handle_Name (N);
541
542 begin
543 if Present (Subpool) then
544 Analyze (Subpool);
545
546 if Is_Overloaded (Subpool) then
547 Error_Msg_N ("ambiguous subpool handle", Subpool);
548 end if;
549
550 -- Check that Etype (Subpool) is descended from Subpool_Handle
551
552 Resolve (Subpool);
553 end if;
554 end;
555
556 -- Analyze the qualified expression or subtype indication
557
558 if Nkind (E) = N_Qualified_Expression then
559 Acc_Type := Create_Itype (E_Allocator_Type, N);
560 Set_Etype (Acc_Type, Acc_Type);
561 Find_Type (Subtype_Mark (E));
562
563 -- Analyze the qualified expression, and apply the name resolution
564 -- rule given in 4.7(3).
565
566 Analyze (E);
567 Type_Id := Etype (E);
568 Set_Directly_Designated_Type (Acc_Type, Type_Id);
569
570 -- A qualified expression requires an exact match of the type,
571 -- class-wide matching is not allowed.
572
573 -- if Is_Class_Wide_Type (Type_Id)
574 -- and then Base_Type
575 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
576 -- then
577 -- Wrong_Type (Expression (E), Type_Id);
578 -- end if;
579
580 -- We don't analyze the qualified expression itself because it's
581 -- part of the allocator. It is fully analyzed and resolved when
582 -- the allocator is resolved with the context type.
583
584 Set_Etype (E, Type_Id);
585
586 -- Case where allocator has a subtype indication
587
588 else
589 declare
590 Def_Id : Entity_Id;
591 Base_Typ : Entity_Id;
592
593 begin
594 -- If the allocator includes a N_Subtype_Indication then a
595 -- constraint is present, otherwise the node is a subtype mark.
596 -- Introduce an explicit subtype declaration into the tree
597 -- defining some anonymous subtype and rewrite the allocator to
598 -- use this subtype rather than the subtype indication.
599
600 -- It is important to introduce the explicit subtype declaration
601 -- so that the bounds of the subtype indication are attached to
602 -- the tree in case the allocator is inside a generic unit.
603
604 -- Finally, if there is no subtype indication and the type is
605 -- a tagged unconstrained type with discriminants, the designated
606 -- object is constrained by their default values, and it is
607 -- simplest to introduce an explicit constraint now. In some cases
608 -- this is done during expansion, but freeze actions are certain
609 -- to be emitted in the proper order if constraint is explicit.
610
611 if Is_Entity_Name (E) and then Expander_Active then
612 Find_Type (E);
613 Type_Id := Entity (E);
614
615 if Is_Tagged_Type (Type_Id)
616 and then Has_Discriminants (Type_Id)
617 and then not Is_Constrained (Type_Id)
618 and then
619 Present
620 (Discriminant_Default_Value
621 (First_Discriminant (Type_Id)))
622 then
623 declare
624 Constr : constant List_Id := New_List;
625 Loc : constant Source_Ptr := Sloc (E);
626 Discr : Entity_Id := First_Discriminant (Type_Id);
627
628 begin
629 if Present (Discriminant_Default_Value (Discr)) then
630 while Present (Discr) loop
631 Append (Discriminant_Default_Value (Discr), Constr);
632 Next_Discriminant (Discr);
633 end loop;
634
635 Rewrite (E,
636 Make_Subtype_Indication (Loc,
637 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
638 Constraint =>
639 Make_Index_Or_Discriminant_Constraint (Loc,
640 Constraints => Constr)));
641 end if;
642 end;
643 end if;
644 end if;
645
646 if Nkind (E) = N_Subtype_Indication then
647
648 -- A constraint is only allowed for a composite type in Ada
649 -- 95. In Ada 83, a constraint is also allowed for an
650 -- access-to-composite type, but the constraint is ignored.
651
652 Find_Type (Subtype_Mark (E));
653 Base_Typ := Entity (Subtype_Mark (E));
654
655 if Is_Elementary_Type (Base_Typ) then
656 if not (Ada_Version = Ada_83
657 and then Is_Access_Type (Base_Typ))
658 then
659 Error_Msg_N ("constraint not allowed here", E);
660
661 if Nkind (Constraint (E)) =
662 N_Index_Or_Discriminant_Constraint
663 then
664 Error_Msg_N -- CODEFIX
665 ("\if qualified expression was meant, " &
666 "use apostrophe", Constraint (E));
667 end if;
668 end if;
669
670 -- Get rid of the bogus constraint:
671
672 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
673 Analyze_Allocator (N);
674 return;
675 end if;
676
677 -- In GNATprove mode we need to preserve the link between
678 -- the original subtype indication and the anonymous subtype,
679 -- to extend proofs to constrained acccess types. We only do
680 -- that outside of spec expressions, otherwise the declaration
681 -- cannot be inserted and analyzed. In such a case, GNATprove
682 -- later rejects the allocator as it is not used here in
683 -- a non-interfering context (SPARK 4.8(2) and 7.1.3(12)).
684
685 if Expander_Active
686 or else (GNATprove_Mode and then not In_Spec_Expression)
687 then
688 Def_Id := Make_Temporary (Loc, 'S');
689
690 Insert_Action (E,
691 Make_Subtype_Declaration (Loc,
692 Defining_Identifier => Def_Id,
693 Subtype_Indication => Relocate_Node (E)));
694
695 if Sav_Errs /= Serious_Errors_Detected
696 and then Nkind (Constraint (E)) =
697 N_Index_Or_Discriminant_Constraint
698 then
699 Error_Msg_N -- CODEFIX
700 ("if qualified expression was meant, "
701 & "use apostrophe!", Constraint (E));
702 end if;
703
704 E := New_Occurrence_Of (Def_Id, Loc);
705 Rewrite (Expression (N), E);
706 end if;
707 end if;
708
709 Type_Id := Process_Subtype (E, N);
710 Acc_Type := Create_Itype (E_Allocator_Type, N);
711 Set_Etype (Acc_Type, Acc_Type);
712 Set_Directly_Designated_Type (Acc_Type, Type_Id);
713 Check_Fully_Declared (Type_Id, N);
714
715 -- Ada 2005 (AI-231): If the designated type is itself an access
716 -- type that excludes null, its default initialization will
717 -- be a null object, and we can insert an unconditional raise
718 -- before the allocator.
719
720 -- Ada 2012 (AI-104): A not null indication here is altogether
721 -- illegal.
722
723 if Can_Never_Be_Null (Type_Id) then
724 declare
725 Not_Null_Check : constant Node_Id :=
726 Make_Raise_Constraint_Error (Sloc (E),
727 Reason => CE_Null_Not_Allowed);
728
729 begin
730 if Expander_Active then
731 Insert_Action (N, Not_Null_Check);
732 Analyze (Not_Null_Check);
733
734 elsif Warn_On_Ada_2012_Compatibility then
735 Error_Msg_N
736 ("null value not allowed here in Ada 2012?y?", E);
737 end if;
738 end;
739 end if;
740
741 -- Check for missing initialization. Skip this check if we already
742 -- had errors on analyzing the allocator, since in that case these
743 -- are probably cascaded errors.
744
745 if not Is_Definite_Subtype (Type_Id)
746 and then Serious_Errors_Detected = Sav_Errs
747 then
748 -- The build-in-place machinery may produce an allocator when
749 -- the designated type is indefinite but the underlying type is
750 -- not. In this case the unknown discriminants are meaningless
751 -- and should not trigger error messages. Check the parent node
752 -- because the allocator is marked as coming from source.
753
754 if Present (Underlying_Type (Type_Id))
755 and then Is_Definite_Subtype (Underlying_Type (Type_Id))
756 and then not Comes_From_Source (Parent (N))
757 then
758 null;
759
760 -- An unusual case arises when the parent of a derived type is
761 -- a limited record extension with unknown discriminants, and
762 -- its full view has no discriminants.
763 --
764 -- A more general fix might be to create the proper underlying
765 -- type for such a derived type, but it is a record type with
766 -- no private attributes, so this required extending the
767 -- meaning of this attribute. ???
768
769 elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private
770 and then Present (Underlying_Type (Etype (Type_Id)))
771 and then
772 not Has_Discriminants (Underlying_Type (Etype (Type_Id)))
773 and then not Comes_From_Source (Parent (N))
774 then
775 null;
776
777 elsif Is_Class_Wide_Type (Type_Id) then
778 Error_Msg_N
779 ("initialization required in class-wide allocation", N);
780
781 else
782 if Ada_Version < Ada_2005
783 and then Is_Limited_Type (Type_Id)
784 then
785 Error_Msg_N ("unconstrained allocation not allowed", N);
786
787 if Is_Array_Type (Type_Id) then
788 Error_Msg_N
789 ("\constraint with array bounds required", N);
790
791 elsif Has_Unknown_Discriminants (Type_Id) then
792 null;
793
794 else pragma Assert (Has_Discriminants (Type_Id));
795 Error_Msg_N
796 ("\constraint with discriminant values required", N);
797 end if;
798
799 -- Limited Ada 2005 and general nonlimited case
800
801 else
802 Error_Msg_N
803 ("uninitialized unconstrained allocation not "
804 & "allowed", N);
805
806 if Is_Array_Type (Type_Id) then
807 Error_Msg_N
808 ("\qualified expression or constraint with "
809 & "array bounds required", N);
810
811 elsif Has_Unknown_Discriminants (Type_Id) then
812 Error_Msg_N ("\qualified expression required", N);
813
814 else pragma Assert (Has_Discriminants (Type_Id));
815 Error_Msg_N
816 ("\qualified expression or constraint with "
817 & "discriminant values required", N);
818 end if;
819 end if;
820 end if;
821 end if;
822 end;
823 end if;
824
825 if Is_Abstract_Type (Type_Id) then
826 Error_Msg_N ("cannot allocate abstract object", E);
827 end if;
828
829 if Has_Task (Designated_Type (Acc_Type)) then
830 Check_Restriction (No_Tasking, N);
831 Check_Restriction (Max_Tasks, N);
832 Check_Restriction (No_Task_Allocators, N);
833 end if;
834
835 -- Check restriction against dynamically allocated protected objects
836
837 if Has_Protected (Designated_Type (Acc_Type)) then
838 Check_Restriction (No_Protected_Type_Allocators, N);
839 end if;
840
841 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
842 -- type is nested, and the designated type needs finalization. The rule
843 -- is conservative in that class-wide types need finalization.
844
845 if Needs_Finalization (Designated_Type (Acc_Type))
846 and then not Is_Library_Level_Entity (Acc_Type)
847 then
848 Check_Restriction (No_Nested_Finalization, N);
849 end if;
850
851 -- Check that an allocator of a nested access type doesn't create a
852 -- protected object when restriction No_Local_Protected_Objects applies.
853
854 if Has_Protected (Designated_Type (Acc_Type))
855 and then not Is_Library_Level_Entity (Acc_Type)
856 then
857 Check_Restriction (No_Local_Protected_Objects, N);
858 end if;
859
860 -- Likewise for No_Local_Timing_Events
861
862 if Has_Timing_Event (Designated_Type (Acc_Type))
863 and then not Is_Library_Level_Entity (Acc_Type)
864 then
865 Check_Restriction (No_Local_Timing_Events, N);
866 end if;
867
868 -- If the No_Streams restriction is set, check that the type of the
869 -- object is not, and does not contain, any subtype derived from
870 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
871 -- Has_Stream just for efficiency reasons. There is no point in
872 -- spending time on a Has_Stream check if the restriction is not set.
873
874 if Restriction_Check_Required (No_Streams) then
875 if Has_Stream (Designated_Type (Acc_Type)) then
876 Check_Restriction (No_Streams, N);
877 end if;
878 end if;
879
880 Set_Etype (N, Acc_Type);
881
882 if not Is_Library_Level_Entity (Acc_Type) then
883 Check_Restriction (No_Local_Allocators, N);
884 end if;
885
886 if Serious_Errors_Detected > Sav_Errs then
887 Set_Error_Posted (N);
888 Set_Etype (N, Any_Type);
889 end if;
890 end Analyze_Allocator;
891
892 ---------------------------
893 -- Analyze_Arithmetic_Op --
894 ---------------------------
895
896 procedure Analyze_Arithmetic_Op (N : Node_Id) is
897 L : constant Node_Id := Left_Opnd (N);
898 R : constant Node_Id := Right_Opnd (N);
899 Op_Id : Entity_Id;
900
901 begin
902 Candidate_Type := Empty;
903 Analyze_Expression (L);
904 Analyze_Expression (R);
905
906 -- If the entity is already set, the node is the instantiation of a
907 -- generic node with a non-local reference, or was manufactured by a
908 -- call to Make_Op_xxx. In either case the entity is known to be valid,
909 -- and we do not need to collect interpretations, instead we just get
910 -- the single possible interpretation.
911
912 Op_Id := Entity (N);
913
914 if Present (Op_Id) then
915 if Ekind (Op_Id) = E_Operator then
916
917 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
918 and then Treat_Fixed_As_Integer (N)
919 then
920 null;
921 else
922 Set_Etype (N, Any_Type);
923 Find_Arithmetic_Types (L, R, Op_Id, N);
924 end if;
925
926 else
927 Set_Etype (N, Any_Type);
928 Add_One_Interp (N, Op_Id, Etype (Op_Id));
929 end if;
930
931 -- Entity is not already set, so we do need to collect interpretations
932
933 else
934 Set_Etype (N, Any_Type);
935
936 Op_Id := Get_Name_Entity_Id (Chars (N));
937 while Present (Op_Id) loop
938 if Ekind (Op_Id) = E_Operator
939 and then Present (Next_Entity (First_Entity (Op_Id)))
940 then
941 Find_Arithmetic_Types (L, R, Op_Id, N);
942
943 -- The following may seem superfluous, because an operator cannot
944 -- be generic, but this ignores the cleverness of the author of
945 -- ACVC bc1013a.
946
947 elsif Is_Overloadable (Op_Id) then
948 Analyze_User_Defined_Binary_Op (N, Op_Id);
949 end if;
950
951 Op_Id := Homonym (Op_Id);
952 end loop;
953 end if;
954
955 Operator_Check (N);
956 Check_Function_Writable_Actuals (N);
957 end Analyze_Arithmetic_Op;
958
959 ------------------
960 -- Analyze_Call --
961 ------------------
962
963 -- Function, procedure, and entry calls are checked here. The Name in
964 -- the call may be overloaded. The actuals have been analyzed and may
965 -- themselves be overloaded. On exit from this procedure, the node N
966 -- may have zero, one or more interpretations. In the first case an
967 -- error message is produced. In the last case, the node is flagged
968 -- as overloaded and the interpretations are collected in All_Interp.
969
970 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
971 -- the type-checking is similar to that of other calls.
972
973 procedure Analyze_Call (N : Node_Id) is
974 Actuals : constant List_Id := Parameter_Associations (N);
975 Loc : constant Source_Ptr := Sloc (N);
976 Nam : Node_Id;
977 X : Interp_Index;
978 It : Interp;
979 Nam_Ent : Entity_Id;
980 Success : Boolean := False;
981
982 Deref : Boolean := False;
983 -- Flag indicates whether an interpretation of the prefix is a
984 -- parameterless call that returns an access_to_subprogram.
985
986 procedure Check_Mixed_Parameter_And_Named_Associations;
987 -- Check that parameter and named associations are not mixed. This is
988 -- a restriction in SPARK mode.
989
990 procedure Check_Writable_Actuals (N : Node_Id);
991 -- If the call has out or in-out parameters then mark its outermost
992 -- enclosing construct as a node on which the writable actuals check
993 -- must be performed.
994
995 function Name_Denotes_Function return Boolean;
996 -- If the type of the name is an access to subprogram, this may be the
997 -- type of a name, or the return type of the function being called. If
998 -- the name is not an entity then it can denote a protected function.
999 -- Until we distinguish Etype from Return_Type, we must use this routine
1000 -- to resolve the meaning of the name in the call.
1001
1002 procedure No_Interpretation;
1003 -- Output error message when no valid interpretation exists
1004
1005 --------------------------------------------------
1006 -- Check_Mixed_Parameter_And_Named_Associations --
1007 --------------------------------------------------
1008
1009 procedure Check_Mixed_Parameter_And_Named_Associations is
1010 Actual : Node_Id;
1011 Named_Seen : Boolean;
1012
1013 begin
1014 Named_Seen := False;
1015
1016 Actual := First (Actuals);
1017 while Present (Actual) loop
1018 case Nkind (Actual) is
1019 when N_Parameter_Association =>
1020 if Named_Seen then
1021 Check_SPARK_05_Restriction
1022 ("named association cannot follow positional one",
1023 Actual);
1024 exit;
1025 end if;
1026
1027 when others =>
1028 Named_Seen := True;
1029 end case;
1030
1031 Next (Actual);
1032 end loop;
1033 end Check_Mixed_Parameter_And_Named_Associations;
1034
1035 ----------------------------
1036 -- Check_Writable_Actuals --
1037 ----------------------------
1038
1039 -- The identification of conflicts in calls to functions with writable
1040 -- actuals is performed in the analysis phase of the front end to ensure
1041 -- that it reports exactly the same errors compiling with and without
1042 -- expansion enabled. It is performed in two stages:
1043
1044 -- 1) When a call to a function with out-mode parameters is found,
1045 -- we climb to the outermost enclosing construct that can be
1046 -- evaluated in arbitrary order and we mark it with the flag
1047 -- Check_Actuals.
1048
1049 -- 2) When the analysis of the marked node is complete, we traverse
1050 -- its decorated subtree searching for conflicts (see function
1051 -- Sem_Util.Check_Function_Writable_Actuals).
1052
1053 -- The unique exception to this general rule is for aggregates, since
1054 -- their analysis is performed by the front end in the resolution
1055 -- phase. For aggregates we do not climb to their enclosing construct:
1056 -- we restrict the analysis to the subexpressions initializing the
1057 -- aggregate components.
1058
1059 -- This implies that the analysis of expressions containing aggregates
1060 -- is not complete, since there may be conflicts on writable actuals
1061 -- involving subexpressions of the enclosing logical or arithmetic
1062 -- expressions. However, we cannot wait and perform the analysis when
1063 -- the whole subtree is resolved, since the subtrees may be transformed,
1064 -- thus adding extra complexity and computation cost to identify and
1065 -- report exactly the same errors compiling with and without expansion
1066 -- enabled.
1067
1068 procedure Check_Writable_Actuals (N : Node_Id) is
1069 begin
1070 if Comes_From_Source (N)
1071 and then Present (Get_Subprogram_Entity (N))
1072 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1073 then
1074 -- For procedures and entries there is no need to climb since
1075 -- we only need to check if the actuals of this call invoke
1076 -- functions whose out-mode parameters overlap.
1077
1078 if Nkind (N) /= N_Function_Call then
1079 Set_Check_Actuals (N);
1080
1081 -- For calls to functions we climb to the outermost enclosing
1082 -- construct where the out-mode actuals of this function may
1083 -- introduce conflicts.
1084
1085 else
1086 declare
1087 Outermost : Node_Id := Empty; -- init to avoid warning
1088 P : Node_Id := N;
1089
1090 begin
1091 while Present (P) loop
1092 -- For object declarations we can climb to the node from
1093 -- its object definition branch or from its initializing
1094 -- expression. We prefer to mark the child node as the
1095 -- outermost construct to avoid adding further complexity
1096 -- to the routine that will later take care of
1097 -- performing the writable actuals check.
1098
1099 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1100 and then not Nkind_In (P, N_Assignment_Statement,
1101 N_Object_Declaration)
1102 then
1103 Outermost := P;
1104 end if;
1105
1106 -- Avoid climbing more than needed
1107
1108 exit when Stop_Subtree_Climbing (Nkind (P))
1109 or else (Nkind (P) = N_Range
1110 and then not
1111 Nkind_In (Parent (P), N_In, N_Not_In));
1112
1113 P := Parent (P);
1114 end loop;
1115
1116 Set_Check_Actuals (Outermost);
1117 end;
1118 end if;
1119 end if;
1120 end Check_Writable_Actuals;
1121
1122 ---------------------------
1123 -- Name_Denotes_Function --
1124 ---------------------------
1125
1126 function Name_Denotes_Function return Boolean is
1127 begin
1128 if Is_Entity_Name (Nam) then
1129 return Ekind (Entity (Nam)) = E_Function;
1130 elsif Nkind (Nam) = N_Selected_Component then
1131 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
1132 else
1133 return False;
1134 end if;
1135 end Name_Denotes_Function;
1136
1137 -----------------------
1138 -- No_Interpretation --
1139 -----------------------
1140
1141 procedure No_Interpretation is
1142 L : constant Boolean := Is_List_Member (N);
1143 K : constant Node_Kind := Nkind (Parent (N));
1144
1145 begin
1146 -- If the node is in a list whose parent is not an expression then it
1147 -- must be an attempted procedure call.
1148
1149 if L and then K not in N_Subexpr then
1150 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1151 Error_Msg_NE
1152 ("must instantiate generic procedure& before call",
1153 Nam, Entity (Nam));
1154 else
1155 Error_Msg_N ("procedure or entry name expected", Nam);
1156 end if;
1157
1158 -- Check for tasking cases where only an entry call will do
1159
1160 elsif not L
1161 and then Nkind_In (K, N_Entry_Call_Alternative,
1162 N_Triggering_Alternative)
1163 then
1164 Error_Msg_N ("entry name expected", Nam);
1165
1166 -- Otherwise give general error message
1167
1168 else
1169 Error_Msg_N ("invalid prefix in call", Nam);
1170 end if;
1171 end No_Interpretation;
1172
1173 -- Start of processing for Analyze_Call
1174
1175 begin
1176 if Restriction_Check_Required (SPARK_05) then
1177 Check_Mixed_Parameter_And_Named_Associations;
1178 end if;
1179
1180 -- Initialize the type of the result of the call to the error type,
1181 -- which will be reset if the type is successfully resolved.
1182
1183 Set_Etype (N, Any_Type);
1184
1185 Nam := Name (N);
1186
1187 if not Is_Overloaded (Nam) then
1188
1189 -- Only one interpretation to check
1190
1191 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1192 Nam_Ent := Etype (Nam);
1193
1194 -- If the prefix is an access_to_subprogram, this may be an indirect
1195 -- call. This is the case if the name in the call is not an entity
1196 -- name, or if it is a function name in the context of a procedure
1197 -- call. In this latter case, we have a call to a parameterless
1198 -- function that returns a pointer_to_procedure which is the entity
1199 -- being called. Finally, F (X) may be a call to a parameterless
1200 -- function that returns a pointer to a function with parameters.
1201 -- Note that if F returns an access-to-subprogram whose designated
1202 -- type is an array, F (X) cannot be interpreted as an indirect call
1203 -- through the result of the call to F.
1204
1205 elsif Is_Access_Type (Etype (Nam))
1206 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
1207 and then
1208 (not Name_Denotes_Function
1209 or else Nkind (N) = N_Procedure_Call_Statement
1210 or else
1211 (Nkind (Parent (N)) /= N_Explicit_Dereference
1212 and then Is_Entity_Name (Nam)
1213 and then No (First_Formal (Entity (Nam)))
1214 and then not
1215 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1216 and then Present (Actuals)))
1217 then
1218 Nam_Ent := Designated_Type (Etype (Nam));
1219 Insert_Explicit_Dereference (Nam);
1220
1221 -- Selected component case. Simple entry or protected operation,
1222 -- where the entry name is given by the selector name.
1223
1224 elsif Nkind (Nam) = N_Selected_Component then
1225 Nam_Ent := Entity (Selector_Name (Nam));
1226
1227 if not Ekind_In (Nam_Ent, E_Entry,
1228 E_Entry_Family,
1229 E_Function,
1230 E_Procedure)
1231 then
1232 Error_Msg_N ("name in call is not a callable entity", Nam);
1233 Set_Etype (N, Any_Type);
1234 return;
1235 end if;
1236
1237 -- If the name is an Indexed component, it can be a call to a member
1238 -- of an entry family. The prefix must be a selected component whose
1239 -- selector is the entry. Analyze_Procedure_Call normalizes several
1240 -- kinds of call into this form.
1241
1242 elsif Nkind (Nam) = N_Indexed_Component then
1243 if Nkind (Prefix (Nam)) = N_Selected_Component then
1244 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1245 else
1246 Error_Msg_N ("name in call is not a callable entity", Nam);
1247 Set_Etype (N, Any_Type);
1248 return;
1249 end if;
1250
1251 elsif not Is_Entity_Name (Nam) then
1252 Error_Msg_N ("name in call is not a callable entity", Nam);
1253 Set_Etype (N, Any_Type);
1254 return;
1255
1256 else
1257 Nam_Ent := Entity (Nam);
1258
1259 -- If not overloadable, this may be a generalized indexing
1260 -- operation with named associations. Rewrite again as an
1261 -- indexed component and analyze as container indexing.
1262
1263 if not Is_Overloadable (Nam_Ent) then
1264 if Present
1265 (Find_Value_Of_Aspect
1266 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1267 then
1268 Replace (N,
1269 Make_Indexed_Component (Sloc (N),
1270 Prefix => Nam,
1271 Expressions => Parameter_Associations (N)));
1272
1273 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1274 return;
1275 else
1276 No_Interpretation;
1277 end if;
1278
1279 else
1280 No_Interpretation;
1281 end if;
1282
1283 return;
1284 end if;
1285 end if;
1286
1287 -- Operations generated for RACW stub types are called only through
1288 -- dispatching, and can never be the static interpretation of a call.
1289
1290 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1291 No_Interpretation;
1292 return;
1293 end if;
1294
1295 Analyze_One_Call (N, Nam_Ent, True, Success);
1296
1297 -- If this is an indirect call, the return type of the access_to
1298 -- subprogram may be an incomplete type. At the point of the call,
1299 -- use the full type if available, and at the same time update the
1300 -- return type of the access_to_subprogram.
1301
1302 if Success
1303 and then Nkind (Nam) = N_Explicit_Dereference
1304 and then Ekind (Etype (N)) = E_Incomplete_Type
1305 and then Present (Full_View (Etype (N)))
1306 then
1307 Set_Etype (N, Full_View (Etype (N)));
1308 Set_Etype (Nam_Ent, Etype (N));
1309 end if;
1310
1311 -- Overloaded call
1312
1313 else
1314 -- An overloaded selected component must denote overloaded operations
1315 -- of a concurrent type. The interpretations are attached to the
1316 -- simple name of those operations.
1317
1318 if Nkind (Nam) = N_Selected_Component then
1319 Nam := Selector_Name (Nam);
1320 end if;
1321
1322 Get_First_Interp (Nam, X, It);
1323 while Present (It.Nam) loop
1324 Nam_Ent := It.Nam;
1325 Deref := False;
1326
1327 -- Name may be call that returns an access to subprogram, or more
1328 -- generally an overloaded expression one of whose interpretations
1329 -- yields an access to subprogram. If the name is an entity, we do
1330 -- not dereference, because the node is a call that returns the
1331 -- access type: note difference between f(x), where the call may
1332 -- return an access subprogram type, and f(x)(y), where the type
1333 -- returned by the call to f is implicitly dereferenced to analyze
1334 -- the outer call.
1335
1336 if Is_Access_Type (Nam_Ent) then
1337 Nam_Ent := Designated_Type (Nam_Ent);
1338
1339 elsif Is_Access_Type (Etype (Nam_Ent))
1340 and then
1341 (not Is_Entity_Name (Nam)
1342 or else Nkind (N) = N_Procedure_Call_Statement)
1343 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1344 = E_Subprogram_Type
1345 then
1346 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1347
1348 if Is_Entity_Name (Nam) then
1349 Deref := True;
1350 end if;
1351 end if;
1352
1353 -- If the call has been rewritten from a prefixed call, the first
1354 -- parameter has been analyzed, but may need a subsequent
1355 -- dereference, so skip its analysis now.
1356
1357 if Is_Rewrite_Substitution (N)
1358 and then Nkind (Original_Node (N)) = Nkind (N)
1359 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1360 and then Present (Parameter_Associations (N))
1361 and then Present (Etype (First (Parameter_Associations (N))))
1362 then
1363 Analyze_One_Call
1364 (N, Nam_Ent, False, Success, Skip_First => True);
1365 else
1366 Analyze_One_Call (N, Nam_Ent, False, Success);
1367 end if;
1368
1369 -- If the interpretation succeeds, mark the proper type of the
1370 -- prefix (any valid candidate will do). If not, remove the
1371 -- candidate interpretation. If this is a parameterless call
1372 -- on an anonymous access to subprogram, X is a variable with
1373 -- an access discriminant D, the entity in the interpretation is
1374 -- D, so rewrite X as X.D.all.
1375
1376 if Success then
1377 if Deref
1378 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1379 then
1380 if Ekind (It.Nam) = E_Discriminant
1381 and then Has_Implicit_Dereference (It.Nam)
1382 then
1383 Rewrite (Name (N),
1384 Make_Explicit_Dereference (Loc,
1385 Prefix =>
1386 Make_Selected_Component (Loc,
1387 Prefix =>
1388 New_Occurrence_Of (Entity (Nam), Loc),
1389 Selector_Name =>
1390 New_Occurrence_Of (It.Nam, Loc))));
1391
1392 Analyze (N);
1393 return;
1394
1395 else
1396 Set_Entity (Nam, It.Nam);
1397 Insert_Explicit_Dereference (Nam);
1398 Set_Etype (Nam, Nam_Ent);
1399 end if;
1400
1401 else
1402 Set_Etype (Nam, It.Typ);
1403 end if;
1404
1405 elsif Nkind_In (Name (N), N_Function_Call, N_Selected_Component)
1406 then
1407 Remove_Interp (X);
1408 end if;
1409
1410 Get_Next_Interp (X, It);
1411 end loop;
1412
1413 -- If the name is the result of a function call, it can only be a
1414 -- call to a function returning an access to subprogram. Insert
1415 -- explicit dereference.
1416
1417 if Nkind (Nam) = N_Function_Call then
1418 Insert_Explicit_Dereference (Nam);
1419 end if;
1420
1421 if Etype (N) = Any_Type then
1422
1423 -- None of the interpretations is compatible with the actuals
1424
1425 Diagnose_Call (N, Nam);
1426
1427 -- Special checks for uninstantiated put routines
1428
1429 if Nkind (N) = N_Procedure_Call_Statement
1430 and then Is_Entity_Name (Nam)
1431 and then Chars (Nam) = Name_Put
1432 and then List_Length (Actuals) = 1
1433 then
1434 declare
1435 Arg : constant Node_Id := First (Actuals);
1436 Typ : Entity_Id;
1437
1438 begin
1439 if Nkind (Arg) = N_Parameter_Association then
1440 Typ := Etype (Explicit_Actual_Parameter (Arg));
1441 else
1442 Typ := Etype (Arg);
1443 end if;
1444
1445 if Is_Signed_Integer_Type (Typ) then
1446 Error_Msg_N
1447 ("possible missing instantiation of "
1448 & "'Text_'I'O.'Integer_'I'O!", Nam);
1449
1450 elsif Is_Modular_Integer_Type (Typ) then
1451 Error_Msg_N
1452 ("possible missing instantiation of "
1453 & "'Text_'I'O.'Modular_'I'O!", Nam);
1454
1455 elsif Is_Floating_Point_Type (Typ) then
1456 Error_Msg_N
1457 ("possible missing instantiation of "
1458 & "'Text_'I'O.'Float_'I'O!", Nam);
1459
1460 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1461 Error_Msg_N
1462 ("possible missing instantiation of "
1463 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1464
1465 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1466 Error_Msg_N
1467 ("possible missing instantiation of "
1468 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1469
1470 elsif Is_Enumeration_Type (Typ) then
1471 Error_Msg_N
1472 ("possible missing instantiation of "
1473 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1474 end if;
1475 end;
1476 end if;
1477
1478 elsif not Is_Overloaded (N)
1479 and then Is_Entity_Name (Nam)
1480 then
1481 -- Resolution yields a single interpretation. Verify that the
1482 -- reference has capitalization consistent with the declaration.
1483
1484 Set_Entity_With_Checks (Nam, Entity (Nam));
1485 Generate_Reference (Entity (Nam), Nam);
1486
1487 Set_Etype (Nam, Etype (Entity (Nam)));
1488 else
1489 Remove_Abstract_Operations (N);
1490 end if;
1491
1492 End_Interp_List;
1493 end if;
1494
1495 if Ada_Version >= Ada_2012 then
1496
1497 -- Check if the call contains a function with writable actuals
1498
1499 Check_Writable_Actuals (N);
1500
1501 -- If found and the outermost construct that can be evaluated in
1502 -- an arbitrary order is precisely this call, then check all its
1503 -- actuals.
1504
1505 Check_Function_Writable_Actuals (N);
1506
1507 -- The return type of the function may be incomplete. This can be
1508 -- the case if the type is a generic formal, or a limited view. It
1509 -- can also happen when the function declaration appears before the
1510 -- full view of the type (which is legal in Ada 2012) and the call
1511 -- appears in a different unit, in which case the incomplete view
1512 -- must be replaced with the full view (or the nonlimited view)
1513 -- to prevent subsequent type errors. Note that the usual install/
1514 -- removal of limited_with clauses is not sufficient to handle this
1515 -- case, because the limited view may have been captured in another
1516 -- compilation unit that defines the current function.
1517
1518 if Is_Incomplete_Type (Etype (N)) then
1519 if Present (Full_View (Etype (N))) then
1520 if Is_Entity_Name (Nam) then
1521 Set_Etype (Nam, Full_View (Etype (N)));
1522 Set_Etype (Entity (Nam), Full_View (Etype (N)));
1523 end if;
1524
1525 Set_Etype (N, Full_View (Etype (N)));
1526
1527 elsif From_Limited_With (Etype (N))
1528 and then Present (Non_Limited_View (Etype (N)))
1529 then
1530 Set_Etype (N, Non_Limited_View (Etype (N)));
1531
1532 -- If there is no completion for the type, this may be because
1533 -- there is only a limited view of it and there is nothing in
1534 -- the context of the current unit that has required a regular
1535 -- compilation of the unit containing the type. We recognize
1536 -- this unusual case by the fact that that unit is not analyzed.
1537 -- Note that the call being analyzed is in a different unit from
1538 -- the function declaration, and nothing indicates that the type
1539 -- is a limited view.
1540
1541 elsif Ekind (Scope (Etype (N))) = E_Package
1542 and then Present (Limited_View (Scope (Etype (N))))
1543 and then not Analyzed (Unit_Declaration_Node (Scope (Etype (N))))
1544 then
1545 Error_Msg_NE
1546 ("cannot call function that returns limited view of}",
1547 N, Etype (N));
1548
1549 Error_Msg_NE
1550 ("\there must be a regular with_clause for package & in the "
1551 & "current unit, or in some unit in its context",
1552 N, Scope (Etype (N)));
1553
1554 Set_Etype (N, Any_Type);
1555 end if;
1556 end if;
1557 end if;
1558 end Analyze_Call;
1559
1560 -----------------------------
1561 -- Analyze_Case_Expression --
1562 -----------------------------
1563
1564 procedure Analyze_Case_Expression (N : Node_Id) is
1565 procedure Non_Static_Choice_Error (Choice : Node_Id);
1566 -- Error routine invoked by the generic instantiation below when
1567 -- the case expression has a non static choice.
1568
1569 package Case_Choices_Analysis is new
1570 Generic_Analyze_Choices
1571 (Process_Associated_Node => No_OP);
1572 use Case_Choices_Analysis;
1573
1574 package Case_Choices_Checking is new
1575 Generic_Check_Choices
1576 (Process_Empty_Choice => No_OP,
1577 Process_Non_Static_Choice => Non_Static_Choice_Error,
1578 Process_Associated_Node => No_OP);
1579 use Case_Choices_Checking;
1580
1581 -----------------------------
1582 -- Non_Static_Choice_Error --
1583 -----------------------------
1584
1585 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1586 begin
1587 Flag_Non_Static_Expr
1588 ("choice given in case expression is not static!", Choice);
1589 end Non_Static_Choice_Error;
1590
1591 -- Local variables
1592
1593 Expr : constant Node_Id := Expression (N);
1594 Alt : Node_Id;
1595 Exp_Type : Entity_Id;
1596 Exp_Btype : Entity_Id;
1597
1598 FirstX : Node_Id := Empty;
1599 -- First expression in the case for which there is some type information
1600 -- available, i.e. it is not Any_Type, which can happen because of some
1601 -- error, or from the use of e.g. raise Constraint_Error.
1602
1603 Others_Present : Boolean;
1604 -- Indicates if Others was present
1605
1606 Wrong_Alt : Node_Id := Empty;
1607 -- For error reporting
1608
1609 -- Start of processing for Analyze_Case_Expression
1610
1611 begin
1612 if Comes_From_Source (N) then
1613 Check_Compiler_Unit ("case expression", N);
1614 end if;
1615
1616 Analyze_And_Resolve (Expr, Any_Discrete);
1617 Check_Unset_Reference (Expr);
1618 Exp_Type := Etype (Expr);
1619 Exp_Btype := Base_Type (Exp_Type);
1620
1621 Alt := First (Alternatives (N));
1622 while Present (Alt) loop
1623 if Error_Posted (Expression (Alt)) then
1624 return;
1625 end if;
1626
1627 Analyze (Expression (Alt));
1628
1629 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1630 FirstX := Expression (Alt);
1631 end if;
1632
1633 Next (Alt);
1634 end loop;
1635
1636 -- Get our initial type from the first expression for which we got some
1637 -- useful type information from the expression.
1638
1639 if No (FirstX) then
1640 return;
1641 end if;
1642
1643 if not Is_Overloaded (FirstX) then
1644 Set_Etype (N, Etype (FirstX));
1645
1646 else
1647 declare
1648 I : Interp_Index;
1649 It : Interp;
1650
1651 begin
1652 Set_Etype (N, Any_Type);
1653
1654 Get_First_Interp (FirstX, I, It);
1655 while Present (It.Nam) loop
1656
1657 -- For each interpretation of the first expression, we only
1658 -- add the interpretation if every other expression in the
1659 -- case expression alternatives has a compatible type.
1660
1661 Alt := Next (First (Alternatives (N)));
1662 while Present (Alt) loop
1663 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1664 Next (Alt);
1665 end loop;
1666
1667 if No (Alt) then
1668 Add_One_Interp (N, It.Typ, It.Typ);
1669 else
1670 Wrong_Alt := Alt;
1671 end if;
1672
1673 Get_Next_Interp (I, It);
1674 end loop;
1675 end;
1676 end if;
1677
1678 Exp_Btype := Base_Type (Exp_Type);
1679
1680 -- The expression must be of a discrete type which must be determinable
1681 -- independently of the context in which the expression occurs, but
1682 -- using the fact that the expression must be of a discrete type.
1683 -- Moreover, the type this expression must not be a character literal
1684 -- (which is always ambiguous).
1685
1686 -- If error already reported by Resolve, nothing more to do
1687
1688 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1689 return;
1690
1691 -- Special casee message for character literal
1692
1693 elsif Exp_Btype = Any_Character then
1694 Error_Msg_N
1695 ("character literal as case expression is ambiguous", Expr);
1696 return;
1697 end if;
1698
1699 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1700 Error_Msg_N
1701 ("type incompatible with that of previous alternatives",
1702 Expression (Wrong_Alt));
1703 return;
1704 end if;
1705
1706 -- If the case expression is a formal object of mode in out, then
1707 -- treat it as having a nonstatic subtype by forcing use of the base
1708 -- type (which has to get passed to Check_Case_Choices below). Also
1709 -- use base type when the case expression is parenthesized.
1710
1711 if Paren_Count (Expr) > 0
1712 or else (Is_Entity_Name (Expr)
1713 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1714 then
1715 Exp_Type := Exp_Btype;
1716 end if;
1717
1718 -- The case expression alternatives cover the range of a static subtype
1719 -- subject to aspect Static_Predicate. Do not check the choices when the
1720 -- case expression has not been fully analyzed yet because this may lead
1721 -- to bogus errors.
1722
1723 if Is_OK_Static_Subtype (Exp_Type)
1724 and then Has_Static_Predicate_Aspect (Exp_Type)
1725 and then In_Spec_Expression
1726 then
1727 null;
1728
1729 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1730
1731 else
1732 Analyze_Choices (Alternatives (N), Exp_Type);
1733 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1734
1735 if Exp_Type = Universal_Integer and then not Others_Present then
1736 Error_Msg_N
1737 ("case on universal integer requires OTHERS choice", Expr);
1738 end if;
1739 end if;
1740 end Analyze_Case_Expression;
1741
1742 ---------------------------
1743 -- Analyze_Comparison_Op --
1744 ---------------------------
1745
1746 procedure Analyze_Comparison_Op (N : Node_Id) is
1747 L : constant Node_Id := Left_Opnd (N);
1748 R : constant Node_Id := Right_Opnd (N);
1749 Op_Id : Entity_Id := Entity (N);
1750
1751 begin
1752 Set_Etype (N, Any_Type);
1753 Candidate_Type := Empty;
1754
1755 Analyze_Expression (L);
1756 Analyze_Expression (R);
1757
1758 if Present (Op_Id) then
1759 if Ekind (Op_Id) = E_Operator then
1760 Find_Comparison_Types (L, R, Op_Id, N);
1761 else
1762 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1763 end if;
1764
1765 if Is_Overloaded (L) then
1766 Set_Etype (L, Intersect_Types (L, R));
1767 end if;
1768
1769 else
1770 Op_Id := Get_Name_Entity_Id (Chars (N));
1771 while Present (Op_Id) loop
1772 if Ekind (Op_Id) = E_Operator then
1773 Find_Comparison_Types (L, R, Op_Id, N);
1774 else
1775 Analyze_User_Defined_Binary_Op (N, Op_Id);
1776 end if;
1777
1778 Op_Id := Homonym (Op_Id);
1779 end loop;
1780 end if;
1781
1782 Operator_Check (N);
1783 Check_Function_Writable_Actuals (N);
1784 end Analyze_Comparison_Op;
1785
1786 ---------------------------
1787 -- Analyze_Concatenation --
1788 ---------------------------
1789
1790 procedure Analyze_Concatenation (N : Node_Id) is
1791
1792 -- We wish to avoid deep recursion, because concatenations are often
1793 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1794 -- operands nonrecursively until we find something that is not a
1795 -- concatenation (A in this case), or has already been analyzed. We
1796 -- analyze that, and then walk back up the tree following Parent
1797 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1798 -- work at each level. The Parent pointers allow us to avoid recursion,
1799 -- and thus avoid running out of memory.
1800
1801 NN : Node_Id := N;
1802 L : Node_Id;
1803
1804 begin
1805 Candidate_Type := Empty;
1806
1807 -- The following code is equivalent to:
1808
1809 -- Set_Etype (N, Any_Type);
1810 -- Analyze_Expression (Left_Opnd (N));
1811 -- Analyze_Concatenation_Rest (N);
1812
1813 -- where the Analyze_Expression call recurses back here if the left
1814 -- operand is a concatenation.
1815
1816 -- Walk down left operands
1817
1818 loop
1819 Set_Etype (NN, Any_Type);
1820 L := Left_Opnd (NN);
1821 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1822 NN := L;
1823 end loop;
1824
1825 -- Now (given the above example) NN is A&B and L is A
1826
1827 -- First analyze L ...
1828
1829 Analyze_Expression (L);
1830
1831 -- ... then walk NN back up until we reach N (where we started), calling
1832 -- Analyze_Concatenation_Rest along the way.
1833
1834 loop
1835 Analyze_Concatenation_Rest (NN);
1836 exit when NN = N;
1837 NN := Parent (NN);
1838 end loop;
1839 end Analyze_Concatenation;
1840
1841 --------------------------------
1842 -- Analyze_Concatenation_Rest --
1843 --------------------------------
1844
1845 -- If the only one-dimensional array type in scope is String,
1846 -- this is the resulting type of the operation. Otherwise there
1847 -- will be a concatenation operation defined for each user-defined
1848 -- one-dimensional array.
1849
1850 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1851 L : constant Node_Id := Left_Opnd (N);
1852 R : constant Node_Id := Right_Opnd (N);
1853 Op_Id : Entity_Id := Entity (N);
1854 LT : Entity_Id;
1855 RT : Entity_Id;
1856
1857 begin
1858 Analyze_Expression (R);
1859
1860 -- If the entity is present, the node appears in an instance, and
1861 -- denotes a predefined concatenation operation. The resulting type is
1862 -- obtained from the arguments when possible. If the arguments are
1863 -- aggregates, the array type and the concatenation type must be
1864 -- visible.
1865
1866 if Present (Op_Id) then
1867 if Ekind (Op_Id) = E_Operator then
1868 LT := Base_Type (Etype (L));
1869 RT := Base_Type (Etype (R));
1870
1871 if Is_Array_Type (LT)
1872 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1873 then
1874 Add_One_Interp (N, Op_Id, LT);
1875
1876 elsif Is_Array_Type (RT)
1877 and then LT = Base_Type (Component_Type (RT))
1878 then
1879 Add_One_Interp (N, Op_Id, RT);
1880
1881 -- If one operand is a string type or a user-defined array type,
1882 -- and the other is a literal, result is of the specific type.
1883
1884 elsif
1885 (Root_Type (LT) = Standard_String
1886 or else Scope (LT) /= Standard_Standard)
1887 and then Etype (R) = Any_String
1888 then
1889 Add_One_Interp (N, Op_Id, LT);
1890
1891 elsif
1892 (Root_Type (RT) = Standard_String
1893 or else Scope (RT) /= Standard_Standard)
1894 and then Etype (L) = Any_String
1895 then
1896 Add_One_Interp (N, Op_Id, RT);
1897
1898 elsif not Is_Generic_Type (Etype (Op_Id)) then
1899 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1900
1901 else
1902 -- Type and its operations must be visible
1903
1904 Set_Entity (N, Empty);
1905 Analyze_Concatenation (N);
1906 end if;
1907
1908 else
1909 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1910 end if;
1911
1912 else
1913 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1914 while Present (Op_Id) loop
1915 if Ekind (Op_Id) = E_Operator then
1916
1917 -- Do not consider operators declared in dead code, they
1918 -- cannot be part of the resolution.
1919
1920 if Is_Eliminated (Op_Id) then
1921 null;
1922 else
1923 Find_Concatenation_Types (L, R, Op_Id, N);
1924 end if;
1925
1926 else
1927 Analyze_User_Defined_Binary_Op (N, Op_Id);
1928 end if;
1929
1930 Op_Id := Homonym (Op_Id);
1931 end loop;
1932 end if;
1933
1934 Operator_Check (N);
1935 end Analyze_Concatenation_Rest;
1936
1937 -------------------------
1938 -- Analyze_Equality_Op --
1939 -------------------------
1940
1941 procedure Analyze_Equality_Op (N : Node_Id) is
1942 Loc : constant Source_Ptr := Sloc (N);
1943 L : constant Node_Id := Left_Opnd (N);
1944 R : constant Node_Id := Right_Opnd (N);
1945 Op_Id : Entity_Id;
1946
1947 begin
1948 Set_Etype (N, Any_Type);
1949 Candidate_Type := Empty;
1950
1951 Analyze_Expression (L);
1952 Analyze_Expression (R);
1953
1954 -- If the entity is set, the node is a generic instance with a non-local
1955 -- reference to the predefined operator or to a user-defined function.
1956 -- It can also be an inequality that is expanded into the negation of a
1957 -- call to a user-defined equality operator.
1958
1959 -- For the predefined case, the result is Boolean, regardless of the
1960 -- type of the operands. The operands may even be limited, if they are
1961 -- generic actuals. If they are overloaded, label the left argument with
1962 -- the common type that must be present, or with the type of the formal
1963 -- of the user-defined function.
1964
1965 if Present (Entity (N)) then
1966 Op_Id := Entity (N);
1967
1968 if Ekind (Op_Id) = E_Operator then
1969 Add_One_Interp (N, Op_Id, Standard_Boolean);
1970 else
1971 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1972 end if;
1973
1974 if Is_Overloaded (L) then
1975 if Ekind (Op_Id) = E_Operator then
1976 Set_Etype (L, Intersect_Types (L, R));
1977 else
1978 Set_Etype (L, Etype (First_Formal (Op_Id)));
1979 end if;
1980 end if;
1981
1982 else
1983 Op_Id := Get_Name_Entity_Id (Chars (N));
1984 while Present (Op_Id) loop
1985 if Ekind (Op_Id) = E_Operator then
1986 Find_Equality_Types (L, R, Op_Id, N);
1987 else
1988 Analyze_User_Defined_Binary_Op (N, Op_Id);
1989 end if;
1990
1991 Op_Id := Homonym (Op_Id);
1992 end loop;
1993 end if;
1994
1995 -- If there was no match, and the operator is inequality, this may be
1996 -- a case where inequality has not been made explicit, as for tagged
1997 -- types. Analyze the node as the negation of an equality operation.
1998 -- This cannot be done earlier, because before analysis we cannot rule
1999 -- out the presence of an explicit inequality.
2000
2001 if Etype (N) = Any_Type
2002 and then Nkind (N) = N_Op_Ne
2003 then
2004 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
2005 while Present (Op_Id) loop
2006 if Ekind (Op_Id) = E_Operator then
2007 Find_Equality_Types (L, R, Op_Id, N);
2008 else
2009 Analyze_User_Defined_Binary_Op (N, Op_Id);
2010 end if;
2011
2012 Op_Id := Homonym (Op_Id);
2013 end loop;
2014
2015 if Etype (N) /= Any_Type then
2016 Op_Id := Entity (N);
2017
2018 Rewrite (N,
2019 Make_Op_Not (Loc,
2020 Right_Opnd =>
2021 Make_Op_Eq (Loc,
2022 Left_Opnd => Left_Opnd (N),
2023 Right_Opnd => Right_Opnd (N))));
2024
2025 Set_Entity (Right_Opnd (N), Op_Id);
2026 Analyze (N);
2027 end if;
2028 end if;
2029
2030 Operator_Check (N);
2031 Check_Function_Writable_Actuals (N);
2032 end Analyze_Equality_Op;
2033
2034 ----------------------------------
2035 -- Analyze_Explicit_Dereference --
2036 ----------------------------------
2037
2038 procedure Analyze_Explicit_Dereference (N : Node_Id) is
2039 Loc : constant Source_Ptr := Sloc (N);
2040 P : constant Node_Id := Prefix (N);
2041 T : Entity_Id;
2042 I : Interp_Index;
2043 It : Interp;
2044 New_N : Node_Id;
2045
2046 function Is_Function_Type return Boolean;
2047 -- Check whether node may be interpreted as an implicit function call
2048
2049 ----------------------
2050 -- Is_Function_Type --
2051 ----------------------
2052
2053 function Is_Function_Type return Boolean is
2054 I : Interp_Index;
2055 It : Interp;
2056
2057 begin
2058 if not Is_Overloaded (N) then
2059 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
2060 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
2061
2062 else
2063 Get_First_Interp (N, I, It);
2064 while Present (It.Nam) loop
2065 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
2066 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
2067 then
2068 return False;
2069 end if;
2070
2071 Get_Next_Interp (I, It);
2072 end loop;
2073
2074 return True;
2075 end if;
2076 end Is_Function_Type;
2077
2078 -- Start of processing for Analyze_Explicit_Dereference
2079
2080 begin
2081 -- If source node, check SPARK restriction. We guard this with the
2082 -- source node check, because ???
2083
2084 if Comes_From_Source (N) then
2085 Check_SPARK_05_Restriction ("explicit dereference is not allowed", N);
2086 end if;
2087
2088 -- In formal verification mode, keep track of all reads and writes
2089 -- through explicit dereferences.
2090
2091 if GNATprove_Mode then
2092 SPARK_Specific.Generate_Dereference (N);
2093 end if;
2094
2095 Analyze (P);
2096 Set_Etype (N, Any_Type);
2097
2098 -- Test for remote access to subprogram type, and if so return
2099 -- after rewriting the original tree.
2100
2101 if Remote_AST_E_Dereference (P) then
2102 return;
2103 end if;
2104
2105 -- Normal processing for other than remote access to subprogram type
2106
2107 if not Is_Overloaded (P) then
2108 if Is_Access_Type (Etype (P)) then
2109
2110 -- Set the Etype
2111
2112 declare
2113 DT : constant Entity_Id := Designated_Type (Etype (P));
2114
2115 begin
2116 -- An explicit dereference is a legal occurrence of an
2117 -- incomplete type imported through a limited_with clause, if
2118 -- the full view is visible, or if we are within an instance
2119 -- body, where the enclosing body has a regular with_clause
2120 -- on the unit.
2121
2122 if From_Limited_With (DT)
2123 and then not From_Limited_With (Scope (DT))
2124 and then
2125 (Is_Immediately_Visible (Scope (DT))
2126 or else
2127 (Is_Child_Unit (Scope (DT))
2128 and then Is_Visible_Lib_Unit (Scope (DT)))
2129 or else In_Instance_Body)
2130 then
2131 Set_Etype (N, Available_View (DT));
2132
2133 else
2134 Set_Etype (N, DT);
2135 end if;
2136 end;
2137
2138 elsif Etype (P) /= Any_Type then
2139 Error_Msg_N ("prefix of dereference must be an access type", N);
2140 return;
2141 end if;
2142
2143 else
2144 Get_First_Interp (P, I, It);
2145 while Present (It.Nam) loop
2146 T := It.Typ;
2147
2148 if Is_Access_Type (T) then
2149 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2150 end if;
2151
2152 Get_Next_Interp (I, It);
2153 end loop;
2154
2155 -- Error if no interpretation of the prefix has an access type
2156
2157 if Etype (N) = Any_Type then
2158 Error_Msg_N
2159 ("access type required in prefix of explicit dereference", P);
2160 Set_Etype (N, Any_Type);
2161 return;
2162 end if;
2163 end if;
2164
2165 if Is_Function_Type
2166 and then Nkind (Parent (N)) /= N_Indexed_Component
2167
2168 and then (Nkind (Parent (N)) /= N_Function_Call
2169 or else N /= Name (Parent (N)))
2170
2171 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2172 or else N /= Name (Parent (N)))
2173
2174 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2175 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2176 or else
2177 (Attribute_Name (Parent (N)) /= Name_Address
2178 and then
2179 Attribute_Name (Parent (N)) /= Name_Access))
2180 then
2181 -- Name is a function call with no actuals, in a context that
2182 -- requires deproceduring (including as an actual in an enclosing
2183 -- function or procedure call). There are some pathological cases
2184 -- where the prefix might include functions that return access to
2185 -- subprograms and others that return a regular type. Disambiguation
2186 -- of those has to take place in Resolve.
2187
2188 New_N :=
2189 Make_Function_Call (Loc,
2190 Name => Make_Explicit_Dereference (Loc, P),
2191 Parameter_Associations => New_List);
2192
2193 -- If the prefix is overloaded, remove operations that have formals,
2194 -- we know that this is a parameterless call.
2195
2196 if Is_Overloaded (P) then
2197 Get_First_Interp (P, I, It);
2198 while Present (It.Nam) loop
2199 T := It.Typ;
2200
2201 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2202 Set_Etype (P, T);
2203 else
2204 Remove_Interp (I);
2205 end if;
2206
2207 Get_Next_Interp (I, It);
2208 end loop;
2209 end if;
2210
2211 Rewrite (N, New_N);
2212 Analyze (N);
2213
2214 elsif not Is_Function_Type
2215 and then Is_Overloaded (N)
2216 then
2217 -- The prefix may include access to subprograms and other access
2218 -- types. If the context selects the interpretation that is a
2219 -- function call (not a procedure call) we cannot rewrite the node
2220 -- yet, but we include the result of the call interpretation.
2221
2222 Get_First_Interp (N, I, It);
2223 while Present (It.Nam) loop
2224 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2225 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
2226 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2227 then
2228 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2229 end if;
2230
2231 Get_Next_Interp (I, It);
2232 end loop;
2233 end if;
2234
2235 -- A value of remote access-to-class-wide must not be dereferenced
2236 -- (RM E.2.2(16)).
2237
2238 Validate_Remote_Access_To_Class_Wide_Type (N);
2239 end Analyze_Explicit_Dereference;
2240
2241 ------------------------
2242 -- Analyze_Expression --
2243 ------------------------
2244
2245 procedure Analyze_Expression (N : Node_Id) is
2246 begin
2247
2248 -- If the expression is an indexed component that will be rewritten
2249 -- as a container indexing, it has already been analyzed.
2250
2251 if Nkind (N) = N_Indexed_Component
2252 and then Present (Generalized_Indexing (N))
2253 then
2254 null;
2255
2256 else
2257 Analyze (N);
2258 Check_Parameterless_Call (N);
2259 end if;
2260 end Analyze_Expression;
2261
2262 -------------------------------------
2263 -- Analyze_Expression_With_Actions --
2264 -------------------------------------
2265
2266 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2267 A : Node_Id;
2268
2269 begin
2270 A := First (Actions (N));
2271 while Present (A) loop
2272 Analyze (A);
2273 Next (A);
2274 end loop;
2275
2276 Analyze_Expression (Expression (N));
2277 Set_Etype (N, Etype (Expression (N)));
2278 end Analyze_Expression_With_Actions;
2279
2280 ---------------------------
2281 -- Analyze_If_Expression --
2282 ---------------------------
2283
2284 procedure Analyze_If_Expression (N : Node_Id) is
2285 Condition : constant Node_Id := First (Expressions (N));
2286 Then_Expr : Node_Id;
2287 Else_Expr : Node_Id;
2288
2289 begin
2290 -- Defend against error of missing expressions from previous error
2291
2292 if No (Condition) then
2293 Check_Error_Detected;
2294 return;
2295 end if;
2296
2297 Then_Expr := Next (Condition);
2298
2299 if No (Then_Expr) then
2300 Check_Error_Detected;
2301 return;
2302 end if;
2303
2304 Else_Expr := Next (Then_Expr);
2305
2306 if Comes_From_Source (N) then
2307 Check_SPARK_05_Restriction ("if expression is not allowed", N);
2308 end if;
2309
2310 if Comes_From_Source (N) then
2311 Check_Compiler_Unit ("if expression", N);
2312 end if;
2313
2314 -- Analyze and resolve the condition. We need to resolve this now so
2315 -- that it gets folded to True/False if possible, before we analyze
2316 -- the THEN/ELSE branches, because when analyzing these branches, we
2317 -- may call Is_Statically_Unevaluated, which expects the condition of
2318 -- an enclosing IF to have been analyze/resolved/evaluated.
2319
2320 Analyze_Expression (Condition);
2321 Resolve (Condition, Any_Boolean);
2322
2323 -- Analyze THEN expression and (if present) ELSE expression. For those
2324 -- we delay resolution in the normal manner, because of overloading etc.
2325
2326 Analyze_Expression (Then_Expr);
2327
2328 if Present (Else_Expr) then
2329 Analyze_Expression (Else_Expr);
2330 end if;
2331
2332 -- If then expression not overloaded, then that decides the type
2333
2334 if not Is_Overloaded (Then_Expr) then
2335 Set_Etype (N, Etype (Then_Expr));
2336
2337 -- Case where then expression is overloaded
2338
2339 else
2340 declare
2341 I : Interp_Index;
2342 It : Interp;
2343
2344 begin
2345 Set_Etype (N, Any_Type);
2346
2347 -- Loop through interpretations of Then_Expr
2348
2349 Get_First_Interp (Then_Expr, I, It);
2350 while Present (It.Nam) loop
2351
2352 -- Add possible interpretation of Then_Expr if no Else_Expr, or
2353 -- Else_Expr is present and has a compatible type.
2354
2355 if No (Else_Expr)
2356 or else Has_Compatible_Type (Else_Expr, It.Typ)
2357 then
2358 Add_One_Interp (N, It.Typ, It.Typ);
2359 end if;
2360
2361 Get_Next_Interp (I, It);
2362 end loop;
2363
2364 -- If no valid interpretation has been found, then the type of the
2365 -- ELSE expression does not match any interpretation of the THEN
2366 -- expression.
2367
2368 if Etype (N) = Any_Type then
2369 Error_Msg_N
2370 ("type incompatible with that of `THEN` expression",
2371 Else_Expr);
2372 return;
2373 end if;
2374 end;
2375 end if;
2376 end Analyze_If_Expression;
2377
2378 ------------------------------------
2379 -- Analyze_Indexed_Component_Form --
2380 ------------------------------------
2381
2382 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2383 P : constant Node_Id := Prefix (N);
2384 Exprs : constant List_Id := Expressions (N);
2385 Exp : Node_Id;
2386 P_T : Entity_Id;
2387 E : Node_Id;
2388 U_N : Entity_Id;
2389
2390 procedure Process_Function_Call;
2391 -- Prefix in indexed component form is an overloadable entity, so the
2392 -- node is a function call. Reformat it as such.
2393
2394 procedure Process_Indexed_Component;
2395 -- Prefix in indexed component form is actually an indexed component.
2396 -- This routine processes it, knowing that the prefix is already
2397 -- resolved.
2398
2399 procedure Process_Indexed_Component_Or_Slice;
2400 -- An indexed component with a single index may designate a slice if
2401 -- the index is a subtype mark. This routine disambiguates these two
2402 -- cases by resolving the prefix to see if it is a subtype mark.
2403
2404 procedure Process_Overloaded_Indexed_Component;
2405 -- If the prefix of an indexed component is overloaded, the proper
2406 -- interpretation is selected by the index types and the context.
2407
2408 ---------------------------
2409 -- Process_Function_Call --
2410 ---------------------------
2411
2412 procedure Process_Function_Call is
2413 Loc : constant Source_Ptr := Sloc (N);
2414 Actual : Node_Id;
2415
2416 begin
2417 Change_Node (N, N_Function_Call);
2418 Set_Name (N, P);
2419 Set_Parameter_Associations (N, Exprs);
2420
2421 -- Analyze actuals prior to analyzing the call itself
2422
2423 Actual := First (Parameter_Associations (N));
2424 while Present (Actual) loop
2425 Analyze (Actual);
2426 Check_Parameterless_Call (Actual);
2427
2428 -- Move to next actual. Note that we use Next, not Next_Actual
2429 -- here. The reason for this is a bit subtle. If a function call
2430 -- includes named associations, the parser recognizes the node
2431 -- as a call, and it is analyzed as such. If all associations are
2432 -- positional, the parser builds an indexed_component node, and
2433 -- it is only after analysis of the prefix that the construct
2434 -- is recognized as a call, in which case Process_Function_Call
2435 -- rewrites the node and analyzes the actuals. If the list of
2436 -- actuals is malformed, the parser may leave the node as an
2437 -- indexed component (despite the presence of named associations).
2438 -- The iterator Next_Actual is equivalent to Next if the list is
2439 -- positional, but follows the normalized chain of actuals when
2440 -- named associations are present. In this case normalization has
2441 -- not taken place, and actuals remain unanalyzed, which leads to
2442 -- subsequent crashes or loops if there is an attempt to continue
2443 -- analysis of the program.
2444
2445 -- IF there is a single actual and it is a type name, the node
2446 -- can only be interpreted as a slice of a parameterless call.
2447 -- Rebuild the node as such and analyze.
2448
2449 if No (Next (Actual))
2450 and then Is_Entity_Name (Actual)
2451 and then Is_Type (Entity (Actual))
2452 and then Is_Discrete_Type (Entity (Actual))
2453 then
2454 Replace (N,
2455 Make_Slice (Loc,
2456 Prefix => P,
2457 Discrete_Range =>
2458 New_Occurrence_Of (Entity (Actual), Loc)));
2459 Analyze (N);
2460 return;
2461
2462 else
2463 Next (Actual);
2464 end if;
2465 end loop;
2466
2467 Analyze_Call (N);
2468 end Process_Function_Call;
2469
2470 -------------------------------
2471 -- Process_Indexed_Component --
2472 -------------------------------
2473
2474 procedure Process_Indexed_Component is
2475 Exp : Node_Id;
2476 Array_Type : Entity_Id;
2477 Index : Node_Id;
2478 Pent : Entity_Id := Empty;
2479
2480 begin
2481 Exp := First (Exprs);
2482
2483 if Is_Overloaded (P) then
2484 Process_Overloaded_Indexed_Component;
2485
2486 else
2487 Array_Type := Etype (P);
2488
2489 if Is_Entity_Name (P) then
2490 Pent := Entity (P);
2491 elsif Nkind (P) = N_Selected_Component
2492 and then Is_Entity_Name (Selector_Name (P))
2493 then
2494 Pent := Entity (Selector_Name (P));
2495 end if;
2496
2497 -- Prefix must be appropriate for an array type, taking into
2498 -- account a possible implicit dereference.
2499
2500 if Is_Access_Type (Array_Type) then
2501 Error_Msg_NW
2502 (Warn_On_Dereference, "?d?implicit dereference", N);
2503 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2504 end if;
2505
2506 if Is_Array_Type (Array_Type) then
2507
2508 -- In order to correctly access First_Index component later,
2509 -- replace string literal subtype by its parent type.
2510
2511 if Ekind (Array_Type) = E_String_Literal_Subtype then
2512 Array_Type := Etype (Array_Type);
2513 end if;
2514
2515 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2516 Analyze (Exp);
2517 Set_Etype (N, Any_Type);
2518
2519 if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then
2520 Error_Msg_N ("invalid index type in entry name", N);
2521
2522 elsif Present (Next (Exp)) then
2523 Error_Msg_N ("too many subscripts in entry reference", N);
2524
2525 else
2526 Set_Etype (N, Etype (P));
2527 end if;
2528
2529 return;
2530
2531 elsif Is_Record_Type (Array_Type)
2532 and then Remote_AST_I_Dereference (P)
2533 then
2534 return;
2535
2536 elsif Try_Container_Indexing (N, P, Exprs) then
2537 return;
2538
2539 elsif Array_Type = Any_Type then
2540 Set_Etype (N, Any_Type);
2541
2542 -- In most cases the analysis of the prefix will have emitted
2543 -- an error already, but if the prefix may be interpreted as a
2544 -- call in prefixed notation, the report is left to the caller.
2545 -- To prevent cascaded errors, report only if no previous ones.
2546
2547 if Serious_Errors_Detected = 0 then
2548 Error_Msg_N ("invalid prefix in indexed component", P);
2549
2550 if Nkind (P) = N_Expanded_Name then
2551 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2552 end if;
2553 end if;
2554
2555 return;
2556
2557 -- Here we definitely have a bad indexing
2558
2559 else
2560 if Nkind (Parent (N)) = N_Requeue_Statement
2561 and then Present (Pent) and then Ekind (Pent) = E_Entry
2562 then
2563 Error_Msg_N
2564 ("REQUEUE does not permit parameters", First (Exprs));
2565
2566 elsif Is_Entity_Name (P)
2567 and then Etype (P) = Standard_Void_Type
2568 then
2569 Error_Msg_NE ("incorrect use of &", P, Entity (P));
2570
2571 else
2572 Error_Msg_N ("array type required in indexed component", P);
2573 end if;
2574
2575 Set_Etype (N, Any_Type);
2576 return;
2577 end if;
2578
2579 Index := First_Index (Array_Type);
2580 while Present (Index) and then Present (Exp) loop
2581 if not Has_Compatible_Type (Exp, Etype (Index)) then
2582 Wrong_Type (Exp, Etype (Index));
2583 Set_Etype (N, Any_Type);
2584 return;
2585 end if;
2586
2587 Next_Index (Index);
2588 Next (Exp);
2589 end loop;
2590
2591 Set_Etype (N, Component_Type (Array_Type));
2592 Check_Implicit_Dereference (N, Etype (N));
2593
2594 if Present (Index) then
2595 Error_Msg_N
2596 ("too few subscripts in array reference", First (Exprs));
2597
2598 elsif Present (Exp) then
2599 Error_Msg_N ("too many subscripts in array reference", Exp);
2600 end if;
2601 end if;
2602 end Process_Indexed_Component;
2603
2604 ----------------------------------------
2605 -- Process_Indexed_Component_Or_Slice --
2606 ----------------------------------------
2607
2608 procedure Process_Indexed_Component_Or_Slice is
2609 begin
2610 Exp := First (Exprs);
2611 while Present (Exp) loop
2612 Analyze_Expression (Exp);
2613 Next (Exp);
2614 end loop;
2615
2616 Exp := First (Exprs);
2617
2618 -- If one index is present, and it is a subtype name, then the node
2619 -- denotes a slice (note that the case of an explicit range for a
2620 -- slice was already built as an N_Slice node in the first place,
2621 -- so that case is not handled here).
2622
2623 -- We use a replace rather than a rewrite here because this is one
2624 -- of the cases in which the tree built by the parser is plain wrong.
2625
2626 if No (Next (Exp))
2627 and then Is_Entity_Name (Exp)
2628 and then Is_Type (Entity (Exp))
2629 then
2630 Replace (N,
2631 Make_Slice (Sloc (N),
2632 Prefix => P,
2633 Discrete_Range => New_Copy (Exp)));
2634 Analyze (N);
2635
2636 -- Otherwise (more than one index present, or single index is not
2637 -- a subtype name), then we have the indexed component case.
2638
2639 else
2640 Process_Indexed_Component;
2641 end if;
2642 end Process_Indexed_Component_Or_Slice;
2643
2644 ------------------------------------------
2645 -- Process_Overloaded_Indexed_Component --
2646 ------------------------------------------
2647
2648 procedure Process_Overloaded_Indexed_Component is
2649 Exp : Node_Id;
2650 I : Interp_Index;
2651 It : Interp;
2652 Typ : Entity_Id;
2653 Index : Node_Id;
2654 Found : Boolean;
2655
2656 begin
2657 Set_Etype (N, Any_Type);
2658
2659 Get_First_Interp (P, I, It);
2660 while Present (It.Nam) loop
2661 Typ := It.Typ;
2662
2663 if Is_Access_Type (Typ) then
2664 Typ := Designated_Type (Typ);
2665 Error_Msg_NW
2666 (Warn_On_Dereference, "?d?implicit dereference", N);
2667 end if;
2668
2669 if Is_Array_Type (Typ) then
2670
2671 -- Got a candidate: verify that index types are compatible
2672
2673 Index := First_Index (Typ);
2674 Found := True;
2675 Exp := First (Exprs);
2676 while Present (Index) and then Present (Exp) loop
2677 if Has_Compatible_Type (Exp, Etype (Index)) then
2678 null;
2679 else
2680 Found := False;
2681 Remove_Interp (I);
2682 exit;
2683 end if;
2684
2685 Next_Index (Index);
2686 Next (Exp);
2687 end loop;
2688
2689 if Found and then No (Index) and then No (Exp) then
2690 declare
2691 CT : constant Entity_Id :=
2692 Base_Type (Component_Type (Typ));
2693 begin
2694 Add_One_Interp (N, CT, CT);
2695 Check_Implicit_Dereference (N, CT);
2696 end;
2697 end if;
2698
2699 elsif Try_Container_Indexing (N, P, Exprs) then
2700 return;
2701
2702 end if;
2703
2704 Get_Next_Interp (I, It);
2705 end loop;
2706
2707 if Etype (N) = Any_Type then
2708 Error_Msg_N ("no legal interpretation for indexed component", N);
2709 Set_Is_Overloaded (N, False);
2710 end if;
2711
2712 End_Interp_List;
2713 end Process_Overloaded_Indexed_Component;
2714
2715 -- Start of processing for Analyze_Indexed_Component_Form
2716
2717 begin
2718 -- Get name of array, function or type
2719
2720 Analyze (P);
2721
2722 -- If P is an explicit dereference whose prefix is of a remote access-
2723 -- to-subprogram type, then N has already been rewritten as a subprogram
2724 -- call and analyzed.
2725
2726 if Nkind (N) in N_Subprogram_Call then
2727 return;
2728
2729 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2730 -- the indexed component denotes a loop name, the indexed form is turned
2731 -- into an attribute reference.
2732
2733 elsif Nkind (N) = N_Attribute_Reference
2734 and then Attribute_Name (N) = Name_Loop_Entry
2735 then
2736 return;
2737 end if;
2738
2739 pragma Assert (Nkind (N) = N_Indexed_Component);
2740
2741 P_T := Base_Type (Etype (P));
2742
2743 if Is_Entity_Name (P) and then Present (Entity (P)) then
2744 U_N := Entity (P);
2745
2746 if Is_Type (U_N) then
2747
2748 -- Reformat node as a type conversion
2749
2750 E := Remove_Head (Exprs);
2751
2752 if Present (First (Exprs)) then
2753 Error_Msg_N
2754 ("argument of type conversion must be single expression", N);
2755 end if;
2756
2757 Change_Node (N, N_Type_Conversion);
2758 Set_Subtype_Mark (N, P);
2759 Set_Etype (N, U_N);
2760 Set_Expression (N, E);
2761
2762 -- After changing the node, call for the specific Analysis
2763 -- routine directly, to avoid a double call to the expander.
2764
2765 Analyze_Type_Conversion (N);
2766 return;
2767 end if;
2768
2769 if Is_Overloadable (U_N) then
2770 Process_Function_Call;
2771
2772 elsif Ekind (Etype (P)) = E_Subprogram_Type
2773 or else (Is_Access_Type (Etype (P))
2774 and then
2775 Ekind (Designated_Type (Etype (P))) =
2776 E_Subprogram_Type)
2777 then
2778 -- Call to access_to-subprogram with possible implicit dereference
2779
2780 Process_Function_Call;
2781
2782 elsif Is_Generic_Subprogram (U_N) then
2783
2784 -- A common beginner's (or C++ templates fan) error
2785
2786 Error_Msg_N ("generic subprogram cannot be called", N);
2787 Set_Etype (N, Any_Type);
2788 return;
2789
2790 else
2791 Process_Indexed_Component_Or_Slice;
2792 end if;
2793
2794 -- If not an entity name, prefix is an expression that may denote
2795 -- an array or an access-to-subprogram.
2796
2797 else
2798 if Ekind (P_T) = E_Subprogram_Type
2799 or else (Is_Access_Type (P_T)
2800 and then
2801 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2802 then
2803 Process_Function_Call;
2804
2805 elsif Nkind (P) = N_Selected_Component
2806 and then Present (Entity (Selector_Name (P)))
2807 and then Is_Overloadable (Entity (Selector_Name (P)))
2808 then
2809 Process_Function_Call;
2810
2811 -- In ASIS mode within a generic, a prefixed call is analyzed and
2812 -- partially rewritten but the original indexed component has not
2813 -- yet been rewritten as a call. Perform the replacement now.
2814
2815 elsif Nkind (P) = N_Selected_Component
2816 and then Nkind (Parent (P)) = N_Function_Call
2817 and then ASIS_Mode
2818 then
2819 Rewrite (N, Parent (P));
2820 Analyze (N);
2821
2822 else
2823 -- Indexed component, slice, or a call to a member of a family
2824 -- entry, which will be converted to an entry call later.
2825
2826 Process_Indexed_Component_Or_Slice;
2827 end if;
2828 end if;
2829
2830 Analyze_Dimension (N);
2831 end Analyze_Indexed_Component_Form;
2832
2833 ------------------------
2834 -- Analyze_Logical_Op --
2835 ------------------------
2836
2837 procedure Analyze_Logical_Op (N : Node_Id) is
2838 L : constant Node_Id := Left_Opnd (N);
2839 R : constant Node_Id := Right_Opnd (N);
2840 Op_Id : Entity_Id := Entity (N);
2841
2842 begin
2843 Set_Etype (N, Any_Type);
2844 Candidate_Type := Empty;
2845
2846 Analyze_Expression (L);
2847 Analyze_Expression (R);
2848
2849 if Present (Op_Id) then
2850
2851 if Ekind (Op_Id) = E_Operator then
2852 Find_Boolean_Types (L, R, Op_Id, N);
2853 else
2854 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2855 end if;
2856
2857 else
2858 Op_Id := Get_Name_Entity_Id (Chars (N));
2859 while Present (Op_Id) loop
2860 if Ekind (Op_Id) = E_Operator then
2861 Find_Boolean_Types (L, R, Op_Id, N);
2862 else
2863 Analyze_User_Defined_Binary_Op (N, Op_Id);
2864 end if;
2865
2866 Op_Id := Homonym (Op_Id);
2867 end loop;
2868 end if;
2869
2870 Operator_Check (N);
2871 Check_Function_Writable_Actuals (N);
2872 end Analyze_Logical_Op;
2873
2874 ---------------------------
2875 -- Analyze_Membership_Op --
2876 ---------------------------
2877
2878 procedure Analyze_Membership_Op (N : Node_Id) is
2879 Loc : constant Source_Ptr := Sloc (N);
2880 L : constant Node_Id := Left_Opnd (N);
2881 R : constant Node_Id := Right_Opnd (N);
2882
2883 Index : Interp_Index;
2884 It : Interp;
2885 Found : Boolean := False;
2886 I_F : Interp_Index;
2887 T_F : Entity_Id;
2888
2889 procedure Try_One_Interp (T1 : Entity_Id);
2890 -- Routine to try one proposed interpretation. Note that the context
2891 -- of the operation plays no role in resolving the arguments, so that
2892 -- if there is more than one interpretation of the operands that is
2893 -- compatible with a membership test, the operation is ambiguous.
2894
2895 --------------------
2896 -- Try_One_Interp --
2897 --------------------
2898
2899 procedure Try_One_Interp (T1 : Entity_Id) is
2900 begin
2901 if Has_Compatible_Type (R, T1) then
2902 if Found
2903 and then Base_Type (T1) /= Base_Type (T_F)
2904 then
2905 It := Disambiguate (L, I_F, Index, Any_Type);
2906
2907 if It = No_Interp then
2908 Ambiguous_Operands (N);
2909 Set_Etype (L, Any_Type);
2910 return;
2911
2912 else
2913 T_F := It.Typ;
2914 end if;
2915
2916 else
2917 Found := True;
2918 T_F := T1;
2919 I_F := Index;
2920 end if;
2921
2922 Set_Etype (L, T_F);
2923 end if;
2924 end Try_One_Interp;
2925
2926 procedure Analyze_Set_Membership;
2927 -- If a set of alternatives is present, analyze each and find the
2928 -- common type to which they must all resolve.
2929
2930 ----------------------------
2931 -- Analyze_Set_Membership --
2932 ----------------------------
2933
2934 procedure Analyze_Set_Membership is
2935 Alt : Node_Id;
2936 Index : Interp_Index;
2937 It : Interp;
2938 Candidate_Interps : Node_Id;
2939 Common_Type : Entity_Id := Empty;
2940
2941 begin
2942 if Comes_From_Source (N) then
2943 Check_Compiler_Unit ("set membership", N);
2944 end if;
2945
2946 Analyze (L);
2947 Candidate_Interps := L;
2948
2949 if not Is_Overloaded (L) then
2950 Common_Type := Etype (L);
2951
2952 Alt := First (Alternatives (N));
2953 while Present (Alt) loop
2954 Analyze (Alt);
2955
2956 if not Has_Compatible_Type (Alt, Common_Type) then
2957 Wrong_Type (Alt, Common_Type);
2958 end if;
2959
2960 Next (Alt);
2961 end loop;
2962
2963 else
2964 Alt := First (Alternatives (N));
2965 while Present (Alt) loop
2966 Analyze (Alt);
2967 if not Is_Overloaded (Alt) then
2968 Common_Type := Etype (Alt);
2969
2970 else
2971 Get_First_Interp (Alt, Index, It);
2972 while Present (It.Typ) loop
2973 if not
2974 Has_Compatible_Type (Candidate_Interps, It.Typ)
2975 then
2976 Remove_Interp (Index);
2977 end if;
2978
2979 Get_Next_Interp (Index, It);
2980 end loop;
2981
2982 Get_First_Interp (Alt, Index, It);
2983
2984 if No (It.Typ) then
2985 Error_Msg_N ("alternative has no legal type", Alt);
2986 return;
2987 end if;
2988
2989 -- If alternative is not overloaded, we have a unique type
2990 -- for all of them.
2991
2992 Set_Etype (Alt, It.Typ);
2993
2994 -- If the alternative is an enumeration literal, use the one
2995 -- for this interpretation.
2996
2997 if Is_Entity_Name (Alt) then
2998 Set_Entity (Alt, It.Nam);
2999 end if;
3000
3001 Get_Next_Interp (Index, It);
3002
3003 if No (It.Typ) then
3004 Set_Is_Overloaded (Alt, False);
3005 Common_Type := Etype (Alt);
3006 end if;
3007
3008 Candidate_Interps := Alt;
3009 end if;
3010
3011 Next (Alt);
3012 end loop;
3013 end if;
3014
3015 Set_Etype (N, Standard_Boolean);
3016
3017 if Present (Common_Type) then
3018 Set_Etype (L, Common_Type);
3019
3020 -- The left operand may still be overloaded, to be resolved using
3021 -- the Common_Type.
3022
3023 else
3024 Error_Msg_N ("cannot resolve membership operation", N);
3025 end if;
3026 end Analyze_Set_Membership;
3027
3028 -- Start of processing for Analyze_Membership_Op
3029
3030 begin
3031 Analyze_Expression (L);
3032
3033 if No (R) then
3034 pragma Assert (Ada_Version >= Ada_2012);
3035 Analyze_Set_Membership;
3036 Check_Function_Writable_Actuals (N);
3037 return;
3038 end if;
3039
3040 if Nkind (R) = N_Range
3041 or else (Nkind (R) = N_Attribute_Reference
3042 and then Attribute_Name (R) = Name_Range)
3043 then
3044 Analyze (R);
3045
3046 if not Is_Overloaded (L) then
3047 Try_One_Interp (Etype (L));
3048
3049 else
3050 Get_First_Interp (L, Index, It);
3051 while Present (It.Typ) loop
3052 Try_One_Interp (It.Typ);
3053 Get_Next_Interp (Index, It);
3054 end loop;
3055 end if;
3056
3057 -- If not a range, it can be a subtype mark, or else it is a degenerate
3058 -- membership test with a singleton value, i.e. a test for equality,
3059 -- if the types are compatible.
3060
3061 else
3062 Analyze (R);
3063
3064 if Is_Entity_Name (R)
3065 and then Is_Type (Entity (R))
3066 then
3067 Find_Type (R);
3068 Check_Fully_Declared (Entity (R), R);
3069
3070 elsif Ada_Version >= Ada_2012
3071 and then Has_Compatible_Type (R, Etype (L))
3072 then
3073 if Nkind (N) = N_In then
3074 Rewrite (N,
3075 Make_Op_Eq (Loc,
3076 Left_Opnd => L,
3077 Right_Opnd => R));
3078 else
3079 Rewrite (N,
3080 Make_Op_Ne (Loc,
3081 Left_Opnd => L,
3082 Right_Opnd => R));
3083 end if;
3084
3085 Analyze (N);
3086 return;
3087
3088 else
3089 -- In all versions of the language, if we reach this point there
3090 -- is a previous error that will be diagnosed below.
3091
3092 Find_Type (R);
3093 end if;
3094 end if;
3095
3096 -- Compatibility between expression and subtype mark or range is
3097 -- checked during resolution. The result of the operation is Boolean
3098 -- in any case.
3099
3100 Set_Etype (N, Standard_Boolean);
3101
3102 if Comes_From_Source (N)
3103 and then Present (Right_Opnd (N))
3104 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3105 then
3106 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3107 end if;
3108
3109 Check_Function_Writable_Actuals (N);
3110 end Analyze_Membership_Op;
3111
3112 -----------------
3113 -- Analyze_Mod --
3114 -----------------
3115
3116 procedure Analyze_Mod (N : Node_Id) is
3117 begin
3118 -- A special warning check, if we have an expression of the form:
3119 -- expr mod 2 * literal
3120 -- where literal is 64 or less, then probably what was meant was
3121 -- expr mod 2 ** literal
3122 -- so issue an appropriate warning.
3123
3124 if Warn_On_Suspicious_Modulus_Value
3125 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3126 and then Intval (Right_Opnd (N)) = Uint_2
3127 and then Nkind (Parent (N)) = N_Op_Multiply
3128 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3129 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
3130 then
3131 Error_Msg_N
3132 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
3133 end if;
3134
3135 -- Remaining processing is same as for other arithmetic operators
3136
3137 Analyze_Arithmetic_Op (N);
3138 end Analyze_Mod;
3139
3140 ----------------------
3141 -- Analyze_Negation --
3142 ----------------------
3143
3144 procedure Analyze_Negation (N : Node_Id) is
3145 R : constant Node_Id := Right_Opnd (N);
3146 Op_Id : Entity_Id := Entity (N);
3147
3148 begin
3149 Set_Etype (N, Any_Type);
3150 Candidate_Type := Empty;
3151
3152 Analyze_Expression (R);
3153
3154 if Present (Op_Id) then
3155 if Ekind (Op_Id) = E_Operator then
3156 Find_Negation_Types (R, Op_Id, N);
3157 else
3158 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3159 end if;
3160
3161 else
3162 Op_Id := Get_Name_Entity_Id (Chars (N));
3163 while Present (Op_Id) loop
3164 if Ekind (Op_Id) = E_Operator then
3165 Find_Negation_Types (R, Op_Id, N);
3166 else
3167 Analyze_User_Defined_Unary_Op (N, Op_Id);
3168 end if;
3169
3170 Op_Id := Homonym (Op_Id);
3171 end loop;
3172 end if;
3173
3174 Operator_Check (N);
3175 end Analyze_Negation;
3176
3177 ------------------
3178 -- Analyze_Null --
3179 ------------------
3180
3181 procedure Analyze_Null (N : Node_Id) is
3182 begin
3183 Check_SPARK_05_Restriction ("null is not allowed", N);
3184
3185 Set_Etype (N, Any_Access);
3186 end Analyze_Null;
3187
3188 ----------------------
3189 -- Analyze_One_Call --
3190 ----------------------
3191
3192 procedure Analyze_One_Call
3193 (N : Node_Id;
3194 Nam : Entity_Id;
3195 Report : Boolean;
3196 Success : out Boolean;
3197 Skip_First : Boolean := False)
3198 is
3199 Actuals : constant List_Id := Parameter_Associations (N);
3200 Prev_T : constant Entity_Id := Etype (N);
3201
3202 -- Recognize cases of prefixed calls that have been rewritten in
3203 -- various ways. The simplest case is a rewritten selected component,
3204 -- but it can also be an already-examined indexed component, or a
3205 -- prefix that is itself a rewritten prefixed call that is in turn
3206 -- an indexed call (the syntactic ambiguity involving the indexing of
3207 -- a function with defaulted parameters that returns an array).
3208 -- A flag Maybe_Indexed_Call might be useful here ???
3209
3210 Must_Skip : constant Boolean := Skip_First
3211 or else Nkind (Original_Node (N)) = N_Selected_Component
3212 or else
3213 (Nkind (Original_Node (N)) = N_Indexed_Component
3214 and then Nkind (Prefix (Original_Node (N))) =
3215 N_Selected_Component)
3216 or else
3217 (Nkind (Parent (N)) = N_Function_Call
3218 and then Is_Array_Type (Etype (Name (N)))
3219 and then Etype (Original_Node (N)) =
3220 Component_Type (Etype (Name (N)))
3221 and then Nkind (Original_Node (Parent (N))) =
3222 N_Selected_Component);
3223
3224 -- The first formal must be omitted from the match when trying to find
3225 -- a primitive operation that is a possible interpretation, and also
3226 -- after the call has been rewritten, because the corresponding actual
3227 -- is already known to be compatible, and because this may be an
3228 -- indexing of a call with default parameters.
3229
3230 Formal : Entity_Id;
3231 Actual : Node_Id;
3232 Is_Indexed : Boolean := False;
3233 Is_Indirect : Boolean := False;
3234 Subp_Type : constant Entity_Id := Etype (Nam);
3235 Norm_OK : Boolean;
3236
3237 function Compatible_Types_In_Predicate
3238 (T1 : Entity_Id;
3239 T2 : Entity_Id) return Boolean;
3240 -- For an Ada 2012 predicate or invariant, a call may mention an
3241 -- incomplete type, while resolution of the corresponding predicate
3242 -- function may see the full view, as a consequence of the delayed
3243 -- resolution of the corresponding expressions. This may occur in
3244 -- the body of a predicate function, or in a call to such. Anomalies
3245 -- involving private and full views can also happen. In each case,
3246 -- rewrite node or add conversions to remove spurious type errors.
3247
3248 procedure Indicate_Name_And_Type;
3249 -- If candidate interpretation matches, indicate name and type of result
3250 -- on call node.
3251
3252 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3253 -- There may be a user-defined operator that hides the current
3254 -- interpretation. We must check for this independently of the
3255 -- analysis of the call with the user-defined operation, because
3256 -- the parameter names may be wrong and yet the hiding takes place.
3257 -- This fixes a problem with ACATS test B34014O.
3258 --
3259 -- When the type Address is a visible integer type, and the DEC
3260 -- system extension is visible, the predefined operator may be
3261 -- hidden as well, by one of the address operations in auxdec.
3262 -- Finally, The abstract operations on address do not hide the
3263 -- predefined operator (this is the purpose of making them abstract).
3264
3265 -----------------------------------
3266 -- Compatible_Types_In_Predicate --
3267 -----------------------------------
3268
3269 function Compatible_Types_In_Predicate
3270 (T1 : Entity_Id;
3271 T2 : Entity_Id) return Boolean
3272 is
3273 function Common_Type (T : Entity_Id) return Entity_Id;
3274 -- Find non-private full view if any, without going to ancestor type
3275 -- (as opposed to Underlying_Type).
3276
3277 -----------------
3278 -- Common_Type --
3279 -----------------
3280
3281 function Common_Type (T : Entity_Id) return Entity_Id is
3282 begin
3283 if Is_Private_Type (T) and then Present (Full_View (T)) then
3284 return Base_Type (Full_View (T));
3285 else
3286 return Base_Type (T);
3287 end if;
3288 end Common_Type;
3289
3290 -- Start of processing for Compatible_Types_In_Predicate
3291
3292 begin
3293 if (Ekind (Current_Scope) = E_Function
3294 and then Is_Predicate_Function (Current_Scope))
3295 or else
3296 (Ekind (Nam) = E_Function
3297 and then Is_Predicate_Function (Nam))
3298 then
3299 if Is_Incomplete_Type (T1)
3300 and then Present (Full_View (T1))
3301 and then Full_View (T1) = T2
3302 then
3303 Set_Etype (Formal, Etype (Actual));
3304 return True;
3305
3306 elsif Common_Type (T1) = Common_Type (T2) then
3307 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3308 return True;
3309
3310 else
3311 return False;
3312 end if;
3313
3314 else
3315 return False;
3316 end if;
3317 end Compatible_Types_In_Predicate;
3318
3319 ----------------------------
3320 -- Indicate_Name_And_Type --
3321 ----------------------------
3322
3323 procedure Indicate_Name_And_Type is
3324 begin
3325 Add_One_Interp (N, Nam, Etype (Nam));
3326 Check_Implicit_Dereference (N, Etype (Nam));
3327 Success := True;
3328
3329 -- If the prefix of the call is a name, indicate the entity
3330 -- being called. If it is not a name, it is an expression that
3331 -- denotes an access to subprogram or else an entry or family. In
3332 -- the latter case, the name is a selected component, and the entity
3333 -- being called is noted on the selector.
3334
3335 if not Is_Type (Nam) then
3336 if Is_Entity_Name (Name (N)) then
3337 Set_Entity (Name (N), Nam);
3338 Set_Etype (Name (N), Etype (Nam));
3339
3340 elsif Nkind (Name (N)) = N_Selected_Component then
3341 Set_Entity (Selector_Name (Name (N)), Nam);
3342 end if;
3343 end if;
3344
3345 if Debug_Flag_E and not Report then
3346 Write_Str (" Overloaded call ");
3347 Write_Int (Int (N));
3348 Write_Str (" compatible with ");
3349 Write_Int (Int (Nam));
3350 Write_Eol;
3351 end if;
3352 end Indicate_Name_And_Type;
3353
3354 ------------------------
3355 -- Operator_Hidden_By --
3356 ------------------------
3357
3358 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3359 Act1 : constant Node_Id := First_Actual (N);
3360 Act2 : constant Node_Id := Next_Actual (Act1);
3361 Form1 : constant Entity_Id := First_Formal (Fun);
3362 Form2 : constant Entity_Id := Next_Formal (Form1);
3363
3364 begin
3365 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
3366 return False;
3367
3368 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3369 return False;
3370
3371 elsif Present (Form2) then
3372 if No (Act2)
3373 or else not Has_Compatible_Type (Act2, Etype (Form2))
3374 then
3375 return False;
3376 end if;
3377
3378 elsif Present (Act2) then
3379 return False;
3380 end if;
3381
3382 -- Now we know that the arity of the operator matches the function,
3383 -- and the function call is a valid interpretation. The function
3384 -- hides the operator if it has the right signature, or if one of
3385 -- its operands is a non-abstract operation on Address when this is
3386 -- a visible integer type.
3387
3388 return Hides_Op (Fun, Nam)
3389 or else Is_Descendant_Of_Address (Etype (Form1))
3390 or else
3391 (Present (Form2)
3392 and then Is_Descendant_Of_Address (Etype (Form2)));
3393 end Operator_Hidden_By;
3394
3395 -- Start of processing for Analyze_One_Call
3396
3397 begin
3398 Success := False;
3399
3400 -- If the subprogram has no formals or if all the formals have defaults,
3401 -- and the return type is an array type, the node may denote an indexing
3402 -- of the result of a parameterless call. In Ada 2005, the subprogram
3403 -- may have one non-defaulted formal, and the call may have been written
3404 -- in prefix notation, so that the rebuilt parameter list has more than
3405 -- one actual.
3406
3407 if not Is_Overloadable (Nam)
3408 and then Ekind (Nam) /= E_Subprogram_Type
3409 and then Ekind (Nam) /= E_Entry_Family
3410 then
3411 return;
3412 end if;
3413
3414 -- An indexing requires at least one actual. The name of the call cannot
3415 -- be an implicit indirect call, so it cannot be a generated explicit
3416 -- dereference.
3417
3418 if not Is_Empty_List (Actuals)
3419 and then
3420 (Needs_No_Actuals (Nam)
3421 or else
3422 (Needs_One_Actual (Nam)
3423 and then Present (Next_Actual (First (Actuals)))))
3424 then
3425 if Is_Array_Type (Subp_Type)
3426 and then
3427 (Nkind (Name (N)) /= N_Explicit_Dereference
3428 or else Comes_From_Source (Name (N)))
3429 then
3430 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3431
3432 elsif Is_Access_Type (Subp_Type)
3433 and then Is_Array_Type (Designated_Type (Subp_Type))
3434 then
3435 Is_Indexed :=
3436 Try_Indexed_Call
3437 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3438
3439 -- The prefix can also be a parameterless function that returns an
3440 -- access to subprogram, in which case this is an indirect call.
3441 -- If this succeeds, an explicit dereference is added later on,
3442 -- in Analyze_Call or Resolve_Call.
3443
3444 elsif Is_Access_Type (Subp_Type)
3445 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3446 then
3447 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3448 end if;
3449
3450 end if;
3451
3452 -- If the call has been transformed into a slice, it is of the form
3453 -- F (Subtype) where F is parameterless. The node has been rewritten in
3454 -- Try_Indexed_Call and there is nothing else to do.
3455
3456 if Is_Indexed
3457 and then Nkind (N) = N_Slice
3458 then
3459 return;
3460 end if;
3461
3462 Normalize_Actuals
3463 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3464
3465 if not Norm_OK then
3466
3467 -- If an indirect call is a possible interpretation, indicate
3468 -- success to the caller. This may be an indexing of an explicit
3469 -- dereference of a call that returns an access type (see above).
3470
3471 if Is_Indirect
3472 or else (Is_Indexed
3473 and then Nkind (Name (N)) = N_Explicit_Dereference
3474 and then Comes_From_Source (Name (N)))
3475 then
3476 Success := True;
3477 return;
3478
3479 -- Mismatch in number or names of parameters
3480
3481 elsif Debug_Flag_E then
3482 Write_Str (" normalization fails in call ");
3483 Write_Int (Int (N));
3484 Write_Str (" with subprogram ");
3485 Write_Int (Int (Nam));
3486 Write_Eol;
3487 end if;
3488
3489 -- If the context expects a function call, discard any interpretation
3490 -- that is a procedure. If the node is not overloaded, leave as is for
3491 -- better error reporting when type mismatch is found.
3492
3493 elsif Nkind (N) = N_Function_Call
3494 and then Is_Overloaded (Name (N))
3495 and then Ekind (Nam) = E_Procedure
3496 then
3497 return;
3498
3499 -- Ditto for function calls in a procedure context
3500
3501 elsif Nkind (N) = N_Procedure_Call_Statement
3502 and then Is_Overloaded (Name (N))
3503 and then Etype (Nam) /= Standard_Void_Type
3504 then
3505 return;
3506
3507 elsif No (Actuals) then
3508
3509 -- If Normalize succeeds, then there are default parameters for
3510 -- all formals.
3511
3512 Indicate_Name_And_Type;
3513
3514 elsif Ekind (Nam) = E_Operator then
3515 if Nkind (N) = N_Procedure_Call_Statement then
3516 return;
3517 end if;
3518
3519 -- This can occur when the prefix of the call is an operator
3520 -- name or an expanded name whose selector is an operator name.
3521
3522 Analyze_Operator_Call (N, Nam);
3523
3524 if Etype (N) /= Prev_T then
3525
3526 -- Check that operator is not hidden by a function interpretation
3527
3528 if Is_Overloaded (Name (N)) then
3529 declare
3530 I : Interp_Index;
3531 It : Interp;
3532
3533 begin
3534 Get_First_Interp (Name (N), I, It);
3535 while Present (It.Nam) loop
3536 if Operator_Hidden_By (It.Nam) then
3537 Set_Etype (N, Prev_T);
3538 return;
3539 end if;
3540
3541 Get_Next_Interp (I, It);
3542 end loop;
3543 end;
3544 end if;
3545
3546 -- If operator matches formals, record its name on the call.
3547 -- If the operator is overloaded, Resolve will select the
3548 -- correct one from the list of interpretations. The call
3549 -- node itself carries the first candidate.
3550
3551 Set_Entity (Name (N), Nam);
3552 Success := True;
3553
3554 elsif Report and then Etype (N) = Any_Type then
3555 Error_Msg_N ("incompatible arguments for operator", N);
3556 end if;
3557
3558 else
3559 -- Normalize_Actuals has chained the named associations in the
3560 -- correct order of the formals.
3561
3562 Actual := First_Actual (N);
3563 Formal := First_Formal (Nam);
3564
3565 -- If we are analyzing a call rewritten from object notation, skip
3566 -- first actual, which may be rewritten later as an explicit
3567 -- dereference.
3568
3569 if Must_Skip then
3570 Next_Actual (Actual);
3571 Next_Formal (Formal);
3572 end if;
3573
3574 while Present (Actual) and then Present (Formal) loop
3575 if Nkind (Parent (Actual)) /= N_Parameter_Association
3576 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3577 then
3578 -- The actual can be compatible with the formal, but we must
3579 -- also check that the context is not an address type that is
3580 -- visibly an integer type. In this case the use of literals is
3581 -- illegal, except in the body of descendants of system, where
3582 -- arithmetic operations on address are of course used.
3583
3584 if Has_Compatible_Type (Actual, Etype (Formal))
3585 and then
3586 (Etype (Actual) /= Universal_Integer
3587 or else not Is_Descendant_Of_Address (Etype (Formal))
3588 or else In_Predefined_Unit (N))
3589 then
3590 Next_Actual (Actual);
3591 Next_Formal (Formal);
3592
3593 -- In Allow_Integer_Address mode, we allow an actual integer to
3594 -- match a formal address type and vice versa. We only do this
3595 -- if we are certain that an error will otherwise be issued
3596
3597 elsif Address_Integer_Convert_OK
3598 (Etype (Actual), Etype (Formal))
3599 and then (Report and not Is_Indexed and not Is_Indirect)
3600 then
3601 -- Handle this case by introducing an unchecked conversion
3602
3603 Rewrite (Actual,
3604 Unchecked_Convert_To (Etype (Formal),
3605 Relocate_Node (Actual)));
3606 Analyze_And_Resolve (Actual, Etype (Formal));
3607 Next_Actual (Actual);
3608 Next_Formal (Formal);
3609
3610 -- Under relaxed RM semantics silently replace occurrences of
3611 -- null by System.Address_Null. We only do this if we know that
3612 -- an error will otherwise be issued.
3613
3614 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3615 and then (Report and not Is_Indexed and not Is_Indirect)
3616 then
3617 Replace_Null_By_Null_Address (Actual);
3618 Analyze_And_Resolve (Actual, Etype (Formal));
3619 Next_Actual (Actual);
3620 Next_Formal (Formal);
3621
3622 elsif Compatible_Types_In_Predicate
3623 (Etype (Formal), Etype (Actual))
3624 then
3625 Next_Actual (Actual);
3626 Next_Formal (Formal);
3627
3628 -- Handle failed type check
3629
3630 else
3631 if Debug_Flag_E then
3632 Write_Str (" type checking fails in call ");
3633 Write_Int (Int (N));
3634 Write_Str (" with formal ");
3635 Write_Int (Int (Formal));
3636 Write_Str (" in subprogram ");
3637 Write_Int (Int (Nam));
3638 Write_Eol;
3639 end if;
3640
3641 -- Comment needed on the following test???
3642
3643 if Report and not Is_Indexed and not Is_Indirect then
3644
3645 -- Ada 2005 (AI-251): Complete the error notification
3646 -- to help new Ada 2005 users.
3647
3648 if Is_Class_Wide_Type (Etype (Formal))
3649 and then Is_Interface (Etype (Etype (Formal)))
3650 and then not Interface_Present_In_Ancestor
3651 (Typ => Etype (Actual),
3652 Iface => Etype (Etype (Formal)))
3653 then
3654 Error_Msg_NE
3655 ("(Ada 2005) does not implement interface }",
3656 Actual, Etype (Etype (Formal)));
3657 end if;
3658
3659 Wrong_Type (Actual, Etype (Formal));
3660
3661 if Nkind (Actual) = N_Op_Eq
3662 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3663 then
3664 Formal := First_Formal (Nam);
3665 while Present (Formal) loop
3666 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3667 Error_Msg_N -- CODEFIX
3668 ("possible misspelling of `='>`!", Actual);
3669 exit;
3670 end if;
3671
3672 Next_Formal (Formal);
3673 end loop;
3674 end if;
3675
3676 if All_Errors_Mode then
3677 Error_Msg_Sloc := Sloc (Nam);
3678
3679 if Etype (Formal) = Any_Type then
3680 Error_Msg_N
3681 ("there is no legal actual parameter", Actual);
3682 end if;
3683
3684 if Is_Overloadable (Nam)
3685 and then Present (Alias (Nam))
3686 and then not Comes_From_Source (Nam)
3687 then
3688 Error_Msg_NE
3689 ("\\ =='> in call to inherited operation & #!",
3690 Actual, Nam);
3691
3692 elsif Ekind (Nam) = E_Subprogram_Type then
3693 declare
3694 Access_To_Subprogram_Typ :
3695 constant Entity_Id :=
3696 Defining_Identifier
3697 (Associated_Node_For_Itype (Nam));
3698 begin
3699 Error_Msg_NE
3700 ("\\ =='> in call to dereference of &#!",
3701 Actual, Access_To_Subprogram_Typ);
3702 end;
3703
3704 else
3705 Error_Msg_NE
3706 ("\\ =='> in call to &#!", Actual, Nam);
3707
3708 end if;
3709 end if;
3710 end if;
3711
3712 return;
3713 end if;
3714
3715 else
3716 -- Normalize_Actuals has verified that a default value exists
3717 -- for this formal. Current actual names a subsequent formal.
3718
3719 Next_Formal (Formal);
3720 end if;
3721 end loop;
3722
3723 -- On exit, all actuals match
3724
3725 Indicate_Name_And_Type;
3726 end if;
3727 end Analyze_One_Call;
3728
3729 ---------------------------
3730 -- Analyze_Operator_Call --
3731 ---------------------------
3732
3733 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3734 Op_Name : constant Name_Id := Chars (Op_Id);
3735 Act1 : constant Node_Id := First_Actual (N);
3736 Act2 : constant Node_Id := Next_Actual (Act1);
3737
3738 begin
3739 -- Binary operator case
3740
3741 if Present (Act2) then
3742
3743 -- If more than two operands, then not binary operator after all
3744
3745 if Present (Next_Actual (Act2)) then
3746 return;
3747 end if;
3748
3749 -- Otherwise action depends on operator
3750
3751 case Op_Name is
3752 when Name_Op_Add
3753 | Name_Op_Divide
3754 | Name_Op_Expon
3755 | Name_Op_Mod
3756 | Name_Op_Multiply
3757 | Name_Op_Rem
3758 | Name_Op_Subtract
3759 =>
3760 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3761
3762 when Name_Op_And
3763 | Name_Op_Or
3764 | Name_Op_Xor
3765 =>
3766 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3767
3768 when Name_Op_Ge
3769 | Name_Op_Gt
3770 | Name_Op_Le
3771 | Name_Op_Lt
3772 =>
3773 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3774
3775 when Name_Op_Eq
3776 | Name_Op_Ne
3777 =>
3778 Find_Equality_Types (Act1, Act2, Op_Id, N);
3779
3780 when Name_Op_Concat =>
3781 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3782
3783 -- Is this when others, or should it be an abort???
3784
3785 when others =>
3786 null;
3787 end case;
3788
3789 -- Unary operator case
3790
3791 else
3792 case Op_Name is
3793 when Name_Op_Abs
3794 | Name_Op_Add
3795 | Name_Op_Subtract
3796 =>
3797 Find_Unary_Types (Act1, Op_Id, N);
3798
3799 when Name_Op_Not =>
3800 Find_Negation_Types (Act1, Op_Id, N);
3801
3802 -- Is this when others correct, or should it be an abort???
3803
3804 when others =>
3805 null;
3806 end case;
3807 end if;
3808 end Analyze_Operator_Call;
3809
3810 -------------------------------------------
3811 -- Analyze_Overloaded_Selected_Component --
3812 -------------------------------------------
3813
3814 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3815 Nam : constant Node_Id := Prefix (N);
3816 Sel : constant Node_Id := Selector_Name (N);
3817 Comp : Entity_Id;
3818 I : Interp_Index;
3819 It : Interp;
3820 T : Entity_Id;
3821
3822 begin
3823 Set_Etype (Sel, Any_Type);
3824
3825 Get_First_Interp (Nam, I, It);
3826 while Present (It.Typ) loop
3827 if Is_Access_Type (It.Typ) then
3828 T := Designated_Type (It.Typ);
3829 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3830 else
3831 T := It.Typ;
3832 end if;
3833
3834 -- Locate the component. For a private prefix the selector can denote
3835 -- a discriminant.
3836
3837 if Is_Record_Type (T) or else Is_Private_Type (T) then
3838
3839 -- If the prefix is a class-wide type, the visible components are
3840 -- those of the base type.
3841
3842 if Is_Class_Wide_Type (T) then
3843 T := Etype (T);
3844 end if;
3845
3846 Comp := First_Entity (T);
3847 while Present (Comp) loop
3848 if Chars (Comp) = Chars (Sel)
3849 and then Is_Visible_Component (Comp, Sel)
3850 then
3851
3852 -- AI05-105: if the context is an object renaming with
3853 -- an anonymous access type, the expected type of the
3854 -- object must be anonymous. This is a name resolution rule.
3855
3856 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3857 or else No (Access_Definition (Parent (N)))
3858 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3859 or else
3860 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3861 then
3862 Set_Entity (Sel, Comp);
3863 Set_Etype (Sel, Etype (Comp));
3864 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3865 Check_Implicit_Dereference (N, Etype (Comp));
3866
3867 -- This also specifies a candidate to resolve the name.
3868 -- Further overloading will be resolved from context.
3869 -- The selector name itself does not carry overloading
3870 -- information.
3871
3872 Set_Etype (Nam, It.Typ);
3873
3874 else
3875 -- Named access type in the context of a renaming
3876 -- declaration with an access definition. Remove
3877 -- inapplicable candidate.
3878
3879 Remove_Interp (I);
3880 end if;
3881 end if;
3882
3883 Next_Entity (Comp);
3884 end loop;
3885
3886 elsif Is_Concurrent_Type (T) then
3887 Comp := First_Entity (T);
3888 while Present (Comp)
3889 and then Comp /= First_Private_Entity (T)
3890 loop
3891 if Chars (Comp) = Chars (Sel) then
3892 if Is_Overloadable (Comp) then
3893 Add_One_Interp (Sel, Comp, Etype (Comp));
3894 else
3895 Set_Entity_With_Checks (Sel, Comp);
3896 Generate_Reference (Comp, Sel);
3897 end if;
3898
3899 Set_Etype (Sel, Etype (Comp));
3900 Set_Etype (N, Etype (Comp));
3901 Set_Etype (Nam, It.Typ);
3902
3903 -- For access type case, introduce explicit dereference for
3904 -- more uniform treatment of entry calls. Do this only once
3905 -- if several interpretations yield an access type.
3906
3907 if Is_Access_Type (Etype (Nam))
3908 and then Nkind (Nam) /= N_Explicit_Dereference
3909 then
3910 Insert_Explicit_Dereference (Nam);
3911 Error_Msg_NW
3912 (Warn_On_Dereference, "?d?implicit dereference", N);
3913 end if;
3914 end if;
3915
3916 Next_Entity (Comp);
3917 end loop;
3918
3919 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3920 end if;
3921
3922 Get_Next_Interp (I, It);
3923 end loop;
3924
3925 if Etype (N) = Any_Type
3926 and then not Try_Object_Operation (N)
3927 then
3928 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3929 Set_Entity (Sel, Any_Id);
3930 Set_Etype (Sel, Any_Type);
3931 end if;
3932 end Analyze_Overloaded_Selected_Component;
3933
3934 ----------------------------------
3935 -- Analyze_Qualified_Expression --
3936 ----------------------------------
3937
3938 procedure Analyze_Qualified_Expression (N : Node_Id) is
3939 Mark : constant Entity_Id := Subtype_Mark (N);
3940 Expr : constant Node_Id := Expression (N);
3941 I : Interp_Index;
3942 It : Interp;
3943 T : Entity_Id;
3944
3945 begin
3946 Analyze_Expression (Expr);
3947
3948 Set_Etype (N, Any_Type);
3949 Find_Type (Mark);
3950 T := Entity (Mark);
3951
3952 if Nkind_In (Enclosing_Declaration (N), N_Formal_Type_Declaration,
3953 N_Full_Type_Declaration,
3954 N_Incomplete_Type_Declaration,
3955 N_Protected_Type_Declaration,
3956 N_Private_Extension_Declaration,
3957 N_Private_Type_Declaration,
3958 N_Subtype_Declaration,
3959 N_Task_Type_Declaration)
3960 and then T = Defining_Identifier (Enclosing_Declaration (N))
3961 then
3962 Error_Msg_N ("current instance not allowed", Mark);
3963 T := Any_Type;
3964 end if;
3965
3966 Set_Etype (N, T);
3967
3968 if T = Any_Type then
3969 return;
3970 end if;
3971
3972 Check_Fully_Declared (T, N);
3973
3974 -- If expected type is class-wide, check for exact match before
3975 -- expansion, because if the expression is a dispatching call it
3976 -- may be rewritten as explicit dereference with class-wide result.
3977 -- If expression is overloaded, retain only interpretations that
3978 -- will yield exact matches.
3979
3980 if Is_Class_Wide_Type (T) then
3981 if not Is_Overloaded (Expr) then
3982 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3983 if Nkind (Expr) = N_Aggregate then
3984 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3985 else
3986 Wrong_Type (Expr, T);
3987 end if;
3988 end if;
3989
3990 else
3991 Get_First_Interp (Expr, I, It);
3992
3993 while Present (It.Nam) loop
3994 if Base_Type (It.Typ) /= Base_Type (T) then
3995 Remove_Interp (I);
3996 end if;
3997
3998 Get_Next_Interp (I, It);
3999 end loop;
4000 end if;
4001 end if;
4002
4003 Set_Etype (N, T);
4004 end Analyze_Qualified_Expression;
4005
4006 -----------------------------------
4007 -- Analyze_Quantified_Expression --
4008 -----------------------------------
4009
4010 procedure Analyze_Quantified_Expression (N : Node_Id) is
4011 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
4012 -- If the iterator is part of a quantified expression, and the range is
4013 -- known to be statically empty, emit a warning and replace expression
4014 -- with its static value. Returns True if the replacement occurs.
4015
4016 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
4017 -- Determine whether if expression If_Expr lacks an else part or if it
4018 -- has one, it evaluates to True.
4019
4020 --------------------
4021 -- Is_Empty_Range --
4022 --------------------
4023
4024 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4025 Loc : constant Source_Ptr := Sloc (N);
4026
4027 begin
4028 if Is_Array_Type (Typ)
4029 and then Compile_Time_Known_Bounds (Typ)
4030 and then
4031 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4032 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
4033 then
4034 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4035
4036 if All_Present (N) then
4037 Error_Msg_N
4038 ("??quantified expression with ALL "
4039 & "over a null range has value True", N);
4040 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4041
4042 else
4043 Error_Msg_N
4044 ("??quantified expression with SOME "
4045 & "over a null range has value False", N);
4046 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4047 end if;
4048
4049 Analyze (N);
4050 return True;
4051
4052 else
4053 return False;
4054 end if;
4055 end Is_Empty_Range;
4056
4057 -----------------------------
4058 -- No_Else_Or_Trivial_True --
4059 -----------------------------
4060
4061 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4062 Else_Expr : constant Node_Id :=
4063 Next (Next (First (Expressions (If_Expr))));
4064 begin
4065 return
4066 No (Else_Expr)
4067 or else (Compile_Time_Known_Value (Else_Expr)
4068 and then Is_True (Expr_Value (Else_Expr)));
4069 end No_Else_Or_Trivial_True;
4070
4071 -- Local variables
4072
4073 Cond : constant Node_Id := Condition (N);
4074 Loop_Id : Entity_Id;
4075 QE_Scop : Entity_Id;
4076
4077 -- Start of processing for Analyze_Quantified_Expression
4078
4079 begin
4080 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
4081
4082 -- Create a scope to emulate the loop-like behavior of the quantified
4083 -- expression. The scope is needed to provide proper visibility of the
4084 -- loop variable.
4085
4086 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
4087 Set_Etype (QE_Scop, Standard_Void_Type);
4088 Set_Scope (QE_Scop, Current_Scope);
4089 Set_Parent (QE_Scop, N);
4090
4091 Push_Scope (QE_Scop);
4092
4093 -- All constituents are preanalyzed and resolved to avoid untimely
4094 -- generation of various temporaries and types. Full analysis and
4095 -- expansion is carried out when the quantified expression is
4096 -- transformed into an expression with actions.
4097
4098 if Present (Iterator_Specification (N)) then
4099 Preanalyze (Iterator_Specification (N));
4100
4101 -- Do not proceed with the analysis when the range of iteration is
4102 -- empty. The appropriate error is issued by Is_Empty_Range.
4103
4104 if Is_Entity_Name (Name (Iterator_Specification (N)))
4105 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4106 then
4107 return;
4108 end if;
4109
4110 else pragma Assert (Present (Loop_Parameter_Specification (N)));
4111 declare
4112 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4113
4114 begin
4115 Preanalyze (Loop_Par);
4116
4117 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
4118 and then Parent (Loop_Par) /= N
4119 then
4120 -- The parser cannot distinguish between a loop specification
4121 -- and an iterator specification. If after preanalysis the
4122 -- proper form has been recognized, rewrite the expression to
4123 -- reflect the right kind. This is needed for proper ASIS
4124 -- navigation. If expansion is enabled, the transformation is
4125 -- performed when the expression is rewritten as a loop.
4126
4127 Set_Iterator_Specification (N,
4128 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
4129
4130 Set_Defining_Identifier (Iterator_Specification (N),
4131 Relocate_Node (Defining_Identifier (Loop_Par)));
4132 Set_Name (Iterator_Specification (N),
4133 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4134 Set_Comes_From_Source (Iterator_Specification (N),
4135 Comes_From_Source (Loop_Parameter_Specification (N)));
4136 Set_Loop_Parameter_Specification (N, Empty);
4137 end if;
4138 end;
4139 end if;
4140
4141 Preanalyze_And_Resolve (Cond, Standard_Boolean);
4142
4143 End_Scope;
4144 Set_Etype (N, Standard_Boolean);
4145
4146 -- Verify that the loop variable is used within the condition of the
4147 -- quantified expression.
4148
4149 if Present (Iterator_Specification (N)) then
4150 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4151 else
4152 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4153 end if;
4154
4155 if Warn_On_Suspicious_Contract
4156 and then not Referenced (Loop_Id, Cond)
4157 then
4158 -- Generating C, this check causes spurious warnings on inlined
4159 -- postconditions; we can safely disable it because this check
4160 -- was previously performed when analyzing the internally built
4161 -- postconditions procedure.
4162
4163 if Modify_Tree_For_C and then In_Inlined_Body then
4164 null;
4165 else
4166 Error_Msg_N ("?T?unused variable &", Loop_Id);
4167 end if;
4168 end if;
4169
4170 -- Diagnose a possible misuse of the SOME existential quantifier. When
4171 -- we have a quantified expression of the form:
4172
4173 -- for some X => (if P then Q [else True])
4174
4175 -- any value for X that makes P False results in the if expression being
4176 -- trivially True, and so also results in the quantified expression
4177 -- being trivially True.
4178
4179 if Warn_On_Suspicious_Contract
4180 and then not All_Present (N)
4181 and then Nkind (Cond) = N_If_Expression
4182 and then No_Else_Or_Trivial_True (Cond)
4183 then
4184 Error_Msg_N ("?T?suspicious expression", N);
4185 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4186 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4187 end if;
4188 end Analyze_Quantified_Expression;
4189
4190 -------------------
4191 -- Analyze_Range --
4192 -------------------
4193
4194 procedure Analyze_Range (N : Node_Id) is
4195 L : constant Node_Id := Low_Bound (N);
4196 H : constant Node_Id := High_Bound (N);
4197 I1, I2 : Interp_Index;
4198 It1, It2 : Interp;
4199
4200 procedure Check_Common_Type (T1, T2 : Entity_Id);
4201 -- Verify the compatibility of two types, and choose the
4202 -- non universal one if the other is universal.
4203
4204 procedure Check_High_Bound (T : Entity_Id);
4205 -- Test one interpretation of the low bound against all those
4206 -- of the high bound.
4207
4208 procedure Check_Universal_Expression (N : Node_Id);
4209 -- In Ada 83, reject bounds of a universal range that are not literals
4210 -- or entity names.
4211
4212 -----------------------
4213 -- Check_Common_Type --
4214 -----------------------
4215
4216 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4217 begin
4218 if Covers (T1 => T1, T2 => T2)
4219 or else
4220 Covers (T1 => T2, T2 => T1)
4221 then
4222 if T1 = Universal_Integer
4223 or else T1 = Universal_Real
4224 or else T1 = Any_Character
4225 then
4226 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4227
4228 elsif T1 = T2 then
4229 Add_One_Interp (N, T1, T1);
4230
4231 else
4232 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4233 end if;
4234 end if;
4235 end Check_Common_Type;
4236
4237 ----------------------
4238 -- Check_High_Bound --
4239 ----------------------
4240
4241 procedure Check_High_Bound (T : Entity_Id) is
4242 begin
4243 if not Is_Overloaded (H) then
4244 Check_Common_Type (T, Etype (H));
4245 else
4246 Get_First_Interp (H, I2, It2);
4247 while Present (It2.Typ) loop
4248 Check_Common_Type (T, It2.Typ);
4249 Get_Next_Interp (I2, It2);
4250 end loop;
4251 end if;
4252 end Check_High_Bound;
4253
4254 --------------------------------
4255 -- Check_Universal_Expression --
4256 --------------------------------
4257
4258 procedure Check_Universal_Expression (N : Node_Id) is
4259 begin
4260 if Etype (N) = Universal_Integer
4261 and then Nkind (N) /= N_Integer_Literal
4262 and then not Is_Entity_Name (N)
4263 and then Nkind (N) /= N_Attribute_Reference
4264 then
4265 Error_Msg_N ("illegal bound in discrete range", N);
4266 end if;
4267 end Check_Universal_Expression;
4268
4269 -- Start of processing for Analyze_Range
4270
4271 begin
4272 Set_Etype (N, Any_Type);
4273 Analyze_Expression (L);
4274 Analyze_Expression (H);
4275
4276 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4277 return;
4278
4279 else
4280 if not Is_Overloaded (L) then
4281 Check_High_Bound (Etype (L));
4282 else
4283 Get_First_Interp (L, I1, It1);
4284 while Present (It1.Typ) loop
4285 Check_High_Bound (It1.Typ);
4286 Get_Next_Interp (I1, It1);
4287 end loop;
4288 end if;
4289
4290 -- If result is Any_Type, then we did not find a compatible pair
4291
4292 if Etype (N) = Any_Type then
4293 Error_Msg_N ("incompatible types in range ", N);
4294 end if;
4295 end if;
4296
4297 if Ada_Version = Ada_83
4298 and then
4299 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4300 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
4301 then
4302 Check_Universal_Expression (L);
4303 Check_Universal_Expression (H);
4304 end if;
4305
4306 Check_Function_Writable_Actuals (N);
4307 end Analyze_Range;
4308
4309 -----------------------
4310 -- Analyze_Reference --
4311 -----------------------
4312
4313 procedure Analyze_Reference (N : Node_Id) is
4314 P : constant Node_Id := Prefix (N);
4315 E : Entity_Id;
4316 T : Entity_Id;
4317 Acc_Type : Entity_Id;
4318
4319 begin
4320 Analyze (P);
4321
4322 -- An interesting error check, if we take the 'Ref of an object for
4323 -- which a pragma Atomic or Volatile has been given, and the type of the
4324 -- object is not Atomic or Volatile, then we are in trouble. The problem
4325 -- is that no trace of the atomic/volatile status will remain for the
4326 -- backend to respect when it deals with the resulting pointer, since
4327 -- the pointer type will not be marked atomic (it is a pointer to the
4328 -- base type of the object).
4329
4330 -- It is not clear if that can ever occur, but in case it does, we will
4331 -- generate an error message. Not clear if this message can ever be
4332 -- generated, and pretty clear that it represents a bug if it is, still
4333 -- seems worth checking, except in CodePeer mode where we do not really
4334 -- care and don't want to bother the user.
4335
4336 T := Etype (P);
4337
4338 if Is_Entity_Name (P)
4339 and then Is_Object_Reference (P)
4340 and then not CodePeer_Mode
4341 then
4342 E := Entity (P);
4343 T := Etype (P);
4344
4345 if (Has_Atomic_Components (E)
4346 and then not Has_Atomic_Components (T))
4347 or else
4348 (Has_Volatile_Components (E)
4349 and then not Has_Volatile_Components (T))
4350 or else (Is_Atomic (E) and then not Is_Atomic (T))
4351 or else (Is_Volatile (E) and then not Is_Volatile (T))
4352 then
4353 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4354 end if;
4355 end if;
4356
4357 -- Carry on with normal processing
4358
4359 Acc_Type := Create_Itype (E_Allocator_Type, N);
4360 Set_Etype (Acc_Type, Acc_Type);
4361 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4362 Set_Etype (N, Acc_Type);
4363 end Analyze_Reference;
4364
4365 --------------------------------
4366 -- Analyze_Selected_Component --
4367 --------------------------------
4368
4369 -- Prefix is a record type or a task or protected type. In the latter case,
4370 -- the selector must denote a visible entry.
4371
4372 procedure Analyze_Selected_Component (N : Node_Id) is
4373 Name : constant Node_Id := Prefix (N);
4374 Sel : constant Node_Id := Selector_Name (N);
4375 Act_Decl : Node_Id;
4376 Comp : Entity_Id;
4377 Has_Candidate : Boolean := False;
4378 Hidden_Comp : Entity_Id;
4379 In_Scope : Boolean;
4380 Is_Private_Op : Boolean;
4381 Parent_N : Node_Id;
4382 Pent : Entity_Id := Empty;
4383 Prefix_Type : Entity_Id;
4384
4385 Type_To_Use : Entity_Id;
4386 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4387 -- a class-wide type, we use its root type, whose components are
4388 -- present in the class-wide type.
4389
4390 Is_Single_Concurrent_Object : Boolean;
4391 -- Set True if the prefix is a single task or a single protected object
4392
4393 procedure Find_Component_In_Instance (Rec : Entity_Id);
4394 -- In an instance, a component of a private extension may not be visible
4395 -- while it was visible in the generic. Search candidate scope for a
4396 -- component with the proper identifier. This is only done if all other
4397 -- searches have failed. If a match is found, the Etype of both N and
4398 -- Sel are set from this component, and the entity of Sel is set to
4399 -- reference this component. If no match is found, Entity (Sel) remains
4400 -- unset. For a derived type that is an actual of the instance, the
4401 -- desired component may be found in any ancestor.
4402
4403 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4404 -- It is known that the parent of N denotes a subprogram call. Comp
4405 -- is an overloadable component of the concurrent type of the prefix.
4406 -- Determine whether all formals of the parent of N and Comp are mode
4407 -- conformant. If the parent node is not analyzed yet it may be an
4408 -- indexed component rather than a function call.
4409
4410 function Has_Dereference (Nod : Node_Id) return Boolean;
4411 -- Check whether prefix includes a dereference at any level.
4412
4413 --------------------------------
4414 -- Find_Component_In_Instance --
4415 --------------------------------
4416
4417 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4418 Comp : Entity_Id;
4419 Typ : Entity_Id;
4420
4421 begin
4422 Typ := Rec;
4423 while Present (Typ) loop
4424 Comp := First_Component (Typ);
4425 while Present (Comp) loop
4426 if Chars (Comp) = Chars (Sel) then
4427 Set_Entity_With_Checks (Sel, Comp);
4428 Set_Etype (Sel, Etype (Comp));
4429 Set_Etype (N, Etype (Comp));
4430 return;
4431 end if;
4432
4433 Next_Component (Comp);
4434 end loop;
4435
4436 -- If not found, the component may be declared in the parent
4437 -- type or its full view, if any.
4438
4439 if Is_Derived_Type (Typ) then
4440 Typ := Etype (Typ);
4441
4442 if Is_Private_Type (Typ) then
4443 Typ := Full_View (Typ);
4444 end if;
4445
4446 else
4447 return;
4448 end if;
4449 end loop;
4450
4451 -- If we fall through, no match, so no changes made
4452
4453 return;
4454 end Find_Component_In_Instance;
4455
4456 ------------------------------
4457 -- Has_Mode_Conformant_Spec --
4458 ------------------------------
4459
4460 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4461 Comp_Param : Entity_Id;
4462 Param : Node_Id;
4463 Param_Typ : Entity_Id;
4464
4465 begin
4466 Comp_Param := First_Formal (Comp);
4467
4468 if Nkind (Parent (N)) = N_Indexed_Component then
4469 Param := First (Expressions (Parent (N)));
4470 else
4471 Param := First (Parameter_Associations (Parent (N)));
4472 end if;
4473
4474 while Present (Comp_Param)
4475 and then Present (Param)
4476 loop
4477 Param_Typ := Find_Parameter_Type (Param);
4478
4479 if Present (Param_Typ)
4480 and then
4481 not Conforming_Types
4482 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4483 then
4484 return False;
4485 end if;
4486
4487 Next_Formal (Comp_Param);
4488 Next (Param);
4489 end loop;
4490
4491 -- One of the specs has additional formals; there is no match, unless
4492 -- this may be an indexing of a parameterless call.
4493
4494 -- Note that when expansion is disabled, the corresponding record
4495 -- type of synchronized types is not constructed, so that there is
4496 -- no point is attempting an interpretation as a prefixed call, as
4497 -- this is bound to fail because the primitive operations will not
4498 -- be properly located.
4499
4500 if Present (Comp_Param) or else Present (Param) then
4501 if Needs_No_Actuals (Comp)
4502 and then Is_Array_Type (Etype (Comp))
4503 and then not Expander_Active
4504 then
4505 return True;
4506 else
4507 return False;
4508 end if;
4509 end if;
4510
4511 return True;
4512 end Has_Mode_Conformant_Spec;
4513
4514 ---------------------
4515 -- Has_Dereference --
4516 ---------------------
4517
4518 function Has_Dereference (Nod : Node_Id) return Boolean is
4519 begin
4520 if Nkind (Nod) = N_Explicit_Dereference then
4521 return True;
4522
4523 -- When expansion is disabled an explicit dereference may not have
4524 -- been inserted, but if this is an access type the indirection makes
4525 -- the call safe.
4526
4527 elsif Is_Access_Type (Etype (Nod)) then
4528 return True;
4529
4530 elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then
4531 return Has_Dereference (Prefix (Nod));
4532
4533 else
4534 return False;
4535 end if;
4536 end Has_Dereference;
4537
4538 -- Start of processing for Analyze_Selected_Component
4539
4540 begin
4541 Set_Etype (N, Any_Type);
4542
4543 if Is_Overloaded (Name) then
4544 Analyze_Overloaded_Selected_Component (N);
4545 return;
4546
4547 elsif Etype (Name) = Any_Type then
4548 Set_Entity (Sel, Any_Id);
4549 Set_Etype (Sel, Any_Type);
4550 return;
4551
4552 else
4553 Prefix_Type := Etype (Name);
4554 end if;
4555
4556 if Is_Access_Type (Prefix_Type) then
4557
4558 -- A RACW object can never be used as prefix of a selected component
4559 -- since that means it is dereferenced without being a controlling
4560 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4561 -- reporting an error, we must check whether this is actually a
4562 -- dispatching call in prefix form.
4563
4564 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4565 and then Comes_From_Source (N)
4566 then
4567 if Try_Object_Operation (N) then
4568 return;
4569 else
4570 Error_Msg_N
4571 ("invalid dereference of a remote access-to-class-wide value",
4572 N);
4573 end if;
4574
4575 -- Normal case of selected component applied to access type
4576
4577 else
4578 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4579
4580 if Is_Entity_Name (Name) then
4581 Pent := Entity (Name);
4582 elsif Nkind (Name) = N_Selected_Component
4583 and then Is_Entity_Name (Selector_Name (Name))
4584 then
4585 Pent := Entity (Selector_Name (Name));
4586 end if;
4587
4588 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4589 end if;
4590
4591 -- If we have an explicit dereference of a remote access-to-class-wide
4592 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4593 -- have to check for the case of a prefix that is a controlling operand
4594 -- of a prefixed dispatching call, as the dereference is legal in that
4595 -- case. Normally this condition is checked in Validate_Remote_Access_
4596 -- To_Class_Wide_Type, but we have to defer the checking for selected
4597 -- component prefixes because of the prefixed dispatching call case.
4598 -- Note that implicit dereferences are checked for this just above.
4599
4600 elsif Nkind (Name) = N_Explicit_Dereference
4601 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4602 and then Comes_From_Source (N)
4603 then
4604 if Try_Object_Operation (N) then
4605 return;
4606 else
4607 Error_Msg_N
4608 ("invalid dereference of a remote access-to-class-wide value",
4609 N);
4610 end if;
4611 end if;
4612
4613 -- (Ada 2005): if the prefix is the limited view of a type, and
4614 -- the context already includes the full view, use the full view
4615 -- in what follows, either to retrieve a component of to find
4616 -- a primitive operation. If the prefix is an explicit dereference,
4617 -- set the type of the prefix to reflect this transformation.
4618 -- If the nonlimited view is itself an incomplete type, get the
4619 -- full view if available.
4620
4621 if From_Limited_With (Prefix_Type)
4622 and then Has_Non_Limited_View (Prefix_Type)
4623 then
4624 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4625
4626 if Nkind (N) = N_Explicit_Dereference then
4627 Set_Etype (Prefix (N), Prefix_Type);
4628 end if;
4629 end if;
4630
4631 if Ekind (Prefix_Type) = E_Private_Subtype then
4632 Prefix_Type := Base_Type (Prefix_Type);
4633 end if;
4634
4635 Type_To_Use := Prefix_Type;
4636
4637 -- For class-wide types, use the entity list of the root type. This
4638 -- indirection is specially important for private extensions because
4639 -- only the root type get switched (not the class-wide type).
4640
4641 if Is_Class_Wide_Type (Prefix_Type) then
4642 Type_To_Use := Root_Type (Prefix_Type);
4643 end if;
4644
4645 -- If the prefix is a single concurrent object, use its name in error
4646 -- messages, rather than that of its anonymous type.
4647
4648 Is_Single_Concurrent_Object :=
4649 Is_Concurrent_Type (Prefix_Type)
4650 and then Is_Internal_Name (Chars (Prefix_Type))
4651 and then not Is_Derived_Type (Prefix_Type)
4652 and then Is_Entity_Name (Name);
4653
4654 Comp := First_Entity (Type_To_Use);
4655
4656 -- If the selector has an original discriminant, the node appears in
4657 -- an instance. Replace the discriminant with the corresponding one
4658 -- in the current discriminated type. For nested generics, this must
4659 -- be done transitively, so note the new original discriminant.
4660
4661 if Nkind (Sel) = N_Identifier
4662 and then In_Instance
4663 and then Present (Original_Discriminant (Sel))
4664 then
4665 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4666
4667 -- Mark entity before rewriting, for completeness and because
4668 -- subsequent semantic checks might examine the original node.
4669
4670 Set_Entity (Sel, Comp);
4671 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4672 Set_Original_Discriminant (Selector_Name (N), Comp);
4673 Set_Etype (N, Etype (Comp));
4674 Check_Implicit_Dereference (N, Etype (Comp));
4675
4676 if Is_Access_Type (Etype (Name)) then
4677 Insert_Explicit_Dereference (Name);
4678 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4679 end if;
4680
4681 elsif Is_Record_Type (Prefix_Type) then
4682
4683 -- Find component with given name. In an instance, if the node is
4684 -- known as a prefixed call, do not examine components whose
4685 -- visibility may be accidental.
4686
4687 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4688 if Chars (Comp) = Chars (Sel)
4689 and then Is_Visible_Component (Comp, N)
4690 then
4691 Set_Entity_With_Checks (Sel, Comp);
4692 Set_Etype (Sel, Etype (Comp));
4693
4694 if Ekind (Comp) = E_Discriminant then
4695 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4696 Error_Msg_N
4697 ("cannot reference discriminant of unchecked union",
4698 Sel);
4699 end if;
4700
4701 if Is_Generic_Type (Prefix_Type)
4702 or else
4703 Is_Generic_Type (Root_Type (Prefix_Type))
4704 then
4705 Set_Original_Discriminant (Sel, Comp);
4706 end if;
4707 end if;
4708
4709 -- Resolve the prefix early otherwise it is not possible to
4710 -- build the actual subtype of the component: it may need
4711 -- to duplicate this prefix and duplication is only allowed
4712 -- on fully resolved expressions.
4713
4714 Resolve (Name);
4715
4716 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4717 -- subtypes in a package specification.
4718 -- Example:
4719
4720 -- limited with Pkg;
4721 -- package Pkg is
4722 -- type Acc_Inc is access Pkg.T;
4723 -- X : Acc_Inc;
4724 -- N : Natural := X.all.Comp; -- ERROR, limited view
4725 -- end Pkg; -- Comp is not visible
4726
4727 if Nkind (Name) = N_Explicit_Dereference
4728 and then From_Limited_With (Etype (Prefix (Name)))
4729 and then not Is_Potentially_Use_Visible (Etype (Name))
4730 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4731 N_Package_Specification
4732 then
4733 Error_Msg_NE
4734 ("premature usage of incomplete}", Prefix (Name),
4735 Etype (Prefix (Name)));
4736 end if;
4737
4738 -- We never need an actual subtype for the case of a selection
4739 -- for a indexed component of a non-packed array, since in
4740 -- this case gigi generates all the checks and can find the
4741 -- necessary bounds information.
4742
4743 -- We also do not need an actual subtype for the case of a
4744 -- first, last, length, or range attribute applied to a
4745 -- non-packed array, since gigi can again get the bounds in
4746 -- these cases (gigi cannot handle the packed case, since it
4747 -- has the bounds of the packed array type, not the original
4748 -- bounds of the type). However, if the prefix is itself a
4749 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4750 -- as a dynamic-sized temporary, so we do generate an actual
4751 -- subtype for this case.
4752
4753 Parent_N := Parent (N);
4754
4755 if not Is_Packed (Etype (Comp))
4756 and then
4757 ((Nkind (Parent_N) = N_Indexed_Component
4758 and then Nkind (Name) /= N_Selected_Component)
4759 or else
4760 (Nkind (Parent_N) = N_Attribute_Reference
4761 and then
4762 Nam_In (Attribute_Name (Parent_N), Name_First,
4763 Name_Last,
4764 Name_Length,
4765 Name_Range)))
4766 then
4767 Set_Etype (N, Etype (Comp));
4768
4769 -- If full analysis is not enabled, we do not generate an
4770 -- actual subtype, because in the absence of expansion
4771 -- reference to a formal of a protected type, for example,
4772 -- will not be properly transformed, and will lead to
4773 -- out-of-scope references in gigi.
4774
4775 -- In all other cases, we currently build an actual subtype.
4776 -- It seems likely that many of these cases can be avoided,
4777 -- but right now, the front end makes direct references to the
4778 -- bounds (e.g. in generating a length check), and if we do
4779 -- not make an actual subtype, we end up getting a direct
4780 -- reference to a discriminant, which will not do.
4781
4782 elsif Full_Analysis then
4783 Act_Decl :=
4784 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4785 Insert_Action (N, Act_Decl);
4786
4787 if No (Act_Decl) then
4788 Set_Etype (N, Etype (Comp));
4789
4790 else
4791 -- Component type depends on discriminants. Enter the
4792 -- main attributes of the subtype.
4793
4794 declare
4795 Subt : constant Entity_Id :=
4796 Defining_Identifier (Act_Decl);
4797
4798 begin
4799 Set_Etype (Subt, Base_Type (Etype (Comp)));
4800 Set_Ekind (Subt, Ekind (Etype (Comp)));
4801 Set_Etype (N, Subt);
4802 end;
4803 end if;
4804
4805 -- If Full_Analysis not enabled, just set the Etype
4806
4807 else
4808 Set_Etype (N, Etype (Comp));
4809 end if;
4810
4811 Check_Implicit_Dereference (N, Etype (N));
4812 return;
4813 end if;
4814
4815 -- If the prefix is a private extension, check only the visible
4816 -- components of the partial view. This must include the tag,
4817 -- which can appear in expanded code in a tag check.
4818
4819 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4820 and then Chars (Selector_Name (N)) /= Name_uTag
4821 then
4822 exit when Comp = Last_Entity (Type_To_Use);
4823 end if;
4824
4825 Next_Entity (Comp);
4826 end loop;
4827
4828 -- Ada 2005 (AI-252): The selected component can be interpreted as
4829 -- a prefixed view of a subprogram. Depending on the context, this is
4830 -- either a name that can appear in a renaming declaration, or part
4831 -- of an enclosing call given in prefix form.
4832
4833 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4834 -- selected component should resolve to a name.
4835
4836 if Ada_Version >= Ada_2005
4837 and then Is_Tagged_Type (Prefix_Type)
4838 and then not Is_Concurrent_Type (Prefix_Type)
4839 then
4840 if Nkind (Parent (N)) = N_Generic_Association
4841 or else Nkind (Parent (N)) = N_Requeue_Statement
4842 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4843 then
4844 if Find_Primitive_Operation (N) then
4845 return;
4846 end if;
4847
4848 elsif Try_Object_Operation (N) then
4849 return;
4850 end if;
4851
4852 -- If the transformation fails, it will be necessary to redo the
4853 -- analysis with all errors enabled, to indicate candidate
4854 -- interpretations and reasons for each failure ???
4855
4856 end if;
4857
4858 elsif Is_Private_Type (Prefix_Type) then
4859
4860 -- Allow access only to discriminants of the type. If the type has
4861 -- no full view, gigi uses the parent type for the components, so we
4862 -- do the same here.
4863
4864 if No (Full_View (Prefix_Type)) then
4865 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4866 Comp := First_Entity (Type_To_Use);
4867 end if;
4868
4869 while Present (Comp) loop
4870 if Chars (Comp) = Chars (Sel) then
4871 if Ekind (Comp) = E_Discriminant then
4872 Set_Entity_With_Checks (Sel, Comp);
4873 Generate_Reference (Comp, Sel);
4874
4875 Set_Etype (Sel, Etype (Comp));
4876 Set_Etype (N, Etype (Comp));
4877 Check_Implicit_Dereference (N, Etype (N));
4878
4879 if Is_Generic_Type (Prefix_Type)
4880 or else Is_Generic_Type (Root_Type (Prefix_Type))
4881 then
4882 Set_Original_Discriminant (Sel, Comp);
4883 end if;
4884
4885 -- Before declaring an error, check whether this is tagged
4886 -- private type and a call to a primitive operation.
4887
4888 elsif Ada_Version >= Ada_2005
4889 and then Is_Tagged_Type (Prefix_Type)
4890 and then Try_Object_Operation (N)
4891 then
4892 return;
4893
4894 else
4895 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4896 Error_Msg_NE ("invisible selector& for }", N, Sel);
4897 Set_Entity (Sel, Any_Id);
4898 Set_Etype (N, Any_Type);
4899 end if;
4900
4901 return;
4902 end if;
4903
4904 Next_Entity (Comp);
4905 end loop;
4906
4907 elsif Is_Concurrent_Type (Prefix_Type) then
4908
4909 -- Find visible operation with given name. For a protected type,
4910 -- the possible candidates are discriminants, entries or protected
4911 -- subprograms. For a task type, the set can only include entries or
4912 -- discriminants if the task type is not an enclosing scope. If it
4913 -- is an enclosing scope (e.g. in an inner task) then all entities
4914 -- are visible, but the prefix must denote the enclosing scope, i.e.
4915 -- can only be a direct name or an expanded name.
4916
4917 Set_Etype (Sel, Any_Type);
4918 Hidden_Comp := Empty;
4919 In_Scope := In_Open_Scopes (Prefix_Type);
4920 Is_Private_Op := False;
4921
4922 while Present (Comp) loop
4923
4924 -- Do not examine private operations of the type if not within
4925 -- its scope.
4926
4927 if Chars (Comp) = Chars (Sel) then
4928 if Is_Overloadable (Comp)
4929 and then (In_Scope
4930 or else Comp /= First_Private_Entity (Type_To_Use))
4931 then
4932 Add_One_Interp (Sel, Comp, Etype (Comp));
4933 if Comp = First_Private_Entity (Type_To_Use) then
4934 Is_Private_Op := True;
4935 end if;
4936
4937 -- If the prefix is tagged, the correct interpretation may
4938 -- lie in the primitive or class-wide operations of the
4939 -- type. Perform a simple conformance check to determine
4940 -- whether Try_Object_Operation should be invoked even if
4941 -- a visible entity is found.
4942
4943 if Is_Tagged_Type (Prefix_Type)
4944 and then Nkind_In (Parent (N), N_Function_Call,
4945 N_Indexed_Component,
4946 N_Procedure_Call_Statement)
4947 and then Has_Mode_Conformant_Spec (Comp)
4948 then
4949 Has_Candidate := True;
4950 end if;
4951
4952 -- Note: a selected component may not denote a component of a
4953 -- protected type (4.1.3(7)).
4954
4955 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4956 or else (In_Scope
4957 and then not Is_Protected_Type (Prefix_Type)
4958 and then Is_Entity_Name (Name))
4959 then
4960 Set_Entity_With_Checks (Sel, Comp);
4961 Generate_Reference (Comp, Sel);
4962
4963 -- The selector is not overloadable, so we have a candidate
4964 -- interpretation.
4965
4966 Has_Candidate := True;
4967
4968 else
4969 if Ekind (Comp) = E_Component then
4970 Hidden_Comp := Comp;
4971 end if;
4972
4973 goto Next_Comp;
4974 end if;
4975
4976 Set_Etype (Sel, Etype (Comp));
4977 Set_Etype (N, Etype (Comp));
4978
4979 if Ekind (Comp) = E_Discriminant then
4980 Set_Original_Discriminant (Sel, Comp);
4981 end if;
4982
4983 -- For access type case, introduce explicit dereference for
4984 -- more uniform treatment of entry calls.
4985
4986 if Is_Access_Type (Etype (Name)) then
4987 Insert_Explicit_Dereference (Name);
4988 Error_Msg_NW
4989 (Warn_On_Dereference, "?d?implicit dereference", N);
4990 end if;
4991 end if;
4992
4993 <<Next_Comp>>
4994 if Comp = First_Private_Entity (Type_To_Use) then
4995 if Etype (Sel) /= Any_Type then
4996
4997 -- If the first private entity's name matches, then treat
4998 -- it as a private op: needed for the error check for
4999 -- illegal selection of private entities further below.
5000
5001 if Chars (Comp) = Chars (Sel) then
5002 Is_Private_Op := True;
5003 end if;
5004
5005 -- We have a candidate, so exit the loop
5006
5007 exit;
5008
5009 else
5010 -- Indicate that subsequent operations are private,
5011 -- for better error reporting.
5012
5013 Is_Private_Op := True;
5014 end if;
5015 end if;
5016
5017 -- Do not examine private operations if not within scope of
5018 -- the synchronized type.
5019
5020 exit when not In_Scope
5021 and then
5022 Comp = First_Private_Entity (Base_Type (Prefix_Type));
5023 Next_Entity (Comp);
5024 end loop;
5025
5026 -- If the scope is a current instance, the prefix cannot be an
5027 -- expression of the same type, unless the selector designates a
5028 -- public operation (otherwise that would represent an attempt to
5029 -- reach an internal entity of another synchronized object).
5030
5031 -- This is legal if prefix is an access to such type and there is
5032 -- a dereference, or is a component with a dereferenced prefix.
5033 -- It is also legal if the prefix is a component of a task type,
5034 -- and the selector is one of the task operations.
5035
5036 if In_Scope
5037 and then not Is_Entity_Name (Name)
5038 and then not Has_Dereference (Name)
5039 then
5040 if Is_Task_Type (Prefix_Type)
5041 and then Present (Entity (Sel))
5042 and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family)
5043 then
5044 null;
5045
5046 elsif Is_Protected_Type (Prefix_Type)
5047 and then Is_Overloadable (Entity (Sel))
5048 and then not Is_Private_Op
5049 then
5050 null;
5051
5052 else
5053 Error_Msg_NE
5054 ("invalid reference to internal operation of some object of "
5055 & "type &", N, Type_To_Use);
5056 Set_Entity (Sel, Any_Id);
5057 Set_Etype (Sel, Any_Type);
5058 return;
5059 end if;
5060
5061 -- Another special case: the prefix may denote an object of the type
5062 -- (but not a type) in which case this is an external call and the
5063 -- operation must be public.
5064
5065 elsif In_Scope
5066 and then Is_Object_Reference (Original_Node (Prefix (N)))
5067 and then Comes_From_Source (N)
5068 and then Is_Private_Op
5069 then
5070 if Present (Hidden_Comp) then
5071 Error_Msg_NE
5072 ("invalid reference to private component of object of type "
5073 & "&", N, Type_To_Use);
5074
5075 else
5076 Error_Msg_NE
5077 ("invalid reference to private operation of some object of "
5078 & "type &", N, Type_To_Use);
5079 end if;
5080
5081 Set_Entity (Sel, Any_Id);
5082 Set_Etype (Sel, Any_Type);
5083 return;
5084 end if;
5085
5086 -- If there is no visible entity with the given name or none of the
5087 -- visible entities are plausible interpretations, check whether
5088 -- there is some other primitive operation with that name.
5089
5090 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
5091 if (Etype (N) = Any_Type
5092 or else not Has_Candidate)
5093 and then Try_Object_Operation (N)
5094 then
5095 return;
5096
5097 -- If the context is not syntactically a procedure call, it
5098 -- may be a call to a primitive function declared outside of
5099 -- the synchronized type.
5100
5101 -- If the context is a procedure call, there might still be
5102 -- an overloading between an entry and a primitive procedure
5103 -- declared outside of the synchronized type, called in prefix
5104 -- notation. This is harder to disambiguate because in one case
5105 -- the controlling formal is implicit ???
5106
5107 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
5108 and then Nkind (Parent (N)) /= N_Indexed_Component
5109 and then Try_Object_Operation (N)
5110 then
5111 return;
5112 end if;
5113
5114 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5115 -- entry or procedure of a tagged concurrent type we must check
5116 -- if there are class-wide subprograms covering the primitive. If
5117 -- true then Try_Object_Operation reports the error.
5118
5119 if Has_Candidate
5120 and then Is_Concurrent_Type (Prefix_Type)
5121 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
5122 then
5123 -- Duplicate the call. This is required to avoid problems with
5124 -- the tree transformations performed by Try_Object_Operation.
5125 -- Set properly the parent of the copied call, because it is
5126 -- about to be reanalyzed.
5127
5128 declare
5129 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5130
5131 begin
5132 Set_Parent (Par, Parent (Parent (N)));
5133
5134 if Try_Object_Operation
5135 (Sinfo.Name (Par), CW_Test_Only => True)
5136 then
5137 return;
5138 end if;
5139 end;
5140 end if;
5141 end if;
5142
5143 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
5144
5145 -- Case of a prefix of a protected type: selector might denote
5146 -- an invisible private component.
5147
5148 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5149 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5150 Next_Entity (Comp);
5151 end loop;
5152
5153 if Present (Comp) then
5154 if Is_Single_Concurrent_Object then
5155 Error_Msg_Node_2 := Entity (Name);
5156 Error_Msg_NE ("invisible selector& for &", N, Sel);
5157
5158 else
5159 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5160 Error_Msg_NE ("invisible selector& for }", N, Sel);
5161 end if;
5162 return;
5163 end if;
5164 end if;
5165
5166 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5167
5168 else
5169 -- Invalid prefix
5170
5171 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5172 end if;
5173
5174 -- If N still has no type, the component is not defined in the prefix
5175
5176 if Etype (N) = Any_Type then
5177
5178 if Is_Single_Concurrent_Object then
5179 Error_Msg_Node_2 := Entity (Name);
5180 Error_Msg_NE ("no selector& for&", N, Sel);
5181
5182 Check_Misspelled_Selector (Type_To_Use, Sel);
5183
5184 -- If this is a derived formal type, the parent may have different
5185 -- visibility at this point. Try for an inherited component before
5186 -- reporting an error.
5187
5188 elsif Is_Generic_Type (Prefix_Type)
5189 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
5190 and then Prefix_Type /= Etype (Prefix_Type)
5191 and then Is_Record_Type (Etype (Prefix_Type))
5192 then
5193 Set_Etype (Prefix (N), Etype (Prefix_Type));
5194 Analyze_Selected_Component (N);
5195 return;
5196
5197 -- Similarly, if this is the actual for a formal derived type, or
5198 -- a derived type thereof, the component inherited from the generic
5199 -- parent may not be visible in the actual, but the selected
5200 -- component is legal. Climb up the derivation chain of the generic
5201 -- parent type until we find the proper ancestor type.
5202
5203 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5204 declare
5205 Par : Entity_Id := Prefix_Type;
5206 begin
5207 -- Climb up derivation chain to generic actual subtype
5208
5209 while not Is_Generic_Actual_Type (Par) loop
5210 if Ekind (Par) = E_Record_Type then
5211 Par := Parent_Subtype (Par);
5212 exit when No (Par);
5213 else
5214 exit when Par = Etype (Par);
5215 Par := Etype (Par);
5216 end if;
5217 end loop;
5218
5219 if Present (Par) and then Is_Generic_Actual_Type (Par) then
5220
5221 -- Now look for component in ancestor types
5222
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;
5230
5231 -- Another special case: the type is an extension of a private
5232 -- type T, is an actual in an instance, and we are in the body
5233 -- of the instance, so the generic body had a full view of the
5234 -- type declaration for T or of some ancestor that defines the
5235 -- component in question.
5236
5237 elsif Is_Derived_Type (Type_To_Use)
5238 and then Used_As_Generic_Actual (Type_To_Use)
5239 and then In_Instance_Body
5240 then
5241 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
5242
5243 -- In ASIS mode the generic parent type may be absent. Examine
5244 -- the parent type directly for a component that may have been
5245 -- visible in a parent generic unit.
5246
5247 elsif Is_Derived_Type (Prefix_Type) then
5248 Par := Etype (Prefix_Type);
5249 Find_Component_In_Instance (Par);
5250 end if;
5251 end;
5252
5253 -- The search above must have eventually succeeded, since the
5254 -- selected component was legal in the generic.
5255
5256 if No (Entity (Sel)) then
5257 raise Program_Error;
5258 end if;
5259
5260 return;
5261
5262 -- Component not found, specialize error message when appropriate
5263
5264 else
5265 if Ekind (Prefix_Type) = E_Record_Subtype then
5266
5267 -- Check whether this is a component of the base type which
5268 -- is absent from a statically constrained subtype. This will
5269 -- raise constraint error at run time, but is not a compile-
5270 -- time error. When the selector is illegal for base type as
5271 -- well fall through and generate a compilation error anyway.
5272
5273 Comp := First_Component (Base_Type (Prefix_Type));
5274 while Present (Comp) loop
5275 if Chars (Comp) = Chars (Sel)
5276 and then Is_Visible_Component (Comp, Sel)
5277 then
5278 Set_Entity_With_Checks (Sel, Comp);
5279 Generate_Reference (Comp, Sel);
5280 Set_Etype (Sel, Etype (Comp));
5281 Set_Etype (N, Etype (Comp));
5282
5283 -- Emit appropriate message. The node will be replaced
5284 -- by an appropriate raise statement.
5285
5286 -- Note that in SPARK mode, as with all calls to apply a
5287 -- compile time constraint error, this will be made into
5288 -- an error to simplify the processing of the formal
5289 -- verification backend.
5290
5291 Apply_Compile_Time_Constraint_Error
5292 (N, "component not present in }??",
5293 CE_Discriminant_Check_Failed,
5294 Ent => Prefix_Type, Rep => False);
5295
5296 Set_Raises_Constraint_Error (N);
5297 return;
5298 end if;
5299
5300 Next_Component (Comp);
5301 end loop;
5302
5303 end if;
5304
5305 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5306 Error_Msg_NE ("no selector& for}", N, Sel);
5307
5308 -- Add information in the case of an incomplete prefix
5309
5310 if Is_Incomplete_Type (Type_To_Use) then
5311 declare
5312 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5313
5314 begin
5315 if From_Limited_With (Scope (Type_To_Use)) then
5316 Error_Msg_NE
5317 ("\limited view of& has no components", N, Inc);
5318
5319 else
5320 Error_Msg_NE
5321 ("\premature usage of incomplete type&", N, Inc);
5322
5323 if Nkind (Parent (Inc)) =
5324 N_Incomplete_Type_Declaration
5325 then
5326 -- Record location of premature use in entity so that
5327 -- a continuation message is generated when the
5328 -- completion is seen.
5329
5330 Set_Premature_Use (Parent (Inc), N);
5331 end if;
5332 end if;
5333 end;
5334 end if;
5335
5336 Check_Misspelled_Selector (Type_To_Use, Sel);
5337 end if;
5338
5339 Set_Entity (Sel, Any_Id);
5340 Set_Etype (Sel, Any_Type);
5341 end if;
5342 end Analyze_Selected_Component;
5343
5344 ---------------------------
5345 -- Analyze_Short_Circuit --
5346 ---------------------------
5347
5348 procedure Analyze_Short_Circuit (N : Node_Id) is
5349 L : constant Node_Id := Left_Opnd (N);
5350 R : constant Node_Id := Right_Opnd (N);
5351 Ind : Interp_Index;
5352 It : Interp;
5353
5354 begin
5355 Analyze_Expression (L);
5356 Analyze_Expression (R);
5357 Set_Etype (N, Any_Type);
5358
5359 if not Is_Overloaded (L) then
5360 if Root_Type (Etype (L)) = Standard_Boolean
5361 and then Has_Compatible_Type (R, Etype (L))
5362 then
5363 Add_One_Interp (N, Etype (L), Etype (L));
5364 end if;
5365
5366 else
5367 Get_First_Interp (L, Ind, It);
5368 while Present (It.Typ) loop
5369 if Root_Type (It.Typ) = Standard_Boolean
5370 and then Has_Compatible_Type (R, It.Typ)
5371 then
5372 Add_One_Interp (N, It.Typ, It.Typ);
5373 end if;
5374
5375 Get_Next_Interp (Ind, It);
5376 end loop;
5377 end if;
5378
5379 -- Here we have failed to find an interpretation. Clearly we know that
5380 -- it is not the case that both operands can have an interpretation of
5381 -- Boolean, but this is by far the most likely intended interpretation.
5382 -- So we simply resolve both operands as Booleans, and at least one of
5383 -- these resolutions will generate an error message, and we do not need
5384 -- to give another error message on the short circuit operation itself.
5385
5386 if Etype (N) = Any_Type then
5387 Resolve (L, Standard_Boolean);
5388 Resolve (R, Standard_Boolean);
5389 Set_Etype (N, Standard_Boolean);
5390 end if;
5391 end Analyze_Short_Circuit;
5392
5393 -------------------
5394 -- Analyze_Slice --
5395 -------------------
5396
5397 procedure Analyze_Slice (N : Node_Id) is
5398 D : constant Node_Id := Discrete_Range (N);
5399 P : constant Node_Id := Prefix (N);
5400 Array_Type : Entity_Id;
5401 Index_Type : Entity_Id;
5402
5403 procedure Analyze_Overloaded_Slice;
5404 -- If the prefix is overloaded, select those interpretations that
5405 -- yield a one-dimensional array type.
5406
5407 ------------------------------
5408 -- Analyze_Overloaded_Slice --
5409 ------------------------------
5410
5411 procedure Analyze_Overloaded_Slice is
5412 I : Interp_Index;
5413 It : Interp;
5414 Typ : Entity_Id;
5415
5416 begin
5417 Set_Etype (N, Any_Type);
5418
5419 Get_First_Interp (P, I, It);
5420 while Present (It.Nam) loop
5421 Typ := It.Typ;
5422
5423 if Is_Access_Type (Typ) then
5424 Typ := Designated_Type (Typ);
5425 Error_Msg_NW
5426 (Warn_On_Dereference, "?d?implicit dereference", N);
5427 end if;
5428
5429 if Is_Array_Type (Typ)
5430 and then Number_Dimensions (Typ) = 1
5431 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
5432 then
5433 Add_One_Interp (N, Typ, Typ);
5434 end if;
5435
5436 Get_Next_Interp (I, It);
5437 end loop;
5438
5439 if Etype (N) = Any_Type then
5440 Error_Msg_N ("expect array type in prefix of slice", N);
5441 end if;
5442 end Analyze_Overloaded_Slice;
5443
5444 -- Start of processing for Analyze_Slice
5445
5446 begin
5447 if Comes_From_Source (N) then
5448 Check_SPARK_05_Restriction ("slice is not allowed", N);
5449 end if;
5450
5451 Analyze (P);
5452 Analyze (D);
5453
5454 if Is_Overloaded (P) then
5455 Analyze_Overloaded_Slice;
5456
5457 else
5458 Array_Type := Etype (P);
5459 Set_Etype (N, Any_Type);
5460
5461 if Is_Access_Type (Array_Type) then
5462 Array_Type := Designated_Type (Array_Type);
5463 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
5464 end if;
5465
5466 if not Is_Array_Type (Array_Type) then
5467 Wrong_Type (P, Any_Array);
5468
5469 elsif Number_Dimensions (Array_Type) > 1 then
5470 Error_Msg_N
5471 ("type is not one-dimensional array in slice prefix", N);
5472
5473 else
5474 if Ekind (Array_Type) = E_String_Literal_Subtype then
5475 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
5476 else
5477 Index_Type := Etype (First_Index (Array_Type));
5478 end if;
5479
5480 if not Has_Compatible_Type (D, Index_Type) then
5481 Wrong_Type (D, Index_Type);
5482 else
5483 Set_Etype (N, Array_Type);
5484 end if;
5485 end if;
5486 end if;
5487 end Analyze_Slice;
5488
5489 -----------------------------
5490 -- Analyze_Type_Conversion --
5491 -----------------------------
5492
5493 procedure Analyze_Type_Conversion (N : Node_Id) is
5494 Expr : constant Node_Id := Expression (N);
5495 Typ : Entity_Id;
5496
5497 begin
5498 -- If Conversion_OK is set, then the Etype is already set, and the only
5499 -- processing required is to analyze the expression. This is used to
5500 -- construct certain "illegal" conversions which are not allowed by Ada
5501 -- semantics, but can be handled by Gigi, see Sinfo for further details.
5502
5503 if Conversion_OK (N) then
5504 Analyze (Expr);
5505 return;
5506 end if;
5507
5508 -- Otherwise full type analysis is required, as well as some semantic
5509 -- checks to make sure the argument of the conversion is appropriate.
5510
5511 Find_Type (Subtype_Mark (N));
5512 Typ := Entity (Subtype_Mark (N));
5513 Set_Etype (N, Typ);
5514 Check_Fully_Declared (Typ, N);
5515 Analyze_Expression (Expr);
5516 Validate_Remote_Type_Type_Conversion (N);
5517
5518 -- Only remaining step is validity checks on the argument. These
5519 -- are skipped if the conversion does not come from the source.
5520
5521 if not Comes_From_Source (N) then
5522 return;
5523
5524 -- If there was an error in a generic unit, no need to replicate the
5525 -- error message. Conversely, constant-folding in the generic may
5526 -- transform the argument of a conversion into a string literal, which
5527 -- is legal. Therefore the following tests are not performed in an
5528 -- instance. The same applies to an inlined body.
5529
5530 elsif In_Instance or In_Inlined_Body then
5531 return;
5532
5533 elsif Nkind (Expr) = N_Null then
5534 Error_Msg_N ("argument of conversion cannot be null", N);
5535 Error_Msg_N ("\use qualified expression instead", N);
5536 Set_Etype (N, Any_Type);
5537
5538 elsif Nkind (Expr) = N_Aggregate then
5539 Error_Msg_N ("argument of conversion cannot be aggregate", N);
5540 Error_Msg_N ("\use qualified expression instead", N);
5541
5542 elsif Nkind (Expr) = N_Allocator then
5543 Error_Msg_N ("argument of conversion cannot be an allocator", N);
5544 Error_Msg_N ("\use qualified expression instead", N);
5545
5546 elsif Nkind (Expr) = N_String_Literal then
5547 Error_Msg_N ("argument of conversion cannot be string literal", N);
5548 Error_Msg_N ("\use qualified expression instead", N);
5549
5550 elsif Nkind (Expr) = N_Character_Literal then
5551 if Ada_Version = Ada_83 then
5552 Resolve (Expr, Typ);
5553 else
5554 Error_Msg_N ("argument of conversion cannot be character literal",
5555 N);
5556 Error_Msg_N ("\use qualified expression instead", N);
5557 end if;
5558
5559 elsif Nkind (Expr) = N_Attribute_Reference
5560 and then Nam_In (Attribute_Name (Expr), Name_Access,
5561 Name_Unchecked_Access,
5562 Name_Unrestricted_Access)
5563 then
5564 Error_Msg_N ("argument of conversion cannot be access", N);
5565 Error_Msg_N ("\use qualified expression instead", N);
5566 end if;
5567
5568 -- A formal parameter of a specific tagged type whose related subprogram
5569 -- is subject to pragma Extensions_Visible with value "False" cannot
5570 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
5571 -- internally generated expressions.
5572
5573 if Is_Class_Wide_Type (Typ)
5574 and then Comes_From_Source (Expr)
5575 and then Is_EVF_Expression (Expr)
5576 then
5577 Error_Msg_N
5578 ("formal parameter cannot be converted to class-wide type when "
5579 & "Extensions_Visible is False", Expr);
5580 end if;
5581 end Analyze_Type_Conversion;
5582
5583 ----------------------
5584 -- Analyze_Unary_Op --
5585 ----------------------
5586
5587 procedure Analyze_Unary_Op (N : Node_Id) is
5588 R : constant Node_Id := Right_Opnd (N);
5589 Op_Id : Entity_Id := Entity (N);
5590
5591 begin
5592 Set_Etype (N, Any_Type);
5593 Candidate_Type := Empty;
5594
5595 Analyze_Expression (R);
5596
5597 if Present (Op_Id) then
5598 if Ekind (Op_Id) = E_Operator then
5599 Find_Unary_Types (R, Op_Id, N);
5600 else
5601 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5602 end if;
5603
5604 else
5605 Op_Id := Get_Name_Entity_Id (Chars (N));
5606 while Present (Op_Id) loop
5607 if Ekind (Op_Id) = E_Operator then
5608 if No (Next_Entity (First_Entity (Op_Id))) then
5609 Find_Unary_Types (R, Op_Id, N);
5610 end if;
5611
5612 elsif Is_Overloadable (Op_Id) then
5613 Analyze_User_Defined_Unary_Op (N, Op_Id);
5614 end if;
5615
5616 Op_Id := Homonym (Op_Id);
5617 end loop;
5618 end if;
5619
5620 Operator_Check (N);
5621 end Analyze_Unary_Op;
5622
5623 ----------------------------------
5624 -- Analyze_Unchecked_Expression --
5625 ----------------------------------
5626
5627 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5628 begin
5629 Analyze (Expression (N), Suppress => All_Checks);
5630 Set_Etype (N, Etype (Expression (N)));
5631 Save_Interps (Expression (N), N);
5632 end Analyze_Unchecked_Expression;
5633
5634 ---------------------------------------
5635 -- Analyze_Unchecked_Type_Conversion --
5636 ---------------------------------------
5637
5638 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5639 begin
5640 Find_Type (Subtype_Mark (N));
5641 Analyze_Expression (Expression (N));
5642 Set_Etype (N, Entity (Subtype_Mark (N)));
5643 end Analyze_Unchecked_Type_Conversion;
5644
5645 ------------------------------------
5646 -- Analyze_User_Defined_Binary_Op --
5647 ------------------------------------
5648
5649 procedure Analyze_User_Defined_Binary_Op
5650 (N : Node_Id;
5651 Op_Id : Entity_Id)
5652 is
5653 begin
5654 -- Only do analysis if the operator Comes_From_Source, since otherwise
5655 -- the operator was generated by the expander, and all such operators
5656 -- always refer to the operators in package Standard.
5657
5658 if Comes_From_Source (N) then
5659 declare
5660 F1 : constant Entity_Id := First_Formal (Op_Id);
5661 F2 : constant Entity_Id := Next_Formal (F1);
5662
5663 begin
5664 -- Verify that Op_Id is a visible binary function. Note that since
5665 -- we know Op_Id is overloaded, potentially use visible means use
5666 -- visible for sure (RM 9.4(11)).
5667
5668 if Ekind (Op_Id) = E_Function
5669 and then Present (F2)
5670 and then (Is_Immediately_Visible (Op_Id)
5671 or else Is_Potentially_Use_Visible (Op_Id))
5672 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5673 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5674 then
5675 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5676
5677 -- If the left operand is overloaded, indicate that the current
5678 -- type is a viable candidate. This is redundant in most cases,
5679 -- but for equality and comparison operators where the context
5680 -- does not impose a type on the operands, setting the proper
5681 -- type is necessary to avoid subsequent ambiguities during
5682 -- resolution, when both user-defined and predefined operators
5683 -- may be candidates.
5684
5685 if Is_Overloaded (Left_Opnd (N)) then
5686 Set_Etype (Left_Opnd (N), Etype (F1));
5687 end if;
5688
5689 if Debug_Flag_E then
5690 Write_Str ("user defined operator ");
5691 Write_Name (Chars (Op_Id));
5692 Write_Str (" on node ");
5693 Write_Int (Int (N));
5694 Write_Eol;
5695 end if;
5696 end if;
5697 end;
5698 end if;
5699 end Analyze_User_Defined_Binary_Op;
5700
5701 -----------------------------------
5702 -- Analyze_User_Defined_Unary_Op --
5703 -----------------------------------
5704
5705 procedure Analyze_User_Defined_Unary_Op
5706 (N : Node_Id;
5707 Op_Id : Entity_Id)
5708 is
5709 begin
5710 -- Only do analysis if the operator Comes_From_Source, since otherwise
5711 -- the operator was generated by the expander, and all such operators
5712 -- always refer to the operators in package Standard.
5713
5714 if Comes_From_Source (N) then
5715 declare
5716 F : constant Entity_Id := First_Formal (Op_Id);
5717
5718 begin
5719 -- Verify that Op_Id is a visible unary function. Note that since
5720 -- we know Op_Id is overloaded, potentially use visible means use
5721 -- visible for sure (RM 9.4(11)).
5722
5723 if Ekind (Op_Id) = E_Function
5724 and then No (Next_Formal (F))
5725 and then (Is_Immediately_Visible (Op_Id)
5726 or else Is_Potentially_Use_Visible (Op_Id))
5727 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5728 then
5729 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5730 end if;
5731 end;
5732 end if;
5733 end Analyze_User_Defined_Unary_Op;
5734
5735 ---------------------------
5736 -- Check_Arithmetic_Pair --
5737 ---------------------------
5738
5739 procedure Check_Arithmetic_Pair
5740 (T1, T2 : Entity_Id;
5741 Op_Id : Entity_Id;
5742 N : Node_Id)
5743 is
5744 Op_Name : constant Name_Id := Chars (Op_Id);
5745
5746 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5747 -- Check whether the fixed-point type Typ has a user-defined operator
5748 -- (multiplication or division) that should hide the corresponding
5749 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5750 -- such operators more visible and therefore useful.
5751 --
5752 -- If the name of the operation is an expanded name with prefix
5753 -- Standard, the predefined universal fixed operator is available,
5754 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5755
5756 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5757 -- Get specific type (i.e. non-universal type if there is one)
5758
5759 ------------------
5760 -- Has_Fixed_Op --
5761 ------------------
5762
5763 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5764 Bas : constant Entity_Id := Base_Type (Typ);
5765 Ent : Entity_Id;
5766 F1 : Entity_Id;
5767 F2 : Entity_Id;
5768
5769 begin
5770 -- If the universal_fixed operation is given explicitly the rule
5771 -- concerning primitive operations of the type do not apply.
5772
5773 if Nkind (N) = N_Function_Call
5774 and then Nkind (Name (N)) = N_Expanded_Name
5775 and then Entity (Prefix (Name (N))) = Standard_Standard
5776 then
5777 return False;
5778 end if;
5779
5780 -- The operation is treated as primitive if it is declared in the
5781 -- same scope as the type, and therefore on the same entity chain.
5782
5783 Ent := Next_Entity (Typ);
5784 while Present (Ent) loop
5785 if Chars (Ent) = Chars (Op) then
5786 F1 := First_Formal (Ent);
5787 F2 := Next_Formal (F1);
5788
5789 -- The operation counts as primitive if either operand or
5790 -- result are of the given base type, and both operands are
5791 -- fixed point types.
5792
5793 if (Base_Type (Etype (F1)) = Bas
5794 and then Is_Fixed_Point_Type (Etype (F2)))
5795
5796 or else
5797 (Base_Type (Etype (F2)) = Bas
5798 and then Is_Fixed_Point_Type (Etype (F1)))
5799
5800 or else
5801 (Base_Type (Etype (Ent)) = Bas
5802 and then Is_Fixed_Point_Type (Etype (F1))
5803 and then Is_Fixed_Point_Type (Etype (F2)))
5804 then
5805 return True;
5806 end if;
5807 end if;
5808
5809 Next_Entity (Ent);
5810 end loop;
5811
5812 return False;
5813 end Has_Fixed_Op;
5814
5815 -------------------
5816 -- Specific_Type --
5817 -------------------
5818
5819 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5820 begin
5821 if T1 = Universal_Integer or else T1 = Universal_Real then
5822 return Base_Type (T2);
5823 else
5824 return Base_Type (T1);
5825 end if;
5826 end Specific_Type;
5827
5828 -- Start of processing for Check_Arithmetic_Pair
5829
5830 begin
5831 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5832 if Is_Numeric_Type (T1)
5833 and then Is_Numeric_Type (T2)
5834 and then (Covers (T1 => T1, T2 => T2)
5835 or else
5836 Covers (T1 => T2, T2 => T1))
5837 then
5838 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5839 end if;
5840
5841 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5842 if Is_Fixed_Point_Type (T1)
5843 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5844 then
5845 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5846 -- and no further processing is required (this is the case of an
5847 -- operator constructed by Exp_Fixd for a fixed point operation)
5848 -- Otherwise add one interpretation with universal fixed result
5849 -- If the operator is given in functional notation, it comes
5850 -- from source and Fixed_As_Integer cannot apply.
5851
5852 if (Nkind (N) not in N_Op
5853 or else not Treat_Fixed_As_Integer (N))
5854 and then
5855 (not Has_Fixed_Op (T1, Op_Id)
5856 or else Nkind (Parent (N)) = N_Type_Conversion)
5857 then
5858 Add_One_Interp (N, Op_Id, Universal_Fixed);
5859 end if;
5860
5861 elsif Is_Fixed_Point_Type (T2)
5862 and then (Nkind (N) not in N_Op
5863 or else not Treat_Fixed_As_Integer (N))
5864 and then T1 = Universal_Real
5865 and then
5866 (not Has_Fixed_Op (T1, Op_Id)
5867 or else Nkind (Parent (N)) = N_Type_Conversion)
5868 then
5869 Add_One_Interp (N, Op_Id, Universal_Fixed);
5870
5871 elsif Is_Numeric_Type (T1)
5872 and then Is_Numeric_Type (T2)
5873 and then (Covers (T1 => T1, T2 => T2)
5874 or else
5875 Covers (T1 => T2, T2 => T1))
5876 then
5877 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5878
5879 elsif Is_Fixed_Point_Type (T1)
5880 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5881 or else T2 = Universal_Integer)
5882 then
5883 Add_One_Interp (N, Op_Id, T1);
5884
5885 elsif T2 = Universal_Real
5886 and then Base_Type (T1) = Base_Type (Standard_Integer)
5887 and then Op_Name = Name_Op_Multiply
5888 then
5889 Add_One_Interp (N, Op_Id, Any_Fixed);
5890
5891 elsif T1 = Universal_Real
5892 and then Base_Type (T2) = Base_Type (Standard_Integer)
5893 then
5894 Add_One_Interp (N, Op_Id, Any_Fixed);
5895
5896 elsif Is_Fixed_Point_Type (T2)
5897 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5898 or else T1 = Universal_Integer)
5899 and then Op_Name = Name_Op_Multiply
5900 then
5901 Add_One_Interp (N, Op_Id, T2);
5902
5903 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5904 Add_One_Interp (N, Op_Id, T1);
5905
5906 elsif T2 = Universal_Real
5907 and then T1 = Universal_Integer
5908 and then Op_Name = Name_Op_Multiply
5909 then
5910 Add_One_Interp (N, Op_Id, T2);
5911 end if;
5912
5913 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5914
5915 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5916 -- set does not require any special processing, since the Etype is
5917 -- already set (case of operation constructed by Exp_Fixed).
5918
5919 if Is_Integer_Type (T1)
5920 and then (Covers (T1 => T1, T2 => T2)
5921 or else
5922 Covers (T1 => T2, T2 => T1))
5923 then
5924 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5925 end if;
5926
5927 elsif Op_Name = Name_Op_Expon then
5928 if Is_Numeric_Type (T1)
5929 and then not Is_Fixed_Point_Type (T1)
5930 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5931 or else T2 = Universal_Integer)
5932 then
5933 Add_One_Interp (N, Op_Id, Base_Type (T1));
5934 end if;
5935
5936 else pragma Assert (Nkind (N) in N_Op_Shift);
5937
5938 -- If not one of the predefined operators, the node may be one
5939 -- of the intrinsic functions. Its kind is always specific, and
5940 -- we can use it directly, rather than the name of the operation.
5941
5942 if Is_Integer_Type (T1)
5943 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5944 or else T2 = Universal_Integer)
5945 then
5946 Add_One_Interp (N, Op_Id, Base_Type (T1));
5947 end if;
5948 end if;
5949 end Check_Arithmetic_Pair;
5950
5951 -------------------------------
5952 -- Check_Misspelled_Selector --
5953 -------------------------------
5954
5955 procedure Check_Misspelled_Selector
5956 (Prefix : Entity_Id;
5957 Sel : Node_Id)
5958 is
5959 Max_Suggestions : constant := 2;
5960 Nr_Of_Suggestions : Natural := 0;
5961
5962 Suggestion_1 : Entity_Id := Empty;
5963 Suggestion_2 : Entity_Id := Empty;
5964
5965 Comp : Entity_Id;
5966
5967 begin
5968 -- All the components of the prefix of selector Sel are matched against
5969 -- Sel and a count is maintained of possible misspellings. When at
5970 -- the end of the analysis there are one or two (not more) possible
5971 -- misspellings, these misspellings will be suggested as possible
5972 -- correction.
5973
5974 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5975
5976 -- Concurrent types should be handled as well ???
5977
5978 return;
5979 end if;
5980
5981 Comp := First_Entity (Prefix);
5982 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5983 if Is_Visible_Component (Comp, Sel) then
5984 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5985 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5986
5987 case Nr_Of_Suggestions is
5988 when 1 => Suggestion_1 := Comp;
5989 when 2 => Suggestion_2 := Comp;
5990 when others => null;
5991 end case;
5992 end if;
5993 end if;
5994
5995 Comp := Next_Entity (Comp);
5996 end loop;
5997
5998 -- Report at most two suggestions
5999
6000 if Nr_Of_Suggestions = 1 then
6001 Error_Msg_NE -- CODEFIX
6002 ("\possible misspelling of&", Sel, Suggestion_1);
6003
6004 elsif Nr_Of_Suggestions = 2 then
6005 Error_Msg_Node_2 := Suggestion_2;
6006 Error_Msg_NE -- CODEFIX
6007 ("\possible misspelling of& or&", Sel, Suggestion_1);
6008 end if;
6009 end Check_Misspelled_Selector;
6010
6011 ----------------------
6012 -- Defined_In_Scope --
6013 ----------------------
6014
6015 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
6016 is
6017 S1 : constant Entity_Id := Scope (Base_Type (T));
6018 begin
6019 return S1 = S
6020 or else (S1 = System_Aux_Id and then S = Scope (S1));
6021 end Defined_In_Scope;
6022
6023 -------------------
6024 -- Diagnose_Call --
6025 -------------------
6026
6027 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
6028 Actual : Node_Id;
6029 X : Interp_Index;
6030 It : Interp;
6031 Err_Mode : Boolean;
6032 New_Nam : Node_Id;
6033 Void_Interp_Seen : Boolean := False;
6034
6035 Success : Boolean;
6036 pragma Warnings (Off, Boolean);
6037
6038 begin
6039 if Ada_Version >= Ada_2005 then
6040 Actual := First_Actual (N);
6041 while Present (Actual) loop
6042
6043 -- Ada 2005 (AI-50217): Post an error in case of premature
6044 -- usage of an entity from the limited view.
6045
6046 if not Analyzed (Etype (Actual))
6047 and then From_Limited_With (Etype (Actual))
6048 then
6049 Error_Msg_Qual_Level := 1;
6050 Error_Msg_NE
6051 ("missing with_clause for scope of imported type&",
6052 Actual, Etype (Actual));
6053 Error_Msg_Qual_Level := 0;
6054 end if;
6055
6056 Next_Actual (Actual);
6057 end loop;
6058 end if;
6059
6060 -- Before listing the possible candidates, check whether this is
6061 -- a prefix of a selected component that has been rewritten as a
6062 -- parameterless function call because there is a callable candidate
6063 -- interpretation. If there is a hidden package in the list of homonyms
6064 -- of the function name (bad programming style in any case) suggest that
6065 -- this is the intended entity.
6066
6067 if No (Parameter_Associations (N))
6068 and then Nkind (Parent (N)) = N_Selected_Component
6069 and then Nkind (Parent (Parent (N))) in N_Declaration
6070 and then Is_Overloaded (Nam)
6071 then
6072 declare
6073 Ent : Entity_Id;
6074
6075 begin
6076 Ent := Current_Entity (Nam);
6077 while Present (Ent) loop
6078 if Ekind (Ent) = E_Package then
6079 Error_Msg_N
6080 ("no legal interpretations as function call,!", Nam);
6081 Error_Msg_NE ("\package& is not visible", N, Ent);
6082
6083 Rewrite (Parent (N),
6084 New_Occurrence_Of (Any_Type, Sloc (N)));
6085 return;
6086 end if;
6087
6088 Ent := Homonym (Ent);
6089 end loop;
6090 end;
6091 end if;
6092
6093 -- Analyze each candidate call again, with full error reporting for
6094 -- each.
6095
6096 Error_Msg_N
6097 ("no candidate interpretations match the actuals:!", Nam);
6098 Err_Mode := All_Errors_Mode;
6099 All_Errors_Mode := True;
6100
6101 -- If this is a call to an operation of a concurrent type,
6102 -- the failed interpretations have been removed from the
6103 -- name. Recover them to provide full diagnostics.
6104
6105 if Nkind (Parent (Nam)) = N_Selected_Component then
6106 Set_Entity (Nam, Empty);
6107 New_Nam := New_Copy_Tree (Parent (Nam));
6108 Set_Is_Overloaded (New_Nam, False);
6109 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6110 Set_Parent (New_Nam, Parent (Parent (Nam)));
6111 Analyze_Selected_Component (New_Nam);
6112 Get_First_Interp (Selector_Name (New_Nam), X, It);
6113 else
6114 Get_First_Interp (Nam, X, It);
6115 end if;
6116
6117 while Present (It.Nam) loop
6118 if Etype (It.Nam) = Standard_Void_Type then
6119 Void_Interp_Seen := True;
6120 end if;
6121
6122 Analyze_One_Call (N, It.Nam, True, Success);
6123 Get_Next_Interp (X, It);
6124 end loop;
6125
6126 if Nkind (N) = N_Function_Call then
6127 Get_First_Interp (Nam, X, It);
6128
6129 if No (It.Typ)
6130 and then Ekind (Entity (Name (N))) = E_Function
6131 and then Present (Homonym (Entity (Name (N))))
6132 then
6133 -- A name may appear overloaded if it has a homonym, even if that
6134 -- homonym is non-overloadable, in which case the overload list is
6135 -- in fact empty. This specialized case deserves a special message
6136 -- if the homonym is a child package.
6137
6138 declare
6139 Nam : constant Node_Id := Name (N);
6140 H : constant Entity_Id := Homonym (Entity (Nam));
6141
6142 begin
6143 if Ekind (H) = E_Package and then Is_Child_Unit (H) then
6144 Error_Msg_Qual_Level := 2;
6145 Error_Msg_NE ("if an entity in package& is meant, ", Nam, H);
6146 Error_Msg_NE ("\use a fully qualified name", Nam, H);
6147 Error_Msg_Qual_Level := 0;
6148 end if;
6149 end;
6150
6151 else
6152 while Present (It.Nam) loop
6153 if Ekind_In (It.Nam, E_Function, E_Operator) then
6154 return;
6155 else
6156 Get_Next_Interp (X, It);
6157 end if;
6158 end loop;
6159
6160 -- If all interpretations are procedures, this deserves a more
6161 -- precise message. Ditto if this appears as the prefix of a
6162 -- selected component, which may be a lexical error.
6163
6164 Error_Msg_N
6165 ("\context requires function call, found procedure name", Nam);
6166
6167 if Nkind (Parent (N)) = N_Selected_Component
6168 and then N = Prefix (Parent (N))
6169 then
6170 Error_Msg_N -- CODEFIX
6171 ("\period should probably be semicolon", Parent (N));
6172 end if;
6173 end if;
6174
6175 elsif Nkind (N) = N_Procedure_Call_Statement
6176 and then not Void_Interp_Seen
6177 then
6178 Error_Msg_N ("\function name found in procedure call", Nam);
6179 end if;
6180
6181 All_Errors_Mode := Err_Mode;
6182 end Diagnose_Call;
6183
6184 ---------------------------
6185 -- Find_Arithmetic_Types --
6186 ---------------------------
6187
6188 procedure Find_Arithmetic_Types
6189 (L, R : Node_Id;
6190 Op_Id : Entity_Id;
6191 N : Node_Id)
6192 is
6193 Index1 : Interp_Index;
6194 Index2 : Interp_Index;
6195 It1 : Interp;
6196 It2 : Interp;
6197
6198 procedure Check_Right_Argument (T : Entity_Id);
6199 -- Check right operand of operator
6200
6201 --------------------------
6202 -- Check_Right_Argument --
6203 --------------------------
6204
6205 procedure Check_Right_Argument (T : Entity_Id) is
6206 begin
6207 if not Is_Overloaded (R) then
6208 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6209 else
6210 Get_First_Interp (R, Index2, It2);
6211 while Present (It2.Typ) loop
6212 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
6213 Get_Next_Interp (Index2, It2);
6214 end loop;
6215 end if;
6216 end Check_Right_Argument;
6217
6218 -- Start of processing for Find_Arithmetic_Types
6219
6220 begin
6221 if not Is_Overloaded (L) then
6222 Check_Right_Argument (Etype (L));
6223
6224 else
6225 Get_First_Interp (L, Index1, It1);
6226 while Present (It1.Typ) loop
6227 Check_Right_Argument (It1.Typ);
6228 Get_Next_Interp (Index1, It1);
6229 end loop;
6230 end if;
6231
6232 end Find_Arithmetic_Types;
6233
6234 ------------------------
6235 -- Find_Boolean_Types --
6236 ------------------------
6237
6238 procedure Find_Boolean_Types
6239 (L, R : Node_Id;
6240 Op_Id : Entity_Id;
6241 N : Node_Id)
6242 is
6243 Index : Interp_Index;
6244 It : Interp;
6245
6246 procedure Check_Numeric_Argument (T : Entity_Id);
6247 -- Special case for logical operations one of whose operands is an
6248 -- integer literal. If both are literal the result is any modular type.
6249
6250 ----------------------------
6251 -- Check_Numeric_Argument --
6252 ----------------------------
6253
6254 procedure Check_Numeric_Argument (T : Entity_Id) is
6255 begin
6256 if T = Universal_Integer then
6257 Add_One_Interp (N, Op_Id, Any_Modular);
6258
6259 elsif Is_Modular_Integer_Type (T) then
6260 Add_One_Interp (N, Op_Id, T);
6261 end if;
6262 end Check_Numeric_Argument;
6263
6264 -- Start of processing for Find_Boolean_Types
6265
6266 begin
6267 if not Is_Overloaded (L) then
6268 if Etype (L) = Universal_Integer
6269 or else Etype (L) = Any_Modular
6270 then
6271 if not Is_Overloaded (R) then
6272 Check_Numeric_Argument (Etype (R));
6273
6274 else
6275 Get_First_Interp (R, Index, It);
6276 while Present (It.Typ) loop
6277 Check_Numeric_Argument (It.Typ);
6278 Get_Next_Interp (Index, It);
6279 end loop;
6280 end if;
6281
6282 -- If operands are aggregates, we must assume that they may be
6283 -- boolean arrays, and leave disambiguation for the second pass.
6284 -- If only one is an aggregate, verify that the other one has an
6285 -- interpretation as a boolean array
6286
6287 elsif Nkind (L) = N_Aggregate then
6288 if Nkind (R) = N_Aggregate then
6289 Add_One_Interp (N, Op_Id, Etype (L));
6290
6291 elsif not Is_Overloaded (R) then
6292 if Valid_Boolean_Arg (Etype (R)) then
6293 Add_One_Interp (N, Op_Id, Etype (R));
6294 end if;
6295
6296 else
6297 Get_First_Interp (R, Index, It);
6298 while Present (It.Typ) loop
6299 if Valid_Boolean_Arg (It.Typ) then
6300 Add_One_Interp (N, Op_Id, It.Typ);
6301 end if;
6302
6303 Get_Next_Interp (Index, It);
6304 end loop;
6305 end if;
6306
6307 elsif Valid_Boolean_Arg (Etype (L))
6308 and then Has_Compatible_Type (R, Etype (L))
6309 then
6310 Add_One_Interp (N, Op_Id, Etype (L));
6311 end if;
6312
6313 else
6314 Get_First_Interp (L, Index, It);
6315 while Present (It.Typ) loop
6316 if Valid_Boolean_Arg (It.Typ)
6317 and then Has_Compatible_Type (R, It.Typ)
6318 then
6319 Add_One_Interp (N, Op_Id, It.Typ);
6320 end if;
6321
6322 Get_Next_Interp (Index, It);
6323 end loop;
6324 end if;
6325 end Find_Boolean_Types;
6326
6327 ---------------------------
6328 -- Find_Comparison_Types --
6329 ---------------------------
6330
6331 procedure Find_Comparison_Types
6332 (L, R : Node_Id;
6333 Op_Id : Entity_Id;
6334 N : Node_Id)
6335 is
6336 Index : Interp_Index;
6337 It : Interp;
6338 Found : Boolean := False;
6339 I_F : Interp_Index;
6340 T_F : Entity_Id;
6341 Scop : Entity_Id := Empty;
6342
6343 procedure Try_One_Interp (T1 : Entity_Id);
6344 -- Routine to try one proposed interpretation. Note that the context
6345 -- of the operator plays no role in resolving the arguments, so that
6346 -- if there is more than one interpretation of the operands that is
6347 -- compatible with comparison, the operation is ambiguous.
6348
6349 --------------------
6350 -- Try_One_Interp --
6351 --------------------
6352
6353 procedure Try_One_Interp (T1 : Entity_Id) is
6354 begin
6355 -- If the operator is an expanded name, then the type of the operand
6356 -- must be defined in the corresponding scope. If the type is
6357 -- universal, the context will impose the correct type. Note that we
6358 -- also avoid returning if we are currently within a generic instance
6359 -- due to the fact that the generic package declaration has already
6360 -- been successfully analyzed and Defined_In_Scope expects the base
6361 -- type to be defined within the instance which will never be the
6362 -- case.
6363
6364 if Present (Scop)
6365 and then not Defined_In_Scope (T1, Scop)
6366 and then not In_Instance
6367 and then T1 /= Universal_Integer
6368 and then T1 /= Universal_Real
6369 and then T1 /= Any_String
6370 and then T1 /= Any_Composite
6371 then
6372 return;
6373 end if;
6374
6375 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
6376 if Found and then Base_Type (T1) /= Base_Type (T_F) then
6377 It := Disambiguate (L, I_F, Index, Any_Type);
6378
6379 if It = No_Interp then
6380 Ambiguous_Operands (N);
6381 Set_Etype (L, Any_Type);
6382 return;
6383
6384 else
6385 T_F := It.Typ;
6386 end if;
6387 else
6388 Found := True;
6389 T_F := T1;
6390 I_F := Index;
6391 end if;
6392
6393 Set_Etype (L, T_F);
6394 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6395 end if;
6396 end Try_One_Interp;
6397
6398 -- Start of processing for Find_Comparison_Types
6399
6400 begin
6401 -- If left operand is aggregate, the right operand has to
6402 -- provide a usable type for it.
6403
6404 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
6405 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6406 return;
6407 end if;
6408
6409 if Nkind (N) = N_Function_Call
6410 and then Nkind (Name (N)) = N_Expanded_Name
6411 then
6412 Scop := Entity (Prefix (Name (N)));
6413
6414 -- The prefix may be a package renaming, and the subsequent test
6415 -- requires the original package.
6416
6417 if Ekind (Scop) = E_Package
6418 and then Present (Renamed_Entity (Scop))
6419 then
6420 Scop := Renamed_Entity (Scop);
6421 Set_Entity (Prefix (Name (N)), Scop);
6422 end if;
6423 end if;
6424
6425 if not Is_Overloaded (L) then
6426 Try_One_Interp (Etype (L));
6427
6428 else
6429 Get_First_Interp (L, Index, It);
6430 while Present (It.Typ) loop
6431 Try_One_Interp (It.Typ);
6432 Get_Next_Interp (Index, It);
6433 end loop;
6434 end if;
6435 end Find_Comparison_Types;
6436
6437 ----------------------------------------
6438 -- Find_Non_Universal_Interpretations --
6439 ----------------------------------------
6440
6441 procedure Find_Non_Universal_Interpretations
6442 (N : Node_Id;
6443 R : Node_Id;
6444 Op_Id : Entity_Id;
6445 T1 : Entity_Id)
6446 is
6447 Index : Interp_Index;
6448 It : Interp;
6449
6450 begin
6451 if T1 = Universal_Integer or else T1 = Universal_Real
6452
6453 -- If the left operand of an equality operator is null, the visibility
6454 -- of the operator must be determined from the interpretation of the
6455 -- right operand. This processing must be done for Any_Access, which
6456 -- is the internal representation of the type of the literal null.
6457
6458 or else T1 = Any_Access
6459 then
6460 if not Is_Overloaded (R) then
6461 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
6462 else
6463 Get_First_Interp (R, Index, It);
6464 while Present (It.Typ) loop
6465 if Covers (It.Typ, T1) then
6466 Add_One_Interp
6467 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
6468 end if;
6469
6470 Get_Next_Interp (Index, It);
6471 end loop;
6472 end if;
6473 else
6474 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
6475 end if;
6476 end Find_Non_Universal_Interpretations;
6477
6478 ------------------------------
6479 -- Find_Concatenation_Types --
6480 ------------------------------
6481
6482 procedure Find_Concatenation_Types
6483 (L, R : Node_Id;
6484 Op_Id : Entity_Id;
6485 N : Node_Id)
6486 is
6487 Is_String : constant Boolean := Nkind (L) = N_String_Literal
6488 or else
6489 Nkind (R) = N_String_Literal;
6490 Op_Type : constant Entity_Id := Etype (Op_Id);
6491
6492 begin
6493 if Is_Array_Type (Op_Type)
6494
6495 -- Small but very effective optimization: if at least one operand is a
6496 -- string literal, then the type of the operator must be either array
6497 -- of characters or array of strings.
6498
6499 and then (not Is_String
6500 or else
6501 Is_Character_Type (Component_Type (Op_Type))
6502 or else
6503 Is_String_Type (Component_Type (Op_Type)))
6504
6505 and then not Is_Limited_Type (Op_Type)
6506
6507 and then (Has_Compatible_Type (L, Op_Type)
6508 or else
6509 Has_Compatible_Type (L, Component_Type (Op_Type)))
6510
6511 and then (Has_Compatible_Type (R, Op_Type)
6512 or else
6513 Has_Compatible_Type (R, Component_Type (Op_Type)))
6514 then
6515 Add_One_Interp (N, Op_Id, Op_Type);
6516 end if;
6517 end Find_Concatenation_Types;
6518
6519 -------------------------
6520 -- Find_Equality_Types --
6521 -------------------------
6522
6523 procedure Find_Equality_Types
6524 (L, R : Node_Id;
6525 Op_Id : Entity_Id;
6526 N : Node_Id)
6527 is
6528 Index : Interp_Index;
6529 It : Interp;
6530 Found : Boolean := False;
6531 I_F : Interp_Index;
6532 T_F : Entity_Id;
6533 Scop : Entity_Id := Empty;
6534
6535 procedure Try_One_Interp (T1 : Entity_Id);
6536 -- The context of the equality operator plays no role in resolving the
6537 -- arguments, so that if there is more than one interpretation of the
6538 -- operands that is compatible with equality, the construct is ambiguous
6539 -- and an error can be emitted now, after trying to disambiguate, i.e.
6540 -- applying preference rules.
6541
6542 --------------------
6543 -- Try_One_Interp --
6544 --------------------
6545
6546 procedure Try_One_Interp (T1 : Entity_Id) is
6547 Bas : Entity_Id;
6548
6549 begin
6550 -- Perform a sanity check in case of previous errors
6551
6552 if No (T1) then
6553 return;
6554 end if;
6555
6556 Bas := Base_Type (T1);
6557
6558 -- If the operator is an expanded name, then the type of the operand
6559 -- must be defined in the corresponding scope. If the type is
6560 -- universal, the context will impose the correct type. An anonymous
6561 -- type for a 'Access reference is also universal in this sense, as
6562 -- the actual type is obtained from context.
6563
6564 -- In Ada 2005, the equality operator for anonymous access types
6565 -- is declared in Standard, and preference rules apply to it.
6566
6567 if Present (Scop) then
6568
6569 -- Note that we avoid returning if we are currently within a
6570 -- generic instance due to the fact that the generic package
6571 -- declaration has already been successfully analyzed and
6572 -- Defined_In_Scope expects the base type to be defined within
6573 -- the instance which will never be the case.
6574
6575 if Defined_In_Scope (T1, Scop)
6576 or else In_Instance
6577 or else T1 = Universal_Integer
6578 or else T1 = Universal_Real
6579 or else T1 = Any_Access
6580 or else T1 = Any_String
6581 or else T1 = Any_Composite
6582 or else (Ekind (T1) = E_Access_Subprogram_Type
6583 and then not Comes_From_Source (T1))
6584 then
6585 null;
6586
6587 elsif Ekind (T1) = E_Anonymous_Access_Type
6588 and then Scop = Standard_Standard
6589 then
6590 null;
6591
6592 else
6593 -- The scope does not contain an operator for the type
6594
6595 return;
6596 end if;
6597
6598 -- If we have infix notation, the operator must be usable. Within
6599 -- an instance, if the type is already established we know it is
6600 -- correct. If an operand is universal it is compatible with any
6601 -- numeric type.
6602
6603 elsif In_Open_Scopes (Scope (Bas))
6604 or else Is_Potentially_Use_Visible (Bas)
6605 or else In_Use (Bas)
6606 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
6607
6608 -- In an instance, the type may have been immediately visible.
6609 -- Either the types are compatible, or one operand is universal
6610 -- (numeric or null).
6611
6612 or else
6613 ((In_Instance or else In_Inlined_Body)
6614 and then
6615 (First_Subtype (T1) = First_Subtype (Etype (R))
6616 or else Nkind (R) = N_Null
6617 or else
6618 (Is_Numeric_Type (T1)
6619 and then Is_Universal_Numeric_Type (Etype (R)))))
6620
6621 -- In Ada 2005, the equality on anonymous access types is declared
6622 -- in Standard, and is always visible.
6623
6624 or else Ekind (T1) = E_Anonymous_Access_Type
6625 then
6626 null;
6627
6628 else
6629 -- Save candidate type for subsequent error message, if any
6630
6631 if not Is_Limited_Type (T1) then
6632 Candidate_Type := T1;
6633 end if;
6634
6635 return;
6636 end if;
6637
6638 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
6639 -- Do not allow anonymous access types in equality operators.
6640
6641 if Ada_Version < Ada_2005
6642 and then Ekind (T1) = E_Anonymous_Access_Type
6643 then
6644 return;
6645 end if;
6646
6647 -- If the right operand has a type compatible with T1, check for an
6648 -- acceptable interpretation, unless T1 is limited (no predefined
6649 -- equality available), or this is use of a "/=" for a tagged type.
6650 -- In the latter case, possible interpretations of equality need
6651 -- to be considered, we don't want the default inequality declared
6652 -- in Standard to be chosen, and the "/=" will be rewritten as a
6653 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
6654 -- that rewriting happens during analysis rather than being
6655 -- delayed until expansion (this is needed for ASIS, which only sees
6656 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
6657 -- is Name_Op_Eq then we still proceed with the interpretation,
6658 -- because that indicates the potential rewriting case where the
6659 -- interpretation to consider is actually "=" and the node may be
6660 -- about to be rewritten by Analyze_Equality_Op.
6661
6662 if T1 /= Standard_Void_Type
6663 and then Has_Compatible_Type (R, T1)
6664
6665 and then
6666 ((not Is_Limited_Type (T1)
6667 and then not Is_Limited_Composite (T1))
6668
6669 or else
6670 (Is_Array_Type (T1)
6671 and then not Is_Limited_Type (Component_Type (T1))
6672 and then Available_Full_View_Of_Component (T1)))
6673
6674 and then
6675 (Nkind (N) /= N_Op_Ne
6676 or else not Is_Tagged_Type (T1)
6677 or else Chars (Op_Id) = Name_Op_Eq)
6678 then
6679 if Found
6680 and then Base_Type (T1) /= Base_Type (T_F)
6681 then
6682 It := Disambiguate (L, I_F, Index, Any_Type);
6683
6684 if It = No_Interp then
6685 Ambiguous_Operands (N);
6686 Set_Etype (L, Any_Type);
6687 return;
6688
6689 else
6690 T_F := It.Typ;
6691 end if;
6692
6693 else
6694 Found := True;
6695 T_F := T1;
6696 I_F := Index;
6697 end if;
6698
6699 if not Analyzed (L) then
6700 Set_Etype (L, T_F);
6701 end if;
6702
6703 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6704
6705 -- Case of operator was not visible, Etype still set to Any_Type
6706
6707 if Etype (N) = Any_Type then
6708 Found := False;
6709 end if;
6710
6711 elsif Scop = Standard_Standard
6712 and then Ekind (T1) = E_Anonymous_Access_Type
6713 then
6714 Found := True;
6715 end if;
6716 end Try_One_Interp;
6717
6718 -- Start of processing for Find_Equality_Types
6719
6720 begin
6721 -- If left operand is aggregate, the right operand has to
6722 -- provide a usable type for it.
6723
6724 if Nkind (L) = N_Aggregate
6725 and then Nkind (R) /= N_Aggregate
6726 then
6727 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6728 return;
6729 end if;
6730
6731 if Nkind (N) = N_Function_Call
6732 and then Nkind (Name (N)) = N_Expanded_Name
6733 then
6734 Scop := Entity (Prefix (Name (N)));
6735
6736 -- The prefix may be a package renaming, and the subsequent test
6737 -- requires the original package.
6738
6739 if Ekind (Scop) = E_Package
6740 and then Present (Renamed_Entity (Scop))
6741 then
6742 Scop := Renamed_Entity (Scop);
6743 Set_Entity (Prefix (Name (N)), Scop);
6744 end if;
6745 end if;
6746
6747 if not Is_Overloaded (L) then
6748 Try_One_Interp (Etype (L));
6749
6750 else
6751 Get_First_Interp (L, Index, It);
6752 while Present (It.Typ) loop
6753 Try_One_Interp (It.Typ);
6754 Get_Next_Interp (Index, It);
6755 end loop;
6756 end if;
6757 end Find_Equality_Types;
6758
6759 -------------------------
6760 -- Find_Negation_Types --
6761 -------------------------
6762
6763 procedure Find_Negation_Types
6764 (R : Node_Id;
6765 Op_Id : Entity_Id;
6766 N : Node_Id)
6767 is
6768 Index : Interp_Index;
6769 It : Interp;
6770
6771 begin
6772 if not Is_Overloaded (R) then
6773 if Etype (R) = Universal_Integer then
6774 Add_One_Interp (N, Op_Id, Any_Modular);
6775 elsif Valid_Boolean_Arg (Etype (R)) then
6776 Add_One_Interp (N, Op_Id, Etype (R));
6777 end if;
6778
6779 else
6780 Get_First_Interp (R, Index, It);
6781 while Present (It.Typ) loop
6782 if Valid_Boolean_Arg (It.Typ) then
6783 Add_One_Interp (N, Op_Id, It.Typ);
6784 end if;
6785
6786 Get_Next_Interp (Index, It);
6787 end loop;
6788 end if;
6789 end Find_Negation_Types;
6790
6791 ------------------------------
6792 -- Find_Primitive_Operation --
6793 ------------------------------
6794
6795 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6796 Obj : constant Node_Id := Prefix (N);
6797 Op : constant Node_Id := Selector_Name (N);
6798
6799 Prim : Elmt_Id;
6800 Prims : Elist_Id;
6801 Typ : Entity_Id;
6802
6803 begin
6804 Set_Etype (Op, Any_Type);
6805
6806 if Is_Access_Type (Etype (Obj)) then
6807 Typ := Designated_Type (Etype (Obj));
6808 else
6809 Typ := Etype (Obj);
6810 end if;
6811
6812 if Is_Class_Wide_Type (Typ) then
6813 Typ := Root_Type (Typ);
6814 end if;
6815
6816 Prims := Primitive_Operations (Typ);
6817
6818 Prim := First_Elmt (Prims);
6819 while Present (Prim) loop
6820 if Chars (Node (Prim)) = Chars (Op) then
6821 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6822 Set_Etype (N, Etype (Node (Prim)));
6823 end if;
6824
6825 Next_Elmt (Prim);
6826 end loop;
6827
6828 -- Now look for class-wide operations of the type or any of its
6829 -- ancestors by iterating over the homonyms of the selector.
6830
6831 declare
6832 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6833 Hom : Entity_Id;
6834
6835 begin
6836 Hom := Current_Entity (Op);
6837 while Present (Hom) loop
6838 if (Ekind (Hom) = E_Procedure
6839 or else
6840 Ekind (Hom) = E_Function)
6841 and then Scope (Hom) = Scope (Typ)
6842 and then Present (First_Formal (Hom))
6843 and then
6844 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6845 or else
6846 (Is_Access_Type (Etype (First_Formal (Hom)))
6847 and then
6848 Ekind (Etype (First_Formal (Hom))) =
6849 E_Anonymous_Access_Type
6850 and then
6851 Base_Type
6852 (Designated_Type (Etype (First_Formal (Hom)))) =
6853 Cls_Type))
6854 then
6855 Add_One_Interp (Op, Hom, Etype (Hom));
6856 Set_Etype (N, Etype (Hom));
6857 end if;
6858
6859 Hom := Homonym (Hom);
6860 end loop;
6861 end;
6862
6863 return Etype (Op) /= Any_Type;
6864 end Find_Primitive_Operation;
6865
6866 ----------------------
6867 -- Find_Unary_Types --
6868 ----------------------
6869
6870 procedure Find_Unary_Types
6871 (R : Node_Id;
6872 Op_Id : Entity_Id;
6873 N : Node_Id)
6874 is
6875 Index : Interp_Index;
6876 It : Interp;
6877
6878 begin
6879 if not Is_Overloaded (R) then
6880 if Is_Numeric_Type (Etype (R)) then
6881
6882 -- In an instance a generic actual may be a numeric type even if
6883 -- the formal in the generic unit was not. In that case, the
6884 -- predefined operator was not a possible interpretation in the
6885 -- generic, and cannot be one in the instance, unless the operator
6886 -- is an actual of an instance.
6887
6888 if In_Instance
6889 and then
6890 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6891 then
6892 null;
6893 else
6894 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6895 end if;
6896 end if;
6897
6898 else
6899 Get_First_Interp (R, Index, It);
6900 while Present (It.Typ) loop
6901 if Is_Numeric_Type (It.Typ) then
6902 if In_Instance
6903 and then
6904 not Is_Numeric_Type
6905 (Corresponding_Generic_Type (Etype (It.Typ)))
6906 then
6907 null;
6908
6909 else
6910 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6911 end if;
6912 end if;
6913
6914 Get_Next_Interp (Index, It);
6915 end loop;
6916 end if;
6917 end Find_Unary_Types;
6918
6919 ------------------
6920 -- Junk_Operand --
6921 ------------------
6922
6923 function Junk_Operand (N : Node_Id) return Boolean is
6924 Enode : Node_Id;
6925
6926 begin
6927 if Error_Posted (N) then
6928 return False;
6929 end if;
6930
6931 -- Get entity to be tested
6932
6933 if Is_Entity_Name (N)
6934 and then Present (Entity (N))
6935 then
6936 Enode := N;
6937
6938 -- An odd case, a procedure name gets converted to a very peculiar
6939 -- function call, and here is where we detect this happening.
6940
6941 elsif Nkind (N) = N_Function_Call
6942 and then Is_Entity_Name (Name (N))
6943 and then Present (Entity (Name (N)))
6944 then
6945 Enode := Name (N);
6946
6947 -- Another odd case, there are at least some cases of selected
6948 -- components where the selected component is not marked as having
6949 -- an entity, even though the selector does have an entity
6950
6951 elsif Nkind (N) = N_Selected_Component
6952 and then Present (Entity (Selector_Name (N)))
6953 then
6954 Enode := Selector_Name (N);
6955
6956 else
6957 return False;
6958 end if;
6959
6960 -- Now test the entity we got to see if it is a bad case
6961
6962 case Ekind (Entity (Enode)) is
6963 when E_Package =>
6964 Error_Msg_N
6965 ("package name cannot be used as operand", Enode);
6966
6967 when Generic_Unit_Kind =>
6968 Error_Msg_N
6969 ("generic unit name cannot be used as operand", Enode);
6970
6971 when Type_Kind =>
6972 Error_Msg_N
6973 ("subtype name cannot be used as operand", Enode);
6974
6975 when Entry_Kind =>
6976 Error_Msg_N
6977 ("entry name cannot be used as operand", Enode);
6978
6979 when E_Procedure =>
6980 Error_Msg_N
6981 ("procedure name cannot be used as operand", Enode);
6982
6983 when E_Exception =>
6984 Error_Msg_N
6985 ("exception name cannot be used as operand", Enode);
6986
6987 when E_Block
6988 | E_Label
6989 | E_Loop
6990 =>
6991 Error_Msg_N
6992 ("label name cannot be used as operand", Enode);
6993
6994 when others =>
6995 return False;
6996 end case;
6997
6998 return True;
6999 end Junk_Operand;
7000
7001 --------------------
7002 -- Operator_Check --
7003 --------------------
7004
7005 procedure Operator_Check (N : Node_Id) is
7006 begin
7007 Remove_Abstract_Operations (N);
7008
7009 -- Test for case of no interpretation found for operator
7010
7011 if Etype (N) = Any_Type then
7012 declare
7013 L : Node_Id;
7014 R : Node_Id;
7015 Op_Id : Entity_Id := Empty;
7016
7017 begin
7018 R := Right_Opnd (N);
7019
7020 if Nkind (N) in N_Binary_Op then
7021 L := Left_Opnd (N);
7022 else
7023 L := Empty;
7024 end if;
7025
7026 -- If either operand has no type, then don't complain further,
7027 -- since this simply means that we have a propagated error.
7028
7029 if R = Error
7030 or else Etype (R) = Any_Type
7031 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
7032 then
7033 -- For the rather unusual case where one of the operands is
7034 -- a Raise_Expression, whose initial type is Any_Type, use
7035 -- the type of the other operand.
7036
7037 if Nkind (L) = N_Raise_Expression then
7038 Set_Etype (L, Etype (R));
7039 Set_Etype (N, Etype (R));
7040
7041 elsif Nkind (R) = N_Raise_Expression then
7042 Set_Etype (R, Etype (L));
7043 Set_Etype (N, Etype (L));
7044 end if;
7045
7046 return;
7047
7048 -- We explicitly check for the case of concatenation of component
7049 -- with component to avoid reporting spurious matching array types
7050 -- that might happen to be lurking in distant packages (such as
7051 -- run-time packages). This also prevents inconsistencies in the
7052 -- messages for certain ACVC B tests, which can vary depending on
7053 -- types declared in run-time interfaces. Another improvement when
7054 -- aggregates are present is to look for a well-typed operand.
7055
7056 elsif Present (Candidate_Type)
7057 and then (Nkind (N) /= N_Op_Concat
7058 or else Is_Array_Type (Etype (L))
7059 or else Is_Array_Type (Etype (R)))
7060 then
7061 if Nkind (N) = N_Op_Concat then
7062 if Etype (L) /= Any_Composite
7063 and then Is_Array_Type (Etype (L))
7064 then
7065 Candidate_Type := Etype (L);
7066
7067 elsif Etype (R) /= Any_Composite
7068 and then Is_Array_Type (Etype (R))
7069 then
7070 Candidate_Type := Etype (R);
7071 end if;
7072 end if;
7073
7074 Error_Msg_NE -- CODEFIX
7075 ("operator for} is not directly visible!",
7076 N, First_Subtype (Candidate_Type));
7077
7078 declare
7079 U : constant Node_Id :=
7080 Cunit (Get_Source_Unit (Candidate_Type));
7081 begin
7082 if Unit_Is_Visible (U) then
7083 Error_Msg_N -- CODEFIX
7084 ("use clause would make operation legal!", N);
7085 else
7086 Error_Msg_NE -- CODEFIX
7087 ("add with_clause and use_clause for&!",
7088 N, Defining_Entity (Unit (U)));
7089 end if;
7090 end;
7091 return;
7092
7093 -- If either operand is a junk operand (e.g. package name), then
7094 -- post appropriate error messages, but do not complain further.
7095
7096 -- Note that the use of OR in this test instead of OR ELSE is
7097 -- quite deliberate, we may as well check both operands in the
7098 -- binary operator case.
7099
7100 elsif Junk_Operand (R)
7101 or -- really mean OR here and not OR ELSE, see above
7102 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
7103 then
7104 return;
7105
7106 -- If we have a logical operator, one of whose operands is
7107 -- Boolean, then we know that the other operand cannot resolve to
7108 -- Boolean (since we got no interpretations), but in that case we
7109 -- pretty much know that the other operand should be Boolean, so
7110 -- resolve it that way (generating an error).
7111
7112 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
7113 if Etype (L) = Standard_Boolean then
7114 Resolve (R, Standard_Boolean);
7115 return;
7116 elsif Etype (R) = Standard_Boolean then
7117 Resolve (L, Standard_Boolean);
7118 return;
7119 end if;
7120
7121 -- For an arithmetic operator or comparison operator, if one
7122 -- of the operands is numeric, then we know the other operand
7123 -- is not the same numeric type. If it is a non-numeric type,
7124 -- then probably it is intended to match the other operand.
7125
7126 elsif Nkind_In (N, N_Op_Add,
7127 N_Op_Divide,
7128 N_Op_Ge,
7129 N_Op_Gt,
7130 N_Op_Le)
7131 or else
7132 Nkind_In (N, N_Op_Lt,
7133 N_Op_Mod,
7134 N_Op_Multiply,
7135 N_Op_Rem,
7136 N_Op_Subtract)
7137 then
7138 -- If Allow_Integer_Address is active, check whether the
7139 -- operation becomes legal after converting an operand.
7140
7141 if Is_Numeric_Type (Etype (L))
7142 and then not Is_Numeric_Type (Etype (R))
7143 then
7144 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7145 Rewrite (R,
7146 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7147
7148 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7149 Analyze_Comparison_Op (N);
7150 else
7151 Analyze_Arithmetic_Op (N);
7152 end if;
7153 else
7154 Resolve (R, Etype (L));
7155 end if;
7156
7157 return;
7158
7159 elsif Is_Numeric_Type (Etype (R))
7160 and then not Is_Numeric_Type (Etype (L))
7161 then
7162 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
7163 Rewrite (L,
7164 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
7165
7166 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7167 Analyze_Comparison_Op (N);
7168 else
7169 Analyze_Arithmetic_Op (N);
7170 end if;
7171
7172 return;
7173
7174 else
7175 Resolve (L, Etype (R));
7176 end if;
7177
7178 return;
7179
7180 elsif Allow_Integer_Address
7181 and then Is_Descendant_Of_Address (Etype (L))
7182 and then Is_Descendant_Of_Address (Etype (R))
7183 and then not Error_Posted (N)
7184 then
7185 declare
7186 Addr_Type : constant Entity_Id := Etype (L);
7187
7188 begin
7189 Rewrite (L,
7190 Unchecked_Convert_To (
7191 Standard_Integer, Relocate_Node (L)));
7192 Rewrite (R,
7193 Unchecked_Convert_To (
7194 Standard_Integer, Relocate_Node (R)));
7195
7196 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7197 Analyze_Comparison_Op (N);
7198 else
7199 Analyze_Arithmetic_Op (N);
7200 end if;
7201
7202 -- If this is an operand in an enclosing arithmetic
7203 -- operation, Convert the result as an address so that
7204 -- arithmetic folding of address can continue.
7205
7206 if Nkind (Parent (N)) in N_Op then
7207 Rewrite (N,
7208 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7209 end if;
7210
7211 return;
7212 end;
7213
7214 -- Under relaxed RM semantics silently replace occurrences of
7215 -- null by System.Address_Null.
7216
7217 elsif Null_To_Null_Address_Convert_OK (N) then
7218 Replace_Null_By_Null_Address (N);
7219
7220 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7221 Analyze_Comparison_Op (N);
7222 else
7223 Analyze_Arithmetic_Op (N);
7224 end if;
7225
7226 return;
7227 end if;
7228
7229 -- Comparisons on A'Access are common enough to deserve a
7230 -- special message.
7231
7232 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
7233 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7234 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7235 then
7236 Error_Msg_N
7237 ("two access attributes cannot be compared directly", N);
7238 Error_Msg_N
7239 ("\use qualified expression for one of the operands",
7240 N);
7241 return;
7242
7243 -- Another one for C programmers
7244
7245 elsif Nkind (N) = N_Op_Concat
7246 and then Valid_Boolean_Arg (Etype (L))
7247 and then Valid_Boolean_Arg (Etype (R))
7248 then
7249 Error_Msg_N ("invalid operands for concatenation", N);
7250 Error_Msg_N -- CODEFIX
7251 ("\maybe AND was meant", N);
7252 return;
7253
7254 -- A special case for comparison of access parameter with null
7255
7256 elsif Nkind (N) = N_Op_Eq
7257 and then Is_Entity_Name (L)
7258 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7259 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7260 N_Access_Definition
7261 and then Nkind (R) = N_Null
7262 then
7263 Error_Msg_N ("access parameter is not allowed to be null", L);
7264 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7265 return;
7266
7267 -- Another special case for exponentiation, where the right
7268 -- operand must be Natural, independently of the base.
7269
7270 elsif Nkind (N) = N_Op_Expon
7271 and then Is_Numeric_Type (Etype (L))
7272 and then not Is_Overloaded (R)
7273 and then
7274 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7275 and then Base_Type (Etype (R)) /= Universal_Integer
7276 then
7277 if Ada_Version >= Ada_2012
7278 and then Has_Dimension_System (Etype (L))
7279 then
7280 Error_Msg_NE
7281 ("exponent for dimensioned type must be a rational" &
7282 ", found}", R, Etype (R));
7283 else
7284 Error_Msg_NE
7285 ("exponent must be of type Natural, found}", R, Etype (R));
7286 end if;
7287
7288 return;
7289
7290 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
7291 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7292 Rewrite (R,
7293 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7294 Analyze_Equality_Op (N);
7295 return;
7296
7297 -- Under relaxed RM semantics silently replace occurrences of
7298 -- null by System.Address_Null.
7299
7300 elsif Null_To_Null_Address_Convert_OK (N) then
7301 Replace_Null_By_Null_Address (N);
7302 Analyze_Equality_Op (N);
7303 return;
7304 end if;
7305 end if;
7306
7307 -- If we fall through then just give general message. Note that in
7308 -- the following messages, if the operand is overloaded we choose
7309 -- an arbitrary type to complain about, but that is probably more
7310 -- useful than not giving a type at all.
7311
7312 if Nkind (N) in N_Unary_Op then
7313 Error_Msg_Node_2 := Etype (R);
7314 Error_Msg_N ("operator& not defined for}", N);
7315 return;
7316
7317 else
7318 if Nkind (N) in N_Binary_Op then
7319 if not Is_Overloaded (L)
7320 and then not Is_Overloaded (R)
7321 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7322 then
7323 Error_Msg_Node_2 := First_Subtype (Etype (R));
7324 Error_Msg_N ("there is no applicable operator& for}", N);
7325
7326 else
7327 -- Another attempt to find a fix: one of the candidate
7328 -- interpretations may not be use-visible. This has
7329 -- already been checked for predefined operators, so
7330 -- we examine only user-defined functions.
7331
7332 Op_Id := Get_Name_Entity_Id (Chars (N));
7333
7334 while Present (Op_Id) loop
7335 if Ekind (Op_Id) /= E_Operator
7336 and then Is_Overloadable (Op_Id)
7337 then
7338 if not Is_Immediately_Visible (Op_Id)
7339 and then not In_Use (Scope (Op_Id))
7340 and then not Is_Abstract_Subprogram (Op_Id)
7341 and then not Is_Hidden (Op_Id)
7342 and then Ekind (Scope (Op_Id)) = E_Package
7343 and then
7344 Has_Compatible_Type
7345 (L, Etype (First_Formal (Op_Id)))
7346 and then Present
7347 (Next_Formal (First_Formal (Op_Id)))
7348 and then
7349 Has_Compatible_Type
7350 (R,
7351 Etype (Next_Formal (First_Formal (Op_Id))))
7352 then
7353 Error_Msg_N
7354 ("no legal interpretation for operator&", N);
7355 Error_Msg_NE
7356 ("\use clause on& would make operation legal",
7357 N, Scope (Op_Id));
7358 exit;
7359 end if;
7360 end if;
7361
7362 Op_Id := Homonym (Op_Id);
7363 end loop;
7364
7365 if No (Op_Id) then
7366 Error_Msg_N ("invalid operand types for operator&", N);
7367
7368 if Nkind (N) /= N_Op_Concat then
7369 Error_Msg_NE ("\left operand has}!", N, Etype (L));
7370 Error_Msg_NE ("\right operand has}!", N, Etype (R));
7371
7372 -- For multiplication and division operators with
7373 -- a fixed-point operand and an integer operand,
7374 -- indicate that the integer operand should be of
7375 -- type Integer.
7376
7377 if Nkind_In (N, N_Op_Multiply, N_Op_Divide)
7378 and then Is_Fixed_Point_Type (Etype (L))
7379 and then Is_Integer_Type (Etype (R))
7380 then
7381 Error_Msg_N
7382 ("\convert right operand to `Integer`", N);
7383
7384 elsif Nkind (N) = N_Op_Multiply
7385 and then Is_Fixed_Point_Type (Etype (R))
7386 and then Is_Integer_Type (Etype (L))
7387 then
7388 Error_Msg_N
7389 ("\convert left operand to `Integer`", N);
7390 end if;
7391
7392 -- For concatenation operators it is more difficult to
7393 -- determine which is the wrong operand. It is worth
7394 -- flagging explicitly an access type, for those who
7395 -- might think that a dereference happens here.
7396
7397 elsif Is_Access_Type (Etype (L)) then
7398 Error_Msg_N ("\left operand is access type", N);
7399
7400 elsif Is_Access_Type (Etype (R)) then
7401 Error_Msg_N ("\right operand is access type", N);
7402 end if;
7403 end if;
7404 end if;
7405 end if;
7406 end if;
7407 end;
7408 end if;
7409 end Operator_Check;
7410
7411 -----------------------------------------
7412 -- Process_Implicit_Dereference_Prefix --
7413 -----------------------------------------
7414
7415 function Process_Implicit_Dereference_Prefix
7416 (E : Entity_Id;
7417 P : Entity_Id) return Entity_Id
7418 is
7419 Ref : Node_Id;
7420 Typ : constant Entity_Id := Designated_Type (Etype (P));
7421
7422 begin
7423 if Present (E)
7424 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
7425 then
7426 -- We create a dummy reference to E to ensure that the reference is
7427 -- not considered as part of an assignment (an implicit dereference
7428 -- can never assign to its prefix). The Comes_From_Source attribute
7429 -- needs to be propagated for accurate warnings.
7430
7431 Ref := New_Occurrence_Of (E, Sloc (P));
7432 Set_Comes_From_Source (Ref, Comes_From_Source (P));
7433 Generate_Reference (E, Ref);
7434 end if;
7435
7436 -- An implicit dereference is a legal occurrence of an incomplete type
7437 -- imported through a limited_with clause, if the full view is visible.
7438
7439 if From_Limited_With (Typ)
7440 and then not From_Limited_With (Scope (Typ))
7441 and then
7442 (Is_Immediately_Visible (Scope (Typ))
7443 or else
7444 (Is_Child_Unit (Scope (Typ))
7445 and then Is_Visible_Lib_Unit (Scope (Typ))))
7446 then
7447 return Available_View (Typ);
7448 else
7449 return Typ;
7450 end if;
7451 end Process_Implicit_Dereference_Prefix;
7452
7453 --------------------------------
7454 -- Remove_Abstract_Operations --
7455 --------------------------------
7456
7457 procedure Remove_Abstract_Operations (N : Node_Id) is
7458 Abstract_Op : Entity_Id := Empty;
7459 Address_Descendant : Boolean := False;
7460 I : Interp_Index;
7461 It : Interp;
7462
7463 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
7464 -- activate this if either extensions are enabled, or if the abstract
7465 -- operation in question comes from a predefined file. This latter test
7466 -- allows us to use abstract to make operations invisible to users. In
7467 -- particular, if type Address is non-private and abstract subprograms
7468 -- are used to hide its operators, they will be truly hidden.
7469
7470 type Operand_Position is (First_Op, Second_Op);
7471 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
7472
7473 procedure Remove_Address_Interpretations (Op : Operand_Position);
7474 -- Ambiguities may arise when the operands are literal and the address
7475 -- operations in s-auxdec are visible. In that case, remove the
7476 -- interpretation of a literal as Address, to retain the semantics
7477 -- of Address as a private type.
7478
7479 ------------------------------------
7480 -- Remove_Address_Interpretations --
7481 ------------------------------------
7482
7483 procedure Remove_Address_Interpretations (Op : Operand_Position) is
7484 Formal : Entity_Id;
7485
7486 begin
7487 if Is_Overloaded (N) then
7488 Get_First_Interp (N, I, It);
7489 while Present (It.Nam) loop
7490 Formal := First_Entity (It.Nam);
7491
7492 if Op = Second_Op then
7493 Formal := Next_Entity (Formal);
7494 end if;
7495
7496 if Is_Descendant_Of_Address (Etype (Formal)) then
7497 Address_Descendant := True;
7498 Remove_Interp (I);
7499 end if;
7500
7501 Get_Next_Interp (I, It);
7502 end loop;
7503 end if;
7504 end Remove_Address_Interpretations;
7505
7506 -- Start of processing for Remove_Abstract_Operations
7507
7508 begin
7509 if Is_Overloaded (N) then
7510 if Debug_Flag_V then
7511 Write_Line ("Remove_Abstract_Operations: ");
7512 Write_Overloads (N);
7513 end if;
7514
7515 Get_First_Interp (N, I, It);
7516
7517 while Present (It.Nam) loop
7518 if Is_Overloadable (It.Nam)
7519 and then Is_Abstract_Subprogram (It.Nam)
7520 and then not Is_Dispatching_Operation (It.Nam)
7521 then
7522 Abstract_Op := It.Nam;
7523
7524 if Is_Descendant_Of_Address (It.Typ) then
7525 Address_Descendant := True;
7526 Remove_Interp (I);
7527 exit;
7528
7529 -- In Ada 2005, this operation does not participate in overload
7530 -- resolution. If the operation is defined in a predefined
7531 -- unit, it is one of the operations declared abstract in some
7532 -- variants of System, and it must be removed as well.
7533
7534 elsif Ada_Version >= Ada_2005
7535 or else In_Predefined_Unit (It.Nam)
7536 then
7537 Remove_Interp (I);
7538 exit;
7539 end if;
7540 end if;
7541
7542 Get_Next_Interp (I, It);
7543 end loop;
7544
7545 if No (Abstract_Op) then
7546
7547 -- If some interpretation yields an integer type, it is still
7548 -- possible that there are address interpretations. Remove them
7549 -- if one operand is a literal, to avoid spurious ambiguities
7550 -- on systems where Address is a visible integer type.
7551
7552 if Is_Overloaded (N)
7553 and then Nkind (N) in N_Op
7554 and then Is_Integer_Type (Etype (N))
7555 then
7556 if Nkind (N) in N_Binary_Op then
7557 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
7558 Remove_Address_Interpretations (Second_Op);
7559
7560 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
7561 Remove_Address_Interpretations (First_Op);
7562 end if;
7563 end if;
7564 end if;
7565
7566 elsif Nkind (N) in N_Op then
7567
7568 -- Remove interpretations that treat literals as addresses. This
7569 -- is never appropriate, even when Address is defined as a visible
7570 -- Integer type. The reason is that we would really prefer Address
7571 -- to behave as a private type, even in this case. If Address is a
7572 -- visible integer type, we get lots of overload ambiguities.
7573
7574 if Nkind (N) in N_Binary_Op then
7575 declare
7576 U1 : constant Boolean :=
7577 Present (Universal_Interpretation (Right_Opnd (N)));
7578 U2 : constant Boolean :=
7579 Present (Universal_Interpretation (Left_Opnd (N)));
7580
7581 begin
7582 if U1 then
7583 Remove_Address_Interpretations (Second_Op);
7584 end if;
7585
7586 if U2 then
7587 Remove_Address_Interpretations (First_Op);
7588 end if;
7589
7590 if not (U1 and U2) then
7591
7592 -- Remove corresponding predefined operator, which is
7593 -- always added to the overload set.
7594
7595 Get_First_Interp (N, I, It);
7596 while Present (It.Nam) loop
7597 if Scope (It.Nam) = Standard_Standard
7598 and then Base_Type (It.Typ) =
7599 Base_Type (Etype (Abstract_Op))
7600 then
7601 Remove_Interp (I);
7602 end if;
7603
7604 Get_Next_Interp (I, It);
7605 end loop;
7606
7607 elsif Is_Overloaded (N)
7608 and then Present (Univ_Type)
7609 then
7610 -- If both operands have a universal interpretation,
7611 -- it is still necessary to remove interpretations that
7612 -- yield Address. Any remaining ambiguities will be
7613 -- removed in Disambiguate.
7614
7615 Get_First_Interp (N, I, It);
7616 while Present (It.Nam) loop
7617 if Is_Descendant_Of_Address (It.Typ) then
7618 Remove_Interp (I);
7619
7620 elsif not Is_Type (It.Nam) then
7621 Set_Entity (N, It.Nam);
7622 end if;
7623
7624 Get_Next_Interp (I, It);
7625 end loop;
7626 end if;
7627 end;
7628 end if;
7629
7630 elsif Nkind (N) = N_Function_Call
7631 and then
7632 (Nkind (Name (N)) = N_Operator_Symbol
7633 or else
7634 (Nkind (Name (N)) = N_Expanded_Name
7635 and then
7636 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
7637 then
7638
7639 declare
7640 Arg1 : constant Node_Id := First (Parameter_Associations (N));
7641 U1 : constant Boolean :=
7642 Present (Universal_Interpretation (Arg1));
7643 U2 : constant Boolean :=
7644 Present (Next (Arg1)) and then
7645 Present (Universal_Interpretation (Next (Arg1)));
7646
7647 begin
7648 if U1 then
7649 Remove_Address_Interpretations (First_Op);
7650 end if;
7651
7652 if U2 then
7653 Remove_Address_Interpretations (Second_Op);
7654 end if;
7655
7656 if not (U1 and U2) then
7657 Get_First_Interp (N, I, It);
7658 while Present (It.Nam) loop
7659 if Scope (It.Nam) = Standard_Standard
7660 and then It.Typ = Base_Type (Etype (Abstract_Op))
7661 then
7662 Remove_Interp (I);
7663 end if;
7664
7665 Get_Next_Interp (I, It);
7666 end loop;
7667 end if;
7668 end;
7669 end if;
7670
7671 -- If the removal has left no valid interpretations, emit an error
7672 -- message now and label node as illegal.
7673
7674 if Present (Abstract_Op) then
7675 Get_First_Interp (N, I, It);
7676
7677 if No (It.Nam) then
7678
7679 -- Removal of abstract operation left no viable candidate
7680
7681 Set_Etype (N, Any_Type);
7682 Error_Msg_Sloc := Sloc (Abstract_Op);
7683 Error_Msg_NE
7684 ("cannot call abstract operation& declared#", N, Abstract_Op);
7685
7686 -- In Ada 2005, an abstract operation may disable predefined
7687 -- operators. Since the context is not yet known, we mark the
7688 -- predefined operators as potentially hidden. Do not include
7689 -- predefined operators when addresses are involved since this
7690 -- case is handled separately.
7691
7692 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
7693 while Present (It.Nam) loop
7694 if Is_Numeric_Type (It.Typ)
7695 and then Scope (It.Typ) = Standard_Standard
7696 then
7697 Set_Abstract_Op (I, Abstract_Op);
7698 end if;
7699
7700 Get_Next_Interp (I, It);
7701 end loop;
7702 end if;
7703 end if;
7704
7705 if Debug_Flag_V then
7706 Write_Line ("Remove_Abstract_Operations done: ");
7707 Write_Overloads (N);
7708 end if;
7709 end if;
7710 end Remove_Abstract_Operations;
7711
7712 ----------------------------
7713 -- Try_Container_Indexing --
7714 ----------------------------
7715
7716 function Try_Container_Indexing
7717 (N : Node_Id;
7718 Prefix : Node_Id;
7719 Exprs : List_Id) return Boolean
7720 is
7721 Pref_Typ : constant Entity_Id := Etype (Prefix);
7722
7723 function Constant_Indexing_OK return Boolean;
7724 -- Constant_Indexing is legal if there is no Variable_Indexing defined
7725 -- for the type, or else node not a target of assignment, or an actual
7726 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
7727
7728 function Expr_Matches_In_Formal
7729 (Subp : Entity_Id;
7730 Par : Node_Id) return Boolean;
7731 -- Find formal corresponding to given indexed component that is an
7732 -- actual in a call. Note that the enclosing subprogram call has not
7733 -- been analyzed yet, and the parameter list is not normalized, so
7734 -- that if the argument is a parameter association we must match it
7735 -- by name and not by position.
7736
7737 function Find_Indexing_Operations
7738 (T : Entity_Id;
7739 Nam : Name_Id;
7740 Is_Constant : Boolean) return Node_Id;
7741 -- Return a reference to the primitive operation of type T denoted by
7742 -- name Nam. If the operation is overloaded, the reference carries all
7743 -- interpretations. Flag Is_Constant should be set when the context is
7744 -- constant indexing.
7745
7746 --------------------------
7747 -- Constant_Indexing_OK --
7748 --------------------------
7749
7750 function Constant_Indexing_OK return Boolean is
7751 Par : Node_Id;
7752
7753 begin
7754 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
7755 return True;
7756
7757 elsif not Is_Variable (Prefix) then
7758 return True;
7759 end if;
7760
7761 Par := N;
7762 while Present (Par) loop
7763 if Nkind (Parent (Par)) = N_Assignment_Statement
7764 and then Par = Name (Parent (Par))
7765 then
7766 return False;
7767
7768 -- The call may be overloaded, in which case we assume that its
7769 -- resolution does not depend on the type of the parameter that
7770 -- includes the indexing operation.
7771
7772 elsif Nkind_In (Parent (Par), N_Function_Call,
7773 N_Procedure_Call_Statement)
7774 and then Is_Entity_Name (Name (Parent (Par)))
7775 then
7776 declare
7777 Proc : Entity_Id;
7778
7779 begin
7780 -- We should look for an interpretation with the proper
7781 -- number of formals, and determine whether it is an
7782 -- In_Parameter, but for now we examine the formal that
7783 -- corresponds to the indexing, and assume that variable
7784 -- indexing is required if some interpretation has an
7785 -- assignable formal at that position. Still does not
7786 -- cover the most complex cases ???
7787
7788 if Is_Overloaded (Name (Parent (Par))) then
7789 declare
7790 Proc : constant Node_Id := Name (Parent (Par));
7791 I : Interp_Index;
7792 It : Interp;
7793
7794 begin
7795 Get_First_Interp (Proc, I, It);
7796 while Present (It.Nam) loop
7797 if not Expr_Matches_In_Formal (It.Nam, Par) then
7798 return False;
7799 end if;
7800
7801 Get_Next_Interp (I, It);
7802 end loop;
7803 end;
7804
7805 -- All interpretations have a matching in-mode formal
7806
7807 return True;
7808
7809 else
7810 Proc := Entity (Name (Parent (Par)));
7811
7812 -- If this is an indirect call, get formals from
7813 -- designated type.
7814
7815 if Is_Access_Subprogram_Type (Etype (Proc)) then
7816 Proc := Designated_Type (Etype (Proc));
7817 end if;
7818 end if;
7819
7820 return Expr_Matches_In_Formal (Proc, Par);
7821 end;
7822
7823 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
7824 return False;
7825
7826 -- If the indexed component is a prefix it may be the first actual
7827 -- of a prefixed call. Retrieve the called entity, if any, and
7828 -- check its first formal. Determine if the context is a procedure
7829 -- or function call.
7830
7831 elsif Nkind (Parent (Par)) = N_Selected_Component then
7832 declare
7833 Sel : constant Node_Id := Selector_Name (Parent (Par));
7834 Nam : constant Entity_Id := Current_Entity (Sel);
7835
7836 begin
7837 if Present (Nam) and then Is_Overloadable (Nam) then
7838 if Nkind (Parent (Parent (Par))) =
7839 N_Procedure_Call_Statement
7840 then
7841 return False;
7842
7843 elsif Ekind (Nam) = E_Function
7844 and then Present (First_Formal (Nam))
7845 then
7846 return Ekind (First_Formal (Nam)) = E_In_Parameter;
7847 end if;
7848 end if;
7849 end;
7850
7851 elsif Nkind (Par) in N_Op then
7852 return True;
7853 end if;
7854
7855 Par := Parent (Par);
7856 end loop;
7857
7858 -- In all other cases, constant indexing is legal
7859
7860 return True;
7861 end Constant_Indexing_OK;
7862
7863 ----------------------------
7864 -- Expr_Matches_In_Formal --
7865 ----------------------------
7866
7867 function Expr_Matches_In_Formal
7868 (Subp : Entity_Id;
7869 Par : Node_Id) return Boolean
7870 is
7871 Actual : Node_Id;
7872 Formal : Node_Id;
7873
7874 begin
7875 Formal := First_Formal (Subp);
7876 Actual := First (Parameter_Associations ((Parent (Par))));
7877
7878 if Nkind (Par) /= N_Parameter_Association then
7879
7880 -- Match by position
7881
7882 while Present (Actual) and then Present (Formal) loop
7883 exit when Actual = Par;
7884 Next (Actual);
7885
7886 if Present (Formal) then
7887 Next_Formal (Formal);
7888
7889 -- Otherwise this is a parameter mismatch, the error is
7890 -- reported elsewhere, or else variable indexing is implied.
7891
7892 else
7893 return False;
7894 end if;
7895 end loop;
7896
7897 else
7898 -- Match by name
7899
7900 while Present (Formal) loop
7901 exit when Chars (Formal) = Chars (Selector_Name (Par));
7902 Next_Formal (Formal);
7903
7904 if No (Formal) then
7905 return False;
7906 end if;
7907 end loop;
7908 end if;
7909
7910 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
7911 end Expr_Matches_In_Formal;
7912
7913 ------------------------------
7914 -- Find_Indexing_Operations --
7915 ------------------------------
7916
7917 function Find_Indexing_Operations
7918 (T : Entity_Id;
7919 Nam : Name_Id;
7920 Is_Constant : Boolean) return Node_Id
7921 is
7922 procedure Inspect_Declarations
7923 (Typ : Entity_Id;
7924 Ref : in out Node_Id);
7925 -- Traverse the declarative list where type Typ resides and collect
7926 -- all suitable interpretations in node Ref.
7927
7928 procedure Inspect_Primitives
7929 (Typ : Entity_Id;
7930 Ref : in out Node_Id);
7931 -- Traverse the list of primitive operations of type Typ and collect
7932 -- all suitable interpretations in node Ref.
7933
7934 function Is_OK_Candidate
7935 (Subp_Id : Entity_Id;
7936 Typ : Entity_Id) return Boolean;
7937 -- Determine whether subprogram Subp_Id is a suitable indexing
7938 -- operation for type Typ. To qualify as such, the subprogram must
7939 -- be a function, have at least two parameters, and the type of the
7940 -- first parameter must be either Typ, or Typ'Class, or access [to
7941 -- constant] with designated type Typ or Typ'Class.
7942
7943 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
7944 -- Store subprogram Subp_Id as an interpretation in node Ref
7945
7946 --------------------------
7947 -- Inspect_Declarations --
7948 --------------------------
7949
7950 procedure Inspect_Declarations
7951 (Typ : Entity_Id;
7952 Ref : in out Node_Id)
7953 is
7954 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
7955 Decl : Node_Id;
7956 Subp_Id : Entity_Id;
7957
7958 begin
7959 -- Ensure that the routine is not called with itypes, which lack a
7960 -- declarative node.
7961
7962 pragma Assert (Present (Typ_Decl));
7963 pragma Assert (Is_List_Member (Typ_Decl));
7964
7965 Decl := First (List_Containing (Typ_Decl));
7966 while Present (Decl) loop
7967 if Nkind (Decl) = N_Subprogram_Declaration then
7968 Subp_Id := Defining_Entity (Decl);
7969
7970 if Is_OK_Candidate (Subp_Id, Typ) then
7971 Record_Interp (Subp_Id, Ref);
7972 end if;
7973 end if;
7974
7975 Next (Decl);
7976 end loop;
7977 end Inspect_Declarations;
7978
7979 ------------------------
7980 -- Inspect_Primitives --
7981 ------------------------
7982
7983 procedure Inspect_Primitives
7984 (Typ : Entity_Id;
7985 Ref : in out Node_Id)
7986 is
7987 Prim_Elmt : Elmt_Id;
7988 Prim_Id : Entity_Id;
7989
7990 begin
7991 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
7992 while Present (Prim_Elmt) loop
7993 Prim_Id := Node (Prim_Elmt);
7994
7995 if Is_OK_Candidate (Prim_Id, Typ) then
7996 Record_Interp (Prim_Id, Ref);
7997 end if;
7998
7999 Next_Elmt (Prim_Elmt);
8000 end loop;
8001 end Inspect_Primitives;
8002
8003 ---------------------
8004 -- Is_OK_Candidate --
8005 ---------------------
8006
8007 function Is_OK_Candidate
8008 (Subp_Id : Entity_Id;
8009 Typ : Entity_Id) return Boolean
8010 is
8011 Formal : Entity_Id;
8012 Formal_Typ : Entity_Id;
8013 Param_Typ : Node_Id;
8014
8015 begin
8016 -- To classify as a suitable candidate, the subprogram must be a
8017 -- function whose name matches the argument of aspect Constant or
8018 -- Variable_Indexing.
8019
8020 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
8021 Formal := First_Formal (Subp_Id);
8022
8023 -- The candidate requires at least two parameters
8024
8025 if Present (Formal) and then Present (Next_Formal (Formal)) then
8026 Formal_Typ := Empty;
8027 Param_Typ := Parameter_Type (Parent (Formal));
8028
8029 -- Use the designated type when the first parameter is of an
8030 -- access type.
8031
8032 if Nkind (Param_Typ) = N_Access_Definition
8033 and then Present (Subtype_Mark (Param_Typ))
8034 then
8035 -- When the context is a constant indexing, the access
8036 -- definition must be access-to-constant. This does not
8037 -- apply to variable indexing.
8038
8039 if not Is_Constant
8040 or else Constant_Present (Param_Typ)
8041 then
8042 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
8043 end if;
8044
8045 -- Otherwise use the parameter type
8046
8047 else
8048 Formal_Typ := Etype (Param_Typ);
8049 end if;
8050
8051 if Present (Formal_Typ) then
8052
8053 -- Use the specific type when the parameter type is
8054 -- class-wide.
8055
8056 if Is_Class_Wide_Type (Formal_Typ) then
8057 Formal_Typ := Etype (Base_Type (Formal_Typ));
8058 end if;
8059
8060 -- Use the full view when the parameter type is private
8061 -- or incomplete.
8062
8063 if Is_Incomplete_Or_Private_Type (Formal_Typ)
8064 and then Present (Full_View (Formal_Typ))
8065 then
8066 Formal_Typ := Full_View (Formal_Typ);
8067 end if;
8068
8069 -- The type of the first parameter must denote the type
8070 -- of the container or acts as its ancestor type.
8071
8072 return
8073 Formal_Typ = Typ
8074 or else Is_Ancestor (Formal_Typ, Typ);
8075 end if;
8076 end if;
8077 end if;
8078
8079 return False;
8080 end Is_OK_Candidate;
8081
8082 -------------------
8083 -- Record_Interp --
8084 -------------------
8085
8086 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8087 begin
8088 if Present (Ref) then
8089 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8090
8091 -- Otherwise this is the first interpretation. Create a reference
8092 -- where all remaining interpretations will be collected.
8093
8094 else
8095 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8096 end if;
8097 end Record_Interp;
8098
8099 -- Local variables
8100
8101 Ref : Node_Id;
8102 Typ : Entity_Id;
8103
8104 -- Start of processing for Find_Indexing_Operations
8105
8106 begin
8107 Typ := T;
8108
8109 -- Use the specific type when the parameter type is class-wide
8110
8111 if Is_Class_Wide_Type (Typ) then
8112 Typ := Root_Type (Typ);
8113 end if;
8114
8115 Ref := Empty;
8116 Typ := Underlying_Type (Base_Type (Typ));
8117
8118 Inspect_Primitives (Typ, Ref);
8119
8120 -- Now look for explicit declarations of an indexing operation.
8121 -- If the type is private the operation may be declared in the
8122 -- visible part that contains the partial view.
8123
8124 if Is_Private_Type (T) then
8125 Inspect_Declarations (T, Ref);
8126 end if;
8127
8128 Inspect_Declarations (Typ, Ref);
8129
8130 return Ref;
8131 end Find_Indexing_Operations;
8132
8133 -- Local variables
8134
8135 Loc : constant Source_Ptr := Sloc (N);
8136 Assoc : List_Id;
8137 C_Type : Entity_Id;
8138 Func : Entity_Id;
8139 Func_Name : Node_Id;
8140 Indexing : Node_Id;
8141
8142 Is_Constant_Indexing : Boolean := False;
8143 -- This flag reflects the nature of the container indexing. Note that
8144 -- the context may be suited for constant indexing, but the type may
8145 -- lack a Constant_Indexing annotation.
8146
8147 -- Start of processing for Try_Container_Indexing
8148
8149 begin
8150 -- Node may have been analyzed already when testing for a prefixed
8151 -- call, in which case do not redo analysis.
8152
8153 if Present (Generalized_Indexing (N)) then
8154 return True;
8155 end if;
8156
8157 C_Type := Pref_Typ;
8158
8159 -- If indexing a class-wide container, obtain indexing primitive from
8160 -- specific type.
8161
8162 if Is_Class_Wide_Type (C_Type) then
8163 C_Type := Etype (Base_Type (C_Type));
8164 end if;
8165
8166 -- Check whether the type has a specified indexing aspect
8167
8168 Func_Name := Empty;
8169
8170 -- The context is suitable for constant indexing, so obtain the name of
8171 -- the indexing function from aspect Constant_Indexing.
8172
8173 if Constant_Indexing_OK then
8174 Func_Name :=
8175 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
8176 end if;
8177
8178 if Present (Func_Name) then
8179 Is_Constant_Indexing := True;
8180
8181 -- Otherwise attempt variable indexing
8182
8183 else
8184 Func_Name :=
8185 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
8186 end if;
8187
8188 -- The type is not subject to either form of indexing, therefore the
8189 -- indexed component does not denote container indexing. If this is a
8190 -- true error, it is diagnosed by the caller.
8191
8192 if No (Func_Name) then
8193
8194 -- The prefix itself may be an indexing of a container. Rewrite it
8195 -- as such and retry.
8196
8197 if Has_Implicit_Dereference (Pref_Typ) then
8198 Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ));
8199 return Try_Container_Indexing (N, Prefix, Exprs);
8200
8201 -- Otherwise this is definitely not container indexing
8202
8203 else
8204 return False;
8205 end if;
8206
8207 -- If the container type is derived from another container type, the
8208 -- value of the inherited aspect is the Reference operation declared
8209 -- for the parent type.
8210
8211 -- However, Reference is also a primitive operation of the type, and the
8212 -- inherited operation has a different signature. We retrieve the right
8213 -- ones (the function may be overloaded) from the list of primitive
8214 -- operations of the derived type.
8215
8216 -- Note that predefined containers are typically all derived from one of
8217 -- the Controlled types. The code below is motivated by containers that
8218 -- are derived from other types with a Reference aspect.
8219
8220 elsif Is_Derived_Type (C_Type)
8221 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
8222 then
8223 Func_Name :=
8224 Find_Indexing_Operations
8225 (T => C_Type,
8226 Nam => Chars (Func_Name),
8227 Is_Constant => Is_Constant_Indexing);
8228 end if;
8229
8230 Assoc := New_List (Relocate_Node (Prefix));
8231
8232 -- A generalized indexing may have nore than one index expression, so
8233 -- transfer all of them to the argument list to be used in the call.
8234 -- Note that there may be named associations, in which case the node
8235 -- was rewritten earlier as a call, and has been transformed back into
8236 -- an indexed expression to share the following processing.
8237
8238 -- The generalized indexing node is the one on which analysis and
8239 -- resolution take place. Before expansion the original node is replaced
8240 -- with the generalized indexing node, which is a call, possibly with a
8241 -- dereference operation.
8242
8243 if Comes_From_Source (N) then
8244 Check_Compiler_Unit ("generalized indexing", N);
8245 end if;
8246
8247 -- Create argument list for function call that represents generalized
8248 -- indexing. Note that indices (i.e. actuals) may themselves be
8249 -- overloaded.
8250
8251 declare
8252 Arg : Node_Id;
8253 New_Arg : Node_Id;
8254
8255 begin
8256 Arg := First (Exprs);
8257 while Present (Arg) loop
8258 New_Arg := Relocate_Node (Arg);
8259
8260 -- The arguments can be parameter associations, in which case the
8261 -- explicit actual parameter carries the overloadings.
8262
8263 if Nkind (New_Arg) /= N_Parameter_Association then
8264 Save_Interps (Arg, New_Arg);
8265 end if;
8266
8267 Append (New_Arg, Assoc);
8268 Next (Arg);
8269 end loop;
8270 end;
8271
8272 if not Is_Overloaded (Func_Name) then
8273 Func := Entity (Func_Name);
8274
8275 Indexing :=
8276 Make_Function_Call (Loc,
8277 Name => New_Occurrence_Of (Func, Loc),
8278 Parameter_Associations => Assoc);
8279
8280 Set_Parent (Indexing, Parent (N));
8281 Set_Generalized_Indexing (N, Indexing);
8282 Analyze (Indexing);
8283 Set_Etype (N, Etype (Indexing));
8284
8285 -- If the return type of the indexing function is a reference type,
8286 -- add the dereference as a possible interpretation. Note that the
8287 -- indexing aspect may be a function that returns the element type
8288 -- with no intervening implicit dereference, and that the reference
8289 -- discriminant is not the first discriminant.
8290
8291 if Has_Discriminants (Etype (Func)) then
8292 Check_Implicit_Dereference (N, Etype (Func));
8293 end if;
8294
8295 else
8296 -- If there are multiple indexing functions, build a function call
8297 -- and analyze it for each of the possible interpretations.
8298
8299 Indexing :=
8300 Make_Function_Call (Loc,
8301 Name =>
8302 Make_Identifier (Loc, Chars (Func_Name)),
8303 Parameter_Associations => Assoc);
8304 Set_Parent (Indexing, Parent (N));
8305 Set_Generalized_Indexing (N, Indexing);
8306 Set_Etype (N, Any_Type);
8307 Set_Etype (Name (Indexing), Any_Type);
8308
8309 declare
8310 I : Interp_Index;
8311 It : Interp;
8312 Success : Boolean;
8313
8314 begin
8315 Get_First_Interp (Func_Name, I, It);
8316 Set_Etype (Indexing, Any_Type);
8317
8318 -- Analyze each candidate function with the given actuals
8319
8320 while Present (It.Nam) loop
8321 Analyze_One_Call (Indexing, It.Nam, False, Success);
8322 Get_Next_Interp (I, It);
8323 end loop;
8324
8325 -- If there are several successful candidates, resolution will
8326 -- be by result. Mark the interpretations of the function name
8327 -- itself.
8328
8329 if Is_Overloaded (Indexing) then
8330 Get_First_Interp (Indexing, I, It);
8331
8332 while Present (It.Nam) loop
8333 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
8334 Get_Next_Interp (I, It);
8335 end loop;
8336
8337 else
8338 Set_Etype (Name (Indexing), Etype (Indexing));
8339 end if;
8340
8341 -- Now add the candidate interpretations to the indexing node
8342 -- itself, to be replaced later by the function call.
8343
8344 if Is_Overloaded (Name (Indexing)) then
8345 Get_First_Interp (Name (Indexing), I, It);
8346
8347 while Present (It.Nam) loop
8348 Add_One_Interp (N, It.Nam, It.Typ);
8349
8350 -- Add dereference interpretation if the result type has
8351 -- implicit reference discriminants.
8352
8353 if Has_Discriminants (Etype (It.Nam)) then
8354 Check_Implicit_Dereference (N, Etype (It.Nam));
8355 end if;
8356
8357 Get_Next_Interp (I, It);
8358 end loop;
8359
8360 else
8361 Set_Etype (N, Etype (Name (Indexing)));
8362 if Has_Discriminants (Etype (N)) then
8363 Check_Implicit_Dereference (N, Etype (N));
8364 end if;
8365 end if;
8366 end;
8367 end if;
8368
8369 if Etype (Indexing) = Any_Type then
8370 Error_Msg_NE
8371 ("container cannot be indexed with&", N, Etype (First (Exprs)));
8372 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
8373 end if;
8374
8375 return True;
8376 end Try_Container_Indexing;
8377
8378 -----------------------
8379 -- Try_Indirect_Call --
8380 -----------------------
8381
8382 function Try_Indirect_Call
8383 (N : Node_Id;
8384 Nam : Entity_Id;
8385 Typ : Entity_Id) return Boolean
8386 is
8387 Actual : Node_Id;
8388 Formal : Entity_Id;
8389
8390 Call_OK : Boolean;
8391 pragma Warnings (Off, Call_OK);
8392
8393 begin
8394 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
8395
8396 Actual := First_Actual (N);
8397 Formal := First_Formal (Designated_Type (Typ));
8398 while Present (Actual) and then Present (Formal) loop
8399 if not Has_Compatible_Type (Actual, Etype (Formal)) then
8400 return False;
8401 end if;
8402
8403 Next (Actual);
8404 Next_Formal (Formal);
8405 end loop;
8406
8407 if No (Actual) and then No (Formal) then
8408 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
8409
8410 -- Nam is a candidate interpretation for the name in the call,
8411 -- if it is not an indirect call.
8412
8413 if not Is_Type (Nam)
8414 and then Is_Entity_Name (Name (N))
8415 then
8416 Set_Entity (Name (N), Nam);
8417 end if;
8418
8419 return True;
8420
8421 else
8422 return False;
8423 end if;
8424 end Try_Indirect_Call;
8425
8426 ----------------------
8427 -- Try_Indexed_Call --
8428 ----------------------
8429
8430 function Try_Indexed_Call
8431 (N : Node_Id;
8432 Nam : Entity_Id;
8433 Typ : Entity_Id;
8434 Skip_First : Boolean) return Boolean
8435 is
8436 Loc : constant Source_Ptr := Sloc (N);
8437 Actuals : constant List_Id := Parameter_Associations (N);
8438 Actual : Node_Id;
8439 Index : Entity_Id;
8440
8441 begin
8442 Actual := First (Actuals);
8443
8444 -- If the call was originally written in prefix form, skip the first
8445 -- actual, which is obviously not defaulted.
8446
8447 if Skip_First then
8448 Next (Actual);
8449 end if;
8450
8451 Index := First_Index (Typ);
8452 while Present (Actual) and then Present (Index) loop
8453
8454 -- If the parameter list has a named association, the expression
8455 -- is definitely a call and not an indexed component.
8456
8457 if Nkind (Actual) = N_Parameter_Association then
8458 return False;
8459 end if;
8460
8461 if Is_Entity_Name (Actual)
8462 and then Is_Type (Entity (Actual))
8463 and then No (Next (Actual))
8464 then
8465 -- A single actual that is a type name indicates a slice if the
8466 -- type is discrete, and an error otherwise.
8467
8468 if Is_Discrete_Type (Entity (Actual)) then
8469 Rewrite (N,
8470 Make_Slice (Loc,
8471 Prefix =>
8472 Make_Function_Call (Loc,
8473 Name => Relocate_Node (Name (N))),
8474 Discrete_Range =>
8475 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
8476
8477 Analyze (N);
8478
8479 else
8480 Error_Msg_N ("invalid use of type in expression", Actual);
8481 Set_Etype (N, Any_Type);
8482 end if;
8483
8484 return True;
8485
8486 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
8487 return False;
8488 end if;
8489
8490 Next (Actual);
8491 Next_Index (Index);
8492 end loop;
8493
8494 if No (Actual) and then No (Index) then
8495 Add_One_Interp (N, Nam, Component_Type (Typ));
8496
8497 -- Nam is a candidate interpretation for the name in the call,
8498 -- if it is not an indirect call.
8499
8500 if not Is_Type (Nam)
8501 and then Is_Entity_Name (Name (N))
8502 then
8503 Set_Entity (Name (N), Nam);
8504 end if;
8505
8506 return True;
8507 else
8508 return False;
8509 end if;
8510 end Try_Indexed_Call;
8511
8512 --------------------------
8513 -- Try_Object_Operation --
8514 --------------------------
8515
8516 function Try_Object_Operation
8517 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
8518 is
8519 K : constant Node_Kind := Nkind (Parent (N));
8520 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
8521 Loc : constant Source_Ptr := Sloc (N);
8522 Obj : constant Node_Id := Prefix (N);
8523
8524 Subprog : constant Node_Id :=
8525 Make_Identifier (Sloc (Selector_Name (N)),
8526 Chars => Chars (Selector_Name (N)));
8527 -- Identifier on which possible interpretations will be collected
8528
8529 Report_Error : Boolean := False;
8530 -- If no candidate interpretation matches the context, redo analysis
8531 -- with Report_Error True to provide additional information.
8532
8533 Actual : Node_Id;
8534 Candidate : Entity_Id := Empty;
8535 New_Call_Node : Node_Id := Empty;
8536 Node_To_Replace : Node_Id;
8537 Obj_Type : Entity_Id := Etype (Obj);
8538 Success : Boolean := False;
8539
8540 procedure Complete_Object_Operation
8541 (Call_Node : Node_Id;
8542 Node_To_Replace : Node_Id);
8543 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
8544 -- Call_Node, insert the object (or its dereference) as the first actual
8545 -- in the call, and complete the analysis of the call.
8546
8547 procedure Report_Ambiguity (Op : Entity_Id);
8548 -- If a prefixed procedure call is ambiguous, indicate whether the call
8549 -- includes an implicit dereference or an implicit 'Access.
8550
8551 procedure Transform_Object_Operation
8552 (Call_Node : out Node_Id;
8553 Node_To_Replace : out Node_Id);
8554 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
8555 -- Call_Node is the resulting subprogram call, Node_To_Replace is
8556 -- either N or the parent of N, and Subprog is a reference to the
8557 -- subprogram we are trying to match.
8558
8559 function Try_Class_Wide_Operation
8560 (Call_Node : Node_Id;
8561 Node_To_Replace : Node_Id) return Boolean;
8562 -- Traverse all ancestor types looking for a class-wide subprogram for
8563 -- which the current operation is a valid non-dispatching call.
8564
8565 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
8566 -- If prefix is overloaded, its interpretation may include different
8567 -- tagged types, and we must examine the primitive operations and the
8568 -- class-wide operations of each in order to find candidate
8569 -- interpretations for the call as a whole.
8570
8571 function Try_Primitive_Operation
8572 (Call_Node : Node_Id;
8573 Node_To_Replace : Node_Id) return Boolean;
8574 -- Traverse the list of primitive subprograms looking for a dispatching
8575 -- operation for which the current node is a valid call.
8576
8577 function Valid_Candidate
8578 (Success : Boolean;
8579 Call : Node_Id;
8580 Subp : Entity_Id) return Entity_Id;
8581 -- If the subprogram is a valid interpretation, record it, and add to
8582 -- the list of interpretations of Subprog. Otherwise return Empty.
8583
8584 -------------------------------
8585 -- Complete_Object_Operation --
8586 -------------------------------
8587
8588 procedure Complete_Object_Operation
8589 (Call_Node : Node_Id;
8590 Node_To_Replace : Node_Id)
8591 is
8592 Control : constant Entity_Id := First_Formal (Entity (Subprog));
8593 Formal_Type : constant Entity_Id := Etype (Control);
8594 First_Actual : Node_Id;
8595
8596 begin
8597 -- Place the name of the operation, with its interpretations,
8598 -- on the rewritten call.
8599
8600 Set_Name (Call_Node, Subprog);
8601
8602 First_Actual := First (Parameter_Associations (Call_Node));
8603
8604 -- For cross-reference purposes, treat the new node as being in the
8605 -- source if the original one is. Set entity and type, even though
8606 -- they may be overwritten during resolution if overloaded.
8607
8608 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
8609 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
8610
8611 if Nkind (N) = N_Selected_Component
8612 and then not Inside_A_Generic
8613 then
8614 Set_Entity (Selector_Name (N), Entity (Subprog));
8615 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
8616 end if;
8617
8618 -- If need be, rewrite first actual as an explicit dereference. If
8619 -- the call is overloaded, the rewriting can only be done once the
8620 -- primitive operation is identified.
8621
8622 if Is_Overloaded (Subprog) then
8623
8624 -- The prefix itself may be overloaded, and its interpretations
8625 -- must be propagated to the new actual in the call.
8626
8627 if Is_Overloaded (Obj) then
8628 Save_Interps (Obj, First_Actual);
8629 end if;
8630
8631 Rewrite (First_Actual, Obj);
8632
8633 elsif not Is_Access_Type (Formal_Type)
8634 and then Is_Access_Type (Etype (Obj))
8635 then
8636 Rewrite (First_Actual,
8637 Make_Explicit_Dereference (Sloc (Obj), Obj));
8638 Analyze (First_Actual);
8639
8640 -- If we need to introduce an explicit dereference, verify that
8641 -- the resulting actual is compatible with the mode of the formal.
8642
8643 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
8644 and then Is_Access_Constant (Etype (Obj))
8645 then
8646 Error_Msg_NE
8647 ("expect variable in call to&", Prefix (N), Entity (Subprog));
8648 end if;
8649
8650 -- Conversely, if the formal is an access parameter and the object is
8651 -- not an access type or a reference type (i.e. a type with the
8652 -- Implicit_Dereference aspect specified), replace the actual with a
8653 -- 'Access reference. Its analysis will check that the object is
8654 -- aliased.
8655
8656 elsif Is_Access_Type (Formal_Type)
8657 and then not Is_Access_Type (Etype (Obj))
8658 and then
8659 (not Has_Implicit_Dereference (Etype (Obj))
8660 or else
8661 not Is_Access_Type (Designated_Type (Etype
8662 (Get_Reference_Discriminant (Etype (Obj))))))
8663 then
8664 -- A special case: A.all'Access is illegal if A is an access to a
8665 -- constant and the context requires an access to a variable.
8666
8667 if not Is_Access_Constant (Formal_Type) then
8668 if (Nkind (Obj) = N_Explicit_Dereference
8669 and then Is_Access_Constant (Etype (Prefix (Obj))))
8670 or else not Is_Variable (Obj)
8671 then
8672 Error_Msg_NE
8673 ("actual for & must be a variable", Obj, Control);
8674 end if;
8675 end if;
8676
8677 Rewrite (First_Actual,
8678 Make_Attribute_Reference (Loc,
8679 Attribute_Name => Name_Access,
8680 Prefix => Relocate_Node (Obj)));
8681
8682 -- If the object is not overloaded verify that taking access of
8683 -- it is legal. Otherwise check is made during resolution.
8684
8685 if not Is_Overloaded (Obj)
8686 and then not Is_Aliased_View (Obj)
8687 then
8688 Error_Msg_NE
8689 ("object in prefixed call to & must be aliased "
8690 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
8691 end if;
8692
8693 Analyze (First_Actual);
8694
8695 else
8696 if Is_Overloaded (Obj) then
8697 Save_Interps (Obj, First_Actual);
8698 end if;
8699
8700 Rewrite (First_Actual, Obj);
8701 end if;
8702
8703 -- The operation is obtained from the dispatch table and not by
8704 -- visibility, and may be declared in a unit that is not explicitly
8705 -- referenced in the source, but is nevertheless required in the
8706 -- context of the current unit. Indicate that operation and its scope
8707 -- are referenced, to prevent spurious and misleading warnings. If
8708 -- the operation is overloaded, all primitives are in the same scope
8709 -- and we can use any of them.
8710
8711 Set_Referenced (Entity (Subprog), True);
8712 Set_Referenced (Scope (Entity (Subprog)), True);
8713
8714 Rewrite (Node_To_Replace, Call_Node);
8715
8716 -- Propagate the interpretations collected in subprog to the new
8717 -- function call node, to be resolved from context.
8718
8719 if Is_Overloaded (Subprog) then
8720 Save_Interps (Subprog, Node_To_Replace);
8721
8722 else
8723 -- The type of the subprogram may be a limited view obtained
8724 -- transitively from another unit. If full view is available,
8725 -- use it to analyze call. If there is no nonlimited view, then
8726 -- this is diagnosed when analyzing the rewritten call.
8727
8728 declare
8729 T : constant Entity_Id := Etype (Subprog);
8730 begin
8731 if From_Limited_With (T) then
8732 Set_Etype (Entity (Subprog), Available_View (T));
8733 end if;
8734 end;
8735
8736 Analyze (Node_To_Replace);
8737
8738 -- If the operation has been rewritten into a call, which may get
8739 -- subsequently an explicit dereference, preserve the type on the
8740 -- original node (selected component or indexed component) for
8741 -- subsequent legality tests, e.g. Is_Variable. which examines
8742 -- the original node.
8743
8744 if Nkind (Node_To_Replace) = N_Function_Call then
8745 Set_Etype
8746 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
8747 end if;
8748 end if;
8749 end Complete_Object_Operation;
8750
8751 ----------------------
8752 -- Report_Ambiguity --
8753 ----------------------
8754
8755 procedure Report_Ambiguity (Op : Entity_Id) is
8756 Access_Actual : constant Boolean :=
8757 Is_Access_Type (Etype (Prefix (N)));
8758 Access_Formal : Boolean := False;
8759
8760 begin
8761 Error_Msg_Sloc := Sloc (Op);
8762
8763 if Present (First_Formal (Op)) then
8764 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
8765 end if;
8766
8767 if Access_Formal and then not Access_Actual then
8768 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8769 Error_Msg_N
8770 ("\possible interpretation "
8771 & "(inherited, with implicit 'Access) #", N);
8772 else
8773 Error_Msg_N
8774 ("\possible interpretation (with implicit 'Access) #", N);
8775 end if;
8776
8777 elsif not Access_Formal and then Access_Actual then
8778 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8779 Error_Msg_N
8780 ("\possible interpretation "
8781 & "(inherited, with implicit dereference) #", N);
8782 else
8783 Error_Msg_N
8784 ("\possible interpretation (with implicit dereference) #", N);
8785 end if;
8786
8787 else
8788 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
8789 Error_Msg_N ("\possible interpretation (inherited)#", N);
8790 else
8791 Error_Msg_N -- CODEFIX
8792 ("\possible interpretation#", N);
8793 end if;
8794 end if;
8795 end Report_Ambiguity;
8796
8797 --------------------------------
8798 -- Transform_Object_Operation --
8799 --------------------------------
8800
8801 procedure Transform_Object_Operation
8802 (Call_Node : out Node_Id;
8803 Node_To_Replace : out Node_Id)
8804 is
8805 Dummy : constant Node_Id := New_Copy (Obj);
8806 -- Placeholder used as a first parameter in the call, replaced
8807 -- eventually by the proper object.
8808
8809 Parent_Node : constant Node_Id := Parent (N);
8810
8811 Actual : Node_Id;
8812 Actuals : List_Id;
8813
8814 begin
8815 -- Obj may already have been rewritten if it involves an implicit
8816 -- dereference (e.g. if it is an access to a limited view). Preserve
8817 -- a link to the original node for ASIS use.
8818
8819 if not Comes_From_Source (Obj) then
8820 Set_Original_Node (Dummy, Original_Node (Obj));
8821 end if;
8822
8823 -- Common case covering 1) Call to a procedure and 2) Call to a
8824 -- function that has some additional actuals.
8825
8826 if Nkind (Parent_Node) in N_Subprogram_Call
8827
8828 -- N is a selected component node containing the name of the
8829 -- subprogram. If N is not the name of the parent node we must
8830 -- not replace the parent node by the new construct. This case
8831 -- occurs when N is a parameterless call to a subprogram that
8832 -- is an actual parameter of a call to another subprogram. For
8833 -- example:
8834 -- Some_Subprogram (..., Obj.Operation, ...)
8835
8836 and then Name (Parent_Node) = N
8837 then
8838 Node_To_Replace := Parent_Node;
8839
8840 Actuals := Parameter_Associations (Parent_Node);
8841
8842 if Present (Actuals) then
8843 Prepend (Dummy, Actuals);
8844 else
8845 Actuals := New_List (Dummy);
8846 end if;
8847
8848 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
8849 Call_Node :=
8850 Make_Procedure_Call_Statement (Loc,
8851 Name => New_Copy (Subprog),
8852 Parameter_Associations => Actuals);
8853
8854 else
8855 Call_Node :=
8856 Make_Function_Call (Loc,
8857 Name => New_Copy (Subprog),
8858 Parameter_Associations => Actuals);
8859 end if;
8860
8861 -- Before analysis, a function call appears as an indexed component
8862 -- if there are no named associations.
8863
8864 elsif Nkind (Parent_Node) = N_Indexed_Component
8865 and then N = Prefix (Parent_Node)
8866 then
8867 Node_To_Replace := Parent_Node;
8868 Actuals := Expressions (Parent_Node);
8869
8870 Actual := First (Actuals);
8871 while Present (Actual) loop
8872 Analyze (Actual);
8873 Next (Actual);
8874 end loop;
8875
8876 Prepend (Dummy, Actuals);
8877
8878 Call_Node :=
8879 Make_Function_Call (Loc,
8880 Name => New_Copy (Subprog),
8881 Parameter_Associations => Actuals);
8882
8883 -- Parameterless call: Obj.F is rewritten as F (Obj)
8884
8885 else
8886 Node_To_Replace := N;
8887
8888 Call_Node :=
8889 Make_Function_Call (Loc,
8890 Name => New_Copy (Subprog),
8891 Parameter_Associations => New_List (Dummy));
8892 end if;
8893 end Transform_Object_Operation;
8894
8895 ------------------------------
8896 -- Try_Class_Wide_Operation --
8897 ------------------------------
8898
8899 function Try_Class_Wide_Operation
8900 (Call_Node : Node_Id;
8901 Node_To_Replace : Node_Id) return Boolean
8902 is
8903 Anc_Type : Entity_Id;
8904 Matching_Op : Entity_Id := Empty;
8905 Error : Boolean;
8906
8907 procedure Traverse_Homonyms
8908 (Anc_Type : Entity_Id;
8909 Error : out Boolean);
8910 -- Traverse the homonym chain of the subprogram searching for those
8911 -- homonyms whose first formal has the Anc_Type's class-wide type,
8912 -- or an anonymous access type designating the class-wide type. If
8913 -- an ambiguity is detected, then Error is set to True.
8914
8915 procedure Traverse_Interfaces
8916 (Anc_Type : Entity_Id;
8917 Error : out Boolean);
8918 -- Traverse the list of interfaces, if any, associated with Anc_Type
8919 -- and search for acceptable class-wide homonyms associated with each
8920 -- interface. If an ambiguity is detected, then Error is set to True.
8921
8922 -----------------------
8923 -- Traverse_Homonyms --
8924 -----------------------
8925
8926 procedure Traverse_Homonyms
8927 (Anc_Type : Entity_Id;
8928 Error : out Boolean)
8929 is
8930 function First_Formal_Match
8931 (Subp_Id : Entity_Id;
8932 Typ : Entity_Id) return Boolean;
8933 -- Predicate to verify that the first foramal of class-wide
8934 -- subprogram Subp_Id matches type Typ of the prefix.
8935
8936 ------------------------
8937 -- First_Formal_Match --
8938 ------------------------
8939
8940 function First_Formal_Match
8941 (Subp_Id : Entity_Id;
8942 Typ : Entity_Id) return Boolean
8943 is
8944 Ctrl : constant Entity_Id := First_Formal (Subp_Id);
8945
8946 begin
8947 return
8948 Present (Ctrl)
8949 and then
8950 (Base_Type (Etype (Ctrl)) = Typ
8951 or else
8952 (Ekind (Etype (Ctrl)) = E_Anonymous_Access_Type
8953 and then
8954 Base_Type (Designated_Type (Etype (Ctrl))) =
8955 Typ));
8956 end First_Formal_Match;
8957
8958 -- Local variables
8959
8960 CW_Typ : constant Entity_Id := Class_Wide_Type (Anc_Type);
8961
8962 Candidate : Entity_Id;
8963 -- If homonym is a renaming, examine the renamed program
8964
8965 Hom : Entity_Id;
8966 Hom_Ref : Node_Id;
8967 Success : Boolean;
8968
8969 -- Start of processing for Traverse_Homonyms
8970
8971 begin
8972 Error := False;
8973
8974 -- Find a non-hidden operation whose first parameter is of the
8975 -- class-wide type, a subtype thereof, or an anonymous access
8976 -- to same. If in an instance, the operation can be considered
8977 -- even if hidden (it may be hidden because the instantiation
8978 -- is expanded after the containing package has been analyzed).
8979 -- If the subprogram is a generic actual in an enclosing instance,
8980 -- it appears as a renaming that is a candidate interpretation as
8981 -- well.
8982
8983 Hom := Current_Entity (Subprog);
8984 while Present (Hom) loop
8985 if Ekind_In (Hom, E_Procedure, E_Function)
8986 and then Present (Renamed_Entity (Hom))
8987 and then Is_Generic_Actual_Subprogram (Hom)
8988 and then In_Open_Scopes (Scope (Hom))
8989 then
8990 Candidate := Renamed_Entity (Hom);
8991 else
8992 Candidate := Hom;
8993 end if;
8994
8995 if Ekind_In (Candidate, E_Function, E_Procedure)
8996 and then (not Is_Hidden (Candidate) or else In_Instance)
8997 and then Scope (Candidate) = Scope (Base_Type (Anc_Type))
8998 and then First_Formal_Match (Candidate, CW_Typ)
8999 then
9000 -- If the context is a procedure call, ignore functions
9001 -- in the name of the call.
9002
9003 if Ekind (Candidate) = E_Function
9004 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
9005 and then N = Name (Parent (N))
9006 then
9007 goto Next_Hom;
9008
9009 -- If the context is a function call, ignore procedures
9010 -- in the name of the call.
9011
9012 elsif Ekind (Candidate) = E_Procedure
9013 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
9014 then
9015 goto Next_Hom;
9016 end if;
9017
9018 Set_Etype (Call_Node, Any_Type);
9019 Set_Is_Overloaded (Call_Node, False);
9020 Success := False;
9021
9022 if No (Matching_Op) then
9023 Hom_Ref := New_Occurrence_Of (Candidate, Sloc (Subprog));
9024
9025 Set_Etype (Call_Node, Any_Type);
9026 Set_Name (Call_Node, Hom_Ref);
9027 Set_Parent (Call_Node, Parent (Node_To_Replace));
9028
9029 Analyze_One_Call
9030 (N => Call_Node,
9031 Nam => Candidate,
9032 Report => Report_Error,
9033 Success => Success,
9034 Skip_First => True);
9035
9036 Matching_Op :=
9037 Valid_Candidate (Success, Call_Node, Candidate);
9038
9039 else
9040 Analyze_One_Call
9041 (N => Call_Node,
9042 Nam => Candidate,
9043 Report => Report_Error,
9044 Success => Success,
9045 Skip_First => True);
9046
9047 -- The same operation may be encountered on two homonym
9048 -- traversals, before and after looking at interfaces.
9049 -- Check for this case before reporting a real ambiguity.
9050
9051 if Present
9052 (Valid_Candidate (Success, Call_Node, Candidate))
9053 and then Nkind (Call_Node) /= N_Function_Call
9054 and then Candidate /= Matching_Op
9055 then
9056 Error_Msg_NE ("ambiguous call to&", N, Hom);
9057 Report_Ambiguity (Matching_Op);
9058 Report_Ambiguity (Hom);
9059 Error := True;
9060 return;
9061 end if;
9062 end if;
9063 end if;
9064
9065 <<Next_Hom>>
9066 Hom := Homonym (Hom);
9067 end loop;
9068 end Traverse_Homonyms;
9069
9070 -------------------------
9071 -- Traverse_Interfaces --
9072 -------------------------
9073
9074 procedure Traverse_Interfaces
9075 (Anc_Type : Entity_Id;
9076 Error : out Boolean)
9077 is
9078 Intface_List : constant List_Id :=
9079 Abstract_Interface_List (Anc_Type);
9080 Intface : Node_Id;
9081
9082 begin
9083 Error := False;
9084
9085 if Is_Non_Empty_List (Intface_List) then
9086 Intface := First (Intface_List);
9087 while Present (Intface) loop
9088
9089 -- Look for acceptable class-wide homonyms associated with
9090 -- the interface.
9091
9092 Traverse_Homonyms (Etype (Intface), Error);
9093
9094 if Error then
9095 return;
9096 end if;
9097
9098 -- Continue the search by looking at each of the interface's
9099 -- associated interface ancestors.
9100
9101 Traverse_Interfaces (Etype (Intface), Error);
9102
9103 if Error then
9104 return;
9105 end if;
9106
9107 Next (Intface);
9108 end loop;
9109 end if;
9110 end Traverse_Interfaces;
9111
9112 -- Start of processing for Try_Class_Wide_Operation
9113
9114 begin
9115 -- If we are searching only for conflicting class-wide subprograms
9116 -- then initialize directly Matching_Op with the target entity.
9117
9118 if CW_Test_Only then
9119 Matching_Op := Entity (Selector_Name (N));
9120 end if;
9121
9122 -- Loop through ancestor types (including interfaces), traversing
9123 -- the homonym chain of the subprogram, trying out those homonyms
9124 -- whose first formal has the class-wide type of the ancestor, or
9125 -- an anonymous access type designating the class-wide type.
9126
9127 Anc_Type := Obj_Type;
9128 loop
9129 -- Look for a match among homonyms associated with the ancestor
9130
9131 Traverse_Homonyms (Anc_Type, Error);
9132
9133 if Error then
9134 return True;
9135 end if;
9136
9137 -- Continue the search for matches among homonyms associated with
9138 -- any interfaces implemented by the ancestor.
9139
9140 Traverse_Interfaces (Anc_Type, Error);
9141
9142 if Error then
9143 return True;
9144 end if;
9145
9146 exit when Etype (Anc_Type) = Anc_Type;
9147 Anc_Type := Etype (Anc_Type);
9148 end loop;
9149
9150 if Present (Matching_Op) then
9151 Set_Etype (Call_Node, Etype (Matching_Op));
9152 end if;
9153
9154 return Present (Matching_Op);
9155 end Try_Class_Wide_Operation;
9156
9157 -----------------------------------
9158 -- Try_One_Prefix_Interpretation --
9159 -----------------------------------
9160
9161 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
9162 Prev_Obj_Type : constant Entity_Id := Obj_Type;
9163 -- If the interpretation does not have a valid candidate type,
9164 -- preserve current value of Obj_Type for subsequent errors.
9165
9166 begin
9167 Obj_Type := T;
9168
9169 if Is_Access_Type (Obj_Type) then
9170 Obj_Type := Designated_Type (Obj_Type);
9171 end if;
9172
9173 if Ekind_In (Obj_Type, E_Private_Subtype,
9174 E_Record_Subtype_With_Private)
9175 then
9176 Obj_Type := Base_Type (Obj_Type);
9177 end if;
9178
9179 if Is_Class_Wide_Type (Obj_Type) then
9180 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9181 end if;
9182
9183 -- The type may have be obtained through a limited_with clause,
9184 -- in which case the primitive operations are available on its
9185 -- nonlimited view. If still incomplete, retrieve full view.
9186
9187 if Ekind (Obj_Type) = E_Incomplete_Type
9188 and then From_Limited_With (Obj_Type)
9189 and then Has_Non_Limited_View (Obj_Type)
9190 then
9191 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
9192 end if;
9193
9194 -- If the object is not tagged, or the type is still an incomplete
9195 -- type, this is not a prefixed call. Restore the previous type as
9196 -- the current one is not a legal candidate.
9197
9198 if not Is_Tagged_Type (Obj_Type)
9199 or else Is_Incomplete_Type (Obj_Type)
9200 then
9201 Obj_Type := Prev_Obj_Type;
9202 return;
9203 end if;
9204
9205 declare
9206 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
9207 Ignore : Boolean;
9208 Prim_Result : Boolean := False;
9209
9210 begin
9211 if not CW_Test_Only then
9212 Prim_Result :=
9213 Try_Primitive_Operation
9214 (Call_Node => New_Call_Node,
9215 Node_To_Replace => Node_To_Replace);
9216 end if;
9217
9218 -- Check if there is a class-wide subprogram covering the
9219 -- primitive. This check must be done even if a candidate
9220 -- was found in order to report ambiguous calls.
9221
9222 if not Prim_Result then
9223 Ignore :=
9224 Try_Class_Wide_Operation
9225 (Call_Node => New_Call_Node,
9226 Node_To_Replace => Node_To_Replace);
9227
9228 -- If we found a primitive we search for class-wide subprograms
9229 -- using a duplicate of the call node (done to avoid missing its
9230 -- decoration if there is no ambiguity).
9231
9232 else
9233 Ignore :=
9234 Try_Class_Wide_Operation
9235 (Call_Node => Dup_Call_Node,
9236 Node_To_Replace => Node_To_Replace);
9237 end if;
9238 end;
9239 end Try_One_Prefix_Interpretation;
9240
9241 -----------------------------
9242 -- Try_Primitive_Operation --
9243 -----------------------------
9244
9245 function Try_Primitive_Operation
9246 (Call_Node : Node_Id;
9247 Node_To_Replace : Node_Id) return Boolean
9248 is
9249 Elmt : Elmt_Id;
9250 Prim_Op : Entity_Id;
9251 Matching_Op : Entity_Id := Empty;
9252 Prim_Op_Ref : Node_Id := Empty;
9253
9254 Corr_Type : Entity_Id := Empty;
9255 -- If the prefix is a synchronized type, the controlling type of
9256 -- the primitive operation is the corresponding record type, else
9257 -- this is the object type itself.
9258
9259 Success : Boolean := False;
9260
9261 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
9262 -- For tagged types the candidate interpretations are found in
9263 -- the list of primitive operations of the type and its ancestors.
9264 -- For formal tagged types we have to find the operations declared
9265 -- in the same scope as the type (including in the generic formal
9266 -- part) because the type itself carries no primitive operations,
9267 -- except for formal derived types that inherit the operations of
9268 -- the parent and progenitors.
9269 --
9270 -- If the context is a generic subprogram body, the generic formals
9271 -- are visible by name, but are not in the entity list of the
9272 -- subprogram because that list starts with the subprogram formals.
9273 -- We retrieve the candidate operations from the generic declaration.
9274
9275 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
9276 -- Prefix notation can also be used on operations that are not
9277 -- primitives of the type, but are declared in the same immediate
9278 -- declarative part, which can only mean the corresponding package
9279 -- body (See RM 4.1.3 (9.2/3)). If we are in that body we extend the
9280 -- list of primitives with body operations with the same name that
9281 -- may be candidates, so that Try_Primitive_Operations can examine
9282 -- them if no real primitive is found.
9283
9284 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
9285 -- An operation that overrides an inherited operation in the private
9286 -- part of its package may be hidden, but if the inherited operation
9287 -- is visible a direct call to it will dispatch to the private one,
9288 -- which is therefore a valid candidate.
9289
9290 function Names_Match
9291 (Obj_Type : Entity_Id;
9292 Prim_Op : Entity_Id;
9293 Subprog : Entity_Id) return Boolean;
9294 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
9295 -- is a protected type then compare also the original name of Prim_Op
9296 -- with the name of Subprog (since the expander may have added a
9297 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
9298
9299 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
9300 -- Verify that the prefix, dereferenced if need be, is a valid
9301 -- controlling argument in a call to Op. The remaining actuals
9302 -- are checked in the subsequent call to Analyze_One_Call.
9303
9304 ------------------------------
9305 -- Collect_Generic_Type_Ops --
9306 ------------------------------
9307
9308 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
9309 Bas : constant Entity_Id := Base_Type (T);
9310 Candidates : constant Elist_Id := New_Elmt_List;
9311 Subp : Entity_Id;
9312 Formal : Entity_Id;
9313
9314 procedure Check_Candidate;
9315 -- The operation is a candidate if its first parameter is a
9316 -- controlling operand of the desired type.
9317
9318 -----------------------
9319 -- Check_Candidate; --
9320 -----------------------
9321
9322 procedure Check_Candidate is
9323 begin
9324 Formal := First_Formal (Subp);
9325
9326 if Present (Formal)
9327 and then Is_Controlling_Formal (Formal)
9328 and then
9329 (Base_Type (Etype (Formal)) = Bas
9330 or else
9331 (Is_Access_Type (Etype (Formal))
9332 and then Designated_Type (Etype (Formal)) = Bas))
9333 then
9334 Append_Elmt (Subp, Candidates);
9335 end if;
9336 end Check_Candidate;
9337
9338 -- Start of processing for Collect_Generic_Type_Ops
9339
9340 begin
9341 if Is_Derived_Type (T) then
9342 return Primitive_Operations (T);
9343
9344 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
9345
9346 -- Scan the list of generic formals to find subprograms
9347 -- that may have a first controlling formal of the type.
9348
9349 if Nkind (Unit_Declaration_Node (Scope (T))) =
9350 N_Generic_Subprogram_Declaration
9351 then
9352 declare
9353 Decl : Node_Id;
9354
9355 begin
9356 Decl :=
9357 First (Generic_Formal_Declarations
9358 (Unit_Declaration_Node (Scope (T))));
9359 while Present (Decl) loop
9360 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
9361 Subp := Defining_Entity (Decl);
9362 Check_Candidate;
9363 end if;
9364
9365 Next (Decl);
9366 end loop;
9367 end;
9368 end if;
9369 return Candidates;
9370
9371 else
9372 -- Scan the list of entities declared in the same scope as
9373 -- the type. In general this will be an open scope, given that
9374 -- the call we are analyzing can only appear within a generic
9375 -- declaration or body (either the one that declares T, or a
9376 -- child unit).
9377
9378 -- For a subtype representing a generic actual type, go to the
9379 -- base type.
9380
9381 if Is_Generic_Actual_Type (T) then
9382 Subp := First_Entity (Scope (Base_Type (T)));
9383 else
9384 Subp := First_Entity (Scope (T));
9385 end if;
9386
9387 while Present (Subp) loop
9388 if Is_Overloadable (Subp) then
9389 Check_Candidate;
9390 end if;
9391
9392 Next_Entity (Subp);
9393 end loop;
9394
9395 return Candidates;
9396 end if;
9397 end Collect_Generic_Type_Ops;
9398
9399 ----------------------------
9400 -- Extended_Primitive_Ops --
9401 ----------------------------
9402
9403 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
9404 Type_Scope : constant Entity_Id := Scope (T);
9405
9406 Body_Decls : List_Id;
9407 Op_Found : Boolean;
9408 Op : Entity_Id;
9409 Op_List : Elist_Id;
9410
9411 begin
9412 Op_List := Primitive_Operations (T);
9413
9414 if Ekind (Type_Scope) = E_Package
9415 and then In_Package_Body (Type_Scope)
9416 and then In_Open_Scopes (Type_Scope)
9417 then
9418 -- Retrieve list of declarations of package body.
9419
9420 Body_Decls :=
9421 Declarations
9422 (Unit_Declaration_Node
9423 (Corresponding_Body
9424 (Unit_Declaration_Node (Type_Scope))));
9425
9426 Op := Current_Entity (Subprog);
9427 Op_Found := False;
9428 while Present (Op) loop
9429 if Comes_From_Source (Op)
9430 and then Is_Overloadable (Op)
9431
9432 -- Exclude overriding primitive operations of a type
9433 -- extension declared in the package body, to prevent
9434 -- duplicates in extended list.
9435
9436 and then not Is_Primitive (Op)
9437 and then Is_List_Member (Unit_Declaration_Node (Op))
9438 and then List_Containing (Unit_Declaration_Node (Op)) =
9439 Body_Decls
9440 then
9441 if not Op_Found then
9442
9443 -- Copy list of primitives so it is not affected for
9444 -- other uses.
9445
9446 Op_List := New_Copy_Elist (Op_List);
9447 Op_Found := True;
9448 end if;
9449
9450 Append_Elmt (Op, Op_List);
9451 end if;
9452
9453 Op := Homonym (Op);
9454 end loop;
9455 end if;
9456
9457 return Op_List;
9458 end Extended_Primitive_Ops;
9459
9460 ---------------------------
9461 -- Is_Private_Overriding --
9462 ---------------------------
9463
9464 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
9465 Visible_Op : Entity_Id;
9466
9467 begin
9468 -- The subprogram may be overloaded with both visible and private
9469 -- entities with the same name. We have to scan the chain of
9470 -- homonyms to determine whether there is a previous implicit
9471 -- declaration in the same scope that is overridden by the
9472 -- private candidate.
9473
9474 Visible_Op := Homonym (Op);
9475 while Present (Visible_Op) loop
9476 if Scope (Op) /= Scope (Visible_Op) then
9477 return False;
9478
9479 elsif not Comes_From_Source (Visible_Op)
9480 and then Alias (Visible_Op) = Op
9481 and then not Is_Hidden (Visible_Op)
9482 then
9483 return True;
9484 end if;
9485
9486 Visible_Op := Homonym (Visible_Op);
9487 end loop;
9488
9489 return False;
9490 end Is_Private_Overriding;
9491
9492 -----------------
9493 -- Names_Match --
9494 -----------------
9495
9496 function Names_Match
9497 (Obj_Type : Entity_Id;
9498 Prim_Op : Entity_Id;
9499 Subprog : Entity_Id) return Boolean is
9500 begin
9501 -- Common case: exact match
9502
9503 if Chars (Prim_Op) = Chars (Subprog) then
9504 return True;
9505
9506 -- For protected type primitives the expander may have built the
9507 -- name of the dispatching primitive prepending the type name to
9508 -- avoid conflicts with the name of the protected subprogram (see
9509 -- Exp_Ch9.Build_Selected_Name).
9510
9511 elsif Is_Protected_Type (Obj_Type) then
9512 return
9513 Present (Original_Protected_Subprogram (Prim_Op))
9514 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
9515 Chars (Subprog);
9516
9517 -- In an instance, the selector name may be a generic actual that
9518 -- renames a primitive operation of the type of the prefix.
9519
9520 elsif In_Instance and then Present (Current_Entity (Subprog)) then
9521 declare
9522 Subp : constant Entity_Id := Current_Entity (Subprog);
9523 begin
9524 if Present (Subp)
9525 and then Is_Subprogram (Subp)
9526 and then Present (Renamed_Entity (Subp))
9527 and then Is_Generic_Actual_Subprogram (Subp)
9528 and then Chars (Renamed_Entity (Subp)) = Chars (Prim_Op)
9529 then
9530 return True;
9531 end if;
9532 end;
9533 end if;
9534
9535 return False;
9536 end Names_Match;
9537
9538 -----------------------------
9539 -- Valid_First_Argument_Of --
9540 -----------------------------
9541
9542 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
9543 Typ : Entity_Id := Etype (First_Formal (Op));
9544
9545 begin
9546 if Is_Concurrent_Type (Typ)
9547 and then Present (Corresponding_Record_Type (Typ))
9548 then
9549 Typ := Corresponding_Record_Type (Typ);
9550 end if;
9551
9552 -- Simple case. Object may be a subtype of the tagged type or may
9553 -- be the corresponding record of a synchronized type.
9554
9555 return Obj_Type = Typ
9556 or else Base_Type (Obj_Type) = Typ
9557 or else Corr_Type = Typ
9558
9559 -- Object may be of a derived type whose parent has unknown
9560 -- discriminants, in which case the type matches the underlying
9561 -- record view of its base.
9562
9563 or else
9564 (Has_Unknown_Discriminants (Typ)
9565 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
9566
9567 -- Prefix can be dereferenced
9568
9569 or else
9570 (Is_Access_Type (Corr_Type)
9571 and then Designated_Type (Corr_Type) = Typ)
9572
9573 -- Formal is an access parameter, for which the object can
9574 -- provide an access.
9575
9576 or else
9577 (Ekind (Typ) = E_Anonymous_Access_Type
9578 and then
9579 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
9580 end Valid_First_Argument_Of;
9581
9582 -- Start of processing for Try_Primitive_Operation
9583
9584 begin
9585 -- Look for subprograms in the list of primitive operations. The name
9586 -- must be identical, and the kind of call indicates the expected
9587 -- kind of operation (function or procedure). If the type is a
9588 -- (tagged) synchronized type, the primitive ops are attached to the
9589 -- corresponding record (base) type.
9590
9591 if Is_Concurrent_Type (Obj_Type) then
9592 if Present (Corresponding_Record_Type (Obj_Type)) then
9593 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
9594 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
9595 else
9596 Corr_Type := Obj_Type;
9597 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9598 end if;
9599
9600 elsif not Is_Generic_Type (Obj_Type) then
9601 Corr_Type := Obj_Type;
9602 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
9603
9604 else
9605 Corr_Type := Obj_Type;
9606 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
9607 end if;
9608
9609 while Present (Elmt) loop
9610 Prim_Op := Node (Elmt);
9611
9612 if Names_Match (Obj_Type, Prim_Op, Subprog)
9613 and then Present (First_Formal (Prim_Op))
9614 and then Valid_First_Argument_Of (Prim_Op)
9615 and then
9616 (Nkind (Call_Node) = N_Function_Call)
9617 =
9618 (Ekind (Prim_Op) = E_Function)
9619 then
9620 -- Ada 2005 (AI-251): If this primitive operation corresponds
9621 -- to an immediate ancestor interface there is no need to add
9622 -- it to the list of interpretations; the corresponding aliased
9623 -- primitive is also in this list of primitive operations and
9624 -- will be used instead.
9625
9626 if (Present (Interface_Alias (Prim_Op))
9627 and then Is_Ancestor (Find_Dispatching_Type
9628 (Alias (Prim_Op)), Corr_Type))
9629
9630 -- Do not consider hidden primitives unless the type is in an
9631 -- open scope or we are within an instance, where visibility
9632 -- is known to be correct, or else if this is an overriding
9633 -- operation in the private part for an inherited operation.
9634
9635 or else (Is_Hidden (Prim_Op)
9636 and then not Is_Immediately_Visible (Obj_Type)
9637 and then not In_Instance
9638 and then not Is_Private_Overriding (Prim_Op))
9639 then
9640 goto Continue;
9641 end if;
9642
9643 Set_Etype (Call_Node, Any_Type);
9644 Set_Is_Overloaded (Call_Node, False);
9645
9646 if No (Matching_Op) then
9647 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
9648 Candidate := Prim_Op;
9649
9650 Set_Parent (Call_Node, Parent (Node_To_Replace));
9651
9652 Set_Name (Call_Node, Prim_Op_Ref);
9653 Success := False;
9654
9655 Analyze_One_Call
9656 (N => Call_Node,
9657 Nam => Prim_Op,
9658 Report => Report_Error,
9659 Success => Success,
9660 Skip_First => True);
9661
9662 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
9663
9664 -- More than one interpretation, collect for subsequent
9665 -- disambiguation. If this is a procedure call and there
9666 -- is another match, report ambiguity now.
9667
9668 else
9669 Analyze_One_Call
9670 (N => Call_Node,
9671 Nam => Prim_Op,
9672 Report => Report_Error,
9673 Success => Success,
9674 Skip_First => True);
9675
9676 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
9677 and then Nkind (Call_Node) /= N_Function_Call
9678 then
9679 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
9680 Report_Ambiguity (Matching_Op);
9681 Report_Ambiguity (Prim_Op);
9682 return True;
9683 end if;
9684 end if;
9685 end if;
9686
9687 <<Continue>>
9688 Next_Elmt (Elmt);
9689 end loop;
9690
9691 if Present (Matching_Op) then
9692 Set_Etype (Call_Node, Etype (Matching_Op));
9693 end if;
9694
9695 return Present (Matching_Op);
9696 end Try_Primitive_Operation;
9697
9698 ---------------------
9699 -- Valid_Candidate --
9700 ---------------------
9701
9702 function Valid_Candidate
9703 (Success : Boolean;
9704 Call : Node_Id;
9705 Subp : Entity_Id) return Entity_Id
9706 is
9707 Arr_Type : Entity_Id;
9708 Comp_Type : Entity_Id;
9709
9710 begin
9711 -- If the subprogram is a valid interpretation, record it in global
9712 -- variable Subprog, to collect all possible overloadings.
9713
9714 if Success then
9715 if Subp /= Entity (Subprog) then
9716 Add_One_Interp (Subprog, Subp, Etype (Subp));
9717 end if;
9718 end if;
9719
9720 -- If the call may be an indexed call, retrieve component type of
9721 -- resulting expression, and add possible interpretation.
9722
9723 Arr_Type := Empty;
9724 Comp_Type := Empty;
9725
9726 if Nkind (Call) = N_Function_Call
9727 and then Nkind (Parent (N)) = N_Indexed_Component
9728 and then Needs_One_Actual (Subp)
9729 then
9730 if Is_Array_Type (Etype (Subp)) then
9731 Arr_Type := Etype (Subp);
9732
9733 elsif Is_Access_Type (Etype (Subp))
9734 and then Is_Array_Type (Designated_Type (Etype (Subp)))
9735 then
9736 Arr_Type := Designated_Type (Etype (Subp));
9737 end if;
9738 end if;
9739
9740 if Present (Arr_Type) then
9741
9742 -- Verify that the actuals (excluding the object) match the types
9743 -- of the indexes.
9744
9745 declare
9746 Actual : Node_Id;
9747 Index : Node_Id;
9748
9749 begin
9750 Actual := Next (First_Actual (Call));
9751 Index := First_Index (Arr_Type);
9752 while Present (Actual) and then Present (Index) loop
9753 if not Has_Compatible_Type (Actual, Etype (Index)) then
9754 Arr_Type := Empty;
9755 exit;
9756 end if;
9757
9758 Next_Actual (Actual);
9759 Next_Index (Index);
9760 end loop;
9761
9762 if No (Actual)
9763 and then No (Index)
9764 and then Present (Arr_Type)
9765 then
9766 Comp_Type := Component_Type (Arr_Type);
9767 end if;
9768 end;
9769
9770 if Present (Comp_Type)
9771 and then Etype (Subprog) /= Comp_Type
9772 then
9773 Add_One_Interp (Subprog, Subp, Comp_Type);
9774 end if;
9775 end if;
9776
9777 if Etype (Call) /= Any_Type then
9778 return Subp;
9779 else
9780 return Empty;
9781 end if;
9782 end Valid_Candidate;
9783
9784 -- Start of processing for Try_Object_Operation
9785
9786 begin
9787 Analyze_Expression (Obj);
9788
9789 -- Analyze the actuals if node is known to be a subprogram call
9790
9791 if Is_Subprg_Call and then N = Name (Parent (N)) then
9792 Actual := First (Parameter_Associations (Parent (N)));
9793 while Present (Actual) loop
9794 Analyze_Expression (Actual);
9795 Next (Actual);
9796 end loop;
9797 end if;
9798
9799 -- Build a subprogram call node, using a copy of Obj as its first
9800 -- actual. This is a placeholder, to be replaced by an explicit
9801 -- dereference when needed.
9802
9803 Transform_Object_Operation
9804 (Call_Node => New_Call_Node,
9805 Node_To_Replace => Node_To_Replace);
9806
9807 Set_Etype (New_Call_Node, Any_Type);
9808 Set_Etype (Subprog, Any_Type);
9809 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
9810
9811 if not Is_Overloaded (Obj) then
9812 Try_One_Prefix_Interpretation (Obj_Type);
9813
9814 else
9815 declare
9816 I : Interp_Index;
9817 It : Interp;
9818 begin
9819 Get_First_Interp (Obj, I, It);
9820 while Present (It.Nam) loop
9821 Try_One_Prefix_Interpretation (It.Typ);
9822 Get_Next_Interp (I, It);
9823 end loop;
9824 end;
9825 end if;
9826
9827 if Etype (New_Call_Node) /= Any_Type then
9828
9829 -- No need to complete the tree transformations if we are only
9830 -- searching for conflicting class-wide subprograms
9831
9832 if CW_Test_Only then
9833 return False;
9834 else
9835 Complete_Object_Operation
9836 (Call_Node => New_Call_Node,
9837 Node_To_Replace => Node_To_Replace);
9838 return True;
9839 end if;
9840
9841 elsif Present (Candidate) then
9842
9843 -- The argument list is not type correct. Re-analyze with error
9844 -- reporting enabled, and use one of the possible candidates.
9845 -- In All_Errors_Mode, re-analyze all failed interpretations.
9846
9847 if All_Errors_Mode then
9848 Report_Error := True;
9849 if Try_Primitive_Operation
9850 (Call_Node => New_Call_Node,
9851 Node_To_Replace => Node_To_Replace)
9852
9853 or else
9854 Try_Class_Wide_Operation
9855 (Call_Node => New_Call_Node,
9856 Node_To_Replace => Node_To_Replace)
9857 then
9858 null;
9859 end if;
9860
9861 else
9862 Analyze_One_Call
9863 (N => New_Call_Node,
9864 Nam => Candidate,
9865 Report => True,
9866 Success => Success,
9867 Skip_First => True);
9868 end if;
9869
9870 -- No need for further errors
9871
9872 return True;
9873
9874 else
9875 -- There was no candidate operation, so report it as an error
9876 -- in the caller: Analyze_Selected_Component.
9877
9878 return False;
9879 end if;
9880 end Try_Object_Operation;
9881
9882 ---------
9883 -- wpo --
9884 ---------
9885
9886 procedure wpo (T : Entity_Id) is
9887 Op : Entity_Id;
9888 E : Elmt_Id;
9889
9890 begin
9891 if not Is_Tagged_Type (T) then
9892 return;
9893 end if;
9894
9895 E := First_Elmt (Primitive_Operations (Base_Type (T)));
9896 while Present (E) loop
9897 Op := Node (E);
9898 Write_Int (Int (Op));
9899 Write_Str (" === ");
9900 Write_Name (Chars (Op));
9901 Write_Str (" in ");
9902 Write_Name (Chars (Scope (Op)));
9903 Next_Elmt (E);
9904 Write_Eol;
9905 end loop;
9906 end wpo;
9907
9908 end Sem_Ch4;