ada: Spurious warning about negative modular literal
[official-gcc.git] / gcc / ada / sem_res.adb
blobc708d04fc32a73a46ed0b05a497ea5843d239efb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ R E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, 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 Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Debug_A; use Debug_A;
32 with Einfo; use Einfo;
33 with Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Elists; use Elists;
36 with Errout; use Errout;
37 with Expander; use Expander;
38 with Exp_Ch6; use Exp_Ch6;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Inline; use Inline;
46 with Itypes; use Itypes;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nmake; use Nmake;
51 with Nlists; use Nlists;
52 with Opt; use Opt;
53 with Output; use Output;
54 with Par_SCO; use Par_SCO;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aggr; use Sem_Aggr;
60 with Sem_Attr; use Sem_Attr;
61 with Sem_Aux; use Sem_Aux;
62 with Sem_Case; use Sem_Case;
63 with Sem_Cat; use Sem_Cat;
64 with Sem_Ch3; use Sem_Ch3;
65 with Sem_Ch4; use Sem_Ch4;
66 with Sem_Ch5; use Sem_Ch5;
67 with Sem_Ch6; use Sem_Ch6;
68 with Sem_Ch8; use Sem_Ch8;
69 with Sem_Ch13; use Sem_Ch13;
70 with Sem_Dim; use Sem_Dim;
71 with Sem_Disp; use Sem_Disp;
72 with Sem_Dist; use Sem_Dist;
73 with Sem_Elab; use Sem_Elab;
74 with Sem_Elim; use Sem_Elim;
75 with Sem_Eval; use Sem_Eval;
76 with Sem_Intr; use Sem_Intr;
77 with Sem_Mech; use Sem_Mech;
78 with Sem_Type; use Sem_Type;
79 with Sem_Util; use Sem_Util;
80 with Sem_Warn; use Sem_Warn;
81 with Sinfo; use Sinfo;
82 with Sinfo.Nodes; use Sinfo.Nodes;
83 with Sinfo.Utils; use Sinfo.Utils;
84 with Sinfo.CN; use Sinfo.CN;
85 with Snames; use Snames;
86 with Stand; use Stand;
87 with Stringt; use Stringt;
88 with Strub; use Strub;
89 with Style; use Style;
90 with Targparm; use Targparm;
91 with Tbuild; use Tbuild;
92 with Uintp; use Uintp;
93 with Urealp; use Urealp;
94 with Warnsw; use Warnsw;
96 package body Sem_Res is
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 -- Second pass (top-down) type checking and overload resolution procedures
103 -- Typ is the type required by context. These procedures propagate the
104 -- type information recursively to the descendants of N. If the node is not
105 -- overloaded, its Etype is established in the first pass. If overloaded,
106 -- the Resolve routines set the correct type. For arithmetic operators, the
107 -- Etype is the base type of the context.
109 -- Note that Resolve_Attribute is separated off in Sem_Attr
111 function Has_Applicable_User_Defined_Literal
112 (N : Node_Id;
113 Typ : Entity_Id) return Boolean;
114 -- Check whether N is a literal or a named number, and whether Typ has a
115 -- user-defined literal aspect that may apply to N. In this case, replace
116 -- N with a call to the corresponding function and return True.
118 procedure Check_Discriminant_Use (N : Node_Id);
119 -- Enforce the restrictions on the use of discriminants when constraining
120 -- a component of a discriminated type (record or concurrent type).
122 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
123 -- Given a node for an operator associated with type T, check that the
124 -- operator is visible. Operators all of whose operands are universal must
125 -- be checked for visibility during resolution because their type is not
126 -- determinable based on their operands.
128 procedure Check_Fully_Declared_Prefix
129 (Typ : Entity_Id;
130 Pref : Node_Id);
131 -- Check that the type of the prefix of a dereference is not incomplete
133 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
134 -- Given a call node, Call, which is known to occur immediately within the
135 -- subprogram being called, determines whether it is a detectable case of
136 -- an infinite recursion, and if so, outputs appropriate messages. Returns
137 -- True if an infinite recursion is detected, and False otherwise.
139 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
140 -- N is the node for a logical operator. If the operator is predefined, and
141 -- the root type of the operands is Standard.Boolean, then a check is made
142 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
143 -- the style check for Style_Check_Boolean_And_Or.
145 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
146 -- N is either an indexed component or a selected component. This function
147 -- returns true if the prefix denotes an atomic object that has an address
148 -- clause (the case in which we may want to issue a warning).
150 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean;
151 -- Determine whether E is an access type declared by an access declaration,
152 -- and not an (anonymous) allocator type.
154 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
155 -- Utility to check whether the entity for an operator is a predefined
156 -- operator, in which case the expression is left as an operator in the
157 -- tree (else it is rewritten into a call). An instance of an intrinsic
158 -- conversion operation may be given an operator name, but is not treated
159 -- like an operator. Note that an operator that is an imported back-end
160 -- builtin has convention Intrinsic, but is expected to be rewritten into
161 -- a call, so such an operator is not treated as predefined by this
162 -- predicate.
164 procedure Preanalyze_And_Resolve
165 (N : Node_Id;
166 T : Entity_Id;
167 With_Freezing : Boolean);
168 -- Subsidiary of public versions of Preanalyze_And_Resolve.
170 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
171 -- If a default expression in entry call N depends on the discriminants
172 -- of the task, it must be replaced with a reference to the discriminant
173 -- of the task being called.
175 procedure Resolve_Dependent_Expression
176 (N : Node_Id;
177 Expr : Node_Id;
178 Typ : Entity_Id);
179 -- Internal procedure to resolve the dependent expression Expr of the
180 -- conditional expression N with type Typ.
182 procedure Resolve_Op_Concat_Arg
183 (N : Node_Id;
184 Arg : Node_Id;
185 Typ : Entity_Id;
186 Is_Comp : Boolean);
187 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
188 -- concatenation operator. The operand is either of the array type or of
189 -- the component type. If the operand is an aggregate, and the component
190 -- type is composite, this is ambiguous if component type has aggregates.
192 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
193 -- Does the first part of the work of Resolve_Op_Concat
195 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
196 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
197 -- has been resolved. See Resolve_Op_Concat for details.
199 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Interpolated_String_Literal
215 (N : Node_Id;
216 Typ : Entity_Id);
217 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
218 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
219 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
220 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
221 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
222 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
223 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
224 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
225 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
226 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
227 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
228 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
229 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
230 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
231 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
232 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
233 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
234 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
235 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
236 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
237 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
238 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
240 function Operator_Kind
241 (Op_Name : Name_Id;
242 Is_Binary : Boolean) return Node_Kind;
243 -- Utility to map the name of an operator into the corresponding Node. Used
244 -- by other node rewriting procedures.
246 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
247 -- Resolve actuals of call, and add default expressions for missing ones.
248 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
249 -- called subprogram.
251 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
252 -- Called from Resolve_Call, when the prefix denotes an entry or element
253 -- of entry family. Actuals are resolved as for subprograms, and the node
254 -- is rebuilt as an entry call. Also called for protected operations. Typ
255 -- is the context type, which is used when the operation is a protected
256 -- function with no arguments, and the return value is indexed.
258 procedure Resolve_Implicit_Dereference (P : Node_Id);
259 -- Called when P is the prefix of an indexed component, or of a selected
260 -- component, or of a slice. If P is of an access type, we unconditionally
261 -- rewrite it as an explicit dereference. This ensures that the expander
262 -- and the code generator have a fully explicit tree to work with.
264 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
265 -- A call to a user-defined intrinsic operator is rewritten as a call to
266 -- the corresponding predefined operator, with suitable conversions. Note
267 -- that this applies only for intrinsic operators that denote predefined
268 -- operators, not ones that are intrinsic imports of back-end builtins.
270 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
271 -- Ditto, for arithmetic unary operators
273 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
274 -- If an operator node resolves to a call to a user-defined operator,
275 -- rewrite the node as a function call.
277 procedure Make_Call_Into_Operator
278 (N : Node_Id;
279 Typ : Entity_Id;
280 Op_Id : Entity_Id);
281 -- Inverse transformation: if an operator is given in functional notation,
282 -- then after resolving the node, transform into an operator node, so that
283 -- operands are resolved properly. Recall that predefined operators do not
284 -- have a full signature and special resolution rules apply.
286 procedure Rewrite_Renamed_Operator
287 (N : Node_Id;
288 Op : Entity_Id;
289 Typ : Entity_Id);
290 -- An operator can rename another, e.g. in an instantiation. In that
291 -- case, the proper operator node must be constructed and resolved.
293 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
294 -- The String_Literal_Subtype is built for all strings that are not
295 -- operands of a static concatenation operation. If the argument is not
296 -- a N_String_Literal node, then the call has no effect.
298 procedure Set_Slice_Subtype (N : Node_Id);
299 -- Build subtype of array type, with the range specified by the slice
301 procedure Simplify_Type_Conversion (N : Node_Id);
302 -- Called after N has been resolved and evaluated, but before range checks
303 -- have been applied. This rewrites the conversion into a simpler form.
305 function Try_User_Defined_Literal
306 (N : Node_Id;
307 Typ : Entity_Id) return Boolean;
308 -- If the node is a literal or a named number or a conditional expression
309 -- whose dependent expressions are all literals or named numbers, and the
310 -- context type has a user-defined literal aspect, then rewrite the node
311 -- or its leaf nodes as calls to the corresponding function, which plays
312 -- the role of an implicit conversion.
314 function Try_User_Defined_Literal_For_Operator
315 (N : Node_Id;
316 Typ : Entity_Id) return Boolean;
317 -- If an operator node has a literal operand, check whether the type of the
318 -- context, or that of the other operand has a user-defined literal aspect
319 -- that can be applied to the literal to resolve the node. If such aspect
320 -- exists, replace literal with a call to the corresponding function and
321 -- return True, return false otherwise.
323 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
324 -- A universal_fixed expression in an universal context is unambiguous if
325 -- there is only one applicable fixed point type. Determining whether there
326 -- is only one requires a search over all visible entities, and happens
327 -- only in very pathological cases (see 6115-006).
329 -------------------------
330 -- Ambiguous_Character --
331 -------------------------
333 procedure Ambiguous_Character (C : Node_Id) is
334 E : Entity_Id;
336 begin
337 if Nkind (C) = N_Character_Literal then
338 Error_Msg_N ("ambiguous character literal", C);
340 -- First the ones in Standard
342 Error_Msg_N ("\\possible interpretation: Character!", C);
343 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
345 -- Include Wide_Wide_Character in Ada 2005 mode
347 if Ada_Version >= Ada_2005 then
348 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
349 end if;
351 -- Now any other types that match
353 E := Current_Entity (C);
354 while Present (E) loop
355 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
356 E := Homonym (E);
357 end loop;
358 end if;
359 end Ambiguous_Character;
361 -------------------------
362 -- Analyze_And_Resolve --
363 -------------------------
365 procedure Analyze_And_Resolve (N : Node_Id) is
366 begin
367 Analyze (N);
368 Resolve (N);
369 end Analyze_And_Resolve;
371 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
372 begin
373 Analyze (N);
374 Resolve (N, Typ);
375 end Analyze_And_Resolve;
377 -- Versions with check(s) suppressed
379 procedure Analyze_And_Resolve
380 (N : Node_Id;
381 Typ : Entity_Id;
382 Suppress : Check_Id)
384 Scop : constant Entity_Id := Current_Scope;
386 begin
387 if Suppress = All_Checks then
388 declare
389 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
390 begin
391 Scope_Suppress.Suppress := (others => True);
392 Analyze_And_Resolve (N, Typ);
393 Scope_Suppress.Suppress := Sva;
394 end;
396 else
397 declare
398 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
399 begin
400 Scope_Suppress.Suppress (Suppress) := True;
401 Analyze_And_Resolve (N, Typ);
402 Scope_Suppress.Suppress (Suppress) := Svg;
403 end;
404 end if;
406 if Current_Scope /= Scop
407 and then Scope_Is_Transient
408 then
409 -- This can only happen if a transient scope was created for an inner
410 -- expression, which will be removed upon completion of the analysis
411 -- of an enclosing construct. The transient scope must have the
412 -- suppress status of the enclosing environment, not of this Analyze
413 -- call.
415 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
416 Scope_Suppress;
417 end if;
418 end Analyze_And_Resolve;
420 procedure Analyze_And_Resolve
421 (N : Node_Id;
422 Suppress : Check_Id)
424 Scop : constant Entity_Id := Current_Scope;
426 begin
427 if Suppress = All_Checks then
428 declare
429 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
430 begin
431 Scope_Suppress.Suppress := (others => True);
432 Analyze_And_Resolve (N);
433 Scope_Suppress.Suppress := Sva;
434 end;
436 else
437 declare
438 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
439 begin
440 Scope_Suppress.Suppress (Suppress) := True;
441 Analyze_And_Resolve (N);
442 Scope_Suppress.Suppress (Suppress) := Svg;
443 end;
444 end if;
446 if Current_Scope /= Scop and then Scope_Is_Transient then
447 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
448 Scope_Suppress;
449 end if;
450 end Analyze_And_Resolve;
452 -------------------------------------
453 -- Has_Applicable_User_Defined_Literal --
454 -------------------------------------
456 function Has_Applicable_User_Defined_Literal
457 (N : Node_Id;
458 Typ : Entity_Id) return Boolean
460 Loc : constant Source_Ptr := Sloc (N);
461 Literal_Aspect_Map :
462 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
463 (N_Integer_Literal => Aspect_Integer_Literal,
464 N_Interpolated_String_Literal => No_Aspect,
465 N_Real_Literal => Aspect_Real_Literal,
466 N_String_Literal => Aspect_String_Literal);
468 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
469 (E_Named_Integer => Aspect_Integer_Literal,
470 E_Named_Real => Aspect_Real_Literal);
472 Lit_Aspect : Aspect_Id;
474 Callee : Entity_Id;
475 Name : Node_Id;
476 Param1 : Node_Id;
477 Param2 : Node_Id;
478 Params : List_Id;
479 Call : Node_Id;
480 Expr : Node_Id;
482 begin
483 if (Nkind (N) in N_Numeric_Or_String_Literal
484 and then Present
485 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
486 or else
487 (Nkind (N) = N_Identifier
488 and then Is_Named_Number (Entity (N))
489 and then
490 Present
491 (Find_Aspect
492 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
493 then
494 Lit_Aspect :=
495 (if Nkind (N) = N_Identifier
496 then Named_Number_Aspect_Map (Ekind (Entity (N)))
497 else Literal_Aspect_Map (Nkind (N)));
498 Callee :=
499 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
500 Name := Make_Identifier (Loc, Chars (Callee));
502 if Is_Derived_Type (Typ)
503 and then Base_Type (Etype (Callee)) /= Base_Type (Typ)
504 then
505 Callee :=
506 Corresponding_Primitive_Op
507 (Ancestor_Op => Callee,
508 Descendant_Type => Base_Type (Typ));
509 end if;
511 -- Handle an identifier that denotes a named number.
513 if Nkind (N) = N_Identifier then
514 Expr := Expression (Declaration_Node (Entity (N)));
516 if Ekind (Entity (N)) = E_Named_Integer then
517 UI_Image (Expr_Value (Expr), Decimal);
518 Start_String;
519 Store_String_Chars
520 (UI_Image_Buffer (1 .. UI_Image_Length));
521 Param1 := Make_String_Literal (Loc, End_String);
522 Params := New_List (Param1);
524 else
525 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
526 Start_String;
528 if UR_Is_Negative (Expr_Value_R (Expr)) then
529 Store_String_Chars ("-");
530 end if;
532 Store_String_Chars
533 (UI_Image_Buffer (1 .. UI_Image_Length));
534 Param1 := Make_String_Literal (Loc, End_String);
536 -- Note: Set_Etype is called below on Param1
538 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
539 Start_String;
540 Store_String_Chars
541 (UI_Image_Buffer (1 .. UI_Image_Length));
542 Param2 := Make_String_Literal (Loc, End_String);
543 Set_Etype (Param2, Standard_String);
545 Params := New_List (Param1, Param2);
547 if Present (Related_Expression (Callee)) then
548 Callee := Related_Expression (Callee);
549 else
550 Error_Msg_NE
551 ("cannot resolve & for a named real", N, Callee);
552 return False;
553 end if;
554 end if;
556 elsif Nkind (N) = N_String_Literal then
557 Param1 := Make_String_Literal (Loc, Strval (N));
558 Params := New_List (Param1);
560 else
561 Param1 :=
562 Make_String_Literal
563 (Loc, String_From_Numeric_Literal (N));
564 Params := New_List (Param1);
565 end if;
567 Call :=
568 Make_Function_Call
569 (Sloc => Loc,
570 Name => Name,
571 Parameter_Associations => Params);
573 Set_Entity (Name, Callee);
574 Set_Is_Overloaded (Name, False);
576 if Lit_Aspect = Aspect_String_Literal then
577 Set_Etype (Param1, Standard_Wide_Wide_String);
578 else
579 Set_Etype (Param1, Standard_String);
580 end if;
582 Set_Etype (Call, Etype (Callee));
584 -- Conversion not needed if the result type of the call is class-wide
585 -- or if the result type matches the context type.
587 if not Is_Class_Wide_Type (Typ)
588 and then Base_Type (Etype (Call)) /= Base_Type (Typ)
589 then
590 -- Conversion may be needed in case of an inherited
591 -- aspect of a derived type. For a null extension, we
592 -- use a null extension aggregate instead because the
593 -- downward type conversion would be illegal.
595 if Is_Null_Extension_Of
596 (Descendant => Typ,
597 Ancestor => Etype (Call))
598 then
599 Call := Make_Extension_Aggregate (Loc,
600 Ancestor_Part => Call,
601 Null_Record_Present => True);
602 else
603 Call := Convert_To (Typ, Call);
604 end if;
605 end if;
607 Rewrite (N, Call);
609 Analyze_And_Resolve (N, Typ);
610 return True;
612 else
613 return False;
614 end if;
615 end Has_Applicable_User_Defined_Literal;
617 ----------------------------
618 -- Check_Discriminant_Use --
619 ----------------------------
621 procedure Check_Discriminant_Use (N : Node_Id) is
622 PN : constant Node_Id := Parent (N);
623 Disc : constant Entity_Id := Entity (N);
624 P : Node_Id;
625 D : Node_Id;
627 begin
628 -- Any use in a spec-expression is legal
630 if In_Spec_Expression then
631 null;
633 elsif Nkind (PN) = N_Range then
635 -- Discriminant cannot be used to constrain a scalar type
637 P := Parent (PN);
639 if Nkind (P) = N_Range_Constraint
640 and then Nkind (Parent (P)) = N_Subtype_Indication
641 and then Nkind (Parent (Parent (P))) = N_Component_Definition
642 then
643 Error_Msg_N ("discriminant cannot constrain scalar type", N);
645 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
647 -- The following check catches the unusual case where a
648 -- discriminant appears within an index constraint that is part
649 -- of a larger expression within a constraint on a component,
650 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
651 -- check case of record components, and note that a similar check
652 -- should also apply in the case of discriminant constraints
653 -- below. ???
655 -- Note that the check for N_Subtype_Declaration below is to
656 -- detect the valid use of discriminants in the constraints of a
657 -- subtype declaration when this subtype declaration appears
658 -- inside the scope of a record type (which is syntactically
659 -- illegal, but which may be created as part of derived type
660 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
661 -- for more info.
663 if Ekind (Current_Scope) = E_Record_Type
664 and then Scope (Disc) = Current_Scope
665 and then not
666 (Nkind (Parent (P)) = N_Subtype_Indication
667 and then
668 Nkind (Parent (Parent (P))) in N_Component_Definition
669 | N_Subtype_Declaration
670 and then Paren_Count (N) = 0)
671 then
672 Error_Msg_N
673 ("discriminant must appear alone in component constraint", N);
674 return;
675 end if;
677 -- Detect a common error:
679 -- type R (D : Positive := 100) is record
680 -- Name : String (1 .. D);
681 -- end record;
683 -- The default value causes an object of type R to be allocated
684 -- with room for Positive'Last characters. The RM does not mandate
685 -- the allocation of the maximum size, but that is what GNAT does
686 -- so we should warn the programmer that there is a problem.
688 Check_Large : declare
689 SI : Node_Id;
690 T : Entity_Id;
691 TB : Node_Id;
692 CB : Entity_Id;
694 function Large_Storage_Type (T : Entity_Id) return Boolean;
695 -- Return True if type T has a large enough range that any
696 -- array whose index type covered the whole range of the type
697 -- would likely raise Storage_Error.
699 ------------------------
700 -- Large_Storage_Type --
701 ------------------------
703 function Large_Storage_Type (T : Entity_Id) return Boolean is
704 begin
705 -- The type is considered large if its bounds are known at
706 -- compile time and if it requires at least as many bits as
707 -- a Positive to store the possible values.
709 return Compile_Time_Known_Value (Type_Low_Bound (T))
710 and then Compile_Time_Known_Value (Type_High_Bound (T))
711 and then
712 Minimum_Size (T, Biased => True) >=
713 RM_Size (Standard_Positive);
714 end Large_Storage_Type;
716 -- Start of processing for Check_Large
718 begin
719 -- Check that the Disc has a large range
721 if not Large_Storage_Type (Etype (Disc)) then
722 goto No_Danger;
723 end if;
725 -- If the enclosing type is limited, we allocate only the
726 -- default value, not the maximum, and there is no need for
727 -- a warning.
729 if Is_Limited_Type (Scope (Disc)) then
730 goto No_Danger;
731 end if;
733 -- Check that it is the high bound
735 if N /= High_Bound (PN)
736 or else No (Discriminant_Default_Value (Disc))
737 then
738 goto No_Danger;
739 end if;
741 -- Check the array allows a large range at this bound. First
742 -- find the array
744 SI := Parent (P);
746 if Nkind (SI) /= N_Subtype_Indication then
747 goto No_Danger;
748 end if;
750 T := Entity (Subtype_Mark (SI));
752 if not Is_Array_Type (T) then
753 goto No_Danger;
754 end if;
756 -- Next, find the dimension
758 TB := First_Index (T);
759 CB := First (Constraints (P));
760 while True
761 and then Present (TB)
762 and then Present (CB)
763 and then CB /= PN
764 loop
765 Next_Index (TB);
766 Next (CB);
767 end loop;
769 if CB /= PN then
770 goto No_Danger;
771 end if;
773 -- Now, check the dimension has a large range
775 if not Large_Storage_Type (Etype (TB)) then
776 goto No_Danger;
777 end if;
779 -- Warn about the danger
781 Error_Msg_N
782 ("??creation of & object may raise Storage_Error!",
783 Scope (Disc));
785 <<No_Danger>>
786 null;
788 end Check_Large;
789 end if;
791 -- Legal case is in index or discriminant constraint
793 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
794 | N_Discriminant_Association
795 then
796 if Paren_Count (N) > 0 then
797 Error_Msg_N
798 ("discriminant in constraint must appear alone", N);
800 elsif Nkind (N) = N_Expanded_Name
801 and then Comes_From_Source (N)
802 then
803 Error_Msg_N
804 ("discriminant must appear alone as a direct name", N);
805 end if;
807 return;
809 -- Otherwise, context is an expression. It should not be within (i.e. a
810 -- subexpression of) a constraint for a component.
812 else
813 D := PN;
814 P := Parent (PN);
815 while Nkind (P) not in
816 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
817 loop
818 D := P;
819 P := Parent (P);
820 exit when No (P);
821 end loop;
823 -- If the discriminant is used in an expression that is a bound of a
824 -- scalar type, an Itype is created and the bounds are attached to
825 -- its range, not to the original subtype indication. Such use is of
826 -- course a double fault.
828 if (Nkind (P) = N_Subtype_Indication
829 and then Nkind (Parent (P)) in N_Component_Definition
830 | N_Derived_Type_Definition
831 and then D = Constraint (P))
833 -- The constraint itself may be given by a subtype indication,
834 -- rather than by a more common discrete range.
836 or else (Nkind (P) = N_Subtype_Indication
837 and then
838 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
839 or else Nkind (P) = N_Entry_Declaration
840 or else Nkind (D) = N_Defining_Identifier
841 then
842 Error_Msg_N
843 ("discriminant in constraint must appear alone", N);
844 end if;
845 end if;
846 end Check_Discriminant_Use;
848 --------------------------------
849 -- Check_For_Visible_Operator --
850 --------------------------------
852 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
853 begin
854 if Comes_From_Source (N)
855 and then not Is_Visible_Operator (Original_Node (N), T)
856 and then not Error_Posted (N)
857 then
858 Error_Msg_NE -- CODEFIX
859 ("operator for} is not directly visible!", N, First_Subtype (T));
860 Error_Msg_N -- CODEFIX
861 ("use clause would make operation legal!", N);
862 end if;
863 end Check_For_Visible_Operator;
865 ---------------------------------
866 -- Check_Fully_Declared_Prefix --
867 ---------------------------------
869 procedure Check_Fully_Declared_Prefix
870 (Typ : Entity_Id;
871 Pref : Node_Id)
873 begin
874 -- Check that the designated type of the prefix of a dereference is
875 -- not an incomplete type. This cannot be done unconditionally, because
876 -- dereferences of private types are legal in default expressions. This
877 -- case is taken care of in Check_Fully_Declared, called below. There
878 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
880 -- This consideration also applies to similar checks for allocators,
881 -- qualified expressions, and type conversions.
883 -- An additional exception concerns other per-object expressions that
884 -- are not directly related to component declarations, in particular
885 -- representation pragmas for tasks. These will be per-object
886 -- expressions if they depend on discriminants or some global entity.
887 -- If the task has access discriminants, the designated type may be
888 -- incomplete at the point the expression is resolved. This resolution
889 -- takes place within the body of the initialization procedure, where
890 -- the discriminant is replaced by its discriminal.
892 if Is_Entity_Name (Pref)
893 and then Ekind (Entity (Pref)) = E_In_Parameter
894 then
895 null;
897 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
898 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
899 -- Analyze_Object_Renaming, and Freeze_Entity.
901 elsif Ada_Version >= Ada_2005
902 and then Is_Entity_Name (Pref)
903 and then Is_Access_Type (Etype (Pref))
904 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
905 E_Incomplete_Type
906 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
907 then
908 null;
909 else
910 Check_Fully_Declared (Typ, Parent (Pref));
911 end if;
912 end Check_Fully_Declared_Prefix;
914 ------------------------------
915 -- Check_Infinite_Recursion --
916 ------------------------------
918 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
919 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
920 -- Determine whether call N invokes the related enclosing subprogram
921 -- with actuals that differ from the subprogram's formals.
923 function Is_Conditional_Statement (N : Node_Id) return Boolean;
924 -- Determine whether arbitrary node N denotes a conditional construct
926 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
927 -- Determine whether arbitrary node N denotes a control flow statement
928 -- or a construct that may contains such a statement.
930 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
931 -- Determine whether arbitrary node N appears immediately within the
932 -- statements of an entry or subprogram body.
934 function Is_Raise_Idiom (N : Node_Id) return Boolean;
935 -- Determine whether arbitrary node N appears immediately within the
936 -- body of an entry or subprogram, and is preceded by a single raise
937 -- statement.
939 function Is_Raise_Statement (N : Node_Id) return Boolean;
940 -- Determine whether arbitrary node N denotes a raise statement
942 function Is_Sole_Statement (N : Node_Id) return Boolean;
943 -- Determine whether arbitrary node N is the sole source statement in
944 -- the body of the enclosing subprogram.
946 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
947 -- Determine whether arbitrary node N is preceded by a control flow
948 -- statement.
950 function Within_Conditional_Statement (N : Node_Id) return Boolean;
951 -- Determine whether arbitrary node N appears within a conditional
952 -- construct.
954 --------------------------------------
955 -- Invoked_With_Different_Arguments --
956 --------------------------------------
958 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
959 Subp : constant Entity_Id := Get_Called_Entity (N);
961 Actual : Node_Id;
962 Formal : Entity_Id;
964 begin
965 -- Determine whether the formals of the invoked subprogram are not
966 -- used as actuals in the call.
968 Actual := First_Actual (N);
969 Formal := First_Formal (Subp);
970 while Present (Actual) and then Present (Formal) loop
972 -- The current actual does not match the current formal
974 if not (Is_Entity_Name (Actual)
975 and then Entity (Actual) = Formal)
976 then
977 return True;
978 end if;
980 Next_Actual (Actual);
981 Next_Formal (Formal);
982 end loop;
984 return False;
985 end Invoked_With_Different_Arguments;
987 ------------------------------
988 -- Is_Conditional_Statement --
989 ------------------------------
991 function Is_Conditional_Statement (N : Node_Id) return Boolean is
992 begin
993 return
994 Nkind (N) in N_And_Then
995 | N_Case_Expression
996 | N_Case_Statement
997 | N_If_Expression
998 | N_If_Statement
999 | N_Or_Else;
1000 end Is_Conditional_Statement;
1002 -------------------------------
1003 -- Is_Control_Flow_Statement --
1004 -------------------------------
1006 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
1007 begin
1008 -- It is assumed that all statements may affect the control flow in
1009 -- some way. A raise statement may be expanded into a non-statement
1010 -- node.
1012 return Is_Statement (N) or else Is_Raise_Statement (N);
1013 end Is_Control_Flow_Statement;
1015 --------------------------------
1016 -- Is_Immediately_Within_Body --
1017 --------------------------------
1019 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
1020 HSS : constant Node_Id := Parent (N);
1022 begin
1023 return
1024 Nkind (HSS) = N_Handled_Sequence_Of_Statements
1025 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
1026 and then Is_List_Member (N)
1027 and then List_Containing (N) = Statements (HSS);
1028 end Is_Immediately_Within_Body;
1030 --------------------
1031 -- Is_Raise_Idiom --
1032 --------------------
1034 function Is_Raise_Idiom (N : Node_Id) return Boolean is
1035 Raise_Stmt : Node_Id;
1036 Stmt : Node_Id;
1038 begin
1039 if Is_Immediately_Within_Body (N) then
1041 -- Assume that no raise statement has been seen yet
1043 Raise_Stmt := Empty;
1045 -- Examine the statements preceding the input node, skipping
1046 -- internally-generated constructs.
1048 Stmt := Prev (N);
1049 while Present (Stmt) loop
1051 -- Multiple raise statements violate the idiom
1053 if Is_Raise_Statement (Stmt) then
1054 if Present (Raise_Stmt) then
1055 return False;
1056 end if;
1058 Raise_Stmt := Stmt;
1060 elsif Comes_From_Source (Stmt) then
1061 exit;
1062 end if;
1064 Stmt := Prev (Stmt);
1065 end loop;
1067 -- At this point the node must be preceded by a raise statement,
1068 -- and the raise statement has to be the sole statement within
1069 -- the enclosing entry or subprogram body.
1071 return
1072 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
1073 end if;
1075 return False;
1076 end Is_Raise_Idiom;
1078 ------------------------
1079 -- Is_Raise_Statement --
1080 ------------------------
1082 function Is_Raise_Statement (N : Node_Id) return Boolean is
1083 begin
1084 -- A raise statement may be transfomed into a Raise_xxx_Error node
1086 return
1087 Nkind (N) = N_Raise_Statement
1088 or else Nkind (N) in N_Raise_xxx_Error;
1089 end Is_Raise_Statement;
1091 -----------------------
1092 -- Is_Sole_Statement --
1093 -----------------------
1095 function Is_Sole_Statement (N : Node_Id) return Boolean is
1096 Stmt : Node_Id;
1098 begin
1099 -- The input node appears within the statements of an entry or
1100 -- subprogram body. Examine the statements preceding the node.
1102 if Is_Immediately_Within_Body (N) then
1103 Stmt := Prev (N);
1105 while Present (Stmt) loop
1107 -- The statement is preceded by another statement or a source
1108 -- construct. This indicates that the node does not appear by
1109 -- itself.
1111 if Is_Control_Flow_Statement (Stmt)
1112 or else Comes_From_Source (Stmt)
1113 then
1114 return False;
1115 end if;
1117 Stmt := Prev (Stmt);
1118 end loop;
1120 return True;
1121 end if;
1123 -- The input node is within a construct nested inside the entry or
1124 -- subprogram body.
1126 return False;
1127 end Is_Sole_Statement;
1129 ----------------------------------------
1130 -- Preceded_By_Control_Flow_Statement --
1131 ----------------------------------------
1133 function Preceded_By_Control_Flow_Statement
1134 (N : Node_Id) return Boolean
1136 Stmt : Node_Id;
1138 begin
1139 if Is_List_Member (N) then
1140 Stmt := Prev (N);
1142 -- Examine the statements preceding the input node
1144 while Present (Stmt) loop
1145 if Is_Control_Flow_Statement (Stmt) then
1146 return True;
1147 end if;
1149 Stmt := Prev (Stmt);
1150 end loop;
1152 return False;
1153 end if;
1155 -- Assume that the node is part of some control flow statement
1157 return True;
1158 end Preceded_By_Control_Flow_Statement;
1160 ----------------------------------
1161 -- Within_Conditional_Statement --
1162 ----------------------------------
1164 function Within_Conditional_Statement (N : Node_Id) return Boolean is
1165 Stmt : Node_Id;
1167 begin
1168 Stmt := Parent (N);
1169 while Present (Stmt) loop
1170 if Is_Conditional_Statement (Stmt) then
1171 return True;
1173 -- Prevent the search from going too far
1175 elsif Is_Body_Or_Package_Declaration (Stmt) then
1176 exit;
1177 end if;
1179 Stmt := Parent (Stmt);
1180 end loop;
1182 return False;
1183 end Within_Conditional_Statement;
1185 -- Local variables
1187 Call_Context : constant Node_Id :=
1188 Enclosing_Declaration_Or_Statement (Call);
1190 -- Start of processing for Check_Infinite_Recursion
1192 begin
1193 -- The call is assumed to be safe when the enclosing subprogram is
1194 -- invoked with actuals other than its formals.
1196 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1197 -- begin
1198 -- ...
1199 -- Proc (A1, A2, ..., AN);
1200 -- ...
1201 -- end Proc;
1203 if Invoked_With_Different_Arguments (Call) then
1204 return False;
1206 -- The call is assumed to be safe when the invocation of the enclosing
1207 -- subprogram depends on a conditional statement.
1209 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1210 -- begin
1211 -- ...
1212 -- if Some_Condition then
1213 -- Proc (F1, F2, ..., FN);
1214 -- end if;
1215 -- ...
1216 -- end Proc;
1218 elsif Within_Conditional_Statement (Call) then
1219 return False;
1221 -- The context of the call is assumed to be safe when the invocation of
1222 -- the enclosing subprogram is preceded by some control flow statement.
1224 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1225 -- begin
1226 -- ...
1227 -- if Some_Condition then
1228 -- ...
1229 -- end if;
1230 -- ...
1231 -- Proc (F1, F2, ..., FN);
1232 -- ...
1233 -- end Proc;
1235 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1236 return False;
1238 -- Detect an idiom where the context of the call is preceded by a single
1239 -- raise statement.
1241 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1242 -- begin
1243 -- raise ...;
1244 -- Proc (F1, F2, ..., FN);
1245 -- end Proc;
1247 elsif Is_Raise_Idiom (Call_Context) then
1248 return False;
1249 end if;
1251 -- At this point it is certain that infinite recursion will take place
1252 -- as long as the call is executed. Detect a case where the context of
1253 -- the call is the sole source statement within the subprogram body.
1255 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1256 -- begin
1257 -- Proc (F1, F2, ..., FN);
1258 -- end Proc;
1260 -- Install an explicit raise to prevent the infinite recursion.
1262 if Is_Sole_Statement (Call_Context) then
1263 Error_Msg_Warn := SPARK_Mode /= On;
1264 Error_Msg_N ("!infinite recursion<<", Call);
1265 Error_Msg_N ("\!Storage_Error [<<", Call);
1267 Insert_Action (Call,
1268 Make_Raise_Storage_Error (Sloc (Call),
1269 Reason => SE_Infinite_Recursion));
1271 -- Otherwise infinite recursion could take place, considering other flow
1272 -- control constructs such as gotos, exit statements, etc.
1274 else
1275 Error_Msg_Warn := SPARK_Mode /= On;
1276 Error_Msg_N ("!possible infinite recursion<<", Call);
1277 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1278 end if;
1280 return True;
1281 end Check_Infinite_Recursion;
1283 ---------------------------------------
1284 -- Check_No_Direct_Boolean_Operators --
1285 ---------------------------------------
1287 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1288 begin
1289 if Scope (Entity (N)) = Standard_Standard
1290 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1291 then
1292 -- Restriction only applies to original source code
1294 if Comes_From_Source (N) then
1295 Check_Restriction (No_Direct_Boolean_Operators, N);
1296 end if;
1297 end if;
1299 -- Do style check (but skip if in instance, error is on template)
1301 if Style_Check then
1302 if not In_Instance then
1303 Check_Boolean_Operator (N);
1304 end if;
1305 end if;
1306 end Check_No_Direct_Boolean_Operators;
1308 ------------------------------
1309 -- Check_Parameterless_Call --
1310 ------------------------------
1312 procedure Check_Parameterless_Call (N : Node_Id) is
1313 Nam : Node_Id;
1315 function Prefix_Is_Access_Subp return Boolean;
1316 -- If the prefix is of an access_to_subprogram type, the node must be
1317 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1318 -- interpretations are access to subprograms.
1320 ---------------------------
1321 -- Prefix_Is_Access_Subp --
1322 ---------------------------
1324 function Prefix_Is_Access_Subp return Boolean is
1325 I : Interp_Index;
1326 It : Interp;
1328 begin
1329 -- If the context is an attribute reference that can apply to
1330 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1332 if Nkind (Parent (N)) = N_Attribute_Reference
1333 and then Attribute_Name (Parent (N))
1334 in Name_Address | Name_Code_Address | Name_Access
1335 then
1336 return False;
1337 end if;
1339 if not Is_Overloaded (N) then
1340 return
1341 Ekind (Etype (N)) = E_Subprogram_Type
1342 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1343 else
1344 Get_First_Interp (N, I, It);
1345 while Present (It.Typ) loop
1346 if Ekind (It.Typ) /= E_Subprogram_Type
1347 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1348 then
1349 return False;
1350 end if;
1352 Get_Next_Interp (I, It);
1353 end loop;
1355 return True;
1356 end if;
1357 end Prefix_Is_Access_Subp;
1359 -- Start of processing for Check_Parameterless_Call
1361 begin
1362 -- Defend against junk stuff if errors already detected
1364 if Total_Errors_Detected /= 0 then
1365 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1366 return;
1367 elsif Nkind (N) in N_Has_Chars
1368 and then not Is_Valid_Name (Chars (N))
1369 then
1370 return;
1371 end if;
1373 Require_Entity (N);
1374 end if;
1376 -- If the context expects a value, and the name is a procedure, this is
1377 -- most likely a missing 'Access. Don't try to resolve the parameterless
1378 -- call, error will be caught when the outer call is analyzed.
1380 if Is_Entity_Name (N)
1381 and then Ekind (Entity (N)) = E_Procedure
1382 and then not Is_Overloaded (N)
1383 and then
1384 Nkind (Parent (N)) in N_Parameter_Association
1385 | N_Function_Call
1386 | N_Procedure_Call_Statement
1387 then
1388 return;
1389 end if;
1391 -- Rewrite as call if overloadable entity that is (or could be, in the
1392 -- overloaded case) a function call. If we know for sure that the entity
1393 -- is an enumeration literal, we do not rewrite it.
1395 -- If the entity is the name of an operator, it cannot be a call because
1396 -- operators cannot have default parameters. In this case, this must be
1397 -- a string whose contents coincide with an operator name. Set the kind
1398 -- of the node appropriately.
1400 if (Is_Entity_Name (N)
1401 and then Nkind (N) /= N_Operator_Symbol
1402 and then Is_Overloadable (Entity (N))
1403 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1404 or else Is_Overloaded (N)))
1406 -- Rewrite as call if it is an explicit dereference of an expression of
1407 -- a subprogram access type, and the subprogram type is not that of a
1408 -- procedure or entry.
1410 or else
1411 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1413 -- Rewrite as call if it is a selected component which is a function,
1414 -- this is the case of a call to a protected function (which may be
1415 -- overloaded with other protected operations).
1417 or else
1418 (Nkind (N) = N_Selected_Component
1419 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1420 or else
1421 (Ekind (Entity (Selector_Name (N))) in
1422 E_Entry | E_Procedure
1423 and then Is_Overloaded (Selector_Name (N)))))
1425 -- If one of the above three conditions is met, rewrite as call. Apply
1426 -- the rewriting only once.
1428 then
1429 if Nkind (Parent (N)) /= N_Function_Call
1430 or else N /= Name (Parent (N))
1431 then
1433 -- This may be a prefixed call that was not fully analyzed, e.g.
1434 -- an actual in an instance.
1436 if Ada_Version >= Ada_2005
1437 and then Nkind (N) = N_Selected_Component
1438 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1439 then
1440 Analyze_Selected_Component (N);
1442 if Nkind (N) /= N_Selected_Component then
1443 return;
1444 end if;
1445 end if;
1447 -- The node is the name of the parameterless call. Preserve its
1448 -- descendants, which may be complex expressions.
1450 Nam := Relocate_Node (N);
1452 -- If overloaded, overload set belongs to new copy
1454 Save_Interps (N, Nam);
1456 -- Change node to parameterless function call (note that the
1457 -- Parameter_Associations associations field is left set to Empty,
1458 -- its normal default value since there are no parameters)
1460 Change_Node (N, N_Function_Call);
1461 Set_Name (N, Nam);
1462 Set_Sloc (N, Sloc (Nam));
1463 Analyze_Call (N);
1464 end if;
1466 elsif Nkind (N) = N_Parameter_Association then
1467 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1469 elsif Nkind (N) = N_Operator_Symbol then
1470 Set_Etype (N, Empty);
1471 Set_Entity (N, Empty);
1472 Set_Is_Overloaded (N, False);
1473 Change_Operator_Symbol_To_String_Literal (N);
1474 Set_Etype (N, Any_String);
1475 end if;
1476 end Check_Parameterless_Call;
1478 --------------------------------
1479 -- Is_Atomic_Ref_With_Address --
1480 --------------------------------
1482 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1483 Pref : constant Node_Id := Prefix (N);
1485 begin
1486 if not Is_Entity_Name (Pref) then
1487 return False;
1489 else
1490 declare
1491 Pent : constant Entity_Id := Entity (Pref);
1492 Ptyp : constant Entity_Id := Etype (Pent);
1493 begin
1494 return not Is_Access_Type (Ptyp)
1495 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1496 and then Present (Address_Clause (Pent));
1497 end;
1498 end if;
1499 end Is_Atomic_Ref_With_Address;
1501 -----------------------------
1502 -- Is_Definite_Access_Type --
1503 -----------------------------
1505 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean is
1506 Btyp : constant Entity_Id := Base_Type (E);
1507 begin
1508 return Ekind (Btyp) = E_Access_Type
1509 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1510 and then Comes_From_Source (Btyp));
1511 end Is_Definite_Access_Type;
1513 ----------------------
1514 -- Is_Predefined_Op --
1515 ----------------------
1517 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1518 begin
1519 -- Predefined operators are intrinsic subprograms
1521 if not Is_Intrinsic_Subprogram (Nam) then
1522 return False;
1523 end if;
1525 -- A call to a back-end builtin is never a predefined operator
1527 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1528 return False;
1529 end if;
1531 return not Is_Generic_Instance (Nam)
1532 and then Chars (Nam) in Any_Operator_Name
1533 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1534 end Is_Predefined_Op;
1536 -----------------------------
1537 -- Make_Call_Into_Operator --
1538 -----------------------------
1540 procedure Make_Call_Into_Operator
1541 (N : Node_Id;
1542 Typ : Entity_Id;
1543 Op_Id : Entity_Id)
1545 Op_Name : constant Name_Id := Chars (Op_Id);
1546 Act1 : Node_Id := First_Actual (N);
1547 Act2 : Node_Id := Next_Actual (Act1);
1548 Error : Boolean := False;
1549 Func : constant Entity_Id := Entity (Name (N));
1550 Is_Binary : constant Boolean := Present (Act2);
1551 Op_Node : Node_Id;
1552 Opnd_Type : Entity_Id := Empty;
1553 Orig_Type : Entity_Id := Empty;
1554 Pack : Entity_Id;
1556 type Kind_Test is access function (E : N_Entity_Id) return Boolean;
1558 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1559 -- If the operand is not universal, and the operator is given by an
1560 -- expanded name, verify that the operand has an interpretation with a
1561 -- type defined in the given scope of the operator.
1563 function Type_In_P (Test : Kind_Test) return Entity_Id;
1564 -- Find a type of the given class in package Pack that contains the
1565 -- operator.
1567 ---------------------------
1568 -- Operand_Type_In_Scope --
1569 ---------------------------
1571 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1572 Nod : constant Node_Id := Right_Opnd (Op_Node);
1573 I : Interp_Index;
1574 It : Interp;
1576 begin
1577 if not Is_Overloaded (Nod) then
1578 return Scope (Base_Type (Etype (Nod))) = S;
1580 else
1581 Get_First_Interp (Nod, I, It);
1582 while Present (It.Typ) loop
1583 if Scope (Base_Type (It.Typ)) = S then
1584 return True;
1585 end if;
1587 Get_Next_Interp (I, It);
1588 end loop;
1590 return False;
1591 end if;
1592 end Operand_Type_In_Scope;
1594 ---------------
1595 -- Type_In_P --
1596 ---------------
1598 function Type_In_P (Test : Kind_Test) return Entity_Id is
1599 E : Entity_Id;
1601 function In_Decl return Boolean;
1602 -- Verify that node is not part of the type declaration for the
1603 -- candidate type, which would otherwise be invisible.
1605 -------------
1606 -- In_Decl --
1607 -------------
1609 function In_Decl return Boolean is
1610 Decl_Node : constant Node_Id := Parent (E);
1611 N2 : Node_Id;
1613 begin
1614 N2 := N;
1616 if Etype (E) = Any_Type then
1617 return True;
1619 elsif No (Decl_Node) then
1620 return False;
1622 else
1623 while Present (N2)
1624 and then Nkind (N2) /= N_Compilation_Unit
1625 loop
1626 if N2 = Decl_Node then
1627 return True;
1628 else
1629 N2 := Parent (N2);
1630 end if;
1631 end loop;
1633 return False;
1634 end if;
1635 end In_Decl;
1637 -- Start of processing for Type_In_P
1639 begin
1640 -- If the context type is declared in the prefix package, this is the
1641 -- desired base type.
1643 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1644 return Base_Type (Typ);
1646 else
1647 E := First_Entity (Pack);
1648 while Present (E) loop
1649 if Test (E) and then not In_Decl then
1650 return E;
1651 end if;
1653 Next_Entity (E);
1654 end loop;
1656 return Empty;
1657 end if;
1658 end Type_In_P;
1660 -- Start of processing for Make_Call_Into_Operator
1662 begin
1663 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1665 -- Preserve the Comes_From_Source flag on the result if the original
1666 -- call came from source. Although it is not strictly the case that the
1667 -- operator as such comes from the source, logically it corresponds
1668 -- exactly to the function call in the source, so it should be marked
1669 -- this way (e.g. to make sure that validity checks work fine).
1671 Preserve_Comes_From_Source (Op_Node, N);
1673 -- Ensure that the corresponding operator has the same parent as the
1674 -- original call. This guarantees that parent traversals performed by
1675 -- the ABE mechanism succeed.
1677 Set_Parent (Op_Node, Parent (N));
1679 -- Binary operator
1681 if Is_Binary then
1682 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1683 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1684 Save_Interps (Act1, Left_Opnd (Op_Node));
1685 Save_Interps (Act2, Right_Opnd (Op_Node));
1686 Act1 := Left_Opnd (Op_Node);
1687 Act2 := Right_Opnd (Op_Node);
1689 -- Unary operator
1691 else
1692 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1693 Save_Interps (Act1, Right_Opnd (Op_Node));
1694 Act1 := Right_Opnd (Op_Node);
1695 end if;
1697 -- If the operator is denoted by an expanded name, and the prefix is
1698 -- not Standard, but the operator is a predefined one whose scope is
1699 -- Standard, then this is an implicit_operator, inserted as an
1700 -- interpretation by the procedure of the same name. This procedure
1701 -- overestimates the presence of implicit operators, because it does
1702 -- not examine the type of the operands. Verify now that the operand
1703 -- type appears in the given scope. If right operand is universal,
1704 -- check the other operand. In the case of concatenation, either
1705 -- argument can be the component type, so check the type of the result.
1706 -- If both arguments are literals, look for a type of the right kind
1707 -- defined in the given scope. This elaborate nonsense is brought to
1708 -- you courtesy of b33302a. The type itself must be frozen, so we must
1709 -- find the type of the proper class in the given scope.
1711 -- A final wrinkle is the multiplication operator for fixed point types,
1712 -- which is defined in Standard only, and not in the scope of the
1713 -- fixed point type itself.
1715 if Nkind (Name (N)) = N_Expanded_Name then
1716 Pack := Entity (Prefix (Name (N)));
1718 -- If this is a package renaming, get renamed entity, which will be
1719 -- the scope of the operands if operaton is type-correct.
1721 if Present (Renamed_Entity (Pack)) then
1722 Pack := Renamed_Entity (Pack);
1723 end if;
1725 -- If the entity being called is defined in the given package, it is
1726 -- a renaming of a predefined operator, and known to be legal.
1728 if Scope (Entity (Name (N))) = Pack
1729 and then Pack /= Standard_Standard
1730 then
1731 null;
1733 -- Visibility does not need to be checked in an instance: if the
1734 -- operator was not visible in the generic it has been diagnosed
1735 -- already, else there is an implicit copy of it in the instance.
1737 elsif In_Instance then
1738 null;
1740 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1741 and then Is_Fixed_Point_Type (Etype (Act1))
1742 and then Is_Fixed_Point_Type (Etype (Act2))
1743 then
1744 if Pack /= Standard_Standard then
1745 Error := True;
1746 end if;
1748 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1749 -- available.
1751 elsif Ada_Version >= Ada_2005
1752 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1753 and then (Is_Anonymous_Access_Type (Etype (Act1))
1754 or else Is_Anonymous_Access_Type (Etype (Act2)))
1755 then
1756 null;
1758 else
1759 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1761 if Op_Name = Name_Op_Concat then
1762 Opnd_Type := Base_Type (Typ);
1764 elsif (Scope (Opnd_Type) = Standard_Standard
1765 and then Is_Binary)
1766 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1767 and then Is_Binary
1768 and then not Comes_From_Source (Opnd_Type))
1769 then
1770 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1771 end if;
1773 if Scope (Opnd_Type) = Standard_Standard then
1775 -- Verify that the scope contains a type that corresponds to
1776 -- the given literal. Optimize the case where Pack is Standard.
1778 if Pack /= Standard_Standard then
1779 if Opnd_Type = Universal_Integer then
1780 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1782 elsif Opnd_Type = Universal_Real then
1783 Orig_Type := Type_In_P (Is_Real_Type'Access);
1785 elsif Opnd_Type = Universal_Access then
1786 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1788 elsif Opnd_Type = Any_String then
1789 Orig_Type := Type_In_P (Is_String_Type'Access);
1791 elsif Opnd_Type = Any_Composite then
1792 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1794 if Present (Orig_Type) then
1795 if Has_Private_Component (Orig_Type) then
1796 Orig_Type := Empty;
1797 else
1798 Set_Etype (Act1, Orig_Type);
1800 if Is_Binary then
1801 Set_Etype (Act2, Orig_Type);
1802 end if;
1803 end if;
1804 end if;
1806 else
1807 Orig_Type := Empty;
1808 end if;
1810 Error := No (Orig_Type);
1811 end if;
1813 elsif Ekind (Opnd_Type) = E_Allocator_Type
1814 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1815 then
1816 Error := True;
1818 -- If the type is defined elsewhere, and the operator is not
1819 -- defined in the given scope (by a renaming declaration, e.g.)
1820 -- then this is an error as well. If an extension of System is
1821 -- present, and the type may be defined there, Pack must be
1822 -- System itself.
1824 elsif Scope (Opnd_Type) /= Pack
1825 and then Scope (Op_Id) /= Pack
1826 and then (No (System_Aux_Id)
1827 or else Scope (Opnd_Type) /= System_Aux_Id
1828 or else Pack /= Scope (System_Aux_Id))
1829 then
1830 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1831 Error := True;
1832 else
1833 Error := not Operand_Type_In_Scope (Pack);
1834 end if;
1836 elsif Pack = Standard_Standard
1837 and then not Operand_Type_In_Scope (Standard_Standard)
1838 then
1839 Error := True;
1840 end if;
1841 end if;
1843 if Error then
1844 Error_Msg_Node_2 := Pack;
1845 Error_Msg_NE
1846 ("& not declared in&", N, Selector_Name (Name (N)));
1847 Set_Etype (N, Any_Type);
1848 return;
1850 -- Detect a mismatch between the context type and the result type
1851 -- in the named package, which is otherwise not detected if the
1852 -- operands are universal. Check is only needed if source entity is
1853 -- an operator, not a function that renames an operator.
1855 elsif Nkind (Parent (N)) /= N_Type_Conversion
1856 and then Ekind (Entity (Name (N))) = E_Operator
1857 and then Is_Numeric_Type (Typ)
1858 and then not Is_Universal_Numeric_Type (Typ)
1859 and then Scope (Base_Type (Typ)) /= Pack
1860 and then not In_Instance
1861 then
1862 if Is_Fixed_Point_Type (Typ)
1863 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1864 then
1865 -- Already checked above
1867 null;
1869 -- Operator may be defined in an extension of System
1871 elsif Present (System_Aux_Id)
1872 and then Present (Opnd_Type)
1873 and then Scope (Opnd_Type) = System_Aux_Id
1874 then
1875 null;
1877 else
1878 -- Could we use Wrong_Type here??? (this would require setting
1879 -- Etype (N) to the actual type found where Typ was expected).
1881 Error_Msg_NE ("expect }", N, Typ);
1882 end if;
1883 end if;
1884 end if;
1886 Set_Chars (Op_Node, Op_Name);
1888 if not Is_Private_Type (Etype (N)) then
1889 Set_Etype (Op_Node, Base_Type (Etype (N)));
1890 else
1891 Set_Etype (Op_Node, Etype (N));
1892 end if;
1894 -- If this is a call to a function that renames a predefined equality,
1895 -- the renaming declaration provides a type that must be used to
1896 -- resolve the operands. This must be done now because resolution of
1897 -- the equality node will not resolve any remaining ambiguity, and it
1898 -- assumes that the first operand is not overloaded.
1900 if Op_Name in Name_Op_Eq | Name_Op_Ne
1901 and then Ekind (Func) = E_Function
1902 and then Is_Overloaded (Act1)
1903 then
1904 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1905 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1906 end if;
1908 Set_Entity (Op_Node, Op_Id);
1909 Generate_Reference (Op_Id, N, ' ');
1911 Rewrite (N, Op_Node);
1913 -- If this is an arithmetic operator and the result type is private,
1914 -- the operands and the result must be wrapped in conversion to
1915 -- expose the underlying numeric type and expand the proper checks,
1916 -- e.g. on division.
1918 if Is_Private_Type (Typ) then
1919 case Nkind (N) is
1920 when N_Op_Add
1921 | N_Op_Divide
1922 | N_Op_Expon
1923 | N_Op_Mod
1924 | N_Op_Multiply
1925 | N_Op_Rem
1926 | N_Op_Subtract
1928 Resolve_Intrinsic_Operator (N, Typ);
1930 when N_Op_Abs
1931 | N_Op_Minus
1932 | N_Op_Plus
1934 Resolve_Intrinsic_Unary_Operator (N, Typ);
1936 when others =>
1937 Resolve (N, Typ);
1938 end case;
1939 else
1940 Resolve (N, Typ);
1941 end if;
1942 end Make_Call_Into_Operator;
1944 -------------------
1945 -- Operator_Kind --
1946 -------------------
1948 function Operator_Kind
1949 (Op_Name : Name_Id;
1950 Is_Binary : Boolean) return Node_Kind
1952 Kind : Node_Kind;
1954 begin
1955 -- Use CASE statement or array???
1957 if Is_Binary then
1958 if Op_Name = Name_Op_And then
1959 Kind := N_Op_And;
1960 elsif Op_Name = Name_Op_Or then
1961 Kind := N_Op_Or;
1962 elsif Op_Name = Name_Op_Xor then
1963 Kind := N_Op_Xor;
1964 elsif Op_Name = Name_Op_Eq then
1965 Kind := N_Op_Eq;
1966 elsif Op_Name = Name_Op_Ne then
1967 Kind := N_Op_Ne;
1968 elsif Op_Name = Name_Op_Lt then
1969 Kind := N_Op_Lt;
1970 elsif Op_Name = Name_Op_Le then
1971 Kind := N_Op_Le;
1972 elsif Op_Name = Name_Op_Gt then
1973 Kind := N_Op_Gt;
1974 elsif Op_Name = Name_Op_Ge then
1975 Kind := N_Op_Ge;
1976 elsif Op_Name = Name_Op_Add then
1977 Kind := N_Op_Add;
1978 elsif Op_Name = Name_Op_Subtract then
1979 Kind := N_Op_Subtract;
1980 elsif Op_Name = Name_Op_Concat then
1981 Kind := N_Op_Concat;
1982 elsif Op_Name = Name_Op_Multiply then
1983 Kind := N_Op_Multiply;
1984 elsif Op_Name = Name_Op_Divide then
1985 Kind := N_Op_Divide;
1986 elsif Op_Name = Name_Op_Mod then
1987 Kind := N_Op_Mod;
1988 elsif Op_Name = Name_Op_Rem then
1989 Kind := N_Op_Rem;
1990 elsif Op_Name = Name_Op_Expon then
1991 Kind := N_Op_Expon;
1992 else
1993 raise Program_Error;
1994 end if;
1996 -- Unary operators
1998 else
1999 if Op_Name = Name_Op_Add then
2000 Kind := N_Op_Plus;
2001 elsif Op_Name = Name_Op_Subtract then
2002 Kind := N_Op_Minus;
2003 elsif Op_Name = Name_Op_Abs then
2004 Kind := N_Op_Abs;
2005 elsif Op_Name = Name_Op_Not then
2006 Kind := N_Op_Not;
2007 else
2008 raise Program_Error;
2009 end if;
2010 end if;
2012 return Kind;
2013 end Operator_Kind;
2015 ----------------------------
2016 -- Preanalyze_And_Resolve --
2017 ----------------------------
2019 procedure Preanalyze_And_Resolve
2020 (N : Node_Id;
2021 T : Entity_Id;
2022 With_Freezing : Boolean)
2024 Save_Full_Analysis : constant Boolean := Full_Analysis;
2025 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
2026 Save_Preanalysis_Count : constant Nat :=
2027 Inside_Preanalysis_Without_Freezing;
2028 begin
2029 pragma Assert (Nkind (N) in N_Subexpr);
2031 if not With_Freezing then
2032 Set_Must_Not_Freeze (N);
2033 Inside_Preanalysis_Without_Freezing :=
2034 Inside_Preanalysis_Without_Freezing + 1;
2035 end if;
2037 Full_Analysis := False;
2038 Expander_Mode_Save_And_Set (False);
2040 -- See also Preanalyze_And_Resolve in sem.adb for similar handling
2042 -- Normally, we suppress all checks for this preanalysis. There is no
2043 -- point in processing them now, since they will be applied properly
2044 -- and in the proper location when the default expressions reanalyzed
2045 -- and reexpanded later on. We will also have more information at that
2046 -- point for possible suppression of individual checks.
2048 -- However, in GNATprove mode, most expansion is suppressed, and this
2049 -- later reanalysis and reexpansion may not occur. GNATprove mode does
2050 -- require the setting of checking flags for proof purposes, so we
2051 -- do the GNATprove preanalysis without suppressing checks.
2053 -- This special handling for SPARK mode is required for example in the
2054 -- case of Ada 2012 constructs such as quantified expressions, which are
2055 -- expanded in two separate steps.
2057 -- We also do not want to suppress checks if we are not dealing
2058 -- with a default expression. One such case that is known to reach
2059 -- this point is the expression of an expression function.
2061 if GNATprove_Mode or Nkind (Parent (N)) = N_Simple_Return_Statement then
2062 Analyze_And_Resolve (N, T);
2063 else
2064 Analyze_And_Resolve (N, T, Suppress => All_Checks);
2065 end if;
2067 Expander_Mode_Restore;
2068 Full_Analysis := Save_Full_Analysis;
2070 if not With_Freezing then
2071 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
2072 Inside_Preanalysis_Without_Freezing :=
2073 Inside_Preanalysis_Without_Freezing - 1;
2074 end if;
2076 pragma Assert
2077 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
2078 end Preanalyze_And_Resolve;
2080 ----------------------------
2081 -- Preanalyze_And_Resolve --
2082 ----------------------------
2084 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
2085 begin
2086 Preanalyze_And_Resolve (N, T, With_Freezing => False);
2087 end Preanalyze_And_Resolve;
2089 -- Version without context type
2091 procedure Preanalyze_And_Resolve (N : Node_Id) is
2092 Save_Full_Analysis : constant Boolean := Full_Analysis;
2094 begin
2095 Full_Analysis := False;
2096 Expander_Mode_Save_And_Set (False);
2098 Analyze (N);
2099 Resolve (N, Etype (N), Suppress => All_Checks);
2101 Expander_Mode_Restore;
2102 Full_Analysis := Save_Full_Analysis;
2103 end Preanalyze_And_Resolve;
2105 ------------------------------------------
2106 -- Preanalyze_With_Freezing_And_Resolve --
2107 ------------------------------------------
2109 procedure Preanalyze_With_Freezing_And_Resolve
2110 (N : Node_Id;
2111 T : Entity_Id)
2113 begin
2114 Preanalyze_And_Resolve (N, T, With_Freezing => True);
2115 end Preanalyze_With_Freezing_And_Resolve;
2117 ----------------------------------
2118 -- Replace_Actual_Discriminants --
2119 ----------------------------------
2121 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2122 Loc : constant Source_Ptr := Sloc (N);
2123 Tsk : Node_Id := Empty;
2125 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2126 -- Comment needed???
2128 -------------------
2129 -- Process_Discr --
2130 -------------------
2132 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2133 Ent : Entity_Id;
2135 begin
2136 if Nkind (Nod) = N_Identifier then
2137 Ent := Entity (Nod);
2139 if Present (Ent)
2140 and then Ekind (Ent) = E_Discriminant
2141 then
2142 Rewrite (Nod,
2143 Make_Selected_Component (Loc,
2144 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2145 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2147 Set_Etype (Nod, Etype (Ent));
2148 end if;
2150 end if;
2152 return OK;
2153 end Process_Discr;
2155 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2157 -- Start of processing for Replace_Actual_Discriminants
2159 begin
2160 if Expander_Active then
2161 null;
2163 -- Allow the replacement of concurrent discriminants in GNATprove even
2164 -- though this is a light expansion activity. Note that generic units
2165 -- are not modified.
2167 elsif GNATprove_Mode and not Inside_A_Generic then
2168 null;
2170 else
2171 return;
2172 end if;
2174 if Nkind (Name (N)) = N_Selected_Component then
2175 Tsk := Prefix (Name (N));
2177 elsif Nkind (Name (N)) = N_Indexed_Component then
2178 Tsk := Prefix (Prefix (Name (N)));
2179 end if;
2181 if Present (Tsk) then
2182 Replace_Discrs (Default);
2183 end if;
2184 end Replace_Actual_Discriminants;
2186 -------------
2187 -- Resolve --
2188 -------------
2190 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2191 Ambiguous : Boolean := False;
2192 Ctx_Type : Entity_Id := Typ;
2193 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2194 Err_Type : Entity_Id := Empty;
2195 Found : Boolean := False;
2196 From_Lib : Boolean;
2197 I : Interp_Index;
2198 I1 : Interp_Index := 0; -- prevent junk warning
2199 It : Interp;
2200 It1 : Interp;
2201 Seen : Entity_Id := Empty; -- prevent junk warning
2203 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2204 -- Determine whether a node comes from a predefined library unit or
2205 -- Standard.
2207 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2208 -- Try and fix up a literal so that it matches its expected type. New
2209 -- literals are manufactured if necessary to avoid cascaded errors.
2211 procedure Report_Ambiguous_Argument;
2212 -- Additional diagnostics when an ambiguous call has an ambiguous
2213 -- argument (typically a controlling actual).
2215 procedure Resolution_Failed;
2216 -- Called when attempt at resolving current expression fails
2218 ------------------------------------
2219 -- Comes_From_Predefined_Lib_Unit --
2220 -------------------------------------
2222 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2223 begin
2224 return
2225 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2226 end Comes_From_Predefined_Lib_Unit;
2228 --------------------
2229 -- Patch_Up_Value --
2230 --------------------
2232 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2233 begin
2234 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2235 Rewrite (N,
2236 Make_Real_Literal (Sloc (N),
2237 Realval => UR_From_Uint (Intval (N))));
2238 Set_Etype (N, Universal_Real);
2239 Set_Is_Static_Expression (N);
2241 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2242 Rewrite (N,
2243 Make_Integer_Literal (Sloc (N),
2244 Intval => UR_To_Uint (Realval (N))));
2245 Set_Etype (N, Universal_Integer);
2246 Set_Is_Static_Expression (N);
2248 elsif Nkind (N) = N_String_Literal
2249 and then Is_Character_Type (Typ)
2250 then
2251 Set_Character_Literal_Name (Get_Char_Code ('A'));
2252 Rewrite (N,
2253 Make_Character_Literal (Sloc (N),
2254 Chars => Name_Find,
2255 Char_Literal_Value =>
2256 UI_From_CC (Get_Char_Code ('A'))));
2257 Set_Etype (N, Any_Character);
2258 Set_Is_Static_Expression (N);
2260 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2261 Rewrite (N,
2262 Make_String_Literal (Sloc (N),
2263 Strval => End_String));
2265 elsif Nkind (N) = N_Range then
2266 Patch_Up_Value (Low_Bound (N), Typ);
2267 Patch_Up_Value (High_Bound (N), Typ);
2268 end if;
2269 end Patch_Up_Value;
2271 -------------------------------
2272 -- Report_Ambiguous_Argument --
2273 -------------------------------
2275 procedure Report_Ambiguous_Argument is
2276 Arg : constant Node_Id := First (Parameter_Associations (N));
2277 I : Interp_Index;
2278 It : Interp;
2280 begin
2281 if Nkind (Arg) = N_Function_Call
2282 and then Is_Entity_Name (Name (Arg))
2283 and then Is_Overloaded (Name (Arg))
2284 then
2285 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2287 -- Examine possible interpretations, and adapt the message
2288 -- for inherited subprograms declared by a type derivation.
2290 Get_First_Interp (Name (Arg), I, It);
2291 while Present (It.Nam) loop
2292 Error_Msg_Sloc := Sloc (It.Nam);
2294 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2295 Error_Msg_N ("interpretation (inherited) #!", Arg);
2296 else
2297 Error_Msg_N ("interpretation #!", Arg);
2298 end if;
2300 Get_Next_Interp (I, It);
2301 end loop;
2302 end if;
2304 -- Additional message and hint if the ambiguity involves an Ada 2022
2305 -- container aggregate.
2307 Check_Ambiguous_Aggregate (N);
2308 end Report_Ambiguous_Argument;
2310 -----------------------
2311 -- Resolution_Failed --
2312 -----------------------
2314 procedure Resolution_Failed is
2315 begin
2316 Patch_Up_Value (N, Typ);
2318 -- Set the type to the desired one to minimize cascaded errors. Note
2319 -- that this is an approximation and does not work in all cases.
2321 Set_Etype (N, Typ);
2323 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2324 Set_Is_Overloaded (N, False);
2326 -- The caller will return without calling the expander, so we need
2327 -- to set the analyzed flag. Note that it is fine to set Analyzed
2328 -- to True even if we are in the middle of a shallow analysis,
2329 -- (see the spec of sem for more details) since this is an error
2330 -- situation anyway, and there is no point in repeating the
2331 -- analysis later (indeed it won't work to repeat it later, since
2332 -- we haven't got a clear resolution of which entity is being
2333 -- referenced.)
2335 Set_Analyzed (N, True);
2336 return;
2337 end Resolution_Failed;
2339 -- Start of processing for Resolve
2341 begin
2342 if N = Error then
2343 return;
2344 end if;
2346 -- Access attribute on remote subprogram cannot be used for a non-remote
2347 -- access-to-subprogram type.
2349 if Nkind (N) = N_Attribute_Reference
2350 and then Attribute_Name (N) in Name_Access
2351 | Name_Unrestricted_Access
2352 | Name_Unchecked_Access
2353 and then Comes_From_Source (N)
2354 and then Is_Entity_Name (Prefix (N))
2355 and then Is_Subprogram (Entity (Prefix (N)))
2356 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2357 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2358 then
2359 Error_Msg_N
2360 ("prefix must statically denote a non-remote subprogram", N);
2361 end if;
2363 -- If the context is a Remote_Access_To_Subprogram, access attributes
2364 -- must be resolved with the corresponding fat pointer. There is no need
2365 -- to check for the attribute name since the return type of an
2366 -- attribute is never a remote type.
2368 if Nkind (N) = N_Attribute_Reference
2369 and then Comes_From_Source (N)
2370 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2371 then
2372 declare
2373 Attr : constant Attribute_Id :=
2374 Get_Attribute_Id (Attribute_Name (N));
2375 Pref : constant Node_Id := Prefix (N);
2376 Decl : Node_Id;
2377 Spec : Node_Id;
2378 Is_Remote : Boolean := True;
2380 begin
2381 -- Check that Typ is a remote access-to-subprogram type
2383 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2385 -- Prefix (N) must statically denote a remote subprogram
2386 -- declared in a package specification.
2388 if Attr = Attribute_Access or else
2389 Attr = Attribute_Unchecked_Access or else
2390 Attr = Attribute_Unrestricted_Access
2391 then
2392 Decl := Unit_Declaration_Node (Entity (Pref));
2394 if Nkind (Decl) = N_Subprogram_Body then
2395 Spec := Corresponding_Spec (Decl);
2397 if Present (Spec) then
2398 Decl := Unit_Declaration_Node (Spec);
2399 end if;
2400 end if;
2402 Spec := Parent (Decl);
2404 if not Is_Entity_Name (Prefix (N))
2405 or else Nkind (Spec) /= N_Package_Specification
2406 or else
2407 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2408 then
2409 Is_Remote := False;
2410 Error_Msg_N
2411 ("prefix must statically denote a remote subprogram",
2413 end if;
2415 -- If we are generating code in distributed mode, perform
2416 -- semantic checks against corresponding remote entities.
2418 if Expander_Active
2419 and then Get_PCS_Name /= Name_No_DSA
2420 then
2421 Check_Subtype_Conformant
2422 (New_Id => Entity (Prefix (N)),
2423 Old_Id => Designated_Type
2424 (Corresponding_Remote_Type (Typ)),
2425 Err_Loc => N);
2427 if Is_Remote then
2428 Process_Remote_AST_Attribute (N, Typ);
2429 end if;
2430 end if;
2431 end if;
2432 end if;
2433 end;
2434 end if;
2436 Debug_A_Entry ("resolving ", N);
2438 if Debug_Flag_V then
2439 Write_Overloads (N);
2440 end if;
2442 if Comes_From_Source (N) then
2443 if Is_Fixed_Point_Type (Typ) then
2444 Check_Restriction (No_Fixed_Point, N);
2446 elsif Is_Floating_Point_Type (Typ)
2447 and then Typ /= Universal_Real
2448 and then Typ /= Any_Real
2449 then
2450 Check_Restriction (No_Floating_Point, N);
2451 end if;
2452 end if;
2454 -- Return if already analyzed
2456 if Analyzed (N) then
2457 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2458 Analyze_Dimension (N);
2459 return;
2461 -- Any case of Any_Type as the Etype value means that we had a
2462 -- previous error.
2464 elsif Etype (N) = Any_Type then
2465 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2466 return;
2467 end if;
2469 Check_Parameterless_Call (N);
2471 -- The resolution of an Expression_With_Actions is determined by
2472 -- its Expression, but if the node comes from source it is a
2473 -- Declare_Expression and requires scope management.
2475 if Nkind (N) = N_Expression_With_Actions then
2476 if Comes_From_Source (N) and then not Is_Rewrite_Substitution (N) then
2477 Resolve_Declare_Expression (N, Typ);
2478 else
2479 Resolve (Expression (N), Typ);
2480 end if;
2482 Found := True;
2483 Expr_Type := Etype (Expression (N));
2485 -- The resolution of a conditional expression that is the operand of a
2486 -- type conversion is determined by the conversion (RM 4.5.7(10/3)).
2488 elsif Nkind (N) in N_Case_Expression | N_If_Expression
2489 and then Nkind (Parent (N)) = N_Type_Conversion
2490 then
2491 Found := True;
2492 Expr_Type := Etype (Parent (N));
2494 -- If not overloaded, then we know the type, and all that needs doing
2495 -- is to check that this type is compatible with the context. But note
2496 -- that we may have an operator with no interpretation in Ada 2022 for
2497 -- the case of possible user-defined literals as operands.
2499 elsif not Is_Overloaded (N) then
2500 if Nkind (N) in N_Op and then No (Entity (N)) then
2501 pragma Assert (Ada_Version >= Ada_2022);
2502 Found := False;
2503 else
2504 Found := Covers (Typ, Etype (N));
2505 end if;
2506 Expr_Type := Etype (N);
2508 -- In the overloaded case, we must select the interpretation that
2509 -- is compatible with the context (i.e. the type passed to Resolve)
2511 else
2512 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2514 -- Loop through possible interpretations
2516 Get_First_Interp (N, I, It);
2517 Interp_Loop : while Present (It.Typ) loop
2518 if Debug_Flag_V then
2519 Write_Str ("Interp: ");
2520 Write_Interp (It);
2521 end if;
2523 -- We are only interested in interpretations that are compatible
2524 -- with the expected type, any other interpretations are ignored.
2526 if not Covers (Typ, It.Typ) then
2527 if Debug_Flag_V then
2528 Write_Str (" interpretation incompatible with context");
2529 Write_Eol;
2530 end if;
2532 else
2533 -- Skip the current interpretation if it is disabled by an
2534 -- abstract operator. This action is performed only when the
2535 -- type against which we are resolving is the same as the
2536 -- type of the interpretation.
2538 if Ada_Version >= Ada_2005
2539 and then It.Typ = Typ
2540 and then not Is_Universal_Numeric_Type (Typ)
2541 and then Present (It.Abstract_Op)
2542 then
2543 if Debug_Flag_V then
2544 Write_Line ("Skip.");
2545 end if;
2547 goto Continue;
2548 end if;
2550 -- First matching interpretation
2552 if not Found then
2553 Found := True;
2554 I1 := I;
2555 Seen := It.Nam;
2556 Expr_Type := It.Typ;
2558 -- Matching interpretation that is not the first, maybe an
2559 -- error, but there are some cases where preference rules are
2560 -- used to choose between the two possibilities. These and
2561 -- some more obscure cases are handled in Disambiguate.
2563 else
2564 -- If the current statement is part of a predefined library
2565 -- unit, then all interpretations which come from user level
2566 -- packages should not be considered. Check previous and
2567 -- current one.
2569 if From_Lib then
2570 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2571 goto Continue;
2573 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2575 -- Previous interpretation must be discarded
2577 I1 := I;
2578 Seen := It.Nam;
2579 Expr_Type := It.Typ;
2580 Set_Entity (N, Seen);
2581 goto Continue;
2582 end if;
2583 end if;
2585 -- Otherwise apply further disambiguation steps
2587 Error_Msg_Sloc := Sloc (Seen);
2588 It1 := Disambiguate (N, I1, I, Typ);
2590 -- Disambiguation has succeeded. Skip the remaining
2591 -- interpretations.
2593 if It1 /= No_Interp then
2594 Seen := It1.Nam;
2595 Expr_Type := It1.Typ;
2597 while Present (It.Typ) loop
2598 Get_Next_Interp (I, It);
2599 end loop;
2601 else
2602 -- Before we issue an ambiguity complaint, check for the
2603 -- case of a subprogram call where at least one of the
2604 -- arguments is Any_Type, and if so suppress the message,
2605 -- since it is a cascaded error. This can also happen for
2606 -- a generalized indexing operation.
2608 if Nkind (N) in N_Subprogram_Call
2609 or else (Nkind (N) = N_Indexed_Component
2610 and then Present (Generalized_Indexing (N)))
2611 then
2612 declare
2613 A : Node_Id;
2614 E : Node_Id;
2616 begin
2617 if Nkind (N) = N_Indexed_Component then
2618 Rewrite (N, Generalized_Indexing (N));
2619 end if;
2621 A := First_Actual (N);
2622 while Present (A) loop
2623 E := A;
2625 if Nkind (E) = N_Parameter_Association then
2626 E := Explicit_Actual_Parameter (E);
2627 end if;
2629 if Etype (E) = Any_Type then
2630 if Debug_Flag_V then
2631 Write_Str ("Any_Type in call");
2632 Write_Eol;
2633 end if;
2635 exit Interp_Loop;
2636 end if;
2638 Next_Actual (A);
2639 end loop;
2640 end;
2642 elsif Nkind (N) in N_Binary_Op
2643 and then (Etype (Left_Opnd (N)) = Any_Type
2644 or else Etype (Right_Opnd (N)) = Any_Type)
2645 then
2646 exit Interp_Loop;
2648 elsif Nkind (N) in N_Unary_Op
2649 and then Etype (Right_Opnd (N)) = Any_Type
2650 then
2651 exit Interp_Loop;
2652 end if;
2654 -- Not that special case, so issue message using the flag
2655 -- Ambiguous to control printing of the header message
2656 -- only at the start of an ambiguous set.
2658 if not Ambiguous then
2659 if Nkind (N) = N_Function_Call
2660 and then Nkind (Name (N)) = N_Explicit_Dereference
2661 then
2662 Error_Msg_N
2663 ("ambiguous expression (cannot resolve indirect "
2664 & "call)!", N);
2665 else
2666 Error_Msg_NE -- CODEFIX
2667 ("ambiguous expression (cannot resolve&)!",
2668 N, It.Nam);
2669 end if;
2671 Ambiguous := True;
2673 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2674 Error_Msg_N
2675 ("\\possible interpretation (inherited)#!", N);
2676 else
2677 Error_Msg_N -- CODEFIX
2678 ("\\possible interpretation#!", N);
2679 end if;
2681 if Nkind (N) in N_Subprogram_Call
2682 and then Present (Parameter_Associations (N))
2683 then
2684 Report_Ambiguous_Argument;
2685 end if;
2686 end if;
2688 Error_Msg_Sloc := Sloc (It.Nam);
2690 -- By default, the error message refers to the candidate
2691 -- interpretation. But if it is a predefined operator, it
2692 -- is implicitly declared at the declaration of the type
2693 -- of the operand. Recover the sloc of that declaration
2694 -- for the error message.
2696 if Nkind (N) in N_Op
2697 and then Scope (It.Nam) = Standard_Standard
2698 and then not Is_Overloaded (Right_Opnd (N))
2699 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2700 Standard_Standard
2701 then
2702 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2704 if Comes_From_Source (Err_Type)
2705 and then Present (Parent (Err_Type))
2706 then
2707 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2708 end if;
2710 elsif Nkind (N) in N_Binary_Op
2711 and then Scope (It.Nam) = Standard_Standard
2712 and then not Is_Overloaded (Left_Opnd (N))
2713 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2714 Standard_Standard
2715 then
2716 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2718 if Comes_From_Source (Err_Type)
2719 and then Present (Parent (Err_Type))
2720 then
2721 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2722 end if;
2724 -- If this is an indirect call, use the subprogram_type
2725 -- in the message, to have a meaningful location. Also
2726 -- indicate if this is an inherited operation, created
2727 -- by a type declaration.
2729 elsif Nkind (N) = N_Function_Call
2730 and then Nkind (Name (N)) = N_Explicit_Dereference
2731 and then Is_Type (It.Nam)
2732 then
2733 Err_Type := It.Nam;
2734 Error_Msg_Sloc :=
2735 Sloc (Associated_Node_For_Itype (Err_Type));
2736 else
2737 Err_Type := Empty;
2738 end if;
2740 if Nkind (N) in N_Op
2741 and then Scope (It.Nam) = Standard_Standard
2742 and then Present (Err_Type)
2743 then
2744 -- Special-case the message for universal_fixed
2745 -- operators, which are not declared with the type
2746 -- of the operand, but appear forever in Standard.
2748 if It.Typ = Universal_Fixed
2749 and then Scope (It.Nam) = Standard_Standard
2750 then
2751 Error_Msg_N
2752 ("\\possible interpretation as universal_fixed "
2753 & "operation (RM 4.5.5 (19))", N);
2754 else
2755 Error_Msg_N
2756 ("\\possible interpretation (predefined)#!", N);
2757 end if;
2759 elsif
2760 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2761 then
2762 Error_Msg_N
2763 ("\\possible interpretation (inherited)#!", N);
2764 else
2765 Error_Msg_N -- CODEFIX
2766 ("\\possible interpretation#!", N);
2767 end if;
2769 end if;
2770 end if;
2772 -- We have a matching interpretation, Expr_Type is the type
2773 -- from this interpretation, and Seen is the entity.
2775 -- For an operator, just set the entity name. The type will be
2776 -- set by the specific operator resolution routine.
2778 if Nkind (N) in N_Op then
2779 Set_Entity (N, Seen);
2780 Generate_Reference (Seen, N);
2782 elsif Nkind (N) in N_Case_Expression
2783 | N_Character_Literal
2784 | N_Delta_Aggregate
2785 | N_If_Expression
2786 then
2787 Set_Etype (N, Expr_Type);
2789 -- AI05-0139-2: Expression is overloaded because type has
2790 -- implicit dereference. The context may be the one that
2791 -- requires implicit dereferemce.
2793 elsif Has_Implicit_Dereference (Expr_Type) then
2794 Set_Etype (N, Expr_Type);
2795 Set_Is_Overloaded (N, False);
2797 -- If the expression is an entity, generate a reference
2798 -- to it, as this is not done for an overloaded construct
2799 -- during analysis.
2801 if Is_Entity_Name (N)
2802 and then Comes_From_Source (N)
2803 then
2804 Generate_Reference (Entity (N), N);
2806 -- Examine access discriminants of entity type,
2807 -- to check whether one of them yields the
2808 -- expected type.
2810 declare
2811 Disc : Entity_Id :=
2812 First_Discriminant (Etype (Entity (N)));
2814 begin
2815 while Present (Disc) loop
2816 exit when Is_Access_Type (Etype (Disc))
2817 and then Has_Implicit_Dereference (Disc)
2818 and then Designated_Type (Etype (Disc)) = Typ;
2820 Next_Discriminant (Disc);
2821 end loop;
2823 if Present (Disc) then
2824 Build_Explicit_Dereference (N, Disc);
2825 end if;
2826 end;
2827 end if;
2829 exit Interp_Loop;
2831 elsif Is_Overloaded (N)
2832 and then Present (It.Nam)
2833 and then Ekind (It.Nam) = E_Discriminant
2834 and then Has_Implicit_Dereference (It.Nam)
2835 then
2836 -- If the node is a general indexing, the dereference is
2837 -- is inserted when resolving the rewritten form, else
2838 -- insert it now.
2840 if Nkind (N) /= N_Indexed_Component
2841 or else No (Generalized_Indexing (N))
2842 then
2843 Build_Explicit_Dereference (N, It.Nam);
2844 end if;
2846 -- For an explicit dereference, attribute reference, range,
2847 -- short-circuit form (which is not an operator node), or call
2848 -- with a name that is an explicit dereference, there is
2849 -- nothing to be done at this point.
2851 elsif Nkind (N) in N_Attribute_Reference
2852 | N_And_Then
2853 | N_Explicit_Dereference
2854 | N_Identifier
2855 | N_Indexed_Component
2856 | N_Or_Else
2857 | N_Range
2858 | N_Selected_Component
2859 | N_Slice
2860 or else Nkind (Name (N)) = N_Explicit_Dereference
2861 then
2862 null;
2864 -- For procedure or function calls, set the type of the name,
2865 -- and also the entity pointer for the prefix.
2867 elsif Nkind (N) in N_Subprogram_Call
2868 and then Is_Entity_Name (Name (N))
2869 then
2870 Set_Etype (Name (N), Expr_Type);
2871 Set_Entity (Name (N), Seen);
2872 Generate_Reference (Seen, Name (N));
2874 elsif Nkind (N) = N_Function_Call
2875 and then Nkind (Name (N)) = N_Selected_Component
2876 then
2877 Set_Etype (Name (N), Expr_Type);
2878 Set_Entity (Selector_Name (Name (N)), Seen);
2879 Generate_Reference (Seen, Selector_Name (Name (N)));
2881 -- For all other cases, just set the type of the Name
2883 else
2884 Set_Etype (Name (N), Expr_Type);
2885 end if;
2887 end if;
2889 <<Continue>>
2891 -- Move to next interpretation
2893 exit Interp_Loop when No (It.Typ);
2895 Get_Next_Interp (I, It);
2896 end loop Interp_Loop;
2897 end if;
2899 -- At this stage Found indicates whether or not an acceptable
2900 -- interpretation exists. If not, then we have an error, except that if
2901 -- the context is Any_Type as a result of some other error, then we
2902 -- suppress the error report.
2904 if not Found then
2905 if Typ /= Any_Type then
2907 -- If type we are looking for is Void, then this is the procedure
2908 -- call case, and the error is simply that what we gave is not a
2909 -- procedure name (we think of procedure calls as expressions with
2910 -- types internally, but the user doesn't think of them this way).
2912 if Typ = Standard_Void_Type then
2914 -- Special case message if function used as a procedure
2916 if Nkind (N) = N_Procedure_Call_Statement
2917 and then Is_Entity_Name (Name (N))
2918 and then Ekind (Entity (Name (N))) = E_Function
2919 then
2920 Error_Msg_NE
2921 ("cannot use call to function & as a statement",
2922 Name (N), Entity (Name (N)));
2923 Error_Msg_N
2924 ("\return value of a function call cannot be ignored",
2925 Name (N));
2927 -- Otherwise give general message (not clear what cases this
2928 -- covers, but no harm in providing for them).
2930 else
2931 Error_Msg_N ("expect procedure name in procedure call", N);
2932 end if;
2934 Found := True;
2936 -- Otherwise we do have a subexpression with the wrong type
2938 -- Check for the case of an allocator which uses an access type
2939 -- instead of the designated type. This is a common error and we
2940 -- specialize the message, posting an error on the operand of the
2941 -- allocator, complaining that we expected the designated type of
2942 -- the allocator.
2944 elsif Nkind (N) = N_Allocator
2945 and then Is_Access_Type (Typ)
2946 and then Is_Access_Type (Etype (N))
2947 and then Designated_Type (Etype (N)) = Typ
2948 then
2949 Wrong_Type (Expression (N), Designated_Type (Typ));
2950 Found := True;
2952 -- Check for view mismatch on Null in instances, for which the
2953 -- view-swapping mechanism has no identifier.
2955 elsif (In_Instance or else In_Inlined_Body)
2956 and then Nkind (N) = N_Null
2957 and then Is_Private_Type (Typ)
2958 and then Is_Access_Type (Full_View (Typ))
2959 then
2960 Resolve (N, Full_View (Typ));
2961 Set_Etype (N, Typ);
2962 return;
2964 -- Check for an aggregate. Sometimes we can get bogus aggregates
2965 -- from misuse of parentheses, and we are about to complain about
2966 -- the aggregate without even looking inside it.
2968 -- Instead, if we have an aggregate of type Any_Composite, then
2969 -- analyze and resolve the component fields, and then only issue
2970 -- another message if we get no errors doing this (otherwise
2971 -- assume that the errors in the aggregate caused the problem).
2973 elsif Nkind (N) = N_Aggregate
2974 and then Etype (N) = Any_Composite
2975 then
2976 if Ada_Version >= Ada_2022
2977 and then Has_Aspect (Typ, Aspect_Aggregate)
2978 then
2979 Resolve_Container_Aggregate (N, Typ);
2981 if Expander_Active then
2982 Expand (N);
2983 end if;
2984 return;
2985 end if;
2987 -- Disable expansion in any case. If there is a type mismatch
2988 -- it may be fatal to try to expand the aggregate. The flag
2989 -- would otherwise be set to false when the error is posted.
2991 Expander_Active := False;
2993 declare
2994 procedure Check_Aggr (Aggr : Node_Id);
2995 -- Check one aggregate, and set Found to True if we have a
2996 -- definite error in any of its elements
2998 procedure Check_Elmt (Aelmt : Node_Id);
2999 -- Check one element of aggregate and set Found to True if
3000 -- we definitely have an error in the element.
3002 ----------------
3003 -- Check_Aggr --
3004 ----------------
3006 procedure Check_Aggr (Aggr : Node_Id) is
3007 Elmt : Node_Id;
3009 begin
3010 if Present (Expressions (Aggr)) then
3011 Elmt := First (Expressions (Aggr));
3012 while Present (Elmt) loop
3013 Check_Elmt (Elmt);
3014 Next (Elmt);
3015 end loop;
3016 end if;
3018 if Present (Component_Associations (Aggr)) then
3019 Elmt := First (Component_Associations (Aggr));
3020 while Present (Elmt) loop
3022 -- If this is a default-initialized component, then
3023 -- there is nothing to check. The box will be
3024 -- replaced by the appropriate call during late
3025 -- expansion.
3027 if Nkind (Elmt) /= N_Iterated_Component_Association
3028 and then not Box_Present (Elmt)
3029 then
3030 Check_Elmt (Expression (Elmt));
3031 end if;
3033 Next (Elmt);
3034 end loop;
3035 end if;
3036 end Check_Aggr;
3038 ----------------
3039 -- Check_Elmt --
3040 ----------------
3042 procedure Check_Elmt (Aelmt : Node_Id) is
3043 begin
3044 -- If we have a nested aggregate, go inside it (to
3045 -- attempt a naked analyze-resolve of the aggregate can
3046 -- cause undesirable cascaded errors). Do not resolve
3047 -- expression if it needs a type from context, as for
3048 -- integer * fixed expression.
3050 if Nkind (Aelmt) = N_Aggregate then
3051 Check_Aggr (Aelmt);
3053 else
3054 Analyze (Aelmt);
3056 if not Is_Overloaded (Aelmt)
3057 and then Etype (Aelmt) /= Any_Fixed
3058 then
3059 Resolve (Aelmt);
3060 end if;
3062 if Etype (Aelmt) = Any_Type then
3063 Found := True;
3064 end if;
3065 end if;
3066 end Check_Elmt;
3068 begin
3069 Check_Aggr (N);
3070 end;
3071 end if;
3073 -- Check whether the node is a literal or a named number or a
3074 -- conditional expression whose dependent expressions are all
3075 -- literals or named numbers.
3077 if Try_User_Defined_Literal (N, Typ) then
3078 return;
3079 end if;
3081 -- Looks like we have a type error, but check for special case
3082 -- of Address wanted, integer found, with the configuration pragma
3083 -- Allow_Integer_Address active. If we have this case, introduce
3084 -- an unchecked conversion to allow the integer expression to be
3085 -- treated as an Address. The reverse case of integer wanted,
3086 -- Address found, is treated in an analogous manner.
3088 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3089 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3090 Analyze_And_Resolve (N, Typ);
3091 return;
3093 -- Under relaxed RM semantics silently replace occurrences of null
3094 -- by System.Null_Address.
3096 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3097 Replace_Null_By_Null_Address (N);
3098 Analyze_And_Resolve (N, Typ);
3099 return;
3100 end if;
3102 -- That special Allow_Integer_Address check did not apply, so we
3103 -- have a real type error. If an error message was issued already,
3104 -- Found got reset to True, so if it's still False, issue standard
3105 -- Wrong_Type message.
3107 if not Found then
3108 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3109 declare
3110 Subp_Name : Node_Id;
3112 begin
3113 if Is_Entity_Name (Name (N)) then
3114 Subp_Name := Name (N);
3116 elsif Nkind (Name (N)) = N_Selected_Component then
3118 -- Protected operation: retrieve operation name
3120 Subp_Name := Selector_Name (Name (N));
3122 else
3123 raise Program_Error;
3124 end if;
3126 Error_Msg_Node_2 := Typ;
3127 Error_Msg_NE
3128 ("no visible interpretation of& matches expected type&",
3129 N, Subp_Name);
3130 end;
3132 if All_Errors_Mode then
3133 declare
3134 Index : Interp_Index;
3135 It : Interp;
3137 begin
3138 Error_Msg_N ("\\possible interpretations:", N);
3140 Get_First_Interp (Name (N), Index, It);
3141 while Present (It.Nam) loop
3142 Error_Msg_Sloc := Sloc (It.Nam);
3143 Error_Msg_Node_2 := It.Nam;
3144 Error_Msg_NE
3145 ("\\ type& for & declared#", N, It.Typ);
3146 Get_Next_Interp (Index, It);
3147 end loop;
3148 end;
3150 else
3151 Error_Msg_N ("\use -gnatf for details", N);
3152 end if;
3154 -- Recognize the case of a quantified expression being mistaken
3155 -- for an iterated component association because the user
3156 -- forgot the "all" or "some" keyword after "for". Because the
3157 -- error message starts with "missing ALL", we automatically
3158 -- benefit from the associated CODEFIX, which requires that
3159 -- the message is located on the identifier following "for"
3160 -- in order for the CODEFIX to insert "all" in the right place.
3162 elsif Nkind (N) = N_Aggregate
3163 and then List_Length (Component_Associations (N)) = 1
3164 and then Nkind (First (Component_Associations (N)))
3165 = N_Iterated_Component_Association
3166 and then Is_Boolean_Type (Typ)
3167 then
3168 if Present
3169 (Iterator_Specification
3170 (First (Component_Associations (N))))
3171 then
3172 Error_Msg_N -- CODEFIX
3173 ("missing ALL or SOME in quantified expression",
3174 Defining_Identifier
3175 (Iterator_Specification
3176 (First (Component_Associations (N)))));
3177 else
3178 Error_Msg_N -- CODEFIX
3179 ("missing ALL or SOME in quantified expression",
3180 Defining_Identifier
3181 (First (Component_Associations (N))));
3182 end if;
3184 -- For an operator with no interpretation, check whether one of
3185 -- its operands may be a user-defined literal.
3187 elsif Nkind (N) in N_Op and then No (Entity (N)) then
3188 if Try_User_Defined_Literal_For_Operator (N, Typ) then
3189 return;
3190 else
3191 Unresolved_Operator (N);
3192 end if;
3194 else
3195 Wrong_Type (N, Typ);
3196 end if;
3197 end if;
3198 end if;
3200 Resolution_Failed;
3201 return;
3203 -- Test if we have more than one interpretation for the context
3205 elsif Ambiguous then
3206 Resolution_Failed;
3207 return;
3209 -- Only one interpretation
3211 else
3212 -- Prevent implicit conversions between access-to-subprogram types
3213 -- with different strub modes. Explicit conversions are acceptable in
3214 -- some circumstances. We don't have to be concerned about data or
3215 -- access-to-data types. Conversions between data types can safely
3216 -- drop or add strub attributes from types, because strub effects are
3217 -- associated with the locations rather than values. E.g., converting
3218 -- a hypothetical Strub_Integer variable to Integer would load the
3219 -- value from the variable, enabling stack scrabbing for the
3220 -- enclosing subprogram, and then convert the value to Integer. As
3221 -- for conversions between access-to-data types, that's no different
3222 -- from any other case of type punning.
3224 if Is_Access_Type (Typ)
3225 and then Ekind (Designated_Type (Typ)) = E_Subprogram_Type
3226 and then Is_Access_Type (Expr_Type)
3227 and then Ekind (Designated_Type (Expr_Type)) = E_Subprogram_Type
3228 then
3229 Check_Same_Strub_Mode
3230 (Designated_Type (Typ), Designated_Type (Expr_Type));
3231 end if;
3233 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3234 -- the "+" on T is abstract, and the operands are of universal type,
3235 -- the above code will have (incorrectly) resolved the "+" to the
3236 -- universal one in Standard. Therefore check for this case and give
3237 -- an error. We can't do this earlier, because it would cause legal
3238 -- cases to get errors (when some other type has an abstract "+").
3240 if Ada_Version >= Ada_2005
3241 and then Nkind (N) in N_Op
3242 and then Is_Overloaded (N)
3243 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3244 then
3245 Get_First_Interp (N, I, It);
3246 while Present (It.Typ) loop
3247 if Present (It.Abstract_Op)
3248 and then Etype (It.Abstract_Op) = Typ
3249 then
3250 Nondispatching_Call_To_Abstract_Operation
3251 (N, It.Abstract_Op);
3252 return;
3253 end if;
3255 Get_Next_Interp (I, It);
3256 end loop;
3257 end if;
3259 -- Here we have an acceptable interpretation for the context
3261 -- Propagate type information and normalize tree for various
3262 -- predefined operations. If the context only imposes a class of
3263 -- types, rather than a specific type, propagate the actual type
3264 -- downward.
3266 if Typ = Any_Integer or else
3267 Typ = Any_Boolean or else
3268 Typ = Any_Modular or else
3269 Typ = Any_Real or else
3270 Typ = Any_Discrete
3271 then
3272 Ctx_Type := Expr_Type;
3274 -- Any_Fixed is legal in a real context only if a specific fixed-
3275 -- point type is imposed. If Norman Cohen can be confused by this,
3276 -- it deserves a separate message.
3278 if Typ = Any_Real
3279 and then Expr_Type = Any_Fixed
3280 then
3281 Error_Msg_N ("illegal context for mixed mode operation", N);
3282 Set_Etype (N, Universal_Real);
3283 Ctx_Type := Universal_Real;
3284 end if;
3285 end if;
3287 -- A user-defined operator is transformed into a function call at
3288 -- this point, so that further processing knows that operators are
3289 -- really operators (i.e. are predefined operators). User-defined
3290 -- operators that are intrinsic are just renamings of the predefined
3291 -- ones, and need not be turned into calls either, but if they rename
3292 -- a different operator, we must transform the node accordingly.
3293 -- Instantiations of Unchecked_Conversion are intrinsic but are
3294 -- treated as functions, even if given an operator designator.
3296 if Nkind (N) in N_Op
3297 and then Present (Entity (N))
3298 and then Ekind (Entity (N)) /= E_Operator
3299 then
3300 if not Is_Predefined_Op (Entity (N)) then
3301 Rewrite_Operator_As_Call (N, Entity (N));
3303 elsif Present (Alias (Entity (N)))
3304 and then
3305 Nkind (Parent (Parent (Entity (N)))) =
3306 N_Subprogram_Renaming_Declaration
3307 then
3308 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3310 -- If the node is rewritten, it will be fully resolved in
3311 -- Rewrite_Renamed_Operator.
3313 if Analyzed (N) then
3314 return;
3315 end if;
3316 end if;
3317 end if;
3319 case N_Subexpr'(Nkind (N)) is
3320 when N_Aggregate =>
3321 Resolve_Aggregate (N, Ctx_Type);
3323 when N_Allocator =>
3324 Resolve_Allocator (N, Ctx_Type);
3326 when N_Short_Circuit =>
3327 Resolve_Short_Circuit (N, Ctx_Type);
3329 when N_Attribute_Reference =>
3330 Resolve_Attribute (N, Ctx_Type);
3332 when N_Case_Expression =>
3333 Resolve_Case_Expression (N, Ctx_Type);
3335 when N_Character_Literal =>
3336 Resolve_Character_Literal (N, Ctx_Type);
3338 when N_Delta_Aggregate =>
3339 Resolve_Delta_Aggregate (N, Ctx_Type);
3341 when N_Expanded_Name =>
3342 Resolve_Entity_Name (N, Ctx_Type);
3344 when N_Explicit_Dereference =>
3345 Resolve_Explicit_Dereference (N, Ctx_Type);
3347 when N_Expression_With_Actions =>
3348 Resolve_Expression_With_Actions (N, Ctx_Type);
3350 when N_Extension_Aggregate =>
3351 Resolve_Extension_Aggregate (N, Ctx_Type);
3353 when N_Function_Call =>
3354 Resolve_Call (N, Ctx_Type);
3356 when N_Identifier =>
3357 Resolve_Entity_Name (N, Ctx_Type);
3359 when N_If_Expression =>
3360 Resolve_If_Expression (N, Ctx_Type);
3362 when N_Indexed_Component =>
3363 Resolve_Indexed_Component (N, Ctx_Type);
3365 when N_Integer_Literal =>
3366 Resolve_Integer_Literal (N, Ctx_Type);
3368 when N_Membership_Test =>
3369 Resolve_Membership_Op (N, Ctx_Type);
3371 when N_Null =>
3372 Resolve_Null (N, Ctx_Type);
3374 when N_Op_And
3375 | N_Op_Or
3376 | N_Op_Xor
3378 Resolve_Logical_Op (N, Ctx_Type);
3380 when N_Op_Eq
3381 | N_Op_Ne
3383 Resolve_Equality_Op (N, Ctx_Type);
3385 when N_Op_Ge
3386 | N_Op_Gt
3387 | N_Op_Le
3388 | N_Op_Lt
3390 Resolve_Comparison_Op (N, Ctx_Type);
3392 when N_Op_Not =>
3393 Resolve_Op_Not (N, Ctx_Type);
3395 when N_Op_Add
3396 | N_Op_Divide
3397 | N_Op_Mod
3398 | N_Op_Multiply
3399 | N_Op_Rem
3400 | N_Op_Subtract
3402 Resolve_Arithmetic_Op (N, Ctx_Type);
3404 when N_Op_Concat =>
3405 Resolve_Op_Concat (N, Ctx_Type);
3407 when N_Op_Expon =>
3408 Resolve_Op_Expon (N, Ctx_Type);
3410 when N_Op_Abs
3411 | N_Op_Minus
3412 | N_Op_Plus
3414 Resolve_Unary_Op (N, Ctx_Type);
3416 when N_Op_Shift =>
3417 Resolve_Shift (N, Ctx_Type);
3419 when N_Procedure_Call_Statement =>
3420 Resolve_Call (N, Ctx_Type);
3422 when N_Operator_Symbol =>
3423 Resolve_Operator_Symbol (N, Ctx_Type);
3425 when N_Qualified_Expression =>
3426 Resolve_Qualified_Expression (N, Ctx_Type);
3428 -- Why is the following null, needs a comment ???
3430 when N_Quantified_Expression =>
3431 null;
3433 when N_Raise_Expression =>
3434 Resolve_Raise_Expression (N, Ctx_Type);
3436 when N_Raise_xxx_Error =>
3437 Set_Etype (N, Ctx_Type);
3439 when N_Range =>
3440 Resolve_Range (N, Ctx_Type);
3442 when N_Real_Literal =>
3443 Resolve_Real_Literal (N, Ctx_Type);
3445 when N_Reference =>
3446 Resolve_Reference (N, Ctx_Type);
3448 when N_Selected_Component =>
3449 Resolve_Selected_Component (N, Ctx_Type);
3451 when N_Slice =>
3452 Resolve_Slice (N, Ctx_Type);
3454 when N_String_Literal =>
3455 Resolve_String_Literal (N, Ctx_Type);
3457 when N_Interpolated_String_Literal =>
3458 Resolve_Interpolated_String_Literal (N, Ctx_Type);
3460 when N_Target_Name =>
3461 Resolve_Target_Name (N, Ctx_Type);
3463 when N_Type_Conversion =>
3464 Resolve_Type_Conversion (N, Ctx_Type);
3466 when N_Unchecked_Expression =>
3467 Resolve_Unchecked_Expression (N, Ctx_Type);
3469 when N_Unchecked_Type_Conversion =>
3470 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3471 end case;
3473 -- Mark relevant use-type and use-package clauses as effective using
3474 -- the original node because constant folding may have occurred and
3475 -- removed references that need to be examined.
3477 if Nkind (Original_Node (N)) in N_Op then
3478 Mark_Use_Clauses (Original_Node (N));
3479 end if;
3481 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3482 -- expression of an anonymous access type that occurs in the context
3483 -- of a named general access type, except when the expression is that
3484 -- of a membership test. This ensures proper legality checking in
3485 -- terms of allowed conversions (expressions that would be illegal to
3486 -- convert implicitly are allowed in membership tests).
3488 if Ada_Version >= Ada_2012
3489 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3490 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3491 and then Nkind (Parent (N)) not in N_Membership_Test
3492 then
3493 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3494 Analyze_And_Resolve (N, Ctx_Type);
3495 end if;
3497 -- If the subexpression was replaced by a non-subexpression, then
3498 -- all we do is to expand it. The only legitimate case we know of
3499 -- is converting procedure call statement to entry call statements,
3500 -- but there may be others, so we are making this test general.
3502 if Nkind (N) not in N_Subexpr then
3503 Debug_A_Exit ("resolving ", N, " (done)");
3504 Expand (N);
3505 return;
3506 end if;
3508 -- The expression is definitely NOT overloaded at this point, so
3509 -- we reset the Is_Overloaded flag to avoid any confusion when
3510 -- reanalyzing the node.
3512 Set_Is_Overloaded (N, False);
3514 -- Freeze expression type, entity if it is a name, and designated
3515 -- type if it is an allocator (RM 13.14(10,11,13)).
3517 -- Now that the resolution of the type of the node is complete, and
3518 -- we did not detect an error, we can expand this node. We skip the
3519 -- expand call if we are in a default expression, see section
3520 -- "Handling of Default Expressions" in Sem spec.
3522 Debug_A_Exit ("resolving ", N, " (done)");
3524 -- We unconditionally freeze the expression, even if we are in
3525 -- default expression mode (the Freeze_Expression routine tests this
3526 -- flag and only freezes static types if it is set).
3528 -- Ada 2012 (AI05-177): The declaration of an expression function
3529 -- does not cause freezing, but we never reach here in that case.
3530 -- Here we are resolving the corresponding expanded body, so we do
3531 -- need to perform normal freezing.
3533 -- As elsewhere we do not emit freeze node within a generic.
3535 if not Inside_A_Generic then
3536 Freeze_Expression (N);
3537 end if;
3539 -- Now we can do the expansion
3541 Expand (N);
3542 end if;
3543 end Resolve;
3545 -------------
3546 -- Resolve --
3547 -------------
3549 -- Version with check(s) suppressed
3551 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3552 begin
3553 if Suppress = All_Checks then
3554 declare
3555 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3556 begin
3557 Scope_Suppress.Suppress := (others => True);
3558 Resolve (N, Typ);
3559 Scope_Suppress.Suppress := Sva;
3560 end;
3562 else
3563 declare
3564 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3565 begin
3566 Scope_Suppress.Suppress (Suppress) := True;
3567 Resolve (N, Typ);
3568 Scope_Suppress.Suppress (Suppress) := Svg;
3569 end;
3570 end if;
3571 end Resolve;
3573 -------------
3574 -- Resolve --
3575 -------------
3577 -- Version with implicit type
3579 procedure Resolve (N : Node_Id) is
3580 begin
3581 Resolve (N, Etype (N));
3582 end Resolve;
3584 ---------------------
3585 -- Resolve_Actuals --
3586 ---------------------
3588 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3589 Loc : constant Source_Ptr := Sloc (N);
3590 A : Node_Id;
3591 A_Typ : Entity_Id := Empty; -- init to avoid warning
3592 F : Entity_Id;
3593 F_Typ : Entity_Id;
3594 Prev : Node_Id := Empty;
3595 Orig_A : Node_Id;
3596 Real_F : Entity_Id := Empty; -- init to avoid warning
3598 Real_Subp : Entity_Id;
3599 -- If the subprogram being called is an inherited operation for
3600 -- a formal derived type in an instance, Real_Subp is the subprogram
3601 -- that will be called. It may have different formal names than the
3602 -- operation of the formal in the generic, so after actual is resolved
3603 -- the name of the actual in a named association must carry the name
3604 -- of the actual of the subprogram being called.
3606 procedure Check_Aliased_Parameter;
3607 -- Check rules on aliased parameters and related accessibility rules
3608 -- in (RM 3.10.2 (10.2-10.4)).
3610 procedure Check_Argument_Order;
3611 -- Performs a check for the case where the actuals are all simple
3612 -- identifiers that correspond to the formal names, but in the wrong
3613 -- order, which is considered suspicious and cause for a warning.
3615 procedure Check_Prefixed_Call;
3616 -- If the original node is an overloaded call in prefix notation,
3617 -- insert an 'Access or a dereference as needed over the first actual.
3618 -- Try_Object_Operation has already verified that there is a valid
3619 -- interpretation, but the form of the actual can only be determined
3620 -- once the primitive operation is identified.
3622 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3623 -- Emit an error concerning the illegal usage of an effectively volatile
3624 -- object for reading in interfering context (SPARK RM 7.1.3(10)).
3626 procedure Insert_Default;
3627 -- If the actual is missing in a call, insert in the actuals list
3628 -- an instance of the default expression. The insertion is always
3629 -- a named association.
3631 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3632 -- Check whether T1 and T2, or their full views, are derived from a
3633 -- common type. Used to enforce the restrictions on array conversions
3634 -- of AI95-00246.
3636 function Static_Concatenation (N : Node_Id) return Boolean;
3637 -- Predicate to determine whether an actual that is a concatenation
3638 -- will be evaluated statically and does not need a transient scope.
3639 -- This must be determined before the actual is resolved and expanded
3640 -- because if needed the transient scope must be introduced earlier.
3642 -----------------------------
3643 -- Check_Aliased_Parameter --
3644 -----------------------------
3646 procedure Check_Aliased_Parameter is
3647 Nominal_Subt : Entity_Id;
3649 begin
3650 if Is_Aliased (F) then
3651 if Is_Tagged_Type (A_Typ) then
3652 null;
3654 elsif Is_Aliased_View (A) then
3655 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3656 Nominal_Subt := Base_Type (A_Typ);
3657 else
3658 Nominal_Subt := A_Typ;
3659 end if;
3661 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3662 null;
3664 -- In a generic body assume the worst for generic formals:
3665 -- they can have a constrained partial view (AI05-041).
3667 elsif Has_Discriminants (F_Typ)
3668 and then not Is_Constrained (F_Typ)
3669 and then not Object_Type_Has_Constrained_Partial_View
3670 (Typ => F_Typ, Scop => Current_Scope)
3671 then
3672 null;
3674 else
3675 Error_Msg_NE ("untagged actual does not statically match "
3676 & "aliased formal&", A, F);
3677 end if;
3679 else
3680 Error_Msg_NE ("actual for aliased formal& must be "
3681 & "aliased object", A, F);
3682 end if;
3684 if Ekind (Nam) = E_Procedure then
3685 null;
3687 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3688 if Nkind (Parent (N)) = N_Type_Conversion
3689 and then Type_Access_Level (Etype (Parent (N)))
3690 < Static_Accessibility_Level (A, Object_Decl_Level)
3691 then
3692 Error_Msg_N ("aliased actual has wrong accessibility", A);
3693 end if;
3695 elsif Nkind (Parent (N)) = N_Qualified_Expression
3696 and then Nkind (Parent (Parent (N))) = N_Allocator
3697 and then Type_Access_Level (Etype (Parent (Parent (N))))
3698 < Static_Accessibility_Level (A, Object_Decl_Level)
3699 then
3700 Error_Msg_N
3701 ("aliased actual in allocator has wrong accessibility", A);
3702 end if;
3703 end if;
3704 end Check_Aliased_Parameter;
3706 --------------------------
3707 -- Check_Argument_Order --
3708 --------------------------
3710 procedure Check_Argument_Order is
3711 begin
3712 -- Nothing to do if no parameters, or original node is neither a
3713 -- function call nor a procedure call statement (happens in the
3714 -- operator-transformed-to-function call case), or the call is to an
3715 -- operator symbol (which is usually in infix form), or the call does
3716 -- not come from source, or this warning is off.
3718 if not Warn_On_Parameter_Order
3719 or else No (Parameter_Associations (N))
3720 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3721 or else (Nkind (Name (N)) = N_Identifier
3722 and then Present (Entity (Name (N)))
3723 and then Nkind (Entity (Name (N))) =
3724 N_Defining_Operator_Symbol)
3725 or else not Comes_From_Source (N)
3726 then
3727 return;
3728 end if;
3730 declare
3731 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3733 begin
3734 -- Nothing to do if only one parameter
3736 if Nargs < 2 then
3737 return;
3738 end if;
3740 -- Here if at least two arguments
3742 declare
3743 Actuals : array (1 .. Nargs) of Node_Id;
3744 Actual : Node_Id;
3745 Formal : Node_Id;
3747 Wrong_Order : Boolean := False;
3748 -- Set True if an out of order case is found
3750 begin
3751 -- Collect identifier names of actuals, fail if any actual is
3752 -- not a simple identifier, and record max length of name.
3754 Actual := First (Parameter_Associations (N));
3755 for J in Actuals'Range loop
3756 if Nkind (Actual) /= N_Identifier then
3757 return;
3758 else
3759 Actuals (J) := Actual;
3760 Next (Actual);
3761 end if;
3762 end loop;
3764 -- If we got this far, all actuals are identifiers and the list
3765 -- of their names is stored in the Actuals array.
3767 Formal := First_Formal (Nam);
3768 for J in Actuals'Range loop
3770 -- If we ran out of formals, that's odd, probably an error
3771 -- which will be detected elsewhere, but abandon the search.
3773 if No (Formal) then
3774 return;
3775 end if;
3777 -- If name matches and is in order OK
3779 if Chars (Formal) = Chars (Actuals (J)) then
3780 null;
3782 else
3783 -- If no match, see if it is elsewhere in list and if so
3784 -- flag potential wrong order if type is compatible.
3786 for K in Actuals'Range loop
3787 if Chars (Formal) = Chars (Actuals (K))
3788 and then
3789 Has_Compatible_Type (Actuals (K), Etype (Formal))
3790 then
3791 Wrong_Order := True;
3792 goto Continue;
3793 end if;
3794 end loop;
3796 -- No match
3798 return;
3799 end if;
3801 <<Continue>> Next_Formal (Formal);
3802 end loop;
3804 -- If Formals left over, also probably an error, skip warning
3806 if Present (Formal) then
3807 return;
3808 end if;
3810 -- Here we give the warning if something was out of order
3812 if Wrong_Order then
3813 Error_Msg_N
3814 ("?.p?actuals for this call may be in wrong order", N);
3815 end if;
3816 end;
3817 end;
3818 end Check_Argument_Order;
3820 -------------------------
3821 -- Check_Prefixed_Call --
3822 -------------------------
3824 procedure Check_Prefixed_Call is
3825 Act : constant Node_Id := First_Actual (N);
3826 A_Type : constant Entity_Id := Etype (Act);
3827 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3828 Orig : constant Node_Id := Original_Node (N);
3829 New_A : Node_Id;
3831 begin
3832 -- Check whether the call is a prefixed call, with or without
3833 -- additional actuals.
3835 if Nkind (Orig) = N_Selected_Component
3836 or else
3837 (Nkind (Orig) = N_Indexed_Component
3838 and then Nkind (Prefix (Orig)) = N_Selected_Component
3839 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3840 and then Is_Entity_Name (Act)
3841 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3842 then
3843 if Is_Access_Type (A_Type)
3844 and then not Is_Access_Type (F_Type)
3845 then
3846 -- Introduce dereference on object in prefix
3848 New_A :=
3849 Make_Explicit_Dereference (Sloc (Act),
3850 Prefix => Relocate_Node (Act));
3851 Rewrite (Act, New_A);
3852 Analyze (Act);
3854 elsif Is_Access_Type (F_Type)
3855 and then not Is_Access_Type (A_Type)
3856 then
3857 -- Introduce an implicit 'Access in prefix
3859 if not Is_Aliased_View (Act) then
3860 Error_Msg_NE
3861 ("object in prefixed call to& must be aliased "
3862 & "(RM 4.1.3 (13 1/2))",
3863 Prefix (Act), Nam);
3864 end if;
3866 Rewrite (Act,
3867 Make_Attribute_Reference (Loc,
3868 Attribute_Name => Name_Access,
3869 Prefix => Relocate_Node (Act)));
3870 end if;
3872 Analyze (Act);
3873 end if;
3874 end Check_Prefixed_Call;
3876 ---------------------------------------
3877 -- Flag_Effectively_Volatile_Objects --
3878 ---------------------------------------
3880 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3881 function Flag_Object (N : Node_Id) return Traverse_Result;
3882 -- Determine whether arbitrary node N denotes an effectively volatile
3883 -- object for reading and if it does, emit an error.
3885 -----------------
3886 -- Flag_Object --
3887 -----------------
3889 function Flag_Object (N : Node_Id) return Traverse_Result is
3890 Id : Entity_Id;
3892 begin
3893 case Nkind (N) is
3894 -- Do not consider nested function calls because they have
3895 -- already been processed during their own resolution.
3897 when N_Function_Call =>
3898 return Skip;
3900 when N_Identifier | N_Expanded_Name =>
3901 Id := Entity (N);
3903 -- Identifiers of components and discriminants are not names
3904 -- in the sense of Ada RM 4.1. They can only occur as a
3905 -- selector_name in selected_component or as a choice in
3906 -- component_association.
3908 if Present (Id)
3909 and then Is_Object (Id)
3910 and then Ekind (Id) not in E_Component | E_Discriminant
3911 and then Is_Effectively_Volatile_For_Reading (Id)
3912 and then
3913 not Is_OK_Volatile_Context (Context => Parent (N),
3914 Obj_Ref => N,
3915 Check_Actuals => True)
3916 then
3917 Error_Msg_Code := GEC_Volatile_Non_Interfering_Context;
3918 Error_Msg_N
3919 ("volatile object cannot appear in this context '[[]']",
3921 end if;
3923 return Skip;
3925 when others =>
3926 return OK;
3927 end case;
3928 end Flag_Object;
3930 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3932 -- Start of processing for Flag_Effectively_Volatile_Objects
3934 begin
3935 Flag_Objects (Expr);
3936 end Flag_Effectively_Volatile_Objects;
3938 --------------------
3939 -- Insert_Default --
3940 --------------------
3942 procedure Insert_Default is
3943 Actval : Node_Id;
3944 Assoc : Node_Id;
3946 begin
3947 -- Missing argument in call, nothing to insert
3949 if No (Default_Value (F)) then
3950 return;
3952 else
3953 -- Note that we do a full New_Copy_Tree, so that any associated
3954 -- Itypes are properly copied. This may not be needed any more,
3955 -- but it does no harm as a safety measure. Defaults of a generic
3956 -- formal may be out of bounds of the corresponding actual (see
3957 -- cc1311b) and an additional check may be required.
3959 Actval :=
3960 New_Copy_Tree
3961 (Default_Value (F),
3962 New_Scope => Current_Scope,
3963 New_Sloc => Loc);
3965 -- Propagate dimension information, if any.
3967 Copy_Dimensions (Default_Value (F), Actval);
3969 if Is_Concurrent_Type (Scope (Nam))
3970 and then Has_Discriminants (Scope (Nam))
3971 then
3972 Replace_Actual_Discriminants (N, Actval);
3973 end if;
3975 if Is_Overloadable (Nam)
3976 and then Present (Alias (Nam))
3977 then
3978 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3979 and then not Is_Tagged_Type (Etype (F))
3980 then
3981 -- If default is a real literal, do not introduce a
3982 -- conversion whose effect may depend on the run-time
3983 -- size of universal real.
3985 if Nkind (Actval) = N_Real_Literal then
3986 Set_Etype (Actval, Base_Type (Etype (F)));
3987 else
3988 Actval := Unchecked_Convert_To (Etype (F), Actval);
3989 end if;
3990 end if;
3992 if Is_Scalar_Type (Etype (F)) then
3993 Enable_Range_Check (Actval);
3994 end if;
3996 Set_Parent (Actval, N);
3998 -- Resolve aggregates with their base type, to avoid scope
3999 -- anomalies: the subtype was first built in the subprogram
4000 -- declaration, and the current call may be nested.
4002 if Nkind (Actval) = N_Aggregate then
4003 Analyze_And_Resolve (Actval, Etype (F));
4004 else
4005 Analyze_And_Resolve (Actval, Etype (Actval));
4006 end if;
4008 else
4009 Set_Parent (Actval, N);
4011 -- See note above concerning aggregates
4013 if Nkind (Actval) = N_Aggregate
4014 and then Has_Discriminants (Etype (Actval))
4015 then
4016 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
4018 -- Resolve entities with their own type, which may differ from
4019 -- the type of a reference in a generic context (the view
4020 -- swapping mechanism did not anticipate the re-analysis of
4021 -- default values in calls).
4023 elsif Is_Entity_Name (Actval) then
4024 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
4026 else
4027 Analyze_And_Resolve (Actval, Etype (Actval));
4028 end if;
4029 end if;
4031 -- If default is a tag indeterminate function call, propagate tag
4032 -- to obtain proper dispatching.
4034 if Is_Controlling_Formal (F)
4035 and then Nkind (Default_Value (F)) = N_Function_Call
4036 then
4037 Set_Is_Controlling_Actual (Actval);
4038 end if;
4039 end if;
4041 -- If the default expression raises constraint error, then just
4042 -- silently replace it with an N_Raise_Constraint_Error node, since
4043 -- we already gave the warning on the subprogram spec. If node is
4044 -- already a Raise_Constraint_Error leave as is, to prevent loops in
4045 -- the warnings removal machinery.
4047 if Raises_Constraint_Error (Actval)
4048 and then Nkind (Actval) /= N_Raise_Constraint_Error
4049 then
4050 Rewrite (Actval,
4051 Make_Raise_Constraint_Error (Loc,
4052 Reason => CE_Range_Check_Failed));
4054 Set_Raises_Constraint_Error (Actval);
4055 Set_Etype (Actval, Etype (F));
4056 end if;
4058 Assoc :=
4059 Make_Parameter_Association (Loc,
4060 Explicit_Actual_Parameter => Actval,
4061 Selector_Name => Make_Identifier (Loc, Chars (F)));
4063 -- Case of insertion is first named actual
4065 if No (Prev)
4066 or else Nkind (Parent (Prev)) /= N_Parameter_Association
4067 then
4068 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
4069 Set_First_Named_Actual (N, Actval);
4071 if No (Prev) then
4072 if No (Parameter_Associations (N)) then
4073 Set_Parameter_Associations (N, New_List (Assoc));
4074 else
4075 Append (Assoc, Parameter_Associations (N));
4076 end if;
4078 else
4079 Insert_After (Prev, Assoc);
4080 end if;
4082 -- Case of insertion is not first named actual
4084 else
4085 Set_Next_Named_Actual
4086 (Assoc, Next_Named_Actual (Parent (Prev)));
4087 Set_Next_Named_Actual (Parent (Prev), Actval);
4088 Append (Assoc, Parameter_Associations (N));
4089 end if;
4091 Mark_Rewrite_Insertion (Assoc);
4092 Mark_Rewrite_Insertion (Actval);
4094 Prev := Actval;
4095 end Insert_Default;
4097 -------------------
4098 -- Same_Ancestor --
4099 -------------------
4101 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
4102 FT1 : Entity_Id := T1;
4103 FT2 : Entity_Id := T2;
4105 begin
4106 if Is_Private_Type (T1)
4107 and then Present (Full_View (T1))
4108 then
4109 FT1 := Full_View (T1);
4110 end if;
4112 if Is_Private_Type (T2)
4113 and then Present (Full_View (T2))
4114 then
4115 FT2 := Full_View (T2);
4116 end if;
4118 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
4119 end Same_Ancestor;
4121 --------------------------
4122 -- Static_Concatenation --
4123 --------------------------
4125 function Static_Concatenation (N : Node_Id) return Boolean is
4126 begin
4127 case Nkind (N) is
4128 when N_String_Literal =>
4129 return True;
4131 when N_Op_Concat =>
4133 -- Concatenation is static when both operands are static and
4134 -- the concatenation operator is a predefined one.
4136 return Scope (Entity (N)) = Standard_Standard
4137 and then
4138 Static_Concatenation (Left_Opnd (N))
4139 and then
4140 Static_Concatenation (Right_Opnd (N));
4142 when others =>
4143 if Is_Entity_Name (N) then
4144 declare
4145 Ent : constant Entity_Id := Entity (N);
4146 begin
4147 return Ekind (Ent) = E_Constant
4148 and then Present (Constant_Value (Ent))
4149 and then
4150 Is_OK_Static_Expression (Constant_Value (Ent));
4151 end;
4153 else
4154 return False;
4155 end if;
4156 end case;
4157 end Static_Concatenation;
4159 -- Start of processing for Resolve_Actuals
4161 begin
4162 Check_Argument_Order;
4164 if Is_Overloadable (Nam)
4165 and then Is_Inherited_Operation (Nam)
4166 and then In_Instance
4167 and then Present (Alias (Nam))
4168 and then Present (Overridden_Operation (Alias (Nam)))
4169 then
4170 Real_Subp := Alias (Nam);
4171 else
4172 Real_Subp := Empty;
4173 end if;
4175 if Present (First_Actual (N)) then
4176 Check_Prefixed_Call;
4177 end if;
4179 A := First_Actual (N);
4180 F := First_Formal (Nam);
4182 if Present (Real_Subp) then
4183 Real_F := First_Formal (Real_Subp);
4184 end if;
4186 while Present (F) loop
4187 if No (A) and then Needs_No_Actuals (Nam) then
4188 null;
4190 -- If we have an error in any formal or actual, indicated by a type
4191 -- of Any_Type, then abandon resolution attempt, and set result type
4192 -- to Any_Type.
4194 elsif Etype (F) = Any_Type then
4195 Set_Etype (N, Any_Type);
4196 return;
4198 elsif Present (A) and then Etype (A) = Any_Type then
4199 -- For the peculiar case of a user-defined comparison or equality
4200 -- operator that does not return a boolean type, the operands may
4201 -- have been ambiguous for the predefined operator and, therefore,
4202 -- marked with Any_Type. Since the operation has been resolved to
4203 -- the user-defined operator, that is irrelevant, so reset Etype.
4205 if Nkind (Original_Node (N)) in N_Op_Compare
4206 and then not Is_Boolean_Type (Etype (N))
4207 then
4208 Set_Etype (A, Etype (F));
4210 -- Also skip this if the actual is a Raise_Expression, whose type
4211 -- is imposed from context.
4213 elsif Nkind (A) = N_Raise_Expression then
4214 null;
4216 else
4217 Set_Etype (N, Any_Type);
4218 return;
4219 end if;
4220 end if;
4222 -- Case where actual is present
4224 -- If the actual is an entity, generate a reference to it now. We
4225 -- do this before the actual is resolved, because a formal of some
4226 -- protected subprogram, or a task discriminant, will be rewritten
4227 -- during expansion, and the source entity reference may be lost.
4229 if Present (A)
4230 and then Is_Entity_Name (A)
4231 and then Comes_From_Source (A)
4232 then
4233 -- Annotate the tree by creating a variable reference marker when
4234 -- the actual denotes a variable reference, in case the reference
4235 -- is folded or optimized away. The variable reference marker is
4236 -- automatically saved for later examination by the ABE Processing
4237 -- phase. The status of the reference is set as follows:
4239 -- status mode
4240 -- read IN, IN OUT
4241 -- write IN OUT, OUT
4243 if Needs_Variable_Reference_Marker
4244 (N => A,
4245 Calls_OK => True)
4246 then
4247 Build_Variable_Reference_Marker
4248 (N => A,
4249 Read => Ekind (F) /= E_Out_Parameter,
4250 Write => Ekind (F) /= E_In_Parameter);
4251 end if;
4253 Orig_A := Entity (A);
4255 if Present (Orig_A) then
4256 if Is_Formal (Orig_A)
4257 and then Ekind (F) /= E_In_Parameter
4258 then
4259 Generate_Reference (Orig_A, A, 'm');
4261 elsif not Is_Overloaded (A) then
4262 if Ekind (F) /= E_Out_Parameter then
4263 Generate_Reference (Orig_A, A);
4265 -- RM 6.4.1(12): For an out parameter that is passed by
4266 -- copy, the formal parameter object is created, and:
4268 -- * For an access type, the formal parameter is initialized
4269 -- from the value of the actual, without checking that the
4270 -- value satisfies any constraint, any predicate, or any
4271 -- exclusion of the null value.
4273 -- * For a scalar type that has the Default_Value aspect
4274 -- specified, the formal parameter is initialized from the
4275 -- value of the actual, without checking that the value
4276 -- satisfies any constraint or any predicate.
4277 -- I do not understand why this case is included??? this is
4278 -- not a case where an OUT parameter is treated as IN OUT.
4280 -- * For a composite type with discriminants or that has
4281 -- implicit initial values for any subcomponents, the
4282 -- behavior is as for an in out parameter passed by copy.
4284 -- Hence for these cases we generate the read reference now
4285 -- (the write reference will be generated later by
4286 -- Note_Possible_Modification).
4288 elsif Is_By_Copy_Type (Etype (F))
4289 and then
4290 (Is_Access_Type (Etype (F))
4291 or else
4292 (Is_Scalar_Type (Etype (F))
4293 and then
4294 Present (Default_Aspect_Value (Etype (F))))
4295 or else
4296 (Is_Composite_Type (Etype (F))
4297 and then (Has_Discriminants (Etype (F))
4298 or else Is_Partially_Initialized_Type
4299 (Etype (F)))))
4300 then
4301 Generate_Reference (Orig_A, A);
4302 end if;
4303 end if;
4304 end if;
4305 end if;
4307 if Present (A)
4308 and then (Nkind (Parent (A)) /= N_Parameter_Association
4309 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4310 then
4311 -- If style checking mode on, check match of formal name
4313 if Style_Check then
4314 if Nkind (Parent (A)) = N_Parameter_Association then
4315 Check_Identifier (Selector_Name (Parent (A)), F);
4316 end if;
4317 end if;
4319 -- If the formal is Out or In_Out, do not resolve and expand the
4320 -- conversion, because it is subsequently expanded into explicit
4321 -- temporaries and assignments. However, the object of the
4322 -- conversion can be resolved. An exception is the case of tagged
4323 -- type conversion with a class-wide actual. In that case we want
4324 -- the tag check to occur and no temporary will be needed (no
4325 -- representation change can occur) and the parameter is passed by
4326 -- reference, so we go ahead and resolve the type conversion.
4327 -- Another exception is the case of reference to component or
4328 -- subcomponent of a bit-packed array, in which case we want to
4329 -- defer expansion to the point the in and out assignments are
4330 -- performed.
4332 if Ekind (F) /= E_In_Parameter
4333 and then Nkind (A) = N_Type_Conversion
4334 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4335 and then not Is_Interface (Etype (A))
4336 then
4337 declare
4338 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4340 begin
4341 -- Check RM 4.6 (24.2/2)
4343 if Is_Array_Type (Etype (F))
4344 and then Is_View_Conversion (A)
4345 then
4346 -- In a view conversion, the conversion must be legal in
4347 -- both directions, and thus both component types must be
4348 -- aliased, or neither (4.6 (8)).
4350 -- Check RM 4.6 (24.8/2)
4352 if Has_Aliased_Components (Expr_Typ) /=
4353 Has_Aliased_Components (Etype (F))
4354 then
4355 -- This normally illegal conversion is legal in an
4356 -- expanded instance body because of RM 12.3(11).
4357 -- At runtime, conversion must create a new object.
4359 if not In_Instance then
4360 Error_Msg_N
4361 ("both component types in a view conversion must"
4362 & " be aliased, or neither", A);
4363 end if;
4365 -- Check RM 4.6 (24/3)
4367 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4368 -- Check view conv between unrelated by ref array
4369 -- types.
4371 if Is_By_Reference_Type (Etype (F))
4372 or else Is_By_Reference_Type (Expr_Typ)
4373 then
4374 Error_Msg_N
4375 ("view conversion between unrelated by reference "
4376 & "array types not allowed ('A'I-00246)", A);
4378 -- In Ada 2005 mode, check view conversion component
4379 -- type cannot be private, tagged, or volatile. Note
4380 -- that we only apply this to source conversions. The
4381 -- generated code can contain conversions which are
4382 -- not subject to this test, and we cannot extract the
4383 -- component type in such cases since it is not
4384 -- present.
4386 elsif Comes_From_Source (A)
4387 and then Ada_Version >= Ada_2005
4388 then
4389 declare
4390 Comp_Type : constant Entity_Id :=
4391 Component_Type (Expr_Typ);
4392 begin
4393 if (Is_Private_Type (Comp_Type)
4394 and then not Is_Generic_Type (Comp_Type))
4395 or else Is_Tagged_Type (Comp_Type)
4396 or else Is_Volatile (Comp_Type)
4397 then
4398 Error_Msg_N
4399 ("component type of a view conversion " &
4400 "cannot be private, tagged, or volatile" &
4401 " (RM 4.6 (24))",
4402 Expression (A));
4403 end if;
4404 end;
4405 end if;
4406 end if;
4408 -- AI12-0074 & AI12-0377
4409 -- Check 6.4.1: If the mode is out, the actual parameter is
4410 -- a view conversion, and the type of the formal parameter
4411 -- is a scalar type, then either:
4412 -- - the target and operand type both do not have the
4413 -- Default_Value aspect specified; or
4414 -- - the target and operand type both have the
4415 -- Default_Value aspect specified, and there shall exist
4416 -- a type (other than a root numeric type) that is an
4417 -- ancestor of both the target type and the operand
4418 -- type.
4420 elsif Ekind (F) = E_Out_Parameter
4421 and then Is_Scalar_Type (Etype (F))
4422 then
4423 if Has_Default_Aspect (Etype (F)) /=
4424 Has_Default_Aspect (Expr_Typ)
4425 then
4426 Error_Msg_N
4427 ("view conversion requires Default_Value on both " &
4428 "types (RM 6.4.1)", A);
4429 elsif Has_Default_Aspect (Expr_Typ)
4430 and then not Same_Ancestor (Etype (F), Expr_Typ)
4431 then
4432 Error_Msg_N
4433 ("view conversion between unrelated types with "
4434 & "Default_Value not allowed (RM 6.4.1)", A);
4435 end if;
4436 end if;
4437 end;
4439 -- Resolve expression if conversion is all OK
4441 if (Conversion_OK (A)
4442 or else Valid_Conversion (A, Etype (A), Expression (A)))
4443 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4444 then
4445 Resolve (Expression (A));
4446 end if;
4448 -- If the actual is a function call that returns a limited
4449 -- unconstrained object that needs finalization, create a
4450 -- transient scope for it, so that it can receive the proper
4451 -- finalization list.
4453 elsif Expander_Active
4454 and then Nkind (A) = N_Function_Call
4455 and then Is_Limited_Record (Etype (F))
4456 and then not Is_Constrained (Etype (F))
4457 and then (Needs_Finalization (Etype (F))
4458 or else Has_Task (Etype (F)))
4459 then
4460 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4461 Resolve (A, Etype (F));
4463 -- A small optimization: if one of the actuals is a concatenation
4464 -- create a block around a procedure call to recover stack space.
4465 -- This alleviates stack usage when several procedure calls in
4466 -- the same statement list use concatenation. We do not perform
4467 -- this wrapping for code statements, where the argument is a
4468 -- static string, and we want to preserve warnings involving
4469 -- sequences of such statements.
4471 elsif Expander_Active
4472 and then Nkind (A) = N_Op_Concat
4473 and then Nkind (N) = N_Procedure_Call_Statement
4474 and then not (Is_Intrinsic_Subprogram (Nam)
4475 and then Chars (Nam) = Name_Asm)
4476 and then not Static_Concatenation (A)
4477 then
4478 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4479 Resolve (A, Etype (F));
4481 else
4482 if Nkind (A) = N_Type_Conversion
4483 and then Is_Array_Type (Etype (F))
4484 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4485 and then
4486 (Is_Limited_Type (Etype (F))
4487 or else Is_Limited_Type (Etype (Expression (A))))
4488 then
4489 Error_Msg_N
4490 ("conversion between unrelated limited array types not "
4491 & "allowed ('A'I-00246)", A);
4493 if Is_Limited_Type (Etype (F)) then
4494 Explain_Limited_Type (Etype (F), A);
4495 end if;
4497 if Is_Limited_Type (Etype (Expression (A))) then
4498 Explain_Limited_Type (Etype (Expression (A)), A);
4499 end if;
4500 end if;
4502 -- (Ada 2005: AI-251): If the actual is an allocator whose
4503 -- directly designated type is a class-wide interface, we build
4504 -- an anonymous access type to use it as the type of the
4505 -- allocator. Later, when the subprogram call is expanded, if
4506 -- the interface has a secondary dispatch table the expander
4507 -- will add a type conversion to force the correct displacement
4508 -- of the pointer.
4510 if Nkind (A) = N_Allocator then
4511 declare
4512 DDT : constant Entity_Id :=
4513 Directly_Designated_Type (Base_Type (Etype (F)));
4515 begin
4516 -- Displace the pointer to the object to reference its
4517 -- secondary dispatch table.
4519 if Is_Class_Wide_Type (DDT)
4520 and then Is_Interface (DDT)
4521 then
4522 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4523 Analyze_And_Resolve (A, Etype (F),
4524 Suppress => Access_Check);
4525 end if;
4527 -- Ada 2005, AI-162:If the actual is an allocator, the
4528 -- innermost enclosing statement is the master of the
4529 -- created object. This needs to be done with expansion
4530 -- enabled only, otherwise the transient scope will not
4531 -- be removed in the expansion of the wrapped construct.
4533 if Expander_Active
4534 and then (Needs_Finalization (DDT)
4535 or else Has_Task (DDT))
4536 then
4537 Establish_Transient_Scope
4538 (A, Manage_Sec_Stack => False);
4539 end if;
4540 end;
4542 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4543 Check_Restriction (No_Access_Parameter_Allocators, A);
4544 end if;
4545 end if;
4547 -- (Ada 2005): The call may be to a primitive operation of a
4548 -- tagged synchronized type, declared outside of the type. In
4549 -- this case the controlling actual must be converted to its
4550 -- corresponding record type, which is the formal type. The
4551 -- actual may be a subtype, either because of a constraint or
4552 -- because it is a generic actual, so use base type to locate
4553 -- concurrent type.
4555 F_Typ := Base_Type (Etype (F));
4557 if Is_Tagged_Type (F_Typ)
4558 and then (Is_Concurrent_Type (F_Typ)
4559 or else Is_Concurrent_Record_Type (F_Typ))
4560 then
4561 -- If the actual is overloaded, look for an interpretation
4562 -- that has a synchronized type.
4564 if not Is_Overloaded (A) then
4565 A_Typ := Base_Type (Etype (A));
4567 else
4568 declare
4569 Index : Interp_Index;
4570 It : Interp;
4572 begin
4573 Get_First_Interp (A, Index, It);
4574 while Present (It.Typ) loop
4575 if Is_Concurrent_Type (It.Typ)
4576 or else Is_Concurrent_Record_Type (It.Typ)
4577 then
4578 A_Typ := Base_Type (It.Typ);
4579 exit;
4580 end if;
4582 Get_Next_Interp (Index, It);
4583 end loop;
4584 end;
4585 end if;
4587 declare
4588 Full_A_Typ : Entity_Id;
4590 begin
4591 if Present (Full_View (A_Typ)) then
4592 Full_A_Typ := Base_Type (Full_View (A_Typ));
4593 else
4594 Full_A_Typ := A_Typ;
4595 end if;
4597 -- Tagged synchronized type (case 1): the actual is a
4598 -- concurrent type.
4600 if Is_Concurrent_Type (A_Typ)
4601 and then Corresponding_Record_Type (A_Typ) = F_Typ
4602 then
4603 Rewrite (A,
4604 Unchecked_Convert_To
4605 (Corresponding_Record_Type (A_Typ), A));
4606 Resolve (A, Etype (F));
4608 -- Tagged synchronized type (case 2): the formal is a
4609 -- concurrent type.
4611 elsif Ekind (Full_A_Typ) = E_Record_Type
4612 and then Present
4613 (Corresponding_Concurrent_Type (Full_A_Typ))
4614 and then Is_Concurrent_Type (F_Typ)
4615 and then Present (Corresponding_Record_Type (F_Typ))
4616 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4617 then
4618 Resolve (A, Corresponding_Record_Type (F_Typ));
4620 -- Common case
4622 else
4623 Resolve (A, Etype (F));
4624 end if;
4625 end;
4627 -- Not a synchronized operation
4629 else
4630 Resolve (A, Etype (F));
4631 end if;
4632 end if;
4634 A_Typ := Etype (A);
4635 F_Typ := Etype (F);
4637 -- An actual cannot be an untagged formal incomplete type
4639 if Ekind (A_Typ) = E_Incomplete_Type
4640 and then not Is_Tagged_Type (A_Typ)
4641 and then Is_Generic_Type (A_Typ)
4642 then
4643 Error_Msg_N
4644 ("invalid use of untagged formal incomplete type", A);
4645 end if;
4647 -- For mode IN, if actual is an entity, and the type of the formal
4648 -- has warnings suppressed, then we reset Never_Set_In_Source for
4649 -- the calling entity. The reason for this is to catch cases like
4650 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4651 -- uses trickery to modify an IN parameter.
4653 if Ekind (F) = E_In_Parameter
4654 and then Is_Entity_Name (A)
4655 and then Present (Entity (A))
4656 and then Ekind (Entity (A)) = E_Variable
4657 and then Has_Warnings_Off (F_Typ)
4658 then
4659 Set_Never_Set_In_Source (Entity (A), False);
4660 end if;
4662 -- Perform error checks for IN and IN OUT parameters
4664 if Ekind (F) /= E_Out_Parameter then
4666 -- Check unset reference. For scalar parameters, it is clearly
4667 -- wrong to pass an uninitialized value as either an IN or
4668 -- IN-OUT parameter. For composites, it is also clearly an
4669 -- error to pass a completely uninitialized value as an IN
4670 -- parameter, but the case of IN OUT is trickier. We prefer
4671 -- not to give a warning here. For example, suppose there is
4672 -- a routine that sets some component of a record to False.
4673 -- It is perfectly reasonable to make this IN-OUT and allow
4674 -- either initialized or uninitialized records to be passed
4675 -- in this case.
4677 -- For partially initialized composite values, we also avoid
4678 -- warnings, since it is quite likely that we are passing a
4679 -- partially initialized value and only the initialized fields
4680 -- will in fact be read in the subprogram.
4682 if Is_Scalar_Type (A_Typ)
4683 or else (Ekind (F) = E_In_Parameter
4684 and then not Is_Partially_Initialized_Type (A_Typ))
4685 then
4686 Check_Unset_Reference (A);
4687 end if;
4689 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4690 -- actual to a nested call, since this constitutes a reading of
4691 -- the parameter, which is not allowed.
4693 if Ada_Version = Ada_83
4694 and then Is_Entity_Name (A)
4695 and then Ekind (Entity (A)) = E_Out_Parameter
4696 then
4697 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4698 end if;
4699 end if;
4701 -- In -gnatd.q mode, forget that a given array is constant when
4702 -- it is passed as an IN parameter to a foreign-convention
4703 -- subprogram. This is in case the subprogram evilly modifies the
4704 -- object. Of course, correct code would use IN OUT.
4706 if Debug_Flag_Dot_Q
4707 and then Ekind (F) = E_In_Parameter
4708 and then Has_Foreign_Convention (Nam)
4709 and then Is_Array_Type (F_Typ)
4710 and then Nkind (A) in N_Has_Entity
4711 and then Present (Entity (A))
4712 then
4713 Set_Is_True_Constant (Entity (A), False);
4714 end if;
4716 -- Case of OUT or IN OUT parameter
4718 if Ekind (F) /= E_In_Parameter then
4720 -- For an Out parameter, check for useless assignment. Note
4721 -- that we can't set Last_Assignment this early, because we may
4722 -- kill current values in Resolve_Call, and that call would
4723 -- clobber the Last_Assignment field.
4725 -- Note: call Warn_On_Useless_Assignment before doing the check
4726 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4727 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4728 -- reflects the last assignment, not this one.
4730 if Ekind (F) = E_Out_Parameter then
4731 if Warn_On_Modified_As_Out_Parameter (F)
4732 and then Is_Entity_Name (A)
4733 and then Present (Entity (A))
4734 and then Comes_From_Source (N)
4735 then
4736 Warn_On_Useless_Assignment (Entity (A), A);
4737 end if;
4738 end if;
4740 -- Validate the form of the actual. Note that the call to
4741 -- Is_OK_Variable_For_Out_Formal generates the required
4742 -- reference in this case.
4744 -- A call to an initialization procedure for an aggregate
4745 -- component may initialize a nested component of a constant
4746 -- designated object. In this context the object is variable.
4748 if not Is_OK_Variable_For_Out_Formal (A)
4749 and then not Is_Init_Proc (Nam)
4750 then
4751 Error_Msg_NE ("actual for& must be a variable", A, F);
4753 if Is_Subprogram (Current_Scope) then
4754 if Is_Invariant_Procedure (Current_Scope)
4755 or else Is_Partial_Invariant_Procedure (Current_Scope)
4756 then
4757 Error_Msg_N
4758 ("function used in invariant cannot modify its "
4759 & "argument", F);
4761 elsif Is_Predicate_Function (Current_Scope) then
4762 Error_Msg_N
4763 ("function used in predicate cannot modify its "
4764 & "argument", F);
4765 end if;
4766 end if;
4767 end if;
4769 -- What's the following about???
4771 if Is_Entity_Name (A) then
4772 Kill_Checks (Entity (A));
4773 else
4774 Kill_All_Checks;
4775 end if;
4776 end if;
4778 if A_Typ = Any_Type then
4779 Set_Etype (N, Any_Type);
4780 return;
4781 end if;
4783 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4785 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4787 -- Apply predicate tests except in certain special cases. Note
4788 -- that it might be more consistent to apply these only when
4789 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4790 -- for the outbound predicate tests ??? In any case indicate
4791 -- the function being called, for better warnings if the call
4792 -- leads to an infinite recursion.
4794 if Predicate_Tests_On_Arguments (Nam) then
4795 Apply_Predicate_Check (A, F_Typ, Nam);
4796 end if;
4798 -- Apply required constraint checks
4800 if Is_Scalar_Type (A_Typ) then
4801 Apply_Scalar_Range_Check (A, F_Typ);
4803 elsif Is_Array_Type (A_Typ) then
4804 Apply_Length_Check (A, F_Typ);
4806 elsif Is_Record_Type (F_Typ)
4807 and then Has_Discriminants (F_Typ)
4808 and then Is_Constrained (F_Typ)
4809 and then (not Is_Derived_Type (F_Typ)
4810 or else Comes_From_Source (Nam))
4811 then
4812 Apply_Discriminant_Check (A, F_Typ);
4814 -- For view conversions of a discriminated object, apply
4815 -- check to object itself, the conversion alreay has the
4816 -- proper type.
4818 if Nkind (A) = N_Type_Conversion
4819 and then Is_Constrained (Etype (Expression (A)))
4820 then
4821 Apply_Discriminant_Check (Expression (A), F_Typ);
4822 end if;
4824 elsif Is_Access_Type (F_Typ)
4825 and then Is_Array_Type (Designated_Type (F_Typ))
4826 and then Is_Constrained (Designated_Type (F_Typ))
4827 then
4828 Apply_Length_Check (A, F_Typ);
4830 elsif Is_Access_Type (F_Typ)
4831 and then Has_Discriminants (Designated_Type (F_Typ))
4832 and then Is_Constrained (Designated_Type (F_Typ))
4833 then
4834 Apply_Discriminant_Check (A, F_Typ);
4836 else
4837 Apply_Range_Check (A, F_Typ);
4838 end if;
4840 -- Ada 2005 (AI-231): Note that the controlling parameter case
4841 -- already existed in Ada 95, which is partially checked
4842 -- elsewhere (see Checks), and we don't want the warning
4843 -- message to differ.
4845 if Is_Access_Type (F_Typ)
4846 and then Can_Never_Be_Null (F_Typ)
4847 and then Known_Null (A)
4848 then
4849 if Is_Controlling_Formal (F) then
4850 Apply_Compile_Time_Constraint_Error
4851 (N => A,
4852 Msg => "null value not allowed here??",
4853 Reason => CE_Access_Check_Failed);
4855 elsif Ada_Version >= Ada_2005 then
4856 Apply_Compile_Time_Constraint_Error
4857 (N => A,
4858 Msg => "(Ada 2005) NULL not allowed in "
4859 & "null-excluding formal??",
4860 Reason => CE_Null_Not_Allowed);
4861 end if;
4862 end if;
4863 end if;
4865 -- Checks for OUT parameters and IN OUT parameters
4867 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4869 -- If there is a type conversion, make sure the return value
4870 -- meets the constraints of the variable before the conversion.
4872 if Nkind (A) = N_Type_Conversion then
4873 if Is_Scalar_Type (A_Typ) then
4875 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4876 -- which would prevent the check from being generated.
4877 -- This is for Starlet only though, so long obsolete.
4879 if Mechanism (F) = By_Reference
4880 and then Ekind (Nam) = E_Procedure
4881 and then Is_Valued_Procedure (Nam)
4882 then
4883 null;
4884 else
4885 Apply_Scalar_Range_Check
4886 (Expression (A), Etype (Expression (A)), A_Typ);
4887 end if;
4889 -- In addition the return value must meet the constraints
4890 -- of the object type (see the comment below).
4892 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4894 else
4895 Apply_Range_Check
4896 (Expression (A), Etype (Expression (A)), A_Typ);
4897 end if;
4899 -- If no conversion, apply scalar range checks and length check
4900 -- based on the subtype of the actual (NOT that of the formal).
4901 -- This indicates that the check takes place on return from the
4902 -- call. During expansion the required constraint checks are
4903 -- inserted. In GNATprove mode, in the absence of expansion,
4904 -- the flag indicates that the returned value is valid.
4906 else
4907 if Is_Scalar_Type (F_Typ) then
4908 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4910 elsif Is_Array_Type (F_Typ)
4911 and then Ekind (F) = E_Out_Parameter
4912 then
4913 Apply_Length_Check (A, F_Typ);
4915 else
4916 Apply_Range_Check (A, A_Typ, F_Typ);
4917 end if;
4918 end if;
4920 -- Note: we do not apply the predicate checks for the case of
4921 -- OUT and IN OUT parameters. They are instead applied in the
4922 -- Expand_Actuals routine in Exp_Ch6.
4923 end if;
4925 -- If the formal is of an unconstrained array subtype with fixed
4926 -- lower bound, then sliding to that bound may be needed.
4928 if Is_Fixed_Lower_Bound_Array_Subtype (F_Typ) then
4929 Expand_Sliding_Conversion (A, F_Typ);
4930 end if;
4932 -- An actual associated with an access parameter is implicitly
4933 -- converted to the anonymous access type of the formal and must
4934 -- satisfy the legality checks for access conversions.
4936 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4937 if not Valid_Conversion (A, F_Typ, A) then
4938 Error_Msg_N
4939 ("invalid implicit conversion for access parameter", A);
4940 end if;
4942 -- If the actual is an access selected component of a variable,
4943 -- the call may modify its designated object. It is reasonable
4944 -- to treat this as a potential modification of the enclosing
4945 -- record, to prevent spurious warnings that it should be
4946 -- declared as a constant, because intuitively programmers
4947 -- regard the designated subcomponent as part of the record.
4949 if Nkind (A) = N_Selected_Component
4950 and then Is_Entity_Name (Prefix (A))
4951 and then not Is_Constant_Object (Entity (Prefix (A)))
4952 then
4953 Note_Possible_Modification (A, Sure => False);
4954 end if;
4955 end if;
4957 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
4959 if (Is_By_Reference_Type (F_Typ) or else Is_Aliased (F))
4960 and then Comes_From_Source (N)
4961 then
4962 if Is_Atomic_Object (A)
4963 and then not Is_Atomic (F_Typ)
4964 then
4965 Error_Msg_NE
4966 ("cannot pass atomic object to nonatomic formal&",
4967 A, F);
4968 Error_Msg_N
4969 ("\which is passed by reference (RM C.6(12))", A);
4971 elsif Is_Volatile_Object_Ref (A)
4972 and then not Is_Volatile (F_Typ)
4973 then
4974 Error_Msg_NE
4975 ("cannot pass volatile object to nonvolatile formal&",
4976 A, F);
4977 Error_Msg_N
4978 ("\which is passed by reference (RM C.6(12))", A);
4980 elsif Is_Volatile_Full_Access_Object_Ref (A)
4981 and then not Is_Volatile_Full_Access (F_Typ)
4982 then
4983 Error_Msg_NE
4984 ("cannot pass full access object to nonfull access "
4985 & "formal&", A, F);
4986 Error_Msg_N
4987 ("\which is passed by reference (RM C.6(12))", A);
4988 end if;
4990 -- Check for nonatomic subcomponent of a full access object
4991 -- in Ada 2022 (RM C.6 (12)).
4993 if Ada_Version >= Ada_2022
4994 and then Is_Subcomponent_Of_Full_Access_Object (A)
4995 and then not Is_Atomic_Object (A)
4996 then
4997 Error_Msg_N
4998 ("cannot pass nonatomic subcomponent of full access "
4999 & "object", A);
5000 Error_Msg_NE
5001 ("\to formal & which is passed by reference (RM C.6(12))",
5002 A, F);
5003 end if;
5004 end if;
5006 -- Check that subprograms don't have improper controlling
5007 -- arguments (RM 3.9.2 (9)).
5009 -- A primitive operation may have an access parameter of an
5010 -- incomplete tagged type, but a dispatching call is illegal
5011 -- if the type is still incomplete.
5013 if Is_Controlling_Formal (F) then
5014 Set_Is_Controlling_Actual (A);
5016 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5017 declare
5018 Desig : constant Entity_Id := Designated_Type (F_Typ);
5019 begin
5020 if Ekind (Desig) = E_Incomplete_Type
5021 and then No (Full_View (Desig))
5022 and then No (Non_Limited_View (Desig))
5023 then
5024 Error_Msg_NE
5025 ("premature use of incomplete type& "
5026 & "in dispatching call", A, Desig);
5027 end if;
5028 end;
5029 end if;
5031 elsif Nkind (A) = N_Explicit_Dereference then
5032 Validate_Remote_Access_To_Class_Wide_Type (A);
5033 end if;
5035 -- Apply legality rule 3.9.2 (9/1)
5037 -- Skip this check on helpers and indirect-call wrappers built to
5038 -- support class-wide preconditions.
5040 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
5041 and then not Is_Class_Wide_Type (F_Typ)
5042 and then not Is_Controlling_Formal (F)
5043 and then not In_Instance
5044 and then (not Is_Subprogram (Nam)
5045 or else No (Class_Preconditions_Subprogram (Nam)))
5046 then
5047 Error_Msg_N ("class-wide argument not allowed here!", A);
5049 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5050 Error_Msg_Node_2 := F_Typ;
5051 Error_Msg_NE
5052 ("& is not a dispatching operation of &!", A, Nam);
5053 end if;
5055 -- Apply the checks described in 3.10.2(27): if the context is a
5056 -- specific access-to-object, the actual cannot be class-wide.
5057 -- Use base type to exclude access_to_subprogram cases.
5059 elsif Is_Access_Type (A_Typ)
5060 and then Is_Access_Type (F_Typ)
5061 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
5062 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
5063 or else (Nkind (A) = N_Attribute_Reference
5064 and then
5065 Is_Class_Wide_Type (Etype (Prefix (A)))))
5066 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
5067 and then not Is_Controlling_Formal (F)
5069 -- Disable these checks for call to imported C++ subprograms
5071 and then not
5072 (Is_Entity_Name (Name (N))
5073 and then Is_Imported (Entity (Name (N)))
5074 and then Convention (Entity (Name (N))) = Convention_CPP)
5075 then
5076 Error_Msg_N
5077 ("access to class-wide argument not allowed here!", A);
5079 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5080 Error_Msg_Node_2 := Designated_Type (F_Typ);
5081 Error_Msg_NE
5082 ("& is not a dispatching operation of &!", A, Nam);
5083 end if;
5084 end if;
5086 Check_Aliased_Parameter;
5088 Eval_Actual (A);
5090 -- If it is a named association, treat the selector_name as a
5091 -- proper identifier, and mark the corresponding entity.
5093 if Nkind (Parent (A)) = N_Parameter_Association
5095 -- Ignore reference in SPARK mode, as it refers to an entity not
5096 -- in scope at the point of reference, so the reference should
5097 -- be ignored for computing effects of subprograms.
5099 and then not GNATprove_Mode
5100 then
5101 -- If subprogram is overridden, use name of formal that
5102 -- is being called.
5104 if Present (Real_Subp) then
5105 Set_Entity (Selector_Name (Parent (A)), Real_F);
5106 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
5108 else
5109 Set_Entity (Selector_Name (Parent (A)), F);
5110 Generate_Reference (F, Selector_Name (Parent (A)));
5111 Set_Etype (Selector_Name (Parent (A)), F_Typ);
5112 Generate_Reference (F_Typ, N, ' ');
5113 end if;
5114 end if;
5116 Prev := A;
5118 if Ekind (F) /= E_Out_Parameter then
5119 Check_Unset_Reference (A);
5120 end if;
5122 -- The following checks are only relevant when SPARK_Mode is on as
5123 -- they are not standard Ada legality rule. Internally generated
5124 -- temporaries are ignored.
5126 if SPARK_Mode = On and then Comes_From_Source (A) then
5128 -- Inspect the expression and flag each effectively volatile
5129 -- object for reading as illegal because it appears within
5130 -- an interfering context. Note that this is usually done
5131 -- in Resolve_Entity_Name, but when the effectively volatile
5132 -- object for reading appears as an actual in a call, the call
5133 -- must be resolved first.
5135 Flag_Effectively_Volatile_Objects (A);
5136 end if;
5138 -- A formal parameter of a specific tagged type whose related
5139 -- subprogram is subject to pragma Extensions_Visible with value
5140 -- "False" cannot act as an actual in a subprogram with value
5141 -- "True" (SPARK RM 6.1.7(3)).
5143 -- No check needed for helpers and indirect-call wrappers built to
5144 -- support class-wide preconditions.
5146 if Is_EVF_Expression (A)
5147 and then Extensions_Visible_Status (Nam) =
5148 Extensions_Visible_True
5149 and then not
5150 (Is_Subprogram (Current_Scope)
5151 and then
5152 Present (Class_Preconditions_Subprogram (Current_Scope)))
5153 then
5154 Error_Msg_N
5155 ("formal parameter cannot act as actual parameter when "
5156 & "Extensions_Visible is False", A);
5157 Error_Msg_NE
5158 ("\subprogram & has Extensions_Visible True", A, Nam);
5159 end if;
5161 -- The actual parameter of a Ghost subprogram whose formal is of
5162 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5164 if Comes_From_Source (Nam)
5165 and then Is_Ghost_Entity (Nam)
5166 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5167 and then Is_Entity_Name (A)
5168 and then Present (Entity (A))
5169 and then not Is_Ghost_Entity (Entity (A))
5170 then
5171 Error_Msg_NE
5172 ("non-ghost variable & cannot appear as actual in call to "
5173 & "ghost procedure", A, Entity (A));
5175 if Ekind (F) = E_In_Out_Parameter then
5176 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5177 else
5178 Error_Msg_N ("\corresponding formal has mode OUT", A);
5179 end if;
5180 end if;
5182 -- (AI12-0397): The target of a subprogram call that occurs within
5183 -- the expression of an Default_Initial_Condition aspect and has
5184 -- an actual that is the current instance of the type must be
5185 -- either a primitive of the type or a class-wide subprogram,
5186 -- because the type of the current instance in such an aspect is
5187 -- considered to be a notional formal derived type whose only
5188 -- operations correspond to the primitives of the enclosing type.
5189 -- Nonprimitives can be called, but the current instance must be
5190 -- converted rather than passed directly. Note that a current
5191 -- instance of a type with DIC will occur as a reference to an
5192 -- in-mode formal of an enclosing DIC procedure or partial DIC
5193 -- procedure. (It seems that this check should perhaps also apply
5194 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5195 -- aspects???)
5197 if Nkind (A) = N_Identifier
5198 and then Ekind (Entity (A)) = E_In_Parameter
5200 and then Is_Subprogram (Scope (Entity (A)))
5201 and then Is_DIC_Procedure (Scope (Entity (A)))
5203 -- We check Comes_From_Source to exclude inherited primitives
5204 -- from being flagged, because such subprograms turn out to not
5205 -- always have the Is_Primitive flag set. ???
5207 and then Comes_From_Source (Nam)
5209 and then not Is_Primitive (Nam)
5210 and then not Is_Class_Wide_Type (F_Typ)
5211 then
5212 Error_Msg_NE
5213 ("call to nonprimitive & with current instance not allowed " &
5214 "for aspect", A, Nam);
5215 end if;
5217 Next_Actual (A);
5219 -- Case where actual is not present
5221 else
5222 Insert_Default;
5223 end if;
5225 Next_Formal (F);
5227 if Present (Real_Subp) then
5228 Next_Formal (Real_F);
5229 end if;
5230 end loop;
5231 end Resolve_Actuals;
5233 -----------------------
5234 -- Resolve_Allocator --
5235 -----------------------
5237 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5238 Desig_T : constant Entity_Id := Designated_Type (Typ);
5239 E : constant Node_Id := Expression (N);
5240 Subtyp : Entity_Id;
5241 Discrim : Entity_Id;
5242 Constr : Node_Id;
5243 Aggr : Node_Id;
5244 Assoc : Node_Id := Empty;
5245 Disc_Exp : Node_Id;
5247 procedure Check_Allocator_Discrim_Accessibility
5248 (Disc_Exp : Node_Id;
5249 Alloc_Typ : Entity_Id);
5250 -- Check that accessibility level associated with an access discriminant
5251 -- initialized in an allocator by the expression Disc_Exp is not deeper
5252 -- than the level of the allocator type Alloc_Typ. An error message is
5253 -- issued if this condition is violated. Specialized checks are done for
5254 -- the cases of a constraint expression which is an access attribute or
5255 -- an access discriminant.
5257 procedure Check_Allocator_Discrim_Accessibility_Exprs
5258 (Curr_Exp : Node_Id;
5259 Alloc_Typ : Entity_Id);
5260 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5261 -- across all expressions within a given conditional expression.
5263 function In_Dispatching_Context return Boolean;
5264 -- If the allocator is an actual in a call, it is allowed to be class-
5265 -- wide when the context is not because it is a controlling actual.
5267 -------------------------------------------
5268 -- Check_Allocator_Discrim_Accessibility --
5269 -------------------------------------------
5271 procedure Check_Allocator_Discrim_Accessibility
5272 (Disc_Exp : Node_Id;
5273 Alloc_Typ : Entity_Id)
5275 begin
5276 if Type_Access_Level (Etype (Disc_Exp)) >
5277 Deepest_Type_Access_Level (Alloc_Typ)
5278 then
5279 Error_Msg_N
5280 ("operand type has deeper level than allocator type", Disc_Exp);
5282 -- When the expression is an Access attribute the level of the prefix
5283 -- object must not be deeper than that of the allocator's type.
5285 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5286 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5287 Attribute_Access
5288 and then Static_Accessibility_Level
5289 (Disc_Exp, Zero_On_Dynamic_Level)
5290 > Deepest_Type_Access_Level (Alloc_Typ)
5291 then
5292 Error_Msg_N
5293 ("prefix of attribute has deeper level than allocator type",
5294 Disc_Exp);
5296 -- When the expression is an access discriminant the check is against
5297 -- the level of the prefix object.
5299 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5300 and then Nkind (Disc_Exp) = N_Selected_Component
5301 and then Static_Accessibility_Level
5302 (Disc_Exp, Zero_On_Dynamic_Level)
5303 > Deepest_Type_Access_Level (Alloc_Typ)
5304 then
5305 Error_Msg_N
5306 ("access discriminant has deeper level than allocator type",
5307 Disc_Exp);
5309 -- All other cases are legal
5311 else
5312 null;
5313 end if;
5314 end Check_Allocator_Discrim_Accessibility;
5316 -------------------------------------------------
5317 -- Check_Allocator_Discrim_Accessibility_Exprs --
5318 -------------------------------------------------
5320 procedure Check_Allocator_Discrim_Accessibility_Exprs
5321 (Curr_Exp : Node_Id;
5322 Alloc_Typ : Entity_Id)
5324 Alt : Node_Id;
5325 Expr : Node_Id;
5326 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5327 begin
5328 -- When conditional expressions are constant folded we know at
5329 -- compile time which expression to check - so don't bother with
5330 -- the rest of the cases.
5332 if Nkind (Curr_Exp) = N_Attribute_Reference then
5333 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5335 -- Non-constant-folded if expressions
5337 elsif Nkind (Disc_Exp) = N_If_Expression then
5338 -- Check both expressions if they are still present in the face
5339 -- of expansion.
5341 Expr := Next (First (Expressions (Disc_Exp)));
5342 if Present (Expr) then
5343 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5344 Next (Expr);
5345 if Present (Expr) then
5346 Check_Allocator_Discrim_Accessibility_Exprs
5347 (Expr, Alloc_Typ);
5348 end if;
5349 end if;
5351 -- Non-constant-folded case expressions
5353 elsif Nkind (Disc_Exp) = N_Case_Expression then
5354 -- Check all alternatives
5356 Alt := First (Alternatives (Disc_Exp));
5357 while Present (Alt) loop
5358 Check_Allocator_Discrim_Accessibility_Exprs
5359 (Expression (Alt), Alloc_Typ);
5361 Next (Alt);
5362 end loop;
5364 -- Base case, check the accessibility of the original node of the
5365 -- expression.
5367 else
5368 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5369 end if;
5370 end Check_Allocator_Discrim_Accessibility_Exprs;
5372 ----------------------------
5373 -- In_Dispatching_Context --
5374 ----------------------------
5376 function In_Dispatching_Context return Boolean is
5377 Par : constant Node_Id := Parent (N);
5379 begin
5380 return Nkind (Par) in N_Subprogram_Call
5381 and then Is_Entity_Name (Name (Par))
5382 and then Is_Dispatching_Operation (Entity (Name (Par)));
5383 end In_Dispatching_Context;
5385 -- Start of processing for Resolve_Allocator
5387 begin
5388 -- Replace general access with specific type
5390 if Ekind (Etype (N)) = E_Allocator_Type then
5391 Set_Etype (N, Base_Type (Typ));
5392 end if;
5394 if Is_Abstract_Type (Typ) then
5395 Error_Msg_N ("type of allocator cannot be abstract", N);
5396 end if;
5398 -- For qualified expression, resolve the expression using the given
5399 -- subtype (nothing to do for type mark, subtype indication)
5401 if Nkind (E) = N_Qualified_Expression then
5402 if Is_Class_Wide_Type (Etype (E))
5403 and then not Is_Class_Wide_Type (Desig_T)
5404 and then not In_Dispatching_Context
5405 then
5406 Error_Msg_N
5407 ("class-wide allocator not allowed for this access type", N);
5408 end if;
5410 -- Do a full resolution to apply constraint and predicate checks
5412 Resolve_Qualified_Expression (E, Etype (E));
5413 Check_Unset_Reference (Expression (E));
5415 -- Allocators generated by the build-in-place expansion mechanism
5416 -- are explicitly marked as coming from source but do not need to be
5417 -- checked for limited initialization. To exclude this case, ensure
5418 -- that the parent of the allocator is a source node.
5419 -- The return statement constructed for an Expression_Function does
5420 -- not come from source but requires a limited check.
5422 if Is_Limited_Type (Etype (E))
5423 and then Comes_From_Source (N)
5424 and then
5425 (Comes_From_Source (Parent (N))
5426 or else
5427 (Ekind (Current_Scope) = E_Function
5428 and then Nkind (Original_Node (Unit_Declaration_Node
5429 (Current_Scope))) = N_Expression_Function))
5430 and then not In_Instance_Body
5431 then
5432 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5433 if Nkind (Parent (N)) = N_Assignment_Statement then
5434 Error_Msg_N
5435 ("illegal expression for initialized allocator of a "
5436 & "limited type (RM 7.5 (2.7/2))", N);
5437 else
5438 Error_Msg_N
5439 ("initialization not allowed for limited types", N);
5440 end if;
5442 Explain_Limited_Type (Etype (E), N);
5443 end if;
5444 end if;
5446 -- Calls to build-in-place functions are not currently supported in
5447 -- allocators for access types associated with a simple storage pool.
5448 -- Supporting such allocators may require passing additional implicit
5449 -- parameters to build-in-place functions (or a significant revision
5450 -- of the current b-i-p implementation to unify the handling for
5451 -- multiple kinds of storage pools). ???
5453 if Is_Limited_View (Desig_T)
5454 and then Nkind (Expression (E)) = N_Function_Call
5455 then
5456 declare
5457 Pool : constant Entity_Id :=
5458 Associated_Storage_Pool (Root_Type (Typ));
5459 begin
5460 if Present (Pool)
5461 and then
5462 Present (Get_Rep_Pragma
5463 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5464 then
5465 Error_Msg_N
5466 ("limited function calls not yet supported in simple "
5467 & "storage pool allocators", Expression (E));
5468 end if;
5469 end;
5470 end if;
5472 -- A special accessibility check is needed for allocators that
5473 -- constrain access discriminants. The level of the type of the
5474 -- expression used to constrain an access discriminant cannot be
5475 -- deeper than the type of the allocator (in contrast to access
5476 -- parameters, where the level of the actual can be arbitrary).
5478 -- We can't use Valid_Conversion to perform this check because in
5479 -- general the type of the allocator is unrelated to the type of
5480 -- the access discriminant.
5482 if Ekind (Typ) /= E_Anonymous_Access_Type
5483 or else Is_Local_Anonymous_Access (Typ)
5484 then
5485 Subtyp := Entity (Subtype_Mark (E));
5487 Aggr := Original_Node (Expression (E));
5489 if Has_Discriminants (Subtyp)
5490 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5491 then
5492 Discrim := First_Discriminant (Base_Type (Subtyp));
5494 -- Get the first component expression of the aggregate
5496 if Present (Expressions (Aggr)) then
5497 Disc_Exp := First (Expressions (Aggr));
5499 elsif Present (Component_Associations (Aggr)) then
5500 Assoc := First (Component_Associations (Aggr));
5502 if Present (Assoc) then
5503 Disc_Exp := Expression (Assoc);
5504 else
5505 Disc_Exp := Empty;
5506 end if;
5508 else
5509 Disc_Exp := Empty;
5510 end if;
5512 while Present (Discrim) and then Present (Disc_Exp) loop
5513 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5514 Check_Allocator_Discrim_Accessibility_Exprs
5515 (Disc_Exp, Typ);
5516 end if;
5518 Next_Discriminant (Discrim);
5520 if Present (Discrim) then
5521 if Present (Assoc) then
5522 Next (Assoc);
5523 Disc_Exp := Expression (Assoc);
5525 elsif Present (Next (Disc_Exp)) then
5526 Next (Disc_Exp);
5528 else
5529 Assoc := First (Component_Associations (Aggr));
5531 if Present (Assoc) then
5532 Disc_Exp := Expression (Assoc);
5533 else
5534 Disc_Exp := Empty;
5535 end if;
5536 end if;
5537 end if;
5538 end loop;
5539 end if;
5540 end if;
5542 -- For a subtype mark or subtype indication, freeze the subtype
5544 else
5545 Freeze_Expression (E);
5547 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5548 Error_Msg_N
5549 ("initialization required for access-to-constant allocator", N);
5550 end if;
5552 -- A special accessibility check is needed for allocators that
5553 -- constrain access discriminants. The level of the type of the
5554 -- expression used to constrain an access discriminant cannot be
5555 -- deeper than the type of the allocator (in contrast to access
5556 -- parameters, where the level of the actual can be arbitrary).
5557 -- We can't use Valid_Conversion to perform this check because
5558 -- in general the type of the allocator is unrelated to the type
5559 -- of the access discriminant.
5561 if Nkind (Original_Node (E)) = N_Subtype_Indication
5562 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5563 or else Is_Local_Anonymous_Access (Typ))
5564 then
5565 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5567 if Has_Discriminants (Subtyp) then
5568 Discrim := First_Discriminant (Base_Type (Subtyp));
5569 Constr := First (Constraints (Constraint (Original_Node (E))));
5570 while Present (Discrim) and then Present (Constr) loop
5571 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5572 if Nkind (Constr) = N_Discriminant_Association then
5573 Disc_Exp := Expression (Constr);
5574 else
5575 Disc_Exp := Constr;
5576 end if;
5578 Check_Allocator_Discrim_Accessibility_Exprs
5579 (Disc_Exp, Typ);
5580 end if;
5582 Next_Discriminant (Discrim);
5583 Next (Constr);
5584 end loop;
5585 end if;
5586 end if;
5587 end if;
5589 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5590 -- check that the level of the type of the created object is not deeper
5591 -- than the level of the allocator's access type, since extensions can
5592 -- now occur at deeper levels than their ancestor types. This is a
5593 -- static accessibility level check; a run-time check is also needed in
5594 -- the case of an initialized allocator with a class-wide argument (see
5595 -- Expand_Allocator_Expression).
5597 if Ada_Version >= Ada_2005
5598 and then Is_Class_Wide_Type (Desig_T)
5599 then
5600 declare
5601 Exp_Typ : Entity_Id;
5603 begin
5604 if Nkind (E) = N_Qualified_Expression then
5605 Exp_Typ := Etype (E);
5606 elsif Nkind (E) = N_Subtype_Indication then
5607 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5608 else
5609 Exp_Typ := Entity (E);
5610 end if;
5612 if Type_Access_Level (Exp_Typ) >
5613 Deepest_Type_Access_Level (Typ)
5614 then
5615 if In_Instance_Body then
5616 Error_Msg_Warn := SPARK_Mode /= On;
5617 Error_Msg_N
5618 ("type in allocator has deeper level than designated "
5619 & "class-wide type<<", E);
5620 Error_Msg_N ("\Program_Error [<<", E);
5622 Rewrite (N,
5623 Make_Raise_Program_Error (Sloc (N),
5624 Reason => PE_Accessibility_Check_Failed));
5625 Set_Etype (N, Typ);
5627 -- Do not apply Ada 2005 accessibility checks on a class-wide
5628 -- allocator if the type given in the allocator is a formal
5629 -- type or within a formal package. A run-time check will be
5630 -- performed in the instance.
5632 elsif not Is_Generic_Type (Exp_Typ)
5633 and then not In_Generic_Formal_Package (Exp_Typ)
5634 then
5635 Error_Msg_N
5636 ("type in allocator has deeper level than designated "
5637 & "class-wide type", E);
5638 end if;
5639 end if;
5640 end;
5641 end if;
5643 -- Check for allocation from an empty storage pool. But do not complain
5644 -- if it's a return statement for a build-in-place function, because the
5645 -- allocator is there just in case the caller uses an allocator. If the
5646 -- caller does use an allocator, it will be caught at the call site.
5648 if No_Pool_Assigned (Typ)
5649 and then not For_Special_Return_Object (N)
5650 then
5651 Error_Msg_N ("allocation from empty storage pool!", N);
5653 -- If the context is an unchecked conversion, as may happen within an
5654 -- inlined subprogram, the allocator is being resolved with its own
5655 -- anonymous type. In that case, if the target type has a specific
5656 -- storage pool, it must be inherited explicitly by the allocator type.
5658 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5659 and then No (Associated_Storage_Pool (Typ))
5660 then
5661 Set_Associated_Storage_Pool
5662 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5663 end if;
5665 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5666 Check_Restriction (No_Anonymous_Allocators, N);
5667 end if;
5669 -- Check that an allocator with task parts isn't for a nested access
5670 -- type when restriction No_Task_Hierarchy applies.
5672 if not Is_Library_Level_Entity (Base_Type (Typ))
5673 and then Has_Task (Base_Type (Desig_T))
5674 then
5675 Check_Restriction (No_Task_Hierarchy, N);
5676 end if;
5678 -- An illegal allocator may be rewritten as a raise Program_Error
5679 -- statement.
5681 if Nkind (N) = N_Allocator then
5683 -- Avoid coextension processing for an allocator that is the
5684 -- expansion of a build-in-place function call.
5686 if Nkind (Original_Node (N)) = N_Allocator
5687 and then Nkind (Expression (Original_Node (N))) =
5688 N_Qualified_Expression
5689 and then Nkind (Expression (Expression (Original_Node (N)))) =
5690 N_Function_Call
5691 and then Is_Expanded_Build_In_Place_Call
5692 (Expression (Expression (Original_Node (N))))
5693 then
5694 null; -- b-i-p function call case
5696 else
5697 -- An anonymous access discriminant is the definition of a
5698 -- coextension.
5700 if Ekind (Typ) = E_Anonymous_Access_Type
5701 and then Nkind (Associated_Node_For_Itype (Typ)) =
5702 N_Discriminant_Specification
5703 then
5704 declare
5705 Discr : constant Entity_Id :=
5706 Defining_Identifier (Associated_Node_For_Itype (Typ));
5708 begin
5709 Check_Restriction (No_Coextensions, N);
5711 -- Ada 2012 AI05-0052: If the designated type of the
5712 -- allocator is limited, then the allocator shall not
5713 -- be used to define the value of an access discriminant
5714 -- unless the discriminated type is immutably limited.
5716 if Ada_Version >= Ada_2012
5717 and then Is_Limited_Type (Desig_T)
5718 and then not Is_Limited_View (Scope (Discr))
5719 then
5720 Error_Msg_N
5721 ("only immutably limited types can have anonymous "
5722 & "access discriminants designating a limited type",
5724 end if;
5725 end;
5727 -- Avoid marking an allocator as a dynamic coextension if it is
5728 -- within a static construct.
5730 if not Is_Static_Coextension (N) then
5731 Set_Is_Dynamic_Coextension (N);
5733 -- Finalization and deallocation of coextensions utilizes an
5734 -- approximate implementation which does not directly adhere
5735 -- to the semantic rules. Warn on potential issues involving
5736 -- coextensions.
5738 if Is_Controlled (Desig_T) then
5739 Error_Msg_N
5740 ("??coextension will not be finalized when its "
5741 & "associated owner is deallocated or finalized", N);
5742 else
5743 Error_Msg_N
5744 ("??coextension will not be deallocated when its "
5745 & "associated owner is deallocated", N);
5746 end if;
5747 end if;
5749 -- Cleanup for potential static coextensions
5751 else
5752 Set_Is_Dynamic_Coextension (N, False);
5753 Set_Is_Static_Coextension (N, False);
5755 -- Anonymous access-to-controlled objects are not finalized on
5756 -- time because this involves run-time ownership and currently
5757 -- this property is not available. In rare cases the object may
5758 -- not be finalized at all. Warn on potential issues involving
5759 -- anonymous access-to-controlled objects.
5761 if Ekind (Typ) = E_Anonymous_Access_Type
5762 and then Is_Controlled_Active (Desig_T)
5763 then
5764 Error_Msg_N
5765 ("??object designated by anonymous access object might "
5766 & "not be finalized until its enclosing library unit "
5767 & "goes out of scope", N);
5768 Error_Msg_N ("\use named access type instead", N);
5769 end if;
5770 end if;
5771 end if;
5772 end if;
5774 -- Report a simple error: if the designated object is a local task,
5775 -- its body has not been seen yet, and its activation will fail an
5776 -- elaboration check.
5778 if Is_Task_Type (Desig_T)
5779 and then Scope (Base_Type (Desig_T)) = Current_Scope
5780 and then Is_Compilation_Unit (Current_Scope)
5781 and then Ekind (Current_Scope) = E_Package
5782 and then not In_Package_Body (Current_Scope)
5783 then
5784 Error_Msg_Warn := SPARK_Mode /= On;
5785 Error_Msg_N ("cannot activate task before body seen<<", N);
5786 Error_Msg_N ("\Program_Error [<<", N);
5787 end if;
5789 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5790 -- type with a task component on a subpool. This action must raise
5791 -- Program_Error at runtime.
5793 if Ada_Version >= Ada_2012
5794 and then Nkind (N) = N_Allocator
5795 and then Present (Subpool_Handle_Name (N))
5796 and then Has_Task (Desig_T)
5797 then
5798 Error_Msg_Warn := SPARK_Mode /= On;
5799 Error_Msg_N ("cannot allocate task on subpool<<", N);
5800 Error_Msg_N ("\Program_Error [<<", N);
5802 Rewrite (N,
5803 Make_Raise_Program_Error (Sloc (N),
5804 Reason => PE_Explicit_Raise));
5805 Set_Etype (N, Typ);
5806 end if;
5807 end Resolve_Allocator;
5809 ---------------------------
5810 -- Resolve_Arithmetic_Op --
5811 ---------------------------
5813 -- Used for resolving all arithmetic operators except exponentiation
5815 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5816 L : constant Node_Id := Left_Opnd (N);
5817 R : constant Node_Id := Right_Opnd (N);
5818 TL : constant Entity_Id := Base_Type (Etype (L));
5819 TR : constant Entity_Id := Base_Type (Etype (R));
5820 T : Entity_Id;
5821 Rop : Node_Id;
5823 B_Typ : constant Entity_Id := Base_Type (Typ);
5824 -- We do the resolution using the base type, because intermediate values
5825 -- in expressions always are of the base type, not a subtype of it.
5827 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5828 -- Returns True if N is in a context that expects "any real type"
5830 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5831 -- Return True iff given type is Integer or universal real/integer
5833 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5834 -- Choose type of integer literal in fixed-point operation to conform
5835 -- to available fixed-point type. T is the type of the other operand,
5836 -- which is needed to determine the expected type of N.
5838 procedure Set_Operand_Type (N : Node_Id);
5839 -- Set operand type to T if universal
5841 -------------------------------
5842 -- Expected_Type_Is_Any_Real --
5843 -------------------------------
5845 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5846 begin
5847 -- N is the expression after "delta" in a fixed_point_definition;
5848 -- see RM-3.5.9(6):
5850 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5851 | N_Decimal_Fixed_Point_Definition
5853 -- N is one of the bounds in a real_range_specification;
5854 -- see RM-3.5.7(5):
5856 | N_Real_Range_Specification
5858 -- N is the expression of a delta_constraint;
5859 -- see RM-J.3(3):
5861 | N_Delta_Constraint;
5862 end Expected_Type_Is_Any_Real;
5864 -----------------------------
5865 -- Is_Integer_Or_Universal --
5866 -----------------------------
5868 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5869 T : Entity_Id;
5870 Index : Interp_Index;
5871 It : Interp;
5873 begin
5874 if not Is_Overloaded (N) then
5875 T := Etype (N);
5876 return Base_Type (T) = Base_Type (Standard_Integer)
5877 or else Is_Universal_Numeric_Type (T);
5878 else
5879 Get_First_Interp (N, Index, It);
5880 while Present (It.Typ) loop
5881 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5882 or else Is_Universal_Numeric_Type (It.Typ)
5883 then
5884 return True;
5885 end if;
5887 Get_Next_Interp (Index, It);
5888 end loop;
5889 end if;
5891 return False;
5892 end Is_Integer_Or_Universal;
5894 ----------------------------
5895 -- Set_Mixed_Mode_Operand --
5896 ----------------------------
5898 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5899 Index : Interp_Index;
5900 It : Interp;
5902 begin
5903 if Universal_Interpretation (N) = Universal_Integer then
5905 -- A universal integer literal is resolved as standard integer
5906 -- except in the case of a fixed-point result, where we leave it
5907 -- as universal (to be handled by Exp_Fixd later on)
5909 if Is_Fixed_Point_Type (T) then
5910 Resolve (N, Universal_Integer);
5911 else
5912 Resolve (N, Standard_Integer);
5913 end if;
5915 elsif Universal_Interpretation (N) = Universal_Real
5916 and then (T = Base_Type (Standard_Integer)
5917 or else Is_Universal_Numeric_Type (T))
5918 then
5919 -- A universal real can appear in a fixed-type context. We resolve
5920 -- the literal with that context, even though this might raise an
5921 -- exception prematurely (the other operand may be zero).
5923 Resolve (N, B_Typ);
5925 elsif Etype (N) = Base_Type (Standard_Integer)
5926 and then T = Universal_Real
5927 and then Is_Overloaded (N)
5928 then
5929 -- Integer arg in mixed-mode operation. Resolve with universal
5930 -- type, in case preference rule must be applied.
5932 Resolve (N, Universal_Integer);
5934 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5936 -- If the operand is part of a fixed multiplication operation,
5937 -- a conversion will be applied to each operand, so resolve it
5938 -- with its own type.
5940 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
5941 Resolve (N);
5943 else
5944 -- Not a mixed-mode operation, resolve with context
5946 Resolve (N, B_Typ);
5947 end if;
5949 elsif Etype (N) = Any_Fixed then
5951 -- N may itself be a mixed-mode operation, so use context type
5953 Resolve (N, B_Typ);
5955 elsif Is_Fixed_Point_Type (T)
5956 and then B_Typ = Universal_Fixed
5957 and then Is_Overloaded (N)
5958 then
5959 -- Must be (fixed * fixed) operation, operand must have one
5960 -- compatible interpretation.
5962 Resolve (N, Any_Fixed);
5964 elsif Is_Fixed_Point_Type (B_Typ)
5965 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5966 and then Is_Overloaded (N)
5967 then
5968 -- C * F(X) in a fixed context, where C is a real literal or a
5969 -- fixed-point expression. F must have either a fixed type
5970 -- interpretation or an integer interpretation, but not both.
5972 Get_First_Interp (N, Index, It);
5973 while Present (It.Typ) loop
5974 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5975 if Analyzed (N) then
5976 Error_Msg_N ("ambiguous operand in fixed operation", N);
5977 else
5978 Resolve (N, Standard_Integer);
5979 end if;
5981 elsif Is_Fixed_Point_Type (It.Typ) then
5982 if Analyzed (N) then
5983 Error_Msg_N ("ambiguous operand in fixed operation", N);
5984 else
5985 Resolve (N, It.Typ);
5986 end if;
5987 end if;
5989 Get_Next_Interp (Index, It);
5990 end loop;
5992 -- Reanalyze the literal with the fixed type of the context. If
5993 -- context is Universal_Fixed, we are within a conversion, leave
5994 -- the literal as a universal real because there is no usable
5995 -- fixed type, and the target of the conversion plays no role in
5996 -- the resolution.
5998 declare
5999 Op2 : Node_Id;
6000 T2 : Entity_Id;
6002 begin
6003 if N = L then
6004 Op2 := R;
6005 else
6006 Op2 := L;
6007 end if;
6009 if B_Typ = Universal_Fixed
6010 and then Nkind (Op2) = N_Real_Literal
6011 then
6012 T2 := Universal_Real;
6013 else
6014 T2 := B_Typ;
6015 end if;
6017 Set_Analyzed (Op2, False);
6018 Resolve (Op2, T2);
6019 end;
6021 -- A universal real conditional expression can appear in a fixed-type
6022 -- context and must be resolved with that context to facilitate the
6023 -- code generation in the back end. However, If the context is
6024 -- Universal_fixed (i.e. as an operand of a multiplication/division
6025 -- involving a fixed-point operand) the conditional expression must
6026 -- resolve to a unique visible fixed_point type, normally Duration.
6028 elsif Nkind (N) in N_Case_Expression | N_If_Expression
6029 and then Etype (N) = Universal_Real
6030 and then Is_Fixed_Point_Type (B_Typ)
6031 then
6032 if B_Typ = Universal_Fixed then
6033 Resolve (N, Unique_Fixed_Point_Type (N));
6035 else
6036 Resolve (N, B_Typ);
6037 end if;
6039 else
6040 Resolve (N);
6041 end if;
6042 end Set_Mixed_Mode_Operand;
6044 ----------------------
6045 -- Set_Operand_Type --
6046 ----------------------
6048 procedure Set_Operand_Type (N : Node_Id) is
6049 begin
6050 if Is_Universal_Numeric_Type (Etype (N)) then
6051 Set_Etype (N, T);
6052 end if;
6053 end Set_Operand_Type;
6055 -- Start of processing for Resolve_Arithmetic_Op
6057 begin
6058 if Ekind (Entity (N)) = E_Function
6059 and then Is_Imported (Entity (N))
6060 and then Is_Intrinsic_Subprogram (Entity (N))
6061 then
6062 Generate_Reference (Entity (N), N);
6063 Resolve_Intrinsic_Operator (N, Typ);
6064 return;
6066 -- Special-case for mixed-mode universal expressions or fixed point type
6067 -- operation: each argument is resolved separately. The same treatment
6068 -- is required if one of the operands of a fixed point operation is
6069 -- universal real, since in this case we don't do a conversion to a
6070 -- specific fixed-point type (instead the expander handles the case).
6072 -- Set the type of the node to its universal interpretation because
6073 -- legality checks on an exponentiation operand need the context.
6075 elsif Is_Universal_Numeric_Type (B_Typ)
6076 and then Present (Universal_Interpretation (L))
6077 and then Present (Universal_Interpretation (R))
6078 then
6079 Set_Etype (N, B_Typ);
6080 Resolve (L, Universal_Interpretation (L));
6081 Resolve (R, Universal_Interpretation (R));
6083 elsif (B_Typ = Universal_Real
6084 or else Etype (N) = Universal_Fixed
6085 or else (Etype (N) = Any_Fixed
6086 and then Is_Fixed_Point_Type (B_Typ))
6087 or else (Is_Fixed_Point_Type (B_Typ)
6088 and then (Is_Integer_Or_Universal (L)
6089 or else
6090 Is_Integer_Or_Universal (R))))
6091 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
6092 then
6093 if TL = Universal_Integer or else TR = Universal_Integer then
6094 Check_For_Visible_Operator (N, B_Typ);
6095 end if;
6097 -- If context is a fixed type and one operand is integer, the other
6098 -- is resolved with the type of the context.
6100 if Is_Fixed_Point_Type (B_Typ)
6101 and then (Base_Type (TL) = Base_Type (Standard_Integer)
6102 or else TL = Universal_Integer)
6103 then
6104 Resolve (R, B_Typ);
6105 Resolve (L, TL);
6107 elsif Is_Fixed_Point_Type (B_Typ)
6108 and then (Base_Type (TR) = Base_Type (Standard_Integer)
6109 or else TR = Universal_Integer)
6110 then
6111 Resolve (L, B_Typ);
6112 Resolve (R, TR);
6114 -- If both operands are universal and the context is a floating
6115 -- point type, the operands are resolved to the type of the context.
6117 elsif Is_Floating_Point_Type (B_Typ) then
6118 Resolve (L, B_Typ);
6119 Resolve (R, B_Typ);
6121 else
6122 Set_Mixed_Mode_Operand (L, TR);
6123 Set_Mixed_Mode_Operand (R, TL);
6124 end if;
6126 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6127 -- multiplying operators from being used when the expected type is
6128 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6129 -- some cases where the expected type is actually Any_Real;
6130 -- Expected_Type_Is_Any_Real takes care of that case.
6132 if Etype (N) = Universal_Fixed
6133 or else Etype (N) = Any_Fixed
6134 then
6135 if B_Typ = Universal_Fixed
6136 and then not Expected_Type_Is_Any_Real (N)
6137 and then Nkind (Parent (N)) not in
6138 N_Type_Conversion | N_Unchecked_Type_Conversion
6139 then
6140 Error_Msg_N ("type cannot be determined from context!", N);
6141 Error_Msg_N ("\explicit conversion to result type required", N);
6143 Set_Etype (L, Any_Type);
6144 Set_Etype (R, Any_Type);
6146 else
6147 if Ada_Version = Ada_83
6148 and then Etype (N) = Universal_Fixed
6149 and then Nkind (Parent (N)) not in
6150 N_Type_Conversion | N_Unchecked_Type_Conversion
6151 then
6152 Error_Msg_N
6153 ("(Ada 83) fixed-point operation needs explicit "
6154 & "conversion", N);
6155 end if;
6157 -- The expected type is "any real type" in contexts like
6159 -- type T is delta <universal_fixed-expression> ...
6161 -- in which case we need to set the type to Universal_Real
6162 -- so that static expression evaluation will work properly.
6164 if Expected_Type_Is_Any_Real (N) then
6165 Set_Etype (N, Universal_Real);
6166 else
6167 Set_Etype (N, B_Typ);
6168 end if;
6169 end if;
6171 elsif Is_Fixed_Point_Type (B_Typ)
6172 and then (Is_Integer_Or_Universal (L)
6173 or else Nkind (L) = N_Real_Literal
6174 or else Nkind (R) = N_Real_Literal
6175 or else Is_Integer_Or_Universal (R))
6176 then
6177 Set_Etype (N, B_Typ);
6179 elsif Etype (N) = Any_Fixed then
6181 -- If no previous errors, this is only possible if one operand is
6182 -- overloaded and the context is universal. Resolve as such.
6184 Set_Etype (N, B_Typ);
6185 end if;
6187 else
6188 if Is_Universal_Numeric_Type (TL)
6189 and then
6190 Is_Universal_Numeric_Type (TR)
6191 then
6192 Check_For_Visible_Operator (N, B_Typ);
6193 end if;
6195 -- If the context is Universal_Fixed and the operands are also
6196 -- universal fixed, this is an error, unless there is only one
6197 -- applicable fixed_point type (usually Duration).
6199 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6200 T := Unique_Fixed_Point_Type (N);
6202 if T = Any_Type then
6203 Set_Etype (N, T);
6204 return;
6205 else
6206 Resolve (L, T);
6207 Resolve (R, T);
6208 end if;
6210 else
6211 Resolve (L, B_Typ);
6212 Resolve (R, B_Typ);
6213 end if;
6215 -- If one of the arguments was resolved to a non-universal type.
6216 -- label the result of the operation itself with the same type.
6217 -- Do the same for the universal argument, if any.
6219 T := Intersect_Types (L, R);
6220 Set_Etype (N, Base_Type (T));
6221 Set_Operand_Type (L);
6222 Set_Operand_Type (R);
6223 end if;
6225 Generate_Operator_Reference (N, Typ);
6226 Analyze_Dimension (N);
6227 Eval_Arithmetic_Op (N);
6229 -- Set overflow and division checking bit
6231 if Nkind (N) in N_Op then
6232 if not Overflow_Checks_Suppressed (Etype (N)) then
6233 Enable_Overflow_Check (N);
6234 end if;
6236 -- Give warning if explicit division by zero
6238 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6239 and then not Division_Checks_Suppressed (Etype (N))
6240 then
6241 Rop := Right_Opnd (N);
6243 if Compile_Time_Known_Value (Rop)
6244 and then ((Is_Integer_Type (Etype (Rop))
6245 and then Expr_Value (Rop) = Uint_0)
6246 or else
6247 (Is_Real_Type (Etype (Rop))
6248 and then Expr_Value_R (Rop) = Ureal_0))
6249 then
6250 -- Specialize the warning message according to the operation.
6251 -- When SPARK_Mode is On, force a warning instead of an error
6252 -- in that case, as this likely corresponds to deactivated
6253 -- code. The following warnings are for the case
6255 case Nkind (N) is
6256 when N_Op_Divide =>
6258 -- For division, we have two cases, for float division
6259 -- of an unconstrained float type, on a machine where
6260 -- Machine_Overflows is false, we don't get an exception
6261 -- at run-time, but rather an infinity or Nan. The Nan
6262 -- case is pretty obscure, so just warn about infinities.
6264 if Is_Floating_Point_Type (Typ)
6265 and then not Is_Constrained (Typ)
6266 and then not Machine_Overflows_On_Target
6267 then
6268 Error_Msg_N
6269 ("float division by zero, may generate "
6270 & "'+'/'- infinity??", Right_Opnd (N));
6272 -- For all other cases, we get a Constraint_Error
6274 else
6275 Apply_Compile_Time_Constraint_Error
6276 (N, "division by zero??", CE_Divide_By_Zero,
6277 Loc => Sloc (Right_Opnd (N)),
6278 Warn => SPARK_Mode = On);
6279 end if;
6281 when N_Op_Rem =>
6282 Apply_Compile_Time_Constraint_Error
6283 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6284 Loc => Sloc (Right_Opnd (N)),
6285 Warn => SPARK_Mode = On);
6287 when N_Op_Mod =>
6288 Apply_Compile_Time_Constraint_Error
6289 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6290 Loc => Sloc (Right_Opnd (N)),
6291 Warn => SPARK_Mode = On);
6293 -- Division by zero can only happen with division, rem,
6294 -- and mod operations.
6296 when others =>
6297 raise Program_Error;
6298 end case;
6300 -- Otherwise just set the flag to check at run time
6302 else
6303 Activate_Division_Check (N);
6304 end if;
6305 end if;
6307 -- If Restriction No_Implicit_Conditionals is active, then it is
6308 -- violated if either operand can be negative for mod, or for rem
6309 -- if both operands can be negative.
6311 if Restriction_Check_Required (No_Implicit_Conditionals)
6312 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6313 then
6314 declare
6315 Lo : Uint;
6316 Hi : Uint;
6317 OK : Boolean;
6319 LNeg : Boolean;
6320 RNeg : Boolean;
6321 -- Set if corresponding operand might be negative
6323 begin
6324 Determine_Range
6325 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6326 LNeg := not OK or else Lo < 0;
6328 Determine_Range
6329 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6330 RNeg := not OK or else Lo < 0;
6332 -- Check if we will be generating conditionals. There are two
6333 -- cases where that can happen, first for REM, the only case
6334 -- is largest negative integer mod -1, where the division can
6335 -- overflow, but we still have to give the right result. The
6336 -- front end generates a test for this annoying case. Here we
6337 -- just test if both operands can be negative (that's what the
6338 -- expander does, so we match its logic here).
6340 -- The second case is mod where either operand can be negative.
6341 -- In this case, the back end has to generate additional tests.
6343 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6344 or else
6345 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6346 then
6347 Check_Restriction (No_Implicit_Conditionals, N);
6348 end if;
6349 end;
6350 end if;
6351 end if;
6353 Check_Unset_Reference (L);
6354 Check_Unset_Reference (R);
6355 end Resolve_Arithmetic_Op;
6357 ------------------
6358 -- Resolve_Call --
6359 ------------------
6361 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6362 Loc : constant Source_Ptr := Sloc (N);
6363 Subp : constant Node_Id := Name (N);
6364 Body_Id : Entity_Id;
6365 I : Interp_Index;
6366 It : Interp;
6367 Nam : Entity_Id;
6368 Nam_Decl : Node_Id;
6369 Nam_UA : Entity_Id;
6370 Norm_OK : Boolean;
6371 Rtype : Entity_Id;
6372 Scop : Entity_Id;
6374 begin
6375 -- Preserve relevant elaboration-related attributes of the context which
6376 -- are no longer available or very expensive to recompute once analysis,
6377 -- resolution, and expansion are over.
6379 Mark_Elaboration_Attributes
6380 (N_Id => N,
6381 Checks => True,
6382 Modes => True,
6383 Warnings => True);
6385 -- The context imposes a unique interpretation with type Typ on a
6386 -- procedure or function call. Find the entity of the subprogram that
6387 -- yields the expected type, and propagate the corresponding formal
6388 -- constraints on the actuals. The caller has established that an
6389 -- interpretation exists, and emitted an error if not unique.
6391 -- First deal with the case of a call to an access-to-subprogram,
6392 -- dereference made explicit in Analyze_Call.
6394 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6395 if not Is_Overloaded (Subp) then
6396 Nam := Etype (Subp);
6398 else
6399 -- Find the interpretation whose type (a subprogram type) has a
6400 -- return type that is compatible with the context. Analysis of
6401 -- the node has established that one exists.
6403 Nam := Empty;
6405 Get_First_Interp (Subp, I, It);
6406 while Present (It.Typ) loop
6407 if Covers (Typ, Etype (It.Typ)) then
6408 Nam := It.Typ;
6409 exit;
6410 end if;
6412 Get_Next_Interp (I, It);
6413 end loop;
6415 if No (Nam) then
6416 raise Program_Error;
6417 end if;
6418 end if;
6420 -- If the prefix is not an entity, then resolve it
6422 if not Is_Entity_Name (Subp) then
6423 Resolve (Subp, Nam);
6424 end if;
6426 -- For an indirect call, we always invalidate checks, since we do not
6427 -- know whether the subprogram is local or global. Yes we could do
6428 -- better here, e.g. by knowing that there are no local subprograms,
6429 -- but it does not seem worth the effort. Similarly, we kill all
6430 -- knowledge of current constant values.
6432 Kill_Current_Values;
6434 -- If this is a procedure call which is really an entry call, do
6435 -- the conversion of the procedure call to an entry call. Protected
6436 -- operations use the same circuitry because the name in the call
6437 -- can be an arbitrary expression with special resolution rules.
6439 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6440 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6441 then
6442 Resolve_Entry_Call (N, Typ);
6444 if Legacy_Elaboration_Checks then
6445 Check_Elab_Call (N);
6446 end if;
6448 -- Annotate the tree by creating a call marker in case the original
6449 -- call is transformed by expansion. The call marker is automatically
6450 -- saved for later examination by the ABE Processing phase.
6452 Build_Call_Marker (N);
6454 -- Kill checks and constant values, as above for indirect case
6455 -- Who knows what happens when another task is activated?
6457 Kill_Current_Values;
6458 return;
6460 -- Normal subprogram call with name established in Resolve
6462 elsif not Is_Type (Entity (Subp)) then
6463 Nam := Entity (Subp);
6464 Set_Entity_With_Checks (Subp, Nam);
6466 -- Otherwise we must have the case of an overloaded call
6468 else
6469 pragma Assert (Is_Overloaded (Subp));
6471 -- Initialize Nam to prevent warning (we know it will be assigned
6472 -- in the loop below, but the compiler does not know that).
6474 Nam := Empty;
6476 Get_First_Interp (Subp, I, It);
6477 while Present (It.Typ) loop
6478 if Covers (Typ, It.Typ) then
6479 Nam := It.Nam;
6480 Set_Entity_With_Checks (Subp, Nam);
6481 exit;
6482 end if;
6484 Get_Next_Interp (I, It);
6485 end loop;
6486 end if;
6488 -- Check that a call to Current_Task does not occur in an entry body
6490 if Is_RTE (Nam, RE_Current_Task) then
6491 declare
6492 P : Node_Id;
6494 begin
6495 P := N;
6496 loop
6497 P := Parent (P);
6499 -- Exclude calls that occur within the default of a formal
6500 -- parameter of the entry, since those are evaluated outside
6501 -- of the body.
6503 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6505 if Nkind (P) = N_Entry_Body
6506 or else (Nkind (P) = N_Subprogram_Body
6507 and then Is_Entry_Barrier_Function (P))
6508 then
6509 Rtype := Etype (N);
6510 Error_Msg_Warn := SPARK_Mode /= On;
6511 Error_Msg_NE
6512 ("& should not be used in entry body (RM C.7(17))<<",
6513 N, Nam);
6514 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6515 Rewrite (N,
6516 Make_Raise_Program_Error (Loc,
6517 Reason => PE_Current_Task_In_Entry_Body));
6518 Set_Etype (N, Rtype);
6519 return;
6520 end if;
6521 end loop;
6522 end;
6523 end if;
6525 -- Check that a procedure call does not occur in the context of the
6526 -- entry call statement of a conditional or timed entry call. Note that
6527 -- the case of a call to a subprogram renaming of an entry will also be
6528 -- rejected. The test for N not being an N_Entry_Call_Statement is
6529 -- defensive, covering the possibility that the processing of entry
6530 -- calls might reach this point due to later modifications of the code
6531 -- above.
6533 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6534 and then Nkind (N) /= N_Entry_Call_Statement
6535 and then Entry_Call_Statement (Parent (N)) = N
6536 then
6537 if Ada_Version < Ada_2005 then
6538 Error_Msg_N ("entry call required in select statement", N);
6540 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6541 -- for a procedure_or_entry_call, the procedure_name or
6542 -- procedure_prefix of the procedure_call_statement shall denote
6543 -- an entry renamed by a procedure, or (a view of) a primitive
6544 -- subprogram of a limited interface whose first parameter is
6545 -- a controlling parameter.
6547 elsif Nkind (N) = N_Procedure_Call_Statement
6548 and then not Is_Renamed_Entry (Nam)
6549 and then not Is_Controlling_Limited_Procedure (Nam)
6550 then
6551 Error_Msg_N
6552 ("entry call or dispatching primitive of interface required", N);
6553 end if;
6554 end if;
6556 -- Check that this is not a call to a protected procedure or entry from
6557 -- within a protected function.
6559 Check_Internal_Protected_Use (N, Nam);
6561 -- Freeze the subprogram name if not in a spec-expression. Note that
6562 -- we freeze procedure calls as well as function calls. Procedure calls
6563 -- are not frozen according to the rules (RM 13.14(14)) because it is
6564 -- impossible to have a procedure call to a non-frozen procedure in
6565 -- pure Ada, but in the code that we generate in the expander, this
6566 -- rule needs extending because we can generate procedure calls that
6567 -- need freezing.
6569 -- In Ada 2012, expression functions may be called within pre/post
6570 -- conditions of subsequent functions or expression functions. Such
6571 -- calls do not freeze when they appear within generated bodies,
6572 -- (including the body of another expression function) which would
6573 -- place the freeze node in the wrong scope. An expression function
6574 -- is frozen in the usual fashion, by the appearance of a real body,
6575 -- or at the end of a declarative part. However an implicit call to
6576 -- an expression function may appear when it is part of a default
6577 -- expression in a call to an initialization procedure, and must be
6578 -- frozen now, even if the body is inserted at a later point.
6579 -- Otherwise, the call freezes the expression if expander is active,
6580 -- for example as part of an object declaration.
6582 if Is_Entity_Name (Subp)
6583 and then not In_Spec_Expression
6584 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6585 and then
6586 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6587 or else Expander_Active)
6588 then
6589 if Is_Expression_Function (Entity (Subp)) then
6591 -- Force freeze of expression function in call
6593 Set_Comes_From_Source (Subp, True);
6594 Set_Must_Not_Freeze (Subp, False);
6595 end if;
6597 Freeze_Expression (Subp);
6598 end if;
6600 -- For a predefined operator, the type of the result is the type imposed
6601 -- by context, except for a predefined operation on universal fixed.
6602 -- Otherwise the type of the call is the type returned by the subprogram
6603 -- being called.
6605 if Is_Predefined_Op (Nam) then
6606 if Etype (N) /= Universal_Fixed then
6607 Set_Etype (N, Typ);
6608 end if;
6610 -- If the subprogram returns an array type, and the context requires the
6611 -- component type of that array type, the node is really an indexing of
6612 -- the parameterless call. Resolve as such. A pathological case occurs
6613 -- when the type of the component is an access to the array type. In
6614 -- this case the call is truly ambiguous. If the call is to an intrinsic
6615 -- subprogram, it can't be an indexed component. This check is necessary
6616 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6617 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6618 -- pointers to the same array), the compiler gets confused and does an
6619 -- infinite recursion.
6621 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6622 and then
6623 ((Is_Array_Type (Etype (Nam))
6624 and then Covers (Typ, Component_Type (Etype (Nam))))
6625 or else
6626 (Is_Access_Type (Etype (Nam))
6627 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6628 and then
6629 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6630 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6631 then
6632 declare
6633 Index_Node : Node_Id;
6634 New_Subp : Node_Id;
6635 Ret_Type : constant Entity_Id := Etype (Nam);
6637 begin
6638 -- If this is a parameterless call there is no ambiguity and the
6639 -- call has the type of the function.
6641 if No (First_Actual (N)) then
6642 Set_Etype (N, Etype (Nam));
6644 if Present (First_Formal (Nam)) then
6645 Resolve_Actuals (N, Nam);
6646 end if;
6648 -- Annotate the tree by creating a call marker in case the
6649 -- original call is transformed by expansion. The call marker
6650 -- is automatically saved for later examination by the ABE
6651 -- Processing phase.
6653 Build_Call_Marker (N);
6655 elsif Is_Access_Type (Ret_Type)
6657 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6658 then
6659 Error_Msg_N
6660 ("cannot disambiguate function call and indexing", N);
6661 else
6662 New_Subp := Relocate_Node (Subp);
6664 -- The called entity may be an explicit dereference, in which
6665 -- case there is no entity to set.
6667 if Nkind (New_Subp) /= N_Explicit_Dereference then
6668 Set_Entity (Subp, Nam);
6669 end if;
6671 if (Is_Array_Type (Ret_Type)
6672 and then Component_Type (Ret_Type) /= Any_Type)
6673 or else
6674 (Is_Access_Type (Ret_Type)
6675 and then
6676 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6677 then
6678 if Needs_No_Actuals (Nam) then
6680 -- Indexed call to a parameterless function
6682 Index_Node :=
6683 Make_Indexed_Component (Loc,
6684 Prefix =>
6685 Make_Function_Call (Loc, Name => New_Subp),
6686 Expressions => Parameter_Associations (N));
6687 else
6688 -- An Ada 2005 prefixed call to a primitive operation
6689 -- whose first parameter is the prefix. This prefix was
6690 -- prepended to the parameter list, which is actually a
6691 -- list of indexes. Remove the prefix in order to build
6692 -- the proper indexed component.
6694 Index_Node :=
6695 Make_Indexed_Component (Loc,
6696 Prefix =>
6697 Make_Function_Call (Loc,
6698 Name => New_Subp,
6699 Parameter_Associations =>
6700 New_List
6701 (Remove_Head (Parameter_Associations (N)))),
6702 Expressions => Parameter_Associations (N));
6703 end if;
6705 -- Preserve the parenthesis count of the node
6707 Set_Paren_Count (Index_Node, Paren_Count (N));
6709 -- Since we are correcting a node classification error made
6710 -- by the parser, we call Replace rather than Rewrite.
6712 Replace (N, Index_Node);
6714 Set_Etype (Prefix (N), Ret_Type);
6715 Set_Etype (N, Typ);
6717 if Legacy_Elaboration_Checks then
6718 Check_Elab_Call (Prefix (N));
6719 end if;
6721 -- Annotate the tree by creating a call marker in case
6722 -- the original call is transformed by expansion. The call
6723 -- marker is automatically saved for later examination by
6724 -- the ABE Processing phase.
6726 Build_Call_Marker (Prefix (N));
6728 Resolve_Indexed_Component (N, Typ);
6729 end if;
6730 end if;
6732 return;
6733 end;
6735 else
6736 -- If the called function is not declared in the main unit and it
6737 -- returns the limited view of type then use the available view (as
6738 -- is done in Try_Object_Operation) to prevent back-end confusion;
6739 -- for the function entity itself. The call must appear in a context
6740 -- where the nonlimited view is available. If the function entity is
6741 -- in the extended main unit then no action is needed, because the
6742 -- back end handles this case. In either case the type of the call
6743 -- is the nonlimited view.
6745 if From_Limited_With (Etype (Nam))
6746 and then Present (Available_View (Etype (Nam)))
6747 then
6748 Set_Etype (N, Available_View (Etype (Nam)));
6750 if not In_Extended_Main_Code_Unit (Nam) then
6751 Set_Etype (Nam, Available_View (Etype (Nam)));
6752 end if;
6754 else
6755 Set_Etype (N, Etype (Nam));
6756 end if;
6757 end if;
6759 -- In the case where the call is to an overloaded subprogram, Analyze
6760 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6761 -- such a case Normalize_Actuals needs to be called once more to order
6762 -- the actuals correctly. Otherwise the call will have the ordering
6763 -- given by the last overloaded subprogram whether this is the correct
6764 -- one being called or not.
6766 if Is_Overloaded (Subp) then
6767 Normalize_Actuals (N, Nam, False, Norm_OK);
6768 pragma Assert (Norm_OK);
6769 end if;
6771 -- In any case, call is fully resolved now. Reset Overload flag, to
6772 -- prevent subsequent overload resolution if node is analyzed again
6774 Set_Is_Overloaded (Subp, False);
6775 Set_Is_Overloaded (N, False);
6777 -- A Ghost entity must appear in a specific context
6779 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6780 Check_Ghost_Context (Nam, N);
6781 end if;
6783 -- If we are calling the current subprogram from immediately within its
6784 -- body, then that is the case where we can sometimes detect cases of
6785 -- infinite recursion statically. Do not try this in case restriction
6786 -- No_Recursion is in effect anyway, and do it only for source calls.
6788 if Comes_From_Source (N) then
6789 Scop := Current_Scope;
6791 -- Issue warning for possible infinite recursion in the absence
6792 -- of the No_Recursion restriction.
6794 if Same_Or_Aliased_Subprograms (Nam, Scop)
6795 and then not Restriction_Active (No_Recursion)
6796 and then not Is_Static_Function (Scop)
6797 and then Check_Infinite_Recursion (N)
6798 then
6799 -- Here we detected and flagged an infinite recursion, so we do
6800 -- not need to test the case below for further warnings. Also we
6801 -- are all done if we now have a raise SE node.
6803 if Nkind (N) = N_Raise_Storage_Error then
6804 return;
6805 end if;
6807 -- If call is to immediately containing subprogram, then check for
6808 -- the case of a possible run-time detectable infinite recursion.
6810 else
6811 Scope_Loop : while Scop /= Standard_Standard loop
6812 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6814 -- Ada 2022 (AI12-0075): Static functions are never allowed
6815 -- to make a recursive call, as specified by 6.8(5.4/5).
6817 if Is_Static_Function (Scop) then
6818 Error_Msg_N
6819 ("recursive call not allowed in static expression "
6820 & "function", N);
6822 Set_Error_Posted (Scop);
6824 exit Scope_Loop;
6825 end if;
6827 -- Although in general case, recursion is not statically
6828 -- checkable, the case of calling an immediately containing
6829 -- subprogram is easy to catch.
6831 if not Is_Ignored_Ghost_Entity (Nam) then
6832 Check_Restriction (No_Recursion, N);
6833 end if;
6835 -- If the recursive call is to a parameterless subprogram,
6836 -- then even if we can't statically detect infinite
6837 -- recursion, this is pretty suspicious, and we output a
6838 -- warning. Furthermore, we will try later to detect some
6839 -- cases here at run time by expanding checking code (see
6840 -- Detect_Infinite_Recursion in package Exp_Ch6).
6842 -- If the recursive call is within a handler, do not emit a
6843 -- warning, because this is a common idiom: loop until input
6844 -- is correct, catch illegal input in handler and restart.
6846 if No (First_Formal (Nam))
6847 and then Etype (Nam) = Standard_Void_Type
6848 and then not Error_Posted (N)
6849 and then Nkind (Parent (N)) /= N_Exception_Handler
6850 then
6851 -- For the case of a procedure call. We give the message
6852 -- only if the call is the first statement in a sequence
6853 -- of statements, or if all previous statements are
6854 -- simple assignments. This is simply a heuristic to
6855 -- decrease false positives, without losing too many good
6856 -- warnings. The idea is that these previous statements
6857 -- may affect global variables the procedure depends on.
6858 -- We also exclude raise statements, that may arise from
6859 -- constraint checks and are probably unrelated to the
6860 -- intended control flow.
6862 if Nkind (N) = N_Procedure_Call_Statement
6863 and then Is_List_Member (N)
6864 then
6865 declare
6866 P : Node_Id;
6867 begin
6868 P := Prev (N);
6869 while Present (P) loop
6870 if Nkind (P) not in N_Assignment_Statement
6871 | N_Raise_Constraint_Error
6872 then
6873 exit Scope_Loop;
6874 end if;
6876 Prev (P);
6877 end loop;
6878 end;
6879 end if;
6881 -- Do not give warning if we are in a conditional context
6883 declare
6884 K : constant Node_Kind := Nkind (Parent (N));
6885 begin
6886 if (K = N_Loop_Statement
6887 and then Present (Iteration_Scheme (Parent (N))))
6888 or else K = N_If_Statement
6889 or else K = N_Elsif_Part
6890 or else K = N_Case_Statement_Alternative
6891 then
6892 exit Scope_Loop;
6893 end if;
6894 end;
6896 -- Here warning is to be issued
6898 Set_Has_Recursive_Call (Nam);
6899 Error_Msg_Warn := SPARK_Mode /= On;
6900 Error_Msg_N ("possible infinite recursion<<!", N);
6901 Error_Msg_N ("\Storage_Error ]<<!", N);
6902 end if;
6904 exit Scope_Loop;
6905 end if;
6907 Scop := Scope (Scop);
6908 end loop Scope_Loop;
6909 end if;
6910 end if;
6912 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6914 Check_Obsolescent_2005_Entity (Nam, Subp);
6916 -- If subprogram name is a predefined operator, it was given in
6917 -- functional notation. Replace call node with operator node, so
6918 -- that actuals can be resolved appropriately.
6920 if Ekind (Nam) = E_Operator or else Is_Predefined_Op (Nam) then
6921 Make_Call_Into_Operator (N, Typ, Nam);
6922 return;
6924 elsif Present (Alias (Nam)) and then Is_Predefined_Op (Alias (Nam)) then
6925 Resolve_Actuals (N, Nam);
6926 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6927 return;
6928 end if;
6930 -- Create a transient scope if the expander is active and the resulting
6931 -- type requires it.
6933 -- There are several notable exceptions:
6935 -- a) Intrinsic subprograms (Unchecked_Conversion and source info
6936 -- functions) do not use the secondary stack even though the return
6937 -- type may be unconstrained.
6939 -- b) Subprograms that are ignored ghost entities do not return anything
6941 -- c) Calls to a build-in-place function, since such functions may
6942 -- allocate their result directly in a target object, and cases where
6943 -- the result does get allocated in the secondary stack are checked for
6944 -- within the specialized Exp_Ch6 procedures for expanding those
6945 -- build-in-place calls.
6947 -- d) Calls to inlinable expression functions do not use the secondary
6948 -- stack (since the call will be replaced by its returned object).
6950 -- e) If the subprogram is marked Inline, then even if it returns
6951 -- an unconstrained type the call does not require use of the secondary
6952 -- stack. However, inlining will only take place if the body to inline
6953 -- is already present. It may not be available if e.g. the subprogram is
6954 -- declared in a child instance.
6956 -- f) If the subprogram is a static expression function and the call is
6957 -- a static call (the actuals are all static expressions), then we never
6958 -- want to create a transient scope (this could occur in the case of a
6959 -- static string-returning call).
6961 -- g) If the call is the expression of a simple return statement that
6962 -- returns on the same stack, since it will be handled as a tail call
6963 -- by Expand_Simple_Function_Return.
6965 if Expander_Active
6966 and then Ekind (Nam) in E_Function | E_Subprogram_Type
6967 and then Requires_Transient_Scope (Etype (Nam))
6968 and then not Is_Intrinsic_Subprogram (Nam)
6969 and then not Is_Ignored_Ghost_Entity (Nam)
6970 and then not Is_Build_In_Place_Function (Nam)
6971 and then not Is_Inlinable_Expression_Function (Nam)
6972 and then not (Is_Inlined (Nam)
6973 and then Has_Pragma_Inline (Nam)
6974 and then Nkind (Unit_Declaration_Node (Nam)) =
6975 N_Subprogram_Declaration
6976 and then
6977 Present (Body_To_Inline (Unit_Declaration_Node (Nam))))
6978 and then not Is_Static_Function_Call (N)
6979 and then not (Nkind (Parent (N)) = N_Simple_Return_Statement
6980 and then
6981 Needs_Secondary_Stack
6982 (Etype
6983 (Return_Applies_To
6984 (Return_Statement_Entity (Parent (N))))) =
6985 Needs_Secondary_Stack (Etype (Nam)))
6986 then
6987 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
6989 -- If the call appears within the bounds of a loop, it will be
6990 -- rewritten and reanalyzed, nothing left to do here.
6992 if Nkind (N) /= N_Function_Call then
6993 return;
6994 end if;
6995 end if;
6997 -- A protected function cannot be called within the definition of the
6998 -- enclosing protected type, unless it is part of a pre/postcondition
6999 -- on another protected operation. This may appear in the entry wrapper
7000 -- created for an entry with preconditions.
7002 if Is_Protected_Type (Scope (Nam))
7003 and then In_Open_Scopes (Scope (Nam))
7004 and then not Has_Completion (Scope (Nam))
7005 and then not In_Spec_Expression
7006 and then not Is_Entry_Wrapper (Current_Scope)
7007 then
7008 Error_Msg_NE
7009 ("& cannot be called before end of protected definition", N, Nam);
7010 end if;
7012 -- Propagate interpretation to actuals, and add default expressions
7013 -- where needed.
7015 if Present (First_Formal (Nam)) then
7016 Resolve_Actuals (N, Nam);
7018 -- Overloaded literals are rewritten as function calls, for purpose of
7019 -- resolution. After resolution, we can replace the call with the
7020 -- literal itself.
7022 elsif Ekind (Nam) = E_Enumeration_Literal then
7023 Copy_Node (Subp, N);
7024 Resolve_Entity_Name (N, Typ);
7026 -- Avoid validation, since it is a static function call
7028 Generate_Reference (Nam, Subp);
7029 return;
7030 end if;
7032 -- If the subprogram is not global, then kill all saved values and
7033 -- checks. This is a bit conservative, since in many cases we could do
7034 -- better, but it is not worth the effort. Similarly, we kill constant
7035 -- values. However we do not need to do this for internal entities
7036 -- (unless they are inherited user-defined subprograms), since they
7037 -- are not in the business of molesting local values.
7039 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
7040 -- kill all checks and values for calls to global subprograms. This
7041 -- takes care of the case where an access to a local subprogram is
7042 -- taken, and could be passed directly or indirectly and then called
7043 -- from almost any context.
7045 -- Note: we do not do this step till after resolving the actuals. That
7046 -- way we still take advantage of the current value information while
7047 -- scanning the actuals.
7049 -- We suppress killing values if we are processing the nodes associated
7050 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
7051 -- type kills all the values as part of analyzing the code that
7052 -- initializes the dispatch tables.
7054 if Inside_Freezing_Actions = 0
7055 and then (not Is_Library_Level_Entity (Nam)
7056 or else Suppress_Value_Tracking_On_Call
7057 (Nearest_Dynamic_Scope (Current_Scope)))
7058 and then (Comes_From_Source (Nam)
7059 or else (Present (Alias (Nam))
7060 and then Comes_From_Source (Alias (Nam))))
7061 then
7062 Kill_Current_Values;
7063 end if;
7065 -- If we are warning about unread OUT parameters, this is the place to
7066 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
7067 -- after the above call to Kill_Current_Values (since that call clears
7068 -- the Last_Assignment field of all local variables).
7070 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
7071 and then Comes_From_Source (N)
7072 and then In_Extended_Main_Source_Unit (N)
7073 then
7074 declare
7075 F : Entity_Id;
7076 A : Node_Id;
7078 begin
7079 F := First_Formal (Nam);
7080 A := First_Actual (N);
7081 while Present (F) and then Present (A) loop
7082 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
7083 and then Warn_On_Modified_As_Out_Parameter (F)
7084 and then Is_Entity_Name (A)
7085 and then Present (Entity (A))
7086 and then Comes_From_Source (N)
7087 and then Safe_To_Capture_Value (N, Entity (A))
7088 then
7089 Set_Last_Assignment (Entity (A), A);
7090 end if;
7092 Next_Formal (F);
7093 Next_Actual (A);
7094 end loop;
7095 end;
7096 end if;
7098 -- If the subprogram is a primitive operation, check whether or not
7099 -- it is a correct dispatching call.
7101 if Is_Overloadable (Nam) and then Is_Dispatching_Operation (Nam) then
7102 Check_Dispatching_Call (N);
7104 -- If the subprogram is an abstract operation, then flag an error
7106 elsif Is_Overloadable (Nam) and then Is_Abstract_Subprogram (Nam) then
7107 Nondispatching_Call_To_Abstract_Operation (N, Nam);
7108 end if;
7110 -- If this is a dispatching call, generate the appropriate reference,
7111 -- for better source navigation in GNAT Studio.
7113 if Is_Overloadable (Nam) and then Present (Controlling_Argument (N)) then
7114 Generate_Reference (Nam, Subp, 'R');
7116 -- Normal case, not a dispatching call: generate a call reference
7118 else
7119 Generate_Reference (Nam, Subp, 's');
7120 end if;
7122 if Is_Intrinsic_Subprogram (Nam) then
7123 Check_Intrinsic_Call (N);
7124 end if;
7126 -- Check for violation of restriction No_Specific_Termination_Handlers
7127 -- and warn on a potentially blocking call to Abort_Task.
7129 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7130 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7131 or else
7132 Is_RTE (Nam, RE_Specific_Handler))
7133 then
7134 Check_Restriction (No_Specific_Termination_Handlers, N);
7136 elsif Is_RTE (Nam, RE_Abort_Task) then
7137 Check_Potentially_Blocking_Operation (N);
7138 end if;
7140 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7141 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7142 -- need to check the second argument to determine whether it is an
7143 -- absolute or relative timing event.
7145 if Restriction_Check_Required (No_Relative_Delay)
7146 and then Is_RTE (Nam, RE_Set_Handler)
7147 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7148 then
7149 Check_Restriction (No_Relative_Delay, N);
7150 end if;
7152 -- Issue an error for a call to an eliminated subprogram. This routine
7153 -- will not perform the check if the call appears within a default
7154 -- expression.
7156 Check_For_Eliminated_Subprogram (Subp, Nam);
7158 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7159 -- class-wide and the call dispatches on result in a context that does
7160 -- not provide a tag, the call raises Program_Error.
7162 if Nkind (N) = N_Function_Call
7163 and then In_Instance
7164 and then Is_Generic_Actual_Type (Typ)
7165 and then Is_Class_Wide_Type (Typ)
7166 and then Has_Controlling_Result (Nam)
7167 and then Nkind (Parent (N)) = N_Object_Declaration
7168 then
7169 -- Verify that none of the formals are controlling
7171 declare
7172 Call_OK : Boolean := False;
7173 F : Entity_Id;
7175 begin
7176 F := First_Formal (Nam);
7177 while Present (F) loop
7178 if Is_Controlling_Formal (F) then
7179 Call_OK := True;
7180 exit;
7181 end if;
7183 Next_Formal (F);
7184 end loop;
7186 if not Call_OK then
7187 Error_Msg_Warn := SPARK_Mode /= On;
7188 Error_Msg_N ("!cannot determine tag of result<<", N);
7189 Error_Msg_N ("\Program_Error [<<!", N);
7190 Insert_Action (N,
7191 Make_Raise_Program_Error (Sloc (N),
7192 Reason => PE_Explicit_Raise));
7193 end if;
7194 end;
7195 end if;
7197 -- Check for calling a function with OUT or IN OUT parameter when the
7198 -- calling context (us right now) is not Ada 2012, so does not allow
7199 -- OUT or IN OUT parameters in function calls. Functions declared in
7200 -- a predefined unit are OK, as they may be called indirectly from a
7201 -- user-declared instantiation.
7203 if Ada_Version < Ada_2012
7204 and then Ekind (Nam) = E_Function
7205 and then Has_Out_Or_In_Out_Parameter (Nam)
7206 and then not In_Predefined_Unit (Nam)
7207 then
7208 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7209 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7210 end if;
7212 -- Check the dimensions of the actuals in the call. For function calls,
7213 -- propagate the dimensions from the returned type to N.
7215 Analyze_Dimension_Call (N, Nam);
7217 -- Check unreachable code after calls to procedures with No_Return
7219 if Ekind (Nam) = E_Procedure and then No_Return (Nam) then
7220 Check_Unreachable_Code (N);
7221 end if;
7223 -- All done, evaluate call and deal with elaboration issues
7225 Eval_Call (N);
7227 if Legacy_Elaboration_Checks then
7228 Check_Elab_Call (N);
7229 end if;
7231 -- Annotate the tree by creating a call marker in case the original call
7232 -- is transformed by expansion. The call marker is automatically saved
7233 -- for later examination by the ABE Processing phase.
7235 Build_Call_Marker (N);
7237 Mark_Use_Clauses (Subp);
7239 Warn_On_Overlapping_Actuals (Nam, N);
7241 -- Ada 2022 (AI12-0075): If the call is a static call to a static
7242 -- expression function, then we want to "inline" the call, replacing
7243 -- it with the folded static result. This is not done if the checking
7244 -- for a potentially static expression is enabled or if an error has
7245 -- been posted on the call (which may be due to the check for recursive
7246 -- calls, in which case we don't want to fall into infinite recursion
7247 -- when doing the inlining).
7249 if not Checking_Potentially_Static_Expression
7250 and then Is_Static_Function_Call (N)
7251 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7252 and then not Error_Posted (Ultimate_Alias (Nam))
7253 then
7254 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7256 -- In GNATprove mode, expansion is disabled, but we want to inline some
7257 -- subprograms to facilitate formal verification. Indirect calls through
7258 -- a subprogram type or within a generic cannot be inlined. Inlining is
7259 -- performed only for calls subject to SPARK_Mode on.
7261 elsif GNATprove_Mode
7262 and then SPARK_Mode = On
7263 and then Is_Overloadable (Nam)
7264 and then not Inside_A_Generic
7265 then
7266 Nam_UA := Ultimate_Alias (Nam);
7267 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7269 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7270 Body_Id := Corresponding_Body (Nam_Decl);
7272 -- Nothing to do if the subprogram is not eligible for inlining in
7273 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7275 if not Is_Inlined_Always (Nam_UA)
7276 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7277 or else Debug_Flag_M
7278 then
7279 null;
7281 -- Calls cannot be inlined inside assertions, as GNATprove treats
7282 -- assertions as logic expressions. Only issue a message when the
7283 -- body has been seen, otherwise this leads to spurious messages
7284 -- on expression functions.
7286 elsif In_Assertion_Expr /= 0 then
7287 Cannot_Inline
7288 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7289 Suppress_Info => No (Body_Id));
7291 -- Calls cannot be inlined inside default expressions
7293 elsif In_Default_Expr then
7294 Cannot_Inline
7295 ("cannot inline & (in default expression)?", N, Nam_UA);
7297 -- Calls cannot be inlined inside potentially unevaluated
7298 -- expressions, as this would create complex actions inside
7299 -- expressions, that are not handled by GNATprove.
7301 elsif Is_Potentially_Unevaluated (N) then
7302 Cannot_Inline
7303 ("cannot inline & (in potentially unevaluated context)?",
7304 N, Nam_UA);
7306 -- Calls are not inlined inside the loop_parameter_specification
7307 -- or iterator_specification of the quantified expression, as they
7308 -- are only preanalyzed. Calls in the predicate part are handled
7309 -- by the previous test on potentially unevaluated expressions.
7311 elsif In_Quantified_Expression (N) then
7312 Cannot_Inline
7313 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7315 -- Inlining should not be performed during preanalysis
7317 elsif Full_Analysis then
7319 -- Do not inline calls inside expression functions or functions
7320 -- generated by the front end for subtype predicates, as this
7321 -- would prevent interpreting them as logical formulas in
7322 -- GNATprove. Only issue a message when the body has been seen,
7323 -- otherwise this leads to spurious messages on callees that
7324 -- are themselves expression functions.
7326 if Present (Current_Subprogram)
7327 and then
7328 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7329 or else Is_Predicate_Function (Current_Subprogram)
7330 or else Is_Invariant_Procedure (Current_Subprogram)
7331 or else Is_DIC_Procedure (Current_Subprogram))
7332 then
7333 declare
7334 Issue_Msg : constant Boolean :=
7335 Present (Body_Id)
7336 and then Present (Body_To_Inline (Nam_Decl));
7337 begin
7338 if Is_Predicate_Function (Current_Subprogram) then
7339 Cannot_Inline
7340 ("cannot inline & (inside predicate)?",
7341 N, Nam_UA, Suppress_Info => not Issue_Msg);
7343 elsif Is_Invariant_Procedure (Current_Subprogram) then
7344 Cannot_Inline
7345 ("cannot inline & (inside invariant)?",
7346 N, Nam_UA, Suppress_Info => not Issue_Msg);
7348 elsif Is_DIC_Procedure (Current_Subprogram) then
7349 Cannot_Inline
7350 ("cannot inline & (inside Default_Initial_Condition)?",
7351 N, Nam_UA, Suppress_Info => not Issue_Msg);
7353 else
7354 Cannot_Inline
7355 ("cannot inline & (inside expression function)?",
7356 N, Nam_UA, Suppress_Info => not Issue_Msg);
7357 end if;
7358 end;
7360 -- Cannot inline a call inside the definition of a record type,
7361 -- typically inside the constraints of the type. Calls in
7362 -- default expressions are also not inlined, but this is
7363 -- filtered out above when testing In_Default_Expr.
7365 elsif Is_Record_Type (Current_Scope) then
7366 Cannot_Inline
7367 ("cannot inline & (inside record type)?", N, Nam_UA);
7369 -- With the one-pass inlining technique, a call cannot be
7370 -- inlined if the corresponding body has not been seen yet.
7372 elsif No (Body_Id) then
7373 Cannot_Inline
7374 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7376 -- Nothing to do if there is no body to inline, indicating that
7377 -- the subprogram is not suitable for inlining in GNATprove
7378 -- mode.
7380 elsif No (Body_To_Inline (Nam_Decl)) then
7381 null;
7383 -- Calls cannot be inlined inside the conditions of while
7384 -- loops, as this would create complex actions inside
7385 -- the condition, that are not handled by GNATprove.
7387 elsif In_Statement_Condition_With_Actions (N) then
7388 Cannot_Inline
7389 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7391 -- Do not inline calls which would possibly lead to missing a
7392 -- type conversion check on an input parameter.
7394 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7395 Cannot_Inline
7396 ("cannot inline & (possible check on input parameters)?",
7397 N, Nam_UA);
7399 -- Otherwise, inline the call, issuing an info message when
7400 -- -gnatd_f is set.
7402 else
7403 if Debug_Flag_Underscore_F then
7404 Error_Msg_NE
7405 ("info: analyzing call to & in context?", N, Nam_UA);
7406 end if;
7408 Expand_Inlined_Call (N, Nam_UA, Nam);
7409 end if;
7410 end if;
7411 end if;
7412 end if;
7413 end Resolve_Call;
7415 -----------------------------
7416 -- Resolve_Case_Expression --
7417 -----------------------------
7419 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7420 Alt : Node_Id;
7421 Alt_Expr : Node_Id;
7422 Alt_Typ : Entity_Id;
7423 Is_Dyn : Boolean;
7425 begin
7426 Alt := First (Alternatives (N));
7427 while Present (Alt) loop
7428 Alt_Expr := Expression (Alt);
7430 if Error_Posted (Alt_Expr) then
7431 return;
7432 end if;
7434 Resolve_Dependent_Expression (N, Alt_Expr, Typ);
7436 Check_Unset_Reference (Alt_Expr);
7437 Alt_Typ := Etype (Alt_Expr);
7439 -- When the expression is of a scalar subtype different from the
7440 -- result subtype, then insert a conversion to ensure the generation
7441 -- of a constraint check.
7443 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7444 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7445 Analyze_And_Resolve (Alt_Expr, Typ);
7446 end if;
7448 Next (Alt);
7449 end loop;
7451 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7452 -- dynamically tagged must be known statically.
7454 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7455 Alt := First (Alternatives (N));
7456 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7458 while Present (Alt) loop
7459 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7460 Error_Msg_N
7461 ("all or none of the dependent expressions can be "
7462 & "dynamically tagged", N);
7463 end if;
7465 Next (Alt);
7466 end loop;
7467 end if;
7469 Set_Etype (N, Typ);
7470 Eval_Case_Expression (N);
7471 Analyze_Dimension (N);
7472 end Resolve_Case_Expression;
7474 -------------------------------
7475 -- Resolve_Character_Literal --
7476 -------------------------------
7478 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7479 B_Typ : constant Entity_Id := Base_Type (Typ);
7480 C : Entity_Id;
7482 begin
7483 -- Verify that the character does belong to the type of the context
7485 Set_Etype (N, B_Typ);
7486 Eval_Character_Literal (N);
7488 -- Wide_Wide_Character literals must always be defined, since the set
7489 -- of wide wide character literals is complete, i.e. if a character
7490 -- literal is accepted by the parser, then it is OK for wide wide
7491 -- character (out of range character literals are rejected).
7493 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7494 return;
7496 -- Always accept character literal for type Any_Character, which
7497 -- occurs in error situations and in comparisons of literals, both
7498 -- of which should accept all literals.
7500 elsif B_Typ = Any_Character then
7501 return;
7503 -- For Standard.Character or a type derived from it, check that the
7504 -- literal is in range.
7506 elsif Root_Type (B_Typ) = Standard_Character then
7507 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7508 return;
7509 end if;
7511 -- For Standard.Wide_Character or a type derived from it, check that the
7512 -- literal is in range.
7514 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7515 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7516 return;
7517 end if;
7519 -- If the entity is already set, this has already been resolved in a
7520 -- generic context, or comes from expansion. Nothing else to do.
7522 elsif Present (Entity (N)) then
7523 return;
7525 -- Otherwise we have a user defined character type, and we can use the
7526 -- standard visibility mechanisms to locate the referenced entity.
7528 else
7529 C := Current_Entity (N);
7530 while Present (C) loop
7531 if Etype (C) = B_Typ then
7532 Set_Entity_With_Checks (N, C);
7533 Generate_Reference (C, N);
7534 return;
7535 end if;
7537 C := Homonym (C);
7538 end loop;
7539 end if;
7541 -- If we fall through, then the literal does not match any of the
7542 -- entries of the enumeration type. This isn't just a constraint error
7543 -- situation, it is an illegality (see RM 4.2).
7545 Error_Msg_NE
7546 ("character not defined for }", N, First_Subtype (B_Typ));
7547 end Resolve_Character_Literal;
7549 ---------------------------
7550 -- Resolve_Comparison_Op --
7551 ---------------------------
7553 -- The operands must have compatible types and the boolean context does not
7554 -- participate in the resolution. The first pass verifies that the operands
7555 -- are not ambiguous and sets their type correctly, or to Any_Type in case
7556 -- of ambiguity. If both operands are strings or aggregates, then they are
7557 -- ambiguous even if they carry a single (universal) type.
7559 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7560 L : constant Node_Id := Left_Opnd (N);
7561 R : constant Node_Id := Right_Opnd (N);
7563 T : Entity_Id := Find_Unique_Type (L, R);
7565 begin
7566 if T = Any_Fixed then
7567 T := Unique_Fixed_Point_Type (L);
7568 end if;
7570 Set_Etype (N, Base_Type (Typ));
7571 Generate_Reference (T, N, ' ');
7573 if T = Any_Type then
7574 -- Deal with explicit ambiguity of operands
7576 if Is_Overloaded (L) or else Is_Overloaded (R) then
7577 Ambiguous_Operands (N);
7578 end if;
7580 return;
7581 end if;
7583 -- Deal with other error cases
7585 if T = Any_String or else
7586 T = Any_Composite or else
7587 T = Any_Character
7588 then
7589 if T = Any_Character then
7590 Ambiguous_Character (L);
7591 else
7592 Error_Msg_N ("ambiguous operands for comparison", N);
7593 end if;
7595 Set_Etype (N, Any_Type);
7596 return;
7597 end if;
7599 -- Resolve the operands if types OK
7601 Resolve (L, T);
7602 Resolve (R, T);
7603 Check_Unset_Reference (L);
7604 Check_Unset_Reference (R);
7605 Generate_Operator_Reference (N, T);
7606 Check_Low_Bound_Tested (N);
7608 -- Check comparison on unordered enumeration
7610 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7611 Error_Msg_Sloc := Sloc (Etype (L));
7612 Error_Msg_NE
7613 ("comparison on unordered enumeration type& declared#?.u?",
7614 N, Etype (L));
7615 end if;
7617 Analyze_Dimension (N);
7619 Eval_Relational_Op (N);
7620 end Resolve_Comparison_Op;
7622 --------------------------------
7623 -- Resolve_Declare_Expression --
7624 --------------------------------
7626 procedure Resolve_Declare_Expression
7627 (N : Node_Id;
7628 Typ : Entity_Id)
7630 Expr : constant Node_Id := Expression (N);
7632 Decl : Node_Id;
7633 Local : Entity_Id := Empty;
7635 function Replace_Local (N : Node_Id) return Traverse_Result;
7636 -- Use a tree traversal to replace each occurrence of the name of
7637 -- a local object declared in the construct, with the corresponding
7638 -- entity. This replaces the usual way to perform name capture by
7639 -- visibility, because it is not possible to place on the scope
7640 -- stack the fake scope created for the analysis of the local
7641 -- declarations; such a scope conflicts with the transient scopes
7642 -- that may be generated if the expression includes function calls
7643 -- requiring finalization.
7645 -------------------
7646 -- Replace_Local --
7647 -------------------
7649 function Replace_Local (N : Node_Id) return Traverse_Result is
7650 begin
7651 -- The identifier may be the prefix of a selected component,
7652 -- but not a selector name, because the local entities do not
7653 -- have a scope that can be named: a selected component whose
7654 -- selector is a homonym of a local entity must denote some
7655 -- global entity.
7657 if Nkind (N) = N_Identifier
7658 and then Chars (N) = Chars (Local)
7659 and then No (Entity (N))
7660 and then
7661 (Nkind (Parent (N)) /= N_Selected_Component
7662 or else N = Prefix (Parent (N)))
7663 then
7664 Set_Entity (N, Local);
7665 Set_Etype (N, Etype (Local));
7666 end if;
7668 return OK;
7669 end Replace_Local;
7671 procedure Replace_Local_Ref is new Traverse_Proc (Replace_Local);
7673 -- Start of processing for Resolve_Declare_Expression
7675 begin
7677 Decl := First (Actions (N));
7679 while Present (Decl) loop
7680 if Nkind (Decl) in
7681 N_Object_Declaration | N_Object_Renaming_Declaration
7682 and then Comes_From_Source (Defining_Identifier (Decl))
7683 then
7684 Local := Defining_Identifier (Decl);
7685 Replace_Local_Ref (Expr);
7687 -- Traverse the expression to replace references to local
7688 -- variables that occur within declarations of the
7689 -- declare_expression.
7691 declare
7692 D : Node_Id := Next (Decl);
7693 begin
7694 while Present (D) loop
7695 Replace_Local_Ref (D);
7696 Next (D);
7697 end loop;
7698 end;
7699 end if;
7701 Next (Decl);
7702 end loop;
7704 -- The end of the declarative list is a freeze point for the
7705 -- local declarations.
7707 if Present (Local) then
7708 Decl := Parent (Local);
7709 Freeze_All (First_Entity (Scope (Local)), Decl);
7710 end if;
7712 Resolve (Expr, Typ);
7713 Check_Unset_Reference (Expr);
7714 end Resolve_Declare_Expression;
7716 -----------------------------------
7717 -- Resolve_Dependent_Expression --
7718 -----------------------------------
7720 procedure Resolve_Dependent_Expression
7721 (N : Node_Id;
7722 Expr : Node_Id;
7723 Typ : Entity_Id)
7725 begin
7726 -- RM 4.5.7(8/3) says that the expected type of dependent expressions is
7727 -- that of the conditional expression but RM 4.5.7(10/3) forces the type
7728 -- of the conditional expression without changing the expected type (the
7729 -- expected type of the operand of a type conversion is any type), so we
7730 -- may have a gap between these two types that is bridged by the dynamic
7731 -- semantics specified by RM 4.5.7(20/3) with the associated legality
7732 -- rule RM 4.5.7(16/3) that will be automatically enforced.
7734 if Nkind (Parent (N)) = N_Type_Conversion
7735 and then Nkind (Expr) /= N_Raise_Expression
7736 then
7737 Convert_To_And_Rewrite (Typ, Expr);
7738 Analyze_And_Resolve (Expr);
7739 else
7740 Resolve (Expr, Typ);
7741 end if;
7742 end Resolve_Dependent_Expression;
7744 -----------------------------------------
7745 -- Resolve_Discrete_Subtype_Indication --
7746 -----------------------------------------
7748 procedure Resolve_Discrete_Subtype_Indication
7749 (N : Node_Id;
7750 Typ : Entity_Id)
7752 R : Node_Id;
7753 S : Entity_Id;
7755 begin
7756 Analyze (Subtype_Mark (N));
7757 S := Entity (Subtype_Mark (N));
7759 if Nkind (Constraint (N)) /= N_Range_Constraint then
7760 Error_Msg_N ("expect range constraint for discrete type", N);
7761 Set_Etype (N, Any_Type);
7763 else
7764 R := Range_Expression (Constraint (N));
7766 if R = Error then
7767 return;
7768 end if;
7770 Analyze (R);
7772 if Base_Type (S) /= Base_Type (Typ) then
7773 Error_Msg_NE
7774 ("expect subtype of }", N, First_Subtype (Typ));
7776 -- Rewrite the constraint as a range of Typ
7777 -- to allow compilation to proceed further.
7779 Set_Etype (N, Typ);
7780 Rewrite (Low_Bound (R),
7781 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7782 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7783 Attribute_Name => Name_First));
7784 Rewrite (High_Bound (R),
7785 Make_Attribute_Reference (Sloc (High_Bound (R)),
7786 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7787 Attribute_Name => Name_First));
7789 else
7790 Resolve (R, Typ);
7791 Set_Etype (N, Etype (R));
7793 -- Additionally, we must check that the bounds are compatible
7794 -- with the given subtype, which might be different from the
7795 -- type of the context.
7797 Apply_Range_Check (R, S);
7799 -- ??? If the above check statically detects a Constraint_Error
7800 -- it replaces the offending bound(s) of the range R with a
7801 -- Constraint_Error node. When the itype which uses these bounds
7802 -- is frozen the resulting call to Duplicate_Subexpr generates
7803 -- a new temporary for the bounds.
7805 -- Unfortunately there are other itypes that are also made depend
7806 -- on these bounds, so when Duplicate_Subexpr is called they get
7807 -- a forward reference to the newly created temporaries and Gigi
7808 -- aborts on such forward references. This is probably sign of a
7809 -- more fundamental problem somewhere else in either the order of
7810 -- itype freezing or the way certain itypes are constructed.
7812 -- To get around this problem we call Remove_Side_Effects right
7813 -- away if either bounds of R are a Constraint_Error.
7815 declare
7816 L : constant Node_Id := Low_Bound (R);
7817 H : constant Node_Id := High_Bound (R);
7819 begin
7820 if Nkind (L) = N_Raise_Constraint_Error then
7821 Remove_Side_Effects (L);
7822 end if;
7824 if Nkind (H) = N_Raise_Constraint_Error then
7825 Remove_Side_Effects (H);
7826 end if;
7827 end;
7829 Check_Unset_Reference (Low_Bound (R));
7830 Check_Unset_Reference (High_Bound (R));
7831 end if;
7832 end if;
7833 end Resolve_Discrete_Subtype_Indication;
7835 -------------------------
7836 -- Resolve_Entity_Name --
7837 -------------------------
7839 -- Used to resolve identifiers and expanded names
7841 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7842 function Is_Assignment_Or_Object_Expression
7843 (Context : Node_Id;
7844 Expr : Node_Id) return Boolean;
7845 -- Determine whether node Context denotes an assignment statement or an
7846 -- object declaration whose expression is node Expr.
7848 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7849 -- Determine whether Expr is part of an N_Attribute_Reference
7850 -- expression.
7852 function In_Attribute_Old (Expr : Node_Id) return Boolean;
7853 -- Determine whether Expr is in attribute Old
7855 function Within_Exceptional_Cases_Consequence
7856 (Expr : Node_Id)
7857 return Boolean;
7858 -- Determine whether Expr is part of an Exceptional_Cases consequence
7860 ----------------------------------------
7861 -- Is_Assignment_Or_Object_Expression --
7862 ----------------------------------------
7864 function Is_Assignment_Or_Object_Expression
7865 (Context : Node_Id;
7866 Expr : Node_Id) return Boolean
7868 begin
7869 if Nkind (Context) in N_Assignment_Statement | N_Object_Declaration
7870 and then Expression (Context) = Expr
7871 then
7872 return True;
7874 -- Check whether a construct that yields a name is the expression of
7875 -- an assignment statement or an object declaration.
7877 elsif (Nkind (Context) in N_Attribute_Reference
7878 | N_Explicit_Dereference
7879 | N_Indexed_Component
7880 | N_Selected_Component
7881 | N_Slice
7882 and then Prefix (Context) = Expr)
7883 or else
7884 (Nkind (Context) in N_Type_Conversion
7885 | N_Unchecked_Type_Conversion
7886 and then Expression (Context) = Expr)
7887 then
7888 return
7889 Is_Assignment_Or_Object_Expression
7890 (Context => Parent (Context),
7891 Expr => Context);
7893 -- Otherwise the context is not an assignment statement or an object
7894 -- declaration.
7896 else
7897 return False;
7898 end if;
7899 end Is_Assignment_Or_Object_Expression;
7901 ----------------------
7902 -- In_Attribute_Old --
7903 ----------------------
7905 function In_Attribute_Old (Expr : Node_Id) return Boolean is
7906 N : Node_Id := Expr;
7907 begin
7908 while Present (N) loop
7909 if Nkind (N) = N_Attribute_Reference
7910 and then Attribute_Name (N) = Name_Old
7911 then
7912 return True;
7914 -- Prevent the search from going too far
7916 elsif Is_Body_Or_Package_Declaration (N) then
7917 return False;
7918 end if;
7920 N := Parent (N);
7921 end loop;
7923 return False;
7924 end In_Attribute_Old;
7926 -----------------------------
7927 -- Is_Attribute_Expression --
7928 -----------------------------
7930 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
7931 N : Node_Id := Expr;
7932 begin
7933 while Present (N) loop
7934 if Nkind (N) = N_Attribute_Reference then
7935 return True;
7937 -- Prevent the search from going too far
7939 elsif Is_Body_Or_Package_Declaration (N) then
7940 return False;
7941 end if;
7943 N := Parent (N);
7944 end loop;
7946 return False;
7947 end Is_Attribute_Expression;
7949 ------------------------------------------
7950 -- Within_Exceptional_Cases_Consequence --
7951 ------------------------------------------
7953 function Within_Exceptional_Cases_Consequence
7954 (Expr : Node_Id)
7955 return Boolean
7957 Context : Node_Id := Parent (Expr);
7958 begin
7959 while Present (Context) loop
7960 if Nkind (Context) = N_Pragma then
7962 -- In Exceptional_Cases references to formal parameters are
7963 -- only allowed within consequences, so it is enough to
7964 -- recognize the pragma itself.
7966 if Get_Pragma_Id (Context) = Pragma_Exceptional_Cases then
7967 return True;
7968 end if;
7970 -- Prevent the search from going too far
7972 elsif Is_Body_Or_Package_Declaration (Context) then
7973 return False;
7974 end if;
7976 Context := Parent (Context);
7977 end loop;
7979 return False;
7980 end Within_Exceptional_Cases_Consequence;
7982 -- Local variables
7984 E : constant Entity_Id := Entity (N);
7985 Par : Node_Id;
7987 -- Start of processing for Resolve_Entity_Name
7989 begin
7990 -- If garbage from errors, set to Any_Type and return
7992 if No (E) and then Total_Errors_Detected /= 0 then
7993 Set_Etype (N, Any_Type);
7994 return;
7995 end if;
7997 -- Replace named numbers by corresponding literals. Note that this is
7998 -- the one case where Resolve_Entity_Name must reset the Etype, since
7999 -- it is currently marked as universal.
8001 if Ekind (E) = E_Named_Integer then
8002 Set_Etype (N, Typ);
8003 Eval_Named_Integer (N);
8005 elsif Ekind (E) = E_Named_Real then
8006 Set_Etype (N, Typ);
8007 Eval_Named_Real (N);
8009 -- For enumeration literals, we need to make sure that a proper style
8010 -- check is done, since such literals are overloaded, and thus we did
8011 -- not do a style check during the first phase of analysis.
8013 elsif Ekind (E) = E_Enumeration_Literal then
8014 Set_Entity_With_Checks (N, E);
8015 Eval_Entity_Name (N);
8017 -- Case of (sub)type name appearing in a context where an expression
8018 -- is expected. This is legal if occurrence is a current instance.
8019 -- See RM 8.6 (17/3). It is also legal if the expression is
8020 -- part of a choice pattern for a case stmt/expr having a
8021 -- non-discrete selecting expression.
8023 elsif Is_Type (E) then
8024 if Is_Current_Instance (N) or else Is_Case_Choice_Pattern (N) then
8025 null;
8027 -- Any other use is an error
8029 else
8030 Error_Msg_N
8031 ("invalid use of subtype mark in expression or call", N);
8032 end if;
8034 -- Check discriminant use if entity is discriminant in current scope,
8035 -- i.e. discriminant of record or concurrent type currently being
8036 -- analyzed. Uses in corresponding body are unrestricted.
8038 elsif Ekind (E) = E_Discriminant
8039 and then Scope (E) = Current_Scope
8040 and then not Has_Completion (Current_Scope)
8041 then
8042 Check_Discriminant_Use (N);
8044 -- A parameterless generic function cannot appear in a context that
8045 -- requires resolution.
8047 elsif Ekind (E) = E_Generic_Function then
8048 Error_Msg_N ("illegal use of generic function", N);
8050 -- In Ada 83 an OUT parameter cannot be read, but attributes of
8051 -- array types (i.e. bounds and length) are legal.
8053 elsif Ekind (E) = E_Out_Parameter
8054 and then (Is_Scalar_Type (Etype (E))
8055 or else not Is_Attribute_Expression (Parent (N)))
8057 and then (Nkind (Parent (N)) in N_Op
8058 or else Nkind (Parent (N)) = N_Explicit_Dereference
8059 or else Is_Assignment_Or_Object_Expression
8060 (Context => Parent (N),
8061 Expr => N))
8062 then
8063 if Ada_Version = Ada_83 then
8064 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
8065 end if;
8067 -- In all other cases, just do the possible static evaluation
8069 else
8070 -- A deferred constant that appears in an expression must have a
8071 -- completion, unless it has been removed by in-place expansion of
8072 -- an aggregate. A constant that is a renaming does not need
8073 -- initialization.
8075 if Ekind (E) = E_Constant
8076 and then Comes_From_Source (E)
8077 and then No (Constant_Value (E))
8078 and then Is_Frozen (Etype (E))
8079 and then not In_Spec_Expression
8080 and then not Is_Imported (E)
8081 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
8082 then
8083 if No_Initialization (Parent (E))
8084 or else (Present (Full_View (E))
8085 and then No_Initialization (Parent (Full_View (E))))
8086 then
8087 null;
8088 else
8089 Error_Msg_N
8090 ("deferred constant is frozen before completion", N);
8091 end if;
8092 end if;
8094 Eval_Entity_Name (N);
8095 end if;
8097 Par := Parent (N);
8099 -- When the entity appears in a parameter association, retrieve the
8100 -- related subprogram call.
8102 if Nkind (Par) = N_Parameter_Association then
8103 Par := Parent (Par);
8104 end if;
8106 if Comes_From_Source (N) then
8108 -- The following checks are only relevant when SPARK_Mode is On as
8109 -- they are not standard Ada legality rules.
8111 if SPARK_Mode = On then
8113 -- An effectively volatile object for reading must appear in
8114 -- non-interfering context (SPARK RM 7.1.3(10)).
8116 if Is_Object (E)
8117 and then Is_Effectively_Volatile_For_Reading (E)
8118 and then
8119 not Is_OK_Volatile_Context (Par, N, Check_Actuals => False)
8120 then
8121 Error_Msg_Code := GEC_Volatile_Non_Interfering_Context;
8122 SPARK_Msg_N
8123 ("volatile object cannot appear in this context '[[]']", N);
8124 end if;
8126 -- Parameters of modes OUT or IN OUT of the subprogram shall not
8127 -- occur in the consequences of an exceptional contract unless
8128 -- they are either passed by reference or occur in the prefix
8129 -- of a reference to the 'Old attribute. For convenience, we also
8130 -- allow them as prefixes of attributes that do not actually read
8131 -- data from the object.
8133 if Ekind (E) in E_Out_Parameter | E_In_Out_Parameter
8134 and then Scope (E) = Current_Scope_No_Loops
8135 and then Within_Exceptional_Cases_Consequence (N)
8136 and then not In_Attribute_Old (N)
8137 and then not (Nkind (Parent (N)) = N_Attribute_Reference
8138 and then
8139 Attribute_Name (Parent (N)) in Name_Constrained
8140 | Name_First
8141 | Name_Last
8142 | Name_Length
8143 | Name_Range)
8144 and then not Is_By_Reference_Type (Etype (E))
8145 and then not Is_Aliased (E)
8146 then
8147 if Ekind (E) = E_Out_Parameter then
8148 Error_Msg_N
8149 ("formal parameter of mode `OUT` cannot appear " &
8150 "in consequence of Exceptional_Cases", N);
8151 else
8152 Error_Msg_N
8153 ("formal parameter of mode `IN OUT` cannot appear " &
8154 "in consequence of Exceptional_Cases", N);
8155 end if;
8156 Error_Msg_N
8157 ("\only parameters passed by reference are allowed", N);
8158 end if;
8160 -- Check for possible elaboration issues with respect to reads of
8161 -- variables. The act of renaming the variable is not considered a
8162 -- read as it simply establishes an alias.
8164 if Legacy_Elaboration_Checks
8165 and then Ekind (E) = E_Variable
8166 and then Dynamic_Elaboration_Checks
8167 and then Nkind (Par) /= N_Object_Renaming_Declaration
8168 then
8169 Check_Elab_Call (N);
8170 end if;
8171 end if;
8173 -- The variable may eventually become a constituent of a single
8174 -- protected/task type. Record the reference now and verify its
8175 -- legality when analyzing the contract of the variable
8176 -- (SPARK RM 9.3).
8178 if Ekind (E) = E_Variable then
8179 Record_Possible_Part_Of_Reference (E, N);
8180 end if;
8182 -- A Ghost entity must appear in a specific context
8184 if Is_Ghost_Entity (E) then
8185 Check_Ghost_Context (E, N);
8186 end if;
8188 -- We may be resolving an entity within expanded code, so a reference
8189 -- to an entity should be ignored when calculating effective use
8190 -- clauses to avoid inappropriate marking.
8192 Mark_Use_Clauses (E);
8193 end if;
8194 end Resolve_Entity_Name;
8196 -------------------
8197 -- Resolve_Entry --
8198 -------------------
8200 procedure Resolve_Entry (Entry_Name : Node_Id) is
8201 Loc : constant Source_Ptr := Sloc (Entry_Name);
8202 Nam : Entity_Id;
8203 New_N : Node_Id;
8204 S : Entity_Id;
8205 Tsk : Entity_Id;
8206 E_Name : Node_Id;
8207 Index : Node_Id;
8209 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
8210 -- If the bounds of the entry family being called depend on task
8211 -- discriminants, build a new index subtype where a discriminant is
8212 -- replaced with the value of the discriminant of the target task.
8213 -- The target task is the prefix of the entry name in the call.
8215 -----------------------
8216 -- Actual_Index_Type --
8217 -----------------------
8219 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
8220 Typ : constant Entity_Id := Entry_Index_Type (E);
8221 Tsk : constant Entity_Id := Scope (E);
8222 Lo : constant Node_Id := Type_Low_Bound (Typ);
8223 Hi : constant Node_Id := Type_High_Bound (Typ);
8224 New_T : Entity_Id;
8226 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8227 -- If the bound is given by a discriminant, replace with a reference
8228 -- to the discriminant of the same name in the target task. If the
8229 -- entry name is the target of a requeue statement and the entry is
8230 -- in the current protected object, the bound to be used is the
8231 -- discriminal of the object (see Apply_Range_Check for details of
8232 -- the transformation).
8234 -----------------------------
8235 -- Actual_Discriminant_Ref --
8236 -----------------------------
8238 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8239 Typ : constant Entity_Id := Etype (Bound);
8240 Ref : Node_Id;
8242 begin
8243 Remove_Side_Effects (Bound);
8245 if not Is_Entity_Name (Bound)
8246 or else Ekind (Entity (Bound)) /= E_Discriminant
8247 then
8248 return Bound;
8250 elsif Is_Protected_Type (Tsk)
8251 and then In_Open_Scopes (Tsk)
8252 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
8253 then
8254 -- Note: here Bound denotes a discriminant of the corresponding
8255 -- record type tskV, whose discriminal is a formal of the
8256 -- init-proc tskVIP. What we want is the body discriminal,
8257 -- which is associated to the discriminant of the original
8258 -- concurrent type tsk.
8260 return New_Occurrence_Of
8261 (Find_Body_Discriminal (Entity (Bound)), Loc);
8263 else
8264 Ref :=
8265 Make_Selected_Component (Loc,
8266 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8267 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8268 Analyze (Ref);
8269 Resolve (Ref, Typ);
8270 return Ref;
8271 end if;
8272 end Actual_Discriminant_Ref;
8274 -- Start of processing for Actual_Index_Type
8276 begin
8277 if not Has_Discriminants (Tsk)
8278 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8279 then
8280 return Entry_Index_Type (E);
8282 else
8283 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8284 Set_Etype (New_T, Base_Type (Typ));
8285 Set_Size_Info (New_T, Typ);
8286 Set_RM_Size (New_T, RM_Size (Typ));
8287 Set_Scalar_Range (New_T,
8288 Make_Range (Sloc (Entry_Name),
8289 Low_Bound => Actual_Discriminant_Ref (Lo),
8290 High_Bound => Actual_Discriminant_Ref (Hi)));
8292 return New_T;
8293 end if;
8294 end Actual_Index_Type;
8296 -- Start of processing for Resolve_Entry
8298 begin
8299 -- Find name of entry being called, and resolve prefix of name with its
8300 -- own type. The prefix can be overloaded, and the name and signature of
8301 -- the entry must be taken into account.
8303 if Nkind (Entry_Name) = N_Indexed_Component then
8305 -- Case of dealing with entry family within the current tasks
8307 E_Name := Prefix (Entry_Name);
8309 else
8310 E_Name := Entry_Name;
8311 end if;
8313 if Is_Entity_Name (E_Name) then
8315 -- Entry call to an entry (or entry family) in the current task. This
8316 -- is legal even though the task will deadlock. Rewrite as call to
8317 -- current task.
8319 -- This can also be a call to an entry in an enclosing task. If this
8320 -- is a single task, we have to retrieve its name, because the scope
8321 -- of the entry is the task type, not the object. If the enclosing
8322 -- task is a task type, the identity of the task is given by its own
8323 -- self variable.
8325 -- Finally this can be a requeue on an entry of the same task or
8326 -- protected object.
8328 S := Scope (Entity (E_Name));
8330 for J in reverse 0 .. Scope_Stack.Last loop
8331 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8332 and then not Comes_From_Source (S)
8333 then
8334 -- S is an enclosing task or protected object. The concurrent
8335 -- declaration has been converted into a type declaration, and
8336 -- the object itself has an object declaration that follows
8337 -- the type in the same declarative part.
8339 Tsk := Next_Entity (S);
8340 while Etype (Tsk) /= S loop
8341 Next_Entity (Tsk);
8342 end loop;
8344 S := Tsk;
8345 exit;
8347 elsif S = Scope_Stack.Table (J).Entity then
8349 -- Call to current task. Will be transformed into call to Self
8351 exit;
8353 end if;
8354 end loop;
8356 New_N :=
8357 Make_Selected_Component (Loc,
8358 Prefix => New_Occurrence_Of (S, Loc),
8359 Selector_Name =>
8360 New_Occurrence_Of (Entity (E_Name), Loc));
8361 Rewrite (E_Name, New_N);
8362 Analyze (E_Name);
8364 elsif Nkind (Entry_Name) = N_Selected_Component
8365 and then Is_Overloaded (Prefix (Entry_Name))
8366 then
8367 -- Use the entry name (which must be unique at this point) to find
8368 -- the prefix that returns the corresponding task/protected type.
8370 declare
8371 Pref : constant Node_Id := Prefix (Entry_Name);
8372 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8373 I : Interp_Index;
8374 It : Interp;
8376 begin
8377 Get_First_Interp (Pref, I, It);
8378 while Present (It.Typ) loop
8379 if Scope (Ent) = It.Typ then
8380 Set_Etype (Pref, It.Typ);
8381 exit;
8382 end if;
8384 Get_Next_Interp (I, It);
8385 end loop;
8386 end;
8387 end if;
8389 if Nkind (Entry_Name) = N_Selected_Component then
8390 Resolve (Prefix (Entry_Name));
8391 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8393 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8394 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8395 Resolve (Prefix (Prefix (Entry_Name)));
8396 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8398 -- We do not resolve the prefix because an Entry_Family has no type,
8399 -- although it has the semantics of an array since it can be indexed.
8400 -- In order to perform the associated range check, we would need to
8401 -- build an array type on the fly and set it on the prefix, but this
8402 -- would be wasteful since only the index type matters. Therefore we
8403 -- attach this index type directly, so that Actual_Index_Expression
8404 -- can pick it up later in order to generate the range check.
8406 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8408 Index := First (Expressions (Entry_Name));
8409 Resolve (Index, Entry_Index_Type (Nam));
8411 -- Generate a reference for the index when it denotes an entity
8413 if Is_Entity_Name (Index) then
8414 Generate_Reference (Entity (Index), Nam);
8415 end if;
8417 -- Up to this point the expression could have been the actual in a
8418 -- simple entry call, and be given by a named association.
8420 if Nkind (Index) = N_Parameter_Association then
8421 Error_Msg_N ("expect expression for entry index", Index);
8422 else
8423 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8424 end if;
8425 end if;
8426 end Resolve_Entry;
8428 ------------------------
8429 -- Resolve_Entry_Call --
8430 ------------------------
8432 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8433 Entry_Name : constant Node_Id := Name (N);
8434 Loc : constant Source_Ptr := Sloc (Entry_Name);
8436 Nam : Entity_Id;
8437 Norm_OK : Boolean;
8438 Obj : Node_Id;
8439 Was_Over : Boolean;
8441 begin
8442 -- We kill all checks here, because it does not seem worth the effort to
8443 -- do anything better, an entry call is a big operation.
8445 Kill_All_Checks;
8447 -- Processing of the name is similar for entry calls and protected
8448 -- operation calls. Once the entity is determined, we can complete
8449 -- the resolution of the actuals.
8451 -- The selector may be overloaded, in the case of a protected object
8452 -- with overloaded functions. The type of the context is used for
8453 -- resolution.
8455 if Nkind (Entry_Name) = N_Selected_Component
8456 and then Is_Overloaded (Selector_Name (Entry_Name))
8457 and then Typ /= Standard_Void_Type
8458 then
8459 declare
8460 I : Interp_Index;
8461 It : Interp;
8463 begin
8464 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8465 while Present (It.Typ) loop
8466 if Covers (Typ, It.Typ) then
8467 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8468 Set_Etype (Entry_Name, It.Typ);
8470 Generate_Reference (It.Typ, N, ' ');
8471 end if;
8473 Get_Next_Interp (I, It);
8474 end loop;
8475 end;
8476 end if;
8478 Resolve_Entry (Entry_Name);
8480 if Nkind (Entry_Name) = N_Selected_Component then
8482 -- Simple entry or protected operation call
8484 Nam := Entity (Selector_Name (Entry_Name));
8485 Obj := Prefix (Entry_Name);
8487 if Is_Subprogram (Nam) then
8488 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8489 end if;
8491 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8493 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8495 -- Call to member of entry family
8497 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8498 Obj := Prefix (Prefix (Entry_Name));
8499 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8500 end if;
8502 -- We cannot in general check the maximum depth of protected entry calls
8503 -- at compile time. But we can tell that any protected entry call at all
8504 -- violates a specified nesting depth of zero.
8506 if Is_Protected_Type (Scope (Nam)) then
8507 Check_Restriction (Max_Entry_Queue_Length, N);
8508 end if;
8510 -- Use context type to disambiguate a protected function that can be
8511 -- called without actuals and that returns an array type, and where the
8512 -- argument list may be an indexing of the returned value.
8514 if Ekind (Nam) = E_Function
8515 and then Needs_No_Actuals (Nam)
8516 and then Present (Parameter_Associations (N))
8517 and then
8518 ((Is_Array_Type (Etype (Nam))
8519 and then Covers (Typ, Component_Type (Etype (Nam))))
8521 or else (Is_Access_Type (Etype (Nam))
8522 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8523 and then
8524 Covers
8525 (Typ,
8526 Component_Type (Designated_Type (Etype (Nam))))))
8527 then
8528 declare
8529 Index_Node : Node_Id;
8531 begin
8532 Index_Node :=
8533 Make_Indexed_Component (Loc,
8534 Prefix =>
8535 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8536 Expressions => Parameter_Associations (N));
8538 -- Since we are correcting a node classification error made by the
8539 -- parser, we call Replace rather than Rewrite.
8541 Replace (N, Index_Node);
8542 Set_Etype (Prefix (N), Etype (Nam));
8543 Set_Etype (N, Typ);
8544 Resolve_Indexed_Component (N, Typ);
8545 return;
8546 end;
8547 end if;
8549 if Is_Entry (Nam)
8550 and then Present (Contract_Wrapper (Nam))
8551 and then Current_Scope /= Contract_Wrapper (Nam)
8552 and then Current_Scope /= Wrapped_Statements (Contract_Wrapper (Nam))
8553 then
8554 -- Note the entity being called before rewriting the call, so that
8555 -- it appears used at this point.
8557 Generate_Reference (Nam, Entry_Name, 'r');
8559 -- Rewrite as call to the precondition wrapper, adding the task
8560 -- object to the list of actuals. If the call is to a member of an
8561 -- entry family, include the index as well.
8563 declare
8564 New_Call : Node_Id;
8565 New_Actuals : List_Id;
8567 begin
8568 New_Actuals := New_List (Obj);
8570 if Nkind (Entry_Name) = N_Indexed_Component then
8571 Append_To (New_Actuals,
8572 New_Copy_Tree (First (Expressions (Entry_Name))));
8573 end if;
8575 Append_List (Parameter_Associations (N), New_Actuals);
8576 New_Call :=
8577 Make_Procedure_Call_Statement (Loc,
8578 Name =>
8579 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8580 Parameter_Associations => New_Actuals);
8581 Rewrite (N, New_Call);
8583 -- Preanalyze and resolve new call. Current procedure is called
8584 -- from Resolve_Call, after which expansion will take place.
8586 Preanalyze_And_Resolve (N);
8587 return;
8588 end;
8589 end if;
8591 -- The operation name may have been overloaded. Order the actuals
8592 -- according to the formals of the resolved entity, and set the return
8593 -- type to that of the operation.
8595 if Was_Over then
8596 Normalize_Actuals (N, Nam, False, Norm_OK);
8597 pragma Assert (Norm_OK);
8598 Set_Etype (N, Etype (Nam));
8600 -- Reset the Is_Overloaded flag, since resolution is now completed
8602 -- Simple entry call
8604 if Nkind (Entry_Name) = N_Selected_Component then
8605 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8607 -- Call to a member of an entry family
8609 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8610 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8611 end if;
8612 end if;
8614 Resolve_Actuals (N, Nam);
8615 Check_Internal_Protected_Use (N, Nam);
8617 -- Create a call reference to the entry
8619 Generate_Reference (Nam, Entry_Name, 's');
8621 if Is_Entry (Nam) then
8622 Check_Potentially_Blocking_Operation (N);
8623 end if;
8625 -- Verify that a procedure call cannot masquerade as an entry
8626 -- call where an entry call is expected.
8628 if Ekind (Nam) = E_Procedure then
8629 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8630 and then N = Entry_Call_Statement (Parent (N))
8631 then
8632 Error_Msg_N ("entry call required in select statement", N);
8634 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8635 and then N = Triggering_Statement (Parent (N))
8636 then
8637 Error_Msg_N ("triggering statement cannot be procedure call", N);
8639 elsif Ekind (Scope (Nam)) = E_Task_Type
8640 and then not In_Open_Scopes (Scope (Nam))
8641 then
8642 Error_Msg_N ("task has no entry with this name", Entry_Name);
8643 end if;
8644 end if;
8646 -- After resolution, entry calls and protected procedure calls are
8647 -- changed into entry calls, for expansion. The structure of the node
8648 -- does not change, so it can safely be done in place. Protected
8649 -- function calls must keep their structure because they are
8650 -- subexpressions.
8652 if Ekind (Nam) /= E_Function then
8654 -- A protected operation that is not a function may modify the
8655 -- corresponding object, and cannot apply to a constant. If this
8656 -- is an internal call, the prefix is the type itself.
8658 if Is_Protected_Type (Scope (Nam))
8659 and then not Is_Variable (Obj)
8660 and then (not Is_Entity_Name (Obj)
8661 or else not Is_Type (Entity (Obj)))
8662 then
8663 Error_Msg_N
8664 ("prefix of protected procedure or entry call must be variable",
8665 Entry_Name);
8666 end if;
8668 declare
8669 Entry_Call : Node_Id;
8671 begin
8672 Entry_Call :=
8673 Make_Entry_Call_Statement (Loc,
8674 Name => Entry_Name,
8675 Parameter_Associations => Parameter_Associations (N));
8677 -- Inherit relevant attributes from the original call
8679 Set_First_Named_Actual
8680 (Entry_Call, First_Named_Actual (N));
8682 Set_Is_Elaboration_Checks_OK_Node
8683 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8685 Set_Is_Elaboration_Warnings_OK_Node
8686 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8688 Set_Is_SPARK_Mode_On_Node
8689 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8691 Rewrite (N, Entry_Call);
8692 Set_Analyzed (N, True);
8693 end;
8695 -- Protected functions can return on the secondary stack, in which case
8696 -- we must trigger the transient scope mechanism.
8698 elsif Expander_Active
8699 and then Requires_Transient_Scope (Etype (Nam))
8700 then
8701 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
8702 end if;
8704 -- Now we know that this is not a call to a function that returns an
8705 -- array type; moreover, we know the name of the called entry. Detect
8706 -- overlapping actuals, just like for a subprogram call.
8708 Warn_On_Overlapping_Actuals (Nam, N);
8709 end Resolve_Entry_Call;
8711 -------------------------
8712 -- Resolve_Equality_Op --
8713 -------------------------
8715 -- The operands must have compatible types and the boolean context does not
8716 -- participate in the resolution. The first pass verifies that the operands
8717 -- are not ambiguous and sets their type correctly, or to Any_Type in case
8718 -- of ambiguity. If both operands are strings, aggregates, allocators, or
8719 -- null, they are ambiguous even if they carry a single (universal) type.
8721 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8722 L : constant Node_Id := Left_Opnd (N);
8723 R : constant Node_Id := Right_Opnd (N);
8725 Implicit_NE_For_User_Defined_Operator : constant Boolean :=
8726 Nkind (N) = N_Op_Ne
8727 and then Ekind (Entity (N)) = E_Function
8728 and then not Comes_From_Source (Entity (N))
8729 and then not
8730 Is_Intrinsic_Subprogram (Corresponding_Equality (Entity (N)));
8731 -- Whether this is a call to the implicit inequality operator created
8732 -- for a user-defined operator that is not an intrinsic subprogram, in
8733 -- which case we need to skip some processing.
8735 T : Entity_Id := Find_Unique_Type (L, R);
8737 procedure Check_Access_Attribute (N : Node_Id);
8738 -- For any object, '[Unchecked_]Access of such object can never be
8739 -- passed as an operand to the Universal_Access equality operators.
8740 -- This is so because the expected type for Obj'Access in a call to
8741 -- these operators, whose formals are of type Universal_Access, is
8742 -- Universal_Access, and Universal_Access does not have a designated
8743 -- type. For more details, see RM 3.10.2(2/2) and 6.4.1(3).
8745 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id);
8746 -- Check RM 4.5.2(9.6/2) on the given designated object types
8748 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id);
8749 -- Check RM 4.5.2(9.7/2) on the given designated subprogram types
8751 procedure Check_If_Expression (Cond : Node_Id);
8752 -- The resolution rule for if expressions requires that each such must
8753 -- have a unique type. This means that if several dependent expressions
8754 -- are of a non-null anonymous access type, and the context does not
8755 -- impose an expected type (as can be the case in an equality operation)
8756 -- the expression must be rejected.
8758 procedure Explain_Redundancy (N : Node_Id);
8759 -- Attempt to explain the nature of a redundant comparison with True. If
8760 -- the expression N is too complex, this routine issues a general error
8761 -- message.
8763 function Find_Unique_Access_Type return Entity_Id;
8764 -- In the case of allocators and access attributes, the context must
8765 -- provide an indication of the specific access type to be used. If
8766 -- one operand is of such a "generic" access type, check whether there
8767 -- is a specific visible access type that has the same designated type.
8768 -- This is semantically dubious, and of no interest to any real code,
8769 -- but c48008a makes it all worthwhile.
8771 function Suspicious_Prio_For_Equality return Boolean;
8772 -- Returns True iff the parent node is a and/or/xor operation that
8773 -- could be the cause of confused priorities. Note that if the not is
8774 -- in parens, then False is returned.
8776 ----------------------------
8777 -- Check_Access_Attribute --
8778 ----------------------------
8780 procedure Check_Access_Attribute (N : Node_Id) is
8781 begin
8782 if Nkind (N) = N_Attribute_Reference
8783 and then Attribute_Name (N) in Name_Access | Name_Unchecked_Access
8784 then
8785 Error_Msg_N
8786 ("access attribute cannot be used as actual for "
8787 & "universal_access equality", N);
8788 end if;
8789 end Check_Access_Attribute;
8791 -----------------------------------
8792 -- Check_Designated_Object_Types --
8793 -----------------------------------
8795 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id) is
8796 begin
8797 if (Is_Elementary_Type (T1) or else Is_Array_Type (T1))
8798 and then (Base_Type (T1) /= Base_Type (T2)
8799 or else not Subtypes_Statically_Match (T1, T2))
8800 then
8801 Error_Msg_N
8802 ("designated subtypes for universal_access equality "
8803 & "do not statically match (RM 4.5.2(9.6/2)", N);
8804 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8805 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8806 end if;
8807 end Check_Designated_Object_Types;
8809 ---------------------------------------
8810 -- Check_Designated_Subprogram_Types --
8811 ---------------------------------------
8813 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id) is
8814 begin
8815 if not Subtype_Conformant (T1, T2) then
8816 Error_Msg_N
8817 ("designated subtypes for universal_access equality "
8818 & "not subtype conformant (RM 4.5.2(9.7/2)", N);
8819 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8820 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8821 end if;
8822 end Check_Designated_Subprogram_Types;
8824 -------------------------
8825 -- Check_If_Expression --
8826 -------------------------
8828 procedure Check_If_Expression (Cond : Node_Id) is
8829 Then_Expr : Node_Id;
8830 Else_Expr : Node_Id;
8832 begin
8833 if Nkind (Cond) = N_If_Expression then
8834 Then_Expr := Next (First (Expressions (Cond)));
8835 Else_Expr := Next (Then_Expr);
8837 if Nkind (Then_Expr) /= N_Null
8838 and then Nkind (Else_Expr) /= N_Null
8839 then
8840 Error_Msg_N ("cannot determine type of if expression", Cond);
8841 end if;
8842 end if;
8843 end Check_If_Expression;
8845 ------------------------
8846 -- Explain_Redundancy --
8847 ------------------------
8849 procedure Explain_Redundancy (N : Node_Id) is
8850 Error : Name_Id;
8851 Val : Node_Id;
8852 Val_Id : Entity_Id;
8854 begin
8855 Val := N;
8857 -- Strip the operand down to an entity
8859 loop
8860 if Nkind (Val) = N_Selected_Component then
8861 Val := Selector_Name (Val);
8862 else
8863 exit;
8864 end if;
8865 end loop;
8867 -- The construct denotes an entity
8869 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8870 Val_Id := Entity (Val);
8872 -- Do not generate an error message when the comparison is done
8873 -- against the enumeration literal Standard.True.
8875 if Ekind (Val_Id) /= E_Enumeration_Literal then
8877 -- Build a customized error message
8879 Name_Len := 0;
8880 Add_Str_To_Name_Buffer ("?r?");
8882 if Ekind (Val_Id) = E_Component then
8883 Add_Str_To_Name_Buffer ("component ");
8885 elsif Ekind (Val_Id) = E_Constant then
8886 Add_Str_To_Name_Buffer ("constant ");
8888 elsif Ekind (Val_Id) = E_Discriminant then
8889 Add_Str_To_Name_Buffer ("discriminant ");
8891 elsif Is_Formal (Val_Id) then
8892 Add_Str_To_Name_Buffer ("parameter ");
8894 elsif Ekind (Val_Id) = E_Variable then
8895 Add_Str_To_Name_Buffer ("variable ");
8896 end if;
8898 Add_Str_To_Name_Buffer ("& is always True!");
8899 Error := Name_Find;
8901 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8902 end if;
8904 -- The construct is too complex to disect, issue a general message
8906 else
8907 Error_Msg_N ("?r?expression is always True!", Val);
8908 end if;
8909 end Explain_Redundancy;
8911 -----------------------------
8912 -- Find_Unique_Access_Type --
8913 -----------------------------
8915 function Find_Unique_Access_Type return Entity_Id is
8916 Acc : Entity_Id;
8917 E : Entity_Id;
8918 S : Entity_Id;
8920 begin
8921 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8922 then
8923 Acc := Designated_Type (Etype (R));
8925 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8926 then
8927 Acc := Designated_Type (Etype (L));
8928 else
8929 return Empty;
8930 end if;
8932 S := Current_Scope;
8933 while S /= Standard_Standard loop
8934 E := First_Entity (S);
8935 while Present (E) loop
8936 if Is_Type (E)
8937 and then Is_Access_Type (E)
8938 and then Ekind (E) /= E_Allocator_Type
8939 and then Designated_Type (E) = Base_Type (Acc)
8940 then
8941 return E;
8942 end if;
8944 Next_Entity (E);
8945 end loop;
8947 S := Scope (S);
8948 end loop;
8950 return Empty;
8951 end Find_Unique_Access_Type;
8953 ----------------------------------
8954 -- Suspicious_Prio_For_Equality --
8955 ----------------------------------
8957 function Suspicious_Prio_For_Equality return Boolean is
8958 Par : constant Node_Id := Parent (N);
8960 begin
8961 -- Check if parent node is one of and/or/xor, not parenthesized
8962 -- explicitly, and its own parent is not of this kind. Otherwise,
8963 -- it's a case of chained Boolean conditions which is likely well
8964 -- parenthesized.
8966 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8967 and then Paren_Count (N) = 0
8968 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8969 then
8970 declare
8971 Compar : Node_Id :=
8972 (if Left_Opnd (Par) = N then
8973 Right_Opnd (Par)
8974 else
8975 Left_Opnd (Par));
8976 begin
8977 -- Compar may have been rewritten, for example from (a /= b)
8978 -- into not (a = b). Use the Original_Node instead.
8980 Compar := Original_Node (Compar);
8982 -- If the other argument of the and/or/xor is also a
8983 -- comparison, or another and/or/xor then most likely
8984 -- the priorities are correctly set.
8986 return Nkind (Compar) not in N_Op_Boolean;
8987 end;
8989 else
8990 return False;
8991 end if;
8992 end Suspicious_Prio_For_Equality;
8994 -- Start of processing for Resolve_Equality_Op
8996 begin
8997 if T = Any_Fixed then
8998 T := Unique_Fixed_Point_Type (L);
8999 end if;
9001 Set_Etype (N, Base_Type (Typ));
9002 Generate_Reference (T, N, ' ');
9004 if T = Any_Type then
9005 -- Deal with explicit ambiguity of operands, unless this is a call
9006 -- to the implicit inequality operator created for a user-defined
9007 -- operator that is not an intrinsic subprogram, since the common
9008 -- resolution of operands done here does not apply to it.
9010 if not Implicit_NE_For_User_Defined_Operator
9011 and then (Is_Overloaded (L) or else Is_Overloaded (R))
9012 then
9013 Ambiguous_Operands (N);
9014 end if;
9016 else
9018 -- For Ada 2022, check for user-defined literals when the type has
9019 -- the appropriate aspect.
9021 if Has_Applicable_User_Defined_Literal (L, Etype (R)) then
9022 Resolve (L, Etype (R));
9023 Set_Etype (N, Standard_Boolean);
9024 end if;
9026 if Has_Applicable_User_Defined_Literal (R, Etype (L)) then
9027 Resolve (R, Etype (L));
9028 Set_Etype (N, Standard_Boolean);
9029 end if;
9031 -- Deal with other error cases
9033 if T = Any_String or else
9034 T = Any_Composite or else
9035 T = Any_Character
9036 then
9037 if T = Any_Character then
9038 Ambiguous_Character (L);
9039 else
9040 Error_Msg_N ("ambiguous operands for equality", N);
9041 end if;
9043 Set_Etype (N, Any_Type);
9044 return;
9046 elsif T = Universal_Access
9047 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
9048 then
9049 T := Find_Unique_Access_Type;
9051 if No (T) then
9052 Error_Msg_N ("ambiguous operands for equality", N);
9053 Set_Etype (N, Any_Type);
9054 return;
9055 end if;
9057 -- If expressions must have a single type, and if the context does
9058 -- not impose one the dependent expressions cannot be anonymous
9059 -- access types.
9061 -- Why no similar processing for case expressions???
9063 elsif Ada_Version >= Ada_2012
9064 and then Is_Anonymous_Access_Type (Etype (L))
9065 and then Is_Anonymous_Access_Type (Etype (R))
9066 then
9067 Check_If_Expression (L);
9068 Check_If_Expression (R);
9069 end if;
9071 -- RM 4.5.2(9.5/2): At least one of the operands of the equality
9072 -- operators for universal_access shall be of type universal_access,
9073 -- or both shall be of access-to-object types, or both shall be of
9074 -- access-to-subprogram types (RM 4.5.2(9.5/2)).
9076 if Is_Anonymous_Access_Type (T)
9077 and then Etype (L) /= Universal_Access
9078 and then Etype (R) /= Universal_Access
9079 then
9080 -- RM 4.5.2(9.6/2): When both are of access-to-object types, the
9081 -- designated types shall be the same or one shall cover the other
9082 -- and if the designated types are elementary or array types, then
9083 -- the designated subtypes shall statically match.
9085 if Is_Access_Object_Type (Etype (L))
9086 and then Is_Access_Object_Type (Etype (R))
9087 then
9088 Check_Designated_Object_Types
9089 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
9091 -- RM 4.5.2(9.7/2): When both are of access-to-subprogram types,
9092 -- the designated profiles shall be subtype conformant.
9094 elsif Is_Access_Subprogram_Type (Etype (L))
9095 and then Is_Access_Subprogram_Type (Etype (R))
9096 then
9097 Check_Designated_Subprogram_Types
9098 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
9099 end if;
9100 end if;
9102 -- Check another case of equality operators for universal_access
9104 if Is_Anonymous_Access_Type (T) and then Comes_From_Source (N) then
9105 Check_Access_Attribute (L);
9106 Check_Access_Attribute (R);
9107 end if;
9109 Resolve (L, T);
9110 Resolve (R, T);
9112 -- AI12-0413: user-defined primitive equality of an untagged record
9113 -- type hides the predefined equality operator, including within a
9114 -- generic, and if it is declared abstract, results in an illegal
9115 -- instance if the operator is used in the spec, or in the raising
9116 -- of Program_Error if used in the body of an instance.
9118 if Nkind (N) = N_Op_Eq
9119 and then In_Instance
9120 and then Ada_Version >= Ada_2012
9121 then
9122 declare
9123 U : constant Entity_Id := Underlying_Type (T);
9125 Eq : Entity_Id;
9127 begin
9128 if Present (U)
9129 and then Is_Record_Type (U)
9130 and then not Is_Tagged_Type (U)
9131 then
9132 Eq := Get_User_Defined_Equality (T);
9134 if Present (Eq) then
9135 if Is_Abstract_Subprogram (Eq) then
9136 Nondispatching_Call_To_Abstract_Operation (N, Eq);
9137 else
9138 Rewrite_Operator_As_Call (N, Eq);
9139 end if;
9141 return;
9142 end if;
9143 end if;
9144 end;
9145 end if;
9147 -- If the unique type is a class-wide type then it will be expanded
9148 -- into a dispatching call to the predefined primitive. Therefore we
9149 -- check here for potential violation of such restriction.
9151 if Is_Class_Wide_Type (T) then
9152 Check_Restriction (No_Dispatching_Calls, N);
9153 end if;
9155 -- Only warn for redundant equality comparison to True for objects
9156 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
9157 -- other expressions, it may be a matter of preference to write
9158 -- "Expr = True" or "Expr".
9160 if Warn_On_Redundant_Constructs
9161 and then Comes_From_Source (N)
9162 and then Comes_From_Source (R)
9163 and then Is_Entity_Name (R)
9164 and then Entity (R) = Standard_True
9165 and then
9166 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
9167 or else
9168 Nkind (L) in N_Op)
9169 then
9170 Error_Msg_N -- CODEFIX
9171 ("?r?comparison with True is redundant!", N);
9172 Explain_Redundancy (Original_Node (R));
9173 end if;
9175 -- Warn on a (in)equality between boolean values which is not
9176 -- parenthesized when the parent expression is one of and/or/xor, as
9177 -- this is interpreted as (a = b) op c where most likely a = (b op c)
9178 -- was intended. Do not generate a warning in generic instances, as
9179 -- the problematic expression may be implicitly parenthesized in
9180 -- the generic itself if one of the operators is a generic formal.
9181 -- Also do not generate a warning for generated equality, for
9182 -- example from rewritting a membership test.
9184 if Warn_On_Questionable_Missing_Parens
9185 and then not In_Instance
9186 and then Comes_From_Source (N)
9187 and then Is_Boolean_Type (T)
9188 and then Suspicious_Prio_For_Equality
9189 then
9190 Error_Msg_N ("?q?equality should be parenthesized here!", N);
9191 end if;
9193 Check_Unset_Reference (L);
9194 Check_Unset_Reference (R);
9195 Generate_Operator_Reference (N, T);
9196 Check_Low_Bound_Tested (N);
9198 -- Unless this is a call to the implicit inequality operator created
9199 -- for a user-defined operator that is not an intrinsic subprogram,
9200 -- try to fold the operation.
9202 if not Implicit_NE_For_User_Defined_Operator then
9203 Analyze_Dimension (N);
9204 Eval_Relational_Op (N);
9206 elsif Nkind (N) = N_Op_Ne
9207 and then Is_Abstract_Subprogram (Entity (N))
9208 then
9209 Nondispatching_Call_To_Abstract_Operation (N, Entity (N));
9210 end if;
9211 end if;
9212 end Resolve_Equality_Op;
9214 ----------------------------------
9215 -- Resolve_Explicit_Dereference --
9216 ----------------------------------
9218 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
9219 Loc : constant Source_Ptr := Sloc (N);
9220 New_N : Node_Id;
9221 P : constant Node_Id := Prefix (N);
9223 P_Typ : Entity_Id;
9224 -- The candidate prefix type, if overloaded
9226 I : Interp_Index;
9227 It : Interp;
9229 begin
9230 Check_Fully_Declared_Prefix (Typ, P);
9231 P_Typ := Empty;
9233 -- A useful optimization: check whether the dereference denotes an
9234 -- element of a container, and if so rewrite it as a call to the
9235 -- corresponding Element function.
9237 -- Disabled for now, on advice of ARG. A more restricted form of the
9238 -- predicate might be acceptable ???
9240 -- if Is_Container_Element (N) then
9241 -- return;
9242 -- end if;
9244 if Is_Overloaded (P) then
9246 -- Use the context type to select the prefix that has the correct
9247 -- designated type. Keep the first match, which will be the inner-
9248 -- most.
9250 Get_First_Interp (P, I, It);
9252 while Present (It.Typ) loop
9253 if Is_Access_Type (It.Typ)
9254 and then Covers (Typ, Designated_Type (It.Typ))
9255 then
9256 if No (P_Typ) then
9257 P_Typ := It.Typ;
9258 end if;
9260 -- Remove access types that do not match, but preserve access
9261 -- to subprogram interpretations, in case a further dereference
9262 -- is needed (see below).
9264 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
9265 Remove_Interp (I);
9266 end if;
9268 Get_Next_Interp (I, It);
9269 end loop;
9271 if Present (P_Typ) then
9272 Resolve (P, P_Typ);
9273 Set_Etype (N, Designated_Type (P_Typ));
9275 else
9276 -- If no interpretation covers the designated type of the prefix,
9277 -- this is the pathological case where not all implementations of
9278 -- the prefix allow the interpretation of the node as a call. Now
9279 -- that the expected type is known, Remove other interpretations
9280 -- from prefix, rewrite it as a call, and resolve again, so that
9281 -- the proper call node is generated.
9283 Get_First_Interp (P, I, It);
9284 while Present (It.Typ) loop
9285 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
9286 Remove_Interp (I);
9287 end if;
9289 Get_Next_Interp (I, It);
9290 end loop;
9292 New_N :=
9293 Make_Function_Call (Loc,
9294 Name =>
9295 Make_Explicit_Dereference (Loc,
9296 Prefix => P),
9297 Parameter_Associations => New_List);
9299 Save_Interps (N, New_N);
9300 Rewrite (N, New_N);
9301 Analyze_And_Resolve (N, Typ);
9302 return;
9303 end if;
9305 -- If not overloaded, resolve P with its own type
9307 else
9308 Resolve (P);
9309 end if;
9311 -- If the prefix might be null, add an access check
9313 if Is_Access_Type (Etype (P))
9314 and then not Can_Never_Be_Null (Etype (P))
9315 then
9316 Apply_Access_Check (N);
9317 end if;
9319 -- If the designated type is a packed unconstrained array type, and the
9320 -- explicit dereference is not in the context of an attribute reference,
9321 -- then we must compute and set the actual subtype, since it is needed
9322 -- by Gigi. The reason we exclude the attribute case is that this is
9323 -- handled fine by Gigi, and in fact we use such attributes to build the
9324 -- actual subtype. We also exclude generated code (which builds actual
9325 -- subtypes directly if they are needed).
9327 if Is_Packed_Array (Etype (N))
9328 and then not Is_Constrained (Etype (N))
9329 and then Nkind (Parent (N)) /= N_Attribute_Reference
9330 and then Comes_From_Source (N)
9331 then
9332 Set_Etype (N, Get_Actual_Subtype (N));
9333 end if;
9335 Analyze_Dimension (N);
9337 -- Note: No Eval processing is required for an explicit dereference,
9338 -- because such a name can never be static.
9340 end Resolve_Explicit_Dereference;
9342 -------------------------------------
9343 -- Resolve_Expression_With_Actions --
9344 -------------------------------------
9346 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9348 function OK_For_Static (Act : Node_Id) return Boolean;
9349 -- True if Act is an action of a declare_expression that is allowed in a
9350 -- static declare_expression.
9352 function All_OK_For_Static return Boolean;
9353 -- True if all actions of N are allowed in a static declare_expression.
9355 function Get_Literal (Expr : Node_Id) return Node_Id;
9356 -- Expr is an expression with compile-time-known value. This returns the
9357 -- literal node that reprsents that value.
9359 -------------------
9360 -- OK_For_Static --
9361 -------------------
9363 function OK_For_Static (Act : Node_Id) return Boolean is
9364 begin
9365 case Nkind (Act) is
9366 when N_Object_Declaration =>
9367 if Constant_Present (Act)
9368 and then Is_Static_Expression (Expression (Act))
9369 then
9370 return True;
9371 end if;
9373 when N_Object_Renaming_Declaration =>
9374 if Statically_Names_Object (Name (Act)) then
9375 return True;
9376 end if;
9378 when others =>
9379 -- No other declarations, nor even pragmas, are allowed in a
9380 -- declare expression, so if we see something else, it must be
9381 -- an internally generated expression_with_actions.
9382 null;
9383 end case;
9385 return False;
9386 end OK_For_Static;
9388 -----------------------
9389 -- All_OK_For_Static --
9390 -----------------------
9392 function All_OK_For_Static return Boolean is
9393 Act : Node_Id := First (Actions (N));
9394 begin
9395 while Present (Act) loop
9396 if not OK_For_Static (Act) then
9397 return False;
9398 end if;
9400 Next (Act);
9401 end loop;
9403 return True;
9404 end All_OK_For_Static;
9406 -----------------
9407 -- Get_Literal --
9408 -----------------
9410 function Get_Literal (Expr : Node_Id) return Node_Id is
9411 pragma Assert (Compile_Time_Known_Value (Expr));
9412 Result : Node_Id;
9413 begin
9414 case Nkind (Expr) is
9415 when N_Has_Entity =>
9416 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9417 Result := Expr;
9418 else
9419 Result := Constant_Value (Entity (Expr));
9420 end if;
9421 when N_Numeric_Or_String_Literal =>
9422 Result := Expr;
9423 when others =>
9424 raise Program_Error;
9425 end case;
9427 pragma Assert
9428 (Nkind (Result) in N_Numeric_Or_String_Literal
9429 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9430 return Result;
9431 end Get_Literal;
9433 -- Local variables
9435 Loc : constant Source_Ptr := Sloc (N);
9437 -- Start of processing for Resolve_Expression_With_Actions
9439 begin
9440 Set_Etype (N, Typ);
9442 if Is_Empty_List (Actions (N)) then
9443 pragma Assert (All_OK_For_Static); null;
9444 end if;
9446 -- If the value of the expression is known at compile time, and all
9447 -- of the actions (if any) are suitable, then replace the declare
9448 -- expression with its expression. This allows the declare expression
9449 -- as a whole to be static if appropriate. See AI12-0368.
9451 if Compile_Time_Known_Value (Expression (N)) then
9452 if Is_Empty_List (Actions (N)) then
9453 Rewrite (N, Expression (N));
9454 elsif All_OK_For_Static then
9455 Rewrite
9456 (N, New_Copy_Tree
9457 (Get_Literal (Expression (N)), New_Sloc => Loc));
9458 end if;
9459 end if;
9460 end Resolve_Expression_With_Actions;
9462 ----------------------------------
9463 -- Resolve_Generalized_Indexing --
9464 ----------------------------------
9466 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9467 Indexing : constant Node_Id := Generalized_Indexing (N);
9468 begin
9469 Rewrite (N, Indexing);
9470 Resolve (N, Typ);
9471 end Resolve_Generalized_Indexing;
9473 ---------------------------
9474 -- Resolve_If_Expression --
9475 ---------------------------
9477 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9478 Condition : constant Node_Id := First (Expressions (N));
9480 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id);
9481 -- When a dependent expression is of a subtype different from
9482 -- the context subtype, then insert a qualification to ensure
9483 -- the generation of a constraint check. This was previously
9484 -- for scalar types. For array types apply a length check, given
9485 -- that the context in general allows sliding, while a qualified
9486 -- expression forces equality of bounds.
9488 -----------------
9489 -- Apply_Check --
9490 -----------------
9492 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id) is
9493 Expr_Typ : constant Entity_Id := Etype (Expr);
9494 Loc : constant Source_Ptr := Sloc (Expr);
9496 begin
9497 if Expr_Typ = Typ
9498 or else Is_Tagged_Type (Typ)
9499 or else Is_Access_Type (Typ)
9500 or else not Is_Constrained (Typ)
9501 or else Inside_A_Generic
9502 then
9503 null;
9505 elsif Is_Array_Type (Typ) then
9506 Apply_Length_Check (Expr, Typ);
9508 else
9509 Rewrite (Expr,
9510 Make_Qualified_Expression (Loc,
9511 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9512 Expression => Relocate_Node (Expr)));
9514 Analyze_And_Resolve (Expr, Result_Type);
9515 end if;
9516 end Apply_Check;
9518 -- Local variables
9520 Else_Expr : Node_Id;
9521 Then_Expr : Node_Id;
9523 Result_Type : Entity_Id;
9524 -- So in most cases the type of the if_expression and of its
9525 -- dependent expressions is that of the context. However, if
9526 -- the expression is the index of an Indexed_Component, we must
9527 -- ensure that a proper index check is applied, rather than a
9528 -- range check on the index type (which might be discriminant
9529 -- dependent). In this case we resolve with the base type of the
9530 -- index type, and the index check is generated in the resolution
9531 -- of the indexed_component above.
9533 -- Start of processing for Resolve_If_Expression
9535 begin
9536 -- Defend against malformed expressions
9538 if No (Condition) then
9539 return;
9540 end if;
9542 if Present (Parent (N))
9543 and then (Nkind (Parent (N)) = N_Indexed_Component
9544 or else Nkind (Parent (Parent (N))) = N_Indexed_Component)
9545 then
9546 Result_Type := Base_Type (Typ);
9548 else
9549 Result_Type := Typ;
9550 end if;
9552 Then_Expr := Next (Condition);
9554 if No (Then_Expr) then
9555 return;
9556 end if;
9558 Resolve (Condition, Any_Boolean);
9559 Check_Unset_Reference (Condition);
9561 Resolve_Dependent_Expression (N, Then_Expr, Result_Type);
9563 Check_Unset_Reference (Then_Expr);
9564 Apply_Check (Then_Expr, Result_Type);
9566 Else_Expr := Next (Then_Expr);
9568 -- If ELSE expression present, just resolve using the determined type
9570 if Present (Else_Expr) then
9571 Resolve_Dependent_Expression (N, Else_Expr, Result_Type);
9573 Check_Unset_Reference (Else_Expr);
9574 Apply_Check (Else_Expr, Result_Type);
9576 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9577 -- dynamically tagged must be known statically.
9579 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9580 if Is_Dynamically_Tagged (Then_Expr) /=
9581 Is_Dynamically_Tagged (Else_Expr)
9582 then
9583 Error_Msg_N ("all or none of the dependent expressions "
9584 & "can be dynamically tagged", N);
9585 end if;
9586 end if;
9588 -- If no ELSE expression is present, root type must be Standard.Boolean
9589 -- and we provide a Standard.True result converted to the appropriate
9590 -- Boolean type (in case it is a derived boolean type).
9592 elsif Root_Type (Typ) = Standard_Boolean then
9593 Else_Expr :=
9594 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9595 Analyze_And_Resolve (Else_Expr, Result_Type);
9596 Append_To (Expressions (N), Else_Expr);
9598 else
9599 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9600 Append_To (Expressions (N), Error);
9601 end if;
9603 Set_Etype (N, Result_Type);
9605 if not Error_Posted (N) then
9606 Eval_If_Expression (N);
9607 end if;
9609 Analyze_Dimension (N);
9610 end Resolve_If_Expression;
9612 ----------------------------------
9613 -- Resolve_Implicit_Dereference --
9614 ----------------------------------
9616 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9617 Desig_Typ : Entity_Id;
9619 begin
9620 if Is_Access_Type (Etype (P)) then
9621 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9622 Insert_Explicit_Dereference (P);
9623 Analyze_And_Resolve (P, Desig_Typ);
9624 end if;
9625 end Resolve_Implicit_Dereference;
9627 -------------------------------
9628 -- Resolve_Indexed_Component --
9629 -------------------------------
9631 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9632 Pref : constant Node_Id := Prefix (N);
9633 Expr : Node_Id;
9634 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9635 Index : Node_Id;
9637 begin
9638 if Present (Generalized_Indexing (N)) then
9639 Resolve_Generalized_Indexing (N, Typ);
9640 return;
9641 end if;
9643 if Is_Overloaded (Pref) then
9645 -- Use the context type to select the prefix that yields the correct
9646 -- component type.
9648 declare
9649 I : Interp_Index;
9650 It : Interp;
9651 I1 : Interp_Index := 0;
9652 Found : Boolean := False;
9654 begin
9655 Get_First_Interp (Pref, I, It);
9656 while Present (It.Typ) loop
9657 if (Is_Array_Type (It.Typ)
9658 and then Covers (Typ, Component_Type (It.Typ)))
9659 or else (Is_Access_Type (It.Typ)
9660 and then Is_Array_Type (Designated_Type (It.Typ))
9661 and then
9662 Covers
9663 (Typ,
9664 Component_Type (Designated_Type (It.Typ))))
9665 then
9666 if Found then
9667 It := Disambiguate (Pref, I1, I, Any_Type);
9669 if It = No_Interp then
9670 Error_Msg_N ("ambiguous prefix for indexing", N);
9671 Set_Etype (N, Typ);
9672 return;
9674 else
9675 Found := True;
9676 Array_Type := It.Typ;
9677 I1 := I;
9678 end if;
9680 else
9681 Found := True;
9682 Array_Type := It.Typ;
9683 I1 := I;
9684 end if;
9685 end if;
9687 Get_Next_Interp (I, It);
9688 end loop;
9689 end;
9691 else
9692 Array_Type := Etype (Pref);
9693 end if;
9695 Resolve (Pref, Array_Type);
9696 Array_Type := Get_Actual_Subtype_If_Available (Pref);
9698 -- If the prefix's type is an access type, get to the real array type.
9699 -- Note: we do not apply an access check because an explicit dereference
9700 -- will be introduced later, and the check will happen there.
9702 if Is_Access_Type (Array_Type) then
9703 Array_Type := Implicitly_Designated_Type (Array_Type);
9704 end if;
9706 -- If name was overloaded, set component type correctly now.
9707 -- If a misplaced call to an entry family (which has no index types)
9708 -- return. Error will be diagnosed from calling context.
9710 if Is_Array_Type (Array_Type) then
9711 Set_Etype (N, Component_Type (Array_Type));
9712 else
9713 return;
9714 end if;
9716 Index := First_Index (Array_Type);
9717 Expr := First (Expressions (N));
9719 -- The prefix may have resolved to a string literal, in which case its
9720 -- etype has a special representation. This is only possible currently
9721 -- if the prefix is a static concatenation, written in functional
9722 -- notation.
9724 if Ekind (Array_Type) = E_String_Literal_Subtype then
9725 Resolve (Expr, Standard_Positive);
9727 else
9728 while Present (Index) and then Present (Expr) loop
9729 Resolve (Expr, Etype (Index));
9730 Check_Unset_Reference (Expr);
9732 Apply_Scalar_Range_Check (Expr, Etype (Index));
9734 Next_Index (Index);
9735 Next (Expr);
9736 end loop;
9737 end if;
9739 Resolve_Implicit_Dereference (Pref);
9740 Analyze_Dimension (N);
9742 -- Do not generate the warning on suspicious index if we are analyzing
9743 -- package Ada.Tags; otherwise we will report the warning with the
9744 -- Prims_Ptr field of the dispatch table.
9746 if Scope (Etype (Pref)) = Standard_Standard
9747 or else not
9748 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Pref))), Ada_Tags)
9749 then
9750 Warn_On_Suspicious_Index (Pref, First (Expressions (N)));
9751 Eval_Indexed_Component (N);
9752 end if;
9754 -- If the array type is atomic and the component is not, then this is
9755 -- worth a warning before Ada 2022, since we have a situation where the
9756 -- access to the component may cause extra read/writes of the atomic
9757 -- object, or partial word accesses, both of which may be unexpected.
9759 if Nkind (N) = N_Indexed_Component
9760 and then Is_Atomic_Ref_With_Address (N)
9761 and then not (Has_Atomic_Components (Array_Type)
9762 or else (Is_Entity_Name (Pref)
9763 and then Has_Atomic_Components
9764 (Entity (Pref))))
9765 and then not Is_Atomic (Component_Type (Array_Type))
9766 and then Ada_Version < Ada_2022
9767 then
9768 Error_Msg_N
9769 ("??access to non-atomic component of atomic array", Pref);
9770 Error_Msg_N
9771 ("??\may cause unexpected accesses to atomic object", Pref);
9772 end if;
9773 end Resolve_Indexed_Component;
9775 -----------------------------
9776 -- Resolve_Integer_Literal --
9777 -----------------------------
9779 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9780 begin
9781 Set_Etype (N, Typ);
9782 Eval_Integer_Literal (N);
9783 end Resolve_Integer_Literal;
9785 -----------------------------------------
9786 -- Resolve_Interpolated_String_Literal --
9787 -----------------------------------------
9789 procedure Resolve_Interpolated_String_Literal (N : Node_Id; Typ : Entity_Id)
9791 Str_Elem : Node_Id;
9793 begin
9794 Str_Elem := First (Expressions (N));
9795 pragma Assert (Nkind (Str_Elem) = N_String_Literal);
9797 while Present (Str_Elem) loop
9799 -- Resolve string elements using the context type; for interpolated
9800 -- expressions there is no need to check if their type has a suitable
9801 -- image function because under Ada 2022 all the types have such
9802 -- function available.
9804 if Etype (Str_Elem) = Any_String then
9805 Resolve (Str_Elem, Typ);
9806 end if;
9808 Next (Str_Elem);
9809 end loop;
9811 Set_Etype (N, Typ);
9812 end Resolve_Interpolated_String_Literal;
9814 --------------------------------
9815 -- Resolve_Intrinsic_Operator --
9816 --------------------------------
9818 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9819 Is_Stoele_Mod : constant Boolean :=
9820 Nkind (N) = N_Op_Mod
9821 and then Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
9822 and then Is_RTE (Etype (Left_Opnd (N)), RE_Address);
9823 -- True if this is the special mod operator of System.Storage_Elements,
9824 -- which needs to be resolved to the type of the left operand in order
9825 -- to implement the correct semantics.
9827 Btyp : constant Entity_Id :=
9828 (if Is_Stoele_Mod
9829 then Implementation_Base_Type (Etype (Left_Opnd (N)))
9830 else Implementation_Base_Type (Typ));
9831 -- The base type to be used for the operator
9833 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9834 -- If the operand is a literal, it cannot be the expression in a
9835 -- conversion. Use a qualified expression instead.
9837 ---------------------
9838 -- Convert_Operand --
9839 ---------------------
9841 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9842 Loc : constant Source_Ptr := Sloc (Opnd);
9843 Res : Node_Id;
9845 begin
9846 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9847 Res :=
9848 Make_Qualified_Expression (Loc,
9849 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9850 Expression => Relocate_Node (Opnd));
9851 Analyze (Res);
9853 else
9854 Res := Unchecked_Convert_To (Btyp, Opnd);
9855 end if;
9857 return Res;
9858 end Convert_Operand;
9860 -- Local variables
9862 Arg1 : Node_Id;
9863 Arg2 : Node_Id;
9864 Op : Entity_Id;
9866 -- Start of processing for Resolve_Intrinsic_Operator
9868 begin
9869 -- We must preserve the original entity in a generic setting, so that
9870 -- the legality of the operation can be verified in an instance.
9872 if not Expander_Active then
9873 return;
9874 end if;
9876 Op := Entity (N);
9877 while Scope (Op) /= Standard_Standard loop
9878 Op := Homonym (Op);
9879 pragma Assert (Present (Op));
9880 end loop;
9882 Set_Entity (N, Op);
9883 Set_Is_Overloaded (N, False);
9885 -- If the result or operand types are private, rewrite with unchecked
9886 -- conversions on the operands and the result, to expose the proper
9887 -- underlying numeric type. Likewise for the special mod operator of
9888 -- System.Storage_Elements, to expose the modified base type.
9890 if Is_Private_Type (Typ)
9891 or else Is_Private_Type (Etype (Left_Opnd (N)))
9892 or else Is_Private_Type (Etype (Right_Opnd (N)))
9893 or else Is_Stoele_Mod
9894 then
9895 Arg1 := Convert_Operand (Left_Opnd (N));
9897 if Nkind (N) = N_Op_Expon then
9898 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9899 else
9900 Arg2 := Convert_Operand (Right_Opnd (N));
9901 end if;
9903 if Nkind (Arg1) = N_Type_Conversion then
9904 Save_Interps (Left_Opnd (N), Expression (Arg1));
9905 end if;
9907 if Nkind (Arg2) = N_Type_Conversion then
9908 Save_Interps (Right_Opnd (N), Expression (Arg2));
9909 end if;
9911 Set_Left_Opnd (N, Arg1);
9912 Set_Right_Opnd (N, Arg2);
9914 Set_Etype (N, Btyp);
9915 Rewrite (N, Unchecked_Convert_To (Typ, N));
9916 Resolve (N, Typ);
9918 elsif Typ /= Etype (Left_Opnd (N))
9919 or else Typ /= Etype (Right_Opnd (N))
9920 then
9921 -- Add explicit conversion where needed, and save interpretations in
9922 -- case operands are overloaded.
9924 Arg1 := Convert_To (Typ, Left_Opnd (N));
9925 Arg2 := Convert_To (Typ, Right_Opnd (N));
9927 if Nkind (Arg1) = N_Type_Conversion then
9928 Save_Interps (Left_Opnd (N), Expression (Arg1));
9929 else
9930 Save_Interps (Left_Opnd (N), Arg1);
9931 end if;
9933 if Nkind (Arg2) = N_Type_Conversion then
9934 Save_Interps (Right_Opnd (N), Expression (Arg2));
9935 else
9936 Save_Interps (Right_Opnd (N), Arg2);
9937 end if;
9939 Rewrite (Left_Opnd (N), Arg1);
9940 Rewrite (Right_Opnd (N), Arg2);
9941 Analyze (Arg1);
9942 Analyze (Arg2);
9943 Resolve_Arithmetic_Op (N, Typ);
9945 else
9946 Resolve_Arithmetic_Op (N, Typ);
9947 end if;
9948 end Resolve_Intrinsic_Operator;
9950 --------------------------------------
9951 -- Resolve_Intrinsic_Unary_Operator --
9952 --------------------------------------
9954 procedure Resolve_Intrinsic_Unary_Operator
9955 (N : Node_Id;
9956 Typ : Entity_Id)
9958 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9959 Op : Entity_Id;
9960 Arg2 : Node_Id;
9962 begin
9963 Op := Entity (N);
9964 while Scope (Op) /= Standard_Standard loop
9965 Op := Homonym (Op);
9966 pragma Assert (Present (Op));
9967 end loop;
9969 Set_Entity (N, Op);
9971 if Is_Private_Type (Typ) then
9972 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9973 Save_Interps (Right_Opnd (N), Expression (Arg2));
9975 Set_Right_Opnd (N, Arg2);
9977 Set_Etype (N, Btyp);
9978 Rewrite (N, Unchecked_Convert_To (Typ, N));
9979 Resolve (N, Typ);
9981 else
9982 Resolve_Unary_Op (N, Typ);
9983 end if;
9984 end Resolve_Intrinsic_Unary_Operator;
9986 ------------------------
9987 -- Resolve_Logical_Op --
9988 ------------------------
9990 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9991 B_Typ : Entity_Id;
9993 begin
9994 Check_No_Direct_Boolean_Operators (N);
9996 -- Predefined operations on scalar types yield the base type. On the
9997 -- other hand, logical operations on arrays yield the type of the
9998 -- arguments (and the context).
10000 if Is_Array_Type (Typ) then
10001 B_Typ := Typ;
10002 else
10003 B_Typ := Base_Type (Typ);
10004 end if;
10006 -- The following test is required because the operands of the operation
10007 -- may be literals, in which case the resulting type appears to be
10008 -- compatible with a signed integer type, when in fact it is compatible
10009 -- only with modular types. If the context itself is universal, the
10010 -- operation is illegal.
10012 if not Valid_Boolean_Arg (Typ) then
10013 Error_Msg_N ("invalid context for logical operation", N);
10014 Set_Etype (N, Any_Type);
10015 return;
10017 elsif Typ = Any_Modular then
10018 Error_Msg_N
10019 ("no modular type available in this context", N);
10020 Set_Etype (N, Any_Type);
10021 return;
10023 elsif Is_Modular_Integer_Type (Typ)
10024 and then Etype (Left_Opnd (N)) = Universal_Integer
10025 and then Etype (Right_Opnd (N)) = Universal_Integer
10026 then
10027 Check_For_Visible_Operator (N, B_Typ);
10028 end if;
10030 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
10031 -- is active and the result type is standard Boolean (do not mess with
10032 -- ops that return a nonstandard Boolean type, because something strange
10033 -- is going on).
10035 -- Note: you might expect this replacement to be done during expansion,
10036 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
10037 -- is used, no part of the right operand of an "and" or "or" operator
10038 -- should be executed if the left operand would short-circuit the
10039 -- evaluation of the corresponding "and then" or "or else". If we left
10040 -- the replacement to expansion time, then run-time checks associated
10041 -- with such operands would be evaluated unconditionally, due to being
10042 -- before the condition prior to the rewriting as short-circuit forms
10043 -- during expansion.
10045 if Short_Circuit_And_Or
10046 and then B_Typ = Standard_Boolean
10047 and then Nkind (N) in N_Op_And | N_Op_Or
10048 then
10049 -- Mark the corresponding putative SCO operator as truly a logical
10050 -- (and short-circuit) operator.
10052 if Generate_SCO and then Comes_From_Source (N) then
10053 Set_SCO_Logical_Operator (N);
10054 end if;
10056 if Nkind (N) = N_Op_And then
10057 Rewrite (N,
10058 Make_And_Then (Sloc (N),
10059 Left_Opnd => Relocate_Node (Left_Opnd (N)),
10060 Right_Opnd => Relocate_Node (Right_Opnd (N))));
10061 Analyze_And_Resolve (N, B_Typ);
10063 -- Case of OR changed to OR ELSE
10065 else
10066 Rewrite (N,
10067 Make_Or_Else (Sloc (N),
10068 Left_Opnd => Relocate_Node (Left_Opnd (N)),
10069 Right_Opnd => Relocate_Node (Right_Opnd (N))));
10070 Analyze_And_Resolve (N, B_Typ);
10071 end if;
10073 -- Return now, since analysis of the rewritten ops will take care of
10074 -- other reference bookkeeping and expression folding.
10076 return;
10077 end if;
10079 Resolve (Left_Opnd (N), B_Typ);
10080 Resolve (Right_Opnd (N), B_Typ);
10082 Check_Unset_Reference (Left_Opnd (N));
10083 Check_Unset_Reference (Right_Opnd (N));
10085 Set_Etype (N, B_Typ);
10086 Generate_Operator_Reference (N, B_Typ);
10087 Eval_Logical_Op (N);
10088 end Resolve_Logical_Op;
10090 ---------------------------------
10091 -- Resolve_Membership_Equality --
10092 ---------------------------------
10094 procedure Resolve_Membership_Equality (N : Node_Id; Typ : Entity_Id) is
10095 Utyp : constant Entity_Id := Underlying_Type (Typ);
10097 begin
10098 -- RM 4.5.2(4.1/3): if the type is limited, then it shall have a visible
10099 -- primitive equality operator. This means that we can use the regular
10100 -- visibility-based resolution and reset Entity in order to trigger it.
10102 if Is_Limited_Type (Typ) then
10103 Set_Entity (N, Empty);
10105 -- RM 4.5.2(28.1/3): if the type is a record, then the membership test
10106 -- uses the primitive equality for the type [even if it is not visible].
10107 -- We only deal with the untagged case here, because the tagged case is
10108 -- handled uniformly in the expander.
10110 elsif Is_Record_Type (Utyp) and then not Is_Tagged_Type (Utyp) then
10111 declare
10112 Eq_Id : constant Entity_Id := Get_User_Defined_Equality (Typ);
10114 begin
10115 if Present (Eq_Id) then
10116 Rewrite_Operator_As_Call (N, Eq_Id);
10117 end if;
10118 end;
10119 end if;
10120 end Resolve_Membership_Equality;
10122 ---------------------------
10123 -- Resolve_Membership_Op --
10124 ---------------------------
10126 -- The context can only be a boolean type, and does not determine the
10127 -- arguments. Arguments should be unambiguous, but the preference rule for
10128 -- universal types applies.
10130 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
10131 pragma Assert (Is_Boolean_Type (Typ));
10133 L : constant Node_Id := Left_Opnd (N);
10134 R : constant Node_Id := Right_Opnd (N);
10135 T : Entity_Id;
10137 procedure Resolve_Set_Membership;
10138 -- Analysis has determined a unique type for the left operand. Use it as
10139 -- the basis to resolve the disjuncts.
10141 ----------------------------
10142 -- Resolve_Set_Membership --
10143 ----------------------------
10145 procedure Resolve_Set_Membership is
10146 Alt : Node_Id;
10148 begin
10149 -- If the left operand is overloaded, find type compatible with not
10150 -- overloaded alternative of the right operand.
10152 Alt := First (Alternatives (N));
10153 if Is_Overloaded (L) then
10154 T := Empty;
10155 while Present (Alt) loop
10156 if not Is_Overloaded (Alt) then
10157 T := Intersect_Types (L, Alt);
10158 exit;
10159 else
10160 Next (Alt);
10161 end if;
10162 end loop;
10164 -- Unclear how to resolve expression if all alternatives are also
10165 -- overloaded.
10167 if No (T) then
10168 Error_Msg_N ("ambiguous expression", N);
10169 end if;
10171 else
10172 T := Intersect_Types (L, Alt);
10173 end if;
10175 Resolve (L, T);
10177 Alt := First (Alternatives (N));
10178 while Present (Alt) loop
10180 -- Alternative is an expression, a range
10181 -- or a subtype mark.
10183 if not Is_Entity_Name (Alt)
10184 or else not Is_Type (Entity (Alt))
10185 then
10186 Resolve (Alt, T);
10187 end if;
10189 Next (Alt);
10190 end loop;
10192 -- Check for duplicates for discrete case
10194 if Is_Discrete_Type (T) then
10195 declare
10196 type Ent is record
10197 Alt : Node_Id;
10198 Val : Uint;
10199 end record;
10201 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
10202 Nalts : Nat;
10204 begin
10205 -- Loop checking duplicates. This is quadratic, but giant sets
10206 -- are unlikely in this context so it's a reasonable choice.
10208 Nalts := 0;
10209 Alt := First (Alternatives (N));
10210 while Present (Alt) loop
10211 if Is_OK_Static_Expression (Alt)
10212 and then Nkind (Alt) in N_Integer_Literal
10213 | N_Character_Literal
10214 | N_Has_Entity
10215 then
10216 Nalts := Nalts + 1;
10217 Alts (Nalts) := (Alt, Expr_Value (Alt));
10219 for J in 1 .. Nalts - 1 loop
10220 if Alts (J).Val = Alts (Nalts).Val then
10221 Error_Msg_Sloc := Sloc (Alts (J).Alt);
10222 Error_Msg_N ("duplicate of value given#??", Alt);
10223 end if;
10224 end loop;
10225 end if;
10227 Next (Alt);
10228 end loop;
10229 end;
10230 end if;
10232 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
10233 -- limited types, evaluation of a membership test uses the predefined
10234 -- equality for the type. This may be confusing to users, and the
10235 -- following warning appears useful for the most common case.
10237 if Is_Scalar_Type (Etype (L))
10238 and then Present (Get_User_Defined_Equality (Etype (L)))
10239 then
10240 Error_Msg_NE
10241 ("membership test on& uses predefined equality?", N, Etype (L));
10242 Error_Msg_N
10243 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
10244 end if;
10245 end Resolve_Set_Membership;
10247 -- Start of processing for Resolve_Membership_Op
10249 begin
10250 if L = Error or else R = Error then
10251 return;
10252 end if;
10254 if Present (Alternatives (N)) then
10255 Resolve_Set_Membership;
10256 goto SM_Exit;
10258 elsif not Is_Overloaded (R)
10259 and then Is_Universal_Numeric_Type (Etype (R))
10260 and then Is_Overloaded (L)
10261 then
10262 T := Etype (R);
10264 -- If the left operand is of a universal numeric type and the right
10265 -- operand is not, we do not resolve the operands to the tested type
10266 -- but to the universal type instead. If not conforming to the letter,
10267 -- it's conforming to the spirit of the specification of membership
10268 -- tests, which are typically used to guard a specific operation and
10269 -- ought not to fail a check in doing so. Without this, in the case of
10271 -- type Small_Length is range 1 .. 16;
10273 -- function Is_Small_String (S : String) return Boolean is
10274 -- begin
10275 -- return S'Length in Small_Length;
10276 -- end;
10278 -- the function Is_Small_String would fail a range check for strings
10279 -- larger than 127 characters.
10281 -- The test on the size is required in GNAT because universal_integer
10282 -- does not cover all the values of all the supported integer types,
10283 -- for example the large values of Long_Long_Long_Unsigned.
10285 elsif not Is_Overloaded (L)
10286 and then Is_Universal_Numeric_Type (Etype (L))
10287 and then (Is_Overloaded (R)
10288 or else
10289 (not Is_Universal_Numeric_Type (Etype (R))
10290 and then
10291 (not Is_Integer_Type (Etype (R))
10292 or else
10293 RM_Size (Etype (R)) < RM_Size (Universal_Integer))))
10294 then
10295 T := Etype (L);
10297 -- If the right operand is 'Range, we first need to resolve it (to
10298 -- the tested type) so that it is rewritten as an N_Range, before
10299 -- converting its bounds and resolving it again below.
10301 if Nkind (R) = N_Attribute_Reference
10302 and then Attribute_Name (R) = Name_Range
10303 then
10304 Resolve (R);
10305 end if;
10307 -- If the right operand is an N_Range, we convert its bounds to the
10308 -- universal type before resolving it.
10310 if Nkind (R) = N_Range then
10311 Rewrite (R,
10312 Make_Range (Sloc (R),
10313 Low_Bound => Convert_To (T, Low_Bound (R)),
10314 High_Bound => Convert_To (T, High_Bound (R))));
10315 Analyze (R);
10316 end if;
10318 -- Ada 2005 (AI-251): Support the following case:
10320 -- type I is interface;
10321 -- type T is tagged ...
10323 -- function Test (O : I'Class) is
10324 -- begin
10325 -- return O in T'Class.
10326 -- end Test;
10328 -- In this case we have nothing else to do. The membership test will be
10329 -- done at run time.
10331 elsif Ada_Version >= Ada_2005
10332 and then Is_Class_Wide_Type (Etype (L))
10333 and then Is_Interface (Etype (L))
10334 and then not Is_Interface (Etype (R))
10335 then
10336 return;
10338 else
10339 T := Intersect_Types (L, R);
10340 end if;
10342 -- If mixed-mode operations are present and operands are all literal,
10343 -- the only interpretation involves Duration, which is probably not
10344 -- the intention of the programmer.
10346 if T = Any_Fixed then
10347 T := Unique_Fixed_Point_Type (N);
10349 if T = Any_Type then
10350 return;
10351 end if;
10352 end if;
10354 Resolve (L, T);
10355 Check_Unset_Reference (L);
10357 if Nkind (R) = N_Range
10358 and then not Is_Scalar_Type (T)
10359 then
10360 Error_Msg_N ("scalar type required for range", R);
10361 end if;
10363 if Is_Entity_Name (R) then
10364 Freeze_Expression (R);
10365 else
10366 Resolve (R, T);
10367 Check_Unset_Reference (R);
10368 end if;
10370 -- Here after resolving membership operation
10372 <<SM_Exit>>
10374 Eval_Membership_Op (N);
10375 end Resolve_Membership_Op;
10377 ------------------
10378 -- Resolve_Null --
10379 ------------------
10381 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
10382 Loc : constant Source_Ptr := Sloc (N);
10384 begin
10385 -- Handle restriction against anonymous null access values This
10386 -- restriction can be turned off using -gnatdj.
10388 -- Ada 2005 (AI-231): Remove restriction
10390 if Ada_Version < Ada_2005
10391 and then not Debug_Flag_J
10392 and then Ekind (Typ) = E_Anonymous_Access_Type
10393 and then Comes_From_Source (N)
10394 then
10395 -- In the common case of a call which uses an explicitly null value
10396 -- for an access parameter, give specialized error message.
10398 if Nkind (Parent (N)) in N_Subprogram_Call then
10399 Error_Msg_N
10400 ("NULL is not allowed as argument for an access parameter", N);
10402 -- Standard message for all other cases (are there any?)
10404 else
10405 Error_Msg_N
10406 ("NULL cannot be of an anonymous access type", N);
10407 end if;
10408 end if;
10410 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
10411 -- assignment to a null-excluding object.
10413 if Ada_Version >= Ada_2005
10414 and then Can_Never_Be_Null (Typ)
10415 and then Nkind (Parent (N)) = N_Assignment_Statement
10416 then
10417 if Inside_Init_Proc then
10419 -- Decide whether to generate an if_statement around our
10420 -- null-excluding check to avoid them on certain internal object
10421 -- declarations by looking at the type the current Init_Proc
10422 -- belongs to.
10424 -- Generate:
10425 -- if T1b_skip_null_excluding_check then
10426 -- [constraint_error "access check failed"]
10427 -- end if;
10429 if Needs_Conditional_Null_Excluding_Check
10430 (Etype (First_Formal (Enclosing_Init_Proc)))
10431 then
10432 Insert_Action (N,
10433 Make_If_Statement (Loc,
10434 Condition =>
10435 Make_Identifier (Loc,
10436 New_External_Name
10437 (Chars (Typ), "_skip_null_excluding_check")),
10438 Then_Statements =>
10439 New_List (
10440 Make_Raise_Constraint_Error (Loc,
10441 Reason => CE_Access_Check_Failed))));
10443 -- Otherwise, simply create the check
10445 else
10446 Insert_Action (N,
10447 Make_Raise_Constraint_Error (Loc,
10448 Reason => CE_Access_Check_Failed));
10449 end if;
10450 else
10451 Insert_Action
10452 (Compile_Time_Constraint_Error (N,
10453 "(Ada 2005) NULL not allowed in null-excluding objects??"),
10454 Make_Raise_Constraint_Error (Loc,
10455 Reason => CE_Access_Check_Failed));
10456 end if;
10457 end if;
10459 -- In a distributed context, null for a remote access to subprogram may
10460 -- need to be replaced with a special record aggregate. In this case,
10461 -- return after having done the transformation.
10463 if (Ekind (Typ) = E_Record_Type
10464 or else Is_Remote_Access_To_Subprogram_Type (Typ))
10465 and then Remote_AST_Null_Value (N, Typ)
10466 then
10467 return;
10468 end if;
10470 -- The null literal takes its type from the context
10472 Set_Etype (N, Typ);
10473 end Resolve_Null;
10475 -----------------------
10476 -- Resolve_Op_Concat --
10477 -----------------------
10479 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
10481 -- We wish to avoid deep recursion, because concatenations are often
10482 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
10483 -- operands nonrecursively until we find something that is not a simple
10484 -- concatenation (A in this case). We resolve that, and then walk back
10485 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10486 -- to do the rest of the work at each level. The Parent pointers allow
10487 -- us to avoid recursion, and thus avoid running out of memory. See also
10488 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10490 NN : Node_Id := N;
10491 Op1 : Node_Id;
10493 begin
10494 -- The following code is equivalent to:
10496 -- Resolve_Op_Concat_First (NN, Typ);
10497 -- Resolve_Op_Concat_Arg (N, ...);
10498 -- Resolve_Op_Concat_Rest (N, Typ);
10500 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10501 -- operand is a concatenation.
10503 -- Walk down left operands
10505 loop
10506 Resolve_Op_Concat_First (NN, Typ);
10507 Op1 := Left_Opnd (NN);
10508 exit when not (Nkind (Op1) = N_Op_Concat
10509 and then not Is_Array_Type (Component_Type (Typ))
10510 and then Entity (Op1) = Entity (NN));
10511 NN := Op1;
10512 end loop;
10514 -- Now (given the above example) NN is A&B and Op1 is A
10516 -- First resolve Op1 ...
10518 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10520 -- ... then walk NN back up until we reach N (where we started), calling
10521 -- Resolve_Op_Concat_Rest along the way.
10523 loop
10524 Resolve_Op_Concat_Rest (NN, Typ);
10525 exit when NN = N;
10526 NN := Parent (NN);
10527 end loop;
10528 end Resolve_Op_Concat;
10530 ---------------------------
10531 -- Resolve_Op_Concat_Arg --
10532 ---------------------------
10534 procedure Resolve_Op_Concat_Arg
10535 (N : Node_Id;
10536 Arg : Node_Id;
10537 Typ : Entity_Id;
10538 Is_Comp : Boolean)
10540 Btyp : constant Entity_Id := Base_Type (Typ);
10541 Ctyp : constant Entity_Id := Component_Type (Typ);
10543 begin
10544 if In_Instance then
10545 if Is_Comp
10546 or else (not Is_Overloaded (Arg)
10547 and then Etype (Arg) /= Any_Composite
10548 and then Covers (Ctyp, Etype (Arg)))
10549 then
10550 Resolve (Arg, Ctyp);
10551 else
10552 Resolve (Arg, Btyp);
10553 end if;
10555 -- If both Array & Array and Array & Component are visible, there is a
10556 -- potential ambiguity that must be reported.
10558 elsif Has_Compatible_Type (Arg, Ctyp) then
10559 if Nkind (Arg) = N_Aggregate
10560 and then Is_Composite_Type (Ctyp)
10561 then
10562 if Is_Private_Type (Ctyp) then
10563 Resolve (Arg, Btyp);
10565 -- If the operation is user-defined and not overloaded use its
10566 -- profile. The operation may be a renaming, in which case it has
10567 -- been rewritten, and we want the original profile.
10569 elsif not Is_Overloaded (N)
10570 and then Comes_From_Source (Entity (Original_Node (N)))
10571 and then Ekind (Entity (Original_Node (N))) = E_Function
10572 then
10573 Resolve (Arg,
10574 Etype
10575 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10576 return;
10578 -- Otherwise an aggregate may match both the array type and the
10579 -- component type.
10581 else
10582 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10583 Set_Etype (Arg, Any_Type);
10584 end if;
10586 else
10587 if Is_Overloaded (Arg)
10588 and then Has_Compatible_Type (Arg, Typ)
10589 and then Etype (Arg) /= Any_Type
10590 then
10591 declare
10592 I : Interp_Index;
10593 It : Interp;
10594 Func : Entity_Id;
10596 begin
10597 Get_First_Interp (Arg, I, It);
10598 Func := It.Nam;
10599 Get_Next_Interp (I, It);
10601 -- Special-case the error message when the overloading is
10602 -- caused by a function that yields an array and can be
10603 -- called without parameters.
10605 if It.Nam = Func then
10606 Error_Msg_Sloc := Sloc (Func);
10607 Error_Msg_N ("ambiguous call to function#", Arg);
10608 Error_Msg_NE
10609 ("\\interpretation as call yields&", Arg, Typ);
10610 Error_Msg_NE
10611 ("\\interpretation as indexing of call yields&",
10612 Arg, Ctyp);
10614 else
10615 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10617 Get_First_Interp (Arg, I, It);
10618 while Present (It.Nam) loop
10619 Error_Msg_Sloc := Sloc (It.Nam);
10621 if Base_Type (It.Typ) = Btyp
10622 or else
10623 Base_Type (It.Typ) = Base_Type (Ctyp)
10624 then
10625 Error_Msg_N -- CODEFIX
10626 ("\\possible interpretation#", Arg);
10627 end if;
10629 Get_Next_Interp (I, It);
10630 end loop;
10631 end if;
10632 end;
10633 end if;
10635 Resolve (Arg, Ctyp);
10637 if Nkind (Arg) = N_String_Literal then
10638 Set_Etype (Arg, Ctyp);
10640 elsif Is_Scalar_Type (Etype (Arg))
10641 and then Compile_Time_Known_Value (Arg)
10642 then
10643 -- Determine if the out-of-range violation constitutes a
10644 -- warning or an error according to the expression base type,
10645 -- according to Ada 2022 RM 4.9 (35/2).
10647 if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then
10648 Apply_Compile_Time_Constraint_Error
10649 (Arg, "value not in range of}", CE_Range_Check_Failed,
10650 Ent => Base_Type (Ctyp),
10651 Typ => Base_Type (Ctyp));
10653 elsif Is_Out_Of_Range (Arg, Ctyp) then
10654 Apply_Compile_Time_Constraint_Error
10655 (Arg, "value not in range of}??", CE_Range_Check_Failed,
10656 Ent => Ctyp,
10657 Typ => Ctyp);
10658 end if;
10659 end if;
10661 if Arg = Left_Opnd (N) then
10662 Set_Is_Component_Left_Opnd (N);
10663 else
10664 Set_Is_Component_Right_Opnd (N);
10665 end if;
10666 end if;
10668 else
10669 Resolve (Arg, Btyp);
10670 end if;
10672 Check_Unset_Reference (Arg);
10673 end Resolve_Op_Concat_Arg;
10675 -----------------------------
10676 -- Resolve_Op_Concat_First --
10677 -----------------------------
10679 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10680 Btyp : constant Entity_Id := Base_Type (Typ);
10681 Op1 : constant Node_Id := Left_Opnd (N);
10682 Op2 : constant Node_Id := Right_Opnd (N);
10684 begin
10685 -- The parser folds an enormous sequence of concatenations of string
10686 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10687 -- in the right operand. If the expression resolves to a predefined "&"
10688 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10689 -- we give an error. See P_Simple_Expression in Par.Ch4.
10691 if Nkind (Op2) = N_String_Literal
10692 and then Is_Folded_In_Parser (Op2)
10693 and then Ekind (Entity (N)) = E_Function
10694 then
10695 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10696 and then String_Length (Strval (Op1)) = 0);
10697 Error_Msg_N ("too many user-defined concatenations", N);
10698 return;
10699 end if;
10701 Set_Etype (N, Btyp);
10703 if Is_Limited_Composite (Btyp) then
10704 Error_Msg_N ("concatenation not available for limited array", N);
10705 Explain_Limited_Type (Btyp, N);
10706 end if;
10707 end Resolve_Op_Concat_First;
10709 ----------------------------
10710 -- Resolve_Op_Concat_Rest --
10711 ----------------------------
10713 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10714 Op1 : constant Node_Id := Left_Opnd (N);
10715 Op2 : constant Node_Id := Right_Opnd (N);
10717 begin
10718 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10720 Generate_Operator_Reference (N, Typ);
10722 if Is_String_Type (Typ) then
10723 Eval_Concatenation (N);
10724 end if;
10726 -- If this is not a static concatenation, but the result is a string
10727 -- type (and not an array of strings) ensure that static string operands
10728 -- have their subtypes properly constructed.
10730 if Nkind (N) /= N_String_Literal
10731 and then Is_Character_Type (Component_Type (Typ))
10732 then
10733 Set_String_Literal_Subtype (Op1, Typ);
10734 Set_String_Literal_Subtype (Op2, Typ);
10735 end if;
10736 end Resolve_Op_Concat_Rest;
10738 ----------------------
10739 -- Resolve_Op_Expon --
10740 ----------------------
10742 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10743 B_Typ : constant Entity_Id := Base_Type (Typ);
10745 begin
10746 -- Catch attempts to do fixed-point exponentiation with universal
10747 -- operands, which is a case where the illegality is not caught during
10748 -- normal operator analysis. This is not done in preanalysis mode
10749 -- since the tree is not fully decorated during preanalysis.
10751 if Full_Analysis then
10752 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10753 Error_Msg_N ("exponentiation not available for fixed point", N);
10754 return;
10756 elsif Nkind (Parent (N)) in N_Op
10757 and then Present (Etype (Parent (N)))
10758 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10759 and then Etype (N) = Universal_Real
10760 and then Comes_From_Source (N)
10761 then
10762 Error_Msg_N ("exponentiation not available for fixed point", N);
10763 return;
10764 end if;
10765 end if;
10767 if Ekind (Entity (N)) = E_Function
10768 and then Is_Imported (Entity (N))
10769 and then Is_Intrinsic_Subprogram (Entity (N))
10770 then
10771 Generate_Reference (Entity (N), N);
10772 Resolve_Intrinsic_Operator (N, Typ);
10773 return;
10774 end if;
10776 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10777 Check_For_Visible_Operator (N, B_Typ);
10778 end if;
10780 -- We do the resolution using the base type, because intermediate values
10781 -- in expressions are always of the base type, not a subtype of it.
10783 Resolve (Left_Opnd (N), B_Typ);
10784 Resolve (Right_Opnd (N), Standard_Integer);
10786 -- For integer types, right argument must be in Natural range
10788 if Is_Integer_Type (Typ) then
10789 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10790 end if;
10792 Check_Unset_Reference (Left_Opnd (N));
10793 Check_Unset_Reference (Right_Opnd (N));
10795 Set_Etype (N, B_Typ);
10796 Generate_Operator_Reference (N, B_Typ);
10798 Analyze_Dimension (N);
10800 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10801 -- Evaluate the exponentiation operator for dimensioned type
10803 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10804 else
10805 Eval_Op_Expon (N);
10806 end if;
10808 -- Set overflow checking bit. Much cleverer code needed here eventually
10809 -- and perhaps the Resolve routines should be separated for the various
10810 -- arithmetic operations, since they will need different processing. ???
10812 if Nkind (N) in N_Op then
10813 if not Overflow_Checks_Suppressed (Etype (N)) then
10814 Enable_Overflow_Check (N);
10815 end if;
10816 end if;
10817 end Resolve_Op_Expon;
10819 --------------------
10820 -- Resolve_Op_Not --
10821 --------------------
10823 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10824 function Parent_Is_Boolean return Boolean;
10825 -- This function determines if the parent node is a boolean operator or
10826 -- operation (comparison op, membership test, or short circuit form) and
10827 -- the not in question is the left operand of this operation. Note that
10828 -- if the not is in parens, then false is returned.
10830 -----------------------
10831 -- Parent_Is_Boolean --
10832 -----------------------
10834 function Parent_Is_Boolean return Boolean is
10835 begin
10836 return Paren_Count (N) = 0
10837 and then Nkind (Parent (N)) in N_Membership_Test
10838 | N_Op_Boolean
10839 | N_Short_Circuit
10840 and then Left_Opnd (Parent (N)) = N;
10841 end Parent_Is_Boolean;
10843 -- Local variables
10845 B_Typ : Entity_Id;
10847 -- Start of processing for Resolve_Op_Not
10849 begin
10850 -- Predefined operations on scalar types yield the base type. On the
10851 -- other hand, logical operations on arrays yield the type of the
10852 -- arguments (and the context).
10854 if Is_Array_Type (Typ) then
10855 B_Typ := Typ;
10856 else
10857 B_Typ := Base_Type (Typ);
10858 end if;
10860 -- Straightforward case of incorrect arguments
10862 if not Valid_Boolean_Arg (Typ) then
10863 Error_Msg_N ("invalid operand type for operator&", N);
10864 Set_Etype (N, Any_Type);
10865 return;
10867 -- Special case of probable missing parens
10869 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10870 if Parent_Is_Boolean then
10871 Error_Msg_N
10872 ("operand of NOT must be enclosed in parentheses",
10873 Right_Opnd (N));
10874 else
10875 Error_Msg_N
10876 ("no modular type available in this context", N);
10877 end if;
10879 Set_Etype (N, Any_Type);
10880 return;
10882 -- OK resolution of NOT
10884 else
10885 -- Warn if non-boolean types involved. This is a case like not a < b
10886 -- where a and b are modular, where we will get (not a) < b and most
10887 -- likely not (a < b) was intended.
10889 if Warn_On_Questionable_Missing_Parens
10890 and then not Is_Boolean_Type (Typ)
10891 and then Parent_Is_Boolean
10892 then
10893 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10894 end if;
10896 -- Warn on double negation if checking redundant constructs
10898 if Warn_On_Redundant_Constructs
10899 and then Comes_From_Source (N)
10900 and then Comes_From_Source (Right_Opnd (N))
10901 and then Root_Type (Typ) = Standard_Boolean
10902 and then Nkind (Right_Opnd (N)) = N_Op_Not
10903 then
10904 Error_Msg_N ("redundant double negation?r?", N);
10905 end if;
10907 -- Complete resolution and evaluation of NOT
10909 Resolve (Right_Opnd (N), B_Typ);
10910 Check_Unset_Reference (Right_Opnd (N));
10911 Set_Etype (N, B_Typ);
10912 Generate_Operator_Reference (N, B_Typ);
10913 Eval_Op_Not (N);
10914 end if;
10915 end Resolve_Op_Not;
10917 -----------------------------
10918 -- Resolve_Operator_Symbol --
10919 -----------------------------
10921 -- Nothing to be done, all resolved already
10923 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10924 pragma Warnings (Off, N);
10925 pragma Warnings (Off, Typ);
10927 begin
10928 null;
10929 end Resolve_Operator_Symbol;
10931 ----------------------------------
10932 -- Resolve_Qualified_Expression --
10933 ----------------------------------
10935 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10936 pragma Warnings (Off, Typ);
10938 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10939 Expr : constant Node_Id := Expression (N);
10941 begin
10942 Resolve (Expr, Target_Typ);
10943 Check_Unset_Reference (Expr);
10945 -- A qualified expression requires an exact match of the type, class-
10946 -- wide matching is not allowed. However, if the qualifying type is
10947 -- specific and the expression has a class-wide type, it may still be
10948 -- okay, since it can be the result of the expansion of a call to a
10949 -- dispatching function, so we also have to check class-wideness of the
10950 -- type of the expression's original node.
10952 if (Is_Class_Wide_Type (Target_Typ)
10953 or else
10954 (Is_Class_Wide_Type (Etype (Expr))
10955 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10956 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10957 then
10958 Wrong_Type (Expr, Target_Typ);
10959 end if;
10961 -- If the target type is unconstrained, then we reset the type of the
10962 -- result from the type of the expression. For other cases, the actual
10963 -- subtype of the expression is the target type. But we avoid doing it
10964 -- for an allocator since this is not needed and might be problematic.
10966 if Is_Composite_Type (Target_Typ)
10967 and then not Is_Constrained (Target_Typ)
10968 and then Nkind (Parent (N)) /= N_Allocator
10969 then
10970 Set_Etype (N, Etype (Expr));
10971 end if;
10973 Analyze_Dimension (N);
10974 Eval_Qualified_Expression (N);
10976 -- If we still have a qualified expression after the static evaluation,
10977 -- then apply a scalar range check if needed. The reason that we do this
10978 -- after the Eval call is that otherwise, the application of the range
10979 -- check may convert an illegal static expression and result in warning
10980 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10982 if Nkind (N) = N_Qualified_Expression
10983 and then Is_Scalar_Type (Target_Typ)
10984 then
10985 Apply_Scalar_Range_Check (Expr, Target_Typ);
10986 end if;
10988 -- AI12-0100: Once the qualified expression is resolved, check whether
10989 -- operand satisfies a static predicate of the target subtype, if any.
10990 -- In the static expression case, a predicate check failure is an error.
10992 if Has_Predicates (Target_Typ) then
10993 Check_Expression_Against_Static_Predicate
10994 (Expr, Target_Typ, Static_Failure_Is_Error => True);
10995 end if;
10996 end Resolve_Qualified_Expression;
10998 ------------------------------
10999 -- Resolve_Raise_Expression --
11000 ------------------------------
11002 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
11003 begin
11004 if Typ = Raise_Type then
11005 Error_Msg_N ("cannot find unique type for raise expression", N);
11006 Set_Etype (N, Any_Type);
11008 else
11009 Set_Etype (N, Typ);
11011 -- Apply check for required parentheses in the enclosing
11012 -- context of raise_expressions (RM 11.3 (2)), including default
11013 -- expressions in contexts that can include aspect specifications,
11014 -- and ancestor parts of extension aggregates.
11016 declare
11017 Par : Node_Id := Parent (N);
11018 Parentheses_Found : Boolean := Paren_Count (N) > 0;
11020 begin
11021 while Present (Par)
11022 and then Nkind (Par) in N_Has_Etype
11023 loop
11024 if Paren_Count (Par) > 0 then
11025 Parentheses_Found := True;
11026 end if;
11028 if Nkind (Par) = N_Extension_Aggregate
11029 and then N = Ancestor_Part (Par)
11030 then
11031 exit;
11032 end if;
11034 Par := Parent (Par);
11035 end loop;
11037 if not Parentheses_Found
11038 and then Comes_From_Source (Par)
11039 and then
11040 (Nkind (Par) in N_Modular_Type_Definition
11041 | N_Floating_Point_Definition
11042 | N_Ordinary_Fixed_Point_Definition
11043 | N_Decimal_Fixed_Point_Definition
11044 | N_Extension_Aggregate
11045 | N_Discriminant_Specification
11046 | N_Parameter_Specification
11047 | N_Formal_Object_Declaration
11049 or else (Nkind (Par) = N_Object_Declaration
11050 and then
11051 Nkind (Parent (Par)) /= N_Extended_Return_Statement))
11052 then
11053 Error_Msg_N
11054 ("raise_expression must be parenthesized in this context",
11056 end if;
11057 end;
11058 end if;
11059 end Resolve_Raise_Expression;
11061 -------------------
11062 -- Resolve_Range --
11063 -------------------
11065 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
11066 L : constant Node_Id := Low_Bound (N);
11067 H : constant Node_Id := High_Bound (N);
11069 function First_Last_Ref return Boolean;
11070 -- Returns True if N is of the form X'First .. X'Last where X is the
11071 -- same entity for both attributes.
11073 --------------------
11074 -- First_Last_Ref --
11075 --------------------
11077 function First_Last_Ref return Boolean is
11078 Lorig : constant Node_Id := Original_Node (L);
11079 Horig : constant Node_Id := Original_Node (H);
11081 begin
11082 if Nkind (Lorig) = N_Attribute_Reference
11083 and then Nkind (Horig) = N_Attribute_Reference
11084 and then Attribute_Name (Lorig) = Name_First
11085 and then Attribute_Name (Horig) = Name_Last
11086 then
11087 declare
11088 PL : constant Node_Id := Prefix (Lorig);
11089 PH : constant Node_Id := Prefix (Horig);
11090 begin
11091 return Is_Entity_Name (PL)
11092 and then Is_Entity_Name (PH)
11093 and then Entity (PL) = Entity (PH);
11094 end;
11095 end if;
11097 return False;
11098 end First_Last_Ref;
11100 -- Start of processing for Resolve_Range
11102 begin
11103 Set_Etype (N, Typ);
11105 Resolve (L, Typ);
11106 Resolve (H, Typ);
11108 -- Reanalyze the lower bound after both bounds have been analyzed, so
11109 -- that the range is known to be static or not by now. This may trigger
11110 -- more compile-time evaluation, which is useful for static analysis
11111 -- with GNATprove. This is not needed for compilation or static analysis
11112 -- with CodePeer, as full expansion does that evaluation then.
11114 if GNATprove_Mode then
11115 Set_Analyzed (L, False);
11116 Resolve (L, Typ);
11117 end if;
11119 -- Check for inappropriate range on unordered enumeration type
11121 if Bad_Unordered_Enumeration_Reference (N, Typ)
11123 -- Exclude X'First .. X'Last if X is the same entity for both
11125 and then not First_Last_Ref
11126 then
11127 Error_Msg_Sloc := Sloc (Typ);
11128 Error_Msg_NE
11129 ("subrange of unordered enumeration type& declared#?.u?", N, Typ);
11130 end if;
11132 Check_Unset_Reference (L);
11133 Check_Unset_Reference (H);
11135 -- We have to check the bounds for being within the base range as
11136 -- required for a non-static context. Normally this is automatic and
11137 -- done as part of evaluating expressions, but the N_Range node is an
11138 -- exception, since in GNAT we consider this node to be a subexpression,
11139 -- even though in Ada it is not. The circuit in Sem_Eval could check for
11140 -- this, but that would put the test on the main evaluation path for
11141 -- expressions.
11143 Check_Non_Static_Context (L);
11144 Check_Non_Static_Context (H);
11146 -- Check for an ambiguous range over character literals. This will
11147 -- happen with a membership test involving only literals.
11149 if Typ = Any_Character then
11150 Ambiguous_Character (L);
11151 Set_Etype (N, Any_Type);
11152 return;
11153 end if;
11155 -- If bounds are static, constant-fold them, so size computations are
11156 -- identical between front-end and back-end. Do not perform this
11157 -- transformation while analyzing generic units, as type information
11158 -- would be lost when reanalyzing the constant node in the instance.
11160 if Is_Discrete_Type (Typ) and then Expander_Active then
11161 if Is_OK_Static_Expression (L) then
11162 Fold_Uint (L, Expr_Value (L), Static => True);
11163 end if;
11165 if Is_OK_Static_Expression (H) then
11166 Fold_Uint (H, Expr_Value (H), Static => True);
11167 end if;
11168 end if;
11170 -- If we have a compile-time-known null range, we warn, because that is
11171 -- likely to be a mistake. (Dynamic null ranges make sense, but often
11172 -- compile-time-known ones do not.) Warn only if this is in a subtype
11173 -- declaration. We do this here, rather than while analyzing a subtype
11174 -- declaration, in case we decide to expand the cases. We do not want to
11175 -- warn in all cases, because some are idiomatic, such as an empty
11176 -- aggregate (1 .. 0 => <>).
11178 -- We don't warn in generics or their instances, because there might be
11179 -- some instances where the range is null, and some where it is not,
11180 -- which would lead to false alarms.
11182 if not (Inside_A_Generic or In_Instance)
11183 and then Comes_From_Source (N)
11184 and then Compile_Time_Compare
11185 (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT
11186 and then Nkind (Parent (N)) = N_Range_Constraint
11187 and then Nkind (Parent (Parent (N))) = N_Subtype_Indication
11188 and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration
11189 and then Is_OK_Static_Range (N)
11190 then
11191 Error_Msg_N ("null range??", N);
11192 end if;
11193 end Resolve_Range;
11195 --------------------------
11196 -- Resolve_Real_Literal --
11197 --------------------------
11199 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
11200 Actual_Typ : constant Entity_Id := Etype (N);
11202 begin
11203 -- Special processing for fixed-point literals to make sure that the
11204 -- value is an exact multiple of the small where this is required. We
11205 -- skip this for the universal real case, and also for generic types.
11207 if Is_Fixed_Point_Type (Typ)
11208 and then Typ /= Universal_Fixed
11209 and then Typ /= Any_Fixed
11210 and then not Is_Generic_Type (Typ)
11211 then
11212 -- We must freeze the base type to get the proper value of the small
11214 if not Is_Frozen (Base_Type (Typ)) then
11215 Freeze_Fixed_Point_Type (Base_Type (Typ));
11216 end if;
11218 declare
11219 Val : constant Ureal := Realval (N);
11220 Cintr : constant Ureal := Val / Small_Value (Base_Type (Typ));
11221 Cint : constant Uint := UR_Trunc (Cintr);
11222 Den : constant Uint := Norm_Den (Cintr);
11223 Stat : Boolean;
11225 begin
11226 -- Case of literal is not an exact multiple of the Small
11228 if Den /= 1 then
11230 -- For a source program literal for a decimal fixed-point type,
11231 -- this is statically illegal (RM 4.9(36)).
11233 if Is_Decimal_Fixed_Point_Type (Typ)
11234 and then Actual_Typ = Universal_Real
11235 and then Comes_From_Source (N)
11236 then
11237 Error_Msg_N ("value has extraneous low order digits", N);
11238 end if;
11240 -- Generate a warning if literal from source
11242 if Is_OK_Static_Expression (N)
11243 and then Warn_On_Bad_Fixed_Value
11244 then
11245 Error_Msg_N
11246 ("?b?static fixed-point value is not a multiple of Small!",
11248 end if;
11250 -- Replace literal by a value that is the exact representation
11251 -- of a value of the type, i.e. a multiple of the small value,
11252 -- by truncation, since Machine_Rounds is false for all GNAT
11253 -- fixed-point types (RM 4.9(38)).
11255 Stat := Is_OK_Static_Expression (N);
11256 Rewrite (N,
11257 Make_Real_Literal (Sloc (N),
11258 Realval => Small_Value (Typ) * Cint));
11260 Set_Is_Static_Expression (N, Stat);
11261 end if;
11263 -- In all cases, set the corresponding integer field
11265 Set_Corresponding_Integer_Value (N, Cint);
11266 end;
11267 end if;
11269 -- Now replace the actual type by the expected type as usual
11271 Set_Etype (N, Typ);
11272 Eval_Real_Literal (N);
11273 end Resolve_Real_Literal;
11275 -----------------------
11276 -- Resolve_Reference --
11277 -----------------------
11279 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
11280 P : constant Node_Id := Prefix (N);
11282 begin
11283 -- Replace general access with specific type
11285 if Ekind (Etype (N)) = E_Allocator_Type then
11286 Set_Etype (N, Base_Type (Typ));
11287 end if;
11289 Resolve (P, Designated_Type (Etype (N)));
11291 -- If we are taking the reference of a volatile entity, then treat it as
11292 -- a potential modification of this entity. This is too conservative,
11293 -- but necessary because remove side effects can cause transformations
11294 -- of normal assignments into reference sequences that otherwise fail to
11295 -- notice the modification.
11297 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
11298 Note_Possible_Modification (P, Sure => False);
11299 end if;
11300 end Resolve_Reference;
11302 --------------------------------
11303 -- Resolve_Selected_Component --
11304 --------------------------------
11306 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
11307 Comp : Entity_Id;
11308 Comp1 : Entity_Id := Empty; -- prevent junk warning
11309 P : constant Node_Id := Prefix (N);
11310 S : constant Node_Id := Selector_Name (N);
11311 T : Entity_Id := Etype (P);
11312 I : Interp_Index;
11313 I1 : Interp_Index := 0; -- prevent junk warning
11314 It : Interp;
11315 It1 : Interp;
11316 Found : Boolean;
11318 function Init_Component return Boolean;
11319 -- Check whether this is the initialization of a component within an
11320 -- init proc (by assignment or call to another init proc). If true,
11321 -- there is no need for a discriminant check.
11323 --------------------
11324 -- Init_Component --
11325 --------------------
11327 function Init_Component return Boolean is
11328 begin
11329 return Inside_Init_Proc
11330 and then Nkind (Prefix (N)) = N_Identifier
11331 and then Chars (Prefix (N)) = Name_uInit
11332 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
11333 end Init_Component;
11335 -- Start of processing for Resolve_Selected_Component
11337 begin
11338 if Is_Overloaded (P) then
11340 -- Use the context type to select the prefix that has a selector
11341 -- of the correct name and type.
11343 Found := False;
11344 Get_First_Interp (P, I, It);
11346 Search : while Present (It.Typ) loop
11347 if Is_Access_Type (It.Typ) then
11348 T := Designated_Type (It.Typ);
11349 else
11350 T := It.Typ;
11351 end if;
11353 -- Locate selected component. For a private prefix the selector
11354 -- can denote a discriminant.
11356 if Is_Record_Type (T) or else Is_Private_Type (T) then
11358 -- The visible components of a class-wide type are those of
11359 -- the root type.
11361 if Is_Class_Wide_Type (T) then
11362 T := Etype (T);
11363 end if;
11365 Comp := First_Entity (T);
11366 while Present (Comp) loop
11367 if Chars (Comp) = Chars (S)
11368 and then Covers (Typ, Etype (Comp))
11369 then
11370 if not Found then
11371 Found := True;
11372 I1 := I;
11373 It1 := It;
11374 Comp1 := Comp;
11376 else
11377 It := Disambiguate (P, I1, I, Any_Type);
11379 if It = No_Interp then
11380 Error_Msg_N
11381 ("ambiguous prefix for selected component", N);
11382 Set_Etype (N, Typ);
11383 return;
11385 else
11386 It1 := It;
11388 -- There may be an implicit dereference. Retrieve
11389 -- designated record type.
11391 if Is_Access_Type (It1.Typ) then
11392 T := Designated_Type (It1.Typ);
11393 else
11394 T := It1.Typ;
11395 end if;
11397 if Scope (Comp1) /= T then
11399 -- Resolution chooses the new interpretation.
11400 -- Find the component with the right name.
11402 Comp1 := First_Entity (T);
11403 while Present (Comp1)
11404 and then Chars (Comp1) /= Chars (S)
11405 loop
11406 Next_Entity (Comp1);
11407 end loop;
11408 end if;
11410 exit Search;
11411 end if;
11412 end if;
11413 end if;
11415 Next_Entity (Comp);
11416 end loop;
11417 end if;
11419 Get_Next_Interp (I, It);
11420 end loop Search;
11422 -- There must be a legal interpretation at this point
11424 pragma Assert (Found);
11425 Resolve (P, It1.Typ);
11427 -- In general the expected type is the type of the context, not the
11428 -- type of the candidate selected component.
11430 Set_Etype (N, Typ);
11431 Set_Entity_With_Checks (S, Comp1);
11433 -- The type of the context and that of the component are
11434 -- compatible and in general identical, but if they are anonymous
11435 -- access-to-subprogram types, the relevant type is that of the
11436 -- component. This matters in Unnest_Subprograms mode, where the
11437 -- relevant context is the one in which the type is declared, not
11438 -- the point of use. This determines what activation record to use.
11440 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
11441 Set_Etype (N, Etype (Comp1));
11443 -- When the type of the component is an access to a class-wide type
11444 -- the relevant type is that of the component (since in such case we
11445 -- may need to generate implicit type conversions or dispatching
11446 -- calls).
11448 elsif Is_Access_Type (Typ)
11449 and then not Is_Class_Wide_Type (Designated_Type (Typ))
11450 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
11451 then
11452 Set_Etype (N, Etype (Comp1));
11453 end if;
11455 else
11456 -- Resolve prefix with its type
11458 Resolve (P, T);
11459 end if;
11461 -- Generate cross-reference. We needed to wait until full overloading
11462 -- resolution was complete to do this, since otherwise we can't tell if
11463 -- we are an lvalue or not.
11465 if Known_To_Be_Assigned (N) then
11466 Generate_Reference (Entity (S), S, 'm');
11467 else
11468 Generate_Reference (Entity (S), S, 'r');
11469 end if;
11471 -- If the prefix's type is an access type, get to the real record type.
11472 -- Note: we do not apply an access check because an explicit dereference
11473 -- will be introduced later, and the check will happen there.
11475 if Is_Access_Type (Etype (P)) then
11476 T := Implicitly_Designated_Type (Etype (P));
11477 Check_Fully_Declared_Prefix (T, P);
11479 else
11480 T := Etype (P);
11481 end if;
11483 -- Set flag for expander if discriminant check required on a component
11484 -- appearing within a variant.
11486 if Has_Discriminants (T)
11487 and then Ekind (Entity (S)) = E_Component
11488 and then Present (Original_Record_Component (Entity (S)))
11489 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
11490 and then
11491 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
11492 and then not Discriminant_Checks_Suppressed (T)
11493 and then not Init_Component
11494 then
11495 Set_Do_Discriminant_Check (N);
11496 end if;
11498 if Ekind (Entity (S)) = E_Void then
11499 Error_Msg_N ("premature use of component", S);
11500 end if;
11502 -- If the prefix is a record conversion, this may be a renamed
11503 -- discriminant whose bounds differ from those of the original
11504 -- one, so we must ensure that a range check is performed.
11506 if Nkind (P) = N_Type_Conversion
11507 and then Ekind (Entity (S)) = E_Discriminant
11508 and then Is_Discrete_Type (Typ)
11509 then
11510 Set_Etype (N, Base_Type (Typ));
11511 end if;
11513 -- Eval_Selected_Component may e.g. fold statically known discriminants.
11515 Eval_Selected_Component (N);
11517 if Nkind (N) = N_Selected_Component then
11519 -- If the record type is atomic and the component is not, then this
11520 -- is worth a warning before Ada 2022, since we have a situation
11521 -- where the access to the component may cause extra read/writes of
11522 -- the atomic object, or partial word accesses, both of which may be
11523 -- unexpected.
11525 if Is_Atomic_Ref_With_Address (N)
11526 and then not Is_Atomic (Entity (S))
11527 and then not Is_Atomic (Etype (Entity (S)))
11528 and then Ada_Version < Ada_2022
11529 then
11530 Error_Msg_N
11531 ("??access to non-atomic component of atomic record",
11532 Prefix (N));
11533 Error_Msg_N
11534 ("\??may cause unexpected accesses to atomic object",
11535 Prefix (N));
11536 end if;
11538 Resolve_Implicit_Dereference (Prefix (N));
11539 Analyze_Dimension (N);
11540 end if;
11541 end Resolve_Selected_Component;
11543 -------------------
11544 -- Resolve_Shift --
11545 -------------------
11547 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11548 B_Typ : constant Entity_Id := Base_Type (Typ);
11549 L : constant Node_Id := Left_Opnd (N);
11550 R : constant Node_Id := Right_Opnd (N);
11552 begin
11553 -- We do the resolution using the base type, because intermediate values
11554 -- in expressions always are of the base type, not a subtype of it.
11556 Resolve (L, B_Typ);
11557 Resolve (R, Standard_Natural);
11559 Check_Unset_Reference (L);
11560 Check_Unset_Reference (R);
11562 Set_Etype (N, B_Typ);
11563 Generate_Operator_Reference (N, B_Typ);
11564 Eval_Shift (N);
11565 end Resolve_Shift;
11567 ---------------------------
11568 -- Resolve_Short_Circuit --
11569 ---------------------------
11571 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11572 B_Typ : constant Entity_Id := Base_Type (Typ);
11573 L : constant Node_Id := Left_Opnd (N);
11574 R : constant Node_Id := Right_Opnd (N);
11576 begin
11577 -- Ensure all actions associated with the left operand (e.g.
11578 -- finalization of transient objects) are fully evaluated locally within
11579 -- an expression with actions. This is particularly helpful for coverage
11580 -- analysis. However this should not happen in generics or if option
11581 -- Minimize_Expression_With_Actions is set.
11583 if Expander_Active and not Minimize_Expression_With_Actions then
11584 declare
11585 Reloc_L : constant Node_Id := Relocate_Node (L);
11586 begin
11587 Save_Interps (Old_N => L, New_N => Reloc_L);
11589 Rewrite (L,
11590 Make_Expression_With_Actions (Sloc (L),
11591 Actions => New_List,
11592 Expression => Reloc_L));
11594 -- Set Comes_From_Source on L to preserve warnings for unset
11595 -- reference.
11597 Preserve_Comes_From_Source (L, Reloc_L);
11598 end;
11599 end if;
11601 Resolve (L, B_Typ);
11602 Resolve (R, B_Typ);
11604 -- Check for issuing warning for always False assert/check, this happens
11605 -- when assertions are turned off, in which case the pragma Assert/Check
11606 -- was transformed into:
11608 -- if False and then <condition> then ...
11610 -- and we detect this pattern
11612 if Warn_On_Assertion_Failure
11613 and then Is_Entity_Name (R)
11614 and then Entity (R) = Standard_False
11615 and then Nkind (Parent (N)) = N_If_Statement
11616 and then Nkind (N) = N_And_Then
11617 and then Is_Entity_Name (L)
11618 and then Entity (L) = Standard_False
11619 then
11620 declare
11621 Orig : constant Node_Id := Original_Node (Parent (N));
11623 begin
11624 -- Special handling of Asssert pragma
11626 if Nkind (Orig) = N_Pragma
11627 and then Pragma_Name (Orig) = Name_Assert
11628 then
11629 declare
11630 Expr : constant Node_Id :=
11631 Original_Node
11632 (Expression
11633 (First (Pragma_Argument_Associations (Orig))));
11635 begin
11636 -- Don't warn if original condition is explicit False,
11637 -- since obviously the failure is expected in this case.
11639 if Is_Entity_Name (Expr)
11640 and then Entity (Expr) = Standard_False
11641 then
11642 null;
11644 -- Issue warning. We do not want the deletion of the
11645 -- IF/AND-THEN to take this message with it. We achieve this
11646 -- by making sure that the expanded code points to the Sloc
11647 -- of the expression, not the original pragma.
11649 else
11650 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11651 -- The source location of the expression is not usually
11652 -- the best choice here. For example, it gets located on
11653 -- the last AND keyword in a chain of boolean expressiond
11654 -- AND'ed together. It is best to put the message on the
11655 -- first character of the assertion, which is the effect
11656 -- of the First_Node call here.
11658 Error_Msg_F
11659 ("?.a?assertion would fail at run time!",
11660 Expression
11661 (First (Pragma_Argument_Associations (Orig))));
11662 end if;
11663 end;
11665 -- Similar processing for Check pragma
11667 elsif Nkind (Orig) = N_Pragma
11668 and then Pragma_Name (Orig) = Name_Check
11669 then
11670 -- Don't want to warn if original condition is explicit False
11672 declare
11673 Expr : constant Node_Id :=
11674 Original_Node
11675 (Expression
11676 (Next (First (Pragma_Argument_Associations (Orig)))));
11677 begin
11678 if Is_Entity_Name (Expr)
11679 and then Entity (Expr) = Standard_False
11680 then
11681 null;
11683 -- Post warning
11685 else
11686 -- Again use Error_Msg_F rather than Error_Msg_N, see
11687 -- comment above for an explanation of why we do this.
11689 Error_Msg_F
11690 ("?.a?check would fail at run time!",
11691 Expression
11692 (Last (Pragma_Argument_Associations (Orig))));
11693 end if;
11694 end;
11695 end if;
11696 end;
11697 end if;
11699 -- Continue with processing of short circuit
11701 Check_Unset_Reference (L);
11702 Check_Unset_Reference (R);
11704 Set_Etype (N, B_Typ);
11705 Eval_Short_Circuit (N);
11706 end Resolve_Short_Circuit;
11708 -------------------
11709 -- Resolve_Slice --
11710 -------------------
11712 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11713 Drange : constant Node_Id := Discrete_Range (N);
11714 Pref : constant Node_Id := Prefix (N);
11715 Array_Type : Entity_Id := Empty;
11716 Dexpr : Node_Id := Empty;
11717 Index_Type : Entity_Id;
11719 begin
11720 if Is_Overloaded (Pref) then
11722 -- Use the context type to select the prefix that yields the correct
11723 -- array type.
11725 declare
11726 I : Interp_Index;
11727 I1 : Interp_Index := 0;
11728 It : Interp;
11729 Found : Boolean := False;
11731 begin
11732 Get_First_Interp (Pref, I, It);
11733 while Present (It.Typ) loop
11734 if (Is_Array_Type (It.Typ)
11735 and then Covers (Typ, It.Typ))
11736 or else (Is_Access_Type (It.Typ)
11737 and then Is_Array_Type (Designated_Type (It.Typ))
11738 and then Covers (Typ, Designated_Type (It.Typ)))
11739 then
11740 if Found then
11741 It := Disambiguate (Pref, I1, I, Any_Type);
11743 if It = No_Interp then
11744 Error_Msg_N ("ambiguous prefix for slicing", N);
11745 Set_Etype (N, Typ);
11746 return;
11747 else
11748 Found := True;
11749 Array_Type := It.Typ;
11750 I1 := I;
11751 end if;
11752 else
11753 Found := True;
11754 Array_Type := It.Typ;
11755 I1 := I;
11756 end if;
11757 end if;
11759 Get_Next_Interp (I, It);
11760 end loop;
11761 end;
11763 else
11764 Array_Type := Etype (Pref);
11765 end if;
11767 Resolve (Pref, Array_Type);
11769 -- If the prefix's type is an access type, get to the real array type.
11770 -- Note: we do not apply an access check because an explicit dereference
11771 -- will be introduced later, and the check will happen there.
11773 if Is_Access_Type (Array_Type) then
11774 Array_Type := Implicitly_Designated_Type (Array_Type);
11776 -- If the prefix is an access to an unconstrained array, we must use
11777 -- the actual subtype of the object to perform the index checks. The
11778 -- object denoted by the prefix is implicit in the node, so we build
11779 -- an explicit representation for it in order to compute the actual
11780 -- subtype.
11782 if not Is_Constrained (Array_Type) then
11783 Remove_Side_Effects (Pref);
11785 declare
11786 Obj : constant Node_Id :=
11787 Make_Explicit_Dereference (Sloc (N),
11788 Prefix => New_Copy_Tree (Pref));
11789 begin
11790 Set_Etype (Obj, Array_Type);
11791 Set_Parent (Obj, Parent (N));
11792 Array_Type := Get_Actual_Subtype (Obj);
11793 end;
11794 end if;
11796 -- In CodePeer mode the attribute Image is not expanded, so when it
11797 -- acts as a prefix of a slice, we handle it like a call to function
11798 -- returning an unconstrained string. Same for the Wide variants of
11799 -- attribute Image.
11801 elsif Is_Entity_Name (Pref)
11802 or else Nkind (Pref) = N_Explicit_Dereference
11803 or else (Nkind (Pref) = N_Function_Call
11804 and then not Is_Constrained (Etype (Pref)))
11805 or else (CodePeer_Mode
11806 and then Nkind (Pref) = N_Attribute_Reference
11807 and then Attribute_Name (Pref) in Name_Image
11808 | Name_Wide_Image
11809 | Name_Wide_Wide_Image)
11810 then
11811 Array_Type := Get_Actual_Subtype (Pref);
11813 -- If the name is a selected component that depends on discriminants,
11814 -- build an actual subtype for it. This can happen only when the name
11815 -- itself is overloaded; otherwise the actual subtype is created when
11816 -- the selected component is analyzed.
11818 elsif Nkind (Pref) = N_Selected_Component
11819 and then Full_Analysis
11820 and then Depends_On_Discriminant (First_Index (Array_Type))
11821 then
11822 declare
11823 Act_Decl : constant Node_Id :=
11824 Build_Actual_Subtype_Of_Component (Array_Type, Pref);
11825 begin
11826 Insert_Action (N, Act_Decl);
11827 Array_Type := Defining_Identifier (Act_Decl);
11828 end;
11830 -- Maybe this should just be "else", instead of checking for the
11831 -- specific case of slice??? This is needed for the case where the
11832 -- prefix is an Image attribute, which gets expanded to a slice, and so
11833 -- has a constrained subtype which we want to use for the slice range
11834 -- check applied below (the range check won't get done if the
11835 -- unconstrained subtype of the 'Image is used).
11837 elsif Nkind (Pref) = N_Slice then
11838 Array_Type := Etype (Pref);
11839 end if;
11841 -- Obtain the type of the array index
11843 if Ekind (Array_Type) = E_String_Literal_Subtype then
11844 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11845 else
11846 Index_Type := Etype (First_Index (Array_Type));
11847 end if;
11849 -- If name was overloaded, set slice type correctly now
11851 Set_Etype (N, Array_Type);
11853 -- Handle the generation of a range check that compares the array index
11854 -- against the discrete_range. The check is not applied to internally
11855 -- built nodes associated with the expansion of dispatch tables. Check
11856 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11857 -- the unit.
11859 if Tagged_Type_Expansion
11860 and then RTU_Loaded (Ada_Tags)
11861 and then Nkind (Pref) = N_Selected_Component
11862 and then Present (Entity (Selector_Name (Pref)))
11863 and then Entity (Selector_Name (Pref)) =
11864 RTE_Record_Component (RE_Prims_Ptr)
11865 then
11866 null;
11868 -- The discrete_range is specified by a subtype name. Create an
11869 -- equivalent range attribute, apply checks to this attribute, but
11870 -- insert them into the range expression of the slice itself.
11872 elsif Is_Entity_Name (Drange) then
11873 Dexpr :=
11874 Make_Attribute_Reference
11875 (Sloc (Drange),
11876 Prefix =>
11877 New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
11878 Attribute_Name => Name_Range);
11880 Analyze_And_Resolve (Dexpr, Etype (Drange));
11882 elsif Nkind (Drange) = N_Subtype_Indication then
11883 Dexpr := Range_Expression (Constraint (Drange));
11885 -- The discrete_range is a regular range (or a range attribute, which
11886 -- will be resolved into a regular range). Resolve the bounds and remove
11887 -- their side effects.
11889 else
11890 Resolve (Drange, Base_Type (Index_Type));
11892 if Nkind (Drange) = N_Range then
11893 Force_Evaluation (Low_Bound (Drange));
11894 Force_Evaluation (High_Bound (Drange));
11896 Dexpr := Drange;
11897 end if;
11898 end if;
11900 if Present (Dexpr) then
11901 Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
11902 end if;
11904 Set_Slice_Subtype (N);
11906 -- Check bad use of type with predicates
11908 declare
11909 Subt : Entity_Id;
11911 begin
11912 if Nkind (Drange) = N_Subtype_Indication
11913 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11914 then
11915 Subt := Entity (Subtype_Mark (Drange));
11916 else
11917 Subt := Etype (Drange);
11918 end if;
11920 if Has_Predicates (Subt) then
11921 Bad_Predicated_Subtype_Use
11922 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11923 end if;
11924 end;
11926 -- Otherwise here is where we check suspicious indexes
11928 if Nkind (Drange) = N_Range then
11929 Warn_On_Suspicious_Index (Pref, Low_Bound (Drange));
11930 Warn_On_Suspicious_Index (Pref, High_Bound (Drange));
11931 end if;
11933 Resolve_Implicit_Dereference (Pref);
11934 Analyze_Dimension (N);
11935 Eval_Slice (N);
11936 end Resolve_Slice;
11938 ----------------------------
11939 -- Resolve_String_Literal --
11940 ----------------------------
11942 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11943 C_Typ : constant Entity_Id := Component_Type (Typ);
11944 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11945 Loc : constant Source_Ptr := Sloc (N);
11946 Str : constant String_Id := Strval (N);
11947 Strlen : constant Nat := String_Length (Str);
11948 Subtype_Id : Entity_Id;
11949 Need_Check : Boolean;
11951 begin
11952 -- For a string appearing in a concatenation, defer creation of the
11953 -- string_literal_subtype until the end of the resolution of the
11954 -- concatenation, because the literal may be constant-folded away. This
11955 -- is a useful optimization for long concatenation expressions.
11957 -- If the string is an aggregate built for a single character (which
11958 -- happens in a non-static context) or a is null string to which special
11959 -- checks may apply, we build the subtype. Wide strings must also get a
11960 -- string subtype if they come from a one character aggregate. Strings
11961 -- generated by attributes might be static, but it is often hard to
11962 -- determine whether the enclosing context is static, so we generate
11963 -- subtypes for them as well, thus losing some rarer optimizations ???
11964 -- Same for strings that come from a static conversion.
11966 Need_Check :=
11967 (Strlen = 0 and then Typ /= Standard_String)
11968 or else Nkind (Parent (N)) /= N_Op_Concat
11969 or else (N /= Left_Opnd (Parent (N))
11970 and then N /= Right_Opnd (Parent (N)))
11971 or else ((Typ = Standard_Wide_String
11972 or else Typ = Standard_Wide_Wide_String)
11973 and then Nkind (Original_Node (N)) /= N_String_Literal);
11975 -- If the resolving type is itself a string literal subtype, we can just
11976 -- reuse it, since there is no point in creating another.
11978 if Ekind (Typ) = E_String_Literal_Subtype then
11979 Subtype_Id := Typ;
11981 elsif Nkind (Parent (N)) = N_Op_Concat
11982 and then not Need_Check
11983 and then Nkind (Original_Node (N)) not in N_Character_Literal
11984 | N_Attribute_Reference
11985 | N_Qualified_Expression
11986 | N_Type_Conversion
11987 then
11988 Subtype_Id := Typ;
11990 -- Do not generate a string literal subtype for the default expression
11991 -- of a formal parameter in GNATprove mode. This is because the string
11992 -- subtype is associated with the freezing actions of the subprogram,
11993 -- however freezing is disabled in GNATprove mode and as a result the
11994 -- subtype is unavailable.
11996 elsif GNATprove_Mode
11997 and then Nkind (Parent (N)) = N_Parameter_Specification
11998 then
11999 Subtype_Id := Typ;
12001 -- Otherwise we must create a string literal subtype. Note that the
12002 -- whole idea of string literal subtypes is simply to avoid the need
12003 -- for building a full fledged array subtype for each literal.
12005 else
12006 Set_String_Literal_Subtype (N, Typ);
12007 Subtype_Id := Etype (N);
12008 end if;
12010 if Nkind (Parent (N)) /= N_Op_Concat
12011 or else Need_Check
12012 then
12013 Set_Etype (N, Subtype_Id);
12014 Eval_String_Literal (N);
12015 end if;
12017 if Is_Limited_Composite (Typ)
12018 or else Is_Private_Composite (Typ)
12019 then
12020 Error_Msg_N ("string literal not available for private array", N);
12021 Set_Etype (N, Any_Type);
12022 return;
12023 end if;
12025 -- The validity of a null string has been checked in the call to
12026 -- Eval_String_Literal.
12028 if Strlen = 0 then
12029 return;
12031 -- Always accept string literal with component type Any_Character, which
12032 -- occurs in error situations and in comparisons of literals, both of
12033 -- which should accept all literals.
12035 elsif R_Typ = Any_Character then
12036 return;
12038 -- If the type is bit-packed, then we always transform the string
12039 -- literal into a full fledged aggregate.
12041 elsif Is_Bit_Packed_Array (Typ) then
12042 null;
12044 -- Deal with cases of Wide_Wide_String, Wide_String, and String
12046 else
12047 -- For Standard.Wide_Wide_String, or any other type whose component
12048 -- type is Standard.Wide_Wide_Character, we know that all the
12049 -- characters in the string must be acceptable, since the parser
12050 -- accepted the characters as valid character literals.
12052 if R_Typ = Standard_Wide_Wide_Character then
12053 null;
12055 -- For the case of Standard.String, or any other type whose component
12056 -- type is Standard.Character, we must make sure that there are no
12057 -- wide characters in the string, i.e. that it is entirely composed
12058 -- of characters in range of type Character.
12060 -- If the string literal is the result of a static concatenation, the
12061 -- test has already been performed on the components, and need not be
12062 -- repeated.
12064 elsif R_Typ = Standard_Character
12065 and then Nkind (Original_Node (N)) /= N_Op_Concat
12066 then
12067 for J in 1 .. Strlen loop
12068 if not In_Character_Range (Get_String_Char (Str, J)) then
12070 -- If we are out of range, post error. This is one of the
12071 -- very few places that we place the flag in the middle of
12072 -- a token, right under the offending wide character. Not
12073 -- quite clear if this is right wrt wide character encoding
12074 -- sequences, but it's only an error message.
12076 Error_Msg
12077 ("literal out of range of type Standard.Character",
12078 Loc + Source_Ptr (J));
12079 return;
12080 end if;
12081 end loop;
12083 -- For the case of Standard.Wide_String, or any other type whose
12084 -- component type is Standard.Wide_Character, we must make sure that
12085 -- there are no wide characters in the string, i.e. that it is
12086 -- entirely composed of characters in range of type Wide_Character.
12088 -- If the string literal is the result of a static concatenation,
12089 -- the test has already been performed on the components, and need
12090 -- not be repeated.
12092 elsif R_Typ = Standard_Wide_Character
12093 and then Nkind (Original_Node (N)) /= N_Op_Concat
12094 then
12095 for J in 1 .. Strlen loop
12096 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
12098 -- If we are out of range, post error. This is one of the
12099 -- very few places that we place the flag in the middle of
12100 -- a token, right under the offending wide character.
12102 -- This is not quite right, because characters in general
12103 -- will take more than one character position ???
12105 Error_Msg
12106 ("literal out of range of type Standard.Wide_Character",
12107 Loc + Source_Ptr (J));
12108 return;
12109 end if;
12110 end loop;
12112 -- If the root type is not a standard character, then we will convert
12113 -- the string into an aggregate and will let the aggregate code do
12114 -- the checking. Standard Wide_Wide_Character is also OK here.
12116 else
12117 null;
12118 end if;
12120 -- See if the component type of the array corresponding to the string
12121 -- has compile time known bounds. If yes we can directly check
12122 -- whether the evaluation of the string will raise constraint error.
12123 -- Otherwise we need to transform the string literal into the
12124 -- corresponding character aggregate and let the aggregate code do
12125 -- the checking. We use the same transformation if the component
12126 -- type has a static predicate, which will be applied to each
12127 -- character when the aggregate is resolved.
12129 if Is_Standard_Character_Type (R_Typ) then
12131 -- Check for the case of full range, where we are definitely OK
12133 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
12134 return;
12135 end if;
12137 -- Here the range is not the complete base type range, so check
12139 declare
12140 Comp_Typ_Lo : constant Node_Id :=
12141 Type_Low_Bound (Component_Type (Typ));
12142 Comp_Typ_Hi : constant Node_Id :=
12143 Type_High_Bound (Component_Type (Typ));
12145 Char_Val : Uint;
12147 begin
12148 if Compile_Time_Known_Value (Comp_Typ_Lo)
12149 and then Compile_Time_Known_Value (Comp_Typ_Hi)
12150 then
12151 for J in 1 .. Strlen loop
12152 Char_Val := UI_From_CC (Get_String_Char (Str, J));
12154 if Char_Val < Expr_Value (Comp_Typ_Lo)
12155 or else Char_Val > Expr_Value (Comp_Typ_Hi)
12156 then
12157 Apply_Compile_Time_Constraint_Error
12158 (N, "character out of range??",
12159 CE_Range_Check_Failed,
12160 Loc => Loc + Source_Ptr (J));
12161 end if;
12162 end loop;
12164 if not Has_Static_Predicate (C_Typ) then
12165 return;
12166 end if;
12167 end if;
12168 end;
12169 end if;
12170 end if;
12172 -- If we got here we meed to transform the string literal into the
12173 -- equivalent qualified positional array aggregate. This is rather
12174 -- heavy artillery for this situation, but it is hard work to avoid.
12176 declare
12177 Lits : constant List_Id := New_List;
12178 P : Source_Ptr := Loc + 1;
12179 C : Char_Code;
12181 begin
12182 -- Build the character literals, we give them source locations that
12183 -- correspond to the string positions, which is a bit tricky given
12184 -- the possible presence of wide character escape sequences.
12186 for J in 1 .. Strlen loop
12187 C := Get_String_Char (Str, J);
12188 Set_Character_Literal_Name (C);
12190 Append_To (Lits,
12191 Make_Character_Literal (P,
12192 Chars => Name_Find,
12193 Char_Literal_Value => UI_From_CC (C)));
12195 if In_Character_Range (C) then
12196 P := P + 1;
12198 -- Should we have a call to Skip_Wide here ???
12200 -- ??? else
12201 -- Skip_Wide (P);
12203 end if;
12204 end loop;
12206 Rewrite (N,
12207 Make_Qualified_Expression (Loc,
12208 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
12209 Expression =>
12210 Make_Aggregate (Loc, Expressions => Lits)));
12212 Analyze_And_Resolve (N, Typ);
12213 end;
12214 end Resolve_String_Literal;
12216 -------------------------
12217 -- Resolve_Target_Name --
12218 -------------------------
12220 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
12221 begin
12222 Set_Etype (N, Typ);
12223 end Resolve_Target_Name;
12225 -----------------------------
12226 -- Resolve_Type_Conversion --
12227 -----------------------------
12229 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
12230 Conv_OK : constant Boolean := Conversion_OK (N);
12231 Operand : constant Node_Id := Expression (N);
12232 Operand_Typ : constant Entity_Id := Etype (Operand);
12233 Target_Typ : constant Entity_Id := Etype (N);
12234 Rop : Node_Id;
12235 Orig_N : Node_Id;
12236 Orig_T : Node_Id;
12238 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
12239 -- Set to False to suppress cases where we want to suppress the test
12240 -- for redundancy to avoid possible false positives on this warning.
12242 begin
12243 if not Conv_OK
12244 and then not Valid_Conversion (N, Target_Typ, Operand)
12245 then
12246 return;
12247 end if;
12249 -- If the Operand Etype is Universal_Fixed, then the conversion is
12250 -- never redundant. We need this check because by the time we have
12251 -- finished the rather complex transformation, the conversion looks
12252 -- redundant when it is not.
12254 if Operand_Typ = Universal_Fixed then
12255 Test_Redundant := False;
12257 -- If the operand is marked as Any_Fixed, then special processing is
12258 -- required. This is also a case where we suppress the test for a
12259 -- redundant conversion, since most certainly it is not redundant.
12261 elsif Operand_Typ = Any_Fixed then
12262 Test_Redundant := False;
12264 -- Mixed-mode operation involving a literal. Context must be a fixed
12265 -- type which is applied to the literal subsequently.
12267 -- Multiplication and division involving two fixed type operands must
12268 -- yield a universal real because the result is computed in arbitrary
12269 -- precision.
12271 if Is_Fixed_Point_Type (Typ)
12272 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
12273 and then Etype (Left_Opnd (Operand)) = Any_Fixed
12274 and then Etype (Right_Opnd (Operand)) = Any_Fixed
12275 then
12276 Set_Etype (Operand, Universal_Real);
12278 elsif Is_Numeric_Type (Typ)
12279 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
12280 and then (Etype (Right_Opnd (Operand)) = Universal_Real
12281 or else
12282 Etype (Left_Opnd (Operand)) = Universal_Real)
12283 then
12284 -- Return if expression is ambiguous
12286 if Unique_Fixed_Point_Type (N) = Any_Type then
12287 return;
12289 -- If nothing else, the available fixed type is Duration
12291 else
12292 Set_Etype (Operand, Standard_Duration);
12293 end if;
12295 -- Resolve the real operand with largest available precision
12297 if Etype (Right_Opnd (Operand)) = Universal_Real then
12298 Rop := New_Copy_Tree (Right_Opnd (Operand));
12299 else
12300 Rop := New_Copy_Tree (Left_Opnd (Operand));
12301 end if;
12303 Resolve (Rop, Universal_Real);
12305 -- If the operand is a literal (it could be a non-static and
12306 -- illegal exponentiation) check whether the use of Duration
12307 -- is potentially inaccurate.
12309 if Nkind (Rop) = N_Real_Literal
12310 and then Realval (Rop) /= Ureal_0
12311 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
12312 then
12313 Error_Msg_N
12314 ("??universal real operand can only "
12315 & "be interpreted as Duration!", Rop);
12316 Error_Msg_N
12317 ("\??precision will be lost in the conversion!", Rop);
12318 end if;
12320 elsif Is_Numeric_Type (Typ)
12321 and then Nkind (Operand) in N_Op
12322 and then Unique_Fixed_Point_Type (N) /= Any_Type
12323 then
12324 Set_Etype (Operand, Standard_Duration);
12326 else
12327 Error_Msg_N ("invalid context for mixed mode operation", N);
12328 Set_Etype (Operand, Any_Type);
12329 return;
12330 end if;
12331 end if;
12333 Resolve (Operand);
12335 Analyze_Dimension (N);
12337 -- Note: we do the Eval_Type_Conversion call before applying the
12338 -- required checks for a subtype conversion. This is important, since
12339 -- both are prepared under certain circumstances to change the type
12340 -- conversion to a constraint error node, but in the case of
12341 -- Eval_Type_Conversion this may reflect an illegality in the static
12342 -- case, and we would miss the illegality (getting only a warning
12343 -- message), if we applied the type conversion checks first.
12345 Eval_Type_Conversion (N);
12347 -- Even when evaluation is not possible, we may be able to simplify the
12348 -- conversion or its expression. This needs to be done before applying
12349 -- checks, since otherwise the checks may use the original expression
12350 -- and defeat the simplifications. This is specifically the case for
12351 -- elimination of the floating-point Truncation attribute in
12352 -- float-to-int conversions.
12354 Simplify_Type_Conversion (N);
12356 -- If after evaluation we still have a type conversion, then we may need
12357 -- to apply checks required for a subtype conversion. But skip them if
12358 -- universal fixed operands are involved, since range checks are handled
12359 -- separately for these cases, after the expansion done by Exp_Fixd.
12361 if Nkind (N) = N_Type_Conversion
12362 and then not Is_Generic_Type (Root_Type (Target_Typ))
12363 and then Target_Typ /= Universal_Fixed
12364 and then Etype (Operand) /= Universal_Fixed
12365 then
12366 Apply_Type_Conversion_Checks (N);
12367 end if;
12369 -- Issue warning for conversion of simple object to its own type. We
12370 -- have to test the original nodes, since they may have been rewritten
12371 -- by various optimizations.
12373 Orig_N := Original_Node (N);
12375 -- Here we test for a redundant conversion if the warning mode is
12376 -- active (and was not locally reset), and we have a type conversion
12377 -- from source not appearing in a generic instance.
12379 if Test_Redundant
12380 and then Nkind (Orig_N) = N_Type_Conversion
12381 and then Comes_From_Source (Orig_N)
12382 and then not In_Instance
12383 then
12384 Orig_N := Original_Node (Expression (Orig_N));
12385 Orig_T := Target_Typ;
12387 -- If the node is part of a larger expression, the Target_Type
12388 -- may not be the original type of the node if the context is a
12389 -- condition. Recover original type to see if conversion is needed.
12391 if Is_Boolean_Type (Orig_T)
12392 and then Nkind (Parent (N)) in N_Op
12393 then
12394 Orig_T := Etype (Parent (N));
12395 end if;
12397 -- If we have an entity name, then give the warning if the entity
12398 -- is the right type, or if it is a loop parameter covered by the
12399 -- original type (that's needed because loop parameters have an
12400 -- odd subtype coming from the bounds).
12402 if (Is_Entity_Name (Orig_N)
12403 and then Present (Entity (Orig_N))
12404 and then
12405 (Etype (Entity (Orig_N)) = Orig_T
12406 or else
12407 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
12408 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
12410 -- If not an entity, then type of expression must match
12412 or else Etype (Orig_N) = Orig_T
12413 then
12414 -- One more check, do not give warning if the analyzed conversion
12415 -- has an expression with non-static bounds, and the bounds of the
12416 -- target are static. This avoids junk warnings in cases where the
12417 -- conversion is necessary to establish staticness, for example in
12418 -- a case statement.
12420 if not Is_OK_Static_Subtype (Operand_Typ)
12421 and then Is_OK_Static_Subtype (Target_Typ)
12422 then
12423 null;
12425 -- Never give a warning if the operand is a conditional expression
12426 -- because RM 4.5.7(10/3) forces its type to be the target type.
12428 elsif Nkind (Orig_N) in N_Case_Expression | N_If_Expression then
12429 null;
12431 -- Finally, if this type conversion occurs in a context requiring
12432 -- a prefix, and the expression is a qualified expression then the
12433 -- type conversion is not redundant, since a qualified expression
12434 -- is not a prefix, whereas a type conversion is. For example, "X
12435 -- := T'(Funx(...)).Y;" is illegal because a selected component
12436 -- requires a prefix, but a type conversion makes it legal: "X :=
12437 -- T(T'(Funx(...))).Y;"
12439 -- In Ada 2012, a qualified expression is a name, so this idiom is
12440 -- no longer needed, but we still suppress the warning because it
12441 -- seems unfriendly for warnings to pop up when you switch to the
12442 -- newer language version.
12444 elsif Nkind (Orig_N) = N_Qualified_Expression
12445 and then Nkind (Parent (N)) in N_Attribute_Reference
12446 | N_Indexed_Component
12447 | N_Selected_Component
12448 | N_Slice
12449 | N_Explicit_Dereference
12450 then
12451 null;
12453 -- Never warn on conversion to Long_Long_Integer'Base since
12454 -- that is most likely an artifact of the extended overflow
12455 -- checking and comes from complex expanded code.
12457 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
12458 null;
12460 -- Do not warn on conversion to class-wide type on helpers of
12461 -- class-wide preconditions because in this context the warning
12462 -- would be spurious (since the class-wide precondition has been
12463 -- installed in the return statement of the helper, which has a
12464 -- class-wide formal type instead of a regular tagged type).
12466 elsif Is_Class_Wide_Type (Orig_T)
12467 and then Is_Subprogram_Or_Generic_Subprogram (Current_Scope)
12468 and then Present (Class_Preconditions_Subprogram (Current_Scope))
12469 then
12470 null;
12472 -- Here we give the redundant conversion warning. If it is an
12473 -- entity, give the name of the entity in the message. If not,
12474 -- just mention the expression.
12476 else
12477 if Is_Entity_Name (Orig_N) then
12478 Error_Msg_Node_2 := Orig_T;
12479 Error_Msg_NE -- CODEFIX
12480 ("?r?redundant conversion, & is of type &!",
12481 N, Entity (Orig_N));
12482 else
12483 Error_Msg_NE
12484 ("?r?redundant conversion, expression is of type&!",
12485 N, Orig_T);
12486 end if;
12487 end if;
12488 end if;
12489 end if;
12491 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
12492 -- No need to perform any interface conversion if the type of the
12493 -- expression coincides with the target type.
12495 if Ada_Version >= Ada_2005
12496 and then Expander_Active
12497 and then Operand_Typ /= Target_Typ
12498 then
12499 declare
12500 Opnd : Entity_Id := Operand_Typ;
12501 Target : Entity_Id := Target_Typ;
12503 begin
12504 -- If the type of the operand is a limited view, use nonlimited
12505 -- view when available. If it is a class-wide type, recover the
12506 -- class-wide type of the nonlimited view.
12508 if From_Limited_With (Opnd)
12509 and then Has_Non_Limited_View (Opnd)
12510 then
12511 Opnd := Non_Limited_View (Opnd);
12512 Set_Etype (Expression (N), Opnd);
12513 end if;
12515 -- It seems that Non_Limited_View should also be applied for
12516 -- Target when it has a limited view, but that leads to missing
12517 -- error checks on interface conversions further below. ???
12519 if Is_Access_Type (Opnd) then
12520 Opnd := Designated_Type (Opnd);
12522 -- If the type of the operand is a limited view, use nonlimited
12523 -- view when available. If it is a class-wide type, recover the
12524 -- class-wide type of the nonlimited view.
12526 if From_Limited_With (Opnd)
12527 and then Has_Non_Limited_View (Opnd)
12528 then
12529 Opnd := Non_Limited_View (Opnd);
12530 end if;
12531 end if;
12533 if Is_Access_Type (Target_Typ) then
12534 Target := Designated_Type (Target);
12536 -- If the target type is a limited view, use nonlimited view
12537 -- when available.
12539 if From_Limited_With (Target)
12540 and then Has_Non_Limited_View (Target)
12541 then
12542 Target := Non_Limited_View (Target);
12543 end if;
12544 end if;
12546 if Opnd = Target then
12547 null;
12549 -- Conversion from interface type
12551 -- It seems that it would be better for the error checks below
12552 -- to be performed as part of Validate_Conversion (and maybe some
12553 -- of the error checks above could be moved as well?). ???
12555 elsif Is_Interface (Opnd) then
12557 -- Ada 2005 (AI-217): Handle entities from limited views
12559 if From_Limited_With (Opnd) then
12560 Error_Msg_Qual_Level := 99;
12561 Error_Msg_NE -- CODEFIX
12562 ("missing WITH clause on package &", N,
12563 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
12564 Error_Msg_N
12565 ("type conversions require visibility of the full view",
12568 elsif From_Limited_With (Target)
12569 and then not
12570 (Is_Access_Type (Target_Typ)
12571 and then Present (Non_Limited_View (Etype (Target))))
12572 then
12573 Error_Msg_Qual_Level := 99;
12574 Error_Msg_NE -- CODEFIX
12575 ("missing WITH clause on package &", N,
12576 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12577 Error_Msg_N
12578 ("type conversions require visibility of the full view",
12581 else
12582 Expand_Interface_Conversion (N);
12583 end if;
12585 -- Conversion to interface type
12587 elsif Is_Interface (Target) then
12588 Expand_Interface_Conversion (N);
12589 end if;
12590 end;
12591 end if;
12593 -- Ada 2012: Once the type conversion is resolved, check whether the
12594 -- operand satisfies a static predicate of the target subtype, if any.
12595 -- In the static expression case, a predicate check failure is an error.
12597 if Has_Predicates (Target_Typ) then
12598 Check_Expression_Against_Static_Predicate
12599 (N, Target_Typ, Static_Failure_Is_Error => True);
12600 end if;
12602 -- If at this stage we have a fixed to integer conversion, make sure the
12603 -- Do_Range_Check flag is set, because such conversions in general need
12604 -- a range check. We only need this if expansion is off, see above why.
12606 if Nkind (N) = N_Type_Conversion
12607 and then not Expander_Active
12608 and then Is_Integer_Type (Target_Typ)
12609 and then Is_Fixed_Point_Type (Operand_Typ)
12610 and then not Range_Checks_Suppressed (Target_Typ)
12611 and then not Range_Checks_Suppressed (Operand_Typ)
12612 then
12613 Set_Do_Range_Check (Operand);
12614 end if;
12616 -- Generating C code a type conversion of an access to constrained
12617 -- array type to access to unconstrained array type involves building
12618 -- a fat pointer which in general cannot be generated on the fly. We
12619 -- remove side effects in order to store the result of the conversion
12620 -- into a temporary.
12622 if Modify_Tree_For_C
12623 and then Nkind (N) = N_Type_Conversion
12624 and then Nkind (Parent (N)) /= N_Object_Declaration
12625 and then Is_Access_Type (Etype (N))
12626 and then Is_Array_Type (Designated_Type (Etype (N)))
12627 and then not Is_Constrained (Designated_Type (Etype (N)))
12628 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
12629 then
12630 Remove_Side_Effects (N);
12631 end if;
12632 end Resolve_Type_Conversion;
12634 ----------------------
12635 -- Resolve_Unary_Op --
12636 ----------------------
12638 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12639 B_Typ : constant Entity_Id := Base_Type (Typ);
12640 R : constant Node_Id := Right_Opnd (N);
12641 OK : Boolean;
12642 Lo : Uint;
12643 Hi : Uint;
12645 begin
12646 -- Deal with intrinsic unary operators
12648 if Comes_From_Source (N)
12649 and then Ekind (Entity (N)) = E_Function
12650 and then Is_Imported (Entity (N))
12651 and then Is_Intrinsic_Subprogram (Entity (N))
12652 then
12653 Resolve_Intrinsic_Unary_Operator (N, Typ);
12654 return;
12655 end if;
12657 -- Deal with universal cases
12659 if Is_Universal_Numeric_Type (Etype (R)) then
12660 Check_For_Visible_Operator (N, B_Typ);
12661 end if;
12663 Set_Etype (N, B_Typ);
12664 Resolve (R, B_Typ);
12666 -- Generate warning for negative literal of a modular type, unless it is
12667 -- enclosed directly in a type qualification or a type conversion, as it
12668 -- is likely not what the user intended. We don't issue the warning for
12669 -- the common use of -1 to denote OxFFFF_FFFF...
12671 if Warn_On_Suspicious_Modulus_Value
12672 and then Nkind (N) = N_Op_Minus
12673 and then Nkind (R) = N_Integer_Literal
12674 and then Comes_From_Source (R)
12675 and then Is_Modular_Integer_Type (B_Typ)
12676 and then Nkind (Parent (N)) not in N_Qualified_Expression
12677 | N_Type_Conversion
12678 and then Expr_Value (R) > Uint_1
12679 then
12680 Error_Msg_N
12681 ("?.m?negative literal of modular type is in fact positive", N);
12682 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12683 Error_Msg_Uint_2 := Expr_Value (R);
12684 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12685 Error_Msg_N
12686 ("\if you do, use qualification to avoid this warning", N);
12687 end if;
12689 -- Generate warning for expressions like abs (x mod 2)
12691 if Warn_On_Redundant_Constructs
12692 and then Nkind (N) = N_Op_Abs
12693 then
12694 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12696 if OK and then Hi >= Lo and then Lo >= 0 then
12697 Error_Msg_N -- CODEFIX
12698 ("?r?abs applied to known non-negative value has no effect", N);
12699 end if;
12700 end if;
12702 -- Deal with reference generation
12704 Check_Unset_Reference (R);
12705 Generate_Operator_Reference (N, B_Typ);
12706 Analyze_Dimension (N);
12707 Eval_Unary_Op (N);
12709 -- Set overflow checking bit. Much cleverer code needed here eventually
12710 -- and perhaps the Resolve routines should be separated for the various
12711 -- arithmetic operations, since they will need different processing ???
12713 if Nkind (N) in N_Op then
12714 if not Overflow_Checks_Suppressed (Etype (N)) then
12715 Enable_Overflow_Check (N);
12716 end if;
12717 end if;
12719 -- Generate warning for expressions like -5 mod 3 for integers. No need
12720 -- to worry in the floating-point case, since parens do not affect the
12721 -- result so there is no point in giving in a warning.
12723 declare
12724 Norig : constant Node_Id := Original_Node (N);
12725 Rorig : Node_Id;
12726 Val : Uint;
12727 HB : Uint;
12728 LB : Uint;
12729 Lval : Uint;
12730 Opnd : Node_Id;
12732 begin
12733 if Warn_On_Questionable_Missing_Parens
12734 and then Comes_From_Source (Norig)
12735 and then Is_Integer_Type (Typ)
12736 and then Nkind (Norig) = N_Op_Minus
12737 then
12738 Rorig := Original_Node (Right_Opnd (Norig));
12740 -- We are looking for cases where the right operand is not
12741 -- parenthesized, and is a binary operator, multiply, divide, or
12742 -- mod. These are the cases where the grouping can affect results.
12744 if Paren_Count (Rorig) = 0
12745 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12746 then
12747 -- For mod, we always give the warning, since the value is
12748 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12749 -- -(5 mod 315)). But for the other cases, the only concern is
12750 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12751 -- overflows, but (-2) * 64 does not). So we try to give the
12752 -- message only when overflow is possible.
12754 if Nkind (Rorig) /= N_Op_Mod
12755 and then Compile_Time_Known_Value (R)
12756 then
12757 Val := Expr_Value (R);
12759 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12760 HB := Expr_Value (Type_High_Bound (Typ));
12761 else
12762 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12763 end if;
12765 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12766 LB := Expr_Value (Type_Low_Bound (Typ));
12767 else
12768 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12769 end if;
12771 -- Note that the test below is deliberately excluding the
12772 -- largest negative number, since that is a potentially
12773 -- troublesome case (e.g. -2 * x, where the result is the
12774 -- largest negative integer has an overflow with 2 * x).
12776 if Val > LB and then Val <= HB then
12777 return;
12778 end if;
12779 end if;
12781 -- For the multiplication case, the only case we have to worry
12782 -- about is when (-a)*b is exactly the largest negative number
12783 -- so that -(a*b) can cause overflow. This can only happen if
12784 -- a is a power of 2, and more generally if any operand is a
12785 -- constant that is not a power of 2, then the parentheses
12786 -- cannot affect whether overflow occurs. We only bother to
12787 -- test the left most operand
12789 -- Loop looking at left operands for one that has known value
12791 Opnd := Rorig;
12792 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12793 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12794 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12796 -- Operand value of 0 or 1 skips warning
12798 if Lval <= 1 then
12799 return;
12801 -- Otherwise check power of 2, if power of 2, warn, if
12802 -- anything else, skip warning.
12804 else
12805 while Lval /= 2 loop
12806 if Lval mod 2 = 1 then
12807 return;
12808 else
12809 Lval := Lval / 2;
12810 end if;
12811 end loop;
12813 exit Opnd_Loop;
12814 end if;
12815 end if;
12817 -- Keep looking at left operands
12819 Opnd := Left_Opnd (Opnd);
12820 end loop Opnd_Loop;
12822 -- For rem or "/" we can only have a problematic situation
12823 -- if the divisor has a value of minus one or one. Otherwise
12824 -- overflow is impossible (divisor > 1) or we have a case of
12825 -- division by zero in any case.
12827 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12828 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12829 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12830 then
12831 return;
12832 end if;
12834 -- If we fall through warning should be issued
12836 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12838 Error_Msg_N
12839 ("??unary minus expression should be parenthesized here!", N);
12840 end if;
12841 end if;
12842 end;
12843 end Resolve_Unary_Op;
12845 ----------------------------------
12846 -- Resolve_Unchecked_Expression --
12847 ----------------------------------
12849 procedure Resolve_Unchecked_Expression
12850 (N : Node_Id;
12851 Typ : Entity_Id)
12853 begin
12854 Resolve (Expression (N), Typ, Suppress => All_Checks);
12855 Set_Etype (N, Typ);
12856 end Resolve_Unchecked_Expression;
12858 ---------------------------------------
12859 -- Resolve_Unchecked_Type_Conversion --
12860 ---------------------------------------
12862 procedure Resolve_Unchecked_Type_Conversion
12863 (N : Node_Id;
12864 Typ : Entity_Id)
12866 pragma Warnings (Off, Typ);
12868 Operand : constant Node_Id := Expression (N);
12869 Opnd_Type : constant Entity_Id := Etype (Operand);
12871 begin
12872 -- Resolve operand using its own type
12874 Resolve (Operand, Opnd_Type);
12876 -- If the expression is a conversion to universal integer of an
12877 -- an expression with an integer type, then we can eliminate the
12878 -- intermediate conversion to universal integer.
12880 if Nkind (Operand) = N_Type_Conversion
12881 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12882 and then Is_Integer_Type (Etype (Expression (Operand)))
12883 then
12884 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12885 Analyze_And_Resolve (Operand);
12886 end if;
12888 -- In an inlined context, the unchecked conversion may be applied
12889 -- to a literal, in which case its type is the type of the context.
12890 -- (In other contexts conversions cannot apply to literals).
12892 if In_Inlined_Body
12893 and then (Opnd_Type = Any_Character or else
12894 Opnd_Type = Any_Integer or else
12895 Opnd_Type = Any_Real)
12896 then
12897 Set_Etype (Operand, Typ);
12898 end if;
12900 Analyze_Dimension (N);
12901 Eval_Unchecked_Conversion (N);
12902 end Resolve_Unchecked_Type_Conversion;
12904 ------------------------------
12905 -- Rewrite_Operator_As_Call --
12906 ------------------------------
12908 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12909 Loc : constant Source_Ptr := Sloc (N);
12910 Actuals : constant List_Id := New_List;
12911 New_N : Node_Id;
12913 begin
12914 if Nkind (N) in N_Binary_Op then
12915 Append (Left_Opnd (N), Actuals);
12916 end if;
12918 Append (Right_Opnd (N), Actuals);
12920 New_N :=
12921 Make_Function_Call (Sloc => Loc,
12922 Name => New_Occurrence_Of (Nam, Loc),
12923 Parameter_Associations => Actuals);
12925 Preserve_Comes_From_Source (New_N, N);
12926 Preserve_Comes_From_Source (Name (New_N), N);
12927 Rewrite (N, New_N);
12928 Set_Etype (N, Etype (Nam));
12929 end Rewrite_Operator_As_Call;
12931 ------------------------------
12932 -- Rewrite_Renamed_Operator --
12933 ------------------------------
12935 procedure Rewrite_Renamed_Operator
12936 (N : Node_Id;
12937 Op : Entity_Id;
12938 Typ : Entity_Id)
12940 Nam : constant Name_Id := Chars (Op);
12941 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12942 Op_Node : Node_Id;
12944 begin
12945 -- Do not perform this transformation within a pre/postcondition,
12946 -- because the expression will be reanalyzed, and the transformation
12947 -- might affect the visibility of the operator, e.g. in an instance.
12948 -- Note that fully analyzed and expanded pre/postconditions appear as
12949 -- pragma Check equivalents.
12951 if In_Pre_Post_Condition (N) then
12952 return;
12953 end if;
12955 -- Likewise when an expression function is being preanalyzed, since the
12956 -- expression will be reanalyzed as part of the generated body.
12958 if In_Spec_Expression then
12959 declare
12960 S : constant Entity_Id := Current_Scope_No_Loops;
12961 begin
12962 if Ekind (S) = E_Function
12963 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12964 N_Expression_Function
12965 then
12966 return;
12967 end if;
12968 end;
12969 end if;
12971 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12972 Set_Chars (Op_Node, Nam);
12973 Set_Etype (Op_Node, Etype (N));
12974 Set_Entity (Op_Node, Op);
12975 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12977 if Is_Binary then
12978 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12979 end if;
12981 -- Indicate that both the original entity and its renaming are
12982 -- referenced at this point.
12984 Generate_Reference (Entity (N), N);
12985 Generate_Reference (Op, N);
12987 Rewrite (N, Op_Node);
12989 -- If the context type is private, add the appropriate conversions so
12990 -- that the operator is applied to the full view. This is done in the
12991 -- routines that resolve intrinsic operators.
12993 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12994 case Nkind (N) is
12995 when N_Op_Add
12996 | N_Op_Divide
12997 | N_Op_Expon
12998 | N_Op_Mod
12999 | N_Op_Multiply
13000 | N_Op_Rem
13001 | N_Op_Subtract
13003 Resolve_Intrinsic_Operator (N, Typ);
13005 when N_Op_Abs
13006 | N_Op_Minus
13007 | N_Op_Plus
13009 Resolve_Intrinsic_Unary_Operator (N, Typ);
13011 when others =>
13012 Resolve (N, Typ);
13013 end case;
13014 end if;
13015 end Rewrite_Renamed_Operator;
13017 -----------------------
13018 -- Set_Slice_Subtype --
13019 -----------------------
13021 -- Build an implicit subtype declaration to represent the type delivered by
13022 -- the slice. This is an abbreviated version of an array subtype. We define
13023 -- an index subtype for the slice, using either the subtype name or the
13024 -- discrete range of the slice. To be consistent with index usage elsewhere
13025 -- we create a list header to hold the single index. This list is not
13026 -- otherwise attached to the syntax tree.
13028 procedure Set_Slice_Subtype (N : Node_Id) is
13029 Loc : constant Source_Ptr := Sloc (N);
13030 Index_List : constant List_Id := New_List;
13031 Index : Node_Id;
13032 Index_Subtype : Entity_Id;
13033 Index_Type : Entity_Id;
13034 Slice_Subtype : Entity_Id;
13035 Drange : constant Node_Id := Discrete_Range (N);
13037 begin
13038 Index_Type := Base_Type (Etype (Drange));
13040 if Is_Entity_Name (Drange) then
13041 Index_Subtype := Entity (Drange);
13043 else
13044 -- We force the evaluation of a range. This is definitely needed in
13045 -- the renamed case, and seems safer to do unconditionally. Note in
13046 -- any case that since we will create and insert an Itype referring
13047 -- to this range, we must make sure any side effect removal actions
13048 -- are inserted before the Itype definition.
13050 if Nkind (Drange) = N_Range then
13051 Force_Evaluation (Low_Bound (Drange));
13052 Force_Evaluation (High_Bound (Drange));
13054 -- If the discrete range is given by a subtype indication, the
13055 -- type of the slice is the base of the subtype mark.
13057 elsif Nkind (Drange) = N_Subtype_Indication then
13058 declare
13059 R : constant Node_Id := Range_Expression (Constraint (Drange));
13060 begin
13061 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
13062 Force_Evaluation (Low_Bound (R));
13063 Force_Evaluation (High_Bound (R));
13064 end;
13065 end if;
13067 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13069 -- Take a new copy of Drange (where bounds have been rewritten to
13070 -- reference side-effect-free names). Using a separate tree ensures
13071 -- that further expansion (e.g. while rewriting a slice assignment
13072 -- into a FOR loop) does not attempt to remove side effects on the
13073 -- bounds again (which would cause the bounds in the index subtype
13074 -- definition to refer to temporaries before they are defined) (the
13075 -- reason is that some names are considered side effect free here
13076 -- for the subtype, but not in the context of a loop iteration
13077 -- scheme).
13079 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
13080 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
13081 Set_Etype (Index_Subtype, Index_Type);
13082 Set_Size_Info (Index_Subtype, Index_Type);
13083 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13084 Set_Is_Constrained (Index_Subtype);
13085 end if;
13087 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
13089 Index := New_Occurrence_Of (Index_Subtype, Loc);
13090 Set_Etype (Index, Index_Subtype);
13091 Append (Index, Index_List);
13093 Set_First_Index (Slice_Subtype, Index);
13094 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
13095 Set_Is_Constrained (Slice_Subtype, True);
13097 Check_Compile_Time_Size (Slice_Subtype);
13099 -- The Etype of the existing Slice node is reset to this slice subtype.
13100 -- Its bounds are obtained from its first index.
13102 Set_Etype (N, Slice_Subtype);
13104 -- For bit-packed slice subtypes, freeze immediately (except in the case
13105 -- of being in a "spec expression" where we never freeze when we first
13106 -- see the expression).
13108 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
13109 Freeze_Itype (Slice_Subtype, N);
13111 -- For all other cases insert an itype reference in the slice's actions
13112 -- so that the itype is frozen at the proper place in the tree (i.e. at
13113 -- the point where actions for the slice are analyzed). Note that this
13114 -- is different from freezing the itype immediately, which might be
13115 -- premature (e.g. if the slice is within a transient scope). This needs
13116 -- to be done only if expansion is enabled.
13118 elsif Expander_Active then
13119 Ensure_Defined (Typ => Slice_Subtype, N => N);
13120 end if;
13121 end Set_Slice_Subtype;
13123 --------------------------------
13124 -- Set_String_Literal_Subtype --
13125 --------------------------------
13127 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
13128 Loc : constant Source_Ptr := Sloc (N);
13129 Low_Bound : constant Node_Id :=
13130 Type_Low_Bound (Etype (First_Index (Typ)));
13131 Subtype_Id : Entity_Id;
13133 begin
13134 if Nkind (N) /= N_String_Literal then
13135 return;
13136 end if;
13138 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
13139 Set_String_Literal_Length (Subtype_Id, UI_From_Int
13140 (String_Length (Strval (N))));
13141 Set_Etype (Subtype_Id, Base_Type (Typ));
13142 Set_Is_Constrained (Subtype_Id);
13143 Set_Etype (N, Subtype_Id);
13145 -- The low bound is set from the low bound of the corresponding index
13146 -- type. Note that we do not store the high bound in the string literal
13147 -- subtype, but it can be deduced if necessary from the length and the
13148 -- low bound.
13150 if Is_OK_Static_Expression (Low_Bound) then
13151 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
13153 -- If the lower bound is not static we create a range for the string
13154 -- literal, using the index type and the known length of the literal.
13155 -- If the length is 1, then the upper bound is set to a mere copy of
13156 -- the lower bound; or else, if the index type is a signed integer,
13157 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
13158 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
13160 else
13161 declare
13162 Length : constant Nat := String_Length (Strval (N));
13163 Index_List : constant List_Id := New_List;
13164 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
13165 Array_Subtype : Entity_Id;
13166 Drange : Node_Id;
13167 High_Bound : Node_Id;
13168 Index : Node_Id;
13169 Index_Subtype : Entity_Id;
13171 begin
13172 if Length = 1 then
13173 High_Bound := New_Copy_Tree (Low_Bound);
13175 elsif Is_Signed_Integer_Type (Index_Type) then
13176 High_Bound :=
13177 Make_Op_Add (Loc,
13178 Left_Opnd => New_Copy_Tree (Low_Bound),
13179 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
13181 else
13182 High_Bound :=
13183 Make_Attribute_Reference (Loc,
13184 Attribute_Name => Name_Val,
13185 Prefix =>
13186 New_Occurrence_Of (Index_Type, Loc),
13187 Expressions => New_List (
13188 Make_Op_Add (Loc,
13189 Left_Opnd =>
13190 Make_Attribute_Reference (Loc,
13191 Attribute_Name => Name_Pos,
13192 Prefix =>
13193 New_Occurrence_Of (Index_Type, Loc),
13194 Expressions =>
13195 New_List (New_Copy_Tree (Low_Bound))),
13196 Right_Opnd =>
13197 Make_Integer_Literal (Loc, Length - 1))));
13198 end if;
13200 if Is_Integer_Type (Index_Type) then
13201 Set_String_Literal_Low_Bound
13202 (Subtype_Id, Make_Integer_Literal (Loc, 1));
13204 else
13205 -- If the index type is an enumeration type, build bounds
13206 -- expression with attributes.
13208 Set_String_Literal_Low_Bound
13209 (Subtype_Id,
13210 Make_Attribute_Reference (Loc,
13211 Attribute_Name => Name_First,
13212 Prefix =>
13213 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
13214 end if;
13216 Analyze_And_Resolve
13217 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
13219 -- Build bona fide subtype for the string, and wrap it in an
13220 -- unchecked conversion, because the back end expects the
13221 -- String_Literal_Subtype to have a static lower bound.
13223 Index_Subtype :=
13224 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13225 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
13226 Set_Scalar_Range (Index_Subtype, Drange);
13227 Set_Parent (Drange, N);
13228 Analyze_And_Resolve (Drange, Index_Type);
13230 -- In this context, the Index_Type may already have a constraint,
13231 -- so use common base type on string subtype. The base type may
13232 -- be used when generating attributes of the string, for example
13233 -- in the context of a slice assignment.
13235 Set_Etype (Index_Subtype, Base_Type (Index_Type));
13236 Set_Size_Info (Index_Subtype, Index_Type);
13237 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13239 Array_Subtype := Create_Itype (E_Array_Subtype, N);
13241 Index := New_Occurrence_Of (Index_Subtype, Loc);
13242 Set_Etype (Index, Index_Subtype);
13243 Append (Index, Index_List);
13245 Set_First_Index (Array_Subtype, Index);
13246 Set_Etype (Array_Subtype, Base_Type (Typ));
13247 Set_Is_Constrained (Array_Subtype, True);
13249 Rewrite (N, Unchecked_Convert_To (Array_Subtype, N));
13250 Set_Etype (N, Array_Subtype);
13251 end;
13252 end if;
13253 end Set_String_Literal_Subtype;
13255 ------------------------------
13256 -- Simplify_Type_Conversion --
13257 ------------------------------
13259 procedure Simplify_Type_Conversion (N : Node_Id) is
13260 begin
13261 if Nkind (N) = N_Type_Conversion then
13262 declare
13263 Operand : constant Node_Id := Expression (N);
13264 Target_Typ : constant Entity_Id := Etype (N);
13265 Opnd_Typ : constant Entity_Id := Etype (Operand);
13267 begin
13268 -- Special processing if the conversion is the expression of a
13269 -- Rounding or Truncation attribute reference. In this case we
13270 -- replace:
13272 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
13274 -- by
13276 -- ityp (x)
13278 -- with the Float_Truncate flag set to False or True respectively,
13279 -- which is more efficient. We reuse Rounding for Machine_Rounding
13280 -- as System.Fat_Gen, which is a permissible behavior.
13282 if Is_Floating_Point_Type (Opnd_Typ)
13283 and then
13284 (Is_Integer_Type (Target_Typ)
13285 or else (Is_Fixed_Point_Type (Target_Typ)
13286 and then Conversion_OK (N)))
13287 and then Nkind (Operand) = N_Attribute_Reference
13288 and then Attribute_Name (Operand) in Name_Rounding
13289 | Name_Machine_Rounding
13290 | Name_Truncation
13291 then
13292 declare
13293 Truncate : constant Boolean :=
13294 Attribute_Name (Operand) = Name_Truncation;
13295 begin
13296 Rewrite (Operand,
13297 Relocate_Node (First (Expressions (Operand))));
13298 Set_Float_Truncate (N, Truncate);
13299 end;
13301 -- Special processing for the conversion of an integer literal to
13302 -- a dynamic type: we first convert the literal to the root type
13303 -- and then convert the result to the target type, the goal being
13304 -- to avoid doing range checks in universal integer.
13306 elsif Is_Integer_Type (Target_Typ)
13307 and then not Is_Generic_Type (Root_Type (Target_Typ))
13308 and then Nkind (Operand) = N_Integer_Literal
13309 and then Opnd_Typ = Universal_Integer
13310 then
13311 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
13312 Analyze_And_Resolve (Operand);
13314 -- If the expression is a conversion to universal integer of an
13315 -- an expression with an integer type, then we can eliminate the
13316 -- intermediate conversion to universal integer.
13318 elsif Nkind (Operand) = N_Type_Conversion
13319 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
13320 and then Is_Integer_Type (Etype (Expression (Operand)))
13321 then
13322 Rewrite (Operand, Relocate_Node (Expression (Operand)));
13323 Analyze_And_Resolve (Operand);
13324 end if;
13325 end;
13326 end if;
13327 end Simplify_Type_Conversion;
13329 ------------------------------
13330 -- Try_User_Defined_Literal --
13331 ------------------------------
13333 function Try_User_Defined_Literal
13334 (N : Node_Id;
13335 Typ : Entity_Id) return Boolean
13337 begin
13338 if Has_Applicable_User_Defined_Literal (N, Typ) then
13339 return True;
13341 elsif Nkind (N) = N_If_Expression then
13342 -- Both dependent expressions must have the same type as the context
13344 declare
13345 Condition : constant Node_Id := First (Expressions (N));
13346 Then_Expr : constant Node_Id := Next (Condition);
13347 Else_Expr : constant Node_Id := Next (Then_Expr);
13349 begin
13350 if Has_Applicable_User_Defined_Literal (Then_Expr, Typ) then
13351 Resolve (Else_Expr, Typ);
13352 Analyze_And_Resolve (N, Typ);
13353 return True;
13355 elsif Has_Applicable_User_Defined_Literal (Else_Expr, Typ) then
13356 Resolve (Then_Expr, Typ);
13357 Analyze_And_Resolve (N, Typ);
13358 return True;
13359 end if;
13360 end;
13362 elsif Nkind (N) = N_Case_Expression then
13363 -- All dependent expressions must have the same type as the context
13365 declare
13366 Alt : Node_Id;
13368 begin
13369 Alt := First (Alternatives (N));
13371 while Present (Alt) loop
13372 if Has_Applicable_User_Defined_Literal (Expression (Alt), Typ)
13373 then
13374 declare
13375 Other_Alt : Node_Id;
13377 begin
13378 Other_Alt := First (Alternatives (N));
13380 while Present (Other_Alt) loop
13381 if Other_Alt /= Alt then
13382 Resolve (Expression (Other_Alt), Typ);
13383 end if;
13385 Next (Other_Alt);
13386 end loop;
13388 Analyze_And_Resolve (N, Typ);
13389 return True;
13390 end;
13391 end if;
13393 Next (Alt);
13394 end loop;
13395 end;
13396 end if;
13398 return False;
13399 end Try_User_Defined_Literal;
13401 -------------------------------------------
13402 -- Try_User_Defined_Literal_For_Operator --
13403 -------------------------------------------
13405 function Try_User_Defined_Literal_For_Operator
13406 (N : Node_Id;
13407 Typ : Entity_Id) return Boolean
13409 begin
13410 if Nkind (N) in N_Op_Add
13411 | N_Op_Divide
13412 | N_Op_Mod
13413 | N_Op_Multiply
13414 | N_Op_Rem
13415 | N_Op_Subtract
13416 then
13417 -- Both operands must have the same type as the context
13418 -- (ignoring for now fixed-point and exponentiation ops).
13420 if Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13421 or else (Nkind (Left_Opnd (N)) in N_Op
13422 and then Covers (Typ, Etype (Right_Opnd (N))))
13423 then
13424 Resolve (Left_Opnd (N), Typ);
13425 Analyze_And_Resolve (N, Typ);
13426 return True;
13428 elsif Has_Applicable_User_Defined_Literal (Left_Opnd (N), Typ)
13429 or else (Nkind (Right_Opnd (N)) in N_Op
13430 and then Covers (Typ, Etype (Left_Opnd (N))))
13431 then
13432 Resolve (Right_Opnd (N), Typ);
13433 Analyze_And_Resolve (N, Typ);
13434 return True;
13435 end if;
13437 elsif Nkind (N) in N_Binary_Op then
13438 -- For other binary operators the context does not impose a type on
13439 -- the operands, but their types must match.
13441 if Nkind (Left_Opnd (N))
13442 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13443 and then
13444 Has_Applicable_User_Defined_Literal
13445 (Right_Opnd (N), Etype (Left_Opnd (N)))
13446 then
13447 Analyze_And_Resolve (N, Typ);
13448 return True;
13450 elsif Nkind (Right_Opnd (N))
13451 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13452 and then
13453 Has_Applicable_User_Defined_Literal
13454 (Left_Opnd (N), Etype (Right_Opnd (N)))
13455 then
13456 Analyze_And_Resolve (N, Typ);
13457 return True;
13458 end if;
13460 elsif Nkind (N) in N_Unary_Op
13461 and then Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13462 then
13463 Analyze_And_Resolve (N, Typ);
13464 return True;
13465 end if;
13467 return False;
13468 end Try_User_Defined_Literal_For_Operator;
13470 -----------------------------
13471 -- Unique_Fixed_Point_Type --
13472 -----------------------------
13474 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
13475 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
13476 -- Give error messages for true ambiguity. Messages are posted on node
13477 -- N, and entities T1, T2 are the possible interpretations.
13479 -----------------------
13480 -- Fixed_Point_Error --
13481 -----------------------
13483 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
13484 begin
13485 Error_Msg_N ("ambiguous universal_fixed_expression", N);
13486 Error_Msg_NE ("\\possible interpretation as}", N, T1);
13487 Error_Msg_NE ("\\possible interpretation as}", N, T2);
13488 end Fixed_Point_Error;
13490 -- Local variables
13492 ErrN : Node_Id;
13493 Item : Node_Id;
13494 Scop : Entity_Id;
13495 T1 : Entity_Id;
13496 T2 : Entity_Id;
13498 -- Start of processing for Unique_Fixed_Point_Type
13500 begin
13501 -- The operations on Duration are visible, so Duration is always a
13502 -- possible interpretation.
13504 T1 := Standard_Duration;
13506 -- Look for fixed-point types in enclosing scopes
13508 Scop := Current_Scope;
13509 while Scop /= Standard_Standard loop
13510 T2 := First_Entity (Scop);
13511 while Present (T2) loop
13512 if Is_Fixed_Point_Type (T2)
13513 and then Current_Entity (T2) = T2
13514 and then Scope (Base_Type (T2)) = Scop
13515 then
13516 if Present (T1) then
13517 Fixed_Point_Error (T1, T2);
13518 return Any_Type;
13519 else
13520 T1 := T2;
13521 end if;
13522 end if;
13524 Next_Entity (T2);
13525 end loop;
13527 Scop := Scope (Scop);
13528 end loop;
13530 -- Look for visible fixed type declarations in the context
13532 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
13533 while Present (Item) loop
13534 if Nkind (Item) = N_With_Clause then
13535 Scop := Entity (Name (Item));
13536 T2 := First_Entity (Scop);
13537 while Present (T2) loop
13538 if Is_Fixed_Point_Type (T2)
13539 and then Scope (Base_Type (T2)) = Scop
13540 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
13541 then
13542 if Present (T1) then
13543 Fixed_Point_Error (T1, T2);
13544 return Any_Type;
13545 else
13546 T1 := T2;
13547 end if;
13548 end if;
13550 Next_Entity (T2);
13551 end loop;
13552 end if;
13554 Next (Item);
13555 end loop;
13557 if Nkind (N) = N_Real_Literal then
13558 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
13560 else
13561 -- When the context is a type conversion, issue the warning on the
13562 -- expression of the conversion because it is the actual operation.
13564 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
13565 ErrN := Expression (N);
13566 else
13567 ErrN := N;
13568 end if;
13570 Error_Msg_NE
13571 ("??universal_fixed expression interpreted as }!", ErrN, T1);
13572 end if;
13574 return T1;
13575 end Unique_Fixed_Point_Type;
13577 ----------------------
13578 -- Valid_Conversion --
13579 ----------------------
13581 function Valid_Conversion
13582 (N : Node_Id;
13583 Target : Entity_Id;
13584 Operand : Node_Id;
13585 Report_Errs : Boolean := True) return Boolean
13587 Target_Type : constant Entity_Id := Base_Type (Target);
13588 Opnd_Type : Entity_Id := Etype (Operand);
13589 Inc_Ancestor : Entity_Id;
13591 function Conversion_Check
13592 (Valid : Boolean;
13593 Msg : String) return Boolean;
13594 -- Little routine to post Msg if Valid is False, returns Valid value
13596 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
13597 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
13599 procedure Conversion_Error_NE
13600 (Msg : String;
13601 N : Node_Or_Entity_Id;
13602 E : Node_Or_Entity_Id);
13603 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
13605 function In_Instance_Code return Boolean;
13606 -- Return True if expression is within an instance but is not in one of
13607 -- the actuals of the instantiation. Type conversions within an instance
13608 -- are not rechecked because type visibility may lead to spurious errors
13609 -- but conversions in an actual for a formal object must be checked.
13611 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13612 (Expr : Node_Id) return Boolean;
13613 -- Implicit anonymous-to-named access type conversions are not allowed
13614 -- if the "statically deeper than" relationship does not apply to the
13615 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
13616 -- We deal with most such cases elsewhere so that we can emit more
13617 -- specific error messages (e.g., if the operand is an access parameter
13618 -- or a saooaaat (stand-alone object of an anonymous access type)), but
13619 -- here is where we catch the case where the operand is an access
13620 -- discriminant selected from a dereference of another such "bad"
13621 -- conversion argument.
13623 function Valid_Tagged_Conversion
13624 (Target_Type : Entity_Id;
13625 Opnd_Type : Entity_Id) return Boolean;
13626 -- Specifically test for validity of tagged conversions
13628 function Valid_Array_Conversion return Boolean;
13629 -- Check index and component conformance, and accessibility levels if
13630 -- the component types are anonymous access types (Ada 2005).
13632 ----------------------
13633 -- Conversion_Check --
13634 ----------------------
13636 function Conversion_Check
13637 (Valid : Boolean;
13638 Msg : String) return Boolean
13640 begin
13641 if not Valid
13643 -- A generic unit has already been analyzed and we have verified
13644 -- that a particular conversion is OK in that context. Since the
13645 -- instance is reanalyzed without relying on the relationships
13646 -- established during the analysis of the generic, it is possible
13647 -- to end up with inconsistent views of private types. Do not emit
13648 -- the error message in such cases. The rest of the machinery in
13649 -- Valid_Conversion still ensures the proper compatibility of
13650 -- target and operand types.
13652 and then not In_Instance_Code
13653 then
13654 Conversion_Error_N (Msg, Operand);
13655 end if;
13657 return Valid;
13658 end Conversion_Check;
13660 ------------------------
13661 -- Conversion_Error_N --
13662 ------------------------
13664 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
13665 begin
13666 if Report_Errs then
13667 Error_Msg_N (Msg, N);
13668 end if;
13669 end Conversion_Error_N;
13671 -------------------------
13672 -- Conversion_Error_NE --
13673 -------------------------
13675 procedure Conversion_Error_NE
13676 (Msg : String;
13677 N : Node_Or_Entity_Id;
13678 E : Node_Or_Entity_Id)
13680 begin
13681 if Report_Errs then
13682 Error_Msg_NE (Msg, N, E);
13683 end if;
13684 end Conversion_Error_NE;
13686 ----------------------
13687 -- In_Instance_Code --
13688 ----------------------
13690 function In_Instance_Code return Boolean is
13691 Par : Node_Id;
13693 begin
13694 if not In_Instance then
13695 return False;
13697 else
13698 Par := Parent (N);
13699 while Present (Par) loop
13701 -- The expression is part of an actual object if it appears in
13702 -- the generated object declaration in the instance.
13704 if Nkind (Par) = N_Object_Declaration
13705 and then Present (Corresponding_Generic_Association (Par))
13706 then
13707 return False;
13709 else
13710 exit when
13711 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13712 or else Nkind (Par) in N_Subprogram_Call
13713 or else Nkind (Par) in N_Declaration;
13714 end if;
13716 Par := Parent (Par);
13717 end loop;
13719 -- Otherwise the expression appears within the instantiated unit
13721 return True;
13722 end if;
13723 end In_Instance_Code;
13725 --------------------------------------------------
13726 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13727 --------------------------------------------------
13729 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13730 (Expr : Node_Id) return Boolean
13732 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13733 pragma Assert (Is_Access_Type (Exp_Type));
13735 Associated_Node : Node_Id;
13736 Deref_Prefix : Node_Id;
13737 begin
13738 if not Is_Anonymous_Access_Type (Exp_Type) then
13739 return False;
13740 end if;
13742 pragma Assert (Is_Itype (Exp_Type));
13743 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13745 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13746 return False; -- not the type of an access discriminant
13747 end if;
13749 -- return False if Expr not of form <prefix>.all.Some_Component
13751 if Nkind (Expr) /= N_Selected_Component
13752 or else Nkind (Prefix (Expr)) /= N_Explicit_Dereference
13753 then
13754 -- conditional expressions, declare expressions ???
13755 return False;
13756 end if;
13758 Deref_Prefix := Prefix (Prefix (Expr));
13759 Exp_Type := Base_Type (Etype (Deref_Prefix));
13761 -- The "statically deeper relationship" does not apply
13762 -- to generic formal access types, so a prefix of such
13763 -- a type is a "bad" prefix.
13765 if Is_Generic_Formal (Exp_Type) then
13766 return True;
13768 -- The "statically deeper relationship" does apply to
13769 -- any other named access type.
13771 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13772 return False;
13773 end if;
13775 pragma Assert (Is_Itype (Exp_Type));
13776 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13778 -- The "statically deeper relationship" applies to some
13779 -- anonymous access types and not to others. Return
13780 -- True for the cases where it does not apply. Also check
13781 -- recursively for the
13782 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13783 -- where the correct result depends on <prefix>.
13785 return Nkind (Associated_Node) in
13786 N_Procedure_Specification | -- access parameter
13787 N_Function_Specification | -- access parameter
13788 N_Object_Declaration -- saooaaat
13789 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13790 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13792 ----------------------------
13793 -- Valid_Array_Conversion --
13794 ----------------------------
13796 function Valid_Array_Conversion return Boolean is
13797 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13798 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13800 Opnd_Index : Node_Id;
13801 Opnd_Index_Type : Entity_Id;
13803 Target_Comp_Type : constant Entity_Id :=
13804 Component_Type (Target_Type);
13805 Target_Comp_Base : constant Entity_Id :=
13806 Base_Type (Target_Comp_Type);
13808 Target_Index : Node_Id;
13809 Target_Index_Type : Entity_Id;
13811 begin
13812 -- Error if wrong number of dimensions
13815 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13816 then
13817 Conversion_Error_N
13818 ("incompatible number of dimensions for conversion", Operand);
13819 return False;
13821 -- Number of dimensions matches
13823 else
13824 -- Loop through indexes of the two arrays
13826 Target_Index := First_Index (Target_Type);
13827 Opnd_Index := First_Index (Opnd_Type);
13828 while Present (Target_Index) and then Present (Opnd_Index) loop
13829 Target_Index_Type := Etype (Target_Index);
13830 Opnd_Index_Type := Etype (Opnd_Index);
13832 -- Error if index types are incompatible
13834 if not (Is_Integer_Type (Target_Index_Type)
13835 and then Is_Integer_Type (Opnd_Index_Type))
13836 and then Root_Type (Target_Index_Type)
13837 /= Root_Type (Opnd_Index_Type)
13838 then
13839 Conversion_Error_N
13840 ("incompatible index types for array conversion",
13841 Operand);
13842 return False;
13843 end if;
13845 Next_Index (Target_Index);
13846 Next_Index (Opnd_Index);
13847 end loop;
13849 -- If component types have same base type, all set
13851 if Target_Comp_Base = Opnd_Comp_Base then
13852 null;
13854 -- Here if base types of components are not the same. The only
13855 -- time this is allowed is if we have anonymous access types.
13857 -- The conversion of arrays of anonymous access types can lead
13858 -- to dangling pointers. AI-392 formalizes the accessibility
13859 -- checks that must be applied to such conversions to prevent
13860 -- out-of-scope references.
13862 elsif Ekind (Target_Comp_Base) in
13863 E_Anonymous_Access_Type
13864 | E_Anonymous_Access_Subprogram_Type
13865 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13866 and then
13867 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13868 then
13869 if Type_Access_Level (Target_Type) <
13870 Deepest_Type_Access_Level (Opnd_Type)
13871 then
13872 if In_Instance_Body then
13873 Error_Msg_Warn := SPARK_Mode /= On;
13874 Conversion_Error_N
13875 ("source array type has deeper accessibility "
13876 & "level than target<<", Operand);
13877 Conversion_Error_N ("\Program_Error [<<", Operand);
13878 Rewrite (N,
13879 Make_Raise_Program_Error (Sloc (N),
13880 Reason => PE_Accessibility_Check_Failed));
13881 Set_Etype (N, Target_Type);
13882 return False;
13884 -- Conversion not allowed because of accessibility levels
13886 else
13887 Conversion_Error_N
13888 ("source array type has deeper accessibility "
13889 & "level than target", Operand);
13890 return False;
13891 end if;
13893 else
13894 null;
13895 end if;
13897 -- All other cases where component base types do not match
13899 else
13900 Conversion_Error_N
13901 ("incompatible component types for array conversion",
13902 Operand);
13903 return False;
13904 end if;
13906 -- Check that component subtypes statically match. For numeric
13907 -- types this means that both must be either constrained or
13908 -- unconstrained. For enumeration types the bounds must match.
13909 -- All of this is checked in Subtypes_Statically_Match.
13911 if not Subtypes_Statically_Match
13912 (Target_Comp_Type, Opnd_Comp_Type)
13913 then
13914 Conversion_Error_N
13915 ("component subtypes must statically match", Operand);
13916 return False;
13917 end if;
13918 end if;
13920 return True;
13921 end Valid_Array_Conversion;
13923 -----------------------------
13924 -- Valid_Tagged_Conversion --
13925 -----------------------------
13927 function Valid_Tagged_Conversion
13928 (Target_Type : Entity_Id;
13929 Opnd_Type : Entity_Id) return Boolean
13931 begin
13932 -- Upward conversions are allowed (RM 4.6(22))
13934 if Covers (Target_Type, Opnd_Type)
13935 or else Is_Ancestor (Target_Type, Opnd_Type)
13936 then
13937 return True;
13939 -- Downward conversion are allowed if the operand is class-wide
13940 -- (RM 4.6(23)).
13942 elsif Is_Class_Wide_Type (Opnd_Type)
13943 and then Covers (Opnd_Type, Target_Type)
13944 then
13945 return True;
13947 elsif Covers (Opnd_Type, Target_Type)
13948 or else Is_Ancestor (Opnd_Type, Target_Type)
13949 then
13950 return
13951 Conversion_Check (False,
13952 "downward conversion of tagged objects not allowed");
13954 -- Ada 2005 (AI-251): A conversion is valid if the operand and target
13955 -- types are both class-wide types and the specific type associated
13956 -- with at least one of them is an interface type (RM 4.6 (23.1/2));
13957 -- at run-time a check will verify the validity of this interface
13958 -- type conversion.
13960 elsif Is_Class_Wide_Type (Target_Type)
13961 and then Is_Class_Wide_Type (Opnd_Type)
13962 and then (Is_Interface (Target_Type)
13963 or else Is_Interface (Opnd_Type))
13964 then
13965 return True;
13967 -- Report errors
13969 elsif Is_Class_Wide_Type (Target_Type)
13970 and then Is_Interface (Target_Type)
13971 and then not Is_Interface (Opnd_Type)
13972 and then not Interface_Present_In_Ancestor
13973 (Typ => Opnd_Type,
13974 Iface => Target_Type)
13975 then
13976 Error_Msg_Name_1 := Chars (Etype (Target_Type));
13977 Error_Msg_Name_2 := Chars (Opnd_Type);
13978 Conversion_Error_N
13979 ("wrong interface conversion (% is not a progenitor "
13980 & "of %)", N);
13981 return False;
13983 elsif Is_Class_Wide_Type (Opnd_Type)
13984 and then Is_Interface (Opnd_Type)
13985 and then not Is_Interface (Target_Type)
13986 and then not Interface_Present_In_Ancestor
13987 (Typ => Target_Type,
13988 Iface => Opnd_Type)
13989 then
13990 Error_Msg_Name_1 := Chars (Etype (Opnd_Type));
13991 Error_Msg_Name_2 := Chars (Target_Type);
13992 Conversion_Error_N
13993 ("wrong interface conversion (% is not a progenitor "
13994 & "of %)", N);
13996 -- Search for interface types shared between the target type and
13997 -- the operand interface type to complete the text of the error
13998 -- since the source of this error is a missing type conversion
13999 -- to such interface type.
14001 if Has_Interfaces (Target_Type) then
14002 declare
14003 Operand_Ifaces_List : Elist_Id;
14004 Operand_Iface_Elmt : Elmt_Id;
14005 Target_Ifaces_List : Elist_Id;
14006 Target_Iface_Elmt : Elmt_Id;
14007 First_Candidate : Boolean := True;
14009 begin
14010 Collect_Interfaces (Base_Type (Target_Type),
14011 Target_Ifaces_List);
14012 Collect_Interfaces (Root_Type (Base_Type (Opnd_Type)),
14013 Operand_Ifaces_List);
14015 Operand_Iface_Elmt := First_Elmt (Operand_Ifaces_List);
14016 while Present (Operand_Iface_Elmt) loop
14017 Target_Iface_Elmt := First_Elmt (Target_Ifaces_List);
14018 while Present (Target_Iface_Elmt) loop
14019 if Node (Operand_Iface_Elmt)
14020 = Node (Target_Iface_Elmt)
14021 then
14022 Error_Msg_Name_1 :=
14023 Chars (Node (Target_Iface_Elmt));
14025 if First_Candidate then
14026 First_Candidate := False;
14027 Conversion_Error_N
14028 ("\must convert to `%''Class` before downward "
14029 & "conversion", Operand);
14030 else
14031 Conversion_Error_N
14032 ("\or must convert to `%''Class` before "
14033 & "downward conversion", Operand);
14034 end if;
14035 end if;
14037 Next_Elmt (Target_Iface_Elmt);
14038 end loop;
14040 Next_Elmt (Operand_Iface_Elmt);
14041 end loop;
14042 end;
14043 end if;
14045 return False;
14047 elsif not Is_Class_Wide_Type (Target_Type)
14048 and then Is_Interface (Target_Type)
14049 then
14050 Conversion_Error_N
14051 ("wrong use of interface type in tagged conversion", N);
14052 Conversion_Error_N
14053 ("\add ''Class to the target interface type", N);
14054 return False;
14056 elsif not Is_Class_Wide_Type (Opnd_Type)
14057 and then Is_Interface (Opnd_Type)
14058 then
14059 Conversion_Error_N
14060 ("must convert to class-wide interface type before downward "
14061 & "conversion", Operand);
14062 return False;
14064 else
14065 Conversion_Error_NE
14066 ("invalid tagged conversion, not compatible with}",
14067 N, First_Subtype (Opnd_Type));
14068 return False;
14069 end if;
14070 end Valid_Tagged_Conversion;
14072 -- Start of processing for Valid_Conversion
14074 begin
14075 Check_Parameterless_Call (Operand);
14077 if Is_Overloaded (Operand) then
14078 declare
14079 I : Interp_Index;
14080 I1 : Interp_Index;
14081 It : Interp;
14082 It1 : Interp;
14083 N1 : Entity_Id;
14084 T1 : Entity_Id;
14086 begin
14087 -- Remove procedure calls, which syntactically cannot appear in
14088 -- this context, but which cannot be removed by type checking,
14089 -- because the context does not impose a type.
14091 -- The node may be labelled overloaded, but still contain only one
14092 -- interpretation because others were discarded earlier. If this
14093 -- is the case, retain the single interpretation if legal.
14095 Get_First_Interp (Operand, I, It);
14096 Opnd_Type := It.Typ;
14097 Get_Next_Interp (I, It);
14099 if Present (It.Typ)
14100 and then Opnd_Type /= Standard_Void_Type
14101 then
14102 -- More than one candidate interpretation is available
14104 Get_First_Interp (Operand, I, It);
14105 while Present (It.Typ) loop
14106 if It.Typ = Standard_Void_Type then
14107 Remove_Interp (I);
14108 end if;
14110 -- When compiling for a system where Address is of a visible
14111 -- integer type, spurious ambiguities can be produced when
14112 -- arithmetic operations have a literal operand and return
14113 -- System.Address or a descendant of it. These ambiguities
14114 -- are usually resolved by the context, but for conversions
14115 -- there is no context type and the removal of the spurious
14116 -- operations must be done explicitly here.
14118 if not Address_Is_Private
14119 and then Is_Descendant_Of_Address (It.Typ)
14120 then
14121 Remove_Interp (I);
14122 end if;
14124 Get_Next_Interp (I, It);
14125 end loop;
14126 end if;
14128 Get_First_Interp (Operand, I, It);
14129 I1 := I;
14130 It1 := It;
14132 if No (It.Typ) then
14133 Conversion_Error_N ("illegal operand in conversion", Operand);
14134 return False;
14135 end if;
14137 Get_Next_Interp (I, It);
14139 if Present (It.Typ) then
14140 N1 := It1.Nam;
14141 T1 := It1.Typ;
14142 It1 := Disambiguate (Operand, I1, I, Any_Type);
14144 if It1 = No_Interp then
14145 Conversion_Error_N
14146 ("ambiguous operand in conversion", Operand);
14148 -- If the interpretation involves a standard operator, use
14149 -- the location of the type, which may be user-defined.
14151 if Sloc (It.Nam) = Standard_Location then
14152 Error_Msg_Sloc := Sloc (It.Typ);
14153 else
14154 Error_Msg_Sloc := Sloc (It.Nam);
14155 end if;
14157 Conversion_Error_N -- CODEFIX
14158 ("\\possible interpretation#!", Operand);
14160 if Sloc (N1) = Standard_Location then
14161 Error_Msg_Sloc := Sloc (T1);
14162 else
14163 Error_Msg_Sloc := Sloc (N1);
14164 end if;
14166 Conversion_Error_N -- CODEFIX
14167 ("\\possible interpretation#!", Operand);
14169 return False;
14170 end if;
14171 end if;
14173 Set_Etype (Operand, It1.Typ);
14174 Opnd_Type := It1.Typ;
14175 end;
14176 end if;
14178 -- Deal with conversion of integer type to address if the pragma
14179 -- Allow_Integer_Address is in effect. We convert the conversion to
14180 -- an unchecked conversion in this case and we are all done.
14182 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
14183 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
14184 Analyze_And_Resolve (N, Target_Type);
14185 return True;
14186 end if;
14188 -- If we are within a child unit, check whether the type of the
14189 -- expression has an ancestor in a parent unit, in which case it
14190 -- belongs to its derivation class even if the ancestor is private.
14191 -- See RM 7.3.1 (5.2/3).
14193 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
14195 -- Numeric types
14197 if Is_Numeric_Type (Target_Type) then
14199 -- A universal fixed expression can be converted to any numeric type
14201 if Opnd_Type = Universal_Fixed then
14202 return True;
14204 -- Also no need to check when in an instance or inlined body, because
14205 -- the legality has been established when the template was analyzed.
14206 -- Furthermore, numeric conversions may occur where only a private
14207 -- view of the operand type is visible at the instantiation point.
14208 -- This results in a spurious error if we check that the operand type
14209 -- is a numeric type.
14211 -- Note: in a previous version of this unit, the following tests were
14212 -- applied only for generated code (Comes_From_Source set to False),
14213 -- but in fact the test is required for source code as well, since
14214 -- this situation can arise in source code.
14216 elsif In_Instance_Code or else In_Inlined_Body then
14217 return True;
14219 -- Otherwise we need the conversion check
14221 else
14222 return Conversion_Check
14223 (Is_Numeric_Type (Opnd_Type)
14224 or else
14225 (Present (Inc_Ancestor)
14226 and then Is_Numeric_Type (Inc_Ancestor)),
14227 "illegal operand for numeric conversion");
14228 end if;
14230 -- Array types
14232 elsif Is_Array_Type (Target_Type) then
14233 if not Is_Array_Type (Opnd_Type)
14234 or else Opnd_Type = Any_Composite
14235 or else Opnd_Type = Any_String
14236 then
14237 Conversion_Error_N
14238 ("illegal operand for array conversion", Operand);
14239 return False;
14241 else
14242 return Valid_Array_Conversion;
14243 end if;
14245 -- Ada 2005 (AI-251): Internally generated conversions of access to
14246 -- interface types added to force the displacement of the pointer to
14247 -- reference the corresponding dispatch table.
14249 elsif not Comes_From_Source (N)
14250 and then Is_Access_Type (Target_Type)
14251 and then Is_Interface (Designated_Type (Target_Type))
14252 then
14253 return True;
14255 -- Ada 2005 (AI-251): Anonymous access types where target references an
14256 -- interface type.
14258 elsif Is_Access_Type (Opnd_Type)
14259 and then Ekind (Target_Type) in
14260 E_General_Access_Type | E_Anonymous_Access_Type
14261 and then Is_Interface (Directly_Designated_Type (Target_Type))
14262 then
14263 -- Check the static accessibility rule of 4.6(17). Note that the
14264 -- check is not enforced when within an instance body, since the
14265 -- RM requires such cases to be caught at run time.
14267 -- If the operand is a rewriting of an allocator no check is needed
14268 -- because there are no accessibility issues.
14270 if Nkind (Original_Node (N)) = N_Allocator then
14271 null;
14273 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
14274 if Type_Access_Level (Opnd_Type) >
14275 Deepest_Type_Access_Level (Target_Type)
14276 then
14277 -- In an instance, this is a run-time check, but one we know
14278 -- will fail, so generate an appropriate warning. The raise
14279 -- will be generated by Expand_N_Type_Conversion.
14281 if In_Instance_Body then
14282 Error_Msg_Warn := SPARK_Mode /= On;
14283 Conversion_Error_N
14284 ("cannot convert local pointer to non-local access type<<",
14285 Operand);
14286 Conversion_Error_N ("\Program_Error [<<", Operand);
14288 else
14289 Conversion_Error_N
14290 ("cannot convert local pointer to non-local access type",
14291 Operand);
14292 return False;
14293 end if;
14295 -- Special accessibility checks are needed in the case of access
14296 -- discriminants declared for a limited type.
14298 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14299 and then not Is_Local_Anonymous_Access (Opnd_Type)
14300 then
14301 -- When the operand is a selected access discriminant the check
14302 -- needs to be made against the level of the object denoted by
14303 -- the prefix of the selected name (Accessibility_Level handles
14304 -- checking the prefix of the operand for this case).
14306 if Nkind (Operand) = N_Selected_Component
14307 and then Static_Accessibility_Level
14308 (Operand, Zero_On_Dynamic_Level)
14309 > Deepest_Type_Access_Level (Target_Type)
14310 then
14311 -- In an instance, this is a run-time check, but one we know
14312 -- will fail, so generate an appropriate warning. The raise
14313 -- will be generated by Expand_N_Type_Conversion.
14315 if In_Instance_Body then
14316 Error_Msg_Warn := SPARK_Mode /= On;
14317 Conversion_Error_N
14318 ("cannot convert access discriminant to non-local "
14319 & "access type<<", Operand);
14320 Conversion_Error_N ("\Program_Error [<<", Operand);
14322 -- Real error if not in instance body
14324 else
14325 Conversion_Error_N
14326 ("cannot convert access discriminant to non-local "
14327 & "access type", Operand);
14328 return False;
14329 end if;
14330 end if;
14332 -- The case of a reference to an access discriminant from
14333 -- within a limited type declaration (which will appear as
14334 -- a discriminal) is always illegal because the level of the
14335 -- discriminant is considered to be deeper than any (nameable)
14336 -- access type.
14338 if Is_Entity_Name (Operand)
14339 and then not Is_Local_Anonymous_Access (Opnd_Type)
14340 and then
14341 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14342 and then Present (Discriminal_Link (Entity (Operand)))
14343 then
14344 Conversion_Error_N
14345 ("discriminant has deeper accessibility level than target",
14346 Operand);
14347 return False;
14348 end if;
14349 end if;
14350 end if;
14352 return True;
14354 -- General and anonymous access types
14356 elsif Ekind (Target_Type) in
14357 E_General_Access_Type | E_Anonymous_Access_Type
14358 and then
14359 Conversion_Check
14360 (Is_Access_Type (Opnd_Type)
14361 and then
14362 Ekind (Opnd_Type) not in
14363 E_Access_Subprogram_Type |
14364 E_Access_Protected_Subprogram_Type,
14365 "must be an access-to-object type")
14366 then
14367 if Is_Access_Constant (Opnd_Type)
14368 and then not Is_Access_Constant (Target_Type)
14369 then
14370 Conversion_Error_N
14371 ("access-to-constant operand type not allowed", Operand);
14372 return False;
14373 end if;
14375 -- Check the static accessibility rule of 4.6(17). Note that the
14376 -- check is not enforced when within an instance body, since the RM
14377 -- requires such cases to be caught at run time.
14379 if Ekind (Target_Type) /= E_Anonymous_Access_Type
14380 or else Is_Local_Anonymous_Access (Target_Type)
14381 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
14382 N_Object_Declaration
14383 then
14384 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
14385 -- conversions from an anonymous access type to a named general
14386 -- access type. Such conversions are not allowed in the case of
14387 -- access parameters and stand-alone objects of an anonymous
14388 -- access type. The implicit conversion case is recognized by
14389 -- testing that Comes_From_Source is False and that it's been
14390 -- rewritten. The Comes_From_Source test isn't sufficient because
14391 -- nodes in inlined calls to predefined library routines can have
14392 -- Comes_From_Source set to False. (Is there a better way to test
14393 -- for implicit conversions???).
14395 -- Do not treat a rewritten 'Old attribute reference like other
14396 -- rewrite substitutions. This makes a difference, for example,
14397 -- in the case where we are generating the expansion of a
14398 -- membership test of the form
14399 -- Saooaaat'Old in Named_Access_Type
14400 -- because in this case Valid_Conversion needs to return True
14401 -- (otherwise the expansion will be False - see the call site
14402 -- in exp_ch4.adb).
14404 if Ada_Version >= Ada_2012
14405 and then not Comes_From_Source (N)
14406 and then Is_Rewrite_Substitution (N)
14407 and then not Is_Attribute_Old (Original_Node (N))
14408 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
14409 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
14410 then
14411 if Is_Itype (Opnd_Type) then
14413 -- When applying restriction No_Dynamic_Accessibility_Check,
14414 -- implicit conversions are allowed when the operand type is
14415 -- not deeper than the target type.
14417 if No_Dynamic_Accessibility_Checks_Enabled (N) then
14418 if Type_Access_Level (Opnd_Type)
14419 > Deepest_Type_Access_Level (Target_Type)
14420 then
14421 Conversion_Error_N
14422 ("operand has deeper level than target", Operand);
14423 end if;
14425 -- Implicit conversions aren't allowed for objects of an
14426 -- anonymous access type, since such objects have nonstatic
14427 -- levels in Ada 2012.
14429 elsif Nkind (Associated_Node_For_Itype (Opnd_Type))
14430 = N_Object_Declaration
14431 then
14432 Conversion_Error_N
14433 ("implicit conversion of stand-alone anonymous "
14434 & "access object not allowed", Operand);
14435 return False;
14437 -- Implicit conversions aren't allowed for anonymous access
14438 -- parameters. We exclude anonymous access results as well
14439 -- as universal_access "=".
14441 elsif not Is_Local_Anonymous_Access (Opnd_Type)
14442 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
14443 N_Function_Specification |
14444 N_Procedure_Specification
14445 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
14446 then
14447 Conversion_Error_N
14448 ("implicit conversion of anonymous access parameter "
14449 & "not allowed", Operand);
14450 return False;
14452 -- Detect access discriminant values that are illegal
14453 -- implicit anonymous-to-named access conversion operands.
14455 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
14456 then
14457 Conversion_Error_N
14458 ("implicit conversion of anonymous access value "
14459 & "not allowed", Operand);
14460 return False;
14462 -- In other cases, the level of the operand's type must be
14463 -- statically less deep than that of the target type, else
14464 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
14466 elsif Type_Access_Level (Opnd_Type) >
14467 Deepest_Type_Access_Level (Target_Type)
14468 then
14469 Conversion_Error_N
14470 ("implicit conversion of anonymous access value "
14471 & "violates accessibility", Operand);
14472 return False;
14473 end if;
14474 end if;
14476 -- Check if the operand is deeper than the target type, taking
14477 -- care to avoid the case where we are converting a result of a
14478 -- function returning an anonymous access type since the "master
14479 -- of the call" would be target type of the conversion unless
14480 -- the target type is anonymous access as well - see RM 3.10.2
14481 -- (10.3/3).
14483 -- Note that when the restriction No_Dynamic_Accessibility_Checks
14484 -- is in effect wei also want to proceed with the conversion check
14485 -- described above.
14487 elsif Type_Access_Level (Opnd_Type, Assoc_Ent => Operand)
14488 > Deepest_Type_Access_Level (Target_Type)
14489 and then (Nkind (Associated_Node_For_Itype (Opnd_Type))
14490 /= N_Function_Specification
14491 or else Ekind (Target_Type) in Anonymous_Access_Kind
14492 or else No_Dynamic_Accessibility_Checks_Enabled (N))
14494 -- Check we are not in a return value ???
14496 and then (not In_Return_Value (N)
14497 or else
14498 Nkind (Associated_Node_For_Itype (Target_Type))
14499 = N_Component_Declaration)
14500 then
14501 -- In an instance, this is a run-time check, but one we know
14502 -- will fail, so generate an appropriate warning. The raise
14503 -- will be generated by Expand_N_Type_Conversion.
14505 if In_Instance_Body then
14506 Error_Msg_Warn := SPARK_Mode /= On;
14507 Conversion_Error_N
14508 ("cannot convert local pointer to non-local access type<<",
14509 Operand);
14510 Conversion_Error_N ("\Program_Error [<<", Operand);
14512 -- If not in an instance body, this is a real error
14514 else
14515 -- Avoid generation of spurious error message
14517 if not Error_Posted (N) then
14518 Conversion_Error_N
14519 ("cannot convert local pointer to non-local access type",
14520 Operand);
14521 end if;
14523 return False;
14524 end if;
14526 -- Special accessibility checks are needed in the case of access
14527 -- discriminants declared for a limited type.
14529 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14530 and then not Is_Local_Anonymous_Access (Opnd_Type)
14531 then
14532 -- When the operand is a selected access discriminant the check
14533 -- needs to be made against the level of the object denoted by
14534 -- the prefix of the selected name (Accessibility_Level handles
14535 -- checking the prefix of the operand for this case).
14537 if Nkind (Operand) = N_Selected_Component
14538 and then Static_Accessibility_Level
14539 (Operand, Zero_On_Dynamic_Level)
14540 > Deepest_Type_Access_Level (Target_Type)
14541 then
14542 -- In an instance, this is a run-time check, but one we know
14543 -- will fail, so generate an appropriate warning. The raise
14544 -- will be generated by Expand_N_Type_Conversion.
14546 if In_Instance_Body then
14547 Error_Msg_Warn := SPARK_Mode /= On;
14548 Conversion_Error_N
14549 ("cannot convert access discriminant to non-local "
14550 & "access type<<", Operand);
14551 Conversion_Error_N ("\Program_Error [<<", Operand);
14553 -- If not in an instance body, this is a real error
14555 else
14556 Conversion_Error_N
14557 ("cannot convert access discriminant to non-local "
14558 & "access type", Operand);
14559 return False;
14560 end if;
14561 end if;
14563 -- The case of a reference to an access discriminant from
14564 -- within a limited type declaration (which will appear as
14565 -- a discriminal) is always illegal because the level of the
14566 -- discriminant is considered to be deeper than any (nameable)
14567 -- access type.
14569 if Is_Entity_Name (Operand)
14570 and then
14571 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14572 and then Present (Discriminal_Link (Entity (Operand)))
14573 then
14574 Conversion_Error_N
14575 ("discriminant has deeper accessibility level than target",
14576 Operand);
14577 return False;
14578 end if;
14579 end if;
14580 end if;
14582 -- In the presence of limited_with clauses we have to use nonlimited
14583 -- views, if available.
14585 Check_Limited : declare
14586 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
14587 -- Helper function to handle limited views
14589 --------------------------
14590 -- Full_Designated_Type --
14591 --------------------------
14593 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
14594 Desig : constant Entity_Id := Designated_Type (T);
14596 begin
14597 -- Handle the limited view of a type
14599 if From_Limited_With (Desig)
14600 and then Has_Non_Limited_View (Desig)
14601 then
14602 return Available_View (Desig);
14603 else
14604 return Desig;
14605 end if;
14606 end Full_Designated_Type;
14608 -- Local Declarations
14610 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
14611 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
14613 Same_Base : constant Boolean :=
14614 Base_Type (Target) = Base_Type (Opnd);
14616 -- Start of processing for Check_Limited
14618 begin
14619 if Is_Tagged_Type (Target) then
14620 return Valid_Tagged_Conversion (Target, Opnd);
14622 else
14623 if not Same_Base then
14624 Conversion_Error_NE
14625 ("target designated type not compatible with }",
14626 N, Base_Type (Opnd));
14627 return False;
14629 -- Ada 2005 AI-384: legality rule is symmetric in both
14630 -- designated types. The conversion is legal (with possible
14631 -- constraint check) if either designated type is
14632 -- unconstrained.
14634 elsif Subtypes_Statically_Match (Target, Opnd)
14635 or else
14636 (Has_Discriminants (Target)
14637 and then
14638 (not Is_Constrained (Opnd)
14639 or else not Is_Constrained (Target)))
14640 then
14641 -- Special case, if Value_Size has been used to make the
14642 -- sizes different, the conversion is not allowed even
14643 -- though the subtypes statically match.
14645 if Known_Static_RM_Size (Target)
14646 and then Known_Static_RM_Size (Opnd)
14647 and then RM_Size (Target) /= RM_Size (Opnd)
14648 then
14649 Conversion_Error_NE
14650 ("target designated subtype not compatible with }",
14651 N, Opnd);
14652 Conversion_Error_NE
14653 ("\because sizes of the two designated subtypes differ",
14654 N, Opnd);
14655 return False;
14657 -- Normal case where conversion is allowed
14659 else
14660 return True;
14661 end if;
14663 else
14664 Error_Msg_NE
14665 ("target designated subtype not compatible with }",
14666 N, Opnd);
14667 return False;
14668 end if;
14669 end if;
14670 end Check_Limited;
14672 -- Access to subprogram types. If the operand is an access parameter,
14673 -- the type has a deeper accessibility that any master, and cannot be
14674 -- assigned. We must make an exception if the conversion is part of an
14675 -- assignment and the target is the return object of an extended return
14676 -- statement, because in that case the accessibility check takes place
14677 -- after the return.
14679 elsif Is_Access_Subprogram_Type (Target_Type)
14681 -- Note: this test of Opnd_Type is there to prevent entering this
14682 -- branch in the case of a remote access to subprogram type, which
14683 -- is internally represented as an E_Record_Type.
14685 and then Is_Access_Type (Opnd_Type)
14686 then
14687 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
14688 and then Is_Entity_Name (Operand)
14689 and then Ekind (Entity (Operand)) = E_In_Parameter
14690 and then
14691 (Nkind (Parent (N)) /= N_Assignment_Statement
14692 or else not Is_Entity_Name (Name (Parent (N)))
14693 or else not Is_Return_Object (Entity (Name (Parent (N)))))
14694 then
14695 Conversion_Error_N
14696 ("illegal attempt to store anonymous access to subprogram",
14697 Operand);
14698 Conversion_Error_N
14699 ("\value has deeper accessibility than any master "
14700 & "(RM 3.10.2 (13))",
14701 Operand);
14703 Error_Msg_NE
14704 ("\use named access type for& instead of access parameter",
14705 Operand, Entity (Operand));
14706 end if;
14708 -- Check that the designated types are subtype conformant
14710 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
14711 Old_Id => Designated_Type (Opnd_Type),
14712 Err_Loc => N);
14714 -- Check the static accessibility rule of 4.6(20)
14716 if Type_Access_Level (Opnd_Type) >
14717 Deepest_Type_Access_Level (Target_Type)
14718 then
14719 Conversion_Error_N
14720 ("operand type has deeper accessibility level than target",
14721 Operand);
14723 -- Check that if the operand type is declared in a generic body,
14724 -- then the target type must be declared within that same body
14725 -- (enforces last sentence of 4.6(20)).
14727 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
14728 declare
14729 O_Gen : constant Node_Id :=
14730 Enclosing_Generic_Body (Opnd_Type);
14732 T_Gen : Node_Id;
14734 begin
14735 T_Gen := Enclosing_Generic_Body (Target_Type);
14736 while Present (T_Gen) and then T_Gen /= O_Gen loop
14737 T_Gen := Enclosing_Generic_Body (T_Gen);
14738 end loop;
14740 if T_Gen /= O_Gen then
14741 Conversion_Error_N
14742 ("target type must be declared in same generic body "
14743 & "as operand type", N);
14744 end if;
14745 end;
14746 end if;
14748 -- Check that the strub modes are compatible.
14749 -- We wish to reject explicit conversions only for
14750 -- incompatible modes.
14752 return Conversion_Check
14753 (Compatible_Strub_Modes
14754 (Designated_Type (Target_Type),
14755 Designated_Type (Opnd_Type)),
14756 "incompatible `strub` modes");
14758 -- Remote access to subprogram types
14760 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
14761 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
14762 then
14763 -- It is valid to convert from one RAS type to another provided
14764 -- that their specification statically match.
14766 -- Note: at this point, remote access to subprogram types have been
14767 -- expanded to their E_Record_Type representation, and we need to
14768 -- go back to the original access type definition using the
14769 -- Corresponding_Remote_Type attribute in order to check that the
14770 -- designated profiles match.
14772 pragma Assert (Ekind (Target_Type) = E_Record_Type);
14773 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
14775 Check_Subtype_Conformant
14776 (New_Id =>
14777 Designated_Type (Corresponding_Remote_Type (Target_Type)),
14778 Old_Id =>
14779 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14780 Err_Loc =>
14783 -- Check that the strub modes are compatible.
14784 -- We wish to reject explicit conversions only for
14785 -- incompatible modes.
14787 return Conversion_Check
14788 (Compatible_Strub_Modes
14789 (Designated_Type (Target_Type),
14790 Designated_Type (Opnd_Type)),
14791 "incompatible `strub` modes");
14793 -- If it was legal in the generic, it's legal in the instance
14795 elsif In_Instance_Body then
14796 return True;
14798 -- If both are tagged types, check legality of view conversions
14800 elsif Is_Tagged_Type (Target_Type)
14801 and then
14802 Is_Tagged_Type (Opnd_Type)
14803 then
14804 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14806 -- Types derived from the same root type are convertible
14808 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14809 return True;
14811 -- In an instance or an inlined body, there may be inconsistent views of
14812 -- the same type, or of types derived from a common root.
14814 elsif (In_Instance or In_Inlined_Body)
14815 and then
14816 Root_Type (Underlying_Type (Target_Type)) =
14817 Root_Type (Underlying_Type (Opnd_Type))
14818 then
14819 return True;
14821 -- Special check for common access type error case
14823 elsif Ekind (Target_Type) = E_Access_Type
14824 and then Is_Access_Type (Opnd_Type)
14825 then
14826 Conversion_Error_N ("target type must be general access type!", N);
14827 Conversion_Error_NE -- CODEFIX
14828 ("\add ALL to }!", N, Target_Type);
14829 return False;
14831 -- Here we have a real conversion error
14833 else
14834 -- Check for missing regular with_clause when only a limited view of
14835 -- target is available.
14837 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14838 Conversion_Error_NE
14839 ("invalid conversion, not compatible with limited view of }",
14840 N, Opnd_Type);
14841 Conversion_Error_NE
14842 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14844 elsif Is_Access_Type (Opnd_Type)
14845 and then From_Limited_With (Designated_Type (Opnd_Type))
14846 and then In_Package_Body
14847 then
14848 Conversion_Error_NE
14849 ("invalid conversion, not compatible with }", N, Opnd_Type);
14850 Conversion_Error_NE
14851 ("\add with_clause for& to current unit!",
14852 N, Scope (Designated_Type (Opnd_Type)));
14854 else
14855 Conversion_Error_NE
14856 ("invalid conversion, not compatible with }", N, Opnd_Type);
14857 end if;
14859 return False;
14860 end if;
14861 end Valid_Conversion;
14863 end Sem_Res;