Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_res.adb
blobdf9ccb1846837e1572a7a336c5dc64b5980a44b2
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ R E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Debug_A; use Debug_A;
32 with Einfo; use Einfo;
33 with Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Elists; use Elists;
36 with Errout; use Errout;
37 with Expander; use Expander;
38 with Exp_Ch6; use Exp_Ch6;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Inline; use Inline;
46 with Itypes; use Itypes;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nmake; use Nmake;
51 with Nlists; use Nlists;
52 with Opt; use Opt;
53 with Output; use Output;
54 with Par_SCO; use Par_SCO;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aggr; use Sem_Aggr;
60 with Sem_Attr; use Sem_Attr;
61 with Sem_Aux; use Sem_Aux;
62 with Sem_Case; use Sem_Case;
63 with Sem_Cat; use Sem_Cat;
64 with Sem_Ch3; use Sem_Ch3;
65 with Sem_Ch4; use Sem_Ch4;
66 with Sem_Ch5; use Sem_Ch5;
67 with Sem_Ch6; use Sem_Ch6;
68 with Sem_Ch8; use Sem_Ch8;
69 with Sem_Ch13; use Sem_Ch13;
70 with Sem_Dim; use Sem_Dim;
71 with Sem_Disp; use Sem_Disp;
72 with Sem_Dist; use Sem_Dist;
73 with Sem_Elab; use Sem_Elab;
74 with Sem_Elim; use Sem_Elim;
75 with Sem_Eval; use Sem_Eval;
76 with Sem_Intr; use Sem_Intr;
77 with Sem_Mech; use Sem_Mech;
78 with Sem_Type; use Sem_Type;
79 with Sem_Util; use Sem_Util;
80 with Sem_Warn; use Sem_Warn;
81 with Sinfo; use Sinfo;
82 with Sinfo.Nodes; use Sinfo.Nodes;
83 with Sinfo.Utils; use Sinfo.Utils;
84 with Sinfo.CN; use Sinfo.CN;
85 with Snames; use Snames;
86 with Stand; use Stand;
87 with Stringt; use Stringt;
88 with Strub; use Strub;
89 with Style; use Style;
90 with Targparm; use Targparm;
91 with Tbuild; use Tbuild;
92 with Uintp; use Uintp;
93 with Urealp; use Urealp;
94 with Warnsw; use Warnsw;
96 package body Sem_Res is
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 -- Second pass (top-down) type checking and overload resolution procedures
103 -- Typ is the type required by context. These procedures propagate the
104 -- type information recursively to the descendants of N. If the node is not
105 -- overloaded, its Etype is established in the first pass. If overloaded,
106 -- the Resolve routines set the correct type. For arithmetic operators, the
107 -- Etype is the base type of the context.
109 -- Note that Resolve_Attribute is separated off in Sem_Attr
111 function Has_Applicable_User_Defined_Literal
112 (N : Node_Id;
113 Typ : Entity_Id) return Boolean;
114 -- If N is a literal or a named number, check whether Typ
115 -- has a user-defined literal aspect that can apply to N.
116 -- If present, replace N with a call to the corresponding
117 -- function and return True.
119 procedure Check_Discriminant_Use (N : Node_Id);
120 -- Enforce the restrictions on the use of discriminants when constraining
121 -- a component of a discriminated type (record or concurrent type).
123 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
124 -- Given a node for an operator associated with type T, check that the
125 -- operator is visible. Operators all of whose operands are universal must
126 -- be checked for visibility during resolution because their type is not
127 -- determinable based on their operands.
129 procedure Check_Fully_Declared_Prefix
130 (Typ : Entity_Id;
131 Pref : Node_Id);
132 -- Check that the type of the prefix of a dereference is not incomplete
134 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
135 -- Given a call node, Call, which is known to occur immediately within the
136 -- subprogram being called, determines whether it is a detectable case of
137 -- an infinite recursion, and if so, outputs appropriate messages. Returns
138 -- True if an infinite recursion is detected, and False otherwise.
140 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
141 -- N is the node for a logical operator. If the operator is predefined, and
142 -- the root type of the operands is Standard.Boolean, then a check is made
143 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
144 -- the style check for Style_Check_Boolean_And_Or.
146 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
147 -- N is either an indexed component or a selected component. This function
148 -- returns true if the prefix denotes an atomic object that has an address
149 -- clause (the case in which we may want to issue a warning).
151 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean;
152 -- Determine whether E is an access type declared by an access declaration,
153 -- and not an (anonymous) allocator type.
155 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
156 -- Utility to check whether the entity for an operator is a predefined
157 -- operator, in which case the expression is left as an operator in the
158 -- tree (else it is rewritten into a call). An instance of an intrinsic
159 -- conversion operation may be given an operator name, but is not treated
160 -- like an operator. Note that an operator that is an imported back-end
161 -- builtin has convention Intrinsic, but is expected to be rewritten into
162 -- a call, so such an operator is not treated as predefined by this
163 -- predicate.
165 procedure Preanalyze_And_Resolve
166 (N : Node_Id;
167 T : Entity_Id;
168 With_Freezing : Boolean);
169 -- Subsidiary of public versions of Preanalyze_And_Resolve.
171 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
172 -- If a default expression in entry call N depends on the discriminants
173 -- of the task, it must be replaced with a reference to the discriminant
174 -- of the task being called.
176 procedure Resolve_Dependent_Expression
177 (N : Node_Id;
178 Expr : Node_Id;
179 Typ : Entity_Id);
180 -- Internal procedure to resolve the dependent expression Expr of the
181 -- conditional expression N with type Typ.
183 procedure Resolve_Op_Concat_Arg
184 (N : Node_Id;
185 Arg : Node_Id;
186 Typ : Entity_Id;
187 Is_Comp : Boolean);
188 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
189 -- concatenation operator. The operand is either of the array type or of
190 -- the component type. If the operand is an aggregate, and the component
191 -- type is composite, this is ambiguous if component type has aggregates.
193 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
194 -- Does the first part of the work of Resolve_Op_Concat
196 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
197 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
198 -- has been resolved. See Resolve_Op_Concat for details.
200 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
215 procedure Resolve_Interpolated_String_Literal
216 (N : Node_Id;
217 Typ : Entity_Id);
218 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
219 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
220 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
221 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
222 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
223 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
224 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
225 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
226 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
227 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
228 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
229 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
230 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
231 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
232 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
233 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
234 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
235 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
236 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
237 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
238 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
239 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
241 function Operator_Kind
242 (Op_Name : Name_Id;
243 Is_Binary : Boolean) return Node_Kind;
244 -- Utility to map the name of an operator into the corresponding Node. Used
245 -- by other node rewriting procedures.
247 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
248 -- Resolve actuals of call, and add default expressions for missing ones.
249 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
250 -- called subprogram.
252 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
253 -- Called from Resolve_Call, when the prefix denotes an entry or element
254 -- of entry family. Actuals are resolved as for subprograms, and the node
255 -- is rebuilt as an entry call. Also called for protected operations. Typ
256 -- is the context type, which is used when the operation is a protected
257 -- function with no arguments, and the return value is indexed.
259 procedure Resolve_Implicit_Dereference (P : Node_Id);
260 -- Called when P is the prefix of an indexed component, or of a selected
261 -- component, or of a slice. If P is of an access type, we unconditionally
262 -- rewrite it as an explicit dereference. This ensures that the expander
263 -- and the code generator have a fully explicit tree to work with.
265 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
266 -- A call to a user-defined intrinsic operator is rewritten as a call to
267 -- the corresponding predefined operator, with suitable conversions. Note
268 -- that this applies only for intrinsic operators that denote predefined
269 -- operators, not ones that are intrinsic imports of back-end builtins.
271 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
272 -- Ditto, for arithmetic unary operators
274 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
275 -- If an operator node resolves to a call to a user-defined operator,
276 -- rewrite the node as a function call.
278 procedure Make_Call_Into_Operator
279 (N : Node_Id;
280 Typ : Entity_Id;
281 Op_Id : Entity_Id);
282 -- Inverse transformation: if an operator is given in functional notation,
283 -- then after resolving the node, transform into an operator node, so that
284 -- operands are resolved properly. Recall that predefined operators do not
285 -- have a full signature and special resolution rules apply.
287 procedure Rewrite_Renamed_Operator
288 (N : Node_Id;
289 Op : Entity_Id;
290 Typ : Entity_Id);
291 -- An operator can rename another, e.g. in an instantiation. In that
292 -- case, the proper operator node must be constructed and resolved.
294 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
295 -- The String_Literal_Subtype is built for all strings that are not
296 -- operands of a static concatenation operation. If the argument is not
297 -- a N_String_Literal node, then the call has no effect.
299 procedure Set_Slice_Subtype (N : Node_Id);
300 -- Build subtype of array type, with the range specified by the slice
302 procedure Simplify_Type_Conversion (N : Node_Id);
303 -- Called after N has been resolved and evaluated, but before range checks
304 -- have been applied. This rewrites the conversion into a simpler form.
306 function Try_User_Defined_Literal
307 (N : Node_Id;
308 Typ : Entity_Id) return Boolean;
309 -- If an operator node has a literal operand, check whether the type
310 -- of the context, or the type of the other operand has a user-defined
311 -- literal aspect that can be applied to the literal to resolve the node.
312 -- If such aspect exists, replace literal with a call to the
313 -- corresponding function and return True, return false otherwise.
315 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
316 -- A universal_fixed expression in an universal context is unambiguous if
317 -- there is only one applicable fixed point type. Determining whether there
318 -- is only one requires a search over all visible entities, and happens
319 -- only in very pathological cases (see 6115-006).
321 -------------------------
322 -- Ambiguous_Character --
323 -------------------------
325 procedure Ambiguous_Character (C : Node_Id) is
326 E : Entity_Id;
328 begin
329 if Nkind (C) = N_Character_Literal then
330 Error_Msg_N ("ambiguous character literal", C);
332 -- First the ones in Standard
334 Error_Msg_N ("\\possible interpretation: Character!", C);
335 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
337 -- Include Wide_Wide_Character in Ada 2005 mode
339 if Ada_Version >= Ada_2005 then
340 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
341 end if;
343 -- Now any other types that match
345 E := Current_Entity (C);
346 while Present (E) loop
347 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
348 E := Homonym (E);
349 end loop;
350 end if;
351 end Ambiguous_Character;
353 -------------------------
354 -- Analyze_And_Resolve --
355 -------------------------
357 procedure Analyze_And_Resolve (N : Node_Id) is
358 begin
359 Analyze (N);
360 Resolve (N);
361 end Analyze_And_Resolve;
363 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
364 begin
365 Analyze (N);
366 Resolve (N, Typ);
367 end Analyze_And_Resolve;
369 -- Versions with check(s) suppressed
371 procedure Analyze_And_Resolve
372 (N : Node_Id;
373 Typ : Entity_Id;
374 Suppress : Check_Id)
376 Scop : constant Entity_Id := Current_Scope;
378 begin
379 if Suppress = All_Checks then
380 declare
381 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
382 begin
383 Scope_Suppress.Suppress := (others => True);
384 Analyze_And_Resolve (N, Typ);
385 Scope_Suppress.Suppress := Sva;
386 end;
388 else
389 declare
390 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
391 begin
392 Scope_Suppress.Suppress (Suppress) := True;
393 Analyze_And_Resolve (N, Typ);
394 Scope_Suppress.Suppress (Suppress) := Svg;
395 end;
396 end if;
398 if Current_Scope /= Scop
399 and then Scope_Is_Transient
400 then
401 -- This can only happen if a transient scope was created for an inner
402 -- expression, which will be removed upon completion of the analysis
403 -- of an enclosing construct. The transient scope must have the
404 -- suppress status of the enclosing environment, not of this Analyze
405 -- call.
407 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
408 Scope_Suppress;
409 end if;
410 end Analyze_And_Resolve;
412 procedure Analyze_And_Resolve
413 (N : Node_Id;
414 Suppress : Check_Id)
416 Scop : constant Entity_Id := Current_Scope;
418 begin
419 if Suppress = All_Checks then
420 declare
421 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
422 begin
423 Scope_Suppress.Suppress := (others => True);
424 Analyze_And_Resolve (N);
425 Scope_Suppress.Suppress := Sva;
426 end;
428 else
429 declare
430 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
431 begin
432 Scope_Suppress.Suppress (Suppress) := True;
433 Analyze_And_Resolve (N);
434 Scope_Suppress.Suppress (Suppress) := Svg;
435 end;
436 end if;
438 if Current_Scope /= Scop and then Scope_Is_Transient then
439 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
440 Scope_Suppress;
441 end if;
442 end Analyze_And_Resolve;
444 -------------------------------------
445 -- Has_Applicable_User_Defined_Literal --
446 -------------------------------------
448 function Has_Applicable_User_Defined_Literal
449 (N : Node_Id;
450 Typ : Entity_Id) return Boolean
452 Loc : constant Source_Ptr := Sloc (N);
453 Literal_Aspect_Map :
454 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
455 (N_Integer_Literal => Aspect_Integer_Literal,
456 N_Interpolated_String_Literal => No_Aspect,
457 N_Real_Literal => Aspect_Real_Literal,
458 N_String_Literal => Aspect_String_Literal);
460 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
461 (E_Named_Integer => Aspect_Integer_Literal,
462 E_Named_Real => Aspect_Real_Literal);
464 Lit_Aspect : Aspect_Id;
466 Callee : Entity_Id;
467 Name : Node_Id;
468 Param1 : Node_Id;
469 Param2 : Node_Id;
470 Params : List_Id;
471 Call : Node_Id;
472 Expr : Node_Id;
474 begin
475 if (Nkind (N) in N_Numeric_Or_String_Literal
476 and then Present
477 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
478 or else
479 (Nkind (N) = N_Identifier
480 and then Is_Named_Number (Entity (N))
481 and then
482 Present
483 (Find_Aspect
484 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
485 then
486 Lit_Aspect :=
487 (if Nkind (N) = N_Identifier
488 then Named_Number_Aspect_Map (Ekind (Entity (N)))
489 else Literal_Aspect_Map (Nkind (N)));
490 Callee :=
491 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
492 Name := Make_Identifier (Loc, Chars (Callee));
494 if Is_Derived_Type (Typ)
495 and then Is_Tagged_Type (Typ)
496 and then Base_Type (Etype (Callee)) /= Base_Type (Typ)
497 then
498 Callee :=
499 Corresponding_Primitive_Op
500 (Ancestor_Op => Callee,
501 Descendant_Type => Base_Type (Typ));
502 end if;
504 -- Handle an identifier that denotes a named number.
506 if Nkind (N) = N_Identifier then
507 Expr := Expression (Declaration_Node (Entity (N)));
509 if Ekind (Entity (N)) = E_Named_Integer then
510 UI_Image (Expr_Value (Expr), Decimal);
511 Start_String;
512 Store_String_Chars
513 (UI_Image_Buffer (1 .. UI_Image_Length));
514 Param1 := Make_String_Literal (Loc, End_String);
515 Params := New_List (Param1);
517 else
518 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
519 Start_String;
521 if UR_Is_Negative (Expr_Value_R (Expr)) then
522 Store_String_Chars ("-");
523 end if;
525 Store_String_Chars
526 (UI_Image_Buffer (1 .. UI_Image_Length));
527 Param1 := Make_String_Literal (Loc, End_String);
529 -- Note: Set_Etype is called below on Param1
531 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
532 Start_String;
533 Store_String_Chars
534 (UI_Image_Buffer (1 .. UI_Image_Length));
535 Param2 := Make_String_Literal (Loc, End_String);
536 Set_Etype (Param2, Standard_String);
538 Params := New_List (Param1, Param2);
540 if Present (Related_Expression (Callee)) then
541 Callee := Related_Expression (Callee);
542 else
543 Error_Msg_NE
544 ("cannot resolve & for a named real", N, Callee);
545 return False;
546 end if;
547 end if;
549 elsif Nkind (N) = N_String_Literal then
550 Param1 := Make_String_Literal (Loc, Strval (N));
551 Params := New_List (Param1);
553 else
554 Param1 :=
555 Make_String_Literal
556 (Loc, String_From_Numeric_Literal (N));
557 Params := New_List (Param1);
558 end if;
560 Call :=
561 Make_Function_Call
562 (Sloc => Loc,
563 Name => Name,
564 Parameter_Associations => Params);
566 Set_Entity (Name, Callee);
567 Set_Is_Overloaded (Name, False);
569 if Lit_Aspect = Aspect_String_Literal then
570 Set_Etype (Param1, Standard_Wide_Wide_String);
571 else
572 Set_Etype (Param1, Standard_String);
573 end if;
575 Set_Etype (Call, Etype (Callee));
577 -- Conversion not needed if the result type of the call is class-wide
578 -- or if the result type matches the context type.
580 if not Is_Class_Wide_Type (Typ)
581 and then Base_Type (Etype (Call)) /= Base_Type (Typ)
582 then
583 -- Conversion may be needed in case of an inherited
584 -- aspect of a derived type. For a null extension, we
585 -- use a null extension aggregate instead because the
586 -- downward type conversion would be illegal.
588 if Is_Null_Extension_Of
589 (Descendant => Typ,
590 Ancestor => Etype (Call))
591 then
592 Call := Make_Extension_Aggregate (Loc,
593 Ancestor_Part => Call,
594 Null_Record_Present => True);
595 else
596 Call := Convert_To (Typ, Call);
597 end if;
598 end if;
600 Rewrite (N, Call);
602 Analyze_And_Resolve (N, Typ);
603 return True;
604 else
605 return False;
606 end if;
607 end Has_Applicable_User_Defined_Literal;
609 ----------------------------
610 -- Check_Discriminant_Use --
611 ----------------------------
613 procedure Check_Discriminant_Use (N : Node_Id) is
614 PN : constant Node_Id := Parent (N);
615 Disc : constant Entity_Id := Entity (N);
616 P : Node_Id;
617 D : Node_Id;
619 begin
620 -- Any use in a spec-expression is legal
622 if In_Spec_Expression then
623 null;
625 elsif Nkind (PN) = N_Range then
627 -- Discriminant cannot be used to constrain a scalar type
629 P := Parent (PN);
631 if Nkind (P) = N_Range_Constraint
632 and then Nkind (Parent (P)) = N_Subtype_Indication
633 and then Nkind (Parent (Parent (P))) = N_Component_Definition
634 then
635 Error_Msg_N ("discriminant cannot constrain scalar type", N);
637 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
639 -- The following check catches the unusual case where a
640 -- discriminant appears within an index constraint that is part
641 -- of a larger expression within a constraint on a component,
642 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
643 -- check case of record components, and note that a similar check
644 -- should also apply in the case of discriminant constraints
645 -- below. ???
647 -- Note that the check for N_Subtype_Declaration below is to
648 -- detect the valid use of discriminants in the constraints of a
649 -- subtype declaration when this subtype declaration appears
650 -- inside the scope of a record type (which is syntactically
651 -- illegal, but which may be created as part of derived type
652 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
653 -- for more info.
655 if Ekind (Current_Scope) = E_Record_Type
656 and then Scope (Disc) = Current_Scope
657 and then not
658 (Nkind (Parent (P)) = N_Subtype_Indication
659 and then
660 Nkind (Parent (Parent (P))) in N_Component_Definition
661 | N_Subtype_Declaration
662 and then Paren_Count (N) = 0)
663 then
664 Error_Msg_N
665 ("discriminant must appear alone in component constraint", N);
666 return;
667 end if;
669 -- Detect a common error:
671 -- type R (D : Positive := 100) is record
672 -- Name : String (1 .. D);
673 -- end record;
675 -- The default value causes an object of type R to be allocated
676 -- with room for Positive'Last characters. The RM does not mandate
677 -- the allocation of the maximum size, but that is what GNAT does
678 -- so we should warn the programmer that there is a problem.
680 Check_Large : declare
681 SI : Node_Id;
682 T : Entity_Id;
683 TB : Node_Id;
684 CB : Entity_Id;
686 function Large_Storage_Type (T : Entity_Id) return Boolean;
687 -- Return True if type T has a large enough range that any
688 -- array whose index type covered the whole range of the type
689 -- would likely raise Storage_Error.
691 ------------------------
692 -- Large_Storage_Type --
693 ------------------------
695 function Large_Storage_Type (T : Entity_Id) return Boolean is
696 begin
697 -- The type is considered large if its bounds are known at
698 -- compile time and if it requires at least as many bits as
699 -- a Positive to store the possible values.
701 return Compile_Time_Known_Value (Type_Low_Bound (T))
702 and then Compile_Time_Known_Value (Type_High_Bound (T))
703 and then
704 Minimum_Size (T, Biased => True) >=
705 RM_Size (Standard_Positive);
706 end Large_Storage_Type;
708 -- Start of processing for Check_Large
710 begin
711 -- Check that the Disc has a large range
713 if not Large_Storage_Type (Etype (Disc)) then
714 goto No_Danger;
715 end if;
717 -- If the enclosing type is limited, we allocate only the
718 -- default value, not the maximum, and there is no need for
719 -- a warning.
721 if Is_Limited_Type (Scope (Disc)) then
722 goto No_Danger;
723 end if;
725 -- Check that it is the high bound
727 if N /= High_Bound (PN)
728 or else No (Discriminant_Default_Value (Disc))
729 then
730 goto No_Danger;
731 end if;
733 -- Check the array allows a large range at this bound. First
734 -- find the array
736 SI := Parent (P);
738 if Nkind (SI) /= N_Subtype_Indication then
739 goto No_Danger;
740 end if;
742 T := Entity (Subtype_Mark (SI));
744 if not Is_Array_Type (T) then
745 goto No_Danger;
746 end if;
748 -- Next, find the dimension
750 TB := First_Index (T);
751 CB := First (Constraints (P));
752 while True
753 and then Present (TB)
754 and then Present (CB)
755 and then CB /= PN
756 loop
757 Next_Index (TB);
758 Next (CB);
759 end loop;
761 if CB /= PN then
762 goto No_Danger;
763 end if;
765 -- Now, check the dimension has a large range
767 if not Large_Storage_Type (Etype (TB)) then
768 goto No_Danger;
769 end if;
771 -- Warn about the danger
773 Error_Msg_N
774 ("??creation of & object may raise Storage_Error!",
775 Scope (Disc));
777 <<No_Danger>>
778 null;
780 end Check_Large;
781 end if;
783 -- Legal case is in index or discriminant constraint
785 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
786 | N_Discriminant_Association
787 then
788 if Paren_Count (N) > 0 then
789 Error_Msg_N
790 ("discriminant in constraint must appear alone", N);
792 elsif Nkind (N) = N_Expanded_Name
793 and then Comes_From_Source (N)
794 then
795 Error_Msg_N
796 ("discriminant must appear alone as a direct name", N);
797 end if;
799 return;
801 -- Otherwise, context is an expression. It should not be within (i.e. a
802 -- subexpression of) a constraint for a component.
804 else
805 D := PN;
806 P := Parent (PN);
807 while Nkind (P) not in
808 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
809 loop
810 D := P;
811 P := Parent (P);
812 exit when No (P);
813 end loop;
815 -- If the discriminant is used in an expression that is a bound of a
816 -- scalar type, an Itype is created and the bounds are attached to
817 -- its range, not to the original subtype indication. Such use is of
818 -- course a double fault.
820 if (Nkind (P) = N_Subtype_Indication
821 and then Nkind (Parent (P)) in N_Component_Definition
822 | N_Derived_Type_Definition
823 and then D = Constraint (P))
825 -- The constraint itself may be given by a subtype indication,
826 -- rather than by a more common discrete range.
828 or else (Nkind (P) = N_Subtype_Indication
829 and then
830 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
831 or else Nkind (P) = N_Entry_Declaration
832 or else Nkind (D) = N_Defining_Identifier
833 then
834 Error_Msg_N
835 ("discriminant in constraint must appear alone", N);
836 end if;
837 end if;
838 end Check_Discriminant_Use;
840 --------------------------------
841 -- Check_For_Visible_Operator --
842 --------------------------------
844 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
845 begin
846 if Comes_From_Source (N)
847 and then not Is_Visible_Operator (Original_Node (N), T)
848 and then not Error_Posted (N)
849 then
850 Error_Msg_NE -- CODEFIX
851 ("operator for} is not directly visible!", N, First_Subtype (T));
852 Error_Msg_N -- CODEFIX
853 ("use clause would make operation legal!", N);
854 end if;
855 end Check_For_Visible_Operator;
857 ---------------------------------
858 -- Check_Fully_Declared_Prefix --
859 ---------------------------------
861 procedure Check_Fully_Declared_Prefix
862 (Typ : Entity_Id;
863 Pref : Node_Id)
865 begin
866 -- Check that the designated type of the prefix of a dereference is
867 -- not an incomplete type. This cannot be done unconditionally, because
868 -- dereferences of private types are legal in default expressions. This
869 -- case is taken care of in Check_Fully_Declared, called below. There
870 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
872 -- This consideration also applies to similar checks for allocators,
873 -- qualified expressions, and type conversions.
875 -- An additional exception concerns other per-object expressions that
876 -- are not directly related to component declarations, in particular
877 -- representation pragmas for tasks. These will be per-object
878 -- expressions if they depend on discriminants or some global entity.
879 -- If the task has access discriminants, the designated type may be
880 -- incomplete at the point the expression is resolved. This resolution
881 -- takes place within the body of the initialization procedure, where
882 -- the discriminant is replaced by its discriminal.
884 if Is_Entity_Name (Pref)
885 and then Ekind (Entity (Pref)) = E_In_Parameter
886 then
887 null;
889 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
890 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
891 -- Analyze_Object_Renaming, and Freeze_Entity.
893 elsif Ada_Version >= Ada_2005
894 and then Is_Entity_Name (Pref)
895 and then Is_Access_Type (Etype (Pref))
896 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
897 E_Incomplete_Type
898 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
899 then
900 null;
901 else
902 Check_Fully_Declared (Typ, Parent (Pref));
903 end if;
904 end Check_Fully_Declared_Prefix;
906 ------------------------------
907 -- Check_Infinite_Recursion --
908 ------------------------------
910 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
911 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
912 -- Determine whether call N invokes the related enclosing subprogram
913 -- with actuals that differ from the subprogram's formals.
915 function Is_Conditional_Statement (N : Node_Id) return Boolean;
916 -- Determine whether arbitrary node N denotes a conditional construct
918 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
919 -- Determine whether arbitrary node N denotes a control flow statement
920 -- or a construct that may contains such a statement.
922 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
923 -- Determine whether arbitrary node N appears immediately within the
924 -- statements of an entry or subprogram body.
926 function Is_Raise_Idiom (N : Node_Id) return Boolean;
927 -- Determine whether arbitrary node N appears immediately within the
928 -- body of an entry or subprogram, and is preceded by a single raise
929 -- statement.
931 function Is_Raise_Statement (N : Node_Id) return Boolean;
932 -- Determine whether arbitrary node N denotes a raise statement
934 function Is_Sole_Statement (N : Node_Id) return Boolean;
935 -- Determine whether arbitrary node N is the sole source statement in
936 -- the body of the enclosing subprogram.
938 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
939 -- Determine whether arbitrary node N is preceded by a control flow
940 -- statement.
942 function Within_Conditional_Statement (N : Node_Id) return Boolean;
943 -- Determine whether arbitrary node N appears within a conditional
944 -- construct.
946 --------------------------------------
947 -- Invoked_With_Different_Arguments --
948 --------------------------------------
950 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
951 Subp : constant Entity_Id := Entity (Name (N));
953 Actual : Node_Id;
954 Formal : Entity_Id;
956 begin
957 -- Determine whether the formals of the invoked subprogram are not
958 -- used as actuals in the call.
960 Actual := First_Actual (Call);
961 Formal := First_Formal (Subp);
962 while Present (Actual) and then Present (Formal) loop
964 -- The current actual does not match the current formal
966 if not (Is_Entity_Name (Actual)
967 and then Entity (Actual) = Formal)
968 then
969 return True;
970 end if;
972 Next_Actual (Actual);
973 Next_Formal (Formal);
974 end loop;
976 return False;
977 end Invoked_With_Different_Arguments;
979 ------------------------------
980 -- Is_Conditional_Statement --
981 ------------------------------
983 function Is_Conditional_Statement (N : Node_Id) return Boolean is
984 begin
985 return
986 Nkind (N) in N_And_Then
987 | N_Case_Expression
988 | N_Case_Statement
989 | N_If_Expression
990 | N_If_Statement
991 | N_Or_Else;
992 end Is_Conditional_Statement;
994 -------------------------------
995 -- Is_Control_Flow_Statement --
996 -------------------------------
998 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
999 begin
1000 -- It is assumed that all statements may affect the control flow in
1001 -- some way. A raise statement may be expanded into a non-statement
1002 -- node.
1004 return Is_Statement (N) or else Is_Raise_Statement (N);
1005 end Is_Control_Flow_Statement;
1007 --------------------------------
1008 -- Is_Immediately_Within_Body --
1009 --------------------------------
1011 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
1012 HSS : constant Node_Id := Parent (N);
1014 begin
1015 return
1016 Nkind (HSS) = N_Handled_Sequence_Of_Statements
1017 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
1018 and then Is_List_Member (N)
1019 and then List_Containing (N) = Statements (HSS);
1020 end Is_Immediately_Within_Body;
1022 --------------------
1023 -- Is_Raise_Idiom --
1024 --------------------
1026 function Is_Raise_Idiom (N : Node_Id) return Boolean is
1027 Raise_Stmt : Node_Id;
1028 Stmt : Node_Id;
1030 begin
1031 if Is_Immediately_Within_Body (N) then
1033 -- Assume that no raise statement has been seen yet
1035 Raise_Stmt := Empty;
1037 -- Examine the statements preceding the input node, skipping
1038 -- internally-generated constructs.
1040 Stmt := Prev (N);
1041 while Present (Stmt) loop
1043 -- Multiple raise statements violate the idiom
1045 if Is_Raise_Statement (Stmt) then
1046 if Present (Raise_Stmt) then
1047 return False;
1048 end if;
1050 Raise_Stmt := Stmt;
1052 elsif Comes_From_Source (Stmt) then
1053 exit;
1054 end if;
1056 Stmt := Prev (Stmt);
1057 end loop;
1059 -- At this point the node must be preceded by a raise statement,
1060 -- and the raise statement has to be the sole statement within
1061 -- the enclosing entry or subprogram body.
1063 return
1064 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
1065 end if;
1067 return False;
1068 end Is_Raise_Idiom;
1070 ------------------------
1071 -- Is_Raise_Statement --
1072 ------------------------
1074 function Is_Raise_Statement (N : Node_Id) return Boolean is
1075 begin
1076 -- A raise statement may be transfomed into a Raise_xxx_Error node
1078 return
1079 Nkind (N) = N_Raise_Statement
1080 or else Nkind (N) in N_Raise_xxx_Error;
1081 end Is_Raise_Statement;
1083 -----------------------
1084 -- Is_Sole_Statement --
1085 -----------------------
1087 function Is_Sole_Statement (N : Node_Id) return Boolean is
1088 Stmt : Node_Id;
1090 begin
1091 -- The input node appears within the statements of an entry or
1092 -- subprogram body. Examine the statements preceding the node.
1094 if Is_Immediately_Within_Body (N) then
1095 Stmt := Prev (N);
1097 while Present (Stmt) loop
1099 -- The statement is preceded by another statement or a source
1100 -- construct. This indicates that the node does not appear by
1101 -- itself.
1103 if Is_Control_Flow_Statement (Stmt)
1104 or else Comes_From_Source (Stmt)
1105 then
1106 return False;
1107 end if;
1109 Stmt := Prev (Stmt);
1110 end loop;
1112 return True;
1113 end if;
1115 -- The input node is within a construct nested inside the entry or
1116 -- subprogram body.
1118 return False;
1119 end Is_Sole_Statement;
1121 ----------------------------------------
1122 -- Preceded_By_Control_Flow_Statement --
1123 ----------------------------------------
1125 function Preceded_By_Control_Flow_Statement
1126 (N : Node_Id) return Boolean
1128 Stmt : Node_Id;
1130 begin
1131 if Is_List_Member (N) then
1132 Stmt := Prev (N);
1134 -- Examine the statements preceding the input node
1136 while Present (Stmt) loop
1137 if Is_Control_Flow_Statement (Stmt) then
1138 return True;
1139 end if;
1141 Stmt := Prev (Stmt);
1142 end loop;
1144 return False;
1145 end if;
1147 -- Assume that the node is part of some control flow statement
1149 return True;
1150 end Preceded_By_Control_Flow_Statement;
1152 ----------------------------------
1153 -- Within_Conditional_Statement --
1154 ----------------------------------
1156 function Within_Conditional_Statement (N : Node_Id) return Boolean is
1157 Stmt : Node_Id;
1159 begin
1160 Stmt := Parent (N);
1161 while Present (Stmt) loop
1162 if Is_Conditional_Statement (Stmt) then
1163 return True;
1165 -- Prevent the search from going too far
1167 elsif Is_Body_Or_Package_Declaration (Stmt) then
1168 exit;
1169 end if;
1171 Stmt := Parent (Stmt);
1172 end loop;
1174 return False;
1175 end Within_Conditional_Statement;
1177 -- Local variables
1179 Call_Context : constant Node_Id :=
1180 Enclosing_Declaration_Or_Statement (Call);
1182 -- Start of processing for Check_Infinite_Recursion
1184 begin
1185 -- The call is assumed to be safe when the enclosing subprogram is
1186 -- invoked with actuals other than its formals.
1188 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1189 -- begin
1190 -- ...
1191 -- Proc (A1, A2, ..., AN);
1192 -- ...
1193 -- end Proc;
1195 if Invoked_With_Different_Arguments (Call) then
1196 return False;
1198 -- The call is assumed to be safe when the invocation of the enclosing
1199 -- subprogram depends on a conditional statement.
1201 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1202 -- begin
1203 -- ...
1204 -- if Some_Condition then
1205 -- Proc (F1, F2, ..., FN);
1206 -- end if;
1207 -- ...
1208 -- end Proc;
1210 elsif Within_Conditional_Statement (Call) then
1211 return False;
1213 -- The context of the call is assumed to be safe when the invocation of
1214 -- the enclosing subprogram is preceded by some control flow statement.
1216 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1217 -- begin
1218 -- ...
1219 -- if Some_Condition then
1220 -- ...
1221 -- end if;
1222 -- ...
1223 -- Proc (F1, F2, ..., FN);
1224 -- ...
1225 -- end Proc;
1227 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1228 return False;
1230 -- Detect an idiom where the context of the call is preceded by a single
1231 -- raise statement.
1233 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1234 -- begin
1235 -- raise ...;
1236 -- Proc (F1, F2, ..., FN);
1237 -- end Proc;
1239 elsif Is_Raise_Idiom (Call_Context) then
1240 return False;
1241 end if;
1243 -- At this point it is certain that infinite recursion will take place
1244 -- as long as the call is executed. Detect a case where the context of
1245 -- the call is the sole source statement within the subprogram body.
1247 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1248 -- begin
1249 -- Proc (F1, F2, ..., FN);
1250 -- end Proc;
1252 -- Install an explicit raise to prevent the infinite recursion.
1254 if Is_Sole_Statement (Call_Context) then
1255 Error_Msg_Warn := SPARK_Mode /= On;
1256 Error_Msg_N ("!infinite recursion<<", Call);
1257 Error_Msg_N ("\!Storage_Error [<<", Call);
1259 Insert_Action (Call,
1260 Make_Raise_Storage_Error (Sloc (Call),
1261 Reason => SE_Infinite_Recursion));
1263 -- Otherwise infinite recursion could take place, considering other flow
1264 -- control constructs such as gotos, exit statements, etc.
1266 else
1267 Error_Msg_Warn := SPARK_Mode /= On;
1268 Error_Msg_N ("!possible infinite recursion<<", Call);
1269 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1270 end if;
1272 return True;
1273 end Check_Infinite_Recursion;
1275 ---------------------------------------
1276 -- Check_No_Direct_Boolean_Operators --
1277 ---------------------------------------
1279 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1280 begin
1281 if Scope (Entity (N)) = Standard_Standard
1282 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1283 then
1284 -- Restriction only applies to original source code
1286 if Comes_From_Source (N) then
1287 Check_Restriction (No_Direct_Boolean_Operators, N);
1288 end if;
1289 end if;
1291 -- Do style check (but skip if in instance, error is on template)
1293 if Style_Check then
1294 if not In_Instance then
1295 Check_Boolean_Operator (N);
1296 end if;
1297 end if;
1298 end Check_No_Direct_Boolean_Operators;
1300 ------------------------------
1301 -- Check_Parameterless_Call --
1302 ------------------------------
1304 procedure Check_Parameterless_Call (N : Node_Id) is
1305 Nam : Node_Id;
1307 function Prefix_Is_Access_Subp return Boolean;
1308 -- If the prefix is of an access_to_subprogram type, the node must be
1309 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1310 -- interpretations are access to subprograms.
1312 ---------------------------
1313 -- Prefix_Is_Access_Subp --
1314 ---------------------------
1316 function Prefix_Is_Access_Subp return Boolean is
1317 I : Interp_Index;
1318 It : Interp;
1320 begin
1321 -- If the context is an attribute reference that can apply to
1322 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1324 if Nkind (Parent (N)) = N_Attribute_Reference
1325 and then Attribute_Name (Parent (N))
1326 in Name_Address | Name_Code_Address | Name_Access
1327 then
1328 return False;
1329 end if;
1331 if not Is_Overloaded (N) then
1332 return
1333 Ekind (Etype (N)) = E_Subprogram_Type
1334 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1335 else
1336 Get_First_Interp (N, I, It);
1337 while Present (It.Typ) loop
1338 if Ekind (It.Typ) /= E_Subprogram_Type
1339 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1340 then
1341 return False;
1342 end if;
1344 Get_Next_Interp (I, It);
1345 end loop;
1347 return True;
1348 end if;
1349 end Prefix_Is_Access_Subp;
1351 -- Start of processing for Check_Parameterless_Call
1353 begin
1354 -- Defend against junk stuff if errors already detected
1356 if Total_Errors_Detected /= 0 then
1357 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1358 return;
1359 elsif Nkind (N) in N_Has_Chars
1360 and then not Is_Valid_Name (Chars (N))
1361 then
1362 return;
1363 end if;
1365 Require_Entity (N);
1366 end if;
1368 -- If the context expects a value, and the name is a procedure, this is
1369 -- most likely a missing 'Access. Don't try to resolve the parameterless
1370 -- call, error will be caught when the outer call is analyzed.
1372 if Is_Entity_Name (N)
1373 and then Ekind (Entity (N)) = E_Procedure
1374 and then not Is_Overloaded (N)
1375 and then
1376 Nkind (Parent (N)) in N_Parameter_Association
1377 | N_Function_Call
1378 | N_Procedure_Call_Statement
1379 then
1380 return;
1381 end if;
1383 -- Rewrite as call if overloadable entity that is (or could be, in the
1384 -- overloaded case) a function call. If we know for sure that the entity
1385 -- is an enumeration literal, we do not rewrite it.
1387 -- If the entity is the name of an operator, it cannot be a call because
1388 -- operators cannot have default parameters. In this case, this must be
1389 -- a string whose contents coincide with an operator name. Set the kind
1390 -- of the node appropriately.
1392 if (Is_Entity_Name (N)
1393 and then Nkind (N) /= N_Operator_Symbol
1394 and then Is_Overloadable (Entity (N))
1395 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1396 or else Is_Overloaded (N)))
1398 -- Rewrite as call if it is an explicit dereference of an expression of
1399 -- a subprogram access type, and the subprogram type is not that of a
1400 -- procedure or entry.
1402 or else
1403 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1405 -- Rewrite as call if it is a selected component which is a function,
1406 -- this is the case of a call to a protected function (which may be
1407 -- overloaded with other protected operations).
1409 or else
1410 (Nkind (N) = N_Selected_Component
1411 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1412 or else
1413 (Ekind (Entity (Selector_Name (N))) in
1414 E_Entry | E_Procedure
1415 and then Is_Overloaded (Selector_Name (N)))))
1417 -- If one of the above three conditions is met, rewrite as call. Apply
1418 -- the rewriting only once.
1420 then
1421 if Nkind (Parent (N)) /= N_Function_Call
1422 or else N /= Name (Parent (N))
1423 then
1425 -- This may be a prefixed call that was not fully analyzed, e.g.
1426 -- an actual in an instance.
1428 if Ada_Version >= Ada_2005
1429 and then Nkind (N) = N_Selected_Component
1430 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1431 then
1432 Analyze_Selected_Component (N);
1434 if Nkind (N) /= N_Selected_Component then
1435 return;
1436 end if;
1437 end if;
1439 -- The node is the name of the parameterless call. Preserve its
1440 -- descendants, which may be complex expressions.
1442 Nam := Relocate_Node (N);
1444 -- If overloaded, overload set belongs to new copy
1446 Save_Interps (N, Nam);
1448 -- Change node to parameterless function call (note that the
1449 -- Parameter_Associations associations field is left set to Empty,
1450 -- its normal default value since there are no parameters)
1452 Change_Node (N, N_Function_Call);
1453 Set_Name (N, Nam);
1454 Set_Sloc (N, Sloc (Nam));
1455 Analyze_Call (N);
1456 end if;
1458 elsif Nkind (N) = N_Parameter_Association then
1459 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1461 elsif Nkind (N) = N_Operator_Symbol then
1462 Set_Etype (N, Empty);
1463 Set_Entity (N, Empty);
1464 Set_Is_Overloaded (N, False);
1465 Change_Operator_Symbol_To_String_Literal (N);
1466 Set_Etype (N, Any_String);
1467 end if;
1468 end Check_Parameterless_Call;
1470 --------------------------------
1471 -- Is_Atomic_Ref_With_Address --
1472 --------------------------------
1474 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1475 Pref : constant Node_Id := Prefix (N);
1477 begin
1478 if not Is_Entity_Name (Pref) then
1479 return False;
1481 else
1482 declare
1483 Pent : constant Entity_Id := Entity (Pref);
1484 Ptyp : constant Entity_Id := Etype (Pent);
1485 begin
1486 return not Is_Access_Type (Ptyp)
1487 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1488 and then Present (Address_Clause (Pent));
1489 end;
1490 end if;
1491 end Is_Atomic_Ref_With_Address;
1493 -----------------------------
1494 -- Is_Definite_Access_Type --
1495 -----------------------------
1497 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean is
1498 Btyp : constant Entity_Id := Base_Type (E);
1499 begin
1500 return Ekind (Btyp) = E_Access_Type
1501 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1502 and then Comes_From_Source (Btyp));
1503 end Is_Definite_Access_Type;
1505 ----------------------
1506 -- Is_Predefined_Op --
1507 ----------------------
1509 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1510 begin
1511 -- Predefined operators are intrinsic subprograms
1513 if not Is_Intrinsic_Subprogram (Nam) then
1514 return False;
1515 end if;
1517 -- A call to a back-end builtin is never a predefined operator
1519 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1520 return False;
1521 end if;
1523 return not Is_Generic_Instance (Nam)
1524 and then Chars (Nam) in Any_Operator_Name
1525 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1526 end Is_Predefined_Op;
1528 -----------------------------
1529 -- Make_Call_Into_Operator --
1530 -----------------------------
1532 procedure Make_Call_Into_Operator
1533 (N : Node_Id;
1534 Typ : Entity_Id;
1535 Op_Id : Entity_Id)
1537 Op_Name : constant Name_Id := Chars (Op_Id);
1538 Act1 : Node_Id := First_Actual (N);
1539 Act2 : Node_Id := Next_Actual (Act1);
1540 Error : Boolean := False;
1541 Func : constant Entity_Id := Entity (Name (N));
1542 Is_Binary : constant Boolean := Present (Act2);
1543 Op_Node : Node_Id;
1544 Opnd_Type : Entity_Id := Empty;
1545 Orig_Type : Entity_Id := Empty;
1546 Pack : Entity_Id;
1548 type Kind_Test is access function (E : N_Entity_Id) return Boolean;
1550 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1551 -- If the operand is not universal, and the operator is given by an
1552 -- expanded name, verify that the operand has an interpretation with a
1553 -- type defined in the given scope of the operator.
1555 function Type_In_P (Test : Kind_Test) return Entity_Id;
1556 -- Find a type of the given class in package Pack that contains the
1557 -- operator.
1559 ---------------------------
1560 -- Operand_Type_In_Scope --
1561 ---------------------------
1563 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1564 Nod : constant Node_Id := Right_Opnd (Op_Node);
1565 I : Interp_Index;
1566 It : Interp;
1568 begin
1569 if not Is_Overloaded (Nod) then
1570 return Scope (Base_Type (Etype (Nod))) = S;
1572 else
1573 Get_First_Interp (Nod, I, It);
1574 while Present (It.Typ) loop
1575 if Scope (Base_Type (It.Typ)) = S then
1576 return True;
1577 end if;
1579 Get_Next_Interp (I, It);
1580 end loop;
1582 return False;
1583 end if;
1584 end Operand_Type_In_Scope;
1586 ---------------
1587 -- Type_In_P --
1588 ---------------
1590 function Type_In_P (Test : Kind_Test) return Entity_Id is
1591 E : Entity_Id;
1593 function In_Decl return Boolean;
1594 -- Verify that node is not part of the type declaration for the
1595 -- candidate type, which would otherwise be invisible.
1597 -------------
1598 -- In_Decl --
1599 -------------
1601 function In_Decl return Boolean is
1602 Decl_Node : constant Node_Id := Parent (E);
1603 N2 : Node_Id;
1605 begin
1606 N2 := N;
1608 if Etype (E) = Any_Type then
1609 return True;
1611 elsif No (Decl_Node) then
1612 return False;
1614 else
1615 while Present (N2)
1616 and then Nkind (N2) /= N_Compilation_Unit
1617 loop
1618 if N2 = Decl_Node then
1619 return True;
1620 else
1621 N2 := Parent (N2);
1622 end if;
1623 end loop;
1625 return False;
1626 end if;
1627 end In_Decl;
1629 -- Start of processing for Type_In_P
1631 begin
1632 -- If the context type is declared in the prefix package, this is the
1633 -- desired base type.
1635 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1636 return Base_Type (Typ);
1638 else
1639 E := First_Entity (Pack);
1640 while Present (E) loop
1641 if Test (E) and then not In_Decl then
1642 return E;
1643 end if;
1645 Next_Entity (E);
1646 end loop;
1648 return Empty;
1649 end if;
1650 end Type_In_P;
1652 -- Start of processing for Make_Call_Into_Operator
1654 begin
1655 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1657 -- Preserve the Comes_From_Source flag on the result if the original
1658 -- call came from source. Although it is not strictly the case that the
1659 -- operator as such comes from the source, logically it corresponds
1660 -- exactly to the function call in the source, so it should be marked
1661 -- this way (e.g. to make sure that validity checks work fine).
1663 Preserve_Comes_From_Source (Op_Node, N);
1665 -- Ensure that the corresponding operator has the same parent as the
1666 -- original call. This guarantees that parent traversals performed by
1667 -- the ABE mechanism succeed.
1669 Set_Parent (Op_Node, Parent (N));
1671 -- Binary operator
1673 if Is_Binary then
1674 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1675 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1676 Save_Interps (Act1, Left_Opnd (Op_Node));
1677 Save_Interps (Act2, Right_Opnd (Op_Node));
1678 Act1 := Left_Opnd (Op_Node);
1679 Act2 := Right_Opnd (Op_Node);
1681 -- Unary operator
1683 else
1684 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1685 Save_Interps (Act1, Right_Opnd (Op_Node));
1686 Act1 := Right_Opnd (Op_Node);
1687 end if;
1689 -- If the operator is denoted by an expanded name, and the prefix is
1690 -- not Standard, but the operator is a predefined one whose scope is
1691 -- Standard, then this is an implicit_operator, inserted as an
1692 -- interpretation by the procedure of the same name. This procedure
1693 -- overestimates the presence of implicit operators, because it does
1694 -- not examine the type of the operands. Verify now that the operand
1695 -- type appears in the given scope. If right operand is universal,
1696 -- check the other operand. In the case of concatenation, either
1697 -- argument can be the component type, so check the type of the result.
1698 -- If both arguments are literals, look for a type of the right kind
1699 -- defined in the given scope. This elaborate nonsense is brought to
1700 -- you courtesy of b33302a. The type itself must be frozen, so we must
1701 -- find the type of the proper class in the given scope.
1703 -- A final wrinkle is the multiplication operator for fixed point types,
1704 -- which is defined in Standard only, and not in the scope of the
1705 -- fixed point type itself.
1707 if Nkind (Name (N)) = N_Expanded_Name then
1708 Pack := Entity (Prefix (Name (N)));
1710 -- If this is a package renaming, get renamed entity, which will be
1711 -- the scope of the operands if operaton is type-correct.
1713 if Present (Renamed_Entity (Pack)) then
1714 Pack := Renamed_Entity (Pack);
1715 end if;
1717 -- If the entity being called is defined in the given package, it is
1718 -- a renaming of a predefined operator, and known to be legal.
1720 if Scope (Entity (Name (N))) = Pack
1721 and then Pack /= Standard_Standard
1722 then
1723 null;
1725 -- Visibility does not need to be checked in an instance: if the
1726 -- operator was not visible in the generic it has been diagnosed
1727 -- already, else there is an implicit copy of it in the instance.
1729 elsif In_Instance then
1730 null;
1732 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1733 and then Is_Fixed_Point_Type (Etype (Act1))
1734 and then Is_Fixed_Point_Type (Etype (Act2))
1735 then
1736 if Pack /= Standard_Standard then
1737 Error := True;
1738 end if;
1740 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1741 -- available.
1743 elsif Ada_Version >= Ada_2005
1744 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1745 and then (Is_Anonymous_Access_Type (Etype (Act1))
1746 or else Is_Anonymous_Access_Type (Etype (Act2)))
1747 then
1748 null;
1750 else
1751 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1753 if Op_Name = Name_Op_Concat then
1754 Opnd_Type := Base_Type (Typ);
1756 elsif (Scope (Opnd_Type) = Standard_Standard
1757 and then Is_Binary)
1758 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1759 and then Is_Binary
1760 and then not Comes_From_Source (Opnd_Type))
1761 then
1762 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1763 end if;
1765 if Scope (Opnd_Type) = Standard_Standard then
1767 -- Verify that the scope contains a type that corresponds to
1768 -- the given literal. Optimize the case where Pack is Standard.
1770 if Pack /= Standard_Standard then
1771 if Opnd_Type = Universal_Integer then
1772 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1774 elsif Opnd_Type = Universal_Real then
1775 Orig_Type := Type_In_P (Is_Real_Type'Access);
1777 elsif Opnd_Type = Universal_Access then
1778 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1780 elsif Opnd_Type = Any_String then
1781 Orig_Type := Type_In_P (Is_String_Type'Access);
1783 elsif Opnd_Type = Any_Composite then
1784 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1786 if Present (Orig_Type) then
1787 if Has_Private_Component (Orig_Type) then
1788 Orig_Type := Empty;
1789 else
1790 Set_Etype (Act1, Orig_Type);
1792 if Is_Binary then
1793 Set_Etype (Act2, Orig_Type);
1794 end if;
1795 end if;
1796 end if;
1798 else
1799 Orig_Type := Empty;
1800 end if;
1802 Error := No (Orig_Type);
1803 end if;
1805 elsif Ekind (Opnd_Type) = E_Allocator_Type
1806 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1807 then
1808 Error := True;
1810 -- If the type is defined elsewhere, and the operator is not
1811 -- defined in the given scope (by a renaming declaration, e.g.)
1812 -- then this is an error as well. If an extension of System is
1813 -- present, and the type may be defined there, Pack must be
1814 -- System itself.
1816 elsif Scope (Opnd_Type) /= Pack
1817 and then Scope (Op_Id) /= Pack
1818 and then (No (System_Aux_Id)
1819 or else Scope (Opnd_Type) /= System_Aux_Id
1820 or else Pack /= Scope (System_Aux_Id))
1821 then
1822 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1823 Error := True;
1824 else
1825 Error := not Operand_Type_In_Scope (Pack);
1826 end if;
1828 elsif Pack = Standard_Standard
1829 and then not Operand_Type_In_Scope (Standard_Standard)
1830 then
1831 Error := True;
1832 end if;
1833 end if;
1835 if Error then
1836 Error_Msg_Node_2 := Pack;
1837 Error_Msg_NE
1838 ("& not declared in&", N, Selector_Name (Name (N)));
1839 Set_Etype (N, Any_Type);
1840 return;
1842 -- Detect a mismatch between the context type and the result type
1843 -- in the named package, which is otherwise not detected if the
1844 -- operands are universal. Check is only needed if source entity is
1845 -- an operator, not a function that renames an operator.
1847 elsif Nkind (Parent (N)) /= N_Type_Conversion
1848 and then Ekind (Entity (Name (N))) = E_Operator
1849 and then Is_Numeric_Type (Typ)
1850 and then not Is_Universal_Numeric_Type (Typ)
1851 and then Scope (Base_Type (Typ)) /= Pack
1852 and then not In_Instance
1853 then
1854 if Is_Fixed_Point_Type (Typ)
1855 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1856 then
1857 -- Already checked above
1859 null;
1861 -- Operator may be defined in an extension of System
1863 elsif Present (System_Aux_Id)
1864 and then Present (Opnd_Type)
1865 and then Scope (Opnd_Type) = System_Aux_Id
1866 then
1867 null;
1869 else
1870 -- Could we use Wrong_Type here??? (this would require setting
1871 -- Etype (N) to the actual type found where Typ was expected).
1873 Error_Msg_NE ("expect }", N, Typ);
1874 end if;
1875 end if;
1876 end if;
1878 Set_Chars (Op_Node, Op_Name);
1880 if not Is_Private_Type (Etype (N)) then
1881 Set_Etype (Op_Node, Base_Type (Etype (N)));
1882 else
1883 Set_Etype (Op_Node, Etype (N));
1884 end if;
1886 -- If this is a call to a function that renames a predefined equality,
1887 -- the renaming declaration provides a type that must be used to
1888 -- resolve the operands. This must be done now because resolution of
1889 -- the equality node will not resolve any remaining ambiguity, and it
1890 -- assumes that the first operand is not overloaded.
1892 if Op_Name in Name_Op_Eq | Name_Op_Ne
1893 and then Ekind (Func) = E_Function
1894 and then Is_Overloaded (Act1)
1895 then
1896 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1897 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1898 end if;
1900 Set_Entity (Op_Node, Op_Id);
1901 Generate_Reference (Op_Id, N, ' ');
1903 Rewrite (N, Op_Node);
1905 -- If this is an arithmetic operator and the result type is private,
1906 -- the operands and the result must be wrapped in conversion to
1907 -- expose the underlying numeric type and expand the proper checks,
1908 -- e.g. on division.
1910 if Is_Private_Type (Typ) then
1911 case Nkind (N) is
1912 when N_Op_Add
1913 | N_Op_Divide
1914 | N_Op_Expon
1915 | N_Op_Mod
1916 | N_Op_Multiply
1917 | N_Op_Rem
1918 | N_Op_Subtract
1920 Resolve_Intrinsic_Operator (N, Typ);
1922 when N_Op_Abs
1923 | N_Op_Minus
1924 | N_Op_Plus
1926 Resolve_Intrinsic_Unary_Operator (N, Typ);
1928 when others =>
1929 Resolve (N, Typ);
1930 end case;
1931 else
1932 Resolve (N, Typ);
1933 end if;
1934 end Make_Call_Into_Operator;
1936 -------------------
1937 -- Operator_Kind --
1938 -------------------
1940 function Operator_Kind
1941 (Op_Name : Name_Id;
1942 Is_Binary : Boolean) return Node_Kind
1944 Kind : Node_Kind;
1946 begin
1947 -- Use CASE statement or array???
1949 if Is_Binary then
1950 if Op_Name = Name_Op_And then
1951 Kind := N_Op_And;
1952 elsif Op_Name = Name_Op_Or then
1953 Kind := N_Op_Or;
1954 elsif Op_Name = Name_Op_Xor then
1955 Kind := N_Op_Xor;
1956 elsif Op_Name = Name_Op_Eq then
1957 Kind := N_Op_Eq;
1958 elsif Op_Name = Name_Op_Ne then
1959 Kind := N_Op_Ne;
1960 elsif Op_Name = Name_Op_Lt then
1961 Kind := N_Op_Lt;
1962 elsif Op_Name = Name_Op_Le then
1963 Kind := N_Op_Le;
1964 elsif Op_Name = Name_Op_Gt then
1965 Kind := N_Op_Gt;
1966 elsif Op_Name = Name_Op_Ge then
1967 Kind := N_Op_Ge;
1968 elsif Op_Name = Name_Op_Add then
1969 Kind := N_Op_Add;
1970 elsif Op_Name = Name_Op_Subtract then
1971 Kind := N_Op_Subtract;
1972 elsif Op_Name = Name_Op_Concat then
1973 Kind := N_Op_Concat;
1974 elsif Op_Name = Name_Op_Multiply then
1975 Kind := N_Op_Multiply;
1976 elsif Op_Name = Name_Op_Divide then
1977 Kind := N_Op_Divide;
1978 elsif Op_Name = Name_Op_Mod then
1979 Kind := N_Op_Mod;
1980 elsif Op_Name = Name_Op_Rem then
1981 Kind := N_Op_Rem;
1982 elsif Op_Name = Name_Op_Expon then
1983 Kind := N_Op_Expon;
1984 else
1985 raise Program_Error;
1986 end if;
1988 -- Unary operators
1990 else
1991 if Op_Name = Name_Op_Add then
1992 Kind := N_Op_Plus;
1993 elsif Op_Name = Name_Op_Subtract then
1994 Kind := N_Op_Minus;
1995 elsif Op_Name = Name_Op_Abs then
1996 Kind := N_Op_Abs;
1997 elsif Op_Name = Name_Op_Not then
1998 Kind := N_Op_Not;
1999 else
2000 raise Program_Error;
2001 end if;
2002 end if;
2004 return Kind;
2005 end Operator_Kind;
2007 ----------------------------
2008 -- Preanalyze_And_Resolve --
2009 ----------------------------
2011 procedure Preanalyze_And_Resolve
2012 (N : Node_Id;
2013 T : Entity_Id;
2014 With_Freezing : Boolean)
2016 Save_Full_Analysis : constant Boolean := Full_Analysis;
2017 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
2018 Save_Preanalysis_Count : constant Nat :=
2019 Inside_Preanalysis_Without_Freezing;
2020 begin
2021 pragma Assert (Nkind (N) in N_Subexpr);
2023 if not With_Freezing then
2024 Set_Must_Not_Freeze (N);
2025 Inside_Preanalysis_Without_Freezing :=
2026 Inside_Preanalysis_Without_Freezing + 1;
2027 end if;
2029 Full_Analysis := False;
2030 Expander_Mode_Save_And_Set (False);
2032 -- See also Preanalyze_And_Resolve in sem.adb for similar handling
2034 -- Normally, we suppress all checks for this preanalysis. There is no
2035 -- point in processing them now, since they will be applied properly
2036 -- and in the proper location when the default expressions reanalyzed
2037 -- and reexpanded later on. We will also have more information at that
2038 -- point for possible suppression of individual checks.
2040 -- However, in GNATprove mode, most expansion is suppressed, and this
2041 -- later reanalysis and reexpansion may not occur. GNATprove mode does
2042 -- require the setting of checking flags for proof purposes, so we
2043 -- do the GNATprove preanalysis without suppressing checks.
2045 -- This special handling for SPARK mode is required for example in the
2046 -- case of Ada 2012 constructs such as quantified expressions, which are
2047 -- expanded in two separate steps.
2049 -- We also do not want to suppress checks if we are not dealing
2050 -- with a default expression. One such case that is known to reach
2051 -- this point is the expression of an expression function.
2053 if GNATprove_Mode or Nkind (Parent (N)) = N_Simple_Return_Statement then
2054 Analyze_And_Resolve (N, T);
2055 else
2056 Analyze_And_Resolve (N, T, Suppress => All_Checks);
2057 end if;
2059 Expander_Mode_Restore;
2060 Full_Analysis := Save_Full_Analysis;
2062 if not With_Freezing then
2063 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
2064 Inside_Preanalysis_Without_Freezing :=
2065 Inside_Preanalysis_Without_Freezing - 1;
2066 end if;
2068 pragma Assert
2069 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
2070 end Preanalyze_And_Resolve;
2072 ----------------------------
2073 -- Preanalyze_And_Resolve --
2074 ----------------------------
2076 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
2077 begin
2078 Preanalyze_And_Resolve (N, T, With_Freezing => False);
2079 end Preanalyze_And_Resolve;
2081 -- Version without context type
2083 procedure Preanalyze_And_Resolve (N : Node_Id) is
2084 Save_Full_Analysis : constant Boolean := Full_Analysis;
2086 begin
2087 Full_Analysis := False;
2088 Expander_Mode_Save_And_Set (False);
2090 Analyze (N);
2091 Resolve (N, Etype (N), Suppress => All_Checks);
2093 Expander_Mode_Restore;
2094 Full_Analysis := Save_Full_Analysis;
2095 end Preanalyze_And_Resolve;
2097 ------------------------------------------
2098 -- Preanalyze_With_Freezing_And_Resolve --
2099 ------------------------------------------
2101 procedure Preanalyze_With_Freezing_And_Resolve
2102 (N : Node_Id;
2103 T : Entity_Id)
2105 begin
2106 Preanalyze_And_Resolve (N, T, With_Freezing => True);
2107 end Preanalyze_With_Freezing_And_Resolve;
2109 ----------------------------------
2110 -- Replace_Actual_Discriminants --
2111 ----------------------------------
2113 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2114 Loc : constant Source_Ptr := Sloc (N);
2115 Tsk : Node_Id := Empty;
2117 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2118 -- Comment needed???
2120 -------------------
2121 -- Process_Discr --
2122 -------------------
2124 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2125 Ent : Entity_Id;
2127 begin
2128 if Nkind (Nod) = N_Identifier then
2129 Ent := Entity (Nod);
2131 if Present (Ent)
2132 and then Ekind (Ent) = E_Discriminant
2133 then
2134 Rewrite (Nod,
2135 Make_Selected_Component (Loc,
2136 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2137 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2139 Set_Etype (Nod, Etype (Ent));
2140 end if;
2142 end if;
2144 return OK;
2145 end Process_Discr;
2147 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2149 -- Start of processing for Replace_Actual_Discriminants
2151 begin
2152 if Expander_Active then
2153 null;
2155 -- Allow the replacement of concurrent discriminants in GNATprove even
2156 -- though this is a light expansion activity. Note that generic units
2157 -- are not modified.
2159 elsif GNATprove_Mode and not Inside_A_Generic then
2160 null;
2162 else
2163 return;
2164 end if;
2166 if Nkind (Name (N)) = N_Selected_Component then
2167 Tsk := Prefix (Name (N));
2169 elsif Nkind (Name (N)) = N_Indexed_Component then
2170 Tsk := Prefix (Prefix (Name (N)));
2171 end if;
2173 if Present (Tsk) then
2174 Replace_Discrs (Default);
2175 end if;
2176 end Replace_Actual_Discriminants;
2178 -------------
2179 -- Resolve --
2180 -------------
2182 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2183 Ambiguous : Boolean := False;
2184 Ctx_Type : Entity_Id := Typ;
2185 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2186 Err_Type : Entity_Id := Empty;
2187 Found : Boolean := False;
2188 From_Lib : Boolean;
2189 I : Interp_Index;
2190 I1 : Interp_Index := 0; -- prevent junk warning
2191 It : Interp;
2192 It1 : Interp;
2193 Seen : Entity_Id := Empty; -- prevent junk warning
2195 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2196 -- Determine whether a node comes from a predefined library unit or
2197 -- Standard.
2199 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2200 -- Try and fix up a literal so that it matches its expected type. New
2201 -- literals are manufactured if necessary to avoid cascaded errors.
2203 procedure Report_Ambiguous_Argument;
2204 -- Additional diagnostics when an ambiguous call has an ambiguous
2205 -- argument (typically a controlling actual).
2207 procedure Resolution_Failed;
2208 -- Called when attempt at resolving current expression fails
2210 ------------------------------------
2211 -- Comes_From_Predefined_Lib_Unit --
2212 -------------------------------------
2214 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2215 begin
2216 return
2217 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2218 end Comes_From_Predefined_Lib_Unit;
2220 --------------------
2221 -- Patch_Up_Value --
2222 --------------------
2224 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2225 begin
2226 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2227 Rewrite (N,
2228 Make_Real_Literal (Sloc (N),
2229 Realval => UR_From_Uint (Intval (N))));
2230 Set_Etype (N, Universal_Real);
2231 Set_Is_Static_Expression (N);
2233 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2234 Rewrite (N,
2235 Make_Integer_Literal (Sloc (N),
2236 Intval => UR_To_Uint (Realval (N))));
2237 Set_Etype (N, Universal_Integer);
2238 Set_Is_Static_Expression (N);
2240 elsif Nkind (N) = N_String_Literal
2241 and then Is_Character_Type (Typ)
2242 then
2243 Set_Character_Literal_Name (Get_Char_Code ('A'));
2244 Rewrite (N,
2245 Make_Character_Literal (Sloc (N),
2246 Chars => Name_Find,
2247 Char_Literal_Value =>
2248 UI_From_CC (Get_Char_Code ('A'))));
2249 Set_Etype (N, Any_Character);
2250 Set_Is_Static_Expression (N);
2252 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2253 Rewrite (N,
2254 Make_String_Literal (Sloc (N),
2255 Strval => End_String));
2257 elsif Nkind (N) = N_Range then
2258 Patch_Up_Value (Low_Bound (N), Typ);
2259 Patch_Up_Value (High_Bound (N), Typ);
2260 end if;
2261 end Patch_Up_Value;
2263 -------------------------------
2264 -- Report_Ambiguous_Argument --
2265 -------------------------------
2267 procedure Report_Ambiguous_Argument is
2268 Arg : constant Node_Id := First (Parameter_Associations (N));
2269 I : Interp_Index;
2270 It : Interp;
2272 begin
2273 if Nkind (Arg) = N_Function_Call
2274 and then Is_Entity_Name (Name (Arg))
2275 and then Is_Overloaded (Name (Arg))
2276 then
2277 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2279 -- Examine possible interpretations, and adapt the message
2280 -- for inherited subprograms declared by a type derivation.
2282 Get_First_Interp (Name (Arg), I, It);
2283 while Present (It.Nam) loop
2284 Error_Msg_Sloc := Sloc (It.Nam);
2286 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2287 Error_Msg_N ("interpretation (inherited) #!", Arg);
2288 else
2289 Error_Msg_N ("interpretation #!", Arg);
2290 end if;
2292 Get_Next_Interp (I, It);
2293 end loop;
2294 end if;
2296 -- Additional message and hint if the ambiguity involves an Ada 2022
2297 -- container aggregate.
2299 Check_Ambiguous_Aggregate (N);
2300 end Report_Ambiguous_Argument;
2302 -----------------------
2303 -- Resolution_Failed --
2304 -----------------------
2306 procedure Resolution_Failed is
2307 begin
2308 Patch_Up_Value (N, Typ);
2310 -- Set the type to the desired one to minimize cascaded errors. Note
2311 -- that this is an approximation and does not work in all cases.
2313 Set_Etype (N, Typ);
2315 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2316 Set_Is_Overloaded (N, False);
2318 -- The caller will return without calling the expander, so we need
2319 -- to set the analyzed flag. Note that it is fine to set Analyzed
2320 -- to True even if we are in the middle of a shallow analysis,
2321 -- (see the spec of sem for more details) since this is an error
2322 -- situation anyway, and there is no point in repeating the
2323 -- analysis later (indeed it won't work to repeat it later, since
2324 -- we haven't got a clear resolution of which entity is being
2325 -- referenced.)
2327 Set_Analyzed (N, True);
2328 return;
2329 end Resolution_Failed;
2331 -- Start of processing for Resolve
2333 begin
2334 if N = Error then
2335 return;
2336 end if;
2338 -- Access attribute on remote subprogram cannot be used for a non-remote
2339 -- access-to-subprogram type.
2341 if Nkind (N) = N_Attribute_Reference
2342 and then Attribute_Name (N) in Name_Access
2343 | Name_Unrestricted_Access
2344 | Name_Unchecked_Access
2345 and then Comes_From_Source (N)
2346 and then Is_Entity_Name (Prefix (N))
2347 and then Is_Subprogram (Entity (Prefix (N)))
2348 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2349 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2350 then
2351 Error_Msg_N
2352 ("prefix must statically denote a non-remote subprogram", N);
2353 end if;
2355 -- If the context is a Remote_Access_To_Subprogram, access attributes
2356 -- must be resolved with the corresponding fat pointer. There is no need
2357 -- to check for the attribute name since the return type of an
2358 -- attribute is never a remote type.
2360 if Nkind (N) = N_Attribute_Reference
2361 and then Comes_From_Source (N)
2362 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2363 then
2364 declare
2365 Attr : constant Attribute_Id :=
2366 Get_Attribute_Id (Attribute_Name (N));
2367 Pref : constant Node_Id := Prefix (N);
2368 Decl : Node_Id;
2369 Spec : Node_Id;
2370 Is_Remote : Boolean := True;
2372 begin
2373 -- Check that Typ is a remote access-to-subprogram type
2375 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2377 -- Prefix (N) must statically denote a remote subprogram
2378 -- declared in a package specification.
2380 if Attr = Attribute_Access or else
2381 Attr = Attribute_Unchecked_Access or else
2382 Attr = Attribute_Unrestricted_Access
2383 then
2384 Decl := Unit_Declaration_Node (Entity (Pref));
2386 if Nkind (Decl) = N_Subprogram_Body then
2387 Spec := Corresponding_Spec (Decl);
2389 if Present (Spec) then
2390 Decl := Unit_Declaration_Node (Spec);
2391 end if;
2392 end if;
2394 Spec := Parent (Decl);
2396 if not Is_Entity_Name (Prefix (N))
2397 or else Nkind (Spec) /= N_Package_Specification
2398 or else
2399 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2400 then
2401 Is_Remote := False;
2402 Error_Msg_N
2403 ("prefix must statically denote a remote subprogram",
2405 end if;
2407 -- If we are generating code in distributed mode, perform
2408 -- semantic checks against corresponding remote entities.
2410 if Expander_Active
2411 and then Get_PCS_Name /= Name_No_DSA
2412 then
2413 Check_Subtype_Conformant
2414 (New_Id => Entity (Prefix (N)),
2415 Old_Id => Designated_Type
2416 (Corresponding_Remote_Type (Typ)),
2417 Err_Loc => N);
2419 if Is_Remote then
2420 Process_Remote_AST_Attribute (N, Typ);
2421 end if;
2422 end if;
2423 end if;
2424 end if;
2425 end;
2426 end if;
2428 Debug_A_Entry ("resolving ", N);
2430 if Debug_Flag_V then
2431 Write_Overloads (N);
2432 end if;
2434 if Comes_From_Source (N) then
2435 if Is_Fixed_Point_Type (Typ) then
2436 Check_Restriction (No_Fixed_Point, N);
2438 elsif Is_Floating_Point_Type (Typ)
2439 and then Typ /= Universal_Real
2440 and then Typ /= Any_Real
2441 then
2442 Check_Restriction (No_Floating_Point, N);
2443 end if;
2444 end if;
2446 -- Return if already analyzed
2448 if Analyzed (N) then
2449 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2450 Analyze_Dimension (N);
2451 return;
2453 -- Any case of Any_Type as the Etype value means that we had a
2454 -- previous error.
2456 elsif Etype (N) = Any_Type then
2457 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2458 return;
2459 end if;
2461 Check_Parameterless_Call (N);
2463 -- The resolution of an Expression_With_Actions is determined by
2464 -- its Expression, but if the node comes from source it is a
2465 -- Declare_Expression and requires scope management.
2467 if Nkind (N) = N_Expression_With_Actions then
2468 if Comes_From_Source (N) and then not Is_Rewrite_Substitution (N) then
2469 Resolve_Declare_Expression (N, Typ);
2470 else
2471 Resolve (Expression (N), Typ);
2472 end if;
2474 Found := True;
2475 Expr_Type := Etype (Expression (N));
2477 -- The resolution of a conditional expression that is the operand of a
2478 -- type conversion is determined by the conversion (RM 4.5.7(10/3)).
2480 elsif Nkind (N) in N_Case_Expression | N_If_Expression
2481 and then Nkind (Parent (N)) = N_Type_Conversion
2482 then
2483 Found := True;
2484 Expr_Type := Etype (Parent (N));
2486 -- If not overloaded, then we know the type, and all that needs doing
2487 -- is to check that this type is compatible with the context.
2489 elsif not Is_Overloaded (N) then
2490 Found := Covers (Typ, Etype (N));
2491 Expr_Type := Etype (N);
2493 -- In the overloaded case, we must select the interpretation that
2494 -- is compatible with the context (i.e. the type passed to Resolve)
2496 else
2497 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2499 -- Loop through possible interpretations
2501 Get_First_Interp (N, I, It);
2502 Interp_Loop : while Present (It.Typ) loop
2503 if Debug_Flag_V then
2504 Write_Str ("Interp: ");
2505 Write_Interp (It);
2506 end if;
2508 -- We are only interested in interpretations that are compatible
2509 -- with the expected type, any other interpretations are ignored.
2511 if not Covers (Typ, It.Typ) then
2512 if Debug_Flag_V then
2513 Write_Str (" interpretation incompatible with context");
2514 Write_Eol;
2515 end if;
2517 else
2518 -- Skip the current interpretation if it is disabled by an
2519 -- abstract operator. This action is performed only when the
2520 -- type against which we are resolving is the same as the
2521 -- type of the interpretation.
2523 if Ada_Version >= Ada_2005
2524 and then It.Typ = Typ
2525 and then not Is_Universal_Numeric_Type (Typ)
2526 and then Present (It.Abstract_Op)
2527 then
2528 if Debug_Flag_V then
2529 Write_Line ("Skip.");
2530 end if;
2532 goto Continue;
2533 end if;
2535 -- First matching interpretation
2537 if not Found then
2538 Found := True;
2539 I1 := I;
2540 Seen := It.Nam;
2541 Expr_Type := It.Typ;
2543 -- Matching interpretation that is not the first, maybe an
2544 -- error, but there are some cases where preference rules are
2545 -- used to choose between the two possibilities. These and
2546 -- some more obscure cases are handled in Disambiguate.
2548 else
2549 -- If the current statement is part of a predefined library
2550 -- unit, then all interpretations which come from user level
2551 -- packages should not be considered. Check previous and
2552 -- current one.
2554 if From_Lib then
2555 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2556 goto Continue;
2558 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2560 -- Previous interpretation must be discarded
2562 I1 := I;
2563 Seen := It.Nam;
2564 Expr_Type := It.Typ;
2565 Set_Entity (N, Seen);
2566 goto Continue;
2567 end if;
2568 end if;
2570 -- Otherwise apply further disambiguation steps
2572 Error_Msg_Sloc := Sloc (Seen);
2573 It1 := Disambiguate (N, I1, I, Typ);
2575 -- Disambiguation has succeeded. Skip the remaining
2576 -- interpretations.
2578 if It1 /= No_Interp then
2579 Seen := It1.Nam;
2580 Expr_Type := It1.Typ;
2582 while Present (It.Typ) loop
2583 Get_Next_Interp (I, It);
2584 end loop;
2586 else
2587 -- Before we issue an ambiguity complaint, check for the
2588 -- case of a subprogram call where at least one of the
2589 -- arguments is Any_Type, and if so suppress the message,
2590 -- since it is a cascaded error. This can also happen for
2591 -- a generalized indexing operation.
2593 if Nkind (N) in N_Subprogram_Call
2594 or else (Nkind (N) = N_Indexed_Component
2595 and then Present (Generalized_Indexing (N)))
2596 then
2597 declare
2598 A : Node_Id;
2599 E : Node_Id;
2601 begin
2602 if Nkind (N) = N_Indexed_Component then
2603 Rewrite (N, Generalized_Indexing (N));
2604 end if;
2606 A := First_Actual (N);
2607 while Present (A) loop
2608 E := A;
2610 if Nkind (E) = N_Parameter_Association then
2611 E := Explicit_Actual_Parameter (E);
2612 end if;
2614 if Etype (E) = Any_Type then
2615 if Debug_Flag_V then
2616 Write_Str ("Any_Type in call");
2617 Write_Eol;
2618 end if;
2620 exit Interp_Loop;
2621 end if;
2623 Next_Actual (A);
2624 end loop;
2625 end;
2627 elsif Nkind (N) in N_Binary_Op
2628 and then (Etype (Left_Opnd (N)) = Any_Type
2629 or else Etype (Right_Opnd (N)) = Any_Type)
2630 then
2631 exit Interp_Loop;
2633 elsif Nkind (N) in N_Unary_Op
2634 and then Etype (Right_Opnd (N)) = Any_Type
2635 then
2636 exit Interp_Loop;
2637 end if;
2639 -- Not that special case, so issue message using the flag
2640 -- Ambiguous to control printing of the header message
2641 -- only at the start of an ambiguous set.
2643 if not Ambiguous then
2644 if Nkind (N) = N_Function_Call
2645 and then Nkind (Name (N)) = N_Explicit_Dereference
2646 then
2647 Error_Msg_N
2648 ("ambiguous expression (cannot resolve indirect "
2649 & "call)!", N);
2650 else
2651 Error_Msg_NE -- CODEFIX
2652 ("ambiguous expression (cannot resolve&)!",
2653 N, It.Nam);
2654 end if;
2656 Ambiguous := True;
2658 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2659 Error_Msg_N
2660 ("\\possible interpretation (inherited)#!", N);
2661 else
2662 Error_Msg_N -- CODEFIX
2663 ("\\possible interpretation#!", N);
2664 end if;
2666 if Nkind (N) in N_Subprogram_Call
2667 and then Present (Parameter_Associations (N))
2668 then
2669 Report_Ambiguous_Argument;
2670 end if;
2671 end if;
2673 Error_Msg_Sloc := Sloc (It.Nam);
2675 -- By default, the error message refers to the candidate
2676 -- interpretation. But if it is a predefined operator, it
2677 -- is implicitly declared at the declaration of the type
2678 -- of the operand. Recover the sloc of that declaration
2679 -- for the error message.
2681 if Nkind (N) in N_Op
2682 and then Scope (It.Nam) = Standard_Standard
2683 and then not Is_Overloaded (Right_Opnd (N))
2684 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2685 Standard_Standard
2686 then
2687 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2689 if Comes_From_Source (Err_Type)
2690 and then Present (Parent (Err_Type))
2691 then
2692 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2693 end if;
2695 elsif Nkind (N) in N_Binary_Op
2696 and then Scope (It.Nam) = Standard_Standard
2697 and then not Is_Overloaded (Left_Opnd (N))
2698 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2699 Standard_Standard
2700 then
2701 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2703 if Comes_From_Source (Err_Type)
2704 and then Present (Parent (Err_Type))
2705 then
2706 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2707 end if;
2709 -- If this is an indirect call, use the subprogram_type
2710 -- in the message, to have a meaningful location. Also
2711 -- indicate if this is an inherited operation, created
2712 -- by a type declaration.
2714 elsif Nkind (N) = N_Function_Call
2715 and then Nkind (Name (N)) = N_Explicit_Dereference
2716 and then Is_Type (It.Nam)
2717 then
2718 Err_Type := It.Nam;
2719 Error_Msg_Sloc :=
2720 Sloc (Associated_Node_For_Itype (Err_Type));
2721 else
2722 Err_Type := Empty;
2723 end if;
2725 if Nkind (N) in N_Op
2726 and then Scope (It.Nam) = Standard_Standard
2727 and then Present (Err_Type)
2728 then
2729 -- Special-case the message for universal_fixed
2730 -- operators, which are not declared with the type
2731 -- of the operand, but appear forever in Standard.
2733 if It.Typ = Universal_Fixed
2734 and then Scope (It.Nam) = Standard_Standard
2735 then
2736 Error_Msg_N
2737 ("\\possible interpretation as universal_fixed "
2738 & "operation (RM 4.5.5 (19))", N);
2739 else
2740 Error_Msg_N
2741 ("\\possible interpretation (predefined)#!", N);
2742 end if;
2744 elsif
2745 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2746 then
2747 Error_Msg_N
2748 ("\\possible interpretation (inherited)#!", N);
2749 else
2750 Error_Msg_N -- CODEFIX
2751 ("\\possible interpretation#!", N);
2752 end if;
2754 end if;
2755 end if;
2757 -- We have a matching interpretation, Expr_Type is the type
2758 -- from this interpretation, and Seen is the entity.
2760 -- For an operator, just set the entity name. The type will be
2761 -- set by the specific operator resolution routine.
2763 if Nkind (N) in N_Op then
2764 Set_Entity (N, Seen);
2765 Generate_Reference (Seen, N);
2767 elsif Nkind (N) in N_Case_Expression
2768 | N_Character_Literal
2769 | N_Delta_Aggregate
2770 | N_If_Expression
2771 then
2772 Set_Etype (N, Expr_Type);
2774 -- AI05-0139-2: Expression is overloaded because type has
2775 -- implicit dereference. The context may be the one that
2776 -- requires implicit dereferemce.
2778 elsif Has_Implicit_Dereference (Expr_Type) then
2779 Set_Etype (N, Expr_Type);
2780 Set_Is_Overloaded (N, False);
2782 -- If the expression is an entity, generate a reference
2783 -- to it, as this is not done for an overloaded construct
2784 -- during analysis.
2786 if Is_Entity_Name (N)
2787 and then Comes_From_Source (N)
2788 then
2789 Generate_Reference (Entity (N), N);
2791 -- Examine access discriminants of entity type,
2792 -- to check whether one of them yields the
2793 -- expected type.
2795 declare
2796 Disc : Entity_Id :=
2797 First_Discriminant (Etype (Entity (N)));
2799 begin
2800 while Present (Disc) loop
2801 exit when Is_Access_Type (Etype (Disc))
2802 and then Has_Implicit_Dereference (Disc)
2803 and then Designated_Type (Etype (Disc)) = Typ;
2805 Next_Discriminant (Disc);
2806 end loop;
2808 if Present (Disc) then
2809 Build_Explicit_Dereference (N, Disc);
2810 end if;
2811 end;
2812 end if;
2814 exit Interp_Loop;
2816 elsif Is_Overloaded (N)
2817 and then Present (It.Nam)
2818 and then Ekind (It.Nam) = E_Discriminant
2819 and then Has_Implicit_Dereference (It.Nam)
2820 then
2821 -- If the node is a general indexing, the dereference is
2822 -- is inserted when resolving the rewritten form, else
2823 -- insert it now.
2825 if Nkind (N) /= N_Indexed_Component
2826 or else No (Generalized_Indexing (N))
2827 then
2828 Build_Explicit_Dereference (N, It.Nam);
2829 end if;
2831 -- For an explicit dereference, attribute reference, range,
2832 -- short-circuit form (which is not an operator node), or call
2833 -- with a name that is an explicit dereference, there is
2834 -- nothing to be done at this point.
2836 elsif Nkind (N) in N_Attribute_Reference
2837 | N_And_Then
2838 | N_Explicit_Dereference
2839 | N_Identifier
2840 | N_Indexed_Component
2841 | N_Or_Else
2842 | N_Range
2843 | N_Selected_Component
2844 | N_Slice
2845 or else Nkind (Name (N)) = N_Explicit_Dereference
2846 then
2847 null;
2849 -- For procedure or function calls, set the type of the name,
2850 -- and also the entity pointer for the prefix.
2852 elsif Nkind (N) in N_Subprogram_Call
2853 and then Is_Entity_Name (Name (N))
2854 then
2855 Set_Etype (Name (N), Expr_Type);
2856 Set_Entity (Name (N), Seen);
2857 Generate_Reference (Seen, Name (N));
2859 elsif Nkind (N) = N_Function_Call
2860 and then Nkind (Name (N)) = N_Selected_Component
2861 then
2862 Set_Etype (Name (N), Expr_Type);
2863 Set_Entity (Selector_Name (Name (N)), Seen);
2864 Generate_Reference (Seen, Selector_Name (Name (N)));
2866 -- For all other cases, just set the type of the Name
2868 else
2869 Set_Etype (Name (N), Expr_Type);
2870 end if;
2872 end if;
2874 <<Continue>>
2876 -- Move to next interpretation
2878 exit Interp_Loop when No (It.Typ);
2880 Get_Next_Interp (I, It);
2881 end loop Interp_Loop;
2882 end if;
2884 -- At this stage Found indicates whether or not an acceptable
2885 -- interpretation exists. If not, then we have an error, except that if
2886 -- the context is Any_Type as a result of some other error, then we
2887 -- suppress the error report.
2889 if not Found then
2890 if Typ /= Any_Type then
2892 -- If type we are looking for is Void, then this is the procedure
2893 -- call case, and the error is simply that what we gave is not a
2894 -- procedure name (we think of procedure calls as expressions with
2895 -- types internally, but the user doesn't think of them this way).
2897 if Typ = Standard_Void_Type then
2899 -- Special case message if function used as a procedure
2901 if Nkind (N) = N_Procedure_Call_Statement
2902 and then Is_Entity_Name (Name (N))
2903 and then Ekind (Entity (Name (N))) = E_Function
2904 then
2905 Error_Msg_NE
2906 ("cannot use call to function & as a statement",
2907 Name (N), Entity (Name (N)));
2908 Error_Msg_N
2909 ("\return value of a function call cannot be ignored",
2910 Name (N));
2912 -- Otherwise give general message (not clear what cases this
2913 -- covers, but no harm in providing for them).
2915 else
2916 Error_Msg_N ("expect procedure name in procedure call", N);
2917 end if;
2919 Found := True;
2921 -- Otherwise we do have a subexpression with the wrong type
2923 -- Check for the case of an allocator which uses an access type
2924 -- instead of the designated type. This is a common error and we
2925 -- specialize the message, posting an error on the operand of the
2926 -- allocator, complaining that we expected the designated type of
2927 -- the allocator.
2929 elsif Nkind (N) = N_Allocator
2930 and then Is_Access_Type (Typ)
2931 and then Is_Access_Type (Etype (N))
2932 and then Designated_Type (Etype (N)) = Typ
2933 then
2934 Wrong_Type (Expression (N), Designated_Type (Typ));
2935 Found := True;
2937 -- Check for view mismatch on Null in instances, for which the
2938 -- view-swapping mechanism has no identifier.
2940 elsif (In_Instance or else In_Inlined_Body)
2941 and then (Nkind (N) = N_Null)
2942 and then Is_Private_Type (Typ)
2943 and then Is_Access_Type (Full_View (Typ))
2944 then
2945 Resolve (N, Full_View (Typ));
2946 Set_Etype (N, Typ);
2947 return;
2949 -- Check for an aggregate. Sometimes we can get bogus aggregates
2950 -- from misuse of parentheses, and we are about to complain about
2951 -- the aggregate without even looking inside it.
2953 -- Instead, if we have an aggregate of type Any_Composite, then
2954 -- analyze and resolve the component fields, and then only issue
2955 -- another message if we get no errors doing this (otherwise
2956 -- assume that the errors in the aggregate caused the problem).
2958 elsif Nkind (N) = N_Aggregate
2959 and then Etype (N) = Any_Composite
2960 then
2961 if Ada_Version >= Ada_2022
2962 and then Has_Aspect (Typ, Aspect_Aggregate)
2963 then
2964 Resolve_Container_Aggregate (N, Typ);
2966 if Expander_Active then
2967 Expand (N);
2968 end if;
2969 return;
2970 end if;
2972 -- Disable expansion in any case. If there is a type mismatch
2973 -- it may be fatal to try to expand the aggregate. The flag
2974 -- would otherwise be set to false when the error is posted.
2976 Expander_Active := False;
2978 declare
2979 procedure Check_Aggr (Aggr : Node_Id);
2980 -- Check one aggregate, and set Found to True if we have a
2981 -- definite error in any of its elements
2983 procedure Check_Elmt (Aelmt : Node_Id);
2984 -- Check one element of aggregate and set Found to True if
2985 -- we definitely have an error in the element.
2987 ----------------
2988 -- Check_Aggr --
2989 ----------------
2991 procedure Check_Aggr (Aggr : Node_Id) is
2992 Elmt : Node_Id;
2994 begin
2995 if Present (Expressions (Aggr)) then
2996 Elmt := First (Expressions (Aggr));
2997 while Present (Elmt) loop
2998 Check_Elmt (Elmt);
2999 Next (Elmt);
3000 end loop;
3001 end if;
3003 if Present (Component_Associations (Aggr)) then
3004 Elmt := First (Component_Associations (Aggr));
3005 while Present (Elmt) loop
3007 -- If this is a default-initialized component, then
3008 -- there is nothing to check. The box will be
3009 -- replaced by the appropriate call during late
3010 -- expansion.
3012 if Nkind (Elmt) /= N_Iterated_Component_Association
3013 and then not Box_Present (Elmt)
3014 then
3015 Check_Elmt (Expression (Elmt));
3016 end if;
3018 Next (Elmt);
3019 end loop;
3020 end if;
3021 end Check_Aggr;
3023 ----------------
3024 -- Check_Elmt --
3025 ----------------
3027 procedure Check_Elmt (Aelmt : Node_Id) is
3028 begin
3029 -- If we have a nested aggregate, go inside it (to
3030 -- attempt a naked analyze-resolve of the aggregate can
3031 -- cause undesirable cascaded errors). Do not resolve
3032 -- expression if it needs a type from context, as for
3033 -- integer * fixed expression.
3035 if Nkind (Aelmt) = N_Aggregate then
3036 Check_Aggr (Aelmt);
3038 else
3039 Analyze (Aelmt);
3041 if not Is_Overloaded (Aelmt)
3042 and then Etype (Aelmt) /= Any_Fixed
3043 then
3044 Resolve (Aelmt);
3045 end if;
3047 if Etype (Aelmt) = Any_Type then
3048 Found := True;
3049 end if;
3050 end if;
3051 end Check_Elmt;
3053 begin
3054 Check_Aggr (N);
3055 end;
3056 end if;
3058 -- If node is a literal and context type has a user-defined
3059 -- literal aspect, rewrite node as a call to the corresponding
3060 -- function, which plays the role of an implicit conversion.
3062 if Nkind (N) in
3063 N_Numeric_Or_String_Literal | N_Identifier
3064 and then Has_Applicable_User_Defined_Literal (N, Typ)
3065 then
3066 Analyze_And_Resolve (N, Typ);
3067 return;
3068 end if;
3070 -- Looks like we have a type error, but check for special case
3071 -- of Address wanted, integer found, with the configuration pragma
3072 -- Allow_Integer_Address active. If we have this case, introduce
3073 -- an unchecked conversion to allow the integer expression to be
3074 -- treated as an Address. The reverse case of integer wanted,
3075 -- Address found, is treated in an analogous manner.
3077 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3078 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3079 Analyze_And_Resolve (N, Typ);
3080 return;
3082 -- Under relaxed RM semantics silently replace occurrences of null
3083 -- by System.Null_Address.
3085 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3086 Replace_Null_By_Null_Address (N);
3087 Analyze_And_Resolve (N, Typ);
3088 return;
3089 end if;
3091 -- That special Allow_Integer_Address check did not apply, so we
3092 -- have a real type error. If an error message was issued already,
3093 -- Found got reset to True, so if it's still False, issue standard
3094 -- Wrong_Type message.
3096 if not Found then
3097 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3098 declare
3099 Subp_Name : Node_Id;
3101 begin
3102 if Is_Entity_Name (Name (N)) then
3103 Subp_Name := Name (N);
3105 elsif Nkind (Name (N)) = N_Selected_Component then
3107 -- Protected operation: retrieve operation name
3109 Subp_Name := Selector_Name (Name (N));
3111 else
3112 raise Program_Error;
3113 end if;
3115 Error_Msg_Node_2 := Typ;
3116 Error_Msg_NE
3117 ("no visible interpretation of& matches expected type&",
3118 N, Subp_Name);
3119 end;
3121 if All_Errors_Mode then
3122 declare
3123 Index : Interp_Index;
3124 It : Interp;
3126 begin
3127 Error_Msg_N ("\\possible interpretations:", N);
3129 Get_First_Interp (Name (N), Index, It);
3130 while Present (It.Nam) loop
3131 Error_Msg_Sloc := Sloc (It.Nam);
3132 Error_Msg_Node_2 := It.Nam;
3133 Error_Msg_NE
3134 ("\\ type& for & declared#", N, It.Typ);
3135 Get_Next_Interp (Index, It);
3136 end loop;
3137 end;
3139 else
3140 Error_Msg_N ("\use -gnatf for details", N);
3141 end if;
3143 -- Recognize the case of a quantified expression being mistaken
3144 -- for an iterated component association because the user
3145 -- forgot the "all" or "some" keyword after "for". Because the
3146 -- error message starts with "missing ALL", we automatically
3147 -- benefit from the associated CODEFIX, which requires that
3148 -- the message is located on the identifier following "for"
3149 -- in order for the CODEFIX to insert "all" in the right place.
3151 elsif Nkind (N) = N_Aggregate
3152 and then List_Length (Component_Associations (N)) = 1
3153 and then Nkind (First (Component_Associations (N)))
3154 = N_Iterated_Component_Association
3155 and then Is_Boolean_Type (Typ)
3156 then
3157 if Present
3158 (Iterator_Specification
3159 (First (Component_Associations (N))))
3160 then
3161 Error_Msg_N -- CODEFIX
3162 ("missing ALL or SOME in quantified expression",
3163 Defining_Identifier
3164 (Iterator_Specification
3165 (First (Component_Associations (N)))));
3166 else
3167 Error_Msg_N -- CODEFIX
3168 ("missing ALL or SOME in quantified expression",
3169 Defining_Identifier
3170 (First (Component_Associations (N))));
3171 end if;
3173 -- For an operator with no interpretation, check whether
3174 -- one of its operands may be a user-defined literal.
3176 elsif Nkind (N) in N_Op
3177 and then Try_User_Defined_Literal (N, Typ)
3178 then
3179 return;
3181 else
3182 Wrong_Type (N, Typ);
3183 end if;
3184 end if;
3185 end if;
3187 Resolution_Failed;
3188 return;
3190 -- Test if we have more than one interpretation for the context
3192 elsif Ambiguous then
3193 Resolution_Failed;
3194 return;
3196 -- Only one interpretation
3198 else
3199 -- Prevent implicit conversions between access-to-subprogram types
3200 -- with different strub modes. Explicit conversions are acceptable in
3201 -- some circumstances. We don't have to be concerned about data or
3202 -- access-to-data types. Conversions between data types can safely
3203 -- drop or add strub attributes from types, because strub effects are
3204 -- associated with the locations rather than values. E.g., converting
3205 -- a hypothetical Strub_Integer variable to Integer would load the
3206 -- value from the variable, enabling stack scrabbing for the
3207 -- enclosing subprogram, and then convert the value to Integer. As
3208 -- for conversions between access-to-data types, that's no different
3209 -- from any other case of type punning.
3211 if Is_Access_Type (Typ)
3212 and then Ekind (Designated_Type (Typ)) = E_Subprogram_Type
3213 and then Is_Access_Type (Expr_Type)
3214 and then Ekind (Designated_Type (Expr_Type)) = E_Subprogram_Type
3215 then
3216 Check_Same_Strub_Mode
3217 (Designated_Type (Typ), Designated_Type (Expr_Type));
3218 end if;
3220 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3221 -- the "+" on T is abstract, and the operands are of universal type,
3222 -- the above code will have (incorrectly) resolved the "+" to the
3223 -- universal one in Standard. Therefore check for this case and give
3224 -- an error. We can't do this earlier, because it would cause legal
3225 -- cases to get errors (when some other type has an abstract "+").
3227 if Ada_Version >= Ada_2005
3228 and then Nkind (N) in N_Op
3229 and then Is_Overloaded (N)
3230 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3231 then
3232 Get_First_Interp (N, I, It);
3233 while Present (It.Typ) loop
3234 if Present (It.Abstract_Op)
3235 and then Etype (It.Abstract_Op) = Typ
3236 then
3237 Nondispatching_Call_To_Abstract_Operation
3238 (N, It.Abstract_Op);
3239 return;
3240 end if;
3242 Get_Next_Interp (I, It);
3243 end loop;
3244 end if;
3246 -- Here we have an acceptable interpretation for the context
3248 -- Propagate type information and normalize tree for various
3249 -- predefined operations. If the context only imposes a class of
3250 -- types, rather than a specific type, propagate the actual type
3251 -- downward.
3253 if Typ = Any_Integer or else
3254 Typ = Any_Boolean or else
3255 Typ = Any_Modular or else
3256 Typ = Any_Real or else
3257 Typ = Any_Discrete
3258 then
3259 Ctx_Type := Expr_Type;
3261 -- Any_Fixed is legal in a real context only if a specific fixed-
3262 -- point type is imposed. If Norman Cohen can be confused by this,
3263 -- it deserves a separate message.
3265 if Typ = Any_Real
3266 and then Expr_Type = Any_Fixed
3267 then
3268 Error_Msg_N ("illegal context for mixed mode operation", N);
3269 Set_Etype (N, Universal_Real);
3270 Ctx_Type := Universal_Real;
3271 end if;
3272 end if;
3274 -- A user-defined operator is transformed into a function call at
3275 -- this point, so that further processing knows that operators are
3276 -- really operators (i.e. are predefined operators). User-defined
3277 -- operators that are intrinsic are just renamings of the predefined
3278 -- ones, and need not be turned into calls either, but if they rename
3279 -- a different operator, we must transform the node accordingly.
3280 -- Instantiations of Unchecked_Conversion are intrinsic but are
3281 -- treated as functions, even if given an operator designator.
3283 if Nkind (N) in N_Op
3284 and then Present (Entity (N))
3285 and then Ekind (Entity (N)) /= E_Operator
3286 then
3287 if not Is_Predefined_Op (Entity (N)) then
3288 Rewrite_Operator_As_Call (N, Entity (N));
3290 elsif Present (Alias (Entity (N)))
3291 and then
3292 Nkind (Parent (Parent (Entity (N)))) =
3293 N_Subprogram_Renaming_Declaration
3294 then
3295 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3297 -- If the node is rewritten, it will be fully resolved in
3298 -- Rewrite_Renamed_Operator.
3300 if Analyzed (N) then
3301 return;
3302 end if;
3303 end if;
3304 end if;
3306 case N_Subexpr'(Nkind (N)) is
3307 when N_Aggregate =>
3308 Resolve_Aggregate (N, Ctx_Type);
3310 when N_Allocator =>
3311 Resolve_Allocator (N, Ctx_Type);
3313 when N_Short_Circuit =>
3314 Resolve_Short_Circuit (N, Ctx_Type);
3316 when N_Attribute_Reference =>
3317 Resolve_Attribute (N, Ctx_Type);
3319 when N_Case_Expression =>
3320 Resolve_Case_Expression (N, Ctx_Type);
3322 when N_Character_Literal =>
3323 Resolve_Character_Literal (N, Ctx_Type);
3325 when N_Delta_Aggregate =>
3326 Resolve_Delta_Aggregate (N, Ctx_Type);
3328 when N_Expanded_Name =>
3329 Resolve_Entity_Name (N, Ctx_Type);
3331 when N_Explicit_Dereference =>
3332 Resolve_Explicit_Dereference (N, Ctx_Type);
3334 when N_Expression_With_Actions =>
3335 Resolve_Expression_With_Actions (N, Ctx_Type);
3337 when N_Extension_Aggregate =>
3338 Resolve_Extension_Aggregate (N, Ctx_Type);
3340 when N_Function_Call =>
3341 Resolve_Call (N, Ctx_Type);
3343 when N_Identifier =>
3344 Resolve_Entity_Name (N, Ctx_Type);
3346 when N_If_Expression =>
3347 Resolve_If_Expression (N, Ctx_Type);
3349 when N_Indexed_Component =>
3350 Resolve_Indexed_Component (N, Ctx_Type);
3352 when N_Integer_Literal =>
3353 Resolve_Integer_Literal (N, Ctx_Type);
3355 when N_Membership_Test =>
3356 Resolve_Membership_Op (N, Ctx_Type);
3358 when N_Null =>
3359 Resolve_Null (N, Ctx_Type);
3361 when N_Op_And
3362 | N_Op_Or
3363 | N_Op_Xor
3365 Resolve_Logical_Op (N, Ctx_Type);
3367 when N_Op_Eq
3368 | N_Op_Ne
3370 Resolve_Equality_Op (N, Ctx_Type);
3372 when N_Op_Ge
3373 | N_Op_Gt
3374 | N_Op_Le
3375 | N_Op_Lt
3377 Resolve_Comparison_Op (N, Ctx_Type);
3379 when N_Op_Not =>
3380 Resolve_Op_Not (N, Ctx_Type);
3382 when N_Op_Add
3383 | N_Op_Divide
3384 | N_Op_Mod
3385 | N_Op_Multiply
3386 | N_Op_Rem
3387 | N_Op_Subtract
3389 Resolve_Arithmetic_Op (N, Ctx_Type);
3391 when N_Op_Concat =>
3392 Resolve_Op_Concat (N, Ctx_Type);
3394 when N_Op_Expon =>
3395 Resolve_Op_Expon (N, Ctx_Type);
3397 when N_Op_Abs
3398 | N_Op_Minus
3399 | N_Op_Plus
3401 Resolve_Unary_Op (N, Ctx_Type);
3403 when N_Op_Shift =>
3404 Resolve_Shift (N, Ctx_Type);
3406 when N_Procedure_Call_Statement =>
3407 Resolve_Call (N, Ctx_Type);
3409 when N_Operator_Symbol =>
3410 Resolve_Operator_Symbol (N, Ctx_Type);
3412 when N_Qualified_Expression =>
3413 Resolve_Qualified_Expression (N, Ctx_Type);
3415 -- Why is the following null, needs a comment ???
3417 when N_Quantified_Expression =>
3418 null;
3420 when N_Raise_Expression =>
3421 Resolve_Raise_Expression (N, Ctx_Type);
3423 when N_Raise_xxx_Error =>
3424 Set_Etype (N, Ctx_Type);
3426 when N_Range =>
3427 Resolve_Range (N, Ctx_Type);
3429 when N_Real_Literal =>
3430 Resolve_Real_Literal (N, Ctx_Type);
3432 when N_Reference =>
3433 Resolve_Reference (N, Ctx_Type);
3435 when N_Selected_Component =>
3436 Resolve_Selected_Component (N, Ctx_Type);
3438 when N_Slice =>
3439 Resolve_Slice (N, Ctx_Type);
3441 when N_String_Literal =>
3442 Resolve_String_Literal (N, Ctx_Type);
3444 when N_Interpolated_String_Literal =>
3445 Resolve_Interpolated_String_Literal (N, Ctx_Type);
3447 when N_Target_Name =>
3448 Resolve_Target_Name (N, Ctx_Type);
3450 when N_Type_Conversion =>
3451 Resolve_Type_Conversion (N, Ctx_Type);
3453 when N_Unchecked_Expression =>
3454 Resolve_Unchecked_Expression (N, Ctx_Type);
3456 when N_Unchecked_Type_Conversion =>
3457 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3458 end case;
3460 -- Mark relevant use-type and use-package clauses as effective using
3461 -- the original node because constant folding may have occurred and
3462 -- removed references that need to be examined.
3464 if Nkind (Original_Node (N)) in N_Op then
3465 Mark_Use_Clauses (Original_Node (N));
3466 end if;
3468 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3469 -- expression of an anonymous access type that occurs in the context
3470 -- of a named general access type, except when the expression is that
3471 -- of a membership test. This ensures proper legality checking in
3472 -- terms of allowed conversions (expressions that would be illegal to
3473 -- convert implicitly are allowed in membership tests).
3475 if Ada_Version >= Ada_2012
3476 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3477 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3478 and then Nkind (Parent (N)) not in N_Membership_Test
3479 then
3480 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3481 Analyze_And_Resolve (N, Ctx_Type);
3482 end if;
3484 -- If the subexpression was replaced by a non-subexpression, then
3485 -- all we do is to expand it. The only legitimate case we know of
3486 -- is converting procedure call statement to entry call statements,
3487 -- but there may be others, so we are making this test general.
3489 if Nkind (N) not in N_Subexpr then
3490 Debug_A_Exit ("resolving ", N, " (done)");
3491 Expand (N);
3492 return;
3493 end if;
3495 -- The expression is definitely NOT overloaded at this point, so
3496 -- we reset the Is_Overloaded flag to avoid any confusion when
3497 -- reanalyzing the node.
3499 Set_Is_Overloaded (N, False);
3501 -- Freeze expression type, entity if it is a name, and designated
3502 -- type if it is an allocator (RM 13.14(10,11,13)).
3504 -- Now that the resolution of the type of the node is complete, and
3505 -- we did not detect an error, we can expand this node. We skip the
3506 -- expand call if we are in a default expression, see section
3507 -- "Handling of Default Expressions" in Sem spec.
3509 Debug_A_Exit ("resolving ", N, " (done)");
3511 -- We unconditionally freeze the expression, even if we are in
3512 -- default expression mode (the Freeze_Expression routine tests this
3513 -- flag and only freezes static types if it is set).
3515 -- Ada 2012 (AI05-177): The declaration of an expression function
3516 -- does not cause freezing, but we never reach here in that case.
3517 -- Here we are resolving the corresponding expanded body, so we do
3518 -- need to perform normal freezing.
3520 -- As elsewhere we do not emit freeze node within a generic.
3522 if not Inside_A_Generic then
3523 Freeze_Expression (N);
3524 end if;
3526 -- Now we can do the expansion
3528 Expand (N);
3529 end if;
3530 end Resolve;
3532 -------------
3533 -- Resolve --
3534 -------------
3536 -- Version with check(s) suppressed
3538 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3539 begin
3540 if Suppress = All_Checks then
3541 declare
3542 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3543 begin
3544 Scope_Suppress.Suppress := (others => True);
3545 Resolve (N, Typ);
3546 Scope_Suppress.Suppress := Sva;
3547 end;
3549 else
3550 declare
3551 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3552 begin
3553 Scope_Suppress.Suppress (Suppress) := True;
3554 Resolve (N, Typ);
3555 Scope_Suppress.Suppress (Suppress) := Svg;
3556 end;
3557 end if;
3558 end Resolve;
3560 -------------
3561 -- Resolve --
3562 -------------
3564 -- Version with implicit type
3566 procedure Resolve (N : Node_Id) is
3567 begin
3568 Resolve (N, Etype (N));
3569 end Resolve;
3571 ---------------------
3572 -- Resolve_Actuals --
3573 ---------------------
3575 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3576 Loc : constant Source_Ptr := Sloc (N);
3577 A : Node_Id;
3578 A_Typ : Entity_Id := Empty; -- init to avoid warning
3579 F : Entity_Id;
3580 F_Typ : Entity_Id;
3581 Prev : Node_Id := Empty;
3582 Orig_A : Node_Id;
3583 Real_F : Entity_Id := Empty; -- init to avoid warning
3585 Real_Subp : Entity_Id;
3586 -- If the subprogram being called is an inherited operation for
3587 -- a formal derived type in an instance, Real_Subp is the subprogram
3588 -- that will be called. It may have different formal names than the
3589 -- operation of the formal in the generic, so after actual is resolved
3590 -- the name of the actual in a named association must carry the name
3591 -- of the actual of the subprogram being called.
3593 procedure Check_Aliased_Parameter;
3594 -- Check rules on aliased parameters and related accessibility rules
3595 -- in (RM 3.10.2 (10.2-10.4)).
3597 procedure Check_Argument_Order;
3598 -- Performs a check for the case where the actuals are all simple
3599 -- identifiers that correspond to the formal names, but in the wrong
3600 -- order, which is considered suspicious and cause for a warning.
3602 procedure Check_Prefixed_Call;
3603 -- If the original node is an overloaded call in prefix notation,
3604 -- insert an 'Access or a dereference as needed over the first actual.
3605 -- Try_Object_Operation has already verified that there is a valid
3606 -- interpretation, but the form of the actual can only be determined
3607 -- once the primitive operation is identified.
3609 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3610 -- Emit an error concerning the illegal usage of an effectively volatile
3611 -- object for reading in interfering context (SPARK RM 7.1.3(10)).
3613 procedure Insert_Default;
3614 -- If the actual is missing in a call, insert in the actuals list
3615 -- an instance of the default expression. The insertion is always
3616 -- a named association.
3618 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3619 -- Check whether T1 and T2, or their full views, are derived from a
3620 -- common type. Used to enforce the restrictions on array conversions
3621 -- of AI95-00246.
3623 function Static_Concatenation (N : Node_Id) return Boolean;
3624 -- Predicate to determine whether an actual that is a concatenation
3625 -- will be evaluated statically and does not need a transient scope.
3626 -- This must be determined before the actual is resolved and expanded
3627 -- because if needed the transient scope must be introduced earlier.
3629 -----------------------------
3630 -- Check_Aliased_Parameter --
3631 -----------------------------
3633 procedure Check_Aliased_Parameter is
3634 Nominal_Subt : Entity_Id;
3636 begin
3637 if Is_Aliased (F) then
3638 if Is_Tagged_Type (A_Typ) then
3639 null;
3641 elsif Is_Aliased_View (A) then
3642 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3643 Nominal_Subt := Base_Type (A_Typ);
3644 else
3645 Nominal_Subt := A_Typ;
3646 end if;
3648 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3649 null;
3651 -- In a generic body assume the worst for generic formals:
3652 -- they can have a constrained partial view (AI05-041).
3654 elsif Has_Discriminants (F_Typ)
3655 and then not Is_Constrained (F_Typ)
3656 and then not Object_Type_Has_Constrained_Partial_View
3657 (Typ => F_Typ, Scop => Current_Scope)
3658 then
3659 null;
3661 else
3662 Error_Msg_NE ("untagged actual does not statically match "
3663 & "aliased formal&", A, F);
3664 end if;
3666 else
3667 Error_Msg_NE ("actual for aliased formal& must be "
3668 & "aliased object", A, F);
3669 end if;
3671 if Ekind (Nam) = E_Procedure then
3672 null;
3674 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3675 if Nkind (Parent (N)) = N_Type_Conversion
3676 and then Type_Access_Level (Etype (Parent (N)))
3677 < Static_Accessibility_Level (A, Object_Decl_Level)
3678 then
3679 Error_Msg_N ("aliased actual has wrong accessibility", A);
3680 end if;
3682 elsif Nkind (Parent (N)) = N_Qualified_Expression
3683 and then Nkind (Parent (Parent (N))) = N_Allocator
3684 and then Type_Access_Level (Etype (Parent (Parent (N))))
3685 < Static_Accessibility_Level (A, Object_Decl_Level)
3686 then
3687 Error_Msg_N
3688 ("aliased actual in allocator has wrong accessibility", A);
3689 end if;
3690 end if;
3691 end Check_Aliased_Parameter;
3693 --------------------------
3694 -- Check_Argument_Order --
3695 --------------------------
3697 procedure Check_Argument_Order is
3698 begin
3699 -- Nothing to do if no parameters, or original node is neither a
3700 -- function call nor a procedure call statement (happens in the
3701 -- operator-transformed-to-function call case), or the call is to an
3702 -- operator symbol (which is usually in infix form), or the call does
3703 -- not come from source, or this warning is off.
3705 if not Warn_On_Parameter_Order
3706 or else No (Parameter_Associations (N))
3707 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3708 or else (Nkind (Name (N)) = N_Identifier
3709 and then Present (Entity (Name (N)))
3710 and then Nkind (Entity (Name (N))) =
3711 N_Defining_Operator_Symbol)
3712 or else not Comes_From_Source (N)
3713 then
3714 return;
3715 end if;
3717 declare
3718 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3720 begin
3721 -- Nothing to do if only one parameter
3723 if Nargs < 2 then
3724 return;
3725 end if;
3727 -- Here if at least two arguments
3729 declare
3730 Actuals : array (1 .. Nargs) of Node_Id;
3731 Actual : Node_Id;
3732 Formal : Node_Id;
3734 Wrong_Order : Boolean := False;
3735 -- Set True if an out of order case is found
3737 begin
3738 -- Collect identifier names of actuals, fail if any actual is
3739 -- not a simple identifier, and record max length of name.
3741 Actual := First (Parameter_Associations (N));
3742 for J in Actuals'Range loop
3743 if Nkind (Actual) /= N_Identifier then
3744 return;
3745 else
3746 Actuals (J) := Actual;
3747 Next (Actual);
3748 end if;
3749 end loop;
3751 -- If we got this far, all actuals are identifiers and the list
3752 -- of their names is stored in the Actuals array.
3754 Formal := First_Formal (Nam);
3755 for J in Actuals'Range loop
3757 -- If we ran out of formals, that's odd, probably an error
3758 -- which will be detected elsewhere, but abandon the search.
3760 if No (Formal) then
3761 return;
3762 end if;
3764 -- If name matches and is in order OK
3766 if Chars (Formal) = Chars (Actuals (J)) then
3767 null;
3769 else
3770 -- If no match, see if it is elsewhere in list and if so
3771 -- flag potential wrong order if type is compatible.
3773 for K in Actuals'Range loop
3774 if Chars (Formal) = Chars (Actuals (K))
3775 and then
3776 Has_Compatible_Type (Actuals (K), Etype (Formal))
3777 then
3778 Wrong_Order := True;
3779 goto Continue;
3780 end if;
3781 end loop;
3783 -- No match
3785 return;
3786 end if;
3788 <<Continue>> Next_Formal (Formal);
3789 end loop;
3791 -- If Formals left over, also probably an error, skip warning
3793 if Present (Formal) then
3794 return;
3795 end if;
3797 -- Here we give the warning if something was out of order
3799 if Wrong_Order then
3800 Error_Msg_N
3801 ("?.p?actuals for this call may be in wrong order", N);
3802 end if;
3803 end;
3804 end;
3805 end Check_Argument_Order;
3807 -------------------------
3808 -- Check_Prefixed_Call --
3809 -------------------------
3811 procedure Check_Prefixed_Call is
3812 Act : constant Node_Id := First_Actual (N);
3813 A_Type : constant Entity_Id := Etype (Act);
3814 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3815 Orig : constant Node_Id := Original_Node (N);
3816 New_A : Node_Id;
3818 begin
3819 -- Check whether the call is a prefixed call, with or without
3820 -- additional actuals.
3822 if Nkind (Orig) = N_Selected_Component
3823 or else
3824 (Nkind (Orig) = N_Indexed_Component
3825 and then Nkind (Prefix (Orig)) = N_Selected_Component
3826 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3827 and then Is_Entity_Name (Act)
3828 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3829 then
3830 if Is_Access_Type (A_Type)
3831 and then not Is_Access_Type (F_Type)
3832 then
3833 -- Introduce dereference on object in prefix
3835 New_A :=
3836 Make_Explicit_Dereference (Sloc (Act),
3837 Prefix => Relocate_Node (Act));
3838 Rewrite (Act, New_A);
3839 Analyze (Act);
3841 elsif Is_Access_Type (F_Type)
3842 and then not Is_Access_Type (A_Type)
3843 then
3844 -- Introduce an implicit 'Access in prefix
3846 if not Is_Aliased_View (Act) then
3847 Error_Msg_NE
3848 ("object in prefixed call to& must be aliased "
3849 & "(RM 4.1.3 (13 1/2))",
3850 Prefix (Act), Nam);
3851 end if;
3853 Rewrite (Act,
3854 Make_Attribute_Reference (Loc,
3855 Attribute_Name => Name_Access,
3856 Prefix => Relocate_Node (Act)));
3857 end if;
3859 Analyze (Act);
3860 end if;
3861 end Check_Prefixed_Call;
3863 ---------------------------------------
3864 -- Flag_Effectively_Volatile_Objects --
3865 ---------------------------------------
3867 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3868 function Flag_Object (N : Node_Id) return Traverse_Result;
3869 -- Determine whether arbitrary node N denotes an effectively volatile
3870 -- object for reading and if it does, emit an error.
3872 -----------------
3873 -- Flag_Object --
3874 -----------------
3876 function Flag_Object (N : Node_Id) return Traverse_Result is
3877 Id : Entity_Id;
3879 begin
3880 case Nkind (N) is
3881 -- Do not consider nested function calls because they have
3882 -- already been processed during their own resolution.
3884 when N_Function_Call =>
3885 return Skip;
3887 when N_Identifier | N_Expanded_Name =>
3888 Id := Entity (N);
3890 -- Identifiers of components and discriminants are not names
3891 -- in the sense of Ada RM 4.1. They can only occur as a
3892 -- selector_name in selected_component or as a choice in
3893 -- component_association.
3895 if Present (Id)
3896 and then Is_Object (Id)
3897 and then Ekind (Id) not in E_Component | E_Discriminant
3898 and then Is_Effectively_Volatile_For_Reading (Id)
3899 and then
3900 not Is_OK_Volatile_Context (Context => Parent (N),
3901 Obj_Ref => N,
3902 Check_Actuals => True)
3903 then
3904 Error_Msg_N
3905 ("volatile object cannot appear in this context"
3906 & " (SPARK RM 7.1.3(10))", N);
3907 end if;
3909 return Skip;
3911 when others =>
3912 return OK;
3913 end case;
3914 end Flag_Object;
3916 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3918 -- Start of processing for Flag_Effectively_Volatile_Objects
3920 begin
3921 Flag_Objects (Expr);
3922 end Flag_Effectively_Volatile_Objects;
3924 --------------------
3925 -- Insert_Default --
3926 --------------------
3928 procedure Insert_Default is
3929 Actval : Node_Id;
3930 Assoc : Node_Id;
3932 begin
3933 -- Missing argument in call, nothing to insert
3935 if No (Default_Value (F)) then
3936 return;
3938 else
3939 -- Note that we do a full New_Copy_Tree, so that any associated
3940 -- Itypes are properly copied. This may not be needed any more,
3941 -- but it does no harm as a safety measure. Defaults of a generic
3942 -- formal may be out of bounds of the corresponding actual (see
3943 -- cc1311b) and an additional check may be required.
3945 Actval :=
3946 New_Copy_Tree
3947 (Default_Value (F),
3948 New_Scope => Current_Scope,
3949 New_Sloc => Loc);
3951 -- Propagate dimension information, if any.
3953 Copy_Dimensions (Default_Value (F), Actval);
3955 if Is_Concurrent_Type (Scope (Nam))
3956 and then Has_Discriminants (Scope (Nam))
3957 then
3958 Replace_Actual_Discriminants (N, Actval);
3959 end if;
3961 if Is_Overloadable (Nam)
3962 and then Present (Alias (Nam))
3963 then
3964 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3965 and then not Is_Tagged_Type (Etype (F))
3966 then
3967 -- If default is a real literal, do not introduce a
3968 -- conversion whose effect may depend on the run-time
3969 -- size of universal real.
3971 if Nkind (Actval) = N_Real_Literal then
3972 Set_Etype (Actval, Base_Type (Etype (F)));
3973 else
3974 Actval := Unchecked_Convert_To (Etype (F), Actval);
3975 end if;
3976 end if;
3978 if Is_Scalar_Type (Etype (F)) then
3979 Enable_Range_Check (Actval);
3980 end if;
3982 Set_Parent (Actval, N);
3984 -- Resolve aggregates with their base type, to avoid scope
3985 -- anomalies: the subtype was first built in the subprogram
3986 -- declaration, and the current call may be nested.
3988 if Nkind (Actval) = N_Aggregate then
3989 Analyze_And_Resolve (Actval, Etype (F));
3990 else
3991 Analyze_And_Resolve (Actval, Etype (Actval));
3992 end if;
3994 else
3995 Set_Parent (Actval, N);
3997 -- See note above concerning aggregates
3999 if Nkind (Actval) = N_Aggregate
4000 and then Has_Discriminants (Etype (Actval))
4001 then
4002 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
4004 -- Resolve entities with their own type, which may differ from
4005 -- the type of a reference in a generic context (the view
4006 -- swapping mechanism did not anticipate the re-analysis of
4007 -- default values in calls).
4009 elsif Is_Entity_Name (Actval) then
4010 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
4012 else
4013 Analyze_And_Resolve (Actval, Etype (Actval));
4014 end if;
4015 end if;
4017 -- If default is a tag indeterminate function call, propagate tag
4018 -- to obtain proper dispatching.
4020 if Is_Controlling_Formal (F)
4021 and then Nkind (Default_Value (F)) = N_Function_Call
4022 then
4023 Set_Is_Controlling_Actual (Actval);
4024 end if;
4025 end if;
4027 -- If the default expression raises constraint error, then just
4028 -- silently replace it with an N_Raise_Constraint_Error node, since
4029 -- we already gave the warning on the subprogram spec. If node is
4030 -- already a Raise_Constraint_Error leave as is, to prevent loops in
4031 -- the warnings removal machinery.
4033 if Raises_Constraint_Error (Actval)
4034 and then Nkind (Actval) /= N_Raise_Constraint_Error
4035 then
4036 Rewrite (Actval,
4037 Make_Raise_Constraint_Error (Loc,
4038 Reason => CE_Range_Check_Failed));
4040 Set_Raises_Constraint_Error (Actval);
4041 Set_Etype (Actval, Etype (F));
4042 end if;
4044 Assoc :=
4045 Make_Parameter_Association (Loc,
4046 Explicit_Actual_Parameter => Actval,
4047 Selector_Name => Make_Identifier (Loc, Chars (F)));
4049 -- Case of insertion is first named actual
4051 if No (Prev)
4052 or else Nkind (Parent (Prev)) /= N_Parameter_Association
4053 then
4054 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
4055 Set_First_Named_Actual (N, Actval);
4057 if No (Prev) then
4058 if No (Parameter_Associations (N)) then
4059 Set_Parameter_Associations (N, New_List (Assoc));
4060 else
4061 Append (Assoc, Parameter_Associations (N));
4062 end if;
4064 else
4065 Insert_After (Prev, Assoc);
4066 end if;
4068 -- Case of insertion is not first named actual
4070 else
4071 Set_Next_Named_Actual
4072 (Assoc, Next_Named_Actual (Parent (Prev)));
4073 Set_Next_Named_Actual (Parent (Prev), Actval);
4074 Append (Assoc, Parameter_Associations (N));
4075 end if;
4077 Mark_Rewrite_Insertion (Assoc);
4078 Mark_Rewrite_Insertion (Actval);
4080 Prev := Actval;
4081 end Insert_Default;
4083 -------------------
4084 -- Same_Ancestor --
4085 -------------------
4087 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
4088 FT1 : Entity_Id := T1;
4089 FT2 : Entity_Id := T2;
4091 begin
4092 if Is_Private_Type (T1)
4093 and then Present (Full_View (T1))
4094 then
4095 FT1 := Full_View (T1);
4096 end if;
4098 if Is_Private_Type (T2)
4099 and then Present (Full_View (T2))
4100 then
4101 FT2 := Full_View (T2);
4102 end if;
4104 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
4105 end Same_Ancestor;
4107 --------------------------
4108 -- Static_Concatenation --
4109 --------------------------
4111 function Static_Concatenation (N : Node_Id) return Boolean is
4112 begin
4113 case Nkind (N) is
4114 when N_String_Literal =>
4115 return True;
4117 when N_Op_Concat =>
4119 -- Concatenation is static when both operands are static and
4120 -- the concatenation operator is a predefined one.
4122 return Scope (Entity (N)) = Standard_Standard
4123 and then
4124 Static_Concatenation (Left_Opnd (N))
4125 and then
4126 Static_Concatenation (Right_Opnd (N));
4128 when others =>
4129 if Is_Entity_Name (N) then
4130 declare
4131 Ent : constant Entity_Id := Entity (N);
4132 begin
4133 return Ekind (Ent) = E_Constant
4134 and then Present (Constant_Value (Ent))
4135 and then
4136 Is_OK_Static_Expression (Constant_Value (Ent));
4137 end;
4139 else
4140 return False;
4141 end if;
4142 end case;
4143 end Static_Concatenation;
4145 -- Start of processing for Resolve_Actuals
4147 begin
4148 Check_Argument_Order;
4150 if Is_Overloadable (Nam)
4151 and then Is_Inherited_Operation (Nam)
4152 and then In_Instance
4153 and then Present (Alias (Nam))
4154 and then Present (Overridden_Operation (Alias (Nam)))
4155 then
4156 Real_Subp := Alias (Nam);
4157 else
4158 Real_Subp := Empty;
4159 end if;
4161 if Present (First_Actual (N)) then
4162 Check_Prefixed_Call;
4163 end if;
4165 A := First_Actual (N);
4166 F := First_Formal (Nam);
4168 if Present (Real_Subp) then
4169 Real_F := First_Formal (Real_Subp);
4170 end if;
4172 while Present (F) loop
4173 if No (A) and then Needs_No_Actuals (Nam) then
4174 null;
4176 -- If we have an error in any formal or actual, indicated by a type
4177 -- of Any_Type, then abandon resolution attempt, and set result type
4178 -- to Any_Type.
4180 elsif Etype (F) = Any_Type then
4181 Set_Etype (N, Any_Type);
4182 return;
4184 elsif Present (A) and then Etype (A) = Any_Type then
4185 -- For the peculiar case of a user-defined comparison or equality
4186 -- operator that does not return a boolean type, the operands may
4187 -- have been ambiguous for the predefined operator and, therefore,
4188 -- marked with Any_Type. Since the operation has been resolved to
4189 -- the user-defined operator, that is irrelevant, so reset Etype.
4191 if Nkind (Original_Node (N)) in N_Op_Compare
4192 and then not Is_Boolean_Type (Etype (N))
4193 then
4194 Set_Etype (A, Etype (F));
4196 -- Also skip this if the actual is a Raise_Expression, whose type
4197 -- is imposed from context.
4199 elsif Nkind (A) = N_Raise_Expression then
4200 null;
4202 else
4203 Set_Etype (N, Any_Type);
4204 return;
4205 end if;
4206 end if;
4208 -- Case where actual is present
4210 -- If the actual is an entity, generate a reference to it now. We
4211 -- do this before the actual is resolved, because a formal of some
4212 -- protected subprogram, or a task discriminant, will be rewritten
4213 -- during expansion, and the source entity reference may be lost.
4215 if Present (A)
4216 and then Is_Entity_Name (A)
4217 and then Comes_From_Source (A)
4218 then
4219 -- Annotate the tree by creating a variable reference marker when
4220 -- the actual denotes a variable reference, in case the reference
4221 -- is folded or optimized away. The variable reference marker is
4222 -- automatically saved for later examination by the ABE Processing
4223 -- phase. The status of the reference is set as follows:
4225 -- status mode
4226 -- read IN, IN OUT
4227 -- write IN OUT, OUT
4229 if Needs_Variable_Reference_Marker
4230 (N => A,
4231 Calls_OK => True)
4232 then
4233 Build_Variable_Reference_Marker
4234 (N => A,
4235 Read => Ekind (F) /= E_Out_Parameter,
4236 Write => Ekind (F) /= E_In_Parameter);
4237 end if;
4239 Orig_A := Entity (A);
4241 if Present (Orig_A) then
4242 if Is_Formal (Orig_A)
4243 and then Ekind (F) /= E_In_Parameter
4244 then
4245 Generate_Reference (Orig_A, A, 'm');
4247 elsif not Is_Overloaded (A) then
4248 if Ekind (F) /= E_Out_Parameter then
4249 Generate_Reference (Orig_A, A);
4251 -- RM 6.4.1(12): For an out parameter that is passed by
4252 -- copy, the formal parameter object is created, and:
4254 -- * For an access type, the formal parameter is initialized
4255 -- from the value of the actual, without checking that the
4256 -- value satisfies any constraint, any predicate, or any
4257 -- exclusion of the null value.
4259 -- * For a scalar type that has the Default_Value aspect
4260 -- specified, the formal parameter is initialized from the
4261 -- value of the actual, without checking that the value
4262 -- satisfies any constraint or any predicate.
4263 -- I do not understand why this case is included??? this is
4264 -- not a case where an OUT parameter is treated as IN OUT.
4266 -- * For a composite type with discriminants or that has
4267 -- implicit initial values for any subcomponents, the
4268 -- behavior is as for an in out parameter passed by copy.
4270 -- Hence for these cases we generate the read reference now
4271 -- (the write reference will be generated later by
4272 -- Note_Possible_Modification).
4274 elsif Is_By_Copy_Type (Etype (F))
4275 and then
4276 (Is_Access_Type (Etype (F))
4277 or else
4278 (Is_Scalar_Type (Etype (F))
4279 and then
4280 Present (Default_Aspect_Value (Etype (F))))
4281 or else
4282 (Is_Composite_Type (Etype (F))
4283 and then (Has_Discriminants (Etype (F))
4284 or else Is_Partially_Initialized_Type
4285 (Etype (F)))))
4286 then
4287 Generate_Reference (Orig_A, A);
4288 end if;
4289 end if;
4290 end if;
4291 end if;
4293 if Present (A)
4294 and then (Nkind (Parent (A)) /= N_Parameter_Association
4295 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4296 then
4297 -- If style checking mode on, check match of formal name
4299 if Style_Check then
4300 if Nkind (Parent (A)) = N_Parameter_Association then
4301 Check_Identifier (Selector_Name (Parent (A)), F);
4302 end if;
4303 end if;
4305 -- If the formal is Out or In_Out, do not resolve and expand the
4306 -- conversion, because it is subsequently expanded into explicit
4307 -- temporaries and assignments. However, the object of the
4308 -- conversion can be resolved. An exception is the case of tagged
4309 -- type conversion with a class-wide actual. In that case we want
4310 -- the tag check to occur and no temporary will be needed (no
4311 -- representation change can occur) and the parameter is passed by
4312 -- reference, so we go ahead and resolve the type conversion.
4313 -- Another exception is the case of reference to component or
4314 -- subcomponent of a bit-packed array, in which case we want to
4315 -- defer expansion to the point the in and out assignments are
4316 -- performed.
4318 if Ekind (F) /= E_In_Parameter
4319 and then Nkind (A) = N_Type_Conversion
4320 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4321 and then not Is_Interface (Etype (A))
4322 then
4323 declare
4324 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4326 begin
4327 -- Check RM 4.6 (24.2/2)
4329 if Is_Array_Type (Etype (F))
4330 and then Is_View_Conversion (A)
4331 then
4332 -- In a view conversion, the conversion must be legal in
4333 -- both directions, and thus both component types must be
4334 -- aliased, or neither (4.6 (8)).
4336 -- Check RM 4.6 (24.8/2)
4338 if Has_Aliased_Components (Expr_Typ) /=
4339 Has_Aliased_Components (Etype (F))
4340 then
4341 -- This normally illegal conversion is legal in an
4342 -- expanded instance body because of RM 12.3(11).
4343 -- At runtime, conversion must create a new object.
4345 if not In_Instance then
4346 Error_Msg_N
4347 ("both component types in a view conversion must"
4348 & " be aliased, or neither", A);
4349 end if;
4351 -- Check RM 4.6 (24/3)
4353 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4354 -- Check view conv between unrelated by ref array
4355 -- types.
4357 if Is_By_Reference_Type (Etype (F))
4358 or else Is_By_Reference_Type (Expr_Typ)
4359 then
4360 Error_Msg_N
4361 ("view conversion between unrelated by reference "
4362 & "array types not allowed ('A'I-00246)", A);
4364 -- In Ada 2005 mode, check view conversion component
4365 -- type cannot be private, tagged, or volatile. Note
4366 -- that we only apply this to source conversions. The
4367 -- generated code can contain conversions which are
4368 -- not subject to this test, and we cannot extract the
4369 -- component type in such cases since it is not
4370 -- present.
4372 elsif Comes_From_Source (A)
4373 and then Ada_Version >= Ada_2005
4374 then
4375 declare
4376 Comp_Type : constant Entity_Id :=
4377 Component_Type (Expr_Typ);
4378 begin
4379 if (Is_Private_Type (Comp_Type)
4380 and then not Is_Generic_Type (Comp_Type))
4381 or else Is_Tagged_Type (Comp_Type)
4382 or else Is_Volatile (Comp_Type)
4383 then
4384 Error_Msg_N
4385 ("component type of a view conversion " &
4386 "cannot be private, tagged, or volatile" &
4387 " (RM 4.6 (24))",
4388 Expression (A));
4389 end if;
4390 end;
4391 end if;
4392 end if;
4394 -- AI12-0074 & AI12-0377
4395 -- Check 6.4.1: If the mode is out, the actual parameter is
4396 -- a view conversion, and the type of the formal parameter
4397 -- is a scalar type, then either:
4398 -- - the target and operand type both do not have the
4399 -- Default_Value aspect specified; or
4400 -- - the target and operand type both have the
4401 -- Default_Value aspect specified, and there shall exist
4402 -- a type (other than a root numeric type) that is an
4403 -- ancestor of both the target type and the operand
4404 -- type.
4406 elsif Ekind (F) = E_Out_Parameter
4407 and then Is_Scalar_Type (Etype (F))
4408 then
4409 if Has_Default_Aspect (Etype (F)) /=
4410 Has_Default_Aspect (Expr_Typ)
4411 then
4412 Error_Msg_N
4413 ("view conversion requires Default_Value on both " &
4414 "types (RM 6.4.1)", A);
4415 elsif Has_Default_Aspect (Expr_Typ)
4416 and then not Same_Ancestor (Etype (F), Expr_Typ)
4417 then
4418 Error_Msg_N
4419 ("view conversion between unrelated types with "
4420 & "Default_Value not allowed (RM 6.4.1)", A);
4421 end if;
4422 end if;
4423 end;
4425 -- Resolve expression if conversion is all OK
4427 if (Conversion_OK (A)
4428 or else Valid_Conversion (A, Etype (A), Expression (A)))
4429 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4430 then
4431 Resolve (Expression (A));
4432 end if;
4434 -- If the actual is a function call that returns a limited
4435 -- unconstrained object that needs finalization, create a
4436 -- transient scope for it, so that it can receive the proper
4437 -- finalization list.
4439 elsif Expander_Active
4440 and then Nkind (A) = N_Function_Call
4441 and then Is_Limited_Record (Etype (F))
4442 and then not Is_Constrained (Etype (F))
4443 and then (Needs_Finalization (Etype (F))
4444 or else Has_Task (Etype (F)))
4445 then
4446 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4447 Resolve (A, Etype (F));
4449 -- A small optimization: if one of the actuals is a concatenation
4450 -- create a block around a procedure call to recover stack space.
4451 -- This alleviates stack usage when several procedure calls in
4452 -- the same statement list use concatenation. We do not perform
4453 -- this wrapping for code statements, where the argument is a
4454 -- static string, and we want to preserve warnings involving
4455 -- sequences of such statements.
4457 elsif Expander_Active
4458 and then Nkind (A) = N_Op_Concat
4459 and then Nkind (N) = N_Procedure_Call_Statement
4460 and then not (Is_Intrinsic_Subprogram (Nam)
4461 and then Chars (Nam) = Name_Asm)
4462 and then not Static_Concatenation (A)
4463 then
4464 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4465 Resolve (A, Etype (F));
4467 else
4468 if Nkind (A) = N_Type_Conversion
4469 and then Is_Array_Type (Etype (F))
4470 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4471 and then
4472 (Is_Limited_Type (Etype (F))
4473 or else Is_Limited_Type (Etype (Expression (A))))
4474 then
4475 Error_Msg_N
4476 ("conversion between unrelated limited array types not "
4477 & "allowed ('A'I-00246)", A);
4479 if Is_Limited_Type (Etype (F)) then
4480 Explain_Limited_Type (Etype (F), A);
4481 end if;
4483 if Is_Limited_Type (Etype (Expression (A))) then
4484 Explain_Limited_Type (Etype (Expression (A)), A);
4485 end if;
4486 end if;
4488 -- (Ada 2005: AI-251): If the actual is an allocator whose
4489 -- directly designated type is a class-wide interface, we build
4490 -- an anonymous access type to use it as the type of the
4491 -- allocator. Later, when the subprogram call is expanded, if
4492 -- the interface has a secondary dispatch table the expander
4493 -- will add a type conversion to force the correct displacement
4494 -- of the pointer.
4496 if Nkind (A) = N_Allocator then
4497 declare
4498 DDT : constant Entity_Id :=
4499 Directly_Designated_Type (Base_Type (Etype (F)));
4501 begin
4502 -- Displace the pointer to the object to reference its
4503 -- secondary dispatch table.
4505 if Is_Class_Wide_Type (DDT)
4506 and then Is_Interface (DDT)
4507 then
4508 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4509 Analyze_And_Resolve (A, Etype (F),
4510 Suppress => Access_Check);
4511 end if;
4513 -- Ada 2005, AI-162:If the actual is an allocator, the
4514 -- innermost enclosing statement is the master of the
4515 -- created object. This needs to be done with expansion
4516 -- enabled only, otherwise the transient scope will not
4517 -- be removed in the expansion of the wrapped construct.
4519 if Expander_Active
4520 and then (Needs_Finalization (DDT)
4521 or else Has_Task (DDT))
4522 then
4523 Establish_Transient_Scope
4524 (A, Manage_Sec_Stack => False);
4525 end if;
4526 end;
4528 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4529 Check_Restriction (No_Access_Parameter_Allocators, A);
4530 end if;
4531 end if;
4533 -- (Ada 2005): The call may be to a primitive operation of a
4534 -- tagged synchronized type, declared outside of the type. In
4535 -- this case the controlling actual must be converted to its
4536 -- corresponding record type, which is the formal type. The
4537 -- actual may be a subtype, either because of a constraint or
4538 -- because it is a generic actual, so use base type to locate
4539 -- concurrent type.
4541 F_Typ := Base_Type (Etype (F));
4543 if Is_Tagged_Type (F_Typ)
4544 and then (Is_Concurrent_Type (F_Typ)
4545 or else Is_Concurrent_Record_Type (F_Typ))
4546 then
4547 -- If the actual is overloaded, look for an interpretation
4548 -- that has a synchronized type.
4550 if not Is_Overloaded (A) then
4551 A_Typ := Base_Type (Etype (A));
4553 else
4554 declare
4555 Index : Interp_Index;
4556 It : Interp;
4558 begin
4559 Get_First_Interp (A, Index, It);
4560 while Present (It.Typ) loop
4561 if Is_Concurrent_Type (It.Typ)
4562 or else Is_Concurrent_Record_Type (It.Typ)
4563 then
4564 A_Typ := Base_Type (It.Typ);
4565 exit;
4566 end if;
4568 Get_Next_Interp (Index, It);
4569 end loop;
4570 end;
4571 end if;
4573 declare
4574 Full_A_Typ : Entity_Id;
4576 begin
4577 if Present (Full_View (A_Typ)) then
4578 Full_A_Typ := Base_Type (Full_View (A_Typ));
4579 else
4580 Full_A_Typ := A_Typ;
4581 end if;
4583 -- Tagged synchronized type (case 1): the actual is a
4584 -- concurrent type.
4586 if Is_Concurrent_Type (A_Typ)
4587 and then Corresponding_Record_Type (A_Typ) = F_Typ
4588 then
4589 Rewrite (A,
4590 Unchecked_Convert_To
4591 (Corresponding_Record_Type (A_Typ), A));
4592 Resolve (A, Etype (F));
4594 -- Tagged synchronized type (case 2): the formal is a
4595 -- concurrent type.
4597 elsif Ekind (Full_A_Typ) = E_Record_Type
4598 and then Present
4599 (Corresponding_Concurrent_Type (Full_A_Typ))
4600 and then Is_Concurrent_Type (F_Typ)
4601 and then Present (Corresponding_Record_Type (F_Typ))
4602 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4603 then
4604 Resolve (A, Corresponding_Record_Type (F_Typ));
4606 -- Common case
4608 else
4609 Resolve (A, Etype (F));
4610 end if;
4611 end;
4613 -- Not a synchronized operation
4615 else
4616 Resolve (A, Etype (F));
4617 end if;
4618 end if;
4620 A_Typ := Etype (A);
4621 F_Typ := Etype (F);
4623 -- An actual cannot be an untagged formal incomplete type
4625 if Ekind (A_Typ) = E_Incomplete_Type
4626 and then not Is_Tagged_Type (A_Typ)
4627 and then Is_Generic_Type (A_Typ)
4628 then
4629 Error_Msg_N
4630 ("invalid use of untagged formal incomplete type", A);
4631 end if;
4633 -- For mode IN, if actual is an entity, and the type of the formal
4634 -- has warnings suppressed, then we reset Never_Set_In_Source for
4635 -- the calling entity. The reason for this is to catch cases like
4636 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4637 -- uses trickery to modify an IN parameter.
4639 if Ekind (F) = E_In_Parameter
4640 and then Is_Entity_Name (A)
4641 and then Present (Entity (A))
4642 and then Ekind (Entity (A)) = E_Variable
4643 and then Has_Warnings_Off (F_Typ)
4644 then
4645 Set_Never_Set_In_Source (Entity (A), False);
4646 end if;
4648 -- Perform error checks for IN and IN OUT parameters
4650 if Ekind (F) /= E_Out_Parameter then
4652 -- Check unset reference. For scalar parameters, it is clearly
4653 -- wrong to pass an uninitialized value as either an IN or
4654 -- IN-OUT parameter. For composites, it is also clearly an
4655 -- error to pass a completely uninitialized value as an IN
4656 -- parameter, but the case of IN OUT is trickier. We prefer
4657 -- not to give a warning here. For example, suppose there is
4658 -- a routine that sets some component of a record to False.
4659 -- It is perfectly reasonable to make this IN-OUT and allow
4660 -- either initialized or uninitialized records to be passed
4661 -- in this case.
4663 -- For partially initialized composite values, we also avoid
4664 -- warnings, since it is quite likely that we are passing a
4665 -- partially initialized value and only the initialized fields
4666 -- will in fact be read in the subprogram.
4668 if Is_Scalar_Type (A_Typ)
4669 or else (Ekind (F) = E_In_Parameter
4670 and then not Is_Partially_Initialized_Type (A_Typ))
4671 then
4672 Check_Unset_Reference (A);
4673 end if;
4675 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4676 -- actual to a nested call, since this constitutes a reading of
4677 -- the parameter, which is not allowed.
4679 if Ada_Version = Ada_83
4680 and then Is_Entity_Name (A)
4681 and then Ekind (Entity (A)) = E_Out_Parameter
4682 then
4683 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4684 end if;
4685 end if;
4687 -- In -gnatd.q mode, forget that a given array is constant when
4688 -- it is passed as an IN parameter to a foreign-convention
4689 -- subprogram. This is in case the subprogram evilly modifies the
4690 -- object. Of course, correct code would use IN OUT.
4692 if Debug_Flag_Dot_Q
4693 and then Ekind (F) = E_In_Parameter
4694 and then Has_Foreign_Convention (Nam)
4695 and then Is_Array_Type (F_Typ)
4696 and then Nkind (A) in N_Has_Entity
4697 and then Present (Entity (A))
4698 then
4699 Set_Is_True_Constant (Entity (A), False);
4700 end if;
4702 -- Case of OUT or IN OUT parameter
4704 if Ekind (F) /= E_In_Parameter then
4706 -- For an Out parameter, check for useless assignment. Note
4707 -- that we can't set Last_Assignment this early, because we may
4708 -- kill current values in Resolve_Call, and that call would
4709 -- clobber the Last_Assignment field.
4711 -- Note: call Warn_On_Useless_Assignment before doing the check
4712 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4713 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4714 -- reflects the last assignment, not this one.
4716 if Ekind (F) = E_Out_Parameter then
4717 if Warn_On_Modified_As_Out_Parameter (F)
4718 and then Is_Entity_Name (A)
4719 and then Present (Entity (A))
4720 and then Comes_From_Source (N)
4721 then
4722 Warn_On_Useless_Assignment (Entity (A), A);
4723 end if;
4724 end if;
4726 -- Validate the form of the actual. Note that the call to
4727 -- Is_OK_Variable_For_Out_Formal generates the required
4728 -- reference in this case.
4730 -- A call to an initialization procedure for an aggregate
4731 -- component may initialize a nested component of a constant
4732 -- designated object. In this context the object is variable.
4734 if not Is_OK_Variable_For_Out_Formal (A)
4735 and then not Is_Init_Proc (Nam)
4736 then
4737 Error_Msg_NE ("actual for& must be a variable", A, F);
4739 if Is_Subprogram (Current_Scope) then
4740 if Is_Invariant_Procedure (Current_Scope)
4741 or else Is_Partial_Invariant_Procedure (Current_Scope)
4742 then
4743 Error_Msg_N
4744 ("function used in invariant cannot modify its "
4745 & "argument", F);
4747 elsif Is_Predicate_Function (Current_Scope) then
4748 Error_Msg_N
4749 ("function used in predicate cannot modify its "
4750 & "argument", F);
4751 end if;
4752 end if;
4753 end if;
4755 -- What's the following about???
4757 if Is_Entity_Name (A) then
4758 Kill_Checks (Entity (A));
4759 else
4760 Kill_All_Checks;
4761 end if;
4762 end if;
4764 if A_Typ = Any_Type then
4765 Set_Etype (N, Any_Type);
4766 return;
4767 end if;
4769 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4771 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4773 -- Apply predicate tests except in certain special cases. Note
4774 -- that it might be more consistent to apply these only when
4775 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4776 -- for the outbound predicate tests ??? In any case indicate
4777 -- the function being called, for better warnings if the call
4778 -- leads to an infinite recursion.
4780 if Predicate_Tests_On_Arguments (Nam) then
4781 Apply_Predicate_Check (A, F_Typ, Nam);
4782 end if;
4784 -- Apply required constraint checks
4786 if Is_Scalar_Type (A_Typ) then
4787 Apply_Scalar_Range_Check (A, F_Typ);
4789 elsif Is_Array_Type (A_Typ) then
4790 Apply_Length_Check (A, F_Typ);
4792 elsif Is_Record_Type (F_Typ)
4793 and then Has_Discriminants (F_Typ)
4794 and then Is_Constrained (F_Typ)
4795 and then (not Is_Derived_Type (F_Typ)
4796 or else Comes_From_Source (Nam))
4797 then
4798 Apply_Discriminant_Check (A, F_Typ);
4800 -- For view conversions of a discriminated object, apply
4801 -- check to object itself, the conversion alreay has the
4802 -- proper type.
4804 if Nkind (A) = N_Type_Conversion
4805 and then Is_Constrained (Etype (Expression (A)))
4806 then
4807 Apply_Discriminant_Check (Expression (A), F_Typ);
4808 end if;
4810 elsif Is_Access_Type (F_Typ)
4811 and then Is_Array_Type (Designated_Type (F_Typ))
4812 and then Is_Constrained (Designated_Type (F_Typ))
4813 then
4814 Apply_Length_Check (A, F_Typ);
4816 elsif Is_Access_Type (F_Typ)
4817 and then Has_Discriminants (Designated_Type (F_Typ))
4818 and then Is_Constrained (Designated_Type (F_Typ))
4819 then
4820 Apply_Discriminant_Check (A, F_Typ);
4822 else
4823 Apply_Range_Check (A, F_Typ);
4824 end if;
4826 -- Ada 2005 (AI-231): Note that the controlling parameter case
4827 -- already existed in Ada 95, which is partially checked
4828 -- elsewhere (see Checks), and we don't want the warning
4829 -- message to differ.
4831 if Is_Access_Type (F_Typ)
4832 and then Can_Never_Be_Null (F_Typ)
4833 and then Known_Null (A)
4834 then
4835 if Is_Controlling_Formal (F) then
4836 Apply_Compile_Time_Constraint_Error
4837 (N => A,
4838 Msg => "null value not allowed here??",
4839 Reason => CE_Access_Check_Failed);
4841 elsif Ada_Version >= Ada_2005 then
4842 Apply_Compile_Time_Constraint_Error
4843 (N => A,
4844 Msg => "(Ada 2005) NULL not allowed in "
4845 & "null-excluding formal??",
4846 Reason => CE_Null_Not_Allowed);
4847 end if;
4848 end if;
4849 end if;
4851 -- Checks for OUT parameters and IN OUT parameters
4853 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4855 -- If there is a type conversion, make sure the return value
4856 -- meets the constraints of the variable before the conversion.
4858 if Nkind (A) = N_Type_Conversion then
4859 if Is_Scalar_Type (A_Typ) then
4861 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4862 -- which would prevent the check from being generated.
4863 -- This is for Starlet only though, so long obsolete.
4865 if Mechanism (F) = By_Reference
4866 and then Ekind (Nam) = E_Procedure
4867 and then Is_Valued_Procedure (Nam)
4868 then
4869 null;
4870 else
4871 Apply_Scalar_Range_Check
4872 (Expression (A), Etype (Expression (A)), A_Typ);
4873 end if;
4875 -- In addition the return value must meet the constraints
4876 -- of the object type (see the comment below).
4878 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4880 else
4881 Apply_Range_Check
4882 (Expression (A), Etype (Expression (A)), A_Typ);
4883 end if;
4885 -- If no conversion, apply scalar range checks and length check
4886 -- based on the subtype of the actual (NOT that of the formal).
4887 -- This indicates that the check takes place on return from the
4888 -- call. During expansion the required constraint checks are
4889 -- inserted. In GNATprove mode, in the absence of expansion,
4890 -- the flag indicates that the returned value is valid.
4892 else
4893 if Is_Scalar_Type (F_Typ) then
4894 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4896 elsif Is_Array_Type (F_Typ)
4897 and then Ekind (F) = E_Out_Parameter
4898 then
4899 Apply_Length_Check (A, F_Typ);
4901 else
4902 Apply_Range_Check (A, A_Typ, F_Typ);
4903 end if;
4904 end if;
4906 -- Note: we do not apply the predicate checks for the case of
4907 -- OUT and IN OUT parameters. They are instead applied in the
4908 -- Expand_Actuals routine in Exp_Ch6.
4909 end if;
4911 -- If the formal is of an unconstrained array subtype with fixed
4912 -- lower bound, then sliding to that bound may be needed.
4914 if Is_Fixed_Lower_Bound_Array_Subtype (F_Typ) then
4915 Expand_Sliding_Conversion (A, F_Typ);
4916 end if;
4918 -- An actual associated with an access parameter is implicitly
4919 -- converted to the anonymous access type of the formal and must
4920 -- satisfy the legality checks for access conversions.
4922 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4923 if not Valid_Conversion (A, F_Typ, A) then
4924 Error_Msg_N
4925 ("invalid implicit conversion for access parameter", A);
4926 end if;
4928 -- If the actual is an access selected component of a variable,
4929 -- the call may modify its designated object. It is reasonable
4930 -- to treat this as a potential modification of the enclosing
4931 -- record, to prevent spurious warnings that it should be
4932 -- declared as a constant, because intuitively programmers
4933 -- regard the designated subcomponent as part of the record.
4935 if Nkind (A) = N_Selected_Component
4936 and then Is_Entity_Name (Prefix (A))
4937 and then not Is_Constant_Object (Entity (Prefix (A)))
4938 then
4939 Note_Possible_Modification (A, Sure => False);
4940 end if;
4941 end if;
4943 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
4945 if (Is_By_Reference_Type (F_Typ) or else Is_Aliased (F))
4946 and then Comes_From_Source (N)
4947 then
4948 if Is_Atomic_Object (A)
4949 and then not Is_Atomic (F_Typ)
4950 then
4951 Error_Msg_NE
4952 ("cannot pass atomic object to nonatomic formal&",
4953 A, F);
4954 Error_Msg_N
4955 ("\which is passed by reference (RM C.6(12))", A);
4957 elsif Is_Volatile_Object_Ref (A)
4958 and then not Is_Volatile (F_Typ)
4959 then
4960 Error_Msg_NE
4961 ("cannot pass volatile object to nonvolatile formal&",
4962 A, F);
4963 Error_Msg_N
4964 ("\which is passed by reference (RM C.6(12))", A);
4966 elsif Is_Volatile_Full_Access_Object_Ref (A)
4967 and then not Is_Volatile_Full_Access (F_Typ)
4968 then
4969 Error_Msg_NE
4970 ("cannot pass full access object to nonfull access "
4971 & "formal&", A, F);
4972 Error_Msg_N
4973 ("\which is passed by reference (RM C.6(12))", A);
4974 end if;
4976 -- Check for nonatomic subcomponent of a full access object
4977 -- in Ada 2022 (RM C.6 (12)).
4979 if Ada_Version >= Ada_2022
4980 and then Is_Subcomponent_Of_Full_Access_Object (A)
4981 and then not Is_Atomic_Object (A)
4982 then
4983 Error_Msg_N
4984 ("cannot pass nonatomic subcomponent of full access "
4985 & "object", A);
4986 Error_Msg_NE
4987 ("\to formal & which is passed by reference (RM C.6(12))",
4988 A, F);
4989 end if;
4990 end if;
4992 -- Check that subprograms don't have improper controlling
4993 -- arguments (RM 3.9.2 (9)).
4995 -- A primitive operation may have an access parameter of an
4996 -- incomplete tagged type, but a dispatching call is illegal
4997 -- if the type is still incomplete.
4999 if Is_Controlling_Formal (F) then
5000 Set_Is_Controlling_Actual (A);
5002 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5003 declare
5004 Desig : constant Entity_Id := Designated_Type (F_Typ);
5005 begin
5006 if Ekind (Desig) = E_Incomplete_Type
5007 and then No (Full_View (Desig))
5008 and then No (Non_Limited_View (Desig))
5009 then
5010 Error_Msg_NE
5011 ("premature use of incomplete type& "
5012 & "in dispatching call", A, Desig);
5013 end if;
5014 end;
5015 end if;
5017 elsif Nkind (A) = N_Explicit_Dereference then
5018 Validate_Remote_Access_To_Class_Wide_Type (A);
5019 end if;
5021 -- Apply legality rule 3.9.2 (9/1)
5023 -- Skip this check on helpers and indirect-call wrappers built to
5024 -- support class-wide preconditions.
5026 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
5027 and then not Is_Class_Wide_Type (F_Typ)
5028 and then not Is_Controlling_Formal (F)
5029 and then not In_Instance
5030 and then (not Is_Subprogram (Nam)
5031 or else No (Class_Preconditions_Subprogram (Nam)))
5032 then
5033 Error_Msg_N ("class-wide argument not allowed here!", A);
5035 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5036 Error_Msg_Node_2 := F_Typ;
5037 Error_Msg_NE
5038 ("& is not a dispatching operation of &!", A, Nam);
5039 end if;
5041 -- Apply the checks described in 3.10.2(27): if the context is a
5042 -- specific access-to-object, the actual cannot be class-wide.
5043 -- Use base type to exclude access_to_subprogram cases.
5045 elsif Is_Access_Type (A_Typ)
5046 and then Is_Access_Type (F_Typ)
5047 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
5048 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
5049 or else (Nkind (A) = N_Attribute_Reference
5050 and then
5051 Is_Class_Wide_Type (Etype (Prefix (A)))))
5052 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
5053 and then not Is_Controlling_Formal (F)
5055 -- Disable these checks for call to imported C++ subprograms
5057 and then not
5058 (Is_Entity_Name (Name (N))
5059 and then Is_Imported (Entity (Name (N)))
5060 and then Convention (Entity (Name (N))) = Convention_CPP)
5061 then
5062 Error_Msg_N
5063 ("access to class-wide argument not allowed here!", A);
5065 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5066 Error_Msg_Node_2 := Designated_Type (F_Typ);
5067 Error_Msg_NE
5068 ("& is not a dispatching operation of &!", A, Nam);
5069 end if;
5070 end if;
5072 Check_Aliased_Parameter;
5074 Eval_Actual (A);
5076 -- If it is a named association, treat the selector_name as a
5077 -- proper identifier, and mark the corresponding entity.
5079 if Nkind (Parent (A)) = N_Parameter_Association
5081 -- Ignore reference in SPARK mode, as it refers to an entity not
5082 -- in scope at the point of reference, so the reference should
5083 -- be ignored for computing effects of subprograms.
5085 and then not GNATprove_Mode
5086 then
5087 -- If subprogram is overridden, use name of formal that
5088 -- is being called.
5090 if Present (Real_Subp) then
5091 Set_Entity (Selector_Name (Parent (A)), Real_F);
5092 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
5094 else
5095 Set_Entity (Selector_Name (Parent (A)), F);
5096 Generate_Reference (F, Selector_Name (Parent (A)));
5097 Set_Etype (Selector_Name (Parent (A)), F_Typ);
5098 Generate_Reference (F_Typ, N, ' ');
5099 end if;
5100 end if;
5102 Prev := A;
5104 if Ekind (F) /= E_Out_Parameter then
5105 Check_Unset_Reference (A);
5106 end if;
5108 -- The following checks are only relevant when SPARK_Mode is on as
5109 -- they are not standard Ada legality rule. Internally generated
5110 -- temporaries are ignored.
5112 if SPARK_Mode = On and then Comes_From_Source (A) then
5114 -- Inspect the expression and flag each effectively volatile
5115 -- object for reading as illegal because it appears within
5116 -- an interfering context. Note that this is usually done
5117 -- in Resolve_Entity_Name, but when the effectively volatile
5118 -- object for reading appears as an actual in a call, the call
5119 -- must be resolved first.
5121 Flag_Effectively_Volatile_Objects (A);
5122 end if;
5124 -- A formal parameter of a specific tagged type whose related
5125 -- subprogram is subject to pragma Extensions_Visible with value
5126 -- "False" cannot act as an actual in a subprogram with value
5127 -- "True" (SPARK RM 6.1.7(3)).
5129 -- No check needed for helpers and indirect-call wrappers built to
5130 -- support class-wide preconditions.
5132 if Is_EVF_Expression (A)
5133 and then Extensions_Visible_Status (Nam) =
5134 Extensions_Visible_True
5135 and then No (Class_Preconditions_Subprogram (Current_Scope))
5136 then
5137 Error_Msg_N
5138 ("formal parameter cannot act as actual parameter when "
5139 & "Extensions_Visible is False", A);
5140 Error_Msg_NE
5141 ("\subprogram & has Extensions_Visible True", A, Nam);
5142 end if;
5144 -- The actual parameter of a Ghost subprogram whose formal is of
5145 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5147 if Comes_From_Source (Nam)
5148 and then Is_Ghost_Entity (Nam)
5149 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5150 and then Is_Entity_Name (A)
5151 and then Present (Entity (A))
5152 and then not Is_Ghost_Entity (Entity (A))
5153 then
5154 Error_Msg_NE
5155 ("non-ghost variable & cannot appear as actual in call to "
5156 & "ghost procedure", A, Entity (A));
5158 if Ekind (F) = E_In_Out_Parameter then
5159 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5160 else
5161 Error_Msg_N ("\corresponding formal has mode OUT", A);
5162 end if;
5163 end if;
5165 -- (AI12-0397): The target of a subprogram call that occurs within
5166 -- the expression of an Default_Initial_Condition aspect and has
5167 -- an actual that is the current instance of the type must be
5168 -- either a primitive of the type or a class-wide subprogram,
5169 -- because the type of the current instance in such an aspect is
5170 -- considered to be a notional formal derived type whose only
5171 -- operations correspond to the primitives of the enclosing type.
5172 -- Nonprimitives can be called, but the current instance must be
5173 -- converted rather than passed directly. Note that a current
5174 -- instance of a type with DIC will occur as a reference to an
5175 -- in-mode formal of an enclosing DIC procedure or partial DIC
5176 -- procedure. (It seems that this check should perhaps also apply
5177 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5178 -- aspects???)
5180 if Nkind (A) = N_Identifier
5181 and then Ekind (Entity (A)) = E_In_Parameter
5183 and then Is_Subprogram (Scope (Entity (A)))
5184 and then Is_DIC_Procedure (Scope (Entity (A)))
5186 -- We check Comes_From_Source to exclude inherited primitives
5187 -- from being flagged, because such subprograms turn out to not
5188 -- always have the Is_Primitive flag set. ???
5190 and then Comes_From_Source (Nam)
5192 and then not Is_Primitive (Nam)
5193 and then not Is_Class_Wide_Type (F_Typ)
5194 then
5195 Error_Msg_NE
5196 ("call to nonprimitive & with current instance not allowed " &
5197 "for aspect", A, Nam);
5198 end if;
5200 Next_Actual (A);
5202 -- Case where actual is not present
5204 else
5205 Insert_Default;
5206 end if;
5208 Next_Formal (F);
5210 if Present (Real_Subp) then
5211 Next_Formal (Real_F);
5212 end if;
5213 end loop;
5214 end Resolve_Actuals;
5216 -----------------------
5217 -- Resolve_Allocator --
5218 -----------------------
5220 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5221 Desig_T : constant Entity_Id := Designated_Type (Typ);
5222 E : constant Node_Id := Expression (N);
5223 Subtyp : Entity_Id;
5224 Discrim : Entity_Id;
5225 Constr : Node_Id;
5226 Aggr : Node_Id;
5227 Assoc : Node_Id := Empty;
5228 Disc_Exp : Node_Id;
5230 procedure Check_Allocator_Discrim_Accessibility
5231 (Disc_Exp : Node_Id;
5232 Alloc_Typ : Entity_Id);
5233 -- Check that accessibility level associated with an access discriminant
5234 -- initialized in an allocator by the expression Disc_Exp is not deeper
5235 -- than the level of the allocator type Alloc_Typ. An error message is
5236 -- issued if this condition is violated. Specialized checks are done for
5237 -- the cases of a constraint expression which is an access attribute or
5238 -- an access discriminant.
5240 procedure Check_Allocator_Discrim_Accessibility_Exprs
5241 (Curr_Exp : Node_Id;
5242 Alloc_Typ : Entity_Id);
5243 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5244 -- across all expressions within a given conditional expression.
5246 function In_Dispatching_Context return Boolean;
5247 -- If the allocator is an actual in a call, it is allowed to be class-
5248 -- wide when the context is not because it is a controlling actual.
5250 -------------------------------------------
5251 -- Check_Allocator_Discrim_Accessibility --
5252 -------------------------------------------
5254 procedure Check_Allocator_Discrim_Accessibility
5255 (Disc_Exp : Node_Id;
5256 Alloc_Typ : Entity_Id)
5258 begin
5259 if Type_Access_Level (Etype (Disc_Exp)) >
5260 Deepest_Type_Access_Level (Alloc_Typ)
5261 then
5262 Error_Msg_N
5263 ("operand type has deeper level than allocator type", Disc_Exp);
5265 -- When the expression is an Access attribute the level of the prefix
5266 -- object must not be deeper than that of the allocator's type.
5268 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5269 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5270 Attribute_Access
5271 and then Static_Accessibility_Level
5272 (Disc_Exp, Zero_On_Dynamic_Level)
5273 > Deepest_Type_Access_Level (Alloc_Typ)
5274 then
5275 Error_Msg_N
5276 ("prefix of attribute has deeper level than allocator type",
5277 Disc_Exp);
5279 -- When the expression is an access discriminant the check is against
5280 -- the level of the prefix object.
5282 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5283 and then Nkind (Disc_Exp) = N_Selected_Component
5284 and then Static_Accessibility_Level
5285 (Disc_Exp, Zero_On_Dynamic_Level)
5286 > Deepest_Type_Access_Level (Alloc_Typ)
5287 then
5288 Error_Msg_N
5289 ("access discriminant has deeper level than allocator type",
5290 Disc_Exp);
5292 -- All other cases are legal
5294 else
5295 null;
5296 end if;
5297 end Check_Allocator_Discrim_Accessibility;
5299 -------------------------------------------------
5300 -- Check_Allocator_Discrim_Accessibility_Exprs --
5301 -------------------------------------------------
5303 procedure Check_Allocator_Discrim_Accessibility_Exprs
5304 (Curr_Exp : Node_Id;
5305 Alloc_Typ : Entity_Id)
5307 Alt : Node_Id;
5308 Expr : Node_Id;
5309 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5310 begin
5311 -- When conditional expressions are constant folded we know at
5312 -- compile time which expression to check - so don't bother with
5313 -- the rest of the cases.
5315 if Nkind (Curr_Exp) = N_Attribute_Reference then
5316 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5318 -- Non-constant-folded if expressions
5320 elsif Nkind (Disc_Exp) = N_If_Expression then
5321 -- Check both expressions if they are still present in the face
5322 -- of expansion.
5324 Expr := Next (First (Expressions (Disc_Exp)));
5325 if Present (Expr) then
5326 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5327 Next (Expr);
5328 if Present (Expr) then
5329 Check_Allocator_Discrim_Accessibility_Exprs
5330 (Expr, Alloc_Typ);
5331 end if;
5332 end if;
5334 -- Non-constant-folded case expressions
5336 elsif Nkind (Disc_Exp) = N_Case_Expression then
5337 -- Check all alternatives
5339 Alt := First (Alternatives (Disc_Exp));
5340 while Present (Alt) loop
5341 Check_Allocator_Discrim_Accessibility_Exprs
5342 (Expression (Alt), Alloc_Typ);
5344 Next (Alt);
5345 end loop;
5347 -- Base case, check the accessibility of the original node of the
5348 -- expression.
5350 else
5351 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5352 end if;
5353 end Check_Allocator_Discrim_Accessibility_Exprs;
5355 ----------------------------
5356 -- In_Dispatching_Context --
5357 ----------------------------
5359 function In_Dispatching_Context return Boolean is
5360 Par : constant Node_Id := Parent (N);
5362 begin
5363 return Nkind (Par) in N_Subprogram_Call
5364 and then Is_Entity_Name (Name (Par))
5365 and then Is_Dispatching_Operation (Entity (Name (Par)));
5366 end In_Dispatching_Context;
5368 -- Start of processing for Resolve_Allocator
5370 begin
5371 -- Replace general access with specific type
5373 if Ekind (Etype (N)) = E_Allocator_Type then
5374 Set_Etype (N, Base_Type (Typ));
5375 end if;
5377 if Is_Abstract_Type (Typ) then
5378 Error_Msg_N ("type of allocator cannot be abstract", N);
5379 end if;
5381 -- For qualified expression, resolve the expression using the given
5382 -- subtype (nothing to do for type mark, subtype indication)
5384 if Nkind (E) = N_Qualified_Expression then
5385 if Is_Class_Wide_Type (Etype (E))
5386 and then not Is_Class_Wide_Type (Desig_T)
5387 and then not In_Dispatching_Context
5388 then
5389 Error_Msg_N
5390 ("class-wide allocator not allowed for this access type", N);
5391 end if;
5393 -- Do a full resolution to apply constraint and predicate checks
5395 Resolve_Qualified_Expression (E, Etype (E));
5396 Check_Unset_Reference (Expression (E));
5398 -- Allocators generated by the build-in-place expansion mechanism
5399 -- are explicitly marked as coming from source but do not need to be
5400 -- checked for limited initialization. To exclude this case, ensure
5401 -- that the parent of the allocator is a source node.
5402 -- The return statement constructed for an Expression_Function does
5403 -- not come from source but requires a limited check.
5405 if Is_Limited_Type (Etype (E))
5406 and then Comes_From_Source (N)
5407 and then
5408 (Comes_From_Source (Parent (N))
5409 or else
5410 (Ekind (Current_Scope) = E_Function
5411 and then Nkind (Original_Node (Unit_Declaration_Node
5412 (Current_Scope))) = N_Expression_Function))
5413 and then not In_Instance_Body
5414 then
5415 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5416 if Nkind (Parent (N)) = N_Assignment_Statement then
5417 Error_Msg_N
5418 ("illegal expression for initialized allocator of a "
5419 & "limited type (RM 7.5 (2.7/2))", N);
5420 else
5421 Error_Msg_N
5422 ("initialization not allowed for limited types", N);
5423 end if;
5425 Explain_Limited_Type (Etype (E), N);
5426 end if;
5427 end if;
5429 -- Calls to build-in-place functions are not currently supported in
5430 -- allocators for access types associated with a simple storage pool.
5431 -- Supporting such allocators may require passing additional implicit
5432 -- parameters to build-in-place functions (or a significant revision
5433 -- of the current b-i-p implementation to unify the handling for
5434 -- multiple kinds of storage pools). ???
5436 if Is_Limited_View (Desig_T)
5437 and then Nkind (Expression (E)) = N_Function_Call
5438 then
5439 declare
5440 Pool : constant Entity_Id :=
5441 Associated_Storage_Pool (Root_Type (Typ));
5442 begin
5443 if Present (Pool)
5444 and then
5445 Present (Get_Rep_Pragma
5446 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5447 then
5448 Error_Msg_N
5449 ("limited function calls not yet supported in simple "
5450 & "storage pool allocators", Expression (E));
5451 end if;
5452 end;
5453 end if;
5455 -- A special accessibility check is needed for allocators that
5456 -- constrain access discriminants. The level of the type of the
5457 -- expression used to constrain an access discriminant cannot be
5458 -- deeper than the type of the allocator (in contrast to access
5459 -- parameters, where the level of the actual can be arbitrary).
5461 -- We can't use Valid_Conversion to perform this check because in
5462 -- general the type of the allocator is unrelated to the type of
5463 -- the access discriminant.
5465 if Ekind (Typ) /= E_Anonymous_Access_Type
5466 or else Is_Local_Anonymous_Access (Typ)
5467 then
5468 Subtyp := Entity (Subtype_Mark (E));
5470 Aggr := Original_Node (Expression (E));
5472 if Has_Discriminants (Subtyp)
5473 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5474 then
5475 Discrim := First_Discriminant (Base_Type (Subtyp));
5477 -- Get the first component expression of the aggregate
5479 if Present (Expressions (Aggr)) then
5480 Disc_Exp := First (Expressions (Aggr));
5482 elsif Present (Component_Associations (Aggr)) then
5483 Assoc := First (Component_Associations (Aggr));
5485 if Present (Assoc) then
5486 Disc_Exp := Expression (Assoc);
5487 else
5488 Disc_Exp := Empty;
5489 end if;
5491 else
5492 Disc_Exp := Empty;
5493 end if;
5495 while Present (Discrim) and then Present (Disc_Exp) loop
5496 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5497 Check_Allocator_Discrim_Accessibility_Exprs
5498 (Disc_Exp, Typ);
5499 end if;
5501 Next_Discriminant (Discrim);
5503 if Present (Discrim) then
5504 if Present (Assoc) then
5505 Next (Assoc);
5506 Disc_Exp := Expression (Assoc);
5508 elsif Present (Next (Disc_Exp)) then
5509 Next (Disc_Exp);
5511 else
5512 Assoc := First (Component_Associations (Aggr));
5514 if Present (Assoc) then
5515 Disc_Exp := Expression (Assoc);
5516 else
5517 Disc_Exp := Empty;
5518 end if;
5519 end if;
5520 end if;
5521 end loop;
5522 end if;
5523 end if;
5525 -- For a subtype mark or subtype indication, freeze the subtype
5527 else
5528 Freeze_Expression (E);
5530 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5531 Error_Msg_N
5532 ("initialization required for access-to-constant allocator", N);
5533 end if;
5535 -- A special accessibility check is needed for allocators that
5536 -- constrain access discriminants. The level of the type of the
5537 -- expression used to constrain an access discriminant cannot be
5538 -- deeper than the type of the allocator (in contrast to access
5539 -- parameters, where the level of the actual can be arbitrary).
5540 -- We can't use Valid_Conversion to perform this check because
5541 -- in general the type of the allocator is unrelated to the type
5542 -- of the access discriminant.
5544 if Nkind (Original_Node (E)) = N_Subtype_Indication
5545 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5546 or else Is_Local_Anonymous_Access (Typ))
5547 then
5548 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5550 if Has_Discriminants (Subtyp) then
5551 Discrim := First_Discriminant (Base_Type (Subtyp));
5552 Constr := First (Constraints (Constraint (Original_Node (E))));
5553 while Present (Discrim) and then Present (Constr) loop
5554 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5555 if Nkind (Constr) = N_Discriminant_Association then
5556 Disc_Exp := Expression (Constr);
5557 else
5558 Disc_Exp := Constr;
5559 end if;
5561 Check_Allocator_Discrim_Accessibility_Exprs
5562 (Disc_Exp, Typ);
5563 end if;
5565 Next_Discriminant (Discrim);
5566 Next (Constr);
5567 end loop;
5568 end if;
5569 end if;
5570 end if;
5572 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5573 -- check that the level of the type of the created object is not deeper
5574 -- than the level of the allocator's access type, since extensions can
5575 -- now occur at deeper levels than their ancestor types. This is a
5576 -- static accessibility level check; a run-time check is also needed in
5577 -- the case of an initialized allocator with a class-wide argument (see
5578 -- Expand_Allocator_Expression).
5580 if Ada_Version >= Ada_2005
5581 and then Is_Class_Wide_Type (Desig_T)
5582 then
5583 declare
5584 Exp_Typ : Entity_Id;
5586 begin
5587 if Nkind (E) = N_Qualified_Expression then
5588 Exp_Typ := Etype (E);
5589 elsif Nkind (E) = N_Subtype_Indication then
5590 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5591 else
5592 Exp_Typ := Entity (E);
5593 end if;
5595 if Type_Access_Level (Exp_Typ) >
5596 Deepest_Type_Access_Level (Typ)
5597 then
5598 if In_Instance_Body then
5599 Error_Msg_Warn := SPARK_Mode /= On;
5600 Error_Msg_N
5601 ("type in allocator has deeper level than designated "
5602 & "class-wide type<<", E);
5603 Error_Msg_N ("\Program_Error [<<", E);
5605 Rewrite (N,
5606 Make_Raise_Program_Error (Sloc (N),
5607 Reason => PE_Accessibility_Check_Failed));
5608 Set_Etype (N, Typ);
5610 -- Do not apply Ada 2005 accessibility checks on a class-wide
5611 -- allocator if the type given in the allocator is a formal
5612 -- type or within a formal package. A run-time check will be
5613 -- performed in the instance.
5615 elsif not Is_Generic_Type (Exp_Typ)
5616 and then not In_Generic_Formal_Package (Exp_Typ)
5617 then
5618 Error_Msg_N
5619 ("type in allocator has deeper level than designated "
5620 & "class-wide type", E);
5621 end if;
5622 end if;
5623 end;
5624 end if;
5626 -- Check for allocation from an empty storage pool. But do not complain
5627 -- if it's a return statement for a build-in-place function, because the
5628 -- allocator is there just in case the caller uses an allocator. If the
5629 -- caller does use an allocator, it will be caught at the call site.
5631 if No_Pool_Assigned (Typ)
5632 and then not For_Special_Return_Object (N)
5633 then
5634 Error_Msg_N ("allocation from empty storage pool!", N);
5636 -- If the context is an unchecked conversion, as may happen within an
5637 -- inlined subprogram, the allocator is being resolved with its own
5638 -- anonymous type. In that case, if the target type has a specific
5639 -- storage pool, it must be inherited explicitly by the allocator type.
5641 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5642 and then No (Associated_Storage_Pool (Typ))
5643 then
5644 Set_Associated_Storage_Pool
5645 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5646 end if;
5648 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5649 Check_Restriction (No_Anonymous_Allocators, N);
5650 end if;
5652 -- Check that an allocator with task parts isn't for a nested access
5653 -- type when restriction No_Task_Hierarchy applies.
5655 if not Is_Library_Level_Entity (Base_Type (Typ))
5656 and then Has_Task (Base_Type (Desig_T))
5657 then
5658 Check_Restriction (No_Task_Hierarchy, N);
5659 end if;
5661 -- An illegal allocator may be rewritten as a raise Program_Error
5662 -- statement.
5664 if Nkind (N) = N_Allocator then
5666 -- Avoid coextension processing for an allocator that is the
5667 -- expansion of a build-in-place function call.
5669 if Nkind (Original_Node (N)) = N_Allocator
5670 and then Nkind (Expression (Original_Node (N))) =
5671 N_Qualified_Expression
5672 and then Nkind (Expression (Expression (Original_Node (N)))) =
5673 N_Function_Call
5674 and then Is_Expanded_Build_In_Place_Call
5675 (Expression (Expression (Original_Node (N))))
5676 then
5677 null; -- b-i-p function call case
5679 else
5680 -- An anonymous access discriminant is the definition of a
5681 -- coextension.
5683 if Ekind (Typ) = E_Anonymous_Access_Type
5684 and then Nkind (Associated_Node_For_Itype (Typ)) =
5685 N_Discriminant_Specification
5686 then
5687 declare
5688 Discr : constant Entity_Id :=
5689 Defining_Identifier (Associated_Node_For_Itype (Typ));
5691 begin
5692 Check_Restriction (No_Coextensions, N);
5694 -- Ada 2012 AI05-0052: If the designated type of the
5695 -- allocator is limited, then the allocator shall not
5696 -- be used to define the value of an access discriminant
5697 -- unless the discriminated type is immutably limited.
5699 if Ada_Version >= Ada_2012
5700 and then Is_Limited_Type (Desig_T)
5701 and then not Is_Limited_View (Scope (Discr))
5702 then
5703 Error_Msg_N
5704 ("only immutably limited types can have anonymous "
5705 & "access discriminants designating a limited type",
5707 end if;
5708 end;
5710 -- Avoid marking an allocator as a dynamic coextension if it is
5711 -- within a static construct.
5713 if not Is_Static_Coextension (N) then
5714 Set_Is_Dynamic_Coextension (N);
5716 -- Finalization and deallocation of coextensions utilizes an
5717 -- approximate implementation which does not directly adhere
5718 -- to the semantic rules. Warn on potential issues involving
5719 -- coextensions.
5721 if Is_Controlled (Desig_T) then
5722 Error_Msg_N
5723 ("??coextension will not be finalized when its "
5724 & "associated owner is deallocated or finalized", N);
5725 else
5726 Error_Msg_N
5727 ("??coextension will not be deallocated when its "
5728 & "associated owner is deallocated", N);
5729 end if;
5730 end if;
5732 -- Cleanup for potential static coextensions
5734 else
5735 Set_Is_Dynamic_Coextension (N, False);
5736 Set_Is_Static_Coextension (N, False);
5738 -- Anonymous access-to-controlled objects are not finalized on
5739 -- time because this involves run-time ownership and currently
5740 -- this property is not available. In rare cases the object may
5741 -- not be finalized at all. Warn on potential issues involving
5742 -- anonymous access-to-controlled objects.
5744 if Ekind (Typ) = E_Anonymous_Access_Type
5745 and then Is_Controlled_Active (Desig_T)
5746 then
5747 Error_Msg_N
5748 ("??object designated by anonymous access object might "
5749 & "not be finalized until its enclosing library unit "
5750 & "goes out of scope", N);
5751 Error_Msg_N ("\use named access type instead", N);
5752 end if;
5753 end if;
5754 end if;
5755 end if;
5757 -- Report a simple error: if the designated object is a local task,
5758 -- its body has not been seen yet, and its activation will fail an
5759 -- elaboration check.
5761 if Is_Task_Type (Desig_T)
5762 and then Scope (Base_Type (Desig_T)) = Current_Scope
5763 and then Is_Compilation_Unit (Current_Scope)
5764 and then Ekind (Current_Scope) = E_Package
5765 and then not In_Package_Body (Current_Scope)
5766 then
5767 Error_Msg_Warn := SPARK_Mode /= On;
5768 Error_Msg_N ("cannot activate task before body seen<<", N);
5769 Error_Msg_N ("\Program_Error [<<", N);
5770 end if;
5772 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5773 -- type with a task component on a subpool. This action must raise
5774 -- Program_Error at runtime.
5776 if Ada_Version >= Ada_2012
5777 and then Nkind (N) = N_Allocator
5778 and then Present (Subpool_Handle_Name (N))
5779 and then Has_Task (Desig_T)
5780 then
5781 Error_Msg_Warn := SPARK_Mode /= On;
5782 Error_Msg_N ("cannot allocate task on subpool<<", N);
5783 Error_Msg_N ("\Program_Error [<<", N);
5785 Rewrite (N,
5786 Make_Raise_Program_Error (Sloc (N),
5787 Reason => PE_Explicit_Raise));
5788 Set_Etype (N, Typ);
5789 end if;
5790 end Resolve_Allocator;
5792 ---------------------------
5793 -- Resolve_Arithmetic_Op --
5794 ---------------------------
5796 -- Used for resolving all arithmetic operators except exponentiation
5798 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5799 L : constant Node_Id := Left_Opnd (N);
5800 R : constant Node_Id := Right_Opnd (N);
5801 TL : constant Entity_Id := Base_Type (Etype (L));
5802 TR : constant Entity_Id := Base_Type (Etype (R));
5803 T : Entity_Id;
5804 Rop : Node_Id;
5806 B_Typ : constant Entity_Id := Base_Type (Typ);
5807 -- We do the resolution using the base type, because intermediate values
5808 -- in expressions always are of the base type, not a subtype of it.
5810 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5811 -- Returns True if N is in a context that expects "any real type"
5813 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5814 -- Return True iff given type is Integer or universal real/integer
5816 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5817 -- Choose type of integer literal in fixed-point operation to conform
5818 -- to available fixed-point type. T is the type of the other operand,
5819 -- which is needed to determine the expected type of N.
5821 procedure Set_Operand_Type (N : Node_Id);
5822 -- Set operand type to T if universal
5824 -------------------------------
5825 -- Expected_Type_Is_Any_Real --
5826 -------------------------------
5828 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5829 begin
5830 -- N is the expression after "delta" in a fixed_point_definition;
5831 -- see RM-3.5.9(6):
5833 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5834 | N_Decimal_Fixed_Point_Definition
5836 -- N is one of the bounds in a real_range_specification;
5837 -- see RM-3.5.7(5):
5839 | N_Real_Range_Specification
5841 -- N is the expression of a delta_constraint;
5842 -- see RM-J.3(3):
5844 | N_Delta_Constraint;
5845 end Expected_Type_Is_Any_Real;
5847 -----------------------------
5848 -- Is_Integer_Or_Universal --
5849 -----------------------------
5851 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5852 T : Entity_Id;
5853 Index : Interp_Index;
5854 It : Interp;
5856 begin
5857 if not Is_Overloaded (N) then
5858 T := Etype (N);
5859 return Base_Type (T) = Base_Type (Standard_Integer)
5860 or else Is_Universal_Numeric_Type (T);
5861 else
5862 Get_First_Interp (N, Index, It);
5863 while Present (It.Typ) loop
5864 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5865 or else Is_Universal_Numeric_Type (It.Typ)
5866 then
5867 return True;
5868 end if;
5870 Get_Next_Interp (Index, It);
5871 end loop;
5872 end if;
5874 return False;
5875 end Is_Integer_Or_Universal;
5877 ----------------------------
5878 -- Set_Mixed_Mode_Operand --
5879 ----------------------------
5881 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5882 Index : Interp_Index;
5883 It : Interp;
5885 begin
5886 if Universal_Interpretation (N) = Universal_Integer then
5888 -- A universal integer literal is resolved as standard integer
5889 -- except in the case of a fixed-point result, where we leave it
5890 -- as universal (to be handled by Exp_Fixd later on)
5892 if Is_Fixed_Point_Type (T) then
5893 Resolve (N, Universal_Integer);
5894 else
5895 Resolve (N, Standard_Integer);
5896 end if;
5898 elsif Universal_Interpretation (N) = Universal_Real
5899 and then (T = Base_Type (Standard_Integer)
5900 or else Is_Universal_Numeric_Type (T))
5901 then
5902 -- A universal real can appear in a fixed-type context. We resolve
5903 -- the literal with that context, even though this might raise an
5904 -- exception prematurely (the other operand may be zero).
5906 Resolve (N, B_Typ);
5908 elsif Etype (N) = Base_Type (Standard_Integer)
5909 and then T = Universal_Real
5910 and then Is_Overloaded (N)
5911 then
5912 -- Integer arg in mixed-mode operation. Resolve with universal
5913 -- type, in case preference rule must be applied.
5915 Resolve (N, Universal_Integer);
5917 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5919 -- If the operand is part of a fixed multiplication operation,
5920 -- a conversion will be applied to each operand, so resolve it
5921 -- with its own type.
5923 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
5924 Resolve (N);
5926 else
5927 -- Not a mixed-mode operation, resolve with context
5929 Resolve (N, B_Typ);
5930 end if;
5932 elsif Etype (N) = Any_Fixed then
5934 -- N may itself be a mixed-mode operation, so use context type
5936 Resolve (N, B_Typ);
5938 elsif Is_Fixed_Point_Type (T)
5939 and then B_Typ = Universal_Fixed
5940 and then Is_Overloaded (N)
5941 then
5942 -- Must be (fixed * fixed) operation, operand must have one
5943 -- compatible interpretation.
5945 Resolve (N, Any_Fixed);
5947 elsif Is_Fixed_Point_Type (B_Typ)
5948 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5949 and then Is_Overloaded (N)
5950 then
5951 -- C * F(X) in a fixed context, where C is a real literal or a
5952 -- fixed-point expression. F must have either a fixed type
5953 -- interpretation or an integer interpretation, but not both.
5955 Get_First_Interp (N, Index, It);
5956 while Present (It.Typ) loop
5957 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5958 if Analyzed (N) then
5959 Error_Msg_N ("ambiguous operand in fixed operation", N);
5960 else
5961 Resolve (N, Standard_Integer);
5962 end if;
5964 elsif Is_Fixed_Point_Type (It.Typ) then
5965 if Analyzed (N) then
5966 Error_Msg_N ("ambiguous operand in fixed operation", N);
5967 else
5968 Resolve (N, It.Typ);
5969 end if;
5970 end if;
5972 Get_Next_Interp (Index, It);
5973 end loop;
5975 -- Reanalyze the literal with the fixed type of the context. If
5976 -- context is Universal_Fixed, we are within a conversion, leave
5977 -- the literal as a universal real because there is no usable
5978 -- fixed type, and the target of the conversion plays no role in
5979 -- the resolution.
5981 declare
5982 Op2 : Node_Id;
5983 T2 : Entity_Id;
5985 begin
5986 if N = L then
5987 Op2 := R;
5988 else
5989 Op2 := L;
5990 end if;
5992 if B_Typ = Universal_Fixed
5993 and then Nkind (Op2) = N_Real_Literal
5994 then
5995 T2 := Universal_Real;
5996 else
5997 T2 := B_Typ;
5998 end if;
6000 Set_Analyzed (Op2, False);
6001 Resolve (Op2, T2);
6002 end;
6004 -- A universal real conditional expression can appear in a fixed-type
6005 -- context and must be resolved with that context to facilitate the
6006 -- code generation in the back end. However, If the context is
6007 -- Universal_fixed (i.e. as an operand of a multiplication/division
6008 -- involving a fixed-point operand) the conditional expression must
6009 -- resolve to a unique visible fixed_point type, normally Duration.
6011 elsif Nkind (N) in N_Case_Expression | N_If_Expression
6012 and then Etype (N) = Universal_Real
6013 and then Is_Fixed_Point_Type (B_Typ)
6014 then
6015 if B_Typ = Universal_Fixed then
6016 Resolve (N, Unique_Fixed_Point_Type (N));
6018 else
6019 Resolve (N, B_Typ);
6020 end if;
6022 else
6023 Resolve (N);
6024 end if;
6025 end Set_Mixed_Mode_Operand;
6027 ----------------------
6028 -- Set_Operand_Type --
6029 ----------------------
6031 procedure Set_Operand_Type (N : Node_Id) is
6032 begin
6033 if Is_Universal_Numeric_Type (Etype (N)) then
6034 Set_Etype (N, T);
6035 end if;
6036 end Set_Operand_Type;
6038 -- Start of processing for Resolve_Arithmetic_Op
6040 begin
6041 if Comes_From_Source (N)
6042 and then Ekind (Entity (N)) = E_Function
6043 and then Is_Imported (Entity (N))
6044 and then Is_Intrinsic_Subprogram (Entity (N))
6045 then
6046 Resolve_Intrinsic_Operator (N, Typ);
6047 return;
6049 -- Special-case for mixed-mode universal expressions or fixed point type
6050 -- operation: each argument is resolved separately. The same treatment
6051 -- is required if one of the operands of a fixed point operation is
6052 -- universal real, since in this case we don't do a conversion to a
6053 -- specific fixed-point type (instead the expander handles the case).
6055 -- Set the type of the node to its universal interpretation because
6056 -- legality checks on an exponentiation operand need the context.
6058 elsif Is_Universal_Numeric_Type (B_Typ)
6059 and then Present (Universal_Interpretation (L))
6060 and then Present (Universal_Interpretation (R))
6061 then
6062 Set_Etype (N, B_Typ);
6063 Resolve (L, Universal_Interpretation (L));
6064 Resolve (R, Universal_Interpretation (R));
6066 elsif (B_Typ = Universal_Real
6067 or else Etype (N) = Universal_Fixed
6068 or else (Etype (N) = Any_Fixed
6069 and then Is_Fixed_Point_Type (B_Typ))
6070 or else (Is_Fixed_Point_Type (B_Typ)
6071 and then (Is_Integer_Or_Universal (L)
6072 or else
6073 Is_Integer_Or_Universal (R))))
6074 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
6075 then
6076 if TL = Universal_Integer or else TR = Universal_Integer then
6077 Check_For_Visible_Operator (N, B_Typ);
6078 end if;
6080 -- If context is a fixed type and one operand is integer, the other
6081 -- is resolved with the type of the context.
6083 if Is_Fixed_Point_Type (B_Typ)
6084 and then (Base_Type (TL) = Base_Type (Standard_Integer)
6085 or else TL = Universal_Integer)
6086 then
6087 Resolve (R, B_Typ);
6088 Resolve (L, TL);
6090 elsif Is_Fixed_Point_Type (B_Typ)
6091 and then (Base_Type (TR) = Base_Type (Standard_Integer)
6092 or else TR = Universal_Integer)
6093 then
6094 Resolve (L, B_Typ);
6095 Resolve (R, TR);
6097 -- If both operands are universal and the context is a floating
6098 -- point type, the operands are resolved to the type of the context.
6100 elsif Is_Floating_Point_Type (B_Typ) then
6101 Resolve (L, B_Typ);
6102 Resolve (R, B_Typ);
6104 else
6105 Set_Mixed_Mode_Operand (L, TR);
6106 Set_Mixed_Mode_Operand (R, TL);
6107 end if;
6109 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6110 -- multiplying operators from being used when the expected type is
6111 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6112 -- some cases where the expected type is actually Any_Real;
6113 -- Expected_Type_Is_Any_Real takes care of that case.
6115 if Etype (N) = Universal_Fixed
6116 or else Etype (N) = Any_Fixed
6117 then
6118 if B_Typ = Universal_Fixed
6119 and then not Expected_Type_Is_Any_Real (N)
6120 and then Nkind (Parent (N)) not in
6121 N_Type_Conversion | N_Unchecked_Type_Conversion
6122 then
6123 Error_Msg_N ("type cannot be determined from context!", N);
6124 Error_Msg_N ("\explicit conversion to result type required", N);
6126 Set_Etype (L, Any_Type);
6127 Set_Etype (R, Any_Type);
6129 else
6130 if Ada_Version = Ada_83
6131 and then Etype (N) = Universal_Fixed
6132 and then Nkind (Parent (N)) not in
6133 N_Type_Conversion | N_Unchecked_Type_Conversion
6134 then
6135 Error_Msg_N
6136 ("(Ada 83) fixed-point operation needs explicit "
6137 & "conversion", N);
6138 end if;
6140 -- The expected type is "any real type" in contexts like
6142 -- type T is delta <universal_fixed-expression> ...
6144 -- in which case we need to set the type to Universal_Real
6145 -- so that static expression evaluation will work properly.
6147 if Expected_Type_Is_Any_Real (N) then
6148 Set_Etype (N, Universal_Real);
6149 else
6150 Set_Etype (N, B_Typ);
6151 end if;
6152 end if;
6154 elsif Is_Fixed_Point_Type (B_Typ)
6155 and then (Is_Integer_Or_Universal (L)
6156 or else Nkind (L) = N_Real_Literal
6157 or else Nkind (R) = N_Real_Literal
6158 or else Is_Integer_Or_Universal (R))
6159 then
6160 Set_Etype (N, B_Typ);
6162 elsif Etype (N) = Any_Fixed then
6164 -- If no previous errors, this is only possible if one operand is
6165 -- overloaded and the context is universal. Resolve as such.
6167 Set_Etype (N, B_Typ);
6168 end if;
6170 else
6171 if Is_Universal_Numeric_Type (TL)
6172 and then
6173 Is_Universal_Numeric_Type (TR)
6174 then
6175 Check_For_Visible_Operator (N, B_Typ);
6176 end if;
6178 -- If the context is Universal_Fixed and the operands are also
6179 -- universal fixed, this is an error, unless there is only one
6180 -- applicable fixed_point type (usually Duration).
6182 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6183 T := Unique_Fixed_Point_Type (N);
6185 if T = Any_Type then
6186 Set_Etype (N, T);
6187 return;
6188 else
6189 Resolve (L, T);
6190 Resolve (R, T);
6191 end if;
6193 else
6194 Resolve (L, B_Typ);
6195 Resolve (R, B_Typ);
6196 end if;
6198 -- If one of the arguments was resolved to a non-universal type.
6199 -- label the result of the operation itself with the same type.
6200 -- Do the same for the universal argument, if any.
6202 T := Intersect_Types (L, R);
6203 Set_Etype (N, Base_Type (T));
6204 Set_Operand_Type (L);
6205 Set_Operand_Type (R);
6206 end if;
6208 Generate_Operator_Reference (N, Typ);
6209 Analyze_Dimension (N);
6210 Eval_Arithmetic_Op (N);
6212 -- Set overflow and division checking bit
6214 if Nkind (N) in N_Op then
6215 if not Overflow_Checks_Suppressed (Etype (N)) then
6216 Enable_Overflow_Check (N);
6217 end if;
6219 -- Give warning if explicit division by zero
6221 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6222 and then not Division_Checks_Suppressed (Etype (N))
6223 then
6224 Rop := Right_Opnd (N);
6226 if Compile_Time_Known_Value (Rop)
6227 and then ((Is_Integer_Type (Etype (Rop))
6228 and then Expr_Value (Rop) = Uint_0)
6229 or else
6230 (Is_Real_Type (Etype (Rop))
6231 and then Expr_Value_R (Rop) = Ureal_0))
6232 then
6233 -- Specialize the warning message according to the operation.
6234 -- When SPARK_Mode is On, force a warning instead of an error
6235 -- in that case, as this likely corresponds to deactivated
6236 -- code. The following warnings are for the case
6238 case Nkind (N) is
6239 when N_Op_Divide =>
6241 -- For division, we have two cases, for float division
6242 -- of an unconstrained float type, on a machine where
6243 -- Machine_Overflows is false, we don't get an exception
6244 -- at run-time, but rather an infinity or Nan. The Nan
6245 -- case is pretty obscure, so just warn about infinities.
6247 if Is_Floating_Point_Type (Typ)
6248 and then not Is_Constrained (Typ)
6249 and then not Machine_Overflows_On_Target
6250 then
6251 Error_Msg_N
6252 ("float division by zero, may generate "
6253 & "'+'/'- infinity??", Right_Opnd (N));
6255 -- For all other cases, we get a Constraint_Error
6257 else
6258 Apply_Compile_Time_Constraint_Error
6259 (N, "division by zero??", CE_Divide_By_Zero,
6260 Loc => Sloc (Right_Opnd (N)),
6261 Warn => SPARK_Mode = On);
6262 end if;
6264 when N_Op_Rem =>
6265 Apply_Compile_Time_Constraint_Error
6266 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6267 Loc => Sloc (Right_Opnd (N)),
6268 Warn => SPARK_Mode = On);
6270 when N_Op_Mod =>
6271 Apply_Compile_Time_Constraint_Error
6272 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6273 Loc => Sloc (Right_Opnd (N)),
6274 Warn => SPARK_Mode = On);
6276 -- Division by zero can only happen with division, rem,
6277 -- and mod operations.
6279 when others =>
6280 raise Program_Error;
6281 end case;
6283 -- Otherwise just set the flag to check at run time
6285 else
6286 Activate_Division_Check (N);
6287 end if;
6288 end if;
6290 -- If Restriction No_Implicit_Conditionals is active, then it is
6291 -- violated if either operand can be negative for mod, or for rem
6292 -- if both operands can be negative.
6294 if Restriction_Check_Required (No_Implicit_Conditionals)
6295 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6296 then
6297 declare
6298 Lo : Uint;
6299 Hi : Uint;
6300 OK : Boolean;
6302 LNeg : Boolean;
6303 RNeg : Boolean;
6304 -- Set if corresponding operand might be negative
6306 begin
6307 Determine_Range
6308 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6309 LNeg := (not OK) or else Lo < 0;
6311 Determine_Range
6312 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6313 RNeg := (not OK) or else Lo < 0;
6315 -- Check if we will be generating conditionals. There are two
6316 -- cases where that can happen, first for REM, the only case
6317 -- is largest negative integer mod -1, where the division can
6318 -- overflow, but we still have to give the right result. The
6319 -- front end generates a test for this annoying case. Here we
6320 -- just test if both operands can be negative (that's what the
6321 -- expander does, so we match its logic here).
6323 -- The second case is mod where either operand can be negative.
6324 -- In this case, the back end has to generate additional tests.
6326 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6327 or else
6328 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6329 then
6330 Check_Restriction (No_Implicit_Conditionals, N);
6331 end if;
6332 end;
6333 end if;
6334 end if;
6336 Check_Unset_Reference (L);
6337 Check_Unset_Reference (R);
6338 end Resolve_Arithmetic_Op;
6340 ------------------
6341 -- Resolve_Call --
6342 ------------------
6344 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6345 Loc : constant Source_Ptr := Sloc (N);
6346 Subp : constant Node_Id := Name (N);
6347 Body_Id : Entity_Id;
6348 I : Interp_Index;
6349 It : Interp;
6350 Nam : Entity_Id;
6351 Nam_Decl : Node_Id;
6352 Nam_UA : Entity_Id;
6353 Norm_OK : Boolean;
6354 Rtype : Entity_Id;
6355 Scop : Entity_Id;
6357 begin
6358 -- Preserve relevant elaboration-related attributes of the context which
6359 -- are no longer available or very expensive to recompute once analysis,
6360 -- resolution, and expansion are over.
6362 Mark_Elaboration_Attributes
6363 (N_Id => N,
6364 Checks => True,
6365 Modes => True,
6366 Warnings => True);
6368 -- The context imposes a unique interpretation with type Typ on a
6369 -- procedure or function call. Find the entity of the subprogram that
6370 -- yields the expected type, and propagate the corresponding formal
6371 -- constraints on the actuals. The caller has established that an
6372 -- interpretation exists, and emitted an error if not unique.
6374 -- First deal with the case of a call to an access-to-subprogram,
6375 -- dereference made explicit in Analyze_Call.
6377 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6378 if not Is_Overloaded (Subp) then
6379 Nam := Etype (Subp);
6381 else
6382 -- Find the interpretation whose type (a subprogram type) has a
6383 -- return type that is compatible with the context. Analysis of
6384 -- the node has established that one exists.
6386 Nam := Empty;
6388 Get_First_Interp (Subp, I, It);
6389 while Present (It.Typ) loop
6390 if Covers (Typ, Etype (It.Typ)) then
6391 Nam := It.Typ;
6392 exit;
6393 end if;
6395 Get_Next_Interp (I, It);
6396 end loop;
6398 if No (Nam) then
6399 raise Program_Error;
6400 end if;
6401 end if;
6403 -- If the prefix is not an entity, then resolve it
6405 if not Is_Entity_Name (Subp) then
6406 Resolve (Subp, Nam);
6407 end if;
6409 -- For an indirect call, we always invalidate checks, since we do not
6410 -- know whether the subprogram is local or global. Yes we could do
6411 -- better here, e.g. by knowing that there are no local subprograms,
6412 -- but it does not seem worth the effort. Similarly, we kill all
6413 -- knowledge of current constant values.
6415 Kill_Current_Values;
6417 -- If this is a procedure call which is really an entry call, do
6418 -- the conversion of the procedure call to an entry call. Protected
6419 -- operations use the same circuitry because the name in the call
6420 -- can be an arbitrary expression with special resolution rules.
6422 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6423 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6424 then
6425 Resolve_Entry_Call (N, Typ);
6427 if Legacy_Elaboration_Checks then
6428 Check_Elab_Call (N);
6429 end if;
6431 -- Annotate the tree by creating a call marker in case the original
6432 -- call is transformed by expansion. The call marker is automatically
6433 -- saved for later examination by the ABE Processing phase.
6435 Build_Call_Marker (N);
6437 -- Kill checks and constant values, as above for indirect case
6438 -- Who knows what happens when another task is activated?
6440 Kill_Current_Values;
6441 return;
6443 -- Normal subprogram call with name established in Resolve
6445 elsif not Is_Type (Entity (Subp)) then
6446 Nam := Entity (Subp);
6447 Set_Entity_With_Checks (Subp, Nam);
6449 -- Otherwise we must have the case of an overloaded call
6451 else
6452 pragma Assert (Is_Overloaded (Subp));
6454 -- Initialize Nam to prevent warning (we know it will be assigned
6455 -- in the loop below, but the compiler does not know that).
6457 Nam := Empty;
6459 Get_First_Interp (Subp, I, It);
6460 while Present (It.Typ) loop
6461 if Covers (Typ, It.Typ) then
6462 Nam := It.Nam;
6463 Set_Entity_With_Checks (Subp, Nam);
6464 exit;
6465 end if;
6467 Get_Next_Interp (I, It);
6468 end loop;
6469 end if;
6471 -- Check that a call to Current_Task does not occur in an entry body
6473 if Is_RTE (Nam, RE_Current_Task) then
6474 declare
6475 P : Node_Id;
6477 begin
6478 P := N;
6479 loop
6480 P := Parent (P);
6482 -- Exclude calls that occur within the default of a formal
6483 -- parameter of the entry, since those are evaluated outside
6484 -- of the body.
6486 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6488 if Nkind (P) = N_Entry_Body
6489 or else (Nkind (P) = N_Subprogram_Body
6490 and then Is_Entry_Barrier_Function (P))
6491 then
6492 Rtype := Etype (N);
6493 Error_Msg_Warn := SPARK_Mode /= On;
6494 Error_Msg_NE
6495 ("& should not be used in entry body (RM C.7(17))<<",
6496 N, Nam);
6497 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6498 Rewrite (N,
6499 Make_Raise_Program_Error (Loc,
6500 Reason => PE_Current_Task_In_Entry_Body));
6501 Set_Etype (N, Rtype);
6502 return;
6503 end if;
6504 end loop;
6505 end;
6506 end if;
6508 -- Check that a procedure call does not occur in the context of the
6509 -- entry call statement of a conditional or timed entry call. Note that
6510 -- the case of a call to a subprogram renaming of an entry will also be
6511 -- rejected. The test for N not being an N_Entry_Call_Statement is
6512 -- defensive, covering the possibility that the processing of entry
6513 -- calls might reach this point due to later modifications of the code
6514 -- above.
6516 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6517 and then Nkind (N) /= N_Entry_Call_Statement
6518 and then Entry_Call_Statement (Parent (N)) = N
6519 then
6520 if Ada_Version < Ada_2005 then
6521 Error_Msg_N ("entry call required in select statement", N);
6523 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6524 -- for a procedure_or_entry_call, the procedure_name or
6525 -- procedure_prefix of the procedure_call_statement shall denote
6526 -- an entry renamed by a procedure, or (a view of) a primitive
6527 -- subprogram of a limited interface whose first parameter is
6528 -- a controlling parameter.
6530 elsif Nkind (N) = N_Procedure_Call_Statement
6531 and then not Is_Renamed_Entry (Nam)
6532 and then not Is_Controlling_Limited_Procedure (Nam)
6533 then
6534 Error_Msg_N
6535 ("entry call or dispatching primitive of interface required", N);
6536 end if;
6537 end if;
6539 -- Check that this is not a call to a protected procedure or entry from
6540 -- within a protected function.
6542 Check_Internal_Protected_Use (N, Nam);
6544 -- Freeze the subprogram name if not in a spec-expression. Note that
6545 -- we freeze procedure calls as well as function calls. Procedure calls
6546 -- are not frozen according to the rules (RM 13.14(14)) because it is
6547 -- impossible to have a procedure call to a non-frozen procedure in
6548 -- pure Ada, but in the code that we generate in the expander, this
6549 -- rule needs extending because we can generate procedure calls that
6550 -- need freezing.
6552 -- In Ada 2012, expression functions may be called within pre/post
6553 -- conditions of subsequent functions or expression functions. Such
6554 -- calls do not freeze when they appear within generated bodies,
6555 -- (including the body of another expression function) which would
6556 -- place the freeze node in the wrong scope. An expression function
6557 -- is frozen in the usual fashion, by the appearance of a real body,
6558 -- or at the end of a declarative part. However an implicit call to
6559 -- an expression function may appear when it is part of a default
6560 -- expression in a call to an initialization procedure, and must be
6561 -- frozen now, even if the body is inserted at a later point.
6562 -- Otherwise, the call freezes the expression if expander is active,
6563 -- for example as part of an object declaration.
6565 if Is_Entity_Name (Subp)
6566 and then not In_Spec_Expression
6567 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6568 and then
6569 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6570 or else Expander_Active)
6571 then
6572 if Is_Expression_Function (Entity (Subp)) then
6574 -- Force freeze of expression function in call
6576 Set_Comes_From_Source (Subp, True);
6577 Set_Must_Not_Freeze (Subp, False);
6578 end if;
6580 Freeze_Expression (Subp);
6581 end if;
6583 -- For a predefined operator, the type of the result is the type imposed
6584 -- by context, except for a predefined operation on universal fixed.
6585 -- Otherwise the type of the call is the type returned by the subprogram
6586 -- being called.
6588 if Is_Predefined_Op (Nam) then
6589 if Etype (N) /= Universal_Fixed then
6590 Set_Etype (N, Typ);
6591 end if;
6593 -- If the subprogram returns an array type, and the context requires the
6594 -- component type of that array type, the node is really an indexing of
6595 -- the parameterless call. Resolve as such. A pathological case occurs
6596 -- when the type of the component is an access to the array type. In
6597 -- this case the call is truly ambiguous. If the call is to an intrinsic
6598 -- subprogram, it can't be an indexed component. This check is necessary
6599 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6600 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6601 -- pointers to the same array), the compiler gets confused and does an
6602 -- infinite recursion.
6604 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6605 and then
6606 ((Is_Array_Type (Etype (Nam))
6607 and then Covers (Typ, Component_Type (Etype (Nam))))
6608 or else
6609 (Is_Access_Type (Etype (Nam))
6610 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6611 and then
6612 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6613 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6614 then
6615 declare
6616 Index_Node : Node_Id;
6617 New_Subp : Node_Id;
6618 Ret_Type : constant Entity_Id := Etype (Nam);
6620 begin
6621 -- If this is a parameterless call there is no ambiguity and the
6622 -- call has the type of the function.
6624 if No (First_Actual (N)) then
6625 Set_Etype (N, Etype (Nam));
6627 if Present (First_Formal (Nam)) then
6628 Resolve_Actuals (N, Nam);
6629 end if;
6631 -- Annotate the tree by creating a call marker in case the
6632 -- original call is transformed by expansion. The call marker
6633 -- is automatically saved for later examination by the ABE
6634 -- Processing phase.
6636 Build_Call_Marker (N);
6638 elsif Is_Access_Type (Ret_Type)
6640 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6641 then
6642 Error_Msg_N
6643 ("cannot disambiguate function call and indexing", N);
6644 else
6645 New_Subp := Relocate_Node (Subp);
6647 -- The called entity may be an explicit dereference, in which
6648 -- case there is no entity to set.
6650 if Nkind (New_Subp) /= N_Explicit_Dereference then
6651 Set_Entity (Subp, Nam);
6652 end if;
6654 if (Is_Array_Type (Ret_Type)
6655 and then Component_Type (Ret_Type) /= Any_Type)
6656 or else
6657 (Is_Access_Type (Ret_Type)
6658 and then
6659 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6660 then
6661 if Needs_No_Actuals (Nam) then
6663 -- Indexed call to a parameterless function
6665 Index_Node :=
6666 Make_Indexed_Component (Loc,
6667 Prefix =>
6668 Make_Function_Call (Loc, Name => New_Subp),
6669 Expressions => Parameter_Associations (N));
6670 else
6671 -- An Ada 2005 prefixed call to a primitive operation
6672 -- whose first parameter is the prefix. This prefix was
6673 -- prepended to the parameter list, which is actually a
6674 -- list of indexes. Remove the prefix in order to build
6675 -- the proper indexed component.
6677 Index_Node :=
6678 Make_Indexed_Component (Loc,
6679 Prefix =>
6680 Make_Function_Call (Loc,
6681 Name => New_Subp,
6682 Parameter_Associations =>
6683 New_List
6684 (Remove_Head (Parameter_Associations (N)))),
6685 Expressions => Parameter_Associations (N));
6686 end if;
6688 -- Preserve the parenthesis count of the node
6690 Set_Paren_Count (Index_Node, Paren_Count (N));
6692 -- Since we are correcting a node classification error made
6693 -- by the parser, we call Replace rather than Rewrite.
6695 Replace (N, Index_Node);
6697 Set_Etype (Prefix (N), Ret_Type);
6698 Set_Etype (N, Typ);
6700 if Legacy_Elaboration_Checks then
6701 Check_Elab_Call (Prefix (N));
6702 end if;
6704 -- Annotate the tree by creating a call marker in case
6705 -- the original call is transformed by expansion. The call
6706 -- marker is automatically saved for later examination by
6707 -- the ABE Processing phase.
6709 Build_Call_Marker (Prefix (N));
6711 Resolve_Indexed_Component (N, Typ);
6712 end if;
6713 end if;
6715 return;
6716 end;
6718 else
6719 -- If the called function is not declared in the main unit and it
6720 -- returns the limited view of type then use the available view (as
6721 -- is done in Try_Object_Operation) to prevent back-end confusion;
6722 -- for the function entity itself. The call must appear in a context
6723 -- where the nonlimited view is available. If the function entity is
6724 -- in the extended main unit then no action is needed, because the
6725 -- back end handles this case. In either case the type of the call
6726 -- is the nonlimited view.
6728 if From_Limited_With (Etype (Nam))
6729 and then Present (Available_View (Etype (Nam)))
6730 then
6731 Set_Etype (N, Available_View (Etype (Nam)));
6733 if not In_Extended_Main_Code_Unit (Nam) then
6734 Set_Etype (Nam, Available_View (Etype (Nam)));
6735 end if;
6737 else
6738 Set_Etype (N, Etype (Nam));
6739 end if;
6740 end if;
6742 -- In the case where the call is to an overloaded subprogram, Analyze
6743 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6744 -- such a case Normalize_Actuals needs to be called once more to order
6745 -- the actuals correctly. Otherwise the call will have the ordering
6746 -- given by the last overloaded subprogram whether this is the correct
6747 -- one being called or not.
6749 if Is_Overloaded (Subp) then
6750 Normalize_Actuals (N, Nam, False, Norm_OK);
6751 pragma Assert (Norm_OK);
6752 end if;
6754 -- In any case, call is fully resolved now. Reset Overload flag, to
6755 -- prevent subsequent overload resolution if node is analyzed again
6757 Set_Is_Overloaded (Subp, False);
6758 Set_Is_Overloaded (N, False);
6760 -- A Ghost entity must appear in a specific context
6762 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6763 Check_Ghost_Context (Nam, N);
6764 end if;
6766 -- If we are calling the current subprogram from immediately within its
6767 -- body, then that is the case where we can sometimes detect cases of
6768 -- infinite recursion statically. Do not try this in case restriction
6769 -- No_Recursion is in effect anyway, and do it only for source calls.
6771 if Comes_From_Source (N) then
6772 Scop := Current_Scope;
6774 -- Issue warning for possible infinite recursion in the absence
6775 -- of the No_Recursion restriction.
6777 if Same_Or_Aliased_Subprograms (Nam, Scop)
6778 and then not Restriction_Active (No_Recursion)
6779 and then not Is_Static_Function (Scop)
6780 and then Check_Infinite_Recursion (N)
6781 then
6782 -- Here we detected and flagged an infinite recursion, so we do
6783 -- not need to test the case below for further warnings. Also we
6784 -- are all done if we now have a raise SE node.
6786 if Nkind (N) = N_Raise_Storage_Error then
6787 return;
6788 end if;
6790 -- If call is to immediately containing subprogram, then check for
6791 -- the case of a possible run-time detectable infinite recursion.
6793 else
6794 Scope_Loop : while Scop /= Standard_Standard loop
6795 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6797 -- Ada 2022 (AI12-0075): Static functions are never allowed
6798 -- to make a recursive call, as specified by 6.8(5.4/5).
6800 if Is_Static_Function (Scop) then
6801 Error_Msg_N
6802 ("recursive call not allowed in static expression "
6803 & "function", N);
6805 Set_Error_Posted (Scop);
6807 exit Scope_Loop;
6808 end if;
6810 -- Although in general case, recursion is not statically
6811 -- checkable, the case of calling an immediately containing
6812 -- subprogram is easy to catch.
6814 if not Is_Ignored_Ghost_Entity (Nam) then
6815 Check_Restriction (No_Recursion, N);
6816 end if;
6818 -- If the recursive call is to a parameterless subprogram,
6819 -- then even if we can't statically detect infinite
6820 -- recursion, this is pretty suspicious, and we output a
6821 -- warning. Furthermore, we will try later to detect some
6822 -- cases here at run time by expanding checking code (see
6823 -- Detect_Infinite_Recursion in package Exp_Ch6).
6825 -- If the recursive call is within a handler, do not emit a
6826 -- warning, because this is a common idiom: loop until input
6827 -- is correct, catch illegal input in handler and restart.
6829 if No (First_Formal (Nam))
6830 and then Etype (Nam) = Standard_Void_Type
6831 and then not Error_Posted (N)
6832 and then Nkind (Parent (N)) /= N_Exception_Handler
6833 then
6834 -- For the case of a procedure call. We give the message
6835 -- only if the call is the first statement in a sequence
6836 -- of statements, or if all previous statements are
6837 -- simple assignments. This is simply a heuristic to
6838 -- decrease false positives, without losing too many good
6839 -- warnings. The idea is that these previous statements
6840 -- may affect global variables the procedure depends on.
6841 -- We also exclude raise statements, that may arise from
6842 -- constraint checks and are probably unrelated to the
6843 -- intended control flow.
6845 if Nkind (N) = N_Procedure_Call_Statement
6846 and then Is_List_Member (N)
6847 then
6848 declare
6849 P : Node_Id;
6850 begin
6851 P := Prev (N);
6852 while Present (P) loop
6853 if Nkind (P) not in N_Assignment_Statement
6854 | N_Raise_Constraint_Error
6855 then
6856 exit Scope_Loop;
6857 end if;
6859 Prev (P);
6860 end loop;
6861 end;
6862 end if;
6864 -- Do not give warning if we are in a conditional context
6866 declare
6867 K : constant Node_Kind := Nkind (Parent (N));
6868 begin
6869 if (K = N_Loop_Statement
6870 and then Present (Iteration_Scheme (Parent (N))))
6871 or else K = N_If_Statement
6872 or else K = N_Elsif_Part
6873 or else K = N_Case_Statement_Alternative
6874 then
6875 exit Scope_Loop;
6876 end if;
6877 end;
6879 -- Here warning is to be issued
6881 Set_Has_Recursive_Call (Nam);
6882 Error_Msg_Warn := SPARK_Mode /= On;
6883 Error_Msg_N ("possible infinite recursion<<!", N);
6884 Error_Msg_N ("\Storage_Error ]<<!", N);
6885 end if;
6887 exit Scope_Loop;
6888 end if;
6890 Scop := Scope (Scop);
6891 end loop Scope_Loop;
6892 end if;
6893 end if;
6895 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6897 Check_Obsolescent_2005_Entity (Nam, Subp);
6899 -- If subprogram name is a predefined operator, it was given in
6900 -- functional notation. Replace call node with operator node, so
6901 -- that actuals can be resolved appropriately.
6903 if Ekind (Nam) = E_Operator or else Is_Predefined_Op (Nam) then
6904 Make_Call_Into_Operator (N, Typ, Nam);
6905 return;
6907 elsif Present (Alias (Nam)) and then Is_Predefined_Op (Alias (Nam)) then
6908 Resolve_Actuals (N, Nam);
6909 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6910 return;
6911 end if;
6913 -- Create a transient scope if the resulting type requires it
6915 -- There are several notable exceptions:
6917 -- a) In init procs, the transient scope overhead is not needed, and is
6918 -- even incorrect when the call is a nested initialization call for a
6919 -- component whose expansion may generate adjust calls. However, if the
6920 -- call is some other procedure call within an initialization procedure
6921 -- (for example a call to Create_Task in the init_proc of the task
6922 -- run-time record) a transient scope must be created around this call.
6924 -- b) Enumeration literal pseudo-calls need no transient scope
6926 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6927 -- functions) do not use the secondary stack even though the return
6928 -- type may be unconstrained.
6930 -- d) Calls to a build-in-place function, since such functions may
6931 -- allocate their result directly in a target object, and cases where
6932 -- the result does get allocated in the secondary stack are checked for
6933 -- within the specialized Exp_Ch6 procedures for expanding those
6934 -- build-in-place calls.
6936 -- e) Calls to inlinable expression functions do not use the secondary
6937 -- stack (since the call will be replaced by its returned object).
6939 -- f) If the subprogram is marked Inline_Always, then even if it returns
6940 -- an unconstrained type the call does not require use of the secondary
6941 -- stack. However, inlining will only take place if the body to inline
6942 -- is already present. It may not be available if e.g. the subprogram is
6943 -- declared in a child instance.
6945 -- g) If the subprogram is a static expression function and the call is
6946 -- a static call (the actuals are all static expressions), then we never
6947 -- want to create a transient scope (this could occur in the case of a
6948 -- static string-returning call).
6950 if Is_Inlined (Nam)
6951 and then Has_Pragma_Inline (Nam)
6952 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6953 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6954 then
6955 null;
6957 elsif Ekind (Nam) = E_Enumeration_Literal
6958 or else Is_Build_In_Place_Function (Nam)
6959 or else Is_Intrinsic_Subprogram (Nam)
6960 or else Is_Inlinable_Expression_Function (Nam)
6961 or else Is_Static_Function_Call (N)
6962 then
6963 null;
6965 -- A return statement from an ignored Ghost function does not use the
6966 -- secondary stack (or any other one).
6968 elsif Expander_Active
6969 and then Ekind (Nam) in E_Function | E_Subprogram_Type
6970 and then Requires_Transient_Scope (Etype (Nam))
6971 and then not Is_Ignored_Ghost_Entity (Nam)
6972 then
6973 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
6975 -- If the call appears within the bounds of a loop, it will be
6976 -- rewritten and reanalyzed, nothing left to do here.
6978 if Nkind (N) /= N_Function_Call then
6979 return;
6980 end if;
6981 end if;
6983 -- A protected function cannot be called within the definition of the
6984 -- enclosing protected type, unless it is part of a pre/postcondition
6985 -- on another protected operation. This may appear in the entry wrapper
6986 -- created for an entry with preconditions.
6988 if Is_Protected_Type (Scope (Nam))
6989 and then In_Open_Scopes (Scope (Nam))
6990 and then not Has_Completion (Scope (Nam))
6991 and then not In_Spec_Expression
6992 and then not Is_Entry_Wrapper (Current_Scope)
6993 then
6994 Error_Msg_NE
6995 ("& cannot be called before end of protected definition", N, Nam);
6996 end if;
6998 -- Propagate interpretation to actuals, and add default expressions
6999 -- where needed.
7001 if Present (First_Formal (Nam)) then
7002 Resolve_Actuals (N, Nam);
7004 -- Overloaded literals are rewritten as function calls, for purpose of
7005 -- resolution. After resolution, we can replace the call with the
7006 -- literal itself.
7008 elsif Ekind (Nam) = E_Enumeration_Literal then
7009 Copy_Node (Subp, N);
7010 Resolve_Entity_Name (N, Typ);
7012 -- Avoid validation, since it is a static function call
7014 Generate_Reference (Nam, Subp);
7015 return;
7016 end if;
7018 -- If the subprogram is not global, then kill all saved values and
7019 -- checks. This is a bit conservative, since in many cases we could do
7020 -- better, but it is not worth the effort. Similarly, we kill constant
7021 -- values. However we do not need to do this for internal entities
7022 -- (unless they are inherited user-defined subprograms), since they
7023 -- are not in the business of molesting local values.
7025 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
7026 -- kill all checks and values for calls to global subprograms. This
7027 -- takes care of the case where an access to a local subprogram is
7028 -- taken, and could be passed directly or indirectly and then called
7029 -- from almost any context.
7031 -- Note: we do not do this step till after resolving the actuals. That
7032 -- way we still take advantage of the current value information while
7033 -- scanning the actuals.
7035 -- We suppress killing values if we are processing the nodes associated
7036 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
7037 -- type kills all the values as part of analyzing the code that
7038 -- initializes the dispatch tables.
7040 if Inside_Freezing_Actions = 0
7041 and then (not Is_Library_Level_Entity (Nam)
7042 or else Suppress_Value_Tracking_On_Call
7043 (Nearest_Dynamic_Scope (Current_Scope)))
7044 and then (Comes_From_Source (Nam)
7045 or else (Present (Alias (Nam))
7046 and then Comes_From_Source (Alias (Nam))))
7047 then
7048 Kill_Current_Values;
7049 end if;
7051 -- If we are warning about unread OUT parameters, this is the place to
7052 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
7053 -- after the above call to Kill_Current_Values (since that call clears
7054 -- the Last_Assignment field of all local variables).
7056 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
7057 and then Comes_From_Source (N)
7058 and then In_Extended_Main_Source_Unit (N)
7059 then
7060 declare
7061 F : Entity_Id;
7062 A : Node_Id;
7064 begin
7065 F := First_Formal (Nam);
7066 A := First_Actual (N);
7067 while Present (F) and then Present (A) loop
7068 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
7069 and then Warn_On_Modified_As_Out_Parameter (F)
7070 and then Is_Entity_Name (A)
7071 and then Present (Entity (A))
7072 and then Comes_From_Source (N)
7073 and then Safe_To_Capture_Value (N, Entity (A))
7074 then
7075 Set_Last_Assignment (Entity (A), A);
7076 end if;
7078 Next_Formal (F);
7079 Next_Actual (A);
7080 end loop;
7081 end;
7082 end if;
7084 -- If the subprogram is a primitive operation, check whether or not
7085 -- it is a correct dispatching call.
7087 if Is_Overloadable (Nam) and then Is_Dispatching_Operation (Nam) then
7088 Check_Dispatching_Call (N);
7090 -- If the subprogram is an abstract operation, then flag an error
7092 elsif Is_Overloadable (Nam) and then Is_Abstract_Subprogram (Nam) then
7093 Nondispatching_Call_To_Abstract_Operation (N, Nam);
7094 end if;
7096 -- If this is a dispatching call, generate the appropriate reference,
7097 -- for better source navigation in GNAT Studio.
7099 if Is_Overloadable (Nam) and then Present (Controlling_Argument (N)) then
7100 Generate_Reference (Nam, Subp, 'R');
7102 -- Normal case, not a dispatching call: generate a call reference
7104 else
7105 Generate_Reference (Nam, Subp, 's');
7106 end if;
7108 if Is_Intrinsic_Subprogram (Nam) then
7109 Check_Intrinsic_Call (N);
7110 end if;
7112 -- Check for violation of restriction No_Specific_Termination_Handlers
7113 -- and warn on a potentially blocking call to Abort_Task.
7115 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7116 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7117 or else
7118 Is_RTE (Nam, RE_Specific_Handler))
7119 then
7120 Check_Restriction (No_Specific_Termination_Handlers, N);
7122 elsif Is_RTE (Nam, RE_Abort_Task) then
7123 Check_Potentially_Blocking_Operation (N);
7124 end if;
7126 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7127 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7128 -- need to check the second argument to determine whether it is an
7129 -- absolute or relative timing event.
7131 if Restriction_Check_Required (No_Relative_Delay)
7132 and then Is_RTE (Nam, RE_Set_Handler)
7133 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7134 then
7135 Check_Restriction (No_Relative_Delay, N);
7136 end if;
7138 -- Issue an error for a call to an eliminated subprogram. This routine
7139 -- will not perform the check if the call appears within a default
7140 -- expression.
7142 Check_For_Eliminated_Subprogram (Subp, Nam);
7144 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7145 -- class-wide and the call dispatches on result in a context that does
7146 -- not provide a tag, the call raises Program_Error.
7148 if Nkind (N) = N_Function_Call
7149 and then In_Instance
7150 and then Is_Generic_Actual_Type (Typ)
7151 and then Is_Class_Wide_Type (Typ)
7152 and then Has_Controlling_Result (Nam)
7153 and then Nkind (Parent (N)) = N_Object_Declaration
7154 then
7155 -- Verify that none of the formals are controlling
7157 declare
7158 Call_OK : Boolean := False;
7159 F : Entity_Id;
7161 begin
7162 F := First_Formal (Nam);
7163 while Present (F) loop
7164 if Is_Controlling_Formal (F) then
7165 Call_OK := True;
7166 exit;
7167 end if;
7169 Next_Formal (F);
7170 end loop;
7172 if not Call_OK then
7173 Error_Msg_Warn := SPARK_Mode /= On;
7174 Error_Msg_N ("!cannot determine tag of result<<", N);
7175 Error_Msg_N ("\Program_Error [<<!", N);
7176 Insert_Action (N,
7177 Make_Raise_Program_Error (Sloc (N),
7178 Reason => PE_Explicit_Raise));
7179 end if;
7180 end;
7181 end if;
7183 -- Check for calling a function with OUT or IN OUT parameter when the
7184 -- calling context (us right now) is not Ada 2012, so does not allow
7185 -- OUT or IN OUT parameters in function calls. Functions declared in
7186 -- a predefined unit are OK, as they may be called indirectly from a
7187 -- user-declared instantiation.
7189 if Ada_Version < Ada_2012
7190 and then Ekind (Nam) = E_Function
7191 and then Has_Out_Or_In_Out_Parameter (Nam)
7192 and then not In_Predefined_Unit (Nam)
7193 then
7194 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7195 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7196 end if;
7198 -- Check the dimensions of the actuals in the call. For function calls,
7199 -- propagate the dimensions from the returned type to N.
7201 Analyze_Dimension_Call (N, Nam);
7203 -- Check unreachable code after calls to procedures with No_Return
7205 if Ekind (Nam) = E_Procedure and then No_Return (Nam) then
7206 Check_Unreachable_Code (N);
7207 end if;
7209 -- All done, evaluate call and deal with elaboration issues
7211 Eval_Call (N);
7213 if Legacy_Elaboration_Checks then
7214 Check_Elab_Call (N);
7215 end if;
7217 -- Annotate the tree by creating a call marker in case the original call
7218 -- is transformed by expansion. The call marker is automatically saved
7219 -- for later examination by the ABE Processing phase.
7221 Build_Call_Marker (N);
7223 Mark_Use_Clauses (Subp);
7225 Warn_On_Overlapping_Actuals (Nam, N);
7227 -- Ada 2022 (AI12-0075): If the call is a static call to a static
7228 -- expression function, then we want to "inline" the call, replacing
7229 -- it with the folded static result. This is not done if the checking
7230 -- for a potentially static expression is enabled or if an error has
7231 -- been posted on the call (which may be due to the check for recursive
7232 -- calls, in which case we don't want to fall into infinite recursion
7233 -- when doing the inlining).
7235 if not Checking_Potentially_Static_Expression
7236 and then Is_Static_Function_Call (N)
7237 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7238 and then not Error_Posted (Ultimate_Alias (Nam))
7239 then
7240 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7242 -- In GNATprove mode, expansion is disabled, but we want to inline some
7243 -- subprograms to facilitate formal verification. Indirect calls through
7244 -- a subprogram type or within a generic cannot be inlined. Inlining is
7245 -- performed only for calls subject to SPARK_Mode on.
7247 elsif GNATprove_Mode
7248 and then SPARK_Mode = On
7249 and then Is_Overloadable (Nam)
7250 and then not Inside_A_Generic
7251 then
7252 Nam_UA := Ultimate_Alias (Nam);
7253 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7255 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7256 Body_Id := Corresponding_Body (Nam_Decl);
7258 -- Nothing to do if the subprogram is not eligible for inlining in
7259 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7261 if not Is_Inlined_Always (Nam_UA)
7262 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7263 or else Debug_Flag_M
7264 then
7265 null;
7267 -- Calls cannot be inlined inside assertions, as GNATprove treats
7268 -- assertions as logic expressions. Only issue a message when the
7269 -- body has been seen, otherwise this leads to spurious messages
7270 -- on expression functions.
7272 elsif In_Assertion_Expr /= 0 then
7273 Cannot_Inline
7274 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7275 Suppress_Info => No (Body_Id));
7277 -- Calls cannot be inlined inside default expressions
7279 elsif In_Default_Expr then
7280 Cannot_Inline
7281 ("cannot inline & (in default expression)?", N, Nam_UA);
7283 -- Calls cannot be inlined inside quantified expressions, which
7284 -- are left in expression form for GNATprove. Since these
7285 -- expressions are only preanalyzed, we need to detect the failure
7286 -- to inline outside of the case for Full_Analysis below.
7288 elsif In_Quantified_Expression (N) then
7289 Cannot_Inline
7290 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7292 -- Inlining should not be performed during preanalysis
7294 elsif Full_Analysis then
7296 -- Do not inline calls inside expression functions or functions
7297 -- generated by the front end for subtype predicates, as this
7298 -- would prevent interpreting them as logical formulas in
7299 -- GNATprove. Only issue a message when the body has been seen,
7300 -- otherwise this leads to spurious messages on callees that
7301 -- are themselves expression functions.
7303 if Present (Current_Subprogram)
7304 and then
7305 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7306 or else Is_Predicate_Function (Current_Subprogram)
7307 or else Is_Invariant_Procedure (Current_Subprogram)
7308 or else Is_DIC_Procedure (Current_Subprogram))
7309 then
7310 if Present (Body_Id)
7311 and then Present (Body_To_Inline (Nam_Decl))
7312 then
7313 if Is_Predicate_Function (Current_Subprogram) then
7314 Cannot_Inline
7315 ("cannot inline & (inside predicate)?",
7316 N, Nam_UA);
7318 elsif Is_Invariant_Procedure (Current_Subprogram) then
7319 Cannot_Inline
7320 ("cannot inline & (inside invariant)?",
7321 N, Nam_UA);
7323 elsif Is_DIC_Procedure (Current_Subprogram) then
7324 Cannot_Inline
7325 ("cannot inline & (inside Default_Initial_Condition)?",
7326 N, Nam_UA);
7328 else
7329 Cannot_Inline
7330 ("cannot inline & (inside expression function)?",
7331 N, Nam_UA);
7332 end if;
7333 end if;
7335 -- Cannot inline a call inside the definition of a record type,
7336 -- typically inside the constraints of the type. Calls in
7337 -- default expressions are also not inlined, but this is
7338 -- filtered out above when testing In_Default_Expr.
7340 elsif Is_Record_Type (Current_Scope) then
7341 Cannot_Inline
7342 ("cannot inline & (inside record type)?", N, Nam_UA);
7344 -- With the one-pass inlining technique, a call cannot be
7345 -- inlined if the corresponding body has not been seen yet.
7347 elsif No (Body_Id) then
7348 Cannot_Inline
7349 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7351 -- Nothing to do if there is no body to inline, indicating that
7352 -- the subprogram is not suitable for inlining in GNATprove
7353 -- mode.
7355 elsif No (Body_To_Inline (Nam_Decl)) then
7356 null;
7358 -- Calls cannot be inlined inside potentially unevaluated
7359 -- expressions, as this would create complex actions inside
7360 -- expressions, that are not handled by GNATprove.
7362 elsif Is_Potentially_Unevaluated (N) then
7363 Cannot_Inline
7364 ("cannot inline & (in potentially unevaluated context)?",
7365 N, Nam_UA);
7367 -- Calls cannot be inlined inside the conditions of while
7368 -- loops, as this would create complex actions inside
7369 -- the condition, that are not handled by GNATprove.
7371 elsif In_Statement_Condition_With_Actions (N) then
7372 Cannot_Inline
7373 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7375 -- Do not inline calls which would possibly lead to missing a
7376 -- type conversion check on an input parameter.
7378 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7379 Cannot_Inline
7380 ("cannot inline & (possible check on input parameters)?",
7381 N, Nam_UA);
7383 -- Otherwise, inline the call, issuing an info message when
7384 -- -gnatd_f is set.
7386 else
7387 if Debug_Flag_Underscore_F then
7388 Error_Msg_NE
7389 ("info: analyzing call to & in context?", N, Nam_UA);
7390 end if;
7392 Expand_Inlined_Call (N, Nam_UA, Nam);
7393 end if;
7394 end if;
7395 end if;
7396 end if;
7397 end Resolve_Call;
7399 -----------------------------
7400 -- Resolve_Case_Expression --
7401 -----------------------------
7403 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7404 Alt : Node_Id;
7405 Alt_Expr : Node_Id;
7406 Alt_Typ : Entity_Id;
7407 Is_Dyn : Boolean;
7409 begin
7410 Alt := First (Alternatives (N));
7411 while Present (Alt) loop
7412 Alt_Expr := Expression (Alt);
7414 if Error_Posted (Alt_Expr) then
7415 return;
7416 end if;
7418 Resolve_Dependent_Expression (N, Alt_Expr, Typ);
7420 Check_Unset_Reference (Alt_Expr);
7421 Alt_Typ := Etype (Alt_Expr);
7423 -- When the expression is of a scalar subtype different from the
7424 -- result subtype, then insert a conversion to ensure the generation
7425 -- of a constraint check.
7427 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7428 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7429 Analyze_And_Resolve (Alt_Expr, Typ);
7430 end if;
7432 Next (Alt);
7433 end loop;
7435 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7436 -- dynamically tagged must be known statically.
7438 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7439 Alt := First (Alternatives (N));
7440 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7442 while Present (Alt) loop
7443 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7444 Error_Msg_N
7445 ("all or none of the dependent expressions can be "
7446 & "dynamically tagged", N);
7447 end if;
7449 Next (Alt);
7450 end loop;
7451 end if;
7453 Set_Etype (N, Typ);
7454 Eval_Case_Expression (N);
7455 Analyze_Dimension (N);
7456 end Resolve_Case_Expression;
7458 -------------------------------
7459 -- Resolve_Character_Literal --
7460 -------------------------------
7462 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7463 B_Typ : constant Entity_Id := Base_Type (Typ);
7464 C : Entity_Id;
7466 begin
7467 -- Verify that the character does belong to the type of the context
7469 Set_Etype (N, B_Typ);
7470 Eval_Character_Literal (N);
7472 -- Wide_Wide_Character literals must always be defined, since the set
7473 -- of wide wide character literals is complete, i.e. if a character
7474 -- literal is accepted by the parser, then it is OK for wide wide
7475 -- character (out of range character literals are rejected).
7477 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7478 return;
7480 -- Always accept character literal for type Any_Character, which
7481 -- occurs in error situations and in comparisons of literals, both
7482 -- of which should accept all literals.
7484 elsif B_Typ = Any_Character then
7485 return;
7487 -- For Standard.Character or a type derived from it, check that the
7488 -- literal is in range.
7490 elsif Root_Type (B_Typ) = Standard_Character then
7491 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7492 return;
7493 end if;
7495 -- For Standard.Wide_Character or a type derived from it, check that the
7496 -- literal is in range.
7498 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7499 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7500 return;
7501 end if;
7503 -- If the entity is already set, this has already been resolved in a
7504 -- generic context, or comes from expansion. Nothing else to do.
7506 elsif Present (Entity (N)) then
7507 return;
7509 -- Otherwise we have a user defined character type, and we can use the
7510 -- standard visibility mechanisms to locate the referenced entity.
7512 else
7513 C := Current_Entity (N);
7514 while Present (C) loop
7515 if Etype (C) = B_Typ then
7516 Set_Entity_With_Checks (N, C);
7517 Generate_Reference (C, N);
7518 return;
7519 end if;
7521 C := Homonym (C);
7522 end loop;
7523 end if;
7525 -- If we fall through, then the literal does not match any of the
7526 -- entries of the enumeration type. This isn't just a constraint error
7527 -- situation, it is an illegality (see RM 4.2).
7529 Error_Msg_NE
7530 ("character not defined for }", N, First_Subtype (B_Typ));
7531 end Resolve_Character_Literal;
7533 ---------------------------
7534 -- Resolve_Comparison_Op --
7535 ---------------------------
7537 -- The operands must have compatible types and the boolean context does not
7538 -- participate in the resolution. The first pass verifies that the operands
7539 -- are not ambiguous and sets their type correctly, or to Any_Type in case
7540 -- of ambiguity. If both operands are strings or aggregates, then they are
7541 -- ambiguous even if they carry a single (universal) type.
7543 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7544 L : constant Node_Id := Left_Opnd (N);
7545 R : constant Node_Id := Right_Opnd (N);
7547 T : Entity_Id := Find_Unique_Type (L, R);
7549 begin
7550 if T = Any_Fixed then
7551 T := Unique_Fixed_Point_Type (L);
7552 end if;
7554 Set_Etype (N, Base_Type (Typ));
7555 Generate_Reference (T, N, ' ');
7557 if T = Any_Type then
7558 -- Deal with explicit ambiguity of operands
7560 if Is_Overloaded (L) or else Is_Overloaded (R) then
7561 Ambiguous_Operands (N);
7562 end if;
7564 return;
7565 end if;
7567 -- Deal with other error cases
7569 if T = Any_String or else
7570 T = Any_Composite or else
7571 T = Any_Character
7572 then
7573 if T = Any_Character then
7574 Ambiguous_Character (L);
7575 else
7576 Error_Msg_N ("ambiguous operands for comparison", N);
7577 end if;
7579 Set_Etype (N, Any_Type);
7580 return;
7581 end if;
7583 -- Resolve the operands if types OK
7585 Resolve (L, T);
7586 Resolve (R, T);
7587 Check_Unset_Reference (L);
7588 Check_Unset_Reference (R);
7589 Generate_Operator_Reference (N, T);
7590 Check_Low_Bound_Tested (N);
7592 -- Check comparison on unordered enumeration
7594 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7595 Error_Msg_Sloc := Sloc (Etype (L));
7596 Error_Msg_NE
7597 ("comparison on unordered enumeration type& declared#?.u?",
7598 N, Etype (L));
7599 end if;
7601 Analyze_Dimension (N);
7603 Eval_Relational_Op (N);
7604 end Resolve_Comparison_Op;
7606 --------------------------------
7607 -- Resolve_Declare_Expression --
7608 --------------------------------
7610 procedure Resolve_Declare_Expression
7611 (N : Node_Id;
7612 Typ : Entity_Id)
7614 Expr : constant Node_Id := Expression (N);
7616 Decl : Node_Id;
7617 Local : Entity_Id := Empty;
7619 function Replace_Local (N : Node_Id) return Traverse_Result;
7620 -- Use a tree traversal to replace each occurrence of the name of
7621 -- a local object declared in the construct, with the corresponding
7622 -- entity. This replaces the usual way to perform name capture by
7623 -- visibility, because it is not possible to place on the scope
7624 -- stack the fake scope created for the analysis of the local
7625 -- declarations; such a scope conflicts with the transient scopes
7626 -- that may be generated if the expression includes function calls
7627 -- requiring finalization.
7629 -------------------
7630 -- Replace_Local --
7631 -------------------
7633 function Replace_Local (N : Node_Id) return Traverse_Result is
7634 begin
7635 -- The identifier may be the prefix of a selected component,
7636 -- but not a selector name, because the local entities do not
7637 -- have a scope that can be named: a selected component whose
7638 -- selector is a homonym of a local entity must denote some
7639 -- global entity.
7641 if Nkind (N) = N_Identifier
7642 and then Chars (N) = Chars (Local)
7643 and then No (Entity (N))
7644 and then
7645 (Nkind (Parent (N)) /= N_Selected_Component
7646 or else N = Prefix (Parent (N)))
7647 then
7648 Set_Entity (N, Local);
7649 Set_Etype (N, Etype (Local));
7650 end if;
7652 return OK;
7653 end Replace_Local;
7655 procedure Replace_Local_Ref is new Traverse_Proc (Replace_Local);
7657 -- Start of processing for Resolve_Declare_Expression
7659 begin
7661 Decl := First (Actions (N));
7663 while Present (Decl) loop
7664 if Nkind (Decl) in
7665 N_Object_Declaration | N_Object_Renaming_Declaration
7666 and then Comes_From_Source (Defining_Identifier (Decl))
7667 then
7668 Local := Defining_Identifier (Decl);
7669 Replace_Local_Ref (Expr);
7671 -- Traverse the expression to replace references to local
7672 -- variables that occur within declarations of the
7673 -- declare_expression.
7675 declare
7676 D : Node_Id := Next (Decl);
7677 begin
7678 while Present (D) loop
7679 Replace_Local_Ref (D);
7680 Next (D);
7681 end loop;
7682 end;
7683 end if;
7685 Next (Decl);
7686 end loop;
7688 -- The end of the declarative list is a freeze point for the
7689 -- local declarations.
7691 if Present (Local) then
7692 Decl := Parent (Local);
7693 Freeze_All (First_Entity (Scope (Local)), Decl);
7694 end if;
7696 Resolve (Expr, Typ);
7697 Check_Unset_Reference (Expr);
7698 end Resolve_Declare_Expression;
7700 -----------------------------------
7701 -- Resolve_Dependent_Expression --
7702 -----------------------------------
7704 procedure Resolve_Dependent_Expression
7705 (N : Node_Id;
7706 Expr : Node_Id;
7707 Typ : Entity_Id)
7709 begin
7710 -- RM 4.5.7(8/3) says that the expected type of dependent expressions is
7711 -- that of the conditional expression but RM 4.5.7(10/3) forces the type
7712 -- of the conditional expression without changing the expected type (the
7713 -- expected type of the operand of a type conversion is any type), so we
7714 -- may have a gap between these two types that is bridged by the dynamic
7715 -- semantics specified by RM 4.5.7(20/3) with the associated legality
7716 -- rule RM 4.5.7(16/3) that will be automatically enforced.
7718 if Nkind (Parent (N)) = N_Type_Conversion
7719 and then Nkind (Expr) /= N_Raise_Expression
7720 then
7721 Convert_To_And_Rewrite (Typ, Expr);
7722 Analyze_And_Resolve (Expr);
7723 else
7724 Resolve (Expr, Typ);
7725 end if;
7726 end Resolve_Dependent_Expression;
7728 -----------------------------------------
7729 -- Resolve_Discrete_Subtype_Indication --
7730 -----------------------------------------
7732 procedure Resolve_Discrete_Subtype_Indication
7733 (N : Node_Id;
7734 Typ : Entity_Id)
7736 R : Node_Id;
7737 S : Entity_Id;
7739 begin
7740 Analyze (Subtype_Mark (N));
7741 S := Entity (Subtype_Mark (N));
7743 if Nkind (Constraint (N)) /= N_Range_Constraint then
7744 Error_Msg_N ("expect range constraint for discrete type", N);
7745 Set_Etype (N, Any_Type);
7747 else
7748 R := Range_Expression (Constraint (N));
7750 if R = Error then
7751 return;
7752 end if;
7754 Analyze (R);
7756 if Base_Type (S) /= Base_Type (Typ) then
7757 Error_Msg_NE
7758 ("expect subtype of }", N, First_Subtype (Typ));
7760 -- Rewrite the constraint as a range of Typ
7761 -- to allow compilation to proceed further.
7763 Set_Etype (N, Typ);
7764 Rewrite (Low_Bound (R),
7765 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7766 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7767 Attribute_Name => Name_First));
7768 Rewrite (High_Bound (R),
7769 Make_Attribute_Reference (Sloc (High_Bound (R)),
7770 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7771 Attribute_Name => Name_First));
7773 else
7774 Resolve (R, Typ);
7775 Set_Etype (N, Etype (R));
7777 -- Additionally, we must check that the bounds are compatible
7778 -- with the given subtype, which might be different from the
7779 -- type of the context.
7781 Apply_Range_Check (R, S);
7783 -- ??? If the above check statically detects a Constraint_Error
7784 -- it replaces the offending bound(s) of the range R with a
7785 -- Constraint_Error node. When the itype which uses these bounds
7786 -- is frozen the resulting call to Duplicate_Subexpr generates
7787 -- a new temporary for the bounds.
7789 -- Unfortunately there are other itypes that are also made depend
7790 -- on these bounds, so when Duplicate_Subexpr is called they get
7791 -- a forward reference to the newly created temporaries and Gigi
7792 -- aborts on such forward references. This is probably sign of a
7793 -- more fundamental problem somewhere else in either the order of
7794 -- itype freezing or the way certain itypes are constructed.
7796 -- To get around this problem we call Remove_Side_Effects right
7797 -- away if either bounds of R are a Constraint_Error.
7799 declare
7800 L : constant Node_Id := Low_Bound (R);
7801 H : constant Node_Id := High_Bound (R);
7803 begin
7804 if Nkind (L) = N_Raise_Constraint_Error then
7805 Remove_Side_Effects (L);
7806 end if;
7808 if Nkind (H) = N_Raise_Constraint_Error then
7809 Remove_Side_Effects (H);
7810 end if;
7811 end;
7813 Check_Unset_Reference (Low_Bound (R));
7814 Check_Unset_Reference (High_Bound (R));
7815 end if;
7816 end if;
7817 end Resolve_Discrete_Subtype_Indication;
7819 -------------------------
7820 -- Resolve_Entity_Name --
7821 -------------------------
7823 -- Used to resolve identifiers and expanded names
7825 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7826 function Is_Assignment_Or_Object_Expression
7827 (Context : Node_Id;
7828 Expr : Node_Id) return Boolean;
7829 -- Determine whether node Context denotes an assignment statement or an
7830 -- object declaration whose expression is node Expr.
7832 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7833 -- Determine whether Expr is part of an N_Attribute_Reference
7834 -- expression.
7836 ----------------------------------------
7837 -- Is_Assignment_Or_Object_Expression --
7838 ----------------------------------------
7840 function Is_Assignment_Or_Object_Expression
7841 (Context : Node_Id;
7842 Expr : Node_Id) return Boolean
7844 begin
7845 if Nkind (Context) in N_Assignment_Statement | N_Object_Declaration
7846 and then Expression (Context) = Expr
7847 then
7848 return True;
7850 -- Check whether a construct that yields a name is the expression of
7851 -- an assignment statement or an object declaration.
7853 elsif (Nkind (Context) in N_Attribute_Reference
7854 | N_Explicit_Dereference
7855 | N_Indexed_Component
7856 | N_Selected_Component
7857 | N_Slice
7858 and then Prefix (Context) = Expr)
7859 or else
7860 (Nkind (Context) in N_Type_Conversion
7861 | N_Unchecked_Type_Conversion
7862 and then Expression (Context) = Expr)
7863 then
7864 return
7865 Is_Assignment_Or_Object_Expression
7866 (Context => Parent (Context),
7867 Expr => Context);
7869 -- Otherwise the context is not an assignment statement or an object
7870 -- declaration.
7872 else
7873 return False;
7874 end if;
7875 end Is_Assignment_Or_Object_Expression;
7877 -----------------------------
7878 -- Is_Attribute_Expression --
7879 -----------------------------
7881 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
7882 N : Node_Id := Expr;
7883 begin
7884 while Present (N) loop
7885 if Nkind (N) = N_Attribute_Reference then
7886 return True;
7888 -- Prevent the search from going too far
7890 elsif Is_Body_Or_Package_Declaration (N) then
7891 return False;
7892 end if;
7894 N := Parent (N);
7895 end loop;
7897 return False;
7898 end Is_Attribute_Expression;
7900 -- Local variables
7902 E : constant Entity_Id := Entity (N);
7903 Par : Node_Id;
7905 -- Start of processing for Resolve_Entity_Name
7907 begin
7908 -- If garbage from errors, set to Any_Type and return
7910 if No (E) and then Total_Errors_Detected /= 0 then
7911 Set_Etype (N, Any_Type);
7912 return;
7913 end if;
7915 -- Replace named numbers by corresponding literals. Note that this is
7916 -- the one case where Resolve_Entity_Name must reset the Etype, since
7917 -- it is currently marked as universal.
7919 if Ekind (E) = E_Named_Integer then
7920 Set_Etype (N, Typ);
7921 Eval_Named_Integer (N);
7923 elsif Ekind (E) = E_Named_Real then
7924 Set_Etype (N, Typ);
7925 Eval_Named_Real (N);
7927 -- For enumeration literals, we need to make sure that a proper style
7928 -- check is done, since such literals are overloaded, and thus we did
7929 -- not do a style check during the first phase of analysis.
7931 elsif Ekind (E) = E_Enumeration_Literal then
7932 Set_Entity_With_Checks (N, E);
7933 Eval_Entity_Name (N);
7935 -- Case of (sub)type name appearing in a context where an expression
7936 -- is expected. This is legal if occurrence is a current instance.
7937 -- See RM 8.6 (17/3). It is also legal if the expression is
7938 -- part of a choice pattern for a case stmt/expr having a
7939 -- non-discrete selecting expression.
7941 elsif Is_Type (E) then
7942 if Is_Current_Instance (N) or else Is_Case_Choice_Pattern (N) then
7943 null;
7945 -- Any other use is an error
7947 else
7948 Error_Msg_N
7949 ("invalid use of subtype mark in expression or call", N);
7950 end if;
7952 -- Check discriminant use if entity is discriminant in current scope,
7953 -- i.e. discriminant of record or concurrent type currently being
7954 -- analyzed. Uses in corresponding body are unrestricted.
7956 elsif Ekind (E) = E_Discriminant
7957 and then Scope (E) = Current_Scope
7958 and then not Has_Completion (Current_Scope)
7959 then
7960 Check_Discriminant_Use (N);
7962 -- A parameterless generic function cannot appear in a context that
7963 -- requires resolution.
7965 elsif Ekind (E) = E_Generic_Function then
7966 Error_Msg_N ("illegal use of generic function", N);
7968 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7969 -- array types (i.e. bounds and length) are legal.
7971 elsif Ekind (E) = E_Out_Parameter
7972 and then (Is_Scalar_Type (Etype (E))
7973 or else not Is_Attribute_Expression (Parent (N)))
7975 and then (Nkind (Parent (N)) in N_Op
7976 or else Nkind (Parent (N)) = N_Explicit_Dereference
7977 or else Is_Assignment_Or_Object_Expression
7978 (Context => Parent (N),
7979 Expr => N))
7980 then
7981 if Ada_Version = Ada_83 then
7982 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7983 end if;
7985 -- In all other cases, just do the possible static evaluation
7987 else
7988 -- A deferred constant that appears in an expression must have a
7989 -- completion, unless it has been removed by in-place expansion of
7990 -- an aggregate. A constant that is a renaming does not need
7991 -- initialization.
7993 if Ekind (E) = E_Constant
7994 and then Comes_From_Source (E)
7995 and then No (Constant_Value (E))
7996 and then Is_Frozen (Etype (E))
7997 and then not In_Spec_Expression
7998 and then not Is_Imported (E)
7999 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
8000 then
8001 if No_Initialization (Parent (E))
8002 or else (Present (Full_View (E))
8003 and then No_Initialization (Parent (Full_View (E))))
8004 then
8005 null;
8006 else
8007 Error_Msg_N
8008 ("deferred constant is frozen before completion", N);
8009 end if;
8010 end if;
8012 Eval_Entity_Name (N);
8013 end if;
8015 Par := Parent (N);
8017 -- When the entity appears in a parameter association, retrieve the
8018 -- related subprogram call.
8020 if Nkind (Par) = N_Parameter_Association then
8021 Par := Parent (Par);
8022 end if;
8024 if Comes_From_Source (N) then
8026 -- The following checks are only relevant when SPARK_Mode is on as
8027 -- they are not standard Ada legality rules.
8029 if SPARK_Mode = On then
8031 -- An effectively volatile object for reading must appear in
8032 -- non-interfering context (SPARK RM 7.1.3(10)).
8034 if Is_Object (E)
8035 and then Is_Effectively_Volatile_For_Reading (E)
8036 and then
8037 not Is_OK_Volatile_Context (Par, N, Check_Actuals => False)
8038 then
8039 SPARK_Msg_N
8040 ("volatile object cannot appear in this context "
8041 & "(SPARK RM 7.1.3(10))", N);
8042 end if;
8044 -- Check for possible elaboration issues with respect to reads of
8045 -- variables. The act of renaming the variable is not considered a
8046 -- read as it simply establishes an alias.
8048 if Legacy_Elaboration_Checks
8049 and then Ekind (E) = E_Variable
8050 and then Dynamic_Elaboration_Checks
8051 and then Nkind (Par) /= N_Object_Renaming_Declaration
8052 then
8053 Check_Elab_Call (N);
8054 end if;
8055 end if;
8057 -- The variable may eventually become a constituent of a single
8058 -- protected/task type. Record the reference now and verify its
8059 -- legality when analyzing the contract of the variable
8060 -- (SPARK RM 9.3).
8062 if Ekind (E) = E_Variable then
8063 Record_Possible_Part_Of_Reference (E, N);
8064 end if;
8066 -- A Ghost entity must appear in a specific context
8068 if Is_Ghost_Entity (E) then
8069 Check_Ghost_Context (E, N);
8070 end if;
8071 end if;
8073 -- We may be resolving an entity within expanded code, so a reference to
8074 -- an entity should be ignored when calculating effective use clauses to
8075 -- avoid inappropriate marking.
8077 if Comes_From_Source (N) then
8078 Mark_Use_Clauses (E);
8079 end if;
8080 end Resolve_Entity_Name;
8082 -------------------
8083 -- Resolve_Entry --
8084 -------------------
8086 procedure Resolve_Entry (Entry_Name : Node_Id) is
8087 Loc : constant Source_Ptr := Sloc (Entry_Name);
8088 Nam : Entity_Id;
8089 New_N : Node_Id;
8090 S : Entity_Id;
8091 Tsk : Entity_Id;
8092 E_Name : Node_Id;
8093 Index : Node_Id;
8095 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
8096 -- If the bounds of the entry family being called depend on task
8097 -- discriminants, build a new index subtype where a discriminant is
8098 -- replaced with the value of the discriminant of the target task.
8099 -- The target task is the prefix of the entry name in the call.
8101 -----------------------
8102 -- Actual_Index_Type --
8103 -----------------------
8105 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
8106 Typ : constant Entity_Id := Entry_Index_Type (E);
8107 Tsk : constant Entity_Id := Scope (E);
8108 Lo : constant Node_Id := Type_Low_Bound (Typ);
8109 Hi : constant Node_Id := Type_High_Bound (Typ);
8110 New_T : Entity_Id;
8112 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8113 -- If the bound is given by a discriminant, replace with a reference
8114 -- to the discriminant of the same name in the target task. If the
8115 -- entry name is the target of a requeue statement and the entry is
8116 -- in the current protected object, the bound to be used is the
8117 -- discriminal of the object (see Apply_Range_Check for details of
8118 -- the transformation).
8120 -----------------------------
8121 -- Actual_Discriminant_Ref --
8122 -----------------------------
8124 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8125 Typ : constant Entity_Id := Etype (Bound);
8126 Ref : Node_Id;
8128 begin
8129 Remove_Side_Effects (Bound);
8131 if not Is_Entity_Name (Bound)
8132 or else Ekind (Entity (Bound)) /= E_Discriminant
8133 then
8134 return Bound;
8136 elsif Is_Protected_Type (Tsk)
8137 and then In_Open_Scopes (Tsk)
8138 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
8139 then
8140 -- Note: here Bound denotes a discriminant of the corresponding
8141 -- record type tskV, whose discriminal is a formal of the
8142 -- init-proc tskVIP. What we want is the body discriminal,
8143 -- which is associated to the discriminant of the original
8144 -- concurrent type tsk.
8146 return New_Occurrence_Of
8147 (Find_Body_Discriminal (Entity (Bound)), Loc);
8149 else
8150 Ref :=
8151 Make_Selected_Component (Loc,
8152 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8153 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8154 Analyze (Ref);
8155 Resolve (Ref, Typ);
8156 return Ref;
8157 end if;
8158 end Actual_Discriminant_Ref;
8160 -- Start of processing for Actual_Index_Type
8162 begin
8163 if not Has_Discriminants (Tsk)
8164 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8165 then
8166 return Entry_Index_Type (E);
8168 else
8169 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8170 Set_Etype (New_T, Base_Type (Typ));
8171 Set_Size_Info (New_T, Typ);
8172 Set_RM_Size (New_T, RM_Size (Typ));
8173 Set_Scalar_Range (New_T,
8174 Make_Range (Sloc (Entry_Name),
8175 Low_Bound => Actual_Discriminant_Ref (Lo),
8176 High_Bound => Actual_Discriminant_Ref (Hi)));
8178 return New_T;
8179 end if;
8180 end Actual_Index_Type;
8182 -- Start of processing for Resolve_Entry
8184 begin
8185 -- Find name of entry being called, and resolve prefix of name with its
8186 -- own type. The prefix can be overloaded, and the name and signature of
8187 -- the entry must be taken into account.
8189 if Nkind (Entry_Name) = N_Indexed_Component then
8191 -- Case of dealing with entry family within the current tasks
8193 E_Name := Prefix (Entry_Name);
8195 else
8196 E_Name := Entry_Name;
8197 end if;
8199 if Is_Entity_Name (E_Name) then
8201 -- Entry call to an entry (or entry family) in the current task. This
8202 -- is legal even though the task will deadlock. Rewrite as call to
8203 -- current task.
8205 -- This can also be a call to an entry in an enclosing task. If this
8206 -- is a single task, we have to retrieve its name, because the scope
8207 -- of the entry is the task type, not the object. If the enclosing
8208 -- task is a task type, the identity of the task is given by its own
8209 -- self variable.
8211 -- Finally this can be a requeue on an entry of the same task or
8212 -- protected object.
8214 S := Scope (Entity (E_Name));
8216 for J in reverse 0 .. Scope_Stack.Last loop
8217 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8218 and then not Comes_From_Source (S)
8219 then
8220 -- S is an enclosing task or protected object. The concurrent
8221 -- declaration has been converted into a type declaration, and
8222 -- the object itself has an object declaration that follows
8223 -- the type in the same declarative part.
8225 Tsk := Next_Entity (S);
8226 while Etype (Tsk) /= S loop
8227 Next_Entity (Tsk);
8228 end loop;
8230 S := Tsk;
8231 exit;
8233 elsif S = Scope_Stack.Table (J).Entity then
8235 -- Call to current task. Will be transformed into call to Self
8237 exit;
8239 end if;
8240 end loop;
8242 New_N :=
8243 Make_Selected_Component (Loc,
8244 Prefix => New_Occurrence_Of (S, Loc),
8245 Selector_Name =>
8246 New_Occurrence_Of (Entity (E_Name), Loc));
8247 Rewrite (E_Name, New_N);
8248 Analyze (E_Name);
8250 elsif Nkind (Entry_Name) = N_Selected_Component
8251 and then Is_Overloaded (Prefix (Entry_Name))
8252 then
8253 -- Use the entry name (which must be unique at this point) to find
8254 -- the prefix that returns the corresponding task/protected type.
8256 declare
8257 Pref : constant Node_Id := Prefix (Entry_Name);
8258 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8259 I : Interp_Index;
8260 It : Interp;
8262 begin
8263 Get_First_Interp (Pref, I, It);
8264 while Present (It.Typ) loop
8265 if Scope (Ent) = It.Typ then
8266 Set_Etype (Pref, It.Typ);
8267 exit;
8268 end if;
8270 Get_Next_Interp (I, It);
8271 end loop;
8272 end;
8273 end if;
8275 if Nkind (Entry_Name) = N_Selected_Component then
8276 Resolve (Prefix (Entry_Name));
8277 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8279 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8280 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8281 Resolve (Prefix (Prefix (Entry_Name)));
8282 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8284 -- We do not resolve the prefix because an Entry_Family has no type,
8285 -- although it has the semantics of an array since it can be indexed.
8286 -- In order to perform the associated range check, we would need to
8287 -- build an array type on the fly and set it on the prefix, but this
8288 -- would be wasteful since only the index type matters. Therefore we
8289 -- attach this index type directly, so that Actual_Index_Expression
8290 -- can pick it up later in order to generate the range check.
8292 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8294 Index := First (Expressions (Entry_Name));
8295 Resolve (Index, Entry_Index_Type (Nam));
8297 -- Generate a reference for the index when it denotes an entity
8299 if Is_Entity_Name (Index) then
8300 Generate_Reference (Entity (Index), Nam);
8301 end if;
8303 -- Up to this point the expression could have been the actual in a
8304 -- simple entry call, and be given by a named association.
8306 if Nkind (Index) = N_Parameter_Association then
8307 Error_Msg_N ("expect expression for entry index", Index);
8308 else
8309 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8310 end if;
8311 end if;
8312 end Resolve_Entry;
8314 ------------------------
8315 -- Resolve_Entry_Call --
8316 ------------------------
8318 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8319 Entry_Name : constant Node_Id := Name (N);
8320 Loc : constant Source_Ptr := Sloc (Entry_Name);
8322 Nam : Entity_Id;
8323 Norm_OK : Boolean;
8324 Obj : Node_Id;
8325 Was_Over : Boolean;
8327 begin
8328 -- We kill all checks here, because it does not seem worth the effort to
8329 -- do anything better, an entry call is a big operation.
8331 Kill_All_Checks;
8333 -- Processing of the name is similar for entry calls and protected
8334 -- operation calls. Once the entity is determined, we can complete
8335 -- the resolution of the actuals.
8337 -- The selector may be overloaded, in the case of a protected object
8338 -- with overloaded functions. The type of the context is used for
8339 -- resolution.
8341 if Nkind (Entry_Name) = N_Selected_Component
8342 and then Is_Overloaded (Selector_Name (Entry_Name))
8343 and then Typ /= Standard_Void_Type
8344 then
8345 declare
8346 I : Interp_Index;
8347 It : Interp;
8349 begin
8350 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8351 while Present (It.Typ) loop
8352 if Covers (Typ, It.Typ) then
8353 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8354 Set_Etype (Entry_Name, It.Typ);
8356 Generate_Reference (It.Typ, N, ' ');
8357 end if;
8359 Get_Next_Interp (I, It);
8360 end loop;
8361 end;
8362 end if;
8364 Resolve_Entry (Entry_Name);
8366 if Nkind (Entry_Name) = N_Selected_Component then
8368 -- Simple entry or protected operation call
8370 Nam := Entity (Selector_Name (Entry_Name));
8371 Obj := Prefix (Entry_Name);
8373 if Is_Subprogram (Nam) then
8374 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8375 end if;
8377 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8379 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8381 -- Call to member of entry family
8383 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8384 Obj := Prefix (Prefix (Entry_Name));
8385 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8386 end if;
8388 -- We cannot in general check the maximum depth of protected entry calls
8389 -- at compile time. But we can tell that any protected entry call at all
8390 -- violates a specified nesting depth of zero.
8392 if Is_Protected_Type (Scope (Nam)) then
8393 Check_Restriction (Max_Entry_Queue_Length, N);
8394 end if;
8396 -- Use context type to disambiguate a protected function that can be
8397 -- called without actuals and that returns an array type, and where the
8398 -- argument list may be an indexing of the returned value.
8400 if Ekind (Nam) = E_Function
8401 and then Needs_No_Actuals (Nam)
8402 and then Present (Parameter_Associations (N))
8403 and then
8404 ((Is_Array_Type (Etype (Nam))
8405 and then Covers (Typ, Component_Type (Etype (Nam))))
8407 or else (Is_Access_Type (Etype (Nam))
8408 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8409 and then
8410 Covers
8411 (Typ,
8412 Component_Type (Designated_Type (Etype (Nam))))))
8413 then
8414 declare
8415 Index_Node : Node_Id;
8417 begin
8418 Index_Node :=
8419 Make_Indexed_Component (Loc,
8420 Prefix =>
8421 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8422 Expressions => Parameter_Associations (N));
8424 -- Since we are correcting a node classification error made by the
8425 -- parser, we call Replace rather than Rewrite.
8427 Replace (N, Index_Node);
8428 Set_Etype (Prefix (N), Etype (Nam));
8429 Set_Etype (N, Typ);
8430 Resolve_Indexed_Component (N, Typ);
8431 return;
8432 end;
8433 end if;
8435 if Is_Entry (Nam)
8436 and then Present (Contract_Wrapper (Nam))
8437 and then Current_Scope /= Contract_Wrapper (Nam)
8438 and then Current_Scope /= Wrapped_Statements (Contract_Wrapper (Nam))
8439 then
8440 -- Note the entity being called before rewriting the call, so that
8441 -- it appears used at this point.
8443 Generate_Reference (Nam, Entry_Name, 'r');
8445 -- Rewrite as call to the precondition wrapper, adding the task
8446 -- object to the list of actuals. If the call is to a member of an
8447 -- entry family, include the index as well.
8449 declare
8450 New_Call : Node_Id;
8451 New_Actuals : List_Id;
8453 begin
8454 New_Actuals := New_List (Obj);
8456 if Nkind (Entry_Name) = N_Indexed_Component then
8457 Append_To (New_Actuals,
8458 New_Copy_Tree (First (Expressions (Entry_Name))));
8459 end if;
8461 Append_List (Parameter_Associations (N), New_Actuals);
8462 New_Call :=
8463 Make_Procedure_Call_Statement (Loc,
8464 Name =>
8465 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8466 Parameter_Associations => New_Actuals);
8467 Rewrite (N, New_Call);
8469 -- Preanalyze and resolve new call. Current procedure is called
8470 -- from Resolve_Call, after which expansion will take place.
8472 Preanalyze_And_Resolve (N);
8473 return;
8474 end;
8475 end if;
8477 -- The operation name may have been overloaded. Order the actuals
8478 -- according to the formals of the resolved entity, and set the return
8479 -- type to that of the operation.
8481 if Was_Over then
8482 Normalize_Actuals (N, Nam, False, Norm_OK);
8483 pragma Assert (Norm_OK);
8484 Set_Etype (N, Etype (Nam));
8486 -- Reset the Is_Overloaded flag, since resolution is now completed
8488 -- Simple entry call
8490 if Nkind (Entry_Name) = N_Selected_Component then
8491 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8493 -- Call to a member of an entry family
8495 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8496 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8497 end if;
8498 end if;
8500 Resolve_Actuals (N, Nam);
8501 Check_Internal_Protected_Use (N, Nam);
8503 -- Create a call reference to the entry
8505 Generate_Reference (Nam, Entry_Name, 's');
8507 if Is_Entry (Nam) then
8508 Check_Potentially_Blocking_Operation (N);
8509 end if;
8511 -- Verify that a procedure call cannot masquerade as an entry
8512 -- call where an entry call is expected.
8514 if Ekind (Nam) = E_Procedure then
8515 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8516 and then N = Entry_Call_Statement (Parent (N))
8517 then
8518 Error_Msg_N ("entry call required in select statement", N);
8520 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8521 and then N = Triggering_Statement (Parent (N))
8522 then
8523 Error_Msg_N ("triggering statement cannot be procedure call", N);
8525 elsif Ekind (Scope (Nam)) = E_Task_Type
8526 and then not In_Open_Scopes (Scope (Nam))
8527 then
8528 Error_Msg_N ("task has no entry with this name", Entry_Name);
8529 end if;
8530 end if;
8532 -- After resolution, entry calls and protected procedure calls are
8533 -- changed into entry calls, for expansion. The structure of the node
8534 -- does not change, so it can safely be done in place. Protected
8535 -- function calls must keep their structure because they are
8536 -- subexpressions.
8538 if Ekind (Nam) /= E_Function then
8540 -- A protected operation that is not a function may modify the
8541 -- corresponding object, and cannot apply to a constant. If this
8542 -- is an internal call, the prefix is the type itself.
8544 if Is_Protected_Type (Scope (Nam))
8545 and then not Is_Variable (Obj)
8546 and then (not Is_Entity_Name (Obj)
8547 or else not Is_Type (Entity (Obj)))
8548 then
8549 Error_Msg_N
8550 ("prefix of protected procedure or entry call must be variable",
8551 Entry_Name);
8552 end if;
8554 declare
8555 Entry_Call : Node_Id;
8557 begin
8558 Entry_Call :=
8559 Make_Entry_Call_Statement (Loc,
8560 Name => Entry_Name,
8561 Parameter_Associations => Parameter_Associations (N));
8563 -- Inherit relevant attributes from the original call
8565 Set_First_Named_Actual
8566 (Entry_Call, First_Named_Actual (N));
8568 Set_Is_Elaboration_Checks_OK_Node
8569 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8571 Set_Is_Elaboration_Warnings_OK_Node
8572 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8574 Set_Is_SPARK_Mode_On_Node
8575 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8577 Rewrite (N, Entry_Call);
8578 Set_Analyzed (N, True);
8579 end;
8581 -- Protected functions can return on the secondary stack, in which case
8582 -- we must trigger the transient scope mechanism.
8584 elsif Expander_Active
8585 and then Requires_Transient_Scope (Etype (Nam))
8586 then
8587 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
8588 end if;
8590 -- Now we know that this is not a call to a function that returns an
8591 -- array type; moreover, we know the name of the called entry. Detect
8592 -- overlapping actuals, just like for a subprogram call.
8594 Warn_On_Overlapping_Actuals (Nam, N);
8595 end Resolve_Entry_Call;
8597 -------------------------
8598 -- Resolve_Equality_Op --
8599 -------------------------
8601 -- The operands must have compatible types and the boolean context does not
8602 -- participate in the resolution. The first pass verifies that the operands
8603 -- are not ambiguous and sets their type correctly, or to Any_Type in case
8604 -- of ambiguity. If both operands are strings, aggregates, allocators, or
8605 -- null, they are ambiguous even if they carry a single (universal) type.
8607 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8608 L : constant Node_Id := Left_Opnd (N);
8609 R : constant Node_Id := Right_Opnd (N);
8611 Implicit_NE_For_User_Defined_Operator : constant Boolean :=
8612 Nkind (N) = N_Op_Ne
8613 and then Ekind (Entity (N)) = E_Function
8614 and then not Comes_From_Source (Entity (N))
8615 and then not
8616 Is_Intrinsic_Subprogram (Corresponding_Equality (Entity (N)));
8617 -- Whether this is a call to the implicit inequality operator created
8618 -- for a user-defined operator that is not an intrinsic subprogram, in
8619 -- which case we need to skip some processing.
8621 T : Entity_Id := Find_Unique_Type (L, R);
8623 procedure Check_Access_Attribute (N : Node_Id);
8624 -- For any object, '[Unchecked_]Access of such object can never be
8625 -- passed as an operand to the Universal_Access equality operators.
8626 -- This is so because the expected type for Obj'Access in a call to
8627 -- these operators, whose formals are of type Universal_Access, is
8628 -- Universal_Access, and Universal_Access does not have a designated
8629 -- type. For more details, see RM 3.10.2(2/2) and 6.4.1(3).
8631 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id);
8632 -- Check RM 4.5.2(9.6/2) on the given designated object types
8634 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id);
8635 -- Check RM 4.5.2(9.7/2) on the given designated subprogram types
8637 procedure Check_If_Expression (Cond : Node_Id);
8638 -- The resolution rule for if expressions requires that each such must
8639 -- have a unique type. This means that if several dependent expressions
8640 -- are of a non-null anonymous access type, and the context does not
8641 -- impose an expected type (as can be the case in an equality operation)
8642 -- the expression must be rejected.
8644 procedure Explain_Redundancy (N : Node_Id);
8645 -- Attempt to explain the nature of a redundant comparison with True. If
8646 -- the expression N is too complex, this routine issues a general error
8647 -- message.
8649 function Find_Unique_Access_Type return Entity_Id;
8650 -- In the case of allocators and access attributes, the context must
8651 -- provide an indication of the specific access type to be used. If
8652 -- one operand is of such a "generic" access type, check whether there
8653 -- is a specific visible access type that has the same designated type.
8654 -- This is semantically dubious, and of no interest to any real code,
8655 -- but c48008a makes it all worthwhile.
8657 function Suspicious_Prio_For_Equality return Boolean;
8658 -- Returns True iff the parent node is a and/or/xor operation that
8659 -- could be the cause of confused priorities. Note that if the not is
8660 -- in parens, then False is returned.
8662 ----------------------------
8663 -- Check_Access_Attribute --
8664 ----------------------------
8666 procedure Check_Access_Attribute (N : Node_Id) is
8667 begin
8668 if Nkind (N) = N_Attribute_Reference
8669 and then Attribute_Name (N) in Name_Access | Name_Unchecked_Access
8670 then
8671 Error_Msg_N
8672 ("access attribute cannot be used as actual for "
8673 & "universal_access equality", N);
8674 end if;
8675 end Check_Access_Attribute;
8677 -----------------------------------
8678 -- Check_Designated_Object_Types --
8679 -----------------------------------
8681 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id) is
8682 begin
8683 if (Is_Elementary_Type (T1) or else Is_Array_Type (T1))
8684 and then (Base_Type (T1) /= Base_Type (T2)
8685 or else not Subtypes_Statically_Match (T1, T2))
8686 then
8687 Error_Msg_N
8688 ("designated subtypes for universal_access equality "
8689 & "do not statically match (RM 4.5.2(9.6/2)", N);
8690 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8691 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8692 end if;
8693 end Check_Designated_Object_Types;
8695 ---------------------------------------
8696 -- Check_Designated_Subprogram_Types --
8697 ---------------------------------------
8699 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id) is
8700 begin
8701 if not Subtype_Conformant (T1, T2) then
8702 Error_Msg_N
8703 ("designated subtypes for universal_access equality "
8704 & "not subtype conformant (RM 4.5.2(9.7/2)", N);
8705 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8706 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8707 end if;
8708 end Check_Designated_Subprogram_Types;
8710 -------------------------
8711 -- Check_If_Expression --
8712 -------------------------
8714 procedure Check_If_Expression (Cond : Node_Id) is
8715 Then_Expr : Node_Id;
8716 Else_Expr : Node_Id;
8718 begin
8719 if Nkind (Cond) = N_If_Expression then
8720 Then_Expr := Next (First (Expressions (Cond)));
8721 Else_Expr := Next (Then_Expr);
8723 if Nkind (Then_Expr) /= N_Null
8724 and then Nkind (Else_Expr) /= N_Null
8725 then
8726 Error_Msg_N ("cannot determine type of if expression", Cond);
8727 end if;
8728 end if;
8729 end Check_If_Expression;
8731 ------------------------
8732 -- Explain_Redundancy --
8733 ------------------------
8735 procedure Explain_Redundancy (N : Node_Id) is
8736 Error : Name_Id;
8737 Val : Node_Id;
8738 Val_Id : Entity_Id;
8740 begin
8741 Val := N;
8743 -- Strip the operand down to an entity
8745 loop
8746 if Nkind (Val) = N_Selected_Component then
8747 Val := Selector_Name (Val);
8748 else
8749 exit;
8750 end if;
8751 end loop;
8753 -- The construct denotes an entity
8755 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8756 Val_Id := Entity (Val);
8758 -- Do not generate an error message when the comparison is done
8759 -- against the enumeration literal Standard.True.
8761 if Ekind (Val_Id) /= E_Enumeration_Literal then
8763 -- Build a customized error message
8765 Name_Len := 0;
8766 Add_Str_To_Name_Buffer ("?r?");
8768 if Ekind (Val_Id) = E_Component then
8769 Add_Str_To_Name_Buffer ("component ");
8771 elsif Ekind (Val_Id) = E_Constant then
8772 Add_Str_To_Name_Buffer ("constant ");
8774 elsif Ekind (Val_Id) = E_Discriminant then
8775 Add_Str_To_Name_Buffer ("discriminant ");
8777 elsif Is_Formal (Val_Id) then
8778 Add_Str_To_Name_Buffer ("parameter ");
8780 elsif Ekind (Val_Id) = E_Variable then
8781 Add_Str_To_Name_Buffer ("variable ");
8782 end if;
8784 Add_Str_To_Name_Buffer ("& is always True!");
8785 Error := Name_Find;
8787 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8788 end if;
8790 -- The construct is too complex to disect, issue a general message
8792 else
8793 Error_Msg_N ("?r?expression is always True!", Val);
8794 end if;
8795 end Explain_Redundancy;
8797 -----------------------------
8798 -- Find_Unique_Access_Type --
8799 -----------------------------
8801 function Find_Unique_Access_Type return Entity_Id is
8802 Acc : Entity_Id;
8803 E : Entity_Id;
8804 S : Entity_Id;
8806 begin
8807 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8808 then
8809 Acc := Designated_Type (Etype (R));
8811 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8812 then
8813 Acc := Designated_Type (Etype (L));
8814 else
8815 return Empty;
8816 end if;
8818 S := Current_Scope;
8819 while S /= Standard_Standard loop
8820 E := First_Entity (S);
8821 while Present (E) loop
8822 if Is_Type (E)
8823 and then Is_Access_Type (E)
8824 and then Ekind (E) /= E_Allocator_Type
8825 and then Designated_Type (E) = Base_Type (Acc)
8826 then
8827 return E;
8828 end if;
8830 Next_Entity (E);
8831 end loop;
8833 S := Scope (S);
8834 end loop;
8836 return Empty;
8837 end Find_Unique_Access_Type;
8839 ----------------------------------
8840 -- Suspicious_Prio_For_Equality --
8841 ----------------------------------
8843 function Suspicious_Prio_For_Equality return Boolean is
8844 Par : constant Node_Id := Parent (N);
8846 begin
8847 -- Check if parent node is one of and/or/xor, not parenthesized
8848 -- explicitly, and its own parent is not of this kind. Otherwise,
8849 -- it's a case of chained Boolean conditions which is likely well
8850 -- parenthesized.
8852 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8853 and then Paren_Count (N) = 0
8854 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8855 then
8856 declare
8857 Compar : Node_Id :=
8858 (if Left_Opnd (Par) = N then
8859 Right_Opnd (Par)
8860 else
8861 Left_Opnd (Par));
8862 begin
8863 -- Compar may have been rewritten, for example from (a /= b)
8864 -- into not (a = b). Use the Original_Node instead.
8866 Compar := Original_Node (Compar);
8868 -- If the other argument of the and/or/xor is also a
8869 -- comparison, or another and/or/xor then most likely
8870 -- the priorities are correctly set.
8872 return Nkind (Compar) not in N_Op_Boolean;
8873 end;
8875 else
8876 return False;
8877 end if;
8878 end Suspicious_Prio_For_Equality;
8880 -- Start of processing for Resolve_Equality_Op
8882 begin
8883 if T = Any_Fixed then
8884 T := Unique_Fixed_Point_Type (L);
8885 end if;
8887 Set_Etype (N, Base_Type (Typ));
8888 Generate_Reference (T, N, ' ');
8890 if T = Any_Type then
8891 -- Deal with explicit ambiguity of operands, unless this is a call
8892 -- to the implicit inequality operator created for a user-defined
8893 -- operator that is not an intrinsic subprogram, since the common
8894 -- resolution of operands done here does not apply to it.
8896 if not Implicit_NE_For_User_Defined_Operator
8897 and then (Is_Overloaded (L) or else Is_Overloaded (R))
8898 then
8899 Ambiguous_Operands (N);
8900 end if;
8902 else
8904 -- For Ada 2022, check for user-defined literals when the type has
8905 -- the appropriate aspect.
8907 if Has_Applicable_User_Defined_Literal (L, Etype (R)) then
8908 Resolve (L, Etype (R));
8909 Set_Etype (N, Standard_Boolean);
8910 end if;
8912 if Has_Applicable_User_Defined_Literal (R, Etype (L)) then
8913 Resolve (R, Etype (L));
8914 Set_Etype (N, Standard_Boolean);
8915 end if;
8917 -- Deal with other error cases
8919 if T = Any_String or else
8920 T = Any_Composite or else
8921 T = Any_Character
8922 then
8923 if T = Any_Character then
8924 Ambiguous_Character (L);
8925 else
8926 Error_Msg_N ("ambiguous operands for equality", N);
8927 end if;
8929 Set_Etype (N, Any_Type);
8930 return;
8932 elsif T = Universal_Access
8933 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
8934 then
8935 T := Find_Unique_Access_Type;
8937 if No (T) then
8938 Error_Msg_N ("ambiguous operands for equality", N);
8939 Set_Etype (N, Any_Type);
8940 return;
8941 end if;
8943 -- If expressions must have a single type, and if the context does
8944 -- not impose one the dependent expressions cannot be anonymous
8945 -- access types.
8947 -- Why no similar processing for case expressions???
8949 elsif Ada_Version >= Ada_2012
8950 and then Is_Anonymous_Access_Type (Etype (L))
8951 and then Is_Anonymous_Access_Type (Etype (R))
8952 then
8953 Check_If_Expression (L);
8954 Check_If_Expression (R);
8955 end if;
8957 -- RM 4.5.2(9.5/2): At least one of the operands of the equality
8958 -- operators for universal_access shall be of type universal_access,
8959 -- or both shall be of access-to-object types, or both shall be of
8960 -- access-to-subprogram types (RM 4.5.2(9.5/2)).
8962 if Is_Anonymous_Access_Type (T)
8963 and then Etype (L) /= Universal_Access
8964 and then Etype (R) /= Universal_Access
8965 then
8966 -- RM 4.5.2(9.6/2): When both are of access-to-object types, the
8967 -- designated types shall be the same or one shall cover the other
8968 -- and if the designated types are elementary or array types, then
8969 -- the designated subtypes shall statically match.
8971 if Is_Access_Object_Type (Etype (L))
8972 and then Is_Access_Object_Type (Etype (R))
8973 then
8974 Check_Designated_Object_Types
8975 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
8977 -- RM 4.5.2(9.7/2): When both are of access-to-subprogram types,
8978 -- the designated profiles shall be subtype conformant.
8980 elsif Is_Access_Subprogram_Type (Etype (L))
8981 and then Is_Access_Subprogram_Type (Etype (R))
8982 then
8983 Check_Designated_Subprogram_Types
8984 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
8985 end if;
8986 end if;
8988 -- Check another case of equality operators for universal_access
8990 if Is_Anonymous_Access_Type (T) and then Comes_From_Source (N) then
8991 Check_Access_Attribute (L);
8992 Check_Access_Attribute (R);
8993 end if;
8995 Resolve (L, T);
8996 Resolve (R, T);
8998 -- AI12-0413: user-defined primitive equality of an untagged record
8999 -- type hides the predefined equality operator, including within a
9000 -- generic, and if it is declared abstract, results in an illegal
9001 -- instance if the operator is used in the spec, or in the raising
9002 -- of Program_Error if used in the body of an instance.
9004 if Nkind (N) = N_Op_Eq
9005 and then In_Instance
9006 and then Ada_Version >= Ada_2012
9007 then
9008 declare
9009 U : constant Entity_Id := Underlying_Type (T);
9011 Eq : Entity_Id;
9013 begin
9014 if Present (U)
9015 and then Is_Record_Type (U)
9016 and then not Is_Tagged_Type (U)
9017 then
9018 Eq := Get_User_Defined_Equality (T);
9020 if Present (Eq) then
9021 if Is_Abstract_Subprogram (Eq) then
9022 Nondispatching_Call_To_Abstract_Operation (N, Eq);
9023 else
9024 Rewrite_Operator_As_Call (N, Eq);
9025 end if;
9027 return;
9028 end if;
9029 end if;
9030 end;
9031 end if;
9033 -- If the unique type is a class-wide type then it will be expanded
9034 -- into a dispatching call to the predefined primitive. Therefore we
9035 -- check here for potential violation of such restriction.
9037 if Is_Class_Wide_Type (T) then
9038 Check_Restriction (No_Dispatching_Calls, N);
9039 end if;
9041 -- Only warn for redundant equality comparison to True for objects
9042 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
9043 -- other expressions, it may be a matter of preference to write
9044 -- "Expr = True" or "Expr".
9046 if Warn_On_Redundant_Constructs
9047 and then Comes_From_Source (N)
9048 and then Comes_From_Source (R)
9049 and then Is_Entity_Name (R)
9050 and then Entity (R) = Standard_True
9051 and then
9052 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
9053 or else
9054 Nkind (L) in N_Op)
9055 then
9056 Error_Msg_N -- CODEFIX
9057 ("?r?comparison with True is redundant!", N);
9058 Explain_Redundancy (Original_Node (R));
9059 end if;
9061 -- Warn on a (in)equality between boolean values which is not
9062 -- parenthesized when the parent expression is one of and/or/xor, as
9063 -- this is interpreted as (a = b) op c where most likely a = (b op c)
9064 -- was intended. Do not generate a warning in generic instances, as
9065 -- the problematic expression may be implicitly parenthesized in
9066 -- the generic itself if one of the operators is a generic formal.
9067 -- Also do not generate a warning for generated equality, for
9068 -- example from rewritting a membership test.
9070 if Warn_On_Questionable_Missing_Parens
9071 and then not In_Instance
9072 and then Comes_From_Source (N)
9073 and then Is_Boolean_Type (T)
9074 and then Suspicious_Prio_For_Equality
9075 then
9076 Error_Msg_N ("?q?equality should be parenthesized here!", N);
9077 end if;
9079 Check_Unset_Reference (L);
9080 Check_Unset_Reference (R);
9081 Generate_Operator_Reference (N, T);
9082 Check_Low_Bound_Tested (N);
9084 -- Unless this is a call to the implicit inequality operator created
9085 -- for a user-defined operator that is not an intrinsic subprogram,
9086 -- try to fold the operation.
9088 if not Implicit_NE_For_User_Defined_Operator then
9089 Analyze_Dimension (N);
9090 Eval_Relational_Op (N);
9092 elsif Nkind (N) = N_Op_Ne
9093 and then Is_Abstract_Subprogram (Entity (N))
9094 then
9095 Nondispatching_Call_To_Abstract_Operation (N, Entity (N));
9096 end if;
9097 end if;
9098 end Resolve_Equality_Op;
9100 ----------------------------------
9101 -- Resolve_Explicit_Dereference --
9102 ----------------------------------
9104 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
9105 Loc : constant Source_Ptr := Sloc (N);
9106 New_N : Node_Id;
9107 P : constant Node_Id := Prefix (N);
9109 P_Typ : Entity_Id;
9110 -- The candidate prefix type, if overloaded
9112 I : Interp_Index;
9113 It : Interp;
9115 begin
9116 Check_Fully_Declared_Prefix (Typ, P);
9117 P_Typ := Empty;
9119 -- A useful optimization: check whether the dereference denotes an
9120 -- element of a container, and if so rewrite it as a call to the
9121 -- corresponding Element function.
9123 -- Disabled for now, on advice of ARG. A more restricted form of the
9124 -- predicate might be acceptable ???
9126 -- if Is_Container_Element (N) then
9127 -- return;
9128 -- end if;
9130 if Is_Overloaded (P) then
9132 -- Use the context type to select the prefix that has the correct
9133 -- designated type. Keep the first match, which will be the inner-
9134 -- most.
9136 Get_First_Interp (P, I, It);
9138 while Present (It.Typ) loop
9139 if Is_Access_Type (It.Typ)
9140 and then Covers (Typ, Designated_Type (It.Typ))
9141 then
9142 if No (P_Typ) then
9143 P_Typ := It.Typ;
9144 end if;
9146 -- Remove access types that do not match, but preserve access
9147 -- to subprogram interpretations, in case a further dereference
9148 -- is needed (see below).
9150 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
9151 Remove_Interp (I);
9152 end if;
9154 Get_Next_Interp (I, It);
9155 end loop;
9157 if Present (P_Typ) then
9158 Resolve (P, P_Typ);
9159 Set_Etype (N, Designated_Type (P_Typ));
9161 else
9162 -- If no interpretation covers the designated type of the prefix,
9163 -- this is the pathological case where not all implementations of
9164 -- the prefix allow the interpretation of the node as a call. Now
9165 -- that the expected type is known, Remove other interpretations
9166 -- from prefix, rewrite it as a call, and resolve again, so that
9167 -- the proper call node is generated.
9169 Get_First_Interp (P, I, It);
9170 while Present (It.Typ) loop
9171 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
9172 Remove_Interp (I);
9173 end if;
9175 Get_Next_Interp (I, It);
9176 end loop;
9178 New_N :=
9179 Make_Function_Call (Loc,
9180 Name =>
9181 Make_Explicit_Dereference (Loc,
9182 Prefix => P),
9183 Parameter_Associations => New_List);
9185 Save_Interps (N, New_N);
9186 Rewrite (N, New_N);
9187 Analyze_And_Resolve (N, Typ);
9188 return;
9189 end if;
9191 -- If not overloaded, resolve P with its own type
9193 else
9194 Resolve (P);
9195 end if;
9197 -- If the prefix might be null, add an access check
9199 if Is_Access_Type (Etype (P))
9200 and then not Can_Never_Be_Null (Etype (P))
9201 then
9202 Apply_Access_Check (N);
9203 end if;
9205 -- If the designated type is a packed unconstrained array type, and the
9206 -- explicit dereference is not in the context of an attribute reference,
9207 -- then we must compute and set the actual subtype, since it is needed
9208 -- by Gigi. The reason we exclude the attribute case is that this is
9209 -- handled fine by Gigi, and in fact we use such attributes to build the
9210 -- actual subtype. We also exclude generated code (which builds actual
9211 -- subtypes directly if they are needed).
9213 if Is_Packed_Array (Etype (N))
9214 and then not Is_Constrained (Etype (N))
9215 and then Nkind (Parent (N)) /= N_Attribute_Reference
9216 and then Comes_From_Source (N)
9217 then
9218 Set_Etype (N, Get_Actual_Subtype (N));
9219 end if;
9221 Analyze_Dimension (N);
9223 -- Note: No Eval processing is required for an explicit dereference,
9224 -- because such a name can never be static.
9226 end Resolve_Explicit_Dereference;
9228 -------------------------------------
9229 -- Resolve_Expression_With_Actions --
9230 -------------------------------------
9232 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9234 function OK_For_Static (Act : Node_Id) return Boolean;
9235 -- True if Act is an action of a declare_expression that is allowed in a
9236 -- static declare_expression.
9238 function All_OK_For_Static return Boolean;
9239 -- True if all actions of N are allowed in a static declare_expression.
9241 function Get_Literal (Expr : Node_Id) return Node_Id;
9242 -- Expr is an expression with compile-time-known value. This returns the
9243 -- literal node that reprsents that value.
9245 -------------------
9246 -- OK_For_Static --
9247 -------------------
9249 function OK_For_Static (Act : Node_Id) return Boolean is
9250 begin
9251 case Nkind (Act) is
9252 when N_Object_Declaration =>
9253 if Constant_Present (Act)
9254 and then Is_Static_Expression (Expression (Act))
9255 then
9256 return True;
9257 end if;
9259 when N_Object_Renaming_Declaration =>
9260 if Statically_Names_Object (Name (Act)) then
9261 return True;
9262 end if;
9264 when others =>
9265 -- No other declarations, nor even pragmas, are allowed in a
9266 -- declare expression, so if we see something else, it must be
9267 -- an internally generated expression_with_actions.
9268 null;
9269 end case;
9271 return False;
9272 end OK_For_Static;
9274 -----------------------
9275 -- All_OK_For_Static --
9276 -----------------------
9278 function All_OK_For_Static return Boolean is
9279 Act : Node_Id := First (Actions (N));
9280 begin
9281 while Present (Act) loop
9282 if not OK_For_Static (Act) then
9283 return False;
9284 end if;
9286 Next (Act);
9287 end loop;
9289 return True;
9290 end All_OK_For_Static;
9292 -----------------
9293 -- Get_Literal --
9294 -----------------
9296 function Get_Literal (Expr : Node_Id) return Node_Id is
9297 pragma Assert (Compile_Time_Known_Value (Expr));
9298 Result : Node_Id;
9299 begin
9300 case Nkind (Expr) is
9301 when N_Has_Entity =>
9302 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9303 Result := Expr;
9304 else
9305 Result := Constant_Value (Entity (Expr));
9306 end if;
9307 when N_Numeric_Or_String_Literal =>
9308 Result := Expr;
9309 when others =>
9310 raise Program_Error;
9311 end case;
9313 pragma Assert
9314 (Nkind (Result) in N_Numeric_Or_String_Literal
9315 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9316 return Result;
9317 end Get_Literal;
9319 -- Local variables
9321 Loc : constant Source_Ptr := Sloc (N);
9323 -- Start of processing for Resolve_Expression_With_Actions
9325 begin
9326 Set_Etype (N, Typ);
9328 if Is_Empty_List (Actions (N)) then
9329 pragma Assert (All_OK_For_Static); null;
9330 end if;
9332 -- If the value of the expression is known at compile time, and all
9333 -- of the actions (if any) are suitable, then replace the declare
9334 -- expression with its expression. This allows the declare expression
9335 -- as a whole to be static if appropriate. See AI12-0368.
9337 if Compile_Time_Known_Value (Expression (N)) then
9338 if Is_Empty_List (Actions (N)) then
9339 Rewrite (N, Expression (N));
9340 elsif All_OK_For_Static then
9341 Rewrite
9342 (N, New_Copy_Tree
9343 (Get_Literal (Expression (N)), New_Sloc => Loc));
9344 end if;
9345 end if;
9346 end Resolve_Expression_With_Actions;
9348 ----------------------------------
9349 -- Resolve_Generalized_Indexing --
9350 ----------------------------------
9352 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9353 Indexing : constant Node_Id := Generalized_Indexing (N);
9354 begin
9355 Rewrite (N, Indexing);
9356 Resolve (N, Typ);
9357 end Resolve_Generalized_Indexing;
9359 ---------------------------
9360 -- Resolve_If_Expression --
9361 ---------------------------
9363 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9364 Condition : constant Node_Id := First (Expressions (N));
9366 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id);
9367 -- When a dependent expression is of a subtype different from
9368 -- the context subtype, then insert a qualification to ensure
9369 -- the generation of a constraint check. This was previously
9370 -- for scalar types. For array types apply a length check, given
9371 -- that the context in general allows sliding, while a qualified
9372 -- expression forces equality of bounds.
9374 -----------------
9375 -- Apply_Check --
9376 -----------------
9378 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id) is
9379 Expr_Typ : constant Entity_Id := Etype (Expr);
9380 Loc : constant Source_Ptr := Sloc (Expr);
9382 begin
9383 if Expr_Typ = Typ
9384 or else Is_Tagged_Type (Typ)
9385 or else Is_Access_Type (Typ)
9386 or else not Is_Constrained (Typ)
9387 or else Inside_A_Generic
9388 then
9389 null;
9391 elsif Is_Array_Type (Typ) then
9392 Apply_Length_Check (Expr, Typ);
9394 else
9395 Rewrite (Expr,
9396 Make_Qualified_Expression (Loc,
9397 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9398 Expression => Relocate_Node (Expr)));
9400 Analyze_And_Resolve (Expr, Result_Type);
9401 end if;
9402 end Apply_Check;
9404 -- Local variables
9406 Else_Expr : Node_Id;
9407 Then_Expr : Node_Id;
9409 Result_Type : Entity_Id;
9410 -- So in most cases the type of the if_expression and of its
9411 -- dependent expressions is that of the context. However, if
9412 -- the expression is the index of an Indexed_Component, we must
9413 -- ensure that a proper index check is applied, rather than a
9414 -- range check on the index type (which might be discriminant
9415 -- dependent). In this case we resolve with the base type of the
9416 -- index type, and the index check is generated in the resolution
9417 -- of the indexed_component above.
9419 -- Start of processing for Resolve_If_Expression
9421 begin
9422 -- Defend against malformed expressions
9424 if No (Condition) then
9425 return;
9426 end if;
9428 if Present (Parent (N))
9429 and then (Nkind (Parent (N)) = N_Indexed_Component
9430 or else Nkind (Parent (Parent (N))) = N_Indexed_Component)
9431 then
9432 Result_Type := Base_Type (Typ);
9434 else
9435 Result_Type := Typ;
9436 end if;
9438 Then_Expr := Next (Condition);
9440 if No (Then_Expr) then
9441 return;
9442 end if;
9444 Resolve (Condition, Any_Boolean);
9445 Check_Unset_Reference (Condition);
9447 Resolve_Dependent_Expression (N, Then_Expr, Result_Type);
9449 Check_Unset_Reference (Then_Expr);
9450 Apply_Check (Then_Expr, Result_Type);
9452 Else_Expr := Next (Then_Expr);
9454 -- If ELSE expression present, just resolve using the determined type
9456 if Present (Else_Expr) then
9457 Resolve_Dependent_Expression (N, Else_Expr, Result_Type);
9459 Check_Unset_Reference (Else_Expr);
9460 Apply_Check (Else_Expr, Result_Type);
9462 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9463 -- dynamically tagged must be known statically.
9465 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9466 if Is_Dynamically_Tagged (Then_Expr) /=
9467 Is_Dynamically_Tagged (Else_Expr)
9468 then
9469 Error_Msg_N ("all or none of the dependent expressions "
9470 & "can be dynamically tagged", N);
9471 end if;
9472 end if;
9474 -- If no ELSE expression is present, root type must be Standard.Boolean
9475 -- and we provide a Standard.True result converted to the appropriate
9476 -- Boolean type (in case it is a derived boolean type).
9478 elsif Root_Type (Typ) = Standard_Boolean then
9479 Else_Expr :=
9480 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9481 Analyze_And_Resolve (Else_Expr, Result_Type);
9482 Append_To (Expressions (N), Else_Expr);
9484 else
9485 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9486 Append_To (Expressions (N), Error);
9487 end if;
9489 Set_Etype (N, Result_Type);
9491 if not Error_Posted (N) then
9492 Eval_If_Expression (N);
9493 end if;
9495 Analyze_Dimension (N);
9496 end Resolve_If_Expression;
9498 ----------------------------------
9499 -- Resolve_Implicit_Dereference --
9500 ----------------------------------
9502 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9503 Desig_Typ : Entity_Id;
9505 begin
9506 -- In an instance the proper view may not always be correct for
9507 -- private types, see e.g. Sem_Type.Covers for similar handling.
9509 if Is_Private_Type (Etype (P))
9510 and then Present (Full_View (Etype (P)))
9511 and then Is_Access_Type (Full_View (Etype (P)))
9512 and then In_Instance
9513 then
9514 Set_Etype (P, Full_View (Etype (P)));
9515 end if;
9517 if Is_Access_Type (Etype (P)) then
9518 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9519 Insert_Explicit_Dereference (P);
9520 Analyze_And_Resolve (P, Desig_Typ);
9521 end if;
9522 end Resolve_Implicit_Dereference;
9524 -------------------------------
9525 -- Resolve_Indexed_Component --
9526 -------------------------------
9528 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9529 Pref : constant Node_Id := Prefix (N);
9530 Expr : Node_Id;
9531 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9532 Index : Node_Id;
9534 begin
9535 if Present (Generalized_Indexing (N)) then
9536 Resolve_Generalized_Indexing (N, Typ);
9537 return;
9538 end if;
9540 if Is_Overloaded (Pref) then
9542 -- Use the context type to select the prefix that yields the correct
9543 -- component type.
9545 declare
9546 I : Interp_Index;
9547 It : Interp;
9548 I1 : Interp_Index := 0;
9549 Found : Boolean := False;
9551 begin
9552 Get_First_Interp (Pref, I, It);
9553 while Present (It.Typ) loop
9554 if (Is_Array_Type (It.Typ)
9555 and then Covers (Typ, Component_Type (It.Typ)))
9556 or else (Is_Access_Type (It.Typ)
9557 and then Is_Array_Type (Designated_Type (It.Typ))
9558 and then
9559 Covers
9560 (Typ,
9561 Component_Type (Designated_Type (It.Typ))))
9562 then
9563 if Found then
9564 It := Disambiguate (Pref, I1, I, Any_Type);
9566 if It = No_Interp then
9567 Error_Msg_N ("ambiguous prefix for indexing", N);
9568 Set_Etype (N, Typ);
9569 return;
9571 else
9572 Found := True;
9573 Array_Type := It.Typ;
9574 I1 := I;
9575 end if;
9577 else
9578 Found := True;
9579 Array_Type := It.Typ;
9580 I1 := I;
9581 end if;
9582 end if;
9584 Get_Next_Interp (I, It);
9585 end loop;
9586 end;
9588 else
9589 Array_Type := Etype (Pref);
9590 end if;
9592 Resolve (Pref, Array_Type);
9593 Array_Type := Get_Actual_Subtype_If_Available (Pref);
9595 -- If the prefix's type is an access type, get to the real array type.
9596 -- Note: we do not apply an access check because an explicit dereference
9597 -- will be introduced later, and the check will happen there.
9599 if Is_Access_Type (Array_Type) then
9600 Array_Type := Implicitly_Designated_Type (Array_Type);
9601 end if;
9603 -- If name was overloaded, set component type correctly now.
9604 -- If a misplaced call to an entry family (which has no index types)
9605 -- return. Error will be diagnosed from calling context.
9607 if Is_Array_Type (Array_Type) then
9608 Set_Etype (N, Component_Type (Array_Type));
9609 else
9610 return;
9611 end if;
9613 Index := First_Index (Array_Type);
9614 Expr := First (Expressions (N));
9616 -- The prefix may have resolved to a string literal, in which case its
9617 -- etype has a special representation. This is only possible currently
9618 -- if the prefix is a static concatenation, written in functional
9619 -- notation.
9621 if Ekind (Array_Type) = E_String_Literal_Subtype then
9622 Resolve (Expr, Standard_Positive);
9624 else
9625 while Present (Index) and then Present (Expr) loop
9626 Resolve (Expr, Etype (Index));
9627 Check_Unset_Reference (Expr);
9629 Apply_Scalar_Range_Check (Expr, Etype (Index));
9631 Next_Index (Index);
9632 Next (Expr);
9633 end loop;
9634 end if;
9636 Resolve_Implicit_Dereference (Pref);
9637 Analyze_Dimension (N);
9639 -- Do not generate the warning on suspicious index if we are analyzing
9640 -- package Ada.Tags; otherwise we will report the warning with the
9641 -- Prims_Ptr field of the dispatch table.
9643 if Scope (Etype (Pref)) = Standard_Standard
9644 or else not
9645 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Pref))), Ada_Tags)
9646 then
9647 Warn_On_Suspicious_Index (Pref, First (Expressions (N)));
9648 Eval_Indexed_Component (N);
9649 end if;
9651 -- If the array type is atomic and the component is not, then this is
9652 -- worth a warning before Ada 2022, since we have a situation where the
9653 -- access to the component may cause extra read/writes of the atomic
9654 -- object, or partial word accesses, both of which may be unexpected.
9656 if Nkind (N) = N_Indexed_Component
9657 and then Is_Atomic_Ref_With_Address (N)
9658 and then not (Has_Atomic_Components (Array_Type)
9659 or else (Is_Entity_Name (Pref)
9660 and then Has_Atomic_Components
9661 (Entity (Pref))))
9662 and then not Is_Atomic (Component_Type (Array_Type))
9663 and then Ada_Version < Ada_2022
9664 then
9665 Error_Msg_N
9666 ("??access to non-atomic component of atomic array", Pref);
9667 Error_Msg_N
9668 ("??\may cause unexpected accesses to atomic object", Pref);
9669 end if;
9670 end Resolve_Indexed_Component;
9672 -----------------------------
9673 -- Resolve_Integer_Literal --
9674 -----------------------------
9676 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9677 begin
9678 Set_Etype (N, Typ);
9679 Eval_Integer_Literal (N);
9680 end Resolve_Integer_Literal;
9682 -----------------------------------------
9683 -- Resolve_Interpolated_String_Literal --
9684 -----------------------------------------
9686 procedure Resolve_Interpolated_String_Literal (N : Node_Id; Typ : Entity_Id)
9688 Str_Elem : Node_Id;
9690 begin
9691 Str_Elem := First (Expressions (N));
9692 pragma Assert (Nkind (Str_Elem) = N_String_Literal);
9694 while Present (Str_Elem) loop
9696 -- Resolve string elements using the context type; for interpolated
9697 -- expressions there is no need to check if their type has a suitable
9698 -- image function because under Ada 2022 all the types have such
9699 -- function available.
9701 if Etype (Str_Elem) = Any_String then
9702 Resolve (Str_Elem, Typ);
9703 end if;
9705 Next (Str_Elem);
9706 end loop;
9708 Set_Etype (N, Typ);
9709 end Resolve_Interpolated_String_Literal;
9711 --------------------------------
9712 -- Resolve_Intrinsic_Operator --
9713 --------------------------------
9715 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9716 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9717 Op : Entity_Id;
9718 Arg1 : Node_Id;
9719 Arg2 : Node_Id;
9721 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9722 -- If the operand is a literal, it cannot be the expression in a
9723 -- conversion. Use a qualified expression instead.
9725 ---------------------
9726 -- Convert_Operand --
9727 ---------------------
9729 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9730 Loc : constant Source_Ptr := Sloc (Opnd);
9731 Res : Node_Id;
9733 begin
9734 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9735 Res :=
9736 Make_Qualified_Expression (Loc,
9737 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9738 Expression => Relocate_Node (Opnd));
9739 Analyze (Res);
9741 else
9742 Res := Unchecked_Convert_To (Btyp, Opnd);
9743 end if;
9745 return Res;
9746 end Convert_Operand;
9748 -- Start of processing for Resolve_Intrinsic_Operator
9750 begin
9751 -- We must preserve the original entity in a generic setting, so that
9752 -- the legality of the operation can be verified in an instance.
9754 if not Expander_Active then
9755 return;
9756 end if;
9758 Op := Entity (N);
9759 while Scope (Op) /= Standard_Standard loop
9760 Op := Homonym (Op);
9761 pragma Assert (Present (Op));
9762 end loop;
9764 Set_Entity (N, Op);
9765 Set_Is_Overloaded (N, False);
9767 -- If the result or operand types are private, rewrite with unchecked
9768 -- conversions on the operands and the result, to expose the proper
9769 -- underlying numeric type.
9771 if Is_Private_Type (Typ)
9772 or else Is_Private_Type (Etype (Left_Opnd (N)))
9773 or else Is_Private_Type (Etype (Right_Opnd (N)))
9774 then
9775 Arg1 := Convert_Operand (Left_Opnd (N));
9777 if Nkind (N) = N_Op_Expon then
9778 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9779 else
9780 Arg2 := Convert_Operand (Right_Opnd (N));
9781 end if;
9783 if Nkind (Arg1) = N_Type_Conversion then
9784 Save_Interps (Left_Opnd (N), Expression (Arg1));
9785 end if;
9787 if Nkind (Arg2) = N_Type_Conversion then
9788 Save_Interps (Right_Opnd (N), Expression (Arg2));
9789 end if;
9791 Set_Left_Opnd (N, Arg1);
9792 Set_Right_Opnd (N, Arg2);
9794 Set_Etype (N, Btyp);
9795 Rewrite (N, Unchecked_Convert_To (Typ, N));
9796 Resolve (N, Typ);
9798 elsif Typ /= Etype (Left_Opnd (N))
9799 or else Typ /= Etype (Right_Opnd (N))
9800 then
9801 -- Add explicit conversion where needed, and save interpretations in
9802 -- case operands are overloaded.
9804 Arg1 := Convert_To (Typ, Left_Opnd (N));
9805 Arg2 := Convert_To (Typ, Right_Opnd (N));
9807 if Nkind (Arg1) = N_Type_Conversion then
9808 Save_Interps (Left_Opnd (N), Expression (Arg1));
9809 else
9810 Save_Interps (Left_Opnd (N), Arg1);
9811 end if;
9813 if Nkind (Arg2) = N_Type_Conversion then
9814 Save_Interps (Right_Opnd (N), Expression (Arg2));
9815 else
9816 Save_Interps (Right_Opnd (N), Arg2);
9817 end if;
9819 Rewrite (Left_Opnd (N), Arg1);
9820 Rewrite (Right_Opnd (N), Arg2);
9821 Analyze (Arg1);
9822 Analyze (Arg2);
9823 Resolve_Arithmetic_Op (N, Typ);
9825 else
9826 Resolve_Arithmetic_Op (N, Typ);
9827 end if;
9828 end Resolve_Intrinsic_Operator;
9830 --------------------------------------
9831 -- Resolve_Intrinsic_Unary_Operator --
9832 --------------------------------------
9834 procedure Resolve_Intrinsic_Unary_Operator
9835 (N : Node_Id;
9836 Typ : Entity_Id)
9838 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9839 Op : Entity_Id;
9840 Arg2 : Node_Id;
9842 begin
9843 Op := Entity (N);
9844 while Scope (Op) /= Standard_Standard loop
9845 Op := Homonym (Op);
9846 pragma Assert (Present (Op));
9847 end loop;
9849 Set_Entity (N, Op);
9851 if Is_Private_Type (Typ) then
9852 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9853 Save_Interps (Right_Opnd (N), Expression (Arg2));
9855 Set_Right_Opnd (N, Arg2);
9857 Set_Etype (N, Btyp);
9858 Rewrite (N, Unchecked_Convert_To (Typ, N));
9859 Resolve (N, Typ);
9861 else
9862 Resolve_Unary_Op (N, Typ);
9863 end if;
9864 end Resolve_Intrinsic_Unary_Operator;
9866 ------------------------
9867 -- Resolve_Logical_Op --
9868 ------------------------
9870 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9871 B_Typ : Entity_Id;
9873 begin
9874 Check_No_Direct_Boolean_Operators (N);
9876 -- Predefined operations on scalar types yield the base type. On the
9877 -- other hand, logical operations on arrays yield the type of the
9878 -- arguments (and the context).
9880 if Is_Array_Type (Typ) then
9881 B_Typ := Typ;
9882 else
9883 B_Typ := Base_Type (Typ);
9884 end if;
9886 -- The following test is required because the operands of the operation
9887 -- may be literals, in which case the resulting type appears to be
9888 -- compatible with a signed integer type, when in fact it is compatible
9889 -- only with modular types. If the context itself is universal, the
9890 -- operation is illegal.
9892 if not Valid_Boolean_Arg (Typ) then
9893 Error_Msg_N ("invalid context for logical operation", N);
9894 Set_Etype (N, Any_Type);
9895 return;
9897 elsif Typ = Any_Modular then
9898 Error_Msg_N
9899 ("no modular type available in this context", N);
9900 Set_Etype (N, Any_Type);
9901 return;
9903 elsif Is_Modular_Integer_Type (Typ)
9904 and then Etype (Left_Opnd (N)) = Universal_Integer
9905 and then Etype (Right_Opnd (N)) = Universal_Integer
9906 then
9907 Check_For_Visible_Operator (N, B_Typ);
9908 end if;
9910 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9911 -- is active and the result type is standard Boolean (do not mess with
9912 -- ops that return a nonstandard Boolean type, because something strange
9913 -- is going on).
9915 -- Note: you might expect this replacement to be done during expansion,
9916 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9917 -- is used, no part of the right operand of an "and" or "or" operator
9918 -- should be executed if the left operand would short-circuit the
9919 -- evaluation of the corresponding "and then" or "or else". If we left
9920 -- the replacement to expansion time, then run-time checks associated
9921 -- with such operands would be evaluated unconditionally, due to being
9922 -- before the condition prior to the rewriting as short-circuit forms
9923 -- during expansion.
9925 if Short_Circuit_And_Or
9926 and then B_Typ = Standard_Boolean
9927 and then Nkind (N) in N_Op_And | N_Op_Or
9928 then
9929 -- Mark the corresponding putative SCO operator as truly a logical
9930 -- (and short-circuit) operator.
9932 if Generate_SCO and then Comes_From_Source (N) then
9933 Set_SCO_Logical_Operator (N);
9934 end if;
9936 if Nkind (N) = N_Op_And then
9937 Rewrite (N,
9938 Make_And_Then (Sloc (N),
9939 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9940 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9941 Analyze_And_Resolve (N, B_Typ);
9943 -- Case of OR changed to OR ELSE
9945 else
9946 Rewrite (N,
9947 Make_Or_Else (Sloc (N),
9948 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9949 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9950 Analyze_And_Resolve (N, B_Typ);
9951 end if;
9953 -- Return now, since analysis of the rewritten ops will take care of
9954 -- other reference bookkeeping and expression folding.
9956 return;
9957 end if;
9959 Resolve (Left_Opnd (N), B_Typ);
9960 Resolve (Right_Opnd (N), B_Typ);
9962 Check_Unset_Reference (Left_Opnd (N));
9963 Check_Unset_Reference (Right_Opnd (N));
9965 Set_Etype (N, B_Typ);
9966 Generate_Operator_Reference (N, B_Typ);
9967 Eval_Logical_Op (N);
9968 end Resolve_Logical_Op;
9970 ---------------------------------
9971 -- Resolve_Membership_Equality --
9972 ---------------------------------
9974 procedure Resolve_Membership_Equality (N : Node_Id; Typ : Entity_Id) is
9975 Utyp : constant Entity_Id := Underlying_Type (Typ);
9977 begin
9978 -- RM 4.5.2(4.1/3): if the type is limited, then it shall have a visible
9979 -- primitive equality operator. This means that we can use the regular
9980 -- visibility-based resolution and reset Entity in order to trigger it.
9982 if Is_Limited_Type (Typ) then
9983 Set_Entity (N, Empty);
9985 -- RM 4.5.2(28.1/3): if the type is a record, then the membership test
9986 -- uses the primitive equality for the type [even if it is not visible].
9987 -- We only deal with the untagged case here, because the tagged case is
9988 -- handled uniformly in the expander.
9990 elsif Is_Record_Type (Utyp) and then not Is_Tagged_Type (Utyp) then
9991 declare
9992 Eq_Id : constant Entity_Id := Get_User_Defined_Equality (Typ);
9994 begin
9995 if Present (Eq_Id) then
9996 Rewrite_Operator_As_Call (N, Eq_Id);
9997 end if;
9998 end;
9999 end if;
10000 end Resolve_Membership_Equality;
10002 ---------------------------
10003 -- Resolve_Membership_Op --
10004 ---------------------------
10006 -- The context can only be a boolean type, and does not determine the
10007 -- arguments. Arguments should be unambiguous, but the preference rule for
10008 -- universal types applies.
10010 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
10011 pragma Assert (Is_Boolean_Type (Typ));
10013 L : constant Node_Id := Left_Opnd (N);
10014 R : constant Node_Id := Right_Opnd (N);
10015 T : Entity_Id;
10017 procedure Resolve_Set_Membership;
10018 -- Analysis has determined a unique type for the left operand. Use it as
10019 -- the basis to resolve the disjuncts.
10021 ----------------------------
10022 -- Resolve_Set_Membership --
10023 ----------------------------
10025 procedure Resolve_Set_Membership is
10026 Alt : Node_Id;
10028 begin
10029 -- If the left operand is overloaded, find type compatible with not
10030 -- overloaded alternative of the right operand.
10032 Alt := First (Alternatives (N));
10033 if Is_Overloaded (L) then
10034 T := Empty;
10035 while Present (Alt) loop
10036 if not Is_Overloaded (Alt) then
10037 T := Intersect_Types (L, Alt);
10038 exit;
10039 else
10040 Next (Alt);
10041 end if;
10042 end loop;
10044 -- Unclear how to resolve expression if all alternatives are also
10045 -- overloaded.
10047 if No (T) then
10048 Error_Msg_N ("ambiguous expression", N);
10049 end if;
10051 else
10052 T := Intersect_Types (L, Alt);
10053 end if;
10055 Resolve (L, T);
10057 Alt := First (Alternatives (N));
10058 while Present (Alt) loop
10060 -- Alternative is an expression, a range
10061 -- or a subtype mark.
10063 if not Is_Entity_Name (Alt)
10064 or else not Is_Type (Entity (Alt))
10065 then
10066 Resolve (Alt, T);
10067 end if;
10069 Next (Alt);
10070 end loop;
10072 -- Check for duplicates for discrete case
10074 if Is_Discrete_Type (T) then
10075 declare
10076 type Ent is record
10077 Alt : Node_Id;
10078 Val : Uint;
10079 end record;
10081 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
10082 Nalts : Nat;
10084 begin
10085 -- Loop checking duplicates. This is quadratic, but giant sets
10086 -- are unlikely in this context so it's a reasonable choice.
10088 Nalts := 0;
10089 Alt := First (Alternatives (N));
10090 while Present (Alt) loop
10091 if Is_OK_Static_Expression (Alt)
10092 and then Nkind (Alt) in N_Integer_Literal
10093 | N_Character_Literal
10094 | N_Has_Entity
10095 then
10096 Nalts := Nalts + 1;
10097 Alts (Nalts) := (Alt, Expr_Value (Alt));
10099 for J in 1 .. Nalts - 1 loop
10100 if Alts (J).Val = Alts (Nalts).Val then
10101 Error_Msg_Sloc := Sloc (Alts (J).Alt);
10102 Error_Msg_N ("duplicate of value given#??", Alt);
10103 end if;
10104 end loop;
10105 end if;
10107 Next (Alt);
10108 end loop;
10109 end;
10110 end if;
10112 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
10113 -- limited types, evaluation of a membership test uses the predefined
10114 -- equality for the type. This may be confusing to users, and the
10115 -- following warning appears useful for the most common case.
10117 if Is_Scalar_Type (Etype (L))
10118 and then Present (Get_User_Defined_Equality (Etype (L)))
10119 then
10120 Error_Msg_NE
10121 ("membership test on& uses predefined equality?", N, Etype (L));
10122 Error_Msg_N
10123 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
10124 end if;
10125 end Resolve_Set_Membership;
10127 -- Start of processing for Resolve_Membership_Op
10129 begin
10130 if L = Error or else R = Error then
10131 return;
10132 end if;
10134 if Present (Alternatives (N)) then
10135 Resolve_Set_Membership;
10136 goto SM_Exit;
10138 elsif not Is_Overloaded (R)
10139 and then Is_Universal_Numeric_Type (Etype (R))
10140 and then Is_Overloaded (L)
10141 then
10142 T := Etype (R);
10144 -- If the left operand is of a universal numeric type and the right
10145 -- operand is not, we do not resolve the operands to the tested type
10146 -- but to the universal type instead. If not conforming to the letter,
10147 -- it's conforming to the spirit of the specification of membership
10148 -- tests, which are typically used to guard a specific operation and
10149 -- ought not to fail a check in doing so. Without this, in the case of
10151 -- type Small_Length is range 1 .. 16;
10153 -- function Is_Small_String (S : String) return Boolean is
10154 -- begin
10155 -- return S'Length in Small_Length;
10156 -- end;
10158 -- the function Is_Small_String would fail a range check for strings
10159 -- larger than 127 characters.
10161 -- The test on the size is required in GNAT because universal_integer
10162 -- does not cover all the values of all the supported integer types,
10163 -- for example the large values of Long_Long_Long_Unsigned.
10165 elsif not Is_Overloaded (L)
10166 and then Is_Universal_Numeric_Type (Etype (L))
10167 and then (Is_Overloaded (R)
10168 or else
10169 (not Is_Universal_Numeric_Type (Etype (R))
10170 and then
10171 (not Is_Integer_Type (Etype (R))
10172 or else
10173 RM_Size (Etype (R)) < RM_Size (Universal_Integer))))
10174 then
10175 T := Etype (L);
10177 -- If the right operand is 'Range, we first need to resolve it (to
10178 -- the tested type) so that it is rewritten as an N_Range, before
10179 -- converting its bounds and resolving it again below.
10181 if Nkind (R) = N_Attribute_Reference
10182 and then Attribute_Name (R) = Name_Range
10183 then
10184 Resolve (R);
10185 end if;
10187 -- If the right operand is an N_Range, we convert its bounds to the
10188 -- universal type before resolving it.
10190 if Nkind (R) = N_Range then
10191 Rewrite (R,
10192 Make_Range (Sloc (R),
10193 Low_Bound => Convert_To (T, Low_Bound (R)),
10194 High_Bound => Convert_To (T, High_Bound (R))));
10195 Analyze (R);
10196 end if;
10198 -- Ada 2005 (AI-251): Support the following case:
10200 -- type I is interface;
10201 -- type T is tagged ...
10203 -- function Test (O : I'Class) is
10204 -- begin
10205 -- return O in T'Class.
10206 -- end Test;
10208 -- In this case we have nothing else to do. The membership test will be
10209 -- done at run time.
10211 elsif Ada_Version >= Ada_2005
10212 and then Is_Class_Wide_Type (Etype (L))
10213 and then Is_Interface (Etype (L))
10214 and then not Is_Interface (Etype (R))
10215 then
10216 return;
10218 else
10219 T := Intersect_Types (L, R);
10220 end if;
10222 -- If mixed-mode operations are present and operands are all literal,
10223 -- the only interpretation involves Duration, which is probably not
10224 -- the intention of the programmer.
10226 if T = Any_Fixed then
10227 T := Unique_Fixed_Point_Type (N);
10229 if T = Any_Type then
10230 return;
10231 end if;
10232 end if;
10234 Resolve (L, T);
10235 Check_Unset_Reference (L);
10237 if Nkind (R) = N_Range
10238 and then not Is_Scalar_Type (T)
10239 then
10240 Error_Msg_N ("scalar type required for range", R);
10241 end if;
10243 if Is_Entity_Name (R) then
10244 Freeze_Expression (R);
10245 else
10246 Resolve (R, T);
10247 Check_Unset_Reference (R);
10248 end if;
10250 -- Here after resolving membership operation
10252 <<SM_Exit>>
10254 Eval_Membership_Op (N);
10255 end Resolve_Membership_Op;
10257 ------------------
10258 -- Resolve_Null --
10259 ------------------
10261 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
10262 Loc : constant Source_Ptr := Sloc (N);
10264 begin
10265 -- Handle restriction against anonymous null access values This
10266 -- restriction can be turned off using -gnatdj.
10268 -- Ada 2005 (AI-231): Remove restriction
10270 if Ada_Version < Ada_2005
10271 and then not Debug_Flag_J
10272 and then Ekind (Typ) = E_Anonymous_Access_Type
10273 and then Comes_From_Source (N)
10274 then
10275 -- In the common case of a call which uses an explicitly null value
10276 -- for an access parameter, give specialized error message.
10278 if Nkind (Parent (N)) in N_Subprogram_Call then
10279 Error_Msg_N
10280 ("NULL is not allowed as argument for an access parameter", N);
10282 -- Standard message for all other cases (are there any?)
10284 else
10285 Error_Msg_N
10286 ("NULL cannot be of an anonymous access type", N);
10287 end if;
10288 end if;
10290 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
10291 -- assignment to a null-excluding object.
10293 if Ada_Version >= Ada_2005
10294 and then Can_Never_Be_Null (Typ)
10295 and then Nkind (Parent (N)) = N_Assignment_Statement
10296 then
10297 if Inside_Init_Proc then
10299 -- Decide whether to generate an if_statement around our
10300 -- null-excluding check to avoid them on certain internal object
10301 -- declarations by looking at the type the current Init_Proc
10302 -- belongs to.
10304 -- Generate:
10305 -- if T1b_skip_null_excluding_check then
10306 -- [constraint_error "access check failed"]
10307 -- end if;
10309 if Needs_Conditional_Null_Excluding_Check
10310 (Etype (First_Formal (Enclosing_Init_Proc)))
10311 then
10312 Insert_Action (N,
10313 Make_If_Statement (Loc,
10314 Condition =>
10315 Make_Identifier (Loc,
10316 New_External_Name
10317 (Chars (Typ), "_skip_null_excluding_check")),
10318 Then_Statements =>
10319 New_List (
10320 Make_Raise_Constraint_Error (Loc,
10321 Reason => CE_Access_Check_Failed))));
10323 -- Otherwise, simply create the check
10325 else
10326 Insert_Action (N,
10327 Make_Raise_Constraint_Error (Loc,
10328 Reason => CE_Access_Check_Failed));
10329 end if;
10330 else
10331 Insert_Action
10332 (Compile_Time_Constraint_Error (N,
10333 "(Ada 2005) NULL not allowed in null-excluding objects??"),
10334 Make_Raise_Constraint_Error (Loc,
10335 Reason => CE_Access_Check_Failed));
10336 end if;
10337 end if;
10339 -- In a distributed context, null for a remote access to subprogram may
10340 -- need to be replaced with a special record aggregate. In this case,
10341 -- return after having done the transformation.
10343 if (Ekind (Typ) = E_Record_Type
10344 or else Is_Remote_Access_To_Subprogram_Type (Typ))
10345 and then Remote_AST_Null_Value (N, Typ)
10346 then
10347 return;
10348 end if;
10350 -- The null literal takes its type from the context
10352 Set_Etype (N, Typ);
10353 end Resolve_Null;
10355 -----------------------
10356 -- Resolve_Op_Concat --
10357 -----------------------
10359 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
10361 -- We wish to avoid deep recursion, because concatenations are often
10362 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
10363 -- operands nonrecursively until we find something that is not a simple
10364 -- concatenation (A in this case). We resolve that, and then walk back
10365 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10366 -- to do the rest of the work at each level. The Parent pointers allow
10367 -- us to avoid recursion, and thus avoid running out of memory. See also
10368 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10370 NN : Node_Id := N;
10371 Op1 : Node_Id;
10373 begin
10374 -- The following code is equivalent to:
10376 -- Resolve_Op_Concat_First (NN, Typ);
10377 -- Resolve_Op_Concat_Arg (N, ...);
10378 -- Resolve_Op_Concat_Rest (N, Typ);
10380 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10381 -- operand is a concatenation.
10383 -- Walk down left operands
10385 loop
10386 Resolve_Op_Concat_First (NN, Typ);
10387 Op1 := Left_Opnd (NN);
10388 exit when not (Nkind (Op1) = N_Op_Concat
10389 and then not Is_Array_Type (Component_Type (Typ))
10390 and then Entity (Op1) = Entity (NN));
10391 NN := Op1;
10392 end loop;
10394 -- Now (given the above example) NN is A&B and Op1 is A
10396 -- First resolve Op1 ...
10398 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10400 -- ... then walk NN back up until we reach N (where we started), calling
10401 -- Resolve_Op_Concat_Rest along the way.
10403 loop
10404 Resolve_Op_Concat_Rest (NN, Typ);
10405 exit when NN = N;
10406 NN := Parent (NN);
10407 end loop;
10408 end Resolve_Op_Concat;
10410 ---------------------------
10411 -- Resolve_Op_Concat_Arg --
10412 ---------------------------
10414 procedure Resolve_Op_Concat_Arg
10415 (N : Node_Id;
10416 Arg : Node_Id;
10417 Typ : Entity_Id;
10418 Is_Comp : Boolean)
10420 Btyp : constant Entity_Id := Base_Type (Typ);
10421 Ctyp : constant Entity_Id := Component_Type (Typ);
10423 begin
10424 if In_Instance then
10425 if Is_Comp
10426 or else (not Is_Overloaded (Arg)
10427 and then Etype (Arg) /= Any_Composite
10428 and then Covers (Ctyp, Etype (Arg)))
10429 then
10430 Resolve (Arg, Ctyp);
10431 else
10432 Resolve (Arg, Btyp);
10433 end if;
10435 -- If both Array & Array and Array & Component are visible, there is a
10436 -- potential ambiguity that must be reported.
10438 elsif Has_Compatible_Type (Arg, Ctyp) then
10439 if Nkind (Arg) = N_Aggregate
10440 and then Is_Composite_Type (Ctyp)
10441 then
10442 if Is_Private_Type (Ctyp) then
10443 Resolve (Arg, Btyp);
10445 -- If the operation is user-defined and not overloaded use its
10446 -- profile. The operation may be a renaming, in which case it has
10447 -- been rewritten, and we want the original profile.
10449 elsif not Is_Overloaded (N)
10450 and then Comes_From_Source (Entity (Original_Node (N)))
10451 and then Ekind (Entity (Original_Node (N))) = E_Function
10452 then
10453 Resolve (Arg,
10454 Etype
10455 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10456 return;
10458 -- Otherwise an aggregate may match both the array type and the
10459 -- component type.
10461 else
10462 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10463 Set_Etype (Arg, Any_Type);
10464 end if;
10466 else
10467 if Is_Overloaded (Arg)
10468 and then Has_Compatible_Type (Arg, Typ)
10469 and then Etype (Arg) /= Any_Type
10470 then
10471 declare
10472 I : Interp_Index;
10473 It : Interp;
10474 Func : Entity_Id;
10476 begin
10477 Get_First_Interp (Arg, I, It);
10478 Func := It.Nam;
10479 Get_Next_Interp (I, It);
10481 -- Special-case the error message when the overloading is
10482 -- caused by a function that yields an array and can be
10483 -- called without parameters.
10485 if It.Nam = Func then
10486 Error_Msg_Sloc := Sloc (Func);
10487 Error_Msg_N ("ambiguous call to function#", Arg);
10488 Error_Msg_NE
10489 ("\\interpretation as call yields&", Arg, Typ);
10490 Error_Msg_NE
10491 ("\\interpretation as indexing of call yields&",
10492 Arg, Ctyp);
10494 else
10495 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10497 Get_First_Interp (Arg, I, It);
10498 while Present (It.Nam) loop
10499 Error_Msg_Sloc := Sloc (It.Nam);
10501 if Base_Type (It.Typ) = Btyp
10502 or else
10503 Base_Type (It.Typ) = Base_Type (Ctyp)
10504 then
10505 Error_Msg_N -- CODEFIX
10506 ("\\possible interpretation#", Arg);
10507 end if;
10509 Get_Next_Interp (I, It);
10510 end loop;
10511 end if;
10512 end;
10513 end if;
10515 Resolve (Arg, Ctyp);
10517 if Nkind (Arg) = N_String_Literal then
10518 Set_Etype (Arg, Ctyp);
10520 elsif Is_Scalar_Type (Etype (Arg))
10521 and then Compile_Time_Known_Value (Arg)
10522 then
10523 -- Determine if the out-of-range violation constitutes a
10524 -- warning or an error according to the expression base type,
10525 -- according to Ada 2022 RM 4.9 (35/2).
10527 if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then
10528 Apply_Compile_Time_Constraint_Error
10529 (Arg, "value not in range of}", CE_Range_Check_Failed,
10530 Ent => Base_Type (Ctyp),
10531 Typ => Base_Type (Ctyp));
10533 elsif Is_Out_Of_Range (Arg, Ctyp) then
10534 Apply_Compile_Time_Constraint_Error
10535 (Arg, "value not in range of}??", CE_Range_Check_Failed,
10536 Ent => Ctyp,
10537 Typ => Ctyp);
10538 end if;
10539 end if;
10541 if Arg = Left_Opnd (N) then
10542 Set_Is_Component_Left_Opnd (N);
10543 else
10544 Set_Is_Component_Right_Opnd (N);
10545 end if;
10546 end if;
10548 else
10549 Resolve (Arg, Btyp);
10550 end if;
10552 Check_Unset_Reference (Arg);
10553 end Resolve_Op_Concat_Arg;
10555 -----------------------------
10556 -- Resolve_Op_Concat_First --
10557 -----------------------------
10559 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10560 Btyp : constant Entity_Id := Base_Type (Typ);
10561 Op1 : constant Node_Id := Left_Opnd (N);
10562 Op2 : constant Node_Id := Right_Opnd (N);
10564 begin
10565 -- The parser folds an enormous sequence of concatenations of string
10566 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10567 -- in the right operand. If the expression resolves to a predefined "&"
10568 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10569 -- we give an error. See P_Simple_Expression in Par.Ch4.
10571 if Nkind (Op2) = N_String_Literal
10572 and then Is_Folded_In_Parser (Op2)
10573 and then Ekind (Entity (N)) = E_Function
10574 then
10575 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10576 and then String_Length (Strval (Op1)) = 0);
10577 Error_Msg_N ("too many user-defined concatenations", N);
10578 return;
10579 end if;
10581 Set_Etype (N, Btyp);
10583 if Is_Limited_Composite (Btyp) then
10584 Error_Msg_N ("concatenation not available for limited array", N);
10585 Explain_Limited_Type (Btyp, N);
10586 end if;
10587 end Resolve_Op_Concat_First;
10589 ----------------------------
10590 -- Resolve_Op_Concat_Rest --
10591 ----------------------------
10593 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10594 Op1 : constant Node_Id := Left_Opnd (N);
10595 Op2 : constant Node_Id := Right_Opnd (N);
10597 begin
10598 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10600 Generate_Operator_Reference (N, Typ);
10602 if Is_String_Type (Typ) then
10603 Eval_Concatenation (N);
10604 end if;
10606 -- If this is not a static concatenation, but the result is a string
10607 -- type (and not an array of strings) ensure that static string operands
10608 -- have their subtypes properly constructed.
10610 if Nkind (N) /= N_String_Literal
10611 and then Is_Character_Type (Component_Type (Typ))
10612 then
10613 Set_String_Literal_Subtype (Op1, Typ);
10614 Set_String_Literal_Subtype (Op2, Typ);
10615 end if;
10616 end Resolve_Op_Concat_Rest;
10618 ----------------------
10619 -- Resolve_Op_Expon --
10620 ----------------------
10622 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10623 B_Typ : constant Entity_Id := Base_Type (Typ);
10625 begin
10626 -- Catch attempts to do fixed-point exponentiation with universal
10627 -- operands, which is a case where the illegality is not caught during
10628 -- normal operator analysis. This is not done in preanalysis mode
10629 -- since the tree is not fully decorated during preanalysis.
10631 if Full_Analysis then
10632 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10633 Error_Msg_N ("exponentiation not available for fixed point", N);
10634 return;
10636 elsif Nkind (Parent (N)) in N_Op
10637 and then Present (Etype (Parent (N)))
10638 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10639 and then Etype (N) = Universal_Real
10640 and then Comes_From_Source (N)
10641 then
10642 Error_Msg_N ("exponentiation not available for fixed point", N);
10643 return;
10644 end if;
10645 end if;
10647 if Comes_From_Source (N)
10648 and then Ekind (Entity (N)) = E_Function
10649 and then Is_Imported (Entity (N))
10650 and then Is_Intrinsic_Subprogram (Entity (N))
10651 then
10652 Resolve_Intrinsic_Operator (N, Typ);
10653 return;
10654 end if;
10656 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10657 Check_For_Visible_Operator (N, B_Typ);
10658 end if;
10660 -- We do the resolution using the base type, because intermediate values
10661 -- in expressions are always of the base type, not a subtype of it.
10663 Resolve (Left_Opnd (N), B_Typ);
10664 Resolve (Right_Opnd (N), Standard_Integer);
10666 -- For integer types, right argument must be in Natural range
10668 if Is_Integer_Type (Typ) then
10669 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10670 end if;
10672 Check_Unset_Reference (Left_Opnd (N));
10673 Check_Unset_Reference (Right_Opnd (N));
10675 Set_Etype (N, B_Typ);
10676 Generate_Operator_Reference (N, B_Typ);
10678 Analyze_Dimension (N);
10680 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10681 -- Evaluate the exponentiation operator for dimensioned type
10683 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10684 else
10685 Eval_Op_Expon (N);
10686 end if;
10688 -- Set overflow checking bit. Much cleverer code needed here eventually
10689 -- and perhaps the Resolve routines should be separated for the various
10690 -- arithmetic operations, since they will need different processing. ???
10692 if Nkind (N) in N_Op then
10693 if not Overflow_Checks_Suppressed (Etype (N)) then
10694 Enable_Overflow_Check (N);
10695 end if;
10696 end if;
10697 end Resolve_Op_Expon;
10699 --------------------
10700 -- Resolve_Op_Not --
10701 --------------------
10703 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10704 function Parent_Is_Boolean return Boolean;
10705 -- This function determines if the parent node is a boolean operator or
10706 -- operation (comparison op, membership test, or short circuit form) and
10707 -- the not in question is the left operand of this operation. Note that
10708 -- if the not is in parens, then false is returned.
10710 -----------------------
10711 -- Parent_Is_Boolean --
10712 -----------------------
10714 function Parent_Is_Boolean return Boolean is
10715 begin
10716 return Paren_Count (N) = 0
10717 and then Nkind (Parent (N)) in N_Membership_Test
10718 | N_Op_Boolean
10719 | N_Short_Circuit
10720 and then Left_Opnd (Parent (N)) = N;
10721 end Parent_Is_Boolean;
10723 -- Local variables
10725 B_Typ : Entity_Id;
10727 -- Start of processing for Resolve_Op_Not
10729 begin
10730 -- Predefined operations on scalar types yield the base type. On the
10731 -- other hand, logical operations on arrays yield the type of the
10732 -- arguments (and the context).
10734 if Is_Array_Type (Typ) then
10735 B_Typ := Typ;
10736 else
10737 B_Typ := Base_Type (Typ);
10738 end if;
10740 -- Straightforward case of incorrect arguments
10742 if not Valid_Boolean_Arg (Typ) then
10743 Error_Msg_N ("invalid operand type for operator&", N);
10744 Set_Etype (N, Any_Type);
10745 return;
10747 -- Special case of probable missing parens
10749 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10750 if Parent_Is_Boolean then
10751 Error_Msg_N
10752 ("operand of NOT must be enclosed in parentheses",
10753 Right_Opnd (N));
10754 else
10755 Error_Msg_N
10756 ("no modular type available in this context", N);
10757 end if;
10759 Set_Etype (N, Any_Type);
10760 return;
10762 -- OK resolution of NOT
10764 else
10765 -- Warn if non-boolean types involved. This is a case like not a < b
10766 -- where a and b are modular, where we will get (not a) < b and most
10767 -- likely not (a < b) was intended.
10769 if Warn_On_Questionable_Missing_Parens
10770 and then not Is_Boolean_Type (Typ)
10771 and then Parent_Is_Boolean
10772 then
10773 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10774 end if;
10776 -- Warn on double negation if checking redundant constructs
10778 if Warn_On_Redundant_Constructs
10779 and then Comes_From_Source (N)
10780 and then Comes_From_Source (Right_Opnd (N))
10781 and then Root_Type (Typ) = Standard_Boolean
10782 and then Nkind (Right_Opnd (N)) = N_Op_Not
10783 then
10784 Error_Msg_N ("redundant double negation?r?", N);
10785 end if;
10787 -- Complete resolution and evaluation of NOT
10789 Resolve (Right_Opnd (N), B_Typ);
10790 Check_Unset_Reference (Right_Opnd (N));
10791 Set_Etype (N, B_Typ);
10792 Generate_Operator_Reference (N, B_Typ);
10793 Eval_Op_Not (N);
10794 end if;
10795 end Resolve_Op_Not;
10797 -----------------------------
10798 -- Resolve_Operator_Symbol --
10799 -----------------------------
10801 -- Nothing to be done, all resolved already
10803 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10804 pragma Warnings (Off, N);
10805 pragma Warnings (Off, Typ);
10807 begin
10808 null;
10809 end Resolve_Operator_Symbol;
10811 ----------------------------------
10812 -- Resolve_Qualified_Expression --
10813 ----------------------------------
10815 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10816 pragma Warnings (Off, Typ);
10818 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10819 Expr : constant Node_Id := Expression (N);
10821 begin
10822 Resolve (Expr, Target_Typ);
10823 Check_Unset_Reference (Expr);
10825 -- A qualified expression requires an exact match of the type, class-
10826 -- wide matching is not allowed. However, if the qualifying type is
10827 -- specific and the expression has a class-wide type, it may still be
10828 -- okay, since it can be the result of the expansion of a call to a
10829 -- dispatching function, so we also have to check class-wideness of the
10830 -- type of the expression's original node.
10832 if (Is_Class_Wide_Type (Target_Typ)
10833 or else
10834 (Is_Class_Wide_Type (Etype (Expr))
10835 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10836 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10837 then
10838 Wrong_Type (Expr, Target_Typ);
10839 end if;
10841 -- If the target type is unconstrained, then we reset the type of the
10842 -- result from the type of the expression. For other cases, the actual
10843 -- subtype of the expression is the target type. But we avoid doing it
10844 -- for an allocator since this is not needed and might be problematic.
10846 if Is_Composite_Type (Target_Typ)
10847 and then not Is_Constrained (Target_Typ)
10848 and then Nkind (Parent (N)) /= N_Allocator
10849 then
10850 Set_Etype (N, Etype (Expr));
10851 end if;
10853 Analyze_Dimension (N);
10854 Eval_Qualified_Expression (N);
10856 -- If we still have a qualified expression after the static evaluation,
10857 -- then apply a scalar range check if needed. The reason that we do this
10858 -- after the Eval call is that otherwise, the application of the range
10859 -- check may convert an illegal static expression and result in warning
10860 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10862 if Nkind (N) = N_Qualified_Expression
10863 and then Is_Scalar_Type (Target_Typ)
10864 then
10865 Apply_Scalar_Range_Check (Expr, Target_Typ);
10866 end if;
10868 -- AI12-0100: Once the qualified expression is resolved, check whether
10869 -- operand satisfies a static predicate of the target subtype, if any.
10870 -- In the static expression case, a predicate check failure is an error.
10872 if Has_Predicates (Target_Typ) then
10873 Check_Expression_Against_Static_Predicate
10874 (Expr, Target_Typ, Static_Failure_Is_Error => True);
10875 end if;
10876 end Resolve_Qualified_Expression;
10878 ------------------------------
10879 -- Resolve_Raise_Expression --
10880 ------------------------------
10882 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10883 begin
10884 if Typ = Raise_Type then
10885 Error_Msg_N ("cannot find unique type for raise expression", N);
10886 Set_Etype (N, Any_Type);
10888 else
10889 Set_Etype (N, Typ);
10891 -- Apply check for required parentheses in the enclosing
10892 -- context of raise_expressions (RM 11.3 (2)), including default
10893 -- expressions in contexts that can include aspect specifications,
10894 -- and ancestor parts of extension aggregates.
10896 declare
10897 Par : Node_Id := Parent (N);
10898 Parentheses_Found : Boolean := Paren_Count (N) > 0;
10900 begin
10901 while Present (Par)
10902 and then Nkind (Par) in N_Has_Etype
10903 loop
10904 if Paren_Count (Par) > 0 then
10905 Parentheses_Found := True;
10906 end if;
10908 if Nkind (Par) = N_Extension_Aggregate
10909 and then N = Ancestor_Part (Par)
10910 then
10911 exit;
10912 end if;
10914 Par := Parent (Par);
10915 end loop;
10917 if not Parentheses_Found
10918 and then Comes_From_Source (Par)
10919 and then
10920 ((Nkind (Par) in N_Modular_Type_Definition
10921 | N_Floating_Point_Definition
10922 | N_Ordinary_Fixed_Point_Definition
10923 | N_Decimal_Fixed_Point_Definition
10924 | N_Extension_Aggregate
10925 | N_Discriminant_Specification
10926 | N_Parameter_Specification
10927 | N_Formal_Object_Declaration)
10929 or else (Nkind (Par) = N_Object_Declaration
10930 and then
10931 Nkind (Parent (Par)) /= N_Extended_Return_Statement))
10932 then
10933 Error_Msg_N
10934 ("raise_expression must be parenthesized in this context",
10936 end if;
10937 end;
10938 end if;
10939 end Resolve_Raise_Expression;
10941 -------------------
10942 -- Resolve_Range --
10943 -------------------
10945 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10946 L : constant Node_Id := Low_Bound (N);
10947 H : constant Node_Id := High_Bound (N);
10949 function First_Last_Ref return Boolean;
10950 -- Returns True if N is of the form X'First .. X'Last where X is the
10951 -- same entity for both attributes.
10953 --------------------
10954 -- First_Last_Ref --
10955 --------------------
10957 function First_Last_Ref return Boolean is
10958 Lorig : constant Node_Id := Original_Node (L);
10959 Horig : constant Node_Id := Original_Node (H);
10961 begin
10962 if Nkind (Lorig) = N_Attribute_Reference
10963 and then Nkind (Horig) = N_Attribute_Reference
10964 and then Attribute_Name (Lorig) = Name_First
10965 and then Attribute_Name (Horig) = Name_Last
10966 then
10967 declare
10968 PL : constant Node_Id := Prefix (Lorig);
10969 PH : constant Node_Id := Prefix (Horig);
10970 begin
10971 return Is_Entity_Name (PL)
10972 and then Is_Entity_Name (PH)
10973 and then Entity (PL) = Entity (PH);
10974 end;
10975 end if;
10977 return False;
10978 end First_Last_Ref;
10980 -- Start of processing for Resolve_Range
10982 begin
10983 Set_Etype (N, Typ);
10985 Resolve (L, Typ);
10986 Resolve (H, Typ);
10988 -- Reanalyze the lower bound after both bounds have been analyzed, so
10989 -- that the range is known to be static or not by now. This may trigger
10990 -- more compile-time evaluation, which is useful for static analysis
10991 -- with GNATprove. This is not needed for compilation or static analysis
10992 -- with CodePeer, as full expansion does that evaluation then.
10994 if GNATprove_Mode then
10995 Set_Analyzed (L, False);
10996 Resolve (L, Typ);
10997 end if;
10999 -- Check for inappropriate range on unordered enumeration type
11001 if Bad_Unordered_Enumeration_Reference (N, Typ)
11003 -- Exclude X'First .. X'Last if X is the same entity for both
11005 and then not First_Last_Ref
11006 then
11007 Error_Msg_Sloc := Sloc (Typ);
11008 Error_Msg_NE
11009 ("subrange of unordered enumeration type& declared#?.u?", N, Typ);
11010 end if;
11012 Check_Unset_Reference (L);
11013 Check_Unset_Reference (H);
11015 -- We have to check the bounds for being within the base range as
11016 -- required for a non-static context. Normally this is automatic and
11017 -- done as part of evaluating expressions, but the N_Range node is an
11018 -- exception, since in GNAT we consider this node to be a subexpression,
11019 -- even though in Ada it is not. The circuit in Sem_Eval could check for
11020 -- this, but that would put the test on the main evaluation path for
11021 -- expressions.
11023 Check_Non_Static_Context (L);
11024 Check_Non_Static_Context (H);
11026 -- Check for an ambiguous range over character literals. This will
11027 -- happen with a membership test involving only literals.
11029 if Typ = Any_Character then
11030 Ambiguous_Character (L);
11031 Set_Etype (N, Any_Type);
11032 return;
11033 end if;
11035 -- If bounds are static, constant-fold them, so size computations are
11036 -- identical between front-end and back-end. Do not perform this
11037 -- transformation while analyzing generic units, as type information
11038 -- would be lost when reanalyzing the constant node in the instance.
11040 if Is_Discrete_Type (Typ) and then Expander_Active then
11041 if Is_OK_Static_Expression (L) then
11042 Fold_Uint (L, Expr_Value (L), Static => True);
11043 end if;
11045 if Is_OK_Static_Expression (H) then
11046 Fold_Uint (H, Expr_Value (H), Static => True);
11047 end if;
11048 end if;
11050 -- If we have a compile-time-known null range, we warn, because that is
11051 -- likely to be a mistake. (Dynamic null ranges make sense, but often
11052 -- compile-time-known ones do not.) Warn only if this is in a subtype
11053 -- declaration. We do this here, rather than while analyzing a subtype
11054 -- declaration, in case we decide to expand the cases. We do not want to
11055 -- warn in all cases, because some are idiomatic, such as an empty
11056 -- aggregate (1 .. 0 => <>).
11058 -- We don't warn in generics or their instances, because there might be
11059 -- some instances where the range is null, and some where it is not,
11060 -- which would lead to false alarms.
11062 if not (Inside_A_Generic or In_Instance)
11063 and then Comes_From_Source (N)
11064 and then Compile_Time_Compare
11065 (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT
11066 and then Nkind (Parent (N)) = N_Range_Constraint
11067 and then Nkind (Parent (Parent (N))) = N_Subtype_Indication
11068 and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration
11069 and then Is_OK_Static_Range (N)
11070 then
11071 Error_Msg_N ("null range??", N);
11072 end if;
11073 end Resolve_Range;
11075 --------------------------
11076 -- Resolve_Real_Literal --
11077 --------------------------
11079 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
11080 Actual_Typ : constant Entity_Id := Etype (N);
11082 begin
11083 -- Special processing for fixed-point literals to make sure that the
11084 -- value is an exact multiple of the small where this is required. We
11085 -- skip this for the universal real case, and also for generic types.
11087 if Is_Fixed_Point_Type (Typ)
11088 and then Typ /= Universal_Fixed
11089 and then Typ /= Any_Fixed
11090 and then not Is_Generic_Type (Typ)
11091 then
11092 -- We must freeze the base type to get the proper value of the small
11094 if not Is_Frozen (Base_Type (Typ)) then
11095 Freeze_Fixed_Point_Type (Base_Type (Typ));
11096 end if;
11098 declare
11099 Val : constant Ureal := Realval (N);
11100 Cintr : constant Ureal := Val / Small_Value (Base_Type (Typ));
11101 Cint : constant Uint := UR_Trunc (Cintr);
11102 Den : constant Uint := Norm_Den (Cintr);
11103 Stat : Boolean;
11105 begin
11106 -- Case of literal is not an exact multiple of the Small
11108 if Den /= 1 then
11110 -- For a source program literal for a decimal fixed-point type,
11111 -- this is statically illegal (RM 4.9(36)).
11113 if Is_Decimal_Fixed_Point_Type (Typ)
11114 and then Actual_Typ = Universal_Real
11115 and then Comes_From_Source (N)
11116 then
11117 Error_Msg_N ("value has extraneous low order digits", N);
11118 end if;
11120 -- Generate a warning if literal from source
11122 if Is_OK_Static_Expression (N)
11123 and then Warn_On_Bad_Fixed_Value
11124 then
11125 Error_Msg_N
11126 ("?b?static fixed-point value is not a multiple of Small!",
11128 end if;
11130 -- Replace literal by a value that is the exact representation
11131 -- of a value of the type, i.e. a multiple of the small value,
11132 -- by truncation, since Machine_Rounds is false for all GNAT
11133 -- fixed-point types (RM 4.9(38)).
11135 Stat := Is_OK_Static_Expression (N);
11136 Rewrite (N,
11137 Make_Real_Literal (Sloc (N),
11138 Realval => Small_Value (Typ) * Cint));
11140 Set_Is_Static_Expression (N, Stat);
11141 end if;
11143 -- In all cases, set the corresponding integer field
11145 Set_Corresponding_Integer_Value (N, Cint);
11146 end;
11147 end if;
11149 -- Now replace the actual type by the expected type as usual
11151 Set_Etype (N, Typ);
11152 Eval_Real_Literal (N);
11153 end Resolve_Real_Literal;
11155 -----------------------
11156 -- Resolve_Reference --
11157 -----------------------
11159 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
11160 P : constant Node_Id := Prefix (N);
11162 begin
11163 -- Replace general access with specific type
11165 if Ekind (Etype (N)) = E_Allocator_Type then
11166 Set_Etype (N, Base_Type (Typ));
11167 end if;
11169 Resolve (P, Designated_Type (Etype (N)));
11171 -- If we are taking the reference of a volatile entity, then treat it as
11172 -- a potential modification of this entity. This is too conservative,
11173 -- but necessary because remove side effects can cause transformations
11174 -- of normal assignments into reference sequences that otherwise fail to
11175 -- notice the modification.
11177 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
11178 Note_Possible_Modification (P, Sure => False);
11179 end if;
11180 end Resolve_Reference;
11182 --------------------------------
11183 -- Resolve_Selected_Component --
11184 --------------------------------
11186 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
11187 Comp : Entity_Id;
11188 Comp1 : Entity_Id := Empty; -- prevent junk warning
11189 P : constant Node_Id := Prefix (N);
11190 S : constant Node_Id := Selector_Name (N);
11191 T : Entity_Id := Etype (P);
11192 I : Interp_Index;
11193 I1 : Interp_Index := 0; -- prevent junk warning
11194 It : Interp;
11195 It1 : Interp;
11196 Found : Boolean;
11198 function Init_Component return Boolean;
11199 -- Check whether this is the initialization of a component within an
11200 -- init proc (by assignment or call to another init proc). If true,
11201 -- there is no need for a discriminant check.
11203 --------------------
11204 -- Init_Component --
11205 --------------------
11207 function Init_Component return Boolean is
11208 begin
11209 return Inside_Init_Proc
11210 and then Nkind (Prefix (N)) = N_Identifier
11211 and then Chars (Prefix (N)) = Name_uInit
11212 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
11213 end Init_Component;
11215 -- Start of processing for Resolve_Selected_Component
11217 begin
11218 if Is_Overloaded (P) then
11220 -- Use the context type to select the prefix that has a selector
11221 -- of the correct name and type.
11223 Found := False;
11224 Get_First_Interp (P, I, It);
11226 Search : while Present (It.Typ) loop
11227 if Is_Access_Type (It.Typ) then
11228 T := Designated_Type (It.Typ);
11229 else
11230 T := It.Typ;
11231 end if;
11233 -- Locate selected component. For a private prefix the selector
11234 -- can denote a discriminant.
11236 if Is_Record_Type (T) or else Is_Private_Type (T) then
11238 -- The visible components of a class-wide type are those of
11239 -- the root type.
11241 if Is_Class_Wide_Type (T) then
11242 T := Etype (T);
11243 end if;
11245 Comp := First_Entity (T);
11246 while Present (Comp) loop
11247 if Chars (Comp) = Chars (S)
11248 and then Covers (Typ, Etype (Comp))
11249 then
11250 if not Found then
11251 Found := True;
11252 I1 := I;
11253 It1 := It;
11254 Comp1 := Comp;
11256 else
11257 It := Disambiguate (P, I1, I, Any_Type);
11259 if It = No_Interp then
11260 Error_Msg_N
11261 ("ambiguous prefix for selected component", N);
11262 Set_Etype (N, Typ);
11263 return;
11265 else
11266 It1 := It;
11268 -- There may be an implicit dereference. Retrieve
11269 -- designated record type.
11271 if Is_Access_Type (It1.Typ) then
11272 T := Designated_Type (It1.Typ);
11273 else
11274 T := It1.Typ;
11275 end if;
11277 if Scope (Comp1) /= T then
11279 -- Resolution chooses the new interpretation.
11280 -- Find the component with the right name.
11282 Comp1 := First_Entity (T);
11283 while Present (Comp1)
11284 and then Chars (Comp1) /= Chars (S)
11285 loop
11286 Next_Entity (Comp1);
11287 end loop;
11288 end if;
11290 exit Search;
11291 end if;
11292 end if;
11293 end if;
11295 Next_Entity (Comp);
11296 end loop;
11297 end if;
11299 Get_Next_Interp (I, It);
11300 end loop Search;
11302 -- There must be a legal interpretation at this point
11304 pragma Assert (Found);
11305 Resolve (P, It1.Typ);
11307 -- In general the expected type is the type of the context, not the
11308 -- type of the candidate selected component.
11310 Set_Etype (N, Typ);
11311 Set_Entity_With_Checks (S, Comp1);
11313 -- The type of the context and that of the component are
11314 -- compatible and in general identical, but if they are anonymous
11315 -- access-to-subprogram types, the relevant type is that of the
11316 -- component. This matters in Unnest_Subprograms mode, where the
11317 -- relevant context is the one in which the type is declared, not
11318 -- the point of use. This determines what activation record to use.
11320 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
11321 Set_Etype (N, Etype (Comp1));
11323 -- When the type of the component is an access to a class-wide type
11324 -- the relevant type is that of the component (since in such case we
11325 -- may need to generate implicit type conversions or dispatching
11326 -- calls).
11328 elsif Is_Access_Type (Typ)
11329 and then not Is_Class_Wide_Type (Designated_Type (Typ))
11330 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
11331 then
11332 Set_Etype (N, Etype (Comp1));
11333 end if;
11335 else
11336 -- Resolve prefix with its type
11338 Resolve (P, T);
11339 end if;
11341 -- Generate cross-reference. We needed to wait until full overloading
11342 -- resolution was complete to do this, since otherwise we can't tell if
11343 -- we are an lvalue or not.
11345 if Known_To_Be_Assigned (N) then
11346 Generate_Reference (Entity (S), S, 'm');
11347 else
11348 Generate_Reference (Entity (S), S, 'r');
11349 end if;
11351 -- If the prefix's type is an access type, get to the real record type.
11352 -- Note: we do not apply an access check because an explicit dereference
11353 -- will be introduced later, and the check will happen there.
11355 if Is_Access_Type (Etype (P)) then
11356 T := Implicitly_Designated_Type (Etype (P));
11357 Check_Fully_Declared_Prefix (T, P);
11359 else
11360 T := Etype (P);
11361 end if;
11363 -- Set flag for expander if discriminant check required on a component
11364 -- appearing within a variant.
11366 if Has_Discriminants (T)
11367 and then Ekind (Entity (S)) = E_Component
11368 and then Present (Original_Record_Component (Entity (S)))
11369 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
11370 and then
11371 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
11372 and then not Discriminant_Checks_Suppressed (T)
11373 and then not Init_Component
11374 then
11375 Set_Do_Discriminant_Check (N);
11376 end if;
11378 if Ekind (Entity (S)) = E_Void then
11379 Error_Msg_N ("premature use of component", S);
11380 end if;
11382 -- If the prefix is a record conversion, this may be a renamed
11383 -- discriminant whose bounds differ from those of the original
11384 -- one, so we must ensure that a range check is performed.
11386 if Nkind (P) = N_Type_Conversion
11387 and then Ekind (Entity (S)) = E_Discriminant
11388 and then Is_Discrete_Type (Typ)
11389 then
11390 Set_Etype (N, Base_Type (Typ));
11391 end if;
11393 -- Eval_Selected_Component may e.g. fold statically known discriminants.
11395 Eval_Selected_Component (N);
11397 if Nkind (N) = N_Selected_Component then
11399 -- If the record type is atomic and the component is not, then this
11400 -- is worth a warning before Ada 2022, since we have a situation
11401 -- where the access to the component may cause extra read/writes of
11402 -- the atomic object, or partial word accesses, both of which may be
11403 -- unexpected.
11405 if Is_Atomic_Ref_With_Address (N)
11406 and then not Is_Atomic (Entity (S))
11407 and then not Is_Atomic (Etype (Entity (S)))
11408 and then Ada_Version < Ada_2022
11409 then
11410 Error_Msg_N
11411 ("??access to non-atomic component of atomic record",
11412 Prefix (N));
11413 Error_Msg_N
11414 ("\??may cause unexpected accesses to atomic object",
11415 Prefix (N));
11416 end if;
11418 Resolve_Implicit_Dereference (Prefix (N));
11419 Analyze_Dimension (N);
11420 end if;
11421 end Resolve_Selected_Component;
11423 -------------------
11424 -- Resolve_Shift --
11425 -------------------
11427 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11428 B_Typ : constant Entity_Id := Base_Type (Typ);
11429 L : constant Node_Id := Left_Opnd (N);
11430 R : constant Node_Id := Right_Opnd (N);
11432 begin
11433 -- We do the resolution using the base type, because intermediate values
11434 -- in expressions always are of the base type, not a subtype of it.
11436 Resolve (L, B_Typ);
11437 Resolve (R, Standard_Natural);
11439 Check_Unset_Reference (L);
11440 Check_Unset_Reference (R);
11442 Set_Etype (N, B_Typ);
11443 Generate_Operator_Reference (N, B_Typ);
11444 Eval_Shift (N);
11445 end Resolve_Shift;
11447 ---------------------------
11448 -- Resolve_Short_Circuit --
11449 ---------------------------
11451 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11452 B_Typ : constant Entity_Id := Base_Type (Typ);
11453 L : constant Node_Id := Left_Opnd (N);
11454 R : constant Node_Id := Right_Opnd (N);
11456 begin
11457 -- Ensure all actions associated with the left operand (e.g.
11458 -- finalization of transient objects) are fully evaluated locally within
11459 -- an expression with actions. This is particularly helpful for coverage
11460 -- analysis. However this should not happen in generics or if option
11461 -- Minimize_Expression_With_Actions is set.
11463 if Expander_Active and not Minimize_Expression_With_Actions then
11464 declare
11465 Reloc_L : constant Node_Id := Relocate_Node (L);
11466 begin
11467 Save_Interps (Old_N => L, New_N => Reloc_L);
11469 Rewrite (L,
11470 Make_Expression_With_Actions (Sloc (L),
11471 Actions => New_List,
11472 Expression => Reloc_L));
11474 -- Set Comes_From_Source on L to preserve warnings for unset
11475 -- reference.
11477 Preserve_Comes_From_Source (L, Reloc_L);
11478 end;
11479 end if;
11481 Resolve (L, B_Typ);
11482 Resolve (R, B_Typ);
11484 -- Check for issuing warning for always False assert/check, this happens
11485 -- when assertions are turned off, in which case the pragma Assert/Check
11486 -- was transformed into:
11488 -- if False and then <condition> then ...
11490 -- and we detect this pattern
11492 if Warn_On_Assertion_Failure
11493 and then Is_Entity_Name (R)
11494 and then Entity (R) = Standard_False
11495 and then Nkind (Parent (N)) = N_If_Statement
11496 and then Nkind (N) = N_And_Then
11497 and then Is_Entity_Name (L)
11498 and then Entity (L) = Standard_False
11499 then
11500 declare
11501 Orig : constant Node_Id := Original_Node (Parent (N));
11503 begin
11504 -- Special handling of Asssert pragma
11506 if Nkind (Orig) = N_Pragma
11507 and then Pragma_Name (Orig) = Name_Assert
11508 then
11509 declare
11510 Expr : constant Node_Id :=
11511 Original_Node
11512 (Expression
11513 (First (Pragma_Argument_Associations (Orig))));
11515 begin
11516 -- Don't warn if original condition is explicit False,
11517 -- since obviously the failure is expected in this case.
11519 if Is_Entity_Name (Expr)
11520 and then Entity (Expr) = Standard_False
11521 then
11522 null;
11524 -- Issue warning. We do not want the deletion of the
11525 -- IF/AND-THEN to take this message with it. We achieve this
11526 -- by making sure that the expanded code points to the Sloc
11527 -- of the expression, not the original pragma.
11529 else
11530 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11531 -- The source location of the expression is not usually
11532 -- the best choice here. For example, it gets located on
11533 -- the last AND keyword in a chain of boolean expressiond
11534 -- AND'ed together. It is best to put the message on the
11535 -- first character of the assertion, which is the effect
11536 -- of the First_Node call here.
11538 Error_Msg_F
11539 ("?.a?assertion would fail at run time!",
11540 Expression
11541 (First (Pragma_Argument_Associations (Orig))));
11542 end if;
11543 end;
11545 -- Similar processing for Check pragma
11547 elsif Nkind (Orig) = N_Pragma
11548 and then Pragma_Name (Orig) = Name_Check
11549 then
11550 -- Don't want to warn if original condition is explicit False
11552 declare
11553 Expr : constant Node_Id :=
11554 Original_Node
11555 (Expression
11556 (Next (First (Pragma_Argument_Associations (Orig)))));
11557 begin
11558 if Is_Entity_Name (Expr)
11559 and then Entity (Expr) = Standard_False
11560 then
11561 null;
11563 -- Post warning
11565 else
11566 -- Again use Error_Msg_F rather than Error_Msg_N, see
11567 -- comment above for an explanation of why we do this.
11569 Error_Msg_F
11570 ("?.a?check would fail at run time!",
11571 Expression
11572 (Last (Pragma_Argument_Associations (Orig))));
11573 end if;
11574 end;
11575 end if;
11576 end;
11577 end if;
11579 -- Continue with processing of short circuit
11581 Check_Unset_Reference (L);
11582 Check_Unset_Reference (R);
11584 Set_Etype (N, B_Typ);
11585 Eval_Short_Circuit (N);
11586 end Resolve_Short_Circuit;
11588 -------------------
11589 -- Resolve_Slice --
11590 -------------------
11592 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11593 Drange : constant Node_Id := Discrete_Range (N);
11594 Pref : constant Node_Id := Prefix (N);
11595 Array_Type : Entity_Id := Empty;
11596 Dexpr : Node_Id := Empty;
11597 Index_Type : Entity_Id;
11599 begin
11600 if Is_Overloaded (Pref) then
11602 -- Use the context type to select the prefix that yields the correct
11603 -- array type.
11605 declare
11606 I : Interp_Index;
11607 I1 : Interp_Index := 0;
11608 It : Interp;
11609 Found : Boolean := False;
11611 begin
11612 Get_First_Interp (Pref, I, It);
11613 while Present (It.Typ) loop
11614 if (Is_Array_Type (It.Typ)
11615 and then Covers (Typ, It.Typ))
11616 or else (Is_Access_Type (It.Typ)
11617 and then Is_Array_Type (Designated_Type (It.Typ))
11618 and then Covers (Typ, Designated_Type (It.Typ)))
11619 then
11620 if Found then
11621 It := Disambiguate (Pref, I1, I, Any_Type);
11623 if It = No_Interp then
11624 Error_Msg_N ("ambiguous prefix for slicing", N);
11625 Set_Etype (N, Typ);
11626 return;
11627 else
11628 Found := True;
11629 Array_Type := It.Typ;
11630 I1 := I;
11631 end if;
11632 else
11633 Found := True;
11634 Array_Type := It.Typ;
11635 I1 := I;
11636 end if;
11637 end if;
11639 Get_Next_Interp (I, It);
11640 end loop;
11641 end;
11643 else
11644 Array_Type := Etype (Pref);
11645 end if;
11647 Resolve (Pref, Array_Type);
11649 -- If the prefix's type is an access type, get to the real array type.
11650 -- Note: we do not apply an access check because an explicit dereference
11651 -- will be introduced later, and the check will happen there.
11653 if Is_Access_Type (Array_Type) then
11654 Array_Type := Implicitly_Designated_Type (Array_Type);
11656 -- If the prefix is an access to an unconstrained array, we must use
11657 -- the actual subtype of the object to perform the index checks. The
11658 -- object denoted by the prefix is implicit in the node, so we build
11659 -- an explicit representation for it in order to compute the actual
11660 -- subtype.
11662 if not Is_Constrained (Array_Type) then
11663 Remove_Side_Effects (Pref);
11665 declare
11666 Obj : constant Node_Id :=
11667 Make_Explicit_Dereference (Sloc (N),
11668 Prefix => New_Copy_Tree (Pref));
11669 begin
11670 Set_Etype (Obj, Array_Type);
11671 Set_Parent (Obj, Parent (N));
11672 Array_Type := Get_Actual_Subtype (Obj);
11673 end;
11674 end if;
11676 -- In CodePeer mode the attribute Image is not expanded, so when it
11677 -- acts as a prefix of a slice, we handle it like a call to function
11678 -- returning an unconstrained string. Same for the Wide variants of
11679 -- attribute Image.
11681 elsif Is_Entity_Name (Pref)
11682 or else Nkind (Pref) = N_Explicit_Dereference
11683 or else (Nkind (Pref) = N_Function_Call
11684 and then not Is_Constrained (Etype (Pref)))
11685 or else (CodePeer_Mode
11686 and then Nkind (Pref) = N_Attribute_Reference
11687 and then Attribute_Name (Pref) in Name_Image
11688 | Name_Wide_Image
11689 | Name_Wide_Wide_Image)
11690 then
11691 Array_Type := Get_Actual_Subtype (Pref);
11693 -- If the name is a selected component that depends on discriminants,
11694 -- build an actual subtype for it. This can happen only when the name
11695 -- itself is overloaded; otherwise the actual subtype is created when
11696 -- the selected component is analyzed.
11698 elsif Nkind (Pref) = N_Selected_Component
11699 and then Full_Analysis
11700 and then Depends_On_Discriminant (First_Index (Array_Type))
11701 then
11702 declare
11703 Act_Decl : constant Node_Id :=
11704 Build_Actual_Subtype_Of_Component (Array_Type, Pref);
11705 begin
11706 Insert_Action (N, Act_Decl);
11707 Array_Type := Defining_Identifier (Act_Decl);
11708 end;
11710 -- Maybe this should just be "else", instead of checking for the
11711 -- specific case of slice??? This is needed for the case where the
11712 -- prefix is an Image attribute, which gets expanded to a slice, and so
11713 -- has a constrained subtype which we want to use for the slice range
11714 -- check applied below (the range check won't get done if the
11715 -- unconstrained subtype of the 'Image is used).
11717 elsif Nkind (Pref) = N_Slice then
11718 Array_Type := Etype (Pref);
11719 end if;
11721 -- Obtain the type of the array index
11723 if Ekind (Array_Type) = E_String_Literal_Subtype then
11724 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11725 else
11726 Index_Type := Etype (First_Index (Array_Type));
11727 end if;
11729 -- If name was overloaded, set slice type correctly now
11731 Set_Etype (N, Array_Type);
11733 -- Handle the generation of a range check that compares the array index
11734 -- against the discrete_range. The check is not applied to internally
11735 -- built nodes associated with the expansion of dispatch tables. Check
11736 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11737 -- the unit.
11739 if Tagged_Type_Expansion
11740 and then RTU_Loaded (Ada_Tags)
11741 and then Nkind (Pref) = N_Selected_Component
11742 and then Present (Entity (Selector_Name (Pref)))
11743 and then Entity (Selector_Name (Pref)) =
11744 RTE_Record_Component (RE_Prims_Ptr)
11745 then
11746 null;
11748 -- The discrete_range is specified by a subtype name. Create an
11749 -- equivalent range attribute, apply checks to this attribute, but
11750 -- insert them into the range expression of the slice itself.
11752 elsif Is_Entity_Name (Drange) then
11753 Dexpr :=
11754 Make_Attribute_Reference
11755 (Sloc (Drange),
11756 Prefix =>
11757 New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
11758 Attribute_Name => Name_Range);
11760 Analyze_And_Resolve (Dexpr, Etype (Drange));
11762 elsif Nkind (Drange) = N_Subtype_Indication then
11763 Dexpr := Range_Expression (Constraint (Drange));
11765 -- The discrete_range is a regular range (or a range attribute, which
11766 -- will be resolved into a regular range). Resolve the bounds and remove
11767 -- their side effects.
11769 else
11770 Resolve (Drange, Base_Type (Index_Type));
11772 if Nkind (Drange) = N_Range then
11773 Force_Evaluation (Low_Bound (Drange));
11774 Force_Evaluation (High_Bound (Drange));
11776 Dexpr := Drange;
11777 end if;
11778 end if;
11780 if Present (Dexpr) then
11781 Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
11782 end if;
11784 Set_Slice_Subtype (N);
11786 -- Check bad use of type with predicates
11788 declare
11789 Subt : Entity_Id;
11791 begin
11792 if Nkind (Drange) = N_Subtype_Indication
11793 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11794 then
11795 Subt := Entity (Subtype_Mark (Drange));
11796 else
11797 Subt := Etype (Drange);
11798 end if;
11800 if Has_Predicates (Subt) then
11801 Bad_Predicated_Subtype_Use
11802 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11803 end if;
11804 end;
11806 -- Otherwise here is where we check suspicious indexes
11808 if Nkind (Drange) = N_Range then
11809 Warn_On_Suspicious_Index (Pref, Low_Bound (Drange));
11810 Warn_On_Suspicious_Index (Pref, High_Bound (Drange));
11811 end if;
11813 Resolve_Implicit_Dereference (Pref);
11814 Analyze_Dimension (N);
11815 Eval_Slice (N);
11816 end Resolve_Slice;
11818 ----------------------------
11819 -- Resolve_String_Literal --
11820 ----------------------------
11822 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11823 C_Typ : constant Entity_Id := Component_Type (Typ);
11824 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11825 Loc : constant Source_Ptr := Sloc (N);
11826 Str : constant String_Id := Strval (N);
11827 Strlen : constant Nat := String_Length (Str);
11828 Subtype_Id : Entity_Id;
11829 Need_Check : Boolean;
11831 begin
11832 -- For a string appearing in a concatenation, defer creation of the
11833 -- string_literal_subtype until the end of the resolution of the
11834 -- concatenation, because the literal may be constant-folded away. This
11835 -- is a useful optimization for long concatenation expressions.
11837 -- If the string is an aggregate built for a single character (which
11838 -- happens in a non-static context) or a is null string to which special
11839 -- checks may apply, we build the subtype. Wide strings must also get a
11840 -- string subtype if they come from a one character aggregate. Strings
11841 -- generated by attributes might be static, but it is often hard to
11842 -- determine whether the enclosing context is static, so we generate
11843 -- subtypes for them as well, thus losing some rarer optimizations ???
11844 -- Same for strings that come from a static conversion.
11846 Need_Check :=
11847 (Strlen = 0 and then Typ /= Standard_String)
11848 or else Nkind (Parent (N)) /= N_Op_Concat
11849 or else (N /= Left_Opnd (Parent (N))
11850 and then N /= Right_Opnd (Parent (N)))
11851 or else ((Typ = Standard_Wide_String
11852 or else Typ = Standard_Wide_Wide_String)
11853 and then Nkind (Original_Node (N)) /= N_String_Literal);
11855 -- If the resolving type is itself a string literal subtype, we can just
11856 -- reuse it, since there is no point in creating another.
11858 if Ekind (Typ) = E_String_Literal_Subtype then
11859 Subtype_Id := Typ;
11861 elsif Nkind (Parent (N)) = N_Op_Concat
11862 and then not Need_Check
11863 and then Nkind (Original_Node (N)) not in N_Character_Literal
11864 | N_Attribute_Reference
11865 | N_Qualified_Expression
11866 | N_Type_Conversion
11867 then
11868 Subtype_Id := Typ;
11870 -- Do not generate a string literal subtype for the default expression
11871 -- of a formal parameter in GNATprove mode. This is because the string
11872 -- subtype is associated with the freezing actions of the subprogram,
11873 -- however freezing is disabled in GNATprove mode and as a result the
11874 -- subtype is unavailable.
11876 elsif GNATprove_Mode
11877 and then Nkind (Parent (N)) = N_Parameter_Specification
11878 then
11879 Subtype_Id := Typ;
11881 -- Otherwise we must create a string literal subtype. Note that the
11882 -- whole idea of string literal subtypes is simply to avoid the need
11883 -- for building a full fledged array subtype for each literal.
11885 else
11886 Set_String_Literal_Subtype (N, Typ);
11887 Subtype_Id := Etype (N);
11888 end if;
11890 if Nkind (Parent (N)) /= N_Op_Concat
11891 or else Need_Check
11892 then
11893 Set_Etype (N, Subtype_Id);
11894 Eval_String_Literal (N);
11895 end if;
11897 if Is_Limited_Composite (Typ)
11898 or else Is_Private_Composite (Typ)
11899 then
11900 Error_Msg_N ("string literal not available for private array", N);
11901 Set_Etype (N, Any_Type);
11902 return;
11903 end if;
11905 -- The validity of a null string has been checked in the call to
11906 -- Eval_String_Literal.
11908 if Strlen = 0 then
11909 return;
11911 -- Always accept string literal with component type Any_Character, which
11912 -- occurs in error situations and in comparisons of literals, both of
11913 -- which should accept all literals.
11915 elsif R_Typ = Any_Character then
11916 return;
11918 -- If the type is bit-packed, then we always transform the string
11919 -- literal into a full fledged aggregate.
11921 elsif Is_Bit_Packed_Array (Typ) then
11922 null;
11924 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11926 else
11927 -- For Standard.Wide_Wide_String, or any other type whose component
11928 -- type is Standard.Wide_Wide_Character, we know that all the
11929 -- characters in the string must be acceptable, since the parser
11930 -- accepted the characters as valid character literals.
11932 if R_Typ = Standard_Wide_Wide_Character then
11933 null;
11935 -- For the case of Standard.String, or any other type whose component
11936 -- type is Standard.Character, we must make sure that there are no
11937 -- wide characters in the string, i.e. that it is entirely composed
11938 -- of characters in range of type Character.
11940 -- If the string literal is the result of a static concatenation, the
11941 -- test has already been performed on the components, and need not be
11942 -- repeated.
11944 elsif R_Typ = Standard_Character
11945 and then Nkind (Original_Node (N)) /= N_Op_Concat
11946 then
11947 for J in 1 .. Strlen loop
11948 if not In_Character_Range (Get_String_Char (Str, J)) then
11950 -- If we are out of range, post error. This is one of the
11951 -- very few places that we place the flag in the middle of
11952 -- a token, right under the offending wide character. Not
11953 -- quite clear if this is right wrt wide character encoding
11954 -- sequences, but it's only an error message.
11956 Error_Msg
11957 ("literal out of range of type Standard.Character",
11958 Loc + Source_Ptr (J));
11959 return;
11960 end if;
11961 end loop;
11963 -- For the case of Standard.Wide_String, or any other type whose
11964 -- component type is Standard.Wide_Character, we must make sure that
11965 -- there are no wide characters in the string, i.e. that it is
11966 -- entirely composed of characters in range of type Wide_Character.
11968 -- If the string literal is the result of a static concatenation,
11969 -- the test has already been performed on the components, and need
11970 -- not be repeated.
11972 elsif R_Typ = Standard_Wide_Character
11973 and then Nkind (Original_Node (N)) /= N_Op_Concat
11974 then
11975 for J in 1 .. Strlen loop
11976 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11978 -- If we are out of range, post error. This is one of the
11979 -- very few places that we place the flag in the middle of
11980 -- a token, right under the offending wide character.
11982 -- This is not quite right, because characters in general
11983 -- will take more than one character position ???
11985 Error_Msg
11986 ("literal out of range of type Standard.Wide_Character",
11987 Loc + Source_Ptr (J));
11988 return;
11989 end if;
11990 end loop;
11992 -- If the root type is not a standard character, then we will convert
11993 -- the string into an aggregate and will let the aggregate code do
11994 -- the checking. Standard Wide_Wide_Character is also OK here.
11996 else
11997 null;
11998 end if;
12000 -- See if the component type of the array corresponding to the string
12001 -- has compile time known bounds. If yes we can directly check
12002 -- whether the evaluation of the string will raise constraint error.
12003 -- Otherwise we need to transform the string literal into the
12004 -- corresponding character aggregate and let the aggregate code do
12005 -- the checking. We use the same transformation if the component
12006 -- type has a static predicate, which will be applied to each
12007 -- character when the aggregate is resolved.
12009 if Is_Standard_Character_Type (R_Typ) then
12011 -- Check for the case of full range, where we are definitely OK
12013 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
12014 return;
12015 end if;
12017 -- Here the range is not the complete base type range, so check
12019 declare
12020 Comp_Typ_Lo : constant Node_Id :=
12021 Type_Low_Bound (Component_Type (Typ));
12022 Comp_Typ_Hi : constant Node_Id :=
12023 Type_High_Bound (Component_Type (Typ));
12025 Char_Val : Uint;
12027 begin
12028 if Compile_Time_Known_Value (Comp_Typ_Lo)
12029 and then Compile_Time_Known_Value (Comp_Typ_Hi)
12030 then
12031 for J in 1 .. Strlen loop
12032 Char_Val := UI_From_CC (Get_String_Char (Str, J));
12034 if Char_Val < Expr_Value (Comp_Typ_Lo)
12035 or else Char_Val > Expr_Value (Comp_Typ_Hi)
12036 then
12037 Apply_Compile_Time_Constraint_Error
12038 (N, "character out of range??",
12039 CE_Range_Check_Failed,
12040 Loc => Loc + Source_Ptr (J));
12041 end if;
12042 end loop;
12044 if not Has_Static_Predicate (C_Typ) then
12045 return;
12046 end if;
12047 end if;
12048 end;
12049 end if;
12050 end if;
12052 -- If we got here we meed to transform the string literal into the
12053 -- equivalent qualified positional array aggregate. This is rather
12054 -- heavy artillery for this situation, but it is hard work to avoid.
12056 declare
12057 Lits : constant List_Id := New_List;
12058 P : Source_Ptr := Loc + 1;
12059 C : Char_Code;
12061 begin
12062 -- Build the character literals, we give them source locations that
12063 -- correspond to the string positions, which is a bit tricky given
12064 -- the possible presence of wide character escape sequences.
12066 for J in 1 .. Strlen loop
12067 C := Get_String_Char (Str, J);
12068 Set_Character_Literal_Name (C);
12070 Append_To (Lits,
12071 Make_Character_Literal (P,
12072 Chars => Name_Find,
12073 Char_Literal_Value => UI_From_CC (C)));
12075 if In_Character_Range (C) then
12076 P := P + 1;
12078 -- Should we have a call to Skip_Wide here ???
12080 -- ??? else
12081 -- Skip_Wide (P);
12083 end if;
12084 end loop;
12086 Rewrite (N,
12087 Make_Qualified_Expression (Loc,
12088 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
12089 Expression =>
12090 Make_Aggregate (Loc, Expressions => Lits)));
12092 Analyze_And_Resolve (N, Typ);
12093 end;
12094 end Resolve_String_Literal;
12096 -------------------------
12097 -- Resolve_Target_Name --
12098 -------------------------
12100 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
12101 begin
12102 Set_Etype (N, Typ);
12103 end Resolve_Target_Name;
12105 -----------------------------
12106 -- Resolve_Type_Conversion --
12107 -----------------------------
12109 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
12110 Conv_OK : constant Boolean := Conversion_OK (N);
12111 Operand : constant Node_Id := Expression (N);
12112 Operand_Typ : constant Entity_Id := Etype (Operand);
12113 Target_Typ : constant Entity_Id := Etype (N);
12114 Rop : Node_Id;
12115 Orig_N : Node_Id;
12116 Orig_T : Node_Id;
12118 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
12119 -- Set to False to suppress cases where we want to suppress the test
12120 -- for redundancy to avoid possible false positives on this warning.
12122 begin
12123 if not Conv_OK
12124 and then not Valid_Conversion (N, Target_Typ, Operand)
12125 then
12126 return;
12127 end if;
12129 -- If the Operand Etype is Universal_Fixed, then the conversion is
12130 -- never redundant. We need this check because by the time we have
12131 -- finished the rather complex transformation, the conversion looks
12132 -- redundant when it is not.
12134 if Operand_Typ = Universal_Fixed then
12135 Test_Redundant := False;
12137 -- If the operand is marked as Any_Fixed, then special processing is
12138 -- required. This is also a case where we suppress the test for a
12139 -- redundant conversion, since most certainly it is not redundant.
12141 elsif Operand_Typ = Any_Fixed then
12142 Test_Redundant := False;
12144 -- Mixed-mode operation involving a literal. Context must be a fixed
12145 -- type which is applied to the literal subsequently.
12147 -- Multiplication and division involving two fixed type operands must
12148 -- yield a universal real because the result is computed in arbitrary
12149 -- precision.
12151 if Is_Fixed_Point_Type (Typ)
12152 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
12153 and then Etype (Left_Opnd (Operand)) = Any_Fixed
12154 and then Etype (Right_Opnd (Operand)) = Any_Fixed
12155 then
12156 Set_Etype (Operand, Universal_Real);
12158 elsif Is_Numeric_Type (Typ)
12159 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
12160 and then (Etype (Right_Opnd (Operand)) = Universal_Real
12161 or else
12162 Etype (Left_Opnd (Operand)) = Universal_Real)
12163 then
12164 -- Return if expression is ambiguous
12166 if Unique_Fixed_Point_Type (N) = Any_Type then
12167 return;
12169 -- If nothing else, the available fixed type is Duration
12171 else
12172 Set_Etype (Operand, Standard_Duration);
12173 end if;
12175 -- Resolve the real operand with largest available precision
12177 if Etype (Right_Opnd (Operand)) = Universal_Real then
12178 Rop := New_Copy_Tree (Right_Opnd (Operand));
12179 else
12180 Rop := New_Copy_Tree (Left_Opnd (Operand));
12181 end if;
12183 Resolve (Rop, Universal_Real);
12185 -- If the operand is a literal (it could be a non-static and
12186 -- illegal exponentiation) check whether the use of Duration
12187 -- is potentially inaccurate.
12189 if Nkind (Rop) = N_Real_Literal
12190 and then Realval (Rop) /= Ureal_0
12191 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
12192 then
12193 Error_Msg_N
12194 ("??universal real operand can only "
12195 & "be interpreted as Duration!", Rop);
12196 Error_Msg_N
12197 ("\??precision will be lost in the conversion!", Rop);
12198 end if;
12200 elsif Is_Numeric_Type (Typ)
12201 and then Nkind (Operand) in N_Op
12202 and then Unique_Fixed_Point_Type (N) /= Any_Type
12203 then
12204 Set_Etype (Operand, Standard_Duration);
12206 else
12207 Error_Msg_N ("invalid context for mixed mode operation", N);
12208 Set_Etype (Operand, Any_Type);
12209 return;
12210 end if;
12211 end if;
12213 Resolve (Operand);
12215 Analyze_Dimension (N);
12217 -- Note: we do the Eval_Type_Conversion call before applying the
12218 -- required checks for a subtype conversion. This is important, since
12219 -- both are prepared under certain circumstances to change the type
12220 -- conversion to a constraint error node, but in the case of
12221 -- Eval_Type_Conversion this may reflect an illegality in the static
12222 -- case, and we would miss the illegality (getting only a warning
12223 -- message), if we applied the type conversion checks first.
12225 Eval_Type_Conversion (N);
12227 -- Even when evaluation is not possible, we may be able to simplify the
12228 -- conversion or its expression. This needs to be done before applying
12229 -- checks, since otherwise the checks may use the original expression
12230 -- and defeat the simplifications. This is specifically the case for
12231 -- elimination of the floating-point Truncation attribute in
12232 -- float-to-int conversions.
12234 Simplify_Type_Conversion (N);
12236 -- If after evaluation we still have a type conversion, then we may need
12237 -- to apply checks required for a subtype conversion. But skip them if
12238 -- universal fixed operands are involved, since range checks are handled
12239 -- separately for these cases, after the expansion done by Exp_Fixd.
12241 if Nkind (N) = N_Type_Conversion
12242 and then not Is_Generic_Type (Root_Type (Target_Typ))
12243 and then Target_Typ /= Universal_Fixed
12244 and then Etype (Operand) /= Universal_Fixed
12245 then
12246 Apply_Type_Conversion_Checks (N);
12247 end if;
12249 -- Issue warning for conversion of simple object to its own type. We
12250 -- have to test the original nodes, since they may have been rewritten
12251 -- by various optimizations.
12253 Orig_N := Original_Node (N);
12255 -- Here we test for a redundant conversion if the warning mode is
12256 -- active (and was not locally reset), and we have a type conversion
12257 -- from source not appearing in a generic instance.
12259 if Test_Redundant
12260 and then Nkind (Orig_N) = N_Type_Conversion
12261 and then Comes_From_Source (Orig_N)
12262 and then not In_Instance
12263 then
12264 Orig_N := Original_Node (Expression (Orig_N));
12265 Orig_T := Target_Typ;
12267 -- If the node is part of a larger expression, the Target_Type
12268 -- may not be the original type of the node if the context is a
12269 -- condition. Recover original type to see if conversion is needed.
12271 if Is_Boolean_Type (Orig_T)
12272 and then Nkind (Parent (N)) in N_Op
12273 then
12274 Orig_T := Etype (Parent (N));
12275 end if;
12277 -- If we have an entity name, then give the warning if the entity
12278 -- is the right type, or if it is a loop parameter covered by the
12279 -- original type (that's needed because loop parameters have an
12280 -- odd subtype coming from the bounds).
12282 if (Is_Entity_Name (Orig_N)
12283 and then Present (Entity (Orig_N))
12284 and then
12285 (Etype (Entity (Orig_N)) = Orig_T
12286 or else
12287 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
12288 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
12290 -- If not an entity, then type of expression must match
12292 or else Etype (Orig_N) = Orig_T
12293 then
12294 -- One more check, do not give warning if the analyzed conversion
12295 -- has an expression with non-static bounds, and the bounds of the
12296 -- target are static. This avoids junk warnings in cases where the
12297 -- conversion is necessary to establish staticness, for example in
12298 -- a case statement.
12300 if not Is_OK_Static_Subtype (Operand_Typ)
12301 and then Is_OK_Static_Subtype (Target_Typ)
12302 then
12303 null;
12305 -- Never give a warning if the operand is a conditional expression
12306 -- because RM 4.5.7(10/3) forces its type to be the target type.
12308 elsif Nkind (Orig_N) in N_Case_Expression | N_If_Expression then
12309 null;
12311 -- Finally, if this type conversion occurs in a context requiring
12312 -- a prefix, and the expression is a qualified expression then the
12313 -- type conversion is not redundant, since a qualified expression
12314 -- is not a prefix, whereas a type conversion is. For example, "X
12315 -- := T'(Funx(...)).Y;" is illegal because a selected component
12316 -- requires a prefix, but a type conversion makes it legal: "X :=
12317 -- T(T'(Funx(...))).Y;"
12319 -- In Ada 2012, a qualified expression is a name, so this idiom is
12320 -- no longer needed, but we still suppress the warning because it
12321 -- seems unfriendly for warnings to pop up when you switch to the
12322 -- newer language version.
12324 elsif Nkind (Orig_N) = N_Qualified_Expression
12325 and then Nkind (Parent (N)) in N_Attribute_Reference
12326 | N_Indexed_Component
12327 | N_Selected_Component
12328 | N_Slice
12329 | N_Explicit_Dereference
12330 then
12331 null;
12333 -- Never warn on conversion to Long_Long_Integer'Base since
12334 -- that is most likely an artifact of the extended overflow
12335 -- checking and comes from complex expanded code.
12337 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
12338 null;
12340 -- Here we give the redundant conversion warning. If it is an
12341 -- entity, give the name of the entity in the message. If not,
12342 -- just mention the expression.
12344 else
12345 if Is_Entity_Name (Orig_N) then
12346 Error_Msg_Node_2 := Orig_T;
12347 Error_Msg_NE -- CODEFIX
12348 ("?r?redundant conversion, & is of type &!",
12349 N, Entity (Orig_N));
12350 else
12351 Error_Msg_NE
12352 ("?r?redundant conversion, expression is of type&!",
12353 N, Orig_T);
12354 end if;
12355 end if;
12356 end if;
12357 end if;
12359 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
12360 -- No need to perform any interface conversion if the type of the
12361 -- expression coincides with the target type.
12363 if Ada_Version >= Ada_2005
12364 and then Expander_Active
12365 and then Operand_Typ /= Target_Typ
12366 then
12367 declare
12368 Opnd : Entity_Id := Operand_Typ;
12369 Target : Entity_Id := Target_Typ;
12371 begin
12372 -- If the type of the operand is a limited view, use nonlimited
12373 -- view when available. If it is a class-wide type, recover the
12374 -- class-wide type of the nonlimited view.
12376 if From_Limited_With (Opnd)
12377 and then Has_Non_Limited_View (Opnd)
12378 then
12379 Opnd := Non_Limited_View (Opnd);
12380 Set_Etype (Expression (N), Opnd);
12381 end if;
12383 -- It seems that Non_Limited_View should also be applied for
12384 -- Target when it has a limited view, but that leads to missing
12385 -- error checks on interface conversions further below. ???
12387 if Is_Access_Type (Opnd) then
12388 Opnd := Designated_Type (Opnd);
12390 -- If the type of the operand is a limited view, use nonlimited
12391 -- view when available. If it is a class-wide type, recover the
12392 -- class-wide type of the nonlimited view.
12394 if From_Limited_With (Opnd)
12395 and then Has_Non_Limited_View (Opnd)
12396 then
12397 Opnd := Non_Limited_View (Opnd);
12398 end if;
12399 end if;
12401 if Is_Access_Type (Target_Typ) then
12402 Target := Designated_Type (Target);
12404 -- If the target type is a limited view, use nonlimited view
12405 -- when available.
12407 if From_Limited_With (Target)
12408 and then Has_Non_Limited_View (Target)
12409 then
12410 Target := Non_Limited_View (Target);
12411 end if;
12412 end if;
12414 if Opnd = Target then
12415 null;
12417 -- Conversion from interface type
12419 -- It seems that it would be better for the error checks below
12420 -- to be performed as part of Validate_Conversion (and maybe some
12421 -- of the error checks above could be moved as well?). ???
12423 elsif Is_Interface (Opnd) then
12425 -- Ada 2005 (AI-217): Handle entities from limited views
12427 if From_Limited_With (Opnd) then
12428 Error_Msg_Qual_Level := 99;
12429 Error_Msg_NE -- CODEFIX
12430 ("missing WITH clause on package &", N,
12431 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
12432 Error_Msg_N
12433 ("type conversions require visibility of the full view",
12436 elsif From_Limited_With (Target)
12437 and then not
12438 (Is_Access_Type (Target_Typ)
12439 and then Present (Non_Limited_View (Etype (Target))))
12440 then
12441 Error_Msg_Qual_Level := 99;
12442 Error_Msg_NE -- CODEFIX
12443 ("missing WITH clause on package &", N,
12444 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12445 Error_Msg_N
12446 ("type conversions require visibility of the full view",
12449 else
12450 Expand_Interface_Conversion (N);
12451 end if;
12453 -- Conversion to interface type
12455 elsif Is_Interface (Target) then
12456 Expand_Interface_Conversion (N);
12457 end if;
12458 end;
12459 end if;
12461 -- Ada 2012: Once the type conversion is resolved, check whether the
12462 -- operand satisfies a static predicate of the target subtype, if any.
12463 -- In the static expression case, a predicate check failure is an error.
12465 if Has_Predicates (Target_Typ) then
12466 Check_Expression_Against_Static_Predicate
12467 (N, Target_Typ, Static_Failure_Is_Error => True);
12468 end if;
12470 -- If at this stage we have a fixed to integer conversion, make sure the
12471 -- Do_Range_Check flag is set, because such conversions in general need
12472 -- a range check. We only need this if expansion is off, see above why.
12474 if Nkind (N) = N_Type_Conversion
12475 and then not Expander_Active
12476 and then Is_Integer_Type (Target_Typ)
12477 and then Is_Fixed_Point_Type (Operand_Typ)
12478 and then not Range_Checks_Suppressed (Target_Typ)
12479 and then not Range_Checks_Suppressed (Operand_Typ)
12480 then
12481 Set_Do_Range_Check (Operand);
12482 end if;
12484 -- Generating C code a type conversion of an access to constrained
12485 -- array type to access to unconstrained array type involves building
12486 -- a fat pointer which in general cannot be generated on the fly. We
12487 -- remove side effects in order to store the result of the conversion
12488 -- into a temporary.
12490 if Modify_Tree_For_C
12491 and then Nkind (N) = N_Type_Conversion
12492 and then Nkind (Parent (N)) /= N_Object_Declaration
12493 and then Is_Access_Type (Etype (N))
12494 and then Is_Array_Type (Designated_Type (Etype (N)))
12495 and then not Is_Constrained (Designated_Type (Etype (N)))
12496 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
12497 then
12498 Remove_Side_Effects (N);
12499 end if;
12500 end Resolve_Type_Conversion;
12502 ----------------------
12503 -- Resolve_Unary_Op --
12504 ----------------------
12506 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12507 B_Typ : constant Entity_Id := Base_Type (Typ);
12508 R : constant Node_Id := Right_Opnd (N);
12509 OK : Boolean;
12510 Lo : Uint;
12511 Hi : Uint;
12513 begin
12514 -- Deal with intrinsic unary operators
12516 if Comes_From_Source (N)
12517 and then Ekind (Entity (N)) = E_Function
12518 and then Is_Imported (Entity (N))
12519 and then Is_Intrinsic_Subprogram (Entity (N))
12520 then
12521 Resolve_Intrinsic_Unary_Operator (N, Typ);
12522 return;
12523 end if;
12525 -- Deal with universal cases
12527 if Is_Universal_Numeric_Type (Etype (R)) then
12528 Check_For_Visible_Operator (N, B_Typ);
12529 end if;
12531 Set_Etype (N, B_Typ);
12532 Resolve (R, B_Typ);
12534 -- Generate warning for negative literal of a modular type, unless it is
12535 -- enclosed directly in a type qualification or a type conversion, as it
12536 -- is likely not what the user intended. We don't issue the warning for
12537 -- the common use of -1 to denote OxFFFF_FFFF...
12539 if Warn_On_Suspicious_Modulus_Value
12540 and then Nkind (N) = N_Op_Minus
12541 and then Nkind (R) = N_Integer_Literal
12542 and then Is_Modular_Integer_Type (B_Typ)
12543 and then Nkind (Parent (N)) not in N_Qualified_Expression
12544 | N_Type_Conversion
12545 and then Expr_Value (R) > Uint_1
12546 then
12547 Error_Msg_N
12548 ("?.m?negative literal of modular type is in fact positive", N);
12549 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12550 Error_Msg_Uint_2 := Expr_Value (R);
12551 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12552 Error_Msg_N
12553 ("\if you do, use qualification to avoid this warning", N);
12554 end if;
12556 -- Generate warning for expressions like abs (x mod 2)
12558 if Warn_On_Redundant_Constructs
12559 and then Nkind (N) = N_Op_Abs
12560 then
12561 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12563 if OK and then Hi >= Lo and then Lo >= 0 then
12564 Error_Msg_N -- CODEFIX
12565 ("?r?abs applied to known non-negative value has no effect", N);
12566 end if;
12567 end if;
12569 -- Deal with reference generation
12571 Check_Unset_Reference (R);
12572 Generate_Operator_Reference (N, B_Typ);
12573 Analyze_Dimension (N);
12574 Eval_Unary_Op (N);
12576 -- Set overflow checking bit. Much cleverer code needed here eventually
12577 -- and perhaps the Resolve routines should be separated for the various
12578 -- arithmetic operations, since they will need different processing ???
12580 if Nkind (N) in N_Op then
12581 if not Overflow_Checks_Suppressed (Etype (N)) then
12582 Enable_Overflow_Check (N);
12583 end if;
12584 end if;
12586 -- Generate warning for expressions like -5 mod 3 for integers. No need
12587 -- to worry in the floating-point case, since parens do not affect the
12588 -- result so there is no point in giving in a warning.
12590 declare
12591 Norig : constant Node_Id := Original_Node (N);
12592 Rorig : Node_Id;
12593 Val : Uint;
12594 HB : Uint;
12595 LB : Uint;
12596 Lval : Uint;
12597 Opnd : Node_Id;
12599 begin
12600 if Warn_On_Questionable_Missing_Parens
12601 and then Comes_From_Source (Norig)
12602 and then Is_Integer_Type (Typ)
12603 and then Nkind (Norig) = N_Op_Minus
12604 then
12605 Rorig := Original_Node (Right_Opnd (Norig));
12607 -- We are looking for cases where the right operand is not
12608 -- parenthesized, and is a binary operator, multiply, divide, or
12609 -- mod. These are the cases where the grouping can affect results.
12611 if Paren_Count (Rorig) = 0
12612 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12613 then
12614 -- For mod, we always give the warning, since the value is
12615 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12616 -- -(5 mod 315)). But for the other cases, the only concern is
12617 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12618 -- overflows, but (-2) * 64 does not). So we try to give the
12619 -- message only when overflow is possible.
12621 if Nkind (Rorig) /= N_Op_Mod
12622 and then Compile_Time_Known_Value (R)
12623 then
12624 Val := Expr_Value (R);
12626 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12627 HB := Expr_Value (Type_High_Bound (Typ));
12628 else
12629 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12630 end if;
12632 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12633 LB := Expr_Value (Type_Low_Bound (Typ));
12634 else
12635 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12636 end if;
12638 -- Note that the test below is deliberately excluding the
12639 -- largest negative number, since that is a potentially
12640 -- troublesome case (e.g. -2 * x, where the result is the
12641 -- largest negative integer has an overflow with 2 * x).
12643 if Val > LB and then Val <= HB then
12644 return;
12645 end if;
12646 end if;
12648 -- For the multiplication case, the only case we have to worry
12649 -- about is when (-a)*b is exactly the largest negative number
12650 -- so that -(a*b) can cause overflow. This can only happen if
12651 -- a is a power of 2, and more generally if any operand is a
12652 -- constant that is not a power of 2, then the parentheses
12653 -- cannot affect whether overflow occurs. We only bother to
12654 -- test the left most operand
12656 -- Loop looking at left operands for one that has known value
12658 Opnd := Rorig;
12659 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12660 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12661 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12663 -- Operand value of 0 or 1 skips warning
12665 if Lval <= 1 then
12666 return;
12668 -- Otherwise check power of 2, if power of 2, warn, if
12669 -- anything else, skip warning.
12671 else
12672 while Lval /= 2 loop
12673 if Lval mod 2 = 1 then
12674 return;
12675 else
12676 Lval := Lval / 2;
12677 end if;
12678 end loop;
12680 exit Opnd_Loop;
12681 end if;
12682 end if;
12684 -- Keep looking at left operands
12686 Opnd := Left_Opnd (Opnd);
12687 end loop Opnd_Loop;
12689 -- For rem or "/" we can only have a problematic situation
12690 -- if the divisor has a value of minus one or one. Otherwise
12691 -- overflow is impossible (divisor > 1) or we have a case of
12692 -- division by zero in any case.
12694 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12695 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12696 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12697 then
12698 return;
12699 end if;
12701 -- If we fall through warning should be issued
12703 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12705 Error_Msg_N
12706 ("??unary minus expression should be parenthesized here!", N);
12707 end if;
12708 end if;
12709 end;
12710 end Resolve_Unary_Op;
12712 ----------------------------------
12713 -- Resolve_Unchecked_Expression --
12714 ----------------------------------
12716 procedure Resolve_Unchecked_Expression
12717 (N : Node_Id;
12718 Typ : Entity_Id)
12720 begin
12721 Resolve (Expression (N), Typ, Suppress => All_Checks);
12722 Set_Etype (N, Typ);
12723 end Resolve_Unchecked_Expression;
12725 ---------------------------------------
12726 -- Resolve_Unchecked_Type_Conversion --
12727 ---------------------------------------
12729 procedure Resolve_Unchecked_Type_Conversion
12730 (N : Node_Id;
12731 Typ : Entity_Id)
12733 pragma Warnings (Off, Typ);
12735 Operand : constant Node_Id := Expression (N);
12736 Opnd_Type : constant Entity_Id := Etype (Operand);
12738 begin
12739 -- Resolve operand using its own type
12741 Resolve (Operand, Opnd_Type);
12743 -- If the expression is a conversion to universal integer of an
12744 -- an expression with an integer type, then we can eliminate the
12745 -- intermediate conversion to universal integer.
12747 if Nkind (Operand) = N_Type_Conversion
12748 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12749 and then Is_Integer_Type (Etype (Expression (Operand)))
12750 then
12751 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12752 Analyze_And_Resolve (Operand);
12753 end if;
12755 -- In an inlined context, the unchecked conversion may be applied
12756 -- to a literal, in which case its type is the type of the context.
12757 -- (In other contexts conversions cannot apply to literals).
12759 if In_Inlined_Body
12760 and then (Opnd_Type = Any_Character or else
12761 Opnd_Type = Any_Integer or else
12762 Opnd_Type = Any_Real)
12763 then
12764 Set_Etype (Operand, Typ);
12765 end if;
12767 Analyze_Dimension (N);
12768 Eval_Unchecked_Conversion (N);
12769 end Resolve_Unchecked_Type_Conversion;
12771 ------------------------------
12772 -- Rewrite_Operator_As_Call --
12773 ------------------------------
12775 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12776 Loc : constant Source_Ptr := Sloc (N);
12777 Actuals : constant List_Id := New_List;
12778 New_N : Node_Id;
12780 begin
12781 if Nkind (N) in N_Binary_Op then
12782 Append (Left_Opnd (N), Actuals);
12783 end if;
12785 Append (Right_Opnd (N), Actuals);
12787 New_N :=
12788 Make_Function_Call (Sloc => Loc,
12789 Name => New_Occurrence_Of (Nam, Loc),
12790 Parameter_Associations => Actuals);
12792 Preserve_Comes_From_Source (New_N, N);
12793 Preserve_Comes_From_Source (Name (New_N), N);
12794 Rewrite (N, New_N);
12795 Set_Etype (N, Etype (Nam));
12796 end Rewrite_Operator_As_Call;
12798 ------------------------------
12799 -- Rewrite_Renamed_Operator --
12800 ------------------------------
12802 procedure Rewrite_Renamed_Operator
12803 (N : Node_Id;
12804 Op : Entity_Id;
12805 Typ : Entity_Id)
12807 Nam : constant Name_Id := Chars (Op);
12808 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12809 Op_Node : Node_Id;
12811 begin
12812 -- Do not perform this transformation within a pre/postcondition,
12813 -- because the expression will be reanalyzed, and the transformation
12814 -- might affect the visibility of the operator, e.g. in an instance.
12815 -- Note that fully analyzed and expanded pre/postconditions appear as
12816 -- pragma Check equivalents.
12818 if In_Pre_Post_Condition (N) then
12819 return;
12820 end if;
12822 -- Likewise when an expression function is being preanalyzed, since the
12823 -- expression will be reanalyzed as part of the generated body.
12825 if In_Spec_Expression then
12826 declare
12827 S : constant Entity_Id := Current_Scope_No_Loops;
12828 begin
12829 if Ekind (S) = E_Function
12830 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12831 N_Expression_Function
12832 then
12833 return;
12834 end if;
12835 end;
12836 end if;
12838 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12839 Set_Chars (Op_Node, Nam);
12840 Set_Etype (Op_Node, Etype (N));
12841 Set_Entity (Op_Node, Op);
12842 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12844 if Is_Binary then
12845 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12846 end if;
12848 -- Indicate that both the original entity and its renaming are
12849 -- referenced at this point.
12851 Generate_Reference (Entity (N), N);
12852 Generate_Reference (Op, N);
12854 Rewrite (N, Op_Node);
12856 -- If the context type is private, add the appropriate conversions so
12857 -- that the operator is applied to the full view. This is done in the
12858 -- routines that resolve intrinsic operators.
12860 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12861 case Nkind (N) is
12862 when N_Op_Add
12863 | N_Op_Divide
12864 | N_Op_Expon
12865 | N_Op_Mod
12866 | N_Op_Multiply
12867 | N_Op_Rem
12868 | N_Op_Subtract
12870 Resolve_Intrinsic_Operator (N, Typ);
12872 when N_Op_Abs
12873 | N_Op_Minus
12874 | N_Op_Plus
12876 Resolve_Intrinsic_Unary_Operator (N, Typ);
12878 when others =>
12879 Resolve (N, Typ);
12880 end case;
12881 end if;
12882 end Rewrite_Renamed_Operator;
12884 -----------------------
12885 -- Set_Slice_Subtype --
12886 -----------------------
12888 -- Build an implicit subtype declaration to represent the type delivered by
12889 -- the slice. This is an abbreviated version of an array subtype. We define
12890 -- an index subtype for the slice, using either the subtype name or the
12891 -- discrete range of the slice. To be consistent with index usage elsewhere
12892 -- we create a list header to hold the single index. This list is not
12893 -- otherwise attached to the syntax tree.
12895 procedure Set_Slice_Subtype (N : Node_Id) is
12896 Loc : constant Source_Ptr := Sloc (N);
12897 Index_List : constant List_Id := New_List;
12898 Index : Node_Id;
12899 Index_Subtype : Entity_Id;
12900 Index_Type : Entity_Id;
12901 Slice_Subtype : Entity_Id;
12902 Drange : constant Node_Id := Discrete_Range (N);
12904 begin
12905 Index_Type := Base_Type (Etype (Drange));
12907 if Is_Entity_Name (Drange) then
12908 Index_Subtype := Entity (Drange);
12910 else
12911 -- We force the evaluation of a range. This is definitely needed in
12912 -- the renamed case, and seems safer to do unconditionally. Note in
12913 -- any case that since we will create and insert an Itype referring
12914 -- to this range, we must make sure any side effect removal actions
12915 -- are inserted before the Itype definition.
12917 if Nkind (Drange) = N_Range then
12918 Force_Evaluation (Low_Bound (Drange));
12919 Force_Evaluation (High_Bound (Drange));
12921 -- If the discrete range is given by a subtype indication, the
12922 -- type of the slice is the base of the subtype mark.
12924 elsif Nkind (Drange) = N_Subtype_Indication then
12925 declare
12926 R : constant Node_Id := Range_Expression (Constraint (Drange));
12927 begin
12928 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12929 Force_Evaluation (Low_Bound (R));
12930 Force_Evaluation (High_Bound (R));
12931 end;
12932 end if;
12934 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12936 -- Take a new copy of Drange (where bounds have been rewritten to
12937 -- reference side-effect-free names). Using a separate tree ensures
12938 -- that further expansion (e.g. while rewriting a slice assignment
12939 -- into a FOR loop) does not attempt to remove side effects on the
12940 -- bounds again (which would cause the bounds in the index subtype
12941 -- definition to refer to temporaries before they are defined) (the
12942 -- reason is that some names are considered side effect free here
12943 -- for the subtype, but not in the context of a loop iteration
12944 -- scheme).
12946 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12947 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12948 Set_Etype (Index_Subtype, Index_Type);
12949 Set_Size_Info (Index_Subtype, Index_Type);
12950 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12951 Set_Is_Constrained (Index_Subtype);
12952 end if;
12954 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12956 Index := New_Occurrence_Of (Index_Subtype, Loc);
12957 Set_Etype (Index, Index_Subtype);
12958 Append (Index, Index_List);
12960 Set_First_Index (Slice_Subtype, Index);
12961 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12962 Set_Is_Constrained (Slice_Subtype, True);
12964 Check_Compile_Time_Size (Slice_Subtype);
12966 -- The Etype of the existing Slice node is reset to this slice subtype.
12967 -- Its bounds are obtained from its first index.
12969 Set_Etype (N, Slice_Subtype);
12971 -- For bit-packed slice subtypes, freeze immediately (except in the case
12972 -- of being in a "spec expression" where we never freeze when we first
12973 -- see the expression).
12975 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12976 Freeze_Itype (Slice_Subtype, N);
12978 -- For all other cases insert an itype reference in the slice's actions
12979 -- so that the itype is frozen at the proper place in the tree (i.e. at
12980 -- the point where actions for the slice are analyzed). Note that this
12981 -- is different from freezing the itype immediately, which might be
12982 -- premature (e.g. if the slice is within a transient scope). This needs
12983 -- to be done only if expansion is enabled.
12985 elsif Expander_Active then
12986 Ensure_Defined (Typ => Slice_Subtype, N => N);
12987 end if;
12988 end Set_Slice_Subtype;
12990 --------------------------------
12991 -- Set_String_Literal_Subtype --
12992 --------------------------------
12994 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12995 Loc : constant Source_Ptr := Sloc (N);
12996 Low_Bound : constant Node_Id :=
12997 Type_Low_Bound (Etype (First_Index (Typ)));
12998 Subtype_Id : Entity_Id;
13000 begin
13001 if Nkind (N) /= N_String_Literal then
13002 return;
13003 end if;
13005 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
13006 Set_String_Literal_Length (Subtype_Id, UI_From_Int
13007 (String_Length (Strval (N))));
13008 Set_Etype (Subtype_Id, Base_Type (Typ));
13009 Set_Is_Constrained (Subtype_Id);
13010 Set_Etype (N, Subtype_Id);
13012 -- The low bound is set from the low bound of the corresponding index
13013 -- type. Note that we do not store the high bound in the string literal
13014 -- subtype, but it can be deduced if necessary from the length and the
13015 -- low bound.
13017 if Is_OK_Static_Expression (Low_Bound) then
13018 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
13020 -- If the lower bound is not static we create a range for the string
13021 -- literal, using the index type and the known length of the literal.
13022 -- If the length is 1, then the upper bound is set to a mere copy of
13023 -- the lower bound; or else, if the index type is a signed integer,
13024 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
13025 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
13027 else
13028 declare
13029 Length : constant Nat := String_Length (Strval (N));
13030 Index_List : constant List_Id := New_List;
13031 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
13032 Array_Subtype : Entity_Id;
13033 Drange : Node_Id;
13034 High_Bound : Node_Id;
13035 Index : Node_Id;
13036 Index_Subtype : Entity_Id;
13038 begin
13039 if Length = 1 then
13040 High_Bound := New_Copy_Tree (Low_Bound);
13042 elsif Is_Signed_Integer_Type (Index_Type) then
13043 High_Bound :=
13044 Make_Op_Add (Loc,
13045 Left_Opnd => New_Copy_Tree (Low_Bound),
13046 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
13048 else
13049 High_Bound :=
13050 Make_Attribute_Reference (Loc,
13051 Attribute_Name => Name_Val,
13052 Prefix =>
13053 New_Occurrence_Of (Index_Type, Loc),
13054 Expressions => New_List (
13055 Make_Op_Add (Loc,
13056 Left_Opnd =>
13057 Make_Attribute_Reference (Loc,
13058 Attribute_Name => Name_Pos,
13059 Prefix =>
13060 New_Occurrence_Of (Index_Type, Loc),
13061 Expressions =>
13062 New_List (New_Copy_Tree (Low_Bound))),
13063 Right_Opnd =>
13064 Make_Integer_Literal (Loc, Length - 1))));
13065 end if;
13067 if Is_Integer_Type (Index_Type) then
13068 Set_String_Literal_Low_Bound
13069 (Subtype_Id, Make_Integer_Literal (Loc, 1));
13071 else
13072 -- If the index type is an enumeration type, build bounds
13073 -- expression with attributes.
13075 Set_String_Literal_Low_Bound
13076 (Subtype_Id,
13077 Make_Attribute_Reference (Loc,
13078 Attribute_Name => Name_First,
13079 Prefix =>
13080 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
13081 end if;
13083 Analyze_And_Resolve
13084 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
13086 -- Build bona fide subtype for the string, and wrap it in an
13087 -- unchecked conversion, because the back end expects the
13088 -- String_Literal_Subtype to have a static lower bound.
13090 Index_Subtype :=
13091 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13092 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
13093 Set_Scalar_Range (Index_Subtype, Drange);
13094 Set_Parent (Drange, N);
13095 Analyze_And_Resolve (Drange, Index_Type);
13097 -- In this context, the Index_Type may already have a constraint,
13098 -- so use common base type on string subtype. The base type may
13099 -- be used when generating attributes of the string, for example
13100 -- in the context of a slice assignment.
13102 Set_Etype (Index_Subtype, Base_Type (Index_Type));
13103 Set_Size_Info (Index_Subtype, Index_Type);
13104 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13106 Array_Subtype := Create_Itype (E_Array_Subtype, N);
13108 Index := New_Occurrence_Of (Index_Subtype, Loc);
13109 Set_Etype (Index, Index_Subtype);
13110 Append (Index, Index_List);
13112 Set_First_Index (Array_Subtype, Index);
13113 Set_Etype (Array_Subtype, Base_Type (Typ));
13114 Set_Is_Constrained (Array_Subtype, True);
13116 Rewrite (N, Unchecked_Convert_To (Array_Subtype, N));
13117 Set_Etype (N, Array_Subtype);
13118 end;
13119 end if;
13120 end Set_String_Literal_Subtype;
13122 ------------------------------
13123 -- Simplify_Type_Conversion --
13124 ------------------------------
13126 procedure Simplify_Type_Conversion (N : Node_Id) is
13127 begin
13128 if Nkind (N) = N_Type_Conversion then
13129 declare
13130 Operand : constant Node_Id := Expression (N);
13131 Target_Typ : constant Entity_Id := Etype (N);
13132 Opnd_Typ : constant Entity_Id := Etype (Operand);
13134 begin
13135 -- Special processing if the conversion is the expression of a
13136 -- Rounding or Truncation attribute reference. In this case we
13137 -- replace:
13139 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
13141 -- by
13143 -- ityp (x)
13145 -- with the Float_Truncate flag set to False or True respectively,
13146 -- which is more efficient. We reuse Rounding for Machine_Rounding
13147 -- as System.Fat_Gen, which is a permissible behavior.
13149 if Is_Floating_Point_Type (Opnd_Typ)
13150 and then
13151 (Is_Integer_Type (Target_Typ)
13152 or else (Is_Fixed_Point_Type (Target_Typ)
13153 and then Conversion_OK (N)))
13154 and then Nkind (Operand) = N_Attribute_Reference
13155 and then Attribute_Name (Operand) in Name_Rounding
13156 | Name_Machine_Rounding
13157 | Name_Truncation
13158 then
13159 declare
13160 Truncate : constant Boolean :=
13161 Attribute_Name (Operand) = Name_Truncation;
13162 begin
13163 Rewrite (Operand,
13164 Relocate_Node (First (Expressions (Operand))));
13165 Set_Float_Truncate (N, Truncate);
13166 end;
13168 -- Special processing for the conversion of an integer literal to
13169 -- a dynamic type: we first convert the literal to the root type
13170 -- and then convert the result to the target type, the goal being
13171 -- to avoid doing range checks in universal integer.
13173 elsif Is_Integer_Type (Target_Typ)
13174 and then not Is_Generic_Type (Root_Type (Target_Typ))
13175 and then Nkind (Operand) = N_Integer_Literal
13176 and then Opnd_Typ = Universal_Integer
13177 then
13178 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
13179 Analyze_And_Resolve (Operand);
13181 -- If the expression is a conversion to universal integer of an
13182 -- an expression with an integer type, then we can eliminate the
13183 -- intermediate conversion to universal integer.
13185 elsif Nkind (Operand) = N_Type_Conversion
13186 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
13187 and then Is_Integer_Type (Etype (Expression (Operand)))
13188 then
13189 Rewrite (Operand, Relocate_Node (Expression (Operand)));
13190 Analyze_And_Resolve (Operand);
13191 end if;
13192 end;
13193 end if;
13194 end Simplify_Type_Conversion;
13196 ------------------------------
13197 -- Try_User_Defined_Literal --
13198 ------------------------------
13200 function Try_User_Defined_Literal
13201 (N : Node_Id;
13202 Typ : Entity_Id) return Boolean
13204 begin
13205 if Nkind (N) in N_Op_Add | N_Op_Divide | N_Op_Mod | N_Op_Multiply
13206 | N_Op_Rem | N_Op_Subtract
13207 then
13209 -- Both operands must have the same type as the context.
13210 -- (ignoring for now fixed-point and exponentiation ops).
13212 if Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ) then
13213 Resolve (Left_Opnd (N), Typ);
13214 Analyze_And_Resolve (N, Typ);
13215 return True;
13216 end if;
13219 Has_Applicable_User_Defined_Literal (Left_Opnd (N), Typ)
13220 then
13221 Resolve (Right_Opnd (N), Typ);
13222 Analyze_And_Resolve (N, Typ);
13223 return True;
13225 else
13226 return False;
13227 end if;
13229 elsif Nkind (N) in N_Binary_Op then
13230 -- For other operators the context does not impose a type on
13231 -- the operands, but their types must match.
13233 if (Nkind (Left_Opnd (N))
13234 not in N_Integer_Literal | N_String_Literal | N_Real_Literal)
13235 and then
13236 Has_Applicable_User_Defined_Literal
13237 (Right_Opnd (N), Etype (Left_Opnd (N)))
13238 then
13239 Analyze_And_Resolve (N, Typ);
13240 return True;
13242 elsif (Nkind (Right_Opnd (N))
13243 not in N_Integer_Literal | N_String_Literal | N_Real_Literal)
13244 and then
13245 Has_Applicable_User_Defined_Literal
13246 (Left_Opnd (N), Etype (Right_Opnd (N)))
13247 then
13248 Analyze_And_Resolve (N, Typ);
13249 return True;
13250 else
13251 return False;
13252 end if;
13254 elsif Nkind (N) in N_Unary_Op
13255 and then
13256 Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13257 then
13258 Analyze_And_Resolve (N, Typ);
13259 return True;
13261 else -- Other operators
13262 return False;
13263 end if;
13264 end Try_User_Defined_Literal;
13266 -----------------------------
13267 -- Unique_Fixed_Point_Type --
13268 -----------------------------
13270 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
13271 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
13272 -- Give error messages for true ambiguity. Messages are posted on node
13273 -- N, and entities T1, T2 are the possible interpretations.
13275 -----------------------
13276 -- Fixed_Point_Error --
13277 -----------------------
13279 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
13280 begin
13281 Error_Msg_N ("ambiguous universal_fixed_expression", N);
13282 Error_Msg_NE ("\\possible interpretation as}", N, T1);
13283 Error_Msg_NE ("\\possible interpretation as}", N, T2);
13284 end Fixed_Point_Error;
13286 -- Local variables
13288 ErrN : Node_Id;
13289 Item : Node_Id;
13290 Scop : Entity_Id;
13291 T1 : Entity_Id;
13292 T2 : Entity_Id;
13294 -- Start of processing for Unique_Fixed_Point_Type
13296 begin
13297 -- The operations on Duration are visible, so Duration is always a
13298 -- possible interpretation.
13300 T1 := Standard_Duration;
13302 -- Look for fixed-point types in enclosing scopes
13304 Scop := Current_Scope;
13305 while Scop /= Standard_Standard loop
13306 T2 := First_Entity (Scop);
13307 while Present (T2) loop
13308 if Is_Fixed_Point_Type (T2)
13309 and then Current_Entity (T2) = T2
13310 and then Scope (Base_Type (T2)) = Scop
13311 then
13312 if Present (T1) then
13313 Fixed_Point_Error (T1, T2);
13314 return Any_Type;
13315 else
13316 T1 := T2;
13317 end if;
13318 end if;
13320 Next_Entity (T2);
13321 end loop;
13323 Scop := Scope (Scop);
13324 end loop;
13326 -- Look for visible fixed type declarations in the context
13328 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
13329 while Present (Item) loop
13330 if Nkind (Item) = N_With_Clause then
13331 Scop := Entity (Name (Item));
13332 T2 := First_Entity (Scop);
13333 while Present (T2) loop
13334 if Is_Fixed_Point_Type (T2)
13335 and then Scope (Base_Type (T2)) = Scop
13336 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
13337 then
13338 if Present (T1) then
13339 Fixed_Point_Error (T1, T2);
13340 return Any_Type;
13341 else
13342 T1 := T2;
13343 end if;
13344 end if;
13346 Next_Entity (T2);
13347 end loop;
13348 end if;
13350 Next (Item);
13351 end loop;
13353 if Nkind (N) = N_Real_Literal then
13354 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
13356 else
13357 -- When the context is a type conversion, issue the warning on the
13358 -- expression of the conversion because it is the actual operation.
13360 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
13361 ErrN := Expression (N);
13362 else
13363 ErrN := N;
13364 end if;
13366 Error_Msg_NE
13367 ("??universal_fixed expression interpreted as }!", ErrN, T1);
13368 end if;
13370 return T1;
13371 end Unique_Fixed_Point_Type;
13373 ----------------------
13374 -- Valid_Conversion --
13375 ----------------------
13377 function Valid_Conversion
13378 (N : Node_Id;
13379 Target : Entity_Id;
13380 Operand : Node_Id;
13381 Report_Errs : Boolean := True) return Boolean
13383 Target_Type : constant Entity_Id := Base_Type (Target);
13384 Opnd_Type : Entity_Id := Etype (Operand);
13385 Inc_Ancestor : Entity_Id;
13387 function Conversion_Check
13388 (Valid : Boolean;
13389 Msg : String) return Boolean;
13390 -- Little routine to post Msg if Valid is False, returns Valid value
13392 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
13393 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
13395 procedure Conversion_Error_NE
13396 (Msg : String;
13397 N : Node_Or_Entity_Id;
13398 E : Node_Or_Entity_Id);
13399 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
13401 function In_Instance_Code return Boolean;
13402 -- Return True if expression is within an instance but is not in one of
13403 -- the actuals of the instantiation. Type conversions within an instance
13404 -- are not rechecked because type visibility may lead to spurious errors
13405 -- but conversions in an actual for a formal object must be checked.
13407 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13408 (Expr : Node_Id) return Boolean;
13409 -- Implicit anonymous-to-named access type conversions are not allowed
13410 -- if the "statically deeper than" relationship does not apply to the
13411 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
13412 -- We deal with most such cases elsewhere so that we can emit more
13413 -- specific error messages (e.g., if the operand is an access parameter
13414 -- or a saooaaat (stand-alone object of an anonymous access type)), but
13415 -- here is where we catch the case where the operand is an access
13416 -- discriminant selected from a dereference of another such "bad"
13417 -- conversion argument.
13419 function Valid_Tagged_Conversion
13420 (Target_Type : Entity_Id;
13421 Opnd_Type : Entity_Id) return Boolean;
13422 -- Specifically test for validity of tagged conversions
13424 function Valid_Array_Conversion return Boolean;
13425 -- Check index and component conformance, and accessibility levels if
13426 -- the component types are anonymous access types (Ada 2005).
13428 ----------------------
13429 -- Conversion_Check --
13430 ----------------------
13432 function Conversion_Check
13433 (Valid : Boolean;
13434 Msg : String) return Boolean
13436 begin
13437 if not Valid
13439 -- A generic unit has already been analyzed and we have verified
13440 -- that a particular conversion is OK in that context. Since the
13441 -- instance is reanalyzed without relying on the relationships
13442 -- established during the analysis of the generic, it is possible
13443 -- to end up with inconsistent views of private types. Do not emit
13444 -- the error message in such cases. The rest of the machinery in
13445 -- Valid_Conversion still ensures the proper compatibility of
13446 -- target and operand types.
13448 and then not In_Instance_Code
13449 then
13450 Conversion_Error_N (Msg, Operand);
13451 end if;
13453 return Valid;
13454 end Conversion_Check;
13456 ------------------------
13457 -- Conversion_Error_N --
13458 ------------------------
13460 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
13461 begin
13462 if Report_Errs then
13463 Error_Msg_N (Msg, N);
13464 end if;
13465 end Conversion_Error_N;
13467 -------------------------
13468 -- Conversion_Error_NE --
13469 -------------------------
13471 procedure Conversion_Error_NE
13472 (Msg : String;
13473 N : Node_Or_Entity_Id;
13474 E : Node_Or_Entity_Id)
13476 begin
13477 if Report_Errs then
13478 Error_Msg_NE (Msg, N, E);
13479 end if;
13480 end Conversion_Error_NE;
13482 ----------------------
13483 -- In_Instance_Code --
13484 ----------------------
13486 function In_Instance_Code return Boolean is
13487 Par : Node_Id;
13489 begin
13490 if not In_Instance then
13491 return False;
13493 else
13494 Par := Parent (N);
13495 while Present (Par) loop
13497 -- The expression is part of an actual object if it appears in
13498 -- the generated object declaration in the instance.
13500 if Nkind (Par) = N_Object_Declaration
13501 and then Present (Corresponding_Generic_Association (Par))
13502 then
13503 return False;
13505 else
13506 exit when
13507 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13508 or else Nkind (Par) in N_Subprogram_Call
13509 or else Nkind (Par) in N_Declaration;
13510 end if;
13512 Par := Parent (Par);
13513 end loop;
13515 -- Otherwise the expression appears within the instantiated unit
13517 return True;
13518 end if;
13519 end In_Instance_Code;
13521 --------------------------------------------------
13522 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13523 --------------------------------------------------
13525 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13526 (Expr : Node_Id) return Boolean
13528 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13529 pragma Assert (Is_Access_Type (Exp_Type));
13531 Associated_Node : Node_Id;
13532 Deref_Prefix : Node_Id;
13533 begin
13534 if not Is_Anonymous_Access_Type (Exp_Type) then
13535 return False;
13536 end if;
13538 pragma Assert (Is_Itype (Exp_Type));
13539 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13541 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13542 return False; -- not the type of an access discriminant
13543 end if;
13545 -- return False if Expr not of form <prefix>.all.Some_Component
13547 if (Nkind (Expr) /= N_Selected_Component)
13548 or else (Nkind (Prefix (Expr)) /= N_Explicit_Dereference)
13549 then
13550 -- conditional expressions, declare expressions ???
13551 return False;
13552 end if;
13554 Deref_Prefix := Prefix (Prefix (Expr));
13555 Exp_Type := Base_Type (Etype (Deref_Prefix));
13557 -- The "statically deeper relationship" does not apply
13558 -- to generic formal access types, so a prefix of such
13559 -- a type is a "bad" prefix.
13561 if Is_Generic_Formal (Exp_Type) then
13562 return True;
13564 -- The "statically deeper relationship" does apply to
13565 -- any other named access type.
13567 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13568 return False;
13569 end if;
13571 pragma Assert (Is_Itype (Exp_Type));
13572 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13574 -- The "statically deeper relationship" applies to some
13575 -- anonymous access types and not to others. Return
13576 -- True for the cases where it does not apply. Also check
13577 -- recursively for the
13578 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13579 -- where the correct result depends on <prefix>.
13581 return Nkind (Associated_Node) in
13582 N_Procedure_Specification | -- access parameter
13583 N_Function_Specification | -- access parameter
13584 N_Object_Declaration -- saooaaat
13585 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13586 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13588 ----------------------------
13589 -- Valid_Array_Conversion --
13590 ----------------------------
13592 function Valid_Array_Conversion return Boolean is
13593 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13594 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13596 Opnd_Index : Node_Id;
13597 Opnd_Index_Type : Entity_Id;
13599 Target_Comp_Type : constant Entity_Id :=
13600 Component_Type (Target_Type);
13601 Target_Comp_Base : constant Entity_Id :=
13602 Base_Type (Target_Comp_Type);
13604 Target_Index : Node_Id;
13605 Target_Index_Type : Entity_Id;
13607 begin
13608 -- Error if wrong number of dimensions
13611 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13612 then
13613 Conversion_Error_N
13614 ("incompatible number of dimensions for conversion", Operand);
13615 return False;
13617 -- Number of dimensions matches
13619 else
13620 -- Loop through indexes of the two arrays
13622 Target_Index := First_Index (Target_Type);
13623 Opnd_Index := First_Index (Opnd_Type);
13624 while Present (Target_Index) and then Present (Opnd_Index) loop
13625 Target_Index_Type := Etype (Target_Index);
13626 Opnd_Index_Type := Etype (Opnd_Index);
13628 -- Error if index types are incompatible
13630 if not (Is_Integer_Type (Target_Index_Type)
13631 and then Is_Integer_Type (Opnd_Index_Type))
13632 and then (Root_Type (Target_Index_Type)
13633 /= Root_Type (Opnd_Index_Type))
13634 then
13635 Conversion_Error_N
13636 ("incompatible index types for array conversion",
13637 Operand);
13638 return False;
13639 end if;
13641 Next_Index (Target_Index);
13642 Next_Index (Opnd_Index);
13643 end loop;
13645 -- If component types have same base type, all set
13647 if Target_Comp_Base = Opnd_Comp_Base then
13648 null;
13650 -- Here if base types of components are not the same. The only
13651 -- time this is allowed is if we have anonymous access types.
13653 -- The conversion of arrays of anonymous access types can lead
13654 -- to dangling pointers. AI-392 formalizes the accessibility
13655 -- checks that must be applied to such conversions to prevent
13656 -- out-of-scope references.
13658 elsif Ekind (Target_Comp_Base) in
13659 E_Anonymous_Access_Type
13660 | E_Anonymous_Access_Subprogram_Type
13661 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13662 and then
13663 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13664 then
13665 if Type_Access_Level (Target_Type) <
13666 Deepest_Type_Access_Level (Opnd_Type)
13667 then
13668 if In_Instance_Body then
13669 Error_Msg_Warn := SPARK_Mode /= On;
13670 Conversion_Error_N
13671 ("source array type has deeper accessibility "
13672 & "level than target<<", Operand);
13673 Conversion_Error_N ("\Program_Error [<<", Operand);
13674 Rewrite (N,
13675 Make_Raise_Program_Error (Sloc (N),
13676 Reason => PE_Accessibility_Check_Failed));
13677 Set_Etype (N, Target_Type);
13678 return False;
13680 -- Conversion not allowed because of accessibility levels
13682 else
13683 Conversion_Error_N
13684 ("source array type has deeper accessibility "
13685 & "level than target", Operand);
13686 return False;
13687 end if;
13689 else
13690 null;
13691 end if;
13693 -- All other cases where component base types do not match
13695 else
13696 Conversion_Error_N
13697 ("incompatible component types for array conversion",
13698 Operand);
13699 return False;
13700 end if;
13702 -- Check that component subtypes statically match. For numeric
13703 -- types this means that both must be either constrained or
13704 -- unconstrained. For enumeration types the bounds must match.
13705 -- All of this is checked in Subtypes_Statically_Match.
13707 if not Subtypes_Statically_Match
13708 (Target_Comp_Type, Opnd_Comp_Type)
13709 then
13710 Conversion_Error_N
13711 ("component subtypes must statically match", Operand);
13712 return False;
13713 end if;
13714 end if;
13716 return True;
13717 end Valid_Array_Conversion;
13719 -----------------------------
13720 -- Valid_Tagged_Conversion --
13721 -----------------------------
13723 function Valid_Tagged_Conversion
13724 (Target_Type : Entity_Id;
13725 Opnd_Type : Entity_Id) return Boolean
13727 begin
13728 -- Upward conversions are allowed (RM 4.6(22))
13730 if Covers (Target_Type, Opnd_Type)
13731 or else Is_Ancestor (Target_Type, Opnd_Type)
13732 then
13733 return True;
13735 -- Downward conversion are allowed if the operand is class-wide
13736 -- (RM 4.6(23)).
13738 elsif Is_Class_Wide_Type (Opnd_Type)
13739 and then Covers (Opnd_Type, Target_Type)
13740 then
13741 return True;
13743 elsif Covers (Opnd_Type, Target_Type)
13744 or else Is_Ancestor (Opnd_Type, Target_Type)
13745 then
13746 return
13747 Conversion_Check (False,
13748 "downward conversion of tagged objects not allowed");
13750 -- Ada 2005 (AI-251): A conversion is valid if the operand and target
13751 -- types are both class-wide types and the specific type associated
13752 -- with at least one of them is an interface type (RM 4.6 (23.1/2));
13753 -- at run-time a check will verify the validity of this interface
13754 -- type conversion.
13756 elsif Is_Class_Wide_Type (Target_Type)
13757 and then Is_Class_Wide_Type (Opnd_Type)
13758 and then (Is_Interface (Target_Type)
13759 or else Is_Interface (Opnd_Type))
13760 then
13761 return True;
13763 -- Report errors
13765 elsif Is_Class_Wide_Type (Target_Type)
13766 and then Is_Interface (Target_Type)
13767 and then not Is_Interface (Opnd_Type)
13768 and then not Interface_Present_In_Ancestor
13769 (Typ => Opnd_Type,
13770 Iface => Target_Type)
13771 then
13772 Error_Msg_Name_1 := Chars (Etype (Target_Type));
13773 Error_Msg_Name_2 := Chars (Opnd_Type);
13774 Conversion_Error_N
13775 ("wrong interface conversion (% is not a progenitor "
13776 & "of %)", N);
13777 return False;
13779 elsif Is_Class_Wide_Type (Opnd_Type)
13780 and then Is_Interface (Opnd_Type)
13781 and then not Is_Interface (Target_Type)
13782 and then not Interface_Present_In_Ancestor
13783 (Typ => Target_Type,
13784 Iface => Opnd_Type)
13785 then
13786 Error_Msg_Name_1 := Chars (Etype (Opnd_Type));
13787 Error_Msg_Name_2 := Chars (Target_Type);
13788 Conversion_Error_N
13789 ("wrong interface conversion (% is not a progenitor "
13790 & "of %)", N);
13792 -- Search for interface types shared between the target type and
13793 -- the operand interface type to complete the text of the error
13794 -- since the source of this error is a missing type conversion
13795 -- to such interface type.
13797 if Has_Interfaces (Target_Type) then
13798 declare
13799 Operand_Ifaces_List : Elist_Id;
13800 Operand_Iface_Elmt : Elmt_Id;
13801 Target_Ifaces_List : Elist_Id;
13802 Target_Iface_Elmt : Elmt_Id;
13803 First_Candidate : Boolean := True;
13805 begin
13806 Collect_Interfaces (Base_Type (Target_Type),
13807 Target_Ifaces_List);
13808 Collect_Interfaces (Root_Type (Base_Type (Opnd_Type)),
13809 Operand_Ifaces_List);
13811 Operand_Iface_Elmt := First_Elmt (Operand_Ifaces_List);
13812 while Present (Operand_Iface_Elmt) loop
13813 Target_Iface_Elmt := First_Elmt (Target_Ifaces_List);
13814 while Present (Target_Iface_Elmt) loop
13815 if Node (Operand_Iface_Elmt)
13816 = Node (Target_Iface_Elmt)
13817 then
13818 Error_Msg_Name_1 :=
13819 Chars (Node (Target_Iface_Elmt));
13821 if First_Candidate then
13822 First_Candidate := False;
13823 Conversion_Error_N
13824 ("\must convert to `%''Class` before downward "
13825 & "conversion", Operand);
13826 else
13827 Conversion_Error_N
13828 ("\or must convert to `%''Class` before "
13829 & "downward conversion", Operand);
13830 end if;
13831 end if;
13833 Next_Elmt (Target_Iface_Elmt);
13834 end loop;
13836 Next_Elmt (Operand_Iface_Elmt);
13837 end loop;
13838 end;
13839 end if;
13841 return False;
13843 elsif not Is_Class_Wide_Type (Target_Type)
13844 and then Is_Interface (Target_Type)
13845 then
13846 Conversion_Error_N
13847 ("wrong use of interface type in tagged conversion", N);
13848 Conversion_Error_N
13849 ("\add ''Class to the target interface type", N);
13850 return False;
13852 elsif not Is_Class_Wide_Type (Opnd_Type)
13853 and then Is_Interface (Opnd_Type)
13854 then
13855 Conversion_Error_N
13856 ("must convert to class-wide interface type before downward "
13857 & "conversion", Operand);
13858 return False;
13860 else
13861 Conversion_Error_NE
13862 ("invalid tagged conversion, not compatible with}",
13863 N, First_Subtype (Opnd_Type));
13864 return False;
13865 end if;
13866 end Valid_Tagged_Conversion;
13868 -- Start of processing for Valid_Conversion
13870 begin
13871 Check_Parameterless_Call (Operand);
13873 if Is_Overloaded (Operand) then
13874 declare
13875 I : Interp_Index;
13876 I1 : Interp_Index;
13877 It : Interp;
13878 It1 : Interp;
13879 N1 : Entity_Id;
13880 T1 : Entity_Id;
13882 begin
13883 -- Remove procedure calls, which syntactically cannot appear in
13884 -- this context, but which cannot be removed by type checking,
13885 -- because the context does not impose a type.
13887 -- The node may be labelled overloaded, but still contain only one
13888 -- interpretation because others were discarded earlier. If this
13889 -- is the case, retain the single interpretation if legal.
13891 Get_First_Interp (Operand, I, It);
13892 Opnd_Type := It.Typ;
13893 Get_Next_Interp (I, It);
13895 if Present (It.Typ)
13896 and then Opnd_Type /= Standard_Void_Type
13897 then
13898 -- More than one candidate interpretation is available
13900 Get_First_Interp (Operand, I, It);
13901 while Present (It.Typ) loop
13902 if It.Typ = Standard_Void_Type then
13903 Remove_Interp (I);
13904 end if;
13906 -- When compiling for a system where Address is of a visible
13907 -- integer type, spurious ambiguities can be produced when
13908 -- arithmetic operations have a literal operand and return
13909 -- System.Address or a descendant of it. These ambiguities
13910 -- are usually resolved by the context, but for conversions
13911 -- there is no context type and the removal of the spurious
13912 -- operations must be done explicitly here.
13914 if not Address_Is_Private
13915 and then Is_Descendant_Of_Address (It.Typ)
13916 then
13917 Remove_Interp (I);
13918 end if;
13920 Get_Next_Interp (I, It);
13921 end loop;
13922 end if;
13924 Get_First_Interp (Operand, I, It);
13925 I1 := I;
13926 It1 := It;
13928 if No (It.Typ) then
13929 Conversion_Error_N ("illegal operand in conversion", Operand);
13930 return False;
13931 end if;
13933 Get_Next_Interp (I, It);
13935 if Present (It.Typ) then
13936 N1 := It1.Nam;
13937 T1 := It1.Typ;
13938 It1 := Disambiguate (Operand, I1, I, Any_Type);
13940 if It1 = No_Interp then
13941 Conversion_Error_N
13942 ("ambiguous operand in conversion", Operand);
13944 -- If the interpretation involves a standard operator, use
13945 -- the location of the type, which may be user-defined.
13947 if Sloc (It.Nam) = Standard_Location then
13948 Error_Msg_Sloc := Sloc (It.Typ);
13949 else
13950 Error_Msg_Sloc := Sloc (It.Nam);
13951 end if;
13953 Conversion_Error_N -- CODEFIX
13954 ("\\possible interpretation#!", Operand);
13956 if Sloc (N1) = Standard_Location then
13957 Error_Msg_Sloc := Sloc (T1);
13958 else
13959 Error_Msg_Sloc := Sloc (N1);
13960 end if;
13962 Conversion_Error_N -- CODEFIX
13963 ("\\possible interpretation#!", Operand);
13965 return False;
13966 end if;
13967 end if;
13969 Set_Etype (Operand, It1.Typ);
13970 Opnd_Type := It1.Typ;
13971 end;
13972 end if;
13974 -- Deal with conversion of integer type to address if the pragma
13975 -- Allow_Integer_Address is in effect. We convert the conversion to
13976 -- an unchecked conversion in this case and we are all done.
13978 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
13979 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
13980 Analyze_And_Resolve (N, Target_Type);
13981 return True;
13982 end if;
13984 -- If we are within a child unit, check whether the type of the
13985 -- expression has an ancestor in a parent unit, in which case it
13986 -- belongs to its derivation class even if the ancestor is private.
13987 -- See RM 7.3.1 (5.2/3).
13989 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
13991 -- Numeric types
13993 if Is_Numeric_Type (Target_Type) then
13995 -- A universal fixed expression can be converted to any numeric type
13997 if Opnd_Type = Universal_Fixed then
13998 return True;
14000 -- Also no need to check when in an instance or inlined body, because
14001 -- the legality has been established when the template was analyzed.
14002 -- Furthermore, numeric conversions may occur where only a private
14003 -- view of the operand type is visible at the instantiation point.
14004 -- This results in a spurious error if we check that the operand type
14005 -- is a numeric type.
14007 -- Note: in a previous version of this unit, the following tests were
14008 -- applied only for generated code (Comes_From_Source set to False),
14009 -- but in fact the test is required for source code as well, since
14010 -- this situation can arise in source code.
14012 elsif In_Instance_Code or else In_Inlined_Body then
14013 return True;
14015 -- Otherwise we need the conversion check
14017 else
14018 return Conversion_Check
14019 (Is_Numeric_Type (Opnd_Type)
14020 or else
14021 (Present (Inc_Ancestor)
14022 and then Is_Numeric_Type (Inc_Ancestor)),
14023 "illegal operand for numeric conversion");
14024 end if;
14026 -- Array types
14028 elsif Is_Array_Type (Target_Type) then
14029 if not Is_Array_Type (Opnd_Type)
14030 or else Opnd_Type = Any_Composite
14031 or else Opnd_Type = Any_String
14032 then
14033 Conversion_Error_N
14034 ("illegal operand for array conversion", Operand);
14035 return False;
14037 else
14038 return Valid_Array_Conversion;
14039 end if;
14041 -- Ada 2005 (AI-251): Internally generated conversions of access to
14042 -- interface types added to force the displacement of the pointer to
14043 -- reference the corresponding dispatch table.
14045 elsif not Comes_From_Source (N)
14046 and then Is_Access_Type (Target_Type)
14047 and then Is_Interface (Designated_Type (Target_Type))
14048 then
14049 return True;
14051 -- Ada 2005 (AI-251): Anonymous access types where target references an
14052 -- interface type.
14054 elsif Is_Access_Type (Opnd_Type)
14055 and then Ekind (Target_Type) in
14056 E_General_Access_Type | E_Anonymous_Access_Type
14057 and then Is_Interface (Directly_Designated_Type (Target_Type))
14058 then
14059 -- Check the static accessibility rule of 4.6(17). Note that the
14060 -- check is not enforced when within an instance body, since the
14061 -- RM requires such cases to be caught at run time.
14063 -- If the operand is a rewriting of an allocator no check is needed
14064 -- because there are no accessibility issues.
14066 if Nkind (Original_Node (N)) = N_Allocator then
14067 null;
14069 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
14070 if Type_Access_Level (Opnd_Type) >
14071 Deepest_Type_Access_Level (Target_Type)
14072 then
14073 -- In an instance, this is a run-time check, but one we know
14074 -- will fail, so generate an appropriate warning. The raise
14075 -- will be generated by Expand_N_Type_Conversion.
14077 if In_Instance_Body then
14078 Error_Msg_Warn := SPARK_Mode /= On;
14079 Conversion_Error_N
14080 ("cannot convert local pointer to non-local access type<<",
14081 Operand);
14082 Conversion_Error_N ("\Program_Error [<<", Operand);
14084 else
14085 Conversion_Error_N
14086 ("cannot convert local pointer to non-local access type",
14087 Operand);
14088 return False;
14089 end if;
14091 -- Special accessibility checks are needed in the case of access
14092 -- discriminants declared for a limited type.
14094 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14095 and then not Is_Local_Anonymous_Access (Opnd_Type)
14096 then
14097 -- When the operand is a selected access discriminant the check
14098 -- needs to be made against the level of the object denoted by
14099 -- the prefix of the selected name (Accessibility_Level handles
14100 -- checking the prefix of the operand for this case).
14102 if Nkind (Operand) = N_Selected_Component
14103 and then Static_Accessibility_Level
14104 (Operand, Zero_On_Dynamic_Level)
14105 > Deepest_Type_Access_Level (Target_Type)
14106 then
14107 -- In an instance, this is a run-time check, but one we know
14108 -- will fail, so generate an appropriate warning. The raise
14109 -- will be generated by Expand_N_Type_Conversion.
14111 if In_Instance_Body then
14112 Error_Msg_Warn := SPARK_Mode /= On;
14113 Conversion_Error_N
14114 ("cannot convert access discriminant to non-local "
14115 & "access type<<", Operand);
14116 Conversion_Error_N ("\Program_Error [<<", Operand);
14118 -- Real error if not in instance body
14120 else
14121 Conversion_Error_N
14122 ("cannot convert access discriminant to non-local "
14123 & "access type", Operand);
14124 return False;
14125 end if;
14126 end if;
14128 -- The case of a reference to an access discriminant from
14129 -- within a limited type declaration (which will appear as
14130 -- a discriminal) is always illegal because the level of the
14131 -- discriminant is considered to be deeper than any (nameable)
14132 -- access type.
14134 if Is_Entity_Name (Operand)
14135 and then not Is_Local_Anonymous_Access (Opnd_Type)
14136 and then
14137 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14138 and then Present (Discriminal_Link (Entity (Operand)))
14139 then
14140 Conversion_Error_N
14141 ("discriminant has deeper accessibility level than target",
14142 Operand);
14143 return False;
14144 end if;
14145 end if;
14146 end if;
14148 return True;
14150 -- General and anonymous access types
14152 elsif Ekind (Target_Type) in
14153 E_General_Access_Type | E_Anonymous_Access_Type
14154 and then
14155 Conversion_Check
14156 (Is_Access_Type (Opnd_Type)
14157 and then
14158 Ekind (Opnd_Type) not in
14159 E_Access_Subprogram_Type |
14160 E_Access_Protected_Subprogram_Type,
14161 "must be an access-to-object type")
14162 then
14163 if Is_Access_Constant (Opnd_Type)
14164 and then not Is_Access_Constant (Target_Type)
14165 then
14166 Conversion_Error_N
14167 ("access-to-constant operand type not allowed", Operand);
14168 return False;
14169 end if;
14171 -- Check the static accessibility rule of 4.6(17). Note that the
14172 -- check is not enforced when within an instance body, since the RM
14173 -- requires such cases to be caught at run time.
14175 if Ekind (Target_Type) /= E_Anonymous_Access_Type
14176 or else Is_Local_Anonymous_Access (Target_Type)
14177 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
14178 N_Object_Declaration
14179 then
14180 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
14181 -- conversions from an anonymous access type to a named general
14182 -- access type. Such conversions are not allowed in the case of
14183 -- access parameters and stand-alone objects of an anonymous
14184 -- access type. The implicit conversion case is recognized by
14185 -- testing that Comes_From_Source is False and that it's been
14186 -- rewritten. The Comes_From_Source test isn't sufficient because
14187 -- nodes in inlined calls to predefined library routines can have
14188 -- Comes_From_Source set to False. (Is there a better way to test
14189 -- for implicit conversions???).
14191 -- Do not treat a rewritten 'Old attribute reference like other
14192 -- rewrite substitutions. This makes a difference, for example,
14193 -- in the case where we are generating the expansion of a
14194 -- membership test of the form
14195 -- Saooaaat'Old in Named_Access_Type
14196 -- because in this case Valid_Conversion needs to return True
14197 -- (otherwise the expansion will be False - see the call site
14198 -- in exp_ch4.adb).
14200 if Ada_Version >= Ada_2012
14201 and then not Comes_From_Source (N)
14202 and then Is_Rewrite_Substitution (N)
14203 and then not Is_Attribute_Old (Original_Node (N))
14204 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
14205 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
14206 then
14207 if Is_Itype (Opnd_Type) then
14209 -- When applying restriction No_Dynamic_Accessibility_Check,
14210 -- implicit conversions are allowed when the operand type is
14211 -- not deeper than the target type.
14213 if No_Dynamic_Accessibility_Checks_Enabled (N) then
14214 if Type_Access_Level (Opnd_Type)
14215 > Deepest_Type_Access_Level (Target_Type)
14216 then
14217 Conversion_Error_N
14218 ("operand has deeper level than target", Operand);
14219 end if;
14221 -- Implicit conversions aren't allowed for objects of an
14222 -- anonymous access type, since such objects have nonstatic
14223 -- levels in Ada 2012.
14225 elsif Nkind (Associated_Node_For_Itype (Opnd_Type))
14226 = N_Object_Declaration
14227 then
14228 Conversion_Error_N
14229 ("implicit conversion of stand-alone anonymous "
14230 & "access object not allowed", Operand);
14231 return False;
14233 -- Implicit conversions aren't allowed for anonymous access
14234 -- parameters. We exclude anonymous access results as well
14235 -- as universal_access "=".
14237 elsif not Is_Local_Anonymous_Access (Opnd_Type)
14238 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
14239 N_Function_Specification |
14240 N_Procedure_Specification
14241 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
14242 then
14243 Conversion_Error_N
14244 ("implicit conversion of anonymous access parameter "
14245 & "not allowed", Operand);
14246 return False;
14248 -- Detect access discriminant values that are illegal
14249 -- implicit anonymous-to-named access conversion operands.
14251 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
14252 then
14253 Conversion_Error_N
14254 ("implicit conversion of anonymous access value "
14255 & "not allowed", Operand);
14256 return False;
14258 -- In other cases, the level of the operand's type must be
14259 -- statically less deep than that of the target type, else
14260 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
14262 elsif Type_Access_Level (Opnd_Type) >
14263 Deepest_Type_Access_Level (Target_Type)
14264 then
14265 Conversion_Error_N
14266 ("implicit conversion of anonymous access value "
14267 & "violates accessibility", Operand);
14268 return False;
14269 end if;
14270 end if;
14272 -- Check if the operand is deeper than the target type, taking
14273 -- care to avoid the case where we are converting a result of a
14274 -- function returning an anonymous access type since the "master
14275 -- of the call" would be target type of the conversion unless
14276 -- the target type is anonymous access as well - see RM 3.10.2
14277 -- (10.3/3).
14279 -- Note that when the restriction No_Dynamic_Accessibility_Checks
14280 -- is in effect wei also want to proceed with the conversion check
14281 -- described above.
14283 elsif Type_Access_Level (Opnd_Type, Assoc_Ent => Operand)
14284 > Deepest_Type_Access_Level (Target_Type)
14285 and then (Nkind (Associated_Node_For_Itype (Opnd_Type))
14286 /= N_Function_Specification
14287 or else Ekind (Target_Type) in Anonymous_Access_Kind
14288 or else No_Dynamic_Accessibility_Checks_Enabled (N))
14290 -- Check we are not in a return value ???
14292 and then (not In_Return_Value (N)
14293 or else
14294 Nkind (Associated_Node_For_Itype (Target_Type))
14295 = N_Component_Declaration)
14296 then
14297 -- In an instance, this is a run-time check, but one we know
14298 -- will fail, so generate an appropriate warning. The raise
14299 -- will be generated by Expand_N_Type_Conversion.
14301 if In_Instance_Body then
14302 Error_Msg_Warn := SPARK_Mode /= On;
14303 Conversion_Error_N
14304 ("cannot convert local pointer to non-local access type<<",
14305 Operand);
14306 Conversion_Error_N ("\Program_Error [<<", Operand);
14308 -- If not in an instance body, this is a real error
14310 else
14311 -- Avoid generation of spurious error message
14313 if not Error_Posted (N) then
14314 Conversion_Error_N
14315 ("cannot convert local pointer to non-local access type",
14316 Operand);
14317 end if;
14319 return False;
14320 end if;
14322 -- Special accessibility checks are needed in the case of access
14323 -- discriminants declared for a limited type.
14325 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14326 and then not Is_Local_Anonymous_Access (Opnd_Type)
14327 then
14328 -- When the operand is a selected access discriminant the check
14329 -- needs to be made against the level of the object denoted by
14330 -- the prefix of the selected name (Accessibility_Level handles
14331 -- checking the prefix of the operand for this case).
14333 if Nkind (Operand) = N_Selected_Component
14334 and then Static_Accessibility_Level
14335 (Operand, Zero_On_Dynamic_Level)
14336 > Deepest_Type_Access_Level (Target_Type)
14337 then
14338 -- In an instance, this is a run-time check, but one we know
14339 -- will fail, so generate an appropriate warning. The raise
14340 -- will be generated by Expand_N_Type_Conversion.
14342 if In_Instance_Body then
14343 Error_Msg_Warn := SPARK_Mode /= On;
14344 Conversion_Error_N
14345 ("cannot convert access discriminant to non-local "
14346 & "access type<<", Operand);
14347 Conversion_Error_N ("\Program_Error [<<", Operand);
14349 -- If not in an instance body, this is a real error
14351 else
14352 Conversion_Error_N
14353 ("cannot convert access discriminant to non-local "
14354 & "access type", Operand);
14355 return False;
14356 end if;
14357 end if;
14359 -- The case of a reference to an access discriminant from
14360 -- within a limited type declaration (which will appear as
14361 -- a discriminal) is always illegal because the level of the
14362 -- discriminant is considered to be deeper than any (nameable)
14363 -- access type.
14365 if Is_Entity_Name (Operand)
14366 and then
14367 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14368 and then Present (Discriminal_Link (Entity (Operand)))
14369 then
14370 Conversion_Error_N
14371 ("discriminant has deeper accessibility level than target",
14372 Operand);
14373 return False;
14374 end if;
14375 end if;
14376 end if;
14378 -- In the presence of limited_with clauses we have to use nonlimited
14379 -- views, if available.
14381 Check_Limited : declare
14382 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
14383 -- Helper function to handle limited views
14385 --------------------------
14386 -- Full_Designated_Type --
14387 --------------------------
14389 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
14390 Desig : constant Entity_Id := Designated_Type (T);
14392 begin
14393 -- Handle the limited view of a type
14395 if From_Limited_With (Desig)
14396 and then Has_Non_Limited_View (Desig)
14397 then
14398 return Available_View (Desig);
14399 else
14400 return Desig;
14401 end if;
14402 end Full_Designated_Type;
14404 -- Local Declarations
14406 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
14407 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
14409 Same_Base : constant Boolean :=
14410 Base_Type (Target) = Base_Type (Opnd);
14412 -- Start of processing for Check_Limited
14414 begin
14415 if Is_Tagged_Type (Target) then
14416 return Valid_Tagged_Conversion (Target, Opnd);
14418 else
14419 if not Same_Base then
14420 Conversion_Error_NE
14421 ("target designated type not compatible with }",
14422 N, Base_Type (Opnd));
14423 return False;
14425 -- Ada 2005 AI-384: legality rule is symmetric in both
14426 -- designated types. The conversion is legal (with possible
14427 -- constraint check) if either designated type is
14428 -- unconstrained.
14430 elsif Subtypes_Statically_Match (Target, Opnd)
14431 or else
14432 (Has_Discriminants (Target)
14433 and then
14434 (not Is_Constrained (Opnd)
14435 or else not Is_Constrained (Target)))
14436 then
14437 -- Special case, if Value_Size has been used to make the
14438 -- sizes different, the conversion is not allowed even
14439 -- though the subtypes statically match.
14441 if Known_Static_RM_Size (Target)
14442 and then Known_Static_RM_Size (Opnd)
14443 and then RM_Size (Target) /= RM_Size (Opnd)
14444 then
14445 Conversion_Error_NE
14446 ("target designated subtype not compatible with }",
14447 N, Opnd);
14448 Conversion_Error_NE
14449 ("\because sizes of the two designated subtypes differ",
14450 N, Opnd);
14451 return False;
14453 -- Normal case where conversion is allowed
14455 else
14456 return True;
14457 end if;
14459 else
14460 Error_Msg_NE
14461 ("target designated subtype not compatible with }",
14462 N, Opnd);
14463 return False;
14464 end if;
14465 end if;
14466 end Check_Limited;
14468 -- Access to subprogram types. If the operand is an access parameter,
14469 -- the type has a deeper accessibility that any master, and cannot be
14470 -- assigned. We must make an exception if the conversion is part of an
14471 -- assignment and the target is the return object of an extended return
14472 -- statement, because in that case the accessibility check takes place
14473 -- after the return.
14475 elsif Is_Access_Subprogram_Type (Target_Type)
14477 -- Note: this test of Opnd_Type is there to prevent entering this
14478 -- branch in the case of a remote access to subprogram type, which
14479 -- is internally represented as an E_Record_Type.
14481 and then Is_Access_Type (Opnd_Type)
14482 then
14483 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
14484 and then Is_Entity_Name (Operand)
14485 and then Ekind (Entity (Operand)) = E_In_Parameter
14486 and then
14487 (Nkind (Parent (N)) /= N_Assignment_Statement
14488 or else not Is_Entity_Name (Name (Parent (N)))
14489 or else not Is_Return_Object (Entity (Name (Parent (N)))))
14490 then
14491 Conversion_Error_N
14492 ("illegal attempt to store anonymous access to subprogram",
14493 Operand);
14494 Conversion_Error_N
14495 ("\value has deeper accessibility than any master "
14496 & "(RM 3.10.2 (13))",
14497 Operand);
14499 Error_Msg_NE
14500 ("\use named access type for& instead of access parameter",
14501 Operand, Entity (Operand));
14502 end if;
14504 -- Check that the designated types are subtype conformant
14506 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
14507 Old_Id => Designated_Type (Opnd_Type),
14508 Err_Loc => N);
14510 -- Check the static accessibility rule of 4.6(20)
14512 if Type_Access_Level (Opnd_Type) >
14513 Deepest_Type_Access_Level (Target_Type)
14514 then
14515 Conversion_Error_N
14516 ("operand type has deeper accessibility level than target",
14517 Operand);
14519 -- Check that if the operand type is declared in a generic body,
14520 -- then the target type must be declared within that same body
14521 -- (enforces last sentence of 4.6(20)).
14523 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
14524 declare
14525 O_Gen : constant Node_Id :=
14526 Enclosing_Generic_Body (Opnd_Type);
14528 T_Gen : Node_Id;
14530 begin
14531 T_Gen := Enclosing_Generic_Body (Target_Type);
14532 while Present (T_Gen) and then T_Gen /= O_Gen loop
14533 T_Gen := Enclosing_Generic_Body (T_Gen);
14534 end loop;
14536 if T_Gen /= O_Gen then
14537 Conversion_Error_N
14538 ("target type must be declared in same generic body "
14539 & "as operand type", N);
14540 end if;
14541 end;
14542 end if;
14544 -- Check that the strub modes are compatible.
14545 -- We wish to reject explicit conversions only for
14546 -- incompatible modes.
14548 return Conversion_Check
14549 (Compatible_Strub_Modes
14550 (Designated_Type (Target_Type),
14551 Designated_Type (Opnd_Type)),
14552 "incompatible `strub` modes");
14554 -- Remote access to subprogram types
14556 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
14557 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
14558 then
14559 -- It is valid to convert from one RAS type to another provided
14560 -- that their specification statically match.
14562 -- Note: at this point, remote access to subprogram types have been
14563 -- expanded to their E_Record_Type representation, and we need to
14564 -- go back to the original access type definition using the
14565 -- Corresponding_Remote_Type attribute in order to check that the
14566 -- designated profiles match.
14568 pragma Assert (Ekind (Target_Type) = E_Record_Type);
14569 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
14571 Check_Subtype_Conformant
14572 (New_Id =>
14573 Designated_Type (Corresponding_Remote_Type (Target_Type)),
14574 Old_Id =>
14575 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14576 Err_Loc =>
14579 -- Check that the strub modes are compatible.
14580 -- We wish to reject explicit conversions only for
14581 -- incompatible modes.
14583 return Conversion_Check
14584 (Compatible_Strub_Modes
14585 (Designated_Type (Target_Type),
14586 Designated_Type (Opnd_Type)),
14587 "incompatible `strub` modes");
14589 -- If it was legal in the generic, it's legal in the instance
14591 elsif In_Instance_Body then
14592 return True;
14594 -- If both are tagged types, check legality of view conversions
14596 elsif Is_Tagged_Type (Target_Type)
14597 and then
14598 Is_Tagged_Type (Opnd_Type)
14599 then
14600 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14602 -- Types derived from the same root type are convertible
14604 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14605 return True;
14607 -- In an instance or an inlined body, there may be inconsistent views of
14608 -- the same type, or of types derived from a common root.
14610 elsif (In_Instance or In_Inlined_Body)
14611 and then
14612 Root_Type (Underlying_Type (Target_Type)) =
14613 Root_Type (Underlying_Type (Opnd_Type))
14614 then
14615 return True;
14617 -- Special check for common access type error case
14619 elsif Ekind (Target_Type) = E_Access_Type
14620 and then Is_Access_Type (Opnd_Type)
14621 then
14622 Conversion_Error_N ("target type must be general access type!", N);
14623 Conversion_Error_NE -- CODEFIX
14624 ("\add ALL to }!", N, Target_Type);
14625 return False;
14627 -- Here we have a real conversion error
14629 else
14630 -- Check for missing regular with_clause when only a limited view of
14631 -- target is available.
14633 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14634 Conversion_Error_NE
14635 ("invalid conversion, not compatible with limited view of }",
14636 N, Opnd_Type);
14637 Conversion_Error_NE
14638 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14640 elsif Is_Access_Type (Opnd_Type)
14641 and then From_Limited_With (Designated_Type (Opnd_Type))
14642 and then In_Package_Body
14643 then
14644 Conversion_Error_NE
14645 ("invalid conversion, not compatible with }", N, Opnd_Type);
14646 Conversion_Error_NE
14647 ("\add with_clause for& to current unit!",
14648 N, Scope (Designated_Type (Opnd_Type)));
14650 else
14651 Conversion_Error_NE
14652 ("invalid conversion, not compatible with }", N, Opnd_Type);
14653 end if;
14655 return False;
14656 end if;
14657 end Valid_Conversion;
14659 end Sem_Res;