* contrib-list.mk (LIST): Remove arm-freebsd6, arm-linux,
[official-gcc.git] / gcc / ada / sem_ch4.adb
blobd1cdeeabf5ffdda4daac53b73b8f4071f3bbb489
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-2012, 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.
197 function Find_Primitive_Operation (N : Node_Id) return Boolean;
198 -- Find candidate interpretations for the name Obj.Proc when it appears
199 -- in a subprogram renaming declaration.
201 procedure Find_Unary_Types
202 (R : Node_Id;
203 Op_Id : Entity_Id;
204 N : Node_Id);
205 -- Unary arithmetic types: plus, minus, abs
207 procedure Check_Arithmetic_Pair
208 (T1, T2 : Entity_Id;
209 Op_Id : Entity_Id;
210 N : Node_Id);
211 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
212 -- types for left and right operand. Determine whether they constitute
213 -- a valid pair for the given operator, and record the corresponding
214 -- interpretation of the operator node. The node N may be an operator
215 -- node (the usual case) or a function call whose prefix is an operator
216 -- designator. In both cases Op_Id is the operator name itself.
218 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
219 -- Give detailed information on overloaded call where none of the
220 -- interpretations match. N is the call node, Nam the designator for
221 -- the overloaded entity being called.
223 function Junk_Operand (N : Node_Id) return Boolean;
224 -- Test for an operand that is an inappropriate entity (e.g. a package
225 -- name or a label). If so, issue an error message and return True. If
226 -- the operand is not an inappropriate entity kind, return False.
228 procedure Operator_Check (N : Node_Id);
229 -- Verify that an operator has received some valid interpretation. If none
230 -- was found, determine whether a use clause would make the operation
231 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
232 -- every type compatible with the operator, even if the operator for the
233 -- type is not directly visible. The routine uses this type to emit a more
234 -- informative message.
236 function Process_Implicit_Dereference_Prefix
237 (E : Entity_Id;
238 P : Node_Id) return Entity_Id;
239 -- Called when P is the prefix of an implicit dereference, denoting an
240 -- object E. The function returns the designated type of the prefix, taking
241 -- into account that the designated type of an anonymous access type may be
242 -- a limited view, when the non-limited view is visible.
243 -- If in semantics only mode (-gnatc or generic), the function also records
244 -- that the prefix is a reference to E, if any. Normally, such a reference
245 -- is generated only when the implicit dereference is expanded into an
246 -- explicit one, but for consistency we must generate the reference when
247 -- expansion is disabled as well.
249 procedure Remove_Abstract_Operations (N : Node_Id);
250 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
251 -- operation is not a candidate interpretation.
253 function Try_Container_Indexing
254 (N : Node_Id;
255 Prefix : Node_Id;
256 Exprs : List_Id) return Boolean;
257 -- AI05-0139: Generalized indexing to support iterators over containers
259 function Try_Indexed_Call
260 (N : Node_Id;
261 Nam : Entity_Id;
262 Typ : Entity_Id;
263 Skip_First : Boolean) return Boolean;
264 -- If a function has defaults for all its actuals, a call to it may in fact
265 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
266 -- interpretation as an indexing, prior to analysis as a call. If both are
267 -- possible, the node is overloaded with both interpretations (same symbol
268 -- but two different types). If the call is written in prefix form, the
269 -- prefix becomes the first parameter in the call, and only the remaining
270 -- actuals must be checked for the presence of defaults.
272 function Try_Indirect_Call
273 (N : Node_Id;
274 Nam : Entity_Id;
275 Typ : Entity_Id) return Boolean;
276 -- Similarly, a function F that needs no actuals can return an access to a
277 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
278 -- the call may be overloaded with both interpretations.
280 function Try_Object_Operation
281 (N : Node_Id;
282 CW_Test_Only : Boolean := False) return Boolean;
283 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
284 -- is a call in this notation, it is transformed into a normal subprogram
285 -- call where the prefix is a parameter, and True is returned. If node
286 -- N is not of this form, it is unchanged, and False is returned. if
287 -- CW_Test_Only is true then N is an N_Selected_Component node which
288 -- is part of a call to an entry or procedure of a tagged concurrent
289 -- type and this routine is invoked to search for class-wide subprograms
290 -- conflicting with the target entity.
292 procedure wpo (T : Entity_Id);
293 pragma Warnings (Off, wpo);
294 -- Used for debugging: obtain list of primitive operations even if
295 -- type is not frozen and dispatch table is not built yet.
297 ------------------------
298 -- Ambiguous_Operands --
299 ------------------------
301 procedure Ambiguous_Operands (N : Node_Id) is
302 procedure List_Operand_Interps (Opnd : Node_Id);
304 --------------------------
305 -- List_Operand_Interps --
306 --------------------------
308 procedure List_Operand_Interps (Opnd : Node_Id) is
309 Nam : Node_Id;
310 Err : Node_Id := N;
312 begin
313 if Is_Overloaded (Opnd) then
314 if Nkind (Opnd) in N_Op then
315 Nam := Opnd;
316 elsif Nkind (Opnd) = N_Function_Call then
317 Nam := Name (Opnd);
318 elsif Ada_Version >= Ada_2012 then
319 declare
320 It : Interp;
321 I : Interp_Index;
323 begin
324 Get_First_Interp (Opnd, I, It);
325 while Present (It.Nam) loop
326 if Has_Implicit_Dereference (It.Typ) then
327 Error_Msg_N
328 ("can be interpreted as implicit dereference", Opnd);
329 return;
330 end if;
332 Get_Next_Interp (I, It);
333 end loop;
334 end;
336 return;
337 end if;
339 else
340 return;
341 end if;
343 if Opnd = Left_Opnd (N) then
344 Error_Msg_N ("\left operand has the following interpretations", N);
345 else
346 Error_Msg_N
347 ("\right operand has the following interpretations", N);
348 Err := Opnd;
349 end if;
351 List_Interps (Nam, Err);
352 end List_Operand_Interps;
354 -- Start of processing for Ambiguous_Operands
356 begin
357 if Nkind (N) in N_Membership_Test then
358 Error_Msg_N ("ambiguous operands for membership", N);
360 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
361 Error_Msg_N ("ambiguous operands for equality", N);
363 else
364 Error_Msg_N ("ambiguous operands for comparison", N);
365 end if;
367 if All_Errors_Mode then
368 List_Operand_Interps (Left_Opnd (N));
369 List_Operand_Interps (Right_Opnd (N));
370 else
371 Error_Msg_N ("\use -gnatf switch for details", N);
372 end if;
373 end Ambiguous_Operands;
375 -----------------------
376 -- Analyze_Aggregate --
377 -----------------------
379 -- Most of the analysis of Aggregates requires that the type be known,
380 -- and is therefore put off until resolution.
382 procedure Analyze_Aggregate (N : Node_Id) is
383 begin
384 if No (Etype (N)) then
385 Set_Etype (N, Any_Composite);
386 end if;
387 end Analyze_Aggregate;
389 -----------------------
390 -- Analyze_Allocator --
391 -----------------------
393 procedure Analyze_Allocator (N : Node_Id) is
394 Loc : constant Source_Ptr := Sloc (N);
395 Sav_Errs : constant Nat := Serious_Errors_Detected;
396 E : Node_Id := Expression (N);
397 Acc_Type : Entity_Id;
398 Type_Id : Entity_Id;
399 P : Node_Id;
400 C : Node_Id;
402 begin
403 Check_SPARK_Restriction ("allocator is not allowed", N);
405 -- Deal with allocator restrictions
407 -- In accordance with H.4(7), the No_Allocators restriction only applies
408 -- to user-written allocators. The same consideration applies to the
409 -- No_Allocators_Before_Elaboration restriction.
411 if Comes_From_Source (N) then
412 Check_Restriction (No_Allocators, N);
414 -- Processing for No_Allocators_After_Elaboration, loop to look at
415 -- enclosing context, checking task case and main subprogram case.
417 C := N;
418 P := Parent (C);
419 while Present (P) loop
421 -- In both cases we need a handled sequence of statements, where
422 -- the occurrence of the allocator is within the statements.
424 if Nkind (P) = N_Handled_Sequence_Of_Statements
425 and then Is_List_Member (C)
426 and then List_Containing (C) = Statements (P)
427 then
428 -- Check for allocator within task body, this is a definite
429 -- violation of No_Allocators_After_Elaboration we can detect.
431 if Nkind (Original_Node (Parent (P))) = N_Task_Body then
432 Check_Restriction (No_Allocators_After_Elaboration, N);
433 exit;
434 end if;
436 -- The other case is appearance in a subprogram body. This may
437 -- be a violation if this is a library level subprogram, and it
438 -- turns out to be used as the main program, but only the
439 -- binder knows that, so just record the occurrence.
441 if Nkind (Original_Node (Parent (P))) = N_Subprogram_Body
442 and then Nkind (Parent (Parent (P))) = N_Compilation_Unit
443 then
444 Set_Has_Allocator (Current_Sem_Unit);
445 end if;
446 end if;
448 C := P;
449 P := Parent (C);
450 end loop;
451 end if;
453 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
454 -- any. The expected type for the name is any type. A non-overloading
455 -- rule then requires it to be of a type descended from
456 -- System.Storage_Pools.Subpools.Subpool_Handle.
458 -- This isn't exactly what the AI says, but it seems to be the right
459 -- rule. The AI should be fixed.???
461 declare
462 Subpool : constant Node_Id := Subpool_Handle_Name (N);
464 begin
465 if Present (Subpool) then
466 Analyze (Subpool);
468 if Is_Overloaded (Subpool) then
469 Error_Msg_N ("ambiguous subpool handle", Subpool);
470 end if;
472 -- Check that Etype (Subpool) is descended from Subpool_Handle
474 Resolve (Subpool);
475 end if;
476 end;
478 -- Analyze the qualified expression or subtype indication
480 if Nkind (E) = N_Qualified_Expression then
481 Acc_Type := Create_Itype (E_Allocator_Type, N);
482 Set_Etype (Acc_Type, Acc_Type);
483 Find_Type (Subtype_Mark (E));
485 -- Analyze the qualified expression, and apply the name resolution
486 -- rule given in 4.7(3).
488 Analyze (E);
489 Type_Id := Etype (E);
490 Set_Directly_Designated_Type (Acc_Type, Type_Id);
492 Resolve (Expression (E), Type_Id);
494 -- Allocators generated by the build-in-place expansion mechanism
495 -- are explicitly marked as coming from source but do not need to be
496 -- checked for limited initialization. To exclude this case, ensure
497 -- that the parent of the allocator is a source node.
499 if Is_Limited_Type (Type_Id)
500 and then Comes_From_Source (N)
501 and then Comes_From_Source (Parent (N))
502 and then not In_Instance_Body
503 then
504 if not OK_For_Limited_Init (Type_Id, Expression (E)) then
505 Error_Msg_N ("initialization not allowed for limited types", N);
506 Explain_Limited_Type (Type_Id, N);
507 end if;
508 end if;
510 -- A qualified expression requires an exact match of the type,
511 -- class-wide matching is not allowed.
513 -- if Is_Class_Wide_Type (Type_Id)
514 -- and then Base_Type
515 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
516 -- then
517 -- Wrong_Type (Expression (E), Type_Id);
518 -- end if;
520 Check_Non_Static_Context (Expression (E));
522 -- We don't analyze the qualified expression itself because it's
523 -- part of the allocator
525 Set_Etype (E, Type_Id);
527 -- Case where allocator has a subtype indication
529 else
530 declare
531 Def_Id : Entity_Id;
532 Base_Typ : Entity_Id;
534 begin
535 -- If the allocator includes a N_Subtype_Indication then a
536 -- constraint is present, otherwise the node is a subtype mark.
537 -- Introduce an explicit subtype declaration into the tree
538 -- defining some anonymous subtype and rewrite the allocator to
539 -- use this subtype rather than the subtype indication.
541 -- It is important to introduce the explicit subtype declaration
542 -- so that the bounds of the subtype indication are attached to
543 -- the tree in case the allocator is inside a generic unit.
545 if Nkind (E) = N_Subtype_Indication then
547 -- A constraint is only allowed for a composite type in Ada
548 -- 95. In Ada 83, a constraint is also allowed for an
549 -- access-to-composite type, but the constraint is ignored.
551 Find_Type (Subtype_Mark (E));
552 Base_Typ := Entity (Subtype_Mark (E));
554 if Is_Elementary_Type (Base_Typ) then
555 if not (Ada_Version = Ada_83
556 and then Is_Access_Type (Base_Typ))
557 then
558 Error_Msg_N ("constraint not allowed here", E);
560 if Nkind (Constraint (E)) =
561 N_Index_Or_Discriminant_Constraint
562 then
563 Error_Msg_N -- CODEFIX
564 ("\if qualified expression was meant, " &
565 "use apostrophe", Constraint (E));
566 end if;
567 end if;
569 -- Get rid of the bogus constraint:
571 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
572 Analyze_Allocator (N);
573 return;
575 -- Ada 2005, AI-363: if the designated type has a constrained
576 -- partial view, it cannot receive a discriminant constraint,
577 -- and the allocated object is unconstrained.
579 elsif Ada_Version >= Ada_2005
580 and then Effectively_Has_Constrained_Partial_View
581 (Typ => Base_Typ,
582 Scop => Current_Scope)
583 then
584 Error_Msg_N
585 ("constraint not allowed when type " &
586 "has a constrained partial view", Constraint (E));
587 end if;
589 if Expander_Active then
590 Def_Id := Make_Temporary (Loc, 'S');
592 Insert_Action (E,
593 Make_Subtype_Declaration (Loc,
594 Defining_Identifier => Def_Id,
595 Subtype_Indication => Relocate_Node (E)));
597 if Sav_Errs /= Serious_Errors_Detected
598 and then Nkind (Constraint (E)) =
599 N_Index_Or_Discriminant_Constraint
600 then
601 Error_Msg_N -- CODEFIX
602 ("if qualified expression was meant, " &
603 "use apostrophe!", Constraint (E));
604 end if;
606 E := New_Occurrence_Of (Def_Id, Loc);
607 Rewrite (Expression (N), E);
608 end if;
609 end if;
611 Type_Id := Process_Subtype (E, N);
612 Acc_Type := Create_Itype (E_Allocator_Type, N);
613 Set_Etype (Acc_Type, Acc_Type);
614 Set_Directly_Designated_Type (Acc_Type, Type_Id);
615 Check_Fully_Declared (Type_Id, N);
617 -- Ada 2005 (AI-231): If the designated type is itself an access
618 -- type that excludes null, its default initialization will
619 -- be a null object, and we can insert an unconditional raise
620 -- before the allocator.
622 -- Ada 2012 (AI-104): A not null indication here is altogether
623 -- illegal.
625 if Can_Never_Be_Null (Type_Id) then
626 declare
627 Not_Null_Check : constant Node_Id :=
628 Make_Raise_Constraint_Error (Sloc (E),
629 Reason => CE_Null_Not_Allowed);
631 begin
632 if Ada_Version >= Ada_2012 then
633 Error_Msg_N
634 ("an uninitialized allocator cannot have"
635 & " a null exclusion", N);
637 elsif Expander_Active then
638 Insert_Action (N, Not_Null_Check);
639 Analyze (Not_Null_Check);
641 else
642 Error_Msg_N ("null value not allowed here?", E);
643 end if;
644 end;
645 end if;
647 -- Check restriction against dynamically allocated protected
648 -- objects. Note that when limited aggregates are supported,
649 -- a similar test should be applied to an allocator with a
650 -- qualified expression ???
652 if Is_Protected_Type (Type_Id) then
653 Check_Restriction (No_Protected_Type_Allocators, N);
654 end if;
656 -- Check for missing initialization. Skip this check if we already
657 -- had errors on analyzing the allocator, since in that case these
658 -- are probably cascaded errors.
660 if Is_Indefinite_Subtype (Type_Id)
661 and then Serious_Errors_Detected = Sav_Errs
662 then
663 -- The build-in-place machinery may produce an allocator when
664 -- the designated type is indefinite but the underlying type is
665 -- not. In this case the unknown discriminants are meaningless
666 -- and should not trigger error messages. Check the parent node
667 -- because the allocator is marked as coming from source.
669 if Present (Underlying_Type (Type_Id))
670 and then not Is_Indefinite_Subtype (Underlying_Type (Type_Id))
671 and then not Comes_From_Source (Parent (N))
672 then
673 null;
675 elsif Is_Class_Wide_Type (Type_Id) then
676 Error_Msg_N
677 ("initialization required in class-wide allocation", N);
679 else
680 if Ada_Version < Ada_2005
681 and then Is_Limited_Type (Type_Id)
682 then
683 Error_Msg_N ("unconstrained allocation not allowed", N);
685 if Is_Array_Type (Type_Id) then
686 Error_Msg_N
687 ("\constraint with array bounds required", N);
689 elsif Has_Unknown_Discriminants (Type_Id) then
690 null;
692 else pragma Assert (Has_Discriminants (Type_Id));
693 Error_Msg_N
694 ("\constraint with discriminant values required", N);
695 end if;
697 -- Limited Ada 2005 and general non-limited case
699 else
700 Error_Msg_N
701 ("uninitialized unconstrained allocation not allowed",
704 if Is_Array_Type (Type_Id) then
705 Error_Msg_N
706 ("\qualified expression or constraint with " &
707 "array bounds required", N);
709 elsif Has_Unknown_Discriminants (Type_Id) then
710 Error_Msg_N ("\qualified expression required", N);
712 else pragma Assert (Has_Discriminants (Type_Id));
713 Error_Msg_N
714 ("\qualified expression or constraint with " &
715 "discriminant values required", N);
716 end if;
717 end if;
718 end if;
719 end if;
720 end;
721 end if;
723 if Is_Abstract_Type (Type_Id) then
724 Error_Msg_N ("cannot allocate abstract object", E);
725 end if;
727 if Has_Task (Designated_Type (Acc_Type)) then
728 Check_Restriction (No_Tasking, N);
729 Check_Restriction (Max_Tasks, N);
730 Check_Restriction (No_Task_Allocators, N);
731 end if;
733 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
734 -- type is nested, and the designated type needs finalization. The rule
735 -- is conservative in that class-wide types need finalization.
737 if Needs_Finalization (Designated_Type (Acc_Type))
738 and then not Is_Library_Level_Entity (Acc_Type)
739 then
740 Check_Restriction (No_Nested_Finalization, N);
741 end if;
743 -- Check that an allocator of a nested access type doesn't create a
744 -- protected object when restriction No_Local_Protected_Objects applies.
745 -- We don't have an equivalent to Has_Task for protected types, so only
746 -- cases where the designated type itself is a protected type are
747 -- currently checked. ???
749 if Is_Protected_Type (Designated_Type (Acc_Type))
750 and then not Is_Library_Level_Entity (Acc_Type)
751 then
752 Check_Restriction (No_Local_Protected_Objects, N);
753 end if;
755 -- If the No_Streams restriction is set, check that the type of the
756 -- object is not, and does not contain, any subtype derived from
757 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
758 -- Has_Stream just for efficiency reasons. There is no point in
759 -- spending time on a Has_Stream check if the restriction is not set.
761 if Restriction_Check_Required (No_Streams) then
762 if Has_Stream (Designated_Type (Acc_Type)) then
763 Check_Restriction (No_Streams, N);
764 end if;
765 end if;
767 Set_Etype (N, Acc_Type);
769 if not Is_Library_Level_Entity (Acc_Type) then
770 Check_Restriction (No_Local_Allocators, N);
771 end if;
773 if Serious_Errors_Detected > Sav_Errs then
774 Set_Error_Posted (N);
775 Set_Etype (N, Any_Type);
776 end if;
777 end Analyze_Allocator;
779 ---------------------------
780 -- Analyze_Arithmetic_Op --
781 ---------------------------
783 procedure Analyze_Arithmetic_Op (N : Node_Id) is
784 L : constant Node_Id := Left_Opnd (N);
785 R : constant Node_Id := Right_Opnd (N);
786 Op_Id : Entity_Id;
788 begin
789 Candidate_Type := Empty;
790 Analyze_Expression (L);
791 Analyze_Expression (R);
793 -- If the entity is already set, the node is the instantiation of a
794 -- generic node with a non-local reference, or was manufactured by a
795 -- call to Make_Op_xxx. In either case the entity is known to be valid,
796 -- and we do not need to collect interpretations, instead we just get
797 -- the single possible interpretation.
799 Op_Id := Entity (N);
801 if Present (Op_Id) then
802 if Ekind (Op_Id) = E_Operator then
804 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
805 and then Treat_Fixed_As_Integer (N)
806 then
807 null;
808 else
809 Set_Etype (N, Any_Type);
810 Find_Arithmetic_Types (L, R, Op_Id, N);
811 end if;
813 else
814 Set_Etype (N, Any_Type);
815 Add_One_Interp (N, Op_Id, Etype (Op_Id));
816 end if;
818 -- Entity is not already set, so we do need to collect interpretations
820 else
821 Op_Id := Get_Name_Entity_Id (Chars (N));
822 Set_Etype (N, Any_Type);
824 while Present (Op_Id) loop
825 if Ekind (Op_Id) = E_Operator
826 and then Present (Next_Entity (First_Entity (Op_Id)))
827 then
828 Find_Arithmetic_Types (L, R, Op_Id, N);
830 -- The following may seem superfluous, because an operator cannot
831 -- be generic, but this ignores the cleverness of the author of
832 -- ACVC bc1013a.
834 elsif Is_Overloadable (Op_Id) then
835 Analyze_User_Defined_Binary_Op (N, Op_Id);
836 end if;
838 Op_Id := Homonym (Op_Id);
839 end loop;
840 end if;
842 Operator_Check (N);
843 end Analyze_Arithmetic_Op;
845 ------------------
846 -- Analyze_Call --
847 ------------------
849 -- Function, procedure, and entry calls are checked here. The Name in
850 -- the call may be overloaded. The actuals have been analyzed and may
851 -- themselves be overloaded. On exit from this procedure, the node N
852 -- may have zero, one or more interpretations. In the first case an
853 -- error message is produced. In the last case, the node is flagged
854 -- as overloaded and the interpretations are collected in All_Interp.
856 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
857 -- the type-checking is similar to that of other calls.
859 procedure Analyze_Call (N : Node_Id) is
860 Actuals : constant List_Id := Parameter_Associations (N);
861 Nam : Node_Id;
862 X : Interp_Index;
863 It : Interp;
864 Nam_Ent : Entity_Id;
865 Success : Boolean := False;
867 Deref : Boolean := False;
868 -- Flag indicates whether an interpretation of the prefix is a
869 -- parameterless call that returns an access_to_subprogram.
871 procedure Check_Mixed_Parameter_And_Named_Associations;
872 -- Check that parameter and named associations are not mixed. This is
873 -- a restriction in SPARK mode.
875 function Name_Denotes_Function return Boolean;
876 -- If the type of the name is an access to subprogram, this may be the
877 -- type of a name, or the return type of the function being called. If
878 -- the name is not an entity then it can denote a protected function.
879 -- Until we distinguish Etype from Return_Type, we must use this routine
880 -- to resolve the meaning of the name in the call.
882 procedure No_Interpretation;
883 -- Output error message when no valid interpretation exists
885 --------------------------------------------------
886 -- Check_Mixed_Parameter_And_Named_Associations --
887 --------------------------------------------------
889 procedure Check_Mixed_Parameter_And_Named_Associations is
890 Actual : Node_Id;
891 Named_Seen : Boolean;
893 begin
894 Named_Seen := False;
896 Actual := First (Actuals);
897 while Present (Actual) loop
898 case Nkind (Actual) is
899 when N_Parameter_Association =>
900 if Named_Seen then
901 Check_SPARK_Restriction
902 ("named association cannot follow positional one",
903 Actual);
904 exit;
905 end if;
906 when others =>
907 Named_Seen := True;
908 end case;
910 Next (Actual);
911 end loop;
912 end Check_Mixed_Parameter_And_Named_Associations;
914 ---------------------------
915 -- Name_Denotes_Function --
916 ---------------------------
918 function Name_Denotes_Function return Boolean is
919 begin
920 if Is_Entity_Name (Nam) then
921 return Ekind (Entity (Nam)) = E_Function;
923 elsif Nkind (Nam) = N_Selected_Component then
924 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
926 else
927 return False;
928 end if;
929 end Name_Denotes_Function;
931 -----------------------
932 -- No_Interpretation --
933 -----------------------
935 procedure No_Interpretation is
936 L : constant Boolean := Is_List_Member (N);
937 K : constant Node_Kind := Nkind (Parent (N));
939 begin
940 -- If the node is in a list whose parent is not an expression then it
941 -- must be an attempted procedure call.
943 if L and then K not in N_Subexpr then
944 if Ekind (Entity (Nam)) = E_Generic_Procedure then
945 Error_Msg_NE
946 ("must instantiate generic procedure& before call",
947 Nam, Entity (Nam));
948 else
949 Error_Msg_N
950 ("procedure or entry name expected", Nam);
951 end if;
953 -- Check for tasking cases where only an entry call will do
955 elsif not L
956 and then Nkind_In (K, N_Entry_Call_Alternative,
957 N_Triggering_Alternative)
958 then
959 Error_Msg_N ("entry name expected", Nam);
961 -- Otherwise give general error message
963 else
964 Error_Msg_N ("invalid prefix in call", Nam);
965 end if;
966 end No_Interpretation;
968 -- Start of processing for Analyze_Call
970 begin
971 if Restriction_Check_Required (SPARK) then
972 Check_Mixed_Parameter_And_Named_Associations;
973 end if;
975 -- Initialize the type of the result of the call to the error type,
976 -- which will be reset if the type is successfully resolved.
978 Set_Etype (N, Any_Type);
980 Nam := Name (N);
982 if not Is_Overloaded (Nam) then
984 -- Only one interpretation to check
986 if Ekind (Etype (Nam)) = E_Subprogram_Type then
987 Nam_Ent := Etype (Nam);
989 -- If the prefix is an access_to_subprogram, this may be an indirect
990 -- call. This is the case if the name in the call is not an entity
991 -- name, or if it is a function name in the context of a procedure
992 -- call. In this latter case, we have a call to a parameterless
993 -- function that returns a pointer_to_procedure which is the entity
994 -- being called. Finally, F (X) may be a call to a parameterless
995 -- function that returns a pointer to a function with parameters.
997 elsif Is_Access_Type (Etype (Nam))
998 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
999 and then
1000 (not Name_Denotes_Function
1001 or else Nkind (N) = N_Procedure_Call_Statement
1002 or else
1003 (Nkind (Parent (N)) /= N_Explicit_Dereference
1004 and then Is_Entity_Name (Nam)
1005 and then No (First_Formal (Entity (Nam)))
1006 and then Present (Actuals)))
1007 then
1008 Nam_Ent := Designated_Type (Etype (Nam));
1009 Insert_Explicit_Dereference (Nam);
1011 -- Selected component case. Simple entry or protected operation,
1012 -- where the entry name is given by the selector name.
1014 elsif Nkind (Nam) = N_Selected_Component then
1015 Nam_Ent := Entity (Selector_Name (Nam));
1017 if not Ekind_In (Nam_Ent, E_Entry,
1018 E_Entry_Family,
1019 E_Function,
1020 E_Procedure)
1021 then
1022 Error_Msg_N ("name in call is not a callable entity", Nam);
1023 Set_Etype (N, Any_Type);
1024 return;
1025 end if;
1027 -- If the name is an Indexed component, it can be a call to a member
1028 -- of an entry family. The prefix must be a selected component whose
1029 -- selector is the entry. Analyze_Procedure_Call normalizes several
1030 -- kinds of call into this form.
1032 elsif Nkind (Nam) = N_Indexed_Component then
1033 if Nkind (Prefix (Nam)) = N_Selected_Component then
1034 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1035 else
1036 Error_Msg_N ("name in call is not a callable entity", Nam);
1037 Set_Etype (N, Any_Type);
1038 return;
1039 end if;
1041 elsif not Is_Entity_Name (Nam) then
1042 Error_Msg_N ("name in call is not a callable entity", Nam);
1043 Set_Etype (N, Any_Type);
1044 return;
1046 else
1047 Nam_Ent := Entity (Nam);
1049 -- If no interpretations, give error message
1051 if not Is_Overloadable (Nam_Ent) then
1052 No_Interpretation;
1053 return;
1054 end if;
1055 end if;
1057 -- Operations generated for RACW stub types are called only through
1058 -- dispatching, and can never be the static interpretation of a call.
1060 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1061 No_Interpretation;
1062 return;
1063 end if;
1065 Analyze_One_Call (N, Nam_Ent, True, Success);
1067 -- If this is an indirect call, the return type of the access_to
1068 -- subprogram may be an incomplete type. At the point of the call,
1069 -- use the full type if available, and at the same time update the
1070 -- return type of the access_to_subprogram.
1072 if Success
1073 and then Nkind (Nam) = N_Explicit_Dereference
1074 and then Ekind (Etype (N)) = E_Incomplete_Type
1075 and then Present (Full_View (Etype (N)))
1076 then
1077 Set_Etype (N, Full_View (Etype (N)));
1078 Set_Etype (Nam_Ent, Etype (N));
1079 end if;
1081 else
1082 -- An overloaded selected component must denote overloaded operations
1083 -- of a concurrent type. The interpretations are attached to the
1084 -- simple name of those operations.
1086 if Nkind (Nam) = N_Selected_Component then
1087 Nam := Selector_Name (Nam);
1088 end if;
1090 Get_First_Interp (Nam, X, It);
1092 while Present (It.Nam) loop
1093 Nam_Ent := It.Nam;
1094 Deref := False;
1096 -- Name may be call that returns an access to subprogram, or more
1097 -- generally an overloaded expression one of whose interpretations
1098 -- yields an access to subprogram. If the name is an entity, we do
1099 -- not dereference, because the node is a call that returns the
1100 -- access type: note difference between f(x), where the call may
1101 -- return an access subprogram type, and f(x)(y), where the type
1102 -- returned by the call to f is implicitly dereferenced to analyze
1103 -- the outer call.
1105 if Is_Access_Type (Nam_Ent) then
1106 Nam_Ent := Designated_Type (Nam_Ent);
1108 elsif Is_Access_Type (Etype (Nam_Ent))
1109 and then
1110 (not Is_Entity_Name (Nam)
1111 or else Nkind (N) = N_Procedure_Call_Statement)
1112 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1113 = E_Subprogram_Type
1114 then
1115 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1117 if Is_Entity_Name (Nam) then
1118 Deref := True;
1119 end if;
1120 end if;
1122 -- If the call has been rewritten from a prefixed call, the first
1123 -- parameter has been analyzed, but may need a subsequent
1124 -- dereference, so skip its analysis now.
1126 if N /= Original_Node (N)
1127 and then Nkind (Original_Node (N)) = Nkind (N)
1128 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1129 and then Present (Parameter_Associations (N))
1130 and then Present (Etype (First (Parameter_Associations (N))))
1131 then
1132 Analyze_One_Call
1133 (N, Nam_Ent, False, Success, Skip_First => True);
1134 else
1135 Analyze_One_Call (N, Nam_Ent, False, Success);
1136 end if;
1138 -- If the interpretation succeeds, mark the proper type of the
1139 -- prefix (any valid candidate will do). If not, remove the
1140 -- candidate interpretation. This only needs to be done for
1141 -- overloaded protected operations, for other entities disambi-
1142 -- guation is done directly in Resolve.
1144 if Success then
1145 if Deref
1146 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1147 then
1148 Set_Entity (Nam, It.Nam);
1149 Insert_Explicit_Dereference (Nam);
1150 Set_Etype (Nam, Nam_Ent);
1152 else
1153 Set_Etype (Nam, It.Typ);
1154 end if;
1156 elsif Nkind_In (Name (N), N_Selected_Component,
1157 N_Function_Call)
1158 then
1159 Remove_Interp (X);
1160 end if;
1162 Get_Next_Interp (X, It);
1163 end loop;
1165 -- If the name is the result of a function call, it can only
1166 -- be a call to a function returning an access to subprogram.
1167 -- Insert explicit dereference.
1169 if Nkind (Nam) = N_Function_Call then
1170 Insert_Explicit_Dereference (Nam);
1171 end if;
1173 if Etype (N) = Any_Type then
1175 -- None of the interpretations is compatible with the actuals
1177 Diagnose_Call (N, Nam);
1179 -- Special checks for uninstantiated put routines
1181 if Nkind (N) = N_Procedure_Call_Statement
1182 and then Is_Entity_Name (Nam)
1183 and then Chars (Nam) = Name_Put
1184 and then List_Length (Actuals) = 1
1185 then
1186 declare
1187 Arg : constant Node_Id := First (Actuals);
1188 Typ : Entity_Id;
1190 begin
1191 if Nkind (Arg) = N_Parameter_Association then
1192 Typ := Etype (Explicit_Actual_Parameter (Arg));
1193 else
1194 Typ := Etype (Arg);
1195 end if;
1197 if Is_Signed_Integer_Type (Typ) then
1198 Error_Msg_N
1199 ("possible missing instantiation of " &
1200 "'Text_'I'O.'Integer_'I'O!", Nam);
1202 elsif Is_Modular_Integer_Type (Typ) then
1203 Error_Msg_N
1204 ("possible missing instantiation of " &
1205 "'Text_'I'O.'Modular_'I'O!", Nam);
1207 elsif Is_Floating_Point_Type (Typ) then
1208 Error_Msg_N
1209 ("possible missing instantiation of " &
1210 "'Text_'I'O.'Float_'I'O!", Nam);
1212 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1213 Error_Msg_N
1214 ("possible missing instantiation of " &
1215 "'Text_'I'O.'Fixed_'I'O!", Nam);
1217 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1218 Error_Msg_N
1219 ("possible missing instantiation of " &
1220 "'Text_'I'O.'Decimal_'I'O!", Nam);
1222 elsif Is_Enumeration_Type (Typ) then
1223 Error_Msg_N
1224 ("possible missing instantiation of " &
1225 "'Text_'I'O.'Enumeration_'I'O!", Nam);
1226 end if;
1227 end;
1228 end if;
1230 elsif not Is_Overloaded (N)
1231 and then Is_Entity_Name (Nam)
1232 then
1233 -- Resolution yields a single interpretation. Verify that the
1234 -- reference has capitalization consistent with the declaration.
1236 Set_Entity_With_Style_Check (Nam, Entity (Nam));
1237 Generate_Reference (Entity (Nam), Nam);
1239 Set_Etype (Nam, Etype (Entity (Nam)));
1240 else
1241 Remove_Abstract_Operations (N);
1242 end if;
1244 End_Interp_List;
1245 end if;
1246 end Analyze_Call;
1248 -----------------------------
1249 -- Analyze_Case_Expression --
1250 -----------------------------
1252 procedure Analyze_Case_Expression (N : Node_Id) is
1253 Expr : constant Node_Id := Expression (N);
1254 FirstX : constant Node_Id := Expression (First (Alternatives (N)));
1255 Alt : Node_Id;
1256 Exp_Type : Entity_Id;
1257 Exp_Btype : Entity_Id;
1259 Dont_Care : Boolean;
1260 Others_Present : Boolean;
1262 procedure Non_Static_Choice_Error (Choice : Node_Id);
1263 -- Error routine invoked by the generic instantiation below when
1264 -- the case expression has a non static choice.
1266 package Case_Choices_Processing is new
1267 Generic_Choices_Processing
1268 (Get_Alternatives => Alternatives,
1269 Get_Choices => Discrete_Choices,
1270 Process_Empty_Choice => No_OP,
1271 Process_Non_Static_Choice => Non_Static_Choice_Error,
1272 Process_Associated_Node => No_OP);
1273 use Case_Choices_Processing;
1275 -----------------------------
1276 -- Non_Static_Choice_Error --
1277 -----------------------------
1279 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1280 begin
1281 Flag_Non_Static_Expr
1282 ("choice given in case expression is not static!", Choice);
1283 end Non_Static_Choice_Error;
1285 -- Start of processing for Analyze_Case_Expression
1287 begin
1288 if Comes_From_Source (N) then
1289 Check_Compiler_Unit (N);
1290 end if;
1292 Analyze_And_Resolve (Expr, Any_Discrete);
1293 Check_Unset_Reference (Expr);
1294 Exp_Type := Etype (Expr);
1295 Exp_Btype := Base_Type (Exp_Type);
1297 Alt := First (Alternatives (N));
1298 while Present (Alt) loop
1299 Analyze (Expression (Alt));
1300 Next (Alt);
1301 end loop;
1303 if not Is_Overloaded (FirstX) then
1304 Set_Etype (N, Etype (FirstX));
1306 else
1307 declare
1308 I : Interp_Index;
1309 It : Interp;
1311 begin
1312 Set_Etype (N, Any_Type);
1314 Get_First_Interp (FirstX, I, It);
1315 while Present (It.Nam) loop
1317 -- For each interpretation of the first expression, we only
1318 -- add the interpretation if every other expression in the
1319 -- case expression alternatives has a compatible type.
1321 Alt := Next (First (Alternatives (N)));
1322 while Present (Alt) loop
1323 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1324 Next (Alt);
1325 end loop;
1327 if No (Alt) then
1328 Add_One_Interp (N, It.Typ, It.Typ);
1329 end if;
1331 Get_Next_Interp (I, It);
1332 end loop;
1333 end;
1334 end if;
1336 Exp_Btype := Base_Type (Exp_Type);
1338 -- The expression must be of a discrete type which must be determinable
1339 -- independently of the context in which the expression occurs, but
1340 -- using the fact that the expression must be of a discrete type.
1341 -- Moreover, the type this expression must not be a character literal
1342 -- (which is always ambiguous).
1344 -- If error already reported by Resolve, nothing more to do
1346 if Exp_Btype = Any_Discrete
1347 or else Exp_Btype = Any_Type
1348 then
1349 return;
1351 elsif Exp_Btype = Any_Character then
1352 Error_Msg_N
1353 ("character literal as case expression is ambiguous", Expr);
1354 return;
1355 end if;
1357 -- If the case expression is a formal object of mode in out, then
1358 -- treat it as having a nonstatic subtype by forcing use of the base
1359 -- type (which has to get passed to Check_Case_Choices below). Also
1360 -- use base type when the case expression is parenthesized.
1362 if Paren_Count (Expr) > 0
1363 or else (Is_Entity_Name (Expr)
1364 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1365 then
1366 Exp_Type := Exp_Btype;
1367 end if;
1369 -- Call instantiated Analyze_Choices which does the rest of the work
1371 Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1373 if Exp_Type = Universal_Integer and then not Others_Present then
1374 Error_Msg_N
1375 ("case on universal integer requires OTHERS choice", Expr);
1376 end if;
1377 end Analyze_Case_Expression;
1379 ---------------------------
1380 -- Analyze_Comparison_Op --
1381 ---------------------------
1383 procedure Analyze_Comparison_Op (N : Node_Id) is
1384 L : constant Node_Id := Left_Opnd (N);
1385 R : constant Node_Id := Right_Opnd (N);
1386 Op_Id : Entity_Id := Entity (N);
1388 begin
1389 Set_Etype (N, Any_Type);
1390 Candidate_Type := Empty;
1392 Analyze_Expression (L);
1393 Analyze_Expression (R);
1395 if Present (Op_Id) then
1396 if Ekind (Op_Id) = E_Operator then
1397 Find_Comparison_Types (L, R, Op_Id, N);
1398 else
1399 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1400 end if;
1402 if Is_Overloaded (L) then
1403 Set_Etype (L, Intersect_Types (L, R));
1404 end if;
1406 else
1407 Op_Id := Get_Name_Entity_Id (Chars (N));
1408 while Present (Op_Id) loop
1409 if Ekind (Op_Id) = E_Operator then
1410 Find_Comparison_Types (L, R, Op_Id, N);
1411 else
1412 Analyze_User_Defined_Binary_Op (N, Op_Id);
1413 end if;
1415 Op_Id := Homonym (Op_Id);
1416 end loop;
1417 end if;
1419 Operator_Check (N);
1420 end Analyze_Comparison_Op;
1422 ---------------------------
1423 -- Analyze_Concatenation --
1424 ---------------------------
1426 procedure Analyze_Concatenation (N : Node_Id) is
1428 -- We wish to avoid deep recursion, because concatenations are often
1429 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1430 -- operands nonrecursively until we find something that is not a
1431 -- concatenation (A in this case), or has already been analyzed. We
1432 -- analyze that, and then walk back up the tree following Parent
1433 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1434 -- work at each level. The Parent pointers allow us to avoid recursion,
1435 -- and thus avoid running out of memory.
1437 NN : Node_Id := N;
1438 L : Node_Id;
1440 begin
1441 Candidate_Type := Empty;
1443 -- The following code is equivalent to:
1445 -- Set_Etype (N, Any_Type);
1446 -- Analyze_Expression (Left_Opnd (N));
1447 -- Analyze_Concatenation_Rest (N);
1449 -- where the Analyze_Expression call recurses back here if the left
1450 -- operand is a concatenation.
1452 -- Walk down left operands
1454 loop
1455 Set_Etype (NN, Any_Type);
1456 L := Left_Opnd (NN);
1457 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1458 NN := L;
1459 end loop;
1461 -- Now (given the above example) NN is A&B and L is A
1463 -- First analyze L ...
1465 Analyze_Expression (L);
1467 -- ... then walk NN back up until we reach N (where we started), calling
1468 -- Analyze_Concatenation_Rest along the way.
1470 loop
1471 Analyze_Concatenation_Rest (NN);
1472 exit when NN = N;
1473 NN := Parent (NN);
1474 end loop;
1475 end Analyze_Concatenation;
1477 --------------------------------
1478 -- Analyze_Concatenation_Rest --
1479 --------------------------------
1481 -- If the only one-dimensional array type in scope is String,
1482 -- this is the resulting type of the operation. Otherwise there
1483 -- will be a concatenation operation defined for each user-defined
1484 -- one-dimensional array.
1486 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1487 L : constant Node_Id := Left_Opnd (N);
1488 R : constant Node_Id := Right_Opnd (N);
1489 Op_Id : Entity_Id := Entity (N);
1490 LT : Entity_Id;
1491 RT : Entity_Id;
1493 begin
1494 Analyze_Expression (R);
1496 -- If the entity is present, the node appears in an instance, and
1497 -- denotes a predefined concatenation operation. The resulting type is
1498 -- obtained from the arguments when possible. If the arguments are
1499 -- aggregates, the array type and the concatenation type must be
1500 -- visible.
1502 if Present (Op_Id) then
1503 if Ekind (Op_Id) = E_Operator then
1504 LT := Base_Type (Etype (L));
1505 RT := Base_Type (Etype (R));
1507 if Is_Array_Type (LT)
1508 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1509 then
1510 Add_One_Interp (N, Op_Id, LT);
1512 elsif Is_Array_Type (RT)
1513 and then LT = Base_Type (Component_Type (RT))
1514 then
1515 Add_One_Interp (N, Op_Id, RT);
1517 -- If one operand is a string type or a user-defined array type,
1518 -- and the other is a literal, result is of the specific type.
1520 elsif
1521 (Root_Type (LT) = Standard_String
1522 or else Scope (LT) /= Standard_Standard)
1523 and then Etype (R) = Any_String
1524 then
1525 Add_One_Interp (N, Op_Id, LT);
1527 elsif
1528 (Root_Type (RT) = Standard_String
1529 or else Scope (RT) /= Standard_Standard)
1530 and then Etype (L) = Any_String
1531 then
1532 Add_One_Interp (N, Op_Id, RT);
1534 elsif not Is_Generic_Type (Etype (Op_Id)) then
1535 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1537 else
1538 -- Type and its operations must be visible
1540 Set_Entity (N, Empty);
1541 Analyze_Concatenation (N);
1542 end if;
1544 else
1545 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1546 end if;
1548 else
1549 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1550 while Present (Op_Id) loop
1551 if Ekind (Op_Id) = E_Operator then
1553 -- Do not consider operators declared in dead code, they can
1554 -- not be part of the resolution.
1556 if Is_Eliminated (Op_Id) then
1557 null;
1558 else
1559 Find_Concatenation_Types (L, R, Op_Id, N);
1560 end if;
1562 else
1563 Analyze_User_Defined_Binary_Op (N, Op_Id);
1564 end if;
1566 Op_Id := Homonym (Op_Id);
1567 end loop;
1568 end if;
1570 Operator_Check (N);
1571 end Analyze_Concatenation_Rest;
1573 ------------------------------------
1574 -- Analyze_Conditional_Expression --
1575 ------------------------------------
1577 procedure Analyze_Conditional_Expression (N : Node_Id) is
1578 Condition : constant Node_Id := First (Expressions (N));
1579 Then_Expr : constant Node_Id := Next (Condition);
1580 Else_Expr : Node_Id;
1582 begin
1583 -- Defend against error of missing expressions from previous error
1585 if No (Then_Expr) then
1586 return;
1587 end if;
1589 Check_SPARK_Restriction ("conditional expression is not allowed", N);
1591 Else_Expr := Next (Then_Expr);
1593 if Comes_From_Source (N) then
1594 Check_Compiler_Unit (N);
1595 end if;
1597 Analyze_Expression (Condition);
1598 Analyze_Expression (Then_Expr);
1600 if Present (Else_Expr) then
1601 Analyze_Expression (Else_Expr);
1602 end if;
1604 -- If then expression not overloaded, then that decides the type
1606 if not Is_Overloaded (Then_Expr) then
1607 Set_Etype (N, Etype (Then_Expr));
1609 -- Case where then expression is overloaded
1611 else
1612 declare
1613 I : Interp_Index;
1614 It : Interp;
1616 begin
1617 Set_Etype (N, Any_Type);
1619 -- Shouldn't the following statement be down in the ELSE of the
1620 -- following loop? ???
1622 Get_First_Interp (Then_Expr, I, It);
1624 -- if no Else_Expression the conditional must be boolean
1626 if No (Else_Expr) then
1627 Set_Etype (N, Standard_Boolean);
1629 -- Else_Expression Present. For each possible intepretation of
1630 -- the Then_Expression, add it only if the Else_Expression has
1631 -- a compatible type.
1633 else
1634 while Present (It.Nam) loop
1635 if Has_Compatible_Type (Else_Expr, It.Typ) then
1636 Add_One_Interp (N, It.Typ, It.Typ);
1637 end if;
1639 Get_Next_Interp (I, It);
1640 end loop;
1641 end if;
1642 end;
1643 end if;
1644 end Analyze_Conditional_Expression;
1646 -------------------------
1647 -- Analyze_Equality_Op --
1648 -------------------------
1650 procedure Analyze_Equality_Op (N : Node_Id) is
1651 Loc : constant Source_Ptr := Sloc (N);
1652 L : constant Node_Id := Left_Opnd (N);
1653 R : constant Node_Id := Right_Opnd (N);
1654 Op_Id : Entity_Id;
1656 begin
1657 Set_Etype (N, Any_Type);
1658 Candidate_Type := Empty;
1660 Analyze_Expression (L);
1661 Analyze_Expression (R);
1663 -- If the entity is set, the node is a generic instance with a non-local
1664 -- reference to the predefined operator or to a user-defined function.
1665 -- It can also be an inequality that is expanded into the negation of a
1666 -- call to a user-defined equality operator.
1668 -- For the predefined case, the result is Boolean, regardless of the
1669 -- type of the operands. The operands may even be limited, if they are
1670 -- generic actuals. If they are overloaded, label the left argument with
1671 -- the common type that must be present, or with the type of the formal
1672 -- of the user-defined function.
1674 if Present (Entity (N)) then
1675 Op_Id := Entity (N);
1677 if Ekind (Op_Id) = E_Operator then
1678 Add_One_Interp (N, Op_Id, Standard_Boolean);
1679 else
1680 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1681 end if;
1683 if Is_Overloaded (L) then
1684 if Ekind (Op_Id) = E_Operator then
1685 Set_Etype (L, Intersect_Types (L, R));
1686 else
1687 Set_Etype (L, Etype (First_Formal (Op_Id)));
1688 end if;
1689 end if;
1691 else
1692 Op_Id := Get_Name_Entity_Id (Chars (N));
1693 while Present (Op_Id) loop
1694 if Ekind (Op_Id) = E_Operator then
1695 Find_Equality_Types (L, R, Op_Id, N);
1696 else
1697 Analyze_User_Defined_Binary_Op (N, Op_Id);
1698 end if;
1700 Op_Id := Homonym (Op_Id);
1701 end loop;
1702 end if;
1704 -- If there was no match, and the operator is inequality, this may
1705 -- be a case where inequality has not been made explicit, as for
1706 -- tagged types. Analyze the node as the negation of an equality
1707 -- operation. This cannot be done earlier, because before analysis
1708 -- we cannot rule out the presence of an explicit inequality.
1710 if Etype (N) = Any_Type
1711 and then Nkind (N) = N_Op_Ne
1712 then
1713 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1714 while Present (Op_Id) loop
1715 if Ekind (Op_Id) = E_Operator then
1716 Find_Equality_Types (L, R, Op_Id, N);
1717 else
1718 Analyze_User_Defined_Binary_Op (N, Op_Id);
1719 end if;
1721 Op_Id := Homonym (Op_Id);
1722 end loop;
1724 if Etype (N) /= Any_Type then
1725 Op_Id := Entity (N);
1727 Rewrite (N,
1728 Make_Op_Not (Loc,
1729 Right_Opnd =>
1730 Make_Op_Eq (Loc,
1731 Left_Opnd => Left_Opnd (N),
1732 Right_Opnd => Right_Opnd (N))));
1734 Set_Entity (Right_Opnd (N), Op_Id);
1735 Analyze (N);
1736 end if;
1737 end if;
1739 Operator_Check (N);
1740 end Analyze_Equality_Op;
1742 ----------------------------------
1743 -- Analyze_Explicit_Dereference --
1744 ----------------------------------
1746 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1747 Loc : constant Source_Ptr := Sloc (N);
1748 P : constant Node_Id := Prefix (N);
1749 T : Entity_Id;
1750 I : Interp_Index;
1751 It : Interp;
1752 New_N : Node_Id;
1754 function Is_Function_Type return Boolean;
1755 -- Check whether node may be interpreted as an implicit function call
1757 ----------------------
1758 -- Is_Function_Type --
1759 ----------------------
1761 function Is_Function_Type return Boolean is
1762 I : Interp_Index;
1763 It : Interp;
1765 begin
1766 if not Is_Overloaded (N) then
1767 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1768 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1770 else
1771 Get_First_Interp (N, I, It);
1772 while Present (It.Nam) loop
1773 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1774 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1775 then
1776 return False;
1777 end if;
1779 Get_Next_Interp (I, It);
1780 end loop;
1782 return True;
1783 end if;
1784 end Is_Function_Type;
1786 -- Start of processing for Analyze_Explicit_Dereference
1788 begin
1789 -- If source node, check SPARK restriction. We guard this with the
1790 -- source node check, because ???
1792 if Comes_From_Source (N) then
1793 Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1794 end if;
1796 -- In formal verification mode, keep track of all reads and writes
1797 -- through explicit dereferences.
1799 if Alfa_Mode then
1800 Alfa.Generate_Dereference (N);
1801 end if;
1803 Analyze (P);
1804 Set_Etype (N, Any_Type);
1806 -- Test for remote access to subprogram type, and if so return
1807 -- after rewriting the original tree.
1809 if Remote_AST_E_Dereference (P) then
1810 return;
1811 end if;
1813 -- Normal processing for other than remote access to subprogram type
1815 if not Is_Overloaded (P) then
1816 if Is_Access_Type (Etype (P)) then
1818 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1819 -- avoid other problems caused by the Private_Subtype and it is
1820 -- safe to go to the Base_Type because this is the same as
1821 -- converting the access value to its Base_Type.
1823 declare
1824 DT : Entity_Id := Designated_Type (Etype (P));
1826 begin
1827 if Ekind (DT) = E_Private_Subtype
1828 and then Is_For_Access_Subtype (DT)
1829 then
1830 DT := Base_Type (DT);
1831 end if;
1833 -- An explicit dereference is a legal occurrence of an
1834 -- incomplete type imported through a limited_with clause,
1835 -- if the full view is visible.
1837 if From_With_Type (DT)
1838 and then not From_With_Type (Scope (DT))
1839 and then
1840 (Is_Immediately_Visible (Scope (DT))
1841 or else
1842 (Is_Child_Unit (Scope (DT))
1843 and then Is_Visible_Child_Unit (Scope (DT))))
1844 then
1845 Set_Etype (N, Available_View (DT));
1847 else
1848 Set_Etype (N, DT);
1849 end if;
1850 end;
1852 elsif Etype (P) /= Any_Type then
1853 Error_Msg_N ("prefix of dereference must be an access type", N);
1854 return;
1855 end if;
1857 else
1858 Get_First_Interp (P, I, It);
1859 while Present (It.Nam) loop
1860 T := It.Typ;
1862 if Is_Access_Type (T) then
1863 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1864 end if;
1866 Get_Next_Interp (I, It);
1867 end loop;
1869 -- Error if no interpretation of the prefix has an access type
1871 if Etype (N) = Any_Type then
1872 Error_Msg_N
1873 ("access type required in prefix of explicit dereference", P);
1874 Set_Etype (N, Any_Type);
1875 return;
1876 end if;
1877 end if;
1879 if Is_Function_Type
1880 and then Nkind (Parent (N)) /= N_Indexed_Component
1882 and then (Nkind (Parent (N)) /= N_Function_Call
1883 or else N /= Name (Parent (N)))
1885 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1886 or else N /= Name (Parent (N)))
1888 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1889 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1890 or else
1891 (Attribute_Name (Parent (N)) /= Name_Address
1892 and then
1893 Attribute_Name (Parent (N)) /= Name_Access))
1894 then
1895 -- Name is a function call with no actuals, in a context that
1896 -- requires deproceduring (including as an actual in an enclosing
1897 -- function or procedure call). There are some pathological cases
1898 -- where the prefix might include functions that return access to
1899 -- subprograms and others that return a regular type. Disambiguation
1900 -- of those has to take place in Resolve.
1902 New_N :=
1903 Make_Function_Call (Loc,
1904 Name => Make_Explicit_Dereference (Loc, P),
1905 Parameter_Associations => New_List);
1907 -- If the prefix is overloaded, remove operations that have formals,
1908 -- we know that this is a parameterless call.
1910 if Is_Overloaded (P) then
1911 Get_First_Interp (P, I, It);
1912 while Present (It.Nam) loop
1913 T := It.Typ;
1915 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1916 Set_Etype (P, T);
1917 else
1918 Remove_Interp (I);
1919 end if;
1921 Get_Next_Interp (I, It);
1922 end loop;
1923 end if;
1925 Rewrite (N, New_N);
1926 Analyze (N);
1928 elsif not Is_Function_Type
1929 and then Is_Overloaded (N)
1930 then
1931 -- The prefix may include access to subprograms and other access
1932 -- types. If the context selects the interpretation that is a
1933 -- function call (not a procedure call) we cannot rewrite the node
1934 -- yet, but we include the result of the call interpretation.
1936 Get_First_Interp (N, I, It);
1937 while Present (It.Nam) loop
1938 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1939 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1940 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1941 then
1942 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1943 end if;
1945 Get_Next_Interp (I, It);
1946 end loop;
1947 end if;
1949 -- A value of remote access-to-class-wide must not be dereferenced
1950 -- (RM E.2.2(16)).
1952 Validate_Remote_Access_To_Class_Wide_Type (N);
1953 end Analyze_Explicit_Dereference;
1955 ------------------------
1956 -- Analyze_Expression --
1957 ------------------------
1959 procedure Analyze_Expression (N : Node_Id) is
1960 begin
1961 Analyze (N);
1962 Check_Parameterless_Call (N);
1963 end Analyze_Expression;
1965 -------------------------------------
1966 -- Analyze_Expression_With_Actions --
1967 -------------------------------------
1969 procedure Analyze_Expression_With_Actions (N : Node_Id) is
1970 A : Node_Id;
1972 begin
1973 A := First (Actions (N));
1974 loop
1975 Analyze (A);
1976 Next (A);
1977 exit when No (A);
1978 end loop;
1980 Analyze_Expression (Expression (N));
1981 Set_Etype (N, Etype (Expression (N)));
1982 end Analyze_Expression_With_Actions;
1984 ------------------------------------
1985 -- Analyze_Indexed_Component_Form --
1986 ------------------------------------
1988 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1989 P : constant Node_Id := Prefix (N);
1990 Exprs : constant List_Id := Expressions (N);
1991 Exp : Node_Id;
1992 P_T : Entity_Id;
1993 E : Node_Id;
1994 U_N : Entity_Id;
1996 procedure Process_Function_Call;
1997 -- Prefix in indexed component form is an overloadable entity,
1998 -- so the node is a function call. Reformat it as such.
2000 procedure Process_Indexed_Component;
2001 -- Prefix in indexed component form is actually an indexed component.
2002 -- This routine processes it, knowing that the prefix is already
2003 -- resolved.
2005 procedure Process_Indexed_Component_Or_Slice;
2006 -- An indexed component with a single index may designate a slice if
2007 -- the index is a subtype mark. This routine disambiguates these two
2008 -- cases by resolving the prefix to see if it is a subtype mark.
2010 procedure Process_Overloaded_Indexed_Component;
2011 -- If the prefix of an indexed component is overloaded, the proper
2012 -- interpretation is selected by the index types and the context.
2014 ---------------------------
2015 -- Process_Function_Call --
2016 ---------------------------
2018 procedure Process_Function_Call is
2019 Actual : Node_Id;
2021 begin
2022 Change_Node (N, N_Function_Call);
2023 Set_Name (N, P);
2024 Set_Parameter_Associations (N, Exprs);
2026 -- Analyze actuals prior to analyzing the call itself
2028 Actual := First (Parameter_Associations (N));
2029 while Present (Actual) loop
2030 Analyze (Actual);
2031 Check_Parameterless_Call (Actual);
2033 -- Move to next actual. Note that we use Next, not Next_Actual
2034 -- here. The reason for this is a bit subtle. If a function call
2035 -- includes named associations, the parser recognizes the node as
2036 -- a call, and it is analyzed as such. If all associations are
2037 -- positional, the parser builds an indexed_component node, and
2038 -- it is only after analysis of the prefix that the construct
2039 -- is recognized as a call, in which case Process_Function_Call
2040 -- rewrites the node and analyzes the actuals. If the list of
2041 -- actuals is malformed, the parser may leave the node as an
2042 -- indexed component (despite the presence of named associations).
2043 -- The iterator Next_Actual is equivalent to Next if the list is
2044 -- positional, but follows the normalized chain of actuals when
2045 -- named associations are present. In this case normalization has
2046 -- not taken place, and actuals remain unanalyzed, which leads to
2047 -- subsequent crashes or loops if there is an attempt to continue
2048 -- analysis of the program.
2050 Next (Actual);
2051 end loop;
2053 Analyze_Call (N);
2054 end Process_Function_Call;
2056 -------------------------------
2057 -- Process_Indexed_Component --
2058 -------------------------------
2060 procedure Process_Indexed_Component is
2061 Exp : Node_Id;
2062 Array_Type : Entity_Id;
2063 Index : Node_Id;
2064 Pent : Entity_Id := Empty;
2066 begin
2067 Exp := First (Exprs);
2069 if Is_Overloaded (P) then
2070 Process_Overloaded_Indexed_Component;
2072 else
2073 Array_Type := Etype (P);
2075 if Is_Entity_Name (P) then
2076 Pent := Entity (P);
2077 elsif Nkind (P) = N_Selected_Component
2078 and then Is_Entity_Name (Selector_Name (P))
2079 then
2080 Pent := Entity (Selector_Name (P));
2081 end if;
2083 -- Prefix must be appropriate for an array type, taking into
2084 -- account a possible implicit dereference.
2086 if Is_Access_Type (Array_Type) then
2087 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2088 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2089 end if;
2091 if Is_Array_Type (Array_Type) then
2092 null;
2094 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2095 Analyze (Exp);
2096 Set_Etype (N, Any_Type);
2098 if not Has_Compatible_Type
2099 (Exp, Entry_Index_Type (Pent))
2100 then
2101 Error_Msg_N ("invalid index type in entry name", N);
2103 elsif Present (Next (Exp)) then
2104 Error_Msg_N ("too many subscripts in entry reference", N);
2106 else
2107 Set_Etype (N, Etype (P));
2108 end if;
2110 return;
2112 elsif Is_Record_Type (Array_Type)
2113 and then Remote_AST_I_Dereference (P)
2114 then
2115 return;
2117 elsif Try_Container_Indexing (N, P, Exprs) then
2118 return;
2120 elsif Array_Type = Any_Type then
2121 Set_Etype (N, Any_Type);
2123 -- In most cases the analysis of the prefix will have emitted
2124 -- an error already, but if the prefix may be interpreted as a
2125 -- call in prefixed notation, the report is left to the caller.
2126 -- To prevent cascaded errors, report only if no previous ones.
2128 if Serious_Errors_Detected = 0 then
2129 Error_Msg_N ("invalid prefix in indexed component", P);
2131 if Nkind (P) = N_Expanded_Name then
2132 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2133 end if;
2134 end if;
2136 return;
2138 -- Here we definitely have a bad indexing
2140 else
2141 if Nkind (Parent (N)) = N_Requeue_Statement
2142 and then Present (Pent) and then Ekind (Pent) = E_Entry
2143 then
2144 Error_Msg_N
2145 ("REQUEUE does not permit parameters", First (Exprs));
2147 elsif Is_Entity_Name (P)
2148 and then Etype (P) = Standard_Void_Type
2149 then
2150 Error_Msg_NE ("incorrect use of&", P, Entity (P));
2152 else
2153 Error_Msg_N ("array type required in indexed component", P);
2154 end if;
2156 Set_Etype (N, Any_Type);
2157 return;
2158 end if;
2160 Index := First_Index (Array_Type);
2161 while Present (Index) and then Present (Exp) loop
2162 if not Has_Compatible_Type (Exp, Etype (Index)) then
2163 Wrong_Type (Exp, Etype (Index));
2164 Set_Etype (N, Any_Type);
2165 return;
2166 end if;
2168 Next_Index (Index);
2169 Next (Exp);
2170 end loop;
2172 Set_Etype (N, Component_Type (Array_Type));
2173 Check_Implicit_Dereference (N, Etype (N));
2175 if Present (Index) then
2176 Error_Msg_N
2177 ("too few subscripts in array reference", First (Exprs));
2179 elsif Present (Exp) then
2180 Error_Msg_N ("too many subscripts in array reference", Exp);
2181 end if;
2182 end if;
2183 end Process_Indexed_Component;
2185 ----------------------------------------
2186 -- Process_Indexed_Component_Or_Slice --
2187 ----------------------------------------
2189 procedure Process_Indexed_Component_Or_Slice is
2190 begin
2191 Exp := First (Exprs);
2192 while Present (Exp) loop
2193 Analyze_Expression (Exp);
2194 Next (Exp);
2195 end loop;
2197 Exp := First (Exprs);
2199 -- If one index is present, and it is a subtype name, then the
2200 -- node denotes a slice (note that the case of an explicit range
2201 -- for a slice was already built as an N_Slice node in the first
2202 -- place, so that case is not handled here).
2204 -- We use a replace rather than a rewrite here because this is one
2205 -- of the cases in which the tree built by the parser is plain wrong.
2207 if No (Next (Exp))
2208 and then Is_Entity_Name (Exp)
2209 and then Is_Type (Entity (Exp))
2210 then
2211 Replace (N,
2212 Make_Slice (Sloc (N),
2213 Prefix => P,
2214 Discrete_Range => New_Copy (Exp)));
2215 Analyze (N);
2217 -- Otherwise (more than one index present, or single index is not
2218 -- a subtype name), then we have the indexed component case.
2220 else
2221 Process_Indexed_Component;
2222 end if;
2223 end Process_Indexed_Component_Or_Slice;
2225 ------------------------------------------
2226 -- Process_Overloaded_Indexed_Component --
2227 ------------------------------------------
2229 procedure Process_Overloaded_Indexed_Component is
2230 Exp : Node_Id;
2231 I : Interp_Index;
2232 It : Interp;
2233 Typ : Entity_Id;
2234 Index : Node_Id;
2235 Found : Boolean;
2237 begin
2238 Set_Etype (N, Any_Type);
2240 Get_First_Interp (P, I, It);
2241 while Present (It.Nam) loop
2242 Typ := It.Typ;
2244 if Is_Access_Type (Typ) then
2245 Typ := Designated_Type (Typ);
2246 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2247 end if;
2249 if Is_Array_Type (Typ) then
2251 -- Got a candidate: verify that index types are compatible
2253 Index := First_Index (Typ);
2254 Found := True;
2255 Exp := First (Exprs);
2256 while Present (Index) and then Present (Exp) loop
2257 if Has_Compatible_Type (Exp, Etype (Index)) then
2258 null;
2259 else
2260 Found := False;
2261 Remove_Interp (I);
2262 exit;
2263 end if;
2265 Next_Index (Index);
2266 Next (Exp);
2267 end loop;
2269 if Found and then No (Index) and then No (Exp) then
2270 declare
2271 CT : constant Entity_Id :=
2272 Base_Type (Component_Type (Typ));
2273 begin
2274 Add_One_Interp (N, CT, CT);
2275 Check_Implicit_Dereference (N, CT);
2276 end;
2277 end if;
2279 elsif Try_Container_Indexing (N, P, Exprs) then
2280 return;
2282 end if;
2284 Get_Next_Interp (I, It);
2285 end loop;
2287 if Etype (N) = Any_Type then
2288 Error_Msg_N ("no legal interpretation for indexed component", N);
2289 Set_Is_Overloaded (N, False);
2290 end if;
2292 End_Interp_List;
2293 end Process_Overloaded_Indexed_Component;
2295 -- Start of processing for Analyze_Indexed_Component_Form
2297 begin
2298 -- Get name of array, function or type
2300 Analyze (P);
2302 if Nkind (N) in N_Subprogram_Call then
2304 -- If P is an explicit dereference whose prefix is of a
2305 -- remote access-to-subprogram type, then N has already
2306 -- been rewritten as a subprogram call and analyzed.
2308 return;
2309 end if;
2311 pragma Assert (Nkind (N) = N_Indexed_Component);
2313 P_T := Base_Type (Etype (P));
2315 if Is_Entity_Name (P) and then Present (Entity (P)) then
2316 U_N := Entity (P);
2318 if Is_Type (U_N) then
2320 -- Reformat node as a type conversion
2322 E := Remove_Head (Exprs);
2324 if Present (First (Exprs)) then
2325 Error_Msg_N
2326 ("argument of type conversion must be single expression", N);
2327 end if;
2329 Change_Node (N, N_Type_Conversion);
2330 Set_Subtype_Mark (N, P);
2331 Set_Etype (N, U_N);
2332 Set_Expression (N, E);
2334 -- After changing the node, call for the specific Analysis
2335 -- routine directly, to avoid a double call to the expander.
2337 Analyze_Type_Conversion (N);
2338 return;
2339 end if;
2341 if Is_Overloadable (U_N) then
2342 Process_Function_Call;
2344 elsif Ekind (Etype (P)) = E_Subprogram_Type
2345 or else (Is_Access_Type (Etype (P))
2346 and then
2347 Ekind (Designated_Type (Etype (P))) =
2348 E_Subprogram_Type)
2349 then
2350 -- Call to access_to-subprogram with possible implicit dereference
2352 Process_Function_Call;
2354 elsif Is_Generic_Subprogram (U_N) then
2356 -- A common beginner's (or C++ templates fan) error
2358 Error_Msg_N ("generic subprogram cannot be called", N);
2359 Set_Etype (N, Any_Type);
2360 return;
2362 else
2363 Process_Indexed_Component_Or_Slice;
2364 end if;
2366 -- If not an entity name, prefix is an expression that may denote
2367 -- an array or an access-to-subprogram.
2369 else
2370 if Ekind (P_T) = E_Subprogram_Type
2371 or else (Is_Access_Type (P_T)
2372 and then
2373 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2374 then
2375 Process_Function_Call;
2377 elsif Nkind (P) = N_Selected_Component
2378 and then Is_Overloadable (Entity (Selector_Name (P)))
2379 then
2380 Process_Function_Call;
2382 else
2383 -- Indexed component, slice, or a call to a member of a family
2384 -- entry, which will be converted to an entry call later.
2386 Process_Indexed_Component_Or_Slice;
2387 end if;
2388 end if;
2389 end Analyze_Indexed_Component_Form;
2391 ------------------------
2392 -- Analyze_Logical_Op --
2393 ------------------------
2395 procedure Analyze_Logical_Op (N : Node_Id) is
2396 L : constant Node_Id := Left_Opnd (N);
2397 R : constant Node_Id := Right_Opnd (N);
2398 Op_Id : Entity_Id := Entity (N);
2400 begin
2401 Set_Etype (N, Any_Type);
2402 Candidate_Type := Empty;
2404 Analyze_Expression (L);
2405 Analyze_Expression (R);
2407 if Present (Op_Id) then
2409 if Ekind (Op_Id) = E_Operator then
2410 Find_Boolean_Types (L, R, Op_Id, N);
2411 else
2412 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2413 end if;
2415 else
2416 Op_Id := Get_Name_Entity_Id (Chars (N));
2417 while Present (Op_Id) loop
2418 if Ekind (Op_Id) = E_Operator then
2419 Find_Boolean_Types (L, R, Op_Id, N);
2420 else
2421 Analyze_User_Defined_Binary_Op (N, Op_Id);
2422 end if;
2424 Op_Id := Homonym (Op_Id);
2425 end loop;
2426 end if;
2428 Operator_Check (N);
2429 end Analyze_Logical_Op;
2431 ---------------------------
2432 -- Analyze_Membership_Op --
2433 ---------------------------
2435 procedure Analyze_Membership_Op (N : Node_Id) is
2436 Loc : constant Source_Ptr := Sloc (N);
2437 L : constant Node_Id := Left_Opnd (N);
2438 R : constant Node_Id := Right_Opnd (N);
2440 Index : Interp_Index;
2441 It : Interp;
2442 Found : Boolean := False;
2443 I_F : Interp_Index;
2444 T_F : Entity_Id;
2446 procedure Try_One_Interp (T1 : Entity_Id);
2447 -- Routine to try one proposed interpretation. Note that the context
2448 -- of the operation plays no role in resolving the arguments, so that
2449 -- if there is more than one interpretation of the operands that is
2450 -- compatible with a membership test, the operation is ambiguous.
2452 --------------------
2453 -- Try_One_Interp --
2454 --------------------
2456 procedure Try_One_Interp (T1 : Entity_Id) is
2457 begin
2458 if Has_Compatible_Type (R, T1) then
2459 if Found
2460 and then Base_Type (T1) /= Base_Type (T_F)
2461 then
2462 It := Disambiguate (L, I_F, Index, Any_Type);
2464 if It = No_Interp then
2465 Ambiguous_Operands (N);
2466 Set_Etype (L, Any_Type);
2467 return;
2469 else
2470 T_F := It.Typ;
2471 end if;
2473 else
2474 Found := True;
2475 T_F := T1;
2476 I_F := Index;
2477 end if;
2479 Set_Etype (L, T_F);
2480 end if;
2481 end Try_One_Interp;
2483 procedure Analyze_Set_Membership;
2484 -- If a set of alternatives is present, analyze each and find the
2485 -- common type to which they must all resolve.
2487 ----------------------------
2488 -- Analyze_Set_Membership --
2489 ----------------------------
2491 procedure Analyze_Set_Membership is
2492 Alt : Node_Id;
2493 Index : Interp_Index;
2494 It : Interp;
2495 Candidate_Interps : Node_Id;
2496 Common_Type : Entity_Id := Empty;
2498 begin
2499 Analyze (L);
2500 Candidate_Interps := L;
2502 if not Is_Overloaded (L) then
2503 Common_Type := Etype (L);
2505 Alt := First (Alternatives (N));
2506 while Present (Alt) loop
2507 Analyze (Alt);
2509 if not Has_Compatible_Type (Alt, Common_Type) then
2510 Wrong_Type (Alt, Common_Type);
2511 end if;
2513 Next (Alt);
2514 end loop;
2516 else
2517 Alt := First (Alternatives (N));
2518 while Present (Alt) loop
2519 Analyze (Alt);
2520 if not Is_Overloaded (Alt) then
2521 Common_Type := Etype (Alt);
2523 else
2524 Get_First_Interp (Alt, Index, It);
2525 while Present (It.Typ) loop
2526 if not
2527 Has_Compatible_Type (Candidate_Interps, It.Typ)
2528 then
2529 Remove_Interp (Index);
2530 end if;
2532 Get_Next_Interp (Index, It);
2533 end loop;
2535 Get_First_Interp (Alt, Index, It);
2537 if No (It.Typ) then
2538 Error_Msg_N ("alternative has no legal type", Alt);
2539 return;
2540 end if;
2542 -- If alternative is not overloaded, we have a unique type
2543 -- for all of them.
2545 Set_Etype (Alt, It.Typ);
2546 Get_Next_Interp (Index, It);
2548 if No (It.Typ) then
2549 Set_Is_Overloaded (Alt, False);
2550 Common_Type := Etype (Alt);
2551 end if;
2553 Candidate_Interps := Alt;
2554 end if;
2556 Next (Alt);
2557 end loop;
2558 end if;
2560 Set_Etype (N, Standard_Boolean);
2562 if Present (Common_Type) then
2563 Set_Etype (L, Common_Type);
2564 Set_Is_Overloaded (L, False);
2566 else
2567 Error_Msg_N ("cannot resolve membership operation", N);
2568 end if;
2569 end Analyze_Set_Membership;
2571 -- Start of processing for Analyze_Membership_Op
2573 begin
2574 Analyze_Expression (L);
2576 if No (R)
2577 and then Ada_Version >= Ada_2012
2578 then
2579 Analyze_Set_Membership;
2580 return;
2581 end if;
2583 if Nkind (R) = N_Range
2584 or else (Nkind (R) = N_Attribute_Reference
2585 and then Attribute_Name (R) = Name_Range)
2586 then
2587 Analyze (R);
2589 if not Is_Overloaded (L) then
2590 Try_One_Interp (Etype (L));
2592 else
2593 Get_First_Interp (L, Index, It);
2594 while Present (It.Typ) loop
2595 Try_One_Interp (It.Typ);
2596 Get_Next_Interp (Index, It);
2597 end loop;
2598 end if;
2600 -- If not a range, it can be a subtype mark, or else it is a degenerate
2601 -- membership test with a singleton value, i.e. a test for equality,
2602 -- if the types are compatible.
2604 else
2605 Analyze (R);
2607 if Is_Entity_Name (R)
2608 and then Is_Type (Entity (R))
2609 then
2610 Find_Type (R);
2611 Check_Fully_Declared (Entity (R), R);
2613 elsif Ada_Version >= Ada_2012
2614 and then Has_Compatible_Type (R, Etype (L))
2615 then
2616 if Nkind (N) = N_In then
2617 Rewrite (N,
2618 Make_Op_Eq (Loc,
2619 Left_Opnd => L,
2620 Right_Opnd => R));
2621 else
2622 Rewrite (N,
2623 Make_Op_Ne (Loc,
2624 Left_Opnd => L,
2625 Right_Opnd => R));
2626 end if;
2628 Analyze (N);
2629 return;
2631 else
2632 -- In all versions of the language, if we reach this point there
2633 -- is a previous error that will be diagnosed below.
2635 Find_Type (R);
2636 end if;
2637 end if;
2639 -- Compatibility between expression and subtype mark or range is
2640 -- checked during resolution. The result of the operation is Boolean
2641 -- in any case.
2643 Set_Etype (N, Standard_Boolean);
2645 if Comes_From_Source (N)
2646 and then Present (Right_Opnd (N))
2647 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2648 then
2649 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2650 end if;
2651 end Analyze_Membership_Op;
2653 -----------------
2654 -- Analyze_Mod --
2655 -----------------
2657 procedure Analyze_Mod (N : Node_Id) is
2658 begin
2659 -- A special warning check, if we have an expression of the form:
2660 -- expr mod 2 * literal
2661 -- where literal is 64 or less, then probably what was meant was
2662 -- expr mod 2 ** literal
2663 -- so issue an appropriate warning.
2665 if Warn_On_Suspicious_Modulus_Value
2666 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
2667 and then Intval (Right_Opnd (N)) = Uint_2
2668 and then Nkind (Parent (N)) = N_Op_Multiply
2669 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
2670 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
2671 then
2672 Error_Msg_N
2673 ("suspicious MOD value, was '*'* intended'??", Parent (N));
2674 end if;
2676 -- Remaining processing is same as for other arithmetic operators
2678 Analyze_Arithmetic_Op (N);
2679 end Analyze_Mod;
2681 ----------------------
2682 -- Analyze_Negation --
2683 ----------------------
2685 procedure Analyze_Negation (N : Node_Id) is
2686 R : constant Node_Id := Right_Opnd (N);
2687 Op_Id : Entity_Id := Entity (N);
2689 begin
2690 Set_Etype (N, Any_Type);
2691 Candidate_Type := Empty;
2693 Analyze_Expression (R);
2695 if Present (Op_Id) then
2696 if Ekind (Op_Id) = E_Operator then
2697 Find_Negation_Types (R, Op_Id, N);
2698 else
2699 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2700 end if;
2702 else
2703 Op_Id := Get_Name_Entity_Id (Chars (N));
2704 while Present (Op_Id) loop
2705 if Ekind (Op_Id) = E_Operator then
2706 Find_Negation_Types (R, Op_Id, N);
2707 else
2708 Analyze_User_Defined_Unary_Op (N, Op_Id);
2709 end if;
2711 Op_Id := Homonym (Op_Id);
2712 end loop;
2713 end if;
2715 Operator_Check (N);
2716 end Analyze_Negation;
2718 ------------------
2719 -- Analyze_Null --
2720 ------------------
2722 procedure Analyze_Null (N : Node_Id) is
2723 begin
2724 Check_SPARK_Restriction ("null is not allowed", N);
2726 Set_Etype (N, Any_Access);
2727 end Analyze_Null;
2729 ----------------------
2730 -- Analyze_One_Call --
2731 ----------------------
2733 procedure Analyze_One_Call
2734 (N : Node_Id;
2735 Nam : Entity_Id;
2736 Report : Boolean;
2737 Success : out Boolean;
2738 Skip_First : Boolean := False)
2740 Actuals : constant List_Id := Parameter_Associations (N);
2741 Prev_T : constant Entity_Id := Etype (N);
2743 Must_Skip : constant Boolean := Skip_First
2744 or else Nkind (Original_Node (N)) = N_Selected_Component
2745 or else
2746 (Nkind (Original_Node (N)) = N_Indexed_Component
2747 and then Nkind (Prefix (Original_Node (N)))
2748 = N_Selected_Component);
2749 -- The first formal must be omitted from the match when trying to find
2750 -- a primitive operation that is a possible interpretation, and also
2751 -- after the call has been rewritten, because the corresponding actual
2752 -- is already known to be compatible, and because this may be an
2753 -- indexing of a call with default parameters.
2755 Formal : Entity_Id;
2756 Actual : Node_Id;
2757 Is_Indexed : Boolean := False;
2758 Is_Indirect : Boolean := False;
2759 Subp_Type : constant Entity_Id := Etype (Nam);
2760 Norm_OK : Boolean;
2762 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2763 -- There may be a user-defined operator that hides the current
2764 -- interpretation. We must check for this independently of the
2765 -- analysis of the call with the user-defined operation, because
2766 -- the parameter names may be wrong and yet the hiding takes place.
2767 -- This fixes a problem with ACATS test B34014O.
2769 -- When the type Address is a visible integer type, and the DEC
2770 -- system extension is visible, the predefined operator may be
2771 -- hidden as well, by one of the address operations in auxdec.
2772 -- Finally, The abstract operations on address do not hide the
2773 -- predefined operator (this is the purpose of making them abstract).
2775 procedure Indicate_Name_And_Type;
2776 -- If candidate interpretation matches, indicate name and type of
2777 -- result on call node.
2779 ----------------------------
2780 -- Indicate_Name_And_Type --
2781 ----------------------------
2783 procedure Indicate_Name_And_Type is
2784 begin
2785 Add_One_Interp (N, Nam, Etype (Nam));
2786 Check_Implicit_Dereference (N, Etype (Nam));
2787 Success := True;
2789 -- If the prefix of the call is a name, indicate the entity
2790 -- being called. If it is not a name, it is an expression that
2791 -- denotes an access to subprogram or else an entry or family. In
2792 -- the latter case, the name is a selected component, and the entity
2793 -- being called is noted on the selector.
2795 if not Is_Type (Nam) then
2796 if Is_Entity_Name (Name (N)) then
2797 Set_Entity (Name (N), Nam);
2799 elsif Nkind (Name (N)) = N_Selected_Component then
2800 Set_Entity (Selector_Name (Name (N)), Nam);
2801 end if;
2802 end if;
2804 if Debug_Flag_E and not Report then
2805 Write_Str (" Overloaded call ");
2806 Write_Int (Int (N));
2807 Write_Str (" compatible with ");
2808 Write_Int (Int (Nam));
2809 Write_Eol;
2810 end if;
2811 end Indicate_Name_And_Type;
2813 ------------------------
2814 -- Operator_Hidden_By --
2815 ------------------------
2817 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2818 Act1 : constant Node_Id := First_Actual (N);
2819 Act2 : constant Node_Id := Next_Actual (Act1);
2820 Form1 : constant Entity_Id := First_Formal (Fun);
2821 Form2 : constant Entity_Id := Next_Formal (Form1);
2823 begin
2824 if Ekind (Fun) /= E_Function
2825 or else Is_Abstract_Subprogram (Fun)
2826 then
2827 return False;
2829 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2830 return False;
2832 elsif Present (Form2) then
2834 No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
2835 then
2836 return False;
2837 end if;
2839 elsif Present (Act2) then
2840 return False;
2841 end if;
2843 -- Now we know that the arity of the operator matches the function,
2844 -- and the function call is a valid interpretation. The function
2845 -- hides the operator if it has the right signature, or if one of
2846 -- its operands is a non-abstract operation on Address when this is
2847 -- a visible integer type.
2849 return Hides_Op (Fun, Nam)
2850 or else Is_Descendent_Of_Address (Etype (Form1))
2851 or else
2852 (Present (Form2)
2853 and then Is_Descendent_Of_Address (Etype (Form2)));
2854 end Operator_Hidden_By;
2856 -- Start of processing for Analyze_One_Call
2858 begin
2859 Success := False;
2861 -- If the subprogram has no formals or if all the formals have defaults,
2862 -- and the return type is an array type, the node may denote an indexing
2863 -- of the result of a parameterless call. In Ada 2005, the subprogram
2864 -- may have one non-defaulted formal, and the call may have been written
2865 -- in prefix notation, so that the rebuilt parameter list has more than
2866 -- one actual.
2868 if not Is_Overloadable (Nam)
2869 and then Ekind (Nam) /= E_Subprogram_Type
2870 and then Ekind (Nam) /= E_Entry_Family
2871 then
2872 return;
2873 end if;
2875 -- An indexing requires at least one actual
2877 if not Is_Empty_List (Actuals)
2878 and then
2879 (Needs_No_Actuals (Nam)
2880 or else
2881 (Needs_One_Actual (Nam)
2882 and then Present (Next_Actual (First (Actuals)))))
2883 then
2884 if Is_Array_Type (Subp_Type) then
2885 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
2887 elsif Is_Access_Type (Subp_Type)
2888 and then Is_Array_Type (Designated_Type (Subp_Type))
2889 then
2890 Is_Indexed :=
2891 Try_Indexed_Call
2892 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
2894 -- The prefix can also be a parameterless function that returns an
2895 -- access to subprogram, in which case this is an indirect call.
2896 -- If this succeeds, an explicit dereference is added later on,
2897 -- in Analyze_Call or Resolve_Call.
2899 elsif Is_Access_Type (Subp_Type)
2900 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
2901 then
2902 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
2903 end if;
2905 end if;
2907 -- If the call has been transformed into a slice, it is of the form
2908 -- F (Subtype) where F is parameterless. The node has been rewritten in
2909 -- Try_Indexed_Call and there is nothing else to do.
2911 if Is_Indexed
2912 and then Nkind (N) = N_Slice
2913 then
2914 return;
2915 end if;
2917 Normalize_Actuals
2918 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
2920 if not Norm_OK then
2922 -- If an indirect call is a possible interpretation, indicate
2923 -- success to the caller.
2925 if Is_Indirect then
2926 Success := True;
2927 return;
2929 -- Mismatch in number or names of parameters
2931 elsif Debug_Flag_E then
2932 Write_Str (" normalization fails in call ");
2933 Write_Int (Int (N));
2934 Write_Str (" with subprogram ");
2935 Write_Int (Int (Nam));
2936 Write_Eol;
2937 end if;
2939 -- If the context expects a function call, discard any interpretation
2940 -- that is a procedure. If the node is not overloaded, leave as is for
2941 -- better error reporting when type mismatch is found.
2943 elsif Nkind (N) = N_Function_Call
2944 and then Is_Overloaded (Name (N))
2945 and then Ekind (Nam) = E_Procedure
2946 then
2947 return;
2949 -- Ditto for function calls in a procedure context
2951 elsif Nkind (N) = N_Procedure_Call_Statement
2952 and then Is_Overloaded (Name (N))
2953 and then Etype (Nam) /= Standard_Void_Type
2954 then
2955 return;
2957 elsif No (Actuals) then
2959 -- If Normalize succeeds, then there are default parameters for
2960 -- all formals.
2962 Indicate_Name_And_Type;
2964 elsif Ekind (Nam) = E_Operator then
2965 if Nkind (N) = N_Procedure_Call_Statement then
2966 return;
2967 end if;
2969 -- This can occur when the prefix of the call is an operator
2970 -- name or an expanded name whose selector is an operator name.
2972 Analyze_Operator_Call (N, Nam);
2974 if Etype (N) /= Prev_T then
2976 -- Check that operator is not hidden by a function interpretation
2978 if Is_Overloaded (Name (N)) then
2979 declare
2980 I : Interp_Index;
2981 It : Interp;
2983 begin
2984 Get_First_Interp (Name (N), I, It);
2985 while Present (It.Nam) loop
2986 if Operator_Hidden_By (It.Nam) then
2987 Set_Etype (N, Prev_T);
2988 return;
2989 end if;
2991 Get_Next_Interp (I, It);
2992 end loop;
2993 end;
2994 end if;
2996 -- If operator matches formals, record its name on the call.
2997 -- If the operator is overloaded, Resolve will select the
2998 -- correct one from the list of interpretations. The call
2999 -- node itself carries the first candidate.
3001 Set_Entity (Name (N), Nam);
3002 Success := True;
3004 elsif Report and then Etype (N) = Any_Type then
3005 Error_Msg_N ("incompatible arguments for operator", N);
3006 end if;
3008 else
3009 -- Normalize_Actuals has chained the named associations in the
3010 -- correct order of the formals.
3012 Actual := First_Actual (N);
3013 Formal := First_Formal (Nam);
3015 -- If we are analyzing a call rewritten from object notation, skip
3016 -- first actual, which may be rewritten later as an explicit
3017 -- dereference.
3019 if Must_Skip then
3020 Next_Actual (Actual);
3021 Next_Formal (Formal);
3022 end if;
3024 while Present (Actual) and then Present (Formal) loop
3025 if Nkind (Parent (Actual)) /= N_Parameter_Association
3026 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3027 then
3028 -- The actual can be compatible with the formal, but we must
3029 -- also check that the context is not an address type that is
3030 -- visibly an integer type, as is the case in VMS_64. In this
3031 -- case the use of literals is illegal, except in the body of
3032 -- descendents of system, where arithmetic operations on
3033 -- address are of course used.
3035 if Has_Compatible_Type (Actual, Etype (Formal))
3036 and then
3037 (Etype (Actual) /= Universal_Integer
3038 or else not Is_Descendent_Of_Address (Etype (Formal))
3039 or else
3040 Is_Predefined_File_Name
3041 (Unit_File_Name (Get_Source_Unit (N))))
3042 then
3043 Next_Actual (Actual);
3044 Next_Formal (Formal);
3046 else
3047 if Debug_Flag_E then
3048 Write_Str (" type checking fails in call ");
3049 Write_Int (Int (N));
3050 Write_Str (" with formal ");
3051 Write_Int (Int (Formal));
3052 Write_Str (" in subprogram ");
3053 Write_Int (Int (Nam));
3054 Write_Eol;
3055 end if;
3057 if Report and not Is_Indexed and not Is_Indirect then
3059 -- Ada 2005 (AI-251): Complete the error notification
3060 -- to help new Ada 2005 users.
3062 if Is_Class_Wide_Type (Etype (Formal))
3063 and then Is_Interface (Etype (Etype (Formal)))
3064 and then not Interface_Present_In_Ancestor
3065 (Typ => Etype (Actual),
3066 Iface => Etype (Etype (Formal)))
3067 then
3068 Error_Msg_NE
3069 ("(Ada 2005) does not implement interface }",
3070 Actual, Etype (Etype (Formal)));
3071 end if;
3073 Wrong_Type (Actual, Etype (Formal));
3075 if Nkind (Actual) = N_Op_Eq
3076 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3077 then
3078 Formal := First_Formal (Nam);
3079 while Present (Formal) loop
3080 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3081 Error_Msg_N -- CODEFIX
3082 ("possible misspelling of `='>`!", Actual);
3083 exit;
3084 end if;
3086 Next_Formal (Formal);
3087 end loop;
3088 end if;
3090 if All_Errors_Mode then
3091 Error_Msg_Sloc := Sloc (Nam);
3093 if Etype (Formal) = Any_Type then
3094 Error_Msg_N
3095 ("there is no legal actual parameter", Actual);
3096 end if;
3098 if Is_Overloadable (Nam)
3099 and then Present (Alias (Nam))
3100 and then not Comes_From_Source (Nam)
3101 then
3102 Error_Msg_NE
3103 ("\\ =='> in call to inherited operation & #!",
3104 Actual, Nam);
3106 elsif Ekind (Nam) = E_Subprogram_Type then
3107 declare
3108 Access_To_Subprogram_Typ :
3109 constant Entity_Id :=
3110 Defining_Identifier
3111 (Associated_Node_For_Itype (Nam));
3112 begin
3113 Error_Msg_NE (
3114 "\\ =='> in call to dereference of &#!",
3115 Actual, Access_To_Subprogram_Typ);
3116 end;
3118 else
3119 Error_Msg_NE
3120 ("\\ =='> in call to &#!", Actual, Nam);
3122 end if;
3123 end if;
3124 end if;
3126 return;
3127 end if;
3129 else
3130 -- Normalize_Actuals has verified that a default value exists
3131 -- for this formal. Current actual names a subsequent formal.
3133 Next_Formal (Formal);
3134 end if;
3135 end loop;
3137 -- On exit, all actuals match
3139 Indicate_Name_And_Type;
3140 end if;
3141 end Analyze_One_Call;
3143 ---------------------------
3144 -- Analyze_Operator_Call --
3145 ---------------------------
3147 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3148 Op_Name : constant Name_Id := Chars (Op_Id);
3149 Act1 : constant Node_Id := First_Actual (N);
3150 Act2 : constant Node_Id := Next_Actual (Act1);
3152 begin
3153 -- Binary operator case
3155 if Present (Act2) then
3157 -- If more than two operands, then not binary operator after all
3159 if Present (Next_Actual (Act2)) then
3160 return;
3161 end if;
3163 -- Otherwise action depends on operator
3165 case Op_Name is
3166 when Name_Op_Add |
3167 Name_Op_Subtract |
3168 Name_Op_Multiply |
3169 Name_Op_Divide |
3170 Name_Op_Mod |
3171 Name_Op_Rem |
3172 Name_Op_Expon =>
3173 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3175 when Name_Op_And |
3176 Name_Op_Or |
3177 Name_Op_Xor =>
3178 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3180 when Name_Op_Lt |
3181 Name_Op_Le |
3182 Name_Op_Gt |
3183 Name_Op_Ge =>
3184 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3186 when Name_Op_Eq |
3187 Name_Op_Ne =>
3188 Find_Equality_Types (Act1, Act2, Op_Id, N);
3190 when Name_Op_Concat =>
3191 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3193 -- Is this when others, or should it be an abort???
3195 when others =>
3196 null;
3197 end case;
3199 -- Unary operator case
3201 else
3202 case Op_Name is
3203 when Name_Op_Subtract |
3204 Name_Op_Add |
3205 Name_Op_Abs =>
3206 Find_Unary_Types (Act1, Op_Id, N);
3208 when Name_Op_Not =>
3209 Find_Negation_Types (Act1, Op_Id, N);
3211 -- Is this when others correct, or should it be an abort???
3213 when others =>
3214 null;
3215 end case;
3216 end if;
3217 end Analyze_Operator_Call;
3219 -------------------------------------------
3220 -- Analyze_Overloaded_Selected_Component --
3221 -------------------------------------------
3223 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3224 Nam : constant Node_Id := Prefix (N);
3225 Sel : constant Node_Id := Selector_Name (N);
3226 Comp : Entity_Id;
3227 I : Interp_Index;
3228 It : Interp;
3229 T : Entity_Id;
3231 begin
3232 Set_Etype (Sel, Any_Type);
3234 Get_First_Interp (Nam, I, It);
3235 while Present (It.Typ) loop
3236 if Is_Access_Type (It.Typ) then
3237 T := Designated_Type (It.Typ);
3238 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3239 else
3240 T := It.Typ;
3241 end if;
3243 -- Locate the component. For a private prefix the selector can denote
3244 -- a discriminant.
3246 if Is_Record_Type (T) or else Is_Private_Type (T) then
3248 -- If the prefix is a class-wide type, the visible components are
3249 -- those of the base type.
3251 if Is_Class_Wide_Type (T) then
3252 T := Etype (T);
3253 end if;
3255 Comp := First_Entity (T);
3256 while Present (Comp) loop
3257 if Chars (Comp) = Chars (Sel)
3258 and then Is_Visible_Component (Comp)
3259 then
3261 -- AI05-105: if the context is an object renaming with
3262 -- an anonymous access type, the expected type of the
3263 -- object must be anonymous. This is a name resolution rule.
3265 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3266 or else No (Access_Definition (Parent (N)))
3267 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3268 or else
3269 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3270 then
3271 Set_Entity (Sel, Comp);
3272 Set_Etype (Sel, Etype (Comp));
3273 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3274 Check_Implicit_Dereference (N, Etype (Comp));
3276 -- This also specifies a candidate to resolve the name.
3277 -- Further overloading will be resolved from context.
3278 -- The selector name itself does not carry overloading
3279 -- information.
3281 Set_Etype (Nam, It.Typ);
3283 else
3284 -- Named access type in the context of a renaming
3285 -- declaration with an access definition. Remove
3286 -- inapplicable candidate.
3288 Remove_Interp (I);
3289 end if;
3290 end if;
3292 Next_Entity (Comp);
3293 end loop;
3295 elsif Is_Concurrent_Type (T) then
3296 Comp := First_Entity (T);
3297 while Present (Comp)
3298 and then Comp /= First_Private_Entity (T)
3299 loop
3300 if Chars (Comp) = Chars (Sel) then
3301 if Is_Overloadable (Comp) then
3302 Add_One_Interp (Sel, Comp, Etype (Comp));
3303 else
3304 Set_Entity_With_Style_Check (Sel, Comp);
3305 Generate_Reference (Comp, Sel);
3306 end if;
3308 Set_Etype (Sel, Etype (Comp));
3309 Set_Etype (N, Etype (Comp));
3310 Set_Etype (Nam, It.Typ);
3312 -- For access type case, introduce explicit dereference for
3313 -- more uniform treatment of entry calls. Do this only once
3314 -- if several interpretations yield an access type.
3316 if Is_Access_Type (Etype (Nam))
3317 and then Nkind (Nam) /= N_Explicit_Dereference
3318 then
3319 Insert_Explicit_Dereference (Nam);
3320 Error_Msg_NW
3321 (Warn_On_Dereference, "?implicit dereference", N);
3322 end if;
3323 end if;
3325 Next_Entity (Comp);
3326 end loop;
3328 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3329 end if;
3331 Get_Next_Interp (I, It);
3332 end loop;
3334 if Etype (N) = Any_Type
3335 and then not Try_Object_Operation (N)
3336 then
3337 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3338 Set_Entity (Sel, Any_Id);
3339 Set_Etype (Sel, Any_Type);
3340 end if;
3341 end Analyze_Overloaded_Selected_Component;
3343 ----------------------------------
3344 -- Analyze_Qualified_Expression --
3345 ----------------------------------
3347 procedure Analyze_Qualified_Expression (N : Node_Id) is
3348 Mark : constant Entity_Id := Subtype_Mark (N);
3349 Expr : constant Node_Id := Expression (N);
3350 I : Interp_Index;
3351 It : Interp;
3352 T : Entity_Id;
3354 begin
3355 Analyze_Expression (Expr);
3357 Set_Etype (N, Any_Type);
3358 Find_Type (Mark);
3359 T := Entity (Mark);
3360 Set_Etype (N, T);
3362 if T = Any_Type then
3363 return;
3364 end if;
3366 Check_Fully_Declared (T, N);
3368 -- If expected type is class-wide, check for exact match before
3369 -- expansion, because if the expression is a dispatching call it
3370 -- may be rewritten as explicit dereference with class-wide result.
3371 -- If expression is overloaded, retain only interpretations that
3372 -- will yield exact matches.
3374 if Is_Class_Wide_Type (T) then
3375 if not Is_Overloaded (Expr) then
3376 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3377 if Nkind (Expr) = N_Aggregate then
3378 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3379 else
3380 Wrong_Type (Expr, T);
3381 end if;
3382 end if;
3384 else
3385 Get_First_Interp (Expr, I, It);
3387 while Present (It.Nam) loop
3388 if Base_Type (It.Typ) /= Base_Type (T) then
3389 Remove_Interp (I);
3390 end if;
3392 Get_Next_Interp (I, It);
3393 end loop;
3394 end if;
3395 end if;
3397 Set_Etype (N, T);
3398 end Analyze_Qualified_Expression;
3400 -----------------------------------
3401 -- Analyze_Quantified_Expression --
3402 -----------------------------------
3404 procedure Analyze_Quantified_Expression (N : Node_Id) is
3405 QE_Scop : Entity_Id;
3407 begin
3408 Check_SPARK_Restriction ("quantified expression is not allowed", N);
3410 -- Create a scope to emulate the loop-like behavior of the quantified
3411 -- expression. The scope is needed to provide proper visibility of the
3412 -- loop variable.
3414 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
3415 Set_Etype (QE_Scop, Standard_Void_Type);
3416 Set_Scope (QE_Scop, Current_Scope);
3417 Set_Parent (QE_Scop, N);
3419 Push_Scope (QE_Scop);
3421 -- All constituents are preanalyzed and resolved to avoid untimely
3422 -- generation of various temporaries and types. Full analysis and
3423 -- expansion is carried out when the quantified expression is
3424 -- transformed into an expression with actions.
3426 if Present (Iterator_Specification (N)) then
3427 Preanalyze (Iterator_Specification (N));
3428 else
3429 Preanalyze (Loop_Parameter_Specification (N));
3430 end if;
3432 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
3434 End_Scope;
3436 Set_Etype (N, Standard_Boolean);
3437 end Analyze_Quantified_Expression;
3439 -------------------
3440 -- Analyze_Range --
3441 -------------------
3443 procedure Analyze_Range (N : Node_Id) is
3444 L : constant Node_Id := Low_Bound (N);
3445 H : constant Node_Id := High_Bound (N);
3446 I1, I2 : Interp_Index;
3447 It1, It2 : Interp;
3449 procedure Check_Common_Type (T1, T2 : Entity_Id);
3450 -- Verify the compatibility of two types, and choose the
3451 -- non universal one if the other is universal.
3453 procedure Check_High_Bound (T : Entity_Id);
3454 -- Test one interpretation of the low bound against all those
3455 -- of the high bound.
3457 procedure Check_Universal_Expression (N : Node_Id);
3458 -- In Ada 83, reject bounds of a universal range that are not literals
3459 -- or entity names.
3461 -----------------------
3462 -- Check_Common_Type --
3463 -----------------------
3465 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3466 begin
3467 if Covers (T1 => T1, T2 => T2)
3468 or else
3469 Covers (T1 => T2, T2 => T1)
3470 then
3471 if T1 = Universal_Integer
3472 or else T1 = Universal_Real
3473 or else T1 = Any_Character
3474 then
3475 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3477 elsif T1 = T2 then
3478 Add_One_Interp (N, T1, T1);
3480 else
3481 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3482 end if;
3483 end if;
3484 end Check_Common_Type;
3486 ----------------------
3487 -- Check_High_Bound --
3488 ----------------------
3490 procedure Check_High_Bound (T : Entity_Id) is
3491 begin
3492 if not Is_Overloaded (H) then
3493 Check_Common_Type (T, Etype (H));
3494 else
3495 Get_First_Interp (H, I2, It2);
3496 while Present (It2.Typ) loop
3497 Check_Common_Type (T, It2.Typ);
3498 Get_Next_Interp (I2, It2);
3499 end loop;
3500 end if;
3501 end Check_High_Bound;
3503 -----------------------------
3504 -- Is_Universal_Expression --
3505 -----------------------------
3507 procedure Check_Universal_Expression (N : Node_Id) is
3508 begin
3509 if Etype (N) = Universal_Integer
3510 and then Nkind (N) /= N_Integer_Literal
3511 and then not Is_Entity_Name (N)
3512 and then Nkind (N) /= N_Attribute_Reference
3513 then
3514 Error_Msg_N ("illegal bound in discrete range", N);
3515 end if;
3516 end Check_Universal_Expression;
3518 -- Start of processing for Analyze_Range
3520 begin
3521 Set_Etype (N, Any_Type);
3522 Analyze_Expression (L);
3523 Analyze_Expression (H);
3525 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3526 return;
3528 else
3529 if not Is_Overloaded (L) then
3530 Check_High_Bound (Etype (L));
3531 else
3532 Get_First_Interp (L, I1, It1);
3533 while Present (It1.Typ) loop
3534 Check_High_Bound (It1.Typ);
3535 Get_Next_Interp (I1, It1);
3536 end loop;
3537 end if;
3539 -- If result is Any_Type, then we did not find a compatible pair
3541 if Etype (N) = Any_Type then
3542 Error_Msg_N ("incompatible types in range ", N);
3543 end if;
3544 end if;
3546 if Ada_Version = Ada_83
3547 and then
3548 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3549 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3550 then
3551 Check_Universal_Expression (L);
3552 Check_Universal_Expression (H);
3553 end if;
3554 end Analyze_Range;
3556 -----------------------
3557 -- Analyze_Reference --
3558 -----------------------
3560 procedure Analyze_Reference (N : Node_Id) is
3561 P : constant Node_Id := Prefix (N);
3562 E : Entity_Id;
3563 T : Entity_Id;
3564 Acc_Type : Entity_Id;
3566 begin
3567 Analyze (P);
3569 -- An interesting error check, if we take the 'Reference of an object
3570 -- for which a pragma Atomic or Volatile has been given, and the type
3571 -- of the object is not Atomic or Volatile, then we are in trouble. The
3572 -- problem is that no trace of the atomic/volatile status will remain
3573 -- for the backend to respect when it deals with the resulting pointer,
3574 -- since the pointer type will not be marked atomic (it is a pointer to
3575 -- the base type of the object).
3577 -- It is not clear if that can ever occur, but in case it does, we will
3578 -- generate an error message. Not clear if this message can ever be
3579 -- generated, and pretty clear that it represents a bug if it is, still
3580 -- seems worth checking, except in CodePeer mode where we do not really
3581 -- care and don't want to bother the user.
3583 T := Etype (P);
3585 if Is_Entity_Name (P)
3586 and then Is_Object_Reference (P)
3587 and then not CodePeer_Mode
3588 then
3589 E := Entity (P);
3590 T := Etype (P);
3592 if (Has_Atomic_Components (E)
3593 and then not Has_Atomic_Components (T))
3594 or else
3595 (Has_Volatile_Components (E)
3596 and then not Has_Volatile_Components (T))
3597 or else (Is_Atomic (E) and then not Is_Atomic (T))
3598 or else (Is_Volatile (E) and then not Is_Volatile (T))
3599 then
3600 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3601 end if;
3602 end if;
3604 -- Carry on with normal processing
3606 Acc_Type := Create_Itype (E_Allocator_Type, N);
3607 Set_Etype (Acc_Type, Acc_Type);
3608 Set_Directly_Designated_Type (Acc_Type, Etype (P));
3609 Set_Etype (N, Acc_Type);
3610 end Analyze_Reference;
3612 --------------------------------
3613 -- Analyze_Selected_Component --
3614 --------------------------------
3616 -- Prefix is a record type or a task or protected type. In the latter case,
3617 -- the selector must denote a visible entry.
3619 procedure Analyze_Selected_Component (N : Node_Id) is
3620 Name : constant Node_Id := Prefix (N);
3621 Sel : constant Node_Id := Selector_Name (N);
3622 Act_Decl : Node_Id;
3623 Comp : Entity_Id;
3624 Has_Candidate : Boolean := False;
3625 In_Scope : Boolean;
3626 Parent_N : Node_Id;
3627 Pent : Entity_Id := Empty;
3628 Prefix_Type : Entity_Id;
3630 Type_To_Use : Entity_Id;
3631 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
3632 -- a class-wide type, we use its root type, whose components are
3633 -- present in the class-wide type.
3635 Is_Single_Concurrent_Object : Boolean;
3636 -- Set True if the prefix is a single task or a single protected object
3638 procedure Find_Component_In_Instance (Rec : Entity_Id);
3639 -- In an instance, a component of a private extension may not be visible
3640 -- while it was visible in the generic. Search candidate scope for a
3641 -- component with the proper identifier. This is only done if all other
3642 -- searches have failed. When the match is found (it always will be),
3643 -- the Etype of both N and Sel are set from this component, and the
3644 -- entity of Sel is set to reference this component.
3646 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3647 -- It is known that the parent of N denotes a subprogram call. Comp
3648 -- is an overloadable component of the concurrent type of the prefix.
3649 -- Determine whether all formals of the parent of N and Comp are mode
3650 -- conformant. If the parent node is not analyzed yet it may be an
3651 -- indexed component rather than a function call.
3653 --------------------------------
3654 -- Find_Component_In_Instance --
3655 --------------------------------
3657 procedure Find_Component_In_Instance (Rec : Entity_Id) is
3658 Comp : Entity_Id;
3660 begin
3661 Comp := First_Component (Rec);
3662 while Present (Comp) loop
3663 if Chars (Comp) = Chars (Sel) then
3664 Set_Entity_With_Style_Check (Sel, Comp);
3665 Set_Etype (Sel, Etype (Comp));
3666 Set_Etype (N, Etype (Comp));
3667 return;
3668 end if;
3670 Next_Component (Comp);
3671 end loop;
3673 -- This must succeed because code was legal in the generic
3675 raise Program_Error;
3676 end Find_Component_In_Instance;
3678 ------------------------------
3679 -- Has_Mode_Conformant_Spec --
3680 ------------------------------
3682 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3683 Comp_Param : Entity_Id;
3684 Param : Node_Id;
3685 Param_Typ : Entity_Id;
3687 begin
3688 Comp_Param := First_Formal (Comp);
3690 if Nkind (Parent (N)) = N_Indexed_Component then
3691 Param := First (Expressions (Parent (N)));
3692 else
3693 Param := First (Parameter_Associations (Parent (N)));
3694 end if;
3696 while Present (Comp_Param)
3697 and then Present (Param)
3698 loop
3699 Param_Typ := Find_Parameter_Type (Param);
3701 if Present (Param_Typ)
3702 and then
3703 not Conforming_Types
3704 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
3705 then
3706 return False;
3707 end if;
3709 Next_Formal (Comp_Param);
3710 Next (Param);
3711 end loop;
3713 -- One of the specs has additional formals
3715 if Present (Comp_Param) or else Present (Param) then
3716 return False;
3717 end if;
3719 return True;
3720 end Has_Mode_Conformant_Spec;
3722 -- Start of processing for Analyze_Selected_Component
3724 begin
3725 Set_Etype (N, Any_Type);
3727 if Is_Overloaded (Name) then
3728 Analyze_Overloaded_Selected_Component (N);
3729 return;
3731 elsif Etype (Name) = Any_Type then
3732 Set_Entity (Sel, Any_Id);
3733 Set_Etype (Sel, Any_Type);
3734 return;
3736 else
3737 Prefix_Type := Etype (Name);
3738 end if;
3740 if Is_Access_Type (Prefix_Type) then
3742 -- A RACW object can never be used as prefix of a selected component
3743 -- since that means it is dereferenced without being a controlling
3744 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
3745 -- reporting an error, we must check whether this is actually a
3746 -- dispatching call in prefix form.
3748 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
3749 and then Comes_From_Source (N)
3750 then
3751 if Try_Object_Operation (N) then
3752 return;
3753 else
3754 Error_Msg_N
3755 ("invalid dereference of a remote access-to-class-wide value",
3757 end if;
3759 -- Normal case of selected component applied to access type
3761 else
3762 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3764 if Is_Entity_Name (Name) then
3765 Pent := Entity (Name);
3766 elsif Nkind (Name) = N_Selected_Component
3767 and then Is_Entity_Name (Selector_Name (Name))
3768 then
3769 Pent := Entity (Selector_Name (Name));
3770 end if;
3772 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
3773 end if;
3775 -- If we have an explicit dereference of a remote access-to-class-wide
3776 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
3777 -- have to check for the case of a prefix that is a controlling operand
3778 -- of a prefixed dispatching call, as the dereference is legal in that
3779 -- case. Normally this condition is checked in Validate_Remote_Access_
3780 -- To_Class_Wide_Type, but we have to defer the checking for selected
3781 -- component prefixes because of the prefixed dispatching call case.
3782 -- Note that implicit dereferences are checked for this just above.
3784 elsif Nkind (Name) = N_Explicit_Dereference
3785 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
3786 and then Comes_From_Source (N)
3787 then
3788 if Try_Object_Operation (N) then
3789 return;
3790 else
3791 Error_Msg_N
3792 ("invalid dereference of a remote access-to-class-wide value",
3794 end if;
3795 end if;
3797 -- (Ada 2005): if the prefix is the limited view of a type, and
3798 -- the context already includes the full view, use the full view
3799 -- in what follows, either to retrieve a component of to find
3800 -- a primitive operation. If the prefix is an explicit dereference,
3801 -- set the type of the prefix to reflect this transformation.
3802 -- If the non-limited view is itself an incomplete type, get the
3803 -- full view if available.
3805 if Is_Incomplete_Type (Prefix_Type)
3806 and then From_With_Type (Prefix_Type)
3807 and then Present (Non_Limited_View (Prefix_Type))
3808 then
3809 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
3811 if Nkind (N) = N_Explicit_Dereference then
3812 Set_Etype (Prefix (N), Prefix_Type);
3813 end if;
3815 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
3816 and then From_With_Type (Prefix_Type)
3817 and then Present (Non_Limited_View (Etype (Prefix_Type)))
3818 then
3819 Prefix_Type :=
3820 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
3822 if Nkind (N) = N_Explicit_Dereference then
3823 Set_Etype (Prefix (N), Prefix_Type);
3824 end if;
3825 end if;
3827 if Ekind (Prefix_Type) = E_Private_Subtype then
3828 Prefix_Type := Base_Type (Prefix_Type);
3829 end if;
3831 Type_To_Use := Prefix_Type;
3833 -- For class-wide types, use the entity list of the root type. This
3834 -- indirection is specially important for private extensions because
3835 -- only the root type get switched (not the class-wide type).
3837 if Is_Class_Wide_Type (Prefix_Type) then
3838 Type_To_Use := Root_Type (Prefix_Type);
3839 end if;
3841 -- If the prefix is a single concurrent object, use its name in error
3842 -- messages, rather than that of its anonymous type.
3844 Is_Single_Concurrent_Object :=
3845 Is_Concurrent_Type (Prefix_Type)
3846 and then Is_Internal_Name (Chars (Prefix_Type))
3847 and then not Is_Derived_Type (Prefix_Type)
3848 and then Is_Entity_Name (Name);
3850 Comp := First_Entity (Type_To_Use);
3852 -- If the selector has an original discriminant, the node appears in
3853 -- an instance. Replace the discriminant with the corresponding one
3854 -- in the current discriminated type. For nested generics, this must
3855 -- be done transitively, so note the new original discriminant.
3857 if Nkind (Sel) = N_Identifier
3858 and then In_Instance
3859 and then Present (Original_Discriminant (Sel))
3860 then
3861 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
3863 -- Mark entity before rewriting, for completeness and because
3864 -- subsequent semantic checks might examine the original node.
3866 Set_Entity (Sel, Comp);
3867 Rewrite (Selector_Name (N),
3868 New_Occurrence_Of (Comp, Sloc (N)));
3869 Set_Original_Discriminant (Selector_Name (N), Comp);
3870 Set_Etype (N, Etype (Comp));
3871 Check_Implicit_Dereference (N, Etype (Comp));
3873 if Is_Access_Type (Etype (Name)) then
3874 Insert_Explicit_Dereference (Name);
3875 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3876 end if;
3878 elsif Is_Record_Type (Prefix_Type) then
3880 -- Find component with given name
3881 -- In an instance, if the node is known as a prefixed call, do
3882 -- not examine components whose visibility may be accidental.
3884 while Present (Comp) and then not Is_Prefixed_Call (N) loop
3885 if Chars (Comp) = Chars (Sel)
3886 and then Is_Visible_Component (Comp)
3887 then
3888 Set_Entity_With_Style_Check (Sel, Comp);
3889 Set_Etype (Sel, Etype (Comp));
3891 if Ekind (Comp) = E_Discriminant then
3892 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
3893 Error_Msg_N
3894 ("cannot reference discriminant of unchecked union",
3895 Sel);
3896 end if;
3898 if Is_Generic_Type (Prefix_Type)
3899 or else
3900 Is_Generic_Type (Root_Type (Prefix_Type))
3901 then
3902 Set_Original_Discriminant (Sel, Comp);
3903 end if;
3904 end if;
3906 -- Resolve the prefix early otherwise it is not possible to
3907 -- build the actual subtype of the component: it may need
3908 -- to duplicate this prefix and duplication is only allowed
3909 -- on fully resolved expressions.
3911 Resolve (Name);
3913 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
3914 -- subtypes in a package specification.
3915 -- Example:
3917 -- limited with Pkg;
3918 -- package Pkg is
3919 -- type Acc_Inc is access Pkg.T;
3920 -- X : Acc_Inc;
3921 -- N : Natural := X.all.Comp; -- ERROR, limited view
3922 -- end Pkg; -- Comp is not visible
3924 if Nkind (Name) = N_Explicit_Dereference
3925 and then From_With_Type (Etype (Prefix (Name)))
3926 and then not Is_Potentially_Use_Visible (Etype (Name))
3927 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
3928 N_Package_Specification
3929 then
3930 Error_Msg_NE
3931 ("premature usage of incomplete}", Prefix (Name),
3932 Etype (Prefix (Name)));
3933 end if;
3935 -- We never need an actual subtype for the case of a selection
3936 -- for a indexed component of a non-packed array, since in
3937 -- this case gigi generates all the checks and can find the
3938 -- necessary bounds information.
3940 -- We also do not need an actual subtype for the case of a
3941 -- first, last, length, or range attribute applied to a
3942 -- non-packed array, since gigi can again get the bounds in
3943 -- these cases (gigi cannot handle the packed case, since it
3944 -- has the bounds of the packed array type, not the original
3945 -- bounds of the type). However, if the prefix is itself a
3946 -- selected component, as in a.b.c (i), gigi may regard a.b.c
3947 -- as a dynamic-sized temporary, so we do generate an actual
3948 -- subtype for this case.
3950 Parent_N := Parent (N);
3952 if not Is_Packed (Etype (Comp))
3953 and then
3954 ((Nkind (Parent_N) = N_Indexed_Component
3955 and then Nkind (Name) /= N_Selected_Component)
3956 or else
3957 (Nkind (Parent_N) = N_Attribute_Reference
3958 and then (Attribute_Name (Parent_N) = Name_First
3959 or else
3960 Attribute_Name (Parent_N) = Name_Last
3961 or else
3962 Attribute_Name (Parent_N) = Name_Length
3963 or else
3964 Attribute_Name (Parent_N) = Name_Range)))
3965 then
3966 Set_Etype (N, Etype (Comp));
3968 -- If full analysis is not enabled, we do not generate an
3969 -- actual subtype, because in the absence of expansion
3970 -- reference to a formal of a protected type, for example,
3971 -- will not be properly transformed, and will lead to
3972 -- out-of-scope references in gigi.
3974 -- In all other cases, we currently build an actual subtype.
3975 -- It seems likely that many of these cases can be avoided,
3976 -- but right now, the front end makes direct references to the
3977 -- bounds (e.g. in generating a length check), and if we do
3978 -- not make an actual subtype, we end up getting a direct
3979 -- reference to a discriminant, which will not do.
3981 elsif Full_Analysis then
3982 Act_Decl :=
3983 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
3984 Insert_Action (N, Act_Decl);
3986 if No (Act_Decl) then
3987 Set_Etype (N, Etype (Comp));
3989 else
3990 -- Component type depends on discriminants. Enter the
3991 -- main attributes of the subtype.
3993 declare
3994 Subt : constant Entity_Id :=
3995 Defining_Identifier (Act_Decl);
3997 begin
3998 Set_Etype (Subt, Base_Type (Etype (Comp)));
3999 Set_Ekind (Subt, Ekind (Etype (Comp)));
4000 Set_Etype (N, Subt);
4001 end;
4002 end if;
4004 -- If Full_Analysis not enabled, just set the Etype
4006 else
4007 Set_Etype (N, Etype (Comp));
4008 end if;
4010 Check_Implicit_Dereference (N, Etype (N));
4011 return;
4012 end if;
4014 -- If the prefix is a private extension, check only the visible
4015 -- components of the partial view. This must include the tag,
4016 -- which can appear in expanded code in a tag check.
4018 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4019 and then Chars (Selector_Name (N)) /= Name_uTag
4020 then
4021 exit when Comp = Last_Entity (Type_To_Use);
4022 end if;
4024 Next_Entity (Comp);
4025 end loop;
4027 -- Ada 2005 (AI-252): The selected component can be interpreted as
4028 -- a prefixed view of a subprogram. Depending on the context, this is
4029 -- either a name that can appear in a renaming declaration, or part
4030 -- of an enclosing call given in prefix form.
4032 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4033 -- selected component should resolve to a name.
4035 if Ada_Version >= Ada_2005
4036 and then Is_Tagged_Type (Prefix_Type)
4037 and then not Is_Concurrent_Type (Prefix_Type)
4038 then
4039 if Nkind (Parent (N)) = N_Generic_Association
4040 or else Nkind (Parent (N)) = N_Requeue_Statement
4041 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4042 then
4043 if Find_Primitive_Operation (N) then
4044 return;
4045 end if;
4047 elsif Try_Object_Operation (N) then
4048 return;
4049 end if;
4051 -- If the transformation fails, it will be necessary to redo the
4052 -- analysis with all errors enabled, to indicate candidate
4053 -- interpretations and reasons for each failure ???
4055 end if;
4057 elsif Is_Private_Type (Prefix_Type) then
4059 -- Allow access only to discriminants of the type. If the type has
4060 -- no full view, gigi uses the parent type for the components, so we
4061 -- do the same here.
4063 if No (Full_View (Prefix_Type)) then
4064 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4065 Comp := First_Entity (Type_To_Use);
4066 end if;
4068 while Present (Comp) loop
4069 if Chars (Comp) = Chars (Sel) then
4070 if Ekind (Comp) = E_Discriminant then
4071 Set_Entity_With_Style_Check (Sel, Comp);
4072 Generate_Reference (Comp, Sel);
4074 Set_Etype (Sel, Etype (Comp));
4075 Set_Etype (N, Etype (Comp));
4076 Check_Implicit_Dereference (N, Etype (N));
4078 if Is_Generic_Type (Prefix_Type)
4079 or else Is_Generic_Type (Root_Type (Prefix_Type))
4080 then
4081 Set_Original_Discriminant (Sel, Comp);
4082 end if;
4084 -- Before declaring an error, check whether this is tagged
4085 -- private type and a call to a primitive operation.
4087 elsif Ada_Version >= Ada_2005
4088 and then Is_Tagged_Type (Prefix_Type)
4089 and then Try_Object_Operation (N)
4090 then
4091 return;
4093 else
4094 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4095 Error_Msg_NE ("invisible selector& for }", N, Sel);
4096 Set_Entity (Sel, Any_Id);
4097 Set_Etype (N, Any_Type);
4098 end if;
4100 return;
4101 end if;
4103 Next_Entity (Comp);
4104 end loop;
4106 elsif Is_Concurrent_Type (Prefix_Type) then
4108 -- Find visible operation with given name. For a protected type,
4109 -- the possible candidates are discriminants, entries or protected
4110 -- procedures. For a task type, the set can only include entries or
4111 -- discriminants if the task type is not an enclosing scope. If it
4112 -- is an enclosing scope (e.g. in an inner task) then all entities
4113 -- are visible, but the prefix must denote the enclosing scope, i.e.
4114 -- can only be a direct name or an expanded name.
4116 Set_Etype (Sel, Any_Type);
4117 In_Scope := In_Open_Scopes (Prefix_Type);
4119 while Present (Comp) loop
4120 if Chars (Comp) = Chars (Sel) then
4121 if Is_Overloadable (Comp) then
4122 Add_One_Interp (Sel, Comp, Etype (Comp));
4124 -- If the prefix is tagged, the correct interpretation may
4125 -- lie in the primitive or class-wide operations of the
4126 -- type. Perform a simple conformance check to determine
4127 -- whether Try_Object_Operation should be invoked even if
4128 -- a visible entity is found.
4130 if Is_Tagged_Type (Prefix_Type)
4131 and then
4132 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4133 N_Function_Call,
4134 N_Indexed_Component)
4135 and then Has_Mode_Conformant_Spec (Comp)
4136 then
4137 Has_Candidate := True;
4138 end if;
4140 -- Note: a selected component may not denote a component of a
4141 -- protected type (4.1.3(7)).
4143 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4144 or else (In_Scope
4145 and then not Is_Protected_Type (Prefix_Type)
4146 and then Is_Entity_Name (Name))
4147 then
4148 Set_Entity_With_Style_Check (Sel, Comp);
4149 Generate_Reference (Comp, Sel);
4151 -- The selector is not overloadable, so we have a candidate
4152 -- interpretation.
4154 Has_Candidate := True;
4156 else
4157 goto Next_Comp;
4158 end if;
4160 Set_Etype (Sel, Etype (Comp));
4161 Set_Etype (N, Etype (Comp));
4163 if Ekind (Comp) = E_Discriminant then
4164 Set_Original_Discriminant (Sel, Comp);
4165 end if;
4167 -- For access type case, introduce explicit dereference for
4168 -- more uniform treatment of entry calls.
4170 if Is_Access_Type (Etype (Name)) then
4171 Insert_Explicit_Dereference (Name);
4172 Error_Msg_NW
4173 (Warn_On_Dereference, "?implicit dereference", N);
4174 end if;
4175 end if;
4177 <<Next_Comp>>
4178 Next_Entity (Comp);
4179 exit when not In_Scope
4180 and then
4181 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4182 end loop;
4184 -- If there is no visible entity with the given name or none of the
4185 -- visible entities are plausible interpretations, check whether
4186 -- there is some other primitive operation with that name.
4188 if Ada_Version >= Ada_2005
4189 and then Is_Tagged_Type (Prefix_Type)
4190 then
4191 if (Etype (N) = Any_Type
4192 or else not Has_Candidate)
4193 and then Try_Object_Operation (N)
4194 then
4195 return;
4197 -- If the context is not syntactically a procedure call, it
4198 -- may be a call to a primitive function declared outside of
4199 -- the synchronized type.
4201 -- If the context is a procedure call, there might still be
4202 -- an overloading between an entry and a primitive procedure
4203 -- declared outside of the synchronized type, called in prefix
4204 -- notation. This is harder to disambiguate because in one case
4205 -- the controlling formal is implicit ???
4207 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4208 and then Nkind (Parent (N)) /= N_Indexed_Component
4209 and then Try_Object_Operation (N)
4210 then
4211 return;
4212 end if;
4214 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4215 -- entry or procedure of a tagged concurrent type we must check
4216 -- if there are class-wide subprograms covering the primitive. If
4217 -- true then Try_Object_Operation reports the error.
4219 if Has_Candidate
4220 and then Is_Concurrent_Type (Prefix_Type)
4221 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4223 -- Duplicate the call. This is required to avoid problems with
4224 -- the tree transformations performed by Try_Object_Operation.
4225 -- Set properly the parent of the copied call, because it is
4226 -- about to be reanalyzed.
4228 then
4229 declare
4230 Par : constant Node_Id := New_Copy_Tree (Parent (N));
4232 begin
4233 Set_Parent (Par, Parent (Parent (N)));
4235 if Try_Object_Operation
4236 (Sinfo.Name (Par), CW_Test_Only => True)
4237 then
4238 return;
4239 end if;
4240 end;
4241 end if;
4242 end if;
4244 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4246 -- Case of a prefix of a protected type: selector might denote
4247 -- an invisible private component.
4249 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4250 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4251 Next_Entity (Comp);
4252 end loop;
4254 if Present (Comp) then
4255 if Is_Single_Concurrent_Object then
4256 Error_Msg_Node_2 := Entity (Name);
4257 Error_Msg_NE ("invisible selector& for &", N, Sel);
4259 else
4260 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4261 Error_Msg_NE ("invisible selector& for }", N, Sel);
4262 end if;
4263 return;
4264 end if;
4265 end if;
4267 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4269 else
4270 -- Invalid prefix
4272 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4273 end if;
4275 -- If N still has no type, the component is not defined in the prefix
4277 if Etype (N) = Any_Type then
4279 if Is_Single_Concurrent_Object then
4280 Error_Msg_Node_2 := Entity (Name);
4281 Error_Msg_NE ("no selector& for&", N, Sel);
4283 Check_Misspelled_Selector (Type_To_Use, Sel);
4285 elsif Is_Generic_Type (Prefix_Type)
4286 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4287 and then Prefix_Type /= Etype (Prefix_Type)
4288 and then Is_Record_Type (Etype (Prefix_Type))
4289 then
4290 -- If this is a derived formal type, the parent may have
4291 -- different visibility at this point. Try for an inherited
4292 -- component before reporting an error.
4294 Set_Etype (Prefix (N), Etype (Prefix_Type));
4295 Analyze_Selected_Component (N);
4296 return;
4298 -- Similarly, if this is the actual for a formal derived type, the
4299 -- component inherited from the generic parent may not be visible
4300 -- in the actual, but the selected component is legal.
4302 elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
4303 and then Is_Generic_Actual_Type (Prefix_Type)
4304 and then Present (Full_View (Prefix_Type))
4305 then
4307 Find_Component_In_Instance
4308 (Generic_Parent_Type (Parent (Prefix_Type)));
4309 return;
4311 -- Finally, the formal and the actual may be private extensions,
4312 -- but the generic is declared in a child unit of the parent, and
4313 -- an additional step is needed to retrieve the proper scope.
4315 elsif In_Instance
4316 and then Present (Parent_Subtype (Etype (Base_Type (Prefix_Type))))
4317 then
4318 Find_Component_In_Instance
4319 (Parent_Subtype (Etype (Base_Type (Prefix_Type))));
4320 return;
4322 -- Component not found, specialize error message when appropriate
4324 else
4325 if Ekind (Prefix_Type) = E_Record_Subtype then
4327 -- Check whether this is a component of the base type which
4328 -- is absent from a statically constrained subtype. This will
4329 -- raise constraint error at run time, but is not a compile-
4330 -- time error. When the selector is illegal for base type as
4331 -- well fall through and generate a compilation error anyway.
4333 Comp := First_Component (Base_Type (Prefix_Type));
4334 while Present (Comp) loop
4335 if Chars (Comp) = Chars (Sel)
4336 and then Is_Visible_Component (Comp)
4337 then
4338 Set_Entity_With_Style_Check (Sel, Comp);
4339 Generate_Reference (Comp, Sel);
4340 Set_Etype (Sel, Etype (Comp));
4341 Set_Etype (N, Etype (Comp));
4343 -- Emit appropriate message. Gigi will replace the
4344 -- node subsequently with the appropriate Raise.
4346 -- In Alfa mode, this is made into an error to simplify
4347 -- the processing of the formal verification backend.
4349 if Alfa_Mode then
4350 Apply_Compile_Time_Constraint_Error
4351 (N, "component not present in }",
4352 CE_Discriminant_Check_Failed,
4353 Ent => Prefix_Type, Rep => False);
4354 else
4355 Apply_Compile_Time_Constraint_Error
4356 (N, "component not present in }?",
4357 CE_Discriminant_Check_Failed,
4358 Ent => Prefix_Type, Rep => False);
4359 end if;
4361 Set_Raises_Constraint_Error (N);
4362 return;
4363 end if;
4365 Next_Component (Comp);
4366 end loop;
4368 end if;
4370 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4371 Error_Msg_NE ("no selector& for}", N, Sel);
4373 -- Add information in the case of an incomplete prefix
4375 if Is_Incomplete_Type (Type_To_Use) then
4376 declare
4377 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
4379 begin
4380 if From_With_Type (Scope (Type_To_Use)) then
4381 Error_Msg_NE
4382 ("\limited view of& has no components", N, Inc);
4384 else
4385 Error_Msg_NE
4386 ("\premature usage of incomplete type&", N, Inc);
4388 if Nkind (Parent (Inc)) =
4389 N_Incomplete_Type_Declaration
4390 then
4391 -- Record location of premature use in entity so that
4392 -- a continuation message is generated when the
4393 -- completion is seen.
4395 Set_Premature_Use (Parent (Inc), N);
4396 end if;
4397 end if;
4398 end;
4399 end if;
4401 Check_Misspelled_Selector (Type_To_Use, Sel);
4402 end if;
4404 Set_Entity (Sel, Any_Id);
4405 Set_Etype (Sel, Any_Type);
4406 end if;
4407 end Analyze_Selected_Component;
4409 ---------------------------
4410 -- Analyze_Short_Circuit --
4411 ---------------------------
4413 procedure Analyze_Short_Circuit (N : Node_Id) is
4414 L : constant Node_Id := Left_Opnd (N);
4415 R : constant Node_Id := Right_Opnd (N);
4416 Ind : Interp_Index;
4417 It : Interp;
4419 begin
4420 Analyze_Expression (L);
4421 Analyze_Expression (R);
4422 Set_Etype (N, Any_Type);
4424 if not Is_Overloaded (L) then
4425 if Root_Type (Etype (L)) = Standard_Boolean
4426 and then Has_Compatible_Type (R, Etype (L))
4427 then
4428 Add_One_Interp (N, Etype (L), Etype (L));
4429 end if;
4431 else
4432 Get_First_Interp (L, Ind, It);
4433 while Present (It.Typ) loop
4434 if Root_Type (It.Typ) = Standard_Boolean
4435 and then Has_Compatible_Type (R, It.Typ)
4436 then
4437 Add_One_Interp (N, It.Typ, It.Typ);
4438 end if;
4440 Get_Next_Interp (Ind, It);
4441 end loop;
4442 end if;
4444 -- Here we have failed to find an interpretation. Clearly we know that
4445 -- it is not the case that both operands can have an interpretation of
4446 -- Boolean, but this is by far the most likely intended interpretation.
4447 -- So we simply resolve both operands as Booleans, and at least one of
4448 -- these resolutions will generate an error message, and we do not need
4449 -- to give another error message on the short circuit operation itself.
4451 if Etype (N) = Any_Type then
4452 Resolve (L, Standard_Boolean);
4453 Resolve (R, Standard_Boolean);
4454 Set_Etype (N, Standard_Boolean);
4455 end if;
4456 end Analyze_Short_Circuit;
4458 -------------------
4459 -- Analyze_Slice --
4460 -------------------
4462 procedure Analyze_Slice (N : Node_Id) is
4463 D : constant Node_Id := Discrete_Range (N);
4464 P : constant Node_Id := Prefix (N);
4465 Array_Type : Entity_Id;
4466 Index_Type : Entity_Id;
4468 procedure Analyze_Overloaded_Slice;
4469 -- If the prefix is overloaded, select those interpretations that
4470 -- yield a one-dimensional array type.
4472 ------------------------------
4473 -- Analyze_Overloaded_Slice --
4474 ------------------------------
4476 procedure Analyze_Overloaded_Slice is
4477 I : Interp_Index;
4478 It : Interp;
4479 Typ : Entity_Id;
4481 begin
4482 Set_Etype (N, Any_Type);
4484 Get_First_Interp (P, I, It);
4485 while Present (It.Nam) loop
4486 Typ := It.Typ;
4488 if Is_Access_Type (Typ) then
4489 Typ := Designated_Type (Typ);
4490 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4491 end if;
4493 if Is_Array_Type (Typ)
4494 and then Number_Dimensions (Typ) = 1
4495 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4496 then
4497 Add_One_Interp (N, Typ, Typ);
4498 end if;
4500 Get_Next_Interp (I, It);
4501 end loop;
4503 if Etype (N) = Any_Type then
4504 Error_Msg_N ("expect array type in prefix of slice", N);
4505 end if;
4506 end Analyze_Overloaded_Slice;
4508 -- Start of processing for Analyze_Slice
4510 begin
4511 if Comes_From_Source (N) then
4512 Check_SPARK_Restriction ("slice is not allowed", N);
4513 end if;
4515 Analyze (P);
4516 Analyze (D);
4518 if Is_Overloaded (P) then
4519 Analyze_Overloaded_Slice;
4521 else
4522 Array_Type := Etype (P);
4523 Set_Etype (N, Any_Type);
4525 if Is_Access_Type (Array_Type) then
4526 Array_Type := Designated_Type (Array_Type);
4527 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4528 end if;
4530 if not Is_Array_Type (Array_Type) then
4531 Wrong_Type (P, Any_Array);
4533 elsif Number_Dimensions (Array_Type) > 1 then
4534 Error_Msg_N
4535 ("type is not one-dimensional array in slice prefix", N);
4537 else
4538 if Ekind (Array_Type) = E_String_Literal_Subtype then
4539 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
4540 else
4541 Index_Type := Etype (First_Index (Array_Type));
4542 end if;
4544 if not Has_Compatible_Type (D, Index_Type) then
4545 Wrong_Type (D, Index_Type);
4546 else
4547 Set_Etype (N, Array_Type);
4548 end if;
4549 end if;
4550 end if;
4551 end Analyze_Slice;
4553 -----------------------------
4554 -- Analyze_Type_Conversion --
4555 -----------------------------
4557 procedure Analyze_Type_Conversion (N : Node_Id) is
4558 Expr : constant Node_Id := Expression (N);
4559 T : Entity_Id;
4561 begin
4562 -- If Conversion_OK is set, then the Etype is already set, and the
4563 -- only processing required is to analyze the expression. This is
4564 -- used to construct certain "illegal" conversions which are not
4565 -- allowed by Ada semantics, but can be handled OK by Gigi, see
4566 -- Sinfo for further details.
4568 if Conversion_OK (N) then
4569 Analyze (Expr);
4570 return;
4571 end if;
4573 -- Otherwise full type analysis is required, as well as some semantic
4574 -- checks to make sure the argument of the conversion is appropriate.
4576 Find_Type (Subtype_Mark (N));
4577 T := Entity (Subtype_Mark (N));
4578 Set_Etype (N, T);
4579 Check_Fully_Declared (T, N);
4580 Analyze_Expression (Expr);
4581 Validate_Remote_Type_Type_Conversion (N);
4583 -- Only remaining step is validity checks on the argument. These
4584 -- are skipped if the conversion does not come from the source.
4586 if not Comes_From_Source (N) then
4587 return;
4589 -- If there was an error in a generic unit, no need to replicate the
4590 -- error message. Conversely, constant-folding in the generic may
4591 -- transform the argument of a conversion into a string literal, which
4592 -- is legal. Therefore the following tests are not performed in an
4593 -- instance.
4595 elsif In_Instance then
4596 return;
4598 elsif Nkind (Expr) = N_Null then
4599 Error_Msg_N ("argument of conversion cannot be null", N);
4600 Error_Msg_N ("\use qualified expression instead", N);
4601 Set_Etype (N, Any_Type);
4603 elsif Nkind (Expr) = N_Aggregate then
4604 Error_Msg_N ("argument of conversion cannot be aggregate", N);
4605 Error_Msg_N ("\use qualified expression instead", N);
4607 elsif Nkind (Expr) = N_Allocator then
4608 Error_Msg_N ("argument of conversion cannot be an allocator", N);
4609 Error_Msg_N ("\use qualified expression instead", N);
4611 elsif Nkind (Expr) = N_String_Literal then
4612 Error_Msg_N ("argument of conversion cannot be string literal", N);
4613 Error_Msg_N ("\use qualified expression instead", N);
4615 elsif Nkind (Expr) = N_Character_Literal then
4616 if Ada_Version = Ada_83 then
4617 Resolve (Expr, T);
4618 else
4619 Error_Msg_N ("argument of conversion cannot be character literal",
4621 Error_Msg_N ("\use qualified expression instead", N);
4622 end if;
4624 elsif Nkind (Expr) = N_Attribute_Reference
4625 and then
4626 (Attribute_Name (Expr) = Name_Access or else
4627 Attribute_Name (Expr) = Name_Unchecked_Access or else
4628 Attribute_Name (Expr) = Name_Unrestricted_Access)
4629 then
4630 Error_Msg_N ("argument of conversion cannot be access", N);
4631 Error_Msg_N ("\use qualified expression instead", N);
4632 end if;
4633 end Analyze_Type_Conversion;
4635 ----------------------
4636 -- Analyze_Unary_Op --
4637 ----------------------
4639 procedure Analyze_Unary_Op (N : Node_Id) is
4640 R : constant Node_Id := Right_Opnd (N);
4641 Op_Id : Entity_Id := Entity (N);
4643 begin
4644 Set_Etype (N, Any_Type);
4645 Candidate_Type := Empty;
4647 Analyze_Expression (R);
4649 if Present (Op_Id) then
4650 if Ekind (Op_Id) = E_Operator then
4651 Find_Unary_Types (R, Op_Id, N);
4652 else
4653 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4654 end if;
4656 else
4657 Op_Id := Get_Name_Entity_Id (Chars (N));
4658 while Present (Op_Id) loop
4659 if Ekind (Op_Id) = E_Operator then
4660 if No (Next_Entity (First_Entity (Op_Id))) then
4661 Find_Unary_Types (R, Op_Id, N);
4662 end if;
4664 elsif Is_Overloadable (Op_Id) then
4665 Analyze_User_Defined_Unary_Op (N, Op_Id);
4666 end if;
4668 Op_Id := Homonym (Op_Id);
4669 end loop;
4670 end if;
4672 Operator_Check (N);
4673 end Analyze_Unary_Op;
4675 ----------------------------------
4676 -- Analyze_Unchecked_Expression --
4677 ----------------------------------
4679 procedure Analyze_Unchecked_Expression (N : Node_Id) is
4680 begin
4681 Analyze (Expression (N), Suppress => All_Checks);
4682 Set_Etype (N, Etype (Expression (N)));
4683 Save_Interps (Expression (N), N);
4684 end Analyze_Unchecked_Expression;
4686 ---------------------------------------
4687 -- Analyze_Unchecked_Type_Conversion --
4688 ---------------------------------------
4690 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
4691 begin
4692 Find_Type (Subtype_Mark (N));
4693 Analyze_Expression (Expression (N));
4694 Set_Etype (N, Entity (Subtype_Mark (N)));
4695 end Analyze_Unchecked_Type_Conversion;
4697 ------------------------------------
4698 -- Analyze_User_Defined_Binary_Op --
4699 ------------------------------------
4701 procedure Analyze_User_Defined_Binary_Op
4702 (N : Node_Id;
4703 Op_Id : Entity_Id)
4705 begin
4706 -- Only do analysis if the operator Comes_From_Source, since otherwise
4707 -- the operator was generated by the expander, and all such operators
4708 -- always refer to the operators in package Standard.
4710 if Comes_From_Source (N) then
4711 declare
4712 F1 : constant Entity_Id := First_Formal (Op_Id);
4713 F2 : constant Entity_Id := Next_Formal (F1);
4715 begin
4716 -- Verify that Op_Id is a visible binary function. Note that since
4717 -- we know Op_Id is overloaded, potentially use visible means use
4718 -- visible for sure (RM 9.4(11)).
4720 if Ekind (Op_Id) = E_Function
4721 and then Present (F2)
4722 and then (Is_Immediately_Visible (Op_Id)
4723 or else Is_Potentially_Use_Visible (Op_Id))
4724 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
4725 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
4726 then
4727 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4729 -- If the left operand is overloaded, indicate that the
4730 -- current type is a viable candidate. This is redundant
4731 -- in most cases, but for equality and comparison operators
4732 -- where the context does not impose a type on the operands,
4733 -- setting the proper type is necessary to avoid subsequent
4734 -- ambiguities during resolution, when both user-defined and
4735 -- predefined operators may be candidates.
4737 if Is_Overloaded (Left_Opnd (N)) then
4738 Set_Etype (Left_Opnd (N), Etype (F1));
4739 end if;
4741 if Debug_Flag_E then
4742 Write_Str ("user defined operator ");
4743 Write_Name (Chars (Op_Id));
4744 Write_Str (" on node ");
4745 Write_Int (Int (N));
4746 Write_Eol;
4747 end if;
4748 end if;
4749 end;
4750 end if;
4751 end Analyze_User_Defined_Binary_Op;
4753 -----------------------------------
4754 -- Analyze_User_Defined_Unary_Op --
4755 -----------------------------------
4757 procedure Analyze_User_Defined_Unary_Op
4758 (N : Node_Id;
4759 Op_Id : Entity_Id)
4761 begin
4762 -- Only do analysis if the operator Comes_From_Source, since otherwise
4763 -- the operator was generated by the expander, and all such operators
4764 -- always refer to the operators in package Standard.
4766 if Comes_From_Source (N) then
4767 declare
4768 F : constant Entity_Id := First_Formal (Op_Id);
4770 begin
4771 -- Verify that Op_Id is a visible unary function. Note that since
4772 -- we know Op_Id is overloaded, potentially use visible means use
4773 -- visible for sure (RM 9.4(11)).
4775 if Ekind (Op_Id) = E_Function
4776 and then No (Next_Formal (F))
4777 and then (Is_Immediately_Visible (Op_Id)
4778 or else Is_Potentially_Use_Visible (Op_Id))
4779 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
4780 then
4781 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4782 end if;
4783 end;
4784 end if;
4785 end Analyze_User_Defined_Unary_Op;
4787 ---------------------------
4788 -- Check_Arithmetic_Pair --
4789 ---------------------------
4791 procedure Check_Arithmetic_Pair
4792 (T1, T2 : Entity_Id;
4793 Op_Id : Entity_Id;
4794 N : Node_Id)
4796 Op_Name : constant Name_Id := Chars (Op_Id);
4798 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
4799 -- Check whether the fixed-point type Typ has a user-defined operator
4800 -- (multiplication or division) that should hide the corresponding
4801 -- predefined operator. Used to implement Ada 2005 AI-264, to make
4802 -- such operators more visible and therefore useful.
4804 -- If the name of the operation is an expanded name with prefix
4805 -- Standard, the predefined universal fixed operator is available,
4806 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
4808 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
4809 -- Get specific type (i.e. non-universal type if there is one)
4811 ------------------
4812 -- Has_Fixed_Op --
4813 ------------------
4815 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
4816 Bas : constant Entity_Id := Base_Type (Typ);
4817 Ent : Entity_Id;
4818 F1 : Entity_Id;
4819 F2 : Entity_Id;
4821 begin
4822 -- If the universal_fixed operation is given explicitly the rule
4823 -- concerning primitive operations of the type do not apply.
4825 if Nkind (N) = N_Function_Call
4826 and then Nkind (Name (N)) = N_Expanded_Name
4827 and then Entity (Prefix (Name (N))) = Standard_Standard
4828 then
4829 return False;
4830 end if;
4832 -- The operation is treated as primitive if it is declared in the
4833 -- same scope as the type, and therefore on the same entity chain.
4835 Ent := Next_Entity (Typ);
4836 while Present (Ent) loop
4837 if Chars (Ent) = Chars (Op) then
4838 F1 := First_Formal (Ent);
4839 F2 := Next_Formal (F1);
4841 -- The operation counts as primitive if either operand or
4842 -- result are of the given base type, and both operands are
4843 -- fixed point types.
4845 if (Base_Type (Etype (F1)) = Bas
4846 and then Is_Fixed_Point_Type (Etype (F2)))
4848 or else
4849 (Base_Type (Etype (F2)) = Bas
4850 and then Is_Fixed_Point_Type (Etype (F1)))
4852 or else
4853 (Base_Type (Etype (Ent)) = Bas
4854 and then Is_Fixed_Point_Type (Etype (F1))
4855 and then Is_Fixed_Point_Type (Etype (F2)))
4856 then
4857 return True;
4858 end if;
4859 end if;
4861 Next_Entity (Ent);
4862 end loop;
4864 return False;
4865 end Has_Fixed_Op;
4867 -------------------
4868 -- Specific_Type --
4869 -------------------
4871 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
4872 begin
4873 if T1 = Universal_Integer or else T1 = Universal_Real then
4874 return Base_Type (T2);
4875 else
4876 return Base_Type (T1);
4877 end if;
4878 end Specific_Type;
4880 -- Start of processing for Check_Arithmetic_Pair
4882 begin
4883 if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
4885 if Is_Numeric_Type (T1)
4886 and then Is_Numeric_Type (T2)
4887 and then (Covers (T1 => T1, T2 => T2)
4888 or else
4889 Covers (T1 => T2, T2 => T1))
4890 then
4891 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4892 end if;
4894 elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
4896 if Is_Fixed_Point_Type (T1)
4897 and then (Is_Fixed_Point_Type (T2)
4898 or else T2 = Universal_Real)
4899 then
4900 -- If Treat_Fixed_As_Integer is set then the Etype is already set
4901 -- and no further processing is required (this is the case of an
4902 -- operator constructed by Exp_Fixd for a fixed point operation)
4903 -- Otherwise add one interpretation with universal fixed result
4904 -- If the operator is given in functional notation, it comes
4905 -- from source and Fixed_As_Integer cannot apply.
4907 if (Nkind (N) not in N_Op
4908 or else not Treat_Fixed_As_Integer (N))
4909 and then
4910 (not Has_Fixed_Op (T1, Op_Id)
4911 or else Nkind (Parent (N)) = N_Type_Conversion)
4912 then
4913 Add_One_Interp (N, Op_Id, Universal_Fixed);
4914 end if;
4916 elsif Is_Fixed_Point_Type (T2)
4917 and then (Nkind (N) not in N_Op
4918 or else not Treat_Fixed_As_Integer (N))
4919 and then T1 = Universal_Real
4920 and then
4921 (not Has_Fixed_Op (T1, Op_Id)
4922 or else Nkind (Parent (N)) = N_Type_Conversion)
4923 then
4924 Add_One_Interp (N, Op_Id, Universal_Fixed);
4926 elsif Is_Numeric_Type (T1)
4927 and then Is_Numeric_Type (T2)
4928 and then (Covers (T1 => T1, T2 => T2)
4929 or else
4930 Covers (T1 => T2, T2 => T1))
4931 then
4932 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4934 elsif Is_Fixed_Point_Type (T1)
4935 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4936 or else T2 = Universal_Integer)
4937 then
4938 Add_One_Interp (N, Op_Id, T1);
4940 elsif T2 = Universal_Real
4941 and then Base_Type (T1) = Base_Type (Standard_Integer)
4942 and then Op_Name = Name_Op_Multiply
4943 then
4944 Add_One_Interp (N, Op_Id, Any_Fixed);
4946 elsif T1 = Universal_Real
4947 and then Base_Type (T2) = Base_Type (Standard_Integer)
4948 then
4949 Add_One_Interp (N, Op_Id, Any_Fixed);
4951 elsif Is_Fixed_Point_Type (T2)
4952 and then (Base_Type (T1) = Base_Type (Standard_Integer)
4953 or else T1 = Universal_Integer)
4954 and then Op_Name = Name_Op_Multiply
4955 then
4956 Add_One_Interp (N, Op_Id, T2);
4958 elsif T1 = Universal_Real and then T2 = Universal_Integer then
4959 Add_One_Interp (N, Op_Id, T1);
4961 elsif T2 = Universal_Real
4962 and then T1 = Universal_Integer
4963 and then Op_Name = Name_Op_Multiply
4964 then
4965 Add_One_Interp (N, Op_Id, T2);
4966 end if;
4968 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
4970 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
4971 -- set does not require any special processing, since the Etype is
4972 -- already set (case of operation constructed by Exp_Fixed).
4974 if Is_Integer_Type (T1)
4975 and then (Covers (T1 => T1, T2 => T2)
4976 or else
4977 Covers (T1 => T2, T2 => T1))
4978 then
4979 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4980 end if;
4982 elsif Op_Name = Name_Op_Expon then
4983 if Is_Numeric_Type (T1)
4984 and then not Is_Fixed_Point_Type (T1)
4985 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4986 or else T2 = Universal_Integer)
4987 then
4988 Add_One_Interp (N, Op_Id, Base_Type (T1));
4989 end if;
4991 else pragma Assert (Nkind (N) in N_Op_Shift);
4993 -- If not one of the predefined operators, the node may be one
4994 -- of the intrinsic functions. Its kind is always specific, and
4995 -- we can use it directly, rather than the name of the operation.
4997 if Is_Integer_Type (T1)
4998 and then (Base_Type (T2) = Base_Type (Standard_Integer)
4999 or else T2 = Universal_Integer)
5000 then
5001 Add_One_Interp (N, Op_Id, Base_Type (T1));
5002 end if;
5003 end if;
5004 end Check_Arithmetic_Pair;
5006 -------------------------------
5007 -- Check_Misspelled_Selector --
5008 -------------------------------
5010 procedure Check_Misspelled_Selector
5011 (Prefix : Entity_Id;
5012 Sel : Node_Id)
5014 Max_Suggestions : constant := 2;
5015 Nr_Of_Suggestions : Natural := 0;
5017 Suggestion_1 : Entity_Id := Empty;
5018 Suggestion_2 : Entity_Id := Empty;
5020 Comp : Entity_Id;
5022 begin
5023 -- All the components of the prefix of selector Sel are matched
5024 -- against Sel and a count is maintained of possible misspellings.
5025 -- When at the end of the analysis there are one or two (not more!)
5026 -- possible misspellings, these misspellings will be suggested as
5027 -- possible correction.
5029 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5031 -- Concurrent types should be handled as well ???
5033 return;
5034 end if;
5036 Comp := First_Entity (Prefix);
5037 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5038 if Is_Visible_Component (Comp) then
5039 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5040 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5042 case Nr_Of_Suggestions is
5043 when 1 => Suggestion_1 := Comp;
5044 when 2 => Suggestion_2 := Comp;
5045 when others => exit;
5046 end case;
5047 end if;
5048 end if;
5050 Comp := Next_Entity (Comp);
5051 end loop;
5053 -- Report at most two suggestions
5055 if Nr_Of_Suggestions = 1 then
5056 Error_Msg_NE -- CODEFIX
5057 ("\possible misspelling of&", Sel, Suggestion_1);
5059 elsif Nr_Of_Suggestions = 2 then
5060 Error_Msg_Node_2 := Suggestion_2;
5061 Error_Msg_NE -- CODEFIX
5062 ("\possible misspelling of& or&", Sel, Suggestion_1);
5063 end if;
5064 end Check_Misspelled_Selector;
5066 ----------------------
5067 -- Defined_In_Scope --
5068 ----------------------
5070 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5072 S1 : constant Entity_Id := Scope (Base_Type (T));
5073 begin
5074 return S1 = S
5075 or else (S1 = System_Aux_Id and then S = Scope (S1));
5076 end Defined_In_Scope;
5078 -------------------
5079 -- Diagnose_Call --
5080 -------------------
5082 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5083 Actual : Node_Id;
5084 X : Interp_Index;
5085 It : Interp;
5086 Err_Mode : Boolean;
5087 New_Nam : Node_Id;
5088 Void_Interp_Seen : Boolean := False;
5090 Success : Boolean;
5091 pragma Warnings (Off, Boolean);
5093 begin
5094 if Ada_Version >= Ada_2005 then
5095 Actual := First_Actual (N);
5096 while Present (Actual) loop
5098 -- Ada 2005 (AI-50217): Post an error in case of premature
5099 -- usage of an entity from the limited view.
5101 if not Analyzed (Etype (Actual))
5102 and then From_With_Type (Etype (Actual))
5103 then
5104 Error_Msg_Qual_Level := 1;
5105 Error_Msg_NE
5106 ("missing with_clause for scope of imported type&",
5107 Actual, Etype (Actual));
5108 Error_Msg_Qual_Level := 0;
5109 end if;
5111 Next_Actual (Actual);
5112 end loop;
5113 end if;
5115 -- Analyze each candidate call again, with full error reporting
5116 -- for each.
5118 Error_Msg_N
5119 ("no candidate interpretations match the actuals:!", Nam);
5120 Err_Mode := All_Errors_Mode;
5121 All_Errors_Mode := True;
5123 -- If this is a call to an operation of a concurrent type,
5124 -- the failed interpretations have been removed from the
5125 -- name. Recover them to provide full diagnostics.
5127 if Nkind (Parent (Nam)) = N_Selected_Component then
5128 Set_Entity (Nam, Empty);
5129 New_Nam := New_Copy_Tree (Parent (Nam));
5130 Set_Is_Overloaded (New_Nam, False);
5131 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5132 Set_Parent (New_Nam, Parent (Parent (Nam)));
5133 Analyze_Selected_Component (New_Nam);
5134 Get_First_Interp (Selector_Name (New_Nam), X, It);
5135 else
5136 Get_First_Interp (Nam, X, It);
5137 end if;
5139 while Present (It.Nam) loop
5140 if Etype (It.Nam) = Standard_Void_Type then
5141 Void_Interp_Seen := True;
5142 end if;
5144 Analyze_One_Call (N, It.Nam, True, Success);
5145 Get_Next_Interp (X, It);
5146 end loop;
5148 if Nkind (N) = N_Function_Call then
5149 Get_First_Interp (Nam, X, It);
5150 while Present (It.Nam) loop
5151 if Ekind_In (It.Nam, E_Function, E_Operator) then
5152 return;
5153 else
5154 Get_Next_Interp (X, It);
5155 end if;
5156 end loop;
5158 -- If all interpretations are procedures, this deserves a
5159 -- more precise message. Ditto if this appears as the prefix
5160 -- of a selected component, which may be a lexical error.
5162 Error_Msg_N
5163 ("\context requires function call, found procedure name", Nam);
5165 if Nkind (Parent (N)) = N_Selected_Component
5166 and then N = Prefix (Parent (N))
5167 then
5168 Error_Msg_N -- CODEFIX
5169 ("\period should probably be semicolon", Parent (N));
5170 end if;
5172 elsif Nkind (N) = N_Procedure_Call_Statement
5173 and then not Void_Interp_Seen
5174 then
5175 Error_Msg_N (
5176 "\function name found in procedure call", Nam);
5177 end if;
5179 All_Errors_Mode := Err_Mode;
5180 end Diagnose_Call;
5182 ---------------------------
5183 -- Find_Arithmetic_Types --
5184 ---------------------------
5186 procedure Find_Arithmetic_Types
5187 (L, R : Node_Id;
5188 Op_Id : Entity_Id;
5189 N : Node_Id)
5191 Index1 : Interp_Index;
5192 Index2 : Interp_Index;
5193 It1 : Interp;
5194 It2 : Interp;
5196 procedure Check_Right_Argument (T : Entity_Id);
5197 -- Check right operand of operator
5199 --------------------------
5200 -- Check_Right_Argument --
5201 --------------------------
5203 procedure Check_Right_Argument (T : Entity_Id) is
5204 begin
5205 if not Is_Overloaded (R) then
5206 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5207 else
5208 Get_First_Interp (R, Index2, It2);
5209 while Present (It2.Typ) loop
5210 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5211 Get_Next_Interp (Index2, It2);
5212 end loop;
5213 end if;
5214 end Check_Right_Argument;
5216 -- Start of processing for Find_Arithmetic_Types
5218 begin
5219 if not Is_Overloaded (L) then
5220 Check_Right_Argument (Etype (L));
5222 else
5223 Get_First_Interp (L, Index1, It1);
5224 while Present (It1.Typ) loop
5225 Check_Right_Argument (It1.Typ);
5226 Get_Next_Interp (Index1, It1);
5227 end loop;
5228 end if;
5230 end Find_Arithmetic_Types;
5232 ------------------------
5233 -- Find_Boolean_Types --
5234 ------------------------
5236 procedure Find_Boolean_Types
5237 (L, R : Node_Id;
5238 Op_Id : Entity_Id;
5239 N : Node_Id)
5241 Index : Interp_Index;
5242 It : Interp;
5244 procedure Check_Numeric_Argument (T : Entity_Id);
5245 -- Special case for logical operations one of whose operands is an
5246 -- integer literal. If both are literal the result is any modular type.
5248 ----------------------------
5249 -- Check_Numeric_Argument --
5250 ----------------------------
5252 procedure Check_Numeric_Argument (T : Entity_Id) is
5253 begin
5254 if T = Universal_Integer then
5255 Add_One_Interp (N, Op_Id, Any_Modular);
5257 elsif Is_Modular_Integer_Type (T) then
5258 Add_One_Interp (N, Op_Id, T);
5259 end if;
5260 end Check_Numeric_Argument;
5262 -- Start of processing for Find_Boolean_Types
5264 begin
5265 if not Is_Overloaded (L) then
5266 if Etype (L) = Universal_Integer
5267 or else Etype (L) = Any_Modular
5268 then
5269 if not Is_Overloaded (R) then
5270 Check_Numeric_Argument (Etype (R));
5272 else
5273 Get_First_Interp (R, Index, It);
5274 while Present (It.Typ) loop
5275 Check_Numeric_Argument (It.Typ);
5276 Get_Next_Interp (Index, It);
5277 end loop;
5278 end if;
5280 -- If operands are aggregates, we must assume that they may be
5281 -- boolean arrays, and leave disambiguation for the second pass.
5282 -- If only one is an aggregate, verify that the other one has an
5283 -- interpretation as a boolean array
5285 elsif Nkind (L) = N_Aggregate then
5286 if Nkind (R) = N_Aggregate then
5287 Add_One_Interp (N, Op_Id, Etype (L));
5289 elsif not Is_Overloaded (R) then
5290 if Valid_Boolean_Arg (Etype (R)) then
5291 Add_One_Interp (N, Op_Id, Etype (R));
5292 end if;
5294 else
5295 Get_First_Interp (R, Index, It);
5296 while Present (It.Typ) loop
5297 if Valid_Boolean_Arg (It.Typ) then
5298 Add_One_Interp (N, Op_Id, It.Typ);
5299 end if;
5301 Get_Next_Interp (Index, It);
5302 end loop;
5303 end if;
5305 elsif Valid_Boolean_Arg (Etype (L))
5306 and then Has_Compatible_Type (R, Etype (L))
5307 then
5308 Add_One_Interp (N, Op_Id, Etype (L));
5309 end if;
5311 else
5312 Get_First_Interp (L, Index, It);
5313 while Present (It.Typ) loop
5314 if Valid_Boolean_Arg (It.Typ)
5315 and then Has_Compatible_Type (R, It.Typ)
5316 then
5317 Add_One_Interp (N, Op_Id, It.Typ);
5318 end if;
5320 Get_Next_Interp (Index, It);
5321 end loop;
5322 end if;
5323 end Find_Boolean_Types;
5325 ---------------------------
5326 -- Find_Comparison_Types --
5327 ---------------------------
5329 procedure Find_Comparison_Types
5330 (L, R : Node_Id;
5331 Op_Id : Entity_Id;
5332 N : Node_Id)
5334 Index : Interp_Index;
5335 It : Interp;
5336 Found : Boolean := False;
5337 I_F : Interp_Index;
5338 T_F : Entity_Id;
5339 Scop : Entity_Id := Empty;
5341 procedure Try_One_Interp (T1 : Entity_Id);
5342 -- Routine to try one proposed interpretation. Note that the context
5343 -- of the operator plays no role in resolving the arguments, so that
5344 -- if there is more than one interpretation of the operands that is
5345 -- compatible with comparison, the operation is ambiguous.
5347 --------------------
5348 -- Try_One_Interp --
5349 --------------------
5351 procedure Try_One_Interp (T1 : Entity_Id) is
5352 begin
5354 -- If the operator is an expanded name, then the type of the operand
5355 -- must be defined in the corresponding scope. If the type is
5356 -- universal, the context will impose the correct type.
5358 if Present (Scop)
5359 and then not Defined_In_Scope (T1, Scop)
5360 and then T1 /= Universal_Integer
5361 and then T1 /= Universal_Real
5362 and then T1 /= Any_String
5363 and then T1 /= Any_Composite
5364 then
5365 return;
5366 end if;
5368 if Valid_Comparison_Arg (T1)
5369 and then Has_Compatible_Type (R, T1)
5370 then
5371 if Found
5372 and then Base_Type (T1) /= Base_Type (T_F)
5373 then
5374 It := Disambiguate (L, I_F, Index, Any_Type);
5376 if It = No_Interp then
5377 Ambiguous_Operands (N);
5378 Set_Etype (L, Any_Type);
5379 return;
5381 else
5382 T_F := It.Typ;
5383 end if;
5385 else
5386 Found := True;
5387 T_F := T1;
5388 I_F := Index;
5389 end if;
5391 Set_Etype (L, T_F);
5392 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5394 end if;
5395 end Try_One_Interp;
5397 -- Start of processing for Find_Comparison_Types
5399 begin
5400 -- If left operand is aggregate, the right operand has to
5401 -- provide a usable type for it.
5403 if Nkind (L) = N_Aggregate
5404 and then Nkind (R) /= N_Aggregate
5405 then
5406 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5407 return;
5408 end if;
5410 if Nkind (N) = N_Function_Call
5411 and then Nkind (Name (N)) = N_Expanded_Name
5412 then
5413 Scop := Entity (Prefix (Name (N)));
5415 -- The prefix may be a package renaming, and the subsequent test
5416 -- requires the original package.
5418 if Ekind (Scop) = E_Package
5419 and then Present (Renamed_Entity (Scop))
5420 then
5421 Scop := Renamed_Entity (Scop);
5422 Set_Entity (Prefix (Name (N)), Scop);
5423 end if;
5424 end if;
5426 if not Is_Overloaded (L) then
5427 Try_One_Interp (Etype (L));
5429 else
5430 Get_First_Interp (L, Index, It);
5431 while Present (It.Typ) loop
5432 Try_One_Interp (It.Typ);
5433 Get_Next_Interp (Index, It);
5434 end loop;
5435 end if;
5436 end Find_Comparison_Types;
5438 ----------------------------------------
5439 -- Find_Non_Universal_Interpretations --
5440 ----------------------------------------
5442 procedure Find_Non_Universal_Interpretations
5443 (N : Node_Id;
5444 R : Node_Id;
5445 Op_Id : Entity_Id;
5446 T1 : Entity_Id)
5448 Index : Interp_Index;
5449 It : Interp;
5451 begin
5452 if T1 = Universal_Integer
5453 or else T1 = Universal_Real
5454 then
5455 if not Is_Overloaded (R) then
5456 Add_One_Interp
5457 (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5458 else
5459 Get_First_Interp (R, Index, It);
5460 while Present (It.Typ) loop
5461 if Covers (It.Typ, T1) then
5462 Add_One_Interp
5463 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5464 end if;
5466 Get_Next_Interp (Index, It);
5467 end loop;
5468 end if;
5469 else
5470 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5471 end if;
5472 end Find_Non_Universal_Interpretations;
5474 ------------------------------
5475 -- Find_Concatenation_Types --
5476 ------------------------------
5478 procedure Find_Concatenation_Types
5479 (L, R : Node_Id;
5480 Op_Id : Entity_Id;
5481 N : Node_Id)
5483 Op_Type : constant Entity_Id := Etype (Op_Id);
5485 begin
5486 if Is_Array_Type (Op_Type)
5487 and then not Is_Limited_Type (Op_Type)
5489 and then (Has_Compatible_Type (L, Op_Type)
5490 or else
5491 Has_Compatible_Type (L, Component_Type (Op_Type)))
5493 and then (Has_Compatible_Type (R, Op_Type)
5494 or else
5495 Has_Compatible_Type (R, Component_Type (Op_Type)))
5496 then
5497 Add_One_Interp (N, Op_Id, Op_Type);
5498 end if;
5499 end Find_Concatenation_Types;
5501 -------------------------
5502 -- Find_Equality_Types --
5503 -------------------------
5505 procedure Find_Equality_Types
5506 (L, R : Node_Id;
5507 Op_Id : Entity_Id;
5508 N : Node_Id)
5510 Index : Interp_Index;
5511 It : Interp;
5512 Found : Boolean := False;
5513 I_F : Interp_Index;
5514 T_F : Entity_Id;
5515 Scop : Entity_Id := Empty;
5517 procedure Try_One_Interp (T1 : Entity_Id);
5518 -- The context of the equality operator plays no role in resolving the
5519 -- arguments, so that if there is more than one interpretation of the
5520 -- operands that is compatible with equality, the construct is ambiguous
5521 -- and an error can be emitted now, after trying to disambiguate, i.e.
5522 -- applying preference rules.
5524 --------------------
5525 -- Try_One_Interp --
5526 --------------------
5528 procedure Try_One_Interp (T1 : Entity_Id) is
5529 Bas : constant Entity_Id := Base_Type (T1);
5531 begin
5532 -- If the operator is an expanded name, then the type of the operand
5533 -- must be defined in the corresponding scope. If the type is
5534 -- universal, the context will impose the correct type. An anonymous
5535 -- type for a 'Access reference is also universal in this sense, as
5536 -- the actual type is obtained from context.
5537 -- In Ada 2005, the equality operator for anonymous access types
5538 -- is declared in Standard, and preference rules apply to it.
5540 if Present (Scop) then
5541 if Defined_In_Scope (T1, Scop)
5542 or else T1 = Universal_Integer
5543 or else T1 = Universal_Real
5544 or else T1 = Any_Access
5545 or else T1 = Any_String
5546 or else T1 = Any_Composite
5547 or else (Ekind (T1) = E_Access_Subprogram_Type
5548 and then not Comes_From_Source (T1))
5549 then
5550 null;
5552 elsif Ekind (T1) = E_Anonymous_Access_Type
5553 and then Scop = Standard_Standard
5554 then
5555 null;
5557 else
5558 -- The scope does not contain an operator for the type
5560 return;
5561 end if;
5563 -- If we have infix notation, the operator must be usable. Within
5564 -- an instance, if the type is already established we know it is
5565 -- correct. If an operand is universal it is compatible with any
5566 -- numeric type.
5568 -- In Ada 2005, the equality on anonymous access types is declared
5569 -- in Standard, and is always visible.
5571 elsif In_Open_Scopes (Scope (Bas))
5572 or else Is_Potentially_Use_Visible (Bas)
5573 or else In_Use (Bas)
5574 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
5575 or else (In_Instance
5576 and then
5577 (First_Subtype (T1) = First_Subtype (Etype (R))
5578 or else
5579 (Is_Numeric_Type (T1)
5580 and then Is_Universal_Numeric_Type (Etype (R)))))
5581 or else Ekind (T1) = E_Anonymous_Access_Type
5582 then
5583 null;
5585 else
5586 -- Save candidate type for subsequent error message, if any
5588 if not Is_Limited_Type (T1) then
5589 Candidate_Type := T1;
5590 end if;
5592 return;
5593 end if;
5595 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5596 -- Do not allow anonymous access types in equality operators.
5598 if Ada_Version < Ada_2005
5599 and then Ekind (T1) = E_Anonymous_Access_Type
5600 then
5601 return;
5602 end if;
5604 if T1 /= Standard_Void_Type
5605 and then Has_Compatible_Type (R, T1)
5606 and then
5607 ((not Is_Limited_Type (T1)
5608 and then not Is_Limited_Composite (T1))
5610 or else
5611 (Is_Array_Type (T1)
5612 and then not Is_Limited_Type (Component_Type (T1))
5613 and then Available_Full_View_Of_Component (T1)))
5614 then
5615 if Found
5616 and then Base_Type (T1) /= Base_Type (T_F)
5617 then
5618 It := Disambiguate (L, I_F, Index, Any_Type);
5620 if It = No_Interp then
5621 Ambiguous_Operands (N);
5622 Set_Etype (L, Any_Type);
5623 return;
5625 else
5626 T_F := It.Typ;
5627 end if;
5629 else
5630 Found := True;
5631 T_F := T1;
5632 I_F := Index;
5633 end if;
5635 if not Analyzed (L) then
5636 Set_Etype (L, T_F);
5637 end if;
5639 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5641 -- Case of operator was not visible, Etype still set to Any_Type
5643 if Etype (N) = Any_Type then
5644 Found := False;
5645 end if;
5647 elsif Scop = Standard_Standard
5648 and then Ekind (T1) = E_Anonymous_Access_Type
5649 then
5650 Found := True;
5651 end if;
5652 end Try_One_Interp;
5654 -- Start of processing for Find_Equality_Types
5656 begin
5657 -- If left operand is aggregate, the right operand has to
5658 -- provide a usable type for it.
5660 if Nkind (L) = N_Aggregate
5661 and then Nkind (R) /= N_Aggregate
5662 then
5663 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5664 return;
5665 end if;
5667 if Nkind (N) = N_Function_Call
5668 and then Nkind (Name (N)) = N_Expanded_Name
5669 then
5670 Scop := Entity (Prefix (Name (N)));
5672 -- The prefix may be a package renaming, and the subsequent test
5673 -- requires the original package.
5675 if Ekind (Scop) = E_Package
5676 and then Present (Renamed_Entity (Scop))
5677 then
5678 Scop := Renamed_Entity (Scop);
5679 Set_Entity (Prefix (Name (N)), Scop);
5680 end if;
5681 end if;
5683 if not Is_Overloaded (L) then
5684 Try_One_Interp (Etype (L));
5686 else
5687 Get_First_Interp (L, Index, It);
5688 while Present (It.Typ) loop
5689 Try_One_Interp (It.Typ);
5690 Get_Next_Interp (Index, It);
5691 end loop;
5692 end if;
5693 end Find_Equality_Types;
5695 -------------------------
5696 -- Find_Negation_Types --
5697 -------------------------
5699 procedure Find_Negation_Types
5700 (R : Node_Id;
5701 Op_Id : Entity_Id;
5702 N : Node_Id)
5704 Index : Interp_Index;
5705 It : Interp;
5707 begin
5708 if not Is_Overloaded (R) then
5709 if Etype (R) = Universal_Integer then
5710 Add_One_Interp (N, Op_Id, Any_Modular);
5711 elsif Valid_Boolean_Arg (Etype (R)) then
5712 Add_One_Interp (N, Op_Id, Etype (R));
5713 end if;
5715 else
5716 Get_First_Interp (R, Index, It);
5717 while Present (It.Typ) loop
5718 if Valid_Boolean_Arg (It.Typ) then
5719 Add_One_Interp (N, Op_Id, It.Typ);
5720 end if;
5722 Get_Next_Interp (Index, It);
5723 end loop;
5724 end if;
5725 end Find_Negation_Types;
5727 ------------------------------
5728 -- Find_Primitive_Operation --
5729 ------------------------------
5731 function Find_Primitive_Operation (N : Node_Id) return Boolean is
5732 Obj : constant Node_Id := Prefix (N);
5733 Op : constant Node_Id := Selector_Name (N);
5735 Prim : Elmt_Id;
5736 Prims : Elist_Id;
5737 Typ : Entity_Id;
5739 begin
5740 Set_Etype (Op, Any_Type);
5742 if Is_Access_Type (Etype (Obj)) then
5743 Typ := Designated_Type (Etype (Obj));
5744 else
5745 Typ := Etype (Obj);
5746 end if;
5748 if Is_Class_Wide_Type (Typ) then
5749 Typ := Root_Type (Typ);
5750 end if;
5752 Prims := Primitive_Operations (Typ);
5754 Prim := First_Elmt (Prims);
5755 while Present (Prim) loop
5756 if Chars (Node (Prim)) = Chars (Op) then
5757 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
5758 Set_Etype (N, Etype (Node (Prim)));
5759 end if;
5761 Next_Elmt (Prim);
5762 end loop;
5764 -- Now look for class-wide operations of the type or any of its
5765 -- ancestors by iterating over the homonyms of the selector.
5767 declare
5768 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
5769 Hom : Entity_Id;
5771 begin
5772 Hom := Current_Entity (Op);
5773 while Present (Hom) loop
5774 if (Ekind (Hom) = E_Procedure
5775 or else
5776 Ekind (Hom) = E_Function)
5777 and then Scope (Hom) = Scope (Typ)
5778 and then Present (First_Formal (Hom))
5779 and then
5780 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
5781 or else
5782 (Is_Access_Type (Etype (First_Formal (Hom)))
5783 and then
5784 Ekind (Etype (First_Formal (Hom))) =
5785 E_Anonymous_Access_Type
5786 and then
5787 Base_Type
5788 (Designated_Type (Etype (First_Formal (Hom)))) =
5789 Cls_Type))
5790 then
5791 Add_One_Interp (Op, Hom, Etype (Hom));
5792 Set_Etype (N, Etype (Hom));
5793 end if;
5795 Hom := Homonym (Hom);
5796 end loop;
5797 end;
5799 return Etype (Op) /= Any_Type;
5800 end Find_Primitive_Operation;
5802 ----------------------
5803 -- Find_Unary_Types --
5804 ----------------------
5806 procedure Find_Unary_Types
5807 (R : Node_Id;
5808 Op_Id : Entity_Id;
5809 N : Node_Id)
5811 Index : Interp_Index;
5812 It : Interp;
5814 begin
5815 if not Is_Overloaded (R) then
5816 if Is_Numeric_Type (Etype (R)) then
5817 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
5818 end if;
5820 else
5821 Get_First_Interp (R, Index, It);
5822 while Present (It.Typ) loop
5823 if Is_Numeric_Type (It.Typ) then
5824 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
5825 end if;
5827 Get_Next_Interp (Index, It);
5828 end loop;
5829 end if;
5830 end Find_Unary_Types;
5832 ------------------
5833 -- Junk_Operand --
5834 ------------------
5836 function Junk_Operand (N : Node_Id) return Boolean is
5837 Enode : Node_Id;
5839 begin
5840 if Error_Posted (N) then
5841 return False;
5842 end if;
5844 -- Get entity to be tested
5846 if Is_Entity_Name (N)
5847 and then Present (Entity (N))
5848 then
5849 Enode := N;
5851 -- An odd case, a procedure name gets converted to a very peculiar
5852 -- function call, and here is where we detect this happening.
5854 elsif Nkind (N) = N_Function_Call
5855 and then Is_Entity_Name (Name (N))
5856 and then Present (Entity (Name (N)))
5857 then
5858 Enode := Name (N);
5860 -- Another odd case, there are at least some cases of selected
5861 -- components where the selected component is not marked as having
5862 -- an entity, even though the selector does have an entity
5864 elsif Nkind (N) = N_Selected_Component
5865 and then Present (Entity (Selector_Name (N)))
5866 then
5867 Enode := Selector_Name (N);
5869 else
5870 return False;
5871 end if;
5873 -- Now test the entity we got to see if it is a bad case
5875 case Ekind (Entity (Enode)) is
5877 when E_Package =>
5878 Error_Msg_N
5879 ("package name cannot be used as operand", Enode);
5881 when Generic_Unit_Kind =>
5882 Error_Msg_N
5883 ("generic unit name cannot be used as operand", Enode);
5885 when Type_Kind =>
5886 Error_Msg_N
5887 ("subtype name cannot be used as operand", Enode);
5889 when Entry_Kind =>
5890 Error_Msg_N
5891 ("entry name cannot be used as operand", Enode);
5893 when E_Procedure =>
5894 Error_Msg_N
5895 ("procedure name cannot be used as operand", Enode);
5897 when E_Exception =>
5898 Error_Msg_N
5899 ("exception name cannot be used as operand", Enode);
5901 when E_Block | E_Label | E_Loop =>
5902 Error_Msg_N
5903 ("label name cannot be used as operand", Enode);
5905 when others =>
5906 return False;
5908 end case;
5910 return True;
5911 end Junk_Operand;
5913 --------------------
5914 -- Operator_Check --
5915 --------------------
5917 procedure Operator_Check (N : Node_Id) is
5918 begin
5919 Remove_Abstract_Operations (N);
5921 -- Test for case of no interpretation found for operator
5923 if Etype (N) = Any_Type then
5924 declare
5925 L : Node_Id;
5926 R : Node_Id;
5927 Op_Id : Entity_Id := Empty;
5929 begin
5930 R := Right_Opnd (N);
5932 if Nkind (N) in N_Binary_Op then
5933 L := Left_Opnd (N);
5934 else
5935 L := Empty;
5936 end if;
5938 -- If either operand has no type, then don't complain further,
5939 -- since this simply means that we have a propagated error.
5941 if R = Error
5942 or else Etype (R) = Any_Type
5943 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
5944 then
5945 return;
5947 -- We explicitly check for the case of concatenation of component
5948 -- with component to avoid reporting spurious matching array types
5949 -- that might happen to be lurking in distant packages (such as
5950 -- run-time packages). This also prevents inconsistencies in the
5951 -- messages for certain ACVC B tests, which can vary depending on
5952 -- types declared in run-time interfaces. Another improvement when
5953 -- aggregates are present is to look for a well-typed operand.
5955 elsif Present (Candidate_Type)
5956 and then (Nkind (N) /= N_Op_Concat
5957 or else Is_Array_Type (Etype (L))
5958 or else Is_Array_Type (Etype (R)))
5959 then
5960 if Nkind (N) = N_Op_Concat then
5961 if Etype (L) /= Any_Composite
5962 and then Is_Array_Type (Etype (L))
5963 then
5964 Candidate_Type := Etype (L);
5966 elsif Etype (R) /= Any_Composite
5967 and then Is_Array_Type (Etype (R))
5968 then
5969 Candidate_Type := Etype (R);
5970 end if;
5971 end if;
5973 Error_Msg_NE -- CODEFIX
5974 ("operator for} is not directly visible!",
5975 N, First_Subtype (Candidate_Type));
5977 declare
5978 U : constant Node_Id :=
5979 Cunit (Get_Source_Unit (Candidate_Type));
5980 begin
5981 if Unit_Is_Visible (U) then
5982 Error_Msg_N -- CODEFIX
5983 ("use clause would make operation legal!", N);
5984 else
5985 Error_Msg_NE -- CODEFIX
5986 ("add with_clause and use_clause for&!",
5987 N, Defining_Entity (Unit (U)));
5988 end if;
5989 end;
5990 return;
5992 -- If either operand is a junk operand (e.g. package name), then
5993 -- post appropriate error messages, but do not complain further.
5995 -- Note that the use of OR in this test instead of OR ELSE is
5996 -- quite deliberate, we may as well check both operands in the
5997 -- binary operator case.
5999 elsif Junk_Operand (R)
6000 or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
6001 then
6002 return;
6004 -- If we have a logical operator, one of whose operands is
6005 -- Boolean, then we know that the other operand cannot resolve to
6006 -- Boolean (since we got no interpretations), but in that case we
6007 -- pretty much know that the other operand should be Boolean, so
6008 -- resolve it that way (generating an error)
6010 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
6011 if Etype (L) = Standard_Boolean then
6012 Resolve (R, Standard_Boolean);
6013 return;
6014 elsif Etype (R) = Standard_Boolean then
6015 Resolve (L, Standard_Boolean);
6016 return;
6017 end if;
6019 -- For an arithmetic operator or comparison operator, if one
6020 -- of the operands is numeric, then we know the other operand
6021 -- is not the same numeric type. If it is a non-numeric type,
6022 -- then probably it is intended to match the other operand.
6024 elsif Nkind_In (N, N_Op_Add,
6025 N_Op_Divide,
6026 N_Op_Ge,
6027 N_Op_Gt,
6028 N_Op_Le)
6029 or else
6030 Nkind_In (N, N_Op_Lt,
6031 N_Op_Mod,
6032 N_Op_Multiply,
6033 N_Op_Rem,
6034 N_Op_Subtract)
6035 then
6036 if Is_Numeric_Type (Etype (L))
6037 and then not Is_Numeric_Type (Etype (R))
6038 then
6039 Resolve (R, Etype (L));
6040 return;
6042 elsif Is_Numeric_Type (Etype (R))
6043 and then not Is_Numeric_Type (Etype (L))
6044 then
6045 Resolve (L, Etype (R));
6046 return;
6047 end if;
6049 -- Comparisons on A'Access are common enough to deserve a
6050 -- special message.
6052 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6053 and then Ekind (Etype (L)) = E_Access_Attribute_Type
6054 and then Ekind (Etype (R)) = E_Access_Attribute_Type
6055 then
6056 Error_Msg_N
6057 ("two access attributes cannot be compared directly", N);
6058 Error_Msg_N
6059 ("\use qualified expression for one of the operands",
6061 return;
6063 -- Another one for C programmers
6065 elsif Nkind (N) = N_Op_Concat
6066 and then Valid_Boolean_Arg (Etype (L))
6067 and then Valid_Boolean_Arg (Etype (R))
6068 then
6069 Error_Msg_N ("invalid operands for concatenation", N);
6070 Error_Msg_N -- CODEFIX
6071 ("\maybe AND was meant", N);
6072 return;
6074 -- A special case for comparison of access parameter with null
6076 elsif Nkind (N) = N_Op_Eq
6077 and then Is_Entity_Name (L)
6078 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6079 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6080 N_Access_Definition
6081 and then Nkind (R) = N_Null
6082 then
6083 Error_Msg_N ("access parameter is not allowed to be null", L);
6084 Error_Msg_N ("\(call would raise Constraint_Error)", L);
6085 return;
6087 -- Another special case for exponentiation, where the right
6088 -- operand must be Natural, independently of the base.
6090 elsif Nkind (N) = N_Op_Expon
6091 and then Is_Numeric_Type (Etype (L))
6092 and then not Is_Overloaded (R)
6093 and then
6094 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6095 and then Base_Type (Etype (R)) /= Universal_Integer
6096 then
6097 if Ada_Version >= Ada_2012
6098 and then Has_Dimension_System (Etype (L))
6099 then
6100 Error_Msg_NE
6101 ("exponent for dimensioned type must be a rational" &
6102 ", found}", R, Etype (R));
6103 else
6104 Error_Msg_NE
6105 ("exponent must be of type Natural, found}", R, Etype (R));
6106 end if;
6108 return;
6109 end if;
6111 -- If we fall through then just give general message. Note that in
6112 -- the following messages, if the operand is overloaded we choose
6113 -- an arbitrary type to complain about, but that is probably more
6114 -- useful than not giving a type at all.
6116 if Nkind (N) in N_Unary_Op then
6117 Error_Msg_Node_2 := Etype (R);
6118 Error_Msg_N ("operator& not defined for}", N);
6119 return;
6121 else
6122 if Nkind (N) in N_Binary_Op then
6123 if not Is_Overloaded (L)
6124 and then not Is_Overloaded (R)
6125 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6126 then
6127 Error_Msg_Node_2 := First_Subtype (Etype (R));
6128 Error_Msg_N ("there is no applicable operator& for}", N);
6130 else
6131 -- Another attempt to find a fix: one of the candidate
6132 -- interpretations may not be use-visible. This has
6133 -- already been checked for predefined operators, so
6134 -- we examine only user-defined functions.
6136 Op_Id := Get_Name_Entity_Id (Chars (N));
6138 while Present (Op_Id) loop
6139 if Ekind (Op_Id) /= E_Operator
6140 and then Is_Overloadable (Op_Id)
6141 then
6142 if not Is_Immediately_Visible (Op_Id)
6143 and then not In_Use (Scope (Op_Id))
6144 and then not Is_Abstract_Subprogram (Op_Id)
6145 and then not Is_Hidden (Op_Id)
6146 and then Ekind (Scope (Op_Id)) = E_Package
6147 and then
6148 Has_Compatible_Type
6149 (L, Etype (First_Formal (Op_Id)))
6150 and then Present
6151 (Next_Formal (First_Formal (Op_Id)))
6152 and then
6153 Has_Compatible_Type
6155 Etype (Next_Formal (First_Formal (Op_Id))))
6156 then
6157 Error_Msg_N
6158 ("No legal interpretation for operator&", N);
6159 Error_Msg_NE
6160 ("\use clause on& would make operation legal",
6161 N, Scope (Op_Id));
6162 exit;
6163 end if;
6164 end if;
6166 Op_Id := Homonym (Op_Id);
6167 end loop;
6169 if No (Op_Id) then
6170 Error_Msg_N ("invalid operand types for operator&", N);
6172 if Nkind (N) /= N_Op_Concat then
6173 Error_Msg_NE ("\left operand has}!", N, Etype (L));
6174 Error_Msg_NE ("\right operand has}!", N, Etype (R));
6175 end if;
6176 end if;
6177 end if;
6178 end if;
6179 end if;
6180 end;
6181 end if;
6182 end Operator_Check;
6184 -----------------------------------------
6185 -- Process_Implicit_Dereference_Prefix --
6186 -----------------------------------------
6188 function Process_Implicit_Dereference_Prefix
6189 (E : Entity_Id;
6190 P : Entity_Id) return Entity_Id
6192 Ref : Node_Id;
6193 Typ : constant Entity_Id := Designated_Type (Etype (P));
6195 begin
6196 if Present (E)
6197 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6198 then
6199 -- We create a dummy reference to E to ensure that the reference
6200 -- is not considered as part of an assignment (an implicit
6201 -- dereference can never assign to its prefix). The Comes_From_Source
6202 -- attribute needs to be propagated for accurate warnings.
6204 Ref := New_Reference_To (E, Sloc (P));
6205 Set_Comes_From_Source (Ref, Comes_From_Source (P));
6206 Generate_Reference (E, Ref);
6207 end if;
6209 -- An implicit dereference is a legal occurrence of an
6210 -- incomplete type imported through a limited_with clause,
6211 -- if the full view is visible.
6213 if From_With_Type (Typ)
6214 and then not From_With_Type (Scope (Typ))
6215 and then
6216 (Is_Immediately_Visible (Scope (Typ))
6217 or else
6218 (Is_Child_Unit (Scope (Typ))
6219 and then Is_Visible_Child_Unit (Scope (Typ))))
6220 then
6221 return Available_View (Typ);
6222 else
6223 return Typ;
6224 end if;
6226 end Process_Implicit_Dereference_Prefix;
6228 --------------------------------
6229 -- Remove_Abstract_Operations --
6230 --------------------------------
6232 procedure Remove_Abstract_Operations (N : Node_Id) is
6233 Abstract_Op : Entity_Id := Empty;
6234 Address_Kludge : Boolean := False;
6235 I : Interp_Index;
6236 It : Interp;
6238 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
6239 -- activate this if either extensions are enabled, or if the abstract
6240 -- operation in question comes from a predefined file. This latter test
6241 -- allows us to use abstract to make operations invisible to users. In
6242 -- particular, if type Address is non-private and abstract subprograms
6243 -- are used to hide its operators, they will be truly hidden.
6245 type Operand_Position is (First_Op, Second_Op);
6246 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6248 procedure Remove_Address_Interpretations (Op : Operand_Position);
6249 -- Ambiguities may arise when the operands are literal and the address
6250 -- operations in s-auxdec are visible. In that case, remove the
6251 -- interpretation of a literal as Address, to retain the semantics of
6252 -- Address as a private type.
6254 ------------------------------------
6255 -- Remove_Address_Interpretations --
6256 ------------------------------------
6258 procedure Remove_Address_Interpretations (Op : Operand_Position) is
6259 Formal : Entity_Id;
6261 begin
6262 if Is_Overloaded (N) then
6263 Get_First_Interp (N, I, It);
6264 while Present (It.Nam) loop
6265 Formal := First_Entity (It.Nam);
6267 if Op = Second_Op then
6268 Formal := Next_Entity (Formal);
6269 end if;
6271 if Is_Descendent_Of_Address (Etype (Formal)) then
6272 Address_Kludge := True;
6273 Remove_Interp (I);
6274 end if;
6276 Get_Next_Interp (I, It);
6277 end loop;
6278 end if;
6279 end Remove_Address_Interpretations;
6281 -- Start of processing for Remove_Abstract_Operations
6283 begin
6284 if Is_Overloaded (N) then
6285 if Debug_Flag_V then
6286 Write_Str ("Remove_Abstract_Operations: ");
6287 Write_Overloads (N);
6288 end if;
6290 Get_First_Interp (N, I, It);
6292 while Present (It.Nam) loop
6293 if Is_Overloadable (It.Nam)
6294 and then Is_Abstract_Subprogram (It.Nam)
6295 and then not Is_Dispatching_Operation (It.Nam)
6296 then
6297 Abstract_Op := It.Nam;
6299 if Is_Descendent_Of_Address (It.Typ) then
6300 Address_Kludge := True;
6301 Remove_Interp (I);
6302 exit;
6304 -- In Ada 2005, this operation does not participate in overload
6305 -- resolution. If the operation is defined in a predefined
6306 -- unit, it is one of the operations declared abstract in some
6307 -- variants of System, and it must be removed as well.
6309 elsif Ada_Version >= Ada_2005
6310 or else Is_Predefined_File_Name
6311 (Unit_File_Name (Get_Source_Unit (It.Nam)))
6312 then
6313 Remove_Interp (I);
6314 exit;
6315 end if;
6316 end if;
6318 Get_Next_Interp (I, It);
6319 end loop;
6321 if No (Abstract_Op) then
6323 -- If some interpretation yields an integer type, it is still
6324 -- possible that there are address interpretations. Remove them
6325 -- if one operand is a literal, to avoid spurious ambiguities
6326 -- on systems where Address is a visible integer type.
6328 if Is_Overloaded (N)
6329 and then Nkind (N) in N_Op
6330 and then Is_Integer_Type (Etype (N))
6331 then
6332 if Nkind (N) in N_Binary_Op then
6333 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6334 Remove_Address_Interpretations (Second_Op);
6336 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6337 Remove_Address_Interpretations (First_Op);
6338 end if;
6339 end if;
6340 end if;
6342 elsif Nkind (N) in N_Op then
6344 -- Remove interpretations that treat literals as addresses. This
6345 -- is never appropriate, even when Address is defined as a visible
6346 -- Integer type. The reason is that we would really prefer Address
6347 -- to behave as a private type, even in this case, which is there
6348 -- only to accommodate oddities of VMS address sizes. If Address
6349 -- is a visible integer type, we get lots of overload ambiguities.
6351 if Nkind (N) in N_Binary_Op then
6352 declare
6353 U1 : constant Boolean :=
6354 Present (Universal_Interpretation (Right_Opnd (N)));
6355 U2 : constant Boolean :=
6356 Present (Universal_Interpretation (Left_Opnd (N)));
6358 begin
6359 if U1 then
6360 Remove_Address_Interpretations (Second_Op);
6361 end if;
6363 if U2 then
6364 Remove_Address_Interpretations (First_Op);
6365 end if;
6367 if not (U1 and U2) then
6369 -- Remove corresponding predefined operator, which is
6370 -- always added to the overload set.
6372 Get_First_Interp (N, I, It);
6373 while Present (It.Nam) loop
6374 if Scope (It.Nam) = Standard_Standard
6375 and then Base_Type (It.Typ) =
6376 Base_Type (Etype (Abstract_Op))
6377 then
6378 Remove_Interp (I);
6379 end if;
6381 Get_Next_Interp (I, It);
6382 end loop;
6384 elsif Is_Overloaded (N)
6385 and then Present (Univ_Type)
6386 then
6387 -- If both operands have a universal interpretation,
6388 -- it is still necessary to remove interpretations that
6389 -- yield Address. Any remaining ambiguities will be
6390 -- removed in Disambiguate.
6392 Get_First_Interp (N, I, It);
6393 while Present (It.Nam) loop
6394 if Is_Descendent_Of_Address (It.Typ) then
6395 Remove_Interp (I);
6397 elsif not Is_Type (It.Nam) then
6398 Set_Entity (N, It.Nam);
6399 end if;
6401 Get_Next_Interp (I, It);
6402 end loop;
6403 end if;
6404 end;
6405 end if;
6407 elsif Nkind (N) = N_Function_Call
6408 and then
6409 (Nkind (Name (N)) = N_Operator_Symbol
6410 or else
6411 (Nkind (Name (N)) = N_Expanded_Name
6412 and then
6413 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6414 then
6416 declare
6417 Arg1 : constant Node_Id := First (Parameter_Associations (N));
6418 U1 : constant Boolean :=
6419 Present (Universal_Interpretation (Arg1));
6420 U2 : constant Boolean :=
6421 Present (Next (Arg1)) and then
6422 Present (Universal_Interpretation (Next (Arg1)));
6424 begin
6425 if U1 then
6426 Remove_Address_Interpretations (First_Op);
6427 end if;
6429 if U2 then
6430 Remove_Address_Interpretations (Second_Op);
6431 end if;
6433 if not (U1 and U2) then
6434 Get_First_Interp (N, I, It);
6435 while Present (It.Nam) loop
6436 if Scope (It.Nam) = Standard_Standard
6437 and then It.Typ = Base_Type (Etype (Abstract_Op))
6438 then
6439 Remove_Interp (I);
6440 end if;
6442 Get_Next_Interp (I, It);
6443 end loop;
6444 end if;
6445 end;
6446 end if;
6448 -- If the removal has left no valid interpretations, emit an error
6449 -- message now and label node as illegal.
6451 if Present (Abstract_Op) then
6452 Get_First_Interp (N, I, It);
6454 if No (It.Nam) then
6456 -- Removal of abstract operation left no viable candidate
6458 Set_Etype (N, Any_Type);
6459 Error_Msg_Sloc := Sloc (Abstract_Op);
6460 Error_Msg_NE
6461 ("cannot call abstract operation& declared#", N, Abstract_Op);
6463 -- In Ada 2005, an abstract operation may disable predefined
6464 -- operators. Since the context is not yet known, we mark the
6465 -- predefined operators as potentially hidden. Do not include
6466 -- predefined operators when addresses are involved since this
6467 -- case is handled separately.
6469 elsif Ada_Version >= Ada_2005
6470 and then not Address_Kludge
6471 then
6472 while Present (It.Nam) loop
6473 if Is_Numeric_Type (It.Typ)
6474 and then Scope (It.Typ) = Standard_Standard
6475 then
6476 Set_Abstract_Op (I, Abstract_Op);
6477 end if;
6479 Get_Next_Interp (I, It);
6480 end loop;
6481 end if;
6482 end if;
6484 if Debug_Flag_V then
6485 Write_Str ("Remove_Abstract_Operations done: ");
6486 Write_Overloads (N);
6487 end if;
6488 end if;
6489 end Remove_Abstract_Operations;
6491 ----------------------------
6492 -- Try_Container_Indexing --
6493 ----------------------------
6495 function Try_Container_Indexing
6496 (N : Node_Id;
6497 Prefix : Node_Id;
6498 Exprs : List_Id) return Boolean
6500 Loc : constant Source_Ptr := Sloc (N);
6501 Assoc : List_Id;
6502 Disc : Entity_Id;
6503 Func : Entity_Id;
6504 Func_Name : Node_Id;
6505 Indexing : Node_Id;
6507 begin
6509 -- Check whether type has a specified indexing aspect
6511 Func_Name := Empty;
6513 if Is_Variable (Prefix) then
6514 Func_Name := Find_Aspect (Etype (Prefix), Aspect_Variable_Indexing);
6515 end if;
6517 if No (Func_Name) then
6518 Func_Name := Find_Aspect (Etype (Prefix), Aspect_Constant_Indexing);
6519 end if;
6521 -- If aspect does not exist the expression is illegal. Error is
6522 -- diagnosed in caller.
6524 if No (Func_Name) then
6526 -- The prefix itself may be an indexing of a container
6527 -- rewrite as such and re-analyze.
6529 if Has_Implicit_Dereference (Etype (Prefix)) then
6530 Build_Explicit_Dereference
6531 (Prefix, First_Discriminant (Etype (Prefix)));
6532 return Try_Container_Indexing (N, Prefix, Exprs);
6534 else
6535 return False;
6536 end if;
6537 end if;
6539 Assoc := New_List (Relocate_Node (Prefix));
6541 -- A generalized iterator may have nore than one index expression, so
6542 -- transfer all of them to the argument list to be used in the call.
6544 declare
6545 Arg : Node_Id;
6546 begin
6547 Arg := First (Exprs);
6548 while Present (Arg) loop
6549 Append (Relocate_Node (Arg), Assoc);
6550 Next (Arg);
6551 end loop;
6552 end;
6554 if not Is_Overloaded (Func_Name) then
6555 Func := Entity (Func_Name);
6556 Indexing :=
6557 Make_Function_Call (Loc,
6558 Name => New_Occurrence_Of (Func, Loc),
6559 Parameter_Associations => Assoc);
6560 Rewrite (N, Indexing);
6561 Analyze (N);
6563 -- If the return type of the indexing function is a reference type,
6564 -- add the dereference as a possible interpretation. Note that the
6565 -- indexing aspect may be a function that returns the element type
6566 -- with no intervening implicit dereference.
6568 if Has_Discriminants (Etype (Func)) then
6569 Disc := First_Discriminant (Etype (Func));
6570 while Present (Disc) loop
6571 if Has_Implicit_Dereference (Disc) then
6572 Add_One_Interp (N, Disc, Designated_Type (Etype (Disc)));
6573 exit;
6574 end if;
6576 Next_Discriminant (Disc);
6577 end loop;
6578 end if;
6580 else
6581 Indexing := Make_Function_Call (Loc,
6582 Name => Make_Identifier (Loc, Chars (Func_Name)),
6583 Parameter_Associations => Assoc);
6585 Rewrite (N, Indexing);
6587 declare
6588 I : Interp_Index;
6589 It : Interp;
6590 Success : Boolean;
6592 begin
6593 Get_First_Interp (Func_Name, I, It);
6594 Set_Etype (N, Any_Type);
6595 while Present (It.Nam) loop
6596 Analyze_One_Call (N, It.Nam, False, Success);
6597 if Success then
6598 Set_Etype (Name (N), It.Typ);
6599 Set_Entity (Name (N), It.Nam);
6601 -- Add implicit dereference interpretation
6603 if Has_Discriminants (Etype (It.Nam)) then
6604 Disc := First_Discriminant (Etype (It.Nam));
6605 while Present (Disc) loop
6606 if Has_Implicit_Dereference (Disc) then
6607 Add_One_Interp
6608 (N, Disc, Designated_Type (Etype (Disc)));
6609 exit;
6610 end if;
6612 Next_Discriminant (Disc);
6613 end loop;
6614 end if;
6616 exit;
6617 end if;
6618 Get_Next_Interp (I, It);
6619 end loop;
6620 end;
6621 end if;
6623 if Etype (N) = Any_Type then
6624 Error_Msg_NE
6625 ("container cannot be indexed with&", N, Etype (First (Exprs)));
6626 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
6627 else
6628 Analyze (N);
6629 end if;
6631 return True;
6632 end Try_Container_Indexing;
6634 -----------------------
6635 -- Try_Indirect_Call --
6636 -----------------------
6638 function Try_Indirect_Call
6639 (N : Node_Id;
6640 Nam : Entity_Id;
6641 Typ : Entity_Id) return Boolean
6643 Actual : Node_Id;
6644 Formal : Entity_Id;
6646 Call_OK : Boolean;
6647 pragma Warnings (Off, Call_OK);
6649 begin
6650 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
6652 Actual := First_Actual (N);
6653 Formal := First_Formal (Designated_Type (Typ));
6654 while Present (Actual) and then Present (Formal) loop
6655 if not Has_Compatible_Type (Actual, Etype (Formal)) then
6656 return False;
6657 end if;
6659 Next (Actual);
6660 Next_Formal (Formal);
6661 end loop;
6663 if No (Actual) and then No (Formal) then
6664 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
6666 -- Nam is a candidate interpretation for the name in the call,
6667 -- if it is not an indirect call.
6669 if not Is_Type (Nam)
6670 and then Is_Entity_Name (Name (N))
6671 then
6672 Set_Entity (Name (N), Nam);
6673 end if;
6675 return True;
6676 else
6677 return False;
6678 end if;
6679 end Try_Indirect_Call;
6681 ----------------------
6682 -- Try_Indexed_Call --
6683 ----------------------
6685 function Try_Indexed_Call
6686 (N : Node_Id;
6687 Nam : Entity_Id;
6688 Typ : Entity_Id;
6689 Skip_First : Boolean) return Boolean
6691 Loc : constant Source_Ptr := Sloc (N);
6692 Actuals : constant List_Id := Parameter_Associations (N);
6693 Actual : Node_Id;
6694 Index : Entity_Id;
6696 begin
6697 Actual := First (Actuals);
6699 -- If the call was originally written in prefix form, skip the first
6700 -- actual, which is obviously not defaulted.
6702 if Skip_First then
6703 Next (Actual);
6704 end if;
6706 Index := First_Index (Typ);
6707 while Present (Actual) and then Present (Index) loop
6709 -- If the parameter list has a named association, the expression
6710 -- is definitely a call and not an indexed component.
6712 if Nkind (Actual) = N_Parameter_Association then
6713 return False;
6714 end if;
6716 if Is_Entity_Name (Actual)
6717 and then Is_Type (Entity (Actual))
6718 and then No (Next (Actual))
6719 then
6720 -- A single actual that is a type name indicates a slice if the
6721 -- type is discrete, and an error otherwise.
6723 if Is_Discrete_Type (Entity (Actual)) then
6724 Rewrite (N,
6725 Make_Slice (Loc,
6726 Prefix =>
6727 Make_Function_Call (Loc,
6728 Name => Relocate_Node (Name (N))),
6729 Discrete_Range =>
6730 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
6732 Analyze (N);
6734 else
6735 Error_Msg_N ("invalid use of type in expression", Actual);
6736 Set_Etype (N, Any_Type);
6737 end if;
6739 return True;
6741 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
6742 return False;
6743 end if;
6745 Next (Actual);
6746 Next_Index (Index);
6747 end loop;
6749 if No (Actual) and then No (Index) then
6750 Add_One_Interp (N, Nam, Component_Type (Typ));
6752 -- Nam is a candidate interpretation for the name in the call,
6753 -- if it is not an indirect call.
6755 if not Is_Type (Nam)
6756 and then Is_Entity_Name (Name (N))
6757 then
6758 Set_Entity (Name (N), Nam);
6759 end if;
6761 return True;
6762 else
6763 return False;
6764 end if;
6765 end Try_Indexed_Call;
6767 --------------------------
6768 -- Try_Object_Operation --
6769 --------------------------
6771 function Try_Object_Operation
6772 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
6774 K : constant Node_Kind := Nkind (Parent (N));
6775 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
6776 Loc : constant Source_Ptr := Sloc (N);
6777 Obj : constant Node_Id := Prefix (N);
6779 Subprog : constant Node_Id :=
6780 Make_Identifier (Sloc (Selector_Name (N)),
6781 Chars => Chars (Selector_Name (N)));
6782 -- Identifier on which possible interpretations will be collected
6784 Report_Error : Boolean := False;
6785 -- If no candidate interpretation matches the context, redo the
6786 -- analysis with error enabled to provide additional information.
6788 Actual : Node_Id;
6789 Candidate : Entity_Id := Empty;
6790 New_Call_Node : Node_Id := Empty;
6791 Node_To_Replace : Node_Id;
6792 Obj_Type : Entity_Id := Etype (Obj);
6793 Success : Boolean := False;
6795 function Valid_Candidate
6796 (Success : Boolean;
6797 Call : Node_Id;
6798 Subp : Entity_Id) return Entity_Id;
6799 -- If the subprogram is a valid interpretation, record it, and add
6800 -- to the list of interpretations of Subprog. Otherwise return Empty.
6802 procedure Complete_Object_Operation
6803 (Call_Node : Node_Id;
6804 Node_To_Replace : Node_Id);
6805 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
6806 -- Call_Node, insert the object (or its dereference) as the first actual
6807 -- in the call, and complete the analysis of the call.
6809 procedure Report_Ambiguity (Op : Entity_Id);
6810 -- If a prefixed procedure call is ambiguous, indicate whether the
6811 -- call includes an implicit dereference or an implicit 'Access.
6813 procedure Transform_Object_Operation
6814 (Call_Node : out Node_Id;
6815 Node_To_Replace : out Node_Id);
6816 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
6817 -- Call_Node is the resulting subprogram call, Node_To_Replace is
6818 -- either N or the parent of N, and Subprog is a reference to the
6819 -- subprogram we are trying to match.
6821 function Try_Class_Wide_Operation
6822 (Call_Node : Node_Id;
6823 Node_To_Replace : Node_Id) return Boolean;
6824 -- Traverse all ancestor types looking for a class-wide subprogram
6825 -- for which the current operation is a valid non-dispatching call.
6827 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
6828 -- If prefix is overloaded, its interpretation may include different
6829 -- tagged types, and we must examine the primitive operations and
6830 -- the class-wide operations of each in order to find candidate
6831 -- interpretations for the call as a whole.
6833 function Try_Primitive_Operation
6834 (Call_Node : Node_Id;
6835 Node_To_Replace : Node_Id) return Boolean;
6836 -- Traverse the list of primitive subprograms looking for a dispatching
6837 -- operation for which the current node is a valid call .
6839 ---------------------
6840 -- Valid_Candidate --
6841 ---------------------
6843 function Valid_Candidate
6844 (Success : Boolean;
6845 Call : Node_Id;
6846 Subp : Entity_Id) return Entity_Id
6848 Arr_Type : Entity_Id;
6849 Comp_Type : Entity_Id;
6851 begin
6852 -- If the subprogram is a valid interpretation, record it in global
6853 -- variable Subprog, to collect all possible overloadings.
6855 if Success then
6856 if Subp /= Entity (Subprog) then
6857 Add_One_Interp (Subprog, Subp, Etype (Subp));
6858 end if;
6859 end if;
6861 -- If the call may be an indexed call, retrieve component type of
6862 -- resulting expression, and add possible interpretation.
6864 Arr_Type := Empty;
6865 Comp_Type := Empty;
6867 if Nkind (Call) = N_Function_Call
6868 and then Nkind (Parent (N)) = N_Indexed_Component
6869 and then Needs_One_Actual (Subp)
6870 then
6871 if Is_Array_Type (Etype (Subp)) then
6872 Arr_Type := Etype (Subp);
6874 elsif Is_Access_Type (Etype (Subp))
6875 and then Is_Array_Type (Designated_Type (Etype (Subp)))
6876 then
6877 Arr_Type := Designated_Type (Etype (Subp));
6878 end if;
6879 end if;
6881 if Present (Arr_Type) then
6883 -- Verify that the actuals (excluding the object) match the types
6884 -- of the indexes.
6886 declare
6887 Actual : Node_Id;
6888 Index : Node_Id;
6890 begin
6891 Actual := Next (First_Actual (Call));
6892 Index := First_Index (Arr_Type);
6893 while Present (Actual) and then Present (Index) loop
6894 if not Has_Compatible_Type (Actual, Etype (Index)) then
6895 Arr_Type := Empty;
6896 exit;
6897 end if;
6899 Next_Actual (Actual);
6900 Next_Index (Index);
6901 end loop;
6903 if No (Actual)
6904 and then No (Index)
6905 and then Present (Arr_Type)
6906 then
6907 Comp_Type := Component_Type (Arr_Type);
6908 end if;
6909 end;
6911 if Present (Comp_Type)
6912 and then Etype (Subprog) /= Comp_Type
6913 then
6914 Add_One_Interp (Subprog, Subp, Comp_Type);
6915 end if;
6916 end if;
6918 if Etype (Call) /= Any_Type then
6919 return Subp;
6920 else
6921 return Empty;
6922 end if;
6923 end Valid_Candidate;
6925 -------------------------------
6926 -- Complete_Object_Operation --
6927 -------------------------------
6929 procedure Complete_Object_Operation
6930 (Call_Node : Node_Id;
6931 Node_To_Replace : Node_Id)
6933 Control : constant Entity_Id := First_Formal (Entity (Subprog));
6934 Formal_Type : constant Entity_Id := Etype (Control);
6935 First_Actual : Node_Id;
6937 begin
6938 -- Place the name of the operation, with its interpretations,
6939 -- on the rewritten call.
6941 Set_Name (Call_Node, Subprog);
6943 First_Actual := First (Parameter_Associations (Call_Node));
6945 -- For cross-reference purposes, treat the new node as being in
6946 -- the source if the original one is. Set entity and type, even
6947 -- though they may be overwritten during resolution if overloaded.
6949 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
6950 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
6952 if Nkind (N) = N_Selected_Component
6953 and then not Inside_A_Generic
6954 then
6955 Set_Entity (Selector_Name (N), Entity (Subprog));
6956 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
6957 end if;
6959 -- If need be, rewrite first actual as an explicit dereference
6960 -- If the call is overloaded, the rewriting can only be done
6961 -- once the primitive operation is identified.
6963 if Is_Overloaded (Subprog) then
6965 -- The prefix itself may be overloaded, and its interpretations
6966 -- must be propagated to the new actual in the call.
6968 if Is_Overloaded (Obj) then
6969 Save_Interps (Obj, First_Actual);
6970 end if;
6972 Rewrite (First_Actual, Obj);
6974 elsif not Is_Access_Type (Formal_Type)
6975 and then Is_Access_Type (Etype (Obj))
6976 then
6977 Rewrite (First_Actual,
6978 Make_Explicit_Dereference (Sloc (Obj), Obj));
6979 Analyze (First_Actual);
6981 -- If we need to introduce an explicit dereference, verify that
6982 -- the resulting actual is compatible with the mode of the formal.
6984 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
6985 and then Is_Access_Constant (Etype (Obj))
6986 then
6987 Error_Msg_NE
6988 ("expect variable in call to&", Prefix (N), Entity (Subprog));
6989 end if;
6991 -- Conversely, if the formal is an access parameter and the object
6992 -- is not, replace the actual with a 'Access reference. Its analysis
6993 -- will check that the object is aliased.
6995 elsif Is_Access_Type (Formal_Type)
6996 and then not Is_Access_Type (Etype (Obj))
6997 then
6998 -- A special case: A.all'access is illegal if A is an access to a
6999 -- constant and the context requires an access to a variable.
7001 if not Is_Access_Constant (Formal_Type) then
7002 if (Nkind (Obj) = N_Explicit_Dereference
7003 and then Is_Access_Constant (Etype (Prefix (Obj))))
7004 or else not Is_Variable (Obj)
7005 then
7006 Error_Msg_NE
7007 ("actual for& must be a variable", Obj, Control);
7008 end if;
7009 end if;
7011 Rewrite (First_Actual,
7012 Make_Attribute_Reference (Loc,
7013 Attribute_Name => Name_Access,
7014 Prefix => Relocate_Node (Obj)));
7016 if not Is_Aliased_View (Obj) then
7017 Error_Msg_NE
7018 ("object in prefixed call to& must be aliased"
7019 & " (RM-2005 4.3.1 (13))",
7020 Prefix (First_Actual), Subprog);
7021 end if;
7023 Analyze (First_Actual);
7025 else
7026 if Is_Overloaded (Obj) then
7027 Save_Interps (Obj, First_Actual);
7028 end if;
7030 Rewrite (First_Actual, Obj);
7031 end if;
7033 Rewrite (Node_To_Replace, Call_Node);
7035 -- Propagate the interpretations collected in subprog to the new
7036 -- function call node, to be resolved from context.
7038 if Is_Overloaded (Subprog) then
7039 Save_Interps (Subprog, Node_To_Replace);
7041 else
7042 Analyze (Node_To_Replace);
7044 -- If the operation has been rewritten into a call, which may get
7045 -- subsequently an explicit dereference, preserve the type on the
7046 -- original node (selected component or indexed component) for
7047 -- subsequent legality tests, e.g. Is_Variable. which examines
7048 -- the original node.
7050 if Nkind (Node_To_Replace) = N_Function_Call then
7051 Set_Etype
7052 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
7053 end if;
7054 end if;
7055 end Complete_Object_Operation;
7057 ----------------------
7058 -- Report_Ambiguity --
7059 ----------------------
7061 procedure Report_Ambiguity (Op : Entity_Id) is
7062 Access_Actual : constant Boolean :=
7063 Is_Access_Type (Etype (Prefix (N)));
7064 Access_Formal : Boolean := False;
7066 begin
7067 Error_Msg_Sloc := Sloc (Op);
7069 if Present (First_Formal (Op)) then
7070 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
7071 end if;
7073 if Access_Formal and then not Access_Actual then
7074 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7075 Error_Msg_N
7076 ("\possible interpretation"
7077 & " (inherited, with implicit 'Access) #", N);
7078 else
7079 Error_Msg_N
7080 ("\possible interpretation (with implicit 'Access) #", N);
7081 end if;
7083 elsif not Access_Formal and then Access_Actual then
7084 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7085 Error_Msg_N
7086 ("\possible interpretation"
7087 & " ( inherited, with implicit dereference) #", N);
7088 else
7089 Error_Msg_N
7090 ("\possible interpretation (with implicit dereference) #", N);
7091 end if;
7093 else
7094 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7095 Error_Msg_N ("\possible interpretation (inherited)#", N);
7096 else
7097 Error_Msg_N -- CODEFIX
7098 ("\possible interpretation#", N);
7099 end if;
7100 end if;
7101 end Report_Ambiguity;
7103 --------------------------------
7104 -- Transform_Object_Operation --
7105 --------------------------------
7107 procedure Transform_Object_Operation
7108 (Call_Node : out Node_Id;
7109 Node_To_Replace : out Node_Id)
7111 Dummy : constant Node_Id := New_Copy (Obj);
7112 -- Placeholder used as a first parameter in the call, replaced
7113 -- eventually by the proper object.
7115 Parent_Node : constant Node_Id := Parent (N);
7117 Actual : Node_Id;
7118 Actuals : List_Id;
7120 begin
7121 -- Common case covering 1) Call to a procedure and 2) Call to a
7122 -- function that has some additional actuals.
7124 if Nkind (Parent_Node) in N_Subprogram_Call
7126 -- N is a selected component node containing the name of the
7127 -- subprogram. If N is not the name of the parent node we must
7128 -- not replace the parent node by the new construct. This case
7129 -- occurs when N is a parameterless call to a subprogram that
7130 -- is an actual parameter of a call to another subprogram. For
7131 -- example:
7132 -- Some_Subprogram (..., Obj.Operation, ...)
7134 and then Name (Parent_Node) = N
7135 then
7136 Node_To_Replace := Parent_Node;
7138 Actuals := Parameter_Associations (Parent_Node);
7140 if Present (Actuals) then
7141 Prepend (Dummy, Actuals);
7142 else
7143 Actuals := New_List (Dummy);
7144 end if;
7146 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
7147 Call_Node :=
7148 Make_Procedure_Call_Statement (Loc,
7149 Name => New_Copy (Subprog),
7150 Parameter_Associations => Actuals);
7152 else
7153 Call_Node :=
7154 Make_Function_Call (Loc,
7155 Name => New_Copy (Subprog),
7156 Parameter_Associations => Actuals);
7158 end if;
7160 -- Before analysis, a function call appears as an indexed component
7161 -- if there are no named associations.
7163 elsif Nkind (Parent_Node) = N_Indexed_Component
7164 and then N = Prefix (Parent_Node)
7165 then
7166 Node_To_Replace := Parent_Node;
7167 Actuals := Expressions (Parent_Node);
7169 Actual := First (Actuals);
7170 while Present (Actual) loop
7171 Analyze (Actual);
7172 Next (Actual);
7173 end loop;
7175 Prepend (Dummy, Actuals);
7177 Call_Node :=
7178 Make_Function_Call (Loc,
7179 Name => New_Copy (Subprog),
7180 Parameter_Associations => Actuals);
7182 -- Parameterless call: Obj.F is rewritten as F (Obj)
7184 else
7185 Node_To_Replace := N;
7187 Call_Node :=
7188 Make_Function_Call (Loc,
7189 Name => New_Copy (Subprog),
7190 Parameter_Associations => New_List (Dummy));
7191 end if;
7192 end Transform_Object_Operation;
7194 ------------------------------
7195 -- Try_Class_Wide_Operation --
7196 ------------------------------
7198 function Try_Class_Wide_Operation
7199 (Call_Node : Node_Id;
7200 Node_To_Replace : Node_Id) return Boolean
7202 Anc_Type : Entity_Id;
7203 Matching_Op : Entity_Id := Empty;
7204 Error : Boolean;
7206 procedure Traverse_Homonyms
7207 (Anc_Type : Entity_Id;
7208 Error : out Boolean);
7209 -- Traverse the homonym chain of the subprogram searching for those
7210 -- homonyms whose first formal has the Anc_Type's class-wide type,
7211 -- or an anonymous access type designating the class-wide type. If
7212 -- an ambiguity is detected, then Error is set to True.
7214 procedure Traverse_Interfaces
7215 (Anc_Type : Entity_Id;
7216 Error : out Boolean);
7217 -- Traverse the list of interfaces, if any, associated with Anc_Type
7218 -- and search for acceptable class-wide homonyms associated with each
7219 -- interface. If an ambiguity is detected, then Error is set to True.
7221 -----------------------
7222 -- Traverse_Homonyms --
7223 -----------------------
7225 procedure Traverse_Homonyms
7226 (Anc_Type : Entity_Id;
7227 Error : out Boolean)
7229 Cls_Type : Entity_Id;
7230 Hom : Entity_Id;
7231 Hom_Ref : Node_Id;
7232 Success : Boolean;
7234 begin
7235 Error := False;
7237 Cls_Type := Class_Wide_Type (Anc_Type);
7239 Hom := Current_Entity (Subprog);
7241 -- Find a non-hidden operation whose first parameter is of the
7242 -- class-wide type, a subtype thereof, or an anonymous access
7243 -- to same. If in an instance, the operation can be considered
7244 -- even if hidden (it may be hidden because the instantiation is
7245 -- expanded after the containing package has been analyzed).
7247 while Present (Hom) loop
7248 if Ekind_In (Hom, E_Procedure, E_Function)
7249 and then (not Is_Hidden (Hom) or else In_Instance)
7250 and then Scope (Hom) = Scope (Anc_Type)
7251 and then Present (First_Formal (Hom))
7252 and then
7253 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7254 or else
7255 (Is_Access_Type (Etype (First_Formal (Hom)))
7256 and then
7257 Ekind (Etype (First_Formal (Hom))) =
7258 E_Anonymous_Access_Type
7259 and then
7260 Base_Type
7261 (Designated_Type (Etype (First_Formal (Hom)))) =
7262 Cls_Type))
7263 then
7264 -- If the context is a procedure call, ignore functions
7265 -- in the name of the call.
7267 if Ekind (Hom) = E_Function
7268 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7269 and then N = Name (Parent (N))
7270 then
7271 goto Next_Hom;
7273 -- If the context is a function call, ignore procedures
7274 -- in the name of the call.
7276 elsif Ekind (Hom) = E_Procedure
7277 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
7278 then
7279 goto Next_Hom;
7280 end if;
7282 Set_Etype (Call_Node, Any_Type);
7283 Set_Is_Overloaded (Call_Node, False);
7284 Success := False;
7286 if No (Matching_Op) then
7287 Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
7288 Set_Etype (Call_Node, Any_Type);
7289 Set_Parent (Call_Node, Parent (Node_To_Replace));
7291 Set_Name (Call_Node, Hom_Ref);
7293 Analyze_One_Call
7294 (N => Call_Node,
7295 Nam => Hom,
7296 Report => Report_Error,
7297 Success => Success,
7298 Skip_First => True);
7300 Matching_Op :=
7301 Valid_Candidate (Success, Call_Node, Hom);
7303 else
7304 Analyze_One_Call
7305 (N => Call_Node,
7306 Nam => Hom,
7307 Report => Report_Error,
7308 Success => Success,
7309 Skip_First => True);
7311 if Present (Valid_Candidate (Success, Call_Node, Hom))
7312 and then Nkind (Call_Node) /= N_Function_Call
7313 then
7314 Error_Msg_NE ("ambiguous call to&", N, Hom);
7315 Report_Ambiguity (Matching_Op);
7316 Report_Ambiguity (Hom);
7317 Error := True;
7318 return;
7319 end if;
7320 end if;
7321 end if;
7323 <<Next_Hom>>
7324 Hom := Homonym (Hom);
7325 end loop;
7326 end Traverse_Homonyms;
7328 -------------------------
7329 -- Traverse_Interfaces --
7330 -------------------------
7332 procedure Traverse_Interfaces
7333 (Anc_Type : Entity_Id;
7334 Error : out Boolean)
7336 Intface_List : constant List_Id :=
7337 Abstract_Interface_List (Anc_Type);
7338 Intface : Node_Id;
7340 begin
7341 Error := False;
7343 if Is_Non_Empty_List (Intface_List) then
7344 Intface := First (Intface_List);
7345 while Present (Intface) loop
7347 -- Look for acceptable class-wide homonyms associated with
7348 -- the interface.
7350 Traverse_Homonyms (Etype (Intface), Error);
7352 if Error then
7353 return;
7354 end if;
7356 -- Continue the search by looking at each of the interface's
7357 -- associated interface ancestors.
7359 Traverse_Interfaces (Etype (Intface), Error);
7361 if Error then
7362 return;
7363 end if;
7365 Next (Intface);
7366 end loop;
7367 end if;
7368 end Traverse_Interfaces;
7370 -- Start of processing for Try_Class_Wide_Operation
7372 begin
7373 -- If we are searching only for conflicting class-wide subprograms
7374 -- then initialize directly Matching_Op with the target entity.
7376 if CW_Test_Only then
7377 Matching_Op := Entity (Selector_Name (N));
7378 end if;
7380 -- Loop through ancestor types (including interfaces), traversing
7381 -- the homonym chain of the subprogram, trying out those homonyms
7382 -- whose first formal has the class-wide type of the ancestor, or
7383 -- an anonymous access type designating the class-wide type.
7385 Anc_Type := Obj_Type;
7386 loop
7387 -- Look for a match among homonyms associated with the ancestor
7389 Traverse_Homonyms (Anc_Type, Error);
7391 if Error then
7392 return True;
7393 end if;
7395 -- Continue the search for matches among homonyms associated with
7396 -- any interfaces implemented by the ancestor.
7398 Traverse_Interfaces (Anc_Type, Error);
7400 if Error then
7401 return True;
7402 end if;
7404 exit when Etype (Anc_Type) = Anc_Type;
7405 Anc_Type := Etype (Anc_Type);
7406 end loop;
7408 if Present (Matching_Op) then
7409 Set_Etype (Call_Node, Etype (Matching_Op));
7410 end if;
7412 return Present (Matching_Op);
7413 end Try_Class_Wide_Operation;
7415 -----------------------------------
7416 -- Try_One_Prefix_Interpretation --
7417 -----------------------------------
7419 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7420 begin
7421 Obj_Type := T;
7423 if Is_Access_Type (Obj_Type) then
7424 Obj_Type := Designated_Type (Obj_Type);
7425 end if;
7427 if Ekind (Obj_Type) = E_Private_Subtype then
7428 Obj_Type := Base_Type (Obj_Type);
7429 end if;
7431 if Is_Class_Wide_Type (Obj_Type) then
7432 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7433 end if;
7435 -- The type may have be obtained through a limited_with clause,
7436 -- in which case the primitive operations are available on its
7437 -- non-limited view. If still incomplete, retrieve full view.
7439 if Ekind (Obj_Type) = E_Incomplete_Type
7440 and then From_With_Type (Obj_Type)
7441 then
7442 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7443 end if;
7445 -- If the object is not tagged, or the type is still an incomplete
7446 -- type, this is not a prefixed call.
7448 if not Is_Tagged_Type (Obj_Type)
7449 or else Is_Incomplete_Type (Obj_Type)
7450 then
7451 return;
7452 end if;
7454 declare
7455 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
7456 CW_Result : Boolean;
7457 Prim_Result : Boolean;
7458 pragma Unreferenced (CW_Result);
7460 begin
7461 if not CW_Test_Only then
7462 Prim_Result :=
7463 Try_Primitive_Operation
7464 (Call_Node => New_Call_Node,
7465 Node_To_Replace => Node_To_Replace);
7466 end if;
7468 -- Check if there is a class-wide subprogram covering the
7469 -- primitive. This check must be done even if a candidate
7470 -- was found in order to report ambiguous calls.
7472 if not (Prim_Result) then
7473 CW_Result :=
7474 Try_Class_Wide_Operation
7475 (Call_Node => New_Call_Node,
7476 Node_To_Replace => Node_To_Replace);
7478 -- If we found a primitive we search for class-wide subprograms
7479 -- using a duplicate of the call node (done to avoid missing its
7480 -- decoration if there is no ambiguity).
7482 else
7483 CW_Result :=
7484 Try_Class_Wide_Operation
7485 (Call_Node => Dup_Call_Node,
7486 Node_To_Replace => Node_To_Replace);
7487 end if;
7488 end;
7489 end Try_One_Prefix_Interpretation;
7491 -----------------------------
7492 -- Try_Primitive_Operation --
7493 -----------------------------
7495 function Try_Primitive_Operation
7496 (Call_Node : Node_Id;
7497 Node_To_Replace : Node_Id) return Boolean
7499 Elmt : Elmt_Id;
7500 Prim_Op : Entity_Id;
7501 Matching_Op : Entity_Id := Empty;
7502 Prim_Op_Ref : Node_Id := Empty;
7504 Corr_Type : Entity_Id := Empty;
7505 -- If the prefix is a synchronized type, the controlling type of
7506 -- the primitive operation is the corresponding record type, else
7507 -- this is the object type itself.
7509 Success : Boolean := False;
7511 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
7512 -- For tagged types the candidate interpretations are found in
7513 -- the list of primitive operations of the type and its ancestors.
7514 -- For formal tagged types we have to find the operations declared
7515 -- in the same scope as the type (including in the generic formal
7516 -- part) because the type itself carries no primitive operations,
7517 -- except for formal derived types that inherit the operations of
7518 -- the parent and progenitors.
7519 -- If the context is a generic subprogram body, the generic formals
7520 -- are visible by name, but are not in the entity list of the
7521 -- subprogram because that list starts with the subprogram formals.
7522 -- We retrieve the candidate operations from the generic declaration.
7524 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
7525 -- An operation that overrides an inherited operation in the private
7526 -- part of its package may be hidden, but if the inherited operation
7527 -- is visible a direct call to it will dispatch to the private one,
7528 -- which is therefore a valid candidate.
7530 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
7531 -- Verify that the prefix, dereferenced if need be, is a valid
7532 -- controlling argument in a call to Op. The remaining actuals
7533 -- are checked in the subsequent call to Analyze_One_Call.
7535 ------------------------------
7536 -- Collect_Generic_Type_Ops --
7537 ------------------------------
7539 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
7540 Bas : constant Entity_Id := Base_Type (T);
7541 Candidates : constant Elist_Id := New_Elmt_List;
7542 Subp : Entity_Id;
7543 Formal : Entity_Id;
7545 procedure Check_Candidate;
7546 -- The operation is a candidate if its first parameter is a
7547 -- controlling operand of the desired type.
7549 -----------------------
7550 -- Check_Candidate; --
7551 -----------------------
7553 procedure Check_Candidate is
7554 begin
7555 Formal := First_Formal (Subp);
7557 if Present (Formal)
7558 and then Is_Controlling_Formal (Formal)
7559 and then
7560 (Base_Type (Etype (Formal)) = Bas
7561 or else
7562 (Is_Access_Type (Etype (Formal))
7563 and then Designated_Type (Etype (Formal)) = Bas))
7564 then
7565 Append_Elmt (Subp, Candidates);
7566 end if;
7567 end Check_Candidate;
7569 -- Start of processing for Collect_Generic_Type_Ops
7571 begin
7572 if Is_Derived_Type (T) then
7573 return Primitive_Operations (T);
7575 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
7577 -- Scan the list of generic formals to find subprograms
7578 -- that may have a first controlling formal of the type.
7580 if Nkind (Unit_Declaration_Node (Scope (T)))
7581 = N_Generic_Subprogram_Declaration
7582 then
7583 declare
7584 Decl : Node_Id;
7586 begin
7587 Decl :=
7588 First (Generic_Formal_Declarations
7589 (Unit_Declaration_Node (Scope (T))));
7590 while Present (Decl) loop
7591 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
7592 Subp := Defining_Entity (Decl);
7593 Check_Candidate;
7594 end if;
7596 Next (Decl);
7597 end loop;
7598 end;
7599 end if;
7600 return Candidates;
7602 else
7603 -- Scan the list of entities declared in the same scope as
7604 -- the type. In general this will be an open scope, given that
7605 -- the call we are analyzing can only appear within a generic
7606 -- declaration or body (either the one that declares T, or a
7607 -- child unit).
7609 -- For a subtype representing a generic actual type, go to the
7610 -- base type.
7612 if Is_Generic_Actual_Type (T) then
7613 Subp := First_Entity (Scope (Base_Type (T)));
7614 else
7615 Subp := First_Entity (Scope (T));
7616 end if;
7618 while Present (Subp) loop
7619 if Is_Overloadable (Subp) then
7620 Check_Candidate;
7621 end if;
7623 Next_Entity (Subp);
7624 end loop;
7626 return Candidates;
7627 end if;
7628 end Collect_Generic_Type_Ops;
7630 ---------------------------
7631 -- Is_Private_Overriding --
7632 ---------------------------
7634 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
7635 Visible_Op : constant Entity_Id := Homonym (Op);
7637 begin
7638 return Present (Visible_Op)
7639 and then Scope (Op) = Scope (Visible_Op)
7640 and then not Comes_From_Source (Visible_Op)
7641 and then Alias (Visible_Op) = Op
7642 and then not Is_Hidden (Visible_Op);
7643 end Is_Private_Overriding;
7645 -----------------------------
7646 -- Valid_First_Argument_Of --
7647 -----------------------------
7649 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
7650 Typ : Entity_Id := Etype (First_Formal (Op));
7652 begin
7653 if Is_Concurrent_Type (Typ)
7654 and then Present (Corresponding_Record_Type (Typ))
7655 then
7656 Typ := Corresponding_Record_Type (Typ);
7657 end if;
7659 -- Simple case. Object may be a subtype of the tagged type or
7660 -- may be the corresponding record of a synchronized type.
7662 return Obj_Type = Typ
7663 or else Base_Type (Obj_Type) = Typ
7664 or else Corr_Type = Typ
7666 -- Prefix can be dereferenced
7668 or else
7669 (Is_Access_Type (Corr_Type)
7670 and then Designated_Type (Corr_Type) = Typ)
7672 -- Formal is an access parameter, for which the object
7673 -- can provide an access.
7675 or else
7676 (Ekind (Typ) = E_Anonymous_Access_Type
7677 and then
7678 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
7679 end Valid_First_Argument_Of;
7681 -- Start of processing for Try_Primitive_Operation
7683 begin
7684 -- Look for subprograms in the list of primitive operations. The name
7685 -- must be identical, and the kind of call indicates the expected
7686 -- kind of operation (function or procedure). If the type is a
7687 -- (tagged) synchronized type, the primitive ops are attached to the
7688 -- corresponding record (base) type.
7690 if Is_Concurrent_Type (Obj_Type) then
7691 if Present (Corresponding_Record_Type (Obj_Type)) then
7692 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
7693 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
7694 else
7695 Corr_Type := Obj_Type;
7696 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7697 end if;
7699 elsif not Is_Generic_Type (Obj_Type) then
7700 Corr_Type := Obj_Type;
7701 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
7703 else
7704 Corr_Type := Obj_Type;
7705 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7706 end if;
7708 while Present (Elmt) loop
7709 Prim_Op := Node (Elmt);
7711 if Chars (Prim_Op) = Chars (Subprog)
7712 and then Present (First_Formal (Prim_Op))
7713 and then Valid_First_Argument_Of (Prim_Op)
7714 and then
7715 (Nkind (Call_Node) = N_Function_Call)
7716 = (Ekind (Prim_Op) = E_Function)
7717 then
7718 -- Ada 2005 (AI-251): If this primitive operation corresponds
7719 -- with an immediate ancestor interface there is no need to add
7720 -- it to the list of interpretations; the corresponding aliased
7721 -- primitive is also in this list of primitive operations and
7722 -- will be used instead.
7724 if (Present (Interface_Alias (Prim_Op))
7725 and then Is_Ancestor (Find_Dispatching_Type
7726 (Alias (Prim_Op)), Corr_Type))
7728 -- Do not consider hidden primitives unless the type is in an
7729 -- open scope or we are within an instance, where visibility
7730 -- is known to be correct, or else if this is an overriding
7731 -- operation in the private part for an inherited operation.
7733 or else (Is_Hidden (Prim_Op)
7734 and then not Is_Immediately_Visible (Obj_Type)
7735 and then not In_Instance
7736 and then not Is_Private_Overriding (Prim_Op))
7737 then
7738 goto Continue;
7739 end if;
7741 Set_Etype (Call_Node, Any_Type);
7742 Set_Is_Overloaded (Call_Node, False);
7744 if No (Matching_Op) then
7745 Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
7746 Candidate := Prim_Op;
7748 Set_Parent (Call_Node, Parent (Node_To_Replace));
7750 Set_Name (Call_Node, Prim_Op_Ref);
7751 Success := False;
7753 Analyze_One_Call
7754 (N => Call_Node,
7755 Nam => Prim_Op,
7756 Report => Report_Error,
7757 Success => Success,
7758 Skip_First => True);
7760 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
7762 -- More than one interpretation, collect for subsequent
7763 -- disambiguation. If this is a procedure call and there
7764 -- is another match, report ambiguity now.
7766 else
7767 Analyze_One_Call
7768 (N => Call_Node,
7769 Nam => Prim_Op,
7770 Report => Report_Error,
7771 Success => Success,
7772 Skip_First => True);
7774 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
7775 and then Nkind (Call_Node) /= N_Function_Call
7776 then
7777 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
7778 Report_Ambiguity (Matching_Op);
7779 Report_Ambiguity (Prim_Op);
7780 return True;
7781 end if;
7782 end if;
7783 end if;
7785 <<Continue>>
7786 Next_Elmt (Elmt);
7787 end loop;
7789 if Present (Matching_Op) then
7790 Set_Etype (Call_Node, Etype (Matching_Op));
7791 end if;
7793 return Present (Matching_Op);
7794 end Try_Primitive_Operation;
7796 -- Start of processing for Try_Object_Operation
7798 begin
7799 Analyze_Expression (Obj);
7801 -- Analyze the actuals if node is known to be a subprogram call
7803 if Is_Subprg_Call and then N = Name (Parent (N)) then
7804 Actual := First (Parameter_Associations (Parent (N)));
7805 while Present (Actual) loop
7806 Analyze_Expression (Actual);
7807 Next (Actual);
7808 end loop;
7809 end if;
7811 -- Build a subprogram call node, using a copy of Obj as its first
7812 -- actual. This is a placeholder, to be replaced by an explicit
7813 -- dereference when needed.
7815 Transform_Object_Operation
7816 (Call_Node => New_Call_Node,
7817 Node_To_Replace => Node_To_Replace);
7819 Set_Etype (New_Call_Node, Any_Type);
7820 Set_Etype (Subprog, Any_Type);
7821 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
7823 if not Is_Overloaded (Obj) then
7824 Try_One_Prefix_Interpretation (Obj_Type);
7826 else
7827 declare
7828 I : Interp_Index;
7829 It : Interp;
7830 begin
7831 Get_First_Interp (Obj, I, It);
7832 while Present (It.Nam) loop
7833 Try_One_Prefix_Interpretation (It.Typ);
7834 Get_Next_Interp (I, It);
7835 end loop;
7836 end;
7837 end if;
7839 if Etype (New_Call_Node) /= Any_Type then
7841 -- No need to complete the tree transformations if we are only
7842 -- searching for conflicting class-wide subprograms
7844 if CW_Test_Only then
7845 return False;
7846 else
7847 Complete_Object_Operation
7848 (Call_Node => New_Call_Node,
7849 Node_To_Replace => Node_To_Replace);
7850 return True;
7851 end if;
7853 elsif Present (Candidate) then
7855 -- The argument list is not type correct. Re-analyze with error
7856 -- reporting enabled, and use one of the possible candidates.
7857 -- In All_Errors_Mode, re-analyze all failed interpretations.
7859 if All_Errors_Mode then
7860 Report_Error := True;
7861 if Try_Primitive_Operation
7862 (Call_Node => New_Call_Node,
7863 Node_To_Replace => Node_To_Replace)
7865 or else
7866 Try_Class_Wide_Operation
7867 (Call_Node => New_Call_Node,
7868 Node_To_Replace => Node_To_Replace)
7869 then
7870 null;
7871 end if;
7873 else
7874 Analyze_One_Call
7875 (N => New_Call_Node,
7876 Nam => Candidate,
7877 Report => True,
7878 Success => Success,
7879 Skip_First => True);
7880 end if;
7882 -- No need for further errors
7884 return True;
7886 else
7887 -- There was no candidate operation, so report it as an error
7888 -- in the caller: Analyze_Selected_Component.
7890 return False;
7891 end if;
7892 end Try_Object_Operation;
7894 ---------
7895 -- wpo --
7896 ---------
7898 procedure wpo (T : Entity_Id) is
7899 Op : Entity_Id;
7900 E : Elmt_Id;
7902 begin
7903 if not Is_Tagged_Type (T) then
7904 return;
7905 end if;
7907 E := First_Elmt (Primitive_Operations (Base_Type (T)));
7908 while Present (E) loop
7909 Op := Node (E);
7910 Write_Int (Int (Op));
7911 Write_Str (" === ");
7912 Write_Name (Chars (Op));
7913 Write_Str (" in ");
7914 Write_Name (Chars (Scope (Op)));
7915 Next_Elmt (E);
7916 Write_Eol;
7917 end loop;
7918 end wpo;
7920 end Sem_Ch4;