Debian Codegraph @

gcc/ada/sem_ch4.adb

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