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