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