[gcc/]
[official-gcc.git] / gcc / ada / sem_ch4.adb
blobb78b06a05e049325d778657dd72e73e5c17ef67f
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-2014, 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 Fname; use Fname;
34 with Itypes; use Itypes;
35 with Lib; use Lib;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Namet.Sp; use Namet.Sp;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Stand; use Stand;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Tbuild; use Tbuild;
64 with Uintp; use Uintp;
66 package body Sem_Ch4 is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Analyze_Concatenation_Rest (N : Node_Id);
73 -- Does the "rest" of the work of Analyze_Concatenation, after the left
74 -- operand has been analyzed. See Analyze_Concatenation for details.
76 procedure Analyze_Expression (N : Node_Id);
77 -- For expressions that are not names, this is just a call to analyze.
78 -- If the expression is a name, it may be a call to a parameterless
79 -- function, and if so must be converted into an explicit call node
80 -- and analyzed as such. This deproceduring must be done during the first
81 -- pass of overload resolution, because otherwise a procedure call with
82 -- overloaded actuals may fail to resolve.
84 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
85 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call
86 -- is an operator name or an expanded name whose selector is an operator
87 -- name, and one possible interpretation is as a predefined operator.
89 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
90 -- If the prefix of a selected_component is overloaded, the proper
91 -- interpretation that yields a record type with the proper selector
92 -- name must be selected.
94 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
95 -- Procedure to analyze a user defined binary operator, which is resolved
96 -- like a function, but instead of a list of actuals it is presented
97 -- with the left and right operands of an operator node.
99 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
100 -- Procedure to analyze a user defined unary operator, which is resolved
101 -- like a function, but instead of a list of actuals, it is presented with
102 -- the operand of the operator node.
104 procedure Ambiguous_Operands (N : Node_Id);
105 -- For equality, membership, and comparison operators with overloaded
106 -- arguments, list possible interpretations.
108 procedure Analyze_One_Call
109 (N : Node_Id;
110 Nam : Entity_Id;
111 Report : Boolean;
112 Success : out Boolean;
113 Skip_First : Boolean := False);
114 -- Check one interpretation of an overloaded subprogram name for
115 -- compatibility with the types of the actuals in a call. If there is a
116 -- single interpretation which does not match, post error if Report is
117 -- set to True.
119 -- Nam is the entity that provides the formals against which the actuals
120 -- are checked. Nam is either the name of a subprogram, or the internal
121 -- subprogram type constructed for an access_to_subprogram. If the actuals
122 -- are compatible with Nam, then Nam is added to the list of candidate
123 -- interpretations for N, and Success is set to True.
125 -- The flag Skip_First is used when analyzing a call that was rewritten
126 -- from object notation. In this case the first actual may have to receive
127 -- an explicit dereference, depending on the first formal of the operation
128 -- being called. The caller will have verified that the object is legal
129 -- for the call. If the remaining parameters match, the first parameter
130 -- will rewritten as a dereference if needed, prior to completing analysis.
132 procedure Check_Misspelled_Selector
133 (Prefix : Entity_Id;
134 Sel : Node_Id);
135 -- Give possible misspelling diagnostic if Sel is likely to be a mis-
136 -- spelling of one of the selectors of the Prefix. This is called by
137 -- Analyze_Selected_Component after producing an invalid selector error
138 -- message.
140 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
141 -- Verify that type T is declared in scope S. Used to find interpretations
142 -- for operators given by expanded names. This is abstracted as a separate
143 -- function to handle extensions to System, where S is System, but T is
144 -- declared in the extension.
146 procedure Find_Arithmetic_Types
147 (L, R : Node_Id;
148 Op_Id : Entity_Id;
149 N : Node_Id);
150 -- L and R are the operands of an arithmetic operator. Find
151 -- consistent pairs of interpretations for L and R that have a
152 -- numeric type consistent with the semantics of the operator.
154 procedure Find_Comparison_Types
155 (L, R : Node_Id;
156 Op_Id : Entity_Id;
157 N : Node_Id);
158 -- L and R are operands of a comparison operator. Find consistent
159 -- pairs of interpretations for L and R.
161 procedure Find_Concatenation_Types
162 (L, R : Node_Id;
163 Op_Id : Entity_Id;
164 N : Node_Id);
165 -- For the four varieties of concatenation
167 procedure Find_Equality_Types
168 (L, R : Node_Id;
169 Op_Id : Entity_Id;
170 N : Node_Id);
171 -- Ditto for equality operators
173 procedure Find_Boolean_Types
174 (L, R : Node_Id;
175 Op_Id : Entity_Id;
176 N : Node_Id);
177 -- Ditto for binary logical operations
179 procedure Find_Negation_Types
180 (R : Node_Id;
181 Op_Id : Entity_Id;
182 N : Node_Id);
183 -- Find consistent interpretation for operand of negation operator
185 procedure Find_Non_Universal_Interpretations
186 (N : Node_Id;
187 R : Node_Id;
188 Op_Id : Entity_Id;
189 T1 : Entity_Id);
190 -- For equality and comparison operators, the result is always boolean,
191 -- and the legality of the operation is determined from the visibility
192 -- of the operand types. If one of the operands has a universal interpre-
193 -- tation, the legality check uses some compatible non-universal
194 -- interpretation of the other operand. N can be an operator node, or
195 -- a function call whose name is an operator designator. Any_Access, which
196 -- is the initial type of the literal NULL, is a universal type for the
197 -- purpose of this routine.
199 function Find_Primitive_Operation (N : Node_Id) return Boolean;
200 -- Find candidate interpretations for the name Obj.Proc when it appears
201 -- in a subprogram renaming declaration.
203 procedure Find_Unary_Types
204 (R : Node_Id;
205 Op_Id : Entity_Id;
206 N : Node_Id);
207 -- Unary arithmetic types: plus, minus, abs
209 procedure Check_Arithmetic_Pair
210 (T1, T2 : Entity_Id;
211 Op_Id : Entity_Id;
212 N : Node_Id);
213 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
214 -- types for left and right operand. Determine whether they constitute
215 -- a valid pair for the given operator, and record the corresponding
216 -- interpretation of the operator node. The node N may be an operator
217 -- node (the usual case) or a function call whose prefix is an operator
218 -- designator. In both cases Op_Id is the operator name itself.
220 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
221 -- Give detailed information on overloaded call where none of the
222 -- interpretations match. N is the call node, Nam the designator for
223 -- the overloaded entity being called.
225 function Junk_Operand (N : Node_Id) return Boolean;
226 -- Test for an operand that is an inappropriate entity (e.g. a package
227 -- name or a label). If so, issue an error message and return True. If
228 -- the operand is not an inappropriate entity kind, return False.
230 procedure Operator_Check (N : Node_Id);
231 -- Verify that an operator has received some valid interpretation. If none
232 -- was found, determine whether a use clause would make the operation
233 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
234 -- every type compatible with the operator, even if the operator for the
235 -- type is not directly visible. The routine uses this type to emit a more
236 -- informative message.
238 function Process_Implicit_Dereference_Prefix
239 (E : Entity_Id;
240 P : Node_Id) return Entity_Id;
241 -- Called when P is the prefix of an implicit dereference, denoting an
242 -- object E. The function returns the designated type of the prefix, taking
243 -- into account that the designated type of an anonymous access type may be
244 -- a limited view, when the non-limited view is visible.
245 -- If in semantics only mode (-gnatc or generic), the function also records
246 -- that the prefix is a reference to E, if any. Normally, such a reference
247 -- is generated only when the implicit dereference is expanded into an
248 -- explicit one, but for consistency we must generate the reference when
249 -- expansion is disabled as well.
251 procedure Remove_Abstract_Operations (N : Node_Id);
252 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
253 -- operation is not a candidate interpretation.
255 function Try_Container_Indexing
256 (N : Node_Id;
257 Prefix : Node_Id;
258 Exprs : List_Id) return Boolean;
259 -- AI05-0139: Generalized indexing to support iterators over containers
261 function Try_Indexed_Call
262 (N : Node_Id;
263 Nam : Entity_Id;
264 Typ : Entity_Id;
265 Skip_First : Boolean) return Boolean;
266 -- If a function has defaults for all its actuals, a call to it may in fact
267 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
268 -- interpretation as an indexing, prior to analysis as a call. If both are
269 -- possible, the node is overloaded with both interpretations (same symbol
270 -- but two different types). If the call is written in prefix form, the
271 -- prefix becomes the first parameter in the call, and only the remaining
272 -- actuals must be checked for the presence of defaults.
274 function Try_Indirect_Call
275 (N : Node_Id;
276 Nam : Entity_Id;
277 Typ : Entity_Id) return Boolean;
278 -- Similarly, a function F that needs no actuals can return an access to a
279 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
280 -- the call may be overloaded with both interpretations.
282 function Try_Object_Operation
283 (N : Node_Id;
284 CW_Test_Only : Boolean := False) return Boolean;
285 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
286 -- is a call in this notation, it is transformed into a normal subprogram
287 -- call where the prefix is a parameter, and True is returned. If node
288 -- N is not of this form, it is unchanged, and False is returned. if
289 -- CW_Test_Only is true then N is an N_Selected_Component node which
290 -- is part of a call to an entry or procedure of a tagged concurrent
291 -- type and this routine is invoked to search for class-wide subprograms
292 -- conflicting with the target entity.
294 procedure wpo (T : Entity_Id);
295 pragma Warnings (Off, wpo);
296 -- Used for debugging: obtain list of primitive operations even if
297 -- type is not frozen and dispatch table is not built yet.
299 ------------------------
300 -- Ambiguous_Operands --
301 ------------------------
303 procedure Ambiguous_Operands (N : Node_Id) is
304 procedure List_Operand_Interps (Opnd : Node_Id);
306 --------------------------
307 -- List_Operand_Interps --
308 --------------------------
310 procedure List_Operand_Interps (Opnd : Node_Id) is
311 Nam : Node_Id;
312 Err : Node_Id := N;
314 begin
315 if Is_Overloaded (Opnd) then
316 if Nkind (Opnd) in N_Op then
317 Nam := Opnd;
318 elsif Nkind (Opnd) = N_Function_Call then
319 Nam := Name (Opnd);
320 elsif Ada_Version >= Ada_2012 then
321 declare
322 It : Interp;
323 I : Interp_Index;
325 begin
326 Get_First_Interp (Opnd, I, It);
327 while Present (It.Nam) loop
328 if Has_Implicit_Dereference (It.Typ) then
329 Error_Msg_N
330 ("can be interpreted as implicit dereference", Opnd);
331 return;
332 end if;
334 Get_Next_Interp (I, It);
335 end loop;
336 end;
338 return;
339 end if;
341 else
342 return;
343 end if;
345 if Opnd = Left_Opnd (N) then
346 Error_Msg_N ("\left operand has the following interpretations", N);
347 else
348 Error_Msg_N
349 ("\right operand has the following interpretations", N);
350 Err := Opnd;
351 end if;
353 List_Interps (Nam, Err);
354 end List_Operand_Interps;
356 -- Start of processing for Ambiguous_Operands
358 begin
359 if Nkind (N) in N_Membership_Test then
360 Error_Msg_N ("ambiguous operands for membership", N);
362 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
363 Error_Msg_N ("ambiguous operands for equality", N);
365 else
366 Error_Msg_N ("ambiguous operands for comparison", N);
367 end if;
369 if All_Errors_Mode then
370 List_Operand_Interps (Left_Opnd (N));
371 List_Operand_Interps (Right_Opnd (N));
372 else
373 Error_Msg_N ("\use -gnatf switch for details", N);
374 end if;
375 end Ambiguous_Operands;
377 -----------------------
378 -- Analyze_Aggregate --
379 -----------------------
381 -- Most of the analysis of Aggregates requires that the type be known,
382 -- and is therefore put off until resolution.
384 procedure Analyze_Aggregate (N : Node_Id) is
385 begin
386 if No (Etype (N)) then
387 Set_Etype (N, Any_Composite);
388 end if;
389 end Analyze_Aggregate;
391 -----------------------
392 -- Analyze_Allocator --
393 -----------------------
395 procedure Analyze_Allocator (N : Node_Id) is
396 Loc : constant Source_Ptr := Sloc (N);
397 Sav_Errs : constant Nat := Serious_Errors_Detected;
398 E : Node_Id := Expression (N);
399 Acc_Type : Entity_Id;
400 Type_Id : Entity_Id;
401 P : Node_Id;
402 C : Node_Id;
403 Onode : Node_Id;
405 begin
406 Check_SPARK_Restriction ("allocator is not allowed", N);
408 -- Deal with allocator restrictions
410 -- In accordance with H.4(7), the No_Allocators restriction only applies
411 -- to user-written allocators. The same consideration applies to the
412 -- No_Standard_Allocators_Before_Elaboration restriction.
414 if Comes_From_Source (N) then
415 Check_Restriction (No_Allocators, N);
417 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
418 -- look at enclosing context, checking task/main subprogram case.
420 C := N;
421 P := Parent (C);
422 while Present (P) loop
424 -- For the task case we need a handled sequence of statements,
425 -- where the occurrence of the allocator is within the statements
426 -- and the parent is a task body
428 if Nkind (P) = N_Handled_Sequence_Of_Statements
429 and then Is_List_Member (C)
430 and then List_Containing (C) = Statements (P)
431 then
432 Onode := Original_Node (Parent (P));
434 -- Check for allocator within task body, this is a definite
435 -- violation of No_Allocators_After_Elaboration we can detect
436 -- at compile time.
438 if Nkind (Onode) = N_Task_Body then
439 Check_Restriction
440 (No_Standard_Allocators_After_Elaboration, N);
441 exit;
442 end if;
443 end if;
445 -- The other case is appearance in a subprogram body. This is
446 -- a violation if this is a library level subprogram with no
447 -- parameters. Note that this is now a static error even if the
448 -- subprogram is not the main program (this is a change, in an
449 -- earlier version only the main program was affected, and the
450 -- check had to be done in the binder.
452 if Nkind (P) = N_Subprogram_Body
453 and then Nkind (Parent (P)) = N_Compilation_Unit
454 and then No (Parameter_Specifications (Specification (P)))
455 then
456 Check_Restriction
457 (No_Standard_Allocators_After_Elaboration, N);
458 end if;
460 C := P;
461 P := Parent (C);
462 end loop;
463 end if;
465 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
466 -- any. The expected type for the name is any type. A non-overloading
467 -- rule then requires it to be of a type descended from
468 -- System.Storage_Pools.Subpools.Subpool_Handle.
470 -- This isn't exactly what the AI says, but it seems to be the right
471 -- rule. The AI should be fixed.???
473 declare
474 Subpool : constant Node_Id := Subpool_Handle_Name (N);
476 begin
477 if Present (Subpool) then
478 Analyze (Subpool);
480 if Is_Overloaded (Subpool) then
481 Error_Msg_N ("ambiguous subpool handle", Subpool);
482 end if;
484 -- Check that Etype (Subpool) is descended from Subpool_Handle
486 Resolve (Subpool);
487 end if;
488 end;
490 -- Analyze the qualified expression or subtype indication
492 if Nkind (E) = N_Qualified_Expression then
493 Acc_Type := Create_Itype (E_Allocator_Type, N);
494 Set_Etype (Acc_Type, Acc_Type);
495 Find_Type (Subtype_Mark (E));
497 -- Analyze the qualified expression, and apply the name resolution
498 -- rule given in 4.7(3).
500 Analyze (E);
501 Type_Id := Etype (E);
502 Set_Directly_Designated_Type (Acc_Type, Type_Id);
504 -- Allocators generated by the build-in-place expansion mechanism
505 -- are explicitly marked as coming from source but do not need to be
506 -- checked for limited initialization. To exclude this case, ensure
507 -- that the parent of the allocator is a source node.
509 if Is_Limited_Type (Type_Id)
510 and then Comes_From_Source (N)
511 and then Comes_From_Source (Parent (N))
512 and then not In_Instance_Body
513 then
514 if not OK_For_Limited_Init (Type_Id, Expression (E)) then
515 Error_Msg_N ("initialization not allowed for limited types", N);
516 Explain_Limited_Type (Type_Id, N);
517 end if;
518 end if;
520 -- A qualified expression requires an exact match of the type,
521 -- class-wide matching is not allowed.
523 -- if Is_Class_Wide_Type (Type_Id)
524 -- and then Base_Type
525 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
526 -- then
527 -- Wrong_Type (Expression (E), Type_Id);
528 -- end if;
530 -- We don't analyze the qualified expression itself because it's
531 -- part of the allocator. It is fully analyzed and resolved when
532 -- the allocator is resolved with the context type.
534 Set_Etype (E, Type_Id);
536 -- Case where allocator has a subtype indication
538 else
539 declare
540 Def_Id : Entity_Id;
541 Base_Typ : Entity_Id;
543 begin
544 -- If the allocator includes a N_Subtype_Indication then a
545 -- constraint is present, otherwise the node is a subtype mark.
546 -- Introduce an explicit subtype declaration into the tree
547 -- defining some anonymous subtype and rewrite the allocator to
548 -- use this subtype rather than the subtype indication.
550 -- It is important to introduce the explicit subtype declaration
551 -- so that the bounds of the subtype indication are attached to
552 -- the tree in case the allocator is inside a generic unit.
554 if Nkind (E) = N_Subtype_Indication then
556 -- A constraint is only allowed for a composite type in Ada
557 -- 95. In Ada 83, a constraint is also allowed for an
558 -- access-to-composite type, but the constraint is ignored.
560 Find_Type (Subtype_Mark (E));
561 Base_Typ := Entity (Subtype_Mark (E));
563 if Is_Elementary_Type (Base_Typ) then
564 if not (Ada_Version = Ada_83
565 and then Is_Access_Type (Base_Typ))
566 then
567 Error_Msg_N ("constraint not allowed here", E);
569 if Nkind (Constraint (E)) =
570 N_Index_Or_Discriminant_Constraint
571 then
572 Error_Msg_N -- CODEFIX
573 ("\if qualified expression was meant, " &
574 "use apostrophe", Constraint (E));
575 end if;
576 end if;
578 -- Get rid of the bogus constraint:
580 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
581 Analyze_Allocator (N);
582 return;
583 end if;
585 if Expander_Active then
586 Def_Id := Make_Temporary (Loc, 'S');
588 Insert_Action (E,
589 Make_Subtype_Declaration (Loc,
590 Defining_Identifier => Def_Id,
591 Subtype_Indication => Relocate_Node (E)));
593 if Sav_Errs /= Serious_Errors_Detected
594 and then Nkind (Constraint (E)) =
595 N_Index_Or_Discriminant_Constraint
596 then
597 Error_Msg_N -- CODEFIX
598 ("if qualified expression was meant, "
599 & "use apostrophe!", Constraint (E));
600 end if;
602 E := New_Occurrence_Of (Def_Id, Loc);
603 Rewrite (Expression (N), E);
604 end if;
605 end if;
607 Type_Id := Process_Subtype (E, N);
608 Acc_Type := Create_Itype (E_Allocator_Type, N);
609 Set_Etype (Acc_Type, Acc_Type);
610 Set_Directly_Designated_Type (Acc_Type, Type_Id);
611 Check_Fully_Declared (Type_Id, N);
613 -- Ada 2005 (AI-231): If the designated type is itself an access
614 -- type that excludes null, its default initialization will
615 -- be a null object, and we can insert an unconditional raise
616 -- before the allocator.
618 -- Ada 2012 (AI-104): A not null indication here is altogether
619 -- illegal.
621 if Can_Never_Be_Null (Type_Id) then
622 declare
623 Not_Null_Check : constant Node_Id :=
624 Make_Raise_Constraint_Error (Sloc (E),
625 Reason => CE_Null_Not_Allowed);
627 begin
628 if Expander_Active then
629 Insert_Action (N, Not_Null_Check);
630 Analyze (Not_Null_Check);
632 elsif Warn_On_Ada_2012_Compatibility then
633 Error_Msg_N
634 ("null value not allowed here in Ada 2012?y?", E);
635 end if;
636 end;
637 end if;
639 -- Check for missing initialization. Skip this check if we already
640 -- had errors on analyzing the allocator, since in that case these
641 -- are probably cascaded errors.
643 if Is_Indefinite_Subtype (Type_Id)
644 and then Serious_Errors_Detected = Sav_Errs
645 then
646 -- The build-in-place machinery may produce an allocator when
647 -- the designated type is indefinite but the underlying type is
648 -- not. In this case the unknown discriminants are meaningless
649 -- and should not trigger error messages. Check the parent node
650 -- because the allocator is marked as coming from source.
652 if Present (Underlying_Type (Type_Id))
653 and then not Is_Indefinite_Subtype (Underlying_Type (Type_Id))
654 and then not Comes_From_Source (Parent (N))
655 then
656 null;
658 elsif Is_Class_Wide_Type (Type_Id) then
659 Error_Msg_N
660 ("initialization required in class-wide allocation", N);
662 else
663 if Ada_Version < Ada_2005
664 and then Is_Limited_Type (Type_Id)
665 then
666 Error_Msg_N ("unconstrained allocation not allowed", N);
668 if Is_Array_Type (Type_Id) then
669 Error_Msg_N
670 ("\constraint with array bounds required", N);
672 elsif Has_Unknown_Discriminants (Type_Id) then
673 null;
675 else pragma Assert (Has_Discriminants (Type_Id));
676 Error_Msg_N
677 ("\constraint with discriminant values required", N);
678 end if;
680 -- Limited Ada 2005 and general non-limited case
682 else
683 Error_Msg_N
684 ("uninitialized unconstrained allocation not allowed",
687 if Is_Array_Type (Type_Id) then
688 Error_Msg_N
689 ("\qualified expression or constraint with " &
690 "array bounds required", N);
692 elsif Has_Unknown_Discriminants (Type_Id) then
693 Error_Msg_N ("\qualified expression required", N);
695 else pragma Assert (Has_Discriminants (Type_Id));
696 Error_Msg_N
697 ("\qualified expression or constraint with " &
698 "discriminant values required", N);
699 end if;
700 end if;
701 end if;
702 end if;
703 end;
704 end if;
706 if Is_Abstract_Type (Type_Id) then
707 Error_Msg_N ("cannot allocate abstract object", E);
708 end if;
710 if Has_Task (Designated_Type (Acc_Type)) then
711 Check_Restriction (No_Tasking, N);
712 Check_Restriction (Max_Tasks, N);
713 Check_Restriction (No_Task_Allocators, N);
714 end if;
716 -- Check restriction against dynamically allocated protected objects
718 if Has_Protected (Designated_Type (Acc_Type)) then
719 Check_Restriction (No_Protected_Type_Allocators, N);
720 end if;
722 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
723 -- type is nested, and the designated type needs finalization. The rule
724 -- is conservative in that class-wide types need finalization.
726 if Needs_Finalization (Designated_Type (Acc_Type))
727 and then not Is_Library_Level_Entity (Acc_Type)
728 then
729 Check_Restriction (No_Nested_Finalization, N);
730 end if;
732 -- Check that an allocator of a nested access type doesn't create a
733 -- protected object when restriction No_Local_Protected_Objects applies.
735 if Has_Protected (Designated_Type (Acc_Type))
736 and then not Is_Library_Level_Entity (Acc_Type)
737 then
738 Check_Restriction (No_Local_Protected_Objects, N);
739 end if;
741 -- If the No_Streams restriction is set, check that the type of the
742 -- object is not, and does not contain, any subtype derived from
743 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
744 -- Has_Stream just for efficiency reasons. There is no point in
745 -- spending time on a Has_Stream check if the restriction is not set.
747 if Restriction_Check_Required (No_Streams) then
748 if Has_Stream (Designated_Type (Acc_Type)) then
749 Check_Restriction (No_Streams, N);
750 end if;
751 end if;
753 Set_Etype (N, Acc_Type);
755 if not Is_Library_Level_Entity (Acc_Type) then
756 Check_Restriction (No_Local_Allocators, N);
757 end if;
759 if Serious_Errors_Detected > Sav_Errs then
760 Set_Error_Posted (N);
761 Set_Etype (N, Any_Type);
762 end if;
763 end Analyze_Allocator;
765 ---------------------------
766 -- Analyze_Arithmetic_Op --
767 ---------------------------
769 procedure Analyze_Arithmetic_Op (N : Node_Id) is
770 L : constant Node_Id := Left_Opnd (N);
771 R : constant Node_Id := Right_Opnd (N);
772 Op_Id : Entity_Id;
774 begin
775 Candidate_Type := Empty;
776 Analyze_Expression (L);
777 Analyze_Expression (R);
779 -- If the entity is already set, the node is the instantiation of a
780 -- generic node with a non-local reference, or was manufactured by a
781 -- call to Make_Op_xxx. In either case the entity is known to be valid,
782 -- and we do not need to collect interpretations, instead we just get
783 -- the single possible interpretation.
785 Op_Id := Entity (N);
787 if Present (Op_Id) then
788 if Ekind (Op_Id) = E_Operator then
790 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
791 and then Treat_Fixed_As_Integer (N)
792 then
793 null;
794 else
795 Set_Etype (N, Any_Type);
796 Find_Arithmetic_Types (L, R, Op_Id, N);
797 end if;
799 else
800 Set_Etype (N, Any_Type);
801 Add_One_Interp (N, Op_Id, Etype (Op_Id));
802 end if;
804 -- Entity is not already set, so we do need to collect interpretations
806 else
807 Op_Id := Get_Name_Entity_Id (Chars (N));
808 Set_Etype (N, Any_Type);
810 while Present (Op_Id) loop
811 if Ekind (Op_Id) = E_Operator
812 and then Present (Next_Entity (First_Entity (Op_Id)))
813 then
814 Find_Arithmetic_Types (L, R, Op_Id, N);
816 -- The following may seem superfluous, because an operator cannot
817 -- be generic, but this ignores the cleverness of the author of
818 -- ACVC bc1013a.
820 elsif Is_Overloadable (Op_Id) then
821 Analyze_User_Defined_Binary_Op (N, Op_Id);
822 end if;
824 Op_Id := Homonym (Op_Id);
825 end loop;
826 end if;
828 Operator_Check (N);
829 end Analyze_Arithmetic_Op;
831 ------------------
832 -- Analyze_Call --
833 ------------------
835 -- Function, procedure, and entry calls are checked here. The Name in
836 -- the call may be overloaded. The actuals have been analyzed and may
837 -- themselves be overloaded. On exit from this procedure, the node N
838 -- may have zero, one or more interpretations. In the first case an
839 -- error message is produced. In the last case, the node is flagged
840 -- as overloaded and the interpretations are collected in All_Interp.
842 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
843 -- the type-checking is similar to that of other calls.
845 procedure Analyze_Call (N : Node_Id) is
846 Actuals : constant List_Id := Parameter_Associations (N);
847 Nam : Node_Id;
848 X : Interp_Index;
849 It : Interp;
850 Nam_Ent : Entity_Id;
851 Success : Boolean := False;
853 Deref : Boolean := False;
854 -- Flag indicates whether an interpretation of the prefix is a
855 -- parameterless call that returns an access_to_subprogram.
857 procedure Check_Ghost_Subprogram_Call;
858 -- Verify the legality of a call to a ghost subprogram. Such calls can
859 -- appear only in assertion expressions except subtype predicates or
860 -- from within another ghost subprogram.
862 procedure Check_Mixed_Parameter_And_Named_Associations;
863 -- Check that parameter and named associations are not mixed. This is
864 -- a restriction in SPARK mode.
866 function Name_Denotes_Function return Boolean;
867 -- If the type of the name is an access to subprogram, this may be the
868 -- type of a name, or the return type of the function being called. If
869 -- the name is not an entity then it can denote a protected function.
870 -- Until we distinguish Etype from Return_Type, we must use this routine
871 -- to resolve the meaning of the name in the call.
873 procedure No_Interpretation;
874 -- Output error message when no valid interpretation exists
876 ---------------------------------
877 -- Check_Ghost_Subprogram_Call --
878 ---------------------------------
880 procedure Check_Ghost_Subprogram_Call is
881 S : Entity_Id;
883 begin
884 -- Do not perform the check while preanalyzing the enclosing context
885 -- because the call is not in its final place. Premature attempts to
886 -- verify the placement lead to bogus errors.
888 if In_Spec_Expression then
889 return;
891 -- The ghost subprogram appears inside an assertion expression which
892 -- is one of the allowed cases.
894 elsif In_Assertion_Expression_Pragma (N) then
895 return;
897 -- Otherwise see if it inside another ghost subprogram
899 else
900 -- Loop to climb scopes
902 S := Current_Scope;
903 while Present (S) and then S /= Standard_Standard loop
905 -- The call appears inside another ghost subprogram
907 if Is_Ghost_Subprogram (S) then
908 return;
909 end if;
911 S := Scope (S);
912 end loop;
914 -- If we fall through the loop it was not within another
915 -- ghost subprogram, so we have bad placement.
917 Error_Msg_N
918 ("call to ghost subprogram must appear in assertion expression "
919 & "or another ghost subprogram", N);
920 end if;
921 end Check_Ghost_Subprogram_Call;
923 --------------------------------------------------
924 -- Check_Mixed_Parameter_And_Named_Associations --
925 --------------------------------------------------
927 procedure Check_Mixed_Parameter_And_Named_Associations is
928 Actual : Node_Id;
929 Named_Seen : Boolean;
931 begin
932 Named_Seen := False;
934 Actual := First (Actuals);
935 while Present (Actual) loop
936 case Nkind (Actual) is
937 when N_Parameter_Association =>
938 if Named_Seen then
939 Check_SPARK_Restriction
940 ("named association cannot follow positional one",
941 Actual);
942 exit;
943 end if;
944 when others =>
945 Named_Seen := True;
946 end case;
948 Next (Actual);
949 end loop;
950 end Check_Mixed_Parameter_And_Named_Associations;
952 ---------------------------
953 -- Name_Denotes_Function --
954 ---------------------------
956 function Name_Denotes_Function return Boolean is
957 begin
958 if Is_Entity_Name (Nam) then
959 return Ekind (Entity (Nam)) = E_Function;
961 elsif Nkind (Nam) = N_Selected_Component then
962 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
964 else
965 return False;
966 end if;
967 end Name_Denotes_Function;
969 -----------------------
970 -- No_Interpretation --
971 -----------------------
973 procedure No_Interpretation is
974 L : constant Boolean := Is_List_Member (N);
975 K : constant Node_Kind := Nkind (Parent (N));
977 begin
978 -- If the node is in a list whose parent is not an expression then it
979 -- must be an attempted procedure call.
981 if L and then K not in N_Subexpr then
982 if Ekind (Entity (Nam)) = E_Generic_Procedure then
983 Error_Msg_NE
984 ("must instantiate generic procedure& before call",
985 Nam, Entity (Nam));
986 else
987 Error_Msg_N
988 ("procedure or entry name expected", Nam);
989 end if;
991 -- Check for tasking cases where only an entry call will do
993 elsif not L
994 and then Nkind_In (K, N_Entry_Call_Alternative,
995 N_Triggering_Alternative)
996 then
997 Error_Msg_N ("entry name expected", Nam);
999 -- Otherwise give general error message
1001 else
1002 Error_Msg_N ("invalid prefix in call", Nam);
1003 end if;
1004 end No_Interpretation;
1006 -- Start of processing for Analyze_Call
1008 begin
1009 if Restriction_Check_Required (SPARK_05) then
1010 Check_Mixed_Parameter_And_Named_Associations;
1011 end if;
1013 -- Initialize the type of the result of the call to the error type,
1014 -- which will be reset if the type is successfully resolved.
1016 Set_Etype (N, Any_Type);
1018 Nam := Name (N);
1020 if not Is_Overloaded (Nam) then
1022 -- Only one interpretation to check
1024 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1025 Nam_Ent := Etype (Nam);
1027 -- If the prefix is an access_to_subprogram, this may be an indirect
1028 -- call. This is the case if the name in the call is not an entity
1029 -- name, or if it is a function name in the context of a procedure
1030 -- call. In this latter case, we have a call to a parameterless
1031 -- function that returns a pointer_to_procedure which is the entity
1032 -- being called. Finally, F (X) may be a call to a parameterless
1033 -- function that returns a pointer to a function with parameters.
1034 -- Note that if F returns an access-to-subprogram whose designated
1035 -- type is an array, F (X) cannot be interpreted as an indirect call
1036 -- through the result of the call to F.
1038 elsif Is_Access_Type (Etype (Nam))
1039 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
1040 and then
1041 (not Name_Denotes_Function
1042 or else Nkind (N) = N_Procedure_Call_Statement
1043 or else
1044 (Nkind (Parent (N)) /= N_Explicit_Dereference
1045 and then Is_Entity_Name (Nam)
1046 and then No (First_Formal (Entity (Nam)))
1047 and then not
1048 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1049 and then Present (Actuals)))
1050 then
1051 Nam_Ent := Designated_Type (Etype (Nam));
1052 Insert_Explicit_Dereference (Nam);
1054 -- Selected component case. Simple entry or protected operation,
1055 -- where the entry name is given by the selector name.
1057 elsif Nkind (Nam) = N_Selected_Component then
1058 Nam_Ent := Entity (Selector_Name (Nam));
1060 if not Ekind_In (Nam_Ent, E_Entry,
1061 E_Entry_Family,
1062 E_Function,
1063 E_Procedure)
1064 then
1065 Error_Msg_N ("name in call is not a callable entity", Nam);
1066 Set_Etype (N, Any_Type);
1067 return;
1068 end if;
1070 -- If the name is an Indexed component, it can be a call to a member
1071 -- of an entry family. The prefix must be a selected component whose
1072 -- selector is the entry. Analyze_Procedure_Call normalizes several
1073 -- kinds of call into this form.
1075 elsif Nkind (Nam) = N_Indexed_Component then
1076 if Nkind (Prefix (Nam)) = N_Selected_Component then
1077 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1078 else
1079 Error_Msg_N ("name in call is not a callable entity", Nam);
1080 Set_Etype (N, Any_Type);
1081 return;
1082 end if;
1084 elsif not Is_Entity_Name (Nam) then
1085 Error_Msg_N ("name in call is not a callable entity", Nam);
1086 Set_Etype (N, Any_Type);
1087 return;
1089 else
1090 Nam_Ent := Entity (Nam);
1092 -- If not overloadable, this may be a generalized indexing
1093 -- operation with named associations. Rewrite again as an
1094 -- indexed component and analyze as container indexing.
1096 if not Is_Overloadable (Nam_Ent) then
1097 if Present
1098 (Find_Value_Of_Aspect
1099 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1100 then
1101 Replace (N,
1102 Make_Indexed_Component (Sloc (N),
1103 Prefix => Nam,
1104 Expressions => Parameter_Associations (N)));
1106 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1107 return;
1108 else
1109 No_Interpretation;
1110 end if;
1112 else
1113 No_Interpretation;
1114 end if;
1116 return;
1117 end if;
1118 end if;
1120 -- Operations generated for RACW stub types are called only through
1121 -- dispatching, and can never be the static interpretation of a call.
1123 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1124 No_Interpretation;
1125 return;
1126 end if;
1128 Analyze_One_Call (N, Nam_Ent, True, Success);
1130 -- If this is an indirect call, the return type of the access_to
1131 -- subprogram may be an incomplete type. At the point of the call,
1132 -- use the full type if available, and at the same time update the
1133 -- return type of the access_to_subprogram.
1135 if Success
1136 and then Nkind (Nam) = N_Explicit_Dereference
1137 and then Ekind (Etype (N)) = E_Incomplete_Type
1138 and then Present (Full_View (Etype (N)))
1139 then
1140 Set_Etype (N, Full_View (Etype (N)));
1141 Set_Etype (Nam_Ent, Etype (N));
1142 end if;
1144 -- Overloaded call
1146 else
1147 -- An overloaded selected component must denote overloaded operations
1148 -- of a concurrent type. The interpretations are attached to the
1149 -- simple name of those operations.
1151 if Nkind (Nam) = N_Selected_Component then
1152 Nam := Selector_Name (Nam);
1153 end if;
1155 Get_First_Interp (Nam, X, It);
1157 while Present (It.Nam) loop
1158 Nam_Ent := It.Nam;
1159 Deref := False;
1161 -- Name may be call that returns an access to subprogram, or more
1162 -- generally an overloaded expression one of whose interpretations
1163 -- yields an access to subprogram. If the name is an entity, we do
1164 -- not dereference, because the node is a call that returns the
1165 -- access type: note difference between f(x), where the call may
1166 -- return an access subprogram type, and f(x)(y), where the type
1167 -- returned by the call to f is implicitly dereferenced to analyze
1168 -- the outer call.
1170 if Is_Access_Type (Nam_Ent) then
1171 Nam_Ent := Designated_Type (Nam_Ent);
1173 elsif Is_Access_Type (Etype (Nam_Ent))
1174 and then
1175 (not Is_Entity_Name (Nam)
1176 or else Nkind (N) = N_Procedure_Call_Statement)
1177 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1178 = E_Subprogram_Type
1179 then
1180 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1182 if Is_Entity_Name (Nam) then
1183 Deref := True;
1184 end if;
1185 end if;
1187 -- If the call has been rewritten from a prefixed call, the first
1188 -- parameter has been analyzed, but may need a subsequent
1189 -- dereference, so skip its analysis now.
1191 if N /= Original_Node (N)
1192 and then Nkind (Original_Node (N)) = Nkind (N)
1193 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1194 and then Present (Parameter_Associations (N))
1195 and then Present (Etype (First (Parameter_Associations (N))))
1196 then
1197 Analyze_One_Call
1198 (N, Nam_Ent, False, Success, Skip_First => True);
1199 else
1200 Analyze_One_Call (N, Nam_Ent, False, Success);
1201 end if;
1203 -- If the interpretation succeeds, mark the proper type of the
1204 -- prefix (any valid candidate will do). If not, remove the
1205 -- candidate interpretation. This only needs to be done for
1206 -- overloaded protected operations, for other entities disambi-
1207 -- guation is done directly in Resolve.
1209 if Success then
1210 if Deref
1211 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1212 then
1213 Set_Entity (Nam, It.Nam);
1214 Insert_Explicit_Dereference (Nam);
1215 Set_Etype (Nam, Nam_Ent);
1217 else
1218 Set_Etype (Nam, It.Typ);
1219 end if;
1221 elsif Nkind_In (Name (N), N_Selected_Component,
1222 N_Function_Call)
1223 then
1224 Remove_Interp (X);
1225 end if;
1227 Get_Next_Interp (X, It);
1228 end loop;
1230 -- If the name is the result of a function call, it can only be a
1231 -- call to a function returning an access to subprogram. Insert
1232 -- explicit dereference.
1234 if Nkind (Nam) = N_Function_Call then
1235 Insert_Explicit_Dereference (Nam);
1236 end if;
1238 if Etype (N) = Any_Type then
1240 -- None of the interpretations is compatible with the actuals
1242 Diagnose_Call (N, Nam);
1244 -- Special checks for uninstantiated put routines
1246 if Nkind (N) = N_Procedure_Call_Statement
1247 and then Is_Entity_Name (Nam)
1248 and then Chars (Nam) = Name_Put
1249 and then List_Length (Actuals) = 1
1250 then
1251 declare
1252 Arg : constant Node_Id := First (Actuals);
1253 Typ : Entity_Id;
1255 begin
1256 if Nkind (Arg) = N_Parameter_Association then
1257 Typ := Etype (Explicit_Actual_Parameter (Arg));
1258 else
1259 Typ := Etype (Arg);
1260 end if;
1262 if Is_Signed_Integer_Type (Typ) then
1263 Error_Msg_N
1264 ("possible missing instantiation of "
1265 & "'Text_'I'O.'Integer_'I'O!", Nam);
1267 elsif Is_Modular_Integer_Type (Typ) then
1268 Error_Msg_N
1269 ("possible missing instantiation of "
1270 & "'Text_'I'O.'Modular_'I'O!", Nam);
1272 elsif Is_Floating_Point_Type (Typ) then
1273 Error_Msg_N
1274 ("possible missing instantiation of "
1275 & "'Text_'I'O.'Float_'I'O!", Nam);
1277 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1278 Error_Msg_N
1279 ("possible missing instantiation of "
1280 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1282 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1283 Error_Msg_N
1284 ("possible missing instantiation of "
1285 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1287 elsif Is_Enumeration_Type (Typ) then
1288 Error_Msg_N
1289 ("possible missing instantiation of "
1290 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1291 end if;
1292 end;
1293 end if;
1295 elsif not Is_Overloaded (N)
1296 and then Is_Entity_Name (Nam)
1297 then
1298 -- Resolution yields a single interpretation. Verify that the
1299 -- reference has capitalization consistent with the declaration.
1301 Set_Entity_With_Checks (Nam, Entity (Nam));
1302 Generate_Reference (Entity (Nam), Nam);
1304 Set_Etype (Nam, Etype (Entity (Nam)));
1305 else
1306 Remove_Abstract_Operations (N);
1307 end if;
1309 End_Interp_List;
1310 end if;
1312 -- A call to a ghost subprogram is allowed only in assertion expressions
1313 -- excluding subtype predicates or from within another ghost subprogram.
1315 if Is_Ghost_Subprogram (Get_Subprogram_Entity (N)) then
1316 Check_Ghost_Subprogram_Call;
1317 end if;
1318 end Analyze_Call;
1320 -----------------------------
1321 -- Analyze_Case_Expression --
1322 -----------------------------
1324 procedure Analyze_Case_Expression (N : Node_Id) is
1325 procedure Non_Static_Choice_Error (Choice : Node_Id);
1326 -- Error routine invoked by the generic instantiation below when
1327 -- the case expression has a non static choice.
1329 package Case_Choices_Analysis is new
1330 Generic_Analyze_Choices
1331 (Process_Associated_Node => No_OP);
1332 use Case_Choices_Analysis;
1334 package Case_Choices_Checking is new
1335 Generic_Check_Choices
1336 (Process_Empty_Choice => No_OP,
1337 Process_Non_Static_Choice => Non_Static_Choice_Error,
1338 Process_Associated_Node => No_OP);
1339 use Case_Choices_Checking;
1341 -----------------------------
1342 -- Non_Static_Choice_Error --
1343 -----------------------------
1345 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1346 begin
1347 Flag_Non_Static_Expr
1348 ("choice given in case expression is not static!", Choice);
1349 end Non_Static_Choice_Error;
1351 -- Local variables
1353 Expr : constant Node_Id := Expression (N);
1354 Alt : Node_Id;
1355 Exp_Type : Entity_Id;
1356 Exp_Btype : Entity_Id;
1358 FirstX : Node_Id := Empty;
1359 -- First expression in the case for which there is some type information
1360 -- available, i.e. it is not Any_Type, which can happen because of some
1361 -- error, or from the use of e.g. raise Constraint_Error.
1363 Others_Present : Boolean;
1364 -- Indicates if Others was present
1366 -- Start of processing for Analyze_Case_Expression
1368 begin
1369 if Comes_From_Source (N) then
1370 Check_Compiler_Unit ("case expression", N);
1371 end if;
1373 Analyze_And_Resolve (Expr, Any_Discrete);
1374 Check_Unset_Reference (Expr);
1375 Exp_Type := Etype (Expr);
1376 Exp_Btype := Base_Type (Exp_Type);
1378 Alt := First (Alternatives (N));
1379 while Present (Alt) loop
1380 Analyze (Expression (Alt));
1382 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1383 FirstX := Expression (Alt);
1384 end if;
1386 Next (Alt);
1387 end loop;
1389 -- Get our initial type from the first expression for which we got some
1390 -- useful type information from the expression.
1392 if not Is_Overloaded (FirstX) then
1393 Set_Etype (N, Etype (FirstX));
1395 else
1396 declare
1397 I : Interp_Index;
1398 It : Interp;
1400 begin
1401 Set_Etype (N, Any_Type);
1403 Get_First_Interp (FirstX, I, It);
1404 while Present (It.Nam) loop
1406 -- For each interpretation of the first expression, we only
1407 -- add the interpretation if every other expression in the
1408 -- case expression alternatives has a compatible type.
1410 Alt := Next (First (Alternatives (N)));
1411 while Present (Alt) loop
1412 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1413 Next (Alt);
1414 end loop;
1416 if No (Alt) then
1417 Add_One_Interp (N, It.Typ, It.Typ);
1418 end if;
1420 Get_Next_Interp (I, It);
1421 end loop;
1422 end;
1423 end if;
1425 Exp_Btype := Base_Type (Exp_Type);
1427 -- The expression must be of a discrete type which must be determinable
1428 -- independently of the context in which the expression occurs, but
1429 -- using the fact that the expression must be of a discrete type.
1430 -- Moreover, the type this expression must not be a character literal
1431 -- (which is always ambiguous).
1433 -- If error already reported by Resolve, nothing more to do
1435 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1436 return;
1438 elsif Exp_Btype = Any_Character then
1439 Error_Msg_N
1440 ("character literal as case expression is ambiguous", Expr);
1441 return;
1442 end if;
1444 -- If the case expression is a formal object of mode in out, then
1445 -- treat it as having a nonstatic subtype by forcing use of the base
1446 -- type (which has to get passed to Check_Case_Choices below). Also
1447 -- use base type when the case expression is parenthesized.
1449 if Paren_Count (Expr) > 0
1450 or else (Is_Entity_Name (Expr)
1451 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1452 then
1453 Exp_Type := Exp_Btype;
1454 end if;
1456 -- The case expression alternatives cover the range of a static subtype
1457 -- subject to aspect Static_Predicate. Do not check the choices when the
1458 -- case expression has not been fully analyzed yet because this may lead
1459 -- to bogus errors.
1461 if Is_OK_Static_Subtype (Exp_Type)
1462 and then Has_Static_Predicate_Aspect (Exp_Type)
1463 and then In_Spec_Expression
1464 then
1465 null;
1467 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1469 else
1470 Analyze_Choices (Alternatives (N), Exp_Type);
1471 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1472 end if;
1474 if Exp_Type = Universal_Integer and then not Others_Present then
1475 Error_Msg_N
1476 ("case on universal integer requires OTHERS choice", Expr);
1477 end if;
1478 end Analyze_Case_Expression;
1480 ---------------------------
1481 -- Analyze_Comparison_Op --
1482 ---------------------------
1484 procedure Analyze_Comparison_Op (N : Node_Id) is
1485 L : constant Node_Id := Left_Opnd (N);
1486 R : constant Node_Id := Right_Opnd (N);
1487 Op_Id : Entity_Id := Entity (N);
1489 begin
1490 Set_Etype (N, Any_Type);
1491 Candidate_Type := Empty;
1493 Analyze_Expression (L);
1494 Analyze_Expression (R);
1496 if Present (Op_Id) then
1497 if Ekind (Op_Id) = E_Operator then
1498 Find_Comparison_Types (L, R, Op_Id, N);
1499 else
1500 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1501 end if;
1503 if Is_Overloaded (L) then
1504 Set_Etype (L, Intersect_Types (L, R));
1505 end if;
1507 else
1508 Op_Id := Get_Name_Entity_Id (Chars (N));
1509 while Present (Op_Id) loop
1510 if Ekind (Op_Id) = E_Operator then
1511 Find_Comparison_Types (L, R, Op_Id, N);
1512 else
1513 Analyze_User_Defined_Binary_Op (N, Op_Id);
1514 end if;
1516 Op_Id := Homonym (Op_Id);
1517 end loop;
1518 end if;
1520 Operator_Check (N);
1521 end Analyze_Comparison_Op;
1523 ---------------------------
1524 -- Analyze_Concatenation --
1525 ---------------------------
1527 procedure Analyze_Concatenation (N : Node_Id) is
1529 -- We wish to avoid deep recursion, because concatenations are often
1530 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1531 -- operands nonrecursively until we find something that is not a
1532 -- concatenation (A in this case), or has already been analyzed. We
1533 -- analyze that, and then walk back up the tree following Parent
1534 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1535 -- work at each level. The Parent pointers allow us to avoid recursion,
1536 -- and thus avoid running out of memory.
1538 NN : Node_Id := N;
1539 L : Node_Id;
1541 begin
1542 Candidate_Type := Empty;
1544 -- The following code is equivalent to:
1546 -- Set_Etype (N, Any_Type);
1547 -- Analyze_Expression (Left_Opnd (N));
1548 -- Analyze_Concatenation_Rest (N);
1550 -- where the Analyze_Expression call recurses back here if the left
1551 -- operand is a concatenation.
1553 -- Walk down left operands
1555 loop
1556 Set_Etype (NN, Any_Type);
1557 L := Left_Opnd (NN);
1558 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1559 NN := L;
1560 end loop;
1562 -- Now (given the above example) NN is A&B and L is A
1564 -- First analyze L ...
1566 Analyze_Expression (L);
1568 -- ... then walk NN back up until we reach N (where we started), calling
1569 -- Analyze_Concatenation_Rest along the way.
1571 loop
1572 Analyze_Concatenation_Rest (NN);
1573 exit when NN = N;
1574 NN := Parent (NN);
1575 end loop;
1576 end Analyze_Concatenation;
1578 --------------------------------
1579 -- Analyze_Concatenation_Rest --
1580 --------------------------------
1582 -- If the only one-dimensional array type in scope is String,
1583 -- this is the resulting type of the operation. Otherwise there
1584 -- will be a concatenation operation defined for each user-defined
1585 -- one-dimensional array.
1587 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1588 L : constant Node_Id := Left_Opnd (N);
1589 R : constant Node_Id := Right_Opnd (N);
1590 Op_Id : Entity_Id := Entity (N);
1591 LT : Entity_Id;
1592 RT : Entity_Id;
1594 begin
1595 Analyze_Expression (R);
1597 -- If the entity is present, the node appears in an instance, and
1598 -- denotes a predefined concatenation operation. The resulting type is
1599 -- obtained from the arguments when possible. If the arguments are
1600 -- aggregates, the array type and the concatenation type must be
1601 -- visible.
1603 if Present (Op_Id) then
1604 if Ekind (Op_Id) = E_Operator then
1605 LT := Base_Type (Etype (L));
1606 RT := Base_Type (Etype (R));
1608 if Is_Array_Type (LT)
1609 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1610 then
1611 Add_One_Interp (N, Op_Id, LT);
1613 elsif Is_Array_Type (RT)
1614 and then LT = Base_Type (Component_Type (RT))
1615 then
1616 Add_One_Interp (N, Op_Id, RT);
1618 -- If one operand is a string type or a user-defined array type,
1619 -- and the other is a literal, result is of the specific type.
1621 elsif
1622 (Root_Type (LT) = Standard_String
1623 or else Scope (LT) /= Standard_Standard)
1624 and then Etype (R) = Any_String
1625 then
1626 Add_One_Interp (N, Op_Id, LT);
1628 elsif
1629 (Root_Type (RT) = Standard_String
1630 or else Scope (RT) /= Standard_Standard)
1631 and then Etype (L) = Any_String
1632 then
1633 Add_One_Interp (N, Op_Id, RT);
1635 elsif not Is_Generic_Type (Etype (Op_Id)) then
1636 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1638 else
1639 -- Type and its operations must be visible
1641 Set_Entity (N, Empty);
1642 Analyze_Concatenation (N);
1643 end if;
1645 else
1646 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1647 end if;
1649 else
1650 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1651 while Present (Op_Id) loop
1652 if Ekind (Op_Id) = E_Operator then
1654 -- Do not consider operators declared in dead code, they can
1655 -- not be part of the resolution.
1657 if Is_Eliminated (Op_Id) then
1658 null;
1659 else
1660 Find_Concatenation_Types (L, R, Op_Id, N);
1661 end if;
1663 else
1664 Analyze_User_Defined_Binary_Op (N, Op_Id);
1665 end if;
1667 Op_Id := Homonym (Op_Id);
1668 end loop;
1669 end if;
1671 Operator_Check (N);
1672 end Analyze_Concatenation_Rest;
1674 -------------------------
1675 -- Analyze_Equality_Op --
1676 -------------------------
1678 procedure Analyze_Equality_Op (N : Node_Id) is
1679 Loc : constant Source_Ptr := Sloc (N);
1680 L : constant Node_Id := Left_Opnd (N);
1681 R : constant Node_Id := Right_Opnd (N);
1682 Op_Id : Entity_Id;
1684 begin
1685 Set_Etype (N, Any_Type);
1686 Candidate_Type := Empty;
1688 Analyze_Expression (L);
1689 Analyze_Expression (R);
1691 -- If the entity is set, the node is a generic instance with a non-local
1692 -- reference to the predefined operator or to a user-defined function.
1693 -- It can also be an inequality that is expanded into the negation of a
1694 -- call to a user-defined equality operator.
1696 -- For the predefined case, the result is Boolean, regardless of the
1697 -- type of the operands. The operands may even be limited, if they are
1698 -- generic actuals. If they are overloaded, label the left argument with
1699 -- the common type that must be present, or with the type of the formal
1700 -- of the user-defined function.
1702 if Present (Entity (N)) then
1703 Op_Id := Entity (N);
1705 if Ekind (Op_Id) = E_Operator then
1706 Add_One_Interp (N, Op_Id, Standard_Boolean);
1707 else
1708 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1709 end if;
1711 if Is_Overloaded (L) then
1712 if Ekind (Op_Id) = E_Operator then
1713 Set_Etype (L, Intersect_Types (L, R));
1714 else
1715 Set_Etype (L, Etype (First_Formal (Op_Id)));
1716 end if;
1717 end if;
1719 else
1720 Op_Id := Get_Name_Entity_Id (Chars (N));
1721 while Present (Op_Id) loop
1722 if Ekind (Op_Id) = E_Operator then
1723 Find_Equality_Types (L, R, Op_Id, N);
1724 else
1725 Analyze_User_Defined_Binary_Op (N, Op_Id);
1726 end if;
1728 Op_Id := Homonym (Op_Id);
1729 end loop;
1730 end if;
1732 -- If there was no match, and the operator is inequality, this may
1733 -- be a case where inequality has not been made explicit, as for
1734 -- tagged types. Analyze the node as the negation of an equality
1735 -- operation. This cannot be done earlier, because before analysis
1736 -- we cannot rule out the presence of an explicit inequality.
1738 if Etype (N) = Any_Type
1739 and then Nkind (N) = N_Op_Ne
1740 then
1741 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1742 while Present (Op_Id) loop
1743 if Ekind (Op_Id) = E_Operator then
1744 Find_Equality_Types (L, R, Op_Id, N);
1745 else
1746 Analyze_User_Defined_Binary_Op (N, Op_Id);
1747 end if;
1749 Op_Id := Homonym (Op_Id);
1750 end loop;
1752 if Etype (N) /= Any_Type then
1753 Op_Id := Entity (N);
1755 Rewrite (N,
1756 Make_Op_Not (Loc,
1757 Right_Opnd =>
1758 Make_Op_Eq (Loc,
1759 Left_Opnd => Left_Opnd (N),
1760 Right_Opnd => Right_Opnd (N))));
1762 Set_Entity (Right_Opnd (N), Op_Id);
1763 Analyze (N);
1764 end if;
1765 end if;
1767 Operator_Check (N);
1768 end Analyze_Equality_Op;
1770 ----------------------------------
1771 -- Analyze_Explicit_Dereference --
1772 ----------------------------------
1774 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1775 Loc : constant Source_Ptr := Sloc (N);
1776 P : constant Node_Id := Prefix (N);
1777 T : Entity_Id;
1778 I : Interp_Index;
1779 It : Interp;
1780 New_N : Node_Id;
1782 function Is_Function_Type return Boolean;
1783 -- Check whether node may be interpreted as an implicit function call
1785 ----------------------
1786 -- Is_Function_Type --
1787 ----------------------
1789 function Is_Function_Type return Boolean is
1790 I : Interp_Index;
1791 It : Interp;
1793 begin
1794 if not Is_Overloaded (N) then
1795 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1796 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1798 else
1799 Get_First_Interp (N, I, It);
1800 while Present (It.Nam) loop
1801 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1802 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1803 then
1804 return False;
1805 end if;
1807 Get_Next_Interp (I, It);
1808 end loop;
1810 return True;
1811 end if;
1812 end Is_Function_Type;
1814 -- Start of processing for Analyze_Explicit_Dereference
1816 begin
1817 -- If source node, check SPARK restriction. We guard this with the
1818 -- source node check, because ???
1820 if Comes_From_Source (N) then
1821 Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1822 end if;
1824 -- In formal verification mode, keep track of all reads and writes
1825 -- through explicit dereferences.
1827 if GNATprove_Mode then
1828 SPARK_Specific.Generate_Dereference (N);
1829 end if;
1831 Analyze (P);
1832 Set_Etype (N, Any_Type);
1834 -- Test for remote access to subprogram type, and if so return
1835 -- after rewriting the original tree.
1837 if Remote_AST_E_Dereference (P) then
1838 return;
1839 end if;
1841 -- Normal processing for other than remote access to subprogram type
1843 if not Is_Overloaded (P) then
1844 if Is_Access_Type (Etype (P)) then
1846 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1847 -- avoid other problems caused by the Private_Subtype and it is
1848 -- safe to go to the Base_Type because this is the same as
1849 -- converting the access value to its Base_Type.
1851 declare
1852 DT : Entity_Id := Designated_Type (Etype (P));
1854 begin
1855 if Ekind (DT) = E_Private_Subtype
1856 and then Is_For_Access_Subtype (DT)
1857 then
1858 DT := Base_Type (DT);
1859 end if;
1861 -- An explicit dereference is a legal occurrence of an
1862 -- incomplete type imported through a limited_with clause,
1863 -- if the full view is visible.
1865 if From_Limited_With (DT)
1866 and then not From_Limited_With (Scope (DT))
1867 and then
1868 (Is_Immediately_Visible (Scope (DT))
1869 or else
1870 (Is_Child_Unit (Scope (DT))
1871 and then Is_Visible_Lib_Unit (Scope (DT))))
1872 then
1873 Set_Etype (N, Available_View (DT));
1875 else
1876 Set_Etype (N, DT);
1877 end if;
1878 end;
1880 elsif Etype (P) /= Any_Type then
1881 Error_Msg_N ("prefix of dereference must be an access type", N);
1882 return;
1883 end if;
1885 else
1886 Get_First_Interp (P, I, It);
1887 while Present (It.Nam) loop
1888 T := It.Typ;
1890 if Is_Access_Type (T) then
1891 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1892 end if;
1894 Get_Next_Interp (I, It);
1895 end loop;
1897 -- Error if no interpretation of the prefix has an access type
1899 if Etype (N) = Any_Type then
1900 Error_Msg_N
1901 ("access type required in prefix of explicit dereference", P);
1902 Set_Etype (N, Any_Type);
1903 return;
1904 end if;
1905 end if;
1907 if Is_Function_Type
1908 and then Nkind (Parent (N)) /= N_Indexed_Component
1910 and then (Nkind (Parent (N)) /= N_Function_Call
1911 or else N /= Name (Parent (N)))
1913 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1914 or else N /= Name (Parent (N)))
1916 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1917 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1918 or else
1919 (Attribute_Name (Parent (N)) /= Name_Address
1920 and then
1921 Attribute_Name (Parent (N)) /= Name_Access))
1922 then
1923 -- Name is a function call with no actuals, in a context that
1924 -- requires deproceduring (including as an actual in an enclosing
1925 -- function or procedure call). There are some pathological cases
1926 -- where the prefix might include functions that return access to
1927 -- subprograms and others that return a regular type. Disambiguation
1928 -- of those has to take place in Resolve.
1930 New_N :=
1931 Make_Function_Call (Loc,
1932 Name => Make_Explicit_Dereference (Loc, P),
1933 Parameter_Associations => New_List);
1935 -- If the prefix is overloaded, remove operations that have formals,
1936 -- we know that this is a parameterless call.
1938 if Is_Overloaded (P) then
1939 Get_First_Interp (P, I, It);
1940 while Present (It.Nam) loop
1941 T := It.Typ;
1943 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1944 Set_Etype (P, T);
1945 else
1946 Remove_Interp (I);
1947 end if;
1949 Get_Next_Interp (I, It);
1950 end loop;
1951 end if;
1953 Rewrite (N, New_N);
1954 Analyze (N);
1956 elsif not Is_Function_Type
1957 and then Is_Overloaded (N)
1958 then
1959 -- The prefix may include access to subprograms and other access
1960 -- types. If the context selects the interpretation that is a
1961 -- function call (not a procedure call) we cannot rewrite the node
1962 -- yet, but we include the result of the call interpretation.
1964 Get_First_Interp (N, I, It);
1965 while Present (It.Nam) loop
1966 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1967 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1968 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1969 then
1970 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1971 end if;
1973 Get_Next_Interp (I, It);
1974 end loop;
1975 end if;
1977 -- A value of remote access-to-class-wide must not be dereferenced
1978 -- (RM E.2.2(16)).
1980 Validate_Remote_Access_To_Class_Wide_Type (N);
1981 end Analyze_Explicit_Dereference;
1983 ------------------------
1984 -- Analyze_Expression --
1985 ------------------------
1987 procedure Analyze_Expression (N : Node_Id) is
1988 begin
1990 -- If the expression is an indexed component that will be rewritten
1991 -- as a container indexing, it has already been analyzed.
1993 if Nkind (N) = N_Indexed_Component
1994 and then Present (Generalized_Indexing (N))
1995 then
1996 null;
1998 else
1999 Analyze (N);
2000 Check_Parameterless_Call (N);
2001 end if;
2002 end Analyze_Expression;
2004 -------------------------------------
2005 -- Analyze_Expression_With_Actions --
2006 -------------------------------------
2008 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2009 A : Node_Id;
2011 begin
2012 A := First (Actions (N));
2013 while Present (A) loop
2014 Analyze (A);
2015 Next (A);
2016 end loop;
2018 Analyze_Expression (Expression (N));
2019 Set_Etype (N, Etype (Expression (N)));
2020 end Analyze_Expression_With_Actions;
2022 ---------------------------
2023 -- Analyze_If_Expression --
2024 ---------------------------
2026 procedure Analyze_If_Expression (N : Node_Id) is
2027 Condition : constant Node_Id := First (Expressions (N));
2028 Then_Expr : constant Node_Id := Next (Condition);
2029 Else_Expr : Node_Id;
2031 begin
2032 -- Defend against error of missing expressions from previous error
2034 if No (Then_Expr) then
2035 Check_Error_Detected;
2036 return;
2037 end if;
2039 if Comes_From_Source (N) then
2040 Check_SPARK_Restriction ("if expression is not allowed", N);
2041 end if;
2043 Else_Expr := Next (Then_Expr);
2045 if Comes_From_Source (N) then
2046 Check_Compiler_Unit ("if expression", N);
2047 end if;
2049 -- Analyze and resolve the condition. We need to resolve this now so
2050 -- that it gets folded to True/False if possible, before we analyze
2051 -- the THEN/ELSE branches, because when analyzing these branches, we
2052 -- may call Is_Statically_Unevaluated, which expects the condition of
2053 -- an enclosing IF to have been analyze/resolved/evaluated.
2055 Analyze_Expression (Condition);
2056 Resolve (Condition, Any_Boolean);
2058 -- Analyze THEN expression and (if present) ELSE expression. For those
2059 -- we delay resolution in the normal manner, because of overloading etc.
2061 Analyze_Expression (Then_Expr);
2063 if Present (Else_Expr) then
2064 Analyze_Expression (Else_Expr);
2065 end if;
2067 -- If then expression not overloaded, then that decides the type
2069 if not Is_Overloaded (Then_Expr) then
2070 Set_Etype (N, Etype (Then_Expr));
2072 -- Case where then expression is overloaded
2074 else
2075 declare
2076 I : Interp_Index;
2077 It : Interp;
2079 begin
2080 Set_Etype (N, Any_Type);
2082 -- Shouldn't the following statement be down in the ELSE of the
2083 -- following loop? ???
2085 Get_First_Interp (Then_Expr, I, It);
2087 -- if no Else_Expression the conditional must be boolean
2089 if No (Else_Expr) then
2090 Set_Etype (N, Standard_Boolean);
2092 -- Else_Expression Present. For each possible intepretation of
2093 -- the Then_Expression, add it only if the Else_Expression has
2094 -- a compatible type.
2096 else
2097 while Present (It.Nam) loop
2098 if Has_Compatible_Type (Else_Expr, It.Typ) then
2099 Add_One_Interp (N, It.Typ, It.Typ);
2100 end if;
2102 Get_Next_Interp (I, It);
2103 end loop;
2104 end if;
2105 end;
2106 end if;
2107 end Analyze_If_Expression;
2109 ------------------------------------
2110 -- Analyze_Indexed_Component_Form --
2111 ------------------------------------
2113 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2114 P : constant Node_Id := Prefix (N);
2115 Exprs : constant List_Id := Expressions (N);
2116 Exp : Node_Id;
2117 P_T : Entity_Id;
2118 E : Node_Id;
2119 U_N : Entity_Id;
2121 procedure Process_Function_Call;
2122 -- Prefix in indexed component form is an overloadable entity,
2123 -- so the node is a function call. Reformat it as such.
2125 procedure Process_Indexed_Component;
2126 -- Prefix in indexed component form is actually an indexed component.
2127 -- This routine processes it, knowing that the prefix is already
2128 -- resolved.
2130 procedure Process_Indexed_Component_Or_Slice;
2131 -- An indexed component with a single index may designate a slice if
2132 -- the index is a subtype mark. This routine disambiguates these two
2133 -- cases by resolving the prefix to see if it is a subtype mark.
2135 procedure Process_Overloaded_Indexed_Component;
2136 -- If the prefix of an indexed component is overloaded, the proper
2137 -- interpretation is selected by the index types and the context.
2139 ---------------------------
2140 -- Process_Function_Call --
2141 ---------------------------
2143 procedure Process_Function_Call is
2144 Actual : Node_Id;
2146 begin
2147 Change_Node (N, N_Function_Call);
2148 Set_Name (N, P);
2149 Set_Parameter_Associations (N, Exprs);
2151 -- Analyze actuals prior to analyzing the call itself
2153 Actual := First (Parameter_Associations (N));
2154 while Present (Actual) loop
2155 Analyze (Actual);
2156 Check_Parameterless_Call (Actual);
2158 -- Move to next actual. Note that we use Next, not Next_Actual
2159 -- here. The reason for this is a bit subtle. If a function call
2160 -- includes named associations, the parser recognizes the node as
2161 -- a call, and it is analyzed as such. If all associations are
2162 -- positional, the parser builds an indexed_component node, and
2163 -- it is only after analysis of the prefix that the construct
2164 -- is recognized as a call, in which case Process_Function_Call
2165 -- rewrites the node and analyzes the actuals. If the list of
2166 -- actuals is malformed, the parser may leave the node as an
2167 -- indexed component (despite the presence of named associations).
2168 -- The iterator Next_Actual is equivalent to Next if the list is
2169 -- positional, but follows the normalized chain of actuals when
2170 -- named associations are present. In this case normalization has
2171 -- not taken place, and actuals remain unanalyzed, which leads to
2172 -- subsequent crashes or loops if there is an attempt to continue
2173 -- analysis of the program.
2175 Next (Actual);
2176 end loop;
2178 Analyze_Call (N);
2179 end Process_Function_Call;
2181 -------------------------------
2182 -- Process_Indexed_Component --
2183 -------------------------------
2185 procedure Process_Indexed_Component is
2186 Exp : Node_Id;
2187 Array_Type : Entity_Id;
2188 Index : Node_Id;
2189 Pent : Entity_Id := Empty;
2191 begin
2192 Exp := First (Exprs);
2194 if Is_Overloaded (P) then
2195 Process_Overloaded_Indexed_Component;
2197 else
2198 Array_Type := Etype (P);
2200 if Is_Entity_Name (P) then
2201 Pent := Entity (P);
2202 elsif Nkind (P) = N_Selected_Component
2203 and then Is_Entity_Name (Selector_Name (P))
2204 then
2205 Pent := Entity (Selector_Name (P));
2206 end if;
2208 -- Prefix must be appropriate for an array type, taking into
2209 -- account a possible implicit dereference.
2211 if Is_Access_Type (Array_Type) then
2212 Error_Msg_NW
2213 (Warn_On_Dereference, "?d?implicit dereference", N);
2214 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2215 end if;
2217 if Is_Array_Type (Array_Type) then
2218 null;
2220 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2221 Analyze (Exp);
2222 Set_Etype (N, Any_Type);
2224 if not Has_Compatible_Type
2225 (Exp, Entry_Index_Type (Pent))
2226 then
2227 Error_Msg_N ("invalid index type in entry name", N);
2229 elsif Present (Next (Exp)) then
2230 Error_Msg_N ("too many subscripts in entry reference", N);
2232 else
2233 Set_Etype (N, Etype (P));
2234 end if;
2236 return;
2238 elsif Is_Record_Type (Array_Type)
2239 and then Remote_AST_I_Dereference (P)
2240 then
2241 return;
2243 elsif Try_Container_Indexing (N, P, Exprs) then
2244 return;
2246 elsif Array_Type = Any_Type then
2247 Set_Etype (N, Any_Type);
2249 -- In most cases the analysis of the prefix will have emitted
2250 -- an error already, but if the prefix may be interpreted as a
2251 -- call in prefixed notation, the report is left to the caller.
2252 -- To prevent cascaded errors, report only if no previous ones.
2254 if Serious_Errors_Detected = 0 then
2255 Error_Msg_N ("invalid prefix in indexed component", P);
2257 if Nkind (P) = N_Expanded_Name then
2258 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2259 end if;
2260 end if;
2262 return;
2264 -- Here we definitely have a bad indexing
2266 else
2267 if Nkind (Parent (N)) = N_Requeue_Statement
2268 and then Present (Pent) and then Ekind (Pent) = E_Entry
2269 then
2270 Error_Msg_N
2271 ("REQUEUE does not permit parameters", First (Exprs));
2273 elsif Is_Entity_Name (P)
2274 and then Etype (P) = Standard_Void_Type
2275 then
2276 Error_Msg_NE ("incorrect use of&", P, Entity (P));
2278 else
2279 Error_Msg_N ("array type required in indexed component", P);
2280 end if;
2282 Set_Etype (N, Any_Type);
2283 return;
2284 end if;
2286 Index := First_Index (Array_Type);
2287 while Present (Index) and then Present (Exp) loop
2288 if not Has_Compatible_Type (Exp, Etype (Index)) then
2289 Wrong_Type (Exp, Etype (Index));
2290 Set_Etype (N, Any_Type);
2291 return;
2292 end if;
2294 Next_Index (Index);
2295 Next (Exp);
2296 end loop;
2298 Set_Etype (N, Component_Type (Array_Type));
2299 Check_Implicit_Dereference (N, Etype (N));
2301 if Present (Index) then
2302 Error_Msg_N
2303 ("too few subscripts in array reference", First (Exprs));
2305 elsif Present (Exp) then
2306 Error_Msg_N ("too many subscripts in array reference", Exp);
2307 end if;
2308 end if;
2309 end Process_Indexed_Component;
2311 ----------------------------------------
2312 -- Process_Indexed_Component_Or_Slice --
2313 ----------------------------------------
2315 procedure Process_Indexed_Component_Or_Slice is
2316 begin
2317 Exp := First (Exprs);
2318 while Present (Exp) loop
2319 Analyze_Expression (Exp);
2320 Next (Exp);
2321 end loop;
2323 Exp := First (Exprs);
2325 -- If one index is present, and it is a subtype name, then the
2326 -- node denotes a slice (note that the case of an explicit range
2327 -- for a slice was already built as an N_Slice node in the first
2328 -- place, so that case is not handled here).
2330 -- We use a replace rather than a rewrite here because this is one
2331 -- of the cases in which the tree built by the parser is plain wrong.
2333 if No (Next (Exp))
2334 and then Is_Entity_Name (Exp)
2335 and then Is_Type (Entity (Exp))
2336 then
2337 Replace (N,
2338 Make_Slice (Sloc (N),
2339 Prefix => P,
2340 Discrete_Range => New_Copy (Exp)));
2341 Analyze (N);
2343 -- Otherwise (more than one index present, or single index is not
2344 -- a subtype name), then we have the indexed component case.
2346 else
2347 Process_Indexed_Component;
2348 end if;
2349 end Process_Indexed_Component_Or_Slice;
2351 ------------------------------------------
2352 -- Process_Overloaded_Indexed_Component --
2353 ------------------------------------------
2355 procedure Process_Overloaded_Indexed_Component is
2356 Exp : Node_Id;
2357 I : Interp_Index;
2358 It : Interp;
2359 Typ : Entity_Id;
2360 Index : Node_Id;
2361 Found : Boolean;
2363 begin
2364 Set_Etype (N, Any_Type);
2366 Get_First_Interp (P, I, It);
2367 while Present (It.Nam) loop
2368 Typ := It.Typ;
2370 if Is_Access_Type (Typ) then
2371 Typ := Designated_Type (Typ);
2372 Error_Msg_NW
2373 (Warn_On_Dereference, "?d?implicit dereference", N);
2374 end if;
2376 if Is_Array_Type (Typ) then
2378 -- Got a candidate: verify that index types are compatible
2380 Index := First_Index (Typ);
2381 Found := True;
2382 Exp := First (Exprs);
2383 while Present (Index) and then Present (Exp) loop
2384 if Has_Compatible_Type (Exp, Etype (Index)) then
2385 null;
2386 else
2387 Found := False;
2388 Remove_Interp (I);
2389 exit;
2390 end if;
2392 Next_Index (Index);
2393 Next (Exp);
2394 end loop;
2396 if Found and then No (Index) and then No (Exp) then
2397 declare
2398 CT : constant Entity_Id :=
2399 Base_Type (Component_Type (Typ));
2400 begin
2401 Add_One_Interp (N, CT, CT);
2402 Check_Implicit_Dereference (N, CT);
2403 end;
2404 end if;
2406 elsif Try_Container_Indexing (N, P, Exprs) then
2407 return;
2409 end if;
2411 Get_Next_Interp (I, It);
2412 end loop;
2414 if Etype (N) = Any_Type then
2415 Error_Msg_N ("no legal interpretation for indexed component", N);
2416 Set_Is_Overloaded (N, False);
2417 end if;
2419 End_Interp_List;
2420 end Process_Overloaded_Indexed_Component;
2422 -- Start of processing for Analyze_Indexed_Component_Form
2424 begin
2425 -- Get name of array, function or type
2427 Analyze (P);
2429 -- If P is an explicit dereference whose prefix is of a remote access-
2430 -- to-subprogram type, then N has already been rewritten as a subprogram
2431 -- call and analyzed.
2433 if Nkind (N) in N_Subprogram_Call then
2434 return;
2436 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2437 -- the indexed component denotes a loop name, the indexed form is turned
2438 -- into an attribute reference.
2440 elsif Nkind (N) = N_Attribute_Reference
2441 and then Attribute_Name (N) = Name_Loop_Entry
2442 then
2443 return;
2444 end if;
2446 pragma Assert (Nkind (N) = N_Indexed_Component);
2448 P_T := Base_Type (Etype (P));
2450 if Is_Entity_Name (P) and then Present (Entity (P)) then
2451 U_N := Entity (P);
2453 if Is_Type (U_N) then
2455 -- Reformat node as a type conversion
2457 E := Remove_Head (Exprs);
2459 if Present (First (Exprs)) then
2460 Error_Msg_N
2461 ("argument of type conversion must be single expression", N);
2462 end if;
2464 Change_Node (N, N_Type_Conversion);
2465 Set_Subtype_Mark (N, P);
2466 Set_Etype (N, U_N);
2467 Set_Expression (N, E);
2469 -- After changing the node, call for the specific Analysis
2470 -- routine directly, to avoid a double call to the expander.
2472 Analyze_Type_Conversion (N);
2473 return;
2474 end if;
2476 if Is_Overloadable (U_N) then
2477 Process_Function_Call;
2479 elsif Ekind (Etype (P)) = E_Subprogram_Type
2480 or else (Is_Access_Type (Etype (P))
2481 and then
2482 Ekind (Designated_Type (Etype (P))) =
2483 E_Subprogram_Type)
2484 then
2485 -- Call to access_to-subprogram with possible implicit dereference
2487 Process_Function_Call;
2489 elsif Is_Generic_Subprogram (U_N) then
2491 -- A common beginner's (or C++ templates fan) error
2493 Error_Msg_N ("generic subprogram cannot be called", N);
2494 Set_Etype (N, Any_Type);
2495 return;
2497 else
2498 Process_Indexed_Component_Or_Slice;
2499 end if;
2501 -- If not an entity name, prefix is an expression that may denote
2502 -- an array or an access-to-subprogram.
2504 else
2505 if Ekind (P_T) = E_Subprogram_Type
2506 or else (Is_Access_Type (P_T)
2507 and then
2508 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2509 then
2510 Process_Function_Call;
2512 elsif Nkind (P) = N_Selected_Component
2513 and then Present (Entity (Selector_Name (P)))
2514 and then Is_Overloadable (Entity (Selector_Name (P)))
2515 then
2516 Process_Function_Call;
2518 -- In ASIS mode within a generic, a prefixed call is analyzed and
2519 -- partially rewritten but the original indexed component has not
2520 -- yet been rewritten as a call. Perform the replacement now.
2522 elsif Nkind (P) = N_Selected_Component
2523 and then Nkind (Parent (P)) = N_Function_Call
2524 and then ASIS_Mode
2525 then
2526 Rewrite (N, Parent (P));
2527 Analyze (N);
2529 else
2530 -- Indexed component, slice, or a call to a member of a family
2531 -- entry, which will be converted to an entry call later.
2533 Process_Indexed_Component_Or_Slice;
2534 end if;
2535 end if;
2537 Analyze_Dimension (N);
2538 end Analyze_Indexed_Component_Form;
2540 ------------------------
2541 -- Analyze_Logical_Op --
2542 ------------------------
2544 procedure Analyze_Logical_Op (N : Node_Id) is
2545 L : constant Node_Id := Left_Opnd (N);
2546 R : constant Node_Id := Right_Opnd (N);
2547 Op_Id : Entity_Id := Entity (N);
2549 begin
2550 Set_Etype (N, Any_Type);
2551 Candidate_Type := Empty;
2553 Analyze_Expression (L);
2554 Analyze_Expression (R);
2556 if Present (Op_Id) then
2558 if Ekind (Op_Id) = E_Operator then
2559 Find_Boolean_Types (L, R, Op_Id, N);
2560 else
2561 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2562 end if;
2564 else
2565 Op_Id := Get_Name_Entity_Id (Chars (N));
2566 while Present (Op_Id) loop
2567 if Ekind (Op_Id) = E_Operator then
2568 Find_Boolean_Types (L, R, Op_Id, N);
2569 else
2570 Analyze_User_Defined_Binary_Op (N, Op_Id);
2571 end if;
2573 Op_Id := Homonym (Op_Id);
2574 end loop;
2575 end if;
2577 Operator_Check (N);
2578 end Analyze_Logical_Op;
2580 ---------------------------
2581 -- Analyze_Membership_Op --
2582 ---------------------------
2584 procedure Analyze_Membership_Op (N : Node_Id) is
2585 Loc : constant Source_Ptr := Sloc (N);
2586 L : constant Node_Id := Left_Opnd (N);
2587 R : constant Node_Id := Right_Opnd (N);
2589 Index : Interp_Index;
2590 It : Interp;
2591 Found : Boolean := False;
2592 I_F : Interp_Index;
2593 T_F : Entity_Id;
2595 procedure Try_One_Interp (T1 : Entity_Id);
2596 -- Routine to try one proposed interpretation. Note that the context
2597 -- of the operation plays no role in resolving the arguments, so that
2598 -- if there is more than one interpretation of the operands that is
2599 -- compatible with a membership test, the operation is ambiguous.
2601 --------------------
2602 -- Try_One_Interp --
2603 --------------------
2605 procedure Try_One_Interp (T1 : Entity_Id) is
2606 begin
2607 if Has_Compatible_Type (R, T1) then
2608 if Found
2609 and then Base_Type (T1) /= Base_Type (T_F)
2610 then
2611 It := Disambiguate (L, I_F, Index, Any_Type);
2613 if It = No_Interp then
2614 Ambiguous_Operands (N);
2615 Set_Etype (L, Any_Type);
2616 return;
2618 else
2619 T_F := It.Typ;
2620 end if;
2622 else
2623 Found := True;
2624 T_F := T1;
2625 I_F := Index;
2626 end if;
2628 Set_Etype (L, T_F);
2629 end if;
2630 end Try_One_Interp;
2632 procedure Analyze_Set_Membership;
2633 -- If a set of alternatives is present, analyze each and find the
2634 -- common type to which they must all resolve.
2636 ----------------------------
2637 -- Analyze_Set_Membership --
2638 ----------------------------
2640 procedure Analyze_Set_Membership is
2641 Alt : Node_Id;
2642 Index : Interp_Index;
2643 It : Interp;
2644 Candidate_Interps : Node_Id;
2645 Common_Type : Entity_Id := Empty;
2647 begin
2648 if Comes_From_Source (N) then
2649 Check_Compiler_Unit ("set membership", N);
2650 end if;
2652 Analyze (L);
2653 Candidate_Interps := L;
2655 if not Is_Overloaded (L) then
2656 Common_Type := Etype (L);
2658 Alt := First (Alternatives (N));
2659 while Present (Alt) loop
2660 Analyze (Alt);
2662 if not Has_Compatible_Type (Alt, Common_Type) then
2663 Wrong_Type (Alt, Common_Type);
2664 end if;
2666 Next (Alt);
2667 end loop;
2669 else
2670 Alt := First (Alternatives (N));
2671 while Present (Alt) loop
2672 Analyze (Alt);
2673 if not Is_Overloaded (Alt) then
2674 Common_Type := Etype (Alt);
2676 else
2677 Get_First_Interp (Alt, Index, It);
2678 while Present (It.Typ) loop
2679 if not
2680 Has_Compatible_Type (Candidate_Interps, It.Typ)
2681 then
2682 Remove_Interp (Index);
2683 end if;
2685 Get_Next_Interp (Index, It);
2686 end loop;
2688 Get_First_Interp (Alt, Index, It);
2690 if No (It.Typ) then
2691 Error_Msg_N ("alternative has no legal type", Alt);
2692 return;
2693 end if;
2695 -- If alternative is not overloaded, we have a unique type
2696 -- for all of them.
2698 Set_Etype (Alt, It.Typ);
2699 Get_Next_Interp (Index, It);
2701 if No (It.Typ) then
2702 Set_Is_Overloaded (Alt, False);
2703 Common_Type := Etype (Alt);
2704 end if;
2706 Candidate_Interps := Alt;
2707 end if;
2709 Next (Alt);
2710 end loop;
2711 end if;
2713 Set_Etype (N, Standard_Boolean);
2715 if Present (Common_Type) then
2716 Set_Etype (L, Common_Type);
2717 Set_Is_Overloaded (L, False);
2719 else
2720 Error_Msg_N ("cannot resolve membership operation", N);
2721 end if;
2722 end Analyze_Set_Membership;
2724 -- Start of processing for Analyze_Membership_Op
2726 begin
2727 Analyze_Expression (L);
2729 if No (R) and then Ada_Version >= Ada_2012 then
2730 Analyze_Set_Membership;
2731 return;
2732 end if;
2734 if Nkind (R) = N_Range
2735 or else (Nkind (R) = N_Attribute_Reference
2736 and then Attribute_Name (R) = Name_Range)
2737 then
2738 Analyze (R);
2740 if not Is_Overloaded (L) then
2741 Try_One_Interp (Etype (L));
2743 else
2744 Get_First_Interp (L, Index, It);
2745 while Present (It.Typ) loop
2746 Try_One_Interp (It.Typ);
2747 Get_Next_Interp (Index, It);
2748 end loop;
2749 end if;
2751 -- If not a range, it can be a subtype mark, or else it is a degenerate
2752 -- membership test with a singleton value, i.e. a test for equality,
2753 -- if the types are compatible.
2755 else
2756 Analyze (R);
2758 if Is_Entity_Name (R)
2759 and then Is_Type (Entity (R))
2760 then
2761 Find_Type (R);
2762 Check_Fully_Declared (Entity (R), R);
2764 elsif Ada_Version >= Ada_2012
2765 and then Has_Compatible_Type (R, Etype (L))
2766 then
2767 if Nkind (N) = N_In then
2768 Rewrite (N,
2769 Make_Op_Eq (Loc,
2770 Left_Opnd => L,
2771 Right_Opnd => R));
2772 else
2773 Rewrite (N,
2774 Make_Op_Ne (Loc,
2775 Left_Opnd => L,
2776 Right_Opnd => R));
2777 end if;
2779 Analyze (N);
2780 return;
2782 else
2783 -- In all versions of the language, if we reach this point there
2784 -- is a previous error that will be diagnosed below.
2786 Find_Type (R);
2787 end if;
2788 end if;
2790 -- Compatibility between expression and subtype mark or range is
2791 -- checked during resolution. The result of the operation is Boolean
2792 -- in any case.
2794 Set_Etype (N, Standard_Boolean);
2796 if Comes_From_Source (N)
2797 and then Present (Right_Opnd (N))
2798 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2799 then
2800 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2801 end if;
2802 end Analyze_Membership_Op;
2804 -----------------
2805 -- Analyze_Mod --
2806 -----------------
2808 procedure Analyze_Mod (N : Node_Id) is
2809 begin
2810 -- A special warning check, if we have an expression of the form:
2811 -- expr mod 2 * literal
2812 -- where literal is 64 or less, then probably what was meant was
2813 -- expr mod 2 ** literal
2814 -- so issue an appropriate warning.
2816 if Warn_On_Suspicious_Modulus_Value
2817 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
2818 and then Intval (Right_Opnd (N)) = Uint_2
2819 and then Nkind (Parent (N)) = N_Op_Multiply
2820 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
2821 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
2822 then
2823 Error_Msg_N
2824 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
2825 end if;
2827 -- Remaining processing is same as for other arithmetic operators
2829 Analyze_Arithmetic_Op (N);
2830 end Analyze_Mod;
2832 ----------------------
2833 -- Analyze_Negation --
2834 ----------------------
2836 procedure Analyze_Negation (N : Node_Id) is
2837 R : constant Node_Id := Right_Opnd (N);
2838 Op_Id : Entity_Id := Entity (N);
2840 begin
2841 Set_Etype (N, Any_Type);
2842 Candidate_Type := Empty;
2844 Analyze_Expression (R);
2846 if Present (Op_Id) then
2847 if Ekind (Op_Id) = E_Operator then
2848 Find_Negation_Types (R, Op_Id, N);
2849 else
2850 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2851 end if;
2853 else
2854 Op_Id := Get_Name_Entity_Id (Chars (N));
2855 while Present (Op_Id) loop
2856 if Ekind (Op_Id) = E_Operator then
2857 Find_Negation_Types (R, Op_Id, N);
2858 else
2859 Analyze_User_Defined_Unary_Op (N, Op_Id);
2860 end if;
2862 Op_Id := Homonym (Op_Id);
2863 end loop;
2864 end if;
2866 Operator_Check (N);
2867 end Analyze_Negation;
2869 ------------------
2870 -- Analyze_Null --
2871 ------------------
2873 procedure Analyze_Null (N : Node_Id) is
2874 begin
2875 Check_SPARK_Restriction ("null is not allowed", N);
2877 Set_Etype (N, Any_Access);
2878 end Analyze_Null;
2880 ----------------------
2881 -- Analyze_One_Call --
2882 ----------------------
2884 procedure Analyze_One_Call
2885 (N : Node_Id;
2886 Nam : Entity_Id;
2887 Report : Boolean;
2888 Success : out Boolean;
2889 Skip_First : Boolean := False)
2891 Actuals : constant List_Id := Parameter_Associations (N);
2892 Prev_T : constant Entity_Id := Etype (N);
2894 Must_Skip : constant Boolean := Skip_First
2895 or else Nkind (Original_Node (N)) = N_Selected_Component
2896 or else
2897 (Nkind (Original_Node (N)) = N_Indexed_Component
2898 and then Nkind (Prefix (Original_Node (N)))
2899 = N_Selected_Component);
2900 -- The first formal must be omitted from the match when trying to find
2901 -- a primitive operation that is a possible interpretation, and also
2902 -- after the call has been rewritten, because the corresponding actual
2903 -- is already known to be compatible, and because this may be an
2904 -- indexing of a call with default parameters.
2906 Formal : Entity_Id;
2907 Actual : Node_Id;
2908 Is_Indexed : Boolean := False;
2909 Is_Indirect : Boolean := False;
2910 Subp_Type : constant Entity_Id := Etype (Nam);
2911 Norm_OK : Boolean;
2913 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2914 -- There may be a user-defined operator that hides the current
2915 -- interpretation. We must check for this independently of the
2916 -- analysis of the call with the user-defined operation, because
2917 -- the parameter names may be wrong and yet the hiding takes place.
2918 -- This fixes a problem with ACATS test B34014O.
2920 -- When the type Address is a visible integer type, and the DEC
2921 -- system extension is visible, the predefined operator may be
2922 -- hidden as well, by one of the address operations in auxdec.
2923 -- Finally, The abstract operations on address do not hide the
2924 -- predefined operator (this is the purpose of making them abstract).
2926 procedure Indicate_Name_And_Type;
2927 -- If candidate interpretation matches, indicate name and type of
2928 -- result on call node.
2930 ----------------------------
2931 -- Indicate_Name_And_Type --
2932 ----------------------------
2934 procedure Indicate_Name_And_Type is
2935 begin
2936 Add_One_Interp (N, Nam, Etype (Nam));
2937 Check_Implicit_Dereference (N, Etype (Nam));
2938 Success := True;
2940 -- If the prefix of the call is a name, indicate the entity
2941 -- being called. If it is not a name, it is an expression that
2942 -- denotes an access to subprogram or else an entry or family. In
2943 -- the latter case, the name is a selected component, and the entity
2944 -- being called is noted on the selector.
2946 if not Is_Type (Nam) then
2947 if Is_Entity_Name (Name (N)) then
2948 Set_Entity (Name (N), Nam);
2950 elsif Nkind (Name (N)) = N_Selected_Component then
2951 Set_Entity (Selector_Name (Name (N)), Nam);
2952 end if;
2953 end if;
2955 if Debug_Flag_E and not Report then
2956 Write_Str (" Overloaded call ");
2957 Write_Int (Int (N));
2958 Write_Str (" compatible with ");
2959 Write_Int (Int (Nam));
2960 Write_Eol;
2961 end if;
2962 end Indicate_Name_And_Type;
2964 ------------------------
2965 -- Operator_Hidden_By --
2966 ------------------------
2968 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2969 Act1 : constant Node_Id := First_Actual (N);
2970 Act2 : constant Node_Id := Next_Actual (Act1);
2971 Form1 : constant Entity_Id := First_Formal (Fun);
2972 Form2 : constant Entity_Id := Next_Formal (Form1);
2974 begin
2975 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
2976 return False;
2978 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2979 return False;
2981 elsif Present (Form2) then
2982 if No (Act2)
2983 or else not Has_Compatible_Type (Act2, Etype (Form2))
2984 then
2985 return False;
2986 end if;
2988 elsif Present (Act2) then
2989 return False;
2990 end if;
2992 -- Now we know that the arity of the operator matches the function,
2993 -- and the function call is a valid interpretation. The function
2994 -- hides the operator if it has the right signature, or if one of
2995 -- its operands is a non-abstract operation on Address when this is
2996 -- a visible integer type.
2998 return Hides_Op (Fun, Nam)
2999 or else Is_Descendent_Of_Address (Etype (Form1))
3000 or else
3001 (Present (Form2)
3002 and then Is_Descendent_Of_Address (Etype (Form2)));
3003 end Operator_Hidden_By;
3005 -- Start of processing for Analyze_One_Call
3007 begin
3008 Success := False;
3010 -- If the subprogram has no formals or if all the formals have defaults,
3011 -- and the return type is an array type, the node may denote an indexing
3012 -- of the result of a parameterless call. In Ada 2005, the subprogram
3013 -- may have one non-defaulted formal, and the call may have been written
3014 -- in prefix notation, so that the rebuilt parameter list has more than
3015 -- one actual.
3017 if not Is_Overloadable (Nam)
3018 and then Ekind (Nam) /= E_Subprogram_Type
3019 and then Ekind (Nam) /= E_Entry_Family
3020 then
3021 return;
3022 end if;
3024 -- An indexing requires at least one actual. The name of the call cannot
3025 -- be an implicit indirect call, so it cannot be a generated explicit
3026 -- dereference.
3028 if not Is_Empty_List (Actuals)
3029 and then
3030 (Needs_No_Actuals (Nam)
3031 or else
3032 (Needs_One_Actual (Nam)
3033 and then Present (Next_Actual (First (Actuals)))))
3034 then
3035 if Is_Array_Type (Subp_Type)
3036 and then
3037 (Nkind (Name (N)) /= N_Explicit_Dereference
3038 or else Comes_From_Source (Name (N)))
3039 then
3040 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3042 elsif Is_Access_Type (Subp_Type)
3043 and then Is_Array_Type (Designated_Type (Subp_Type))
3044 then
3045 Is_Indexed :=
3046 Try_Indexed_Call
3047 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3049 -- The prefix can also be a parameterless function that returns an
3050 -- access to subprogram, in which case this is an indirect call.
3051 -- If this succeeds, an explicit dereference is added later on,
3052 -- in Analyze_Call or Resolve_Call.
3054 elsif Is_Access_Type (Subp_Type)
3055 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3056 then
3057 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3058 end if;
3060 end if;
3062 -- If the call has been transformed into a slice, it is of the form
3063 -- F (Subtype) where F is parameterless. The node has been rewritten in
3064 -- Try_Indexed_Call and there is nothing else to do.
3066 if Is_Indexed
3067 and then Nkind (N) = N_Slice
3068 then
3069 return;
3070 end if;
3072 Normalize_Actuals
3073 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3075 if not Norm_OK then
3077 -- If an indirect call is a possible interpretation, indicate
3078 -- success to the caller. This may be an indexing of an explicit
3079 -- dereference of a call that returns an access type (see above).
3081 if Is_Indirect
3082 or else (Is_Indexed
3083 and then Nkind (Name (N)) = N_Explicit_Dereference
3084 and then Comes_From_Source (Name (N)))
3085 then
3086 Success := True;
3087 return;
3089 -- Mismatch in number or names of parameters
3091 elsif Debug_Flag_E then
3092 Write_Str (" normalization fails in call ");
3093 Write_Int (Int (N));
3094 Write_Str (" with subprogram ");
3095 Write_Int (Int (Nam));
3096 Write_Eol;
3097 end if;
3099 -- If the context expects a function call, discard any interpretation
3100 -- that is a procedure. If the node is not overloaded, leave as is for
3101 -- better error reporting when type mismatch is found.
3103 elsif Nkind (N) = N_Function_Call
3104 and then Is_Overloaded (Name (N))
3105 and then Ekind (Nam) = E_Procedure
3106 then
3107 return;
3109 -- Ditto for function calls in a procedure context
3111 elsif Nkind (N) = N_Procedure_Call_Statement
3112 and then Is_Overloaded (Name (N))
3113 and then Etype (Nam) /= Standard_Void_Type
3114 then
3115 return;
3117 elsif No (Actuals) then
3119 -- If Normalize succeeds, then there are default parameters for
3120 -- all formals.
3122 Indicate_Name_And_Type;
3124 elsif Ekind (Nam) = E_Operator then
3125 if Nkind (N) = N_Procedure_Call_Statement then
3126 return;
3127 end if;
3129 -- This can occur when the prefix of the call is an operator
3130 -- name or an expanded name whose selector is an operator name.
3132 Analyze_Operator_Call (N, Nam);
3134 if Etype (N) /= Prev_T then
3136 -- Check that operator is not hidden by a function interpretation
3138 if Is_Overloaded (Name (N)) then
3139 declare
3140 I : Interp_Index;
3141 It : Interp;
3143 begin
3144 Get_First_Interp (Name (N), I, It);
3145 while Present (It.Nam) loop
3146 if Operator_Hidden_By (It.Nam) then
3147 Set_Etype (N, Prev_T);
3148 return;
3149 end if;
3151 Get_Next_Interp (I, It);
3152 end loop;
3153 end;
3154 end if;
3156 -- If operator matches formals, record its name on the call.
3157 -- If the operator is overloaded, Resolve will select the
3158 -- correct one from the list of interpretations. The call
3159 -- node itself carries the first candidate.
3161 Set_Entity (Name (N), Nam);
3162 Success := True;
3164 elsif Report and then Etype (N) = Any_Type then
3165 Error_Msg_N ("incompatible arguments for operator", N);
3166 end if;
3168 else
3169 -- Normalize_Actuals has chained the named associations in the
3170 -- correct order of the formals.
3172 Actual := First_Actual (N);
3173 Formal := First_Formal (Nam);
3175 -- If we are analyzing a call rewritten from object notation, skip
3176 -- first actual, which may be rewritten later as an explicit
3177 -- dereference.
3179 if Must_Skip then
3180 Next_Actual (Actual);
3181 Next_Formal (Formal);
3182 end if;
3184 while Present (Actual) and then Present (Formal) loop
3185 if Nkind (Parent (Actual)) /= N_Parameter_Association
3186 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3187 then
3188 -- The actual can be compatible with the formal, but we must
3189 -- also check that the context is not an address type that is
3190 -- visibly an integer type, as is the case in VMS_64. In this
3191 -- case the use of literals is illegal, except in the body of
3192 -- descendents of system, where arithmetic operations on
3193 -- address are of course used.
3195 if Has_Compatible_Type (Actual, Etype (Formal))
3196 and then
3197 (Etype (Actual) /= Universal_Integer
3198 or else not Is_Descendent_Of_Address (Etype (Formal))
3199 or else
3200 Is_Predefined_File_Name
3201 (Unit_File_Name (Get_Source_Unit (N))))
3202 then
3203 Next_Actual (Actual);
3204 Next_Formal (Formal);
3206 -- In Allow_Integer_Address mode, we allow an actual integer to
3207 -- match a formal address type and vice versa. We only do this
3208 -- if we are certain that an error will otherwise be issued
3210 elsif Address_Integer_Convert_OK
3211 (Etype (Actual), Etype (Formal))
3212 and then (Report and not Is_Indexed and not Is_Indirect)
3213 then
3214 -- Handle this case by introducing an unchecked conversion
3216 Rewrite (Actual,
3217 Unchecked_Convert_To (Etype (Formal),
3218 Relocate_Node (Actual)));
3219 Analyze_And_Resolve (Actual, Etype (Formal));
3220 Next_Actual (Actual);
3221 Next_Formal (Formal);
3223 else
3224 if Debug_Flag_E then
3225 Write_Str (" type checking fails in call ");
3226 Write_Int (Int (N));
3227 Write_Str (" with formal ");
3228 Write_Int (Int (Formal));
3229 Write_Str (" in subprogram ");
3230 Write_Int (Int (Nam));
3231 Write_Eol;
3232 end if;
3234 -- Comment needed on the following test???
3236 if Report and not Is_Indexed and not Is_Indirect then
3238 -- Ada 2005 (AI-251): Complete the error notification
3239 -- to help new Ada 2005 users.
3241 if Is_Class_Wide_Type (Etype (Formal))
3242 and then Is_Interface (Etype (Etype (Formal)))
3243 and then not Interface_Present_In_Ancestor
3244 (Typ => Etype (Actual),
3245 Iface => Etype (Etype (Formal)))
3246 then
3247 Error_Msg_NE
3248 ("(Ada 2005) does not implement interface }",
3249 Actual, Etype (Etype (Formal)));
3250 end if;
3252 Wrong_Type (Actual, Etype (Formal));
3254 if Nkind (Actual) = N_Op_Eq
3255 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3256 then
3257 Formal := First_Formal (Nam);
3258 while Present (Formal) loop
3259 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3260 Error_Msg_N -- CODEFIX
3261 ("possible misspelling of `='>`!", Actual);
3262 exit;
3263 end if;
3265 Next_Formal (Formal);
3266 end loop;
3267 end if;
3269 if All_Errors_Mode then
3270 Error_Msg_Sloc := Sloc (Nam);
3272 if Etype (Formal) = Any_Type then
3273 Error_Msg_N
3274 ("there is no legal actual parameter", Actual);
3275 end if;
3277 if Is_Overloadable (Nam)
3278 and then Present (Alias (Nam))
3279 and then not Comes_From_Source (Nam)
3280 then
3281 Error_Msg_NE
3282 ("\\ =='> in call to inherited operation & #!",
3283 Actual, Nam);
3285 elsif Ekind (Nam) = E_Subprogram_Type then
3286 declare
3287 Access_To_Subprogram_Typ :
3288 constant Entity_Id :=
3289 Defining_Identifier
3290 (Associated_Node_For_Itype (Nam));
3291 begin
3292 Error_Msg_NE
3293 ("\\ =='> in call to dereference of &#!",
3294 Actual, Access_To_Subprogram_Typ);
3295 end;
3297 else
3298 Error_Msg_NE
3299 ("\\ =='> in call to &#!", Actual, Nam);
3301 end if;
3302 end if;
3303 end if;
3305 return;
3306 end if;
3308 else
3309 -- Normalize_Actuals has verified that a default value exists
3310 -- for this formal. Current actual names a subsequent formal.
3312 Next_Formal (Formal);
3313 end if;
3314 end loop;
3316 -- On exit, all actuals match
3318 Indicate_Name_And_Type;
3319 end if;
3320 end Analyze_One_Call;
3322 ---------------------------
3323 -- Analyze_Operator_Call --
3324 ---------------------------
3326 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3327 Op_Name : constant Name_Id := Chars (Op_Id);
3328 Act1 : constant Node_Id := First_Actual (N);
3329 Act2 : constant Node_Id := Next_Actual (Act1);
3331 begin
3332 -- Binary operator case
3334 if Present (Act2) then
3336 -- If more than two operands, then not binary operator after all
3338 if Present (Next_Actual (Act2)) then
3339 return;
3340 end if;
3342 -- Otherwise action depends on operator
3344 case Op_Name is
3345 when Name_Op_Add |
3346 Name_Op_Subtract |
3347 Name_Op_Multiply |
3348 Name_Op_Divide |
3349 Name_Op_Mod |
3350 Name_Op_Rem |
3351 Name_Op_Expon =>
3352 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3354 when Name_Op_And |
3355 Name_Op_Or |
3356 Name_Op_Xor =>
3357 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3359 when Name_Op_Lt |
3360 Name_Op_Le |
3361 Name_Op_Gt |
3362 Name_Op_Ge =>
3363 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3365 when Name_Op_Eq |
3366 Name_Op_Ne =>
3367 Find_Equality_Types (Act1, Act2, Op_Id, N);
3369 when Name_Op_Concat =>
3370 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3372 -- Is this when others, or should it be an abort???
3374 when others =>
3375 null;
3376 end case;
3378 -- Unary operator case
3380 else
3381 case Op_Name is
3382 when Name_Op_Subtract |
3383 Name_Op_Add |
3384 Name_Op_Abs =>
3385 Find_Unary_Types (Act1, Op_Id, N);
3387 when Name_Op_Not =>
3388 Find_Negation_Types (Act1, Op_Id, N);
3390 -- Is this when others correct, or should it be an abort???
3392 when others =>
3393 null;
3394 end case;
3395 end if;
3396 end Analyze_Operator_Call;
3398 -------------------------------------------
3399 -- Analyze_Overloaded_Selected_Component --
3400 -------------------------------------------
3402 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3403 Nam : constant Node_Id := Prefix (N);
3404 Sel : constant Node_Id := Selector_Name (N);
3405 Comp : Entity_Id;
3406 I : Interp_Index;
3407 It : Interp;
3408 T : Entity_Id;
3410 begin
3411 Set_Etype (Sel, Any_Type);
3413 Get_First_Interp (Nam, I, It);
3414 while Present (It.Typ) loop
3415 if Is_Access_Type (It.Typ) then
3416 T := Designated_Type (It.Typ);
3417 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3418 else
3419 T := It.Typ;
3420 end if;
3422 -- Locate the component. For a private prefix the selector can denote
3423 -- a discriminant.
3425 if Is_Record_Type (T) or else Is_Private_Type (T) then
3427 -- If the prefix is a class-wide type, the visible components are
3428 -- those of the base type.
3430 if Is_Class_Wide_Type (T) then
3431 T := Etype (T);
3432 end if;
3434 Comp := First_Entity (T);
3435 while Present (Comp) loop
3436 if Chars (Comp) = Chars (Sel)
3437 and then Is_Visible_Component (Comp)
3438 then
3440 -- AI05-105: if the context is an object renaming with
3441 -- an anonymous access type, the expected type of the
3442 -- object must be anonymous. This is a name resolution rule.
3444 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3445 or else No (Access_Definition (Parent (N)))
3446 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3447 or else
3448 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3449 then
3450 Set_Entity (Sel, Comp);
3451 Set_Etype (Sel, Etype (Comp));
3452 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3453 Check_Implicit_Dereference (N, Etype (Comp));
3455 -- This also specifies a candidate to resolve the name.
3456 -- Further overloading will be resolved from context.
3457 -- The selector name itself does not carry overloading
3458 -- information.
3460 Set_Etype (Nam, It.Typ);
3462 else
3463 -- Named access type in the context of a renaming
3464 -- declaration with an access definition. Remove
3465 -- inapplicable candidate.
3467 Remove_Interp (I);
3468 end if;
3469 end if;
3471 Next_Entity (Comp);
3472 end loop;
3474 elsif Is_Concurrent_Type (T) then
3475 Comp := First_Entity (T);
3476 while Present (Comp)
3477 and then Comp /= First_Private_Entity (T)
3478 loop
3479 if Chars (Comp) = Chars (Sel) then
3480 if Is_Overloadable (Comp) then
3481 Add_One_Interp (Sel, Comp, Etype (Comp));
3482 else
3483 Set_Entity_With_Checks (Sel, Comp);
3484 Generate_Reference (Comp, Sel);
3485 end if;
3487 Set_Etype (Sel, Etype (Comp));
3488 Set_Etype (N, Etype (Comp));
3489 Set_Etype (Nam, It.Typ);
3491 -- For access type case, introduce explicit dereference for
3492 -- more uniform treatment of entry calls. Do this only once
3493 -- if several interpretations yield an access type.
3495 if Is_Access_Type (Etype (Nam))
3496 and then Nkind (Nam) /= N_Explicit_Dereference
3497 then
3498 Insert_Explicit_Dereference (Nam);
3499 Error_Msg_NW
3500 (Warn_On_Dereference, "?d?implicit dereference", N);
3501 end if;
3502 end if;
3504 Next_Entity (Comp);
3505 end loop;
3507 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3508 end if;
3510 Get_Next_Interp (I, It);
3511 end loop;
3513 if Etype (N) = Any_Type
3514 and then not Try_Object_Operation (N)
3515 then
3516 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3517 Set_Entity (Sel, Any_Id);
3518 Set_Etype (Sel, Any_Type);
3519 end if;
3520 end Analyze_Overloaded_Selected_Component;
3522 ----------------------------------
3523 -- Analyze_Qualified_Expression --
3524 ----------------------------------
3526 procedure Analyze_Qualified_Expression (N : Node_Id) is
3527 Mark : constant Entity_Id := Subtype_Mark (N);
3528 Expr : constant Node_Id := Expression (N);
3529 I : Interp_Index;
3530 It : Interp;
3531 T : Entity_Id;
3533 begin
3534 Analyze_Expression (Expr);
3536 Set_Etype (N, Any_Type);
3537 Find_Type (Mark);
3538 T := Entity (Mark);
3539 Set_Etype (N, T);
3541 if T = Any_Type then
3542 return;
3543 end if;
3545 Check_Fully_Declared (T, N);
3547 -- If expected type is class-wide, check for exact match before
3548 -- expansion, because if the expression is a dispatching call it
3549 -- may be rewritten as explicit dereference with class-wide result.
3550 -- If expression is overloaded, retain only interpretations that
3551 -- will yield exact matches.
3553 if Is_Class_Wide_Type (T) then
3554 if not Is_Overloaded (Expr) then
3555 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3556 if Nkind (Expr) = N_Aggregate then
3557 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3558 else
3559 Wrong_Type (Expr, T);
3560 end if;
3561 end if;
3563 else
3564 Get_First_Interp (Expr, I, It);
3566 while Present (It.Nam) loop
3567 if Base_Type (It.Typ) /= Base_Type (T) then
3568 Remove_Interp (I);
3569 end if;
3571 Get_Next_Interp (I, It);
3572 end loop;
3573 end if;
3574 end if;
3576 Set_Etype (N, T);
3577 end Analyze_Qualified_Expression;
3579 -----------------------------------
3580 -- Analyze_Quantified_Expression --
3581 -----------------------------------
3583 procedure Analyze_Quantified_Expression (N : Node_Id) is
3584 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
3585 -- If the iterator is part of a quantified expression, and the range is
3586 -- known to be statically empty, emit a warning and replace expression
3587 -- with its static value. Returns True if the replacement occurs.
3589 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
3590 -- Determine whether if expression If_Expr lacks an else part or if it
3591 -- has one, it evaluates to True.
3593 --------------------
3594 -- Is_Empty_Range --
3595 --------------------
3597 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
3598 Loc : constant Source_Ptr := Sloc (N);
3600 begin
3601 if Is_Array_Type (Typ)
3602 and then Compile_Time_Known_Bounds (Typ)
3603 and then
3604 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
3605 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
3606 then
3607 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
3609 if All_Present (N) then
3610 Error_Msg_N
3611 ("??quantified expression with ALL "
3612 & "over a null range has value True", N);
3613 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3615 else
3616 Error_Msg_N
3617 ("??quantified expression with SOME "
3618 & "over a null range has value False", N);
3619 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3620 end if;
3622 Analyze (N);
3623 return True;
3625 else
3626 return False;
3627 end if;
3628 end Is_Empty_Range;
3630 -----------------------------
3631 -- No_Else_Or_Trivial_True --
3632 -----------------------------
3634 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
3635 Else_Expr : constant Node_Id :=
3636 Next (Next (First (Expressions (If_Expr))));
3637 begin
3638 return
3639 No (Else_Expr)
3640 or else (Compile_Time_Known_Value (Else_Expr)
3641 and then Is_True (Expr_Value (Else_Expr)));
3642 end No_Else_Or_Trivial_True;
3644 -- Local variables
3646 Cond : constant Node_Id := Condition (N);
3647 Loop_Id : Entity_Id;
3648 QE_Scop : Entity_Id;
3650 -- Start of processing for Analyze_Quantified_Expression
3652 begin
3653 Check_SPARK_Restriction ("quantified expression is not allowed", N);
3655 -- Create a scope to emulate the loop-like behavior of the quantified
3656 -- expression. The scope is needed to provide proper visibility of the
3657 -- loop variable.
3659 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
3660 Set_Etype (QE_Scop, Standard_Void_Type);
3661 Set_Scope (QE_Scop, Current_Scope);
3662 Set_Parent (QE_Scop, N);
3664 Push_Scope (QE_Scop);
3666 -- All constituents are preanalyzed and resolved to avoid untimely
3667 -- generation of various temporaries and types. Full analysis and
3668 -- expansion is carried out when the quantified expression is
3669 -- transformed into an expression with actions.
3671 if Present (Iterator_Specification (N)) then
3672 Preanalyze (Iterator_Specification (N));
3674 -- Do not proceed with the analysis when the range of iteration is
3675 -- empty. The appropriate error is issued by Is_Empty_Range.
3677 if Is_Entity_Name (Name (Iterator_Specification (N)))
3678 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
3679 then
3680 return;
3681 end if;
3683 else pragma Assert (Present (Loop_Parameter_Specification (N)));
3684 declare
3685 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
3687 begin
3688 Preanalyze (Loop_Par);
3690 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
3691 and then Parent (Loop_Par) /= N
3692 then
3693 -- The parser cannot distinguish between a loop specification
3694 -- and an iterator specification. If after pre-analysis the
3695 -- proper form has been recognized, rewrite the expression to
3696 -- reflect the right kind. This is needed for proper ASIS
3697 -- navigation. If expansion is enabled, the transformation is
3698 -- performed when the expression is rewritten as a loop.
3700 Set_Iterator_Specification (N,
3701 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
3703 Set_Defining_Identifier (Iterator_Specification (N),
3704 Relocate_Node (Defining_Identifier (Loop_Par)));
3705 Set_Name (Iterator_Specification (N),
3706 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
3707 Set_Comes_From_Source (Iterator_Specification (N),
3708 Comes_From_Source (Loop_Parameter_Specification (N)));
3709 Set_Loop_Parameter_Specification (N, Empty);
3710 end if;
3711 end;
3712 end if;
3714 Preanalyze_And_Resolve (Cond, Standard_Boolean);
3716 End_Scope;
3717 Set_Etype (N, Standard_Boolean);
3719 -- Verify that the loop variable is used within the condition of the
3720 -- quantified expression.
3722 if Present (Iterator_Specification (N)) then
3723 Loop_Id := Defining_Identifier (Iterator_Specification (N));
3724 else
3725 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
3726 end if;
3728 if Warn_On_Suspicious_Contract
3729 and then not Referenced (Loop_Id, Cond)
3730 then
3731 Error_Msg_N ("?T?unused variable &", Loop_Id);
3732 end if;
3734 -- Diagnose a possible misuse of the SOME existential quantifier. When
3735 -- we have a quantified expression of the form:
3737 -- for some X => (if P then Q [else True])
3739 -- any value for X that makes P False results in the if expression being
3740 -- trivially True, and so also results in the the quantified expression
3741 -- being trivially True.
3743 if Warn_On_Suspicious_Contract
3744 and then not All_Present (N)
3745 and then Nkind (Cond) = N_If_Expression
3746 and then No_Else_Or_Trivial_True (Cond)
3747 then
3748 Error_Msg_N ("?T?suspicious expression", N);
3749 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
3750 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
3751 end if;
3752 end Analyze_Quantified_Expression;
3754 -------------------
3755 -- Analyze_Range --
3756 -------------------
3758 procedure Analyze_Range (N : Node_Id) is
3759 L : constant Node_Id := Low_Bound (N);
3760 H : constant Node_Id := High_Bound (N);
3761 I1, I2 : Interp_Index;
3762 It1, It2 : Interp;
3764 procedure Check_Common_Type (T1, T2 : Entity_Id);
3765 -- Verify the compatibility of two types, and choose the
3766 -- non universal one if the other is universal.
3768 procedure Check_High_Bound (T : Entity_Id);
3769 -- Test one interpretation of the low bound against all those
3770 -- of the high bound.
3772 procedure Check_Universal_Expression (N : Node_Id);
3773 -- In Ada 83, reject bounds of a universal range that are not literals
3774 -- or entity names.
3776 -----------------------
3777 -- Check_Common_Type --
3778 -----------------------
3780 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3781 begin
3782 if Covers (T1 => T1, T2 => T2)
3783 or else
3784 Covers (T1 => T2, T2 => T1)
3785 then
3786 if T1 = Universal_Integer
3787 or else T1 = Universal_Real
3788 or else T1 = Any_Character
3789 then
3790 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3792 elsif T1 = T2 then
3793 Add_One_Interp (N, T1, T1);
3795 else
3796 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3797 end if;
3798 end if;
3799 end Check_Common_Type;
3801 ----------------------
3802 -- Check_High_Bound --
3803 ----------------------
3805 procedure Check_High_Bound (T : Entity_Id) is
3806 begin
3807 if not Is_Overloaded (H) then
3808 Check_Common_Type (T, Etype (H));
3809 else
3810 Get_First_Interp (H, I2, It2);
3811 while Present (It2.Typ) loop
3812 Check_Common_Type (T, It2.Typ);
3813 Get_Next_Interp (I2, It2);
3814 end loop;
3815 end if;
3816 end Check_High_Bound;
3818 -----------------------------
3819 -- Is_Universal_Expression --
3820 -----------------------------
3822 procedure Check_Universal_Expression (N : Node_Id) is
3823 begin
3824 if Etype (N) = Universal_Integer
3825 and then Nkind (N) /= N_Integer_Literal
3826 and then not Is_Entity_Name (N)
3827 and then Nkind (N) /= N_Attribute_Reference
3828 then
3829 Error_Msg_N ("illegal bound in discrete range", N);
3830 end if;
3831 end Check_Universal_Expression;
3833 -- Start of processing for Analyze_Range
3835 begin
3836 Set_Etype (N, Any_Type);
3837 Analyze_Expression (L);
3838 Analyze_Expression (H);
3840 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3841 return;
3843 else
3844 if not Is_Overloaded (L) then
3845 Check_High_Bound (Etype (L));
3846 else
3847 Get_First_Interp (L, I1, It1);
3848 while Present (It1.Typ) loop
3849 Check_High_Bound (It1.Typ);
3850 Get_Next_Interp (I1, It1);
3851 end loop;
3852 end if;
3854 -- If result is Any_Type, then we did not find a compatible pair
3856 if Etype (N) = Any_Type then
3857 Error_Msg_N ("incompatible types in range ", N);
3858 end if;
3859 end if;
3861 if Ada_Version = Ada_83
3862 and then
3863 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3864 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3865 then
3866 Check_Universal_Expression (L);
3867 Check_Universal_Expression (H);
3868 end if;
3870 Check_Function_Writable_Actuals (N);
3871 end Analyze_Range;
3873 -----------------------
3874 -- Analyze_Reference --
3875 -----------------------
3877 procedure Analyze_Reference (N : Node_Id) is
3878 P : constant Node_Id := Prefix (N);
3879 E : Entity_Id;
3880 T : Entity_Id;
3881 Acc_Type : Entity_Id;
3883 begin
3884 Analyze (P);
3886 -- An interesting error check, if we take the 'Reference of an object
3887 -- for which a pragma Atomic or Volatile has been given, and the type
3888 -- of the object is not Atomic or Volatile, then we are in trouble. The
3889 -- problem is that no trace of the atomic/volatile status will remain
3890 -- for the backend to respect when it deals with the resulting pointer,
3891 -- since the pointer type will not be marked atomic (it is a pointer to
3892 -- the base type of the object).
3894 -- It is not clear if that can ever occur, but in case it does, we will
3895 -- generate an error message. Not clear if this message can ever be
3896 -- generated, and pretty clear that it represents a bug if it is, still
3897 -- seems worth checking, except in CodePeer mode where we do not really
3898 -- care and don't want to bother the user.
3900 T := Etype (P);
3902 if Is_Entity_Name (P)
3903 and then Is_Object_Reference (P)
3904 and then not CodePeer_Mode
3905 then
3906 E := Entity (P);
3907 T := Etype (P);
3909 if (Has_Atomic_Components (E)
3910 and then not Has_Atomic_Components (T))
3911 or else
3912 (Has_Volatile_Components (E)
3913 and then not Has_Volatile_Components (T))
3914 or else (Is_Atomic (E) and then not Is_Atomic (T))
3915 or else (Is_Volatile (E) and then not Is_Volatile (T))
3916 then
3917 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3918 end if;
3919 end if;
3921 -- Carry on with normal processing
3923 Acc_Type := Create_Itype (E_Allocator_Type, N);
3924 Set_Etype (Acc_Type, Acc_Type);
3925 Set_Directly_Designated_Type (Acc_Type, Etype (P));
3926 Set_Etype (N, Acc_Type);
3927 end Analyze_Reference;
3929 --------------------------------
3930 -- Analyze_Selected_Component --
3931 --------------------------------
3933 -- Prefix is a record type or a task or protected type. In the latter case,
3934 -- the selector must denote a visible entry.
3936 procedure Analyze_Selected_Component (N : Node_Id) is
3937 Name : constant Node_Id := Prefix (N);
3938 Sel : constant Node_Id := Selector_Name (N);
3939 Act_Decl : Node_Id;
3940 Comp : Entity_Id;
3941 Has_Candidate : Boolean := False;
3942 In_Scope : Boolean;
3943 Parent_N : Node_Id;
3944 Pent : Entity_Id := Empty;
3945 Prefix_Type : Entity_Id;
3947 Type_To_Use : Entity_Id;
3948 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
3949 -- a class-wide type, we use its root type, whose components are
3950 -- present in the class-wide type.
3952 Is_Single_Concurrent_Object : Boolean;
3953 -- Set True if the prefix is a single task or a single protected object
3955 procedure Find_Component_In_Instance (Rec : Entity_Id);
3956 -- In an instance, a component of a private extension may not be visible
3957 -- while it was visible in the generic. Search candidate scope for a
3958 -- component with the proper identifier. This is only done if all other
3959 -- searches have failed. If a match is found, the Etype of both N and
3960 -- Sel are set from this component, and the entity of Sel is set to
3961 -- reference this component. If no match is found, Entity (Sel) remains
3962 -- unset.
3964 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3965 -- It is known that the parent of N denotes a subprogram call. Comp
3966 -- is an overloadable component of the concurrent type of the prefix.
3967 -- Determine whether all formals of the parent of N and Comp are mode
3968 -- conformant. If the parent node is not analyzed yet it may be an
3969 -- indexed component rather than a function call.
3971 --------------------------------
3972 -- Find_Component_In_Instance --
3973 --------------------------------
3975 procedure Find_Component_In_Instance (Rec : Entity_Id) is
3976 Comp : Entity_Id;
3978 begin
3979 Comp := First_Component (Rec);
3980 while Present (Comp) loop
3981 if Chars (Comp) = Chars (Sel) then
3982 Set_Entity_With_Checks (Sel, Comp);
3983 Set_Etype (Sel, Etype (Comp));
3984 Set_Etype (N, Etype (Comp));
3985 return;
3986 end if;
3988 Next_Component (Comp);
3989 end loop;
3991 -- If we fall through, no match, so no changes made
3993 return;
3994 end Find_Component_In_Instance;
3996 ------------------------------
3997 -- Has_Mode_Conformant_Spec --
3998 ------------------------------
4000 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4001 Comp_Param : Entity_Id;
4002 Param : Node_Id;
4003 Param_Typ : Entity_Id;
4005 begin
4006 Comp_Param := First_Formal (Comp);
4008 if Nkind (Parent (N)) = N_Indexed_Component then
4009 Param := First (Expressions (Parent (N)));
4010 else
4011 Param := First (Parameter_Associations (Parent (N)));
4012 end if;
4014 while Present (Comp_Param)
4015 and then Present (Param)
4016 loop
4017 Param_Typ := Find_Parameter_Type (Param);
4019 if Present (Param_Typ)
4020 and then
4021 not Conforming_Types
4022 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4023 then
4024 return False;
4025 end if;
4027 Next_Formal (Comp_Param);
4028 Next (Param);
4029 end loop;
4031 -- One of the specs has additional formals; there is no match, unless
4032 -- this may be an indexing of a parameterless call.
4034 -- Note that when expansion is disabled, the corresponding record
4035 -- type of synchronized types is not constructed, so that there is
4036 -- no point is attempting an interpretation as a prefixed call, as
4037 -- this is bound to fail because the primitive operations will not
4038 -- be properly located.
4040 if Present (Comp_Param) or else Present (Param) then
4041 if Needs_No_Actuals (Comp)
4042 and then Is_Array_Type (Etype (Comp))
4043 and then not Expander_Active
4044 then
4045 return True;
4046 else
4047 return False;
4048 end if;
4049 end if;
4051 return True;
4052 end Has_Mode_Conformant_Spec;
4054 -- Start of processing for Analyze_Selected_Component
4056 begin
4057 Set_Etype (N, Any_Type);
4059 if Is_Overloaded (Name) then
4060 Analyze_Overloaded_Selected_Component (N);
4061 return;
4063 elsif Etype (Name) = Any_Type then
4064 Set_Entity (Sel, Any_Id);
4065 Set_Etype (Sel, Any_Type);
4066 return;
4068 else
4069 Prefix_Type := Etype (Name);
4070 end if;
4072 if Is_Access_Type (Prefix_Type) then
4074 -- A RACW object can never be used as prefix of a selected component
4075 -- since that means it is dereferenced without being a controlling
4076 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4077 -- reporting an error, we must check whether this is actually a
4078 -- dispatching call in prefix form.
4080 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4081 and then Comes_From_Source (N)
4082 then
4083 if Try_Object_Operation (N) then
4084 return;
4085 else
4086 Error_Msg_N
4087 ("invalid dereference of a remote access-to-class-wide value",
4089 end if;
4091 -- Normal case of selected component applied to access type
4093 else
4094 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4096 if Is_Entity_Name (Name) then
4097 Pent := Entity (Name);
4098 elsif Nkind (Name) = N_Selected_Component
4099 and then Is_Entity_Name (Selector_Name (Name))
4100 then
4101 Pent := Entity (Selector_Name (Name));
4102 end if;
4104 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4105 end if;
4107 -- If we have an explicit dereference of a remote access-to-class-wide
4108 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4109 -- have to check for the case of a prefix that is a controlling operand
4110 -- of a prefixed dispatching call, as the dereference is legal in that
4111 -- case. Normally this condition is checked in Validate_Remote_Access_
4112 -- To_Class_Wide_Type, but we have to defer the checking for selected
4113 -- component prefixes because of the prefixed dispatching call case.
4114 -- Note that implicit dereferences are checked for this just above.
4116 elsif Nkind (Name) = N_Explicit_Dereference
4117 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4118 and then Comes_From_Source (N)
4119 then
4120 if Try_Object_Operation (N) then
4121 return;
4122 else
4123 Error_Msg_N
4124 ("invalid dereference of a remote access-to-class-wide value",
4126 end if;
4127 end if;
4129 -- (Ada 2005): if the prefix is the limited view of a type, and
4130 -- the context already includes the full view, use the full view
4131 -- in what follows, either to retrieve a component of to find
4132 -- a primitive operation. If the prefix is an explicit dereference,
4133 -- set the type of the prefix to reflect this transformation.
4134 -- If the non-limited view is itself an incomplete type, get the
4135 -- full view if available.
4137 if Is_Incomplete_Type (Prefix_Type)
4138 and then From_Limited_With (Prefix_Type)
4139 and then Present (Non_Limited_View (Prefix_Type))
4140 then
4141 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4143 if Nkind (N) = N_Explicit_Dereference then
4144 Set_Etype (Prefix (N), Prefix_Type);
4145 end if;
4147 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
4148 and then From_Limited_With (Prefix_Type)
4149 and then Present (Non_Limited_View (Etype (Prefix_Type)))
4150 then
4151 Prefix_Type :=
4152 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
4154 if Nkind (N) = N_Explicit_Dereference then
4155 Set_Etype (Prefix (N), Prefix_Type);
4156 end if;
4157 end if;
4159 if Ekind (Prefix_Type) = E_Private_Subtype then
4160 Prefix_Type := Base_Type (Prefix_Type);
4161 end if;
4163 Type_To_Use := Prefix_Type;
4165 -- For class-wide types, use the entity list of the root type. This
4166 -- indirection is specially important for private extensions because
4167 -- only the root type get switched (not the class-wide type).
4169 if Is_Class_Wide_Type (Prefix_Type) then
4170 Type_To_Use := Root_Type (Prefix_Type);
4171 end if;
4173 -- If the prefix is a single concurrent object, use its name in error
4174 -- messages, rather than that of its anonymous type.
4176 Is_Single_Concurrent_Object :=
4177 Is_Concurrent_Type (Prefix_Type)
4178 and then Is_Internal_Name (Chars (Prefix_Type))
4179 and then not Is_Derived_Type (Prefix_Type)
4180 and then Is_Entity_Name (Name);
4182 Comp := First_Entity (Type_To_Use);
4184 -- If the selector has an original discriminant, the node appears in
4185 -- an instance. Replace the discriminant with the corresponding one
4186 -- in the current discriminated type. For nested generics, this must
4187 -- be done transitively, so note the new original discriminant.
4189 if Nkind (Sel) = N_Identifier
4190 and then In_Instance
4191 and then Present (Original_Discriminant (Sel))
4192 then
4193 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4195 -- Mark entity before rewriting, for completeness and because
4196 -- subsequent semantic checks might examine the original node.
4198 Set_Entity (Sel, Comp);
4199 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4200 Set_Original_Discriminant (Selector_Name (N), Comp);
4201 Set_Etype (N, Etype (Comp));
4202 Check_Implicit_Dereference (N, Etype (Comp));
4204 if Is_Access_Type (Etype (Name)) then
4205 Insert_Explicit_Dereference (Name);
4206 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4207 end if;
4209 elsif Is_Record_Type (Prefix_Type) then
4211 -- Find component with given name. In an instance, if the node is
4212 -- known as a prefixed call, do not examine components whose
4213 -- visibility may be accidental.
4215 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4216 if Chars (Comp) = Chars (Sel)
4217 and then Is_Visible_Component (Comp, N)
4218 then
4219 Set_Entity_With_Checks (Sel, Comp);
4220 Set_Etype (Sel, Etype (Comp));
4222 if Ekind (Comp) = E_Discriminant then
4223 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4224 Error_Msg_N
4225 ("cannot reference discriminant of unchecked union",
4226 Sel);
4227 end if;
4229 if Is_Generic_Type (Prefix_Type)
4230 or else
4231 Is_Generic_Type (Root_Type (Prefix_Type))
4232 then
4233 Set_Original_Discriminant (Sel, Comp);
4234 end if;
4235 end if;
4237 -- Resolve the prefix early otherwise it is not possible to
4238 -- build the actual subtype of the component: it may need
4239 -- to duplicate this prefix and duplication is only allowed
4240 -- on fully resolved expressions.
4242 Resolve (Name);
4244 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4245 -- subtypes in a package specification.
4246 -- Example:
4248 -- limited with Pkg;
4249 -- package Pkg is
4250 -- type Acc_Inc is access Pkg.T;
4251 -- X : Acc_Inc;
4252 -- N : Natural := X.all.Comp; -- ERROR, limited view
4253 -- end Pkg; -- Comp is not visible
4255 if Nkind (Name) = N_Explicit_Dereference
4256 and then From_Limited_With (Etype (Prefix (Name)))
4257 and then not Is_Potentially_Use_Visible (Etype (Name))
4258 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4259 N_Package_Specification
4260 then
4261 Error_Msg_NE
4262 ("premature usage of incomplete}", Prefix (Name),
4263 Etype (Prefix (Name)));
4264 end if;
4266 -- We never need an actual subtype for the case of a selection
4267 -- for a indexed component of a non-packed array, since in
4268 -- this case gigi generates all the checks and can find the
4269 -- necessary bounds information.
4271 -- We also do not need an actual subtype for the case of a
4272 -- first, last, length, or range attribute applied to a
4273 -- non-packed array, since gigi can again get the bounds in
4274 -- these cases (gigi cannot handle the packed case, since it
4275 -- has the bounds of the packed array type, not the original
4276 -- bounds of the type). However, if the prefix is itself a
4277 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4278 -- as a dynamic-sized temporary, so we do generate an actual
4279 -- subtype for this case.
4281 Parent_N := Parent (N);
4283 if not Is_Packed (Etype (Comp))
4284 and then
4285 ((Nkind (Parent_N) = N_Indexed_Component
4286 and then Nkind (Name) /= N_Selected_Component)
4287 or else
4288 (Nkind (Parent_N) = N_Attribute_Reference
4289 and then
4290 Nam_In (Attribute_Name (Parent_N), Name_First,
4291 Name_Last,
4292 Name_Length,
4293 Name_Range)))
4294 then
4295 Set_Etype (N, Etype (Comp));
4297 -- If full analysis is not enabled, we do not generate an
4298 -- actual subtype, because in the absence of expansion
4299 -- reference to a formal of a protected type, for example,
4300 -- will not be properly transformed, and will lead to
4301 -- out-of-scope references in gigi.
4303 -- In all other cases, we currently build an actual subtype.
4304 -- It seems likely that many of these cases can be avoided,
4305 -- but right now, the front end makes direct references to the
4306 -- bounds (e.g. in generating a length check), and if we do
4307 -- not make an actual subtype, we end up getting a direct
4308 -- reference to a discriminant, which will not do.
4310 elsif Full_Analysis then
4311 Act_Decl :=
4312 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4313 Insert_Action (N, Act_Decl);
4315 if No (Act_Decl) then
4316 Set_Etype (N, Etype (Comp));
4318 else
4319 -- Component type depends on discriminants. Enter the
4320 -- main attributes of the subtype.
4322 declare
4323 Subt : constant Entity_Id :=
4324 Defining_Identifier (Act_Decl);
4326 begin
4327 Set_Etype (Subt, Base_Type (Etype (Comp)));
4328 Set_Ekind (Subt, Ekind (Etype (Comp)));
4329 Set_Etype (N, Subt);
4330 end;
4331 end if;
4333 -- If Full_Analysis not enabled, just set the Etype
4335 else
4336 Set_Etype (N, Etype (Comp));
4337 end if;
4339 Check_Implicit_Dereference (N, Etype (N));
4340 return;
4341 end if;
4343 -- If the prefix is a private extension, check only the visible
4344 -- components of the partial view. This must include the tag,
4345 -- which can appear in expanded code in a tag check.
4347 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4348 and then Chars (Selector_Name (N)) /= Name_uTag
4349 then
4350 exit when Comp = Last_Entity (Type_To_Use);
4351 end if;
4353 Next_Entity (Comp);
4354 end loop;
4356 -- Ada 2005 (AI-252): The selected component can be interpreted as
4357 -- a prefixed view of a subprogram. Depending on the context, this is
4358 -- either a name that can appear in a renaming declaration, or part
4359 -- of an enclosing call given in prefix form.
4361 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4362 -- selected component should resolve to a name.
4364 if Ada_Version >= Ada_2005
4365 and then Is_Tagged_Type (Prefix_Type)
4366 and then not Is_Concurrent_Type (Prefix_Type)
4367 then
4368 if Nkind (Parent (N)) = N_Generic_Association
4369 or else Nkind (Parent (N)) = N_Requeue_Statement
4370 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4371 then
4372 if Find_Primitive_Operation (N) then
4373 return;
4374 end if;
4376 elsif Try_Object_Operation (N) then
4377 return;
4378 end if;
4380 -- If the transformation fails, it will be necessary to redo the
4381 -- analysis with all errors enabled, to indicate candidate
4382 -- interpretations and reasons for each failure ???
4384 end if;
4386 elsif Is_Private_Type (Prefix_Type) then
4388 -- Allow access only to discriminants of the type. If the type has
4389 -- no full view, gigi uses the parent type for the components, so we
4390 -- do the same here.
4392 if No (Full_View (Prefix_Type)) then
4393 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4394 Comp := First_Entity (Type_To_Use);
4395 end if;
4397 while Present (Comp) loop
4398 if Chars (Comp) = Chars (Sel) then
4399 if Ekind (Comp) = E_Discriminant then
4400 Set_Entity_With_Checks (Sel, Comp);
4401 Generate_Reference (Comp, Sel);
4403 Set_Etype (Sel, Etype (Comp));
4404 Set_Etype (N, Etype (Comp));
4405 Check_Implicit_Dereference (N, Etype (N));
4407 if Is_Generic_Type (Prefix_Type)
4408 or else Is_Generic_Type (Root_Type (Prefix_Type))
4409 then
4410 Set_Original_Discriminant (Sel, Comp);
4411 end if;
4413 -- Before declaring an error, check whether this is tagged
4414 -- private type and a call to a primitive operation.
4416 elsif Ada_Version >= Ada_2005
4417 and then Is_Tagged_Type (Prefix_Type)
4418 and then Try_Object_Operation (N)
4419 then
4420 return;
4422 else
4423 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4424 Error_Msg_NE ("invisible selector& for }", N, Sel);
4425 Set_Entity (Sel, Any_Id);
4426 Set_Etype (N, Any_Type);
4427 end if;
4429 return;
4430 end if;
4432 Next_Entity (Comp);
4433 end loop;
4435 elsif Is_Concurrent_Type (Prefix_Type) then
4437 -- Find visible operation with given name. For a protected type,
4438 -- the possible candidates are discriminants, entries or protected
4439 -- procedures. For a task type, the set can only include entries or
4440 -- discriminants if the task type is not an enclosing scope. If it
4441 -- is an enclosing scope (e.g. in an inner task) then all entities
4442 -- are visible, but the prefix must denote the enclosing scope, i.e.
4443 -- can only be a direct name or an expanded name.
4445 Set_Etype (Sel, Any_Type);
4446 In_Scope := In_Open_Scopes (Prefix_Type);
4448 while Present (Comp) loop
4449 if Chars (Comp) = Chars (Sel) then
4450 if Is_Overloadable (Comp) then
4451 Add_One_Interp (Sel, Comp, Etype (Comp));
4453 -- If the prefix is tagged, the correct interpretation may
4454 -- lie in the primitive or class-wide operations of the
4455 -- type. Perform a simple conformance check to determine
4456 -- whether Try_Object_Operation should be invoked even if
4457 -- a visible entity is found.
4459 if Is_Tagged_Type (Prefix_Type)
4460 and then
4461 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4462 N_Function_Call,
4463 N_Indexed_Component)
4464 and then Has_Mode_Conformant_Spec (Comp)
4465 then
4466 Has_Candidate := True;
4467 end if;
4469 -- Note: a selected component may not denote a component of a
4470 -- protected type (4.1.3(7)).
4472 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4473 or else (In_Scope
4474 and then not Is_Protected_Type (Prefix_Type)
4475 and then Is_Entity_Name (Name))
4476 then
4477 Set_Entity_With_Checks (Sel, Comp);
4478 Generate_Reference (Comp, Sel);
4480 -- The selector is not overloadable, so we have a candidate
4481 -- interpretation.
4483 Has_Candidate := True;
4485 else
4486 goto Next_Comp;
4487 end if;
4489 Set_Etype (Sel, Etype (Comp));
4490 Set_Etype (N, Etype (Comp));
4492 if Ekind (Comp) = E_Discriminant then
4493 Set_Original_Discriminant (Sel, Comp);
4494 end if;
4496 -- For access type case, introduce explicit dereference for
4497 -- more uniform treatment of entry calls.
4499 if Is_Access_Type (Etype (Name)) then
4500 Insert_Explicit_Dereference (Name);
4501 Error_Msg_NW
4502 (Warn_On_Dereference, "?d?implicit dereference", N);
4503 end if;
4504 end if;
4506 <<Next_Comp>>
4507 Next_Entity (Comp);
4508 exit when not In_Scope
4509 and then
4510 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4511 end loop;
4513 -- If there is no visible entity with the given name or none of the
4514 -- visible entities are plausible interpretations, check whether
4515 -- there is some other primitive operation with that name.
4517 if Ada_Version >= Ada_2005
4518 and then Is_Tagged_Type (Prefix_Type)
4519 then
4520 if (Etype (N) = Any_Type
4521 or else not Has_Candidate)
4522 and then Try_Object_Operation (N)
4523 then
4524 return;
4526 -- If the context is not syntactically a procedure call, it
4527 -- may be a call to a primitive function declared outside of
4528 -- the synchronized type.
4530 -- If the context is a procedure call, there might still be
4531 -- an overloading between an entry and a primitive procedure
4532 -- declared outside of the synchronized type, called in prefix
4533 -- notation. This is harder to disambiguate because in one case
4534 -- the controlling formal is implicit ???
4536 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4537 and then Nkind (Parent (N)) /= N_Indexed_Component
4538 and then Try_Object_Operation (N)
4539 then
4540 return;
4541 end if;
4543 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4544 -- entry or procedure of a tagged concurrent type we must check
4545 -- if there are class-wide subprograms covering the primitive. If
4546 -- true then Try_Object_Operation reports the error.
4548 if Has_Candidate
4549 and then Is_Concurrent_Type (Prefix_Type)
4550 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4552 -- Duplicate the call. This is required to avoid problems with
4553 -- the tree transformations performed by Try_Object_Operation.
4554 -- Set properly the parent of the copied call, because it is
4555 -- about to be reanalyzed.
4557 then
4558 declare
4559 Par : constant Node_Id := New_Copy_Tree (Parent (N));
4561 begin
4562 Set_Parent (Par, Parent (Parent (N)));
4564 if Try_Object_Operation
4565 (Sinfo.Name (Par), CW_Test_Only => True)
4566 then
4567 return;
4568 end if;
4569 end;
4570 end if;
4571 end if;
4573 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4575 -- Case of a prefix of a protected type: selector might denote
4576 -- an invisible private component.
4578 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4579 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4580 Next_Entity (Comp);
4581 end loop;
4583 if Present (Comp) then
4584 if Is_Single_Concurrent_Object then
4585 Error_Msg_Node_2 := Entity (Name);
4586 Error_Msg_NE ("invisible selector& for &", N, Sel);
4588 else
4589 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4590 Error_Msg_NE ("invisible selector& for }", N, Sel);
4591 end if;
4592 return;
4593 end if;
4594 end if;
4596 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4598 else
4599 -- Invalid prefix
4601 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4602 end if;
4604 -- If N still has no type, the component is not defined in the prefix
4606 if Etype (N) = Any_Type then
4608 if Is_Single_Concurrent_Object then
4609 Error_Msg_Node_2 := Entity (Name);
4610 Error_Msg_NE ("no selector& for&", N, Sel);
4612 Check_Misspelled_Selector (Type_To_Use, Sel);
4614 -- If this is a derived formal type, the parent may have different
4615 -- visibility at this point. Try for an inherited component before
4616 -- reporting an error.
4618 elsif Is_Generic_Type (Prefix_Type)
4619 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4620 and then Prefix_Type /= Etype (Prefix_Type)
4621 and then Is_Record_Type (Etype (Prefix_Type))
4622 then
4623 Set_Etype (Prefix (N), Etype (Prefix_Type));
4624 Analyze_Selected_Component (N);
4625 return;
4627 -- Similarly, if this is the actual for a formal derived type, or
4628 -- a derived type thereof, the component inherited from the generic
4629 -- parent may not be visible in the actual, but the selected
4630 -- component is legal. Climb up the derivation chain of the generic
4631 -- parent type until we find the proper ancestor type.
4633 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
4634 declare
4635 Par : Entity_Id := Prefix_Type;
4636 begin
4637 -- Climb up derivation chain to generic actual subtype
4639 while not Is_Generic_Actual_Type (Par) loop
4640 if Ekind (Par) = E_Record_Type then
4641 Par := Parent_Subtype (Par);
4642 exit when No (Par);
4643 else
4644 exit when Par = Etype (Par);
4645 Par := Etype (Par);
4646 end if;
4647 end loop;
4649 if Present (Par) and then Is_Generic_Actual_Type (Par) then
4650 -- Now look for component in ancestor types
4652 Par := Generic_Parent_Type (Declaration_Node (Par));
4653 loop
4654 Find_Component_In_Instance (Par);
4655 exit when Present (Entity (Sel))
4656 or else Par = Etype (Par);
4657 Par := Etype (Par);
4658 end loop;
4659 end if;
4660 end;
4662 -- The search above must have eventually succeeded, since the
4663 -- selected component was legal in the generic.
4665 if No (Entity (Sel)) then
4666 raise Program_Error;
4667 end if;
4668 return;
4670 -- Component not found, specialize error message when appropriate
4672 else
4673 if Ekind (Prefix_Type) = E_Record_Subtype then
4675 -- Check whether this is a component of the base type which
4676 -- is absent from a statically constrained subtype. This will
4677 -- raise constraint error at run time, but is not a compile-
4678 -- time error. When the selector is illegal for base type as
4679 -- well fall through and generate a compilation error anyway.
4681 Comp := First_Component (Base_Type (Prefix_Type));
4682 while Present (Comp) loop
4683 if Chars (Comp) = Chars (Sel)
4684 and then Is_Visible_Component (Comp)
4685 then
4686 Set_Entity_With_Checks (Sel, Comp);
4687 Generate_Reference (Comp, Sel);
4688 Set_Etype (Sel, Etype (Comp));
4689 Set_Etype (N, Etype (Comp));
4691 -- Emit appropriate message. The node will be replaced
4692 -- by an appropriate raise statement.
4694 -- Note that in SPARK mode, as with all calls to apply a
4695 -- compile time constraint error, this will be made into
4696 -- an error to simplify the processing of the formal
4697 -- verification backend.
4699 Apply_Compile_Time_Constraint_Error
4700 (N, "component not present in }??",
4701 CE_Discriminant_Check_Failed,
4702 Ent => Prefix_Type, Rep => False);
4704 Set_Raises_Constraint_Error (N);
4705 return;
4706 end if;
4708 Next_Component (Comp);
4709 end loop;
4711 end if;
4713 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4714 Error_Msg_NE ("no selector& for}", N, Sel);
4716 -- Add information in the case of an incomplete prefix
4718 if Is_Incomplete_Type (Type_To_Use) then
4719 declare
4720 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
4722 begin
4723 if From_Limited_With (Scope (Type_To_Use)) then
4724 Error_Msg_NE
4725 ("\limited view of& has no components", N, Inc);
4727 else
4728 Error_Msg_NE
4729 ("\premature usage of incomplete type&", N, Inc);
4731 if Nkind (Parent (Inc)) =
4732 N_Incomplete_Type_Declaration
4733 then
4734 -- Record location of premature use in entity so that
4735 -- a continuation message is generated when the
4736 -- completion is seen.
4738 Set_Premature_Use (Parent (Inc), N);
4739 end if;
4740 end if;
4741 end;
4742 end if;
4744 Check_Misspelled_Selector (Type_To_Use, Sel);
4745 end if;
4747 Set_Entity (Sel, Any_Id);
4748 Set_Etype (Sel, Any_Type);
4749 end if;
4750 end Analyze_Selected_Component;
4752 ---------------------------
4753 -- Analyze_Short_Circuit --
4754 ---------------------------
4756 procedure Analyze_Short_Circuit (N : Node_Id) is
4757 L : constant Node_Id := Left_Opnd (N);
4758 R : constant Node_Id := Right_Opnd (N);
4759 Ind : Interp_Index;
4760 It : Interp;
4762 begin
4763 Analyze_Expression (L);
4764 Analyze_Expression (R);
4765 Set_Etype (N, Any_Type);
4767 if not Is_Overloaded (L) then
4768 if Root_Type (Etype (L)) = Standard_Boolean
4769 and then Has_Compatible_Type (R, Etype (L))
4770 then
4771 Add_One_Interp (N, Etype (L), Etype (L));
4772 end if;
4774 else
4775 Get_First_Interp (L, Ind, It);
4776 while Present (It.Typ) loop
4777 if Root_Type (It.Typ) = Standard_Boolean
4778 and then Has_Compatible_Type (R, It.Typ)
4779 then
4780 Add_One_Interp (N, It.Typ, It.Typ);
4781 end if;
4783 Get_Next_Interp (Ind, It);
4784 end loop;
4785 end if;
4787 -- Here we have failed to find an interpretation. Clearly we know that
4788 -- it is not the case that both operands can have an interpretation of
4789 -- Boolean, but this is by far the most likely intended interpretation.
4790 -- So we simply resolve both operands as Booleans, and at least one of
4791 -- these resolutions will generate an error message, and we do not need
4792 -- to give another error message on the short circuit operation itself.
4794 if Etype (N) = Any_Type then
4795 Resolve (L, Standard_Boolean);
4796 Resolve (R, Standard_Boolean);
4797 Set_Etype (N, Standard_Boolean);
4798 end if;
4799 end Analyze_Short_Circuit;
4801 -------------------
4802 -- Analyze_Slice --
4803 -------------------
4805 procedure Analyze_Slice (N : Node_Id) is
4806 D : constant Node_Id := Discrete_Range (N);
4807 P : constant Node_Id := Prefix (N);
4808 Array_Type : Entity_Id;
4809 Index_Type : Entity_Id;
4811 procedure Analyze_Overloaded_Slice;
4812 -- If the prefix is overloaded, select those interpretations that
4813 -- yield a one-dimensional array type.
4815 ------------------------------
4816 -- Analyze_Overloaded_Slice --
4817 ------------------------------
4819 procedure Analyze_Overloaded_Slice is
4820 I : Interp_Index;
4821 It : Interp;
4822 Typ : Entity_Id;
4824 begin
4825 Set_Etype (N, Any_Type);
4827 Get_First_Interp (P, I, It);
4828 while Present (It.Nam) loop
4829 Typ := It.Typ;
4831 if Is_Access_Type (Typ) then
4832 Typ := Designated_Type (Typ);
4833 Error_Msg_NW
4834 (Warn_On_Dereference, "?d?implicit dereference", N);
4835 end if;
4837 if Is_Array_Type (Typ)
4838 and then Number_Dimensions (Typ) = 1
4839 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4840 then
4841 Add_One_Interp (N, Typ, Typ);
4842 end if;
4844 Get_Next_Interp (I, It);
4845 end loop;
4847 if Etype (N) = Any_Type then
4848 Error_Msg_N ("expect array type in prefix of slice", N);
4849 end if;
4850 end Analyze_Overloaded_Slice;
4852 -- Start of processing for Analyze_Slice
4854 begin
4855 if Comes_From_Source (N) then
4856 Check_SPARK_Restriction ("slice is not allowed", N);
4857 end if;
4859 Analyze (P);
4860 Analyze (D);
4862 if Is_Overloaded (P) then
4863 Analyze_Overloaded_Slice;
4865 else
4866 Array_Type := Etype (P);
4867 Set_Etype (N, Any_Type);
4869 if Is_Access_Type (Array_Type) then
4870 Array_Type := Designated_Type (Array_Type);
4871 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4872 end if;
4874 if not Is_Array_Type (Array_Type) then
4875 Wrong_Type (P, Any_Array);
4877 elsif Number_Dimensions (Array_Type) > 1 then
4878 Error_Msg_N
4879 ("type is not one-dimensional array in slice prefix", N);
4881 else
4882 if Ekind (Array_Type) = E_String_Literal_Subtype then
4883 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
4884 else
4885 Index_Type := Etype (First_Index (Array_Type));
4886 end if;
4888 if not Has_Compatible_Type (D, Index_Type) then
4889 Wrong_Type (D, Index_Type);
4890 else
4891 Set_Etype (N, Array_Type);
4892 end if;
4893 end if;
4894 end if;
4895 end Analyze_Slice;
4897 -----------------------------
4898 -- Analyze_Type_Conversion --
4899 -----------------------------
4901 procedure Analyze_Type_Conversion (N : Node_Id) is
4902 Expr : constant Node_Id := Expression (N);
4903 T : Entity_Id;
4905 begin
4906 -- If Conversion_OK is set, then the Etype is already set, and the
4907 -- only processing required is to analyze the expression. This is
4908 -- used to construct certain "illegal" conversions which are not
4909 -- allowed by Ada semantics, but can be handled OK by Gigi, see
4910 -- Sinfo for further details.
4912 if Conversion_OK (N) then
4913 Analyze (Expr);
4914 return;
4915 end if;
4917 -- Otherwise full type analysis is required, as well as some semantic
4918 -- checks to make sure the argument of the conversion is appropriate.
4920 Find_Type (Subtype_Mark (N));
4921 T := Entity (Subtype_Mark (N));
4922 Set_Etype (N, T);
4923 Check_Fully_Declared (T, N);
4924 Analyze_Expression (Expr);
4925 Validate_Remote_Type_Type_Conversion (N);
4927 -- Only remaining step is validity checks on the argument. These
4928 -- are skipped if the conversion does not come from the source.
4930 if not Comes_From_Source (N) then
4931 return;
4933 -- If there was an error in a generic unit, no need to replicate the
4934 -- error message. Conversely, constant-folding in the generic may
4935 -- transform the argument of a conversion into a string literal, which
4936 -- is legal. Therefore the following tests are not performed in an
4937 -- instance. The same applies to an inlined body.
4939 elsif In_Instance or In_Inlined_Body then
4940 return;
4942 elsif Nkind (Expr) = N_Null then
4943 Error_Msg_N ("argument of conversion cannot be null", N);
4944 Error_Msg_N ("\use qualified expression instead", N);
4945 Set_Etype (N, Any_Type);
4947 elsif Nkind (Expr) = N_Aggregate then
4948 Error_Msg_N ("argument of conversion cannot be aggregate", N);
4949 Error_Msg_N ("\use qualified expression instead", N);
4951 elsif Nkind (Expr) = N_Allocator then
4952 Error_Msg_N ("argument of conversion cannot be an allocator", N);
4953 Error_Msg_N ("\use qualified expression instead", N);
4955 elsif Nkind (Expr) = N_String_Literal then
4956 Error_Msg_N ("argument of conversion cannot be string literal", N);
4957 Error_Msg_N ("\use qualified expression instead", N);
4959 elsif Nkind (Expr) = N_Character_Literal then
4960 if Ada_Version = Ada_83 then
4961 Resolve (Expr, T);
4962 else
4963 Error_Msg_N ("argument of conversion cannot be character literal",
4965 Error_Msg_N ("\use qualified expression instead", N);
4966 end if;
4968 elsif Nkind (Expr) = N_Attribute_Reference
4969 and then
4970 Nam_In (Attribute_Name (Expr), Name_Access,
4971 Name_Unchecked_Access,
4972 Name_Unrestricted_Access)
4973 then
4974 Error_Msg_N ("argument of conversion cannot be access", N);
4975 Error_Msg_N ("\use qualified expression instead", N);
4976 end if;
4977 end Analyze_Type_Conversion;
4979 ----------------------
4980 -- Analyze_Unary_Op --
4981 ----------------------
4983 procedure Analyze_Unary_Op (N : Node_Id) is
4984 R : constant Node_Id := Right_Opnd (N);
4985 Op_Id : Entity_Id := Entity (N);
4987 begin
4988 Set_Etype (N, Any_Type);
4989 Candidate_Type := Empty;
4991 Analyze_Expression (R);
4993 if Present (Op_Id) then
4994 if Ekind (Op_Id) = E_Operator then
4995 Find_Unary_Types (R, Op_Id, N);
4996 else
4997 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4998 end if;
5000 else
5001 Op_Id := Get_Name_Entity_Id (Chars (N));
5002 while Present (Op_Id) loop
5003 if Ekind (Op_Id) = E_Operator then
5004 if No (Next_Entity (First_Entity (Op_Id))) then
5005 Find_Unary_Types (R, Op_Id, N);
5006 end if;
5008 elsif Is_Overloadable (Op_Id) then
5009 Analyze_User_Defined_Unary_Op (N, Op_Id);
5010 end if;
5012 Op_Id := Homonym (Op_Id);
5013 end loop;
5014 end if;
5016 Operator_Check (N);
5017 end Analyze_Unary_Op;
5019 ----------------------------------
5020 -- Analyze_Unchecked_Expression --
5021 ----------------------------------
5023 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5024 begin
5025 Analyze (Expression (N), Suppress => All_Checks);
5026 Set_Etype (N, Etype (Expression (N)));
5027 Save_Interps (Expression (N), N);
5028 end Analyze_Unchecked_Expression;
5030 ---------------------------------------
5031 -- Analyze_Unchecked_Type_Conversion --
5032 ---------------------------------------
5034 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5035 begin
5036 Find_Type (Subtype_Mark (N));
5037 Analyze_Expression (Expression (N));
5038 Set_Etype (N, Entity (Subtype_Mark (N)));
5039 end Analyze_Unchecked_Type_Conversion;
5041 ------------------------------------
5042 -- Analyze_User_Defined_Binary_Op --
5043 ------------------------------------
5045 procedure Analyze_User_Defined_Binary_Op
5046 (N : Node_Id;
5047 Op_Id : Entity_Id)
5049 begin
5050 -- Only do analysis if the operator Comes_From_Source, since otherwise
5051 -- the operator was generated by the expander, and all such operators
5052 -- always refer to the operators in package Standard.
5054 if Comes_From_Source (N) then
5055 declare
5056 F1 : constant Entity_Id := First_Formal (Op_Id);
5057 F2 : constant Entity_Id := Next_Formal (F1);
5059 begin
5060 -- Verify that Op_Id is a visible binary function. Note that since
5061 -- we know Op_Id is overloaded, potentially use visible means use
5062 -- visible for sure (RM 9.4(11)).
5064 if Ekind (Op_Id) = E_Function
5065 and then Present (F2)
5066 and then (Is_Immediately_Visible (Op_Id)
5067 or else Is_Potentially_Use_Visible (Op_Id))
5068 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5069 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5070 then
5071 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5073 -- If the left operand is overloaded, indicate that the current
5074 -- type is a viable candidate. This is redundant in most cases,
5075 -- but for equality and comparison operators where the context
5076 -- does not impose a type on the operands, setting the proper
5077 -- type is necessary to avoid subsequent ambiguities during
5078 -- resolution, when both user-defined and predefined operators
5079 -- may be candidates.
5081 if Is_Overloaded (Left_Opnd (N)) then
5082 Set_Etype (Left_Opnd (N), Etype (F1));
5083 end if;
5085 if Debug_Flag_E then
5086 Write_Str ("user defined operator ");
5087 Write_Name (Chars (Op_Id));
5088 Write_Str (" on node ");
5089 Write_Int (Int (N));
5090 Write_Eol;
5091 end if;
5092 end if;
5093 end;
5094 end if;
5095 end Analyze_User_Defined_Binary_Op;
5097 -----------------------------------
5098 -- Analyze_User_Defined_Unary_Op --
5099 -----------------------------------
5101 procedure Analyze_User_Defined_Unary_Op
5102 (N : Node_Id;
5103 Op_Id : Entity_Id)
5105 begin
5106 -- Only do analysis if the operator Comes_From_Source, since otherwise
5107 -- the operator was generated by the expander, and all such operators
5108 -- always refer to the operators in package Standard.
5110 if Comes_From_Source (N) then
5111 declare
5112 F : constant Entity_Id := First_Formal (Op_Id);
5114 begin
5115 -- Verify that Op_Id is a visible unary function. Note that since
5116 -- we know Op_Id is overloaded, potentially use visible means use
5117 -- visible for sure (RM 9.4(11)).
5119 if Ekind (Op_Id) = E_Function
5120 and then No (Next_Formal (F))
5121 and then (Is_Immediately_Visible (Op_Id)
5122 or else Is_Potentially_Use_Visible (Op_Id))
5123 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5124 then
5125 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5126 end if;
5127 end;
5128 end if;
5129 end Analyze_User_Defined_Unary_Op;
5131 ---------------------------
5132 -- Check_Arithmetic_Pair --
5133 ---------------------------
5135 procedure Check_Arithmetic_Pair
5136 (T1, T2 : Entity_Id;
5137 Op_Id : Entity_Id;
5138 N : Node_Id)
5140 Op_Name : constant Name_Id := Chars (Op_Id);
5142 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5143 -- Check whether the fixed-point type Typ has a user-defined operator
5144 -- (multiplication or division) that should hide the corresponding
5145 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5146 -- such operators more visible and therefore useful.
5148 -- If the name of the operation is an expanded name with prefix
5149 -- Standard, the predefined universal fixed operator is available,
5150 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5152 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5153 -- Get specific type (i.e. non-universal type if there is one)
5155 ------------------
5156 -- Has_Fixed_Op --
5157 ------------------
5159 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5160 Bas : constant Entity_Id := Base_Type (Typ);
5161 Ent : Entity_Id;
5162 F1 : Entity_Id;
5163 F2 : Entity_Id;
5165 begin
5166 -- If the universal_fixed operation is given explicitly the rule
5167 -- concerning primitive operations of the type do not apply.
5169 if Nkind (N) = N_Function_Call
5170 and then Nkind (Name (N)) = N_Expanded_Name
5171 and then Entity (Prefix (Name (N))) = Standard_Standard
5172 then
5173 return False;
5174 end if;
5176 -- The operation is treated as primitive if it is declared in the
5177 -- same scope as the type, and therefore on the same entity chain.
5179 Ent := Next_Entity (Typ);
5180 while Present (Ent) loop
5181 if Chars (Ent) = Chars (Op) then
5182 F1 := First_Formal (Ent);
5183 F2 := Next_Formal (F1);
5185 -- The operation counts as primitive if either operand or
5186 -- result are of the given base type, and both operands are
5187 -- fixed point types.
5189 if (Base_Type (Etype (F1)) = Bas
5190 and then Is_Fixed_Point_Type (Etype (F2)))
5192 or else
5193 (Base_Type (Etype (F2)) = Bas
5194 and then Is_Fixed_Point_Type (Etype (F1)))
5196 or else
5197 (Base_Type (Etype (Ent)) = Bas
5198 and then Is_Fixed_Point_Type (Etype (F1))
5199 and then Is_Fixed_Point_Type (Etype (F2)))
5200 then
5201 return True;
5202 end if;
5203 end if;
5205 Next_Entity (Ent);
5206 end loop;
5208 return False;
5209 end Has_Fixed_Op;
5211 -------------------
5212 -- Specific_Type --
5213 -------------------
5215 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5216 begin
5217 if T1 = Universal_Integer or else T1 = Universal_Real then
5218 return Base_Type (T2);
5219 else
5220 return Base_Type (T1);
5221 end if;
5222 end Specific_Type;
5224 -- Start of processing for Check_Arithmetic_Pair
5226 begin
5227 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5228 if Is_Numeric_Type (T1)
5229 and then Is_Numeric_Type (T2)
5230 and then (Covers (T1 => T1, T2 => T2)
5231 or else
5232 Covers (T1 => T2, T2 => T1))
5233 then
5234 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5235 end if;
5237 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5238 if Is_Fixed_Point_Type (T1)
5239 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5240 then
5241 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5242 -- and no further processing is required (this is the case of an
5243 -- operator constructed by Exp_Fixd for a fixed point operation)
5244 -- Otherwise add one interpretation with universal fixed result
5245 -- If the operator is given in functional notation, it comes
5246 -- from source and Fixed_As_Integer cannot apply.
5248 if (Nkind (N) not in N_Op
5249 or else not Treat_Fixed_As_Integer (N))
5250 and then
5251 (not Has_Fixed_Op (T1, Op_Id)
5252 or else Nkind (Parent (N)) = N_Type_Conversion)
5253 then
5254 Add_One_Interp (N, Op_Id, Universal_Fixed);
5255 end if;
5257 elsif Is_Fixed_Point_Type (T2)
5258 and then (Nkind (N) not in N_Op
5259 or else not Treat_Fixed_As_Integer (N))
5260 and then T1 = Universal_Real
5261 and then
5262 (not Has_Fixed_Op (T1, Op_Id)
5263 or else Nkind (Parent (N)) = N_Type_Conversion)
5264 then
5265 Add_One_Interp (N, Op_Id, Universal_Fixed);
5267 elsif Is_Numeric_Type (T1)
5268 and then Is_Numeric_Type (T2)
5269 and then (Covers (T1 => T1, T2 => T2)
5270 or else
5271 Covers (T1 => T2, T2 => T1))
5272 then
5273 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5275 elsif Is_Fixed_Point_Type (T1)
5276 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5277 or else T2 = Universal_Integer)
5278 then
5279 Add_One_Interp (N, Op_Id, T1);
5281 elsif T2 = Universal_Real
5282 and then Base_Type (T1) = Base_Type (Standard_Integer)
5283 and then Op_Name = Name_Op_Multiply
5284 then
5285 Add_One_Interp (N, Op_Id, Any_Fixed);
5287 elsif T1 = Universal_Real
5288 and then Base_Type (T2) = Base_Type (Standard_Integer)
5289 then
5290 Add_One_Interp (N, Op_Id, Any_Fixed);
5292 elsif Is_Fixed_Point_Type (T2)
5293 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5294 or else T1 = Universal_Integer)
5295 and then Op_Name = Name_Op_Multiply
5296 then
5297 Add_One_Interp (N, Op_Id, T2);
5299 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5300 Add_One_Interp (N, Op_Id, T1);
5302 elsif T2 = Universal_Real
5303 and then T1 = Universal_Integer
5304 and then Op_Name = Name_Op_Multiply
5305 then
5306 Add_One_Interp (N, Op_Id, T2);
5307 end if;
5309 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5311 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5312 -- set does not require any special processing, since the Etype is
5313 -- already set (case of operation constructed by Exp_Fixed).
5315 if Is_Integer_Type (T1)
5316 and then (Covers (T1 => T1, T2 => T2)
5317 or else
5318 Covers (T1 => T2, T2 => T1))
5319 then
5320 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5321 end if;
5323 elsif Op_Name = Name_Op_Expon then
5324 if Is_Numeric_Type (T1)
5325 and then not Is_Fixed_Point_Type (T1)
5326 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5327 or else T2 = Universal_Integer)
5328 then
5329 Add_One_Interp (N, Op_Id, Base_Type (T1));
5330 end if;
5332 else pragma Assert (Nkind (N) in N_Op_Shift);
5334 -- If not one of the predefined operators, the node may be one
5335 -- of the intrinsic functions. Its kind is always specific, and
5336 -- we can use it directly, rather than the name of the operation.
5338 if Is_Integer_Type (T1)
5339 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5340 or else T2 = Universal_Integer)
5341 then
5342 Add_One_Interp (N, Op_Id, Base_Type (T1));
5343 end if;
5344 end if;
5345 end Check_Arithmetic_Pair;
5347 -------------------------------
5348 -- Check_Misspelled_Selector --
5349 -------------------------------
5351 procedure Check_Misspelled_Selector
5352 (Prefix : Entity_Id;
5353 Sel : Node_Id)
5355 Max_Suggestions : constant := 2;
5356 Nr_Of_Suggestions : Natural := 0;
5358 Suggestion_1 : Entity_Id := Empty;
5359 Suggestion_2 : Entity_Id := Empty;
5361 Comp : Entity_Id;
5363 begin
5364 -- All the components of the prefix of selector Sel are matched against
5365 -- Sel and a count is maintained of possible misspellings. When at
5366 -- the end of the analysis there are one or two (not more) possible
5367 -- misspellings, these misspellings will be suggested as possible
5368 -- correction.
5370 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5372 -- Concurrent types should be handled as well ???
5374 return;
5375 end if;
5377 Comp := First_Entity (Prefix);
5378 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5379 if Is_Visible_Component (Comp) then
5380 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5381 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5383 case Nr_Of_Suggestions is
5384 when 1 => Suggestion_1 := Comp;
5385 when 2 => Suggestion_2 := Comp;
5386 when others => exit;
5387 end case;
5388 end if;
5389 end if;
5391 Comp := Next_Entity (Comp);
5392 end loop;
5394 -- Report at most two suggestions
5396 if Nr_Of_Suggestions = 1 then
5397 Error_Msg_NE -- CODEFIX
5398 ("\possible misspelling of&", Sel, Suggestion_1);
5400 elsif Nr_Of_Suggestions = 2 then
5401 Error_Msg_Node_2 := Suggestion_2;
5402 Error_Msg_NE -- CODEFIX
5403 ("\possible misspelling of& or&", Sel, Suggestion_1);
5404 end if;
5405 end Check_Misspelled_Selector;
5407 ----------------------
5408 -- Defined_In_Scope --
5409 ----------------------
5411 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5413 S1 : constant Entity_Id := Scope (Base_Type (T));
5414 begin
5415 return S1 = S
5416 or else (S1 = System_Aux_Id and then S = Scope (S1));
5417 end Defined_In_Scope;
5419 -------------------
5420 -- Diagnose_Call --
5421 -------------------
5423 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5424 Actual : Node_Id;
5425 X : Interp_Index;
5426 It : Interp;
5427 Err_Mode : Boolean;
5428 New_Nam : Node_Id;
5429 Void_Interp_Seen : Boolean := False;
5431 Success : Boolean;
5432 pragma Warnings (Off, Boolean);
5434 begin
5435 if Ada_Version >= Ada_2005 then
5436 Actual := First_Actual (N);
5437 while Present (Actual) loop
5439 -- Ada 2005 (AI-50217): Post an error in case of premature
5440 -- usage of an entity from the limited view.
5442 if not Analyzed (Etype (Actual))
5443 and then From_Limited_With (Etype (Actual))
5444 then
5445 Error_Msg_Qual_Level := 1;
5446 Error_Msg_NE
5447 ("missing with_clause for scope of imported type&",
5448 Actual, Etype (Actual));
5449 Error_Msg_Qual_Level := 0;
5450 end if;
5452 Next_Actual (Actual);
5453 end loop;
5454 end if;
5456 -- Analyze each candidate call again, with full error reporting
5457 -- for each.
5459 Error_Msg_N
5460 ("no candidate interpretations match the actuals:!", Nam);
5461 Err_Mode := All_Errors_Mode;
5462 All_Errors_Mode := True;
5464 -- If this is a call to an operation of a concurrent type,
5465 -- the failed interpretations have been removed from the
5466 -- name. Recover them to provide full diagnostics.
5468 if Nkind (Parent (Nam)) = N_Selected_Component then
5469 Set_Entity (Nam, Empty);
5470 New_Nam := New_Copy_Tree (Parent (Nam));
5471 Set_Is_Overloaded (New_Nam, False);
5472 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5473 Set_Parent (New_Nam, Parent (Parent (Nam)));
5474 Analyze_Selected_Component (New_Nam);
5475 Get_First_Interp (Selector_Name (New_Nam), X, It);
5476 else
5477 Get_First_Interp (Nam, X, It);
5478 end if;
5480 while Present (It.Nam) loop
5481 if Etype (It.Nam) = Standard_Void_Type then
5482 Void_Interp_Seen := True;
5483 end if;
5485 Analyze_One_Call (N, It.Nam, True, Success);
5486 Get_Next_Interp (X, It);
5487 end loop;
5489 if Nkind (N) = N_Function_Call then
5490 Get_First_Interp (Nam, X, It);
5491 while Present (It.Nam) loop
5492 if Ekind_In (It.Nam, E_Function, E_Operator) then
5493 return;
5494 else
5495 Get_Next_Interp (X, It);
5496 end if;
5497 end loop;
5499 -- If all interpretations are procedures, this deserves a
5500 -- more precise message. Ditto if this appears as the prefix
5501 -- of a selected component, which may be a lexical error.
5503 Error_Msg_N
5504 ("\context requires function call, found procedure name", Nam);
5506 if Nkind (Parent (N)) = N_Selected_Component
5507 and then N = Prefix (Parent (N))
5508 then
5509 Error_Msg_N -- CODEFIX
5510 ("\period should probably be semicolon", Parent (N));
5511 end if;
5513 elsif Nkind (N) = N_Procedure_Call_Statement
5514 and then not Void_Interp_Seen
5515 then
5516 Error_Msg_N (
5517 "\function name found in procedure call", Nam);
5518 end if;
5520 All_Errors_Mode := Err_Mode;
5521 end Diagnose_Call;
5523 ---------------------------
5524 -- Find_Arithmetic_Types --
5525 ---------------------------
5527 procedure Find_Arithmetic_Types
5528 (L, R : Node_Id;
5529 Op_Id : Entity_Id;
5530 N : Node_Id)
5532 Index1 : Interp_Index;
5533 Index2 : Interp_Index;
5534 It1 : Interp;
5535 It2 : Interp;
5537 procedure Check_Right_Argument (T : Entity_Id);
5538 -- Check right operand of operator
5540 --------------------------
5541 -- Check_Right_Argument --
5542 --------------------------
5544 procedure Check_Right_Argument (T : Entity_Id) is
5545 begin
5546 if not Is_Overloaded (R) then
5547 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5548 else
5549 Get_First_Interp (R, Index2, It2);
5550 while Present (It2.Typ) loop
5551 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5552 Get_Next_Interp (Index2, It2);
5553 end loop;
5554 end if;
5555 end Check_Right_Argument;
5557 -- Start of processing for Find_Arithmetic_Types
5559 begin
5560 if not Is_Overloaded (L) then
5561 Check_Right_Argument (Etype (L));
5563 else
5564 Get_First_Interp (L, Index1, It1);
5565 while Present (It1.Typ) loop
5566 Check_Right_Argument (It1.Typ);
5567 Get_Next_Interp (Index1, It1);
5568 end loop;
5569 end if;
5571 end Find_Arithmetic_Types;
5573 ------------------------
5574 -- Find_Boolean_Types --
5575 ------------------------
5577 procedure Find_Boolean_Types
5578 (L, R : Node_Id;
5579 Op_Id : Entity_Id;
5580 N : Node_Id)
5582 Index : Interp_Index;
5583 It : Interp;
5585 procedure Check_Numeric_Argument (T : Entity_Id);
5586 -- Special case for logical operations one of whose operands is an
5587 -- integer literal. If both are literal the result is any modular type.
5589 ----------------------------
5590 -- Check_Numeric_Argument --
5591 ----------------------------
5593 procedure Check_Numeric_Argument (T : Entity_Id) is
5594 begin
5595 if T = Universal_Integer then
5596 Add_One_Interp (N, Op_Id, Any_Modular);
5598 elsif Is_Modular_Integer_Type (T) then
5599 Add_One_Interp (N, Op_Id, T);
5600 end if;
5601 end Check_Numeric_Argument;
5603 -- Start of processing for Find_Boolean_Types
5605 begin
5606 if not Is_Overloaded (L) then
5607 if Etype (L) = Universal_Integer
5608 or else Etype (L) = Any_Modular
5609 then
5610 if not Is_Overloaded (R) then
5611 Check_Numeric_Argument (Etype (R));
5613 else
5614 Get_First_Interp (R, Index, It);
5615 while Present (It.Typ) loop
5616 Check_Numeric_Argument (It.Typ);
5617 Get_Next_Interp (Index, It);
5618 end loop;
5619 end if;
5621 -- If operands are aggregates, we must assume that they may be
5622 -- boolean arrays, and leave disambiguation for the second pass.
5623 -- If only one is an aggregate, verify that the other one has an
5624 -- interpretation as a boolean array
5626 elsif Nkind (L) = N_Aggregate then
5627 if Nkind (R) = N_Aggregate then
5628 Add_One_Interp (N, Op_Id, Etype (L));
5630 elsif not Is_Overloaded (R) then
5631 if Valid_Boolean_Arg (Etype (R)) then
5632 Add_One_Interp (N, Op_Id, Etype (R));
5633 end if;
5635 else
5636 Get_First_Interp (R, Index, It);
5637 while Present (It.Typ) loop
5638 if Valid_Boolean_Arg (It.Typ) then
5639 Add_One_Interp (N, Op_Id, It.Typ);
5640 end if;
5642 Get_Next_Interp (Index, It);
5643 end loop;
5644 end if;
5646 elsif Valid_Boolean_Arg (Etype (L))
5647 and then Has_Compatible_Type (R, Etype (L))
5648 then
5649 Add_One_Interp (N, Op_Id, Etype (L));
5650 end if;
5652 else
5653 Get_First_Interp (L, Index, It);
5654 while Present (It.Typ) loop
5655 if Valid_Boolean_Arg (It.Typ)
5656 and then Has_Compatible_Type (R, It.Typ)
5657 then
5658 Add_One_Interp (N, Op_Id, It.Typ);
5659 end if;
5661 Get_Next_Interp (Index, It);
5662 end loop;
5663 end if;
5664 end Find_Boolean_Types;
5666 ---------------------------
5667 -- Find_Comparison_Types --
5668 ---------------------------
5670 procedure Find_Comparison_Types
5671 (L, R : Node_Id;
5672 Op_Id : Entity_Id;
5673 N : Node_Id)
5675 Index : Interp_Index;
5676 It : Interp;
5677 Found : Boolean := False;
5678 I_F : Interp_Index;
5679 T_F : Entity_Id;
5680 Scop : Entity_Id := Empty;
5682 procedure Try_One_Interp (T1 : Entity_Id);
5683 -- Routine to try one proposed interpretation. Note that the context
5684 -- of the operator plays no role in resolving the arguments, so that
5685 -- if there is more than one interpretation of the operands that is
5686 -- compatible with comparison, the operation is ambiguous.
5688 --------------------
5689 -- Try_One_Interp --
5690 --------------------
5692 procedure Try_One_Interp (T1 : Entity_Id) is
5693 begin
5695 -- If the operator is an expanded name, then the type of the operand
5696 -- must be defined in the corresponding scope. If the type is
5697 -- universal, the context will impose the correct type.
5699 if Present (Scop)
5700 and then not Defined_In_Scope (T1, Scop)
5701 and then T1 /= Universal_Integer
5702 and then T1 /= Universal_Real
5703 and then T1 /= Any_String
5704 and then T1 /= Any_Composite
5705 then
5706 return;
5707 end if;
5709 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
5710 if Found and then Base_Type (T1) /= Base_Type (T_F) then
5711 It := Disambiguate (L, I_F, Index, Any_Type);
5713 if It = No_Interp then
5714 Ambiguous_Operands (N);
5715 Set_Etype (L, Any_Type);
5716 return;
5718 else
5719 T_F := It.Typ;
5720 end if;
5722 else
5723 Found := True;
5724 T_F := T1;
5725 I_F := Index;
5726 end if;
5728 Set_Etype (L, T_F);
5729 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5731 end if;
5732 end Try_One_Interp;
5734 -- Start of processing for Find_Comparison_Types
5736 begin
5737 -- If left operand is aggregate, the right operand has to
5738 -- provide a usable type for it.
5740 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
5741 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5742 return;
5743 end if;
5745 if Nkind (N) = N_Function_Call
5746 and then Nkind (Name (N)) = N_Expanded_Name
5747 then
5748 Scop := Entity (Prefix (Name (N)));
5750 -- The prefix may be a package renaming, and the subsequent test
5751 -- requires the original package.
5753 if Ekind (Scop) = E_Package
5754 and then Present (Renamed_Entity (Scop))
5755 then
5756 Scop := Renamed_Entity (Scop);
5757 Set_Entity (Prefix (Name (N)), Scop);
5758 end if;
5759 end if;
5761 if not Is_Overloaded (L) then
5762 Try_One_Interp (Etype (L));
5764 else
5765 Get_First_Interp (L, Index, It);
5766 while Present (It.Typ) loop
5767 Try_One_Interp (It.Typ);
5768 Get_Next_Interp (Index, It);
5769 end loop;
5770 end if;
5771 end Find_Comparison_Types;
5773 ----------------------------------------
5774 -- Find_Non_Universal_Interpretations --
5775 ----------------------------------------
5777 procedure Find_Non_Universal_Interpretations
5778 (N : Node_Id;
5779 R : Node_Id;
5780 Op_Id : Entity_Id;
5781 T1 : Entity_Id)
5783 Index : Interp_Index;
5784 It : Interp;
5786 begin
5787 if T1 = Universal_Integer or else T1 = Universal_Real
5789 -- If the left operand of an equality operator is null, the visibility
5790 -- of the operator must be determined from the interpretation of the
5791 -- right operand. This processing must be done for Any_Access, which
5792 -- is the internal representation of the type of the literal null.
5794 or else T1 = Any_Access
5795 then
5796 if not Is_Overloaded (R) then
5797 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5798 else
5799 Get_First_Interp (R, Index, It);
5800 while Present (It.Typ) loop
5801 if Covers (It.Typ, T1) then
5802 Add_One_Interp
5803 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5804 end if;
5806 Get_Next_Interp (Index, It);
5807 end loop;
5808 end if;
5809 else
5810 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5811 end if;
5812 end Find_Non_Universal_Interpretations;
5814 ------------------------------
5815 -- Find_Concatenation_Types --
5816 ------------------------------
5818 procedure Find_Concatenation_Types
5819 (L, R : Node_Id;
5820 Op_Id : Entity_Id;
5821 N : Node_Id)
5823 Op_Type : constant Entity_Id := Etype (Op_Id);
5825 begin
5826 if Is_Array_Type (Op_Type)
5827 and then not Is_Limited_Type (Op_Type)
5829 and then (Has_Compatible_Type (L, Op_Type)
5830 or else
5831 Has_Compatible_Type (L, Component_Type (Op_Type)))
5833 and then (Has_Compatible_Type (R, Op_Type)
5834 or else
5835 Has_Compatible_Type (R, Component_Type (Op_Type)))
5836 then
5837 Add_One_Interp (N, Op_Id, Op_Type);
5838 end if;
5839 end Find_Concatenation_Types;
5841 -------------------------
5842 -- Find_Equality_Types --
5843 -------------------------
5845 procedure Find_Equality_Types
5846 (L, R : Node_Id;
5847 Op_Id : Entity_Id;
5848 N : Node_Id)
5850 Index : Interp_Index;
5851 It : Interp;
5852 Found : Boolean := False;
5853 I_F : Interp_Index;
5854 T_F : Entity_Id;
5855 Scop : Entity_Id := Empty;
5857 procedure Try_One_Interp (T1 : Entity_Id);
5858 -- The context of the equality operator plays no role in resolving the
5859 -- arguments, so that if there is more than one interpretation of the
5860 -- operands that is compatible with equality, the construct is ambiguous
5861 -- and an error can be emitted now, after trying to disambiguate, i.e.
5862 -- applying preference rules.
5864 --------------------
5865 -- Try_One_Interp --
5866 --------------------
5868 procedure Try_One_Interp (T1 : Entity_Id) is
5869 Bas : constant Entity_Id := Base_Type (T1);
5871 begin
5872 -- If the operator is an expanded name, then the type of the operand
5873 -- must be defined in the corresponding scope. If the type is
5874 -- universal, the context will impose the correct type. An anonymous
5875 -- type for a 'Access reference is also universal in this sense, as
5876 -- the actual type is obtained from context.
5878 -- In Ada 2005, the equality operator for anonymous access types
5879 -- is declared in Standard, and preference rules apply to it.
5881 if Present (Scop) then
5882 if Defined_In_Scope (T1, Scop)
5883 or else T1 = Universal_Integer
5884 or else T1 = Universal_Real
5885 or else T1 = Any_Access
5886 or else T1 = Any_String
5887 or else T1 = Any_Composite
5888 or else (Ekind (T1) = E_Access_Subprogram_Type
5889 and then not Comes_From_Source (T1))
5890 then
5891 null;
5893 elsif Ekind (T1) = E_Anonymous_Access_Type
5894 and then Scop = Standard_Standard
5895 then
5896 null;
5898 else
5899 -- The scope does not contain an operator for the type
5901 return;
5902 end if;
5904 -- If we have infix notation, the operator must be usable. Within
5905 -- an instance, if the type is already established we know it is
5906 -- correct. If an operand is universal it is compatible with any
5907 -- numeric type.
5909 elsif In_Open_Scopes (Scope (Bas))
5910 or else Is_Potentially_Use_Visible (Bas)
5911 or else In_Use (Bas)
5912 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
5914 -- In an instance, the type may have been immediately visible.
5915 -- Either the types are compatible, or one operand is universal
5916 -- (numeric or null).
5918 or else (In_Instance
5919 and then
5920 (First_Subtype (T1) = First_Subtype (Etype (R))
5921 or else Nkind (R) = N_Null
5922 or else
5923 (Is_Numeric_Type (T1)
5924 and then Is_Universal_Numeric_Type (Etype (R)))))
5926 -- In Ada 2005, the equality on anonymous access types is declared
5927 -- in Standard, and is always visible.
5929 or else Ekind (T1) = E_Anonymous_Access_Type
5930 then
5931 null;
5933 else
5934 -- Save candidate type for subsequent error message, if any
5936 if not Is_Limited_Type (T1) then
5937 Candidate_Type := T1;
5938 end if;
5940 return;
5941 end if;
5943 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5944 -- Do not allow anonymous access types in equality operators.
5946 if Ada_Version < Ada_2005
5947 and then Ekind (T1) = E_Anonymous_Access_Type
5948 then
5949 return;
5950 end if;
5952 -- If the right operand has a type compatible with T1, check for an
5953 -- acceptable interpretation, unless T1 is limited (no predefined
5954 -- equality available), or this is use of a "/=" for a tagged type.
5955 -- In the latter case, possible interpretations of equality need
5956 -- to be considered, we don't want the default inequality declared
5957 -- in Standard to be chosen, and the "/=" will be rewritten as a
5958 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
5959 -- that that rewriting happens during analysis rather than being
5960 -- delayed until expansion (this is needed for ASIS, which only sees
5961 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
5962 -- is Name_Op_Eq then we still proceed with the interpretation,
5963 -- because that indicates the potential rewriting case where the
5964 -- interpretation to consider is actually "=" and the node may be
5965 -- about to be rewritten by Analyze_Equality_Op.
5967 if T1 /= Standard_Void_Type
5968 and then Has_Compatible_Type (R, T1)
5970 and then
5971 ((not Is_Limited_Type (T1)
5972 and then not Is_Limited_Composite (T1))
5974 or else
5975 (Is_Array_Type (T1)
5976 and then not Is_Limited_Type (Component_Type (T1))
5977 and then Available_Full_View_Of_Component (T1)))
5979 and then
5980 (Nkind (N) /= N_Op_Ne
5981 or else not Is_Tagged_Type (T1)
5982 or else Chars (Op_Id) = Name_Op_Eq)
5983 then
5984 if Found
5985 and then Base_Type (T1) /= Base_Type (T_F)
5986 then
5987 It := Disambiguate (L, I_F, Index, Any_Type);
5989 if It = No_Interp then
5990 Ambiguous_Operands (N);
5991 Set_Etype (L, Any_Type);
5992 return;
5994 else
5995 T_F := It.Typ;
5996 end if;
5998 else
5999 Found := True;
6000 T_F := T1;
6001 I_F := Index;
6002 end if;
6004 if not Analyzed (L) then
6005 Set_Etype (L, T_F);
6006 end if;
6008 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6010 -- Case of operator was not visible, Etype still set to Any_Type
6012 if Etype (N) = Any_Type then
6013 Found := False;
6014 end if;
6016 elsif Scop = Standard_Standard
6017 and then Ekind (T1) = E_Anonymous_Access_Type
6018 then
6019 Found := True;
6020 end if;
6021 end Try_One_Interp;
6023 -- Start of processing for Find_Equality_Types
6025 begin
6026 -- If left operand is aggregate, the right operand has to
6027 -- provide a usable type for it.
6029 if Nkind (L) = N_Aggregate
6030 and then Nkind (R) /= N_Aggregate
6031 then
6032 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6033 return;
6034 end if;
6036 if Nkind (N) = N_Function_Call
6037 and then Nkind (Name (N)) = N_Expanded_Name
6038 then
6039 Scop := Entity (Prefix (Name (N)));
6041 -- The prefix may be a package renaming, and the subsequent test
6042 -- requires the original package.
6044 if Ekind (Scop) = E_Package
6045 and then Present (Renamed_Entity (Scop))
6046 then
6047 Scop := Renamed_Entity (Scop);
6048 Set_Entity (Prefix (Name (N)), Scop);
6049 end if;
6050 end if;
6052 if not Is_Overloaded (L) then
6053 Try_One_Interp (Etype (L));
6055 else
6056 Get_First_Interp (L, Index, It);
6057 while Present (It.Typ) loop
6058 Try_One_Interp (It.Typ);
6059 Get_Next_Interp (Index, It);
6060 end loop;
6061 end if;
6062 end Find_Equality_Types;
6064 -------------------------
6065 -- Find_Negation_Types --
6066 -------------------------
6068 procedure Find_Negation_Types
6069 (R : Node_Id;
6070 Op_Id : Entity_Id;
6071 N : Node_Id)
6073 Index : Interp_Index;
6074 It : Interp;
6076 begin
6077 if not Is_Overloaded (R) then
6078 if Etype (R) = Universal_Integer then
6079 Add_One_Interp (N, Op_Id, Any_Modular);
6080 elsif Valid_Boolean_Arg (Etype (R)) then
6081 Add_One_Interp (N, Op_Id, Etype (R));
6082 end if;
6084 else
6085 Get_First_Interp (R, Index, It);
6086 while Present (It.Typ) loop
6087 if Valid_Boolean_Arg (It.Typ) then
6088 Add_One_Interp (N, Op_Id, It.Typ);
6089 end if;
6091 Get_Next_Interp (Index, It);
6092 end loop;
6093 end if;
6094 end Find_Negation_Types;
6096 ------------------------------
6097 -- Find_Primitive_Operation --
6098 ------------------------------
6100 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6101 Obj : constant Node_Id := Prefix (N);
6102 Op : constant Node_Id := Selector_Name (N);
6104 Prim : Elmt_Id;
6105 Prims : Elist_Id;
6106 Typ : Entity_Id;
6108 begin
6109 Set_Etype (Op, Any_Type);
6111 if Is_Access_Type (Etype (Obj)) then
6112 Typ := Designated_Type (Etype (Obj));
6113 else
6114 Typ := Etype (Obj);
6115 end if;
6117 if Is_Class_Wide_Type (Typ) then
6118 Typ := Root_Type (Typ);
6119 end if;
6121 Prims := Primitive_Operations (Typ);
6123 Prim := First_Elmt (Prims);
6124 while Present (Prim) loop
6125 if Chars (Node (Prim)) = Chars (Op) then
6126 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6127 Set_Etype (N, Etype (Node (Prim)));
6128 end if;
6130 Next_Elmt (Prim);
6131 end loop;
6133 -- Now look for class-wide operations of the type or any of its
6134 -- ancestors by iterating over the homonyms of the selector.
6136 declare
6137 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6138 Hom : Entity_Id;
6140 begin
6141 Hom := Current_Entity (Op);
6142 while Present (Hom) loop
6143 if (Ekind (Hom) = E_Procedure
6144 or else
6145 Ekind (Hom) = E_Function)
6146 and then Scope (Hom) = Scope (Typ)
6147 and then Present (First_Formal (Hom))
6148 and then
6149 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6150 or else
6151 (Is_Access_Type (Etype (First_Formal (Hom)))
6152 and then
6153 Ekind (Etype (First_Formal (Hom))) =
6154 E_Anonymous_Access_Type
6155 and then
6156 Base_Type
6157 (Designated_Type (Etype (First_Formal (Hom)))) =
6158 Cls_Type))
6159 then
6160 Add_One_Interp (Op, Hom, Etype (Hom));
6161 Set_Etype (N, Etype (Hom));
6162 end if;
6164 Hom := Homonym (Hom);
6165 end loop;
6166 end;
6168 return Etype (Op) /= Any_Type;
6169 end Find_Primitive_Operation;
6171 ----------------------
6172 -- Find_Unary_Types --
6173 ----------------------
6175 procedure Find_Unary_Types
6176 (R : Node_Id;
6177 Op_Id : Entity_Id;
6178 N : Node_Id)
6180 Index : Interp_Index;
6181 It : Interp;
6183 begin
6184 if not Is_Overloaded (R) then
6185 if Is_Numeric_Type (Etype (R)) then
6187 -- In an instance a generic actual may be a numeric type even if
6188 -- the formal in the generic unit was not. In that case, the
6189 -- predefined operator was not a possible interpretation in the
6190 -- generic, and cannot be one in the instance, unless the operator
6191 -- is an actual of an instance.
6193 if In_Instance
6194 and then
6195 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6196 then
6197 null;
6198 else
6199 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6200 end if;
6201 end if;
6203 else
6204 Get_First_Interp (R, Index, It);
6205 while Present (It.Typ) loop
6206 if Is_Numeric_Type (It.Typ) then
6207 if In_Instance
6208 and then
6209 not Is_Numeric_Type
6210 (Corresponding_Generic_Type (Etype (It.Typ)))
6211 then
6212 null;
6214 else
6215 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6216 end if;
6217 end if;
6219 Get_Next_Interp (Index, It);
6220 end loop;
6221 end if;
6222 end Find_Unary_Types;
6224 ------------------
6225 -- Junk_Operand --
6226 ------------------
6228 function Junk_Operand (N : Node_Id) return Boolean is
6229 Enode : Node_Id;
6231 begin
6232 if Error_Posted (N) then
6233 return False;
6234 end if;
6236 -- Get entity to be tested
6238 if Is_Entity_Name (N)
6239 and then Present (Entity (N))
6240 then
6241 Enode := N;
6243 -- An odd case, a procedure name gets converted to a very peculiar
6244 -- function call, and here is where we detect this happening.
6246 elsif Nkind (N) = N_Function_Call
6247 and then Is_Entity_Name (Name (N))
6248 and then Present (Entity (Name (N)))
6249 then
6250 Enode := Name (N);
6252 -- Another odd case, there are at least some cases of selected
6253 -- components where the selected component is not marked as having
6254 -- an entity, even though the selector does have an entity
6256 elsif Nkind (N) = N_Selected_Component
6257 and then Present (Entity (Selector_Name (N)))
6258 then
6259 Enode := Selector_Name (N);
6261 else
6262 return False;
6263 end if;
6265 -- Now test the entity we got to see if it is a bad case
6267 case Ekind (Entity (Enode)) is
6269 when E_Package =>
6270 Error_Msg_N
6271 ("package name cannot be used as operand", Enode);
6273 when Generic_Unit_Kind =>
6274 Error_Msg_N
6275 ("generic unit name cannot be used as operand", Enode);
6277 when Type_Kind =>
6278 Error_Msg_N
6279 ("subtype name cannot be used as operand", Enode);
6281 when Entry_Kind =>
6282 Error_Msg_N
6283 ("entry name cannot be used as operand", Enode);
6285 when E_Procedure =>
6286 Error_Msg_N
6287 ("procedure name cannot be used as operand", Enode);
6289 when E_Exception =>
6290 Error_Msg_N
6291 ("exception name cannot be used as operand", Enode);
6293 when E_Block | E_Label | E_Loop =>
6294 Error_Msg_N
6295 ("label name cannot be used as operand", Enode);
6297 when others =>
6298 return False;
6300 end case;
6302 return True;
6303 end Junk_Operand;
6305 --------------------
6306 -- Operator_Check --
6307 --------------------
6309 procedure Operator_Check (N : Node_Id) is
6310 begin
6311 Remove_Abstract_Operations (N);
6313 -- Test for case of no interpretation found for operator
6315 if Etype (N) = Any_Type then
6316 declare
6317 L : Node_Id;
6318 R : Node_Id;
6319 Op_Id : Entity_Id := Empty;
6321 begin
6322 R := Right_Opnd (N);
6324 if Nkind (N) in N_Binary_Op then
6325 L := Left_Opnd (N);
6326 else
6327 L := Empty;
6328 end if;
6330 -- If either operand has no type, then don't complain further,
6331 -- since this simply means that we have a propagated error.
6333 if R = Error
6334 or else Etype (R) = Any_Type
6335 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
6336 then
6337 -- For the rather unusual case where one of the operands is
6338 -- a Raise_Expression, whose initial type is Any_Type, use
6339 -- the type of the other operand.
6341 if Nkind (L) = N_Raise_Expression then
6342 Set_Etype (L, Etype (R));
6343 Set_Etype (N, Etype (R));
6345 elsif Nkind (R) = N_Raise_Expression then
6346 Set_Etype (R, Etype (L));
6347 Set_Etype (N, Etype (L));
6348 end if;
6350 return;
6352 -- We explicitly check for the case of concatenation of component
6353 -- with component to avoid reporting spurious matching array types
6354 -- that might happen to be lurking in distant packages (such as
6355 -- run-time packages). This also prevents inconsistencies in the
6356 -- messages for certain ACVC B tests, which can vary depending on
6357 -- types declared in run-time interfaces. Another improvement when
6358 -- aggregates are present is to look for a well-typed operand.
6360 elsif Present (Candidate_Type)
6361 and then (Nkind (N) /= N_Op_Concat
6362 or else Is_Array_Type (Etype (L))
6363 or else Is_Array_Type (Etype (R)))
6364 then
6365 if Nkind (N) = N_Op_Concat then
6366 if Etype (L) /= Any_Composite
6367 and then Is_Array_Type (Etype (L))
6368 then
6369 Candidate_Type := Etype (L);
6371 elsif Etype (R) /= Any_Composite
6372 and then Is_Array_Type (Etype (R))
6373 then
6374 Candidate_Type := Etype (R);
6375 end if;
6376 end if;
6378 Error_Msg_NE -- CODEFIX
6379 ("operator for} is not directly visible!",
6380 N, First_Subtype (Candidate_Type));
6382 declare
6383 U : constant Node_Id :=
6384 Cunit (Get_Source_Unit (Candidate_Type));
6385 begin
6386 if Unit_Is_Visible (U) then
6387 Error_Msg_N -- CODEFIX
6388 ("use clause would make operation legal!", N);
6389 else
6390 Error_Msg_NE -- CODEFIX
6391 ("add with_clause and use_clause for&!",
6392 N, Defining_Entity (Unit (U)));
6393 end if;
6394 end;
6395 return;
6397 -- If either operand is a junk operand (e.g. package name), then
6398 -- post appropriate error messages, but do not complain further.
6400 -- Note that the use of OR in this test instead of OR ELSE is
6401 -- quite deliberate, we may as well check both operands in the
6402 -- binary operator case.
6404 elsif Junk_Operand (R)
6405 or -- really mean OR here and not OR ELSE, see above
6406 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
6407 then
6408 return;
6410 -- If we have a logical operator, one of whose operands is
6411 -- Boolean, then we know that the other operand cannot resolve to
6412 -- Boolean (since we got no interpretations), but in that case we
6413 -- pretty much know that the other operand should be Boolean, so
6414 -- resolve it that way (generating an error)
6416 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
6417 if Etype (L) = Standard_Boolean then
6418 Resolve (R, Standard_Boolean);
6419 return;
6420 elsif Etype (R) = Standard_Boolean then
6421 Resolve (L, Standard_Boolean);
6422 return;
6423 end if;
6425 -- For an arithmetic operator or comparison operator, if one
6426 -- of the operands is numeric, then we know the other operand
6427 -- is not the same numeric type. If it is a non-numeric type,
6428 -- then probably it is intended to match the other operand.
6430 elsif Nkind_In (N, N_Op_Add,
6431 N_Op_Divide,
6432 N_Op_Ge,
6433 N_Op_Gt,
6434 N_Op_Le)
6435 or else
6436 Nkind_In (N, N_Op_Lt,
6437 N_Op_Mod,
6438 N_Op_Multiply,
6439 N_Op_Rem,
6440 N_Op_Subtract)
6441 then
6442 -- If Allow_Integer_Address is active, check whether the
6443 -- operation becomes legal after converting an operand.
6445 if Is_Numeric_Type (Etype (L))
6446 and then not Is_Numeric_Type (Etype (R))
6447 then
6448 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6449 Rewrite (R,
6450 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6451 Analyze_Arithmetic_Op (N);
6453 else
6454 Resolve (R, Etype (L));
6455 end if;
6456 return;
6458 elsif Is_Numeric_Type (Etype (R))
6459 and then not Is_Numeric_Type (Etype (L))
6460 then
6461 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
6462 Rewrite (L,
6463 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
6464 Analyze_Arithmetic_Op (N);
6465 return;
6467 else
6468 Resolve (L, Etype (R));
6469 end if;
6471 return;
6473 elsif Allow_Integer_Address
6474 and then Is_Descendent_Of_Address (Etype (L))
6475 and then Is_Descendent_Of_Address (Etype (R))
6476 and then not Error_Posted (N)
6477 then
6478 declare
6479 Addr_Type : constant Entity_Id := Etype (L);
6481 begin
6482 Rewrite (L,
6483 Unchecked_Convert_To (
6484 Standard_Integer, Relocate_Node (L)));
6485 Rewrite (R,
6486 Unchecked_Convert_To (
6487 Standard_Integer, Relocate_Node (R)));
6488 Analyze_Arithmetic_Op (N);
6490 -- If this is an operand in an enclosing arithmetic
6491 -- operation, Convert the result as an address so that
6492 -- arithmetic folding of address can continue.
6494 if Nkind (Parent (N)) in N_Op then
6495 Rewrite (N,
6496 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
6497 end if;
6499 return;
6500 end;
6501 end if;
6503 -- Comparisons on A'Access are common enough to deserve a
6504 -- special message.
6506 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6507 and then Ekind (Etype (L)) = E_Access_Attribute_Type
6508 and then Ekind (Etype (R)) = E_Access_Attribute_Type
6509 then
6510 Error_Msg_N
6511 ("two access attributes cannot be compared directly", N);
6512 Error_Msg_N
6513 ("\use qualified expression for one of the operands",
6515 return;
6517 -- Another one for C programmers
6519 elsif Nkind (N) = N_Op_Concat
6520 and then Valid_Boolean_Arg (Etype (L))
6521 and then Valid_Boolean_Arg (Etype (R))
6522 then
6523 Error_Msg_N ("invalid operands for concatenation", N);
6524 Error_Msg_N -- CODEFIX
6525 ("\maybe AND was meant", N);
6526 return;
6528 -- A special case for comparison of access parameter with null
6530 elsif Nkind (N) = N_Op_Eq
6531 and then Is_Entity_Name (L)
6532 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6533 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6534 N_Access_Definition
6535 and then Nkind (R) = N_Null
6536 then
6537 Error_Msg_N ("access parameter is not allowed to be null", L);
6538 Error_Msg_N ("\(call would raise Constraint_Error)", L);
6539 return;
6541 -- Another special case for exponentiation, where the right
6542 -- operand must be Natural, independently of the base.
6544 elsif Nkind (N) = N_Op_Expon
6545 and then Is_Numeric_Type (Etype (L))
6546 and then not Is_Overloaded (R)
6547 and then
6548 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6549 and then Base_Type (Etype (R)) /= Universal_Integer
6550 then
6551 if Ada_Version >= Ada_2012
6552 and then Has_Dimension_System (Etype (L))
6553 then
6554 Error_Msg_NE
6555 ("exponent for dimensioned type must be a rational" &
6556 ", found}", R, Etype (R));
6557 else
6558 Error_Msg_NE
6559 ("exponent must be of type Natural, found}", R, Etype (R));
6560 end if;
6562 return;
6563 end if;
6565 -- If we fall through then just give general message. Note that in
6566 -- the following messages, if the operand is overloaded we choose
6567 -- an arbitrary type to complain about, but that is probably more
6568 -- useful than not giving a type at all.
6570 if Nkind (N) in N_Unary_Op then
6571 Error_Msg_Node_2 := Etype (R);
6572 Error_Msg_N ("operator& not defined for}", N);
6573 return;
6575 else
6576 if Nkind (N) in N_Binary_Op then
6577 if not Is_Overloaded (L)
6578 and then not Is_Overloaded (R)
6579 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6580 then
6581 Error_Msg_Node_2 := First_Subtype (Etype (R));
6582 Error_Msg_N ("there is no applicable operator& for}", N);
6584 else
6585 -- Another attempt to find a fix: one of the candidate
6586 -- interpretations may not be use-visible. This has
6587 -- already been checked for predefined operators, so
6588 -- we examine only user-defined functions.
6590 Op_Id := Get_Name_Entity_Id (Chars (N));
6592 while Present (Op_Id) loop
6593 if Ekind (Op_Id) /= E_Operator
6594 and then Is_Overloadable (Op_Id)
6595 then
6596 if not Is_Immediately_Visible (Op_Id)
6597 and then not In_Use (Scope (Op_Id))
6598 and then not Is_Abstract_Subprogram (Op_Id)
6599 and then not Is_Hidden (Op_Id)
6600 and then Ekind (Scope (Op_Id)) = E_Package
6601 and then
6602 Has_Compatible_Type
6603 (L, Etype (First_Formal (Op_Id)))
6604 and then Present
6605 (Next_Formal (First_Formal (Op_Id)))
6606 and then
6607 Has_Compatible_Type
6609 Etype (Next_Formal (First_Formal (Op_Id))))
6610 then
6611 Error_Msg_N
6612 ("No legal interpretation for operator&", N);
6613 Error_Msg_NE
6614 ("\use clause on& would make operation legal",
6615 N, Scope (Op_Id));
6616 exit;
6617 end if;
6618 end if;
6620 Op_Id := Homonym (Op_Id);
6621 end loop;
6623 if No (Op_Id) then
6624 Error_Msg_N ("invalid operand types for operator&", N);
6626 if Nkind (N) /= N_Op_Concat then
6627 Error_Msg_NE ("\left operand has}!", N, Etype (L));
6628 Error_Msg_NE ("\right operand has}!", N, Etype (R));
6630 -- For concatenation operators it is more difficult to
6631 -- determine which is the wrong operand. It is worth
6632 -- flagging explicitly an access type, for those who
6633 -- might think that a dereference happens here.
6635 elsif Is_Access_Type (Etype (L)) then
6636 Error_Msg_N ("\left operand is access type", N);
6638 elsif Is_Access_Type (Etype (R)) then
6639 Error_Msg_N ("\right operand is access type", N);
6640 end if;
6641 end if;
6642 end if;
6643 end if;
6644 end if;
6645 end;
6646 end if;
6647 end Operator_Check;
6649 -----------------------------------------
6650 -- Process_Implicit_Dereference_Prefix --
6651 -----------------------------------------
6653 function Process_Implicit_Dereference_Prefix
6654 (E : Entity_Id;
6655 P : Entity_Id) return Entity_Id
6657 Ref : Node_Id;
6658 Typ : constant Entity_Id := Designated_Type (Etype (P));
6660 begin
6661 if Present (E)
6662 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6663 then
6664 -- We create a dummy reference to E to ensure that the reference is
6665 -- not considered as part of an assignment (an implicit dereference
6666 -- can never assign to its prefix). The Comes_From_Source attribute
6667 -- needs to be propagated for accurate warnings.
6669 Ref := New_Occurrence_Of (E, Sloc (P));
6670 Set_Comes_From_Source (Ref, Comes_From_Source (P));
6671 Generate_Reference (E, Ref);
6672 end if;
6674 -- An implicit dereference is a legal occurrence of an incomplete type
6675 -- imported through a limited_with clause, if the full view is visible.
6677 if From_Limited_With (Typ)
6678 and then not From_Limited_With (Scope (Typ))
6679 and then
6680 (Is_Immediately_Visible (Scope (Typ))
6681 or else
6682 (Is_Child_Unit (Scope (Typ))
6683 and then Is_Visible_Lib_Unit (Scope (Typ))))
6684 then
6685 return Available_View (Typ);
6686 else
6687 return Typ;
6688 end if;
6689 end Process_Implicit_Dereference_Prefix;
6691 --------------------------------
6692 -- Remove_Abstract_Operations --
6693 --------------------------------
6695 procedure Remove_Abstract_Operations (N : Node_Id) is
6696 Abstract_Op : Entity_Id := Empty;
6697 Address_Descendent : Boolean := False;
6698 I : Interp_Index;
6699 It : Interp;
6701 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
6702 -- activate this if either extensions are enabled, or if the abstract
6703 -- operation in question comes from a predefined file. This latter test
6704 -- allows us to use abstract to make operations invisible to users. In
6705 -- particular, if type Address is non-private and abstract subprograms
6706 -- are used to hide its operators, they will be truly hidden.
6708 type Operand_Position is (First_Op, Second_Op);
6709 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6711 procedure Remove_Address_Interpretations (Op : Operand_Position);
6712 -- Ambiguities may arise when the operands are literal and the address
6713 -- operations in s-auxdec are visible. In that case, remove the
6714 -- interpretation of a literal as Address, to retain the semantics
6715 -- of Address as a private type.
6717 ------------------------------------
6718 -- Remove_Address_Interpretations --
6719 ------------------------------------
6721 procedure Remove_Address_Interpretations (Op : Operand_Position) is
6722 Formal : Entity_Id;
6724 begin
6725 if Is_Overloaded (N) then
6726 Get_First_Interp (N, I, It);
6727 while Present (It.Nam) loop
6728 Formal := First_Entity (It.Nam);
6730 if Op = Second_Op then
6731 Formal := Next_Entity (Formal);
6732 end if;
6734 if Is_Descendent_Of_Address (Etype (Formal)) then
6735 Address_Descendent := True;
6736 Remove_Interp (I);
6737 end if;
6739 Get_Next_Interp (I, It);
6740 end loop;
6741 end if;
6742 end Remove_Address_Interpretations;
6744 -- Start of processing for Remove_Abstract_Operations
6746 begin
6747 if Is_Overloaded (N) then
6748 if Debug_Flag_V then
6749 Write_Str ("Remove_Abstract_Operations: ");
6750 Write_Overloads (N);
6751 end if;
6753 Get_First_Interp (N, I, It);
6755 while Present (It.Nam) loop
6756 if Is_Overloadable (It.Nam)
6757 and then Is_Abstract_Subprogram (It.Nam)
6758 and then not Is_Dispatching_Operation (It.Nam)
6759 then
6760 Abstract_Op := It.Nam;
6762 if Is_Descendent_Of_Address (It.Typ) then
6763 Address_Descendent := True;
6764 Remove_Interp (I);
6765 exit;
6767 -- In Ada 2005, this operation does not participate in overload
6768 -- resolution. If the operation is defined in a predefined
6769 -- unit, it is one of the operations declared abstract in some
6770 -- variants of System, and it must be removed as well.
6772 elsif Ada_Version >= Ada_2005
6773 or else Is_Predefined_File_Name
6774 (Unit_File_Name (Get_Source_Unit (It.Nam)))
6775 then
6776 Remove_Interp (I);
6777 exit;
6778 end if;
6779 end if;
6781 Get_Next_Interp (I, It);
6782 end loop;
6784 if No (Abstract_Op) then
6786 -- If some interpretation yields an integer type, it is still
6787 -- possible that there are address interpretations. Remove them
6788 -- if one operand is a literal, to avoid spurious ambiguities
6789 -- on systems where Address is a visible integer type.
6791 if Is_Overloaded (N)
6792 and then Nkind (N) in N_Op
6793 and then Is_Integer_Type (Etype (N))
6794 then
6795 if Nkind (N) in N_Binary_Op then
6796 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6797 Remove_Address_Interpretations (Second_Op);
6799 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6800 Remove_Address_Interpretations (First_Op);
6801 end if;
6802 end if;
6803 end if;
6805 elsif Nkind (N) in N_Op then
6807 -- Remove interpretations that treat literals as addresses. This
6808 -- is never appropriate, even when Address is defined as a visible
6809 -- Integer type. The reason is that we would really prefer Address
6810 -- to behave as a private type, even in this case, which is there
6811 -- only to accommodate oddities of VMS address sizes. If Address
6812 -- is a visible integer type, we get lots of overload ambiguities.
6814 if Nkind (N) in N_Binary_Op then
6815 declare
6816 U1 : constant Boolean :=
6817 Present (Universal_Interpretation (Right_Opnd (N)));
6818 U2 : constant Boolean :=
6819 Present (Universal_Interpretation (Left_Opnd (N)));
6821 begin
6822 if U1 then
6823 Remove_Address_Interpretations (Second_Op);
6824 end if;
6826 if U2 then
6827 Remove_Address_Interpretations (First_Op);
6828 end if;
6830 if not (U1 and U2) then
6832 -- Remove corresponding predefined operator, which is
6833 -- always added to the overload set.
6835 Get_First_Interp (N, I, It);
6836 while Present (It.Nam) loop
6837 if Scope (It.Nam) = Standard_Standard
6838 and then Base_Type (It.Typ) =
6839 Base_Type (Etype (Abstract_Op))
6840 then
6841 Remove_Interp (I);
6842 end if;
6844 Get_Next_Interp (I, It);
6845 end loop;
6847 elsif Is_Overloaded (N)
6848 and then Present (Univ_Type)
6849 then
6850 -- If both operands have a universal interpretation,
6851 -- it is still necessary to remove interpretations that
6852 -- yield Address. Any remaining ambiguities will be
6853 -- removed in Disambiguate.
6855 Get_First_Interp (N, I, It);
6856 while Present (It.Nam) loop
6857 if Is_Descendent_Of_Address (It.Typ) then
6858 Remove_Interp (I);
6860 elsif not Is_Type (It.Nam) then
6861 Set_Entity (N, It.Nam);
6862 end if;
6864 Get_Next_Interp (I, It);
6865 end loop;
6866 end if;
6867 end;
6868 end if;
6870 elsif Nkind (N) = N_Function_Call
6871 and then
6872 (Nkind (Name (N)) = N_Operator_Symbol
6873 or else
6874 (Nkind (Name (N)) = N_Expanded_Name
6875 and then
6876 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6877 then
6879 declare
6880 Arg1 : constant Node_Id := First (Parameter_Associations (N));
6881 U1 : constant Boolean :=
6882 Present (Universal_Interpretation (Arg1));
6883 U2 : constant Boolean :=
6884 Present (Next (Arg1)) and then
6885 Present (Universal_Interpretation (Next (Arg1)));
6887 begin
6888 if U1 then
6889 Remove_Address_Interpretations (First_Op);
6890 end if;
6892 if U2 then
6893 Remove_Address_Interpretations (Second_Op);
6894 end if;
6896 if not (U1 and U2) then
6897 Get_First_Interp (N, I, It);
6898 while Present (It.Nam) loop
6899 if Scope (It.Nam) = Standard_Standard
6900 and then It.Typ = Base_Type (Etype (Abstract_Op))
6901 then
6902 Remove_Interp (I);
6903 end if;
6905 Get_Next_Interp (I, It);
6906 end loop;
6907 end if;
6908 end;
6909 end if;
6911 -- If the removal has left no valid interpretations, emit an error
6912 -- message now and label node as illegal.
6914 if Present (Abstract_Op) then
6915 Get_First_Interp (N, I, It);
6917 if No (It.Nam) then
6919 -- Removal of abstract operation left no viable candidate
6921 Set_Etype (N, Any_Type);
6922 Error_Msg_Sloc := Sloc (Abstract_Op);
6923 Error_Msg_NE
6924 ("cannot call abstract operation& declared#", N, Abstract_Op);
6926 -- In Ada 2005, an abstract operation may disable predefined
6927 -- operators. Since the context is not yet known, we mark the
6928 -- predefined operators as potentially hidden. Do not include
6929 -- predefined operators when addresses are involved since this
6930 -- case is handled separately.
6932 elsif Ada_Version >= Ada_2005 and then not Address_Descendent then
6933 while Present (It.Nam) loop
6934 if Is_Numeric_Type (It.Typ)
6935 and then Scope (It.Typ) = Standard_Standard
6936 then
6937 Set_Abstract_Op (I, Abstract_Op);
6938 end if;
6940 Get_Next_Interp (I, It);
6941 end loop;
6942 end if;
6943 end if;
6945 if Debug_Flag_V then
6946 Write_Str ("Remove_Abstract_Operations done: ");
6947 Write_Overloads (N);
6948 end if;
6949 end if;
6950 end Remove_Abstract_Operations;
6952 ----------------------------
6953 -- Try_Container_Indexing --
6954 ----------------------------
6956 function Try_Container_Indexing
6957 (N : Node_Id;
6958 Prefix : Node_Id;
6959 Exprs : List_Id) return Boolean
6961 Loc : constant Source_Ptr := Sloc (N);
6962 Assoc : List_Id;
6963 Disc : Entity_Id;
6964 Func : Entity_Id;
6965 Func_Name : Node_Id;
6966 Indexing : Node_Id;
6968 begin
6970 -- Check whether type has a specified indexing aspect
6972 Func_Name := Empty;
6974 if Is_Variable (Prefix) then
6975 Func_Name :=
6976 Find_Value_Of_Aspect (Etype (Prefix), Aspect_Variable_Indexing);
6977 end if;
6979 if No (Func_Name) then
6980 Func_Name :=
6981 Find_Value_Of_Aspect (Etype (Prefix), Aspect_Constant_Indexing);
6982 end if;
6984 -- If aspect does not exist the expression is illegal. Error is
6985 -- diagnosed in caller.
6987 if No (Func_Name) then
6989 -- The prefix itself may be an indexing of a container: rewrite
6990 -- as such and re-analyze.
6992 if Has_Implicit_Dereference (Etype (Prefix)) then
6993 Build_Explicit_Dereference
6994 (Prefix, First_Discriminant (Etype (Prefix)));
6995 return Try_Container_Indexing (N, Prefix, Exprs);
6997 else
6998 return False;
6999 end if;
7001 -- If the container type is derived from another container type, the
7002 -- value of the inherited aspect is the Reference operation declared
7003 -- for the parent type.
7005 -- However, Reference is also a primitive operation of the type, and
7006 -- the inherited operation has a different signature. We retrieve the
7007 -- right one from the list of primitive operations of the derived type.
7009 -- Note that predefined containers are typically all derived from one
7010 -- of the Controlled types. The code below is motivated by containers
7011 -- that are derived from other types with a Reference aspect.
7013 -- Additional machinery may be needed for types that have several user-
7014 -- defined Reference operations with different signatures ???
7016 elsif Is_Derived_Type (Etype (Prefix))
7017 and then Etype (First_Formal (Entity (Func_Name))) /= Etype (Prefix)
7018 then
7019 Func := Find_Prim_Op (Etype (Prefix), Chars (Func_Name));
7020 Func_Name := New_Occurrence_Of (Func, Loc);
7021 end if;
7023 Assoc := New_List (Relocate_Node (Prefix));
7025 -- A generalized indexing may have nore than one index expression, so
7026 -- transfer all of them to the argument list to be used in the call.
7027 -- Note that there may be named associations, in which case the node
7028 -- was rewritten earlier as a call, and has been transformed back into
7029 -- an indexed expression to share the following processing.
7031 -- The generalized indexing node is the one on which analysis and
7032 -- resolution take place. Before expansion the original node is replaced
7033 -- with the generalized indexing node, which is a call, possibly with
7034 -- a dereference operation.
7036 if Comes_From_Source (N) then
7037 Check_Compiler_Unit ("generalized indexing", N);
7038 end if;
7040 declare
7041 Arg : Node_Id;
7042 begin
7043 Arg := First (Exprs);
7044 while Present (Arg) loop
7045 Append (Relocate_Node (Arg), Assoc);
7046 Next (Arg);
7047 end loop;
7048 end;
7050 if not Is_Overloaded (Func_Name) then
7051 Func := Entity (Func_Name);
7052 Indexing :=
7053 Make_Function_Call (Loc,
7054 Name => New_Occurrence_Of (Func, Loc),
7055 Parameter_Associations => Assoc);
7056 Set_Parent (Indexing, Parent (N));
7057 Set_Generalized_Indexing (N, Indexing);
7058 Analyze (Indexing);
7059 Set_Etype (N, Etype (Indexing));
7061 -- If the return type of the indexing function is a reference type,
7062 -- add the dereference as a possible interpretation. Note that the
7063 -- indexing aspect may be a function that returns the element type
7064 -- with no intervening implicit dereference, and that the reference
7065 -- discriminant is not the first discriminant.
7067 if Has_Discriminants (Etype (Func)) then
7068 Disc := First_Discriminant (Etype (Func));
7069 while Present (Disc) loop
7070 declare
7071 Elmt_Type : Entity_Id;
7072 begin
7073 if Has_Implicit_Dereference (Disc) then
7074 Elmt_Type := Designated_Type (Etype (Disc));
7075 Add_One_Interp (Indexing, Disc, Elmt_Type);
7076 Add_One_Interp (N, Disc, Elmt_Type);
7077 exit;
7078 end if;
7079 end;
7081 Next_Discriminant (Disc);
7082 end loop;
7083 end if;
7085 else
7086 Indexing :=
7087 Make_Function_Call (Loc,
7088 Name => Make_Identifier (Loc, Chars (Func_Name)),
7089 Parameter_Associations => Assoc);
7091 Set_Parent (Indexing, Parent (N));
7092 Set_Generalized_Indexing (N, Indexing);
7094 declare
7095 I : Interp_Index;
7096 It : Interp;
7097 Success : Boolean;
7099 begin
7100 Get_First_Interp (Func_Name, I, It);
7101 Set_Etype (Indexing, Any_Type);
7102 while Present (It.Nam) loop
7103 Analyze_One_Call (Indexing, It.Nam, False, Success);
7105 if Success then
7106 Set_Etype (Name (Indexing), It.Typ);
7107 Set_Entity (Name (Indexing), It.Nam);
7108 Set_Etype (N, Etype (Indexing));
7110 -- Add implicit dereference interpretation
7112 if Has_Discriminants (Etype (It.Nam)) then
7113 Disc := First_Discriminant (Etype (It.Nam));
7114 while Present (Disc) loop
7115 if Has_Implicit_Dereference (Disc) then
7116 Add_One_Interp
7117 (Indexing, Disc, Designated_Type (Etype (Disc)));
7118 Add_One_Interp
7119 (N, Disc, Designated_Type (Etype (Disc)));
7120 exit;
7121 end if;
7123 Next_Discriminant (Disc);
7124 end loop;
7125 end if;
7127 exit;
7128 end if;
7130 Get_Next_Interp (I, It);
7131 end loop;
7132 end;
7133 end if;
7135 if Etype (Indexing) = Any_Type then
7136 Error_Msg_NE
7137 ("container cannot be indexed with&", N, Etype (First (Exprs)));
7138 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
7139 end if;
7141 return True;
7142 end Try_Container_Indexing;
7144 -----------------------
7145 -- Try_Indirect_Call --
7146 -----------------------
7148 function Try_Indirect_Call
7149 (N : Node_Id;
7150 Nam : Entity_Id;
7151 Typ : Entity_Id) return Boolean
7153 Actual : Node_Id;
7154 Formal : Entity_Id;
7156 Call_OK : Boolean;
7157 pragma Warnings (Off, Call_OK);
7159 begin
7160 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
7162 Actual := First_Actual (N);
7163 Formal := First_Formal (Designated_Type (Typ));
7164 while Present (Actual) and then Present (Formal) loop
7165 if not Has_Compatible_Type (Actual, Etype (Formal)) then
7166 return False;
7167 end if;
7169 Next (Actual);
7170 Next_Formal (Formal);
7171 end loop;
7173 if No (Actual) and then No (Formal) then
7174 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
7176 -- Nam is a candidate interpretation for the name in the call,
7177 -- if it is not an indirect call.
7179 if not Is_Type (Nam)
7180 and then Is_Entity_Name (Name (N))
7181 then
7182 Set_Entity (Name (N), Nam);
7183 end if;
7185 return True;
7187 else
7188 return False;
7189 end if;
7190 end Try_Indirect_Call;
7192 ----------------------
7193 -- Try_Indexed_Call --
7194 ----------------------
7196 function Try_Indexed_Call
7197 (N : Node_Id;
7198 Nam : Entity_Id;
7199 Typ : Entity_Id;
7200 Skip_First : Boolean) return Boolean
7202 Loc : constant Source_Ptr := Sloc (N);
7203 Actuals : constant List_Id := Parameter_Associations (N);
7204 Actual : Node_Id;
7205 Index : Entity_Id;
7207 begin
7208 Actual := First (Actuals);
7210 -- If the call was originally written in prefix form, skip the first
7211 -- actual, which is obviously not defaulted.
7213 if Skip_First then
7214 Next (Actual);
7215 end if;
7217 Index := First_Index (Typ);
7218 while Present (Actual) and then Present (Index) loop
7220 -- If the parameter list has a named association, the expression
7221 -- is definitely a call and not an indexed component.
7223 if Nkind (Actual) = N_Parameter_Association then
7224 return False;
7225 end if;
7227 if Is_Entity_Name (Actual)
7228 and then Is_Type (Entity (Actual))
7229 and then No (Next (Actual))
7230 then
7231 -- A single actual that is a type name indicates a slice if the
7232 -- type is discrete, and an error otherwise.
7234 if Is_Discrete_Type (Entity (Actual)) then
7235 Rewrite (N,
7236 Make_Slice (Loc,
7237 Prefix =>
7238 Make_Function_Call (Loc,
7239 Name => Relocate_Node (Name (N))),
7240 Discrete_Range =>
7241 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
7243 Analyze (N);
7245 else
7246 Error_Msg_N ("invalid use of type in expression", Actual);
7247 Set_Etype (N, Any_Type);
7248 end if;
7250 return True;
7252 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
7253 return False;
7254 end if;
7256 Next (Actual);
7257 Next_Index (Index);
7258 end loop;
7260 if No (Actual) and then No (Index) then
7261 Add_One_Interp (N, Nam, Component_Type (Typ));
7263 -- Nam is a candidate interpretation for the name in the call,
7264 -- if it is not an indirect call.
7266 if not Is_Type (Nam)
7267 and then Is_Entity_Name (Name (N))
7268 then
7269 Set_Entity (Name (N), Nam);
7270 end if;
7272 return True;
7273 else
7274 return False;
7275 end if;
7276 end Try_Indexed_Call;
7278 --------------------------
7279 -- Try_Object_Operation --
7280 --------------------------
7282 function Try_Object_Operation
7283 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
7285 K : constant Node_Kind := Nkind (Parent (N));
7286 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
7287 Loc : constant Source_Ptr := Sloc (N);
7288 Obj : constant Node_Id := Prefix (N);
7290 Subprog : constant Node_Id :=
7291 Make_Identifier (Sloc (Selector_Name (N)),
7292 Chars => Chars (Selector_Name (N)));
7293 -- Identifier on which possible interpretations will be collected
7295 Report_Error : Boolean := False;
7296 -- If no candidate interpretation matches the context, redo analysis
7297 -- with Report_Error True to provide additional information.
7299 Actual : Node_Id;
7300 Candidate : Entity_Id := Empty;
7301 New_Call_Node : Node_Id := Empty;
7302 Node_To_Replace : Node_Id;
7303 Obj_Type : Entity_Id := Etype (Obj);
7304 Success : Boolean := False;
7306 function Valid_Candidate
7307 (Success : Boolean;
7308 Call : Node_Id;
7309 Subp : Entity_Id) return Entity_Id;
7310 -- If the subprogram is a valid interpretation, record it, and add
7311 -- to the list of interpretations of Subprog. Otherwise return Empty.
7313 procedure Complete_Object_Operation
7314 (Call_Node : Node_Id;
7315 Node_To_Replace : Node_Id);
7316 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
7317 -- Call_Node, insert the object (or its dereference) as the first actual
7318 -- in the call, and complete the analysis of the call.
7320 procedure Report_Ambiguity (Op : Entity_Id);
7321 -- If a prefixed procedure call is ambiguous, indicate whether the
7322 -- call includes an implicit dereference or an implicit 'Access.
7324 procedure Transform_Object_Operation
7325 (Call_Node : out Node_Id;
7326 Node_To_Replace : out Node_Id);
7327 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
7328 -- Call_Node is the resulting subprogram call, Node_To_Replace is
7329 -- either N or the parent of N, and Subprog is a reference to the
7330 -- subprogram we are trying to match.
7332 function Try_Class_Wide_Operation
7333 (Call_Node : Node_Id;
7334 Node_To_Replace : Node_Id) return Boolean;
7335 -- Traverse all ancestor types looking for a class-wide subprogram
7336 -- for which the current operation is a valid non-dispatching call.
7338 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
7339 -- If prefix is overloaded, its interpretation may include different
7340 -- tagged types, and we must examine the primitive operations and
7341 -- the class-wide operations of each in order to find candidate
7342 -- interpretations for the call as a whole.
7344 function Try_Primitive_Operation
7345 (Call_Node : Node_Id;
7346 Node_To_Replace : Node_Id) return Boolean;
7347 -- Traverse the list of primitive subprograms looking for a dispatching
7348 -- operation for which the current node is a valid call .
7350 ---------------------
7351 -- Valid_Candidate --
7352 ---------------------
7354 function Valid_Candidate
7355 (Success : Boolean;
7356 Call : Node_Id;
7357 Subp : Entity_Id) return Entity_Id
7359 Arr_Type : Entity_Id;
7360 Comp_Type : Entity_Id;
7362 begin
7363 -- If the subprogram is a valid interpretation, record it in global
7364 -- variable Subprog, to collect all possible overloadings.
7366 if Success then
7367 if Subp /= Entity (Subprog) then
7368 Add_One_Interp (Subprog, Subp, Etype (Subp));
7369 end if;
7370 end if;
7372 -- If the call may be an indexed call, retrieve component type of
7373 -- resulting expression, and add possible interpretation.
7375 Arr_Type := Empty;
7376 Comp_Type := Empty;
7378 if Nkind (Call) = N_Function_Call
7379 and then Nkind (Parent (N)) = N_Indexed_Component
7380 and then Needs_One_Actual (Subp)
7381 then
7382 if Is_Array_Type (Etype (Subp)) then
7383 Arr_Type := Etype (Subp);
7385 elsif Is_Access_Type (Etype (Subp))
7386 and then Is_Array_Type (Designated_Type (Etype (Subp)))
7387 then
7388 Arr_Type := Designated_Type (Etype (Subp));
7389 end if;
7390 end if;
7392 if Present (Arr_Type) then
7394 -- Verify that the actuals (excluding the object) match the types
7395 -- of the indexes.
7397 declare
7398 Actual : Node_Id;
7399 Index : Node_Id;
7401 begin
7402 Actual := Next (First_Actual (Call));
7403 Index := First_Index (Arr_Type);
7404 while Present (Actual) and then Present (Index) loop
7405 if not Has_Compatible_Type (Actual, Etype (Index)) then
7406 Arr_Type := Empty;
7407 exit;
7408 end if;
7410 Next_Actual (Actual);
7411 Next_Index (Index);
7412 end loop;
7414 if No (Actual)
7415 and then No (Index)
7416 and then Present (Arr_Type)
7417 then
7418 Comp_Type := Component_Type (Arr_Type);
7419 end if;
7420 end;
7422 if Present (Comp_Type)
7423 and then Etype (Subprog) /= Comp_Type
7424 then
7425 Add_One_Interp (Subprog, Subp, Comp_Type);
7426 end if;
7427 end if;
7429 if Etype (Call) /= Any_Type then
7430 return Subp;
7431 else
7432 return Empty;
7433 end if;
7434 end Valid_Candidate;
7436 -------------------------------
7437 -- Complete_Object_Operation --
7438 -------------------------------
7440 procedure Complete_Object_Operation
7441 (Call_Node : Node_Id;
7442 Node_To_Replace : Node_Id)
7444 Control : constant Entity_Id := First_Formal (Entity (Subprog));
7445 Formal_Type : constant Entity_Id := Etype (Control);
7446 First_Actual : Node_Id;
7448 begin
7449 -- Place the name of the operation, with its interpretations,
7450 -- on the rewritten call.
7452 Set_Name (Call_Node, Subprog);
7454 First_Actual := First (Parameter_Associations (Call_Node));
7456 -- For cross-reference purposes, treat the new node as being in the
7457 -- source if the original one is. Set entity and type, even though
7458 -- they may be overwritten during resolution if overloaded.
7460 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
7461 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
7463 if Nkind (N) = N_Selected_Component
7464 and then not Inside_A_Generic
7465 then
7466 Set_Entity (Selector_Name (N), Entity (Subprog));
7467 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
7468 end if;
7470 -- If need be, rewrite first actual as an explicit dereference. If
7471 -- the call is overloaded, the rewriting can only be done once the
7472 -- primitive operation is identified.
7474 if Is_Overloaded (Subprog) then
7476 -- The prefix itself may be overloaded, and its interpretations
7477 -- must be propagated to the new actual in the call.
7479 if Is_Overloaded (Obj) then
7480 Save_Interps (Obj, First_Actual);
7481 end if;
7483 Rewrite (First_Actual, Obj);
7485 elsif not Is_Access_Type (Formal_Type)
7486 and then Is_Access_Type (Etype (Obj))
7487 then
7488 Rewrite (First_Actual,
7489 Make_Explicit_Dereference (Sloc (Obj), Obj));
7490 Analyze (First_Actual);
7492 -- If we need to introduce an explicit dereference, verify that
7493 -- the resulting actual is compatible with the mode of the formal.
7495 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
7496 and then Is_Access_Constant (Etype (Obj))
7497 then
7498 Error_Msg_NE
7499 ("expect variable in call to&", Prefix (N), Entity (Subprog));
7500 end if;
7502 -- Conversely, if the formal is an access parameter and the object
7503 -- is not, replace the actual with a 'Access reference. Its analysis
7504 -- will check that the object is aliased.
7506 elsif Is_Access_Type (Formal_Type)
7507 and then not Is_Access_Type (Etype (Obj))
7508 then
7509 -- A special case: A.all'access is illegal if A is an access to a
7510 -- constant and the context requires an access to a variable.
7512 if not Is_Access_Constant (Formal_Type) then
7513 if (Nkind (Obj) = N_Explicit_Dereference
7514 and then Is_Access_Constant (Etype (Prefix (Obj))))
7515 or else not Is_Variable (Obj)
7516 then
7517 Error_Msg_NE
7518 ("actual for& must be a variable", Obj, Control);
7519 end if;
7520 end if;
7522 Rewrite (First_Actual,
7523 Make_Attribute_Reference (Loc,
7524 Attribute_Name => Name_Access,
7525 Prefix => Relocate_Node (Obj)));
7527 if not Is_Aliased_View (Obj) then
7528 Error_Msg_NE
7529 ("object in prefixed call to& must be aliased"
7530 & " (RM-2005 4.3.1 (13))",
7531 Prefix (First_Actual), Subprog);
7532 end if;
7534 Analyze (First_Actual);
7536 else
7537 if Is_Overloaded (Obj) then
7538 Save_Interps (Obj, First_Actual);
7539 end if;
7541 Rewrite (First_Actual, Obj);
7542 end if;
7544 Rewrite (Node_To_Replace, Call_Node);
7546 -- Propagate the interpretations collected in subprog to the new
7547 -- function call node, to be resolved from context.
7549 if Is_Overloaded (Subprog) then
7550 Save_Interps (Subprog, Node_To_Replace);
7552 else
7553 -- The type of the subprogram may be a limited view obtained
7554 -- transitively from another unit. If full view is available,
7555 -- use it to analyze call.
7557 declare
7558 T : constant Entity_Id := Etype (Subprog);
7559 begin
7560 if From_Limited_With (T) then
7561 Set_Etype (Entity (Subprog), Available_View (T));
7562 end if;
7563 end;
7565 Analyze (Node_To_Replace);
7567 -- If the operation has been rewritten into a call, which may get
7568 -- subsequently an explicit dereference, preserve the type on the
7569 -- original node (selected component or indexed component) for
7570 -- subsequent legality tests, e.g. Is_Variable. which examines
7571 -- the original node.
7573 if Nkind (Node_To_Replace) = N_Function_Call then
7574 Set_Etype
7575 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
7576 end if;
7577 end if;
7578 end Complete_Object_Operation;
7580 ----------------------
7581 -- Report_Ambiguity --
7582 ----------------------
7584 procedure Report_Ambiguity (Op : Entity_Id) is
7585 Access_Actual : constant Boolean :=
7586 Is_Access_Type (Etype (Prefix (N)));
7587 Access_Formal : Boolean := False;
7589 begin
7590 Error_Msg_Sloc := Sloc (Op);
7592 if Present (First_Formal (Op)) then
7593 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
7594 end if;
7596 if Access_Formal and then not Access_Actual then
7597 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7598 Error_Msg_N
7599 ("\possible interpretation "
7600 & "(inherited, with implicit 'Access) #", N);
7601 else
7602 Error_Msg_N
7603 ("\possible interpretation (with implicit 'Access) #", N);
7604 end if;
7606 elsif not Access_Formal and then Access_Actual then
7607 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7608 Error_Msg_N
7609 ("\possible interpretation "
7610 & "(inherited, with implicit dereference) #", N);
7611 else
7612 Error_Msg_N
7613 ("\possible interpretation (with implicit dereference) #", N);
7614 end if;
7616 else
7617 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7618 Error_Msg_N ("\possible interpretation (inherited)#", N);
7619 else
7620 Error_Msg_N -- CODEFIX
7621 ("\possible interpretation#", N);
7622 end if;
7623 end if;
7624 end Report_Ambiguity;
7626 --------------------------------
7627 -- Transform_Object_Operation --
7628 --------------------------------
7630 procedure Transform_Object_Operation
7631 (Call_Node : out Node_Id;
7632 Node_To_Replace : out Node_Id)
7634 Dummy : constant Node_Id := New_Copy (Obj);
7635 -- Placeholder used as a first parameter in the call, replaced
7636 -- eventually by the proper object.
7638 Parent_Node : constant Node_Id := Parent (N);
7640 Actual : Node_Id;
7641 Actuals : List_Id;
7643 begin
7644 -- Common case covering 1) Call to a procedure and 2) Call to a
7645 -- function that has some additional actuals.
7647 if Nkind (Parent_Node) in N_Subprogram_Call
7649 -- N is a selected component node containing the name of the
7650 -- subprogram. If N is not the name of the parent node we must
7651 -- not replace the parent node by the new construct. This case
7652 -- occurs when N is a parameterless call to a subprogram that
7653 -- is an actual parameter of a call to another subprogram. For
7654 -- example:
7655 -- Some_Subprogram (..., Obj.Operation, ...)
7657 and then Name (Parent_Node) = N
7658 then
7659 Node_To_Replace := Parent_Node;
7661 Actuals := Parameter_Associations (Parent_Node);
7663 if Present (Actuals) then
7664 Prepend (Dummy, Actuals);
7665 else
7666 Actuals := New_List (Dummy);
7667 end if;
7669 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
7670 Call_Node :=
7671 Make_Procedure_Call_Statement (Loc,
7672 Name => New_Copy (Subprog),
7673 Parameter_Associations => Actuals);
7675 else
7676 Call_Node :=
7677 Make_Function_Call (Loc,
7678 Name => New_Copy (Subprog),
7679 Parameter_Associations => Actuals);
7680 end if;
7682 -- Before analysis, a function call appears as an indexed component
7683 -- if there are no named associations.
7685 elsif Nkind (Parent_Node) = N_Indexed_Component
7686 and then N = Prefix (Parent_Node)
7687 then
7688 Node_To_Replace := Parent_Node;
7689 Actuals := Expressions (Parent_Node);
7691 Actual := First (Actuals);
7692 while Present (Actual) loop
7693 Analyze (Actual);
7694 Next (Actual);
7695 end loop;
7697 Prepend (Dummy, Actuals);
7699 Call_Node :=
7700 Make_Function_Call (Loc,
7701 Name => New_Copy (Subprog),
7702 Parameter_Associations => Actuals);
7704 -- Parameterless call: Obj.F is rewritten as F (Obj)
7706 else
7707 Node_To_Replace := N;
7709 Call_Node :=
7710 Make_Function_Call (Loc,
7711 Name => New_Copy (Subprog),
7712 Parameter_Associations => New_List (Dummy));
7713 end if;
7714 end Transform_Object_Operation;
7716 ------------------------------
7717 -- Try_Class_Wide_Operation --
7718 ------------------------------
7720 function Try_Class_Wide_Operation
7721 (Call_Node : Node_Id;
7722 Node_To_Replace : Node_Id) return Boolean
7724 Anc_Type : Entity_Id;
7725 Matching_Op : Entity_Id := Empty;
7726 Error : Boolean;
7728 procedure Traverse_Homonyms
7729 (Anc_Type : Entity_Id;
7730 Error : out Boolean);
7731 -- Traverse the homonym chain of the subprogram searching for those
7732 -- homonyms whose first formal has the Anc_Type's class-wide type,
7733 -- or an anonymous access type designating the class-wide type. If
7734 -- an ambiguity is detected, then Error is set to True.
7736 procedure Traverse_Interfaces
7737 (Anc_Type : Entity_Id;
7738 Error : out Boolean);
7739 -- Traverse the list of interfaces, if any, associated with Anc_Type
7740 -- and search for acceptable class-wide homonyms associated with each
7741 -- interface. If an ambiguity is detected, then Error is set to True.
7743 -----------------------
7744 -- Traverse_Homonyms --
7745 -----------------------
7747 procedure Traverse_Homonyms
7748 (Anc_Type : Entity_Id;
7749 Error : out Boolean)
7751 Cls_Type : Entity_Id;
7752 Hom : Entity_Id;
7753 Hom_Ref : Node_Id;
7754 Success : Boolean;
7756 begin
7757 Error := False;
7759 Cls_Type := Class_Wide_Type (Anc_Type);
7761 Hom := Current_Entity (Subprog);
7763 -- Find a non-hidden operation whose first parameter is of the
7764 -- class-wide type, a subtype thereof, or an anonymous access
7765 -- to same. If in an instance, the operation can be considered
7766 -- even if hidden (it may be hidden because the instantiation
7767 -- is expanded after the containing package has been analyzed).
7769 while Present (Hom) loop
7770 if Ekind_In (Hom, E_Procedure, E_Function)
7771 and then (not Is_Hidden (Hom) or else In_Instance)
7772 and then Scope (Hom) = Scope (Anc_Type)
7773 and then Present (First_Formal (Hom))
7774 and then
7775 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7776 or else
7777 (Is_Access_Type (Etype (First_Formal (Hom)))
7778 and then
7779 Ekind (Etype (First_Formal (Hom))) =
7780 E_Anonymous_Access_Type
7781 and then
7782 Base_Type
7783 (Designated_Type (Etype (First_Formal (Hom)))) =
7784 Cls_Type))
7785 then
7786 -- If the context is a procedure call, ignore functions
7787 -- in the name of the call.
7789 if Ekind (Hom) = E_Function
7790 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7791 and then N = Name (Parent (N))
7792 then
7793 goto Next_Hom;
7795 -- If the context is a function call, ignore procedures
7796 -- in the name of the call.
7798 elsif Ekind (Hom) = E_Procedure
7799 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
7800 then
7801 goto Next_Hom;
7802 end if;
7804 Set_Etype (Call_Node, Any_Type);
7805 Set_Is_Overloaded (Call_Node, False);
7806 Success := False;
7808 if No (Matching_Op) then
7809 Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog));
7810 Set_Etype (Call_Node, Any_Type);
7811 Set_Parent (Call_Node, Parent (Node_To_Replace));
7813 Set_Name (Call_Node, Hom_Ref);
7815 Analyze_One_Call
7816 (N => Call_Node,
7817 Nam => Hom,
7818 Report => Report_Error,
7819 Success => Success,
7820 Skip_First => True);
7822 Matching_Op :=
7823 Valid_Candidate (Success, Call_Node, Hom);
7825 else
7826 Analyze_One_Call
7827 (N => Call_Node,
7828 Nam => Hom,
7829 Report => Report_Error,
7830 Success => Success,
7831 Skip_First => True);
7833 if Present (Valid_Candidate (Success, Call_Node, Hom))
7834 and then Nkind (Call_Node) /= N_Function_Call
7835 then
7836 Error_Msg_NE ("ambiguous call to&", N, Hom);
7837 Report_Ambiguity (Matching_Op);
7838 Report_Ambiguity (Hom);
7839 Error := True;
7840 return;
7841 end if;
7842 end if;
7843 end if;
7845 <<Next_Hom>>
7846 Hom := Homonym (Hom);
7847 end loop;
7848 end Traverse_Homonyms;
7850 -------------------------
7851 -- Traverse_Interfaces --
7852 -------------------------
7854 procedure Traverse_Interfaces
7855 (Anc_Type : Entity_Id;
7856 Error : out Boolean)
7858 Intface_List : constant List_Id :=
7859 Abstract_Interface_List (Anc_Type);
7860 Intface : Node_Id;
7862 begin
7863 Error := False;
7865 if Is_Non_Empty_List (Intface_List) then
7866 Intface := First (Intface_List);
7867 while Present (Intface) loop
7869 -- Look for acceptable class-wide homonyms associated with
7870 -- the interface.
7872 Traverse_Homonyms (Etype (Intface), Error);
7874 if Error then
7875 return;
7876 end if;
7878 -- Continue the search by looking at each of the interface's
7879 -- associated interface ancestors.
7881 Traverse_Interfaces (Etype (Intface), Error);
7883 if Error then
7884 return;
7885 end if;
7887 Next (Intface);
7888 end loop;
7889 end if;
7890 end Traverse_Interfaces;
7892 -- Start of processing for Try_Class_Wide_Operation
7894 begin
7895 -- If we are searching only for conflicting class-wide subprograms
7896 -- then initialize directly Matching_Op with the target entity.
7898 if CW_Test_Only then
7899 Matching_Op := Entity (Selector_Name (N));
7900 end if;
7902 -- Loop through ancestor types (including interfaces), traversing
7903 -- the homonym chain of the subprogram, trying out those homonyms
7904 -- whose first formal has the class-wide type of the ancestor, or
7905 -- an anonymous access type designating the class-wide type.
7907 Anc_Type := Obj_Type;
7908 loop
7909 -- Look for a match among homonyms associated with the ancestor
7911 Traverse_Homonyms (Anc_Type, Error);
7913 if Error then
7914 return True;
7915 end if;
7917 -- Continue the search for matches among homonyms associated with
7918 -- any interfaces implemented by the ancestor.
7920 Traverse_Interfaces (Anc_Type, Error);
7922 if Error then
7923 return True;
7924 end if;
7926 exit when Etype (Anc_Type) = Anc_Type;
7927 Anc_Type := Etype (Anc_Type);
7928 end loop;
7930 if Present (Matching_Op) then
7931 Set_Etype (Call_Node, Etype (Matching_Op));
7932 end if;
7934 return Present (Matching_Op);
7935 end Try_Class_Wide_Operation;
7937 -----------------------------------
7938 -- Try_One_Prefix_Interpretation --
7939 -----------------------------------
7941 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7942 begin
7943 Obj_Type := T;
7945 if Is_Access_Type (Obj_Type) then
7946 Obj_Type := Designated_Type (Obj_Type);
7947 end if;
7949 if Ekind (Obj_Type) = E_Private_Subtype then
7950 Obj_Type := Base_Type (Obj_Type);
7951 end if;
7953 if Is_Class_Wide_Type (Obj_Type) then
7954 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7955 end if;
7957 -- The type may have be obtained through a limited_with clause,
7958 -- in which case the primitive operations are available on its
7959 -- non-limited view. If still incomplete, retrieve full view.
7961 if Ekind (Obj_Type) = E_Incomplete_Type
7962 and then From_Limited_With (Obj_Type)
7963 then
7964 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7965 end if;
7967 -- If the object is not tagged, or the type is still an incomplete
7968 -- type, this is not a prefixed call.
7970 if not Is_Tagged_Type (Obj_Type)
7971 or else Is_Incomplete_Type (Obj_Type)
7972 then
7973 return;
7974 end if;
7976 declare
7977 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
7978 CW_Result : Boolean;
7979 Prim_Result : Boolean;
7980 pragma Unreferenced (CW_Result);
7982 begin
7983 if not CW_Test_Only then
7984 Prim_Result :=
7985 Try_Primitive_Operation
7986 (Call_Node => New_Call_Node,
7987 Node_To_Replace => Node_To_Replace);
7988 end if;
7990 -- Check if there is a class-wide subprogram covering the
7991 -- primitive. This check must be done even if a candidate
7992 -- was found in order to report ambiguous calls.
7994 if not (Prim_Result) then
7995 CW_Result :=
7996 Try_Class_Wide_Operation
7997 (Call_Node => New_Call_Node,
7998 Node_To_Replace => Node_To_Replace);
8000 -- If we found a primitive we search for class-wide subprograms
8001 -- using a duplicate of the call node (done to avoid missing its
8002 -- decoration if there is no ambiguity).
8004 else
8005 CW_Result :=
8006 Try_Class_Wide_Operation
8007 (Call_Node => Dup_Call_Node,
8008 Node_To_Replace => Node_To_Replace);
8009 end if;
8010 end;
8011 end Try_One_Prefix_Interpretation;
8013 -----------------------------
8014 -- Try_Primitive_Operation --
8015 -----------------------------
8017 function Try_Primitive_Operation
8018 (Call_Node : Node_Id;
8019 Node_To_Replace : Node_Id) return Boolean
8021 Elmt : Elmt_Id;
8022 Prim_Op : Entity_Id;
8023 Matching_Op : Entity_Id := Empty;
8024 Prim_Op_Ref : Node_Id := Empty;
8026 Corr_Type : Entity_Id := Empty;
8027 -- If the prefix is a synchronized type, the controlling type of
8028 -- the primitive operation is the corresponding record type, else
8029 -- this is the object type itself.
8031 Success : Boolean := False;
8033 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
8034 -- For tagged types the candidate interpretations are found in
8035 -- the list of primitive operations of the type and its ancestors.
8036 -- For formal tagged types we have to find the operations declared
8037 -- in the same scope as the type (including in the generic formal
8038 -- part) because the type itself carries no primitive operations,
8039 -- except for formal derived types that inherit the operations of
8040 -- the parent and progenitors.
8042 -- If the context is a generic subprogram body, the generic formals
8043 -- are visible by name, but are not in the entity list of the
8044 -- subprogram because that list starts with the subprogram formals.
8045 -- We retrieve the candidate operations from the generic declaration.
8047 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
8048 -- An operation that overrides an inherited operation in the private
8049 -- part of its package may be hidden, but if the inherited operation
8050 -- is visible a direct call to it will dispatch to the private one,
8051 -- which is therefore a valid candidate.
8053 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
8054 -- Verify that the prefix, dereferenced if need be, is a valid
8055 -- controlling argument in a call to Op. The remaining actuals
8056 -- are checked in the subsequent call to Analyze_One_Call.
8058 ------------------------------
8059 -- Collect_Generic_Type_Ops --
8060 ------------------------------
8062 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
8063 Bas : constant Entity_Id := Base_Type (T);
8064 Candidates : constant Elist_Id := New_Elmt_List;
8065 Subp : Entity_Id;
8066 Formal : Entity_Id;
8068 procedure Check_Candidate;
8069 -- The operation is a candidate if its first parameter is a
8070 -- controlling operand of the desired type.
8072 -----------------------
8073 -- Check_Candidate; --
8074 -----------------------
8076 procedure Check_Candidate is
8077 begin
8078 Formal := First_Formal (Subp);
8080 if Present (Formal)
8081 and then Is_Controlling_Formal (Formal)
8082 and then
8083 (Base_Type (Etype (Formal)) = Bas
8084 or else
8085 (Is_Access_Type (Etype (Formal))
8086 and then Designated_Type (Etype (Formal)) = Bas))
8087 then
8088 Append_Elmt (Subp, Candidates);
8089 end if;
8090 end Check_Candidate;
8092 -- Start of processing for Collect_Generic_Type_Ops
8094 begin
8095 if Is_Derived_Type (T) then
8096 return Primitive_Operations (T);
8098 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
8100 -- Scan the list of generic formals to find subprograms
8101 -- that may have a first controlling formal of the type.
8103 if Nkind (Unit_Declaration_Node (Scope (T))) =
8104 N_Generic_Subprogram_Declaration
8105 then
8106 declare
8107 Decl : Node_Id;
8109 begin
8110 Decl :=
8111 First (Generic_Formal_Declarations
8112 (Unit_Declaration_Node (Scope (T))));
8113 while Present (Decl) loop
8114 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
8115 Subp := Defining_Entity (Decl);
8116 Check_Candidate;
8117 end if;
8119 Next (Decl);
8120 end loop;
8121 end;
8122 end if;
8123 return Candidates;
8125 else
8126 -- Scan the list of entities declared in the same scope as
8127 -- the type. In general this will be an open scope, given that
8128 -- the call we are analyzing can only appear within a generic
8129 -- declaration or body (either the one that declares T, or a
8130 -- child unit).
8132 -- For a subtype representing a generic actual type, go to the
8133 -- base type.
8135 if Is_Generic_Actual_Type (T) then
8136 Subp := First_Entity (Scope (Base_Type (T)));
8137 else
8138 Subp := First_Entity (Scope (T));
8139 end if;
8141 while Present (Subp) loop
8142 if Is_Overloadable (Subp) then
8143 Check_Candidate;
8144 end if;
8146 Next_Entity (Subp);
8147 end loop;
8149 return Candidates;
8150 end if;
8151 end Collect_Generic_Type_Ops;
8153 ---------------------------
8154 -- Is_Private_Overriding --
8155 ---------------------------
8157 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
8158 Visible_Op : constant Entity_Id := Homonym (Op);
8160 begin
8161 return Present (Visible_Op)
8162 and then Scope (Op) = Scope (Visible_Op)
8163 and then not Comes_From_Source (Visible_Op)
8164 and then Alias (Visible_Op) = Op
8165 and then not Is_Hidden (Visible_Op);
8166 end Is_Private_Overriding;
8168 -----------------------------
8169 -- Valid_First_Argument_Of --
8170 -----------------------------
8172 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
8173 Typ : Entity_Id := Etype (First_Formal (Op));
8175 begin
8176 if Is_Concurrent_Type (Typ)
8177 and then Present (Corresponding_Record_Type (Typ))
8178 then
8179 Typ := Corresponding_Record_Type (Typ);
8180 end if;
8182 -- Simple case. Object may be a subtype of the tagged type or
8183 -- may be the corresponding record of a synchronized type.
8185 return Obj_Type = Typ
8186 or else Base_Type (Obj_Type) = Typ
8187 or else Corr_Type = Typ
8189 -- Prefix can be dereferenced
8191 or else
8192 (Is_Access_Type (Corr_Type)
8193 and then Designated_Type (Corr_Type) = Typ)
8195 -- Formal is an access parameter, for which the object
8196 -- can provide an access.
8198 or else
8199 (Ekind (Typ) = E_Anonymous_Access_Type
8200 and then
8201 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
8202 end Valid_First_Argument_Of;
8204 -- Start of processing for Try_Primitive_Operation
8206 begin
8207 -- Look for subprograms in the list of primitive operations. The name
8208 -- must be identical, and the kind of call indicates the expected
8209 -- kind of operation (function or procedure). If the type is a
8210 -- (tagged) synchronized type, the primitive ops are attached to the
8211 -- corresponding record (base) type.
8213 if Is_Concurrent_Type (Obj_Type) then
8214 if Present (Corresponding_Record_Type (Obj_Type)) then
8215 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
8216 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
8217 else
8218 Corr_Type := Obj_Type;
8219 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
8220 end if;
8222 elsif not Is_Generic_Type (Obj_Type) then
8223 Corr_Type := Obj_Type;
8224 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
8226 else
8227 Corr_Type := Obj_Type;
8228 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
8229 end if;
8231 while Present (Elmt) loop
8232 Prim_Op := Node (Elmt);
8234 if Chars (Prim_Op) = Chars (Subprog)
8235 and then Present (First_Formal (Prim_Op))
8236 and then Valid_First_Argument_Of (Prim_Op)
8237 and then
8238 (Nkind (Call_Node) = N_Function_Call)
8240 (Ekind (Prim_Op) = E_Function)
8241 then
8242 -- Ada 2005 (AI-251): If this primitive operation corresponds
8243 -- to an immediate ancestor interface there is no need to add
8244 -- it to the list of interpretations; the corresponding aliased
8245 -- primitive is also in this list of primitive operations and
8246 -- will be used instead.
8248 if (Present (Interface_Alias (Prim_Op))
8249 and then Is_Ancestor (Find_Dispatching_Type
8250 (Alias (Prim_Op)), Corr_Type))
8252 -- Do not consider hidden primitives unless the type is in an
8253 -- open scope or we are within an instance, where visibility
8254 -- is known to be correct, or else if this is an overriding
8255 -- operation in the private part for an inherited operation.
8257 or else (Is_Hidden (Prim_Op)
8258 and then not Is_Immediately_Visible (Obj_Type)
8259 and then not In_Instance
8260 and then not Is_Private_Overriding (Prim_Op))
8261 then
8262 goto Continue;
8263 end if;
8265 Set_Etype (Call_Node, Any_Type);
8266 Set_Is_Overloaded (Call_Node, False);
8268 if No (Matching_Op) then
8269 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
8270 Candidate := Prim_Op;
8272 Set_Parent (Call_Node, Parent (Node_To_Replace));
8274 Set_Name (Call_Node, Prim_Op_Ref);
8275 Success := False;
8277 Analyze_One_Call
8278 (N => Call_Node,
8279 Nam => Prim_Op,
8280 Report => Report_Error,
8281 Success => Success,
8282 Skip_First => True);
8284 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
8286 -- More than one interpretation, collect for subsequent
8287 -- disambiguation. If this is a procedure call and there
8288 -- is another match, report ambiguity now.
8290 else
8291 Analyze_One_Call
8292 (N => Call_Node,
8293 Nam => Prim_Op,
8294 Report => Report_Error,
8295 Success => Success,
8296 Skip_First => True);
8298 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
8299 and then Nkind (Call_Node) /= N_Function_Call
8300 then
8301 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
8302 Report_Ambiguity (Matching_Op);
8303 Report_Ambiguity (Prim_Op);
8304 return True;
8305 end if;
8306 end if;
8307 end if;
8309 <<Continue>>
8310 Next_Elmt (Elmt);
8311 end loop;
8313 if Present (Matching_Op) then
8314 Set_Etype (Call_Node, Etype (Matching_Op));
8315 end if;
8317 return Present (Matching_Op);
8318 end Try_Primitive_Operation;
8320 -- Start of processing for Try_Object_Operation
8322 begin
8323 Analyze_Expression (Obj);
8325 -- Analyze the actuals if node is known to be a subprogram call
8327 if Is_Subprg_Call and then N = Name (Parent (N)) then
8328 Actual := First (Parameter_Associations (Parent (N)));
8329 while Present (Actual) loop
8330 Analyze_Expression (Actual);
8331 Next (Actual);
8332 end loop;
8333 end if;
8335 -- Build a subprogram call node, using a copy of Obj as its first
8336 -- actual. This is a placeholder, to be replaced by an explicit
8337 -- dereference when needed.
8339 Transform_Object_Operation
8340 (Call_Node => New_Call_Node,
8341 Node_To_Replace => Node_To_Replace);
8343 Set_Etype (New_Call_Node, Any_Type);
8344 Set_Etype (Subprog, Any_Type);
8345 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
8347 if not Is_Overloaded (Obj) then
8348 Try_One_Prefix_Interpretation (Obj_Type);
8350 else
8351 declare
8352 I : Interp_Index;
8353 It : Interp;
8354 begin
8355 Get_First_Interp (Obj, I, It);
8356 while Present (It.Nam) loop
8357 Try_One_Prefix_Interpretation (It.Typ);
8358 Get_Next_Interp (I, It);
8359 end loop;
8360 end;
8361 end if;
8363 if Etype (New_Call_Node) /= Any_Type then
8365 -- No need to complete the tree transformations if we are only
8366 -- searching for conflicting class-wide subprograms
8368 if CW_Test_Only then
8369 return False;
8370 else
8371 Complete_Object_Operation
8372 (Call_Node => New_Call_Node,
8373 Node_To_Replace => Node_To_Replace);
8374 return True;
8375 end if;
8377 elsif Present (Candidate) then
8379 -- The argument list is not type correct. Re-analyze with error
8380 -- reporting enabled, and use one of the possible candidates.
8381 -- In All_Errors_Mode, re-analyze all failed interpretations.
8383 if All_Errors_Mode then
8384 Report_Error := True;
8385 if Try_Primitive_Operation
8386 (Call_Node => New_Call_Node,
8387 Node_To_Replace => Node_To_Replace)
8389 or else
8390 Try_Class_Wide_Operation
8391 (Call_Node => New_Call_Node,
8392 Node_To_Replace => Node_To_Replace)
8393 then
8394 null;
8395 end if;
8397 else
8398 Analyze_One_Call
8399 (N => New_Call_Node,
8400 Nam => Candidate,
8401 Report => True,
8402 Success => Success,
8403 Skip_First => True);
8404 end if;
8406 -- No need for further errors
8408 return True;
8410 else
8411 -- There was no candidate operation, so report it as an error
8412 -- in the caller: Analyze_Selected_Component.
8414 return False;
8415 end if;
8416 end Try_Object_Operation;
8418 ---------
8419 -- wpo --
8420 ---------
8422 procedure wpo (T : Entity_Id) is
8423 Op : Entity_Id;
8424 E : Elmt_Id;
8426 begin
8427 if not Is_Tagged_Type (T) then
8428 return;
8429 end if;
8431 E := First_Elmt (Primitive_Operations (Base_Type (T)));
8432 while Present (E) loop
8433 Op := Node (E);
8434 Write_Int (Int (Op));
8435 Write_Str (" === ");
8436 Write_Name (Chars (Op));
8437 Write_Str (" in ");
8438 Write_Name (Chars (Scope (Op)));
8439 Next_Elmt (E);
8440 Write_Eol;
8441 end loop;
8442 end wpo;
8444 end Sem_Ch4;