testsuite: arm: Use effective-target for nomve_fp_1 test
[official-gcc.git] / gcc / ada / sem_res.adb
blob658f9eb2b7243676cf095e6e5cd400eac17d3717
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-2024, 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 Local_Restrict;
50 with Mutably_Tagged; use Mutably_Tagged;
51 with Namet; use Namet;
52 with Nmake; use Nmake;
53 with Nlists; use Nlists;
54 with Opt; use Opt;
55 with Output; use Output;
56 with Par_SCO; use Par_SCO;
57 with Restrict; use Restrict;
58 with Rident; use Rident;
59 with Rtsfind; use Rtsfind;
60 with Sem; use Sem;
61 with Sem_Aggr; use Sem_Aggr;
62 with Sem_Attr; use Sem_Attr;
63 with Sem_Aux; use Sem_Aux;
64 with Sem_Case; use Sem_Case;
65 with Sem_Cat; use Sem_Cat;
66 with Sem_Ch3; use Sem_Ch3;
67 with Sem_Ch4; use Sem_Ch4;
68 with Sem_Ch5; use Sem_Ch5;
69 with Sem_Ch6; use Sem_Ch6;
70 with Sem_Ch8; use Sem_Ch8;
71 with Sem_Ch13; use Sem_Ch13;
72 with Sem_Dim; use Sem_Dim;
73 with Sem_Disp; use Sem_Disp;
74 with Sem_Dist; use Sem_Dist;
75 with Sem_Elab; use Sem_Elab;
76 with Sem_Elim; use Sem_Elim;
77 with Sem_Eval; use Sem_Eval;
78 with Sem_Intr; use Sem_Intr;
79 with Sem_Mech; use Sem_Mech;
80 with Sem_Type; use Sem_Type;
81 with Sem_Util; use Sem_Util;
82 with Sem_Warn; use Sem_Warn;
83 with Sinfo; use Sinfo;
84 with Sinfo.Nodes; use Sinfo.Nodes;
85 with Sinfo.Utils; use Sinfo.Utils;
86 with Sinfo.CN; use Sinfo.CN;
87 with Sinput; use Sinput;
88 with Snames; use Snames;
89 with Stand; use Stand;
90 with Stringt; use Stringt;
91 with Strub; use Strub;
92 with Style; use Style;
93 with Targparm; use Targparm;
94 with Tbuild; use Tbuild;
95 with Uintp; use Uintp;
96 with Urealp; use Urealp;
97 with Warnsw; use Warnsw;
99 package body Sem_Res is
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
105 -- Second pass (top-down) type checking and overload resolution procedures
106 -- Typ is the type required by context. These procedures propagate the
107 -- type information recursively to the descendants of N. If the node is not
108 -- overloaded, its Etype is established in the first pass. If overloaded,
109 -- the Resolve routines set the correct type. For arithmetic operators, the
110 -- Etype is the base type of the context.
112 -- Note that Resolve_Attribute is separated off in Sem_Attr
114 function Has_Applicable_User_Defined_Literal
115 (N : Node_Id;
116 Typ : Entity_Id) return Boolean;
117 -- Check whether N is a literal or a named number, and whether Typ has a
118 -- user-defined literal aspect that may apply to N. In this case, replace
119 -- N with a call to the corresponding function and return True.
121 procedure Check_Discriminant_Use (N : Node_Id);
122 -- Enforce the restrictions on the use of discriminants when constraining
123 -- a component of a discriminated type (record or concurrent type).
125 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
126 -- Given a node for an operator associated with type T, check that the
127 -- operator is visible. Operators all of whose operands are universal must
128 -- be checked for visibility during resolution because their type is not
129 -- determinable based on their operands.
131 procedure Check_Fully_Declared_Prefix
132 (Typ : Entity_Id;
133 Pref : Node_Id);
134 -- Check that the type of the prefix of a dereference is not incomplete
136 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
137 -- Given a call node, Call, which is known to occur immediately within the
138 -- subprogram being called, determines whether it is a detectable case of
139 -- an infinite recursion, and if so, outputs appropriate messages. Returns
140 -- True if an infinite recursion is detected, and False otherwise.
142 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
143 -- N is the node for a logical operator. If the operator is predefined, and
144 -- the root type of the operands is Standard.Boolean, then a check is made
145 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
146 -- the style check for Style_Check_Boolean_And_Or.
148 function Is_Atomic_Non_VFA_Ref_With_Address (N : Node_Id) return Boolean;
149 -- N is either an indexed component or a selected component. Return true
150 -- if the prefix denotes an Atomic but not Volatile_Full_Access object that
151 -- has an address clause (the case in which we may want to give a warning).
153 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean;
154 -- Determine whether E is an access type declared by an access declaration,
155 -- and not an (anonymous) allocator type.
157 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
158 -- Utility to check whether the entity for an operator is a predefined
159 -- operator, in which case the expression is left as an operator in the
160 -- tree (else it is rewritten into a call). An instance of an intrinsic
161 -- conversion operation may be given an operator name, but is not treated
162 -- like an operator. Note that an operator that is an imported back-end
163 -- builtin has convention Intrinsic, but is expected to be rewritten into
164 -- a call, so such an operator is not treated as predefined by this
165 -- predicate.
167 function Original_Implementation_Base_Type
168 (Id : Entity_Id) return Entity_Id;
169 -- Like Implementation_Base_Type, but looks at Original_Node.
171 procedure Preanalyze_And_Resolve
172 (N : Node_Id;
173 T : Entity_Id;
174 With_Freezing : Boolean);
175 -- Subsidiary of public versions of Preanalyze_And_Resolve.
177 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
178 -- If a default expression in entry call N depends on the discriminants
179 -- of the task, it must be replaced with a reference to the discriminant
180 -- of the task being called.
182 procedure Resolve_Dependent_Expression
183 (N : Node_Id;
184 Expr : Node_Id;
185 Typ : Entity_Id);
186 -- Internal procedure to resolve the dependent expression Expr of the
187 -- conditional expression N with type Typ.
189 procedure Resolve_Op_Concat_Arg
190 (N : Node_Id;
191 Arg : Node_Id;
192 Typ : Entity_Id;
193 Is_Comp : Boolean);
194 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
195 -- concatenation operator. The operand is either of the array type or of
196 -- the component type. If the operand is an aggregate, and the component
197 -- type is composite, this is ambiguous if component type has aggregates.
199 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
200 -- Does the first part of the work of Resolve_Op_Concat
202 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
203 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
204 -- has been resolved. See Resolve_Op_Concat for details.
206 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
215 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
216 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
217 procedure Resolve_External_Initializer (N : Node_Id; Typ : Entity_Id);
218 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
219 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
220 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
221 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
222 procedure Resolve_Interpolated_String_Literal
223 (N : Node_Id;
224 Typ : Entity_Id);
225 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
226 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
227 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
228 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
229 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
230 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
231 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
232 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
233 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
234 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
235 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
236 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
237 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
238 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
239 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
240 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
241 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
242 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
243 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
244 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
245 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
246 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
248 function Operator_Kind
249 (Op_Name : Name_Id;
250 Is_Binary : Boolean) return Node_Kind;
251 -- Utility to map the name of an operator into the corresponding Node. Used
252 -- by other node rewriting procedures.
254 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
255 -- Resolve actuals of call, and add default expressions for missing ones.
256 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
257 -- called subprogram.
259 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
260 -- Called from Resolve_Call, when the prefix denotes an entry or element
261 -- of entry family. Actuals are resolved as for subprograms, and the node
262 -- is rebuilt as an entry call. Also called for protected operations. Typ
263 -- is the context type, which is used when the operation is a protected
264 -- function with no arguments, and the return value is indexed.
266 procedure Resolve_Implicit_Dereference (P : Node_Id);
267 -- Called when P is the prefix of an indexed component, or of a selected
268 -- component, or of a slice. If P is of an access type, we unconditionally
269 -- rewrite it as an explicit dereference. This ensures that the expander
270 -- and the code generator have a fully explicit tree to work with.
272 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
273 -- A call to a user-defined intrinsic operator is rewritten as a call to
274 -- the corresponding predefined operator, with suitable conversions. Note
275 -- that this applies only for intrinsic operators that denote predefined
276 -- operators, not ones that are intrinsic imports of back-end builtins.
278 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
279 -- Ditto, for arithmetic unary operators
281 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
282 -- If an operator node resolves to a call to a user-defined operator,
283 -- rewrite the node as a function call.
285 procedure Make_Call_Into_Operator
286 (N : Node_Id;
287 Typ : Entity_Id;
288 Op_Id : Entity_Id);
289 -- Inverse transformation: if an operator is given in functional notation,
290 -- then after resolving the node, transform into an operator node, so that
291 -- operands are resolved properly. Recall that predefined operators do not
292 -- have a full signature and special resolution rules apply.
294 procedure Rewrite_Renamed_Operator
295 (N : Node_Id;
296 Op : Entity_Id;
297 Typ : Entity_Id);
298 -- An operator can rename another, e.g. in an instantiation. In that
299 -- case, the proper operator node must be constructed and resolved.
301 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
302 -- The String_Literal_Subtype is built for all strings that are not
303 -- operands of a static concatenation operation. It is also built for
304 -- expressions generated by the expansion of the External_Initialization
305 -- aspect. If the argument is not an N_String_Literal node or an
306 -- N_External_Initializer node, then the call has no effect.
308 procedure Set_Slice_Subtype (N : Node_Id);
309 -- Build subtype of array type, with the range specified by the slice
311 procedure Simplify_Type_Conversion (N : Node_Id);
312 -- Called after N has been resolved and evaluated, but before range checks
313 -- have been applied. This rewrites the conversion into a simpler form.
315 function Try_User_Defined_Literal
316 (N : Node_Id;
317 Typ : Entity_Id) return Boolean;
318 -- If the node is a literal or a named number or a conditional expression
319 -- whose dependent expressions are all literals or named numbers, and the
320 -- context type has a user-defined literal aspect, then rewrite the node
321 -- or its leaf nodes as calls to the corresponding function, which plays
322 -- the role of an implicit conversion.
324 function Try_User_Defined_Literal_For_Operator
325 (N : Node_Id;
326 Typ : Entity_Id) return Boolean;
327 -- If an operator node has a literal operand, check whether the type of the
328 -- context, or that of the other operand has a user-defined literal aspect
329 -- that can be applied to the literal to resolve the node. If such aspect
330 -- exists, replace literal with a call to the corresponding function and
331 -- return True, return false otherwise.
333 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
334 -- A universal_fixed expression in an universal context is unambiguous if
335 -- there is only one applicable fixed point type. Determining whether there
336 -- is only one requires a search over all visible entities, and happens
337 -- only in very pathological cases (see 6115-006).
339 -------------------------
340 -- Ambiguous_Character --
341 -------------------------
343 procedure Ambiguous_Character (C : Node_Id) is
344 E : Entity_Id;
346 begin
347 if Nkind (C) = N_Character_Literal then
348 Error_Msg_N ("ambiguous character literal", C);
350 -- First the ones in Standard
352 Error_Msg_N ("\\possible interpretation: Character!", C);
353 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
355 -- Include Wide_Wide_Character in Ada 2005 mode
357 if Ada_Version >= Ada_2005 then
358 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
359 end if;
361 -- Now any other types that match
363 E := Current_Entity (C);
364 while Present (E) loop
365 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
366 E := Homonym (E);
367 end loop;
368 end if;
369 end Ambiguous_Character;
371 -------------------------
372 -- Analyze_And_Resolve --
373 -------------------------
375 procedure Analyze_And_Resolve (N : Node_Id) is
376 begin
377 Analyze (N);
378 Resolve (N);
379 end Analyze_And_Resolve;
381 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
382 begin
383 Analyze (N);
384 Resolve (N, Typ);
385 end Analyze_And_Resolve;
387 -- Versions with check(s) suppressed
389 procedure Analyze_And_Resolve
390 (N : Node_Id;
391 Typ : Entity_Id;
392 Suppress : Check_Id)
394 Scop : constant Entity_Id := Current_Scope;
396 begin
397 if Suppress = All_Checks then
398 declare
399 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
400 begin
401 Scope_Suppress.Suppress := (others => True);
402 Analyze_And_Resolve (N, Typ);
403 Scope_Suppress.Suppress := Sva;
404 end;
406 else
407 declare
408 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
409 begin
410 Scope_Suppress.Suppress (Suppress) := True;
411 Analyze_And_Resolve (N, Typ);
412 Scope_Suppress.Suppress (Suppress) := Svg;
413 end;
414 end if;
416 if Current_Scope /= Scop
417 and then Scope_Is_Transient
418 then
419 -- This can only happen if a transient scope was created for an inner
420 -- expression, which will be removed upon completion of the analysis
421 -- of an enclosing construct. The transient scope must have the
422 -- suppress status of the enclosing environment, not of this Analyze
423 -- call.
425 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
426 Scope_Suppress;
427 end if;
428 end Analyze_And_Resolve;
430 procedure Analyze_And_Resolve
431 (N : Node_Id;
432 Suppress : Check_Id)
434 Scop : constant Entity_Id := Current_Scope;
436 begin
437 if Suppress = All_Checks then
438 declare
439 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
440 begin
441 Scope_Suppress.Suppress := (others => True);
442 Analyze_And_Resolve (N);
443 Scope_Suppress.Suppress := Sva;
444 end;
446 else
447 declare
448 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
449 begin
450 Scope_Suppress.Suppress (Suppress) := True;
451 Analyze_And_Resolve (N);
452 Scope_Suppress.Suppress (Suppress) := Svg;
453 end;
454 end if;
456 if Current_Scope /= Scop and then Scope_Is_Transient then
457 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
458 Scope_Suppress;
459 end if;
460 end Analyze_And_Resolve;
462 -------------------------------------
463 -- Has_Applicable_User_Defined_Literal --
464 -------------------------------------
466 function Has_Applicable_User_Defined_Literal
467 (N : Node_Id;
468 Typ : Entity_Id) return Boolean
470 Loc : constant Source_Ptr := Sloc (N);
471 Literal_Aspect_Map :
472 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
473 (N_Integer_Literal => Aspect_Integer_Literal,
474 N_Interpolated_String_Literal => Aspect_String_Literal,
475 N_Real_Literal => Aspect_Real_Literal,
476 N_String_Literal => Aspect_String_Literal);
478 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
479 (E_Named_Integer => Aspect_Integer_Literal,
480 E_Named_Real => Aspect_Real_Literal);
482 Lit_Aspect : Aspect_Id;
484 Callee : Entity_Id;
485 Name : Node_Id;
486 Param1 : Node_Id;
487 Param2 : Node_Id;
488 Params : List_Id;
489 Call : Node_Id;
490 Expr : Node_Id;
492 begin
493 if (Nkind (N) in N_Numeric_Or_String_Literal
494 | N_Interpolated_String_Literal
495 and then Present
496 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
497 or else
498 (Nkind (N) = N_Identifier
499 and then Is_Named_Number (Entity (N))
500 and then
501 Present
502 (Find_Aspect
503 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
504 then
505 Lit_Aspect :=
506 (if Nkind (N) = N_Identifier
507 then Named_Number_Aspect_Map (Ekind (Entity (N)))
508 else Literal_Aspect_Map (Nkind (N)));
509 Callee :=
510 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
511 Name := Make_Identifier (Loc, Chars (Callee));
513 if Is_Derived_Type (Typ)
514 and then Base_Type (Etype (Callee)) /= Base_Type (Typ)
515 then
516 Callee :=
517 Corresponding_Primitive_Op
518 (Ancestor_Op => Callee,
519 Descendant_Type => Base_Type (Typ));
520 end if;
522 -- Handle an identifier that denotes a named number.
524 if Nkind (N) = N_Identifier then
525 Expr := Expression (Declaration_Node (Entity (N)));
527 if Ekind (Entity (N)) = E_Named_Integer then
528 UI_Image (Expr_Value (Expr), Decimal);
529 Start_String;
530 Store_String_Chars
531 (UI_Image_Buffer (1 .. UI_Image_Length));
532 Param1 := Make_String_Literal (Loc, End_String);
533 Params := New_List (Param1);
535 else
536 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
537 Start_String;
539 if UR_Is_Negative (Expr_Value_R (Expr)) then
540 Store_String_Chars ("-");
541 end if;
543 Store_String_Chars
544 (UI_Image_Buffer (1 .. UI_Image_Length));
545 Param1 := Make_String_Literal (Loc, End_String);
547 -- Note: Set_Etype is called below on Param1
549 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
550 Start_String;
551 Store_String_Chars
552 (UI_Image_Buffer (1 .. UI_Image_Length));
553 Param2 := Make_String_Literal (Loc, End_String);
554 Set_Etype (Param2, Standard_String);
556 Params := New_List (Param1, Param2);
558 if Present (Related_Expression (Callee)) then
559 Callee := Related_Expression (Callee);
560 else
561 Error_Msg_NE
562 ("cannot resolve & for a named real", N, Callee);
563 return False;
564 end if;
565 end if;
567 elsif Nkind (N) = N_String_Literal then
568 Param1 := Make_String_Literal (Loc, Strval (N));
569 Params := New_List (Param1);
571 elsif Nkind (N) = N_Interpolated_String_Literal then
572 Param1 := New_Copy_Tree (N);
573 Params := New_List (Param1);
575 else
576 Param1 :=
577 Make_String_Literal
578 (Loc, String_From_Numeric_Literal (N));
579 Params := New_List (Param1);
580 end if;
582 Call :=
583 Make_Function_Call
584 (Sloc => Loc,
585 Name => Name,
586 Parameter_Associations => Params);
588 Set_Entity (Name, Callee);
589 Set_Is_Overloaded (Name, False);
591 if Lit_Aspect = Aspect_String_Literal then
592 Set_Etype (Param1, Standard_Wide_Wide_String);
593 else
594 Set_Etype (Param1, Standard_String);
595 end if;
597 Set_Etype (Call, Etype (Callee));
599 -- Conversion not needed if the result type of the call is class-wide
600 -- or if the result type matches the context type.
602 if not Is_Class_Wide_Type (Typ)
603 and then Base_Type (Etype (Call)) /= Base_Type (Typ)
604 then
605 -- Conversion may be needed in case of an inherited
606 -- aspect of a derived type. For a null extension, we
607 -- use a null extension aggregate instead because the
608 -- downward type conversion would be illegal.
610 if Is_Null_Extension_Of
611 (Descendant => Typ,
612 Ancestor => Etype (Call))
613 then
614 Call := Make_Extension_Aggregate (Loc,
615 Ancestor_Part => Call,
616 Null_Record_Present => True);
617 else
618 Call := Convert_To (Typ, Call);
619 end if;
620 end if;
622 Rewrite (N, Call);
624 Analyze_And_Resolve (N, Typ);
625 return True;
627 else
628 return False;
629 end if;
630 end Has_Applicable_User_Defined_Literal;
632 ----------------------------
633 -- Check_Discriminant_Use --
634 ----------------------------
636 procedure Check_Discriminant_Use (N : Node_Id) is
637 PN : constant Node_Id := Parent (N);
638 Disc : constant Entity_Id := Entity (N);
639 P : Node_Id;
640 D : Node_Id;
642 begin
643 -- Any use in a spec-expression is legal
645 if In_Spec_Expression then
646 null;
648 elsif Nkind (PN) = N_Range then
650 -- Discriminant cannot be used to constrain a scalar type
652 P := Parent (PN);
654 if Nkind (P) = N_Range_Constraint
655 and then Nkind (Parent (P)) = N_Subtype_Indication
656 and then Nkind (Parent (Parent (P))) = N_Component_Definition
657 then
658 Error_Msg_N ("discriminant cannot constrain scalar type", N);
660 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
662 -- The following check catches the unusual case where a
663 -- discriminant appears within an index constraint that is part
664 -- of a larger expression within a constraint on a component,
665 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
666 -- check case of record components, and note that a similar check
667 -- should also apply in the case of discriminant constraints
668 -- below. ???
670 -- Note that the check for N_Subtype_Declaration below is to
671 -- detect the valid use of discriminants in the constraints of a
672 -- subtype declaration when this subtype declaration appears
673 -- inside the scope of a record type (which is syntactically
674 -- illegal, but which may be created as part of derived type
675 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
676 -- for more info.
678 if Ekind (Current_Scope) = E_Record_Type
679 and then Scope (Disc) = Current_Scope
680 and then not
681 (Nkind (Parent (P)) = N_Subtype_Indication
682 and then
683 Nkind (Parent (Parent (P))) in N_Component_Definition
684 | N_Subtype_Declaration
685 and then Paren_Count (N) = 0)
686 then
687 Error_Msg_N
688 ("discriminant must appear alone in component constraint", N);
689 return;
690 end if;
692 -- Detect a common error:
694 -- type R (D : Positive := 100) is record
695 -- Name : String (1 .. D);
696 -- end record;
698 -- The default value causes an object of type R to be allocated
699 -- with room for Positive'Last characters. The RM does not mandate
700 -- the allocation of the maximum size, but that is what GNAT does
701 -- so we should warn the programmer that there is a problem.
703 Check_Large : declare
704 SI : Node_Id;
705 T : Entity_Id;
706 TB : Node_Id;
707 CB : Entity_Id;
709 function Large_Storage_Type (T : Entity_Id) return Boolean;
710 -- Return True if type T has a large enough range that any
711 -- array whose index type covered the whole range of the type
712 -- would likely raise Storage_Error.
714 ------------------------
715 -- Large_Storage_Type --
716 ------------------------
718 function Large_Storage_Type (T : Entity_Id) return Boolean is
719 begin
720 -- The type is considered large if its bounds are known at
721 -- compile time and if it requires at least as many bits as
722 -- a Positive to store the possible values.
724 return Compile_Time_Known_Value (Type_Low_Bound (T))
725 and then Compile_Time_Known_Value (Type_High_Bound (T))
726 and then
727 Minimum_Size (T, Biased => True) >=
728 RM_Size (Standard_Positive);
729 end Large_Storage_Type;
731 -- Start of processing for Check_Large
733 begin
734 -- Check that the Disc has a large range
736 if not Large_Storage_Type (Etype (Disc)) then
737 goto No_Danger;
738 end if;
740 -- If the enclosing type is limited, we allocate only the
741 -- default value, not the maximum, and there is no need for
742 -- a warning.
744 if Is_Limited_Type (Scope (Disc)) then
745 goto No_Danger;
746 end if;
748 -- Check that it is the high bound
750 if N /= High_Bound (PN)
751 or else No (Discriminant_Default_Value (Disc))
752 then
753 goto No_Danger;
754 end if;
756 -- Check the array allows a large range at this bound. First
757 -- find the array
759 SI := Parent (P);
761 if Nkind (SI) /= N_Subtype_Indication then
762 goto No_Danger;
763 end if;
765 T := Entity (Subtype_Mark (SI));
767 if not Is_Array_Type (T) then
768 goto No_Danger;
769 end if;
771 -- Next, find the dimension
773 TB := First_Index (T);
774 CB := First (Constraints (P));
775 while True
776 and then Present (TB)
777 and then Present (CB)
778 and then CB /= PN
779 loop
780 Next_Index (TB);
781 Next (CB);
782 end loop;
784 if CB /= PN then
785 goto No_Danger;
786 end if;
788 -- Now, check the dimension has a large range
790 if not Large_Storage_Type (Etype (TB)) then
791 goto No_Danger;
792 end if;
794 -- Warn about the danger
796 Error_Msg_N
797 ("??creation of & object may raise Storage_Error!",
798 Scope (Disc));
800 <<No_Danger>>
801 null;
803 end Check_Large;
804 end if;
806 -- Legal case is in index or discriminant constraint
808 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
809 | N_Discriminant_Association
810 then
811 if Paren_Count (N) > 0 then
812 Error_Msg_N
813 ("discriminant in constraint must appear alone", N);
815 elsif Nkind (N) = N_Expanded_Name
816 and then Comes_From_Source (N)
817 then
818 Error_Msg_N
819 ("discriminant must appear alone as a direct name", N);
820 end if;
822 return;
824 -- Otherwise, context is an expression. It should not be within (i.e. a
825 -- subexpression of) a constraint for a component.
827 else
828 D := PN;
829 P := Parent (PN);
830 while Nkind (P) not in
831 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
832 loop
833 D := P;
834 P := Parent (P);
835 exit when No (P);
836 end loop;
838 -- If the discriminant is used in an expression that is a bound of a
839 -- scalar type, an Itype is created and the bounds are attached to
840 -- its range, not to the original subtype indication. Such use is of
841 -- course a double fault.
843 if (Nkind (P) = N_Subtype_Indication
844 and then Nkind (Parent (P)) in N_Component_Definition
845 | N_Derived_Type_Definition
846 and then D = Constraint (P))
848 -- The constraint itself may be given by a subtype indication,
849 -- rather than by a more common discrete range.
851 or else (Nkind (P) = N_Subtype_Indication
852 and then
853 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
854 or else Nkind (P) = N_Entry_Declaration
855 or else Nkind (D) = N_Defining_Identifier
856 then
857 Error_Msg_N
858 ("discriminant in constraint must appear alone", N);
859 end if;
860 end if;
861 end Check_Discriminant_Use;
863 --------------------------------
864 -- Check_For_Visible_Operator --
865 --------------------------------
867 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
868 begin
869 if Comes_From_Source (N)
870 and then not Is_Visible_Operator (Original_Node (N), T)
871 and then not Error_Posted (N)
872 then
873 Error_Msg_NE -- CODEFIX
874 ("operator for} is not directly visible!", N, First_Subtype (T));
875 Error_Msg_N -- CODEFIX
876 ("use clause would make operation legal!", N);
877 end if;
878 end Check_For_Visible_Operator;
880 ---------------------------------
881 -- Check_Fully_Declared_Prefix --
882 ---------------------------------
884 procedure Check_Fully_Declared_Prefix
885 (Typ : Entity_Id;
886 Pref : Node_Id)
888 begin
889 -- Check that the designated type of the prefix of a dereference is
890 -- not an incomplete type. This cannot be done unconditionally, because
891 -- dereferences of private types are legal in default expressions. This
892 -- case is taken care of in Check_Fully_Declared, called below. There
893 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
895 -- This consideration also applies to similar checks for allocators,
896 -- qualified expressions, and type conversions.
898 -- An additional exception concerns other per-object expressions that
899 -- are not directly related to component declarations, in particular
900 -- representation pragmas for tasks. These will be per-object
901 -- expressions if they depend on discriminants or some global entity.
902 -- If the task has access discriminants, the designated type may be
903 -- incomplete at the point the expression is resolved. This resolution
904 -- takes place within the body of the initialization procedure, where
905 -- the discriminant is replaced by its discriminal.
907 if Is_Entity_Name (Pref)
908 and then Ekind (Entity (Pref)) = E_In_Parameter
909 then
910 null;
912 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
913 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
914 -- Analyze_Object_Renaming, and Freeze_Entity.
916 elsif Ada_Version >= Ada_2005
917 and then Is_Entity_Name (Pref)
918 and then Is_Access_Type (Etype (Pref))
919 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
920 E_Incomplete_Type
921 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
922 then
923 null;
924 else
925 Check_Fully_Declared (Typ, Parent (Pref));
926 end if;
927 end Check_Fully_Declared_Prefix;
929 ------------------------------
930 -- Check_Infinite_Recursion --
931 ------------------------------
933 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
934 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
935 -- Determine whether call N invokes the related enclosing subprogram
936 -- with actuals that differ from the subprogram's formals.
938 function Is_Conditional_Statement (N : Node_Id) return Boolean;
939 -- Determine whether arbitrary node N denotes a conditional construct
941 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
942 -- Determine whether arbitrary node N denotes a control flow statement
943 -- or a construct that may contains such a statement.
945 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
946 -- Determine whether arbitrary node N appears immediately within the
947 -- statements of an entry or subprogram body.
949 function Is_Raise_Idiom (N : Node_Id) return Boolean;
950 -- Determine whether arbitrary node N appears immediately within the
951 -- body of an entry or subprogram, and is preceded by a single raise
952 -- statement.
954 function Is_Raise_Statement (N : Node_Id) return Boolean;
955 -- Determine whether arbitrary node N denotes a raise statement
957 function Is_Sole_Statement (N : Node_Id) return Boolean;
958 -- Determine whether arbitrary node N is the sole source statement in
959 -- the body of the enclosing subprogram.
961 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
962 -- Determine whether arbitrary node N is preceded by a control flow
963 -- statement.
965 function Within_Conditional_Statement (N : Node_Id) return Boolean;
966 -- Determine whether arbitrary node N appears within a conditional
967 -- construct.
969 --------------------------------------
970 -- Invoked_With_Different_Arguments --
971 --------------------------------------
973 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
974 Subp : constant Entity_Id := Get_Called_Entity (N);
976 Actual : Node_Id;
977 Formal : Entity_Id;
979 begin
980 -- Determine whether the formals of the invoked subprogram are not
981 -- used as actuals in the call.
983 Actual := First_Actual (N);
984 Formal := First_Formal (Subp);
985 while Present (Actual) and then Present (Formal) loop
987 -- The current actual does not match the current formal
989 if not (Is_Entity_Name (Actual)
990 and then Entity (Actual) = Formal)
991 then
992 return True;
993 end if;
995 Next_Actual (Actual);
996 Next_Formal (Formal);
997 end loop;
999 return False;
1000 end Invoked_With_Different_Arguments;
1002 ------------------------------
1003 -- Is_Conditional_Statement --
1004 ------------------------------
1006 function Is_Conditional_Statement (N : Node_Id) return Boolean is
1007 begin
1008 return
1009 Nkind (N) in N_And_Then
1010 | N_Case_Expression
1011 | N_Case_Statement
1012 | N_If_Expression
1013 | N_If_Statement
1014 | N_Or_Else;
1015 end Is_Conditional_Statement;
1017 -------------------------------
1018 -- Is_Control_Flow_Statement --
1019 -------------------------------
1021 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
1022 begin
1023 -- It is assumed that all statements may affect the control flow in
1024 -- some way. A raise statement may be expanded into a non-statement
1025 -- node.
1027 return Is_Statement (N) or else Is_Raise_Statement (N);
1028 end Is_Control_Flow_Statement;
1030 --------------------------------
1031 -- Is_Immediately_Within_Body --
1032 --------------------------------
1034 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
1035 HSS : constant Node_Id := Parent (N);
1037 begin
1038 return
1039 Nkind (HSS) = N_Handled_Sequence_Of_Statements
1040 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
1041 and then Is_List_Member (N)
1042 and then List_Containing (N) = Statements (HSS);
1043 end Is_Immediately_Within_Body;
1045 --------------------
1046 -- Is_Raise_Idiom --
1047 --------------------
1049 function Is_Raise_Idiom (N : Node_Id) return Boolean is
1050 Raise_Stmt : Node_Id;
1051 Stmt : Node_Id;
1053 begin
1054 if Is_Immediately_Within_Body (N) then
1056 -- Assume that no raise statement has been seen yet
1058 Raise_Stmt := Empty;
1060 -- Examine the statements preceding the input node, skipping
1061 -- internally-generated constructs.
1063 Stmt := Prev (N);
1064 while Present (Stmt) loop
1066 -- Multiple raise statements violate the idiom
1068 if Is_Raise_Statement (Stmt) then
1069 if Present (Raise_Stmt) then
1070 return False;
1071 end if;
1073 Raise_Stmt := Stmt;
1075 elsif Comes_From_Source (Stmt) then
1076 exit;
1077 end if;
1079 Stmt := Prev (Stmt);
1080 end loop;
1082 -- At this point the node must be preceded by a raise statement,
1083 -- and the raise statement has to be the sole statement within
1084 -- the enclosing entry or subprogram body.
1086 return
1087 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
1088 end if;
1090 return False;
1091 end Is_Raise_Idiom;
1093 ------------------------
1094 -- Is_Raise_Statement --
1095 ------------------------
1097 function Is_Raise_Statement (N : Node_Id) return Boolean is
1098 begin
1099 -- A raise statement may be transfomed into a Raise_xxx_Error node
1101 return
1102 Nkind (N) = N_Raise_Statement
1103 or else Nkind (N) in N_Raise_xxx_Error;
1104 end Is_Raise_Statement;
1106 -----------------------
1107 -- Is_Sole_Statement --
1108 -----------------------
1110 function Is_Sole_Statement (N : Node_Id) return Boolean is
1111 Stmt : Node_Id;
1113 begin
1114 -- The input node appears within the statements of an entry or
1115 -- subprogram body. Examine the statements preceding the node.
1117 if Is_Immediately_Within_Body (N) then
1118 Stmt := Prev (N);
1120 while Present (Stmt) loop
1122 -- The statement is preceded by another statement or a source
1123 -- construct. This indicates that the node does not appear by
1124 -- itself.
1126 if Is_Control_Flow_Statement (Stmt)
1127 or else Comes_From_Source (Stmt)
1128 then
1129 return False;
1130 end if;
1132 Stmt := Prev (Stmt);
1133 end loop;
1135 return True;
1136 end if;
1138 -- The input node is within a construct nested inside the entry or
1139 -- subprogram body.
1141 return False;
1142 end Is_Sole_Statement;
1144 ----------------------------------------
1145 -- Preceded_By_Control_Flow_Statement --
1146 ----------------------------------------
1148 function Preceded_By_Control_Flow_Statement
1149 (N : Node_Id) return Boolean
1151 Stmt : Node_Id;
1153 begin
1154 if Is_List_Member (N) then
1155 Stmt := Prev (N);
1157 -- Examine the statements preceding the input node
1159 while Present (Stmt) loop
1160 if Is_Control_Flow_Statement (Stmt) then
1161 return True;
1162 end if;
1164 Stmt := Prev (Stmt);
1165 end loop;
1167 return False;
1168 end if;
1170 -- Assume that the node is part of some control flow statement
1172 return True;
1173 end Preceded_By_Control_Flow_Statement;
1175 ----------------------------------
1176 -- Within_Conditional_Statement --
1177 ----------------------------------
1179 function Within_Conditional_Statement (N : Node_Id) return Boolean is
1180 Stmt : Node_Id;
1182 begin
1183 Stmt := Parent (N);
1184 while Present (Stmt) loop
1185 if Is_Conditional_Statement (Stmt) then
1186 return True;
1188 -- Prevent the search from going too far
1190 elsif Is_Body_Or_Package_Declaration (Stmt) then
1191 exit;
1192 end if;
1194 Stmt := Parent (Stmt);
1195 end loop;
1197 return False;
1198 end Within_Conditional_Statement;
1200 -- Local variables
1202 Call_Context : constant Node_Id :=
1203 Enclosing_Declaration_Or_Statement (Call);
1205 -- Start of processing for Check_Infinite_Recursion
1207 begin
1208 -- The call is assumed to be safe when the enclosing subprogram is
1209 -- invoked with actuals other than its formals.
1211 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1212 -- begin
1213 -- ...
1214 -- Proc (A1, A2, ..., AN);
1215 -- ...
1216 -- end Proc;
1218 if Invoked_With_Different_Arguments (Call) then
1219 return False;
1221 -- The call is assumed to be safe when the invocation of the enclosing
1222 -- subprogram depends on a conditional statement.
1224 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1225 -- begin
1226 -- ...
1227 -- if Some_Condition then
1228 -- Proc (F1, F2, ..., FN);
1229 -- end if;
1230 -- ...
1231 -- end Proc;
1233 elsif Within_Conditional_Statement (Call) then
1234 return False;
1236 -- The context of the call is assumed to be safe when the invocation of
1237 -- the enclosing subprogram is preceded by some control flow statement.
1239 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1240 -- begin
1241 -- ...
1242 -- if Some_Condition then
1243 -- ...
1244 -- end if;
1245 -- ...
1246 -- Proc (F1, F2, ..., FN);
1247 -- ...
1248 -- end Proc;
1250 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1251 return False;
1253 -- Detect an idiom where the context of the call is preceded by a single
1254 -- raise statement.
1256 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1257 -- begin
1258 -- raise ...;
1259 -- Proc (F1, F2, ..., FN);
1260 -- end Proc;
1262 elsif Is_Raise_Idiom (Call_Context) then
1263 return False;
1264 end if;
1266 -- At this point it is certain that infinite recursion will take place
1267 -- as long as the call is executed. Detect a case where the context of
1268 -- the call is the sole source statement within the subprogram body.
1270 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1271 -- begin
1272 -- Proc (F1, F2, ..., FN);
1273 -- end Proc;
1275 -- Install an explicit raise to prevent the infinite recursion.
1277 if Is_Sole_Statement (Call_Context) then
1278 Error_Msg_Warn := SPARK_Mode /= On;
1279 Error_Msg_N ("!infinite recursion<<", Call);
1280 Error_Msg_N ("\!Storage_Error [<<", Call);
1282 Insert_Action (Call,
1283 Make_Raise_Storage_Error (Sloc (Call),
1284 Reason => SE_Infinite_Recursion));
1286 -- Otherwise infinite recursion could take place, considering other flow
1287 -- control constructs such as gotos, exit statements, etc.
1289 else
1290 Error_Msg_Warn := SPARK_Mode /= On;
1291 Error_Msg_N ("!possible infinite recursion<<", Call);
1292 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1293 end if;
1295 return True;
1296 end Check_Infinite_Recursion;
1298 ---------------------------------------
1299 -- Check_No_Direct_Boolean_Operators --
1300 ---------------------------------------
1302 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1303 begin
1304 if Scope (Entity (N)) = Standard_Standard
1305 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1306 then
1307 -- Restriction only applies to original source code
1309 if Comes_From_Source (N) then
1310 Check_Restriction (No_Direct_Boolean_Operators, N);
1311 end if;
1312 end if;
1314 -- Do style check (but skip if in instance, error is on template)
1316 if Style_Check then
1317 if not In_Instance then
1318 Check_Boolean_Operator (N);
1319 end if;
1320 end if;
1321 end Check_No_Direct_Boolean_Operators;
1323 ------------------------------
1324 -- Check_Parameterless_Call --
1325 ------------------------------
1327 procedure Check_Parameterless_Call (N : Node_Id) is
1328 Nam : Node_Id;
1330 function Prefix_Is_Access_Subp return Boolean;
1331 -- If the prefix is of an access_to_subprogram type, the node must be
1332 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1333 -- interpretations are access to subprograms.
1335 ---------------------------
1336 -- Prefix_Is_Access_Subp --
1337 ---------------------------
1339 function Prefix_Is_Access_Subp return Boolean is
1340 I : Interp_Index;
1341 It : Interp;
1343 begin
1344 -- If the context is an attribute reference that can apply to
1345 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1347 if Nkind (Parent (N)) = N_Attribute_Reference
1348 and then Attribute_Name (Parent (N))
1349 in Name_Address | Name_Code_Address | Name_Access
1350 then
1351 return False;
1352 end if;
1354 if not Is_Overloaded (N) then
1355 return
1356 Ekind (Etype (N)) = E_Subprogram_Type
1357 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1358 else
1359 Get_First_Interp (N, I, It);
1360 while Present (It.Typ) loop
1361 if Ekind (It.Typ) /= E_Subprogram_Type
1362 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1363 then
1364 return False;
1365 end if;
1367 Get_Next_Interp (I, It);
1368 end loop;
1370 return True;
1371 end if;
1372 end Prefix_Is_Access_Subp;
1374 -- Start of processing for Check_Parameterless_Call
1376 begin
1377 -- Defend against junk stuff if errors already detected
1379 if Total_Errors_Detected /= 0 then
1380 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1381 return;
1382 elsif Nkind (N) in N_Has_Chars
1383 and then not Is_Valid_Name (Chars (N))
1384 then
1385 return;
1386 end if;
1388 Require_Entity (N);
1389 end if;
1391 -- If the context expects a value, and the name is a procedure, this is
1392 -- most likely a missing 'Access. Don't try to resolve the parameterless
1393 -- call, error will be caught when the outer call is analyzed.
1395 if Is_Entity_Name (N)
1396 and then Ekind (Entity (N)) = E_Procedure
1397 and then not Is_Overloaded (N)
1398 and then
1399 Nkind (Parent (N)) in N_Parameter_Association
1400 | N_Function_Call
1401 | N_Procedure_Call_Statement
1402 then
1403 return;
1404 end if;
1406 -- Rewrite as call if overloadable entity that is (or could be, in the
1407 -- overloaded case) a function call. If we know for sure that the entity
1408 -- is an enumeration literal, we do not rewrite it.
1410 -- If the entity is the name of an operator, it cannot be a call because
1411 -- operators cannot have default parameters. In this case, this must be
1412 -- a string whose contents coincide with an operator name. Set the kind
1413 -- of the node appropriately.
1415 if (Is_Entity_Name (N)
1416 and then Nkind (N) /= N_Operator_Symbol
1417 and then Is_Overloadable (Entity (N))
1418 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1419 or else Is_Overloaded (N)))
1421 -- Rewrite as call if it is an explicit dereference of an expression of
1422 -- a subprogram access type, and the subprogram type is not that of a
1423 -- procedure or entry.
1425 or else
1426 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1428 -- Rewrite as call if it is a selected component which is a function,
1429 -- this is the case of a call to a protected function (which may be
1430 -- overloaded with other protected operations).
1432 or else
1433 (Nkind (N) = N_Selected_Component
1434 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1435 or else
1436 (Ekind (Entity (Selector_Name (N))) in
1437 E_Entry | E_Procedure
1438 and then Is_Overloaded (Selector_Name (N)))))
1440 -- If one of the above three conditions is met, rewrite as call. Apply
1441 -- the rewriting only once.
1443 then
1444 if Nkind (Parent (N)) /= N_Function_Call
1445 or else N /= Name (Parent (N))
1446 then
1448 -- This may be a prefixed call that was not fully analyzed, e.g.
1449 -- an actual in an instance.
1451 if Ada_Version >= Ada_2005
1452 and then Nkind (N) = N_Selected_Component
1453 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1454 then
1455 Analyze_Selected_Component (N);
1457 if Nkind (N) /= N_Selected_Component then
1458 return;
1459 end if;
1460 end if;
1462 -- The node is the name of the parameterless call. Preserve its
1463 -- descendants, which may be complex expressions.
1465 Nam := Relocate_Node (N);
1467 -- If overloaded, overload set belongs to new copy
1469 Save_Interps (N, Nam);
1471 -- Change node to parameterless function call (note that the
1472 -- Parameter_Associations associations field is left set to Empty,
1473 -- its normal default value since there are no parameters)
1475 Change_Node (N, N_Function_Call);
1476 Set_Name (N, Nam);
1477 Set_Sloc (N, Sloc (Nam));
1478 Analyze_Call (N);
1479 end if;
1481 elsif Nkind (N) = N_Parameter_Association then
1482 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1484 elsif Nkind (N) = N_Operator_Symbol then
1485 Set_Etype (N, Empty);
1486 Set_Entity (N, Empty);
1487 Set_Is_Overloaded (N, False);
1488 Change_Operator_Symbol_To_String_Literal (N);
1489 Set_Etype (N, Any_String);
1490 end if;
1491 end Check_Parameterless_Call;
1493 ----------------------------------------
1494 -- Is_Atomic_Non_VFA_Ref_With_Address --
1495 ----------------------------------------
1497 function Is_Atomic_Non_VFA_Ref_With_Address (N : Node_Id) return Boolean is
1498 Pref : constant Node_Id := Prefix (N);
1500 function Is_Atomic_Non_VFA (E : Entity_Id) return Boolean;
1501 -- Return true if E is Atomic but not Volatile_Full_Access
1503 -----------------------
1504 -- Is_Atomic_Non_VFA --
1505 -----------------------
1507 function Is_Atomic_Non_VFA (E : Entity_Id) return Boolean is
1508 begin
1509 return Is_Atomic (E) and then not Is_Volatile_Full_Access (E);
1510 end Is_Atomic_Non_VFA;
1512 begin
1513 if Is_Entity_Name (Pref) then
1514 declare
1515 Pent : constant Entity_Id := Entity (Pref);
1516 Ptyp : constant Entity_Id := Etype (Pent);
1518 begin
1519 return not Is_Access_Type (Ptyp)
1520 and then (Is_Atomic_Non_VFA (Ptyp)
1521 or else Is_Atomic_Non_VFA (Pent))
1522 and then Present (Address_Clause (Pent));
1523 end;
1525 else
1526 return False;
1527 end if;
1528 end Is_Atomic_Non_VFA_Ref_With_Address;
1530 -----------------------------
1531 -- Is_Definite_Access_Type --
1532 -----------------------------
1534 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean is
1535 Btyp : constant Entity_Id := Base_Type (E);
1536 begin
1537 return Ekind (Btyp) = E_Access_Type
1538 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1539 and then Comes_From_Source (Btyp));
1540 end Is_Definite_Access_Type;
1542 ----------------------
1543 -- Is_Predefined_Op --
1544 ----------------------
1546 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1547 begin
1548 -- Predefined operators are intrinsic subprograms
1550 if not Is_Intrinsic_Subprogram (Nam) then
1551 return False;
1552 end if;
1554 -- A call to a back-end builtin is never a predefined operator
1556 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1557 return False;
1558 end if;
1560 return not Is_Generic_Instance (Nam)
1561 and then Chars (Nam) in Any_Operator_Name
1562 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1563 end Is_Predefined_Op;
1565 -----------------------------
1566 -- Make_Call_Into_Operator --
1567 -----------------------------
1569 procedure Make_Call_Into_Operator
1570 (N : Node_Id;
1571 Typ : Entity_Id;
1572 Op_Id : Entity_Id)
1574 Op_Name : constant Name_Id := Chars (Op_Id);
1575 Act1 : Node_Id := First_Actual (N);
1576 Act2 : Node_Id := Next_Actual (Act1);
1577 Error : Boolean := False;
1578 Func : constant Entity_Id := Entity (Name (N));
1579 Is_Binary : constant Boolean := Present (Act2);
1580 Op_Node : Node_Id;
1581 Opnd_Type : Entity_Id := Empty;
1582 Orig_Type : Entity_Id := Empty;
1583 Pack : Entity_Id;
1585 type Kind_Test is access function (E : N_Entity_Id) return Boolean;
1587 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1588 -- If the operand is not universal, and the operator is given by an
1589 -- expanded name, verify that the operand has an interpretation with a
1590 -- type defined in the given scope of the operator.
1592 function Type_In_P (Test : not null Kind_Test) return Entity_Id;
1593 -- Find a type of the given class in package Pack that contains the
1594 -- operator.
1596 ---------------------------
1597 -- Operand_Type_In_Scope --
1598 ---------------------------
1600 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1601 Nod : constant Node_Id := Right_Opnd (Op_Node);
1602 I : Interp_Index;
1603 It : Interp;
1605 begin
1606 if not Is_Overloaded (Nod) then
1607 return Scope (Base_Type (Etype (Nod))) = S;
1609 else
1610 Get_First_Interp (Nod, I, It);
1611 while Present (It.Typ) loop
1612 if Scope (Base_Type (It.Typ)) = S then
1613 return True;
1614 end if;
1616 Get_Next_Interp (I, It);
1617 end loop;
1619 return False;
1620 end if;
1621 end Operand_Type_In_Scope;
1623 ---------------
1624 -- Type_In_P --
1625 ---------------
1627 function Type_In_P (Test : not null Kind_Test) return Entity_Id is
1628 E : Entity_Id;
1630 function In_Decl return Boolean;
1631 -- Verify that node is not part of the type declaration for the
1632 -- candidate type, which would otherwise be invisible.
1634 -------------
1635 -- In_Decl --
1636 -------------
1638 function In_Decl return Boolean is
1639 Decl_Node : constant Node_Id := Parent (E);
1640 Context : Node_Id;
1642 begin
1643 if Etype (E) = Any_Type then
1644 return True;
1646 elsif No (Decl_Node) then
1647 return False;
1649 else
1650 Context := N;
1652 while Present (Context)
1653 and then Nkind (Context) /= N_Compilation_Unit
1654 loop
1655 if Context = Decl_Node then
1656 return True;
1657 else
1658 Context := Parent (Context);
1659 end if;
1660 end loop;
1662 return False;
1663 end if;
1664 end In_Decl;
1666 -- Start of processing for Type_In_P
1668 begin
1669 -- If the context type is declared in the prefix package, this is the
1670 -- desired base type.
1672 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1673 return Base_Type (Typ);
1675 else
1676 E := First_Entity (Pack);
1677 while Present (E) loop
1678 if Test (E) and then not In_Decl then
1679 return E;
1680 end if;
1682 Next_Entity (E);
1683 end loop;
1685 return Empty;
1686 end if;
1687 end Type_In_P;
1689 -- Start of processing for Make_Call_Into_Operator
1691 begin
1692 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1694 -- Preserve the Comes_From_Source flag on the result if the original
1695 -- call came from source. Although it is not strictly the case that the
1696 -- operator as such comes from the source, logically it corresponds
1697 -- exactly to the function call in the source, so it should be marked
1698 -- this way (e.g. to make sure that validity checks work fine).
1700 Preserve_Comes_From_Source (Op_Node, N);
1702 -- Ensure that the corresponding operator has the same parent as the
1703 -- original call. This guarantees that parent traversals performed by
1704 -- the ABE mechanism succeed.
1706 Set_Parent (Op_Node, Parent (N));
1708 -- Binary operator
1710 if Is_Binary then
1711 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1712 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1713 Save_Interps (Act1, Left_Opnd (Op_Node));
1714 Save_Interps (Act2, Right_Opnd (Op_Node));
1715 Act1 := Left_Opnd (Op_Node);
1716 Act2 := Right_Opnd (Op_Node);
1718 -- Unary operator
1720 else
1721 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1722 Save_Interps (Act1, Right_Opnd (Op_Node));
1723 Act1 := Right_Opnd (Op_Node);
1724 end if;
1726 -- If the operator is denoted by an expanded name, and the prefix is
1727 -- not Standard, but the operator is a predefined one whose scope is
1728 -- Standard, then this is an implicit_operator, inserted as an
1729 -- interpretation by the procedure of the same name. This procedure
1730 -- overestimates the presence of implicit operators, because it does
1731 -- not examine the type of the operands. Verify now that the operand
1732 -- type appears in the given scope. If right operand is universal,
1733 -- check the other operand. In the case of concatenation, either
1734 -- argument can be the component type, so check the type of the result.
1735 -- If both arguments are literals, look for a type of the right kind
1736 -- defined in the given scope. This elaborate nonsense is brought to
1737 -- you courtesy of b33302a. The type itself must be frozen, so we must
1738 -- find the type of the proper class in the given scope.
1740 -- A final wrinkle is the multiplication operator for fixed point types,
1741 -- which is defined in Standard only, and not in the scope of the
1742 -- fixed point type itself.
1744 if Nkind (Name (N)) = N_Expanded_Name then
1745 Pack := Entity (Prefix (Name (N)));
1747 -- If this is a package renaming, get renamed entity, which will be
1748 -- the scope of the operands if operaton is type-correct.
1750 if Present (Renamed_Entity (Pack)) then
1751 Pack := Renamed_Entity (Pack);
1752 end if;
1754 -- If the entity being called is defined in the given package, it is
1755 -- a renaming of a predefined operator, and known to be legal.
1757 if Scope (Entity (Name (N))) = Pack
1758 and then Pack /= Standard_Standard
1759 then
1760 null;
1762 -- Visibility does not need to be checked in an instance: if the
1763 -- operator was not visible in the generic it has been diagnosed
1764 -- already, else there is an implicit copy of it in the instance.
1766 elsif In_Instance then
1767 null;
1769 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1770 and then Is_Fixed_Point_Type (Etype (Act1))
1771 and then Is_Fixed_Point_Type (Etype (Act2))
1772 then
1773 if Pack /= Standard_Standard then
1774 Error := True;
1775 end if;
1777 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1778 -- available.
1780 elsif Ada_Version >= Ada_2005
1781 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1782 and then (Is_Anonymous_Access_Type (Etype (Act1))
1783 or else Is_Anonymous_Access_Type (Etype (Act2)))
1784 then
1785 null;
1787 else
1788 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1790 if Op_Name = Name_Op_Concat then
1791 Opnd_Type := Base_Type (Typ);
1793 elsif (Scope (Opnd_Type) = Standard_Standard
1794 and then Is_Binary)
1795 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1796 and then Is_Binary
1797 and then not Comes_From_Source (Opnd_Type))
1798 then
1799 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1800 end if;
1802 if Scope (Opnd_Type) = Standard_Standard then
1804 -- Verify that the scope contains a type that corresponds to
1805 -- the given literal. Optimize the case where Pack is Standard.
1807 if Pack /= Standard_Standard then
1808 if Opnd_Type = Universal_Integer then
1809 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1811 elsif Opnd_Type = Universal_Real then
1812 Orig_Type := Type_In_P (Is_Real_Type'Access);
1814 elsif Opnd_Type = Universal_Access then
1815 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1817 elsif Opnd_Type = Any_String then
1818 Orig_Type := Type_In_P (Is_String_Type'Access);
1820 elsif Opnd_Type = Any_Composite then
1821 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1823 if Present (Orig_Type) then
1824 if Has_Private_Component (Orig_Type) then
1825 Orig_Type := Empty;
1826 else
1827 Set_Etype (Act1, Orig_Type);
1829 if Is_Binary then
1830 Set_Etype (Act2, Orig_Type);
1831 end if;
1832 end if;
1833 end if;
1835 else
1836 Orig_Type := Empty;
1837 end if;
1839 Error := No (Orig_Type);
1840 end if;
1842 elsif Ekind (Opnd_Type) = E_Allocator_Type
1843 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1844 then
1845 Error := True;
1847 -- If the type is defined elsewhere, and the operator is not
1848 -- defined in the given scope (by a renaming declaration, e.g.)
1849 -- then this is an error as well. If an extension of System is
1850 -- present, and the type may be defined there, Pack must be
1851 -- System itself.
1853 elsif Scope (Opnd_Type) /= Pack
1854 and then Scope (Op_Id) /= Pack
1855 and then (No (System_Aux_Id)
1856 or else Scope (Opnd_Type) /= System_Aux_Id
1857 or else Pack /= Scope (System_Aux_Id))
1858 then
1859 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1860 Error := True;
1861 else
1862 Error := not Operand_Type_In_Scope (Pack);
1863 end if;
1865 elsif Pack = Standard_Standard
1866 and then not Operand_Type_In_Scope (Standard_Standard)
1867 then
1868 Error := True;
1869 end if;
1870 end if;
1872 if Error then
1873 Error_Msg_Node_2 := Pack;
1874 Error_Msg_NE
1875 ("& not declared in&", N, Selector_Name (Name (N)));
1876 Set_Etype (N, Any_Type);
1877 return;
1879 -- Detect a mismatch between the context type and the result type
1880 -- in the named package, which is otherwise not detected if the
1881 -- operands are universal. Check is only needed if source entity is
1882 -- an operator, not a function that renames an operator.
1884 elsif Nkind (Parent (N)) /= N_Type_Conversion
1885 and then Ekind (Entity (Name (N))) = E_Operator
1886 and then Is_Numeric_Type (Typ)
1887 and then not Is_Universal_Numeric_Type (Typ)
1888 and then Scope (Base_Type (Typ)) /= Pack
1889 and then not In_Instance
1890 then
1891 if Is_Fixed_Point_Type (Typ)
1892 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1893 then
1894 -- Already checked above
1896 null;
1898 -- Operator may be defined in an extension of System
1900 elsif Present (System_Aux_Id)
1901 and then Present (Opnd_Type)
1902 and then Scope (Opnd_Type) = System_Aux_Id
1903 then
1904 null;
1906 else
1907 -- Could we use Wrong_Type here??? (this would require setting
1908 -- Etype (N) to the actual type found where Typ was expected).
1910 Error_Msg_NE ("expect }", N, Typ);
1911 end if;
1912 end if;
1913 end if;
1915 Set_Chars (Op_Node, Op_Name);
1917 if not Is_Private_Type (Etype (N)) then
1918 Set_Etype (Op_Node, Base_Type (Etype (N)));
1919 else
1920 Set_Etype (Op_Node, Etype (N));
1921 end if;
1923 -- If this is a call to a function that renames a predefined equality,
1924 -- the renaming declaration provides a type that must be used to
1925 -- resolve the operands. This must be done now because resolution of
1926 -- the equality node will not resolve any remaining ambiguity, and it
1927 -- assumes that the first operand is not overloaded.
1929 if Op_Name in Name_Op_Eq | Name_Op_Ne
1930 and then Ekind (Func) = E_Function
1931 and then Is_Overloaded (Act1)
1932 then
1933 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1934 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1935 end if;
1937 Set_Entity (Op_Node, Op_Id);
1938 Generate_Reference (Op_Id, N, ' ');
1940 Rewrite (N, Op_Node);
1942 -- If this is an arithmetic operator and the result type is private,
1943 -- the operands and the result must be wrapped in conversion to
1944 -- expose the underlying numeric type and expand the proper checks,
1945 -- e.g. on division.
1947 if Is_Private_Type (Typ) then
1948 case Nkind (N) is
1949 when N_Op_Add
1950 | N_Op_Divide
1951 | N_Op_Expon
1952 | N_Op_Mod
1953 | N_Op_Multiply
1954 | N_Op_Rem
1955 | N_Op_Subtract
1957 Resolve_Intrinsic_Operator (N, Typ);
1959 when N_Op_Abs
1960 | N_Op_Minus
1961 | N_Op_Plus
1963 Resolve_Intrinsic_Unary_Operator (N, Typ);
1965 when others =>
1966 Resolve (N, Typ);
1967 end case;
1968 else
1969 Resolve (N, Typ);
1970 end if;
1971 end Make_Call_Into_Operator;
1973 -------------------
1974 -- Operator_Kind --
1975 -------------------
1977 function Operator_Kind
1978 (Op_Name : Name_Id;
1979 Is_Binary : Boolean) return Node_Kind
1981 Kind : Node_Kind;
1983 begin
1984 -- Use CASE statement or array???
1986 if Is_Binary then
1987 if Op_Name = Name_Op_And then
1988 Kind := N_Op_And;
1989 elsif Op_Name = Name_Op_Or then
1990 Kind := N_Op_Or;
1991 elsif Op_Name = Name_Op_Xor then
1992 Kind := N_Op_Xor;
1993 elsif Op_Name = Name_Op_Eq then
1994 Kind := N_Op_Eq;
1995 elsif Op_Name = Name_Op_Ne then
1996 Kind := N_Op_Ne;
1997 elsif Op_Name = Name_Op_Lt then
1998 Kind := N_Op_Lt;
1999 elsif Op_Name = Name_Op_Le then
2000 Kind := N_Op_Le;
2001 elsif Op_Name = Name_Op_Gt then
2002 Kind := N_Op_Gt;
2003 elsif Op_Name = Name_Op_Ge then
2004 Kind := N_Op_Ge;
2005 elsif Op_Name = Name_Op_Add then
2006 Kind := N_Op_Add;
2007 elsif Op_Name = Name_Op_Subtract then
2008 Kind := N_Op_Subtract;
2009 elsif Op_Name = Name_Op_Concat then
2010 Kind := N_Op_Concat;
2011 elsif Op_Name = Name_Op_Multiply then
2012 Kind := N_Op_Multiply;
2013 elsif Op_Name = Name_Op_Divide then
2014 Kind := N_Op_Divide;
2015 elsif Op_Name = Name_Op_Mod then
2016 Kind := N_Op_Mod;
2017 elsif Op_Name = Name_Op_Rem then
2018 Kind := N_Op_Rem;
2019 elsif Op_Name = Name_Op_Expon then
2020 Kind := N_Op_Expon;
2021 else
2022 raise Program_Error;
2023 end if;
2025 -- Unary operators
2027 else
2028 if Op_Name = Name_Op_Add then
2029 Kind := N_Op_Plus;
2030 elsif Op_Name = Name_Op_Subtract then
2031 Kind := N_Op_Minus;
2032 elsif Op_Name = Name_Op_Abs then
2033 Kind := N_Op_Abs;
2034 elsif Op_Name = Name_Op_Not then
2035 Kind := N_Op_Not;
2036 else
2037 raise Program_Error;
2038 end if;
2039 end if;
2041 return Kind;
2042 end Operator_Kind;
2044 ---------------------------------------
2045 -- Original_Implementation_Base_Type --
2046 ---------------------------------------
2048 function Original_Implementation_Base_Type
2049 (Id : Entity_Id) return Entity_Id
2051 IBT : constant Entity_Id := Implementation_Base_Type (Id);
2052 IBT_Decl : constant Node_Id := Parent (IBT);
2053 Parent_Id : Node_Id;
2054 begin
2055 if Nkind (IBT_Decl) = N_Full_Type_Declaration
2056 and then Original_Node (IBT_Decl) /= IBT_Decl
2057 and then Nkind (Original_Node (IBT_Decl)) =
2058 N_Full_Type_Declaration
2059 and then Nkind (Type_Definition (Original_Node (IBT_Decl)))
2060 = N_Derived_Type_Definition
2061 then
2062 Parent_Id := Subtype_Indication (Type_Definition
2063 (Original_Node (IBT_Decl)));
2065 if Nkind (Parent_Id) = N_Subtype_Indication then
2066 Parent_Id := Subtype_Mark (Parent_Id);
2067 end if;
2069 return Original_Implementation_Base_Type
2070 (Etype (Parent_Id));
2071 else
2072 return IBT;
2073 end if;
2074 end Original_Implementation_Base_Type;
2076 ----------------------------
2077 -- Preanalyze_And_Resolve --
2078 ----------------------------
2080 procedure Preanalyze_And_Resolve
2081 (N : Node_Id;
2082 T : Entity_Id;
2083 With_Freezing : Boolean)
2085 Save_Full_Analysis : constant Boolean := Full_Analysis;
2086 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
2087 Save_Preanalysis_Count : constant Nat :=
2088 Inside_Preanalysis_Without_Freezing;
2089 begin
2090 pragma Assert (Nkind (N) in N_Subexpr);
2092 if not With_Freezing then
2093 Set_Must_Not_Freeze (N);
2094 Inside_Preanalysis_Without_Freezing :=
2095 Inside_Preanalysis_Without_Freezing + 1;
2096 end if;
2098 Full_Analysis := False;
2099 Expander_Mode_Save_And_Set (False);
2101 -- See also Preanalyze_And_Resolve in sem.adb for similar handling
2103 -- Normally, we suppress all checks for this preanalysis. There is no
2104 -- point in processing them now, since they will be applied properly
2105 -- and in the proper location when the default expressions reanalyzed
2106 -- and reexpanded later on. We will also have more information at that
2107 -- point for possible suppression of individual checks.
2109 -- However, in GNATprove mode, most expansion is suppressed, and this
2110 -- later reanalysis and reexpansion may not occur. GNATprove mode does
2111 -- require the setting of checking flags for proof purposes, so we
2112 -- do the GNATprove preanalysis without suppressing checks.
2114 -- This special handling for SPARK mode is required for example in the
2115 -- case of Ada 2012 constructs such as quantified expressions, which are
2116 -- expanded in two separate steps.
2118 -- We also do not want to suppress checks if we are not dealing
2119 -- with a default expression. One such case that is known to reach
2120 -- this point is the expression of an expression function.
2122 if GNATprove_Mode or Nkind (Parent (N)) = N_Simple_Return_Statement then
2123 Analyze_And_Resolve (N, T);
2124 else
2125 Analyze_And_Resolve (N, T, Suppress => All_Checks);
2126 end if;
2128 Expander_Mode_Restore;
2129 Full_Analysis := Save_Full_Analysis;
2131 if not With_Freezing then
2132 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
2133 Inside_Preanalysis_Without_Freezing :=
2134 Inside_Preanalysis_Without_Freezing - 1;
2135 end if;
2137 pragma Assert
2138 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
2139 end Preanalyze_And_Resolve;
2141 ----------------------------
2142 -- Preanalyze_And_Resolve --
2143 ----------------------------
2145 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
2146 begin
2147 Preanalyze_And_Resolve (N, T, With_Freezing => False);
2148 end Preanalyze_And_Resolve;
2150 -- Version without context type
2152 procedure Preanalyze_And_Resolve (N : Node_Id) is
2153 Save_Full_Analysis : constant Boolean := Full_Analysis;
2155 begin
2156 Full_Analysis := False;
2157 Expander_Mode_Save_And_Set (False);
2159 Analyze (N);
2160 Resolve (N, Etype (N), Suppress => All_Checks);
2162 Expander_Mode_Restore;
2163 Full_Analysis := Save_Full_Analysis;
2164 end Preanalyze_And_Resolve;
2166 ------------------------------------------
2167 -- Preanalyze_With_Freezing_And_Resolve --
2168 ------------------------------------------
2170 procedure Preanalyze_With_Freezing_And_Resolve
2171 (N : Node_Id;
2172 T : Entity_Id)
2174 begin
2175 Preanalyze_And_Resolve (N, T, With_Freezing => True);
2176 end Preanalyze_With_Freezing_And_Resolve;
2178 ----------------------------------
2179 -- Replace_Actual_Discriminants --
2180 ----------------------------------
2182 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2183 Loc : constant Source_Ptr := Sloc (N);
2184 Tsk : Node_Id := Empty;
2186 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2187 -- Comment needed???
2189 -------------------
2190 -- Process_Discr --
2191 -------------------
2193 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2194 Ent : Entity_Id;
2196 begin
2197 if Nkind (Nod) = N_Identifier then
2198 Ent := Entity (Nod);
2200 if Present (Ent)
2201 and then Ekind (Ent) = E_Discriminant
2202 then
2203 Rewrite (Nod,
2204 Make_Selected_Component (Loc,
2205 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2206 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2208 Set_Etype (Nod, Etype (Ent));
2209 end if;
2211 end if;
2213 return OK;
2214 end Process_Discr;
2216 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2218 -- Start of processing for Replace_Actual_Discriminants
2220 begin
2221 if Expander_Active then
2222 null;
2224 -- Allow the replacement of concurrent discriminants in GNATprove even
2225 -- though this is a light expansion activity. Note that generic units
2226 -- are not modified.
2228 elsif GNATprove_Mode and not Inside_A_Generic then
2229 null;
2231 else
2232 return;
2233 end if;
2235 if Nkind (Name (N)) = N_Selected_Component then
2236 Tsk := Prefix (Name (N));
2238 elsif Nkind (Name (N)) = N_Indexed_Component then
2239 Tsk := Prefix (Prefix (Name (N)));
2240 end if;
2242 if Present (Tsk) then
2243 Replace_Discrs (Default);
2244 end if;
2245 end Replace_Actual_Discriminants;
2247 -------------
2248 -- Resolve --
2249 -------------
2251 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2252 Ambiguous : Boolean := False;
2253 Ctx_Type : Entity_Id := Typ;
2254 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2255 Err_Type : Entity_Id := Empty;
2256 Found : Boolean := False;
2257 From_Lib : Boolean;
2258 I : Interp_Index;
2259 I1 : Interp_Index := 0; -- prevent junk warning
2260 It : Interp;
2261 It1 : Interp;
2262 Seen : Entity_Id := Empty; -- prevent junk warning
2264 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2265 -- Determine whether a node comes from a predefined library unit or
2266 -- Standard.
2268 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2269 -- Try and fix up a literal so that it matches its expected type. New
2270 -- literals are manufactured if necessary to avoid cascaded errors.
2272 procedure Report_Ambiguous_Argument;
2273 -- Additional diagnostics when an ambiguous call has an ambiguous
2274 -- argument (typically a controlling actual).
2276 procedure Resolution_Failed;
2277 -- Called when attempt at resolving current expression fails
2279 ------------------------------------
2280 -- Comes_From_Predefined_Lib_Unit --
2281 -------------------------------------
2283 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2284 begin
2285 return
2286 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2287 end Comes_From_Predefined_Lib_Unit;
2289 --------------------
2290 -- Patch_Up_Value --
2291 --------------------
2293 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2294 begin
2295 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2296 Rewrite (N,
2297 Make_Real_Literal (Sloc (N),
2298 Realval => UR_From_Uint (Intval (N))));
2299 Set_Etype (N, Universal_Real);
2300 Set_Is_Static_Expression (N);
2302 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2303 Rewrite (N,
2304 Make_Integer_Literal (Sloc (N),
2305 Intval => UR_To_Uint (Realval (N))));
2306 Set_Etype (N, Universal_Integer);
2307 Set_Is_Static_Expression (N);
2309 elsif Nkind (N) = N_String_Literal
2310 and then Is_Character_Type (Typ)
2311 then
2312 Set_Character_Literal_Name (Get_Char_Code ('A'));
2313 Rewrite (N,
2314 Make_Character_Literal (Sloc (N),
2315 Chars => Name_Find,
2316 Char_Literal_Value =>
2317 UI_From_CC (Get_Char_Code ('A'))));
2318 Set_Etype (N, Any_Character);
2319 Set_Is_Static_Expression (N);
2321 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2322 Rewrite (N,
2323 Make_String_Literal (Sloc (N),
2324 Strval => End_String));
2326 elsif Nkind (N) = N_Range then
2327 Patch_Up_Value (Low_Bound (N), Typ);
2328 Patch_Up_Value (High_Bound (N), Typ);
2329 end if;
2330 end Patch_Up_Value;
2332 -------------------------------
2333 -- Report_Ambiguous_Argument --
2334 -------------------------------
2336 procedure Report_Ambiguous_Argument is
2337 Arg : constant Node_Id := First (Parameter_Associations (N));
2338 I : Interp_Index;
2339 It : Interp;
2341 begin
2342 if Nkind (Arg) = N_Function_Call
2343 and then Is_Entity_Name (Name (Arg))
2344 and then Is_Overloaded (Name (Arg))
2345 then
2346 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2348 -- Examine possible interpretations, and adapt the message
2349 -- for inherited subprograms declared by a type derivation.
2351 Get_First_Interp (Name (Arg), I, It);
2352 while Present (It.Nam) loop
2353 Error_Msg_Sloc := Sloc (It.Nam);
2355 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2356 Error_Msg_N ("interpretation (inherited) #!", Arg);
2357 else
2358 Error_Msg_N ("interpretation #!", Arg);
2359 end if;
2361 Get_Next_Interp (I, It);
2362 end loop;
2363 end if;
2365 -- Additional message and hint if the ambiguity involves an Ada 2022
2366 -- container aggregate.
2368 Check_Ambiguous_Aggregate (N);
2369 end Report_Ambiguous_Argument;
2371 -----------------------
2372 -- Resolution_Failed --
2373 -----------------------
2375 procedure Resolution_Failed is
2376 begin
2377 Patch_Up_Value (N, Typ);
2379 -- Set the type to the desired one to minimize cascaded errors. Note
2380 -- that this is an approximation and does not work in all cases.
2382 Set_Etype (N, Typ);
2384 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2385 Set_Is_Overloaded (N, False);
2387 -- The caller will return without calling the expander, so we need
2388 -- to set the analyzed flag. Note that it is fine to set Analyzed
2389 -- to True even if we are in the middle of a shallow analysis,
2390 -- (see the spec of sem for more details) since this is an error
2391 -- situation anyway, and there is no point in repeating the
2392 -- analysis later (indeed it won't work to repeat it later, since
2393 -- we haven't got a clear resolution of which entity is being
2394 -- referenced.)
2396 Set_Analyzed (N, True);
2397 return;
2398 end Resolution_Failed;
2400 -- Start of processing for Resolve
2402 begin
2403 if N = Error then
2404 return;
2405 end if;
2407 -- Access attribute on remote subprogram cannot be used for a non-remote
2408 -- access-to-subprogram type.
2410 if Nkind (N) = N_Attribute_Reference
2411 and then Attribute_Name (N) in Name_Access
2412 | Name_Unrestricted_Access
2413 | Name_Unchecked_Access
2414 and then Comes_From_Source (N)
2415 and then Is_Entity_Name (Prefix (N))
2416 and then Is_Subprogram (Entity (Prefix (N)))
2417 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2418 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2419 then
2420 Error_Msg_N
2421 ("prefix must statically denote a non-remote subprogram", N);
2422 end if;
2424 -- If the context is a Remote_Access_To_Subprogram, access attributes
2425 -- must be resolved with the corresponding fat pointer. There is no need
2426 -- to check for the attribute name since the return type of an
2427 -- attribute is never a remote type.
2429 if Nkind (N) = N_Attribute_Reference
2430 and then Comes_From_Source (N)
2431 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2432 then
2433 declare
2434 Attr : constant Attribute_Id :=
2435 Get_Attribute_Id (Attribute_Name (N));
2436 Pref : constant Node_Id := Prefix (N);
2437 Decl : Node_Id;
2438 Spec : Node_Id;
2439 Is_Remote : Boolean := True;
2441 begin
2442 -- Check that Typ is a remote access-to-subprogram type
2444 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2446 -- Prefix (N) must statically denote a remote subprogram
2447 -- declared in a package specification.
2449 if Attr = Attribute_Access or else
2450 Attr = Attribute_Unchecked_Access or else
2451 Attr = Attribute_Unrestricted_Access
2452 then
2453 Decl := Unit_Declaration_Node (Entity (Pref));
2455 if Nkind (Decl) = N_Subprogram_Body then
2456 Spec := Corresponding_Spec (Decl);
2458 if Present (Spec) then
2459 Decl := Unit_Declaration_Node (Spec);
2460 end if;
2461 end if;
2463 Spec := Parent (Decl);
2465 if not Is_Entity_Name (Prefix (N))
2466 or else Nkind (Spec) /= N_Package_Specification
2467 or else
2468 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2469 then
2470 Is_Remote := False;
2471 Error_Msg_N
2472 ("prefix must statically denote a remote subprogram",
2474 end if;
2476 -- If we are generating code in distributed mode, perform
2477 -- semantic checks against corresponding remote entities.
2479 if Expander_Active
2480 and then Get_PCS_Name /= Name_No_DSA
2481 then
2482 Check_Subtype_Conformant
2483 (New_Id => Entity (Prefix (N)),
2484 Old_Id => Designated_Type
2485 (Corresponding_Remote_Type (Typ)),
2486 Err_Loc => N);
2488 if Is_Remote then
2489 Process_Remote_AST_Attribute (N, Typ);
2490 end if;
2491 end if;
2492 end if;
2493 end if;
2494 end;
2495 end if;
2497 Debug_A_Entry ("resolving ", N);
2499 if Debug_Flag_V then
2500 Write_Overloads (N);
2501 end if;
2503 if Comes_From_Source (N) then
2504 if Is_Fixed_Point_Type (Typ) then
2505 Check_Restriction (No_Fixed_Point, N);
2507 elsif Is_Floating_Point_Type (Typ)
2508 and then Typ /= Universal_Real
2509 and then Typ /= Any_Real
2510 then
2511 Check_Restriction (No_Floating_Point, N);
2512 end if;
2513 end if;
2515 -- Return if already analyzed
2517 if Analyzed (N) then
2518 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2519 Analyze_Dimension (N);
2520 return;
2522 -- Any case of Any_Type as the Etype value means that we had a
2523 -- previous error.
2525 elsif Etype (N) = Any_Type then
2526 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2527 return;
2528 end if;
2530 Check_Parameterless_Call (N);
2532 -- The resolution of an Expression_With_Actions is determined by
2533 -- its Expression, but if the node comes from source it is a
2534 -- Declare_Expression and requires scope management.
2536 if Nkind (N) = N_Expression_With_Actions then
2537 if Comes_From_Source (N) and then not Is_Rewrite_Substitution (N) then
2538 Resolve_Declare_Expression (N, Typ);
2539 else
2540 Resolve (Expression (N), Typ);
2541 end if;
2543 Found := True;
2544 Expr_Type := Etype (Expression (N));
2546 -- The resolution of a conditional expression that is the operand of a
2547 -- type conversion is determined by the conversion (RM 4.5.7(10/3)).
2549 elsif Nkind (N) in N_Case_Expression | N_If_Expression
2550 and then Nkind (Parent (N)) = N_Type_Conversion
2551 then
2552 Found := True;
2553 Expr_Type := Etype (Parent (N));
2555 -- If not overloaded, then we know the type, and all that needs doing
2556 -- is to check that this type is compatible with the context. But note
2557 -- that we may have an operator with no interpretation in Ada 2022 for
2558 -- the case of possible user-defined literals as operands.
2560 elsif not Is_Overloaded (N) then
2561 if Nkind (N) in N_Op and then No (Entity (N)) then
2562 pragma Assert (Ada_Version >= Ada_2022);
2563 Found := False;
2564 elsif not Comes_From_Source (N) and then
2565 Original_Implementation_Base_Type (Typ) =
2566 Original_Implementation_Base_Type (Etype (N))
2567 then
2568 -- Ignore privacy for streaming or Put_Image routines
2569 Found := True;
2570 else
2571 Found := Covers (Typ, Etype (N));
2572 end if;
2574 Expr_Type := Etype (N);
2576 -- In the overloaded case, we must select the interpretation that
2577 -- is compatible with the context (i.e. the type passed to Resolve)
2579 else
2580 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2582 -- Loop through possible interpretations
2584 Get_First_Interp (N, I, It);
2585 Interp_Loop : while Present (It.Typ) loop
2586 if Debug_Flag_V then
2587 Write_Str ("Interp: ");
2588 Write_Interp (It);
2589 end if;
2591 -- We are only interested in interpretations that are compatible
2592 -- with the expected type, any other interpretations are ignored.
2594 if not Covers (Typ, It.Typ) then
2595 if Debug_Flag_V then
2596 Write_Str (" interpretation incompatible with context");
2597 Write_Eol;
2598 end if;
2600 else
2601 -- Skip the current interpretation if it is disabled by an
2602 -- abstract operator. This action is performed only when the
2603 -- type against which we are resolving is the same as the
2604 -- type of the interpretation.
2606 if Ada_Version >= Ada_2005
2607 and then It.Typ = Typ
2608 and then not Is_Universal_Numeric_Type (Typ)
2609 and then Present (It.Abstract_Op)
2610 then
2611 if Debug_Flag_V then
2612 Write_Line ("Skip.");
2613 end if;
2615 goto Continue;
2616 end if;
2618 -- First matching interpretation
2620 if not Found then
2621 Found := True;
2622 I1 := I;
2623 Seen := It.Nam;
2624 Expr_Type := It.Typ;
2626 -- Matching interpretation that is not the first, maybe an
2627 -- error, but there are some cases where preference rules are
2628 -- used to choose between the two possibilities. These and
2629 -- some more obscure cases are handled in Disambiguate.
2631 else
2632 -- If the current statement is part of a predefined library
2633 -- unit, then all interpretations which come from user level
2634 -- packages should not be considered. Check previous and
2635 -- current one.
2637 if From_Lib then
2638 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2639 goto Continue;
2641 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2643 -- Previous interpretation must be discarded
2645 I1 := I;
2646 Seen := It.Nam;
2647 Expr_Type := It.Typ;
2648 Set_Entity (N, Seen);
2649 goto Continue;
2650 end if;
2651 end if;
2653 -- Otherwise apply further disambiguation steps
2655 Error_Msg_Sloc := Sloc (Seen);
2656 It1 := Disambiguate (N, I1, I, Typ);
2658 -- Disambiguation has succeeded. Skip the remaining
2659 -- interpretations.
2661 if It1 /= No_Interp then
2662 Seen := It1.Nam;
2663 Expr_Type := It1.Typ;
2665 while Present (It.Typ) loop
2666 Get_Next_Interp (I, It);
2667 end loop;
2669 else
2670 -- Before we issue an ambiguity complaint, check for the
2671 -- case of a subprogram call where at least one of the
2672 -- arguments is Any_Type, and if so suppress the message,
2673 -- since it is a cascaded error. This can also happen for
2674 -- a generalized indexing operation.
2676 if Nkind (N) in N_Subprogram_Call
2677 or else (Nkind (N) = N_Indexed_Component
2678 and then Present (Generalized_Indexing (N)))
2679 then
2680 declare
2681 A : Node_Id;
2682 E : Node_Id;
2684 begin
2685 if Nkind (N) = N_Indexed_Component then
2686 Rewrite (N, Generalized_Indexing (N));
2687 end if;
2689 A := First_Actual (N);
2690 while Present (A) loop
2691 E := A;
2693 if Nkind (E) = N_Parameter_Association then
2694 E := Explicit_Actual_Parameter (E);
2695 end if;
2697 if Etype (E) = Any_Type then
2698 if Debug_Flag_V then
2699 Write_Str ("Any_Type in call");
2700 Write_Eol;
2701 end if;
2703 exit Interp_Loop;
2704 end if;
2706 Next_Actual (A);
2707 end loop;
2708 end;
2710 elsif Nkind (N) in N_Binary_Op
2711 and then (Etype (Left_Opnd (N)) = Any_Type
2712 or else Etype (Right_Opnd (N)) = Any_Type)
2713 then
2714 exit Interp_Loop;
2716 elsif Nkind (N) in N_Unary_Op
2717 and then Etype (Right_Opnd (N)) = Any_Type
2718 then
2719 exit Interp_Loop;
2720 end if;
2722 -- Not that special case, so issue message using the flag
2723 -- Ambiguous to control printing of the header message
2724 -- only at the start of an ambiguous set.
2726 if not Ambiguous then
2727 if Nkind (N) = N_Function_Call
2728 and then Nkind (Name (N)) = N_Explicit_Dereference
2729 then
2730 Error_Msg_N
2731 ("ambiguous expression (cannot resolve indirect "
2732 & "call)!", N);
2733 else
2734 Error_Msg_NE -- CODEFIX
2735 ("ambiguous expression (cannot resolve&)!",
2736 N, It.Nam);
2737 end if;
2739 Ambiguous := True;
2741 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2742 Error_Msg_N
2743 ("\\possible interpretation (inherited)#!", N);
2744 else
2745 Error_Msg_N -- CODEFIX
2746 ("\\possible interpretation#!", N);
2747 end if;
2749 if Nkind (N) in N_Subprogram_Call
2750 and then Present (Parameter_Associations (N))
2751 then
2752 Report_Ambiguous_Argument;
2753 end if;
2754 end if;
2756 Error_Msg_Sloc := Sloc (It.Nam);
2758 -- By default, the error message refers to the candidate
2759 -- interpretation. But if it is a predefined operator, it
2760 -- is implicitly declared at the declaration of the type
2761 -- of the operand. Recover the sloc of that declaration
2762 -- for the error message.
2764 if Nkind (N) in N_Op
2765 and then Scope (It.Nam) = Standard_Standard
2766 and then not Is_Overloaded (Right_Opnd (N))
2767 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2768 Standard_Standard
2769 then
2770 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2772 if Comes_From_Source (Err_Type)
2773 and then Present (Parent (Err_Type))
2774 then
2775 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2776 end if;
2778 elsif Nkind (N) in N_Binary_Op
2779 and then Scope (It.Nam) = Standard_Standard
2780 and then not Is_Overloaded (Left_Opnd (N))
2781 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2782 Standard_Standard
2783 then
2784 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2786 if Comes_From_Source (Err_Type)
2787 and then Present (Parent (Err_Type))
2788 then
2789 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2790 end if;
2792 -- If this is an indirect call, use the subprogram_type
2793 -- in the message, to have a meaningful location. Also
2794 -- indicate if this is an inherited operation, created
2795 -- by a type declaration.
2797 elsif Nkind (N) = N_Function_Call
2798 and then Nkind (Name (N)) = N_Explicit_Dereference
2799 and then Is_Type (It.Nam)
2800 then
2801 Err_Type := It.Nam;
2802 Error_Msg_Sloc :=
2803 Sloc (Associated_Node_For_Itype (Err_Type));
2804 else
2805 Err_Type := Empty;
2806 end if;
2808 if Nkind (N) in N_Op
2809 and then Scope (It.Nam) = Standard_Standard
2810 and then Present (Err_Type)
2811 then
2812 -- Special-case the message for universal_fixed
2813 -- operators, which are not declared with the type
2814 -- of the operand, but appear forever in Standard.
2816 if It.Typ = Universal_Fixed
2817 and then Scope (It.Nam) = Standard_Standard
2818 then
2819 Error_Msg_N
2820 ("\\possible interpretation as universal_fixed "
2821 & "operation (RM 4.5.5 (19))", N);
2822 else
2823 Error_Msg_N
2824 ("\\possible interpretation (predefined)#!", N);
2825 end if;
2827 elsif
2828 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2829 then
2830 Error_Msg_N
2831 ("\\possible interpretation (inherited)#!", N);
2832 else
2833 Error_Msg_N -- CODEFIX
2834 ("\\possible interpretation#!", N);
2835 end if;
2837 end if;
2838 end if;
2840 -- We have a matching interpretation, Expr_Type is the type
2841 -- from this interpretation, and Seen is the entity.
2843 -- For an operator, just set the entity name. The type will be
2844 -- set by the specific operator resolution routine.
2846 if Nkind (N) in N_Op then
2847 Set_Entity (N, Seen);
2848 Generate_Reference (Seen, N);
2850 elsif Nkind (N) in N_Case_Expression
2851 | N_Character_Literal
2852 | N_Delta_Aggregate
2853 | N_If_Expression
2854 then
2855 Set_Etype (N, Expr_Type);
2857 -- AI05-0139-2: Expression is overloaded because type has
2858 -- implicit dereference. The context may be the one that
2859 -- requires implicit dereferemce.
2861 elsif Has_Implicit_Dereference (Expr_Type) then
2862 Set_Etype (N, Expr_Type);
2863 Set_Is_Overloaded (N, False);
2865 -- If the expression is an entity, generate a reference
2866 -- to it, as this is not done for an overloaded construct
2867 -- during analysis.
2869 if Is_Entity_Name (N)
2870 and then Comes_From_Source (N)
2871 then
2872 Generate_Reference (Entity (N), N);
2874 -- Examine access discriminants of entity type,
2875 -- to check whether one of them yields the
2876 -- expected type.
2878 declare
2879 Disc : Entity_Id :=
2880 First_Discriminant (Etype (Entity (N)));
2882 begin
2883 while Present (Disc) loop
2884 exit when Is_Access_Type (Etype (Disc))
2885 and then Has_Implicit_Dereference (Disc)
2886 and then Designated_Type (Etype (Disc)) = Typ;
2888 Next_Discriminant (Disc);
2889 end loop;
2891 if Present (Disc) then
2892 Build_Explicit_Dereference (N, Disc);
2893 end if;
2894 end;
2895 end if;
2897 exit Interp_Loop;
2899 elsif Is_Overloaded (N)
2900 and then Present (It.Nam)
2901 and then Ekind (It.Nam) = E_Discriminant
2902 and then Has_Implicit_Dereference (It.Nam)
2903 then
2904 -- If the node is a general indexing, the dereference is
2905 -- is inserted when resolving the rewritten form, else
2906 -- insert it now.
2908 if Nkind (N) /= N_Indexed_Component
2909 or else No (Generalized_Indexing (N))
2910 then
2911 Build_Explicit_Dereference (N, It.Nam);
2912 end if;
2914 -- For an explicit dereference, attribute reference, range,
2915 -- short-circuit form (which is not an operator node), or call
2916 -- with a name that is an explicit dereference, there is
2917 -- nothing to be done at this point.
2919 elsif Nkind (N) in N_Attribute_Reference
2920 | N_And_Then
2921 | N_Explicit_Dereference
2922 | N_Identifier
2923 | N_Indexed_Component
2924 | N_Or_Else
2925 | N_Range
2926 | N_Selected_Component
2927 | N_Slice
2928 or else Nkind (Name (N)) = N_Explicit_Dereference
2929 then
2930 null;
2932 -- For procedure or function calls, set the type of the name,
2933 -- and also the entity pointer for the prefix.
2935 elsif Nkind (N) in N_Subprogram_Call
2936 and then Is_Entity_Name (Name (N))
2937 then
2938 Set_Etype (Name (N), Expr_Type);
2939 Set_Entity (Name (N), Seen);
2940 Generate_Reference (Seen, Name (N));
2942 elsif Nkind (N) = N_Function_Call
2943 and then Nkind (Name (N)) = N_Selected_Component
2944 then
2945 Set_Etype (Name (N), Expr_Type);
2946 Set_Entity (Selector_Name (Name (N)), Seen);
2947 Generate_Reference (Seen, Selector_Name (Name (N)));
2949 -- For all other cases, just set the type of the Name
2951 else
2952 Set_Etype (Name (N), Expr_Type);
2953 end if;
2955 end if;
2957 <<Continue>>
2959 -- Move to next interpretation
2961 exit Interp_Loop when No (It.Typ);
2963 Get_Next_Interp (I, It);
2964 end loop Interp_Loop;
2965 end if;
2967 -- At this stage Found indicates whether or not an acceptable
2968 -- interpretation exists. If not, then we have an error, except that if
2969 -- the context is Any_Type as a result of some other error, then we
2970 -- suppress the error report.
2972 if not Found then
2973 if Typ /= Any_Type then
2975 -- If type we are looking for is Void, then this is the procedure
2976 -- call case, and the error is simply that what we gave is not a
2977 -- procedure name (we think of procedure calls as expressions with
2978 -- types internally, but the user doesn't think of them this way).
2980 if Typ = Standard_Void_Type then
2982 -- Special case message if function used as a procedure
2984 if Nkind (N) = N_Procedure_Call_Statement
2985 and then Is_Entity_Name (Name (N))
2986 and then Ekind (Entity (Name (N))) = E_Function
2987 then
2988 Error_Msg_NE
2989 ("cannot use call to function & as a statement",
2990 Name (N), Entity (Name (N)));
2991 Error_Msg_N
2992 ("\return value of a function call cannot be ignored",
2993 Name (N));
2995 -- Otherwise give general message (not clear what cases this
2996 -- covers, but no harm in providing for them).
2998 else
2999 Error_Msg_N ("expect procedure name in procedure call", N);
3000 end if;
3002 Found := True;
3004 -- Otherwise we do have a subexpression with the wrong type
3006 -- Check for the case of an allocator which uses an access type
3007 -- instead of the designated type. This is a common error and we
3008 -- specialize the message, posting an error on the operand of the
3009 -- allocator, complaining that we expected the designated type of
3010 -- the allocator.
3012 elsif Nkind (N) = N_Allocator
3013 and then Is_Access_Type (Typ)
3014 and then Is_Access_Type (Etype (N))
3015 and then Designated_Type (Etype (N)) = Typ
3016 then
3017 Wrong_Type (Expression (N), Designated_Type (Typ));
3018 Found := True;
3020 -- Check for view mismatch on Null in instances, for which the
3021 -- view-swapping mechanism has no identifier.
3023 elsif (In_Instance or else In_Inlined_Body)
3024 and then Nkind (N) = N_Null
3025 and then Is_Private_Type (Typ)
3026 and then Is_Access_Type (Full_View (Typ))
3027 then
3028 Resolve (N, Full_View (Typ));
3029 Set_Etype (N, Typ);
3030 return;
3032 -- Check for an aggregate. Sometimes we can get bogus aggregates
3033 -- from misuse of parentheses, and we are about to complain about
3034 -- the aggregate without even looking inside it.
3036 -- Instead, if we have an aggregate of type Any_Composite, then
3037 -- analyze and resolve the component fields, and then only issue
3038 -- another message if we get no errors doing this (otherwise
3039 -- assume that the errors in the aggregate caused the problem).
3041 elsif Nkind (N) = N_Aggregate
3042 and then Etype (N) = Any_Composite
3043 then
3044 if Ada_Version >= Ada_2022
3045 and then Has_Aspect (Typ, Aspect_Aggregate)
3046 then
3047 Resolve_Container_Aggregate (N, Typ);
3049 if Expander_Active then
3050 Expand (N);
3051 end if;
3052 return;
3053 end if;
3055 -- Disable expansion in any case. If there is a type mismatch
3056 -- it may be fatal to try to expand the aggregate. The flag
3057 -- would otherwise be set to false when the error is posted.
3059 Expander_Active := False;
3061 declare
3062 procedure Check_Aggr (Aggr : Node_Id);
3063 -- Check one aggregate, and set Found to True if we have a
3064 -- definite error in any of its elements
3066 procedure Check_Elmt (Aelmt : Node_Id);
3067 -- Check one element of aggregate and set Found to True if
3068 -- we definitely have an error in the element.
3070 ----------------
3071 -- Check_Aggr --
3072 ----------------
3074 procedure Check_Aggr (Aggr : Node_Id) is
3075 Elmt : Node_Id;
3077 begin
3078 if Present (Expressions (Aggr)) then
3079 Elmt := First (Expressions (Aggr));
3080 while Present (Elmt) loop
3081 Check_Elmt (Elmt);
3082 Next (Elmt);
3083 end loop;
3084 end if;
3086 if Present (Component_Associations (Aggr)) then
3087 Elmt := First (Component_Associations (Aggr));
3088 while Present (Elmt) loop
3090 -- If this is a default-initialized component, then
3091 -- there is nothing to check. The box will be
3092 -- replaced by the appropriate call during late
3093 -- expansion.
3095 if Nkind (Elmt) /= N_Iterated_Component_Association
3096 and then not Box_Present (Elmt)
3097 then
3098 Check_Elmt (Expression (Elmt));
3099 end if;
3101 Next (Elmt);
3102 end loop;
3103 end if;
3104 end Check_Aggr;
3106 ----------------
3107 -- Check_Elmt --
3108 ----------------
3110 procedure Check_Elmt (Aelmt : Node_Id) is
3111 begin
3112 -- If we have a nested aggregate, go inside it (to
3113 -- attempt a naked analyze-resolve of the aggregate can
3114 -- cause undesirable cascaded errors). Do not resolve
3115 -- expression if it needs a type from context, as for
3116 -- integer * fixed expression.
3118 if Nkind (Aelmt) = N_Aggregate then
3119 Check_Aggr (Aelmt);
3121 else
3122 Analyze (Aelmt);
3124 if not Is_Overloaded (Aelmt)
3125 and then Etype (Aelmt) /= Any_Fixed
3126 then
3127 Resolve (Aelmt);
3128 end if;
3130 if Etype (Aelmt) = Any_Type then
3131 Found := True;
3132 end if;
3133 end if;
3134 end Check_Elmt;
3136 begin
3137 Check_Aggr (N);
3138 end;
3139 end if;
3141 -- Check whether the node is a literal or a named number or a
3142 -- conditional expression whose dependent expressions are all
3143 -- literals or named numbers.
3145 if Try_User_Defined_Literal (N, Typ) then
3146 return;
3147 end if;
3149 -- Looks like we have a type error, but check for special case
3150 -- of Address wanted, integer found, with the configuration pragma
3151 -- Allow_Integer_Address active. If we have this case, introduce
3152 -- an unchecked conversion to allow the integer expression to be
3153 -- treated as an Address. The reverse case of integer wanted,
3154 -- Address found, is treated in an analogous manner.
3156 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3157 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3158 Analyze_And_Resolve (N, Typ);
3159 return;
3161 -- Under relaxed RM semantics silently replace occurrences of null
3162 -- by System.Null_Address.
3164 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3165 Replace_Null_By_Null_Address (N);
3166 Analyze_And_Resolve (N, Typ);
3167 return;
3168 end if;
3170 -- That special Allow_Integer_Address check did not apply, so we
3171 -- have a real type error. If an error message was issued already,
3172 -- Found got reset to True, so if it's still False, issue standard
3173 -- Wrong_Type message.
3175 if not Found then
3176 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3177 declare
3178 Subp_Name : Node_Id;
3180 begin
3181 if Is_Entity_Name (Name (N)) then
3182 Subp_Name := Name (N);
3184 elsif Nkind (Name (N)) = N_Selected_Component then
3186 -- Protected operation: retrieve operation name
3188 Subp_Name := Selector_Name (Name (N));
3190 else
3191 raise Program_Error;
3192 end if;
3194 Error_Msg_Node_2 := Typ;
3195 Error_Msg_NE
3196 ("no visible interpretation of& matches expected type&",
3197 N, Subp_Name);
3198 end;
3200 if All_Errors_Mode then
3201 declare
3202 Index : Interp_Index;
3203 It : Interp;
3205 begin
3206 Error_Msg_N ("\\possible interpretations:", N);
3208 Get_First_Interp (Name (N), Index, It);
3209 while Present (It.Nam) loop
3210 Error_Msg_Sloc := Sloc (It.Nam);
3211 Error_Msg_Node_2 := It.Nam;
3212 Error_Msg_NE
3213 ("\\ type& for & declared#", N, It.Typ);
3214 Get_Next_Interp (Index, It);
3215 end loop;
3216 end;
3218 else
3219 Error_Msg_N ("\use -gnatf for details", N);
3220 end if;
3222 -- Recognize the case of a quantified expression being mistaken
3223 -- for an iterated component association because the user
3224 -- forgot the "all" or "some" keyword after "for". Because the
3225 -- error message starts with "missing ALL", we automatically
3226 -- benefit from the associated CODEFIX, which requires that
3227 -- the message is located on the identifier following "for"
3228 -- in order for the CODEFIX to insert "all" in the right place.
3230 elsif Nkind (N) = N_Aggregate
3231 and then List_Length (Component_Associations (N)) = 1
3232 and then Nkind (First (Component_Associations (N)))
3233 = N_Iterated_Component_Association
3234 and then Is_Boolean_Type (Typ)
3235 then
3236 if Present
3237 (Iterator_Specification
3238 (First (Component_Associations (N))))
3239 then
3240 Error_Msg_N -- CODEFIX
3241 ("missing ALL or SOME in quantified expression",
3242 Defining_Identifier
3243 (Iterator_Specification
3244 (First (Component_Associations (N)))));
3245 else
3246 Error_Msg_N -- CODEFIX
3247 ("missing ALL or SOME in quantified expression",
3248 Defining_Identifier
3249 (First (Component_Associations (N))));
3250 end if;
3252 -- For an operator with no interpretation, check whether one of
3253 -- its operands may be a user-defined literal.
3255 elsif Nkind (N) in N_Op and then No (Entity (N)) then
3256 if Try_User_Defined_Literal_For_Operator (N, Typ) then
3257 return;
3258 else
3259 Unresolved_Operator (N);
3260 end if;
3262 else
3263 Wrong_Type (N, Typ);
3264 end if;
3265 end if;
3266 end if;
3268 Resolution_Failed;
3269 return;
3271 -- Test if we have more than one interpretation for the context
3273 elsif Ambiguous then
3274 Resolution_Failed;
3275 return;
3277 -- Only one interpretation
3279 else
3280 -- Prevent implicit conversions between access-to-subprogram types
3281 -- with different strub modes. Explicit conversions are acceptable in
3282 -- some circumstances. We don't have to be concerned about data or
3283 -- access-to-data types. Conversions between data types can safely
3284 -- drop or add strub attributes from types, because strub effects are
3285 -- associated with the locations rather than values. E.g., converting
3286 -- a hypothetical Strub_Integer variable to Integer would load the
3287 -- value from the variable, enabling stack scrabbing for the
3288 -- enclosing subprogram, and then convert the value to Integer. As
3289 -- for conversions between access-to-data types, that's no different
3290 -- from any other case of type punning.
3292 if Is_Access_Type (Typ)
3293 and then Ekind (Designated_Type (Typ)) = E_Subprogram_Type
3294 and then Is_Access_Type (Expr_Type)
3295 and then Ekind (Designated_Type (Expr_Type)) = E_Subprogram_Type
3296 then
3297 Check_Same_Strub_Mode
3298 (Designated_Type (Typ), Designated_Type (Expr_Type));
3299 end if;
3301 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3302 -- the "+" on T is abstract, and the operands are of universal type,
3303 -- the above code will have (incorrectly) resolved the "+" to the
3304 -- universal one in Standard. Therefore check for this case and give
3305 -- an error. We can't do this earlier, because it would cause legal
3306 -- cases to get errors (when some other type has an abstract "+").
3308 if Ada_Version >= Ada_2005
3309 and then Nkind (N) in N_Op
3310 and then Is_Overloaded (N)
3311 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3312 then
3313 Get_First_Interp (N, I, It);
3314 while Present (It.Typ) loop
3315 if Present (It.Abstract_Op)
3316 and then Etype (It.Abstract_Op) = Typ
3317 then
3318 Nondispatching_Call_To_Abstract_Operation
3319 (N, It.Abstract_Op);
3320 return;
3321 end if;
3323 Get_Next_Interp (I, It);
3324 end loop;
3325 end if;
3327 -- Here we have an acceptable interpretation for the context
3329 -- Propagate type information and normalize tree for various
3330 -- predefined operations. If the context only imposes a class of
3331 -- types, rather than a specific type, propagate the actual type
3332 -- downward.
3334 if Typ = Any_Integer or else
3335 Typ = Any_Boolean or else
3336 Typ = Any_Modular or else
3337 Typ = Any_Real or else
3338 Typ = Any_Discrete
3339 then
3340 Ctx_Type := Expr_Type;
3342 -- Any_Fixed is legal in a real context only if a specific fixed-
3343 -- point type is imposed. If Norman Cohen can be confused by this,
3344 -- it deserves a separate message.
3346 if Typ = Any_Real
3347 and then Expr_Type = Any_Fixed
3348 then
3349 Error_Msg_N ("illegal context for mixed mode operation", N);
3350 Set_Etype (N, Universal_Real);
3351 Ctx_Type := Universal_Real;
3352 end if;
3353 end if;
3355 -- A user-defined operator is transformed into a function call at
3356 -- this point, so that further processing knows that operators are
3357 -- really operators (i.e. are predefined operators). User-defined
3358 -- operators that are intrinsic are just renamings of the predefined
3359 -- ones, and need not be turned into calls either, but if they rename
3360 -- a different operator, we must transform the node accordingly.
3361 -- Instantiations of Unchecked_Conversion are intrinsic but are
3362 -- treated as functions, even if given an operator designator.
3364 if Nkind (N) in N_Op
3365 and then Present (Entity (N))
3366 and then Ekind (Entity (N)) /= E_Operator
3367 then
3368 if not Is_Predefined_Op (Entity (N)) then
3369 Rewrite_Operator_As_Call (N, Entity (N));
3371 elsif Present (Alias (Entity (N)))
3372 and then
3373 Nkind (Parent (Parent (Entity (N)))) =
3374 N_Subprogram_Renaming_Declaration
3375 then
3376 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3378 -- If the node is rewritten, it will be fully resolved in
3379 -- Rewrite_Renamed_Operator.
3381 if Analyzed (N) then
3382 return;
3383 end if;
3384 end if;
3385 end if;
3387 case N_Subexpr'(Nkind (N)) is
3388 when N_Aggregate =>
3389 Resolve_Aggregate (N, Ctx_Type);
3391 when N_Allocator =>
3392 Resolve_Allocator (N, Ctx_Type);
3394 when N_Short_Circuit =>
3395 Resolve_Short_Circuit (N, Ctx_Type);
3397 when N_Attribute_Reference =>
3398 Resolve_Attribute (N, Ctx_Type);
3400 when N_Case_Expression =>
3401 Resolve_Case_Expression (N, Ctx_Type);
3403 when N_Character_Literal =>
3404 Resolve_Character_Literal (N, Ctx_Type);
3406 when N_Delta_Aggregate =>
3407 Resolve_Delta_Aggregate (N, Ctx_Type);
3409 when N_Expanded_Name =>
3410 Resolve_Entity_Name (N, Ctx_Type);
3412 when N_Explicit_Dereference =>
3413 Resolve_Explicit_Dereference (N, Ctx_Type);
3415 when N_Expression_With_Actions =>
3416 Resolve_Expression_With_Actions (N, Ctx_Type);
3418 when N_Extension_Aggregate =>
3419 Resolve_Extension_Aggregate (N, Ctx_Type);
3421 when N_External_Initializer =>
3422 Resolve_External_Initializer (N, Ctx_Type);
3424 when N_Function_Call =>
3425 Resolve_Call (N, Ctx_Type);
3427 when N_Identifier =>
3428 Resolve_Entity_Name (N, Ctx_Type);
3430 when N_If_Expression =>
3431 Resolve_If_Expression (N, Ctx_Type);
3433 when N_Indexed_Component =>
3434 Resolve_Indexed_Component (N, Ctx_Type);
3436 when N_Integer_Literal =>
3437 Resolve_Integer_Literal (N, Ctx_Type);
3439 when N_Membership_Test =>
3440 Resolve_Membership_Op (N, Ctx_Type);
3442 when N_Null =>
3443 Resolve_Null (N, Ctx_Type);
3445 when N_Op_And
3446 | N_Op_Or
3447 | N_Op_Xor
3449 Resolve_Logical_Op (N, Ctx_Type);
3451 when N_Op_Eq
3452 | N_Op_Ne
3454 Resolve_Equality_Op (N, Ctx_Type);
3456 when N_Op_Ge
3457 | N_Op_Gt
3458 | N_Op_Le
3459 | N_Op_Lt
3461 Resolve_Comparison_Op (N, Ctx_Type);
3463 when N_Op_Not =>
3464 Resolve_Op_Not (N, Ctx_Type);
3466 when N_Op_Add
3467 | N_Op_Divide
3468 | N_Op_Mod
3469 | N_Op_Multiply
3470 | N_Op_Rem
3471 | N_Op_Subtract
3473 Resolve_Arithmetic_Op (N, Ctx_Type);
3475 when N_Op_Concat =>
3476 Resolve_Op_Concat (N, Ctx_Type);
3478 when N_Op_Expon =>
3479 Resolve_Op_Expon (N, Ctx_Type);
3481 when N_Op_Abs
3482 | N_Op_Minus
3483 | N_Op_Plus
3485 Resolve_Unary_Op (N, Ctx_Type);
3487 when N_Op_Shift =>
3488 Resolve_Shift (N, Ctx_Type);
3490 when N_Procedure_Call_Statement =>
3491 Resolve_Call (N, Ctx_Type);
3493 when N_Operator_Symbol =>
3494 Resolve_Operator_Symbol (N, Ctx_Type);
3496 when N_Qualified_Expression =>
3497 Resolve_Qualified_Expression (N, Ctx_Type);
3499 -- Why is the following null, needs a comment ???
3501 when N_Quantified_Expression =>
3502 null;
3504 when N_Raise_Expression =>
3505 Resolve_Raise_Expression (N, Ctx_Type);
3507 when N_Raise_xxx_Error =>
3508 Set_Etype (N, Ctx_Type);
3510 when N_Range =>
3511 Resolve_Range (N, Ctx_Type);
3513 when N_Real_Literal =>
3514 Resolve_Real_Literal (N, Ctx_Type);
3516 when N_Reference =>
3517 Resolve_Reference (N, Ctx_Type);
3519 when N_Selected_Component =>
3520 Resolve_Selected_Component (N, Ctx_Type);
3522 when N_Slice =>
3523 Resolve_Slice (N, Ctx_Type);
3525 when N_String_Literal =>
3526 Resolve_String_Literal (N, Ctx_Type);
3528 when N_Interpolated_String_Literal =>
3529 Resolve_Interpolated_String_Literal (N, Ctx_Type);
3531 when N_Target_Name =>
3532 Resolve_Target_Name (N, Ctx_Type);
3534 when N_Type_Conversion =>
3535 Resolve_Type_Conversion (N, Ctx_Type);
3537 when N_Unchecked_Expression =>
3538 Resolve_Unchecked_Expression (N, Ctx_Type);
3540 when N_Unchecked_Type_Conversion =>
3541 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3542 end case;
3544 -- Mark relevant use-type and use-package clauses as effective using
3545 -- the original node because constant folding may have occurred and
3546 -- removed references that need to be examined.
3548 if Nkind (Original_Node (N)) in N_Op then
3549 Mark_Use_Clauses (Original_Node (N));
3550 end if;
3552 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3553 -- expression of an anonymous access type that occurs in the context
3554 -- of a named general access type, except when the expression is that
3555 -- of a membership test. This ensures proper legality checking in
3556 -- terms of allowed conversions (expressions that would be illegal to
3557 -- convert implicitly are allowed in membership tests).
3559 if Ada_Version >= Ada_2012
3560 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3561 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3562 and then Nkind (Parent (N)) not in N_Membership_Test
3563 then
3564 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3565 Analyze_And_Resolve (N, Ctx_Type);
3566 end if;
3568 -- If the subexpression was replaced by a non-subexpression, then
3569 -- all we do is to expand it. The only legitimate case we know of
3570 -- is converting procedure call statement to entry call statements,
3571 -- but there may be others, so we are making this test general.
3573 if Nkind (N) not in N_Subexpr then
3574 Debug_A_Exit ("resolving ", N, " (done)");
3575 Expand (N);
3576 return;
3577 end if;
3579 -- The expression is definitely NOT overloaded at this point, so
3580 -- we reset the Is_Overloaded flag to avoid any confusion when
3581 -- reanalyzing the node.
3583 Set_Is_Overloaded (N, False);
3585 -- Freeze expression type, entity if it is a name, and designated
3586 -- type if it is an allocator (RM 13.14(10,11,13)).
3588 -- Now that the resolution of the type of the node is complete, and
3589 -- we did not detect an error, we can expand this node. We skip the
3590 -- expand call if we are in a default expression, see section
3591 -- "Handling of Default Expressions" in Sem spec.
3593 Debug_A_Exit ("resolving ", N, " (done)");
3595 -- We unconditionally freeze the expression, even if we are in
3596 -- default expression mode (the Freeze_Expression routine tests this
3597 -- flag and only freezes static types if it is set).
3599 -- Ada 2012 (AI05-177): The declaration of an expression function
3600 -- does not cause freezing, but we never reach here in that case.
3601 -- Here we are resolving the corresponding expanded body, so we do
3602 -- need to perform normal freezing.
3604 -- As elsewhere we do not emit freeze node within a generic.
3606 if not Inside_A_Generic then
3607 Freeze_Expression (N);
3608 end if;
3610 -- Now we can do the expansion
3612 Expand (N);
3613 end if;
3614 end Resolve;
3616 -------------
3617 -- Resolve --
3618 -------------
3620 -- Version with check(s) suppressed
3622 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3623 begin
3624 if Suppress = All_Checks then
3625 declare
3626 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3627 begin
3628 Scope_Suppress.Suppress := (others => True);
3629 Resolve (N, Typ);
3630 Scope_Suppress.Suppress := Sva;
3631 end;
3633 else
3634 declare
3635 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3636 begin
3637 Scope_Suppress.Suppress (Suppress) := True;
3638 Resolve (N, Typ);
3639 Scope_Suppress.Suppress (Suppress) := Svg;
3640 end;
3641 end if;
3642 end Resolve;
3644 -------------
3645 -- Resolve --
3646 -------------
3648 -- Version with implicit type
3650 procedure Resolve (N : Node_Id) is
3651 begin
3652 Resolve (N, Etype (N));
3653 end Resolve;
3655 ---------------------
3656 -- Resolve_Actuals --
3657 ---------------------
3659 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3660 Loc : constant Source_Ptr := Sloc (N);
3661 A : Node_Id;
3662 A_Typ : Entity_Id := Empty; -- init to avoid warning
3663 F : Entity_Id;
3664 F_Typ : Entity_Id;
3665 Prev : Node_Id := Empty;
3666 Orig_A : Node_Id;
3667 Real_F : Entity_Id := Empty; -- init to avoid warning
3669 Real_Subp : Entity_Id;
3670 -- If the subprogram being called is an inherited operation for
3671 -- a formal derived type in an instance, Real_Subp is the subprogram
3672 -- that will be called. It may have different formal names than the
3673 -- operation of the formal in the generic, so after actual is resolved
3674 -- the name of the actual in a named association must carry the name
3675 -- of the actual of the subprogram being called.
3677 procedure Check_Aliased_Parameter;
3678 -- Check rules on aliased parameters and related accessibility rules
3679 -- in (RM 3.10.2 (10.2-10.4)).
3681 procedure Check_Argument_Order;
3682 -- Performs a check for the case where the actuals are all simple
3683 -- identifiers that correspond to the formal names, but in the wrong
3684 -- order, which is considered suspicious and cause for a warning.
3686 procedure Check_Prefixed_Call;
3687 -- If the original node is an overloaded call in prefix notation,
3688 -- insert an 'Access or a dereference as needed over the first actual.
3689 -- Try_Object_Operation has already verified that there is a valid
3690 -- interpretation, but the form of the actual can only be determined
3691 -- once the primitive operation is identified.
3693 procedure Insert_Default;
3694 -- If the actual is missing in a call, insert in the actuals list
3695 -- an instance of the default expression. The insertion is always
3696 -- a named association.
3698 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3699 -- Check whether T1 and T2, or their full views, are derived from a
3700 -- common type. Used to enforce the restrictions on array conversions
3701 -- of AI95-00246.
3703 function Static_Concatenation (N : Node_Id) return Boolean;
3704 -- Predicate to determine whether an actual that is a concatenation
3705 -- will be evaluated statically and does not need a transient scope.
3706 -- This must be determined before the actual is resolved and expanded
3707 -- because if needed the transient scope must be introduced earlier.
3709 -----------------------------
3710 -- Check_Aliased_Parameter --
3711 -----------------------------
3713 procedure Check_Aliased_Parameter is
3714 Nominal_Subt : Entity_Id;
3716 begin
3717 if Is_Aliased (F) then
3718 if Is_Tagged_Type (A_Typ) then
3719 null;
3721 elsif Is_Aliased_View (A) then
3722 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3723 Nominal_Subt := Base_Type (A_Typ);
3724 else
3725 Nominal_Subt := A_Typ;
3726 end if;
3728 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3729 null;
3731 -- In a generic body assume the worst for generic formals:
3732 -- they can have a constrained partial view (AI05-041).
3734 elsif Has_Discriminants (F_Typ)
3735 and then not Is_Constrained (F_Typ)
3736 and then not Object_Type_Has_Constrained_Partial_View
3737 (Typ => F_Typ, Scop => Current_Scope)
3738 then
3739 null;
3741 else
3742 Error_Msg_NE ("untagged actual does not statically match "
3743 & "aliased formal&", A, F);
3744 end if;
3746 else
3747 Error_Msg_NE ("actual for aliased formal& must be "
3748 & "aliased object", A, F);
3749 end if;
3751 if Ekind (Nam) = E_Procedure then
3752 null;
3754 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3755 if Nkind (Parent (N)) = N_Type_Conversion
3756 and then Type_Access_Level (Etype (Parent (N)))
3757 < Static_Accessibility_Level (A, Object_Decl_Level)
3758 then
3759 Error_Msg_N ("aliased actual has wrong accessibility", A);
3760 end if;
3762 elsif Nkind (Parent (N)) = N_Qualified_Expression
3763 and then Nkind (Parent (Parent (N))) = N_Allocator
3764 and then Type_Access_Level (Etype (Parent (Parent (N))))
3765 < Static_Accessibility_Level (A, Object_Decl_Level)
3766 then
3767 Error_Msg_N
3768 ("aliased actual in allocator has wrong accessibility", A);
3769 end if;
3770 end if;
3771 end Check_Aliased_Parameter;
3773 --------------------------
3774 -- Check_Argument_Order --
3775 --------------------------
3777 procedure Check_Argument_Order is
3778 begin
3779 -- Nothing to do if no parameters, or original node is neither a
3780 -- function call nor a procedure call statement (happens in the
3781 -- operator-transformed-to-function call case), or the call is to an
3782 -- operator symbol (which is usually in infix form), or the call does
3783 -- not come from source, or this warning is off.
3785 if not Warn_On_Parameter_Order
3786 or else No (Parameter_Associations (N))
3787 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3788 or else (Nkind (Name (N)) = N_Identifier
3789 and then Present (Entity (Name (N)))
3790 and then Nkind (Entity (Name (N))) =
3791 N_Defining_Operator_Symbol)
3792 or else not Comes_From_Source (N)
3793 then
3794 return;
3795 end if;
3797 declare
3798 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3800 begin
3801 -- Nothing to do if only one parameter
3803 if Nargs < 2 then
3804 return;
3805 end if;
3807 -- Here if at least two arguments
3809 declare
3810 Actuals : array (1 .. Nargs) of Node_Id;
3811 Actual : Node_Id;
3812 Formal : Node_Id;
3814 Wrong_Order : Boolean := False;
3815 -- Set True if an out of order case is found
3817 begin
3818 -- Collect identifier names of actuals, fail if any actual is
3819 -- not a simple identifier, and record max length of name.
3821 Actual := First (Parameter_Associations (N));
3822 for J in Actuals'Range loop
3823 if Nkind (Actual) /= N_Identifier then
3824 return;
3825 else
3826 Actuals (J) := Actual;
3827 Next (Actual);
3828 end if;
3829 end loop;
3831 -- If we got this far, all actuals are identifiers and the list
3832 -- of their names is stored in the Actuals array.
3834 Formal := First_Formal (Nam);
3835 for J in Actuals'Range loop
3837 -- If we ran out of formals, that's odd, probably an error
3838 -- which will be detected elsewhere, but abandon the search.
3840 if No (Formal) then
3841 return;
3842 end if;
3844 -- If name matches and is in order OK
3846 if Chars (Formal) = Chars (Actuals (J)) then
3847 null;
3849 else
3850 -- If no match, see if it is elsewhere in list and if so
3851 -- flag potential wrong order if type is compatible.
3853 for K in Actuals'Range loop
3854 if Chars (Formal) = Chars (Actuals (K))
3855 and then
3856 Has_Compatible_Type (Actuals (K), Etype (Formal))
3857 then
3858 Wrong_Order := True;
3859 goto Continue;
3860 end if;
3861 end loop;
3863 -- No match
3865 return;
3866 end if;
3868 <<Continue>> Next_Formal (Formal);
3869 end loop;
3871 -- If Formals left over, also probably an error, skip warning
3873 if Present (Formal) then
3874 return;
3875 end if;
3877 -- Here we give the warning if something was out of order
3879 if Wrong_Order then
3880 Error_Msg_N
3881 ("?.p?actuals for this call may be in wrong order", N);
3882 end if;
3883 end;
3884 end;
3885 end Check_Argument_Order;
3887 -------------------------
3888 -- Check_Prefixed_Call --
3889 -------------------------
3891 procedure Check_Prefixed_Call is
3892 Actual : constant Node_Id := First_Actual (N);
3893 Actual_Type : constant Entity_Id := Etype (Actual);
3894 Formal_Type : constant Entity_Id := Etype (First_Formal (Nam));
3895 New_Actual : Node_Id;
3897 begin
3898 -- Check whether the call is a prefixed call, with or without
3899 -- additional actuals.
3901 if Is_Expanded_Prefixed_Call (N) then
3903 -- Introduce dereference on object in prefix
3905 if Is_Access_Type (Actual_Type)
3906 and then not Is_Access_Type (Formal_Type)
3907 then
3908 New_Actual :=
3909 Make_Explicit_Dereference (Sloc (Actual),
3910 Prefix => Relocate_Node (Actual));
3911 Rewrite (Actual, New_Actual);
3912 Analyze (Actual);
3914 -- Conversely, if the formal is an access parameter and the object
3915 -- is not an access type or a reference type (i.e. a type with the
3916 -- Implicit_Dereference aspect specified), add an implicit 'Access
3917 -- to the prefix. Its analysis will check that the object is
3918 -- aliased.
3920 elsif Is_Access_Type (Formal_Type)
3921 and then not Is_Access_Type (Actual_Type)
3922 and then (not Has_Implicit_Dereference (Actual_Type)
3923 or else
3924 not Is_Access_Type
3925 (Designated_Type
3926 (Etype (Get_Reference_Discriminant (Actual_Type)))))
3927 then
3928 Rewrite (Actual,
3929 Make_Attribute_Reference (Loc,
3930 Attribute_Name => Name_Access,
3931 Prefix => Relocate_Node (Actual)));
3932 Analyze (Actual);
3933 end if;
3934 end if;
3935 end Check_Prefixed_Call;
3937 --------------------
3938 -- Insert_Default --
3939 --------------------
3941 procedure Insert_Default is
3942 Actval : Node_Id;
3943 Assoc : Node_Id;
3945 begin
3946 -- Missing argument in call, nothing to insert
3948 if No (Default_Value (F)) then
3949 return;
3951 else
3952 -- Note that we do a full New_Copy_Tree, so that any associated
3953 -- Itypes are properly copied. This may not be needed any more,
3954 -- but it does no harm as a safety measure. Defaults of a generic
3955 -- formal may be out of bounds of the corresponding actual (see
3956 -- cc1311b) and an additional check may be required.
3958 Actval :=
3959 New_Copy_Tree
3960 (Default_Value (F),
3961 New_Scope => Current_Scope,
3962 New_Sloc => Loc);
3964 -- Propagate dimension information, if any.
3966 Copy_Dimensions (Default_Value (F), Actval);
3968 if Is_Concurrent_Type (Scope (Nam))
3969 and then Has_Discriminants (Scope (Nam))
3970 then
3971 Replace_Actual_Discriminants (N, Actval);
3972 end if;
3974 if Is_Overloadable (Nam)
3975 and then Present (Alias (Nam))
3976 then
3977 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3978 and then not Is_Tagged_Type (Etype (F))
3979 then
3980 -- If default is a real literal, do not introduce a
3981 -- conversion whose effect may depend on the run-time
3982 -- size of universal real.
3984 if Nkind (Actval) = N_Real_Literal then
3985 Set_Etype (Actval, Base_Type (Etype (F)));
3986 else
3987 Actval := Unchecked_Convert_To (Etype (F), Actval);
3988 end if;
3989 end if;
3991 if Is_Scalar_Type (Etype (F)) then
3992 Enable_Range_Check (Actval);
3993 end if;
3995 Set_Parent (Actval, N);
3997 -- Resolve aggregates with their base type, to avoid scope
3998 -- anomalies: the subtype was first built in the subprogram
3999 -- declaration, and the current call may be nested.
4001 if Nkind (Actval) = N_Aggregate then
4002 Analyze_And_Resolve (Actval, Etype (F));
4003 else
4004 Analyze_And_Resolve (Actval, Etype (Actval));
4005 end if;
4007 else
4008 Set_Parent (Actval, N);
4010 -- See note above concerning aggregates
4012 if Nkind (Actval) = N_Aggregate
4013 and then Has_Discriminants (Etype (Actval))
4014 then
4015 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
4017 -- Resolve entities with their own type, which may differ from
4018 -- the type of a reference in a generic context because of the
4019 -- trick used in Save_Global_References.Set_Global_Type to set
4020 -- full views forcefully, which did not anticipate the need to
4021 -- re-analyze default values in calls.
4023 elsif Is_Entity_Name (Actval) then
4024 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
4026 -- Ditto for calls whose name is an entity, for the same reason
4028 elsif Nkind (Actval) = N_Function_Call
4029 and then Is_Entity_Name (Name (Actval))
4030 then
4031 Analyze_And_Resolve (Actval, Etype (Entity (Name (Actval))));
4033 else
4034 Analyze_And_Resolve (Actval, Etype (Actval));
4035 end if;
4036 end if;
4038 -- If default is a tag indeterminate function call, propagate tag
4039 -- to obtain proper dispatching.
4041 if Is_Controlling_Formal (F)
4042 and then Nkind (Default_Value (F)) = N_Function_Call
4043 then
4044 Set_Is_Controlling_Actual (Actval);
4045 end if;
4046 end if;
4048 -- If the default expression raises constraint error, then just
4049 -- silently replace it with an N_Raise_Constraint_Error node, since
4050 -- we already gave the warning on the subprogram spec. If node is
4051 -- already a Raise_Constraint_Error leave as is, to prevent loops in
4052 -- the warnings removal machinery.
4054 if Raises_Constraint_Error (Actval)
4055 and then Nkind (Actval) /= N_Raise_Constraint_Error
4056 then
4057 Rewrite (Actval,
4058 Make_Raise_Constraint_Error (Loc,
4059 Reason => CE_Range_Check_Failed));
4061 Set_Raises_Constraint_Error (Actval);
4062 Set_Etype (Actval, Etype (F));
4063 end if;
4065 Assoc :=
4066 Make_Parameter_Association (Loc,
4067 Explicit_Actual_Parameter => Actval,
4068 Selector_Name => Make_Identifier (Loc, Chars (F)));
4070 -- Case of insertion is first named actual
4072 if No (Prev)
4073 or else Nkind (Parent (Prev)) /= N_Parameter_Association
4074 then
4075 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
4076 Set_First_Named_Actual (N, Actval);
4078 if No (Prev) then
4079 if No (Parameter_Associations (N)) then
4080 Set_Parameter_Associations (N, New_List (Assoc));
4081 else
4082 Append (Assoc, Parameter_Associations (N));
4083 end if;
4085 else
4086 Insert_After (Prev, Assoc);
4087 end if;
4089 -- Case of insertion is not first named actual
4091 else
4092 Set_Next_Named_Actual
4093 (Assoc, Next_Named_Actual (Parent (Prev)));
4094 Set_Next_Named_Actual (Parent (Prev), Actval);
4095 Append (Assoc, Parameter_Associations (N));
4096 end if;
4098 Mark_Rewrite_Insertion (Assoc);
4099 Mark_Rewrite_Insertion (Actval);
4101 Prev := Actval;
4102 end Insert_Default;
4104 -------------------
4105 -- Same_Ancestor --
4106 -------------------
4108 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
4109 FT1 : Entity_Id := T1;
4110 FT2 : Entity_Id := T2;
4112 begin
4113 if Is_Private_Type (T1)
4114 and then Present (Full_View (T1))
4115 then
4116 FT1 := Full_View (T1);
4117 end if;
4119 if Is_Private_Type (T2)
4120 and then Present (Full_View (T2))
4121 then
4122 FT2 := Full_View (T2);
4123 end if;
4125 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
4126 end Same_Ancestor;
4128 --------------------------
4129 -- Static_Concatenation --
4130 --------------------------
4132 function Static_Concatenation (N : Node_Id) return Boolean is
4133 begin
4134 case Nkind (N) is
4135 when N_String_Literal =>
4136 return True;
4138 when N_Op_Concat =>
4140 -- Concatenation is static when both operands are static and
4141 -- the concatenation operator is a predefined one.
4143 return Scope (Entity (N)) = Standard_Standard
4144 and then
4145 Static_Concatenation (Left_Opnd (N))
4146 and then
4147 Static_Concatenation (Right_Opnd (N));
4149 when others =>
4150 if Is_Entity_Name (N) then
4151 declare
4152 Ent : constant Entity_Id := Entity (N);
4153 begin
4154 return Ekind (Ent) = E_Constant
4155 and then Present (Constant_Value (Ent))
4156 and then
4157 Is_OK_Static_Expression (Constant_Value (Ent));
4158 end;
4160 else
4161 return False;
4162 end if;
4163 end case;
4164 end Static_Concatenation;
4166 -- Start of processing for Resolve_Actuals
4168 begin
4169 Check_Argument_Order;
4171 if Is_Overloadable (Nam)
4172 and then Is_Inherited_Operation (Nam)
4173 and then In_Instance
4174 and then Present (Alias (Nam))
4175 and then Present (Overridden_Operation (Alias (Nam)))
4176 then
4177 Real_Subp := Alias (Nam);
4178 else
4179 Real_Subp := Empty;
4180 end if;
4182 if Present (First_Actual (N)) then
4183 Check_Prefixed_Call;
4184 end if;
4186 A := First_Actual (N);
4187 F := First_Formal (Nam);
4189 if Present (Real_Subp) then
4190 Real_F := First_Formal (Real_Subp);
4191 end if;
4193 while Present (F) loop
4194 if No (A) and then Needs_No_Actuals (Nam) then
4195 null;
4197 -- If we have an error in any formal or actual, indicated by a type
4198 -- of Any_Type, then abandon resolution attempt, and set result type
4199 -- to Any_Type.
4201 elsif Etype (F) = Any_Type then
4202 Set_Etype (N, Any_Type);
4203 return;
4205 elsif Present (A) and then Etype (A) = Any_Type then
4206 -- For the peculiar case of a user-defined comparison or equality
4207 -- operator that does not return a boolean type, the operands may
4208 -- have been ambiguous for the predefined operator and, therefore,
4209 -- marked with Any_Type. Since the operation has been resolved to
4210 -- the user-defined operator, that is irrelevant, so reset Etype.
4212 if Nkind (Original_Node (N)) in N_Op_Compare
4213 and then not Is_Boolean_Type (Etype (N))
4214 then
4215 Set_Etype (A, Etype (F));
4217 -- Also skip this if the actual is a Raise_Expression, whose type
4218 -- is imposed from context.
4220 elsif Nkind (A) = N_Raise_Expression then
4221 null;
4223 else
4224 Set_Etype (N, Any_Type);
4225 return;
4226 end if;
4227 end if;
4229 -- Case where actual is present
4231 -- If the actual is an entity, generate a reference to it now. We
4232 -- do this before the actual is resolved, because a formal of some
4233 -- protected subprogram, or a task discriminant, will be rewritten
4234 -- during expansion, and the source entity reference may be lost.
4236 if Present (A)
4237 and then Is_Entity_Name (A)
4238 and then Comes_From_Source (A)
4239 then
4240 -- Annotate the tree by creating a variable reference marker when
4241 -- the actual denotes a variable reference, in case the reference
4242 -- is folded or optimized away. The variable reference marker is
4243 -- automatically saved for later examination by the ABE Processing
4244 -- phase. The status of the reference is set as follows:
4246 -- status mode
4247 -- read IN, IN OUT
4248 -- write IN OUT, OUT
4250 if Needs_Variable_Reference_Marker
4251 (N => A,
4252 Calls_OK => True)
4253 then
4254 Build_Variable_Reference_Marker
4255 (N => A,
4256 Read => Ekind (F) /= E_Out_Parameter,
4257 Write => Ekind (F) /= E_In_Parameter);
4258 end if;
4260 Orig_A := Entity (A);
4262 if Present (Orig_A) then
4263 if Is_Formal (Orig_A)
4264 and then Ekind (F) /= E_In_Parameter
4265 then
4266 Generate_Reference (Orig_A, A, 'm');
4268 elsif not Is_Overloaded (A) then
4269 if Ekind (F) /= E_Out_Parameter then
4270 Generate_Reference (Orig_A, A);
4272 -- RM 6.4.1(12): For an out parameter that is passed by
4273 -- copy, the formal parameter object is created, and:
4275 -- * For an access type, the formal parameter is initialized
4276 -- from the value of the actual, without checking that the
4277 -- value satisfies any constraint, any predicate, or any
4278 -- exclusion of the null value.
4280 -- * For a scalar type that has the Default_Value aspect
4281 -- specified, the formal parameter is initialized from the
4282 -- value of the actual, without checking that the value
4283 -- satisfies any constraint or any predicate.
4284 -- I do not understand why this case is included??? this is
4285 -- not a case where an OUT parameter is treated as IN OUT.
4287 -- * For a composite type with discriminants or that has
4288 -- implicit initial values for any subcomponents, the
4289 -- behavior is as for an in out parameter passed by copy.
4291 -- Hence for these cases we generate the read reference now
4292 -- (the write reference will be generated later by
4293 -- Note_Possible_Modification).
4295 elsif Is_By_Copy_Type (Etype (F))
4296 and then
4297 (Is_Access_Type (Etype (F))
4298 or else
4299 (Is_Scalar_Type (Etype (F))
4300 and then
4301 Present (Default_Aspect_Value (Etype (F))))
4302 or else
4303 (Is_Composite_Type (Etype (F))
4304 and then (Has_Discriminants (Etype (F))
4305 or else Is_Partially_Initialized_Type
4306 (Etype (F)))))
4307 then
4308 Generate_Reference (Orig_A, A);
4309 end if;
4310 end if;
4311 end if;
4312 end if;
4314 if Present (A)
4315 and then (Nkind (Parent (A)) /= N_Parameter_Association
4316 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4317 then
4318 -- If style checking mode on, check match of formal name
4320 if Style_Check then
4321 if Nkind (Parent (A)) = N_Parameter_Association then
4322 Check_Identifier (Selector_Name (Parent (A)), F);
4323 end if;
4324 end if;
4326 -- If the formal is Out or In_Out, do not resolve and expand the
4327 -- conversion, because it is subsequently expanded into explicit
4328 -- temporaries and assignments. However, the object of the
4329 -- conversion can be resolved. An exception is the case of tagged
4330 -- type conversion with a class-wide actual. In that case we want
4331 -- the tag check to occur and no temporary will be needed (no
4332 -- representation change can occur) and the parameter is passed by
4333 -- reference, so we go ahead and resolve the type conversion.
4334 -- Another exception is the case of reference to component or
4335 -- subcomponent of a bit-packed array, in which case we want to
4336 -- defer expansion to the point the in and out assignments are
4337 -- performed.
4339 if Ekind (F) /= E_In_Parameter
4340 and then Nkind (A) = N_Type_Conversion
4341 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4342 and then not Is_Interface (Etype (A))
4343 then
4344 declare
4345 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4347 begin
4348 -- Check RM 4.6 (24.2/2)
4350 if Is_Array_Type (Etype (F))
4351 and then Is_View_Conversion (A)
4352 then
4353 -- In a view conversion, the conversion must be legal in
4354 -- both directions, and thus both component types must be
4355 -- aliased, or neither (4.6 (8)).
4357 -- Check RM 4.6 (24.8/2)
4359 if Has_Aliased_Components (Expr_Typ) /=
4360 Has_Aliased_Components (Etype (F))
4361 then
4362 -- This normally illegal conversion is legal in an
4363 -- expanded instance body because of RM 12.3(11).
4364 -- At runtime, conversion must create a new object.
4366 if not In_Instance then
4367 Error_Msg_N
4368 ("both component types in a view conversion must"
4369 & " be aliased, or neither", A);
4370 end if;
4372 -- Check RM 4.6 (24/3)
4374 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4375 -- Check view conv between unrelated by ref array
4376 -- types.
4378 if Is_By_Reference_Type (Etype (F))
4379 or else Is_By_Reference_Type (Expr_Typ)
4380 then
4381 Error_Msg_N
4382 ("view conversion between unrelated by reference "
4383 & "array types not allowed ('A'I-00246)", A);
4385 -- In Ada 2005 mode, check view conversion component
4386 -- type cannot be private, tagged, or volatile. Note
4387 -- that we only apply this to source conversions. The
4388 -- generated code can contain conversions which are
4389 -- not subject to this test, and we cannot extract the
4390 -- component type in such cases since it is not
4391 -- present.
4393 elsif Comes_From_Source (A)
4394 and then Ada_Version >= Ada_2005
4395 then
4396 declare
4397 Comp_Type : constant Entity_Id :=
4398 Component_Type (Expr_Typ);
4399 begin
4400 if (Is_Private_Type (Comp_Type)
4401 and then not Is_Generic_Type (Comp_Type))
4402 or else Is_Tagged_Type (Comp_Type)
4403 or else Is_Volatile (Comp_Type)
4404 then
4405 Error_Msg_N
4406 ("component type of a view conversion " &
4407 "cannot be private, tagged, or volatile" &
4408 " (RM 4.6 (24))",
4409 Expression (A));
4410 end if;
4411 end;
4412 end if;
4413 end if;
4415 -- AI12-0074 & AI12-0377
4416 -- Check 6.4.1: If the mode is out, the actual parameter is
4417 -- a view conversion, and the type of the formal parameter
4418 -- is a scalar type, then either:
4419 -- - the target and operand type both do not have the
4420 -- Default_Value aspect specified; or
4421 -- - the target and operand type both have the
4422 -- Default_Value aspect specified, and there shall exist
4423 -- a type (other than a root numeric type) that is an
4424 -- ancestor of both the target type and the operand
4425 -- type.
4427 elsif Ekind (F) = E_Out_Parameter
4428 and then Is_Scalar_Type (Etype (F))
4429 then
4430 if Has_Default_Aspect (Etype (F)) /=
4431 Has_Default_Aspect (Expr_Typ)
4432 then
4433 Error_Msg_N
4434 ("view conversion requires Default_Value on both " &
4435 "types (RM 6.4.1)", A);
4436 elsif Has_Default_Aspect (Expr_Typ)
4437 and then not Same_Ancestor (Etype (F), Expr_Typ)
4438 then
4439 Error_Msg_N
4440 ("view conversion between unrelated types with "
4441 & "Default_Value not allowed (RM 6.4.1)", A);
4442 end if;
4443 end if;
4444 end;
4446 -- Resolve expression if conversion is all OK
4448 if (Conversion_OK (A)
4449 or else Valid_Conversion (A, Etype (A), Expression (A)))
4450 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4451 then
4452 Resolve (Expression (A));
4453 end if;
4455 -- In GNATprove mode, add a range check flag on scalar
4456 -- conversions for IN OUT parameters. The check may be
4457 -- needed on entry from the call.
4459 if GNATprove_Mode
4460 and then Ekind (F) = E_In_Out_Parameter
4461 and then Is_Scalar_Type (Etype (F))
4462 then
4463 Set_Do_Range_Check (Expression (A));
4464 end if;
4466 -- If the actual is a function call that returns a limited
4467 -- unconstrained object that needs finalization, create a
4468 -- transient scope for it, so that it can receive the proper
4469 -- finalization list.
4471 elsif Expander_Active
4472 and then Nkind (A) = N_Function_Call
4473 and then Is_Limited_Record (Etype (F))
4474 and then not Is_Constrained (Etype (F))
4475 and then (Needs_Finalization (Etype (F))
4476 or else Has_Task (Etype (F)))
4477 then
4478 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4479 Resolve (A, Etype (F));
4481 -- A small optimization: if one of the actuals is a concatenation
4482 -- create a block around a procedure call to recover stack space.
4483 -- This alleviates stack usage when several procedure calls in
4484 -- the same statement list use concatenation. We do not perform
4485 -- this wrapping for code statements, where the argument is a
4486 -- static string, and we want to preserve warnings involving
4487 -- sequences of such statements.
4489 elsif Expander_Active
4490 and then Nkind (A) = N_Op_Concat
4491 and then Nkind (N) = N_Procedure_Call_Statement
4492 and then not (Is_Intrinsic_Subprogram (Nam)
4493 and then Chars (Nam) = Name_Asm)
4494 and then not Static_Concatenation (A)
4495 then
4496 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4497 Resolve (A, Etype (F));
4499 else
4500 if Nkind (A) = N_Type_Conversion
4501 and then Is_Array_Type (Etype (F))
4502 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4503 and then
4504 (Is_Limited_Type (Etype (F))
4505 or else Is_Limited_Type (Etype (Expression (A))))
4506 then
4507 Error_Msg_N
4508 ("conversion between unrelated limited array types not "
4509 & "allowed ('A'I-00246)", A);
4511 if Is_Limited_Type (Etype (F)) then
4512 Explain_Limited_Type (Etype (F), A);
4513 end if;
4515 if Is_Limited_Type (Etype (Expression (A))) then
4516 Explain_Limited_Type (Etype (Expression (A)), A);
4517 end if;
4518 end if;
4520 -- (Ada 2005: AI-251): If the actual is an allocator whose
4521 -- directly designated type is a class-wide interface, we build
4522 -- an anonymous access type to use it as the type of the
4523 -- allocator. Later, when the subprogram call is expanded, if
4524 -- the interface has a secondary dispatch table the expander
4525 -- will add a type conversion to force the correct displacement
4526 -- of the pointer.
4528 if Nkind (A) = N_Allocator then
4529 declare
4530 DDT : constant Entity_Id :=
4531 Directly_Designated_Type (Base_Type (Etype (F)));
4533 begin
4534 -- Displace the pointer to the object to reference its
4535 -- secondary dispatch table.
4537 if Is_Class_Wide_Type (DDT)
4538 and then Is_Interface (DDT)
4539 then
4540 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4541 Analyze_And_Resolve (A, Etype (F),
4542 Suppress => Access_Check);
4543 end if;
4545 -- Ada 2005, AI-162:If the actual is an allocator, the
4546 -- innermost enclosing statement is the master of the
4547 -- created object. This needs to be done with expansion
4548 -- enabled only, otherwise the transient scope will not
4549 -- be removed in the expansion of the wrapped construct.
4551 if Expander_Active
4552 and then (Needs_Finalization (DDT)
4553 or else Has_Task (DDT))
4554 then
4555 Establish_Transient_Scope
4556 (A, Manage_Sec_Stack => False);
4557 end if;
4558 end;
4560 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4561 Check_Restriction (No_Access_Parameter_Allocators, A);
4562 end if;
4563 end if;
4565 -- (Ada 2005): The call may be to a primitive operation of a
4566 -- tagged synchronized type, declared outside of the type. In
4567 -- this case the controlling actual must be converted to its
4568 -- corresponding record type, which is the formal type. The
4569 -- actual may be a subtype, either because of a constraint or
4570 -- because it is a generic actual, so use base type to locate
4571 -- concurrent type.
4573 F_Typ := Base_Type (Etype (F));
4575 if Is_Tagged_Type (F_Typ)
4576 and then (Is_Concurrent_Type (F_Typ)
4577 or else Is_Concurrent_Record_Type (F_Typ))
4578 then
4579 -- If the actual is overloaded, look for an interpretation
4580 -- that has a synchronized type.
4582 if not Is_Overloaded (A) then
4583 A_Typ := Base_Type (Etype (A));
4585 else
4586 declare
4587 Index : Interp_Index;
4588 It : Interp;
4590 begin
4591 Get_First_Interp (A, Index, It);
4592 while Present (It.Typ) loop
4593 if Is_Concurrent_Type (It.Typ)
4594 or else Is_Concurrent_Record_Type (It.Typ)
4595 then
4596 A_Typ := Base_Type (It.Typ);
4597 exit;
4598 end if;
4600 Get_Next_Interp (Index, It);
4601 end loop;
4602 end;
4603 end if;
4605 declare
4606 Full_A_Typ : Entity_Id;
4608 begin
4609 if Present (Full_View (A_Typ)) then
4610 Full_A_Typ := Base_Type (Full_View (A_Typ));
4611 else
4612 Full_A_Typ := A_Typ;
4613 end if;
4615 -- Tagged synchronized type (case 1): the actual is a
4616 -- concurrent type.
4618 if Is_Concurrent_Type (A_Typ)
4619 and then Corresponding_Record_Type (A_Typ) = F_Typ
4620 then
4621 Rewrite (A,
4622 Unchecked_Convert_To
4623 (Corresponding_Record_Type (A_Typ), A));
4624 Resolve (A, Etype (F));
4626 -- Tagged synchronized type (case 2): the formal is a
4627 -- concurrent type.
4629 elsif Ekind (Full_A_Typ) = E_Record_Type
4630 and then Present
4631 (Corresponding_Concurrent_Type (Full_A_Typ))
4632 and then Is_Concurrent_Type (F_Typ)
4633 and then Present (Corresponding_Record_Type (F_Typ))
4634 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4635 then
4636 Resolve (A, Corresponding_Record_Type (F_Typ));
4638 -- Common case
4640 else
4641 Resolve (A, Etype (F));
4642 end if;
4643 end;
4645 -- Not a synchronized operation
4647 else
4648 Resolve (A, Etype (F));
4649 end if;
4650 end if;
4652 A_Typ := Etype (A);
4653 F_Typ := Etype (F);
4655 -- If A_Typ is complete and F_Typ is not, then adjust F_Typ
4657 if Ekind (F_Typ) = E_Incomplete_Type
4658 and then Present (Full_View (F_Typ))
4659 and then not Is_Incomplete_Type (A_Typ)
4660 then
4661 F_Typ := Full_View (F_Typ);
4662 end if;
4664 -- An actual cannot be of an untagged incomplete view; the result
4665 -- object of a function call cannot be of an incomplete view
4666 -- (RM 3.10.1(10)).
4668 if Ekind (A_Typ) = E_Incomplete_Type
4669 and then (Nkind (A) = N_Function_Call
4670 or else not Is_Tagged_Type (A_Typ))
4671 then
4672 -- No error if the call is placed in the initializing
4673 -- expression of a component of the full-view of the
4674 -- incomplete type. For example:
4676 -- type T;
4677 -- function F (Obj : T) return Integer;
4679 -- type T is record
4680 -- Data : Integer := F (T);
4681 -- end record;
4683 if Present (Full_View (A_Typ))
4684 and then Full_View (A_Typ) = Current_Scope
4685 and then In_Spec_Expression
4686 and then In_Default_Expr
4687 then
4688 null;
4690 -- No error if the call is performed in pre/postconditions, and
4691 -- it is an incomplete type of a limited-with clause.
4693 elsif From_Limited_With (A_Typ)
4694 and then Present (Non_Limited_View (A_Typ))
4695 and then Is_Subprogram (Current_Scope)
4696 and then
4697 (In_Spec_Expression
4698 or else
4699 Present (Class_Preconditions_Subprogram (Current_Scope)))
4700 then
4701 null;
4703 elsif Is_Generic_Type (A_Typ) then
4704 if Is_Tagged_Type (A_Typ) then
4705 Error_Msg_N
4706 ("invalid use of tagged formal incomplete type", A);
4707 else
4708 Error_Msg_N
4709 ("invalid use of untagged formal incomplete type", A);
4710 end if;
4711 else
4712 Check_Fully_Declared (A_Typ, A);
4713 end if;
4714 end if;
4716 -- For mode IN, if actual is an entity, and the type of the formal
4717 -- has warnings suppressed, then we reset Never_Set_In_Source for
4718 -- the calling entity. The reason for this is to catch cases like
4719 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4720 -- uses trickery to modify an IN parameter.
4722 if Ekind (F) = E_In_Parameter
4723 and then Is_Entity_Name (A)
4724 and then Present (Entity (A))
4725 and then Ekind (Entity (A)) = E_Variable
4726 and then Has_Warnings_Off (F_Typ)
4727 then
4728 Set_Never_Set_In_Source (Entity (A), False);
4729 end if;
4731 -- Perform error checks for IN and IN OUT parameters
4733 if Ekind (F) /= E_Out_Parameter then
4735 -- Check unset reference. For scalar parameters, it is clearly
4736 -- wrong to pass an uninitialized value as either an IN or
4737 -- IN-OUT parameter. For composites, it is also clearly an
4738 -- error to pass a completely uninitialized value as an IN
4739 -- parameter, but the case of IN OUT is trickier. We prefer
4740 -- not to give a warning here. For example, suppose there is
4741 -- a routine that sets some component of a record to False.
4742 -- It is perfectly reasonable to make this IN-OUT and allow
4743 -- either initialized or uninitialized records to be passed
4744 -- in this case.
4746 -- For partially initialized composite values, we also avoid
4747 -- warnings, since it is quite likely that we are passing a
4748 -- partially initialized value and only the initialized fields
4749 -- will in fact be read in the subprogram.
4751 if Is_Scalar_Type (A_Typ)
4752 or else (Ekind (F) = E_In_Parameter
4753 and then not Is_Partially_Initialized_Type (A_Typ))
4754 then
4755 Check_Unset_Reference (A);
4756 end if;
4758 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4759 -- actual to a nested call, since this constitutes a reading of
4760 -- the parameter, which is not allowed.
4762 if Ada_Version = Ada_83
4763 and then Is_Entity_Name (A)
4764 and then Ekind (Entity (A)) = E_Out_Parameter
4765 then
4766 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4767 end if;
4768 end if;
4770 -- In -gnatd.q mode, forget that a given array is constant when
4771 -- it is passed as an IN parameter to a foreign-convention
4772 -- subprogram. This is in case the subprogram evilly modifies the
4773 -- object. Of course, correct code would use IN OUT.
4775 if Debug_Flag_Dot_Q
4776 and then Ekind (F) = E_In_Parameter
4777 and then Has_Foreign_Convention (Nam)
4778 and then Is_Array_Type (F_Typ)
4779 and then Nkind (A) in N_Has_Entity
4780 and then Present (Entity (A))
4781 then
4782 Set_Is_True_Constant (Entity (A), False);
4783 end if;
4785 -- Case of OUT or IN OUT parameter
4787 if Ekind (F) /= E_In_Parameter then
4789 -- For an Out parameter, check for useless assignment. Note
4790 -- that we can't set Last_Assignment this early, because we may
4791 -- kill current values in Resolve_Call, and that call would
4792 -- clobber the Last_Assignment field.
4794 -- Note: call Warn_On_Useless_Assignment before doing the check
4795 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4796 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4797 -- reflects the last assignment, not this one.
4799 if Ekind (F) = E_Out_Parameter then
4800 if Warn_On_Modified_As_Out_Parameter (F)
4801 and then Is_Entity_Name (A)
4802 and then Present (Entity (A))
4803 and then Comes_From_Source (N)
4804 then
4805 Warn_On_Useless_Assignment (Entity (A), A);
4806 end if;
4807 end if;
4809 -- Validate the form of the actual. Note that the call to
4810 -- Is_OK_Variable_For_Out_Formal generates the required
4811 -- reference in this case.
4813 -- A call to an initialization procedure for an aggregate
4814 -- component may initialize a nested component of a constant
4815 -- designated object. In this context the object is variable.
4817 if not Is_OK_Variable_For_Out_Formal (A)
4818 and then not Is_Init_Proc (Nam)
4819 then
4820 Error_Msg_NE ("actual for& must be a variable", A, F);
4822 if Is_Subprogram (Current_Scope) then
4823 if Is_Invariant_Procedure (Current_Scope)
4824 or else Is_Partial_Invariant_Procedure (Current_Scope)
4825 then
4826 Error_Msg_N
4827 ("function used in invariant cannot modify its "
4828 & "argument", F);
4830 elsif Is_Predicate_Function (Current_Scope) then
4831 Error_Msg_N
4832 ("function used in predicate cannot modify its "
4833 & "argument", F);
4834 end if;
4835 end if;
4836 end if;
4838 -- What's the following about???
4840 if Is_Entity_Name (A) then
4841 Kill_Checks (Entity (A));
4842 else
4843 Kill_All_Checks;
4844 end if;
4845 end if;
4847 if A_Typ = Any_Type then
4848 Set_Etype (N, Any_Type);
4849 return;
4850 end if;
4852 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4854 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4856 -- Apply predicate tests except in certain special cases. Note
4857 -- that it might be more consistent to apply these only when
4858 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4859 -- for the outbound predicate tests ??? In any case indicate
4860 -- the function being called, for better warnings if the call
4861 -- leads to an infinite recursion.
4863 if Predicate_Tests_On_Arguments (Nam) then
4864 Apply_Predicate_Check (A, F_Typ, Fun => Nam);
4865 end if;
4867 -- Apply required constraint checks
4869 if Is_Scalar_Type (A_Typ) then
4870 Apply_Scalar_Range_Check (A, F_Typ);
4872 elsif Is_Array_Type (A_Typ) then
4873 Apply_Length_Check (A, F_Typ);
4875 elsif Is_Record_Type (F_Typ)
4876 and then Has_Discriminants (F_Typ)
4877 and then Is_Constrained (F_Typ)
4878 and then (not Is_Derived_Type (F_Typ)
4879 or else Comes_From_Source (Nam))
4880 then
4881 Apply_Discriminant_Check (A, F_Typ);
4883 -- For view conversions of a discriminated object, apply
4884 -- check to object itself, the conversion alreay has the
4885 -- proper type.
4887 if Nkind (A) = N_Type_Conversion
4888 and then Is_Constrained (Etype (Expression (A)))
4889 then
4890 Apply_Discriminant_Check (Expression (A), F_Typ);
4891 end if;
4893 elsif Is_Access_Type (F_Typ)
4894 and then Is_Array_Type (Designated_Type (F_Typ))
4895 and then Is_Constrained (Designated_Type (F_Typ))
4896 then
4897 Apply_Length_Check (A, F_Typ);
4899 elsif Is_Access_Type (F_Typ)
4900 and then Has_Discriminants (Designated_Type (F_Typ))
4901 and then Is_Constrained (Designated_Type (F_Typ))
4902 then
4903 Apply_Discriminant_Check (A, F_Typ);
4905 else
4906 Apply_Range_Check (A, F_Typ);
4907 end if;
4909 -- Ada 2005 (AI-231): Note that the controlling parameter case
4910 -- already existed in Ada 95, which is partially checked
4911 -- elsewhere (see Checks), and we don't want the warning
4912 -- message to differ.
4914 if Is_Access_Type (F_Typ)
4915 and then Can_Never_Be_Null (F_Typ)
4916 and then Known_Null (A)
4917 then
4918 if Is_Controlling_Formal (F) then
4919 Apply_Compile_Time_Constraint_Error
4920 (N => A,
4921 Msg => "null value not allowed here??",
4922 Reason => CE_Access_Check_Failed);
4924 elsif Ada_Version >= Ada_2005 then
4925 Apply_Compile_Time_Constraint_Error
4926 (N => A,
4927 Msg => "(Ada 2005) NULL not allowed in "
4928 & "null-excluding formal??",
4929 Reason => CE_Null_Not_Allowed);
4930 end if;
4931 end if;
4933 -- In a prefixed call, if the prefix is an access type
4934 -- it cannot be null.
4936 if Is_Access_Type (F_Typ)
4937 and then A = First_Actual (N)
4938 and then Is_Expanded_Prefixed_Call (N)
4939 then
4940 if not Is_Access_Type (A_Typ)
4941 and then not Is_Aliased_View (A)
4942 then
4943 Error_Msg_NE
4944 ("object in prefixed call to& must be aliased "
4945 & "(RM 4.1.3 (13 1/2))",
4946 A, Nam);
4947 end if;
4949 if Debug_Flag_Underscore_PP
4950 and then
4951 (Is_Controlling_Formal (F)
4952 or else Is_Class_Wide_Type (Designated_Type (F_Typ)))
4953 then
4954 Install_Null_Excluding_Check (A);
4955 end if;
4956 end if;
4957 end if;
4959 -- Checks for OUT parameters and IN OUT parameters
4961 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4963 -- If there is a type conversion, make sure the return value
4964 -- meets the constraints of the variable before the conversion.
4966 if Nkind (A) = N_Type_Conversion then
4967 if Is_Scalar_Type (A_Typ) then
4969 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4970 -- which would prevent the check from being generated.
4971 -- This is for Starlet only though, so long obsolete.
4973 if Mechanism (F) = By_Reference
4974 and then Ekind (Nam) = E_Procedure
4975 and then Is_Valued_Procedure (Nam)
4976 then
4977 null;
4978 else
4979 Apply_Scalar_Range_Check
4980 (Expression (A), Etype (Expression (A)), A_Typ);
4981 end if;
4983 -- In addition the return value must meet the constraints
4984 -- of the object type (see the comment below).
4986 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4988 else
4989 Apply_Range_Check
4990 (Expression (A), Etype (Expression (A)), A_Typ);
4991 end if;
4993 -- If no conversion, apply scalar range checks and length check
4994 -- based on the subtype of the actual (NOT that of the formal).
4995 -- This indicates that the check takes place on return from the
4996 -- call. During expansion the required constraint checks are
4997 -- inserted. In GNATprove mode, in the absence of expansion,
4998 -- the flag indicates that the returned value is valid.
5000 else
5001 if Is_Scalar_Type (F_Typ) then
5002 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
5004 elsif Is_Array_Type (F_Typ)
5005 and then Ekind (F) = E_Out_Parameter
5006 then
5007 Apply_Length_Check (A, F_Typ);
5009 else
5010 Apply_Range_Check (A, A_Typ, F_Typ);
5011 end if;
5012 end if;
5014 -- Note: we do not apply the predicate checks for the case of
5015 -- OUT and IN OUT parameters. They are instead applied in the
5016 -- Expand_Actuals routine in Exp_Ch6.
5017 end if;
5019 -- If the formal is of an unconstrained array subtype with fixed
5020 -- lower bound, then sliding to that bound may be needed.
5022 if Is_Fixed_Lower_Bound_Array_Subtype (F_Typ) then
5023 Expand_Sliding_Conversion (A, F_Typ);
5024 end if;
5026 -- An actual associated with an access parameter is implicitly
5027 -- converted to the anonymous access type of the formal and must
5028 -- satisfy the legality checks for access conversions.
5030 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5031 if not Valid_Conversion (A, F_Typ, A) then
5032 Error_Msg_N
5033 ("invalid implicit conversion for access parameter", A);
5034 end if;
5036 -- If the actual is an access selected component of a variable,
5037 -- the call may modify its designated object. It is reasonable
5038 -- to treat this as a potential modification of the enclosing
5039 -- record, to prevent spurious warnings that it should be
5040 -- declared as a constant, because intuitively programmers
5041 -- regard the designated subcomponent as part of the record.
5043 if Nkind (A) = N_Selected_Component
5044 and then Is_Entity_Name (Prefix (A))
5045 and then not Is_Constant_Object (Entity (Prefix (A)))
5046 then
5047 Note_Possible_Modification (A, Sure => False);
5048 end if;
5049 end if;
5051 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
5053 if (Is_By_Reference_Type (F_Typ) or else Is_Aliased (F))
5054 and then Comes_From_Source (N)
5055 then
5056 if Is_Atomic_Object (A)
5057 and then not Is_Atomic (F_Typ)
5058 then
5059 Error_Msg_NE
5060 ("cannot pass atomic object to nonatomic formal&",
5061 A, F);
5062 Error_Msg_N
5063 ("\which is passed by reference (RM C.6(12))", A);
5065 elsif Is_Volatile_Object_Ref (A)
5066 and then not Is_Volatile (F_Typ)
5067 then
5068 Error_Msg_NE
5069 ("cannot pass volatile object to nonvolatile formal&",
5070 A, F);
5071 Error_Msg_N
5072 ("\which is passed by reference (RM C.6(12))", A);
5074 elsif Is_Volatile_Full_Access_Object_Ref (A)
5075 and then not Is_Volatile_Full_Access (F_Typ)
5076 then
5077 Error_Msg_NE
5078 ("cannot pass full access object to nonfull access "
5079 & "formal&", A, F);
5080 Error_Msg_N
5081 ("\which is passed by reference (RM C.6(12))", A);
5082 end if;
5084 -- Check for nonatomic subcomponent of a full access object
5085 -- in Ada 2022 (RM C.6 (12)).
5087 if Ada_Version >= Ada_2022
5088 and then Is_Subcomponent_Of_Full_Access_Object (A)
5089 and then not Is_Atomic_Object (A)
5090 then
5091 Error_Msg_N
5092 ("cannot pass nonatomic subcomponent of full access "
5093 & "object", A);
5094 Error_Msg_NE
5095 ("\to formal & which is passed by reference (RM C.6(12))",
5096 A, F);
5097 end if;
5098 end if;
5100 -- Check that subprograms don't have improper controlling
5101 -- arguments (RM 3.9.2 (9)).
5103 -- A primitive operation may have an access parameter of an
5104 -- incomplete tagged type, but a dispatching call is illegal
5105 -- if the type is still incomplete.
5107 if Is_Controlling_Formal (F) then
5108 Set_Is_Controlling_Actual (A);
5110 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5111 declare
5112 Desig : constant Entity_Id := Designated_Type (F_Typ);
5113 begin
5114 if Ekind (Desig) = E_Incomplete_Type
5115 and then No (Full_View (Desig))
5116 and then No (Non_Limited_View (Desig))
5117 then
5118 Error_Msg_NE
5119 ("premature use of incomplete type& "
5120 & "in dispatching call", A, Desig);
5121 end if;
5122 end;
5123 end if;
5125 elsif Nkind (A) = N_Explicit_Dereference then
5126 Validate_Remote_Access_To_Class_Wide_Type (A);
5127 end if;
5129 -- Apply legality rule 3.9.2 (9/1)
5131 -- Skip this check on helpers and indirect-call wrappers built to
5132 -- support class-wide preconditions.
5134 -- We make special exception here for mutably tagged types and
5135 -- related calls to their initialization procedures.
5137 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
5138 and then not Is_Class_Wide_Type (F_Typ)
5139 and then not Is_Controlling_Formal (F)
5140 and then not In_Instance
5141 and then (not Is_Subprogram (Nam)
5142 or else No (Class_Preconditions_Subprogram (Nam)))
5144 -- Ignore mutably tagged types and their use in calls to init
5145 -- procs.
5147 and then not Is_Mutably_Tagged_CW_Equivalent_Type (A_Typ)
5148 and then not Is_Init_Proc (Nam)
5149 then
5150 Error_Msg_N ("class-wide argument not allowed here!", A);
5152 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5153 Error_Msg_Node_2 := F_Typ;
5154 Error_Msg_NE
5155 ("& is not a dispatching operation of &!", A, Nam);
5156 end if;
5158 -- Apply the checks described in 3.10.2(27): if the context is a
5159 -- specific access-to-object, the actual cannot be class-wide.
5160 -- Use base type to exclude access_to_subprogram cases.
5162 elsif Is_Access_Type (A_Typ)
5163 and then Is_Access_Type (F_Typ)
5164 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
5165 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
5166 or else (Nkind (A) = N_Attribute_Reference
5167 and then
5168 Is_Class_Wide_Type (Etype (Prefix (A)))))
5169 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
5170 and then not Is_Controlling_Formal (F)
5172 -- Disable these checks for call to imported C++ subprograms
5174 and then not
5175 (Is_Entity_Name (Name (N))
5176 and then Is_Imported (Entity (Name (N)))
5177 and then Convention (Entity (Name (N))) = Convention_CPP)
5178 then
5179 Error_Msg_N
5180 ("access to class-wide argument not allowed here!", A);
5182 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5183 Error_Msg_Node_2 := Designated_Type (F_Typ);
5184 Error_Msg_NE
5185 ("& is not a dispatching operation of &!", A, Nam);
5186 end if;
5187 end if;
5189 Check_Aliased_Parameter;
5191 Eval_Actual (A);
5193 -- If it is a named association, treat the selector_name as a
5194 -- proper identifier, and mark the corresponding entity.
5196 if Nkind (Parent (A)) = N_Parameter_Association
5198 -- Ignore reference in SPARK mode, as it refers to an entity not
5199 -- in scope at the point of reference, so the reference should
5200 -- be ignored for computing effects of subprograms.
5202 and then not GNATprove_Mode
5203 then
5204 -- If subprogram is overridden, use name of formal that
5205 -- is being called.
5207 if Present (Real_Subp) then
5208 Set_Entity (Selector_Name (Parent (A)), Real_F);
5209 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
5211 else
5212 Set_Entity (Selector_Name (Parent (A)), F);
5213 Generate_Reference (F, Selector_Name (Parent (A)));
5214 Set_Etype (Selector_Name (Parent (A)), F_Typ);
5215 Generate_Reference (F_Typ, N, ' ');
5216 end if;
5217 end if;
5219 Prev := A;
5221 if Ekind (F) /= E_Out_Parameter then
5222 Check_Unset_Reference (A);
5223 end if;
5225 -- A formal parameter of a specific tagged type whose related
5226 -- subprogram is subject to pragma Extensions_Visible with value
5227 -- "False" cannot act as an actual in a subprogram with value
5228 -- "True" (SPARK RM 6.1.7(3)).
5230 -- No check needed for helpers and indirect-call wrappers built to
5231 -- support class-wide preconditions.
5233 if Is_EVF_Expression (A)
5234 and then Extensions_Visible_Status (Nam) =
5235 Extensions_Visible_True
5236 and then not
5237 (Is_Subprogram (Current_Scope)
5238 and then
5239 Present (Class_Preconditions_Subprogram (Current_Scope)))
5240 then
5241 Error_Msg_N
5242 ("formal parameter cannot act as actual parameter when "
5243 & "Extensions_Visible is False", A);
5244 Error_Msg_NE
5245 ("\subprogram & has Extensions_Visible True", A, Nam);
5246 end if;
5248 -- The actual parameter of a Ghost subprogram whose formal is of
5249 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5251 if Comes_From_Source (Nam)
5252 and then Is_Ghost_Entity (Nam)
5253 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5254 and then Is_Entity_Name (A)
5255 and then Present (Entity (A))
5256 and then not Is_Ghost_Entity (Entity (A))
5257 then
5258 Error_Msg_NE
5259 ("non-ghost variable & cannot appear as actual in call to "
5260 & "ghost procedure", A, Entity (A));
5262 if Ekind (F) = E_In_Out_Parameter then
5263 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5264 else
5265 Error_Msg_N ("\corresponding formal has mode OUT", A);
5266 end if;
5267 end if;
5269 -- (AI12-0397): The target of a subprogram call that occurs within
5270 -- the expression of an Default_Initial_Condition aspect and has
5271 -- an actual that is the current instance of the type must be
5272 -- either a primitive of the type or a class-wide subprogram,
5273 -- because the type of the current instance in such an aspect is
5274 -- considered to be a notional formal derived type whose only
5275 -- operations correspond to the primitives of the enclosing type.
5276 -- Nonprimitives can be called, but the current instance must be
5277 -- converted rather than passed directly. Note that a current
5278 -- instance of a type with DIC will occur as a reference to an
5279 -- in-mode formal of an enclosing DIC procedure or partial DIC
5280 -- procedure. (It seems that this check should perhaps also apply
5281 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5282 -- aspects???)
5284 if Nkind (A) = N_Identifier
5285 and then Ekind (Entity (A)) = E_In_Parameter
5287 and then Is_Subprogram (Scope (Entity (A)))
5288 and then Is_DIC_Procedure (Scope (Entity (A)))
5290 -- We check Comes_From_Source to exclude inherited primitives
5291 -- from being flagged, because such subprograms turn out to not
5292 -- always have the Is_Primitive flag set. ???
5294 and then Comes_From_Source (Nam)
5296 and then not Is_Primitive (Nam)
5297 and then not Is_Class_Wide_Type (F_Typ)
5298 then
5299 Error_Msg_NE
5300 ("call to nonprimitive & with current instance not allowed " &
5301 "for aspect", A, Nam);
5302 end if;
5304 Next_Actual (A);
5306 -- Case where actual is not present
5308 else
5309 Insert_Default;
5310 end if;
5312 Next_Formal (F);
5314 if Present (Real_Subp) then
5315 Next_Formal (Real_F);
5316 end if;
5317 end loop;
5318 end Resolve_Actuals;
5320 -----------------------
5321 -- Resolve_Allocator --
5322 -----------------------
5324 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5325 Desig_T : constant Entity_Id := Designated_Type (Typ);
5326 E : constant Node_Id := Expression (N);
5327 Subtyp : Entity_Id;
5328 Discrim : Entity_Id;
5329 Constr : Node_Id;
5330 Aggr : Node_Id;
5331 Assoc : Node_Id := Empty;
5332 Disc_Exp : Node_Id;
5334 procedure Check_Allocator_Discrim_Accessibility
5335 (Disc_Exp : Node_Id;
5336 Alloc_Typ : Entity_Id);
5337 -- Check that accessibility level associated with an access discriminant
5338 -- initialized in an allocator by the expression Disc_Exp is not deeper
5339 -- than the level of the allocator type Alloc_Typ. An error message is
5340 -- issued if this condition is violated. Specialized checks are done for
5341 -- the cases of a constraint expression which is an access attribute or
5342 -- an access discriminant.
5344 procedure Check_Allocator_Discrim_Accessibility_Exprs
5345 (Curr_Exp : Node_Id;
5346 Alloc_Typ : Entity_Id);
5347 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5348 -- across all expressions within a given conditional expression.
5350 function In_Dispatching_Context return Boolean;
5351 -- If the allocator is an actual in a call, it is allowed to be class-
5352 -- wide when the context is not because it is a controlling actual.
5354 -------------------------------------------
5355 -- Check_Allocator_Discrim_Accessibility --
5356 -------------------------------------------
5358 procedure Check_Allocator_Discrim_Accessibility
5359 (Disc_Exp : Node_Id;
5360 Alloc_Typ : Entity_Id)
5362 begin
5363 if Type_Access_Level (Etype (Disc_Exp)) >
5364 Deepest_Type_Access_Level (Alloc_Typ)
5365 then
5366 Error_Msg_N
5367 ("operand type has deeper level than allocator type", Disc_Exp);
5369 -- When the expression is an Access attribute the level of the prefix
5370 -- object must not be deeper than that of the allocator's type.
5372 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5373 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5374 Attribute_Access
5375 and then Static_Accessibility_Level
5376 (Disc_Exp, Zero_On_Dynamic_Level)
5377 > Deepest_Type_Access_Level (Alloc_Typ)
5378 then
5379 Error_Msg_N
5380 ("prefix of attribute has deeper level than allocator type",
5381 Disc_Exp);
5383 -- When the expression is an access discriminant the check is against
5384 -- the level of the prefix object.
5386 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5387 and then Nkind (Disc_Exp) = N_Selected_Component
5388 and then Static_Accessibility_Level
5389 (Disc_Exp, Zero_On_Dynamic_Level)
5390 > Deepest_Type_Access_Level (Alloc_Typ)
5391 then
5392 Error_Msg_N
5393 ("access discriminant has deeper level than allocator type",
5394 Disc_Exp);
5396 -- All other cases are legal
5398 else
5399 null;
5400 end if;
5401 end Check_Allocator_Discrim_Accessibility;
5403 -------------------------------------------------
5404 -- Check_Allocator_Discrim_Accessibility_Exprs --
5405 -------------------------------------------------
5407 procedure Check_Allocator_Discrim_Accessibility_Exprs
5408 (Curr_Exp : Node_Id;
5409 Alloc_Typ : Entity_Id)
5411 Alt : Node_Id;
5412 Expr : Node_Id;
5413 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5414 begin
5415 -- When conditional expressions are constant folded we know at
5416 -- compile time which expression to check - so don't bother with
5417 -- the rest of the cases.
5419 if Nkind (Curr_Exp) = N_Attribute_Reference then
5420 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5422 -- Non-constant-folded if expressions
5424 elsif Nkind (Disc_Exp) = N_If_Expression then
5425 -- Check both expressions if they are still present in the face
5426 -- of expansion.
5428 Expr := Next (First (Expressions (Disc_Exp)));
5429 if Present (Expr) then
5430 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5431 Next (Expr);
5432 if Present (Expr) then
5433 Check_Allocator_Discrim_Accessibility_Exprs
5434 (Expr, Alloc_Typ);
5435 end if;
5436 end if;
5438 -- Non-constant-folded case expressions
5440 elsif Nkind (Disc_Exp) = N_Case_Expression then
5441 -- Check all alternatives
5443 Alt := First (Alternatives (Disc_Exp));
5444 while Present (Alt) loop
5445 Check_Allocator_Discrim_Accessibility_Exprs
5446 (Expression (Alt), Alloc_Typ);
5448 Next (Alt);
5449 end loop;
5451 -- Base case, check the accessibility of the original node of the
5452 -- expression.
5454 else
5455 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5456 end if;
5457 end Check_Allocator_Discrim_Accessibility_Exprs;
5459 ----------------------------
5460 -- In_Dispatching_Context --
5461 ----------------------------
5463 function In_Dispatching_Context return Boolean is
5464 Par : constant Node_Id := Parent (N);
5466 begin
5467 return Nkind (Par) in N_Subprogram_Call
5468 and then Is_Entity_Name (Name (Par))
5469 and then Is_Dispatching_Operation (Entity (Name (Par)));
5470 end In_Dispatching_Context;
5472 -- Start of processing for Resolve_Allocator
5474 begin
5475 -- Replace general access with specific type
5477 if Ekind (Etype (N)) = E_Allocator_Type then
5478 Set_Etype (N, Base_Type (Typ));
5479 end if;
5481 if Is_Abstract_Type (Typ) then
5482 Error_Msg_N ("type of allocator cannot be abstract", N);
5483 end if;
5485 -- For qualified expression, resolve the expression using the given
5486 -- subtype (nothing to do for type mark, subtype indication)
5488 if Nkind (E) = N_Qualified_Expression then
5489 if Is_Class_Wide_Type (Etype (E))
5490 and then not Is_Class_Wide_Type (Desig_T)
5491 and then not In_Dispatching_Context
5492 then
5493 Error_Msg_N
5494 ("class-wide allocator not allowed for this access type", N);
5495 end if;
5497 -- Do a full resolution to apply constraint and predicate checks
5499 Resolve_Qualified_Expression (E, Etype (E));
5500 Check_Unset_Reference (Expression (E));
5502 -- Allocators generated by the build-in-place expansion mechanism
5503 -- are explicitly marked as coming from source but do not need to be
5504 -- checked for limited initialization. To exclude this case, ensure
5505 -- that the parent of the allocator is a source node.
5506 -- The return statement constructed for an Expression_Function does
5507 -- not come from source but requires a limited check.
5509 if Is_Limited_Type (Etype (E))
5510 and then Comes_From_Source (N)
5511 and then
5512 (Comes_From_Source (Parent (N))
5513 or else
5514 (Ekind (Current_Scope) = E_Function
5515 and then Nkind (Original_Node (Unit_Declaration_Node
5516 (Current_Scope))) = N_Expression_Function))
5517 and then not In_Instance_Body
5518 then
5519 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5520 if Nkind (Parent (N)) = N_Assignment_Statement then
5521 Error_Msg_N
5522 ("illegal expression for initialized allocator of a "
5523 & "limited type (RM 7.5 (2.7/2))", N);
5524 else
5525 Error_Msg_N
5526 ("initialization not allowed for limited types", N);
5527 end if;
5529 Explain_Limited_Type (Etype (E), N);
5530 end if;
5531 end if;
5533 -- Calls to build-in-place functions are not currently supported in
5534 -- allocators for access types associated with a simple storage pool.
5535 -- Supporting such allocators may require passing additional implicit
5536 -- parameters to build-in-place functions (or a significant revision
5537 -- of the current b-i-p implementation to unify the handling for
5538 -- multiple kinds of storage pools). ???
5540 if Is_Inherently_Limited_Type (Desig_T)
5541 and then Nkind (Expression (E)) = N_Function_Call
5542 then
5543 declare
5544 Pool : constant Entity_Id :=
5545 Associated_Storage_Pool (Root_Type (Typ));
5546 begin
5547 if Present (Pool)
5548 and then
5549 Present (Get_Rep_Pragma
5550 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5551 then
5552 Error_Msg_N
5553 ("limited function calls not yet supported in simple "
5554 & "storage pool allocators", Expression (E));
5555 end if;
5556 end;
5557 end if;
5559 -- A special accessibility check is needed for allocators that
5560 -- constrain access discriminants. The level of the type of the
5561 -- expression used to constrain an access discriminant cannot be
5562 -- deeper than the type of the allocator (in contrast to access
5563 -- parameters, where the level of the actual can be arbitrary).
5565 -- We can't use Valid_Conversion to perform this check because in
5566 -- general the type of the allocator is unrelated to the type of
5567 -- the access discriminant.
5569 if Ekind (Typ) /= E_Anonymous_Access_Type
5570 or else Is_Local_Anonymous_Access (Typ)
5571 then
5572 Subtyp := Entity (Subtype_Mark (E));
5574 Aggr := Original_Node (Expression (E));
5576 if Has_Discriminants (Subtyp)
5577 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5578 then
5579 Discrim := First_Discriminant (Base_Type (Subtyp));
5581 -- Get the first component expression of the aggregate
5583 if Present (Expressions (Aggr)) then
5584 Disc_Exp := First (Expressions (Aggr));
5586 elsif Present (Component_Associations (Aggr)) then
5587 Assoc := First (Component_Associations (Aggr));
5589 if Present (Assoc) then
5590 Disc_Exp := Expression (Assoc);
5591 else
5592 Disc_Exp := Empty;
5593 end if;
5595 else
5596 Disc_Exp := Empty;
5597 end if;
5599 while Present (Discrim) and then Present (Disc_Exp) loop
5600 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5601 Check_Allocator_Discrim_Accessibility_Exprs
5602 (Disc_Exp, Typ);
5603 end if;
5605 Next_Discriminant (Discrim);
5607 if Present (Discrim) then
5608 if Present (Assoc) then
5609 Next (Assoc);
5610 Disc_Exp := Expression (Assoc);
5612 elsif Present (Next (Disc_Exp)) then
5613 Next (Disc_Exp);
5615 else
5616 Assoc := First (Component_Associations (Aggr));
5618 if Present (Assoc) then
5619 Disc_Exp := Expression (Assoc);
5620 else
5621 Disc_Exp := Empty;
5622 end if;
5623 end if;
5624 end if;
5625 end loop;
5626 end if;
5627 end if;
5629 -- For a subtype mark or subtype indication, freeze the subtype
5631 else
5632 Freeze_Expression (E);
5634 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5635 Error_Msg_N
5636 ("initialization required for access-to-constant allocator", N);
5637 end if;
5639 -- A special accessibility check is needed for allocators that
5640 -- constrain access discriminants. The level of the type of the
5641 -- expression used to constrain an access discriminant cannot be
5642 -- deeper than the type of the allocator (in contrast to access
5643 -- parameters, where the level of the actual can be arbitrary).
5644 -- We can't use Valid_Conversion to perform this check because
5645 -- in general the type of the allocator is unrelated to the type
5646 -- of the access discriminant.
5648 if Nkind (Original_Node (E)) = N_Subtype_Indication
5649 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5650 or else Is_Local_Anonymous_Access (Typ))
5651 then
5652 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5654 if Has_Discriminants (Subtyp) then
5655 Discrim := First_Discriminant (Base_Type (Subtyp));
5656 Constr := First (Constraints (Constraint (Original_Node (E))));
5657 while Present (Discrim) and then Present (Constr) loop
5658 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5659 if Nkind (Constr) = N_Discriminant_Association then
5660 Disc_Exp := Expression (Constr);
5661 else
5662 Disc_Exp := Constr;
5663 end if;
5665 Check_Allocator_Discrim_Accessibility_Exprs
5666 (Disc_Exp, Typ);
5667 end if;
5669 Next_Discriminant (Discrim);
5670 Next (Constr);
5671 end loop;
5672 end if;
5673 end if;
5674 end if;
5676 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5677 -- check that the level of the type of the created object is not deeper
5678 -- than the level of the allocator's access type, since extensions can
5679 -- now occur at deeper levels than their ancestor types. This is a
5680 -- static accessibility level check; a run-time check is also needed in
5681 -- the case of an initialized allocator with a class-wide argument (see
5682 -- Expand_Allocator_Expression).
5684 if Ada_Version >= Ada_2005
5685 and then Is_Class_Wide_Type (Desig_T)
5686 then
5687 declare
5688 Exp_Typ : Entity_Id;
5690 begin
5691 if Nkind (E) = N_Qualified_Expression then
5692 Exp_Typ := Etype (E);
5693 elsif Nkind (E) = N_Subtype_Indication then
5694 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5695 else
5696 Exp_Typ := Entity (E);
5697 end if;
5699 if Type_Access_Level (Exp_Typ) >
5700 Deepest_Type_Access_Level (Typ)
5701 then
5702 if In_Instance_Body then
5703 Error_Msg_Warn := SPARK_Mode /= On;
5704 Error_Msg_N
5705 ("type in allocator has deeper level than designated "
5706 & "class-wide type<<", E);
5707 Error_Msg_N ("\Program_Error [<<", E);
5709 Rewrite (N,
5710 Make_Raise_Program_Error (Sloc (N),
5711 Reason => PE_Accessibility_Check_Failed));
5712 Set_Etype (N, Typ);
5714 -- Do not apply Ada 2005 accessibility checks on a class-wide
5715 -- allocator if the type given in the allocator is a formal
5716 -- type or within a formal package. A run-time check will be
5717 -- performed in the instance.
5719 elsif not Is_Generic_Type (Exp_Typ)
5720 and then not In_Generic_Formal_Package (Exp_Typ)
5721 then
5722 Error_Msg_N
5723 ("type in allocator has deeper level than designated "
5724 & "class-wide type", E);
5725 end if;
5726 end if;
5727 end;
5728 end if;
5730 -- Check for allocation from an empty storage pool. But do not complain
5731 -- if it's a return statement for a build-in-place function, because the
5732 -- allocator is there just in case the caller uses an allocator. If the
5733 -- caller does use an allocator, it will be caught at the call site.
5735 if No_Pool_Assigned (Typ)
5736 and then not For_Special_Return_Object (N)
5737 then
5738 Error_Msg_N ("allocation from empty storage pool!", N);
5740 -- If the context is an unchecked conversion, as may happen within an
5741 -- inlined subprogram, the allocator is being resolved with its own
5742 -- anonymous type. In that case, if the target type has a specific
5743 -- storage pool, it must be inherited explicitly by the allocator type.
5745 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5746 and then No (Associated_Storage_Pool (Typ))
5747 then
5748 Set_Associated_Storage_Pool
5749 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5750 end if;
5752 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5753 Check_Restriction (No_Anonymous_Allocators, N);
5754 end if;
5756 -- Check that an allocator with task parts isn't for a nested access
5757 -- type when restriction No_Task_Hierarchy applies.
5759 if not Is_Library_Level_Entity (Base_Type (Typ))
5760 and then Has_Task (Base_Type (Desig_T))
5761 then
5762 Check_Restriction (No_Task_Hierarchy, N);
5763 end if;
5765 -- An illegal allocator may be rewritten as a raise Program_Error
5766 -- statement.
5768 if Nkind (N) = N_Allocator then
5770 -- Avoid coextension processing for an allocator that is the
5771 -- expansion of a build-in-place function call.
5773 if Nkind (Original_Node (N)) = N_Allocator
5774 and then Nkind (Expression (Original_Node (N))) =
5775 N_Qualified_Expression
5776 and then Nkind (Expression (Expression (Original_Node (N)))) =
5777 N_Function_Call
5778 and then Is_Expanded_Build_In_Place_Call
5779 (Expression (Expression (Original_Node (N))))
5780 then
5781 null; -- b-i-p function call case
5783 else
5784 -- An anonymous access discriminant is the definition of a
5785 -- coextension.
5787 if Ekind (Typ) = E_Anonymous_Access_Type
5788 and then Nkind (Associated_Node_For_Itype (Typ)) =
5789 N_Discriminant_Specification
5790 then
5791 declare
5792 Discr : constant Entity_Id :=
5793 Defining_Identifier (Associated_Node_For_Itype (Typ));
5795 begin
5796 Check_Restriction (No_Coextensions, N);
5798 -- Ada 2012 AI05-0052: If the designated type of the
5799 -- allocator is limited, then the allocator shall not
5800 -- be used to define the value of an access discriminant
5801 -- unless the discriminated type is immutably limited.
5803 if Ada_Version >= Ada_2012
5804 and then Is_Limited_Type (Desig_T)
5805 and then not Is_Inherently_Limited_Type (Scope (Discr))
5806 then
5807 Error_Msg_N
5808 ("only immutably limited types can have anonymous "
5809 & "access discriminants designating a limited type",
5811 end if;
5812 end;
5814 -- Avoid marking an allocator as a dynamic coextension if it is
5815 -- within a static construct.
5817 if not Is_Static_Coextension (N) then
5818 Set_Is_Dynamic_Coextension (N);
5820 -- Finalization and deallocation of coextensions utilizes an
5821 -- approximate implementation which does not directly adhere
5822 -- to the semantic rules. Warn on potential issues involving
5823 -- coextensions.
5825 if Is_Controlled (Desig_T) then
5826 Error_Msg_N
5827 ("??coextension will not be finalized when its "
5828 & "associated owner is deallocated or finalized", N);
5829 else
5830 Error_Msg_N
5831 ("??coextension will not be deallocated when its "
5832 & "associated owner is deallocated", N);
5833 end if;
5834 end if;
5836 -- Cleanup for potential static coextensions
5838 else
5839 Set_Is_Dynamic_Coextension (N, False);
5840 Set_Is_Static_Coextension (N, False);
5842 -- Objects allocated through anonymous access types are not
5843 -- finalized on time because this involves run-time ownership
5844 -- and currently this property is not available. In rare cases
5845 -- the object might not be finalized at all. Warn on potential
5846 -- issues involving anonymous access-to-controlled types.
5848 if Ekind (Typ) = E_Anonymous_Access_Type
5849 and then Is_Controlled_Active (Desig_T)
5850 then
5851 Error_Msg_N
5852 ("??object designated by anonymous access value might "
5853 & "not be finalized until its enclosing library unit "
5854 & "goes out of scope, or not be finalized at all", N);
5855 Error_Msg_N ("\use named access type instead", N);
5856 end if;
5857 end if;
5858 end if;
5859 end if;
5861 -- Report a simple error: if the designated object is a local task,
5862 -- its body has not been seen yet, and its activation will fail an
5863 -- elaboration check.
5865 if Is_Task_Type (Desig_T)
5866 and then Scope (Base_Type (Desig_T)) = Current_Scope
5867 and then Is_Compilation_Unit (Current_Scope)
5868 and then Ekind (Current_Scope) = E_Package
5869 and then not In_Package_Body (Current_Scope)
5870 then
5871 Error_Msg_Warn := SPARK_Mode /= On;
5872 Error_Msg_N ("cannot activate task before body seen<<", N);
5873 Error_Msg_N ("\Program_Error [<<", N);
5874 end if;
5876 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5877 -- type with a task component on a subpool. This action must raise
5878 -- Program_Error at runtime.
5880 if Ada_Version >= Ada_2012
5881 and then Nkind (N) = N_Allocator
5882 and then Present (Subpool_Handle_Name (N))
5883 and then Has_Task (Desig_T)
5884 then
5885 Error_Msg_Warn := SPARK_Mode /= On;
5886 Error_Msg_N ("cannot allocate task on subpool<<", N);
5887 Error_Msg_N ("\Program_Error [<<", N);
5889 Rewrite (N,
5890 Make_Raise_Program_Error (Sloc (N),
5891 Reason => PE_Explicit_Raise));
5892 Set_Etype (N, Typ);
5893 end if;
5894 end Resolve_Allocator;
5896 ---------------------------
5897 -- Resolve_Arithmetic_Op --
5898 ---------------------------
5900 -- Used for resolving all arithmetic operators except exponentiation
5902 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5903 L : constant Node_Id := Left_Opnd (N);
5904 R : constant Node_Id := Right_Opnd (N);
5905 TL : constant Entity_Id := Base_Type (Etype (L));
5906 TR : constant Entity_Id := Base_Type (Etype (R));
5907 T : Entity_Id;
5908 Rop : Node_Id;
5910 B_Typ : constant Entity_Id := Base_Type (Typ);
5911 -- We do the resolution using the base type, because intermediate values
5912 -- in expressions always are of the base type, not a subtype of it.
5914 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5915 -- Returns True if N is in a context that expects "any real type"
5917 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5918 -- Return True iff given type is Integer or universal real/integer
5920 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5921 -- Choose type of integer literal in fixed-point operation to conform
5922 -- to available fixed-point type. T is the type of the other operand,
5923 -- which is needed to determine the expected type of N.
5925 procedure Set_Operand_Type (N : Node_Id);
5926 -- Set operand type to T if universal
5928 -------------------------------
5929 -- Expected_Type_Is_Any_Real --
5930 -------------------------------
5932 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5933 begin
5934 -- N is the expression after "delta" in a fixed_point_definition;
5935 -- see RM-3.5.9(6):
5937 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5938 | N_Decimal_Fixed_Point_Definition
5940 -- N is one of the bounds in a real_range_specification;
5941 -- see RM-3.5.7(5):
5943 | N_Real_Range_Specification
5945 -- N is the expression of a delta_constraint;
5946 -- see RM-J.3(3):
5948 | N_Delta_Constraint;
5949 end Expected_Type_Is_Any_Real;
5951 -----------------------------
5952 -- Is_Integer_Or_Universal --
5953 -----------------------------
5955 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5956 T : Entity_Id;
5957 Index : Interp_Index;
5958 It : Interp;
5960 begin
5961 if not Is_Overloaded (N) then
5962 T := Etype (N);
5963 return Base_Type (T) = Base_Type (Standard_Integer)
5964 or else Is_Universal_Numeric_Type (T);
5965 else
5966 Get_First_Interp (N, Index, It);
5967 while Present (It.Typ) loop
5968 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5969 or else Is_Universal_Numeric_Type (It.Typ)
5970 then
5971 return True;
5972 end if;
5974 Get_Next_Interp (Index, It);
5975 end loop;
5976 end if;
5978 return False;
5979 end Is_Integer_Or_Universal;
5981 ----------------------------
5982 -- Set_Mixed_Mode_Operand --
5983 ----------------------------
5985 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5986 Index : Interp_Index;
5987 It : Interp;
5989 begin
5990 if Universal_Interpretation (N) = Universal_Integer then
5992 -- A universal integer literal is resolved as standard integer
5993 -- except in the case of a fixed-point result, where we leave it
5994 -- as universal (to be handled by Exp_Fixd later on)
5996 if Is_Fixed_Point_Type (T) then
5997 Resolve (N, Universal_Integer);
5998 else
5999 Resolve (N, Standard_Integer);
6000 end if;
6002 elsif Universal_Interpretation (N) = Universal_Real
6003 and then (T = Base_Type (Standard_Integer)
6004 or else Is_Universal_Numeric_Type (T))
6005 then
6006 -- A universal real can appear in a fixed-type context. We resolve
6007 -- the literal with that context, even though this might raise an
6008 -- exception prematurely (the other operand may be zero).
6010 Resolve (N, B_Typ);
6012 elsif Etype (N) = Base_Type (Standard_Integer)
6013 and then T = Universal_Real
6014 and then Is_Overloaded (N)
6015 then
6016 -- Integer arg in mixed-mode operation. Resolve with universal
6017 -- type, in case preference rule must be applied.
6019 Resolve (N, Universal_Integer);
6021 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
6023 -- If the operand is part of a fixed multiplication operation,
6024 -- a conversion will be applied to each operand, so resolve it
6025 -- with its own type.
6027 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
6028 Resolve (N);
6030 else
6031 -- Not a mixed-mode operation, resolve with context
6033 Resolve (N, B_Typ);
6034 end if;
6036 elsif Etype (N) = Any_Fixed then
6038 -- N may itself be a mixed-mode operation, so use context type
6040 Resolve (N, B_Typ);
6042 elsif Is_Fixed_Point_Type (T)
6043 and then B_Typ = Universal_Fixed
6044 and then Is_Overloaded (N)
6045 then
6046 -- Must be (fixed * fixed) operation, operand must have one
6047 -- compatible interpretation.
6049 Resolve (N, Any_Fixed);
6051 elsif Is_Fixed_Point_Type (B_Typ)
6052 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
6053 and then Is_Overloaded (N)
6054 then
6055 -- C * F(X) in a fixed context, where C is a real literal or a
6056 -- fixed-point expression. F must have either a fixed type
6057 -- interpretation or an integer interpretation, but not both.
6059 Get_First_Interp (N, Index, It);
6060 while Present (It.Typ) loop
6061 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
6062 if Analyzed (N) then
6063 Error_Msg_N ("ambiguous operand in fixed operation", N);
6064 else
6065 Resolve (N, Standard_Integer);
6066 end if;
6068 elsif Is_Fixed_Point_Type (It.Typ) then
6069 if Analyzed (N) then
6070 Error_Msg_N ("ambiguous operand in fixed operation", N);
6071 else
6072 Resolve (N, It.Typ);
6073 end if;
6074 end if;
6076 Get_Next_Interp (Index, It);
6077 end loop;
6079 -- Reanalyze the literal with the fixed type of the context. If
6080 -- context is Universal_Fixed, we are within a conversion, leave
6081 -- the literal as a universal real because there is no usable
6082 -- fixed type, and the target of the conversion plays no role in
6083 -- the resolution.
6085 declare
6086 Op2 : Node_Id;
6087 T2 : Entity_Id;
6089 begin
6090 if N = L then
6091 Op2 := R;
6092 else
6093 Op2 := L;
6094 end if;
6096 if B_Typ = Universal_Fixed
6097 and then Nkind (Op2) = N_Real_Literal
6098 then
6099 T2 := Universal_Real;
6100 else
6101 T2 := B_Typ;
6102 end if;
6104 Set_Analyzed (Op2, False);
6105 Resolve (Op2, T2);
6106 end;
6108 -- A universal real conditional expression can appear in a fixed-type
6109 -- context and must be resolved with that context to facilitate the
6110 -- code generation in the back end. However, If the context is
6111 -- Universal_fixed (i.e. as an operand of a multiplication/division
6112 -- involving a fixed-point operand) the conditional expression must
6113 -- resolve to a unique visible fixed_point type, normally Duration.
6115 elsif Nkind (N) in N_Case_Expression | N_If_Expression
6116 and then Etype (N) = Universal_Real
6117 and then Is_Fixed_Point_Type (B_Typ)
6118 then
6119 if B_Typ = Universal_Fixed then
6120 Resolve (N, Unique_Fixed_Point_Type (N));
6122 else
6123 Resolve (N, B_Typ);
6124 end if;
6126 else
6127 Resolve (N);
6128 end if;
6129 end Set_Mixed_Mode_Operand;
6131 ----------------------
6132 -- Set_Operand_Type --
6133 ----------------------
6135 procedure Set_Operand_Type (N : Node_Id) is
6136 begin
6137 if Is_Universal_Numeric_Type (Etype (N)) then
6138 Set_Etype (N, T);
6139 end if;
6140 end Set_Operand_Type;
6142 -- Start of processing for Resolve_Arithmetic_Op
6144 begin
6145 if Ekind (Entity (N)) = E_Function
6146 and then Is_Imported (Entity (N))
6147 and then Is_Intrinsic_Subprogram (Entity (N))
6148 then
6149 Generate_Reference (Entity (N), N);
6150 Resolve_Intrinsic_Operator (N, Typ);
6151 return;
6153 -- Special-case for mixed-mode universal expressions or fixed point type
6154 -- operation: each argument is resolved separately. The same treatment
6155 -- is required if one of the operands of a fixed point operation is
6156 -- universal real, since in this case we don't do a conversion to a
6157 -- specific fixed-point type (instead the expander handles the case).
6159 -- Set the type of the node to its universal interpretation because
6160 -- legality checks on an exponentiation operand need the context.
6162 elsif Is_Universal_Numeric_Type (B_Typ)
6163 and then Present (Universal_Interpretation (L))
6164 and then Present (Universal_Interpretation (R))
6165 then
6166 Set_Etype (N, B_Typ);
6167 Resolve (L, Universal_Interpretation (L));
6168 Resolve (R, Universal_Interpretation (R));
6170 elsif (B_Typ = Universal_Real
6171 or else Etype (N) = Universal_Fixed
6172 or else (Etype (N) = Any_Fixed
6173 and then Is_Fixed_Point_Type (B_Typ))
6174 or else (Is_Fixed_Point_Type (B_Typ)
6175 and then (Is_Integer_Or_Universal (L)
6176 or else
6177 Is_Integer_Or_Universal (R))))
6178 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
6179 then
6180 if TL = Universal_Integer or else TR = Universal_Integer then
6181 Check_For_Visible_Operator (N, B_Typ);
6182 end if;
6184 -- If context is a fixed type and one operand is integer, the other
6185 -- is resolved with the type of the context.
6187 if Is_Fixed_Point_Type (B_Typ)
6188 and then (Base_Type (TL) = Base_Type (Standard_Integer)
6189 or else TL = Universal_Integer)
6190 then
6191 Resolve (R, B_Typ);
6192 Resolve (L, TL);
6194 elsif Is_Fixed_Point_Type (B_Typ)
6195 and then (Base_Type (TR) = Base_Type (Standard_Integer)
6196 or else TR = Universal_Integer)
6197 then
6198 Resolve (L, B_Typ);
6199 Resolve (R, TR);
6201 -- If both operands are universal and the context is a floating
6202 -- point type, the operands are resolved to the type of the context.
6204 elsif Is_Floating_Point_Type (B_Typ) then
6205 Resolve (L, B_Typ);
6206 Resolve (R, B_Typ);
6208 else
6209 Set_Mixed_Mode_Operand (L, TR);
6210 Set_Mixed_Mode_Operand (R, TL);
6211 end if;
6213 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6214 -- multiplying operators from being used when the expected type is
6215 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6216 -- some cases where the expected type is actually Any_Real;
6217 -- Expected_Type_Is_Any_Real takes care of that case.
6219 if Etype (N) = Universal_Fixed
6220 or else Etype (N) = Any_Fixed
6221 then
6222 if B_Typ = Universal_Fixed
6223 and then not Expected_Type_Is_Any_Real (N)
6224 and then Nkind (Parent (N)) not in
6225 N_Type_Conversion | N_Unchecked_Type_Conversion
6226 then
6227 Error_Msg_N ("type cannot be determined from context!", N);
6228 Error_Msg_N ("\explicit conversion to result type required", N);
6230 Set_Etype (L, Any_Type);
6231 Set_Etype (R, Any_Type);
6233 else
6234 if Ada_Version = Ada_83
6235 and then Etype (N) = Universal_Fixed
6236 and then Nkind (Parent (N)) not in
6237 N_Type_Conversion | N_Unchecked_Type_Conversion
6238 then
6239 Error_Msg_N
6240 ("(Ada 83) fixed-point operation needs explicit "
6241 & "conversion", N);
6242 end if;
6244 -- The expected type is "any real type" in contexts like
6246 -- type T is delta <universal_fixed-expression> ...
6248 -- in which case we need to set the type to Universal_Real
6249 -- so that static expression evaluation will work properly.
6251 if Expected_Type_Is_Any_Real (N) then
6252 Set_Etype (N, Universal_Real);
6253 else
6254 Set_Etype (N, B_Typ);
6255 end if;
6256 end if;
6258 elsif Is_Fixed_Point_Type (B_Typ)
6259 and then (Is_Integer_Or_Universal (L)
6260 or else Nkind (L) = N_Real_Literal
6261 or else Nkind (R) = N_Real_Literal
6262 or else Is_Integer_Or_Universal (R))
6263 then
6264 Set_Etype (N, B_Typ);
6266 elsif Etype (N) = Any_Fixed then
6268 -- If no previous errors, this is only possible if one operand is
6269 -- overloaded and the context is universal. Resolve as such.
6271 Set_Etype (N, B_Typ);
6272 end if;
6274 else
6275 if Is_Universal_Numeric_Type (TL)
6276 and then
6277 Is_Universal_Numeric_Type (TR)
6278 then
6279 Check_For_Visible_Operator (N, B_Typ);
6280 end if;
6282 -- If the context is Universal_Fixed and the operands are also
6283 -- universal fixed, this is an error, unless there is only one
6284 -- applicable fixed_point type (usually Duration).
6286 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6287 T := Unique_Fixed_Point_Type (N);
6289 if T = Any_Type then
6290 Set_Etype (N, T);
6291 return;
6292 else
6293 Resolve (L, T);
6294 Resolve (R, T);
6295 end if;
6297 else
6298 Resolve (L, B_Typ);
6299 Resolve (R, B_Typ);
6300 end if;
6302 -- If one of the arguments was resolved to a non-universal type.
6303 -- label the result of the operation itself with the same type.
6304 -- Do the same for the universal argument, if any.
6306 T := Intersect_Types (L, R);
6307 Set_Etype (N, Base_Type (T));
6308 Set_Operand_Type (L);
6309 Set_Operand_Type (R);
6310 end if;
6312 Generate_Operator_Reference (N, Typ);
6313 Analyze_Dimension (N);
6314 Eval_Arithmetic_Op (N);
6316 -- Set overflow and division checking bit
6318 if Nkind (N) in N_Op then
6319 if not Overflow_Checks_Suppressed (Etype (N)) then
6320 Enable_Overflow_Check (N);
6321 end if;
6323 -- Give warning if explicit division by zero
6325 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6326 and then not Division_Checks_Suppressed (Etype (N))
6327 then
6328 Rop := Right_Opnd (N);
6330 if Compile_Time_Known_Value (Rop)
6331 and then ((Is_Integer_Type (Etype (Rop))
6332 and then Expr_Value (Rop) = Uint_0)
6333 or else
6334 (Is_Real_Type (Etype (Rop))
6335 and then Expr_Value_R (Rop) = Ureal_0))
6336 then
6337 -- Specialize the warning message according to the operation.
6338 -- When SPARK_Mode is On, force a warning instead of an error
6339 -- in that case, as this likely corresponds to deactivated
6340 -- code. The following warnings are for the case
6342 case Nkind (N) is
6343 when N_Op_Divide =>
6345 -- For division, we have two cases, for float division
6346 -- of an unconstrained float type, on a machine where
6347 -- Machine_Overflows is false, we don't get an exception
6348 -- at run-time, but rather an infinity or Nan. The Nan
6349 -- case is pretty obscure, so just warn about infinities.
6351 if Is_Floating_Point_Type (Typ)
6352 and then not Is_Constrained (Typ)
6353 and then not Machine_Overflows_On_Target
6354 then
6355 Error_Msg_N
6356 ("float division by zero, may generate "
6357 & "'+'/'- infinity??", Right_Opnd (N));
6359 -- For all other cases, we get a Constraint_Error
6361 else
6362 Apply_Compile_Time_Constraint_Error
6363 (N, "division by zero??", CE_Divide_By_Zero,
6364 Loc => Sloc (Right_Opnd (N)),
6365 Warn => SPARK_Mode = On);
6366 end if;
6368 when N_Op_Rem =>
6369 Apply_Compile_Time_Constraint_Error
6370 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6371 Loc => Sloc (Right_Opnd (N)),
6372 Warn => SPARK_Mode = On);
6374 when N_Op_Mod =>
6375 Apply_Compile_Time_Constraint_Error
6376 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6377 Loc => Sloc (Right_Opnd (N)),
6378 Warn => SPARK_Mode = On);
6380 -- Division by zero can only happen with division, rem,
6381 -- and mod operations.
6383 when others =>
6384 raise Program_Error;
6385 end case;
6387 -- Otherwise just set the flag to check at run time
6389 else
6390 Activate_Division_Check (N);
6391 end if;
6392 end if;
6394 -- If Restriction No_Implicit_Conditionals is active, then it is
6395 -- violated if either operand can be negative for mod, or for rem
6396 -- if both operands can be negative.
6398 if Restriction_Check_Required (No_Implicit_Conditionals)
6399 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6400 then
6401 declare
6402 Lo : Uint;
6403 Hi : Uint;
6404 OK : Boolean;
6406 LNeg : Boolean;
6407 RNeg : Boolean;
6408 -- Set if corresponding operand might be negative
6410 begin
6411 Determine_Range
6412 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6413 LNeg := not OK or else Lo < 0;
6415 Determine_Range
6416 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6417 RNeg := not OK or else Lo < 0;
6419 -- Check if we will be generating conditionals. There are two
6420 -- cases where that can happen, first for REM, the only case
6421 -- is largest negative integer mod -1, where the division can
6422 -- overflow, but we still have to give the right result. The
6423 -- front end generates a test for this annoying case. Here we
6424 -- just test if both operands can be negative (that's what the
6425 -- expander does, so we match its logic here).
6427 -- The second case is mod where either operand can be negative.
6428 -- In this case, the back end has to generate additional tests.
6430 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6431 or else
6432 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6433 then
6434 Check_Restriction (No_Implicit_Conditionals, N);
6435 end if;
6436 end;
6437 end if;
6438 end if;
6440 Check_Unset_Reference (L);
6441 Check_Unset_Reference (R);
6442 end Resolve_Arithmetic_Op;
6444 ------------------
6445 -- Resolve_Call --
6446 ------------------
6448 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6449 Loc : constant Source_Ptr := Sloc (N);
6450 Subp : constant Node_Id := Name (N);
6451 Body_Id : Entity_Id;
6452 I : Interp_Index;
6453 It : Interp;
6454 Nam : Entity_Id;
6455 Nam_Decl : Node_Id;
6456 Nam_UA : Entity_Id;
6457 Norm_OK : Boolean;
6458 Rtype : Entity_Id;
6459 Scop : Entity_Id;
6461 begin
6462 -- Preserve relevant elaboration-related attributes of the context which
6463 -- are no longer available or very expensive to recompute once analysis,
6464 -- resolution, and expansion are over.
6466 Mark_Elaboration_Attributes
6467 (N_Id => N,
6468 Checks => True,
6469 Modes => True,
6470 Warnings => True);
6472 -- The context imposes a unique interpretation with type Typ on a
6473 -- procedure or function call. Find the entity of the subprogram that
6474 -- yields the expected type, and propagate the corresponding formal
6475 -- constraints on the actuals. The caller has established that an
6476 -- interpretation exists, and emitted an error if not unique.
6478 -- First deal with the case of a call to an access-to-subprogram,
6479 -- dereference made explicit in Analyze_Call.
6481 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6482 if not Is_Overloaded (Subp) then
6483 Nam := Etype (Subp);
6485 else
6486 -- Find the interpretation whose type (a subprogram type) has a
6487 -- return type that is compatible with the context. Analysis of
6488 -- the node has established that one exists.
6490 Nam := Empty;
6492 Get_First_Interp (Subp, I, It);
6493 while Present (It.Typ) loop
6494 if Covers (Typ, Etype (It.Typ)) then
6495 Nam := It.Typ;
6496 exit;
6497 end if;
6499 Get_Next_Interp (I, It);
6500 end loop;
6502 if No (Nam) then
6503 raise Program_Error;
6504 end if;
6505 end if;
6507 -- If the prefix is not an entity, then resolve it
6509 if not Is_Entity_Name (Subp) then
6510 Resolve (Subp, Nam);
6511 end if;
6513 -- For an indirect call, we always invalidate checks, since we do not
6514 -- know whether the subprogram is local or global. Yes we could do
6515 -- better here, e.g. by knowing that there are no local subprograms,
6516 -- but it does not seem worth the effort. Similarly, we kill all
6517 -- knowledge of current constant values.
6519 Kill_Current_Values;
6521 -- If this is a procedure call which is really an entry call, do
6522 -- the conversion of the procedure call to an entry call. Protected
6523 -- operations use the same circuitry because the name in the call
6524 -- can be an arbitrary expression with special resolution rules.
6526 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6527 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6528 then
6529 Resolve_Entry_Call (N, Typ);
6531 if Legacy_Elaboration_Checks then
6532 Check_Elab_Call (N);
6533 end if;
6535 -- Annotate the tree by creating a call marker in case the original
6536 -- call is transformed by expansion. The call marker is automatically
6537 -- saved for later examination by the ABE Processing phase.
6539 Build_Call_Marker (N);
6541 -- Kill checks and constant values, as above for indirect case
6542 -- Who knows what happens when another task is activated?
6544 Kill_Current_Values;
6545 return;
6547 -- Normal subprogram call with name established in Resolve
6549 elsif not Is_Type (Entity (Subp)) then
6550 Nam := Entity (Subp);
6551 Set_Entity_With_Checks (Subp, Nam);
6553 -- Otherwise we must have the case of an overloaded call
6555 else
6556 pragma Assert (Is_Overloaded (Subp));
6558 -- Initialize Nam to prevent warning (we know it will be assigned
6559 -- in the loop below, but the compiler does not know that).
6561 Nam := Empty;
6563 Get_First_Interp (Subp, I, It);
6564 while Present (It.Typ) loop
6565 if Covers (Typ, It.Typ) then
6566 Nam := It.Nam;
6567 Set_Entity_With_Checks (Subp, Nam);
6568 exit;
6569 end if;
6571 Get_Next_Interp (I, It);
6572 end loop;
6573 end if;
6575 -- Check that a call to Current_Task does not occur in an entry body
6577 if Is_RTE (Nam, RE_Current_Task) then
6578 declare
6579 P : Node_Id;
6581 begin
6582 P := N;
6583 loop
6584 P := Parent (P);
6586 -- Exclude calls that occur within the default of a formal
6587 -- parameter of the entry, since those are evaluated outside
6588 -- of the body.
6590 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6592 if Nkind (P) = N_Entry_Body
6593 or else (Nkind (P) = N_Subprogram_Body
6594 and then Is_Entry_Barrier_Function (P))
6595 then
6596 Rtype := Etype (N);
6597 Error_Msg_Warn := SPARK_Mode /= On;
6598 Error_Msg_NE
6599 ("& should not be used in entry body (RM C.7(17))<<",
6600 N, Nam);
6601 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6602 Rewrite (N,
6603 Make_Raise_Program_Error (Loc,
6604 Reason => PE_Current_Task_In_Entry_Body));
6605 Set_Etype (N, Rtype);
6606 return;
6607 end if;
6608 end loop;
6609 end;
6610 end if;
6612 -- Check that a procedure call does not occur in the context of the
6613 -- entry call statement of a conditional or timed entry call. Note that
6614 -- the case of a call to a subprogram renaming of an entry will also be
6615 -- rejected. The test for N not being an N_Entry_Call_Statement is
6616 -- defensive, covering the possibility that the processing of entry
6617 -- calls might reach this point due to later modifications of the code
6618 -- above.
6620 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6621 and then Nkind (N) /= N_Entry_Call_Statement
6622 and then Entry_Call_Statement (Parent (N)) = N
6623 then
6624 if Ada_Version < Ada_2005 then
6625 Error_Msg_N ("entry call required in select statement", N);
6627 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6628 -- for a procedure_or_entry_call, the procedure_name or
6629 -- procedure_prefix of the procedure_call_statement shall denote
6630 -- an entry renamed by a procedure, or (a view of) a primitive
6631 -- subprogram of a limited interface whose first parameter is
6632 -- a controlling parameter.
6634 elsif Nkind (N) = N_Procedure_Call_Statement
6635 and then not Is_Renamed_Entry (Nam)
6636 and then not Is_Controlling_Limited_Procedure (Nam)
6637 then
6638 Error_Msg_N
6639 ("entry call or dispatching primitive of interface required", N);
6640 end if;
6641 end if;
6643 -- Check that this is not a call to a protected procedure or entry from
6644 -- within a protected function.
6646 Check_Internal_Protected_Use (N, Nam);
6648 -- Freeze the subprogram name if not in a spec-expression. Note that
6649 -- we freeze procedure calls as well as function calls. Procedure calls
6650 -- are not frozen according to the rules (RM 13.14(14)) because it is
6651 -- impossible to have a procedure call to a non-frozen procedure in
6652 -- pure Ada, but in the code that we generate in the expander, this
6653 -- rule needs extending because we can generate procedure calls that
6654 -- need freezing.
6656 -- In Ada 2012, expression functions may be called within pre/post
6657 -- conditions of subsequent functions or expression functions. Such
6658 -- calls do not freeze when they appear within generated bodies,
6659 -- (including the body of another expression function) which would
6660 -- place the freeze node in the wrong scope. An expression function
6661 -- is frozen in the usual fashion, by the appearance of a real body,
6662 -- or at the end of a declarative part. However an implicit call to
6663 -- an expression function may appear when it is part of a default
6664 -- expression in a call to an initialization procedure, and must be
6665 -- frozen now, even if the body is inserted at a later point.
6666 -- Otherwise, the call freezes the expression if expander is active,
6667 -- for example as part of an object declaration.
6669 if Is_Entity_Name (Subp)
6670 and then not In_Spec_Expression
6671 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6672 and then not (Chars (Current_Scope) = Name_uWrapped_Statements
6673 and then Is_Expression_Function_Or_Completion
6674 (Scope (Current_Scope)))
6675 and then
6676 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6677 or else Expander_Active)
6678 then
6679 if Is_Expression_Function (Entity (Subp)) then
6681 -- Force freeze of expression function in call
6683 Set_Comes_From_Source (Subp, True);
6684 Set_Must_Not_Freeze (Subp, False);
6685 end if;
6687 Freeze_Expression (Subp);
6688 end if;
6690 -- For a predefined operator, the type of the result is the type imposed
6691 -- by context, except for a predefined operation on universal fixed.
6692 -- Otherwise the type of the call is the type returned by the subprogram
6693 -- being called.
6695 if Is_Predefined_Op (Nam) then
6696 if Etype (N) /= Universal_Fixed then
6697 Set_Etype (N, Typ);
6698 end if;
6700 -- If the subprogram returns an array type, and the context requires the
6701 -- component type of that array type, the node is really an indexing of
6702 -- the parameterless call. Resolve as such. A pathological case occurs
6703 -- when the type of the component is an access to the array type. In
6704 -- this case the call is truly ambiguous. If the call is to an intrinsic
6705 -- subprogram, it can't be an indexed component. This check is necessary
6706 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6707 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6708 -- pointers to the same array), the compiler gets confused and does an
6709 -- infinite recursion.
6711 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6712 and then
6713 ((Is_Array_Type (Etype (Nam))
6714 and then Covers (Typ, Component_Type (Etype (Nam))))
6715 or else
6716 (Is_Access_Type (Etype (Nam))
6717 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6718 and then
6719 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6720 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6721 then
6722 declare
6723 Index_Node : Node_Id;
6724 New_Subp : Node_Id;
6725 Ret_Type : constant Entity_Id := Etype (Nam);
6727 begin
6728 -- If this is a parameterless call there is no ambiguity and the
6729 -- call has the type of the function.
6731 if No (First_Actual (N)) then
6732 Set_Etype (N, Etype (Nam));
6734 if Present (First_Formal (Nam)) then
6735 Resolve_Actuals (N, Nam);
6736 end if;
6738 -- Annotate the tree by creating a call marker in case the
6739 -- original call is transformed by expansion. The call marker
6740 -- is automatically saved for later examination by the ABE
6741 -- Processing phase.
6743 Build_Call_Marker (N);
6745 elsif Is_Access_Type (Ret_Type)
6747 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6748 then
6749 Error_Msg_N
6750 ("cannot disambiguate function call and indexing", N);
6751 else
6752 New_Subp := Relocate_Node (Subp);
6754 -- The called entity may be an explicit dereference, in which
6755 -- case there is no entity to set.
6757 if Nkind (New_Subp) /= N_Explicit_Dereference then
6758 Set_Entity (Subp, Nam);
6759 end if;
6761 if (Is_Array_Type (Ret_Type)
6762 and then Component_Type (Ret_Type) /= Any_Type)
6763 or else
6764 (Is_Access_Type (Ret_Type)
6765 and then
6766 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6767 then
6768 if Needs_No_Actuals (Nam) then
6770 -- Indexed call to a parameterless function
6772 Index_Node :=
6773 Make_Indexed_Component (Loc,
6774 Prefix =>
6775 Make_Function_Call (Loc, Name => New_Subp),
6776 Expressions => Parameter_Associations (N));
6777 else
6778 -- An Ada 2005 prefixed call to a primitive operation
6779 -- whose first parameter is the prefix. This prefix was
6780 -- prepended to the parameter list, which is actually a
6781 -- list of indexes. Remove the prefix in order to build
6782 -- the proper indexed component.
6784 Index_Node :=
6785 Make_Indexed_Component (Loc,
6786 Prefix =>
6787 Make_Function_Call (Loc,
6788 Name => New_Subp,
6789 Parameter_Associations =>
6790 New_List
6791 (Remove_Head (Parameter_Associations (N)))),
6792 Expressions => Parameter_Associations (N));
6793 end if;
6795 -- Preserve the parenthesis count of the node
6797 Set_Paren_Count (Index_Node, Paren_Count (N));
6799 -- Since we are correcting a node classification error made
6800 -- by the parser, we call Replace rather than Rewrite.
6802 Replace (N, Index_Node);
6804 Set_Etype (Prefix (N), Ret_Type);
6805 Set_Etype (N, Typ);
6807 if Legacy_Elaboration_Checks then
6808 Check_Elab_Call (Prefix (N));
6809 end if;
6811 -- Annotate the tree by creating a call marker in case
6812 -- the original call is transformed by expansion. The call
6813 -- marker is automatically saved for later examination by
6814 -- the ABE Processing phase.
6816 Build_Call_Marker (Prefix (N));
6818 Resolve_Indexed_Component (N, Typ);
6819 end if;
6820 end if;
6822 return;
6823 end;
6825 else
6826 -- If the called function is not declared in the main unit and it
6827 -- returns the limited view of type then use the available view (as
6828 -- is done in Try_Object_Operation) to prevent back-end confusion;
6829 -- for the function entity itself. The call must appear in a context
6830 -- where the nonlimited view is available. If the function entity is
6831 -- in the extended main unit then no action is needed, because the
6832 -- back end handles this case. In either case the type of the call
6833 -- is the nonlimited view.
6835 if From_Limited_With (Etype (Nam))
6836 and then Present (Available_View (Etype (Nam)))
6837 then
6838 Set_Etype (N, Available_View (Etype (Nam)));
6840 if not In_Extended_Main_Code_Unit (Nam) then
6841 Set_Etype (Nam, Available_View (Etype (Nam)));
6842 end if;
6844 else
6845 Set_Etype (N, Etype (Nam));
6846 end if;
6847 end if;
6849 -- In the case where the call is to an overloaded subprogram, Analyze
6850 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6851 -- such a case Normalize_Actuals needs to be called once more to order
6852 -- the actuals correctly. Otherwise the call will have the ordering
6853 -- given by the last overloaded subprogram whether this is the correct
6854 -- one being called or not.
6856 if Is_Overloaded (Subp) then
6857 Normalize_Actuals (N, Nam, False, Norm_OK);
6858 pragma Assert (Norm_OK);
6859 end if;
6861 -- In any case, call is fully resolved now. Reset Overload flag, to
6862 -- prevent subsequent overload resolution if node is analyzed again
6864 Set_Is_Overloaded (Subp, False);
6865 Set_Is_Overloaded (N, False);
6867 -- A Ghost entity must appear in a specific context
6869 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6870 Check_Ghost_Context (Nam, N);
6871 end if;
6873 if Is_Entity_Name (Subp) then
6874 Local_Restrict.Check_Call
6875 (Call => N, Callee => Ultimate_Alias (Nam));
6876 else
6877 Local_Restrict.Check_Call (Call => N);
6878 end if;
6880 -- If we are calling the current subprogram from immediately within its
6881 -- body, then that is the case where we can sometimes detect cases of
6882 -- infinite recursion statically. Do not try this in case restriction
6883 -- No_Recursion is in effect anyway, and do it only for source calls.
6885 if Comes_From_Source (N) then
6886 Scop := Current_Scope;
6888 -- Issue warning for possible infinite recursion in the absence
6889 -- of the No_Recursion restriction.
6891 if Same_Or_Aliased_Subprograms (Nam, Scop)
6892 and then not Restriction_Active (No_Recursion)
6893 and then not Is_Static_Function (Scop)
6894 and then Check_Infinite_Recursion (N)
6895 then
6896 -- Here we detected and flagged an infinite recursion, so we do
6897 -- not need to test the case below for further warnings. Also we
6898 -- are all done if we now have a raise SE node.
6900 if Nkind (N) = N_Raise_Storage_Error then
6901 return;
6902 end if;
6904 -- If call is to immediately containing subprogram, then check for
6905 -- the case of a possible run-time detectable infinite recursion.
6907 else
6908 Scope_Loop : while Scop /= Standard_Standard loop
6909 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6911 -- Ada 2022 (AI12-0075): Static functions are never allowed
6912 -- to make a recursive call, as specified by 6.8(5.4/5).
6914 if Is_Static_Function (Scop) then
6915 Error_Msg_N
6916 ("recursive call not allowed in static expression "
6917 & "function", N);
6919 Set_Error_Posted (Scop);
6921 exit Scope_Loop;
6922 end if;
6924 -- Although in general case, recursion is not statically
6925 -- checkable, the case of calling an immediately containing
6926 -- subprogram is easy to catch.
6928 if not Is_Ignored_Ghost_Entity (Nam) then
6929 Check_Restriction (No_Recursion, N);
6930 end if;
6932 -- If the recursive call is to a parameterless subprogram,
6933 -- then even if we can't statically detect infinite
6934 -- recursion, this is pretty suspicious, and we output a
6935 -- warning. Furthermore, we will try later to detect some
6936 -- cases here at run time by expanding checking code (see
6937 -- Detect_Infinite_Recursion in package Exp_Ch6).
6939 -- If the recursive call is within a handler, do not emit a
6940 -- warning, because this is a common idiom: loop until input
6941 -- is correct, catch illegal input in handler and restart.
6943 if No (First_Formal (Nam))
6944 and then Etype (Nam) = Standard_Void_Type
6945 and then not Error_Posted (N)
6946 and then Nkind (Parent (N)) /= N_Exception_Handler
6947 then
6948 -- For the case of a procedure call. We give the message
6949 -- only if the call is the first statement in a sequence
6950 -- of statements, or if all previous statements are
6951 -- simple assignments. This is simply a heuristic to
6952 -- decrease false positives, without losing too many good
6953 -- warnings. The idea is that these previous statements
6954 -- may affect global variables the procedure depends on.
6955 -- We also exclude raise statements, that may arise from
6956 -- constraint checks and are probably unrelated to the
6957 -- intended control flow.
6959 if Nkind (N) = N_Procedure_Call_Statement
6960 and then Is_List_Member (N)
6961 then
6962 declare
6963 P : Node_Id;
6964 begin
6965 P := Prev (N);
6966 while Present (P) loop
6967 if Nkind (P) not in N_Assignment_Statement
6968 | N_Raise_Constraint_Error
6969 then
6970 exit Scope_Loop;
6971 end if;
6973 Prev (P);
6974 end loop;
6975 end;
6976 end if;
6978 -- Do not give warning if we are in a conditional context
6980 declare
6981 K : constant Node_Kind := Nkind (Parent (N));
6982 begin
6983 if (K = N_Loop_Statement
6984 and then Present (Iteration_Scheme (Parent (N))))
6985 or else K = N_If_Statement
6986 or else K = N_Elsif_Part
6987 or else K = N_Case_Statement_Alternative
6988 then
6989 exit Scope_Loop;
6990 end if;
6991 end;
6993 -- Here warning is to be issued
6995 Set_Has_Recursive_Call (Nam);
6996 Error_Msg_Warn := SPARK_Mode /= On;
6997 Error_Msg_N ("possible infinite recursion<<!", N);
6998 Error_Msg_N ("\Storage_Error ]<<!", N);
6999 end if;
7001 exit Scope_Loop;
7002 end if;
7004 Scop := Scope (Scop);
7005 end loop Scope_Loop;
7006 end if;
7007 end if;
7009 -- Check obsolescent reference to Ada.Characters.Handling subprogram
7011 Check_Obsolescent_2005_Entity (Nam, Subp);
7013 -- If subprogram name is a predefined operator, it was given in
7014 -- functional notation. Replace call node with operator node, so
7015 -- that actuals can be resolved appropriately.
7017 if Ekind (Nam) = E_Operator or else Is_Predefined_Op (Nam) then
7018 Make_Call_Into_Operator (N, Typ, Nam);
7019 return;
7021 elsif Present (Alias (Nam)) and then Is_Predefined_Op (Alias (Nam)) then
7022 Resolve_Actuals (N, Nam);
7023 Make_Call_Into_Operator (N, Typ, Alias (Nam));
7024 return;
7025 end if;
7027 -- Create a transient scope if the expander is active and the resulting
7028 -- type requires it.
7030 -- There are several notable exceptions:
7032 -- a) Intrinsic subprograms (Unchecked_Conversion and source info
7033 -- functions) do not use the secondary stack even though the return
7034 -- type may be unconstrained.
7036 -- b) Subprograms that are ignored ghost entities do not return anything
7038 -- c) Calls to a build-in-place function, since such functions may
7039 -- allocate their result directly in a target object, and cases where
7040 -- the result does get allocated in the secondary stack are checked for
7041 -- within the specialized Exp_Ch6 procedures for expanding those
7042 -- build-in-place calls.
7044 -- d) Calls to inlinable expression functions do not use the secondary
7045 -- stack (since the call will be replaced by its returned object).
7047 -- e) If the subprogram is marked Inline, then even if it returns
7048 -- an unconstrained type the call does not require use of the secondary
7049 -- stack. However, inlining will only take place if the body to inline
7050 -- is already present. It may not be available if e.g. the subprogram is
7051 -- declared in a child instance.
7053 -- f) If the subprogram is a static expression function and the call is
7054 -- a static call (the actuals are all static expressions), then we never
7055 -- want to create a transient scope (this could occur in the case of a
7056 -- static string-returning call).
7058 -- g) If the call is the expression of a simple return statement that
7059 -- returns on the same stack, since it will be handled as a tail call
7060 -- by Expand_Simple_Function_Return.
7062 if Expander_Active
7063 and then Ekind (Nam) in E_Function | E_Subprogram_Type
7064 and then Requires_Transient_Scope (Etype (Nam))
7065 and then not Is_Intrinsic_Subprogram (Nam)
7066 and then not Is_Ignored_Ghost_Entity (Nam)
7067 and then not Is_Build_In_Place_Function (Nam)
7068 and then not Is_Inlinable_Expression_Function (Nam)
7069 and then not (Is_Inlined (Nam)
7070 and then Has_Pragma_Inline (Nam)
7071 and then Nkind (Unit_Declaration_Node (Nam)) =
7072 N_Subprogram_Declaration
7073 and then
7074 Present (Body_To_Inline (Unit_Declaration_Node (Nam))))
7075 and then not Is_Static_Function_Call (N)
7076 and then not (Nkind (Parent (N)) = N_Simple_Return_Statement
7077 and then
7078 Needs_Secondary_Stack
7079 (Etype
7080 (Return_Applies_To
7081 (Return_Statement_Entity (Parent (N))))) =
7082 Needs_Secondary_Stack (Etype (Nam)))
7083 then
7084 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
7086 -- If the call appears within the bounds of a loop, it will be
7087 -- rewritten and reanalyzed, nothing left to do here.
7089 if Nkind (N) /= N_Function_Call then
7090 return;
7091 end if;
7092 end if;
7094 -- A protected function cannot be called within the definition of the
7095 -- enclosing protected type, unless it is part of a pre/postcondition
7096 -- on another protected operation. This may appear in the entry wrapper
7097 -- created for an entry with preconditions.
7099 if Is_Protected_Type (Scope (Nam))
7100 and then In_Open_Scopes (Scope (Nam))
7101 and then not Has_Completion (Scope (Nam))
7102 and then not In_Spec_Expression
7103 and then not Is_Entry_Wrapper (Current_Scope)
7104 then
7105 Error_Msg_NE
7106 ("& cannot be called before end of protected definition", N, Nam);
7107 end if;
7109 -- Propagate interpretation to actuals, and add default expressions
7110 -- where needed.
7112 if Present (First_Formal (Nam)) then
7113 Resolve_Actuals (N, Nam);
7115 -- Overloaded literals are rewritten as function calls, for purpose of
7116 -- resolution. After resolution, we can replace the call with the
7117 -- literal itself.
7119 elsif Ekind (Nam) = E_Enumeration_Literal then
7120 Copy_Node (Subp, N);
7121 Resolve_Entity_Name (N, Typ);
7123 -- Avoid validation, since it is a static function call
7125 Generate_Reference (Nam, Subp);
7126 return;
7127 end if;
7129 -- If the subprogram is not global, then kill all saved values and
7130 -- checks. This is a bit conservative, since in many cases we could do
7131 -- better, but it is not worth the effort. Similarly, we kill constant
7132 -- values. However we do not need to do this for internal entities
7133 -- (unless they are inherited user-defined subprograms), since they
7134 -- are not in the business of molesting local values.
7136 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
7137 -- kill all checks and values for calls to global subprograms. This
7138 -- takes care of the case where an access to a local subprogram is
7139 -- taken, and could be passed directly or indirectly and then called
7140 -- from almost any context.
7142 -- Note: we do not do this step till after resolving the actuals. That
7143 -- way we still take advantage of the current value information while
7144 -- scanning the actuals.
7146 -- We suppress killing values if we are processing the nodes associated
7147 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
7148 -- type kills all the values as part of analyzing the code that
7149 -- initializes the dispatch tables.
7151 if Inside_Freezing_Actions = 0
7152 and then (not Is_Library_Level_Entity (Nam)
7153 or else Suppress_Value_Tracking_On_Call
7154 (Nearest_Dynamic_Scope (Current_Scope)))
7155 and then (Comes_From_Source (Nam)
7156 or else (Present (Alias (Nam))
7157 and then Comes_From_Source (Alias (Nam))))
7158 then
7159 Kill_Current_Values;
7160 end if;
7162 -- If we are warning about unread OUT parameters, this is the place to
7163 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
7164 -- after the above call to Kill_Current_Values (since that call clears
7165 -- the Last_Assignment field of all local variables).
7167 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
7168 and then Comes_From_Source (N)
7169 and then In_Extended_Main_Source_Unit (N)
7170 then
7171 declare
7172 F : Entity_Id;
7173 A : Node_Id;
7175 begin
7176 F := First_Formal (Nam);
7177 A := First_Actual (N);
7178 while Present (F) and then Present (A) loop
7179 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
7180 and then Warn_On_Modified_As_Out_Parameter (F)
7181 and then Is_Entity_Name (A)
7182 and then Present (Entity (A))
7183 and then Comes_From_Source (N)
7184 and then Safe_To_Capture_Value (N, Entity (A))
7185 then
7186 Set_Last_Assignment (Entity (A), A);
7187 end if;
7189 Next_Formal (F);
7190 Next_Actual (A);
7191 end loop;
7192 end;
7193 end if;
7195 -- If the subprogram is a primitive operation, check whether or not
7196 -- it is a correct dispatching call.
7198 if Is_Overloadable (Nam) and then Is_Dispatching_Operation (Nam) then
7199 Check_Dispatching_Call (N);
7201 -- If the subprogram is an abstract operation, then flag an error
7203 elsif Is_Overloadable (Nam) and then Is_Abstract_Subprogram (Nam) then
7204 Nondispatching_Call_To_Abstract_Operation (N, Nam);
7205 end if;
7207 -- If this is a dispatching call, generate the appropriate reference,
7208 -- for better source navigation in GNAT Studio.
7210 if Is_Overloadable (Nam) and then Present (Controlling_Argument (N)) then
7211 Generate_Reference (Nam, Subp, 'R');
7213 -- Normal case, not a dispatching call: generate a call reference
7215 else
7216 Generate_Reference (Nam, Subp, 's');
7217 end if;
7219 if Is_Intrinsic_Subprogram (Nam) then
7220 Check_Intrinsic_Call (N);
7221 end if;
7223 -- Check for violation of restriction No_Specific_Termination_Handlers
7224 -- and warn on a potentially blocking call to Abort_Task.
7226 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7227 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7228 or else
7229 Is_RTE (Nam, RE_Specific_Handler))
7230 then
7231 Check_Restriction (No_Specific_Termination_Handlers, N);
7233 elsif Is_RTE (Nam, RE_Abort_Task) then
7234 Check_Potentially_Blocking_Operation (N);
7235 end if;
7237 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7238 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7239 -- need to check the second argument to determine whether it is an
7240 -- absolute or relative timing event.
7242 if Restriction_Check_Required (No_Relative_Delay)
7243 and then Is_RTE (Nam, RE_Set_Handler)
7244 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7245 then
7246 Check_Restriction (No_Relative_Delay, N);
7247 end if;
7249 -- Issue an error for a call to an eliminated subprogram. This routine
7250 -- will not perform the check if the call appears within a default
7251 -- expression.
7253 Check_For_Eliminated_Subprogram (Subp, Nam);
7255 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7256 -- class-wide and the call dispatches on result in a context that does
7257 -- not provide a tag, the call raises Program_Error.
7259 if Nkind (N) = N_Function_Call
7260 and then In_Instance
7261 and then Is_Generic_Actual_Type (Typ)
7262 and then Is_Class_Wide_Type (Typ)
7263 and then Has_Controlling_Result (Nam)
7264 and then Nkind (Parent (N)) = N_Object_Declaration
7265 then
7266 -- Verify that none of the formals are controlling
7268 declare
7269 Call_OK : Boolean := False;
7270 F : Entity_Id;
7272 begin
7273 F := First_Formal (Nam);
7274 while Present (F) loop
7275 if Is_Controlling_Formal (F) then
7276 Call_OK := True;
7277 exit;
7278 end if;
7280 Next_Formal (F);
7281 end loop;
7283 if not Call_OK then
7284 Error_Msg_Warn := SPARK_Mode /= On;
7285 Error_Msg_N ("!cannot determine tag of result<<", N);
7286 Error_Msg_N ("\Program_Error [<<!", N);
7287 Insert_Action (N,
7288 Make_Raise_Program_Error (Sloc (N),
7289 Reason => PE_Explicit_Raise));
7290 end if;
7291 end;
7292 end if;
7294 -- Check for calling a function with OUT or IN OUT parameter when the
7295 -- calling context (us right now) is not Ada 2012, so does not allow
7296 -- OUT or IN OUT parameters in function calls. Functions declared in
7297 -- a predefined unit are OK, as they may be called indirectly from a
7298 -- user-declared instantiation.
7300 if Ada_Version < Ada_2012
7301 and then Ekind (Nam) = E_Function
7302 and then Has_Out_Or_In_Out_Parameter (Nam)
7303 and then not In_Predefined_Unit (Nam)
7304 then
7305 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7306 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7307 end if;
7309 -- Check the dimensions of the actuals in the call. For function calls,
7310 -- propagate the dimensions from the returned type to N.
7312 Analyze_Dimension_Call (N, Nam);
7314 -- Check unreachable code after calls to procedures with No_Return
7316 if Ekind (Nam) = E_Procedure and then No_Return (Nam) then
7317 Check_Unreachable_Code (N);
7318 end if;
7320 -- All done, evaluate call and deal with elaboration issues
7322 Eval_Call (N);
7324 if Legacy_Elaboration_Checks then
7325 Check_Elab_Call (N);
7326 end if;
7328 -- Annotate the tree by creating a call marker in case the original call
7329 -- is transformed by expansion. The call marker is automatically saved
7330 -- for later examination by the ABE Processing phase.
7332 Build_Call_Marker (N);
7334 Mark_Use_Clauses (Subp);
7336 Warn_On_Overlapping_Actuals (Nam, N);
7338 -- Ada 2022 (AI12-0075): If the call is a static call to a static
7339 -- expression function, then we want to "inline" the call, replacing
7340 -- it with the folded static result. This is not done if the checking
7341 -- for a potentially static expression is enabled or if an error has
7342 -- been posted on the call (which may be due to the check for recursive
7343 -- calls, in which case we don't want to fall into infinite recursion
7344 -- when doing the inlining).
7346 if not Checking_Potentially_Static_Expression
7347 and then Is_Static_Function_Call (N)
7348 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7349 and then not Error_Posted (Ultimate_Alias (Nam))
7350 then
7351 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7353 -- In GNATprove mode, expansion is disabled, but we want to inline some
7354 -- subprograms to facilitate formal verification. Indirect calls through
7355 -- a subprogram type or within a generic cannot be inlined. Inlining is
7356 -- performed only for calls subject to SPARK_Mode => On.
7358 elsif GNATprove_Mode
7359 and then SPARK_Mode = On
7360 and then Is_Overloadable (Nam)
7361 and then not Inside_A_Generic
7362 then
7363 Nam_UA := Ultimate_Alias (Nam);
7364 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7366 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7367 Body_Id := Corresponding_Body (Nam_Decl);
7369 -- Nothing to do if the subprogram is not inlined (because it is
7370 -- recursive, directly or indirectly), or is not eligible for
7371 -- inlining GNATprove mode (because of properties of the
7372 -- subprogram itself), or inlining has been disabled with switch
7373 -- -gnatdm.
7375 if not Is_Inlined (Nam_UA)
7376 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7377 or else Debug_Flag_M
7378 then
7379 null;
7381 -- Calls cannot be inlined inside assertions, as GNATprove treats
7382 -- assertions as logic expressions. Only issue a message when the
7383 -- body has been seen, otherwise this leads to spurious messages
7384 -- on expression functions.
7386 elsif In_Assertion_Expr /= 0 then
7387 Cannot_Inline
7388 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7389 Suppress_Info => No (Body_Id));
7391 -- Calls cannot be inlined inside default expressions
7393 elsif In_Default_Expr then
7394 Cannot_Inline
7395 ("cannot inline & (in default expression)?", N, Nam_UA);
7397 -- Calls cannot be inlined inside potentially unevaluated
7398 -- expressions, as this would create complex actions inside
7399 -- expressions, that are not handled by GNATprove.
7401 elsif Is_Potentially_Unevaluated (N) then
7402 Cannot_Inline
7403 ("cannot inline & (in potentially unevaluated context)?",
7404 N, Nam_UA);
7406 -- Calls are not inlined inside the loop_parameter_specification
7407 -- or iterator_specification of the quantified expression, as they
7408 -- are only preanalyzed. Calls in the predicate part are handled
7409 -- by the previous test on potentially unevaluated expressions.
7411 elsif In_Quantified_Expression (N) then
7412 Cannot_Inline
7413 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7415 -- Inlining should not be performed during preanalysis
7417 elsif Full_Analysis then
7419 -- Do not inline calls inside expression functions or functions
7420 -- generated by the front end for subtype predicates, as this
7421 -- would prevent interpreting them as logical formulas in
7422 -- GNATprove. Only issue a message when the body has been seen,
7423 -- otherwise this leads to spurious messages on callees that
7424 -- are themselves expression functions.
7426 if Present (Current_Subprogram)
7427 and then
7428 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7429 or else Is_Predicate_Function (Current_Subprogram)
7430 or else Is_Invariant_Procedure (Current_Subprogram)
7431 or else Is_DIC_Procedure (Current_Subprogram))
7432 then
7433 declare
7434 Issue_Msg : constant Boolean :=
7435 Present (Body_Id)
7436 and then Present (Body_To_Inline (Nam_Decl));
7437 begin
7438 if Is_Predicate_Function (Current_Subprogram) then
7439 Cannot_Inline
7440 ("cannot inline & (inside predicate)?",
7441 N, Nam_UA, Suppress_Info => not Issue_Msg);
7443 elsif Is_Invariant_Procedure (Current_Subprogram) then
7444 Cannot_Inline
7445 ("cannot inline & (inside invariant)?",
7446 N, Nam_UA, Suppress_Info => not Issue_Msg);
7448 elsif Is_DIC_Procedure (Current_Subprogram) then
7449 Cannot_Inline
7450 ("cannot inline & (inside Default_Initial_Condition)?",
7451 N, Nam_UA, Suppress_Info => not Issue_Msg);
7453 else
7454 Cannot_Inline
7455 ("cannot inline & (inside expression function)?",
7456 N, Nam_UA, Suppress_Info => not Issue_Msg);
7457 end if;
7458 end;
7460 -- Cannot inline a call inside the definition of a record type,
7461 -- typically inside the constraints of the type. Calls in
7462 -- default expressions are also not inlined, but this is
7463 -- filtered out above when testing In_Default_Expr.
7465 elsif Is_Record_Type (Current_Scope) then
7466 Cannot_Inline
7467 ("cannot inline & (inside record type)?", N, Nam_UA);
7469 -- With the one-pass inlining technique, a call cannot be
7470 -- inlined if the corresponding body has not been seen yet.
7472 elsif No (Body_Id) then
7473 Cannot_Inline
7474 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7476 -- Nothing to do if there is no body to inline, indicating that
7477 -- the subprogram is not suitable for inlining in GNATprove
7478 -- mode.
7480 elsif No (Body_To_Inline (Nam_Decl)) then
7481 null;
7483 -- Calls cannot be inlined inside the conditions of while
7484 -- loops, as this would create complex actions inside
7485 -- the condition, that are not handled by GNATprove.
7487 elsif In_Statement_Condition_With_Actions (N) then
7488 Cannot_Inline
7489 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7491 -- Do not inline calls which would possibly lead to missing a
7492 -- type conversion check on an input parameter or a memory leak
7493 -- on an output parameter.
7495 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7496 Cannot_Inline
7497 ("cannot inline & (possible check on parameters)?",
7498 N, Nam_UA);
7500 -- Otherwise, inline the call, issuing an info message when
7501 -- -gnatd_f is set.
7503 else
7504 if Debug_Flag_Underscore_F then
7505 Error_Msg_NE
7506 ("info: analyzing call to & in context?", N, Nam_UA);
7507 end if;
7509 Expand_Inlined_Call (N, Nam_UA, Nam);
7510 end if;
7511 end if;
7512 end if;
7513 end if;
7514 end Resolve_Call;
7516 -----------------------------
7517 -- Resolve_Case_Expression --
7518 -----------------------------
7520 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7521 Alt : Node_Id;
7522 Alt_Expr : Node_Id;
7523 Alt_Typ : Entity_Id;
7524 Is_Dyn : Boolean;
7526 begin
7527 Alt := First (Alternatives (N));
7528 while Present (Alt) loop
7529 Alt_Expr := Expression (Alt);
7531 if Error_Posted (Alt_Expr) then
7532 return;
7533 end if;
7535 Resolve_Dependent_Expression (N, Alt_Expr, Typ);
7537 Check_Unset_Reference (Alt_Expr);
7538 Alt_Typ := Etype (Alt_Expr);
7540 -- When the expression is of a scalar subtype different from the
7541 -- result subtype, then insert a conversion to ensure the generation
7542 -- of a constraint check.
7544 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7545 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7546 Analyze_And_Resolve (Alt_Expr, Typ);
7548 elsif Is_Array_Type (Typ) then
7549 Apply_Length_Check (Alt_Expr, Typ);
7550 end if;
7552 Next (Alt);
7553 end loop;
7555 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7556 -- dynamically tagged must be known statically.
7558 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7559 Alt := First (Alternatives (N));
7560 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7562 while Present (Alt) loop
7563 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7564 Error_Msg_N
7565 ("all or none of the dependent expressions can be "
7566 & "dynamically tagged", N);
7567 end if;
7569 Next (Alt);
7570 end loop;
7571 end if;
7573 Set_Etype (N, Typ);
7574 Eval_Case_Expression (N);
7575 Analyze_Dimension (N);
7576 end Resolve_Case_Expression;
7578 -------------------------------
7579 -- Resolve_Character_Literal --
7580 -------------------------------
7582 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7583 B_Typ : constant Entity_Id := Base_Type (Typ);
7584 C : Entity_Id;
7586 begin
7587 -- Verify that the character does belong to the type of the context
7589 Set_Etype (N, B_Typ);
7590 Eval_Character_Literal (N);
7592 -- Wide_Wide_Character literals must always be defined, since the set
7593 -- of wide wide character literals is complete, i.e. if a character
7594 -- literal is accepted by the parser, then it is OK for wide wide
7595 -- character (out of range character literals are rejected).
7597 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7598 return;
7600 -- Always accept character literal for type Any_Character, which
7601 -- occurs in error situations and in comparisons of literals, both
7602 -- of which should accept all literals.
7604 elsif B_Typ = Any_Character then
7605 return;
7607 -- For Standard.Character or a type derived from it, check that the
7608 -- literal is in range.
7610 elsif Root_Type (B_Typ) = Standard_Character then
7611 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7612 return;
7613 end if;
7615 -- For Standard.Wide_Character or a type derived from it, check that the
7616 -- literal is in range.
7618 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7619 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7620 return;
7621 end if;
7623 -- If the entity is already set, this has already been resolved in a
7624 -- generic context, or comes from expansion. Nothing else to do.
7626 elsif Present (Entity (N)) then
7627 return;
7629 -- Otherwise we have a user defined character type, and we can use the
7630 -- standard visibility mechanisms to locate the referenced entity.
7632 else
7633 C := Current_Entity (N);
7634 while Present (C) loop
7635 if Etype (C) = B_Typ then
7636 Set_Entity_With_Checks (N, C);
7637 Generate_Reference (C, N);
7638 return;
7639 end if;
7641 C := Homonym (C);
7642 end loop;
7643 end if;
7645 -- If we fall through, then the literal does not match any of the
7646 -- entries of the enumeration type. This isn't just a constraint error
7647 -- situation, it is an illegality (see RM 4.2).
7649 Error_Msg_NE
7650 ("character not defined for }", N, First_Subtype (B_Typ));
7651 end Resolve_Character_Literal;
7653 ---------------------------
7654 -- Resolve_Comparison_Op --
7655 ---------------------------
7657 -- The operands must have compatible types and the boolean context does not
7658 -- participate in the resolution. The first pass verifies that the operands
7659 -- are not ambiguous and sets their type correctly, or to Any_Type in case
7660 -- of ambiguity. If both operands are strings or aggregates, then they are
7661 -- ambiguous even if they carry a single (universal) type.
7663 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7664 L : constant Node_Id := Left_Opnd (N);
7665 R : constant Node_Id := Right_Opnd (N);
7667 T : Entity_Id := Find_Unique_Type (L, R);
7669 begin
7670 if T = Any_Fixed then
7671 T := Unique_Fixed_Point_Type (L);
7672 end if;
7674 Set_Etype (N, Base_Type (Typ));
7675 Generate_Reference (T, N, ' ');
7677 if T = Any_Type then
7678 -- Deal with explicit ambiguity of operands
7680 if Is_Overloaded (L) or else Is_Overloaded (R) then
7681 Ambiguous_Operands (N);
7682 end if;
7684 return;
7685 end if;
7687 -- Deal with other error cases
7689 if T = Any_String or else
7690 T = Any_Composite or else
7691 T = Any_Character
7692 then
7693 if T = Any_Character then
7694 Ambiguous_Character (L);
7695 else
7696 Error_Msg_N ("ambiguous operands for comparison", N);
7697 end if;
7699 Set_Etype (N, Any_Type);
7700 return;
7701 end if;
7703 -- Resolve the operands if types OK
7705 Resolve (L, T);
7706 Resolve (R, T);
7707 Set_Compare_Type (N, T);
7708 Check_Unset_Reference (L);
7709 Check_Unset_Reference (R);
7710 Generate_Operator_Reference (N, T);
7711 Check_Low_Bound_Tested (N);
7713 -- Check comparison on unordered enumeration
7715 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7716 Error_Msg_Sloc := Sloc (Etype (L));
7717 Error_Msg_NE
7718 ("comparison on unordered enumeration type& declared#?.u?",
7719 N, Etype (L));
7720 end if;
7722 Analyze_Dimension (N);
7724 Eval_Relational_Op (N);
7725 end Resolve_Comparison_Op;
7727 --------------------------------
7728 -- Resolve_Declare_Expression --
7729 --------------------------------
7731 procedure Resolve_Declare_Expression
7732 (N : Node_Id;
7733 Typ : Entity_Id)
7735 Expr : constant Node_Id := Expression (N);
7737 Decl : Node_Id;
7738 Local : Entity_Id := Empty;
7740 function Replace_Local (N : Node_Id) return Traverse_Result;
7741 -- Use a tree traversal to replace each occurrence of the name of
7742 -- a local object declared in the construct, with the corresponding
7743 -- entity. This replaces the usual way to perform name capture by
7744 -- visibility, because it is not possible to place on the scope
7745 -- stack the fake scope created for the analysis of the local
7746 -- declarations; such a scope conflicts with the transient scopes
7747 -- that may be generated if the expression includes function calls
7748 -- requiring finalization.
7750 -------------------
7751 -- Replace_Local --
7752 -------------------
7754 function Replace_Local (N : Node_Id) return Traverse_Result is
7755 begin
7756 -- The identifier may be the prefix of a selected component,
7757 -- but not a selector name, because the local entities do not
7758 -- have a scope that can be named: a selected component whose
7759 -- selector is a homonym of a local entity must denote some
7760 -- global entity.
7762 if Nkind (N) = N_Identifier
7763 and then Chars (N) = Chars (Local)
7764 and then No (Entity (N))
7765 and then
7766 (Nkind (Parent (N)) /= N_Selected_Component
7767 or else N = Prefix (Parent (N)))
7768 then
7769 Set_Entity (N, Local);
7770 Set_Etype (N, Etype (Local));
7771 end if;
7773 return OK;
7774 end Replace_Local;
7776 procedure Replace_Local_Ref is new Traverse_Proc (Replace_Local);
7778 -- Start of processing for Resolve_Declare_Expression
7780 begin
7781 Decl := First (Actions (N));
7783 while Present (Decl) loop
7784 if Nkind (Decl) in
7785 N_Object_Declaration | N_Object_Renaming_Declaration
7786 and then Comes_From_Source (Defining_Identifier (Decl))
7787 then
7788 Local := Defining_Identifier (Decl);
7789 Replace_Local_Ref (Expr);
7791 -- Traverse the expression to replace references to local
7792 -- variables that occur within declarations of the
7793 -- declare_expression.
7795 declare
7796 D : Node_Id := Next (Decl);
7797 begin
7798 while Present (D) loop
7799 Replace_Local_Ref (D);
7800 Next (D);
7801 end loop;
7802 end;
7803 end if;
7805 Next (Decl);
7806 end loop;
7808 -- The end of the declarative list is a freeze point for the
7809 -- local declarations.
7811 if Present (Local) then
7812 Decl := Parent (Local);
7813 Freeze_All (First_Entity (Scope (Local)), Decl);
7814 end if;
7816 Resolve (Expr, Typ);
7817 Check_Unset_Reference (Expr);
7818 end Resolve_Declare_Expression;
7820 -----------------------------------
7821 -- Resolve_Dependent_Expression --
7822 -----------------------------------
7824 procedure Resolve_Dependent_Expression
7825 (N : Node_Id;
7826 Expr : Node_Id;
7827 Typ : Entity_Id)
7829 begin
7830 -- RM 4.5.7(8/3) says that the expected type of dependent expressions is
7831 -- that of the conditional expression but RM 4.5.7(10/3) forces the type
7832 -- of the conditional expression without changing the expected type (the
7833 -- expected type of the operand of a type conversion is any type), so we
7834 -- may have a gap between these two types that is bridged by the dynamic
7835 -- semantics specified by RM 4.5.7(20/3) with the associated legality
7836 -- rule RM 4.5.7(16/3) that will be automatically enforced.
7838 if Nkind (Parent (N)) = N_Type_Conversion
7839 and then Nkind (Expr) /= N_Raise_Expression
7840 then
7841 Convert_To_And_Rewrite (Typ, Expr);
7842 Analyze_And_Resolve (Expr);
7843 else
7844 Resolve (Expr, Typ);
7845 end if;
7846 end Resolve_Dependent_Expression;
7848 -----------------------------------------
7849 -- Resolve_Discrete_Subtype_Indication --
7850 -----------------------------------------
7852 procedure Resolve_Discrete_Subtype_Indication
7853 (N : Node_Id;
7854 Typ : Entity_Id)
7856 R : Node_Id;
7857 S : Entity_Id;
7859 begin
7860 Analyze (Subtype_Mark (N));
7861 S := Entity (Subtype_Mark (N));
7863 if Nkind (Constraint (N)) /= N_Range_Constraint then
7864 Error_Msg_N ("expect range constraint for discrete type", N);
7865 Set_Etype (N, Any_Type);
7867 else
7868 R := Range_Expression (Constraint (N));
7870 if R = Error then
7871 return;
7872 end if;
7874 Analyze (R);
7876 if Base_Type (S) /= Base_Type (Typ) then
7877 Error_Msg_NE
7878 ("expect subtype of }", N, First_Subtype (Typ));
7880 -- Rewrite the constraint as a range of Typ
7881 -- to allow compilation to proceed further.
7883 Set_Etype (N, Typ);
7884 Rewrite (Low_Bound (R),
7885 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7886 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7887 Attribute_Name => Name_First));
7888 Rewrite (High_Bound (R),
7889 Make_Attribute_Reference (Sloc (High_Bound (R)),
7890 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7891 Attribute_Name => Name_First));
7893 else
7894 Resolve (R, Typ);
7895 Set_Etype (N, Etype (R));
7897 -- Additionally, we must check that the bounds are compatible
7898 -- with the given subtype, which might be different from the
7899 -- type of the context.
7901 Apply_Range_Check (R, S);
7903 -- ??? If the above check statically detects a Constraint_Error
7904 -- it replaces the offending bound(s) of the range R with a
7905 -- Constraint_Error node. When the itype which uses these bounds
7906 -- is frozen the resulting call to Duplicate_Subexpr generates
7907 -- a new temporary for the bounds.
7909 -- Unfortunately there are other itypes that are also made depend
7910 -- on these bounds, so when Duplicate_Subexpr is called they get
7911 -- a forward reference to the newly created temporaries and Gigi
7912 -- aborts on such forward references. This is probably sign of a
7913 -- more fundamental problem somewhere else in either the order of
7914 -- itype freezing or the way certain itypes are constructed.
7916 -- To get around this problem we call Remove_Side_Effects right
7917 -- away if either bounds of R are a Constraint_Error.
7919 declare
7920 L : constant Node_Id := Low_Bound (R);
7921 H : constant Node_Id := High_Bound (R);
7923 begin
7924 if Nkind (L) = N_Raise_Constraint_Error then
7925 Remove_Side_Effects (L);
7926 end if;
7928 if Nkind (H) = N_Raise_Constraint_Error then
7929 Remove_Side_Effects (H);
7930 end if;
7931 end;
7933 Check_Unset_Reference (Low_Bound (R));
7934 Check_Unset_Reference (High_Bound (R));
7935 end if;
7936 end if;
7937 end Resolve_Discrete_Subtype_Indication;
7939 -------------------------
7940 -- Resolve_Entity_Name --
7941 -------------------------
7943 -- Used to resolve identifiers and expanded names
7945 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7946 function Is_Assignment_Or_Object_Expression
7947 (Context : Node_Id;
7948 Expr : Node_Id) return Boolean;
7949 -- Determine whether node Context denotes an assignment statement or an
7950 -- object declaration whose expression is node Expr.
7952 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7953 -- Determine whether Expr is part of an N_Attribute_Reference
7954 -- expression.
7956 ----------------------------------------
7957 -- Is_Assignment_Or_Object_Expression --
7958 ----------------------------------------
7960 function Is_Assignment_Or_Object_Expression
7961 (Context : Node_Id;
7962 Expr : Node_Id) return Boolean
7964 begin
7965 if Nkind (Context) in N_Assignment_Statement | N_Object_Declaration
7966 and then Expression (Context) = Expr
7967 then
7968 return True;
7970 -- Check whether a construct that yields a name is the expression of
7971 -- an assignment statement or an object declaration.
7973 elsif (Nkind (Context) in N_Attribute_Reference
7974 | N_Explicit_Dereference
7975 | N_Indexed_Component
7976 | N_Selected_Component
7977 | N_Slice
7978 and then Prefix (Context) = Expr)
7979 or else
7980 (Nkind (Context) in N_Type_Conversion
7981 | N_Unchecked_Type_Conversion
7982 and then Expression (Context) = Expr)
7983 then
7984 return
7985 Is_Assignment_Or_Object_Expression
7986 (Context => Parent (Context),
7987 Expr => Context);
7989 -- Otherwise the context is not an assignment statement or an object
7990 -- declaration.
7992 else
7993 return False;
7994 end if;
7995 end Is_Assignment_Or_Object_Expression;
7997 -----------------------------
7998 -- Is_Attribute_Expression --
7999 -----------------------------
8001 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
8002 N : Node_Id := Expr;
8003 begin
8004 while Present (N) loop
8005 if Nkind (N) = N_Attribute_Reference then
8006 return True;
8008 -- Prevent the search from going too far
8010 elsif Is_Body_Or_Package_Declaration (N) then
8011 return False;
8012 end if;
8014 N := Parent (N);
8015 end loop;
8017 return False;
8018 end Is_Attribute_Expression;
8020 -- Local variables
8022 E : constant Entity_Id := Entity (N);
8023 Par : Node_Id;
8025 -- Start of processing for Resolve_Entity_Name
8027 begin
8028 -- If garbage from errors, set to Any_Type and return
8030 if No (E) and then Total_Errors_Detected /= 0 then
8031 Set_Etype (N, Any_Type);
8032 return;
8033 end if;
8035 -- Replace named numbers by corresponding literals. Note that this is
8036 -- the one case where Resolve_Entity_Name must reset the Etype, since
8037 -- it is currently marked as universal.
8039 if Ekind (E) = E_Named_Integer then
8040 Set_Etype (N, Typ);
8041 Eval_Named_Integer (N);
8043 elsif Ekind (E) = E_Named_Real then
8044 Set_Etype (N, Typ);
8045 Eval_Named_Real (N);
8047 -- For enumeration literals, we need to make sure that a proper style
8048 -- check is done, since such literals are overloaded, and thus we did
8049 -- not do a style check during the first phase of analysis.
8051 elsif Ekind (E) = E_Enumeration_Literal then
8052 Set_Entity_With_Checks (N, E);
8053 Eval_Entity_Name (N);
8055 -- Case of (sub)type name appearing in a context where an expression
8056 -- is expected. This is legal if occurrence is a current instance.
8057 -- See RM 8.6 (17/3). It is also legal if the expression is
8058 -- part of a choice pattern for a case stmt/expr having a
8059 -- non-discrete selecting expression.
8061 elsif Is_Type (E) then
8062 if Is_Current_Instance (N) or else Is_Case_Choice_Pattern (N) then
8063 null;
8065 -- Any other use is an error
8067 else
8068 Error_Msg_N
8069 ("invalid use of subtype mark in expression or call", N);
8070 end if;
8072 -- Check discriminant use if entity is discriminant in current scope,
8073 -- i.e. discriminant of record or concurrent type currently being
8074 -- analyzed. Uses in corresponding body are unrestricted.
8076 elsif Ekind (E) = E_Discriminant
8077 and then Scope (E) = Current_Scope
8078 and then not Has_Completion (Current_Scope)
8079 then
8080 Check_Discriminant_Use (N);
8082 -- A parameterless generic function cannot appear in a context that
8083 -- requires resolution.
8085 elsif Ekind (E) = E_Generic_Function then
8086 Error_Msg_N ("illegal use of generic function", N);
8088 -- In Ada 83 an OUT parameter cannot be read, but attributes of
8089 -- array types (i.e. bounds and length) are legal.
8091 elsif Ekind (E) = E_Out_Parameter
8092 and then (Is_Scalar_Type (Etype (E))
8093 or else not Is_Attribute_Expression (Parent (N)))
8095 and then (Nkind (Parent (N)) in N_Op
8096 or else Nkind (Parent (N)) = N_Explicit_Dereference
8097 or else Is_Assignment_Or_Object_Expression
8098 (Context => Parent (N),
8099 Expr => N))
8100 then
8101 if Ada_Version = Ada_83 then
8102 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
8103 end if;
8105 -- In all other cases, just do the possible static evaluation
8107 else
8108 -- A deferred constant that appears in an expression must have a
8109 -- completion, unless it has been removed by in-place expansion of
8110 -- an aggregate. A constant that is a renaming does not need
8111 -- initialization.
8113 if Ekind (E) = E_Constant
8114 and then Comes_From_Source (E)
8115 and then No (Constant_Value (E))
8116 and then Is_Frozen (Etype (E))
8117 and then not In_Spec_Expression
8118 and then not Is_Imported (E)
8119 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
8120 then
8121 if No_Initialization (Parent (E))
8122 or else (Present (Full_View (E))
8123 and then No_Initialization (Parent (Full_View (E))))
8124 then
8125 null;
8126 else
8127 Error_Msg_N
8128 ("deferred constant is frozen before completion", N);
8129 end if;
8130 end if;
8132 Eval_Entity_Name (N);
8133 end if;
8135 Par := Parent (N);
8137 -- When the entity appears in a parameter association, retrieve the
8138 -- related subprogram call.
8140 if Nkind (Par) = N_Parameter_Association then
8141 Par := Parent (Par);
8142 end if;
8144 if Comes_From_Source (N) then
8146 -- The following checks are only relevant when SPARK_Mode is On as
8147 -- they are not standard Ada legality rules.
8149 if SPARK_Mode = On then
8151 -- Check for possible elaboration issues with respect to reads of
8152 -- variables. The act of renaming the variable is not considered a
8153 -- read as it simply establishes an alias.
8155 if Legacy_Elaboration_Checks
8156 and then Ekind (E) = E_Variable
8157 and then Dynamic_Elaboration_Checks
8158 and then Nkind (Par) /= N_Object_Renaming_Declaration
8159 then
8160 Check_Elab_Call (N);
8161 end if;
8162 end if;
8164 -- The variable may eventually become a constituent of a single
8165 -- protected/task type. Record the reference now and verify its
8166 -- legality when analyzing the contract of the variable
8167 -- (SPARK RM 9.3).
8169 if Ekind (E) = E_Variable then
8170 Record_Possible_Part_Of_Reference (E, N);
8171 end if;
8173 -- A Ghost entity must appear in a specific context
8175 if Is_Ghost_Entity (E) then
8176 Check_Ghost_Context (E, N);
8177 end if;
8179 -- We may be resolving an entity within expanded code, so a reference
8180 -- to an entity should be ignored when calculating effective use
8181 -- clauses to avoid inappropriate marking.
8183 Mark_Use_Clauses (E);
8184 end if;
8185 end Resolve_Entity_Name;
8187 -------------------
8188 -- Resolve_Entry --
8189 -------------------
8191 procedure Resolve_Entry (Entry_Name : Node_Id) is
8192 Loc : constant Source_Ptr := Sloc (Entry_Name);
8193 Nam : Entity_Id;
8194 New_N : Node_Id;
8195 S : Entity_Id;
8196 Tsk : Entity_Id;
8197 E_Name : Node_Id;
8198 Index : Node_Id;
8200 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
8201 -- If the bounds of the entry family being called depend on task
8202 -- discriminants, build a new index subtype where a discriminant is
8203 -- replaced with the value of the discriminant of the target task.
8204 -- The target task is the prefix of the entry name in the call.
8206 -----------------------
8207 -- Actual_Index_Type --
8208 -----------------------
8210 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
8211 Typ : constant Entity_Id := Entry_Index_Type (E);
8212 Tsk : constant Entity_Id := Scope (E);
8213 Lo : constant Node_Id := Type_Low_Bound (Typ);
8214 Hi : constant Node_Id := Type_High_Bound (Typ);
8215 New_T : Entity_Id;
8217 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8218 -- If the bound is given by a discriminant, replace with a reference
8219 -- to the discriminant of the same name in the target task. If the
8220 -- entry name is the target of a requeue statement and the entry is
8221 -- in the current protected object, the bound to be used is the
8222 -- discriminal of the object (see Apply_Range_Check for details of
8223 -- the transformation).
8225 -----------------------------
8226 -- Actual_Discriminant_Ref --
8227 -----------------------------
8229 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8230 Typ : constant Entity_Id := Etype (Bound);
8231 Ref : Node_Id;
8233 begin
8234 Remove_Side_Effects (Bound);
8236 if not Is_Entity_Name (Bound)
8237 or else Ekind (Entity (Bound)) /= E_Discriminant
8238 then
8239 return Bound;
8241 elsif Is_Protected_Type (Tsk)
8242 and then In_Open_Scopes (Tsk)
8243 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
8244 then
8245 -- Note: here Bound denotes a discriminant of the corresponding
8246 -- record type tskV, whose discriminal is a formal of the
8247 -- init-proc tskVIP. What we want is the body discriminal,
8248 -- which is associated to the discriminant of the original
8249 -- concurrent type tsk.
8251 return New_Occurrence_Of
8252 (Find_Body_Discriminal (Entity (Bound)), Loc);
8254 else
8255 Ref :=
8256 Make_Selected_Component (Loc,
8257 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8258 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8259 Analyze (Ref);
8260 Resolve (Ref, Typ);
8261 return Ref;
8262 end if;
8263 end Actual_Discriminant_Ref;
8265 -- Start of processing for Actual_Index_Type
8267 begin
8268 if not Has_Discriminants (Tsk)
8269 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8270 then
8271 return Entry_Index_Type (E);
8273 else
8274 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8275 Set_Etype (New_T, Base_Type (Typ));
8276 Set_Size_Info (New_T, Typ);
8277 Set_RM_Size (New_T, RM_Size (Typ));
8278 Set_Scalar_Range (New_T,
8279 Make_Range (Sloc (Entry_Name),
8280 Low_Bound => Actual_Discriminant_Ref (Lo),
8281 High_Bound => Actual_Discriminant_Ref (Hi)));
8283 return New_T;
8284 end if;
8285 end Actual_Index_Type;
8287 -- Start of processing for Resolve_Entry
8289 begin
8290 -- Find name of entry being called, and resolve prefix of name with its
8291 -- own type. The prefix can be overloaded, and the name and signature of
8292 -- the entry must be taken into account.
8294 if Nkind (Entry_Name) = N_Indexed_Component then
8296 -- Case of dealing with entry family within the current tasks
8298 E_Name := Prefix (Entry_Name);
8300 else
8301 E_Name := Entry_Name;
8302 end if;
8304 if Is_Entity_Name (E_Name) then
8306 -- Entry call to an entry (or entry family) in the current task. This
8307 -- is legal even though the task will deadlock. Rewrite as call to
8308 -- current task.
8310 -- This can also be a call to an entry in an enclosing task. If this
8311 -- is a single task, we have to retrieve its name, because the scope
8312 -- of the entry is the task type, not the object. If the enclosing
8313 -- task is a task type, the identity of the task is given by its own
8314 -- self variable.
8316 -- Finally this can be a requeue on an entry of the same task or
8317 -- protected object.
8319 S := Scope (Entity (E_Name));
8321 for J in reverse 0 .. Scope_Stack.Last loop
8322 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8323 and then not Comes_From_Source (S)
8324 then
8325 -- S is an enclosing task or protected object. The concurrent
8326 -- declaration has been converted into a type declaration, and
8327 -- the object itself has an object declaration that follows
8328 -- the type in the same declarative part.
8330 Tsk := Next_Entity (S);
8331 while Etype (Tsk) /= S loop
8332 Next_Entity (Tsk);
8333 end loop;
8335 S := Tsk;
8336 exit;
8338 elsif S = Scope_Stack.Table (J).Entity then
8340 -- Call to current task. Will be transformed into call to Self
8342 exit;
8344 end if;
8345 end loop;
8347 New_N :=
8348 Make_Selected_Component (Loc,
8349 Prefix => New_Occurrence_Of (S, Loc),
8350 Selector_Name =>
8351 New_Occurrence_Of (Entity (E_Name), Loc));
8352 Rewrite (E_Name, New_N);
8353 Analyze (E_Name);
8355 elsif Nkind (Entry_Name) = N_Selected_Component
8356 and then Is_Overloaded (Prefix (Entry_Name))
8357 then
8358 -- Use the entry name (which must be unique at this point) to find
8359 -- the prefix that returns the corresponding task/protected type.
8361 declare
8362 Pref : constant Node_Id := Prefix (Entry_Name);
8363 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8364 I : Interp_Index;
8365 It : Interp;
8367 begin
8368 Get_First_Interp (Pref, I, It);
8369 while Present (It.Typ) loop
8370 if Scope (Ent) = It.Typ then
8371 Set_Etype (Pref, It.Typ);
8372 exit;
8373 end if;
8375 Get_Next_Interp (I, It);
8376 end loop;
8377 end;
8378 end if;
8380 if Nkind (Entry_Name) = N_Selected_Component then
8381 Resolve (Prefix (Entry_Name));
8382 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8384 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8385 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8386 Resolve (Prefix (Prefix (Entry_Name)));
8387 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8389 -- We do not resolve the prefix because an Entry_Family has no type,
8390 -- although it has the semantics of an array since it can be indexed.
8391 -- In order to perform the associated range check, we would need to
8392 -- build an array type on the fly and set it on the prefix, but this
8393 -- would be wasteful since only the index type matters. Therefore we
8394 -- attach this index type directly, so that Actual_Index_Expression
8395 -- can pick it up later in order to generate the range check.
8397 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8399 Index := First (Expressions (Entry_Name));
8400 Resolve (Index, Entry_Index_Type (Nam));
8402 -- Generate a reference for the index when it denotes an entity
8404 if Is_Entity_Name (Index) then
8405 Generate_Reference (Entity (Index), Nam);
8406 end if;
8408 -- Up to this point the expression could have been the actual in a
8409 -- simple entry call, and be given by a named association.
8411 if Nkind (Index) = N_Parameter_Association then
8412 Error_Msg_N ("expect expression for entry index", Index);
8413 else
8414 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8415 end if;
8416 end if;
8417 end Resolve_Entry;
8419 ------------------------
8420 -- Resolve_Entry_Call --
8421 ------------------------
8423 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8424 Entry_Name : constant Node_Id := Name (N);
8425 Loc : constant Source_Ptr := Sloc (Entry_Name);
8427 Nam : Entity_Id;
8428 Norm_OK : Boolean;
8429 Obj : Node_Id;
8430 Was_Over : Boolean;
8432 begin
8433 -- We kill all checks here, because it does not seem worth the effort to
8434 -- do anything better, an entry call is a big operation.
8436 Kill_All_Checks;
8438 -- Processing of the name is similar for entry calls and protected
8439 -- operation calls. Once the entity is determined, we can complete
8440 -- the resolution of the actuals.
8442 -- The selector may be overloaded, in the case of a protected object
8443 -- with overloaded functions. The type of the context is used for
8444 -- resolution.
8446 if Nkind (Entry_Name) = N_Selected_Component
8447 and then Is_Overloaded (Selector_Name (Entry_Name))
8448 and then Typ /= Standard_Void_Type
8449 then
8450 declare
8451 I : Interp_Index;
8452 It : Interp;
8454 begin
8455 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8456 while Present (It.Typ) loop
8457 if Covers (Typ, It.Typ) then
8458 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8459 Set_Etype (Entry_Name, It.Typ);
8461 Generate_Reference (It.Typ, N, ' ');
8462 end if;
8464 Get_Next_Interp (I, It);
8465 end loop;
8466 end;
8467 end if;
8469 Resolve_Entry (Entry_Name);
8471 if Nkind (Entry_Name) = N_Selected_Component then
8473 -- Simple entry or protected operation call
8475 Nam := Entity (Selector_Name (Entry_Name));
8476 Obj := Prefix (Entry_Name);
8478 if Is_Subprogram (Nam) then
8479 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8480 end if;
8482 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8484 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8486 -- Call to member of entry family
8488 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8489 Obj := Prefix (Prefix (Entry_Name));
8490 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8491 end if;
8493 -- We cannot in general check the maximum depth of protected entry calls
8494 -- at compile time. But we can tell that any protected entry call at all
8495 -- violates a specified nesting depth of zero.
8497 if Is_Protected_Type (Scope (Nam)) then
8498 Check_Restriction (Max_Entry_Queue_Length, N);
8499 end if;
8501 -- Use context type to disambiguate a protected function that can be
8502 -- called without actuals and that returns an array type, and where the
8503 -- argument list may be an indexing of the returned value.
8505 if Ekind (Nam) = E_Function
8506 and then Needs_No_Actuals (Nam)
8507 and then Present (Parameter_Associations (N))
8508 and then
8509 ((Is_Array_Type (Etype (Nam))
8510 and then Covers (Typ, Component_Type (Etype (Nam))))
8512 or else (Is_Access_Type (Etype (Nam))
8513 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8514 and then
8515 Covers
8516 (Typ,
8517 Component_Type (Designated_Type (Etype (Nam))))))
8518 then
8519 declare
8520 Index_Node : Node_Id;
8522 begin
8523 Index_Node :=
8524 Make_Indexed_Component (Loc,
8525 Prefix =>
8526 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8527 Expressions => Parameter_Associations (N));
8529 -- Since we are correcting a node classification error made by the
8530 -- parser, we call Replace rather than Rewrite.
8532 Replace (N, Index_Node);
8533 Set_Etype (Prefix (N), Etype (Nam));
8534 Set_Etype (N, Typ);
8535 Resolve_Indexed_Component (N, Typ);
8536 return;
8537 end;
8538 end if;
8540 if Is_Entry (Nam)
8541 and then Present (Contract_Wrapper (Nam))
8542 and then Current_Scope /= Contract_Wrapper (Nam)
8543 and then Current_Scope /= Wrapped_Statements (Contract_Wrapper (Nam))
8544 then
8545 -- Note the entity being called before rewriting the call, so that
8546 -- it appears used at this point.
8548 Generate_Reference (Nam, Entry_Name, 'r');
8550 -- Rewrite as call to the precondition wrapper, adding the task
8551 -- object to the list of actuals. If the call is to a member of an
8552 -- entry family, include the index as well.
8554 declare
8555 New_Call : Node_Id;
8556 New_Actuals : List_Id;
8558 begin
8559 New_Actuals := New_List (Obj);
8561 if Nkind (Entry_Name) = N_Indexed_Component then
8562 Append_To (New_Actuals,
8563 New_Copy_Tree (First (Expressions (Entry_Name))));
8564 end if;
8566 Append_List (Parameter_Associations (N), New_Actuals);
8567 New_Call :=
8568 Make_Procedure_Call_Statement (Loc,
8569 Name =>
8570 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8571 Parameter_Associations => New_Actuals);
8572 Rewrite (N, New_Call);
8574 -- Preanalyze and resolve new call. Current procedure is called
8575 -- from Resolve_Call, after which expansion will take place.
8577 Preanalyze_And_Resolve (N);
8578 return;
8579 end;
8580 end if;
8582 -- The operation name may have been overloaded. Order the actuals
8583 -- according to the formals of the resolved entity, and set the return
8584 -- type to that of the operation.
8586 if Was_Over then
8587 Normalize_Actuals (N, Nam, False, Norm_OK);
8588 pragma Assert (Norm_OK);
8589 Set_Etype (N, Etype (Nam));
8591 -- Reset the Is_Overloaded flag, since resolution is now completed
8593 -- Simple entry call
8595 if Nkind (Entry_Name) = N_Selected_Component then
8596 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8598 -- Call to a member of an entry family
8600 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8601 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8602 end if;
8603 end if;
8605 Resolve_Actuals (N, Nam);
8606 Check_Internal_Protected_Use (N, Nam);
8608 -- Create a call reference to the entry
8610 Generate_Reference (Nam, Entry_Name, 's');
8612 if Is_Entry (Nam) then
8613 Check_Potentially_Blocking_Operation (N);
8614 end if;
8616 -- Verify that a procedure call cannot masquerade as an entry
8617 -- call where an entry call is expected.
8619 if Ekind (Nam) = E_Procedure then
8620 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8621 and then N = Entry_Call_Statement (Parent (N))
8622 then
8623 Error_Msg_N ("entry call required in select statement", N);
8625 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8626 and then N = Triggering_Statement (Parent (N))
8627 then
8628 Error_Msg_N ("triggering statement cannot be procedure call", N);
8630 elsif Ekind (Scope (Nam)) = E_Task_Type
8631 and then not In_Open_Scopes (Scope (Nam))
8632 then
8633 Error_Msg_N ("task has no entry with this name", Entry_Name);
8634 end if;
8635 end if;
8637 -- After resolution, entry calls and protected procedure calls are
8638 -- changed into entry calls, for expansion. The structure of the node
8639 -- does not change, so it can safely be done in place. Protected
8640 -- function calls must keep their structure because they are
8641 -- subexpressions.
8643 if Ekind (Nam) /= E_Function then
8645 -- A protected operation that is not a function may modify the
8646 -- corresponding object, and cannot apply to a constant. If this
8647 -- is an internal call, the prefix is the type itself.
8649 if Is_Protected_Type (Scope (Nam))
8650 and then not Is_Variable (Obj)
8651 and then (not Is_Entity_Name (Obj)
8652 or else not Is_Type (Entity (Obj)))
8653 then
8654 Error_Msg_N
8655 ("prefix of protected procedure or entry call must be variable",
8656 Entry_Name);
8657 end if;
8659 declare
8660 Entry_Call : Node_Id;
8662 begin
8663 Entry_Call :=
8664 Make_Entry_Call_Statement (Loc,
8665 Name => Entry_Name,
8666 Parameter_Associations => Parameter_Associations (N));
8668 -- Inherit relevant attributes from the original call
8670 Set_First_Named_Actual
8671 (Entry_Call, First_Named_Actual (N));
8673 Set_Is_Elaboration_Checks_OK_Node
8674 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8676 Set_Is_Elaboration_Warnings_OK_Node
8677 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8679 Set_Is_SPARK_Mode_On_Node
8680 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8682 Rewrite (N, Entry_Call);
8683 Set_Analyzed (N, True);
8684 end;
8686 -- Protected functions can return on the secondary stack, in which case
8687 -- we must trigger the transient scope mechanism.
8689 elsif Expander_Active
8690 and then Requires_Transient_Scope (Etype (Nam))
8691 then
8692 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
8693 end if;
8695 -- Now we know that this is not a call to a function that returns an
8696 -- array type; moreover, we know the name of the called entry. Detect
8697 -- overlapping actuals, just like for a subprogram call.
8699 Warn_On_Overlapping_Actuals (Nam, N);
8700 end Resolve_Entry_Call;
8702 -------------------------
8703 -- Resolve_Equality_Op --
8704 -------------------------
8706 -- The operands must have compatible types and the boolean context does not
8707 -- participate in the resolution. The first pass verifies that the operands
8708 -- are not ambiguous and sets their type correctly, or to Any_Type in case
8709 -- of ambiguity. If both operands are strings, aggregates, allocators, or
8710 -- null, they are ambiguous even if they carry a single (universal) type.
8712 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8713 L : constant Node_Id := Left_Opnd (N);
8714 R : constant Node_Id := Right_Opnd (N);
8716 Implicit_NE_For_User_Defined_Operator : constant Boolean :=
8717 Nkind (N) = N_Op_Ne
8718 and then Ekind (Entity (N)) = E_Function
8719 and then not Comes_From_Source (Entity (N))
8720 and then not
8721 Is_Intrinsic_Subprogram (Corresponding_Equality (Entity (N)));
8722 -- Whether this is a call to the implicit inequality operator created
8723 -- for a user-defined operator that is not an intrinsic subprogram, in
8724 -- which case we need to skip some processing.
8726 T : Entity_Id := Find_Unique_Type (L, R);
8728 procedure Check_Access_Attribute (N : Node_Id);
8729 -- For any object, '[Unchecked_]Access of such object can never be
8730 -- passed as an operand to the Universal_Access equality operators.
8731 -- This is so because the expected type for Obj'Access in a call to
8732 -- these operators, whose formals are of type Universal_Access, is
8733 -- Universal_Access, and Universal_Access does not have a designated
8734 -- type. For more details, see RM 3.10.2(2/2) and 6.4.1(3).
8736 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id);
8737 -- Check RM 4.5.2(9.6/2) on the given designated object types
8739 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id);
8740 -- Check RM 4.5.2(9.7/2) on the given designated subprogram types
8742 procedure Check_If_Expression (Cond : Node_Id);
8743 -- The resolution rule for if expressions requires that each such must
8744 -- have a unique type. This means that if several dependent expressions
8745 -- are of a non-null anonymous access type, and the context does not
8746 -- impose an expected type (as can be the case in an equality operation)
8747 -- the expression must be rejected.
8749 procedure Explain_Redundancy (N : Node_Id);
8750 -- Attempt to explain the nature of a redundant comparison with True. If
8751 -- the expression N is too complex, this routine issues a general error
8752 -- message.
8754 function Find_Unique_Access_Type return Entity_Id;
8755 -- In the case of allocators and access attributes, the context must
8756 -- provide an indication of the specific access type to be used. If
8757 -- one operand is of such a "generic" access type, check whether there
8758 -- is a specific visible access type that has the same designated type.
8759 -- This is semantically dubious, and of no interest to any real code,
8760 -- but c48008a makes it all worthwhile.
8762 function Suspicious_Prio_For_Equality return Boolean;
8763 -- Returns True iff the parent node is a and/or/xor operation that
8764 -- could be the cause of confused priorities. Note that if the not is
8765 -- in parens, then False is returned.
8767 ----------------------------
8768 -- Check_Access_Attribute --
8769 ----------------------------
8771 procedure Check_Access_Attribute (N : Node_Id) is
8772 begin
8773 if Nkind (N) = N_Attribute_Reference
8774 and then Attribute_Name (N) in Name_Access | Name_Unchecked_Access
8775 then
8776 Error_Msg_N
8777 ("access attribute cannot be used as actual for "
8778 & "universal_access equality", N);
8779 end if;
8780 end Check_Access_Attribute;
8782 -----------------------------------
8783 -- Check_Designated_Object_Types --
8784 -----------------------------------
8786 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id) is
8787 begin
8788 if (Is_Elementary_Type (T1) or else Is_Array_Type (T1))
8789 and then (Base_Type (T1) /= Base_Type (T2)
8790 or else not Subtypes_Statically_Match (T1, T2))
8791 then
8792 Error_Msg_N
8793 ("designated subtypes for universal_access equality "
8794 & "do not statically match (RM 4.5.2(9.6/2)", N);
8795 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8796 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8797 end if;
8798 end Check_Designated_Object_Types;
8800 ---------------------------------------
8801 -- Check_Designated_Subprogram_Types --
8802 ---------------------------------------
8804 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id) is
8805 begin
8806 if not Subtype_Conformant (T1, T2) then
8807 Error_Msg_N
8808 ("designated subtypes for universal_access equality "
8809 & "not subtype conformant (RM 4.5.2(9.7/2)", N);
8810 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8811 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8812 end if;
8813 end Check_Designated_Subprogram_Types;
8815 -------------------------
8816 -- Check_If_Expression --
8817 -------------------------
8819 procedure Check_If_Expression (Cond : Node_Id) is
8820 Then_Expr : Node_Id;
8821 Else_Expr : Node_Id;
8823 begin
8824 if Nkind (Cond) = N_If_Expression then
8825 Then_Expr := Next (First (Expressions (Cond)));
8826 Else_Expr := Next (Then_Expr);
8828 if Nkind (Then_Expr) /= N_Null
8829 and then Nkind (Else_Expr) /= N_Null
8830 then
8831 Error_Msg_N ("cannot determine type of if expression", Cond);
8832 end if;
8833 end if;
8834 end Check_If_Expression;
8836 ------------------------
8837 -- Explain_Redundancy --
8838 ------------------------
8840 procedure Explain_Redundancy (N : Node_Id) is
8841 Error : Name_Id;
8842 Val : Node_Id;
8843 Val_Id : Entity_Id;
8845 begin
8846 Val := N;
8848 -- Strip the operand down to an entity
8850 loop
8851 if Nkind (Val) = N_Selected_Component then
8852 Val := Selector_Name (Val);
8853 else
8854 exit;
8855 end if;
8856 end loop;
8858 -- The construct denotes an entity
8860 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8861 Val_Id := Entity (Val);
8863 -- Do not generate an error message when the comparison is done
8864 -- against the enumeration literal Standard.True.
8866 if Ekind (Val_Id) /= E_Enumeration_Literal then
8868 -- Build a customized error message
8870 Name_Len := 0;
8871 Add_Str_To_Name_Buffer ("?r?");
8873 if Ekind (Val_Id) = E_Component then
8874 Add_Str_To_Name_Buffer ("component ");
8876 elsif Ekind (Val_Id) = E_Constant then
8877 Add_Str_To_Name_Buffer ("constant ");
8879 elsif Ekind (Val_Id) = E_Discriminant then
8880 Add_Str_To_Name_Buffer ("discriminant ");
8882 elsif Is_Formal (Val_Id) then
8883 Add_Str_To_Name_Buffer ("parameter ");
8885 elsif Ekind (Val_Id) = E_Variable then
8886 Add_Str_To_Name_Buffer ("variable ");
8887 end if;
8889 Add_Str_To_Name_Buffer ("& is always True!");
8890 Error := Name_Find;
8892 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8893 end if;
8895 -- The construct is too complex to disect, issue a general message
8897 else
8898 Error_Msg_N ("?r?expression is always True!", Val);
8899 end if;
8900 end Explain_Redundancy;
8902 -----------------------------
8903 -- Find_Unique_Access_Type --
8904 -----------------------------
8906 function Find_Unique_Access_Type return Entity_Id is
8907 Acc : Entity_Id;
8908 E : Entity_Id;
8909 S : Entity_Id;
8911 begin
8912 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8913 then
8914 Acc := Designated_Type (Etype (R));
8916 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8917 then
8918 Acc := Designated_Type (Etype (L));
8919 else
8920 return Empty;
8921 end if;
8923 S := Current_Scope;
8924 while S /= Standard_Standard loop
8925 E := First_Entity (S);
8926 while Present (E) loop
8927 if Is_Type (E)
8928 and then Is_Access_Type (E)
8929 and then Ekind (E) /= E_Allocator_Type
8930 and then Designated_Type (E) = Base_Type (Acc)
8931 then
8932 return E;
8933 end if;
8935 Next_Entity (E);
8936 end loop;
8938 S := Scope (S);
8939 end loop;
8941 return Empty;
8942 end Find_Unique_Access_Type;
8944 ----------------------------------
8945 -- Suspicious_Prio_For_Equality --
8946 ----------------------------------
8948 function Suspicious_Prio_For_Equality return Boolean is
8949 Par : constant Node_Id := Parent (N);
8951 begin
8952 -- Check if parent node is one of and/or/xor, not parenthesized
8953 -- explicitly, and its own parent is not of this kind. Otherwise,
8954 -- it's a case of chained Boolean conditions which is likely well
8955 -- parenthesized.
8957 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8958 and then Paren_Count (N) = 0
8959 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8960 then
8961 declare
8962 Compar : Node_Id :=
8963 (if Left_Opnd (Par) = N then
8964 Right_Opnd (Par)
8965 else
8966 Left_Opnd (Par));
8967 begin
8968 -- Compar may have been rewritten, for example from (a /= b)
8969 -- into not (a = b). Use the Original_Node instead.
8971 Compar := Original_Node (Compar);
8973 -- If the other argument of the and/or/xor is also a
8974 -- comparison, or another and/or/xor then most likely
8975 -- the priorities are correctly set.
8977 return Nkind (Compar) not in N_Op_Boolean;
8978 end;
8980 else
8981 return False;
8982 end if;
8983 end Suspicious_Prio_For_Equality;
8985 -- Start of processing for Resolve_Equality_Op
8987 begin
8988 if T = Any_Fixed then
8989 T := Unique_Fixed_Point_Type (L);
8990 end if;
8992 Set_Etype (N, Base_Type (Typ));
8993 Generate_Reference (T, N, ' ');
8995 if T = Any_Type then
8996 -- Deal with explicit ambiguity of operands, unless this is a call
8997 -- to the implicit inequality operator created for a user-defined
8998 -- operator that is not an intrinsic subprogram, since the common
8999 -- resolution of operands done here does not apply to it.
9001 if not Implicit_NE_For_User_Defined_Operator
9002 and then (Is_Overloaded (L) or else Is_Overloaded (R))
9003 then
9004 Ambiguous_Operands (N);
9005 end if;
9007 else
9009 -- For Ada 2022, check for user-defined literals when the type has
9010 -- the appropriate aspect.
9012 if Has_Applicable_User_Defined_Literal (L, Etype (R)) then
9013 Resolve (L, Etype (R));
9014 Set_Etype (N, Standard_Boolean);
9015 end if;
9017 if Has_Applicable_User_Defined_Literal (R, Etype (L)) then
9018 Resolve (R, Etype (L));
9019 Set_Etype (N, Standard_Boolean);
9020 end if;
9022 -- Deal with other error cases
9024 if T = Any_String or else
9025 T = Any_Composite or else
9026 T = Any_Character
9027 then
9028 if T = Any_Character then
9029 Ambiguous_Character (L);
9030 else
9031 Error_Msg_N ("ambiguous operands for equality", N);
9032 end if;
9034 Set_Etype (N, Any_Type);
9035 return;
9037 elsif T = Universal_Access
9038 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
9039 then
9040 T := Find_Unique_Access_Type;
9042 if No (T) then
9043 Error_Msg_N ("ambiguous operands for equality", N);
9044 Set_Etype (N, Any_Type);
9045 return;
9046 end if;
9048 -- If expressions must have a single type, and if the context does
9049 -- not impose one the dependent expressions cannot be anonymous
9050 -- access types.
9052 -- Why no similar processing for case expressions???
9054 elsif Ada_Version >= Ada_2012
9055 and then Is_Anonymous_Access_Type (Etype (L))
9056 and then Is_Anonymous_Access_Type (Etype (R))
9057 then
9058 Check_If_Expression (L);
9059 Check_If_Expression (R);
9060 end if;
9062 -- RM 4.5.2(9.5/2): At least one of the operands of the equality
9063 -- operators for universal_access shall be of type universal_access,
9064 -- or both shall be of access-to-object types, or both shall be of
9065 -- access-to-subprogram types (RM 4.5.2(9.5/2)).
9067 if Is_Anonymous_Access_Type (T)
9068 and then Etype (L) /= Universal_Access
9069 and then Etype (R) /= Universal_Access
9070 then
9071 -- RM 4.5.2(9.6/2): When both are of access-to-object types, the
9072 -- designated types shall be the same or one shall cover the other
9073 -- and if the designated types are elementary or array types, then
9074 -- the designated subtypes shall statically match.
9076 if Is_Access_Object_Type (Etype (L))
9077 and then Is_Access_Object_Type (Etype (R))
9078 then
9079 Check_Designated_Object_Types
9080 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
9082 -- RM 4.5.2(9.7/2): When both are of access-to-subprogram types,
9083 -- the designated profiles shall be subtype conformant.
9085 elsif Is_Access_Subprogram_Type (Etype (L))
9086 and then Is_Access_Subprogram_Type (Etype (R))
9087 then
9088 Check_Designated_Subprogram_Types
9089 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
9090 end if;
9091 end if;
9093 -- Check another case of equality operators for universal_access
9095 if Is_Anonymous_Access_Type (T) and then Comes_From_Source (N) then
9096 Check_Access_Attribute (L);
9097 Check_Access_Attribute (R);
9098 end if;
9100 Resolve (L, T);
9101 Resolve (R, T);
9102 Set_Compare_Type (N, T);
9104 -- AI12-0413: user-defined primitive equality of an untagged record
9105 -- type hides the predefined equality operator, including within a
9106 -- generic, and if it is declared abstract, results in an illegal
9107 -- instance if the operator is used in the spec, or in the raising
9108 -- of Program_Error if used in the body of an instance.
9110 if Nkind (N) = N_Op_Eq
9111 and then In_Instance
9112 and then Ada_Version >= Ada_2012
9113 then
9114 declare
9115 U : constant Entity_Id := Underlying_Type (T);
9117 Eq : Entity_Id;
9119 begin
9120 if Present (U)
9121 and then Is_Record_Type (U)
9122 and then not Is_Tagged_Type (U)
9123 then
9124 Eq := Get_User_Defined_Equality (T);
9126 if Present (Eq) then
9127 if Is_Abstract_Subprogram (Eq) then
9128 Nondispatching_Call_To_Abstract_Operation (N, Eq);
9129 else
9130 Rewrite_Operator_As_Call (N, Eq);
9131 end if;
9133 return;
9134 end if;
9135 end if;
9136 end;
9137 end if;
9139 -- If the unique type is a class-wide type then it will be expanded
9140 -- into a dispatching call to the predefined primitive. Therefore we
9141 -- check here for potential violation of such restriction.
9143 if Is_Class_Wide_Type (T) then
9144 Check_Restriction (No_Dispatching_Calls, N);
9145 end if;
9147 -- Only warn for redundant equality comparison to True for objects
9148 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
9149 -- other expressions, it may be a matter of preference to write
9150 -- "Expr = True" or "Expr".
9152 if Warn_On_Redundant_Constructs
9153 and then Comes_From_Source (N)
9154 and then Comes_From_Source (R)
9155 and then Is_Entity_Name (R)
9156 and then Entity (R) = Standard_True
9157 and then
9158 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
9159 or else
9160 Nkind (L) in N_Op)
9161 then
9162 Error_Msg_N -- CODEFIX
9163 ("?r?comparison with True is redundant!", N);
9164 Explain_Redundancy (Original_Node (R));
9165 end if;
9167 -- Warn on a (in)equality between boolean values which is not
9168 -- parenthesized when the parent expression is one of and/or/xor, as
9169 -- this is interpreted as (a = b) op c where most likely a = (b op c)
9170 -- was intended. Do not generate a warning in generic instances, as
9171 -- the problematic expression may be implicitly parenthesized in
9172 -- the generic itself if one of the operators is a generic formal.
9173 -- Also do not generate a warning for generated equality, for
9174 -- example from rewritting a membership test.
9176 if Warn_On_Questionable_Missing_Parens
9177 and then not In_Instance
9178 and then Comes_From_Source (N)
9179 and then Is_Boolean_Type (T)
9180 and then Suspicious_Prio_For_Equality
9181 then
9182 Error_Msg_N ("?q?equality should be parenthesized here!", N);
9183 end if;
9185 Check_Unset_Reference (L);
9186 Check_Unset_Reference (R);
9187 Generate_Operator_Reference (N, T);
9188 Check_Low_Bound_Tested (N);
9190 -- Unless this is a call to the implicit inequality operator created
9191 -- for a user-defined operator that is not an intrinsic subprogram,
9192 -- try to fold the operation.
9194 if not Implicit_NE_For_User_Defined_Operator then
9195 Analyze_Dimension (N);
9196 Eval_Relational_Op (N);
9198 elsif Nkind (N) = N_Op_Ne
9199 and then Is_Abstract_Subprogram (Entity (N))
9200 then
9201 Nondispatching_Call_To_Abstract_Operation (N, Entity (N));
9202 end if;
9203 end if;
9204 end Resolve_Equality_Op;
9206 ----------------------------------
9207 -- Resolve_Explicit_Dereference --
9208 ----------------------------------
9210 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
9211 Loc : constant Source_Ptr := Sloc (N);
9212 New_N : Node_Id;
9213 P : constant Node_Id := Prefix (N);
9215 P_Typ : Entity_Id;
9216 -- The candidate prefix type, if overloaded
9218 I : Interp_Index;
9219 It : Interp;
9221 begin
9222 Check_Fully_Declared_Prefix (Typ, P);
9223 P_Typ := Empty;
9225 -- A useful optimization: check whether the dereference denotes an
9226 -- element of a container, and if so rewrite it as a call to the
9227 -- corresponding Element function.
9229 -- Disabled for now, on advice of ARG. A more restricted form of the
9230 -- predicate might be acceptable ???
9232 -- if Is_Container_Element (N) then
9233 -- return;
9234 -- end if;
9236 if Is_Overloaded (P) then
9238 -- Use the context type to select the prefix that has the correct
9239 -- designated type. Keep the first match, which will be the inner-
9240 -- most.
9242 Get_First_Interp (P, I, It);
9244 while Present (It.Typ) loop
9245 if Is_Access_Type (It.Typ)
9246 and then Covers (Typ, Designated_Type (It.Typ))
9247 then
9248 if No (P_Typ) then
9249 P_Typ := It.Typ;
9250 end if;
9252 -- Remove access types that do not match, but preserve access
9253 -- to subprogram interpretations, in case a further dereference
9254 -- is needed (see below).
9256 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
9257 Remove_Interp (I);
9258 end if;
9260 Get_Next_Interp (I, It);
9261 end loop;
9263 if Present (P_Typ) then
9264 Resolve (P, P_Typ);
9265 Set_Etype (N, Designated_Type (P_Typ));
9267 else
9268 -- If no interpretation covers the designated type of the prefix,
9269 -- this is the pathological case where not all implementations of
9270 -- the prefix allow the interpretation of the node as a call. Now
9271 -- that the expected type is known, Remove other interpretations
9272 -- from prefix, rewrite it as a call, and resolve again, so that
9273 -- the proper call node is generated.
9275 Get_First_Interp (P, I, It);
9276 while Present (It.Typ) loop
9277 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
9278 Remove_Interp (I);
9279 end if;
9281 Get_Next_Interp (I, It);
9282 end loop;
9284 New_N :=
9285 Make_Function_Call (Loc,
9286 Name =>
9287 Make_Explicit_Dereference (Loc,
9288 Prefix => P),
9289 Parameter_Associations => New_List);
9291 Save_Interps (N, New_N);
9292 Rewrite (N, New_N);
9293 Analyze_And_Resolve (N, Typ);
9294 return;
9295 end if;
9297 -- If not overloaded, resolve P with its own type
9299 else
9300 Resolve (P);
9301 end if;
9303 -- If the prefix might be null, add an access check
9305 if Is_Access_Type (Etype (P))
9306 and then not Can_Never_Be_Null (Etype (P))
9307 then
9308 Apply_Access_Check (N);
9309 end if;
9311 -- If the designated type is a packed unconstrained array type, and the
9312 -- explicit dereference is not in the context of an attribute reference,
9313 -- then we must compute and set the actual subtype, since it is needed
9314 -- by Gigi. The reason we exclude the attribute case is that this is
9315 -- handled fine by Gigi, and in fact we use such attributes to build the
9316 -- actual subtype. We also exclude generated code (which builds actual
9317 -- subtypes directly if they are needed).
9319 if Is_Packed_Array (Etype (N))
9320 and then not Is_Constrained (Etype (N))
9321 and then Nkind (Parent (N)) /= N_Attribute_Reference
9322 and then Comes_From_Source (N)
9323 then
9324 Set_Etype (N, Get_Actual_Subtype (N));
9325 end if;
9327 Analyze_Dimension (N);
9329 -- Note: No Eval processing is required for an explicit dereference,
9330 -- because such a name can never be static.
9332 end Resolve_Explicit_Dereference;
9334 -------------------------------------
9335 -- Resolve_Expression_With_Actions --
9336 -------------------------------------
9338 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9340 function OK_For_Static (Act : Node_Id) return Boolean;
9341 -- True if Act is an action of a declare_expression that is allowed in a
9342 -- static declare_expression.
9344 function All_OK_For_Static return Boolean;
9345 -- True if all actions of N are allowed in a static declare_expression.
9347 function Get_Literal (Expr : Node_Id) return Node_Id;
9348 -- Expr is an expression with compile-time-known value. This returns the
9349 -- literal node that reprsents that value.
9351 -------------------
9352 -- OK_For_Static --
9353 -------------------
9355 function OK_For_Static (Act : Node_Id) return Boolean is
9356 begin
9357 case Nkind (Act) is
9358 when N_Object_Declaration =>
9359 if Constant_Present (Act)
9360 and then Is_Static_Expression (Expression (Act))
9361 then
9362 return True;
9363 end if;
9365 when N_Object_Renaming_Declaration =>
9366 if Statically_Names_Object (Name (Act)) then
9367 return True;
9368 end if;
9370 when others =>
9371 -- No other declarations, nor even pragmas, are allowed in a
9372 -- declare expression, so if we see something else, it must be
9373 -- an internally generated expression_with_actions.
9374 null;
9375 end case;
9377 return False;
9378 end OK_For_Static;
9380 -----------------------
9381 -- All_OK_For_Static --
9382 -----------------------
9384 function All_OK_For_Static return Boolean is
9385 Act : Node_Id := First (Actions (N));
9386 begin
9387 while Present (Act) loop
9388 if not OK_For_Static (Act) then
9389 return False;
9390 end if;
9392 Next (Act);
9393 end loop;
9395 return True;
9396 end All_OK_For_Static;
9398 -----------------
9399 -- Get_Literal --
9400 -----------------
9402 function Get_Literal (Expr : Node_Id) return Node_Id is
9403 pragma Assert (Compile_Time_Known_Value (Expr));
9404 Result : Node_Id;
9405 begin
9406 case Nkind (Expr) is
9407 when N_Has_Entity =>
9408 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9409 Result := Expr;
9410 else
9411 Result := Constant_Value (Entity (Expr));
9412 end if;
9413 when N_Numeric_Or_String_Literal =>
9414 Result := Expr;
9415 when others =>
9416 raise Program_Error;
9417 end case;
9419 pragma Assert
9420 (Nkind (Result) in N_Numeric_Or_String_Literal
9421 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9422 return Result;
9423 end Get_Literal;
9425 -- Local variables
9427 Loc : constant Source_Ptr := Sloc (N);
9429 -- Start of processing for Resolve_Expression_With_Actions
9431 begin
9432 Set_Etype (N, Typ);
9434 if Is_Empty_List (Actions (N)) then
9435 pragma Assert (All_OK_For_Static); null;
9436 end if;
9438 -- If the value of the expression is known at compile time, and all
9439 -- of the actions (if any) are suitable, then replace the declare
9440 -- expression with its expression. This allows the declare expression
9441 -- as a whole to be static if appropriate. See AI12-0368.
9443 if Compile_Time_Known_Value (Expression (N)) then
9444 if Is_Empty_List (Actions (N)) then
9445 Rewrite (N, Expression (N));
9446 elsif All_OK_For_Static then
9447 Rewrite
9448 (N, New_Copy_Tree
9449 (Get_Literal (Expression (N)), New_Sloc => Loc));
9450 end if;
9451 end if;
9452 end Resolve_Expression_With_Actions;
9454 ----------------------------------
9455 -- Resolve_External_Initializer --
9456 ----------------------------------
9458 procedure Resolve_External_Initializer (N : Node_Id; Typ : Entity_Id) is
9459 begin
9460 Set_String_Literal_Subtype (N, Typ);
9461 end Resolve_External_Initializer;
9463 ----------------------------------
9464 -- Resolve_Generalized_Indexing --
9465 ----------------------------------
9467 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9468 Indexing : constant Node_Id := Generalized_Indexing (N);
9469 begin
9470 Rewrite (N, Indexing);
9471 Resolve (N, Typ);
9472 end Resolve_Generalized_Indexing;
9474 ---------------------------
9475 -- Resolve_If_Expression --
9476 ---------------------------
9478 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9479 Condition : constant Node_Id := First (Expressions (N));
9481 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id);
9482 -- When a dependent expression is of a subtype different from
9483 -- the context subtype, then insert a qualification to ensure
9484 -- the generation of a constraint check. This was previously
9485 -- for scalar types. For array types apply a length check, given
9486 -- that the context in general allows sliding, while a qualified
9487 -- expression forces equality of bounds.
9489 -----------------
9490 -- Apply_Check --
9491 -----------------
9493 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id) is
9494 Expr_Typ : constant Entity_Id := Etype (Expr);
9495 Loc : constant Source_Ptr := Sloc (Expr);
9497 begin
9498 if Expr_Typ = Typ
9499 or else Is_Tagged_Type (Typ)
9500 or else Is_Access_Type (Typ)
9501 or else not Is_Constrained (Typ)
9502 or else Inside_A_Generic
9503 then
9504 null;
9506 elsif Is_Array_Type (Typ) then
9507 Apply_Length_Check (Expr, Typ);
9509 else
9510 Rewrite (Expr,
9511 Make_Qualified_Expression (Loc,
9512 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9513 Expression => Relocate_Node (Expr)));
9515 Analyze_And_Resolve (Expr, Result_Type);
9516 end if;
9517 end Apply_Check;
9519 -- Local variables
9521 Else_Expr : Node_Id;
9522 Then_Expr : Node_Id;
9524 Result_Type : Entity_Id;
9525 -- So in most cases the type of the if_expression and of its
9526 -- dependent expressions is that of the context. However, if
9527 -- the expression is the index of an Indexed_Component, we must
9528 -- ensure that a proper index check is applied, rather than a
9529 -- range check on the index type (which might be discriminant
9530 -- dependent). In this case we resolve with the base type of the
9531 -- index type, and the index check is generated in the resolution
9532 -- of the indexed_component above.
9534 -- Start of processing for Resolve_If_Expression
9536 begin
9537 -- Defend against malformed expressions
9539 if No (Condition) then
9540 return;
9541 end if;
9543 if Present (Parent (N))
9544 and then (Nkind (Parent (N)) = N_Indexed_Component
9545 or else Nkind (Parent (Parent (N))) = N_Indexed_Component)
9546 then
9547 Result_Type := Base_Type (Typ);
9549 else
9550 Result_Type := Typ;
9551 end if;
9553 Then_Expr := Next (Condition);
9555 if No (Then_Expr) then
9556 return;
9557 end if;
9559 Resolve (Condition, Any_Boolean);
9560 Check_Unset_Reference (Condition);
9562 Resolve_Dependent_Expression (N, Then_Expr, Result_Type);
9564 Check_Unset_Reference (Then_Expr);
9565 Apply_Check (Then_Expr, Result_Type);
9567 Else_Expr := Next (Then_Expr);
9569 -- If ELSE expression present, just resolve using the determined type
9571 if Present (Else_Expr) then
9572 Resolve_Dependent_Expression (N, Else_Expr, Result_Type);
9574 Check_Unset_Reference (Else_Expr);
9575 Apply_Check (Else_Expr, Result_Type);
9577 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9578 -- dynamically tagged must be known statically.
9580 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9581 if Is_Dynamically_Tagged (Then_Expr) /=
9582 Is_Dynamically_Tagged (Else_Expr)
9583 then
9584 Error_Msg_N ("all or none of the dependent expressions "
9585 & "can be dynamically tagged", N);
9586 end if;
9587 end if;
9589 -- If no ELSE expression is present, root type must be Standard.Boolean
9590 -- and we provide a Standard.True result converted to the appropriate
9591 -- Boolean type (in case it is a derived boolean type).
9593 elsif Root_Type (Typ) = Standard_Boolean then
9594 Else_Expr :=
9595 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9596 Analyze_And_Resolve (Else_Expr, Result_Type);
9597 Append_To (Expressions (N), Else_Expr);
9599 else
9600 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9601 Append_To (Expressions (N), Error);
9602 end if;
9604 Set_Etype (N, Result_Type);
9606 if not Error_Posted (N) then
9607 Eval_If_Expression (N);
9608 end if;
9610 Analyze_Dimension (N);
9611 end Resolve_If_Expression;
9613 ----------------------------------
9614 -- Resolve_Implicit_Dereference --
9615 ----------------------------------
9617 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9618 Desig_Typ : Entity_Id;
9620 begin
9621 if Is_Access_Type (Etype (P)) then
9622 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9623 Insert_Explicit_Dereference (P);
9624 Analyze_And_Resolve (P, Desig_Typ);
9625 end if;
9626 end Resolve_Implicit_Dereference;
9628 -------------------------------
9629 -- Resolve_Indexed_Component --
9630 -------------------------------
9632 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9633 Pref : constant Node_Id := Prefix (N);
9634 Expr : Node_Id;
9635 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9636 Index : Node_Id;
9638 begin
9639 if Present (Generalized_Indexing (N)) then
9640 Resolve_Generalized_Indexing (N, Typ);
9641 return;
9642 end if;
9644 if Is_Overloaded (Pref) then
9646 -- Use the context type to select the prefix that yields the correct
9647 -- component type.
9649 declare
9650 I : Interp_Index;
9651 It : Interp;
9652 I1 : Interp_Index := 0;
9653 Found : Boolean := False;
9655 begin
9656 Get_First_Interp (Pref, I, It);
9657 while Present (It.Typ) loop
9658 if (Is_Array_Type (It.Typ)
9659 and then Covers (Typ, Component_Type (It.Typ)))
9660 or else (Is_Access_Type (It.Typ)
9661 and then Is_Array_Type (Designated_Type (It.Typ))
9662 and then
9663 Covers
9664 (Typ,
9665 Component_Type (Designated_Type (It.Typ))))
9666 then
9667 if Found then
9668 It := Disambiguate (Pref, I1, I, Any_Type);
9670 if It = No_Interp then
9671 Error_Msg_N ("ambiguous prefix for indexing", N);
9672 Set_Etype (N, Typ);
9673 return;
9675 else
9676 Found := True;
9677 Array_Type := It.Typ;
9678 I1 := I;
9679 end if;
9681 else
9682 Found := True;
9683 Array_Type := It.Typ;
9684 I1 := I;
9685 end if;
9686 end if;
9688 Get_Next_Interp (I, It);
9689 end loop;
9690 end;
9692 else
9693 Array_Type := Etype (Pref);
9694 end if;
9696 Resolve (Pref, Array_Type);
9697 Array_Type := Get_Actual_Subtype_If_Available (Pref);
9699 -- If the prefix's type is an access type, get to the real array type.
9700 -- Note: we do not apply an access check because an explicit dereference
9701 -- will be introduced later, and the check will happen there.
9703 if Is_Access_Type (Array_Type) then
9704 Array_Type := Implicitly_Designated_Type (Array_Type);
9705 end if;
9707 -- If name was overloaded, set component type correctly now.
9708 -- If a misplaced call to an entry family (which has no index types)
9709 -- return. Error will be diagnosed from calling context.
9711 if Is_Array_Type (Array_Type) then
9712 Set_Etype (N, Component_Type (Array_Type));
9713 else
9714 return;
9715 end if;
9717 Index := First_Index (Array_Type);
9718 Expr := First (Expressions (N));
9720 -- The prefix may have resolved to a string literal, in which case its
9721 -- etype has a special representation. This is only possible currently
9722 -- if the prefix is a static concatenation, written in functional
9723 -- notation.
9725 if Ekind (Array_Type) = E_String_Literal_Subtype then
9726 Resolve (Expr, Standard_Positive);
9728 else
9729 while Present (Index) and then Present (Expr) loop
9730 Resolve (Expr, Etype (Index));
9731 Check_Unset_Reference (Expr);
9733 Apply_Scalar_Range_Check (Expr, Etype (Index));
9735 Next_Index (Index);
9736 Next (Expr);
9737 end loop;
9738 end if;
9740 Resolve_Implicit_Dereference (Pref);
9741 Analyze_Dimension (N);
9743 -- Do not generate the warning on suspicious index if we are analyzing
9744 -- package Ada.Tags; otherwise we will report the warning with the
9745 -- Prims_Ptr field of the dispatch table.
9747 if Scope (Etype (Pref)) = Standard_Standard
9748 or else not
9749 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Pref))), Ada_Tags)
9750 then
9751 Warn_On_Suspicious_Index (Pref, First (Expressions (N)));
9752 Eval_Indexed_Component (N);
9753 end if;
9755 -- If the array type is atomic and the component is not, then this is
9756 -- worth a warning before Ada 2022, since we have a situation where the
9757 -- access to the component may cause extra read/writes of the atomic
9758 -- object, or partial word accesses, both of which may be unexpected.
9760 if Nkind (N) = N_Indexed_Component
9761 and then Is_Atomic_Non_VFA_Ref_With_Address (N)
9762 and then not (Has_Atomic_Components (Array_Type)
9763 or else (Is_Entity_Name (Pref)
9764 and then Has_Atomic_Components
9765 (Entity (Pref))))
9766 and then not Is_Atomic (Component_Type (Array_Type))
9767 and then Ada_Version < Ada_2022
9768 then
9769 Error_Msg_N
9770 ("??access to non-atomic component of atomic array", Pref);
9771 Error_Msg_N
9772 ("??\may cause unexpected accesses to atomic object", Pref);
9773 end if;
9774 end Resolve_Indexed_Component;
9776 -----------------------------
9777 -- Resolve_Integer_Literal --
9778 -----------------------------
9780 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9781 begin
9782 Set_Etype (N, Typ);
9783 Eval_Integer_Literal (N);
9784 end Resolve_Integer_Literal;
9786 -----------------------------------------
9787 -- Resolve_Interpolated_String_Literal --
9788 -----------------------------------------
9790 procedure Resolve_Interpolated_String_Literal (N : Node_Id; Typ : Entity_Id)
9792 Str_Elem : Node_Id;
9794 begin
9795 Str_Elem := First (Expressions (N));
9796 pragma Assert (Nkind (Str_Elem) = N_String_Literal);
9798 while Present (Str_Elem) loop
9800 -- Resolve string elements using the context type; for interpolated
9801 -- expressions there is no need to check if their type has a suitable
9802 -- image function because under Ada 2022 all the types have such
9803 -- function available.
9805 if Nkind (Str_Elem) = N_String_Literal
9806 and then Is_Interpolated_String_Literal (Str_Elem)
9807 then
9808 Resolve (Str_Elem, Typ);
9810 -- Must have been rejected during analysis
9812 elsif Nkind (Str_Elem) in N_Character_Literal
9813 | N_Integer_Literal
9814 | N_Real_Literal
9815 | N_String_Literal
9816 then
9817 pragma Assert (Error_Posted (Str_Elem));
9818 end if;
9820 Next (Str_Elem);
9821 end loop;
9823 Set_Etype (N, Typ);
9824 end Resolve_Interpolated_String_Literal;
9826 --------------------------------
9827 -- Resolve_Intrinsic_Operator --
9828 --------------------------------
9830 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9831 Is_Stoele_Mod : constant Boolean :=
9832 Nkind (N) = N_Op_Mod
9833 and then Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
9834 and then Is_RTE (Etype (Left_Opnd (N)), RE_Address);
9835 -- True if this is the special mod operator of System.Storage_Elements,
9836 -- which needs to be resolved to the type of the left operand in order
9837 -- to implement the correct semantics.
9839 Btyp : constant Entity_Id :=
9840 (if Is_Stoele_Mod
9841 then Implementation_Base_Type (Etype (Left_Opnd (N)))
9842 else Implementation_Base_Type (Typ));
9843 -- The base type to be used for the operator
9845 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9846 -- If the operand is a literal, it cannot be the expression in a
9847 -- conversion. Use a qualified expression instead.
9849 ---------------------
9850 -- Convert_Operand --
9851 ---------------------
9853 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9854 Loc : constant Source_Ptr := Sloc (Opnd);
9855 Res : Node_Id;
9857 begin
9858 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9859 Res :=
9860 Make_Qualified_Expression (Loc,
9861 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9862 Expression => Relocate_Node (Opnd));
9863 Analyze (Res);
9865 else
9866 Res := Unchecked_Convert_To (Btyp, Opnd);
9867 end if;
9869 return Res;
9870 end Convert_Operand;
9872 -- Local variables
9874 Arg1 : Node_Id;
9875 Arg2 : Node_Id;
9876 Op : Entity_Id;
9878 -- Start of processing for Resolve_Intrinsic_Operator
9880 begin
9881 -- We must preserve the original entity in a generic setting, so that
9882 -- the legality of the operation can be verified in an instance.
9884 if not Expander_Active then
9885 return;
9886 end if;
9888 case N_Binary_Op'(Nkind (N)) is
9889 when N_Op_Add =>
9890 Op := Standard_Op_Add;
9891 when N_Op_Expon =>
9892 Op := Standard_Op_Expon;
9893 when N_Op_Subtract =>
9894 Op := Standard_Op_Subtract;
9895 when N_Op_Divide =>
9896 Op := Standard_Op_Divide;
9897 when N_Op_Mod =>
9898 Op := Standard_Op_Mod;
9899 when N_Op_Multiply =>
9900 Op := Standard_Op_Multiply;
9901 when N_Op_Rem =>
9902 Op := Standard_Op_Rem;
9904 -- Non-arithmetic operators are handled elsewhere
9906 when N_Op_Boolean
9907 | N_Op_Concat
9908 | N_Op_Shift
9910 raise Program_Error;
9911 end case;
9913 Set_Entity (N, Op);
9914 Set_Is_Overloaded (N, False);
9916 -- If the result or operand types are private, rewrite with unchecked
9917 -- conversions on the operands and the result, to expose the proper
9918 -- underlying numeric type. Likewise for the special mod operator of
9919 -- System.Storage_Elements, to expose the modified base type.
9921 if Is_Private_Type (Typ)
9922 or else Is_Private_Type (Etype (Left_Opnd (N)))
9923 or else Is_Private_Type (Etype (Right_Opnd (N)))
9924 or else Is_Stoele_Mod
9925 then
9926 Arg1 := Convert_Operand (Left_Opnd (N));
9928 if Nkind (N) = N_Op_Expon then
9929 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9930 else
9931 Arg2 := Convert_Operand (Right_Opnd (N));
9932 end if;
9934 if Nkind (Arg1) = N_Type_Conversion then
9935 Save_Interps (Left_Opnd (N), Expression (Arg1));
9936 end if;
9938 if Nkind (Arg2) = N_Type_Conversion then
9939 Save_Interps (Right_Opnd (N), Expression (Arg2));
9940 end if;
9942 Set_Left_Opnd (N, Arg1);
9943 Set_Right_Opnd (N, Arg2);
9945 Set_Etype (N, Btyp);
9946 Rewrite (N, Unchecked_Convert_To (Typ, N));
9947 Resolve (N, Typ);
9949 elsif Typ /= Etype (Left_Opnd (N))
9950 or else Typ /= Etype (Right_Opnd (N))
9951 then
9952 -- Add explicit conversion where needed, and save interpretations in
9953 -- case operands are overloaded.
9955 Arg1 := Convert_To (Typ, Left_Opnd (N));
9956 Arg2 := Convert_To (Typ, Right_Opnd (N));
9958 if Nkind (Arg1) = N_Type_Conversion then
9959 Save_Interps (Left_Opnd (N), Expression (Arg1));
9960 else
9961 Save_Interps (Left_Opnd (N), Arg1);
9962 end if;
9964 if Nkind (Arg2) = N_Type_Conversion then
9965 Save_Interps (Right_Opnd (N), Expression (Arg2));
9966 else
9967 Save_Interps (Right_Opnd (N), Arg2);
9968 end if;
9970 Rewrite (Left_Opnd (N), Arg1);
9971 Rewrite (Right_Opnd (N), Arg2);
9972 Analyze (Arg1);
9973 Analyze (Arg2);
9974 Resolve_Arithmetic_Op (N, Typ);
9976 else
9977 Resolve_Arithmetic_Op (N, Typ);
9978 end if;
9979 end Resolve_Intrinsic_Operator;
9981 --------------------------------------
9982 -- Resolve_Intrinsic_Unary_Operator --
9983 --------------------------------------
9985 procedure Resolve_Intrinsic_Unary_Operator
9986 (N : Node_Id;
9987 Typ : Entity_Id)
9989 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9990 Op : Entity_Id;
9991 Arg2 : Node_Id;
9993 begin
9994 -- We must preserve the original entity in a generic setting, so that
9995 -- the legality of the operation can be verified in an instance.
9997 if not Expander_Active then
9998 return;
9999 end if;
10001 case N_Unary_Op'(Nkind (N)) is
10002 when N_Op_Abs =>
10003 Op := Standard_Op_Abs;
10004 when N_Op_Minus =>
10005 Op := Standard_Op_Minus;
10006 when N_Op_Plus =>
10007 Op := Standard_Op_Plus;
10009 -- Non-arithmetic operators are handled elsewhere
10011 when N_Op_Not =>
10012 raise Program_Error;
10013 end case;
10015 Set_Entity (N, Op);
10017 if Is_Private_Type (Typ) then
10018 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
10019 Save_Interps (Right_Opnd (N), Expression (Arg2));
10021 Set_Right_Opnd (N, Arg2);
10023 Set_Etype (N, Btyp);
10024 Rewrite (N, Unchecked_Convert_To (Typ, N));
10025 Resolve (N, Typ);
10027 else
10028 Resolve_Unary_Op (N, Typ);
10029 end if;
10030 end Resolve_Intrinsic_Unary_Operator;
10032 ------------------------
10033 -- Resolve_Logical_Op --
10034 ------------------------
10036 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
10037 B_Typ : Entity_Id;
10039 begin
10040 Check_No_Direct_Boolean_Operators (N);
10042 -- Predefined operations on scalar types yield the base type. On the
10043 -- other hand, logical operations on arrays yield the type of the
10044 -- arguments (and the context).
10046 if Is_Array_Type (Typ) then
10047 B_Typ := Typ;
10048 else
10049 B_Typ := Base_Type (Typ);
10050 end if;
10052 -- The following test is required because the operands of the operation
10053 -- may be literals, in which case the resulting type appears to be
10054 -- compatible with a signed integer type, when in fact it is compatible
10055 -- only with modular types. If the context itself is universal, the
10056 -- operation is illegal.
10058 if not Valid_Boolean_Arg (Typ) then
10059 Error_Msg_N ("invalid context for logical operation", N);
10060 Set_Etype (N, Any_Type);
10061 return;
10063 elsif Typ = Any_Modular then
10064 Error_Msg_N
10065 ("no modular type available in this context", N);
10066 Set_Etype (N, Any_Type);
10067 return;
10069 elsif Is_Modular_Integer_Type (Typ)
10070 and then Etype (Left_Opnd (N)) = Universal_Integer
10071 and then Etype (Right_Opnd (N)) = Universal_Integer
10072 then
10073 Check_For_Visible_Operator (N, B_Typ);
10074 end if;
10076 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
10077 -- is active and the result type is standard Boolean (do not mess with
10078 -- ops that return a nonstandard Boolean type, because something strange
10079 -- is going on).
10081 -- Note: you might expect this replacement to be done during expansion,
10082 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
10083 -- is used, no part of the right operand of an "and" or "or" operator
10084 -- should be executed if the left operand would short-circuit the
10085 -- evaluation of the corresponding "and then" or "or else". If we left
10086 -- the replacement to expansion time, then run-time checks associated
10087 -- with such operands would be evaluated unconditionally, due to being
10088 -- before the condition prior to the rewriting as short-circuit forms
10089 -- during expansion.
10091 if Short_Circuit_And_Or
10092 and then B_Typ = Standard_Boolean
10093 and then Nkind (N) in N_Op_And | N_Op_Or
10094 then
10095 -- Mark the corresponding putative SCO operator as truly a logical
10096 -- (and short-circuit) operator.
10098 if Generate_SCO and then Comes_From_Source (N) then
10099 Set_SCO_Logical_Operator (N);
10100 end if;
10102 if Nkind (N) = N_Op_And then
10103 Rewrite (N,
10104 Make_And_Then (Sloc (N),
10105 Left_Opnd => Relocate_Node (Left_Opnd (N)),
10106 Right_Opnd => Relocate_Node (Right_Opnd (N))));
10107 Analyze_And_Resolve (N, B_Typ);
10109 -- Case of OR changed to OR ELSE
10111 else
10112 Rewrite (N,
10113 Make_Or_Else (Sloc (N),
10114 Left_Opnd => Relocate_Node (Left_Opnd (N)),
10115 Right_Opnd => Relocate_Node (Right_Opnd (N))));
10116 Analyze_And_Resolve (N, B_Typ);
10117 end if;
10119 -- Return now, since analysis of the rewritten ops will take care of
10120 -- other reference bookkeeping and expression folding.
10122 return;
10123 end if;
10125 Resolve (Left_Opnd (N), B_Typ);
10126 Resolve (Right_Opnd (N), B_Typ);
10128 Check_Unset_Reference (Left_Opnd (N));
10129 Check_Unset_Reference (Right_Opnd (N));
10131 Set_Etype (N, B_Typ);
10132 Generate_Operator_Reference (N, B_Typ);
10133 Eval_Logical_Op (N);
10134 end Resolve_Logical_Op;
10136 ---------------------------------
10137 -- Resolve_Membership_Equality --
10138 ---------------------------------
10140 procedure Resolve_Membership_Equality (N : Node_Id; Typ : Entity_Id) is
10141 Utyp : constant Entity_Id := Underlying_Type (Typ);
10143 begin
10144 -- RM 4.5.2(4.1/3): if the type is limited, then it shall have a visible
10145 -- primitive equality operator. This means that we can use the regular
10146 -- visibility-based resolution and reset Entity in order to trigger it.
10148 if Is_Limited_Type (Typ) then
10149 Set_Entity (N, Empty);
10151 -- RM 4.5.2(28.1/3): if the type is a record, then the membership test
10152 -- uses the primitive equality for the type [even if it is not visible].
10153 -- We only deal with the untagged case here, because the tagged case is
10154 -- handled uniformly in the expander.
10156 elsif Is_Record_Type (Utyp) and then not Is_Tagged_Type (Utyp) then
10157 declare
10158 Eq_Id : constant Entity_Id := Get_User_Defined_Equality (Typ);
10160 begin
10161 if Present (Eq_Id) then
10162 Rewrite_Operator_As_Call (N, Eq_Id);
10163 end if;
10164 end;
10165 end if;
10166 end Resolve_Membership_Equality;
10168 ---------------------------
10169 -- Resolve_Membership_Op --
10170 ---------------------------
10172 -- The context can only be a boolean type, and does not determine the
10173 -- arguments. Arguments should be unambiguous, but the preference rule for
10174 -- universal types applies.
10176 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
10177 pragma Assert (Is_Boolean_Type (Typ));
10179 L : constant Node_Id := Left_Opnd (N);
10180 R : constant Node_Id := Right_Opnd (N);
10181 T : Entity_Id;
10183 procedure Resolve_Set_Membership;
10184 -- Analysis has determined a unique type for the left operand. Use it as
10185 -- the basis to resolve the disjuncts.
10187 ----------------------------
10188 -- Resolve_Set_Membership --
10189 ----------------------------
10191 procedure Resolve_Set_Membership is
10192 Alt : Node_Id;
10194 begin
10195 -- If the left operand is overloaded, find type compatible with not
10196 -- overloaded alternative of the right operand.
10198 Alt := First (Alternatives (N));
10199 if Is_Overloaded (L) then
10200 T := Empty;
10201 while Present (Alt) loop
10202 if not Is_Overloaded (Alt) then
10203 T := Intersect_Types (L, Alt);
10204 exit;
10205 else
10206 Next (Alt);
10207 end if;
10208 end loop;
10210 -- Unclear how to resolve expression if all alternatives are also
10211 -- overloaded.
10213 if No (T) then
10214 Error_Msg_N ("ambiguous expression", N);
10215 end if;
10217 else
10218 T := Intersect_Types (L, Alt);
10219 end if;
10221 Resolve (L, T);
10223 Alt := First (Alternatives (N));
10224 while Present (Alt) loop
10226 -- Alternative is an expression, a range
10227 -- or a subtype mark.
10229 if not Is_Entity_Name (Alt)
10230 or else not Is_Type (Entity (Alt))
10231 then
10232 Resolve (Alt, T);
10233 end if;
10235 Next (Alt);
10236 end loop;
10238 -- Check for duplicates for discrete case
10240 if Is_Discrete_Type (T) then
10241 declare
10242 type Ent is record
10243 Alt : Node_Id;
10244 Val : Uint;
10245 end record;
10247 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
10248 Nalts : Nat;
10250 begin
10251 -- Loop checking duplicates. This is quadratic, but giant sets
10252 -- are unlikely in this context so it's a reasonable choice.
10254 Nalts := 0;
10255 Alt := First (Alternatives (N));
10256 while Present (Alt) loop
10257 if Is_OK_Static_Expression (Alt)
10258 and then Nkind (Alt) in N_Integer_Literal
10259 | N_Character_Literal
10260 | N_Has_Entity
10261 then
10262 Nalts := Nalts + 1;
10263 Alts (Nalts) := (Alt, Expr_Value (Alt));
10265 for J in 1 .. Nalts - 1 loop
10266 if Alts (J).Val = Alts (Nalts).Val then
10267 Error_Msg_Sloc := Sloc (Alts (J).Alt);
10268 Error_Msg_N ("duplicate of value given#??", Alt);
10269 end if;
10270 end loop;
10271 end if;
10273 Next (Alt);
10274 end loop;
10275 end;
10276 end if;
10278 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
10279 -- limited types, evaluation of a membership test uses the predefined
10280 -- equality for the type. This may be confusing to users, and the
10281 -- following warning appears useful for the most common case.
10283 if Is_Scalar_Type (Etype (L))
10284 and then Present (Get_User_Defined_Equality (Etype (L)))
10285 then
10286 Error_Msg_NE
10287 ("membership test on& uses predefined equality?", N, Etype (L));
10288 Error_Msg_N
10289 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
10290 end if;
10291 end Resolve_Set_Membership;
10293 -- Start of processing for Resolve_Membership_Op
10295 begin
10296 if L = Error or else R = Error then
10297 return;
10298 end if;
10300 if Present (Alternatives (N)) then
10301 Resolve_Set_Membership;
10302 goto SM_Exit;
10304 elsif not Is_Overloaded (R)
10305 and then Is_Universal_Numeric_Type (Etype (R))
10306 and then Is_Overloaded (L)
10307 then
10308 T := Etype (R);
10310 -- If the left operand is of a universal numeric type and the right
10311 -- operand is not, we do not resolve the operands to the tested type
10312 -- but to the universal type instead. If not conforming to the letter,
10313 -- it's conforming to the spirit of the specification of membership
10314 -- tests, which are typically used to guard a specific operation and
10315 -- ought not to fail a check in doing so. Without this, in the case of
10317 -- type Small_Length is range 1 .. 16;
10319 -- function Is_Small_String (S : String) return Boolean is
10320 -- begin
10321 -- return S'Length in Small_Length;
10322 -- end;
10324 -- the function Is_Small_String would fail a range check for strings
10325 -- larger than 127 characters.
10327 -- The test on the size is required in GNAT because universal_integer
10328 -- does not cover all the values of all the supported integer types,
10329 -- for example the large values of Long_Long_Long_Unsigned.
10331 elsif not Is_Overloaded (L)
10332 and then Is_Universal_Numeric_Type (Etype (L))
10333 and then (Is_Overloaded (R)
10334 or else
10335 (not Is_Universal_Numeric_Type (Etype (R))
10336 and then
10337 (not Is_Integer_Type (Etype (R))
10338 or else
10339 RM_Size (Etype (R)) < RM_Size (Universal_Integer))))
10340 then
10341 T := Etype (L);
10343 -- If the right operand is 'Range, we first need to resolve it (to
10344 -- the tested type) so that it is rewritten as an N_Range, before
10345 -- converting its bounds and resolving it again below.
10347 if Nkind (R) = N_Attribute_Reference
10348 and then Attribute_Name (R) = Name_Range
10349 then
10350 Resolve (R);
10351 end if;
10353 -- If the right operand is an N_Range, we convert its bounds to the
10354 -- universal type before resolving it.
10356 if Nkind (R) = N_Range then
10357 Rewrite (R,
10358 Make_Range (Sloc (R),
10359 Low_Bound => Convert_To (T, Low_Bound (R)),
10360 High_Bound => Convert_To (T, High_Bound (R))));
10361 Analyze (R);
10362 end if;
10364 -- Ada 2005 (AI-251): Support the following case:
10366 -- type I is interface;
10367 -- type T is tagged ...
10369 -- function Test (O : I'Class) is
10370 -- begin
10371 -- return O in T'Class.
10372 -- end Test;
10374 -- In this case we have nothing else to do. The membership test will be
10375 -- done at run time.
10377 elsif Ada_Version >= Ada_2005
10378 and then Is_Class_Wide_Type (Etype (L))
10379 and then Is_Interface (Etype (L))
10380 and then not Is_Interface (Etype (R))
10381 then
10382 return;
10384 else
10385 T := Intersect_Types (L, R);
10386 end if;
10388 -- If mixed-mode operations are present and operands are all literal,
10389 -- the only interpretation involves Duration, which is probably not
10390 -- the intention of the programmer.
10392 if T = Any_Fixed then
10393 T := Unique_Fixed_Point_Type (N);
10395 if T = Any_Type then
10396 return;
10397 end if;
10398 end if;
10400 Resolve (L, T);
10401 Check_Unset_Reference (L);
10403 if Nkind (R) = N_Range
10404 and then not Is_Scalar_Type (T)
10405 then
10406 Error_Msg_N ("scalar type required for range", R);
10407 end if;
10409 if Is_Entity_Name (R) then
10410 Freeze_Expression (R);
10411 else
10412 Resolve (R, T);
10413 Check_Unset_Reference (R);
10414 end if;
10416 -- Here after resolving membership operation
10418 <<SM_Exit>>
10420 Eval_Membership_Op (N);
10421 end Resolve_Membership_Op;
10423 ------------------
10424 -- Resolve_Null --
10425 ------------------
10427 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
10428 Loc : constant Source_Ptr := Sloc (N);
10430 begin
10431 -- Handle restriction against anonymous null access values This
10432 -- restriction can be turned off using -gnatdj.
10434 -- Ada 2005 (AI-231): Remove restriction
10436 if Ada_Version < Ada_2005
10437 and then not Debug_Flag_J
10438 and then Ekind (Typ) = E_Anonymous_Access_Type
10439 and then Comes_From_Source (N)
10440 then
10441 -- In the common case of a call which uses an explicitly null value
10442 -- for an access parameter, give specialized error message.
10444 if Nkind (Parent (N)) in N_Subprogram_Call then
10445 Error_Msg_N
10446 ("NULL is not allowed as argument for an access parameter", N);
10448 -- Standard message for all other cases (are there any?)
10450 else
10451 Error_Msg_N
10452 ("NULL cannot be of an anonymous access type", N);
10453 end if;
10454 end if;
10456 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
10457 -- assignment to a null-excluding object.
10459 if Ada_Version >= Ada_2005
10460 and then Can_Never_Be_Null (Typ)
10461 and then Nkind (Parent (N)) = N_Assignment_Statement
10462 then
10463 if Inside_Init_Proc then
10465 -- Decide whether to generate an if_statement around our
10466 -- null-excluding check to avoid them on certain internal object
10467 -- declarations by looking at the type the current Init_Proc
10468 -- belongs to.
10470 -- Generate:
10471 -- if T1b_skip_null_excluding_check then
10472 -- [constraint_error "access check failed"]
10473 -- end if;
10475 if Needs_Conditional_Null_Excluding_Check
10476 (Etype (First_Formal (Enclosing_Init_Proc)))
10477 then
10478 Insert_Action (N,
10479 Make_If_Statement (Loc,
10480 Condition =>
10481 Make_Identifier (Loc,
10482 New_External_Name
10483 (Chars (Typ), "_skip_null_excluding_check")),
10484 Then_Statements =>
10485 New_List (
10486 Make_Raise_Constraint_Error (Loc,
10487 Reason => CE_Access_Check_Failed))));
10489 -- Otherwise, simply create the check
10491 else
10492 Insert_Action (N,
10493 Make_Raise_Constraint_Error (Loc,
10494 Reason => CE_Access_Check_Failed));
10495 end if;
10496 else
10497 Insert_Action
10498 (Compile_Time_Constraint_Error (N,
10499 "(Ada 2005) NULL not allowed in null-excluding objects??"),
10500 Make_Raise_Constraint_Error (Loc,
10501 Reason => CE_Access_Check_Failed));
10502 end if;
10503 end if;
10505 -- In a distributed context, null for a remote access to subprogram may
10506 -- need to be replaced with a special record aggregate. In this case,
10507 -- return after having done the transformation.
10509 if (Ekind (Typ) = E_Record_Type
10510 or else Is_Remote_Access_To_Subprogram_Type (Typ))
10511 and then Remote_AST_Null_Value (N, Typ)
10512 then
10513 return;
10514 end if;
10516 -- The null literal takes its type from the context
10518 Set_Etype (N, Typ);
10519 end Resolve_Null;
10521 -----------------------
10522 -- Resolve_Op_Concat --
10523 -----------------------
10525 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
10527 -- We wish to avoid deep recursion, because concatenations are often
10528 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
10529 -- operands nonrecursively until we find something that is not a simple
10530 -- concatenation (A in this case). We resolve that, and then walk back
10531 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10532 -- to do the rest of the work at each level. The Parent pointers allow
10533 -- us to avoid recursion, and thus avoid running out of memory. See also
10534 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10536 NN : Node_Id := N;
10537 Op1 : Node_Id;
10539 begin
10540 -- The following code is equivalent to:
10542 -- Resolve_Op_Concat_First (NN, Typ);
10543 -- Resolve_Op_Concat_Arg (N, ...);
10544 -- Resolve_Op_Concat_Rest (N, Typ);
10546 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10547 -- operand is a concatenation.
10549 -- Walk down left operands
10551 loop
10552 Resolve_Op_Concat_First (NN, Typ);
10553 Op1 := Left_Opnd (NN);
10554 exit when not (Nkind (Op1) = N_Op_Concat
10555 and then not Is_Array_Type (Component_Type (Typ))
10556 and then Entity (Op1) = Entity (NN));
10557 NN := Op1;
10558 end loop;
10560 -- Now (given the above example) NN is A&B and Op1 is A
10562 -- First resolve Op1 ...
10564 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10566 -- ... then walk NN back up until we reach N (where we started), calling
10567 -- Resolve_Op_Concat_Rest along the way.
10569 loop
10570 Resolve_Op_Concat_Rest (NN, Typ);
10571 exit when NN = N;
10572 NN := Parent (NN);
10573 end loop;
10574 end Resolve_Op_Concat;
10576 ---------------------------
10577 -- Resolve_Op_Concat_Arg --
10578 ---------------------------
10580 procedure Resolve_Op_Concat_Arg
10581 (N : Node_Id;
10582 Arg : Node_Id;
10583 Typ : Entity_Id;
10584 Is_Comp : Boolean)
10586 Btyp : constant Entity_Id := Base_Type (Typ);
10587 Ctyp : constant Entity_Id := Component_Type (Typ);
10589 begin
10590 if In_Instance then
10591 if Is_Comp
10592 or else (not Is_Overloaded (Arg)
10593 and then Etype (Arg) /= Any_Composite
10594 and then Covers (Ctyp, Etype (Arg)))
10595 then
10596 Resolve (Arg, Ctyp);
10597 else
10598 Resolve (Arg, Btyp);
10599 end if;
10601 -- If both Array & Array and Array & Component are visible, there is a
10602 -- potential ambiguity that must be reported.
10604 elsif Has_Compatible_Type (Arg, Ctyp) then
10605 if Nkind (Arg) = N_Aggregate
10606 and then Is_Composite_Type (Ctyp)
10607 then
10608 if Is_Private_Type (Ctyp) then
10609 Resolve (Arg, Btyp);
10611 -- If the operation is user-defined and not overloaded use its
10612 -- profile. The operation may be a renaming, in which case it has
10613 -- been rewritten, and we want the original profile.
10615 elsif not Is_Overloaded (N)
10616 and then Comes_From_Source (Entity (Original_Node (N)))
10617 and then Ekind (Entity (Original_Node (N))) = E_Function
10618 then
10619 Resolve (Arg,
10620 Etype
10621 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10622 return;
10624 -- Otherwise an aggregate may match both the array type and the
10625 -- component type.
10627 else
10628 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10629 Set_Etype (Arg, Any_Type);
10630 end if;
10632 else
10633 if Is_Overloaded (Arg)
10634 and then Has_Compatible_Type (Arg, Typ)
10635 and then Etype (Arg) /= Any_Type
10636 then
10637 declare
10638 I : Interp_Index;
10639 It : Interp;
10640 Func : Entity_Id;
10642 begin
10643 Get_First_Interp (Arg, I, It);
10644 Func := It.Nam;
10645 Get_Next_Interp (I, It);
10647 -- Special-case the error message when the overloading is
10648 -- caused by a function that yields an array and can be
10649 -- called without parameters.
10651 if It.Nam = Func then
10652 Error_Msg_Sloc := Sloc (Func);
10653 Error_Msg_N ("ambiguous call to function#", Arg);
10654 Error_Msg_NE
10655 ("\\interpretation as call yields&", Arg, Typ);
10656 Error_Msg_NE
10657 ("\\interpretation as indexing of call yields&",
10658 Arg, Ctyp);
10660 else
10661 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10663 Get_First_Interp (Arg, I, It);
10664 while Present (It.Nam) loop
10665 Error_Msg_Sloc := Sloc (It.Nam);
10667 if Base_Type (It.Typ) = Btyp
10668 or else
10669 Base_Type (It.Typ) = Base_Type (Ctyp)
10670 then
10671 Error_Msg_N -- CODEFIX
10672 ("\\possible interpretation#", Arg);
10673 end if;
10675 Get_Next_Interp (I, It);
10676 end loop;
10677 end if;
10678 end;
10679 end if;
10681 Resolve (Arg, Ctyp);
10683 if Nkind (Arg) = N_String_Literal then
10684 Set_Etype (Arg, Ctyp);
10686 elsif Is_Scalar_Type (Etype (Arg))
10687 and then Compile_Time_Known_Value (Arg)
10688 then
10689 -- Determine if the out-of-range violation constitutes a
10690 -- warning or an error according to the expression base type,
10691 -- according to Ada 2022 RM 4.9 (35/2).
10693 if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then
10694 Apply_Compile_Time_Constraint_Error
10695 (Arg, "value not in range of}", CE_Range_Check_Failed,
10696 Ent => Base_Type (Ctyp),
10697 Typ => Base_Type (Ctyp));
10699 elsif Is_Out_Of_Range (Arg, Ctyp) then
10700 Apply_Compile_Time_Constraint_Error
10701 (Arg, "value not in range of}??", CE_Range_Check_Failed,
10702 Ent => Ctyp,
10703 Typ => Ctyp);
10704 end if;
10705 end if;
10707 if Arg = Left_Opnd (N) then
10708 Set_Is_Component_Left_Opnd (N);
10709 else
10710 Set_Is_Component_Right_Opnd (N);
10711 end if;
10712 end if;
10714 else
10715 Resolve (Arg, Btyp);
10716 end if;
10718 Check_Unset_Reference (Arg);
10719 end Resolve_Op_Concat_Arg;
10721 -----------------------------
10722 -- Resolve_Op_Concat_First --
10723 -----------------------------
10725 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10726 Btyp : constant Entity_Id := Base_Type (Typ);
10727 Op1 : constant Node_Id := Left_Opnd (N);
10728 Op2 : constant Node_Id := Right_Opnd (N);
10730 begin
10731 -- The parser folds an enormous sequence of concatenations of string
10732 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10733 -- in the right operand. If the expression resolves to a predefined "&"
10734 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10735 -- we give an error. See P_Simple_Expression in Par.Ch4.
10737 if Nkind (Op2) = N_String_Literal
10738 and then Is_Folded_In_Parser (Op2)
10739 and then Ekind (Entity (N)) = E_Function
10740 then
10741 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10742 and then String_Length (Strval (Op1)) = 0);
10743 Error_Msg_N ("too many user-defined concatenations", N);
10744 return;
10745 end if;
10747 Set_Etype (N, Btyp);
10749 if Is_Limited_Composite (Btyp) then
10750 Error_Msg_N ("concatenation not available for limited array", N);
10751 Explain_Limited_Type (Btyp, N);
10752 end if;
10753 end Resolve_Op_Concat_First;
10755 ----------------------------
10756 -- Resolve_Op_Concat_Rest --
10757 ----------------------------
10759 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10760 Op1 : constant Node_Id := Left_Opnd (N);
10761 Op2 : constant Node_Id := Right_Opnd (N);
10763 begin
10764 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10766 Generate_Operator_Reference (N, Typ);
10768 if Is_String_Type (Typ) then
10769 Eval_Concatenation (N);
10770 end if;
10772 -- If this is not a static concatenation, but the result is a string
10773 -- type (and not an array of strings) ensure that static string operands
10774 -- have their subtypes properly constructed.
10776 if Nkind (N) /= N_String_Literal
10777 and then Is_Character_Type (Component_Type (Typ))
10778 then
10779 Set_String_Literal_Subtype (Op1, Typ);
10780 Set_String_Literal_Subtype (Op2, Typ);
10781 end if;
10782 end Resolve_Op_Concat_Rest;
10784 ----------------------
10785 -- Resolve_Op_Expon --
10786 ----------------------
10788 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10789 B_Typ : constant Entity_Id := Base_Type (Typ);
10791 begin
10792 -- Catch attempts to do fixed-point exponentiation with universal
10793 -- operands, which is a case where the illegality is not caught during
10794 -- normal operator analysis. This is not done in preanalysis mode
10795 -- since the tree is not fully decorated during preanalysis.
10797 if Full_Analysis then
10798 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10799 Error_Msg_N ("exponentiation not available for fixed point", N);
10800 return;
10802 elsif Nkind (Parent (N)) in N_Op
10803 and then Present (Etype (Parent (N)))
10804 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10805 and then Etype (N) = Universal_Real
10806 and then Comes_From_Source (N)
10807 then
10808 Error_Msg_N ("exponentiation not available for fixed point", N);
10809 return;
10810 end if;
10811 end if;
10813 if Ekind (Entity (N)) = E_Function
10814 and then Is_Imported (Entity (N))
10815 and then Is_Intrinsic_Subprogram (Entity (N))
10816 then
10817 Generate_Reference (Entity (N), N);
10818 Resolve_Intrinsic_Operator (N, Typ);
10819 return;
10820 end if;
10822 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10823 Check_For_Visible_Operator (N, B_Typ);
10824 end if;
10826 -- We do the resolution using the base type, because intermediate values
10827 -- in expressions are always of the base type, not a subtype of it.
10829 Resolve (Left_Opnd (N), B_Typ);
10830 Resolve (Right_Opnd (N), Standard_Integer);
10832 -- For integer types, right argument must be in Natural range
10834 if Is_Integer_Type (Typ) then
10835 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10836 end if;
10838 Check_Unset_Reference (Left_Opnd (N));
10839 Check_Unset_Reference (Right_Opnd (N));
10841 Set_Etype (N, B_Typ);
10842 Generate_Operator_Reference (N, B_Typ);
10844 Analyze_Dimension (N);
10846 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10847 -- Evaluate the exponentiation operator for dimensioned type
10849 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10850 else
10851 Eval_Op_Expon (N);
10852 end if;
10854 -- Set overflow checking bit. Much cleverer code needed here eventually
10855 -- and perhaps the Resolve routines should be separated for the various
10856 -- arithmetic operations, since they will need different processing. ???
10858 if Nkind (N) in N_Op then
10859 if not Overflow_Checks_Suppressed (Etype (N)) then
10860 Enable_Overflow_Check (N);
10861 end if;
10862 end if;
10863 end Resolve_Op_Expon;
10865 --------------------
10866 -- Resolve_Op_Not --
10867 --------------------
10869 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10870 function Parent_Is_Boolean return Boolean;
10871 -- This function determines if the parent node is a boolean operator or
10872 -- operation (comparison op, membership test, or short circuit form) and
10873 -- the not in question is the left operand of this operation. Note that
10874 -- if the not is in parens, then false is returned.
10876 -----------------------
10877 -- Parent_Is_Boolean --
10878 -----------------------
10880 function Parent_Is_Boolean return Boolean is
10881 begin
10882 return Paren_Count (N) = 0
10883 and then Nkind (Parent (N)) in N_Membership_Test
10884 | N_Op_Boolean
10885 | N_Short_Circuit
10886 and then Left_Opnd (Parent (N)) = N;
10887 end Parent_Is_Boolean;
10889 -- Local variables
10891 B_Typ : Entity_Id;
10893 -- Start of processing for Resolve_Op_Not
10895 begin
10896 -- Predefined operations on scalar types yield the base type. On the
10897 -- other hand, logical operations on arrays yield the type of the
10898 -- arguments (and the context).
10900 if Is_Array_Type (Typ) then
10901 B_Typ := Typ;
10902 else
10903 B_Typ := Base_Type (Typ);
10904 end if;
10906 -- Straightforward case of incorrect arguments
10908 if not Valid_Boolean_Arg (Typ) then
10909 Error_Msg_N ("invalid operand type for operator&", N);
10910 Set_Etype (N, Any_Type);
10911 return;
10913 -- Special case of probable missing parens
10915 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10916 if Parent_Is_Boolean then
10917 Error_Msg_N
10918 ("operand of NOT must be enclosed in parentheses",
10919 Right_Opnd (N));
10920 else
10921 Error_Msg_N
10922 ("no modular type available in this context", N);
10923 end if;
10925 Set_Etype (N, Any_Type);
10926 return;
10928 -- OK resolution of NOT
10930 else
10931 -- Warn if non-boolean types involved. This is a case like not a < b
10932 -- where a and b are modular, where we will get (not a) < b and most
10933 -- likely not (a < b) was intended.
10935 if Warn_On_Questionable_Missing_Parens
10936 and then not Is_Boolean_Type (Typ)
10937 and then Parent_Is_Boolean
10938 then
10939 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10940 end if;
10942 -- Warn on double negation if checking redundant constructs
10944 if Warn_On_Redundant_Constructs
10945 and then Comes_From_Source (N)
10946 and then Comes_From_Source (Right_Opnd (N))
10947 and then Root_Type (Typ) = Standard_Boolean
10948 and then Nkind (Right_Opnd (N)) = N_Op_Not
10949 then
10950 Error_Msg_N ("redundant double negation?r?", N);
10951 end if;
10953 -- Complete resolution and evaluation of NOT
10955 Resolve (Right_Opnd (N), B_Typ);
10956 Check_Unset_Reference (Right_Opnd (N));
10957 Set_Etype (N, B_Typ);
10958 Generate_Operator_Reference (N, B_Typ);
10959 Eval_Op_Not (N);
10960 end if;
10961 end Resolve_Op_Not;
10963 -----------------------------
10964 -- Resolve_Operator_Symbol --
10965 -----------------------------
10967 -- Nothing to be done, all resolved already
10969 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10970 pragma Warnings (Off, N);
10971 pragma Warnings (Off, Typ);
10973 begin
10974 null;
10975 end Resolve_Operator_Symbol;
10977 ----------------------------------
10978 -- Resolve_Qualified_Expression --
10979 ----------------------------------
10981 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10982 pragma Warnings (Off, Typ);
10984 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10985 Expr : constant Node_Id := Expression (N);
10987 begin
10988 Resolve (Expr, Target_Typ);
10989 Check_Unset_Reference (Expr);
10991 -- A qualified expression requires an exact match of the type, class-
10992 -- wide matching is not allowed. However, if the qualifying type is
10993 -- specific and the expression has a class-wide type, it may still be
10994 -- okay, since it can be the result of the expansion of a call to a
10995 -- dispatching function, so we also have to check class-wideness of the
10996 -- type of the expression's original node.
10998 if (Is_Class_Wide_Type (Target_Typ)
10999 or else
11000 (Is_Class_Wide_Type (Etype (Expr))
11001 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
11002 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
11003 then
11004 Wrong_Type (Expr, Target_Typ);
11005 end if;
11007 -- If the target type is unconstrained, then we reset the type of the
11008 -- result from the type of the expression. For other cases, the actual
11009 -- subtype of the expression is the target type. But we avoid doing it
11010 -- for an allocator since this is not needed and might be problematic.
11012 if Is_Composite_Type (Target_Typ)
11013 and then not Is_Constrained (Target_Typ)
11014 and then Nkind (Parent (N)) /= N_Allocator
11015 then
11016 Set_Etype (N, Etype (Expr));
11017 end if;
11019 Analyze_Dimension (N);
11020 Eval_Qualified_Expression (N);
11022 -- If we still have a qualified expression after the static evaluation,
11023 -- then apply a scalar range check if needed. The reason that we do this
11024 -- after the Eval call is that otherwise, the application of the range
11025 -- check may convert an illegal static expression and result in warning
11026 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
11028 if Nkind (N) = N_Qualified_Expression
11029 and then Is_Scalar_Type (Target_Typ)
11030 then
11031 Apply_Scalar_Range_Check (Expr, Target_Typ);
11032 end if;
11034 -- AI12-0100: Once the qualified expression is resolved, check whether
11035 -- operand satisfies a static predicate of the target subtype, if any.
11036 -- In the static expression case, a predicate check failure is an error.
11038 if Has_Predicates (Target_Typ) then
11039 Check_Expression_Against_Static_Predicate
11040 (Expr, Target_Typ, Static_Failure_Is_Error => True);
11041 end if;
11042 end Resolve_Qualified_Expression;
11044 ------------------------------
11045 -- Resolve_Raise_Expression --
11046 ------------------------------
11048 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
11049 begin
11050 if Typ = Raise_Type then
11051 Error_Msg_N ("cannot find unique type for raise expression", N);
11052 Set_Etype (N, Any_Type);
11054 else
11055 Set_Etype (N, Typ);
11057 -- Apply check for required parentheses in the enclosing
11058 -- context of raise_expressions (RM 11.3 (2)), including default
11059 -- expressions in contexts that can include aspect specifications,
11060 -- and ancestor parts of extension aggregates.
11062 declare
11063 Par : Node_Id := Parent (N);
11064 Parentheses_Found : Boolean := Paren_Count (N) > 0;
11066 begin
11067 while Present (Par)
11068 and then Nkind (Par) in N_Has_Etype
11069 loop
11070 if Paren_Count (Par) > 0 then
11071 Parentheses_Found := True;
11072 end if;
11074 if Nkind (Par) = N_Extension_Aggregate
11075 and then N = Ancestor_Part (Par)
11076 then
11077 exit;
11078 end if;
11080 Par := Parent (Par);
11081 end loop;
11083 if not Parentheses_Found
11084 and then Comes_From_Source (Par)
11085 and then
11086 (Nkind (Par) in N_Modular_Type_Definition
11087 | N_Floating_Point_Definition
11088 | N_Ordinary_Fixed_Point_Definition
11089 | N_Decimal_Fixed_Point_Definition
11090 | N_Extension_Aggregate
11091 | N_Discriminant_Specification
11092 | N_Parameter_Specification
11093 | N_Formal_Object_Declaration
11095 or else (Nkind (Par) = N_Object_Declaration
11096 and then
11097 Nkind (Parent (Par)) /= N_Extended_Return_Statement))
11098 then
11099 Error_Msg_N
11100 ("raise_expression must be parenthesized in this context",
11102 end if;
11103 end;
11104 end if;
11105 end Resolve_Raise_Expression;
11107 -------------------
11108 -- Resolve_Range --
11109 -------------------
11111 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
11112 L : constant Node_Id := Low_Bound (N);
11113 H : constant Node_Id := High_Bound (N);
11115 function First_Last_Ref return Boolean;
11116 -- Returns True if N is of the form X'First .. X'Last where X is the
11117 -- same entity for both attributes.
11119 --------------------
11120 -- First_Last_Ref --
11121 --------------------
11123 function First_Last_Ref return Boolean is
11124 Lorig : constant Node_Id := Original_Node (L);
11125 Horig : constant Node_Id := Original_Node (H);
11127 begin
11128 if Nkind (Lorig) = N_Attribute_Reference
11129 and then Nkind (Horig) = N_Attribute_Reference
11130 and then Attribute_Name (Lorig) = Name_First
11131 and then Attribute_Name (Horig) = Name_Last
11132 then
11133 declare
11134 PL : constant Node_Id := Prefix (Lorig);
11135 PH : constant Node_Id := Prefix (Horig);
11136 begin
11137 return Is_Entity_Name (PL)
11138 and then Is_Entity_Name (PH)
11139 and then Entity (PL) = Entity (PH);
11140 end;
11141 end if;
11143 return False;
11144 end First_Last_Ref;
11146 -- Start of processing for Resolve_Range
11148 begin
11149 Set_Etype (N, Typ);
11151 Resolve (L, Typ);
11152 Resolve (H, Typ);
11154 -- Reanalyze the lower bound after both bounds have been analyzed, so
11155 -- that the range is known to be static or not by now. This may trigger
11156 -- more compile-time evaluation, which is useful for static analysis
11157 -- with GNATprove. This is not needed for compilation or static analysis
11158 -- with CodePeer, as full expansion does that evaluation then.
11160 if GNATprove_Mode then
11161 Set_Analyzed (L, False);
11162 Resolve (L, Typ);
11163 end if;
11165 -- Check for inappropriate range on unordered enumeration type
11167 if Bad_Unordered_Enumeration_Reference (N, Typ)
11169 -- Exclude X'First .. X'Last if X is the same entity for both
11171 and then not First_Last_Ref
11172 then
11173 Error_Msg_Sloc := Sloc (Typ);
11174 Error_Msg_NE
11175 ("subrange of unordered enumeration type& declared#?.u?", N, Typ);
11176 end if;
11178 Check_Unset_Reference (L);
11179 Check_Unset_Reference (H);
11181 -- We have to check the bounds for being within the base range as
11182 -- required for a non-static context. Normally this is automatic and
11183 -- done as part of evaluating expressions, but the N_Range node is an
11184 -- exception, since in GNAT we consider this node to be a subexpression,
11185 -- even though in Ada it is not. The circuit in Sem_Eval could check for
11186 -- this, but that would put the test on the main evaluation path for
11187 -- expressions.
11189 Check_Non_Static_Context (L);
11190 Check_Non_Static_Context (H);
11192 -- Check for an ambiguous range over character literals. This will
11193 -- happen with a membership test involving only literals.
11195 if Typ = Any_Character then
11196 Ambiguous_Character (L);
11197 Set_Etype (N, Any_Type);
11198 return;
11199 end if;
11201 -- If bounds are static, constant-fold them, so size computations are
11202 -- identical between front-end and back-end. Do not perform this
11203 -- transformation while analyzing generic units, as type information
11204 -- would be lost when reanalyzing the constant node in the instance.
11206 if Is_Discrete_Type (Typ) and then Expander_Active then
11207 if Is_OK_Static_Expression (L) then
11208 Fold_Uint (L, Expr_Value (L), Static => True);
11209 end if;
11211 if Is_OK_Static_Expression (H) then
11212 Fold_Uint (H, Expr_Value (H), Static => True);
11213 end if;
11214 end if;
11216 -- If we have a compile-time-known null range, we warn, because that is
11217 -- likely to be a mistake. (Dynamic null ranges make sense, but often
11218 -- compile-time-known ones do not.) Warn only if this is in a subtype
11219 -- declaration. We do this here, rather than while analyzing a subtype
11220 -- declaration, in case we decide to expand the cases. We do not want to
11221 -- warn in all cases, because some are idiomatic, such as an empty
11222 -- aggregate (1 .. 0 => <>).
11224 -- We don't warn in generics or their instances, because there might be
11225 -- some instances where the range is null, and some where it is not,
11226 -- which would lead to false alarms.
11228 if not (Inside_A_Generic or In_Instance)
11229 and then Comes_From_Source (N)
11230 and then Compile_Time_Compare
11231 (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT
11232 and then Nkind (Parent (N)) = N_Range_Constraint
11233 and then Nkind (Parent (Parent (N))) = N_Subtype_Indication
11234 and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration
11235 and then Is_OK_Static_Range (N)
11236 then
11237 Error_Msg_N ("null range??", N);
11238 end if;
11239 end Resolve_Range;
11241 --------------------------
11242 -- Resolve_Real_Literal --
11243 --------------------------
11245 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
11246 Actual_Typ : constant Entity_Id := Etype (N);
11248 begin
11249 -- Special processing for fixed-point literals to make sure that the
11250 -- value is an exact multiple of the small where this is required. We
11251 -- skip this for the universal real case, and also for generic types.
11253 if Is_Fixed_Point_Type (Typ)
11254 and then Typ /= Universal_Fixed
11255 and then Typ /= Any_Fixed
11256 and then not Is_Generic_Type (Typ)
11257 then
11258 -- We must freeze the base type to get the proper value of the small
11260 if not Is_Frozen (Base_Type (Typ)) then
11261 Freeze_Fixed_Point_Type (Base_Type (Typ));
11262 end if;
11264 declare
11265 Val : constant Ureal := Realval (N);
11266 Cintr : constant Ureal := Val / Small_Value (Base_Type (Typ));
11267 Cint : constant Uint := UR_Trunc (Cintr);
11268 Den : constant Uint := Norm_Den (Cintr);
11269 Stat : Boolean;
11271 begin
11272 -- Case of literal is not an exact multiple of the Small
11274 if Den /= 1 then
11276 -- For a source program literal for a decimal fixed-point type,
11277 -- this is statically illegal (RM 4.9(36)).
11279 if Is_Decimal_Fixed_Point_Type (Typ)
11280 and then Actual_Typ = Universal_Real
11281 and then Comes_From_Source (N)
11282 then
11283 Error_Msg_N ("value has extraneous low order digits", N);
11284 end if;
11286 -- Generate a warning if literal from source
11288 if Is_OK_Static_Expression (N)
11289 and then Warn_On_Bad_Fixed_Value
11290 then
11291 Error_Msg_N
11292 ("?b?static fixed-point value is not a multiple of Small!",
11294 end if;
11296 -- Replace literal by a value that is the exact representation
11297 -- of a value of the type, i.e. a multiple of the small value,
11298 -- by truncation, since Machine_Rounds is false for all GNAT
11299 -- fixed-point types (RM 4.9(38)).
11301 Stat := Is_OK_Static_Expression (N);
11302 Rewrite (N,
11303 Make_Real_Literal (Sloc (N),
11304 Realval => Small_Value (Typ) * Cint));
11306 Set_Is_Static_Expression (N, Stat);
11307 end if;
11309 -- In all cases, set the corresponding integer field
11311 Set_Corresponding_Integer_Value (N, Cint);
11312 end;
11313 end if;
11315 -- Now replace the actual type by the expected type as usual
11317 Set_Etype (N, Typ);
11318 Eval_Real_Literal (N);
11319 end Resolve_Real_Literal;
11321 -----------------------
11322 -- Resolve_Reference --
11323 -----------------------
11325 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
11326 P : constant Node_Id := Prefix (N);
11328 begin
11329 -- Replace general access with specific type
11331 if Ekind (Etype (N)) = E_Allocator_Type then
11332 Set_Etype (N, Base_Type (Typ));
11333 end if;
11335 Resolve (P, Designated_Type (Etype (N)));
11337 -- If we are taking the reference of a volatile entity, then treat it as
11338 -- a potential modification of this entity. This is too conservative,
11339 -- but necessary because remove side effects can cause transformations
11340 -- of normal assignments into reference sequences that otherwise fail to
11341 -- notice the modification.
11343 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
11344 Note_Possible_Modification (P, Sure => False);
11345 end if;
11346 end Resolve_Reference;
11348 --------------------------------
11349 -- Resolve_Selected_Component --
11350 --------------------------------
11352 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
11353 Comp : Entity_Id;
11354 Comp1 : Entity_Id := Empty; -- prevent junk warning
11355 P : constant Node_Id := Prefix (N);
11356 S : constant Node_Id := Selector_Name (N);
11357 T : Entity_Id := Etype (P);
11358 I : Interp_Index;
11359 I1 : Interp_Index := 0; -- prevent junk warning
11360 It : Interp;
11361 It1 : Interp;
11362 Found : Boolean;
11364 function Init_Component return Boolean;
11365 -- Check whether this is the initialization of a component within an
11366 -- init proc (by assignment or call to another init proc). If true,
11367 -- there is no need for a discriminant check.
11369 --------------------
11370 -- Init_Component --
11371 --------------------
11373 function Init_Component return Boolean is
11374 begin
11375 return Inside_Init_Proc
11376 and then Nkind (Prefix (N)) = N_Identifier
11377 and then Chars (Prefix (N)) = Name_uInit
11378 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
11379 end Init_Component;
11381 -- Start of processing for Resolve_Selected_Component
11383 begin
11384 if Is_Overloaded (P) then
11386 -- Use the context type to select the prefix that has a selector
11387 -- of the correct name and type.
11389 Found := False;
11390 Get_First_Interp (P, I, It);
11392 Search : while Present (It.Typ) loop
11393 if Is_Access_Type (It.Typ) then
11394 T := Designated_Type (It.Typ);
11395 else
11396 T := It.Typ;
11397 end if;
11399 -- Locate selected component. For a private prefix the selector
11400 -- can denote a discriminant.
11402 if Is_Record_Type (T) or else Is_Private_Type (T) then
11404 -- The visible components of a class-wide type are those of
11405 -- the root type.
11407 if Is_Class_Wide_Type (T) then
11408 T := Etype (T);
11409 end if;
11411 Comp := First_Entity (T);
11412 while Present (Comp) loop
11413 if Chars (Comp) = Chars (S)
11414 and then Covers (Typ, Etype (Comp))
11415 then
11416 if not Found then
11417 Found := True;
11418 I1 := I;
11419 It1 := It;
11420 Comp1 := Comp;
11422 else
11423 It := Disambiguate (P, I1, I, Any_Type);
11425 if It = No_Interp then
11426 Error_Msg_N
11427 ("ambiguous prefix for selected component", N);
11428 Set_Etype (N, Typ);
11429 return;
11431 else
11432 It1 := It;
11434 -- There may be an implicit dereference. Retrieve
11435 -- designated record type.
11437 if Is_Access_Type (It1.Typ) then
11438 T := Designated_Type (It1.Typ);
11439 else
11440 T := It1.Typ;
11441 end if;
11443 if Scope (Comp1) /= T then
11445 -- Resolution chooses the new interpretation.
11446 -- Find the component with the right name.
11448 Comp1 := First_Entity (T);
11449 while Present (Comp1)
11450 and then Chars (Comp1) /= Chars (S)
11451 loop
11452 Next_Entity (Comp1);
11453 end loop;
11454 end if;
11456 exit Search;
11457 end if;
11458 end if;
11459 end if;
11461 Next_Entity (Comp);
11462 end loop;
11463 end if;
11465 Get_Next_Interp (I, It);
11466 end loop Search;
11468 -- There must be a legal interpretation at this point
11470 pragma Assert (Found);
11471 Resolve (P, It1.Typ);
11473 -- In general the expected type is the type of the context, not the
11474 -- type of the candidate selected component.
11476 Set_Etype (N, Typ);
11477 Set_Entity_With_Checks (S, Comp1);
11479 -- The type of the context and that of the component are
11480 -- compatible and in general identical, but if they are anonymous
11481 -- access-to-subprogram types, the relevant type is that of the
11482 -- component. This matters in Unnest_Subprograms mode, where the
11483 -- relevant context is the one in which the type is declared, not
11484 -- the point of use. This determines what activation record to use.
11486 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
11487 Set_Etype (N, Etype (Comp1));
11489 -- When the type of the component is an access to a class-wide type
11490 -- the relevant type is that of the component (since in such case we
11491 -- may need to generate implicit type conversions or dispatching
11492 -- calls).
11494 elsif Is_Access_Type (Typ)
11495 and then not Is_Class_Wide_Type (Designated_Type (Typ))
11496 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
11497 then
11498 Set_Etype (N, Etype (Comp1));
11499 end if;
11501 else
11502 -- Resolve prefix with its type
11504 Resolve (P, T);
11505 end if;
11507 -- Generate cross-reference. We needed to wait until full overloading
11508 -- resolution was complete to do this, since otherwise we can't tell if
11509 -- we are an lvalue or not.
11511 if Known_To_Be_Assigned (N) then
11512 Generate_Reference (Entity (S), S, 'm');
11513 else
11514 Generate_Reference (Entity (S), S, 'r');
11515 end if;
11517 -- If the prefix's type is an access type, get to the real record type.
11518 -- Note: we do not apply an access check because an explicit dereference
11519 -- will be introduced later, and the check will happen there.
11521 if Is_Access_Type (Etype (P)) then
11522 T := Implicitly_Designated_Type (Etype (P));
11523 Check_Fully_Declared_Prefix (T, P);
11525 else
11526 T := Etype (P);
11527 end if;
11529 -- Set flag for expander if discriminant check required on a component
11530 -- appearing within a variant.
11532 if Has_Discriminants (T)
11533 and then Ekind (Entity (S)) = E_Component
11534 and then Present (Original_Record_Component (Entity (S)))
11535 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
11536 and then
11537 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
11538 and then not Discriminant_Checks_Suppressed (T)
11539 and then not Init_Component
11540 then
11541 Set_Do_Discriminant_Check (N);
11542 end if;
11544 if Ekind (Entity (S)) = E_Void then
11545 Error_Msg_N ("premature use of component", S);
11546 end if;
11548 -- If the prefix is a record conversion, this may be a renamed
11549 -- discriminant whose bounds differ from those of the original
11550 -- one, so we must ensure that a range check is performed.
11552 if Nkind (P) = N_Type_Conversion
11553 and then Ekind (Entity (S)) = E_Discriminant
11554 and then Is_Discrete_Type (Typ)
11555 then
11556 Set_Etype (N, Base_Type (Typ));
11557 end if;
11559 -- Eval_Selected_Component may e.g. fold statically known discriminants.
11561 Eval_Selected_Component (N);
11563 if Nkind (N) = N_Selected_Component then
11565 -- If the record type is atomic and the component is not, then this
11566 -- is worth a warning before Ada 2022, since we have a situation
11567 -- where the access to the component may cause extra read/writes of
11568 -- the atomic object, or partial word accesses, both of which may be
11569 -- unexpected.
11571 if Is_Atomic_Non_VFA_Ref_With_Address (N)
11572 and then not Is_Atomic (Entity (S))
11573 and then not Is_Atomic (Etype (Entity (S)))
11574 and then Ada_Version < Ada_2022
11575 then
11576 Error_Msg_N
11577 ("??access to non-atomic component of atomic record",
11578 Prefix (N));
11579 Error_Msg_N
11580 ("\??may cause unexpected accesses to atomic object",
11581 Prefix (N));
11582 end if;
11584 Resolve_Implicit_Dereference (Prefix (N));
11585 Analyze_Dimension (N);
11586 end if;
11587 end Resolve_Selected_Component;
11589 -------------------
11590 -- Resolve_Shift --
11591 -------------------
11593 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11594 B_Typ : constant Entity_Id := Base_Type (Typ);
11595 L : constant Node_Id := Left_Opnd (N);
11596 R : constant Node_Id := Right_Opnd (N);
11598 begin
11599 -- We do the resolution using the base type, because intermediate values
11600 -- in expressions always are of the base type, not a subtype of it.
11602 Resolve (L, B_Typ);
11603 Resolve (R, Standard_Natural);
11605 Check_Unset_Reference (L);
11606 Check_Unset_Reference (R);
11608 Set_Etype (N, B_Typ);
11609 Generate_Operator_Reference (N, B_Typ);
11610 Eval_Shift (N);
11611 end Resolve_Shift;
11613 ---------------------------
11614 -- Resolve_Short_Circuit --
11615 ---------------------------
11617 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11618 B_Typ : constant Entity_Id := Base_Type (Typ);
11619 L : constant Node_Id := Left_Opnd (N);
11620 R : constant Node_Id := Right_Opnd (N);
11622 begin
11623 -- Ensure all actions associated with the left operand (e.g.
11624 -- finalization of transient objects) are fully evaluated locally within
11625 -- an expression with actions. This is particularly helpful for coverage
11626 -- analysis at the object level. However this should not happen in
11627 -- generics.
11629 if Expander_Active then
11630 declare
11631 Reloc_L : constant Node_Id := Relocate_Node (L);
11632 begin
11633 Save_Interps (Old_N => L, New_N => Reloc_L);
11635 Rewrite (L,
11636 Make_Expression_With_Actions (Sloc (L),
11637 Actions => New_List,
11638 Expression => Reloc_L));
11640 -- Set Comes_From_Source on L to preserve warnings for unset
11641 -- reference.
11643 Preserve_Comes_From_Source (L, Reloc_L);
11644 end;
11645 end if;
11647 Resolve (L, B_Typ);
11648 Resolve (R, B_Typ);
11650 -- Check for issuing warning for always False assert/check, this happens
11651 -- when assertions are turned off, in which case the pragma Assert/Check
11652 -- was transformed into:
11654 -- if False and then <condition> then ...
11656 -- and we detect this pattern
11658 if Warn_On_Assertion_Failure
11659 and then Is_Entity_Name (R)
11660 and then Entity (R) = Standard_False
11661 and then Nkind (Parent (N)) = N_If_Statement
11662 and then Nkind (N) = N_And_Then
11663 and then Is_Entity_Name (L)
11664 and then Entity (L) = Standard_False
11665 then
11666 declare
11667 Orig : constant Node_Id := Original_Node (Parent (N));
11669 begin
11670 -- Special handling of Asssert pragma
11672 if Nkind (Orig) = N_Pragma
11673 and then Pragma_Name (Orig) = Name_Assert
11674 then
11675 declare
11676 Expr : constant Node_Id :=
11677 Original_Node
11678 (Expression
11679 (First (Pragma_Argument_Associations (Orig))));
11681 begin
11682 -- Don't warn if original condition is explicit False,
11683 -- since obviously the failure is expected in this case.
11685 if Is_Entity_Name (Expr)
11686 and then Entity (Expr) = Standard_False
11687 then
11688 null;
11690 -- Issue warning. We do not want the deletion of the
11691 -- IF/AND-THEN to take this message with it. We achieve this
11692 -- by making sure that the expanded code points to the Sloc
11693 -- of the expression, not the original pragma.
11695 else
11696 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11697 -- The source location of the expression is not usually
11698 -- the best choice here. For example, it gets located on
11699 -- the last AND keyword in a chain of boolean expressiond
11700 -- AND'ed together. It is best to put the message on the
11701 -- first character of the assertion, which is the effect
11702 -- of the First_Node call here.
11704 Error_Msg_F
11705 ("?.a?assertion would fail at run time!",
11706 Expression
11707 (First (Pragma_Argument_Associations (Orig))));
11708 end if;
11709 end;
11711 -- Similar processing for Check pragma
11713 elsif Nkind (Orig) = N_Pragma
11714 and then Pragma_Name (Orig) = Name_Check
11715 then
11716 -- Don't want to warn if original condition is explicit False
11718 declare
11719 Expr : constant Node_Id :=
11720 Original_Node
11721 (Expression
11722 (Next (First (Pragma_Argument_Associations (Orig)))));
11723 begin
11724 if Is_Entity_Name (Expr)
11725 and then Entity (Expr) = Standard_False
11726 then
11727 null;
11729 -- Post warning
11731 else
11732 -- Again use Error_Msg_F rather than Error_Msg_N, see
11733 -- comment above for an explanation of why we do this.
11735 Error_Msg_F
11736 ("?.a?check would fail at run time!",
11737 Expression
11738 (Last (Pragma_Argument_Associations (Orig))));
11739 end if;
11740 end;
11741 end if;
11742 end;
11743 end if;
11745 -- Continue with processing of short circuit
11747 Check_Unset_Reference (L);
11748 Check_Unset_Reference (R);
11750 Set_Etype (N, B_Typ);
11751 Eval_Short_Circuit (N);
11752 end Resolve_Short_Circuit;
11754 -------------------
11755 -- Resolve_Slice --
11756 -------------------
11758 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11759 Drange : constant Node_Id := Discrete_Range (N);
11760 Pref : constant Node_Id := Prefix (N);
11761 Array_Type : Entity_Id := Empty;
11762 Dexpr : Node_Id := Empty;
11763 Index_Type : Entity_Id;
11765 begin
11766 if Is_Overloaded (Pref) then
11768 -- Use the context type to select the prefix that yields the correct
11769 -- array type.
11771 declare
11772 I : Interp_Index;
11773 I1 : Interp_Index := 0;
11774 It : Interp;
11775 Found : Boolean := False;
11777 begin
11778 Get_First_Interp (Pref, I, It);
11779 while Present (It.Typ) loop
11780 if (Is_Array_Type (It.Typ)
11781 and then Covers (Typ, It.Typ))
11782 or else (Is_Access_Type (It.Typ)
11783 and then Is_Array_Type (Designated_Type (It.Typ))
11784 and then Covers (Typ, Designated_Type (It.Typ)))
11785 then
11786 if Found then
11787 It := Disambiguate (Pref, I1, I, Any_Type);
11789 if It = No_Interp then
11790 Error_Msg_N ("ambiguous prefix for slicing", N);
11791 Set_Etype (N, Typ);
11792 return;
11793 else
11794 Found := True;
11795 Array_Type := It.Typ;
11796 I1 := I;
11797 end if;
11798 else
11799 Found := True;
11800 Array_Type := It.Typ;
11801 I1 := I;
11802 end if;
11803 end if;
11805 Get_Next_Interp (I, It);
11806 end loop;
11807 end;
11809 else
11810 Array_Type := Etype (Pref);
11811 end if;
11813 Resolve (Pref, Array_Type);
11815 -- If the prefix's type is an access type, get to the real array type.
11816 -- Note: we do not apply an access check because an explicit dereference
11817 -- will be introduced later, and the check will happen there.
11819 if Is_Access_Type (Array_Type) then
11820 Array_Type := Implicitly_Designated_Type (Array_Type);
11822 -- If the prefix is an access to an unconstrained array, we must use
11823 -- the actual subtype of the object to perform the index checks. The
11824 -- object denoted by the prefix is implicit in the node, so we build
11825 -- an explicit representation for it in order to compute the actual
11826 -- subtype.
11828 if not Is_Constrained (Array_Type) then
11829 Remove_Side_Effects (Pref);
11831 declare
11832 Obj : constant Node_Id :=
11833 Make_Explicit_Dereference (Sloc (N),
11834 Prefix => New_Copy_Tree (Pref));
11835 begin
11836 Set_Etype (Obj, Array_Type);
11837 Set_Parent (Obj, Parent (N));
11838 Array_Type := Get_Actual_Subtype (Obj);
11839 end;
11840 end if;
11842 -- In CodePeer mode the attribute Image is not expanded, so when it
11843 -- acts as a prefix of a slice, we handle it like a call to function
11844 -- returning an unconstrained string. Same for the Wide variants of
11845 -- attribute Image.
11847 elsif Is_Entity_Name (Pref)
11848 or else Nkind (Pref) = N_Explicit_Dereference
11849 or else (Nkind (Pref) = N_Function_Call
11850 and then not Is_Constrained (Etype (Pref)))
11851 or else (CodePeer_Mode
11852 and then Nkind (Pref) = N_Attribute_Reference
11853 and then Attribute_Name (Pref) in Name_Image
11854 | Name_Wide_Image
11855 | Name_Wide_Wide_Image)
11856 then
11857 Array_Type := Get_Actual_Subtype (Pref);
11859 -- If the name is a selected component that depends on discriminants,
11860 -- build an actual subtype for it. This can happen only when the name
11861 -- itself is overloaded; otherwise the actual subtype is created when
11862 -- the selected component is analyzed.
11864 elsif Nkind (Pref) = N_Selected_Component
11865 and then Full_Analysis
11866 and then Depends_On_Discriminant (First_Index (Array_Type))
11867 then
11868 declare
11869 Act_Decl : constant Node_Id :=
11870 Build_Actual_Subtype_Of_Component (Array_Type, Pref);
11871 begin
11872 Insert_Action (N, Act_Decl);
11873 Array_Type := Defining_Identifier (Act_Decl);
11874 end;
11876 -- Maybe this should just be "else", instead of checking for the
11877 -- specific case of slice??? This is needed for the case where the
11878 -- prefix is an Image attribute, which gets expanded to a slice, and so
11879 -- has a constrained subtype which we want to use for the slice range
11880 -- check applied below (the range check won't get done if the
11881 -- unconstrained subtype of the 'Image is used).
11883 elsif Nkind (Pref) = N_Slice then
11884 Array_Type := Etype (Pref);
11885 end if;
11887 -- Obtain the type of the array index
11889 if Ekind (Array_Type) = E_String_Literal_Subtype then
11890 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11891 else
11892 Index_Type := Etype (First_Index (Array_Type));
11893 end if;
11895 -- If name was overloaded, set slice type correctly now
11897 Set_Etype (N, Array_Type);
11899 -- Handle the generation of a range check that compares the array index
11900 -- against the discrete_range. The check is not applied to internally
11901 -- built nodes associated with the expansion of dispatch tables. Check
11902 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11903 -- the unit.
11905 if Tagged_Type_Expansion
11906 and then RTU_Loaded (Ada_Tags)
11907 and then Nkind (Pref) = N_Selected_Component
11908 and then Present (Entity (Selector_Name (Pref)))
11909 and then Entity (Selector_Name (Pref)) =
11910 RTE_Record_Component (RE_Prims_Ptr)
11911 then
11912 null;
11914 -- The discrete_range is specified by a subtype name. Create an
11915 -- equivalent range attribute, apply checks to this attribute, but
11916 -- insert them into the range expression of the slice itself.
11918 elsif Is_Entity_Name (Drange) then
11919 Dexpr :=
11920 Make_Attribute_Reference
11921 (Sloc (Drange),
11922 Prefix =>
11923 New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
11924 Attribute_Name => Name_Range);
11926 Analyze_And_Resolve (Dexpr, Etype (Drange));
11928 elsif Nkind (Drange) = N_Subtype_Indication then
11929 Dexpr := Range_Expression (Constraint (Drange));
11931 -- The discrete_range is a regular range (or a range attribute, which
11932 -- will be resolved into a regular range). Resolve the bounds and remove
11933 -- their side effects.
11935 else
11936 Resolve (Drange, Base_Type (Index_Type));
11938 if Nkind (Drange) = N_Range then
11939 Force_Evaluation (Low_Bound (Drange));
11940 Force_Evaluation (High_Bound (Drange));
11942 Dexpr := Drange;
11943 end if;
11944 end if;
11946 if Present (Dexpr) then
11947 Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
11948 end if;
11950 Set_Slice_Subtype (N);
11952 -- Check bad use of type with predicates
11954 declare
11955 Subt : Entity_Id;
11957 begin
11958 if Nkind (Drange) = N_Subtype_Indication
11959 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11960 then
11961 Subt := Entity (Subtype_Mark (Drange));
11962 else
11963 Subt := Etype (Drange);
11964 end if;
11966 if Has_Predicates (Subt) then
11967 Bad_Predicated_Subtype_Use
11968 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11969 end if;
11970 end;
11972 -- Otherwise here is where we check suspicious indexes
11974 if Nkind (Drange) = N_Range then
11975 Warn_On_Suspicious_Index (Pref, Low_Bound (Drange));
11976 Warn_On_Suspicious_Index (Pref, High_Bound (Drange));
11977 end if;
11979 Resolve_Implicit_Dereference (Pref);
11980 Analyze_Dimension (N);
11981 Eval_Slice (N);
11982 end Resolve_Slice;
11984 ----------------------------
11985 -- Resolve_String_Literal --
11986 ----------------------------
11988 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11989 C_Typ : constant Entity_Id := Component_Type (Typ);
11990 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11991 Loc : constant Source_Ptr := Sloc (N);
11992 Str : constant String_Id := Strval (N);
11993 Strlen : constant Nat := String_Length (Str);
11994 Subtype_Id : Entity_Id;
11995 Need_Check : Boolean;
11997 begin
11998 -- For a string appearing in a concatenation, defer creation of the
11999 -- string_literal_subtype until the end of the resolution of the
12000 -- concatenation, because the literal may be constant-folded away. This
12001 -- is a useful optimization for long concatenation expressions.
12003 -- If the string is an aggregate built for a single character (which
12004 -- happens in a non-static context) or a is null string to which special
12005 -- checks may apply, we build the subtype. Wide strings must also get a
12006 -- string subtype if they come from a one character aggregate. Strings
12007 -- generated by attributes might be static, but it is often hard to
12008 -- determine whether the enclosing context is static, so we generate
12009 -- subtypes for them as well, thus losing some rarer optimizations ???
12010 -- Same for strings that come from a static conversion.
12012 Need_Check :=
12013 (Strlen = 0 and then Typ /= Standard_String)
12014 or else Nkind (Parent (N)) /= N_Op_Concat
12015 or else (N /= Left_Opnd (Parent (N))
12016 and then N /= Right_Opnd (Parent (N)))
12017 or else ((Typ = Standard_Wide_String
12018 or else Typ = Standard_Wide_Wide_String)
12019 and then Nkind (Original_Node (N)) /= N_String_Literal);
12021 -- If the resolving type is itself a string literal subtype, we can just
12022 -- reuse it, since there is no point in creating another.
12024 if Ekind (Typ) = E_String_Literal_Subtype then
12025 Subtype_Id := Typ;
12027 elsif Nkind (Parent (N)) = N_Op_Concat
12028 and then not Need_Check
12029 and then Nkind (Original_Node (N)) not in N_Character_Literal
12030 | N_Attribute_Reference
12031 | N_Qualified_Expression
12032 | N_Type_Conversion
12033 then
12034 Subtype_Id := Typ;
12036 -- Do not generate a string literal subtype for the default expression
12037 -- of a formal parameter in GNATprove mode. This is because the string
12038 -- subtype is associated with the freezing actions of the subprogram,
12039 -- however freezing is disabled in GNATprove mode and as a result the
12040 -- subtype is unavailable.
12042 elsif GNATprove_Mode
12043 and then Nkind (Parent (N)) = N_Parameter_Specification
12044 then
12045 Subtype_Id := Typ;
12047 -- Otherwise we must create a string literal subtype. Note that the
12048 -- whole idea of string literal subtypes is simply to avoid the need
12049 -- for building a full fledged array subtype for each literal.
12051 else
12052 Set_String_Literal_Subtype (N, Typ);
12053 Subtype_Id := Etype (N);
12054 end if;
12056 if Nkind (Parent (N)) /= N_Op_Concat
12057 or else Need_Check
12058 then
12059 Set_Etype (N, Subtype_Id);
12060 Eval_String_Literal (N);
12061 end if;
12063 if Is_Limited_Composite (Typ)
12064 or else Is_Private_Composite (Typ)
12065 then
12066 Error_Msg_N ("string literal not available for private array", N);
12067 Set_Etype (N, Any_Type);
12068 return;
12069 end if;
12071 -- The validity of a null string has been checked in the call to
12072 -- Eval_String_Literal.
12074 if Strlen = 0 then
12075 return;
12077 -- Always accept string literal with component type Any_Character, which
12078 -- occurs in error situations and in comparisons of literals, both of
12079 -- which should accept all literals.
12081 elsif R_Typ = Any_Character then
12082 return;
12084 -- If the type is bit-packed, then we always transform the string
12085 -- literal into a full fledged aggregate.
12087 elsif Is_Bit_Packed_Array (Typ) then
12088 null;
12090 -- Deal with cases of Wide_Wide_String, Wide_String, and String
12092 else
12093 -- For Standard.Wide_Wide_String, or any other type whose component
12094 -- type is Standard.Wide_Wide_Character, we know that all the
12095 -- characters in the string must be acceptable, since the parser
12096 -- accepted the characters as valid character literals.
12098 if R_Typ = Standard_Wide_Wide_Character then
12099 null;
12101 -- For the case of Standard.String, or any other type whose component
12102 -- type is Standard.Character, we must make sure that there are no
12103 -- wide characters in the string, i.e. that it is entirely composed
12104 -- of characters in range of type Character.
12106 -- If the string literal is the result of a static concatenation, the
12107 -- test has already been performed on the components, and need not be
12108 -- repeated.
12110 elsif R_Typ = Standard_Character
12111 and then Nkind (Original_Node (N)) /= N_Op_Concat
12112 then
12113 for J in 1 .. Strlen loop
12114 if not In_Character_Range (Get_String_Char (Str, J)) then
12116 -- If we are out of range, post error. This is one of the
12117 -- very few places that we place the flag in the middle of
12118 -- a token, right under the offending wide character. Not
12119 -- quite clear if this is right wrt wide character encoding
12120 -- sequences, but it's only an error message.
12122 Error_Msg
12123 ("literal out of range of type Standard.Character",
12124 Loc + Source_Ptr (J));
12125 return;
12126 end if;
12127 end loop;
12129 -- For the case of Standard.Wide_String, or any other type whose
12130 -- component type is Standard.Wide_Character, we must make sure that
12131 -- there are no wide characters in the string, i.e. that it is
12132 -- entirely composed of characters in range of type Wide_Character.
12134 -- If the string literal is the result of a static concatenation,
12135 -- the test has already been performed on the components, and need
12136 -- not be repeated.
12138 elsif R_Typ = Standard_Wide_Character
12139 and then Nkind (Original_Node (N)) /= N_Op_Concat
12140 then
12141 for J in 1 .. Strlen loop
12142 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
12144 -- If we are out of range, post error. This is one of the
12145 -- very few places that we place the flag in the middle of
12146 -- a token, right under the offending wide character.
12148 -- This is not quite right, because characters in general
12149 -- will take more than one character position ???
12151 Error_Msg
12152 ("literal out of range of type Standard.Wide_Character",
12153 Loc + Source_Ptr (J));
12154 return;
12155 end if;
12156 end loop;
12158 -- If the root type is not a standard character, then we will convert
12159 -- the string into an aggregate and will let the aggregate code do
12160 -- the checking. Standard Wide_Wide_Character is also OK here.
12162 else
12163 null;
12164 end if;
12166 -- See if the component type of the array corresponding to the string
12167 -- has compile time known bounds. If yes we can directly check
12168 -- whether the evaluation of the string will raise constraint error.
12169 -- Otherwise we need to transform the string literal into the
12170 -- corresponding character aggregate and let the aggregate code do
12171 -- the checking. We use the same transformation if the component
12172 -- type has a static predicate, which will be applied to each
12173 -- character when the aggregate is resolved.
12175 if Is_Standard_Character_Type (R_Typ) then
12177 -- Check for the case of full range, where we are definitely OK
12179 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
12180 return;
12181 end if;
12183 -- Here the range is not the complete base type range, so check
12185 declare
12186 Comp_Typ_Lo : constant Node_Id :=
12187 Type_Low_Bound (Component_Type (Typ));
12188 Comp_Typ_Hi : constant Node_Id :=
12189 Type_High_Bound (Component_Type (Typ));
12191 Char_Val : Uint;
12193 begin
12194 if Compile_Time_Known_Value (Comp_Typ_Lo)
12195 and then Compile_Time_Known_Value (Comp_Typ_Hi)
12196 then
12197 for J in 1 .. Strlen loop
12198 Char_Val := UI_From_CC (Get_String_Char (Str, J));
12200 if Char_Val < Expr_Value (Comp_Typ_Lo)
12201 or else Char_Val > Expr_Value (Comp_Typ_Hi)
12202 then
12203 Apply_Compile_Time_Constraint_Error
12204 (N, "character out of range??",
12205 CE_Range_Check_Failed,
12206 Loc => Loc + Source_Ptr (J));
12207 end if;
12208 end loop;
12210 if not Has_Static_Predicate (C_Typ) then
12211 return;
12212 end if;
12213 end if;
12214 end;
12215 end if;
12216 end if;
12218 -- If we got here we meed to transform the string literal into the
12219 -- equivalent qualified positional array aggregate. This is rather
12220 -- heavy artillery for this situation, but it is hard work to avoid.
12222 declare
12223 Lits : constant List_Id := New_List;
12224 P : Source_Ptr := Loc + 1;
12225 C : Char_Code;
12227 begin
12228 -- Build the character literals, we give them source locations that
12229 -- correspond to the string positions, which is a bit tricky given
12230 -- the possible presence of wide character escape sequences.
12232 for J in 1 .. Strlen loop
12233 C := Get_String_Char (Str, J);
12234 Set_Character_Literal_Name (C);
12236 Append_To (Lits,
12237 Make_Character_Literal (P,
12238 Chars => Name_Find,
12239 Char_Literal_Value => UI_From_CC (C)));
12241 if In_Character_Range (C) then
12242 P := P + 1;
12244 -- Should we have a call to Skip_Wide here ???
12246 -- ??? else
12247 -- Skip_Wide (P);
12249 end if;
12250 end loop;
12252 Rewrite (N,
12253 Make_Qualified_Expression (Loc,
12254 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
12255 Expression =>
12256 Make_Aggregate (Loc, Expressions => Lits)));
12258 Analyze_And_Resolve (N, Typ);
12259 end;
12260 end Resolve_String_Literal;
12262 -------------------------
12263 -- Resolve_Target_Name --
12264 -------------------------
12266 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
12267 begin
12268 Set_Etype (N, Typ);
12269 end Resolve_Target_Name;
12271 -----------------------------
12272 -- Resolve_Type_Conversion --
12273 -----------------------------
12275 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
12276 Conv_OK : constant Boolean := Conversion_OK (N);
12277 Operand : constant Node_Id := Expression (N);
12278 Operand_Typ : constant Entity_Id := Etype (Operand);
12279 Target_Typ : constant Entity_Id := Etype (N);
12280 Rop : Node_Id;
12281 Orig_N : Node_Id;
12282 Orig_T : Node_Id;
12284 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
12285 -- Set to False to suppress cases where we want to suppress the test
12286 -- for redundancy to avoid possible false positives on this warning.
12288 begin
12289 if not Conv_OK
12290 and then not Valid_Conversion (N, Target_Typ, Operand)
12291 then
12292 return;
12293 end if;
12295 -- If the Operand Etype is Universal_Fixed, then the conversion is
12296 -- never redundant. We need this check because by the time we have
12297 -- finished the rather complex transformation, the conversion looks
12298 -- redundant when it is not.
12300 if Operand_Typ = Universal_Fixed then
12301 Test_Redundant := False;
12303 -- If the operand is marked as Any_Fixed, then special processing is
12304 -- required. This is also a case where we suppress the test for a
12305 -- redundant conversion, since most certainly it is not redundant.
12307 elsif Operand_Typ = Any_Fixed then
12308 Test_Redundant := False;
12310 -- Mixed-mode operation involving a literal. Context must be a fixed
12311 -- type which is applied to the literal subsequently.
12313 -- Multiplication and division involving two fixed type operands must
12314 -- yield a universal real because the result is computed in arbitrary
12315 -- precision.
12317 if Is_Fixed_Point_Type (Typ)
12318 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
12319 and then Etype (Left_Opnd (Operand)) = Any_Fixed
12320 and then Etype (Right_Opnd (Operand)) = Any_Fixed
12321 then
12322 Set_Etype (Operand, Universal_Real);
12324 elsif Is_Numeric_Type (Typ)
12325 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
12326 and then (Etype (Right_Opnd (Operand)) = Universal_Real
12327 or else
12328 Etype (Left_Opnd (Operand)) = Universal_Real)
12329 then
12330 -- Return if expression is ambiguous
12332 if Unique_Fixed_Point_Type (N) = Any_Type then
12333 return;
12335 -- If nothing else, the available fixed type is Duration
12337 else
12338 Set_Etype (Operand, Standard_Duration);
12339 end if;
12341 -- Resolve the real operand with largest available precision
12343 if Etype (Right_Opnd (Operand)) = Universal_Real then
12344 Rop := New_Copy_Tree (Right_Opnd (Operand));
12345 else
12346 Rop := New_Copy_Tree (Left_Opnd (Operand));
12347 end if;
12349 Resolve (Rop, Universal_Real);
12351 -- If the operand is a literal (it could be a non-static and
12352 -- illegal exponentiation) check whether the use of Duration
12353 -- is potentially inaccurate.
12355 if Nkind (Rop) = N_Real_Literal
12356 and then Realval (Rop) /= Ureal_0
12357 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
12358 then
12359 Error_Msg_N
12360 ("??universal real operand can only "
12361 & "be interpreted as Duration!", Rop);
12362 Error_Msg_N
12363 ("\??precision will be lost in the conversion!", Rop);
12364 end if;
12366 elsif Is_Numeric_Type (Typ)
12367 and then Nkind (Operand) in N_Op
12368 and then Unique_Fixed_Point_Type (N) /= Any_Type
12369 then
12370 Set_Etype (Operand, Standard_Duration);
12372 else
12373 Error_Msg_N ("invalid context for mixed mode operation", N);
12374 Set_Etype (Operand, Any_Type);
12375 return;
12376 end if;
12377 end if;
12379 Resolve (Operand);
12381 Analyze_Dimension (N);
12383 -- Note: we do the Eval_Type_Conversion call before applying the
12384 -- required checks for a subtype conversion. This is important, since
12385 -- both are prepared under certain circumstances to change the type
12386 -- conversion to a constraint error node, but in the case of
12387 -- Eval_Type_Conversion this may reflect an illegality in the static
12388 -- case, and we would miss the illegality (getting only a warning
12389 -- message), if we applied the type conversion checks first.
12391 Eval_Type_Conversion (N);
12393 -- Even when evaluation is not possible, we may be able to simplify the
12394 -- conversion or its expression. This needs to be done before applying
12395 -- checks, since otherwise the checks may use the original expression
12396 -- and defeat the simplifications. This is specifically the case for
12397 -- elimination of the floating-point Truncation attribute in
12398 -- float-to-int conversions.
12400 Simplify_Type_Conversion (N);
12402 -- If after evaluation we still have a type conversion, then we may need
12403 -- to apply checks required for a subtype conversion. But skip them if
12404 -- universal fixed operands are involved, since range checks are handled
12405 -- separately for these cases, after the expansion done by Exp_Fixd.
12407 if Nkind (N) = N_Type_Conversion
12408 and then not Is_Generic_Type (Root_Type (Target_Typ))
12409 and then Target_Typ /= Universal_Fixed
12410 and then Etype (Operand) /= Universal_Fixed
12411 then
12412 Apply_Type_Conversion_Checks (N);
12413 end if;
12415 -- Issue warning for conversion of simple object to its own type. We
12416 -- have to test the original nodes, since they may have been rewritten
12417 -- by various optimizations.
12419 Orig_N := Original_Node (N);
12421 -- Here we test for a redundant conversion if the warning mode is
12422 -- active (and was not locally reset), and we have a type conversion
12423 -- from source not appearing in a generic instance.
12425 if Test_Redundant
12426 and then Nkind (Orig_N) = N_Type_Conversion
12427 and then Comes_From_Source (Orig_N)
12428 and then not In_Instance
12429 then
12430 Orig_N := Original_Node (Expression (Orig_N));
12431 Orig_T := Target_Typ;
12433 -- If the node is part of a larger expression, the Target_Type
12434 -- may not be the original type of the node if the context is a
12435 -- condition. Recover original type to see if conversion is needed.
12437 if Is_Boolean_Type (Orig_T)
12438 and then Nkind (Parent (N)) in N_Op
12439 then
12440 Orig_T := Etype (Parent (N));
12441 end if;
12443 -- If we have an entity name, then give the warning if the entity
12444 -- is the right type, or if it is a loop parameter covered by the
12445 -- original type (that's needed because loop parameters have an
12446 -- odd subtype coming from the bounds).
12448 if (Is_Entity_Name (Orig_N)
12449 and then Present (Entity (Orig_N))
12450 and then
12451 (Etype (Entity (Orig_N)) = Orig_T
12452 or else
12453 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
12454 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
12456 -- If not an entity, then type of expression must match
12458 or else Etype (Orig_N) = Orig_T
12459 then
12460 -- One more check, do not give warning if the analyzed conversion
12461 -- has an expression with non-static bounds, and the bounds of the
12462 -- target are static. This avoids junk warnings in cases where the
12463 -- conversion is necessary to establish staticness, for example in
12464 -- a case statement.
12466 if not Is_OK_Static_Subtype (Operand_Typ)
12467 and then Is_OK_Static_Subtype (Target_Typ)
12468 then
12469 null;
12471 -- Never give a warning if the operand is a conditional expression
12472 -- because RM 4.5.7(10/3) forces its type to be the target type.
12474 elsif Nkind (Orig_N) in N_Case_Expression | N_If_Expression then
12475 null;
12477 -- Finally, if this type conversion occurs in a context requiring
12478 -- a prefix, and the expression is a qualified expression then the
12479 -- type conversion is not redundant, since a qualified expression
12480 -- is not a prefix, whereas a type conversion is. For example, "X
12481 -- := T'(Funx(...)).Y;" is illegal because a selected component
12482 -- requires a prefix, but a type conversion makes it legal: "X :=
12483 -- T(T'(Funx(...))).Y;"
12485 -- In Ada 2012, a qualified expression is a name, so this idiom is
12486 -- no longer needed, but we still suppress the warning because it
12487 -- seems unfriendly for warnings to pop up when you switch to the
12488 -- newer language version.
12490 elsif Nkind (Orig_N) = N_Qualified_Expression
12491 and then Nkind (Parent (N)) in N_Attribute_Reference
12492 | N_Indexed_Component
12493 | N_Selected_Component
12494 | N_Slice
12495 | N_Explicit_Dereference
12496 then
12497 null;
12499 -- Never warn on conversion to Long_Long_Integer'Base since
12500 -- that is most likely an artifact of the extended overflow
12501 -- checking and comes from complex expanded code.
12503 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
12504 null;
12506 -- Do not warn on conversion to class-wide type on helpers of
12507 -- class-wide preconditions because in this context the warning
12508 -- would be spurious (since the class-wide precondition has been
12509 -- installed in the return statement of the helper, which has a
12510 -- class-wide formal type instead of a regular tagged type).
12512 elsif Is_Class_Wide_Type (Orig_T)
12513 and then Is_Subprogram_Or_Generic_Subprogram (Current_Scope)
12514 and then Present (Class_Preconditions_Subprogram (Current_Scope))
12515 then
12516 null;
12518 -- Here we give the redundant conversion warning. If it is an
12519 -- entity, give the name of the entity in the message. If not,
12520 -- just mention the expression.
12522 else
12523 if Is_Entity_Name (Orig_N) then
12524 Error_Msg_Node_2 := Orig_T;
12525 Error_Msg_NE -- CODEFIX
12526 ("?r?redundant conversion, & is of type &!",
12527 N, Entity (Orig_N));
12528 else
12529 Error_Msg_NE
12530 ("?r?redundant conversion, expression is of type&!",
12531 N, Orig_T);
12532 end if;
12533 end if;
12534 end if;
12535 end if;
12537 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
12538 -- No need to perform any interface conversion if the type of the
12539 -- expression coincides with the target type.
12541 if Ada_Version >= Ada_2005
12542 and then Expander_Active
12543 and then Operand_Typ /= Target_Typ
12544 then
12545 declare
12546 Opnd : Entity_Id := Operand_Typ;
12547 Target : Entity_Id := Target_Typ;
12549 begin
12550 -- If the type of the operand is a limited view, use nonlimited
12551 -- view when available. If it is a class-wide type, recover the
12552 -- class-wide type of the nonlimited view.
12554 if From_Limited_With (Opnd)
12555 and then Has_Non_Limited_View (Opnd)
12556 then
12557 Opnd := Non_Limited_View (Opnd);
12558 Set_Etype (Expression (N), Opnd);
12559 end if;
12561 -- It seems that Non_Limited_View should also be applied for
12562 -- Target when it has a limited view, but that leads to missing
12563 -- error checks on interface conversions further below. ???
12565 if Is_Access_Type (Opnd) then
12566 Opnd := Designated_Type (Opnd);
12568 -- If the type of the operand is a limited view, use nonlimited
12569 -- view when available. If it is a class-wide type, recover the
12570 -- class-wide type of the nonlimited view.
12572 if From_Limited_With (Opnd)
12573 and then Has_Non_Limited_View (Opnd)
12574 then
12575 Opnd := Non_Limited_View (Opnd);
12576 end if;
12577 end if;
12579 if Is_Access_Type (Target_Typ) then
12580 Target := Designated_Type (Target);
12582 -- If the target type is a limited view, use nonlimited view
12583 -- when available.
12585 if From_Limited_With (Target)
12586 and then Has_Non_Limited_View (Target)
12587 then
12588 Target := Non_Limited_View (Target);
12589 end if;
12590 end if;
12592 if Opnd = Target then
12593 null;
12595 -- Conversion from interface type
12597 -- It seems that it would be better for the error checks below
12598 -- to be performed as part of Validate_Conversion (and maybe some
12599 -- of the error checks above could be moved as well?). ???
12601 elsif Is_Interface (Opnd) then
12603 -- Ada 2005 (AI-217): Handle entities from limited views
12605 if From_Limited_With (Opnd) then
12606 Error_Msg_Qual_Level := 99;
12607 Error_Msg_NE -- CODEFIX
12608 ("missing WITH clause on package &", N,
12609 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
12610 Error_Msg_N
12611 ("type conversions require visibility of the full view",
12614 elsif From_Limited_With (Target)
12615 and then not
12616 (Is_Access_Type (Target_Typ)
12617 and then Present (Non_Limited_View (Etype (Target))))
12618 then
12619 Error_Msg_Qual_Level := 99;
12620 Error_Msg_NE -- CODEFIX
12621 ("missing WITH clause on package &", N,
12622 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12623 Error_Msg_N
12624 ("type conversions require visibility of the full view",
12627 else
12628 Expand_Interface_Conversion (N);
12629 end if;
12631 -- Conversion to interface type
12633 elsif Is_Interface (Target) then
12634 Expand_Interface_Conversion (N);
12635 end if;
12636 end;
12637 end if;
12639 -- Ada 2012: Once the type conversion is resolved, check whether the
12640 -- operand satisfies a static predicate of the target subtype, if any.
12641 -- In the static expression case, a predicate check failure is an error.
12643 if Has_Predicates (Target_Typ) then
12644 Check_Expression_Against_Static_Predicate
12645 (N, Target_Typ, Static_Failure_Is_Error => True);
12646 end if;
12648 -- If at this stage we have a fixed to integer conversion, make sure the
12649 -- Do_Range_Check flag is set, because such conversions in general need
12650 -- a range check. We only need this if expansion is off, see above why.
12652 if Nkind (N) = N_Type_Conversion
12653 and then not Expander_Active
12654 and then Is_Integer_Type (Target_Typ)
12655 and then Is_Fixed_Point_Type (Operand_Typ)
12656 and then not Range_Checks_Suppressed (Target_Typ)
12657 and then not Range_Checks_Suppressed (Operand_Typ)
12658 then
12659 Set_Do_Range_Check (Operand);
12660 end if;
12661 end Resolve_Type_Conversion;
12663 ----------------------
12664 -- Resolve_Unary_Op --
12665 ----------------------
12667 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12668 B_Typ : constant Entity_Id := Base_Type (Typ);
12669 R : constant Node_Id := Right_Opnd (N);
12670 OK : Boolean;
12671 Lo : Uint;
12672 Hi : Uint;
12674 begin
12675 -- Deal with intrinsic unary operators
12677 if Comes_From_Source (N)
12678 and then Ekind (Entity (N)) = E_Function
12679 and then Is_Imported (Entity (N))
12680 and then Is_Intrinsic_Subprogram (Entity (N))
12681 then
12682 Resolve_Intrinsic_Unary_Operator (N, Typ);
12683 return;
12684 end if;
12686 -- Deal with universal cases
12688 if Is_Universal_Numeric_Type (Etype (R)) then
12689 Check_For_Visible_Operator (N, B_Typ);
12690 end if;
12692 Set_Etype (N, B_Typ);
12693 Resolve (R, B_Typ);
12695 -- Generate warning for negative literal of a modular type, unless it is
12696 -- enclosed directly in a type qualification or a type conversion, as it
12697 -- is likely not what the user intended. We don't issue the warning for
12698 -- the common use of -1 to denote OxFFFF_FFFF...
12700 if Warn_On_Suspicious_Modulus_Value
12701 and then Nkind (N) = N_Op_Minus
12702 and then Nkind (R) = N_Integer_Literal
12703 and then Comes_From_Source (R)
12704 and then Is_Modular_Integer_Type (B_Typ)
12705 and then Nkind (Parent (N)) not in N_Qualified_Expression
12706 | N_Type_Conversion
12707 and then Expr_Value (R) > Uint_1
12708 then
12709 Error_Msg_N
12710 ("?.m?negative literal of modular type is in fact positive", N);
12711 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12712 Error_Msg_Uint_2 := Expr_Value (R);
12713 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12714 Error_Msg_N
12715 ("\if you do, use qualification to avoid this warning", N);
12716 end if;
12718 -- Generate warning for expressions like abs (x mod 2)
12720 if Warn_On_Redundant_Constructs
12721 and then Nkind (N) = N_Op_Abs
12722 then
12723 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12725 if OK and then Hi >= Lo and then Lo >= 0 then
12726 Error_Msg_N -- CODEFIX
12727 ("?r?abs applied to known non-negative value has no effect", N);
12728 end if;
12729 end if;
12731 -- Deal with reference generation
12733 Check_Unset_Reference (R);
12734 Generate_Operator_Reference (N, B_Typ);
12735 Analyze_Dimension (N);
12736 Eval_Unary_Op (N);
12738 -- Set overflow checking bit. Much cleverer code needed here eventually
12739 -- and perhaps the Resolve routines should be separated for the various
12740 -- arithmetic operations, since they will need different processing ???
12742 if Nkind (N) in N_Op then
12743 if not Overflow_Checks_Suppressed (Etype (N)) then
12744 Enable_Overflow_Check (N);
12745 end if;
12746 end if;
12748 -- Generate warning for expressions like -5 mod 3 for integers. No need
12749 -- to worry in the floating-point case, since parens do not affect the
12750 -- result so there is no point in giving in a warning.
12752 declare
12753 Norig : constant Node_Id := Original_Node (N);
12754 Rorig : Node_Id;
12755 Val : Uint;
12756 HB : Uint;
12757 LB : Uint;
12758 Lval : Uint;
12759 Opnd : Node_Id;
12761 begin
12762 if Warn_On_Questionable_Missing_Parens
12763 and then Comes_From_Source (Norig)
12764 and then Is_Integer_Type (Typ)
12765 and then Nkind (Norig) = N_Op_Minus
12766 then
12767 Rorig := Original_Node (Right_Opnd (Norig));
12769 -- We are looking for cases where the right operand is not
12770 -- parenthesized, and is a binary operator, multiply, divide, or
12771 -- mod. These are the cases where the grouping can affect results.
12773 if Paren_Count (Rorig) = 0
12774 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12775 then
12776 -- For mod, we always give the warning, since the value is
12777 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12778 -- -(5 mod 315)). But for the other cases, the only concern is
12779 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12780 -- overflows, but (-2) * 64 does not). So we try to give the
12781 -- message only when overflow is possible.
12783 if Nkind (Rorig) /= N_Op_Mod
12784 and then Compile_Time_Known_Value (R)
12785 then
12786 Val := Expr_Value (R);
12788 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12789 HB := Expr_Value (Type_High_Bound (Typ));
12790 else
12791 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12792 end if;
12794 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12795 LB := Expr_Value (Type_Low_Bound (Typ));
12796 else
12797 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12798 end if;
12800 -- Note that the test below is deliberately excluding the
12801 -- largest negative number, since that is a potentially
12802 -- troublesome case (e.g. -2 * x, where the result is the
12803 -- largest negative integer has an overflow with 2 * x).
12805 if Val > LB and then Val <= HB then
12806 return;
12807 end if;
12808 end if;
12810 -- For the multiplication case, the only case we have to worry
12811 -- about is when (-a)*b is exactly the largest negative number
12812 -- so that -(a*b) can cause overflow. This can only happen if
12813 -- a is a power of 2, and more generally if any operand is a
12814 -- constant that is not a power of 2, then the parentheses
12815 -- cannot affect whether overflow occurs. We only bother to
12816 -- test the left most operand
12818 -- Loop looking at left operands for one that has known value
12820 Opnd := Rorig;
12821 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12822 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12823 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12825 -- Operand value of 0 or 1 skips warning
12827 if Lval <= 1 then
12828 return;
12830 -- Otherwise check power of 2, if power of 2, warn, if
12831 -- anything else, skip warning.
12833 else
12834 while Lval /= 2 loop
12835 if Lval mod 2 = 1 then
12836 return;
12837 else
12838 Lval := Lval / 2;
12839 end if;
12840 end loop;
12842 exit Opnd_Loop;
12843 end if;
12844 end if;
12846 -- Keep looking at left operands
12848 Opnd := Left_Opnd (Opnd);
12849 end loop Opnd_Loop;
12851 -- For rem or "/" we can only have a problematic situation
12852 -- if the divisor has a value of minus one or one. Otherwise
12853 -- overflow is impossible (divisor > 1) or we have a case of
12854 -- division by zero in any case.
12856 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12857 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12858 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12859 then
12860 return;
12861 end if;
12863 -- If we fall through warning should be issued
12865 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12867 Error_Msg_N
12868 ("??unary minus expression should be parenthesized here!", N);
12869 end if;
12870 end if;
12871 end;
12872 end Resolve_Unary_Op;
12874 ----------------------------------
12875 -- Resolve_Unchecked_Expression --
12876 ----------------------------------
12878 procedure Resolve_Unchecked_Expression
12879 (N : Node_Id;
12880 Typ : Entity_Id)
12882 begin
12883 Resolve (Expression (N), Typ, Suppress => All_Checks);
12884 Set_Etype (N, Typ);
12885 end Resolve_Unchecked_Expression;
12887 ---------------------------------------
12888 -- Resolve_Unchecked_Type_Conversion --
12889 ---------------------------------------
12891 procedure Resolve_Unchecked_Type_Conversion
12892 (N : Node_Id;
12893 Typ : Entity_Id)
12895 pragma Warnings (Off, Typ);
12897 Operand : constant Node_Id := Expression (N);
12898 Opnd_Type : constant Entity_Id := Etype (Operand);
12900 begin
12901 -- Resolve operand using its own type
12903 Resolve (Operand, Opnd_Type);
12905 -- If the expression is a conversion to universal integer of an
12906 -- an expression with an integer type, then we can eliminate the
12907 -- intermediate conversion to universal integer.
12909 if Nkind (Operand) = N_Type_Conversion
12910 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12911 and then Is_Integer_Type (Etype (Expression (Operand)))
12912 then
12913 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12914 Analyze_And_Resolve (Operand);
12915 end if;
12917 -- In an inlined context, the unchecked conversion may be applied
12918 -- to a literal, in which case its type is the type of the context.
12919 -- (In other contexts conversions cannot apply to literals).
12921 if In_Inlined_Body
12922 and then (Opnd_Type = Any_Character or else
12923 Opnd_Type = Any_Integer or else
12924 Opnd_Type = Any_Real)
12925 then
12926 Set_Etype (Operand, Typ);
12927 end if;
12929 Analyze_Dimension (N);
12930 Eval_Unchecked_Conversion (N);
12931 end Resolve_Unchecked_Type_Conversion;
12933 ------------------------------
12934 -- Rewrite_Operator_As_Call --
12935 ------------------------------
12937 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12938 Loc : constant Source_Ptr := Sloc (N);
12939 Actuals : constant List_Id := New_List;
12940 New_N : Node_Id;
12942 begin
12943 if Nkind (N) in N_Binary_Op then
12944 Append (Left_Opnd (N), Actuals);
12945 end if;
12947 Append (Right_Opnd (N), Actuals);
12949 New_N :=
12950 Make_Function_Call (Sloc => Loc,
12951 Name => New_Occurrence_Of (Nam, Loc),
12952 Parameter_Associations => Actuals);
12954 Preserve_Comes_From_Source (New_N, N);
12955 Preserve_Comes_From_Source (Name (New_N), N);
12956 Rewrite (N, New_N);
12957 Set_Etype (N, Etype (Nam));
12958 end Rewrite_Operator_As_Call;
12960 ------------------------------
12961 -- Rewrite_Renamed_Operator --
12962 ------------------------------
12964 procedure Rewrite_Renamed_Operator
12965 (N : Node_Id;
12966 Op : Entity_Id;
12967 Typ : Entity_Id)
12969 Nam : constant Name_Id := Chars (Op);
12970 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12971 Op_Node : Node_Id;
12973 begin
12974 -- Do not perform this transformation within a pre/postcondition,
12975 -- because the expression will be reanalyzed, and the transformation
12976 -- might affect the visibility of the operator, e.g. in an instance.
12977 -- Note that fully analyzed and expanded pre/postconditions appear as
12978 -- pragma Check equivalents.
12980 if In_Pre_Post_Condition (N) then
12981 return;
12982 end if;
12984 -- Likewise when an expression function is being preanalyzed, since the
12985 -- expression will be reanalyzed as part of the generated body.
12987 if In_Spec_Expression then
12988 declare
12989 S : constant Entity_Id := Current_Scope_No_Loops;
12990 begin
12991 if Ekind (S) = E_Function
12992 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12993 N_Expression_Function
12994 then
12995 return;
12996 end if;
12997 end;
12998 end if;
13000 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
13001 Set_Chars (Op_Node, Nam);
13002 Set_Etype (Op_Node, Etype (N));
13003 Set_Entity (Op_Node, Op);
13004 Set_Right_Opnd (Op_Node, Right_Opnd (N));
13006 if Is_Binary then
13007 Set_Left_Opnd (Op_Node, Left_Opnd (N));
13008 end if;
13010 -- Indicate that both the original entity and its renaming are
13011 -- referenced at this point.
13013 Generate_Reference (Entity (N), N);
13014 Generate_Reference (Op, N);
13016 Rewrite (N, Op_Node);
13018 -- If the context type is private, add the appropriate conversions so
13019 -- that the operator is applied to the full view. This is done in the
13020 -- routines that resolve intrinsic operators.
13022 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
13023 case Nkind (N) is
13024 when N_Op_Add
13025 | N_Op_Divide
13026 | N_Op_Expon
13027 | N_Op_Mod
13028 | N_Op_Multiply
13029 | N_Op_Rem
13030 | N_Op_Subtract
13032 Resolve_Intrinsic_Operator (N, Typ);
13034 when N_Op_Abs
13035 | N_Op_Minus
13036 | N_Op_Plus
13038 Resolve_Intrinsic_Unary_Operator (N, Typ);
13040 when others =>
13041 Resolve (N, Typ);
13042 end case;
13043 end if;
13044 end Rewrite_Renamed_Operator;
13046 -----------------------
13047 -- Set_Slice_Subtype --
13048 -----------------------
13050 -- Build an implicit subtype declaration to represent the type delivered by
13051 -- the slice. This is an abbreviated version of an array subtype. We define
13052 -- an index subtype for the slice, using either the subtype name or the
13053 -- discrete range of the slice. To be consistent with index usage elsewhere
13054 -- we create a list header to hold the single index. This list is not
13055 -- otherwise attached to the syntax tree.
13057 procedure Set_Slice_Subtype (N : Node_Id) is
13058 Loc : constant Source_Ptr := Sloc (N);
13059 Index_List : constant List_Id := New_List;
13060 Index : Node_Id;
13061 Index_Subtype : Entity_Id;
13062 Index_Type : Entity_Id;
13063 Slice_Subtype : Entity_Id;
13064 Drange : constant Node_Id := Discrete_Range (N);
13066 begin
13067 Index_Type := Base_Type (Etype (Drange));
13069 if Is_Entity_Name (Drange) then
13070 Index_Subtype := Entity (Drange);
13072 else
13073 -- We force the evaluation of a range. This is definitely needed in
13074 -- the renamed case, and seems safer to do unconditionally. Note in
13075 -- any case that since we will create and insert an Itype referring
13076 -- to this range, we must make sure any side effect removal actions
13077 -- are inserted before the Itype definition.
13079 if Nkind (Drange) = N_Range then
13080 Force_Evaluation (Low_Bound (Drange));
13081 Force_Evaluation (High_Bound (Drange));
13083 -- If the discrete range is given by a subtype indication, the
13084 -- type of the slice is the base of the subtype mark.
13086 elsif Nkind (Drange) = N_Subtype_Indication then
13087 declare
13088 R : constant Node_Id := Range_Expression (Constraint (Drange));
13089 begin
13090 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
13091 Force_Evaluation (Low_Bound (R));
13092 Force_Evaluation (High_Bound (R));
13093 end;
13094 end if;
13096 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13098 -- Take a new copy of Drange (where bounds have been rewritten to
13099 -- reference side-effect-free names). Using a separate tree ensures
13100 -- that further expansion (e.g. while rewriting a slice assignment
13101 -- into a FOR loop) does not attempt to remove side effects on the
13102 -- bounds again (which would cause the bounds in the index subtype
13103 -- definition to refer to temporaries before they are defined) (the
13104 -- reason is that some names are considered side effect free here
13105 -- for the subtype, but not in the context of a loop iteration
13106 -- scheme).
13108 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
13109 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
13110 Set_Etype (Index_Subtype, Index_Type);
13111 Set_Size_Info (Index_Subtype, Index_Type);
13112 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13113 Set_Is_Constrained (Index_Subtype);
13114 end if;
13116 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
13118 Index := New_Occurrence_Of (Index_Subtype, Loc);
13119 Set_Etype (Index, Index_Subtype);
13120 Append (Index, Index_List);
13122 Set_First_Index (Slice_Subtype, Index);
13123 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
13124 Set_Is_Constrained (Slice_Subtype, True);
13126 Check_Compile_Time_Size (Slice_Subtype);
13128 -- The Etype of the existing Slice node is reset to this slice subtype.
13129 -- Its bounds are obtained from its first index.
13131 Set_Etype (N, Slice_Subtype);
13133 -- For bit-packed slice subtypes, freeze immediately (except in the case
13134 -- of being in a "spec expression" where we never freeze when we first
13135 -- see the expression).
13137 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
13138 Freeze_Itype (Slice_Subtype, N);
13140 -- For all other cases insert an itype reference in the slice's actions
13141 -- so that the itype is frozen at the proper place in the tree (i.e. at
13142 -- the point where actions for the slice are analyzed). Note that this
13143 -- is different from freezing the itype immediately, which might be
13144 -- premature (e.g. if the slice is within a transient scope). This needs
13145 -- to be done only if expansion is enabled.
13147 elsif Expander_Active then
13148 Ensure_Defined (Typ => Slice_Subtype, N => N);
13149 end if;
13150 end Set_Slice_Subtype;
13152 --------------------------------
13153 -- Set_String_Literal_Subtype --
13154 --------------------------------
13156 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
13157 Loc : constant Source_Ptr := Sloc (N);
13158 Low_Bound : constant Node_Id :=
13159 Type_Low_Bound (Etype (First_Index (Typ)));
13160 Length : constant Nat :=
13161 (case Nkind (N) is
13162 when N_String_Literal => String_Length (Strval (N)),
13163 -- Sinput.Source_Last points to an EOF character that's not in the
13164 -- original file and we do not include that character.
13165 when N_External_Initializer => Nat (
13166 Source_Last (File_Index (N)) - Source_First (File_Index (N))),
13167 when others => 0);
13168 Subtype_Id : Entity_Id;
13170 begin
13171 if Nkind (N) not in N_String_Literal | N_External_Initializer then
13172 return;
13173 end if;
13175 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
13176 Set_String_Literal_Length (Subtype_Id, UI_From_Int (Length));
13177 Set_Etype (Subtype_Id, Base_Type (Typ));
13178 Set_Is_Constrained (Subtype_Id);
13179 Set_Etype (N, Subtype_Id);
13181 -- The low bound is set from the low bound of the corresponding index
13182 -- type. Note that we do not store the high bound in the string literal
13183 -- subtype, but it can be deduced if necessary from the length and the
13184 -- low bound.
13186 if Is_OK_Static_Expression (Low_Bound) then
13187 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
13189 -- If the lower bound is not static we create a range for the string
13190 -- literal, using the index type and the known length of the literal.
13191 -- If the length is 1, then the upper bound is set to a mere copy of
13192 -- the lower bound; or else, if the index type is a signed integer,
13193 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
13194 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
13196 else
13197 declare
13198 Index_List : constant List_Id := New_List;
13199 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
13200 Array_Subtype : Entity_Id;
13201 Drange : Node_Id;
13202 High_Bound : Node_Id;
13203 Index : Node_Id;
13204 Index_Subtype : Entity_Id;
13206 begin
13207 if Length = 1 then
13208 High_Bound := New_Copy_Tree (Low_Bound);
13210 elsif Is_Signed_Integer_Type (Index_Type) then
13211 High_Bound :=
13212 Make_Op_Add (Loc,
13213 Left_Opnd => New_Copy_Tree (Low_Bound),
13214 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
13216 else
13217 High_Bound :=
13218 Make_Attribute_Reference (Loc,
13219 Attribute_Name => Name_Val,
13220 Prefix =>
13221 New_Occurrence_Of (Index_Type, Loc),
13222 Expressions => New_List (
13223 Make_Op_Add (Loc,
13224 Left_Opnd =>
13225 Make_Attribute_Reference (Loc,
13226 Attribute_Name => Name_Pos,
13227 Prefix =>
13228 New_Occurrence_Of (Index_Type, Loc),
13229 Expressions =>
13230 New_List (New_Copy_Tree (Low_Bound))),
13231 Right_Opnd =>
13232 Make_Integer_Literal (Loc, Length - 1))));
13233 end if;
13235 if Is_Integer_Type (Index_Type) then
13236 Set_String_Literal_Low_Bound
13237 (Subtype_Id, Make_Integer_Literal (Loc, 1));
13239 else
13240 -- If the index type is an enumeration type, build bounds
13241 -- expression with attributes.
13243 Set_String_Literal_Low_Bound
13244 (Subtype_Id,
13245 Make_Attribute_Reference (Loc,
13246 Attribute_Name => Name_First,
13247 Prefix =>
13248 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
13249 end if;
13251 Analyze_And_Resolve
13252 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
13254 -- Build bona fide subtype for the string, and wrap it in an
13255 -- unchecked conversion, because the back end expects the
13256 -- String_Literal_Subtype to have a static lower bound.
13258 Index_Subtype :=
13259 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13260 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
13261 Set_Scalar_Range (Index_Subtype, Drange);
13262 Set_Parent (Drange, N);
13263 Analyze_And_Resolve (Drange, Index_Type);
13265 -- In this context, the Index_Type may already have a constraint,
13266 -- so use common base type on string subtype. The base type may
13267 -- be used when generating attributes of the string, for example
13268 -- in the context of a slice assignment.
13270 Set_Etype (Index_Subtype, Base_Type (Index_Type));
13271 Set_Size_Info (Index_Subtype, Index_Type);
13272 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13274 Array_Subtype := Create_Itype (E_Array_Subtype, N);
13276 Index := New_Occurrence_Of (Index_Subtype, Loc);
13277 Set_Etype (Index, Index_Subtype);
13278 Append (Index, Index_List);
13280 Set_First_Index (Array_Subtype, Index);
13281 Set_Etype (Array_Subtype, Base_Type (Typ));
13282 Set_Is_Constrained (Array_Subtype, True);
13284 Rewrite (N, Unchecked_Convert_To (Array_Subtype, N));
13285 Set_Etype (N, Array_Subtype);
13286 end;
13287 end if;
13288 end Set_String_Literal_Subtype;
13290 ------------------------------
13291 -- Simplify_Type_Conversion --
13292 ------------------------------
13294 procedure Simplify_Type_Conversion (N : Node_Id) is
13295 begin
13296 if Nkind (N) = N_Type_Conversion then
13297 declare
13298 Operand : constant Node_Id := Expression (N);
13299 Target_Typ : constant Entity_Id := Etype (N);
13300 Opnd_Typ : constant Entity_Id := Etype (Operand);
13302 begin
13303 -- Special processing if the conversion is the expression of a
13304 -- Rounding or Truncation attribute reference. In this case we
13305 -- replace:
13307 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
13309 -- by
13311 -- ityp (x)
13313 -- with the Float_Truncate flag set to False or True respectively,
13314 -- which is more efficient. We reuse Rounding for Machine_Rounding
13315 -- as System.Fat_Gen, which is a permissible behavior.
13317 if Is_Floating_Point_Type (Opnd_Typ)
13318 and then
13319 (Is_Integer_Type (Target_Typ)
13320 or else (Is_Fixed_Point_Type (Target_Typ)
13321 and then Conversion_OK (N)))
13322 and then Nkind (Operand) = N_Attribute_Reference
13323 and then Attribute_Name (Operand) in Name_Rounding
13324 | Name_Machine_Rounding
13325 | Name_Truncation
13326 then
13327 declare
13328 Truncate : constant Boolean :=
13329 Attribute_Name (Operand) = Name_Truncation;
13330 begin
13331 Rewrite (Operand,
13332 Relocate_Node (First (Expressions (Operand))));
13333 Set_Float_Truncate (N, Truncate);
13334 end;
13336 -- Special processing for the conversion of an integer literal to
13337 -- a dynamic type: we first convert the literal to the root type
13338 -- and then convert the result to the target type, the goal being
13339 -- to avoid doing range checks in universal integer.
13341 elsif Is_Integer_Type (Target_Typ)
13342 and then not Is_Generic_Type (Root_Type (Target_Typ))
13343 and then Nkind (Operand) = N_Integer_Literal
13344 and then Opnd_Typ = Universal_Integer
13345 then
13346 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
13347 Analyze_And_Resolve (Operand);
13349 -- If the expression is a conversion to universal integer of an
13350 -- an expression with an integer type, then we can eliminate the
13351 -- intermediate conversion to universal integer.
13353 elsif Nkind (Operand) = N_Type_Conversion
13354 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
13355 and then Is_Integer_Type (Etype (Expression (Operand)))
13356 then
13357 Rewrite (Operand, Relocate_Node (Expression (Operand)));
13358 Analyze_And_Resolve (Operand);
13359 end if;
13360 end;
13361 end if;
13362 end Simplify_Type_Conversion;
13364 ------------------------------
13365 -- Try_User_Defined_Literal --
13366 ------------------------------
13368 function Try_User_Defined_Literal
13369 (N : Node_Id;
13370 Typ : Entity_Id) return Boolean
13372 begin
13373 if Has_Applicable_User_Defined_Literal (N, Typ) then
13374 return True;
13376 elsif Nkind (N) = N_If_Expression then
13377 -- Both dependent expressions must have the same type as the context
13379 declare
13380 Condition : constant Node_Id := First (Expressions (N));
13381 Then_Expr : constant Node_Id := Next (Condition);
13382 Else_Expr : constant Node_Id := Next (Then_Expr);
13384 begin
13385 if Has_Applicable_User_Defined_Literal (Then_Expr, Typ) then
13386 Resolve (Else_Expr, Typ);
13387 Analyze_And_Resolve (N, Typ);
13388 return True;
13390 elsif Has_Applicable_User_Defined_Literal (Else_Expr, Typ) then
13391 Resolve (Then_Expr, Typ);
13392 Analyze_And_Resolve (N, Typ);
13393 return True;
13394 end if;
13395 end;
13397 elsif Nkind (N) = N_Case_Expression then
13398 -- All dependent expressions must have the same type as the context
13400 declare
13401 Alt : Node_Id;
13403 begin
13404 Alt := First (Alternatives (N));
13406 while Present (Alt) loop
13407 if Has_Applicable_User_Defined_Literal (Expression (Alt), Typ)
13408 then
13409 declare
13410 Other_Alt : Node_Id;
13412 begin
13413 Other_Alt := First (Alternatives (N));
13415 while Present (Other_Alt) loop
13416 if Other_Alt /= Alt then
13417 Resolve (Expression (Other_Alt), Typ);
13418 end if;
13420 Next (Other_Alt);
13421 end loop;
13423 Analyze_And_Resolve (N, Typ);
13424 return True;
13425 end;
13426 end if;
13428 Next (Alt);
13429 end loop;
13430 end;
13431 end if;
13433 return False;
13434 end Try_User_Defined_Literal;
13436 -------------------------------------------
13437 -- Try_User_Defined_Literal_For_Operator --
13438 -------------------------------------------
13440 function Try_User_Defined_Literal_For_Operator
13441 (N : Node_Id;
13442 Typ : Entity_Id) return Boolean
13444 begin
13445 if Nkind (N) in N_Op_Add
13446 | N_Op_Divide
13447 | N_Op_Mod
13448 | N_Op_Multiply
13449 | N_Op_Rem
13450 | N_Op_Subtract
13451 then
13452 -- Both operands must have the same type as the context
13453 -- (ignoring for now fixed-point and exponentiation ops).
13455 if Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13456 or else (Nkind (Left_Opnd (N)) in N_Op
13457 and then Covers (Typ, Etype (Right_Opnd (N))))
13458 then
13459 Resolve (Left_Opnd (N), Typ);
13460 Analyze_And_Resolve (N, Typ);
13461 return True;
13463 elsif Has_Applicable_User_Defined_Literal (Left_Opnd (N), Typ)
13464 or else (Nkind (Right_Opnd (N)) in N_Op
13465 and then Covers (Typ, Etype (Left_Opnd (N))))
13466 then
13467 Resolve (Right_Opnd (N), Typ);
13468 Analyze_And_Resolve (N, Typ);
13469 return True;
13470 end if;
13472 elsif Nkind (N) in N_Binary_Op then
13473 -- For other binary operators the context does not impose a type on
13474 -- the operands, but their types must match.
13476 if Nkind (Left_Opnd (N))
13477 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13478 and then
13479 Has_Applicable_User_Defined_Literal
13480 (Right_Opnd (N), Etype (Left_Opnd (N)))
13481 then
13482 Analyze_And_Resolve (N, Typ);
13483 return True;
13485 elsif Nkind (Right_Opnd (N))
13486 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13487 and then
13488 Has_Applicable_User_Defined_Literal
13489 (Left_Opnd (N), Etype (Right_Opnd (N)))
13490 then
13491 Analyze_And_Resolve (N, Typ);
13492 return True;
13493 end if;
13495 elsif Nkind (N) in N_Unary_Op
13496 and then Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13497 then
13498 Analyze_And_Resolve (N, Typ);
13499 return True;
13500 end if;
13502 return False;
13503 end Try_User_Defined_Literal_For_Operator;
13505 -----------------------------
13506 -- Unique_Fixed_Point_Type --
13507 -----------------------------
13509 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
13510 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
13511 -- Give error messages for true ambiguity. Messages are posted on node
13512 -- N, and entities T1, T2 are the possible interpretations.
13514 -----------------------
13515 -- Fixed_Point_Error --
13516 -----------------------
13518 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
13519 begin
13520 Error_Msg_N ("ambiguous universal_fixed_expression", N);
13521 Error_Msg_NE ("\\possible interpretation as}", N, T1);
13522 Error_Msg_NE ("\\possible interpretation as}", N, T2);
13523 end Fixed_Point_Error;
13525 -- Local variables
13527 ErrN : Node_Id;
13528 Item : Node_Id;
13529 Scop : Entity_Id;
13530 T1 : Entity_Id;
13531 T2 : Entity_Id;
13533 -- Start of processing for Unique_Fixed_Point_Type
13535 begin
13536 -- The operations on Duration are visible, so Duration is always a
13537 -- possible interpretation.
13539 T1 := Standard_Duration;
13541 -- Look for fixed-point types in enclosing scopes
13543 Scop := Current_Scope;
13544 while Scop /= Standard_Standard loop
13545 T2 := First_Entity (Scop);
13546 while Present (T2) loop
13547 if Is_Fixed_Point_Type (T2)
13548 and then Current_Entity (T2) = T2
13549 and then Scope (Base_Type (T2)) = Scop
13550 then
13551 if Present (T1) then
13552 Fixed_Point_Error (T1, T2);
13553 return Any_Type;
13554 else
13555 T1 := T2;
13556 end if;
13557 end if;
13559 Next_Entity (T2);
13560 end loop;
13562 Scop := Scope (Scop);
13563 end loop;
13565 -- Look for visible fixed type declarations in the context
13567 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
13568 while Present (Item) loop
13569 if Nkind (Item) = N_With_Clause then
13570 Scop := Entity (Name (Item));
13571 T2 := First_Entity (Scop);
13572 while Present (T2) loop
13573 if Is_Fixed_Point_Type (T2)
13574 and then Scope (Base_Type (T2)) = Scop
13575 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
13576 then
13577 if Present (T1) then
13578 Fixed_Point_Error (T1, T2);
13579 return Any_Type;
13580 else
13581 T1 := T2;
13582 end if;
13583 end if;
13585 Next_Entity (T2);
13586 end loop;
13587 end if;
13589 Next (Item);
13590 end loop;
13592 if Nkind (N) = N_Real_Literal then
13593 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
13595 else
13596 -- When the context is a type conversion, issue the warning on the
13597 -- expression of the conversion because it is the actual operation.
13599 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
13600 ErrN := Expression (N);
13601 else
13602 ErrN := N;
13603 end if;
13605 Error_Msg_NE
13606 ("??universal_fixed expression interpreted as }!", ErrN, T1);
13607 end if;
13609 return T1;
13610 end Unique_Fixed_Point_Type;
13612 ----------------------
13613 -- Valid_Conversion --
13614 ----------------------
13616 function Valid_Conversion
13617 (N : Node_Id;
13618 Target : Entity_Id;
13619 Operand : Node_Id;
13620 Report_Errs : Boolean := True) return Boolean
13622 Target_Type : constant Entity_Id := Base_Type (Target);
13623 Opnd_Type : Entity_Id := Etype (Operand);
13624 Inc_Ancestor : Entity_Id;
13626 function Conversion_Check
13627 (Valid : Boolean;
13628 Msg : String) return Boolean;
13629 -- Little routine to post Msg if Valid is False, returns Valid value
13631 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
13632 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
13634 procedure Conversion_Error_NE
13635 (Msg : String;
13636 N : Node_Or_Entity_Id;
13637 E : Node_Or_Entity_Id);
13638 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
13640 function In_Instance_Code return Boolean;
13641 -- Return True if expression is within an instance but is not in one of
13642 -- the actuals of the instantiation. Type conversions within an instance
13643 -- are not rechecked because type visibility may lead to spurious errors
13644 -- but conversions in an actual for a formal object must be checked.
13646 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13647 (Expr : Node_Id) return Boolean;
13648 -- Implicit anonymous-to-named access type conversions are not allowed
13649 -- if the "statically deeper than" relationship does not apply to the
13650 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
13651 -- We deal with most such cases elsewhere so that we can emit more
13652 -- specific error messages (e.g., if the operand is an access parameter
13653 -- or a saooaaat (stand-alone object of an anonymous access type)), but
13654 -- here is where we catch the case where the operand is an access
13655 -- discriminant selected from a dereference of another such "bad"
13656 -- conversion argument.
13658 function Valid_Tagged_Conversion
13659 (Target_Type : Entity_Id;
13660 Opnd_Type : Entity_Id) return Boolean;
13661 -- Specifically test for validity of tagged conversions
13663 function Valid_Array_Conversion return Boolean;
13664 -- Check index and component conformance, and accessibility levels if
13665 -- the component types are anonymous access types (Ada 2005).
13667 ----------------------
13668 -- Conversion_Check --
13669 ----------------------
13671 function Conversion_Check
13672 (Valid : Boolean;
13673 Msg : String) return Boolean
13675 begin
13676 if not Valid
13678 -- A generic unit has already been analyzed and we have verified
13679 -- that a particular conversion is OK in that context. Since the
13680 -- instance is reanalyzed without relying on the relationships
13681 -- established during the analysis of the generic, it is possible
13682 -- to end up with inconsistent views of private types. Do not emit
13683 -- the error message in such cases. The rest of the machinery in
13684 -- Valid_Conversion still ensures the proper compatibility of
13685 -- target and operand types.
13687 and then not In_Instance_Code
13688 then
13689 Conversion_Error_N (Msg, Operand);
13690 end if;
13692 return Valid;
13693 end Conversion_Check;
13695 ------------------------
13696 -- Conversion_Error_N --
13697 ------------------------
13699 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
13700 begin
13701 if Report_Errs then
13702 Error_Msg_N (Msg, N);
13703 end if;
13704 end Conversion_Error_N;
13706 -------------------------
13707 -- Conversion_Error_NE --
13708 -------------------------
13710 procedure Conversion_Error_NE
13711 (Msg : String;
13712 N : Node_Or_Entity_Id;
13713 E : Node_Or_Entity_Id)
13715 begin
13716 if Report_Errs then
13717 Error_Msg_NE (Msg, N, E);
13718 end if;
13719 end Conversion_Error_NE;
13721 ----------------------
13722 -- In_Instance_Code --
13723 ----------------------
13725 function In_Instance_Code return Boolean is
13726 Par : Node_Id;
13728 begin
13729 if not In_Instance then
13730 return False;
13732 else
13733 Par := Parent (N);
13734 while Present (Par) loop
13736 -- The expression is part of an actual object if it appears in
13737 -- the generated object declaration in the instance.
13739 if Nkind (Par) = N_Object_Declaration
13740 and then Present (Corresponding_Generic_Association (Par))
13741 then
13742 return False;
13744 else
13745 exit when
13746 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13747 or else Nkind (Par) in N_Subprogram_Call
13748 or else Nkind (Par) in N_Declaration;
13749 end if;
13751 Par := Parent (Par);
13752 end loop;
13754 -- Otherwise the expression appears within the instantiated unit
13756 return True;
13757 end if;
13758 end In_Instance_Code;
13760 --------------------------------------------------
13761 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13762 --------------------------------------------------
13764 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13765 (Expr : Node_Id) return Boolean
13767 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13768 pragma Assert (Is_Access_Type (Exp_Type));
13770 Associated_Node : Node_Id;
13771 Deref_Prefix : Node_Id;
13772 begin
13773 if not Is_Anonymous_Access_Type (Exp_Type) then
13774 return False;
13775 end if;
13777 pragma Assert (Is_Itype (Exp_Type));
13778 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13780 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13781 return False; -- not the type of an access discriminant
13782 end if;
13784 -- return False if Expr not of form <prefix>.all.Some_Component
13786 if Nkind (Expr) /= N_Selected_Component
13787 or else Nkind (Prefix (Expr)) /= N_Explicit_Dereference
13788 then
13789 -- conditional expressions, declare expressions ???
13790 return False;
13791 end if;
13793 Deref_Prefix := Prefix (Prefix (Expr));
13794 Exp_Type := Base_Type (Etype (Deref_Prefix));
13796 -- The "statically deeper relationship" does not apply
13797 -- to generic formal access types, so a prefix of such
13798 -- a type is a "bad" prefix.
13800 if Is_Generic_Formal (Exp_Type) then
13801 return True;
13803 -- The "statically deeper relationship" does apply to
13804 -- any other named access type.
13806 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13807 return False;
13808 end if;
13810 pragma Assert (Is_Itype (Exp_Type));
13811 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13813 -- The "statically deeper relationship" applies to some
13814 -- anonymous access types and not to others. Return
13815 -- True for the cases where it does not apply. Also check
13816 -- recursively for the
13817 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13818 -- where the correct result depends on <prefix>.
13820 return Nkind (Associated_Node) in
13821 N_Procedure_Specification | -- access parameter
13822 N_Function_Specification | -- access parameter
13823 N_Object_Declaration -- saooaaat
13824 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13825 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13827 ----------------------------
13828 -- Valid_Array_Conversion --
13829 ----------------------------
13831 function Valid_Array_Conversion return Boolean is
13832 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13833 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13835 Opnd_Index : Node_Id;
13836 Opnd_Index_Type : Entity_Id;
13838 Target_Comp_Type : constant Entity_Id :=
13839 Component_Type (Target_Type);
13840 Target_Comp_Base : constant Entity_Id :=
13841 Base_Type (Target_Comp_Type);
13843 Target_Index : Node_Id;
13844 Target_Index_Type : Entity_Id;
13846 begin
13847 -- Error if wrong number of dimensions
13850 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13851 then
13852 Conversion_Error_N
13853 ("incompatible number of dimensions for conversion", Operand);
13854 return False;
13856 -- Number of dimensions matches
13858 else
13859 -- Loop through indexes of the two arrays
13861 Target_Index := First_Index (Target_Type);
13862 Opnd_Index := First_Index (Opnd_Type);
13863 while Present (Target_Index) and then Present (Opnd_Index) loop
13864 Target_Index_Type := Etype (Target_Index);
13865 Opnd_Index_Type := Etype (Opnd_Index);
13867 -- Error if index types are incompatible
13869 if not (Is_Integer_Type (Target_Index_Type)
13870 and then Is_Integer_Type (Opnd_Index_Type))
13871 and then Root_Type (Target_Index_Type)
13872 /= Root_Type (Opnd_Index_Type)
13873 then
13874 Conversion_Error_N
13875 ("incompatible index types for array conversion",
13876 Operand);
13877 return False;
13878 end if;
13880 Next_Index (Target_Index);
13881 Next_Index (Opnd_Index);
13882 end loop;
13884 -- If component types have same base type, all set
13886 if Target_Comp_Base = Opnd_Comp_Base then
13887 null;
13889 -- Here if base types of components are not the same. The only
13890 -- time this is allowed is if we have anonymous access types.
13892 -- The conversion of arrays of anonymous access types can lead
13893 -- to dangling pointers. AI-392 formalizes the accessibility
13894 -- checks that must be applied to such conversions to prevent
13895 -- out-of-scope references.
13897 elsif Ekind (Target_Comp_Base) in
13898 E_Anonymous_Access_Type
13899 | E_Anonymous_Access_Subprogram_Type
13900 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13901 and then
13902 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13903 then
13904 if Type_Access_Level (Target_Type) <
13905 Deepest_Type_Access_Level (Opnd_Type)
13906 then
13907 if In_Instance_Body then
13908 Error_Msg_Warn := SPARK_Mode /= On;
13909 Conversion_Error_N
13910 ("source array type has deeper accessibility "
13911 & "level than target<<", Operand);
13912 Conversion_Error_N ("\Program_Error [<<", Operand);
13913 Rewrite (N,
13914 Make_Raise_Program_Error (Sloc (N),
13915 Reason => PE_Accessibility_Check_Failed));
13916 Set_Etype (N, Target_Type);
13917 return False;
13919 -- Conversion not allowed because of accessibility levels
13921 else
13922 Conversion_Error_N
13923 ("source array type has deeper accessibility "
13924 & "level than target", Operand);
13925 return False;
13926 end if;
13928 else
13929 null;
13930 end if;
13932 -- All other cases where component base types do not match
13934 else
13935 Conversion_Error_N
13936 ("incompatible component types for array conversion",
13937 Operand);
13938 return False;
13939 end if;
13941 -- Check that component subtypes statically match. For numeric
13942 -- types this means that both must be either constrained or
13943 -- unconstrained. For enumeration types the bounds must match.
13944 -- All of this is checked in Subtypes_Statically_Match.
13946 if not Subtypes_Statically_Match
13947 (Target_Comp_Type, Opnd_Comp_Type)
13948 then
13949 Conversion_Error_N
13950 ("component subtypes must statically match", Operand);
13951 return False;
13952 end if;
13953 end if;
13955 return True;
13956 end Valid_Array_Conversion;
13958 -----------------------------
13959 -- Valid_Tagged_Conversion --
13960 -----------------------------
13962 function Valid_Tagged_Conversion
13963 (Target_Type : Entity_Id;
13964 Opnd_Type : Entity_Id) return Boolean
13966 begin
13967 -- Upward conversions are allowed (RM 4.6(22))
13969 if Covers (Target_Type, Opnd_Type)
13970 or else Is_Ancestor (Target_Type, Opnd_Type)
13971 then
13972 return True;
13974 -- Downward conversion are allowed if the operand is class-wide
13975 -- (RM 4.6(23)).
13977 elsif Is_Class_Wide_Type (Opnd_Type)
13978 and then Covers (Opnd_Type, Target_Type)
13979 then
13980 return True;
13982 elsif Covers (Opnd_Type, Target_Type)
13983 or else Is_Ancestor (Opnd_Type, Target_Type)
13984 then
13985 -- Deal with non-extension derivation involving an
13986 -- untagged view of a tagged type.
13988 if not Is_Tagged_Type (Target_Type) then
13989 return True;
13990 end if;
13992 return
13993 Conversion_Check (False,
13994 "downward conversion of tagged objects not allowed");
13996 -- Ada 2005 (AI-251): A conversion is valid if the operand and target
13997 -- types are both class-wide types and the specific type associated
13998 -- with at least one of them is an interface type (RM 4.6 (23.1/2));
13999 -- at run-time a check will verify the validity of this interface
14000 -- type conversion.
14002 elsif Is_Class_Wide_Type (Target_Type)
14003 and then Is_Class_Wide_Type (Opnd_Type)
14004 and then (Is_Interface (Target_Type)
14005 or else Is_Interface (Opnd_Type))
14006 then
14007 return True;
14009 -- Report errors
14011 elsif Is_Class_Wide_Type (Target_Type)
14012 and then Is_Interface (Target_Type)
14013 and then not Is_Interface (Opnd_Type)
14014 and then not Interface_Present_In_Ancestor
14015 (Typ => Opnd_Type,
14016 Iface => Target_Type)
14017 then
14018 Error_Msg_Name_1 := Chars (Etype (Target_Type));
14019 Error_Msg_Name_2 := Chars (Opnd_Type);
14020 Conversion_Error_N
14021 ("wrong interface conversion (% is not a progenitor "
14022 & "of %)", N);
14023 return False;
14025 elsif Is_Class_Wide_Type (Opnd_Type)
14026 and then Is_Interface (Opnd_Type)
14027 and then not Is_Interface (Target_Type)
14028 and then not Interface_Present_In_Ancestor
14029 (Typ => Target_Type,
14030 Iface => Opnd_Type)
14031 then
14032 Error_Msg_Name_1 := Chars (Etype (Opnd_Type));
14033 Error_Msg_Name_2 := Chars (Target_Type);
14034 Conversion_Error_N
14035 ("wrong interface conversion (% is not a progenitor "
14036 & "of %)", N);
14038 -- Search for interface types shared between the target type and
14039 -- the operand interface type to complete the text of the error
14040 -- since the source of this error is a missing type conversion
14041 -- to such interface type.
14043 if Has_Interfaces (Target_Type) then
14044 declare
14045 Operand_Ifaces_List : Elist_Id;
14046 Operand_Iface_Elmt : Elmt_Id;
14047 Target_Ifaces_List : Elist_Id;
14048 Target_Iface_Elmt : Elmt_Id;
14049 First_Candidate : Boolean := True;
14051 begin
14052 Collect_Interfaces (Base_Type (Target_Type),
14053 Target_Ifaces_List);
14054 Collect_Interfaces (Root_Type (Base_Type (Opnd_Type)),
14055 Operand_Ifaces_List);
14057 Operand_Iface_Elmt := First_Elmt (Operand_Ifaces_List);
14058 while Present (Operand_Iface_Elmt) loop
14059 Target_Iface_Elmt := First_Elmt (Target_Ifaces_List);
14060 while Present (Target_Iface_Elmt) loop
14061 if Node (Operand_Iface_Elmt)
14062 = Node (Target_Iface_Elmt)
14063 then
14064 Error_Msg_Name_1 :=
14065 Chars (Node (Target_Iface_Elmt));
14067 if First_Candidate then
14068 First_Candidate := False;
14069 Conversion_Error_N
14070 ("\must convert to `%''Class` before downward "
14071 & "conversion", Operand);
14072 else
14073 Conversion_Error_N
14074 ("\or must convert to `%''Class` before "
14075 & "downward conversion", Operand);
14076 end if;
14077 end if;
14079 Next_Elmt (Target_Iface_Elmt);
14080 end loop;
14082 Next_Elmt (Operand_Iface_Elmt);
14083 end loop;
14084 end;
14085 end if;
14087 return False;
14089 elsif not Is_Class_Wide_Type (Target_Type)
14090 and then Is_Interface (Target_Type)
14091 then
14092 Conversion_Error_N
14093 ("wrong use of interface type in tagged conversion", N);
14094 Conversion_Error_N
14095 ("\add ''Class to the target interface type", N);
14096 return False;
14098 elsif not Is_Class_Wide_Type (Opnd_Type)
14099 and then Is_Interface (Opnd_Type)
14100 then
14101 Conversion_Error_N
14102 ("must convert to class-wide interface type before downward "
14103 & "conversion", Operand);
14104 return False;
14106 else
14107 Conversion_Error_NE
14108 ("invalid tagged conversion, not compatible with}",
14109 N, First_Subtype (Opnd_Type));
14110 return False;
14111 end if;
14112 end Valid_Tagged_Conversion;
14114 -- Start of processing for Valid_Conversion
14116 begin
14117 Check_Parameterless_Call (Operand);
14119 if Is_Overloaded (Operand) then
14120 declare
14121 I : Interp_Index;
14122 I1 : Interp_Index;
14123 It : Interp;
14124 It1 : Interp;
14125 N1 : Entity_Id;
14126 T1 : Entity_Id;
14128 begin
14129 -- Remove procedure calls, which syntactically cannot appear in
14130 -- this context, but which cannot be removed by type checking,
14131 -- because the context does not impose a type.
14133 -- The node may be labelled overloaded, but still contain only one
14134 -- interpretation because others were discarded earlier. If this
14135 -- is the case, retain the single interpretation if legal.
14137 Get_First_Interp (Operand, I, It);
14138 Opnd_Type := It.Typ;
14139 Get_Next_Interp (I, It);
14141 if Present (It.Typ)
14142 and then Opnd_Type /= Standard_Void_Type
14143 then
14144 -- More than one candidate interpretation is available
14146 Get_First_Interp (Operand, I, It);
14147 while Present (It.Typ) loop
14148 if It.Typ = Standard_Void_Type then
14149 Remove_Interp (I);
14150 end if;
14152 -- When compiling for a system where Address is of a visible
14153 -- integer type, spurious ambiguities can be produced when
14154 -- arithmetic operations have a literal operand and return
14155 -- System.Address or a descendant of it. These ambiguities
14156 -- are usually resolved by the context, but for conversions
14157 -- there is no context type and the removal of the spurious
14158 -- operations must be done explicitly here.
14160 if not Address_Is_Private
14161 and then Is_Descendant_Of_Address (It.Typ)
14162 then
14163 Remove_Interp (I);
14164 end if;
14166 Get_Next_Interp (I, It);
14167 end loop;
14168 end if;
14170 Get_First_Interp (Operand, I, It);
14171 I1 := I;
14172 It1 := It;
14174 if No (It.Typ) then
14175 Conversion_Error_N ("illegal operand in conversion", Operand);
14176 return False;
14177 end if;
14179 Get_Next_Interp (I, It);
14181 if Present (It.Typ) then
14182 N1 := It1.Nam;
14183 T1 := It1.Typ;
14184 It1 := Disambiguate (Operand, I1, I, Any_Type);
14186 if It1 = No_Interp then
14187 Conversion_Error_N
14188 ("ambiguous operand in conversion", Operand);
14190 -- If the interpretation involves a standard operator, use
14191 -- the location of the type, which may be user-defined.
14193 if Sloc (It.Nam) = Standard_Location then
14194 Error_Msg_Sloc := Sloc (It.Typ);
14195 else
14196 Error_Msg_Sloc := Sloc (It.Nam);
14197 end if;
14199 Conversion_Error_N -- CODEFIX
14200 ("\\possible interpretation#!", Operand);
14202 if Sloc (N1) = Standard_Location then
14203 Error_Msg_Sloc := Sloc (T1);
14204 else
14205 Error_Msg_Sloc := Sloc (N1);
14206 end if;
14208 Conversion_Error_N -- CODEFIX
14209 ("\\possible interpretation#!", Operand);
14211 return False;
14212 end if;
14213 end if;
14215 Set_Etype (Operand, It1.Typ);
14216 Opnd_Type := It1.Typ;
14217 end;
14218 end if;
14220 -- When we encounter a class-wide equivalent type used to represent
14221 -- a fully sized mutably tagged type, pretend we are actually looking
14222 -- at the class-wide mutably tagged type instead.
14224 Opnd_Type :=
14225 Get_Corresponding_Mutably_Tagged_Type_If_Present (Opnd_Type);
14227 -- Deal with conversion of integer type to address if the pragma
14228 -- Allow_Integer_Address is in effect. We convert the conversion to
14229 -- an unchecked conversion in this case and we are all done.
14231 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
14232 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
14233 Analyze_And_Resolve (N, Target_Type);
14234 return True;
14235 end if;
14237 -- If we are within a child unit, check whether the type of the
14238 -- expression has an ancestor in a parent unit, in which case it
14239 -- belongs to its derivation class even if the ancestor is private.
14240 -- See RM 7.3.1 (5.2/3).
14242 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
14244 -- Numeric types
14246 if Is_Numeric_Type (Target_Type) then
14248 -- A universal fixed expression can be converted to any numeric type
14250 if Opnd_Type = Universal_Fixed then
14251 return True;
14253 -- Also no need to check when in an instance or inlined body, because
14254 -- the legality has been established when the template was analyzed.
14255 -- Furthermore, numeric conversions may occur where only a private
14256 -- view of the operand type is visible at the instantiation point.
14257 -- This results in a spurious error if we check that the operand type
14258 -- is a numeric type.
14260 -- Note: in a previous version of this unit, the following tests were
14261 -- applied only for generated code (Comes_From_Source set to False),
14262 -- but in fact the test is required for source code as well, since
14263 -- this situation can arise in source code.
14265 elsif In_Instance_Code or else In_Inlined_Body then
14266 return True;
14268 -- Otherwise we need the conversion check
14270 else
14271 return Conversion_Check
14272 (Is_Numeric_Type (Opnd_Type)
14273 or else
14274 (Present (Inc_Ancestor)
14275 and then Is_Numeric_Type (Inc_Ancestor)),
14276 "illegal operand for numeric conversion");
14277 end if;
14279 -- Array types
14281 elsif Is_Array_Type (Target_Type) then
14282 if not Is_Array_Type (Opnd_Type)
14283 or else Opnd_Type = Any_Composite
14284 or else Opnd_Type = Any_String
14285 then
14286 if not Comes_From_Source (N)
14287 and then Implementation_Base_Type (Target_Type) =
14288 Implementation_Base_Type (Opnd_Type)
14289 then
14290 return True;
14291 end if;
14293 Conversion_Error_N
14294 ("illegal operand for array conversion", Operand);
14295 return False;
14297 else
14298 return Valid_Array_Conversion;
14299 end if;
14301 -- Ada 2005 (AI-251): Internally generated conversions of access to
14302 -- interface types added to force the displacement of the pointer to
14303 -- reference the corresponding dispatch table.
14305 elsif not Comes_From_Source (N)
14306 and then Is_Access_Type (Target_Type)
14307 and then Is_Interface (Designated_Type (Target_Type))
14308 then
14309 return True;
14311 -- Ada 2005 (AI-251): Anonymous access types where target references an
14312 -- interface type.
14314 elsif Is_Access_Type (Opnd_Type)
14315 and then Ekind (Target_Type) in
14316 E_General_Access_Type | E_Anonymous_Access_Type
14317 and then Is_Interface (Directly_Designated_Type (Target_Type))
14318 then
14319 -- Check the static accessibility rule of 4.6(17). Note that the
14320 -- check is not enforced when within an instance body, since the
14321 -- RM requires such cases to be caught at run time.
14323 -- If the operand is a rewriting of an allocator no check is needed
14324 -- because there are no accessibility issues.
14326 if Nkind (Original_Node (N)) = N_Allocator then
14327 null;
14329 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
14330 if Type_Access_Level (Opnd_Type) >
14331 Deepest_Type_Access_Level (Target_Type)
14332 then
14333 -- In an instance, this is a run-time check, but one we know
14334 -- will fail, so generate an appropriate warning. The raise
14335 -- will be generated by Expand_N_Type_Conversion.
14337 if In_Instance_Body then
14338 Error_Msg_Warn := SPARK_Mode /= On;
14339 Conversion_Error_N
14340 ("cannot convert local pointer to non-local access type<<",
14341 Operand);
14342 Conversion_Error_N ("\Program_Error [<<", Operand);
14344 else
14345 Conversion_Error_N
14346 ("cannot convert local pointer to non-local access type",
14347 Operand);
14348 return False;
14349 end if;
14351 -- Special accessibility checks are needed in the case of access
14352 -- discriminants declared for a limited type.
14354 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14355 and then not Is_Local_Anonymous_Access (Opnd_Type)
14356 then
14357 -- When the operand is a selected access discriminant the check
14358 -- needs to be made against the level of the object denoted by
14359 -- the prefix of the selected name (Accessibility_Level handles
14360 -- checking the prefix of the operand for this case).
14362 if Nkind (Operand) = N_Selected_Component
14363 and then Static_Accessibility_Level
14364 (Operand, Zero_On_Dynamic_Level)
14365 > Deepest_Type_Access_Level (Target_Type)
14366 then
14367 -- In an instance, this is a run-time check, but one we know
14368 -- will fail, so generate an appropriate warning. The raise
14369 -- will be generated by Expand_N_Type_Conversion.
14371 if In_Instance_Body then
14372 Error_Msg_Warn := SPARK_Mode /= On;
14373 Conversion_Error_N
14374 ("cannot convert access discriminant to non-local "
14375 & "access type<<", Operand);
14376 Conversion_Error_N ("\Program_Error [<<", Operand);
14378 -- Real error if not in instance body
14380 else
14381 Conversion_Error_N
14382 ("cannot convert access discriminant to non-local "
14383 & "access type", Operand);
14384 return False;
14385 end if;
14386 end if;
14388 -- The case of a reference to an access discriminant from
14389 -- within a limited type declaration (which will appear as
14390 -- a discriminal) is always illegal because the level of the
14391 -- discriminant is considered to be deeper than any (nameable)
14392 -- access type.
14394 if Is_Entity_Name (Operand)
14395 and then not Is_Local_Anonymous_Access (Opnd_Type)
14396 and then
14397 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14398 and then Present (Discriminal_Link (Entity (Operand)))
14399 then
14400 Conversion_Error_N
14401 ("discriminant has deeper accessibility level than target",
14402 Operand);
14403 return False;
14404 end if;
14405 end if;
14406 end if;
14408 return True;
14410 -- General and anonymous access types
14412 elsif Ekind (Target_Type) in
14413 E_General_Access_Type | E_Anonymous_Access_Type
14414 and then
14415 Conversion_Check
14416 (Is_Access_Type (Opnd_Type)
14417 and then
14418 Ekind (Opnd_Type) not in
14419 E_Access_Subprogram_Type |
14420 E_Access_Protected_Subprogram_Type,
14421 "must be an access-to-object type")
14422 then
14423 if Is_Access_Constant (Opnd_Type)
14424 and then not Is_Access_Constant (Target_Type)
14425 then
14426 Conversion_Error_N
14427 ("access-to-constant operand type not allowed", Operand);
14428 return False;
14429 end if;
14431 declare
14432 Extended_Opnd : constant Boolean :=
14433 Is_Extended_Access_Type (Opnd_Type);
14434 Extended_Target : constant Boolean :=
14435 Is_Extended_Access_Type (Target_Type);
14436 begin
14437 -- An extended access value may designate objects that are
14438 -- impossible to reference using a non-extended type, so
14439 -- prohibit conversions that would require being able to
14440 -- do the impossible.
14442 if Extended_Opnd then
14443 if not Extended_Target then
14444 Conversion_Error_N
14445 ("cannot convert extended access value"
14446 & " to non-extended access type",
14447 Operand);
14448 return False;
14449 end if;
14451 -- Detect bad conversion on copy back for a view conversion
14453 elsif Extended_Target and then Is_View_Conversion (N) then
14454 Conversion_Error_N
14455 ("cannot convert non-extended value"
14456 & " to extended access type in view conversion",
14457 Operand);
14458 return False;
14459 end if;
14460 end;
14462 -- Check the static accessibility rule of 4.6(17). Note that the
14463 -- check is not enforced when within an instance body, since the RM
14464 -- requires such cases to be caught at run time.
14466 if Ekind (Target_Type) /= E_Anonymous_Access_Type
14467 or else Is_Local_Anonymous_Access (Target_Type)
14468 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
14469 N_Object_Declaration
14470 then
14471 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
14472 -- conversions from an anonymous access type to a named general
14473 -- access type. Such conversions are not allowed in the case of
14474 -- access parameters and stand-alone objects of an anonymous
14475 -- access type. The implicit conversion case is recognized by
14476 -- testing that Comes_From_Source is False and that it's been
14477 -- rewritten. The Comes_From_Source test isn't sufficient because
14478 -- nodes in inlined calls to predefined library routines can have
14479 -- Comes_From_Source set to False. (Is there a better way to test
14480 -- for implicit conversions???).
14482 -- Do not treat a rewritten 'Old attribute reference like other
14483 -- rewrite substitutions. This makes a difference, for example,
14484 -- in the case where we are generating the expansion of a
14485 -- membership test of the form
14486 -- Saooaaat'Old in Named_Access_Type
14487 -- because in this case Valid_Conversion needs to return True
14488 -- (otherwise the expansion will be False - see the call site
14489 -- in exp_ch4.adb).
14491 if Ada_Version >= Ada_2012
14492 and then not Comes_From_Source (N)
14493 and then Is_Rewrite_Substitution (N)
14494 and then not Is_Attribute_Old (Original_Node (N))
14495 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
14496 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
14497 then
14498 if Is_Itype (Opnd_Type) then
14500 -- When applying restriction No_Dynamic_Accessibility_Check,
14501 -- implicit conversions are allowed when the operand type is
14502 -- not deeper than the target type.
14504 if No_Dynamic_Accessibility_Checks_Enabled (N) then
14505 if Type_Access_Level (Opnd_Type)
14506 > Deepest_Type_Access_Level (Target_Type)
14507 then
14508 Conversion_Error_N
14509 ("operand has deeper level than target", Operand);
14510 return False;
14511 end if;
14513 -- Implicit conversions aren't allowed for objects of an
14514 -- anonymous access type, since such objects have nonstatic
14515 -- levels in Ada 2012.
14517 elsif Nkind (Associated_Node_For_Itype (Opnd_Type))
14518 = N_Object_Declaration
14519 then
14520 Conversion_Error_N
14521 ("implicit conversion of stand-alone anonymous "
14522 & "access object not allowed", Operand);
14523 return False;
14525 -- Implicit conversions aren't allowed for anonymous access
14526 -- parameters. We exclude anonymous access results as well
14527 -- as universal_access "=".
14529 elsif not Is_Local_Anonymous_Access (Opnd_Type)
14530 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
14531 N_Function_Specification |
14532 N_Procedure_Specification
14533 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
14534 then
14535 Conversion_Error_N
14536 ("implicit conversion of anonymous access parameter "
14537 & "not allowed", Operand);
14538 return False;
14540 -- Detect access discriminant values that are illegal
14541 -- implicit anonymous-to-named access conversion operands.
14543 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
14544 then
14545 Conversion_Error_N
14546 ("implicit conversion of anonymous access value "
14547 & "not allowed", Operand);
14548 return False;
14550 -- In other cases, the level of the operand's type must be
14551 -- statically less deep than that of the target type, else
14552 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
14554 elsif Type_Access_Level (Opnd_Type) >
14555 Deepest_Type_Access_Level (Target_Type)
14556 then
14557 Conversion_Error_N
14558 ("implicit conversion of anonymous access value "
14559 & "violates accessibility", Operand);
14560 return False;
14561 end if;
14562 end if;
14564 -- Check if the operand is deeper than the target type, taking
14565 -- care to avoid the case where we are converting a result of a
14566 -- function returning an anonymous access type since the "master
14567 -- of the call" would be target type of the conversion unless
14568 -- the target type is anonymous access as well - see RM 3.10.2
14569 -- (10.3/3).
14571 -- Note that when the restriction No_Dynamic_Accessibility_Checks
14572 -- is in effect wei also want to proceed with the conversion check
14573 -- described above.
14575 elsif Type_Access_Level (Opnd_Type, Assoc_Ent => Operand)
14576 > Deepest_Type_Access_Level (Target_Type)
14577 and then (Nkind (Associated_Node_For_Itype (Opnd_Type))
14578 /= N_Function_Specification
14579 or else Ekind (Target_Type) in Anonymous_Access_Kind
14580 or else No_Dynamic_Accessibility_Checks_Enabled (N))
14582 -- Check we are not in a return value ???
14584 and then (not In_Return_Value (N)
14585 or else
14586 Nkind (Associated_Node_For_Itype (Target_Type))
14587 = N_Component_Declaration)
14588 then
14589 -- In an instance, this is a run-time check, but one we know
14590 -- will fail, so generate an appropriate warning. The raise
14591 -- will be generated by Expand_N_Type_Conversion.
14593 if In_Instance_Body then
14594 Error_Msg_Warn := SPARK_Mode /= On;
14595 Conversion_Error_N
14596 ("cannot convert local pointer to non-local access type<<",
14597 Operand);
14598 Conversion_Error_N ("\Program_Error [<<", Operand);
14600 -- If not in an instance body, this is a real error
14602 else
14603 -- Avoid generation of spurious error message
14605 if not Error_Posted (N) then
14606 Conversion_Error_N
14607 ("cannot convert local pointer to non-local access type",
14608 Operand);
14609 end if;
14611 return False;
14612 end if;
14614 -- Special accessibility checks are needed in the case of access
14615 -- discriminants declared for a limited type.
14617 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14618 and then not Is_Local_Anonymous_Access (Opnd_Type)
14619 then
14620 -- When the operand is a selected access discriminant the check
14621 -- needs to be made against the level of the object denoted by
14622 -- the prefix of the selected name (Accessibility_Level handles
14623 -- checking the prefix of the operand for this case).
14625 if Nkind (Operand) = N_Selected_Component
14626 and then Static_Accessibility_Level
14627 (Operand, Zero_On_Dynamic_Level)
14628 > Deepest_Type_Access_Level (Target_Type)
14629 then
14630 -- In an instance, this is a run-time check, but one we know
14631 -- will fail, so generate an appropriate warning. The raise
14632 -- will be generated by Expand_N_Type_Conversion.
14634 if In_Instance_Body then
14635 Error_Msg_Warn := SPARK_Mode /= On;
14636 Conversion_Error_N
14637 ("cannot convert access discriminant to non-local "
14638 & "access type<<", Operand);
14639 Conversion_Error_N ("\Program_Error [<<", Operand);
14641 -- If not in an instance body, this is a real error
14643 else
14644 Conversion_Error_N
14645 ("cannot convert access discriminant to non-local "
14646 & "access type", Operand);
14647 return False;
14648 end if;
14649 end if;
14651 -- The case of a reference to an access discriminant from
14652 -- within a limited type declaration (which will appear as
14653 -- a discriminal) is always illegal because the level of the
14654 -- discriminant is considered to be deeper than any (nameable)
14655 -- access type.
14657 if Is_Entity_Name (Operand)
14658 and then
14659 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14660 and then Present (Discriminal_Link (Entity (Operand)))
14661 then
14662 Conversion_Error_N
14663 ("discriminant has deeper accessibility level than target",
14664 Operand);
14665 return False;
14666 end if;
14667 end if;
14668 end if;
14670 -- In the presence of limited_with clauses we have to use nonlimited
14671 -- views, if available.
14673 Check_Limited : declare
14674 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
14675 -- Helper function to handle limited views
14677 --------------------------
14678 -- Full_Designated_Type --
14679 --------------------------
14681 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
14682 Desig : constant Entity_Id := Designated_Type (T);
14684 begin
14685 -- Handle the limited view of a type
14687 if From_Limited_With (Desig)
14688 and then Has_Non_Limited_View (Desig)
14689 then
14690 return Available_View (Desig);
14691 else
14692 return Desig;
14693 end if;
14694 end Full_Designated_Type;
14696 -- Local Declarations
14698 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
14699 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
14701 Same_Base : constant Boolean :=
14702 Base_Type (Target) = Base_Type (Opnd);
14704 -- Start of processing for Check_Limited
14706 begin
14707 if Is_Tagged_Type (Target) then
14708 return Valid_Tagged_Conversion (Target, Opnd);
14710 else
14711 if not Same_Base then
14712 Conversion_Error_NE
14713 ("target designated type not compatible with }",
14714 N, Base_Type (Opnd));
14715 return False;
14717 -- Ada 2005 AI-384: legality rule is symmetric in both
14718 -- designated types. The conversion is legal (with possible
14719 -- constraint check) if either designated type is
14720 -- unconstrained.
14722 elsif Subtypes_Statically_Match (Target, Opnd)
14723 or else
14724 (Has_Discriminants (Target)
14725 and then
14726 (not Is_Constrained (Opnd)
14727 or else not Is_Constrained (Target)))
14728 then
14729 -- Special case, if Value_Size has been used to make the
14730 -- sizes different, the conversion is not allowed even
14731 -- though the subtypes statically match.
14733 if Known_Static_RM_Size (Target)
14734 and then Known_Static_RM_Size (Opnd)
14735 and then RM_Size (Target) /= RM_Size (Opnd)
14736 then
14737 Conversion_Error_NE
14738 ("target designated subtype not compatible with }",
14739 N, Opnd);
14740 Conversion_Error_NE
14741 ("\because sizes of the two designated subtypes differ",
14742 N, Opnd);
14743 return False;
14745 -- Normal case where conversion is allowed
14747 else
14748 return True;
14749 end if;
14751 else
14752 Error_Msg_NE
14753 ("target designated subtype not compatible with }",
14754 N, Opnd);
14755 return False;
14756 end if;
14757 end if;
14758 end Check_Limited;
14760 -- Access to subprogram types. If the operand is an access parameter,
14761 -- the type has a deeper accessibility that any master, and cannot be
14762 -- assigned. We must make an exception if the conversion is part of an
14763 -- assignment and the target is the return object of an extended return
14764 -- statement, because in that case the accessibility check takes place
14765 -- after the return.
14767 elsif Is_Access_Subprogram_Type (Target_Type)
14769 -- Note: this test of Opnd_Type is there to prevent entering this
14770 -- branch in the case of a remote access to subprogram type, which
14771 -- is internally represented as an E_Record_Type.
14773 and then Is_Access_Type (Opnd_Type)
14774 then
14775 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
14776 and then Is_Entity_Name (Operand)
14777 and then Ekind (Entity (Operand)) = E_In_Parameter
14778 and then
14779 (Nkind (Parent (N)) /= N_Assignment_Statement
14780 or else not Is_Entity_Name (Name (Parent (N)))
14781 or else not Is_Return_Object (Entity (Name (Parent (N)))))
14782 then
14783 Conversion_Error_N
14784 ("illegal attempt to store anonymous access to subprogram",
14785 Operand);
14786 Conversion_Error_N
14787 ("\value has deeper accessibility than any master "
14788 & "(RM 3.10.2 (13))",
14789 Operand);
14791 Error_Msg_NE
14792 ("\use named access type for& instead of access parameter",
14793 Operand, Entity (Operand));
14794 end if;
14796 -- Check that the designated types are subtype conformant
14798 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
14799 Old_Id => Designated_Type (Opnd_Type),
14800 Err_Loc => N);
14802 -- Check the static accessibility rule of 4.6(20)
14804 if Type_Access_Level (Opnd_Type) >
14805 Deepest_Type_Access_Level (Target_Type)
14806 then
14807 Conversion_Error_N
14808 ("operand type has deeper accessibility level than target",
14809 Operand);
14811 -- Check that if the operand type is declared in a generic body,
14812 -- then the target type must be declared within that same body
14813 -- (enforces last sentence of 4.6(20)).
14815 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
14816 declare
14817 O_Gen : constant Node_Id :=
14818 Enclosing_Generic_Body (Opnd_Type);
14820 T_Gen : Node_Id;
14822 begin
14823 T_Gen := Enclosing_Generic_Body (Target_Type);
14824 while Present (T_Gen) and then T_Gen /= O_Gen loop
14825 T_Gen := Enclosing_Generic_Body (T_Gen);
14826 end loop;
14828 if T_Gen /= O_Gen then
14829 Conversion_Error_N
14830 ("target type must be declared in same generic body "
14831 & "as operand type", N);
14832 end if;
14833 end;
14834 end if;
14836 -- Check that the strub modes are compatible.
14837 -- We wish to reject explicit conversions only for
14838 -- incompatible modes.
14840 return Conversion_Check
14841 (Compatible_Strub_Modes
14842 (Designated_Type (Target_Type),
14843 Designated_Type (Opnd_Type)),
14844 "incompatible `strub` modes");
14846 -- Remote access to subprogram types
14848 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
14849 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
14850 then
14851 -- It is valid to convert from one RAS type to another provided
14852 -- that their specification statically match.
14854 -- Note: at this point, remote access to subprogram types have been
14855 -- expanded to their E_Record_Type representation, and we need to
14856 -- go back to the original access type definition using the
14857 -- Corresponding_Remote_Type attribute in order to check that the
14858 -- designated profiles match.
14860 pragma Assert (Ekind (Target_Type) = E_Record_Type);
14861 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
14863 Check_Subtype_Conformant
14864 (New_Id =>
14865 Designated_Type (Corresponding_Remote_Type (Target_Type)),
14866 Old_Id =>
14867 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14868 Err_Loc =>
14871 -- Check that the strub modes are compatible.
14872 -- We wish to reject explicit conversions only for
14873 -- incompatible modes.
14875 return Conversion_Check
14876 (Compatible_Strub_Modes
14877 (Designated_Type (Target_Type),
14878 Designated_Type (Opnd_Type)),
14879 "incompatible `strub` modes");
14881 -- If it was legal in the generic, it's legal in the instance
14883 elsif In_Instance then
14884 return True;
14886 -- Ignore privacy for streaming or Put_Image routines
14888 elsif not Comes_From_Source (N)
14889 and then Original_Implementation_Base_Type (Target_Type) =
14890 Original_Implementation_Base_Type (Opnd_Type)
14891 then
14892 return True;
14894 -- If both are tagged types, check legality of view conversions
14896 elsif (Is_Tagged_Type (Target_Type) and then Is_Tagged_Type (Opnd_Type))
14897 or else (not Comes_From_Source (N)
14898 and then
14899 Is_Tagged_Type (Implementation_Base_Type (Target_Type))
14900 and then
14901 Is_Tagged_Type (Implementation_Base_Type (Opnd_Type)))
14902 then
14903 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14905 -- Types derived from the same root type are convertible
14907 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14908 return True;
14910 -- In an instance or an inlined body, there may be inconsistent views of
14911 -- the same type, or of types derived from a common root. Similarly
14912 -- for compiler-generated streaming or Put_Image subprograms.
14914 elsif (In_Instance or In_Inlined_Body or not Comes_From_Source (N))
14915 and then
14916 Root_Type (Underlying_Type (Target_Type)) =
14917 Root_Type (Underlying_Type (Opnd_Type))
14918 then
14919 return True;
14921 -- Special check for common access type error case
14923 elsif Ekind (Target_Type) = E_Access_Type
14924 and then Is_Access_Type (Opnd_Type)
14925 then
14926 Conversion_Error_N ("target type must be general access type!", N);
14927 Conversion_Error_NE -- CODEFIX
14928 ("\add ALL to }!", N, Target_Type);
14929 return False;
14931 -- Here we have a real conversion error
14933 else
14934 -- Check for missing regular with_clause when only a limited view of
14935 -- target is available.
14937 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14938 Conversion_Error_NE
14939 ("invalid conversion, not compatible with limited view of }",
14940 N, Opnd_Type);
14941 Conversion_Error_NE
14942 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14944 elsif Is_Access_Type (Opnd_Type)
14945 and then From_Limited_With (Designated_Type (Opnd_Type))
14946 and then In_Package_Body
14947 then
14948 Conversion_Error_NE
14949 ("invalid conversion, not compatible with }", N, Opnd_Type);
14950 Conversion_Error_NE
14951 ("\add with_clause for& to current unit!",
14952 N, Scope (Designated_Type (Opnd_Type)));
14954 else
14955 Conversion_Error_NE
14956 ("invalid conversion, not compatible with }", N, Opnd_Type);
14957 end if;
14959 return False;
14960 end if;
14961 end Valid_Conversion;
14963 end Sem_Res;