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