cap frequency in make_forwarder_block
[official-gcc.git] / gcc / ada / sem_ch4.adb
blob1d33d1b2ce2dadb18fc01e849469d3bd81885a10
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
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. If
78 -- the expression is a name, it may be a call to a parameterless function,
79 -- and if so must be converted into an explicit call node and analyzed as
80 -- such. This deproceduring must be done during the first pass of overload
81 -- resolution, because otherwise a procedure call with overloaded actuals
82 -- 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 is an
86 -- operator name or an expanded name whose selector is an operator name,
87 -- 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 message if Sel seems 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 consistent
151 -- pairs of interpretations for L and R that have a numeric type consistent
152 -- 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 pairs of
159 -- interpretations for L and R.
161 procedure Find_Concatenation_Types
162 (L, R : Node_Id;
163 Op_Id : Entity_Id;
164 N : Node_Id);
165 -- For the four varieties of concatenation
167 procedure Find_Equality_Types
168 (L, R : Node_Id;
169 Op_Id : Entity_Id;
170 N : Node_Id);
171 -- Ditto for equality operators
173 procedure Find_Boolean_Types
174 (L, R : Node_Id;
175 Op_Id : Entity_Id;
176 N : Node_Id);
177 -- Ditto for binary logical operations
179 procedure Find_Negation_Types
180 (R : Node_Id;
181 Op_Id : Entity_Id;
182 N : Node_Id);
183 -- Find consistent interpretation for operand of negation operator
185 procedure Find_Non_Universal_Interpretations
186 (N : Node_Id;
187 R : Node_Id;
188 Op_Id : Entity_Id;
189 T1 : Entity_Id);
190 -- For equality and comparison operators, the result is always boolean,
191 -- and the legality of the operation is determined from the visibility
192 -- of the operand types. If one of the operands has a universal interpre-
193 -- tation, the legality check uses some compatible non-universal
194 -- interpretation of the other operand. N can be an operator node, or
195 -- a function call whose name is an operator designator. Any_Access, which
196 -- is the initial type of the literal NULL, is a universal type for the
197 -- purpose of this routine.
199 function Find_Primitive_Operation (N : Node_Id) return Boolean;
200 -- Find candidate interpretations for the name Obj.Proc when it appears
201 -- in a subprogram renaming declaration.
203 procedure Find_Unary_Types
204 (R : Node_Id;
205 Op_Id : Entity_Id;
206 N : Node_Id);
207 -- Unary arithmetic types: plus, minus, abs
209 procedure Check_Arithmetic_Pair
210 (T1, T2 : Entity_Id;
211 Op_Id : Entity_Id;
212 N : Node_Id);
213 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
214 -- types for left and right operand. Determine whether they constitute
215 -- a valid pair for the given operator, and record the corresponding
216 -- interpretation of the operator node. The node N may be an operator
217 -- node (the usual case) or a function call whose prefix is an operator
218 -- designator. In both cases Op_Id is the operator name itself.
220 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
221 -- Give detailed information on overloaded call where none of the
222 -- interpretations match. N is the call node, Nam the designator for
223 -- the overloaded entity being called.
225 function Junk_Operand (N : Node_Id) return Boolean;
226 -- Test for an operand that is an inappropriate entity (e.g. a package
227 -- name or a label). If so, issue an error message and return True. If
228 -- the operand is not an inappropriate entity kind, return False.
230 procedure Operator_Check (N : Node_Id);
231 -- Verify that an operator has received some valid interpretation. If none
232 -- was found, determine whether a use clause would make the operation
233 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
234 -- every type compatible with the operator, even if the operator for the
235 -- type is not directly visible. The routine uses this type to emit a more
236 -- informative message.
238 function Process_Implicit_Dereference_Prefix
239 (E : Entity_Id;
240 P : Node_Id) return Entity_Id;
241 -- Called when P is the prefix of an implicit dereference, denoting an
242 -- object E. The function returns the designated type of the prefix, taking
243 -- into account that the designated type of an anonymous access type may be
244 -- a limited view, when the non-limited view is visible.
245 -- If in semantics only mode (-gnatc or generic), the function also records
246 -- that the prefix is a reference to E, if any. Normally, such a reference
247 -- is generated only when the implicit dereference is expanded into an
248 -- explicit one, but for consistency we must generate the reference when
249 -- expansion is disabled as well.
251 procedure Remove_Abstract_Operations (N : Node_Id);
252 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
253 -- operation is not a candidate interpretation.
255 function Try_Container_Indexing
256 (N : Node_Id;
257 Prefix : Node_Id;
258 Exprs : List_Id) return Boolean;
259 -- AI05-0139: Generalized indexing to support iterators over containers
261 function Try_Indexed_Call
262 (N : Node_Id;
263 Nam : Entity_Id;
264 Typ : Entity_Id;
265 Skip_First : Boolean) return Boolean;
266 -- If a function has defaults for all its actuals, a call to it may in fact
267 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
268 -- interpretation as an indexing, prior to analysis as a call. If both are
269 -- possible, the node is overloaded with both interpretations (same symbol
270 -- but two different types). If the call is written in prefix form, the
271 -- prefix becomes the first parameter in the call, and only the remaining
272 -- actuals must be checked for the presence of defaults.
274 function Try_Indirect_Call
275 (N : Node_Id;
276 Nam : Entity_Id;
277 Typ : Entity_Id) return Boolean;
278 -- Similarly, a function F that needs no actuals can return an access to a
279 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
280 -- the call may be overloaded with both interpretations.
282 function Try_Object_Operation
283 (N : Node_Id;
284 CW_Test_Only : Boolean := False) return Boolean;
285 -- Ada 2005 (AI-252): Support the object.operation notation. If node N
286 -- is a call in this notation, it is transformed into a normal subprogram
287 -- call where the prefix is a parameter, and True is returned. If node
288 -- N is not of this form, it is unchanged, and False is returned. if
289 -- CW_Test_Only is true then N is an N_Selected_Component node which
290 -- is part of a call to an entry or procedure of a tagged concurrent
291 -- type and this routine is invoked to search for class-wide subprograms
292 -- conflicting with the target entity.
294 procedure wpo (T : Entity_Id);
295 pragma Warnings (Off, wpo);
296 -- Used for debugging: obtain list of primitive operations even if
297 -- type is not frozen and dispatch table is not built yet.
299 ------------------------
300 -- Ambiguous_Operands --
301 ------------------------
303 procedure Ambiguous_Operands (N : Node_Id) is
304 procedure List_Operand_Interps (Opnd : Node_Id);
306 --------------------------
307 -- List_Operand_Interps --
308 --------------------------
310 procedure List_Operand_Interps (Opnd : Node_Id) is
311 Nam : Node_Id;
312 Err : Node_Id := N;
314 begin
315 if Is_Overloaded (Opnd) then
316 if Nkind (Opnd) in N_Op then
317 Nam := Opnd;
318 elsif Nkind (Opnd) = N_Function_Call then
319 Nam := Name (Opnd);
320 elsif Ada_Version >= Ada_2012 then
321 declare
322 It : Interp;
323 I : Interp_Index;
325 begin
326 Get_First_Interp (Opnd, I, It);
327 while Present (It.Nam) loop
328 if Has_Implicit_Dereference (It.Typ) then
329 Error_Msg_N
330 ("can be interpreted as implicit dereference", Opnd);
331 return;
332 end if;
334 Get_Next_Interp (I, It);
335 end loop;
336 end;
338 return;
339 end if;
341 else
342 return;
343 end if;
345 if Opnd = Left_Opnd (N) then
346 Error_Msg_N ("\left operand has the following interpretations", N);
347 else
348 Error_Msg_N
349 ("\right operand has the following interpretations", N);
350 Err := Opnd;
351 end if;
353 List_Interps (Nam, Err);
354 end List_Operand_Interps;
356 -- Start of processing for Ambiguous_Operands
358 begin
359 if Nkind (N) in N_Membership_Test then
360 Error_Msg_N ("ambiguous operands for membership", N);
362 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
363 Error_Msg_N ("ambiguous operands for equality", N);
365 else
366 Error_Msg_N ("ambiguous operands for comparison", N);
367 end if;
369 if All_Errors_Mode then
370 List_Operand_Interps (Left_Opnd (N));
371 List_Operand_Interps (Right_Opnd (N));
372 else
373 Error_Msg_N ("\use -gnatf switch for details", N);
374 end if;
375 end Ambiguous_Operands;
377 -----------------------
378 -- Analyze_Aggregate --
379 -----------------------
381 -- Most of the analysis of Aggregates requires that the type be known,
382 -- and is therefore put off until resolution.
384 procedure Analyze_Aggregate (N : Node_Id) is
385 begin
386 if No (Etype (N)) then
387 Set_Etype (N, Any_Composite);
388 end if;
389 end Analyze_Aggregate;
391 -----------------------
392 -- Analyze_Allocator --
393 -----------------------
395 procedure Analyze_Allocator (N : Node_Id) is
396 Loc : constant Source_Ptr := Sloc (N);
397 Sav_Errs : constant Nat := Serious_Errors_Detected;
398 E : Node_Id := Expression (N);
399 Acc_Type : Entity_Id;
400 Type_Id : Entity_Id;
401 P : Node_Id;
402 C : Node_Id;
403 Onode : Node_Id;
405 begin
406 Check_SPARK_05_Restriction ("allocator is not allowed", N);
408 -- Deal with allocator restrictions
410 -- In accordance with H.4(7), the No_Allocators restriction only applies
411 -- to user-written allocators. The same consideration applies to the
412 -- No_Standard_Allocators_Before_Elaboration restriction.
414 if Comes_From_Source (N) then
415 Check_Restriction (No_Allocators, N);
417 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
418 -- look at enclosing context, checking task/main subprogram case.
420 C := N;
421 P := Parent (C);
422 while Present (P) loop
424 -- For the task case we need a handled sequence of statements,
425 -- where the occurrence of the allocator is within the statements
426 -- and the parent is a task body
428 if Nkind (P) = N_Handled_Sequence_Of_Statements
429 and then Is_List_Member (C)
430 and then List_Containing (C) = Statements (P)
431 then
432 Onode := Original_Node (Parent (P));
434 -- Check for allocator within task body, this is a definite
435 -- violation of No_Allocators_After_Elaboration we can detect
436 -- at compile time.
438 if Nkind (Onode) = N_Task_Body then
439 Check_Restriction
440 (No_Standard_Allocators_After_Elaboration, N);
441 exit;
442 end if;
443 end if;
445 -- The other case is appearance in a subprogram body. This is
446 -- a violation if this is a library level subprogram with no
447 -- parameters. Note that this is now a static error even if the
448 -- subprogram is not the main program (this is a change, in an
449 -- earlier version only the main program was affected, and the
450 -- check had to be done in the binder.
452 if Nkind (P) = N_Subprogram_Body
453 and then Nkind (Parent (P)) = N_Compilation_Unit
454 and then No (Parameter_Specifications (Specification (P)))
455 then
456 Check_Restriction
457 (No_Standard_Allocators_After_Elaboration, N);
458 end if;
460 C := P;
461 P := Parent (C);
462 end loop;
463 end if;
465 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
466 -- any. The expected type for the name is any type. A non-overloading
467 -- rule then requires it to be of a type descended from
468 -- System.Storage_Pools.Subpools.Subpool_Handle.
470 -- This isn't exactly what the AI says, but it seems to be the right
471 -- rule. The AI should be fixed.???
473 declare
474 Subpool : constant Node_Id := Subpool_Handle_Name (N);
476 begin
477 if Present (Subpool) then
478 Analyze (Subpool);
480 if Is_Overloaded (Subpool) then
481 Error_Msg_N ("ambiguous subpool handle", Subpool);
482 end if;
484 -- Check that Etype (Subpool) is descended from Subpool_Handle
486 Resolve (Subpool);
487 end if;
488 end;
490 -- Analyze the qualified expression or subtype indication
492 if Nkind (E) = N_Qualified_Expression then
493 Acc_Type := Create_Itype (E_Allocator_Type, N);
494 Set_Etype (Acc_Type, Acc_Type);
495 Find_Type (Subtype_Mark (E));
497 -- Analyze the qualified expression, and apply the name resolution
498 -- rule given in 4.7(3).
500 Analyze (E);
501 Type_Id := Etype (E);
502 Set_Directly_Designated_Type (Acc_Type, Type_Id);
504 -- Allocators generated by the build-in-place expansion mechanism
505 -- are explicitly marked as coming from source but do not need to be
506 -- checked for limited initialization. To exclude this case, ensure
507 -- that the parent of the allocator is a source node.
509 if Is_Limited_Type (Type_Id)
510 and then Comes_From_Source (N)
511 and then Comes_From_Source (Parent (N))
512 and then not In_Instance_Body
513 then
514 if not OK_For_Limited_Init (Type_Id, Expression (E)) then
515 Error_Msg_N ("initialization not allowed for limited types", N);
516 Explain_Limited_Type (Type_Id, N);
517 end if;
518 end if;
520 -- A qualified expression requires an exact match of the type,
521 -- class-wide matching is not allowed.
523 -- if Is_Class_Wide_Type (Type_Id)
524 -- and then Base_Type
525 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
526 -- then
527 -- Wrong_Type (Expression (E), Type_Id);
528 -- end if;
530 -- We don't analyze the qualified expression itself because it's
531 -- part of the allocator. It is fully analyzed and resolved when
532 -- the allocator is resolved with the context type.
534 Set_Etype (E, Type_Id);
536 -- Case where allocator has a subtype indication
538 else
539 declare
540 Def_Id : Entity_Id;
541 Base_Typ : Entity_Id;
543 begin
544 -- If the allocator includes a N_Subtype_Indication then a
545 -- constraint is present, otherwise the node is a subtype mark.
546 -- Introduce an explicit subtype declaration into the tree
547 -- defining some anonymous subtype and rewrite the allocator to
548 -- use this subtype rather than the subtype indication.
550 -- It is important to introduce the explicit subtype declaration
551 -- so that the bounds of the subtype indication are attached to
552 -- the tree in case the allocator is inside a generic unit.
554 if Nkind (E) = N_Subtype_Indication then
556 -- A constraint is only allowed for a composite type in Ada
557 -- 95. In Ada 83, a constraint is also allowed for an
558 -- access-to-composite type, but the constraint is ignored.
560 Find_Type (Subtype_Mark (E));
561 Base_Typ := Entity (Subtype_Mark (E));
563 if Is_Elementary_Type (Base_Typ) then
564 if not (Ada_Version = Ada_83
565 and then Is_Access_Type (Base_Typ))
566 then
567 Error_Msg_N ("constraint not allowed here", E);
569 if Nkind (Constraint (E)) =
570 N_Index_Or_Discriminant_Constraint
571 then
572 Error_Msg_N -- CODEFIX
573 ("\if qualified expression was meant, " &
574 "use apostrophe", Constraint (E));
575 end if;
576 end if;
578 -- Get rid of the bogus constraint:
580 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
581 Analyze_Allocator (N);
582 return;
583 end if;
585 if Expander_Active then
586 Def_Id := Make_Temporary (Loc, 'S');
588 Insert_Action (E,
589 Make_Subtype_Declaration (Loc,
590 Defining_Identifier => Def_Id,
591 Subtype_Indication => Relocate_Node (E)));
593 if Sav_Errs /= Serious_Errors_Detected
594 and then Nkind (Constraint (E)) =
595 N_Index_Or_Discriminant_Constraint
596 then
597 Error_Msg_N -- CODEFIX
598 ("if qualified expression was meant, "
599 & "use apostrophe!", Constraint (E));
600 end if;
602 E := New_Occurrence_Of (Def_Id, Loc);
603 Rewrite (Expression (N), E);
604 end if;
605 end if;
607 Type_Id := Process_Subtype (E, N);
608 Acc_Type := Create_Itype (E_Allocator_Type, N);
609 Set_Etype (Acc_Type, Acc_Type);
610 Set_Directly_Designated_Type (Acc_Type, Type_Id);
611 Check_Fully_Declared (Type_Id, N);
613 -- Ada 2005 (AI-231): If the designated type is itself an access
614 -- type that excludes null, its default initialization will
615 -- be a null object, and we can insert an unconditional raise
616 -- before the allocator.
618 -- Ada 2012 (AI-104): A not null indication here is altogether
619 -- illegal.
621 if Can_Never_Be_Null (Type_Id) then
622 declare
623 Not_Null_Check : constant Node_Id :=
624 Make_Raise_Constraint_Error (Sloc (E),
625 Reason => CE_Null_Not_Allowed);
627 begin
628 if Expander_Active then
629 Insert_Action (N, Not_Null_Check);
630 Analyze (Not_Null_Check);
632 elsif Warn_On_Ada_2012_Compatibility then
633 Error_Msg_N
634 ("null value not allowed here in Ada 2012?y?", E);
635 end if;
636 end;
637 end if;
639 -- Check for missing initialization. Skip this check if we already
640 -- had errors on analyzing the allocator, since in that case these
641 -- are probably cascaded errors.
643 if Is_Indefinite_Subtype (Type_Id)
644 and then Serious_Errors_Detected = Sav_Errs
645 then
646 -- The build-in-place machinery may produce an allocator when
647 -- the designated type is indefinite but the underlying type is
648 -- not. In this case the unknown discriminants are meaningless
649 -- and should not trigger error messages. Check the parent node
650 -- because the allocator is marked as coming from source.
652 if Present (Underlying_Type (Type_Id))
653 and then not Is_Indefinite_Subtype (Underlying_Type (Type_Id))
654 and then not Comes_From_Source (Parent (N))
655 then
656 null;
658 elsif Is_Class_Wide_Type (Type_Id) then
659 Error_Msg_N
660 ("initialization required in class-wide allocation", N);
662 else
663 if Ada_Version < Ada_2005
664 and then Is_Limited_Type (Type_Id)
665 then
666 Error_Msg_N ("unconstrained allocation not allowed", N);
668 if Is_Array_Type (Type_Id) then
669 Error_Msg_N
670 ("\constraint with array bounds required", N);
672 elsif Has_Unknown_Discriminants (Type_Id) then
673 null;
675 else pragma Assert (Has_Discriminants (Type_Id));
676 Error_Msg_N
677 ("\constraint with discriminant values required", N);
678 end if;
680 -- Limited Ada 2005 and general non-limited case
682 else
683 Error_Msg_N
684 ("uninitialized unconstrained allocation not allowed",
687 if Is_Array_Type (Type_Id) then
688 Error_Msg_N
689 ("\qualified expression or constraint with " &
690 "array bounds required", N);
692 elsif Has_Unknown_Discriminants (Type_Id) then
693 Error_Msg_N ("\qualified expression required", N);
695 else pragma Assert (Has_Discriminants (Type_Id));
696 Error_Msg_N
697 ("\qualified expression or constraint with " &
698 "discriminant values required", N);
699 end if;
700 end if;
701 end if;
702 end if;
703 end;
704 end if;
706 if Is_Abstract_Type (Type_Id) then
707 Error_Msg_N ("cannot allocate abstract object", E);
708 end if;
710 if Has_Task (Designated_Type (Acc_Type)) then
711 Check_Restriction (No_Tasking, N);
712 Check_Restriction (Max_Tasks, N);
713 Check_Restriction (No_Task_Allocators, N);
714 end if;
716 -- Check restriction against dynamically allocated protected objects
718 if Has_Protected (Designated_Type (Acc_Type)) then
719 Check_Restriction (No_Protected_Type_Allocators, N);
720 end if;
722 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
723 -- type is nested, and the designated type needs finalization. The rule
724 -- is conservative in that class-wide types need finalization.
726 if Needs_Finalization (Designated_Type (Acc_Type))
727 and then not Is_Library_Level_Entity (Acc_Type)
728 then
729 Check_Restriction (No_Nested_Finalization, N);
730 end if;
732 -- Check that an allocator of a nested access type doesn't create a
733 -- protected object when restriction No_Local_Protected_Objects applies.
735 if Has_Protected (Designated_Type (Acc_Type))
736 and then not Is_Library_Level_Entity (Acc_Type)
737 then
738 Check_Restriction (No_Local_Protected_Objects, N);
739 end if;
741 -- If the No_Streams restriction is set, check that the type of the
742 -- object is not, and does not contain, any subtype derived from
743 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
744 -- Has_Stream just for efficiency reasons. There is no point in
745 -- spending time on a Has_Stream check if the restriction is not set.
747 if Restriction_Check_Required (No_Streams) then
748 if Has_Stream (Designated_Type (Acc_Type)) then
749 Check_Restriction (No_Streams, N);
750 end if;
751 end if;
753 Set_Etype (N, Acc_Type);
755 if not Is_Library_Level_Entity (Acc_Type) then
756 Check_Restriction (No_Local_Allocators, N);
757 end if;
759 if Serious_Errors_Detected > Sav_Errs then
760 Set_Error_Posted (N);
761 Set_Etype (N, Any_Type);
762 end if;
763 end Analyze_Allocator;
765 ---------------------------
766 -- Analyze_Arithmetic_Op --
767 ---------------------------
769 procedure Analyze_Arithmetic_Op (N : Node_Id) is
770 L : constant Node_Id := Left_Opnd (N);
771 R : constant Node_Id := Right_Opnd (N);
772 Op_Id : Entity_Id;
774 begin
775 Candidate_Type := Empty;
776 Analyze_Expression (L);
777 Analyze_Expression (R);
779 -- If the entity is already set, the node is the instantiation of a
780 -- generic node with a non-local reference, or was manufactured by a
781 -- call to Make_Op_xxx. In either case the entity is known to be valid,
782 -- and we do not need to collect interpretations, instead we just get
783 -- the single possible interpretation.
785 Op_Id := Entity (N);
787 if Present (Op_Id) then
788 if Ekind (Op_Id) = E_Operator then
790 if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
791 and then Treat_Fixed_As_Integer (N)
792 then
793 null;
794 else
795 Set_Etype (N, Any_Type);
796 Find_Arithmetic_Types (L, R, Op_Id, N);
797 end if;
799 else
800 Set_Etype (N, Any_Type);
801 Add_One_Interp (N, Op_Id, Etype (Op_Id));
802 end if;
804 -- Entity is not already set, so we do need to collect interpretations
806 else
807 Op_Id := Get_Name_Entity_Id (Chars (N));
808 Set_Etype (N, Any_Type);
810 while Present (Op_Id) loop
811 if Ekind (Op_Id) = E_Operator
812 and then Present (Next_Entity (First_Entity (Op_Id)))
813 then
814 Find_Arithmetic_Types (L, R, Op_Id, N);
816 -- The following may seem superfluous, because an operator cannot
817 -- be generic, but this ignores the cleverness of the author of
818 -- ACVC bc1013a.
820 elsif Is_Overloadable (Op_Id) then
821 Analyze_User_Defined_Binary_Op (N, Op_Id);
822 end if;
824 Op_Id := Homonym (Op_Id);
825 end loop;
826 end if;
828 Operator_Check (N);
829 end Analyze_Arithmetic_Op;
831 ------------------
832 -- Analyze_Call --
833 ------------------
835 -- Function, procedure, and entry calls are checked here. The Name in
836 -- the call may be overloaded. The actuals have been analyzed and may
837 -- themselves be overloaded. On exit from this procedure, the node N
838 -- may have zero, one or more interpretations. In the first case an
839 -- error message is produced. In the last case, the node is flagged
840 -- as overloaded and the interpretations are collected in All_Interp.
842 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
843 -- the type-checking is similar to that of other calls.
845 procedure Analyze_Call (N : Node_Id) is
846 Actuals : constant List_Id := Parameter_Associations (N);
847 Nam : Node_Id;
848 X : Interp_Index;
849 It : Interp;
850 Nam_Ent : Entity_Id;
851 Success : Boolean := False;
853 Deref : Boolean := False;
854 -- Flag indicates whether an interpretation of the prefix is a
855 -- parameterless call that returns an access_to_subprogram.
857 procedure Check_Mixed_Parameter_And_Named_Associations;
858 -- Check that parameter and named associations are not mixed. This is
859 -- a restriction in SPARK mode.
861 function Name_Denotes_Function return Boolean;
862 -- If the type of the name is an access to subprogram, this may be the
863 -- type of a name, or the return type of the function being called. If
864 -- the name is not an entity then it can denote a protected function.
865 -- Until we distinguish Etype from Return_Type, we must use this routine
866 -- to resolve the meaning of the name in the call.
868 procedure No_Interpretation;
869 -- Output error message when no valid interpretation exists
871 --------------------------------------------------
872 -- Check_Mixed_Parameter_And_Named_Associations --
873 --------------------------------------------------
875 procedure Check_Mixed_Parameter_And_Named_Associations is
876 Actual : Node_Id;
877 Named_Seen : Boolean;
879 begin
880 Named_Seen := False;
882 Actual := First (Actuals);
883 while Present (Actual) loop
884 case Nkind (Actual) is
885 when N_Parameter_Association =>
886 if Named_Seen then
887 Check_SPARK_05_Restriction
888 ("named association cannot follow positional one",
889 Actual);
890 exit;
891 end if;
892 when others =>
893 Named_Seen := True;
894 end case;
896 Next (Actual);
897 end loop;
898 end Check_Mixed_Parameter_And_Named_Associations;
900 ---------------------------
901 -- Name_Denotes_Function --
902 ---------------------------
904 function Name_Denotes_Function return Boolean is
905 begin
906 if Is_Entity_Name (Nam) then
907 return Ekind (Entity (Nam)) = E_Function;
909 elsif Nkind (Nam) = N_Selected_Component then
910 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
912 else
913 return False;
914 end if;
915 end Name_Denotes_Function;
917 -----------------------
918 -- No_Interpretation --
919 -----------------------
921 procedure No_Interpretation is
922 L : constant Boolean := Is_List_Member (N);
923 K : constant Node_Kind := Nkind (Parent (N));
925 begin
926 -- If the node is in a list whose parent is not an expression then it
927 -- must be an attempted procedure call.
929 if L and then K not in N_Subexpr then
930 if Ekind (Entity (Nam)) = E_Generic_Procedure then
931 Error_Msg_NE
932 ("must instantiate generic procedure& before call",
933 Nam, Entity (Nam));
934 else
935 Error_Msg_N
936 ("procedure or entry name expected", Nam);
937 end if;
939 -- Check for tasking cases where only an entry call will do
941 elsif not L
942 and then Nkind_In (K, N_Entry_Call_Alternative,
943 N_Triggering_Alternative)
944 then
945 Error_Msg_N ("entry name expected", Nam);
947 -- Otherwise give general error message
949 else
950 Error_Msg_N ("invalid prefix in call", Nam);
951 end if;
952 end No_Interpretation;
954 -- Start of processing for Analyze_Call
956 begin
957 if Restriction_Check_Required (SPARK_05) then
958 Check_Mixed_Parameter_And_Named_Associations;
959 end if;
961 -- Initialize the type of the result of the call to the error type,
962 -- which will be reset if the type is successfully resolved.
964 Set_Etype (N, Any_Type);
966 Nam := Name (N);
968 if not Is_Overloaded (Nam) then
970 -- Only one interpretation to check
972 if Ekind (Etype (Nam)) = E_Subprogram_Type then
973 Nam_Ent := Etype (Nam);
975 -- If the prefix is an access_to_subprogram, this may be an indirect
976 -- call. This is the case if the name in the call is not an entity
977 -- name, or if it is a function name in the context of a procedure
978 -- call. In this latter case, we have a call to a parameterless
979 -- function that returns a pointer_to_procedure which is the entity
980 -- being called. Finally, F (X) may be a call to a parameterless
981 -- function that returns a pointer to a function with parameters.
982 -- Note that if F returns an access-to-subprogram whose designated
983 -- type is an array, F (X) cannot be interpreted as an indirect call
984 -- through the result of the call to F.
986 elsif Is_Access_Type (Etype (Nam))
987 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
988 and then
989 (not Name_Denotes_Function
990 or else Nkind (N) = N_Procedure_Call_Statement
991 or else
992 (Nkind (Parent (N)) /= N_Explicit_Dereference
993 and then Is_Entity_Name (Nam)
994 and then No (First_Formal (Entity (Nam)))
995 and then not
996 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
997 and then Present (Actuals)))
998 then
999 Nam_Ent := Designated_Type (Etype (Nam));
1000 Insert_Explicit_Dereference (Nam);
1002 -- Selected component case. Simple entry or protected operation,
1003 -- where the entry name is given by the selector name.
1005 elsif Nkind (Nam) = N_Selected_Component then
1006 Nam_Ent := Entity (Selector_Name (Nam));
1008 if not Ekind_In (Nam_Ent, E_Entry,
1009 E_Entry_Family,
1010 E_Function,
1011 E_Procedure)
1012 then
1013 Error_Msg_N ("name in call is not a callable entity", Nam);
1014 Set_Etype (N, Any_Type);
1015 return;
1016 end if;
1018 -- If the name is an Indexed component, it can be a call to a member
1019 -- of an entry family. The prefix must be a selected component whose
1020 -- selector is the entry. Analyze_Procedure_Call normalizes several
1021 -- kinds of call into this form.
1023 elsif Nkind (Nam) = N_Indexed_Component then
1024 if Nkind (Prefix (Nam)) = N_Selected_Component then
1025 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1026 else
1027 Error_Msg_N ("name in call is not a callable entity", Nam);
1028 Set_Etype (N, Any_Type);
1029 return;
1030 end if;
1032 elsif not Is_Entity_Name (Nam) then
1033 Error_Msg_N ("name in call is not a callable entity", Nam);
1034 Set_Etype (N, Any_Type);
1035 return;
1037 else
1038 Nam_Ent := Entity (Nam);
1040 -- If not overloadable, this may be a generalized indexing
1041 -- operation with named associations. Rewrite again as an
1042 -- indexed component and analyze as container indexing.
1044 if not Is_Overloadable (Nam_Ent) then
1045 if Present
1046 (Find_Value_Of_Aspect
1047 (Etype (Nam_Ent), Aspect_Constant_Indexing))
1048 then
1049 Replace (N,
1050 Make_Indexed_Component (Sloc (N),
1051 Prefix => Nam,
1052 Expressions => Parameter_Associations (N)));
1054 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1055 return;
1056 else
1057 No_Interpretation;
1058 end if;
1060 else
1061 No_Interpretation;
1062 end if;
1064 return;
1065 end if;
1066 end if;
1068 -- Operations generated for RACW stub types are called only through
1069 -- dispatching, and can never be the static interpretation of a call.
1071 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1072 No_Interpretation;
1073 return;
1074 end if;
1076 Analyze_One_Call (N, Nam_Ent, True, Success);
1078 -- If this is an indirect call, the return type of the access_to
1079 -- subprogram may be an incomplete type. At the point of the call,
1080 -- use the full type if available, and at the same time update the
1081 -- return type of the access_to_subprogram.
1083 if Success
1084 and then Nkind (Nam) = N_Explicit_Dereference
1085 and then Ekind (Etype (N)) = E_Incomplete_Type
1086 and then Present (Full_View (Etype (N)))
1087 then
1088 Set_Etype (N, Full_View (Etype (N)));
1089 Set_Etype (Nam_Ent, Etype (N));
1090 end if;
1092 -- Overloaded call
1094 else
1095 -- An overloaded selected component must denote overloaded operations
1096 -- of a concurrent type. The interpretations are attached to the
1097 -- simple name of those operations.
1099 if Nkind (Nam) = N_Selected_Component then
1100 Nam := Selector_Name (Nam);
1101 end if;
1103 Get_First_Interp (Nam, X, It);
1105 while Present (It.Nam) loop
1106 Nam_Ent := It.Nam;
1107 Deref := False;
1109 -- Name may be call that returns an access to subprogram, or more
1110 -- generally an overloaded expression one of whose interpretations
1111 -- yields an access to subprogram. If the name is an entity, we do
1112 -- not dereference, because the node is a call that returns the
1113 -- access type: note difference between f(x), where the call may
1114 -- return an access subprogram type, and f(x)(y), where the type
1115 -- returned by the call to f is implicitly dereferenced to analyze
1116 -- the outer call.
1118 if Is_Access_Type (Nam_Ent) then
1119 Nam_Ent := Designated_Type (Nam_Ent);
1121 elsif Is_Access_Type (Etype (Nam_Ent))
1122 and then
1123 (not Is_Entity_Name (Nam)
1124 or else Nkind (N) = N_Procedure_Call_Statement)
1125 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1126 = E_Subprogram_Type
1127 then
1128 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1130 if Is_Entity_Name (Nam) then
1131 Deref := True;
1132 end if;
1133 end if;
1135 -- If the call has been rewritten from a prefixed call, the first
1136 -- parameter has been analyzed, but may need a subsequent
1137 -- dereference, so skip its analysis now.
1139 if N /= Original_Node (N)
1140 and then Nkind (Original_Node (N)) = Nkind (N)
1141 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1142 and then Present (Parameter_Associations (N))
1143 and then Present (Etype (First (Parameter_Associations (N))))
1144 then
1145 Analyze_One_Call
1146 (N, Nam_Ent, False, Success, Skip_First => True);
1147 else
1148 Analyze_One_Call (N, Nam_Ent, False, Success);
1149 end if;
1151 -- If the interpretation succeeds, mark the proper type of the
1152 -- prefix (any valid candidate will do). If not, remove the
1153 -- candidate interpretation. This only needs to be done for
1154 -- overloaded protected operations, for other entities disambi-
1155 -- guation is done directly in Resolve.
1157 if Success then
1158 if Deref
1159 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1160 then
1161 Set_Entity (Nam, It.Nam);
1162 Insert_Explicit_Dereference (Nam);
1163 Set_Etype (Nam, Nam_Ent);
1165 else
1166 Set_Etype (Nam, It.Typ);
1167 end if;
1169 elsif Nkind_In (Name (N), N_Selected_Component,
1170 N_Function_Call)
1171 then
1172 Remove_Interp (X);
1173 end if;
1175 Get_Next_Interp (X, It);
1176 end loop;
1178 -- If the name is the result of a function call, it can only be a
1179 -- call to a function returning an access to subprogram. Insert
1180 -- explicit dereference.
1182 if Nkind (Nam) = N_Function_Call then
1183 Insert_Explicit_Dereference (Nam);
1184 end if;
1186 if Etype (N) = Any_Type then
1188 -- None of the interpretations is compatible with the actuals
1190 Diagnose_Call (N, Nam);
1192 -- Special checks for uninstantiated put routines
1194 if Nkind (N) = N_Procedure_Call_Statement
1195 and then Is_Entity_Name (Nam)
1196 and then Chars (Nam) = Name_Put
1197 and then List_Length (Actuals) = 1
1198 then
1199 declare
1200 Arg : constant Node_Id := First (Actuals);
1201 Typ : Entity_Id;
1203 begin
1204 if Nkind (Arg) = N_Parameter_Association then
1205 Typ := Etype (Explicit_Actual_Parameter (Arg));
1206 else
1207 Typ := Etype (Arg);
1208 end if;
1210 if Is_Signed_Integer_Type (Typ) then
1211 Error_Msg_N
1212 ("possible missing instantiation of "
1213 & "'Text_'I'O.'Integer_'I'O!", Nam);
1215 elsif Is_Modular_Integer_Type (Typ) then
1216 Error_Msg_N
1217 ("possible missing instantiation of "
1218 & "'Text_'I'O.'Modular_'I'O!", Nam);
1220 elsif Is_Floating_Point_Type (Typ) then
1221 Error_Msg_N
1222 ("possible missing instantiation of "
1223 & "'Text_'I'O.'Float_'I'O!", Nam);
1225 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1226 Error_Msg_N
1227 ("possible missing instantiation of "
1228 & "'Text_'I'O.'Fixed_'I'O!", Nam);
1230 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1231 Error_Msg_N
1232 ("possible missing instantiation of "
1233 & "'Text_'I'O.'Decimal_'I'O!", Nam);
1235 elsif Is_Enumeration_Type (Typ) then
1236 Error_Msg_N
1237 ("possible missing instantiation of "
1238 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
1239 end if;
1240 end;
1241 end if;
1243 elsif not Is_Overloaded (N)
1244 and then Is_Entity_Name (Nam)
1245 then
1246 -- Resolution yields a single interpretation. Verify that the
1247 -- reference has capitalization consistent with the declaration.
1249 Set_Entity_With_Checks (Nam, Entity (Nam));
1250 Generate_Reference (Entity (Nam), Nam);
1252 Set_Etype (Nam, Etype (Entity (Nam)));
1253 else
1254 Remove_Abstract_Operations (N);
1255 end if;
1257 End_Interp_List;
1258 end if;
1259 end Analyze_Call;
1261 -----------------------------
1262 -- Analyze_Case_Expression --
1263 -----------------------------
1265 procedure Analyze_Case_Expression (N : Node_Id) is
1266 procedure Non_Static_Choice_Error (Choice : Node_Id);
1267 -- Error routine invoked by the generic instantiation below when
1268 -- the case expression has a non static choice.
1270 package Case_Choices_Analysis is new
1271 Generic_Analyze_Choices
1272 (Process_Associated_Node => No_OP);
1273 use Case_Choices_Analysis;
1275 package Case_Choices_Checking is new
1276 Generic_Check_Choices
1277 (Process_Empty_Choice => No_OP,
1278 Process_Non_Static_Choice => Non_Static_Choice_Error,
1279 Process_Associated_Node => No_OP);
1280 use Case_Choices_Checking;
1282 -----------------------------
1283 -- Non_Static_Choice_Error --
1284 -----------------------------
1286 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1287 begin
1288 Flag_Non_Static_Expr
1289 ("choice given in case expression is not static!", Choice);
1290 end Non_Static_Choice_Error;
1292 -- Local variables
1294 Expr : constant Node_Id := Expression (N);
1295 Alt : Node_Id;
1296 Exp_Type : Entity_Id;
1297 Exp_Btype : Entity_Id;
1299 FirstX : Node_Id := Empty;
1300 -- First expression in the case for which there is some type information
1301 -- available, i.e. it is not Any_Type, which can happen because of some
1302 -- error, or from the use of e.g. raise Constraint_Error.
1304 Others_Present : Boolean;
1305 -- Indicates if Others was present
1307 Wrong_Alt : Node_Id;
1308 -- For error reporting
1310 -- Start of processing for Analyze_Case_Expression
1312 begin
1313 if Comes_From_Source (N) then
1314 Check_Compiler_Unit ("case expression", N);
1315 end if;
1317 Analyze_And_Resolve (Expr, Any_Discrete);
1318 Check_Unset_Reference (Expr);
1319 Exp_Type := Etype (Expr);
1320 Exp_Btype := Base_Type (Exp_Type);
1322 Alt := First (Alternatives (N));
1323 while Present (Alt) loop
1324 Analyze (Expression (Alt));
1326 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1327 FirstX := Expression (Alt);
1328 end if;
1330 Next (Alt);
1331 end loop;
1333 -- Get our initial type from the first expression for which we got some
1334 -- useful type information from the expression.
1336 if not Is_Overloaded (FirstX) then
1337 Set_Etype (N, Etype (FirstX));
1339 else
1340 declare
1341 I : Interp_Index;
1342 It : Interp;
1344 begin
1345 Set_Etype (N, Any_Type);
1347 Get_First_Interp (FirstX, I, It);
1348 while Present (It.Nam) loop
1350 -- For each interpretation of the first expression, we only
1351 -- add the interpretation if every other expression in the
1352 -- case expression alternatives has a compatible type.
1354 Alt := Next (First (Alternatives (N)));
1355 while Present (Alt) loop
1356 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1357 Next (Alt);
1358 end loop;
1360 if No (Alt) then
1361 Add_One_Interp (N, It.Typ, It.Typ);
1363 else
1364 Wrong_Alt := Alt;
1365 end if;
1367 Get_Next_Interp (I, It);
1368 end loop;
1369 end;
1370 end if;
1372 Exp_Btype := Base_Type (Exp_Type);
1374 -- The expression must be of a discrete type which must be determinable
1375 -- independently of the context in which the expression occurs, but
1376 -- using the fact that the expression must be of a discrete type.
1377 -- Moreover, the type this expression must not be a character literal
1378 -- (which is always ambiguous).
1380 -- If error already reported by Resolve, nothing more to do
1382 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1383 return;
1385 -- Special casee message for character literal
1387 elsif Exp_Btype = Any_Character then
1388 Error_Msg_N
1389 ("character literal as case expression is ambiguous", Expr);
1390 return;
1391 end if;
1393 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
1394 Error_Msg_N
1395 ("type incompatible with that of previous alternatives",
1396 Expression (Wrong_Alt));
1397 return;
1398 end if;
1400 -- If the case expression is a formal object of mode in out, then
1401 -- treat it as having a nonstatic subtype by forcing use of the base
1402 -- type (which has to get passed to Check_Case_Choices below). Also
1403 -- use base type when the case expression is parenthesized.
1405 if Paren_Count (Expr) > 0
1406 or else (Is_Entity_Name (Expr)
1407 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1408 then
1409 Exp_Type := Exp_Btype;
1410 end if;
1412 -- The case expression alternatives cover the range of a static subtype
1413 -- subject to aspect Static_Predicate. Do not check the choices when the
1414 -- case expression has not been fully analyzed yet because this may lead
1415 -- to bogus errors.
1417 if Is_OK_Static_Subtype (Exp_Type)
1418 and then Has_Static_Predicate_Aspect (Exp_Type)
1419 and then In_Spec_Expression
1420 then
1421 null;
1423 -- Call Analyze_Choices and Check_Choices to do the rest of the work
1425 else
1426 Analyze_Choices (Alternatives (N), Exp_Type);
1427 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1428 end if;
1430 if Exp_Type = Universal_Integer and then not Others_Present then
1431 Error_Msg_N
1432 ("case on universal integer requires OTHERS choice", Expr);
1433 end if;
1434 end Analyze_Case_Expression;
1436 ---------------------------
1437 -- Analyze_Comparison_Op --
1438 ---------------------------
1440 procedure Analyze_Comparison_Op (N : Node_Id) is
1441 L : constant Node_Id := Left_Opnd (N);
1442 R : constant Node_Id := Right_Opnd (N);
1443 Op_Id : Entity_Id := Entity (N);
1445 begin
1446 Set_Etype (N, Any_Type);
1447 Candidate_Type := Empty;
1449 Analyze_Expression (L);
1450 Analyze_Expression (R);
1452 if Present (Op_Id) then
1453 if Ekind (Op_Id) = E_Operator then
1454 Find_Comparison_Types (L, R, Op_Id, N);
1455 else
1456 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1457 end if;
1459 if Is_Overloaded (L) then
1460 Set_Etype (L, Intersect_Types (L, R));
1461 end if;
1463 else
1464 Op_Id := Get_Name_Entity_Id (Chars (N));
1465 while Present (Op_Id) loop
1466 if Ekind (Op_Id) = E_Operator then
1467 Find_Comparison_Types (L, R, Op_Id, N);
1468 else
1469 Analyze_User_Defined_Binary_Op (N, Op_Id);
1470 end if;
1472 Op_Id := Homonym (Op_Id);
1473 end loop;
1474 end if;
1476 Operator_Check (N);
1477 end Analyze_Comparison_Op;
1479 ---------------------------
1480 -- Analyze_Concatenation --
1481 ---------------------------
1483 procedure Analyze_Concatenation (N : Node_Id) is
1485 -- We wish to avoid deep recursion, because concatenations are often
1486 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1487 -- operands nonrecursively until we find something that is not a
1488 -- concatenation (A in this case), or has already been analyzed. We
1489 -- analyze that, and then walk back up the tree following Parent
1490 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1491 -- work at each level. The Parent pointers allow us to avoid recursion,
1492 -- and thus avoid running out of memory.
1494 NN : Node_Id := N;
1495 L : Node_Id;
1497 begin
1498 Candidate_Type := Empty;
1500 -- The following code is equivalent to:
1502 -- Set_Etype (N, Any_Type);
1503 -- Analyze_Expression (Left_Opnd (N));
1504 -- Analyze_Concatenation_Rest (N);
1506 -- where the Analyze_Expression call recurses back here if the left
1507 -- operand is a concatenation.
1509 -- Walk down left operands
1511 loop
1512 Set_Etype (NN, Any_Type);
1513 L := Left_Opnd (NN);
1514 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1515 NN := L;
1516 end loop;
1518 -- Now (given the above example) NN is A&B and L is A
1520 -- First analyze L ...
1522 Analyze_Expression (L);
1524 -- ... then walk NN back up until we reach N (where we started), calling
1525 -- Analyze_Concatenation_Rest along the way.
1527 loop
1528 Analyze_Concatenation_Rest (NN);
1529 exit when NN = N;
1530 NN := Parent (NN);
1531 end loop;
1532 end Analyze_Concatenation;
1534 --------------------------------
1535 -- Analyze_Concatenation_Rest --
1536 --------------------------------
1538 -- If the only one-dimensional array type in scope is String,
1539 -- this is the resulting type of the operation. Otherwise there
1540 -- will be a concatenation operation defined for each user-defined
1541 -- one-dimensional array.
1543 procedure Analyze_Concatenation_Rest (N : Node_Id) is
1544 L : constant Node_Id := Left_Opnd (N);
1545 R : constant Node_Id := Right_Opnd (N);
1546 Op_Id : Entity_Id := Entity (N);
1547 LT : Entity_Id;
1548 RT : Entity_Id;
1550 begin
1551 Analyze_Expression (R);
1553 -- If the entity is present, the node appears in an instance, and
1554 -- denotes a predefined concatenation operation. The resulting type is
1555 -- obtained from the arguments when possible. If the arguments are
1556 -- aggregates, the array type and the concatenation type must be
1557 -- visible.
1559 if Present (Op_Id) then
1560 if Ekind (Op_Id) = E_Operator then
1561 LT := Base_Type (Etype (L));
1562 RT := Base_Type (Etype (R));
1564 if Is_Array_Type (LT)
1565 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1566 then
1567 Add_One_Interp (N, Op_Id, LT);
1569 elsif Is_Array_Type (RT)
1570 and then LT = Base_Type (Component_Type (RT))
1571 then
1572 Add_One_Interp (N, Op_Id, RT);
1574 -- If one operand is a string type or a user-defined array type,
1575 -- and the other is a literal, result is of the specific type.
1577 elsif
1578 (Root_Type (LT) = Standard_String
1579 or else Scope (LT) /= Standard_Standard)
1580 and then Etype (R) = Any_String
1581 then
1582 Add_One_Interp (N, Op_Id, LT);
1584 elsif
1585 (Root_Type (RT) = Standard_String
1586 or else Scope (RT) /= Standard_Standard)
1587 and then Etype (L) = Any_String
1588 then
1589 Add_One_Interp (N, Op_Id, RT);
1591 elsif not Is_Generic_Type (Etype (Op_Id)) then
1592 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1594 else
1595 -- Type and its operations must be visible
1597 Set_Entity (N, Empty);
1598 Analyze_Concatenation (N);
1599 end if;
1601 else
1602 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1603 end if;
1605 else
1606 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1607 while Present (Op_Id) loop
1608 if Ekind (Op_Id) = E_Operator then
1610 -- Do not consider operators declared in dead code, they can
1611 -- not be part of the resolution.
1613 if Is_Eliminated (Op_Id) then
1614 null;
1615 else
1616 Find_Concatenation_Types (L, R, Op_Id, N);
1617 end if;
1619 else
1620 Analyze_User_Defined_Binary_Op (N, Op_Id);
1621 end if;
1623 Op_Id := Homonym (Op_Id);
1624 end loop;
1625 end if;
1627 Operator_Check (N);
1628 end Analyze_Concatenation_Rest;
1630 -------------------------
1631 -- Analyze_Equality_Op --
1632 -------------------------
1634 procedure Analyze_Equality_Op (N : Node_Id) is
1635 Loc : constant Source_Ptr := Sloc (N);
1636 L : constant Node_Id := Left_Opnd (N);
1637 R : constant Node_Id := Right_Opnd (N);
1638 Op_Id : Entity_Id;
1640 begin
1641 Set_Etype (N, Any_Type);
1642 Candidate_Type := Empty;
1644 Analyze_Expression (L);
1645 Analyze_Expression (R);
1647 -- If the entity is set, the node is a generic instance with a non-local
1648 -- reference to the predefined operator or to a user-defined function.
1649 -- It can also be an inequality that is expanded into the negation of a
1650 -- call to a user-defined equality operator.
1652 -- For the predefined case, the result is Boolean, regardless of the
1653 -- type of the operands. The operands may even be limited, if they are
1654 -- generic actuals. If they are overloaded, label the left argument with
1655 -- the common type that must be present, or with the type of the formal
1656 -- of the user-defined function.
1658 if Present (Entity (N)) then
1659 Op_Id := Entity (N);
1661 if Ekind (Op_Id) = E_Operator then
1662 Add_One_Interp (N, Op_Id, Standard_Boolean);
1663 else
1664 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1665 end if;
1667 if Is_Overloaded (L) then
1668 if Ekind (Op_Id) = E_Operator then
1669 Set_Etype (L, Intersect_Types (L, R));
1670 else
1671 Set_Etype (L, Etype (First_Formal (Op_Id)));
1672 end if;
1673 end if;
1675 else
1676 Op_Id := Get_Name_Entity_Id (Chars (N));
1677 while Present (Op_Id) loop
1678 if Ekind (Op_Id) = E_Operator then
1679 Find_Equality_Types (L, R, Op_Id, N);
1680 else
1681 Analyze_User_Defined_Binary_Op (N, Op_Id);
1682 end if;
1684 Op_Id := Homonym (Op_Id);
1685 end loop;
1686 end if;
1688 -- If there was no match, and the operator is inequality, this may
1689 -- be a case where inequality has not been made explicit, as for
1690 -- tagged types. Analyze the node as the negation of an equality
1691 -- operation. This cannot be done earlier, because before analysis
1692 -- we cannot rule out the presence of an explicit inequality.
1694 if Etype (N) = Any_Type
1695 and then Nkind (N) = N_Op_Ne
1696 then
1697 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1698 while Present (Op_Id) loop
1699 if Ekind (Op_Id) = E_Operator then
1700 Find_Equality_Types (L, R, Op_Id, N);
1701 else
1702 Analyze_User_Defined_Binary_Op (N, Op_Id);
1703 end if;
1705 Op_Id := Homonym (Op_Id);
1706 end loop;
1708 if Etype (N) /= Any_Type then
1709 Op_Id := Entity (N);
1711 Rewrite (N,
1712 Make_Op_Not (Loc,
1713 Right_Opnd =>
1714 Make_Op_Eq (Loc,
1715 Left_Opnd => Left_Opnd (N),
1716 Right_Opnd => Right_Opnd (N))));
1718 Set_Entity (Right_Opnd (N), Op_Id);
1719 Analyze (N);
1720 end if;
1721 end if;
1723 Operator_Check (N);
1724 end Analyze_Equality_Op;
1726 ----------------------------------
1727 -- Analyze_Explicit_Dereference --
1728 ----------------------------------
1730 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1731 Loc : constant Source_Ptr := Sloc (N);
1732 P : constant Node_Id := Prefix (N);
1733 T : Entity_Id;
1734 I : Interp_Index;
1735 It : Interp;
1736 New_N : Node_Id;
1738 function Is_Function_Type return Boolean;
1739 -- Check whether node may be interpreted as an implicit function call
1741 ----------------------
1742 -- Is_Function_Type --
1743 ----------------------
1745 function Is_Function_Type return Boolean is
1746 I : Interp_Index;
1747 It : Interp;
1749 begin
1750 if not Is_Overloaded (N) then
1751 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1752 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1754 else
1755 Get_First_Interp (N, I, It);
1756 while Present (It.Nam) loop
1757 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1758 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1759 then
1760 return False;
1761 end if;
1763 Get_Next_Interp (I, It);
1764 end loop;
1766 return True;
1767 end if;
1768 end Is_Function_Type;
1770 -- Start of processing for Analyze_Explicit_Dereference
1772 begin
1773 -- If source node, check SPARK restriction. We guard this with the
1774 -- source node check, because ???
1776 if Comes_From_Source (N) then
1777 Check_SPARK_05_Restriction ("explicit dereference is not allowed", N);
1778 end if;
1780 -- In formal verification mode, keep track of all reads and writes
1781 -- through explicit dereferences.
1783 if GNATprove_Mode then
1784 SPARK_Specific.Generate_Dereference (N);
1785 end if;
1787 Analyze (P);
1788 Set_Etype (N, Any_Type);
1790 -- Test for remote access to subprogram type, and if so return
1791 -- after rewriting the original tree.
1793 if Remote_AST_E_Dereference (P) then
1794 return;
1795 end if;
1797 -- Normal processing for other than remote access to subprogram type
1799 if not Is_Overloaded (P) then
1800 if Is_Access_Type (Etype (P)) then
1802 -- Set the Etype. We need to go through Is_For_Access_Subtypes to
1803 -- avoid other problems caused by the Private_Subtype and it is
1804 -- safe to go to the Base_Type because this is the same as
1805 -- converting the access value to its Base_Type.
1807 declare
1808 DT : Entity_Id := Designated_Type (Etype (P));
1810 begin
1811 if Ekind (DT) = E_Private_Subtype
1812 and then Is_For_Access_Subtype (DT)
1813 then
1814 DT := Base_Type (DT);
1815 end if;
1817 -- An explicit dereference is a legal occurrence of an
1818 -- incomplete type imported through a limited_with clause,
1819 -- if the full view is visible.
1821 if From_Limited_With (DT)
1822 and then not From_Limited_With (Scope (DT))
1823 and then
1824 (Is_Immediately_Visible (Scope (DT))
1825 or else
1826 (Is_Child_Unit (Scope (DT))
1827 and then Is_Visible_Lib_Unit (Scope (DT))))
1828 then
1829 Set_Etype (N, Available_View (DT));
1831 else
1832 Set_Etype (N, DT);
1833 end if;
1834 end;
1836 elsif Etype (P) /= Any_Type then
1837 Error_Msg_N ("prefix of dereference must be an access type", N);
1838 return;
1839 end if;
1841 else
1842 Get_First_Interp (P, I, It);
1843 while Present (It.Nam) loop
1844 T := It.Typ;
1846 if Is_Access_Type (T) then
1847 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1848 end if;
1850 Get_Next_Interp (I, It);
1851 end loop;
1853 -- Error if no interpretation of the prefix has an access type
1855 if Etype (N) = Any_Type then
1856 Error_Msg_N
1857 ("access type required in prefix of explicit dereference", P);
1858 Set_Etype (N, Any_Type);
1859 return;
1860 end if;
1861 end if;
1863 if Is_Function_Type
1864 and then Nkind (Parent (N)) /= N_Indexed_Component
1866 and then (Nkind (Parent (N)) /= N_Function_Call
1867 or else N /= Name (Parent (N)))
1869 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1870 or else N /= Name (Parent (N)))
1872 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1873 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1874 or else
1875 (Attribute_Name (Parent (N)) /= Name_Address
1876 and then
1877 Attribute_Name (Parent (N)) /= Name_Access))
1878 then
1879 -- Name is a function call with no actuals, in a context that
1880 -- requires deproceduring (including as an actual in an enclosing
1881 -- function or procedure call). There are some pathological cases
1882 -- where the prefix might include functions that return access to
1883 -- subprograms and others that return a regular type. Disambiguation
1884 -- of those has to take place in Resolve.
1886 New_N :=
1887 Make_Function_Call (Loc,
1888 Name => Make_Explicit_Dereference (Loc, P),
1889 Parameter_Associations => New_List);
1891 -- If the prefix is overloaded, remove operations that have formals,
1892 -- we know that this is a parameterless call.
1894 if Is_Overloaded (P) then
1895 Get_First_Interp (P, I, It);
1896 while Present (It.Nam) loop
1897 T := It.Typ;
1899 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1900 Set_Etype (P, T);
1901 else
1902 Remove_Interp (I);
1903 end if;
1905 Get_Next_Interp (I, It);
1906 end loop;
1907 end if;
1909 Rewrite (N, New_N);
1910 Analyze (N);
1912 elsif not Is_Function_Type
1913 and then Is_Overloaded (N)
1914 then
1915 -- The prefix may include access to subprograms and other access
1916 -- types. If the context selects the interpretation that is a
1917 -- function call (not a procedure call) we cannot rewrite the node
1918 -- yet, but we include the result of the call interpretation.
1920 Get_First_Interp (N, I, It);
1921 while Present (It.Nam) loop
1922 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1923 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1924 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1925 then
1926 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1927 end if;
1929 Get_Next_Interp (I, It);
1930 end loop;
1931 end if;
1933 -- A value of remote access-to-class-wide must not be dereferenced
1934 -- (RM E.2.2(16)).
1936 Validate_Remote_Access_To_Class_Wide_Type (N);
1937 end Analyze_Explicit_Dereference;
1939 ------------------------
1940 -- Analyze_Expression --
1941 ------------------------
1943 procedure Analyze_Expression (N : Node_Id) is
1944 begin
1946 -- If the expression is an indexed component that will be rewritten
1947 -- as a container indexing, it has already been analyzed.
1949 if Nkind (N) = N_Indexed_Component
1950 and then Present (Generalized_Indexing (N))
1951 then
1952 null;
1954 else
1955 Analyze (N);
1956 Check_Parameterless_Call (N);
1957 end if;
1958 end Analyze_Expression;
1960 -------------------------------------
1961 -- Analyze_Expression_With_Actions --
1962 -------------------------------------
1964 procedure Analyze_Expression_With_Actions (N : Node_Id) is
1965 A : Node_Id;
1967 begin
1968 A := First (Actions (N));
1969 while Present (A) loop
1970 Analyze (A);
1971 Next (A);
1972 end loop;
1974 Analyze_Expression (Expression (N));
1975 Set_Etype (N, Etype (Expression (N)));
1976 end Analyze_Expression_With_Actions;
1978 ---------------------------
1979 -- Analyze_If_Expression --
1980 ---------------------------
1982 procedure Analyze_If_Expression (N : Node_Id) is
1983 Condition : constant Node_Id := First (Expressions (N));
1984 Then_Expr : constant Node_Id := Next (Condition);
1985 Else_Expr : Node_Id;
1987 begin
1988 -- Defend against error of missing expressions from previous error
1990 if No (Then_Expr) then
1991 Check_Error_Detected;
1992 return;
1993 end if;
1995 if Comes_From_Source (N) then
1996 Check_SPARK_05_Restriction ("if expression is not allowed", N);
1997 end if;
1999 Else_Expr := Next (Then_Expr);
2001 if Comes_From_Source (N) then
2002 Check_Compiler_Unit ("if expression", N);
2003 end if;
2005 -- Analyze and resolve the condition. We need to resolve this now so
2006 -- that it gets folded to True/False if possible, before we analyze
2007 -- the THEN/ELSE branches, because when analyzing these branches, we
2008 -- may call Is_Statically_Unevaluated, which expects the condition of
2009 -- an enclosing IF to have been analyze/resolved/evaluated.
2011 Analyze_Expression (Condition);
2012 Resolve (Condition, Any_Boolean);
2014 -- Analyze THEN expression and (if present) ELSE expression. For those
2015 -- we delay resolution in the normal manner, because of overloading etc.
2017 Analyze_Expression (Then_Expr);
2019 if Present (Else_Expr) then
2020 Analyze_Expression (Else_Expr);
2021 end if;
2023 -- If then expression not overloaded, then that decides the type
2025 if not Is_Overloaded (Then_Expr) then
2026 Set_Etype (N, Etype (Then_Expr));
2028 -- Case where then expression is overloaded
2030 else
2031 declare
2032 I : Interp_Index;
2033 It : Interp;
2035 begin
2036 Set_Etype (N, Any_Type);
2038 -- Loop through intepretations of Then_Expr
2040 Get_First_Interp (Then_Expr, I, It);
2041 while Present (It.Nam) loop
2043 -- Add possible intepretation of Then_Expr if no Else_Expr,
2044 -- or Else_Expr is present and has a compatible type.
2046 if No (Else_Expr)
2047 or else Has_Compatible_Type (Else_Expr, It.Typ)
2048 then
2049 Add_One_Interp (N, It.Typ, It.Typ);
2050 end if;
2052 Get_Next_Interp (I, It);
2053 end loop;
2054 end;
2055 end if;
2056 end Analyze_If_Expression;
2058 ------------------------------------
2059 -- Analyze_Indexed_Component_Form --
2060 ------------------------------------
2062 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
2063 P : constant Node_Id := Prefix (N);
2064 Exprs : constant List_Id := Expressions (N);
2065 Exp : Node_Id;
2066 P_T : Entity_Id;
2067 E : Node_Id;
2068 U_N : Entity_Id;
2070 procedure Process_Function_Call;
2071 -- Prefix in indexed component form is an overloadable entity,
2072 -- so the node is a function call. Reformat it as such.
2074 procedure Process_Indexed_Component;
2075 -- Prefix in indexed component form is actually an indexed component.
2076 -- This routine processes it, knowing that the prefix is already
2077 -- resolved.
2079 procedure Process_Indexed_Component_Or_Slice;
2080 -- An indexed component with a single index may designate a slice if
2081 -- the index is a subtype mark. This routine disambiguates these two
2082 -- cases by resolving the prefix to see if it is a subtype mark.
2084 procedure Process_Overloaded_Indexed_Component;
2085 -- If the prefix of an indexed component is overloaded, the proper
2086 -- interpretation is selected by the index types and the context.
2088 ---------------------------
2089 -- Process_Function_Call --
2090 ---------------------------
2092 procedure Process_Function_Call is
2093 Loc : constant Source_Ptr := Sloc (N);
2094 Actual : Node_Id;
2096 begin
2097 Change_Node (N, N_Function_Call);
2098 Set_Name (N, P);
2099 Set_Parameter_Associations (N, Exprs);
2101 -- Analyze actuals prior to analyzing the call itself
2103 Actual := First (Parameter_Associations (N));
2104 while Present (Actual) loop
2105 Analyze (Actual);
2106 Check_Parameterless_Call (Actual);
2108 -- Move to next actual. Note that we use Next, not Next_Actual
2109 -- here. The reason for this is a bit subtle. If a function call
2110 -- includes named associations, the parser recognizes the node as
2111 -- a call, and it is analyzed as such. If all associations are
2112 -- positional, the parser builds an indexed_component node, and
2113 -- it is only after analysis of the prefix that the construct
2114 -- is recognized as a call, in which case Process_Function_Call
2115 -- rewrites the node and analyzes the actuals. If the list of
2116 -- actuals is malformed, the parser may leave the node as an
2117 -- indexed component (despite the presence of named associations).
2118 -- The iterator Next_Actual is equivalent to Next if the list is
2119 -- positional, but follows the normalized chain of actuals when
2120 -- named associations are present. In this case normalization has
2121 -- not taken place, and actuals remain unanalyzed, which leads to
2122 -- subsequent crashes or loops if there is an attempt to continue
2123 -- analysis of the program.
2125 -- IF there is a single actual and it is a type name, the node
2126 -- can only be interpreted as a slice of a parameterless call.
2127 -- Rebuild the node as such and analyze.
2129 if No (Next (Actual))
2130 and then Is_Entity_Name (Actual)
2131 and then Is_Type (Entity (Actual))
2132 and then Is_Discrete_Type (Entity (Actual))
2133 then
2134 Replace (N,
2135 Make_Slice (Loc,
2136 Prefix => P,
2137 Discrete_Range =>
2138 New_Occurrence_Of (Entity (Actual), Loc)));
2139 Analyze (N);
2140 return;
2142 else
2143 Next (Actual);
2144 end if;
2145 end loop;
2147 Analyze_Call (N);
2148 end Process_Function_Call;
2150 -------------------------------
2151 -- Process_Indexed_Component --
2152 -------------------------------
2154 procedure Process_Indexed_Component is
2155 Exp : Node_Id;
2156 Array_Type : Entity_Id;
2157 Index : Node_Id;
2158 Pent : Entity_Id := Empty;
2160 begin
2161 Exp := First (Exprs);
2163 if Is_Overloaded (P) then
2164 Process_Overloaded_Indexed_Component;
2166 else
2167 Array_Type := Etype (P);
2169 if Is_Entity_Name (P) then
2170 Pent := Entity (P);
2171 elsif Nkind (P) = N_Selected_Component
2172 and then Is_Entity_Name (Selector_Name (P))
2173 then
2174 Pent := Entity (Selector_Name (P));
2175 end if;
2177 -- Prefix must be appropriate for an array type, taking into
2178 -- account a possible implicit dereference.
2180 if Is_Access_Type (Array_Type) then
2181 Error_Msg_NW
2182 (Warn_On_Dereference, "?d?implicit dereference", N);
2183 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2184 end if;
2186 if Is_Array_Type (Array_Type) then
2187 null;
2189 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2190 Analyze (Exp);
2191 Set_Etype (N, Any_Type);
2193 if not Has_Compatible_Type
2194 (Exp, Entry_Index_Type (Pent))
2195 then
2196 Error_Msg_N ("invalid index type in entry name", N);
2198 elsif Present (Next (Exp)) then
2199 Error_Msg_N ("too many subscripts in entry reference", N);
2201 else
2202 Set_Etype (N, Etype (P));
2203 end if;
2205 return;
2207 elsif Is_Record_Type (Array_Type)
2208 and then Remote_AST_I_Dereference (P)
2209 then
2210 return;
2212 elsif Try_Container_Indexing (N, P, Exprs) then
2213 return;
2215 elsif Array_Type = Any_Type then
2216 Set_Etype (N, Any_Type);
2218 -- In most cases the analysis of the prefix will have emitted
2219 -- an error already, but if the prefix may be interpreted as a
2220 -- call in prefixed notation, the report is left to the caller.
2221 -- To prevent cascaded errors, report only if no previous ones.
2223 if Serious_Errors_Detected = 0 then
2224 Error_Msg_N ("invalid prefix in indexed component", P);
2226 if Nkind (P) = N_Expanded_Name then
2227 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2228 end if;
2229 end if;
2231 return;
2233 -- Here we definitely have a bad indexing
2235 else
2236 if Nkind (Parent (N)) = N_Requeue_Statement
2237 and then Present (Pent) and then Ekind (Pent) = E_Entry
2238 then
2239 Error_Msg_N
2240 ("REQUEUE does not permit parameters", First (Exprs));
2242 elsif Is_Entity_Name (P)
2243 and then Etype (P) = Standard_Void_Type
2244 then
2245 Error_Msg_NE ("incorrect use of&", P, Entity (P));
2247 else
2248 Error_Msg_N ("array type required in indexed component", P);
2249 end if;
2251 Set_Etype (N, Any_Type);
2252 return;
2253 end if;
2255 Index := First_Index (Array_Type);
2256 while Present (Index) and then Present (Exp) loop
2257 if not Has_Compatible_Type (Exp, Etype (Index)) then
2258 Wrong_Type (Exp, Etype (Index));
2259 Set_Etype (N, Any_Type);
2260 return;
2261 end if;
2263 Next_Index (Index);
2264 Next (Exp);
2265 end loop;
2267 Set_Etype (N, Component_Type (Array_Type));
2268 Check_Implicit_Dereference (N, Etype (N));
2270 if Present (Index) then
2271 Error_Msg_N
2272 ("too few subscripts in array reference", First (Exprs));
2274 elsif Present (Exp) then
2275 Error_Msg_N ("too many subscripts in array reference", Exp);
2276 end if;
2277 end if;
2278 end Process_Indexed_Component;
2280 ----------------------------------------
2281 -- Process_Indexed_Component_Or_Slice --
2282 ----------------------------------------
2284 procedure Process_Indexed_Component_Or_Slice is
2285 begin
2286 Exp := First (Exprs);
2287 while Present (Exp) loop
2288 Analyze_Expression (Exp);
2289 Next (Exp);
2290 end loop;
2292 Exp := First (Exprs);
2294 -- If one index is present, and it is a subtype name, then the
2295 -- node denotes a slice (note that the case of an explicit range
2296 -- for a slice was already built as an N_Slice node in the first
2297 -- place, so that case is not handled here).
2299 -- We use a replace rather than a rewrite here because this is one
2300 -- of the cases in which the tree built by the parser is plain wrong.
2302 if No (Next (Exp))
2303 and then Is_Entity_Name (Exp)
2304 and then Is_Type (Entity (Exp))
2305 then
2306 Replace (N,
2307 Make_Slice (Sloc (N),
2308 Prefix => P,
2309 Discrete_Range => New_Copy (Exp)));
2310 Analyze (N);
2312 -- Otherwise (more than one index present, or single index is not
2313 -- a subtype name), then we have the indexed component case.
2315 else
2316 Process_Indexed_Component;
2317 end if;
2318 end Process_Indexed_Component_Or_Slice;
2320 ------------------------------------------
2321 -- Process_Overloaded_Indexed_Component --
2322 ------------------------------------------
2324 procedure Process_Overloaded_Indexed_Component is
2325 Exp : Node_Id;
2326 I : Interp_Index;
2327 It : Interp;
2328 Typ : Entity_Id;
2329 Index : Node_Id;
2330 Found : Boolean;
2332 begin
2333 Set_Etype (N, Any_Type);
2335 Get_First_Interp (P, I, It);
2336 while Present (It.Nam) loop
2337 Typ := It.Typ;
2339 if Is_Access_Type (Typ) then
2340 Typ := Designated_Type (Typ);
2341 Error_Msg_NW
2342 (Warn_On_Dereference, "?d?implicit dereference", N);
2343 end if;
2345 if Is_Array_Type (Typ) then
2347 -- Got a candidate: verify that index types are compatible
2349 Index := First_Index (Typ);
2350 Found := True;
2351 Exp := First (Exprs);
2352 while Present (Index) and then Present (Exp) loop
2353 if Has_Compatible_Type (Exp, Etype (Index)) then
2354 null;
2355 else
2356 Found := False;
2357 Remove_Interp (I);
2358 exit;
2359 end if;
2361 Next_Index (Index);
2362 Next (Exp);
2363 end loop;
2365 if Found and then No (Index) and then No (Exp) then
2366 declare
2367 CT : constant Entity_Id :=
2368 Base_Type (Component_Type (Typ));
2369 begin
2370 Add_One_Interp (N, CT, CT);
2371 Check_Implicit_Dereference (N, CT);
2372 end;
2373 end if;
2375 elsif Try_Container_Indexing (N, P, Exprs) then
2376 return;
2378 end if;
2380 Get_Next_Interp (I, It);
2381 end loop;
2383 if Etype (N) = Any_Type then
2384 Error_Msg_N ("no legal interpretation for indexed component", N);
2385 Set_Is_Overloaded (N, False);
2386 end if;
2388 End_Interp_List;
2389 end Process_Overloaded_Indexed_Component;
2391 -- Start of processing for Analyze_Indexed_Component_Form
2393 begin
2394 -- Get name of array, function or type
2396 Analyze (P);
2398 -- If P is an explicit dereference whose prefix is of a remote access-
2399 -- to-subprogram type, then N has already been rewritten as a subprogram
2400 -- call and analyzed.
2402 if Nkind (N) in N_Subprogram_Call then
2403 return;
2405 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2406 -- the indexed component denotes a loop name, the indexed form is turned
2407 -- into an attribute reference.
2409 elsif Nkind (N) = N_Attribute_Reference
2410 and then Attribute_Name (N) = Name_Loop_Entry
2411 then
2412 return;
2413 end if;
2415 pragma Assert (Nkind (N) = N_Indexed_Component);
2417 P_T := Base_Type (Etype (P));
2419 if Is_Entity_Name (P) and then Present (Entity (P)) then
2420 U_N := Entity (P);
2422 if Is_Type (U_N) then
2424 -- Reformat node as a type conversion
2426 E := Remove_Head (Exprs);
2428 if Present (First (Exprs)) then
2429 Error_Msg_N
2430 ("argument of type conversion must be single expression", N);
2431 end if;
2433 Change_Node (N, N_Type_Conversion);
2434 Set_Subtype_Mark (N, P);
2435 Set_Etype (N, U_N);
2436 Set_Expression (N, E);
2438 -- After changing the node, call for the specific Analysis
2439 -- routine directly, to avoid a double call to the expander.
2441 Analyze_Type_Conversion (N);
2442 return;
2443 end if;
2445 if Is_Overloadable (U_N) then
2446 Process_Function_Call;
2448 elsif Ekind (Etype (P)) = E_Subprogram_Type
2449 or else (Is_Access_Type (Etype (P))
2450 and then
2451 Ekind (Designated_Type (Etype (P))) =
2452 E_Subprogram_Type)
2453 then
2454 -- Call to access_to-subprogram with possible implicit dereference
2456 Process_Function_Call;
2458 elsif Is_Generic_Subprogram (U_N) then
2460 -- A common beginner's (or C++ templates fan) error
2462 Error_Msg_N ("generic subprogram cannot be called", N);
2463 Set_Etype (N, Any_Type);
2464 return;
2466 else
2467 Process_Indexed_Component_Or_Slice;
2468 end if;
2470 -- If not an entity name, prefix is an expression that may denote
2471 -- an array or an access-to-subprogram.
2473 else
2474 if Ekind (P_T) = E_Subprogram_Type
2475 or else (Is_Access_Type (P_T)
2476 and then
2477 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2478 then
2479 Process_Function_Call;
2481 elsif Nkind (P) = N_Selected_Component
2482 and then Present (Entity (Selector_Name (P)))
2483 and then Is_Overloadable (Entity (Selector_Name (P)))
2484 then
2485 Process_Function_Call;
2487 -- In ASIS mode within a generic, a prefixed call is analyzed and
2488 -- partially rewritten but the original indexed component has not
2489 -- yet been rewritten as a call. Perform the replacement now.
2491 elsif Nkind (P) = N_Selected_Component
2492 and then Nkind (Parent (P)) = N_Function_Call
2493 and then ASIS_Mode
2494 then
2495 Rewrite (N, Parent (P));
2496 Analyze (N);
2498 else
2499 -- Indexed component, slice, or a call to a member of a family
2500 -- entry, which will be converted to an entry call later.
2502 Process_Indexed_Component_Or_Slice;
2503 end if;
2504 end if;
2506 Analyze_Dimension (N);
2507 end Analyze_Indexed_Component_Form;
2509 ------------------------
2510 -- Analyze_Logical_Op --
2511 ------------------------
2513 procedure Analyze_Logical_Op (N : Node_Id) is
2514 L : constant Node_Id := Left_Opnd (N);
2515 R : constant Node_Id := Right_Opnd (N);
2516 Op_Id : Entity_Id := Entity (N);
2518 begin
2519 Set_Etype (N, Any_Type);
2520 Candidate_Type := Empty;
2522 Analyze_Expression (L);
2523 Analyze_Expression (R);
2525 if Present (Op_Id) then
2527 if Ekind (Op_Id) = E_Operator then
2528 Find_Boolean_Types (L, R, Op_Id, N);
2529 else
2530 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2531 end if;
2533 else
2534 Op_Id := Get_Name_Entity_Id (Chars (N));
2535 while Present (Op_Id) loop
2536 if Ekind (Op_Id) = E_Operator then
2537 Find_Boolean_Types (L, R, Op_Id, N);
2538 else
2539 Analyze_User_Defined_Binary_Op (N, Op_Id);
2540 end if;
2542 Op_Id := Homonym (Op_Id);
2543 end loop;
2544 end if;
2546 Operator_Check (N);
2547 end Analyze_Logical_Op;
2549 ---------------------------
2550 -- Analyze_Membership_Op --
2551 ---------------------------
2553 procedure Analyze_Membership_Op (N : Node_Id) is
2554 Loc : constant Source_Ptr := Sloc (N);
2555 L : constant Node_Id := Left_Opnd (N);
2556 R : constant Node_Id := Right_Opnd (N);
2558 Index : Interp_Index;
2559 It : Interp;
2560 Found : Boolean := False;
2561 I_F : Interp_Index;
2562 T_F : Entity_Id;
2564 procedure Try_One_Interp (T1 : Entity_Id);
2565 -- Routine to try one proposed interpretation. Note that the context
2566 -- of the operation plays no role in resolving the arguments, so that
2567 -- if there is more than one interpretation of the operands that is
2568 -- compatible with a membership test, the operation is ambiguous.
2570 --------------------
2571 -- Try_One_Interp --
2572 --------------------
2574 procedure Try_One_Interp (T1 : Entity_Id) is
2575 begin
2576 if Has_Compatible_Type (R, T1) then
2577 if Found
2578 and then Base_Type (T1) /= Base_Type (T_F)
2579 then
2580 It := Disambiguate (L, I_F, Index, Any_Type);
2582 if It = No_Interp then
2583 Ambiguous_Operands (N);
2584 Set_Etype (L, Any_Type);
2585 return;
2587 else
2588 T_F := It.Typ;
2589 end if;
2591 else
2592 Found := True;
2593 T_F := T1;
2594 I_F := Index;
2595 end if;
2597 Set_Etype (L, T_F);
2598 end if;
2599 end Try_One_Interp;
2601 procedure Analyze_Set_Membership;
2602 -- If a set of alternatives is present, analyze each and find the
2603 -- common type to which they must all resolve.
2605 ----------------------------
2606 -- Analyze_Set_Membership --
2607 ----------------------------
2609 procedure Analyze_Set_Membership is
2610 Alt : Node_Id;
2611 Index : Interp_Index;
2612 It : Interp;
2613 Candidate_Interps : Node_Id;
2614 Common_Type : Entity_Id := Empty;
2616 begin
2617 if Comes_From_Source (N) then
2618 Check_Compiler_Unit ("set membership", N);
2619 end if;
2621 Analyze (L);
2622 Candidate_Interps := L;
2624 if not Is_Overloaded (L) then
2625 Common_Type := Etype (L);
2627 Alt := First (Alternatives (N));
2628 while Present (Alt) loop
2629 Analyze (Alt);
2631 if not Has_Compatible_Type (Alt, Common_Type) then
2632 Wrong_Type (Alt, Common_Type);
2633 end if;
2635 Next (Alt);
2636 end loop;
2638 else
2639 Alt := First (Alternatives (N));
2640 while Present (Alt) loop
2641 Analyze (Alt);
2642 if not Is_Overloaded (Alt) then
2643 Common_Type := Etype (Alt);
2645 else
2646 Get_First_Interp (Alt, Index, It);
2647 while Present (It.Typ) loop
2648 if not
2649 Has_Compatible_Type (Candidate_Interps, It.Typ)
2650 then
2651 Remove_Interp (Index);
2652 end if;
2654 Get_Next_Interp (Index, It);
2655 end loop;
2657 Get_First_Interp (Alt, Index, It);
2659 if No (It.Typ) then
2660 Error_Msg_N ("alternative has no legal type", Alt);
2661 return;
2662 end if;
2664 -- If alternative is not overloaded, we have a unique type
2665 -- for all of them.
2667 Set_Etype (Alt, It.Typ);
2668 Get_Next_Interp (Index, It);
2670 if No (It.Typ) then
2671 Set_Is_Overloaded (Alt, False);
2672 Common_Type := Etype (Alt);
2673 end if;
2675 Candidate_Interps := Alt;
2676 end if;
2678 Next (Alt);
2679 end loop;
2680 end if;
2682 Set_Etype (N, Standard_Boolean);
2684 if Present (Common_Type) then
2685 Set_Etype (L, Common_Type);
2686 Set_Is_Overloaded (L, False);
2688 else
2689 Error_Msg_N ("cannot resolve membership operation", N);
2690 end if;
2691 end Analyze_Set_Membership;
2693 -- Start of processing for Analyze_Membership_Op
2695 begin
2696 Analyze_Expression (L);
2698 if No (R) and then Ada_Version >= Ada_2012 then
2699 Analyze_Set_Membership;
2700 return;
2701 end if;
2703 if Nkind (R) = N_Range
2704 or else (Nkind (R) = N_Attribute_Reference
2705 and then Attribute_Name (R) = Name_Range)
2706 then
2707 Analyze (R);
2709 if not Is_Overloaded (L) then
2710 Try_One_Interp (Etype (L));
2712 else
2713 Get_First_Interp (L, Index, It);
2714 while Present (It.Typ) loop
2715 Try_One_Interp (It.Typ);
2716 Get_Next_Interp (Index, It);
2717 end loop;
2718 end if;
2720 -- If not a range, it can be a subtype mark, or else it is a degenerate
2721 -- membership test with a singleton value, i.e. a test for equality,
2722 -- if the types are compatible.
2724 else
2725 Analyze (R);
2727 if Is_Entity_Name (R)
2728 and then Is_Type (Entity (R))
2729 then
2730 Find_Type (R);
2731 Check_Fully_Declared (Entity (R), R);
2733 elsif Ada_Version >= Ada_2012
2734 and then Has_Compatible_Type (R, Etype (L))
2735 then
2736 if Nkind (N) = N_In then
2737 Rewrite (N,
2738 Make_Op_Eq (Loc,
2739 Left_Opnd => L,
2740 Right_Opnd => R));
2741 else
2742 Rewrite (N,
2743 Make_Op_Ne (Loc,
2744 Left_Opnd => L,
2745 Right_Opnd => R));
2746 end if;
2748 Analyze (N);
2749 return;
2751 else
2752 -- In all versions of the language, if we reach this point there
2753 -- is a previous error that will be diagnosed below.
2755 Find_Type (R);
2756 end if;
2757 end if;
2759 -- Compatibility between expression and subtype mark or range is
2760 -- checked during resolution. The result of the operation is Boolean
2761 -- in any case.
2763 Set_Etype (N, Standard_Boolean);
2765 if Comes_From_Source (N)
2766 and then Present (Right_Opnd (N))
2767 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2768 then
2769 Error_Msg_N ("membership test not applicable to cpp-class types", N);
2770 end if;
2771 end Analyze_Membership_Op;
2773 -----------------
2774 -- Analyze_Mod --
2775 -----------------
2777 procedure Analyze_Mod (N : Node_Id) is
2778 begin
2779 -- A special warning check, if we have an expression of the form:
2780 -- expr mod 2 * literal
2781 -- where literal is 64 or less, then probably what was meant was
2782 -- expr mod 2 ** literal
2783 -- so issue an appropriate warning.
2785 if Warn_On_Suspicious_Modulus_Value
2786 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
2787 and then Intval (Right_Opnd (N)) = Uint_2
2788 and then Nkind (Parent (N)) = N_Op_Multiply
2789 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
2790 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
2791 then
2792 Error_Msg_N
2793 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
2794 end if;
2796 -- Remaining processing is same as for other arithmetic operators
2798 Analyze_Arithmetic_Op (N);
2799 end Analyze_Mod;
2801 ----------------------
2802 -- Analyze_Negation --
2803 ----------------------
2805 procedure Analyze_Negation (N : Node_Id) is
2806 R : constant Node_Id := Right_Opnd (N);
2807 Op_Id : Entity_Id := Entity (N);
2809 begin
2810 Set_Etype (N, Any_Type);
2811 Candidate_Type := Empty;
2813 Analyze_Expression (R);
2815 if Present (Op_Id) then
2816 if Ekind (Op_Id) = E_Operator then
2817 Find_Negation_Types (R, Op_Id, N);
2818 else
2819 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2820 end if;
2822 else
2823 Op_Id := Get_Name_Entity_Id (Chars (N));
2824 while Present (Op_Id) loop
2825 if Ekind (Op_Id) = E_Operator then
2826 Find_Negation_Types (R, Op_Id, N);
2827 else
2828 Analyze_User_Defined_Unary_Op (N, Op_Id);
2829 end if;
2831 Op_Id := Homonym (Op_Id);
2832 end loop;
2833 end if;
2835 Operator_Check (N);
2836 end Analyze_Negation;
2838 ------------------
2839 -- Analyze_Null --
2840 ------------------
2842 procedure Analyze_Null (N : Node_Id) is
2843 begin
2844 Check_SPARK_05_Restriction ("null is not allowed", N);
2846 Set_Etype (N, Any_Access);
2847 end Analyze_Null;
2849 ----------------------
2850 -- Analyze_One_Call --
2851 ----------------------
2853 procedure Analyze_One_Call
2854 (N : Node_Id;
2855 Nam : Entity_Id;
2856 Report : Boolean;
2857 Success : out Boolean;
2858 Skip_First : Boolean := False)
2860 Actuals : constant List_Id := Parameter_Associations (N);
2861 Prev_T : constant Entity_Id := Etype (N);
2863 Must_Skip : constant Boolean := Skip_First
2864 or else Nkind (Original_Node (N)) = N_Selected_Component
2865 or else
2866 (Nkind (Original_Node (N)) = N_Indexed_Component
2867 and then Nkind (Prefix (Original_Node (N)))
2868 = N_Selected_Component);
2869 -- The first formal must be omitted from the match when trying to find
2870 -- a primitive operation that is a possible interpretation, and also
2871 -- after the call has been rewritten, because the corresponding actual
2872 -- is already known to be compatible, and because this may be an
2873 -- indexing of a call with default parameters.
2875 Formal : Entity_Id;
2876 Actual : Node_Id;
2877 Is_Indexed : Boolean := False;
2878 Is_Indirect : Boolean := False;
2879 Subp_Type : constant Entity_Id := Etype (Nam);
2880 Norm_OK : Boolean;
2882 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2883 -- There may be a user-defined operator that hides the current
2884 -- interpretation. We must check for this independently of the
2885 -- analysis of the call with the user-defined operation, because
2886 -- the parameter names may be wrong and yet the hiding takes place.
2887 -- This fixes a problem with ACATS test B34014O.
2889 -- When the type Address is a visible integer type, and the DEC
2890 -- system extension is visible, the predefined operator may be
2891 -- hidden as well, by one of the address operations in auxdec.
2892 -- Finally, The abstract operations on address do not hide the
2893 -- predefined operator (this is the purpose of making them abstract).
2895 procedure Indicate_Name_And_Type;
2896 -- If candidate interpretation matches, indicate name and type of
2897 -- result on call node.
2899 ----------------------------
2900 -- Indicate_Name_And_Type --
2901 ----------------------------
2903 procedure Indicate_Name_And_Type is
2904 begin
2905 Add_One_Interp (N, Nam, Etype (Nam));
2906 Check_Implicit_Dereference (N, Etype (Nam));
2907 Success := True;
2909 -- If the prefix of the call is a name, indicate the entity
2910 -- being called. If it is not a name, it is an expression that
2911 -- denotes an access to subprogram or else an entry or family. In
2912 -- the latter case, the name is a selected component, and the entity
2913 -- being called is noted on the selector.
2915 if not Is_Type (Nam) then
2916 if Is_Entity_Name (Name (N)) then
2917 Set_Entity (Name (N), Nam);
2919 elsif Nkind (Name (N)) = N_Selected_Component then
2920 Set_Entity (Selector_Name (Name (N)), Nam);
2921 end if;
2922 end if;
2924 if Debug_Flag_E and not Report then
2925 Write_Str (" Overloaded call ");
2926 Write_Int (Int (N));
2927 Write_Str (" compatible with ");
2928 Write_Int (Int (Nam));
2929 Write_Eol;
2930 end if;
2931 end Indicate_Name_And_Type;
2933 ------------------------
2934 -- Operator_Hidden_By --
2935 ------------------------
2937 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2938 Act1 : constant Node_Id := First_Actual (N);
2939 Act2 : constant Node_Id := Next_Actual (Act1);
2940 Form1 : constant Entity_Id := First_Formal (Fun);
2941 Form2 : constant Entity_Id := Next_Formal (Form1);
2943 begin
2944 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
2945 return False;
2947 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2948 return False;
2950 elsif Present (Form2) then
2951 if No (Act2)
2952 or else not Has_Compatible_Type (Act2, Etype (Form2))
2953 then
2954 return False;
2955 end if;
2957 elsif Present (Act2) then
2958 return False;
2959 end if;
2961 -- Now we know that the arity of the operator matches the function,
2962 -- and the function call is a valid interpretation. The function
2963 -- hides the operator if it has the right signature, or if one of
2964 -- its operands is a non-abstract operation on Address when this is
2965 -- a visible integer type.
2967 return Hides_Op (Fun, Nam)
2968 or else Is_Descendent_Of_Address (Etype (Form1))
2969 or else
2970 (Present (Form2)
2971 and then Is_Descendent_Of_Address (Etype (Form2)));
2972 end Operator_Hidden_By;
2974 -- Start of processing for Analyze_One_Call
2976 begin
2977 Success := False;
2979 -- If the subprogram has no formals or if all the formals have defaults,
2980 -- and the return type is an array type, the node may denote an indexing
2981 -- of the result of a parameterless call. In Ada 2005, the subprogram
2982 -- may have one non-defaulted formal, and the call may have been written
2983 -- in prefix notation, so that the rebuilt parameter list has more than
2984 -- one actual.
2986 if not Is_Overloadable (Nam)
2987 and then Ekind (Nam) /= E_Subprogram_Type
2988 and then Ekind (Nam) /= E_Entry_Family
2989 then
2990 return;
2991 end if;
2993 -- An indexing requires at least one actual. The name of the call cannot
2994 -- be an implicit indirect call, so it cannot be a generated explicit
2995 -- dereference.
2997 if not Is_Empty_List (Actuals)
2998 and then
2999 (Needs_No_Actuals (Nam)
3000 or else
3001 (Needs_One_Actual (Nam)
3002 and then Present (Next_Actual (First (Actuals)))))
3003 then
3004 if Is_Array_Type (Subp_Type)
3005 and then
3006 (Nkind (Name (N)) /= N_Explicit_Dereference
3007 or else Comes_From_Source (Name (N)))
3008 then
3009 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
3011 elsif Is_Access_Type (Subp_Type)
3012 and then Is_Array_Type (Designated_Type (Subp_Type))
3013 then
3014 Is_Indexed :=
3015 Try_Indexed_Call
3016 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
3018 -- The prefix can also be a parameterless function that returns an
3019 -- access to subprogram, in which case this is an indirect call.
3020 -- If this succeeds, an explicit dereference is added later on,
3021 -- in Analyze_Call or Resolve_Call.
3023 elsif Is_Access_Type (Subp_Type)
3024 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
3025 then
3026 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
3027 end if;
3029 end if;
3031 -- If the call has been transformed into a slice, it is of the form
3032 -- F (Subtype) where F is parameterless. The node has been rewritten in
3033 -- Try_Indexed_Call and there is nothing else to do.
3035 if Is_Indexed
3036 and then Nkind (N) = N_Slice
3037 then
3038 return;
3039 end if;
3041 Normalize_Actuals
3042 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
3044 if not Norm_OK then
3046 -- If an indirect call is a possible interpretation, indicate
3047 -- success to the caller. This may be an indexing of an explicit
3048 -- dereference of a call that returns an access type (see above).
3050 if Is_Indirect
3051 or else (Is_Indexed
3052 and then Nkind (Name (N)) = N_Explicit_Dereference
3053 and then Comes_From_Source (Name (N)))
3054 then
3055 Success := True;
3056 return;
3058 -- Mismatch in number or names of parameters
3060 elsif Debug_Flag_E then
3061 Write_Str (" normalization fails in call ");
3062 Write_Int (Int (N));
3063 Write_Str (" with subprogram ");
3064 Write_Int (Int (Nam));
3065 Write_Eol;
3066 end if;
3068 -- If the context expects a function call, discard any interpretation
3069 -- that is a procedure. If the node is not overloaded, leave as is for
3070 -- better error reporting when type mismatch is found.
3072 elsif Nkind (N) = N_Function_Call
3073 and then Is_Overloaded (Name (N))
3074 and then Ekind (Nam) = E_Procedure
3075 then
3076 return;
3078 -- Ditto for function calls in a procedure context
3080 elsif Nkind (N) = N_Procedure_Call_Statement
3081 and then Is_Overloaded (Name (N))
3082 and then Etype (Nam) /= Standard_Void_Type
3083 then
3084 return;
3086 elsif No (Actuals) then
3088 -- If Normalize succeeds, then there are default parameters for
3089 -- all formals.
3091 Indicate_Name_And_Type;
3093 elsif Ekind (Nam) = E_Operator then
3094 if Nkind (N) = N_Procedure_Call_Statement then
3095 return;
3096 end if;
3098 -- This can occur when the prefix of the call is an operator
3099 -- name or an expanded name whose selector is an operator name.
3101 Analyze_Operator_Call (N, Nam);
3103 if Etype (N) /= Prev_T then
3105 -- Check that operator is not hidden by a function interpretation
3107 if Is_Overloaded (Name (N)) then
3108 declare
3109 I : Interp_Index;
3110 It : Interp;
3112 begin
3113 Get_First_Interp (Name (N), I, It);
3114 while Present (It.Nam) loop
3115 if Operator_Hidden_By (It.Nam) then
3116 Set_Etype (N, Prev_T);
3117 return;
3118 end if;
3120 Get_Next_Interp (I, It);
3121 end loop;
3122 end;
3123 end if;
3125 -- If operator matches formals, record its name on the call.
3126 -- If the operator is overloaded, Resolve will select the
3127 -- correct one from the list of interpretations. The call
3128 -- node itself carries the first candidate.
3130 Set_Entity (Name (N), Nam);
3131 Success := True;
3133 elsif Report and then Etype (N) = Any_Type then
3134 Error_Msg_N ("incompatible arguments for operator", N);
3135 end if;
3137 else
3138 -- Normalize_Actuals has chained the named associations in the
3139 -- correct order of the formals.
3141 Actual := First_Actual (N);
3142 Formal := First_Formal (Nam);
3144 -- If we are analyzing a call rewritten from object notation, skip
3145 -- first actual, which may be rewritten later as an explicit
3146 -- dereference.
3148 if Must_Skip then
3149 Next_Actual (Actual);
3150 Next_Formal (Formal);
3151 end if;
3153 while Present (Actual) and then Present (Formal) loop
3154 if Nkind (Parent (Actual)) /= N_Parameter_Association
3155 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3156 then
3157 -- The actual can be compatible with the formal, but we must
3158 -- also check that the context is not an address type that is
3159 -- visibly an integer type. In this case the use of literals is
3160 -- illegal, except in the body of descendents of system, where
3161 -- arithmetic operations on address are of course used.
3163 if Has_Compatible_Type (Actual, Etype (Formal))
3164 and then
3165 (Etype (Actual) /= Universal_Integer
3166 or else not Is_Descendent_Of_Address (Etype (Formal))
3167 or else
3168 Is_Predefined_File_Name
3169 (Unit_File_Name (Get_Source_Unit (N))))
3170 then
3171 Next_Actual (Actual);
3172 Next_Formal (Formal);
3174 -- In Allow_Integer_Address mode, we allow an actual integer to
3175 -- match a formal address type and vice versa. We only do this
3176 -- if we are certain that an error will otherwise be issued
3178 elsif Address_Integer_Convert_OK
3179 (Etype (Actual), Etype (Formal))
3180 and then (Report and not Is_Indexed and not Is_Indirect)
3181 then
3182 -- Handle this case by introducing an unchecked conversion
3184 Rewrite (Actual,
3185 Unchecked_Convert_To (Etype (Formal),
3186 Relocate_Node (Actual)));
3187 Analyze_And_Resolve (Actual, Etype (Formal));
3188 Next_Actual (Actual);
3189 Next_Formal (Formal);
3191 -- For an Ada 2012 predicate or invariant, a call may mention
3192 -- an incomplete type, while resolution of the corresponding
3193 -- predicate function may see the full view, as a consequence
3194 -- of the delayed resolution of the corresponding expressions.
3196 elsif Ekind (Etype (Formal)) = E_Incomplete_Type
3197 and then Full_View (Etype (Formal)) = Etype (Actual)
3198 then
3199 Set_Etype (Formal, Etype (Actual));
3200 Next_Actual (Actual);
3201 Next_Formal (Formal);
3203 else
3204 if Debug_Flag_E then
3205 Write_Str (" type checking fails in call ");
3206 Write_Int (Int (N));
3207 Write_Str (" with formal ");
3208 Write_Int (Int (Formal));
3209 Write_Str (" in subprogram ");
3210 Write_Int (Int (Nam));
3211 Write_Eol;
3212 end if;
3214 -- Comment needed on the following test???
3216 if Report and not Is_Indexed and not Is_Indirect then
3218 -- Ada 2005 (AI-251): Complete the error notification
3219 -- to help new Ada 2005 users.
3221 if Is_Class_Wide_Type (Etype (Formal))
3222 and then Is_Interface (Etype (Etype (Formal)))
3223 and then not Interface_Present_In_Ancestor
3224 (Typ => Etype (Actual),
3225 Iface => Etype (Etype (Formal)))
3226 then
3227 Error_Msg_NE
3228 ("(Ada 2005) does not implement interface }",
3229 Actual, Etype (Etype (Formal)));
3230 end if;
3232 Wrong_Type (Actual, Etype (Formal));
3234 if Nkind (Actual) = N_Op_Eq
3235 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3236 then
3237 Formal := First_Formal (Nam);
3238 while Present (Formal) loop
3239 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3240 Error_Msg_N -- CODEFIX
3241 ("possible misspelling of `='>`!", Actual);
3242 exit;
3243 end if;
3245 Next_Formal (Formal);
3246 end loop;
3247 end if;
3249 if All_Errors_Mode then
3250 Error_Msg_Sloc := Sloc (Nam);
3252 if Etype (Formal) = Any_Type then
3253 Error_Msg_N
3254 ("there is no legal actual parameter", Actual);
3255 end if;
3257 if Is_Overloadable (Nam)
3258 and then Present (Alias (Nam))
3259 and then not Comes_From_Source (Nam)
3260 then
3261 Error_Msg_NE
3262 ("\\ =='> in call to inherited operation & #!",
3263 Actual, Nam);
3265 elsif Ekind (Nam) = E_Subprogram_Type then
3266 declare
3267 Access_To_Subprogram_Typ :
3268 constant Entity_Id :=
3269 Defining_Identifier
3270 (Associated_Node_For_Itype (Nam));
3271 begin
3272 Error_Msg_NE
3273 ("\\ =='> in call to dereference of &#!",
3274 Actual, Access_To_Subprogram_Typ);
3275 end;
3277 else
3278 Error_Msg_NE
3279 ("\\ =='> in call to &#!", Actual, Nam);
3281 end if;
3282 end if;
3283 end if;
3285 return;
3286 end if;
3288 else
3289 -- Normalize_Actuals has verified that a default value exists
3290 -- for this formal. Current actual names a subsequent formal.
3292 Next_Formal (Formal);
3293 end if;
3294 end loop;
3296 -- On exit, all actuals match
3298 Indicate_Name_And_Type;
3299 end if;
3300 end Analyze_One_Call;
3302 ---------------------------
3303 -- Analyze_Operator_Call --
3304 ---------------------------
3306 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3307 Op_Name : constant Name_Id := Chars (Op_Id);
3308 Act1 : constant Node_Id := First_Actual (N);
3309 Act2 : constant Node_Id := Next_Actual (Act1);
3311 begin
3312 -- Binary operator case
3314 if Present (Act2) then
3316 -- If more than two operands, then not binary operator after all
3318 if Present (Next_Actual (Act2)) then
3319 return;
3320 end if;
3322 -- Otherwise action depends on operator
3324 case Op_Name is
3325 when Name_Op_Add |
3326 Name_Op_Subtract |
3327 Name_Op_Multiply |
3328 Name_Op_Divide |
3329 Name_Op_Mod |
3330 Name_Op_Rem |
3331 Name_Op_Expon =>
3332 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3334 when Name_Op_And |
3335 Name_Op_Or |
3336 Name_Op_Xor =>
3337 Find_Boolean_Types (Act1, Act2, Op_Id, N);
3339 when Name_Op_Lt |
3340 Name_Op_Le |
3341 Name_Op_Gt |
3342 Name_Op_Ge =>
3343 Find_Comparison_Types (Act1, Act2, Op_Id, N);
3345 when Name_Op_Eq |
3346 Name_Op_Ne =>
3347 Find_Equality_Types (Act1, Act2, Op_Id, N);
3349 when Name_Op_Concat =>
3350 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3352 -- Is this when others, or should it be an abort???
3354 when others =>
3355 null;
3356 end case;
3358 -- Unary operator case
3360 else
3361 case Op_Name is
3362 when Name_Op_Subtract |
3363 Name_Op_Add |
3364 Name_Op_Abs =>
3365 Find_Unary_Types (Act1, Op_Id, N);
3367 when Name_Op_Not =>
3368 Find_Negation_Types (Act1, Op_Id, N);
3370 -- Is this when others correct, or should it be an abort???
3372 when others =>
3373 null;
3374 end case;
3375 end if;
3376 end Analyze_Operator_Call;
3378 -------------------------------------------
3379 -- Analyze_Overloaded_Selected_Component --
3380 -------------------------------------------
3382 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3383 Nam : constant Node_Id := Prefix (N);
3384 Sel : constant Node_Id := Selector_Name (N);
3385 Comp : Entity_Id;
3386 I : Interp_Index;
3387 It : Interp;
3388 T : Entity_Id;
3390 begin
3391 Set_Etype (Sel, Any_Type);
3393 Get_First_Interp (Nam, I, It);
3394 while Present (It.Typ) loop
3395 if Is_Access_Type (It.Typ) then
3396 T := Designated_Type (It.Typ);
3397 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
3398 else
3399 T := It.Typ;
3400 end if;
3402 -- Locate the component. For a private prefix the selector can denote
3403 -- a discriminant.
3405 if Is_Record_Type (T) or else Is_Private_Type (T) then
3407 -- If the prefix is a class-wide type, the visible components are
3408 -- those of the base type.
3410 if Is_Class_Wide_Type (T) then
3411 T := Etype (T);
3412 end if;
3414 Comp := First_Entity (T);
3415 while Present (Comp) loop
3416 if Chars (Comp) = Chars (Sel)
3417 and then Is_Visible_Component (Comp)
3418 then
3420 -- AI05-105: if the context is an object renaming with
3421 -- an anonymous access type, the expected type of the
3422 -- object must be anonymous. This is a name resolution rule.
3424 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3425 or else No (Access_Definition (Parent (N)))
3426 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3427 or else
3428 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3429 then
3430 Set_Entity (Sel, Comp);
3431 Set_Etype (Sel, Etype (Comp));
3432 Add_One_Interp (N, Etype (Comp), Etype (Comp));
3433 Check_Implicit_Dereference (N, Etype (Comp));
3435 -- This also specifies a candidate to resolve the name.
3436 -- Further overloading will be resolved from context.
3437 -- The selector name itself does not carry overloading
3438 -- information.
3440 Set_Etype (Nam, It.Typ);
3442 else
3443 -- Named access type in the context of a renaming
3444 -- declaration with an access definition. Remove
3445 -- inapplicable candidate.
3447 Remove_Interp (I);
3448 end if;
3449 end if;
3451 Next_Entity (Comp);
3452 end loop;
3454 elsif Is_Concurrent_Type (T) then
3455 Comp := First_Entity (T);
3456 while Present (Comp)
3457 and then Comp /= First_Private_Entity (T)
3458 loop
3459 if Chars (Comp) = Chars (Sel) then
3460 if Is_Overloadable (Comp) then
3461 Add_One_Interp (Sel, Comp, Etype (Comp));
3462 else
3463 Set_Entity_With_Checks (Sel, Comp);
3464 Generate_Reference (Comp, Sel);
3465 end if;
3467 Set_Etype (Sel, Etype (Comp));
3468 Set_Etype (N, Etype (Comp));
3469 Set_Etype (Nam, It.Typ);
3471 -- For access type case, introduce explicit dereference for
3472 -- more uniform treatment of entry calls. Do this only once
3473 -- if several interpretations yield an access type.
3475 if Is_Access_Type (Etype (Nam))
3476 and then Nkind (Nam) /= N_Explicit_Dereference
3477 then
3478 Insert_Explicit_Dereference (Nam);
3479 Error_Msg_NW
3480 (Warn_On_Dereference, "?d?implicit dereference", N);
3481 end if;
3482 end if;
3484 Next_Entity (Comp);
3485 end loop;
3487 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3488 end if;
3490 Get_Next_Interp (I, It);
3491 end loop;
3493 if Etype (N) = Any_Type
3494 and then not Try_Object_Operation (N)
3495 then
3496 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3497 Set_Entity (Sel, Any_Id);
3498 Set_Etype (Sel, Any_Type);
3499 end if;
3500 end Analyze_Overloaded_Selected_Component;
3502 ----------------------------------
3503 -- Analyze_Qualified_Expression --
3504 ----------------------------------
3506 procedure Analyze_Qualified_Expression (N : Node_Id) is
3507 Mark : constant Entity_Id := Subtype_Mark (N);
3508 Expr : constant Node_Id := Expression (N);
3509 I : Interp_Index;
3510 It : Interp;
3511 T : Entity_Id;
3513 begin
3514 Analyze_Expression (Expr);
3516 Set_Etype (N, Any_Type);
3517 Find_Type (Mark);
3518 T := Entity (Mark);
3519 Set_Etype (N, T);
3521 if T = Any_Type then
3522 return;
3523 end if;
3525 Check_Fully_Declared (T, N);
3527 -- If expected type is class-wide, check for exact match before
3528 -- expansion, because if the expression is a dispatching call it
3529 -- may be rewritten as explicit dereference with class-wide result.
3530 -- If expression is overloaded, retain only interpretations that
3531 -- will yield exact matches.
3533 if Is_Class_Wide_Type (T) then
3534 if not Is_Overloaded (Expr) then
3535 if Base_Type (Etype (Expr)) /= Base_Type (T) then
3536 if Nkind (Expr) = N_Aggregate then
3537 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3538 else
3539 Wrong_Type (Expr, T);
3540 end if;
3541 end if;
3543 else
3544 Get_First_Interp (Expr, I, It);
3546 while Present (It.Nam) loop
3547 if Base_Type (It.Typ) /= Base_Type (T) then
3548 Remove_Interp (I);
3549 end if;
3551 Get_Next_Interp (I, It);
3552 end loop;
3553 end if;
3554 end if;
3556 Set_Etype (N, T);
3557 end Analyze_Qualified_Expression;
3559 -----------------------------------
3560 -- Analyze_Quantified_Expression --
3561 -----------------------------------
3563 procedure Analyze_Quantified_Expression (N : Node_Id) is
3564 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
3565 -- If the iterator is part of a quantified expression, and the range is
3566 -- known to be statically empty, emit a warning and replace expression
3567 -- with its static value. Returns True if the replacement occurs.
3569 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
3570 -- Determine whether if expression If_Expr lacks an else part or if it
3571 -- has one, it evaluates to True.
3573 --------------------
3574 -- Is_Empty_Range --
3575 --------------------
3577 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
3578 Loc : constant Source_Ptr := Sloc (N);
3580 begin
3581 if Is_Array_Type (Typ)
3582 and then Compile_Time_Known_Bounds (Typ)
3583 and then
3584 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
3585 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
3586 then
3587 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
3589 if All_Present (N) then
3590 Error_Msg_N
3591 ("??quantified expression with ALL "
3592 & "over a null range has value True", N);
3593 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3595 else
3596 Error_Msg_N
3597 ("??quantified expression with SOME "
3598 & "over a null range has value False", N);
3599 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3600 end if;
3602 Analyze (N);
3603 return True;
3605 else
3606 return False;
3607 end if;
3608 end Is_Empty_Range;
3610 -----------------------------
3611 -- No_Else_Or_Trivial_True --
3612 -----------------------------
3614 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
3615 Else_Expr : constant Node_Id :=
3616 Next (Next (First (Expressions (If_Expr))));
3617 begin
3618 return
3619 No (Else_Expr)
3620 or else (Compile_Time_Known_Value (Else_Expr)
3621 and then Is_True (Expr_Value (Else_Expr)));
3622 end No_Else_Or_Trivial_True;
3624 -- Local variables
3626 Cond : constant Node_Id := Condition (N);
3627 Loop_Id : Entity_Id;
3628 QE_Scop : Entity_Id;
3630 -- Start of processing for Analyze_Quantified_Expression
3632 begin
3633 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
3635 -- Create a scope to emulate the loop-like behavior of the quantified
3636 -- expression. The scope is needed to provide proper visibility of the
3637 -- loop variable.
3639 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
3640 Set_Etype (QE_Scop, Standard_Void_Type);
3641 Set_Scope (QE_Scop, Current_Scope);
3642 Set_Parent (QE_Scop, N);
3644 Push_Scope (QE_Scop);
3646 -- All constituents are preanalyzed and resolved to avoid untimely
3647 -- generation of various temporaries and types. Full analysis and
3648 -- expansion is carried out when the quantified expression is
3649 -- transformed into an expression with actions.
3651 if Present (Iterator_Specification (N)) then
3652 Preanalyze (Iterator_Specification (N));
3654 -- Do not proceed with the analysis when the range of iteration is
3655 -- empty. The appropriate error is issued by Is_Empty_Range.
3657 if Is_Entity_Name (Name (Iterator_Specification (N)))
3658 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
3659 then
3660 return;
3661 end if;
3663 else pragma Assert (Present (Loop_Parameter_Specification (N)));
3664 declare
3665 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
3667 begin
3668 Preanalyze (Loop_Par);
3670 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
3671 and then Parent (Loop_Par) /= N
3672 then
3673 -- The parser cannot distinguish between a loop specification
3674 -- and an iterator specification. If after pre-analysis the
3675 -- proper form has been recognized, rewrite the expression to
3676 -- reflect the right kind. This is needed for proper ASIS
3677 -- navigation. If expansion is enabled, the transformation is
3678 -- performed when the expression is rewritten as a loop.
3680 Set_Iterator_Specification (N,
3681 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
3683 Set_Defining_Identifier (Iterator_Specification (N),
3684 Relocate_Node (Defining_Identifier (Loop_Par)));
3685 Set_Name (Iterator_Specification (N),
3686 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
3687 Set_Comes_From_Source (Iterator_Specification (N),
3688 Comes_From_Source (Loop_Parameter_Specification (N)));
3689 Set_Loop_Parameter_Specification (N, Empty);
3690 end if;
3691 end;
3692 end if;
3694 Preanalyze_And_Resolve (Cond, Standard_Boolean);
3696 End_Scope;
3697 Set_Etype (N, Standard_Boolean);
3699 -- Verify that the loop variable is used within the condition of the
3700 -- quantified expression.
3702 if Present (Iterator_Specification (N)) then
3703 Loop_Id := Defining_Identifier (Iterator_Specification (N));
3704 else
3705 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
3706 end if;
3708 if Warn_On_Suspicious_Contract
3709 and then not Referenced (Loop_Id, Cond)
3710 then
3711 Error_Msg_N ("?T?unused variable &", Loop_Id);
3712 end if;
3714 -- Diagnose a possible misuse of the SOME existential quantifier. When
3715 -- we have a quantified expression of the form:
3717 -- for some X => (if P then Q [else True])
3719 -- any value for X that makes P False results in the if expression being
3720 -- trivially True, and so also results in the the quantified expression
3721 -- being trivially True.
3723 if Warn_On_Suspicious_Contract
3724 and then not All_Present (N)
3725 and then Nkind (Cond) = N_If_Expression
3726 and then No_Else_Or_Trivial_True (Cond)
3727 then
3728 Error_Msg_N ("?T?suspicious expression", N);
3729 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
3730 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
3731 end if;
3732 end Analyze_Quantified_Expression;
3734 -------------------
3735 -- Analyze_Range --
3736 -------------------
3738 procedure Analyze_Range (N : Node_Id) is
3739 L : constant Node_Id := Low_Bound (N);
3740 H : constant Node_Id := High_Bound (N);
3741 I1, I2 : Interp_Index;
3742 It1, It2 : Interp;
3744 procedure Check_Common_Type (T1, T2 : Entity_Id);
3745 -- Verify the compatibility of two types, and choose the
3746 -- non universal one if the other is universal.
3748 procedure Check_High_Bound (T : Entity_Id);
3749 -- Test one interpretation of the low bound against all those
3750 -- of the high bound.
3752 procedure Check_Universal_Expression (N : Node_Id);
3753 -- In Ada 83, reject bounds of a universal range that are not literals
3754 -- or entity names.
3756 -----------------------
3757 -- Check_Common_Type --
3758 -----------------------
3760 procedure Check_Common_Type (T1, T2 : Entity_Id) is
3761 begin
3762 if Covers (T1 => T1, T2 => T2)
3763 or else
3764 Covers (T1 => T2, T2 => T1)
3765 then
3766 if T1 = Universal_Integer
3767 or else T1 = Universal_Real
3768 or else T1 = Any_Character
3769 then
3770 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3772 elsif T1 = T2 then
3773 Add_One_Interp (N, T1, T1);
3775 else
3776 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3777 end if;
3778 end if;
3779 end Check_Common_Type;
3781 ----------------------
3782 -- Check_High_Bound --
3783 ----------------------
3785 procedure Check_High_Bound (T : Entity_Id) is
3786 begin
3787 if not Is_Overloaded (H) then
3788 Check_Common_Type (T, Etype (H));
3789 else
3790 Get_First_Interp (H, I2, It2);
3791 while Present (It2.Typ) loop
3792 Check_Common_Type (T, It2.Typ);
3793 Get_Next_Interp (I2, It2);
3794 end loop;
3795 end if;
3796 end Check_High_Bound;
3798 -----------------------------
3799 -- Is_Universal_Expression --
3800 -----------------------------
3802 procedure Check_Universal_Expression (N : Node_Id) is
3803 begin
3804 if Etype (N) = Universal_Integer
3805 and then Nkind (N) /= N_Integer_Literal
3806 and then not Is_Entity_Name (N)
3807 and then Nkind (N) /= N_Attribute_Reference
3808 then
3809 Error_Msg_N ("illegal bound in discrete range", N);
3810 end if;
3811 end Check_Universal_Expression;
3813 -- Start of processing for Analyze_Range
3815 begin
3816 Set_Etype (N, Any_Type);
3817 Analyze_Expression (L);
3818 Analyze_Expression (H);
3820 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3821 return;
3823 else
3824 if not Is_Overloaded (L) then
3825 Check_High_Bound (Etype (L));
3826 else
3827 Get_First_Interp (L, I1, It1);
3828 while Present (It1.Typ) loop
3829 Check_High_Bound (It1.Typ);
3830 Get_Next_Interp (I1, It1);
3831 end loop;
3832 end if;
3834 -- If result is Any_Type, then we did not find a compatible pair
3836 if Etype (N) = Any_Type then
3837 Error_Msg_N ("incompatible types in range ", N);
3838 end if;
3839 end if;
3841 if Ada_Version = Ada_83
3842 and then
3843 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3844 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3845 then
3846 Check_Universal_Expression (L);
3847 Check_Universal_Expression (H);
3848 end if;
3850 Check_Function_Writable_Actuals (N);
3851 end Analyze_Range;
3853 -----------------------
3854 -- Analyze_Reference --
3855 -----------------------
3857 procedure Analyze_Reference (N : Node_Id) is
3858 P : constant Node_Id := Prefix (N);
3859 E : Entity_Id;
3860 T : Entity_Id;
3861 Acc_Type : Entity_Id;
3863 begin
3864 Analyze (P);
3866 -- An interesting error check, if we take the 'Reference of an object
3867 -- for which a pragma Atomic or Volatile has been given, and the type
3868 -- of the object is not Atomic or Volatile, then we are in trouble. The
3869 -- problem is that no trace of the atomic/volatile status will remain
3870 -- for the backend to respect when it deals with the resulting pointer,
3871 -- since the pointer type will not be marked atomic (it is a pointer to
3872 -- the base type of the object).
3874 -- It is not clear if that can ever occur, but in case it does, we will
3875 -- generate an error message. Not clear if this message can ever be
3876 -- generated, and pretty clear that it represents a bug if it is, still
3877 -- seems worth checking, except in CodePeer mode where we do not really
3878 -- care and don't want to bother the user.
3880 T := Etype (P);
3882 if Is_Entity_Name (P)
3883 and then Is_Object_Reference (P)
3884 and then not CodePeer_Mode
3885 then
3886 E := Entity (P);
3887 T := Etype (P);
3889 if (Has_Atomic_Components (E)
3890 and then not Has_Atomic_Components (T))
3891 or else
3892 (Has_Volatile_Components (E)
3893 and then not Has_Volatile_Components (T))
3894 or else (Is_Atomic (E) and then not Is_Atomic (T))
3895 or else (Is_Volatile (E) and then not Is_Volatile (T))
3896 then
3897 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3898 end if;
3899 end if;
3901 -- Carry on with normal processing
3903 Acc_Type := Create_Itype (E_Allocator_Type, N);
3904 Set_Etype (Acc_Type, Acc_Type);
3905 Set_Directly_Designated_Type (Acc_Type, Etype (P));
3906 Set_Etype (N, Acc_Type);
3907 end Analyze_Reference;
3909 --------------------------------
3910 -- Analyze_Selected_Component --
3911 --------------------------------
3913 -- Prefix is a record type or a task or protected type. In the latter case,
3914 -- the selector must denote a visible entry.
3916 procedure Analyze_Selected_Component (N : Node_Id) is
3917 Name : constant Node_Id := Prefix (N);
3918 Sel : constant Node_Id := Selector_Name (N);
3919 Act_Decl : Node_Id;
3920 Comp : Entity_Id;
3921 Has_Candidate : Boolean := False;
3922 In_Scope : Boolean;
3923 Parent_N : Node_Id;
3924 Pent : Entity_Id := Empty;
3925 Prefix_Type : Entity_Id;
3927 Type_To_Use : Entity_Id;
3928 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
3929 -- a class-wide type, we use its root type, whose components are
3930 -- present in the class-wide type.
3932 Is_Single_Concurrent_Object : Boolean;
3933 -- Set True if the prefix is a single task or a single protected object
3935 procedure Find_Component_In_Instance (Rec : Entity_Id);
3936 -- In an instance, a component of a private extension may not be visible
3937 -- while it was visible in the generic. Search candidate scope for a
3938 -- component with the proper identifier. This is only done if all other
3939 -- searches have failed. If a match is found, the Etype of both N and
3940 -- Sel are set from this component, and the entity of Sel is set to
3941 -- reference this component. If no match is found, Entity (Sel) remains
3942 -- unset.
3944 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3945 -- It is known that the parent of N denotes a subprogram call. Comp
3946 -- is an overloadable component of the concurrent type of the prefix.
3947 -- Determine whether all formals of the parent of N and Comp are mode
3948 -- conformant. If the parent node is not analyzed yet it may be an
3949 -- indexed component rather than a function call.
3951 --------------------------------
3952 -- Find_Component_In_Instance --
3953 --------------------------------
3955 procedure Find_Component_In_Instance (Rec : Entity_Id) is
3956 Comp : Entity_Id;
3958 begin
3959 Comp := First_Component (Rec);
3960 while Present (Comp) loop
3961 if Chars (Comp) = Chars (Sel) then
3962 Set_Entity_With_Checks (Sel, Comp);
3963 Set_Etype (Sel, Etype (Comp));
3964 Set_Etype (N, Etype (Comp));
3965 return;
3966 end if;
3968 Next_Component (Comp);
3969 end loop;
3971 -- If we fall through, no match, so no changes made
3973 return;
3974 end Find_Component_In_Instance;
3976 ------------------------------
3977 -- Has_Mode_Conformant_Spec --
3978 ------------------------------
3980 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3981 Comp_Param : Entity_Id;
3982 Param : Node_Id;
3983 Param_Typ : Entity_Id;
3985 begin
3986 Comp_Param := First_Formal (Comp);
3988 if Nkind (Parent (N)) = N_Indexed_Component then
3989 Param := First (Expressions (Parent (N)));
3990 else
3991 Param := First (Parameter_Associations (Parent (N)));
3992 end if;
3994 while Present (Comp_Param)
3995 and then Present (Param)
3996 loop
3997 Param_Typ := Find_Parameter_Type (Param);
3999 if Present (Param_Typ)
4000 and then
4001 not Conforming_Types
4002 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4003 then
4004 return False;
4005 end if;
4007 Next_Formal (Comp_Param);
4008 Next (Param);
4009 end loop;
4011 -- One of the specs has additional formals; there is no match, unless
4012 -- this may be an indexing of a parameterless call.
4014 -- Note that when expansion is disabled, the corresponding record
4015 -- type of synchronized types is not constructed, so that there is
4016 -- no point is attempting an interpretation as a prefixed call, as
4017 -- this is bound to fail because the primitive operations will not
4018 -- be properly located.
4020 if Present (Comp_Param) or else Present (Param) then
4021 if Needs_No_Actuals (Comp)
4022 and then Is_Array_Type (Etype (Comp))
4023 and then not Expander_Active
4024 then
4025 return True;
4026 else
4027 return False;
4028 end if;
4029 end if;
4031 return True;
4032 end Has_Mode_Conformant_Spec;
4034 -- Start of processing for Analyze_Selected_Component
4036 begin
4037 Set_Etype (N, Any_Type);
4039 if Is_Overloaded (Name) then
4040 Analyze_Overloaded_Selected_Component (N);
4041 return;
4043 elsif Etype (Name) = Any_Type then
4044 Set_Entity (Sel, Any_Id);
4045 Set_Etype (Sel, Any_Type);
4046 return;
4048 else
4049 Prefix_Type := Etype (Name);
4050 end if;
4052 if Is_Access_Type (Prefix_Type) then
4054 -- A RACW object can never be used as prefix of a selected component
4055 -- since that means it is dereferenced without being a controlling
4056 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4057 -- reporting an error, we must check whether this is actually a
4058 -- dispatching call in prefix form.
4060 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4061 and then Comes_From_Source (N)
4062 then
4063 if Try_Object_Operation (N) then
4064 return;
4065 else
4066 Error_Msg_N
4067 ("invalid dereference of a remote access-to-class-wide value",
4069 end if;
4071 -- Normal case of selected component applied to access type
4073 else
4074 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4076 if Is_Entity_Name (Name) then
4077 Pent := Entity (Name);
4078 elsif Nkind (Name) = N_Selected_Component
4079 and then Is_Entity_Name (Selector_Name (Name))
4080 then
4081 Pent := Entity (Selector_Name (Name));
4082 end if;
4084 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
4085 end if;
4087 -- If we have an explicit dereference of a remote access-to-class-wide
4088 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4089 -- have to check for the case of a prefix that is a controlling operand
4090 -- of a prefixed dispatching call, as the dereference is legal in that
4091 -- case. Normally this condition is checked in Validate_Remote_Access_
4092 -- To_Class_Wide_Type, but we have to defer the checking for selected
4093 -- component prefixes because of the prefixed dispatching call case.
4094 -- Note that implicit dereferences are checked for this just above.
4096 elsif Nkind (Name) = N_Explicit_Dereference
4097 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4098 and then Comes_From_Source (N)
4099 then
4100 if Try_Object_Operation (N) then
4101 return;
4102 else
4103 Error_Msg_N
4104 ("invalid dereference of a remote access-to-class-wide value",
4106 end if;
4107 end if;
4109 -- (Ada 2005): if the prefix is the limited view of a type, and
4110 -- the context already includes the full view, use the full view
4111 -- in what follows, either to retrieve a component of to find
4112 -- a primitive operation. If the prefix is an explicit dereference,
4113 -- set the type of the prefix to reflect this transformation.
4114 -- If the non-limited view is itself an incomplete type, get the
4115 -- full view if available.
4117 if Is_Incomplete_Type (Prefix_Type)
4118 and then From_Limited_With (Prefix_Type)
4119 and then Present (Non_Limited_View (Prefix_Type))
4120 then
4121 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
4123 if Nkind (N) = N_Explicit_Dereference then
4124 Set_Etype (Prefix (N), Prefix_Type);
4125 end if;
4127 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
4128 and then From_Limited_With (Prefix_Type)
4129 and then Present (Non_Limited_View (Etype (Prefix_Type)))
4130 then
4131 Prefix_Type :=
4132 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
4134 if Nkind (N) = N_Explicit_Dereference then
4135 Set_Etype (Prefix (N), Prefix_Type);
4136 end if;
4137 end if;
4139 if Ekind (Prefix_Type) = E_Private_Subtype then
4140 Prefix_Type := Base_Type (Prefix_Type);
4141 end if;
4143 Type_To_Use := Prefix_Type;
4145 -- For class-wide types, use the entity list of the root type. This
4146 -- indirection is specially important for private extensions because
4147 -- only the root type get switched (not the class-wide type).
4149 if Is_Class_Wide_Type (Prefix_Type) then
4150 Type_To_Use := Root_Type (Prefix_Type);
4151 end if;
4153 -- If the prefix is a single concurrent object, use its name in error
4154 -- messages, rather than that of its anonymous type.
4156 Is_Single_Concurrent_Object :=
4157 Is_Concurrent_Type (Prefix_Type)
4158 and then Is_Internal_Name (Chars (Prefix_Type))
4159 and then not Is_Derived_Type (Prefix_Type)
4160 and then Is_Entity_Name (Name);
4162 Comp := First_Entity (Type_To_Use);
4164 -- If the selector has an original discriminant, the node appears in
4165 -- an instance. Replace the discriminant with the corresponding one
4166 -- in the current discriminated type. For nested generics, this must
4167 -- be done transitively, so note the new original discriminant.
4169 if Nkind (Sel) = N_Identifier
4170 and then In_Instance
4171 and then Present (Original_Discriminant (Sel))
4172 then
4173 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4175 -- Mark entity before rewriting, for completeness and because
4176 -- subsequent semantic checks might examine the original node.
4178 Set_Entity (Sel, Comp);
4179 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
4180 Set_Original_Discriminant (Selector_Name (N), Comp);
4181 Set_Etype (N, Etype (Comp));
4182 Check_Implicit_Dereference (N, Etype (Comp));
4184 if Is_Access_Type (Etype (Name)) then
4185 Insert_Explicit_Dereference (Name);
4186 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4187 end if;
4189 elsif Is_Record_Type (Prefix_Type) then
4191 -- Find component with given name. In an instance, if the node is
4192 -- known as a prefixed call, do not examine components whose
4193 -- visibility may be accidental.
4195 while Present (Comp) and then not Is_Prefixed_Call (N) loop
4196 if Chars (Comp) = Chars (Sel)
4197 and then Is_Visible_Component (Comp, N)
4198 then
4199 Set_Entity_With_Checks (Sel, Comp);
4200 Set_Etype (Sel, Etype (Comp));
4202 if Ekind (Comp) = E_Discriminant then
4203 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
4204 Error_Msg_N
4205 ("cannot reference discriminant of unchecked union",
4206 Sel);
4207 end if;
4209 if Is_Generic_Type (Prefix_Type)
4210 or else
4211 Is_Generic_Type (Root_Type (Prefix_Type))
4212 then
4213 Set_Original_Discriminant (Sel, Comp);
4214 end if;
4215 end if;
4217 -- Resolve the prefix early otherwise it is not possible to
4218 -- build the actual subtype of the component: it may need
4219 -- to duplicate this prefix and duplication is only allowed
4220 -- on fully resolved expressions.
4222 Resolve (Name);
4224 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4225 -- subtypes in a package specification.
4226 -- Example:
4228 -- limited with Pkg;
4229 -- package Pkg is
4230 -- type Acc_Inc is access Pkg.T;
4231 -- X : Acc_Inc;
4232 -- N : Natural := X.all.Comp; -- ERROR, limited view
4233 -- end Pkg; -- Comp is not visible
4235 if Nkind (Name) = N_Explicit_Dereference
4236 and then From_Limited_With (Etype (Prefix (Name)))
4237 and then not Is_Potentially_Use_Visible (Etype (Name))
4238 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4239 N_Package_Specification
4240 then
4241 Error_Msg_NE
4242 ("premature usage of incomplete}", Prefix (Name),
4243 Etype (Prefix (Name)));
4244 end if;
4246 -- We never need an actual subtype for the case of a selection
4247 -- for a indexed component of a non-packed array, since in
4248 -- this case gigi generates all the checks and can find the
4249 -- necessary bounds information.
4251 -- We also do not need an actual subtype for the case of a
4252 -- first, last, length, or range attribute applied to a
4253 -- non-packed array, since gigi can again get the bounds in
4254 -- these cases (gigi cannot handle the packed case, since it
4255 -- has the bounds of the packed array type, not the original
4256 -- bounds of the type). However, if the prefix is itself a
4257 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4258 -- as a dynamic-sized temporary, so we do generate an actual
4259 -- subtype for this case.
4261 Parent_N := Parent (N);
4263 if not Is_Packed (Etype (Comp))
4264 and then
4265 ((Nkind (Parent_N) = N_Indexed_Component
4266 and then Nkind (Name) /= N_Selected_Component)
4267 or else
4268 (Nkind (Parent_N) = N_Attribute_Reference
4269 and then
4270 Nam_In (Attribute_Name (Parent_N), Name_First,
4271 Name_Last,
4272 Name_Length,
4273 Name_Range)))
4274 then
4275 Set_Etype (N, Etype (Comp));
4277 -- If full analysis is not enabled, we do not generate an
4278 -- actual subtype, because in the absence of expansion
4279 -- reference to a formal of a protected type, for example,
4280 -- will not be properly transformed, and will lead to
4281 -- out-of-scope references in gigi.
4283 -- In all other cases, we currently build an actual subtype.
4284 -- It seems likely that many of these cases can be avoided,
4285 -- but right now, the front end makes direct references to the
4286 -- bounds (e.g. in generating a length check), and if we do
4287 -- not make an actual subtype, we end up getting a direct
4288 -- reference to a discriminant, which will not do.
4290 elsif Full_Analysis then
4291 Act_Decl :=
4292 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4293 Insert_Action (N, Act_Decl);
4295 if No (Act_Decl) then
4296 Set_Etype (N, Etype (Comp));
4298 else
4299 -- Component type depends on discriminants. Enter the
4300 -- main attributes of the subtype.
4302 declare
4303 Subt : constant Entity_Id :=
4304 Defining_Identifier (Act_Decl);
4306 begin
4307 Set_Etype (Subt, Base_Type (Etype (Comp)));
4308 Set_Ekind (Subt, Ekind (Etype (Comp)));
4309 Set_Etype (N, Subt);
4310 end;
4311 end if;
4313 -- If Full_Analysis not enabled, just set the Etype
4315 else
4316 Set_Etype (N, Etype (Comp));
4317 end if;
4319 Check_Implicit_Dereference (N, Etype (N));
4320 return;
4321 end if;
4323 -- If the prefix is a private extension, check only the visible
4324 -- components of the partial view. This must include the tag,
4325 -- which can appear in expanded code in a tag check.
4327 if Ekind (Type_To_Use) = E_Record_Type_With_Private
4328 and then Chars (Selector_Name (N)) /= Name_uTag
4329 then
4330 exit when Comp = Last_Entity (Type_To_Use);
4331 end if;
4333 Next_Entity (Comp);
4334 end loop;
4336 -- Ada 2005 (AI-252): The selected component can be interpreted as
4337 -- a prefixed view of a subprogram. Depending on the context, this is
4338 -- either a name that can appear in a renaming declaration, or part
4339 -- of an enclosing call given in prefix form.
4341 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4342 -- selected component should resolve to a name.
4344 if Ada_Version >= Ada_2005
4345 and then Is_Tagged_Type (Prefix_Type)
4346 and then not Is_Concurrent_Type (Prefix_Type)
4347 then
4348 if Nkind (Parent (N)) = N_Generic_Association
4349 or else Nkind (Parent (N)) = N_Requeue_Statement
4350 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4351 then
4352 if Find_Primitive_Operation (N) then
4353 return;
4354 end if;
4356 elsif Try_Object_Operation (N) then
4357 return;
4358 end if;
4360 -- If the transformation fails, it will be necessary to redo the
4361 -- analysis with all errors enabled, to indicate candidate
4362 -- interpretations and reasons for each failure ???
4364 end if;
4366 elsif Is_Private_Type (Prefix_Type) then
4368 -- Allow access only to discriminants of the type. If the type has
4369 -- no full view, gigi uses the parent type for the components, so we
4370 -- do the same here.
4372 if No (Full_View (Prefix_Type)) then
4373 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4374 Comp := First_Entity (Type_To_Use);
4375 end if;
4377 while Present (Comp) loop
4378 if Chars (Comp) = Chars (Sel) then
4379 if Ekind (Comp) = E_Discriminant then
4380 Set_Entity_With_Checks (Sel, Comp);
4381 Generate_Reference (Comp, Sel);
4383 Set_Etype (Sel, Etype (Comp));
4384 Set_Etype (N, Etype (Comp));
4385 Check_Implicit_Dereference (N, Etype (N));
4387 if Is_Generic_Type (Prefix_Type)
4388 or else Is_Generic_Type (Root_Type (Prefix_Type))
4389 then
4390 Set_Original_Discriminant (Sel, Comp);
4391 end if;
4393 -- Before declaring an error, check whether this is tagged
4394 -- private type and a call to a primitive operation.
4396 elsif Ada_Version >= Ada_2005
4397 and then Is_Tagged_Type (Prefix_Type)
4398 and then Try_Object_Operation (N)
4399 then
4400 return;
4402 else
4403 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4404 Error_Msg_NE ("invisible selector& for }", N, Sel);
4405 Set_Entity (Sel, Any_Id);
4406 Set_Etype (N, Any_Type);
4407 end if;
4409 return;
4410 end if;
4412 Next_Entity (Comp);
4413 end loop;
4415 elsif Is_Concurrent_Type (Prefix_Type) then
4417 -- Find visible operation with given name. For a protected type,
4418 -- the possible candidates are discriminants, entries or protected
4419 -- procedures. For a task type, the set can only include entries or
4420 -- discriminants if the task type is not an enclosing scope. If it
4421 -- is an enclosing scope (e.g. in an inner task) then all entities
4422 -- are visible, but the prefix must denote the enclosing scope, i.e.
4423 -- can only be a direct name or an expanded name.
4425 Set_Etype (Sel, Any_Type);
4426 In_Scope := In_Open_Scopes (Prefix_Type);
4428 while Present (Comp) loop
4429 if Chars (Comp) = Chars (Sel) then
4430 if Is_Overloadable (Comp) then
4431 Add_One_Interp (Sel, Comp, Etype (Comp));
4433 -- If the prefix is tagged, the correct interpretation may
4434 -- lie in the primitive or class-wide operations of the
4435 -- type. Perform a simple conformance check to determine
4436 -- whether Try_Object_Operation should be invoked even if
4437 -- a visible entity is found.
4439 if Is_Tagged_Type (Prefix_Type)
4440 and then
4441 Nkind_In (Parent (N), N_Procedure_Call_Statement,
4442 N_Function_Call,
4443 N_Indexed_Component)
4444 and then Has_Mode_Conformant_Spec (Comp)
4445 then
4446 Has_Candidate := True;
4447 end if;
4449 -- Note: a selected component may not denote a component of a
4450 -- protected type (4.1.3(7)).
4452 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4453 or else (In_Scope
4454 and then not Is_Protected_Type (Prefix_Type)
4455 and then Is_Entity_Name (Name))
4456 then
4457 Set_Entity_With_Checks (Sel, Comp);
4458 Generate_Reference (Comp, Sel);
4460 -- The selector is not overloadable, so we have a candidate
4461 -- interpretation.
4463 Has_Candidate := True;
4465 else
4466 goto Next_Comp;
4467 end if;
4469 Set_Etype (Sel, Etype (Comp));
4470 Set_Etype (N, Etype (Comp));
4472 if Ekind (Comp) = E_Discriminant then
4473 Set_Original_Discriminant (Sel, Comp);
4474 end if;
4476 -- For access type case, introduce explicit dereference for
4477 -- more uniform treatment of entry calls.
4479 if Is_Access_Type (Etype (Name)) then
4480 Insert_Explicit_Dereference (Name);
4481 Error_Msg_NW
4482 (Warn_On_Dereference, "?d?implicit dereference", N);
4483 end if;
4484 end if;
4486 <<Next_Comp>>
4487 Next_Entity (Comp);
4488 exit when not In_Scope
4489 and then
4490 Comp = First_Private_Entity (Base_Type (Prefix_Type));
4491 end loop;
4493 -- If there is no visible entity with the given name or none of the
4494 -- visible entities are plausible interpretations, check whether
4495 -- there is some other primitive operation with that name.
4497 if Ada_Version >= Ada_2005
4498 and then Is_Tagged_Type (Prefix_Type)
4499 then
4500 if (Etype (N) = Any_Type
4501 or else not Has_Candidate)
4502 and then Try_Object_Operation (N)
4503 then
4504 return;
4506 -- If the context is not syntactically a procedure call, it
4507 -- may be a call to a primitive function declared outside of
4508 -- the synchronized type.
4510 -- If the context is a procedure call, there might still be
4511 -- an overloading between an entry and a primitive procedure
4512 -- declared outside of the synchronized type, called in prefix
4513 -- notation. This is harder to disambiguate because in one case
4514 -- the controlling formal is implicit ???
4516 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4517 and then Nkind (Parent (N)) /= N_Indexed_Component
4518 and then Try_Object_Operation (N)
4519 then
4520 return;
4521 end if;
4523 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4524 -- entry or procedure of a tagged concurrent type we must check
4525 -- if there are class-wide subprograms covering the primitive. If
4526 -- true then Try_Object_Operation reports the error.
4528 if Has_Candidate
4529 and then Is_Concurrent_Type (Prefix_Type)
4530 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4532 -- Duplicate the call. This is required to avoid problems with
4533 -- the tree transformations performed by Try_Object_Operation.
4534 -- Set properly the parent of the copied call, because it is
4535 -- about to be reanalyzed.
4537 then
4538 declare
4539 Par : constant Node_Id := New_Copy_Tree (Parent (N));
4541 begin
4542 Set_Parent (Par, Parent (Parent (N)));
4544 if Try_Object_Operation
4545 (Sinfo.Name (Par), CW_Test_Only => True)
4546 then
4547 return;
4548 end if;
4549 end;
4550 end if;
4551 end if;
4553 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4555 -- Case of a prefix of a protected type: selector might denote
4556 -- an invisible private component.
4558 Comp := First_Private_Entity (Base_Type (Prefix_Type));
4559 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4560 Next_Entity (Comp);
4561 end loop;
4563 if Present (Comp) then
4564 if Is_Single_Concurrent_Object then
4565 Error_Msg_Node_2 := Entity (Name);
4566 Error_Msg_NE ("invisible selector& for &", N, Sel);
4568 else
4569 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4570 Error_Msg_NE ("invisible selector& for }", N, Sel);
4571 end if;
4572 return;
4573 end if;
4574 end if;
4576 Set_Is_Overloaded (N, Is_Overloaded (Sel));
4578 else
4579 -- Invalid prefix
4581 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4582 end if;
4584 -- If N still has no type, the component is not defined in the prefix
4586 if Etype (N) = Any_Type then
4588 if Is_Single_Concurrent_Object then
4589 Error_Msg_Node_2 := Entity (Name);
4590 Error_Msg_NE ("no selector& for&", N, Sel);
4592 Check_Misspelled_Selector (Type_To_Use, Sel);
4594 -- If this is a derived formal type, the parent may have different
4595 -- visibility at this point. Try for an inherited component before
4596 -- reporting an error.
4598 elsif Is_Generic_Type (Prefix_Type)
4599 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4600 and then Prefix_Type /= Etype (Prefix_Type)
4601 and then Is_Record_Type (Etype (Prefix_Type))
4602 then
4603 Set_Etype (Prefix (N), Etype (Prefix_Type));
4604 Analyze_Selected_Component (N);
4605 return;
4607 -- Similarly, if this is the actual for a formal derived type, or
4608 -- a derived type thereof, the component inherited from the generic
4609 -- parent may not be visible in the actual, but the selected
4610 -- component is legal. Climb up the derivation chain of the generic
4611 -- parent type until we find the proper ancestor type.
4613 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
4614 declare
4615 Par : Entity_Id := Prefix_Type;
4616 begin
4617 -- Climb up derivation chain to generic actual subtype
4619 while not Is_Generic_Actual_Type (Par) loop
4620 if Ekind (Par) = E_Record_Type then
4621 Par := Parent_Subtype (Par);
4622 exit when No (Par);
4623 else
4624 exit when Par = Etype (Par);
4625 Par := Etype (Par);
4626 end if;
4627 end loop;
4629 if Present (Par) and then Is_Generic_Actual_Type (Par) then
4631 -- Now look for component in ancestor types
4633 Par := Generic_Parent_Type (Declaration_Node (Par));
4634 loop
4635 Find_Component_In_Instance (Par);
4636 exit when Present (Entity (Sel))
4637 or else Par = Etype (Par);
4638 Par := Etype (Par);
4639 end loop;
4641 -- In ASIS mode the generic parent type may be absent. Examine
4642 -- the parent type directly for a component that may have been
4643 -- visible in a parent generic unit.
4645 elsif Is_Derived_Type (Prefix_Type) then
4646 Par := Etype (Prefix_Type);
4647 Find_Component_In_Instance (Par);
4648 end if;
4649 end;
4651 -- The search above must have eventually succeeded, since the
4652 -- selected component was legal in the generic.
4654 if No (Entity (Sel)) then
4655 raise Program_Error;
4656 end if;
4658 return;
4660 -- Component not found, specialize error message when appropriate
4662 else
4663 if Ekind (Prefix_Type) = E_Record_Subtype then
4665 -- Check whether this is a component of the base type which
4666 -- is absent from a statically constrained subtype. This will
4667 -- raise constraint error at run time, but is not a compile-
4668 -- time error. When the selector is illegal for base type as
4669 -- well fall through and generate a compilation error anyway.
4671 Comp := First_Component (Base_Type (Prefix_Type));
4672 while Present (Comp) loop
4673 if Chars (Comp) = Chars (Sel)
4674 and then Is_Visible_Component (Comp)
4675 then
4676 Set_Entity_With_Checks (Sel, Comp);
4677 Generate_Reference (Comp, Sel);
4678 Set_Etype (Sel, Etype (Comp));
4679 Set_Etype (N, Etype (Comp));
4681 -- Emit appropriate message. The node will be replaced
4682 -- by an appropriate raise statement.
4684 -- Note that in SPARK mode, as with all calls to apply a
4685 -- compile time constraint error, this will be made into
4686 -- an error to simplify the processing of the formal
4687 -- verification backend.
4689 Apply_Compile_Time_Constraint_Error
4690 (N, "component not present in }??",
4691 CE_Discriminant_Check_Failed,
4692 Ent => Prefix_Type, Rep => False);
4694 Set_Raises_Constraint_Error (N);
4695 return;
4696 end if;
4698 Next_Component (Comp);
4699 end loop;
4701 end if;
4703 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4704 Error_Msg_NE ("no selector& for}", N, Sel);
4706 -- Add information in the case of an incomplete prefix
4708 if Is_Incomplete_Type (Type_To_Use) then
4709 declare
4710 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
4712 begin
4713 if From_Limited_With (Scope (Type_To_Use)) then
4714 Error_Msg_NE
4715 ("\limited view of& has no components", N, Inc);
4717 else
4718 Error_Msg_NE
4719 ("\premature usage of incomplete type&", N, Inc);
4721 if Nkind (Parent (Inc)) =
4722 N_Incomplete_Type_Declaration
4723 then
4724 -- Record location of premature use in entity so that
4725 -- a continuation message is generated when the
4726 -- completion is seen.
4728 Set_Premature_Use (Parent (Inc), N);
4729 end if;
4730 end if;
4731 end;
4732 end if;
4734 Check_Misspelled_Selector (Type_To_Use, Sel);
4735 end if;
4737 Set_Entity (Sel, Any_Id);
4738 Set_Etype (Sel, Any_Type);
4739 end if;
4740 end Analyze_Selected_Component;
4742 ---------------------------
4743 -- Analyze_Short_Circuit --
4744 ---------------------------
4746 procedure Analyze_Short_Circuit (N : Node_Id) is
4747 L : constant Node_Id := Left_Opnd (N);
4748 R : constant Node_Id := Right_Opnd (N);
4749 Ind : Interp_Index;
4750 It : Interp;
4752 begin
4753 Analyze_Expression (L);
4754 Analyze_Expression (R);
4755 Set_Etype (N, Any_Type);
4757 if not Is_Overloaded (L) then
4758 if Root_Type (Etype (L)) = Standard_Boolean
4759 and then Has_Compatible_Type (R, Etype (L))
4760 then
4761 Add_One_Interp (N, Etype (L), Etype (L));
4762 end if;
4764 else
4765 Get_First_Interp (L, Ind, It);
4766 while Present (It.Typ) loop
4767 if Root_Type (It.Typ) = Standard_Boolean
4768 and then Has_Compatible_Type (R, It.Typ)
4769 then
4770 Add_One_Interp (N, It.Typ, It.Typ);
4771 end if;
4773 Get_Next_Interp (Ind, It);
4774 end loop;
4775 end if;
4777 -- Here we have failed to find an interpretation. Clearly we know that
4778 -- it is not the case that both operands can have an interpretation of
4779 -- Boolean, but this is by far the most likely intended interpretation.
4780 -- So we simply resolve both operands as Booleans, and at least one of
4781 -- these resolutions will generate an error message, and we do not need
4782 -- to give another error message on the short circuit operation itself.
4784 if Etype (N) = Any_Type then
4785 Resolve (L, Standard_Boolean);
4786 Resolve (R, Standard_Boolean);
4787 Set_Etype (N, Standard_Boolean);
4788 end if;
4789 end Analyze_Short_Circuit;
4791 -------------------
4792 -- Analyze_Slice --
4793 -------------------
4795 procedure Analyze_Slice (N : Node_Id) is
4796 D : constant Node_Id := Discrete_Range (N);
4797 P : constant Node_Id := Prefix (N);
4798 Array_Type : Entity_Id;
4799 Index_Type : Entity_Id;
4801 procedure Analyze_Overloaded_Slice;
4802 -- If the prefix is overloaded, select those interpretations that
4803 -- yield a one-dimensional array type.
4805 ------------------------------
4806 -- Analyze_Overloaded_Slice --
4807 ------------------------------
4809 procedure Analyze_Overloaded_Slice is
4810 I : Interp_Index;
4811 It : Interp;
4812 Typ : Entity_Id;
4814 begin
4815 Set_Etype (N, Any_Type);
4817 Get_First_Interp (P, I, It);
4818 while Present (It.Nam) loop
4819 Typ := It.Typ;
4821 if Is_Access_Type (Typ) then
4822 Typ := Designated_Type (Typ);
4823 Error_Msg_NW
4824 (Warn_On_Dereference, "?d?implicit dereference", N);
4825 end if;
4827 if Is_Array_Type (Typ)
4828 and then Number_Dimensions (Typ) = 1
4829 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4830 then
4831 Add_One_Interp (N, Typ, Typ);
4832 end if;
4834 Get_Next_Interp (I, It);
4835 end loop;
4837 if Etype (N) = Any_Type then
4838 Error_Msg_N ("expect array type in prefix of slice", N);
4839 end if;
4840 end Analyze_Overloaded_Slice;
4842 -- Start of processing for Analyze_Slice
4844 begin
4845 if Comes_From_Source (N) then
4846 Check_SPARK_05_Restriction ("slice is not allowed", N);
4847 end if;
4849 Analyze (P);
4850 Analyze (D);
4852 if Is_Overloaded (P) then
4853 Analyze_Overloaded_Slice;
4855 else
4856 Array_Type := Etype (P);
4857 Set_Etype (N, Any_Type);
4859 if Is_Access_Type (Array_Type) then
4860 Array_Type := Designated_Type (Array_Type);
4861 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
4862 end if;
4864 if not Is_Array_Type (Array_Type) then
4865 Wrong_Type (P, Any_Array);
4867 elsif Number_Dimensions (Array_Type) > 1 then
4868 Error_Msg_N
4869 ("type is not one-dimensional array in slice prefix", N);
4871 else
4872 if Ekind (Array_Type) = E_String_Literal_Subtype then
4873 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
4874 else
4875 Index_Type := Etype (First_Index (Array_Type));
4876 end if;
4878 if not Has_Compatible_Type (D, Index_Type) then
4879 Wrong_Type (D, Index_Type);
4880 else
4881 Set_Etype (N, Array_Type);
4882 end if;
4883 end if;
4884 end if;
4885 end Analyze_Slice;
4887 -----------------------------
4888 -- Analyze_Type_Conversion --
4889 -----------------------------
4891 procedure Analyze_Type_Conversion (N : Node_Id) is
4892 Expr : constant Node_Id := Expression (N);
4893 Typ : Entity_Id;
4895 begin
4896 -- If Conversion_OK is set, then the Etype is already set, and the only
4897 -- processing required is to analyze the expression. This is used to
4898 -- construct certain "illegal" conversions which are not allowed by Ada
4899 -- semantics, but can be handled by Gigi, see Sinfo for further details.
4901 if Conversion_OK (N) then
4902 Analyze (Expr);
4903 return;
4904 end if;
4906 -- Otherwise full type analysis is required, as well as some semantic
4907 -- checks to make sure the argument of the conversion is appropriate.
4909 Find_Type (Subtype_Mark (N));
4910 Typ := Entity (Subtype_Mark (N));
4911 Set_Etype (N, Typ);
4912 Check_Fully_Declared (Typ, N);
4913 Analyze_Expression (Expr);
4914 Validate_Remote_Type_Type_Conversion (N);
4916 -- Only remaining step is validity checks on the argument. These
4917 -- are skipped if the conversion does not come from the source.
4919 if not Comes_From_Source (N) then
4920 return;
4922 -- If there was an error in a generic unit, no need to replicate the
4923 -- error message. Conversely, constant-folding in the generic may
4924 -- transform the argument of a conversion into a string literal, which
4925 -- is legal. Therefore the following tests are not performed in an
4926 -- instance. The same applies to an inlined body.
4928 elsif In_Instance or In_Inlined_Body then
4929 return;
4931 elsif Nkind (Expr) = N_Null then
4932 Error_Msg_N ("argument of conversion cannot be null", N);
4933 Error_Msg_N ("\use qualified expression instead", N);
4934 Set_Etype (N, Any_Type);
4936 elsif Nkind (Expr) = N_Aggregate then
4937 Error_Msg_N ("argument of conversion cannot be aggregate", N);
4938 Error_Msg_N ("\use qualified expression instead", N);
4940 elsif Nkind (Expr) = N_Allocator then
4941 Error_Msg_N ("argument of conversion cannot be an allocator", N);
4942 Error_Msg_N ("\use qualified expression instead", N);
4944 elsif Nkind (Expr) = N_String_Literal then
4945 Error_Msg_N ("argument of conversion cannot be string literal", N);
4946 Error_Msg_N ("\use qualified expression instead", N);
4948 elsif Nkind (Expr) = N_Character_Literal then
4949 if Ada_Version = Ada_83 then
4950 Resolve (Expr, Typ);
4951 else
4952 Error_Msg_N ("argument of conversion cannot be character literal",
4954 Error_Msg_N ("\use qualified expression instead", N);
4955 end if;
4957 elsif Nkind (Expr) = N_Attribute_Reference
4958 and then Nam_In (Attribute_Name (Expr), Name_Access,
4959 Name_Unchecked_Access,
4960 Name_Unrestricted_Access)
4961 then
4962 Error_Msg_N ("argument of conversion cannot be access", N);
4963 Error_Msg_N ("\use qualified expression instead", N);
4964 end if;
4966 -- A formal parameter of a specific tagged type whose related subprogram
4967 -- is subject to pragma Extensions_Visible with value "False" cannot
4968 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)).
4970 if Is_Class_Wide_Type (Typ) and then Is_EVF_Expression (Expr) then
4971 Error_Msg_N
4972 ("formal parameter with Extensions_Visible False cannot be "
4973 & "converted to class-wide type", Expr);
4974 end if;
4975 end Analyze_Type_Conversion;
4977 ----------------------
4978 -- Analyze_Unary_Op --
4979 ----------------------
4981 procedure Analyze_Unary_Op (N : Node_Id) is
4982 R : constant Node_Id := Right_Opnd (N);
4983 Op_Id : Entity_Id := Entity (N);
4985 begin
4986 Set_Etype (N, Any_Type);
4987 Candidate_Type := Empty;
4989 Analyze_Expression (R);
4991 if Present (Op_Id) then
4992 if Ekind (Op_Id) = E_Operator then
4993 Find_Unary_Types (R, Op_Id, N);
4994 else
4995 Add_One_Interp (N, Op_Id, Etype (Op_Id));
4996 end if;
4998 else
4999 Op_Id := Get_Name_Entity_Id (Chars (N));
5000 while Present (Op_Id) loop
5001 if Ekind (Op_Id) = E_Operator then
5002 if No (Next_Entity (First_Entity (Op_Id))) then
5003 Find_Unary_Types (R, Op_Id, N);
5004 end if;
5006 elsif Is_Overloadable (Op_Id) then
5007 Analyze_User_Defined_Unary_Op (N, Op_Id);
5008 end if;
5010 Op_Id := Homonym (Op_Id);
5011 end loop;
5012 end if;
5014 Operator_Check (N);
5015 end Analyze_Unary_Op;
5017 ----------------------------------
5018 -- Analyze_Unchecked_Expression --
5019 ----------------------------------
5021 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5022 begin
5023 Analyze (Expression (N), Suppress => All_Checks);
5024 Set_Etype (N, Etype (Expression (N)));
5025 Save_Interps (Expression (N), N);
5026 end Analyze_Unchecked_Expression;
5028 ---------------------------------------
5029 -- Analyze_Unchecked_Type_Conversion --
5030 ---------------------------------------
5032 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5033 begin
5034 Find_Type (Subtype_Mark (N));
5035 Analyze_Expression (Expression (N));
5036 Set_Etype (N, Entity (Subtype_Mark (N)));
5037 end Analyze_Unchecked_Type_Conversion;
5039 ------------------------------------
5040 -- Analyze_User_Defined_Binary_Op --
5041 ------------------------------------
5043 procedure Analyze_User_Defined_Binary_Op
5044 (N : Node_Id;
5045 Op_Id : Entity_Id)
5047 begin
5048 -- Only do analysis if the operator Comes_From_Source, since otherwise
5049 -- the operator was generated by the expander, and all such operators
5050 -- always refer to the operators in package Standard.
5052 if Comes_From_Source (N) then
5053 declare
5054 F1 : constant Entity_Id := First_Formal (Op_Id);
5055 F2 : constant Entity_Id := Next_Formal (F1);
5057 begin
5058 -- Verify that Op_Id is a visible binary function. Note that since
5059 -- we know Op_Id is overloaded, potentially use visible means use
5060 -- visible for sure (RM 9.4(11)).
5062 if Ekind (Op_Id) = E_Function
5063 and then Present (F2)
5064 and then (Is_Immediately_Visible (Op_Id)
5065 or else Is_Potentially_Use_Visible (Op_Id))
5066 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5067 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5068 then
5069 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5071 -- If the left operand is overloaded, indicate that the current
5072 -- type is a viable candidate. This is redundant in most cases,
5073 -- but for equality and comparison operators where the context
5074 -- does not impose a type on the operands, setting the proper
5075 -- type is necessary to avoid subsequent ambiguities during
5076 -- resolution, when both user-defined and predefined operators
5077 -- may be candidates.
5079 if Is_Overloaded (Left_Opnd (N)) then
5080 Set_Etype (Left_Opnd (N), Etype (F1));
5081 end if;
5083 if Debug_Flag_E then
5084 Write_Str ("user defined operator ");
5085 Write_Name (Chars (Op_Id));
5086 Write_Str (" on node ");
5087 Write_Int (Int (N));
5088 Write_Eol;
5089 end if;
5090 end if;
5091 end;
5092 end if;
5093 end Analyze_User_Defined_Binary_Op;
5095 -----------------------------------
5096 -- Analyze_User_Defined_Unary_Op --
5097 -----------------------------------
5099 procedure Analyze_User_Defined_Unary_Op
5100 (N : Node_Id;
5101 Op_Id : Entity_Id)
5103 begin
5104 -- Only do analysis if the operator Comes_From_Source, since otherwise
5105 -- the operator was generated by the expander, and all such operators
5106 -- always refer to the operators in package Standard.
5108 if Comes_From_Source (N) then
5109 declare
5110 F : constant Entity_Id := First_Formal (Op_Id);
5112 begin
5113 -- Verify that Op_Id is a visible unary function. Note that since
5114 -- we know Op_Id is overloaded, potentially use visible means use
5115 -- visible for sure (RM 9.4(11)).
5117 if Ekind (Op_Id) = E_Function
5118 and then No (Next_Formal (F))
5119 and then (Is_Immediately_Visible (Op_Id)
5120 or else Is_Potentially_Use_Visible (Op_Id))
5121 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5122 then
5123 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5124 end if;
5125 end;
5126 end if;
5127 end Analyze_User_Defined_Unary_Op;
5129 ---------------------------
5130 -- Check_Arithmetic_Pair --
5131 ---------------------------
5133 procedure Check_Arithmetic_Pair
5134 (T1, T2 : Entity_Id;
5135 Op_Id : Entity_Id;
5136 N : Node_Id)
5138 Op_Name : constant Name_Id := Chars (Op_Id);
5140 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5141 -- Check whether the fixed-point type Typ has a user-defined operator
5142 -- (multiplication or division) that should hide the corresponding
5143 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5144 -- such operators more visible and therefore useful.
5146 -- If the name of the operation is an expanded name with prefix
5147 -- Standard, the predefined universal fixed operator is available,
5148 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5150 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5151 -- Get specific type (i.e. non-universal type if there is one)
5153 ------------------
5154 -- Has_Fixed_Op --
5155 ------------------
5157 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
5158 Bas : constant Entity_Id := Base_Type (Typ);
5159 Ent : Entity_Id;
5160 F1 : Entity_Id;
5161 F2 : Entity_Id;
5163 begin
5164 -- If the universal_fixed operation is given explicitly the rule
5165 -- concerning primitive operations of the type do not apply.
5167 if Nkind (N) = N_Function_Call
5168 and then Nkind (Name (N)) = N_Expanded_Name
5169 and then Entity (Prefix (Name (N))) = Standard_Standard
5170 then
5171 return False;
5172 end if;
5174 -- The operation is treated as primitive if it is declared in the
5175 -- same scope as the type, and therefore on the same entity chain.
5177 Ent := Next_Entity (Typ);
5178 while Present (Ent) loop
5179 if Chars (Ent) = Chars (Op) then
5180 F1 := First_Formal (Ent);
5181 F2 := Next_Formal (F1);
5183 -- The operation counts as primitive if either operand or
5184 -- result are of the given base type, and both operands are
5185 -- fixed point types.
5187 if (Base_Type (Etype (F1)) = Bas
5188 and then Is_Fixed_Point_Type (Etype (F2)))
5190 or else
5191 (Base_Type (Etype (F2)) = Bas
5192 and then Is_Fixed_Point_Type (Etype (F1)))
5194 or else
5195 (Base_Type (Etype (Ent)) = Bas
5196 and then Is_Fixed_Point_Type (Etype (F1))
5197 and then Is_Fixed_Point_Type (Etype (F2)))
5198 then
5199 return True;
5200 end if;
5201 end if;
5203 Next_Entity (Ent);
5204 end loop;
5206 return False;
5207 end Has_Fixed_Op;
5209 -------------------
5210 -- Specific_Type --
5211 -------------------
5213 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5214 begin
5215 if T1 = Universal_Integer or else T1 = Universal_Real then
5216 return Base_Type (T2);
5217 else
5218 return Base_Type (T1);
5219 end if;
5220 end Specific_Type;
5222 -- Start of processing for Check_Arithmetic_Pair
5224 begin
5225 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
5226 if Is_Numeric_Type (T1)
5227 and then Is_Numeric_Type (T2)
5228 and then (Covers (T1 => T1, T2 => T2)
5229 or else
5230 Covers (T1 => T2, T2 => T1))
5231 then
5232 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5233 end if;
5235 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
5236 if Is_Fixed_Point_Type (T1)
5237 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
5238 then
5239 -- If Treat_Fixed_As_Integer is set then the Etype is already set
5240 -- and no further processing is required (this is the case of an
5241 -- operator constructed by Exp_Fixd for a fixed point operation)
5242 -- Otherwise add one interpretation with universal fixed result
5243 -- If the operator is given in functional notation, it comes
5244 -- from source and Fixed_As_Integer cannot apply.
5246 if (Nkind (N) not in N_Op
5247 or else not Treat_Fixed_As_Integer (N))
5248 and then
5249 (not Has_Fixed_Op (T1, Op_Id)
5250 or else Nkind (Parent (N)) = N_Type_Conversion)
5251 then
5252 Add_One_Interp (N, Op_Id, Universal_Fixed);
5253 end if;
5255 elsif Is_Fixed_Point_Type (T2)
5256 and then (Nkind (N) not in N_Op
5257 or else not Treat_Fixed_As_Integer (N))
5258 and then T1 = Universal_Real
5259 and then
5260 (not Has_Fixed_Op (T1, Op_Id)
5261 or else Nkind (Parent (N)) = N_Type_Conversion)
5262 then
5263 Add_One_Interp (N, Op_Id, Universal_Fixed);
5265 elsif Is_Numeric_Type (T1)
5266 and then Is_Numeric_Type (T2)
5267 and then (Covers (T1 => T1, T2 => T2)
5268 or else
5269 Covers (T1 => T2, T2 => T1))
5270 then
5271 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5273 elsif Is_Fixed_Point_Type (T1)
5274 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5275 or else T2 = Universal_Integer)
5276 then
5277 Add_One_Interp (N, Op_Id, T1);
5279 elsif T2 = Universal_Real
5280 and then Base_Type (T1) = Base_Type (Standard_Integer)
5281 and then Op_Name = Name_Op_Multiply
5282 then
5283 Add_One_Interp (N, Op_Id, Any_Fixed);
5285 elsif T1 = Universal_Real
5286 and then Base_Type (T2) = Base_Type (Standard_Integer)
5287 then
5288 Add_One_Interp (N, Op_Id, Any_Fixed);
5290 elsif Is_Fixed_Point_Type (T2)
5291 and then (Base_Type (T1) = Base_Type (Standard_Integer)
5292 or else T1 = Universal_Integer)
5293 and then Op_Name = Name_Op_Multiply
5294 then
5295 Add_One_Interp (N, Op_Id, T2);
5297 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5298 Add_One_Interp (N, Op_Id, T1);
5300 elsif T2 = Universal_Real
5301 and then T1 = Universal_Integer
5302 and then Op_Name = Name_Op_Multiply
5303 then
5304 Add_One_Interp (N, Op_Id, T2);
5305 end if;
5307 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5309 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
5310 -- set does not require any special processing, since the Etype is
5311 -- already set (case of operation constructed by Exp_Fixed).
5313 if Is_Integer_Type (T1)
5314 and then (Covers (T1 => T1, T2 => T2)
5315 or else
5316 Covers (T1 => T2, T2 => T1))
5317 then
5318 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5319 end if;
5321 elsif Op_Name = Name_Op_Expon then
5322 if Is_Numeric_Type (T1)
5323 and then not Is_Fixed_Point_Type (T1)
5324 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5325 or else T2 = Universal_Integer)
5326 then
5327 Add_One_Interp (N, Op_Id, Base_Type (T1));
5328 end if;
5330 else pragma Assert (Nkind (N) in N_Op_Shift);
5332 -- If not one of the predefined operators, the node may be one
5333 -- of the intrinsic functions. Its kind is always specific, and
5334 -- we can use it directly, rather than the name of the operation.
5336 if Is_Integer_Type (T1)
5337 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5338 or else T2 = Universal_Integer)
5339 then
5340 Add_One_Interp (N, Op_Id, Base_Type (T1));
5341 end if;
5342 end if;
5343 end Check_Arithmetic_Pair;
5345 -------------------------------
5346 -- Check_Misspelled_Selector --
5347 -------------------------------
5349 procedure Check_Misspelled_Selector
5350 (Prefix : Entity_Id;
5351 Sel : Node_Id)
5353 Max_Suggestions : constant := 2;
5354 Nr_Of_Suggestions : Natural := 0;
5356 Suggestion_1 : Entity_Id := Empty;
5357 Suggestion_2 : Entity_Id := Empty;
5359 Comp : Entity_Id;
5361 begin
5362 -- All the components of the prefix of selector Sel are matched against
5363 -- Sel and a count is maintained of possible misspellings. When at
5364 -- the end of the analysis there are one or two (not more) possible
5365 -- misspellings, these misspellings will be suggested as possible
5366 -- correction.
5368 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5370 -- Concurrent types should be handled as well ???
5372 return;
5373 end if;
5375 Comp := First_Entity (Prefix);
5376 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5377 if Is_Visible_Component (Comp) then
5378 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5379 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5381 case Nr_Of_Suggestions is
5382 when 1 => Suggestion_1 := Comp;
5383 when 2 => Suggestion_2 := Comp;
5384 when others => exit;
5385 end case;
5386 end if;
5387 end if;
5389 Comp := Next_Entity (Comp);
5390 end loop;
5392 -- Report at most two suggestions
5394 if Nr_Of_Suggestions = 1 then
5395 Error_Msg_NE -- CODEFIX
5396 ("\possible misspelling of&", Sel, Suggestion_1);
5398 elsif Nr_Of_Suggestions = 2 then
5399 Error_Msg_Node_2 := Suggestion_2;
5400 Error_Msg_NE -- CODEFIX
5401 ("\possible misspelling of& or&", Sel, Suggestion_1);
5402 end if;
5403 end Check_Misspelled_Selector;
5405 ----------------------
5406 -- Defined_In_Scope --
5407 ----------------------
5409 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5411 S1 : constant Entity_Id := Scope (Base_Type (T));
5412 begin
5413 return S1 = S
5414 or else (S1 = System_Aux_Id and then S = Scope (S1));
5415 end Defined_In_Scope;
5417 -------------------
5418 -- Diagnose_Call --
5419 -------------------
5421 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5422 Actual : Node_Id;
5423 X : Interp_Index;
5424 It : Interp;
5425 Err_Mode : Boolean;
5426 New_Nam : Node_Id;
5427 Void_Interp_Seen : Boolean := False;
5429 Success : Boolean;
5430 pragma Warnings (Off, Boolean);
5432 begin
5433 if Ada_Version >= Ada_2005 then
5434 Actual := First_Actual (N);
5435 while Present (Actual) loop
5437 -- Ada 2005 (AI-50217): Post an error in case of premature
5438 -- usage of an entity from the limited view.
5440 if not Analyzed (Etype (Actual))
5441 and then From_Limited_With (Etype (Actual))
5442 then
5443 Error_Msg_Qual_Level := 1;
5444 Error_Msg_NE
5445 ("missing with_clause for scope of imported type&",
5446 Actual, Etype (Actual));
5447 Error_Msg_Qual_Level := 0;
5448 end if;
5450 Next_Actual (Actual);
5451 end loop;
5452 end if;
5454 -- Analyze each candidate call again, with full error reporting
5455 -- for each.
5457 Error_Msg_N
5458 ("no candidate interpretations match the actuals:!", Nam);
5459 Err_Mode := All_Errors_Mode;
5460 All_Errors_Mode := True;
5462 -- If this is a call to an operation of a concurrent type,
5463 -- the failed interpretations have been removed from the
5464 -- name. Recover them to provide full diagnostics.
5466 if Nkind (Parent (Nam)) = N_Selected_Component then
5467 Set_Entity (Nam, Empty);
5468 New_Nam := New_Copy_Tree (Parent (Nam));
5469 Set_Is_Overloaded (New_Nam, False);
5470 Set_Is_Overloaded (Selector_Name (New_Nam), False);
5471 Set_Parent (New_Nam, Parent (Parent (Nam)));
5472 Analyze_Selected_Component (New_Nam);
5473 Get_First_Interp (Selector_Name (New_Nam), X, It);
5474 else
5475 Get_First_Interp (Nam, X, It);
5476 end if;
5478 while Present (It.Nam) loop
5479 if Etype (It.Nam) = Standard_Void_Type then
5480 Void_Interp_Seen := True;
5481 end if;
5483 Analyze_One_Call (N, It.Nam, True, Success);
5484 Get_Next_Interp (X, It);
5485 end loop;
5487 if Nkind (N) = N_Function_Call then
5488 Get_First_Interp (Nam, X, It);
5489 while Present (It.Nam) loop
5490 if Ekind_In (It.Nam, E_Function, E_Operator) then
5491 return;
5492 else
5493 Get_Next_Interp (X, It);
5494 end if;
5495 end loop;
5497 -- If all interpretations are procedures, this deserves a
5498 -- more precise message. Ditto if this appears as the prefix
5499 -- of a selected component, which may be a lexical error.
5501 Error_Msg_N
5502 ("\context requires function call, found procedure name", Nam);
5504 if Nkind (Parent (N)) = N_Selected_Component
5505 and then N = Prefix (Parent (N))
5506 then
5507 Error_Msg_N -- CODEFIX
5508 ("\period should probably be semicolon", Parent (N));
5509 end if;
5511 elsif Nkind (N) = N_Procedure_Call_Statement
5512 and then not Void_Interp_Seen
5513 then
5514 Error_Msg_N (
5515 "\function name found in procedure call", Nam);
5516 end if;
5518 All_Errors_Mode := Err_Mode;
5519 end Diagnose_Call;
5521 ---------------------------
5522 -- Find_Arithmetic_Types --
5523 ---------------------------
5525 procedure Find_Arithmetic_Types
5526 (L, R : Node_Id;
5527 Op_Id : Entity_Id;
5528 N : Node_Id)
5530 Index1 : Interp_Index;
5531 Index2 : Interp_Index;
5532 It1 : Interp;
5533 It2 : Interp;
5535 procedure Check_Right_Argument (T : Entity_Id);
5536 -- Check right operand of operator
5538 --------------------------
5539 -- Check_Right_Argument --
5540 --------------------------
5542 procedure Check_Right_Argument (T : Entity_Id) is
5543 begin
5544 if not Is_Overloaded (R) then
5545 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
5546 else
5547 Get_First_Interp (R, Index2, It2);
5548 while Present (It2.Typ) loop
5549 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5550 Get_Next_Interp (Index2, It2);
5551 end loop;
5552 end if;
5553 end Check_Right_Argument;
5555 -- Start of processing for Find_Arithmetic_Types
5557 begin
5558 if not Is_Overloaded (L) then
5559 Check_Right_Argument (Etype (L));
5561 else
5562 Get_First_Interp (L, Index1, It1);
5563 while Present (It1.Typ) loop
5564 Check_Right_Argument (It1.Typ);
5565 Get_Next_Interp (Index1, It1);
5566 end loop;
5567 end if;
5569 end Find_Arithmetic_Types;
5571 ------------------------
5572 -- Find_Boolean_Types --
5573 ------------------------
5575 procedure Find_Boolean_Types
5576 (L, R : Node_Id;
5577 Op_Id : Entity_Id;
5578 N : Node_Id)
5580 Index : Interp_Index;
5581 It : Interp;
5583 procedure Check_Numeric_Argument (T : Entity_Id);
5584 -- Special case for logical operations one of whose operands is an
5585 -- integer literal. If both are literal the result is any modular type.
5587 ----------------------------
5588 -- Check_Numeric_Argument --
5589 ----------------------------
5591 procedure Check_Numeric_Argument (T : Entity_Id) is
5592 begin
5593 if T = Universal_Integer then
5594 Add_One_Interp (N, Op_Id, Any_Modular);
5596 elsif Is_Modular_Integer_Type (T) then
5597 Add_One_Interp (N, Op_Id, T);
5598 end if;
5599 end Check_Numeric_Argument;
5601 -- Start of processing for Find_Boolean_Types
5603 begin
5604 if not Is_Overloaded (L) then
5605 if Etype (L) = Universal_Integer
5606 or else Etype (L) = Any_Modular
5607 then
5608 if not Is_Overloaded (R) then
5609 Check_Numeric_Argument (Etype (R));
5611 else
5612 Get_First_Interp (R, Index, It);
5613 while Present (It.Typ) loop
5614 Check_Numeric_Argument (It.Typ);
5615 Get_Next_Interp (Index, It);
5616 end loop;
5617 end if;
5619 -- If operands are aggregates, we must assume that they may be
5620 -- boolean arrays, and leave disambiguation for the second pass.
5621 -- If only one is an aggregate, verify that the other one has an
5622 -- interpretation as a boolean array
5624 elsif Nkind (L) = N_Aggregate then
5625 if Nkind (R) = N_Aggregate then
5626 Add_One_Interp (N, Op_Id, Etype (L));
5628 elsif not Is_Overloaded (R) then
5629 if Valid_Boolean_Arg (Etype (R)) then
5630 Add_One_Interp (N, Op_Id, Etype (R));
5631 end if;
5633 else
5634 Get_First_Interp (R, Index, It);
5635 while Present (It.Typ) loop
5636 if Valid_Boolean_Arg (It.Typ) then
5637 Add_One_Interp (N, Op_Id, It.Typ);
5638 end if;
5640 Get_Next_Interp (Index, It);
5641 end loop;
5642 end if;
5644 elsif Valid_Boolean_Arg (Etype (L))
5645 and then Has_Compatible_Type (R, Etype (L))
5646 then
5647 Add_One_Interp (N, Op_Id, Etype (L));
5648 end if;
5650 else
5651 Get_First_Interp (L, Index, It);
5652 while Present (It.Typ) loop
5653 if Valid_Boolean_Arg (It.Typ)
5654 and then Has_Compatible_Type (R, It.Typ)
5655 then
5656 Add_One_Interp (N, Op_Id, It.Typ);
5657 end if;
5659 Get_Next_Interp (Index, It);
5660 end loop;
5661 end if;
5662 end Find_Boolean_Types;
5664 ---------------------------
5665 -- Find_Comparison_Types --
5666 ---------------------------
5668 procedure Find_Comparison_Types
5669 (L, R : Node_Id;
5670 Op_Id : Entity_Id;
5671 N : Node_Id)
5673 Index : Interp_Index;
5674 It : Interp;
5675 Found : Boolean := False;
5676 I_F : Interp_Index;
5677 T_F : Entity_Id;
5678 Scop : Entity_Id := Empty;
5680 procedure Try_One_Interp (T1 : Entity_Id);
5681 -- Routine to try one proposed interpretation. Note that the context
5682 -- of the operator plays no role in resolving the arguments, so that
5683 -- if there is more than one interpretation of the operands that is
5684 -- compatible with comparison, the operation is ambiguous.
5686 --------------------
5687 -- Try_One_Interp --
5688 --------------------
5690 procedure Try_One_Interp (T1 : Entity_Id) is
5691 begin
5693 -- If the operator is an expanded name, then the type of the operand
5694 -- must be defined in the corresponding scope. If the type is
5695 -- universal, the context will impose the correct type.
5697 if Present (Scop)
5698 and then not Defined_In_Scope (T1, Scop)
5699 and then T1 /= Universal_Integer
5700 and then T1 /= Universal_Real
5701 and then T1 /= Any_String
5702 and then T1 /= Any_Composite
5703 then
5704 return;
5705 end if;
5707 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
5708 if Found and then Base_Type (T1) /= Base_Type (T_F) then
5709 It := Disambiguate (L, I_F, Index, Any_Type);
5711 if It = No_Interp then
5712 Ambiguous_Operands (N);
5713 Set_Etype (L, Any_Type);
5714 return;
5716 else
5717 T_F := It.Typ;
5718 end if;
5720 else
5721 Found := True;
5722 T_F := T1;
5723 I_F := Index;
5724 end if;
5726 Set_Etype (L, T_F);
5727 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5729 end if;
5730 end Try_One_Interp;
5732 -- Start of processing for Find_Comparison_Types
5734 begin
5735 -- If left operand is aggregate, the right operand has to
5736 -- provide a usable type for it.
5738 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
5739 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5740 return;
5741 end if;
5743 if Nkind (N) = N_Function_Call
5744 and then Nkind (Name (N)) = N_Expanded_Name
5745 then
5746 Scop := Entity (Prefix (Name (N)));
5748 -- The prefix may be a package renaming, and the subsequent test
5749 -- requires the original package.
5751 if Ekind (Scop) = E_Package
5752 and then Present (Renamed_Entity (Scop))
5753 then
5754 Scop := Renamed_Entity (Scop);
5755 Set_Entity (Prefix (Name (N)), Scop);
5756 end if;
5757 end if;
5759 if not Is_Overloaded (L) then
5760 Try_One_Interp (Etype (L));
5762 else
5763 Get_First_Interp (L, Index, It);
5764 while Present (It.Typ) loop
5765 Try_One_Interp (It.Typ);
5766 Get_Next_Interp (Index, It);
5767 end loop;
5768 end if;
5769 end Find_Comparison_Types;
5771 ----------------------------------------
5772 -- Find_Non_Universal_Interpretations --
5773 ----------------------------------------
5775 procedure Find_Non_Universal_Interpretations
5776 (N : Node_Id;
5777 R : Node_Id;
5778 Op_Id : Entity_Id;
5779 T1 : Entity_Id)
5781 Index : Interp_Index;
5782 It : Interp;
5784 begin
5785 if T1 = Universal_Integer or else T1 = Universal_Real
5787 -- If the left operand of an equality operator is null, the visibility
5788 -- of the operator must be determined from the interpretation of the
5789 -- right operand. This processing must be done for Any_Access, which
5790 -- is the internal representation of the type of the literal null.
5792 or else T1 = Any_Access
5793 then
5794 if not Is_Overloaded (R) then
5795 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5796 else
5797 Get_First_Interp (R, Index, It);
5798 while Present (It.Typ) loop
5799 if Covers (It.Typ, T1) then
5800 Add_One_Interp
5801 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5802 end if;
5804 Get_Next_Interp (Index, It);
5805 end loop;
5806 end if;
5807 else
5808 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5809 end if;
5810 end Find_Non_Universal_Interpretations;
5812 ------------------------------
5813 -- Find_Concatenation_Types --
5814 ------------------------------
5816 procedure Find_Concatenation_Types
5817 (L, R : Node_Id;
5818 Op_Id : Entity_Id;
5819 N : Node_Id)
5821 Op_Type : constant Entity_Id := Etype (Op_Id);
5823 begin
5824 if Is_Array_Type (Op_Type)
5825 and then not Is_Limited_Type (Op_Type)
5827 and then (Has_Compatible_Type (L, Op_Type)
5828 or else
5829 Has_Compatible_Type (L, Component_Type (Op_Type)))
5831 and then (Has_Compatible_Type (R, Op_Type)
5832 or else
5833 Has_Compatible_Type (R, Component_Type (Op_Type)))
5834 then
5835 Add_One_Interp (N, Op_Id, Op_Type);
5836 end if;
5837 end Find_Concatenation_Types;
5839 -------------------------
5840 -- Find_Equality_Types --
5841 -------------------------
5843 procedure Find_Equality_Types
5844 (L, R : Node_Id;
5845 Op_Id : Entity_Id;
5846 N : Node_Id)
5848 Index : Interp_Index;
5849 It : Interp;
5850 Found : Boolean := False;
5851 I_F : Interp_Index;
5852 T_F : Entity_Id;
5853 Scop : Entity_Id := Empty;
5855 procedure Try_One_Interp (T1 : Entity_Id);
5856 -- The context of the equality operator plays no role in resolving the
5857 -- arguments, so that if there is more than one interpretation of the
5858 -- operands that is compatible with equality, the construct is ambiguous
5859 -- and an error can be emitted now, after trying to disambiguate, i.e.
5860 -- applying preference rules.
5862 --------------------
5863 -- Try_One_Interp --
5864 --------------------
5866 procedure Try_One_Interp (T1 : Entity_Id) is
5867 Bas : constant Entity_Id := Base_Type (T1);
5869 begin
5870 -- If the operator is an expanded name, then the type of the operand
5871 -- must be defined in the corresponding scope. If the type is
5872 -- universal, the context will impose the correct type. An anonymous
5873 -- type for a 'Access reference is also universal in this sense, as
5874 -- the actual type is obtained from context.
5876 -- In Ada 2005, the equality operator for anonymous access types
5877 -- is declared in Standard, and preference rules apply to it.
5879 if Present (Scop) then
5880 if Defined_In_Scope (T1, Scop)
5881 or else T1 = Universal_Integer
5882 or else T1 = Universal_Real
5883 or else T1 = Any_Access
5884 or else T1 = Any_String
5885 or else T1 = Any_Composite
5886 or else (Ekind (T1) = E_Access_Subprogram_Type
5887 and then not Comes_From_Source (T1))
5888 then
5889 null;
5891 elsif Ekind (T1) = E_Anonymous_Access_Type
5892 and then Scop = Standard_Standard
5893 then
5894 null;
5896 else
5897 -- The scope does not contain an operator for the type
5899 return;
5900 end if;
5902 -- If we have infix notation, the operator must be usable. Within
5903 -- an instance, if the type is already established we know it is
5904 -- correct. If an operand is universal it is compatible with any
5905 -- numeric type.
5907 elsif In_Open_Scopes (Scope (Bas))
5908 or else Is_Potentially_Use_Visible (Bas)
5909 or else In_Use (Bas)
5910 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
5912 -- In an instance, the type may have been immediately visible.
5913 -- Either the types are compatible, or one operand is universal
5914 -- (numeric or null).
5916 or else (In_Instance
5917 and then
5918 (First_Subtype (T1) = First_Subtype (Etype (R))
5919 or else Nkind (R) = N_Null
5920 or else
5921 (Is_Numeric_Type (T1)
5922 and then Is_Universal_Numeric_Type (Etype (R)))))
5924 -- In Ada 2005, the equality on anonymous access types is declared
5925 -- in Standard, and is always visible.
5927 or else Ekind (T1) = E_Anonymous_Access_Type
5928 then
5929 null;
5931 else
5932 -- Save candidate type for subsequent error message, if any
5934 if not Is_Limited_Type (T1) then
5935 Candidate_Type := T1;
5936 end if;
5938 return;
5939 end if;
5941 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5942 -- Do not allow anonymous access types in equality operators.
5944 if Ada_Version < Ada_2005
5945 and then Ekind (T1) = E_Anonymous_Access_Type
5946 then
5947 return;
5948 end if;
5950 -- If the right operand has a type compatible with T1, check for an
5951 -- acceptable interpretation, unless T1 is limited (no predefined
5952 -- equality available), or this is use of a "/=" for a tagged type.
5953 -- In the latter case, possible interpretations of equality need
5954 -- to be considered, we don't want the default inequality declared
5955 -- in Standard to be chosen, and the "/=" will be rewritten as a
5956 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
5957 -- that that rewriting happens during analysis rather than being
5958 -- delayed until expansion (this is needed for ASIS, which only sees
5959 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
5960 -- is Name_Op_Eq then we still proceed with the interpretation,
5961 -- because that indicates the potential rewriting case where the
5962 -- interpretation to consider is actually "=" and the node may be
5963 -- about to be rewritten by Analyze_Equality_Op.
5965 if T1 /= Standard_Void_Type
5966 and then Has_Compatible_Type (R, T1)
5968 and then
5969 ((not Is_Limited_Type (T1)
5970 and then not Is_Limited_Composite (T1))
5972 or else
5973 (Is_Array_Type (T1)
5974 and then not Is_Limited_Type (Component_Type (T1))
5975 and then Available_Full_View_Of_Component (T1)))
5977 and then
5978 (Nkind (N) /= N_Op_Ne
5979 or else not Is_Tagged_Type (T1)
5980 or else Chars (Op_Id) = Name_Op_Eq)
5981 then
5982 if Found
5983 and then Base_Type (T1) /= Base_Type (T_F)
5984 then
5985 It := Disambiguate (L, I_F, Index, Any_Type);
5987 if It = No_Interp then
5988 Ambiguous_Operands (N);
5989 Set_Etype (L, Any_Type);
5990 return;
5992 else
5993 T_F := It.Typ;
5994 end if;
5996 else
5997 Found := True;
5998 T_F := T1;
5999 I_F := Index;
6000 end if;
6002 if not Analyzed (L) then
6003 Set_Etype (L, T_F);
6004 end if;
6006 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6008 -- Case of operator was not visible, Etype still set to Any_Type
6010 if Etype (N) = Any_Type then
6011 Found := False;
6012 end if;
6014 elsif Scop = Standard_Standard
6015 and then Ekind (T1) = E_Anonymous_Access_Type
6016 then
6017 Found := True;
6018 end if;
6019 end Try_One_Interp;
6021 -- Start of processing for Find_Equality_Types
6023 begin
6024 -- If left operand is aggregate, the right operand has to
6025 -- provide a usable type for it.
6027 if Nkind (L) = N_Aggregate
6028 and then Nkind (R) /= N_Aggregate
6029 then
6030 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
6031 return;
6032 end if;
6034 if Nkind (N) = N_Function_Call
6035 and then Nkind (Name (N)) = N_Expanded_Name
6036 then
6037 Scop := Entity (Prefix (Name (N)));
6039 -- The prefix may be a package renaming, and the subsequent test
6040 -- requires the original package.
6042 if Ekind (Scop) = E_Package
6043 and then Present (Renamed_Entity (Scop))
6044 then
6045 Scop := Renamed_Entity (Scop);
6046 Set_Entity (Prefix (Name (N)), Scop);
6047 end if;
6048 end if;
6050 if not Is_Overloaded (L) then
6051 Try_One_Interp (Etype (L));
6053 else
6054 Get_First_Interp (L, Index, It);
6055 while Present (It.Typ) loop
6056 Try_One_Interp (It.Typ);
6057 Get_Next_Interp (Index, It);
6058 end loop;
6059 end if;
6060 end Find_Equality_Types;
6062 -------------------------
6063 -- Find_Negation_Types --
6064 -------------------------
6066 procedure Find_Negation_Types
6067 (R : Node_Id;
6068 Op_Id : Entity_Id;
6069 N : Node_Id)
6071 Index : Interp_Index;
6072 It : Interp;
6074 begin
6075 if not Is_Overloaded (R) then
6076 if Etype (R) = Universal_Integer then
6077 Add_One_Interp (N, Op_Id, Any_Modular);
6078 elsif Valid_Boolean_Arg (Etype (R)) then
6079 Add_One_Interp (N, Op_Id, Etype (R));
6080 end if;
6082 else
6083 Get_First_Interp (R, Index, It);
6084 while Present (It.Typ) loop
6085 if Valid_Boolean_Arg (It.Typ) then
6086 Add_One_Interp (N, Op_Id, It.Typ);
6087 end if;
6089 Get_Next_Interp (Index, It);
6090 end loop;
6091 end if;
6092 end Find_Negation_Types;
6094 ------------------------------
6095 -- Find_Primitive_Operation --
6096 ------------------------------
6098 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6099 Obj : constant Node_Id := Prefix (N);
6100 Op : constant Node_Id := Selector_Name (N);
6102 Prim : Elmt_Id;
6103 Prims : Elist_Id;
6104 Typ : Entity_Id;
6106 begin
6107 Set_Etype (Op, Any_Type);
6109 if Is_Access_Type (Etype (Obj)) then
6110 Typ := Designated_Type (Etype (Obj));
6111 else
6112 Typ := Etype (Obj);
6113 end if;
6115 if Is_Class_Wide_Type (Typ) then
6116 Typ := Root_Type (Typ);
6117 end if;
6119 Prims := Primitive_Operations (Typ);
6121 Prim := First_Elmt (Prims);
6122 while Present (Prim) loop
6123 if Chars (Node (Prim)) = Chars (Op) then
6124 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6125 Set_Etype (N, Etype (Node (Prim)));
6126 end if;
6128 Next_Elmt (Prim);
6129 end loop;
6131 -- Now look for class-wide operations of the type or any of its
6132 -- ancestors by iterating over the homonyms of the selector.
6134 declare
6135 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6136 Hom : Entity_Id;
6138 begin
6139 Hom := Current_Entity (Op);
6140 while Present (Hom) loop
6141 if (Ekind (Hom) = E_Procedure
6142 or else
6143 Ekind (Hom) = E_Function)
6144 and then Scope (Hom) = Scope (Typ)
6145 and then Present (First_Formal (Hom))
6146 and then
6147 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6148 or else
6149 (Is_Access_Type (Etype (First_Formal (Hom)))
6150 and then
6151 Ekind (Etype (First_Formal (Hom))) =
6152 E_Anonymous_Access_Type
6153 and then
6154 Base_Type
6155 (Designated_Type (Etype (First_Formal (Hom)))) =
6156 Cls_Type))
6157 then
6158 Add_One_Interp (Op, Hom, Etype (Hom));
6159 Set_Etype (N, Etype (Hom));
6160 end if;
6162 Hom := Homonym (Hom);
6163 end loop;
6164 end;
6166 return Etype (Op) /= Any_Type;
6167 end Find_Primitive_Operation;
6169 ----------------------
6170 -- Find_Unary_Types --
6171 ----------------------
6173 procedure Find_Unary_Types
6174 (R : Node_Id;
6175 Op_Id : Entity_Id;
6176 N : Node_Id)
6178 Index : Interp_Index;
6179 It : Interp;
6181 begin
6182 if not Is_Overloaded (R) then
6183 if Is_Numeric_Type (Etype (R)) then
6185 -- In an instance a generic actual may be a numeric type even if
6186 -- the formal in the generic unit was not. In that case, the
6187 -- predefined operator was not a possible interpretation in the
6188 -- generic, and cannot be one in the instance, unless the operator
6189 -- is an actual of an instance.
6191 if In_Instance
6192 and then
6193 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6194 then
6195 null;
6196 else
6197 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6198 end if;
6199 end if;
6201 else
6202 Get_First_Interp (R, Index, It);
6203 while Present (It.Typ) loop
6204 if Is_Numeric_Type (It.Typ) then
6205 if In_Instance
6206 and then
6207 not Is_Numeric_Type
6208 (Corresponding_Generic_Type (Etype (It.Typ)))
6209 then
6210 null;
6212 else
6213 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6214 end if;
6215 end if;
6217 Get_Next_Interp (Index, It);
6218 end loop;
6219 end if;
6220 end Find_Unary_Types;
6222 ------------------
6223 -- Junk_Operand --
6224 ------------------
6226 function Junk_Operand (N : Node_Id) return Boolean is
6227 Enode : Node_Id;
6229 begin
6230 if Error_Posted (N) then
6231 return False;
6232 end if;
6234 -- Get entity to be tested
6236 if Is_Entity_Name (N)
6237 and then Present (Entity (N))
6238 then
6239 Enode := N;
6241 -- An odd case, a procedure name gets converted to a very peculiar
6242 -- function call, and here is where we detect this happening.
6244 elsif Nkind (N) = N_Function_Call
6245 and then Is_Entity_Name (Name (N))
6246 and then Present (Entity (Name (N)))
6247 then
6248 Enode := Name (N);
6250 -- Another odd case, there are at least some cases of selected
6251 -- components where the selected component is not marked as having
6252 -- an entity, even though the selector does have an entity
6254 elsif Nkind (N) = N_Selected_Component
6255 and then Present (Entity (Selector_Name (N)))
6256 then
6257 Enode := Selector_Name (N);
6259 else
6260 return False;
6261 end if;
6263 -- Now test the entity we got to see if it is a bad case
6265 case Ekind (Entity (Enode)) is
6267 when E_Package =>
6268 Error_Msg_N
6269 ("package name cannot be used as operand", Enode);
6271 when Generic_Unit_Kind =>
6272 Error_Msg_N
6273 ("generic unit name cannot be used as operand", Enode);
6275 when Type_Kind =>
6276 Error_Msg_N
6277 ("subtype name cannot be used as operand", Enode);
6279 when Entry_Kind =>
6280 Error_Msg_N
6281 ("entry name cannot be used as operand", Enode);
6283 when E_Procedure =>
6284 Error_Msg_N
6285 ("procedure name cannot be used as operand", Enode);
6287 when E_Exception =>
6288 Error_Msg_N
6289 ("exception name cannot be used as operand", Enode);
6291 when E_Block | E_Label | E_Loop =>
6292 Error_Msg_N
6293 ("label name cannot be used as operand", Enode);
6295 when others =>
6296 return False;
6298 end case;
6300 return True;
6301 end Junk_Operand;
6303 --------------------
6304 -- Operator_Check --
6305 --------------------
6307 procedure Operator_Check (N : Node_Id) is
6308 begin
6309 Remove_Abstract_Operations (N);
6311 -- Test for case of no interpretation found for operator
6313 if Etype (N) = Any_Type then
6314 declare
6315 L : Node_Id;
6316 R : Node_Id;
6317 Op_Id : Entity_Id := Empty;
6319 begin
6320 R := Right_Opnd (N);
6322 if Nkind (N) in N_Binary_Op then
6323 L := Left_Opnd (N);
6324 else
6325 L := Empty;
6326 end if;
6328 -- If either operand has no type, then don't complain further,
6329 -- since this simply means that we have a propagated error.
6331 if R = Error
6332 or else Etype (R) = Any_Type
6333 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
6334 then
6335 -- For the rather unusual case where one of the operands is
6336 -- a Raise_Expression, whose initial type is Any_Type, use
6337 -- the type of the other operand.
6339 if Nkind (L) = N_Raise_Expression then
6340 Set_Etype (L, Etype (R));
6341 Set_Etype (N, Etype (R));
6343 elsif Nkind (R) = N_Raise_Expression then
6344 Set_Etype (R, Etype (L));
6345 Set_Etype (N, Etype (L));
6346 end if;
6348 return;
6350 -- We explicitly check for the case of concatenation of component
6351 -- with component to avoid reporting spurious matching array types
6352 -- that might happen to be lurking in distant packages (such as
6353 -- run-time packages). This also prevents inconsistencies in the
6354 -- messages for certain ACVC B tests, which can vary depending on
6355 -- types declared in run-time interfaces. Another improvement when
6356 -- aggregates are present is to look for a well-typed operand.
6358 elsif Present (Candidate_Type)
6359 and then (Nkind (N) /= N_Op_Concat
6360 or else Is_Array_Type (Etype (L))
6361 or else Is_Array_Type (Etype (R)))
6362 then
6363 if Nkind (N) = N_Op_Concat then
6364 if Etype (L) /= Any_Composite
6365 and then Is_Array_Type (Etype (L))
6366 then
6367 Candidate_Type := Etype (L);
6369 elsif Etype (R) /= Any_Composite
6370 and then Is_Array_Type (Etype (R))
6371 then
6372 Candidate_Type := Etype (R);
6373 end if;
6374 end if;
6376 Error_Msg_NE -- CODEFIX
6377 ("operator for} is not directly visible!",
6378 N, First_Subtype (Candidate_Type));
6380 declare
6381 U : constant Node_Id :=
6382 Cunit (Get_Source_Unit (Candidate_Type));
6383 begin
6384 if Unit_Is_Visible (U) then
6385 Error_Msg_N -- CODEFIX
6386 ("use clause would make operation legal!", N);
6387 else
6388 Error_Msg_NE -- CODEFIX
6389 ("add with_clause and use_clause for&!",
6390 N, Defining_Entity (Unit (U)));
6391 end if;
6392 end;
6393 return;
6395 -- If either operand is a junk operand (e.g. package name), then
6396 -- post appropriate error messages, but do not complain further.
6398 -- Note that the use of OR in this test instead of OR ELSE is
6399 -- quite deliberate, we may as well check both operands in the
6400 -- binary operator case.
6402 elsif Junk_Operand (R)
6403 or -- really mean OR here and not OR ELSE, see above
6404 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
6405 then
6406 return;
6408 -- If we have a logical operator, one of whose operands is
6409 -- Boolean, then we know that the other operand cannot resolve to
6410 -- Boolean (since we got no interpretations), but in that case we
6411 -- pretty much know that the other operand should be Boolean, so
6412 -- resolve it that way (generating an error)
6414 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
6415 if Etype (L) = Standard_Boolean then
6416 Resolve (R, Standard_Boolean);
6417 return;
6418 elsif Etype (R) = Standard_Boolean then
6419 Resolve (L, Standard_Boolean);
6420 return;
6421 end if;
6423 -- For an arithmetic operator or comparison operator, if one
6424 -- of the operands is numeric, then we know the other operand
6425 -- is not the same numeric type. If it is a non-numeric type,
6426 -- then probably it is intended to match the other operand.
6428 elsif Nkind_In (N, N_Op_Add,
6429 N_Op_Divide,
6430 N_Op_Ge,
6431 N_Op_Gt,
6432 N_Op_Le)
6433 or else
6434 Nkind_In (N, N_Op_Lt,
6435 N_Op_Mod,
6436 N_Op_Multiply,
6437 N_Op_Rem,
6438 N_Op_Subtract)
6439 then
6440 -- If Allow_Integer_Address is active, check whether the
6441 -- operation becomes legal after converting an operand.
6443 if Is_Numeric_Type (Etype (L))
6444 and then not Is_Numeric_Type (Etype (R))
6445 then
6446 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6447 Rewrite (R,
6448 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6450 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6451 Analyze_Comparison_Op (N);
6452 else
6453 Analyze_Arithmetic_Op (N);
6454 end if;
6455 else
6456 Resolve (R, Etype (L));
6457 end if;
6459 return;
6461 elsif Is_Numeric_Type (Etype (R))
6462 and then not Is_Numeric_Type (Etype (L))
6463 then
6464 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
6465 Rewrite (L,
6466 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
6468 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6469 Analyze_Comparison_Op (N);
6470 else
6471 Analyze_Arithmetic_Op (N);
6472 end if;
6474 return;
6476 else
6477 Resolve (L, Etype (R));
6478 end if;
6480 return;
6482 elsif Allow_Integer_Address
6483 and then Is_Descendent_Of_Address (Etype (L))
6484 and then Is_Descendent_Of_Address (Etype (R))
6485 and then not Error_Posted (N)
6486 then
6487 declare
6488 Addr_Type : constant Entity_Id := Etype (L);
6490 begin
6491 Rewrite (L,
6492 Unchecked_Convert_To (
6493 Standard_Integer, Relocate_Node (L)));
6494 Rewrite (R,
6495 Unchecked_Convert_To (
6496 Standard_Integer, Relocate_Node (R)));
6498 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
6499 Analyze_Comparison_Op (N);
6500 else
6501 Analyze_Arithmetic_Op (N);
6502 end if;
6504 -- If this is an operand in an enclosing arithmetic
6505 -- operation, Convert the result as an address so that
6506 -- arithmetic folding of address can continue.
6508 if Nkind (Parent (N)) in N_Op then
6509 Rewrite (N,
6510 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
6511 end if;
6513 return;
6514 end;
6515 end if;
6517 -- Comparisons on A'Access are common enough to deserve a
6518 -- special message.
6520 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6521 and then Ekind (Etype (L)) = E_Access_Attribute_Type
6522 and then Ekind (Etype (R)) = E_Access_Attribute_Type
6523 then
6524 Error_Msg_N
6525 ("two access attributes cannot be compared directly", N);
6526 Error_Msg_N
6527 ("\use qualified expression for one of the operands",
6529 return;
6531 -- Another one for C programmers
6533 elsif Nkind (N) = N_Op_Concat
6534 and then Valid_Boolean_Arg (Etype (L))
6535 and then Valid_Boolean_Arg (Etype (R))
6536 then
6537 Error_Msg_N ("invalid operands for concatenation", N);
6538 Error_Msg_N -- CODEFIX
6539 ("\maybe AND was meant", N);
6540 return;
6542 -- A special case for comparison of access parameter with null
6544 elsif Nkind (N) = N_Op_Eq
6545 and then Is_Entity_Name (L)
6546 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6547 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6548 N_Access_Definition
6549 and then Nkind (R) = N_Null
6550 then
6551 Error_Msg_N ("access parameter is not allowed to be null", L);
6552 Error_Msg_N ("\(call would raise Constraint_Error)", L);
6553 return;
6555 -- Another special case for exponentiation, where the right
6556 -- operand must be Natural, independently of the base.
6558 elsif Nkind (N) = N_Op_Expon
6559 and then Is_Numeric_Type (Etype (L))
6560 and then not Is_Overloaded (R)
6561 and then
6562 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6563 and then Base_Type (Etype (R)) /= Universal_Integer
6564 then
6565 if Ada_Version >= Ada_2012
6566 and then Has_Dimension_System (Etype (L))
6567 then
6568 Error_Msg_NE
6569 ("exponent for dimensioned type must be a rational" &
6570 ", found}", R, Etype (R));
6571 else
6572 Error_Msg_NE
6573 ("exponent must be of type Natural, found}", R, Etype (R));
6574 end if;
6576 return;
6578 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
6579 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
6580 Rewrite (R,
6581 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
6582 Analyze_Equality_Op (N);
6583 return;
6584 end if;
6585 end if;
6587 -- If we fall through then just give general message. Note that in
6588 -- the following messages, if the operand is overloaded we choose
6589 -- an arbitrary type to complain about, but that is probably more
6590 -- useful than not giving a type at all.
6592 if Nkind (N) in N_Unary_Op then
6593 Error_Msg_Node_2 := Etype (R);
6594 Error_Msg_N ("operator& not defined for}", N);
6595 return;
6597 else
6598 if Nkind (N) in N_Binary_Op then
6599 if not Is_Overloaded (L)
6600 and then not Is_Overloaded (R)
6601 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6602 then
6603 Error_Msg_Node_2 := First_Subtype (Etype (R));
6604 Error_Msg_N ("there is no applicable operator& for}", N);
6606 else
6607 -- Another attempt to find a fix: one of the candidate
6608 -- interpretations may not be use-visible. This has
6609 -- already been checked for predefined operators, so
6610 -- we examine only user-defined functions.
6612 Op_Id := Get_Name_Entity_Id (Chars (N));
6614 while Present (Op_Id) loop
6615 if Ekind (Op_Id) /= E_Operator
6616 and then Is_Overloadable (Op_Id)
6617 then
6618 if not Is_Immediately_Visible (Op_Id)
6619 and then not In_Use (Scope (Op_Id))
6620 and then not Is_Abstract_Subprogram (Op_Id)
6621 and then not Is_Hidden (Op_Id)
6622 and then Ekind (Scope (Op_Id)) = E_Package
6623 and then
6624 Has_Compatible_Type
6625 (L, Etype (First_Formal (Op_Id)))
6626 and then Present
6627 (Next_Formal (First_Formal (Op_Id)))
6628 and then
6629 Has_Compatible_Type
6631 Etype (Next_Formal (First_Formal (Op_Id))))
6632 then
6633 Error_Msg_N
6634 ("No legal interpretation for operator&", N);
6635 Error_Msg_NE
6636 ("\use clause on& would make operation legal",
6637 N, Scope (Op_Id));
6638 exit;
6639 end if;
6640 end if;
6642 Op_Id := Homonym (Op_Id);
6643 end loop;
6645 if No (Op_Id) then
6646 Error_Msg_N ("invalid operand types for operator&", N);
6648 if Nkind (N) /= N_Op_Concat then
6649 Error_Msg_NE ("\left operand has}!", N, Etype (L));
6650 Error_Msg_NE ("\right operand has}!", N, Etype (R));
6652 -- For concatenation operators it is more difficult to
6653 -- determine which is the wrong operand. It is worth
6654 -- flagging explicitly an access type, for those who
6655 -- might think that a dereference happens here.
6657 elsif Is_Access_Type (Etype (L)) then
6658 Error_Msg_N ("\left operand is access type", N);
6660 elsif Is_Access_Type (Etype (R)) then
6661 Error_Msg_N ("\right operand is access type", N);
6662 end if;
6663 end if;
6664 end if;
6665 end if;
6666 end if;
6667 end;
6668 end if;
6669 end Operator_Check;
6671 -----------------------------------------
6672 -- Process_Implicit_Dereference_Prefix --
6673 -----------------------------------------
6675 function Process_Implicit_Dereference_Prefix
6676 (E : Entity_Id;
6677 P : Entity_Id) return Entity_Id
6679 Ref : Node_Id;
6680 Typ : constant Entity_Id := Designated_Type (Etype (P));
6682 begin
6683 if Present (E)
6684 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6685 then
6686 -- We create a dummy reference to E to ensure that the reference is
6687 -- not considered as part of an assignment (an implicit dereference
6688 -- can never assign to its prefix). The Comes_From_Source attribute
6689 -- needs to be propagated for accurate warnings.
6691 Ref := New_Occurrence_Of (E, Sloc (P));
6692 Set_Comes_From_Source (Ref, Comes_From_Source (P));
6693 Generate_Reference (E, Ref);
6694 end if;
6696 -- An implicit dereference is a legal occurrence of an incomplete type
6697 -- imported through a limited_with clause, if the full view is visible.
6699 if From_Limited_With (Typ)
6700 and then not From_Limited_With (Scope (Typ))
6701 and then
6702 (Is_Immediately_Visible (Scope (Typ))
6703 or else
6704 (Is_Child_Unit (Scope (Typ))
6705 and then Is_Visible_Lib_Unit (Scope (Typ))))
6706 then
6707 return Available_View (Typ);
6708 else
6709 return Typ;
6710 end if;
6711 end Process_Implicit_Dereference_Prefix;
6713 --------------------------------
6714 -- Remove_Abstract_Operations --
6715 --------------------------------
6717 procedure Remove_Abstract_Operations (N : Node_Id) is
6718 Abstract_Op : Entity_Id := Empty;
6719 Address_Descendent : Boolean := False;
6720 I : Interp_Index;
6721 It : Interp;
6723 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
6724 -- activate this if either extensions are enabled, or if the abstract
6725 -- operation in question comes from a predefined file. This latter test
6726 -- allows us to use abstract to make operations invisible to users. In
6727 -- particular, if type Address is non-private and abstract subprograms
6728 -- are used to hide its operators, they will be truly hidden.
6730 type Operand_Position is (First_Op, Second_Op);
6731 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6733 procedure Remove_Address_Interpretations (Op : Operand_Position);
6734 -- Ambiguities may arise when the operands are literal and the address
6735 -- operations in s-auxdec are visible. In that case, remove the
6736 -- interpretation of a literal as Address, to retain the semantics
6737 -- of Address as a private type.
6739 ------------------------------------
6740 -- Remove_Address_Interpretations --
6741 ------------------------------------
6743 procedure Remove_Address_Interpretations (Op : Operand_Position) is
6744 Formal : Entity_Id;
6746 begin
6747 if Is_Overloaded (N) then
6748 Get_First_Interp (N, I, It);
6749 while Present (It.Nam) loop
6750 Formal := First_Entity (It.Nam);
6752 if Op = Second_Op then
6753 Formal := Next_Entity (Formal);
6754 end if;
6756 if Is_Descendent_Of_Address (Etype (Formal)) then
6757 Address_Descendent := True;
6758 Remove_Interp (I);
6759 end if;
6761 Get_Next_Interp (I, It);
6762 end loop;
6763 end if;
6764 end Remove_Address_Interpretations;
6766 -- Start of processing for Remove_Abstract_Operations
6768 begin
6769 if Is_Overloaded (N) then
6770 if Debug_Flag_V then
6771 Write_Str ("Remove_Abstract_Operations: ");
6772 Write_Overloads (N);
6773 end if;
6775 Get_First_Interp (N, I, It);
6777 while Present (It.Nam) loop
6778 if Is_Overloadable (It.Nam)
6779 and then Is_Abstract_Subprogram (It.Nam)
6780 and then not Is_Dispatching_Operation (It.Nam)
6781 then
6782 Abstract_Op := It.Nam;
6784 if Is_Descendent_Of_Address (It.Typ) then
6785 Address_Descendent := True;
6786 Remove_Interp (I);
6787 exit;
6789 -- In Ada 2005, this operation does not participate in overload
6790 -- resolution. If the operation is defined in a predefined
6791 -- unit, it is one of the operations declared abstract in some
6792 -- variants of System, and it must be removed as well.
6794 elsif Ada_Version >= Ada_2005
6795 or else Is_Predefined_File_Name
6796 (Unit_File_Name (Get_Source_Unit (It.Nam)))
6797 then
6798 Remove_Interp (I);
6799 exit;
6800 end if;
6801 end if;
6803 Get_Next_Interp (I, It);
6804 end loop;
6806 if No (Abstract_Op) then
6808 -- If some interpretation yields an integer type, it is still
6809 -- possible that there are address interpretations. Remove them
6810 -- if one operand is a literal, to avoid spurious ambiguities
6811 -- on systems where Address is a visible integer type.
6813 if Is_Overloaded (N)
6814 and then Nkind (N) in N_Op
6815 and then Is_Integer_Type (Etype (N))
6816 then
6817 if Nkind (N) in N_Binary_Op then
6818 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6819 Remove_Address_Interpretations (Second_Op);
6821 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6822 Remove_Address_Interpretations (First_Op);
6823 end if;
6824 end if;
6825 end if;
6827 elsif Nkind (N) in N_Op then
6829 -- Remove interpretations that treat literals as addresses. This
6830 -- is never appropriate, even when Address is defined as a visible
6831 -- Integer type. The reason is that we would really prefer Address
6832 -- to behave as a private type, even in this case. If Address is a
6833 -- visible integer type, we get lots of overload ambiguities.
6835 if Nkind (N) in N_Binary_Op then
6836 declare
6837 U1 : constant Boolean :=
6838 Present (Universal_Interpretation (Right_Opnd (N)));
6839 U2 : constant Boolean :=
6840 Present (Universal_Interpretation (Left_Opnd (N)));
6842 begin
6843 if U1 then
6844 Remove_Address_Interpretations (Second_Op);
6845 end if;
6847 if U2 then
6848 Remove_Address_Interpretations (First_Op);
6849 end if;
6851 if not (U1 and U2) then
6853 -- Remove corresponding predefined operator, which is
6854 -- always added to the overload set.
6856 Get_First_Interp (N, I, It);
6857 while Present (It.Nam) loop
6858 if Scope (It.Nam) = Standard_Standard
6859 and then Base_Type (It.Typ) =
6860 Base_Type (Etype (Abstract_Op))
6861 then
6862 Remove_Interp (I);
6863 end if;
6865 Get_Next_Interp (I, It);
6866 end loop;
6868 elsif Is_Overloaded (N)
6869 and then Present (Univ_Type)
6870 then
6871 -- If both operands have a universal interpretation,
6872 -- it is still necessary to remove interpretations that
6873 -- yield Address. Any remaining ambiguities will be
6874 -- removed in Disambiguate.
6876 Get_First_Interp (N, I, It);
6877 while Present (It.Nam) loop
6878 if Is_Descendent_Of_Address (It.Typ) then
6879 Remove_Interp (I);
6881 elsif not Is_Type (It.Nam) then
6882 Set_Entity (N, It.Nam);
6883 end if;
6885 Get_Next_Interp (I, It);
6886 end loop;
6887 end if;
6888 end;
6889 end if;
6891 elsif Nkind (N) = N_Function_Call
6892 and then
6893 (Nkind (Name (N)) = N_Operator_Symbol
6894 or else
6895 (Nkind (Name (N)) = N_Expanded_Name
6896 and then
6897 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6898 then
6900 declare
6901 Arg1 : constant Node_Id := First (Parameter_Associations (N));
6902 U1 : constant Boolean :=
6903 Present (Universal_Interpretation (Arg1));
6904 U2 : constant Boolean :=
6905 Present (Next (Arg1)) and then
6906 Present (Universal_Interpretation (Next (Arg1)));
6908 begin
6909 if U1 then
6910 Remove_Address_Interpretations (First_Op);
6911 end if;
6913 if U2 then
6914 Remove_Address_Interpretations (Second_Op);
6915 end if;
6917 if not (U1 and U2) then
6918 Get_First_Interp (N, I, It);
6919 while Present (It.Nam) loop
6920 if Scope (It.Nam) = Standard_Standard
6921 and then It.Typ = Base_Type (Etype (Abstract_Op))
6922 then
6923 Remove_Interp (I);
6924 end if;
6926 Get_Next_Interp (I, It);
6927 end loop;
6928 end if;
6929 end;
6930 end if;
6932 -- If the removal has left no valid interpretations, emit an error
6933 -- message now and label node as illegal.
6935 if Present (Abstract_Op) then
6936 Get_First_Interp (N, I, It);
6938 if No (It.Nam) then
6940 -- Removal of abstract operation left no viable candidate
6942 Set_Etype (N, Any_Type);
6943 Error_Msg_Sloc := Sloc (Abstract_Op);
6944 Error_Msg_NE
6945 ("cannot call abstract operation& declared#", N, Abstract_Op);
6947 -- In Ada 2005, an abstract operation may disable predefined
6948 -- operators. Since the context is not yet known, we mark the
6949 -- predefined operators as potentially hidden. Do not include
6950 -- predefined operators when addresses are involved since this
6951 -- case is handled separately.
6953 elsif Ada_Version >= Ada_2005 and then not Address_Descendent then
6954 while Present (It.Nam) loop
6955 if Is_Numeric_Type (It.Typ)
6956 and then Scope (It.Typ) = Standard_Standard
6957 then
6958 Set_Abstract_Op (I, Abstract_Op);
6959 end if;
6961 Get_Next_Interp (I, It);
6962 end loop;
6963 end if;
6964 end if;
6966 if Debug_Flag_V then
6967 Write_Str ("Remove_Abstract_Operations done: ");
6968 Write_Overloads (N);
6969 end if;
6970 end if;
6971 end Remove_Abstract_Operations;
6973 ----------------------------
6974 -- Try_Container_Indexing --
6975 ----------------------------
6977 function Try_Container_Indexing
6978 (N : Node_Id;
6979 Prefix : Node_Id;
6980 Exprs : List_Id) return Boolean
6982 Loc : constant Source_Ptr := Sloc (N);
6983 C_Type : Entity_Id;
6984 Assoc : List_Id;
6985 Func : Entity_Id;
6986 Func_Name : Node_Id;
6987 Indexing : Node_Id;
6989 begin
6990 C_Type := Etype (Prefix);
6992 -- If indexing a class-wide container, obtain indexing primitive
6993 -- from specific type.
6995 if Is_Class_Wide_Type (C_Type) then
6996 C_Type := Etype (Base_Type (C_Type));
6997 end if;
6999 -- Check whether type has a specified indexing aspect
7001 Func_Name := Empty;
7003 if Is_Variable (Prefix) then
7004 Func_Name :=
7005 Find_Value_Of_Aspect (Etype (Prefix), Aspect_Variable_Indexing);
7006 end if;
7008 if No (Func_Name) then
7009 Func_Name :=
7010 Find_Value_Of_Aspect (Etype (Prefix), Aspect_Constant_Indexing);
7011 end if;
7013 -- If aspect does not exist the expression is illegal. Error is
7014 -- diagnosed in caller.
7016 if No (Func_Name) then
7018 -- The prefix itself may be an indexing of a container: rewrite
7019 -- as such and re-analyze.
7021 if Has_Implicit_Dereference (Etype (Prefix)) then
7022 Build_Explicit_Dereference
7023 (Prefix, First_Discriminant (Etype (Prefix)));
7024 return Try_Container_Indexing (N, Prefix, Exprs);
7026 else
7027 return False;
7028 end if;
7030 -- If the container type is derived from another container type, the
7031 -- value of the inherited aspect is the Reference operation declared
7032 -- for the parent type.
7034 -- However, Reference is also a primitive operation of the type, and
7035 -- the inherited operation has a different signature. We retrieve the
7036 -- right one from the list of primitive operations of the derived type.
7038 -- Note that predefined containers are typically all derived from one
7039 -- of the Controlled types. The code below is motivated by containers
7040 -- that are derived from other types with a Reference aspect.
7042 -- Additional machinery may be needed for types that have several user-
7043 -- defined Reference operations with different signatures ???
7045 elsif Is_Derived_Type (C_Type)
7046 and then Etype (First_Formal (Entity (Func_Name))) /= Etype (Prefix)
7047 then
7048 Func := Find_Prim_Op (C_Type, Chars (Func_Name));
7049 Func_Name := New_Occurrence_Of (Func, Loc);
7050 end if;
7052 Assoc := New_List (Relocate_Node (Prefix));
7054 -- A generalized indexing may have nore than one index expression, so
7055 -- transfer all of them to the argument list to be used in the call.
7056 -- Note that there may be named associations, in which case the node
7057 -- was rewritten earlier as a call, and has been transformed back into
7058 -- an indexed expression to share the following processing.
7060 -- The generalized indexing node is the one on which analysis and
7061 -- resolution take place. Before expansion the original node is replaced
7062 -- with the generalized indexing node, which is a call, possibly with
7063 -- a dereference operation.
7065 if Comes_From_Source (N) then
7066 Check_Compiler_Unit ("generalized indexing", N);
7067 end if;
7069 declare
7070 Arg : Node_Id;
7071 begin
7072 Arg := First (Exprs);
7073 while Present (Arg) loop
7074 Append (Relocate_Node (Arg), Assoc);
7075 Next (Arg);
7076 end loop;
7077 end;
7079 if not Is_Overloaded (Func_Name) then
7080 Func := Entity (Func_Name);
7081 Indexing :=
7082 Make_Function_Call (Loc,
7083 Name => New_Occurrence_Of (Func, Loc),
7084 Parameter_Associations => Assoc);
7085 Set_Parent (Indexing, Parent (N));
7086 Set_Generalized_Indexing (N, Indexing);
7087 Analyze (Indexing);
7088 Set_Etype (N, Etype (Indexing));
7090 -- If the return type of the indexing function is a reference type,
7091 -- add the dereference as a possible interpretation. Note that the
7092 -- indexing aspect may be a function that returns the element type
7093 -- with no intervening implicit dereference, and that the reference
7094 -- discriminant is not the first discriminant.
7096 if Has_Discriminants (Etype (Func)) then
7097 Check_Implicit_Dereference (N, Etype (Func));
7098 end if;
7100 else
7101 Indexing :=
7102 Make_Function_Call (Loc,
7103 Name => Make_Identifier (Loc, Chars (Func_Name)),
7104 Parameter_Associations => Assoc);
7106 Set_Parent (Indexing, Parent (N));
7107 Set_Generalized_Indexing (N, Indexing);
7109 declare
7110 I : Interp_Index;
7111 It : Interp;
7112 Success : Boolean;
7114 begin
7115 Get_First_Interp (Func_Name, I, It);
7116 Set_Etype (Indexing, Any_Type);
7117 while Present (It.Nam) loop
7118 Analyze_One_Call (Indexing, It.Nam, False, Success);
7120 if Success then
7121 Set_Etype (Name (Indexing), It.Typ);
7122 Set_Entity (Name (Indexing), It.Nam);
7123 Set_Etype (N, Etype (Indexing));
7125 -- Add implicit dereference interpretation
7127 if Has_Discriminants (Etype (It.Nam)) then
7128 Check_Implicit_Dereference (N, Etype (It.Nam));
7129 end if;
7131 exit;
7132 end if;
7134 Get_Next_Interp (I, It);
7135 end loop;
7136 end;
7137 end if;
7139 if Etype (Indexing) = Any_Type then
7140 Error_Msg_NE
7141 ("container cannot be indexed with&", N, Etype (First (Exprs)));
7142 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
7143 end if;
7145 return True;
7146 end Try_Container_Indexing;
7148 -----------------------
7149 -- Try_Indirect_Call --
7150 -----------------------
7152 function Try_Indirect_Call
7153 (N : Node_Id;
7154 Nam : Entity_Id;
7155 Typ : Entity_Id) return Boolean
7157 Actual : Node_Id;
7158 Formal : Entity_Id;
7160 Call_OK : Boolean;
7161 pragma Warnings (Off, Call_OK);
7163 begin
7164 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
7166 Actual := First_Actual (N);
7167 Formal := First_Formal (Designated_Type (Typ));
7168 while Present (Actual) and then Present (Formal) loop
7169 if not Has_Compatible_Type (Actual, Etype (Formal)) then
7170 return False;
7171 end if;
7173 Next (Actual);
7174 Next_Formal (Formal);
7175 end loop;
7177 if No (Actual) and then No (Formal) then
7178 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
7180 -- Nam is a candidate interpretation for the name in the call,
7181 -- if it is not an indirect call.
7183 if not Is_Type (Nam)
7184 and then Is_Entity_Name (Name (N))
7185 then
7186 Set_Entity (Name (N), Nam);
7187 end if;
7189 return True;
7191 else
7192 return False;
7193 end if;
7194 end Try_Indirect_Call;
7196 ----------------------
7197 -- Try_Indexed_Call --
7198 ----------------------
7200 function Try_Indexed_Call
7201 (N : Node_Id;
7202 Nam : Entity_Id;
7203 Typ : Entity_Id;
7204 Skip_First : Boolean) return Boolean
7206 Loc : constant Source_Ptr := Sloc (N);
7207 Actuals : constant List_Id := Parameter_Associations (N);
7208 Actual : Node_Id;
7209 Index : Entity_Id;
7211 begin
7212 Actual := First (Actuals);
7214 -- If the call was originally written in prefix form, skip the first
7215 -- actual, which is obviously not defaulted.
7217 if Skip_First then
7218 Next (Actual);
7219 end if;
7221 Index := First_Index (Typ);
7222 while Present (Actual) and then Present (Index) loop
7224 -- If the parameter list has a named association, the expression
7225 -- is definitely a call and not an indexed component.
7227 if Nkind (Actual) = N_Parameter_Association then
7228 return False;
7229 end if;
7231 if Is_Entity_Name (Actual)
7232 and then Is_Type (Entity (Actual))
7233 and then No (Next (Actual))
7234 then
7235 -- A single actual that is a type name indicates a slice if the
7236 -- type is discrete, and an error otherwise.
7238 if Is_Discrete_Type (Entity (Actual)) then
7239 Rewrite (N,
7240 Make_Slice (Loc,
7241 Prefix =>
7242 Make_Function_Call (Loc,
7243 Name => Relocate_Node (Name (N))),
7244 Discrete_Range =>
7245 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
7247 Analyze (N);
7249 else
7250 Error_Msg_N ("invalid use of type in expression", Actual);
7251 Set_Etype (N, Any_Type);
7252 end if;
7254 return True;
7256 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
7257 return False;
7258 end if;
7260 Next (Actual);
7261 Next_Index (Index);
7262 end loop;
7264 if No (Actual) and then No (Index) then
7265 Add_One_Interp (N, Nam, Component_Type (Typ));
7267 -- Nam is a candidate interpretation for the name in the call,
7268 -- if it is not an indirect call.
7270 if not Is_Type (Nam)
7271 and then Is_Entity_Name (Name (N))
7272 then
7273 Set_Entity (Name (N), Nam);
7274 end if;
7276 return True;
7277 else
7278 return False;
7279 end if;
7280 end Try_Indexed_Call;
7282 --------------------------
7283 -- Try_Object_Operation --
7284 --------------------------
7286 function Try_Object_Operation
7287 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
7289 K : constant Node_Kind := Nkind (Parent (N));
7290 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
7291 Loc : constant Source_Ptr := Sloc (N);
7292 Obj : constant Node_Id := Prefix (N);
7294 Subprog : constant Node_Id :=
7295 Make_Identifier (Sloc (Selector_Name (N)),
7296 Chars => Chars (Selector_Name (N)));
7297 -- Identifier on which possible interpretations will be collected
7299 Report_Error : Boolean := False;
7300 -- If no candidate interpretation matches the context, redo analysis
7301 -- with Report_Error True to provide additional information.
7303 Actual : Node_Id;
7304 Candidate : Entity_Id := Empty;
7305 New_Call_Node : Node_Id := Empty;
7306 Node_To_Replace : Node_Id;
7307 Obj_Type : Entity_Id := Etype (Obj);
7308 Success : Boolean := False;
7310 function Valid_Candidate
7311 (Success : Boolean;
7312 Call : Node_Id;
7313 Subp : Entity_Id) return Entity_Id;
7314 -- If the subprogram is a valid interpretation, record it, and add
7315 -- to the list of interpretations of Subprog. Otherwise return Empty.
7317 procedure Complete_Object_Operation
7318 (Call_Node : Node_Id;
7319 Node_To_Replace : Node_Id);
7320 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
7321 -- Call_Node, insert the object (or its dereference) as the first actual
7322 -- in the call, and complete the analysis of the call.
7324 procedure Report_Ambiguity (Op : Entity_Id);
7325 -- If a prefixed procedure call is ambiguous, indicate whether the
7326 -- call includes an implicit dereference or an implicit 'Access.
7328 procedure Transform_Object_Operation
7329 (Call_Node : out Node_Id;
7330 Node_To_Replace : out Node_Id);
7331 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
7332 -- Call_Node is the resulting subprogram call, Node_To_Replace is
7333 -- either N or the parent of N, and Subprog is a reference to the
7334 -- subprogram we are trying to match.
7336 function Try_Class_Wide_Operation
7337 (Call_Node : Node_Id;
7338 Node_To_Replace : Node_Id) return Boolean;
7339 -- Traverse all ancestor types looking for a class-wide subprogram
7340 -- for which the current operation is a valid non-dispatching call.
7342 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
7343 -- If prefix is overloaded, its interpretation may include different
7344 -- tagged types, and we must examine the primitive operations and
7345 -- the class-wide operations of each in order to find candidate
7346 -- interpretations for the call as a whole.
7348 function Try_Primitive_Operation
7349 (Call_Node : Node_Id;
7350 Node_To_Replace : Node_Id) return Boolean;
7351 -- Traverse the list of primitive subprograms looking for a dispatching
7352 -- operation for which the current node is a valid call .
7354 ---------------------
7355 -- Valid_Candidate --
7356 ---------------------
7358 function Valid_Candidate
7359 (Success : Boolean;
7360 Call : Node_Id;
7361 Subp : Entity_Id) return Entity_Id
7363 Arr_Type : Entity_Id;
7364 Comp_Type : Entity_Id;
7366 begin
7367 -- If the subprogram is a valid interpretation, record it in global
7368 -- variable Subprog, to collect all possible overloadings.
7370 if Success then
7371 if Subp /= Entity (Subprog) then
7372 Add_One_Interp (Subprog, Subp, Etype (Subp));
7373 end if;
7374 end if;
7376 -- If the call may be an indexed call, retrieve component type of
7377 -- resulting expression, and add possible interpretation.
7379 Arr_Type := Empty;
7380 Comp_Type := Empty;
7382 if Nkind (Call) = N_Function_Call
7383 and then Nkind (Parent (N)) = N_Indexed_Component
7384 and then Needs_One_Actual (Subp)
7385 then
7386 if Is_Array_Type (Etype (Subp)) then
7387 Arr_Type := Etype (Subp);
7389 elsif Is_Access_Type (Etype (Subp))
7390 and then Is_Array_Type (Designated_Type (Etype (Subp)))
7391 then
7392 Arr_Type := Designated_Type (Etype (Subp));
7393 end if;
7394 end if;
7396 if Present (Arr_Type) then
7398 -- Verify that the actuals (excluding the object) match the types
7399 -- of the indexes.
7401 declare
7402 Actual : Node_Id;
7403 Index : Node_Id;
7405 begin
7406 Actual := Next (First_Actual (Call));
7407 Index := First_Index (Arr_Type);
7408 while Present (Actual) and then Present (Index) loop
7409 if not Has_Compatible_Type (Actual, Etype (Index)) then
7410 Arr_Type := Empty;
7411 exit;
7412 end if;
7414 Next_Actual (Actual);
7415 Next_Index (Index);
7416 end loop;
7418 if No (Actual)
7419 and then No (Index)
7420 and then Present (Arr_Type)
7421 then
7422 Comp_Type := Component_Type (Arr_Type);
7423 end if;
7424 end;
7426 if Present (Comp_Type)
7427 and then Etype (Subprog) /= Comp_Type
7428 then
7429 Add_One_Interp (Subprog, Subp, Comp_Type);
7430 end if;
7431 end if;
7433 if Etype (Call) /= Any_Type then
7434 return Subp;
7435 else
7436 return Empty;
7437 end if;
7438 end Valid_Candidate;
7440 -------------------------------
7441 -- Complete_Object_Operation --
7442 -------------------------------
7444 procedure Complete_Object_Operation
7445 (Call_Node : Node_Id;
7446 Node_To_Replace : Node_Id)
7448 Control : constant Entity_Id := First_Formal (Entity (Subprog));
7449 Formal_Type : constant Entity_Id := Etype (Control);
7450 First_Actual : Node_Id;
7452 begin
7453 -- Place the name of the operation, with its interpretations,
7454 -- on the rewritten call.
7456 Set_Name (Call_Node, Subprog);
7458 First_Actual := First (Parameter_Associations (Call_Node));
7460 -- For cross-reference purposes, treat the new node as being in the
7461 -- source if the original one is. Set entity and type, even though
7462 -- they may be overwritten during resolution if overloaded.
7464 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
7465 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
7467 if Nkind (N) = N_Selected_Component
7468 and then not Inside_A_Generic
7469 then
7470 Set_Entity (Selector_Name (N), Entity (Subprog));
7471 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
7472 end if;
7474 -- If need be, rewrite first actual as an explicit dereference. If
7475 -- the call is overloaded, the rewriting can only be done once the
7476 -- primitive operation is identified.
7478 if Is_Overloaded (Subprog) then
7480 -- The prefix itself may be overloaded, and its interpretations
7481 -- must be propagated to the new actual in the call.
7483 if Is_Overloaded (Obj) then
7484 Save_Interps (Obj, First_Actual);
7485 end if;
7487 Rewrite (First_Actual, Obj);
7489 elsif not Is_Access_Type (Formal_Type)
7490 and then Is_Access_Type (Etype (Obj))
7491 then
7492 Rewrite (First_Actual,
7493 Make_Explicit_Dereference (Sloc (Obj), Obj));
7494 Analyze (First_Actual);
7496 -- If we need to introduce an explicit dereference, verify that
7497 -- the resulting actual is compatible with the mode of the formal.
7499 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
7500 and then Is_Access_Constant (Etype (Obj))
7501 then
7502 Error_Msg_NE
7503 ("expect variable in call to&", Prefix (N), Entity (Subprog));
7504 end if;
7506 -- Conversely, if the formal is an access parameter and the object
7507 -- is not, replace the actual with a 'Access reference. Its analysis
7508 -- will check that the object is aliased.
7510 elsif Is_Access_Type (Formal_Type)
7511 and then not Is_Access_Type (Etype (Obj))
7512 then
7513 -- A special case: A.all'access is illegal if A is an access to a
7514 -- constant and the context requires an access to a variable.
7516 if not Is_Access_Constant (Formal_Type) then
7517 if (Nkind (Obj) = N_Explicit_Dereference
7518 and then Is_Access_Constant (Etype (Prefix (Obj))))
7519 or else not Is_Variable (Obj)
7520 then
7521 Error_Msg_NE
7522 ("actual for & must be a variable", Obj, Control);
7523 end if;
7524 end if;
7526 Rewrite (First_Actual,
7527 Make_Attribute_Reference (Loc,
7528 Attribute_Name => Name_Access,
7529 Prefix => Relocate_Node (Obj)));
7531 if not Is_Aliased_View (Obj) then
7532 Error_Msg_NE
7533 ("object in prefixed call to & must be aliased "
7534 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
7535 end if;
7537 Analyze (First_Actual);
7539 else
7540 if Is_Overloaded (Obj) then
7541 Save_Interps (Obj, First_Actual);
7542 end if;
7544 Rewrite (First_Actual, Obj);
7545 end if;
7547 -- The operation is obtained from the dispatch table and not by
7548 -- visibility, and may be declared in a unit that is not explicitly
7549 -- referenced in the source, but is nevertheless required in the
7550 -- context of the current unit. Indicate that operation and its scope
7551 -- are referenced, to prevent spurious and misleading warnings. If
7552 -- the operation is overloaded, all primitives are in the same scope
7553 -- and we can use any of them.
7555 Set_Referenced (Entity (Subprog), True);
7556 Set_Referenced (Scope (Entity (Subprog)), True);
7558 Rewrite (Node_To_Replace, Call_Node);
7560 -- Propagate the interpretations collected in subprog to the new
7561 -- function call node, to be resolved from context.
7563 if Is_Overloaded (Subprog) then
7564 Save_Interps (Subprog, Node_To_Replace);
7566 else
7567 -- The type of the subprogram may be a limited view obtained
7568 -- transitively from another unit. If full view is available,
7569 -- use it to analyze call.
7571 declare
7572 T : constant Entity_Id := Etype (Subprog);
7573 begin
7574 if From_Limited_With (T) then
7575 Set_Etype (Entity (Subprog), Available_View (T));
7576 end if;
7577 end;
7579 Analyze (Node_To_Replace);
7581 -- If the operation has been rewritten into a call, which may get
7582 -- subsequently an explicit dereference, preserve the type on the
7583 -- original node (selected component or indexed component) for
7584 -- subsequent legality tests, e.g. Is_Variable. which examines
7585 -- the original node.
7587 if Nkind (Node_To_Replace) = N_Function_Call then
7588 Set_Etype
7589 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
7590 end if;
7591 end if;
7592 end Complete_Object_Operation;
7594 ----------------------
7595 -- Report_Ambiguity --
7596 ----------------------
7598 procedure Report_Ambiguity (Op : Entity_Id) is
7599 Access_Actual : constant Boolean :=
7600 Is_Access_Type (Etype (Prefix (N)));
7601 Access_Formal : Boolean := False;
7603 begin
7604 Error_Msg_Sloc := Sloc (Op);
7606 if Present (First_Formal (Op)) then
7607 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
7608 end if;
7610 if Access_Formal and then not Access_Actual then
7611 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7612 Error_Msg_N
7613 ("\possible interpretation "
7614 & "(inherited, with implicit 'Access) #", N);
7615 else
7616 Error_Msg_N
7617 ("\possible interpretation (with implicit 'Access) #", N);
7618 end if;
7620 elsif not Access_Formal and then Access_Actual then
7621 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7622 Error_Msg_N
7623 ("\possible interpretation "
7624 & "(inherited, with implicit dereference) #", N);
7625 else
7626 Error_Msg_N
7627 ("\possible interpretation (with implicit dereference) #", N);
7628 end if;
7630 else
7631 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7632 Error_Msg_N ("\possible interpretation (inherited)#", N);
7633 else
7634 Error_Msg_N -- CODEFIX
7635 ("\possible interpretation#", N);
7636 end if;
7637 end if;
7638 end Report_Ambiguity;
7640 --------------------------------
7641 -- Transform_Object_Operation --
7642 --------------------------------
7644 procedure Transform_Object_Operation
7645 (Call_Node : out Node_Id;
7646 Node_To_Replace : out Node_Id)
7648 Dummy : constant Node_Id := New_Copy (Obj);
7649 -- Placeholder used as a first parameter in the call, replaced
7650 -- eventually by the proper object.
7652 Parent_Node : constant Node_Id := Parent (N);
7654 Actual : Node_Id;
7655 Actuals : List_Id;
7657 begin
7658 -- Common case covering 1) Call to a procedure and 2) Call to a
7659 -- function that has some additional actuals.
7661 if Nkind (Parent_Node) in N_Subprogram_Call
7663 -- N is a selected component node containing the name of the
7664 -- subprogram. If N is not the name of the parent node we must
7665 -- not replace the parent node by the new construct. This case
7666 -- occurs when N is a parameterless call to a subprogram that
7667 -- is an actual parameter of a call to another subprogram. For
7668 -- example:
7669 -- Some_Subprogram (..., Obj.Operation, ...)
7671 and then Name (Parent_Node) = N
7672 then
7673 Node_To_Replace := Parent_Node;
7675 Actuals := Parameter_Associations (Parent_Node);
7677 if Present (Actuals) then
7678 Prepend (Dummy, Actuals);
7679 else
7680 Actuals := New_List (Dummy);
7681 end if;
7683 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
7684 Call_Node :=
7685 Make_Procedure_Call_Statement (Loc,
7686 Name => New_Copy (Subprog),
7687 Parameter_Associations => Actuals);
7689 else
7690 Call_Node :=
7691 Make_Function_Call (Loc,
7692 Name => New_Copy (Subprog),
7693 Parameter_Associations => Actuals);
7694 end if;
7696 -- Before analysis, a function call appears as an indexed component
7697 -- if there are no named associations.
7699 elsif Nkind (Parent_Node) = N_Indexed_Component
7700 and then N = Prefix (Parent_Node)
7701 then
7702 Node_To_Replace := Parent_Node;
7703 Actuals := Expressions (Parent_Node);
7705 Actual := First (Actuals);
7706 while Present (Actual) loop
7707 Analyze (Actual);
7708 Next (Actual);
7709 end loop;
7711 Prepend (Dummy, Actuals);
7713 Call_Node :=
7714 Make_Function_Call (Loc,
7715 Name => New_Copy (Subprog),
7716 Parameter_Associations => Actuals);
7718 -- Parameterless call: Obj.F is rewritten as F (Obj)
7720 else
7721 Node_To_Replace := N;
7723 Call_Node :=
7724 Make_Function_Call (Loc,
7725 Name => New_Copy (Subprog),
7726 Parameter_Associations => New_List (Dummy));
7727 end if;
7728 end Transform_Object_Operation;
7730 ------------------------------
7731 -- Try_Class_Wide_Operation --
7732 ------------------------------
7734 function Try_Class_Wide_Operation
7735 (Call_Node : Node_Id;
7736 Node_To_Replace : Node_Id) return Boolean
7738 Anc_Type : Entity_Id;
7739 Matching_Op : Entity_Id := Empty;
7740 Error : Boolean;
7742 procedure Traverse_Homonyms
7743 (Anc_Type : Entity_Id;
7744 Error : out Boolean);
7745 -- Traverse the homonym chain of the subprogram searching for those
7746 -- homonyms whose first formal has the Anc_Type's class-wide type,
7747 -- or an anonymous access type designating the class-wide type. If
7748 -- an ambiguity is detected, then Error is set to True.
7750 procedure Traverse_Interfaces
7751 (Anc_Type : Entity_Id;
7752 Error : out Boolean);
7753 -- Traverse the list of interfaces, if any, associated with Anc_Type
7754 -- and search for acceptable class-wide homonyms associated with each
7755 -- interface. If an ambiguity is detected, then Error is set to True.
7757 -----------------------
7758 -- Traverse_Homonyms --
7759 -----------------------
7761 procedure Traverse_Homonyms
7762 (Anc_Type : Entity_Id;
7763 Error : out Boolean)
7765 Cls_Type : Entity_Id;
7766 Hom : Entity_Id;
7767 Hom_Ref : Node_Id;
7768 Success : Boolean;
7770 begin
7771 Error := False;
7773 Cls_Type := Class_Wide_Type (Anc_Type);
7775 Hom := Current_Entity (Subprog);
7777 -- Find a non-hidden operation whose first parameter is of the
7778 -- class-wide type, a subtype thereof, or an anonymous access
7779 -- to same. If in an instance, the operation can be considered
7780 -- even if hidden (it may be hidden because the instantiation
7781 -- is expanded after the containing package has been analyzed).
7783 while Present (Hom) loop
7784 if Ekind_In (Hom, E_Procedure, E_Function)
7785 and then (not Is_Hidden (Hom) or else In_Instance)
7786 and then Scope (Hom) = Scope (Anc_Type)
7787 and then Present (First_Formal (Hom))
7788 and then
7789 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7790 or else
7791 (Is_Access_Type (Etype (First_Formal (Hom)))
7792 and then
7793 Ekind (Etype (First_Formal (Hom))) =
7794 E_Anonymous_Access_Type
7795 and then
7796 Base_Type
7797 (Designated_Type (Etype (First_Formal (Hom)))) =
7798 Cls_Type))
7799 then
7800 -- If the context is a procedure call, ignore functions
7801 -- in the name of the call.
7803 if Ekind (Hom) = E_Function
7804 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7805 and then N = Name (Parent (N))
7806 then
7807 goto Next_Hom;
7809 -- If the context is a function call, ignore procedures
7810 -- in the name of the call.
7812 elsif Ekind (Hom) = E_Procedure
7813 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
7814 then
7815 goto Next_Hom;
7816 end if;
7818 Set_Etype (Call_Node, Any_Type);
7819 Set_Is_Overloaded (Call_Node, False);
7820 Success := False;
7822 if No (Matching_Op) then
7823 Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog));
7824 Set_Etype (Call_Node, Any_Type);
7825 Set_Parent (Call_Node, Parent (Node_To_Replace));
7827 Set_Name (Call_Node, Hom_Ref);
7829 Analyze_One_Call
7830 (N => Call_Node,
7831 Nam => Hom,
7832 Report => Report_Error,
7833 Success => Success,
7834 Skip_First => True);
7836 Matching_Op :=
7837 Valid_Candidate (Success, Call_Node, Hom);
7839 else
7840 Analyze_One_Call
7841 (N => Call_Node,
7842 Nam => Hom,
7843 Report => Report_Error,
7844 Success => Success,
7845 Skip_First => True);
7847 if Present (Valid_Candidate (Success, Call_Node, Hom))
7848 and then Nkind (Call_Node) /= N_Function_Call
7849 then
7850 Error_Msg_NE ("ambiguous call to&", N, Hom);
7851 Report_Ambiguity (Matching_Op);
7852 Report_Ambiguity (Hom);
7853 Error := True;
7854 return;
7855 end if;
7856 end if;
7857 end if;
7859 <<Next_Hom>>
7860 Hom := Homonym (Hom);
7861 end loop;
7862 end Traverse_Homonyms;
7864 -------------------------
7865 -- Traverse_Interfaces --
7866 -------------------------
7868 procedure Traverse_Interfaces
7869 (Anc_Type : Entity_Id;
7870 Error : out Boolean)
7872 Intface_List : constant List_Id :=
7873 Abstract_Interface_List (Anc_Type);
7874 Intface : Node_Id;
7876 begin
7877 Error := False;
7879 if Is_Non_Empty_List (Intface_List) then
7880 Intface := First (Intface_List);
7881 while Present (Intface) loop
7883 -- Look for acceptable class-wide homonyms associated with
7884 -- the interface.
7886 Traverse_Homonyms (Etype (Intface), Error);
7888 if Error then
7889 return;
7890 end if;
7892 -- Continue the search by looking at each of the interface's
7893 -- associated interface ancestors.
7895 Traverse_Interfaces (Etype (Intface), Error);
7897 if Error then
7898 return;
7899 end if;
7901 Next (Intface);
7902 end loop;
7903 end if;
7904 end Traverse_Interfaces;
7906 -- Start of processing for Try_Class_Wide_Operation
7908 begin
7909 -- If we are searching only for conflicting class-wide subprograms
7910 -- then initialize directly Matching_Op with the target entity.
7912 if CW_Test_Only then
7913 Matching_Op := Entity (Selector_Name (N));
7914 end if;
7916 -- Loop through ancestor types (including interfaces), traversing
7917 -- the homonym chain of the subprogram, trying out those homonyms
7918 -- whose first formal has the class-wide type of the ancestor, or
7919 -- an anonymous access type designating the class-wide type.
7921 Anc_Type := Obj_Type;
7922 loop
7923 -- Look for a match among homonyms associated with the ancestor
7925 Traverse_Homonyms (Anc_Type, Error);
7927 if Error then
7928 return True;
7929 end if;
7931 -- Continue the search for matches among homonyms associated with
7932 -- any interfaces implemented by the ancestor.
7934 Traverse_Interfaces (Anc_Type, Error);
7936 if Error then
7937 return True;
7938 end if;
7940 exit when Etype (Anc_Type) = Anc_Type;
7941 Anc_Type := Etype (Anc_Type);
7942 end loop;
7944 if Present (Matching_Op) then
7945 Set_Etype (Call_Node, Etype (Matching_Op));
7946 end if;
7948 return Present (Matching_Op);
7949 end Try_Class_Wide_Operation;
7951 -----------------------------------
7952 -- Try_One_Prefix_Interpretation --
7953 -----------------------------------
7955 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7956 begin
7957 Obj_Type := T;
7959 if Is_Access_Type (Obj_Type) then
7960 Obj_Type := Designated_Type (Obj_Type);
7961 end if;
7963 if Ekind (Obj_Type) = E_Private_Subtype then
7964 Obj_Type := Base_Type (Obj_Type);
7965 end if;
7967 if Is_Class_Wide_Type (Obj_Type) then
7968 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7969 end if;
7971 -- The type may have be obtained through a limited_with clause,
7972 -- in which case the primitive operations are available on its
7973 -- non-limited view. If still incomplete, retrieve full view.
7975 if Ekind (Obj_Type) = E_Incomplete_Type
7976 and then From_Limited_With (Obj_Type)
7977 then
7978 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7979 end if;
7981 -- If the object is not tagged, or the type is still an incomplete
7982 -- type, this is not a prefixed call.
7984 if not Is_Tagged_Type (Obj_Type)
7985 or else Is_Incomplete_Type (Obj_Type)
7986 then
7987 return;
7988 end if;
7990 declare
7991 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
7992 CW_Result : Boolean;
7993 Prim_Result : Boolean;
7994 pragma Unreferenced (CW_Result);
7996 begin
7997 if not CW_Test_Only then
7998 Prim_Result :=
7999 Try_Primitive_Operation
8000 (Call_Node => New_Call_Node,
8001 Node_To_Replace => Node_To_Replace);
8002 end if;
8004 -- Check if there is a class-wide subprogram covering the
8005 -- primitive. This check must be done even if a candidate
8006 -- was found in order to report ambiguous calls.
8008 if not (Prim_Result) then
8009 CW_Result :=
8010 Try_Class_Wide_Operation
8011 (Call_Node => New_Call_Node,
8012 Node_To_Replace => Node_To_Replace);
8014 -- If we found a primitive we search for class-wide subprograms
8015 -- using a duplicate of the call node (done to avoid missing its
8016 -- decoration if there is no ambiguity).
8018 else
8019 CW_Result :=
8020 Try_Class_Wide_Operation
8021 (Call_Node => Dup_Call_Node,
8022 Node_To_Replace => Node_To_Replace);
8023 end if;
8024 end;
8025 end Try_One_Prefix_Interpretation;
8027 -----------------------------
8028 -- Try_Primitive_Operation --
8029 -----------------------------
8031 function Try_Primitive_Operation
8032 (Call_Node : Node_Id;
8033 Node_To_Replace : Node_Id) return Boolean
8035 Elmt : Elmt_Id;
8036 Prim_Op : Entity_Id;
8037 Matching_Op : Entity_Id := Empty;
8038 Prim_Op_Ref : Node_Id := Empty;
8040 Corr_Type : Entity_Id := Empty;
8041 -- If the prefix is a synchronized type, the controlling type of
8042 -- the primitive operation is the corresponding record type, else
8043 -- this is the object type itself.
8045 Success : Boolean := False;
8047 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
8048 -- For tagged types the candidate interpretations are found in
8049 -- the list of primitive operations of the type and its ancestors.
8050 -- For formal tagged types we have to find the operations declared
8051 -- in the same scope as the type (including in the generic formal
8052 -- part) because the type itself carries no primitive operations,
8053 -- except for formal derived types that inherit the operations of
8054 -- the parent and progenitors.
8056 -- If the context is a generic subprogram body, the generic formals
8057 -- are visible by name, but are not in the entity list of the
8058 -- subprogram because that list starts with the subprogram formals.
8059 -- We retrieve the candidate operations from the generic declaration.
8061 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
8062 -- An operation that overrides an inherited operation in the private
8063 -- part of its package may be hidden, but if the inherited operation
8064 -- is visible a direct call to it will dispatch to the private one,
8065 -- which is therefore a valid candidate.
8067 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
8068 -- Verify that the prefix, dereferenced if need be, is a valid
8069 -- controlling argument in a call to Op. The remaining actuals
8070 -- are checked in the subsequent call to Analyze_One_Call.
8072 ------------------------------
8073 -- Collect_Generic_Type_Ops --
8074 ------------------------------
8076 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
8077 Bas : constant Entity_Id := Base_Type (T);
8078 Candidates : constant Elist_Id := New_Elmt_List;
8079 Subp : Entity_Id;
8080 Formal : Entity_Id;
8082 procedure Check_Candidate;
8083 -- The operation is a candidate if its first parameter is a
8084 -- controlling operand of the desired type.
8086 -----------------------
8087 -- Check_Candidate; --
8088 -----------------------
8090 procedure Check_Candidate is
8091 begin
8092 Formal := First_Formal (Subp);
8094 if Present (Formal)
8095 and then Is_Controlling_Formal (Formal)
8096 and then
8097 (Base_Type (Etype (Formal)) = Bas
8098 or else
8099 (Is_Access_Type (Etype (Formal))
8100 and then Designated_Type (Etype (Formal)) = Bas))
8101 then
8102 Append_Elmt (Subp, Candidates);
8103 end if;
8104 end Check_Candidate;
8106 -- Start of processing for Collect_Generic_Type_Ops
8108 begin
8109 if Is_Derived_Type (T) then
8110 return Primitive_Operations (T);
8112 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
8114 -- Scan the list of generic formals to find subprograms
8115 -- that may have a first controlling formal of the type.
8117 if Nkind (Unit_Declaration_Node (Scope (T))) =
8118 N_Generic_Subprogram_Declaration
8119 then
8120 declare
8121 Decl : Node_Id;
8123 begin
8124 Decl :=
8125 First (Generic_Formal_Declarations
8126 (Unit_Declaration_Node (Scope (T))));
8127 while Present (Decl) loop
8128 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
8129 Subp := Defining_Entity (Decl);
8130 Check_Candidate;
8131 end if;
8133 Next (Decl);
8134 end loop;
8135 end;
8136 end if;
8137 return Candidates;
8139 else
8140 -- Scan the list of entities declared in the same scope as
8141 -- the type. In general this will be an open scope, given that
8142 -- the call we are analyzing can only appear within a generic
8143 -- declaration or body (either the one that declares T, or a
8144 -- child unit).
8146 -- For a subtype representing a generic actual type, go to the
8147 -- base type.
8149 if Is_Generic_Actual_Type (T) then
8150 Subp := First_Entity (Scope (Base_Type (T)));
8151 else
8152 Subp := First_Entity (Scope (T));
8153 end if;
8155 while Present (Subp) loop
8156 if Is_Overloadable (Subp) then
8157 Check_Candidate;
8158 end if;
8160 Next_Entity (Subp);
8161 end loop;
8163 return Candidates;
8164 end if;
8165 end Collect_Generic_Type_Ops;
8167 ---------------------------
8168 -- Is_Private_Overriding --
8169 ---------------------------
8171 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
8172 Visible_Op : constant Entity_Id := Homonym (Op);
8174 begin
8175 return Present (Visible_Op)
8176 and then Scope (Op) = Scope (Visible_Op)
8177 and then not Comes_From_Source (Visible_Op)
8178 and then Alias (Visible_Op) = Op
8179 and then not Is_Hidden (Visible_Op);
8180 end Is_Private_Overriding;
8182 -----------------------------
8183 -- Valid_First_Argument_Of --
8184 -----------------------------
8186 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
8187 Typ : Entity_Id := Etype (First_Formal (Op));
8189 begin
8190 if Is_Concurrent_Type (Typ)
8191 and then Present (Corresponding_Record_Type (Typ))
8192 then
8193 Typ := Corresponding_Record_Type (Typ);
8194 end if;
8196 -- Simple case. Object may be a subtype of the tagged type or
8197 -- may be the corresponding record of a synchronized type.
8199 return Obj_Type = Typ
8200 or else Base_Type (Obj_Type) = Typ
8201 or else Corr_Type = Typ
8203 -- Prefix can be dereferenced
8205 or else
8206 (Is_Access_Type (Corr_Type)
8207 and then Designated_Type (Corr_Type) = Typ)
8209 -- Formal is an access parameter, for which the object
8210 -- can provide an access.
8212 or else
8213 (Ekind (Typ) = E_Anonymous_Access_Type
8214 and then
8215 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
8216 end Valid_First_Argument_Of;
8218 -- Start of processing for Try_Primitive_Operation
8220 begin
8221 -- Look for subprograms in the list of primitive operations. The name
8222 -- must be identical, and the kind of call indicates the expected
8223 -- kind of operation (function or procedure). If the type is a
8224 -- (tagged) synchronized type, the primitive ops are attached to the
8225 -- corresponding record (base) type.
8227 if Is_Concurrent_Type (Obj_Type) then
8228 if Present (Corresponding_Record_Type (Obj_Type)) then
8229 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
8230 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
8231 else
8232 Corr_Type := Obj_Type;
8233 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
8234 end if;
8236 elsif not Is_Generic_Type (Obj_Type) then
8237 Corr_Type := Obj_Type;
8238 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
8240 else
8241 Corr_Type := Obj_Type;
8242 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
8243 end if;
8245 while Present (Elmt) loop
8246 Prim_Op := Node (Elmt);
8248 if Chars (Prim_Op) = Chars (Subprog)
8249 and then Present (First_Formal (Prim_Op))
8250 and then Valid_First_Argument_Of (Prim_Op)
8251 and then
8252 (Nkind (Call_Node) = N_Function_Call)
8254 (Ekind (Prim_Op) = E_Function)
8255 then
8256 -- Ada 2005 (AI-251): If this primitive operation corresponds
8257 -- to an immediate ancestor interface there is no need to add
8258 -- it to the list of interpretations; the corresponding aliased
8259 -- primitive is also in this list of primitive operations and
8260 -- will be used instead.
8262 if (Present (Interface_Alias (Prim_Op))
8263 and then Is_Ancestor (Find_Dispatching_Type
8264 (Alias (Prim_Op)), Corr_Type))
8266 -- Do not consider hidden primitives unless the type is in an
8267 -- open scope or we are within an instance, where visibility
8268 -- is known to be correct, or else if this is an overriding
8269 -- operation in the private part for an inherited operation.
8271 or else (Is_Hidden (Prim_Op)
8272 and then not Is_Immediately_Visible (Obj_Type)
8273 and then not In_Instance
8274 and then not Is_Private_Overriding (Prim_Op))
8275 then
8276 goto Continue;
8277 end if;
8279 Set_Etype (Call_Node, Any_Type);
8280 Set_Is_Overloaded (Call_Node, False);
8282 if No (Matching_Op) then
8283 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
8284 Candidate := Prim_Op;
8286 Set_Parent (Call_Node, Parent (Node_To_Replace));
8288 Set_Name (Call_Node, Prim_Op_Ref);
8289 Success := False;
8291 Analyze_One_Call
8292 (N => Call_Node,
8293 Nam => Prim_Op,
8294 Report => Report_Error,
8295 Success => Success,
8296 Skip_First => True);
8298 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
8300 -- More than one interpretation, collect for subsequent
8301 -- disambiguation. If this is a procedure call and there
8302 -- is another match, report ambiguity now.
8304 else
8305 Analyze_One_Call
8306 (N => Call_Node,
8307 Nam => Prim_Op,
8308 Report => Report_Error,
8309 Success => Success,
8310 Skip_First => True);
8312 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
8313 and then Nkind (Call_Node) /= N_Function_Call
8314 then
8315 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
8316 Report_Ambiguity (Matching_Op);
8317 Report_Ambiguity (Prim_Op);
8318 return True;
8319 end if;
8320 end if;
8321 end if;
8323 <<Continue>>
8324 Next_Elmt (Elmt);
8325 end loop;
8327 if Present (Matching_Op) then
8328 Set_Etype (Call_Node, Etype (Matching_Op));
8329 end if;
8331 return Present (Matching_Op);
8332 end Try_Primitive_Operation;
8334 -- Start of processing for Try_Object_Operation
8336 begin
8337 Analyze_Expression (Obj);
8339 -- Analyze the actuals if node is known to be a subprogram call
8341 if Is_Subprg_Call and then N = Name (Parent (N)) then
8342 Actual := First (Parameter_Associations (Parent (N)));
8343 while Present (Actual) loop
8344 Analyze_Expression (Actual);
8345 Next (Actual);
8346 end loop;
8347 end if;
8349 -- Build a subprogram call node, using a copy of Obj as its first
8350 -- actual. This is a placeholder, to be replaced by an explicit
8351 -- dereference when needed.
8353 Transform_Object_Operation
8354 (Call_Node => New_Call_Node,
8355 Node_To_Replace => Node_To_Replace);
8357 Set_Etype (New_Call_Node, Any_Type);
8358 Set_Etype (Subprog, Any_Type);
8359 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
8361 if not Is_Overloaded (Obj) then
8362 Try_One_Prefix_Interpretation (Obj_Type);
8364 else
8365 declare
8366 I : Interp_Index;
8367 It : Interp;
8368 begin
8369 Get_First_Interp (Obj, I, It);
8370 while Present (It.Nam) loop
8371 Try_One_Prefix_Interpretation (It.Typ);
8372 Get_Next_Interp (I, It);
8373 end loop;
8374 end;
8375 end if;
8377 if Etype (New_Call_Node) /= Any_Type then
8379 -- No need to complete the tree transformations if we are only
8380 -- searching for conflicting class-wide subprograms
8382 if CW_Test_Only then
8383 return False;
8384 else
8385 Complete_Object_Operation
8386 (Call_Node => New_Call_Node,
8387 Node_To_Replace => Node_To_Replace);
8388 return True;
8389 end if;
8391 elsif Present (Candidate) then
8393 -- The argument list is not type correct. Re-analyze with error
8394 -- reporting enabled, and use one of the possible candidates.
8395 -- In All_Errors_Mode, re-analyze all failed interpretations.
8397 if All_Errors_Mode then
8398 Report_Error := True;
8399 if Try_Primitive_Operation
8400 (Call_Node => New_Call_Node,
8401 Node_To_Replace => Node_To_Replace)
8403 or else
8404 Try_Class_Wide_Operation
8405 (Call_Node => New_Call_Node,
8406 Node_To_Replace => Node_To_Replace)
8407 then
8408 null;
8409 end if;
8411 else
8412 Analyze_One_Call
8413 (N => New_Call_Node,
8414 Nam => Candidate,
8415 Report => True,
8416 Success => Success,
8417 Skip_First => True);
8418 end if;
8420 -- No need for further errors
8422 return True;
8424 else
8425 -- There was no candidate operation, so report it as an error
8426 -- in the caller: Analyze_Selected_Component.
8428 return False;
8429 end if;
8430 end Try_Object_Operation;
8432 ---------
8433 -- wpo --
8434 ---------
8436 procedure wpo (T : Entity_Id) is
8437 Op : Entity_Id;
8438 E : Elmt_Id;
8440 begin
8441 if not Is_Tagged_Type (T) then
8442 return;
8443 end if;
8445 E := First_Elmt (Primitive_Operations (Base_Type (T)));
8446 while Present (E) loop
8447 Op := Node (E);
8448 Write_Int (Int (Op));
8449 Write_Str (" === ");
8450 Write_Name (Chars (Op));
8451 Write_Str (" in ");
8452 Write_Name (Chars (Scope (Op)));
8453 Next_Elmt (E);
8454 Write_Eol;
8455 end loop;
8456 end wpo;
8458 end Sem_Ch4;