[Ada] Rename Returns_On_Secondary_Stack into Needs_Secondary_Stack
[official-gcc.git] / gcc / ada / sem_res.adb
blob7d595eb23e88996bf591f881e89e4bda2f1b3213
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-2022, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Debug_A; use Debug_A;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Inline; use Inline;
44 with Itypes; use Itypes;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
50 with Opt; use Opt;
51 with Output; use Output;
52 with Par_SCO; use Par_SCO;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aggr; use Sem_Aggr;
58 with Sem_Attr; use Sem_Attr;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Case; use Sem_Case;
61 with Sem_Cat; use Sem_Cat;
62 with Sem_Ch3; use Sem_Ch3;
63 with Sem_Ch4; use Sem_Ch4;
64 with Sem_Ch6; use Sem_Ch6;
65 with Sem_Ch8; use Sem_Ch8;
66 with Sem_Ch13; use Sem_Ch13;
67 with Sem_Dim; use Sem_Dim;
68 with Sem_Disp; use Sem_Disp;
69 with Sem_Dist; use Sem_Dist;
70 with Sem_Elab; use Sem_Elab;
71 with Sem_Elim; use Sem_Elim;
72 with Sem_Eval; use Sem_Eval;
73 with Sem_Intr; use Sem_Intr;
74 with Sem_Mech; use Sem_Mech;
75 with Sem_Type; use Sem_Type;
76 with Sem_Util; use Sem_Util;
77 with Sem_Warn; use Sem_Warn;
78 with Sinfo; use Sinfo;
79 with Sinfo.Nodes; use Sinfo.Nodes;
80 with Sinfo.Utils; use Sinfo.Utils;
81 with Sinfo.CN; use Sinfo.CN;
82 with Snames; use Snames;
83 with Stand; use Stand;
84 with Stringt; use Stringt;
85 with Strub; use Strub;
86 with Style; use Style;
87 with Targparm; use Targparm;
88 with Tbuild; use Tbuild;
89 with Uintp; use Uintp;
90 with Urealp; use Urealp;
92 package body Sem_Res is
94 -----------------------
95 -- Local Subprograms --
96 -----------------------
98 -- Second pass (top-down) type checking and overload resolution procedures
99 -- Typ is the type required by context. These procedures propagate the
100 -- type information recursively to the descendants of N. If the node is not
101 -- overloaded, its Etype is established in the first pass. If overloaded,
102 -- the Resolve routines set the correct type. For arithmetic operators, the
103 -- Etype is the base type of the context.
105 -- Note that Resolve_Attribute is separated off in Sem_Attr
107 function Has_Applicable_User_Defined_Literal
108 (N : Node_Id;
109 Typ : Entity_Id) return Boolean;
110 -- If N is a literal or a named number, check whether Typ
111 -- has a user-defined literal aspect that can apply to N.
112 -- If present, replace N with a call to the corresponding
113 -- function and return True.
115 procedure Check_Discriminant_Use (N : Node_Id);
116 -- Enforce the restrictions on the use of discriminants when constraining
117 -- a component of a discriminated type (record or concurrent type).
119 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
120 -- Given a node for an operator associated with type T, check that the
121 -- operator is visible. Operators all of whose operands are universal must
122 -- be checked for visibility during resolution because their type is not
123 -- determinable based on their operands.
125 procedure Check_Fully_Declared_Prefix
126 (Typ : Entity_Id;
127 Pref : Node_Id);
128 -- Check that the type of the prefix of a dereference is not incomplete
130 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
131 -- Given a call node, Call, which is known to occur immediately within the
132 -- subprogram being called, determines whether it is a detectable case of
133 -- an infinite recursion, and if so, outputs appropriate messages. Returns
134 -- True if an infinite recursion is detected, and False otherwise.
136 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
137 -- N is the node for a logical operator. If the operator is predefined, and
138 -- the root type of the operands is Standard.Boolean, then a check is made
139 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
140 -- the style check for Style_Check_Boolean_And_Or.
142 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
143 -- N is either an indexed component or a selected component. This function
144 -- returns true if the prefix denotes an atomic object that has an address
145 -- clause (the case in which we may want to issue a warning).
147 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
148 -- Determine whether E is an access type declared by an access declaration,
149 -- and not an (anonymous) allocator type.
151 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
152 -- Utility to check whether the entity for an operator is a predefined
153 -- operator, in which case the expression is left as an operator in the
154 -- tree (else it is rewritten into a call). An instance of an intrinsic
155 -- conversion operation may be given an operator name, but is not treated
156 -- like an operator. Note that an operator that is an imported back-end
157 -- builtin has convention Intrinsic, but is expected to be rewritten into
158 -- a call, so such an operator is not treated as predefined by this
159 -- predicate.
161 procedure Preanalyze_And_Resolve
162 (N : Node_Id;
163 T : Entity_Id;
164 With_Freezing : Boolean);
165 -- Subsidiary of public versions of Preanalyze_And_Resolve.
167 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
168 -- If a default expression in entry call N depends on the discriminants
169 -- of the task, it must be replaced with a reference to the discriminant
170 -- of the task being called.
172 procedure Resolve_Op_Concat_Arg
173 (N : Node_Id;
174 Arg : Node_Id;
175 Typ : Entity_Id;
176 Is_Comp : Boolean);
177 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
178 -- concatenation operator. The operand is either of the array type or of
179 -- the component type. If the operand is an aggregate, and the component
180 -- type is composite, this is ambiguous if component type has aggregates.
182 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
183 -- Does the first part of the work of Resolve_Op_Concat
185 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
186 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
187 -- has been resolved. See Resolve_Op_Concat for details.
189 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
215 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
216 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
217 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
218 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
219 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
220 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
221 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
222 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
223 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
224 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
225 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
227 function Operator_Kind
228 (Op_Name : Name_Id;
229 Is_Binary : Boolean) return Node_Kind;
230 -- Utility to map the name of an operator into the corresponding Node. Used
231 -- by other node rewriting procedures.
233 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
234 -- Resolve actuals of call, and add default expressions for missing ones.
235 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
236 -- called subprogram.
238 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
239 -- Called from Resolve_Call, when the prefix denotes an entry or element
240 -- of entry family. Actuals are resolved as for subprograms, and the node
241 -- is rebuilt as an entry call. Also called for protected operations. Typ
242 -- is the context type, which is used when the operation is a protected
243 -- function with no arguments, and the return value is indexed.
245 procedure Resolve_Implicit_Dereference (P : Node_Id);
246 -- Called when P is the prefix of an indexed component, or of a selected
247 -- component, or of a slice. If P is of an access type, we unconditionally
248 -- rewrite it as an explicit dereference. This ensures that the expander
249 -- and the code generator have a fully explicit tree to work with.
251 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
252 -- A call to a user-defined intrinsic operator is rewritten as a call to
253 -- the corresponding predefined operator, with suitable conversions. Note
254 -- that this applies only for intrinsic operators that denote predefined
255 -- operators, not ones that are intrinsic imports of back-end builtins.
257 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
258 -- Ditto, for arithmetic unary operators
260 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
261 -- If an operator node resolves to a call to a user-defined operator,
262 -- rewrite the node as a function call.
264 procedure Make_Call_Into_Operator
265 (N : Node_Id;
266 Typ : Entity_Id;
267 Op_Id : Entity_Id);
268 -- Inverse transformation: if an operator is given in functional notation,
269 -- then after resolving the node, transform into an operator node, so that
270 -- operands are resolved properly. Recall that predefined operators do not
271 -- have a full signature and special resolution rules apply.
273 procedure Rewrite_Renamed_Operator
274 (N : Node_Id;
275 Op : Entity_Id;
276 Typ : Entity_Id);
277 -- An operator can rename another, e.g. in an instantiation. In that
278 -- case, the proper operator node must be constructed and resolved.
280 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
281 -- The String_Literal_Subtype is built for all strings that are not
282 -- operands of a static concatenation operation. If the argument is not
283 -- a N_String_Literal node, then the call has no effect.
285 procedure Set_Slice_Subtype (N : Node_Id);
286 -- Build subtype of array type, with the range specified by the slice
288 procedure Simplify_Type_Conversion (N : Node_Id);
289 -- Called after N has been resolved and evaluated, but before range checks
290 -- have been applied. This rewrites the conversion into a simpler form.
292 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
293 -- A universal_fixed expression in an universal context is unambiguous if
294 -- there is only one applicable fixed point type. Determining whether there
295 -- is only one requires a search over all visible entities, and happens
296 -- only in very pathological cases (see 6115-006).
298 function Try_User_Defined_Literal
299 (N : Node_Id;
300 Typ : Entity_Id) return Boolean;
301 -- If an operator node has a literal operand, check whether the type
302 -- of the context, or the type of the other operand has a user-defined
303 -- literal aspect that can be applied to the literal to resolve the node.
304 -- If such aspect exists, replace literal with a call to the
305 -- corresponding function and return True, return false otherwise.
307 -------------------------
308 -- Ambiguous_Character --
309 -------------------------
311 procedure Ambiguous_Character (C : Node_Id) is
312 E : Entity_Id;
314 begin
315 if Nkind (C) = N_Character_Literal then
316 Error_Msg_N ("ambiguous character literal", C);
318 -- First the ones in Standard
320 Error_Msg_N ("\\possible interpretation: Character!", C);
321 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
323 -- Include Wide_Wide_Character in Ada 2005 mode
325 if Ada_Version >= Ada_2005 then
326 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
327 end if;
329 -- Now any other types that match
331 E := Current_Entity (C);
332 while Present (E) loop
333 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
334 E := Homonym (E);
335 end loop;
336 end if;
337 end Ambiguous_Character;
339 -------------------------
340 -- Analyze_And_Resolve --
341 -------------------------
343 procedure Analyze_And_Resolve (N : Node_Id) is
344 begin
345 Analyze (N);
346 Resolve (N);
347 end Analyze_And_Resolve;
349 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
350 begin
351 Analyze (N);
352 Resolve (N, Typ);
353 end Analyze_And_Resolve;
355 -- Versions with check(s) suppressed
357 procedure Analyze_And_Resolve
358 (N : Node_Id;
359 Typ : Entity_Id;
360 Suppress : Check_Id)
362 Scop : constant Entity_Id := Current_Scope;
364 begin
365 if Suppress = All_Checks then
366 declare
367 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
368 begin
369 Scope_Suppress.Suppress := (others => True);
370 Analyze_And_Resolve (N, Typ);
371 Scope_Suppress.Suppress := Sva;
372 end;
374 else
375 declare
376 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
377 begin
378 Scope_Suppress.Suppress (Suppress) := True;
379 Analyze_And_Resolve (N, Typ);
380 Scope_Suppress.Suppress (Suppress) := Svg;
381 end;
382 end if;
384 if Current_Scope /= Scop
385 and then Scope_Is_Transient
386 then
387 -- This can only happen if a transient scope was created for an inner
388 -- expression, which will be removed upon completion of the analysis
389 -- of an enclosing construct. The transient scope must have the
390 -- suppress status of the enclosing environment, not of this Analyze
391 -- call.
393 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
394 Scope_Suppress;
395 end if;
396 end Analyze_And_Resolve;
398 procedure Analyze_And_Resolve
399 (N : Node_Id;
400 Suppress : Check_Id)
402 Scop : constant Entity_Id := Current_Scope;
404 begin
405 if Suppress = All_Checks then
406 declare
407 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
408 begin
409 Scope_Suppress.Suppress := (others => True);
410 Analyze_And_Resolve (N);
411 Scope_Suppress.Suppress := Sva;
412 end;
414 else
415 declare
416 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
417 begin
418 Scope_Suppress.Suppress (Suppress) := True;
419 Analyze_And_Resolve (N);
420 Scope_Suppress.Suppress (Suppress) := Svg;
421 end;
422 end if;
424 if Current_Scope /= Scop and then Scope_Is_Transient then
425 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
426 Scope_Suppress;
427 end if;
428 end Analyze_And_Resolve;
430 -------------------------------------
431 -- Has_Applicable_User_Defined_Literal --
432 -------------------------------------
434 function Has_Applicable_User_Defined_Literal
435 (N : Node_Id;
436 Typ : Entity_Id) return Boolean
438 Loc : constant Source_Ptr := Sloc (N);
439 Literal_Aspect_Map :
440 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
441 (N_Integer_Literal => Aspect_Integer_Literal,
442 N_Real_Literal => Aspect_Real_Literal,
443 N_String_Literal => Aspect_String_Literal);
445 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
446 (E_Named_Integer => Aspect_Integer_Literal,
447 E_Named_Real => Aspect_Real_Literal);
449 Lit_Aspect : Aspect_Id;
451 Callee : Entity_Id;
452 Name : Node_Id;
453 Param1 : Node_Id;
454 Param2 : Node_Id;
455 Params : List_Id;
456 Call : Node_Id;
457 Expr : Node_Id;
459 begin
460 if (Nkind (N) in N_Numeric_Or_String_Literal
461 and then Present
462 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
463 or else
464 (Nkind (N) = N_Identifier
465 and then Is_Named_Number (Entity (N))
466 and then
467 Present
468 (Find_Aspect
469 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
470 then
471 Lit_Aspect :=
472 (if Nkind (N) = N_Identifier
473 then Named_Number_Aspect_Map (Ekind (Entity (N)))
474 else Literal_Aspect_Map (Nkind (N)));
475 Callee :=
476 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
477 Name := Make_Identifier (Loc, Chars (Callee));
479 if Is_Derived_Type (Typ)
480 and then Is_Tagged_Type (Typ)
481 and then Base_Type (Etype (Callee)) /= Base_Type (Typ)
482 then
483 Callee :=
484 Corresponding_Primitive_Op
485 (Ancestor_Op => Callee,
486 Descendant_Type => Base_Type (Typ));
487 end if;
489 -- Handle an identifier that denotes a named number.
491 if Nkind (N) = N_Identifier then
492 Expr := Expression (Declaration_Node (Entity (N)));
494 if Ekind (Entity (N)) = E_Named_Integer then
495 UI_Image (Expr_Value (Expr), Decimal);
496 Start_String;
497 Store_String_Chars
498 (UI_Image_Buffer (1 .. UI_Image_Length));
499 Param1 := Make_String_Literal (Loc, End_String);
500 Params := New_List (Param1);
502 else
503 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
504 Start_String;
506 if UR_Is_Negative (Expr_Value_R (Expr)) then
507 Store_String_Chars ("-");
508 end if;
510 Store_String_Chars
511 (UI_Image_Buffer (1 .. UI_Image_Length));
512 Param1 := Make_String_Literal (Loc, End_String);
514 -- Note: Set_Etype is called below on Param1
516 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
517 Start_String;
518 Store_String_Chars
519 (UI_Image_Buffer (1 .. UI_Image_Length));
520 Param2 := Make_String_Literal (Loc, End_String);
521 Set_Etype (Param2, Standard_String);
523 Params := New_List (Param1, Param2);
525 if Present (Related_Expression (Callee)) then
526 Callee := Related_Expression (Callee);
527 else
528 Error_Msg_NE
529 ("cannot resolve & for a named real", N, Callee);
530 return False;
531 end if;
532 end if;
534 elsif Nkind (N) = N_String_Literal then
535 Param1 := Make_String_Literal (Loc, Strval (N));
536 Params := New_List (Param1);
538 else
539 Param1 :=
540 Make_String_Literal
541 (Loc, String_From_Numeric_Literal (N));
542 Params := New_List (Param1);
543 end if;
545 Call :=
546 Make_Function_Call
547 (Sloc => Loc,
548 Name => Name,
549 Parameter_Associations => Params);
551 Set_Entity (Name, Callee);
552 Set_Is_Overloaded (Name, False);
554 if Lit_Aspect = Aspect_String_Literal then
555 Set_Etype (Param1, Standard_Wide_Wide_String);
556 else
557 Set_Etype (Param1, Standard_String);
558 end if;
560 Set_Etype (Call, Etype (Callee));
562 -- Conversion not needed if the result type of the call is class-wide
563 -- or if the result type matches the context type.
565 if not Is_Class_Wide_Type (Typ)
566 and then Base_Type (Etype (Call)) /= Base_Type (Typ)
567 then
568 -- Conversion may be needed in case of an inherited
569 -- aspect of a derived type. For a null extension, we
570 -- use a null extension aggregate instead because the
571 -- downward type conversion would be illegal.
573 if Is_Null_Extension_Of
574 (Descendant => Typ,
575 Ancestor => Etype (Call))
576 then
577 Call := Make_Extension_Aggregate (Loc,
578 Ancestor_Part => Call,
579 Null_Record_Present => True);
580 else
581 Call := Convert_To (Typ, Call);
582 end if;
583 end if;
585 Rewrite (N, Call);
587 Analyze_And_Resolve (N, Typ);
588 return True;
589 else
590 return False;
591 end if;
592 end Has_Applicable_User_Defined_Literal;
594 ----------------------------
595 -- Check_Discriminant_Use --
596 ----------------------------
598 procedure Check_Discriminant_Use (N : Node_Id) is
599 PN : constant Node_Id := Parent (N);
600 Disc : constant Entity_Id := Entity (N);
601 P : Node_Id;
602 D : Node_Id;
604 begin
605 -- Any use in a spec-expression is legal
607 if In_Spec_Expression then
608 null;
610 elsif Nkind (PN) = N_Range then
612 -- Discriminant cannot be used to constrain a scalar type
614 P := Parent (PN);
616 if Nkind (P) = N_Range_Constraint
617 and then Nkind (Parent (P)) = N_Subtype_Indication
618 and then Nkind (Parent (Parent (P))) = N_Component_Definition
619 then
620 Error_Msg_N ("discriminant cannot constrain scalar type", N);
622 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
624 -- The following check catches the unusual case where a
625 -- discriminant appears within an index constraint that is part
626 -- of a larger expression within a constraint on a component,
627 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
628 -- check case of record components, and note that a similar check
629 -- should also apply in the case of discriminant constraints
630 -- below. ???
632 -- Note that the check for N_Subtype_Declaration below is to
633 -- detect the valid use of discriminants in the constraints of a
634 -- subtype declaration when this subtype declaration appears
635 -- inside the scope of a record type (which is syntactically
636 -- illegal, but which may be created as part of derived type
637 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
638 -- for more info.
640 if Ekind (Current_Scope) = E_Record_Type
641 and then Scope (Disc) = Current_Scope
642 and then not
643 (Nkind (Parent (P)) = N_Subtype_Indication
644 and then
645 Nkind (Parent (Parent (P))) in N_Component_Definition
646 | N_Subtype_Declaration
647 and then Paren_Count (N) = 0)
648 then
649 Error_Msg_N
650 ("discriminant must appear alone in component constraint", N);
651 return;
652 end if;
654 -- Detect a common error:
656 -- type R (D : Positive := 100) is record
657 -- Name : String (1 .. D);
658 -- end record;
660 -- The default value causes an object of type R to be allocated
661 -- with room for Positive'Last characters. The RM does not mandate
662 -- the allocation of the maximum size, but that is what GNAT does
663 -- so we should warn the programmer that there is a problem.
665 Check_Large : declare
666 SI : Node_Id;
667 T : Entity_Id;
668 TB : Node_Id;
669 CB : Entity_Id;
671 function Large_Storage_Type (T : Entity_Id) return Boolean;
672 -- Return True if type T has a large enough range that any
673 -- array whose index type covered the whole range of the type
674 -- would likely raise Storage_Error.
676 ------------------------
677 -- Large_Storage_Type --
678 ------------------------
680 function Large_Storage_Type (T : Entity_Id) return Boolean is
681 begin
682 -- The type is considered large if its bounds are known at
683 -- compile time and if it requires at least as many bits as
684 -- a Positive to store the possible values.
686 return Compile_Time_Known_Value (Type_Low_Bound (T))
687 and then Compile_Time_Known_Value (Type_High_Bound (T))
688 and then
689 Minimum_Size (T, Biased => True) >=
690 RM_Size (Standard_Positive);
691 end Large_Storage_Type;
693 -- Start of processing for Check_Large
695 begin
696 -- Check that the Disc has a large range
698 if not Large_Storage_Type (Etype (Disc)) then
699 goto No_Danger;
700 end if;
702 -- If the enclosing type is limited, we allocate only the
703 -- default value, not the maximum, and there is no need for
704 -- a warning.
706 if Is_Limited_Type (Scope (Disc)) then
707 goto No_Danger;
708 end if;
710 -- Check that it is the high bound
712 if N /= High_Bound (PN)
713 or else No (Discriminant_Default_Value (Disc))
714 then
715 goto No_Danger;
716 end if;
718 -- Check the array allows a large range at this bound. First
719 -- find the array
721 SI := Parent (P);
723 if Nkind (SI) /= N_Subtype_Indication then
724 goto No_Danger;
725 end if;
727 T := Entity (Subtype_Mark (SI));
729 if not Is_Array_Type (T) then
730 goto No_Danger;
731 end if;
733 -- Next, find the dimension
735 TB := First_Index (T);
736 CB := First (Constraints (P));
737 while True
738 and then Present (TB)
739 and then Present (CB)
740 and then CB /= PN
741 loop
742 Next_Index (TB);
743 Next (CB);
744 end loop;
746 if CB /= PN then
747 goto No_Danger;
748 end if;
750 -- Now, check the dimension has a large range
752 if not Large_Storage_Type (Etype (TB)) then
753 goto No_Danger;
754 end if;
756 -- Warn about the danger
758 Error_Msg_N
759 ("??creation of & object may raise Storage_Error!",
760 Scope (Disc));
762 <<No_Danger>>
763 null;
765 end Check_Large;
766 end if;
768 -- Legal case is in index or discriminant constraint
770 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
771 | N_Discriminant_Association
772 then
773 if Paren_Count (N) > 0 then
774 Error_Msg_N
775 ("discriminant in constraint must appear alone", N);
777 elsif Nkind (N) = N_Expanded_Name
778 and then Comes_From_Source (N)
779 then
780 Error_Msg_N
781 ("discriminant must appear alone as a direct name", N);
782 end if;
784 return;
786 -- Otherwise, context is an expression. It should not be within (i.e. a
787 -- subexpression of) a constraint for a component.
789 else
790 D := PN;
791 P := Parent (PN);
792 while Nkind (P) not in
793 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
794 loop
795 D := P;
796 P := Parent (P);
797 exit when No (P);
798 end loop;
800 -- If the discriminant is used in an expression that is a bound of a
801 -- scalar type, an Itype is created and the bounds are attached to
802 -- its range, not to the original subtype indication. Such use is of
803 -- course a double fault.
805 if (Nkind (P) = N_Subtype_Indication
806 and then Nkind (Parent (P)) in N_Component_Definition
807 | N_Derived_Type_Definition
808 and then D = Constraint (P))
810 -- The constraint itself may be given by a subtype indication,
811 -- rather than by a more common discrete range.
813 or else (Nkind (P) = N_Subtype_Indication
814 and then
815 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
816 or else Nkind (P) = N_Entry_Declaration
817 or else Nkind (D) = N_Defining_Identifier
818 then
819 Error_Msg_N
820 ("discriminant in constraint must appear alone", N);
821 end if;
822 end if;
823 end Check_Discriminant_Use;
825 --------------------------------
826 -- Check_For_Visible_Operator --
827 --------------------------------
829 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
830 begin
831 if Comes_From_Source (N)
832 and then not Is_Visible_Operator (Original_Node (N), T)
833 and then not Error_Posted (N)
834 then
835 Error_Msg_NE -- CODEFIX
836 ("operator for} is not directly visible!", N, First_Subtype (T));
837 Error_Msg_N -- CODEFIX
838 ("use clause would make operation legal!", N);
839 end if;
840 end Check_For_Visible_Operator;
842 ---------------------------------
843 -- Check_Fully_Declared_Prefix --
844 ---------------------------------
846 procedure Check_Fully_Declared_Prefix
847 (Typ : Entity_Id;
848 Pref : Node_Id)
850 begin
851 -- Check that the designated type of the prefix of a dereference is
852 -- not an incomplete type. This cannot be done unconditionally, because
853 -- dereferences of private types are legal in default expressions. This
854 -- case is taken care of in Check_Fully_Declared, called below. There
855 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
857 -- This consideration also applies to similar checks for allocators,
858 -- qualified expressions, and type conversions.
860 -- An additional exception concerns other per-object expressions that
861 -- are not directly related to component declarations, in particular
862 -- representation pragmas for tasks. These will be per-object
863 -- expressions if they depend on discriminants or some global entity.
864 -- If the task has access discriminants, the designated type may be
865 -- incomplete at the point the expression is resolved. This resolution
866 -- takes place within the body of the initialization procedure, where
867 -- the discriminant is replaced by its discriminal.
869 if Is_Entity_Name (Pref)
870 and then Ekind (Entity (Pref)) = E_In_Parameter
871 then
872 null;
874 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
875 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
876 -- Analyze_Object_Renaming, and Freeze_Entity.
878 elsif Ada_Version >= Ada_2005
879 and then Is_Entity_Name (Pref)
880 and then Is_Access_Type (Etype (Pref))
881 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
882 E_Incomplete_Type
883 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
884 then
885 null;
886 else
887 Check_Fully_Declared (Typ, Parent (Pref));
888 end if;
889 end Check_Fully_Declared_Prefix;
891 ------------------------------
892 -- Check_Infinite_Recursion --
893 ------------------------------
895 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
896 function Enclosing_Declaration_Or_Statement (N : Node_Id) return Node_Id;
897 -- Return the nearest enclosing declaration or statement that houses
898 -- arbitrary node N.
900 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
901 -- Determine whether call N invokes the related enclosing subprogram
902 -- with actuals that differ from the subprogram's formals.
904 function Is_Conditional_Statement (N : Node_Id) return Boolean;
905 -- Determine whether arbitrary node N denotes a conditional construct
907 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
908 -- Determine whether arbitrary node N denotes a control flow statement
909 -- or a construct that may contains such a statement.
911 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
912 -- Determine whether arbitrary node N appears immediately within the
913 -- statements of an entry or subprogram body.
915 function Is_Raise_Idiom (N : Node_Id) return Boolean;
916 -- Determine whether arbitrary node N appears immediately within the
917 -- body of an entry or subprogram, and is preceded by a single raise
918 -- statement.
920 function Is_Raise_Statement (N : Node_Id) return Boolean;
921 -- Determine whether arbitrary node N denotes a raise statement
923 function Is_Sole_Statement (N : Node_Id) return Boolean;
924 -- Determine whether arbitrary node N is the sole source statement in
925 -- the body of the enclosing subprogram.
927 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
928 -- Determine whether arbitrary node N is preceded by a control flow
929 -- statement.
931 function Within_Conditional_Statement (N : Node_Id) return Boolean;
932 -- Determine whether arbitrary node N appears within a conditional
933 -- construct.
935 ----------------------------------------
936 -- Enclosing_Declaration_Or_Statement --
937 ----------------------------------------
939 function Enclosing_Declaration_Or_Statement
940 (N : Node_Id) return Node_Id
942 Par : Node_Id;
944 begin
945 Par := N;
946 while Present (Par) loop
947 if Is_Declaration (Par) or else Is_Statement (Par) then
948 return Par;
950 -- Prevent the search from going too far
952 elsif Is_Body_Or_Package_Declaration (Par) then
953 exit;
954 end if;
956 Par := Parent (Par);
957 end loop;
959 return N;
960 end Enclosing_Declaration_Or_Statement;
962 --------------------------------------
963 -- Invoked_With_Different_Arguments --
964 --------------------------------------
966 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
967 Subp : constant Entity_Id := Entity (Name (N));
969 Actual : Node_Id;
970 Formal : Entity_Id;
972 begin
973 -- Determine whether the formals of the invoked subprogram are not
974 -- used as actuals in the call.
976 Actual := First_Actual (Call);
977 Formal := First_Formal (Subp);
978 while Present (Actual) and then Present (Formal) loop
980 -- The current actual does not match the current formal
982 if not (Is_Entity_Name (Actual)
983 and then Entity (Actual) = Formal)
984 then
985 return True;
986 end if;
988 Next_Actual (Actual);
989 Next_Formal (Formal);
990 end loop;
992 return False;
993 end Invoked_With_Different_Arguments;
995 ------------------------------
996 -- Is_Conditional_Statement --
997 ------------------------------
999 function Is_Conditional_Statement (N : Node_Id) return Boolean is
1000 begin
1001 return
1002 Nkind (N) in N_And_Then
1003 | N_Case_Expression
1004 | N_Case_Statement
1005 | N_If_Expression
1006 | N_If_Statement
1007 | N_Or_Else;
1008 end Is_Conditional_Statement;
1010 -------------------------------
1011 -- Is_Control_Flow_Statement --
1012 -------------------------------
1014 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
1015 begin
1016 -- It is assumed that all statements may affect the control flow in
1017 -- some way. A raise statement may be expanded into a non-statement
1018 -- node.
1020 return Is_Statement (N) or else Is_Raise_Statement (N);
1021 end Is_Control_Flow_Statement;
1023 --------------------------------
1024 -- Is_Immediately_Within_Body --
1025 --------------------------------
1027 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
1028 HSS : constant Node_Id := Parent (N);
1030 begin
1031 return
1032 Nkind (HSS) = N_Handled_Sequence_Of_Statements
1033 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
1034 and then Is_List_Member (N)
1035 and then List_Containing (N) = Statements (HSS);
1036 end Is_Immediately_Within_Body;
1038 --------------------
1039 -- Is_Raise_Idiom --
1040 --------------------
1042 function Is_Raise_Idiom (N : Node_Id) return Boolean is
1043 Raise_Stmt : Node_Id;
1044 Stmt : Node_Id;
1046 begin
1047 if Is_Immediately_Within_Body (N) then
1049 -- Assume that no raise statement has been seen yet
1051 Raise_Stmt := Empty;
1053 -- Examine the statements preceding the input node, skipping
1054 -- internally-generated constructs.
1056 Stmt := Prev (N);
1057 while Present (Stmt) loop
1059 -- Multiple raise statements violate the idiom
1061 if Is_Raise_Statement (Stmt) then
1062 if Present (Raise_Stmt) then
1063 return False;
1064 end if;
1066 Raise_Stmt := Stmt;
1068 elsif Comes_From_Source (Stmt) then
1069 exit;
1070 end if;
1072 Stmt := Prev (Stmt);
1073 end loop;
1075 -- At this point the node must be preceded by a raise statement,
1076 -- and the raise statement has to be the sole statement within
1077 -- the enclosing entry or subprogram body.
1079 return
1080 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
1081 end if;
1083 return False;
1084 end Is_Raise_Idiom;
1086 ------------------------
1087 -- Is_Raise_Statement --
1088 ------------------------
1090 function Is_Raise_Statement (N : Node_Id) return Boolean is
1091 begin
1092 -- A raise statement may be transfomed into a Raise_xxx_Error node
1094 return
1095 Nkind (N) = N_Raise_Statement
1096 or else Nkind (N) in N_Raise_xxx_Error;
1097 end Is_Raise_Statement;
1099 -----------------------
1100 -- Is_Sole_Statement --
1101 -----------------------
1103 function Is_Sole_Statement (N : Node_Id) return Boolean is
1104 Stmt : Node_Id;
1106 begin
1107 -- The input node appears within the statements of an entry or
1108 -- subprogram body. Examine the statements preceding the node.
1110 if Is_Immediately_Within_Body (N) then
1111 Stmt := Prev (N);
1113 while Present (Stmt) loop
1115 -- The statement is preceded by another statement or a source
1116 -- construct. This indicates that the node does not appear by
1117 -- itself.
1119 if Is_Control_Flow_Statement (Stmt)
1120 or else Comes_From_Source (Stmt)
1121 then
1122 return False;
1123 end if;
1125 Stmt := Prev (Stmt);
1126 end loop;
1128 return True;
1129 end if;
1131 -- The input node is within a construct nested inside the entry or
1132 -- subprogram body.
1134 return False;
1135 end Is_Sole_Statement;
1137 ----------------------------------------
1138 -- Preceded_By_Control_Flow_Statement --
1139 ----------------------------------------
1141 function Preceded_By_Control_Flow_Statement
1142 (N : Node_Id) return Boolean
1144 Stmt : Node_Id;
1146 begin
1147 if Is_List_Member (N) then
1148 Stmt := Prev (N);
1150 -- Examine the statements preceding the input node
1152 while Present (Stmt) loop
1153 if Is_Control_Flow_Statement (Stmt) then
1154 return True;
1155 end if;
1157 Stmt := Prev (Stmt);
1158 end loop;
1160 return False;
1161 end if;
1163 -- Assume that the node is part of some control flow statement
1165 return True;
1166 end Preceded_By_Control_Flow_Statement;
1168 ----------------------------------
1169 -- Within_Conditional_Statement --
1170 ----------------------------------
1172 function Within_Conditional_Statement (N : Node_Id) return Boolean is
1173 Stmt : Node_Id;
1175 begin
1176 Stmt := Parent (N);
1177 while Present (Stmt) loop
1178 if Is_Conditional_Statement (Stmt) then
1179 return True;
1181 -- Prevent the search from going too far
1183 elsif Is_Body_Or_Package_Declaration (Stmt) then
1184 exit;
1185 end if;
1187 Stmt := Parent (Stmt);
1188 end loop;
1190 return False;
1191 end Within_Conditional_Statement;
1193 -- Local variables
1195 Call_Context : constant Node_Id :=
1196 Enclosing_Declaration_Or_Statement (Call);
1198 -- Start of processing for Check_Infinite_Recursion
1200 begin
1201 -- The call is assumed to be safe when the enclosing subprogram is
1202 -- invoked with actuals other than its formals.
1204 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1205 -- begin
1206 -- ...
1207 -- Proc (A1, A2, ..., AN);
1208 -- ...
1209 -- end Proc;
1211 if Invoked_With_Different_Arguments (Call) then
1212 return False;
1214 -- The call is assumed to be safe when the invocation of the enclosing
1215 -- subprogram depends on a conditional statement.
1217 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1218 -- begin
1219 -- ...
1220 -- if Some_Condition then
1221 -- Proc (F1, F2, ..., FN);
1222 -- end if;
1223 -- ...
1224 -- end Proc;
1226 elsif Within_Conditional_Statement (Call) then
1227 return False;
1229 -- The context of the call is assumed to be safe when the invocation of
1230 -- the enclosing subprogram is preceded by some control flow statement.
1232 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1233 -- begin
1234 -- ...
1235 -- if Some_Condition then
1236 -- ...
1237 -- end if;
1238 -- ...
1239 -- Proc (F1, F2, ..., FN);
1240 -- ...
1241 -- end Proc;
1243 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1244 return False;
1246 -- Detect an idiom where the context of the call is preceded by a single
1247 -- raise statement.
1249 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1250 -- begin
1251 -- raise ...;
1252 -- Proc (F1, F2, ..., FN);
1253 -- end Proc;
1255 elsif Is_Raise_Idiom (Call_Context) then
1256 return False;
1257 end if;
1259 -- At this point it is certain that infinite recursion will take place
1260 -- as long as the call is executed. Detect a case where the context of
1261 -- the call is the sole source statement within the subprogram body.
1263 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1264 -- begin
1265 -- Proc (F1, F2, ..., FN);
1266 -- end Proc;
1268 -- Install an explicit raise to prevent the infinite recursion.
1270 if Is_Sole_Statement (Call_Context) then
1271 Error_Msg_Warn := SPARK_Mode /= On;
1272 Error_Msg_N ("!infinite recursion<<", Call);
1273 Error_Msg_N ("\!Storage_Error [<<", Call);
1275 Insert_Action (Call,
1276 Make_Raise_Storage_Error (Sloc (Call),
1277 Reason => SE_Infinite_Recursion));
1279 -- Otherwise infinite recursion could take place, considering other flow
1280 -- control constructs such as gotos, exit statements, etc.
1282 else
1283 Error_Msg_Warn := SPARK_Mode /= On;
1284 Error_Msg_N ("!possible infinite recursion<<", Call);
1285 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1286 end if;
1288 return True;
1289 end Check_Infinite_Recursion;
1291 ---------------------------------------
1292 -- Check_No_Direct_Boolean_Operators --
1293 ---------------------------------------
1295 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1296 begin
1297 if Scope (Entity (N)) = Standard_Standard
1298 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1299 then
1300 -- Restriction only applies to original source code
1302 if Comes_From_Source (N) then
1303 Check_Restriction (No_Direct_Boolean_Operators, N);
1304 end if;
1305 end if;
1307 -- Do style check (but skip if in instance, error is on template)
1309 if Style_Check then
1310 if not In_Instance then
1311 Check_Boolean_Operator (N);
1312 end if;
1313 end if;
1314 end Check_No_Direct_Boolean_Operators;
1316 ------------------------------
1317 -- Check_Parameterless_Call --
1318 ------------------------------
1320 procedure Check_Parameterless_Call (N : Node_Id) is
1321 Nam : Node_Id;
1323 function Prefix_Is_Access_Subp return Boolean;
1324 -- If the prefix is of an access_to_subprogram type, the node must be
1325 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1326 -- interpretations are access to subprograms.
1328 ---------------------------
1329 -- Prefix_Is_Access_Subp --
1330 ---------------------------
1332 function Prefix_Is_Access_Subp return Boolean is
1333 I : Interp_Index;
1334 It : Interp;
1336 begin
1337 -- If the context is an attribute reference that can apply to
1338 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1340 if Nkind (Parent (N)) = N_Attribute_Reference
1341 and then Attribute_Name (Parent (N))
1342 in Name_Address | Name_Code_Address | Name_Access
1343 then
1344 return False;
1345 end if;
1347 if not Is_Overloaded (N) then
1348 return
1349 Ekind (Etype (N)) = E_Subprogram_Type
1350 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1351 else
1352 Get_First_Interp (N, I, It);
1353 while Present (It.Typ) loop
1354 if Ekind (It.Typ) /= E_Subprogram_Type
1355 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1356 then
1357 return False;
1358 end if;
1360 Get_Next_Interp (I, It);
1361 end loop;
1363 return True;
1364 end if;
1365 end Prefix_Is_Access_Subp;
1367 -- Start of processing for Check_Parameterless_Call
1369 begin
1370 -- Defend against junk stuff if errors already detected
1372 if Total_Errors_Detected /= 0 then
1373 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1374 return;
1375 elsif Nkind (N) in N_Has_Chars
1376 and then not Is_Valid_Name (Chars (N))
1377 then
1378 return;
1379 end if;
1381 Require_Entity (N);
1382 end if;
1384 -- If the context expects a value, and the name is a procedure, this is
1385 -- most likely a missing 'Access. Don't try to resolve the parameterless
1386 -- call, error will be caught when the outer call is analyzed.
1388 if Is_Entity_Name (N)
1389 and then Ekind (Entity (N)) = E_Procedure
1390 and then not Is_Overloaded (N)
1391 and then
1392 Nkind (Parent (N)) in N_Parameter_Association
1393 | N_Function_Call
1394 | N_Procedure_Call_Statement
1395 then
1396 return;
1397 end if;
1399 -- Rewrite as call if overloadable entity that is (or could be, in the
1400 -- overloaded case) a function call. If we know for sure that the entity
1401 -- is an enumeration literal, we do not rewrite it.
1403 -- If the entity is the name of an operator, it cannot be a call because
1404 -- operators cannot have default parameters. In this case, this must be
1405 -- a string whose contents coincide with an operator name. Set the kind
1406 -- of the node appropriately.
1408 if (Is_Entity_Name (N)
1409 and then Nkind (N) /= N_Operator_Symbol
1410 and then Is_Overloadable (Entity (N))
1411 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1412 or else Is_Overloaded (N)))
1414 -- Rewrite as call if it is an explicit dereference of an expression of
1415 -- a subprogram access type, and the subprogram type is not that of a
1416 -- procedure or entry.
1418 or else
1419 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1421 -- Rewrite as call if it is a selected component which is a function,
1422 -- this is the case of a call to a protected function (which may be
1423 -- overloaded with other protected operations).
1425 or else
1426 (Nkind (N) = N_Selected_Component
1427 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1428 or else
1429 (Ekind (Entity (Selector_Name (N))) in
1430 E_Entry | E_Procedure
1431 and then Is_Overloaded (Selector_Name (N)))))
1433 -- If one of the above three conditions is met, rewrite as call. Apply
1434 -- the rewriting only once.
1436 then
1437 if Nkind (Parent (N)) /= N_Function_Call
1438 or else N /= Name (Parent (N))
1439 then
1441 -- This may be a prefixed call that was not fully analyzed, e.g.
1442 -- an actual in an instance.
1444 if Ada_Version >= Ada_2005
1445 and then Nkind (N) = N_Selected_Component
1446 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1447 then
1448 Analyze_Selected_Component (N);
1450 if Nkind (N) /= N_Selected_Component then
1451 return;
1452 end if;
1453 end if;
1455 -- The node is the name of the parameterless call. Preserve its
1456 -- descendants, which may be complex expressions.
1458 Nam := Relocate_Node (N);
1460 -- If overloaded, overload set belongs to new copy
1462 Save_Interps (N, Nam);
1464 -- Change node to parameterless function call (note that the
1465 -- Parameter_Associations associations field is left set to Empty,
1466 -- its normal default value since there are no parameters)
1468 Change_Node (N, N_Function_Call);
1469 Set_Name (N, Nam);
1470 Set_Sloc (N, Sloc (Nam));
1471 Analyze_Call (N);
1472 end if;
1474 elsif Nkind (N) = N_Parameter_Association then
1475 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1477 elsif Nkind (N) = N_Operator_Symbol then
1478 Set_Etype (N, Empty);
1479 Set_Entity (N, Empty);
1480 Set_Is_Overloaded (N, False);
1481 Change_Operator_Symbol_To_String_Literal (N);
1482 Set_Etype (N, Any_String);
1483 end if;
1484 end Check_Parameterless_Call;
1486 --------------------------------
1487 -- Is_Atomic_Ref_With_Address --
1488 --------------------------------
1490 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1491 Pref : constant Node_Id := Prefix (N);
1493 begin
1494 if not Is_Entity_Name (Pref) then
1495 return False;
1497 else
1498 declare
1499 Pent : constant Entity_Id := Entity (Pref);
1500 Ptyp : constant Entity_Id := Etype (Pent);
1501 begin
1502 return not Is_Access_Type (Ptyp)
1503 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1504 and then Present (Address_Clause (Pent));
1505 end;
1506 end if;
1507 end Is_Atomic_Ref_With_Address;
1509 -----------------------------
1510 -- Is_Definite_Access_Type --
1511 -----------------------------
1513 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1514 Btyp : constant Entity_Id := Base_Type (E);
1515 begin
1516 return Ekind (Btyp) = E_Access_Type
1517 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1518 and then Comes_From_Source (Btyp));
1519 end Is_Definite_Access_Type;
1521 ----------------------
1522 -- Is_Predefined_Op --
1523 ----------------------
1525 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1526 begin
1527 -- Predefined operators are intrinsic subprograms
1529 if not Is_Intrinsic_Subprogram (Nam) then
1530 return False;
1531 end if;
1533 -- A call to a back-end builtin is never a predefined operator
1535 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1536 return False;
1537 end if;
1539 return not Is_Generic_Instance (Nam)
1540 and then Chars (Nam) in Any_Operator_Name
1541 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1542 end Is_Predefined_Op;
1544 -----------------------------
1545 -- Make_Call_Into_Operator --
1546 -----------------------------
1548 procedure Make_Call_Into_Operator
1549 (N : Node_Id;
1550 Typ : Entity_Id;
1551 Op_Id : Entity_Id)
1553 Op_Name : constant Name_Id := Chars (Op_Id);
1554 Act1 : Node_Id := First_Actual (N);
1555 Act2 : Node_Id := Next_Actual (Act1);
1556 Error : Boolean := False;
1557 Func : constant Entity_Id := Entity (Name (N));
1558 Is_Binary : constant Boolean := Present (Act2);
1559 Op_Node : Node_Id;
1560 Opnd_Type : Entity_Id := Empty;
1561 Orig_Type : Entity_Id := Empty;
1562 Pack : Entity_Id;
1564 type Kind_Test is access function (E : Entity_Id) return Boolean;
1566 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1567 -- If the operand is not universal, and the operator is given by an
1568 -- expanded name, verify that the operand has an interpretation with a
1569 -- type defined in the given scope of the operator.
1571 function Type_In_P (Test : Kind_Test) return Entity_Id;
1572 -- Find a type of the given class in package Pack that contains the
1573 -- operator.
1575 ---------------------------
1576 -- Operand_Type_In_Scope --
1577 ---------------------------
1579 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1580 Nod : constant Node_Id := Right_Opnd (Op_Node);
1581 I : Interp_Index;
1582 It : Interp;
1584 begin
1585 if not Is_Overloaded (Nod) then
1586 return Scope (Base_Type (Etype (Nod))) = S;
1588 else
1589 Get_First_Interp (Nod, I, It);
1590 while Present (It.Typ) loop
1591 if Scope (Base_Type (It.Typ)) = S then
1592 return True;
1593 end if;
1595 Get_Next_Interp (I, It);
1596 end loop;
1598 return False;
1599 end if;
1600 end Operand_Type_In_Scope;
1602 ---------------
1603 -- Type_In_P --
1604 ---------------
1606 function Type_In_P (Test : Kind_Test) return Entity_Id is
1607 E : Entity_Id;
1609 function In_Decl return Boolean;
1610 -- Verify that node is not part of the type declaration for the
1611 -- candidate type, which would otherwise be invisible.
1613 -------------
1614 -- In_Decl --
1615 -------------
1617 function In_Decl return Boolean is
1618 Decl_Node : constant Node_Id := Parent (E);
1619 N2 : Node_Id;
1621 begin
1622 N2 := N;
1624 if Etype (E) = Any_Type then
1625 return True;
1627 elsif No (Decl_Node) then
1628 return False;
1630 else
1631 while Present (N2)
1632 and then Nkind (N2) /= N_Compilation_Unit
1633 loop
1634 if N2 = Decl_Node then
1635 return True;
1636 else
1637 N2 := Parent (N2);
1638 end if;
1639 end loop;
1641 return False;
1642 end if;
1643 end In_Decl;
1645 -- Start of processing for Type_In_P
1647 begin
1648 -- If the context type is declared in the prefix package, this is the
1649 -- desired base type.
1651 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1652 return Base_Type (Typ);
1654 else
1655 E := First_Entity (Pack);
1656 while Present (E) loop
1657 if Test (E) and then not In_Decl then
1658 return E;
1659 end if;
1661 Next_Entity (E);
1662 end loop;
1664 return Empty;
1665 end if;
1666 end Type_In_P;
1668 -- Start of processing for Make_Call_Into_Operator
1670 begin
1671 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1673 -- Preserve the Comes_From_Source flag on the result if the original
1674 -- call came from source. Although it is not strictly the case that the
1675 -- operator as such comes from the source, logically it corresponds
1676 -- exactly to the function call in the source, so it should be marked
1677 -- this way (e.g. to make sure that validity checks work fine).
1679 Preserve_Comes_From_Source (Op_Node, N);
1681 -- Ensure that the corresponding operator has the same parent as the
1682 -- original call. This guarantees that parent traversals performed by
1683 -- the ABE mechanism succeed.
1685 Set_Parent (Op_Node, Parent (N));
1687 -- Binary operator
1689 if Is_Binary then
1690 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1691 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1692 Save_Interps (Act1, Left_Opnd (Op_Node));
1693 Save_Interps (Act2, Right_Opnd (Op_Node));
1694 Act1 := Left_Opnd (Op_Node);
1695 Act2 := Right_Opnd (Op_Node);
1697 -- Unary operator
1699 else
1700 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1701 Save_Interps (Act1, Right_Opnd (Op_Node));
1702 Act1 := Right_Opnd (Op_Node);
1703 end if;
1705 -- If the operator is denoted by an expanded name, and the prefix is
1706 -- not Standard, but the operator is a predefined one whose scope is
1707 -- Standard, then this is an implicit_operator, inserted as an
1708 -- interpretation by the procedure of the same name. This procedure
1709 -- overestimates the presence of implicit operators, because it does
1710 -- not examine the type of the operands. Verify now that the operand
1711 -- type appears in the given scope. If right operand is universal,
1712 -- check the other operand. In the case of concatenation, either
1713 -- argument can be the component type, so check the type of the result.
1714 -- If both arguments are literals, look for a type of the right kind
1715 -- defined in the given scope. This elaborate nonsense is brought to
1716 -- you courtesy of b33302a. The type itself must be frozen, so we must
1717 -- find the type of the proper class in the given scope.
1719 -- A final wrinkle is the multiplication operator for fixed point types,
1720 -- which is defined in Standard only, and not in the scope of the
1721 -- fixed point type itself.
1723 if Nkind (Name (N)) = N_Expanded_Name then
1724 Pack := Entity (Prefix (Name (N)));
1726 -- If this is a package renaming, get renamed entity, which will be
1727 -- the scope of the operands if operaton is type-correct.
1729 if Present (Renamed_Entity (Pack)) then
1730 Pack := Renamed_Entity (Pack);
1731 end if;
1733 -- If the entity being called is defined in the given package, it is
1734 -- a renaming of a predefined operator, and known to be legal.
1736 if Scope (Entity (Name (N))) = Pack
1737 and then Pack /= Standard_Standard
1738 then
1739 null;
1741 -- Visibility does not need to be checked in an instance: if the
1742 -- operator was not visible in the generic it has been diagnosed
1743 -- already, else there is an implicit copy of it in the instance.
1745 elsif In_Instance then
1746 null;
1748 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1749 and then Is_Fixed_Point_Type (Etype (Act1))
1750 and then Is_Fixed_Point_Type (Etype (Act2))
1751 then
1752 if Pack /= Standard_Standard then
1753 Error := True;
1754 end if;
1756 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1757 -- available.
1759 elsif Ada_Version >= Ada_2005
1760 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1761 and then (Is_Anonymous_Access_Type (Etype (Act1))
1762 or else Is_Anonymous_Access_Type (Etype (Act2)))
1763 then
1764 null;
1766 else
1767 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1769 if Op_Name = Name_Op_Concat then
1770 Opnd_Type := Base_Type (Typ);
1772 elsif (Scope (Opnd_Type) = Standard_Standard
1773 and then Is_Binary)
1774 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1775 and then Is_Binary
1776 and then not Comes_From_Source (Opnd_Type))
1777 then
1778 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1779 end if;
1781 if Scope (Opnd_Type) = Standard_Standard then
1783 -- Verify that the scope contains a type that corresponds to
1784 -- the given literal. Optimize the case where Pack is Standard.
1786 if Pack /= Standard_Standard then
1787 if Opnd_Type = Universal_Integer then
1788 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1790 elsif Opnd_Type = Universal_Real then
1791 Orig_Type := Type_In_P (Is_Real_Type'Access);
1793 elsif Opnd_Type = Universal_Access then
1794 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1796 elsif Opnd_Type = Any_String then
1797 Orig_Type := Type_In_P (Is_String_Type'Access);
1799 elsif Opnd_Type = Any_Composite then
1800 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1802 if Present (Orig_Type) then
1803 if Has_Private_Component (Orig_Type) then
1804 Orig_Type := Empty;
1805 else
1806 Set_Etype (Act1, Orig_Type);
1808 if Is_Binary then
1809 Set_Etype (Act2, Orig_Type);
1810 end if;
1811 end if;
1812 end if;
1814 else
1815 Orig_Type := Empty;
1816 end if;
1818 Error := No (Orig_Type);
1819 end if;
1821 elsif Ekind (Opnd_Type) = E_Allocator_Type
1822 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1823 then
1824 Error := True;
1826 -- If the type is defined elsewhere, and the operator is not
1827 -- defined in the given scope (by a renaming declaration, e.g.)
1828 -- then this is an error as well. If an extension of System is
1829 -- present, and the type may be defined there, Pack must be
1830 -- System itself.
1832 elsif Scope (Opnd_Type) /= Pack
1833 and then Scope (Op_Id) /= Pack
1834 and then (No (System_Aux_Id)
1835 or else Scope (Opnd_Type) /= System_Aux_Id
1836 or else Pack /= Scope (System_Aux_Id))
1837 then
1838 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1839 Error := True;
1840 else
1841 Error := not Operand_Type_In_Scope (Pack);
1842 end if;
1844 elsif Pack = Standard_Standard
1845 and then not Operand_Type_In_Scope (Standard_Standard)
1846 then
1847 Error := True;
1848 end if;
1849 end if;
1851 if Error then
1852 Error_Msg_Node_2 := Pack;
1853 Error_Msg_NE
1854 ("& not declared in&", N, Selector_Name (Name (N)));
1855 Set_Etype (N, Any_Type);
1856 return;
1858 -- Detect a mismatch between the context type and the result type
1859 -- in the named package, which is otherwise not detected if the
1860 -- operands are universal. Check is only needed if source entity is
1861 -- an operator, not a function that renames an operator.
1863 elsif Nkind (Parent (N)) /= N_Type_Conversion
1864 and then Ekind (Entity (Name (N))) = E_Operator
1865 and then Is_Numeric_Type (Typ)
1866 and then not Is_Universal_Numeric_Type (Typ)
1867 and then Scope (Base_Type (Typ)) /= Pack
1868 and then not In_Instance
1869 then
1870 if Is_Fixed_Point_Type (Typ)
1871 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1872 then
1873 -- Already checked above
1875 null;
1877 -- Operator may be defined in an extension of System
1879 elsif Present (System_Aux_Id)
1880 and then Present (Opnd_Type)
1881 and then Scope (Opnd_Type) = System_Aux_Id
1882 then
1883 null;
1885 else
1886 -- Could we use Wrong_Type here??? (this would require setting
1887 -- Etype (N) to the actual type found where Typ was expected).
1889 Error_Msg_NE ("expect }", N, Typ);
1890 end if;
1891 end if;
1892 end if;
1894 Set_Chars (Op_Node, Op_Name);
1896 if not Is_Private_Type (Etype (N)) then
1897 Set_Etype (Op_Node, Base_Type (Etype (N)));
1898 else
1899 Set_Etype (Op_Node, Etype (N));
1900 end if;
1902 -- If this is a call to a function that renames a predefined equality,
1903 -- the renaming declaration provides a type that must be used to
1904 -- resolve the operands. This must be done now because resolution of
1905 -- the equality node will not resolve any remaining ambiguity, and it
1906 -- assumes that the first operand is not overloaded.
1908 if Op_Name in Name_Op_Eq | Name_Op_Ne
1909 and then Ekind (Func) = E_Function
1910 and then Is_Overloaded (Act1)
1911 then
1912 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1913 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1914 end if;
1916 Set_Entity (Op_Node, Op_Id);
1917 Generate_Reference (Op_Id, N, ' ');
1919 Rewrite (N, Op_Node);
1921 -- If this is an arithmetic operator and the result type is private,
1922 -- the operands and the result must be wrapped in conversion to
1923 -- expose the underlying numeric type and expand the proper checks,
1924 -- e.g. on division.
1926 if Is_Private_Type (Typ) then
1927 case Nkind (N) is
1928 when N_Op_Add
1929 | N_Op_Divide
1930 | N_Op_Expon
1931 | N_Op_Mod
1932 | N_Op_Multiply
1933 | N_Op_Rem
1934 | N_Op_Subtract
1936 Resolve_Intrinsic_Operator (N, Typ);
1938 when N_Op_Abs
1939 | N_Op_Minus
1940 | N_Op_Plus
1942 Resolve_Intrinsic_Unary_Operator (N, Typ);
1944 when others =>
1945 Resolve (N, Typ);
1946 end case;
1947 else
1948 Resolve (N, Typ);
1949 end if;
1950 end Make_Call_Into_Operator;
1952 -------------------
1953 -- Operator_Kind --
1954 -------------------
1956 function Operator_Kind
1957 (Op_Name : Name_Id;
1958 Is_Binary : Boolean) return Node_Kind
1960 Kind : Node_Kind;
1962 begin
1963 -- Use CASE statement or array???
1965 if Is_Binary then
1966 if Op_Name = Name_Op_And then
1967 Kind := N_Op_And;
1968 elsif Op_Name = Name_Op_Or then
1969 Kind := N_Op_Or;
1970 elsif Op_Name = Name_Op_Xor then
1971 Kind := N_Op_Xor;
1972 elsif Op_Name = Name_Op_Eq then
1973 Kind := N_Op_Eq;
1974 elsif Op_Name = Name_Op_Ne then
1975 Kind := N_Op_Ne;
1976 elsif Op_Name = Name_Op_Lt then
1977 Kind := N_Op_Lt;
1978 elsif Op_Name = Name_Op_Le then
1979 Kind := N_Op_Le;
1980 elsif Op_Name = Name_Op_Gt then
1981 Kind := N_Op_Gt;
1982 elsif Op_Name = Name_Op_Ge then
1983 Kind := N_Op_Ge;
1984 elsif Op_Name = Name_Op_Add then
1985 Kind := N_Op_Add;
1986 elsif Op_Name = Name_Op_Subtract then
1987 Kind := N_Op_Subtract;
1988 elsif Op_Name = Name_Op_Concat then
1989 Kind := N_Op_Concat;
1990 elsif Op_Name = Name_Op_Multiply then
1991 Kind := N_Op_Multiply;
1992 elsif Op_Name = Name_Op_Divide then
1993 Kind := N_Op_Divide;
1994 elsif Op_Name = Name_Op_Mod then
1995 Kind := N_Op_Mod;
1996 elsif Op_Name = Name_Op_Rem then
1997 Kind := N_Op_Rem;
1998 elsif Op_Name = Name_Op_Expon then
1999 Kind := N_Op_Expon;
2000 else
2001 raise Program_Error;
2002 end if;
2004 -- Unary operators
2006 else
2007 if Op_Name = Name_Op_Add then
2008 Kind := N_Op_Plus;
2009 elsif Op_Name = Name_Op_Subtract then
2010 Kind := N_Op_Minus;
2011 elsif Op_Name = Name_Op_Abs then
2012 Kind := N_Op_Abs;
2013 elsif Op_Name = Name_Op_Not then
2014 Kind := N_Op_Not;
2015 else
2016 raise Program_Error;
2017 end if;
2018 end if;
2020 return Kind;
2021 end Operator_Kind;
2023 ----------------------------
2024 -- Preanalyze_And_Resolve --
2025 ----------------------------
2027 procedure Preanalyze_And_Resolve
2028 (N : Node_Id;
2029 T : Entity_Id;
2030 With_Freezing : Boolean)
2032 Save_Full_Analysis : constant Boolean := Full_Analysis;
2033 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
2034 Save_Preanalysis_Count : constant Nat :=
2035 Inside_Preanalysis_Without_Freezing;
2036 begin
2037 pragma Assert (Nkind (N) in N_Subexpr);
2039 if not With_Freezing then
2040 Set_Must_Not_Freeze (N);
2041 Inside_Preanalysis_Without_Freezing :=
2042 Inside_Preanalysis_Without_Freezing + 1;
2043 end if;
2045 Full_Analysis := False;
2046 Expander_Mode_Save_And_Set (False);
2048 -- Normally, we suppress all checks for this preanalysis. There is no
2049 -- point in processing them now, since they will be applied properly
2050 -- and in the proper location when the default expressions reanalyzed
2051 -- and reexpanded later on. We will also have more information at that
2052 -- point for possible suppression of individual checks.
2054 -- However, in SPARK mode, most expansion is suppressed, and this
2055 -- later reanalysis and reexpansion may not occur. SPARK mode does
2056 -- require the setting of checking flags for proof purposes, so we
2057 -- do the SPARK preanalysis without suppressing checks.
2059 -- This special handling for SPARK mode is required for example in the
2060 -- case of Ada 2012 constructs such as quantified expressions, which are
2061 -- expanded in two separate steps.
2063 -- We also do not want to suppress checks if we are not dealing
2064 -- with a default expression. One such case that is known to reach
2065 -- this point is the expression of an expression function.
2067 if GNATprove_Mode or Nkind (Parent (N)) = N_Simple_Return_Statement then
2068 Analyze_And_Resolve (N, T);
2069 else
2070 Analyze_And_Resolve (N, T, Suppress => All_Checks);
2071 end if;
2073 Expander_Mode_Restore;
2074 Full_Analysis := Save_Full_Analysis;
2076 if not With_Freezing then
2077 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
2078 Inside_Preanalysis_Without_Freezing :=
2079 Inside_Preanalysis_Without_Freezing - 1;
2080 end if;
2082 pragma Assert
2083 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
2084 end Preanalyze_And_Resolve;
2086 ----------------------------
2087 -- Preanalyze_And_Resolve --
2088 ----------------------------
2090 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
2091 begin
2092 Preanalyze_And_Resolve (N, T, With_Freezing => False);
2093 end Preanalyze_And_Resolve;
2095 -- Version without context type
2097 procedure Preanalyze_And_Resolve (N : Node_Id) is
2098 Save_Full_Analysis : constant Boolean := Full_Analysis;
2100 begin
2101 Full_Analysis := False;
2102 Expander_Mode_Save_And_Set (False);
2104 Analyze (N);
2105 Resolve (N, Etype (N), Suppress => All_Checks);
2107 Expander_Mode_Restore;
2108 Full_Analysis := Save_Full_Analysis;
2109 end Preanalyze_And_Resolve;
2111 ------------------------------------------
2112 -- Preanalyze_With_Freezing_And_Resolve --
2113 ------------------------------------------
2115 procedure Preanalyze_With_Freezing_And_Resolve
2116 (N : Node_Id;
2117 T : Entity_Id)
2119 begin
2120 Preanalyze_And_Resolve (N, T, With_Freezing => True);
2121 end Preanalyze_With_Freezing_And_Resolve;
2123 ----------------------------------
2124 -- Replace_Actual_Discriminants --
2125 ----------------------------------
2127 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2128 Loc : constant Source_Ptr := Sloc (N);
2129 Tsk : Node_Id := Empty;
2131 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2132 -- Comment needed???
2134 -------------------
2135 -- Process_Discr --
2136 -------------------
2138 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2139 Ent : Entity_Id;
2141 begin
2142 if Nkind (Nod) = N_Identifier then
2143 Ent := Entity (Nod);
2145 if Present (Ent)
2146 and then Ekind (Ent) = E_Discriminant
2147 then
2148 Rewrite (Nod,
2149 Make_Selected_Component (Loc,
2150 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2151 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2153 Set_Etype (Nod, Etype (Ent));
2154 end if;
2156 end if;
2158 return OK;
2159 end Process_Discr;
2161 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2163 -- Start of processing for Replace_Actual_Discriminants
2165 begin
2166 if Expander_Active then
2167 null;
2169 -- Allow the replacement of concurrent discriminants in GNATprove even
2170 -- though this is a light expansion activity. Note that generic units
2171 -- are not modified.
2173 elsif GNATprove_Mode and not Inside_A_Generic then
2174 null;
2176 else
2177 return;
2178 end if;
2180 if Nkind (Name (N)) = N_Selected_Component then
2181 Tsk := Prefix (Name (N));
2183 elsif Nkind (Name (N)) = N_Indexed_Component then
2184 Tsk := Prefix (Prefix (Name (N)));
2185 end if;
2187 if Present (Tsk) then
2188 Replace_Discrs (Default);
2189 end if;
2190 end Replace_Actual_Discriminants;
2192 -------------
2193 -- Resolve --
2194 -------------
2196 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2197 Ambiguous : Boolean := False;
2198 Ctx_Type : Entity_Id := Typ;
2199 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2200 Err_Type : Entity_Id := Empty;
2201 Found : Boolean := False;
2202 From_Lib : Boolean;
2203 I : Interp_Index;
2204 I1 : Interp_Index := 0; -- prevent junk warning
2205 It : Interp;
2206 It1 : Interp;
2207 Seen : Entity_Id := Empty; -- prevent junk warning
2209 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2210 -- Determine whether a node comes from a predefined library unit or
2211 -- Standard.
2213 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2214 -- Try and fix up a literal so that it matches its expected type. New
2215 -- literals are manufactured if necessary to avoid cascaded errors.
2217 procedure Report_Ambiguous_Argument;
2218 -- Additional diagnostics when an ambiguous call has an ambiguous
2219 -- argument (typically a controlling actual).
2221 procedure Resolution_Failed;
2222 -- Called when attempt at resolving current expression fails
2224 ------------------------------------
2225 -- Comes_From_Predefined_Lib_Unit --
2226 -------------------------------------
2228 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2229 begin
2230 return
2231 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2232 end Comes_From_Predefined_Lib_Unit;
2234 --------------------
2235 -- Patch_Up_Value --
2236 --------------------
2238 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2239 begin
2240 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2241 Rewrite (N,
2242 Make_Real_Literal (Sloc (N),
2243 Realval => UR_From_Uint (Intval (N))));
2244 Set_Etype (N, Universal_Real);
2245 Set_Is_Static_Expression (N);
2247 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2248 Rewrite (N,
2249 Make_Integer_Literal (Sloc (N),
2250 Intval => UR_To_Uint (Realval (N))));
2251 Set_Etype (N, Universal_Integer);
2252 Set_Is_Static_Expression (N);
2254 elsif Nkind (N) = N_String_Literal
2255 and then Is_Character_Type (Typ)
2256 then
2257 Set_Character_Literal_Name (Get_Char_Code ('A'));
2258 Rewrite (N,
2259 Make_Character_Literal (Sloc (N),
2260 Chars => Name_Find,
2261 Char_Literal_Value =>
2262 UI_From_CC (Get_Char_Code ('A'))));
2263 Set_Etype (N, Any_Character);
2264 Set_Is_Static_Expression (N);
2266 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2267 Rewrite (N,
2268 Make_String_Literal (Sloc (N),
2269 Strval => End_String));
2271 elsif Nkind (N) = N_Range then
2272 Patch_Up_Value (Low_Bound (N), Typ);
2273 Patch_Up_Value (High_Bound (N), Typ);
2274 end if;
2275 end Patch_Up_Value;
2277 -------------------------------
2278 -- Report_Ambiguous_Argument --
2279 -------------------------------
2281 procedure Report_Ambiguous_Argument is
2282 Arg : constant Node_Id := First (Parameter_Associations (N));
2283 I : Interp_Index;
2284 It : Interp;
2286 begin
2287 if Nkind (Arg) = N_Function_Call
2288 and then Is_Entity_Name (Name (Arg))
2289 and then Is_Overloaded (Name (Arg))
2290 then
2291 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2293 -- Examine possible interpretations, and adapt the message
2294 -- for inherited subprograms declared by a type derivation.
2296 Get_First_Interp (Name (Arg), I, It);
2297 while Present (It.Nam) loop
2298 Error_Msg_Sloc := Sloc (It.Nam);
2300 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2301 Error_Msg_N ("interpretation (inherited) #!", Arg);
2302 else
2303 Error_Msg_N ("interpretation #!", Arg);
2304 end if;
2306 Get_Next_Interp (I, It);
2307 end loop;
2308 end if;
2310 -- Additional message and hint if the ambiguity involves an Ada 2022
2311 -- container aggregate.
2313 Check_Ambiguous_Aggregate (N);
2314 end Report_Ambiguous_Argument;
2316 -----------------------
2317 -- Resolution_Failed --
2318 -----------------------
2320 procedure Resolution_Failed is
2321 begin
2322 Patch_Up_Value (N, Typ);
2324 -- Set the type to the desired one to minimize cascaded errors. Note
2325 -- that this is an approximation and does not work in all cases.
2327 Set_Etype (N, Typ);
2329 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2330 Set_Is_Overloaded (N, False);
2332 -- The caller will return without calling the expander, so we need
2333 -- to set the analyzed flag. Note that it is fine to set Analyzed
2334 -- to True even if we are in the middle of a shallow analysis,
2335 -- (see the spec of sem for more details) since this is an error
2336 -- situation anyway, and there is no point in repeating the
2337 -- analysis later (indeed it won't work to repeat it later, since
2338 -- we haven't got a clear resolution of which entity is being
2339 -- referenced.)
2341 Set_Analyzed (N, True);
2342 return;
2343 end Resolution_Failed;
2345 -- Start of processing for Resolve
2347 begin
2348 if N = Error then
2349 return;
2350 end if;
2352 -- Access attribute on remote subprogram cannot be used for a non-remote
2353 -- access-to-subprogram type.
2355 if Nkind (N) = N_Attribute_Reference
2356 and then Attribute_Name (N) in Name_Access
2357 | Name_Unrestricted_Access
2358 | Name_Unchecked_Access
2359 and then Comes_From_Source (N)
2360 and then Is_Entity_Name (Prefix (N))
2361 and then Is_Subprogram (Entity (Prefix (N)))
2362 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2363 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2364 then
2365 Error_Msg_N
2366 ("prefix must statically denote a non-remote subprogram", N);
2367 end if;
2369 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2371 -- If the context is a Remote_Access_To_Subprogram, access attributes
2372 -- must be resolved with the corresponding fat pointer. There is no need
2373 -- to check for the attribute name since the return type of an
2374 -- attribute is never a remote type.
2376 if Nkind (N) = N_Attribute_Reference
2377 and then Comes_From_Source (N)
2378 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2379 then
2380 declare
2381 Attr : constant Attribute_Id :=
2382 Get_Attribute_Id (Attribute_Name (N));
2383 Pref : constant Node_Id := Prefix (N);
2384 Decl : Node_Id;
2385 Spec : Node_Id;
2386 Is_Remote : Boolean := True;
2388 begin
2389 -- Check that Typ is a remote access-to-subprogram type
2391 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2393 -- Prefix (N) must statically denote a remote subprogram
2394 -- declared in a package specification.
2396 if Attr = Attribute_Access or else
2397 Attr = Attribute_Unchecked_Access or else
2398 Attr = Attribute_Unrestricted_Access
2399 then
2400 Decl := Unit_Declaration_Node (Entity (Pref));
2402 if Nkind (Decl) = N_Subprogram_Body then
2403 Spec := Corresponding_Spec (Decl);
2405 if Present (Spec) then
2406 Decl := Unit_Declaration_Node (Spec);
2407 end if;
2408 end if;
2410 Spec := Parent (Decl);
2412 if not Is_Entity_Name (Prefix (N))
2413 or else Nkind (Spec) /= N_Package_Specification
2414 or else
2415 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2416 then
2417 Is_Remote := False;
2418 Error_Msg_N
2419 ("prefix must statically denote a remote subprogram",
2421 end if;
2423 -- If we are generating code in distributed mode, perform
2424 -- semantic checks against corresponding remote entities.
2426 if Expander_Active
2427 and then Get_PCS_Name /= Name_No_DSA
2428 then
2429 Check_Subtype_Conformant
2430 (New_Id => Entity (Prefix (N)),
2431 Old_Id => Designated_Type
2432 (Corresponding_Remote_Type (Typ)),
2433 Err_Loc => N);
2435 if Is_Remote then
2436 Process_Remote_AST_Attribute (N, Typ);
2437 end if;
2438 end if;
2439 end if;
2440 end if;
2441 end;
2442 end if;
2444 Debug_A_Entry ("resolving ", N);
2446 if Debug_Flag_V then
2447 Write_Overloads (N);
2448 end if;
2450 if Comes_From_Source (N) then
2451 if Is_Fixed_Point_Type (Typ) then
2452 Check_Restriction (No_Fixed_Point, N);
2454 elsif Is_Floating_Point_Type (Typ)
2455 and then Typ /= Universal_Real
2456 and then Typ /= Any_Real
2457 then
2458 Check_Restriction (No_Floating_Point, N);
2459 end if;
2460 end if;
2462 -- Return if already analyzed
2464 if Analyzed (N) then
2465 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2466 Analyze_Dimension (N);
2467 return;
2469 -- Any case of Any_Type as the Etype value means that we had a
2470 -- previous error.
2472 elsif Etype (N) = Any_Type then
2473 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2474 return;
2475 end if;
2477 Check_Parameterless_Call (N);
2479 -- The resolution of an Expression_With_Actions is determined by
2480 -- its Expression, but if the node comes from source it is a
2481 -- Declare_Expression and requires scope management.
2483 if Nkind (N) = N_Expression_With_Actions then
2484 if Comes_From_Source (N) and then not Is_Rewrite_Substitution (N) then
2485 Resolve_Declare_Expression (N, Typ);
2486 else
2487 Resolve (Expression (N), Typ);
2488 end if;
2490 Found := True;
2491 Expr_Type := Etype (Expression (N));
2493 -- If not overloaded, then we know the type, and all that needs doing
2494 -- is to check that this type is compatible with the context.
2496 elsif not Is_Overloaded (N) then
2497 Found := Covers (Typ, Etype (N));
2498 Expr_Type := Etype (N);
2500 -- In the overloaded case, we must select the interpretation that
2501 -- is compatible with the context (i.e. the type passed to Resolve)
2503 else
2504 -- Loop through possible interpretations
2506 Get_First_Interp (N, I, It);
2507 Interp_Loop : while Present (It.Typ) loop
2508 if Debug_Flag_V then
2509 Write_Str ("Interp: ");
2510 Write_Interp (It);
2511 end if;
2513 -- We are only interested in interpretations that are compatible
2514 -- with the expected type, any other interpretations are ignored.
2516 if not Covers (Typ, It.Typ) then
2517 if Debug_Flag_V then
2518 Write_Str (" interpretation incompatible with context");
2519 Write_Eol;
2520 end if;
2522 else
2523 -- Skip the current interpretation if it is disabled by an
2524 -- abstract operator. This action is performed only when the
2525 -- type against which we are resolving is the same as the
2526 -- type of the interpretation.
2528 if Ada_Version >= Ada_2005
2529 and then It.Typ = Typ
2530 and then not Is_Universal_Numeric_Type (Typ)
2531 and then Present (It.Abstract_Op)
2532 then
2533 if Debug_Flag_V then
2534 Write_Line ("Skip.");
2535 end if;
2537 goto Continue;
2538 end if;
2540 -- First matching interpretation
2542 if not Found then
2543 Found := True;
2544 I1 := I;
2545 Seen := It.Nam;
2546 Expr_Type := It.Typ;
2548 -- Matching interpretation that is not the first, maybe an
2549 -- error, but there are some cases where preference rules are
2550 -- used to choose between the two possibilities. These and
2551 -- some more obscure cases are handled in Disambiguate.
2553 else
2554 -- If the current statement is part of a predefined library
2555 -- unit, then all interpretations which come from user level
2556 -- packages should not be considered. Check previous and
2557 -- current one.
2559 if From_Lib then
2560 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2561 goto Continue;
2563 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2565 -- Previous interpretation must be discarded
2567 I1 := I;
2568 Seen := It.Nam;
2569 Expr_Type := It.Typ;
2570 Set_Entity (N, Seen);
2571 goto Continue;
2572 end if;
2573 end if;
2575 -- Otherwise apply further disambiguation steps
2577 Error_Msg_Sloc := Sloc (Seen);
2578 It1 := Disambiguate (N, I1, I, Typ);
2580 -- Disambiguation has succeeded. Skip the remaining
2581 -- interpretations.
2583 if It1 /= No_Interp then
2584 Seen := It1.Nam;
2585 Expr_Type := It1.Typ;
2587 while Present (It.Typ) loop
2588 Get_Next_Interp (I, It);
2589 end loop;
2591 else
2592 -- Before we issue an ambiguity complaint, check for the
2593 -- case of a subprogram call where at least one of the
2594 -- arguments is Any_Type, and if so suppress the message,
2595 -- since it is a cascaded error. This can also happen for
2596 -- a generalized indexing operation.
2598 if Nkind (N) in N_Subprogram_Call
2599 or else (Nkind (N) = N_Indexed_Component
2600 and then Present (Generalized_Indexing (N)))
2601 then
2602 declare
2603 A : Node_Id;
2604 E : Node_Id;
2606 begin
2607 if Nkind (N) = N_Indexed_Component then
2608 Rewrite (N, Generalized_Indexing (N));
2609 end if;
2611 A := First_Actual (N);
2612 while Present (A) loop
2613 E := A;
2615 if Nkind (E) = N_Parameter_Association then
2616 E := Explicit_Actual_Parameter (E);
2617 end if;
2619 if Etype (E) = Any_Type then
2620 if Debug_Flag_V then
2621 Write_Str ("Any_Type in call");
2622 Write_Eol;
2623 end if;
2625 exit Interp_Loop;
2626 end if;
2628 Next_Actual (A);
2629 end loop;
2630 end;
2632 elsif Nkind (N) in N_Binary_Op
2633 and then (Etype (Left_Opnd (N)) = Any_Type
2634 or else Etype (Right_Opnd (N)) = Any_Type)
2635 then
2636 exit Interp_Loop;
2638 elsif Nkind (N) in N_Unary_Op
2639 and then Etype (Right_Opnd (N)) = Any_Type
2640 then
2641 exit Interp_Loop;
2642 end if;
2644 -- Not that special case, so issue message using the flag
2645 -- Ambiguous to control printing of the header message
2646 -- only at the start of an ambiguous set.
2648 if not Ambiguous then
2649 if Nkind (N) = N_Function_Call
2650 and then Nkind (Name (N)) = N_Explicit_Dereference
2651 then
2652 Error_Msg_N
2653 ("ambiguous expression (cannot resolve indirect "
2654 & "call)!", N);
2655 else
2656 Error_Msg_NE -- CODEFIX
2657 ("ambiguous expression (cannot resolve&)!",
2658 N, It.Nam);
2659 end if;
2661 Ambiguous := True;
2663 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2664 Error_Msg_N
2665 ("\\possible interpretation (inherited)#!", N);
2666 else
2667 Error_Msg_N -- CODEFIX
2668 ("\\possible interpretation#!", N);
2669 end if;
2671 if Nkind (N) in N_Subprogram_Call
2672 and then Present (Parameter_Associations (N))
2673 then
2674 Report_Ambiguous_Argument;
2675 end if;
2676 end if;
2678 Error_Msg_Sloc := Sloc (It.Nam);
2680 -- By default, the error message refers to the candidate
2681 -- interpretation. But if it is a predefined operator, it
2682 -- is implicitly declared at the declaration of the type
2683 -- of the operand. Recover the sloc of that declaration
2684 -- for the error message.
2686 if Nkind (N) in N_Op
2687 and then Scope (It.Nam) = Standard_Standard
2688 and then not Is_Overloaded (Right_Opnd (N))
2689 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2690 Standard_Standard
2691 then
2692 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2694 if Comes_From_Source (Err_Type)
2695 and then Present (Parent (Err_Type))
2696 then
2697 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2698 end if;
2700 elsif Nkind (N) in N_Binary_Op
2701 and then Scope (It.Nam) = Standard_Standard
2702 and then not Is_Overloaded (Left_Opnd (N))
2703 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2704 Standard_Standard
2705 then
2706 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2708 if Comes_From_Source (Err_Type)
2709 and then Present (Parent (Err_Type))
2710 then
2711 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2712 end if;
2714 -- If this is an indirect call, use the subprogram_type
2715 -- in the message, to have a meaningful location. Also
2716 -- indicate if this is an inherited operation, created
2717 -- by a type declaration.
2719 elsif Nkind (N) = N_Function_Call
2720 and then Nkind (Name (N)) = N_Explicit_Dereference
2721 and then Is_Type (It.Nam)
2722 then
2723 Err_Type := It.Nam;
2724 Error_Msg_Sloc :=
2725 Sloc (Associated_Node_For_Itype (Err_Type));
2726 else
2727 Err_Type := Empty;
2728 end if;
2730 if Nkind (N) in N_Op
2731 and then Scope (It.Nam) = Standard_Standard
2732 and then Present (Err_Type)
2733 then
2734 -- Special-case the message for universal_fixed
2735 -- operators, which are not declared with the type
2736 -- of the operand, but appear forever in Standard.
2738 if It.Typ = Universal_Fixed
2739 and then Scope (It.Nam) = Standard_Standard
2740 then
2741 Error_Msg_N
2742 ("\\possible interpretation as universal_fixed "
2743 & "operation (RM 4.5.5 (19))", N);
2744 else
2745 Error_Msg_N
2746 ("\\possible interpretation (predefined)#!", N);
2747 end if;
2749 elsif
2750 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2751 then
2752 Error_Msg_N
2753 ("\\possible interpretation (inherited)#!", N);
2754 else
2755 Error_Msg_N -- CODEFIX
2756 ("\\possible interpretation#!", N);
2757 end if;
2759 end if;
2760 end if;
2762 -- We have a matching interpretation, Expr_Type is the type
2763 -- from this interpretation, and Seen is the entity.
2765 -- For an operator, just set the entity name. The type will be
2766 -- set by the specific operator resolution routine.
2768 if Nkind (N) in N_Op then
2769 Set_Entity (N, Seen);
2770 Generate_Reference (Seen, N);
2772 elsif Nkind (N) in N_Case_Expression
2773 | N_Character_Literal
2774 | N_Delta_Aggregate
2775 | N_If_Expression
2776 then
2777 Set_Etype (N, Expr_Type);
2779 -- AI05-0139-2: Expression is overloaded because type has
2780 -- implicit dereference. The context may be the one that
2781 -- requires implicit dereferemce.
2783 elsif Has_Implicit_Dereference (Expr_Type) then
2784 Set_Etype (N, Expr_Type);
2785 Set_Is_Overloaded (N, False);
2787 -- If the expression is an entity, generate a reference
2788 -- to it, as this is not done for an overloaded construct
2789 -- during analysis.
2791 if Is_Entity_Name (N)
2792 and then Comes_From_Source (N)
2793 then
2794 Generate_Reference (Entity (N), N);
2796 -- Examine access discriminants of entity type,
2797 -- to check whether one of them yields the
2798 -- expected type.
2800 declare
2801 Disc : Entity_Id :=
2802 First_Discriminant (Etype (Entity (N)));
2804 begin
2805 while Present (Disc) loop
2806 exit when Is_Access_Type (Etype (Disc))
2807 and then Has_Implicit_Dereference (Disc)
2808 and then Designated_Type (Etype (Disc)) = Typ;
2810 Next_Discriminant (Disc);
2811 end loop;
2813 if Present (Disc) then
2814 Build_Explicit_Dereference (N, Disc);
2815 end if;
2816 end;
2817 end if;
2819 exit Interp_Loop;
2821 elsif Is_Overloaded (N)
2822 and then Present (It.Nam)
2823 and then Ekind (It.Nam) = E_Discriminant
2824 and then Has_Implicit_Dereference (It.Nam)
2825 then
2826 -- If the node is a general indexing, the dereference is
2827 -- is inserted when resolving the rewritten form, else
2828 -- insert it now.
2830 if Nkind (N) /= N_Indexed_Component
2831 or else No (Generalized_Indexing (N))
2832 then
2833 Build_Explicit_Dereference (N, It.Nam);
2834 end if;
2836 -- For an explicit dereference, attribute reference, range,
2837 -- short-circuit form (which is not an operator node), or call
2838 -- with a name that is an explicit dereference, there is
2839 -- nothing to be done at this point.
2841 elsif Nkind (N) in N_Attribute_Reference
2842 | N_And_Then
2843 | N_Explicit_Dereference
2844 | N_Identifier
2845 | N_Indexed_Component
2846 | N_Or_Else
2847 | N_Range
2848 | N_Selected_Component
2849 | N_Slice
2850 or else Nkind (Name (N)) = N_Explicit_Dereference
2851 then
2852 null;
2854 -- For procedure or function calls, set the type of the name,
2855 -- and also the entity pointer for the prefix.
2857 elsif Nkind (N) in N_Subprogram_Call
2858 and then Is_Entity_Name (Name (N))
2859 then
2860 Set_Etype (Name (N), Expr_Type);
2861 Set_Entity (Name (N), Seen);
2862 Generate_Reference (Seen, Name (N));
2864 elsif Nkind (N) = N_Function_Call
2865 and then Nkind (Name (N)) = N_Selected_Component
2866 then
2867 Set_Etype (Name (N), Expr_Type);
2868 Set_Entity (Selector_Name (Name (N)), Seen);
2869 Generate_Reference (Seen, Selector_Name (Name (N)));
2871 -- For all other cases, just set the type of the Name
2873 else
2874 Set_Etype (Name (N), Expr_Type);
2875 end if;
2877 end if;
2879 <<Continue>>
2881 -- Move to next interpretation
2883 exit Interp_Loop when No (It.Typ);
2885 Get_Next_Interp (I, It);
2886 end loop Interp_Loop;
2887 end if;
2889 -- At this stage Found indicates whether or not an acceptable
2890 -- interpretation exists. If not, then we have an error, except that if
2891 -- the context is Any_Type as a result of some other error, then we
2892 -- suppress the error report.
2894 if not Found then
2895 if Typ /= Any_Type then
2897 -- If type we are looking for is Void, then this is the procedure
2898 -- call case, and the error is simply that what we gave is not a
2899 -- procedure name (we think of procedure calls as expressions with
2900 -- types internally, but the user doesn't think of them this way).
2902 if Typ = Standard_Void_Type then
2904 -- Special case message if function used as a procedure
2906 if Nkind (N) = N_Procedure_Call_Statement
2907 and then Is_Entity_Name (Name (N))
2908 and then Ekind (Entity (Name (N))) = E_Function
2909 then
2910 Error_Msg_NE
2911 ("cannot use call to function & as a statement",
2912 Name (N), Entity (Name (N)));
2913 Error_Msg_N
2914 ("\return value of a function call cannot be ignored",
2915 Name (N));
2917 -- Otherwise give general message (not clear what cases this
2918 -- covers, but no harm in providing for them).
2920 else
2921 Error_Msg_N ("expect procedure name in procedure call", N);
2922 end if;
2924 Found := True;
2926 -- Otherwise we do have a subexpression with the wrong type
2928 -- Check for the case of an allocator which uses an access type
2929 -- instead of the designated type. This is a common error and we
2930 -- specialize the message, posting an error on the operand of the
2931 -- allocator, complaining that we expected the designated type of
2932 -- the allocator.
2934 elsif Nkind (N) = N_Allocator
2935 and then Is_Access_Type (Typ)
2936 and then Is_Access_Type (Etype (N))
2937 and then Designated_Type (Etype (N)) = Typ
2938 then
2939 Wrong_Type (Expression (N), Designated_Type (Typ));
2940 Found := True;
2942 -- Check for view mismatch on Null in instances, for which the
2943 -- view-swapping mechanism has no identifier.
2945 elsif (In_Instance or else In_Inlined_Body)
2946 and then (Nkind (N) = N_Null)
2947 and then Is_Private_Type (Typ)
2948 and then Is_Access_Type (Full_View (Typ))
2949 then
2950 Resolve (N, Full_View (Typ));
2951 Set_Etype (N, Typ);
2952 return;
2954 -- Check for an aggregate. Sometimes we can get bogus aggregates
2955 -- from misuse of parentheses, and we are about to complain about
2956 -- the aggregate without even looking inside it.
2958 -- Instead, if we have an aggregate of type Any_Composite, then
2959 -- analyze and resolve the component fields, and then only issue
2960 -- another message if we get no errors doing this (otherwise
2961 -- assume that the errors in the aggregate caused the problem).
2963 elsif Nkind (N) = N_Aggregate
2964 and then Etype (N) = Any_Composite
2965 then
2966 if Ada_Version >= Ada_2022
2967 and then Has_Aspect (Typ, Aspect_Aggregate)
2968 then
2969 Resolve_Container_Aggregate (N, Typ);
2971 if Expander_Active then
2972 Expand (N);
2973 end if;
2974 return;
2975 end if;
2977 -- Disable expansion in any case. If there is a type mismatch
2978 -- it may be fatal to try to expand the aggregate. The flag
2979 -- would otherwise be set to false when the error is posted.
2981 Expander_Active := False;
2983 declare
2984 procedure Check_Aggr (Aggr : Node_Id);
2985 -- Check one aggregate, and set Found to True if we have a
2986 -- definite error in any of its elements
2988 procedure Check_Elmt (Aelmt : Node_Id);
2989 -- Check one element of aggregate and set Found to True if
2990 -- we definitely have an error in the element.
2992 ----------------
2993 -- Check_Aggr --
2994 ----------------
2996 procedure Check_Aggr (Aggr : Node_Id) is
2997 Elmt : Node_Id;
2999 begin
3000 if Present (Expressions (Aggr)) then
3001 Elmt := First (Expressions (Aggr));
3002 while Present (Elmt) loop
3003 Check_Elmt (Elmt);
3004 Next (Elmt);
3005 end loop;
3006 end if;
3008 if Present (Component_Associations (Aggr)) then
3009 Elmt := First (Component_Associations (Aggr));
3010 while Present (Elmt) loop
3012 -- If this is a default-initialized component, then
3013 -- there is nothing to check. The box will be
3014 -- replaced by the appropriate call during late
3015 -- expansion.
3017 if Nkind (Elmt) /= N_Iterated_Component_Association
3018 and then not Box_Present (Elmt)
3019 then
3020 Check_Elmt (Expression (Elmt));
3021 end if;
3023 Next (Elmt);
3024 end loop;
3025 end if;
3026 end Check_Aggr;
3028 ----------------
3029 -- Check_Elmt --
3030 ----------------
3032 procedure Check_Elmt (Aelmt : Node_Id) is
3033 begin
3034 -- If we have a nested aggregate, go inside it (to
3035 -- attempt a naked analyze-resolve of the aggregate can
3036 -- cause undesirable cascaded errors). Do not resolve
3037 -- expression if it needs a type from context, as for
3038 -- integer * fixed expression.
3040 if Nkind (Aelmt) = N_Aggregate then
3041 Check_Aggr (Aelmt);
3043 else
3044 Analyze (Aelmt);
3046 if not Is_Overloaded (Aelmt)
3047 and then Etype (Aelmt) /= Any_Fixed
3048 then
3049 Resolve (Aelmt);
3050 end if;
3052 if Etype (Aelmt) = Any_Type then
3053 Found := True;
3054 end if;
3055 end if;
3056 end Check_Elmt;
3058 begin
3059 Check_Aggr (N);
3060 end;
3061 end if;
3063 -- If node is a literal and context type has a user-defined
3064 -- literal aspect, rewrite node as a call to the corresponding
3065 -- function, which plays the role of an implicit conversion.
3067 if Nkind (N) in
3068 N_Numeric_Or_String_Literal | N_Identifier
3069 and then Has_Applicable_User_Defined_Literal (N, Typ)
3070 then
3071 Analyze_And_Resolve (N, Typ);
3072 return;
3073 end if;
3075 -- Looks like we have a type error, but check for special case
3076 -- of Address wanted, integer found, with the configuration pragma
3077 -- Allow_Integer_Address active. If we have this case, introduce
3078 -- an unchecked conversion to allow the integer expression to be
3079 -- treated as an Address. The reverse case of integer wanted,
3080 -- Address found, is treated in an analogous manner.
3082 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3083 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3084 Analyze_And_Resolve (N, Typ);
3085 return;
3087 -- Under relaxed RM semantics silently replace occurrences of null
3088 -- by System.Null_Address.
3090 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3091 Replace_Null_By_Null_Address (N);
3092 Analyze_And_Resolve (N, Typ);
3093 return;
3094 end if;
3096 -- That special Allow_Integer_Address check did not apply, so we
3097 -- have a real type error. If an error message was issued already,
3098 -- Found got reset to True, so if it's still False, issue standard
3099 -- Wrong_Type message.
3101 if not Found then
3102 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3103 declare
3104 Subp_Name : Node_Id;
3106 begin
3107 if Is_Entity_Name (Name (N)) then
3108 Subp_Name := Name (N);
3110 elsif Nkind (Name (N)) = N_Selected_Component then
3112 -- Protected operation: retrieve operation name
3114 Subp_Name := Selector_Name (Name (N));
3116 else
3117 raise Program_Error;
3118 end if;
3120 Error_Msg_Node_2 := Typ;
3121 Error_Msg_NE
3122 ("no visible interpretation of& matches expected type&",
3123 N, Subp_Name);
3124 end;
3126 if All_Errors_Mode then
3127 declare
3128 Index : Interp_Index;
3129 It : Interp;
3131 begin
3132 Error_Msg_N ("\\possible interpretations:", N);
3134 Get_First_Interp (Name (N), Index, It);
3135 while Present (It.Nam) loop
3136 Error_Msg_Sloc := Sloc (It.Nam);
3137 Error_Msg_Node_2 := It.Nam;
3138 Error_Msg_NE
3139 ("\\ type& for & declared#", N, It.Typ);
3140 Get_Next_Interp (Index, It);
3141 end loop;
3142 end;
3144 else
3145 Error_Msg_N ("\use -gnatf for details", N);
3146 end if;
3148 -- Recognize the case of a quantified expression being mistaken
3149 -- for an iterated component association because the user
3150 -- forgot the "all" or "some" keyword after "for". Because the
3151 -- error message starts with "missing ALL", we automatically
3152 -- benefit from the associated CODEFIX, which requires that
3153 -- the message is located on the identifier following "for"
3154 -- in order for the CODEFIX to insert "all" in the right place.
3156 elsif Nkind (N) = N_Aggregate
3157 and then List_Length (Component_Associations (N)) = 1
3158 and then Nkind (First (Component_Associations (N)))
3159 = N_Iterated_Component_Association
3160 and then Is_Boolean_Type (Typ)
3161 then
3162 Error_Msg_N -- CODEFIX
3163 ("missing ALL or SOME in quantified expression",
3164 Defining_Identifier (First (Component_Associations (N))));
3166 -- For an operator with no interpretation, check whether
3167 -- one of its operands may be a user-defined literal.
3169 elsif Nkind (N) in N_Op
3170 and then Try_User_Defined_Literal (N, Typ)
3171 then
3172 return;
3174 else
3175 Wrong_Type (N, Typ);
3176 end if;
3177 end if;
3178 end if;
3180 Resolution_Failed;
3181 return;
3183 -- Test if we have more than one interpretation for the context
3185 elsif Ambiguous then
3186 Resolution_Failed;
3187 return;
3189 -- Only one interpretation
3191 else
3192 -- Prevent implicit conversions between access-to-subprogram types
3193 -- with different strub modes. Explicit conversions are acceptable in
3194 -- some circumstances. We don't have to be concerned about data or
3195 -- access-to-data types. Conversions between data types can safely
3196 -- drop or add strub attributes from types, because strub effects are
3197 -- associated with the locations rather than values. E.g., converting
3198 -- a hypothetical Strub_Integer variable to Integer would load the
3199 -- value from the variable, enabling stack scrabbing for the
3200 -- enclosing subprogram, and then convert the value to Integer. As
3201 -- for conversions between access-to-data types, that's no different
3202 -- from any other case of type punning.
3204 if Is_Access_Type (Typ)
3205 and then Ekind (Designated_Type (Typ)) = E_Subprogram_Type
3206 and then Is_Access_Type (Expr_Type)
3207 and then Ekind (Designated_Type (Expr_Type)) = E_Subprogram_Type
3208 then
3209 Check_Same_Strub_Mode
3210 (Designated_Type (Typ), Designated_Type (Expr_Type));
3211 end if;
3213 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3214 -- the "+" on T is abstract, and the operands are of universal type,
3215 -- the above code will have (incorrectly) resolved the "+" to the
3216 -- universal one in Standard. Therefore check for this case and give
3217 -- an error. We can't do this earlier, because it would cause legal
3218 -- cases to get errors (when some other type has an abstract "+").
3220 if Ada_Version >= Ada_2005
3221 and then Nkind (N) in N_Op
3222 and then Is_Overloaded (N)
3223 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3224 then
3225 Get_First_Interp (N, I, It);
3226 while Present (It.Typ) loop
3227 if Present (It.Abstract_Op)
3228 and then Etype (It.Abstract_Op) = Typ
3229 then
3230 Nondispatching_Call_To_Abstract_Operation
3231 (N, It.Abstract_Op);
3232 return;
3233 end if;
3235 Get_Next_Interp (I, It);
3236 end loop;
3237 end if;
3239 -- Here we have an acceptable interpretation for the context
3241 -- Propagate type information and normalize tree for various
3242 -- predefined operations. If the context only imposes a class of
3243 -- types, rather than a specific type, propagate the actual type
3244 -- downward.
3246 if Typ = Any_Integer or else
3247 Typ = Any_Boolean or else
3248 Typ = Any_Modular or else
3249 Typ = Any_Real or else
3250 Typ = Any_Discrete
3251 then
3252 Ctx_Type := Expr_Type;
3254 -- Any_Fixed is legal in a real context only if a specific fixed-
3255 -- point type is imposed. If Norman Cohen can be confused by this,
3256 -- it deserves a separate message.
3258 if Typ = Any_Real
3259 and then Expr_Type = Any_Fixed
3260 then
3261 Error_Msg_N ("illegal context for mixed mode operation", N);
3262 Set_Etype (N, Universal_Real);
3263 Ctx_Type := Universal_Real;
3264 end if;
3265 end if;
3267 -- A user-defined operator is transformed into a function call at
3268 -- this point, so that further processing knows that operators are
3269 -- really operators (i.e. are predefined operators). User-defined
3270 -- operators that are intrinsic are just renamings of the predefined
3271 -- ones, and need not be turned into calls either, but if they rename
3272 -- a different operator, we must transform the node accordingly.
3273 -- Instantiations of Unchecked_Conversion are intrinsic but are
3274 -- treated as functions, even if given an operator designator.
3276 if Nkind (N) in N_Op
3277 and then Present (Entity (N))
3278 and then Ekind (Entity (N)) /= E_Operator
3279 then
3280 if not Is_Predefined_Op (Entity (N)) then
3281 Rewrite_Operator_As_Call (N, Entity (N));
3283 elsif Present (Alias (Entity (N)))
3284 and then
3285 Nkind (Parent (Parent (Entity (N)))) =
3286 N_Subprogram_Renaming_Declaration
3287 then
3288 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3290 -- If the node is rewritten, it will be fully resolved in
3291 -- Rewrite_Renamed_Operator.
3293 if Analyzed (N) then
3294 return;
3295 end if;
3296 end if;
3297 end if;
3299 case N_Subexpr'(Nkind (N)) is
3300 when N_Aggregate =>
3301 Resolve_Aggregate (N, Ctx_Type);
3303 when N_Allocator =>
3304 Resolve_Allocator (N, Ctx_Type);
3306 when N_Short_Circuit =>
3307 Resolve_Short_Circuit (N, Ctx_Type);
3309 when N_Attribute_Reference =>
3310 Resolve_Attribute (N, Ctx_Type);
3312 when N_Case_Expression =>
3313 Resolve_Case_Expression (N, Ctx_Type);
3315 when N_Character_Literal =>
3316 Resolve_Character_Literal (N, Ctx_Type);
3318 when N_Delta_Aggregate =>
3319 Resolve_Delta_Aggregate (N, Ctx_Type);
3321 when N_Expanded_Name =>
3322 Resolve_Entity_Name (N, Ctx_Type);
3324 when N_Explicit_Dereference =>
3325 Resolve_Explicit_Dereference (N, Ctx_Type);
3327 when N_Expression_With_Actions =>
3328 Resolve_Expression_With_Actions (N, Ctx_Type);
3330 when N_Extension_Aggregate =>
3331 Resolve_Extension_Aggregate (N, Ctx_Type);
3333 when N_Function_Call =>
3334 Resolve_Call (N, Ctx_Type);
3336 when N_Identifier =>
3337 Resolve_Entity_Name (N, Ctx_Type);
3339 when N_If_Expression =>
3340 Resolve_If_Expression (N, Ctx_Type);
3342 when N_Indexed_Component =>
3343 Resolve_Indexed_Component (N, Ctx_Type);
3345 when N_Integer_Literal =>
3346 Resolve_Integer_Literal (N, Ctx_Type);
3348 when N_Membership_Test =>
3349 Resolve_Membership_Op (N, Ctx_Type);
3351 when N_Null =>
3352 Resolve_Null (N, Ctx_Type);
3354 when N_Op_And
3355 | N_Op_Or
3356 | N_Op_Xor
3358 Resolve_Logical_Op (N, Ctx_Type);
3360 when N_Op_Eq
3361 | N_Op_Ne
3363 Resolve_Equality_Op (N, Ctx_Type);
3365 when N_Op_Ge
3366 | N_Op_Gt
3367 | N_Op_Le
3368 | N_Op_Lt
3370 Resolve_Comparison_Op (N, Ctx_Type);
3372 when N_Op_Not =>
3373 Resolve_Op_Not (N, Ctx_Type);
3375 when N_Op_Add
3376 | N_Op_Divide
3377 | N_Op_Mod
3378 | N_Op_Multiply
3379 | N_Op_Rem
3380 | N_Op_Subtract
3382 Resolve_Arithmetic_Op (N, Ctx_Type);
3384 when N_Op_Concat =>
3385 Resolve_Op_Concat (N, Ctx_Type);
3387 when N_Op_Expon =>
3388 Resolve_Op_Expon (N, Ctx_Type);
3390 when N_Op_Abs
3391 | N_Op_Minus
3392 | N_Op_Plus
3394 Resolve_Unary_Op (N, Ctx_Type);
3396 when N_Op_Shift =>
3397 Resolve_Shift (N, Ctx_Type);
3399 when N_Procedure_Call_Statement =>
3400 Resolve_Call (N, Ctx_Type);
3402 when N_Operator_Symbol =>
3403 Resolve_Operator_Symbol (N, Ctx_Type);
3405 when N_Qualified_Expression =>
3406 Resolve_Qualified_Expression (N, Ctx_Type);
3408 -- Why is the following null, needs a comment ???
3410 when N_Quantified_Expression =>
3411 null;
3413 when N_Raise_Expression =>
3414 Resolve_Raise_Expression (N, Ctx_Type);
3416 when N_Raise_xxx_Error =>
3417 Set_Etype (N, Ctx_Type);
3419 when N_Range =>
3420 Resolve_Range (N, Ctx_Type);
3422 when N_Real_Literal =>
3423 Resolve_Real_Literal (N, Ctx_Type);
3425 when N_Reference =>
3426 Resolve_Reference (N, Ctx_Type);
3428 when N_Selected_Component =>
3429 Resolve_Selected_Component (N, Ctx_Type);
3431 when N_Slice =>
3432 Resolve_Slice (N, Ctx_Type);
3434 when N_String_Literal =>
3435 Resolve_String_Literal (N, Ctx_Type);
3437 when N_Target_Name =>
3438 Resolve_Target_Name (N, Ctx_Type);
3440 when N_Type_Conversion =>
3441 Resolve_Type_Conversion (N, Ctx_Type);
3443 when N_Unchecked_Expression =>
3444 Resolve_Unchecked_Expression (N, Ctx_Type);
3446 when N_Unchecked_Type_Conversion =>
3447 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3448 end case;
3450 -- Mark relevant use-type and use-package clauses as effective using
3451 -- the original node because constant folding may have occurred and
3452 -- removed references that need to be examined.
3454 if Nkind (Original_Node (N)) in N_Op then
3455 Mark_Use_Clauses (Original_Node (N));
3456 end if;
3458 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3459 -- expression of an anonymous access type that occurs in the context
3460 -- of a named general access type, except when the expression is that
3461 -- of a membership test. This ensures proper legality checking in
3462 -- terms of allowed conversions (expressions that would be illegal to
3463 -- convert implicitly are allowed in membership tests).
3465 if Ada_Version >= Ada_2012
3466 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3467 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3468 and then Nkind (Parent (N)) not in N_Membership_Test
3469 then
3470 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3471 Analyze_And_Resolve (N, Ctx_Type);
3472 end if;
3474 -- If the subexpression was replaced by a non-subexpression, then
3475 -- all we do is to expand it. The only legitimate case we know of
3476 -- is converting procedure call statement to entry call statements,
3477 -- but there may be others, so we are making this test general.
3479 if Nkind (N) not in N_Subexpr then
3480 Debug_A_Exit ("resolving ", N, " (done)");
3481 Expand (N);
3482 return;
3483 end if;
3485 -- The expression is definitely NOT overloaded at this point, so
3486 -- we reset the Is_Overloaded flag to avoid any confusion when
3487 -- reanalyzing the node.
3489 Set_Is_Overloaded (N, False);
3491 -- Freeze expression type, entity if it is a name, and designated
3492 -- type if it is an allocator (RM 13.14(10,11,13)).
3494 -- Now that the resolution of the type of the node is complete, and
3495 -- we did not detect an error, we can expand this node. We skip the
3496 -- expand call if we are in a default expression, see section
3497 -- "Handling of Default Expressions" in Sem spec.
3499 Debug_A_Exit ("resolving ", N, " (done)");
3501 -- We unconditionally freeze the expression, even if we are in
3502 -- default expression mode (the Freeze_Expression routine tests this
3503 -- flag and only freezes static types if it is set).
3505 -- Ada 2012 (AI05-177): The declaration of an expression function
3506 -- does not cause freezing, but we never reach here in that case.
3507 -- Here we are resolving the corresponding expanded body, so we do
3508 -- need to perform normal freezing.
3510 -- As elsewhere we do not emit freeze node within a generic.
3512 if not Inside_A_Generic then
3513 Freeze_Expression (N);
3514 end if;
3516 -- Now we can do the expansion
3518 Expand (N);
3519 end if;
3520 end Resolve;
3522 -------------
3523 -- Resolve --
3524 -------------
3526 -- Version with check(s) suppressed
3528 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3529 begin
3530 if Suppress = All_Checks then
3531 declare
3532 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3533 begin
3534 Scope_Suppress.Suppress := (others => True);
3535 Resolve (N, Typ);
3536 Scope_Suppress.Suppress := Sva;
3537 end;
3539 else
3540 declare
3541 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3542 begin
3543 Scope_Suppress.Suppress (Suppress) := True;
3544 Resolve (N, Typ);
3545 Scope_Suppress.Suppress (Suppress) := Svg;
3546 end;
3547 end if;
3548 end Resolve;
3550 -------------
3551 -- Resolve --
3552 -------------
3554 -- Version with implicit type
3556 procedure Resolve (N : Node_Id) is
3557 begin
3558 Resolve (N, Etype (N));
3559 end Resolve;
3561 ---------------------
3562 -- Resolve_Actuals --
3563 ---------------------
3565 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3566 Loc : constant Source_Ptr := Sloc (N);
3567 A : Node_Id;
3568 A_Typ : Entity_Id := Empty; -- init to avoid warning
3569 F : Entity_Id;
3570 F_Typ : Entity_Id;
3571 Prev : Node_Id := Empty;
3572 Orig_A : Node_Id;
3573 Real_F : Entity_Id := Empty; -- init to avoid warning
3575 Real_Subp : Entity_Id;
3576 -- If the subprogram being called is an inherited operation for
3577 -- a formal derived type in an instance, Real_Subp is the subprogram
3578 -- that will be called. It may have different formal names than the
3579 -- operation of the formal in the generic, so after actual is resolved
3580 -- the name of the actual in a named association must carry the name
3581 -- of the actual of the subprogram being called.
3583 procedure Check_Aliased_Parameter;
3584 -- Check rules on aliased parameters and related accessibility rules
3585 -- in (RM 3.10.2 (10.2-10.4)).
3587 procedure Check_Argument_Order;
3588 -- Performs a check for the case where the actuals are all simple
3589 -- identifiers that correspond to the formal names, but in the wrong
3590 -- order, which is considered suspicious and cause for a warning.
3592 procedure Check_Prefixed_Call;
3593 -- If the original node is an overloaded call in prefix notation,
3594 -- insert an 'Access or a dereference as needed over the first actual.
3595 -- Try_Object_Operation has already verified that there is a valid
3596 -- interpretation, but the form of the actual can only be determined
3597 -- once the primitive operation is identified.
3599 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3600 -- Emit an error concerning the illegal usage of an effectively volatile
3601 -- object for reading in interfering context (SPARK RM 7.1.3(10)).
3603 procedure Insert_Default;
3604 -- If the actual is missing in a call, insert in the actuals list
3605 -- an instance of the default expression. The insertion is always
3606 -- a named association.
3608 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3609 -- Check whether T1 and T2, or their full views, are derived from a
3610 -- common type. Used to enforce the restrictions on array conversions
3611 -- of AI95-00246.
3613 function Static_Concatenation (N : Node_Id) return Boolean;
3614 -- Predicate to determine whether an actual that is a concatenation
3615 -- will be evaluated statically and does not need a transient scope.
3616 -- This must be determined before the actual is resolved and expanded
3617 -- because if needed the transient scope must be introduced earlier.
3619 -----------------------------
3620 -- Check_Aliased_Parameter --
3621 -----------------------------
3623 procedure Check_Aliased_Parameter is
3624 Nominal_Subt : Entity_Id;
3626 begin
3627 if Is_Aliased (F) then
3628 if Is_Tagged_Type (A_Typ) then
3629 null;
3631 elsif Is_Aliased_View (A) then
3632 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3633 Nominal_Subt := Base_Type (A_Typ);
3634 else
3635 Nominal_Subt := A_Typ;
3636 end if;
3638 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3639 null;
3641 -- In a generic body assume the worst for generic formals:
3642 -- they can have a constrained partial view (AI05-041).
3644 elsif Has_Discriminants (F_Typ)
3645 and then not Is_Constrained (F_Typ)
3646 and then not Object_Type_Has_Constrained_Partial_View
3647 (Typ => F_Typ, Scop => Current_Scope)
3648 then
3649 null;
3651 else
3652 Error_Msg_NE ("untagged actual does not statically match "
3653 & "aliased formal&", A, F);
3654 end if;
3656 else
3657 Error_Msg_NE ("actual for aliased formal& must be "
3658 & "aliased object", A, F);
3659 end if;
3661 if Ekind (Nam) = E_Procedure then
3662 null;
3664 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3665 if Nkind (Parent (N)) = N_Type_Conversion
3666 and then Type_Access_Level (Etype (Parent (N)))
3667 < Static_Accessibility_Level (A, Object_Decl_Level)
3668 then
3669 Error_Msg_N ("aliased actual has wrong accessibility", A);
3670 end if;
3672 elsif Nkind (Parent (N)) = N_Qualified_Expression
3673 and then Nkind (Parent (Parent (N))) = N_Allocator
3674 and then Type_Access_Level (Etype (Parent (Parent (N))))
3675 < Static_Accessibility_Level (A, Object_Decl_Level)
3676 then
3677 Error_Msg_N
3678 ("aliased actual in allocator has wrong accessibility", A);
3679 end if;
3680 end if;
3681 end Check_Aliased_Parameter;
3683 --------------------------
3684 -- Check_Argument_Order --
3685 --------------------------
3687 procedure Check_Argument_Order is
3688 begin
3689 -- Nothing to do if no parameters, or original node is neither a
3690 -- function call nor a procedure call statement (happens in the
3691 -- operator-transformed-to-function call case), or the call is to an
3692 -- operator symbol (which is usually in infix form), or the call does
3693 -- not come from source, or this warning is off.
3695 if not Warn_On_Parameter_Order
3696 or else No (Parameter_Associations (N))
3697 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3698 or else (Nkind (Name (N)) = N_Identifier
3699 and then Present (Entity (Name (N)))
3700 and then Nkind (Entity (Name (N))) =
3701 N_Defining_Operator_Symbol)
3702 or else not Comes_From_Source (N)
3703 then
3704 return;
3705 end if;
3707 declare
3708 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3710 begin
3711 -- Nothing to do if only one parameter
3713 if Nargs < 2 then
3714 return;
3715 end if;
3717 -- Here if at least two arguments
3719 declare
3720 Actuals : array (1 .. Nargs) of Node_Id;
3721 Actual : Node_Id;
3722 Formal : Node_Id;
3724 Wrong_Order : Boolean := False;
3725 -- Set True if an out of order case is found
3727 begin
3728 -- Collect identifier names of actuals, fail if any actual is
3729 -- not a simple identifier, and record max length of name.
3731 Actual := First (Parameter_Associations (N));
3732 for J in Actuals'Range loop
3733 if Nkind (Actual) /= N_Identifier then
3734 return;
3735 else
3736 Actuals (J) := Actual;
3737 Next (Actual);
3738 end if;
3739 end loop;
3741 -- If we got this far, all actuals are identifiers and the list
3742 -- of their names is stored in the Actuals array.
3744 Formal := First_Formal (Nam);
3745 for J in Actuals'Range loop
3747 -- If we ran out of formals, that's odd, probably an error
3748 -- which will be detected elsewhere, but abandon the search.
3750 if No (Formal) then
3751 return;
3752 end if;
3754 -- If name matches and is in order OK
3756 if Chars (Formal) = Chars (Actuals (J)) then
3757 null;
3759 else
3760 -- If no match, see if it is elsewhere in list and if so
3761 -- flag potential wrong order if type is compatible.
3763 for K in Actuals'Range loop
3764 if Chars (Formal) = Chars (Actuals (K))
3765 and then
3766 Has_Compatible_Type (Actuals (K), Etype (Formal))
3767 then
3768 Wrong_Order := True;
3769 goto Continue;
3770 end if;
3771 end loop;
3773 -- No match
3775 return;
3776 end if;
3778 <<Continue>> Next_Formal (Formal);
3779 end loop;
3781 -- If Formals left over, also probably an error, skip warning
3783 if Present (Formal) then
3784 return;
3785 end if;
3787 -- Here we give the warning if something was out of order
3789 if Wrong_Order then
3790 Error_Msg_N
3791 ("?.p?actuals for this call may be in wrong order", N);
3792 end if;
3793 end;
3794 end;
3795 end Check_Argument_Order;
3797 -------------------------
3798 -- Check_Prefixed_Call --
3799 -------------------------
3801 procedure Check_Prefixed_Call is
3802 Act : constant Node_Id := First_Actual (N);
3803 A_Type : constant Entity_Id := Etype (Act);
3804 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3805 Orig : constant Node_Id := Original_Node (N);
3806 New_A : Node_Id;
3808 begin
3809 -- Check whether the call is a prefixed call, with or without
3810 -- additional actuals.
3812 if Nkind (Orig) = N_Selected_Component
3813 or else
3814 (Nkind (Orig) = N_Indexed_Component
3815 and then Nkind (Prefix (Orig)) = N_Selected_Component
3816 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3817 and then Is_Entity_Name (Act)
3818 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3819 then
3820 if Is_Access_Type (A_Type)
3821 and then not Is_Access_Type (F_Type)
3822 then
3823 -- Introduce dereference on object in prefix
3825 New_A :=
3826 Make_Explicit_Dereference (Sloc (Act),
3827 Prefix => Relocate_Node (Act));
3828 Rewrite (Act, New_A);
3829 Analyze (Act);
3831 elsif Is_Access_Type (F_Type)
3832 and then not Is_Access_Type (A_Type)
3833 then
3834 -- Introduce an implicit 'Access in prefix
3836 if not Is_Aliased_View (Act) then
3837 Error_Msg_NE
3838 ("object in prefixed call to& must be aliased "
3839 & "(RM 4.1.3 (13 1/2))",
3840 Prefix (Act), Nam);
3841 end if;
3843 Rewrite (Act,
3844 Make_Attribute_Reference (Loc,
3845 Attribute_Name => Name_Access,
3846 Prefix => Relocate_Node (Act)));
3847 end if;
3849 Analyze (Act);
3850 end if;
3851 end Check_Prefixed_Call;
3853 ---------------------------------------
3854 -- Flag_Effectively_Volatile_Objects --
3855 ---------------------------------------
3857 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3858 function Flag_Object (N : Node_Id) return Traverse_Result;
3859 -- Determine whether arbitrary node N denotes an effectively volatile
3860 -- object for reading and if it does, emit an error.
3862 -----------------
3863 -- Flag_Object --
3864 -----------------
3866 function Flag_Object (N : Node_Id) return Traverse_Result is
3867 Id : Entity_Id;
3869 begin
3870 case Nkind (N) is
3871 -- Do not consider nested function calls because they have
3872 -- already been processed during their own resolution.
3874 when N_Function_Call =>
3875 return Skip;
3877 when N_Identifier | N_Expanded_Name =>
3878 Id := Entity (N);
3880 -- Identifiers of components and discriminants are not names
3881 -- in the sense of Ada RM 4.1. They can only occur as a
3882 -- selector_name in selected_component or as a choice in
3883 -- component_association.
3885 if Present (Id)
3886 and then Is_Object (Id)
3887 and then Ekind (Id) not in E_Component | E_Discriminant
3888 and then Is_Effectively_Volatile_For_Reading (Id)
3889 and then
3890 not Is_OK_Volatile_Context (Context => Parent (N),
3891 Obj_Ref => N,
3892 Check_Actuals => True)
3893 then
3894 Error_Msg_N
3895 ("volatile object cannot appear in this context"
3896 & " (SPARK RM 7.1.3(10))", N);
3897 end if;
3899 return Skip;
3901 when others =>
3902 return OK;
3903 end case;
3904 end Flag_Object;
3906 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3908 -- Start of processing for Flag_Effectively_Volatile_Objects
3910 begin
3911 Flag_Objects (Expr);
3912 end Flag_Effectively_Volatile_Objects;
3914 --------------------
3915 -- Insert_Default --
3916 --------------------
3918 procedure Insert_Default is
3919 Actval : Node_Id;
3920 Assoc : Node_Id;
3922 begin
3923 -- Missing argument in call, nothing to insert
3925 if No (Default_Value (F)) then
3926 return;
3928 else
3929 -- Note that we do a full New_Copy_Tree, so that any associated
3930 -- Itypes are properly copied. This may not be needed any more,
3931 -- but it does no harm as a safety measure. Defaults of a generic
3932 -- formal may be out of bounds of the corresponding actual (see
3933 -- cc1311b) and an additional check may be required.
3935 Actval :=
3936 New_Copy_Tree
3937 (Default_Value (F),
3938 New_Scope => Current_Scope,
3939 New_Sloc => Loc);
3941 -- Propagate dimension information, if any.
3943 Copy_Dimensions (Default_Value (F), Actval);
3945 if Is_Concurrent_Type (Scope (Nam))
3946 and then Has_Discriminants (Scope (Nam))
3947 then
3948 Replace_Actual_Discriminants (N, Actval);
3949 end if;
3951 if Is_Overloadable (Nam)
3952 and then Present (Alias (Nam))
3953 then
3954 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3955 and then not Is_Tagged_Type (Etype (F))
3956 then
3957 -- If default is a real literal, do not introduce a
3958 -- conversion whose effect may depend on the run-time
3959 -- size of universal real.
3961 if Nkind (Actval) = N_Real_Literal then
3962 Set_Etype (Actval, Base_Type (Etype (F)));
3963 else
3964 Actval := Unchecked_Convert_To (Etype (F), Actval);
3965 end if;
3966 end if;
3968 if Is_Scalar_Type (Etype (F)) then
3969 Enable_Range_Check (Actval);
3970 end if;
3972 Set_Parent (Actval, N);
3974 -- Resolve aggregates with their base type, to avoid scope
3975 -- anomalies: the subtype was first built in the subprogram
3976 -- declaration, and the current call may be nested.
3978 if Nkind (Actval) = N_Aggregate then
3979 Analyze_And_Resolve (Actval, Etype (F));
3980 else
3981 Analyze_And_Resolve (Actval, Etype (Actval));
3982 end if;
3984 else
3985 Set_Parent (Actval, N);
3987 -- See note above concerning aggregates
3989 if Nkind (Actval) = N_Aggregate
3990 and then Has_Discriminants (Etype (Actval))
3991 then
3992 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3994 -- Resolve entities with their own type, which may differ from
3995 -- the type of a reference in a generic context (the view
3996 -- swapping mechanism did not anticipate the re-analysis of
3997 -- default values in calls).
3999 elsif Is_Entity_Name (Actval) then
4000 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
4002 else
4003 Analyze_And_Resolve (Actval, Etype (Actval));
4004 end if;
4005 end if;
4007 -- If default is a tag indeterminate function call, propagate tag
4008 -- to obtain proper dispatching.
4010 if Is_Controlling_Formal (F)
4011 and then Nkind (Default_Value (F)) = N_Function_Call
4012 then
4013 Set_Is_Controlling_Actual (Actval);
4014 end if;
4015 end if;
4017 -- If the default expression raises constraint error, then just
4018 -- silently replace it with an N_Raise_Constraint_Error node, since
4019 -- we already gave the warning on the subprogram spec. If node is
4020 -- already a Raise_Constraint_Error leave as is, to prevent loops in
4021 -- the warnings removal machinery.
4023 if Raises_Constraint_Error (Actval)
4024 and then Nkind (Actval) /= N_Raise_Constraint_Error
4025 then
4026 Rewrite (Actval,
4027 Make_Raise_Constraint_Error (Loc,
4028 Reason => CE_Range_Check_Failed));
4030 Set_Raises_Constraint_Error (Actval);
4031 Set_Etype (Actval, Etype (F));
4032 end if;
4034 Assoc :=
4035 Make_Parameter_Association (Loc,
4036 Explicit_Actual_Parameter => Actval,
4037 Selector_Name => Make_Identifier (Loc, Chars (F)));
4039 -- Case of insertion is first named actual
4041 if No (Prev)
4042 or else Nkind (Parent (Prev)) /= N_Parameter_Association
4043 then
4044 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
4045 Set_First_Named_Actual (N, Actval);
4047 if No (Prev) then
4048 if No (Parameter_Associations (N)) then
4049 Set_Parameter_Associations (N, New_List (Assoc));
4050 else
4051 Append (Assoc, Parameter_Associations (N));
4052 end if;
4054 else
4055 Insert_After (Prev, Assoc);
4056 end if;
4058 -- Case of insertion is not first named actual
4060 else
4061 Set_Next_Named_Actual
4062 (Assoc, Next_Named_Actual (Parent (Prev)));
4063 Set_Next_Named_Actual (Parent (Prev), Actval);
4064 Append (Assoc, Parameter_Associations (N));
4065 end if;
4067 Mark_Rewrite_Insertion (Assoc);
4068 Mark_Rewrite_Insertion (Actval);
4070 Prev := Actval;
4071 end Insert_Default;
4073 -------------------
4074 -- Same_Ancestor --
4075 -------------------
4077 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
4078 FT1 : Entity_Id := T1;
4079 FT2 : Entity_Id := T2;
4081 begin
4082 if Is_Private_Type (T1)
4083 and then Present (Full_View (T1))
4084 then
4085 FT1 := Full_View (T1);
4086 end if;
4088 if Is_Private_Type (T2)
4089 and then Present (Full_View (T2))
4090 then
4091 FT2 := Full_View (T2);
4092 end if;
4094 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
4095 end Same_Ancestor;
4097 --------------------------
4098 -- Static_Concatenation --
4099 --------------------------
4101 function Static_Concatenation (N : Node_Id) return Boolean is
4102 begin
4103 case Nkind (N) is
4104 when N_String_Literal =>
4105 return True;
4107 when N_Op_Concat =>
4109 -- Concatenation is static when both operands are static and
4110 -- the concatenation operator is a predefined one.
4112 return Scope (Entity (N)) = Standard_Standard
4113 and then
4114 Static_Concatenation (Left_Opnd (N))
4115 and then
4116 Static_Concatenation (Right_Opnd (N));
4118 when others =>
4119 if Is_Entity_Name (N) then
4120 declare
4121 Ent : constant Entity_Id := Entity (N);
4122 begin
4123 return Ekind (Ent) = E_Constant
4124 and then Present (Constant_Value (Ent))
4125 and then
4126 Is_OK_Static_Expression (Constant_Value (Ent));
4127 end;
4129 else
4130 return False;
4131 end if;
4132 end case;
4133 end Static_Concatenation;
4135 -- Start of processing for Resolve_Actuals
4137 begin
4138 Check_Argument_Order;
4140 if Is_Overloadable (Nam)
4141 and then Is_Inherited_Operation (Nam)
4142 and then In_Instance
4143 and then Present (Alias (Nam))
4144 and then Present (Overridden_Operation (Alias (Nam)))
4145 then
4146 Real_Subp := Alias (Nam);
4147 else
4148 Real_Subp := Empty;
4149 end if;
4151 if Present (First_Actual (N)) then
4152 Check_Prefixed_Call;
4153 end if;
4155 A := First_Actual (N);
4156 F := First_Formal (Nam);
4158 if Present (Real_Subp) then
4159 Real_F := First_Formal (Real_Subp);
4160 end if;
4162 while Present (F) loop
4163 if No (A) and then Needs_No_Actuals (Nam) then
4164 null;
4166 -- If we have an error in any formal or actual, indicated by a type
4167 -- of Any_Type, then abandon resolution attempt, and set result type
4168 -- to Any_Type.
4170 elsif Etype (F) = Any_Type then
4171 Set_Etype (N, Any_Type);
4172 return;
4174 elsif Present (A) and then Etype (A) = Any_Type then
4175 -- For the peculiar case of a user-defined comparison or equality
4176 -- operator that does not return a boolean type, the operands may
4177 -- have been ambiguous for the predefined operator and, therefore,
4178 -- marked with Any_Type. Since the operation has been resolved to
4179 -- the user-defined operator, that is irrelevant, so reset Etype.
4181 if Nkind (Original_Node (N)) in N_Op_Compare
4182 and then not Is_Boolean_Type (Etype (N))
4183 then
4184 Set_Etype (A, Etype (F));
4186 -- Also skip this if the actual is a Raise_Expression, whose type
4187 -- is imposed from context.
4189 elsif Nkind (A) = N_Raise_Expression then
4190 null;
4192 else
4193 Set_Etype (N, Any_Type);
4194 return;
4195 end if;
4196 end if;
4198 -- Case where actual is present
4200 -- If the actual is an entity, generate a reference to it now. We
4201 -- do this before the actual is resolved, because a formal of some
4202 -- protected subprogram, or a task discriminant, will be rewritten
4203 -- during expansion, and the source entity reference may be lost.
4205 if Present (A)
4206 and then Is_Entity_Name (A)
4207 and then Comes_From_Source (A)
4208 then
4209 -- Annotate the tree by creating a variable reference marker when
4210 -- the actual denotes a variable reference, in case the reference
4211 -- is folded or optimized away. The variable reference marker is
4212 -- automatically saved for later examination by the ABE Processing
4213 -- phase. The status of the reference is set as follows:
4215 -- status mode
4216 -- read IN, IN OUT
4217 -- write IN OUT, OUT
4219 if Needs_Variable_Reference_Marker
4220 (N => A,
4221 Calls_OK => True)
4222 then
4223 Build_Variable_Reference_Marker
4224 (N => A,
4225 Read => Ekind (F) /= E_Out_Parameter,
4226 Write => Ekind (F) /= E_In_Parameter);
4227 end if;
4229 Orig_A := Entity (A);
4231 if Present (Orig_A) then
4232 if Is_Formal (Orig_A)
4233 and then Ekind (F) /= E_In_Parameter
4234 then
4235 Generate_Reference (Orig_A, A, 'm');
4237 elsif not Is_Overloaded (A) then
4238 if Ekind (F) /= E_Out_Parameter then
4239 Generate_Reference (Orig_A, A);
4241 -- RM 6.4.1(12): For an out parameter that is passed by
4242 -- copy, the formal parameter object is created, and:
4244 -- * For an access type, the formal parameter is initialized
4245 -- from the value of the actual, without checking that the
4246 -- value satisfies any constraint, any predicate, or any
4247 -- exclusion of the null value.
4249 -- * For a scalar type that has the Default_Value aspect
4250 -- specified, the formal parameter is initialized from the
4251 -- value of the actual, without checking that the value
4252 -- satisfies any constraint or any predicate.
4253 -- I do not understand why this case is included??? this is
4254 -- not a case where an OUT parameter is treated as IN OUT.
4256 -- * For a composite type with discriminants or that has
4257 -- implicit initial values for any subcomponents, the
4258 -- behavior is as for an in out parameter passed by copy.
4260 -- Hence for these cases we generate the read reference now
4261 -- (the write reference will be generated later by
4262 -- Note_Possible_Modification).
4264 elsif Is_By_Copy_Type (Etype (F))
4265 and then
4266 (Is_Access_Type (Etype (F))
4267 or else
4268 (Is_Scalar_Type (Etype (F))
4269 and then
4270 Present (Default_Aspect_Value (Etype (F))))
4271 or else
4272 (Is_Composite_Type (Etype (F))
4273 and then (Has_Discriminants (Etype (F))
4274 or else Is_Partially_Initialized_Type
4275 (Etype (F)))))
4276 then
4277 Generate_Reference (Orig_A, A);
4278 end if;
4279 end if;
4280 end if;
4281 end if;
4283 if Present (A)
4284 and then (Nkind (Parent (A)) /= N_Parameter_Association
4285 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4286 then
4287 -- If style checking mode on, check match of formal name
4289 if Style_Check then
4290 if Nkind (Parent (A)) = N_Parameter_Association then
4291 Check_Identifier (Selector_Name (Parent (A)), F);
4292 end if;
4293 end if;
4295 -- If the formal is Out or In_Out, do not resolve and expand the
4296 -- conversion, because it is subsequently expanded into explicit
4297 -- temporaries and assignments. However, the object of the
4298 -- conversion can be resolved. An exception is the case of tagged
4299 -- type conversion with a class-wide actual. In that case we want
4300 -- the tag check to occur and no temporary will be needed (no
4301 -- representation change can occur) and the parameter is passed by
4302 -- reference, so we go ahead and resolve the type conversion.
4303 -- Another exception is the case of reference to component or
4304 -- subcomponent of a bit-packed array, in which case we want to
4305 -- defer expansion to the point the in and out assignments are
4306 -- performed.
4308 if Ekind (F) /= E_In_Parameter
4309 and then Nkind (A) = N_Type_Conversion
4310 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4311 and then not Is_Interface (Etype (A))
4312 then
4313 declare
4314 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4316 begin
4317 -- Check RM 4.6 (24.2/2)
4319 if Is_Array_Type (Etype (F))
4320 and then Is_View_Conversion (A)
4321 then
4322 -- In a view conversion, the conversion must be legal in
4323 -- both directions, and thus both component types must be
4324 -- aliased, or neither (4.6 (8)).
4326 -- Check RM 4.6 (24.8/2)
4328 if Has_Aliased_Components (Expr_Typ) /=
4329 Has_Aliased_Components (Etype (F))
4330 then
4331 -- This normally illegal conversion is legal in an
4332 -- expanded instance body because of RM 12.3(11).
4333 -- At runtime, conversion must create a new object.
4335 if not In_Instance then
4336 Error_Msg_N
4337 ("both component types in a view conversion must"
4338 & " be aliased, or neither", A);
4339 end if;
4341 -- Check RM 4.6 (24/3)
4343 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4344 -- Check view conv between unrelated by ref array
4345 -- types.
4347 if Is_By_Reference_Type (Etype (F))
4348 or else Is_By_Reference_Type (Expr_Typ)
4349 then
4350 Error_Msg_N
4351 ("view conversion between unrelated by reference "
4352 & "array types not allowed ('A'I-00246)", A);
4354 -- In Ada 2005 mode, check view conversion component
4355 -- type cannot be private, tagged, or volatile. Note
4356 -- that we only apply this to source conversions. The
4357 -- generated code can contain conversions which are
4358 -- not subject to this test, and we cannot extract the
4359 -- component type in such cases since it is not
4360 -- present.
4362 elsif Comes_From_Source (A)
4363 and then Ada_Version >= Ada_2005
4364 then
4365 declare
4366 Comp_Type : constant Entity_Id :=
4367 Component_Type (Expr_Typ);
4368 begin
4369 if (Is_Private_Type (Comp_Type)
4370 and then not Is_Generic_Type (Comp_Type))
4371 or else Is_Tagged_Type (Comp_Type)
4372 or else Is_Volatile (Comp_Type)
4373 then
4374 Error_Msg_N
4375 ("component type of a view conversion " &
4376 "cannot be private, tagged, or volatile" &
4377 " (RM 4.6 (24))",
4378 Expression (A));
4379 end if;
4380 end;
4381 end if;
4382 end if;
4384 -- AI12-0074 & AI12-0377
4385 -- Check 6.4.1: If the mode is out, the actual parameter is
4386 -- a view conversion, and the type of the formal parameter
4387 -- is a scalar type, then either:
4388 -- - the target and operand type both do not have the
4389 -- Default_Value aspect specified; or
4390 -- - the target and operand type both have the
4391 -- Default_Value aspect specified, and there shall exist
4392 -- a type (other than a root numeric type) that is an
4393 -- ancestor of both the target type and the operand
4394 -- type.
4396 elsif Ekind (F) = E_Out_Parameter
4397 and then Is_Scalar_Type (Etype (F))
4398 then
4399 if Has_Default_Aspect (Etype (F)) /=
4400 Has_Default_Aspect (Expr_Typ)
4401 then
4402 Error_Msg_N
4403 ("view conversion requires Default_Value on both " &
4404 "types (RM 6.4.1)", A);
4405 elsif Has_Default_Aspect (Expr_Typ)
4406 and then not Same_Ancestor (Etype (F), Expr_Typ)
4407 then
4408 Error_Msg_N
4409 ("view conversion between unrelated types with "
4410 & "Default_Value not allowed (RM 6.4.1)", A);
4411 end if;
4412 end if;
4413 end;
4415 -- Resolve expression if conversion is all OK
4417 if (Conversion_OK (A)
4418 or else Valid_Conversion (A, Etype (A), Expression (A)))
4419 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4420 then
4421 Resolve (Expression (A));
4422 end if;
4424 -- If the actual is a function call that returns a limited
4425 -- unconstrained object that needs finalization, create a
4426 -- transient scope for it, so that it can receive the proper
4427 -- finalization list.
4429 elsif Expander_Active
4430 and then Nkind (A) = N_Function_Call
4431 and then Is_Limited_Record (Etype (F))
4432 and then not Is_Constrained (Etype (F))
4433 and then (Needs_Finalization (Etype (F))
4434 or else Has_Task (Etype (F)))
4435 then
4436 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4437 Resolve (A, Etype (F));
4439 -- A small optimization: if one of the actuals is a concatenation
4440 -- create a block around a procedure call to recover stack space.
4441 -- This alleviates stack usage when several procedure calls in
4442 -- the same statement list use concatenation. We do not perform
4443 -- this wrapping for code statements, where the argument is a
4444 -- static string, and we want to preserve warnings involving
4445 -- sequences of such statements.
4447 elsif Expander_Active
4448 and then Nkind (A) = N_Op_Concat
4449 and then Nkind (N) = N_Procedure_Call_Statement
4450 and then not (Is_Intrinsic_Subprogram (Nam)
4451 and then Chars (Nam) = Name_Asm)
4452 and then not Static_Concatenation (A)
4453 then
4454 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4455 Resolve (A, Etype (F));
4457 else
4458 if Nkind (A) = N_Type_Conversion
4459 and then Is_Array_Type (Etype (F))
4460 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4461 and then
4462 (Is_Limited_Type (Etype (F))
4463 or else Is_Limited_Type (Etype (Expression (A))))
4464 then
4465 Error_Msg_N
4466 ("conversion between unrelated limited array types not "
4467 & "allowed ('A'I-00246)", A);
4469 if Is_Limited_Type (Etype (F)) then
4470 Explain_Limited_Type (Etype (F), A);
4471 end if;
4473 if Is_Limited_Type (Etype (Expression (A))) then
4474 Explain_Limited_Type (Etype (Expression (A)), A);
4475 end if;
4476 end if;
4478 -- (Ada 2005: AI-251): If the actual is an allocator whose
4479 -- directly designated type is a class-wide interface, we build
4480 -- an anonymous access type to use it as the type of the
4481 -- allocator. Later, when the subprogram call is expanded, if
4482 -- the interface has a secondary dispatch table the expander
4483 -- will add a type conversion to force the correct displacement
4484 -- of the pointer.
4486 if Nkind (A) = N_Allocator then
4487 declare
4488 DDT : constant Entity_Id :=
4489 Directly_Designated_Type (Base_Type (Etype (F)));
4491 begin
4492 -- Displace the pointer to the object to reference its
4493 -- secondary dispatch table.
4495 if Is_Class_Wide_Type (DDT)
4496 and then Is_Interface (DDT)
4497 then
4498 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4499 Analyze_And_Resolve (A, Etype (F),
4500 Suppress => Access_Check);
4501 end if;
4503 -- Ada 2005, AI-162:If the actual is an allocator, the
4504 -- innermost enclosing statement is the master of the
4505 -- created object. This needs to be done with expansion
4506 -- enabled only, otherwise the transient scope will not
4507 -- be removed in the expansion of the wrapped construct.
4509 if Expander_Active
4510 and then (Needs_Finalization (DDT)
4511 or else Has_Task (DDT))
4512 then
4513 Establish_Transient_Scope
4514 (A, Manage_Sec_Stack => False);
4515 end if;
4516 end;
4518 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4519 Check_Restriction (No_Access_Parameter_Allocators, A);
4520 end if;
4521 end if;
4523 -- (Ada 2005): The call may be to a primitive operation of a
4524 -- tagged synchronized type, declared outside of the type. In
4525 -- this case the controlling actual must be converted to its
4526 -- corresponding record type, which is the formal type. The
4527 -- actual may be a subtype, either because of a constraint or
4528 -- because it is a generic actual, so use base type to locate
4529 -- concurrent type.
4531 F_Typ := Base_Type (Etype (F));
4533 if Is_Tagged_Type (F_Typ)
4534 and then (Is_Concurrent_Type (F_Typ)
4535 or else Is_Concurrent_Record_Type (F_Typ))
4536 then
4537 -- If the actual is overloaded, look for an interpretation
4538 -- that has a synchronized type.
4540 if not Is_Overloaded (A) then
4541 A_Typ := Base_Type (Etype (A));
4543 else
4544 declare
4545 Index : Interp_Index;
4546 It : Interp;
4548 begin
4549 Get_First_Interp (A, Index, It);
4550 while Present (It.Typ) loop
4551 if Is_Concurrent_Type (It.Typ)
4552 or else Is_Concurrent_Record_Type (It.Typ)
4553 then
4554 A_Typ := Base_Type (It.Typ);
4555 exit;
4556 end if;
4558 Get_Next_Interp (Index, It);
4559 end loop;
4560 end;
4561 end if;
4563 declare
4564 Full_A_Typ : Entity_Id;
4566 begin
4567 if Present (Full_View (A_Typ)) then
4568 Full_A_Typ := Base_Type (Full_View (A_Typ));
4569 else
4570 Full_A_Typ := A_Typ;
4571 end if;
4573 -- Tagged synchronized type (case 1): the actual is a
4574 -- concurrent type.
4576 if Is_Concurrent_Type (A_Typ)
4577 and then Corresponding_Record_Type (A_Typ) = F_Typ
4578 then
4579 Rewrite (A,
4580 Unchecked_Convert_To
4581 (Corresponding_Record_Type (A_Typ), A));
4582 Resolve (A, Etype (F));
4584 -- Tagged synchronized type (case 2): the formal is a
4585 -- concurrent type.
4587 elsif Ekind (Full_A_Typ) = E_Record_Type
4588 and then Present
4589 (Corresponding_Concurrent_Type (Full_A_Typ))
4590 and then Is_Concurrent_Type (F_Typ)
4591 and then Present (Corresponding_Record_Type (F_Typ))
4592 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4593 then
4594 Resolve (A, Corresponding_Record_Type (F_Typ));
4596 -- Common case
4598 else
4599 Resolve (A, Etype (F));
4600 end if;
4601 end;
4603 -- Not a synchronized operation
4605 else
4606 Resolve (A, Etype (F));
4607 end if;
4608 end if;
4610 A_Typ := Etype (A);
4611 F_Typ := Etype (F);
4613 -- An actual cannot be an untagged formal incomplete type
4615 if Ekind (A_Typ) = E_Incomplete_Type
4616 and then not Is_Tagged_Type (A_Typ)
4617 and then Is_Generic_Type (A_Typ)
4618 then
4619 Error_Msg_N
4620 ("invalid use of untagged formal incomplete type", A);
4621 end if;
4623 -- has warnings suppressed, then we reset Never_Set_In_Source for
4624 -- the calling entity. The reason for this is to catch cases like
4625 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4626 -- uses trickery to modify an IN parameter.
4628 if Ekind (F) = E_In_Parameter
4629 and then Is_Entity_Name (A)
4630 and then Present (Entity (A))
4631 and then Ekind (Entity (A)) = E_Variable
4632 and then Has_Warnings_Off (F_Typ)
4633 then
4634 Set_Never_Set_In_Source (Entity (A), False);
4635 end if;
4637 -- Perform error checks for IN and IN OUT parameters
4639 if Ekind (F) /= E_Out_Parameter then
4641 -- Check unset reference. For scalar parameters, it is clearly
4642 -- wrong to pass an uninitialized value as either an IN or
4643 -- IN-OUT parameter. For composites, it is also clearly an
4644 -- error to pass a completely uninitialized value as an IN
4645 -- parameter, but the case of IN OUT is trickier. We prefer
4646 -- not to give a warning here. For example, suppose there is
4647 -- a routine that sets some component of a record to False.
4648 -- It is perfectly reasonable to make this IN-OUT and allow
4649 -- either initialized or uninitialized records to be passed
4650 -- in this case.
4652 -- For partially initialized composite values, we also avoid
4653 -- warnings, since it is quite likely that we are passing a
4654 -- partially initialized value and only the initialized fields
4655 -- will in fact be read in the subprogram.
4657 if Is_Scalar_Type (A_Typ)
4658 or else (Ekind (F) = E_In_Parameter
4659 and then not Is_Partially_Initialized_Type (A_Typ))
4660 then
4661 Check_Unset_Reference (A);
4662 end if;
4664 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4665 -- actual to a nested call, since this constitutes a reading of
4666 -- the parameter, which is not allowed.
4668 if Ada_Version = Ada_83
4669 and then Is_Entity_Name (A)
4670 and then Ekind (Entity (A)) = E_Out_Parameter
4671 then
4672 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4673 end if;
4674 end if;
4676 -- In -gnatd.q mode, forget that a given array is constant when
4677 -- it is passed as an IN parameter to a foreign-convention
4678 -- subprogram. This is in case the subprogram evilly modifies the
4679 -- object. Of course, correct code would use IN OUT.
4681 if Debug_Flag_Dot_Q
4682 and then Ekind (F) = E_In_Parameter
4683 and then Has_Foreign_Convention (Nam)
4684 and then Is_Array_Type (F_Typ)
4685 and then Nkind (A) in N_Has_Entity
4686 and then Present (Entity (A))
4687 then
4688 Set_Is_True_Constant (Entity (A), False);
4689 end if;
4691 -- Case of OUT or IN OUT parameter
4693 if Ekind (F) /= E_In_Parameter then
4695 -- For an Out parameter, check for useless assignment. Note
4696 -- that we can't set Last_Assignment this early, because we may
4697 -- kill current values in Resolve_Call, and that call would
4698 -- clobber the Last_Assignment field.
4700 -- Note: call Warn_On_Useless_Assignment before doing the check
4701 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4702 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4703 -- reflects the last assignment, not this one.
4705 if Ekind (F) = E_Out_Parameter then
4706 if Warn_On_Modified_As_Out_Parameter (F)
4707 and then Is_Entity_Name (A)
4708 and then Present (Entity (A))
4709 and then Comes_From_Source (N)
4710 then
4711 Warn_On_Useless_Assignment (Entity (A), A);
4712 end if;
4713 end if;
4715 -- Validate the form of the actual. Note that the call to
4716 -- Is_OK_Variable_For_Out_Formal generates the required
4717 -- reference in this case.
4719 -- A call to an initialization procedure for an aggregate
4720 -- component may initialize a nested component of a constant
4721 -- designated object. In this context the object is variable.
4723 if not Is_OK_Variable_For_Out_Formal (A)
4724 and then not Is_Init_Proc (Nam)
4725 then
4726 Error_Msg_NE ("actual for& must be a variable", A, F);
4728 if Is_Subprogram (Current_Scope) then
4729 if Is_Invariant_Procedure (Current_Scope)
4730 or else Is_Partial_Invariant_Procedure (Current_Scope)
4731 then
4732 Error_Msg_N
4733 ("function used in invariant cannot modify its "
4734 & "argument", F);
4736 elsif Is_Predicate_Function (Current_Scope) then
4737 Error_Msg_N
4738 ("function used in predicate cannot modify its "
4739 & "argument", F);
4740 end if;
4741 end if;
4742 end if;
4744 -- What's the following about???
4746 if Is_Entity_Name (A) then
4747 Kill_Checks (Entity (A));
4748 else
4749 Kill_All_Checks;
4750 end if;
4751 end if;
4753 if A_Typ = Any_Type then
4754 Set_Etype (N, Any_Type);
4755 return;
4756 end if;
4758 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4760 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4762 -- Apply predicate tests except in certain special cases. Note
4763 -- that it might be more consistent to apply these only when
4764 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4765 -- for the outbound predicate tests ??? In any case indicate
4766 -- the function being called, for better warnings if the call
4767 -- leads to an infinite recursion.
4769 if Predicate_Tests_On_Arguments (Nam) then
4770 Apply_Predicate_Check (A, F_Typ, Nam);
4771 end if;
4773 -- Apply required constraint checks
4775 if Is_Scalar_Type (A_Typ) then
4776 Apply_Scalar_Range_Check (A, F_Typ);
4778 elsif Is_Array_Type (A_Typ) then
4779 Apply_Length_Check (A, F_Typ);
4781 elsif Is_Record_Type (F_Typ)
4782 and then Has_Discriminants (F_Typ)
4783 and then Is_Constrained (F_Typ)
4784 and then (not Is_Derived_Type (F_Typ)
4785 or else Comes_From_Source (Nam))
4786 then
4787 Apply_Discriminant_Check (A, F_Typ);
4789 -- For view conversions of a discriminated object, apply
4790 -- check to object itself, the conversion alreay has the
4791 -- proper type.
4793 if Nkind (A) = N_Type_Conversion
4794 and then Is_Constrained (Etype (Expression (A)))
4795 then
4796 Apply_Discriminant_Check (Expression (A), F_Typ);
4797 end if;
4799 elsif Is_Access_Type (F_Typ)
4800 and then Is_Array_Type (Designated_Type (F_Typ))
4801 and then Is_Constrained (Designated_Type (F_Typ))
4802 then
4803 Apply_Length_Check (A, F_Typ);
4805 elsif Is_Access_Type (F_Typ)
4806 and then Has_Discriminants (Designated_Type (F_Typ))
4807 and then Is_Constrained (Designated_Type (F_Typ))
4808 then
4809 Apply_Discriminant_Check (A, F_Typ);
4811 else
4812 Apply_Range_Check (A, F_Typ);
4813 end if;
4815 -- Ada 2005 (AI-231): Note that the controlling parameter case
4816 -- already existed in Ada 95, which is partially checked
4817 -- elsewhere (see Checks), and we don't want the warning
4818 -- message to differ.
4820 if Is_Access_Type (F_Typ)
4821 and then Can_Never_Be_Null (F_Typ)
4822 and then Known_Null (A)
4823 then
4824 if Is_Controlling_Formal (F) then
4825 Apply_Compile_Time_Constraint_Error
4826 (N => A,
4827 Msg => "null value not allowed here??",
4828 Reason => CE_Access_Check_Failed);
4830 elsif Ada_Version >= Ada_2005 then
4831 Apply_Compile_Time_Constraint_Error
4832 (N => A,
4833 Msg => "(Ada 2005) NULL not allowed in "
4834 & "null-excluding formal??",
4835 Reason => CE_Null_Not_Allowed);
4836 end if;
4837 end if;
4838 end if;
4840 -- Checks for OUT parameters and IN OUT parameters
4842 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4844 -- If there is a type conversion, make sure the return value
4845 -- meets the constraints of the variable before the conversion.
4847 if Nkind (A) = N_Type_Conversion then
4848 if Is_Scalar_Type (A_Typ) then
4850 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4851 -- which would prevent the check from being generated.
4852 -- This is for Starlet only though, so long obsolete.
4854 if Mechanism (F) = By_Reference
4855 and then Ekind (Nam) = E_Procedure
4856 and then Is_Valued_Procedure (Nam)
4857 then
4858 null;
4859 else
4860 Apply_Scalar_Range_Check
4861 (Expression (A), Etype (Expression (A)), A_Typ);
4862 end if;
4864 -- In addition the return value must meet the constraints
4865 -- of the object type (see the comment below).
4867 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4869 else
4870 Apply_Range_Check
4871 (Expression (A), Etype (Expression (A)), A_Typ);
4872 end if;
4874 -- If no conversion, apply scalar range checks and length check
4875 -- based on the subtype of the actual (NOT that of the formal).
4876 -- This indicates that the check takes place on return from the
4877 -- call. During expansion the required constraint checks are
4878 -- inserted. In GNATprove mode, in the absence of expansion,
4879 -- the flag indicates that the returned value is valid.
4881 else
4882 if Is_Scalar_Type (F_Typ) then
4883 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4885 elsif Is_Array_Type (F_Typ)
4886 and then Ekind (F) = E_Out_Parameter
4887 then
4888 Apply_Length_Check (A, F_Typ);
4890 else
4891 Apply_Range_Check (A, A_Typ, F_Typ);
4892 end if;
4893 end if;
4895 -- Note: we do not apply the predicate checks for the case of
4896 -- OUT and IN OUT parameters. They are instead applied in the
4897 -- Expand_Actuals routine in Exp_Ch6.
4898 end if;
4900 -- If the formal is of an unconstrained array subtype with fixed
4901 -- lower bound, then sliding to that bound may be needed.
4903 if Is_Fixed_Lower_Bound_Array_Subtype (F_Typ) then
4904 Expand_Sliding_Conversion (A, F_Typ);
4905 end if;
4907 -- An actual associated with an access parameter is implicitly
4908 -- converted to the anonymous access type of the formal and must
4909 -- satisfy the legality checks for access conversions.
4911 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4912 if not Valid_Conversion (A, F_Typ, A) then
4913 Error_Msg_N
4914 ("invalid implicit conversion for access parameter", A);
4915 end if;
4917 -- If the actual is an access selected component of a variable,
4918 -- the call may modify its designated object. It is reasonable
4919 -- to treat this as a potential modification of the enclosing
4920 -- record, to prevent spurious warnings that it should be
4921 -- declared as a constant, because intuitively programmers
4922 -- regard the designated subcomponent as part of the record.
4924 if Nkind (A) = N_Selected_Component
4925 and then Is_Entity_Name (Prefix (A))
4926 and then not Is_Constant_Object (Entity (Prefix (A)))
4927 then
4928 Note_Possible_Modification (A, Sure => False);
4929 end if;
4930 end if;
4932 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
4934 if (Is_By_Reference_Type (F_Typ) or else Is_Aliased (F))
4935 and then Comes_From_Source (N)
4936 then
4937 if Is_Atomic_Object (A)
4938 and then not Is_Atomic (F_Typ)
4939 then
4940 Error_Msg_NE
4941 ("cannot pass atomic object to nonatomic formal&",
4942 A, F);
4943 Error_Msg_N
4944 ("\which is passed by reference (RM C.6(12))", A);
4946 elsif Is_Volatile_Object_Ref (A)
4947 and then not Is_Volatile (F_Typ)
4948 then
4949 Error_Msg_NE
4950 ("cannot pass volatile object to nonvolatile formal&",
4951 A, F);
4952 Error_Msg_N
4953 ("\which is passed by reference (RM C.6(12))", A);
4955 elsif Is_Volatile_Full_Access_Object_Ref (A)
4956 and then not Is_Volatile_Full_Access (F_Typ)
4957 then
4958 Error_Msg_NE
4959 ("cannot pass full access object to nonfull access "
4960 & "formal&", A, F);
4961 Error_Msg_N
4962 ("\which is passed by reference (RM C.6(12))", A);
4963 end if;
4965 -- Check for nonatomic subcomponent of a full access object
4966 -- in Ada 2022 (RM C.6 (12)).
4968 if Ada_Version >= Ada_2022
4969 and then Is_Subcomponent_Of_Full_Access_Object (A)
4970 and then not Is_Atomic_Object (A)
4971 then
4972 Error_Msg_N
4973 ("cannot pass nonatomic subcomponent of full access "
4974 & "object", A);
4975 Error_Msg_NE
4976 ("\to formal & which is passed by reference (RM C.6(12))",
4977 A, F);
4978 end if;
4979 end if;
4981 -- Check that subprograms don't have improper controlling
4982 -- arguments (RM 3.9.2 (9)).
4984 -- A primitive operation may have an access parameter of an
4985 -- incomplete tagged type, but a dispatching call is illegal
4986 -- if the type is still incomplete.
4988 if Is_Controlling_Formal (F) then
4989 Set_Is_Controlling_Actual (A);
4991 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4992 declare
4993 Desig : constant Entity_Id := Designated_Type (F_Typ);
4994 begin
4995 if Ekind (Desig) = E_Incomplete_Type
4996 and then No (Full_View (Desig))
4997 and then No (Non_Limited_View (Desig))
4998 then
4999 Error_Msg_NE
5000 ("premature use of incomplete type& "
5001 & "in dispatching call", A, Desig);
5002 end if;
5003 end;
5004 end if;
5006 elsif Nkind (A) = N_Explicit_Dereference then
5007 Validate_Remote_Access_To_Class_Wide_Type (A);
5008 end if;
5010 -- Apply legality rule 3.9.2 (9/1)
5012 -- Skip this check on helpers and indirect-call wrappers built to
5013 -- support class-wide preconditions.
5015 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
5016 and then not Is_Class_Wide_Type (F_Typ)
5017 and then not Is_Controlling_Formal (F)
5018 and then not In_Instance
5019 and then (not Is_Subprogram (Nam)
5020 or else No (Class_Preconditions_Subprogram (Nam)))
5021 then
5022 Error_Msg_N ("class-wide argument not allowed here!", A);
5024 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5025 Error_Msg_Node_2 := F_Typ;
5026 Error_Msg_NE
5027 ("& is not a dispatching operation of &!", A, Nam);
5028 end if;
5030 -- Apply the checks described in 3.10.2(27): if the context is a
5031 -- specific access-to-object, the actual cannot be class-wide.
5032 -- Use base type to exclude access_to_subprogram cases.
5034 elsif Is_Access_Type (A_Typ)
5035 and then Is_Access_Type (F_Typ)
5036 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
5037 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
5038 or else (Nkind (A) = N_Attribute_Reference
5039 and then
5040 Is_Class_Wide_Type (Etype (Prefix (A)))))
5041 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
5042 and then not Is_Controlling_Formal (F)
5044 -- Disable these checks for call to imported C++ subprograms
5046 and then not
5047 (Is_Entity_Name (Name (N))
5048 and then Is_Imported (Entity (Name (N)))
5049 and then Convention (Entity (Name (N))) = Convention_CPP)
5050 then
5051 Error_Msg_N
5052 ("access to class-wide argument not allowed here!", A);
5054 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5055 Error_Msg_Node_2 := Designated_Type (F_Typ);
5056 Error_Msg_NE
5057 ("& is not a dispatching operation of &!", A, Nam);
5058 end if;
5059 end if;
5061 Check_Aliased_Parameter;
5063 Eval_Actual (A);
5065 -- If it is a named association, treat the selector_name as a
5066 -- proper identifier, and mark the corresponding entity.
5068 if Nkind (Parent (A)) = N_Parameter_Association
5070 -- Ignore reference in SPARK mode, as it refers to an entity not
5071 -- in scope at the point of reference, so the reference should
5072 -- be ignored for computing effects of subprograms.
5074 and then not GNATprove_Mode
5075 then
5076 -- If subprogram is overridden, use name of formal that
5077 -- is being called.
5079 if Present (Real_Subp) then
5080 Set_Entity (Selector_Name (Parent (A)), Real_F);
5081 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
5083 else
5084 Set_Entity (Selector_Name (Parent (A)), F);
5085 Generate_Reference (F, Selector_Name (Parent (A)));
5086 Set_Etype (Selector_Name (Parent (A)), F_Typ);
5087 Generate_Reference (F_Typ, N, ' ');
5088 end if;
5089 end if;
5091 Prev := A;
5093 if Ekind (F) /= E_Out_Parameter then
5094 Check_Unset_Reference (A);
5095 end if;
5097 -- The following checks are only relevant when SPARK_Mode is on as
5098 -- they are not standard Ada legality rule. Internally generated
5099 -- temporaries are ignored.
5101 if SPARK_Mode = On and then Comes_From_Source (A) then
5103 -- Inspect the expression and flag each effectively volatile
5104 -- object for reading as illegal because it appears within
5105 -- an interfering context. Note that this is usually done
5106 -- in Resolve_Entity_Name, but when the effectively volatile
5107 -- object for reading appears as an actual in a call, the call
5108 -- must be resolved first.
5110 Flag_Effectively_Volatile_Objects (A);
5111 end if;
5113 -- A formal parameter of a specific tagged type whose related
5114 -- subprogram is subject to pragma Extensions_Visible with value
5115 -- "False" cannot act as an actual in a subprogram with value
5116 -- "True" (SPARK RM 6.1.7(3)).
5118 -- No check needed for helpers and indirect-call wrappers built to
5119 -- support class-wide preconditions.
5121 if Is_EVF_Expression (A)
5122 and then Extensions_Visible_Status (Nam) =
5123 Extensions_Visible_True
5124 and then No (Class_Preconditions_Subprogram (Current_Scope))
5125 then
5126 Error_Msg_N
5127 ("formal parameter cannot act as actual parameter when "
5128 & "Extensions_Visible is False", A);
5129 Error_Msg_NE
5130 ("\subprogram & has Extensions_Visible True", A, Nam);
5131 end if;
5133 -- The actual parameter of a Ghost subprogram whose formal is of
5134 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5136 if Comes_From_Source (Nam)
5137 and then Is_Ghost_Entity (Nam)
5138 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5139 and then Is_Entity_Name (A)
5140 and then Present (Entity (A))
5141 and then not Is_Ghost_Entity (Entity (A))
5142 then
5143 Error_Msg_NE
5144 ("non-ghost variable & cannot appear as actual in call to "
5145 & "ghost procedure", A, Entity (A));
5147 if Ekind (F) = E_In_Out_Parameter then
5148 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5149 else
5150 Error_Msg_N ("\corresponding formal has mode OUT", A);
5151 end if;
5152 end if;
5154 -- (AI12-0397): The target of a subprogram call that occurs within
5155 -- the expression of an Default_Initial_Condition aspect and has
5156 -- an actual that is the current instance of the type must be
5157 -- either a primitive of the type or a class-wide subprogram,
5158 -- because the type of the current instance in such an aspect is
5159 -- considered to be a notional formal derived type whose only
5160 -- operations correspond to the primitives of the enclosing type.
5161 -- Nonprimitives can be called, but the current instance must be
5162 -- converted rather than passed directly. Note that a current
5163 -- instance of a type with DIC will occur as a reference to an
5164 -- in-mode formal of an enclosing DIC procedure or partial DIC
5165 -- procedure. (It seems that this check should perhaps also apply
5166 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5167 -- aspects???)
5169 if Nkind (A) = N_Identifier
5170 and then Ekind (Entity (A)) = E_In_Parameter
5172 and then Is_Subprogram (Scope (Entity (A)))
5173 and then Is_DIC_Procedure (Scope (Entity (A)))
5175 -- We check Comes_From_Source to exclude inherited primitives
5176 -- from being flagged, because such subprograms turn out to not
5177 -- always have the Is_Primitive flag set. ???
5179 and then Comes_From_Source (Nam)
5181 and then not Is_Primitive (Nam)
5182 and then not Is_Class_Wide_Type (F_Typ)
5183 then
5184 Error_Msg_NE
5185 ("call to nonprimitive & with current instance not allowed " &
5186 "for aspect", A, Nam);
5187 end if;
5189 Next_Actual (A);
5191 -- Case where actual is not present
5193 else
5194 Insert_Default;
5195 end if;
5197 Next_Formal (F);
5199 if Present (Real_Subp) then
5200 Next_Formal (Real_F);
5201 end if;
5202 end loop;
5203 end Resolve_Actuals;
5205 -----------------------
5206 -- Resolve_Allocator --
5207 -----------------------
5209 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5210 Desig_T : constant Entity_Id := Designated_Type (Typ);
5211 E : constant Node_Id := Expression (N);
5212 Subtyp : Entity_Id;
5213 Discrim : Entity_Id;
5214 Constr : Node_Id;
5215 Aggr : Node_Id;
5216 Assoc : Node_Id := Empty;
5217 Disc_Exp : Node_Id;
5219 procedure Check_Allocator_Discrim_Accessibility
5220 (Disc_Exp : Node_Id;
5221 Alloc_Typ : Entity_Id);
5222 -- Check that accessibility level associated with an access discriminant
5223 -- initialized in an allocator by the expression Disc_Exp is not deeper
5224 -- than the level of the allocator type Alloc_Typ. An error message is
5225 -- issued if this condition is violated. Specialized checks are done for
5226 -- the cases of a constraint expression which is an access attribute or
5227 -- an access discriminant.
5229 procedure Check_Allocator_Discrim_Accessibility_Exprs
5230 (Curr_Exp : Node_Id;
5231 Alloc_Typ : Entity_Id);
5232 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5233 -- across all expressions within a given conditional expression.
5235 function In_Dispatching_Context return Boolean;
5236 -- If the allocator is an actual in a call, it is allowed to be class-
5237 -- wide when the context is not because it is a controlling actual.
5239 -------------------------------------------
5240 -- Check_Allocator_Discrim_Accessibility --
5241 -------------------------------------------
5243 procedure Check_Allocator_Discrim_Accessibility
5244 (Disc_Exp : Node_Id;
5245 Alloc_Typ : Entity_Id)
5247 begin
5248 if Type_Access_Level (Etype (Disc_Exp)) >
5249 Deepest_Type_Access_Level (Alloc_Typ)
5250 then
5251 Error_Msg_N
5252 ("operand type has deeper level than allocator type", Disc_Exp);
5254 -- When the expression is an Access attribute the level of the prefix
5255 -- object must not be deeper than that of the allocator's type.
5257 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5258 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5259 Attribute_Access
5260 and then Static_Accessibility_Level
5261 (Disc_Exp, Zero_On_Dynamic_Level)
5262 > Deepest_Type_Access_Level (Alloc_Typ)
5263 then
5264 Error_Msg_N
5265 ("prefix of attribute has deeper level than allocator type",
5266 Disc_Exp);
5268 -- When the expression is an access discriminant the check is against
5269 -- the level of the prefix object.
5271 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5272 and then Nkind (Disc_Exp) = N_Selected_Component
5273 and then Static_Accessibility_Level
5274 (Disc_Exp, Zero_On_Dynamic_Level)
5275 > Deepest_Type_Access_Level (Alloc_Typ)
5276 then
5277 Error_Msg_N
5278 ("access discriminant has deeper level than allocator type",
5279 Disc_Exp);
5281 -- All other cases are legal
5283 else
5284 null;
5285 end if;
5286 end Check_Allocator_Discrim_Accessibility;
5288 -------------------------------------------------
5289 -- Check_Allocator_Discrim_Accessibility_Exprs --
5290 -------------------------------------------------
5292 procedure Check_Allocator_Discrim_Accessibility_Exprs
5293 (Curr_Exp : Node_Id;
5294 Alloc_Typ : Entity_Id)
5296 Alt : Node_Id;
5297 Expr : Node_Id;
5298 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5299 begin
5300 -- When conditional expressions are constant folded we know at
5301 -- compile time which expression to check - so don't bother with
5302 -- the rest of the cases.
5304 if Nkind (Curr_Exp) = N_Attribute_Reference then
5305 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5307 -- Non-constant-folded if expressions
5309 elsif Nkind (Disc_Exp) = N_If_Expression then
5310 -- Check both expressions if they are still present in the face
5311 -- of expansion.
5313 Expr := Next (First (Expressions (Disc_Exp)));
5314 if Present (Expr) then
5315 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5316 Next (Expr);
5317 if Present (Expr) then
5318 Check_Allocator_Discrim_Accessibility_Exprs
5319 (Expr, Alloc_Typ);
5320 end if;
5321 end if;
5323 -- Non-constant-folded case expressions
5325 elsif Nkind (Disc_Exp) = N_Case_Expression then
5326 -- Check all alternatives
5328 Alt := First (Alternatives (Disc_Exp));
5329 while Present (Alt) loop
5330 Check_Allocator_Discrim_Accessibility_Exprs
5331 (Expression (Alt), Alloc_Typ);
5333 Next (Alt);
5334 end loop;
5336 -- Base case, check the accessibility of the original node of the
5337 -- expression.
5339 else
5340 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5341 end if;
5342 end Check_Allocator_Discrim_Accessibility_Exprs;
5344 ----------------------------
5345 -- In_Dispatching_Context --
5346 ----------------------------
5348 function In_Dispatching_Context return Boolean is
5349 Par : constant Node_Id := Parent (N);
5351 begin
5352 return Nkind (Par) in N_Subprogram_Call
5353 and then Is_Entity_Name (Name (Par))
5354 and then Is_Dispatching_Operation (Entity (Name (Par)));
5355 end In_Dispatching_Context;
5357 -- Start of processing for Resolve_Allocator
5359 begin
5360 -- Replace general access with specific type
5362 if Ekind (Etype (N)) = E_Allocator_Type then
5363 Set_Etype (N, Base_Type (Typ));
5364 end if;
5366 if Is_Abstract_Type (Typ) then
5367 Error_Msg_N ("type of allocator cannot be abstract", N);
5368 end if;
5370 -- For qualified expression, resolve the expression using the given
5371 -- subtype (nothing to do for type mark, subtype indication)
5373 if Nkind (E) = N_Qualified_Expression then
5374 if Is_Class_Wide_Type (Etype (E))
5375 and then not Is_Class_Wide_Type (Desig_T)
5376 and then not In_Dispatching_Context
5377 then
5378 Error_Msg_N
5379 ("class-wide allocator not allowed for this access type", N);
5380 end if;
5382 -- Do a full resolution to apply constraint and predicate checks
5384 Resolve_Qualified_Expression (E, Etype (E));
5385 Check_Unset_Reference (Expression (E));
5387 -- Allocators generated by the build-in-place expansion mechanism
5388 -- are explicitly marked as coming from source but do not need to be
5389 -- checked for limited initialization. To exclude this case, ensure
5390 -- that the parent of the allocator is a source node.
5391 -- The return statement constructed for an Expression_Function does
5392 -- not come from source but requires a limited check.
5394 if Is_Limited_Type (Etype (E))
5395 and then Comes_From_Source (N)
5396 and then
5397 (Comes_From_Source (Parent (N))
5398 or else
5399 (Ekind (Current_Scope) = E_Function
5400 and then Nkind (Original_Node (Unit_Declaration_Node
5401 (Current_Scope))) = N_Expression_Function))
5402 and then not In_Instance_Body
5403 then
5404 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5405 if Nkind (Parent (N)) = N_Assignment_Statement then
5406 Error_Msg_N
5407 ("illegal expression for initialized allocator of a "
5408 & "limited type (RM 7.5 (2.7/2))", N);
5409 else
5410 Error_Msg_N
5411 ("initialization not allowed for limited types", N);
5412 end if;
5414 Explain_Limited_Type (Etype (E), N);
5415 end if;
5416 end if;
5418 -- Calls to build-in-place functions are not currently supported in
5419 -- allocators for access types associated with a simple storage pool.
5420 -- Supporting such allocators may require passing additional implicit
5421 -- parameters to build-in-place functions (or a significant revision
5422 -- of the current b-i-p implementation to unify the handling for
5423 -- multiple kinds of storage pools). ???
5425 if Is_Limited_View (Desig_T)
5426 and then Nkind (Expression (E)) = N_Function_Call
5427 then
5428 declare
5429 Pool : constant Entity_Id :=
5430 Associated_Storage_Pool (Root_Type (Typ));
5431 begin
5432 if Present (Pool)
5433 and then
5434 Present (Get_Rep_Pragma
5435 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5436 then
5437 Error_Msg_N
5438 ("limited function calls not yet supported in simple "
5439 & "storage pool allocators", Expression (E));
5440 end if;
5441 end;
5442 end if;
5444 -- A special accessibility check is needed for allocators that
5445 -- constrain access discriminants. The level of the type of the
5446 -- expression used to constrain an access discriminant cannot be
5447 -- deeper than the type of the allocator (in contrast to access
5448 -- parameters, where the level of the actual can be arbitrary).
5450 -- We can't use Valid_Conversion to perform this check because in
5451 -- general the type of the allocator is unrelated to the type of
5452 -- the access discriminant.
5454 if Ekind (Typ) /= E_Anonymous_Access_Type
5455 or else Is_Local_Anonymous_Access (Typ)
5456 then
5457 Subtyp := Entity (Subtype_Mark (E));
5459 Aggr := Original_Node (Expression (E));
5461 if Has_Discriminants (Subtyp)
5462 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5463 then
5464 Discrim := First_Discriminant (Base_Type (Subtyp));
5466 -- Get the first component expression of the aggregate
5468 if Present (Expressions (Aggr)) then
5469 Disc_Exp := First (Expressions (Aggr));
5471 elsif Present (Component_Associations (Aggr)) then
5472 Assoc := First (Component_Associations (Aggr));
5474 if Present (Assoc) then
5475 Disc_Exp := Expression (Assoc);
5476 else
5477 Disc_Exp := Empty;
5478 end if;
5480 else
5481 Disc_Exp := Empty;
5482 end if;
5484 while Present (Discrim) and then Present (Disc_Exp) loop
5485 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5486 Check_Allocator_Discrim_Accessibility_Exprs
5487 (Disc_Exp, Typ);
5488 end if;
5490 Next_Discriminant (Discrim);
5492 if Present (Discrim) then
5493 if Present (Assoc) then
5494 Next (Assoc);
5495 Disc_Exp := Expression (Assoc);
5497 elsif Present (Next (Disc_Exp)) then
5498 Next (Disc_Exp);
5500 else
5501 Assoc := First (Component_Associations (Aggr));
5503 if Present (Assoc) then
5504 Disc_Exp := Expression (Assoc);
5505 else
5506 Disc_Exp := Empty;
5507 end if;
5508 end if;
5509 end if;
5510 end loop;
5511 end if;
5512 end if;
5514 -- For a subtype mark or subtype indication, freeze the subtype
5516 else
5517 Freeze_Expression (E);
5519 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5520 Error_Msg_N
5521 ("initialization required for access-to-constant allocator", N);
5522 end if;
5524 -- A special accessibility check is needed for allocators that
5525 -- constrain access discriminants. The level of the type of the
5526 -- expression used to constrain an access discriminant cannot be
5527 -- deeper than the type of the allocator (in contrast to access
5528 -- parameters, where the level of the actual can be arbitrary).
5529 -- We can't use Valid_Conversion to perform this check because
5530 -- in general the type of the allocator is unrelated to the type
5531 -- of the access discriminant.
5533 if Nkind (Original_Node (E)) = N_Subtype_Indication
5534 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5535 or else Is_Local_Anonymous_Access (Typ))
5536 then
5537 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5539 if Has_Discriminants (Subtyp) then
5540 Discrim := First_Discriminant (Base_Type (Subtyp));
5541 Constr := First (Constraints (Constraint (Original_Node (E))));
5542 while Present (Discrim) and then Present (Constr) loop
5543 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5544 if Nkind (Constr) = N_Discriminant_Association then
5545 Disc_Exp := Expression (Constr);
5546 else
5547 Disc_Exp := Constr;
5548 end if;
5550 Check_Allocator_Discrim_Accessibility_Exprs
5551 (Disc_Exp, Typ);
5552 end if;
5554 Next_Discriminant (Discrim);
5555 Next (Constr);
5556 end loop;
5557 end if;
5558 end if;
5559 end if;
5561 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5562 -- check that the level of the type of the created object is not deeper
5563 -- than the level of the allocator's access type, since extensions can
5564 -- now occur at deeper levels than their ancestor types. This is a
5565 -- static accessibility level check; a run-time check is also needed in
5566 -- the case of an initialized allocator with a class-wide argument (see
5567 -- Expand_Allocator_Expression).
5569 if Ada_Version >= Ada_2005
5570 and then Is_Class_Wide_Type (Desig_T)
5571 then
5572 declare
5573 Exp_Typ : Entity_Id;
5575 begin
5576 if Nkind (E) = N_Qualified_Expression then
5577 Exp_Typ := Etype (E);
5578 elsif Nkind (E) = N_Subtype_Indication then
5579 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5580 else
5581 Exp_Typ := Entity (E);
5582 end if;
5584 if Type_Access_Level (Exp_Typ) >
5585 Deepest_Type_Access_Level (Typ)
5586 then
5587 if In_Instance_Body then
5588 Error_Msg_Warn := SPARK_Mode /= On;
5589 Error_Msg_N
5590 ("type in allocator has deeper level than designated "
5591 & "class-wide type<<", E);
5592 Error_Msg_N ("\Program_Error [<<", E);
5594 Rewrite (N,
5595 Make_Raise_Program_Error (Sloc (N),
5596 Reason => PE_Accessibility_Check_Failed));
5597 Set_Etype (N, Typ);
5599 -- Do not apply Ada 2005 accessibility checks on a class-wide
5600 -- allocator if the type given in the allocator is a formal
5601 -- type or within a formal package. A run-time check will be
5602 -- performed in the instance.
5604 elsif not Is_Generic_Type (Exp_Typ)
5605 and then not In_Generic_Formal_Package (Exp_Typ)
5606 then
5607 Error_Msg_N
5608 ("type in allocator has deeper level than designated "
5609 & "class-wide type", E);
5610 end if;
5611 end if;
5612 end;
5613 end if;
5615 -- Check for allocation from an empty storage pool. But do not complain
5616 -- if it's a return statement for a build-in-place function, because the
5617 -- allocator is there just in case the caller uses an allocator. If the
5618 -- caller does use an allocator, it will be caught at the call site.
5620 if No_Pool_Assigned (Typ)
5621 and then not Alloc_For_BIP_Return (N)
5622 then
5623 Error_Msg_N ("allocation from empty storage pool!", N);
5625 -- If the context is an unchecked conversion, as may happen within an
5626 -- inlined subprogram, the allocator is being resolved with its own
5627 -- anonymous type. In that case, if the target type has a specific
5628 -- storage pool, it must be inherited explicitly by the allocator type.
5630 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5631 and then No (Associated_Storage_Pool (Typ))
5632 then
5633 Set_Associated_Storage_Pool
5634 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5635 end if;
5637 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5638 Check_Restriction (No_Anonymous_Allocators, N);
5639 end if;
5641 -- Check that an allocator with task parts isn't for a nested access
5642 -- type when restriction No_Task_Hierarchy applies.
5644 if not Is_Library_Level_Entity (Base_Type (Typ))
5645 and then Has_Task (Base_Type (Desig_T))
5646 then
5647 Check_Restriction (No_Task_Hierarchy, N);
5648 end if;
5650 -- An illegal allocator may be rewritten as a raise Program_Error
5651 -- statement.
5653 if Nkind (N) = N_Allocator then
5655 -- Avoid coextension processing for an allocator that is the
5656 -- expansion of a build-in-place function call.
5658 if Nkind (Original_Node (N)) = N_Allocator
5659 and then Nkind (Expression (Original_Node (N))) =
5660 N_Qualified_Expression
5661 and then Nkind (Expression (Expression (Original_Node (N)))) =
5662 N_Function_Call
5663 and then Is_Expanded_Build_In_Place_Call
5664 (Expression (Expression (Original_Node (N))))
5665 then
5666 null; -- b-i-p function call case
5668 else
5669 -- An anonymous access discriminant is the definition of a
5670 -- coextension.
5672 if Ekind (Typ) = E_Anonymous_Access_Type
5673 and then Nkind (Associated_Node_For_Itype (Typ)) =
5674 N_Discriminant_Specification
5675 then
5676 declare
5677 Discr : constant Entity_Id :=
5678 Defining_Identifier (Associated_Node_For_Itype (Typ));
5680 begin
5681 Check_Restriction (No_Coextensions, N);
5683 -- Ada 2012 AI05-0052: If the designated type of the
5684 -- allocator is limited, then the allocator shall not
5685 -- be used to define the value of an access discriminant
5686 -- unless the discriminated type is immutably limited.
5688 if Ada_Version >= Ada_2012
5689 and then Is_Limited_Type (Desig_T)
5690 and then not Is_Limited_View (Scope (Discr))
5691 then
5692 Error_Msg_N
5693 ("only immutably limited types can have anonymous "
5694 & "access discriminants designating a limited type",
5696 end if;
5697 end;
5699 -- Avoid marking an allocator as a dynamic coextension if it is
5700 -- within a static construct.
5702 if not Is_Static_Coextension (N) then
5703 Set_Is_Dynamic_Coextension (N);
5705 -- Finalization and deallocation of coextensions utilizes an
5706 -- approximate implementation which does not directly adhere
5707 -- to the semantic rules. Warn on potential issues involving
5708 -- coextensions.
5710 if Is_Controlled (Desig_T) then
5711 Error_Msg_N
5712 ("??coextension will not be finalized when its "
5713 & "associated owner is deallocated or finalized", N);
5714 else
5715 Error_Msg_N
5716 ("??coextension will not be deallocated when its "
5717 & "associated owner is deallocated", N);
5718 end if;
5719 end if;
5721 -- Cleanup for potential static coextensions
5723 else
5724 Set_Is_Dynamic_Coextension (N, False);
5725 Set_Is_Static_Coextension (N, False);
5727 -- Anonymous access-to-controlled objects are not finalized on
5728 -- time because this involves run-time ownership and currently
5729 -- this property is not available. In rare cases the object may
5730 -- not be finalized at all. Warn on potential issues involving
5731 -- anonymous access-to-controlled objects.
5733 if Ekind (Typ) = E_Anonymous_Access_Type
5734 and then Is_Controlled_Active (Desig_T)
5735 then
5736 Error_Msg_N
5737 ("??object designated by anonymous access object might "
5738 & "not be finalized until its enclosing library unit "
5739 & "goes out of scope", N);
5740 Error_Msg_N ("\use named access type instead", N);
5741 end if;
5742 end if;
5743 end if;
5744 end if;
5746 -- Report a simple error: if the designated object is a local task,
5747 -- its body has not been seen yet, and its activation will fail an
5748 -- elaboration check.
5750 if Is_Task_Type (Desig_T)
5751 and then Scope (Base_Type (Desig_T)) = Current_Scope
5752 and then Is_Compilation_Unit (Current_Scope)
5753 and then Ekind (Current_Scope) = E_Package
5754 and then not In_Package_Body (Current_Scope)
5755 then
5756 Error_Msg_Warn := SPARK_Mode /= On;
5757 Error_Msg_N ("cannot activate task before body seen<<", N);
5758 Error_Msg_N ("\Program_Error [<<", N);
5759 end if;
5761 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5762 -- type with a task component on a subpool. This action must raise
5763 -- Program_Error at runtime.
5765 if Ada_Version >= Ada_2012
5766 and then Nkind (N) = N_Allocator
5767 and then Present (Subpool_Handle_Name (N))
5768 and then Has_Task (Desig_T)
5769 then
5770 Error_Msg_Warn := SPARK_Mode /= On;
5771 Error_Msg_N ("cannot allocate task on subpool<<", N);
5772 Error_Msg_N ("\Program_Error [<<", N);
5774 Rewrite (N,
5775 Make_Raise_Program_Error (Sloc (N),
5776 Reason => PE_Explicit_Raise));
5777 Set_Etype (N, Typ);
5778 end if;
5779 end Resolve_Allocator;
5781 ---------------------------
5782 -- Resolve_Arithmetic_Op --
5783 ---------------------------
5785 -- Used for resolving all arithmetic operators except exponentiation
5787 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5788 L : constant Node_Id := Left_Opnd (N);
5789 R : constant Node_Id := Right_Opnd (N);
5790 TL : constant Entity_Id := Base_Type (Etype (L));
5791 TR : constant Entity_Id := Base_Type (Etype (R));
5792 T : Entity_Id;
5793 Rop : Node_Id;
5795 B_Typ : constant Entity_Id := Base_Type (Typ);
5796 -- We do the resolution using the base type, because intermediate values
5797 -- in expressions always are of the base type, not a subtype of it.
5799 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5800 -- Returns True if N is in a context that expects "any real type"
5802 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5803 -- Return True iff given type is Integer or universal real/integer
5805 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5806 -- Choose type of integer literal in fixed-point operation to conform
5807 -- to available fixed-point type. T is the type of the other operand,
5808 -- which is needed to determine the expected type of N.
5810 procedure Set_Operand_Type (N : Node_Id);
5811 -- Set operand type to T if universal
5813 -------------------------------
5814 -- Expected_Type_Is_Any_Real --
5815 -------------------------------
5817 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5818 begin
5819 -- N is the expression after "delta" in a fixed_point_definition;
5820 -- see RM-3.5.9(6):
5822 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5823 | N_Decimal_Fixed_Point_Definition
5825 -- N is one of the bounds in a real_range_specification;
5826 -- see RM-3.5.7(5):
5828 | N_Real_Range_Specification
5830 -- N is the expression of a delta_constraint;
5831 -- see RM-J.3(3):
5833 | N_Delta_Constraint;
5834 end Expected_Type_Is_Any_Real;
5836 -----------------------------
5837 -- Is_Integer_Or_Universal --
5838 -----------------------------
5840 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5841 T : Entity_Id;
5842 Index : Interp_Index;
5843 It : Interp;
5845 begin
5846 if not Is_Overloaded (N) then
5847 T := Etype (N);
5848 return Base_Type (T) = Base_Type (Standard_Integer)
5849 or else Is_Universal_Numeric_Type (T);
5850 else
5851 Get_First_Interp (N, Index, It);
5852 while Present (It.Typ) loop
5853 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5854 or else Is_Universal_Numeric_Type (It.Typ)
5855 then
5856 return True;
5857 end if;
5859 Get_Next_Interp (Index, It);
5860 end loop;
5861 end if;
5863 return False;
5864 end Is_Integer_Or_Universal;
5866 ----------------------------
5867 -- Set_Mixed_Mode_Operand --
5868 ----------------------------
5870 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5871 Index : Interp_Index;
5872 It : Interp;
5874 begin
5875 if Universal_Interpretation (N) = Universal_Integer then
5877 -- A universal integer literal is resolved as standard integer
5878 -- except in the case of a fixed-point result, where we leave it
5879 -- as universal (to be handled by Exp_Fixd later on)
5881 if Is_Fixed_Point_Type (T) then
5882 Resolve (N, Universal_Integer);
5883 else
5884 Resolve (N, Standard_Integer);
5885 end if;
5887 elsif Universal_Interpretation (N) = Universal_Real
5888 and then (T = Base_Type (Standard_Integer)
5889 or else Is_Universal_Numeric_Type (T))
5890 then
5891 -- A universal real can appear in a fixed-type context. We resolve
5892 -- the literal with that context, even though this might raise an
5893 -- exception prematurely (the other operand may be zero).
5895 Resolve (N, B_Typ);
5897 elsif Etype (N) = Base_Type (Standard_Integer)
5898 and then T = Universal_Real
5899 and then Is_Overloaded (N)
5900 then
5901 -- Integer arg in mixed-mode operation. Resolve with universal
5902 -- type, in case preference rule must be applied.
5904 Resolve (N, Universal_Integer);
5906 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5908 -- If the operand is part of a fixed multiplication operation,
5909 -- a conversion will be applied to each operand, so resolve it
5910 -- with its own type.
5912 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
5913 Resolve (N);
5915 else
5916 -- Not a mixed-mode operation, resolve with context
5918 Resolve (N, B_Typ);
5919 end if;
5921 elsif Etype (N) = Any_Fixed then
5923 -- N may itself be a mixed-mode operation, so use context type
5925 Resolve (N, B_Typ);
5927 elsif Is_Fixed_Point_Type (T)
5928 and then B_Typ = Universal_Fixed
5929 and then Is_Overloaded (N)
5930 then
5931 -- Must be (fixed * fixed) operation, operand must have one
5932 -- compatible interpretation.
5934 Resolve (N, Any_Fixed);
5936 elsif Is_Fixed_Point_Type (B_Typ)
5937 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5938 and then Is_Overloaded (N)
5939 then
5940 -- C * F(X) in a fixed context, where C is a real literal or a
5941 -- fixed-point expression. F must have either a fixed type
5942 -- interpretation or an integer interpretation, but not both.
5944 Get_First_Interp (N, Index, It);
5945 while Present (It.Typ) loop
5946 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5947 if Analyzed (N) then
5948 Error_Msg_N ("ambiguous operand in fixed operation", N);
5949 else
5950 Resolve (N, Standard_Integer);
5951 end if;
5953 elsif Is_Fixed_Point_Type (It.Typ) then
5954 if Analyzed (N) then
5955 Error_Msg_N ("ambiguous operand in fixed operation", N);
5956 else
5957 Resolve (N, It.Typ);
5958 end if;
5959 end if;
5961 Get_Next_Interp (Index, It);
5962 end loop;
5964 -- Reanalyze the literal with the fixed type of the context. If
5965 -- context is Universal_Fixed, we are within a conversion, leave
5966 -- the literal as a universal real because there is no usable
5967 -- fixed type, and the target of the conversion plays no role in
5968 -- the resolution.
5970 declare
5971 Op2 : Node_Id;
5972 T2 : Entity_Id;
5974 begin
5975 if N = L then
5976 Op2 := R;
5977 else
5978 Op2 := L;
5979 end if;
5981 if B_Typ = Universal_Fixed
5982 and then Nkind (Op2) = N_Real_Literal
5983 then
5984 T2 := Universal_Real;
5985 else
5986 T2 := B_Typ;
5987 end if;
5989 Set_Analyzed (Op2, False);
5990 Resolve (Op2, T2);
5991 end;
5993 -- A universal real conditional expression can appear in a fixed-type
5994 -- context and must be resolved with that context to facilitate the
5995 -- code generation in the back end. However, If the context is
5996 -- Universal_fixed (i.e. as an operand of a multiplication/division
5997 -- involving a fixed-point operand) the conditional expression must
5998 -- resolve to a unique visible fixed_point type, normally Duration.
6000 elsif Nkind (N) in N_Case_Expression | N_If_Expression
6001 and then Etype (N) = Universal_Real
6002 and then Is_Fixed_Point_Type (B_Typ)
6003 then
6004 if B_Typ = Universal_Fixed then
6005 Resolve (N, Unique_Fixed_Point_Type (N));
6007 else
6008 Resolve (N, B_Typ);
6009 end if;
6011 else
6012 Resolve (N);
6013 end if;
6014 end Set_Mixed_Mode_Operand;
6016 ----------------------
6017 -- Set_Operand_Type --
6018 ----------------------
6020 procedure Set_Operand_Type (N : Node_Id) is
6021 begin
6022 if Is_Universal_Numeric_Type (Etype (N)) then
6023 Set_Etype (N, T);
6024 end if;
6025 end Set_Operand_Type;
6027 -- Start of processing for Resolve_Arithmetic_Op
6029 begin
6030 if Comes_From_Source (N)
6031 and then Ekind (Entity (N)) = E_Function
6032 and then Is_Imported (Entity (N))
6033 and then Is_Intrinsic_Subprogram (Entity (N))
6034 then
6035 Resolve_Intrinsic_Operator (N, Typ);
6036 return;
6038 -- Special-case for mixed-mode universal expressions or fixed point type
6039 -- operation: each argument is resolved separately. The same treatment
6040 -- is required if one of the operands of a fixed point operation is
6041 -- universal real, since in this case we don't do a conversion to a
6042 -- specific fixed-point type (instead the expander handles the case).
6044 -- Set the type of the node to its universal interpretation because
6045 -- legality checks on an exponentiation operand need the context.
6047 elsif Is_Universal_Numeric_Type (B_Typ)
6048 and then Present (Universal_Interpretation (L))
6049 and then Present (Universal_Interpretation (R))
6050 then
6051 Set_Etype (N, B_Typ);
6052 Resolve (L, Universal_Interpretation (L));
6053 Resolve (R, Universal_Interpretation (R));
6055 elsif (B_Typ = Universal_Real
6056 or else Etype (N) = Universal_Fixed
6057 or else (Etype (N) = Any_Fixed
6058 and then Is_Fixed_Point_Type (B_Typ))
6059 or else (Is_Fixed_Point_Type (B_Typ)
6060 and then (Is_Integer_Or_Universal (L)
6061 or else
6062 Is_Integer_Or_Universal (R))))
6063 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
6064 then
6065 if TL = Universal_Integer or else TR = Universal_Integer then
6066 Check_For_Visible_Operator (N, B_Typ);
6067 end if;
6069 -- If context is a fixed type and one operand is integer, the other
6070 -- is resolved with the type of the context.
6072 if Is_Fixed_Point_Type (B_Typ)
6073 and then (Base_Type (TL) = Base_Type (Standard_Integer)
6074 or else TL = Universal_Integer)
6075 then
6076 Resolve (R, B_Typ);
6077 Resolve (L, TL);
6079 elsif Is_Fixed_Point_Type (B_Typ)
6080 and then (Base_Type (TR) = Base_Type (Standard_Integer)
6081 or else TR = Universal_Integer)
6082 then
6083 Resolve (L, B_Typ);
6084 Resolve (R, TR);
6086 -- If both operands are universal and the context is a floating
6087 -- point type, the operands are resolved to the type of the context.
6089 elsif Is_Floating_Point_Type (B_Typ) then
6090 Resolve (L, B_Typ);
6091 Resolve (R, B_Typ);
6093 else
6094 Set_Mixed_Mode_Operand (L, TR);
6095 Set_Mixed_Mode_Operand (R, TL);
6096 end if;
6098 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6099 -- multiplying operators from being used when the expected type is
6100 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6101 -- some cases where the expected type is actually Any_Real;
6102 -- Expected_Type_Is_Any_Real takes care of that case.
6104 if Etype (N) = Universal_Fixed
6105 or else Etype (N) = Any_Fixed
6106 then
6107 if B_Typ = Universal_Fixed
6108 and then not Expected_Type_Is_Any_Real (N)
6109 and then Nkind (Parent (N)) not in
6110 N_Type_Conversion | N_Unchecked_Type_Conversion
6111 then
6112 Error_Msg_N ("type cannot be determined from context!", N);
6113 Error_Msg_N ("\explicit conversion to result type required", N);
6115 Set_Etype (L, Any_Type);
6116 Set_Etype (R, Any_Type);
6118 else
6119 if Ada_Version = Ada_83
6120 and then Etype (N) = Universal_Fixed
6121 and then Nkind (Parent (N)) not in
6122 N_Type_Conversion | N_Unchecked_Type_Conversion
6123 then
6124 Error_Msg_N
6125 ("(Ada 83) fixed-point operation needs explicit "
6126 & "conversion", N);
6127 end if;
6129 -- The expected type is "any real type" in contexts like
6131 -- type T is delta <universal_fixed-expression> ...
6133 -- in which case we need to set the type to Universal_Real
6134 -- so that static expression evaluation will work properly.
6136 if Expected_Type_Is_Any_Real (N) then
6137 Set_Etype (N, Universal_Real);
6138 else
6139 Set_Etype (N, B_Typ);
6140 end if;
6141 end if;
6143 elsif Is_Fixed_Point_Type (B_Typ)
6144 and then (Is_Integer_Or_Universal (L)
6145 or else Nkind (L) = N_Real_Literal
6146 or else Nkind (R) = N_Real_Literal
6147 or else Is_Integer_Or_Universal (R))
6148 then
6149 Set_Etype (N, B_Typ);
6151 elsif Etype (N) = Any_Fixed then
6153 -- If no previous errors, this is only possible if one operand is
6154 -- overloaded and the context is universal. Resolve as such.
6156 Set_Etype (N, B_Typ);
6157 end if;
6159 else
6160 if Is_Universal_Numeric_Type (TL)
6161 and then
6162 Is_Universal_Numeric_Type (TR)
6163 then
6164 Check_For_Visible_Operator (N, B_Typ);
6165 end if;
6167 -- If the context is Universal_Fixed and the operands are also
6168 -- universal fixed, this is an error, unless there is only one
6169 -- applicable fixed_point type (usually Duration).
6171 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6172 T := Unique_Fixed_Point_Type (N);
6174 if T = Any_Type then
6175 Set_Etype (N, T);
6176 return;
6177 else
6178 Resolve (L, T);
6179 Resolve (R, T);
6180 end if;
6182 else
6183 Resolve (L, B_Typ);
6184 Resolve (R, B_Typ);
6185 end if;
6187 -- If one of the arguments was resolved to a non-universal type.
6188 -- label the result of the operation itself with the same type.
6189 -- Do the same for the universal argument, if any.
6191 T := Intersect_Types (L, R);
6192 Set_Etype (N, Base_Type (T));
6193 Set_Operand_Type (L);
6194 Set_Operand_Type (R);
6195 end if;
6197 Generate_Operator_Reference (N, Typ);
6198 Analyze_Dimension (N);
6199 Eval_Arithmetic_Op (N);
6201 -- Set overflow and division checking bit
6203 if Nkind (N) in N_Op then
6204 if not Overflow_Checks_Suppressed (Etype (N)) then
6205 Enable_Overflow_Check (N);
6206 end if;
6208 -- Give warning if explicit division by zero
6210 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6211 and then not Division_Checks_Suppressed (Etype (N))
6212 then
6213 Rop := Right_Opnd (N);
6215 if Compile_Time_Known_Value (Rop)
6216 and then ((Is_Integer_Type (Etype (Rop))
6217 and then Expr_Value (Rop) = Uint_0)
6218 or else
6219 (Is_Real_Type (Etype (Rop))
6220 and then Expr_Value_R (Rop) = Ureal_0))
6221 then
6222 -- Specialize the warning message according to the operation.
6223 -- When SPARK_Mode is On, force a warning instead of an error
6224 -- in that case, as this likely corresponds to deactivated
6225 -- code. The following warnings are for the case
6227 case Nkind (N) is
6228 when N_Op_Divide =>
6230 -- For division, we have two cases, for float division
6231 -- of an unconstrained float type, on a machine where
6232 -- Machine_Overflows is false, we don't get an exception
6233 -- at run-time, but rather an infinity or Nan. The Nan
6234 -- case is pretty obscure, so just warn about infinities.
6236 if Is_Floating_Point_Type (Typ)
6237 and then not Is_Constrained (Typ)
6238 and then not Machine_Overflows_On_Target
6239 then
6240 Error_Msg_N
6241 ("float division by zero, may generate "
6242 & "'+'/'- infinity??", Right_Opnd (N));
6244 -- For all other cases, we get a Constraint_Error
6246 else
6247 Apply_Compile_Time_Constraint_Error
6248 (N, "division by zero??", CE_Divide_By_Zero,
6249 Loc => Sloc (Right_Opnd (N)),
6250 Warn => SPARK_Mode = On);
6251 end if;
6253 when N_Op_Rem =>
6254 Apply_Compile_Time_Constraint_Error
6255 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6256 Loc => Sloc (Right_Opnd (N)),
6257 Warn => SPARK_Mode = On);
6259 when N_Op_Mod =>
6260 Apply_Compile_Time_Constraint_Error
6261 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6262 Loc => Sloc (Right_Opnd (N)),
6263 Warn => SPARK_Mode = On);
6265 -- Division by zero can only happen with division, rem,
6266 -- and mod operations.
6268 when others =>
6269 raise Program_Error;
6270 end case;
6272 -- Otherwise just set the flag to check at run time
6274 else
6275 Activate_Division_Check (N);
6276 end if;
6277 end if;
6279 -- If Restriction No_Implicit_Conditionals is active, then it is
6280 -- violated if either operand can be negative for mod, or for rem
6281 -- if both operands can be negative.
6283 if Restriction_Check_Required (No_Implicit_Conditionals)
6284 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6285 then
6286 declare
6287 Lo : Uint;
6288 Hi : Uint;
6289 OK : Boolean;
6291 LNeg : Boolean;
6292 RNeg : Boolean;
6293 -- Set if corresponding operand might be negative
6295 begin
6296 Determine_Range
6297 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6298 LNeg := (not OK) or else Lo < 0;
6300 Determine_Range
6301 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6302 RNeg := (not OK) or else Lo < 0;
6304 -- Check if we will be generating conditionals. There are two
6305 -- cases where that can happen, first for REM, the only case
6306 -- is largest negative integer mod -1, where the division can
6307 -- overflow, but we still have to give the right result. The
6308 -- front end generates a test for this annoying case. Here we
6309 -- just test if both operands can be negative (that's what the
6310 -- expander does, so we match its logic here).
6312 -- The second case is mod where either operand can be negative.
6313 -- In this case, the back end has to generate additional tests.
6315 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6316 or else
6317 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6318 then
6319 Check_Restriction (No_Implicit_Conditionals, N);
6320 end if;
6321 end;
6322 end if;
6323 end if;
6325 Check_Unset_Reference (L);
6326 Check_Unset_Reference (R);
6327 end Resolve_Arithmetic_Op;
6329 ------------------
6330 -- Resolve_Call --
6331 ------------------
6333 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6334 Loc : constant Source_Ptr := Sloc (N);
6335 Subp : constant Node_Id := Name (N);
6336 Body_Id : Entity_Id;
6337 I : Interp_Index;
6338 It : Interp;
6339 Nam : Entity_Id;
6340 Nam_Decl : Node_Id;
6341 Nam_UA : Entity_Id;
6342 Norm_OK : Boolean;
6343 Rtype : Entity_Id;
6344 Scop : Entity_Id;
6346 begin
6347 -- Preserve relevant elaboration-related attributes of the context which
6348 -- are no longer available or very expensive to recompute once analysis,
6349 -- resolution, and expansion are over.
6351 Mark_Elaboration_Attributes
6352 (N_Id => N,
6353 Checks => True,
6354 Modes => True,
6355 Warnings => True);
6357 -- The context imposes a unique interpretation with type Typ on a
6358 -- procedure or function call. Find the entity of the subprogram that
6359 -- yields the expected type, and propagate the corresponding formal
6360 -- constraints on the actuals. The caller has established that an
6361 -- interpretation exists, and emitted an error if not unique.
6363 -- First deal with the case of a call to an access-to-subprogram,
6364 -- dereference made explicit in Analyze_Call.
6366 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6367 if not Is_Overloaded (Subp) then
6368 Nam := Etype (Subp);
6370 else
6371 -- Find the interpretation whose type (a subprogram type) has a
6372 -- return type that is compatible with the context. Analysis of
6373 -- the node has established that one exists.
6375 Nam := Empty;
6377 Get_First_Interp (Subp, I, It);
6378 while Present (It.Typ) loop
6379 if Covers (Typ, Etype (It.Typ)) then
6380 Nam := It.Typ;
6381 exit;
6382 end if;
6384 Get_Next_Interp (I, It);
6385 end loop;
6387 if No (Nam) then
6388 raise Program_Error;
6389 end if;
6390 end if;
6392 -- If the prefix is not an entity, then resolve it
6394 if not Is_Entity_Name (Subp) then
6395 Resolve (Subp, Nam);
6396 end if;
6398 -- For an indirect call, we always invalidate checks, since we do not
6399 -- know whether the subprogram is local or global. Yes we could do
6400 -- better here, e.g. by knowing that there are no local subprograms,
6401 -- but it does not seem worth the effort. Similarly, we kill all
6402 -- knowledge of current constant values.
6404 Kill_Current_Values;
6406 -- If this is a procedure call which is really an entry call, do
6407 -- the conversion of the procedure call to an entry call. Protected
6408 -- operations use the same circuitry because the name in the call
6409 -- can be an arbitrary expression with special resolution rules.
6411 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6412 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6413 then
6414 Resolve_Entry_Call (N, Typ);
6416 if Legacy_Elaboration_Checks then
6417 Check_Elab_Call (N);
6418 end if;
6420 -- Annotate the tree by creating a call marker in case the original
6421 -- call is transformed by expansion. The call marker is automatically
6422 -- saved for later examination by the ABE Processing phase.
6424 Build_Call_Marker (N);
6426 -- Kill checks and constant values, as above for indirect case
6427 -- Who knows what happens when another task is activated?
6429 Kill_Current_Values;
6430 return;
6432 -- Normal subprogram call with name established in Resolve
6434 elsif not Is_Type (Entity (Subp)) then
6435 Nam := Entity (Subp);
6436 Set_Entity_With_Checks (Subp, Nam);
6438 -- Otherwise we must have the case of an overloaded call
6440 else
6441 pragma Assert (Is_Overloaded (Subp));
6443 -- Initialize Nam to prevent warning (we know it will be assigned
6444 -- in the loop below, but the compiler does not know that).
6446 Nam := Empty;
6448 Get_First_Interp (Subp, I, It);
6449 while Present (It.Typ) loop
6450 if Covers (Typ, It.Typ) then
6451 Nam := It.Nam;
6452 Set_Entity_With_Checks (Subp, Nam);
6453 exit;
6454 end if;
6456 Get_Next_Interp (I, It);
6457 end loop;
6458 end if;
6460 -- Check that a call to Current_Task does not occur in an entry body
6462 if Is_RTE (Nam, RE_Current_Task) then
6463 declare
6464 P : Node_Id;
6466 begin
6467 P := N;
6468 loop
6469 P := Parent (P);
6471 -- Exclude calls that occur within the default of a formal
6472 -- parameter of the entry, since those are evaluated outside
6473 -- of the body.
6475 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6477 if Nkind (P) = N_Entry_Body
6478 or else (Nkind (P) = N_Subprogram_Body
6479 and then Is_Entry_Barrier_Function (P))
6480 then
6481 Rtype := Etype (N);
6482 Error_Msg_Warn := SPARK_Mode /= On;
6483 Error_Msg_NE
6484 ("& should not be used in entry body (RM C.7(17))<<",
6485 N, Nam);
6486 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6487 Rewrite (N,
6488 Make_Raise_Program_Error (Loc,
6489 Reason => PE_Current_Task_In_Entry_Body));
6490 Set_Etype (N, Rtype);
6491 return;
6492 end if;
6493 end loop;
6494 end;
6495 end if;
6497 -- Check that a procedure call does not occur in the context of the
6498 -- entry call statement of a conditional or timed entry call. Note that
6499 -- the case of a call to a subprogram renaming of an entry will also be
6500 -- rejected. The test for N not being an N_Entry_Call_Statement is
6501 -- defensive, covering the possibility that the processing of entry
6502 -- calls might reach this point due to later modifications of the code
6503 -- above.
6505 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6506 and then Nkind (N) /= N_Entry_Call_Statement
6507 and then Entry_Call_Statement (Parent (N)) = N
6508 then
6509 if Ada_Version < Ada_2005 then
6510 Error_Msg_N ("entry call required in select statement", N);
6512 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6513 -- for a procedure_or_entry_call, the procedure_name or
6514 -- procedure_prefix of the procedure_call_statement shall denote
6515 -- an entry renamed by a procedure, or (a view of) a primitive
6516 -- subprogram of a limited interface whose first parameter is
6517 -- a controlling parameter.
6519 elsif Nkind (N) = N_Procedure_Call_Statement
6520 and then not Is_Renamed_Entry (Nam)
6521 and then not Is_Controlling_Limited_Procedure (Nam)
6522 then
6523 Error_Msg_N
6524 ("entry call or dispatching primitive of interface required", N);
6525 end if;
6526 end if;
6528 -- Check that this is not a call to a protected procedure or entry from
6529 -- within a protected function.
6531 Check_Internal_Protected_Use (N, Nam);
6533 -- Freeze the subprogram name if not in a spec-expression. Note that
6534 -- we freeze procedure calls as well as function calls. Procedure calls
6535 -- are not frozen according to the rules (RM 13.14(14)) because it is
6536 -- impossible to have a procedure call to a non-frozen procedure in
6537 -- pure Ada, but in the code that we generate in the expander, this
6538 -- rule needs extending because we can generate procedure calls that
6539 -- need freezing.
6541 -- In Ada 2012, expression functions may be called within pre/post
6542 -- conditions of subsequent functions or expression functions. Such
6543 -- calls do not freeze when they appear within generated bodies,
6544 -- (including the body of another expression function) which would
6545 -- place the freeze node in the wrong scope. An expression function
6546 -- is frozen in the usual fashion, by the appearance of a real body,
6547 -- or at the end of a declarative part. However an implicit call to
6548 -- an expression function may appear when it is part of a default
6549 -- expression in a call to an initialization procedure, and must be
6550 -- frozen now, even if the body is inserted at a later point.
6551 -- Otherwise, the call freezes the expression if expander is active,
6552 -- for example as part of an object declaration.
6554 if Is_Entity_Name (Subp)
6555 and then not In_Spec_Expression
6556 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6557 and then
6558 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6559 or else Expander_Active)
6560 then
6561 if Is_Expression_Function (Entity (Subp)) then
6563 -- Force freeze of expression function in call
6565 Set_Comes_From_Source (Subp, True);
6566 Set_Must_Not_Freeze (Subp, False);
6567 end if;
6569 Freeze_Expression (Subp);
6570 end if;
6572 -- For a predefined operator, the type of the result is the type imposed
6573 -- by context, except for a predefined operation on universal fixed.
6574 -- Otherwise the type of the call is the type returned by the subprogram
6575 -- being called.
6577 if Is_Predefined_Op (Nam) then
6578 if Etype (N) /= Universal_Fixed then
6579 Set_Etype (N, Typ);
6580 end if;
6582 -- If the subprogram returns an array type, and the context requires the
6583 -- component type of that array type, the node is really an indexing of
6584 -- the parameterless call. Resolve as such. A pathological case occurs
6585 -- when the type of the component is an access to the array type. In
6586 -- this case the call is truly ambiguous. If the call is to an intrinsic
6587 -- subprogram, it can't be an indexed component. This check is necessary
6588 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6589 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6590 -- pointers to the same array), the compiler gets confused and does an
6591 -- infinite recursion.
6593 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6594 and then
6595 ((Is_Array_Type (Etype (Nam))
6596 and then Covers (Typ, Component_Type (Etype (Nam))))
6597 or else
6598 (Is_Access_Type (Etype (Nam))
6599 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6600 and then
6601 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6602 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6603 then
6604 declare
6605 Index_Node : Node_Id;
6606 New_Subp : Node_Id;
6607 Ret_Type : constant Entity_Id := Etype (Nam);
6609 begin
6610 -- If this is a parameterless call there is no ambiguity and the
6611 -- call has the type of the function.
6613 if No (First_Actual (N)) then
6614 Set_Etype (N, Etype (Nam));
6616 if Present (First_Formal (Nam)) then
6617 Resolve_Actuals (N, Nam);
6618 end if;
6620 -- Annotate the tree by creating a call marker in case the
6621 -- original call is transformed by expansion. The call marker
6622 -- is automatically saved for later examination by the ABE
6623 -- Processing phase.
6625 Build_Call_Marker (N);
6627 elsif Is_Access_Type (Ret_Type)
6629 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6630 then
6631 Error_Msg_N
6632 ("cannot disambiguate function call and indexing", N);
6633 else
6634 New_Subp := Relocate_Node (Subp);
6636 -- The called entity may be an explicit dereference, in which
6637 -- case there is no entity to set.
6639 if Nkind (New_Subp) /= N_Explicit_Dereference then
6640 Set_Entity (Subp, Nam);
6641 end if;
6643 if (Is_Array_Type (Ret_Type)
6644 and then Component_Type (Ret_Type) /= Any_Type)
6645 or else
6646 (Is_Access_Type (Ret_Type)
6647 and then
6648 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6649 then
6650 if Needs_No_Actuals (Nam) then
6652 -- Indexed call to a parameterless function
6654 Index_Node :=
6655 Make_Indexed_Component (Loc,
6656 Prefix =>
6657 Make_Function_Call (Loc, Name => New_Subp),
6658 Expressions => Parameter_Associations (N));
6659 else
6660 -- An Ada 2005 prefixed call to a primitive operation
6661 -- whose first parameter is the prefix. This prefix was
6662 -- prepended to the parameter list, which is actually a
6663 -- list of indexes. Remove the prefix in order to build
6664 -- the proper indexed component.
6666 Index_Node :=
6667 Make_Indexed_Component (Loc,
6668 Prefix =>
6669 Make_Function_Call (Loc,
6670 Name => New_Subp,
6671 Parameter_Associations =>
6672 New_List
6673 (Remove_Head (Parameter_Associations (N)))),
6674 Expressions => Parameter_Associations (N));
6675 end if;
6677 -- Preserve the parenthesis count of the node
6679 Set_Paren_Count (Index_Node, Paren_Count (N));
6681 -- Since we are correcting a node classification error made
6682 -- by the parser, we call Replace rather than Rewrite.
6684 Replace (N, Index_Node);
6686 Set_Etype (Prefix (N), Ret_Type);
6687 Set_Etype (N, Typ);
6689 if Legacy_Elaboration_Checks then
6690 Check_Elab_Call (Prefix (N));
6691 end if;
6693 -- Annotate the tree by creating a call marker in case
6694 -- the original call is transformed by expansion. The call
6695 -- marker is automatically saved for later examination by
6696 -- the ABE Processing phase.
6698 Build_Call_Marker (Prefix (N));
6700 Resolve_Indexed_Component (N, Typ);
6701 end if;
6702 end if;
6704 return;
6705 end;
6707 else
6708 -- If the called function is not declared in the main unit and it
6709 -- returns the limited view of type then use the available view (as
6710 -- is done in Try_Object_Operation) to prevent back-end confusion;
6711 -- for the function entity itself. The call must appear in a context
6712 -- where the nonlimited view is available. If the function entity is
6713 -- in the extended main unit then no action is needed, because the
6714 -- back end handles this case. In either case the type of the call
6715 -- is the nonlimited view.
6717 if From_Limited_With (Etype (Nam))
6718 and then Present (Available_View (Etype (Nam)))
6719 then
6720 Set_Etype (N, Available_View (Etype (Nam)));
6722 if not In_Extended_Main_Code_Unit (Nam) then
6723 Set_Etype (Nam, Available_View (Etype (Nam)));
6724 end if;
6726 else
6727 Set_Etype (N, Etype (Nam));
6728 end if;
6729 end if;
6731 -- In the case where the call is to an overloaded subprogram, Analyze
6732 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6733 -- such a case Normalize_Actuals needs to be called once more to order
6734 -- the actuals correctly. Otherwise the call will have the ordering
6735 -- given by the last overloaded subprogram whether this is the correct
6736 -- one being called or not.
6738 if Is_Overloaded (Subp) then
6739 Normalize_Actuals (N, Nam, False, Norm_OK);
6740 pragma Assert (Norm_OK);
6741 end if;
6743 -- In any case, call is fully resolved now. Reset Overload flag, to
6744 -- prevent subsequent overload resolution if node is analyzed again
6746 Set_Is_Overloaded (Subp, False);
6747 Set_Is_Overloaded (N, False);
6749 -- A Ghost entity must appear in a specific context
6751 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6752 Check_Ghost_Context (Nam, N);
6753 end if;
6755 -- If we are calling the current subprogram from immediately within its
6756 -- body, then that is the case where we can sometimes detect cases of
6757 -- infinite recursion statically. Do not try this in case restriction
6758 -- No_Recursion is in effect anyway, and do it only for source calls.
6760 if Comes_From_Source (N) then
6761 Scop := Current_Scope;
6763 -- Issue warning for possible infinite recursion in the absence
6764 -- of the No_Recursion restriction.
6766 if Same_Or_Aliased_Subprograms (Nam, Scop)
6767 and then not Restriction_Active (No_Recursion)
6768 and then not Is_Static_Function (Scop)
6769 and then Check_Infinite_Recursion (N)
6770 then
6771 -- Here we detected and flagged an infinite recursion, so we do
6772 -- not need to test the case below for further warnings. Also we
6773 -- are all done if we now have a raise SE node.
6775 if Nkind (N) = N_Raise_Storage_Error then
6776 return;
6777 end if;
6779 -- If call is to immediately containing subprogram, then check for
6780 -- the case of a possible run-time detectable infinite recursion.
6782 else
6783 Scope_Loop : while Scop /= Standard_Standard loop
6784 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6786 -- Ada 2022 (AI12-0075): Static functions are never allowed
6787 -- to make a recursive call, as specified by 6.8(5.4/5).
6789 if Is_Static_Function (Scop) then
6790 Error_Msg_N
6791 ("recursive call not allowed in static expression "
6792 & "function", N);
6794 Set_Error_Posted (Scop);
6796 exit Scope_Loop;
6797 end if;
6799 -- Although in general case, recursion is not statically
6800 -- checkable, the case of calling an immediately containing
6801 -- subprogram is easy to catch.
6803 if not Is_Ignored_Ghost_Entity (Nam) then
6804 Check_Restriction (No_Recursion, N);
6805 end if;
6807 -- If the recursive call is to a parameterless subprogram,
6808 -- then even if we can't statically detect infinite
6809 -- recursion, this is pretty suspicious, and we output a
6810 -- warning. Furthermore, we will try later to detect some
6811 -- cases here at run time by expanding checking code (see
6812 -- Detect_Infinite_Recursion in package Exp_Ch6).
6814 -- If the recursive call is within a handler, do not emit a
6815 -- warning, because this is a common idiom: loop until input
6816 -- is correct, catch illegal input in handler and restart.
6818 if No (First_Formal (Nam))
6819 and then Etype (Nam) = Standard_Void_Type
6820 and then not Error_Posted (N)
6821 and then Nkind (Parent (N)) /= N_Exception_Handler
6822 then
6823 -- For the case of a procedure call. We give the message
6824 -- only if the call is the first statement in a sequence
6825 -- of statements, or if all previous statements are
6826 -- simple assignments. This is simply a heuristic to
6827 -- decrease false positives, without losing too many good
6828 -- warnings. The idea is that these previous statements
6829 -- may affect global variables the procedure depends on.
6830 -- We also exclude raise statements, that may arise from
6831 -- constraint checks and are probably unrelated to the
6832 -- intended control flow.
6834 if Nkind (N) = N_Procedure_Call_Statement
6835 and then Is_List_Member (N)
6836 then
6837 declare
6838 P : Node_Id;
6839 begin
6840 P := Prev (N);
6841 while Present (P) loop
6842 if Nkind (P) not in N_Assignment_Statement
6843 | N_Raise_Constraint_Error
6844 then
6845 exit Scope_Loop;
6846 end if;
6848 Prev (P);
6849 end loop;
6850 end;
6851 end if;
6853 -- Do not give warning if we are in a conditional context
6855 declare
6856 K : constant Node_Kind := Nkind (Parent (N));
6857 begin
6858 if (K = N_Loop_Statement
6859 and then Present (Iteration_Scheme (Parent (N))))
6860 or else K = N_If_Statement
6861 or else K = N_Elsif_Part
6862 or else K = N_Case_Statement_Alternative
6863 then
6864 exit Scope_Loop;
6865 end if;
6866 end;
6868 -- Here warning is to be issued
6870 Set_Has_Recursive_Call (Nam);
6871 Error_Msg_Warn := SPARK_Mode /= On;
6872 Error_Msg_N ("possible infinite recursion<<!", N);
6873 Error_Msg_N ("\Storage_Error ]<<!", N);
6874 end if;
6876 exit Scope_Loop;
6877 end if;
6879 Scop := Scope (Scop);
6880 end loop Scope_Loop;
6881 end if;
6882 end if;
6884 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6886 Check_Obsolescent_2005_Entity (Nam, Subp);
6888 -- If subprogram name is a predefined operator, it was given in
6889 -- functional notation. Replace call node with operator node, so
6890 -- that actuals can be resolved appropriately.
6892 if Ekind (Nam) = E_Operator or else Is_Predefined_Op (Nam) then
6893 Make_Call_Into_Operator (N, Typ, Nam);
6894 return;
6896 elsif Present (Alias (Nam)) and then Is_Predefined_Op (Alias (Nam)) then
6897 Resolve_Actuals (N, Nam);
6898 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6899 return;
6900 end if;
6902 -- Create a transient scope if the resulting type requires it
6904 -- There are several notable exceptions:
6906 -- a) In init procs, the transient scope overhead is not needed, and is
6907 -- even incorrect when the call is a nested initialization call for a
6908 -- component whose expansion may generate adjust calls. However, if the
6909 -- call is some other procedure call within an initialization procedure
6910 -- (for example a call to Create_Task in the init_proc of the task
6911 -- run-time record) a transient scope must be created around this call.
6913 -- b) Enumeration literal pseudo-calls need no transient scope
6915 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6916 -- functions) do not use the secondary stack even though the return
6917 -- type may be unconstrained.
6919 -- d) Calls to a build-in-place function, since such functions may
6920 -- allocate their result directly in a target object, and cases where
6921 -- the result does get allocated in the secondary stack are checked for
6922 -- within the specialized Exp_Ch6 procedures for expanding those
6923 -- build-in-place calls.
6925 -- e) Calls to inlinable expression functions do not use the secondary
6926 -- stack (since the call will be replaced by its returned object).
6928 -- f) If the subprogram is marked Inline_Always, then even if it returns
6929 -- an unconstrained type the call does not require use of the secondary
6930 -- stack. However, inlining will only take place if the body to inline
6931 -- is already present. It may not be available if e.g. the subprogram is
6932 -- declared in a child instance.
6934 -- g) If the subprogram is a static expression function and the call is
6935 -- a static call (the actuals are all static expressions), then we never
6936 -- want to create a transient scope (this could occur in the case of a
6937 -- static string-returning call).
6939 if Is_Inlined (Nam)
6940 and then Has_Pragma_Inline (Nam)
6941 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6942 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6943 then
6944 null;
6946 elsif Ekind (Nam) = E_Enumeration_Literal
6947 or else Is_Build_In_Place_Function (Nam)
6948 or else Is_Intrinsic_Subprogram (Nam)
6949 or else Is_Inlinable_Expression_Function (Nam)
6950 or else Is_Static_Function_Call (N)
6951 then
6952 null;
6954 -- A return statement from an ignored Ghost function does not use the
6955 -- secondary stack (or any other one).
6957 elsif Expander_Active
6958 and then Ekind (Nam) in E_Function | E_Subprogram_Type
6959 and then Requires_Transient_Scope (Etype (Nam))
6960 and then not Is_Ignored_Ghost_Entity (Nam)
6961 then
6962 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
6964 -- If the call appears within the bounds of a loop, it will be
6965 -- rewritten and reanalyzed, nothing left to do here.
6967 if Nkind (N) /= N_Function_Call then
6968 return;
6969 end if;
6970 end if;
6972 -- A protected function cannot be called within the definition of the
6973 -- enclosing protected type, unless it is part of a pre/postcondition
6974 -- on another protected operation. This may appear in the entry wrapper
6975 -- created for an entry with preconditions.
6977 if Is_Protected_Type (Scope (Nam))
6978 and then In_Open_Scopes (Scope (Nam))
6979 and then not Has_Completion (Scope (Nam))
6980 and then not In_Spec_Expression
6981 and then not Is_Entry_Wrapper (Current_Scope)
6982 then
6983 Error_Msg_NE
6984 ("& cannot be called before end of protected definition", N, Nam);
6985 end if;
6987 -- Propagate interpretation to actuals, and add default expressions
6988 -- where needed.
6990 if Present (First_Formal (Nam)) then
6991 Resolve_Actuals (N, Nam);
6993 -- Overloaded literals are rewritten as function calls, for purpose of
6994 -- resolution. After resolution, we can replace the call with the
6995 -- literal itself.
6997 elsif Ekind (Nam) = E_Enumeration_Literal then
6998 Copy_Node (Subp, N);
6999 Resolve_Entity_Name (N, Typ);
7001 -- Avoid validation, since it is a static function call
7003 Generate_Reference (Nam, Subp);
7004 return;
7005 end if;
7007 -- If the subprogram is not global, then kill all saved values and
7008 -- checks. This is a bit conservative, since in many cases we could do
7009 -- better, but it is not worth the effort. Similarly, we kill constant
7010 -- values. However we do not need to do this for internal entities
7011 -- (unless they are inherited user-defined subprograms), since they
7012 -- are not in the business of molesting local values.
7014 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
7015 -- kill all checks and values for calls to global subprograms. This
7016 -- takes care of the case where an access to a local subprogram is
7017 -- taken, and could be passed directly or indirectly and then called
7018 -- from almost any context.
7020 -- Note: we do not do this step till after resolving the actuals. That
7021 -- way we still take advantage of the current value information while
7022 -- scanning the actuals.
7024 -- We suppress killing values if we are processing the nodes associated
7025 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
7026 -- type kills all the values as part of analyzing the code that
7027 -- initializes the dispatch tables.
7029 if Inside_Freezing_Actions = 0
7030 and then (not Is_Library_Level_Entity (Nam)
7031 or else Suppress_Value_Tracking_On_Call
7032 (Nearest_Dynamic_Scope (Current_Scope)))
7033 and then (Comes_From_Source (Nam)
7034 or else (Present (Alias (Nam))
7035 and then Comes_From_Source (Alias (Nam))))
7036 then
7037 Kill_Current_Values;
7038 end if;
7040 -- If we are warning about unread OUT parameters, this is the place to
7041 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
7042 -- after the above call to Kill_Current_Values (since that call clears
7043 -- the Last_Assignment field of all local variables).
7045 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
7046 and then Comes_From_Source (N)
7047 and then In_Extended_Main_Source_Unit (N)
7048 then
7049 declare
7050 F : Entity_Id;
7051 A : Node_Id;
7053 begin
7054 F := First_Formal (Nam);
7055 A := First_Actual (N);
7056 while Present (F) and then Present (A) loop
7057 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
7058 and then Warn_On_Modified_As_Out_Parameter (F)
7059 and then Is_Entity_Name (A)
7060 and then Present (Entity (A))
7061 and then Comes_From_Source (N)
7062 and then Safe_To_Capture_Value (N, Entity (A))
7063 then
7064 Set_Last_Assignment (Entity (A), A);
7065 end if;
7067 Next_Formal (F);
7068 Next_Actual (A);
7069 end loop;
7070 end;
7071 end if;
7073 -- If the subprogram is a primitive operation, check whether or not
7074 -- it is a correct dispatching call.
7076 if Is_Overloadable (Nam) and then Is_Dispatching_Operation (Nam) then
7077 Check_Dispatching_Call (N);
7079 -- If the subprogram is an abstract operation, then flag an error
7081 elsif Is_Overloadable (Nam) and then Is_Abstract_Subprogram (Nam) then
7082 Nondispatching_Call_To_Abstract_Operation (N, Nam);
7083 end if;
7085 -- If this is a dispatching call, generate the appropriate reference,
7086 -- for better source navigation in GNAT Studio.
7088 if Is_Overloadable (Nam) and then Present (Controlling_Argument (N)) then
7089 Generate_Reference (Nam, Subp, 'R');
7091 -- Normal case, not a dispatching call: generate a call reference
7093 else
7094 Generate_Reference (Nam, Subp, 's');
7095 end if;
7097 if Is_Intrinsic_Subprogram (Nam) then
7098 Check_Intrinsic_Call (N);
7099 end if;
7101 -- Check for violation of restriction No_Specific_Termination_Handlers
7102 -- and warn on a potentially blocking call to Abort_Task.
7104 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7105 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7106 or else
7107 Is_RTE (Nam, RE_Specific_Handler))
7108 then
7109 Check_Restriction (No_Specific_Termination_Handlers, N);
7111 elsif Is_RTE (Nam, RE_Abort_Task) then
7112 Check_Potentially_Blocking_Operation (N);
7113 end if;
7115 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7116 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7117 -- need to check the second argument to determine whether it is an
7118 -- absolute or relative timing event.
7120 if Restriction_Check_Required (No_Relative_Delay)
7121 and then Is_RTE (Nam, RE_Set_Handler)
7122 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7123 then
7124 Check_Restriction (No_Relative_Delay, N);
7125 end if;
7127 -- Issue an error for a call to an eliminated subprogram. This routine
7128 -- will not perform the check if the call appears within a default
7129 -- expression.
7131 Check_For_Eliminated_Subprogram (Subp, Nam);
7133 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7134 -- class-wide and the call dispatches on result in a context that does
7135 -- not provide a tag, the call raises Program_Error.
7137 if Nkind (N) = N_Function_Call
7138 and then In_Instance
7139 and then Is_Generic_Actual_Type (Typ)
7140 and then Is_Class_Wide_Type (Typ)
7141 and then Has_Controlling_Result (Nam)
7142 and then Nkind (Parent (N)) = N_Object_Declaration
7143 then
7144 -- Verify that none of the formals are controlling
7146 declare
7147 Call_OK : Boolean := False;
7148 F : Entity_Id;
7150 begin
7151 F := First_Formal (Nam);
7152 while Present (F) loop
7153 if Is_Controlling_Formal (F) then
7154 Call_OK := True;
7155 exit;
7156 end if;
7158 Next_Formal (F);
7159 end loop;
7161 if not Call_OK then
7162 Error_Msg_Warn := SPARK_Mode /= On;
7163 Error_Msg_N ("!cannot determine tag of result<<", N);
7164 Error_Msg_N ("\Program_Error [<<!", N);
7165 Insert_Action (N,
7166 Make_Raise_Program_Error (Sloc (N),
7167 Reason => PE_Explicit_Raise));
7168 end if;
7169 end;
7170 end if;
7172 -- Check for calling a function with OUT or IN OUT parameter when the
7173 -- calling context (us right now) is not Ada 2012, so does not allow
7174 -- OUT or IN OUT parameters in function calls. Functions declared in
7175 -- a predefined unit are OK, as they may be called indirectly from a
7176 -- user-declared instantiation.
7178 if Ada_Version < Ada_2012
7179 and then Ekind (Nam) = E_Function
7180 and then Has_Out_Or_In_Out_Parameter (Nam)
7181 and then not In_Predefined_Unit (Nam)
7182 then
7183 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7184 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7185 end if;
7187 -- Check the dimensions of the actuals in the call. For function calls,
7188 -- propagate the dimensions from the returned type to N.
7190 Analyze_Dimension_Call (N, Nam);
7192 -- All done, evaluate call and deal with elaboration issues
7194 Eval_Call (N);
7196 if Legacy_Elaboration_Checks then
7197 Check_Elab_Call (N);
7198 end if;
7200 -- Annotate the tree by creating a call marker in case the original call
7201 -- is transformed by expansion. The call marker is automatically saved
7202 -- for later examination by the ABE Processing phase.
7204 Build_Call_Marker (N);
7206 Mark_Use_Clauses (Subp);
7208 Warn_On_Overlapping_Actuals (Nam, N);
7210 -- Ada 2022 (AI12-0075): If the call is a static call to a static
7211 -- expression function, then we want to "inline" the call, replacing
7212 -- it with the folded static result. This is not done if the checking
7213 -- for a potentially static expression is enabled or if an error has
7214 -- been posted on the call (which may be due to the check for recursive
7215 -- calls, in which case we don't want to fall into infinite recursion
7216 -- when doing the inlining).
7218 if not Checking_Potentially_Static_Expression
7219 and then Is_Static_Function_Call (N)
7220 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7221 and then not Error_Posted (Ultimate_Alias (Nam))
7222 then
7223 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7225 -- In GNATprove mode, expansion is disabled, but we want to inline some
7226 -- subprograms to facilitate formal verification. Indirect calls through
7227 -- a subprogram type or within a generic cannot be inlined. Inlining is
7228 -- performed only for calls subject to SPARK_Mode on.
7230 elsif GNATprove_Mode
7231 and then SPARK_Mode = On
7232 and then Is_Overloadable (Nam)
7233 and then not Inside_A_Generic
7234 then
7235 Nam_UA := Ultimate_Alias (Nam);
7236 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7238 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7239 Body_Id := Corresponding_Body (Nam_Decl);
7241 -- Nothing to do if the subprogram is not eligible for inlining in
7242 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7244 if not Is_Inlined_Always (Nam_UA)
7245 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7246 or else Debug_Flag_M
7247 then
7248 null;
7250 -- Calls cannot be inlined inside assertions, as GNATprove treats
7251 -- assertions as logic expressions. Only issue a message when the
7252 -- body has been seen, otherwise this leads to spurious messages
7253 -- on expression functions.
7255 elsif In_Assertion_Expr /= 0 then
7256 Cannot_Inline
7257 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7258 Suppress_Info => No (Body_Id));
7260 -- Calls cannot be inlined inside default expressions
7262 elsif In_Default_Expr then
7263 Cannot_Inline
7264 ("cannot inline & (in default expression)?", N, Nam_UA);
7266 -- Calls cannot be inlined inside quantified expressions, which
7267 -- are left in expression form for GNATprove. Since these
7268 -- expressions are only preanalyzed, we need to detect the failure
7269 -- to inline outside of the case for Full_Analysis below.
7271 elsif In_Quantified_Expression (N) then
7272 Cannot_Inline
7273 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7275 -- Inlining should not be performed during preanalysis
7277 elsif Full_Analysis then
7279 -- Do not inline calls inside expression functions or functions
7280 -- generated by the front end for subtype predicates, as this
7281 -- would prevent interpreting them as logical formulas in
7282 -- GNATprove. Only issue a message when the body has been seen,
7283 -- otherwise this leads to spurious messages on callees that
7284 -- are themselves expression functions.
7286 if Present (Current_Subprogram)
7287 and then
7288 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7289 or else Is_Predicate_Function (Current_Subprogram)
7290 or else Is_Invariant_Procedure (Current_Subprogram)
7291 or else Is_DIC_Procedure (Current_Subprogram))
7292 then
7293 if Present (Body_Id)
7294 and then Present (Body_To_Inline (Nam_Decl))
7295 then
7296 if Is_Predicate_Function (Current_Subprogram) then
7297 Cannot_Inline
7298 ("cannot inline & (inside predicate)?",
7299 N, Nam_UA);
7301 elsif Is_Invariant_Procedure (Current_Subprogram) then
7302 Cannot_Inline
7303 ("cannot inline & (inside invariant)?",
7304 N, Nam_UA);
7306 elsif Is_DIC_Procedure (Current_Subprogram) then
7307 Cannot_Inline
7308 ("cannot inline & (inside Default_Initial_Condition)?",
7309 N, Nam_UA);
7311 else
7312 Cannot_Inline
7313 ("cannot inline & (inside expression function)?",
7314 N, Nam_UA);
7315 end if;
7316 end if;
7318 -- Cannot inline a call inside the definition of a record type,
7319 -- typically inside the constraints of the type. Calls in
7320 -- default expressions are also not inlined, but this is
7321 -- filtered out above when testing In_Default_Expr.
7323 elsif Is_Record_Type (Current_Scope) then
7324 Cannot_Inline
7325 ("cannot inline & (inside record type)?", N, Nam_UA);
7327 -- With the one-pass inlining technique, a call cannot be
7328 -- inlined if the corresponding body has not been seen yet.
7330 elsif No (Body_Id) then
7331 Cannot_Inline
7332 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7334 -- Nothing to do if there is no body to inline, indicating that
7335 -- the subprogram is not suitable for inlining in GNATprove
7336 -- mode.
7338 elsif No (Body_To_Inline (Nam_Decl)) then
7339 null;
7341 -- Calls cannot be inlined inside potentially unevaluated
7342 -- expressions, as this would create complex actions inside
7343 -- expressions, that are not handled by GNATprove.
7345 elsif Is_Potentially_Unevaluated (N) then
7346 Cannot_Inline
7347 ("cannot inline & (in potentially unevaluated context)?",
7348 N, Nam_UA);
7350 -- Calls cannot be inlined inside the conditions of while
7351 -- loops, as this would create complex actions inside
7352 -- the condition, that are not handled by GNATprove.
7354 elsif In_Statement_Condition_With_Actions (N) then
7355 Cannot_Inline
7356 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7358 -- Do not inline calls which would possibly lead to missing a
7359 -- type conversion check on an input parameter.
7361 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7362 Cannot_Inline
7363 ("cannot inline & (possible check on input parameters)?",
7364 N, Nam_UA);
7366 -- Otherwise, inline the call, issuing an info message when
7367 -- -gnatd_f is set.
7369 else
7370 if Debug_Flag_Underscore_F then
7371 Error_Msg_NE
7372 ("info: analyzing call to & in context?", N, Nam_UA);
7373 end if;
7375 Expand_Inlined_Call (N, Nam_UA, Nam);
7376 end if;
7377 end if;
7378 end if;
7379 end if;
7380 end Resolve_Call;
7382 -----------------------------
7383 -- Resolve_Case_Expression --
7384 -----------------------------
7386 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7387 Alt : Node_Id;
7388 Alt_Expr : Node_Id;
7389 Alt_Typ : Entity_Id;
7390 Is_Dyn : Boolean;
7392 begin
7393 Alt := First (Alternatives (N));
7394 while Present (Alt) loop
7395 Alt_Expr := Expression (Alt);
7397 if Error_Posted (Alt_Expr) then
7398 return;
7399 end if;
7401 Resolve (Alt_Expr, Typ);
7402 Check_Unset_Reference (Alt_Expr);
7403 Alt_Typ := Etype (Alt_Expr);
7405 -- When the expression is of a scalar subtype different from the
7406 -- result subtype, then insert a conversion to ensure the generation
7407 -- of a constraint check.
7409 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7410 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7411 Analyze_And_Resolve (Alt_Expr, Typ);
7412 end if;
7414 Next (Alt);
7415 end loop;
7417 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7418 -- dynamically tagged must be known statically.
7420 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7421 Alt := First (Alternatives (N));
7422 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7424 while Present (Alt) loop
7425 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7426 Error_Msg_N
7427 ("all or none of the dependent expressions can be "
7428 & "dynamically tagged", N);
7429 end if;
7431 Next (Alt);
7432 end loop;
7433 end if;
7435 Set_Etype (N, Typ);
7436 Eval_Case_Expression (N);
7437 Analyze_Dimension (N);
7438 end Resolve_Case_Expression;
7440 -------------------------------
7441 -- Resolve_Character_Literal --
7442 -------------------------------
7444 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7445 B_Typ : constant Entity_Id := Base_Type (Typ);
7446 C : Entity_Id;
7448 begin
7449 -- Verify that the character does belong to the type of the context
7451 Set_Etype (N, B_Typ);
7452 Eval_Character_Literal (N);
7454 -- Wide_Wide_Character literals must always be defined, since the set
7455 -- of wide wide character literals is complete, i.e. if a character
7456 -- literal is accepted by the parser, then it is OK for wide wide
7457 -- character (out of range character literals are rejected).
7459 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7460 return;
7462 -- Always accept character literal for type Any_Character, which
7463 -- occurs in error situations and in comparisons of literals, both
7464 -- of which should accept all literals.
7466 elsif B_Typ = Any_Character then
7467 return;
7469 -- For Standard.Character or a type derived from it, check that the
7470 -- literal is in range.
7472 elsif Root_Type (B_Typ) = Standard_Character then
7473 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7474 return;
7475 end if;
7477 -- For Standard.Wide_Character or a type derived from it, check that the
7478 -- literal is in range.
7480 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7481 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7482 return;
7483 end if;
7485 -- If the entity is already set, this has already been resolved in a
7486 -- generic context, or comes from expansion. Nothing else to do.
7488 elsif Present (Entity (N)) then
7489 return;
7491 -- Otherwise we have a user defined character type, and we can use the
7492 -- standard visibility mechanisms to locate the referenced entity.
7494 else
7495 C := Current_Entity (N);
7496 while Present (C) loop
7497 if Etype (C) = B_Typ then
7498 Set_Entity_With_Checks (N, C);
7499 Generate_Reference (C, N);
7500 return;
7501 end if;
7503 C := Homonym (C);
7504 end loop;
7505 end if;
7507 -- If we fall through, then the literal does not match any of the
7508 -- entries of the enumeration type. This isn't just a constraint error
7509 -- situation, it is an illegality (see RM 4.2).
7511 Error_Msg_NE
7512 ("character not defined for }", N, First_Subtype (B_Typ));
7513 end Resolve_Character_Literal;
7515 ---------------------------
7516 -- Resolve_Comparison_Op --
7517 ---------------------------
7519 -- The operands must have compatible types and the boolean context does not
7520 -- participate in the resolution. The first pass verifies that the operands
7521 -- are not ambiguous and sets their type correctly, or to Any_Type in case
7522 -- of ambiguity. If both operands are strings or aggregates, then they are
7523 -- ambiguous even if they carry a single (universal) type.
7525 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7526 L : constant Node_Id := Left_Opnd (N);
7527 R : constant Node_Id := Right_Opnd (N);
7529 T : Entity_Id := Find_Unique_Type (L, R);
7531 begin
7532 if T = Any_Fixed then
7533 T := Unique_Fixed_Point_Type (L);
7534 end if;
7536 Set_Etype (N, Base_Type (Typ));
7537 Generate_Reference (T, N, ' ');
7539 if T = Any_Type then
7540 -- Deal with explicit ambiguity of operands
7542 if Ekind (Entity (N)) = E_Operator
7543 and then (Is_Overloaded (L) or else Is_Overloaded (R))
7544 then
7545 Ambiguous_Operands (N);
7546 end if;
7548 return;
7549 end if;
7551 -- Deal with other error cases
7553 if T = Any_String or else
7554 T = Any_Composite or else
7555 T = Any_Character
7556 then
7557 if T = Any_Character then
7558 Ambiguous_Character (L);
7559 else
7560 Error_Msg_N ("ambiguous operands for comparison", N);
7561 end if;
7563 Set_Etype (N, Any_Type);
7564 return;
7565 end if;
7567 -- Resolve the operands if types OK
7569 Resolve (L, T);
7570 Resolve (R, T);
7571 Check_Unset_Reference (L);
7572 Check_Unset_Reference (R);
7573 Generate_Operator_Reference (N, T);
7574 Check_Low_Bound_Tested (N);
7576 -- Check comparison on unordered enumeration
7578 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7579 Error_Msg_Sloc := Sloc (Etype (L));
7580 Error_Msg_NE
7581 ("comparison on unordered enumeration type& declared#?.u?",
7582 N, Etype (L));
7583 end if;
7585 Analyze_Dimension (N);
7587 Eval_Relational_Op (N);
7588 end Resolve_Comparison_Op;
7590 --------------------------------
7591 -- Resolve_Declare_Expression --
7592 --------------------------------
7594 procedure Resolve_Declare_Expression
7595 (N : Node_Id;
7596 Typ : Entity_Id)
7598 Expr : constant Node_Id := Expression (N);
7600 Decl : Node_Id;
7601 Local : Entity_Id := Empty;
7603 function Replace_Local (N : Node_Id) return Traverse_Result;
7604 -- Use a tree traversal to replace each occurrence of the name of
7605 -- a local object declared in the construct, with the corresponding
7606 -- entity. This replaces the usual way to perform name capture by
7607 -- visibility, because it is not possible to place on the scope
7608 -- stack the fake scope created for the analysis of the local
7609 -- declarations; such a scope conflicts with the transient scopes
7610 -- that may be generated if the expression includes function calls
7611 -- requiring finalization.
7613 -------------------
7614 -- Replace_Local --
7615 -------------------
7617 function Replace_Local (N : Node_Id) return Traverse_Result is
7618 begin
7619 -- The identifier may be the prefix of a selected component,
7620 -- but not a selector name, because the local entities do not
7621 -- have a scope that can be named: a selected component whose
7622 -- selector is a homonym of a local entity must denote some
7623 -- global entity.
7625 if Nkind (N) = N_Identifier
7626 and then Chars (N) = Chars (Local)
7627 and then No (Entity (N))
7628 and then
7629 (Nkind (Parent (N)) /= N_Selected_Component
7630 or else N = Prefix (Parent (N)))
7631 then
7632 Set_Entity (N, Local);
7633 Set_Etype (N, Etype (Local));
7634 end if;
7636 return OK;
7637 end Replace_Local;
7639 procedure Replace_Local_Ref is new Traverse_Proc (Replace_Local);
7641 -- Start of processing for Resolve_Declare_Expression
7643 begin
7645 Decl := First (Actions (N));
7647 while Present (Decl) loop
7648 if Nkind (Decl) in
7649 N_Object_Declaration | N_Object_Renaming_Declaration
7650 and then Comes_From_Source (Defining_Identifier (Decl))
7651 then
7652 Local := Defining_Identifier (Decl);
7653 Replace_Local_Ref (Expr);
7655 -- Traverse the expression to replace references to local
7656 -- variables that occur within declarations of the
7657 -- declare_expression.
7659 declare
7660 D : Node_Id := Next (Decl);
7661 begin
7662 while Present (D) loop
7663 Replace_Local_Ref (D);
7664 Next (D);
7665 end loop;
7666 end;
7667 end if;
7669 Next (Decl);
7670 end loop;
7672 -- The end of the declarative list is a freeze point for the
7673 -- local declarations.
7675 if Present (Local) then
7676 Decl := Parent (Local);
7677 Freeze_All (First_Entity (Scope (Local)), Decl);
7678 end if;
7680 Resolve (Expr, Typ);
7681 Check_Unset_Reference (Expr);
7682 end Resolve_Declare_Expression;
7684 -----------------------------------------
7685 -- Resolve_Discrete_Subtype_Indication --
7686 -----------------------------------------
7688 procedure Resolve_Discrete_Subtype_Indication
7689 (N : Node_Id;
7690 Typ : Entity_Id)
7692 R : Node_Id;
7693 S : Entity_Id;
7695 begin
7696 Analyze (Subtype_Mark (N));
7697 S := Entity (Subtype_Mark (N));
7699 if Nkind (Constraint (N)) /= N_Range_Constraint then
7700 Error_Msg_N ("expect range constraint for discrete type", N);
7701 Set_Etype (N, Any_Type);
7703 else
7704 R := Range_Expression (Constraint (N));
7706 if R = Error then
7707 return;
7708 end if;
7710 Analyze (R);
7712 if Base_Type (S) /= Base_Type (Typ) then
7713 Error_Msg_NE
7714 ("expect subtype of }", N, First_Subtype (Typ));
7716 -- Rewrite the constraint as a range of Typ
7717 -- to allow compilation to proceed further.
7719 Set_Etype (N, Typ);
7720 Rewrite (Low_Bound (R),
7721 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7722 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7723 Attribute_Name => Name_First));
7724 Rewrite (High_Bound (R),
7725 Make_Attribute_Reference (Sloc (High_Bound (R)),
7726 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7727 Attribute_Name => Name_First));
7729 else
7730 Resolve (R, Typ);
7731 Set_Etype (N, Etype (R));
7733 -- Additionally, we must check that the bounds are compatible
7734 -- with the given subtype, which might be different from the
7735 -- type of the context.
7737 Apply_Range_Check (R, S);
7739 -- ??? If the above check statically detects a Constraint_Error
7740 -- it replaces the offending bound(s) of the range R with a
7741 -- Constraint_Error node. When the itype which uses these bounds
7742 -- is frozen the resulting call to Duplicate_Subexpr generates
7743 -- a new temporary for the bounds.
7745 -- Unfortunately there are other itypes that are also made depend
7746 -- on these bounds, so when Duplicate_Subexpr is called they get
7747 -- a forward reference to the newly created temporaries and Gigi
7748 -- aborts on such forward references. This is probably sign of a
7749 -- more fundamental problem somewhere else in either the order of
7750 -- itype freezing or the way certain itypes are constructed.
7752 -- To get around this problem we call Remove_Side_Effects right
7753 -- away if either bounds of R are a Constraint_Error.
7755 declare
7756 L : constant Node_Id := Low_Bound (R);
7757 H : constant Node_Id := High_Bound (R);
7759 begin
7760 if Nkind (L) = N_Raise_Constraint_Error then
7761 Remove_Side_Effects (L);
7762 end if;
7764 if Nkind (H) = N_Raise_Constraint_Error then
7765 Remove_Side_Effects (H);
7766 end if;
7767 end;
7769 Check_Unset_Reference (Low_Bound (R));
7770 Check_Unset_Reference (High_Bound (R));
7771 end if;
7772 end if;
7773 end Resolve_Discrete_Subtype_Indication;
7775 -------------------------
7776 -- Resolve_Entity_Name --
7777 -------------------------
7779 -- Used to resolve identifiers and expanded names
7781 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7782 function Is_Assignment_Or_Object_Expression
7783 (Context : Node_Id;
7784 Expr : Node_Id) return Boolean;
7785 -- Determine whether node Context denotes an assignment statement or an
7786 -- object declaration whose expression is node Expr.
7788 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7789 -- Determine whether Expr is part of an N_Attribute_Reference
7790 -- expression.
7792 ----------------------------------------
7793 -- Is_Assignment_Or_Object_Expression --
7794 ----------------------------------------
7796 function Is_Assignment_Or_Object_Expression
7797 (Context : Node_Id;
7798 Expr : Node_Id) return Boolean
7800 begin
7801 if Nkind (Context) in N_Assignment_Statement | N_Object_Declaration
7802 and then Expression (Context) = Expr
7803 then
7804 return True;
7806 -- Check whether a construct that yields a name is the expression of
7807 -- an assignment statement or an object declaration.
7809 elsif (Nkind (Context) in N_Attribute_Reference
7810 | N_Explicit_Dereference
7811 | N_Indexed_Component
7812 | N_Selected_Component
7813 | N_Slice
7814 and then Prefix (Context) = Expr)
7815 or else
7816 (Nkind (Context) in N_Type_Conversion
7817 | N_Unchecked_Type_Conversion
7818 and then Expression (Context) = Expr)
7819 then
7820 return
7821 Is_Assignment_Or_Object_Expression
7822 (Context => Parent (Context),
7823 Expr => Context);
7825 -- Otherwise the context is not an assignment statement or an object
7826 -- declaration.
7828 else
7829 return False;
7830 end if;
7831 end Is_Assignment_Or_Object_Expression;
7833 -----------------------------
7834 -- Is_Attribute_Expression --
7835 -----------------------------
7837 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
7838 N : Node_Id := Expr;
7839 begin
7840 while Present (N) loop
7841 if Nkind (N) = N_Attribute_Reference then
7842 return True;
7844 -- Prevent the search from going too far
7846 elsif Is_Body_Or_Package_Declaration (N) then
7847 return False;
7848 end if;
7850 N := Parent (N);
7851 end loop;
7853 return False;
7854 end Is_Attribute_Expression;
7856 -- Local variables
7858 E : constant Entity_Id := Entity (N);
7859 Par : Node_Id;
7861 -- Start of processing for Resolve_Entity_Name
7863 begin
7864 -- If garbage from errors, set to Any_Type and return
7866 if No (E) and then Total_Errors_Detected /= 0 then
7867 Set_Etype (N, Any_Type);
7868 return;
7869 end if;
7871 -- Replace named numbers by corresponding literals. Note that this is
7872 -- the one case where Resolve_Entity_Name must reset the Etype, since
7873 -- it is currently marked as universal.
7875 if Ekind (E) = E_Named_Integer then
7876 Set_Etype (N, Typ);
7877 Eval_Named_Integer (N);
7879 elsif Ekind (E) = E_Named_Real then
7880 Set_Etype (N, Typ);
7881 Eval_Named_Real (N);
7883 -- For enumeration literals, we need to make sure that a proper style
7884 -- check is done, since such literals are overloaded, and thus we did
7885 -- not do a style check during the first phase of analysis.
7887 elsif Ekind (E) = E_Enumeration_Literal then
7888 Set_Entity_With_Checks (N, E);
7889 Eval_Entity_Name (N);
7891 -- Case of (sub)type name appearing in a context where an expression
7892 -- is expected. This is legal if occurrence is a current instance.
7893 -- See RM 8.6 (17/3). It is also legal if the expression is
7894 -- part of a choice pattern for a case stmt/expr having a
7895 -- non-discrete selecting expression.
7897 elsif Is_Type (E) then
7898 if Is_Current_Instance (N) or else Is_Case_Choice_Pattern (N) then
7899 null;
7901 -- Any other use is an error
7903 else
7904 Error_Msg_N
7905 ("invalid use of subtype mark in expression or call", N);
7906 end if;
7908 -- Check discriminant use if entity is discriminant in current scope,
7909 -- i.e. discriminant of record or concurrent type currently being
7910 -- analyzed. Uses in corresponding body are unrestricted.
7912 elsif Ekind (E) = E_Discriminant
7913 and then Scope (E) = Current_Scope
7914 and then not Has_Completion (Current_Scope)
7915 then
7916 Check_Discriminant_Use (N);
7918 -- A parameterless generic function cannot appear in a context that
7919 -- requires resolution.
7921 elsif Ekind (E) = E_Generic_Function then
7922 Error_Msg_N ("illegal use of generic function", N);
7924 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7925 -- array types (i.e. bounds and length) are legal.
7927 elsif Ekind (E) = E_Out_Parameter
7928 and then (Is_Scalar_Type (Etype (E))
7929 or else not Is_Attribute_Expression (Parent (N)))
7931 and then (Nkind (Parent (N)) in N_Op
7932 or else Nkind (Parent (N)) = N_Explicit_Dereference
7933 or else Is_Assignment_Or_Object_Expression
7934 (Context => Parent (N),
7935 Expr => N))
7936 then
7937 if Ada_Version = Ada_83 then
7938 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7939 end if;
7941 -- In all other cases, just do the possible static evaluation
7943 else
7944 -- A deferred constant that appears in an expression must have a
7945 -- completion, unless it has been removed by in-place expansion of
7946 -- an aggregate. A constant that is a renaming does not need
7947 -- initialization.
7949 if Ekind (E) = E_Constant
7950 and then Comes_From_Source (E)
7951 and then No (Constant_Value (E))
7952 and then Is_Frozen (Etype (E))
7953 and then not In_Spec_Expression
7954 and then not Is_Imported (E)
7955 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7956 then
7957 if No_Initialization (Parent (E))
7958 or else (Present (Full_View (E))
7959 and then No_Initialization (Parent (Full_View (E))))
7960 then
7961 null;
7962 else
7963 Error_Msg_N
7964 ("deferred constant is frozen before completion", N);
7965 end if;
7966 end if;
7968 Eval_Entity_Name (N);
7969 end if;
7971 Par := Parent (N);
7973 -- When the entity appears in a parameter association, retrieve the
7974 -- related subprogram call.
7976 if Nkind (Par) = N_Parameter_Association then
7977 Par := Parent (Par);
7978 end if;
7980 if Comes_From_Source (N) then
7982 -- The following checks are only relevant when SPARK_Mode is on as
7983 -- they are not standard Ada legality rules.
7985 if SPARK_Mode = On then
7987 -- An effectively volatile object for reading must appear in
7988 -- non-interfering context (SPARK RM 7.1.3(10)).
7990 if Is_Object (E)
7991 and then Is_Effectively_Volatile_For_Reading (E)
7992 and then
7993 not Is_OK_Volatile_Context (Par, N, Check_Actuals => False)
7994 then
7995 SPARK_Msg_N
7996 ("volatile object cannot appear in this context "
7997 & "(SPARK RM 7.1.3(10))", N);
7998 end if;
8000 -- Check for possible elaboration issues with respect to reads of
8001 -- variables. The act of renaming the variable is not considered a
8002 -- read as it simply establishes an alias.
8004 if Legacy_Elaboration_Checks
8005 and then Ekind (E) = E_Variable
8006 and then Dynamic_Elaboration_Checks
8007 and then Nkind (Par) /= N_Object_Renaming_Declaration
8008 then
8009 Check_Elab_Call (N);
8010 end if;
8011 end if;
8013 -- The variable may eventually become a constituent of a single
8014 -- protected/task type. Record the reference now and verify its
8015 -- legality when analyzing the contract of the variable
8016 -- (SPARK RM 9.3).
8018 if Ekind (E) = E_Variable then
8019 Record_Possible_Part_Of_Reference (E, N);
8020 end if;
8022 -- A Ghost entity must appear in a specific context
8024 if Is_Ghost_Entity (E) then
8025 Check_Ghost_Context (E, N);
8026 end if;
8027 end if;
8029 -- We may be resolving an entity within expanded code, so a reference to
8030 -- an entity should be ignored when calculating effective use clauses to
8031 -- avoid inappropriate marking.
8033 if Comes_From_Source (N) then
8034 Mark_Use_Clauses (E);
8035 end if;
8036 end Resolve_Entity_Name;
8038 -------------------
8039 -- Resolve_Entry --
8040 -------------------
8042 procedure Resolve_Entry (Entry_Name : Node_Id) is
8043 Loc : constant Source_Ptr := Sloc (Entry_Name);
8044 Nam : Entity_Id;
8045 New_N : Node_Id;
8046 S : Entity_Id;
8047 Tsk : Entity_Id;
8048 E_Name : Node_Id;
8049 Index : Node_Id;
8051 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
8052 -- If the bounds of the entry family being called depend on task
8053 -- discriminants, build a new index subtype where a discriminant is
8054 -- replaced with the value of the discriminant of the target task.
8055 -- The target task is the prefix of the entry name in the call.
8057 -----------------------
8058 -- Actual_Index_Type --
8059 -----------------------
8061 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
8062 Typ : constant Entity_Id := Entry_Index_Type (E);
8063 Tsk : constant Entity_Id := Scope (E);
8064 Lo : constant Node_Id := Type_Low_Bound (Typ);
8065 Hi : constant Node_Id := Type_High_Bound (Typ);
8066 New_T : Entity_Id;
8068 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8069 -- If the bound is given by a discriminant, replace with a reference
8070 -- to the discriminant of the same name in the target task. If the
8071 -- entry name is the target of a requeue statement and the entry is
8072 -- in the current protected object, the bound to be used is the
8073 -- discriminal of the object (see Apply_Range_Check for details of
8074 -- the transformation).
8076 -----------------------------
8077 -- Actual_Discriminant_Ref --
8078 -----------------------------
8080 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8081 Typ : constant Entity_Id := Etype (Bound);
8082 Ref : Node_Id;
8084 begin
8085 Remove_Side_Effects (Bound);
8087 if not Is_Entity_Name (Bound)
8088 or else Ekind (Entity (Bound)) /= E_Discriminant
8089 then
8090 return Bound;
8092 elsif Is_Protected_Type (Tsk)
8093 and then In_Open_Scopes (Tsk)
8094 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
8095 then
8096 -- Note: here Bound denotes a discriminant of the corresponding
8097 -- record type tskV, whose discriminal is a formal of the
8098 -- init-proc tskVIP. What we want is the body discriminal,
8099 -- which is associated to the discriminant of the original
8100 -- concurrent type tsk.
8102 return New_Occurrence_Of
8103 (Find_Body_Discriminal (Entity (Bound)), Loc);
8105 else
8106 Ref :=
8107 Make_Selected_Component (Loc,
8108 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8109 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8110 Analyze (Ref);
8111 Resolve (Ref, Typ);
8112 return Ref;
8113 end if;
8114 end Actual_Discriminant_Ref;
8116 -- Start of processing for Actual_Index_Type
8118 begin
8119 if not Has_Discriminants (Tsk)
8120 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8121 then
8122 return Entry_Index_Type (E);
8124 else
8125 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8126 Set_Etype (New_T, Base_Type (Typ));
8127 Set_Size_Info (New_T, Typ);
8128 Set_RM_Size (New_T, RM_Size (Typ));
8129 Set_Scalar_Range (New_T,
8130 Make_Range (Sloc (Entry_Name),
8131 Low_Bound => Actual_Discriminant_Ref (Lo),
8132 High_Bound => Actual_Discriminant_Ref (Hi)));
8134 return New_T;
8135 end if;
8136 end Actual_Index_Type;
8138 -- Start of processing for Resolve_Entry
8140 begin
8141 -- Find name of entry being called, and resolve prefix of name with its
8142 -- own type. The prefix can be overloaded, and the name and signature of
8143 -- the entry must be taken into account.
8145 if Nkind (Entry_Name) = N_Indexed_Component then
8147 -- Case of dealing with entry family within the current tasks
8149 E_Name := Prefix (Entry_Name);
8151 else
8152 E_Name := Entry_Name;
8153 end if;
8155 if Is_Entity_Name (E_Name) then
8157 -- Entry call to an entry (or entry family) in the current task. This
8158 -- is legal even though the task will deadlock. Rewrite as call to
8159 -- current task.
8161 -- This can also be a call to an entry in an enclosing task. If this
8162 -- is a single task, we have to retrieve its name, because the scope
8163 -- of the entry is the task type, not the object. If the enclosing
8164 -- task is a task type, the identity of the task is given by its own
8165 -- self variable.
8167 -- Finally this can be a requeue on an entry of the same task or
8168 -- protected object.
8170 S := Scope (Entity (E_Name));
8172 for J in reverse 0 .. Scope_Stack.Last loop
8173 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8174 and then not Comes_From_Source (S)
8175 then
8176 -- S is an enclosing task or protected object. The concurrent
8177 -- declaration has been converted into a type declaration, and
8178 -- the object itself has an object declaration that follows
8179 -- the type in the same declarative part.
8181 Tsk := Next_Entity (S);
8182 while Etype (Tsk) /= S loop
8183 Next_Entity (Tsk);
8184 end loop;
8186 S := Tsk;
8187 exit;
8189 elsif S = Scope_Stack.Table (J).Entity then
8191 -- Call to current task. Will be transformed into call to Self
8193 exit;
8195 end if;
8196 end loop;
8198 New_N :=
8199 Make_Selected_Component (Loc,
8200 Prefix => New_Occurrence_Of (S, Loc),
8201 Selector_Name =>
8202 New_Occurrence_Of (Entity (E_Name), Loc));
8203 Rewrite (E_Name, New_N);
8204 Analyze (E_Name);
8206 elsif Nkind (Entry_Name) = N_Selected_Component
8207 and then Is_Overloaded (Prefix (Entry_Name))
8208 then
8209 -- Use the entry name (which must be unique at this point) to find
8210 -- the prefix that returns the corresponding task/protected type.
8212 declare
8213 Pref : constant Node_Id := Prefix (Entry_Name);
8214 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8215 I : Interp_Index;
8216 It : Interp;
8218 begin
8219 Get_First_Interp (Pref, I, It);
8220 while Present (It.Typ) loop
8221 if Scope (Ent) = It.Typ then
8222 Set_Etype (Pref, It.Typ);
8223 exit;
8224 end if;
8226 Get_Next_Interp (I, It);
8227 end loop;
8228 end;
8229 end if;
8231 if Nkind (Entry_Name) = N_Selected_Component then
8232 Resolve (Prefix (Entry_Name));
8233 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8235 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8236 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8237 Resolve (Prefix (Prefix (Entry_Name)));
8238 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8240 -- We do not resolve the prefix because an Entry_Family has no type,
8241 -- although it has the semantics of an array since it can be indexed.
8242 -- In order to perform the associated range check, we would need to
8243 -- build an array type on the fly and set it on the prefix, but this
8244 -- would be wasteful since only the index type matters. Therefore we
8245 -- attach this index type directly, so that Actual_Index_Expression
8246 -- can pick it up later in order to generate the range check.
8248 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8250 Index := First (Expressions (Entry_Name));
8251 Resolve (Index, Entry_Index_Type (Nam));
8253 -- Generate a reference for the index when it denotes an entity
8255 if Is_Entity_Name (Index) then
8256 Generate_Reference (Entity (Index), Nam);
8257 end if;
8259 -- Up to this point the expression could have been the actual in a
8260 -- simple entry call, and be given by a named association.
8262 if Nkind (Index) = N_Parameter_Association then
8263 Error_Msg_N ("expect expression for entry index", Index);
8264 else
8265 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8266 end if;
8267 end if;
8268 end Resolve_Entry;
8270 ------------------------
8271 -- Resolve_Entry_Call --
8272 ------------------------
8274 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8275 Entry_Name : constant Node_Id := Name (N);
8276 Loc : constant Source_Ptr := Sloc (Entry_Name);
8278 Nam : Entity_Id;
8279 Norm_OK : Boolean;
8280 Obj : Node_Id;
8281 Was_Over : Boolean;
8283 begin
8284 -- We kill all checks here, because it does not seem worth the effort to
8285 -- do anything better, an entry call is a big operation.
8287 Kill_All_Checks;
8289 -- Processing of the name is similar for entry calls and protected
8290 -- operation calls. Once the entity is determined, we can complete
8291 -- the resolution of the actuals.
8293 -- The selector may be overloaded, in the case of a protected object
8294 -- with overloaded functions. The type of the context is used for
8295 -- resolution.
8297 if Nkind (Entry_Name) = N_Selected_Component
8298 and then Is_Overloaded (Selector_Name (Entry_Name))
8299 and then Typ /= Standard_Void_Type
8300 then
8301 declare
8302 I : Interp_Index;
8303 It : Interp;
8305 begin
8306 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8307 while Present (It.Typ) loop
8308 if Covers (Typ, It.Typ) then
8309 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8310 Set_Etype (Entry_Name, It.Typ);
8312 Generate_Reference (It.Typ, N, ' ');
8313 end if;
8315 Get_Next_Interp (I, It);
8316 end loop;
8317 end;
8318 end if;
8320 Resolve_Entry (Entry_Name);
8322 if Nkind (Entry_Name) = N_Selected_Component then
8324 -- Simple entry or protected operation call
8326 Nam := Entity (Selector_Name (Entry_Name));
8327 Obj := Prefix (Entry_Name);
8329 if Is_Subprogram (Nam) then
8330 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8331 end if;
8333 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8335 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8337 -- Call to member of entry family
8339 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8340 Obj := Prefix (Prefix (Entry_Name));
8341 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8342 end if;
8344 -- We cannot in general check the maximum depth of protected entry calls
8345 -- at compile time. But we can tell that any protected entry call at all
8346 -- violates a specified nesting depth of zero.
8348 if Is_Protected_Type (Scope (Nam)) then
8349 Check_Restriction (Max_Entry_Queue_Length, N);
8350 end if;
8352 -- Use context type to disambiguate a protected function that can be
8353 -- called without actuals and that returns an array type, and where the
8354 -- argument list may be an indexing of the returned value.
8356 if Ekind (Nam) = E_Function
8357 and then Needs_No_Actuals (Nam)
8358 and then Present (Parameter_Associations (N))
8359 and then
8360 ((Is_Array_Type (Etype (Nam))
8361 and then Covers (Typ, Component_Type (Etype (Nam))))
8363 or else (Is_Access_Type (Etype (Nam))
8364 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8365 and then
8366 Covers
8367 (Typ,
8368 Component_Type (Designated_Type (Etype (Nam))))))
8369 then
8370 declare
8371 Index_Node : Node_Id;
8373 begin
8374 Index_Node :=
8375 Make_Indexed_Component (Loc,
8376 Prefix =>
8377 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8378 Expressions => Parameter_Associations (N));
8380 -- Since we are correcting a node classification error made by the
8381 -- parser, we call Replace rather than Rewrite.
8383 Replace (N, Index_Node);
8384 Set_Etype (Prefix (N), Etype (Nam));
8385 Set_Etype (N, Typ);
8386 Resolve_Indexed_Component (N, Typ);
8387 return;
8388 end;
8389 end if;
8391 if Is_Entry (Nam)
8392 and then Present (Contract_Wrapper (Nam))
8393 and then Current_Scope /= Contract_Wrapper (Nam)
8394 then
8395 -- Note the entity being called before rewriting the call, so that
8396 -- it appears used at this point.
8398 Generate_Reference (Nam, Entry_Name, 'r');
8400 -- Rewrite as call to the precondition wrapper, adding the task
8401 -- object to the list of actuals. If the call is to a member of an
8402 -- entry family, include the index as well.
8404 declare
8405 New_Call : Node_Id;
8406 New_Actuals : List_Id;
8408 begin
8409 New_Actuals := New_List (Obj);
8411 if Nkind (Entry_Name) = N_Indexed_Component then
8412 Append_To (New_Actuals,
8413 New_Copy_Tree (First (Expressions (Entry_Name))));
8414 end if;
8416 Append_List (Parameter_Associations (N), New_Actuals);
8417 New_Call :=
8418 Make_Procedure_Call_Statement (Loc,
8419 Name =>
8420 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8421 Parameter_Associations => New_Actuals);
8422 Rewrite (N, New_Call);
8424 -- Preanalyze and resolve new call. Current procedure is called
8425 -- from Resolve_Call, after which expansion will take place.
8427 Preanalyze_And_Resolve (N);
8428 return;
8429 end;
8430 end if;
8432 -- The operation name may have been overloaded. Order the actuals
8433 -- according to the formals of the resolved entity, and set the return
8434 -- type to that of the operation.
8436 if Was_Over then
8437 Normalize_Actuals (N, Nam, False, Norm_OK);
8438 pragma Assert (Norm_OK);
8439 Set_Etype (N, Etype (Nam));
8441 -- Reset the Is_Overloaded flag, since resolution is now completed
8443 -- Simple entry call
8445 if Nkind (Entry_Name) = N_Selected_Component then
8446 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8448 -- Call to a member of an entry family
8450 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8451 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8452 end if;
8453 end if;
8455 Resolve_Actuals (N, Nam);
8456 Check_Internal_Protected_Use (N, Nam);
8458 -- Create a call reference to the entry
8460 Generate_Reference (Nam, Entry_Name, 's');
8462 if Is_Entry (Nam) then
8463 Check_Potentially_Blocking_Operation (N);
8464 end if;
8466 -- Verify that a procedure call cannot masquerade as an entry
8467 -- call where an entry call is expected.
8469 if Ekind (Nam) = E_Procedure then
8470 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8471 and then N = Entry_Call_Statement (Parent (N))
8472 then
8473 Error_Msg_N ("entry call required in select statement", N);
8475 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8476 and then N = Triggering_Statement (Parent (N))
8477 then
8478 Error_Msg_N ("triggering statement cannot be procedure call", N);
8480 elsif Ekind (Scope (Nam)) = E_Task_Type
8481 and then not In_Open_Scopes (Scope (Nam))
8482 then
8483 Error_Msg_N ("task has no entry with this name", Entry_Name);
8484 end if;
8485 end if;
8487 -- After resolution, entry calls and protected procedure calls are
8488 -- changed into entry calls, for expansion. The structure of the node
8489 -- does not change, so it can safely be done in place. Protected
8490 -- function calls must keep their structure because they are
8491 -- subexpressions.
8493 if Ekind (Nam) /= E_Function then
8495 -- A protected operation that is not a function may modify the
8496 -- corresponding object, and cannot apply to a constant. If this
8497 -- is an internal call, the prefix is the type itself.
8499 if Is_Protected_Type (Scope (Nam))
8500 and then not Is_Variable (Obj)
8501 and then (not Is_Entity_Name (Obj)
8502 or else not Is_Type (Entity (Obj)))
8503 then
8504 Error_Msg_N
8505 ("prefix of protected procedure or entry call must be variable",
8506 Entry_Name);
8507 end if;
8509 declare
8510 Entry_Call : Node_Id;
8512 begin
8513 Entry_Call :=
8514 Make_Entry_Call_Statement (Loc,
8515 Name => Entry_Name,
8516 Parameter_Associations => Parameter_Associations (N));
8518 -- Inherit relevant attributes from the original call
8520 Set_First_Named_Actual
8521 (Entry_Call, First_Named_Actual (N));
8523 Set_Is_Elaboration_Checks_OK_Node
8524 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8526 Set_Is_Elaboration_Warnings_OK_Node
8527 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8529 Set_Is_SPARK_Mode_On_Node
8530 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8532 Rewrite (N, Entry_Call);
8533 Set_Analyzed (N, True);
8534 end;
8536 -- Protected functions can return on the secondary stack, in which case
8537 -- we must trigger the transient scope mechanism.
8539 elsif Expander_Active
8540 and then Requires_Transient_Scope (Etype (Nam))
8541 then
8542 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
8543 end if;
8545 -- Now we know that this is not a call to a function that returns an
8546 -- array type; moreover, we know the name of the called entry. Detect
8547 -- overlapping actuals, just like for a subprogram call.
8549 Warn_On_Overlapping_Actuals (Nam, N);
8550 end Resolve_Entry_Call;
8552 -------------------------
8553 -- Resolve_Equality_Op --
8554 -------------------------
8556 -- The operands must have compatible types and the boolean context does not
8557 -- participate in the resolution. The first pass verifies that the operands
8558 -- are not ambiguous and sets their type correctly, or to Any_Type in case
8559 -- of ambiguity. If both operands are strings, aggregates, allocators, or
8560 -- null, they are ambiguous even if they carry a single (universal) type.
8562 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8563 L : constant Node_Id := Left_Opnd (N);
8564 R : constant Node_Id := Right_Opnd (N);
8566 T : Entity_Id := Find_Unique_Type (L, R);
8568 procedure Check_Access_Attribute (N : Node_Id);
8569 -- For any object, '[Unchecked_]Access of such object can never be
8570 -- passed as an operand to the Universal_Access equality operators.
8571 -- This is so because the expected type for Obj'Access in a call to
8572 -- these operators, whose formals are of type Universal_Access, is
8573 -- Universal_Access, and Universal_Access does not have a designated
8574 -- type. For more details, see RM 3.10.2(2/2) and 6.4.1(3).
8576 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id);
8577 -- Check RM 4.5.2(9.6/2) on the given designated object types
8579 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id);
8580 -- Check RM 4.5.2(9.7/2) on the given designated subprogram types
8582 procedure Check_If_Expression (Cond : Node_Id);
8583 -- The resolution rule for if expressions requires that each such must
8584 -- have a unique type. This means that if several dependent expressions
8585 -- are of a non-null anonymous access type, and the context does not
8586 -- impose an expected type (as can be the case in an equality operation)
8587 -- the expression must be rejected.
8589 procedure Explain_Redundancy (N : Node_Id);
8590 -- Attempt to explain the nature of a redundant comparison with True. If
8591 -- the expression N is too complex, this routine issues a general error
8592 -- message.
8594 function Find_Unique_Access_Type return Entity_Id;
8595 -- In the case of allocators and access attributes, the context must
8596 -- provide an indication of the specific access type to be used. If
8597 -- one operand is of such a "generic" access type, check whether there
8598 -- is a specific visible access type that has the same designated type.
8599 -- This is semantically dubious, and of no interest to any real code,
8600 -- but c48008a makes it all worthwhile.
8602 function Suspicious_Prio_For_Equality return Boolean;
8603 -- Returns True iff the parent node is a and/or/xor operation that
8604 -- could be the cause of confused priorities. Note that if the not is
8605 -- in parens, then False is returned.
8607 ----------------------------
8608 -- Check_Access_Attribute --
8609 ----------------------------
8611 procedure Check_Access_Attribute (N : Node_Id) is
8612 begin
8613 if Nkind (N) = N_Attribute_Reference
8614 and then Attribute_Name (N) in Name_Access | Name_Unchecked_Access
8615 then
8616 Error_Msg_N
8617 ("access attribute cannot be used as actual for "
8618 & "universal_access equality", N);
8619 end if;
8620 end Check_Access_Attribute;
8622 -----------------------------------
8623 -- Check_Designated_Object_Types --
8624 -----------------------------------
8626 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id) is
8627 begin
8628 if (Is_Elementary_Type (T1) or else Is_Array_Type (T1))
8629 and then (Base_Type (T1) /= Base_Type (T2)
8630 or else not Subtypes_Statically_Match (T1, T2))
8631 then
8632 Error_Msg_N
8633 ("designated subtypes for universal_access equality "
8634 & "do not statically match (RM 4.5.2(9.6/2)", N);
8635 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8636 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8637 end if;
8638 end Check_Designated_Object_Types;
8640 ---------------------------------------
8641 -- Check_Designated_Subprogram_Types --
8642 ---------------------------------------
8644 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id) is
8645 begin
8646 if not Subtype_Conformant (T1, T2) then
8647 Error_Msg_N
8648 ("designated subtypes for universal_access equality "
8649 & "not subtype conformant (RM 4.5.2(9.7/2)", N);
8650 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8651 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8652 end if;
8653 end Check_Designated_Subprogram_Types;
8655 -------------------------
8656 -- Check_If_Expression --
8657 -------------------------
8659 procedure Check_If_Expression (Cond : Node_Id) is
8660 Then_Expr : Node_Id;
8661 Else_Expr : Node_Id;
8663 begin
8664 if Nkind (Cond) = N_If_Expression then
8665 Then_Expr := Next (First (Expressions (Cond)));
8666 Else_Expr := Next (Then_Expr);
8668 if Nkind (Then_Expr) /= N_Null
8669 and then Nkind (Else_Expr) /= N_Null
8670 then
8671 Error_Msg_N ("cannot determine type of if expression", Cond);
8672 end if;
8673 end if;
8674 end Check_If_Expression;
8676 ------------------------
8677 -- Explain_Redundancy --
8678 ------------------------
8680 procedure Explain_Redundancy (N : Node_Id) is
8681 Error : Name_Id;
8682 Val : Node_Id;
8683 Val_Id : Entity_Id;
8685 begin
8686 Val := N;
8688 -- Strip the operand down to an entity
8690 loop
8691 if Nkind (Val) = N_Selected_Component then
8692 Val := Selector_Name (Val);
8693 else
8694 exit;
8695 end if;
8696 end loop;
8698 -- The construct denotes an entity
8700 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8701 Val_Id := Entity (Val);
8703 -- Do not generate an error message when the comparison is done
8704 -- against the enumeration literal Standard.True.
8706 if Ekind (Val_Id) /= E_Enumeration_Literal then
8708 -- Build a customized error message
8710 Name_Len := 0;
8711 Add_Str_To_Name_Buffer ("?r?");
8713 if Ekind (Val_Id) = E_Component then
8714 Add_Str_To_Name_Buffer ("component ");
8716 elsif Ekind (Val_Id) = E_Constant then
8717 Add_Str_To_Name_Buffer ("constant ");
8719 elsif Ekind (Val_Id) = E_Discriminant then
8720 Add_Str_To_Name_Buffer ("discriminant ");
8722 elsif Is_Formal (Val_Id) then
8723 Add_Str_To_Name_Buffer ("parameter ");
8725 elsif Ekind (Val_Id) = E_Variable then
8726 Add_Str_To_Name_Buffer ("variable ");
8727 end if;
8729 Add_Str_To_Name_Buffer ("& is always True!");
8730 Error := Name_Find;
8732 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8733 end if;
8735 -- The construct is too complex to disect, issue a general message
8737 else
8738 Error_Msg_N ("?r?expression is always True!", Val);
8739 end if;
8740 end Explain_Redundancy;
8742 -----------------------------
8743 -- Find_Unique_Access_Type --
8744 -----------------------------
8746 function Find_Unique_Access_Type return Entity_Id is
8747 Acc : Entity_Id;
8748 E : Entity_Id;
8749 S : Entity_Id;
8751 begin
8752 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8753 then
8754 Acc := Designated_Type (Etype (R));
8756 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8757 then
8758 Acc := Designated_Type (Etype (L));
8759 else
8760 return Empty;
8761 end if;
8763 S := Current_Scope;
8764 while S /= Standard_Standard loop
8765 E := First_Entity (S);
8766 while Present (E) loop
8767 if Is_Type (E)
8768 and then Is_Access_Type (E)
8769 and then Ekind (E) /= E_Allocator_Type
8770 and then Designated_Type (E) = Base_Type (Acc)
8771 then
8772 return E;
8773 end if;
8775 Next_Entity (E);
8776 end loop;
8778 S := Scope (S);
8779 end loop;
8781 return Empty;
8782 end Find_Unique_Access_Type;
8784 ----------------------------------
8785 -- Suspicious_Prio_For_Equality --
8786 ----------------------------------
8788 function Suspicious_Prio_For_Equality return Boolean is
8789 Par : constant Node_Id := Parent (N);
8791 begin
8792 -- Check if parent node is one of and/or/xor, not parenthesized
8793 -- explicitly, and its own parent is not of this kind. Otherwise,
8794 -- it's a case of chained Boolean conditions which is likely well
8795 -- parenthesized.
8797 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8798 and then Paren_Count (N) = 0
8799 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8800 then
8801 declare
8802 Compar : Node_Id :=
8803 (if Left_Opnd (Par) = N then
8804 Right_Opnd (Par)
8805 else
8806 Left_Opnd (Par));
8807 begin
8808 -- Compar may have been rewritten, for example from (a /= b)
8809 -- into not (a = b). Use the Original_Node instead.
8811 Compar := Original_Node (Compar);
8813 -- If the other argument of the and/or/xor is also a
8814 -- comparison, or another and/or/xor then most likely
8815 -- the priorities are correctly set.
8817 return Nkind (Compar) not in N_Op_Boolean;
8818 end;
8820 else
8821 return False;
8822 end if;
8823 end Suspicious_Prio_For_Equality;
8825 -- Start of processing for Resolve_Equality_Op
8827 begin
8828 if T = Any_Fixed then
8829 T := Unique_Fixed_Point_Type (L);
8830 end if;
8832 Set_Etype (N, Base_Type (Typ));
8833 Generate_Reference (T, N, ' ');
8835 if T = Any_Type then
8836 -- Deal with explicit ambiguity of operands
8838 if Ekind (Entity (N)) = E_Operator
8839 and then (Is_Overloaded (L) or else Is_Overloaded (R))
8840 then
8841 Ambiguous_Operands (N);
8842 end if;
8844 else
8845 -- Deal with other error cases
8847 if T = Any_String or else
8848 T = Any_Composite or else
8849 T = Any_Character
8850 then
8851 if T = Any_Character then
8852 Ambiguous_Character (L);
8853 else
8854 Error_Msg_N ("ambiguous operands for equality", N);
8855 end if;
8857 Set_Etype (N, Any_Type);
8858 return;
8860 elsif T = Universal_Access
8861 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
8862 then
8863 T := Find_Unique_Access_Type;
8865 if No (T) then
8866 Error_Msg_N ("ambiguous operands for equality", N);
8867 Set_Etype (N, Any_Type);
8868 return;
8869 end if;
8871 -- If expressions must have a single type, and if the context does
8872 -- not impose one the dependent expressions cannot be anonymous
8873 -- access types.
8875 -- Why no similar processing for case expressions???
8877 elsif Ada_Version >= Ada_2012
8878 and then Is_Anonymous_Access_Type (Etype (L))
8879 and then Is_Anonymous_Access_Type (Etype (R))
8880 then
8881 Check_If_Expression (L);
8882 Check_If_Expression (R);
8883 end if;
8885 -- RM 4.5.2(9.5/2): At least one of the operands of the equality
8886 -- operators for universal_access shall be of type universal_access,
8887 -- or both shall be of access-to-object types, or both shall be of
8888 -- access-to-subprogram types (RM 4.5.2(9.5/2)).
8890 if Is_Anonymous_Access_Type (T)
8891 and then Etype (L) /= Universal_Access
8892 and then Etype (R) /= Universal_Access
8893 then
8894 -- RM 4.5.2(9.6/2): When both are of access-to-object types, the
8895 -- designated types shall be the same or one shall cover the other
8896 -- and if the designated types are elementary or array types, then
8897 -- the designated subtypes shall statically match.
8899 if Is_Access_Object_Type (Etype (L))
8900 and then Is_Access_Object_Type (Etype (R))
8901 then
8902 Check_Designated_Object_Types
8903 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
8905 -- RM 4.5.2(9.7/2): When both are of access-to-subprogram types,
8906 -- the designated profiles shall be subtype conformant.
8908 elsif Is_Access_Subprogram_Type (Etype (L))
8909 and then Is_Access_Subprogram_Type (Etype (R))
8910 then
8911 Check_Designated_Subprogram_Types
8912 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
8913 end if;
8914 end if;
8916 -- Check another case of equality operators for universal_access
8918 if Is_Anonymous_Access_Type (T) and then Comes_From_Source (N) then
8919 Check_Access_Attribute (L);
8920 Check_Access_Attribute (R);
8921 end if;
8923 Resolve (L, T);
8924 Resolve (R, T);
8926 -- AI12-0413: user-defined primitive equality of an untagged record
8927 -- type hides the predefined equality operator, including within a
8928 -- generic, and if it is declared abstract, results in an illegal
8929 -- instance if the operator is used in the spec, or in the raising
8930 -- of Program_Error if used in the body of an instance.
8932 if Nkind (N) = N_Op_Eq
8933 and then In_Instance
8934 and then Ada_Version >= Ada_2012
8935 then
8936 declare
8937 U : constant Entity_Id := Underlying_Type (T);
8939 Eq : Entity_Id;
8941 begin
8942 if Present (U)
8943 and then Is_Record_Type (U)
8944 and then not Is_Tagged_Type (U)
8945 then
8946 Eq := Get_User_Defined_Equality (T);
8948 if Present (Eq) then
8949 if Is_Abstract_Subprogram (Eq) then
8950 Nondispatching_Call_To_Abstract_Operation (N, Eq);
8951 else
8952 Rewrite_Operator_As_Call (N, Eq);
8953 end if;
8955 return;
8956 end if;
8957 end if;
8958 end;
8959 end if;
8961 -- If the unique type is a class-wide type then it will be expanded
8962 -- into a dispatching call to the predefined primitive. Therefore we
8963 -- check here for potential violation of such restriction.
8965 if Is_Class_Wide_Type (T) then
8966 Check_Restriction (No_Dispatching_Calls, N);
8967 end if;
8969 -- Only warn for redundant equality comparison to True for objects
8970 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
8971 -- other expressions, it may be a matter of preference to write
8972 -- "Expr = True" or "Expr".
8974 if Warn_On_Redundant_Constructs
8975 and then Comes_From_Source (N)
8976 and then Comes_From_Source (R)
8977 and then Is_Entity_Name (R)
8978 and then Entity (R) = Standard_True
8979 and then
8980 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
8981 or else
8982 Nkind (L) in N_Op)
8983 then
8984 Error_Msg_N -- CODEFIX
8985 ("?r?comparison with True is redundant!", N);
8986 Explain_Redundancy (Original_Node (R));
8987 end if;
8989 -- Warn on a (in)equality between boolean values which is not
8990 -- parenthesized when the parent expression is one of and/or/xor, as
8991 -- this is interpreted as (a = b) op c where most likely a = (b op c)
8992 -- was intended. Do not generate a warning in generic instances, as
8993 -- the problematic expression may be implicitly parenthesized in
8994 -- the generic itself if one of the operators is a generic formal.
8995 -- Also do not generate a warning for generated equality, for
8996 -- example from rewritting a membership test.
8998 if Warn_On_Questionable_Missing_Parens
8999 and then not In_Instance
9000 and then Comes_From_Source (N)
9001 and then Is_Boolean_Type (T)
9002 and then Suspicious_Prio_For_Equality
9003 then
9004 Error_Msg_N ("?q?equality should be parenthesized here!", N);
9005 end if;
9007 Check_Unset_Reference (L);
9008 Check_Unset_Reference (R);
9009 Generate_Operator_Reference (N, T);
9010 Check_Low_Bound_Tested (N);
9012 -- If this is an inequality, it may be the implicit inequality
9013 -- created for a user-defined operation, in which case the corres-
9014 -- ponding equality operation is not intrinsic, and the operation
9015 -- cannot be constant-folded. Else fold.
9017 if Nkind (N) = N_Op_Eq
9018 or else Comes_From_Source (Entity (N))
9019 or else Ekind (Entity (N)) = E_Operator
9020 or else
9021 Is_Intrinsic_Subprogram (Corresponding_Equality (Entity (N)))
9022 then
9023 Analyze_Dimension (N);
9024 Eval_Relational_Op (N);
9026 elsif Nkind (N) = N_Op_Ne
9027 and then Is_Abstract_Subprogram (Entity (N))
9028 then
9029 Nondispatching_Call_To_Abstract_Operation (N, Entity (N));
9030 end if;
9031 end if;
9032 end Resolve_Equality_Op;
9034 ----------------------------------
9035 -- Resolve_Explicit_Dereference --
9036 ----------------------------------
9038 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
9039 Loc : constant Source_Ptr := Sloc (N);
9040 New_N : Node_Id;
9041 P : constant Node_Id := Prefix (N);
9043 P_Typ : Entity_Id;
9044 -- The candidate prefix type, if overloaded
9046 I : Interp_Index;
9047 It : Interp;
9049 begin
9050 Check_Fully_Declared_Prefix (Typ, P);
9051 P_Typ := Empty;
9053 -- A useful optimization: check whether the dereference denotes an
9054 -- element of a container, and if so rewrite it as a call to the
9055 -- corresponding Element function.
9057 -- Disabled for now, on advice of ARG. A more restricted form of the
9058 -- predicate might be acceptable ???
9060 -- if Is_Container_Element (N) then
9061 -- return;
9062 -- end if;
9064 if Is_Overloaded (P) then
9066 -- Use the context type to select the prefix that has the correct
9067 -- designated type. Keep the first match, which will be the inner-
9068 -- most.
9070 Get_First_Interp (P, I, It);
9072 while Present (It.Typ) loop
9073 if Is_Access_Type (It.Typ)
9074 and then Covers (Typ, Designated_Type (It.Typ))
9075 then
9076 if No (P_Typ) then
9077 P_Typ := It.Typ;
9078 end if;
9080 -- Remove access types that do not match, but preserve access
9081 -- to subprogram interpretations, in case a further dereference
9082 -- is needed (see below).
9084 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
9085 Remove_Interp (I);
9086 end if;
9088 Get_Next_Interp (I, It);
9089 end loop;
9091 if Present (P_Typ) then
9092 Resolve (P, P_Typ);
9093 Set_Etype (N, Designated_Type (P_Typ));
9095 else
9096 -- If no interpretation covers the designated type of the prefix,
9097 -- this is the pathological case where not all implementations of
9098 -- the prefix allow the interpretation of the node as a call. Now
9099 -- that the expected type is known, Remove other interpretations
9100 -- from prefix, rewrite it as a call, and resolve again, so that
9101 -- the proper call node is generated.
9103 Get_First_Interp (P, I, It);
9104 while Present (It.Typ) loop
9105 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
9106 Remove_Interp (I);
9107 end if;
9109 Get_Next_Interp (I, It);
9110 end loop;
9112 New_N :=
9113 Make_Function_Call (Loc,
9114 Name =>
9115 Make_Explicit_Dereference (Loc,
9116 Prefix => P),
9117 Parameter_Associations => New_List);
9119 Save_Interps (N, New_N);
9120 Rewrite (N, New_N);
9121 Analyze_And_Resolve (N, Typ);
9122 return;
9123 end if;
9125 -- If not overloaded, resolve P with its own type
9127 else
9128 Resolve (P);
9129 end if;
9131 -- If the prefix might be null, add an access check
9133 if Is_Access_Type (Etype (P))
9134 and then not Can_Never_Be_Null (Etype (P))
9135 then
9136 Apply_Access_Check (N);
9137 end if;
9139 -- If the designated type is a packed unconstrained array type, and the
9140 -- explicit dereference is not in the context of an attribute reference,
9141 -- then we must compute and set the actual subtype, since it is needed
9142 -- by Gigi. The reason we exclude the attribute case is that this is
9143 -- handled fine by Gigi, and in fact we use such attributes to build the
9144 -- actual subtype. We also exclude generated code (which builds actual
9145 -- subtypes directly if they are needed).
9147 if Is_Packed_Array (Etype (N))
9148 and then not Is_Constrained (Etype (N))
9149 and then Nkind (Parent (N)) /= N_Attribute_Reference
9150 and then Comes_From_Source (N)
9151 then
9152 Set_Etype (N, Get_Actual_Subtype (N));
9153 end if;
9155 Analyze_Dimension (N);
9157 -- Note: No Eval processing is required for an explicit dereference,
9158 -- because such a name can never be static.
9160 end Resolve_Explicit_Dereference;
9162 -------------------------------------
9163 -- Resolve_Expression_With_Actions --
9164 -------------------------------------
9166 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9168 function OK_For_Static (Act : Node_Id) return Boolean;
9169 -- True if Act is an action of a declare_expression that is allowed in a
9170 -- static declare_expression.
9172 function All_OK_For_Static return Boolean;
9173 -- True if all actions of N are allowed in a static declare_expression.
9175 function Get_Literal (Expr : Node_Id) return Node_Id;
9176 -- Expr is an expression with compile-time-known value. This returns the
9177 -- literal node that reprsents that value.
9179 -------------------
9180 -- OK_For_Static --
9181 -------------------
9183 function OK_For_Static (Act : Node_Id) return Boolean is
9184 begin
9185 case Nkind (Act) is
9186 when N_Object_Declaration =>
9187 if Constant_Present (Act)
9188 and then Is_Static_Expression (Expression (Act))
9189 then
9190 return True;
9191 end if;
9193 when N_Object_Renaming_Declaration =>
9194 if Statically_Names_Object (Name (Act)) then
9195 return True;
9196 end if;
9198 when others =>
9199 -- No other declarations, nor even pragmas, are allowed in a
9200 -- declare expression, so if we see something else, it must be
9201 -- an internally generated expression_with_actions.
9202 null;
9203 end case;
9205 return False;
9206 end OK_For_Static;
9208 -----------------------
9209 -- All_OK_For_Static --
9210 -----------------------
9212 function All_OK_For_Static return Boolean is
9213 Act : Node_Id := First (Actions (N));
9214 begin
9215 while Present (Act) loop
9216 if not OK_For_Static (Act) then
9217 return False;
9218 end if;
9220 Next (Act);
9221 end loop;
9223 return True;
9224 end All_OK_For_Static;
9226 -----------------
9227 -- Get_Literal --
9228 -----------------
9230 function Get_Literal (Expr : Node_Id) return Node_Id is
9231 pragma Assert (Compile_Time_Known_Value (Expr));
9232 Result : Node_Id;
9233 begin
9234 case Nkind (Expr) is
9235 when N_Has_Entity =>
9236 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9237 Result := Expr;
9238 else
9239 Result := Constant_Value (Entity (Expr));
9240 end if;
9241 when N_Numeric_Or_String_Literal =>
9242 Result := Expr;
9243 when others =>
9244 raise Program_Error;
9245 end case;
9247 pragma Assert
9248 (Nkind (Result) in N_Numeric_Or_String_Literal
9249 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9250 return Result;
9251 end Get_Literal;
9253 -- Local variables
9255 Loc : constant Source_Ptr := Sloc (N);
9257 -- Start of processing for Resolve_Expression_With_Actions
9259 begin
9260 Set_Etype (N, Typ);
9262 if Is_Empty_List (Actions (N)) then
9263 pragma Assert (All_OK_For_Static); null;
9264 end if;
9266 -- If the value of the expression is known at compile time, and all
9267 -- of the actions (if any) are suitable, then replace the declare
9268 -- expression with its expression. This allows the declare expression
9269 -- as a whole to be static if appropriate. See AI12-0368.
9271 if Compile_Time_Known_Value (Expression (N)) then
9272 if Is_Empty_List (Actions (N)) then
9273 Rewrite (N, Expression (N));
9274 elsif All_OK_For_Static then
9275 Rewrite
9276 (N, New_Copy_Tree
9277 (Get_Literal (Expression (N)), New_Sloc => Loc));
9278 end if;
9279 end if;
9280 end Resolve_Expression_With_Actions;
9282 ----------------------------------
9283 -- Resolve_Generalized_Indexing --
9284 ----------------------------------
9286 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9287 Indexing : constant Node_Id := Generalized_Indexing (N);
9288 begin
9289 Rewrite (N, Indexing);
9290 Resolve (N, Typ);
9291 end Resolve_Generalized_Indexing;
9293 ---------------------------
9294 -- Resolve_If_Expression --
9295 ---------------------------
9297 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9298 procedure Apply_Check (Expr : Node_Id);
9299 -- When a dependent expression is of a subtype different from
9300 -- the context subtype, then insert a qualification to ensure
9301 -- the generation of a constraint check. This was previously
9302 -- for scalar types. For array types apply a length check, given
9303 -- that the context in general allows sliding, while a qualified
9304 -- expression forces equality of bounds.
9306 Result_Type : Entity_Id := Typ;
9307 -- So in most cases the type of the If_Expression and of its
9308 -- dependent expressions is that of the context. However, if
9309 -- the expression is the index of an Indexed_Component, we must
9310 -- ensure that a proper index check is applied, rather than a
9311 -- range check on the index type (which might be discriminant
9312 -- dependent). In this case we resolve with the base type of the
9313 -- index type, and the index check is generated in the resolution
9314 -- of the indexed_component above.
9316 -----------------
9317 -- Apply_Check --
9318 -----------------
9320 procedure Apply_Check (Expr : Node_Id) is
9321 Expr_Typ : constant Entity_Id := Etype (Expr);
9322 Loc : constant Source_Ptr := Sloc (Expr);
9324 begin
9325 if Expr_Typ = Typ
9326 or else Is_Tagged_Type (Typ)
9327 or else Is_Access_Type (Typ)
9328 or else not Is_Constrained (Typ)
9329 or else Inside_A_Generic
9330 then
9331 null;
9333 elsif Is_Array_Type (Typ) then
9334 Apply_Length_Check (Expr, Typ);
9336 else
9337 Rewrite (Expr,
9338 Make_Qualified_Expression (Loc,
9339 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9340 Expression => Relocate_Node (Expr)));
9342 Analyze_And_Resolve (Expr, Result_Type);
9343 end if;
9344 end Apply_Check;
9346 -- Local variables
9348 Condition : constant Node_Id := First (Expressions (N));
9349 Else_Expr : Node_Id;
9350 Then_Expr : Node_Id;
9352 -- Start of processing for Resolve_If_Expression
9354 begin
9355 -- Defend against malformed expressions
9357 if No (Condition) then
9358 return;
9359 end if;
9361 if Present (Parent (N))
9362 and then (Nkind (Parent (N)) = N_Indexed_Component
9363 or else Nkind (Parent (Parent (N))) = N_Indexed_Component)
9364 then
9365 Result_Type := Base_Type (Typ);
9366 end if;
9368 Then_Expr := Next (Condition);
9370 if No (Then_Expr) then
9371 return;
9372 end if;
9374 Else_Expr := Next (Then_Expr);
9376 Resolve (Condition, Any_Boolean);
9377 Resolve (Then_Expr, Result_Type);
9378 Check_Unset_Reference (Condition);
9379 Check_Unset_Reference (Then_Expr);
9381 Apply_Check (Then_Expr);
9383 -- If ELSE expression present, just resolve using the determined type
9384 -- If type is universal, resolve to any member of the class.
9386 if Present (Else_Expr) then
9387 if Typ = Universal_Integer then
9388 Resolve (Else_Expr, Any_Integer);
9390 elsif Typ = Universal_Real then
9391 Resolve (Else_Expr, Any_Real);
9393 else
9394 Resolve (Else_Expr, Result_Type);
9395 end if;
9397 Check_Unset_Reference (Else_Expr);
9399 Apply_Check (Else_Expr);
9401 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9402 -- dynamically tagged must be known statically.
9404 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9405 if Is_Dynamically_Tagged (Then_Expr) /=
9406 Is_Dynamically_Tagged (Else_Expr)
9407 then
9408 Error_Msg_N ("all or none of the dependent expressions "
9409 & "can be dynamically tagged", N);
9410 end if;
9411 end if;
9413 -- If no ELSE expression is present, root type must be Standard.Boolean
9414 -- and we provide a Standard.True result converted to the appropriate
9415 -- Boolean type (in case it is a derived boolean type).
9417 elsif Root_Type (Typ) = Standard_Boolean then
9418 Else_Expr :=
9419 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9420 Analyze_And_Resolve (Else_Expr, Result_Type);
9421 Append_To (Expressions (N), Else_Expr);
9423 else
9424 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9425 Append_To (Expressions (N), Error);
9426 end if;
9428 Set_Etype (N, Result_Type);
9430 if not Error_Posted (N) then
9431 Eval_If_Expression (N);
9432 end if;
9434 Analyze_Dimension (N);
9435 end Resolve_If_Expression;
9437 ----------------------------------
9438 -- Resolve_Implicit_Dereference --
9439 ----------------------------------
9441 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9442 Desig_Typ : Entity_Id;
9444 begin
9445 -- In an instance the proper view may not always be correct for
9446 -- private types, see e.g. Sem_Type.Covers for similar handling.
9448 if Is_Private_Type (Etype (P))
9449 and then Present (Full_View (Etype (P)))
9450 and then Is_Access_Type (Full_View (Etype (P)))
9451 and then In_Instance
9452 then
9453 Set_Etype (P, Full_View (Etype (P)));
9454 end if;
9456 if Is_Access_Type (Etype (P)) then
9457 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9458 Insert_Explicit_Dereference (P);
9459 Analyze_And_Resolve (P, Desig_Typ);
9460 end if;
9461 end Resolve_Implicit_Dereference;
9463 -------------------------------
9464 -- Resolve_Indexed_Component --
9465 -------------------------------
9467 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9468 Pref : constant Node_Id := Prefix (N);
9469 Expr : Node_Id;
9470 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9471 Index : Node_Id;
9473 begin
9474 if Present (Generalized_Indexing (N)) then
9475 Resolve_Generalized_Indexing (N, Typ);
9476 return;
9477 end if;
9479 if Is_Overloaded (Pref) then
9481 -- Use the context type to select the prefix that yields the correct
9482 -- component type.
9484 declare
9485 I : Interp_Index;
9486 It : Interp;
9487 I1 : Interp_Index := 0;
9488 Found : Boolean := False;
9490 begin
9491 Get_First_Interp (Pref, I, It);
9492 while Present (It.Typ) loop
9493 if (Is_Array_Type (It.Typ)
9494 and then Covers (Typ, Component_Type (It.Typ)))
9495 or else (Is_Access_Type (It.Typ)
9496 and then Is_Array_Type (Designated_Type (It.Typ))
9497 and then
9498 Covers
9499 (Typ,
9500 Component_Type (Designated_Type (It.Typ))))
9501 then
9502 if Found then
9503 It := Disambiguate (Pref, I1, I, Any_Type);
9505 if It = No_Interp then
9506 Error_Msg_N ("ambiguous prefix for indexing", N);
9507 Set_Etype (N, Typ);
9508 return;
9510 else
9511 Found := True;
9512 Array_Type := It.Typ;
9513 I1 := I;
9514 end if;
9516 else
9517 Found := True;
9518 Array_Type := It.Typ;
9519 I1 := I;
9520 end if;
9521 end if;
9523 Get_Next_Interp (I, It);
9524 end loop;
9525 end;
9527 else
9528 Array_Type := Etype (Pref);
9529 end if;
9531 Resolve (Pref, Array_Type);
9532 Array_Type := Get_Actual_Subtype_If_Available (Pref);
9534 -- If the prefix's type is an access type, get to the real array type.
9535 -- Note: we do not apply an access check because an explicit dereference
9536 -- will be introduced later, and the check will happen there.
9538 if Is_Access_Type (Array_Type) then
9539 Array_Type := Implicitly_Designated_Type (Array_Type);
9540 end if;
9542 -- If name was overloaded, set component type correctly now.
9543 -- If a misplaced call to an entry family (which has no index types)
9544 -- return. Error will be diagnosed from calling context.
9546 if Is_Array_Type (Array_Type) then
9547 Set_Etype (N, Component_Type (Array_Type));
9548 else
9549 return;
9550 end if;
9552 Index := First_Index (Array_Type);
9553 Expr := First (Expressions (N));
9555 -- The prefix may have resolved to a string literal, in which case its
9556 -- etype has a special representation. This is only possible currently
9557 -- if the prefix is a static concatenation, written in functional
9558 -- notation.
9560 if Ekind (Array_Type) = E_String_Literal_Subtype then
9561 Resolve (Expr, Standard_Positive);
9563 else
9564 while Present (Index) and then Present (Expr) loop
9565 Resolve (Expr, Etype (Index));
9566 Check_Unset_Reference (Expr);
9568 Apply_Scalar_Range_Check (Expr, Etype (Index));
9570 Next_Index (Index);
9571 Next (Expr);
9572 end loop;
9573 end if;
9575 Resolve_Implicit_Dereference (Pref);
9576 Analyze_Dimension (N);
9578 -- Do not generate the warning on suspicious index if we are analyzing
9579 -- package Ada.Tags; otherwise we will report the warning with the
9580 -- Prims_Ptr field of the dispatch table.
9582 if Scope (Etype (Pref)) = Standard_Standard
9583 or else not
9584 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Pref))), Ada_Tags)
9585 then
9586 Warn_On_Suspicious_Index (Pref, First (Expressions (N)));
9587 Eval_Indexed_Component (N);
9588 end if;
9590 -- If the array type is atomic and the component is not, then this is
9591 -- worth a warning before Ada 2022, since we have a situation where the
9592 -- access to the component may cause extra read/writes of the atomic
9593 -- object, or partial word accesses, both of which may be unexpected.
9595 if Nkind (N) = N_Indexed_Component
9596 and then Is_Atomic_Ref_With_Address (N)
9597 and then not (Has_Atomic_Components (Array_Type)
9598 or else (Is_Entity_Name (Pref)
9599 and then Has_Atomic_Components
9600 (Entity (Pref))))
9601 and then not Is_Atomic (Component_Type (Array_Type))
9602 and then Ada_Version < Ada_2022
9603 then
9604 Error_Msg_N
9605 ("??access to non-atomic component of atomic array", Pref);
9606 Error_Msg_N
9607 ("??\may cause unexpected accesses to atomic object", Pref);
9608 end if;
9609 end Resolve_Indexed_Component;
9611 -----------------------------
9612 -- Resolve_Integer_Literal --
9613 -----------------------------
9615 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9616 begin
9617 Set_Etype (N, Typ);
9618 Eval_Integer_Literal (N);
9619 end Resolve_Integer_Literal;
9621 --------------------------------
9622 -- Resolve_Intrinsic_Operator --
9623 --------------------------------
9625 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9626 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9627 Op : Entity_Id;
9628 Arg1 : Node_Id;
9629 Arg2 : Node_Id;
9631 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9632 -- If the operand is a literal, it cannot be the expression in a
9633 -- conversion. Use a qualified expression instead.
9635 ---------------------
9636 -- Convert_Operand --
9637 ---------------------
9639 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9640 Loc : constant Source_Ptr := Sloc (Opnd);
9641 Res : Node_Id;
9643 begin
9644 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9645 Res :=
9646 Make_Qualified_Expression (Loc,
9647 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9648 Expression => Relocate_Node (Opnd));
9649 Analyze (Res);
9651 else
9652 Res := Unchecked_Convert_To (Btyp, Opnd);
9653 end if;
9655 return Res;
9656 end Convert_Operand;
9658 -- Start of processing for Resolve_Intrinsic_Operator
9660 begin
9661 -- We must preserve the original entity in a generic setting, so that
9662 -- the legality of the operation can be verified in an instance.
9664 if not Expander_Active then
9665 return;
9666 end if;
9668 Op := Entity (N);
9669 while Scope (Op) /= Standard_Standard loop
9670 Op := Homonym (Op);
9671 pragma Assert (Present (Op));
9672 end loop;
9674 Set_Entity (N, Op);
9675 Set_Is_Overloaded (N, False);
9677 -- If the result or operand types are private, rewrite with unchecked
9678 -- conversions on the operands and the result, to expose the proper
9679 -- underlying numeric type.
9681 if Is_Private_Type (Typ)
9682 or else Is_Private_Type (Etype (Left_Opnd (N)))
9683 or else Is_Private_Type (Etype (Right_Opnd (N)))
9684 then
9685 Arg1 := Convert_Operand (Left_Opnd (N));
9687 if Nkind (N) = N_Op_Expon then
9688 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9689 else
9690 Arg2 := Convert_Operand (Right_Opnd (N));
9691 end if;
9693 if Nkind (Arg1) = N_Type_Conversion then
9694 Save_Interps (Left_Opnd (N), Expression (Arg1));
9695 end if;
9697 if Nkind (Arg2) = N_Type_Conversion then
9698 Save_Interps (Right_Opnd (N), Expression (Arg2));
9699 end if;
9701 Set_Left_Opnd (N, Arg1);
9702 Set_Right_Opnd (N, Arg2);
9704 Set_Etype (N, Btyp);
9705 Rewrite (N, Unchecked_Convert_To (Typ, N));
9706 Resolve (N, Typ);
9708 elsif Typ /= Etype (Left_Opnd (N))
9709 or else Typ /= Etype (Right_Opnd (N))
9710 then
9711 -- Add explicit conversion where needed, and save interpretations in
9712 -- case operands are overloaded.
9714 Arg1 := Convert_To (Typ, Left_Opnd (N));
9715 Arg2 := Convert_To (Typ, Right_Opnd (N));
9717 if Nkind (Arg1) = N_Type_Conversion then
9718 Save_Interps (Left_Opnd (N), Expression (Arg1));
9719 else
9720 Save_Interps (Left_Opnd (N), Arg1);
9721 end if;
9723 if Nkind (Arg2) = N_Type_Conversion then
9724 Save_Interps (Right_Opnd (N), Expression (Arg2));
9725 else
9726 Save_Interps (Right_Opnd (N), Arg2);
9727 end if;
9729 Rewrite (Left_Opnd (N), Arg1);
9730 Rewrite (Right_Opnd (N), Arg2);
9731 Analyze (Arg1);
9732 Analyze (Arg2);
9733 Resolve_Arithmetic_Op (N, Typ);
9735 else
9736 Resolve_Arithmetic_Op (N, Typ);
9737 end if;
9738 end Resolve_Intrinsic_Operator;
9740 --------------------------------------
9741 -- Resolve_Intrinsic_Unary_Operator --
9742 --------------------------------------
9744 procedure Resolve_Intrinsic_Unary_Operator
9745 (N : Node_Id;
9746 Typ : Entity_Id)
9748 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9749 Op : Entity_Id;
9750 Arg2 : Node_Id;
9752 begin
9753 Op := Entity (N);
9754 while Scope (Op) /= Standard_Standard loop
9755 Op := Homonym (Op);
9756 pragma Assert (Present (Op));
9757 end loop;
9759 Set_Entity (N, Op);
9761 if Is_Private_Type (Typ) then
9762 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9763 Save_Interps (Right_Opnd (N), Expression (Arg2));
9765 Set_Right_Opnd (N, Arg2);
9767 Set_Etype (N, Btyp);
9768 Rewrite (N, Unchecked_Convert_To (Typ, N));
9769 Resolve (N, Typ);
9771 else
9772 Resolve_Unary_Op (N, Typ);
9773 end if;
9774 end Resolve_Intrinsic_Unary_Operator;
9776 ------------------------
9777 -- Resolve_Logical_Op --
9778 ------------------------
9780 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9781 B_Typ : Entity_Id;
9783 begin
9784 Check_No_Direct_Boolean_Operators (N);
9786 -- Predefined operations on scalar types yield the base type. On the
9787 -- other hand, logical operations on arrays yield the type of the
9788 -- arguments (and the context).
9790 if Is_Array_Type (Typ) then
9791 B_Typ := Typ;
9792 else
9793 B_Typ := Base_Type (Typ);
9794 end if;
9796 -- The following test is required because the operands of the operation
9797 -- may be literals, in which case the resulting type appears to be
9798 -- compatible with a signed integer type, when in fact it is compatible
9799 -- only with modular types. If the context itself is universal, the
9800 -- operation is illegal.
9802 if not Valid_Boolean_Arg (Typ) then
9803 Error_Msg_N ("invalid context for logical operation", N);
9804 Set_Etype (N, Any_Type);
9805 return;
9807 elsif Typ = Any_Modular then
9808 Error_Msg_N
9809 ("no modular type available in this context", N);
9810 Set_Etype (N, Any_Type);
9811 return;
9813 elsif Is_Modular_Integer_Type (Typ)
9814 and then Etype (Left_Opnd (N)) = Universal_Integer
9815 and then Etype (Right_Opnd (N)) = Universal_Integer
9816 then
9817 Check_For_Visible_Operator (N, B_Typ);
9818 end if;
9820 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9821 -- is active and the result type is standard Boolean (do not mess with
9822 -- ops that return a nonstandard Boolean type, because something strange
9823 -- is going on).
9825 -- Note: you might expect this replacement to be done during expansion,
9826 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9827 -- is used, no part of the right operand of an "and" or "or" operator
9828 -- should be executed if the left operand would short-circuit the
9829 -- evaluation of the corresponding "and then" or "or else". If we left
9830 -- the replacement to expansion time, then run-time checks associated
9831 -- with such operands would be evaluated unconditionally, due to being
9832 -- before the condition prior to the rewriting as short-circuit forms
9833 -- during expansion.
9835 if Short_Circuit_And_Or
9836 and then B_Typ = Standard_Boolean
9837 and then Nkind (N) in N_Op_And | N_Op_Or
9838 then
9839 -- Mark the corresponding putative SCO operator as truly a logical
9840 -- (and short-circuit) operator.
9842 if Generate_SCO and then Comes_From_Source (N) then
9843 Set_SCO_Logical_Operator (N);
9844 end if;
9846 if Nkind (N) = N_Op_And then
9847 Rewrite (N,
9848 Make_And_Then (Sloc (N),
9849 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9850 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9851 Analyze_And_Resolve (N, B_Typ);
9853 -- Case of OR changed to OR ELSE
9855 else
9856 Rewrite (N,
9857 Make_Or_Else (Sloc (N),
9858 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9859 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9860 Analyze_And_Resolve (N, B_Typ);
9861 end if;
9863 -- Return now, since analysis of the rewritten ops will take care of
9864 -- other reference bookkeeping and expression folding.
9866 return;
9867 end if;
9869 Resolve (Left_Opnd (N), B_Typ);
9870 Resolve (Right_Opnd (N), B_Typ);
9872 Check_Unset_Reference (Left_Opnd (N));
9873 Check_Unset_Reference (Right_Opnd (N));
9875 Set_Etype (N, B_Typ);
9876 Generate_Operator_Reference (N, B_Typ);
9877 Eval_Logical_Op (N);
9878 end Resolve_Logical_Op;
9880 ---------------------------------
9881 -- Resolve_Membership_Equality --
9882 ---------------------------------
9884 procedure Resolve_Membership_Equality (N : Node_Id; Typ : Entity_Id) is
9885 Utyp : constant Entity_Id := Underlying_Type (Typ);
9887 begin
9888 -- RM 4.5.2(4.1/3): if the type is limited, then it shall have a visible
9889 -- primitive equality operator. This means that we can use the regular
9890 -- visibility-based resolution and reset Entity in order to trigger it.
9892 if Is_Limited_Type (Typ) then
9893 Set_Entity (N, Empty);
9895 -- RM 4.5.2(28.1/3): if the type is a record, then the membership test
9896 -- uses the primitive equality for the type [even if it is not visible].
9897 -- We only deal with the untagged case here, because the tagged case is
9898 -- handled uniformly in the expander.
9900 elsif Is_Record_Type (Utyp) and then not Is_Tagged_Type (Utyp) then
9901 declare
9902 Eq_Id : constant Entity_Id := Get_User_Defined_Equality (Typ);
9904 begin
9905 if Present (Eq_Id) then
9906 Rewrite_Operator_As_Call (N, Eq_Id);
9907 end if;
9908 end;
9909 end if;
9910 end Resolve_Membership_Equality;
9912 ---------------------------
9913 -- Resolve_Membership_Op --
9914 ---------------------------
9916 -- The context can only be a boolean type, and does not determine the
9917 -- arguments. Arguments should be unambiguous, but the preference rule for
9918 -- universal types applies.
9920 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
9921 pragma Assert (Is_Boolean_Type (Typ));
9923 L : constant Node_Id := Left_Opnd (N);
9924 R : constant Node_Id := Right_Opnd (N);
9925 T : Entity_Id;
9927 procedure Resolve_Set_Membership;
9928 -- Analysis has determined a unique type for the left operand. Use it as
9929 -- the basis to resolve the disjuncts.
9931 ----------------------------
9932 -- Resolve_Set_Membership --
9933 ----------------------------
9935 procedure Resolve_Set_Membership is
9936 Alt : Node_Id;
9938 begin
9939 -- If the left operand is overloaded, find type compatible with not
9940 -- overloaded alternative of the right operand.
9942 Alt := First (Alternatives (N));
9943 if Is_Overloaded (L) then
9944 T := Empty;
9945 while Present (Alt) loop
9946 if not Is_Overloaded (Alt) then
9947 T := Intersect_Types (L, Alt);
9948 exit;
9949 else
9950 Next (Alt);
9951 end if;
9952 end loop;
9954 -- Unclear how to resolve expression if all alternatives are also
9955 -- overloaded.
9957 if No (T) then
9958 Error_Msg_N ("ambiguous expression", N);
9959 end if;
9961 else
9962 T := Intersect_Types (L, Alt);
9963 end if;
9965 Resolve (L, T);
9967 Alt := First (Alternatives (N));
9968 while Present (Alt) loop
9970 -- Alternative is an expression, a range
9971 -- or a subtype mark.
9973 if not Is_Entity_Name (Alt)
9974 or else not Is_Type (Entity (Alt))
9975 then
9976 Resolve (Alt, T);
9977 end if;
9979 Next (Alt);
9980 end loop;
9982 -- Check for duplicates for discrete case
9984 if Is_Discrete_Type (T) then
9985 declare
9986 type Ent is record
9987 Alt : Node_Id;
9988 Val : Uint;
9989 end record;
9991 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
9992 Nalts : Nat;
9994 begin
9995 -- Loop checking duplicates. This is quadratic, but giant sets
9996 -- are unlikely in this context so it's a reasonable choice.
9998 Nalts := 0;
9999 Alt := First (Alternatives (N));
10000 while Present (Alt) loop
10001 if Is_OK_Static_Expression (Alt)
10002 and then Nkind (Alt) in N_Integer_Literal
10003 | N_Character_Literal
10004 | N_Has_Entity
10005 then
10006 Nalts := Nalts + 1;
10007 Alts (Nalts) := (Alt, Expr_Value (Alt));
10009 for J in 1 .. Nalts - 1 loop
10010 if Alts (J).Val = Alts (Nalts).Val then
10011 Error_Msg_Sloc := Sloc (Alts (J).Alt);
10012 Error_Msg_N ("duplicate of value given#??", Alt);
10013 end if;
10014 end loop;
10015 end if;
10017 Next (Alt);
10018 end loop;
10019 end;
10020 end if;
10022 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
10023 -- limited types, evaluation of a membership test uses the predefined
10024 -- equality for the type. This may be confusing to users, and the
10025 -- following warning appears useful for the most common case.
10027 if Is_Scalar_Type (Etype (L))
10028 and then Present (Get_User_Defined_Equality (Etype (L)))
10029 then
10030 Error_Msg_NE
10031 ("membership test on& uses predefined equality?", N, Etype (L));
10032 Error_Msg_N
10033 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
10034 end if;
10035 end Resolve_Set_Membership;
10037 -- Start of processing for Resolve_Membership_Op
10039 begin
10040 if L = Error or else R = Error then
10041 return;
10042 end if;
10044 if Present (Alternatives (N)) then
10045 Resolve_Set_Membership;
10046 goto SM_Exit;
10048 elsif not Is_Overloaded (R)
10049 and then Is_Universal_Numeric_Type (Etype (R))
10050 and then Is_Overloaded (L)
10051 then
10052 T := Etype (R);
10054 -- Ada 2005 (AI-251): Support the following case:
10056 -- type I is interface;
10057 -- type T is tagged ...
10059 -- function Test (O : I'Class) is
10060 -- begin
10061 -- return O in T'Class.
10062 -- end Test;
10064 -- In this case we have nothing else to do. The membership test will be
10065 -- done at run time.
10067 elsif Ada_Version >= Ada_2005
10068 and then Is_Class_Wide_Type (Etype (L))
10069 and then Is_Interface (Etype (L))
10070 and then not Is_Interface (Etype (R))
10071 then
10072 return;
10073 else
10074 T := Intersect_Types (L, R);
10075 end if;
10077 -- If mixed-mode operations are present and operands are all literal,
10078 -- the only interpretation involves Duration, which is probably not
10079 -- the intention of the programmer.
10081 if T = Any_Fixed then
10082 T := Unique_Fixed_Point_Type (N);
10084 if T = Any_Type then
10085 return;
10086 end if;
10087 end if;
10089 Resolve (L, T);
10090 Check_Unset_Reference (L);
10092 if Nkind (R) = N_Range
10093 and then not Is_Scalar_Type (T)
10094 then
10095 Error_Msg_N ("scalar type required for range", R);
10096 end if;
10098 if Is_Entity_Name (R) then
10099 Freeze_Expression (R);
10100 else
10101 Resolve (R, T);
10102 Check_Unset_Reference (R);
10103 end if;
10105 -- Here after resolving membership operation
10107 <<SM_Exit>>
10109 Eval_Membership_Op (N);
10110 end Resolve_Membership_Op;
10112 ------------------
10113 -- Resolve_Null --
10114 ------------------
10116 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
10117 Loc : constant Source_Ptr := Sloc (N);
10119 begin
10120 -- Handle restriction against anonymous null access values This
10121 -- restriction can be turned off using -gnatdj.
10123 -- Ada 2005 (AI-231): Remove restriction
10125 if Ada_Version < Ada_2005
10126 and then not Debug_Flag_J
10127 and then Ekind (Typ) = E_Anonymous_Access_Type
10128 and then Comes_From_Source (N)
10129 then
10130 -- In the common case of a call which uses an explicitly null value
10131 -- for an access parameter, give specialized error message.
10133 if Nkind (Parent (N)) in N_Subprogram_Call then
10134 Error_Msg_N
10135 ("NULL is not allowed as argument for an access parameter", N);
10137 -- Standard message for all other cases (are there any?)
10139 else
10140 Error_Msg_N
10141 ("NULL cannot be of an anonymous access type", N);
10142 end if;
10143 end if;
10145 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
10146 -- assignment to a null-excluding object.
10148 if Ada_Version >= Ada_2005
10149 and then Can_Never_Be_Null (Typ)
10150 and then Nkind (Parent (N)) = N_Assignment_Statement
10151 then
10152 if Inside_Init_Proc then
10154 -- Decide whether to generate an if_statement around our
10155 -- null-excluding check to avoid them on certain internal object
10156 -- declarations by looking at the type the current Init_Proc
10157 -- belongs to.
10159 -- Generate:
10160 -- if T1b_skip_null_excluding_check then
10161 -- [constraint_error "access check failed"]
10162 -- end if;
10164 if Needs_Conditional_Null_Excluding_Check
10165 (Etype (First_Formal (Enclosing_Init_Proc)))
10166 then
10167 Insert_Action (N,
10168 Make_If_Statement (Loc,
10169 Condition =>
10170 Make_Identifier (Loc,
10171 New_External_Name
10172 (Chars (Typ), "_skip_null_excluding_check")),
10173 Then_Statements =>
10174 New_List (
10175 Make_Raise_Constraint_Error (Loc,
10176 Reason => CE_Access_Check_Failed))));
10178 -- Otherwise, simply create the check
10180 else
10181 Insert_Action (N,
10182 Make_Raise_Constraint_Error (Loc,
10183 Reason => CE_Access_Check_Failed));
10184 end if;
10185 else
10186 Insert_Action
10187 (Compile_Time_Constraint_Error (N,
10188 "(Ada 2005) NULL not allowed in null-excluding objects??"),
10189 Make_Raise_Constraint_Error (Loc,
10190 Reason => CE_Access_Check_Failed));
10191 end if;
10192 end if;
10194 -- In a distributed context, null for a remote access to subprogram may
10195 -- need to be replaced with a special record aggregate. In this case,
10196 -- return after having done the transformation.
10198 if (Ekind (Typ) = E_Record_Type
10199 or else Is_Remote_Access_To_Subprogram_Type (Typ))
10200 and then Remote_AST_Null_Value (N, Typ)
10201 then
10202 return;
10203 end if;
10205 -- The null literal takes its type from the context
10207 Set_Etype (N, Typ);
10208 end Resolve_Null;
10210 -----------------------
10211 -- Resolve_Op_Concat --
10212 -----------------------
10214 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
10216 -- We wish to avoid deep recursion, because concatenations are often
10217 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
10218 -- operands nonrecursively until we find something that is not a simple
10219 -- concatenation (A in this case). We resolve that, and then walk back
10220 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10221 -- to do the rest of the work at each level. The Parent pointers allow
10222 -- us to avoid recursion, and thus avoid running out of memory. See also
10223 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10225 NN : Node_Id := N;
10226 Op1 : Node_Id;
10228 begin
10229 -- The following code is equivalent to:
10231 -- Resolve_Op_Concat_First (NN, Typ);
10232 -- Resolve_Op_Concat_Arg (N, ...);
10233 -- Resolve_Op_Concat_Rest (N, Typ);
10235 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10236 -- operand is a concatenation.
10238 -- Walk down left operands
10240 loop
10241 Resolve_Op_Concat_First (NN, Typ);
10242 Op1 := Left_Opnd (NN);
10243 exit when not (Nkind (Op1) = N_Op_Concat
10244 and then not Is_Array_Type (Component_Type (Typ))
10245 and then Entity (Op1) = Entity (NN));
10246 NN := Op1;
10247 end loop;
10249 -- Now (given the above example) NN is A&B and Op1 is A
10251 -- First resolve Op1 ...
10253 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10255 -- ... then walk NN back up until we reach N (where we started), calling
10256 -- Resolve_Op_Concat_Rest along the way.
10258 loop
10259 Resolve_Op_Concat_Rest (NN, Typ);
10260 exit when NN = N;
10261 NN := Parent (NN);
10262 end loop;
10263 end Resolve_Op_Concat;
10265 ---------------------------
10266 -- Resolve_Op_Concat_Arg --
10267 ---------------------------
10269 procedure Resolve_Op_Concat_Arg
10270 (N : Node_Id;
10271 Arg : Node_Id;
10272 Typ : Entity_Id;
10273 Is_Comp : Boolean)
10275 Btyp : constant Entity_Id := Base_Type (Typ);
10276 Ctyp : constant Entity_Id := Component_Type (Typ);
10278 begin
10279 if In_Instance then
10280 if Is_Comp
10281 or else (not Is_Overloaded (Arg)
10282 and then Etype (Arg) /= Any_Composite
10283 and then Covers (Ctyp, Etype (Arg)))
10284 then
10285 Resolve (Arg, Ctyp);
10286 else
10287 Resolve (Arg, Btyp);
10288 end if;
10290 -- If both Array & Array and Array & Component are visible, there is a
10291 -- potential ambiguity that must be reported.
10293 elsif Has_Compatible_Type (Arg, Ctyp) then
10294 if Nkind (Arg) = N_Aggregate
10295 and then Is_Composite_Type (Ctyp)
10296 then
10297 if Is_Private_Type (Ctyp) then
10298 Resolve (Arg, Btyp);
10300 -- If the operation is user-defined and not overloaded use its
10301 -- profile. The operation may be a renaming, in which case it has
10302 -- been rewritten, and we want the original profile.
10304 elsif not Is_Overloaded (N)
10305 and then Comes_From_Source (Entity (Original_Node (N)))
10306 and then Ekind (Entity (Original_Node (N))) = E_Function
10307 then
10308 Resolve (Arg,
10309 Etype
10310 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10311 return;
10313 -- Otherwise an aggregate may match both the array type and the
10314 -- component type.
10316 else
10317 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10318 Set_Etype (Arg, Any_Type);
10319 end if;
10321 else
10322 if Is_Overloaded (Arg)
10323 and then Has_Compatible_Type (Arg, Typ)
10324 and then Etype (Arg) /= Any_Type
10325 then
10326 declare
10327 I : Interp_Index;
10328 It : Interp;
10329 Func : Entity_Id;
10331 begin
10332 Get_First_Interp (Arg, I, It);
10333 Func := It.Nam;
10334 Get_Next_Interp (I, It);
10336 -- Special-case the error message when the overloading is
10337 -- caused by a function that yields an array and can be
10338 -- called without parameters.
10340 if It.Nam = Func then
10341 Error_Msg_Sloc := Sloc (Func);
10342 Error_Msg_N ("ambiguous call to function#", Arg);
10343 Error_Msg_NE
10344 ("\\interpretation as call yields&", Arg, Typ);
10345 Error_Msg_NE
10346 ("\\interpretation as indexing of call yields&",
10347 Arg, Ctyp);
10349 else
10350 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10352 Get_First_Interp (Arg, I, It);
10353 while Present (It.Nam) loop
10354 Error_Msg_Sloc := Sloc (It.Nam);
10356 if Base_Type (It.Typ) = Btyp
10357 or else
10358 Base_Type (It.Typ) = Base_Type (Ctyp)
10359 then
10360 Error_Msg_N -- CODEFIX
10361 ("\\possible interpretation#", Arg);
10362 end if;
10364 Get_Next_Interp (I, It);
10365 end loop;
10366 end if;
10367 end;
10368 end if;
10370 Resolve (Arg, Ctyp);
10372 if Nkind (Arg) = N_String_Literal then
10373 Set_Etype (Arg, Ctyp);
10375 elsif Is_Scalar_Type (Etype (Arg))
10376 and then Compile_Time_Known_Value (Arg)
10377 then
10378 -- Determine if the out-of-range violation constitutes a
10379 -- warning or an error according to the expression base type,
10380 -- according to Ada 2022 RM 4.9 (35/2).
10382 if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then
10383 Apply_Compile_Time_Constraint_Error
10384 (Arg, "value not in range of}", CE_Range_Check_Failed,
10385 Ent => Base_Type (Ctyp),
10386 Typ => Base_Type (Ctyp));
10388 elsif Is_Out_Of_Range (Arg, Ctyp) then
10389 Apply_Compile_Time_Constraint_Error
10390 (Arg, "value not in range of}??", CE_Range_Check_Failed,
10391 Ent => Ctyp,
10392 Typ => Ctyp);
10393 end if;
10394 end if;
10396 if Arg = Left_Opnd (N) then
10397 Set_Is_Component_Left_Opnd (N);
10398 else
10399 Set_Is_Component_Right_Opnd (N);
10400 end if;
10401 end if;
10403 else
10404 Resolve (Arg, Btyp);
10405 end if;
10407 Check_Unset_Reference (Arg);
10408 end Resolve_Op_Concat_Arg;
10410 -----------------------------
10411 -- Resolve_Op_Concat_First --
10412 -----------------------------
10414 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10415 Btyp : constant Entity_Id := Base_Type (Typ);
10416 Op1 : constant Node_Id := Left_Opnd (N);
10417 Op2 : constant Node_Id := Right_Opnd (N);
10419 begin
10420 -- The parser folds an enormous sequence of concatenations of string
10421 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10422 -- in the right operand. If the expression resolves to a predefined "&"
10423 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10424 -- we give an error. See P_Simple_Expression in Par.Ch4.
10426 if Nkind (Op2) = N_String_Literal
10427 and then Is_Folded_In_Parser (Op2)
10428 and then Ekind (Entity (N)) = E_Function
10429 then
10430 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10431 and then String_Length (Strval (Op1)) = 0);
10432 Error_Msg_N ("too many user-defined concatenations", N);
10433 return;
10434 end if;
10436 Set_Etype (N, Btyp);
10438 if Is_Limited_Composite (Btyp) then
10439 Error_Msg_N ("concatenation not available for limited array", N);
10440 Explain_Limited_Type (Btyp, N);
10441 end if;
10442 end Resolve_Op_Concat_First;
10444 ----------------------------
10445 -- Resolve_Op_Concat_Rest --
10446 ----------------------------
10448 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10449 Op1 : constant Node_Id := Left_Opnd (N);
10450 Op2 : constant Node_Id := Right_Opnd (N);
10452 begin
10453 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10455 Generate_Operator_Reference (N, Typ);
10457 if Is_String_Type (Typ) then
10458 Eval_Concatenation (N);
10459 end if;
10461 -- If this is not a static concatenation, but the result is a string
10462 -- type (and not an array of strings) ensure that static string operands
10463 -- have their subtypes properly constructed.
10465 if Nkind (N) /= N_String_Literal
10466 and then Is_Character_Type (Component_Type (Typ))
10467 then
10468 Set_String_Literal_Subtype (Op1, Typ);
10469 Set_String_Literal_Subtype (Op2, Typ);
10470 end if;
10471 end Resolve_Op_Concat_Rest;
10473 ----------------------
10474 -- Resolve_Op_Expon --
10475 ----------------------
10477 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10478 B_Typ : constant Entity_Id := Base_Type (Typ);
10480 begin
10481 -- Catch attempts to do fixed-point exponentiation with universal
10482 -- operands, which is a case where the illegality is not caught during
10483 -- normal operator analysis. This is not done in preanalysis mode
10484 -- since the tree is not fully decorated during preanalysis.
10486 if Full_Analysis then
10487 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10488 Error_Msg_N ("exponentiation not available for fixed point", N);
10489 return;
10491 elsif Nkind (Parent (N)) in N_Op
10492 and then Present (Etype (Parent (N)))
10493 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10494 and then Etype (N) = Universal_Real
10495 and then Comes_From_Source (N)
10496 then
10497 Error_Msg_N ("exponentiation not available for fixed point", N);
10498 return;
10499 end if;
10500 end if;
10502 if Comes_From_Source (N)
10503 and then Ekind (Entity (N)) = E_Function
10504 and then Is_Imported (Entity (N))
10505 and then Is_Intrinsic_Subprogram (Entity (N))
10506 then
10507 Resolve_Intrinsic_Operator (N, Typ);
10508 return;
10509 end if;
10511 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10512 Check_For_Visible_Operator (N, B_Typ);
10513 end if;
10515 -- We do the resolution using the base type, because intermediate values
10516 -- in expressions are always of the base type, not a subtype of it.
10518 Resolve (Left_Opnd (N), B_Typ);
10519 Resolve (Right_Opnd (N), Standard_Integer);
10521 -- For integer types, right argument must be in Natural range
10523 if Is_Integer_Type (Typ) then
10524 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10525 end if;
10527 Check_Unset_Reference (Left_Opnd (N));
10528 Check_Unset_Reference (Right_Opnd (N));
10530 Set_Etype (N, B_Typ);
10531 Generate_Operator_Reference (N, B_Typ);
10533 Analyze_Dimension (N);
10535 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10536 -- Evaluate the exponentiation operator for dimensioned type
10538 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10539 else
10540 Eval_Op_Expon (N);
10541 end if;
10543 -- Set overflow checking bit. Much cleverer code needed here eventually
10544 -- and perhaps the Resolve routines should be separated for the various
10545 -- arithmetic operations, since they will need different processing. ???
10547 if Nkind (N) in N_Op then
10548 if not Overflow_Checks_Suppressed (Etype (N)) then
10549 Enable_Overflow_Check (N);
10550 end if;
10551 end if;
10552 end Resolve_Op_Expon;
10554 --------------------
10555 -- Resolve_Op_Not --
10556 --------------------
10558 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10559 function Parent_Is_Boolean return Boolean;
10560 -- This function determines if the parent node is a boolean operator or
10561 -- operation (comparison op, membership test, or short circuit form) and
10562 -- the not in question is the left operand of this operation. Note that
10563 -- if the not is in parens, then false is returned.
10565 -----------------------
10566 -- Parent_Is_Boolean --
10567 -----------------------
10569 function Parent_Is_Boolean return Boolean is
10570 begin
10571 return Paren_Count (N) = 0
10572 and then Nkind (Parent (N)) in N_Membership_Test
10573 | N_Op_Boolean
10574 | N_Short_Circuit
10575 and then Left_Opnd (Parent (N)) = N;
10576 end Parent_Is_Boolean;
10578 -- Local variables
10580 B_Typ : Entity_Id;
10582 -- Start of processing for Resolve_Op_Not
10584 begin
10585 -- Predefined operations on scalar types yield the base type. On the
10586 -- other hand, logical operations on arrays yield the type of the
10587 -- arguments (and the context).
10589 if Is_Array_Type (Typ) then
10590 B_Typ := Typ;
10591 else
10592 B_Typ := Base_Type (Typ);
10593 end if;
10595 -- Straightforward case of incorrect arguments
10597 if not Valid_Boolean_Arg (Typ) then
10598 Error_Msg_N ("invalid operand type for operator&", N);
10599 Set_Etype (N, Any_Type);
10600 return;
10602 -- Special case of probable missing parens
10604 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10605 if Parent_Is_Boolean then
10606 Error_Msg_N
10607 ("operand of NOT must be enclosed in parentheses",
10608 Right_Opnd (N));
10609 else
10610 Error_Msg_N
10611 ("no modular type available in this context", N);
10612 end if;
10614 Set_Etype (N, Any_Type);
10615 return;
10617 -- OK resolution of NOT
10619 else
10620 -- Warn if non-boolean types involved. This is a case like not a < b
10621 -- where a and b are modular, where we will get (not a) < b and most
10622 -- likely not (a < b) was intended.
10624 if Warn_On_Questionable_Missing_Parens
10625 and then not Is_Boolean_Type (Typ)
10626 and then Parent_Is_Boolean
10627 then
10628 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10629 end if;
10631 -- Warn on double negation if checking redundant constructs
10633 if Warn_On_Redundant_Constructs
10634 and then Comes_From_Source (N)
10635 and then Comes_From_Source (Right_Opnd (N))
10636 and then Root_Type (Typ) = Standard_Boolean
10637 and then Nkind (Right_Opnd (N)) = N_Op_Not
10638 then
10639 Error_Msg_N ("redundant double negation?r?", N);
10640 end if;
10642 -- Complete resolution and evaluation of NOT
10644 Resolve (Right_Opnd (N), B_Typ);
10645 Check_Unset_Reference (Right_Opnd (N));
10646 Set_Etype (N, B_Typ);
10647 Generate_Operator_Reference (N, B_Typ);
10648 Eval_Op_Not (N);
10649 end if;
10650 end Resolve_Op_Not;
10652 -----------------------------
10653 -- Resolve_Operator_Symbol --
10654 -----------------------------
10656 -- Nothing to be done, all resolved already
10658 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10659 pragma Warnings (Off, N);
10660 pragma Warnings (Off, Typ);
10662 begin
10663 null;
10664 end Resolve_Operator_Symbol;
10666 ----------------------------------
10667 -- Resolve_Qualified_Expression --
10668 ----------------------------------
10670 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10671 pragma Warnings (Off, Typ);
10673 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10674 Expr : constant Node_Id := Expression (N);
10676 begin
10677 Resolve (Expr, Target_Typ);
10678 Check_Unset_Reference (Expr);
10680 -- A qualified expression requires an exact match of the type, class-
10681 -- wide matching is not allowed. However, if the qualifying type is
10682 -- specific and the expression has a class-wide type, it may still be
10683 -- okay, since it can be the result of the expansion of a call to a
10684 -- dispatching function, so we also have to check class-wideness of the
10685 -- type of the expression's original node.
10687 if (Is_Class_Wide_Type (Target_Typ)
10688 or else
10689 (Is_Class_Wide_Type (Etype (Expr))
10690 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10691 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10692 then
10693 Wrong_Type (Expr, Target_Typ);
10694 end if;
10696 -- If the target type is unconstrained, then we reset the type of the
10697 -- result from the type of the expression. For other cases, the actual
10698 -- subtype of the expression is the target type. But we avoid doing it
10699 -- for an allocator since this is not needed and might be problematic.
10701 if Is_Composite_Type (Target_Typ)
10702 and then not Is_Constrained (Target_Typ)
10703 and then Nkind (Parent (N)) /= N_Allocator
10704 then
10705 Set_Etype (N, Etype (Expr));
10706 end if;
10708 Analyze_Dimension (N);
10709 Eval_Qualified_Expression (N);
10711 -- If we still have a qualified expression after the static evaluation,
10712 -- then apply a scalar range check if needed. The reason that we do this
10713 -- after the Eval call is that otherwise, the application of the range
10714 -- check may convert an illegal static expression and result in warning
10715 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10717 if Nkind (N) = N_Qualified_Expression
10718 and then Is_Scalar_Type (Target_Typ)
10719 then
10720 Apply_Scalar_Range_Check (Expr, Target_Typ);
10721 end if;
10723 -- AI12-0100: Once the qualified expression is resolved, check whether
10724 -- operand satisfies a static predicate of the target subtype, if any.
10725 -- In the static expression case, a predicate check failure is an error.
10727 if Has_Predicates (Target_Typ) then
10728 Check_Expression_Against_Static_Predicate
10729 (Expr, Target_Typ, Static_Failure_Is_Error => True);
10730 end if;
10731 end Resolve_Qualified_Expression;
10733 ------------------------------
10734 -- Resolve_Raise_Expression --
10735 ------------------------------
10737 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10738 begin
10739 if Typ = Raise_Type then
10740 Error_Msg_N ("cannot find unique type for raise expression", N);
10741 Set_Etype (N, Any_Type);
10743 else
10744 Set_Etype (N, Typ);
10746 -- Apply check for required parentheses in the enclosing
10747 -- context of raise_expressions (RM 11.3 (2)), including default
10748 -- expressions in contexts that can include aspect specifications,
10749 -- and ancestor parts of extension aggregates.
10751 declare
10752 Par : Node_Id := Parent (N);
10753 Parentheses_Found : Boolean := Paren_Count (N) > 0;
10755 begin
10756 while Present (Par)
10757 and then Nkind (Par) in N_Has_Etype
10758 loop
10759 if Paren_Count (Par) > 0 then
10760 Parentheses_Found := True;
10761 end if;
10763 if Nkind (Par) = N_Extension_Aggregate
10764 and then N = Ancestor_Part (Par)
10765 then
10766 exit;
10767 end if;
10769 Par := Parent (Par);
10770 end loop;
10772 if not Parentheses_Found
10773 and then Comes_From_Source (Par)
10774 and then
10775 ((Nkind (Par) in N_Modular_Type_Definition
10776 | N_Floating_Point_Definition
10777 | N_Ordinary_Fixed_Point_Definition
10778 | N_Decimal_Fixed_Point_Definition
10779 | N_Extension_Aggregate
10780 | N_Discriminant_Specification
10781 | N_Parameter_Specification
10782 | N_Formal_Object_Declaration)
10784 or else (Nkind (Par) = N_Object_Declaration
10785 and then
10786 Nkind (Parent (Par)) /= N_Extended_Return_Statement))
10787 then
10788 Error_Msg_N
10789 ("raise_expression must be parenthesized in this context",
10791 end if;
10792 end;
10793 end if;
10794 end Resolve_Raise_Expression;
10796 -------------------
10797 -- Resolve_Range --
10798 -------------------
10800 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10801 L : constant Node_Id := Low_Bound (N);
10802 H : constant Node_Id := High_Bound (N);
10804 function First_Last_Ref return Boolean;
10805 -- Returns True if N is of the form X'First .. X'Last where X is the
10806 -- same entity for both attributes.
10808 --------------------
10809 -- First_Last_Ref --
10810 --------------------
10812 function First_Last_Ref return Boolean is
10813 Lorig : constant Node_Id := Original_Node (L);
10814 Horig : constant Node_Id := Original_Node (H);
10816 begin
10817 if Nkind (Lorig) = N_Attribute_Reference
10818 and then Nkind (Horig) = N_Attribute_Reference
10819 and then Attribute_Name (Lorig) = Name_First
10820 and then Attribute_Name (Horig) = Name_Last
10821 then
10822 declare
10823 PL : constant Node_Id := Prefix (Lorig);
10824 PH : constant Node_Id := Prefix (Horig);
10825 begin
10826 return Is_Entity_Name (PL)
10827 and then Is_Entity_Name (PH)
10828 and then Entity (PL) = Entity (PH);
10829 end;
10830 end if;
10832 return False;
10833 end First_Last_Ref;
10835 -- Start of processing for Resolve_Range
10837 begin
10838 Set_Etype (N, Typ);
10840 Resolve (L, Typ);
10841 Resolve (H, Typ);
10843 -- Reanalyze the lower bound after both bounds have been analyzed, so
10844 -- that the range is known to be static or not by now. This may trigger
10845 -- more compile-time evaluation, which is useful for static analysis
10846 -- with GNATprove. This is not needed for compilation or static analysis
10847 -- with CodePeer, as full expansion does that evaluation then.
10849 if GNATprove_Mode then
10850 Set_Analyzed (L, False);
10851 Resolve (L, Typ);
10852 end if;
10854 -- Check for inappropriate range on unordered enumeration type
10856 if Bad_Unordered_Enumeration_Reference (N, Typ)
10858 -- Exclude X'First .. X'Last if X is the same entity for both
10860 and then not First_Last_Ref
10861 then
10862 Error_Msg_Sloc := Sloc (Typ);
10863 Error_Msg_NE
10864 ("subrange of unordered enumeration type& declared#?.u?", N, Typ);
10865 end if;
10867 Check_Unset_Reference (L);
10868 Check_Unset_Reference (H);
10870 -- We have to check the bounds for being within the base range as
10871 -- required for a non-static context. Normally this is automatic and
10872 -- done as part of evaluating expressions, but the N_Range node is an
10873 -- exception, since in GNAT we consider this node to be a subexpression,
10874 -- even though in Ada it is not. The circuit in Sem_Eval could check for
10875 -- this, but that would put the test on the main evaluation path for
10876 -- expressions.
10878 Check_Non_Static_Context (L);
10879 Check_Non_Static_Context (H);
10881 -- Check for an ambiguous range over character literals. This will
10882 -- happen with a membership test involving only literals.
10884 if Typ = Any_Character then
10885 Ambiguous_Character (L);
10886 Set_Etype (N, Any_Type);
10887 return;
10888 end if;
10890 -- If bounds are static, constant-fold them, so size computations are
10891 -- identical between front-end and back-end. Do not perform this
10892 -- transformation while analyzing generic units, as type information
10893 -- would be lost when reanalyzing the constant node in the instance.
10895 if Is_Discrete_Type (Typ) and then Expander_Active then
10896 if Is_OK_Static_Expression (L) then
10897 Fold_Uint (L, Expr_Value (L), Static => True);
10898 end if;
10900 if Is_OK_Static_Expression (H) then
10901 Fold_Uint (H, Expr_Value (H), Static => True);
10902 end if;
10903 end if;
10905 -- If we have a compile-time-known null range, we warn, because that is
10906 -- likely to be a mistake. (Dynamic null ranges make sense, but often
10907 -- compile-time-known ones do not.) Warn only if this is in a subtype
10908 -- declaration. We do this here, rather than while analyzing a subtype
10909 -- declaration, in case we decide to expand the cases. We do not want to
10910 -- warn in all cases, because some are idiomatic, such as an empty
10911 -- aggregate (1 .. 0 => <>).
10913 -- We don't warn in generics or their instances, because there might be
10914 -- some instances where the range is null, and some where it is not,
10915 -- which would lead to false alarms.
10917 if not (Inside_A_Generic or In_Instance)
10918 and then Comes_From_Source (N)
10919 and then Compile_Time_Compare
10920 (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT
10921 and then Nkind (Parent (N)) = N_Range_Constraint
10922 and then Nkind (Parent (Parent (N))) = N_Subtype_Indication
10923 and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration
10924 and then Is_OK_Static_Range (N)
10925 then
10926 Error_Msg_N ("null range??", N);
10927 end if;
10928 end Resolve_Range;
10930 --------------------------
10931 -- Resolve_Real_Literal --
10932 --------------------------
10934 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
10935 Actual_Typ : constant Entity_Id := Etype (N);
10937 begin
10938 -- Special processing for fixed-point literals to make sure that the
10939 -- value is an exact multiple of the small where this is required. We
10940 -- skip this for the universal real case, and also for generic types.
10942 if Is_Fixed_Point_Type (Typ)
10943 and then Typ /= Universal_Fixed
10944 and then Typ /= Any_Fixed
10945 and then not Is_Generic_Type (Typ)
10946 then
10947 -- We must freeze the base type to get the proper value of the small
10949 if not Is_Frozen (Base_Type (Typ)) then
10950 Freeze_Fixed_Point_Type (Base_Type (Typ));
10951 end if;
10953 declare
10954 Val : constant Ureal := Realval (N);
10955 Cintr : constant Ureal := Val / Small_Value (Base_Type (Typ));
10956 Cint : constant Uint := UR_Trunc (Cintr);
10957 Den : constant Uint := Norm_Den (Cintr);
10958 Stat : Boolean;
10960 begin
10961 -- Case of literal is not an exact multiple of the Small
10963 if Den /= 1 then
10965 -- For a source program literal for a decimal fixed-point type,
10966 -- this is statically illegal (RM 4.9(36)).
10968 if Is_Decimal_Fixed_Point_Type (Typ)
10969 and then Actual_Typ = Universal_Real
10970 and then Comes_From_Source (N)
10971 then
10972 Error_Msg_N ("value has extraneous low order digits", N);
10973 end if;
10975 -- Generate a warning if literal from source
10977 if Is_OK_Static_Expression (N)
10978 and then Warn_On_Bad_Fixed_Value
10979 then
10980 Error_Msg_N
10981 ("?b?static fixed-point value is not a multiple of Small!",
10983 end if;
10985 -- Replace literal by a value that is the exact representation
10986 -- of a value of the type, i.e. a multiple of the small value,
10987 -- by truncation, since Machine_Rounds is false for all GNAT
10988 -- fixed-point types (RM 4.9(38)).
10990 Stat := Is_OK_Static_Expression (N);
10991 Rewrite (N,
10992 Make_Real_Literal (Sloc (N),
10993 Realval => Small_Value (Typ) * Cint));
10995 Set_Is_Static_Expression (N, Stat);
10996 end if;
10998 -- In all cases, set the corresponding integer field
11000 Set_Corresponding_Integer_Value (N, Cint);
11001 end;
11002 end if;
11004 -- Now replace the actual type by the expected type as usual
11006 Set_Etype (N, Typ);
11007 Eval_Real_Literal (N);
11008 end Resolve_Real_Literal;
11010 -----------------------
11011 -- Resolve_Reference --
11012 -----------------------
11014 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
11015 P : constant Node_Id := Prefix (N);
11017 begin
11018 -- Replace general access with specific type
11020 if Ekind (Etype (N)) = E_Allocator_Type then
11021 Set_Etype (N, Base_Type (Typ));
11022 end if;
11024 Resolve (P, Designated_Type (Etype (N)));
11026 -- If we are taking the reference of a volatile entity, then treat it as
11027 -- a potential modification of this entity. This is too conservative,
11028 -- but necessary because remove side effects can cause transformations
11029 -- of normal assignments into reference sequences that otherwise fail to
11030 -- notice the modification.
11032 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
11033 Note_Possible_Modification (P, Sure => False);
11034 end if;
11035 end Resolve_Reference;
11037 --------------------------------
11038 -- Resolve_Selected_Component --
11039 --------------------------------
11041 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
11042 Comp : Entity_Id;
11043 Comp1 : Entity_Id := Empty; -- prevent junk warning
11044 P : constant Node_Id := Prefix (N);
11045 S : constant Node_Id := Selector_Name (N);
11046 T : Entity_Id := Etype (P);
11047 I : Interp_Index;
11048 I1 : Interp_Index := 0; -- prevent junk warning
11049 It : Interp;
11050 It1 : Interp;
11051 Found : Boolean;
11053 function Init_Component return Boolean;
11054 -- Check whether this is the initialization of a component within an
11055 -- init proc (by assignment or call to another init proc). If true,
11056 -- there is no need for a discriminant check.
11058 --------------------
11059 -- Init_Component --
11060 --------------------
11062 function Init_Component return Boolean is
11063 begin
11064 return Inside_Init_Proc
11065 and then Nkind (Prefix (N)) = N_Identifier
11066 and then Chars (Prefix (N)) = Name_uInit
11067 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
11068 end Init_Component;
11070 -- Start of processing for Resolve_Selected_Component
11072 begin
11073 if Is_Overloaded (P) then
11075 -- Use the context type to select the prefix that has a selector
11076 -- of the correct name and type.
11078 Found := False;
11079 Get_First_Interp (P, I, It);
11081 Search : while Present (It.Typ) loop
11082 if Is_Access_Type (It.Typ) then
11083 T := Designated_Type (It.Typ);
11084 else
11085 T := It.Typ;
11086 end if;
11088 -- Locate selected component. For a private prefix the selector
11089 -- can denote a discriminant.
11091 if Is_Record_Type (T) or else Is_Private_Type (T) then
11093 -- The visible components of a class-wide type are those of
11094 -- the root type.
11096 if Is_Class_Wide_Type (T) then
11097 T := Etype (T);
11098 end if;
11100 Comp := First_Entity (T);
11101 while Present (Comp) loop
11102 if Chars (Comp) = Chars (S)
11103 and then Covers (Typ, Etype (Comp))
11104 then
11105 if not Found then
11106 Found := True;
11107 I1 := I;
11108 It1 := It;
11109 Comp1 := Comp;
11111 else
11112 It := Disambiguate (P, I1, I, Any_Type);
11114 if It = No_Interp then
11115 Error_Msg_N
11116 ("ambiguous prefix for selected component", N);
11117 Set_Etype (N, Typ);
11118 return;
11120 else
11121 It1 := It;
11123 -- There may be an implicit dereference. Retrieve
11124 -- designated record type.
11126 if Is_Access_Type (It1.Typ) then
11127 T := Designated_Type (It1.Typ);
11128 else
11129 T := It1.Typ;
11130 end if;
11132 if Scope (Comp1) /= T then
11134 -- Resolution chooses the new interpretation.
11135 -- Find the component with the right name.
11137 Comp1 := First_Entity (T);
11138 while Present (Comp1)
11139 and then Chars (Comp1) /= Chars (S)
11140 loop
11141 Next_Entity (Comp1);
11142 end loop;
11143 end if;
11145 exit Search;
11146 end if;
11147 end if;
11148 end if;
11150 Next_Entity (Comp);
11151 end loop;
11152 end if;
11154 Get_Next_Interp (I, It);
11155 end loop Search;
11157 -- There must be a legal interpretation at this point
11159 pragma Assert (Found);
11160 Resolve (P, It1.Typ);
11162 -- In general the expected type is the type of the context, not the
11163 -- type of the candidate selected component.
11165 Set_Etype (N, Typ);
11166 Set_Entity_With_Checks (S, Comp1);
11168 -- The type of the context and that of the component are
11169 -- compatible and in general identical, but if they are anonymous
11170 -- access-to-subprogram types, the relevant type is that of the
11171 -- component. This matters in Unnest_Subprograms mode, where the
11172 -- relevant context is the one in which the type is declared, not
11173 -- the point of use. This determines what activation record to use.
11175 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
11176 Set_Etype (N, Etype (Comp1));
11178 -- When the type of the component is an access to a class-wide type
11179 -- the relevant type is that of the component (since in such case we
11180 -- may need to generate implicit type conversions or dispatching
11181 -- calls).
11183 elsif Is_Access_Type (Typ)
11184 and then not Is_Class_Wide_Type (Designated_Type (Typ))
11185 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
11186 then
11187 Set_Etype (N, Etype (Comp1));
11188 end if;
11190 else
11191 -- Resolve prefix with its type
11193 Resolve (P, T);
11194 end if;
11196 -- Generate cross-reference. We needed to wait until full overloading
11197 -- resolution was complete to do this, since otherwise we can't tell if
11198 -- we are an lvalue or not.
11200 if Known_To_Be_Assigned (N) then
11201 Generate_Reference (Entity (S), S, 'm');
11202 else
11203 Generate_Reference (Entity (S), S, 'r');
11204 end if;
11206 -- If the prefix's type is an access type, get to the real record type.
11207 -- Note: we do not apply an access check because an explicit dereference
11208 -- will be introduced later, and the check will happen there.
11210 if Is_Access_Type (Etype (P)) then
11211 T := Implicitly_Designated_Type (Etype (P));
11212 Check_Fully_Declared_Prefix (T, P);
11214 else
11215 T := Etype (P);
11216 end if;
11218 -- Set flag for expander if discriminant check required on a component
11219 -- appearing within a variant.
11221 if Has_Discriminants (T)
11222 and then Ekind (Entity (S)) = E_Component
11223 and then Present (Original_Record_Component (Entity (S)))
11224 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
11225 and then
11226 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
11227 and then not Discriminant_Checks_Suppressed (T)
11228 and then not Init_Component
11229 then
11230 Set_Do_Discriminant_Check (N);
11231 end if;
11233 if Ekind (Entity (S)) = E_Void then
11234 Error_Msg_N ("premature use of component", S);
11235 end if;
11237 -- If the prefix is a record conversion, this may be a renamed
11238 -- discriminant whose bounds differ from those of the original
11239 -- one, so we must ensure that a range check is performed.
11241 if Nkind (P) = N_Type_Conversion
11242 and then Ekind (Entity (S)) = E_Discriminant
11243 and then Is_Discrete_Type (Typ)
11244 then
11245 Set_Etype (N, Base_Type (Typ));
11246 end if;
11248 -- Eval_Selected_Component may e.g. fold statically known discriminants.
11250 Eval_Selected_Component (N);
11252 if Nkind (N) = N_Selected_Component then
11254 -- If the record type is atomic and the component is not, then this
11255 -- is worth a warning before Ada 2022, since we have a situation
11256 -- where the access to the component may cause extra read/writes of
11257 -- the atomic object, or partial word accesses, both of which may be
11258 -- unexpected.
11260 if Is_Atomic_Ref_With_Address (N)
11261 and then not Is_Atomic (Entity (S))
11262 and then not Is_Atomic (Etype (Entity (S)))
11263 and then Ada_Version < Ada_2022
11264 then
11265 Error_Msg_N
11266 ("??access to non-atomic component of atomic record",
11267 Prefix (N));
11268 Error_Msg_N
11269 ("\??may cause unexpected accesses to atomic object",
11270 Prefix (N));
11271 end if;
11273 Resolve_Implicit_Dereference (Prefix (N));
11274 Analyze_Dimension (N);
11275 end if;
11276 end Resolve_Selected_Component;
11278 -------------------
11279 -- Resolve_Shift --
11280 -------------------
11282 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11283 B_Typ : constant Entity_Id := Base_Type (Typ);
11284 L : constant Node_Id := Left_Opnd (N);
11285 R : constant Node_Id := Right_Opnd (N);
11287 begin
11288 -- We do the resolution using the base type, because intermediate values
11289 -- in expressions always are of the base type, not a subtype of it.
11291 Resolve (L, B_Typ);
11292 Resolve (R, Standard_Natural);
11294 Check_Unset_Reference (L);
11295 Check_Unset_Reference (R);
11297 Set_Etype (N, B_Typ);
11298 Generate_Operator_Reference (N, B_Typ);
11299 Eval_Shift (N);
11300 end Resolve_Shift;
11302 ---------------------------
11303 -- Resolve_Short_Circuit --
11304 ---------------------------
11306 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11307 B_Typ : constant Entity_Id := Base_Type (Typ);
11308 L : constant Node_Id := Left_Opnd (N);
11309 R : constant Node_Id := Right_Opnd (N);
11311 begin
11312 -- Ensure all actions associated with the left operand (e.g.
11313 -- finalization of transient objects) are fully evaluated locally within
11314 -- an expression with actions. This is particularly helpful for coverage
11315 -- analysis. However this should not happen in generics or if option
11316 -- Minimize_Expression_With_Actions is set.
11318 if Expander_Active and not Minimize_Expression_With_Actions then
11319 declare
11320 Reloc_L : constant Node_Id := Relocate_Node (L);
11321 begin
11322 Save_Interps (Old_N => L, New_N => Reloc_L);
11324 Rewrite (L,
11325 Make_Expression_With_Actions (Sloc (L),
11326 Actions => New_List,
11327 Expression => Reloc_L));
11329 -- Set Comes_From_Source on L to preserve warnings for unset
11330 -- reference.
11332 Preserve_Comes_From_Source (L, Reloc_L);
11333 end;
11334 end if;
11336 Resolve (L, B_Typ);
11337 Resolve (R, B_Typ);
11339 -- Check for issuing warning for always False assert/check, this happens
11340 -- when assertions are turned off, in which case the pragma Assert/Check
11341 -- was transformed into:
11343 -- if False and then <condition> then ...
11345 -- and we detect this pattern
11347 if Warn_On_Assertion_Failure
11348 and then Is_Entity_Name (R)
11349 and then Entity (R) = Standard_False
11350 and then Nkind (Parent (N)) = N_If_Statement
11351 and then Nkind (N) = N_And_Then
11352 and then Is_Entity_Name (L)
11353 and then Entity (L) = Standard_False
11354 then
11355 declare
11356 Orig : constant Node_Id := Original_Node (Parent (N));
11358 begin
11359 -- Special handling of Asssert pragma
11361 if Nkind (Orig) = N_Pragma
11362 and then Pragma_Name (Orig) = Name_Assert
11363 then
11364 declare
11365 Expr : constant Node_Id :=
11366 Original_Node
11367 (Expression
11368 (First (Pragma_Argument_Associations (Orig))));
11370 begin
11371 -- Don't warn if original condition is explicit False,
11372 -- since obviously the failure is expected in this case.
11374 if Is_Entity_Name (Expr)
11375 and then Entity (Expr) = Standard_False
11376 then
11377 null;
11379 -- Issue warning. We do not want the deletion of the
11380 -- IF/AND-THEN to take this message with it. We achieve this
11381 -- by making sure that the expanded code points to the Sloc
11382 -- of the expression, not the original pragma.
11384 else
11385 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11386 -- The source location of the expression is not usually
11387 -- the best choice here. For example, it gets located on
11388 -- the last AND keyword in a chain of boolean expressiond
11389 -- AND'ed together. It is best to put the message on the
11390 -- first character of the assertion, which is the effect
11391 -- of the First_Node call here.
11393 Error_Msg_F
11394 ("?.a?assertion would fail at run time!",
11395 Expression
11396 (First (Pragma_Argument_Associations (Orig))));
11397 end if;
11398 end;
11400 -- Similar processing for Check pragma
11402 elsif Nkind (Orig) = N_Pragma
11403 and then Pragma_Name (Orig) = Name_Check
11404 then
11405 -- Don't want to warn if original condition is explicit False
11407 declare
11408 Expr : constant Node_Id :=
11409 Original_Node
11410 (Expression
11411 (Next (First (Pragma_Argument_Associations (Orig)))));
11412 begin
11413 if Is_Entity_Name (Expr)
11414 and then Entity (Expr) = Standard_False
11415 then
11416 null;
11418 -- Post warning
11420 else
11421 -- Again use Error_Msg_F rather than Error_Msg_N, see
11422 -- comment above for an explanation of why we do this.
11424 Error_Msg_F
11425 ("?.a?check would fail at run time!",
11426 Expression
11427 (Last (Pragma_Argument_Associations (Orig))));
11428 end if;
11429 end;
11430 end if;
11431 end;
11432 end if;
11434 -- Continue with processing of short circuit
11436 Check_Unset_Reference (L);
11437 Check_Unset_Reference (R);
11439 Set_Etype (N, B_Typ);
11440 Eval_Short_Circuit (N);
11441 end Resolve_Short_Circuit;
11443 -------------------
11444 -- Resolve_Slice --
11445 -------------------
11447 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11448 Drange : constant Node_Id := Discrete_Range (N);
11449 Pref : constant Node_Id := Prefix (N);
11450 Array_Type : Entity_Id := Empty;
11451 Dexpr : Node_Id := Empty;
11452 Index_Type : Entity_Id;
11454 begin
11455 if Is_Overloaded (Pref) then
11457 -- Use the context type to select the prefix that yields the correct
11458 -- array type.
11460 declare
11461 I : Interp_Index;
11462 I1 : Interp_Index := 0;
11463 It : Interp;
11464 Found : Boolean := False;
11466 begin
11467 Get_First_Interp (Pref, I, It);
11468 while Present (It.Typ) loop
11469 if (Is_Array_Type (It.Typ)
11470 and then Covers (Typ, It.Typ))
11471 or else (Is_Access_Type (It.Typ)
11472 and then Is_Array_Type (Designated_Type (It.Typ))
11473 and then Covers (Typ, Designated_Type (It.Typ)))
11474 then
11475 if Found then
11476 It := Disambiguate (Pref, I1, I, Any_Type);
11478 if It = No_Interp then
11479 Error_Msg_N ("ambiguous prefix for slicing", N);
11480 Set_Etype (N, Typ);
11481 return;
11482 else
11483 Found := True;
11484 Array_Type := It.Typ;
11485 I1 := I;
11486 end if;
11487 else
11488 Found := True;
11489 Array_Type := It.Typ;
11490 I1 := I;
11491 end if;
11492 end if;
11494 Get_Next_Interp (I, It);
11495 end loop;
11496 end;
11498 else
11499 Array_Type := Etype (Pref);
11500 end if;
11502 Resolve (Pref, Array_Type);
11504 -- If the prefix's type is an access type, get to the real array type.
11505 -- Note: we do not apply an access check because an explicit dereference
11506 -- will be introduced later, and the check will happen there.
11508 if Is_Access_Type (Array_Type) then
11509 Array_Type := Implicitly_Designated_Type (Array_Type);
11511 -- If the prefix is an access to an unconstrained array, we must use
11512 -- the actual subtype of the object to perform the index checks. The
11513 -- object denoted by the prefix is implicit in the node, so we build
11514 -- an explicit representation for it in order to compute the actual
11515 -- subtype.
11517 if not Is_Constrained (Array_Type) then
11518 Remove_Side_Effects (Pref);
11520 declare
11521 Obj : constant Node_Id :=
11522 Make_Explicit_Dereference (Sloc (N),
11523 Prefix => New_Copy_Tree (Pref));
11524 begin
11525 Set_Etype (Obj, Array_Type);
11526 Set_Parent (Obj, Parent (N));
11527 Array_Type := Get_Actual_Subtype (Obj);
11528 end;
11529 end if;
11531 -- In CodePeer mode the attribute Image is not expanded, so when it
11532 -- acts as a prefix of a slice, we handle it like a call to function
11533 -- returning an unconstrained string. Same for the Wide variants of
11534 -- attribute Image.
11536 elsif Is_Entity_Name (Pref)
11537 or else Nkind (Pref) = N_Explicit_Dereference
11538 or else (Nkind (Pref) = N_Function_Call
11539 and then not Is_Constrained (Etype (Pref)))
11540 or else (CodePeer_Mode
11541 and then Nkind (Pref) = N_Attribute_Reference
11542 and then Attribute_Name (Pref) in Name_Image
11543 | Name_Wide_Image
11544 | Name_Wide_Wide_Image)
11545 then
11546 Array_Type := Get_Actual_Subtype (Pref);
11548 -- If the name is a selected component that depends on discriminants,
11549 -- build an actual subtype for it. This can happen only when the name
11550 -- itself is overloaded; otherwise the actual subtype is created when
11551 -- the selected component is analyzed.
11553 elsif Nkind (Pref) = N_Selected_Component
11554 and then Full_Analysis
11555 and then Depends_On_Discriminant (First_Index (Array_Type))
11556 then
11557 declare
11558 Act_Decl : constant Node_Id :=
11559 Build_Actual_Subtype_Of_Component (Array_Type, Pref);
11560 begin
11561 Insert_Action (N, Act_Decl);
11562 Array_Type := Defining_Identifier (Act_Decl);
11563 end;
11565 -- Maybe this should just be "else", instead of checking for the
11566 -- specific case of slice??? This is needed for the case where the
11567 -- prefix is an Image attribute, which gets expanded to a slice, and so
11568 -- has a constrained subtype which we want to use for the slice range
11569 -- check applied below (the range check won't get done if the
11570 -- unconstrained subtype of the 'Image is used).
11572 elsif Nkind (Pref) = N_Slice then
11573 Array_Type := Etype (Pref);
11574 end if;
11576 -- Obtain the type of the array index
11578 if Ekind (Array_Type) = E_String_Literal_Subtype then
11579 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11580 else
11581 Index_Type := Etype (First_Index (Array_Type));
11582 end if;
11584 -- If name was overloaded, set slice type correctly now
11586 Set_Etype (N, Array_Type);
11588 -- Handle the generation of a range check that compares the array index
11589 -- against the discrete_range. The check is not applied to internally
11590 -- built nodes associated with the expansion of dispatch tables. Check
11591 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11592 -- the unit.
11594 if Tagged_Type_Expansion
11595 and then RTU_Loaded (Ada_Tags)
11596 and then Nkind (Pref) = N_Selected_Component
11597 and then Present (Entity (Selector_Name (Pref)))
11598 and then Entity (Selector_Name (Pref)) =
11599 RTE_Record_Component (RE_Prims_Ptr)
11600 then
11601 null;
11603 -- The discrete_range is specified by a subtype name. Create an
11604 -- equivalent range attribute, apply checks to this attribute, but
11605 -- insert them into the range expression of the slice itself.
11607 elsif Is_Entity_Name (Drange) then
11608 Dexpr :=
11609 Make_Attribute_Reference
11610 (Sloc (Drange),
11611 Prefix =>
11612 New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
11613 Attribute_Name => Name_Range);
11615 Analyze_And_Resolve (Dexpr, Etype (Drange));
11617 elsif Nkind (Drange) = N_Subtype_Indication then
11618 Dexpr := Range_Expression (Constraint (Drange));
11620 -- The discrete_range is a regular range (or a range attribute, which
11621 -- will be resolved into a regular range). Resolve the bounds and remove
11622 -- their side effects.
11624 else
11625 Resolve (Drange, Base_Type (Index_Type));
11627 if Nkind (Drange) = N_Range then
11628 Force_Evaluation (Low_Bound (Drange));
11629 Force_Evaluation (High_Bound (Drange));
11631 Dexpr := Drange;
11632 end if;
11633 end if;
11635 if Present (Dexpr) then
11636 Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
11637 end if;
11639 Set_Slice_Subtype (N);
11641 -- Check bad use of type with predicates
11643 declare
11644 Subt : Entity_Id;
11646 begin
11647 if Nkind (Drange) = N_Subtype_Indication
11648 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11649 then
11650 Subt := Entity (Subtype_Mark (Drange));
11651 else
11652 Subt := Etype (Drange);
11653 end if;
11655 if Has_Predicates (Subt) then
11656 Bad_Predicated_Subtype_Use
11657 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11658 end if;
11659 end;
11661 -- Otherwise here is where we check suspicious indexes
11663 if Nkind (Drange) = N_Range then
11664 Warn_On_Suspicious_Index (Pref, Low_Bound (Drange));
11665 Warn_On_Suspicious_Index (Pref, High_Bound (Drange));
11666 end if;
11668 Resolve_Implicit_Dereference (Pref);
11669 Analyze_Dimension (N);
11670 Eval_Slice (N);
11671 end Resolve_Slice;
11673 ----------------------------
11674 -- Resolve_String_Literal --
11675 ----------------------------
11677 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11678 C_Typ : constant Entity_Id := Component_Type (Typ);
11679 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11680 Loc : constant Source_Ptr := Sloc (N);
11681 Str : constant String_Id := Strval (N);
11682 Strlen : constant Nat := String_Length (Str);
11683 Subtype_Id : Entity_Id;
11684 Need_Check : Boolean;
11686 begin
11687 -- For a string appearing in a concatenation, defer creation of the
11688 -- string_literal_subtype until the end of the resolution of the
11689 -- concatenation, because the literal may be constant-folded away. This
11690 -- is a useful optimization for long concatenation expressions.
11692 -- If the string is an aggregate built for a single character (which
11693 -- happens in a non-static context) or a is null string to which special
11694 -- checks may apply, we build the subtype. Wide strings must also get a
11695 -- string subtype if they come from a one character aggregate. Strings
11696 -- generated by attributes might be static, but it is often hard to
11697 -- determine whether the enclosing context is static, so we generate
11698 -- subtypes for them as well, thus losing some rarer optimizations ???
11699 -- Same for strings that come from a static conversion.
11701 Need_Check :=
11702 (Strlen = 0 and then Typ /= Standard_String)
11703 or else Nkind (Parent (N)) /= N_Op_Concat
11704 or else (N /= Left_Opnd (Parent (N))
11705 and then N /= Right_Opnd (Parent (N)))
11706 or else ((Typ = Standard_Wide_String
11707 or else Typ = Standard_Wide_Wide_String)
11708 and then Nkind (Original_Node (N)) /= N_String_Literal);
11710 -- If the resolving type is itself a string literal subtype, we can just
11711 -- reuse it, since there is no point in creating another.
11713 if Ekind (Typ) = E_String_Literal_Subtype then
11714 Subtype_Id := Typ;
11716 elsif Nkind (Parent (N)) = N_Op_Concat
11717 and then not Need_Check
11718 and then Nkind (Original_Node (N)) not in N_Character_Literal
11719 | N_Attribute_Reference
11720 | N_Qualified_Expression
11721 | N_Type_Conversion
11722 then
11723 Subtype_Id := Typ;
11725 -- Do not generate a string literal subtype for the default expression
11726 -- of a formal parameter in GNATprove mode. This is because the string
11727 -- subtype is associated with the freezing actions of the subprogram,
11728 -- however freezing is disabled in GNATprove mode and as a result the
11729 -- subtype is unavailable.
11731 elsif GNATprove_Mode
11732 and then Nkind (Parent (N)) = N_Parameter_Specification
11733 then
11734 Subtype_Id := Typ;
11736 -- Otherwise we must create a string literal subtype. Note that the
11737 -- whole idea of string literal subtypes is simply to avoid the need
11738 -- for building a full fledged array subtype for each literal.
11740 else
11741 Set_String_Literal_Subtype (N, Typ);
11742 Subtype_Id := Etype (N);
11743 end if;
11745 if Nkind (Parent (N)) /= N_Op_Concat
11746 or else Need_Check
11747 then
11748 Set_Etype (N, Subtype_Id);
11749 Eval_String_Literal (N);
11750 end if;
11752 if Is_Limited_Composite (Typ)
11753 or else Is_Private_Composite (Typ)
11754 then
11755 Error_Msg_N ("string literal not available for private array", N);
11756 Set_Etype (N, Any_Type);
11757 return;
11758 end if;
11760 -- The validity of a null string has been checked in the call to
11761 -- Eval_String_Literal.
11763 if Strlen = 0 then
11764 return;
11766 -- Always accept string literal with component type Any_Character, which
11767 -- occurs in error situations and in comparisons of literals, both of
11768 -- which should accept all literals.
11770 elsif R_Typ = Any_Character then
11771 return;
11773 -- If the type is bit-packed, then we always transform the string
11774 -- literal into a full fledged aggregate.
11776 elsif Is_Bit_Packed_Array (Typ) then
11777 null;
11779 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11781 else
11782 -- For Standard.Wide_Wide_String, or any other type whose component
11783 -- type is Standard.Wide_Wide_Character, we know that all the
11784 -- characters in the string must be acceptable, since the parser
11785 -- accepted the characters as valid character literals.
11787 if R_Typ = Standard_Wide_Wide_Character then
11788 null;
11790 -- For the case of Standard.String, or any other type whose component
11791 -- type is Standard.Character, we must make sure that there are no
11792 -- wide characters in the string, i.e. that it is entirely composed
11793 -- of characters in range of type Character.
11795 -- If the string literal is the result of a static concatenation, the
11796 -- test has already been performed on the components, and need not be
11797 -- repeated.
11799 elsif R_Typ = Standard_Character
11800 and then Nkind (Original_Node (N)) /= N_Op_Concat
11801 then
11802 for J in 1 .. Strlen loop
11803 if not In_Character_Range (Get_String_Char (Str, J)) then
11805 -- If we are out of range, post error. This is one of the
11806 -- very few places that we place the flag in the middle of
11807 -- a token, right under the offending wide character. Not
11808 -- quite clear if this is right wrt wide character encoding
11809 -- sequences, but it's only an error message.
11811 Error_Msg
11812 ("literal out of range of type Standard.Character",
11813 Loc + Source_Ptr (J));
11814 return;
11815 end if;
11816 end loop;
11818 -- For the case of Standard.Wide_String, or any other type whose
11819 -- component type is Standard.Wide_Character, we must make sure that
11820 -- there are no wide characters in the string, i.e. that it is
11821 -- entirely composed of characters in range of type Wide_Character.
11823 -- If the string literal is the result of a static concatenation,
11824 -- the test has already been performed on the components, and need
11825 -- not be repeated.
11827 elsif R_Typ = Standard_Wide_Character
11828 and then Nkind (Original_Node (N)) /= N_Op_Concat
11829 then
11830 for J in 1 .. Strlen loop
11831 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11833 -- If we are out of range, post error. This is one of the
11834 -- very few places that we place the flag in the middle of
11835 -- a token, right under the offending wide character.
11837 -- This is not quite right, because characters in general
11838 -- will take more than one character position ???
11840 Error_Msg
11841 ("literal out of range of type Standard.Wide_Character",
11842 Loc + Source_Ptr (J));
11843 return;
11844 end if;
11845 end loop;
11847 -- If the root type is not a standard character, then we will convert
11848 -- the string into an aggregate and will let the aggregate code do
11849 -- the checking. Standard Wide_Wide_Character is also OK here.
11851 else
11852 null;
11853 end if;
11855 -- See if the component type of the array corresponding to the string
11856 -- has compile time known bounds. If yes we can directly check
11857 -- whether the evaluation of the string will raise constraint error.
11858 -- Otherwise we need to transform the string literal into the
11859 -- corresponding character aggregate and let the aggregate code do
11860 -- the checking. We use the same transformation if the component
11861 -- type has a static predicate, which will be applied to each
11862 -- character when the aggregate is resolved.
11864 if Is_Standard_Character_Type (R_Typ) then
11866 -- Check for the case of full range, where we are definitely OK
11868 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
11869 return;
11870 end if;
11872 -- Here the range is not the complete base type range, so check
11874 declare
11875 Comp_Typ_Lo : constant Node_Id :=
11876 Type_Low_Bound (Component_Type (Typ));
11877 Comp_Typ_Hi : constant Node_Id :=
11878 Type_High_Bound (Component_Type (Typ));
11880 Char_Val : Uint;
11882 begin
11883 if Compile_Time_Known_Value (Comp_Typ_Lo)
11884 and then Compile_Time_Known_Value (Comp_Typ_Hi)
11885 then
11886 for J in 1 .. Strlen loop
11887 Char_Val := UI_From_CC (Get_String_Char (Str, J));
11889 if Char_Val < Expr_Value (Comp_Typ_Lo)
11890 or else Char_Val > Expr_Value (Comp_Typ_Hi)
11891 then
11892 Apply_Compile_Time_Constraint_Error
11893 (N, "character out of range??",
11894 CE_Range_Check_Failed,
11895 Loc => Loc + Source_Ptr (J));
11896 end if;
11897 end loop;
11899 if not Has_Static_Predicate (C_Typ) then
11900 return;
11901 end if;
11902 end if;
11903 end;
11904 end if;
11905 end if;
11907 -- If we got here we meed to transform the string literal into the
11908 -- equivalent qualified positional array aggregate. This is rather
11909 -- heavy artillery for this situation, but it is hard work to avoid.
11911 declare
11912 Lits : constant List_Id := New_List;
11913 P : Source_Ptr := Loc + 1;
11914 C : Char_Code;
11916 begin
11917 -- Build the character literals, we give them source locations that
11918 -- correspond to the string positions, which is a bit tricky given
11919 -- the possible presence of wide character escape sequences.
11921 for J in 1 .. Strlen loop
11922 C := Get_String_Char (Str, J);
11923 Set_Character_Literal_Name (C);
11925 Append_To (Lits,
11926 Make_Character_Literal (P,
11927 Chars => Name_Find,
11928 Char_Literal_Value => UI_From_CC (C)));
11930 if In_Character_Range (C) then
11931 P := P + 1;
11933 -- Should we have a call to Skip_Wide here ???
11935 -- ??? else
11936 -- Skip_Wide (P);
11938 end if;
11939 end loop;
11941 Rewrite (N,
11942 Make_Qualified_Expression (Loc,
11943 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
11944 Expression =>
11945 Make_Aggregate (Loc, Expressions => Lits)));
11947 Analyze_And_Resolve (N, Typ);
11948 end;
11949 end Resolve_String_Literal;
11951 -------------------------
11952 -- Resolve_Target_Name --
11953 -------------------------
11955 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
11956 begin
11957 Set_Etype (N, Typ);
11958 end Resolve_Target_Name;
11960 -----------------------------
11961 -- Resolve_Type_Conversion --
11962 -----------------------------
11964 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
11965 Conv_OK : constant Boolean := Conversion_OK (N);
11966 Operand : constant Node_Id := Expression (N);
11967 Operand_Typ : constant Entity_Id := Etype (Operand);
11968 Target_Typ : constant Entity_Id := Etype (N);
11969 Rop : Node_Id;
11970 Orig_N : Node_Id;
11971 Orig_T : Node_Id;
11973 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
11974 -- Set to False to suppress cases where we want to suppress the test
11975 -- for redundancy to avoid possible false positives on this warning.
11977 begin
11978 if not Conv_OK
11979 and then not Valid_Conversion (N, Target_Typ, Operand)
11980 then
11981 return;
11982 end if;
11984 -- If the Operand Etype is Universal_Fixed, then the conversion is
11985 -- never redundant. We need this check because by the time we have
11986 -- finished the rather complex transformation, the conversion looks
11987 -- redundant when it is not.
11989 if Operand_Typ = Universal_Fixed then
11990 Test_Redundant := False;
11992 -- If the operand is marked as Any_Fixed, then special processing is
11993 -- required. This is also a case where we suppress the test for a
11994 -- redundant conversion, since most certainly it is not redundant.
11996 elsif Operand_Typ = Any_Fixed then
11997 Test_Redundant := False;
11999 -- Mixed-mode operation involving a literal. Context must be a fixed
12000 -- type which is applied to the literal subsequently.
12002 -- Multiplication and division involving two fixed type operands must
12003 -- yield a universal real because the result is computed in arbitrary
12004 -- precision.
12006 if Is_Fixed_Point_Type (Typ)
12007 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
12008 and then Etype (Left_Opnd (Operand)) = Any_Fixed
12009 and then Etype (Right_Opnd (Operand)) = Any_Fixed
12010 then
12011 Set_Etype (Operand, Universal_Real);
12013 elsif Is_Numeric_Type (Typ)
12014 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
12015 and then (Etype (Right_Opnd (Operand)) = Universal_Real
12016 or else
12017 Etype (Left_Opnd (Operand)) = Universal_Real)
12018 then
12019 -- Return if expression is ambiguous
12021 if Unique_Fixed_Point_Type (N) = Any_Type then
12022 return;
12024 -- If nothing else, the available fixed type is Duration
12026 else
12027 Set_Etype (Operand, Standard_Duration);
12028 end if;
12030 -- Resolve the real operand with largest available precision
12032 if Etype (Right_Opnd (Operand)) = Universal_Real then
12033 Rop := New_Copy_Tree (Right_Opnd (Operand));
12034 else
12035 Rop := New_Copy_Tree (Left_Opnd (Operand));
12036 end if;
12038 Resolve (Rop, Universal_Real);
12040 -- If the operand is a literal (it could be a non-static and
12041 -- illegal exponentiation) check whether the use of Duration
12042 -- is potentially inaccurate.
12044 if Nkind (Rop) = N_Real_Literal
12045 and then Realval (Rop) /= Ureal_0
12046 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
12047 then
12048 Error_Msg_N
12049 ("??universal real operand can only "
12050 & "be interpreted as Duration!", Rop);
12051 Error_Msg_N
12052 ("\??precision will be lost in the conversion!", Rop);
12053 end if;
12055 elsif Is_Numeric_Type (Typ)
12056 and then Nkind (Operand) in N_Op
12057 and then Unique_Fixed_Point_Type (N) /= Any_Type
12058 then
12059 Set_Etype (Operand, Standard_Duration);
12061 else
12062 Error_Msg_N ("invalid context for mixed mode operation", N);
12063 Set_Etype (Operand, Any_Type);
12064 return;
12065 end if;
12066 end if;
12068 Resolve (Operand);
12070 Analyze_Dimension (N);
12072 -- Note: we do the Eval_Type_Conversion call before applying the
12073 -- required checks for a subtype conversion. This is important, since
12074 -- both are prepared under certain circumstances to change the type
12075 -- conversion to a constraint error node, but in the case of
12076 -- Eval_Type_Conversion this may reflect an illegality in the static
12077 -- case, and we would miss the illegality (getting only a warning
12078 -- message), if we applied the type conversion checks first.
12080 Eval_Type_Conversion (N);
12082 -- Even when evaluation is not possible, we may be able to simplify the
12083 -- conversion or its expression. This needs to be done before applying
12084 -- checks, since otherwise the checks may use the original expression
12085 -- and defeat the simplifications. This is specifically the case for
12086 -- elimination of the floating-point Truncation attribute in
12087 -- float-to-int conversions.
12089 Simplify_Type_Conversion (N);
12091 -- If after evaluation we still have a type conversion, then we may need
12092 -- to apply checks required for a subtype conversion. But skip them if
12093 -- universal fixed operands are involved, since range checks are handled
12094 -- separately for these cases, after the expansion done by Exp_Fixd.
12096 if Nkind (N) = N_Type_Conversion
12097 and then not Is_Generic_Type (Root_Type (Target_Typ))
12098 and then Target_Typ /= Universal_Fixed
12099 and then Etype (Operand) /= Universal_Fixed
12100 then
12101 Apply_Type_Conversion_Checks (N);
12102 end if;
12104 -- Issue warning for conversion of simple object to its own type. We
12105 -- have to test the original nodes, since they may have been rewritten
12106 -- by various optimizations.
12108 Orig_N := Original_Node (N);
12110 -- Here we test for a redundant conversion if the warning mode is
12111 -- active (and was not locally reset), and we have a type conversion
12112 -- from source not appearing in a generic instance.
12114 if Test_Redundant
12115 and then Nkind (Orig_N) = N_Type_Conversion
12116 and then Comes_From_Source (Orig_N)
12117 and then not In_Instance
12118 then
12119 Orig_N := Original_Node (Expression (Orig_N));
12120 Orig_T := Target_Typ;
12122 -- If the node is part of a larger expression, the Target_Type
12123 -- may not be the original type of the node if the context is a
12124 -- condition. Recover original type to see if conversion is needed.
12126 if Is_Boolean_Type (Orig_T)
12127 and then Nkind (Parent (N)) in N_Op
12128 then
12129 Orig_T := Etype (Parent (N));
12130 end if;
12132 -- If we have an entity name, then give the warning if the entity
12133 -- is the right type, or if it is a loop parameter covered by the
12134 -- original type (that's needed because loop parameters have an
12135 -- odd subtype coming from the bounds).
12137 if (Is_Entity_Name (Orig_N)
12138 and then Present (Entity (Orig_N))
12139 and then
12140 (Etype (Entity (Orig_N)) = Orig_T
12141 or else
12142 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
12143 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
12145 -- If not an entity, then type of expression must match
12147 or else Etype (Orig_N) = Orig_T
12148 then
12149 -- One more check, do not give warning if the analyzed conversion
12150 -- has an expression with non-static bounds, and the bounds of the
12151 -- target are static. This avoids junk warnings in cases where the
12152 -- conversion is necessary to establish staticness, for example in
12153 -- a case statement.
12155 if not Is_OK_Static_Subtype (Operand_Typ)
12156 and then Is_OK_Static_Subtype (Target_Typ)
12157 then
12158 null;
12160 -- Finally, if this type conversion occurs in a context requiring
12161 -- a prefix, and the expression is a qualified expression then the
12162 -- type conversion is not redundant, since a qualified expression
12163 -- is not a prefix, whereas a type conversion is. For example, "X
12164 -- := T'(Funx(...)).Y;" is illegal because a selected component
12165 -- requires a prefix, but a type conversion makes it legal: "X :=
12166 -- T(T'(Funx(...))).Y;"
12168 -- In Ada 2012, a qualified expression is a name, so this idiom is
12169 -- no longer needed, but we still suppress the warning because it
12170 -- seems unfriendly for warnings to pop up when you switch to the
12171 -- newer language version.
12173 elsif Nkind (Orig_N) = N_Qualified_Expression
12174 and then Nkind (Parent (N)) in N_Attribute_Reference
12175 | N_Indexed_Component
12176 | N_Selected_Component
12177 | N_Slice
12178 | N_Explicit_Dereference
12179 then
12180 null;
12182 -- Never warn on conversion to Long_Long_Integer'Base since
12183 -- that is most likely an artifact of the extended overflow
12184 -- checking and comes from complex expanded code.
12186 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
12187 null;
12189 -- Here we give the redundant conversion warning. If it is an
12190 -- entity, give the name of the entity in the message. If not,
12191 -- just mention the expression.
12193 else
12194 if Is_Entity_Name (Orig_N) then
12195 Error_Msg_Node_2 := Orig_T;
12196 Error_Msg_NE -- CODEFIX
12197 ("?r?redundant conversion, & is of type &!",
12198 N, Entity (Orig_N));
12199 else
12200 Error_Msg_NE
12201 ("?r?redundant conversion, expression is of type&!",
12202 N, Orig_T);
12203 end if;
12204 end if;
12205 end if;
12206 end if;
12208 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
12209 -- No need to perform any interface conversion if the type of the
12210 -- expression coincides with the target type.
12212 if Ada_Version >= Ada_2005
12213 and then Expander_Active
12214 and then Operand_Typ /= Target_Typ
12215 then
12216 declare
12217 Opnd : Entity_Id := Operand_Typ;
12218 Target : Entity_Id := Target_Typ;
12220 begin
12221 -- If the type of the operand is a limited view, use nonlimited
12222 -- view when available. If it is a class-wide type, recover the
12223 -- class-wide type of the nonlimited view.
12225 if From_Limited_With (Opnd)
12226 and then Has_Non_Limited_View (Opnd)
12227 then
12228 Opnd := Non_Limited_View (Opnd);
12229 Set_Etype (Expression (N), Opnd);
12230 end if;
12232 -- It seems that Non_Limited_View should also be applied for
12233 -- Target when it has a limited view, but that leads to missing
12234 -- error checks on interface conversions further below. ???
12236 if Is_Access_Type (Opnd) then
12237 Opnd := Designated_Type (Opnd);
12239 -- If the type of the operand is a limited view, use nonlimited
12240 -- view when available. If it is a class-wide type, recover the
12241 -- class-wide type of the nonlimited view.
12243 if From_Limited_With (Opnd)
12244 and then Has_Non_Limited_View (Opnd)
12245 then
12246 Opnd := Non_Limited_View (Opnd);
12247 end if;
12248 end if;
12250 if Is_Access_Type (Target_Typ) then
12251 Target := Designated_Type (Target);
12253 -- If the target type is a limited view, use nonlimited view
12254 -- when available.
12256 if From_Limited_With (Target)
12257 and then Has_Non_Limited_View (Target)
12258 then
12259 Target := Non_Limited_View (Target);
12260 end if;
12261 end if;
12263 if Opnd = Target then
12264 null;
12266 -- Conversion from interface type
12268 -- It seems that it would be better for the error checks below
12269 -- to be performed as part of Validate_Conversion (and maybe some
12270 -- of the error checks above could be moved as well?). ???
12272 elsif Is_Interface (Opnd) then
12274 -- Ada 2005 (AI-217): Handle entities from limited views
12276 if From_Limited_With (Opnd) then
12277 Error_Msg_Qual_Level := 99;
12278 Error_Msg_NE -- CODEFIX
12279 ("missing WITH clause on package &", N,
12280 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
12281 Error_Msg_N
12282 ("type conversions require visibility of the full view",
12285 elsif From_Limited_With (Target)
12286 and then not
12287 (Is_Access_Type (Target_Typ)
12288 and then Present (Non_Limited_View (Etype (Target))))
12289 then
12290 Error_Msg_Qual_Level := 99;
12291 Error_Msg_NE -- CODEFIX
12292 ("missing WITH clause on package &", N,
12293 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12294 Error_Msg_N
12295 ("type conversions require visibility of the full view",
12298 else
12299 Expand_Interface_Conversion (N);
12300 end if;
12302 -- Conversion to interface type
12304 elsif Is_Interface (Target) then
12306 -- Handle subtypes
12308 if Ekind (Opnd) in E_Protected_Subtype | E_Task_Subtype then
12309 Opnd := Etype (Opnd);
12310 end if;
12312 if Is_Class_Wide_Type (Opnd)
12313 or else Interface_Present_In_Ancestor
12314 (Typ => Opnd,
12315 Iface => Target)
12316 then
12317 Expand_Interface_Conversion (N);
12318 else
12319 Error_Msg_Name_1 := Chars (Etype (Target));
12320 Error_Msg_Name_2 := Chars (Opnd);
12321 Error_Msg_N
12322 ("wrong interface conversion (% is not a progenitor "
12323 & "of %)", N);
12324 end if;
12325 end if;
12326 end;
12327 end if;
12329 -- Ada 2012: Once the type conversion is resolved, check whether the
12330 -- operand satisfies a static predicate of the target subtype, if any.
12331 -- In the static expression case, a predicate check failure is an error.
12333 if Has_Predicates (Target_Typ) then
12334 Check_Expression_Against_Static_Predicate
12335 (N, Target_Typ, Static_Failure_Is_Error => True);
12336 end if;
12338 -- If at this stage we have a fixed to integer conversion, make sure the
12339 -- Do_Range_Check flag is set, because such conversions in general need
12340 -- a range check. We only need this if expansion is off, see above why.
12342 if Nkind (N) = N_Type_Conversion
12343 and then not Expander_Active
12344 and then Is_Integer_Type (Target_Typ)
12345 and then Is_Fixed_Point_Type (Operand_Typ)
12346 and then not Range_Checks_Suppressed (Target_Typ)
12347 and then not Range_Checks_Suppressed (Operand_Typ)
12348 then
12349 Set_Do_Range_Check (Operand);
12350 end if;
12352 -- Generating C code a type conversion of an access to constrained
12353 -- array type to access to unconstrained array type involves building
12354 -- a fat pointer which in general cannot be generated on the fly. We
12355 -- remove side effects in order to store the result of the conversion
12356 -- into a temporary.
12358 if Modify_Tree_For_C
12359 and then Nkind (N) = N_Type_Conversion
12360 and then Nkind (Parent (N)) /= N_Object_Declaration
12361 and then Is_Access_Type (Etype (N))
12362 and then Is_Array_Type (Designated_Type (Etype (N)))
12363 and then not Is_Constrained (Designated_Type (Etype (N)))
12364 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
12365 then
12366 Remove_Side_Effects (N);
12367 end if;
12368 end Resolve_Type_Conversion;
12370 ----------------------
12371 -- Resolve_Unary_Op --
12372 ----------------------
12374 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12375 B_Typ : constant Entity_Id := Base_Type (Typ);
12376 R : constant Node_Id := Right_Opnd (N);
12377 OK : Boolean;
12378 Lo : Uint;
12379 Hi : Uint;
12381 begin
12382 -- Deal with intrinsic unary operators
12384 if Comes_From_Source (N)
12385 and then Ekind (Entity (N)) = E_Function
12386 and then Is_Imported (Entity (N))
12387 and then Is_Intrinsic_Subprogram (Entity (N))
12388 then
12389 Resolve_Intrinsic_Unary_Operator (N, Typ);
12390 return;
12391 end if;
12393 -- Deal with universal cases
12395 if Is_Universal_Numeric_Type (Etype (R)) then
12396 Check_For_Visible_Operator (N, B_Typ);
12397 end if;
12399 Set_Etype (N, B_Typ);
12400 Resolve (R, B_Typ);
12402 -- Generate warning for negative literal of a modular type, unless it is
12403 -- enclosed directly in a type qualification or a type conversion, as it
12404 -- is likely not what the user intended. We don't issue the warning for
12405 -- the common use of -1 to denote OxFFFF_FFFF...
12407 if Warn_On_Suspicious_Modulus_Value
12408 and then Nkind (N) = N_Op_Minus
12409 and then Nkind (R) = N_Integer_Literal
12410 and then Is_Modular_Integer_Type (B_Typ)
12411 and then Nkind (Parent (N)) not in N_Qualified_Expression
12412 | N_Type_Conversion
12413 and then Expr_Value (R) > Uint_1
12414 then
12415 Error_Msg_N
12416 ("?.m?negative literal of modular type is in fact positive", N);
12417 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12418 Error_Msg_Uint_2 := Expr_Value (R);
12419 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12420 Error_Msg_N
12421 ("\if you do, use qualification to avoid this warning", N);
12422 end if;
12424 -- Generate warning for expressions like abs (x mod 2)
12426 if Warn_On_Redundant_Constructs
12427 and then Nkind (N) = N_Op_Abs
12428 then
12429 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12431 if OK and then Hi >= Lo and then Lo >= 0 then
12432 Error_Msg_N -- CODEFIX
12433 ("?r?abs applied to known non-negative value has no effect", N);
12434 end if;
12435 end if;
12437 -- Deal with reference generation
12439 Check_Unset_Reference (R);
12440 Generate_Operator_Reference (N, B_Typ);
12441 Analyze_Dimension (N);
12442 Eval_Unary_Op (N);
12444 -- Set overflow checking bit. Much cleverer code needed here eventually
12445 -- and perhaps the Resolve routines should be separated for the various
12446 -- arithmetic operations, since they will need different processing ???
12448 if Nkind (N) in N_Op then
12449 if not Overflow_Checks_Suppressed (Etype (N)) then
12450 Enable_Overflow_Check (N);
12451 end if;
12452 end if;
12454 -- Generate warning for expressions like -5 mod 3 for integers. No need
12455 -- to worry in the floating-point case, since parens do not affect the
12456 -- result so there is no point in giving in a warning.
12458 declare
12459 Norig : constant Node_Id := Original_Node (N);
12460 Rorig : Node_Id;
12461 Val : Uint;
12462 HB : Uint;
12463 LB : Uint;
12464 Lval : Uint;
12465 Opnd : Node_Id;
12467 begin
12468 if Warn_On_Questionable_Missing_Parens
12469 and then Comes_From_Source (Norig)
12470 and then Is_Integer_Type (Typ)
12471 and then Nkind (Norig) = N_Op_Minus
12472 then
12473 Rorig := Original_Node (Right_Opnd (Norig));
12475 -- We are looking for cases where the right operand is not
12476 -- parenthesized, and is a binary operator, multiply, divide, or
12477 -- mod. These are the cases where the grouping can affect results.
12479 if Paren_Count (Rorig) = 0
12480 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12481 then
12482 -- For mod, we always give the warning, since the value is
12483 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12484 -- -(5 mod 315)). But for the other cases, the only concern is
12485 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12486 -- overflows, but (-2) * 64 does not). So we try to give the
12487 -- message only when overflow is possible.
12489 if Nkind (Rorig) /= N_Op_Mod
12490 and then Compile_Time_Known_Value (R)
12491 then
12492 Val := Expr_Value (R);
12494 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12495 HB := Expr_Value (Type_High_Bound (Typ));
12496 else
12497 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12498 end if;
12500 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12501 LB := Expr_Value (Type_Low_Bound (Typ));
12502 else
12503 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12504 end if;
12506 -- Note that the test below is deliberately excluding the
12507 -- largest negative number, since that is a potentially
12508 -- troublesome case (e.g. -2 * x, where the result is the
12509 -- largest negative integer has an overflow with 2 * x).
12511 if Val > LB and then Val <= HB then
12512 return;
12513 end if;
12514 end if;
12516 -- For the multiplication case, the only case we have to worry
12517 -- about is when (-a)*b is exactly the largest negative number
12518 -- so that -(a*b) can cause overflow. This can only happen if
12519 -- a is a power of 2, and more generally if any operand is a
12520 -- constant that is not a power of 2, then the parentheses
12521 -- cannot affect whether overflow occurs. We only bother to
12522 -- test the left most operand
12524 -- Loop looking at left operands for one that has known value
12526 Opnd := Rorig;
12527 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12528 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12529 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12531 -- Operand value of 0 or 1 skips warning
12533 if Lval <= 1 then
12534 return;
12536 -- Otherwise check power of 2, if power of 2, warn, if
12537 -- anything else, skip warning.
12539 else
12540 while Lval /= 2 loop
12541 if Lval mod 2 = 1 then
12542 return;
12543 else
12544 Lval := Lval / 2;
12545 end if;
12546 end loop;
12548 exit Opnd_Loop;
12549 end if;
12550 end if;
12552 -- Keep looking at left operands
12554 Opnd := Left_Opnd (Opnd);
12555 end loop Opnd_Loop;
12557 -- For rem or "/" we can only have a problematic situation
12558 -- if the divisor has a value of minus one or one. Otherwise
12559 -- overflow is impossible (divisor > 1) or we have a case of
12560 -- division by zero in any case.
12562 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12563 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12564 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12565 then
12566 return;
12567 end if;
12569 -- If we fall through warning should be issued
12571 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12573 Error_Msg_N
12574 ("??unary minus expression should be parenthesized here!", N);
12575 end if;
12576 end if;
12577 end;
12578 end Resolve_Unary_Op;
12580 ----------------------------------
12581 -- Resolve_Unchecked_Expression --
12582 ----------------------------------
12584 procedure Resolve_Unchecked_Expression
12585 (N : Node_Id;
12586 Typ : Entity_Id)
12588 begin
12589 Resolve (Expression (N), Typ, Suppress => All_Checks);
12590 Set_Etype (N, Typ);
12591 end Resolve_Unchecked_Expression;
12593 ---------------------------------------
12594 -- Resolve_Unchecked_Type_Conversion --
12595 ---------------------------------------
12597 procedure Resolve_Unchecked_Type_Conversion
12598 (N : Node_Id;
12599 Typ : Entity_Id)
12601 pragma Warnings (Off, Typ);
12603 Operand : constant Node_Id := Expression (N);
12604 Opnd_Type : constant Entity_Id := Etype (Operand);
12606 begin
12607 -- Resolve operand using its own type
12609 Resolve (Operand, Opnd_Type);
12611 -- If the expression is a conversion to universal integer of an
12612 -- an expression with an integer type, then we can eliminate the
12613 -- intermediate conversion to universal integer.
12615 if Nkind (Operand) = N_Type_Conversion
12616 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12617 and then Is_Integer_Type (Etype (Expression (Operand)))
12618 then
12619 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12620 Analyze_And_Resolve (Operand);
12621 end if;
12623 -- In an inlined context, the unchecked conversion may be applied
12624 -- to a literal, in which case its type is the type of the context.
12625 -- (In other contexts conversions cannot apply to literals).
12627 if In_Inlined_Body
12628 and then (Opnd_Type = Any_Character or else
12629 Opnd_Type = Any_Integer or else
12630 Opnd_Type = Any_Real)
12631 then
12632 Set_Etype (Operand, Typ);
12633 end if;
12635 Analyze_Dimension (N);
12636 Eval_Unchecked_Conversion (N);
12637 end Resolve_Unchecked_Type_Conversion;
12639 ------------------------------
12640 -- Rewrite_Operator_As_Call --
12641 ------------------------------
12643 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12644 Loc : constant Source_Ptr := Sloc (N);
12645 Actuals : constant List_Id := New_List;
12646 New_N : Node_Id;
12648 begin
12649 if Nkind (N) in N_Binary_Op then
12650 Append (Left_Opnd (N), Actuals);
12651 end if;
12653 Append (Right_Opnd (N), Actuals);
12655 New_N :=
12656 Make_Function_Call (Sloc => Loc,
12657 Name => New_Occurrence_Of (Nam, Loc),
12658 Parameter_Associations => Actuals);
12660 Preserve_Comes_From_Source (New_N, N);
12661 Preserve_Comes_From_Source (Name (New_N), N);
12662 Rewrite (N, New_N);
12663 Set_Etype (N, Etype (Nam));
12664 end Rewrite_Operator_As_Call;
12666 ------------------------------
12667 -- Rewrite_Renamed_Operator --
12668 ------------------------------
12670 procedure Rewrite_Renamed_Operator
12671 (N : Node_Id;
12672 Op : Entity_Id;
12673 Typ : Entity_Id)
12675 Nam : constant Name_Id := Chars (Op);
12676 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12677 Op_Node : Node_Id;
12679 begin
12680 -- Do not perform this transformation within a pre/postcondition,
12681 -- because the expression will be reanalyzed, and the transformation
12682 -- might affect the visibility of the operator, e.g. in an instance.
12683 -- Note that fully analyzed and expanded pre/postconditions appear as
12684 -- pragma Check equivalents.
12686 if In_Pre_Post_Condition (N) then
12687 return;
12688 end if;
12690 -- Likewise when an expression function is being preanalyzed, since the
12691 -- expression will be reanalyzed as part of the generated body.
12693 if In_Spec_Expression then
12694 declare
12695 S : constant Entity_Id := Current_Scope_No_Loops;
12696 begin
12697 if Ekind (S) = E_Function
12698 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12699 N_Expression_Function
12700 then
12701 return;
12702 end if;
12703 end;
12704 end if;
12706 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12707 Set_Chars (Op_Node, Nam);
12708 Set_Etype (Op_Node, Etype (N));
12709 Set_Entity (Op_Node, Op);
12710 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12712 if Is_Binary then
12713 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12714 end if;
12716 -- Indicate that both the original entity and its renaming are
12717 -- referenced at this point.
12719 Generate_Reference (Entity (N), N);
12720 Generate_Reference (Op, N);
12722 Rewrite (N, Op_Node);
12724 -- If the context type is private, add the appropriate conversions so
12725 -- that the operator is applied to the full view. This is done in the
12726 -- routines that resolve intrinsic operators.
12728 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12729 case Nkind (N) is
12730 when N_Op_Add
12731 | N_Op_Divide
12732 | N_Op_Expon
12733 | N_Op_Mod
12734 | N_Op_Multiply
12735 | N_Op_Rem
12736 | N_Op_Subtract
12738 Resolve_Intrinsic_Operator (N, Typ);
12740 when N_Op_Abs
12741 | N_Op_Minus
12742 | N_Op_Plus
12744 Resolve_Intrinsic_Unary_Operator (N, Typ);
12746 when others =>
12747 Resolve (N, Typ);
12748 end case;
12749 end if;
12750 end Rewrite_Renamed_Operator;
12752 -----------------------
12753 -- Set_Slice_Subtype --
12754 -----------------------
12756 -- Build an implicit subtype declaration to represent the type delivered by
12757 -- the slice. This is an abbreviated version of an array subtype. We define
12758 -- an index subtype for the slice, using either the subtype name or the
12759 -- discrete range of the slice. To be consistent with index usage elsewhere
12760 -- we create a list header to hold the single index. This list is not
12761 -- otherwise attached to the syntax tree.
12763 procedure Set_Slice_Subtype (N : Node_Id) is
12764 Loc : constant Source_Ptr := Sloc (N);
12765 Index_List : constant List_Id := New_List;
12766 Index : Node_Id;
12767 Index_Subtype : Entity_Id;
12768 Index_Type : Entity_Id;
12769 Slice_Subtype : Entity_Id;
12770 Drange : constant Node_Id := Discrete_Range (N);
12772 begin
12773 Index_Type := Base_Type (Etype (Drange));
12775 if Is_Entity_Name (Drange) then
12776 Index_Subtype := Entity (Drange);
12778 else
12779 -- We force the evaluation of a range. This is definitely needed in
12780 -- the renamed case, and seems safer to do unconditionally. Note in
12781 -- any case that since we will create and insert an Itype referring
12782 -- to this range, we must make sure any side effect removal actions
12783 -- are inserted before the Itype definition.
12785 if Nkind (Drange) = N_Range then
12786 Force_Evaluation (Low_Bound (Drange));
12787 Force_Evaluation (High_Bound (Drange));
12789 -- If the discrete range is given by a subtype indication, the
12790 -- type of the slice is the base of the subtype mark.
12792 elsif Nkind (Drange) = N_Subtype_Indication then
12793 declare
12794 R : constant Node_Id := Range_Expression (Constraint (Drange));
12795 begin
12796 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12797 Force_Evaluation (Low_Bound (R));
12798 Force_Evaluation (High_Bound (R));
12799 end;
12800 end if;
12802 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12804 -- Take a new copy of Drange (where bounds have been rewritten to
12805 -- reference side-effect-free names). Using a separate tree ensures
12806 -- that further expansion (e.g. while rewriting a slice assignment
12807 -- into a FOR loop) does not attempt to remove side effects on the
12808 -- bounds again (which would cause the bounds in the index subtype
12809 -- definition to refer to temporaries before they are defined) (the
12810 -- reason is that some names are considered side effect free here
12811 -- for the subtype, but not in the context of a loop iteration
12812 -- scheme).
12814 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12815 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12816 Set_Etype (Index_Subtype, Index_Type);
12817 Set_Size_Info (Index_Subtype, Index_Type);
12818 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12819 Set_Is_Constrained (Index_Subtype);
12820 end if;
12822 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12824 Index := New_Occurrence_Of (Index_Subtype, Loc);
12825 Set_Etype (Index, Index_Subtype);
12826 Append (Index, Index_List);
12828 Set_First_Index (Slice_Subtype, Index);
12829 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12830 Set_Is_Constrained (Slice_Subtype, True);
12832 Check_Compile_Time_Size (Slice_Subtype);
12834 -- The Etype of the existing Slice node is reset to this slice subtype.
12835 -- Its bounds are obtained from its first index.
12837 Set_Etype (N, Slice_Subtype);
12839 -- For bit-packed slice subtypes, freeze immediately (except in the case
12840 -- of being in a "spec expression" where we never freeze when we first
12841 -- see the expression).
12843 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12844 Freeze_Itype (Slice_Subtype, N);
12846 -- For all other cases insert an itype reference in the slice's actions
12847 -- so that the itype is frozen at the proper place in the tree (i.e. at
12848 -- the point where actions for the slice are analyzed). Note that this
12849 -- is different from freezing the itype immediately, which might be
12850 -- premature (e.g. if the slice is within a transient scope). This needs
12851 -- to be done only if expansion is enabled.
12853 elsif Expander_Active then
12854 Ensure_Defined (Typ => Slice_Subtype, N => N);
12855 end if;
12856 end Set_Slice_Subtype;
12858 --------------------------------
12859 -- Set_String_Literal_Subtype --
12860 --------------------------------
12862 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12863 Loc : constant Source_Ptr := Sloc (N);
12864 Low_Bound : constant Node_Id :=
12865 Type_Low_Bound (Etype (First_Index (Typ)));
12866 Subtype_Id : Entity_Id;
12868 begin
12869 if Nkind (N) /= N_String_Literal then
12870 return;
12871 end if;
12873 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
12874 Set_String_Literal_Length (Subtype_Id, UI_From_Int
12875 (String_Length (Strval (N))));
12876 Set_Etype (Subtype_Id, Base_Type (Typ));
12877 Set_Is_Constrained (Subtype_Id);
12878 Set_Etype (N, Subtype_Id);
12880 -- The low bound is set from the low bound of the corresponding index
12881 -- type. Note that we do not store the high bound in the string literal
12882 -- subtype, but it can be deduced if necessary from the length and the
12883 -- low bound.
12885 if Is_OK_Static_Expression (Low_Bound) then
12886 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
12888 -- If the lower bound is not static we create a range for the string
12889 -- literal, using the index type and the known length of the literal.
12890 -- If the length is 1, then the upper bound is set to a mere copy of
12891 -- the lower bound; or else, if the index type is a signed integer,
12892 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
12893 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
12895 else
12896 declare
12897 Length : constant Nat := String_Length (Strval (N));
12898 Index_List : constant List_Id := New_List;
12899 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
12900 Array_Subtype : Entity_Id;
12901 Drange : Node_Id;
12902 High_Bound : Node_Id;
12903 Index : Node_Id;
12904 Index_Subtype : Entity_Id;
12906 begin
12907 if Length = 1 then
12908 High_Bound := New_Copy_Tree (Low_Bound);
12910 elsif Is_Signed_Integer_Type (Index_Type) then
12911 High_Bound :=
12912 Make_Op_Add (Loc,
12913 Left_Opnd => New_Copy_Tree (Low_Bound),
12914 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
12916 else
12917 High_Bound :=
12918 Make_Attribute_Reference (Loc,
12919 Attribute_Name => Name_Val,
12920 Prefix =>
12921 New_Occurrence_Of (Index_Type, Loc),
12922 Expressions => New_List (
12923 Make_Op_Add (Loc,
12924 Left_Opnd =>
12925 Make_Attribute_Reference (Loc,
12926 Attribute_Name => Name_Pos,
12927 Prefix =>
12928 New_Occurrence_Of (Index_Type, Loc),
12929 Expressions =>
12930 New_List (New_Copy_Tree (Low_Bound))),
12931 Right_Opnd =>
12932 Make_Integer_Literal (Loc, Length - 1))));
12933 end if;
12935 if Is_Integer_Type (Index_Type) then
12936 Set_String_Literal_Low_Bound
12937 (Subtype_Id, Make_Integer_Literal (Loc, 1));
12939 else
12940 -- If the index type is an enumeration type, build bounds
12941 -- expression with attributes.
12943 Set_String_Literal_Low_Bound
12944 (Subtype_Id,
12945 Make_Attribute_Reference (Loc,
12946 Attribute_Name => Name_First,
12947 Prefix =>
12948 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
12949 end if;
12951 Analyze_And_Resolve
12952 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
12954 -- Build bona fide subtype for the string, and wrap it in an
12955 -- unchecked conversion, because the back end expects the
12956 -- String_Literal_Subtype to have a static lower bound.
12958 Index_Subtype :=
12959 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12960 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
12961 Set_Scalar_Range (Index_Subtype, Drange);
12962 Set_Parent (Drange, N);
12963 Analyze_And_Resolve (Drange, Index_Type);
12965 -- In this context, the Index_Type may already have a constraint,
12966 -- so use common base type on string subtype. The base type may
12967 -- be used when generating attributes of the string, for example
12968 -- in the context of a slice assignment.
12970 Set_Etype (Index_Subtype, Base_Type (Index_Type));
12971 Set_Size_Info (Index_Subtype, Index_Type);
12972 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12974 Array_Subtype := Create_Itype (E_Array_Subtype, N);
12976 Index := New_Occurrence_Of (Index_Subtype, Loc);
12977 Set_Etype (Index, Index_Subtype);
12978 Append (Index, Index_List);
12980 Set_First_Index (Array_Subtype, Index);
12981 Set_Etype (Array_Subtype, Base_Type (Typ));
12982 Set_Is_Constrained (Array_Subtype, True);
12984 Rewrite (N, Unchecked_Convert_To (Array_Subtype, N));
12985 Set_Etype (N, Array_Subtype);
12986 end;
12987 end if;
12988 end Set_String_Literal_Subtype;
12990 ------------------------------
12991 -- Simplify_Type_Conversion --
12992 ------------------------------
12994 procedure Simplify_Type_Conversion (N : Node_Id) is
12995 begin
12996 if Nkind (N) = N_Type_Conversion then
12997 declare
12998 Operand : constant Node_Id := Expression (N);
12999 Target_Typ : constant Entity_Id := Etype (N);
13000 Opnd_Typ : constant Entity_Id := Etype (Operand);
13002 begin
13003 -- Special processing if the conversion is the expression of a
13004 -- Rounding or Truncation attribute reference. In this case we
13005 -- replace:
13007 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
13009 -- by
13011 -- ityp (x)
13013 -- with the Float_Truncate flag set to False or True respectively,
13014 -- which is more efficient. We reuse Rounding for Machine_Rounding
13015 -- as System.Fat_Gen, which is a permissible behavior.
13017 if Is_Floating_Point_Type (Opnd_Typ)
13018 and then
13019 (Is_Integer_Type (Target_Typ)
13020 or else (Is_Fixed_Point_Type (Target_Typ)
13021 and then Conversion_OK (N)))
13022 and then Nkind (Operand) = N_Attribute_Reference
13023 and then Attribute_Name (Operand) in Name_Rounding
13024 | Name_Machine_Rounding
13025 | Name_Truncation
13026 then
13027 declare
13028 Truncate : constant Boolean :=
13029 Attribute_Name (Operand) = Name_Truncation;
13030 begin
13031 Rewrite (Operand,
13032 Relocate_Node (First (Expressions (Operand))));
13033 Set_Float_Truncate (N, Truncate);
13034 end;
13036 -- Special processing for the conversion of an integer literal to
13037 -- a dynamic type: we first convert the literal to the root type
13038 -- and then convert the result to the target type, the goal being
13039 -- to avoid doing range checks in universal integer.
13041 elsif Is_Integer_Type (Target_Typ)
13042 and then not Is_Generic_Type (Root_Type (Target_Typ))
13043 and then Nkind (Operand) = N_Integer_Literal
13044 and then Opnd_Typ = Universal_Integer
13045 then
13046 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
13047 Analyze_And_Resolve (Operand);
13049 -- If the expression is a conversion to universal integer of an
13050 -- an expression with an integer type, then we can eliminate the
13051 -- intermediate conversion to universal integer.
13053 elsif Nkind (Operand) = N_Type_Conversion
13054 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
13055 and then Is_Integer_Type (Etype (Expression (Operand)))
13056 then
13057 Rewrite (Operand, Relocate_Node (Expression (Operand)));
13058 Analyze_And_Resolve (Operand);
13059 end if;
13060 end;
13061 end if;
13062 end Simplify_Type_Conversion;
13064 ------------------------------
13065 -- Try_User_Defined_Literal --
13066 ------------------------------
13068 function Try_User_Defined_Literal
13069 (N : Node_Id;
13070 Typ : Entity_Id) return Boolean
13072 begin
13073 if Nkind (N) in N_Op_Add | N_Op_Divide | N_Op_Mod | N_Op_Multiply
13074 | N_Op_Rem | N_Op_Subtract
13075 then
13077 -- Both operands must have the same type as the context.
13078 -- (ignoring for now fixed-point and exponentiation ops).
13080 if Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ) then
13081 Resolve (Left_Opnd (N), Typ);
13082 Analyze_And_Resolve (N, Typ);
13083 return True;
13084 end if;
13087 Has_Applicable_User_Defined_Literal (Left_Opnd (N), Typ)
13088 then
13089 Resolve (Right_Opnd (N), Typ);
13090 Analyze_And_Resolve (N, Typ);
13091 return True;
13093 else
13094 return False;
13095 end if;
13097 elsif Nkind (N) in N_Binary_Op then
13098 -- For other operators the context does not impose a type on
13099 -- the operands, but their types must match.
13101 if (Nkind (Left_Opnd (N))
13102 not in N_Integer_Literal | N_String_Literal | N_Real_Literal)
13103 and then
13104 Has_Applicable_User_Defined_Literal
13105 (Right_Opnd (N), Etype (Left_Opnd (N)))
13106 then
13107 Analyze_And_Resolve (N, Typ);
13108 return True;
13110 elsif (Nkind (Right_Opnd (N))
13111 not in N_Integer_Literal | N_String_Literal | N_Real_Literal)
13112 and then
13113 Has_Applicable_User_Defined_Literal
13114 (Left_Opnd (N), Etype (Right_Opnd (N)))
13115 then
13116 Analyze_And_Resolve (N, Typ);
13117 return True;
13118 else
13119 return False;
13120 end if;
13122 elsif Nkind (N) in N_Unary_Op
13123 and then
13124 Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13125 then
13126 Analyze_And_Resolve (N, Typ);
13127 return True;
13129 else -- Other operators
13130 return False;
13131 end if;
13132 end Try_User_Defined_Literal;
13134 -----------------------------
13135 -- Unique_Fixed_Point_Type --
13136 -----------------------------
13138 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
13139 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
13140 -- Give error messages for true ambiguity. Messages are posted on node
13141 -- N, and entities T1, T2 are the possible interpretations.
13143 -----------------------
13144 -- Fixed_Point_Error --
13145 -----------------------
13147 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
13148 begin
13149 Error_Msg_N ("ambiguous universal_fixed_expression", N);
13150 Error_Msg_NE ("\\possible interpretation as}", N, T1);
13151 Error_Msg_NE ("\\possible interpretation as}", N, T2);
13152 end Fixed_Point_Error;
13154 -- Local variables
13156 ErrN : Node_Id;
13157 Item : Node_Id;
13158 Scop : Entity_Id;
13159 T1 : Entity_Id;
13160 T2 : Entity_Id;
13162 -- Start of processing for Unique_Fixed_Point_Type
13164 begin
13165 -- The operations on Duration are visible, so Duration is always a
13166 -- possible interpretation.
13168 T1 := Standard_Duration;
13170 -- Look for fixed-point types in enclosing scopes
13172 Scop := Current_Scope;
13173 while Scop /= Standard_Standard loop
13174 T2 := First_Entity (Scop);
13175 while Present (T2) loop
13176 if Is_Fixed_Point_Type (T2)
13177 and then Current_Entity (T2) = T2
13178 and then Scope (Base_Type (T2)) = Scop
13179 then
13180 if Present (T1) then
13181 Fixed_Point_Error (T1, T2);
13182 return Any_Type;
13183 else
13184 T1 := T2;
13185 end if;
13186 end if;
13188 Next_Entity (T2);
13189 end loop;
13191 Scop := Scope (Scop);
13192 end loop;
13194 -- Look for visible fixed type declarations in the context
13196 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
13197 while Present (Item) loop
13198 if Nkind (Item) = N_With_Clause then
13199 Scop := Entity (Name (Item));
13200 T2 := First_Entity (Scop);
13201 while Present (T2) loop
13202 if Is_Fixed_Point_Type (T2)
13203 and then Scope (Base_Type (T2)) = Scop
13204 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
13205 then
13206 if Present (T1) then
13207 Fixed_Point_Error (T1, T2);
13208 return Any_Type;
13209 else
13210 T1 := T2;
13211 end if;
13212 end if;
13214 Next_Entity (T2);
13215 end loop;
13216 end if;
13218 Next (Item);
13219 end loop;
13221 if Nkind (N) = N_Real_Literal then
13222 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
13224 else
13225 -- When the context is a type conversion, issue the warning on the
13226 -- expression of the conversion because it is the actual operation.
13228 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
13229 ErrN := Expression (N);
13230 else
13231 ErrN := N;
13232 end if;
13234 Error_Msg_NE
13235 ("??universal_fixed expression interpreted as }!", ErrN, T1);
13236 end if;
13238 return T1;
13239 end Unique_Fixed_Point_Type;
13241 ----------------------
13242 -- Valid_Conversion --
13243 ----------------------
13245 function Valid_Conversion
13246 (N : Node_Id;
13247 Target : Entity_Id;
13248 Operand : Node_Id;
13249 Report_Errs : Boolean := True) return Boolean
13251 Target_Type : constant Entity_Id := Base_Type (Target);
13252 Opnd_Type : Entity_Id := Etype (Operand);
13253 Inc_Ancestor : Entity_Id;
13255 function Conversion_Check
13256 (Valid : Boolean;
13257 Msg : String) return Boolean;
13258 -- Little routine to post Msg if Valid is False, returns Valid value
13260 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
13261 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
13263 procedure Conversion_Error_NE
13264 (Msg : String;
13265 N : Node_Or_Entity_Id;
13266 E : Node_Or_Entity_Id);
13267 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
13269 function In_Instance_Code return Boolean;
13270 -- Return True if expression is within an instance but is not in one of
13271 -- the actuals of the instantiation. Type conversions within an instance
13272 -- are not rechecked because type visibility may lead to spurious errors
13273 -- but conversions in an actual for a formal object must be checked.
13275 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13276 (Expr : Node_Id) return Boolean;
13277 -- Implicit anonymous-to-named access type conversions are not allowed
13278 -- if the "statically deeper than" relationship does not apply to the
13279 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
13280 -- We deal with most such cases elsewhere so that we can emit more
13281 -- specific error messages (e.g., if the operand is an access parameter
13282 -- or a saooaaat (stand-alone object of an anonymous access type)), but
13283 -- here is where we catch the case where the operand is an access
13284 -- discriminant selected from a dereference of another such "bad"
13285 -- conversion argument.
13287 function Valid_Tagged_Conversion
13288 (Target_Type : Entity_Id;
13289 Opnd_Type : Entity_Id) return Boolean;
13290 -- Specifically test for validity of tagged conversions
13292 function Valid_Array_Conversion return Boolean;
13293 -- Check index and component conformance, and accessibility levels if
13294 -- the component types are anonymous access types (Ada 2005).
13296 ----------------------
13297 -- Conversion_Check --
13298 ----------------------
13300 function Conversion_Check
13301 (Valid : Boolean;
13302 Msg : String) return Boolean
13304 begin
13305 if not Valid
13307 -- A generic unit has already been analyzed and we have verified
13308 -- that a particular conversion is OK in that context. Since the
13309 -- instance is reanalyzed without relying on the relationships
13310 -- established during the analysis of the generic, it is possible
13311 -- to end up with inconsistent views of private types. Do not emit
13312 -- the error message in such cases. The rest of the machinery in
13313 -- Valid_Conversion still ensures the proper compatibility of
13314 -- target and operand types.
13316 and then not In_Instance_Code
13317 then
13318 Conversion_Error_N (Msg, Operand);
13319 end if;
13321 return Valid;
13322 end Conversion_Check;
13324 ------------------------
13325 -- Conversion_Error_N --
13326 ------------------------
13328 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
13329 begin
13330 if Report_Errs then
13331 Error_Msg_N (Msg, N);
13332 end if;
13333 end Conversion_Error_N;
13335 -------------------------
13336 -- Conversion_Error_NE --
13337 -------------------------
13339 procedure Conversion_Error_NE
13340 (Msg : String;
13341 N : Node_Or_Entity_Id;
13342 E : Node_Or_Entity_Id)
13344 begin
13345 if Report_Errs then
13346 Error_Msg_NE (Msg, N, E);
13347 end if;
13348 end Conversion_Error_NE;
13350 ----------------------
13351 -- In_Instance_Code --
13352 ----------------------
13354 function In_Instance_Code return Boolean is
13355 Par : Node_Id;
13357 begin
13358 if not In_Instance then
13359 return False;
13361 else
13362 Par := Parent (N);
13363 while Present (Par) loop
13365 -- The expression is part of an actual object if it appears in
13366 -- the generated object declaration in the instance.
13368 if Nkind (Par) = N_Object_Declaration
13369 and then Present (Corresponding_Generic_Association (Par))
13370 then
13371 return False;
13373 else
13374 exit when
13375 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13376 or else Nkind (Par) in N_Subprogram_Call
13377 or else Nkind (Par) in N_Declaration;
13378 end if;
13380 Par := Parent (Par);
13381 end loop;
13383 -- Otherwise the expression appears within the instantiated unit
13385 return True;
13386 end if;
13387 end In_Instance_Code;
13389 --------------------------------------------------
13390 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13391 --------------------------------------------------
13393 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13394 (Expr : Node_Id) return Boolean
13396 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13397 pragma Assert (Is_Access_Type (Exp_Type));
13399 Associated_Node : Node_Id;
13400 Deref_Prefix : Node_Id;
13401 begin
13402 if not Is_Anonymous_Access_Type (Exp_Type) then
13403 return False;
13404 end if;
13406 pragma Assert (Is_Itype (Exp_Type));
13407 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13409 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13410 return False; -- not the type of an access discriminant
13411 end if;
13413 -- return False if Expr not of form <prefix>.all.Some_Component
13415 if (Nkind (Expr) /= N_Selected_Component)
13416 or else (Nkind (Prefix (Expr)) /= N_Explicit_Dereference)
13417 then
13418 -- conditional expressions, declare expressions ???
13419 return False;
13420 end if;
13422 Deref_Prefix := Prefix (Prefix (Expr));
13423 Exp_Type := Base_Type (Etype (Deref_Prefix));
13425 -- The "statically deeper relationship" does not apply
13426 -- to generic formal access types, so a prefix of such
13427 -- a type is a "bad" prefix.
13429 if Is_Generic_Formal (Exp_Type) then
13430 return True;
13432 -- The "statically deeper relationship" does apply to
13433 -- any other named access type.
13435 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13436 return False;
13437 end if;
13439 pragma Assert (Is_Itype (Exp_Type));
13440 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13442 -- The "statically deeper relationship" applies to some
13443 -- anonymous access types and not to others. Return
13444 -- True for the cases where it does not apply. Also check
13445 -- recursively for the
13446 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13447 -- where the correct result depends on <prefix>.
13449 return Nkind (Associated_Node) in
13450 N_Procedure_Specification | -- access parameter
13451 N_Function_Specification | -- access parameter
13452 N_Object_Declaration -- saooaaat
13453 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13454 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13456 ----------------------------
13457 -- Valid_Array_Conversion --
13458 ----------------------------
13460 function Valid_Array_Conversion return Boolean is
13461 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13462 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13464 Opnd_Index : Node_Id;
13465 Opnd_Index_Type : Entity_Id;
13467 Target_Comp_Type : constant Entity_Id :=
13468 Component_Type (Target_Type);
13469 Target_Comp_Base : constant Entity_Id :=
13470 Base_Type (Target_Comp_Type);
13472 Target_Index : Node_Id;
13473 Target_Index_Type : Entity_Id;
13475 begin
13476 -- Error if wrong number of dimensions
13479 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13480 then
13481 Conversion_Error_N
13482 ("incompatible number of dimensions for conversion", Operand);
13483 return False;
13485 -- Number of dimensions matches
13487 else
13488 -- Loop through indexes of the two arrays
13490 Target_Index := First_Index (Target_Type);
13491 Opnd_Index := First_Index (Opnd_Type);
13492 while Present (Target_Index) and then Present (Opnd_Index) loop
13493 Target_Index_Type := Etype (Target_Index);
13494 Opnd_Index_Type := Etype (Opnd_Index);
13496 -- Error if index types are incompatible
13498 if not (Is_Integer_Type (Target_Index_Type)
13499 and then Is_Integer_Type (Opnd_Index_Type))
13500 and then (Root_Type (Target_Index_Type)
13501 /= Root_Type (Opnd_Index_Type))
13502 then
13503 Conversion_Error_N
13504 ("incompatible index types for array conversion",
13505 Operand);
13506 return False;
13507 end if;
13509 Next_Index (Target_Index);
13510 Next_Index (Opnd_Index);
13511 end loop;
13513 -- If component types have same base type, all set
13515 if Target_Comp_Base = Opnd_Comp_Base then
13516 null;
13518 -- Here if base types of components are not the same. The only
13519 -- time this is allowed is if we have anonymous access types.
13521 -- The conversion of arrays of anonymous access types can lead
13522 -- to dangling pointers. AI-392 formalizes the accessibility
13523 -- checks that must be applied to such conversions to prevent
13524 -- out-of-scope references.
13526 elsif Ekind (Target_Comp_Base) in
13527 E_Anonymous_Access_Type
13528 | E_Anonymous_Access_Subprogram_Type
13529 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13530 and then
13531 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13532 then
13533 if Type_Access_Level (Target_Type) <
13534 Deepest_Type_Access_Level (Opnd_Type)
13535 then
13536 if In_Instance_Body then
13537 Error_Msg_Warn := SPARK_Mode /= On;
13538 Conversion_Error_N
13539 ("source array type has deeper accessibility "
13540 & "level than target<<", Operand);
13541 Conversion_Error_N ("\Program_Error [<<", Operand);
13542 Rewrite (N,
13543 Make_Raise_Program_Error (Sloc (N),
13544 Reason => PE_Accessibility_Check_Failed));
13545 Set_Etype (N, Target_Type);
13546 return False;
13548 -- Conversion not allowed because of accessibility levels
13550 else
13551 Conversion_Error_N
13552 ("source array type has deeper accessibility "
13553 & "level than target", Operand);
13554 return False;
13555 end if;
13557 else
13558 null;
13559 end if;
13561 -- All other cases where component base types do not match
13563 else
13564 Conversion_Error_N
13565 ("incompatible component types for array conversion",
13566 Operand);
13567 return False;
13568 end if;
13570 -- Check that component subtypes statically match. For numeric
13571 -- types this means that both must be either constrained or
13572 -- unconstrained. For enumeration types the bounds must match.
13573 -- All of this is checked in Subtypes_Statically_Match.
13575 if not Subtypes_Statically_Match
13576 (Target_Comp_Type, Opnd_Comp_Type)
13577 then
13578 Conversion_Error_N
13579 ("component subtypes must statically match", Operand);
13580 return False;
13581 end if;
13582 end if;
13584 return True;
13585 end Valid_Array_Conversion;
13587 -----------------------------
13588 -- Valid_Tagged_Conversion --
13589 -----------------------------
13591 function Valid_Tagged_Conversion
13592 (Target_Type : Entity_Id;
13593 Opnd_Type : Entity_Id) return Boolean
13595 begin
13596 -- Upward conversions are allowed (RM 4.6(22))
13598 if Covers (Target_Type, Opnd_Type)
13599 or else Is_Ancestor (Target_Type, Opnd_Type)
13600 then
13601 return True;
13603 -- Downward conversion are allowed if the operand is class-wide
13604 -- (RM 4.6(23)).
13606 elsif Is_Class_Wide_Type (Opnd_Type)
13607 and then Covers (Opnd_Type, Target_Type)
13608 then
13609 return True;
13611 elsif Covers (Opnd_Type, Target_Type)
13612 or else Is_Ancestor (Opnd_Type, Target_Type)
13613 then
13614 return
13615 Conversion_Check (False,
13616 "downward conversion of tagged objects not allowed");
13618 -- Ada 2005 (AI-251): The conversion to/from interface types is
13619 -- always valid. The types involved may be class-wide (sub)types.
13621 elsif Is_Interface (Etype (Base_Type (Target_Type)))
13622 or else Is_Interface (Etype (Base_Type (Opnd_Type)))
13623 then
13624 return True;
13626 -- If the operand is a class-wide type obtained through a limited_
13627 -- with clause, and the context includes the nonlimited view, use
13628 -- it to determine whether the conversion is legal.
13630 elsif Is_Class_Wide_Type (Opnd_Type)
13631 and then From_Limited_With (Opnd_Type)
13632 and then Present (Non_Limited_View (Etype (Opnd_Type)))
13633 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
13634 then
13635 return True;
13637 elsif Is_Access_Type (Opnd_Type)
13638 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
13639 then
13640 return True;
13642 else
13643 Conversion_Error_NE
13644 ("invalid tagged conversion, not compatible with}",
13645 N, First_Subtype (Opnd_Type));
13646 return False;
13647 end if;
13648 end Valid_Tagged_Conversion;
13650 -- Start of processing for Valid_Conversion
13652 begin
13653 Check_Parameterless_Call (Operand);
13655 if Is_Overloaded (Operand) then
13656 declare
13657 I : Interp_Index;
13658 I1 : Interp_Index;
13659 It : Interp;
13660 It1 : Interp;
13661 N1 : Entity_Id;
13662 T1 : Entity_Id;
13664 begin
13665 -- Remove procedure calls, which syntactically cannot appear in
13666 -- this context, but which cannot be removed by type checking,
13667 -- because the context does not impose a type.
13669 -- The node may be labelled overloaded, but still contain only one
13670 -- interpretation because others were discarded earlier. If this
13671 -- is the case, retain the single interpretation if legal.
13673 Get_First_Interp (Operand, I, It);
13674 Opnd_Type := It.Typ;
13675 Get_Next_Interp (I, It);
13677 if Present (It.Typ)
13678 and then Opnd_Type /= Standard_Void_Type
13679 then
13680 -- More than one candidate interpretation is available
13682 Get_First_Interp (Operand, I, It);
13683 while Present (It.Typ) loop
13684 if It.Typ = Standard_Void_Type then
13685 Remove_Interp (I);
13686 end if;
13688 -- When compiling for a system where Address is of a visible
13689 -- integer type, spurious ambiguities can be produced when
13690 -- arithmetic operations have a literal operand and return
13691 -- System.Address or a descendant of it. These ambiguities
13692 -- are usually resolved by the context, but for conversions
13693 -- there is no context type and the removal of the spurious
13694 -- operations must be done explicitly here.
13696 if not Address_Is_Private
13697 and then Is_Descendant_Of_Address (It.Typ)
13698 then
13699 Remove_Interp (I);
13700 end if;
13702 Get_Next_Interp (I, It);
13703 end loop;
13704 end if;
13706 Get_First_Interp (Operand, I, It);
13707 I1 := I;
13708 It1 := It;
13710 if No (It.Typ) then
13711 Conversion_Error_N ("illegal operand in conversion", Operand);
13712 return False;
13713 end if;
13715 Get_Next_Interp (I, It);
13717 if Present (It.Typ) then
13718 N1 := It1.Nam;
13719 T1 := It1.Typ;
13720 It1 := Disambiguate (Operand, I1, I, Any_Type);
13722 if It1 = No_Interp then
13723 Conversion_Error_N
13724 ("ambiguous operand in conversion", Operand);
13726 -- If the interpretation involves a standard operator, use
13727 -- the location of the type, which may be user-defined.
13729 if Sloc (It.Nam) = Standard_Location then
13730 Error_Msg_Sloc := Sloc (It.Typ);
13731 else
13732 Error_Msg_Sloc := Sloc (It.Nam);
13733 end if;
13735 Conversion_Error_N -- CODEFIX
13736 ("\\possible interpretation#!", Operand);
13738 if Sloc (N1) = Standard_Location then
13739 Error_Msg_Sloc := Sloc (T1);
13740 else
13741 Error_Msg_Sloc := Sloc (N1);
13742 end if;
13744 Conversion_Error_N -- CODEFIX
13745 ("\\possible interpretation#!", Operand);
13747 return False;
13748 end if;
13749 end if;
13751 Set_Etype (Operand, It1.Typ);
13752 Opnd_Type := It1.Typ;
13753 end;
13754 end if;
13756 -- Deal with conversion of integer type to address if the pragma
13757 -- Allow_Integer_Address is in effect. We convert the conversion to
13758 -- an unchecked conversion in this case and we are all done.
13760 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
13761 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
13762 Analyze_And_Resolve (N, Target_Type);
13763 return True;
13764 end if;
13766 -- If we are within a child unit, check whether the type of the
13767 -- expression has an ancestor in a parent unit, in which case it
13768 -- belongs to its derivation class even if the ancestor is private.
13769 -- See RM 7.3.1 (5.2/3).
13771 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
13773 -- Numeric types
13775 if Is_Numeric_Type (Target_Type) then
13777 -- A universal fixed expression can be converted to any numeric type
13779 if Opnd_Type = Universal_Fixed then
13780 return True;
13782 -- Also no need to check when in an instance or inlined body, because
13783 -- the legality has been established when the template was analyzed.
13784 -- Furthermore, numeric conversions may occur where only a private
13785 -- view of the operand type is visible at the instantiation point.
13786 -- This results in a spurious error if we check that the operand type
13787 -- is a numeric type.
13789 -- Note: in a previous version of this unit, the following tests were
13790 -- applied only for generated code (Comes_From_Source set to False),
13791 -- but in fact the test is required for source code as well, since
13792 -- this situation can arise in source code.
13794 elsif In_Instance_Code or else In_Inlined_Body then
13795 return True;
13797 -- Otherwise we need the conversion check
13799 else
13800 return Conversion_Check
13801 (Is_Numeric_Type (Opnd_Type)
13802 or else
13803 (Present (Inc_Ancestor)
13804 and then Is_Numeric_Type (Inc_Ancestor)),
13805 "illegal operand for numeric conversion");
13806 end if;
13808 -- Array types
13810 elsif Is_Array_Type (Target_Type) then
13811 if not Is_Array_Type (Opnd_Type)
13812 or else Opnd_Type = Any_Composite
13813 or else Opnd_Type = Any_String
13814 then
13815 Conversion_Error_N
13816 ("illegal operand for array conversion", Operand);
13817 return False;
13819 else
13820 return Valid_Array_Conversion;
13821 end if;
13823 -- Ada 2005 (AI-251): Internally generated conversions of access to
13824 -- interface types added to force the displacement of the pointer to
13825 -- reference the corresponding dispatch table.
13827 elsif not Comes_From_Source (N)
13828 and then Is_Access_Type (Target_Type)
13829 and then Is_Interface (Designated_Type (Target_Type))
13830 then
13831 return True;
13833 -- Ada 2005 (AI-251): Anonymous access types where target references an
13834 -- interface type.
13836 elsif Is_Access_Type (Opnd_Type)
13837 and then Ekind (Target_Type) in
13838 E_General_Access_Type | E_Anonymous_Access_Type
13839 and then Is_Interface (Directly_Designated_Type (Target_Type))
13840 then
13841 -- Check the static accessibility rule of 4.6(17). Note that the
13842 -- check is not enforced when within an instance body, since the
13843 -- RM requires such cases to be caught at run time.
13845 -- If the operand is a rewriting of an allocator no check is needed
13846 -- because there are no accessibility issues.
13848 if Nkind (Original_Node (N)) = N_Allocator then
13849 null;
13851 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
13852 if Type_Access_Level (Opnd_Type) >
13853 Deepest_Type_Access_Level (Target_Type)
13854 then
13855 -- In an instance, this is a run-time check, but one we know
13856 -- will fail, so generate an appropriate warning. The raise
13857 -- will be generated by Expand_N_Type_Conversion.
13859 if In_Instance_Body then
13860 Error_Msg_Warn := SPARK_Mode /= On;
13861 Conversion_Error_N
13862 ("cannot convert local pointer to non-local access type<<",
13863 Operand);
13864 Conversion_Error_N ("\Program_Error [<<", Operand);
13866 else
13867 Conversion_Error_N
13868 ("cannot convert local pointer to non-local access type",
13869 Operand);
13870 return False;
13871 end if;
13873 -- Special accessibility checks are needed in the case of access
13874 -- discriminants declared for a limited type.
13876 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13877 and then not Is_Local_Anonymous_Access (Opnd_Type)
13878 then
13879 -- When the operand is a selected access discriminant the check
13880 -- needs to be made against the level of the object denoted by
13881 -- the prefix of the selected name (Accessibility_Level handles
13882 -- checking the prefix of the operand for this case).
13884 if Nkind (Operand) = N_Selected_Component
13885 and then Static_Accessibility_Level
13886 (Operand, Zero_On_Dynamic_Level)
13887 > Deepest_Type_Access_Level (Target_Type)
13888 then
13889 -- In an instance, this is a run-time check, but one we know
13890 -- will fail, so generate an appropriate warning. The raise
13891 -- will be generated by Expand_N_Type_Conversion.
13893 if In_Instance_Body then
13894 Error_Msg_Warn := SPARK_Mode /= On;
13895 Conversion_Error_N
13896 ("cannot convert access discriminant to non-local "
13897 & "access type<<", Operand);
13898 Conversion_Error_N ("\Program_Error [<<", Operand);
13900 -- Real error if not in instance body
13902 else
13903 Conversion_Error_N
13904 ("cannot convert access discriminant to non-local "
13905 & "access type", Operand);
13906 return False;
13907 end if;
13908 end if;
13910 -- The case of a reference to an access discriminant from
13911 -- within a limited type declaration (which will appear as
13912 -- a discriminal) is always illegal because the level of the
13913 -- discriminant is considered to be deeper than any (nameable)
13914 -- access type.
13916 if Is_Entity_Name (Operand)
13917 and then not Is_Local_Anonymous_Access (Opnd_Type)
13918 and then
13919 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
13920 and then Present (Discriminal_Link (Entity (Operand)))
13921 then
13922 Conversion_Error_N
13923 ("discriminant has deeper accessibility level than target",
13924 Operand);
13925 return False;
13926 end if;
13927 end if;
13928 end if;
13930 return True;
13932 -- General and anonymous access types
13934 elsif Ekind (Target_Type) in
13935 E_General_Access_Type | E_Anonymous_Access_Type
13936 and then
13937 Conversion_Check
13938 (Is_Access_Type (Opnd_Type)
13939 and then
13940 Ekind (Opnd_Type) not in
13941 E_Access_Subprogram_Type |
13942 E_Access_Protected_Subprogram_Type,
13943 "must be an access-to-object type")
13944 then
13945 if Is_Access_Constant (Opnd_Type)
13946 and then not Is_Access_Constant (Target_Type)
13947 then
13948 Conversion_Error_N
13949 ("access-to-constant operand type not allowed", Operand);
13950 return False;
13951 end if;
13953 -- Check the static accessibility rule of 4.6(17). Note that the
13954 -- check is not enforced when within an instance body, since the RM
13955 -- requires such cases to be caught at run time.
13957 if Ekind (Target_Type) /= E_Anonymous_Access_Type
13958 or else Is_Local_Anonymous_Access (Target_Type)
13959 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
13960 N_Object_Declaration
13961 then
13962 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
13963 -- conversions from an anonymous access type to a named general
13964 -- access type. Such conversions are not allowed in the case of
13965 -- access parameters and stand-alone objects of an anonymous
13966 -- access type. The implicit conversion case is recognized by
13967 -- testing that Comes_From_Source is False and that it's been
13968 -- rewritten. The Comes_From_Source test isn't sufficient because
13969 -- nodes in inlined calls to predefined library routines can have
13970 -- Comes_From_Source set to False. (Is there a better way to test
13971 -- for implicit conversions???).
13973 -- Do not treat a rewritten 'Old attribute reference like other
13974 -- rewrite substitutions. This makes a difference, for example,
13975 -- in the case where we are generating the expansion of a
13976 -- membership test of the form
13977 -- Saooaaat'Old in Named_Access_Type
13978 -- because in this case Valid_Conversion needs to return True
13979 -- (otherwise the expansion will be False - see the call site
13980 -- in exp_ch4.adb).
13982 if Ada_Version >= Ada_2012
13983 and then not Comes_From_Source (N)
13984 and then Is_Rewrite_Substitution (N)
13985 and then not Is_Attribute_Old (Original_Node (N))
13986 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
13987 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
13988 then
13989 if Is_Itype (Opnd_Type) then
13991 -- When applying restriction No_Dynamic_Accessibility_Check,
13992 -- implicit conversions are allowed when the operand type is
13993 -- not deeper than the target type.
13995 if No_Dynamic_Accessibility_Checks_Enabled (N) then
13996 if Type_Access_Level (Opnd_Type)
13997 > Deepest_Type_Access_Level (Target_Type)
13998 then
13999 Conversion_Error_N
14000 ("operand has deeper level than target", Operand);
14001 end if;
14003 -- Implicit conversions aren't allowed for objects of an
14004 -- anonymous access type, since such objects have nonstatic
14005 -- levels in Ada 2012.
14007 elsif Nkind (Associated_Node_For_Itype (Opnd_Type))
14008 = N_Object_Declaration
14009 then
14010 Conversion_Error_N
14011 ("implicit conversion of stand-alone anonymous "
14012 & "access object not allowed", Operand);
14013 return False;
14015 -- Implicit conversions aren't allowed for anonymous access
14016 -- parameters. We exclude anonymous access results as well
14017 -- as universal_access "=".
14019 elsif not Is_Local_Anonymous_Access (Opnd_Type)
14020 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
14021 N_Function_Specification |
14022 N_Procedure_Specification
14023 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
14024 then
14025 Conversion_Error_N
14026 ("implicit conversion of anonymous access parameter "
14027 & "not allowed", Operand);
14028 return False;
14030 -- Detect access discriminant values that are illegal
14031 -- implicit anonymous-to-named access conversion operands.
14033 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
14034 then
14035 Conversion_Error_N
14036 ("implicit conversion of anonymous access value "
14037 & "not allowed", Operand);
14038 return False;
14040 -- In other cases, the level of the operand's type must be
14041 -- statically less deep than that of the target type, else
14042 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
14044 elsif Type_Access_Level (Opnd_Type) >
14045 Deepest_Type_Access_Level (Target_Type)
14046 then
14047 Conversion_Error_N
14048 ("implicit conversion of anonymous access value "
14049 & "violates accessibility", Operand);
14050 return False;
14051 end if;
14052 end if;
14054 -- Check if the operand is deeper than the target type, taking
14055 -- care to avoid the case where we are converting a result of a
14056 -- function returning an anonymous access type since the "master
14057 -- of the call" would be target type of the conversion unless
14058 -- the target type is anonymous access as well - see RM 3.10.2
14059 -- (10.3/3).
14061 -- Note that when the restriction No_Dynamic_Accessibility_Checks
14062 -- is in effect wei also want to proceed with the conversion check
14063 -- described above.
14065 elsif Type_Access_Level (Opnd_Type, Assoc_Ent => Operand)
14066 > Deepest_Type_Access_Level (Target_Type)
14067 and then (Nkind (Associated_Node_For_Itype (Opnd_Type))
14068 /= N_Function_Specification
14069 or else Ekind (Target_Type) in Anonymous_Access_Kind
14070 or else No_Dynamic_Accessibility_Checks_Enabled (N))
14072 -- Check we are not in a return value ???
14074 and then (not In_Return_Value (N)
14075 or else
14076 Nkind (Associated_Node_For_Itype (Target_Type))
14077 = N_Component_Declaration)
14078 then
14079 -- In an instance, this is a run-time check, but one we know
14080 -- will fail, so generate an appropriate warning. The raise
14081 -- will be generated by Expand_N_Type_Conversion.
14083 if In_Instance_Body then
14084 Error_Msg_Warn := SPARK_Mode /= On;
14085 Conversion_Error_N
14086 ("cannot convert local pointer to non-local access type<<",
14087 Operand);
14088 Conversion_Error_N ("\Program_Error [<<", Operand);
14090 -- If not in an instance body, this is a real error
14092 else
14093 -- Avoid generation of spurious error message
14095 if not Error_Posted (N) then
14096 Conversion_Error_N
14097 ("cannot convert local pointer to non-local access type",
14098 Operand);
14099 end if;
14101 return False;
14102 end if;
14104 -- Special accessibility checks are needed in the case of access
14105 -- discriminants declared for a limited type.
14107 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14108 and then not Is_Local_Anonymous_Access (Opnd_Type)
14109 then
14110 -- When the operand is a selected access discriminant the check
14111 -- needs to be made against the level of the object denoted by
14112 -- the prefix of the selected name (Accessibility_Level handles
14113 -- checking the prefix of the operand for this case).
14115 if Nkind (Operand) = N_Selected_Component
14116 and then Static_Accessibility_Level
14117 (Operand, Zero_On_Dynamic_Level)
14118 > Deepest_Type_Access_Level (Target_Type)
14119 then
14120 -- In an instance, this is a run-time check, but one we know
14121 -- will fail, so generate an appropriate warning. The raise
14122 -- will be generated by Expand_N_Type_Conversion.
14124 if In_Instance_Body then
14125 Error_Msg_Warn := SPARK_Mode /= On;
14126 Conversion_Error_N
14127 ("cannot convert access discriminant to non-local "
14128 & "access type<<", Operand);
14129 Conversion_Error_N ("\Program_Error [<<", Operand);
14131 -- If not in an instance body, this is a real error
14133 else
14134 Conversion_Error_N
14135 ("cannot convert access discriminant to non-local "
14136 & "access type", Operand);
14137 return False;
14138 end if;
14139 end if;
14141 -- The case of a reference to an access discriminant from
14142 -- within a limited type declaration (which will appear as
14143 -- a discriminal) is always illegal because the level of the
14144 -- discriminant is considered to be deeper than any (nameable)
14145 -- access type.
14147 if Is_Entity_Name (Operand)
14148 and then
14149 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14150 and then Present (Discriminal_Link (Entity (Operand)))
14151 then
14152 Conversion_Error_N
14153 ("discriminant has deeper accessibility level than target",
14154 Operand);
14155 return False;
14156 end if;
14157 end if;
14158 end if;
14160 -- In the presence of limited_with clauses we have to use nonlimited
14161 -- views, if available.
14163 Check_Limited : declare
14164 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
14165 -- Helper function to handle limited views
14167 --------------------------
14168 -- Full_Designated_Type --
14169 --------------------------
14171 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
14172 Desig : constant Entity_Id := Designated_Type (T);
14174 begin
14175 -- Handle the limited view of a type
14177 if From_Limited_With (Desig)
14178 and then Has_Non_Limited_View (Desig)
14179 then
14180 return Available_View (Desig);
14181 else
14182 return Desig;
14183 end if;
14184 end Full_Designated_Type;
14186 -- Local Declarations
14188 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
14189 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
14191 Same_Base : constant Boolean :=
14192 Base_Type (Target) = Base_Type (Opnd);
14194 -- Start of processing for Check_Limited
14196 begin
14197 if Is_Tagged_Type (Target) then
14198 return Valid_Tagged_Conversion (Target, Opnd);
14200 else
14201 if not Same_Base then
14202 Conversion_Error_NE
14203 ("target designated type not compatible with }",
14204 N, Base_Type (Opnd));
14205 return False;
14207 -- Ada 2005 AI-384: legality rule is symmetric in both
14208 -- designated types. The conversion is legal (with possible
14209 -- constraint check) if either designated type is
14210 -- unconstrained.
14212 elsif Subtypes_Statically_Match (Target, Opnd)
14213 or else
14214 (Has_Discriminants (Target)
14215 and then
14216 (not Is_Constrained (Opnd)
14217 or else not Is_Constrained (Target)))
14218 then
14219 -- Special case, if Value_Size has been used to make the
14220 -- sizes different, the conversion is not allowed even
14221 -- though the subtypes statically match.
14223 if Known_Static_RM_Size (Target)
14224 and then Known_Static_RM_Size (Opnd)
14225 and then RM_Size (Target) /= RM_Size (Opnd)
14226 then
14227 Conversion_Error_NE
14228 ("target designated subtype not compatible with }",
14229 N, Opnd);
14230 Conversion_Error_NE
14231 ("\because sizes of the two designated subtypes differ",
14232 N, Opnd);
14233 return False;
14235 -- Normal case where conversion is allowed
14237 else
14238 return True;
14239 end if;
14241 else
14242 Error_Msg_NE
14243 ("target designated subtype not compatible with }",
14244 N, Opnd);
14245 return False;
14246 end if;
14247 end if;
14248 end Check_Limited;
14250 -- Access to subprogram types. If the operand is an access parameter,
14251 -- the type has a deeper accessibility that any master, and cannot be
14252 -- assigned. We must make an exception if the conversion is part of an
14253 -- assignment and the target is the return object of an extended return
14254 -- statement, because in that case the accessibility check takes place
14255 -- after the return.
14257 elsif Is_Access_Subprogram_Type (Target_Type)
14259 -- Note: this test of Opnd_Type is there to prevent entering this
14260 -- branch in the case of a remote access to subprogram type, which
14261 -- is internally represented as an E_Record_Type.
14263 and then Is_Access_Type (Opnd_Type)
14264 then
14265 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
14266 and then Is_Entity_Name (Operand)
14267 and then Ekind (Entity (Operand)) = E_In_Parameter
14268 and then
14269 (Nkind (Parent (N)) /= N_Assignment_Statement
14270 or else not Is_Entity_Name (Name (Parent (N)))
14271 or else not Is_Return_Object (Entity (Name (Parent (N)))))
14272 then
14273 Conversion_Error_N
14274 ("illegal attempt to store anonymous access to subprogram",
14275 Operand);
14276 Conversion_Error_N
14277 ("\value has deeper accessibility than any master "
14278 & "(RM 3.10.2 (13))",
14279 Operand);
14281 Error_Msg_NE
14282 ("\use named access type for& instead of access parameter",
14283 Operand, Entity (Operand));
14284 end if;
14286 -- Check that the designated types are subtype conformant
14288 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
14289 Old_Id => Designated_Type (Opnd_Type),
14290 Err_Loc => N);
14292 -- Check the static accessibility rule of 4.6(20)
14294 if Type_Access_Level (Opnd_Type) >
14295 Deepest_Type_Access_Level (Target_Type)
14296 then
14297 Conversion_Error_N
14298 ("operand type has deeper accessibility level than target",
14299 Operand);
14301 -- Check that if the operand type is declared in a generic body,
14302 -- then the target type must be declared within that same body
14303 -- (enforces last sentence of 4.6(20)).
14305 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
14306 declare
14307 O_Gen : constant Node_Id :=
14308 Enclosing_Generic_Body (Opnd_Type);
14310 T_Gen : Node_Id;
14312 begin
14313 T_Gen := Enclosing_Generic_Body (Target_Type);
14314 while Present (T_Gen) and then T_Gen /= O_Gen loop
14315 T_Gen := Enclosing_Generic_Body (T_Gen);
14316 end loop;
14318 if T_Gen /= O_Gen then
14319 Conversion_Error_N
14320 ("target type must be declared in same generic body "
14321 & "as operand type", N);
14322 end if;
14323 end;
14324 end if;
14326 -- Check that the strub modes are compatible.
14327 -- We wish to reject explicit conversions only for
14328 -- incompatible modes.
14330 return Conversion_Check
14331 (Compatible_Strub_Modes
14332 (Designated_Type (Target_Type),
14333 Designated_Type (Opnd_Type)),
14334 "incompatible `strub` modes");
14336 -- Remote access to subprogram types
14338 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
14339 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
14340 then
14341 -- It is valid to convert from one RAS type to another provided
14342 -- that their specification statically match.
14344 -- Note: at this point, remote access to subprogram types have been
14345 -- expanded to their E_Record_Type representation, and we need to
14346 -- go back to the original access type definition using the
14347 -- Corresponding_Remote_Type attribute in order to check that the
14348 -- designated profiles match.
14350 pragma Assert (Ekind (Target_Type) = E_Record_Type);
14351 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
14353 Check_Subtype_Conformant
14354 (New_Id =>
14355 Designated_Type (Corresponding_Remote_Type (Target_Type)),
14356 Old_Id =>
14357 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14358 Err_Loc =>
14361 -- Check that the strub modes are compatible.
14362 -- We wish to reject explicit conversions only for
14363 -- incompatible modes.
14365 return Conversion_Check
14366 (Compatible_Strub_Modes
14367 (Designated_Type (Target_Type),
14368 Designated_Type (Opnd_Type)),
14369 "incompatible `strub` modes");
14371 -- If it was legal in the generic, it's legal in the instance
14373 elsif In_Instance_Body then
14374 return True;
14376 -- If both are tagged types, check legality of view conversions
14378 elsif Is_Tagged_Type (Target_Type)
14379 and then
14380 Is_Tagged_Type (Opnd_Type)
14381 then
14382 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14384 -- Types derived from the same root type are convertible
14386 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14387 return True;
14389 -- In an instance or an inlined body, there may be inconsistent views of
14390 -- the same type, or of types derived from a common root.
14392 elsif (In_Instance or In_Inlined_Body)
14393 and then
14394 Root_Type (Underlying_Type (Target_Type)) =
14395 Root_Type (Underlying_Type (Opnd_Type))
14396 then
14397 return True;
14399 -- Special check for common access type error case
14401 elsif Ekind (Target_Type) = E_Access_Type
14402 and then Is_Access_Type (Opnd_Type)
14403 then
14404 Conversion_Error_N ("target type must be general access type!", N);
14405 Conversion_Error_NE -- CODEFIX
14406 ("\add ALL to }!", N, Target_Type);
14407 return False;
14409 -- Here we have a real conversion error
14411 else
14412 -- Check for missing regular with_clause when only a limited view of
14413 -- target is available.
14415 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14416 Conversion_Error_NE
14417 ("invalid conversion, not compatible with limited view of }",
14418 N, Opnd_Type);
14419 Conversion_Error_NE
14420 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14422 elsif Is_Access_Type (Opnd_Type)
14423 and then From_Limited_With (Designated_Type (Opnd_Type))
14424 and then In_Package_Body
14425 then
14426 Conversion_Error_NE
14427 ("invalid conversion, not compatible with }", N, Opnd_Type);
14428 Conversion_Error_NE
14429 ("\add with_clause for& to current unit!",
14430 N, Scope (Designated_Type (Opnd_Type)));
14432 else
14433 Conversion_Error_NE
14434 ("invalid conversion, not compatible with }", N, Opnd_Type);
14435 end if;
14437 return False;
14438 end if;
14439 end Valid_Conversion;
14441 end Sem_Res;