Finalization of object allocated by anonymous access designating local type
[official-gcc.git] / gcc / ada / sem_res.adb
blob075c0d85ccd96a28fb4489e92a1b3730624558ba
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 Namet; use Namet;
51 with Nmake; use Nmake;
52 with Nlists; use Nlists;
53 with Opt; use Opt;
54 with Output; use Output;
55 with Par_SCO; use Par_SCO;
56 with Restrict; use Restrict;
57 with Rident; use Rident;
58 with Rtsfind; use Rtsfind;
59 with Sem; use Sem;
60 with Sem_Aggr; use Sem_Aggr;
61 with Sem_Attr; use Sem_Attr;
62 with Sem_Aux; use Sem_Aux;
63 with Sem_Case; use Sem_Case;
64 with Sem_Cat; use Sem_Cat;
65 with Sem_Ch3; use Sem_Ch3;
66 with Sem_Ch4; use Sem_Ch4;
67 with Sem_Ch5; use Sem_Ch5;
68 with Sem_Ch6; use Sem_Ch6;
69 with Sem_Ch8; use Sem_Ch8;
70 with Sem_Ch13; use Sem_Ch13;
71 with Sem_Dim; use Sem_Dim;
72 with Sem_Disp; use Sem_Disp;
73 with Sem_Dist; use Sem_Dist;
74 with Sem_Elab; use Sem_Elab;
75 with Sem_Elim; use Sem_Elim;
76 with Sem_Eval; use Sem_Eval;
77 with Sem_Intr; use Sem_Intr;
78 with Sem_Mech; use Sem_Mech;
79 with Sem_Type; use Sem_Type;
80 with Sem_Util; use Sem_Util;
81 with Sem_Warn; use Sem_Warn;
82 with Sinfo; use Sinfo;
83 with Sinfo.Nodes; use Sinfo.Nodes;
84 with Sinfo.Utils; use Sinfo.Utils;
85 with Sinfo.CN; use Sinfo.CN;
86 with Snames; use Snames;
87 with Stand; use Stand;
88 with Stringt; use Stringt;
89 with Strub; use Strub;
90 with Style; use Style;
91 with Targparm; use Targparm;
92 with Tbuild; use Tbuild;
93 with Uintp; use Uintp;
94 with Urealp; use Urealp;
95 with Warnsw; use Warnsw;
97 package body Sem_Res is
99 -----------------------
100 -- Local Subprograms --
101 -----------------------
103 -- Second pass (top-down) type checking and overload resolution procedures
104 -- Typ is the type required by context. These procedures propagate the
105 -- type information recursively to the descendants of N. If the node is not
106 -- overloaded, its Etype is established in the first pass. If overloaded,
107 -- the Resolve routines set the correct type. For arithmetic operators, the
108 -- Etype is the base type of the context.
110 -- Note that Resolve_Attribute is separated off in Sem_Attr
112 function Has_Applicable_User_Defined_Literal
113 (N : Node_Id;
114 Typ : Entity_Id) return Boolean;
115 -- Check whether N is a literal or a named number, and whether Typ has a
116 -- user-defined literal aspect that may apply to N. In this case, replace
117 -- N with a call to the corresponding function and return True.
119 procedure Check_Discriminant_Use (N : Node_Id);
120 -- Enforce the restrictions on the use of discriminants when constraining
121 -- a component of a discriminated type (record or concurrent type).
123 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
124 -- Given a node for an operator associated with type T, check that the
125 -- operator is visible. Operators all of whose operands are universal must
126 -- be checked for visibility during resolution because their type is not
127 -- determinable based on their operands.
129 procedure Check_Fully_Declared_Prefix
130 (Typ : Entity_Id;
131 Pref : Node_Id);
132 -- Check that the type of the prefix of a dereference is not incomplete
134 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
135 -- Given a call node, Call, which is known to occur immediately within the
136 -- subprogram being called, determines whether it is a detectable case of
137 -- an infinite recursion, and if so, outputs appropriate messages. Returns
138 -- True if an infinite recursion is detected, and False otherwise.
140 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
141 -- N is the node for a logical operator. If the operator is predefined, and
142 -- the root type of the operands is Standard.Boolean, then a check is made
143 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
144 -- the style check for Style_Check_Boolean_And_Or.
146 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
147 -- N is either an indexed component or a selected component. This function
148 -- returns true if the prefix denotes an atomic object that has an address
149 -- clause (the case in which we may want to issue a warning).
151 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean;
152 -- Determine whether E is an access type declared by an access declaration,
153 -- and not an (anonymous) allocator type.
155 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
156 -- Utility to check whether the entity for an operator is a predefined
157 -- operator, in which case the expression is left as an operator in the
158 -- tree (else it is rewritten into a call). An instance of an intrinsic
159 -- conversion operation may be given an operator name, but is not treated
160 -- like an operator. Note that an operator that is an imported back-end
161 -- builtin has convention Intrinsic, but is expected to be rewritten into
162 -- a call, so such an operator is not treated as predefined by this
163 -- predicate.
165 procedure Preanalyze_And_Resolve
166 (N : Node_Id;
167 T : Entity_Id;
168 With_Freezing : Boolean);
169 -- Subsidiary of public versions of Preanalyze_And_Resolve.
171 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
172 -- If a default expression in entry call N depends on the discriminants
173 -- of the task, it must be replaced with a reference to the discriminant
174 -- of the task being called.
176 procedure Resolve_Dependent_Expression
177 (N : Node_Id;
178 Expr : Node_Id;
179 Typ : Entity_Id);
180 -- Internal procedure to resolve the dependent expression Expr of the
181 -- conditional expression N with type Typ.
183 procedure Resolve_Op_Concat_Arg
184 (N : Node_Id;
185 Arg : Node_Id;
186 Typ : Entity_Id;
187 Is_Comp : Boolean);
188 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
189 -- concatenation operator. The operand is either of the array type or of
190 -- the component type. If the operand is an aggregate, and the component
191 -- type is composite, this is ambiguous if component type has aggregates.
193 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
194 -- Does the first part of the work of Resolve_Op_Concat
196 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
197 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
198 -- has been resolved. See Resolve_Op_Concat for details.
200 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
215 procedure Resolve_Interpolated_String_Literal
216 (N : Node_Id;
217 Typ : Entity_Id);
218 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
219 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
220 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
221 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
222 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
223 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
224 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
225 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
226 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
227 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
228 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
229 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
230 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
231 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
232 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
233 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
234 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
235 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
236 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
237 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
238 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
239 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
241 function Operator_Kind
242 (Op_Name : Name_Id;
243 Is_Binary : Boolean) return Node_Kind;
244 -- Utility to map the name of an operator into the corresponding Node. Used
245 -- by other node rewriting procedures.
247 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
248 -- Resolve actuals of call, and add default expressions for missing ones.
249 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
250 -- called subprogram.
252 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
253 -- Called from Resolve_Call, when the prefix denotes an entry or element
254 -- of entry family. Actuals are resolved as for subprograms, and the node
255 -- is rebuilt as an entry call. Also called for protected operations. Typ
256 -- is the context type, which is used when the operation is a protected
257 -- function with no arguments, and the return value is indexed.
259 procedure Resolve_Implicit_Dereference (P : Node_Id);
260 -- Called when P is the prefix of an indexed component, or of a selected
261 -- component, or of a slice. If P is of an access type, we unconditionally
262 -- rewrite it as an explicit dereference. This ensures that the expander
263 -- and the code generator have a fully explicit tree to work with.
265 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
266 -- A call to a user-defined intrinsic operator is rewritten as a call to
267 -- the corresponding predefined operator, with suitable conversions. Note
268 -- that this applies only for intrinsic operators that denote predefined
269 -- operators, not ones that are intrinsic imports of back-end builtins.
271 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
272 -- Ditto, for arithmetic unary operators
274 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
275 -- If an operator node resolves to a call to a user-defined operator,
276 -- rewrite the node as a function call.
278 procedure Make_Call_Into_Operator
279 (N : Node_Id;
280 Typ : Entity_Id;
281 Op_Id : Entity_Id);
282 -- Inverse transformation: if an operator is given in functional notation,
283 -- then after resolving the node, transform into an operator node, so that
284 -- operands are resolved properly. Recall that predefined operators do not
285 -- have a full signature and special resolution rules apply.
287 procedure Rewrite_Renamed_Operator
288 (N : Node_Id;
289 Op : Entity_Id;
290 Typ : Entity_Id);
291 -- An operator can rename another, e.g. in an instantiation. In that
292 -- case, the proper operator node must be constructed and resolved.
294 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
295 -- The String_Literal_Subtype is built for all strings that are not
296 -- operands of a static concatenation operation. If the argument is not
297 -- a N_String_Literal node, then the call has no effect.
299 procedure Set_Slice_Subtype (N : Node_Id);
300 -- Build subtype of array type, with the range specified by the slice
302 procedure Simplify_Type_Conversion (N : Node_Id);
303 -- Called after N has been resolved and evaluated, but before range checks
304 -- have been applied. This rewrites the conversion into a simpler form.
306 function Try_User_Defined_Literal
307 (N : Node_Id;
308 Typ : Entity_Id) return Boolean;
309 -- If the node is a literal or a named number or a conditional expression
310 -- whose dependent expressions are all literals or named numbers, and the
311 -- context type has a user-defined literal aspect, then rewrite the node
312 -- or its leaf nodes as calls to the corresponding function, which plays
313 -- the role of an implicit conversion.
315 function Try_User_Defined_Literal_For_Operator
316 (N : Node_Id;
317 Typ : Entity_Id) return Boolean;
318 -- If an operator node has a literal operand, check whether the type of the
319 -- context, or that of the other operand has a user-defined literal aspect
320 -- that can be applied to the literal to resolve the node. If such aspect
321 -- exists, replace literal with a call to the corresponding function and
322 -- return True, return false otherwise.
324 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
325 -- A universal_fixed expression in an universal context is unambiguous if
326 -- there is only one applicable fixed point type. Determining whether there
327 -- is only one requires a search over all visible entities, and happens
328 -- only in very pathological cases (see 6115-006).
330 -------------------------
331 -- Ambiguous_Character --
332 -------------------------
334 procedure Ambiguous_Character (C : Node_Id) is
335 E : Entity_Id;
337 begin
338 if Nkind (C) = N_Character_Literal then
339 Error_Msg_N ("ambiguous character literal", C);
341 -- First the ones in Standard
343 Error_Msg_N ("\\possible interpretation: Character!", C);
344 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
346 -- Include Wide_Wide_Character in Ada 2005 mode
348 if Ada_Version >= Ada_2005 then
349 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
350 end if;
352 -- Now any other types that match
354 E := Current_Entity (C);
355 while Present (E) loop
356 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
357 E := Homonym (E);
358 end loop;
359 end if;
360 end Ambiguous_Character;
362 -------------------------
363 -- Analyze_And_Resolve --
364 -------------------------
366 procedure Analyze_And_Resolve (N : Node_Id) is
367 begin
368 Analyze (N);
369 Resolve (N);
370 end Analyze_And_Resolve;
372 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
373 begin
374 Analyze (N);
375 Resolve (N, Typ);
376 end Analyze_And_Resolve;
378 -- Versions with check(s) suppressed
380 procedure Analyze_And_Resolve
381 (N : Node_Id;
382 Typ : Entity_Id;
383 Suppress : Check_Id)
385 Scop : constant Entity_Id := Current_Scope;
387 begin
388 if Suppress = All_Checks then
389 declare
390 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
391 begin
392 Scope_Suppress.Suppress := (others => True);
393 Analyze_And_Resolve (N, Typ);
394 Scope_Suppress.Suppress := Sva;
395 end;
397 else
398 declare
399 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
400 begin
401 Scope_Suppress.Suppress (Suppress) := True;
402 Analyze_And_Resolve (N, Typ);
403 Scope_Suppress.Suppress (Suppress) := Svg;
404 end;
405 end if;
407 if Current_Scope /= Scop
408 and then Scope_Is_Transient
409 then
410 -- This can only happen if a transient scope was created for an inner
411 -- expression, which will be removed upon completion of the analysis
412 -- of an enclosing construct. The transient scope must have the
413 -- suppress status of the enclosing environment, not of this Analyze
414 -- call.
416 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
417 Scope_Suppress;
418 end if;
419 end Analyze_And_Resolve;
421 procedure Analyze_And_Resolve
422 (N : Node_Id;
423 Suppress : Check_Id)
425 Scop : constant Entity_Id := Current_Scope;
427 begin
428 if Suppress = All_Checks then
429 declare
430 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
431 begin
432 Scope_Suppress.Suppress := (others => True);
433 Analyze_And_Resolve (N);
434 Scope_Suppress.Suppress := Sva;
435 end;
437 else
438 declare
439 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
440 begin
441 Scope_Suppress.Suppress (Suppress) := True;
442 Analyze_And_Resolve (N);
443 Scope_Suppress.Suppress (Suppress) := Svg;
444 end;
445 end if;
447 if Current_Scope /= Scop and then Scope_Is_Transient then
448 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
449 Scope_Suppress;
450 end if;
451 end Analyze_And_Resolve;
453 -------------------------------------
454 -- Has_Applicable_User_Defined_Literal --
455 -------------------------------------
457 function Has_Applicable_User_Defined_Literal
458 (N : Node_Id;
459 Typ : Entity_Id) return Boolean
461 Loc : constant Source_Ptr := Sloc (N);
462 Literal_Aspect_Map :
463 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
464 (N_Integer_Literal => Aspect_Integer_Literal,
465 N_Interpolated_String_Literal => No_Aspect,
466 N_Real_Literal => Aspect_Real_Literal,
467 N_String_Literal => Aspect_String_Literal);
469 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
470 (E_Named_Integer => Aspect_Integer_Literal,
471 E_Named_Real => Aspect_Real_Literal);
473 Lit_Aspect : Aspect_Id;
475 Callee : Entity_Id;
476 Name : Node_Id;
477 Param1 : Node_Id;
478 Param2 : Node_Id;
479 Params : List_Id;
480 Call : Node_Id;
481 Expr : Node_Id;
483 begin
484 if (Nkind (N) in N_Numeric_Or_String_Literal
485 and then Present
486 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
487 or else
488 (Nkind (N) = N_Identifier
489 and then Is_Named_Number (Entity (N))
490 and then
491 Present
492 (Find_Aspect
493 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
494 then
495 Lit_Aspect :=
496 (if Nkind (N) = N_Identifier
497 then Named_Number_Aspect_Map (Ekind (Entity (N)))
498 else Literal_Aspect_Map (Nkind (N)));
499 Callee :=
500 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
501 Name := Make_Identifier (Loc, Chars (Callee));
503 if Is_Derived_Type (Typ)
504 and then Base_Type (Etype (Callee)) /= Base_Type (Typ)
505 then
506 Callee :=
507 Corresponding_Primitive_Op
508 (Ancestor_Op => Callee,
509 Descendant_Type => Base_Type (Typ));
510 end if;
512 -- Handle an identifier that denotes a named number.
514 if Nkind (N) = N_Identifier then
515 Expr := Expression (Declaration_Node (Entity (N)));
517 if Ekind (Entity (N)) = E_Named_Integer then
518 UI_Image (Expr_Value (Expr), Decimal);
519 Start_String;
520 Store_String_Chars
521 (UI_Image_Buffer (1 .. UI_Image_Length));
522 Param1 := Make_String_Literal (Loc, End_String);
523 Params := New_List (Param1);
525 else
526 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
527 Start_String;
529 if UR_Is_Negative (Expr_Value_R (Expr)) then
530 Store_String_Chars ("-");
531 end if;
533 Store_String_Chars
534 (UI_Image_Buffer (1 .. UI_Image_Length));
535 Param1 := Make_String_Literal (Loc, End_String);
537 -- Note: Set_Etype is called below on Param1
539 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
540 Start_String;
541 Store_String_Chars
542 (UI_Image_Buffer (1 .. UI_Image_Length));
543 Param2 := Make_String_Literal (Loc, End_String);
544 Set_Etype (Param2, Standard_String);
546 Params := New_List (Param1, Param2);
548 if Present (Related_Expression (Callee)) then
549 Callee := Related_Expression (Callee);
550 else
551 Error_Msg_NE
552 ("cannot resolve & for a named real", N, Callee);
553 return False;
554 end if;
555 end if;
557 elsif Nkind (N) = N_String_Literal then
558 Param1 := Make_String_Literal (Loc, Strval (N));
559 Params := New_List (Param1);
561 else
562 Param1 :=
563 Make_String_Literal
564 (Loc, String_From_Numeric_Literal (N));
565 Params := New_List (Param1);
566 end if;
568 Call :=
569 Make_Function_Call
570 (Sloc => Loc,
571 Name => Name,
572 Parameter_Associations => Params);
574 Set_Entity (Name, Callee);
575 Set_Is_Overloaded (Name, False);
577 if Lit_Aspect = Aspect_String_Literal then
578 Set_Etype (Param1, Standard_Wide_Wide_String);
579 else
580 Set_Etype (Param1, Standard_String);
581 end if;
583 Set_Etype (Call, Etype (Callee));
585 -- Conversion not needed if the result type of the call is class-wide
586 -- or if the result type matches the context type.
588 if not Is_Class_Wide_Type (Typ)
589 and then Base_Type (Etype (Call)) /= Base_Type (Typ)
590 then
591 -- Conversion may be needed in case of an inherited
592 -- aspect of a derived type. For a null extension, we
593 -- use a null extension aggregate instead because the
594 -- downward type conversion would be illegal.
596 if Is_Null_Extension_Of
597 (Descendant => Typ,
598 Ancestor => Etype (Call))
599 then
600 Call := Make_Extension_Aggregate (Loc,
601 Ancestor_Part => Call,
602 Null_Record_Present => True);
603 else
604 Call := Convert_To (Typ, Call);
605 end if;
606 end if;
608 Rewrite (N, Call);
610 Analyze_And_Resolve (N, Typ);
611 return True;
613 else
614 return False;
615 end if;
616 end Has_Applicable_User_Defined_Literal;
618 ----------------------------
619 -- Check_Discriminant_Use --
620 ----------------------------
622 procedure Check_Discriminant_Use (N : Node_Id) is
623 PN : constant Node_Id := Parent (N);
624 Disc : constant Entity_Id := Entity (N);
625 P : Node_Id;
626 D : Node_Id;
628 begin
629 -- Any use in a spec-expression is legal
631 if In_Spec_Expression then
632 null;
634 elsif Nkind (PN) = N_Range then
636 -- Discriminant cannot be used to constrain a scalar type
638 P := Parent (PN);
640 if Nkind (P) = N_Range_Constraint
641 and then Nkind (Parent (P)) = N_Subtype_Indication
642 and then Nkind (Parent (Parent (P))) = N_Component_Definition
643 then
644 Error_Msg_N ("discriminant cannot constrain scalar type", N);
646 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
648 -- The following check catches the unusual case where a
649 -- discriminant appears within an index constraint that is part
650 -- of a larger expression within a constraint on a component,
651 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
652 -- check case of record components, and note that a similar check
653 -- should also apply in the case of discriminant constraints
654 -- below. ???
656 -- Note that the check for N_Subtype_Declaration below is to
657 -- detect the valid use of discriminants in the constraints of a
658 -- subtype declaration when this subtype declaration appears
659 -- inside the scope of a record type (which is syntactically
660 -- illegal, but which may be created as part of derived type
661 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
662 -- for more info.
664 if Ekind (Current_Scope) = E_Record_Type
665 and then Scope (Disc) = Current_Scope
666 and then not
667 (Nkind (Parent (P)) = N_Subtype_Indication
668 and then
669 Nkind (Parent (Parent (P))) in N_Component_Definition
670 | N_Subtype_Declaration
671 and then Paren_Count (N) = 0)
672 then
673 Error_Msg_N
674 ("discriminant must appear alone in component constraint", N);
675 return;
676 end if;
678 -- Detect a common error:
680 -- type R (D : Positive := 100) is record
681 -- Name : String (1 .. D);
682 -- end record;
684 -- The default value causes an object of type R to be allocated
685 -- with room for Positive'Last characters. The RM does not mandate
686 -- the allocation of the maximum size, but that is what GNAT does
687 -- so we should warn the programmer that there is a problem.
689 Check_Large : declare
690 SI : Node_Id;
691 T : Entity_Id;
692 TB : Node_Id;
693 CB : Entity_Id;
695 function Large_Storage_Type (T : Entity_Id) return Boolean;
696 -- Return True if type T has a large enough range that any
697 -- array whose index type covered the whole range of the type
698 -- would likely raise Storage_Error.
700 ------------------------
701 -- Large_Storage_Type --
702 ------------------------
704 function Large_Storage_Type (T : Entity_Id) return Boolean is
705 begin
706 -- The type is considered large if its bounds are known at
707 -- compile time and if it requires at least as many bits as
708 -- a Positive to store the possible values.
710 return Compile_Time_Known_Value (Type_Low_Bound (T))
711 and then Compile_Time_Known_Value (Type_High_Bound (T))
712 and then
713 Minimum_Size (T, Biased => True) >=
714 RM_Size (Standard_Positive);
715 end Large_Storage_Type;
717 -- Start of processing for Check_Large
719 begin
720 -- Check that the Disc has a large range
722 if not Large_Storage_Type (Etype (Disc)) then
723 goto No_Danger;
724 end if;
726 -- If the enclosing type is limited, we allocate only the
727 -- default value, not the maximum, and there is no need for
728 -- a warning.
730 if Is_Limited_Type (Scope (Disc)) then
731 goto No_Danger;
732 end if;
734 -- Check that it is the high bound
736 if N /= High_Bound (PN)
737 or else No (Discriminant_Default_Value (Disc))
738 then
739 goto No_Danger;
740 end if;
742 -- Check the array allows a large range at this bound. First
743 -- find the array
745 SI := Parent (P);
747 if Nkind (SI) /= N_Subtype_Indication then
748 goto No_Danger;
749 end if;
751 T := Entity (Subtype_Mark (SI));
753 if not Is_Array_Type (T) then
754 goto No_Danger;
755 end if;
757 -- Next, find the dimension
759 TB := First_Index (T);
760 CB := First (Constraints (P));
761 while True
762 and then Present (TB)
763 and then Present (CB)
764 and then CB /= PN
765 loop
766 Next_Index (TB);
767 Next (CB);
768 end loop;
770 if CB /= PN then
771 goto No_Danger;
772 end if;
774 -- Now, check the dimension has a large range
776 if not Large_Storage_Type (Etype (TB)) then
777 goto No_Danger;
778 end if;
780 -- Warn about the danger
782 Error_Msg_N
783 ("??creation of & object may raise Storage_Error!",
784 Scope (Disc));
786 <<No_Danger>>
787 null;
789 end Check_Large;
790 end if;
792 -- Legal case is in index or discriminant constraint
794 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
795 | N_Discriminant_Association
796 then
797 if Paren_Count (N) > 0 then
798 Error_Msg_N
799 ("discriminant in constraint must appear alone", N);
801 elsif Nkind (N) = N_Expanded_Name
802 and then Comes_From_Source (N)
803 then
804 Error_Msg_N
805 ("discriminant must appear alone as a direct name", N);
806 end if;
808 return;
810 -- Otherwise, context is an expression. It should not be within (i.e. a
811 -- subexpression of) a constraint for a component.
813 else
814 D := PN;
815 P := Parent (PN);
816 while Nkind (P) not in
817 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
818 loop
819 D := P;
820 P := Parent (P);
821 exit when No (P);
822 end loop;
824 -- If the discriminant is used in an expression that is a bound of a
825 -- scalar type, an Itype is created and the bounds are attached to
826 -- its range, not to the original subtype indication. Such use is of
827 -- course a double fault.
829 if (Nkind (P) = N_Subtype_Indication
830 and then Nkind (Parent (P)) in N_Component_Definition
831 | N_Derived_Type_Definition
832 and then D = Constraint (P))
834 -- The constraint itself may be given by a subtype indication,
835 -- rather than by a more common discrete range.
837 or else (Nkind (P) = N_Subtype_Indication
838 and then
839 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
840 or else Nkind (P) = N_Entry_Declaration
841 or else Nkind (D) = N_Defining_Identifier
842 then
843 Error_Msg_N
844 ("discriminant in constraint must appear alone", N);
845 end if;
846 end if;
847 end Check_Discriminant_Use;
849 --------------------------------
850 -- Check_For_Visible_Operator --
851 --------------------------------
853 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
854 begin
855 if Comes_From_Source (N)
856 and then not Is_Visible_Operator (Original_Node (N), T)
857 and then not Error_Posted (N)
858 then
859 Error_Msg_NE -- CODEFIX
860 ("operator for} is not directly visible!", N, First_Subtype (T));
861 Error_Msg_N -- CODEFIX
862 ("use clause would make operation legal!", N);
863 end if;
864 end Check_For_Visible_Operator;
866 ---------------------------------
867 -- Check_Fully_Declared_Prefix --
868 ---------------------------------
870 procedure Check_Fully_Declared_Prefix
871 (Typ : Entity_Id;
872 Pref : Node_Id)
874 begin
875 -- Check that the designated type of the prefix of a dereference is
876 -- not an incomplete type. This cannot be done unconditionally, because
877 -- dereferences of private types are legal in default expressions. This
878 -- case is taken care of in Check_Fully_Declared, called below. There
879 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
881 -- This consideration also applies to similar checks for allocators,
882 -- qualified expressions, and type conversions.
884 -- An additional exception concerns other per-object expressions that
885 -- are not directly related to component declarations, in particular
886 -- representation pragmas for tasks. These will be per-object
887 -- expressions if they depend on discriminants or some global entity.
888 -- If the task has access discriminants, the designated type may be
889 -- incomplete at the point the expression is resolved. This resolution
890 -- takes place within the body of the initialization procedure, where
891 -- the discriminant is replaced by its discriminal.
893 if Is_Entity_Name (Pref)
894 and then Ekind (Entity (Pref)) = E_In_Parameter
895 then
896 null;
898 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
899 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
900 -- Analyze_Object_Renaming, and Freeze_Entity.
902 elsif Ada_Version >= Ada_2005
903 and then Is_Entity_Name (Pref)
904 and then Is_Access_Type (Etype (Pref))
905 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
906 E_Incomplete_Type
907 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
908 then
909 null;
910 else
911 Check_Fully_Declared (Typ, Parent (Pref));
912 end if;
913 end Check_Fully_Declared_Prefix;
915 ------------------------------
916 -- Check_Infinite_Recursion --
917 ------------------------------
919 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
920 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
921 -- Determine whether call N invokes the related enclosing subprogram
922 -- with actuals that differ from the subprogram's formals.
924 function Is_Conditional_Statement (N : Node_Id) return Boolean;
925 -- Determine whether arbitrary node N denotes a conditional construct
927 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
928 -- Determine whether arbitrary node N denotes a control flow statement
929 -- or a construct that may contains such a statement.
931 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
932 -- Determine whether arbitrary node N appears immediately within the
933 -- statements of an entry or subprogram body.
935 function Is_Raise_Idiom (N : Node_Id) return Boolean;
936 -- Determine whether arbitrary node N appears immediately within the
937 -- body of an entry or subprogram, and is preceded by a single raise
938 -- statement.
940 function Is_Raise_Statement (N : Node_Id) return Boolean;
941 -- Determine whether arbitrary node N denotes a raise statement
943 function Is_Sole_Statement (N : Node_Id) return Boolean;
944 -- Determine whether arbitrary node N is the sole source statement in
945 -- the body of the enclosing subprogram.
947 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
948 -- Determine whether arbitrary node N is preceded by a control flow
949 -- statement.
951 function Within_Conditional_Statement (N : Node_Id) return Boolean;
952 -- Determine whether arbitrary node N appears within a conditional
953 -- construct.
955 --------------------------------------
956 -- Invoked_With_Different_Arguments --
957 --------------------------------------
959 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
960 Subp : constant Entity_Id := Get_Called_Entity (N);
962 Actual : Node_Id;
963 Formal : Entity_Id;
965 begin
966 -- Determine whether the formals of the invoked subprogram are not
967 -- used as actuals in the call.
969 Actual := First_Actual (N);
970 Formal := First_Formal (Subp);
971 while Present (Actual) and then Present (Formal) loop
973 -- The current actual does not match the current formal
975 if not (Is_Entity_Name (Actual)
976 and then Entity (Actual) = Formal)
977 then
978 return True;
979 end if;
981 Next_Actual (Actual);
982 Next_Formal (Formal);
983 end loop;
985 return False;
986 end Invoked_With_Different_Arguments;
988 ------------------------------
989 -- Is_Conditional_Statement --
990 ------------------------------
992 function Is_Conditional_Statement (N : Node_Id) return Boolean is
993 begin
994 return
995 Nkind (N) in N_And_Then
996 | N_Case_Expression
997 | N_Case_Statement
998 | N_If_Expression
999 | N_If_Statement
1000 | N_Or_Else;
1001 end Is_Conditional_Statement;
1003 -------------------------------
1004 -- Is_Control_Flow_Statement --
1005 -------------------------------
1007 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
1008 begin
1009 -- It is assumed that all statements may affect the control flow in
1010 -- some way. A raise statement may be expanded into a non-statement
1011 -- node.
1013 return Is_Statement (N) or else Is_Raise_Statement (N);
1014 end Is_Control_Flow_Statement;
1016 --------------------------------
1017 -- Is_Immediately_Within_Body --
1018 --------------------------------
1020 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
1021 HSS : constant Node_Id := Parent (N);
1023 begin
1024 return
1025 Nkind (HSS) = N_Handled_Sequence_Of_Statements
1026 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
1027 and then Is_List_Member (N)
1028 and then List_Containing (N) = Statements (HSS);
1029 end Is_Immediately_Within_Body;
1031 --------------------
1032 -- Is_Raise_Idiom --
1033 --------------------
1035 function Is_Raise_Idiom (N : Node_Id) return Boolean is
1036 Raise_Stmt : Node_Id;
1037 Stmt : Node_Id;
1039 begin
1040 if Is_Immediately_Within_Body (N) then
1042 -- Assume that no raise statement has been seen yet
1044 Raise_Stmt := Empty;
1046 -- Examine the statements preceding the input node, skipping
1047 -- internally-generated constructs.
1049 Stmt := Prev (N);
1050 while Present (Stmt) loop
1052 -- Multiple raise statements violate the idiom
1054 if Is_Raise_Statement (Stmt) then
1055 if Present (Raise_Stmt) then
1056 return False;
1057 end if;
1059 Raise_Stmt := Stmt;
1061 elsif Comes_From_Source (Stmt) then
1062 exit;
1063 end if;
1065 Stmt := Prev (Stmt);
1066 end loop;
1068 -- At this point the node must be preceded by a raise statement,
1069 -- and the raise statement has to be the sole statement within
1070 -- the enclosing entry or subprogram body.
1072 return
1073 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
1074 end if;
1076 return False;
1077 end Is_Raise_Idiom;
1079 ------------------------
1080 -- Is_Raise_Statement --
1081 ------------------------
1083 function Is_Raise_Statement (N : Node_Id) return Boolean is
1084 begin
1085 -- A raise statement may be transfomed into a Raise_xxx_Error node
1087 return
1088 Nkind (N) = N_Raise_Statement
1089 or else Nkind (N) in N_Raise_xxx_Error;
1090 end Is_Raise_Statement;
1092 -----------------------
1093 -- Is_Sole_Statement --
1094 -----------------------
1096 function Is_Sole_Statement (N : Node_Id) return Boolean is
1097 Stmt : Node_Id;
1099 begin
1100 -- The input node appears within the statements of an entry or
1101 -- subprogram body. Examine the statements preceding the node.
1103 if Is_Immediately_Within_Body (N) then
1104 Stmt := Prev (N);
1106 while Present (Stmt) loop
1108 -- The statement is preceded by another statement or a source
1109 -- construct. This indicates that the node does not appear by
1110 -- itself.
1112 if Is_Control_Flow_Statement (Stmt)
1113 or else Comes_From_Source (Stmt)
1114 then
1115 return False;
1116 end if;
1118 Stmt := Prev (Stmt);
1119 end loop;
1121 return True;
1122 end if;
1124 -- The input node is within a construct nested inside the entry or
1125 -- subprogram body.
1127 return False;
1128 end Is_Sole_Statement;
1130 ----------------------------------------
1131 -- Preceded_By_Control_Flow_Statement --
1132 ----------------------------------------
1134 function Preceded_By_Control_Flow_Statement
1135 (N : Node_Id) return Boolean
1137 Stmt : Node_Id;
1139 begin
1140 if Is_List_Member (N) then
1141 Stmt := Prev (N);
1143 -- Examine the statements preceding the input node
1145 while Present (Stmt) loop
1146 if Is_Control_Flow_Statement (Stmt) then
1147 return True;
1148 end if;
1150 Stmt := Prev (Stmt);
1151 end loop;
1153 return False;
1154 end if;
1156 -- Assume that the node is part of some control flow statement
1158 return True;
1159 end Preceded_By_Control_Flow_Statement;
1161 ----------------------------------
1162 -- Within_Conditional_Statement --
1163 ----------------------------------
1165 function Within_Conditional_Statement (N : Node_Id) return Boolean is
1166 Stmt : Node_Id;
1168 begin
1169 Stmt := Parent (N);
1170 while Present (Stmt) loop
1171 if Is_Conditional_Statement (Stmt) then
1172 return True;
1174 -- Prevent the search from going too far
1176 elsif Is_Body_Or_Package_Declaration (Stmt) then
1177 exit;
1178 end if;
1180 Stmt := Parent (Stmt);
1181 end loop;
1183 return False;
1184 end Within_Conditional_Statement;
1186 -- Local variables
1188 Call_Context : constant Node_Id :=
1189 Enclosing_Declaration_Or_Statement (Call);
1191 -- Start of processing for Check_Infinite_Recursion
1193 begin
1194 -- The call is assumed to be safe when the enclosing subprogram is
1195 -- invoked with actuals other than its formals.
1197 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1198 -- begin
1199 -- ...
1200 -- Proc (A1, A2, ..., AN);
1201 -- ...
1202 -- end Proc;
1204 if Invoked_With_Different_Arguments (Call) then
1205 return False;
1207 -- The call is assumed to be safe when the invocation of the enclosing
1208 -- subprogram depends on a conditional statement.
1210 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1211 -- begin
1212 -- ...
1213 -- if Some_Condition then
1214 -- Proc (F1, F2, ..., FN);
1215 -- end if;
1216 -- ...
1217 -- end Proc;
1219 elsif Within_Conditional_Statement (Call) then
1220 return False;
1222 -- The context of the call is assumed to be safe when the invocation of
1223 -- the enclosing subprogram is preceded by some control flow statement.
1225 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1226 -- begin
1227 -- ...
1228 -- if Some_Condition then
1229 -- ...
1230 -- end if;
1231 -- ...
1232 -- Proc (F1, F2, ..., FN);
1233 -- ...
1234 -- end Proc;
1236 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1237 return False;
1239 -- Detect an idiom where the context of the call is preceded by a single
1240 -- raise statement.
1242 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1243 -- begin
1244 -- raise ...;
1245 -- Proc (F1, F2, ..., FN);
1246 -- end Proc;
1248 elsif Is_Raise_Idiom (Call_Context) then
1249 return False;
1250 end if;
1252 -- At this point it is certain that infinite recursion will take place
1253 -- as long as the call is executed. Detect a case where the context of
1254 -- the call is the sole source statement within the subprogram body.
1256 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1257 -- begin
1258 -- Proc (F1, F2, ..., FN);
1259 -- end Proc;
1261 -- Install an explicit raise to prevent the infinite recursion.
1263 if Is_Sole_Statement (Call_Context) then
1264 Error_Msg_Warn := SPARK_Mode /= On;
1265 Error_Msg_N ("!infinite recursion<<", Call);
1266 Error_Msg_N ("\!Storage_Error [<<", Call);
1268 Insert_Action (Call,
1269 Make_Raise_Storage_Error (Sloc (Call),
1270 Reason => SE_Infinite_Recursion));
1272 -- Otherwise infinite recursion could take place, considering other flow
1273 -- control constructs such as gotos, exit statements, etc.
1275 else
1276 Error_Msg_Warn := SPARK_Mode /= On;
1277 Error_Msg_N ("!possible infinite recursion<<", Call);
1278 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1279 end if;
1281 return True;
1282 end Check_Infinite_Recursion;
1284 ---------------------------------------
1285 -- Check_No_Direct_Boolean_Operators --
1286 ---------------------------------------
1288 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1289 begin
1290 if Scope (Entity (N)) = Standard_Standard
1291 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1292 then
1293 -- Restriction only applies to original source code
1295 if Comes_From_Source (N) then
1296 Check_Restriction (No_Direct_Boolean_Operators, N);
1297 end if;
1298 end if;
1300 -- Do style check (but skip if in instance, error is on template)
1302 if Style_Check then
1303 if not In_Instance then
1304 Check_Boolean_Operator (N);
1305 end if;
1306 end if;
1307 end Check_No_Direct_Boolean_Operators;
1309 ------------------------------
1310 -- Check_Parameterless_Call --
1311 ------------------------------
1313 procedure Check_Parameterless_Call (N : Node_Id) is
1314 Nam : Node_Id;
1316 function Prefix_Is_Access_Subp return Boolean;
1317 -- If the prefix is of an access_to_subprogram type, the node must be
1318 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1319 -- interpretations are access to subprograms.
1321 ---------------------------
1322 -- Prefix_Is_Access_Subp --
1323 ---------------------------
1325 function Prefix_Is_Access_Subp return Boolean is
1326 I : Interp_Index;
1327 It : Interp;
1329 begin
1330 -- If the context is an attribute reference that can apply to
1331 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1333 if Nkind (Parent (N)) = N_Attribute_Reference
1334 and then Attribute_Name (Parent (N))
1335 in Name_Address | Name_Code_Address | Name_Access
1336 then
1337 return False;
1338 end if;
1340 if not Is_Overloaded (N) then
1341 return
1342 Ekind (Etype (N)) = E_Subprogram_Type
1343 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1344 else
1345 Get_First_Interp (N, I, It);
1346 while Present (It.Typ) loop
1347 if Ekind (It.Typ) /= E_Subprogram_Type
1348 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1349 then
1350 return False;
1351 end if;
1353 Get_Next_Interp (I, It);
1354 end loop;
1356 return True;
1357 end if;
1358 end Prefix_Is_Access_Subp;
1360 -- Start of processing for Check_Parameterless_Call
1362 begin
1363 -- Defend against junk stuff if errors already detected
1365 if Total_Errors_Detected /= 0 then
1366 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1367 return;
1368 elsif Nkind (N) in N_Has_Chars
1369 and then not Is_Valid_Name (Chars (N))
1370 then
1371 return;
1372 end if;
1374 Require_Entity (N);
1375 end if;
1377 -- If the context expects a value, and the name is a procedure, this is
1378 -- most likely a missing 'Access. Don't try to resolve the parameterless
1379 -- call, error will be caught when the outer call is analyzed.
1381 if Is_Entity_Name (N)
1382 and then Ekind (Entity (N)) = E_Procedure
1383 and then not Is_Overloaded (N)
1384 and then
1385 Nkind (Parent (N)) in N_Parameter_Association
1386 | N_Function_Call
1387 | N_Procedure_Call_Statement
1388 then
1389 return;
1390 end if;
1392 -- Rewrite as call if overloadable entity that is (or could be, in the
1393 -- overloaded case) a function call. If we know for sure that the entity
1394 -- is an enumeration literal, we do not rewrite it.
1396 -- If the entity is the name of an operator, it cannot be a call because
1397 -- operators cannot have default parameters. In this case, this must be
1398 -- a string whose contents coincide with an operator name. Set the kind
1399 -- of the node appropriately.
1401 if (Is_Entity_Name (N)
1402 and then Nkind (N) /= N_Operator_Symbol
1403 and then Is_Overloadable (Entity (N))
1404 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1405 or else Is_Overloaded (N)))
1407 -- Rewrite as call if it is an explicit dereference of an expression of
1408 -- a subprogram access type, and the subprogram type is not that of a
1409 -- procedure or entry.
1411 or else
1412 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1414 -- Rewrite as call if it is a selected component which is a function,
1415 -- this is the case of a call to a protected function (which may be
1416 -- overloaded with other protected operations).
1418 or else
1419 (Nkind (N) = N_Selected_Component
1420 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1421 or else
1422 (Ekind (Entity (Selector_Name (N))) in
1423 E_Entry | E_Procedure
1424 and then Is_Overloaded (Selector_Name (N)))))
1426 -- If one of the above three conditions is met, rewrite as call. Apply
1427 -- the rewriting only once.
1429 then
1430 if Nkind (Parent (N)) /= N_Function_Call
1431 or else N /= Name (Parent (N))
1432 then
1434 -- This may be a prefixed call that was not fully analyzed, e.g.
1435 -- an actual in an instance.
1437 if Ada_Version >= Ada_2005
1438 and then Nkind (N) = N_Selected_Component
1439 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1440 then
1441 Analyze_Selected_Component (N);
1443 if Nkind (N) /= N_Selected_Component then
1444 return;
1445 end if;
1446 end if;
1448 -- The node is the name of the parameterless call. Preserve its
1449 -- descendants, which may be complex expressions.
1451 Nam := Relocate_Node (N);
1453 -- If overloaded, overload set belongs to new copy
1455 Save_Interps (N, Nam);
1457 -- Change node to parameterless function call (note that the
1458 -- Parameter_Associations associations field is left set to Empty,
1459 -- its normal default value since there are no parameters)
1461 Change_Node (N, N_Function_Call);
1462 Set_Name (N, Nam);
1463 Set_Sloc (N, Sloc (Nam));
1464 Analyze_Call (N);
1465 end if;
1467 elsif Nkind (N) = N_Parameter_Association then
1468 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1470 elsif Nkind (N) = N_Operator_Symbol then
1471 Set_Etype (N, Empty);
1472 Set_Entity (N, Empty);
1473 Set_Is_Overloaded (N, False);
1474 Change_Operator_Symbol_To_String_Literal (N);
1475 Set_Etype (N, Any_String);
1476 end if;
1477 end Check_Parameterless_Call;
1479 --------------------------------
1480 -- Is_Atomic_Ref_With_Address --
1481 --------------------------------
1483 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1484 Pref : constant Node_Id := Prefix (N);
1486 begin
1487 if not Is_Entity_Name (Pref) then
1488 return False;
1490 else
1491 declare
1492 Pent : constant Entity_Id := Entity (Pref);
1493 Ptyp : constant Entity_Id := Etype (Pent);
1494 begin
1495 return not Is_Access_Type (Ptyp)
1496 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1497 and then Present (Address_Clause (Pent));
1498 end;
1499 end if;
1500 end Is_Atomic_Ref_With_Address;
1502 -----------------------------
1503 -- Is_Definite_Access_Type --
1504 -----------------------------
1506 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean is
1507 Btyp : constant Entity_Id := Base_Type (E);
1508 begin
1509 return Ekind (Btyp) = E_Access_Type
1510 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1511 and then Comes_From_Source (Btyp));
1512 end Is_Definite_Access_Type;
1514 ----------------------
1515 -- Is_Predefined_Op --
1516 ----------------------
1518 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1519 begin
1520 -- Predefined operators are intrinsic subprograms
1522 if not Is_Intrinsic_Subprogram (Nam) then
1523 return False;
1524 end if;
1526 -- A call to a back-end builtin is never a predefined operator
1528 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1529 return False;
1530 end if;
1532 return not Is_Generic_Instance (Nam)
1533 and then Chars (Nam) in Any_Operator_Name
1534 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1535 end Is_Predefined_Op;
1537 -----------------------------
1538 -- Make_Call_Into_Operator --
1539 -----------------------------
1541 procedure Make_Call_Into_Operator
1542 (N : Node_Id;
1543 Typ : Entity_Id;
1544 Op_Id : Entity_Id)
1546 Op_Name : constant Name_Id := Chars (Op_Id);
1547 Act1 : Node_Id := First_Actual (N);
1548 Act2 : Node_Id := Next_Actual (Act1);
1549 Error : Boolean := False;
1550 Func : constant Entity_Id := Entity (Name (N));
1551 Is_Binary : constant Boolean := Present (Act2);
1552 Op_Node : Node_Id;
1553 Opnd_Type : Entity_Id := Empty;
1554 Orig_Type : Entity_Id := Empty;
1555 Pack : Entity_Id;
1557 type Kind_Test is access function (E : N_Entity_Id) return Boolean;
1559 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1560 -- If the operand is not universal, and the operator is given by an
1561 -- expanded name, verify that the operand has an interpretation with a
1562 -- type defined in the given scope of the operator.
1564 function Type_In_P (Test : Kind_Test) return Entity_Id;
1565 -- Find a type of the given class in package Pack that contains the
1566 -- operator.
1568 ---------------------------
1569 -- Operand_Type_In_Scope --
1570 ---------------------------
1572 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1573 Nod : constant Node_Id := Right_Opnd (Op_Node);
1574 I : Interp_Index;
1575 It : Interp;
1577 begin
1578 if not Is_Overloaded (Nod) then
1579 return Scope (Base_Type (Etype (Nod))) = S;
1581 else
1582 Get_First_Interp (Nod, I, It);
1583 while Present (It.Typ) loop
1584 if Scope (Base_Type (It.Typ)) = S then
1585 return True;
1586 end if;
1588 Get_Next_Interp (I, It);
1589 end loop;
1591 return False;
1592 end if;
1593 end Operand_Type_In_Scope;
1595 ---------------
1596 -- Type_In_P --
1597 ---------------
1599 function Type_In_P (Test : Kind_Test) return Entity_Id is
1600 E : Entity_Id;
1602 function In_Decl return Boolean;
1603 -- Verify that node is not part of the type declaration for the
1604 -- candidate type, which would otherwise be invisible.
1606 -------------
1607 -- In_Decl --
1608 -------------
1610 function In_Decl return Boolean is
1611 Decl_Node : constant Node_Id := Parent (E);
1612 N2 : Node_Id;
1614 begin
1615 N2 := N;
1617 if Etype (E) = Any_Type then
1618 return True;
1620 elsif No (Decl_Node) then
1621 return False;
1623 else
1624 while Present (N2)
1625 and then Nkind (N2) /= N_Compilation_Unit
1626 loop
1627 if N2 = Decl_Node then
1628 return True;
1629 else
1630 N2 := Parent (N2);
1631 end if;
1632 end loop;
1634 return False;
1635 end if;
1636 end In_Decl;
1638 -- Start of processing for Type_In_P
1640 begin
1641 -- If the context type is declared in the prefix package, this is the
1642 -- desired base type.
1644 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1645 return Base_Type (Typ);
1647 else
1648 E := First_Entity (Pack);
1649 while Present (E) loop
1650 if Test (E) and then not In_Decl then
1651 return E;
1652 end if;
1654 Next_Entity (E);
1655 end loop;
1657 return Empty;
1658 end if;
1659 end Type_In_P;
1661 -- Start of processing for Make_Call_Into_Operator
1663 begin
1664 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1666 -- Preserve the Comes_From_Source flag on the result if the original
1667 -- call came from source. Although it is not strictly the case that the
1668 -- operator as such comes from the source, logically it corresponds
1669 -- exactly to the function call in the source, so it should be marked
1670 -- this way (e.g. to make sure that validity checks work fine).
1672 Preserve_Comes_From_Source (Op_Node, N);
1674 -- Ensure that the corresponding operator has the same parent as the
1675 -- original call. This guarantees that parent traversals performed by
1676 -- the ABE mechanism succeed.
1678 Set_Parent (Op_Node, Parent (N));
1680 -- Binary operator
1682 if Is_Binary then
1683 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1684 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1685 Save_Interps (Act1, Left_Opnd (Op_Node));
1686 Save_Interps (Act2, Right_Opnd (Op_Node));
1687 Act1 := Left_Opnd (Op_Node);
1688 Act2 := Right_Opnd (Op_Node);
1690 -- Unary operator
1692 else
1693 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1694 Save_Interps (Act1, Right_Opnd (Op_Node));
1695 Act1 := Right_Opnd (Op_Node);
1696 end if;
1698 -- If the operator is denoted by an expanded name, and the prefix is
1699 -- not Standard, but the operator is a predefined one whose scope is
1700 -- Standard, then this is an implicit_operator, inserted as an
1701 -- interpretation by the procedure of the same name. This procedure
1702 -- overestimates the presence of implicit operators, because it does
1703 -- not examine the type of the operands. Verify now that the operand
1704 -- type appears in the given scope. If right operand is universal,
1705 -- check the other operand. In the case of concatenation, either
1706 -- argument can be the component type, so check the type of the result.
1707 -- If both arguments are literals, look for a type of the right kind
1708 -- defined in the given scope. This elaborate nonsense is brought to
1709 -- you courtesy of b33302a. The type itself must be frozen, so we must
1710 -- find the type of the proper class in the given scope.
1712 -- A final wrinkle is the multiplication operator for fixed point types,
1713 -- which is defined in Standard only, and not in the scope of the
1714 -- fixed point type itself.
1716 if Nkind (Name (N)) = N_Expanded_Name then
1717 Pack := Entity (Prefix (Name (N)));
1719 -- If this is a package renaming, get renamed entity, which will be
1720 -- the scope of the operands if operaton is type-correct.
1722 if Present (Renamed_Entity (Pack)) then
1723 Pack := Renamed_Entity (Pack);
1724 end if;
1726 -- If the entity being called is defined in the given package, it is
1727 -- a renaming of a predefined operator, and known to be legal.
1729 if Scope (Entity (Name (N))) = Pack
1730 and then Pack /= Standard_Standard
1731 then
1732 null;
1734 -- Visibility does not need to be checked in an instance: if the
1735 -- operator was not visible in the generic it has been diagnosed
1736 -- already, else there is an implicit copy of it in the instance.
1738 elsif In_Instance then
1739 null;
1741 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1742 and then Is_Fixed_Point_Type (Etype (Act1))
1743 and then Is_Fixed_Point_Type (Etype (Act2))
1744 then
1745 if Pack /= Standard_Standard then
1746 Error := True;
1747 end if;
1749 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1750 -- available.
1752 elsif Ada_Version >= Ada_2005
1753 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1754 and then (Is_Anonymous_Access_Type (Etype (Act1))
1755 or else Is_Anonymous_Access_Type (Etype (Act2)))
1756 then
1757 null;
1759 else
1760 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1762 if Op_Name = Name_Op_Concat then
1763 Opnd_Type := Base_Type (Typ);
1765 elsif (Scope (Opnd_Type) = Standard_Standard
1766 and then Is_Binary)
1767 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1768 and then Is_Binary
1769 and then not Comes_From_Source (Opnd_Type))
1770 then
1771 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1772 end if;
1774 if Scope (Opnd_Type) = Standard_Standard then
1776 -- Verify that the scope contains a type that corresponds to
1777 -- the given literal. Optimize the case where Pack is Standard.
1779 if Pack /= Standard_Standard then
1780 if Opnd_Type = Universal_Integer then
1781 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1783 elsif Opnd_Type = Universal_Real then
1784 Orig_Type := Type_In_P (Is_Real_Type'Access);
1786 elsif Opnd_Type = Universal_Access then
1787 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1789 elsif Opnd_Type = Any_String then
1790 Orig_Type := Type_In_P (Is_String_Type'Access);
1792 elsif Opnd_Type = Any_Composite then
1793 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1795 if Present (Orig_Type) then
1796 if Has_Private_Component (Orig_Type) then
1797 Orig_Type := Empty;
1798 else
1799 Set_Etype (Act1, Orig_Type);
1801 if Is_Binary then
1802 Set_Etype (Act2, Orig_Type);
1803 end if;
1804 end if;
1805 end if;
1807 else
1808 Orig_Type := Empty;
1809 end if;
1811 Error := No (Orig_Type);
1812 end if;
1814 elsif Ekind (Opnd_Type) = E_Allocator_Type
1815 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1816 then
1817 Error := True;
1819 -- If the type is defined elsewhere, and the operator is not
1820 -- defined in the given scope (by a renaming declaration, e.g.)
1821 -- then this is an error as well. If an extension of System is
1822 -- present, and the type may be defined there, Pack must be
1823 -- System itself.
1825 elsif Scope (Opnd_Type) /= Pack
1826 and then Scope (Op_Id) /= Pack
1827 and then (No (System_Aux_Id)
1828 or else Scope (Opnd_Type) /= System_Aux_Id
1829 or else Pack /= Scope (System_Aux_Id))
1830 then
1831 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1832 Error := True;
1833 else
1834 Error := not Operand_Type_In_Scope (Pack);
1835 end if;
1837 elsif Pack = Standard_Standard
1838 and then not Operand_Type_In_Scope (Standard_Standard)
1839 then
1840 Error := True;
1841 end if;
1842 end if;
1844 if Error then
1845 Error_Msg_Node_2 := Pack;
1846 Error_Msg_NE
1847 ("& not declared in&", N, Selector_Name (Name (N)));
1848 Set_Etype (N, Any_Type);
1849 return;
1851 -- Detect a mismatch between the context type and the result type
1852 -- in the named package, which is otherwise not detected if the
1853 -- operands are universal. Check is only needed if source entity is
1854 -- an operator, not a function that renames an operator.
1856 elsif Nkind (Parent (N)) /= N_Type_Conversion
1857 and then Ekind (Entity (Name (N))) = E_Operator
1858 and then Is_Numeric_Type (Typ)
1859 and then not Is_Universal_Numeric_Type (Typ)
1860 and then Scope (Base_Type (Typ)) /= Pack
1861 and then not In_Instance
1862 then
1863 if Is_Fixed_Point_Type (Typ)
1864 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1865 then
1866 -- Already checked above
1868 null;
1870 -- Operator may be defined in an extension of System
1872 elsif Present (System_Aux_Id)
1873 and then Present (Opnd_Type)
1874 and then Scope (Opnd_Type) = System_Aux_Id
1875 then
1876 null;
1878 else
1879 -- Could we use Wrong_Type here??? (this would require setting
1880 -- Etype (N) to the actual type found where Typ was expected).
1882 Error_Msg_NE ("expect }", N, Typ);
1883 end if;
1884 end if;
1885 end if;
1887 Set_Chars (Op_Node, Op_Name);
1889 if not Is_Private_Type (Etype (N)) then
1890 Set_Etype (Op_Node, Base_Type (Etype (N)));
1891 else
1892 Set_Etype (Op_Node, Etype (N));
1893 end if;
1895 -- If this is a call to a function that renames a predefined equality,
1896 -- the renaming declaration provides a type that must be used to
1897 -- resolve the operands. This must be done now because resolution of
1898 -- the equality node will not resolve any remaining ambiguity, and it
1899 -- assumes that the first operand is not overloaded.
1901 if Op_Name in Name_Op_Eq | Name_Op_Ne
1902 and then Ekind (Func) = E_Function
1903 and then Is_Overloaded (Act1)
1904 then
1905 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1906 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1907 end if;
1909 Set_Entity (Op_Node, Op_Id);
1910 Generate_Reference (Op_Id, N, ' ');
1912 Rewrite (N, Op_Node);
1914 -- If this is an arithmetic operator and the result type is private,
1915 -- the operands and the result must be wrapped in conversion to
1916 -- expose the underlying numeric type and expand the proper checks,
1917 -- e.g. on division.
1919 if Is_Private_Type (Typ) then
1920 case Nkind (N) is
1921 when N_Op_Add
1922 | N_Op_Divide
1923 | N_Op_Expon
1924 | N_Op_Mod
1925 | N_Op_Multiply
1926 | N_Op_Rem
1927 | N_Op_Subtract
1929 Resolve_Intrinsic_Operator (N, Typ);
1931 when N_Op_Abs
1932 | N_Op_Minus
1933 | N_Op_Plus
1935 Resolve_Intrinsic_Unary_Operator (N, Typ);
1937 when others =>
1938 Resolve (N, Typ);
1939 end case;
1940 else
1941 Resolve (N, Typ);
1942 end if;
1943 end Make_Call_Into_Operator;
1945 -------------------
1946 -- Operator_Kind --
1947 -------------------
1949 function Operator_Kind
1950 (Op_Name : Name_Id;
1951 Is_Binary : Boolean) return Node_Kind
1953 Kind : Node_Kind;
1955 begin
1956 -- Use CASE statement or array???
1958 if Is_Binary then
1959 if Op_Name = Name_Op_And then
1960 Kind := N_Op_And;
1961 elsif Op_Name = Name_Op_Or then
1962 Kind := N_Op_Or;
1963 elsif Op_Name = Name_Op_Xor then
1964 Kind := N_Op_Xor;
1965 elsif Op_Name = Name_Op_Eq then
1966 Kind := N_Op_Eq;
1967 elsif Op_Name = Name_Op_Ne then
1968 Kind := N_Op_Ne;
1969 elsif Op_Name = Name_Op_Lt then
1970 Kind := N_Op_Lt;
1971 elsif Op_Name = Name_Op_Le then
1972 Kind := N_Op_Le;
1973 elsif Op_Name = Name_Op_Gt then
1974 Kind := N_Op_Gt;
1975 elsif Op_Name = Name_Op_Ge then
1976 Kind := N_Op_Ge;
1977 elsif Op_Name = Name_Op_Add then
1978 Kind := N_Op_Add;
1979 elsif Op_Name = Name_Op_Subtract then
1980 Kind := N_Op_Subtract;
1981 elsif Op_Name = Name_Op_Concat then
1982 Kind := N_Op_Concat;
1983 elsif Op_Name = Name_Op_Multiply then
1984 Kind := N_Op_Multiply;
1985 elsif Op_Name = Name_Op_Divide then
1986 Kind := N_Op_Divide;
1987 elsif Op_Name = Name_Op_Mod then
1988 Kind := N_Op_Mod;
1989 elsif Op_Name = Name_Op_Rem then
1990 Kind := N_Op_Rem;
1991 elsif Op_Name = Name_Op_Expon then
1992 Kind := N_Op_Expon;
1993 else
1994 raise Program_Error;
1995 end if;
1997 -- Unary operators
1999 else
2000 if Op_Name = Name_Op_Add then
2001 Kind := N_Op_Plus;
2002 elsif Op_Name = Name_Op_Subtract then
2003 Kind := N_Op_Minus;
2004 elsif Op_Name = Name_Op_Abs then
2005 Kind := N_Op_Abs;
2006 elsif Op_Name = Name_Op_Not then
2007 Kind := N_Op_Not;
2008 else
2009 raise Program_Error;
2010 end if;
2011 end if;
2013 return Kind;
2014 end Operator_Kind;
2016 ----------------------------
2017 -- Preanalyze_And_Resolve --
2018 ----------------------------
2020 procedure Preanalyze_And_Resolve
2021 (N : Node_Id;
2022 T : Entity_Id;
2023 With_Freezing : Boolean)
2025 Save_Full_Analysis : constant Boolean := Full_Analysis;
2026 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
2027 Save_Preanalysis_Count : constant Nat :=
2028 Inside_Preanalysis_Without_Freezing;
2029 begin
2030 pragma Assert (Nkind (N) in N_Subexpr);
2032 if not With_Freezing then
2033 Set_Must_Not_Freeze (N);
2034 Inside_Preanalysis_Without_Freezing :=
2035 Inside_Preanalysis_Without_Freezing + 1;
2036 end if;
2038 Full_Analysis := False;
2039 Expander_Mode_Save_And_Set (False);
2041 -- See also Preanalyze_And_Resolve in sem.adb for similar handling
2043 -- Normally, we suppress all checks for this preanalysis. There is no
2044 -- point in processing them now, since they will be applied properly
2045 -- and in the proper location when the default expressions reanalyzed
2046 -- and reexpanded later on. We will also have more information at that
2047 -- point for possible suppression of individual checks.
2049 -- However, in GNATprove mode, most expansion is suppressed, and this
2050 -- later reanalysis and reexpansion may not occur. GNATprove mode does
2051 -- require the setting of checking flags for proof purposes, so we
2052 -- do the GNATprove preanalysis without suppressing checks.
2054 -- This special handling for SPARK mode is required for example in the
2055 -- case of Ada 2012 constructs such as quantified expressions, which are
2056 -- expanded in two separate steps.
2058 -- We also do not want to suppress checks if we are not dealing
2059 -- with a default expression. One such case that is known to reach
2060 -- this point is the expression of an expression function.
2062 if GNATprove_Mode or Nkind (Parent (N)) = N_Simple_Return_Statement then
2063 Analyze_And_Resolve (N, T);
2064 else
2065 Analyze_And_Resolve (N, T, Suppress => All_Checks);
2066 end if;
2068 Expander_Mode_Restore;
2069 Full_Analysis := Save_Full_Analysis;
2071 if not With_Freezing then
2072 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
2073 Inside_Preanalysis_Without_Freezing :=
2074 Inside_Preanalysis_Without_Freezing - 1;
2075 end if;
2077 pragma Assert
2078 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
2079 end Preanalyze_And_Resolve;
2081 ----------------------------
2082 -- Preanalyze_And_Resolve --
2083 ----------------------------
2085 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
2086 begin
2087 Preanalyze_And_Resolve (N, T, With_Freezing => False);
2088 end Preanalyze_And_Resolve;
2090 -- Version without context type
2092 procedure Preanalyze_And_Resolve (N : Node_Id) is
2093 Save_Full_Analysis : constant Boolean := Full_Analysis;
2095 begin
2096 Full_Analysis := False;
2097 Expander_Mode_Save_And_Set (False);
2099 Analyze (N);
2100 Resolve (N, Etype (N), Suppress => All_Checks);
2102 Expander_Mode_Restore;
2103 Full_Analysis := Save_Full_Analysis;
2104 end Preanalyze_And_Resolve;
2106 ------------------------------------------
2107 -- Preanalyze_With_Freezing_And_Resolve --
2108 ------------------------------------------
2110 procedure Preanalyze_With_Freezing_And_Resolve
2111 (N : Node_Id;
2112 T : Entity_Id)
2114 begin
2115 Preanalyze_And_Resolve (N, T, With_Freezing => True);
2116 end Preanalyze_With_Freezing_And_Resolve;
2118 ----------------------------------
2119 -- Replace_Actual_Discriminants --
2120 ----------------------------------
2122 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2123 Loc : constant Source_Ptr := Sloc (N);
2124 Tsk : Node_Id := Empty;
2126 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2127 -- Comment needed???
2129 -------------------
2130 -- Process_Discr --
2131 -------------------
2133 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2134 Ent : Entity_Id;
2136 begin
2137 if Nkind (Nod) = N_Identifier then
2138 Ent := Entity (Nod);
2140 if Present (Ent)
2141 and then Ekind (Ent) = E_Discriminant
2142 then
2143 Rewrite (Nod,
2144 Make_Selected_Component (Loc,
2145 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2146 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2148 Set_Etype (Nod, Etype (Ent));
2149 end if;
2151 end if;
2153 return OK;
2154 end Process_Discr;
2156 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2158 -- Start of processing for Replace_Actual_Discriminants
2160 begin
2161 if Expander_Active then
2162 null;
2164 -- Allow the replacement of concurrent discriminants in GNATprove even
2165 -- though this is a light expansion activity. Note that generic units
2166 -- are not modified.
2168 elsif GNATprove_Mode and not Inside_A_Generic then
2169 null;
2171 else
2172 return;
2173 end if;
2175 if Nkind (Name (N)) = N_Selected_Component then
2176 Tsk := Prefix (Name (N));
2178 elsif Nkind (Name (N)) = N_Indexed_Component then
2179 Tsk := Prefix (Prefix (Name (N)));
2180 end if;
2182 if Present (Tsk) then
2183 Replace_Discrs (Default);
2184 end if;
2185 end Replace_Actual_Discriminants;
2187 -------------
2188 -- Resolve --
2189 -------------
2191 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2192 Ambiguous : Boolean := False;
2193 Ctx_Type : Entity_Id := Typ;
2194 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2195 Err_Type : Entity_Id := Empty;
2196 Found : Boolean := False;
2197 From_Lib : Boolean;
2198 I : Interp_Index;
2199 I1 : Interp_Index := 0; -- prevent junk warning
2200 It : Interp;
2201 It1 : Interp;
2202 Seen : Entity_Id := Empty; -- prevent junk warning
2204 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2205 -- Determine whether a node comes from a predefined library unit or
2206 -- Standard.
2208 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2209 -- Try and fix up a literal so that it matches its expected type. New
2210 -- literals are manufactured if necessary to avoid cascaded errors.
2212 procedure Report_Ambiguous_Argument;
2213 -- Additional diagnostics when an ambiguous call has an ambiguous
2214 -- argument (typically a controlling actual).
2216 procedure Resolution_Failed;
2217 -- Called when attempt at resolving current expression fails
2219 ------------------------------------
2220 -- Comes_From_Predefined_Lib_Unit --
2221 -------------------------------------
2223 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2224 begin
2225 return
2226 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2227 end Comes_From_Predefined_Lib_Unit;
2229 --------------------
2230 -- Patch_Up_Value --
2231 --------------------
2233 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2234 begin
2235 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2236 Rewrite (N,
2237 Make_Real_Literal (Sloc (N),
2238 Realval => UR_From_Uint (Intval (N))));
2239 Set_Etype (N, Universal_Real);
2240 Set_Is_Static_Expression (N);
2242 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2243 Rewrite (N,
2244 Make_Integer_Literal (Sloc (N),
2245 Intval => UR_To_Uint (Realval (N))));
2246 Set_Etype (N, Universal_Integer);
2247 Set_Is_Static_Expression (N);
2249 elsif Nkind (N) = N_String_Literal
2250 and then Is_Character_Type (Typ)
2251 then
2252 Set_Character_Literal_Name (Get_Char_Code ('A'));
2253 Rewrite (N,
2254 Make_Character_Literal (Sloc (N),
2255 Chars => Name_Find,
2256 Char_Literal_Value =>
2257 UI_From_CC (Get_Char_Code ('A'))));
2258 Set_Etype (N, Any_Character);
2259 Set_Is_Static_Expression (N);
2261 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2262 Rewrite (N,
2263 Make_String_Literal (Sloc (N),
2264 Strval => End_String));
2266 elsif Nkind (N) = N_Range then
2267 Patch_Up_Value (Low_Bound (N), Typ);
2268 Patch_Up_Value (High_Bound (N), Typ);
2269 end if;
2270 end Patch_Up_Value;
2272 -------------------------------
2273 -- Report_Ambiguous_Argument --
2274 -------------------------------
2276 procedure Report_Ambiguous_Argument is
2277 Arg : constant Node_Id := First (Parameter_Associations (N));
2278 I : Interp_Index;
2279 It : Interp;
2281 begin
2282 if Nkind (Arg) = N_Function_Call
2283 and then Is_Entity_Name (Name (Arg))
2284 and then Is_Overloaded (Name (Arg))
2285 then
2286 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2288 -- Examine possible interpretations, and adapt the message
2289 -- for inherited subprograms declared by a type derivation.
2291 Get_First_Interp (Name (Arg), I, It);
2292 while Present (It.Nam) loop
2293 Error_Msg_Sloc := Sloc (It.Nam);
2295 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2296 Error_Msg_N ("interpretation (inherited) #!", Arg);
2297 else
2298 Error_Msg_N ("interpretation #!", Arg);
2299 end if;
2301 Get_Next_Interp (I, It);
2302 end loop;
2303 end if;
2305 -- Additional message and hint if the ambiguity involves an Ada 2022
2306 -- container aggregate.
2308 Check_Ambiguous_Aggregate (N);
2309 end Report_Ambiguous_Argument;
2311 -----------------------
2312 -- Resolution_Failed --
2313 -----------------------
2315 procedure Resolution_Failed is
2316 begin
2317 Patch_Up_Value (N, Typ);
2319 -- Set the type to the desired one to minimize cascaded errors. Note
2320 -- that this is an approximation and does not work in all cases.
2322 Set_Etype (N, Typ);
2324 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2325 Set_Is_Overloaded (N, False);
2327 -- The caller will return without calling the expander, so we need
2328 -- to set the analyzed flag. Note that it is fine to set Analyzed
2329 -- to True even if we are in the middle of a shallow analysis,
2330 -- (see the spec of sem for more details) since this is an error
2331 -- situation anyway, and there is no point in repeating the
2332 -- analysis later (indeed it won't work to repeat it later, since
2333 -- we haven't got a clear resolution of which entity is being
2334 -- referenced.)
2336 Set_Analyzed (N, True);
2337 return;
2338 end Resolution_Failed;
2340 -- Start of processing for Resolve
2342 begin
2343 if N = Error then
2344 return;
2345 end if;
2347 -- Access attribute on remote subprogram cannot be used for a non-remote
2348 -- access-to-subprogram type.
2350 if Nkind (N) = N_Attribute_Reference
2351 and then Attribute_Name (N) in Name_Access
2352 | Name_Unrestricted_Access
2353 | Name_Unchecked_Access
2354 and then Comes_From_Source (N)
2355 and then Is_Entity_Name (Prefix (N))
2356 and then Is_Subprogram (Entity (Prefix (N)))
2357 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2358 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2359 then
2360 Error_Msg_N
2361 ("prefix must statically denote a non-remote subprogram", N);
2362 end if;
2364 -- If the context is a Remote_Access_To_Subprogram, access attributes
2365 -- must be resolved with the corresponding fat pointer. There is no need
2366 -- to check for the attribute name since the return type of an
2367 -- attribute is never a remote type.
2369 if Nkind (N) = N_Attribute_Reference
2370 and then Comes_From_Source (N)
2371 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2372 then
2373 declare
2374 Attr : constant Attribute_Id :=
2375 Get_Attribute_Id (Attribute_Name (N));
2376 Pref : constant Node_Id := Prefix (N);
2377 Decl : Node_Id;
2378 Spec : Node_Id;
2379 Is_Remote : Boolean := True;
2381 begin
2382 -- Check that Typ is a remote access-to-subprogram type
2384 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2386 -- Prefix (N) must statically denote a remote subprogram
2387 -- declared in a package specification.
2389 if Attr = Attribute_Access or else
2390 Attr = Attribute_Unchecked_Access or else
2391 Attr = Attribute_Unrestricted_Access
2392 then
2393 Decl := Unit_Declaration_Node (Entity (Pref));
2395 if Nkind (Decl) = N_Subprogram_Body then
2396 Spec := Corresponding_Spec (Decl);
2398 if Present (Spec) then
2399 Decl := Unit_Declaration_Node (Spec);
2400 end if;
2401 end if;
2403 Spec := Parent (Decl);
2405 if not Is_Entity_Name (Prefix (N))
2406 or else Nkind (Spec) /= N_Package_Specification
2407 or else
2408 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2409 then
2410 Is_Remote := False;
2411 Error_Msg_N
2412 ("prefix must statically denote a remote subprogram",
2414 end if;
2416 -- If we are generating code in distributed mode, perform
2417 -- semantic checks against corresponding remote entities.
2419 if Expander_Active
2420 and then Get_PCS_Name /= Name_No_DSA
2421 then
2422 Check_Subtype_Conformant
2423 (New_Id => Entity (Prefix (N)),
2424 Old_Id => Designated_Type
2425 (Corresponding_Remote_Type (Typ)),
2426 Err_Loc => N);
2428 if Is_Remote then
2429 Process_Remote_AST_Attribute (N, Typ);
2430 end if;
2431 end if;
2432 end if;
2433 end if;
2434 end;
2435 end if;
2437 Debug_A_Entry ("resolving ", N);
2439 if Debug_Flag_V then
2440 Write_Overloads (N);
2441 end if;
2443 if Comes_From_Source (N) then
2444 if Is_Fixed_Point_Type (Typ) then
2445 Check_Restriction (No_Fixed_Point, N);
2447 elsif Is_Floating_Point_Type (Typ)
2448 and then Typ /= Universal_Real
2449 and then Typ /= Any_Real
2450 then
2451 Check_Restriction (No_Floating_Point, N);
2452 end if;
2453 end if;
2455 -- Return if already analyzed
2457 if Analyzed (N) then
2458 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2459 Analyze_Dimension (N);
2460 return;
2462 -- Any case of Any_Type as the Etype value means that we had a
2463 -- previous error.
2465 elsif Etype (N) = Any_Type then
2466 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2467 return;
2468 end if;
2470 Check_Parameterless_Call (N);
2472 -- The resolution of an Expression_With_Actions is determined by
2473 -- its Expression, but if the node comes from source it is a
2474 -- Declare_Expression and requires scope management.
2476 if Nkind (N) = N_Expression_With_Actions then
2477 if Comes_From_Source (N) and then not Is_Rewrite_Substitution (N) then
2478 Resolve_Declare_Expression (N, Typ);
2479 else
2480 Resolve (Expression (N), Typ);
2481 end if;
2483 Found := True;
2484 Expr_Type := Etype (Expression (N));
2486 -- The resolution of a conditional expression that is the operand of a
2487 -- type conversion is determined by the conversion (RM 4.5.7(10/3)).
2489 elsif Nkind (N) in N_Case_Expression | N_If_Expression
2490 and then Nkind (Parent (N)) = N_Type_Conversion
2491 then
2492 Found := True;
2493 Expr_Type := Etype (Parent (N));
2495 -- If not overloaded, then we know the type, and all that needs doing
2496 -- is to check that this type is compatible with the context. But note
2497 -- that we may have an operator with no interpretation in Ada 2022 for
2498 -- the case of possible user-defined literals as operands.
2500 elsif not Is_Overloaded (N) then
2501 if Nkind (N) in N_Op and then No (Entity (N)) then
2502 pragma Assert (Ada_Version >= Ada_2022);
2503 Found := False;
2504 else
2505 Found := Covers (Typ, Etype (N));
2506 end if;
2507 Expr_Type := Etype (N);
2509 -- In the overloaded case, we must select the interpretation that
2510 -- is compatible with the context (i.e. the type passed to Resolve)
2512 else
2513 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2515 -- Loop through possible interpretations
2517 Get_First_Interp (N, I, It);
2518 Interp_Loop : while Present (It.Typ) loop
2519 if Debug_Flag_V then
2520 Write_Str ("Interp: ");
2521 Write_Interp (It);
2522 end if;
2524 -- We are only interested in interpretations that are compatible
2525 -- with the expected type, any other interpretations are ignored.
2527 if not Covers (Typ, It.Typ) then
2528 if Debug_Flag_V then
2529 Write_Str (" interpretation incompatible with context");
2530 Write_Eol;
2531 end if;
2533 else
2534 -- Skip the current interpretation if it is disabled by an
2535 -- abstract operator. This action is performed only when the
2536 -- type against which we are resolving is the same as the
2537 -- type of the interpretation.
2539 if Ada_Version >= Ada_2005
2540 and then It.Typ = Typ
2541 and then not Is_Universal_Numeric_Type (Typ)
2542 and then Present (It.Abstract_Op)
2543 then
2544 if Debug_Flag_V then
2545 Write_Line ("Skip.");
2546 end if;
2548 goto Continue;
2549 end if;
2551 -- First matching interpretation
2553 if not Found then
2554 Found := True;
2555 I1 := I;
2556 Seen := It.Nam;
2557 Expr_Type := It.Typ;
2559 -- Matching interpretation that is not the first, maybe an
2560 -- error, but there are some cases where preference rules are
2561 -- used to choose between the two possibilities. These and
2562 -- some more obscure cases are handled in Disambiguate.
2564 else
2565 -- If the current statement is part of a predefined library
2566 -- unit, then all interpretations which come from user level
2567 -- packages should not be considered. Check previous and
2568 -- current one.
2570 if From_Lib then
2571 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2572 goto Continue;
2574 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2576 -- Previous interpretation must be discarded
2578 I1 := I;
2579 Seen := It.Nam;
2580 Expr_Type := It.Typ;
2581 Set_Entity (N, Seen);
2582 goto Continue;
2583 end if;
2584 end if;
2586 -- Otherwise apply further disambiguation steps
2588 Error_Msg_Sloc := Sloc (Seen);
2589 It1 := Disambiguate (N, I1, I, Typ);
2591 -- Disambiguation has succeeded. Skip the remaining
2592 -- interpretations.
2594 if It1 /= No_Interp then
2595 Seen := It1.Nam;
2596 Expr_Type := It1.Typ;
2598 while Present (It.Typ) loop
2599 Get_Next_Interp (I, It);
2600 end loop;
2602 else
2603 -- Before we issue an ambiguity complaint, check for the
2604 -- case of a subprogram call where at least one of the
2605 -- arguments is Any_Type, and if so suppress the message,
2606 -- since it is a cascaded error. This can also happen for
2607 -- a generalized indexing operation.
2609 if Nkind (N) in N_Subprogram_Call
2610 or else (Nkind (N) = N_Indexed_Component
2611 and then Present (Generalized_Indexing (N)))
2612 then
2613 declare
2614 A : Node_Id;
2615 E : Node_Id;
2617 begin
2618 if Nkind (N) = N_Indexed_Component then
2619 Rewrite (N, Generalized_Indexing (N));
2620 end if;
2622 A := First_Actual (N);
2623 while Present (A) loop
2624 E := A;
2626 if Nkind (E) = N_Parameter_Association then
2627 E := Explicit_Actual_Parameter (E);
2628 end if;
2630 if Etype (E) = Any_Type then
2631 if Debug_Flag_V then
2632 Write_Str ("Any_Type in call");
2633 Write_Eol;
2634 end if;
2636 exit Interp_Loop;
2637 end if;
2639 Next_Actual (A);
2640 end loop;
2641 end;
2643 elsif Nkind (N) in N_Binary_Op
2644 and then (Etype (Left_Opnd (N)) = Any_Type
2645 or else Etype (Right_Opnd (N)) = Any_Type)
2646 then
2647 exit Interp_Loop;
2649 elsif Nkind (N) in N_Unary_Op
2650 and then Etype (Right_Opnd (N)) = Any_Type
2651 then
2652 exit Interp_Loop;
2653 end if;
2655 -- Not that special case, so issue message using the flag
2656 -- Ambiguous to control printing of the header message
2657 -- only at the start of an ambiguous set.
2659 if not Ambiguous then
2660 if Nkind (N) = N_Function_Call
2661 and then Nkind (Name (N)) = N_Explicit_Dereference
2662 then
2663 Error_Msg_N
2664 ("ambiguous expression (cannot resolve indirect "
2665 & "call)!", N);
2666 else
2667 Error_Msg_NE -- CODEFIX
2668 ("ambiguous expression (cannot resolve&)!",
2669 N, It.Nam);
2670 end if;
2672 Ambiguous := True;
2674 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2675 Error_Msg_N
2676 ("\\possible interpretation (inherited)#!", N);
2677 else
2678 Error_Msg_N -- CODEFIX
2679 ("\\possible interpretation#!", N);
2680 end if;
2682 if Nkind (N) in N_Subprogram_Call
2683 and then Present (Parameter_Associations (N))
2684 then
2685 Report_Ambiguous_Argument;
2686 end if;
2687 end if;
2689 Error_Msg_Sloc := Sloc (It.Nam);
2691 -- By default, the error message refers to the candidate
2692 -- interpretation. But if it is a predefined operator, it
2693 -- is implicitly declared at the declaration of the type
2694 -- of the operand. Recover the sloc of that declaration
2695 -- for the error message.
2697 if Nkind (N) in N_Op
2698 and then Scope (It.Nam) = Standard_Standard
2699 and then not Is_Overloaded (Right_Opnd (N))
2700 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2701 Standard_Standard
2702 then
2703 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2705 if Comes_From_Source (Err_Type)
2706 and then Present (Parent (Err_Type))
2707 then
2708 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2709 end if;
2711 elsif Nkind (N) in N_Binary_Op
2712 and then Scope (It.Nam) = Standard_Standard
2713 and then not Is_Overloaded (Left_Opnd (N))
2714 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2715 Standard_Standard
2716 then
2717 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2719 if Comes_From_Source (Err_Type)
2720 and then Present (Parent (Err_Type))
2721 then
2722 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2723 end if;
2725 -- If this is an indirect call, use the subprogram_type
2726 -- in the message, to have a meaningful location. Also
2727 -- indicate if this is an inherited operation, created
2728 -- by a type declaration.
2730 elsif Nkind (N) = N_Function_Call
2731 and then Nkind (Name (N)) = N_Explicit_Dereference
2732 and then Is_Type (It.Nam)
2733 then
2734 Err_Type := It.Nam;
2735 Error_Msg_Sloc :=
2736 Sloc (Associated_Node_For_Itype (Err_Type));
2737 else
2738 Err_Type := Empty;
2739 end if;
2741 if Nkind (N) in N_Op
2742 and then Scope (It.Nam) = Standard_Standard
2743 and then Present (Err_Type)
2744 then
2745 -- Special-case the message for universal_fixed
2746 -- operators, which are not declared with the type
2747 -- of the operand, but appear forever in Standard.
2749 if It.Typ = Universal_Fixed
2750 and then Scope (It.Nam) = Standard_Standard
2751 then
2752 Error_Msg_N
2753 ("\\possible interpretation as universal_fixed "
2754 & "operation (RM 4.5.5 (19))", N);
2755 else
2756 Error_Msg_N
2757 ("\\possible interpretation (predefined)#!", N);
2758 end if;
2760 elsif
2761 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2762 then
2763 Error_Msg_N
2764 ("\\possible interpretation (inherited)#!", N);
2765 else
2766 Error_Msg_N -- CODEFIX
2767 ("\\possible interpretation#!", N);
2768 end if;
2770 end if;
2771 end if;
2773 -- We have a matching interpretation, Expr_Type is the type
2774 -- from this interpretation, and Seen is the entity.
2776 -- For an operator, just set the entity name. The type will be
2777 -- set by the specific operator resolution routine.
2779 if Nkind (N) in N_Op then
2780 Set_Entity (N, Seen);
2781 Generate_Reference (Seen, N);
2783 elsif Nkind (N) in N_Case_Expression
2784 | N_Character_Literal
2785 | N_Delta_Aggregate
2786 | N_If_Expression
2787 then
2788 Set_Etype (N, Expr_Type);
2790 -- AI05-0139-2: Expression is overloaded because type has
2791 -- implicit dereference. The context may be the one that
2792 -- requires implicit dereferemce.
2794 elsif Has_Implicit_Dereference (Expr_Type) then
2795 Set_Etype (N, Expr_Type);
2796 Set_Is_Overloaded (N, False);
2798 -- If the expression is an entity, generate a reference
2799 -- to it, as this is not done for an overloaded construct
2800 -- during analysis.
2802 if Is_Entity_Name (N)
2803 and then Comes_From_Source (N)
2804 then
2805 Generate_Reference (Entity (N), N);
2807 -- Examine access discriminants of entity type,
2808 -- to check whether one of them yields the
2809 -- expected type.
2811 declare
2812 Disc : Entity_Id :=
2813 First_Discriminant (Etype (Entity (N)));
2815 begin
2816 while Present (Disc) loop
2817 exit when Is_Access_Type (Etype (Disc))
2818 and then Has_Implicit_Dereference (Disc)
2819 and then Designated_Type (Etype (Disc)) = Typ;
2821 Next_Discriminant (Disc);
2822 end loop;
2824 if Present (Disc) then
2825 Build_Explicit_Dereference (N, Disc);
2826 end if;
2827 end;
2828 end if;
2830 exit Interp_Loop;
2832 elsif Is_Overloaded (N)
2833 and then Present (It.Nam)
2834 and then Ekind (It.Nam) = E_Discriminant
2835 and then Has_Implicit_Dereference (It.Nam)
2836 then
2837 -- If the node is a general indexing, the dereference is
2838 -- is inserted when resolving the rewritten form, else
2839 -- insert it now.
2841 if Nkind (N) /= N_Indexed_Component
2842 or else No (Generalized_Indexing (N))
2843 then
2844 Build_Explicit_Dereference (N, It.Nam);
2845 end if;
2847 -- For an explicit dereference, attribute reference, range,
2848 -- short-circuit form (which is not an operator node), or call
2849 -- with a name that is an explicit dereference, there is
2850 -- nothing to be done at this point.
2852 elsif Nkind (N) in N_Attribute_Reference
2853 | N_And_Then
2854 | N_Explicit_Dereference
2855 | N_Identifier
2856 | N_Indexed_Component
2857 | N_Or_Else
2858 | N_Range
2859 | N_Selected_Component
2860 | N_Slice
2861 or else Nkind (Name (N)) = N_Explicit_Dereference
2862 then
2863 null;
2865 -- For procedure or function calls, set the type of the name,
2866 -- and also the entity pointer for the prefix.
2868 elsif Nkind (N) in N_Subprogram_Call
2869 and then Is_Entity_Name (Name (N))
2870 then
2871 Set_Etype (Name (N), Expr_Type);
2872 Set_Entity (Name (N), Seen);
2873 Generate_Reference (Seen, Name (N));
2875 elsif Nkind (N) = N_Function_Call
2876 and then Nkind (Name (N)) = N_Selected_Component
2877 then
2878 Set_Etype (Name (N), Expr_Type);
2879 Set_Entity (Selector_Name (Name (N)), Seen);
2880 Generate_Reference (Seen, Selector_Name (Name (N)));
2882 -- For all other cases, just set the type of the Name
2884 else
2885 Set_Etype (Name (N), Expr_Type);
2886 end if;
2888 end if;
2890 <<Continue>>
2892 -- Move to next interpretation
2894 exit Interp_Loop when No (It.Typ);
2896 Get_Next_Interp (I, It);
2897 end loop Interp_Loop;
2898 end if;
2900 -- At this stage Found indicates whether or not an acceptable
2901 -- interpretation exists. If not, then we have an error, except that if
2902 -- the context is Any_Type as a result of some other error, then we
2903 -- suppress the error report.
2905 if not Found then
2906 if Typ /= Any_Type then
2908 -- If type we are looking for is Void, then this is the procedure
2909 -- call case, and the error is simply that what we gave is not a
2910 -- procedure name (we think of procedure calls as expressions with
2911 -- types internally, but the user doesn't think of them this way).
2913 if Typ = Standard_Void_Type then
2915 -- Special case message if function used as a procedure
2917 if Nkind (N) = N_Procedure_Call_Statement
2918 and then Is_Entity_Name (Name (N))
2919 and then Ekind (Entity (Name (N))) = E_Function
2920 then
2921 Error_Msg_NE
2922 ("cannot use call to function & as a statement",
2923 Name (N), Entity (Name (N)));
2924 Error_Msg_N
2925 ("\return value of a function call cannot be ignored",
2926 Name (N));
2928 -- Otherwise give general message (not clear what cases this
2929 -- covers, but no harm in providing for them).
2931 else
2932 Error_Msg_N ("expect procedure name in procedure call", N);
2933 end if;
2935 Found := True;
2937 -- Otherwise we do have a subexpression with the wrong type
2939 -- Check for the case of an allocator which uses an access type
2940 -- instead of the designated type. This is a common error and we
2941 -- specialize the message, posting an error on the operand of the
2942 -- allocator, complaining that we expected the designated type of
2943 -- the allocator.
2945 elsif Nkind (N) = N_Allocator
2946 and then Is_Access_Type (Typ)
2947 and then Is_Access_Type (Etype (N))
2948 and then Designated_Type (Etype (N)) = Typ
2949 then
2950 Wrong_Type (Expression (N), Designated_Type (Typ));
2951 Found := True;
2953 -- Check for view mismatch on Null in instances, for which the
2954 -- view-swapping mechanism has no identifier.
2956 elsif (In_Instance or else In_Inlined_Body)
2957 and then Nkind (N) = N_Null
2958 and then Is_Private_Type (Typ)
2959 and then Is_Access_Type (Full_View (Typ))
2960 then
2961 Resolve (N, Full_View (Typ));
2962 Set_Etype (N, Typ);
2963 return;
2965 -- Check for an aggregate. Sometimes we can get bogus aggregates
2966 -- from misuse of parentheses, and we are about to complain about
2967 -- the aggregate without even looking inside it.
2969 -- Instead, if we have an aggregate of type Any_Composite, then
2970 -- analyze and resolve the component fields, and then only issue
2971 -- another message if we get no errors doing this (otherwise
2972 -- assume that the errors in the aggregate caused the problem).
2974 elsif Nkind (N) = N_Aggregate
2975 and then Etype (N) = Any_Composite
2976 then
2977 if Ada_Version >= Ada_2022
2978 and then Has_Aspect (Typ, Aspect_Aggregate)
2979 then
2980 Resolve_Container_Aggregate (N, Typ);
2982 if Expander_Active then
2983 Expand (N);
2984 end if;
2985 return;
2986 end if;
2988 -- Disable expansion in any case. If there is a type mismatch
2989 -- it may be fatal to try to expand the aggregate. The flag
2990 -- would otherwise be set to false when the error is posted.
2992 Expander_Active := False;
2994 declare
2995 procedure Check_Aggr (Aggr : Node_Id);
2996 -- Check one aggregate, and set Found to True if we have a
2997 -- definite error in any of its elements
2999 procedure Check_Elmt (Aelmt : Node_Id);
3000 -- Check one element of aggregate and set Found to True if
3001 -- we definitely have an error in the element.
3003 ----------------
3004 -- Check_Aggr --
3005 ----------------
3007 procedure Check_Aggr (Aggr : Node_Id) is
3008 Elmt : Node_Id;
3010 begin
3011 if Present (Expressions (Aggr)) then
3012 Elmt := First (Expressions (Aggr));
3013 while Present (Elmt) loop
3014 Check_Elmt (Elmt);
3015 Next (Elmt);
3016 end loop;
3017 end if;
3019 if Present (Component_Associations (Aggr)) then
3020 Elmt := First (Component_Associations (Aggr));
3021 while Present (Elmt) loop
3023 -- If this is a default-initialized component, then
3024 -- there is nothing to check. The box will be
3025 -- replaced by the appropriate call during late
3026 -- expansion.
3028 if Nkind (Elmt) /= N_Iterated_Component_Association
3029 and then not Box_Present (Elmt)
3030 then
3031 Check_Elmt (Expression (Elmt));
3032 end if;
3034 Next (Elmt);
3035 end loop;
3036 end if;
3037 end Check_Aggr;
3039 ----------------
3040 -- Check_Elmt --
3041 ----------------
3043 procedure Check_Elmt (Aelmt : Node_Id) is
3044 begin
3045 -- If we have a nested aggregate, go inside it (to
3046 -- attempt a naked analyze-resolve of the aggregate can
3047 -- cause undesirable cascaded errors). Do not resolve
3048 -- expression if it needs a type from context, as for
3049 -- integer * fixed expression.
3051 if Nkind (Aelmt) = N_Aggregate then
3052 Check_Aggr (Aelmt);
3054 else
3055 Analyze (Aelmt);
3057 if not Is_Overloaded (Aelmt)
3058 and then Etype (Aelmt) /= Any_Fixed
3059 then
3060 Resolve (Aelmt);
3061 end if;
3063 if Etype (Aelmt) = Any_Type then
3064 Found := True;
3065 end if;
3066 end if;
3067 end Check_Elmt;
3069 begin
3070 Check_Aggr (N);
3071 end;
3072 end if;
3074 -- Check whether the node is a literal or a named number or a
3075 -- conditional expression whose dependent expressions are all
3076 -- literals or named numbers.
3078 if Try_User_Defined_Literal (N, Typ) then
3079 return;
3080 end if;
3082 -- Looks like we have a type error, but check for special case
3083 -- of Address wanted, integer found, with the configuration pragma
3084 -- Allow_Integer_Address active. If we have this case, introduce
3085 -- an unchecked conversion to allow the integer expression to be
3086 -- treated as an Address. The reverse case of integer wanted,
3087 -- Address found, is treated in an analogous manner.
3089 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3090 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3091 Analyze_And_Resolve (N, Typ);
3092 return;
3094 -- Under relaxed RM semantics silently replace occurrences of null
3095 -- by System.Null_Address.
3097 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3098 Replace_Null_By_Null_Address (N);
3099 Analyze_And_Resolve (N, Typ);
3100 return;
3101 end if;
3103 -- That special Allow_Integer_Address check did not apply, so we
3104 -- have a real type error. If an error message was issued already,
3105 -- Found got reset to True, so if it's still False, issue standard
3106 -- Wrong_Type message.
3108 if not Found then
3109 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3110 declare
3111 Subp_Name : Node_Id;
3113 begin
3114 if Is_Entity_Name (Name (N)) then
3115 Subp_Name := Name (N);
3117 elsif Nkind (Name (N)) = N_Selected_Component then
3119 -- Protected operation: retrieve operation name
3121 Subp_Name := Selector_Name (Name (N));
3123 else
3124 raise Program_Error;
3125 end if;
3127 Error_Msg_Node_2 := Typ;
3128 Error_Msg_NE
3129 ("no visible interpretation of& matches expected type&",
3130 N, Subp_Name);
3131 end;
3133 if All_Errors_Mode then
3134 declare
3135 Index : Interp_Index;
3136 It : Interp;
3138 begin
3139 Error_Msg_N ("\\possible interpretations:", N);
3141 Get_First_Interp (Name (N), Index, It);
3142 while Present (It.Nam) loop
3143 Error_Msg_Sloc := Sloc (It.Nam);
3144 Error_Msg_Node_2 := It.Nam;
3145 Error_Msg_NE
3146 ("\\ type& for & declared#", N, It.Typ);
3147 Get_Next_Interp (Index, It);
3148 end loop;
3149 end;
3151 else
3152 Error_Msg_N ("\use -gnatf for details", N);
3153 end if;
3155 -- Recognize the case of a quantified expression being mistaken
3156 -- for an iterated component association because the user
3157 -- forgot the "all" or "some" keyword after "for". Because the
3158 -- error message starts with "missing ALL", we automatically
3159 -- benefit from the associated CODEFIX, which requires that
3160 -- the message is located on the identifier following "for"
3161 -- in order for the CODEFIX to insert "all" in the right place.
3163 elsif Nkind (N) = N_Aggregate
3164 and then List_Length (Component_Associations (N)) = 1
3165 and then Nkind (First (Component_Associations (N)))
3166 = N_Iterated_Component_Association
3167 and then Is_Boolean_Type (Typ)
3168 then
3169 if Present
3170 (Iterator_Specification
3171 (First (Component_Associations (N))))
3172 then
3173 Error_Msg_N -- CODEFIX
3174 ("missing ALL or SOME in quantified expression",
3175 Defining_Identifier
3176 (Iterator_Specification
3177 (First (Component_Associations (N)))));
3178 else
3179 Error_Msg_N -- CODEFIX
3180 ("missing ALL or SOME in quantified expression",
3181 Defining_Identifier
3182 (First (Component_Associations (N))));
3183 end if;
3185 -- For an operator with no interpretation, check whether one of
3186 -- its operands may be a user-defined literal.
3188 elsif Nkind (N) in N_Op and then No (Entity (N)) then
3189 if Try_User_Defined_Literal_For_Operator (N, Typ) then
3190 return;
3191 else
3192 Unresolved_Operator (N);
3193 end if;
3195 else
3196 Wrong_Type (N, Typ);
3197 end if;
3198 end if;
3199 end if;
3201 Resolution_Failed;
3202 return;
3204 -- Test if we have more than one interpretation for the context
3206 elsif Ambiguous then
3207 Resolution_Failed;
3208 return;
3210 -- Only one interpretation
3212 else
3213 -- Prevent implicit conversions between access-to-subprogram types
3214 -- with different strub modes. Explicit conversions are acceptable in
3215 -- some circumstances. We don't have to be concerned about data or
3216 -- access-to-data types. Conversions between data types can safely
3217 -- drop or add strub attributes from types, because strub effects are
3218 -- associated with the locations rather than values. E.g., converting
3219 -- a hypothetical Strub_Integer variable to Integer would load the
3220 -- value from the variable, enabling stack scrabbing for the
3221 -- enclosing subprogram, and then convert the value to Integer. As
3222 -- for conversions between access-to-data types, that's no different
3223 -- from any other case of type punning.
3225 if Is_Access_Type (Typ)
3226 and then Ekind (Designated_Type (Typ)) = E_Subprogram_Type
3227 and then Is_Access_Type (Expr_Type)
3228 and then Ekind (Designated_Type (Expr_Type)) = E_Subprogram_Type
3229 then
3230 Check_Same_Strub_Mode
3231 (Designated_Type (Typ), Designated_Type (Expr_Type));
3232 end if;
3234 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3235 -- the "+" on T is abstract, and the operands are of universal type,
3236 -- the above code will have (incorrectly) resolved the "+" to the
3237 -- universal one in Standard. Therefore check for this case and give
3238 -- an error. We can't do this earlier, because it would cause legal
3239 -- cases to get errors (when some other type has an abstract "+").
3241 if Ada_Version >= Ada_2005
3242 and then Nkind (N) in N_Op
3243 and then Is_Overloaded (N)
3244 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3245 then
3246 Get_First_Interp (N, I, It);
3247 while Present (It.Typ) loop
3248 if Present (It.Abstract_Op)
3249 and then Etype (It.Abstract_Op) = Typ
3250 then
3251 Nondispatching_Call_To_Abstract_Operation
3252 (N, It.Abstract_Op);
3253 return;
3254 end if;
3256 Get_Next_Interp (I, It);
3257 end loop;
3258 end if;
3260 -- Here we have an acceptable interpretation for the context
3262 -- Propagate type information and normalize tree for various
3263 -- predefined operations. If the context only imposes a class of
3264 -- types, rather than a specific type, propagate the actual type
3265 -- downward.
3267 if Typ = Any_Integer or else
3268 Typ = Any_Boolean or else
3269 Typ = Any_Modular or else
3270 Typ = Any_Real or else
3271 Typ = Any_Discrete
3272 then
3273 Ctx_Type := Expr_Type;
3275 -- Any_Fixed is legal in a real context only if a specific fixed-
3276 -- point type is imposed. If Norman Cohen can be confused by this,
3277 -- it deserves a separate message.
3279 if Typ = Any_Real
3280 and then Expr_Type = Any_Fixed
3281 then
3282 Error_Msg_N ("illegal context for mixed mode operation", N);
3283 Set_Etype (N, Universal_Real);
3284 Ctx_Type := Universal_Real;
3285 end if;
3286 end if;
3288 -- A user-defined operator is transformed into a function call at
3289 -- this point, so that further processing knows that operators are
3290 -- really operators (i.e. are predefined operators). User-defined
3291 -- operators that are intrinsic are just renamings of the predefined
3292 -- ones, and need not be turned into calls either, but if they rename
3293 -- a different operator, we must transform the node accordingly.
3294 -- Instantiations of Unchecked_Conversion are intrinsic but are
3295 -- treated as functions, even if given an operator designator.
3297 if Nkind (N) in N_Op
3298 and then Present (Entity (N))
3299 and then Ekind (Entity (N)) /= E_Operator
3300 then
3301 if not Is_Predefined_Op (Entity (N)) then
3302 Rewrite_Operator_As_Call (N, Entity (N));
3304 elsif Present (Alias (Entity (N)))
3305 and then
3306 Nkind (Parent (Parent (Entity (N)))) =
3307 N_Subprogram_Renaming_Declaration
3308 then
3309 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3311 -- If the node is rewritten, it will be fully resolved in
3312 -- Rewrite_Renamed_Operator.
3314 if Analyzed (N) then
3315 return;
3316 end if;
3317 end if;
3318 end if;
3320 case N_Subexpr'(Nkind (N)) is
3321 when N_Aggregate =>
3322 Resolve_Aggregate (N, Ctx_Type);
3324 when N_Allocator =>
3325 Resolve_Allocator (N, Ctx_Type);
3327 when N_Short_Circuit =>
3328 Resolve_Short_Circuit (N, Ctx_Type);
3330 when N_Attribute_Reference =>
3331 Resolve_Attribute (N, Ctx_Type);
3333 when N_Case_Expression =>
3334 Resolve_Case_Expression (N, Ctx_Type);
3336 when N_Character_Literal =>
3337 Resolve_Character_Literal (N, Ctx_Type);
3339 when N_Delta_Aggregate =>
3340 Resolve_Delta_Aggregate (N, Ctx_Type);
3342 when N_Expanded_Name =>
3343 Resolve_Entity_Name (N, Ctx_Type);
3345 when N_Explicit_Dereference =>
3346 Resolve_Explicit_Dereference (N, Ctx_Type);
3348 when N_Expression_With_Actions =>
3349 Resolve_Expression_With_Actions (N, Ctx_Type);
3351 when N_Extension_Aggregate =>
3352 Resolve_Extension_Aggregate (N, Ctx_Type);
3354 when N_Function_Call =>
3355 Resolve_Call (N, Ctx_Type);
3357 when N_Identifier =>
3358 Resolve_Entity_Name (N, Ctx_Type);
3360 when N_If_Expression =>
3361 Resolve_If_Expression (N, Ctx_Type);
3363 when N_Indexed_Component =>
3364 Resolve_Indexed_Component (N, Ctx_Type);
3366 when N_Integer_Literal =>
3367 Resolve_Integer_Literal (N, Ctx_Type);
3369 when N_Membership_Test =>
3370 Resolve_Membership_Op (N, Ctx_Type);
3372 when N_Null =>
3373 Resolve_Null (N, Ctx_Type);
3375 when N_Op_And
3376 | N_Op_Or
3377 | N_Op_Xor
3379 Resolve_Logical_Op (N, Ctx_Type);
3381 when N_Op_Eq
3382 | N_Op_Ne
3384 Resolve_Equality_Op (N, Ctx_Type);
3386 when N_Op_Ge
3387 | N_Op_Gt
3388 | N_Op_Le
3389 | N_Op_Lt
3391 Resolve_Comparison_Op (N, Ctx_Type);
3393 when N_Op_Not =>
3394 Resolve_Op_Not (N, Ctx_Type);
3396 when N_Op_Add
3397 | N_Op_Divide
3398 | N_Op_Mod
3399 | N_Op_Multiply
3400 | N_Op_Rem
3401 | N_Op_Subtract
3403 Resolve_Arithmetic_Op (N, Ctx_Type);
3405 when N_Op_Concat =>
3406 Resolve_Op_Concat (N, Ctx_Type);
3408 when N_Op_Expon =>
3409 Resolve_Op_Expon (N, Ctx_Type);
3411 when N_Op_Abs
3412 | N_Op_Minus
3413 | N_Op_Plus
3415 Resolve_Unary_Op (N, Ctx_Type);
3417 when N_Op_Shift =>
3418 Resolve_Shift (N, Ctx_Type);
3420 when N_Procedure_Call_Statement =>
3421 Resolve_Call (N, Ctx_Type);
3423 when N_Operator_Symbol =>
3424 Resolve_Operator_Symbol (N, Ctx_Type);
3426 when N_Qualified_Expression =>
3427 Resolve_Qualified_Expression (N, Ctx_Type);
3429 -- Why is the following null, needs a comment ???
3431 when N_Quantified_Expression =>
3432 null;
3434 when N_Raise_Expression =>
3435 Resolve_Raise_Expression (N, Ctx_Type);
3437 when N_Raise_xxx_Error =>
3438 Set_Etype (N, Ctx_Type);
3440 when N_Range =>
3441 Resolve_Range (N, Ctx_Type);
3443 when N_Real_Literal =>
3444 Resolve_Real_Literal (N, Ctx_Type);
3446 when N_Reference =>
3447 Resolve_Reference (N, Ctx_Type);
3449 when N_Selected_Component =>
3450 Resolve_Selected_Component (N, Ctx_Type);
3452 when N_Slice =>
3453 Resolve_Slice (N, Ctx_Type);
3455 when N_String_Literal =>
3456 Resolve_String_Literal (N, Ctx_Type);
3458 when N_Interpolated_String_Literal =>
3459 Resolve_Interpolated_String_Literal (N, Ctx_Type);
3461 when N_Target_Name =>
3462 Resolve_Target_Name (N, Ctx_Type);
3464 when N_Type_Conversion =>
3465 Resolve_Type_Conversion (N, Ctx_Type);
3467 when N_Unchecked_Expression =>
3468 Resolve_Unchecked_Expression (N, Ctx_Type);
3470 when N_Unchecked_Type_Conversion =>
3471 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3472 end case;
3474 -- Mark relevant use-type and use-package clauses as effective using
3475 -- the original node because constant folding may have occurred and
3476 -- removed references that need to be examined.
3478 if Nkind (Original_Node (N)) in N_Op then
3479 Mark_Use_Clauses (Original_Node (N));
3480 end if;
3482 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3483 -- expression of an anonymous access type that occurs in the context
3484 -- of a named general access type, except when the expression is that
3485 -- of a membership test. This ensures proper legality checking in
3486 -- terms of allowed conversions (expressions that would be illegal to
3487 -- convert implicitly are allowed in membership tests).
3489 if Ada_Version >= Ada_2012
3490 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3491 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3492 and then Nkind (Parent (N)) not in N_Membership_Test
3493 then
3494 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3495 Analyze_And_Resolve (N, Ctx_Type);
3496 end if;
3498 -- If the subexpression was replaced by a non-subexpression, then
3499 -- all we do is to expand it. The only legitimate case we know of
3500 -- is converting procedure call statement to entry call statements,
3501 -- but there may be others, so we are making this test general.
3503 if Nkind (N) not in N_Subexpr then
3504 Debug_A_Exit ("resolving ", N, " (done)");
3505 Expand (N);
3506 return;
3507 end if;
3509 -- The expression is definitely NOT overloaded at this point, so
3510 -- we reset the Is_Overloaded flag to avoid any confusion when
3511 -- reanalyzing the node.
3513 Set_Is_Overloaded (N, False);
3515 -- Freeze expression type, entity if it is a name, and designated
3516 -- type if it is an allocator (RM 13.14(10,11,13)).
3518 -- Now that the resolution of the type of the node is complete, and
3519 -- we did not detect an error, we can expand this node. We skip the
3520 -- expand call if we are in a default expression, see section
3521 -- "Handling of Default Expressions" in Sem spec.
3523 Debug_A_Exit ("resolving ", N, " (done)");
3525 -- We unconditionally freeze the expression, even if we are in
3526 -- default expression mode (the Freeze_Expression routine tests this
3527 -- flag and only freezes static types if it is set).
3529 -- Ada 2012 (AI05-177): The declaration of an expression function
3530 -- does not cause freezing, but we never reach here in that case.
3531 -- Here we are resolving the corresponding expanded body, so we do
3532 -- need to perform normal freezing.
3534 -- As elsewhere we do not emit freeze node within a generic.
3536 if not Inside_A_Generic then
3537 Freeze_Expression (N);
3538 end if;
3540 -- Now we can do the expansion
3542 Expand (N);
3543 end if;
3544 end Resolve;
3546 -------------
3547 -- Resolve --
3548 -------------
3550 -- Version with check(s) suppressed
3552 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3553 begin
3554 if Suppress = All_Checks then
3555 declare
3556 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3557 begin
3558 Scope_Suppress.Suppress := (others => True);
3559 Resolve (N, Typ);
3560 Scope_Suppress.Suppress := Sva;
3561 end;
3563 else
3564 declare
3565 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3566 begin
3567 Scope_Suppress.Suppress (Suppress) := True;
3568 Resolve (N, Typ);
3569 Scope_Suppress.Suppress (Suppress) := Svg;
3570 end;
3571 end if;
3572 end Resolve;
3574 -------------
3575 -- Resolve --
3576 -------------
3578 -- Version with implicit type
3580 procedure Resolve (N : Node_Id) is
3581 begin
3582 Resolve (N, Etype (N));
3583 end Resolve;
3585 ---------------------
3586 -- Resolve_Actuals --
3587 ---------------------
3589 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3590 Loc : constant Source_Ptr := Sloc (N);
3591 A : Node_Id;
3592 A_Typ : Entity_Id := Empty; -- init to avoid warning
3593 F : Entity_Id;
3594 F_Typ : Entity_Id;
3595 Prev : Node_Id := Empty;
3596 Orig_A : Node_Id;
3597 Real_F : Entity_Id := Empty; -- init to avoid warning
3599 Real_Subp : Entity_Id;
3600 -- If the subprogram being called is an inherited operation for
3601 -- a formal derived type in an instance, Real_Subp is the subprogram
3602 -- that will be called. It may have different formal names than the
3603 -- operation of the formal in the generic, so after actual is resolved
3604 -- the name of the actual in a named association must carry the name
3605 -- of the actual of the subprogram being called.
3607 procedure Check_Aliased_Parameter;
3608 -- Check rules on aliased parameters and related accessibility rules
3609 -- in (RM 3.10.2 (10.2-10.4)).
3611 procedure Check_Argument_Order;
3612 -- Performs a check for the case where the actuals are all simple
3613 -- identifiers that correspond to the formal names, but in the wrong
3614 -- order, which is considered suspicious and cause for a warning.
3616 procedure Check_Prefixed_Call;
3617 -- If the original node is an overloaded call in prefix notation,
3618 -- insert an 'Access or a dereference as needed over the first actual.
3619 -- Try_Object_Operation has already verified that there is a valid
3620 -- interpretation, but the form of the actual can only be determined
3621 -- once the primitive operation is identified.
3623 procedure Insert_Default;
3624 -- If the actual is missing in a call, insert in the actuals list
3625 -- an instance of the default expression. The insertion is always
3626 -- a named association.
3628 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3629 -- Check whether T1 and T2, or their full views, are derived from a
3630 -- common type. Used to enforce the restrictions on array conversions
3631 -- of AI95-00246.
3633 function Static_Concatenation (N : Node_Id) return Boolean;
3634 -- Predicate to determine whether an actual that is a concatenation
3635 -- will be evaluated statically and does not need a transient scope.
3636 -- This must be determined before the actual is resolved and expanded
3637 -- because if needed the transient scope must be introduced earlier.
3639 -----------------------------
3640 -- Check_Aliased_Parameter --
3641 -----------------------------
3643 procedure Check_Aliased_Parameter is
3644 Nominal_Subt : Entity_Id;
3646 begin
3647 if Is_Aliased (F) then
3648 if Is_Tagged_Type (A_Typ) then
3649 null;
3651 elsif Is_Aliased_View (A) then
3652 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3653 Nominal_Subt := Base_Type (A_Typ);
3654 else
3655 Nominal_Subt := A_Typ;
3656 end if;
3658 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3659 null;
3661 -- In a generic body assume the worst for generic formals:
3662 -- they can have a constrained partial view (AI05-041).
3664 elsif Has_Discriminants (F_Typ)
3665 and then not Is_Constrained (F_Typ)
3666 and then not Object_Type_Has_Constrained_Partial_View
3667 (Typ => F_Typ, Scop => Current_Scope)
3668 then
3669 null;
3671 else
3672 Error_Msg_NE ("untagged actual does not statically match "
3673 & "aliased formal&", A, F);
3674 end if;
3676 else
3677 Error_Msg_NE ("actual for aliased formal& must be "
3678 & "aliased object", A, F);
3679 end if;
3681 if Ekind (Nam) = E_Procedure then
3682 null;
3684 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3685 if Nkind (Parent (N)) = N_Type_Conversion
3686 and then Type_Access_Level (Etype (Parent (N)))
3687 < Static_Accessibility_Level (A, Object_Decl_Level)
3688 then
3689 Error_Msg_N ("aliased actual has wrong accessibility", A);
3690 end if;
3692 elsif Nkind (Parent (N)) = N_Qualified_Expression
3693 and then Nkind (Parent (Parent (N))) = N_Allocator
3694 and then Type_Access_Level (Etype (Parent (Parent (N))))
3695 < Static_Accessibility_Level (A, Object_Decl_Level)
3696 then
3697 Error_Msg_N
3698 ("aliased actual in allocator has wrong accessibility", A);
3699 end if;
3700 end if;
3701 end Check_Aliased_Parameter;
3703 --------------------------
3704 -- Check_Argument_Order --
3705 --------------------------
3707 procedure Check_Argument_Order is
3708 begin
3709 -- Nothing to do if no parameters, or original node is neither a
3710 -- function call nor a procedure call statement (happens in the
3711 -- operator-transformed-to-function call case), or the call is to an
3712 -- operator symbol (which is usually in infix form), or the call does
3713 -- not come from source, or this warning is off.
3715 if not Warn_On_Parameter_Order
3716 or else No (Parameter_Associations (N))
3717 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3718 or else (Nkind (Name (N)) = N_Identifier
3719 and then Present (Entity (Name (N)))
3720 and then Nkind (Entity (Name (N))) =
3721 N_Defining_Operator_Symbol)
3722 or else not Comes_From_Source (N)
3723 then
3724 return;
3725 end if;
3727 declare
3728 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3730 begin
3731 -- Nothing to do if only one parameter
3733 if Nargs < 2 then
3734 return;
3735 end if;
3737 -- Here if at least two arguments
3739 declare
3740 Actuals : array (1 .. Nargs) of Node_Id;
3741 Actual : Node_Id;
3742 Formal : Node_Id;
3744 Wrong_Order : Boolean := False;
3745 -- Set True if an out of order case is found
3747 begin
3748 -- Collect identifier names of actuals, fail if any actual is
3749 -- not a simple identifier, and record max length of name.
3751 Actual := First (Parameter_Associations (N));
3752 for J in Actuals'Range loop
3753 if Nkind (Actual) /= N_Identifier then
3754 return;
3755 else
3756 Actuals (J) := Actual;
3757 Next (Actual);
3758 end if;
3759 end loop;
3761 -- If we got this far, all actuals are identifiers and the list
3762 -- of their names is stored in the Actuals array.
3764 Formal := First_Formal (Nam);
3765 for J in Actuals'Range loop
3767 -- If we ran out of formals, that's odd, probably an error
3768 -- which will be detected elsewhere, but abandon the search.
3770 if No (Formal) then
3771 return;
3772 end if;
3774 -- If name matches and is in order OK
3776 if Chars (Formal) = Chars (Actuals (J)) then
3777 null;
3779 else
3780 -- If no match, see if it is elsewhere in list and if so
3781 -- flag potential wrong order if type is compatible.
3783 for K in Actuals'Range loop
3784 if Chars (Formal) = Chars (Actuals (K))
3785 and then
3786 Has_Compatible_Type (Actuals (K), Etype (Formal))
3787 then
3788 Wrong_Order := True;
3789 goto Continue;
3790 end if;
3791 end loop;
3793 -- No match
3795 return;
3796 end if;
3798 <<Continue>> Next_Formal (Formal);
3799 end loop;
3801 -- If Formals left over, also probably an error, skip warning
3803 if Present (Formal) then
3804 return;
3805 end if;
3807 -- Here we give the warning if something was out of order
3809 if Wrong_Order then
3810 Error_Msg_N
3811 ("?.p?actuals for this call may be in wrong order", N);
3812 end if;
3813 end;
3814 end;
3815 end Check_Argument_Order;
3817 -------------------------
3818 -- Check_Prefixed_Call --
3819 -------------------------
3821 procedure Check_Prefixed_Call is
3822 Act : constant Node_Id := First_Actual (N);
3823 A_Type : constant Entity_Id := Etype (Act);
3824 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3825 Orig : constant Node_Id := Original_Node (N);
3826 New_A : Node_Id;
3828 begin
3829 -- Check whether the call is a prefixed call, with or without
3830 -- additional actuals.
3832 if Nkind (Orig) = N_Selected_Component
3833 or else
3834 (Nkind (Orig) = N_Indexed_Component
3835 and then Nkind (Prefix (Orig)) = N_Selected_Component
3836 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3837 and then Is_Entity_Name (Act)
3838 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3839 then
3840 if Is_Access_Type (A_Type)
3841 and then not Is_Access_Type (F_Type)
3842 then
3843 -- Introduce dereference on object in prefix
3845 New_A :=
3846 Make_Explicit_Dereference (Sloc (Act),
3847 Prefix => Relocate_Node (Act));
3848 Rewrite (Act, New_A);
3849 Analyze (Act);
3851 elsif Is_Access_Type (F_Type)
3852 and then not Is_Access_Type (A_Type)
3853 then
3854 -- Introduce an implicit 'Access in prefix
3856 if not Is_Aliased_View (Act) then
3857 Error_Msg_NE
3858 ("object in prefixed call to& must be aliased "
3859 & "(RM 4.1.3 (13 1/2))",
3860 Prefix (Act), Nam);
3861 end if;
3863 Rewrite (Act,
3864 Make_Attribute_Reference (Loc,
3865 Attribute_Name => Name_Access,
3866 Prefix => Relocate_Node (Act)));
3867 end if;
3869 Analyze (Act);
3870 end if;
3871 end Check_Prefixed_Call;
3873 --------------------
3874 -- Insert_Default --
3875 --------------------
3877 procedure Insert_Default is
3878 Actval : Node_Id;
3879 Assoc : Node_Id;
3881 begin
3882 -- Missing argument in call, nothing to insert
3884 if No (Default_Value (F)) then
3885 return;
3887 else
3888 -- Note that we do a full New_Copy_Tree, so that any associated
3889 -- Itypes are properly copied. This may not be needed any more,
3890 -- but it does no harm as a safety measure. Defaults of a generic
3891 -- formal may be out of bounds of the corresponding actual (see
3892 -- cc1311b) and an additional check may be required.
3894 Actval :=
3895 New_Copy_Tree
3896 (Default_Value (F),
3897 New_Scope => Current_Scope,
3898 New_Sloc => Loc);
3900 -- Propagate dimension information, if any.
3902 Copy_Dimensions (Default_Value (F), Actval);
3904 if Is_Concurrent_Type (Scope (Nam))
3905 and then Has_Discriminants (Scope (Nam))
3906 then
3907 Replace_Actual_Discriminants (N, Actval);
3908 end if;
3910 if Is_Overloadable (Nam)
3911 and then Present (Alias (Nam))
3912 then
3913 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3914 and then not Is_Tagged_Type (Etype (F))
3915 then
3916 -- If default is a real literal, do not introduce a
3917 -- conversion whose effect may depend on the run-time
3918 -- size of universal real.
3920 if Nkind (Actval) = N_Real_Literal then
3921 Set_Etype (Actval, Base_Type (Etype (F)));
3922 else
3923 Actval := Unchecked_Convert_To (Etype (F), Actval);
3924 end if;
3925 end if;
3927 if Is_Scalar_Type (Etype (F)) then
3928 Enable_Range_Check (Actval);
3929 end if;
3931 Set_Parent (Actval, N);
3933 -- Resolve aggregates with their base type, to avoid scope
3934 -- anomalies: the subtype was first built in the subprogram
3935 -- declaration, and the current call may be nested.
3937 if Nkind (Actval) = N_Aggregate then
3938 Analyze_And_Resolve (Actval, Etype (F));
3939 else
3940 Analyze_And_Resolve (Actval, Etype (Actval));
3941 end if;
3943 else
3944 Set_Parent (Actval, N);
3946 -- See note above concerning aggregates
3948 if Nkind (Actval) = N_Aggregate
3949 and then Has_Discriminants (Etype (Actval))
3950 then
3951 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3953 -- Resolve entities with their own type, which may differ from
3954 -- the type of a reference in a generic context because of the
3955 -- trick used in Save_Global_References.Set_Global_Type to set
3956 -- full views forcefully, which did not anticipate the need to
3957 -- re-analyze default values in calls.
3959 elsif Is_Entity_Name (Actval) then
3960 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3962 -- Ditto for calls whose name is an entity, for the same reason
3964 elsif Nkind (Actval) = N_Function_Call
3965 and then Is_Entity_Name (Name (Actval))
3966 then
3967 Analyze_And_Resolve (Actval, Etype (Entity (Name (Actval))));
3969 else
3970 Analyze_And_Resolve (Actval, Etype (Actval));
3971 end if;
3972 end if;
3974 -- If default is a tag indeterminate function call, propagate tag
3975 -- to obtain proper dispatching.
3977 if Is_Controlling_Formal (F)
3978 and then Nkind (Default_Value (F)) = N_Function_Call
3979 then
3980 Set_Is_Controlling_Actual (Actval);
3981 end if;
3982 end if;
3984 -- If the default expression raises constraint error, then just
3985 -- silently replace it with an N_Raise_Constraint_Error node, since
3986 -- we already gave the warning on the subprogram spec. If node is
3987 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3988 -- the warnings removal machinery.
3990 if Raises_Constraint_Error (Actval)
3991 and then Nkind (Actval) /= N_Raise_Constraint_Error
3992 then
3993 Rewrite (Actval,
3994 Make_Raise_Constraint_Error (Loc,
3995 Reason => CE_Range_Check_Failed));
3997 Set_Raises_Constraint_Error (Actval);
3998 Set_Etype (Actval, Etype (F));
3999 end if;
4001 Assoc :=
4002 Make_Parameter_Association (Loc,
4003 Explicit_Actual_Parameter => Actval,
4004 Selector_Name => Make_Identifier (Loc, Chars (F)));
4006 -- Case of insertion is first named actual
4008 if No (Prev)
4009 or else Nkind (Parent (Prev)) /= N_Parameter_Association
4010 then
4011 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
4012 Set_First_Named_Actual (N, Actval);
4014 if No (Prev) then
4015 if No (Parameter_Associations (N)) then
4016 Set_Parameter_Associations (N, New_List (Assoc));
4017 else
4018 Append (Assoc, Parameter_Associations (N));
4019 end if;
4021 else
4022 Insert_After (Prev, Assoc);
4023 end if;
4025 -- Case of insertion is not first named actual
4027 else
4028 Set_Next_Named_Actual
4029 (Assoc, Next_Named_Actual (Parent (Prev)));
4030 Set_Next_Named_Actual (Parent (Prev), Actval);
4031 Append (Assoc, Parameter_Associations (N));
4032 end if;
4034 Mark_Rewrite_Insertion (Assoc);
4035 Mark_Rewrite_Insertion (Actval);
4037 Prev := Actval;
4038 end Insert_Default;
4040 -------------------
4041 -- Same_Ancestor --
4042 -------------------
4044 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
4045 FT1 : Entity_Id := T1;
4046 FT2 : Entity_Id := T2;
4048 begin
4049 if Is_Private_Type (T1)
4050 and then Present (Full_View (T1))
4051 then
4052 FT1 := Full_View (T1);
4053 end if;
4055 if Is_Private_Type (T2)
4056 and then Present (Full_View (T2))
4057 then
4058 FT2 := Full_View (T2);
4059 end if;
4061 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
4062 end Same_Ancestor;
4064 --------------------------
4065 -- Static_Concatenation --
4066 --------------------------
4068 function Static_Concatenation (N : Node_Id) return Boolean is
4069 begin
4070 case Nkind (N) is
4071 when N_String_Literal =>
4072 return True;
4074 when N_Op_Concat =>
4076 -- Concatenation is static when both operands are static and
4077 -- the concatenation operator is a predefined one.
4079 return Scope (Entity (N)) = Standard_Standard
4080 and then
4081 Static_Concatenation (Left_Opnd (N))
4082 and then
4083 Static_Concatenation (Right_Opnd (N));
4085 when others =>
4086 if Is_Entity_Name (N) then
4087 declare
4088 Ent : constant Entity_Id := Entity (N);
4089 begin
4090 return Ekind (Ent) = E_Constant
4091 and then Present (Constant_Value (Ent))
4092 and then
4093 Is_OK_Static_Expression (Constant_Value (Ent));
4094 end;
4096 else
4097 return False;
4098 end if;
4099 end case;
4100 end Static_Concatenation;
4102 -- Start of processing for Resolve_Actuals
4104 begin
4105 Check_Argument_Order;
4107 if Is_Overloadable (Nam)
4108 and then Is_Inherited_Operation (Nam)
4109 and then In_Instance
4110 and then Present (Alias (Nam))
4111 and then Present (Overridden_Operation (Alias (Nam)))
4112 then
4113 Real_Subp := Alias (Nam);
4114 else
4115 Real_Subp := Empty;
4116 end if;
4118 if Present (First_Actual (N)) then
4119 Check_Prefixed_Call;
4120 end if;
4122 A := First_Actual (N);
4123 F := First_Formal (Nam);
4125 if Present (Real_Subp) then
4126 Real_F := First_Formal (Real_Subp);
4127 end if;
4129 while Present (F) loop
4130 if No (A) and then Needs_No_Actuals (Nam) then
4131 null;
4133 -- If we have an error in any formal or actual, indicated by a type
4134 -- of Any_Type, then abandon resolution attempt, and set result type
4135 -- to Any_Type.
4137 elsif Etype (F) = Any_Type then
4138 Set_Etype (N, Any_Type);
4139 return;
4141 elsif Present (A) and then Etype (A) = Any_Type then
4142 -- For the peculiar case of a user-defined comparison or equality
4143 -- operator that does not return a boolean type, the operands may
4144 -- have been ambiguous for the predefined operator and, therefore,
4145 -- marked with Any_Type. Since the operation has been resolved to
4146 -- the user-defined operator, that is irrelevant, so reset Etype.
4148 if Nkind (Original_Node (N)) in N_Op_Compare
4149 and then not Is_Boolean_Type (Etype (N))
4150 then
4151 Set_Etype (A, Etype (F));
4153 -- Also skip this if the actual is a Raise_Expression, whose type
4154 -- is imposed from context.
4156 elsif Nkind (A) = N_Raise_Expression then
4157 null;
4159 else
4160 Set_Etype (N, Any_Type);
4161 return;
4162 end if;
4163 end if;
4165 -- Case where actual is present
4167 -- If the actual is an entity, generate a reference to it now. We
4168 -- do this before the actual is resolved, because a formal of some
4169 -- protected subprogram, or a task discriminant, will be rewritten
4170 -- during expansion, and the source entity reference may be lost.
4172 if Present (A)
4173 and then Is_Entity_Name (A)
4174 and then Comes_From_Source (A)
4175 then
4176 -- Annotate the tree by creating a variable reference marker when
4177 -- the actual denotes a variable reference, in case the reference
4178 -- is folded or optimized away. The variable reference marker is
4179 -- automatically saved for later examination by the ABE Processing
4180 -- phase. The status of the reference is set as follows:
4182 -- status mode
4183 -- read IN, IN OUT
4184 -- write IN OUT, OUT
4186 if Needs_Variable_Reference_Marker
4187 (N => A,
4188 Calls_OK => True)
4189 then
4190 Build_Variable_Reference_Marker
4191 (N => A,
4192 Read => Ekind (F) /= E_Out_Parameter,
4193 Write => Ekind (F) /= E_In_Parameter);
4194 end if;
4196 Orig_A := Entity (A);
4198 if Present (Orig_A) then
4199 if Is_Formal (Orig_A)
4200 and then Ekind (F) /= E_In_Parameter
4201 then
4202 Generate_Reference (Orig_A, A, 'm');
4204 elsif not Is_Overloaded (A) then
4205 if Ekind (F) /= E_Out_Parameter then
4206 Generate_Reference (Orig_A, A);
4208 -- RM 6.4.1(12): For an out parameter that is passed by
4209 -- copy, the formal parameter object is created, and:
4211 -- * For an access type, the formal parameter is initialized
4212 -- from the value of the actual, without checking that the
4213 -- value satisfies any constraint, any predicate, or any
4214 -- exclusion of the null value.
4216 -- * For a scalar type that has the Default_Value aspect
4217 -- specified, the formal parameter is initialized from the
4218 -- value of the actual, without checking that the value
4219 -- satisfies any constraint or any predicate.
4220 -- I do not understand why this case is included??? this is
4221 -- not a case where an OUT parameter is treated as IN OUT.
4223 -- * For a composite type with discriminants or that has
4224 -- implicit initial values for any subcomponents, the
4225 -- behavior is as for an in out parameter passed by copy.
4227 -- Hence for these cases we generate the read reference now
4228 -- (the write reference will be generated later by
4229 -- Note_Possible_Modification).
4231 elsif Is_By_Copy_Type (Etype (F))
4232 and then
4233 (Is_Access_Type (Etype (F))
4234 or else
4235 (Is_Scalar_Type (Etype (F))
4236 and then
4237 Present (Default_Aspect_Value (Etype (F))))
4238 or else
4239 (Is_Composite_Type (Etype (F))
4240 and then (Has_Discriminants (Etype (F))
4241 or else Is_Partially_Initialized_Type
4242 (Etype (F)))))
4243 then
4244 Generate_Reference (Orig_A, A);
4245 end if;
4246 end if;
4247 end if;
4248 end if;
4250 if Present (A)
4251 and then (Nkind (Parent (A)) /= N_Parameter_Association
4252 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4253 then
4254 -- If style checking mode on, check match of formal name
4256 if Style_Check then
4257 if Nkind (Parent (A)) = N_Parameter_Association then
4258 Check_Identifier (Selector_Name (Parent (A)), F);
4259 end if;
4260 end if;
4262 -- If the formal is Out or In_Out, do not resolve and expand the
4263 -- conversion, because it is subsequently expanded into explicit
4264 -- temporaries and assignments. However, the object of the
4265 -- conversion can be resolved. An exception is the case of tagged
4266 -- type conversion with a class-wide actual. In that case we want
4267 -- the tag check to occur and no temporary will be needed (no
4268 -- representation change can occur) and the parameter is passed by
4269 -- reference, so we go ahead and resolve the type conversion.
4270 -- Another exception is the case of reference to component or
4271 -- subcomponent of a bit-packed array, in which case we want to
4272 -- defer expansion to the point the in and out assignments are
4273 -- performed.
4275 if Ekind (F) /= E_In_Parameter
4276 and then Nkind (A) = N_Type_Conversion
4277 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4278 and then not Is_Interface (Etype (A))
4279 then
4280 declare
4281 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4283 begin
4284 -- Check RM 4.6 (24.2/2)
4286 if Is_Array_Type (Etype (F))
4287 and then Is_View_Conversion (A)
4288 then
4289 -- In a view conversion, the conversion must be legal in
4290 -- both directions, and thus both component types must be
4291 -- aliased, or neither (4.6 (8)).
4293 -- Check RM 4.6 (24.8/2)
4295 if Has_Aliased_Components (Expr_Typ) /=
4296 Has_Aliased_Components (Etype (F))
4297 then
4298 -- This normally illegal conversion is legal in an
4299 -- expanded instance body because of RM 12.3(11).
4300 -- At runtime, conversion must create a new object.
4302 if not In_Instance then
4303 Error_Msg_N
4304 ("both component types in a view conversion must"
4305 & " be aliased, or neither", A);
4306 end if;
4308 -- Check RM 4.6 (24/3)
4310 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4311 -- Check view conv between unrelated by ref array
4312 -- types.
4314 if Is_By_Reference_Type (Etype (F))
4315 or else Is_By_Reference_Type (Expr_Typ)
4316 then
4317 Error_Msg_N
4318 ("view conversion between unrelated by reference "
4319 & "array types not allowed ('A'I-00246)", A);
4321 -- In Ada 2005 mode, check view conversion component
4322 -- type cannot be private, tagged, or volatile. Note
4323 -- that we only apply this to source conversions. The
4324 -- generated code can contain conversions which are
4325 -- not subject to this test, and we cannot extract the
4326 -- component type in such cases since it is not
4327 -- present.
4329 elsif Comes_From_Source (A)
4330 and then Ada_Version >= Ada_2005
4331 then
4332 declare
4333 Comp_Type : constant Entity_Id :=
4334 Component_Type (Expr_Typ);
4335 begin
4336 if (Is_Private_Type (Comp_Type)
4337 and then not Is_Generic_Type (Comp_Type))
4338 or else Is_Tagged_Type (Comp_Type)
4339 or else Is_Volatile (Comp_Type)
4340 then
4341 Error_Msg_N
4342 ("component type of a view conversion " &
4343 "cannot be private, tagged, or volatile" &
4344 " (RM 4.6 (24))",
4345 Expression (A));
4346 end if;
4347 end;
4348 end if;
4349 end if;
4351 -- AI12-0074 & AI12-0377
4352 -- Check 6.4.1: If the mode is out, the actual parameter is
4353 -- a view conversion, and the type of the formal parameter
4354 -- is a scalar type, then either:
4355 -- - the target and operand type both do not have the
4356 -- Default_Value aspect specified; or
4357 -- - the target and operand type both have the
4358 -- Default_Value aspect specified, and there shall exist
4359 -- a type (other than a root numeric type) that is an
4360 -- ancestor of both the target type and the operand
4361 -- type.
4363 elsif Ekind (F) = E_Out_Parameter
4364 and then Is_Scalar_Type (Etype (F))
4365 then
4366 if Has_Default_Aspect (Etype (F)) /=
4367 Has_Default_Aspect (Expr_Typ)
4368 then
4369 Error_Msg_N
4370 ("view conversion requires Default_Value on both " &
4371 "types (RM 6.4.1)", A);
4372 elsif Has_Default_Aspect (Expr_Typ)
4373 and then not Same_Ancestor (Etype (F), Expr_Typ)
4374 then
4375 Error_Msg_N
4376 ("view conversion between unrelated types with "
4377 & "Default_Value not allowed (RM 6.4.1)", A);
4378 end if;
4379 end if;
4380 end;
4382 -- Resolve expression if conversion is all OK
4384 if (Conversion_OK (A)
4385 or else Valid_Conversion (A, Etype (A), Expression (A)))
4386 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4387 then
4388 Resolve (Expression (A));
4389 end if;
4391 -- If the actual is a function call that returns a limited
4392 -- unconstrained object that needs finalization, create a
4393 -- transient scope for it, so that it can receive the proper
4394 -- finalization list.
4396 elsif Expander_Active
4397 and then Nkind (A) = N_Function_Call
4398 and then Is_Limited_Record (Etype (F))
4399 and then not Is_Constrained (Etype (F))
4400 and then (Needs_Finalization (Etype (F))
4401 or else Has_Task (Etype (F)))
4402 then
4403 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4404 Resolve (A, Etype (F));
4406 -- A small optimization: if one of the actuals is a concatenation
4407 -- create a block around a procedure call to recover stack space.
4408 -- This alleviates stack usage when several procedure calls in
4409 -- the same statement list use concatenation. We do not perform
4410 -- this wrapping for code statements, where the argument is a
4411 -- static string, and we want to preserve warnings involving
4412 -- sequences of such statements.
4414 elsif Expander_Active
4415 and then Nkind (A) = N_Op_Concat
4416 and then Nkind (N) = N_Procedure_Call_Statement
4417 and then not (Is_Intrinsic_Subprogram (Nam)
4418 and then Chars (Nam) = Name_Asm)
4419 and then not Static_Concatenation (A)
4420 then
4421 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4422 Resolve (A, Etype (F));
4424 else
4425 if Nkind (A) = N_Type_Conversion
4426 and then Is_Array_Type (Etype (F))
4427 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4428 and then
4429 (Is_Limited_Type (Etype (F))
4430 or else Is_Limited_Type (Etype (Expression (A))))
4431 then
4432 Error_Msg_N
4433 ("conversion between unrelated limited array types not "
4434 & "allowed ('A'I-00246)", A);
4436 if Is_Limited_Type (Etype (F)) then
4437 Explain_Limited_Type (Etype (F), A);
4438 end if;
4440 if Is_Limited_Type (Etype (Expression (A))) then
4441 Explain_Limited_Type (Etype (Expression (A)), A);
4442 end if;
4443 end if;
4445 -- (Ada 2005: AI-251): If the actual is an allocator whose
4446 -- directly designated type is a class-wide interface, we build
4447 -- an anonymous access type to use it as the type of the
4448 -- allocator. Later, when the subprogram call is expanded, if
4449 -- the interface has a secondary dispatch table the expander
4450 -- will add a type conversion to force the correct displacement
4451 -- of the pointer.
4453 if Nkind (A) = N_Allocator then
4454 declare
4455 DDT : constant Entity_Id :=
4456 Directly_Designated_Type (Base_Type (Etype (F)));
4458 begin
4459 -- Displace the pointer to the object to reference its
4460 -- secondary dispatch table.
4462 if Is_Class_Wide_Type (DDT)
4463 and then Is_Interface (DDT)
4464 then
4465 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4466 Analyze_And_Resolve (A, Etype (F),
4467 Suppress => Access_Check);
4468 end if;
4470 -- Ada 2005, AI-162:If the actual is an allocator, the
4471 -- innermost enclosing statement is the master of the
4472 -- created object. This needs to be done with expansion
4473 -- enabled only, otherwise the transient scope will not
4474 -- be removed in the expansion of the wrapped construct.
4476 if Expander_Active
4477 and then (Needs_Finalization (DDT)
4478 or else Has_Task (DDT))
4479 then
4480 Establish_Transient_Scope
4481 (A, Manage_Sec_Stack => False);
4482 end if;
4483 end;
4485 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4486 Check_Restriction (No_Access_Parameter_Allocators, A);
4487 end if;
4488 end if;
4490 -- (Ada 2005): The call may be to a primitive operation of a
4491 -- tagged synchronized type, declared outside of the type. In
4492 -- this case the controlling actual must be converted to its
4493 -- corresponding record type, which is the formal type. The
4494 -- actual may be a subtype, either because of a constraint or
4495 -- because it is a generic actual, so use base type to locate
4496 -- concurrent type.
4498 F_Typ := Base_Type (Etype (F));
4500 if Is_Tagged_Type (F_Typ)
4501 and then (Is_Concurrent_Type (F_Typ)
4502 or else Is_Concurrent_Record_Type (F_Typ))
4503 then
4504 -- If the actual is overloaded, look for an interpretation
4505 -- that has a synchronized type.
4507 if not Is_Overloaded (A) then
4508 A_Typ := Base_Type (Etype (A));
4510 else
4511 declare
4512 Index : Interp_Index;
4513 It : Interp;
4515 begin
4516 Get_First_Interp (A, Index, It);
4517 while Present (It.Typ) loop
4518 if Is_Concurrent_Type (It.Typ)
4519 or else Is_Concurrent_Record_Type (It.Typ)
4520 then
4521 A_Typ := Base_Type (It.Typ);
4522 exit;
4523 end if;
4525 Get_Next_Interp (Index, It);
4526 end loop;
4527 end;
4528 end if;
4530 declare
4531 Full_A_Typ : Entity_Id;
4533 begin
4534 if Present (Full_View (A_Typ)) then
4535 Full_A_Typ := Base_Type (Full_View (A_Typ));
4536 else
4537 Full_A_Typ := A_Typ;
4538 end if;
4540 -- Tagged synchronized type (case 1): the actual is a
4541 -- concurrent type.
4543 if Is_Concurrent_Type (A_Typ)
4544 and then Corresponding_Record_Type (A_Typ) = F_Typ
4545 then
4546 Rewrite (A,
4547 Unchecked_Convert_To
4548 (Corresponding_Record_Type (A_Typ), A));
4549 Resolve (A, Etype (F));
4551 -- Tagged synchronized type (case 2): the formal is a
4552 -- concurrent type.
4554 elsif Ekind (Full_A_Typ) = E_Record_Type
4555 and then Present
4556 (Corresponding_Concurrent_Type (Full_A_Typ))
4557 and then Is_Concurrent_Type (F_Typ)
4558 and then Present (Corresponding_Record_Type (F_Typ))
4559 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4560 then
4561 Resolve (A, Corresponding_Record_Type (F_Typ));
4563 -- Common case
4565 else
4566 Resolve (A, Etype (F));
4567 end if;
4568 end;
4570 -- Not a synchronized operation
4572 else
4573 Resolve (A, Etype (F));
4574 end if;
4575 end if;
4577 A_Typ := Etype (A);
4578 F_Typ := Etype (F);
4580 -- An actual cannot be an untagged formal incomplete type
4582 if Ekind (A_Typ) = E_Incomplete_Type
4583 and then not Is_Tagged_Type (A_Typ)
4584 and then Is_Generic_Type (A_Typ)
4585 then
4586 Error_Msg_N
4587 ("invalid use of untagged formal incomplete type", A);
4588 end if;
4590 -- For mode IN, if actual is an entity, and the type of the formal
4591 -- has warnings suppressed, then we reset Never_Set_In_Source for
4592 -- the calling entity. The reason for this is to catch cases like
4593 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4594 -- uses trickery to modify an IN parameter.
4596 if Ekind (F) = E_In_Parameter
4597 and then Is_Entity_Name (A)
4598 and then Present (Entity (A))
4599 and then Ekind (Entity (A)) = E_Variable
4600 and then Has_Warnings_Off (F_Typ)
4601 then
4602 Set_Never_Set_In_Source (Entity (A), False);
4603 end if;
4605 -- Perform error checks for IN and IN OUT parameters
4607 if Ekind (F) /= E_Out_Parameter then
4609 -- Check unset reference. For scalar parameters, it is clearly
4610 -- wrong to pass an uninitialized value as either an IN or
4611 -- IN-OUT parameter. For composites, it is also clearly an
4612 -- error to pass a completely uninitialized value as an IN
4613 -- parameter, but the case of IN OUT is trickier. We prefer
4614 -- not to give a warning here. For example, suppose there is
4615 -- a routine that sets some component of a record to False.
4616 -- It is perfectly reasonable to make this IN-OUT and allow
4617 -- either initialized or uninitialized records to be passed
4618 -- in this case.
4620 -- For partially initialized composite values, we also avoid
4621 -- warnings, since it is quite likely that we are passing a
4622 -- partially initialized value and only the initialized fields
4623 -- will in fact be read in the subprogram.
4625 if Is_Scalar_Type (A_Typ)
4626 or else (Ekind (F) = E_In_Parameter
4627 and then not Is_Partially_Initialized_Type (A_Typ))
4628 then
4629 Check_Unset_Reference (A);
4630 end if;
4632 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4633 -- actual to a nested call, since this constitutes a reading of
4634 -- the parameter, which is not allowed.
4636 if Ada_Version = Ada_83
4637 and then Is_Entity_Name (A)
4638 and then Ekind (Entity (A)) = E_Out_Parameter
4639 then
4640 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4641 end if;
4642 end if;
4644 -- In -gnatd.q mode, forget that a given array is constant when
4645 -- it is passed as an IN parameter to a foreign-convention
4646 -- subprogram. This is in case the subprogram evilly modifies the
4647 -- object. Of course, correct code would use IN OUT.
4649 if Debug_Flag_Dot_Q
4650 and then Ekind (F) = E_In_Parameter
4651 and then Has_Foreign_Convention (Nam)
4652 and then Is_Array_Type (F_Typ)
4653 and then Nkind (A) in N_Has_Entity
4654 and then Present (Entity (A))
4655 then
4656 Set_Is_True_Constant (Entity (A), False);
4657 end if;
4659 -- Case of OUT or IN OUT parameter
4661 if Ekind (F) /= E_In_Parameter then
4663 -- For an Out parameter, check for useless assignment. Note
4664 -- that we can't set Last_Assignment this early, because we may
4665 -- kill current values in Resolve_Call, and that call would
4666 -- clobber the Last_Assignment field.
4668 -- Note: call Warn_On_Useless_Assignment before doing the check
4669 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4670 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4671 -- reflects the last assignment, not this one.
4673 if Ekind (F) = E_Out_Parameter then
4674 if Warn_On_Modified_As_Out_Parameter (F)
4675 and then Is_Entity_Name (A)
4676 and then Present (Entity (A))
4677 and then Comes_From_Source (N)
4678 then
4679 Warn_On_Useless_Assignment (Entity (A), A);
4680 end if;
4681 end if;
4683 -- Validate the form of the actual. Note that the call to
4684 -- Is_OK_Variable_For_Out_Formal generates the required
4685 -- reference in this case.
4687 -- A call to an initialization procedure for an aggregate
4688 -- component may initialize a nested component of a constant
4689 -- designated object. In this context the object is variable.
4691 if not Is_OK_Variable_For_Out_Formal (A)
4692 and then not Is_Init_Proc (Nam)
4693 then
4694 Error_Msg_NE ("actual for& must be a variable", A, F);
4696 if Is_Subprogram (Current_Scope) then
4697 if Is_Invariant_Procedure (Current_Scope)
4698 or else Is_Partial_Invariant_Procedure (Current_Scope)
4699 then
4700 Error_Msg_N
4701 ("function used in invariant cannot modify its "
4702 & "argument", F);
4704 elsif Is_Predicate_Function (Current_Scope) then
4705 Error_Msg_N
4706 ("function used in predicate cannot modify its "
4707 & "argument", F);
4708 end if;
4709 end if;
4710 end if;
4712 -- What's the following about???
4714 if Is_Entity_Name (A) then
4715 Kill_Checks (Entity (A));
4716 else
4717 Kill_All_Checks;
4718 end if;
4719 end if;
4721 if A_Typ = Any_Type then
4722 Set_Etype (N, Any_Type);
4723 return;
4724 end if;
4726 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4728 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4730 -- Apply predicate tests except in certain special cases. Note
4731 -- that it might be more consistent to apply these only when
4732 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4733 -- for the outbound predicate tests ??? In any case indicate
4734 -- the function being called, for better warnings if the call
4735 -- leads to an infinite recursion.
4737 if Predicate_Tests_On_Arguments (Nam) then
4738 Apply_Predicate_Check (A, F_Typ, Fun => Nam);
4739 end if;
4741 -- Apply required constraint checks
4743 if Is_Scalar_Type (A_Typ) then
4744 Apply_Scalar_Range_Check (A, F_Typ);
4746 elsif Is_Array_Type (A_Typ) then
4747 Apply_Length_Check (A, F_Typ);
4749 elsif Is_Record_Type (F_Typ)
4750 and then Has_Discriminants (F_Typ)
4751 and then Is_Constrained (F_Typ)
4752 and then (not Is_Derived_Type (F_Typ)
4753 or else Comes_From_Source (Nam))
4754 then
4755 Apply_Discriminant_Check (A, F_Typ);
4757 -- For view conversions of a discriminated object, apply
4758 -- check to object itself, the conversion alreay has the
4759 -- proper type.
4761 if Nkind (A) = N_Type_Conversion
4762 and then Is_Constrained (Etype (Expression (A)))
4763 then
4764 Apply_Discriminant_Check (Expression (A), F_Typ);
4765 end if;
4767 elsif Is_Access_Type (F_Typ)
4768 and then Is_Array_Type (Designated_Type (F_Typ))
4769 and then Is_Constrained (Designated_Type (F_Typ))
4770 then
4771 Apply_Length_Check (A, F_Typ);
4773 elsif Is_Access_Type (F_Typ)
4774 and then Has_Discriminants (Designated_Type (F_Typ))
4775 and then Is_Constrained (Designated_Type (F_Typ))
4776 then
4777 Apply_Discriminant_Check (A, F_Typ);
4779 else
4780 Apply_Range_Check (A, F_Typ);
4781 end if;
4783 -- Ada 2005 (AI-231): Note that the controlling parameter case
4784 -- already existed in Ada 95, which is partially checked
4785 -- elsewhere (see Checks), and we don't want the warning
4786 -- message to differ.
4788 if Is_Access_Type (F_Typ)
4789 and then Can_Never_Be_Null (F_Typ)
4790 and then Known_Null (A)
4791 then
4792 if Is_Controlling_Formal (F) then
4793 Apply_Compile_Time_Constraint_Error
4794 (N => A,
4795 Msg => "null value not allowed here??",
4796 Reason => CE_Access_Check_Failed);
4798 elsif Ada_Version >= Ada_2005 then
4799 Apply_Compile_Time_Constraint_Error
4800 (N => A,
4801 Msg => "(Ada 2005) NULL not allowed in "
4802 & "null-excluding formal??",
4803 Reason => CE_Null_Not_Allowed);
4804 end if;
4805 end if;
4806 end if;
4808 -- Checks for OUT parameters and IN OUT parameters
4810 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4812 -- If there is a type conversion, make sure the return value
4813 -- meets the constraints of the variable before the conversion.
4815 if Nkind (A) = N_Type_Conversion then
4816 if Is_Scalar_Type (A_Typ) then
4818 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4819 -- which would prevent the check from being generated.
4820 -- This is for Starlet only though, so long obsolete.
4822 if Mechanism (F) = By_Reference
4823 and then Ekind (Nam) = E_Procedure
4824 and then Is_Valued_Procedure (Nam)
4825 then
4826 null;
4827 else
4828 Apply_Scalar_Range_Check
4829 (Expression (A), Etype (Expression (A)), A_Typ);
4830 end if;
4832 -- In addition the return value must meet the constraints
4833 -- of the object type (see the comment below).
4835 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4837 else
4838 Apply_Range_Check
4839 (Expression (A), Etype (Expression (A)), A_Typ);
4840 end if;
4842 -- If no conversion, apply scalar range checks and length check
4843 -- based on the subtype of the actual (NOT that of the formal).
4844 -- This indicates that the check takes place on return from the
4845 -- call. During expansion the required constraint checks are
4846 -- inserted. In GNATprove mode, in the absence of expansion,
4847 -- the flag indicates that the returned value is valid.
4849 else
4850 if Is_Scalar_Type (F_Typ) then
4851 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4853 elsif Is_Array_Type (F_Typ)
4854 and then Ekind (F) = E_Out_Parameter
4855 then
4856 Apply_Length_Check (A, F_Typ);
4858 else
4859 Apply_Range_Check (A, A_Typ, F_Typ);
4860 end if;
4861 end if;
4863 -- Note: we do not apply the predicate checks for the case of
4864 -- OUT and IN OUT parameters. They are instead applied in the
4865 -- Expand_Actuals routine in Exp_Ch6.
4866 end if;
4868 -- If the formal is of an unconstrained array subtype with fixed
4869 -- lower bound, then sliding to that bound may be needed.
4871 if Is_Fixed_Lower_Bound_Array_Subtype (F_Typ) then
4872 Expand_Sliding_Conversion (A, F_Typ);
4873 end if;
4875 -- An actual associated with an access parameter is implicitly
4876 -- converted to the anonymous access type of the formal and must
4877 -- satisfy the legality checks for access conversions.
4879 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4880 if not Valid_Conversion (A, F_Typ, A) then
4881 Error_Msg_N
4882 ("invalid implicit conversion for access parameter", A);
4883 end if;
4885 -- If the actual is an access selected component of a variable,
4886 -- the call may modify its designated object. It is reasonable
4887 -- to treat this as a potential modification of the enclosing
4888 -- record, to prevent spurious warnings that it should be
4889 -- declared as a constant, because intuitively programmers
4890 -- regard the designated subcomponent as part of the record.
4892 if Nkind (A) = N_Selected_Component
4893 and then Is_Entity_Name (Prefix (A))
4894 and then not Is_Constant_Object (Entity (Prefix (A)))
4895 then
4896 Note_Possible_Modification (A, Sure => False);
4897 end if;
4898 end if;
4900 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
4902 if (Is_By_Reference_Type (F_Typ) or else Is_Aliased (F))
4903 and then Comes_From_Source (N)
4904 then
4905 if Is_Atomic_Object (A)
4906 and then not Is_Atomic (F_Typ)
4907 then
4908 Error_Msg_NE
4909 ("cannot pass atomic object to nonatomic formal&",
4910 A, F);
4911 Error_Msg_N
4912 ("\which is passed by reference (RM C.6(12))", A);
4914 elsif Is_Volatile_Object_Ref (A)
4915 and then not Is_Volatile (F_Typ)
4916 then
4917 Error_Msg_NE
4918 ("cannot pass volatile object to nonvolatile formal&",
4919 A, F);
4920 Error_Msg_N
4921 ("\which is passed by reference (RM C.6(12))", A);
4923 elsif Is_Volatile_Full_Access_Object_Ref (A)
4924 and then not Is_Volatile_Full_Access (F_Typ)
4925 then
4926 Error_Msg_NE
4927 ("cannot pass full access object to nonfull access "
4928 & "formal&", A, F);
4929 Error_Msg_N
4930 ("\which is passed by reference (RM C.6(12))", A);
4931 end if;
4933 -- Check for nonatomic subcomponent of a full access object
4934 -- in Ada 2022 (RM C.6 (12)).
4936 if Ada_Version >= Ada_2022
4937 and then Is_Subcomponent_Of_Full_Access_Object (A)
4938 and then not Is_Atomic_Object (A)
4939 then
4940 Error_Msg_N
4941 ("cannot pass nonatomic subcomponent of full access "
4942 & "object", A);
4943 Error_Msg_NE
4944 ("\to formal & which is passed by reference (RM C.6(12))",
4945 A, F);
4946 end if;
4947 end if;
4949 -- Check that subprograms don't have improper controlling
4950 -- arguments (RM 3.9.2 (9)).
4952 -- A primitive operation may have an access parameter of an
4953 -- incomplete tagged type, but a dispatching call is illegal
4954 -- if the type is still incomplete.
4956 if Is_Controlling_Formal (F) then
4957 Set_Is_Controlling_Actual (A);
4959 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4960 declare
4961 Desig : constant Entity_Id := Designated_Type (F_Typ);
4962 begin
4963 if Ekind (Desig) = E_Incomplete_Type
4964 and then No (Full_View (Desig))
4965 and then No (Non_Limited_View (Desig))
4966 then
4967 Error_Msg_NE
4968 ("premature use of incomplete type& "
4969 & "in dispatching call", A, Desig);
4970 end if;
4971 end;
4972 end if;
4974 elsif Nkind (A) = N_Explicit_Dereference then
4975 Validate_Remote_Access_To_Class_Wide_Type (A);
4976 end if;
4978 -- Apply legality rule 3.9.2 (9/1)
4980 -- Skip this check on helpers and indirect-call wrappers built to
4981 -- support class-wide preconditions.
4983 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4984 and then not Is_Class_Wide_Type (F_Typ)
4985 and then not Is_Controlling_Formal (F)
4986 and then not In_Instance
4987 and then (not Is_Subprogram (Nam)
4988 or else No (Class_Preconditions_Subprogram (Nam)))
4989 then
4990 Error_Msg_N ("class-wide argument not allowed here!", A);
4992 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4993 Error_Msg_Node_2 := F_Typ;
4994 Error_Msg_NE
4995 ("& is not a dispatching operation of &!", A, Nam);
4996 end if;
4998 -- Apply the checks described in 3.10.2(27): if the context is a
4999 -- specific access-to-object, the actual cannot be class-wide.
5000 -- Use base type to exclude access_to_subprogram cases.
5002 elsif Is_Access_Type (A_Typ)
5003 and then Is_Access_Type (F_Typ)
5004 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
5005 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
5006 or else (Nkind (A) = N_Attribute_Reference
5007 and then
5008 Is_Class_Wide_Type (Etype (Prefix (A)))))
5009 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
5010 and then not Is_Controlling_Formal (F)
5012 -- Disable these checks for call to imported C++ subprograms
5014 and then not
5015 (Is_Entity_Name (Name (N))
5016 and then Is_Imported (Entity (Name (N)))
5017 and then Convention (Entity (Name (N))) = Convention_CPP)
5018 then
5019 Error_Msg_N
5020 ("access to class-wide argument not allowed here!", A);
5022 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5023 Error_Msg_Node_2 := Designated_Type (F_Typ);
5024 Error_Msg_NE
5025 ("& is not a dispatching operation of &!", A, Nam);
5026 end if;
5027 end if;
5029 Check_Aliased_Parameter;
5031 Eval_Actual (A);
5033 -- If it is a named association, treat the selector_name as a
5034 -- proper identifier, and mark the corresponding entity.
5036 if Nkind (Parent (A)) = N_Parameter_Association
5038 -- Ignore reference in SPARK mode, as it refers to an entity not
5039 -- in scope at the point of reference, so the reference should
5040 -- be ignored for computing effects of subprograms.
5042 and then not GNATprove_Mode
5043 then
5044 -- If subprogram is overridden, use name of formal that
5045 -- is being called.
5047 if Present (Real_Subp) then
5048 Set_Entity (Selector_Name (Parent (A)), Real_F);
5049 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
5051 else
5052 Set_Entity (Selector_Name (Parent (A)), F);
5053 Generate_Reference (F, Selector_Name (Parent (A)));
5054 Set_Etype (Selector_Name (Parent (A)), F_Typ);
5055 Generate_Reference (F_Typ, N, ' ');
5056 end if;
5057 end if;
5059 Prev := A;
5061 if Ekind (F) /= E_Out_Parameter then
5062 Check_Unset_Reference (A);
5063 end if;
5065 -- A formal parameter of a specific tagged type whose related
5066 -- subprogram is subject to pragma Extensions_Visible with value
5067 -- "False" cannot act as an actual in a subprogram with value
5068 -- "True" (SPARK RM 6.1.7(3)).
5070 -- No check needed for helpers and indirect-call wrappers built to
5071 -- support class-wide preconditions.
5073 if Is_EVF_Expression (A)
5074 and then Extensions_Visible_Status (Nam) =
5075 Extensions_Visible_True
5076 and then not
5077 (Is_Subprogram (Current_Scope)
5078 and then
5079 Present (Class_Preconditions_Subprogram (Current_Scope)))
5080 then
5081 Error_Msg_N
5082 ("formal parameter cannot act as actual parameter when "
5083 & "Extensions_Visible is False", A);
5084 Error_Msg_NE
5085 ("\subprogram & has Extensions_Visible True", A, Nam);
5086 end if;
5088 -- The actual parameter of a Ghost subprogram whose formal is of
5089 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5091 if Comes_From_Source (Nam)
5092 and then Is_Ghost_Entity (Nam)
5093 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5094 and then Is_Entity_Name (A)
5095 and then Present (Entity (A))
5096 and then not Is_Ghost_Entity (Entity (A))
5097 then
5098 Error_Msg_NE
5099 ("non-ghost variable & cannot appear as actual in call to "
5100 & "ghost procedure", A, Entity (A));
5102 if Ekind (F) = E_In_Out_Parameter then
5103 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5104 else
5105 Error_Msg_N ("\corresponding formal has mode OUT", A);
5106 end if;
5107 end if;
5109 -- (AI12-0397): The target of a subprogram call that occurs within
5110 -- the expression of an Default_Initial_Condition aspect and has
5111 -- an actual that is the current instance of the type must be
5112 -- either a primitive of the type or a class-wide subprogram,
5113 -- because the type of the current instance in such an aspect is
5114 -- considered to be a notional formal derived type whose only
5115 -- operations correspond to the primitives of the enclosing type.
5116 -- Nonprimitives can be called, but the current instance must be
5117 -- converted rather than passed directly. Note that a current
5118 -- instance of a type with DIC will occur as a reference to an
5119 -- in-mode formal of an enclosing DIC procedure or partial DIC
5120 -- procedure. (It seems that this check should perhaps also apply
5121 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5122 -- aspects???)
5124 if Nkind (A) = N_Identifier
5125 and then Ekind (Entity (A)) = E_In_Parameter
5127 and then Is_Subprogram (Scope (Entity (A)))
5128 and then Is_DIC_Procedure (Scope (Entity (A)))
5130 -- We check Comes_From_Source to exclude inherited primitives
5131 -- from being flagged, because such subprograms turn out to not
5132 -- always have the Is_Primitive flag set. ???
5134 and then Comes_From_Source (Nam)
5136 and then not Is_Primitive (Nam)
5137 and then not Is_Class_Wide_Type (F_Typ)
5138 then
5139 Error_Msg_NE
5140 ("call to nonprimitive & with current instance not allowed " &
5141 "for aspect", A, Nam);
5142 end if;
5144 Next_Actual (A);
5146 -- Case where actual is not present
5148 else
5149 Insert_Default;
5150 end if;
5152 Next_Formal (F);
5154 if Present (Real_Subp) then
5155 Next_Formal (Real_F);
5156 end if;
5157 end loop;
5158 end Resolve_Actuals;
5160 -----------------------
5161 -- Resolve_Allocator --
5162 -----------------------
5164 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5165 Desig_T : constant Entity_Id := Designated_Type (Typ);
5166 E : constant Node_Id := Expression (N);
5167 Subtyp : Entity_Id;
5168 Discrim : Entity_Id;
5169 Constr : Node_Id;
5170 Aggr : Node_Id;
5171 Assoc : Node_Id := Empty;
5172 Disc_Exp : Node_Id;
5174 procedure Check_Allocator_Discrim_Accessibility
5175 (Disc_Exp : Node_Id;
5176 Alloc_Typ : Entity_Id);
5177 -- Check that accessibility level associated with an access discriminant
5178 -- initialized in an allocator by the expression Disc_Exp is not deeper
5179 -- than the level of the allocator type Alloc_Typ. An error message is
5180 -- issued if this condition is violated. Specialized checks are done for
5181 -- the cases of a constraint expression which is an access attribute or
5182 -- an access discriminant.
5184 procedure Check_Allocator_Discrim_Accessibility_Exprs
5185 (Curr_Exp : Node_Id;
5186 Alloc_Typ : Entity_Id);
5187 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5188 -- across all expressions within a given conditional expression.
5190 function In_Dispatching_Context return Boolean;
5191 -- If the allocator is an actual in a call, it is allowed to be class-
5192 -- wide when the context is not because it is a controlling actual.
5194 -------------------------------------------
5195 -- Check_Allocator_Discrim_Accessibility --
5196 -------------------------------------------
5198 procedure Check_Allocator_Discrim_Accessibility
5199 (Disc_Exp : Node_Id;
5200 Alloc_Typ : Entity_Id)
5202 begin
5203 if Type_Access_Level (Etype (Disc_Exp)) >
5204 Deepest_Type_Access_Level (Alloc_Typ)
5205 then
5206 Error_Msg_N
5207 ("operand type has deeper level than allocator type", Disc_Exp);
5209 -- When the expression is an Access attribute the level of the prefix
5210 -- object must not be deeper than that of the allocator's type.
5212 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5213 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5214 Attribute_Access
5215 and then Static_Accessibility_Level
5216 (Disc_Exp, Zero_On_Dynamic_Level)
5217 > Deepest_Type_Access_Level (Alloc_Typ)
5218 then
5219 Error_Msg_N
5220 ("prefix of attribute has deeper level than allocator type",
5221 Disc_Exp);
5223 -- When the expression is an access discriminant the check is against
5224 -- the level of the prefix object.
5226 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5227 and then Nkind (Disc_Exp) = N_Selected_Component
5228 and then Static_Accessibility_Level
5229 (Disc_Exp, Zero_On_Dynamic_Level)
5230 > Deepest_Type_Access_Level (Alloc_Typ)
5231 then
5232 Error_Msg_N
5233 ("access discriminant has deeper level than allocator type",
5234 Disc_Exp);
5236 -- All other cases are legal
5238 else
5239 null;
5240 end if;
5241 end Check_Allocator_Discrim_Accessibility;
5243 -------------------------------------------------
5244 -- Check_Allocator_Discrim_Accessibility_Exprs --
5245 -------------------------------------------------
5247 procedure Check_Allocator_Discrim_Accessibility_Exprs
5248 (Curr_Exp : Node_Id;
5249 Alloc_Typ : Entity_Id)
5251 Alt : Node_Id;
5252 Expr : Node_Id;
5253 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5254 begin
5255 -- When conditional expressions are constant folded we know at
5256 -- compile time which expression to check - so don't bother with
5257 -- the rest of the cases.
5259 if Nkind (Curr_Exp) = N_Attribute_Reference then
5260 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5262 -- Non-constant-folded if expressions
5264 elsif Nkind (Disc_Exp) = N_If_Expression then
5265 -- Check both expressions if they are still present in the face
5266 -- of expansion.
5268 Expr := Next (First (Expressions (Disc_Exp)));
5269 if Present (Expr) then
5270 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5271 Next (Expr);
5272 if Present (Expr) then
5273 Check_Allocator_Discrim_Accessibility_Exprs
5274 (Expr, Alloc_Typ);
5275 end if;
5276 end if;
5278 -- Non-constant-folded case expressions
5280 elsif Nkind (Disc_Exp) = N_Case_Expression then
5281 -- Check all alternatives
5283 Alt := First (Alternatives (Disc_Exp));
5284 while Present (Alt) loop
5285 Check_Allocator_Discrim_Accessibility_Exprs
5286 (Expression (Alt), Alloc_Typ);
5288 Next (Alt);
5289 end loop;
5291 -- Base case, check the accessibility of the original node of the
5292 -- expression.
5294 else
5295 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5296 end if;
5297 end Check_Allocator_Discrim_Accessibility_Exprs;
5299 ----------------------------
5300 -- In_Dispatching_Context --
5301 ----------------------------
5303 function In_Dispatching_Context return Boolean is
5304 Par : constant Node_Id := Parent (N);
5306 begin
5307 return Nkind (Par) in N_Subprogram_Call
5308 and then Is_Entity_Name (Name (Par))
5309 and then Is_Dispatching_Operation (Entity (Name (Par)));
5310 end In_Dispatching_Context;
5312 -- Start of processing for Resolve_Allocator
5314 begin
5315 -- Replace general access with specific type
5317 if Ekind (Etype (N)) = E_Allocator_Type then
5318 Set_Etype (N, Base_Type (Typ));
5319 end if;
5321 if Is_Abstract_Type (Typ) then
5322 Error_Msg_N ("type of allocator cannot be abstract", N);
5323 end if;
5325 -- For qualified expression, resolve the expression using the given
5326 -- subtype (nothing to do for type mark, subtype indication)
5328 if Nkind (E) = N_Qualified_Expression then
5329 if Is_Class_Wide_Type (Etype (E))
5330 and then not Is_Class_Wide_Type (Desig_T)
5331 and then not In_Dispatching_Context
5332 then
5333 Error_Msg_N
5334 ("class-wide allocator not allowed for this access type", N);
5335 end if;
5337 -- Do a full resolution to apply constraint and predicate checks
5339 Resolve_Qualified_Expression (E, Etype (E));
5340 Check_Unset_Reference (Expression (E));
5342 -- Allocators generated by the build-in-place expansion mechanism
5343 -- are explicitly marked as coming from source but do not need to be
5344 -- checked for limited initialization. To exclude this case, ensure
5345 -- that the parent of the allocator is a source node.
5346 -- The return statement constructed for an Expression_Function does
5347 -- not come from source but requires a limited check.
5349 if Is_Limited_Type (Etype (E))
5350 and then Comes_From_Source (N)
5351 and then
5352 (Comes_From_Source (Parent (N))
5353 or else
5354 (Ekind (Current_Scope) = E_Function
5355 and then Nkind (Original_Node (Unit_Declaration_Node
5356 (Current_Scope))) = N_Expression_Function))
5357 and then not In_Instance_Body
5358 then
5359 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5360 if Nkind (Parent (N)) = N_Assignment_Statement then
5361 Error_Msg_N
5362 ("illegal expression for initialized allocator of a "
5363 & "limited type (RM 7.5 (2.7/2))", N);
5364 else
5365 Error_Msg_N
5366 ("initialization not allowed for limited types", N);
5367 end if;
5369 Explain_Limited_Type (Etype (E), N);
5370 end if;
5371 end if;
5373 -- Calls to build-in-place functions are not currently supported in
5374 -- allocators for access types associated with a simple storage pool.
5375 -- Supporting such allocators may require passing additional implicit
5376 -- parameters to build-in-place functions (or a significant revision
5377 -- of the current b-i-p implementation to unify the handling for
5378 -- multiple kinds of storage pools). ???
5380 if Is_Inherently_Limited_Type (Desig_T)
5381 and then Nkind (Expression (E)) = N_Function_Call
5382 then
5383 declare
5384 Pool : constant Entity_Id :=
5385 Associated_Storage_Pool (Root_Type (Typ));
5386 begin
5387 if Present (Pool)
5388 and then
5389 Present (Get_Rep_Pragma
5390 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5391 then
5392 Error_Msg_N
5393 ("limited function calls not yet supported in simple "
5394 & "storage pool allocators", Expression (E));
5395 end if;
5396 end;
5397 end if;
5399 -- A special accessibility check is needed for allocators that
5400 -- constrain access discriminants. The level of the type of the
5401 -- expression used to constrain an access discriminant cannot be
5402 -- deeper than the type of the allocator (in contrast to access
5403 -- parameters, where the level of the actual can be arbitrary).
5405 -- We can't use Valid_Conversion to perform this check because in
5406 -- general the type of the allocator is unrelated to the type of
5407 -- the access discriminant.
5409 if Ekind (Typ) /= E_Anonymous_Access_Type
5410 or else Is_Local_Anonymous_Access (Typ)
5411 then
5412 Subtyp := Entity (Subtype_Mark (E));
5414 Aggr := Original_Node (Expression (E));
5416 if Has_Discriminants (Subtyp)
5417 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5418 then
5419 Discrim := First_Discriminant (Base_Type (Subtyp));
5421 -- Get the first component expression of the aggregate
5423 if Present (Expressions (Aggr)) then
5424 Disc_Exp := First (Expressions (Aggr));
5426 elsif Present (Component_Associations (Aggr)) then
5427 Assoc := First (Component_Associations (Aggr));
5429 if Present (Assoc) then
5430 Disc_Exp := Expression (Assoc);
5431 else
5432 Disc_Exp := Empty;
5433 end if;
5435 else
5436 Disc_Exp := Empty;
5437 end if;
5439 while Present (Discrim) and then Present (Disc_Exp) loop
5440 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5441 Check_Allocator_Discrim_Accessibility_Exprs
5442 (Disc_Exp, Typ);
5443 end if;
5445 Next_Discriminant (Discrim);
5447 if Present (Discrim) then
5448 if Present (Assoc) then
5449 Next (Assoc);
5450 Disc_Exp := Expression (Assoc);
5452 elsif Present (Next (Disc_Exp)) then
5453 Next (Disc_Exp);
5455 else
5456 Assoc := First (Component_Associations (Aggr));
5458 if Present (Assoc) then
5459 Disc_Exp := Expression (Assoc);
5460 else
5461 Disc_Exp := Empty;
5462 end if;
5463 end if;
5464 end if;
5465 end loop;
5466 end if;
5467 end if;
5469 -- For a subtype mark or subtype indication, freeze the subtype
5471 else
5472 Freeze_Expression (E);
5474 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5475 Error_Msg_N
5476 ("initialization required for access-to-constant allocator", N);
5477 end if;
5479 -- A special accessibility check is needed for allocators that
5480 -- constrain access discriminants. The level of the type of the
5481 -- expression used to constrain an access discriminant cannot be
5482 -- deeper than the type of the allocator (in contrast to access
5483 -- parameters, where the level of the actual can be arbitrary).
5484 -- We can't use Valid_Conversion to perform this check because
5485 -- in general the type of the allocator is unrelated to the type
5486 -- of the access discriminant.
5488 if Nkind (Original_Node (E)) = N_Subtype_Indication
5489 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5490 or else Is_Local_Anonymous_Access (Typ))
5491 then
5492 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5494 if Has_Discriminants (Subtyp) then
5495 Discrim := First_Discriminant (Base_Type (Subtyp));
5496 Constr := First (Constraints (Constraint (Original_Node (E))));
5497 while Present (Discrim) and then Present (Constr) loop
5498 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5499 if Nkind (Constr) = N_Discriminant_Association then
5500 Disc_Exp := Expression (Constr);
5501 else
5502 Disc_Exp := Constr;
5503 end if;
5505 Check_Allocator_Discrim_Accessibility_Exprs
5506 (Disc_Exp, Typ);
5507 end if;
5509 Next_Discriminant (Discrim);
5510 Next (Constr);
5511 end loop;
5512 end if;
5513 end if;
5514 end if;
5516 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5517 -- check that the level of the type of the created object is not deeper
5518 -- than the level of the allocator's access type, since extensions can
5519 -- now occur at deeper levels than their ancestor types. This is a
5520 -- static accessibility level check; a run-time check is also needed in
5521 -- the case of an initialized allocator with a class-wide argument (see
5522 -- Expand_Allocator_Expression).
5524 if Ada_Version >= Ada_2005
5525 and then Is_Class_Wide_Type (Desig_T)
5526 then
5527 declare
5528 Exp_Typ : Entity_Id;
5530 begin
5531 if Nkind (E) = N_Qualified_Expression then
5532 Exp_Typ := Etype (E);
5533 elsif Nkind (E) = N_Subtype_Indication then
5534 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5535 else
5536 Exp_Typ := Entity (E);
5537 end if;
5539 if Type_Access_Level (Exp_Typ) >
5540 Deepest_Type_Access_Level (Typ)
5541 then
5542 if In_Instance_Body then
5543 Error_Msg_Warn := SPARK_Mode /= On;
5544 Error_Msg_N
5545 ("type in allocator has deeper level than designated "
5546 & "class-wide type<<", E);
5547 Error_Msg_N ("\Program_Error [<<", E);
5549 Rewrite (N,
5550 Make_Raise_Program_Error (Sloc (N),
5551 Reason => PE_Accessibility_Check_Failed));
5552 Set_Etype (N, Typ);
5554 -- Do not apply Ada 2005 accessibility checks on a class-wide
5555 -- allocator if the type given in the allocator is a formal
5556 -- type or within a formal package. A run-time check will be
5557 -- performed in the instance.
5559 elsif not Is_Generic_Type (Exp_Typ)
5560 and then not In_Generic_Formal_Package (Exp_Typ)
5561 then
5562 Error_Msg_N
5563 ("type in allocator has deeper level than designated "
5564 & "class-wide type", E);
5565 end if;
5566 end if;
5567 end;
5568 end if;
5570 -- Check for allocation from an empty storage pool. But do not complain
5571 -- if it's a return statement for a build-in-place function, because the
5572 -- allocator is there just in case the caller uses an allocator. If the
5573 -- caller does use an allocator, it will be caught at the call site.
5575 if No_Pool_Assigned (Typ)
5576 and then not For_Special_Return_Object (N)
5577 then
5578 Error_Msg_N ("allocation from empty storage pool!", N);
5580 -- If the context is an unchecked conversion, as may happen within an
5581 -- inlined subprogram, the allocator is being resolved with its own
5582 -- anonymous type. In that case, if the target type has a specific
5583 -- storage pool, it must be inherited explicitly by the allocator type.
5585 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5586 and then No (Associated_Storage_Pool (Typ))
5587 then
5588 Set_Associated_Storage_Pool
5589 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5590 end if;
5592 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5593 Check_Restriction (No_Anonymous_Allocators, N);
5594 end if;
5596 -- Check that an allocator with task parts isn't for a nested access
5597 -- type when restriction No_Task_Hierarchy applies.
5599 if not Is_Library_Level_Entity (Base_Type (Typ))
5600 and then Has_Task (Base_Type (Desig_T))
5601 then
5602 Check_Restriction (No_Task_Hierarchy, N);
5603 end if;
5605 -- An illegal allocator may be rewritten as a raise Program_Error
5606 -- statement.
5608 if Nkind (N) = N_Allocator then
5610 -- Avoid coextension processing for an allocator that is the
5611 -- expansion of a build-in-place function call.
5613 if Nkind (Original_Node (N)) = N_Allocator
5614 and then Nkind (Expression (Original_Node (N))) =
5615 N_Qualified_Expression
5616 and then Nkind (Expression (Expression (Original_Node (N)))) =
5617 N_Function_Call
5618 and then Is_Expanded_Build_In_Place_Call
5619 (Expression (Expression (Original_Node (N))))
5620 then
5621 null; -- b-i-p function call case
5623 else
5624 -- An anonymous access discriminant is the definition of a
5625 -- coextension.
5627 if Ekind (Typ) = E_Anonymous_Access_Type
5628 and then Nkind (Associated_Node_For_Itype (Typ)) =
5629 N_Discriminant_Specification
5630 then
5631 declare
5632 Discr : constant Entity_Id :=
5633 Defining_Identifier (Associated_Node_For_Itype (Typ));
5635 begin
5636 Check_Restriction (No_Coextensions, N);
5638 -- Ada 2012 AI05-0052: If the designated type of the
5639 -- allocator is limited, then the allocator shall not
5640 -- be used to define the value of an access discriminant
5641 -- unless the discriminated type is immutably limited.
5643 if Ada_Version >= Ada_2012
5644 and then Is_Limited_Type (Desig_T)
5645 and then not Is_Inherently_Limited_Type (Scope (Discr))
5646 then
5647 Error_Msg_N
5648 ("only immutably limited types can have anonymous "
5649 & "access discriminants designating a limited type",
5651 end if;
5652 end;
5654 -- Avoid marking an allocator as a dynamic coextension if it is
5655 -- within a static construct.
5657 if not Is_Static_Coextension (N) then
5658 Set_Is_Dynamic_Coextension (N);
5660 -- Finalization and deallocation of coextensions utilizes an
5661 -- approximate implementation which does not directly adhere
5662 -- to the semantic rules. Warn on potential issues involving
5663 -- coextensions.
5665 if Is_Controlled (Desig_T) then
5666 Error_Msg_N
5667 ("??coextension will not be finalized when its "
5668 & "associated owner is deallocated or finalized", N);
5669 else
5670 Error_Msg_N
5671 ("??coextension will not be deallocated when its "
5672 & "associated owner is deallocated", N);
5673 end if;
5674 end if;
5676 -- Cleanup for potential static coextensions
5678 else
5679 Set_Is_Dynamic_Coextension (N, False);
5680 Set_Is_Static_Coextension (N, False);
5682 -- Objects allocated through anonymous access types are not
5683 -- finalized on time because this involves run-time ownership
5684 -- and currently this property is not available. In rare cases
5685 -- the object might not be finalized at all. Warn on potential
5686 -- issues involving anonymous access-to-controlled types.
5688 if Ekind (Typ) = E_Anonymous_Access_Type
5689 and then Is_Controlled_Active (Desig_T)
5690 then
5691 Error_Msg_N
5692 ("??object designated by anonymous access value might "
5693 & "not be finalized until its enclosing library unit "
5694 & "goes out of scope, or not be finalized at all", N);
5695 Error_Msg_N ("\use named access type instead", N);
5696 end if;
5697 end if;
5698 end if;
5699 end if;
5701 -- Report a simple error: if the designated object is a local task,
5702 -- its body has not been seen yet, and its activation will fail an
5703 -- elaboration check.
5705 if Is_Task_Type (Desig_T)
5706 and then Scope (Base_Type (Desig_T)) = Current_Scope
5707 and then Is_Compilation_Unit (Current_Scope)
5708 and then Ekind (Current_Scope) = E_Package
5709 and then not In_Package_Body (Current_Scope)
5710 then
5711 Error_Msg_Warn := SPARK_Mode /= On;
5712 Error_Msg_N ("cannot activate task before body seen<<", N);
5713 Error_Msg_N ("\Program_Error [<<", N);
5714 end if;
5716 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5717 -- type with a task component on a subpool. This action must raise
5718 -- Program_Error at runtime.
5720 if Ada_Version >= Ada_2012
5721 and then Nkind (N) = N_Allocator
5722 and then Present (Subpool_Handle_Name (N))
5723 and then Has_Task (Desig_T)
5724 then
5725 Error_Msg_Warn := SPARK_Mode /= On;
5726 Error_Msg_N ("cannot allocate task on subpool<<", N);
5727 Error_Msg_N ("\Program_Error [<<", N);
5729 Rewrite (N,
5730 Make_Raise_Program_Error (Sloc (N),
5731 Reason => PE_Explicit_Raise));
5732 Set_Etype (N, Typ);
5733 end if;
5734 end Resolve_Allocator;
5736 ---------------------------
5737 -- Resolve_Arithmetic_Op --
5738 ---------------------------
5740 -- Used for resolving all arithmetic operators except exponentiation
5742 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5743 L : constant Node_Id := Left_Opnd (N);
5744 R : constant Node_Id := Right_Opnd (N);
5745 TL : constant Entity_Id := Base_Type (Etype (L));
5746 TR : constant Entity_Id := Base_Type (Etype (R));
5747 T : Entity_Id;
5748 Rop : Node_Id;
5750 B_Typ : constant Entity_Id := Base_Type (Typ);
5751 -- We do the resolution using the base type, because intermediate values
5752 -- in expressions always are of the base type, not a subtype of it.
5754 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5755 -- Returns True if N is in a context that expects "any real type"
5757 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5758 -- Return True iff given type is Integer or universal real/integer
5760 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5761 -- Choose type of integer literal in fixed-point operation to conform
5762 -- to available fixed-point type. T is the type of the other operand,
5763 -- which is needed to determine the expected type of N.
5765 procedure Set_Operand_Type (N : Node_Id);
5766 -- Set operand type to T if universal
5768 -------------------------------
5769 -- Expected_Type_Is_Any_Real --
5770 -------------------------------
5772 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5773 begin
5774 -- N is the expression after "delta" in a fixed_point_definition;
5775 -- see RM-3.5.9(6):
5777 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5778 | N_Decimal_Fixed_Point_Definition
5780 -- N is one of the bounds in a real_range_specification;
5781 -- see RM-3.5.7(5):
5783 | N_Real_Range_Specification
5785 -- N is the expression of a delta_constraint;
5786 -- see RM-J.3(3):
5788 | N_Delta_Constraint;
5789 end Expected_Type_Is_Any_Real;
5791 -----------------------------
5792 -- Is_Integer_Or_Universal --
5793 -----------------------------
5795 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5796 T : Entity_Id;
5797 Index : Interp_Index;
5798 It : Interp;
5800 begin
5801 if not Is_Overloaded (N) then
5802 T := Etype (N);
5803 return Base_Type (T) = Base_Type (Standard_Integer)
5804 or else Is_Universal_Numeric_Type (T);
5805 else
5806 Get_First_Interp (N, Index, It);
5807 while Present (It.Typ) loop
5808 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5809 or else Is_Universal_Numeric_Type (It.Typ)
5810 then
5811 return True;
5812 end if;
5814 Get_Next_Interp (Index, It);
5815 end loop;
5816 end if;
5818 return False;
5819 end Is_Integer_Or_Universal;
5821 ----------------------------
5822 -- Set_Mixed_Mode_Operand --
5823 ----------------------------
5825 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5826 Index : Interp_Index;
5827 It : Interp;
5829 begin
5830 if Universal_Interpretation (N) = Universal_Integer then
5832 -- A universal integer literal is resolved as standard integer
5833 -- except in the case of a fixed-point result, where we leave it
5834 -- as universal (to be handled by Exp_Fixd later on)
5836 if Is_Fixed_Point_Type (T) then
5837 Resolve (N, Universal_Integer);
5838 else
5839 Resolve (N, Standard_Integer);
5840 end if;
5842 elsif Universal_Interpretation (N) = Universal_Real
5843 and then (T = Base_Type (Standard_Integer)
5844 or else Is_Universal_Numeric_Type (T))
5845 then
5846 -- A universal real can appear in a fixed-type context. We resolve
5847 -- the literal with that context, even though this might raise an
5848 -- exception prematurely (the other operand may be zero).
5850 Resolve (N, B_Typ);
5852 elsif Etype (N) = Base_Type (Standard_Integer)
5853 and then T = Universal_Real
5854 and then Is_Overloaded (N)
5855 then
5856 -- Integer arg in mixed-mode operation. Resolve with universal
5857 -- type, in case preference rule must be applied.
5859 Resolve (N, Universal_Integer);
5861 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5863 -- If the operand is part of a fixed multiplication operation,
5864 -- a conversion will be applied to each operand, so resolve it
5865 -- with its own type.
5867 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
5868 Resolve (N);
5870 else
5871 -- Not a mixed-mode operation, resolve with context
5873 Resolve (N, B_Typ);
5874 end if;
5876 elsif Etype (N) = Any_Fixed then
5878 -- N may itself be a mixed-mode operation, so use context type
5880 Resolve (N, B_Typ);
5882 elsif Is_Fixed_Point_Type (T)
5883 and then B_Typ = Universal_Fixed
5884 and then Is_Overloaded (N)
5885 then
5886 -- Must be (fixed * fixed) operation, operand must have one
5887 -- compatible interpretation.
5889 Resolve (N, Any_Fixed);
5891 elsif Is_Fixed_Point_Type (B_Typ)
5892 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5893 and then Is_Overloaded (N)
5894 then
5895 -- C * F(X) in a fixed context, where C is a real literal or a
5896 -- fixed-point expression. F must have either a fixed type
5897 -- interpretation or an integer interpretation, but not both.
5899 Get_First_Interp (N, Index, It);
5900 while Present (It.Typ) loop
5901 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5902 if Analyzed (N) then
5903 Error_Msg_N ("ambiguous operand in fixed operation", N);
5904 else
5905 Resolve (N, Standard_Integer);
5906 end if;
5908 elsif Is_Fixed_Point_Type (It.Typ) then
5909 if Analyzed (N) then
5910 Error_Msg_N ("ambiguous operand in fixed operation", N);
5911 else
5912 Resolve (N, It.Typ);
5913 end if;
5914 end if;
5916 Get_Next_Interp (Index, It);
5917 end loop;
5919 -- Reanalyze the literal with the fixed type of the context. If
5920 -- context is Universal_Fixed, we are within a conversion, leave
5921 -- the literal as a universal real because there is no usable
5922 -- fixed type, and the target of the conversion plays no role in
5923 -- the resolution.
5925 declare
5926 Op2 : Node_Id;
5927 T2 : Entity_Id;
5929 begin
5930 if N = L then
5931 Op2 := R;
5932 else
5933 Op2 := L;
5934 end if;
5936 if B_Typ = Universal_Fixed
5937 and then Nkind (Op2) = N_Real_Literal
5938 then
5939 T2 := Universal_Real;
5940 else
5941 T2 := B_Typ;
5942 end if;
5944 Set_Analyzed (Op2, False);
5945 Resolve (Op2, T2);
5946 end;
5948 -- A universal real conditional expression can appear in a fixed-type
5949 -- context and must be resolved with that context to facilitate the
5950 -- code generation in the back end. However, If the context is
5951 -- Universal_fixed (i.e. as an operand of a multiplication/division
5952 -- involving a fixed-point operand) the conditional expression must
5953 -- resolve to a unique visible fixed_point type, normally Duration.
5955 elsif Nkind (N) in N_Case_Expression | N_If_Expression
5956 and then Etype (N) = Universal_Real
5957 and then Is_Fixed_Point_Type (B_Typ)
5958 then
5959 if B_Typ = Universal_Fixed then
5960 Resolve (N, Unique_Fixed_Point_Type (N));
5962 else
5963 Resolve (N, B_Typ);
5964 end if;
5966 else
5967 Resolve (N);
5968 end if;
5969 end Set_Mixed_Mode_Operand;
5971 ----------------------
5972 -- Set_Operand_Type --
5973 ----------------------
5975 procedure Set_Operand_Type (N : Node_Id) is
5976 begin
5977 if Is_Universal_Numeric_Type (Etype (N)) then
5978 Set_Etype (N, T);
5979 end if;
5980 end Set_Operand_Type;
5982 -- Start of processing for Resolve_Arithmetic_Op
5984 begin
5985 if Ekind (Entity (N)) = E_Function
5986 and then Is_Imported (Entity (N))
5987 and then Is_Intrinsic_Subprogram (Entity (N))
5988 then
5989 Generate_Reference (Entity (N), N);
5990 Resolve_Intrinsic_Operator (N, Typ);
5991 return;
5993 -- Special-case for mixed-mode universal expressions or fixed point type
5994 -- operation: each argument is resolved separately. The same treatment
5995 -- is required if one of the operands of a fixed point operation is
5996 -- universal real, since in this case we don't do a conversion to a
5997 -- specific fixed-point type (instead the expander handles the case).
5999 -- Set the type of the node to its universal interpretation because
6000 -- legality checks on an exponentiation operand need the context.
6002 elsif Is_Universal_Numeric_Type (B_Typ)
6003 and then Present (Universal_Interpretation (L))
6004 and then Present (Universal_Interpretation (R))
6005 then
6006 Set_Etype (N, B_Typ);
6007 Resolve (L, Universal_Interpretation (L));
6008 Resolve (R, Universal_Interpretation (R));
6010 elsif (B_Typ = Universal_Real
6011 or else Etype (N) = Universal_Fixed
6012 or else (Etype (N) = Any_Fixed
6013 and then Is_Fixed_Point_Type (B_Typ))
6014 or else (Is_Fixed_Point_Type (B_Typ)
6015 and then (Is_Integer_Or_Universal (L)
6016 or else
6017 Is_Integer_Or_Universal (R))))
6018 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
6019 then
6020 if TL = Universal_Integer or else TR = Universal_Integer then
6021 Check_For_Visible_Operator (N, B_Typ);
6022 end if;
6024 -- If context is a fixed type and one operand is integer, the other
6025 -- is resolved with the type of the context.
6027 if Is_Fixed_Point_Type (B_Typ)
6028 and then (Base_Type (TL) = Base_Type (Standard_Integer)
6029 or else TL = Universal_Integer)
6030 then
6031 Resolve (R, B_Typ);
6032 Resolve (L, TL);
6034 elsif Is_Fixed_Point_Type (B_Typ)
6035 and then (Base_Type (TR) = Base_Type (Standard_Integer)
6036 or else TR = Universal_Integer)
6037 then
6038 Resolve (L, B_Typ);
6039 Resolve (R, TR);
6041 -- If both operands are universal and the context is a floating
6042 -- point type, the operands are resolved to the type of the context.
6044 elsif Is_Floating_Point_Type (B_Typ) then
6045 Resolve (L, B_Typ);
6046 Resolve (R, B_Typ);
6048 else
6049 Set_Mixed_Mode_Operand (L, TR);
6050 Set_Mixed_Mode_Operand (R, TL);
6051 end if;
6053 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6054 -- multiplying operators from being used when the expected type is
6055 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6056 -- some cases where the expected type is actually Any_Real;
6057 -- Expected_Type_Is_Any_Real takes care of that case.
6059 if Etype (N) = Universal_Fixed
6060 or else Etype (N) = Any_Fixed
6061 then
6062 if B_Typ = Universal_Fixed
6063 and then not Expected_Type_Is_Any_Real (N)
6064 and then Nkind (Parent (N)) not in
6065 N_Type_Conversion | N_Unchecked_Type_Conversion
6066 then
6067 Error_Msg_N ("type cannot be determined from context!", N);
6068 Error_Msg_N ("\explicit conversion to result type required", N);
6070 Set_Etype (L, Any_Type);
6071 Set_Etype (R, Any_Type);
6073 else
6074 if Ada_Version = Ada_83
6075 and then Etype (N) = Universal_Fixed
6076 and then Nkind (Parent (N)) not in
6077 N_Type_Conversion | N_Unchecked_Type_Conversion
6078 then
6079 Error_Msg_N
6080 ("(Ada 83) fixed-point operation needs explicit "
6081 & "conversion", N);
6082 end if;
6084 -- The expected type is "any real type" in contexts like
6086 -- type T is delta <universal_fixed-expression> ...
6088 -- in which case we need to set the type to Universal_Real
6089 -- so that static expression evaluation will work properly.
6091 if Expected_Type_Is_Any_Real (N) then
6092 Set_Etype (N, Universal_Real);
6093 else
6094 Set_Etype (N, B_Typ);
6095 end if;
6096 end if;
6098 elsif Is_Fixed_Point_Type (B_Typ)
6099 and then (Is_Integer_Or_Universal (L)
6100 or else Nkind (L) = N_Real_Literal
6101 or else Nkind (R) = N_Real_Literal
6102 or else Is_Integer_Or_Universal (R))
6103 then
6104 Set_Etype (N, B_Typ);
6106 elsif Etype (N) = Any_Fixed then
6108 -- If no previous errors, this is only possible if one operand is
6109 -- overloaded and the context is universal. Resolve as such.
6111 Set_Etype (N, B_Typ);
6112 end if;
6114 else
6115 if Is_Universal_Numeric_Type (TL)
6116 and then
6117 Is_Universal_Numeric_Type (TR)
6118 then
6119 Check_For_Visible_Operator (N, B_Typ);
6120 end if;
6122 -- If the context is Universal_Fixed and the operands are also
6123 -- universal fixed, this is an error, unless there is only one
6124 -- applicable fixed_point type (usually Duration).
6126 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6127 T := Unique_Fixed_Point_Type (N);
6129 if T = Any_Type then
6130 Set_Etype (N, T);
6131 return;
6132 else
6133 Resolve (L, T);
6134 Resolve (R, T);
6135 end if;
6137 else
6138 Resolve (L, B_Typ);
6139 Resolve (R, B_Typ);
6140 end if;
6142 -- If one of the arguments was resolved to a non-universal type.
6143 -- label the result of the operation itself with the same type.
6144 -- Do the same for the universal argument, if any.
6146 T := Intersect_Types (L, R);
6147 Set_Etype (N, Base_Type (T));
6148 Set_Operand_Type (L);
6149 Set_Operand_Type (R);
6150 end if;
6152 Generate_Operator_Reference (N, Typ);
6153 Analyze_Dimension (N);
6154 Eval_Arithmetic_Op (N);
6156 -- Set overflow and division checking bit
6158 if Nkind (N) in N_Op then
6159 if not Overflow_Checks_Suppressed (Etype (N)) then
6160 Enable_Overflow_Check (N);
6161 end if;
6163 -- Give warning if explicit division by zero
6165 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6166 and then not Division_Checks_Suppressed (Etype (N))
6167 then
6168 Rop := Right_Opnd (N);
6170 if Compile_Time_Known_Value (Rop)
6171 and then ((Is_Integer_Type (Etype (Rop))
6172 and then Expr_Value (Rop) = Uint_0)
6173 or else
6174 (Is_Real_Type (Etype (Rop))
6175 and then Expr_Value_R (Rop) = Ureal_0))
6176 then
6177 -- Specialize the warning message according to the operation.
6178 -- When SPARK_Mode is On, force a warning instead of an error
6179 -- in that case, as this likely corresponds to deactivated
6180 -- code. The following warnings are for the case
6182 case Nkind (N) is
6183 when N_Op_Divide =>
6185 -- For division, we have two cases, for float division
6186 -- of an unconstrained float type, on a machine where
6187 -- Machine_Overflows is false, we don't get an exception
6188 -- at run-time, but rather an infinity or Nan. The Nan
6189 -- case is pretty obscure, so just warn about infinities.
6191 if Is_Floating_Point_Type (Typ)
6192 and then not Is_Constrained (Typ)
6193 and then not Machine_Overflows_On_Target
6194 then
6195 Error_Msg_N
6196 ("float division by zero, may generate "
6197 & "'+'/'- infinity??", Right_Opnd (N));
6199 -- For all other cases, we get a Constraint_Error
6201 else
6202 Apply_Compile_Time_Constraint_Error
6203 (N, "division by zero??", CE_Divide_By_Zero,
6204 Loc => Sloc (Right_Opnd (N)),
6205 Warn => SPARK_Mode = On);
6206 end if;
6208 when N_Op_Rem =>
6209 Apply_Compile_Time_Constraint_Error
6210 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6211 Loc => Sloc (Right_Opnd (N)),
6212 Warn => SPARK_Mode = On);
6214 when N_Op_Mod =>
6215 Apply_Compile_Time_Constraint_Error
6216 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6217 Loc => Sloc (Right_Opnd (N)),
6218 Warn => SPARK_Mode = On);
6220 -- Division by zero can only happen with division, rem,
6221 -- and mod operations.
6223 when others =>
6224 raise Program_Error;
6225 end case;
6227 -- Otherwise just set the flag to check at run time
6229 else
6230 Activate_Division_Check (N);
6231 end if;
6232 end if;
6234 -- If Restriction No_Implicit_Conditionals is active, then it is
6235 -- violated if either operand can be negative for mod, or for rem
6236 -- if both operands can be negative.
6238 if Restriction_Check_Required (No_Implicit_Conditionals)
6239 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6240 then
6241 declare
6242 Lo : Uint;
6243 Hi : Uint;
6244 OK : Boolean;
6246 LNeg : Boolean;
6247 RNeg : Boolean;
6248 -- Set if corresponding operand might be negative
6250 begin
6251 Determine_Range
6252 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6253 LNeg := not OK or else Lo < 0;
6255 Determine_Range
6256 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6257 RNeg := not OK or else Lo < 0;
6259 -- Check if we will be generating conditionals. There are two
6260 -- cases where that can happen, first for REM, the only case
6261 -- is largest negative integer mod -1, where the division can
6262 -- overflow, but we still have to give the right result. The
6263 -- front end generates a test for this annoying case. Here we
6264 -- just test if both operands can be negative (that's what the
6265 -- expander does, so we match its logic here).
6267 -- The second case is mod where either operand can be negative.
6268 -- In this case, the back end has to generate additional tests.
6270 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6271 or else
6272 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6273 then
6274 Check_Restriction (No_Implicit_Conditionals, N);
6275 end if;
6276 end;
6277 end if;
6278 end if;
6280 Check_Unset_Reference (L);
6281 Check_Unset_Reference (R);
6282 end Resolve_Arithmetic_Op;
6284 ------------------
6285 -- Resolve_Call --
6286 ------------------
6288 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6289 Loc : constant Source_Ptr := Sloc (N);
6290 Subp : constant Node_Id := Name (N);
6291 Body_Id : Entity_Id;
6292 I : Interp_Index;
6293 It : Interp;
6294 Nam : Entity_Id;
6295 Nam_Decl : Node_Id;
6296 Nam_UA : Entity_Id;
6297 Norm_OK : Boolean;
6298 Rtype : Entity_Id;
6299 Scop : Entity_Id;
6301 begin
6302 -- Preserve relevant elaboration-related attributes of the context which
6303 -- are no longer available or very expensive to recompute once analysis,
6304 -- resolution, and expansion are over.
6306 Mark_Elaboration_Attributes
6307 (N_Id => N,
6308 Checks => True,
6309 Modes => True,
6310 Warnings => True);
6312 -- The context imposes a unique interpretation with type Typ on a
6313 -- procedure or function call. Find the entity of the subprogram that
6314 -- yields the expected type, and propagate the corresponding formal
6315 -- constraints on the actuals. The caller has established that an
6316 -- interpretation exists, and emitted an error if not unique.
6318 -- First deal with the case of a call to an access-to-subprogram,
6319 -- dereference made explicit in Analyze_Call.
6321 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6322 if not Is_Overloaded (Subp) then
6323 Nam := Etype (Subp);
6325 else
6326 -- Find the interpretation whose type (a subprogram type) has a
6327 -- return type that is compatible with the context. Analysis of
6328 -- the node has established that one exists.
6330 Nam := Empty;
6332 Get_First_Interp (Subp, I, It);
6333 while Present (It.Typ) loop
6334 if Covers (Typ, Etype (It.Typ)) then
6335 Nam := It.Typ;
6336 exit;
6337 end if;
6339 Get_Next_Interp (I, It);
6340 end loop;
6342 if No (Nam) then
6343 raise Program_Error;
6344 end if;
6345 end if;
6347 -- If the prefix is not an entity, then resolve it
6349 if not Is_Entity_Name (Subp) then
6350 Resolve (Subp, Nam);
6351 end if;
6353 -- For an indirect call, we always invalidate checks, since we do not
6354 -- know whether the subprogram is local or global. Yes we could do
6355 -- better here, e.g. by knowing that there are no local subprograms,
6356 -- but it does not seem worth the effort. Similarly, we kill all
6357 -- knowledge of current constant values.
6359 Kill_Current_Values;
6361 -- If this is a procedure call which is really an entry call, do
6362 -- the conversion of the procedure call to an entry call. Protected
6363 -- operations use the same circuitry because the name in the call
6364 -- can be an arbitrary expression with special resolution rules.
6366 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6367 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6368 then
6369 Resolve_Entry_Call (N, Typ);
6371 if Legacy_Elaboration_Checks then
6372 Check_Elab_Call (N);
6373 end if;
6375 -- Annotate the tree by creating a call marker in case the original
6376 -- call is transformed by expansion. The call marker is automatically
6377 -- saved for later examination by the ABE Processing phase.
6379 Build_Call_Marker (N);
6381 -- Kill checks and constant values, as above for indirect case
6382 -- Who knows what happens when another task is activated?
6384 Kill_Current_Values;
6385 return;
6387 -- Normal subprogram call with name established in Resolve
6389 elsif not Is_Type (Entity (Subp)) then
6390 Nam := Entity (Subp);
6391 Set_Entity_With_Checks (Subp, Nam);
6393 -- Otherwise we must have the case of an overloaded call
6395 else
6396 pragma Assert (Is_Overloaded (Subp));
6398 -- Initialize Nam to prevent warning (we know it will be assigned
6399 -- in the loop below, but the compiler does not know that).
6401 Nam := Empty;
6403 Get_First_Interp (Subp, I, It);
6404 while Present (It.Typ) loop
6405 if Covers (Typ, It.Typ) then
6406 Nam := It.Nam;
6407 Set_Entity_With_Checks (Subp, Nam);
6408 exit;
6409 end if;
6411 Get_Next_Interp (I, It);
6412 end loop;
6413 end if;
6415 -- Check that a call to Current_Task does not occur in an entry body
6417 if Is_RTE (Nam, RE_Current_Task) then
6418 declare
6419 P : Node_Id;
6421 begin
6422 P := N;
6423 loop
6424 P := Parent (P);
6426 -- Exclude calls that occur within the default of a formal
6427 -- parameter of the entry, since those are evaluated outside
6428 -- of the body.
6430 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6432 if Nkind (P) = N_Entry_Body
6433 or else (Nkind (P) = N_Subprogram_Body
6434 and then Is_Entry_Barrier_Function (P))
6435 then
6436 Rtype := Etype (N);
6437 Error_Msg_Warn := SPARK_Mode /= On;
6438 Error_Msg_NE
6439 ("& should not be used in entry body (RM C.7(17))<<",
6440 N, Nam);
6441 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6442 Rewrite (N,
6443 Make_Raise_Program_Error (Loc,
6444 Reason => PE_Current_Task_In_Entry_Body));
6445 Set_Etype (N, Rtype);
6446 return;
6447 end if;
6448 end loop;
6449 end;
6450 end if;
6452 -- Check that a procedure call does not occur in the context of the
6453 -- entry call statement of a conditional or timed entry call. Note that
6454 -- the case of a call to a subprogram renaming of an entry will also be
6455 -- rejected. The test for N not being an N_Entry_Call_Statement is
6456 -- defensive, covering the possibility that the processing of entry
6457 -- calls might reach this point due to later modifications of the code
6458 -- above.
6460 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6461 and then Nkind (N) /= N_Entry_Call_Statement
6462 and then Entry_Call_Statement (Parent (N)) = N
6463 then
6464 if Ada_Version < Ada_2005 then
6465 Error_Msg_N ("entry call required in select statement", N);
6467 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6468 -- for a procedure_or_entry_call, the procedure_name or
6469 -- procedure_prefix of the procedure_call_statement shall denote
6470 -- an entry renamed by a procedure, or (a view of) a primitive
6471 -- subprogram of a limited interface whose first parameter is
6472 -- a controlling parameter.
6474 elsif Nkind (N) = N_Procedure_Call_Statement
6475 and then not Is_Renamed_Entry (Nam)
6476 and then not Is_Controlling_Limited_Procedure (Nam)
6477 then
6478 Error_Msg_N
6479 ("entry call or dispatching primitive of interface required", N);
6480 end if;
6481 end if;
6483 -- Check that this is not a call to a protected procedure or entry from
6484 -- within a protected function.
6486 Check_Internal_Protected_Use (N, Nam);
6488 -- Freeze the subprogram name if not in a spec-expression. Note that
6489 -- we freeze procedure calls as well as function calls. Procedure calls
6490 -- are not frozen according to the rules (RM 13.14(14)) because it is
6491 -- impossible to have a procedure call to a non-frozen procedure in
6492 -- pure Ada, but in the code that we generate in the expander, this
6493 -- rule needs extending because we can generate procedure calls that
6494 -- need freezing.
6496 -- In Ada 2012, expression functions may be called within pre/post
6497 -- conditions of subsequent functions or expression functions. Such
6498 -- calls do not freeze when they appear within generated bodies,
6499 -- (including the body of another expression function) which would
6500 -- place the freeze node in the wrong scope. An expression function
6501 -- is frozen in the usual fashion, by the appearance of a real body,
6502 -- or at the end of a declarative part. However an implicit call to
6503 -- an expression function may appear when it is part of a default
6504 -- expression in a call to an initialization procedure, and must be
6505 -- frozen now, even if the body is inserted at a later point.
6506 -- Otherwise, the call freezes the expression if expander is active,
6507 -- for example as part of an object declaration.
6509 if Is_Entity_Name (Subp)
6510 and then not In_Spec_Expression
6511 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6512 and then not (Chars (Current_Scope) = Name_uWrapped_Statements
6513 and then Is_Expression_Function_Or_Completion
6514 (Scope (Current_Scope)))
6515 and then
6516 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6517 or else Expander_Active)
6518 then
6519 if Is_Expression_Function (Entity (Subp)) then
6521 -- Force freeze of expression function in call
6523 Set_Comes_From_Source (Subp, True);
6524 Set_Must_Not_Freeze (Subp, False);
6525 end if;
6527 Freeze_Expression (Subp);
6528 end if;
6530 -- For a predefined operator, the type of the result is the type imposed
6531 -- by context, except for a predefined operation on universal fixed.
6532 -- Otherwise the type of the call is the type returned by the subprogram
6533 -- being called.
6535 if Is_Predefined_Op (Nam) then
6536 if Etype (N) /= Universal_Fixed then
6537 Set_Etype (N, Typ);
6538 end if;
6540 -- If the subprogram returns an array type, and the context requires the
6541 -- component type of that array type, the node is really an indexing of
6542 -- the parameterless call. Resolve as such. A pathological case occurs
6543 -- when the type of the component is an access to the array type. In
6544 -- this case the call is truly ambiguous. If the call is to an intrinsic
6545 -- subprogram, it can't be an indexed component. This check is necessary
6546 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6547 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6548 -- pointers to the same array), the compiler gets confused and does an
6549 -- infinite recursion.
6551 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6552 and then
6553 ((Is_Array_Type (Etype (Nam))
6554 and then Covers (Typ, Component_Type (Etype (Nam))))
6555 or else
6556 (Is_Access_Type (Etype (Nam))
6557 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6558 and then
6559 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6560 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6561 then
6562 declare
6563 Index_Node : Node_Id;
6564 New_Subp : Node_Id;
6565 Ret_Type : constant Entity_Id := Etype (Nam);
6567 begin
6568 -- If this is a parameterless call there is no ambiguity and the
6569 -- call has the type of the function.
6571 if No (First_Actual (N)) then
6572 Set_Etype (N, Etype (Nam));
6574 if Present (First_Formal (Nam)) then
6575 Resolve_Actuals (N, Nam);
6576 end if;
6578 -- Annotate the tree by creating a call marker in case the
6579 -- original call is transformed by expansion. The call marker
6580 -- is automatically saved for later examination by the ABE
6581 -- Processing phase.
6583 Build_Call_Marker (N);
6585 elsif Is_Access_Type (Ret_Type)
6587 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6588 then
6589 Error_Msg_N
6590 ("cannot disambiguate function call and indexing", N);
6591 else
6592 New_Subp := Relocate_Node (Subp);
6594 -- The called entity may be an explicit dereference, in which
6595 -- case there is no entity to set.
6597 if Nkind (New_Subp) /= N_Explicit_Dereference then
6598 Set_Entity (Subp, Nam);
6599 end if;
6601 if (Is_Array_Type (Ret_Type)
6602 and then Component_Type (Ret_Type) /= Any_Type)
6603 or else
6604 (Is_Access_Type (Ret_Type)
6605 and then
6606 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6607 then
6608 if Needs_No_Actuals (Nam) then
6610 -- Indexed call to a parameterless function
6612 Index_Node :=
6613 Make_Indexed_Component (Loc,
6614 Prefix =>
6615 Make_Function_Call (Loc, Name => New_Subp),
6616 Expressions => Parameter_Associations (N));
6617 else
6618 -- An Ada 2005 prefixed call to a primitive operation
6619 -- whose first parameter is the prefix. This prefix was
6620 -- prepended to the parameter list, which is actually a
6621 -- list of indexes. Remove the prefix in order to build
6622 -- the proper indexed component.
6624 Index_Node :=
6625 Make_Indexed_Component (Loc,
6626 Prefix =>
6627 Make_Function_Call (Loc,
6628 Name => New_Subp,
6629 Parameter_Associations =>
6630 New_List
6631 (Remove_Head (Parameter_Associations (N)))),
6632 Expressions => Parameter_Associations (N));
6633 end if;
6635 -- Preserve the parenthesis count of the node
6637 Set_Paren_Count (Index_Node, Paren_Count (N));
6639 -- Since we are correcting a node classification error made
6640 -- by the parser, we call Replace rather than Rewrite.
6642 Replace (N, Index_Node);
6644 Set_Etype (Prefix (N), Ret_Type);
6645 Set_Etype (N, Typ);
6647 if Legacy_Elaboration_Checks then
6648 Check_Elab_Call (Prefix (N));
6649 end if;
6651 -- Annotate the tree by creating a call marker in case
6652 -- the original call is transformed by expansion. The call
6653 -- marker is automatically saved for later examination by
6654 -- the ABE Processing phase.
6656 Build_Call_Marker (Prefix (N));
6658 Resolve_Indexed_Component (N, Typ);
6659 end if;
6660 end if;
6662 return;
6663 end;
6665 else
6666 -- If the called function is not declared in the main unit and it
6667 -- returns the limited view of type then use the available view (as
6668 -- is done in Try_Object_Operation) to prevent back-end confusion;
6669 -- for the function entity itself. The call must appear in a context
6670 -- where the nonlimited view is available. If the function entity is
6671 -- in the extended main unit then no action is needed, because the
6672 -- back end handles this case. In either case the type of the call
6673 -- is the nonlimited view.
6675 if From_Limited_With (Etype (Nam))
6676 and then Present (Available_View (Etype (Nam)))
6677 then
6678 Set_Etype (N, Available_View (Etype (Nam)));
6680 if not In_Extended_Main_Code_Unit (Nam) then
6681 Set_Etype (Nam, Available_View (Etype (Nam)));
6682 end if;
6684 else
6685 Set_Etype (N, Etype (Nam));
6686 end if;
6687 end if;
6689 -- In the case where the call is to an overloaded subprogram, Analyze
6690 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6691 -- such a case Normalize_Actuals needs to be called once more to order
6692 -- the actuals correctly. Otherwise the call will have the ordering
6693 -- given by the last overloaded subprogram whether this is the correct
6694 -- one being called or not.
6696 if Is_Overloaded (Subp) then
6697 Normalize_Actuals (N, Nam, False, Norm_OK);
6698 pragma Assert (Norm_OK);
6699 end if;
6701 -- In any case, call is fully resolved now. Reset Overload flag, to
6702 -- prevent subsequent overload resolution if node is analyzed again
6704 Set_Is_Overloaded (Subp, False);
6705 Set_Is_Overloaded (N, False);
6707 -- A Ghost entity must appear in a specific context
6709 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6710 Check_Ghost_Context (Nam, N);
6711 end if;
6713 if Is_Entity_Name (Subp) then
6714 Local_Restrict.Check_Call
6715 (Call => N, Callee => Ultimate_Alias (Nam));
6716 else
6717 Local_Restrict.Check_Call (Call => N);
6718 end if;
6720 -- If we are calling the current subprogram from immediately within its
6721 -- body, then that is the case where we can sometimes detect cases of
6722 -- infinite recursion statically. Do not try this in case restriction
6723 -- No_Recursion is in effect anyway, and do it only for source calls.
6725 if Comes_From_Source (N) then
6726 Scop := Current_Scope;
6728 -- Issue warning for possible infinite recursion in the absence
6729 -- of the No_Recursion restriction.
6731 if Same_Or_Aliased_Subprograms (Nam, Scop)
6732 and then not Restriction_Active (No_Recursion)
6733 and then not Is_Static_Function (Scop)
6734 and then Check_Infinite_Recursion (N)
6735 then
6736 -- Here we detected and flagged an infinite recursion, so we do
6737 -- not need to test the case below for further warnings. Also we
6738 -- are all done if we now have a raise SE node.
6740 if Nkind (N) = N_Raise_Storage_Error then
6741 return;
6742 end if;
6744 -- If call is to immediately containing subprogram, then check for
6745 -- the case of a possible run-time detectable infinite recursion.
6747 else
6748 Scope_Loop : while Scop /= Standard_Standard loop
6749 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6751 -- Ada 2022 (AI12-0075): Static functions are never allowed
6752 -- to make a recursive call, as specified by 6.8(5.4/5).
6754 if Is_Static_Function (Scop) then
6755 Error_Msg_N
6756 ("recursive call not allowed in static expression "
6757 & "function", N);
6759 Set_Error_Posted (Scop);
6761 exit Scope_Loop;
6762 end if;
6764 -- Although in general case, recursion is not statically
6765 -- checkable, the case of calling an immediately containing
6766 -- subprogram is easy to catch.
6768 if not Is_Ignored_Ghost_Entity (Nam) then
6769 Check_Restriction (No_Recursion, N);
6770 end if;
6772 -- If the recursive call is to a parameterless subprogram,
6773 -- then even if we can't statically detect infinite
6774 -- recursion, this is pretty suspicious, and we output a
6775 -- warning. Furthermore, we will try later to detect some
6776 -- cases here at run time by expanding checking code (see
6777 -- Detect_Infinite_Recursion in package Exp_Ch6).
6779 -- If the recursive call is within a handler, do not emit a
6780 -- warning, because this is a common idiom: loop until input
6781 -- is correct, catch illegal input in handler and restart.
6783 if No (First_Formal (Nam))
6784 and then Etype (Nam) = Standard_Void_Type
6785 and then not Error_Posted (N)
6786 and then Nkind (Parent (N)) /= N_Exception_Handler
6787 then
6788 -- For the case of a procedure call. We give the message
6789 -- only if the call is the first statement in a sequence
6790 -- of statements, or if all previous statements are
6791 -- simple assignments. This is simply a heuristic to
6792 -- decrease false positives, without losing too many good
6793 -- warnings. The idea is that these previous statements
6794 -- may affect global variables the procedure depends on.
6795 -- We also exclude raise statements, that may arise from
6796 -- constraint checks and are probably unrelated to the
6797 -- intended control flow.
6799 if Nkind (N) = N_Procedure_Call_Statement
6800 and then Is_List_Member (N)
6801 then
6802 declare
6803 P : Node_Id;
6804 begin
6805 P := Prev (N);
6806 while Present (P) loop
6807 if Nkind (P) not in N_Assignment_Statement
6808 | N_Raise_Constraint_Error
6809 then
6810 exit Scope_Loop;
6811 end if;
6813 Prev (P);
6814 end loop;
6815 end;
6816 end if;
6818 -- Do not give warning if we are in a conditional context
6820 declare
6821 K : constant Node_Kind := Nkind (Parent (N));
6822 begin
6823 if (K = N_Loop_Statement
6824 and then Present (Iteration_Scheme (Parent (N))))
6825 or else K = N_If_Statement
6826 or else K = N_Elsif_Part
6827 or else K = N_Case_Statement_Alternative
6828 then
6829 exit Scope_Loop;
6830 end if;
6831 end;
6833 -- Here warning is to be issued
6835 Set_Has_Recursive_Call (Nam);
6836 Error_Msg_Warn := SPARK_Mode /= On;
6837 Error_Msg_N ("possible infinite recursion<<!", N);
6838 Error_Msg_N ("\Storage_Error ]<<!", N);
6839 end if;
6841 exit Scope_Loop;
6842 end if;
6844 Scop := Scope (Scop);
6845 end loop Scope_Loop;
6846 end if;
6847 end if;
6849 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6851 Check_Obsolescent_2005_Entity (Nam, Subp);
6853 -- If subprogram name is a predefined operator, it was given in
6854 -- functional notation. Replace call node with operator node, so
6855 -- that actuals can be resolved appropriately.
6857 if Ekind (Nam) = E_Operator or else Is_Predefined_Op (Nam) then
6858 Make_Call_Into_Operator (N, Typ, Nam);
6859 return;
6861 elsif Present (Alias (Nam)) and then Is_Predefined_Op (Alias (Nam)) then
6862 Resolve_Actuals (N, Nam);
6863 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6864 return;
6865 end if;
6867 -- Create a transient scope if the expander is active and the resulting
6868 -- type requires it.
6870 -- There are several notable exceptions:
6872 -- a) Intrinsic subprograms (Unchecked_Conversion and source info
6873 -- functions) do not use the secondary stack even though the return
6874 -- type may be unconstrained.
6876 -- b) Subprograms that are ignored ghost entities do not return anything
6878 -- c) Calls to a build-in-place function, since such functions may
6879 -- allocate their result directly in a target object, and cases where
6880 -- the result does get allocated in the secondary stack are checked for
6881 -- within the specialized Exp_Ch6 procedures for expanding those
6882 -- build-in-place calls.
6884 -- d) Calls to inlinable expression functions do not use the secondary
6885 -- stack (since the call will be replaced by its returned object).
6887 -- e) If the subprogram is marked Inline, then even if it returns
6888 -- an unconstrained type the call does not require use of the secondary
6889 -- stack. However, inlining will only take place if the body to inline
6890 -- is already present. It may not be available if e.g. the subprogram is
6891 -- declared in a child instance.
6893 -- f) If the subprogram is a static expression function and the call is
6894 -- a static call (the actuals are all static expressions), then we never
6895 -- want to create a transient scope (this could occur in the case of a
6896 -- static string-returning call).
6898 -- g) If the call is the expression of a simple return statement that
6899 -- returns on the same stack, since it will be handled as a tail call
6900 -- by Expand_Simple_Function_Return.
6902 if Expander_Active
6903 and then Ekind (Nam) in E_Function | E_Subprogram_Type
6904 and then Requires_Transient_Scope (Etype (Nam))
6905 and then not Is_Intrinsic_Subprogram (Nam)
6906 and then not Is_Ignored_Ghost_Entity (Nam)
6907 and then not Is_Build_In_Place_Function (Nam)
6908 and then not Is_Inlinable_Expression_Function (Nam)
6909 and then not (Is_Inlined (Nam)
6910 and then Has_Pragma_Inline (Nam)
6911 and then Nkind (Unit_Declaration_Node (Nam)) =
6912 N_Subprogram_Declaration
6913 and then
6914 Present (Body_To_Inline (Unit_Declaration_Node (Nam))))
6915 and then not Is_Static_Function_Call (N)
6916 and then not (Nkind (Parent (N)) = N_Simple_Return_Statement
6917 and then
6918 Needs_Secondary_Stack
6919 (Etype
6920 (Return_Applies_To
6921 (Return_Statement_Entity (Parent (N))))) =
6922 Needs_Secondary_Stack (Etype (Nam)))
6923 then
6924 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
6926 -- If the call appears within the bounds of a loop, it will be
6927 -- rewritten and reanalyzed, nothing left to do here.
6929 if Nkind (N) /= N_Function_Call then
6930 return;
6931 end if;
6932 end if;
6934 -- A protected function cannot be called within the definition of the
6935 -- enclosing protected type, unless it is part of a pre/postcondition
6936 -- on another protected operation. This may appear in the entry wrapper
6937 -- created for an entry with preconditions.
6939 if Is_Protected_Type (Scope (Nam))
6940 and then In_Open_Scopes (Scope (Nam))
6941 and then not Has_Completion (Scope (Nam))
6942 and then not In_Spec_Expression
6943 and then not Is_Entry_Wrapper (Current_Scope)
6944 then
6945 Error_Msg_NE
6946 ("& cannot be called before end of protected definition", N, Nam);
6947 end if;
6949 -- Propagate interpretation to actuals, and add default expressions
6950 -- where needed.
6952 if Present (First_Formal (Nam)) then
6953 Resolve_Actuals (N, Nam);
6955 -- Overloaded literals are rewritten as function calls, for purpose of
6956 -- resolution. After resolution, we can replace the call with the
6957 -- literal itself.
6959 elsif Ekind (Nam) = E_Enumeration_Literal then
6960 Copy_Node (Subp, N);
6961 Resolve_Entity_Name (N, Typ);
6963 -- Avoid validation, since it is a static function call
6965 Generate_Reference (Nam, Subp);
6966 return;
6967 end if;
6969 -- If the subprogram is not global, then kill all saved values and
6970 -- checks. This is a bit conservative, since in many cases we could do
6971 -- better, but it is not worth the effort. Similarly, we kill constant
6972 -- values. However we do not need to do this for internal entities
6973 -- (unless they are inherited user-defined subprograms), since they
6974 -- are not in the business of molesting local values.
6976 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6977 -- kill all checks and values for calls to global subprograms. This
6978 -- takes care of the case where an access to a local subprogram is
6979 -- taken, and could be passed directly or indirectly and then called
6980 -- from almost any context.
6982 -- Note: we do not do this step till after resolving the actuals. That
6983 -- way we still take advantage of the current value information while
6984 -- scanning the actuals.
6986 -- We suppress killing values if we are processing the nodes associated
6987 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6988 -- type kills all the values as part of analyzing the code that
6989 -- initializes the dispatch tables.
6991 if Inside_Freezing_Actions = 0
6992 and then (not Is_Library_Level_Entity (Nam)
6993 or else Suppress_Value_Tracking_On_Call
6994 (Nearest_Dynamic_Scope (Current_Scope)))
6995 and then (Comes_From_Source (Nam)
6996 or else (Present (Alias (Nam))
6997 and then Comes_From_Source (Alias (Nam))))
6998 then
6999 Kill_Current_Values;
7000 end if;
7002 -- If we are warning about unread OUT parameters, this is the place to
7003 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
7004 -- after the above call to Kill_Current_Values (since that call clears
7005 -- the Last_Assignment field of all local variables).
7007 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
7008 and then Comes_From_Source (N)
7009 and then In_Extended_Main_Source_Unit (N)
7010 then
7011 declare
7012 F : Entity_Id;
7013 A : Node_Id;
7015 begin
7016 F := First_Formal (Nam);
7017 A := First_Actual (N);
7018 while Present (F) and then Present (A) loop
7019 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
7020 and then Warn_On_Modified_As_Out_Parameter (F)
7021 and then Is_Entity_Name (A)
7022 and then Present (Entity (A))
7023 and then Comes_From_Source (N)
7024 and then Safe_To_Capture_Value (N, Entity (A))
7025 then
7026 Set_Last_Assignment (Entity (A), A);
7027 end if;
7029 Next_Formal (F);
7030 Next_Actual (A);
7031 end loop;
7032 end;
7033 end if;
7035 -- If the subprogram is a primitive operation, check whether or not
7036 -- it is a correct dispatching call.
7038 if Is_Overloadable (Nam) and then Is_Dispatching_Operation (Nam) then
7039 Check_Dispatching_Call (N);
7041 -- If the subprogram is an abstract operation, then flag an error
7043 elsif Is_Overloadable (Nam) and then Is_Abstract_Subprogram (Nam) then
7044 Nondispatching_Call_To_Abstract_Operation (N, Nam);
7045 end if;
7047 -- If this is a dispatching call, generate the appropriate reference,
7048 -- for better source navigation in GNAT Studio.
7050 if Is_Overloadable (Nam) and then Present (Controlling_Argument (N)) then
7051 Generate_Reference (Nam, Subp, 'R');
7053 -- Normal case, not a dispatching call: generate a call reference
7055 else
7056 Generate_Reference (Nam, Subp, 's');
7057 end if;
7059 if Is_Intrinsic_Subprogram (Nam) then
7060 Check_Intrinsic_Call (N);
7061 end if;
7063 -- Check for violation of restriction No_Specific_Termination_Handlers
7064 -- and warn on a potentially blocking call to Abort_Task.
7066 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7067 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7068 or else
7069 Is_RTE (Nam, RE_Specific_Handler))
7070 then
7071 Check_Restriction (No_Specific_Termination_Handlers, N);
7073 elsif Is_RTE (Nam, RE_Abort_Task) then
7074 Check_Potentially_Blocking_Operation (N);
7075 end if;
7077 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7078 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7079 -- need to check the second argument to determine whether it is an
7080 -- absolute or relative timing event.
7082 if Restriction_Check_Required (No_Relative_Delay)
7083 and then Is_RTE (Nam, RE_Set_Handler)
7084 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7085 then
7086 Check_Restriction (No_Relative_Delay, N);
7087 end if;
7089 -- Issue an error for a call to an eliminated subprogram. This routine
7090 -- will not perform the check if the call appears within a default
7091 -- expression.
7093 Check_For_Eliminated_Subprogram (Subp, Nam);
7095 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7096 -- class-wide and the call dispatches on result in a context that does
7097 -- not provide a tag, the call raises Program_Error.
7099 if Nkind (N) = N_Function_Call
7100 and then In_Instance
7101 and then Is_Generic_Actual_Type (Typ)
7102 and then Is_Class_Wide_Type (Typ)
7103 and then Has_Controlling_Result (Nam)
7104 and then Nkind (Parent (N)) = N_Object_Declaration
7105 then
7106 -- Verify that none of the formals are controlling
7108 declare
7109 Call_OK : Boolean := False;
7110 F : Entity_Id;
7112 begin
7113 F := First_Formal (Nam);
7114 while Present (F) loop
7115 if Is_Controlling_Formal (F) then
7116 Call_OK := True;
7117 exit;
7118 end if;
7120 Next_Formal (F);
7121 end loop;
7123 if not Call_OK then
7124 Error_Msg_Warn := SPARK_Mode /= On;
7125 Error_Msg_N ("!cannot determine tag of result<<", N);
7126 Error_Msg_N ("\Program_Error [<<!", N);
7127 Insert_Action (N,
7128 Make_Raise_Program_Error (Sloc (N),
7129 Reason => PE_Explicit_Raise));
7130 end if;
7131 end;
7132 end if;
7134 -- Check for calling a function with OUT or IN OUT parameter when the
7135 -- calling context (us right now) is not Ada 2012, so does not allow
7136 -- OUT or IN OUT parameters in function calls. Functions declared in
7137 -- a predefined unit are OK, as they may be called indirectly from a
7138 -- user-declared instantiation.
7140 if Ada_Version < Ada_2012
7141 and then Ekind (Nam) = E_Function
7142 and then Has_Out_Or_In_Out_Parameter (Nam)
7143 and then not In_Predefined_Unit (Nam)
7144 then
7145 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7146 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7147 end if;
7149 -- Check the dimensions of the actuals in the call. For function calls,
7150 -- propagate the dimensions from the returned type to N.
7152 Analyze_Dimension_Call (N, Nam);
7154 -- Check unreachable code after calls to procedures with No_Return
7156 if Ekind (Nam) = E_Procedure and then No_Return (Nam) then
7157 Check_Unreachable_Code (N);
7158 end if;
7160 -- All done, evaluate call and deal with elaboration issues
7162 Eval_Call (N);
7164 if Legacy_Elaboration_Checks then
7165 Check_Elab_Call (N);
7166 end if;
7168 -- Annotate the tree by creating a call marker in case the original call
7169 -- is transformed by expansion. The call marker is automatically saved
7170 -- for later examination by the ABE Processing phase.
7172 Build_Call_Marker (N);
7174 Mark_Use_Clauses (Subp);
7176 Warn_On_Overlapping_Actuals (Nam, N);
7178 -- Ada 2022 (AI12-0075): If the call is a static call to a static
7179 -- expression function, then we want to "inline" the call, replacing
7180 -- it with the folded static result. This is not done if the checking
7181 -- for a potentially static expression is enabled or if an error has
7182 -- been posted on the call (which may be due to the check for recursive
7183 -- calls, in which case we don't want to fall into infinite recursion
7184 -- when doing the inlining).
7186 if not Checking_Potentially_Static_Expression
7187 and then Is_Static_Function_Call (N)
7188 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7189 and then not Error_Posted (Ultimate_Alias (Nam))
7190 then
7191 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7193 -- In GNATprove mode, expansion is disabled, but we want to inline some
7194 -- subprograms to facilitate formal verification. Indirect calls through
7195 -- a subprogram type or within a generic cannot be inlined. Inlining is
7196 -- performed only for calls subject to SPARK_Mode => On.
7198 elsif GNATprove_Mode
7199 and then SPARK_Mode = On
7200 and then Is_Overloadable (Nam)
7201 and then not Inside_A_Generic
7202 then
7203 Nam_UA := Ultimate_Alias (Nam);
7204 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7206 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7207 Body_Id := Corresponding_Body (Nam_Decl);
7209 -- Nothing to do if the subprogram is not inlined (because it is
7210 -- recursive, directly or indirectly), or is not eligible for
7211 -- inlining GNATprove mode (because of properties of the
7212 -- subprogram itself), or inlining has been disabled with switch
7213 -- -gnatdm.
7215 if not Is_Inlined (Nam_UA)
7216 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7217 or else Debug_Flag_M
7218 then
7219 null;
7221 -- Calls cannot be inlined inside assertions, as GNATprove treats
7222 -- assertions as logic expressions. Only issue a message when the
7223 -- body has been seen, otherwise this leads to spurious messages
7224 -- on expression functions.
7226 elsif In_Assertion_Expr /= 0 then
7227 Cannot_Inline
7228 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7229 Suppress_Info => No (Body_Id));
7231 -- Calls cannot be inlined inside default expressions
7233 elsif In_Default_Expr then
7234 Cannot_Inline
7235 ("cannot inline & (in default expression)?", N, Nam_UA);
7237 -- Calls cannot be inlined inside potentially unevaluated
7238 -- expressions, as this would create complex actions inside
7239 -- expressions, that are not handled by GNATprove.
7241 elsif Is_Potentially_Unevaluated (N) then
7242 Cannot_Inline
7243 ("cannot inline & (in potentially unevaluated context)?",
7244 N, Nam_UA);
7246 -- Calls are not inlined inside the loop_parameter_specification
7247 -- or iterator_specification of the quantified expression, as they
7248 -- are only preanalyzed. Calls in the predicate part are handled
7249 -- by the previous test on potentially unevaluated expressions.
7251 elsif In_Quantified_Expression (N) then
7252 Cannot_Inline
7253 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7255 -- Inlining should not be performed during preanalysis
7257 elsif Full_Analysis then
7259 -- Do not inline calls inside expression functions or functions
7260 -- generated by the front end for subtype predicates, as this
7261 -- would prevent interpreting them as logical formulas in
7262 -- GNATprove. Only issue a message when the body has been seen,
7263 -- otherwise this leads to spurious messages on callees that
7264 -- are themselves expression functions.
7266 if Present (Current_Subprogram)
7267 and then
7268 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7269 or else Is_Predicate_Function (Current_Subprogram)
7270 or else Is_Invariant_Procedure (Current_Subprogram)
7271 or else Is_DIC_Procedure (Current_Subprogram))
7272 then
7273 declare
7274 Issue_Msg : constant Boolean :=
7275 Present (Body_Id)
7276 and then Present (Body_To_Inline (Nam_Decl));
7277 begin
7278 if Is_Predicate_Function (Current_Subprogram) then
7279 Cannot_Inline
7280 ("cannot inline & (inside predicate)?",
7281 N, Nam_UA, Suppress_Info => not Issue_Msg);
7283 elsif Is_Invariant_Procedure (Current_Subprogram) then
7284 Cannot_Inline
7285 ("cannot inline & (inside invariant)?",
7286 N, Nam_UA, Suppress_Info => not Issue_Msg);
7288 elsif Is_DIC_Procedure (Current_Subprogram) then
7289 Cannot_Inline
7290 ("cannot inline & (inside Default_Initial_Condition)?",
7291 N, Nam_UA, Suppress_Info => not Issue_Msg);
7293 else
7294 Cannot_Inline
7295 ("cannot inline & (inside expression function)?",
7296 N, Nam_UA, Suppress_Info => not Issue_Msg);
7297 end if;
7298 end;
7300 -- Cannot inline a call inside the definition of a record type,
7301 -- typically inside the constraints of the type. Calls in
7302 -- default expressions are also not inlined, but this is
7303 -- filtered out above when testing In_Default_Expr.
7305 elsif Is_Record_Type (Current_Scope) then
7306 Cannot_Inline
7307 ("cannot inline & (inside record type)?", N, Nam_UA);
7309 -- With the one-pass inlining technique, a call cannot be
7310 -- inlined if the corresponding body has not been seen yet.
7312 elsif No (Body_Id) then
7313 Cannot_Inline
7314 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7316 -- Nothing to do if there is no body to inline, indicating that
7317 -- the subprogram is not suitable for inlining in GNATprove
7318 -- mode.
7320 elsif No (Body_To_Inline (Nam_Decl)) then
7321 null;
7323 -- Calls cannot be inlined inside the conditions of while
7324 -- loops, as this would create complex actions inside
7325 -- the condition, that are not handled by GNATprove.
7327 elsif In_Statement_Condition_With_Actions (N) then
7328 Cannot_Inline
7329 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7331 -- Do not inline calls which would possibly lead to missing a
7332 -- type conversion check on an input parameter.
7334 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7335 Cannot_Inline
7336 ("cannot inline & (possible check on input parameters)?",
7337 N, Nam_UA);
7339 -- Otherwise, inline the call, issuing an info message when
7340 -- -gnatd_f is set.
7342 else
7343 if Debug_Flag_Underscore_F then
7344 Error_Msg_NE
7345 ("info: analyzing call to & in context?", N, Nam_UA);
7346 end if;
7348 Expand_Inlined_Call (N, Nam_UA, Nam);
7349 end if;
7350 end if;
7351 end if;
7352 end if;
7353 end Resolve_Call;
7355 -----------------------------
7356 -- Resolve_Case_Expression --
7357 -----------------------------
7359 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7360 Alt : Node_Id;
7361 Alt_Expr : Node_Id;
7362 Alt_Typ : Entity_Id;
7363 Is_Dyn : Boolean;
7365 begin
7366 Alt := First (Alternatives (N));
7367 while Present (Alt) loop
7368 Alt_Expr := Expression (Alt);
7370 if Error_Posted (Alt_Expr) then
7371 return;
7372 end if;
7374 Resolve_Dependent_Expression (N, Alt_Expr, Typ);
7376 Check_Unset_Reference (Alt_Expr);
7377 Alt_Typ := Etype (Alt_Expr);
7379 -- When the expression is of a scalar subtype different from the
7380 -- result subtype, then insert a conversion to ensure the generation
7381 -- of a constraint check.
7383 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7384 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7385 Analyze_And_Resolve (Alt_Expr, Typ);
7386 end if;
7388 Next (Alt);
7389 end loop;
7391 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7392 -- dynamically tagged must be known statically.
7394 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7395 Alt := First (Alternatives (N));
7396 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7398 while Present (Alt) loop
7399 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7400 Error_Msg_N
7401 ("all or none of the dependent expressions can be "
7402 & "dynamically tagged", N);
7403 end if;
7405 Next (Alt);
7406 end loop;
7407 end if;
7409 Set_Etype (N, Typ);
7410 Eval_Case_Expression (N);
7411 Analyze_Dimension (N);
7412 end Resolve_Case_Expression;
7414 -------------------------------
7415 -- Resolve_Character_Literal --
7416 -------------------------------
7418 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7419 B_Typ : constant Entity_Id := Base_Type (Typ);
7420 C : Entity_Id;
7422 begin
7423 -- Verify that the character does belong to the type of the context
7425 Set_Etype (N, B_Typ);
7426 Eval_Character_Literal (N);
7428 -- Wide_Wide_Character literals must always be defined, since the set
7429 -- of wide wide character literals is complete, i.e. if a character
7430 -- literal is accepted by the parser, then it is OK for wide wide
7431 -- character (out of range character literals are rejected).
7433 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7434 return;
7436 -- Always accept character literal for type Any_Character, which
7437 -- occurs in error situations and in comparisons of literals, both
7438 -- of which should accept all literals.
7440 elsif B_Typ = Any_Character then
7441 return;
7443 -- For Standard.Character or a type derived from it, check that the
7444 -- literal is in range.
7446 elsif Root_Type (B_Typ) = Standard_Character then
7447 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7448 return;
7449 end if;
7451 -- For Standard.Wide_Character or a type derived from it, check that the
7452 -- literal is in range.
7454 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7455 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7456 return;
7457 end if;
7459 -- If the entity is already set, this has already been resolved in a
7460 -- generic context, or comes from expansion. Nothing else to do.
7462 elsif Present (Entity (N)) then
7463 return;
7465 -- Otherwise we have a user defined character type, and we can use the
7466 -- standard visibility mechanisms to locate the referenced entity.
7468 else
7469 C := Current_Entity (N);
7470 while Present (C) loop
7471 if Etype (C) = B_Typ then
7472 Set_Entity_With_Checks (N, C);
7473 Generate_Reference (C, N);
7474 return;
7475 end if;
7477 C := Homonym (C);
7478 end loop;
7479 end if;
7481 -- If we fall through, then the literal does not match any of the
7482 -- entries of the enumeration type. This isn't just a constraint error
7483 -- situation, it is an illegality (see RM 4.2).
7485 Error_Msg_NE
7486 ("character not defined for }", N, First_Subtype (B_Typ));
7487 end Resolve_Character_Literal;
7489 ---------------------------
7490 -- Resolve_Comparison_Op --
7491 ---------------------------
7493 -- The operands must have compatible types and the boolean context does not
7494 -- participate in the resolution. The first pass verifies that the operands
7495 -- are not ambiguous and sets their type correctly, or to Any_Type in case
7496 -- of ambiguity. If both operands are strings or aggregates, then they are
7497 -- ambiguous even if they carry a single (universal) type.
7499 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7500 L : constant Node_Id := Left_Opnd (N);
7501 R : constant Node_Id := Right_Opnd (N);
7503 T : Entity_Id := Find_Unique_Type (L, R);
7505 begin
7506 if T = Any_Fixed then
7507 T := Unique_Fixed_Point_Type (L);
7508 end if;
7510 Set_Etype (N, Base_Type (Typ));
7511 Generate_Reference (T, N, ' ');
7513 if T = Any_Type then
7514 -- Deal with explicit ambiguity of operands
7516 if Is_Overloaded (L) or else Is_Overloaded (R) then
7517 Ambiguous_Operands (N);
7518 end if;
7520 return;
7521 end if;
7523 -- Deal with other error cases
7525 if T = Any_String or else
7526 T = Any_Composite or else
7527 T = Any_Character
7528 then
7529 if T = Any_Character then
7530 Ambiguous_Character (L);
7531 else
7532 Error_Msg_N ("ambiguous operands for comparison", N);
7533 end if;
7535 Set_Etype (N, Any_Type);
7536 return;
7537 end if;
7539 -- Resolve the operands if types OK
7541 Resolve (L, T);
7542 Resolve (R, T);
7543 Set_Compare_Type (N, T);
7544 Check_Unset_Reference (L);
7545 Check_Unset_Reference (R);
7546 Generate_Operator_Reference (N, T);
7547 Check_Low_Bound_Tested (N);
7549 -- Check comparison on unordered enumeration
7551 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7552 Error_Msg_Sloc := Sloc (Etype (L));
7553 Error_Msg_NE
7554 ("comparison on unordered enumeration type& declared#?.u?",
7555 N, Etype (L));
7556 end if;
7558 Analyze_Dimension (N);
7560 Eval_Relational_Op (N);
7561 end Resolve_Comparison_Op;
7563 --------------------------------
7564 -- Resolve_Declare_Expression --
7565 --------------------------------
7567 procedure Resolve_Declare_Expression
7568 (N : Node_Id;
7569 Typ : Entity_Id)
7571 Expr : constant Node_Id := Expression (N);
7573 Decl : Node_Id;
7574 Local : Entity_Id := Empty;
7576 function Replace_Local (N : Node_Id) return Traverse_Result;
7577 -- Use a tree traversal to replace each occurrence of the name of
7578 -- a local object declared in the construct, with the corresponding
7579 -- entity. This replaces the usual way to perform name capture by
7580 -- visibility, because it is not possible to place on the scope
7581 -- stack the fake scope created for the analysis of the local
7582 -- declarations; such a scope conflicts with the transient scopes
7583 -- that may be generated if the expression includes function calls
7584 -- requiring finalization.
7586 -------------------
7587 -- Replace_Local --
7588 -------------------
7590 function Replace_Local (N : Node_Id) return Traverse_Result is
7591 begin
7592 -- The identifier may be the prefix of a selected component,
7593 -- but not a selector name, because the local entities do not
7594 -- have a scope that can be named: a selected component whose
7595 -- selector is a homonym of a local entity must denote some
7596 -- global entity.
7598 if Nkind (N) = N_Identifier
7599 and then Chars (N) = Chars (Local)
7600 and then No (Entity (N))
7601 and then
7602 (Nkind (Parent (N)) /= N_Selected_Component
7603 or else N = Prefix (Parent (N)))
7604 then
7605 Set_Entity (N, Local);
7606 Set_Etype (N, Etype (Local));
7607 end if;
7609 return OK;
7610 end Replace_Local;
7612 procedure Replace_Local_Ref is new Traverse_Proc (Replace_Local);
7614 -- Start of processing for Resolve_Declare_Expression
7616 begin
7618 Decl := First (Actions (N));
7620 while Present (Decl) loop
7621 if Nkind (Decl) in
7622 N_Object_Declaration | N_Object_Renaming_Declaration
7623 and then Comes_From_Source (Defining_Identifier (Decl))
7624 then
7625 Local := Defining_Identifier (Decl);
7626 Replace_Local_Ref (Expr);
7628 -- Traverse the expression to replace references to local
7629 -- variables that occur within declarations of the
7630 -- declare_expression.
7632 declare
7633 D : Node_Id := Next (Decl);
7634 begin
7635 while Present (D) loop
7636 Replace_Local_Ref (D);
7637 Next (D);
7638 end loop;
7639 end;
7640 end if;
7642 Next (Decl);
7643 end loop;
7645 -- The end of the declarative list is a freeze point for the
7646 -- local declarations.
7648 if Present (Local) then
7649 Decl := Parent (Local);
7650 Freeze_All (First_Entity (Scope (Local)), Decl);
7651 end if;
7653 Resolve (Expr, Typ);
7654 Check_Unset_Reference (Expr);
7655 end Resolve_Declare_Expression;
7657 -----------------------------------
7658 -- Resolve_Dependent_Expression --
7659 -----------------------------------
7661 procedure Resolve_Dependent_Expression
7662 (N : Node_Id;
7663 Expr : Node_Id;
7664 Typ : Entity_Id)
7666 begin
7667 -- RM 4.5.7(8/3) says that the expected type of dependent expressions is
7668 -- that of the conditional expression but RM 4.5.7(10/3) forces the type
7669 -- of the conditional expression without changing the expected type (the
7670 -- expected type of the operand of a type conversion is any type), so we
7671 -- may have a gap between these two types that is bridged by the dynamic
7672 -- semantics specified by RM 4.5.7(20/3) with the associated legality
7673 -- rule RM 4.5.7(16/3) that will be automatically enforced.
7675 if Nkind (Parent (N)) = N_Type_Conversion
7676 and then Nkind (Expr) /= N_Raise_Expression
7677 then
7678 Convert_To_And_Rewrite (Typ, Expr);
7679 Analyze_And_Resolve (Expr);
7680 else
7681 Resolve (Expr, Typ);
7682 end if;
7683 end Resolve_Dependent_Expression;
7685 -----------------------------------------
7686 -- Resolve_Discrete_Subtype_Indication --
7687 -----------------------------------------
7689 procedure Resolve_Discrete_Subtype_Indication
7690 (N : Node_Id;
7691 Typ : Entity_Id)
7693 R : Node_Id;
7694 S : Entity_Id;
7696 begin
7697 Analyze (Subtype_Mark (N));
7698 S := Entity (Subtype_Mark (N));
7700 if Nkind (Constraint (N)) /= N_Range_Constraint then
7701 Error_Msg_N ("expect range constraint for discrete type", N);
7702 Set_Etype (N, Any_Type);
7704 else
7705 R := Range_Expression (Constraint (N));
7707 if R = Error then
7708 return;
7709 end if;
7711 Analyze (R);
7713 if Base_Type (S) /= Base_Type (Typ) then
7714 Error_Msg_NE
7715 ("expect subtype of }", N, First_Subtype (Typ));
7717 -- Rewrite the constraint as a range of Typ
7718 -- to allow compilation to proceed further.
7720 Set_Etype (N, Typ);
7721 Rewrite (Low_Bound (R),
7722 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7723 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7724 Attribute_Name => Name_First));
7725 Rewrite (High_Bound (R),
7726 Make_Attribute_Reference (Sloc (High_Bound (R)),
7727 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7728 Attribute_Name => Name_First));
7730 else
7731 Resolve (R, Typ);
7732 Set_Etype (N, Etype (R));
7734 -- Additionally, we must check that the bounds are compatible
7735 -- with the given subtype, which might be different from the
7736 -- type of the context.
7738 Apply_Range_Check (R, S);
7740 -- ??? If the above check statically detects a Constraint_Error
7741 -- it replaces the offending bound(s) of the range R with a
7742 -- Constraint_Error node. When the itype which uses these bounds
7743 -- is frozen the resulting call to Duplicate_Subexpr generates
7744 -- a new temporary for the bounds.
7746 -- Unfortunately there are other itypes that are also made depend
7747 -- on these bounds, so when Duplicate_Subexpr is called they get
7748 -- a forward reference to the newly created temporaries and Gigi
7749 -- aborts on such forward references. This is probably sign of a
7750 -- more fundamental problem somewhere else in either the order of
7751 -- itype freezing or the way certain itypes are constructed.
7753 -- To get around this problem we call Remove_Side_Effects right
7754 -- away if either bounds of R are a Constraint_Error.
7756 declare
7757 L : constant Node_Id := Low_Bound (R);
7758 H : constant Node_Id := High_Bound (R);
7760 begin
7761 if Nkind (L) = N_Raise_Constraint_Error then
7762 Remove_Side_Effects (L);
7763 end if;
7765 if Nkind (H) = N_Raise_Constraint_Error then
7766 Remove_Side_Effects (H);
7767 end if;
7768 end;
7770 Check_Unset_Reference (Low_Bound (R));
7771 Check_Unset_Reference (High_Bound (R));
7772 end if;
7773 end if;
7774 end Resolve_Discrete_Subtype_Indication;
7776 -------------------------
7777 -- Resolve_Entity_Name --
7778 -------------------------
7780 -- Used to resolve identifiers and expanded names
7782 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7783 function Is_Assignment_Or_Object_Expression
7784 (Context : Node_Id;
7785 Expr : Node_Id) return Boolean;
7786 -- Determine whether node Context denotes an assignment statement or an
7787 -- object declaration whose expression is node Expr.
7789 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7790 -- Determine whether Expr is part of an N_Attribute_Reference
7791 -- expression.
7793 ----------------------------------------
7794 -- Is_Assignment_Or_Object_Expression --
7795 ----------------------------------------
7797 function Is_Assignment_Or_Object_Expression
7798 (Context : Node_Id;
7799 Expr : Node_Id) return Boolean
7801 begin
7802 if Nkind (Context) in N_Assignment_Statement | N_Object_Declaration
7803 and then Expression (Context) = Expr
7804 then
7805 return True;
7807 -- Check whether a construct that yields a name is the expression of
7808 -- an assignment statement or an object declaration.
7810 elsif (Nkind (Context) in N_Attribute_Reference
7811 | N_Explicit_Dereference
7812 | N_Indexed_Component
7813 | N_Selected_Component
7814 | N_Slice
7815 and then Prefix (Context) = Expr)
7816 or else
7817 (Nkind (Context) in N_Type_Conversion
7818 | N_Unchecked_Type_Conversion
7819 and then Expression (Context) = Expr)
7820 then
7821 return
7822 Is_Assignment_Or_Object_Expression
7823 (Context => Parent (Context),
7824 Expr => Context);
7826 -- Otherwise the context is not an assignment statement or an object
7827 -- declaration.
7829 else
7830 return False;
7831 end if;
7832 end Is_Assignment_Or_Object_Expression;
7834 -----------------------------
7835 -- Is_Attribute_Expression --
7836 -----------------------------
7838 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
7839 N : Node_Id := Expr;
7840 begin
7841 while Present (N) loop
7842 if Nkind (N) = N_Attribute_Reference then
7843 return True;
7845 -- Prevent the search from going too far
7847 elsif Is_Body_Or_Package_Declaration (N) then
7848 return False;
7849 end if;
7851 N := Parent (N);
7852 end loop;
7854 return False;
7855 end Is_Attribute_Expression;
7857 -- Local variables
7859 E : constant Entity_Id := Entity (N);
7860 Par : Node_Id;
7862 -- Start of processing for Resolve_Entity_Name
7864 begin
7865 -- If garbage from errors, set to Any_Type and return
7867 if No (E) and then Total_Errors_Detected /= 0 then
7868 Set_Etype (N, Any_Type);
7869 return;
7870 end if;
7872 -- Replace named numbers by corresponding literals. Note that this is
7873 -- the one case where Resolve_Entity_Name must reset the Etype, since
7874 -- it is currently marked as universal.
7876 if Ekind (E) = E_Named_Integer then
7877 Set_Etype (N, Typ);
7878 Eval_Named_Integer (N);
7880 elsif Ekind (E) = E_Named_Real then
7881 Set_Etype (N, Typ);
7882 Eval_Named_Real (N);
7884 -- For enumeration literals, we need to make sure that a proper style
7885 -- check is done, since such literals are overloaded, and thus we did
7886 -- not do a style check during the first phase of analysis.
7888 elsif Ekind (E) = E_Enumeration_Literal then
7889 Set_Entity_With_Checks (N, E);
7890 Eval_Entity_Name (N);
7892 -- Case of (sub)type name appearing in a context where an expression
7893 -- is expected. This is legal if occurrence is a current instance.
7894 -- See RM 8.6 (17/3). It is also legal if the expression is
7895 -- part of a choice pattern for a case stmt/expr having a
7896 -- non-discrete selecting expression.
7898 elsif Is_Type (E) then
7899 if Is_Current_Instance (N) or else Is_Case_Choice_Pattern (N) then
7900 null;
7902 -- Any other use is an error
7904 else
7905 Error_Msg_N
7906 ("invalid use of subtype mark in expression or call", N);
7907 end if;
7909 -- Check discriminant use if entity is discriminant in current scope,
7910 -- i.e. discriminant of record or concurrent type currently being
7911 -- analyzed. Uses in corresponding body are unrestricted.
7913 elsif Ekind (E) = E_Discriminant
7914 and then Scope (E) = Current_Scope
7915 and then not Has_Completion (Current_Scope)
7916 then
7917 Check_Discriminant_Use (N);
7919 -- A parameterless generic function cannot appear in a context that
7920 -- requires resolution.
7922 elsif Ekind (E) = E_Generic_Function then
7923 Error_Msg_N ("illegal use of generic function", N);
7925 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7926 -- array types (i.e. bounds and length) are legal.
7928 elsif Ekind (E) = E_Out_Parameter
7929 and then (Is_Scalar_Type (Etype (E))
7930 or else not Is_Attribute_Expression (Parent (N)))
7932 and then (Nkind (Parent (N)) in N_Op
7933 or else Nkind (Parent (N)) = N_Explicit_Dereference
7934 or else Is_Assignment_Or_Object_Expression
7935 (Context => Parent (N),
7936 Expr => N))
7937 then
7938 if Ada_Version = Ada_83 then
7939 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7940 end if;
7942 -- In all other cases, just do the possible static evaluation
7944 else
7945 -- A deferred constant that appears in an expression must have a
7946 -- completion, unless it has been removed by in-place expansion of
7947 -- an aggregate. A constant that is a renaming does not need
7948 -- initialization.
7950 if Ekind (E) = E_Constant
7951 and then Comes_From_Source (E)
7952 and then No (Constant_Value (E))
7953 and then Is_Frozen (Etype (E))
7954 and then not In_Spec_Expression
7955 and then not Is_Imported (E)
7956 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7957 then
7958 if No_Initialization (Parent (E))
7959 or else (Present (Full_View (E))
7960 and then No_Initialization (Parent (Full_View (E))))
7961 then
7962 null;
7963 else
7964 Error_Msg_N
7965 ("deferred constant is frozen before completion", N);
7966 end if;
7967 end if;
7969 Eval_Entity_Name (N);
7970 end if;
7972 Par := Parent (N);
7974 -- When the entity appears in a parameter association, retrieve the
7975 -- related subprogram call.
7977 if Nkind (Par) = N_Parameter_Association then
7978 Par := Parent (Par);
7979 end if;
7981 if Comes_From_Source (N) then
7983 -- The following checks are only relevant when SPARK_Mode is On as
7984 -- they are not standard Ada legality rules.
7986 if SPARK_Mode = On then
7988 -- Check for possible elaboration issues with respect to reads of
7989 -- variables. The act of renaming the variable is not considered a
7990 -- read as it simply establishes an alias.
7992 if Legacy_Elaboration_Checks
7993 and then Ekind (E) = E_Variable
7994 and then Dynamic_Elaboration_Checks
7995 and then Nkind (Par) /= N_Object_Renaming_Declaration
7996 then
7997 Check_Elab_Call (N);
7998 end if;
7999 end if;
8001 -- The variable may eventually become a constituent of a single
8002 -- protected/task type. Record the reference now and verify its
8003 -- legality when analyzing the contract of the variable
8004 -- (SPARK RM 9.3).
8006 if Ekind (E) = E_Variable then
8007 Record_Possible_Part_Of_Reference (E, N);
8008 end if;
8010 -- A Ghost entity must appear in a specific context
8012 if Is_Ghost_Entity (E) then
8013 Check_Ghost_Context (E, N);
8014 end if;
8016 -- We may be resolving an entity within expanded code, so a reference
8017 -- to an entity should be ignored when calculating effective use
8018 -- clauses to avoid inappropriate marking.
8020 Mark_Use_Clauses (E);
8021 end if;
8022 end Resolve_Entity_Name;
8024 -------------------
8025 -- Resolve_Entry --
8026 -------------------
8028 procedure Resolve_Entry (Entry_Name : Node_Id) is
8029 Loc : constant Source_Ptr := Sloc (Entry_Name);
8030 Nam : Entity_Id;
8031 New_N : Node_Id;
8032 S : Entity_Id;
8033 Tsk : Entity_Id;
8034 E_Name : Node_Id;
8035 Index : Node_Id;
8037 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
8038 -- If the bounds of the entry family being called depend on task
8039 -- discriminants, build a new index subtype where a discriminant is
8040 -- replaced with the value of the discriminant of the target task.
8041 -- The target task is the prefix of the entry name in the call.
8043 -----------------------
8044 -- Actual_Index_Type --
8045 -----------------------
8047 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
8048 Typ : constant Entity_Id := Entry_Index_Type (E);
8049 Tsk : constant Entity_Id := Scope (E);
8050 Lo : constant Node_Id := Type_Low_Bound (Typ);
8051 Hi : constant Node_Id := Type_High_Bound (Typ);
8052 New_T : Entity_Id;
8054 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8055 -- If the bound is given by a discriminant, replace with a reference
8056 -- to the discriminant of the same name in the target task. If the
8057 -- entry name is the target of a requeue statement and the entry is
8058 -- in the current protected object, the bound to be used is the
8059 -- discriminal of the object (see Apply_Range_Check for details of
8060 -- the transformation).
8062 -----------------------------
8063 -- Actual_Discriminant_Ref --
8064 -----------------------------
8066 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8067 Typ : constant Entity_Id := Etype (Bound);
8068 Ref : Node_Id;
8070 begin
8071 Remove_Side_Effects (Bound);
8073 if not Is_Entity_Name (Bound)
8074 or else Ekind (Entity (Bound)) /= E_Discriminant
8075 then
8076 return Bound;
8078 elsif Is_Protected_Type (Tsk)
8079 and then In_Open_Scopes (Tsk)
8080 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
8081 then
8082 -- Note: here Bound denotes a discriminant of the corresponding
8083 -- record type tskV, whose discriminal is a formal of the
8084 -- init-proc tskVIP. What we want is the body discriminal,
8085 -- which is associated to the discriminant of the original
8086 -- concurrent type tsk.
8088 return New_Occurrence_Of
8089 (Find_Body_Discriminal (Entity (Bound)), Loc);
8091 else
8092 Ref :=
8093 Make_Selected_Component (Loc,
8094 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8095 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8096 Analyze (Ref);
8097 Resolve (Ref, Typ);
8098 return Ref;
8099 end if;
8100 end Actual_Discriminant_Ref;
8102 -- Start of processing for Actual_Index_Type
8104 begin
8105 if not Has_Discriminants (Tsk)
8106 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8107 then
8108 return Entry_Index_Type (E);
8110 else
8111 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8112 Set_Etype (New_T, Base_Type (Typ));
8113 Set_Size_Info (New_T, Typ);
8114 Set_RM_Size (New_T, RM_Size (Typ));
8115 Set_Scalar_Range (New_T,
8116 Make_Range (Sloc (Entry_Name),
8117 Low_Bound => Actual_Discriminant_Ref (Lo),
8118 High_Bound => Actual_Discriminant_Ref (Hi)));
8120 return New_T;
8121 end if;
8122 end Actual_Index_Type;
8124 -- Start of processing for Resolve_Entry
8126 begin
8127 -- Find name of entry being called, and resolve prefix of name with its
8128 -- own type. The prefix can be overloaded, and the name and signature of
8129 -- the entry must be taken into account.
8131 if Nkind (Entry_Name) = N_Indexed_Component then
8133 -- Case of dealing with entry family within the current tasks
8135 E_Name := Prefix (Entry_Name);
8137 else
8138 E_Name := Entry_Name;
8139 end if;
8141 if Is_Entity_Name (E_Name) then
8143 -- Entry call to an entry (or entry family) in the current task. This
8144 -- is legal even though the task will deadlock. Rewrite as call to
8145 -- current task.
8147 -- This can also be a call to an entry in an enclosing task. If this
8148 -- is a single task, we have to retrieve its name, because the scope
8149 -- of the entry is the task type, not the object. If the enclosing
8150 -- task is a task type, the identity of the task is given by its own
8151 -- self variable.
8153 -- Finally this can be a requeue on an entry of the same task or
8154 -- protected object.
8156 S := Scope (Entity (E_Name));
8158 for J in reverse 0 .. Scope_Stack.Last loop
8159 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8160 and then not Comes_From_Source (S)
8161 then
8162 -- S is an enclosing task or protected object. The concurrent
8163 -- declaration has been converted into a type declaration, and
8164 -- the object itself has an object declaration that follows
8165 -- the type in the same declarative part.
8167 Tsk := Next_Entity (S);
8168 while Etype (Tsk) /= S loop
8169 Next_Entity (Tsk);
8170 end loop;
8172 S := Tsk;
8173 exit;
8175 elsif S = Scope_Stack.Table (J).Entity then
8177 -- Call to current task. Will be transformed into call to Self
8179 exit;
8181 end if;
8182 end loop;
8184 New_N :=
8185 Make_Selected_Component (Loc,
8186 Prefix => New_Occurrence_Of (S, Loc),
8187 Selector_Name =>
8188 New_Occurrence_Of (Entity (E_Name), Loc));
8189 Rewrite (E_Name, New_N);
8190 Analyze (E_Name);
8192 elsif Nkind (Entry_Name) = N_Selected_Component
8193 and then Is_Overloaded (Prefix (Entry_Name))
8194 then
8195 -- Use the entry name (which must be unique at this point) to find
8196 -- the prefix that returns the corresponding task/protected type.
8198 declare
8199 Pref : constant Node_Id := Prefix (Entry_Name);
8200 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8201 I : Interp_Index;
8202 It : Interp;
8204 begin
8205 Get_First_Interp (Pref, I, It);
8206 while Present (It.Typ) loop
8207 if Scope (Ent) = It.Typ then
8208 Set_Etype (Pref, It.Typ);
8209 exit;
8210 end if;
8212 Get_Next_Interp (I, It);
8213 end loop;
8214 end;
8215 end if;
8217 if Nkind (Entry_Name) = N_Selected_Component then
8218 Resolve (Prefix (Entry_Name));
8219 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8221 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8222 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8223 Resolve (Prefix (Prefix (Entry_Name)));
8224 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8226 -- We do not resolve the prefix because an Entry_Family has no type,
8227 -- although it has the semantics of an array since it can be indexed.
8228 -- In order to perform the associated range check, we would need to
8229 -- build an array type on the fly and set it on the prefix, but this
8230 -- would be wasteful since only the index type matters. Therefore we
8231 -- attach this index type directly, so that Actual_Index_Expression
8232 -- can pick it up later in order to generate the range check.
8234 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8236 Index := First (Expressions (Entry_Name));
8237 Resolve (Index, Entry_Index_Type (Nam));
8239 -- Generate a reference for the index when it denotes an entity
8241 if Is_Entity_Name (Index) then
8242 Generate_Reference (Entity (Index), Nam);
8243 end if;
8245 -- Up to this point the expression could have been the actual in a
8246 -- simple entry call, and be given by a named association.
8248 if Nkind (Index) = N_Parameter_Association then
8249 Error_Msg_N ("expect expression for entry index", Index);
8250 else
8251 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8252 end if;
8253 end if;
8254 end Resolve_Entry;
8256 ------------------------
8257 -- Resolve_Entry_Call --
8258 ------------------------
8260 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8261 Entry_Name : constant Node_Id := Name (N);
8262 Loc : constant Source_Ptr := Sloc (Entry_Name);
8264 Nam : Entity_Id;
8265 Norm_OK : Boolean;
8266 Obj : Node_Id;
8267 Was_Over : Boolean;
8269 begin
8270 -- We kill all checks here, because it does not seem worth the effort to
8271 -- do anything better, an entry call is a big operation.
8273 Kill_All_Checks;
8275 -- Processing of the name is similar for entry calls and protected
8276 -- operation calls. Once the entity is determined, we can complete
8277 -- the resolution of the actuals.
8279 -- The selector may be overloaded, in the case of a protected object
8280 -- with overloaded functions. The type of the context is used for
8281 -- resolution.
8283 if Nkind (Entry_Name) = N_Selected_Component
8284 and then Is_Overloaded (Selector_Name (Entry_Name))
8285 and then Typ /= Standard_Void_Type
8286 then
8287 declare
8288 I : Interp_Index;
8289 It : Interp;
8291 begin
8292 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8293 while Present (It.Typ) loop
8294 if Covers (Typ, It.Typ) then
8295 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8296 Set_Etype (Entry_Name, It.Typ);
8298 Generate_Reference (It.Typ, N, ' ');
8299 end if;
8301 Get_Next_Interp (I, It);
8302 end loop;
8303 end;
8304 end if;
8306 Resolve_Entry (Entry_Name);
8308 if Nkind (Entry_Name) = N_Selected_Component then
8310 -- Simple entry or protected operation call
8312 Nam := Entity (Selector_Name (Entry_Name));
8313 Obj := Prefix (Entry_Name);
8315 if Is_Subprogram (Nam) then
8316 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8317 end if;
8319 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8321 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8323 -- Call to member of entry family
8325 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8326 Obj := Prefix (Prefix (Entry_Name));
8327 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8328 end if;
8330 -- We cannot in general check the maximum depth of protected entry calls
8331 -- at compile time. But we can tell that any protected entry call at all
8332 -- violates a specified nesting depth of zero.
8334 if Is_Protected_Type (Scope (Nam)) then
8335 Check_Restriction (Max_Entry_Queue_Length, N);
8336 end if;
8338 -- Use context type to disambiguate a protected function that can be
8339 -- called without actuals and that returns an array type, and where the
8340 -- argument list may be an indexing of the returned value.
8342 if Ekind (Nam) = E_Function
8343 and then Needs_No_Actuals (Nam)
8344 and then Present (Parameter_Associations (N))
8345 and then
8346 ((Is_Array_Type (Etype (Nam))
8347 and then Covers (Typ, Component_Type (Etype (Nam))))
8349 or else (Is_Access_Type (Etype (Nam))
8350 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8351 and then
8352 Covers
8353 (Typ,
8354 Component_Type (Designated_Type (Etype (Nam))))))
8355 then
8356 declare
8357 Index_Node : Node_Id;
8359 begin
8360 Index_Node :=
8361 Make_Indexed_Component (Loc,
8362 Prefix =>
8363 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8364 Expressions => Parameter_Associations (N));
8366 -- Since we are correcting a node classification error made by the
8367 -- parser, we call Replace rather than Rewrite.
8369 Replace (N, Index_Node);
8370 Set_Etype (Prefix (N), Etype (Nam));
8371 Set_Etype (N, Typ);
8372 Resolve_Indexed_Component (N, Typ);
8373 return;
8374 end;
8375 end if;
8377 if Is_Entry (Nam)
8378 and then Present (Contract_Wrapper (Nam))
8379 and then Current_Scope /= Contract_Wrapper (Nam)
8380 and then Current_Scope /= Wrapped_Statements (Contract_Wrapper (Nam))
8381 then
8382 -- Note the entity being called before rewriting the call, so that
8383 -- it appears used at this point.
8385 Generate_Reference (Nam, Entry_Name, 'r');
8387 -- Rewrite as call to the precondition wrapper, adding the task
8388 -- object to the list of actuals. If the call is to a member of an
8389 -- entry family, include the index as well.
8391 declare
8392 New_Call : Node_Id;
8393 New_Actuals : List_Id;
8395 begin
8396 New_Actuals := New_List (Obj);
8398 if Nkind (Entry_Name) = N_Indexed_Component then
8399 Append_To (New_Actuals,
8400 New_Copy_Tree (First (Expressions (Entry_Name))));
8401 end if;
8403 Append_List (Parameter_Associations (N), New_Actuals);
8404 New_Call :=
8405 Make_Procedure_Call_Statement (Loc,
8406 Name =>
8407 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8408 Parameter_Associations => New_Actuals);
8409 Rewrite (N, New_Call);
8411 -- Preanalyze and resolve new call. Current procedure is called
8412 -- from Resolve_Call, after which expansion will take place.
8414 Preanalyze_And_Resolve (N);
8415 return;
8416 end;
8417 end if;
8419 -- The operation name may have been overloaded. Order the actuals
8420 -- according to the formals of the resolved entity, and set the return
8421 -- type to that of the operation.
8423 if Was_Over then
8424 Normalize_Actuals (N, Nam, False, Norm_OK);
8425 pragma Assert (Norm_OK);
8426 Set_Etype (N, Etype (Nam));
8428 -- Reset the Is_Overloaded flag, since resolution is now completed
8430 -- Simple entry call
8432 if Nkind (Entry_Name) = N_Selected_Component then
8433 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8435 -- Call to a member of an entry family
8437 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8438 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8439 end if;
8440 end if;
8442 Resolve_Actuals (N, Nam);
8443 Check_Internal_Protected_Use (N, Nam);
8445 -- Create a call reference to the entry
8447 Generate_Reference (Nam, Entry_Name, 's');
8449 if Is_Entry (Nam) then
8450 Check_Potentially_Blocking_Operation (N);
8451 end if;
8453 -- Verify that a procedure call cannot masquerade as an entry
8454 -- call where an entry call is expected.
8456 if Ekind (Nam) = E_Procedure then
8457 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8458 and then N = Entry_Call_Statement (Parent (N))
8459 then
8460 Error_Msg_N ("entry call required in select statement", N);
8462 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8463 and then N = Triggering_Statement (Parent (N))
8464 then
8465 Error_Msg_N ("triggering statement cannot be procedure call", N);
8467 elsif Ekind (Scope (Nam)) = E_Task_Type
8468 and then not In_Open_Scopes (Scope (Nam))
8469 then
8470 Error_Msg_N ("task has no entry with this name", Entry_Name);
8471 end if;
8472 end if;
8474 -- After resolution, entry calls and protected procedure calls are
8475 -- changed into entry calls, for expansion. The structure of the node
8476 -- does not change, so it can safely be done in place. Protected
8477 -- function calls must keep their structure because they are
8478 -- subexpressions.
8480 if Ekind (Nam) /= E_Function then
8482 -- A protected operation that is not a function may modify the
8483 -- corresponding object, and cannot apply to a constant. If this
8484 -- is an internal call, the prefix is the type itself.
8486 if Is_Protected_Type (Scope (Nam))
8487 and then not Is_Variable (Obj)
8488 and then (not Is_Entity_Name (Obj)
8489 or else not Is_Type (Entity (Obj)))
8490 then
8491 Error_Msg_N
8492 ("prefix of protected procedure or entry call must be variable",
8493 Entry_Name);
8494 end if;
8496 declare
8497 Entry_Call : Node_Id;
8499 begin
8500 Entry_Call :=
8501 Make_Entry_Call_Statement (Loc,
8502 Name => Entry_Name,
8503 Parameter_Associations => Parameter_Associations (N));
8505 -- Inherit relevant attributes from the original call
8507 Set_First_Named_Actual
8508 (Entry_Call, First_Named_Actual (N));
8510 Set_Is_Elaboration_Checks_OK_Node
8511 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8513 Set_Is_Elaboration_Warnings_OK_Node
8514 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8516 Set_Is_SPARK_Mode_On_Node
8517 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8519 Rewrite (N, Entry_Call);
8520 Set_Analyzed (N, True);
8521 end;
8523 -- Protected functions can return on the secondary stack, in which case
8524 -- we must trigger the transient scope mechanism.
8526 elsif Expander_Active
8527 and then Requires_Transient_Scope (Etype (Nam))
8528 then
8529 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
8530 end if;
8532 -- Now we know that this is not a call to a function that returns an
8533 -- array type; moreover, we know the name of the called entry. Detect
8534 -- overlapping actuals, just like for a subprogram call.
8536 Warn_On_Overlapping_Actuals (Nam, N);
8537 end Resolve_Entry_Call;
8539 -------------------------
8540 -- Resolve_Equality_Op --
8541 -------------------------
8543 -- The operands must have compatible types and the boolean context does not
8544 -- participate in the resolution. The first pass verifies that the operands
8545 -- are not ambiguous and sets their type correctly, or to Any_Type in case
8546 -- of ambiguity. If both operands are strings, aggregates, allocators, or
8547 -- null, they are ambiguous even if they carry a single (universal) type.
8549 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8550 L : constant Node_Id := Left_Opnd (N);
8551 R : constant Node_Id := Right_Opnd (N);
8553 Implicit_NE_For_User_Defined_Operator : constant Boolean :=
8554 Nkind (N) = N_Op_Ne
8555 and then Ekind (Entity (N)) = E_Function
8556 and then not Comes_From_Source (Entity (N))
8557 and then not
8558 Is_Intrinsic_Subprogram (Corresponding_Equality (Entity (N)));
8559 -- Whether this is a call to the implicit inequality operator created
8560 -- for a user-defined operator that is not an intrinsic subprogram, in
8561 -- which case we need to skip some processing.
8563 T : Entity_Id := Find_Unique_Type (L, R);
8565 procedure Check_Access_Attribute (N : Node_Id);
8566 -- For any object, '[Unchecked_]Access of such object can never be
8567 -- passed as an operand to the Universal_Access equality operators.
8568 -- This is so because the expected type for Obj'Access in a call to
8569 -- these operators, whose formals are of type Universal_Access, is
8570 -- Universal_Access, and Universal_Access does not have a designated
8571 -- type. For more details, see RM 3.10.2(2/2) and 6.4.1(3).
8573 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id);
8574 -- Check RM 4.5.2(9.6/2) on the given designated object types
8576 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id);
8577 -- Check RM 4.5.2(9.7/2) on the given designated subprogram types
8579 procedure Check_If_Expression (Cond : Node_Id);
8580 -- The resolution rule for if expressions requires that each such must
8581 -- have a unique type. This means that if several dependent expressions
8582 -- are of a non-null anonymous access type, and the context does not
8583 -- impose an expected type (as can be the case in an equality operation)
8584 -- the expression must be rejected.
8586 procedure Explain_Redundancy (N : Node_Id);
8587 -- Attempt to explain the nature of a redundant comparison with True. If
8588 -- the expression N is too complex, this routine issues a general error
8589 -- message.
8591 function Find_Unique_Access_Type return Entity_Id;
8592 -- In the case of allocators and access attributes, the context must
8593 -- provide an indication of the specific access type to be used. If
8594 -- one operand is of such a "generic" access type, check whether there
8595 -- is a specific visible access type that has the same designated type.
8596 -- This is semantically dubious, and of no interest to any real code,
8597 -- but c48008a makes it all worthwhile.
8599 function Suspicious_Prio_For_Equality return Boolean;
8600 -- Returns True iff the parent node is a and/or/xor operation that
8601 -- could be the cause of confused priorities. Note that if the not is
8602 -- in parens, then False is returned.
8604 ----------------------------
8605 -- Check_Access_Attribute --
8606 ----------------------------
8608 procedure Check_Access_Attribute (N : Node_Id) is
8609 begin
8610 if Nkind (N) = N_Attribute_Reference
8611 and then Attribute_Name (N) in Name_Access | Name_Unchecked_Access
8612 then
8613 Error_Msg_N
8614 ("access attribute cannot be used as actual for "
8615 & "universal_access equality", N);
8616 end if;
8617 end Check_Access_Attribute;
8619 -----------------------------------
8620 -- Check_Designated_Object_Types --
8621 -----------------------------------
8623 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id) is
8624 begin
8625 if (Is_Elementary_Type (T1) or else Is_Array_Type (T1))
8626 and then (Base_Type (T1) /= Base_Type (T2)
8627 or else not Subtypes_Statically_Match (T1, T2))
8628 then
8629 Error_Msg_N
8630 ("designated subtypes for universal_access equality "
8631 & "do not statically match (RM 4.5.2(9.6/2)", N);
8632 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8633 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8634 end if;
8635 end Check_Designated_Object_Types;
8637 ---------------------------------------
8638 -- Check_Designated_Subprogram_Types --
8639 ---------------------------------------
8641 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id) is
8642 begin
8643 if not Subtype_Conformant (T1, T2) then
8644 Error_Msg_N
8645 ("designated subtypes for universal_access equality "
8646 & "not subtype conformant (RM 4.5.2(9.7/2)", N);
8647 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8648 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8649 end if;
8650 end Check_Designated_Subprogram_Types;
8652 -------------------------
8653 -- Check_If_Expression --
8654 -------------------------
8656 procedure Check_If_Expression (Cond : Node_Id) is
8657 Then_Expr : Node_Id;
8658 Else_Expr : Node_Id;
8660 begin
8661 if Nkind (Cond) = N_If_Expression then
8662 Then_Expr := Next (First (Expressions (Cond)));
8663 Else_Expr := Next (Then_Expr);
8665 if Nkind (Then_Expr) /= N_Null
8666 and then Nkind (Else_Expr) /= N_Null
8667 then
8668 Error_Msg_N ("cannot determine type of if expression", Cond);
8669 end if;
8670 end if;
8671 end Check_If_Expression;
8673 ------------------------
8674 -- Explain_Redundancy --
8675 ------------------------
8677 procedure Explain_Redundancy (N : Node_Id) is
8678 Error : Name_Id;
8679 Val : Node_Id;
8680 Val_Id : Entity_Id;
8682 begin
8683 Val := N;
8685 -- Strip the operand down to an entity
8687 loop
8688 if Nkind (Val) = N_Selected_Component then
8689 Val := Selector_Name (Val);
8690 else
8691 exit;
8692 end if;
8693 end loop;
8695 -- The construct denotes an entity
8697 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8698 Val_Id := Entity (Val);
8700 -- Do not generate an error message when the comparison is done
8701 -- against the enumeration literal Standard.True.
8703 if Ekind (Val_Id) /= E_Enumeration_Literal then
8705 -- Build a customized error message
8707 Name_Len := 0;
8708 Add_Str_To_Name_Buffer ("?r?");
8710 if Ekind (Val_Id) = E_Component then
8711 Add_Str_To_Name_Buffer ("component ");
8713 elsif Ekind (Val_Id) = E_Constant then
8714 Add_Str_To_Name_Buffer ("constant ");
8716 elsif Ekind (Val_Id) = E_Discriminant then
8717 Add_Str_To_Name_Buffer ("discriminant ");
8719 elsif Is_Formal (Val_Id) then
8720 Add_Str_To_Name_Buffer ("parameter ");
8722 elsif Ekind (Val_Id) = E_Variable then
8723 Add_Str_To_Name_Buffer ("variable ");
8724 end if;
8726 Add_Str_To_Name_Buffer ("& is always True!");
8727 Error := Name_Find;
8729 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8730 end if;
8732 -- The construct is too complex to disect, issue a general message
8734 else
8735 Error_Msg_N ("?r?expression is always True!", Val);
8736 end if;
8737 end Explain_Redundancy;
8739 -----------------------------
8740 -- Find_Unique_Access_Type --
8741 -----------------------------
8743 function Find_Unique_Access_Type return Entity_Id is
8744 Acc : Entity_Id;
8745 E : Entity_Id;
8746 S : Entity_Id;
8748 begin
8749 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8750 then
8751 Acc := Designated_Type (Etype (R));
8753 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8754 then
8755 Acc := Designated_Type (Etype (L));
8756 else
8757 return Empty;
8758 end if;
8760 S := Current_Scope;
8761 while S /= Standard_Standard loop
8762 E := First_Entity (S);
8763 while Present (E) loop
8764 if Is_Type (E)
8765 and then Is_Access_Type (E)
8766 and then Ekind (E) /= E_Allocator_Type
8767 and then Designated_Type (E) = Base_Type (Acc)
8768 then
8769 return E;
8770 end if;
8772 Next_Entity (E);
8773 end loop;
8775 S := Scope (S);
8776 end loop;
8778 return Empty;
8779 end Find_Unique_Access_Type;
8781 ----------------------------------
8782 -- Suspicious_Prio_For_Equality --
8783 ----------------------------------
8785 function Suspicious_Prio_For_Equality return Boolean is
8786 Par : constant Node_Id := Parent (N);
8788 begin
8789 -- Check if parent node is one of and/or/xor, not parenthesized
8790 -- explicitly, and its own parent is not of this kind. Otherwise,
8791 -- it's a case of chained Boolean conditions which is likely well
8792 -- parenthesized.
8794 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8795 and then Paren_Count (N) = 0
8796 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8797 then
8798 declare
8799 Compar : Node_Id :=
8800 (if Left_Opnd (Par) = N then
8801 Right_Opnd (Par)
8802 else
8803 Left_Opnd (Par));
8804 begin
8805 -- Compar may have been rewritten, for example from (a /= b)
8806 -- into not (a = b). Use the Original_Node instead.
8808 Compar := Original_Node (Compar);
8810 -- If the other argument of the and/or/xor is also a
8811 -- comparison, or another and/or/xor then most likely
8812 -- the priorities are correctly set.
8814 return Nkind (Compar) not in N_Op_Boolean;
8815 end;
8817 else
8818 return False;
8819 end if;
8820 end Suspicious_Prio_For_Equality;
8822 -- Start of processing for Resolve_Equality_Op
8824 begin
8825 if T = Any_Fixed then
8826 T := Unique_Fixed_Point_Type (L);
8827 end if;
8829 Set_Etype (N, Base_Type (Typ));
8830 Generate_Reference (T, N, ' ');
8832 if T = Any_Type then
8833 -- Deal with explicit ambiguity of operands, unless this is a call
8834 -- to the implicit inequality operator created for a user-defined
8835 -- operator that is not an intrinsic subprogram, since the common
8836 -- resolution of operands done here does not apply to it.
8838 if not Implicit_NE_For_User_Defined_Operator
8839 and then (Is_Overloaded (L) or else Is_Overloaded (R))
8840 then
8841 Ambiguous_Operands (N);
8842 end if;
8844 else
8846 -- For Ada 2022, check for user-defined literals when the type has
8847 -- the appropriate aspect.
8849 if Has_Applicable_User_Defined_Literal (L, Etype (R)) then
8850 Resolve (L, Etype (R));
8851 Set_Etype (N, Standard_Boolean);
8852 end if;
8854 if Has_Applicable_User_Defined_Literal (R, Etype (L)) then
8855 Resolve (R, Etype (L));
8856 Set_Etype (N, Standard_Boolean);
8857 end if;
8859 -- Deal with other error cases
8861 if T = Any_String or else
8862 T = Any_Composite or else
8863 T = Any_Character
8864 then
8865 if T = Any_Character then
8866 Ambiguous_Character (L);
8867 else
8868 Error_Msg_N ("ambiguous operands for equality", N);
8869 end if;
8871 Set_Etype (N, Any_Type);
8872 return;
8874 elsif T = Universal_Access
8875 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
8876 then
8877 T := Find_Unique_Access_Type;
8879 if No (T) then
8880 Error_Msg_N ("ambiguous operands for equality", N);
8881 Set_Etype (N, Any_Type);
8882 return;
8883 end if;
8885 -- If expressions must have a single type, and if the context does
8886 -- not impose one the dependent expressions cannot be anonymous
8887 -- access types.
8889 -- Why no similar processing for case expressions???
8891 elsif Ada_Version >= Ada_2012
8892 and then Is_Anonymous_Access_Type (Etype (L))
8893 and then Is_Anonymous_Access_Type (Etype (R))
8894 then
8895 Check_If_Expression (L);
8896 Check_If_Expression (R);
8897 end if;
8899 -- RM 4.5.2(9.5/2): At least one of the operands of the equality
8900 -- operators for universal_access shall be of type universal_access,
8901 -- or both shall be of access-to-object types, or both shall be of
8902 -- access-to-subprogram types (RM 4.5.2(9.5/2)).
8904 if Is_Anonymous_Access_Type (T)
8905 and then Etype (L) /= Universal_Access
8906 and then Etype (R) /= Universal_Access
8907 then
8908 -- RM 4.5.2(9.6/2): When both are of access-to-object types, the
8909 -- designated types shall be the same or one shall cover the other
8910 -- and if the designated types are elementary or array types, then
8911 -- the designated subtypes shall statically match.
8913 if Is_Access_Object_Type (Etype (L))
8914 and then Is_Access_Object_Type (Etype (R))
8915 then
8916 Check_Designated_Object_Types
8917 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
8919 -- RM 4.5.2(9.7/2): When both are of access-to-subprogram types,
8920 -- the designated profiles shall be subtype conformant.
8922 elsif Is_Access_Subprogram_Type (Etype (L))
8923 and then Is_Access_Subprogram_Type (Etype (R))
8924 then
8925 Check_Designated_Subprogram_Types
8926 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
8927 end if;
8928 end if;
8930 -- Check another case of equality operators for universal_access
8932 if Is_Anonymous_Access_Type (T) and then Comes_From_Source (N) then
8933 Check_Access_Attribute (L);
8934 Check_Access_Attribute (R);
8935 end if;
8937 Resolve (L, T);
8938 Resolve (R, T);
8939 Set_Compare_Type (N, T);
8941 -- AI12-0413: user-defined primitive equality of an untagged record
8942 -- type hides the predefined equality operator, including within a
8943 -- generic, and if it is declared abstract, results in an illegal
8944 -- instance if the operator is used in the spec, or in the raising
8945 -- of Program_Error if used in the body of an instance.
8947 if Nkind (N) = N_Op_Eq
8948 and then In_Instance
8949 and then Ada_Version >= Ada_2012
8950 then
8951 declare
8952 U : constant Entity_Id := Underlying_Type (T);
8954 Eq : Entity_Id;
8956 begin
8957 if Present (U)
8958 and then Is_Record_Type (U)
8959 and then not Is_Tagged_Type (U)
8960 then
8961 Eq := Get_User_Defined_Equality (T);
8963 if Present (Eq) then
8964 if Is_Abstract_Subprogram (Eq) then
8965 Nondispatching_Call_To_Abstract_Operation (N, Eq);
8966 else
8967 Rewrite_Operator_As_Call (N, Eq);
8968 end if;
8970 return;
8971 end if;
8972 end if;
8973 end;
8974 end if;
8976 -- If the unique type is a class-wide type then it will be expanded
8977 -- into a dispatching call to the predefined primitive. Therefore we
8978 -- check here for potential violation of such restriction.
8980 if Is_Class_Wide_Type (T) then
8981 Check_Restriction (No_Dispatching_Calls, N);
8982 end if;
8984 -- Only warn for redundant equality comparison to True for objects
8985 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
8986 -- other expressions, it may be a matter of preference to write
8987 -- "Expr = True" or "Expr".
8989 if Warn_On_Redundant_Constructs
8990 and then Comes_From_Source (N)
8991 and then Comes_From_Source (R)
8992 and then Is_Entity_Name (R)
8993 and then Entity (R) = Standard_True
8994 and then
8995 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
8996 or else
8997 Nkind (L) in N_Op)
8998 then
8999 Error_Msg_N -- CODEFIX
9000 ("?r?comparison with True is redundant!", N);
9001 Explain_Redundancy (Original_Node (R));
9002 end if;
9004 -- Warn on a (in)equality between boolean values which is not
9005 -- parenthesized when the parent expression is one of and/or/xor, as
9006 -- this is interpreted as (a = b) op c where most likely a = (b op c)
9007 -- was intended. Do not generate a warning in generic instances, as
9008 -- the problematic expression may be implicitly parenthesized in
9009 -- the generic itself if one of the operators is a generic formal.
9010 -- Also do not generate a warning for generated equality, for
9011 -- example from rewritting a membership test.
9013 if Warn_On_Questionable_Missing_Parens
9014 and then not In_Instance
9015 and then Comes_From_Source (N)
9016 and then Is_Boolean_Type (T)
9017 and then Suspicious_Prio_For_Equality
9018 then
9019 Error_Msg_N ("?q?equality should be parenthesized here!", N);
9020 end if;
9022 Check_Unset_Reference (L);
9023 Check_Unset_Reference (R);
9024 Generate_Operator_Reference (N, T);
9025 Check_Low_Bound_Tested (N);
9027 -- Unless this is a call to the implicit inequality operator created
9028 -- for a user-defined operator that is not an intrinsic subprogram,
9029 -- try to fold the operation.
9031 if not Implicit_NE_For_User_Defined_Operator then
9032 Analyze_Dimension (N);
9033 Eval_Relational_Op (N);
9035 elsif Nkind (N) = N_Op_Ne
9036 and then Is_Abstract_Subprogram (Entity (N))
9037 then
9038 Nondispatching_Call_To_Abstract_Operation (N, Entity (N));
9039 end if;
9040 end if;
9041 end Resolve_Equality_Op;
9043 ----------------------------------
9044 -- Resolve_Explicit_Dereference --
9045 ----------------------------------
9047 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
9048 Loc : constant Source_Ptr := Sloc (N);
9049 New_N : Node_Id;
9050 P : constant Node_Id := Prefix (N);
9052 P_Typ : Entity_Id;
9053 -- The candidate prefix type, if overloaded
9055 I : Interp_Index;
9056 It : Interp;
9058 begin
9059 Check_Fully_Declared_Prefix (Typ, P);
9060 P_Typ := Empty;
9062 -- A useful optimization: check whether the dereference denotes an
9063 -- element of a container, and if so rewrite it as a call to the
9064 -- corresponding Element function.
9066 -- Disabled for now, on advice of ARG. A more restricted form of the
9067 -- predicate might be acceptable ???
9069 -- if Is_Container_Element (N) then
9070 -- return;
9071 -- end if;
9073 if Is_Overloaded (P) then
9075 -- Use the context type to select the prefix that has the correct
9076 -- designated type. Keep the first match, which will be the inner-
9077 -- most.
9079 Get_First_Interp (P, I, It);
9081 while Present (It.Typ) loop
9082 if Is_Access_Type (It.Typ)
9083 and then Covers (Typ, Designated_Type (It.Typ))
9084 then
9085 if No (P_Typ) then
9086 P_Typ := It.Typ;
9087 end if;
9089 -- Remove access types that do not match, but preserve access
9090 -- to subprogram interpretations, in case a further dereference
9091 -- is needed (see below).
9093 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
9094 Remove_Interp (I);
9095 end if;
9097 Get_Next_Interp (I, It);
9098 end loop;
9100 if Present (P_Typ) then
9101 Resolve (P, P_Typ);
9102 Set_Etype (N, Designated_Type (P_Typ));
9104 else
9105 -- If no interpretation covers the designated type of the prefix,
9106 -- this is the pathological case where not all implementations of
9107 -- the prefix allow the interpretation of the node as a call. Now
9108 -- that the expected type is known, Remove other interpretations
9109 -- from prefix, rewrite it as a call, and resolve again, so that
9110 -- the proper call node is generated.
9112 Get_First_Interp (P, I, It);
9113 while Present (It.Typ) loop
9114 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
9115 Remove_Interp (I);
9116 end if;
9118 Get_Next_Interp (I, It);
9119 end loop;
9121 New_N :=
9122 Make_Function_Call (Loc,
9123 Name =>
9124 Make_Explicit_Dereference (Loc,
9125 Prefix => P),
9126 Parameter_Associations => New_List);
9128 Save_Interps (N, New_N);
9129 Rewrite (N, New_N);
9130 Analyze_And_Resolve (N, Typ);
9131 return;
9132 end if;
9134 -- If not overloaded, resolve P with its own type
9136 else
9137 Resolve (P);
9138 end if;
9140 -- If the prefix might be null, add an access check
9142 if Is_Access_Type (Etype (P))
9143 and then not Can_Never_Be_Null (Etype (P))
9144 then
9145 Apply_Access_Check (N);
9146 end if;
9148 -- If the designated type is a packed unconstrained array type, and the
9149 -- explicit dereference is not in the context of an attribute reference,
9150 -- then we must compute and set the actual subtype, since it is needed
9151 -- by Gigi. The reason we exclude the attribute case is that this is
9152 -- handled fine by Gigi, and in fact we use such attributes to build the
9153 -- actual subtype. We also exclude generated code (which builds actual
9154 -- subtypes directly if they are needed).
9156 if Is_Packed_Array (Etype (N))
9157 and then not Is_Constrained (Etype (N))
9158 and then Nkind (Parent (N)) /= N_Attribute_Reference
9159 and then Comes_From_Source (N)
9160 then
9161 Set_Etype (N, Get_Actual_Subtype (N));
9162 end if;
9164 Analyze_Dimension (N);
9166 -- Note: No Eval processing is required for an explicit dereference,
9167 -- because such a name can never be static.
9169 end Resolve_Explicit_Dereference;
9171 -------------------------------------
9172 -- Resolve_Expression_With_Actions --
9173 -------------------------------------
9175 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9177 function OK_For_Static (Act : Node_Id) return Boolean;
9178 -- True if Act is an action of a declare_expression that is allowed in a
9179 -- static declare_expression.
9181 function All_OK_For_Static return Boolean;
9182 -- True if all actions of N are allowed in a static declare_expression.
9184 function Get_Literal (Expr : Node_Id) return Node_Id;
9185 -- Expr is an expression with compile-time-known value. This returns the
9186 -- literal node that reprsents that value.
9188 -------------------
9189 -- OK_For_Static --
9190 -------------------
9192 function OK_For_Static (Act : Node_Id) return Boolean is
9193 begin
9194 case Nkind (Act) is
9195 when N_Object_Declaration =>
9196 if Constant_Present (Act)
9197 and then Is_Static_Expression (Expression (Act))
9198 then
9199 return True;
9200 end if;
9202 when N_Object_Renaming_Declaration =>
9203 if Statically_Names_Object (Name (Act)) then
9204 return True;
9205 end if;
9207 when others =>
9208 -- No other declarations, nor even pragmas, are allowed in a
9209 -- declare expression, so if we see something else, it must be
9210 -- an internally generated expression_with_actions.
9211 null;
9212 end case;
9214 return False;
9215 end OK_For_Static;
9217 -----------------------
9218 -- All_OK_For_Static --
9219 -----------------------
9221 function All_OK_For_Static return Boolean is
9222 Act : Node_Id := First (Actions (N));
9223 begin
9224 while Present (Act) loop
9225 if not OK_For_Static (Act) then
9226 return False;
9227 end if;
9229 Next (Act);
9230 end loop;
9232 return True;
9233 end All_OK_For_Static;
9235 -----------------
9236 -- Get_Literal --
9237 -----------------
9239 function Get_Literal (Expr : Node_Id) return Node_Id is
9240 pragma Assert (Compile_Time_Known_Value (Expr));
9241 Result : Node_Id;
9242 begin
9243 case Nkind (Expr) is
9244 when N_Has_Entity =>
9245 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9246 Result := Expr;
9247 else
9248 Result := Constant_Value (Entity (Expr));
9249 end if;
9250 when N_Numeric_Or_String_Literal =>
9251 Result := Expr;
9252 when others =>
9253 raise Program_Error;
9254 end case;
9256 pragma Assert
9257 (Nkind (Result) in N_Numeric_Or_String_Literal
9258 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9259 return Result;
9260 end Get_Literal;
9262 -- Local variables
9264 Loc : constant Source_Ptr := Sloc (N);
9266 -- Start of processing for Resolve_Expression_With_Actions
9268 begin
9269 Set_Etype (N, Typ);
9271 if Is_Empty_List (Actions (N)) then
9272 pragma Assert (All_OK_For_Static); null;
9273 end if;
9275 -- If the value of the expression is known at compile time, and all
9276 -- of the actions (if any) are suitable, then replace the declare
9277 -- expression with its expression. This allows the declare expression
9278 -- as a whole to be static if appropriate. See AI12-0368.
9280 if Compile_Time_Known_Value (Expression (N)) then
9281 if Is_Empty_List (Actions (N)) then
9282 Rewrite (N, Expression (N));
9283 elsif All_OK_For_Static then
9284 Rewrite
9285 (N, New_Copy_Tree
9286 (Get_Literal (Expression (N)), New_Sloc => Loc));
9287 end if;
9288 end if;
9289 end Resolve_Expression_With_Actions;
9291 ----------------------------------
9292 -- Resolve_Generalized_Indexing --
9293 ----------------------------------
9295 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9296 Indexing : constant Node_Id := Generalized_Indexing (N);
9297 begin
9298 Rewrite (N, Indexing);
9299 Resolve (N, Typ);
9300 end Resolve_Generalized_Indexing;
9302 ---------------------------
9303 -- Resolve_If_Expression --
9304 ---------------------------
9306 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9307 Condition : constant Node_Id := First (Expressions (N));
9309 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id);
9310 -- When a dependent expression is of a subtype different from
9311 -- the context subtype, then insert a qualification to ensure
9312 -- the generation of a constraint check. This was previously
9313 -- for scalar types. For array types apply a length check, given
9314 -- that the context in general allows sliding, while a qualified
9315 -- expression forces equality of bounds.
9317 -----------------
9318 -- Apply_Check --
9319 -----------------
9321 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id) is
9322 Expr_Typ : constant Entity_Id := Etype (Expr);
9323 Loc : constant Source_Ptr := Sloc (Expr);
9325 begin
9326 if Expr_Typ = Typ
9327 or else Is_Tagged_Type (Typ)
9328 or else Is_Access_Type (Typ)
9329 or else not Is_Constrained (Typ)
9330 or else Inside_A_Generic
9331 then
9332 null;
9334 elsif Is_Array_Type (Typ) then
9335 Apply_Length_Check (Expr, Typ);
9337 else
9338 Rewrite (Expr,
9339 Make_Qualified_Expression (Loc,
9340 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9341 Expression => Relocate_Node (Expr)));
9343 Analyze_And_Resolve (Expr, Result_Type);
9344 end if;
9345 end Apply_Check;
9347 -- Local variables
9349 Else_Expr : Node_Id;
9350 Then_Expr : Node_Id;
9352 Result_Type : Entity_Id;
9353 -- So in most cases the type of the if_expression and of its
9354 -- dependent expressions is that of the context. However, if
9355 -- the expression is the index of an Indexed_Component, we must
9356 -- ensure that a proper index check is applied, rather than a
9357 -- range check on the index type (which might be discriminant
9358 -- dependent). In this case we resolve with the base type of the
9359 -- index type, and the index check is generated in the resolution
9360 -- of the indexed_component above.
9362 -- Start of processing for Resolve_If_Expression
9364 begin
9365 -- Defend against malformed expressions
9367 if No (Condition) then
9368 return;
9369 end if;
9371 if Present (Parent (N))
9372 and then (Nkind (Parent (N)) = N_Indexed_Component
9373 or else Nkind (Parent (Parent (N))) = N_Indexed_Component)
9374 then
9375 Result_Type := Base_Type (Typ);
9377 else
9378 Result_Type := Typ;
9379 end if;
9381 Then_Expr := Next (Condition);
9383 if No (Then_Expr) then
9384 return;
9385 end if;
9387 Resolve (Condition, Any_Boolean);
9388 Check_Unset_Reference (Condition);
9390 Resolve_Dependent_Expression (N, Then_Expr, Result_Type);
9392 Check_Unset_Reference (Then_Expr);
9393 Apply_Check (Then_Expr, Result_Type);
9395 Else_Expr := Next (Then_Expr);
9397 -- If ELSE expression present, just resolve using the determined type
9399 if Present (Else_Expr) then
9400 Resolve_Dependent_Expression (N, Else_Expr, Result_Type);
9402 Check_Unset_Reference (Else_Expr);
9403 Apply_Check (Else_Expr, Result_Type);
9405 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9406 -- dynamically tagged must be known statically.
9408 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9409 if Is_Dynamically_Tagged (Then_Expr) /=
9410 Is_Dynamically_Tagged (Else_Expr)
9411 then
9412 Error_Msg_N ("all or none of the dependent expressions "
9413 & "can be dynamically tagged", N);
9414 end if;
9415 end if;
9417 -- If no ELSE expression is present, root type must be Standard.Boolean
9418 -- and we provide a Standard.True result converted to the appropriate
9419 -- Boolean type (in case it is a derived boolean type).
9421 elsif Root_Type (Typ) = Standard_Boolean then
9422 Else_Expr :=
9423 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9424 Analyze_And_Resolve (Else_Expr, Result_Type);
9425 Append_To (Expressions (N), Else_Expr);
9427 else
9428 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9429 Append_To (Expressions (N), Error);
9430 end if;
9432 Set_Etype (N, Result_Type);
9434 if not Error_Posted (N) then
9435 Eval_If_Expression (N);
9436 end if;
9438 Analyze_Dimension (N);
9439 end Resolve_If_Expression;
9441 ----------------------------------
9442 -- Resolve_Implicit_Dereference --
9443 ----------------------------------
9445 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9446 Desig_Typ : Entity_Id;
9448 begin
9449 if Is_Access_Type (Etype (P)) then
9450 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9451 Insert_Explicit_Dereference (P);
9452 Analyze_And_Resolve (P, Desig_Typ);
9453 end if;
9454 end Resolve_Implicit_Dereference;
9456 -------------------------------
9457 -- Resolve_Indexed_Component --
9458 -------------------------------
9460 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9461 Pref : constant Node_Id := Prefix (N);
9462 Expr : Node_Id;
9463 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9464 Index : Node_Id;
9466 begin
9467 if Present (Generalized_Indexing (N)) then
9468 Resolve_Generalized_Indexing (N, Typ);
9469 return;
9470 end if;
9472 if Is_Overloaded (Pref) then
9474 -- Use the context type to select the prefix that yields the correct
9475 -- component type.
9477 declare
9478 I : Interp_Index;
9479 It : Interp;
9480 I1 : Interp_Index := 0;
9481 Found : Boolean := False;
9483 begin
9484 Get_First_Interp (Pref, I, It);
9485 while Present (It.Typ) loop
9486 if (Is_Array_Type (It.Typ)
9487 and then Covers (Typ, Component_Type (It.Typ)))
9488 or else (Is_Access_Type (It.Typ)
9489 and then Is_Array_Type (Designated_Type (It.Typ))
9490 and then
9491 Covers
9492 (Typ,
9493 Component_Type (Designated_Type (It.Typ))))
9494 then
9495 if Found then
9496 It := Disambiguate (Pref, I1, I, Any_Type);
9498 if It = No_Interp then
9499 Error_Msg_N ("ambiguous prefix for indexing", N);
9500 Set_Etype (N, Typ);
9501 return;
9503 else
9504 Found := True;
9505 Array_Type := It.Typ;
9506 I1 := I;
9507 end if;
9509 else
9510 Found := True;
9511 Array_Type := It.Typ;
9512 I1 := I;
9513 end if;
9514 end if;
9516 Get_Next_Interp (I, It);
9517 end loop;
9518 end;
9520 else
9521 Array_Type := Etype (Pref);
9522 end if;
9524 Resolve (Pref, Array_Type);
9525 Array_Type := Get_Actual_Subtype_If_Available (Pref);
9527 -- If the prefix's type is an access type, get to the real array type.
9528 -- Note: we do not apply an access check because an explicit dereference
9529 -- will be introduced later, and the check will happen there.
9531 if Is_Access_Type (Array_Type) then
9532 Array_Type := Implicitly_Designated_Type (Array_Type);
9533 end if;
9535 -- If name was overloaded, set component type correctly now.
9536 -- If a misplaced call to an entry family (which has no index types)
9537 -- return. Error will be diagnosed from calling context.
9539 if Is_Array_Type (Array_Type) then
9540 Set_Etype (N, Component_Type (Array_Type));
9541 else
9542 return;
9543 end if;
9545 Index := First_Index (Array_Type);
9546 Expr := First (Expressions (N));
9548 -- The prefix may have resolved to a string literal, in which case its
9549 -- etype has a special representation. This is only possible currently
9550 -- if the prefix is a static concatenation, written in functional
9551 -- notation.
9553 if Ekind (Array_Type) = E_String_Literal_Subtype then
9554 Resolve (Expr, Standard_Positive);
9556 else
9557 while Present (Index) and then Present (Expr) loop
9558 Resolve (Expr, Etype (Index));
9559 Check_Unset_Reference (Expr);
9561 Apply_Scalar_Range_Check (Expr, Etype (Index));
9563 Next_Index (Index);
9564 Next (Expr);
9565 end loop;
9566 end if;
9568 Resolve_Implicit_Dereference (Pref);
9569 Analyze_Dimension (N);
9571 -- Do not generate the warning on suspicious index if we are analyzing
9572 -- package Ada.Tags; otherwise we will report the warning with the
9573 -- Prims_Ptr field of the dispatch table.
9575 if Scope (Etype (Pref)) = Standard_Standard
9576 or else not
9577 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Pref))), Ada_Tags)
9578 then
9579 Warn_On_Suspicious_Index (Pref, First (Expressions (N)));
9580 Eval_Indexed_Component (N);
9581 end if;
9583 -- If the array type is atomic and the component is not, then this is
9584 -- worth a warning before Ada 2022, since we have a situation where the
9585 -- access to the component may cause extra read/writes of the atomic
9586 -- object, or partial word accesses, both of which may be unexpected.
9588 if Nkind (N) = N_Indexed_Component
9589 and then Is_Atomic_Ref_With_Address (N)
9590 and then not (Has_Atomic_Components (Array_Type)
9591 or else (Is_Entity_Name (Pref)
9592 and then Has_Atomic_Components
9593 (Entity (Pref))))
9594 and then not Is_Atomic (Component_Type (Array_Type))
9595 and then Ada_Version < Ada_2022
9596 then
9597 Error_Msg_N
9598 ("??access to non-atomic component of atomic array", Pref);
9599 Error_Msg_N
9600 ("??\may cause unexpected accesses to atomic object", Pref);
9601 end if;
9602 end Resolve_Indexed_Component;
9604 -----------------------------
9605 -- Resolve_Integer_Literal --
9606 -----------------------------
9608 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9609 begin
9610 Set_Etype (N, Typ);
9611 Eval_Integer_Literal (N);
9612 end Resolve_Integer_Literal;
9614 -----------------------------------------
9615 -- Resolve_Interpolated_String_Literal --
9616 -----------------------------------------
9618 procedure Resolve_Interpolated_String_Literal (N : Node_Id; Typ : Entity_Id)
9620 Str_Elem : Node_Id;
9622 begin
9623 Str_Elem := First (Expressions (N));
9624 pragma Assert (Nkind (Str_Elem) = N_String_Literal);
9626 while Present (Str_Elem) loop
9628 -- Resolve string elements using the context type; for interpolated
9629 -- expressions there is no need to check if their type has a suitable
9630 -- image function because under Ada 2022 all the types have such
9631 -- function available.
9633 if Etype (Str_Elem) = Any_String then
9634 Resolve (Str_Elem, Typ);
9635 end if;
9637 Next (Str_Elem);
9638 end loop;
9640 Set_Etype (N, Typ);
9641 end Resolve_Interpolated_String_Literal;
9643 --------------------------------
9644 -- Resolve_Intrinsic_Operator --
9645 --------------------------------
9647 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9648 Is_Stoele_Mod : constant Boolean :=
9649 Nkind (N) = N_Op_Mod
9650 and then Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
9651 and then Is_RTE (Etype (Left_Opnd (N)), RE_Address);
9652 -- True if this is the special mod operator of System.Storage_Elements,
9653 -- which needs to be resolved to the type of the left operand in order
9654 -- to implement the correct semantics.
9656 Btyp : constant Entity_Id :=
9657 (if Is_Stoele_Mod
9658 then Implementation_Base_Type (Etype (Left_Opnd (N)))
9659 else Implementation_Base_Type (Typ));
9660 -- The base type to be used for the operator
9662 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9663 -- If the operand is a literal, it cannot be the expression in a
9664 -- conversion. Use a qualified expression instead.
9666 ---------------------
9667 -- Convert_Operand --
9668 ---------------------
9670 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9671 Loc : constant Source_Ptr := Sloc (Opnd);
9672 Res : Node_Id;
9674 begin
9675 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9676 Res :=
9677 Make_Qualified_Expression (Loc,
9678 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9679 Expression => Relocate_Node (Opnd));
9680 Analyze (Res);
9682 else
9683 Res := Unchecked_Convert_To (Btyp, Opnd);
9684 end if;
9686 return Res;
9687 end Convert_Operand;
9689 -- Local variables
9691 Arg1 : Node_Id;
9692 Arg2 : Node_Id;
9693 Op : Entity_Id;
9695 -- Start of processing for Resolve_Intrinsic_Operator
9697 begin
9698 -- We must preserve the original entity in a generic setting, so that
9699 -- the legality of the operation can be verified in an instance.
9701 if not Expander_Active then
9702 return;
9703 end if;
9705 Op := Entity (N);
9706 while Scope (Op) /= Standard_Standard loop
9707 Op := Homonym (Op);
9708 pragma Assert (Present (Op));
9709 end loop;
9711 Set_Entity (N, Op);
9712 Set_Is_Overloaded (N, False);
9714 -- If the result or operand types are private, rewrite with unchecked
9715 -- conversions on the operands and the result, to expose the proper
9716 -- underlying numeric type. Likewise for the special mod operator of
9717 -- System.Storage_Elements, to expose the modified base type.
9719 if Is_Private_Type (Typ)
9720 or else Is_Private_Type (Etype (Left_Opnd (N)))
9721 or else Is_Private_Type (Etype (Right_Opnd (N)))
9722 or else Is_Stoele_Mod
9723 then
9724 Arg1 := Convert_Operand (Left_Opnd (N));
9726 if Nkind (N) = N_Op_Expon then
9727 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9728 else
9729 Arg2 := Convert_Operand (Right_Opnd (N));
9730 end if;
9732 if Nkind (Arg1) = N_Type_Conversion then
9733 Save_Interps (Left_Opnd (N), Expression (Arg1));
9734 end if;
9736 if Nkind (Arg2) = N_Type_Conversion then
9737 Save_Interps (Right_Opnd (N), Expression (Arg2));
9738 end if;
9740 Set_Left_Opnd (N, Arg1);
9741 Set_Right_Opnd (N, Arg2);
9743 Set_Etype (N, Btyp);
9744 Rewrite (N, Unchecked_Convert_To (Typ, N));
9745 Resolve (N, Typ);
9747 elsif Typ /= Etype (Left_Opnd (N))
9748 or else Typ /= Etype (Right_Opnd (N))
9749 then
9750 -- Add explicit conversion where needed, and save interpretations in
9751 -- case operands are overloaded.
9753 Arg1 := Convert_To (Typ, Left_Opnd (N));
9754 Arg2 := Convert_To (Typ, Right_Opnd (N));
9756 if Nkind (Arg1) = N_Type_Conversion then
9757 Save_Interps (Left_Opnd (N), Expression (Arg1));
9758 else
9759 Save_Interps (Left_Opnd (N), Arg1);
9760 end if;
9762 if Nkind (Arg2) = N_Type_Conversion then
9763 Save_Interps (Right_Opnd (N), Expression (Arg2));
9764 else
9765 Save_Interps (Right_Opnd (N), Arg2);
9766 end if;
9768 Rewrite (Left_Opnd (N), Arg1);
9769 Rewrite (Right_Opnd (N), Arg2);
9770 Analyze (Arg1);
9771 Analyze (Arg2);
9772 Resolve_Arithmetic_Op (N, Typ);
9774 else
9775 Resolve_Arithmetic_Op (N, Typ);
9776 end if;
9777 end Resolve_Intrinsic_Operator;
9779 --------------------------------------
9780 -- Resolve_Intrinsic_Unary_Operator --
9781 --------------------------------------
9783 procedure Resolve_Intrinsic_Unary_Operator
9784 (N : Node_Id;
9785 Typ : Entity_Id)
9787 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9788 Op : Entity_Id;
9789 Arg2 : Node_Id;
9791 begin
9792 Op := Entity (N);
9793 while Scope (Op) /= Standard_Standard loop
9794 Op := Homonym (Op);
9795 pragma Assert (Present (Op));
9796 end loop;
9798 Set_Entity (N, Op);
9800 if Is_Private_Type (Typ) then
9801 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9802 Save_Interps (Right_Opnd (N), Expression (Arg2));
9804 Set_Right_Opnd (N, Arg2);
9806 Set_Etype (N, Btyp);
9807 Rewrite (N, Unchecked_Convert_To (Typ, N));
9808 Resolve (N, Typ);
9810 else
9811 Resolve_Unary_Op (N, Typ);
9812 end if;
9813 end Resolve_Intrinsic_Unary_Operator;
9815 ------------------------
9816 -- Resolve_Logical_Op --
9817 ------------------------
9819 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9820 B_Typ : Entity_Id;
9822 begin
9823 Check_No_Direct_Boolean_Operators (N);
9825 -- Predefined operations on scalar types yield the base type. On the
9826 -- other hand, logical operations on arrays yield the type of the
9827 -- arguments (and the context).
9829 if Is_Array_Type (Typ) then
9830 B_Typ := Typ;
9831 else
9832 B_Typ := Base_Type (Typ);
9833 end if;
9835 -- The following test is required because the operands of the operation
9836 -- may be literals, in which case the resulting type appears to be
9837 -- compatible with a signed integer type, when in fact it is compatible
9838 -- only with modular types. If the context itself is universal, the
9839 -- operation is illegal.
9841 if not Valid_Boolean_Arg (Typ) then
9842 Error_Msg_N ("invalid context for logical operation", N);
9843 Set_Etype (N, Any_Type);
9844 return;
9846 elsif Typ = Any_Modular then
9847 Error_Msg_N
9848 ("no modular type available in this context", N);
9849 Set_Etype (N, Any_Type);
9850 return;
9852 elsif Is_Modular_Integer_Type (Typ)
9853 and then Etype (Left_Opnd (N)) = Universal_Integer
9854 and then Etype (Right_Opnd (N)) = Universal_Integer
9855 then
9856 Check_For_Visible_Operator (N, B_Typ);
9857 end if;
9859 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9860 -- is active and the result type is standard Boolean (do not mess with
9861 -- ops that return a nonstandard Boolean type, because something strange
9862 -- is going on).
9864 -- Note: you might expect this replacement to be done during expansion,
9865 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9866 -- is used, no part of the right operand of an "and" or "or" operator
9867 -- should be executed if the left operand would short-circuit the
9868 -- evaluation of the corresponding "and then" or "or else". If we left
9869 -- the replacement to expansion time, then run-time checks associated
9870 -- with such operands would be evaluated unconditionally, due to being
9871 -- before the condition prior to the rewriting as short-circuit forms
9872 -- during expansion.
9874 if Short_Circuit_And_Or
9875 and then B_Typ = Standard_Boolean
9876 and then Nkind (N) in N_Op_And | N_Op_Or
9877 then
9878 -- Mark the corresponding putative SCO operator as truly a logical
9879 -- (and short-circuit) operator.
9881 if Generate_SCO and then Comes_From_Source (N) then
9882 Set_SCO_Logical_Operator (N);
9883 end if;
9885 if Nkind (N) = N_Op_And then
9886 Rewrite (N,
9887 Make_And_Then (Sloc (N),
9888 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9889 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9890 Analyze_And_Resolve (N, B_Typ);
9892 -- Case of OR changed to OR ELSE
9894 else
9895 Rewrite (N,
9896 Make_Or_Else (Sloc (N),
9897 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9898 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9899 Analyze_And_Resolve (N, B_Typ);
9900 end if;
9902 -- Return now, since analysis of the rewritten ops will take care of
9903 -- other reference bookkeeping and expression folding.
9905 return;
9906 end if;
9908 Resolve (Left_Opnd (N), B_Typ);
9909 Resolve (Right_Opnd (N), B_Typ);
9911 Check_Unset_Reference (Left_Opnd (N));
9912 Check_Unset_Reference (Right_Opnd (N));
9914 Set_Etype (N, B_Typ);
9915 Generate_Operator_Reference (N, B_Typ);
9916 Eval_Logical_Op (N);
9917 end Resolve_Logical_Op;
9919 ---------------------------------
9920 -- Resolve_Membership_Equality --
9921 ---------------------------------
9923 procedure Resolve_Membership_Equality (N : Node_Id; Typ : Entity_Id) is
9924 Utyp : constant Entity_Id := Underlying_Type (Typ);
9926 begin
9927 -- RM 4.5.2(4.1/3): if the type is limited, then it shall have a visible
9928 -- primitive equality operator. This means that we can use the regular
9929 -- visibility-based resolution and reset Entity in order to trigger it.
9931 if Is_Limited_Type (Typ) then
9932 Set_Entity (N, Empty);
9934 -- RM 4.5.2(28.1/3): if the type is a record, then the membership test
9935 -- uses the primitive equality for the type [even if it is not visible].
9936 -- We only deal with the untagged case here, because the tagged case is
9937 -- handled uniformly in the expander.
9939 elsif Is_Record_Type (Utyp) and then not Is_Tagged_Type (Utyp) then
9940 declare
9941 Eq_Id : constant Entity_Id := Get_User_Defined_Equality (Typ);
9943 begin
9944 if Present (Eq_Id) then
9945 Rewrite_Operator_As_Call (N, Eq_Id);
9946 end if;
9947 end;
9948 end if;
9949 end Resolve_Membership_Equality;
9951 ---------------------------
9952 -- Resolve_Membership_Op --
9953 ---------------------------
9955 -- The context can only be a boolean type, and does not determine the
9956 -- arguments. Arguments should be unambiguous, but the preference rule for
9957 -- universal types applies.
9959 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
9960 pragma Assert (Is_Boolean_Type (Typ));
9962 L : constant Node_Id := Left_Opnd (N);
9963 R : constant Node_Id := Right_Opnd (N);
9964 T : Entity_Id;
9966 procedure Resolve_Set_Membership;
9967 -- Analysis has determined a unique type for the left operand. Use it as
9968 -- the basis to resolve the disjuncts.
9970 ----------------------------
9971 -- Resolve_Set_Membership --
9972 ----------------------------
9974 procedure Resolve_Set_Membership is
9975 Alt : Node_Id;
9977 begin
9978 -- If the left operand is overloaded, find type compatible with not
9979 -- overloaded alternative of the right operand.
9981 Alt := First (Alternatives (N));
9982 if Is_Overloaded (L) then
9983 T := Empty;
9984 while Present (Alt) loop
9985 if not Is_Overloaded (Alt) then
9986 T := Intersect_Types (L, Alt);
9987 exit;
9988 else
9989 Next (Alt);
9990 end if;
9991 end loop;
9993 -- Unclear how to resolve expression if all alternatives are also
9994 -- overloaded.
9996 if No (T) then
9997 Error_Msg_N ("ambiguous expression", N);
9998 end if;
10000 else
10001 T := Intersect_Types (L, Alt);
10002 end if;
10004 Resolve (L, T);
10006 Alt := First (Alternatives (N));
10007 while Present (Alt) loop
10009 -- Alternative is an expression, a range
10010 -- or a subtype mark.
10012 if not Is_Entity_Name (Alt)
10013 or else not Is_Type (Entity (Alt))
10014 then
10015 Resolve (Alt, T);
10016 end if;
10018 Next (Alt);
10019 end loop;
10021 -- Check for duplicates for discrete case
10023 if Is_Discrete_Type (T) then
10024 declare
10025 type Ent is record
10026 Alt : Node_Id;
10027 Val : Uint;
10028 end record;
10030 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
10031 Nalts : Nat;
10033 begin
10034 -- Loop checking duplicates. This is quadratic, but giant sets
10035 -- are unlikely in this context so it's a reasonable choice.
10037 Nalts := 0;
10038 Alt := First (Alternatives (N));
10039 while Present (Alt) loop
10040 if Is_OK_Static_Expression (Alt)
10041 and then Nkind (Alt) in N_Integer_Literal
10042 | N_Character_Literal
10043 | N_Has_Entity
10044 then
10045 Nalts := Nalts + 1;
10046 Alts (Nalts) := (Alt, Expr_Value (Alt));
10048 for J in 1 .. Nalts - 1 loop
10049 if Alts (J).Val = Alts (Nalts).Val then
10050 Error_Msg_Sloc := Sloc (Alts (J).Alt);
10051 Error_Msg_N ("duplicate of value given#??", Alt);
10052 end if;
10053 end loop;
10054 end if;
10056 Next (Alt);
10057 end loop;
10058 end;
10059 end if;
10061 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
10062 -- limited types, evaluation of a membership test uses the predefined
10063 -- equality for the type. This may be confusing to users, and the
10064 -- following warning appears useful for the most common case.
10066 if Is_Scalar_Type (Etype (L))
10067 and then Present (Get_User_Defined_Equality (Etype (L)))
10068 then
10069 Error_Msg_NE
10070 ("membership test on& uses predefined equality?", N, Etype (L));
10071 Error_Msg_N
10072 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
10073 end if;
10074 end Resolve_Set_Membership;
10076 -- Start of processing for Resolve_Membership_Op
10078 begin
10079 if L = Error or else R = Error then
10080 return;
10081 end if;
10083 if Present (Alternatives (N)) then
10084 Resolve_Set_Membership;
10085 goto SM_Exit;
10087 elsif not Is_Overloaded (R)
10088 and then Is_Universal_Numeric_Type (Etype (R))
10089 and then Is_Overloaded (L)
10090 then
10091 T := Etype (R);
10093 -- If the left operand is of a universal numeric type and the right
10094 -- operand is not, we do not resolve the operands to the tested type
10095 -- but to the universal type instead. If not conforming to the letter,
10096 -- it's conforming to the spirit of the specification of membership
10097 -- tests, which are typically used to guard a specific operation and
10098 -- ought not to fail a check in doing so. Without this, in the case of
10100 -- type Small_Length is range 1 .. 16;
10102 -- function Is_Small_String (S : String) return Boolean is
10103 -- begin
10104 -- return S'Length in Small_Length;
10105 -- end;
10107 -- the function Is_Small_String would fail a range check for strings
10108 -- larger than 127 characters.
10110 -- The test on the size is required in GNAT because universal_integer
10111 -- does not cover all the values of all the supported integer types,
10112 -- for example the large values of Long_Long_Long_Unsigned.
10114 elsif not Is_Overloaded (L)
10115 and then Is_Universal_Numeric_Type (Etype (L))
10116 and then (Is_Overloaded (R)
10117 or else
10118 (not Is_Universal_Numeric_Type (Etype (R))
10119 and then
10120 (not Is_Integer_Type (Etype (R))
10121 or else
10122 RM_Size (Etype (R)) < RM_Size (Universal_Integer))))
10123 then
10124 T := Etype (L);
10126 -- If the right operand is 'Range, we first need to resolve it (to
10127 -- the tested type) so that it is rewritten as an N_Range, before
10128 -- converting its bounds and resolving it again below.
10130 if Nkind (R) = N_Attribute_Reference
10131 and then Attribute_Name (R) = Name_Range
10132 then
10133 Resolve (R);
10134 end if;
10136 -- If the right operand is an N_Range, we convert its bounds to the
10137 -- universal type before resolving it.
10139 if Nkind (R) = N_Range then
10140 Rewrite (R,
10141 Make_Range (Sloc (R),
10142 Low_Bound => Convert_To (T, Low_Bound (R)),
10143 High_Bound => Convert_To (T, High_Bound (R))));
10144 Analyze (R);
10145 end if;
10147 -- Ada 2005 (AI-251): Support the following case:
10149 -- type I is interface;
10150 -- type T is tagged ...
10152 -- function Test (O : I'Class) is
10153 -- begin
10154 -- return O in T'Class.
10155 -- end Test;
10157 -- In this case we have nothing else to do. The membership test will be
10158 -- done at run time.
10160 elsif Ada_Version >= Ada_2005
10161 and then Is_Class_Wide_Type (Etype (L))
10162 and then Is_Interface (Etype (L))
10163 and then not Is_Interface (Etype (R))
10164 then
10165 return;
10167 else
10168 T := Intersect_Types (L, R);
10169 end if;
10171 -- If mixed-mode operations are present and operands are all literal,
10172 -- the only interpretation involves Duration, which is probably not
10173 -- the intention of the programmer.
10175 if T = Any_Fixed then
10176 T := Unique_Fixed_Point_Type (N);
10178 if T = Any_Type then
10179 return;
10180 end if;
10181 end if;
10183 Resolve (L, T);
10184 Check_Unset_Reference (L);
10186 if Nkind (R) = N_Range
10187 and then not Is_Scalar_Type (T)
10188 then
10189 Error_Msg_N ("scalar type required for range", R);
10190 end if;
10192 if Is_Entity_Name (R) then
10193 Freeze_Expression (R);
10194 else
10195 Resolve (R, T);
10196 Check_Unset_Reference (R);
10197 end if;
10199 -- Here after resolving membership operation
10201 <<SM_Exit>>
10203 Eval_Membership_Op (N);
10204 end Resolve_Membership_Op;
10206 ------------------
10207 -- Resolve_Null --
10208 ------------------
10210 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
10211 Loc : constant Source_Ptr := Sloc (N);
10213 begin
10214 -- Handle restriction against anonymous null access values This
10215 -- restriction can be turned off using -gnatdj.
10217 -- Ada 2005 (AI-231): Remove restriction
10219 if Ada_Version < Ada_2005
10220 and then not Debug_Flag_J
10221 and then Ekind (Typ) = E_Anonymous_Access_Type
10222 and then Comes_From_Source (N)
10223 then
10224 -- In the common case of a call which uses an explicitly null value
10225 -- for an access parameter, give specialized error message.
10227 if Nkind (Parent (N)) in N_Subprogram_Call then
10228 Error_Msg_N
10229 ("NULL is not allowed as argument for an access parameter", N);
10231 -- Standard message for all other cases (are there any?)
10233 else
10234 Error_Msg_N
10235 ("NULL cannot be of an anonymous access type", N);
10236 end if;
10237 end if;
10239 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
10240 -- assignment to a null-excluding object.
10242 if Ada_Version >= Ada_2005
10243 and then Can_Never_Be_Null (Typ)
10244 and then Nkind (Parent (N)) = N_Assignment_Statement
10245 then
10246 if Inside_Init_Proc then
10248 -- Decide whether to generate an if_statement around our
10249 -- null-excluding check to avoid them on certain internal object
10250 -- declarations by looking at the type the current Init_Proc
10251 -- belongs to.
10253 -- Generate:
10254 -- if T1b_skip_null_excluding_check then
10255 -- [constraint_error "access check failed"]
10256 -- end if;
10258 if Needs_Conditional_Null_Excluding_Check
10259 (Etype (First_Formal (Enclosing_Init_Proc)))
10260 then
10261 Insert_Action (N,
10262 Make_If_Statement (Loc,
10263 Condition =>
10264 Make_Identifier (Loc,
10265 New_External_Name
10266 (Chars (Typ), "_skip_null_excluding_check")),
10267 Then_Statements =>
10268 New_List (
10269 Make_Raise_Constraint_Error (Loc,
10270 Reason => CE_Access_Check_Failed))));
10272 -- Otherwise, simply create the check
10274 else
10275 Insert_Action (N,
10276 Make_Raise_Constraint_Error (Loc,
10277 Reason => CE_Access_Check_Failed));
10278 end if;
10279 else
10280 Insert_Action
10281 (Compile_Time_Constraint_Error (N,
10282 "(Ada 2005) NULL not allowed in null-excluding objects??"),
10283 Make_Raise_Constraint_Error (Loc,
10284 Reason => CE_Access_Check_Failed));
10285 end if;
10286 end if;
10288 -- In a distributed context, null for a remote access to subprogram may
10289 -- need to be replaced with a special record aggregate. In this case,
10290 -- return after having done the transformation.
10292 if (Ekind (Typ) = E_Record_Type
10293 or else Is_Remote_Access_To_Subprogram_Type (Typ))
10294 and then Remote_AST_Null_Value (N, Typ)
10295 then
10296 return;
10297 end if;
10299 -- The null literal takes its type from the context
10301 Set_Etype (N, Typ);
10302 end Resolve_Null;
10304 -----------------------
10305 -- Resolve_Op_Concat --
10306 -----------------------
10308 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
10310 -- We wish to avoid deep recursion, because concatenations are often
10311 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
10312 -- operands nonrecursively until we find something that is not a simple
10313 -- concatenation (A in this case). We resolve that, and then walk back
10314 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10315 -- to do the rest of the work at each level. The Parent pointers allow
10316 -- us to avoid recursion, and thus avoid running out of memory. See also
10317 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10319 NN : Node_Id := N;
10320 Op1 : Node_Id;
10322 begin
10323 -- The following code is equivalent to:
10325 -- Resolve_Op_Concat_First (NN, Typ);
10326 -- Resolve_Op_Concat_Arg (N, ...);
10327 -- Resolve_Op_Concat_Rest (N, Typ);
10329 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10330 -- operand is a concatenation.
10332 -- Walk down left operands
10334 loop
10335 Resolve_Op_Concat_First (NN, Typ);
10336 Op1 := Left_Opnd (NN);
10337 exit when not (Nkind (Op1) = N_Op_Concat
10338 and then not Is_Array_Type (Component_Type (Typ))
10339 and then Entity (Op1) = Entity (NN));
10340 NN := Op1;
10341 end loop;
10343 -- Now (given the above example) NN is A&B and Op1 is A
10345 -- First resolve Op1 ...
10347 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10349 -- ... then walk NN back up until we reach N (where we started), calling
10350 -- Resolve_Op_Concat_Rest along the way.
10352 loop
10353 Resolve_Op_Concat_Rest (NN, Typ);
10354 exit when NN = N;
10355 NN := Parent (NN);
10356 end loop;
10357 end Resolve_Op_Concat;
10359 ---------------------------
10360 -- Resolve_Op_Concat_Arg --
10361 ---------------------------
10363 procedure Resolve_Op_Concat_Arg
10364 (N : Node_Id;
10365 Arg : Node_Id;
10366 Typ : Entity_Id;
10367 Is_Comp : Boolean)
10369 Btyp : constant Entity_Id := Base_Type (Typ);
10370 Ctyp : constant Entity_Id := Component_Type (Typ);
10372 begin
10373 if In_Instance then
10374 if Is_Comp
10375 or else (not Is_Overloaded (Arg)
10376 and then Etype (Arg) /= Any_Composite
10377 and then Covers (Ctyp, Etype (Arg)))
10378 then
10379 Resolve (Arg, Ctyp);
10380 else
10381 Resolve (Arg, Btyp);
10382 end if;
10384 -- If both Array & Array and Array & Component are visible, there is a
10385 -- potential ambiguity that must be reported.
10387 elsif Has_Compatible_Type (Arg, Ctyp) then
10388 if Nkind (Arg) = N_Aggregate
10389 and then Is_Composite_Type (Ctyp)
10390 then
10391 if Is_Private_Type (Ctyp) then
10392 Resolve (Arg, Btyp);
10394 -- If the operation is user-defined and not overloaded use its
10395 -- profile. The operation may be a renaming, in which case it has
10396 -- been rewritten, and we want the original profile.
10398 elsif not Is_Overloaded (N)
10399 and then Comes_From_Source (Entity (Original_Node (N)))
10400 and then Ekind (Entity (Original_Node (N))) = E_Function
10401 then
10402 Resolve (Arg,
10403 Etype
10404 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10405 return;
10407 -- Otherwise an aggregate may match both the array type and the
10408 -- component type.
10410 else
10411 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10412 Set_Etype (Arg, Any_Type);
10413 end if;
10415 else
10416 if Is_Overloaded (Arg)
10417 and then Has_Compatible_Type (Arg, Typ)
10418 and then Etype (Arg) /= Any_Type
10419 then
10420 declare
10421 I : Interp_Index;
10422 It : Interp;
10423 Func : Entity_Id;
10425 begin
10426 Get_First_Interp (Arg, I, It);
10427 Func := It.Nam;
10428 Get_Next_Interp (I, It);
10430 -- Special-case the error message when the overloading is
10431 -- caused by a function that yields an array and can be
10432 -- called without parameters.
10434 if It.Nam = Func then
10435 Error_Msg_Sloc := Sloc (Func);
10436 Error_Msg_N ("ambiguous call to function#", Arg);
10437 Error_Msg_NE
10438 ("\\interpretation as call yields&", Arg, Typ);
10439 Error_Msg_NE
10440 ("\\interpretation as indexing of call yields&",
10441 Arg, Ctyp);
10443 else
10444 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10446 Get_First_Interp (Arg, I, It);
10447 while Present (It.Nam) loop
10448 Error_Msg_Sloc := Sloc (It.Nam);
10450 if Base_Type (It.Typ) = Btyp
10451 or else
10452 Base_Type (It.Typ) = Base_Type (Ctyp)
10453 then
10454 Error_Msg_N -- CODEFIX
10455 ("\\possible interpretation#", Arg);
10456 end if;
10458 Get_Next_Interp (I, It);
10459 end loop;
10460 end if;
10461 end;
10462 end if;
10464 Resolve (Arg, Ctyp);
10466 if Nkind (Arg) = N_String_Literal then
10467 Set_Etype (Arg, Ctyp);
10469 elsif Is_Scalar_Type (Etype (Arg))
10470 and then Compile_Time_Known_Value (Arg)
10471 then
10472 -- Determine if the out-of-range violation constitutes a
10473 -- warning or an error according to the expression base type,
10474 -- according to Ada 2022 RM 4.9 (35/2).
10476 if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then
10477 Apply_Compile_Time_Constraint_Error
10478 (Arg, "value not in range of}", CE_Range_Check_Failed,
10479 Ent => Base_Type (Ctyp),
10480 Typ => Base_Type (Ctyp));
10482 elsif Is_Out_Of_Range (Arg, Ctyp) then
10483 Apply_Compile_Time_Constraint_Error
10484 (Arg, "value not in range of}??", CE_Range_Check_Failed,
10485 Ent => Ctyp,
10486 Typ => Ctyp);
10487 end if;
10488 end if;
10490 if Arg = Left_Opnd (N) then
10491 Set_Is_Component_Left_Opnd (N);
10492 else
10493 Set_Is_Component_Right_Opnd (N);
10494 end if;
10495 end if;
10497 else
10498 Resolve (Arg, Btyp);
10499 end if;
10501 Check_Unset_Reference (Arg);
10502 end Resolve_Op_Concat_Arg;
10504 -----------------------------
10505 -- Resolve_Op_Concat_First --
10506 -----------------------------
10508 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10509 Btyp : constant Entity_Id := Base_Type (Typ);
10510 Op1 : constant Node_Id := Left_Opnd (N);
10511 Op2 : constant Node_Id := Right_Opnd (N);
10513 begin
10514 -- The parser folds an enormous sequence of concatenations of string
10515 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10516 -- in the right operand. If the expression resolves to a predefined "&"
10517 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10518 -- we give an error. See P_Simple_Expression in Par.Ch4.
10520 if Nkind (Op2) = N_String_Literal
10521 and then Is_Folded_In_Parser (Op2)
10522 and then Ekind (Entity (N)) = E_Function
10523 then
10524 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10525 and then String_Length (Strval (Op1)) = 0);
10526 Error_Msg_N ("too many user-defined concatenations", N);
10527 return;
10528 end if;
10530 Set_Etype (N, Btyp);
10532 if Is_Limited_Composite (Btyp) then
10533 Error_Msg_N ("concatenation not available for limited array", N);
10534 Explain_Limited_Type (Btyp, N);
10535 end if;
10536 end Resolve_Op_Concat_First;
10538 ----------------------------
10539 -- Resolve_Op_Concat_Rest --
10540 ----------------------------
10542 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10543 Op1 : constant Node_Id := Left_Opnd (N);
10544 Op2 : constant Node_Id := Right_Opnd (N);
10546 begin
10547 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10549 Generate_Operator_Reference (N, Typ);
10551 if Is_String_Type (Typ) then
10552 Eval_Concatenation (N);
10553 end if;
10555 -- If this is not a static concatenation, but the result is a string
10556 -- type (and not an array of strings) ensure that static string operands
10557 -- have their subtypes properly constructed.
10559 if Nkind (N) /= N_String_Literal
10560 and then Is_Character_Type (Component_Type (Typ))
10561 then
10562 Set_String_Literal_Subtype (Op1, Typ);
10563 Set_String_Literal_Subtype (Op2, Typ);
10564 end if;
10565 end Resolve_Op_Concat_Rest;
10567 ----------------------
10568 -- Resolve_Op_Expon --
10569 ----------------------
10571 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10572 B_Typ : constant Entity_Id := Base_Type (Typ);
10574 begin
10575 -- Catch attempts to do fixed-point exponentiation with universal
10576 -- operands, which is a case where the illegality is not caught during
10577 -- normal operator analysis. This is not done in preanalysis mode
10578 -- since the tree is not fully decorated during preanalysis.
10580 if Full_Analysis then
10581 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10582 Error_Msg_N ("exponentiation not available for fixed point", N);
10583 return;
10585 elsif Nkind (Parent (N)) in N_Op
10586 and then Present (Etype (Parent (N)))
10587 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10588 and then Etype (N) = Universal_Real
10589 and then Comes_From_Source (N)
10590 then
10591 Error_Msg_N ("exponentiation not available for fixed point", N);
10592 return;
10593 end if;
10594 end if;
10596 if Ekind (Entity (N)) = E_Function
10597 and then Is_Imported (Entity (N))
10598 and then Is_Intrinsic_Subprogram (Entity (N))
10599 then
10600 Generate_Reference (Entity (N), N);
10601 Resolve_Intrinsic_Operator (N, Typ);
10602 return;
10603 end if;
10605 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10606 Check_For_Visible_Operator (N, B_Typ);
10607 end if;
10609 -- We do the resolution using the base type, because intermediate values
10610 -- in expressions are always of the base type, not a subtype of it.
10612 Resolve (Left_Opnd (N), B_Typ);
10613 Resolve (Right_Opnd (N), Standard_Integer);
10615 -- For integer types, right argument must be in Natural range
10617 if Is_Integer_Type (Typ) then
10618 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10619 end if;
10621 Check_Unset_Reference (Left_Opnd (N));
10622 Check_Unset_Reference (Right_Opnd (N));
10624 Set_Etype (N, B_Typ);
10625 Generate_Operator_Reference (N, B_Typ);
10627 Analyze_Dimension (N);
10629 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10630 -- Evaluate the exponentiation operator for dimensioned type
10632 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10633 else
10634 Eval_Op_Expon (N);
10635 end if;
10637 -- Set overflow checking bit. Much cleverer code needed here eventually
10638 -- and perhaps the Resolve routines should be separated for the various
10639 -- arithmetic operations, since they will need different processing. ???
10641 if Nkind (N) in N_Op then
10642 if not Overflow_Checks_Suppressed (Etype (N)) then
10643 Enable_Overflow_Check (N);
10644 end if;
10645 end if;
10646 end Resolve_Op_Expon;
10648 --------------------
10649 -- Resolve_Op_Not --
10650 --------------------
10652 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10653 function Parent_Is_Boolean return Boolean;
10654 -- This function determines if the parent node is a boolean operator or
10655 -- operation (comparison op, membership test, or short circuit form) and
10656 -- the not in question is the left operand of this operation. Note that
10657 -- if the not is in parens, then false is returned.
10659 -----------------------
10660 -- Parent_Is_Boolean --
10661 -----------------------
10663 function Parent_Is_Boolean return Boolean is
10664 begin
10665 return Paren_Count (N) = 0
10666 and then Nkind (Parent (N)) in N_Membership_Test
10667 | N_Op_Boolean
10668 | N_Short_Circuit
10669 and then Left_Opnd (Parent (N)) = N;
10670 end Parent_Is_Boolean;
10672 -- Local variables
10674 B_Typ : Entity_Id;
10676 -- Start of processing for Resolve_Op_Not
10678 begin
10679 -- Predefined operations on scalar types yield the base type. On the
10680 -- other hand, logical operations on arrays yield the type of the
10681 -- arguments (and the context).
10683 if Is_Array_Type (Typ) then
10684 B_Typ := Typ;
10685 else
10686 B_Typ := Base_Type (Typ);
10687 end if;
10689 -- Straightforward case of incorrect arguments
10691 if not Valid_Boolean_Arg (Typ) then
10692 Error_Msg_N ("invalid operand type for operator&", N);
10693 Set_Etype (N, Any_Type);
10694 return;
10696 -- Special case of probable missing parens
10698 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10699 if Parent_Is_Boolean then
10700 Error_Msg_N
10701 ("operand of NOT must be enclosed in parentheses",
10702 Right_Opnd (N));
10703 else
10704 Error_Msg_N
10705 ("no modular type available in this context", N);
10706 end if;
10708 Set_Etype (N, Any_Type);
10709 return;
10711 -- OK resolution of NOT
10713 else
10714 -- Warn if non-boolean types involved. This is a case like not a < b
10715 -- where a and b are modular, where we will get (not a) < b and most
10716 -- likely not (a < b) was intended.
10718 if Warn_On_Questionable_Missing_Parens
10719 and then not Is_Boolean_Type (Typ)
10720 and then Parent_Is_Boolean
10721 then
10722 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10723 end if;
10725 -- Warn on double negation if checking redundant constructs
10727 if Warn_On_Redundant_Constructs
10728 and then Comes_From_Source (N)
10729 and then Comes_From_Source (Right_Opnd (N))
10730 and then Root_Type (Typ) = Standard_Boolean
10731 and then Nkind (Right_Opnd (N)) = N_Op_Not
10732 then
10733 Error_Msg_N ("redundant double negation?r?", N);
10734 end if;
10736 -- Complete resolution and evaluation of NOT
10738 Resolve (Right_Opnd (N), B_Typ);
10739 Check_Unset_Reference (Right_Opnd (N));
10740 Set_Etype (N, B_Typ);
10741 Generate_Operator_Reference (N, B_Typ);
10742 Eval_Op_Not (N);
10743 end if;
10744 end Resolve_Op_Not;
10746 -----------------------------
10747 -- Resolve_Operator_Symbol --
10748 -----------------------------
10750 -- Nothing to be done, all resolved already
10752 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10753 pragma Warnings (Off, N);
10754 pragma Warnings (Off, Typ);
10756 begin
10757 null;
10758 end Resolve_Operator_Symbol;
10760 ----------------------------------
10761 -- Resolve_Qualified_Expression --
10762 ----------------------------------
10764 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10765 pragma Warnings (Off, Typ);
10767 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10768 Expr : constant Node_Id := Expression (N);
10770 begin
10771 Resolve (Expr, Target_Typ);
10772 Check_Unset_Reference (Expr);
10774 -- A qualified expression requires an exact match of the type, class-
10775 -- wide matching is not allowed. However, if the qualifying type is
10776 -- specific and the expression has a class-wide type, it may still be
10777 -- okay, since it can be the result of the expansion of a call to a
10778 -- dispatching function, so we also have to check class-wideness of the
10779 -- type of the expression's original node.
10781 if (Is_Class_Wide_Type (Target_Typ)
10782 or else
10783 (Is_Class_Wide_Type (Etype (Expr))
10784 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10785 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10786 then
10787 Wrong_Type (Expr, Target_Typ);
10788 end if;
10790 -- If the target type is unconstrained, then we reset the type of the
10791 -- result from the type of the expression. For other cases, the actual
10792 -- subtype of the expression is the target type. But we avoid doing it
10793 -- for an allocator since this is not needed and might be problematic.
10795 if Is_Composite_Type (Target_Typ)
10796 and then not Is_Constrained (Target_Typ)
10797 and then Nkind (Parent (N)) /= N_Allocator
10798 then
10799 Set_Etype (N, Etype (Expr));
10800 end if;
10802 Analyze_Dimension (N);
10803 Eval_Qualified_Expression (N);
10805 -- If we still have a qualified expression after the static evaluation,
10806 -- then apply a scalar range check if needed. The reason that we do this
10807 -- after the Eval call is that otherwise, the application of the range
10808 -- check may convert an illegal static expression and result in warning
10809 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10811 if Nkind (N) = N_Qualified_Expression
10812 and then Is_Scalar_Type (Target_Typ)
10813 then
10814 Apply_Scalar_Range_Check (Expr, Target_Typ);
10815 end if;
10817 -- AI12-0100: Once the qualified expression is resolved, check whether
10818 -- operand satisfies a static predicate of the target subtype, if any.
10819 -- In the static expression case, a predicate check failure is an error.
10821 if Has_Predicates (Target_Typ) then
10822 Check_Expression_Against_Static_Predicate
10823 (Expr, Target_Typ, Static_Failure_Is_Error => True);
10824 end if;
10825 end Resolve_Qualified_Expression;
10827 ------------------------------
10828 -- Resolve_Raise_Expression --
10829 ------------------------------
10831 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10832 begin
10833 if Typ = Raise_Type then
10834 Error_Msg_N ("cannot find unique type for raise expression", N);
10835 Set_Etype (N, Any_Type);
10837 else
10838 Set_Etype (N, Typ);
10840 -- Apply check for required parentheses in the enclosing
10841 -- context of raise_expressions (RM 11.3 (2)), including default
10842 -- expressions in contexts that can include aspect specifications,
10843 -- and ancestor parts of extension aggregates.
10845 declare
10846 Par : Node_Id := Parent (N);
10847 Parentheses_Found : Boolean := Paren_Count (N) > 0;
10849 begin
10850 while Present (Par)
10851 and then Nkind (Par) in N_Has_Etype
10852 loop
10853 if Paren_Count (Par) > 0 then
10854 Parentheses_Found := True;
10855 end if;
10857 if Nkind (Par) = N_Extension_Aggregate
10858 and then N = Ancestor_Part (Par)
10859 then
10860 exit;
10861 end if;
10863 Par := Parent (Par);
10864 end loop;
10866 if not Parentheses_Found
10867 and then Comes_From_Source (Par)
10868 and then
10869 (Nkind (Par) in N_Modular_Type_Definition
10870 | N_Floating_Point_Definition
10871 | N_Ordinary_Fixed_Point_Definition
10872 | N_Decimal_Fixed_Point_Definition
10873 | N_Extension_Aggregate
10874 | N_Discriminant_Specification
10875 | N_Parameter_Specification
10876 | N_Formal_Object_Declaration
10878 or else (Nkind (Par) = N_Object_Declaration
10879 and then
10880 Nkind (Parent (Par)) /= N_Extended_Return_Statement))
10881 then
10882 Error_Msg_N
10883 ("raise_expression must be parenthesized in this context",
10885 end if;
10886 end;
10887 end if;
10888 end Resolve_Raise_Expression;
10890 -------------------
10891 -- Resolve_Range --
10892 -------------------
10894 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10895 L : constant Node_Id := Low_Bound (N);
10896 H : constant Node_Id := High_Bound (N);
10898 function First_Last_Ref return Boolean;
10899 -- Returns True if N is of the form X'First .. X'Last where X is the
10900 -- same entity for both attributes.
10902 --------------------
10903 -- First_Last_Ref --
10904 --------------------
10906 function First_Last_Ref return Boolean is
10907 Lorig : constant Node_Id := Original_Node (L);
10908 Horig : constant Node_Id := Original_Node (H);
10910 begin
10911 if Nkind (Lorig) = N_Attribute_Reference
10912 and then Nkind (Horig) = N_Attribute_Reference
10913 and then Attribute_Name (Lorig) = Name_First
10914 and then Attribute_Name (Horig) = Name_Last
10915 then
10916 declare
10917 PL : constant Node_Id := Prefix (Lorig);
10918 PH : constant Node_Id := Prefix (Horig);
10919 begin
10920 return Is_Entity_Name (PL)
10921 and then Is_Entity_Name (PH)
10922 and then Entity (PL) = Entity (PH);
10923 end;
10924 end if;
10926 return False;
10927 end First_Last_Ref;
10929 -- Start of processing for Resolve_Range
10931 begin
10932 Set_Etype (N, Typ);
10934 Resolve (L, Typ);
10935 Resolve (H, Typ);
10937 -- Reanalyze the lower bound after both bounds have been analyzed, so
10938 -- that the range is known to be static or not by now. This may trigger
10939 -- more compile-time evaluation, which is useful for static analysis
10940 -- with GNATprove. This is not needed for compilation or static analysis
10941 -- with CodePeer, as full expansion does that evaluation then.
10943 if GNATprove_Mode then
10944 Set_Analyzed (L, False);
10945 Resolve (L, Typ);
10946 end if;
10948 -- Check for inappropriate range on unordered enumeration type
10950 if Bad_Unordered_Enumeration_Reference (N, Typ)
10952 -- Exclude X'First .. X'Last if X is the same entity for both
10954 and then not First_Last_Ref
10955 then
10956 Error_Msg_Sloc := Sloc (Typ);
10957 Error_Msg_NE
10958 ("subrange of unordered enumeration type& declared#?.u?", N, Typ);
10959 end if;
10961 Check_Unset_Reference (L);
10962 Check_Unset_Reference (H);
10964 -- We have to check the bounds for being within the base range as
10965 -- required for a non-static context. Normally this is automatic and
10966 -- done as part of evaluating expressions, but the N_Range node is an
10967 -- exception, since in GNAT we consider this node to be a subexpression,
10968 -- even though in Ada it is not. The circuit in Sem_Eval could check for
10969 -- this, but that would put the test on the main evaluation path for
10970 -- expressions.
10972 Check_Non_Static_Context (L);
10973 Check_Non_Static_Context (H);
10975 -- Check for an ambiguous range over character literals. This will
10976 -- happen with a membership test involving only literals.
10978 if Typ = Any_Character then
10979 Ambiguous_Character (L);
10980 Set_Etype (N, Any_Type);
10981 return;
10982 end if;
10984 -- If bounds are static, constant-fold them, so size computations are
10985 -- identical between front-end and back-end. Do not perform this
10986 -- transformation while analyzing generic units, as type information
10987 -- would be lost when reanalyzing the constant node in the instance.
10989 if Is_Discrete_Type (Typ) and then Expander_Active then
10990 if Is_OK_Static_Expression (L) then
10991 Fold_Uint (L, Expr_Value (L), Static => True);
10992 end if;
10994 if Is_OK_Static_Expression (H) then
10995 Fold_Uint (H, Expr_Value (H), Static => True);
10996 end if;
10997 end if;
10999 -- If we have a compile-time-known null range, we warn, because that is
11000 -- likely to be a mistake. (Dynamic null ranges make sense, but often
11001 -- compile-time-known ones do not.) Warn only if this is in a subtype
11002 -- declaration. We do this here, rather than while analyzing a subtype
11003 -- declaration, in case we decide to expand the cases. We do not want to
11004 -- warn in all cases, because some are idiomatic, such as an empty
11005 -- aggregate (1 .. 0 => <>).
11007 -- We don't warn in generics or their instances, because there might be
11008 -- some instances where the range is null, and some where it is not,
11009 -- which would lead to false alarms.
11011 if not (Inside_A_Generic or In_Instance)
11012 and then Comes_From_Source (N)
11013 and then Compile_Time_Compare
11014 (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT
11015 and then Nkind (Parent (N)) = N_Range_Constraint
11016 and then Nkind (Parent (Parent (N))) = N_Subtype_Indication
11017 and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration
11018 and then Is_OK_Static_Range (N)
11019 then
11020 Error_Msg_N ("null range??", N);
11021 end if;
11022 end Resolve_Range;
11024 --------------------------
11025 -- Resolve_Real_Literal --
11026 --------------------------
11028 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
11029 Actual_Typ : constant Entity_Id := Etype (N);
11031 begin
11032 -- Special processing for fixed-point literals to make sure that the
11033 -- value is an exact multiple of the small where this is required. We
11034 -- skip this for the universal real case, and also for generic types.
11036 if Is_Fixed_Point_Type (Typ)
11037 and then Typ /= Universal_Fixed
11038 and then Typ /= Any_Fixed
11039 and then not Is_Generic_Type (Typ)
11040 then
11041 -- We must freeze the base type to get the proper value of the small
11043 if not Is_Frozen (Base_Type (Typ)) then
11044 Freeze_Fixed_Point_Type (Base_Type (Typ));
11045 end if;
11047 declare
11048 Val : constant Ureal := Realval (N);
11049 Cintr : constant Ureal := Val / Small_Value (Base_Type (Typ));
11050 Cint : constant Uint := UR_Trunc (Cintr);
11051 Den : constant Uint := Norm_Den (Cintr);
11052 Stat : Boolean;
11054 begin
11055 -- Case of literal is not an exact multiple of the Small
11057 if Den /= 1 then
11059 -- For a source program literal for a decimal fixed-point type,
11060 -- this is statically illegal (RM 4.9(36)).
11062 if Is_Decimal_Fixed_Point_Type (Typ)
11063 and then Actual_Typ = Universal_Real
11064 and then Comes_From_Source (N)
11065 then
11066 Error_Msg_N ("value has extraneous low order digits", N);
11067 end if;
11069 -- Generate a warning if literal from source
11071 if Is_OK_Static_Expression (N)
11072 and then Warn_On_Bad_Fixed_Value
11073 then
11074 Error_Msg_N
11075 ("?b?static fixed-point value is not a multiple of Small!",
11077 end if;
11079 -- Replace literal by a value that is the exact representation
11080 -- of a value of the type, i.e. a multiple of the small value,
11081 -- by truncation, since Machine_Rounds is false for all GNAT
11082 -- fixed-point types (RM 4.9(38)).
11084 Stat := Is_OK_Static_Expression (N);
11085 Rewrite (N,
11086 Make_Real_Literal (Sloc (N),
11087 Realval => Small_Value (Typ) * Cint));
11089 Set_Is_Static_Expression (N, Stat);
11090 end if;
11092 -- In all cases, set the corresponding integer field
11094 Set_Corresponding_Integer_Value (N, Cint);
11095 end;
11096 end if;
11098 -- Now replace the actual type by the expected type as usual
11100 Set_Etype (N, Typ);
11101 Eval_Real_Literal (N);
11102 end Resolve_Real_Literal;
11104 -----------------------
11105 -- Resolve_Reference --
11106 -----------------------
11108 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
11109 P : constant Node_Id := Prefix (N);
11111 begin
11112 -- Replace general access with specific type
11114 if Ekind (Etype (N)) = E_Allocator_Type then
11115 Set_Etype (N, Base_Type (Typ));
11116 end if;
11118 Resolve (P, Designated_Type (Etype (N)));
11120 -- If we are taking the reference of a volatile entity, then treat it as
11121 -- a potential modification of this entity. This is too conservative,
11122 -- but necessary because remove side effects can cause transformations
11123 -- of normal assignments into reference sequences that otherwise fail to
11124 -- notice the modification.
11126 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
11127 Note_Possible_Modification (P, Sure => False);
11128 end if;
11129 end Resolve_Reference;
11131 --------------------------------
11132 -- Resolve_Selected_Component --
11133 --------------------------------
11135 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
11136 Comp : Entity_Id;
11137 Comp1 : Entity_Id := Empty; -- prevent junk warning
11138 P : constant Node_Id := Prefix (N);
11139 S : constant Node_Id := Selector_Name (N);
11140 T : Entity_Id := Etype (P);
11141 I : Interp_Index;
11142 I1 : Interp_Index := 0; -- prevent junk warning
11143 It : Interp;
11144 It1 : Interp;
11145 Found : Boolean;
11147 function Init_Component return Boolean;
11148 -- Check whether this is the initialization of a component within an
11149 -- init proc (by assignment or call to another init proc). If true,
11150 -- there is no need for a discriminant check.
11152 --------------------
11153 -- Init_Component --
11154 --------------------
11156 function Init_Component return Boolean is
11157 begin
11158 return Inside_Init_Proc
11159 and then Nkind (Prefix (N)) = N_Identifier
11160 and then Chars (Prefix (N)) = Name_uInit
11161 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
11162 end Init_Component;
11164 -- Start of processing for Resolve_Selected_Component
11166 begin
11167 if Is_Overloaded (P) then
11169 -- Use the context type to select the prefix that has a selector
11170 -- of the correct name and type.
11172 Found := False;
11173 Get_First_Interp (P, I, It);
11175 Search : while Present (It.Typ) loop
11176 if Is_Access_Type (It.Typ) then
11177 T := Designated_Type (It.Typ);
11178 else
11179 T := It.Typ;
11180 end if;
11182 -- Locate selected component. For a private prefix the selector
11183 -- can denote a discriminant.
11185 if Is_Record_Type (T) or else Is_Private_Type (T) then
11187 -- The visible components of a class-wide type are those of
11188 -- the root type.
11190 if Is_Class_Wide_Type (T) then
11191 T := Etype (T);
11192 end if;
11194 Comp := First_Entity (T);
11195 while Present (Comp) loop
11196 if Chars (Comp) = Chars (S)
11197 and then Covers (Typ, Etype (Comp))
11198 then
11199 if not Found then
11200 Found := True;
11201 I1 := I;
11202 It1 := It;
11203 Comp1 := Comp;
11205 else
11206 It := Disambiguate (P, I1, I, Any_Type);
11208 if It = No_Interp then
11209 Error_Msg_N
11210 ("ambiguous prefix for selected component", N);
11211 Set_Etype (N, Typ);
11212 return;
11214 else
11215 It1 := It;
11217 -- There may be an implicit dereference. Retrieve
11218 -- designated record type.
11220 if Is_Access_Type (It1.Typ) then
11221 T := Designated_Type (It1.Typ);
11222 else
11223 T := It1.Typ;
11224 end if;
11226 if Scope (Comp1) /= T then
11228 -- Resolution chooses the new interpretation.
11229 -- Find the component with the right name.
11231 Comp1 := First_Entity (T);
11232 while Present (Comp1)
11233 and then Chars (Comp1) /= Chars (S)
11234 loop
11235 Next_Entity (Comp1);
11236 end loop;
11237 end if;
11239 exit Search;
11240 end if;
11241 end if;
11242 end if;
11244 Next_Entity (Comp);
11245 end loop;
11246 end if;
11248 Get_Next_Interp (I, It);
11249 end loop Search;
11251 -- There must be a legal interpretation at this point
11253 pragma Assert (Found);
11254 Resolve (P, It1.Typ);
11256 -- In general the expected type is the type of the context, not the
11257 -- type of the candidate selected component.
11259 Set_Etype (N, Typ);
11260 Set_Entity_With_Checks (S, Comp1);
11262 -- The type of the context and that of the component are
11263 -- compatible and in general identical, but if they are anonymous
11264 -- access-to-subprogram types, the relevant type is that of the
11265 -- component. This matters in Unnest_Subprograms mode, where the
11266 -- relevant context is the one in which the type is declared, not
11267 -- the point of use. This determines what activation record to use.
11269 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
11270 Set_Etype (N, Etype (Comp1));
11272 -- When the type of the component is an access to a class-wide type
11273 -- the relevant type is that of the component (since in such case we
11274 -- may need to generate implicit type conversions or dispatching
11275 -- calls).
11277 elsif Is_Access_Type (Typ)
11278 and then not Is_Class_Wide_Type (Designated_Type (Typ))
11279 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
11280 then
11281 Set_Etype (N, Etype (Comp1));
11282 end if;
11284 else
11285 -- Resolve prefix with its type
11287 Resolve (P, T);
11288 end if;
11290 -- Generate cross-reference. We needed to wait until full overloading
11291 -- resolution was complete to do this, since otherwise we can't tell if
11292 -- we are an lvalue or not.
11294 if Known_To_Be_Assigned (N) then
11295 Generate_Reference (Entity (S), S, 'm');
11296 else
11297 Generate_Reference (Entity (S), S, 'r');
11298 end if;
11300 -- If the prefix's type is an access type, get to the real record type.
11301 -- Note: we do not apply an access check because an explicit dereference
11302 -- will be introduced later, and the check will happen there.
11304 if Is_Access_Type (Etype (P)) then
11305 T := Implicitly_Designated_Type (Etype (P));
11306 Check_Fully_Declared_Prefix (T, P);
11308 else
11309 T := Etype (P);
11310 end if;
11312 -- Set flag for expander if discriminant check required on a component
11313 -- appearing within a variant.
11315 if Has_Discriminants (T)
11316 and then Ekind (Entity (S)) = E_Component
11317 and then Present (Original_Record_Component (Entity (S)))
11318 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
11319 and then
11320 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
11321 and then not Discriminant_Checks_Suppressed (T)
11322 and then not Init_Component
11323 then
11324 Set_Do_Discriminant_Check (N);
11325 end if;
11327 if Ekind (Entity (S)) = E_Void then
11328 Error_Msg_N ("premature use of component", S);
11329 end if;
11331 -- If the prefix is a record conversion, this may be a renamed
11332 -- discriminant whose bounds differ from those of the original
11333 -- one, so we must ensure that a range check is performed.
11335 if Nkind (P) = N_Type_Conversion
11336 and then Ekind (Entity (S)) = E_Discriminant
11337 and then Is_Discrete_Type (Typ)
11338 then
11339 Set_Etype (N, Base_Type (Typ));
11340 end if;
11342 -- Eval_Selected_Component may e.g. fold statically known discriminants.
11344 Eval_Selected_Component (N);
11346 if Nkind (N) = N_Selected_Component then
11348 -- If the record type is atomic and the component is not, then this
11349 -- is worth a warning before Ada 2022, since we have a situation
11350 -- where the access to the component may cause extra read/writes of
11351 -- the atomic object, or partial word accesses, both of which may be
11352 -- unexpected.
11354 if Is_Atomic_Ref_With_Address (N)
11355 and then not Is_Atomic (Entity (S))
11356 and then not Is_Atomic (Etype (Entity (S)))
11357 and then Ada_Version < Ada_2022
11358 then
11359 Error_Msg_N
11360 ("??access to non-atomic component of atomic record",
11361 Prefix (N));
11362 Error_Msg_N
11363 ("\??may cause unexpected accesses to atomic object",
11364 Prefix (N));
11365 end if;
11367 Resolve_Implicit_Dereference (Prefix (N));
11368 Analyze_Dimension (N);
11369 end if;
11370 end Resolve_Selected_Component;
11372 -------------------
11373 -- Resolve_Shift --
11374 -------------------
11376 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11377 B_Typ : constant Entity_Id := Base_Type (Typ);
11378 L : constant Node_Id := Left_Opnd (N);
11379 R : constant Node_Id := Right_Opnd (N);
11381 begin
11382 -- We do the resolution using the base type, because intermediate values
11383 -- in expressions always are of the base type, not a subtype of it.
11385 Resolve (L, B_Typ);
11386 Resolve (R, Standard_Natural);
11388 Check_Unset_Reference (L);
11389 Check_Unset_Reference (R);
11391 Set_Etype (N, B_Typ);
11392 Generate_Operator_Reference (N, B_Typ);
11393 Eval_Shift (N);
11394 end Resolve_Shift;
11396 ---------------------------
11397 -- Resolve_Short_Circuit --
11398 ---------------------------
11400 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11401 B_Typ : constant Entity_Id := Base_Type (Typ);
11402 L : constant Node_Id := Left_Opnd (N);
11403 R : constant Node_Id := Right_Opnd (N);
11405 begin
11406 -- Ensure all actions associated with the left operand (e.g.
11407 -- finalization of transient objects) are fully evaluated locally within
11408 -- an expression with actions. This is particularly helpful for coverage
11409 -- analysis. However this should not happen in generics or if option
11410 -- Minimize_Expression_With_Actions is set.
11412 if Expander_Active and not Minimize_Expression_With_Actions then
11413 declare
11414 Reloc_L : constant Node_Id := Relocate_Node (L);
11415 begin
11416 Save_Interps (Old_N => L, New_N => Reloc_L);
11418 Rewrite (L,
11419 Make_Expression_With_Actions (Sloc (L),
11420 Actions => New_List,
11421 Expression => Reloc_L));
11423 -- Set Comes_From_Source on L to preserve warnings for unset
11424 -- reference.
11426 Preserve_Comes_From_Source (L, Reloc_L);
11427 end;
11428 end if;
11430 Resolve (L, B_Typ);
11431 Resolve (R, B_Typ);
11433 -- Check for issuing warning for always False assert/check, this happens
11434 -- when assertions are turned off, in which case the pragma Assert/Check
11435 -- was transformed into:
11437 -- if False and then <condition> then ...
11439 -- and we detect this pattern
11441 if Warn_On_Assertion_Failure
11442 and then Is_Entity_Name (R)
11443 and then Entity (R) = Standard_False
11444 and then Nkind (Parent (N)) = N_If_Statement
11445 and then Nkind (N) = N_And_Then
11446 and then Is_Entity_Name (L)
11447 and then Entity (L) = Standard_False
11448 then
11449 declare
11450 Orig : constant Node_Id := Original_Node (Parent (N));
11452 begin
11453 -- Special handling of Asssert pragma
11455 if Nkind (Orig) = N_Pragma
11456 and then Pragma_Name (Orig) = Name_Assert
11457 then
11458 declare
11459 Expr : constant Node_Id :=
11460 Original_Node
11461 (Expression
11462 (First (Pragma_Argument_Associations (Orig))));
11464 begin
11465 -- Don't warn if original condition is explicit False,
11466 -- since obviously the failure is expected in this case.
11468 if Is_Entity_Name (Expr)
11469 and then Entity (Expr) = Standard_False
11470 then
11471 null;
11473 -- Issue warning. We do not want the deletion of the
11474 -- IF/AND-THEN to take this message with it. We achieve this
11475 -- by making sure that the expanded code points to the Sloc
11476 -- of the expression, not the original pragma.
11478 else
11479 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11480 -- The source location of the expression is not usually
11481 -- the best choice here. For example, it gets located on
11482 -- the last AND keyword in a chain of boolean expressiond
11483 -- AND'ed together. It is best to put the message on the
11484 -- first character of the assertion, which is the effect
11485 -- of the First_Node call here.
11487 Error_Msg_F
11488 ("?.a?assertion would fail at run time!",
11489 Expression
11490 (First (Pragma_Argument_Associations (Orig))));
11491 end if;
11492 end;
11494 -- Similar processing for Check pragma
11496 elsif Nkind (Orig) = N_Pragma
11497 and then Pragma_Name (Orig) = Name_Check
11498 then
11499 -- Don't want to warn if original condition is explicit False
11501 declare
11502 Expr : constant Node_Id :=
11503 Original_Node
11504 (Expression
11505 (Next (First (Pragma_Argument_Associations (Orig)))));
11506 begin
11507 if Is_Entity_Name (Expr)
11508 and then Entity (Expr) = Standard_False
11509 then
11510 null;
11512 -- Post warning
11514 else
11515 -- Again use Error_Msg_F rather than Error_Msg_N, see
11516 -- comment above for an explanation of why we do this.
11518 Error_Msg_F
11519 ("?.a?check would fail at run time!",
11520 Expression
11521 (Last (Pragma_Argument_Associations (Orig))));
11522 end if;
11523 end;
11524 end if;
11525 end;
11526 end if;
11528 -- Continue with processing of short circuit
11530 Check_Unset_Reference (L);
11531 Check_Unset_Reference (R);
11533 Set_Etype (N, B_Typ);
11534 Eval_Short_Circuit (N);
11535 end Resolve_Short_Circuit;
11537 -------------------
11538 -- Resolve_Slice --
11539 -------------------
11541 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11542 Drange : constant Node_Id := Discrete_Range (N);
11543 Pref : constant Node_Id := Prefix (N);
11544 Array_Type : Entity_Id := Empty;
11545 Dexpr : Node_Id := Empty;
11546 Index_Type : Entity_Id;
11548 begin
11549 if Is_Overloaded (Pref) then
11551 -- Use the context type to select the prefix that yields the correct
11552 -- array type.
11554 declare
11555 I : Interp_Index;
11556 I1 : Interp_Index := 0;
11557 It : Interp;
11558 Found : Boolean := False;
11560 begin
11561 Get_First_Interp (Pref, I, It);
11562 while Present (It.Typ) loop
11563 if (Is_Array_Type (It.Typ)
11564 and then Covers (Typ, It.Typ))
11565 or else (Is_Access_Type (It.Typ)
11566 and then Is_Array_Type (Designated_Type (It.Typ))
11567 and then Covers (Typ, Designated_Type (It.Typ)))
11568 then
11569 if Found then
11570 It := Disambiguate (Pref, I1, I, Any_Type);
11572 if It = No_Interp then
11573 Error_Msg_N ("ambiguous prefix for slicing", N);
11574 Set_Etype (N, Typ);
11575 return;
11576 else
11577 Found := True;
11578 Array_Type := It.Typ;
11579 I1 := I;
11580 end if;
11581 else
11582 Found := True;
11583 Array_Type := It.Typ;
11584 I1 := I;
11585 end if;
11586 end if;
11588 Get_Next_Interp (I, It);
11589 end loop;
11590 end;
11592 else
11593 Array_Type := Etype (Pref);
11594 end if;
11596 Resolve (Pref, Array_Type);
11598 -- If the prefix's type is an access type, get to the real array type.
11599 -- Note: we do not apply an access check because an explicit dereference
11600 -- will be introduced later, and the check will happen there.
11602 if Is_Access_Type (Array_Type) then
11603 Array_Type := Implicitly_Designated_Type (Array_Type);
11605 -- If the prefix is an access to an unconstrained array, we must use
11606 -- the actual subtype of the object to perform the index checks. The
11607 -- object denoted by the prefix is implicit in the node, so we build
11608 -- an explicit representation for it in order to compute the actual
11609 -- subtype.
11611 if not Is_Constrained (Array_Type) then
11612 Remove_Side_Effects (Pref);
11614 declare
11615 Obj : constant Node_Id :=
11616 Make_Explicit_Dereference (Sloc (N),
11617 Prefix => New_Copy_Tree (Pref));
11618 begin
11619 Set_Etype (Obj, Array_Type);
11620 Set_Parent (Obj, Parent (N));
11621 Array_Type := Get_Actual_Subtype (Obj);
11622 end;
11623 end if;
11625 -- In CodePeer mode the attribute Image is not expanded, so when it
11626 -- acts as a prefix of a slice, we handle it like a call to function
11627 -- returning an unconstrained string. Same for the Wide variants of
11628 -- attribute Image.
11630 elsif Is_Entity_Name (Pref)
11631 or else Nkind (Pref) = N_Explicit_Dereference
11632 or else (Nkind (Pref) = N_Function_Call
11633 and then not Is_Constrained (Etype (Pref)))
11634 or else (CodePeer_Mode
11635 and then Nkind (Pref) = N_Attribute_Reference
11636 and then Attribute_Name (Pref) in Name_Image
11637 | Name_Wide_Image
11638 | Name_Wide_Wide_Image)
11639 then
11640 Array_Type := Get_Actual_Subtype (Pref);
11642 -- If the name is a selected component that depends on discriminants,
11643 -- build an actual subtype for it. This can happen only when the name
11644 -- itself is overloaded; otherwise the actual subtype is created when
11645 -- the selected component is analyzed.
11647 elsif Nkind (Pref) = N_Selected_Component
11648 and then Full_Analysis
11649 and then Depends_On_Discriminant (First_Index (Array_Type))
11650 then
11651 declare
11652 Act_Decl : constant Node_Id :=
11653 Build_Actual_Subtype_Of_Component (Array_Type, Pref);
11654 begin
11655 Insert_Action (N, Act_Decl);
11656 Array_Type := Defining_Identifier (Act_Decl);
11657 end;
11659 -- Maybe this should just be "else", instead of checking for the
11660 -- specific case of slice??? This is needed for the case where the
11661 -- prefix is an Image attribute, which gets expanded to a slice, and so
11662 -- has a constrained subtype which we want to use for the slice range
11663 -- check applied below (the range check won't get done if the
11664 -- unconstrained subtype of the 'Image is used).
11666 elsif Nkind (Pref) = N_Slice then
11667 Array_Type := Etype (Pref);
11668 end if;
11670 -- Obtain the type of the array index
11672 if Ekind (Array_Type) = E_String_Literal_Subtype then
11673 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11674 else
11675 Index_Type := Etype (First_Index (Array_Type));
11676 end if;
11678 -- If name was overloaded, set slice type correctly now
11680 Set_Etype (N, Array_Type);
11682 -- Handle the generation of a range check that compares the array index
11683 -- against the discrete_range. The check is not applied to internally
11684 -- built nodes associated with the expansion of dispatch tables. Check
11685 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11686 -- the unit.
11688 if Tagged_Type_Expansion
11689 and then RTU_Loaded (Ada_Tags)
11690 and then Nkind (Pref) = N_Selected_Component
11691 and then Present (Entity (Selector_Name (Pref)))
11692 and then Entity (Selector_Name (Pref)) =
11693 RTE_Record_Component (RE_Prims_Ptr)
11694 then
11695 null;
11697 -- The discrete_range is specified by a subtype name. Create an
11698 -- equivalent range attribute, apply checks to this attribute, but
11699 -- insert them into the range expression of the slice itself.
11701 elsif Is_Entity_Name (Drange) then
11702 Dexpr :=
11703 Make_Attribute_Reference
11704 (Sloc (Drange),
11705 Prefix =>
11706 New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
11707 Attribute_Name => Name_Range);
11709 Analyze_And_Resolve (Dexpr, Etype (Drange));
11711 elsif Nkind (Drange) = N_Subtype_Indication then
11712 Dexpr := Range_Expression (Constraint (Drange));
11714 -- The discrete_range is a regular range (or a range attribute, which
11715 -- will be resolved into a regular range). Resolve the bounds and remove
11716 -- their side effects.
11718 else
11719 Resolve (Drange, Base_Type (Index_Type));
11721 if Nkind (Drange) = N_Range then
11722 Force_Evaluation (Low_Bound (Drange));
11723 Force_Evaluation (High_Bound (Drange));
11725 Dexpr := Drange;
11726 end if;
11727 end if;
11729 if Present (Dexpr) then
11730 Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
11731 end if;
11733 Set_Slice_Subtype (N);
11735 -- Check bad use of type with predicates
11737 declare
11738 Subt : Entity_Id;
11740 begin
11741 if Nkind (Drange) = N_Subtype_Indication
11742 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11743 then
11744 Subt := Entity (Subtype_Mark (Drange));
11745 else
11746 Subt := Etype (Drange);
11747 end if;
11749 if Has_Predicates (Subt) then
11750 Bad_Predicated_Subtype_Use
11751 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11752 end if;
11753 end;
11755 -- Otherwise here is where we check suspicious indexes
11757 if Nkind (Drange) = N_Range then
11758 Warn_On_Suspicious_Index (Pref, Low_Bound (Drange));
11759 Warn_On_Suspicious_Index (Pref, High_Bound (Drange));
11760 end if;
11762 Resolve_Implicit_Dereference (Pref);
11763 Analyze_Dimension (N);
11764 Eval_Slice (N);
11765 end Resolve_Slice;
11767 ----------------------------
11768 -- Resolve_String_Literal --
11769 ----------------------------
11771 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11772 C_Typ : constant Entity_Id := Component_Type (Typ);
11773 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11774 Loc : constant Source_Ptr := Sloc (N);
11775 Str : constant String_Id := Strval (N);
11776 Strlen : constant Nat := String_Length (Str);
11777 Subtype_Id : Entity_Id;
11778 Need_Check : Boolean;
11780 begin
11781 -- For a string appearing in a concatenation, defer creation of the
11782 -- string_literal_subtype until the end of the resolution of the
11783 -- concatenation, because the literal may be constant-folded away. This
11784 -- is a useful optimization for long concatenation expressions.
11786 -- If the string is an aggregate built for a single character (which
11787 -- happens in a non-static context) or a is null string to which special
11788 -- checks may apply, we build the subtype. Wide strings must also get a
11789 -- string subtype if they come from a one character aggregate. Strings
11790 -- generated by attributes might be static, but it is often hard to
11791 -- determine whether the enclosing context is static, so we generate
11792 -- subtypes for them as well, thus losing some rarer optimizations ???
11793 -- Same for strings that come from a static conversion.
11795 Need_Check :=
11796 (Strlen = 0 and then Typ /= Standard_String)
11797 or else Nkind (Parent (N)) /= N_Op_Concat
11798 or else (N /= Left_Opnd (Parent (N))
11799 and then N /= Right_Opnd (Parent (N)))
11800 or else ((Typ = Standard_Wide_String
11801 or else Typ = Standard_Wide_Wide_String)
11802 and then Nkind (Original_Node (N)) /= N_String_Literal);
11804 -- If the resolving type is itself a string literal subtype, we can just
11805 -- reuse it, since there is no point in creating another.
11807 if Ekind (Typ) = E_String_Literal_Subtype then
11808 Subtype_Id := Typ;
11810 elsif Nkind (Parent (N)) = N_Op_Concat
11811 and then not Need_Check
11812 and then Nkind (Original_Node (N)) not in N_Character_Literal
11813 | N_Attribute_Reference
11814 | N_Qualified_Expression
11815 | N_Type_Conversion
11816 then
11817 Subtype_Id := Typ;
11819 -- Do not generate a string literal subtype for the default expression
11820 -- of a formal parameter in GNATprove mode. This is because the string
11821 -- subtype is associated with the freezing actions of the subprogram,
11822 -- however freezing is disabled in GNATprove mode and as a result the
11823 -- subtype is unavailable.
11825 elsif GNATprove_Mode
11826 and then Nkind (Parent (N)) = N_Parameter_Specification
11827 then
11828 Subtype_Id := Typ;
11830 -- Otherwise we must create a string literal subtype. Note that the
11831 -- whole idea of string literal subtypes is simply to avoid the need
11832 -- for building a full fledged array subtype for each literal.
11834 else
11835 Set_String_Literal_Subtype (N, Typ);
11836 Subtype_Id := Etype (N);
11837 end if;
11839 if Nkind (Parent (N)) /= N_Op_Concat
11840 or else Need_Check
11841 then
11842 Set_Etype (N, Subtype_Id);
11843 Eval_String_Literal (N);
11844 end if;
11846 if Is_Limited_Composite (Typ)
11847 or else Is_Private_Composite (Typ)
11848 then
11849 Error_Msg_N ("string literal not available for private array", N);
11850 Set_Etype (N, Any_Type);
11851 return;
11852 end if;
11854 -- The validity of a null string has been checked in the call to
11855 -- Eval_String_Literal.
11857 if Strlen = 0 then
11858 return;
11860 -- Always accept string literal with component type Any_Character, which
11861 -- occurs in error situations and in comparisons of literals, both of
11862 -- which should accept all literals.
11864 elsif R_Typ = Any_Character then
11865 return;
11867 -- If the type is bit-packed, then we always transform the string
11868 -- literal into a full fledged aggregate.
11870 elsif Is_Bit_Packed_Array (Typ) then
11871 null;
11873 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11875 else
11876 -- For Standard.Wide_Wide_String, or any other type whose component
11877 -- type is Standard.Wide_Wide_Character, we know that all the
11878 -- characters in the string must be acceptable, since the parser
11879 -- accepted the characters as valid character literals.
11881 if R_Typ = Standard_Wide_Wide_Character then
11882 null;
11884 -- For the case of Standard.String, or any other type whose component
11885 -- type is Standard.Character, we must make sure that there are no
11886 -- wide characters in the string, i.e. that it is entirely composed
11887 -- of characters in range of type Character.
11889 -- If the string literal is the result of a static concatenation, the
11890 -- test has already been performed on the components, and need not be
11891 -- repeated.
11893 elsif R_Typ = Standard_Character
11894 and then Nkind (Original_Node (N)) /= N_Op_Concat
11895 then
11896 for J in 1 .. Strlen loop
11897 if not In_Character_Range (Get_String_Char (Str, J)) then
11899 -- If we are out of range, post error. This is one of the
11900 -- very few places that we place the flag in the middle of
11901 -- a token, right under the offending wide character. Not
11902 -- quite clear if this is right wrt wide character encoding
11903 -- sequences, but it's only an error message.
11905 Error_Msg
11906 ("literal out of range of type Standard.Character",
11907 Loc + Source_Ptr (J));
11908 return;
11909 end if;
11910 end loop;
11912 -- For the case of Standard.Wide_String, or any other type whose
11913 -- component type is Standard.Wide_Character, we must make sure that
11914 -- there are no wide characters in the string, i.e. that it is
11915 -- entirely composed of characters in range of type Wide_Character.
11917 -- If the string literal is the result of a static concatenation,
11918 -- the test has already been performed on the components, and need
11919 -- not be repeated.
11921 elsif R_Typ = Standard_Wide_Character
11922 and then Nkind (Original_Node (N)) /= N_Op_Concat
11923 then
11924 for J in 1 .. Strlen loop
11925 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11927 -- If we are out of range, post error. This is one of the
11928 -- very few places that we place the flag in the middle of
11929 -- a token, right under the offending wide character.
11931 -- This is not quite right, because characters in general
11932 -- will take more than one character position ???
11934 Error_Msg
11935 ("literal out of range of type Standard.Wide_Character",
11936 Loc + Source_Ptr (J));
11937 return;
11938 end if;
11939 end loop;
11941 -- If the root type is not a standard character, then we will convert
11942 -- the string into an aggregate and will let the aggregate code do
11943 -- the checking. Standard Wide_Wide_Character is also OK here.
11945 else
11946 null;
11947 end if;
11949 -- See if the component type of the array corresponding to the string
11950 -- has compile time known bounds. If yes we can directly check
11951 -- whether the evaluation of the string will raise constraint error.
11952 -- Otherwise we need to transform the string literal into the
11953 -- corresponding character aggregate and let the aggregate code do
11954 -- the checking. We use the same transformation if the component
11955 -- type has a static predicate, which will be applied to each
11956 -- character when the aggregate is resolved.
11958 if Is_Standard_Character_Type (R_Typ) then
11960 -- Check for the case of full range, where we are definitely OK
11962 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
11963 return;
11964 end if;
11966 -- Here the range is not the complete base type range, so check
11968 declare
11969 Comp_Typ_Lo : constant Node_Id :=
11970 Type_Low_Bound (Component_Type (Typ));
11971 Comp_Typ_Hi : constant Node_Id :=
11972 Type_High_Bound (Component_Type (Typ));
11974 Char_Val : Uint;
11976 begin
11977 if Compile_Time_Known_Value (Comp_Typ_Lo)
11978 and then Compile_Time_Known_Value (Comp_Typ_Hi)
11979 then
11980 for J in 1 .. Strlen loop
11981 Char_Val := UI_From_CC (Get_String_Char (Str, J));
11983 if Char_Val < Expr_Value (Comp_Typ_Lo)
11984 or else Char_Val > Expr_Value (Comp_Typ_Hi)
11985 then
11986 Apply_Compile_Time_Constraint_Error
11987 (N, "character out of range??",
11988 CE_Range_Check_Failed,
11989 Loc => Loc + Source_Ptr (J));
11990 end if;
11991 end loop;
11993 if not Has_Static_Predicate (C_Typ) then
11994 return;
11995 end if;
11996 end if;
11997 end;
11998 end if;
11999 end if;
12001 -- If we got here we meed to transform the string literal into the
12002 -- equivalent qualified positional array aggregate. This is rather
12003 -- heavy artillery for this situation, but it is hard work to avoid.
12005 declare
12006 Lits : constant List_Id := New_List;
12007 P : Source_Ptr := Loc + 1;
12008 C : Char_Code;
12010 begin
12011 -- Build the character literals, we give them source locations that
12012 -- correspond to the string positions, which is a bit tricky given
12013 -- the possible presence of wide character escape sequences.
12015 for J in 1 .. Strlen loop
12016 C := Get_String_Char (Str, J);
12017 Set_Character_Literal_Name (C);
12019 Append_To (Lits,
12020 Make_Character_Literal (P,
12021 Chars => Name_Find,
12022 Char_Literal_Value => UI_From_CC (C)));
12024 if In_Character_Range (C) then
12025 P := P + 1;
12027 -- Should we have a call to Skip_Wide here ???
12029 -- ??? else
12030 -- Skip_Wide (P);
12032 end if;
12033 end loop;
12035 Rewrite (N,
12036 Make_Qualified_Expression (Loc,
12037 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
12038 Expression =>
12039 Make_Aggregate (Loc, Expressions => Lits)));
12041 Analyze_And_Resolve (N, Typ);
12042 end;
12043 end Resolve_String_Literal;
12045 -------------------------
12046 -- Resolve_Target_Name --
12047 -------------------------
12049 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
12050 begin
12051 Set_Etype (N, Typ);
12052 end Resolve_Target_Name;
12054 -----------------------------
12055 -- Resolve_Type_Conversion --
12056 -----------------------------
12058 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
12059 Conv_OK : constant Boolean := Conversion_OK (N);
12060 Operand : constant Node_Id := Expression (N);
12061 Operand_Typ : constant Entity_Id := Etype (Operand);
12062 Target_Typ : constant Entity_Id := Etype (N);
12063 Rop : Node_Id;
12064 Orig_N : Node_Id;
12065 Orig_T : Node_Id;
12067 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
12068 -- Set to False to suppress cases where we want to suppress the test
12069 -- for redundancy to avoid possible false positives on this warning.
12071 begin
12072 if not Conv_OK
12073 and then not Valid_Conversion (N, Target_Typ, Operand)
12074 then
12075 return;
12076 end if;
12078 -- If the Operand Etype is Universal_Fixed, then the conversion is
12079 -- never redundant. We need this check because by the time we have
12080 -- finished the rather complex transformation, the conversion looks
12081 -- redundant when it is not.
12083 if Operand_Typ = Universal_Fixed then
12084 Test_Redundant := False;
12086 -- If the operand is marked as Any_Fixed, then special processing is
12087 -- required. This is also a case where we suppress the test for a
12088 -- redundant conversion, since most certainly it is not redundant.
12090 elsif Operand_Typ = Any_Fixed then
12091 Test_Redundant := False;
12093 -- Mixed-mode operation involving a literal. Context must be a fixed
12094 -- type which is applied to the literal subsequently.
12096 -- Multiplication and division involving two fixed type operands must
12097 -- yield a universal real because the result is computed in arbitrary
12098 -- precision.
12100 if Is_Fixed_Point_Type (Typ)
12101 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
12102 and then Etype (Left_Opnd (Operand)) = Any_Fixed
12103 and then Etype (Right_Opnd (Operand)) = Any_Fixed
12104 then
12105 Set_Etype (Operand, Universal_Real);
12107 elsif Is_Numeric_Type (Typ)
12108 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
12109 and then (Etype (Right_Opnd (Operand)) = Universal_Real
12110 or else
12111 Etype (Left_Opnd (Operand)) = Universal_Real)
12112 then
12113 -- Return if expression is ambiguous
12115 if Unique_Fixed_Point_Type (N) = Any_Type then
12116 return;
12118 -- If nothing else, the available fixed type is Duration
12120 else
12121 Set_Etype (Operand, Standard_Duration);
12122 end if;
12124 -- Resolve the real operand with largest available precision
12126 if Etype (Right_Opnd (Operand)) = Universal_Real then
12127 Rop := New_Copy_Tree (Right_Opnd (Operand));
12128 else
12129 Rop := New_Copy_Tree (Left_Opnd (Operand));
12130 end if;
12132 Resolve (Rop, Universal_Real);
12134 -- If the operand is a literal (it could be a non-static and
12135 -- illegal exponentiation) check whether the use of Duration
12136 -- is potentially inaccurate.
12138 if Nkind (Rop) = N_Real_Literal
12139 and then Realval (Rop) /= Ureal_0
12140 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
12141 then
12142 Error_Msg_N
12143 ("??universal real operand can only "
12144 & "be interpreted as Duration!", Rop);
12145 Error_Msg_N
12146 ("\??precision will be lost in the conversion!", Rop);
12147 end if;
12149 elsif Is_Numeric_Type (Typ)
12150 and then Nkind (Operand) in N_Op
12151 and then Unique_Fixed_Point_Type (N) /= Any_Type
12152 then
12153 Set_Etype (Operand, Standard_Duration);
12155 else
12156 Error_Msg_N ("invalid context for mixed mode operation", N);
12157 Set_Etype (Operand, Any_Type);
12158 return;
12159 end if;
12160 end if;
12162 Resolve (Operand);
12164 Analyze_Dimension (N);
12166 -- Note: we do the Eval_Type_Conversion call before applying the
12167 -- required checks for a subtype conversion. This is important, since
12168 -- both are prepared under certain circumstances to change the type
12169 -- conversion to a constraint error node, but in the case of
12170 -- Eval_Type_Conversion this may reflect an illegality in the static
12171 -- case, and we would miss the illegality (getting only a warning
12172 -- message), if we applied the type conversion checks first.
12174 Eval_Type_Conversion (N);
12176 -- Even when evaluation is not possible, we may be able to simplify the
12177 -- conversion or its expression. This needs to be done before applying
12178 -- checks, since otherwise the checks may use the original expression
12179 -- and defeat the simplifications. This is specifically the case for
12180 -- elimination of the floating-point Truncation attribute in
12181 -- float-to-int conversions.
12183 Simplify_Type_Conversion (N);
12185 -- If after evaluation we still have a type conversion, then we may need
12186 -- to apply checks required for a subtype conversion. But skip them if
12187 -- universal fixed operands are involved, since range checks are handled
12188 -- separately for these cases, after the expansion done by Exp_Fixd.
12190 if Nkind (N) = N_Type_Conversion
12191 and then not Is_Generic_Type (Root_Type (Target_Typ))
12192 and then Target_Typ /= Universal_Fixed
12193 and then Etype (Operand) /= Universal_Fixed
12194 then
12195 Apply_Type_Conversion_Checks (N);
12196 end if;
12198 -- Issue warning for conversion of simple object to its own type. We
12199 -- have to test the original nodes, since they may have been rewritten
12200 -- by various optimizations.
12202 Orig_N := Original_Node (N);
12204 -- Here we test for a redundant conversion if the warning mode is
12205 -- active (and was not locally reset), and we have a type conversion
12206 -- from source not appearing in a generic instance.
12208 if Test_Redundant
12209 and then Nkind (Orig_N) = N_Type_Conversion
12210 and then Comes_From_Source (Orig_N)
12211 and then not In_Instance
12212 then
12213 Orig_N := Original_Node (Expression (Orig_N));
12214 Orig_T := Target_Typ;
12216 -- If the node is part of a larger expression, the Target_Type
12217 -- may not be the original type of the node if the context is a
12218 -- condition. Recover original type to see if conversion is needed.
12220 if Is_Boolean_Type (Orig_T)
12221 and then Nkind (Parent (N)) in N_Op
12222 then
12223 Orig_T := Etype (Parent (N));
12224 end if;
12226 -- If we have an entity name, then give the warning if the entity
12227 -- is the right type, or if it is a loop parameter covered by the
12228 -- original type (that's needed because loop parameters have an
12229 -- odd subtype coming from the bounds).
12231 if (Is_Entity_Name (Orig_N)
12232 and then Present (Entity (Orig_N))
12233 and then
12234 (Etype (Entity (Orig_N)) = Orig_T
12235 or else
12236 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
12237 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
12239 -- If not an entity, then type of expression must match
12241 or else Etype (Orig_N) = Orig_T
12242 then
12243 -- One more check, do not give warning if the analyzed conversion
12244 -- has an expression with non-static bounds, and the bounds of the
12245 -- target are static. This avoids junk warnings in cases where the
12246 -- conversion is necessary to establish staticness, for example in
12247 -- a case statement.
12249 if not Is_OK_Static_Subtype (Operand_Typ)
12250 and then Is_OK_Static_Subtype (Target_Typ)
12251 then
12252 null;
12254 -- Never give a warning if the operand is a conditional expression
12255 -- because RM 4.5.7(10/3) forces its type to be the target type.
12257 elsif Nkind (Orig_N) in N_Case_Expression | N_If_Expression then
12258 null;
12260 -- Finally, if this type conversion occurs in a context requiring
12261 -- a prefix, and the expression is a qualified expression then the
12262 -- type conversion is not redundant, since a qualified expression
12263 -- is not a prefix, whereas a type conversion is. For example, "X
12264 -- := T'(Funx(...)).Y;" is illegal because a selected component
12265 -- requires a prefix, but a type conversion makes it legal: "X :=
12266 -- T(T'(Funx(...))).Y;"
12268 -- In Ada 2012, a qualified expression is a name, so this idiom is
12269 -- no longer needed, but we still suppress the warning because it
12270 -- seems unfriendly for warnings to pop up when you switch to the
12271 -- newer language version.
12273 elsif Nkind (Orig_N) = N_Qualified_Expression
12274 and then Nkind (Parent (N)) in N_Attribute_Reference
12275 | N_Indexed_Component
12276 | N_Selected_Component
12277 | N_Slice
12278 | N_Explicit_Dereference
12279 then
12280 null;
12282 -- Never warn on conversion to Long_Long_Integer'Base since
12283 -- that is most likely an artifact of the extended overflow
12284 -- checking and comes from complex expanded code.
12286 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
12287 null;
12289 -- Do not warn on conversion to class-wide type on helpers of
12290 -- class-wide preconditions because in this context the warning
12291 -- would be spurious (since the class-wide precondition has been
12292 -- installed in the return statement of the helper, which has a
12293 -- class-wide formal type instead of a regular tagged type).
12295 elsif Is_Class_Wide_Type (Orig_T)
12296 and then Is_Subprogram_Or_Generic_Subprogram (Current_Scope)
12297 and then Present (Class_Preconditions_Subprogram (Current_Scope))
12298 then
12299 null;
12301 -- Here we give the redundant conversion warning. If it is an
12302 -- entity, give the name of the entity in the message. If not,
12303 -- just mention the expression.
12305 else
12306 if Is_Entity_Name (Orig_N) then
12307 Error_Msg_Node_2 := Orig_T;
12308 Error_Msg_NE -- CODEFIX
12309 ("?r?redundant conversion, & is of type &!",
12310 N, Entity (Orig_N));
12311 else
12312 Error_Msg_NE
12313 ("?r?redundant conversion, expression is of type&!",
12314 N, Orig_T);
12315 end if;
12316 end if;
12317 end if;
12318 end if;
12320 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
12321 -- No need to perform any interface conversion if the type of the
12322 -- expression coincides with the target type.
12324 if Ada_Version >= Ada_2005
12325 and then Expander_Active
12326 and then Operand_Typ /= Target_Typ
12327 then
12328 declare
12329 Opnd : Entity_Id := Operand_Typ;
12330 Target : Entity_Id := Target_Typ;
12332 begin
12333 -- If the type of the operand is a limited view, use nonlimited
12334 -- view when available. If it is a class-wide type, recover the
12335 -- class-wide type of the nonlimited view.
12337 if From_Limited_With (Opnd)
12338 and then Has_Non_Limited_View (Opnd)
12339 then
12340 Opnd := Non_Limited_View (Opnd);
12341 Set_Etype (Expression (N), Opnd);
12342 end if;
12344 -- It seems that Non_Limited_View should also be applied for
12345 -- Target when it has a limited view, but that leads to missing
12346 -- error checks on interface conversions further below. ???
12348 if Is_Access_Type (Opnd) then
12349 Opnd := Designated_Type (Opnd);
12351 -- If the type of the operand is a limited view, use nonlimited
12352 -- view when available. If it is a class-wide type, recover the
12353 -- class-wide type of the nonlimited view.
12355 if From_Limited_With (Opnd)
12356 and then Has_Non_Limited_View (Opnd)
12357 then
12358 Opnd := Non_Limited_View (Opnd);
12359 end if;
12360 end if;
12362 if Is_Access_Type (Target_Typ) then
12363 Target := Designated_Type (Target);
12365 -- If the target type is a limited view, use nonlimited view
12366 -- when available.
12368 if From_Limited_With (Target)
12369 and then Has_Non_Limited_View (Target)
12370 then
12371 Target := Non_Limited_View (Target);
12372 end if;
12373 end if;
12375 if Opnd = Target then
12376 null;
12378 -- Conversion from interface type
12380 -- It seems that it would be better for the error checks below
12381 -- to be performed as part of Validate_Conversion (and maybe some
12382 -- of the error checks above could be moved as well?). ???
12384 elsif Is_Interface (Opnd) then
12386 -- Ada 2005 (AI-217): Handle entities from limited views
12388 if From_Limited_With (Opnd) then
12389 Error_Msg_Qual_Level := 99;
12390 Error_Msg_NE -- CODEFIX
12391 ("missing WITH clause on package &", N,
12392 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
12393 Error_Msg_N
12394 ("type conversions require visibility of the full view",
12397 elsif From_Limited_With (Target)
12398 and then not
12399 (Is_Access_Type (Target_Typ)
12400 and then Present (Non_Limited_View (Etype (Target))))
12401 then
12402 Error_Msg_Qual_Level := 99;
12403 Error_Msg_NE -- CODEFIX
12404 ("missing WITH clause on package &", N,
12405 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12406 Error_Msg_N
12407 ("type conversions require visibility of the full view",
12410 else
12411 Expand_Interface_Conversion (N);
12412 end if;
12414 -- Conversion to interface type
12416 elsif Is_Interface (Target) then
12417 Expand_Interface_Conversion (N);
12418 end if;
12419 end;
12420 end if;
12422 -- Ada 2012: Once the type conversion is resolved, check whether the
12423 -- operand satisfies a static predicate of the target subtype, if any.
12424 -- In the static expression case, a predicate check failure is an error.
12426 if Has_Predicates (Target_Typ) then
12427 Check_Expression_Against_Static_Predicate
12428 (N, Target_Typ, Static_Failure_Is_Error => True);
12429 end if;
12431 -- If at this stage we have a fixed to integer conversion, make sure the
12432 -- Do_Range_Check flag is set, because such conversions in general need
12433 -- a range check. We only need this if expansion is off, see above why.
12435 if Nkind (N) = N_Type_Conversion
12436 and then not Expander_Active
12437 and then Is_Integer_Type (Target_Typ)
12438 and then Is_Fixed_Point_Type (Operand_Typ)
12439 and then not Range_Checks_Suppressed (Target_Typ)
12440 and then not Range_Checks_Suppressed (Operand_Typ)
12441 then
12442 Set_Do_Range_Check (Operand);
12443 end if;
12445 -- Generating C code a type conversion of an access to constrained
12446 -- array type to access to unconstrained array type involves building
12447 -- a fat pointer which in general cannot be generated on the fly. We
12448 -- remove side effects in order to store the result of the conversion
12449 -- into a temporary.
12451 if Modify_Tree_For_C
12452 and then Nkind (N) = N_Type_Conversion
12453 and then Nkind (Parent (N)) /= N_Object_Declaration
12454 and then Is_Access_Type (Etype (N))
12455 and then Is_Array_Type (Designated_Type (Etype (N)))
12456 and then not Is_Constrained (Designated_Type (Etype (N)))
12457 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
12458 then
12459 Remove_Side_Effects (N);
12460 end if;
12461 end Resolve_Type_Conversion;
12463 ----------------------
12464 -- Resolve_Unary_Op --
12465 ----------------------
12467 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12468 B_Typ : constant Entity_Id := Base_Type (Typ);
12469 R : constant Node_Id := Right_Opnd (N);
12470 OK : Boolean;
12471 Lo : Uint;
12472 Hi : Uint;
12474 begin
12475 -- Deal with intrinsic unary operators
12477 if Comes_From_Source (N)
12478 and then Ekind (Entity (N)) = E_Function
12479 and then Is_Imported (Entity (N))
12480 and then Is_Intrinsic_Subprogram (Entity (N))
12481 then
12482 Resolve_Intrinsic_Unary_Operator (N, Typ);
12483 return;
12484 end if;
12486 -- Deal with universal cases
12488 if Is_Universal_Numeric_Type (Etype (R)) then
12489 Check_For_Visible_Operator (N, B_Typ);
12490 end if;
12492 Set_Etype (N, B_Typ);
12493 Resolve (R, B_Typ);
12495 -- Generate warning for negative literal of a modular type, unless it is
12496 -- enclosed directly in a type qualification or a type conversion, as it
12497 -- is likely not what the user intended. We don't issue the warning for
12498 -- the common use of -1 to denote OxFFFF_FFFF...
12500 if Warn_On_Suspicious_Modulus_Value
12501 and then Nkind (N) = N_Op_Minus
12502 and then Nkind (R) = N_Integer_Literal
12503 and then Comes_From_Source (R)
12504 and then Is_Modular_Integer_Type (B_Typ)
12505 and then Nkind (Parent (N)) not in N_Qualified_Expression
12506 | N_Type_Conversion
12507 and then Expr_Value (R) > Uint_1
12508 then
12509 Error_Msg_N
12510 ("?.m?negative literal of modular type is in fact positive", N);
12511 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12512 Error_Msg_Uint_2 := Expr_Value (R);
12513 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12514 Error_Msg_N
12515 ("\if you do, use qualification to avoid this warning", N);
12516 end if;
12518 -- Generate warning for expressions like abs (x mod 2)
12520 if Warn_On_Redundant_Constructs
12521 and then Nkind (N) = N_Op_Abs
12522 then
12523 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12525 if OK and then Hi >= Lo and then Lo >= 0 then
12526 Error_Msg_N -- CODEFIX
12527 ("?r?abs applied to known non-negative value has no effect", N);
12528 end if;
12529 end if;
12531 -- Deal with reference generation
12533 Check_Unset_Reference (R);
12534 Generate_Operator_Reference (N, B_Typ);
12535 Analyze_Dimension (N);
12536 Eval_Unary_Op (N);
12538 -- Set overflow checking bit. Much cleverer code needed here eventually
12539 -- and perhaps the Resolve routines should be separated for the various
12540 -- arithmetic operations, since they will need different processing ???
12542 if Nkind (N) in N_Op then
12543 if not Overflow_Checks_Suppressed (Etype (N)) then
12544 Enable_Overflow_Check (N);
12545 end if;
12546 end if;
12548 -- Generate warning for expressions like -5 mod 3 for integers. No need
12549 -- to worry in the floating-point case, since parens do not affect the
12550 -- result so there is no point in giving in a warning.
12552 declare
12553 Norig : constant Node_Id := Original_Node (N);
12554 Rorig : Node_Id;
12555 Val : Uint;
12556 HB : Uint;
12557 LB : Uint;
12558 Lval : Uint;
12559 Opnd : Node_Id;
12561 begin
12562 if Warn_On_Questionable_Missing_Parens
12563 and then Comes_From_Source (Norig)
12564 and then Is_Integer_Type (Typ)
12565 and then Nkind (Norig) = N_Op_Minus
12566 then
12567 Rorig := Original_Node (Right_Opnd (Norig));
12569 -- We are looking for cases where the right operand is not
12570 -- parenthesized, and is a binary operator, multiply, divide, or
12571 -- mod. These are the cases where the grouping can affect results.
12573 if Paren_Count (Rorig) = 0
12574 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12575 then
12576 -- For mod, we always give the warning, since the value is
12577 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12578 -- -(5 mod 315)). But for the other cases, the only concern is
12579 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12580 -- overflows, but (-2) * 64 does not). So we try to give the
12581 -- message only when overflow is possible.
12583 if Nkind (Rorig) /= N_Op_Mod
12584 and then Compile_Time_Known_Value (R)
12585 then
12586 Val := Expr_Value (R);
12588 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12589 HB := Expr_Value (Type_High_Bound (Typ));
12590 else
12591 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12592 end if;
12594 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12595 LB := Expr_Value (Type_Low_Bound (Typ));
12596 else
12597 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12598 end if;
12600 -- Note that the test below is deliberately excluding the
12601 -- largest negative number, since that is a potentially
12602 -- troublesome case (e.g. -2 * x, where the result is the
12603 -- largest negative integer has an overflow with 2 * x).
12605 if Val > LB and then Val <= HB then
12606 return;
12607 end if;
12608 end if;
12610 -- For the multiplication case, the only case we have to worry
12611 -- about is when (-a)*b is exactly the largest negative number
12612 -- so that -(a*b) can cause overflow. This can only happen if
12613 -- a is a power of 2, and more generally if any operand is a
12614 -- constant that is not a power of 2, then the parentheses
12615 -- cannot affect whether overflow occurs. We only bother to
12616 -- test the left most operand
12618 -- Loop looking at left operands for one that has known value
12620 Opnd := Rorig;
12621 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12622 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12623 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12625 -- Operand value of 0 or 1 skips warning
12627 if Lval <= 1 then
12628 return;
12630 -- Otherwise check power of 2, if power of 2, warn, if
12631 -- anything else, skip warning.
12633 else
12634 while Lval /= 2 loop
12635 if Lval mod 2 = 1 then
12636 return;
12637 else
12638 Lval := Lval / 2;
12639 end if;
12640 end loop;
12642 exit Opnd_Loop;
12643 end if;
12644 end if;
12646 -- Keep looking at left operands
12648 Opnd := Left_Opnd (Opnd);
12649 end loop Opnd_Loop;
12651 -- For rem or "/" we can only have a problematic situation
12652 -- if the divisor has a value of minus one or one. Otherwise
12653 -- overflow is impossible (divisor > 1) or we have a case of
12654 -- division by zero in any case.
12656 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12657 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12658 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12659 then
12660 return;
12661 end if;
12663 -- If we fall through warning should be issued
12665 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12667 Error_Msg_N
12668 ("??unary minus expression should be parenthesized here!", N);
12669 end if;
12670 end if;
12671 end;
12672 end Resolve_Unary_Op;
12674 ----------------------------------
12675 -- Resolve_Unchecked_Expression --
12676 ----------------------------------
12678 procedure Resolve_Unchecked_Expression
12679 (N : Node_Id;
12680 Typ : Entity_Id)
12682 begin
12683 Resolve (Expression (N), Typ, Suppress => All_Checks);
12684 Set_Etype (N, Typ);
12685 end Resolve_Unchecked_Expression;
12687 ---------------------------------------
12688 -- Resolve_Unchecked_Type_Conversion --
12689 ---------------------------------------
12691 procedure Resolve_Unchecked_Type_Conversion
12692 (N : Node_Id;
12693 Typ : Entity_Id)
12695 pragma Warnings (Off, Typ);
12697 Operand : constant Node_Id := Expression (N);
12698 Opnd_Type : constant Entity_Id := Etype (Operand);
12700 begin
12701 -- Resolve operand using its own type
12703 Resolve (Operand, Opnd_Type);
12705 -- If the expression is a conversion to universal integer of an
12706 -- an expression with an integer type, then we can eliminate the
12707 -- intermediate conversion to universal integer.
12709 if Nkind (Operand) = N_Type_Conversion
12710 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12711 and then Is_Integer_Type (Etype (Expression (Operand)))
12712 then
12713 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12714 Analyze_And_Resolve (Operand);
12715 end if;
12717 -- In an inlined context, the unchecked conversion may be applied
12718 -- to a literal, in which case its type is the type of the context.
12719 -- (In other contexts conversions cannot apply to literals).
12721 if In_Inlined_Body
12722 and then (Opnd_Type = Any_Character or else
12723 Opnd_Type = Any_Integer or else
12724 Opnd_Type = Any_Real)
12725 then
12726 Set_Etype (Operand, Typ);
12727 end if;
12729 Analyze_Dimension (N);
12730 Eval_Unchecked_Conversion (N);
12731 end Resolve_Unchecked_Type_Conversion;
12733 ------------------------------
12734 -- Rewrite_Operator_As_Call --
12735 ------------------------------
12737 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12738 Loc : constant Source_Ptr := Sloc (N);
12739 Actuals : constant List_Id := New_List;
12740 New_N : Node_Id;
12742 begin
12743 if Nkind (N) in N_Binary_Op then
12744 Append (Left_Opnd (N), Actuals);
12745 end if;
12747 Append (Right_Opnd (N), Actuals);
12749 New_N :=
12750 Make_Function_Call (Sloc => Loc,
12751 Name => New_Occurrence_Of (Nam, Loc),
12752 Parameter_Associations => Actuals);
12754 Preserve_Comes_From_Source (New_N, N);
12755 Preserve_Comes_From_Source (Name (New_N), N);
12756 Rewrite (N, New_N);
12757 Set_Etype (N, Etype (Nam));
12758 end Rewrite_Operator_As_Call;
12760 ------------------------------
12761 -- Rewrite_Renamed_Operator --
12762 ------------------------------
12764 procedure Rewrite_Renamed_Operator
12765 (N : Node_Id;
12766 Op : Entity_Id;
12767 Typ : Entity_Id)
12769 Nam : constant Name_Id := Chars (Op);
12770 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12771 Op_Node : Node_Id;
12773 begin
12774 -- Do not perform this transformation within a pre/postcondition,
12775 -- because the expression will be reanalyzed, and the transformation
12776 -- might affect the visibility of the operator, e.g. in an instance.
12777 -- Note that fully analyzed and expanded pre/postconditions appear as
12778 -- pragma Check equivalents.
12780 if In_Pre_Post_Condition (N) then
12781 return;
12782 end if;
12784 -- Likewise when an expression function is being preanalyzed, since the
12785 -- expression will be reanalyzed as part of the generated body.
12787 if In_Spec_Expression then
12788 declare
12789 S : constant Entity_Id := Current_Scope_No_Loops;
12790 begin
12791 if Ekind (S) = E_Function
12792 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12793 N_Expression_Function
12794 then
12795 return;
12796 end if;
12797 end;
12798 end if;
12800 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12801 Set_Chars (Op_Node, Nam);
12802 Set_Etype (Op_Node, Etype (N));
12803 Set_Entity (Op_Node, Op);
12804 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12806 if Is_Binary then
12807 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12808 end if;
12810 -- Indicate that both the original entity and its renaming are
12811 -- referenced at this point.
12813 Generate_Reference (Entity (N), N);
12814 Generate_Reference (Op, N);
12816 Rewrite (N, Op_Node);
12818 -- If the context type is private, add the appropriate conversions so
12819 -- that the operator is applied to the full view. This is done in the
12820 -- routines that resolve intrinsic operators.
12822 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12823 case Nkind (N) is
12824 when N_Op_Add
12825 | N_Op_Divide
12826 | N_Op_Expon
12827 | N_Op_Mod
12828 | N_Op_Multiply
12829 | N_Op_Rem
12830 | N_Op_Subtract
12832 Resolve_Intrinsic_Operator (N, Typ);
12834 when N_Op_Abs
12835 | N_Op_Minus
12836 | N_Op_Plus
12838 Resolve_Intrinsic_Unary_Operator (N, Typ);
12840 when others =>
12841 Resolve (N, Typ);
12842 end case;
12843 end if;
12844 end Rewrite_Renamed_Operator;
12846 -----------------------
12847 -- Set_Slice_Subtype --
12848 -----------------------
12850 -- Build an implicit subtype declaration to represent the type delivered by
12851 -- the slice. This is an abbreviated version of an array subtype. We define
12852 -- an index subtype for the slice, using either the subtype name or the
12853 -- discrete range of the slice. To be consistent with index usage elsewhere
12854 -- we create a list header to hold the single index. This list is not
12855 -- otherwise attached to the syntax tree.
12857 procedure Set_Slice_Subtype (N : Node_Id) is
12858 Loc : constant Source_Ptr := Sloc (N);
12859 Index_List : constant List_Id := New_List;
12860 Index : Node_Id;
12861 Index_Subtype : Entity_Id;
12862 Index_Type : Entity_Id;
12863 Slice_Subtype : Entity_Id;
12864 Drange : constant Node_Id := Discrete_Range (N);
12866 begin
12867 Index_Type := Base_Type (Etype (Drange));
12869 if Is_Entity_Name (Drange) then
12870 Index_Subtype := Entity (Drange);
12872 else
12873 -- We force the evaluation of a range. This is definitely needed in
12874 -- the renamed case, and seems safer to do unconditionally. Note in
12875 -- any case that since we will create and insert an Itype referring
12876 -- to this range, we must make sure any side effect removal actions
12877 -- are inserted before the Itype definition.
12879 if Nkind (Drange) = N_Range then
12880 Force_Evaluation (Low_Bound (Drange));
12881 Force_Evaluation (High_Bound (Drange));
12883 -- If the discrete range is given by a subtype indication, the
12884 -- type of the slice is the base of the subtype mark.
12886 elsif Nkind (Drange) = N_Subtype_Indication then
12887 declare
12888 R : constant Node_Id := Range_Expression (Constraint (Drange));
12889 begin
12890 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12891 Force_Evaluation (Low_Bound (R));
12892 Force_Evaluation (High_Bound (R));
12893 end;
12894 end if;
12896 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12898 -- Take a new copy of Drange (where bounds have been rewritten to
12899 -- reference side-effect-free names). Using a separate tree ensures
12900 -- that further expansion (e.g. while rewriting a slice assignment
12901 -- into a FOR loop) does not attempt to remove side effects on the
12902 -- bounds again (which would cause the bounds in the index subtype
12903 -- definition to refer to temporaries before they are defined) (the
12904 -- reason is that some names are considered side effect free here
12905 -- for the subtype, but not in the context of a loop iteration
12906 -- scheme).
12908 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12909 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12910 Set_Etype (Index_Subtype, Index_Type);
12911 Set_Size_Info (Index_Subtype, Index_Type);
12912 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12913 Set_Is_Constrained (Index_Subtype);
12914 end if;
12916 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12918 Index := New_Occurrence_Of (Index_Subtype, Loc);
12919 Set_Etype (Index, Index_Subtype);
12920 Append (Index, Index_List);
12922 Set_First_Index (Slice_Subtype, Index);
12923 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12924 Set_Is_Constrained (Slice_Subtype, True);
12926 Check_Compile_Time_Size (Slice_Subtype);
12928 -- The Etype of the existing Slice node is reset to this slice subtype.
12929 -- Its bounds are obtained from its first index.
12931 Set_Etype (N, Slice_Subtype);
12933 -- For bit-packed slice subtypes, freeze immediately (except in the case
12934 -- of being in a "spec expression" where we never freeze when we first
12935 -- see the expression).
12937 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12938 Freeze_Itype (Slice_Subtype, N);
12940 -- For all other cases insert an itype reference in the slice's actions
12941 -- so that the itype is frozen at the proper place in the tree (i.e. at
12942 -- the point where actions for the slice are analyzed). Note that this
12943 -- is different from freezing the itype immediately, which might be
12944 -- premature (e.g. if the slice is within a transient scope). This needs
12945 -- to be done only if expansion is enabled.
12947 elsif Expander_Active then
12948 Ensure_Defined (Typ => Slice_Subtype, N => N);
12949 end if;
12950 end Set_Slice_Subtype;
12952 --------------------------------
12953 -- Set_String_Literal_Subtype --
12954 --------------------------------
12956 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12957 Loc : constant Source_Ptr := Sloc (N);
12958 Low_Bound : constant Node_Id :=
12959 Type_Low_Bound (Etype (First_Index (Typ)));
12960 Subtype_Id : Entity_Id;
12962 begin
12963 if Nkind (N) /= N_String_Literal then
12964 return;
12965 end if;
12967 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
12968 Set_String_Literal_Length (Subtype_Id, UI_From_Int
12969 (String_Length (Strval (N))));
12970 Set_Etype (Subtype_Id, Base_Type (Typ));
12971 Set_Is_Constrained (Subtype_Id);
12972 Set_Etype (N, Subtype_Id);
12974 -- The low bound is set from the low bound of the corresponding index
12975 -- type. Note that we do not store the high bound in the string literal
12976 -- subtype, but it can be deduced if necessary from the length and the
12977 -- low bound.
12979 if Is_OK_Static_Expression (Low_Bound) then
12980 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
12982 -- If the lower bound is not static we create a range for the string
12983 -- literal, using the index type and the known length of the literal.
12984 -- If the length is 1, then the upper bound is set to a mere copy of
12985 -- the lower bound; or else, if the index type is a signed integer,
12986 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
12987 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
12989 else
12990 declare
12991 Length : constant Nat := String_Length (Strval (N));
12992 Index_List : constant List_Id := New_List;
12993 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
12994 Array_Subtype : Entity_Id;
12995 Drange : Node_Id;
12996 High_Bound : Node_Id;
12997 Index : Node_Id;
12998 Index_Subtype : Entity_Id;
13000 begin
13001 if Length = 1 then
13002 High_Bound := New_Copy_Tree (Low_Bound);
13004 elsif Is_Signed_Integer_Type (Index_Type) then
13005 High_Bound :=
13006 Make_Op_Add (Loc,
13007 Left_Opnd => New_Copy_Tree (Low_Bound),
13008 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
13010 else
13011 High_Bound :=
13012 Make_Attribute_Reference (Loc,
13013 Attribute_Name => Name_Val,
13014 Prefix =>
13015 New_Occurrence_Of (Index_Type, Loc),
13016 Expressions => New_List (
13017 Make_Op_Add (Loc,
13018 Left_Opnd =>
13019 Make_Attribute_Reference (Loc,
13020 Attribute_Name => Name_Pos,
13021 Prefix =>
13022 New_Occurrence_Of (Index_Type, Loc),
13023 Expressions =>
13024 New_List (New_Copy_Tree (Low_Bound))),
13025 Right_Opnd =>
13026 Make_Integer_Literal (Loc, Length - 1))));
13027 end if;
13029 if Is_Integer_Type (Index_Type) then
13030 Set_String_Literal_Low_Bound
13031 (Subtype_Id, Make_Integer_Literal (Loc, 1));
13033 else
13034 -- If the index type is an enumeration type, build bounds
13035 -- expression with attributes.
13037 Set_String_Literal_Low_Bound
13038 (Subtype_Id,
13039 Make_Attribute_Reference (Loc,
13040 Attribute_Name => Name_First,
13041 Prefix =>
13042 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
13043 end if;
13045 Analyze_And_Resolve
13046 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
13048 -- Build bona fide subtype for the string, and wrap it in an
13049 -- unchecked conversion, because the back end expects the
13050 -- String_Literal_Subtype to have a static lower bound.
13052 Index_Subtype :=
13053 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13054 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
13055 Set_Scalar_Range (Index_Subtype, Drange);
13056 Set_Parent (Drange, N);
13057 Analyze_And_Resolve (Drange, Index_Type);
13059 -- In this context, the Index_Type may already have a constraint,
13060 -- so use common base type on string subtype. The base type may
13061 -- be used when generating attributes of the string, for example
13062 -- in the context of a slice assignment.
13064 Set_Etype (Index_Subtype, Base_Type (Index_Type));
13065 Set_Size_Info (Index_Subtype, Index_Type);
13066 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13068 Array_Subtype := Create_Itype (E_Array_Subtype, N);
13070 Index := New_Occurrence_Of (Index_Subtype, Loc);
13071 Set_Etype (Index, Index_Subtype);
13072 Append (Index, Index_List);
13074 Set_First_Index (Array_Subtype, Index);
13075 Set_Etype (Array_Subtype, Base_Type (Typ));
13076 Set_Is_Constrained (Array_Subtype, True);
13078 Rewrite (N, Unchecked_Convert_To (Array_Subtype, N));
13079 Set_Etype (N, Array_Subtype);
13080 end;
13081 end if;
13082 end Set_String_Literal_Subtype;
13084 ------------------------------
13085 -- Simplify_Type_Conversion --
13086 ------------------------------
13088 procedure Simplify_Type_Conversion (N : Node_Id) is
13089 begin
13090 if Nkind (N) = N_Type_Conversion then
13091 declare
13092 Operand : constant Node_Id := Expression (N);
13093 Target_Typ : constant Entity_Id := Etype (N);
13094 Opnd_Typ : constant Entity_Id := Etype (Operand);
13096 begin
13097 -- Special processing if the conversion is the expression of a
13098 -- Rounding or Truncation attribute reference. In this case we
13099 -- replace:
13101 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
13103 -- by
13105 -- ityp (x)
13107 -- with the Float_Truncate flag set to False or True respectively,
13108 -- which is more efficient. We reuse Rounding for Machine_Rounding
13109 -- as System.Fat_Gen, which is a permissible behavior.
13111 if Is_Floating_Point_Type (Opnd_Typ)
13112 and then
13113 (Is_Integer_Type (Target_Typ)
13114 or else (Is_Fixed_Point_Type (Target_Typ)
13115 and then Conversion_OK (N)))
13116 and then Nkind (Operand) = N_Attribute_Reference
13117 and then Attribute_Name (Operand) in Name_Rounding
13118 | Name_Machine_Rounding
13119 | Name_Truncation
13120 then
13121 declare
13122 Truncate : constant Boolean :=
13123 Attribute_Name (Operand) = Name_Truncation;
13124 begin
13125 Rewrite (Operand,
13126 Relocate_Node (First (Expressions (Operand))));
13127 Set_Float_Truncate (N, Truncate);
13128 end;
13130 -- Special processing for the conversion of an integer literal to
13131 -- a dynamic type: we first convert the literal to the root type
13132 -- and then convert the result to the target type, the goal being
13133 -- to avoid doing range checks in universal integer.
13135 elsif Is_Integer_Type (Target_Typ)
13136 and then not Is_Generic_Type (Root_Type (Target_Typ))
13137 and then Nkind (Operand) = N_Integer_Literal
13138 and then Opnd_Typ = Universal_Integer
13139 then
13140 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
13141 Analyze_And_Resolve (Operand);
13143 -- If the expression is a conversion to universal integer of an
13144 -- an expression with an integer type, then we can eliminate the
13145 -- intermediate conversion to universal integer.
13147 elsif Nkind (Operand) = N_Type_Conversion
13148 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
13149 and then Is_Integer_Type (Etype (Expression (Operand)))
13150 then
13151 Rewrite (Operand, Relocate_Node (Expression (Operand)));
13152 Analyze_And_Resolve (Operand);
13153 end if;
13154 end;
13155 end if;
13156 end Simplify_Type_Conversion;
13158 ------------------------------
13159 -- Try_User_Defined_Literal --
13160 ------------------------------
13162 function Try_User_Defined_Literal
13163 (N : Node_Id;
13164 Typ : Entity_Id) return Boolean
13166 begin
13167 if Has_Applicable_User_Defined_Literal (N, Typ) then
13168 return True;
13170 elsif Nkind (N) = N_If_Expression then
13171 -- Both dependent expressions must have the same type as the context
13173 declare
13174 Condition : constant Node_Id := First (Expressions (N));
13175 Then_Expr : constant Node_Id := Next (Condition);
13176 Else_Expr : constant Node_Id := Next (Then_Expr);
13178 begin
13179 if Has_Applicable_User_Defined_Literal (Then_Expr, Typ) then
13180 Resolve (Else_Expr, Typ);
13181 Analyze_And_Resolve (N, Typ);
13182 return True;
13184 elsif Has_Applicable_User_Defined_Literal (Else_Expr, Typ) then
13185 Resolve (Then_Expr, Typ);
13186 Analyze_And_Resolve (N, Typ);
13187 return True;
13188 end if;
13189 end;
13191 elsif Nkind (N) = N_Case_Expression then
13192 -- All dependent expressions must have the same type as the context
13194 declare
13195 Alt : Node_Id;
13197 begin
13198 Alt := First (Alternatives (N));
13200 while Present (Alt) loop
13201 if Has_Applicable_User_Defined_Literal (Expression (Alt), Typ)
13202 then
13203 declare
13204 Other_Alt : Node_Id;
13206 begin
13207 Other_Alt := First (Alternatives (N));
13209 while Present (Other_Alt) loop
13210 if Other_Alt /= Alt then
13211 Resolve (Expression (Other_Alt), Typ);
13212 end if;
13214 Next (Other_Alt);
13215 end loop;
13217 Analyze_And_Resolve (N, Typ);
13218 return True;
13219 end;
13220 end if;
13222 Next (Alt);
13223 end loop;
13224 end;
13225 end if;
13227 return False;
13228 end Try_User_Defined_Literal;
13230 -------------------------------------------
13231 -- Try_User_Defined_Literal_For_Operator --
13232 -------------------------------------------
13234 function Try_User_Defined_Literal_For_Operator
13235 (N : Node_Id;
13236 Typ : Entity_Id) return Boolean
13238 begin
13239 if Nkind (N) in N_Op_Add
13240 | N_Op_Divide
13241 | N_Op_Mod
13242 | N_Op_Multiply
13243 | N_Op_Rem
13244 | N_Op_Subtract
13245 then
13246 -- Both operands must have the same type as the context
13247 -- (ignoring for now fixed-point and exponentiation ops).
13249 if Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13250 or else (Nkind (Left_Opnd (N)) in N_Op
13251 and then Covers (Typ, Etype (Right_Opnd (N))))
13252 then
13253 Resolve (Left_Opnd (N), Typ);
13254 Analyze_And_Resolve (N, Typ);
13255 return True;
13257 elsif Has_Applicable_User_Defined_Literal (Left_Opnd (N), Typ)
13258 or else (Nkind (Right_Opnd (N)) in N_Op
13259 and then Covers (Typ, Etype (Left_Opnd (N))))
13260 then
13261 Resolve (Right_Opnd (N), Typ);
13262 Analyze_And_Resolve (N, Typ);
13263 return True;
13264 end if;
13266 elsif Nkind (N) in N_Binary_Op then
13267 -- For other binary operators the context does not impose a type on
13268 -- the operands, but their types must match.
13270 if Nkind (Left_Opnd (N))
13271 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13272 and then
13273 Has_Applicable_User_Defined_Literal
13274 (Right_Opnd (N), Etype (Left_Opnd (N)))
13275 then
13276 Analyze_And_Resolve (N, Typ);
13277 return True;
13279 elsif Nkind (Right_Opnd (N))
13280 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13281 and then
13282 Has_Applicable_User_Defined_Literal
13283 (Left_Opnd (N), Etype (Right_Opnd (N)))
13284 then
13285 Analyze_And_Resolve (N, Typ);
13286 return True;
13287 end if;
13289 elsif Nkind (N) in N_Unary_Op
13290 and then Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13291 then
13292 Analyze_And_Resolve (N, Typ);
13293 return True;
13294 end if;
13296 return False;
13297 end Try_User_Defined_Literal_For_Operator;
13299 -----------------------------
13300 -- Unique_Fixed_Point_Type --
13301 -----------------------------
13303 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
13304 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
13305 -- Give error messages for true ambiguity. Messages are posted on node
13306 -- N, and entities T1, T2 are the possible interpretations.
13308 -----------------------
13309 -- Fixed_Point_Error --
13310 -----------------------
13312 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
13313 begin
13314 Error_Msg_N ("ambiguous universal_fixed_expression", N);
13315 Error_Msg_NE ("\\possible interpretation as}", N, T1);
13316 Error_Msg_NE ("\\possible interpretation as}", N, T2);
13317 end Fixed_Point_Error;
13319 -- Local variables
13321 ErrN : Node_Id;
13322 Item : Node_Id;
13323 Scop : Entity_Id;
13324 T1 : Entity_Id;
13325 T2 : Entity_Id;
13327 -- Start of processing for Unique_Fixed_Point_Type
13329 begin
13330 -- The operations on Duration are visible, so Duration is always a
13331 -- possible interpretation.
13333 T1 := Standard_Duration;
13335 -- Look for fixed-point types in enclosing scopes
13337 Scop := Current_Scope;
13338 while Scop /= Standard_Standard loop
13339 T2 := First_Entity (Scop);
13340 while Present (T2) loop
13341 if Is_Fixed_Point_Type (T2)
13342 and then Current_Entity (T2) = T2
13343 and then Scope (Base_Type (T2)) = Scop
13344 then
13345 if Present (T1) then
13346 Fixed_Point_Error (T1, T2);
13347 return Any_Type;
13348 else
13349 T1 := T2;
13350 end if;
13351 end if;
13353 Next_Entity (T2);
13354 end loop;
13356 Scop := Scope (Scop);
13357 end loop;
13359 -- Look for visible fixed type declarations in the context
13361 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
13362 while Present (Item) loop
13363 if Nkind (Item) = N_With_Clause then
13364 Scop := Entity (Name (Item));
13365 T2 := First_Entity (Scop);
13366 while Present (T2) loop
13367 if Is_Fixed_Point_Type (T2)
13368 and then Scope (Base_Type (T2)) = Scop
13369 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
13370 then
13371 if Present (T1) then
13372 Fixed_Point_Error (T1, T2);
13373 return Any_Type;
13374 else
13375 T1 := T2;
13376 end if;
13377 end if;
13379 Next_Entity (T2);
13380 end loop;
13381 end if;
13383 Next (Item);
13384 end loop;
13386 if Nkind (N) = N_Real_Literal then
13387 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
13389 else
13390 -- When the context is a type conversion, issue the warning on the
13391 -- expression of the conversion because it is the actual operation.
13393 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
13394 ErrN := Expression (N);
13395 else
13396 ErrN := N;
13397 end if;
13399 Error_Msg_NE
13400 ("??universal_fixed expression interpreted as }!", ErrN, T1);
13401 end if;
13403 return T1;
13404 end Unique_Fixed_Point_Type;
13406 ----------------------
13407 -- Valid_Conversion --
13408 ----------------------
13410 function Valid_Conversion
13411 (N : Node_Id;
13412 Target : Entity_Id;
13413 Operand : Node_Id;
13414 Report_Errs : Boolean := True) return Boolean
13416 Target_Type : constant Entity_Id := Base_Type (Target);
13417 Opnd_Type : Entity_Id := Etype (Operand);
13418 Inc_Ancestor : Entity_Id;
13420 function Conversion_Check
13421 (Valid : Boolean;
13422 Msg : String) return Boolean;
13423 -- Little routine to post Msg if Valid is False, returns Valid value
13425 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
13426 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
13428 procedure Conversion_Error_NE
13429 (Msg : String;
13430 N : Node_Or_Entity_Id;
13431 E : Node_Or_Entity_Id);
13432 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
13434 function In_Instance_Code return Boolean;
13435 -- Return True if expression is within an instance but is not in one of
13436 -- the actuals of the instantiation. Type conversions within an instance
13437 -- are not rechecked because type visibility may lead to spurious errors
13438 -- but conversions in an actual for a formal object must be checked.
13440 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13441 (Expr : Node_Id) return Boolean;
13442 -- Implicit anonymous-to-named access type conversions are not allowed
13443 -- if the "statically deeper than" relationship does not apply to the
13444 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
13445 -- We deal with most such cases elsewhere so that we can emit more
13446 -- specific error messages (e.g., if the operand is an access parameter
13447 -- or a saooaaat (stand-alone object of an anonymous access type)), but
13448 -- here is where we catch the case where the operand is an access
13449 -- discriminant selected from a dereference of another such "bad"
13450 -- conversion argument.
13452 function Valid_Tagged_Conversion
13453 (Target_Type : Entity_Id;
13454 Opnd_Type : Entity_Id) return Boolean;
13455 -- Specifically test for validity of tagged conversions
13457 function Valid_Array_Conversion return Boolean;
13458 -- Check index and component conformance, and accessibility levels if
13459 -- the component types are anonymous access types (Ada 2005).
13461 ----------------------
13462 -- Conversion_Check --
13463 ----------------------
13465 function Conversion_Check
13466 (Valid : Boolean;
13467 Msg : String) return Boolean
13469 begin
13470 if not Valid
13472 -- A generic unit has already been analyzed and we have verified
13473 -- that a particular conversion is OK in that context. Since the
13474 -- instance is reanalyzed without relying on the relationships
13475 -- established during the analysis of the generic, it is possible
13476 -- to end up with inconsistent views of private types. Do not emit
13477 -- the error message in such cases. The rest of the machinery in
13478 -- Valid_Conversion still ensures the proper compatibility of
13479 -- target and operand types.
13481 and then not In_Instance_Code
13482 then
13483 Conversion_Error_N (Msg, Operand);
13484 end if;
13486 return Valid;
13487 end Conversion_Check;
13489 ------------------------
13490 -- Conversion_Error_N --
13491 ------------------------
13493 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
13494 begin
13495 if Report_Errs then
13496 Error_Msg_N (Msg, N);
13497 end if;
13498 end Conversion_Error_N;
13500 -------------------------
13501 -- Conversion_Error_NE --
13502 -------------------------
13504 procedure Conversion_Error_NE
13505 (Msg : String;
13506 N : Node_Or_Entity_Id;
13507 E : Node_Or_Entity_Id)
13509 begin
13510 if Report_Errs then
13511 Error_Msg_NE (Msg, N, E);
13512 end if;
13513 end Conversion_Error_NE;
13515 ----------------------
13516 -- In_Instance_Code --
13517 ----------------------
13519 function In_Instance_Code return Boolean is
13520 Par : Node_Id;
13522 begin
13523 if not In_Instance then
13524 return False;
13526 else
13527 Par := Parent (N);
13528 while Present (Par) loop
13530 -- The expression is part of an actual object if it appears in
13531 -- the generated object declaration in the instance.
13533 if Nkind (Par) = N_Object_Declaration
13534 and then Present (Corresponding_Generic_Association (Par))
13535 then
13536 return False;
13538 else
13539 exit when
13540 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13541 or else Nkind (Par) in N_Subprogram_Call
13542 or else Nkind (Par) in N_Declaration;
13543 end if;
13545 Par := Parent (Par);
13546 end loop;
13548 -- Otherwise the expression appears within the instantiated unit
13550 return True;
13551 end if;
13552 end In_Instance_Code;
13554 --------------------------------------------------
13555 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13556 --------------------------------------------------
13558 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13559 (Expr : Node_Id) return Boolean
13561 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13562 pragma Assert (Is_Access_Type (Exp_Type));
13564 Associated_Node : Node_Id;
13565 Deref_Prefix : Node_Id;
13566 begin
13567 if not Is_Anonymous_Access_Type (Exp_Type) then
13568 return False;
13569 end if;
13571 pragma Assert (Is_Itype (Exp_Type));
13572 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13574 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13575 return False; -- not the type of an access discriminant
13576 end if;
13578 -- return False if Expr not of form <prefix>.all.Some_Component
13580 if Nkind (Expr) /= N_Selected_Component
13581 or else Nkind (Prefix (Expr)) /= N_Explicit_Dereference
13582 then
13583 -- conditional expressions, declare expressions ???
13584 return False;
13585 end if;
13587 Deref_Prefix := Prefix (Prefix (Expr));
13588 Exp_Type := Base_Type (Etype (Deref_Prefix));
13590 -- The "statically deeper relationship" does not apply
13591 -- to generic formal access types, so a prefix of such
13592 -- a type is a "bad" prefix.
13594 if Is_Generic_Formal (Exp_Type) then
13595 return True;
13597 -- The "statically deeper relationship" does apply to
13598 -- any other named access type.
13600 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13601 return False;
13602 end if;
13604 pragma Assert (Is_Itype (Exp_Type));
13605 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13607 -- The "statically deeper relationship" applies to some
13608 -- anonymous access types and not to others. Return
13609 -- True for the cases where it does not apply. Also check
13610 -- recursively for the
13611 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13612 -- where the correct result depends on <prefix>.
13614 return Nkind (Associated_Node) in
13615 N_Procedure_Specification | -- access parameter
13616 N_Function_Specification | -- access parameter
13617 N_Object_Declaration -- saooaaat
13618 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13619 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13621 ----------------------------
13622 -- Valid_Array_Conversion --
13623 ----------------------------
13625 function Valid_Array_Conversion return Boolean is
13626 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13627 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13629 Opnd_Index : Node_Id;
13630 Opnd_Index_Type : Entity_Id;
13632 Target_Comp_Type : constant Entity_Id :=
13633 Component_Type (Target_Type);
13634 Target_Comp_Base : constant Entity_Id :=
13635 Base_Type (Target_Comp_Type);
13637 Target_Index : Node_Id;
13638 Target_Index_Type : Entity_Id;
13640 begin
13641 -- Error if wrong number of dimensions
13644 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13645 then
13646 Conversion_Error_N
13647 ("incompatible number of dimensions for conversion", Operand);
13648 return False;
13650 -- Number of dimensions matches
13652 else
13653 -- Loop through indexes of the two arrays
13655 Target_Index := First_Index (Target_Type);
13656 Opnd_Index := First_Index (Opnd_Type);
13657 while Present (Target_Index) and then Present (Opnd_Index) loop
13658 Target_Index_Type := Etype (Target_Index);
13659 Opnd_Index_Type := Etype (Opnd_Index);
13661 -- Error if index types are incompatible
13663 if not (Is_Integer_Type (Target_Index_Type)
13664 and then Is_Integer_Type (Opnd_Index_Type))
13665 and then Root_Type (Target_Index_Type)
13666 /= Root_Type (Opnd_Index_Type)
13667 then
13668 Conversion_Error_N
13669 ("incompatible index types for array conversion",
13670 Operand);
13671 return False;
13672 end if;
13674 Next_Index (Target_Index);
13675 Next_Index (Opnd_Index);
13676 end loop;
13678 -- If component types have same base type, all set
13680 if Target_Comp_Base = Opnd_Comp_Base then
13681 null;
13683 -- Here if base types of components are not the same. The only
13684 -- time this is allowed is if we have anonymous access types.
13686 -- The conversion of arrays of anonymous access types can lead
13687 -- to dangling pointers. AI-392 formalizes the accessibility
13688 -- checks that must be applied to such conversions to prevent
13689 -- out-of-scope references.
13691 elsif Ekind (Target_Comp_Base) in
13692 E_Anonymous_Access_Type
13693 | E_Anonymous_Access_Subprogram_Type
13694 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13695 and then
13696 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13697 then
13698 if Type_Access_Level (Target_Type) <
13699 Deepest_Type_Access_Level (Opnd_Type)
13700 then
13701 if In_Instance_Body then
13702 Error_Msg_Warn := SPARK_Mode /= On;
13703 Conversion_Error_N
13704 ("source array type has deeper accessibility "
13705 & "level than target<<", Operand);
13706 Conversion_Error_N ("\Program_Error [<<", Operand);
13707 Rewrite (N,
13708 Make_Raise_Program_Error (Sloc (N),
13709 Reason => PE_Accessibility_Check_Failed));
13710 Set_Etype (N, Target_Type);
13711 return False;
13713 -- Conversion not allowed because of accessibility levels
13715 else
13716 Conversion_Error_N
13717 ("source array type has deeper accessibility "
13718 & "level than target", Operand);
13719 return False;
13720 end if;
13722 else
13723 null;
13724 end if;
13726 -- All other cases where component base types do not match
13728 else
13729 Conversion_Error_N
13730 ("incompatible component types for array conversion",
13731 Operand);
13732 return False;
13733 end if;
13735 -- Check that component subtypes statically match. For numeric
13736 -- types this means that both must be either constrained or
13737 -- unconstrained. For enumeration types the bounds must match.
13738 -- All of this is checked in Subtypes_Statically_Match.
13740 if not Subtypes_Statically_Match
13741 (Target_Comp_Type, Opnd_Comp_Type)
13742 then
13743 Conversion_Error_N
13744 ("component subtypes must statically match", Operand);
13745 return False;
13746 end if;
13747 end if;
13749 return True;
13750 end Valid_Array_Conversion;
13752 -----------------------------
13753 -- Valid_Tagged_Conversion --
13754 -----------------------------
13756 function Valid_Tagged_Conversion
13757 (Target_Type : Entity_Id;
13758 Opnd_Type : Entity_Id) return Boolean
13760 begin
13761 -- Upward conversions are allowed (RM 4.6(22))
13763 if Covers (Target_Type, Opnd_Type)
13764 or else Is_Ancestor (Target_Type, Opnd_Type)
13765 then
13766 return True;
13768 -- Downward conversion are allowed if the operand is class-wide
13769 -- (RM 4.6(23)).
13771 elsif Is_Class_Wide_Type (Opnd_Type)
13772 and then Covers (Opnd_Type, Target_Type)
13773 then
13774 return True;
13776 elsif Covers (Opnd_Type, Target_Type)
13777 or else Is_Ancestor (Opnd_Type, Target_Type)
13778 then
13779 return
13780 Conversion_Check (False,
13781 "downward conversion of tagged objects not allowed");
13783 -- Ada 2005 (AI-251): A conversion is valid if the operand and target
13784 -- types are both class-wide types and the specific type associated
13785 -- with at least one of them is an interface type (RM 4.6 (23.1/2));
13786 -- at run-time a check will verify the validity of this interface
13787 -- type conversion.
13789 elsif Is_Class_Wide_Type (Target_Type)
13790 and then Is_Class_Wide_Type (Opnd_Type)
13791 and then (Is_Interface (Target_Type)
13792 or else Is_Interface (Opnd_Type))
13793 then
13794 return True;
13796 -- Report errors
13798 elsif Is_Class_Wide_Type (Target_Type)
13799 and then Is_Interface (Target_Type)
13800 and then not Is_Interface (Opnd_Type)
13801 and then not Interface_Present_In_Ancestor
13802 (Typ => Opnd_Type,
13803 Iface => Target_Type)
13804 then
13805 Error_Msg_Name_1 := Chars (Etype (Target_Type));
13806 Error_Msg_Name_2 := Chars (Opnd_Type);
13807 Conversion_Error_N
13808 ("wrong interface conversion (% is not a progenitor "
13809 & "of %)", N);
13810 return False;
13812 elsif Is_Class_Wide_Type (Opnd_Type)
13813 and then Is_Interface (Opnd_Type)
13814 and then not Is_Interface (Target_Type)
13815 and then not Interface_Present_In_Ancestor
13816 (Typ => Target_Type,
13817 Iface => Opnd_Type)
13818 then
13819 Error_Msg_Name_1 := Chars (Etype (Opnd_Type));
13820 Error_Msg_Name_2 := Chars (Target_Type);
13821 Conversion_Error_N
13822 ("wrong interface conversion (% is not a progenitor "
13823 & "of %)", N);
13825 -- Search for interface types shared between the target type and
13826 -- the operand interface type to complete the text of the error
13827 -- since the source of this error is a missing type conversion
13828 -- to such interface type.
13830 if Has_Interfaces (Target_Type) then
13831 declare
13832 Operand_Ifaces_List : Elist_Id;
13833 Operand_Iface_Elmt : Elmt_Id;
13834 Target_Ifaces_List : Elist_Id;
13835 Target_Iface_Elmt : Elmt_Id;
13836 First_Candidate : Boolean := True;
13838 begin
13839 Collect_Interfaces (Base_Type (Target_Type),
13840 Target_Ifaces_List);
13841 Collect_Interfaces (Root_Type (Base_Type (Opnd_Type)),
13842 Operand_Ifaces_List);
13844 Operand_Iface_Elmt := First_Elmt (Operand_Ifaces_List);
13845 while Present (Operand_Iface_Elmt) loop
13846 Target_Iface_Elmt := First_Elmt (Target_Ifaces_List);
13847 while Present (Target_Iface_Elmt) loop
13848 if Node (Operand_Iface_Elmt)
13849 = Node (Target_Iface_Elmt)
13850 then
13851 Error_Msg_Name_1 :=
13852 Chars (Node (Target_Iface_Elmt));
13854 if First_Candidate then
13855 First_Candidate := False;
13856 Conversion_Error_N
13857 ("\must convert to `%''Class` before downward "
13858 & "conversion", Operand);
13859 else
13860 Conversion_Error_N
13861 ("\or must convert to `%''Class` before "
13862 & "downward conversion", Operand);
13863 end if;
13864 end if;
13866 Next_Elmt (Target_Iface_Elmt);
13867 end loop;
13869 Next_Elmt (Operand_Iface_Elmt);
13870 end loop;
13871 end;
13872 end if;
13874 return False;
13876 elsif not Is_Class_Wide_Type (Target_Type)
13877 and then Is_Interface (Target_Type)
13878 then
13879 Conversion_Error_N
13880 ("wrong use of interface type in tagged conversion", N);
13881 Conversion_Error_N
13882 ("\add ''Class to the target interface type", N);
13883 return False;
13885 elsif not Is_Class_Wide_Type (Opnd_Type)
13886 and then Is_Interface (Opnd_Type)
13887 then
13888 Conversion_Error_N
13889 ("must convert to class-wide interface type before downward "
13890 & "conversion", Operand);
13891 return False;
13893 else
13894 Conversion_Error_NE
13895 ("invalid tagged conversion, not compatible with}",
13896 N, First_Subtype (Opnd_Type));
13897 return False;
13898 end if;
13899 end Valid_Tagged_Conversion;
13901 -- Start of processing for Valid_Conversion
13903 begin
13904 Check_Parameterless_Call (Operand);
13906 if Is_Overloaded (Operand) then
13907 declare
13908 I : Interp_Index;
13909 I1 : Interp_Index;
13910 It : Interp;
13911 It1 : Interp;
13912 N1 : Entity_Id;
13913 T1 : Entity_Id;
13915 begin
13916 -- Remove procedure calls, which syntactically cannot appear in
13917 -- this context, but which cannot be removed by type checking,
13918 -- because the context does not impose a type.
13920 -- The node may be labelled overloaded, but still contain only one
13921 -- interpretation because others were discarded earlier. If this
13922 -- is the case, retain the single interpretation if legal.
13924 Get_First_Interp (Operand, I, It);
13925 Opnd_Type := It.Typ;
13926 Get_Next_Interp (I, It);
13928 if Present (It.Typ)
13929 and then Opnd_Type /= Standard_Void_Type
13930 then
13931 -- More than one candidate interpretation is available
13933 Get_First_Interp (Operand, I, It);
13934 while Present (It.Typ) loop
13935 if It.Typ = Standard_Void_Type then
13936 Remove_Interp (I);
13937 end if;
13939 -- When compiling for a system where Address is of a visible
13940 -- integer type, spurious ambiguities can be produced when
13941 -- arithmetic operations have a literal operand and return
13942 -- System.Address or a descendant of it. These ambiguities
13943 -- are usually resolved by the context, but for conversions
13944 -- there is no context type and the removal of the spurious
13945 -- operations must be done explicitly here.
13947 if not Address_Is_Private
13948 and then Is_Descendant_Of_Address (It.Typ)
13949 then
13950 Remove_Interp (I);
13951 end if;
13953 Get_Next_Interp (I, It);
13954 end loop;
13955 end if;
13957 Get_First_Interp (Operand, I, It);
13958 I1 := I;
13959 It1 := It;
13961 if No (It.Typ) then
13962 Conversion_Error_N ("illegal operand in conversion", Operand);
13963 return False;
13964 end if;
13966 Get_Next_Interp (I, It);
13968 if Present (It.Typ) then
13969 N1 := It1.Nam;
13970 T1 := It1.Typ;
13971 It1 := Disambiguate (Operand, I1, I, Any_Type);
13973 if It1 = No_Interp then
13974 Conversion_Error_N
13975 ("ambiguous operand in conversion", Operand);
13977 -- If the interpretation involves a standard operator, use
13978 -- the location of the type, which may be user-defined.
13980 if Sloc (It.Nam) = Standard_Location then
13981 Error_Msg_Sloc := Sloc (It.Typ);
13982 else
13983 Error_Msg_Sloc := Sloc (It.Nam);
13984 end if;
13986 Conversion_Error_N -- CODEFIX
13987 ("\\possible interpretation#!", Operand);
13989 if Sloc (N1) = Standard_Location then
13990 Error_Msg_Sloc := Sloc (T1);
13991 else
13992 Error_Msg_Sloc := Sloc (N1);
13993 end if;
13995 Conversion_Error_N -- CODEFIX
13996 ("\\possible interpretation#!", Operand);
13998 return False;
13999 end if;
14000 end if;
14002 Set_Etype (Operand, It1.Typ);
14003 Opnd_Type := It1.Typ;
14004 end;
14005 end if;
14007 -- Deal with conversion of integer type to address if the pragma
14008 -- Allow_Integer_Address is in effect. We convert the conversion to
14009 -- an unchecked conversion in this case and we are all done.
14011 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
14012 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
14013 Analyze_And_Resolve (N, Target_Type);
14014 return True;
14015 end if;
14017 -- If we are within a child unit, check whether the type of the
14018 -- expression has an ancestor in a parent unit, in which case it
14019 -- belongs to its derivation class even if the ancestor is private.
14020 -- See RM 7.3.1 (5.2/3).
14022 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
14024 -- Numeric types
14026 if Is_Numeric_Type (Target_Type) then
14028 -- A universal fixed expression can be converted to any numeric type
14030 if Opnd_Type = Universal_Fixed then
14031 return True;
14033 -- Also no need to check when in an instance or inlined body, because
14034 -- the legality has been established when the template was analyzed.
14035 -- Furthermore, numeric conversions may occur where only a private
14036 -- view of the operand type is visible at the instantiation point.
14037 -- This results in a spurious error if we check that the operand type
14038 -- is a numeric type.
14040 -- Note: in a previous version of this unit, the following tests were
14041 -- applied only for generated code (Comes_From_Source set to False),
14042 -- but in fact the test is required for source code as well, since
14043 -- this situation can arise in source code.
14045 elsif In_Instance_Code or else In_Inlined_Body then
14046 return True;
14048 -- Otherwise we need the conversion check
14050 else
14051 return Conversion_Check
14052 (Is_Numeric_Type (Opnd_Type)
14053 or else
14054 (Present (Inc_Ancestor)
14055 and then Is_Numeric_Type (Inc_Ancestor)),
14056 "illegal operand for numeric conversion");
14057 end if;
14059 -- Array types
14061 elsif Is_Array_Type (Target_Type) then
14062 if not Is_Array_Type (Opnd_Type)
14063 or else Opnd_Type = Any_Composite
14064 or else Opnd_Type = Any_String
14065 then
14066 Conversion_Error_N
14067 ("illegal operand for array conversion", Operand);
14068 return False;
14070 else
14071 return Valid_Array_Conversion;
14072 end if;
14074 -- Ada 2005 (AI-251): Internally generated conversions of access to
14075 -- interface types added to force the displacement of the pointer to
14076 -- reference the corresponding dispatch table.
14078 elsif not Comes_From_Source (N)
14079 and then Is_Access_Type (Target_Type)
14080 and then Is_Interface (Designated_Type (Target_Type))
14081 then
14082 return True;
14084 -- Ada 2005 (AI-251): Anonymous access types where target references an
14085 -- interface type.
14087 elsif Is_Access_Type (Opnd_Type)
14088 and then Ekind (Target_Type) in
14089 E_General_Access_Type | E_Anonymous_Access_Type
14090 and then Is_Interface (Directly_Designated_Type (Target_Type))
14091 then
14092 -- Check the static accessibility rule of 4.6(17). Note that the
14093 -- check is not enforced when within an instance body, since the
14094 -- RM requires such cases to be caught at run time.
14096 -- If the operand is a rewriting of an allocator no check is needed
14097 -- because there are no accessibility issues.
14099 if Nkind (Original_Node (N)) = N_Allocator then
14100 null;
14102 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
14103 if Type_Access_Level (Opnd_Type) >
14104 Deepest_Type_Access_Level (Target_Type)
14105 then
14106 -- In an instance, this is a run-time check, but one we know
14107 -- will fail, so generate an appropriate warning. The raise
14108 -- will be generated by Expand_N_Type_Conversion.
14110 if In_Instance_Body then
14111 Error_Msg_Warn := SPARK_Mode /= On;
14112 Conversion_Error_N
14113 ("cannot convert local pointer to non-local access type<<",
14114 Operand);
14115 Conversion_Error_N ("\Program_Error [<<", Operand);
14117 else
14118 Conversion_Error_N
14119 ("cannot convert local pointer to non-local access type",
14120 Operand);
14121 return False;
14122 end if;
14124 -- Special accessibility checks are needed in the case of access
14125 -- discriminants declared for a limited type.
14127 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14128 and then not Is_Local_Anonymous_Access (Opnd_Type)
14129 then
14130 -- When the operand is a selected access discriminant the check
14131 -- needs to be made against the level of the object denoted by
14132 -- the prefix of the selected name (Accessibility_Level handles
14133 -- checking the prefix of the operand for this case).
14135 if Nkind (Operand) = N_Selected_Component
14136 and then Static_Accessibility_Level
14137 (Operand, Zero_On_Dynamic_Level)
14138 > Deepest_Type_Access_Level (Target_Type)
14139 then
14140 -- In an instance, this is a run-time check, but one we know
14141 -- will fail, so generate an appropriate warning. The raise
14142 -- will be generated by Expand_N_Type_Conversion.
14144 if In_Instance_Body then
14145 Error_Msg_Warn := SPARK_Mode /= On;
14146 Conversion_Error_N
14147 ("cannot convert access discriminant to non-local "
14148 & "access type<<", Operand);
14149 Conversion_Error_N ("\Program_Error [<<", Operand);
14151 -- Real error if not in instance body
14153 else
14154 Conversion_Error_N
14155 ("cannot convert access discriminant to non-local "
14156 & "access type", Operand);
14157 return False;
14158 end if;
14159 end if;
14161 -- The case of a reference to an access discriminant from
14162 -- within a limited type declaration (which will appear as
14163 -- a discriminal) is always illegal because the level of the
14164 -- discriminant is considered to be deeper than any (nameable)
14165 -- access type.
14167 if Is_Entity_Name (Operand)
14168 and then not Is_Local_Anonymous_Access (Opnd_Type)
14169 and then
14170 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14171 and then Present (Discriminal_Link (Entity (Operand)))
14172 then
14173 Conversion_Error_N
14174 ("discriminant has deeper accessibility level than target",
14175 Operand);
14176 return False;
14177 end if;
14178 end if;
14179 end if;
14181 return True;
14183 -- General and anonymous access types
14185 elsif Ekind (Target_Type) in
14186 E_General_Access_Type | E_Anonymous_Access_Type
14187 and then
14188 Conversion_Check
14189 (Is_Access_Type (Opnd_Type)
14190 and then
14191 Ekind (Opnd_Type) not in
14192 E_Access_Subprogram_Type |
14193 E_Access_Protected_Subprogram_Type,
14194 "must be an access-to-object type")
14195 then
14196 if Is_Access_Constant (Opnd_Type)
14197 and then not Is_Access_Constant (Target_Type)
14198 then
14199 Conversion_Error_N
14200 ("access-to-constant operand type not allowed", Operand);
14201 return False;
14202 end if;
14204 -- Check the static accessibility rule of 4.6(17). Note that the
14205 -- check is not enforced when within an instance body, since the RM
14206 -- requires such cases to be caught at run time.
14208 if Ekind (Target_Type) /= E_Anonymous_Access_Type
14209 or else Is_Local_Anonymous_Access (Target_Type)
14210 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
14211 N_Object_Declaration
14212 then
14213 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
14214 -- conversions from an anonymous access type to a named general
14215 -- access type. Such conversions are not allowed in the case of
14216 -- access parameters and stand-alone objects of an anonymous
14217 -- access type. The implicit conversion case is recognized by
14218 -- testing that Comes_From_Source is False and that it's been
14219 -- rewritten. The Comes_From_Source test isn't sufficient because
14220 -- nodes in inlined calls to predefined library routines can have
14221 -- Comes_From_Source set to False. (Is there a better way to test
14222 -- for implicit conversions???).
14224 -- Do not treat a rewritten 'Old attribute reference like other
14225 -- rewrite substitutions. This makes a difference, for example,
14226 -- in the case where we are generating the expansion of a
14227 -- membership test of the form
14228 -- Saooaaat'Old in Named_Access_Type
14229 -- because in this case Valid_Conversion needs to return True
14230 -- (otherwise the expansion will be False - see the call site
14231 -- in exp_ch4.adb).
14233 if Ada_Version >= Ada_2012
14234 and then not Comes_From_Source (N)
14235 and then Is_Rewrite_Substitution (N)
14236 and then not Is_Attribute_Old (Original_Node (N))
14237 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
14238 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
14239 then
14240 if Is_Itype (Opnd_Type) then
14242 -- When applying restriction No_Dynamic_Accessibility_Check,
14243 -- implicit conversions are allowed when the operand type is
14244 -- not deeper than the target type.
14246 if No_Dynamic_Accessibility_Checks_Enabled (N) then
14247 if Type_Access_Level (Opnd_Type)
14248 > Deepest_Type_Access_Level (Target_Type)
14249 then
14250 Conversion_Error_N
14251 ("operand has deeper level than target", Operand);
14252 end if;
14254 -- Implicit conversions aren't allowed for objects of an
14255 -- anonymous access type, since such objects have nonstatic
14256 -- levels in Ada 2012.
14258 elsif Nkind (Associated_Node_For_Itype (Opnd_Type))
14259 = N_Object_Declaration
14260 then
14261 Conversion_Error_N
14262 ("implicit conversion of stand-alone anonymous "
14263 & "access object not allowed", Operand);
14264 return False;
14266 -- Implicit conversions aren't allowed for anonymous access
14267 -- parameters. We exclude anonymous access results as well
14268 -- as universal_access "=".
14270 elsif not Is_Local_Anonymous_Access (Opnd_Type)
14271 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
14272 N_Function_Specification |
14273 N_Procedure_Specification
14274 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
14275 then
14276 Conversion_Error_N
14277 ("implicit conversion of anonymous access parameter "
14278 & "not allowed", Operand);
14279 return False;
14281 -- Detect access discriminant values that are illegal
14282 -- implicit anonymous-to-named access conversion operands.
14284 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
14285 then
14286 Conversion_Error_N
14287 ("implicit conversion of anonymous access value "
14288 & "not allowed", Operand);
14289 return False;
14291 -- In other cases, the level of the operand's type must be
14292 -- statically less deep than that of the target type, else
14293 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
14295 elsif Type_Access_Level (Opnd_Type) >
14296 Deepest_Type_Access_Level (Target_Type)
14297 then
14298 Conversion_Error_N
14299 ("implicit conversion of anonymous access value "
14300 & "violates accessibility", Operand);
14301 return False;
14302 end if;
14303 end if;
14305 -- Check if the operand is deeper than the target type, taking
14306 -- care to avoid the case where we are converting a result of a
14307 -- function returning an anonymous access type since the "master
14308 -- of the call" would be target type of the conversion unless
14309 -- the target type is anonymous access as well - see RM 3.10.2
14310 -- (10.3/3).
14312 -- Note that when the restriction No_Dynamic_Accessibility_Checks
14313 -- is in effect wei also want to proceed with the conversion check
14314 -- described above.
14316 elsif Type_Access_Level (Opnd_Type, Assoc_Ent => Operand)
14317 > Deepest_Type_Access_Level (Target_Type)
14318 and then (Nkind (Associated_Node_For_Itype (Opnd_Type))
14319 /= N_Function_Specification
14320 or else Ekind (Target_Type) in Anonymous_Access_Kind
14321 or else No_Dynamic_Accessibility_Checks_Enabled (N))
14323 -- Check we are not in a return value ???
14325 and then (not In_Return_Value (N)
14326 or else
14327 Nkind (Associated_Node_For_Itype (Target_Type))
14328 = N_Component_Declaration)
14329 then
14330 -- In an instance, this is a run-time check, but one we know
14331 -- will fail, so generate an appropriate warning. The raise
14332 -- will be generated by Expand_N_Type_Conversion.
14334 if In_Instance_Body then
14335 Error_Msg_Warn := SPARK_Mode /= On;
14336 Conversion_Error_N
14337 ("cannot convert local pointer to non-local access type<<",
14338 Operand);
14339 Conversion_Error_N ("\Program_Error [<<", Operand);
14341 -- If not in an instance body, this is a real error
14343 else
14344 -- Avoid generation of spurious error message
14346 if not Error_Posted (N) then
14347 Conversion_Error_N
14348 ("cannot convert local pointer to non-local access type",
14349 Operand);
14350 end if;
14352 return False;
14353 end if;
14355 -- Special accessibility checks are needed in the case of access
14356 -- discriminants declared for a limited type.
14358 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14359 and then not Is_Local_Anonymous_Access (Opnd_Type)
14360 then
14361 -- When the operand is a selected access discriminant the check
14362 -- needs to be made against the level of the object denoted by
14363 -- the prefix of the selected name (Accessibility_Level handles
14364 -- checking the prefix of the operand for this case).
14366 if Nkind (Operand) = N_Selected_Component
14367 and then Static_Accessibility_Level
14368 (Operand, Zero_On_Dynamic_Level)
14369 > Deepest_Type_Access_Level (Target_Type)
14370 then
14371 -- In an instance, this is a run-time check, but one we know
14372 -- will fail, so generate an appropriate warning. The raise
14373 -- will be generated by Expand_N_Type_Conversion.
14375 if In_Instance_Body then
14376 Error_Msg_Warn := SPARK_Mode /= On;
14377 Conversion_Error_N
14378 ("cannot convert access discriminant to non-local "
14379 & "access type<<", Operand);
14380 Conversion_Error_N ("\Program_Error [<<", Operand);
14382 -- If not in an instance body, this is a real error
14384 else
14385 Conversion_Error_N
14386 ("cannot convert access discriminant to non-local "
14387 & "access type", Operand);
14388 return False;
14389 end if;
14390 end if;
14392 -- The case of a reference to an access discriminant from
14393 -- within a limited type declaration (which will appear as
14394 -- a discriminal) is always illegal because the level of the
14395 -- discriminant is considered to be deeper than any (nameable)
14396 -- access type.
14398 if Is_Entity_Name (Operand)
14399 and then
14400 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14401 and then Present (Discriminal_Link (Entity (Operand)))
14402 then
14403 Conversion_Error_N
14404 ("discriminant has deeper accessibility level than target",
14405 Operand);
14406 return False;
14407 end if;
14408 end if;
14409 end if;
14411 -- In the presence of limited_with clauses we have to use nonlimited
14412 -- views, if available.
14414 Check_Limited : declare
14415 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
14416 -- Helper function to handle limited views
14418 --------------------------
14419 -- Full_Designated_Type --
14420 --------------------------
14422 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
14423 Desig : constant Entity_Id := Designated_Type (T);
14425 begin
14426 -- Handle the limited view of a type
14428 if From_Limited_With (Desig)
14429 and then Has_Non_Limited_View (Desig)
14430 then
14431 return Available_View (Desig);
14432 else
14433 return Desig;
14434 end if;
14435 end Full_Designated_Type;
14437 -- Local Declarations
14439 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
14440 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
14442 Same_Base : constant Boolean :=
14443 Base_Type (Target) = Base_Type (Opnd);
14445 -- Start of processing for Check_Limited
14447 begin
14448 if Is_Tagged_Type (Target) then
14449 return Valid_Tagged_Conversion (Target, Opnd);
14451 else
14452 if not Same_Base then
14453 Conversion_Error_NE
14454 ("target designated type not compatible with }",
14455 N, Base_Type (Opnd));
14456 return False;
14458 -- Ada 2005 AI-384: legality rule is symmetric in both
14459 -- designated types. The conversion is legal (with possible
14460 -- constraint check) if either designated type is
14461 -- unconstrained.
14463 elsif Subtypes_Statically_Match (Target, Opnd)
14464 or else
14465 (Has_Discriminants (Target)
14466 and then
14467 (not Is_Constrained (Opnd)
14468 or else not Is_Constrained (Target)))
14469 then
14470 -- Special case, if Value_Size has been used to make the
14471 -- sizes different, the conversion is not allowed even
14472 -- though the subtypes statically match.
14474 if Known_Static_RM_Size (Target)
14475 and then Known_Static_RM_Size (Opnd)
14476 and then RM_Size (Target) /= RM_Size (Opnd)
14477 then
14478 Conversion_Error_NE
14479 ("target designated subtype not compatible with }",
14480 N, Opnd);
14481 Conversion_Error_NE
14482 ("\because sizes of the two designated subtypes differ",
14483 N, Opnd);
14484 return False;
14486 -- Normal case where conversion is allowed
14488 else
14489 return True;
14490 end if;
14492 else
14493 Error_Msg_NE
14494 ("target designated subtype not compatible with }",
14495 N, Opnd);
14496 return False;
14497 end if;
14498 end if;
14499 end Check_Limited;
14501 -- Access to subprogram types. If the operand is an access parameter,
14502 -- the type has a deeper accessibility that any master, and cannot be
14503 -- assigned. We must make an exception if the conversion is part of an
14504 -- assignment and the target is the return object of an extended return
14505 -- statement, because in that case the accessibility check takes place
14506 -- after the return.
14508 elsif Is_Access_Subprogram_Type (Target_Type)
14510 -- Note: this test of Opnd_Type is there to prevent entering this
14511 -- branch in the case of a remote access to subprogram type, which
14512 -- is internally represented as an E_Record_Type.
14514 and then Is_Access_Type (Opnd_Type)
14515 then
14516 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
14517 and then Is_Entity_Name (Operand)
14518 and then Ekind (Entity (Operand)) = E_In_Parameter
14519 and then
14520 (Nkind (Parent (N)) /= N_Assignment_Statement
14521 or else not Is_Entity_Name (Name (Parent (N)))
14522 or else not Is_Return_Object (Entity (Name (Parent (N)))))
14523 then
14524 Conversion_Error_N
14525 ("illegal attempt to store anonymous access to subprogram",
14526 Operand);
14527 Conversion_Error_N
14528 ("\value has deeper accessibility than any master "
14529 & "(RM 3.10.2 (13))",
14530 Operand);
14532 Error_Msg_NE
14533 ("\use named access type for& instead of access parameter",
14534 Operand, Entity (Operand));
14535 end if;
14537 -- Check that the designated types are subtype conformant
14539 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
14540 Old_Id => Designated_Type (Opnd_Type),
14541 Err_Loc => N);
14543 -- Check the static accessibility rule of 4.6(20)
14545 if Type_Access_Level (Opnd_Type) >
14546 Deepest_Type_Access_Level (Target_Type)
14547 then
14548 Conversion_Error_N
14549 ("operand type has deeper accessibility level than target",
14550 Operand);
14552 -- Check that if the operand type is declared in a generic body,
14553 -- then the target type must be declared within that same body
14554 -- (enforces last sentence of 4.6(20)).
14556 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
14557 declare
14558 O_Gen : constant Node_Id :=
14559 Enclosing_Generic_Body (Opnd_Type);
14561 T_Gen : Node_Id;
14563 begin
14564 T_Gen := Enclosing_Generic_Body (Target_Type);
14565 while Present (T_Gen) and then T_Gen /= O_Gen loop
14566 T_Gen := Enclosing_Generic_Body (T_Gen);
14567 end loop;
14569 if T_Gen /= O_Gen then
14570 Conversion_Error_N
14571 ("target type must be declared in same generic body "
14572 & "as operand type", N);
14573 end if;
14574 end;
14575 end if;
14577 -- Check that the strub modes are compatible.
14578 -- We wish to reject explicit conversions only for
14579 -- incompatible modes.
14581 return Conversion_Check
14582 (Compatible_Strub_Modes
14583 (Designated_Type (Target_Type),
14584 Designated_Type (Opnd_Type)),
14585 "incompatible `strub` modes");
14587 -- Remote access to subprogram types
14589 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
14590 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
14591 then
14592 -- It is valid to convert from one RAS type to another provided
14593 -- that their specification statically match.
14595 -- Note: at this point, remote access to subprogram types have been
14596 -- expanded to their E_Record_Type representation, and we need to
14597 -- go back to the original access type definition using the
14598 -- Corresponding_Remote_Type attribute in order to check that the
14599 -- designated profiles match.
14601 pragma Assert (Ekind (Target_Type) = E_Record_Type);
14602 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
14604 Check_Subtype_Conformant
14605 (New_Id =>
14606 Designated_Type (Corresponding_Remote_Type (Target_Type)),
14607 Old_Id =>
14608 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14609 Err_Loc =>
14612 -- Check that the strub modes are compatible.
14613 -- We wish to reject explicit conversions only for
14614 -- incompatible modes.
14616 return Conversion_Check
14617 (Compatible_Strub_Modes
14618 (Designated_Type (Target_Type),
14619 Designated_Type (Opnd_Type)),
14620 "incompatible `strub` modes");
14622 -- If it was legal in the generic, it's legal in the instance
14624 elsif In_Instance_Body then
14625 return True;
14627 -- If both are tagged types, check legality of view conversions
14629 elsif Is_Tagged_Type (Target_Type)
14630 and then
14631 Is_Tagged_Type (Opnd_Type)
14632 then
14633 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14635 -- Types derived from the same root type are convertible
14637 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14638 return True;
14640 -- In an instance or an inlined body, there may be inconsistent views of
14641 -- the same type, or of types derived from a common root.
14643 elsif (In_Instance or In_Inlined_Body)
14644 and then
14645 Root_Type (Underlying_Type (Target_Type)) =
14646 Root_Type (Underlying_Type (Opnd_Type))
14647 then
14648 return True;
14650 -- Special check for common access type error case
14652 elsif Ekind (Target_Type) = E_Access_Type
14653 and then Is_Access_Type (Opnd_Type)
14654 then
14655 Conversion_Error_N ("target type must be general access type!", N);
14656 Conversion_Error_NE -- CODEFIX
14657 ("\add ALL to }!", N, Target_Type);
14658 return False;
14660 -- Here we have a real conversion error
14662 else
14663 -- Check for missing regular with_clause when only a limited view of
14664 -- target is available.
14666 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14667 Conversion_Error_NE
14668 ("invalid conversion, not compatible with limited view of }",
14669 N, Opnd_Type);
14670 Conversion_Error_NE
14671 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14673 elsif Is_Access_Type (Opnd_Type)
14674 and then From_Limited_With (Designated_Type (Opnd_Type))
14675 and then In_Package_Body
14676 then
14677 Conversion_Error_NE
14678 ("invalid conversion, not compatible with }", N, Opnd_Type);
14679 Conversion_Error_NE
14680 ("\add with_clause for& to current unit!",
14681 N, Scope (Designated_Type (Opnd_Type)));
14683 else
14684 Conversion_Error_NE
14685 ("invalid conversion, not compatible with }", N, Opnd_Type);
14686 end if;
14688 return False;
14689 end if;
14690 end Valid_Conversion;
14692 end Sem_Res;