]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/sem_res.adb
Fix typo.
[gcc.git] / gcc / ada / sem_res.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ R E S --
6-- --
7-- B o d y --
8-- --
c28408b7 9-- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
27with Checks; use Checks;
28with Debug; use Debug;
29with Debug_A; use Debug_A;
30with Einfo; use Einfo;
b7d1f17f 31with Elists; use Elists;
996ae0b0
RK
32with Errout; use Errout;
33with Expander; use Expander;
758c442c 34with Exp_Disp; use Exp_Disp;
0669bebe 35with Exp_Ch6; use Exp_Ch6;
996ae0b0 36with Exp_Ch7; use Exp_Ch7;
fbf5a39b 37with Exp_Tss; use Exp_Tss;
996ae0b0 38with Exp_Util; use Exp_Util;
dae2b8ea 39with Fname; use Fname;
996ae0b0
RK
40with Freeze; use Freeze;
41with Itypes; use Itypes;
42with Lib; use Lib;
43with Lib.Xref; use Lib.Xref;
44with Namet; use Namet;
45with Nmake; use Nmake;
46with Nlists; use Nlists;
47with Opt; use Opt;
48with Output; use Output;
49with Restrict; use Restrict;
6e937c1c 50with Rident; use Rident;
996ae0b0
RK
51with Rtsfind; use Rtsfind;
52with Sem; use Sem;
a4100e55 53with Sem_Aux; use Sem_Aux;
996ae0b0
RK
54with Sem_Aggr; use Sem_Aggr;
55with Sem_Attr; use Sem_Attr;
56with Sem_Cat; use Sem_Cat;
57with Sem_Ch4; use Sem_Ch4;
58with Sem_Ch6; use Sem_Ch6;
59with Sem_Ch8; use Sem_Ch8;
4b92fd3c 60with Sem_Ch13; use Sem_Ch13;
996ae0b0
RK
61with Sem_Disp; use Sem_Disp;
62with Sem_Dist; use Sem_Dist;
16212e89 63with Sem_Elim; use Sem_Elim;
996ae0b0
RK
64with Sem_Elab; use Sem_Elab;
65with Sem_Eval; use Sem_Eval;
66with Sem_Intr; use Sem_Intr;
67with Sem_Util; use Sem_Util;
68with Sem_Type; use Sem_Type;
69with Sem_Warn; use Sem_Warn;
70with Sinfo; use Sinfo;
f4b049db 71with Sinfo.CN; use Sinfo.CN;
fbf5a39b 72with Snames; use Snames;
996ae0b0
RK
73with Stand; use Stand;
74with Stringt; use Stringt;
45fc7ddb 75with Style; use Style;
996ae0b0
RK
76with Tbuild; use Tbuild;
77with Uintp; use Uintp;
78with Urealp; use Urealp;
79
80package body Sem_Res is
81
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
85
86 -- Second pass (top-down) type checking and overload resolution procedures
87 -- Typ is the type required by context. These procedures propagate the
88 -- type information recursively to the descendants of N. If the node
89 -- is not overloaded, its Etype is established in the first pass. If
90 -- overloaded, the Resolve routines set the correct type. For arith.
91 -- operators, the Etype is the base type of the context.
92
93 -- Note that Resolve_Attribute is separated off in Sem_Attr
94
bd29d519
AC
95 function Bad_Unordered_Enumeration_Reference
96 (N : Node_Id;
97 T : Entity_Id) return Boolean;
98 -- Node N contains a potentially dubious reference to type T, either an
99 -- explicit comparison, or an explicit range. This function returns True
100 -- if the type T is an enumeration type for which No pragma Order has been
101 -- given, and the reference N is not in the same extended source unit as
102 -- the declaration of T.
103
996ae0b0
RK
104 procedure Check_Discriminant_Use (N : Node_Id);
105 -- Enforce the restrictions on the use of discriminants when constraining
106 -- a component of a discriminated type (record or concurrent type).
107
108 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
109 -- Given a node for an operator associated with type T, check that
110 -- the operator is visible. Operators all of whose operands are
111 -- universal must be checked for visibility during resolution
112 -- because their type is not determinable based on their operands.
113
c8ef728f
ES
114 procedure Check_Fully_Declared_Prefix
115 (Typ : Entity_Id;
116 Pref : Node_Id);
117 -- Check that the type of the prefix of a dereference is not incomplete
118
996ae0b0
RK
119 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
120 -- Given a call node, N, which is known to occur immediately within the
121 -- subprogram being called, determines whether it is a detectable case of
122 -- an infinite recursion, and if so, outputs appropriate messages. Returns
123 -- True if an infinite recursion is detected, and False otherwise.
124
125 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
126 -- If the type of the object being initialized uses the secondary stack
127 -- directly or indirectly, create a transient scope for the call to the
fbf5a39b
AC
128 -- init proc. This is because we do not create transient scopes for the
129 -- initialization of individual components within the init proc itself.
996ae0b0
RK
130 -- Could be optimized away perhaps?
131
f61580d4 132 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
6fb4cdde
AC
133 -- N is the node for a logical operator. If the operator is predefined, and
134 -- the root type of the operands is Standard.Boolean, then a check is made
a36c1c3e
RD
135 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
136 -- the style check for Style_Check_Boolean_And_Or.
f61580d4 137
67ce0d7e
RD
138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
139 -- Determine whether E is an access type declared by an access
140 -- declaration, and not an (anonymous) allocator type.
141
996ae0b0 142 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
6a497607
AC
143 -- Utility to check whether the entity for an operator is a predefined
144 -- operator, in which case the expression is left as an operator in the
145 -- tree (else it is rewritten into a call). An instance of an intrinsic
146 -- conversion operation may be given an operator name, but is not treated
147 -- like an operator. Note that an operator that is an imported back-end
148 -- builtin has convention Intrinsic, but is expected to be rewritten into
149 -- a call, so such an operator is not treated as predefined by this
150 -- predicate.
996ae0b0
RK
151
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
156
10303118
BD
157 procedure Resolve_Op_Concat_Arg
158 (N : Node_Id;
159 Arg : Node_Id;
160 Typ : Entity_Id;
161 Is_Comp : Boolean);
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
166
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
169
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
173
996ae0b0
RK
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
19d846a0 177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
955871d3 181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
955871d3 184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
185 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
a961aa79 195 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
196 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
209
210 function Operator_Kind
211 (Op_Name : Name_Id;
0ab80019 212 Is_Binary : Boolean) return Node_Kind;
996ae0b0
RK
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
215
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
bc5f3720
RD
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
996ae0b0
RK
220
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
227
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
229 -- A call to a user-defined intrinsic operator is rewritten as a call
230 -- to the corresponding predefined operator, with suitable conversions.
6a497607 231 -- Note that this applies only for intrinsic operators that denote
841dd0f5 232 -- predefined operators, not operators that are intrinsic imports of
6a497607 233 -- back-end builtins.
996ae0b0 234
fbf5a39b 235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
305caf42
AC
236 -- Ditto, for unary operators (arithmetic ones and "not" on signed
237 -- integer types for VMS).
fbf5a39b 238
996ae0b0
RK
239 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
240 -- If an operator node resolves to a call to a user-defined operator,
241 -- rewrite the node as a function call.
242
243 procedure Make_Call_Into_Operator
244 (N : Node_Id;
245 Typ : Entity_Id;
246 Op_Id : Entity_Id);
247 -- Inverse transformation: if an operator is given in functional notation,
248 -- then after resolving the node, transform into an operator node, so
249 -- that operands are resolved properly. Recall that predefined operators
250 -- do not have a full signature and special resolution rules apply.
251
0ab80019
AC
252 procedure Rewrite_Renamed_Operator
253 (N : Node_Id;
254 Op : Entity_Id;
255 Typ : Entity_Id);
996ae0b0 256 -- An operator can rename another, e.g. in an instantiation. In that
0ab80019 257 -- case, the proper operator node must be constructed and resolved.
996ae0b0
RK
258
259 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
260 -- The String_Literal_Subtype is built for all strings that are not
07fc65c4
GB
261 -- operands of a static concatenation operation. If the argument is
262 -- not a N_String_Literal node, then the call has no effect.
996ae0b0
RK
263
264 procedure Set_Slice_Subtype (N : Node_Id);
fbf5a39b 265 -- Build subtype of array type, with the range specified by the slice
996ae0b0 266
0669bebe
GB
267 procedure Simplify_Type_Conversion (N : Node_Id);
268 -- Called after N has been resolved and evaluated, but before range checks
269 -- have been applied. Currently simplifies a combination of floating-point
270 -- to integer conversion and Truncation attribute.
271
996ae0b0 272 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
07fc65c4
GB
273 -- A universal_fixed expression in an universal context is unambiguous
274 -- if there is only one applicable fixed point type. Determining whether
996ae0b0
RK
275 -- there is only one requires a search over all visible entities, and
276 -- happens only in very pathological cases (see 6115-006).
277
278 function Valid_Conversion
279 (N : Node_Id;
280 Target : Entity_Id;
0ab80019 281 Operand : Node_Id) return Boolean;
996ae0b0
RK
282 -- Verify legality rules given in 4.6 (8-23). Target is the target
283 -- type of the conversion, which may be an implicit conversion of
284 -- an actual parameter to an anonymous access type (in which case
285 -- N denotes the actual parameter and N = Operand).
286
287 -------------------------
288 -- Ambiguous_Character --
289 -------------------------
290
291 procedure Ambiguous_Character (C : Node_Id) is
292 E : Entity_Id;
293
294 begin
295 if Nkind (C) = N_Character_Literal then
ed2233dc 296 Error_Msg_N ("ambiguous character literal", C);
b7d1f17f
HK
297
298 -- First the ones in Standard
299
ed2233dc
AC
300 Error_Msg_N ("\\possible interpretation: Character!", C);
301 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
b7d1f17f
HK
302
303 -- Include Wide_Wide_Character in Ada 2005 mode
304
0791fbe9 305 if Ada_Version >= Ada_2005 then
ed2233dc 306 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
b7d1f17f
HK
307 end if;
308
309 -- Now any other types that match
996ae0b0
RK
310
311 E := Current_Entity (C);
1420b484 312 while Present (E) loop
ed2233dc 313 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
1420b484
JM
314 E := Homonym (E);
315 end loop;
996ae0b0
RK
316 end if;
317 end Ambiguous_Character;
318
319 -------------------------
320 -- Analyze_And_Resolve --
321 -------------------------
322
323 procedure Analyze_And_Resolve (N : Node_Id) is
324 begin
325 Analyze (N);
fbf5a39b 326 Resolve (N);
996ae0b0
RK
327 end Analyze_And_Resolve;
328
329 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
330 begin
331 Analyze (N);
332 Resolve (N, Typ);
333 end Analyze_And_Resolve;
334
335 -- Version withs check(s) suppressed
336
337 procedure Analyze_And_Resolve
338 (N : Node_Id;
339 Typ : Entity_Id;
340 Suppress : Check_Id)
341 is
fbf5a39b 342 Scop : constant Entity_Id := Current_Scope;
996ae0b0
RK
343
344 begin
345 if Suppress = All_Checks then
346 declare
fbf5a39b 347 Svg : constant Suppress_Array := Scope_Suppress;
996ae0b0
RK
348 begin
349 Scope_Suppress := (others => True);
350 Analyze_And_Resolve (N, Typ);
351 Scope_Suppress := Svg;
352 end;
353
354 else
355 declare
fbf5a39b 356 Svg : constant Boolean := Scope_Suppress (Suppress);
996ae0b0
RK
357
358 begin
fbf5a39b 359 Scope_Suppress (Suppress) := True;
996ae0b0 360 Analyze_And_Resolve (N, Typ);
fbf5a39b 361 Scope_Suppress (Suppress) := Svg;
996ae0b0
RK
362 end;
363 end if;
364
365 if Current_Scope /= Scop
366 and then Scope_Is_Transient
367 then
368 -- This can only happen if a transient scope was created
369 -- for an inner expression, which will be removed upon
370 -- completion of the analysis of an enclosing construct.
371 -- The transient scope must have the suppress status of
372 -- the enclosing environment, not of this Analyze call.
373
374 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
375 Scope_Suppress;
376 end if;
377 end Analyze_And_Resolve;
378
379 procedure Analyze_And_Resolve
380 (N : Node_Id;
381 Suppress : Check_Id)
382 is
fbf5a39b 383 Scop : constant Entity_Id := Current_Scope;
996ae0b0
RK
384
385 begin
386 if Suppress = All_Checks then
387 declare
fbf5a39b 388 Svg : constant Suppress_Array := Scope_Suppress;
996ae0b0
RK
389 begin
390 Scope_Suppress := (others => True);
391 Analyze_And_Resolve (N);
392 Scope_Suppress := Svg;
393 end;
394
395 else
396 declare
fbf5a39b 397 Svg : constant Boolean := Scope_Suppress (Suppress);
996ae0b0
RK
398
399 begin
fbf5a39b 400 Scope_Suppress (Suppress) := True;
996ae0b0 401 Analyze_And_Resolve (N);
fbf5a39b 402 Scope_Suppress (Suppress) := Svg;
996ae0b0
RK
403 end;
404 end if;
405
406 if Current_Scope /= Scop
407 and then Scope_Is_Transient
408 then
409 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
410 Scope_Suppress;
411 end if;
412 end Analyze_And_Resolve;
413
bd29d519
AC
414 ----------------------------------------
415 -- Bad_Unordered_Enumeration_Reference --
416 ----------------------------------------
417
418 function Bad_Unordered_Enumeration_Reference
419 (N : Node_Id;
420 T : Entity_Id) return Boolean
421 is
422 begin
423 return Is_Enumeration_Type (T)
424 and then Comes_From_Source (N)
425 and then Warn_On_Unordered_Enumeration_Type
426 and then not Has_Pragma_Ordered (T)
427 and then not In_Same_Extended_Unit (N, T);
428 end Bad_Unordered_Enumeration_Reference;
429
996ae0b0
RK
430 ----------------------------
431 -- Check_Discriminant_Use --
432 ----------------------------
433
434 procedure Check_Discriminant_Use (N : Node_Id) is
435 PN : constant Node_Id := Parent (N);
436 Disc : constant Entity_Id := Entity (N);
437 P : Node_Id;
438 D : Node_Id;
439
440 begin
f3d0f304 441 -- Any use in a spec-expression is legal
996ae0b0 442
45fc7ddb 443 if In_Spec_Expression then
996ae0b0
RK
444 null;
445
446 elsif Nkind (PN) = N_Range then
447
a77842bd 448 -- Discriminant cannot be used to constrain a scalar type
996ae0b0
RK
449
450 P := Parent (PN);
451
452 if Nkind (P) = N_Range_Constraint
453 and then Nkind (Parent (P)) = N_Subtype_Indication
a397db96 454 and then Nkind (Parent (Parent (P))) = N_Component_Definition
996ae0b0
RK
455 then
456 Error_Msg_N ("discriminant cannot constrain scalar type", N);
457
458 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
459
460 -- The following check catches the unusual case where
461 -- a discriminant appears within an index constraint
462 -- that is part of a larger expression within a constraint
463 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
464 -- For now we only check case of record components, and
465 -- note that a similar check should also apply in the
466 -- case of discriminant constraints below. ???
467
468 -- Note that the check for N_Subtype_Declaration below is to
469 -- detect the valid use of discriminants in the constraints of a
470 -- subtype declaration when this subtype declaration appears
471 -- inside the scope of a record type (which is syntactically
472 -- illegal, but which may be created as part of derived type
473 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
474 -- for more info.
475
476 if Ekind (Current_Scope) = E_Record_Type
477 and then Scope (Disc) = Current_Scope
478 and then not
479 (Nkind (Parent (P)) = N_Subtype_Indication
45fc7ddb
HK
480 and then
481 Nkind_In (Parent (Parent (P)), N_Component_Definition,
482 N_Subtype_Declaration)
996ae0b0
RK
483 and then Paren_Count (N) = 0)
484 then
485 Error_Msg_N
486 ("discriminant must appear alone in component constraint", N);
487 return;
488 end if;
489
a0ac3932 490 -- Detect a common error:
9bc43c53 491
996ae0b0 492 -- type R (D : Positive := 100) is record
9bc43c53 493 -- Name : String (1 .. D);
996ae0b0
RK
494 -- end record;
495
a0ac3932
RD
496 -- The default value causes an object of type R to be allocated
497 -- with room for Positive'Last characters. The RM does not mandate
498 -- the allocation of the maximum size, but that is what GNAT does
499 -- so we should warn the programmer that there is a problem.
996ae0b0 500
a0ac3932 501 Check_Large : declare
996ae0b0
RK
502 SI : Node_Id;
503 T : Entity_Id;
504 TB : Node_Id;
505 CB : Entity_Id;
506
507 function Large_Storage_Type (T : Entity_Id) return Boolean;
508 -- Return True if type T has a large enough range that
509 -- any array whose index type covered the whole range of
510 -- the type would likely raise Storage_Error.
511
fbf5a39b
AC
512 ------------------------
513 -- Large_Storage_Type --
514 ------------------------
515
996ae0b0
RK
516 function Large_Storage_Type (T : Entity_Id) return Boolean is
517 begin
4b92fd3c
ST
518 -- The type is considered large if its bounds are known at
519 -- compile time and if it requires at least as many bits as
520 -- a Positive to store the possible values.
521
522 return Compile_Time_Known_Value (Type_Low_Bound (T))
523 and then Compile_Time_Known_Value (Type_High_Bound (T))
524 and then
525 Minimum_Size (T, Biased => True) >=
a0ac3932 526 RM_Size (Standard_Positive);
996ae0b0
RK
527 end Large_Storage_Type;
528
a0ac3932
RD
529 -- Start of processing for Check_Large
530
996ae0b0
RK
531 begin
532 -- Check that the Disc has a large range
533
534 if not Large_Storage_Type (Etype (Disc)) then
535 goto No_Danger;
536 end if;
537
538 -- If the enclosing type is limited, we allocate only the
539 -- default value, not the maximum, and there is no need for
540 -- a warning.
541
542 if Is_Limited_Type (Scope (Disc)) then
543 goto No_Danger;
544 end if;
545
546 -- Check that it is the high bound
547
548 if N /= High_Bound (PN)
c8ef728f 549 or else No (Discriminant_Default_Value (Disc))
996ae0b0
RK
550 then
551 goto No_Danger;
552 end if;
553
554 -- Check the array allows a large range at this bound.
555 -- First find the array
556
557 SI := Parent (P);
558
559 if Nkind (SI) /= N_Subtype_Indication then
560 goto No_Danger;
561 end if;
562
563 T := Entity (Subtype_Mark (SI));
564
565 if not Is_Array_Type (T) then
566 goto No_Danger;
567 end if;
568
569 -- Next, find the dimension
570
571 TB := First_Index (T);
572 CB := First (Constraints (P));
573 while True
574 and then Present (TB)
575 and then Present (CB)
576 and then CB /= PN
577 loop
578 Next_Index (TB);
579 Next (CB);
580 end loop;
581
582 if CB /= PN then
583 goto No_Danger;
584 end if;
585
586 -- Now, check the dimension has a large range
587
588 if not Large_Storage_Type (Etype (TB)) then
589 goto No_Danger;
590 end if;
591
592 -- Warn about the danger
593
594 Error_Msg_N
aa5147f0 595 ("?creation of & object may raise Storage_Error!",
fbf5a39b 596 Scope (Disc));
996ae0b0
RK
597
598 <<No_Danger>>
599 null;
600
a0ac3932 601 end Check_Large;
996ae0b0
RK
602 end if;
603
604 -- Legal case is in index or discriminant constraint
605
45fc7ddb
HK
606 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
607 N_Discriminant_Association)
996ae0b0
RK
608 then
609 if Paren_Count (N) > 0 then
610 Error_Msg_N
611 ("discriminant in constraint must appear alone", N);
758c442c
GD
612
613 elsif Nkind (N) = N_Expanded_Name
614 and then Comes_From_Source (N)
615 then
616 Error_Msg_N
617 ("discriminant must appear alone as a direct name", N);
996ae0b0
RK
618 end if;
619
620 return;
621
622 -- Otherwise, context is an expression. It should not be within
623 -- (i.e. a subexpression of) a constraint for a component.
624
625 else
626 D := PN;
627 P := Parent (PN);
45fc7ddb
HK
628 while not Nkind_In (P, N_Component_Declaration,
629 N_Subtype_Indication,
630 N_Entry_Declaration)
996ae0b0
RK
631 loop
632 D := P;
633 P := Parent (P);
634 exit when No (P);
635 end loop;
636
637 -- If the discriminant is used in an expression that is a bound
638 -- of a scalar type, an Itype is created and the bounds are attached
639 -- to its range, not to the original subtype indication. Such use
640 -- is of course a double fault.
641
642 if (Nkind (P) = N_Subtype_Indication
45fc7ddb
HK
643 and then Nkind_In (Parent (P), N_Component_Definition,
644 N_Derived_Type_Definition)
996ae0b0
RK
645 and then D = Constraint (P))
646
647 -- The constraint itself may be given by a subtype indication,
648 -- rather than by a more common discrete range.
649
650 or else (Nkind (P) = N_Subtype_Indication
fbf5a39b
AC
651 and then
652 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
996ae0b0
RK
653 or else Nkind (P) = N_Entry_Declaration
654 or else Nkind (D) = N_Defining_Identifier
655 then
656 Error_Msg_N
657 ("discriminant in constraint must appear alone", N);
658 end if;
659 end if;
660 end Check_Discriminant_Use;
661
662 --------------------------------
663 -- Check_For_Visible_Operator --
664 --------------------------------
665
666 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
996ae0b0 667 begin
fbf5a39b 668 if Is_Invisible_Operator (N, T) then
305caf42 669 Error_Msg_NE -- CODEFIX
996ae0b0 670 ("operator for} is not directly visible!", N, First_Subtype (T));
305caf42
AC
671 Error_Msg_N -- CODEFIX
672 ("use clause would make operation legal!", N);
996ae0b0
RK
673 end if;
674 end Check_For_Visible_Operator;
675
c8ef728f
ES
676 ----------------------------------
677 -- Check_Fully_Declared_Prefix --
678 ----------------------------------
679
680 procedure Check_Fully_Declared_Prefix
681 (Typ : Entity_Id;
682 Pref : Node_Id)
683 is
684 begin
685 -- Check that the designated type of the prefix of a dereference is
686 -- not an incomplete type. This cannot be done unconditionally, because
687 -- dereferences of private types are legal in default expressions. This
688 -- case is taken care of in Check_Fully_Declared, called below. There
689 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
690
691 -- This consideration also applies to similar checks for allocators,
692 -- qualified expressions, and type conversions.
693
694 -- An additional exception concerns other per-object expressions that
695 -- are not directly related to component declarations, in particular
696 -- representation pragmas for tasks. These will be per-object
697 -- expressions if they depend on discriminants or some global entity.
698 -- If the task has access discriminants, the designated type may be
699 -- incomplete at the point the expression is resolved. This resolution
700 -- takes place within the body of the initialization procedure, where
701 -- the discriminant is replaced by its discriminal.
702
703 if Is_Entity_Name (Pref)
704 and then Ekind (Entity (Pref)) = E_In_Parameter
705 then
706 null;
707
708 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
709 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
710 -- Analyze_Object_Renaming, and Freeze_Entity.
711
0791fbe9 712 elsif Ada_Version >= Ada_2005
c8ef728f 713 and then Is_Entity_Name (Pref)
811c6a85 714 and then Is_Access_Type (Etype (Pref))
c8ef728f
ES
715 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
716 E_Incomplete_Type
717 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
718 then
719 null;
720 else
721 Check_Fully_Declared (Typ, Parent (Pref));
722 end if;
723 end Check_Fully_Declared_Prefix;
724
996ae0b0
RK
725 ------------------------------
726 -- Check_Infinite_Recursion --
727 ------------------------------
728
729 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
730 P : Node_Id;
731 C : Node_Id;
732
07fc65c4
GB
733 function Same_Argument_List return Boolean;
734 -- Check whether list of actuals is identical to list of formals
735 -- of called function (which is also the enclosing scope).
736
737 ------------------------
738 -- Same_Argument_List --
739 ------------------------
740
741 function Same_Argument_List return Boolean is
742 A : Node_Id;
743 F : Entity_Id;
744 Subp : Entity_Id;
745
746 begin
747 if not Is_Entity_Name (Name (N)) then
748 return False;
749 else
750 Subp := Entity (Name (N));
751 end if;
752
753 F := First_Formal (Subp);
754 A := First_Actual (N);
07fc65c4
GB
755 while Present (F) and then Present (A) loop
756 if not Is_Entity_Name (A)
757 or else Entity (A) /= F
758 then
759 return False;
760 end if;
761
762 Next_Actual (A);
763 Next_Formal (F);
764 end loop;
765
766 return True;
767 end Same_Argument_List;
768
769 -- Start of processing for Check_Infinite_Recursion
770
996ae0b0 771 begin
26570b21
RD
772 -- Special case, if this is a procedure call and is a call to the
773 -- current procedure with the same argument list, then this is for
774 -- sure an infinite recursion and we insert a call to raise SE.
775
776 if Is_List_Member (N)
777 and then List_Length (List_Containing (N)) = 1
778 and then Same_Argument_List
779 then
780 declare
781 P : constant Node_Id := Parent (N);
782 begin
783 if Nkind (P) = N_Handled_Sequence_Of_Statements
784 and then Nkind (Parent (P)) = N_Subprogram_Body
785 and then Is_Empty_List (Declarations (Parent (P)))
786 then
787 Error_Msg_N ("!?infinite recursion", N);
788 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
789 Insert_Action (N,
790 Make_Raise_Storage_Error (Sloc (N),
791 Reason => SE_Infinite_Recursion));
792 return True;
793 end if;
794 end;
795 end if;
796
797 -- If not that special case, search up tree, quitting if we reach a
798 -- construct (e.g. a conditional) that tells us that this is not a
799 -- case for an infinite recursion warning.
996ae0b0
RK
800
801 C := N;
802 loop
803 P := Parent (C);
9a7da240
RD
804
805 -- If no parent, then we were not inside a subprogram, this can for
806 -- example happen when processing certain pragmas in a spec. Just
807 -- return False in this case.
808
809 if No (P) then
810 return False;
811 end if;
812
813 -- Done if we get to subprogram body, this is definitely an infinite
814 -- recursion case if we did not find anything to stop us.
815
996ae0b0 816 exit when Nkind (P) = N_Subprogram_Body;
9a7da240
RD
817
818 -- If appearing in conditional, result is false
819
45fc7ddb
HK
820 if Nkind_In (P, N_Or_Else,
821 N_And_Then,
d347f572
AC
822 N_Case_Expression,
823 N_Case_Statement,
824 N_Conditional_Expression,
825 N_If_Statement)
996ae0b0
RK
826 then
827 return False;
828
829 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
830 and then C /= First (Statements (P))
831 then
26570b21
RD
832 -- If the call is the expression of a return statement and the
833 -- actuals are identical to the formals, it's worth a warning.
834 -- However, we skip this if there is an immediately preceding
835 -- raise statement, since the call is never executed.
07fc65c4
GB
836
837 -- Furthermore, this corresponds to a common idiom:
838
839 -- function F (L : Thing) return Boolean is
840 -- begin
841 -- raise Program_Error;
842 -- return F (L);
843 -- end F;
844
845 -- for generating a stub function
846
aa5147f0 847 if Nkind (Parent (N)) = N_Simple_Return_Statement
07fc65c4
GB
848 and then Same_Argument_List
849 then
9ebe3743
HK
850 exit when not Is_List_Member (Parent (N));
851
852 -- OK, return statement is in a statement list, look for raise
853
854 declare
855 Nod : Node_Id;
856
857 begin
858 -- Skip past N_Freeze_Entity nodes generated by expansion
859
860 Nod := Prev (Parent (N));
861 while Present (Nod)
862 and then Nkind (Nod) = N_Freeze_Entity
863 loop
864 Prev (Nod);
865 end loop;
866
867 -- If no raise statement, give warning
868
869 exit when Nkind (Nod) /= N_Raise_Statement
870 and then
871 (Nkind (Nod) not in N_Raise_xxx_Error
872 or else Present (Condition (Nod)));
873 end;
07fc65c4
GB
874 end if;
875
996ae0b0
RK
876 return False;
877
878 else
879 C := P;
880 end if;
881 end loop;
882
aa5147f0
ES
883 Error_Msg_N ("!?possible infinite recursion", N);
884 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
996ae0b0
RK
885
886 return True;
887 end Check_Infinite_Recursion;
888
889 -------------------------------
890 -- Check_Initialization_Call --
891 -------------------------------
892
893 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
fbf5a39b 894 Typ : constant Entity_Id := Etype (First_Formal (Nam));
996ae0b0
RK
895
896 function Uses_SS (T : Entity_Id) return Boolean;
07fc65c4
GB
897 -- Check whether the creation of an object of the type will involve
898 -- use of the secondary stack. If T is a record type, this is true
f3d57416 899 -- if the expression for some component uses the secondary stack, e.g.
07fc65c4
GB
900 -- through a call to a function that returns an unconstrained value.
901 -- False if T is controlled, because cleanups occur elsewhere.
902
903 -------------
904 -- Uses_SS --
905 -------------
996ae0b0
RK
906
907 function Uses_SS (T : Entity_Id) return Boolean is
aa5147f0
ES
908 Comp : Entity_Id;
909 Expr : Node_Id;
910 Full_Type : Entity_Id := Underlying_Type (T);
996ae0b0
RK
911
912 begin
aa5147f0
ES
913 -- Normally we want to use the underlying type, but if it's not set
914 -- then continue with T.
915
916 if not Present (Full_Type) then
917 Full_Type := T;
918 end if;
919
920 if Is_Controlled (Full_Type) then
996ae0b0
RK
921 return False;
922
aa5147f0
ES
923 elsif Is_Array_Type (Full_Type) then
924 return Uses_SS (Component_Type (Full_Type));
996ae0b0 925
aa5147f0
ES
926 elsif Is_Record_Type (Full_Type) then
927 Comp := First_Component (Full_Type);
996ae0b0 928 while Present (Comp) loop
996ae0b0
RK
929 if Ekind (Comp) = E_Component
930 and then Nkind (Parent (Comp)) = N_Component_Declaration
931 then
aa5147f0
ES
932 -- The expression for a dynamic component may be rewritten
933 -- as a dereference, so retrieve original node.
934
935 Expr := Original_Node (Expression (Parent (Comp)));
996ae0b0 936
aa5147f0 937 -- Return True if the expression is a call to a function
1d57c04f
AC
938 -- (including an attribute function such as Image, or a
939 -- user-defined operator) with a result that requires a
940 -- transient scope.
fbf5a39b 941
aa5147f0 942 if (Nkind (Expr) = N_Function_Call
1d57c04f 943 or else Nkind (Expr) in N_Op
aa5147f0
ES
944 or else (Nkind (Expr) = N_Attribute_Reference
945 and then Present (Expressions (Expr))))
996ae0b0
RK
946 and then Requires_Transient_Scope (Etype (Expr))
947 then
948 return True;
949
950 elsif Uses_SS (Etype (Comp)) then
951 return True;
952 end if;
953 end if;
954
955 Next_Component (Comp);
956 end loop;
957
958 return False;
959
960 else
961 return False;
962 end if;
963 end Uses_SS;
964
07fc65c4
GB
965 -- Start of processing for Check_Initialization_Call
966
996ae0b0 967 begin
0669bebe 968 -- Establish a transient scope if the type needs it
07fc65c4 969
0669bebe 970 if Uses_SS (Typ) then
996ae0b0
RK
971 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
972 end if;
973 end Check_Initialization_Call;
974
f61580d4
AC
975 ---------------------------------------
976 -- Check_No_Direct_Boolean_Operators --
977 ---------------------------------------
978
979 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
980 begin
981 if Scope (Entity (N)) = Standard_Standard
982 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
983 then
6fb4cdde 984 -- Restriction only applies to original source code
f61580d4 985
6fb4cdde 986 if Comes_From_Source (N) then
f61580d4
AC
987 Check_Restriction (No_Direct_Boolean_Operators, N);
988 end if;
989 end if;
a36c1c3e
RD
990
991 if Style_Check then
992 Check_Boolean_Operator (N);
993 end if;
f61580d4
AC
994 end Check_No_Direct_Boolean_Operators;
995
996ae0b0
RK
996 ------------------------------
997 -- Check_Parameterless_Call --
998 ------------------------------
999
1000 procedure Check_Parameterless_Call (N : Node_Id) is
1001 Nam : Node_Id;
1002
bc5f3720
RD
1003 function Prefix_Is_Access_Subp return Boolean;
1004 -- If the prefix is of an access_to_subprogram type, the node must be
1005 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1006 -- interpretations are access to subprograms.
1007
1008 ---------------------------
1009 -- Prefix_Is_Access_Subp --
1010 ---------------------------
1011
1012 function Prefix_Is_Access_Subp return Boolean is
1013 I : Interp_Index;
1014 It : Interp;
1015
1016 begin
22b77f68 1017 -- If the context is an attribute reference that can apply to
b4a4936b 1018 -- functions, this is never a parameterless call (RM 4.1.4(6)).
96d2756f
AC
1019
1020 if Nkind (Parent (N)) = N_Attribute_Reference
1021 and then (Attribute_Name (Parent (N)) = Name_Address
1022 or else Attribute_Name (Parent (N)) = Name_Code_Address
1023 or else Attribute_Name (Parent (N)) = Name_Access)
1024 then
1025 return False;
1026 end if;
1027
bc5f3720
RD
1028 if not Is_Overloaded (N) then
1029 return
1030 Ekind (Etype (N)) = E_Subprogram_Type
1031 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1032 else
1033 Get_First_Interp (N, I, It);
1034 while Present (It.Typ) loop
1035 if Ekind (It.Typ) /= E_Subprogram_Type
1036 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1037 then
1038 return False;
1039 end if;
1040
1041 Get_Next_Interp (I, It);
1042 end loop;
1043
1044 return True;
1045 end if;
1046 end Prefix_Is_Access_Subp;
1047
1048 -- Start of processing for Check_Parameterless_Call
1049
996ae0b0 1050 begin
07fc65c4
GB
1051 -- Defend against junk stuff if errors already detected
1052
1053 if Total_Errors_Detected /= 0 then
1054 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1055 return;
1056 elsif Nkind (N) in N_Has_Chars
1057 and then Chars (N) in Error_Name_Or_No_Name
1058 then
1059 return;
1060 end if;
fbf5a39b
AC
1061
1062 Require_Entity (N);
996ae0b0
RK
1063 end if;
1064
45fc7ddb
HK
1065 -- If the context expects a value, and the name is a procedure, this is
1066 -- most likely a missing 'Access. Don't try to resolve the parameterless
1067 -- call, error will be caught when the outer call is analyzed.
18c0ecbe
AC
1068
1069 if Is_Entity_Name (N)
1070 and then Ekind (Entity (N)) = E_Procedure
1071 and then not Is_Overloaded (N)
1072 and then
45fc7ddb
HK
1073 Nkind_In (Parent (N), N_Parameter_Association,
1074 N_Function_Call,
1075 N_Procedure_Call_Statement)
18c0ecbe
AC
1076 then
1077 return;
1078 end if;
1079
45fc7ddb
HK
1080 -- Rewrite as call if overloadable entity that is (or could be, in the
1081 -- overloaded case) a function call. If we know for sure that the entity
1082 -- is an enumeration literal, we do not rewrite it.
f4b049db 1083
e1d9659d
AC
1084 -- If the entity is the name of an operator, it cannot be a call because
1085 -- operators cannot have default parameters. In this case, this must be
1086 -- a string whose contents coincide with an operator name. Set the kind
96d2756f 1087 -- of the node appropriately.
996ae0b0
RK
1088
1089 if (Is_Entity_Name (N)
e1d9659d 1090 and then Nkind (N) /= N_Operator_Symbol
996ae0b0
RK
1091 and then Is_Overloadable (Entity (N))
1092 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
964f13da 1093 or else Is_Overloaded (N)))
996ae0b0 1094
09494c32 1095 -- Rewrite as call if it is an explicit dereference of an expression of
f3d57416 1096 -- a subprogram access type, and the subprogram type is not that of a
996ae0b0
RK
1097 -- procedure or entry.
1098
1099 or else
bc5f3720 1100 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
996ae0b0
RK
1101
1102 -- Rewrite as call if it is a selected component which is a function,
1103 -- this is the case of a call to a protected function (which may be
1104 -- overloaded with other protected operations).
1105
1106 or else
1107 (Nkind (N) = N_Selected_Component
1108 and then (Ekind (Entity (Selector_Name (N))) = E_Function
964f13da
RD
1109 or else
1110 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1111 E_Procedure)
1112 and then Is_Overloaded (Selector_Name (N)))))
996ae0b0
RK
1113
1114 -- If one of the above three conditions is met, rewrite as call.
1115 -- Apply the rewriting only once.
1116
1117 then
1118 if Nkind (Parent (N)) /= N_Function_Call
1119 or else N /= Name (Parent (N))
1120 then
1121 Nam := New_Copy (N);
1122
bc5f3720 1123 -- If overloaded, overload set belongs to new copy
996ae0b0
RK
1124
1125 Save_Interps (N, Nam);
1126
1127 -- Change node to parameterless function call (note that the
1128 -- Parameter_Associations associations field is left set to Empty,
1129 -- its normal default value since there are no parameters)
1130
1131 Change_Node (N, N_Function_Call);
1132 Set_Name (N, Nam);
1133 Set_Sloc (N, Sloc (Nam));
1134 Analyze_Call (N);
1135 end if;
1136
1137 elsif Nkind (N) = N_Parameter_Association then
1138 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
e1d9659d
AC
1139
1140 elsif Nkind (N) = N_Operator_Symbol then
1141 Change_Operator_Symbol_To_String_Literal (N);
1142 Set_Is_Overloaded (N, False);
1143 Set_Etype (N, Any_String);
996ae0b0
RK
1144 end if;
1145 end Check_Parameterless_Call;
1146
67ce0d7e
RD
1147 -----------------------------
1148 -- Is_Definite_Access_Type --
1149 -----------------------------
1150
1151 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1152 Btyp : constant Entity_Id := Base_Type (E);
1153 begin
1154 return Ekind (Btyp) = E_Access_Type
1155 or else (Ekind (Btyp) = E_Access_Subprogram_Type
72e9f2b9 1156 and then Comes_From_Source (Btyp));
67ce0d7e
RD
1157 end Is_Definite_Access_Type;
1158
996ae0b0
RK
1159 ----------------------
1160 -- Is_Predefined_Op --
1161 ----------------------
1162
1163 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1164 begin
6a497607
AC
1165 -- Predefined operators are intrinsic subprograms
1166
1167 if not Is_Intrinsic_Subprogram (Nam) then
1168 return False;
1169 end if;
1170
1171 -- A call to a back-end builtin is never a predefined operator
1172
1173 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1174 return False;
1175 end if;
1176
1177 return not Is_Generic_Instance (Nam)
996ae0b0 1178 and then Chars (Nam) in Any_Operator_Name
6a497607 1179 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
996ae0b0
RK
1180 end Is_Predefined_Op;
1181
1182 -----------------------------
1183 -- Make_Call_Into_Operator --
1184 -----------------------------
1185
1186 procedure Make_Call_Into_Operator
1187 (N : Node_Id;
1188 Typ : Entity_Id;
1189 Op_Id : Entity_Id)
1190 is
1191 Op_Name : constant Name_Id := Chars (Op_Id);
1192 Act1 : Node_Id := First_Actual (N);
1193 Act2 : Node_Id := Next_Actual (Act1);
1194 Error : Boolean := False;
2820d220
AC
1195 Func : constant Entity_Id := Entity (Name (N));
1196 Is_Binary : constant Boolean := Present (Act2);
996ae0b0
RK
1197 Op_Node : Node_Id;
1198 Opnd_Type : Entity_Id;
1199 Orig_Type : Entity_Id := Empty;
1200 Pack : Entity_Id;
1201
1202 type Kind_Test is access function (E : Entity_Id) return Boolean;
1203
996ae0b0 1204 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
b4a4936b
AC
1205 -- If the operand is not universal, and the operator is given by an
1206 -- expanded name, verify that the operand has an interpretation with a
1207 -- type defined in the given scope of the operator.
996ae0b0
RK
1208
1209 function Type_In_P (Test : Kind_Test) return Entity_Id;
b4a4936b
AC
1210 -- Find a type of the given class in package Pack that contains the
1211 -- operator.
996ae0b0 1212
996ae0b0
RK
1213 ---------------------------
1214 -- Operand_Type_In_Scope --
1215 ---------------------------
1216
1217 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1218 Nod : constant Node_Id := Right_Opnd (Op_Node);
1219 I : Interp_Index;
1220 It : Interp;
1221
1222 begin
1223 if not Is_Overloaded (Nod) then
1224 return Scope (Base_Type (Etype (Nod))) = S;
1225
1226 else
1227 Get_First_Interp (Nod, I, It);
996ae0b0 1228 while Present (It.Typ) loop
996ae0b0
RK
1229 if Scope (Base_Type (It.Typ)) = S then
1230 return True;
1231 end if;
1232
1233 Get_Next_Interp (I, It);
1234 end loop;
1235
1236 return False;
1237 end if;
1238 end Operand_Type_In_Scope;
1239
1240 ---------------
1241 -- Type_In_P --
1242 ---------------
1243
1244 function Type_In_P (Test : Kind_Test) return Entity_Id is
1245 E : Entity_Id;
1246
1247 function In_Decl return Boolean;
1248 -- Verify that node is not part of the type declaration for the
1249 -- candidate type, which would otherwise be invisible.
1250
1251 -------------
1252 -- In_Decl --
1253 -------------
1254
1255 function In_Decl return Boolean is
1256 Decl_Node : constant Node_Id := Parent (E);
1257 N2 : Node_Id;
1258
1259 begin
1260 N2 := N;
1261
1262 if Etype (E) = Any_Type then
1263 return True;
1264
1265 elsif No (Decl_Node) then
1266 return False;
1267
1268 else
1269 while Present (N2)
1270 and then Nkind (N2) /= N_Compilation_Unit
1271 loop
1272 if N2 = Decl_Node then
1273 return True;
1274 else
1275 N2 := Parent (N2);
1276 end if;
1277 end loop;
1278
1279 return False;
1280 end if;
1281 end In_Decl;
1282
1283 -- Start of processing for Type_In_P
1284
1285 begin
b4a4936b
AC
1286 -- If the context type is declared in the prefix package, this is the
1287 -- desired base type.
996ae0b0 1288
b4a4936b 1289 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
996ae0b0
RK
1290 return Base_Type (Typ);
1291
1292 else
1293 E := First_Entity (Pack);
996ae0b0 1294 while Present (E) loop
996ae0b0
RK
1295 if Test (E)
1296 and then not In_Decl
1297 then
1298 return E;
1299 end if;
1300
1301 Next_Entity (E);
1302 end loop;
1303
1304 return Empty;
1305 end if;
1306 end Type_In_P;
1307
996ae0b0
RK
1308 -- Start of processing for Make_Call_Into_Operator
1309
1310 begin
1311 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1312
1313 -- Binary operator
1314
1315 if Is_Binary then
1316 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1317 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1318 Save_Interps (Act1, Left_Opnd (Op_Node));
1319 Save_Interps (Act2, Right_Opnd (Op_Node));
1320 Act1 := Left_Opnd (Op_Node);
1321 Act2 := Right_Opnd (Op_Node);
1322
1323 -- Unary operator
1324
1325 else
1326 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1327 Save_Interps (Act1, Right_Opnd (Op_Node));
1328 Act1 := Right_Opnd (Op_Node);
1329 end if;
1330
1331 -- If the operator is denoted by an expanded name, and the prefix is
1332 -- not Standard, but the operator is a predefined one whose scope is
1333 -- Standard, then this is an implicit_operator, inserted as an
1334 -- interpretation by the procedure of the same name. This procedure
1335 -- overestimates the presence of implicit operators, because it does
1336 -- not examine the type of the operands. Verify now that the operand
1337 -- type appears in the given scope. If right operand is universal,
1338 -- check the other operand. In the case of concatenation, either
1339 -- argument can be the component type, so check the type of the result.
1340 -- If both arguments are literals, look for a type of the right kind
1341 -- defined in the given scope. This elaborate nonsense is brought to
1342 -- you courtesy of b33302a. The type itself must be frozen, so we must
1343 -- find the type of the proper class in the given scope.
1344
06f2efd7
TQ
1345 -- A final wrinkle is the multiplication operator for fixed point types,
1346 -- which is defined in Standard only, and not in the scope of the
b4a4936b 1347 -- fixed point type itself.
996ae0b0
RK
1348
1349 if Nkind (Name (N)) = N_Expanded_Name then
1350 Pack := Entity (Prefix (Name (N)));
1351
06f2efd7
TQ
1352 -- If the entity being called is defined in the given package, it is
1353 -- a renaming of a predefined operator, and known to be legal.
996ae0b0
RK
1354
1355 if Scope (Entity (Name (N))) = Pack
1356 and then Pack /= Standard_Standard
1357 then
1358 null;
1359
9ebe3743
HK
1360 -- Visibility does not need to be checked in an instance: if the
1361 -- operator was not visible in the generic it has been diagnosed
1362 -- already, else there is an implicit copy of it in the instance.
1363
1364 elsif In_Instance then
1365 null;
1366
12577815 1367 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
996ae0b0
RK
1368 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1369 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1370 then
1371 if Pack /= Standard_Standard then
1372 Error := True;
1373 end if;
1374
b4a4936b 1375 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
06f2efd7 1376 -- available.
c8ef728f 1377
0791fbe9 1378 elsif Ada_Version >= Ada_2005
c8ef728f
ES
1379 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1380 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1381 then
1382 null;
1383
996ae0b0
RK
1384 else
1385 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1386
1387 if Op_Name = Name_Op_Concat then
1388 Opnd_Type := Base_Type (Typ);
1389
1390 elsif (Scope (Opnd_Type) = Standard_Standard
1391 and then Is_Binary)
1392 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1393 and then Is_Binary
1394 and then not Comes_From_Source (Opnd_Type))
1395 then
1396 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1397 end if;
1398
1399 if Scope (Opnd_Type) = Standard_Standard then
1400
1401 -- Verify that the scope contains a type that corresponds to
1402 -- the given literal. Optimize the case where Pack is Standard.
1403
1404 if Pack /= Standard_Standard then
1405
1406 if Opnd_Type = Universal_Integer then
06f2efd7 1407 Orig_Type := Type_In_P (Is_Integer_Type'Access);
996ae0b0
RK
1408
1409 elsif Opnd_Type = Universal_Real then
1410 Orig_Type := Type_In_P (Is_Real_Type'Access);
1411
1412 elsif Opnd_Type = Any_String then
1413 Orig_Type := Type_In_P (Is_String_Type'Access);
1414
1415 elsif Opnd_Type = Any_Access then
06f2efd7 1416 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
996ae0b0
RK
1417
1418 elsif Opnd_Type = Any_Composite then
1419 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1420
1421 if Present (Orig_Type) then
1422 if Has_Private_Component (Orig_Type) then
1423 Orig_Type := Empty;
1424 else
1425 Set_Etype (Act1, Orig_Type);
1426
1427 if Is_Binary then
1428 Set_Etype (Act2, Orig_Type);
1429 end if;
1430 end if;
1431 end if;
1432
1433 else
1434 Orig_Type := Empty;
1435 end if;
1436
1437 Error := No (Orig_Type);
1438 end if;
1439
1440 elsif Ekind (Opnd_Type) = E_Allocator_Type
1441 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1442 then
1443 Error := True;
1444
1445 -- If the type is defined elsewhere, and the operator is not
1446 -- defined in the given scope (by a renaming declaration, e.g.)
1447 -- then this is an error as well. If an extension of System is
1448 -- present, and the type may be defined there, Pack must be
1449 -- System itself.
1450
1451 elsif Scope (Opnd_Type) /= Pack
1452 and then Scope (Op_Id) /= Pack
1453 and then (No (System_Aux_Id)
1454 or else Scope (Opnd_Type) /= System_Aux_Id
1455 or else Pack /= Scope (System_Aux_Id))
1456 then
244e5a2c
AC
1457 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1458 Error := True;
1459 else
1460 Error := not Operand_Type_In_Scope (Pack);
1461 end if;
996ae0b0
RK
1462
1463 elsif Pack = Standard_Standard
1464 and then not Operand_Type_In_Scope (Standard_Standard)
1465 then
1466 Error := True;
1467 end if;
1468 end if;
1469
1470 if Error then
1471 Error_Msg_Node_2 := Pack;
1472 Error_Msg_NE
1473 ("& not declared in&", N, Selector_Name (Name (N)));
1474 Set_Etype (N, Any_Type);
1475 return;
88b17d45
AC
1476
1477 -- Detect a mismatch between the context type and the result type
1478 -- in the named package, which is otherwise not detected if the
1479 -- operands are universal. Check is only needed if source entity is
1480 -- an operator, not a function that renames an operator.
1481
1482 elsif Nkind (Parent (N)) /= N_Type_Conversion
1483 and then Ekind (Entity (Name (N))) = E_Operator
1484 and then Is_Numeric_Type (Typ)
1485 and then not Is_Universal_Numeric_Type (Typ)
1486 and then Scope (Base_Type (Typ)) /= Pack
1487 and then not In_Instance
1488 then
1489 if Is_Fixed_Point_Type (Typ)
1490 and then (Op_Name = Name_Op_Multiply
1491 or else
1492 Op_Name = Name_Op_Divide)
1493 then
1494 -- Already checked above
1495
1496 null;
1497
e86a3a7e 1498 -- Operator may be defined in an extension of System
80c3be7a
AC
1499
1500 elsif Present (System_Aux_Id)
1501 and then Scope (Opnd_Type) = System_Aux_Id
1502 then
1503 null;
1504
88b17d45 1505 else
be5a1b93
TQ
1506 -- Could we use Wrong_Type here??? (this would require setting
1507 -- Etype (N) to the actual type found where Typ was expected).
1508
e86a3a7e 1509 Error_Msg_NE ("expect }", N, Typ);
88b17d45 1510 end if;
996ae0b0
RK
1511 end if;
1512 end if;
1513
1514 Set_Chars (Op_Node, Op_Name);
fbf5a39b
AC
1515
1516 if not Is_Private_Type (Etype (N)) then
1517 Set_Etype (Op_Node, Base_Type (Etype (N)));
1518 else
1519 Set_Etype (Op_Node, Etype (N));
1520 end if;
1521
2820d220
AC
1522 -- If this is a call to a function that renames a predefined equality,
1523 -- the renaming declaration provides a type that must be used to
1524 -- resolve the operands. This must be done now because resolution of
1525 -- the equality node will not resolve any remaining ambiguity, and it
1526 -- assumes that the first operand is not overloaded.
1527
1528 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1529 and then Ekind (Func) = E_Function
1530 and then Is_Overloaded (Act1)
1531 then
1532 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1533 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1534 end if;
1535
996ae0b0
RK
1536 Set_Entity (Op_Node, Op_Id);
1537 Generate_Reference (Op_Id, N, ' ');
45fc7ddb
HK
1538
1539 -- Do rewrite setting Comes_From_Source on the result if the original
1540 -- call came from source. Although it is not strictly the case that the
1541 -- operator as such comes from the source, logically it corresponds
1542 -- exactly to the function call in the source, so it should be marked
1543 -- this way (e.g. to make sure that validity checks work fine).
1544
1545 declare
1546 CS : constant Boolean := Comes_From_Source (N);
1547 begin
1548 Rewrite (N, Op_Node);
1549 Set_Comes_From_Source (N, CS);
1550 end;
fbf5a39b
AC
1551
1552 -- If this is an arithmetic operator and the result type is private,
1553 -- the operands and the result must be wrapped in conversion to
1554 -- expose the underlying numeric type and expand the proper checks,
1555 -- e.g. on division.
1556
1557 if Is_Private_Type (Typ) then
1558 case Nkind (N) is
1559 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1560 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1561 Resolve_Intrinsic_Operator (N, Typ);
1562
1563 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1564 Resolve_Intrinsic_Unary_Operator (N, Typ);
1565
1566 when others =>
1567 Resolve (N, Typ);
1568 end case;
1569 else
1570 Resolve (N, Typ);
1571 end if;
996ae0b0
RK
1572 end Make_Call_Into_Operator;
1573
1574 -------------------
1575 -- Operator_Kind --
1576 -------------------
1577
1578 function Operator_Kind
1579 (Op_Name : Name_Id;
0ab80019 1580 Is_Binary : Boolean) return Node_Kind
996ae0b0
RK
1581 is
1582 Kind : Node_Kind;
1583
1584 begin
b0186f71
AC
1585 -- Use CASE statement or array???
1586
996ae0b0 1587 if Is_Binary then
aa5147f0
ES
1588 if Op_Name = Name_Op_And then
1589 Kind := N_Op_And;
1590 elsif Op_Name = Name_Op_Or then
1591 Kind := N_Op_Or;
1592 elsif Op_Name = Name_Op_Xor then
1593 Kind := N_Op_Xor;
1594 elsif Op_Name = Name_Op_Eq then
1595 Kind := N_Op_Eq;
1596 elsif Op_Name = Name_Op_Ne then
1597 Kind := N_Op_Ne;
1598 elsif Op_Name = Name_Op_Lt then
1599 Kind := N_Op_Lt;
1600 elsif Op_Name = Name_Op_Le then
1601 Kind := N_Op_Le;
1602 elsif Op_Name = Name_Op_Gt then
1603 Kind := N_Op_Gt;
1604 elsif Op_Name = Name_Op_Ge then
1605 Kind := N_Op_Ge;
1606 elsif Op_Name = Name_Op_Add then
1607 Kind := N_Op_Add;
1608 elsif Op_Name = Name_Op_Subtract then
1609 Kind := N_Op_Subtract;
1610 elsif Op_Name = Name_Op_Concat then
1611 Kind := N_Op_Concat;
1612 elsif Op_Name = Name_Op_Multiply then
1613 Kind := N_Op_Multiply;
1614 elsif Op_Name = Name_Op_Divide then
1615 Kind := N_Op_Divide;
1616 elsif Op_Name = Name_Op_Mod then
1617 Kind := N_Op_Mod;
1618 elsif Op_Name = Name_Op_Rem then
1619 Kind := N_Op_Rem;
1620 elsif Op_Name = Name_Op_Expon then
1621 Kind := N_Op_Expon;
996ae0b0
RK
1622 else
1623 raise Program_Error;
1624 end if;
1625
1626 -- Unary operators
1627
1628 else
aa5147f0
ES
1629 if Op_Name = Name_Op_Add then
1630 Kind := N_Op_Plus;
1631 elsif Op_Name = Name_Op_Subtract then
1632 Kind := N_Op_Minus;
1633 elsif Op_Name = Name_Op_Abs then
1634 Kind := N_Op_Abs;
1635 elsif Op_Name = Name_Op_Not then
1636 Kind := N_Op_Not;
996ae0b0
RK
1637 else
1638 raise Program_Error;
1639 end if;
1640 end if;
1641
1642 return Kind;
1643 end Operator_Kind;
1644
45fc7ddb
HK
1645 ----------------------------
1646 -- Preanalyze_And_Resolve --
1647 ----------------------------
996ae0b0 1648
45fc7ddb 1649 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
996ae0b0
RK
1650 Save_Full_Analysis : constant Boolean := Full_Analysis;
1651
1652 begin
1653 Full_Analysis := False;
1654 Expander_Mode_Save_And_Set (False);
1655
1656 -- We suppress all checks for this analysis, since the checks will
1657 -- be applied properly, and in the right location, when the default
1658 -- expression is reanalyzed and reexpanded later on.
1659
1660 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1661
1662 Expander_Mode_Restore;
1663 Full_Analysis := Save_Full_Analysis;
45fc7ddb 1664 end Preanalyze_And_Resolve;
996ae0b0 1665
a77842bd 1666 -- Version without context type
996ae0b0 1667
45fc7ddb 1668 procedure Preanalyze_And_Resolve (N : Node_Id) is
996ae0b0
RK
1669 Save_Full_Analysis : constant Boolean := Full_Analysis;
1670
1671 begin
1672 Full_Analysis := False;
1673 Expander_Mode_Save_And_Set (False);
1674
1675 Analyze (N);
1676 Resolve (N, Etype (N), Suppress => All_Checks);
1677
1678 Expander_Mode_Restore;
1679 Full_Analysis := Save_Full_Analysis;
45fc7ddb 1680 end Preanalyze_And_Resolve;
996ae0b0
RK
1681
1682 ----------------------------------
1683 -- Replace_Actual_Discriminants --
1684 ----------------------------------
1685
1686 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1687 Loc : constant Source_Ptr := Sloc (N);
1688 Tsk : Node_Id := Empty;
1689
1690 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1691
1692 -------------------
1693 -- Process_Discr --
1694 -------------------
1695
1696 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1697 Ent : Entity_Id;
1698
1699 begin
1700 if Nkind (Nod) = N_Identifier then
1701 Ent := Entity (Nod);
1702
1703 if Present (Ent)
1704 and then Ekind (Ent) = E_Discriminant
1705 then
1706 Rewrite (Nod,
1707 Make_Selected_Component (Loc,
1708 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1709 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1710
1711 Set_Etype (Nod, Etype (Ent));
1712 end if;
1713
1714 end if;
1715
1716 return OK;
1717 end Process_Discr;
1718
1719 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1720
1721 -- Start of processing for Replace_Actual_Discriminants
1722
1723 begin
1724 if not Expander_Active then
1725 return;
1726 end if;
1727
1728 if Nkind (Name (N)) = N_Selected_Component then
1729 Tsk := Prefix (Name (N));
1730
1731 elsif Nkind (Name (N)) = N_Indexed_Component then
1732 Tsk := Prefix (Prefix (Name (N)));
1733 end if;
1734
1735 if No (Tsk) then
1736 return;
1737 else
1738 Replace_Discrs (Default);
1739 end if;
1740 end Replace_Actual_Discriminants;
1741
1742 -------------
1743 -- Resolve --
1744 -------------
1745
1746 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
dae2b8ea
HK
1747 Ambiguous : Boolean := False;
1748 Ctx_Type : Entity_Id := Typ;
1749 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1750 Err_Type : Entity_Id := Empty;
1751 Found : Boolean := False;
1752 From_Lib : Boolean;
996ae0b0 1753 I : Interp_Index;
dae2b8ea 1754 I1 : Interp_Index := 0; -- prevent junk warning
996ae0b0
RK
1755 It : Interp;
1756 It1 : Interp;
996ae0b0 1757 Seen : Entity_Id := Empty; -- prevent junk warning
dae2b8ea
HK
1758
1759 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1760 -- Determine whether a node comes from a predefined library unit or
1761 -- Standard.
996ae0b0
RK
1762
1763 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1764 -- Try and fix up a literal so that it matches its expected type. New
1765 -- literals are manufactured if necessary to avoid cascaded errors.
1766
7415029d
AC
1767 procedure Report_Ambiguous_Argument;
1768 -- Additional diagnostics when an ambiguous call has an ambiguous
1769 -- argument (typically a controlling actual).
1770
996ae0b0
RK
1771 procedure Resolution_Failed;
1772 -- Called when attempt at resolving current expression fails
1773
dae2b8ea
HK
1774 ------------------------------------
1775 -- Comes_From_Predefined_Lib_Unit --
1776 -------------------------------------
1777
1778 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1779 begin
1780 return
1781 Sloc (Nod) = Standard_Location
1782 or else Is_Predefined_File_Name (Unit_File_Name (
1783 Get_Source_Unit (Sloc (Nod))));
1784 end Comes_From_Predefined_Lib_Unit;
1785
996ae0b0
RK
1786 --------------------
1787 -- Patch_Up_Value --
1788 --------------------
1789
1790 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1791 begin
1792 if Nkind (N) = N_Integer_Literal
1793 and then Is_Real_Type (Typ)
1794 then
1795 Rewrite (N,
1796 Make_Real_Literal (Sloc (N),
1797 Realval => UR_From_Uint (Intval (N))));
1798 Set_Etype (N, Universal_Real);
1799 Set_Is_Static_Expression (N);
1800
1801 elsif Nkind (N) = N_Real_Literal
1802 and then Is_Integer_Type (Typ)
1803 then
1804 Rewrite (N,
1805 Make_Integer_Literal (Sloc (N),
1806 Intval => UR_To_Uint (Realval (N))));
1807 Set_Etype (N, Universal_Integer);
1808 Set_Is_Static_Expression (N);
45fc7ddb 1809
996ae0b0
RK
1810 elsif Nkind (N) = N_String_Literal
1811 and then Is_Character_Type (Typ)
1812 then
1813 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1814 Rewrite (N,
1815 Make_Character_Literal (Sloc (N),
1816 Chars => Name_Find,
82c80734
RD
1817 Char_Literal_Value =>
1818 UI_From_Int (Character'Pos ('A'))));
996ae0b0
RK
1819 Set_Etype (N, Any_Character);
1820 Set_Is_Static_Expression (N);
1821
1822 elsif Nkind (N) /= N_String_Literal
1823 and then Is_String_Type (Typ)
1824 then
1825 Rewrite (N,
1826 Make_String_Literal (Sloc (N),
1827 Strval => End_String));
1828
1829 elsif Nkind (N) = N_Range then
1830 Patch_Up_Value (Low_Bound (N), Typ);
1831 Patch_Up_Value (High_Bound (N), Typ);
1832 end if;
1833 end Patch_Up_Value;
1834
7415029d
AC
1835 -------------------------------
1836 -- Report_Ambiguous_Argument --
1837 -------------------------------
1838
1839 procedure Report_Ambiguous_Argument is
1840 Arg : constant Node_Id := First (Parameter_Associations (N));
1841 I : Interp_Index;
1842 It : Interp;
1843
1844 begin
1845 if Nkind (Arg) = N_Function_Call
1846 and then Is_Entity_Name (Name (Arg))
1847 and then Is_Overloaded (Name (Arg))
1848 then
ed2233dc 1849 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
7415029d 1850
bfc07071
AC
1851 -- Could use comments on what is going on here ???
1852
7415029d
AC
1853 Get_First_Interp (Name (Arg), I, It);
1854 while Present (It.Nam) loop
1855 Error_Msg_Sloc := Sloc (It.Nam);
1856
1857 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
ed2233dc 1858 Error_Msg_N ("interpretation (inherited) #!", Arg);
7415029d 1859 else
ed2233dc 1860 Error_Msg_N ("interpretation #!", Arg);
7415029d
AC
1861 end if;
1862
1863 Get_Next_Interp (I, It);
1864 end loop;
1865 end if;
1866 end Report_Ambiguous_Argument;
1867
996ae0b0
RK
1868 -----------------------
1869 -- Resolution_Failed --
1870 -----------------------
1871
1872 procedure Resolution_Failed is
1873 begin
1874 Patch_Up_Value (N, Typ);
1875 Set_Etype (N, Typ);
1876 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1877 Set_Is_Overloaded (N, False);
1878
1879 -- The caller will return without calling the expander, so we need
1880 -- to set the analyzed flag. Note that it is fine to set Analyzed
1881 -- to True even if we are in the middle of a shallow analysis,
1882 -- (see the spec of sem for more details) since this is an error
1883 -- situation anyway, and there is no point in repeating the
1884 -- analysis later (indeed it won't work to repeat it later, since
1885 -- we haven't got a clear resolution of which entity is being
1886 -- referenced.)
1887
1888 Set_Analyzed (N, True);
1889 return;
1890 end Resolution_Failed;
1891
1892 -- Start of processing for Resolve
1893
1894 begin
5c736541
RD
1895 if N = Error then
1896 return;
1897 end if;
1898
996ae0b0
RK
1899 -- Access attribute on remote subprogram cannot be used for
1900 -- a non-remote access-to-subprogram type.
1901
1902 if Nkind (N) = N_Attribute_Reference
1903 and then (Attribute_Name (N) = Name_Access
ea985d95
RD
1904 or else Attribute_Name (N) = Name_Unrestricted_Access
1905 or else Attribute_Name (N) = Name_Unchecked_Access)
996ae0b0
RK
1906 and then Comes_From_Source (N)
1907 and then Is_Entity_Name (Prefix (N))
1908 and then Is_Subprogram (Entity (Prefix (N)))
1909 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1910 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1911 then
1912 Error_Msg_N
1913 ("prefix must statically denote a non-remote subprogram", N);
1914 end if;
1915
dae2b8ea
HK
1916 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1917
996ae0b0
RK
1918 -- If the context is a Remote_Access_To_Subprogram, access attributes
1919 -- must be resolved with the corresponding fat pointer. There is no need
1920 -- to check for the attribute name since the return type of an
1921 -- attribute is never a remote type.
1922
1923 if Nkind (N) = N_Attribute_Reference
1924 and then Comes_From_Source (N)
1925 and then (Is_Remote_Call_Interface (Typ)
1926 or else Is_Remote_Types (Typ))
1927 then
1928 declare
1929 Attr : constant Attribute_Id :=
1930 Get_Attribute_Id (Attribute_Name (N));
1931 Pref : constant Node_Id := Prefix (N);
1932 Decl : Node_Id;
1933 Spec : Node_Id;
1934 Is_Remote : Boolean := True;
1935
1936 begin
a77842bd 1937 -- Check that Typ is a remote access-to-subprogram type
996ae0b0 1938
a77842bd 1939 if Is_Remote_Access_To_Subprogram_Type (Typ) then
955871d3 1940
996ae0b0
RK
1941 -- Prefix (N) must statically denote a remote subprogram
1942 -- declared in a package specification.
1943
1944 if Attr = Attribute_Access then
1945 Decl := Unit_Declaration_Node (Entity (Pref));
1946
1947 if Nkind (Decl) = N_Subprogram_Body then
1948 Spec := Corresponding_Spec (Decl);
1949
1950 if not No (Spec) then
1951 Decl := Unit_Declaration_Node (Spec);
1952 end if;
1953 end if;
1954
1955 Spec := Parent (Decl);
1956
1957 if not Is_Entity_Name (Prefix (N))
1958 or else Nkind (Spec) /= N_Package_Specification
1959 or else
1960 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1961 then
1962 Is_Remote := False;
1963 Error_Msg_N
1964 ("prefix must statically denote a remote subprogram ",
1965 N);
1966 end if;
1967 end if;
1968
fbf5a39b
AC
1969 -- If we are generating code for a distributed program.
1970 -- perform semantic checks against the corresponding
1971 -- remote entities.
1972
1973 if (Attr = Attribute_Access
1974 or else Attr = Attribute_Unchecked_Access
1975 or else Attr = Attribute_Unrestricted_Access)
1976 and then Expander_Active
a77842bd 1977 and then Get_PCS_Name /= Name_No_DSA
996ae0b0
RK
1978 then
1979 Check_Subtype_Conformant
1980 (New_Id => Entity (Prefix (N)),
1981 Old_Id => Designated_Type
1982 (Corresponding_Remote_Type (Typ)),
1983 Err_Loc => N);
b7d1f17f 1984
996ae0b0
RK
1985 if Is_Remote then
1986 Process_Remote_AST_Attribute (N, Typ);
1987 end if;
1988 end if;
1989 end if;
1990 end;
1991 end if;
1992
1993 Debug_A_Entry ("resolving ", N);
1994
07fc65c4
GB
1995 if Comes_From_Source (N) then
1996 if Is_Fixed_Point_Type (Typ) then
1997 Check_Restriction (No_Fixed_Point, N);
996ae0b0 1998
07fc65c4
GB
1999 elsif Is_Floating_Point_Type (Typ)
2000 and then Typ /= Universal_Real
2001 and then Typ /= Any_Real
2002 then
2003 Check_Restriction (No_Floating_Point, N);
2004 end if;
996ae0b0
RK
2005 end if;
2006
2007 -- Return if already analyzed
2008
2009 if Analyzed (N) then
2010 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2011 return;
2012
2013 -- Return if type = Any_Type (previous error encountered)
2014
2015 elsif Etype (N) = Any_Type then
2016 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2017 return;
2018 end if;
2019
2020 Check_Parameterless_Call (N);
2021
2022 -- If not overloaded, then we know the type, and all that needs doing
2023 -- is to check that this type is compatible with the context.
2024
2025 if not Is_Overloaded (N) then
2026 Found := Covers (Typ, Etype (N));
2027 Expr_Type := Etype (N);
2028
2029 -- In the overloaded case, we must select the interpretation that
2030 -- is compatible with the context (i.e. the type passed to Resolve)
2031
2032 else
996ae0b0
RK
2033 -- Loop through possible interpretations
2034
1420b484 2035 Get_First_Interp (N, I, It);
996ae0b0
RK
2036 Interp_Loop : while Present (It.Typ) loop
2037
2038 -- We are only interested in interpretations that are compatible
aa5147f0 2039 -- with the expected type, any other interpretations are ignored.
996ae0b0 2040
fbf5a39b
AC
2041 if not Covers (Typ, It.Typ) then
2042 if Debug_Flag_V then
2043 Write_Str (" interpretation incompatible with context");
2044 Write_Eol;
2045 end if;
996ae0b0 2046
fbf5a39b 2047 else
aa5147f0
ES
2048 -- Skip the current interpretation if it is disabled by an
2049 -- abstract operator. This action is performed only when the
2050 -- type against which we are resolving is the same as the
2051 -- type of the interpretation.
2052
0791fbe9 2053 if Ada_Version >= Ada_2005
aa5147f0
ES
2054 and then It.Typ = Typ
2055 and then Typ /= Universal_Integer
2056 and then Typ /= Universal_Real
2057 and then Present (It.Abstract_Op)
2058 then
2059 goto Continue;
2060 end if;
2061
996ae0b0
RK
2062 -- First matching interpretation
2063
2064 if not Found then
2065 Found := True;
2066 I1 := I;
2067 Seen := It.Nam;
2068 Expr_Type := It.Typ;
2069
fbf5a39b 2070 -- Matching interpretation that is not the first, maybe an
996ae0b0
RK
2071 -- error, but there are some cases where preference rules are
2072 -- used to choose between the two possibilities. These and
2073 -- some more obscure cases are handled in Disambiguate.
2074
2075 else
dae2b8ea
HK
2076 -- If the current statement is part of a predefined library
2077 -- unit, then all interpretations which come from user level
2078 -- packages should not be considered.
2079
2080 if From_Lib
2081 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2082 then
2083 goto Continue;
2084 end if;
2085
996ae0b0
RK
2086 Error_Msg_Sloc := Sloc (Seen);
2087 It1 := Disambiguate (N, I1, I, Typ);
2088
fbf5a39b
AC
2089 -- Disambiguation has succeeded. Skip the remaining
2090 -- interpretations.
996ae0b0 2091
fbf5a39b
AC
2092 if It1 /= No_Interp then
2093 Seen := It1.Nam;
2094 Expr_Type := It1.Typ;
2095
2096 while Present (It.Typ) loop
2097 Get_Next_Interp (I, It);
2098 end loop;
2099
2100 else
996ae0b0
RK
2101 -- Before we issue an ambiguity complaint, check for
2102 -- the case of a subprogram call where at least one
2103 -- of the arguments is Any_Type, and if so, suppress
2104 -- the message, since it is a cascaded error.
2105
45fc7ddb
HK
2106 if Nkind_In (N, N_Function_Call,
2107 N_Procedure_Call_Statement)
996ae0b0
RK
2108 then
2109 declare
1420b484 2110 A : Node_Id;
996ae0b0
RK
2111 E : Node_Id;
2112
2113 begin
1420b484 2114 A := First_Actual (N);
996ae0b0
RK
2115 while Present (A) loop
2116 E := A;
2117
2118 if Nkind (E) = N_Parameter_Association then
2119 E := Explicit_Actual_Parameter (E);
2120 end if;
2121
2122 if Etype (E) = Any_Type then
2123 if Debug_Flag_V then
2124 Write_Str ("Any_Type in call");
2125 Write_Eol;
2126 end if;
2127
2128 exit Interp_Loop;
2129 end if;
2130
2131 Next_Actual (A);
2132 end loop;
2133 end;
2134
aa5147f0 2135 elsif Nkind (N) in N_Binary_Op
996ae0b0
RK
2136 and then (Etype (Left_Opnd (N)) = Any_Type
2137 or else Etype (Right_Opnd (N)) = Any_Type)
2138 then
2139 exit Interp_Loop;
2140
2141 elsif Nkind (N) in N_Unary_Op
2142 and then Etype (Right_Opnd (N)) = Any_Type
2143 then
2144 exit Interp_Loop;
2145 end if;
2146
2147 -- Not that special case, so issue message using the
2148 -- flag Ambiguous to control printing of the header
2149 -- message only at the start of an ambiguous set.
2150
2151 if not Ambiguous then
aa180613
RD
2152 if Nkind (N) = N_Function_Call
2153 and then Nkind (Name (N)) = N_Explicit_Dereference
2154 then
ed2233dc 2155 Error_Msg_N
aa180613
RD
2156 ("ambiguous expression "
2157 & "(cannot resolve indirect call)!", N);
2158 else
483c78cb 2159 Error_Msg_NE -- CODEFIX
aa180613
RD
2160 ("ambiguous expression (cannot resolve&)!",
2161 N, It.Nam);
2162 end if;
fbf5a39b 2163
996ae0b0 2164 Ambiguous := True;
0669bebe
GB
2165
2166 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
ed2233dc 2167 Error_Msg_N
0669bebe
GB
2168 ("\\possible interpretation (inherited)#!", N);
2169 else
4e7a4f6e
AC
2170 Error_Msg_N -- CODEFIX
2171 ("\\possible interpretation#!", N);
0669bebe 2172 end if;
7415029d
AC
2173
2174 if Nkind_In
4519314c 2175 (N, N_Procedure_Call_Statement, N_Function_Call)
7415029d
AC
2176 and then Present (Parameter_Associations (N))
2177 then
2178 Report_Ambiguous_Argument;
2179 end if;
996ae0b0
RK
2180 end if;
2181
2182 Error_Msg_Sloc := Sloc (It.Nam);
996ae0b0 2183
fbf5a39b 2184 -- By default, the error message refers to the candidate
0669bebe
GB
2185 -- interpretation. But if it is a predefined operator, it
2186 -- is implicitly declared at the declaration of the type
2187 -- of the operand. Recover the sloc of that declaration
2188 -- for the error message.
fbf5a39b
AC
2189
2190 if Nkind (N) in N_Op
2191 and then Scope (It.Nam) = Standard_Standard
2192 and then not Is_Overloaded (Right_Opnd (N))
0669bebe
GB
2193 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2194 Standard_Standard
fbf5a39b
AC
2195 then
2196 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2197
2198 if Comes_From_Source (Err_Type)
2199 and then Present (Parent (Err_Type))
2200 then
2201 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2202 end if;
2203
2204 elsif Nkind (N) in N_Binary_Op
2205 and then Scope (It.Nam) = Standard_Standard
2206 and then not Is_Overloaded (Left_Opnd (N))
0669bebe
GB
2207 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2208 Standard_Standard
fbf5a39b
AC
2209 then
2210 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2211
2212 if Comes_From_Source (Err_Type)
2213 and then Present (Parent (Err_Type))
2214 then
2215 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2216 end if;
aa180613
RD
2217
2218 -- If this is an indirect call, use the subprogram_type
2219 -- in the message, to have a meaningful location.
4519314c 2220 -- Also indicate if this is an inherited operation,
aa180613
RD
2221 -- created by a type declaration.
2222
2223 elsif Nkind (N) = N_Function_Call
2224 and then Nkind (Name (N)) = N_Explicit_Dereference
2225 and then Is_Type (It.Nam)
2226 then
2227 Err_Type := It.Nam;
2228 Error_Msg_Sloc :=
2229 Sloc (Associated_Node_For_Itype (Err_Type));
fbf5a39b
AC
2230 else
2231 Err_Type := Empty;
2232 end if;
2233
2234 if Nkind (N) in N_Op
2235 and then Scope (It.Nam) = Standard_Standard
2236 and then Present (Err_Type)
2237 then
aa5147f0
ES
2238 -- Special-case the message for universal_fixed
2239 -- operators, which are not declared with the type
2240 -- of the operand, but appear forever in Standard.
2241
2242 if It.Typ = Universal_Fixed
2243 and then Scope (It.Nam) = Standard_Standard
2244 then
ed2233dc 2245 Error_Msg_N
aa5147f0
ES
2246 ("\\possible interpretation as " &
2247 "universal_fixed operation " &
2248 "(RM 4.5.5 (19))", N);
2249 else
ed2233dc 2250 Error_Msg_N
aa5147f0
ES
2251 ("\\possible interpretation (predefined)#!", N);
2252 end if;
aa180613
RD
2253
2254 elsif
2255 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2256 then
ed2233dc 2257 Error_Msg_N
aa180613 2258 ("\\possible interpretation (inherited)#!", N);
fbf5a39b 2259 else
4e7a4f6e
AC
2260 Error_Msg_N -- CODEFIX
2261 ("\\possible interpretation#!", N);
fbf5a39b 2262 end if;
996ae0b0 2263
996ae0b0
RK
2264 end if;
2265 end if;
2266
0669bebe
GB
2267 -- We have a matching interpretation, Expr_Type is the type
2268 -- from this interpretation, and Seen is the entity.
996ae0b0 2269
0669bebe
GB
2270 -- For an operator, just set the entity name. The type will be
2271 -- set by the specific operator resolution routine.
996ae0b0
RK
2272
2273 if Nkind (N) in N_Op then
2274 Set_Entity (N, Seen);
2275 Generate_Reference (Seen, N);
2276
19d846a0
RD
2277 elsif Nkind (N) = N_Case_Expression then
2278 Set_Etype (N, Expr_Type);
2279
996ae0b0
RK
2280 elsif Nkind (N) = N_Character_Literal then
2281 Set_Etype (N, Expr_Type);
2282
e0ba1bfd
ES
2283 elsif Nkind (N) = N_Conditional_Expression then
2284 Set_Etype (N, Expr_Type);
2285
996ae0b0 2286 -- For an explicit dereference, attribute reference, range,
0669bebe
GB
2287 -- short-circuit form (which is not an operator node), or call
2288 -- with a name that is an explicit dereference, there is
2289 -- nothing to be done at this point.
996ae0b0 2290
45fc7ddb
HK
2291 elsif Nkind_In (N, N_Explicit_Dereference,
2292 N_Attribute_Reference,
2293 N_And_Then,
2294 N_Indexed_Component,
2295 N_Or_Else,
2296 N_Range,
2297 N_Selected_Component,
2298 N_Slice)
996ae0b0
RK
2299 or else Nkind (Name (N)) = N_Explicit_Dereference
2300 then
2301 null;
2302
0669bebe 2303 -- For procedure or function calls, set the type of the name,
4519314c 2304 -- and also the entity pointer for the prefix.
996ae0b0 2305
45fc7ddb 2306 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
a3f2babd 2307 and then Is_Entity_Name (Name (N))
996ae0b0
RK
2308 then
2309 Set_Etype (Name (N), Expr_Type);
2310 Set_Entity (Name (N), Seen);
2311 Generate_Reference (Seen, Name (N));
2312
2313 elsif Nkind (N) = N_Function_Call
2314 and then Nkind (Name (N)) = N_Selected_Component
2315 then
2316 Set_Etype (Name (N), Expr_Type);
2317 Set_Entity (Selector_Name (Name (N)), Seen);
2318 Generate_Reference (Seen, Selector_Name (Name (N)));
2319
2320 -- For all other cases, just set the type of the Name
2321
2322 else
2323 Set_Etype (Name (N), Expr_Type);
2324 end if;
2325
996ae0b0
RK
2326 end if;
2327
aa5147f0
ES
2328 <<Continue>>
2329
996ae0b0
RK
2330 -- Move to next interpretation
2331
c8ef728f 2332 exit Interp_Loop when No (It.Typ);
996ae0b0
RK
2333
2334 Get_Next_Interp (I, It);
2335 end loop Interp_Loop;
2336 end if;
2337
2338 -- At this stage Found indicates whether or not an acceptable
4519314c
AC
2339 -- interpretation exists. If not, then we have an error, except that if
2340 -- the context is Any_Type as a result of some other error, then we
2341 -- suppress the error report.
996ae0b0
RK
2342
2343 if not Found then
2344 if Typ /= Any_Type then
2345
0669bebe
GB
2346 -- If type we are looking for is Void, then this is the procedure
2347 -- call case, and the error is simply that what we gave is not a
2348 -- procedure name (we think of procedure calls as expressions with
2349 -- types internally, but the user doesn't think of them this way!)
996ae0b0
RK
2350
2351 if Typ = Standard_Void_Type then
91b1417d
AC
2352
2353 -- Special case message if function used as a procedure
2354
2355 if Nkind (N) = N_Procedure_Call_Statement
2356 and then Is_Entity_Name (Name (N))
2357 and then Ekind (Entity (Name (N))) = E_Function
2358 then
2359 Error_Msg_NE
2360 ("cannot use function & in a procedure call",
2361 Name (N), Entity (Name (N)));
2362
0669bebe
GB
2363 -- Otherwise give general message (not clear what cases this
2364 -- covers, but no harm in providing for them!)
91b1417d
AC
2365
2366 else
2367 Error_Msg_N ("expect procedure name in procedure call", N);
2368 end if;
2369
996ae0b0
RK
2370 Found := True;
2371
2372 -- Otherwise we do have a subexpression with the wrong type
2373
0669bebe
GB
2374 -- Check for the case of an allocator which uses an access type
2375 -- instead of the designated type. This is a common error and we
2376 -- specialize the message, posting an error on the operand of the
2377 -- allocator, complaining that we expected the designated type of
2378 -- the allocator.
996ae0b0
RK
2379
2380 elsif Nkind (N) = N_Allocator
2381 and then Ekind (Typ) in Access_Kind
2382 and then Ekind (Etype (N)) in Access_Kind
2383 and then Designated_Type (Etype (N)) = Typ
2384 then
2385 Wrong_Type (Expression (N), Designated_Type (Typ));
2386 Found := True;
2387
0669bebe
GB
2388 -- Check for view mismatch on Null in instances, for which the
2389 -- view-swapping mechanism has no identifier.
17be0cdf
ES
2390
2391 elsif (In_Instance or else In_Inlined_Body)
2392 and then (Nkind (N) = N_Null)
2393 and then Is_Private_Type (Typ)
2394 and then Is_Access_Type (Full_View (Typ))
2395 then
2396 Resolve (N, Full_View (Typ));
2397 Set_Etype (N, Typ);
2398 return;
2399
aa180613
RD
2400 -- Check for an aggregate. Sometimes we can get bogus aggregates
2401 -- from misuse of parentheses, and we are about to complain about
2402 -- the aggregate without even looking inside it.
996ae0b0 2403
aa180613
RD
2404 -- Instead, if we have an aggregate of type Any_Composite, then
2405 -- analyze and resolve the component fields, and then only issue
2406 -- another message if we get no errors doing this (otherwise
2407 -- assume that the errors in the aggregate caused the problem).
996ae0b0
RK
2408
2409 elsif Nkind (N) = N_Aggregate
2410 and then Etype (N) = Any_Composite
2411 then
996ae0b0
RK
2412 -- Disable expansion in any case. If there is a type mismatch
2413 -- it may be fatal to try to expand the aggregate. The flag
2414 -- would otherwise be set to false when the error is posted.
2415
2416 Expander_Active := False;
2417
2418 declare
2419 procedure Check_Aggr (Aggr : Node_Id);
aa180613
RD
2420 -- Check one aggregate, and set Found to True if we have a
2421 -- definite error in any of its elements
996ae0b0
RK
2422
2423 procedure Check_Elmt (Aelmt : Node_Id);
aa180613
RD
2424 -- Check one element of aggregate and set Found to True if
2425 -- we definitely have an error in the element.
2426
2427 ----------------
2428 -- Check_Aggr --
2429 ----------------
996ae0b0
RK
2430
2431 procedure Check_Aggr (Aggr : Node_Id) is
2432 Elmt : Node_Id;
2433
2434 begin
2435 if Present (Expressions (Aggr)) then
2436 Elmt := First (Expressions (Aggr));
2437 while Present (Elmt) loop
2438 Check_Elmt (Elmt);
2439 Next (Elmt);
2440 end loop;
2441 end if;
2442
2443 if Present (Component_Associations (Aggr)) then
2444 Elmt := First (Component_Associations (Aggr));
2445 while Present (Elmt) loop
aa180613 2446
0669bebe
GB
2447 -- If this is a default-initialized component, then
2448 -- there is nothing to check. The box will be
2449 -- replaced by the appropriate call during late
2450 -- expansion.
aa180613
RD
2451
2452 if not Box_Present (Elmt) then
2453 Check_Elmt (Expression (Elmt));
2454 end if;
2455
996ae0b0
RK
2456 Next (Elmt);
2457 end loop;
2458 end if;
2459 end Check_Aggr;
2460
fbf5a39b
AC
2461 ----------------
2462 -- Check_Elmt --
2463 ----------------
2464
996ae0b0
RK
2465 procedure Check_Elmt (Aelmt : Node_Id) is
2466 begin
2467 -- If we have a nested aggregate, go inside it (to
2468 -- attempt a naked analyze-resolve of the aggregate
2469 -- can cause undesirable cascaded errors). Do not
2470 -- resolve expression if it needs a type from context,
2471 -- as for integer * fixed expression.
2472
2473 if Nkind (Aelmt) = N_Aggregate then
2474 Check_Aggr (Aelmt);
2475
2476 else
2477 Analyze (Aelmt);
2478
2479 if not Is_Overloaded (Aelmt)
2480 and then Etype (Aelmt) /= Any_Fixed
2481 then
fbf5a39b 2482 Resolve (Aelmt);
996ae0b0
RK
2483 end if;
2484
2485 if Etype (Aelmt) = Any_Type then
2486 Found := True;
2487 end if;
2488 end if;
2489 end Check_Elmt;
2490
2491 begin
2492 Check_Aggr (N);
2493 end;
2494 end if;
2495
2496 -- If an error message was issued already, Found got reset
2497 -- to True, so if it is still False, issue the standard
2498 -- Wrong_Type message.
2499
2500 if not Found then
2501 if Is_Overloaded (N)
2502 and then Nkind (N) = N_Function_Call
2503 then
65356e64
AC
2504 declare
2505 Subp_Name : Node_Id;
2506 begin
2507 if Is_Entity_Name (Name (N)) then
2508 Subp_Name := Name (N);
2509
2510 elsif Nkind (Name (N)) = N_Selected_Component then
2511
a77842bd 2512 -- Protected operation: retrieve operation name
65356e64
AC
2513
2514 Subp_Name := Selector_Name (Name (N));
2515 else
2516 raise Program_Error;
2517 end if;
2518
2519 Error_Msg_Node_2 := Typ;
2520 Error_Msg_NE ("no visible interpretation of&" &
2521 " matches expected type&", N, Subp_Name);
2522 end;
996ae0b0
RK
2523
2524 if All_Errors_Mode then
2525 declare
2526 Index : Interp_Index;
2527 It : Interp;
2528
2529 begin
aa180613 2530 Error_Msg_N ("\\possible interpretations:", N);
996ae0b0 2531
1420b484 2532 Get_First_Interp (Name (N), Index, It);
996ae0b0 2533 while Present (It.Nam) loop
ea985d95 2534 Error_Msg_Sloc := Sloc (It.Nam);
aa5147f0
ES
2535 Error_Msg_Node_2 := It.Nam;
2536 Error_Msg_NE
2537 ("\\ type& for & declared#", N, It.Typ);
996ae0b0
RK
2538 Get_Next_Interp (Index, It);
2539 end loop;
2540 end;
aa5147f0 2541
996ae0b0
RK
2542 else
2543 Error_Msg_N ("\use -gnatf for details", N);
2544 end if;
2545 else
2546 Wrong_Type (N, Typ);
2547 end if;
2548 end if;
2549 end if;
2550
2551 Resolution_Failed;
2552 return;
2553
2554 -- Test if we have more than one interpretation for the context
2555
2556 elsif Ambiguous then
2557 Resolution_Failed;
2558 return;
2559
2560 -- Here we have an acceptable interpretation for the context
2561
2562 else
996ae0b0
RK
2563 -- Propagate type information and normalize tree for various
2564 -- predefined operations. If the context only imposes a class of
2565 -- types, rather than a specific type, propagate the actual type
2566 -- downward.
2567
2568 if Typ = Any_Integer
2569 or else Typ = Any_Boolean
2570 or else Typ = Any_Modular
2571 or else Typ = Any_Real
2572 or else Typ = Any_Discrete
2573 then
2574 Ctx_Type := Expr_Type;
2575
2576 -- Any_Fixed is legal in a real context only if a specific
2577 -- fixed point type is imposed. If Norman Cohen can be
2578 -- confused by this, it deserves a separate message.
2579
2580 if Typ = Any_Real
2581 and then Expr_Type = Any_Fixed
2582 then
758c442c 2583 Error_Msg_N ("illegal context for mixed mode operation", N);
996ae0b0
RK
2584 Set_Etype (N, Universal_Real);
2585 Ctx_Type := Universal_Real;
2586 end if;
2587 end if;
2588
f3d57416 2589 -- A user-defined operator is transformed into a function call at
0ab80019
AC
2590 -- this point, so that further processing knows that operators are
2591 -- really operators (i.e. are predefined operators). User-defined
2592 -- operators that are intrinsic are just renamings of the predefined
2593 -- ones, and need not be turned into calls either, but if they rename
2594 -- a different operator, we must transform the node accordingly.
2595 -- Instantiations of Unchecked_Conversion are intrinsic but are
2596 -- treated as functions, even if given an operator designator.
2597
2598 if Nkind (N) in N_Op
2599 and then Present (Entity (N))
2600 and then Ekind (Entity (N)) /= E_Operator
2601 then
2602
2603 if not Is_Predefined_Op (Entity (N)) then
2604 Rewrite_Operator_As_Call (N, Entity (N));
2605
615cbd95
AC
2606 elsif Present (Alias (Entity (N)))
2607 and then
45fc7ddb
HK
2608 Nkind (Parent (Parent (Entity (N)))) =
2609 N_Subprogram_Renaming_Declaration
615cbd95 2610 then
0ab80019
AC
2611 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2612
2613 -- If the node is rewritten, it will be fully resolved in
2614 -- Rewrite_Renamed_Operator.
2615
2616 if Analyzed (N) then
2617 return;
2618 end if;
2619 end if;
2620 end if;
2621
996ae0b0
RK
2622 case N_Subexpr'(Nkind (N)) is
2623
2624 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2625
2626 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2627
514d0fc5 2628 when N_Short_Circuit
996ae0b0
RK
2629 => Resolve_Short_Circuit (N, Ctx_Type);
2630
2631 when N_Attribute_Reference
2632 => Resolve_Attribute (N, Ctx_Type);
2633
19d846a0
RD
2634 when N_Case_Expression
2635 => Resolve_Case_Expression (N, Ctx_Type);
2636
996ae0b0
RK
2637 when N_Character_Literal
2638 => Resolve_Character_Literal (N, Ctx_Type);
2639
2640 when N_Conditional_Expression
2641 => Resolve_Conditional_Expression (N, Ctx_Type);
2642
2643 when N_Expanded_Name
2644 => Resolve_Entity_Name (N, Ctx_Type);
2645
996ae0b0
RK
2646 when N_Explicit_Dereference
2647 => Resolve_Explicit_Dereference (N, Ctx_Type);
2648
955871d3
AC
2649 when N_Expression_With_Actions
2650 => Resolve_Expression_With_Actions (N, Ctx_Type);
2651
2652 when N_Extension_Aggregate
2653 => Resolve_Extension_Aggregate (N, Ctx_Type);
2654
996ae0b0
RK
2655 when N_Function_Call
2656 => Resolve_Call (N, Ctx_Type);
2657
2658 when N_Identifier
2659 => Resolve_Entity_Name (N, Ctx_Type);
2660
996ae0b0
RK
2661 when N_Indexed_Component
2662 => Resolve_Indexed_Component (N, Ctx_Type);
2663
2664 when N_Integer_Literal
2665 => Resolve_Integer_Literal (N, Ctx_Type);
2666
0669bebe
GB
2667 when N_Membership_Test
2668 => Resolve_Membership_Op (N, Ctx_Type);
2669
996ae0b0
RK
2670 when N_Null => Resolve_Null (N, Ctx_Type);
2671
2672 when N_Op_And | N_Op_Or | N_Op_Xor
2673 => Resolve_Logical_Op (N, Ctx_Type);
2674
2675 when N_Op_Eq | N_Op_Ne
2676 => Resolve_Equality_Op (N, Ctx_Type);
2677
2678 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2679 => Resolve_Comparison_Op (N, Ctx_Type);
2680
2681 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2682
2683 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2684 N_Op_Divide | N_Op_Mod | N_Op_Rem
2685
2686 => Resolve_Arithmetic_Op (N, Ctx_Type);
2687
2688 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2689
2690 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2691
2692 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2693 => Resolve_Unary_Op (N, Ctx_Type);
2694
2695 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2696
2697 when N_Procedure_Call_Statement
2698 => Resolve_Call (N, Ctx_Type);
2699
2700 when N_Operator_Symbol
2701 => Resolve_Operator_Symbol (N, Ctx_Type);
2702
2703 when N_Qualified_Expression
2704 => Resolve_Qualified_Expression (N, Ctx_Type);
2705
a961aa79
AC
2706 when N_Quantified_Expression
2707 => Resolve_Quantified_Expression (N, Ctx_Type);
2708
996ae0b0
RK
2709 when N_Raise_xxx_Error
2710 => Set_Etype (N, Ctx_Type);
2711
2712 when N_Range => Resolve_Range (N, Ctx_Type);
2713
2714 when N_Real_Literal
2715 => Resolve_Real_Literal (N, Ctx_Type);
2716
2717 when N_Reference => Resolve_Reference (N, Ctx_Type);
2718
2719 when N_Selected_Component
2720 => Resolve_Selected_Component (N, Ctx_Type);
2721
2722 when N_Slice => Resolve_Slice (N, Ctx_Type);
2723
2724 when N_String_Literal
2725 => Resolve_String_Literal (N, Ctx_Type);
2726
2727 when N_Subprogram_Info
2728 => Resolve_Subprogram_Info (N, Ctx_Type);
2729
2730 when N_Type_Conversion
2731 => Resolve_Type_Conversion (N, Ctx_Type);
2732
2733 when N_Unchecked_Expression =>
2734 Resolve_Unchecked_Expression (N, Ctx_Type);
2735
2736 when N_Unchecked_Type_Conversion =>
2737 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
996ae0b0
RK
2738 end case;
2739
2740 -- If the subexpression was replaced by a non-subexpression, then
2741 -- all we do is to expand it. The only legitimate case we know of
2742 -- is converting procedure call statement to entry call statements,
2743 -- but there may be others, so we are making this test general.
2744
2745 if Nkind (N) not in N_Subexpr then
2746 Debug_A_Exit ("resolving ", N, " (done)");
2747 Expand (N);
2748 return;
2749 end if;
2750
1e194575
AC
2751 -- AI05-144-2: Check dangerous order dependence within an expression
2752 -- that is not a subexpression. Exclude RHS of an assignment, because
2753 -- both sides may have side-effects and the check must be performed
2754 -- over the statement.
2755
2756 if Nkind (Parent (N)) not in N_Subexpr
2757 and then Nkind (Parent (N)) /= N_Assignment_Statement
2758 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2759 then
2760 Check_Order_Dependence;
2761 end if;
2762
996ae0b0
RK
2763 -- The expression is definitely NOT overloaded at this point, so
2764 -- we reset the Is_Overloaded flag to avoid any confusion when
2765 -- reanalyzing the node.
2766
2767 Set_Is_Overloaded (N, False);
2768
2769 -- Freeze expression type, entity if it is a name, and designated
fbf5a39b 2770 -- type if it is an allocator (RM 13.14(10,11,13)).
996ae0b0
RK
2771
2772 -- Now that the resolution of the type of the node is complete,
2773 -- and we did not detect an error, we can expand this node. We
2774 -- skip the expand call if we are in a default expression, see
2775 -- section "Handling of Default Expressions" in Sem spec.
2776
2777 Debug_A_Exit ("resolving ", N, " (done)");
2778
2779 -- We unconditionally freeze the expression, even if we are in
2780 -- default expression mode (the Freeze_Expression routine tests
2781 -- this flag and only freezes static types if it is set).
2782
2783 Freeze_Expression (N);
2784
2785 -- Now we can do the expansion
2786
2787 Expand (N);
2788 end if;
996ae0b0
RK
2789 end Resolve;
2790
fbf5a39b
AC
2791 -------------
2792 -- Resolve --
2793 -------------
2794
996ae0b0
RK
2795 -- Version with check(s) suppressed
2796
2797 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2798 begin
2799 if Suppress = All_Checks then
2800 declare
fbf5a39b 2801 Svg : constant Suppress_Array := Scope_Suppress;
996ae0b0
RK
2802 begin
2803 Scope_Suppress := (others => True);
2804 Resolve (N, Typ);
2805 Scope_Suppress := Svg;
2806 end;
2807
2808 else
2809 declare
fbf5a39b 2810 Svg : constant Boolean := Scope_Suppress (Suppress);
996ae0b0 2811 begin
fbf5a39b 2812 Scope_Suppress (Suppress) := True;
996ae0b0 2813 Resolve (N, Typ);
fbf5a39b 2814 Scope_Suppress (Suppress) := Svg;
996ae0b0
RK
2815 end;
2816 end if;
2817 end Resolve;
2818
fbf5a39b
AC
2819 -------------
2820 -- Resolve --
2821 -------------
2822
2823 -- Version with implicit type
2824
2825 procedure Resolve (N : Node_Id) is
2826 begin
2827 Resolve (N, Etype (N));
2828 end Resolve;
2829
996ae0b0
RK
2830 ---------------------
2831 -- Resolve_Actuals --
2832 ---------------------
2833
2834 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2835 Loc : constant Source_Ptr := Sloc (N);
2836 A : Node_Id;
2837 F : Entity_Id;
2838 A_Typ : Entity_Id;
2839 F_Typ : Entity_Id;
2840 Prev : Node_Id := Empty;
67ce0d7e 2841 Orig_A : Node_Id;
996ae0b0 2842
45fc7ddb
HK
2843 procedure Check_Argument_Order;
2844 -- Performs a check for the case where the actuals are all simple
2845 -- identifiers that correspond to the formal names, but in the wrong
2846 -- order, which is considered suspicious and cause for a warning.
2847
b7d1f17f
HK
2848 procedure Check_Prefixed_Call;
2849 -- If the original node is an overloaded call in prefix notation,
2850 -- insert an 'Access or a dereference as needed over the first actual.
2851 -- Try_Object_Operation has already verified that there is a valid
2852 -- interpretation, but the form of the actual can only be determined
2853 -- once the primitive operation is identified.
2854
996ae0b0
RK
2855 procedure Insert_Default;
2856 -- If the actual is missing in a call, insert in the actuals list
2857 -- an instance of the default expression. The insertion is always
2858 -- a named association.
2859
fbf5a39b
AC
2860 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2861 -- Check whether T1 and T2, or their full views, are derived from a
2862 -- common type. Used to enforce the restrictions on array conversions
2863 -- of AI95-00246.
2864
a7a3cf5c
AC
2865 function Static_Concatenation (N : Node_Id) return Boolean;
2866 -- Predicate to determine whether an actual that is a concatenation
2867 -- will be evaluated statically and does not need a transient scope.
2868 -- This must be determined before the actual is resolved and expanded
2869 -- because if needed the transient scope must be introduced earlier.
2870
45fc7ddb
HK
2871 --------------------------
2872 -- Check_Argument_Order --
2873 --------------------------
2874
2875 procedure Check_Argument_Order is
2876 begin
2877 -- Nothing to do if no parameters, or original node is neither a
2878 -- function call nor a procedure call statement (happens in the
2879 -- operator-transformed-to-function call case), or the call does
2880 -- not come from source, or this warning is off.
2881
2882 if not Warn_On_Parameter_Order
2883 or else
2884 No (Parameter_Associations (N))
2885 or else
2886 not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2887 N_Function_Call)
2888 or else
2889 not Comes_From_Source (N)
2890 then
2891 return;
2892 end if;
2893
2894 declare
2895 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2896
2897 begin
2898 -- Nothing to do if only one parameter
2899
2900 if Nargs < 2 then
2901 return;
2902 end if;
2903
2904 -- Here if at least two arguments
2905
2906 declare
2907 Actuals : array (1 .. Nargs) of Node_Id;
2908 Actual : Node_Id;
2909 Formal : Node_Id;
2910
2911 Wrong_Order : Boolean := False;
2912 -- Set True if an out of order case is found
2913
2914 begin
2915 -- Collect identifier names of actuals, fail if any actual is
2916 -- not a simple identifier, and record max length of name.
2917
2918 Actual := First (Parameter_Associations (N));
2919 for J in Actuals'Range loop
2920 if Nkind (Actual) /= N_Identifier then
2921 return;
2922 else
2923 Actuals (J) := Actual;
2924 Next (Actual);
2925 end if;
2926 end loop;
2927
2928 -- If we got this far, all actuals are identifiers and the list
2929 -- of their names is stored in the Actuals array.
2930
2931 Formal := First_Formal (Nam);
2932 for J in Actuals'Range loop
2933
2934 -- If we ran out of formals, that's odd, probably an error
2935 -- which will be detected elsewhere, but abandon the search.
2936
2937 if No (Formal) then
2938 return;
2939 end if;
2940
2941 -- If name matches and is in order OK
2942
2943 if Chars (Formal) = Chars (Actuals (J)) then
2944 null;
2945
2946 else
2947 -- If no match, see if it is elsewhere in list and if so
2948 -- flag potential wrong order if type is compatible.
2949
2950 for K in Actuals'Range loop
2951 if Chars (Formal) = Chars (Actuals (K))
2952 and then
2953 Has_Compatible_Type (Actuals (K), Etype (Formal))
2954 then
2955 Wrong_Order := True;
2956 goto Continue;
2957 end if;
2958 end loop;
2959
2960 -- No match
2961
2962 return;
2963 end if;
2964
2965 <<Continue>> Next_Formal (Formal);
2966 end loop;
2967
2968 -- If Formals left over, also probably an error, skip warning
2969
2970 if Present (Formal) then
2971 return;
2972 end if;
2973
2974 -- Here we give the warning if something was out of order
2975
2976 if Wrong_Order then
2977 Error_Msg_N
2978 ("actuals for this call may be in wrong order?", N);
2979 end if;
2980 end;
2981 end;
2982 end Check_Argument_Order;
2983
b7d1f17f
HK
2984 -------------------------
2985 -- Check_Prefixed_Call --
2986 -------------------------
2987
2988 procedure Check_Prefixed_Call is
2989 Act : constant Node_Id := First_Actual (N);
2990 A_Type : constant Entity_Id := Etype (Act);
2991 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2992 Orig : constant Node_Id := Original_Node (N);
2993 New_A : Node_Id;
2994
2995 begin
2996 -- Check whether the call is a prefixed call, with or without
2997 -- additional actuals.
2998
2999 if Nkind (Orig) = N_Selected_Component
3000 or else
3001 (Nkind (Orig) = N_Indexed_Component
3002 and then Nkind (Prefix (Orig)) = N_Selected_Component
3003 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3004 and then Is_Entity_Name (Act)
3005 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3006 then
3007 if Is_Access_Type (A_Type)
3008 and then not Is_Access_Type (F_Type)
3009 then
3010 -- Introduce dereference on object in prefix
3011
3012 New_A :=
3013 Make_Explicit_Dereference (Sloc (Act),
3014 Prefix => Relocate_Node (Act));
3015 Rewrite (Act, New_A);
3016 Analyze (Act);
3017
3018 elsif Is_Access_Type (F_Type)
3019 and then not Is_Access_Type (A_Type)
3020 then
3021 -- Introduce an implicit 'Access in prefix
3022
3023 if not Is_Aliased_View (Act) then
ed2233dc 3024 Error_Msg_NE
b7d1f17f 3025 ("object in prefixed call to& must be aliased"
aa5147f0 3026 & " (RM-2005 4.3.1 (13))",
b7d1f17f
HK
3027 Prefix (Act), Nam);
3028 end if;
3029
3030 Rewrite (Act,
3031 Make_Attribute_Reference (Loc,
3032 Attribute_Name => Name_Access,
3033 Prefix => Relocate_Node (Act)));
3034 end if;
3035
3036 Analyze (Act);
3037 end if;
3038 end Check_Prefixed_Call;
3039
996ae0b0
RK
3040 --------------------
3041 -- Insert_Default --
3042 --------------------
3043
3044 procedure Insert_Default is
3045 Actval : Node_Id;
3046 Assoc : Node_Id;
3047
3048 begin
fbf5a39b 3049 -- Missing argument in call, nothing to insert
996ae0b0 3050
fbf5a39b
AC
3051 if No (Default_Value (F)) then
3052 return;
3053
3054 else
3055 -- Note that we do a full New_Copy_Tree, so that any associated
3056 -- Itypes are properly copied. This may not be needed any more,
3057 -- but it does no harm as a safety measure! Defaults of a generic
3058 -- formal may be out of bounds of the corresponding actual (see
3059 -- cc1311b) and an additional check may be required.
996ae0b0 3060
b7d1f17f
HK
3061 Actval :=
3062 New_Copy_Tree
3063 (Default_Value (F),
3064 New_Scope => Current_Scope,
3065 New_Sloc => Loc);
996ae0b0
RK
3066
3067 if Is_Concurrent_Type (Scope (Nam))
3068 and then Has_Discriminants (Scope (Nam))
3069 then
3070 Replace_Actual_Discriminants (N, Actval);
3071 end if;
3072
3073 if Is_Overloadable (Nam)
3074 and then Present (Alias (Nam))
3075 then
3076 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3077 and then not Is_Tagged_Type (Etype (F))
3078 then
3079 -- If default is a real literal, do not introduce a
3080 -- conversion whose effect may depend on the run-time
3081 -- size of universal real.
3082
3083 if Nkind (Actval) = N_Real_Literal then
3084 Set_Etype (Actval, Base_Type (Etype (F)));
3085 else
3086 Actval := Unchecked_Convert_To (Etype (F), Actval);
3087 end if;
3088 end if;
3089
3090 if Is_Scalar_Type (Etype (F)) then
3091 Enable_Range_Check (Actval);
3092 end if;
3093
996ae0b0
RK
3094 Set_Parent (Actval, N);
3095
3096 -- Resolve aggregates with their base type, to avoid scope
f3d57416 3097 -- anomalies: the subtype was first built in the subprogram
996ae0b0
RK
3098 -- declaration, and the current call may be nested.
3099
76b84bf0
AC
3100 if Nkind (Actval) = N_Aggregate then
3101 Analyze_And_Resolve (Actval, Etype (F));
996ae0b0
RK
3102 else
3103 Analyze_And_Resolve (Actval, Etype (Actval));
3104 end if;
fbf5a39b
AC
3105
3106 else
3107 Set_Parent (Actval, N);
3108
a77842bd 3109 -- See note above concerning aggregates
fbf5a39b
AC
3110
3111 if Nkind (Actval) = N_Aggregate
3112 and then Has_Discriminants (Etype (Actval))
3113 then
3114 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3115
3116 -- Resolve entities with their own type, which may differ
3117 -- from the type of a reference in a generic context (the
3118 -- view swapping mechanism did not anticipate the re-analysis
3119 -- of default values in calls).
3120
3121 elsif Is_Entity_Name (Actval) then
3122 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3123
3124 else
3125 Analyze_And_Resolve (Actval, Etype (Actval));
3126 end if;
996ae0b0
RK
3127 end if;
3128
3129 -- If default is a tag indeterminate function call, propagate
3130 -- tag to obtain proper dispatching.
3131
3132 if Is_Controlling_Formal (F)
3133 and then Nkind (Default_Value (F)) = N_Function_Call
3134 then
3135 Set_Is_Controlling_Actual (Actval);
3136 end if;
3137
996ae0b0
RK
3138 end if;
3139
3140 -- If the default expression raises constraint error, then just
3141 -- silently replace it with an N_Raise_Constraint_Error node,
3142 -- since we already gave the warning on the subprogram spec.
2604ec03
AC
3143 -- If node is already a Raise_Constraint_Error leave as is, to
3144 -- prevent loops in the warnings removal machinery.
996ae0b0 3145
2604ec03
AC
3146 if Raises_Constraint_Error (Actval)
3147 and then Nkind (Actval) /= N_Raise_Constraint_Error
3148 then
996ae0b0 3149 Rewrite (Actval,
07fc65c4
GB
3150 Make_Raise_Constraint_Error (Loc,
3151 Reason => CE_Range_Check_Failed));
996ae0b0
RK
3152 Set_Raises_Constraint_Error (Actval);
3153 Set_Etype (Actval, Etype (F));
3154 end if;
3155
3156 Assoc :=
3157 Make_Parameter_Association (Loc,
3158 Explicit_Actual_Parameter => Actval,
3159 Selector_Name => Make_Identifier (Loc, Chars (F)));
3160
3161 -- Case of insertion is first named actual
3162
3163 if No (Prev) or else
3164 Nkind (Parent (Prev)) /= N_Parameter_Association
3165 then
3166 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3167 Set_First_Named_Actual (N, Actval);
3168
3169 if No (Prev) then
c8ef728f 3170 if No (Parameter_Associations (N)) then
996ae0b0
RK
3171 Set_Parameter_Associations (N, New_List (Assoc));
3172 else
3173 Append (Assoc, Parameter_Associations (N));
3174 end if;
3175
3176 else
3177 Insert_After (Prev, Assoc);
3178 end if;
3179
3180 -- Case of insertion is not first named actual
3181
3182 else
3183 Set_Next_Named_Actual
3184 (Assoc, Next_Named_Actual (Parent (Prev)));
3185 Set_Next_Named_Actual (Parent (Prev), Actval);
3186 Append (Assoc, Parameter_Associations (N));
3187 end if;
3188
3189 Mark_Rewrite_Insertion (Assoc);
3190 Mark_Rewrite_Insertion (Actval);
3191
3192 Prev := Actval;
3193 end Insert_Default;
3194
fbf5a39b
AC
3195 -------------------
3196 -- Same_Ancestor --
3197 -------------------
3198
3199 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3200 FT1 : Entity_Id := T1;
3201 FT2 : Entity_Id := T2;
3202
3203 begin
3204 if Is_Private_Type (T1)
3205 and then Present (Full_View (T1))
3206 then
3207 FT1 := Full_View (T1);
3208 end if;
3209
3210 if Is_Private_Type (T2)
3211 and then Present (Full_View (T2))
3212 then
3213 FT2 := Full_View (T2);
3214 end if;
3215
3216 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3217 end Same_Ancestor;
3218
a7a3cf5c
AC
3219 --------------------------
3220 -- Static_Concatenation --
3221 --------------------------
3222
3223 function Static_Concatenation (N : Node_Id) return Boolean is
3224 begin
c72a85f2
TQ
3225 case Nkind (N) is
3226 when N_String_Literal =>
3227 return True;
a7a3cf5c 3228
d81b4bfe
TQ
3229 when N_Op_Concat =>
3230
4342eda9
TQ
3231 -- Concatenation is static when both operands are static
3232 -- and the concatenation operator is a predefined one.
3233
3234 return Scope (Entity (N)) = Standard_Standard
3235 and then
3236 Static_Concatenation (Left_Opnd (N))
c72a85f2
TQ
3237 and then
3238 Static_Concatenation (Right_Opnd (N));
3239
3240 when others =>
3241 if Is_Entity_Name (N) then
3242 declare
3243 Ent : constant Entity_Id := Entity (N);
3244 begin
3245 return Ekind (Ent) = E_Constant
3246 and then Present (Constant_Value (Ent))
d81b4bfe
TQ
3247 and then
3248 Is_Static_Expression (Constant_Value (Ent));
c72a85f2 3249 end;
a7a3cf5c 3250
a7a3cf5c
AC
3251 else
3252 return False;
3253 end if;
c72a85f2 3254 end case;
a7a3cf5c
AC
3255 end Static_Concatenation;
3256
996ae0b0
RK
3257 -- Start of processing for Resolve_Actuals
3258
3259 begin
45fc7ddb
HK
3260 Check_Argument_Order;
3261
b7d1f17f
HK
3262 if Present (First_Actual (N)) then
3263 Check_Prefixed_Call;
3264 end if;
3265
996ae0b0
RK
3266 A := First_Actual (N);
3267 F := First_Formal (Nam);
996ae0b0 3268 while Present (F) loop
fbf5a39b
AC
3269 if No (A) and then Needs_No_Actuals (Nam) then
3270 null;
996ae0b0 3271
d81b4bfe
TQ
3272 -- If we have an error in any actual or formal, indicated by a type
3273 -- of Any_Type, then abandon resolution attempt, and set result type
3274 -- to Any_Type.
07fc65c4 3275
fbf5a39b
AC
3276 elsif (Present (A) and then Etype (A) = Any_Type)
3277 or else Etype (F) = Any_Type
07fc65c4
GB
3278 then
3279 Set_Etype (N, Any_Type);
3280 return;
3281 end if;
3282
e65f50ec
ES
3283 -- Case where actual is present
3284
45fc7ddb 3285 -- If the actual is an entity, generate a reference to it now. We
36fcf362
RD
3286 -- do this before the actual is resolved, because a formal of some
3287 -- protected subprogram, or a task discriminant, will be rewritten
3288 -- during expansion, and the reference to the source entity may
3289 -- be lost.
3290
3291 if Present (A)
3292 and then Is_Entity_Name (A)
3293 and then Comes_From_Source (N)
3294 then
3295 Orig_A := Entity (A);
3296
3297 if Present (Orig_A) then
3298 if Is_Formal (Orig_A)
3299 and then Ekind (F) /= E_In_Parameter
3300 then
3301 Generate_Reference (Orig_A, A, 'm');
36fcf362
RD
3302 elsif not Is_Overloaded (A) then
3303 Generate_Reference (Orig_A, A);
3304 end if;
3305 end if;
3306 end if;
3307
996ae0b0
RK
3308 if Present (A)
3309 and then (Nkind (Parent (A)) /= N_Parameter_Association
3310 or else
3311 Chars (Selector_Name (Parent (A))) = Chars (F))
3312 then
45fc7ddb
HK
3313 -- If style checking mode on, check match of formal name
3314
3315 if Style_Check then
3316 if Nkind (Parent (A)) = N_Parameter_Association then
3317 Check_Identifier (Selector_Name (Parent (A)), F);
3318 end if;
3319 end if;
3320
996ae0b0
RK
3321 -- If the formal is Out or In_Out, do not resolve and expand the
3322 -- conversion, because it is subsequently expanded into explicit
3323 -- temporaries and assignments. However, the object of the
ea985d95
RD
3324 -- conversion can be resolved. An exception is the case of tagged
3325 -- type conversion with a class-wide actual. In that case we want
3326 -- the tag check to occur and no temporary will be needed (no
3327 -- representation change can occur) and the parameter is passed by
3328 -- reference, so we go ahead and resolve the type conversion.
c8ef728f 3329 -- Another exception is the case of reference to component or
ea985d95
RD
3330 -- subcomponent of a bit-packed array, in which case we want to
3331 -- defer expansion to the point the in and out assignments are
3332 -- performed.
996ae0b0
RK
3333
3334 if Ekind (F) /= E_In_Parameter
3335 and then Nkind (A) = N_Type_Conversion
3336 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3337 then
07fc65c4
GB
3338 if Ekind (F) = E_In_Out_Parameter
3339 and then Is_Array_Type (Etype (F))
07fc65c4 3340 then
038140ed
AC
3341 -- In a view conversion, the conversion must be legal in
3342 -- both directions, and thus both component types must be
3343 -- aliased, or neither (4.6 (8)).
758c442c 3344
038140ed
AC
3345 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3346 -- the privacy requirement should not apply to generic
3347 -- types, and should be checked in an instance. ARG query
3348 -- is in order ???
45fc7ddb 3349
038140ed
AC
3350 if Has_Aliased_Components (Etype (Expression (A))) /=
3351 Has_Aliased_Components (Etype (F))
3352 then
45fc7ddb
HK
3353 Error_Msg_N
3354 ("both component types in a view conversion must be"
3355 & " aliased, or neither", A);
3356
038140ed
AC
3357 -- Comment here??? what set of cases???
3358
45fc7ddb
HK
3359 elsif
3360 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3361 then
038140ed
AC
3362 -- Check view conv between unrelated by ref array types
3363
45fc7ddb
HK
3364 if Is_By_Reference_Type (Etype (F))
3365 or else Is_By_Reference_Type (Etype (Expression (A)))
758c442c
GD
3366 then
3367 Error_Msg_N
45fc7ddb
HK
3368 ("view conversion between unrelated by reference " &
3369 "array types not allowed (\'A'I-00246)", A);
038140ed
AC
3370
3371 -- In Ada 2005 mode, check view conversion component
3372 -- type cannot be private, tagged, or volatile. Note
3373 -- that we only apply this to source conversions. The
3374 -- generated code can contain conversions which are
3375 -- not subject to this test, and we cannot extract the
3376 -- component type in such cases since it is not present.
3377
3378 elsif Comes_From_Source (A)
3379 and then Ada_Version >= Ada_2005
3380 then
45fc7ddb
HK
3381 declare
3382 Comp_Type : constant Entity_Id :=
3383 Component_Type
3384 (Etype (Expression (A)));
3385 begin
038140ed
AC
3386 if (Is_Private_Type (Comp_Type)
3387 and then not Is_Generic_Type (Comp_Type))
3388 or else Is_Tagged_Type (Comp_Type)
3389 or else Is_Volatile (Comp_Type)
45fc7ddb
HK
3390 then
3391 Error_Msg_N
3392 ("component type of a view conversion cannot"
3393 & " be private, tagged, or volatile"
3394 & " (RM 4.6 (24))",
3395 Expression (A));
3396 end if;
3397 end;
758c442c 3398 end if;
fbf5a39b 3399 end if;
07fc65c4
GB
3400 end if;
3401
038140ed
AC
3402 -- Resolve expression if conversion is all OK
3403
16397eff 3404 if (Conversion_OK (A)
038140ed 3405 or else Valid_Conversion (A, Etype (A), Expression (A)))
16397eff 3406 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
996ae0b0 3407 then
fbf5a39b 3408 Resolve (Expression (A));
996ae0b0
RK
3409 end if;
3410
b7d1f17f
HK
3411 -- If the actual is a function call that returns a limited
3412 -- unconstrained object that needs finalization, create a
3413 -- transient scope for it, so that it can receive the proper
3414 -- finalization list.
3415
3416 elsif Nkind (A) = N_Function_Call
3417 and then Is_Limited_Record (Etype (F))
3418 and then not Is_Constrained (Etype (F))
3419 and then Expander_Active
3420 and then
3421 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3422 then
3423 Establish_Transient_Scope (A, False);
3424
a52fefe6
AC
3425 -- A small optimization: if one of the actuals is a concatenation
3426 -- create a block around a procedure call to recover stack space.
3427 -- This alleviates stack usage when several procedure calls in
76e776e5
AC
3428 -- the same statement list use concatenation. We do not perform
3429 -- this wrapping for code statements, where the argument is a
3430 -- static string, and we want to preserve warnings involving
3431 -- sequences of such statements.
a52fefe6
AC
3432
3433 elsif Nkind (A) = N_Op_Concat
3434 and then Nkind (N) = N_Procedure_Call_Statement
3435 and then Expander_Active
76e776e5
AC
3436 and then
3437 not (Is_Intrinsic_Subprogram (Nam)
3438 and then Chars (Nam) = Name_Asm)
a7a3cf5c 3439 and then not Static_Concatenation (A)
a52fefe6
AC
3440 then
3441 Establish_Transient_Scope (A, False);
3442 Resolve (A, Etype (F));
3443
996ae0b0 3444 else
fbf5a39b
AC
3445 if Nkind (A) = N_Type_Conversion
3446 and then Is_Array_Type (Etype (F))
3447 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3448 and then
3449 (Is_Limited_Type (Etype (F))
3450 or else Is_Limited_Type (Etype (Expression (A))))
3451 then
3452 Error_Msg_N
758c442c
GD
3453 ("conversion between unrelated limited array types " &
3454 "not allowed (\A\I-00246)", A);
fbf5a39b 3455
758c442c
GD
3456 if Is_Limited_Type (Etype (F)) then
3457 Explain_Limited_Type (Etype (F), A);
3458 end if;
fbf5a39b 3459
758c442c
GD
3460 if Is_Limited_Type (Etype (Expression (A))) then
3461 Explain_Limited_Type (Etype (Expression (A)), A);
3462 end if;
fbf5a39b
AC
3463 end if;
3464
c8ef728f
ES
3465 -- (Ada 2005: AI-251): If the actual is an allocator whose
3466 -- directly designated type is a class-wide interface, we build
3467 -- an anonymous access type to use it as the type of the
3468 -- allocator. Later, when the subprogram call is expanded, if
3469 -- the interface has a secondary dispatch table the expander
3470 -- will add a type conversion to force the correct displacement
3471 -- of the pointer.
3472
3473 if Nkind (A) = N_Allocator then
3474 declare
3475 DDT : constant Entity_Id :=
3476 Directly_Designated_Type (Base_Type (Etype (F)));
45fc7ddb 3477
c8ef728f 3478 New_Itype : Entity_Id;
45fc7ddb 3479
c8ef728f
ES
3480 begin
3481 if Is_Class_Wide_Type (DDT)
3482 and then Is_Interface (DDT)
3483 then
3484 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
45fc7ddb 3485 Set_Etype (New_Itype, Etype (A));
c8ef728f
ES
3486 Set_Directly_Designated_Type (New_Itype,
3487 Directly_Designated_Type (Etype (A)));
3488 Set_Etype (A, New_Itype);
3489 end if;
0669bebe
GB
3490
3491 -- Ada 2005, AI-162:If the actual is an allocator, the
3492 -- innermost enclosing statement is the master of the
b7d1f17f
HK
3493 -- created object. This needs to be done with expansion
3494 -- enabled only, otherwise the transient scope will not
3495 -- be removed in the expansion of the wrapped construct.
0669bebe 3496
45fc7ddb 3497 if (Is_Controlled (DDT) or else Has_Task (DDT))
b7d1f17f 3498 and then Expander_Active
0669bebe
GB
3499 then
3500 Establish_Transient_Scope (A, False);
3501 end if;
c8ef728f
ES
3502 end;
3503 end if;
3504
b7d1f17f
HK
3505 -- (Ada 2005): The call may be to a primitive operation of
3506 -- a tagged synchronized type, declared outside of the type.
3507 -- In this case the controlling actual must be converted to
3508 -- its corresponding record type, which is the formal type.
45fc7ddb
HK
3509 -- The actual may be a subtype, either because of a constraint
3510 -- or because it is a generic actual, so use base type to
3511 -- locate concurrent type.
b7d1f17f 3512
15e4986c
JM
3513 A_Typ := Base_Type (Etype (A));
3514 F_Typ := Base_Type (Etype (F));
3515
3516 declare
3517 Full_A_Typ : Entity_Id;
3518
3519 begin
3520 if Present (Full_View (A_Typ)) then
3521 Full_A_Typ := Base_Type (Full_View (A_Typ));
3522 else
3523 Full_A_Typ := A_Typ;
3524 end if;
b7d1f17f 3525
15e4986c
JM
3526 -- Tagged synchronized type (case 1): the actual is a
3527 -- concurrent type
3528
3529 if Is_Concurrent_Type (A_Typ)
3530 and then Corresponding_Record_Type (A_Typ) = F_Typ
3531 then
3532 Rewrite (A,
3533 Unchecked_Convert_To
3534 (Corresponding_Record_Type (A_Typ), A));
3535 Resolve (A, Etype (F));
3536
3537 -- Tagged synchronized type (case 2): the formal is a
3538 -- concurrent type
3539
3540 elsif Ekind (Full_A_Typ) = E_Record_Type
3541 and then Present
3542 (Corresponding_Concurrent_Type (Full_A_Typ))
3543 and then Is_Concurrent_Type (F_Typ)
3544 and then Present (Corresponding_Record_Type (F_Typ))
3545 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3546 then
3547 Resolve (A, Corresponding_Record_Type (F_Typ));
3548
3549 -- Common case
3550
3551 else
3552 Resolve (A, Etype (F));
3553 end if;
3554 end;
996ae0b0
RK
3555 end if;
3556
3557 A_Typ := Etype (A);
3558 F_Typ := Etype (F);
3559
b0186f71
AC
3560 -- In SPARK or ALFA, the only view conversions are those involving
3561 -- ancestor conversion of an extended type.
3562
fe5d3068 3563 if Nkind (A) = N_Type_Conversion
b0186f71
AC
3564 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3565 then
3566 declare
3567 Operand : constant Node_Id := Expression (A);
3568 Operand_Typ : constant Entity_Id := Etype (Operand);
3569 Target_Typ : constant Entity_Id := A_Typ;
780d052e 3570
b0186f71
AC
3571 begin
3572 if not (Is_Tagged_Type (Target_Typ)
780d052e
RD
3573 and then not Is_Class_Wide_Type (Target_Typ)
3574 and then Is_Tagged_Type (Operand_Typ)
3575 and then not Is_Class_Wide_Type (Operand_Typ)
3576 and then Is_Ancestor (Target_Typ, Operand_Typ))
b0186f71 3577 then
fe5d3068
YM
3578 Check_Formal_Restriction
3579 ("ancestor conversion is the only permitted view "
3580 & "conversion", A);
b0186f71
AC
3581 end if;
3582 end;
3583 end if;
3584
1e194575
AC
3585 -- Save actual for subsequent check on order dependence, and
3586 -- indicate whether actual is modifiable. For AI05-0144-2.
bb481772 3587
1e194575 3588 Save_Actual (A, Ekind (F) /= E_In_Parameter);
bb481772 3589
26570b21
RD
3590 -- For mode IN, if actual is an entity, and the type of the formal
3591 -- has warnings suppressed, then we reset Never_Set_In_Source for
3592 -- the calling entity. The reason for this is to catch cases like
3593 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3594 -- uses trickery to modify an IN parameter.
3595
3596 if Ekind (F) = E_In_Parameter
3597 and then Is_Entity_Name (A)
3598 and then Present (Entity (A))
3599 and then Ekind (Entity (A)) = E_Variable
3600 and then Has_Warnings_Off (F_Typ)
3601 then
3602 Set_Never_Set_In_Source (Entity (A), False);
3603 end if;
3604
fbf5a39b
AC
3605 -- Perform error checks for IN and IN OUT parameters
3606
3607 if Ekind (F) /= E_Out_Parameter then
3608
3609 -- Check unset reference. For scalar parameters, it is clearly
3610 -- wrong to pass an uninitialized value as either an IN or
3611 -- IN-OUT parameter. For composites, it is also clearly an
3612 -- error to pass a completely uninitialized value as an IN
3613 -- parameter, but the case of IN OUT is trickier. We prefer
3614 -- not to give a warning here. For example, suppose there is
3615 -- a routine that sets some component of a record to False.
3616 -- It is perfectly reasonable to make this IN-OUT and allow
3617 -- either initialized or uninitialized records to be passed
3618 -- in this case.
3619
3620 -- For partially initialized composite values, we also avoid
3621 -- warnings, since it is quite likely that we are passing a
3622 -- partially initialized value and only the initialized fields
3623 -- will in fact be read in the subprogram.
3624
3625 if Is_Scalar_Type (A_Typ)
3626 or else (Ekind (F) = E_In_Parameter
3627 and then not Is_Partially_Initialized_Type (A_Typ))
996ae0b0 3628 then
fbf5a39b 3629 Check_Unset_Reference (A);
996ae0b0 3630 end if;
996ae0b0 3631
758c442c
GD
3632 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3633 -- actual to a nested call, since this is case of reading an
3634 -- out parameter, which is not allowed.
996ae0b0 3635
0ab80019 3636 if Ada_Version = Ada_83
996ae0b0
RK
3637 and then Is_Entity_Name (A)
3638 and then Ekind (Entity (A)) = E_Out_Parameter
3639 then
3640 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3641 end if;
3642 end if;
3643
67ce0d7e
RD
3644 -- Case of OUT or IN OUT parameter
3645
36fcf362 3646 if Ekind (F) /= E_In_Parameter then
67ce0d7e
RD
3647
3648 -- For an Out parameter, check for useless assignment. Note
45fc7ddb
HK
3649 -- that we can't set Last_Assignment this early, because we may
3650 -- kill current values in Resolve_Call, and that call would
3651 -- clobber the Last_Assignment field.
67ce0d7e 3652
45fc7ddb
HK
3653 -- Note: call Warn_On_Useless_Assignment before doing the check
3654 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3655 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3656 -- reflects the last assignment, not this one!
36fcf362 3657
67ce0d7e 3658 if Ekind (F) = E_Out_Parameter then
36fcf362 3659 if Warn_On_Modified_As_Out_Parameter (F)
67ce0d7e
RD
3660 and then Is_Entity_Name (A)
3661 and then Present (Entity (A))
36fcf362 3662 and then Comes_From_Source (N)
67ce0d7e 3663 then
36fcf362 3664 Warn_On_Useless_Assignment (Entity (A), A);
67ce0d7e
RD
3665 end if;
3666 end if;
3667
36fcf362
RD
3668 -- Validate the form of the actual. Note that the call to
3669 -- Is_OK_Variable_For_Out_Formal generates the required
3670 -- reference in this case.
3671
3672 if not Is_OK_Variable_For_Out_Formal (A) then
3673 Error_Msg_NE ("actual for& must be a variable", A, F);
3674 end if;
3675
67ce0d7e 3676 -- What's the following about???
fbf5a39b
AC
3677
3678 if Is_Entity_Name (A) then
3679 Kill_Checks (Entity (A));
3680 else
3681 Kill_All_Checks;
3682 end if;
3683 end if;
3684
3685 if Etype (A) = Any_Type then
3686 Set_Etype (N, Any_Type);
3687 return;
3688 end if;
3689
996ae0b0
RK
3690 -- Apply appropriate range checks for in, out, and in-out
3691 -- parameters. Out and in-out parameters also need a separate
3692 -- check, if there is a type conversion, to make sure the return
3693 -- value meets the constraints of the variable before the
3694 -- conversion.
3695
3696 -- Gigi looks at the check flag and uses the appropriate types.
3697 -- For now since one flag is used there is an optimization which
3698 -- might not be done in the In Out case since Gigi does not do
3699 -- any analysis. More thought required about this ???
3700
8a95f4e8 3701 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
48f91b44
RD
3702
3703 -- Apply predicate checks, unless this is a call to the
3704 -- predicate check function itself, which would cause an
3705 -- infinite recursion.
3706
3707 if not (Ekind (Nam) = E_Function
3708 and then Has_Predicates (Nam))
3709 then
3710 Apply_Predicate_Check (A, F_Typ);
3711 end if;
3712
3713 -- Apply required constraint checks
3714
996ae0b0
RK
3715 if Is_Scalar_Type (Etype (A)) then
3716 Apply_Scalar_Range_Check (A, F_Typ);
3717
3718 elsif Is_Array_Type (Etype (A)) then
3719 Apply_Length_Check (A, F_Typ);
3720
3721 elsif Is_Record_Type (F_Typ)
3722 and then Has_Discriminants (F_Typ)
3723 and then Is_Constrained (F_Typ)
3724 and then (not Is_Derived_Type (F_Typ)
3725 or else Comes_From_Source (Nam))
3726 then
3727 Apply_Discriminant_Check (A, F_Typ);
3728
3729 elsif Is_Access_Type (F_Typ)
3730 and then Is_Array_Type (Designated_Type (F_Typ))
3731 and then Is_Constrained (Designated_Type (F_Typ))
3732 then
3733 Apply_Length_Check (A, F_Typ);
3734
3735 elsif Is_Access_Type (F_Typ)
3736 and then Has_Discriminants (Designated_Type (F_Typ))
3737 and then Is_Constrained (Designated_Type (F_Typ))
3738 then
3739 Apply_Discriminant_Check (A, F_Typ);
3740
3741 else
3742 Apply_Range_Check (A, F_Typ);
3743 end if;
2820d220 3744
0f1a6a0b
AC
3745 -- Ada 2005 (AI-231): Note that the controlling parameter case
3746 -- already existed in Ada 95, which is partially checked
3747 -- elsewhere (see Checks), and we don't want the warning
3748 -- message to differ.
2820d220 3749
0f1a6a0b 3750 if Is_Access_Type (F_Typ)
1420b484 3751 and then Can_Never_Be_Null (F_Typ)
aa5147f0 3752 and then Known_Null (A)
2820d220 3753 then
0f1a6a0b
AC
3754 if Is_Controlling_Formal (F) then
3755 Apply_Compile_Time_Constraint_Error
3756 (N => A,
3757 Msg => "null value not allowed here?",
3758 Reason => CE_Access_Check_Failed);
3759
3760 elsif Ada_Version >= Ada_2005 then
3761 Apply_Compile_Time_Constraint_Error
3762 (N => A,
3763 Msg => "(Ada 2005) null not allowed in "
3764 & "null-excluding formal?",
3765 Reason => CE_Null_Not_Allowed);
3766 end if;
2820d220 3767 end if;
996ae0b0
RK
3768 end if;
3769
8a95f4e8 3770 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
996ae0b0
RK
3771 if Nkind (A) = N_Type_Conversion then
3772 if Is_Scalar_Type (A_Typ) then
3773 Apply_Scalar_Range_Check
3774 (Expression (A), Etype (Expression (A)), A_Typ);
3775 else
3776 Apply_Range_Check
3777 (Expression (A), Etype (Expression (A)), A_Typ);
3778 end if;
3779
3780 else
3781 if Is_Scalar_Type (F_Typ) then
3782 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3783
3784 elsif Is_Array_Type (F_Typ)
3785 and then Ekind (F) = E_Out_Parameter
3786 then
3787 Apply_Length_Check (A, F_Typ);
3788
3789 else
3790 Apply_Range_Check (A, A_Typ, F_Typ);
3791 end if;
3792 end if;
3793 end if;
3794
3795 -- An actual associated with an access parameter is implicitly
45fc7ddb
HK
3796 -- converted to the anonymous access type of the formal and must
3797 -- satisfy the legality checks for access conversions.
996ae0b0
RK
3798
3799 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3800 if not Valid_Conversion (A, F_Typ, A) then
3801 Error_Msg_N
3802 ("invalid implicit conversion for access parameter", A);
3803 end if;
3804 end if;
3805
3806 -- Check bad case of atomic/volatile argument (RM C.6(12))
3807
3808 if Is_By_Reference_Type (Etype (F))
3809 and then Comes_From_Source (N)
3810 then
3811 if Is_Atomic_Object (A)
3812 and then not Is_Atomic (Etype (F))
3813 then
3814 Error_Msg_N
3815 ("cannot pass atomic argument to non-atomic formal",
3816 N);
3817
3818 elsif Is_Volatile_Object (A)
3819 and then not Is_Volatile (Etype (F))
3820 then
3821 Error_Msg_N
3822 ("cannot pass volatile argument to non-volatile formal",
3823 N);
3824 end if;
3825 end if;
3826
3827 -- Check that subprograms don't have improper controlling
d81b4bfe 3828 -- arguments (RM 3.9.2 (9)).
996ae0b0 3829
0669bebe
GB
3830 -- A primitive operation may have an access parameter of an
3831 -- incomplete tagged type, but a dispatching call is illegal
3832 -- if the type is still incomplete.
3833
996ae0b0
RK
3834 if Is_Controlling_Formal (F) then
3835 Set_Is_Controlling_Actual (A);
0669bebe
GB
3836
3837 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3838 declare
3839 Desig : constant Entity_Id := Designated_Type (Etype (F));
3840 begin
3841 if Ekind (Desig) = E_Incomplete_Type
3842 and then No (Full_View (Desig))
3843 and then No (Non_Limited_View (Desig))
3844 then
3845 Error_Msg_NE
3846 ("premature use of incomplete type& " &
3847 "in dispatching call", A, Desig);
3848 end if;
3849 end;
3850 end if;
3851
996ae0b0
RK
3852 elsif Nkind (A) = N_Explicit_Dereference then
3853 Validate_Remote_Access_To_Class_Wide_Type (A);
3854 end if;
3855
3856 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3857 and then not Is_Class_Wide_Type (F_Typ)
3858 and then not Is_Controlling_Formal (F)
3859 then
3860 Error_Msg_N ("class-wide argument not allowed here!", A);
07fc65c4
GB
3861
3862 if Is_Subprogram (Nam)
3863 and then Comes_From_Source (Nam)
3864 then
996ae0b0
RK
3865 Error_Msg_Node_2 := F_Typ;
3866 Error_Msg_NE
82c80734 3867 ("& is not a dispatching operation of &!", A, Nam);
996ae0b0
RK
3868 end if;
3869
3870 elsif Is_Access_Type (A_Typ)
3871 and then Is_Access_Type (F_Typ)
3872 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
aa5147f0 3873 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
996ae0b0 3874 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
07fc65c4
GB
3875 or else (Nkind (A) = N_Attribute_Reference
3876 and then
46fe0142 3877 Is_Class_Wide_Type (Etype (Prefix (A)))))
996ae0b0
RK
3878 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3879 and then not Is_Controlling_Formal (F)
ae65d635 3880
46fe0142 3881 -- Disable these checks for call to imported C++ subprograms
ae65d635 3882
46fe0142
AC
3883 and then not
3884 (Is_Entity_Name (Name (N))
3885 and then Is_Imported (Entity (Name (N)))
3886 and then Convention (Entity (Name (N))) = Convention_CPP)
996ae0b0
RK
3887 then
3888 Error_Msg_N
3889 ("access to class-wide argument not allowed here!", A);
07fc65c4
GB
3890
3891 if Is_Subprogram (Nam)
3892 and then Comes_From_Source (Nam)
3893 then
996ae0b0
RK
3894 Error_Msg_Node_2 := Designated_Type (F_Typ);
3895 Error_Msg_NE
82c80734 3896 ("& is not a dispatching operation of &!", A, Nam);
996ae0b0
RK
3897 end if;
3898 end if;
3899
3900 Eval_Actual (A);
3901
8e4dac80
TQ
3902 -- If it is a named association, treat the selector_name as a
3903 -- proper identifier, and mark the corresponding entity.
996ae0b0
RK
3904
3905 if Nkind (Parent (A)) = N_Parameter_Association then
3906 Set_Entity (Selector_Name (Parent (A)), F);
3907 Generate_Reference (F, Selector_Name (Parent (A)));
3908 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3909 Generate_Reference (F_Typ, N, ' ');
3910 end if;
3911
3912 Prev := A;
fbf5a39b
AC
3913
3914 if Ekind (F) /= E_Out_Parameter then
3915 Check_Unset_Reference (A);
3916 end if;
3917
996ae0b0
RK
3918 Next_Actual (A);
3919
fbf5a39b
AC
3920 -- Case where actual is not present
3921
996ae0b0
RK
3922 else
3923 Insert_Default;
3924 end if;
3925
3926 Next_Formal (F);
3927 end loop;
996ae0b0
RK
3928 end Resolve_Actuals;
3929
3930 -----------------------
3931 -- Resolve_Allocator --
3932 -----------------------
3933
3934 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3935 E : constant Node_Id := Expression (N);
3936 Subtyp : Entity_Id;
3937 Discrim : Entity_Id;
3938 Constr : Node_Id;
b7d1f17f
HK
3939 Aggr : Node_Id;
3940 Assoc : Node_Id := Empty;
996ae0b0
RK
3941 Disc_Exp : Node_Id;
3942
b7d1f17f
HK
3943 procedure Check_Allocator_Discrim_Accessibility
3944 (Disc_Exp : Node_Id;
3945 Alloc_Typ : Entity_Id);
3946 -- Check that accessibility level associated with an access discriminant
3947 -- initialized in an allocator by the expression Disc_Exp is not deeper
3948 -- than the level of the allocator type Alloc_Typ. An error message is
3949 -- issued if this condition is violated. Specialized checks are done for
3950 -- the cases of a constraint expression which is an access attribute or
3951 -- an access discriminant.
3952
07fc65c4 3953 function In_Dispatching_Context return Boolean;
b7d1f17f
HK
3954 -- If the allocator is an actual in a call, it is allowed to be class-
3955 -- wide when the context is not because it is a controlling actual.
3956
3957 procedure Propagate_Coextensions (Root : Node_Id);
3958 -- Propagate all nested coextensions which are located one nesting
3959 -- level down the tree to the node Root. Example:
3960 --
3961 -- Top_Record
3962 -- Level_1_Coextension
3963 -- Level_2_Coextension
3964 --
3965 -- The algorithm is paired with delay actions done by the Expander. In
3966 -- the above example, assume all coextensions are controlled types.
3967 -- The cycle of analysis, resolution and expansion will yield:
3968 --
3969 -- 1) Analyze Top_Record
3970 -- 2) Analyze Level_1_Coextension
3971 -- 3) Analyze Level_2_Coextension
f3d57416 3972 -- 4) Resolve Level_2_Coextension. The allocator is marked as a
b7d1f17f
HK
3973 -- coextension.
3974 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3975 -- generated to capture the allocated object. Temp_1 is attached
3976 -- to the coextension chain of Level_2_Coextension.
3977 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3978 -- coextension. A forward tree traversal is performed which finds
3979 -- Level_2_Coextension's list and copies its contents into its
3980 -- own list.
3981 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3982 -- generated to capture the allocated object. Temp_2 is attached
3983 -- to the coextension chain of Level_1_Coextension. Currently, the
3984 -- contents of the list are [Temp_2, Temp_1].
3985 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3986 -- finds Level_1_Coextension's list and copies its contents into
3987 -- its own list.
3988 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3989 -- Temp_2 and attach them to Top_Record's finalization list.
3990
3991 -------------------------------------------
3992 -- Check_Allocator_Discrim_Accessibility --
3993 -------------------------------------------
3994
3995 procedure Check_Allocator_Discrim_Accessibility
3996 (Disc_Exp : Node_Id;
3997 Alloc_Typ : Entity_Id)
3998 is
3999 begin
4000 if Type_Access_Level (Etype (Disc_Exp)) >
4001 Type_Access_Level (Alloc_Typ)
4002 then
4003 Error_Msg_N
4004 ("operand type has deeper level than allocator type", Disc_Exp);
4005
4006 -- When the expression is an Access attribute the level of the prefix
4007 -- object must not be deeper than that of the allocator's type.
4008
4009 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4010 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
4011 = Attribute_Access
4012 and then Object_Access_Level (Prefix (Disc_Exp))
4013 > Type_Access_Level (Alloc_Typ)
4014 then
4015 Error_Msg_N
4016 ("prefix of attribute has deeper level than allocator type",
4017 Disc_Exp);
4018
4019 -- When the expression is an access discriminant the check is against
4020 -- the level of the prefix object.
4021
4022 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4023 and then Nkind (Disc_Exp) = N_Selected_Component
4024 and then Object_Access_Level (Prefix (Disc_Exp))
4025 > Type_Access_Level (Alloc_Typ)
4026 then
4027 Error_Msg_N
4028 ("access discriminant has deeper level than allocator type",
4029 Disc_Exp);
4030
4031 -- All other cases are legal
4032
4033 else
4034 null;
4035 end if;
4036 end Check_Allocator_Discrim_Accessibility;
07fc65c4
GB
4037
4038 ----------------------------
4039 -- In_Dispatching_Context --
4040 ----------------------------
4041
4042 function In_Dispatching_Context return Boolean is
4043 Par : constant Node_Id := Parent (N);
07fc65c4 4044 begin
45fc7ddb 4045 return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
07fc65c4
GB
4046 and then Is_Entity_Name (Name (Par))
4047 and then Is_Dispatching_Operation (Entity (Name (Par)));
4048 end In_Dispatching_Context;
4049
b7d1f17f
HK
4050 ----------------------------
4051 -- Propagate_Coextensions --
4052 ----------------------------
4053
4054 procedure Propagate_Coextensions (Root : Node_Id) is
4055
4056 procedure Copy_List (From : Elist_Id; To : Elist_Id);
4057 -- Copy the contents of list From into list To, preserving the
4058 -- order of elements.
4059
4060 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
4061 -- Recognize an allocator or a rewritten allocator node and add it
f3d57416 4062 -- along with its nested coextensions to the list of Root.
b7d1f17f
HK
4063
4064 ---------------
4065 -- Copy_List --
4066 ---------------
4067
4068 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
4069 From_Elmt : Elmt_Id;
4070 begin
4071 From_Elmt := First_Elmt (From);
4072 while Present (From_Elmt) loop
4073 Append_Elmt (Node (From_Elmt), To);
4074 Next_Elmt (From_Elmt);
4075 end loop;
4076 end Copy_List;
4077
4078 -----------------------
4079 -- Process_Allocator --
4080 -----------------------
4081
4082 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
4083 Orig_Nod : Node_Id := Nod;
4084
4085 begin
4086 -- This is a possible rewritten subtype indication allocator. Any
4087 -- nested coextensions will appear as discriminant constraints.
4088
4089 if Nkind (Nod) = N_Identifier
4090 and then Present (Original_Node (Nod))
4091 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
4092 then
4093 declare
4094 Discr : Node_Id;
4095 Discr_Elmt : Elmt_Id;
4096
4097 begin
4098 if Is_Record_Type (Entity (Nod)) then
4099 Discr_Elmt :=
4100 First_Elmt (Discriminant_Constraint (Entity (Nod)));
4101 while Present (Discr_Elmt) loop
4102 Discr := Node (Discr_Elmt);
4103
4104 if Nkind (Discr) = N_Identifier
4105 and then Present (Original_Node (Discr))
4106 and then Nkind (Original_Node (Discr)) = N_Allocator
4107 and then Present (Coextensions (
4108 Original_Node (Discr)))
4109 then
4110 if No (Coextensions (Root)) then
4111 Set_Coextensions (Root, New_Elmt_List);
4112 end if;
4113
4114 Copy_List
4115 (From => Coextensions (Original_Node (Discr)),
4116 To => Coextensions (Root));
4117 end if;
4118
4119 Next_Elmt (Discr_Elmt);
4120 end loop;
4121
4122 -- There is no need to continue the traversal of this
4123 -- subtree since all the information has already been
4124 -- propagated.
4125
4126 return Skip;
4127 end if;
4128 end;
4129
4130 -- Case of either a stand alone allocator or a rewritten allocator
4131 -- with an aggregate.
4132
4133 else
4134 if Present (Original_Node (Nod)) then
4135 Orig_Nod := Original_Node (Nod);
4136 end if;
4137
4138 if Nkind (Orig_Nod) = N_Allocator then
4139
4140 -- Propagate the list of nested coextensions to the Root
4141 -- allocator. This is done through list copy since a single
4142 -- allocator may have multiple coextensions. Do not touch
4143 -- coextensions roots.
4144
4145 if not Is_Coextension_Root (Orig_Nod)
4146 and then Present (Coextensions (Orig_Nod))
4147 then
4148 if No (Coextensions (Root)) then
4149 Set_Coextensions (Root, New_Elmt_List);
4150 end if;
4151
4152 Copy_List
4153 (From => Coextensions (Orig_Nod),
4154 To => Coextensions (Root));
4155 end if;
4156
4157 -- There is no need to continue the traversal of this
4158 -- subtree since all the information has already been
4159 -- propagated.
4160
4161 return Skip;
4162 end if;
4163 end if;
4164
4165 -- Keep on traversing, looking for the next allocator
4166
4167 return OK;
4168 end Process_Allocator;
4169
4170 procedure Process_Allocators is
4171 new Traverse_Proc (Process_Allocator);
4172
4173 -- Start of processing for Propagate_Coextensions
4174
4175 begin
4176 Process_Allocators (Expression (Root));
4177 end Propagate_Coextensions;
4178
07fc65c4
GB
4179 -- Start of processing for Resolve_Allocator
4180
996ae0b0
RK
4181 begin
4182 -- Replace general access with specific type
4183
4184 if Ekind (Etype (N)) = E_Allocator_Type then
4185 Set_Etype (N, Base_Type (Typ));
4186 end if;
4187
0669bebe 4188 if Is_Abstract_Type (Typ) then
996ae0b0
RK
4189 Error_Msg_N ("type of allocator cannot be abstract", N);
4190 end if;
4191
4192 -- For qualified expression, resolve the expression using the
4193 -- given subtype (nothing to do for type mark, subtype indication)
4194
4195 if Nkind (E) = N_Qualified_Expression then
4196 if Is_Class_Wide_Type (Etype (E))
4197 and then not Is_Class_Wide_Type (Designated_Type (Typ))
07fc65c4 4198 and then not In_Dispatching_Context
996ae0b0
RK
4199 then
4200 Error_Msg_N
4201 ("class-wide allocator not allowed for this access type", N);
4202 end if;
4203
4204 Resolve (Expression (E), Etype (E));
4205 Check_Unset_Reference (Expression (E));
4206
fbf5a39b 4207 -- A qualified expression requires an exact match of the type,
7b4db06c 4208 -- class-wide matching is not allowed.
fbf5a39b 4209
7b4db06c 4210 if (Is_Class_Wide_Type (Etype (Expression (E)))
b46be8a2 4211 or else Is_Class_Wide_Type (Etype (E)))
fbf5a39b
AC
4212 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4213 then
4214 Wrong_Type (Expression (E), Etype (E));
4215 end if;
4216
b7d1f17f
HK
4217 -- A special accessibility check is needed for allocators that
4218 -- constrain access discriminants. The level of the type of the
4219 -- expression used to constrain an access discriminant cannot be
f3d57416 4220 -- deeper than the type of the allocator (in contrast to access
b7d1f17f
HK
4221 -- parameters, where the level of the actual can be arbitrary).
4222
4223 -- We can't use Valid_Conversion to perform this check because
4224 -- in general the type of the allocator is unrelated to the type
4225 -- of the access discriminant.
4226
4227 if Ekind (Typ) /= E_Anonymous_Access_Type
4228 or else Is_Local_Anonymous_Access (Typ)
4229 then
4230 Subtyp := Entity (Subtype_Mark (E));
4231
4232 Aggr := Original_Node (Expression (E));
4233
4234 if Has_Discriminants (Subtyp)
45fc7ddb 4235 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
b7d1f17f
HK
4236 then
4237 Discrim := First_Discriminant (Base_Type (Subtyp));
4238
4239 -- Get the first component expression of the aggregate
4240
4241 if Present (Expressions (Aggr)) then
4242 Disc_Exp := First (Expressions (Aggr));
4243
4244 elsif Present (Component_Associations (Aggr)) then
4245 Assoc := First (Component_Associations (Aggr));
4246
4247 if Present (Assoc) then
4248 Disc_Exp := Expression (Assoc);
4249 else
4250 Disc_Exp := Empty;
4251 end if;
4252
4253 else
4254 Disc_Exp := Empty;
4255 end if;
4256
4257 while Present (Discrim) and then Present (Disc_Exp) loop
4258 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4259 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4260 end if;
4261
4262 Next_Discriminant (Discrim);
4263
4264 if Present (Discrim) then
4265 if Present (Assoc) then
4266 Next (Assoc);
4267 Disc_Exp := Expression (Assoc);
4268
4269 elsif Present (Next (Disc_Exp)) then
4270 Next (Disc_Exp);
4271
4272 else
4273 Assoc := First (Component_Associations (Aggr));
4274
4275 if Present (Assoc) then
4276 Disc_Exp := Expression (Assoc);
4277 else
4278 Disc_Exp := Empty;
4279 end if;
4280 end if;
4281 end if;
4282 end loop;
4283 end if;
4284 end if;
4285
996ae0b0
RK
4286 -- For a subtype mark or subtype indication, freeze the subtype
4287
4288 else
4289 Freeze_Expression (E);
4290
4291 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4292 Error_Msg_N
4293 ("initialization required for access-to-constant allocator", N);
4294 end if;
4295
4296 -- A special accessibility check is needed for allocators that
4297 -- constrain access discriminants. The level of the type of the
b7d1f17f 4298 -- expression used to constrain an access discriminant cannot be
f3d57416 4299 -- deeper than the type of the allocator (in contrast to access
996ae0b0
RK
4300 -- parameters, where the level of the actual can be arbitrary).
4301 -- We can't use Valid_Conversion to perform this check because
4302 -- in general the type of the allocator is unrelated to the type
b7d1f17f 4303 -- of the access discriminant.
996ae0b0
RK
4304
4305 if Nkind (Original_Node (E)) = N_Subtype_Indication
b7d1f17f
HK
4306 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4307 or else Is_Local_Anonymous_Access (Typ))
996ae0b0
RK
4308 then
4309 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4310
4311 if Has_Discriminants (Subtyp) then
4312 Discrim := First_Discriminant (Base_Type (Subtyp));
4313 Constr := First (Constraints (Constraint (Original_Node (E))));
996ae0b0
RK
4314 while Present (Discrim) and then Present (Constr) loop
4315 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4316 if Nkind (Constr) = N_Discriminant_Association then
4317 Disc_Exp := Original_Node (Expression (Constr));
4318 else
4319 Disc_Exp := Original_Node (Constr);
4320 end if;
4321
b7d1f17f 4322 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
996ae0b0 4323 end if;
b7d1f17f 4324
996ae0b0
RK
4325 Next_Discriminant (Discrim);
4326 Next (Constr);
4327 end loop;
4328 end if;
4329 end if;
4330 end if;
4331
758c442c
GD
4332 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4333 -- check that the level of the type of the created object is not deeper
4334 -- than the level of the allocator's access type, since extensions can
4335 -- now occur at deeper levels than their ancestor types. This is a
4336 -- static accessibility level check; a run-time check is also needed in
4337 -- the case of an initialized allocator with a class-wide argument (see
4338 -- Expand_Allocator_Expression).
4339
0791fbe9 4340 if Ada_Version >= Ada_2005
758c442c
GD
4341 and then Is_Class_Wide_Type (Designated_Type (Typ))
4342 then
4343 declare
b7d1f17f 4344 Exp_Typ : Entity_Id;
758c442c
GD
4345
4346 begin
4347 if Nkind (E) = N_Qualified_Expression then
4348 Exp_Typ := Etype (E);
4349 elsif Nkind (E) = N_Subtype_Indication then
4350 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4351 else
4352 Exp_Typ := Entity (E);
4353 end if;
4354
4355 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4356 if In_Instance_Body then
4357 Error_Msg_N ("?type in allocator has deeper level than" &
4358 " designated class-wide type", E);
c8ef728f
ES
4359 Error_Msg_N ("\?Program_Error will be raised at run time",
4360 E);
758c442c
GD
4361 Rewrite (N,
4362 Make_Raise_Program_Error (Sloc (N),
4363 Reason => PE_Accessibility_Check_Failed));
4364 Set_Etype (N, Typ);
aa180613
RD
4365
4366 -- Do not apply Ada 2005 accessibility checks on a class-wide
4367 -- allocator if the type given in the allocator is a formal
4368 -- type. A run-time check will be performed in the instance.
4369
4370 elsif not Is_Generic_Type (Exp_Typ) then
758c442c
GD
4371 Error_Msg_N ("type in allocator has deeper level than" &
4372 " designated class-wide type", E);
4373 end if;
4374 end if;
4375 end;
4376 end if;
4377
996ae0b0
RK
4378 -- Check for allocation from an empty storage pool
4379
4380 if No_Pool_Assigned (Typ) then
8da337c5 4381 Error_Msg_N ("allocation from empty storage pool!", N);
1420b484
JM
4382
4383 -- If the context is an unchecked conversion, as may happen within
4384 -- an inlined subprogram, the allocator is being resolved with its
4385 -- own anonymous type. In that case, if the target type has a specific
4386 -- storage pool, it must be inherited explicitly by the allocator type.
4387
4388 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4389 and then No (Associated_Storage_Pool (Typ))
4390 then
4391 Set_Associated_Storage_Pool
4392 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
996ae0b0 4393 end if;
b7d1f17f 4394
e57ab550
AC
4395 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4396 Check_Restriction (No_Anonymous_Allocators, N);
4397 end if;
4398
b7d1f17f
HK
4399 -- An erroneous allocator may be rewritten as a raise Program_Error
4400 -- statement.
4401
4402 if Nkind (N) = N_Allocator then
4403
4404 -- An anonymous access discriminant is the definition of a
aa5147f0 4405 -- coextension.
b7d1f17f
HK
4406
4407 if Ekind (Typ) = E_Anonymous_Access_Type
4408 and then Nkind (Associated_Node_For_Itype (Typ)) =
4409 N_Discriminant_Specification
4410 then
4411 -- Avoid marking an allocator as a dynamic coextension if it is
aa5147f0 4412 -- within a static construct.
b7d1f17f
HK
4413
4414 if not Is_Static_Coextension (N) then
aa5147f0 4415 Set_Is_Dynamic_Coextension (N);
b7d1f17f
HK
4416 end if;
4417
4418 -- Cleanup for potential static coextensions
4419
4420 else
aa5147f0
ES
4421 Set_Is_Dynamic_Coextension (N, False);
4422 Set_Is_Static_Coextension (N, False);
b7d1f17f
HK
4423 end if;
4424
aa5147f0
ES
4425 -- There is no need to propagate any nested coextensions if they
4426 -- are marked as static since they will be rewritten on the spot.
4427
4428 if not Is_Static_Coextension (N) then
4429 Propagate_Coextensions (N);
4430 end if;
b7d1f17f 4431 end if;
996ae0b0
RK
4432 end Resolve_Allocator;
4433
4434 ---------------------------
4435 -- Resolve_Arithmetic_Op --
4436 ---------------------------
4437
4438 -- Used for resolving all arithmetic operators except exponentiation
4439
4440 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
fbf5a39b
AC
4441 L : constant Node_Id := Left_Opnd (N);
4442 R : constant Node_Id := Right_Opnd (N);
4443 TL : constant Entity_Id := Base_Type (Etype (L));
4444 TR : constant Entity_Id := Base_Type (Etype (R));
4445 T : Entity_Id;
4446 Rop : Node_Id;
996ae0b0
RK
4447
4448 B_Typ : constant Entity_Id := Base_Type (Typ);
4449 -- We do the resolution using the base type, because intermediate values
4450 -- in expressions always are of the base type, not a subtype of it.
4451
aa180613
RD
4452 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4453 -- Returns True if N is in a context that expects "any real type"
4454
996ae0b0
RK
4455 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4456 -- Return True iff given type is Integer or universal real/integer
4457
4458 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4459 -- Choose type of integer literal in fixed-point operation to conform
4460 -- to available fixed-point type. T is the type of the other operand,
4461 -- which is needed to determine the expected type of N.
4462
4463 procedure Set_Operand_Type (N : Node_Id);
4464 -- Set operand type to T if universal
4465
aa180613
RD
4466 -------------------------------
4467 -- Expected_Type_Is_Any_Real --
4468 -------------------------------
4469
4470 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4471 begin
4472 -- N is the expression after "delta" in a fixed_point_definition;
4473 -- see RM-3.5.9(6):
4474
45fc7ddb
HK
4475 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4476 N_Decimal_Fixed_Point_Definition,
aa180613
RD
4477
4478 -- N is one of the bounds in a real_range_specification;
4479 -- see RM-3.5.7(5):
4480
45fc7ddb 4481 N_Real_Range_Specification,
aa180613
RD
4482
4483 -- N is the expression of a delta_constraint;
4484 -- see RM-J.3(3):
4485
45fc7ddb 4486 N_Delta_Constraint);
aa180613
RD
4487 end Expected_Type_Is_Any_Real;
4488
996ae0b0
RK
4489 -----------------------------
4490 -- Is_Integer_Or_Universal --
4491 -----------------------------
4492
4493 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4494 T : Entity_Id;
4495 Index : Interp_Index;
4496 It : Interp;
4497
4498 begin
4499 if not Is_Overloaded (N) then
4500 T := Etype (N);
4501 return Base_Type (T) = Base_Type (Standard_Integer)
4502 or else T = Universal_Integer
4503 or else T = Universal_Real;
4504 else
4505 Get_First_Interp (N, Index, It);
996ae0b0 4506 while Present (It.Typ) loop
996ae0b0
RK
4507 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4508 or else It.Typ = Universal_Integer
4509 or else It.Typ = Universal_Real
4510 then
4511 return True;
4512 end if;
4513
4514 Get_Next_Interp (Index, It);
4515 end loop;
4516 end if;
4517
4518 return False;
4519 end Is_Integer_Or_Universal;
4520
4521 ----------------------------
4522 -- Set_Mixed_Mode_Operand --
4523 ----------------------------
4524
4525 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4526 Index : Interp_Index;
4527 It : Interp;
4528
4529 begin
4530 if Universal_Interpretation (N) = Universal_Integer then
4531
4532 -- A universal integer literal is resolved as standard integer
758c442c
GD
4533 -- except in the case of a fixed-point result, where we leave it
4534 -- as universal (to be handled by Exp_Fixd later on)
996ae0b0
RK
4535
4536 if Is_Fixed_Point_Type (T) then
4537 Resolve (N, Universal_Integer);
4538 else
4539 Resolve (N, Standard_Integer);
4540 end if;
4541
4542 elsif Universal_Interpretation (N) = Universal_Real
4543 and then (T = Base_Type (Standard_Integer)
4544 or else T = Universal_Integer
4545 or else T = Universal_Real)
4546 then
4547 -- A universal real can appear in a fixed-type context. We resolve
4548 -- the literal with that context, even though this might raise an
4549 -- exception prematurely (the other operand may be zero).
4550
4551 Resolve (N, B_Typ);
4552
4553 elsif Etype (N) = Base_Type (Standard_Integer)
4554 and then T = Universal_Real
4555 and then Is_Overloaded (N)
4556 then
4557 -- Integer arg in mixed-mode operation. Resolve with universal
4558 -- type, in case preference rule must be applied.
4559
4560 Resolve (N, Universal_Integer);
4561
4562 elsif Etype (N) = T
4563 and then B_Typ /= Universal_Fixed
4564 then
a77842bd 4565 -- Not a mixed-mode operation, resolve with context
996ae0b0
RK
4566
4567 Resolve (N, B_Typ);
4568
4569 elsif Etype (N) = Any_Fixed then
4570
a77842bd 4571 -- N may itself be a mixed-mode operation, so use context type
996ae0b0
RK
4572
4573 Resolve (N, B_Typ);
4574
4575 elsif Is_Fixed_Point_Type (T)
4576 and then B_Typ = Universal_Fixed
4577 and then Is_Overloaded (N)
4578 then
4579 -- Must be (fixed * fixed) operation, operand must have one
4580 -- compatible interpretation.
4581
4582 Resolve (N, Any_Fixed);
4583
4584 elsif Is_Fixed_Point_Type (B_Typ)
4585 and then (T = Universal_Real
4586 or else Is_Fixed_Point_Type (T))
4587 and then Is_Overloaded (N)
4588 then
4589 -- C * F(X) in a fixed context, where C is a real literal or a
4590 -- fixed-point expression. F must have either a fixed type
4591 -- interpretation or an integer interpretation, but not both.
4592
4593 Get_First_Interp (N, Index, It);
996ae0b0 4594 while Present (It.Typ) loop
996ae0b0
RK
4595 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4596
4597 if Analyzed (N) then
4598 Error_Msg_N ("ambiguous operand in fixed operation", N);
4599 else
4600 Resolve (N, Standard_Integer);
4601 end if;
4602
4603 elsif Is_Fixed_Point_Type (It.Typ) then
4604
4605 if Analyzed (N) then
4606 Error_Msg_N ("ambiguous operand in fixed operation", N);
4607 else
4608 Resolve (N, It.Typ);
4609 end if;
4610 end if;
4611
4612 Get_Next_Interp (Index, It);
4613 end loop;
4614
758c442c
GD
4615 -- Reanalyze the literal with the fixed type of the context. If
4616 -- context is Universal_Fixed, we are within a conversion, leave
4617 -- the literal as a universal real because there is no usable
4618 -- fixed type, and the target of the conversion plays no role in
4619 -- the resolution.
996ae0b0 4620
0ab80019
AC
4621 declare
4622 Op2 : Node_Id;
4623 T2 : Entity_Id;
4624
4625 begin
4626 if N = L then
4627 Op2 := R;
4628 else
4629 Op2 := L;
4630 end if;
4631
4632 if B_Typ = Universal_Fixed
4633 and then Nkind (Op2) = N_Real_Literal
4634 then
4635 T2 := Universal_Real;
4636 else
4637 T2 := B_Typ;
4638 end if;
4639
4640 Set_Analyzed (Op2, False);
4641 Resolve (Op2, T2);
4642 end;
996ae0b0
RK
4643
4644 else
fbf5a39b 4645 Resolve (N);
996ae0b0
RK
4646 end if;
4647 end Set_Mixed_Mode_Operand;
4648
4649 ----------------------
4650 -- Set_Operand_Type --
4651 ----------------------
4652
4653 procedure Set_Operand_Type (N : Node_Id) is
4654 begin
4655 if Etype (N) = Universal_Integer
4656 or else Etype (N) = Universal_Real
4657 then
4658 Set_Etype (N, T);
4659 end if;
4660 end Set_Operand_Type;
4661
996ae0b0
RK
4662 -- Start of processing for Resolve_Arithmetic_Op
4663
4664 begin
4665 if Comes_From_Source (N)
4666 and then Ekind (Entity (N)) = E_Function
4667 and then Is_Imported (Entity (N))
fbf5a39b 4668 and then Is_Intrinsic_Subprogram (Entity (N))
996ae0b0
RK
4669 then
4670 Resolve_Intrinsic_Operator (N, Typ);
4671 return;
4672
4673 -- Special-case for mixed-mode universal expressions or fixed point
4674 -- type operation: each argument is resolved separately. The same
4675 -- treatment is required if one of the operands of a fixed point
4676 -- operation is universal real, since in this case we don't do a
4677 -- conversion to a specific fixed-point type (instead the expander
4678 -- takes care of the case).
4679
45fc7ddb 4680 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
996ae0b0
RK
4681 and then Present (Universal_Interpretation (L))
4682 and then Present (Universal_Interpretation (R))
4683 then
4684 Resolve (L, Universal_Interpretation (L));
4685 Resolve (R, Universal_Interpretation (R));
4686 Set_Etype (N, B_Typ);
4687
4688 elsif (B_Typ = Universal_Real
45fc7ddb
HK
4689 or else Etype (N) = Universal_Fixed
4690 or else (Etype (N) = Any_Fixed
4691 and then Is_Fixed_Point_Type (B_Typ))
4692 or else (Is_Fixed_Point_Type (B_Typ)
4693 and then (Is_Integer_Or_Universal (L)
4694 or else
4695 Is_Integer_Or_Universal (R))))
4696 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
996ae0b0
RK
4697 then
4698 if TL = Universal_Integer or else TR = Universal_Integer then
4699 Check_For_Visible_Operator (N, B_Typ);
4700 end if;
4701
4702 -- If context is a fixed type and one operand is integer, the
4703 -- other is resolved with the type of the context.
4704
4705 if Is_Fixed_Point_Type (B_Typ)
4706 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4707 or else TL = Universal_Integer)
4708 then
4709 Resolve (R, B_Typ);
4710 Resolve (L, TL);
4711
4712 elsif Is_Fixed_Point_Type (B_Typ)
4713 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4714 or else TR = Universal_Integer)
4715 then
4716 Resolve (L, B_Typ);
4717 Resolve (R, TR);
4718
4719 else
4720 Set_Mixed_Mode_Operand (L, TR);
4721 Set_Mixed_Mode_Operand (R, TL);
4722 end if;
4723
45fc7ddb
HK
4724 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4725 -- multiplying operators from being used when the expected type is
4726 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4727 -- some cases where the expected type is actually Any_Real;
4728 -- Expected_Type_Is_Any_Real takes care of that case.
aa180613 4729
996ae0b0
RK
4730 if Etype (N) = Universal_Fixed
4731 or else Etype (N) = Any_Fixed
4732 then
4733 if B_Typ = Universal_Fixed
aa180613 4734 and then not Expected_Type_Is_Any_Real (N)
45fc7ddb
HK
4735 and then not Nkind_In (Parent (N), N_Type_Conversion,
4736 N_Unchecked_Type_Conversion)
996ae0b0 4737 then
45fc7ddb
HK
4738 Error_Msg_N ("type cannot be determined from context!", N);
4739 Error_Msg_N ("\explicit conversion to result type required", N);
996ae0b0
RK
4740
4741 Set_Etype (L, Any_Type);
4742 Set_Etype (R, Any_Type);
4743
4744 else
0ab80019 4745 if Ada_Version = Ada_83
45fc7ddb
HK
4746 and then Etype (N) = Universal_Fixed
4747 and then not
4748 Nkind_In (Parent (N), N_Type_Conversion,
4749 N_Unchecked_Type_Conversion)
996ae0b0
RK
4750 then
4751 Error_Msg_N
45fc7ddb
HK
4752 ("(Ada 83) fixed-point operation "
4753 & "needs explicit conversion", N);
996ae0b0
RK
4754 end if;
4755
aa180613
RD
4756 -- The expected type is "any real type" in contexts like
4757 -- type T is delta <universal_fixed-expression> ...
4758 -- in which case we need to set the type to Universal_Real
4759 -- so that static expression evaluation will work properly.
4760
4761 if Expected_Type_Is_Any_Real (N) then
4762 Set_Etype (N, Universal_Real);
4763 else
4764 Set_Etype (N, B_Typ);
4765 end if;
996ae0b0
RK
4766 end if;
4767
4768 elsif Is_Fixed_Point_Type (B_Typ)
4769 and then (Is_Integer_Or_Universal (L)
4770 or else Nkind (L) = N_Real_Literal
4771 or else Nkind (R) = N_Real_Literal
45fc7ddb 4772 or else Is_Integer_Or_Universal (R))
996ae0b0
RK
4773 then
4774 Set_Etype (N, B_Typ);
4775
4776 elsif Etype (N) = Any_Fixed then
4777
4778 -- If no previous errors, this is only possible if one operand
4779 -- is overloaded and the context is universal. Resolve as such.
4780
4781 Set_Etype (N, B_Typ);
4782 end if;
4783
4784 else
4785 if (TL = Universal_Integer or else TL = Universal_Real)
45fc7ddb
HK
4786 and then
4787 (TR = Universal_Integer or else TR = Universal_Real)
996ae0b0
RK
4788 then
4789 Check_For_Visible_Operator (N, B_Typ);
4790 end if;
4791
4792 -- If the context is Universal_Fixed and the operands are also
4793 -- universal fixed, this is an error, unless there is only one
841dd0f5 4794 -- applicable fixed_point type (usually Duration).
996ae0b0 4795
45fc7ddb 4796 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
996ae0b0
RK
4797 T := Unique_Fixed_Point_Type (N);
4798
4799 if T = Any_Type then
4800 Set_Etype (N, T);
4801 return;
4802 else
4803 Resolve (L, T);
4804 Resolve (R, T);
4805 end if;
4806
4807 else
4808 Resolve (L, B_Typ);
4809 Resolve (R, B_Typ);
4810 end if;
4811
4812 -- If one of the arguments was resolved to a non-universal type.
4813 -- label the result of the operation itself with the same type.
4814 -- Do the same for the universal argument, if any.
4815
4816 T := Intersect_Types (L, R);
4817 Set_Etype (N, Base_Type (T));
4818 Set_Operand_Type (L);
4819 Set_Operand_Type (R);
4820 end if;
4821
fbf5a39b 4822 Generate_Operator_Reference (N, Typ);
996ae0b0
RK
4823 Eval_Arithmetic_Op (N);
4824
b0186f71
AC
4825 -- In SPARK and ALFA, a multiplication or division with operands of
4826 -- fixed point types shall be qualified or explicitly converted to
4827 -- identify the result type.
4828
fe5d3068
YM
4829 if (Is_Fixed_Point_Type (Etype (L))
4830 or else Is_Fixed_Point_Type (Etype (R)))
b0186f71
AC
4831 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4832 and then
4833 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4834 then
fe5d3068
YM
4835 Check_Formal_Restriction
4836 ("operation should be qualified or explicitly converted", N);
b0186f71
AC
4837 end if;
4838
996ae0b0
RK
4839 -- Set overflow and division checking bit. Much cleverer code needed
4840 -- here eventually and perhaps the Resolve routines should be separated
4841 -- for the various arithmetic operations, since they will need
4842 -- different processing. ???
4843
4844 if Nkind (N) in N_Op then
4845 if not Overflow_Checks_Suppressed (Etype (N)) then
fbf5a39b 4846 Enable_Overflow_Check (N);
996ae0b0
RK
4847 end if;
4848
fbf5a39b
AC
4849 -- Give warning if explicit division by zero
4850
45fc7ddb 4851 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
996ae0b0
RK
4852 and then not Division_Checks_Suppressed (Etype (N))
4853 then
fbf5a39b
AC
4854 Rop := Right_Opnd (N);
4855
4856 if Compile_Time_Known_Value (Rop)
4857 and then ((Is_Integer_Type (Etype (Rop))
780d052e
RD
4858 and then Expr_Value (Rop) = Uint_0)
4859 or else
4860 (Is_Real_Type (Etype (Rop))
4861 and then Expr_Value_R (Rop) = Ureal_0))
fbf5a39b 4862 then
aa180613
RD
4863 -- Specialize the warning message according to the operation
4864
4865 case Nkind (N) is
4866 when N_Op_Divide =>
4867 Apply_Compile_Time_Constraint_Error
4868 (N, "division by zero?", CE_Divide_By_Zero,
4869 Loc => Sloc (Right_Opnd (N)));
4870
4871 when N_Op_Rem =>
4872 Apply_Compile_Time_Constraint_Error
4873 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4874 Loc => Sloc (Right_Opnd (N)));
4875
4876 when N_Op_Mod =>
4877 Apply_Compile_Time_Constraint_Error
4878 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4879 Loc => Sloc (Right_Opnd (N)));
4880
4881 -- Division by zero can only happen with division, rem,
4882 -- and mod operations.
4883
4884 when others =>
4885 raise Program_Error;
4886 end case;
fbf5a39b
AC
4887
4888 -- Otherwise just set the flag to check at run time
4889
4890 else
b7d1f17f 4891 Activate_Division_Check (N);
fbf5a39b 4892 end if;
996ae0b0 4893 end if;
45fc7ddb
HK
4894
4895 -- If Restriction No_Implicit_Conditionals is active, then it is
4896 -- violated if either operand can be negative for mod, or for rem
4897 -- if both operands can be negative.
4898
7a963087 4899 if Restriction_Check_Required (No_Implicit_Conditionals)
45fc7ddb
HK
4900 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4901 then
4902 declare
4903 Lo : Uint;
4904 Hi : Uint;
4905 OK : Boolean;
4906
4907 LNeg : Boolean;
4908 RNeg : Boolean;
4909 -- Set if corresponding operand might be negative
4910
4911 begin
5d5e9775
AC
4912 Determine_Range
4913 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
45fc7ddb
HK
4914 LNeg := (not OK) or else Lo < 0;
4915
5d5e9775
AC
4916 Determine_Range
4917 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
45fc7ddb
HK
4918 RNeg := (not OK) or else Lo < 0;
4919
5d5e9775
AC
4920 -- Check if we will be generating conditionals. There are two
4921 -- cases where that can happen, first for REM, the only case
4922 -- is largest negative integer mod -1, where the division can
4923 -- overflow, but we still have to give the right result. The
4924 -- front end generates a test for this annoying case. Here we
4925 -- just test if both operands can be negative (that's what the
4926 -- expander does, so we match its logic here).
4927
4928 -- The second case is mod where either operand can be negative.
308e6f3a 4929 -- In this case, the back end has to generate additional tests.
5d5e9775 4930
45fc7ddb
HK
4931 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4932 or else
4933 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4934 then
4935 Check_Restriction (No_Implicit_Conditionals, N);
4936 end if;
4937 end;
4938 end if;
996ae0b0
RK
4939 end if;
4940
4941 Check_Unset_Reference (L);
4942 Check_Unset_Reference (R);
996ae0b0
RK
4943 end Resolve_Arithmetic_Op;
4944
4945 ------------------
4946 -- Resolve_Call --
4947 ------------------
4948
4949 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4950 Loc : constant Source_Ptr := Sloc (N);
4951 Subp : constant Node_Id := Name (N);
4952 Nam : Entity_Id;
4953 I : Interp_Index;
4954 It : Interp;
4955 Norm_OK : Boolean;
4956 Scop : Entity_Id;
aa180613 4957 Rtype : Entity_Id;
996ae0b0 4958
ee81cbe9
AC
4959 function Same_Or_Aliased_Subprograms
4960 (S : Entity_Id;
4961 E : Entity_Id) return Boolean;
4962 -- Returns True if the subprogram entity S is the same as E or else
4963 -- S is an alias of E.
4964
001c7783
AC
4965 ---------------------------------
4966 -- Same_Or_Aliased_Subprograms --
4967 ---------------------------------
4968
ee81cbe9
AC
4969 function Same_Or_Aliased_Subprograms
4970 (S : Entity_Id;
4971 E : Entity_Id) return Boolean
4972 is
4973 Subp_Alias : constant Entity_Id := Alias (S);
ee81cbe9
AC
4974 begin
4975 return S = E
4976 or else (Present (Subp_Alias) and then Subp_Alias = E);
4977 end Same_Or_Aliased_Subprograms;
4978
4979 -- Start of processing for Resolve_Call
4980
996ae0b0 4981 begin
758c442c
GD
4982 -- The context imposes a unique interpretation with type Typ on a
4983 -- procedure or function call. Find the entity of the subprogram that
4984 -- yields the expected type, and propagate the corresponding formal
4985 -- constraints on the actuals. The caller has established that an
4986 -- interpretation exists, and emitted an error if not unique.
996ae0b0
RK
4987
4988 -- First deal with the case of a call to an access-to-subprogram,
4989 -- dereference made explicit in Analyze_Call.
4990
4991 if Ekind (Etype (Subp)) = E_Subprogram_Type then
996ae0b0
RK
4992 if not Is_Overloaded (Subp) then
4993 Nam := Etype (Subp);
4994
4995 else
758c442c
GD
4996 -- Find the interpretation whose type (a subprogram type) has a
4997 -- return type that is compatible with the context. Analysis of
4998 -- the node has established that one exists.
996ae0b0 4999
996ae0b0
RK
5000 Nam := Empty;
5001
1420b484 5002 Get_First_Interp (Subp, I, It);
996ae0b0 5003 while Present (It.Typ) loop
996ae0b0
RK
5004 if Covers (Typ, Etype (It.Typ)) then
5005 Nam := It.Typ;
5006 exit;
5007 end if;
5008
5009 Get_Next_Interp (I, It);
5010 end loop;
5011
5012 if No (Nam) then
5013 raise Program_Error;
5014 end if;
5015 end if;
5016
5017 -- If the prefix is not an entity, then resolve it
5018
5019 if not Is_Entity_Name (Subp) then
5020 Resolve (Subp, Nam);
5021 end if;
5022
758c442c
GD
5023 -- For an indirect call, we always invalidate checks, since we do not
5024 -- know whether the subprogram is local or global. Yes we could do
5025 -- better here, e.g. by knowing that there are no local subprograms,
aa180613 5026 -- but it does not seem worth the effort. Similarly, we kill all
758c442c 5027 -- knowledge of current constant values.
fbf5a39b
AC
5028
5029 Kill_Current_Values;
5030
b7d1f17f
HK
5031 -- If this is a procedure call which is really an entry call, do
5032 -- the conversion of the procedure call to an entry call. Protected
5033 -- operations use the same circuitry because the name in the call
5034 -- can be an arbitrary expression with special resolution rules.
996ae0b0 5035
45fc7ddb 5036 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
996ae0b0
RK
5037 or else (Is_Entity_Name (Subp)
5038 and then Ekind (Entity (Subp)) = E_Entry)
5039 then
5040 Resolve_Entry_Call (N, Typ);
5041 Check_Elab_Call (N);
fbf5a39b
AC
5042
5043 -- Kill checks and constant values, as above for indirect case
5044 -- Who knows what happens when another task is activated?
5045
5046 Kill_Current_Values;
996ae0b0
RK
5047 return;
5048
5049 -- Normal subprogram call with name established in Resolve
5050
5051 elsif not (Is_Type (Entity (Subp))) then
5052 Nam := Entity (Subp);
5053 Set_Entity_With_Style_Check (Subp, Nam);
996ae0b0
RK
5054
5055 -- Otherwise we must have the case of an overloaded call
5056
5057 else
5058 pragma Assert (Is_Overloaded (Subp));
d81b4bfe
TQ
5059
5060 -- Initialize Nam to prevent warning (we know it will be assigned
5061 -- in the loop below, but the compiler does not know that).
5062
5063 Nam := Empty;
996ae0b0
RK
5064
5065 Get_First_Interp (Subp, I, It);
996ae0b0
RK
5066 while Present (It.Typ) loop
5067 if Covers (Typ, It.Typ) then
5068 Nam := It.Nam;
5069 Set_Entity_With_Style_Check (Subp, Nam);
996ae0b0
RK
5070 exit;
5071 end if;
5072
5073 Get_Next_Interp (I, It);
5074 end loop;
5075 end if;
5076
c9b99571
ES
5077 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5078 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
53cf4600
ES
5079 and then Nkind (Subp) /= N_Explicit_Dereference
5080 and then Present (Parameter_Associations (N))
5081 then
66aa7643
TQ
5082 -- The prefix is a parameterless function call that returns an access
5083 -- to subprogram. If parameters are present in the current call, add
5084 -- add an explicit dereference. We use the base type here because
5085 -- within an instance these may be subtypes.
53cf4600
ES
5086
5087 -- The dereference is added either in Analyze_Call or here. Should
5088 -- be consolidated ???
5089
5090 Set_Is_Overloaded (Subp, False);
5091 Set_Etype (Subp, Etype (Nam));
5092 Insert_Explicit_Dereference (Subp);
5093 Nam := Designated_Type (Etype (Nam));
5094 Resolve (Subp, Nam);
5095 end if;
5096
996ae0b0
RK
5097 -- Check that a call to Current_Task does not occur in an entry body
5098
5099 if Is_RTE (Nam, RE_Current_Task) then
5100 declare
5101 P : Node_Id;
5102
5103 begin
5104 P := N;
5105 loop
5106 P := Parent (P);
45fc7ddb
HK
5107
5108 -- Exclude calls that occur within the default of a formal
5109 -- parameter of the entry, since those are evaluated outside
5110 -- of the body.
5111
5112 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
996ae0b0 5113
aa180613
RD
5114 if Nkind (P) = N_Entry_Body
5115 or else (Nkind (P) = N_Subprogram_Body
45fc7ddb 5116 and then Is_Entry_Barrier_Function (P))
aa180613
RD
5117 then
5118 Rtype := Etype (N);
996ae0b0 5119 Error_Msg_NE
aa5147f0 5120 ("?& should not be used in entry body (RM C.7(17))",
996ae0b0 5121 N, Nam);
aa180613
RD
5122 Error_Msg_NE
5123 ("\Program_Error will be raised at run time?", N, Nam);
5124 Rewrite (N,
5125 Make_Raise_Program_Error (Loc,
5126 Reason => PE_Current_Task_In_Entry_Body));
5127 Set_Etype (N, Rtype);
e65f50ec 5128 return;
996ae0b0
RK
5129 end if;
5130 end loop;
5131 end;
5132 end if;
5133
758c442c
GD
5134 -- Check that a procedure call does not occur in the context of the
5135 -- entry call statement of a conditional or timed entry call. Note that
5136 -- the case of a call to a subprogram renaming of an entry will also be
5137 -- rejected. The test for N not being an N_Entry_Call_Statement is
5138 -- defensive, covering the possibility that the processing of entry
5139 -- calls might reach this point due to later modifications of the code
5140 -- above.
996ae0b0
RK
5141
5142 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5143 and then Nkind (N) /= N_Entry_Call_Statement
5144 and then Entry_Call_Statement (Parent (N)) = N
5145 then
0791fbe9 5146 if Ada_Version < Ada_2005 then
1420b484
JM
5147 Error_Msg_N ("entry call required in select statement", N);
5148
5149 -- Ada 2005 (AI-345): If a procedure_call_statement is used
66aa7643
TQ
5150 -- for a procedure_or_entry_call, the procedure_name or
5151 -- procedure_prefix of the procedure_call_statement shall denote
1420b484
JM
5152 -- an entry renamed by a procedure, or (a view of) a primitive
5153 -- subprogram of a limited interface whose first parameter is
5154 -- a controlling parameter.
5155
5156 elsif Nkind (N) = N_Procedure_Call_Statement
5157 and then not Is_Renamed_Entry (Nam)
5158 and then not Is_Controlling_Limited_Procedure (Nam)
5159 then
5160 Error_Msg_N
c8ef728f 5161 ("entry call or dispatching primitive of interface required", N);
1420b484 5162 end if;
996ae0b0
RK
5163 end if;
5164
66aa7643
TQ
5165 -- Check that this is not a call to a protected procedure or entry from
5166 -- within a protected function.
fbf5a39b
AC
5167
5168 if Ekind (Current_Scope) = E_Function
5169 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5170 and then Ekind (Nam) /= E_Function
5171 and then Scope (Nam) = Scope (Current_Scope)
5172 then
5173 Error_Msg_N ("within protected function, protected " &
5174 "object is constant", N);
5175 Error_Msg_N ("\cannot call operation that may modify it", N);
5176 end if;
5177
45fc7ddb 5178 -- Freeze the subprogram name if not in a spec-expression. Note that we
758c442c
GD
5179 -- freeze procedure calls as well as function calls. Procedure calls are
5180 -- not frozen according to the rules (RM 13.14(14)) because it is
5181 -- impossible to have a procedure call to a non-frozen procedure in pure
5182 -- Ada, but in the code that we generate in the expander, this rule
5183 -- needs extending because we can generate procedure calls that need
5184 -- freezing.
996ae0b0 5185
45fc7ddb 5186 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
996ae0b0
RK
5187 Freeze_Expression (Subp);
5188 end if;
5189
758c442c
GD
5190 -- For a predefined operator, the type of the result is the type imposed
5191 -- by context, except for a predefined operation on universal fixed.
5192 -- Otherwise The type of the call is the type returned by the subprogram
5193 -- being called.
996ae0b0
RK
5194
5195 if Is_Predefined_Op (Nam) then
996ae0b0
RK
5196 if Etype (N) /= Universal_Fixed then
5197 Set_Etype (N, Typ);
5198 end if;
5199
758c442c
GD
5200 -- If the subprogram returns an array type, and the context requires the
5201 -- component type of that array type, the node is really an indexing of
5202 -- the parameterless call. Resolve as such. A pathological case occurs
5203 -- when the type of the component is an access to the array type. In
5204 -- this case the call is truly ambiguous.
996ae0b0 5205
0669bebe 5206 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
996ae0b0
RK
5207 and then
5208 ((Is_Array_Type (Etype (Nam))
5209 and then Covers (Typ, Component_Type (Etype (Nam))))
5210 or else (Is_Access_Type (Etype (Nam))
5211 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5212 and then
5213 Covers (Typ,
5214 Component_Type (Designated_Type (Etype (Nam))))))
5215 then
5216 declare
5217 Index_Node : Node_Id;
fbf5a39b
AC
5218 New_Subp : Node_Id;
5219 Ret_Type : constant Entity_Id := Etype (Nam);
996ae0b0
RK
5220
5221 begin
fbf5a39b
AC
5222 if Is_Access_Type (Ret_Type)
5223 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5224 then
5225 Error_Msg_N
5226 ("cannot disambiguate function call and indexing", N);
5227 else
5228 New_Subp := Relocate_Node (Subp);
5229 Set_Entity (Subp, Nam);
5230
7205254b 5231 if (Is_Array_Type (Ret_Type)
5d5e9775 5232 and then Component_Type (Ret_Type) /= Any_Type)
7205254b
JM
5233 or else
5234 (Is_Access_Type (Ret_Type)
5d5e9775
AC
5235 and then
5236 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
7205254b 5237 then
0669bebe
GB
5238 if Needs_No_Actuals (Nam) then
5239
5240 -- Indexed call to a parameterless function
5241
5242 Index_Node :=
5243 Make_Indexed_Component (Loc,
5244 Prefix =>
5245 Make_Function_Call (Loc,
5246 Name => New_Subp),
5247 Expressions => Parameter_Associations (N));
5248 else
5249 -- An Ada 2005 prefixed call to a primitive operation
5250 -- whose first parameter is the prefix. This prefix was
5251 -- prepended to the parameter list, which is actually a
3b42c566 5252 -- list of indexes. Remove the prefix in order to build
0669bebe
GB
5253 -- the proper indexed component.
5254
5255 Index_Node :=
5256 Make_Indexed_Component (Loc,
5257 Prefix =>
5258 Make_Function_Call (Loc,
5259 Name => New_Subp,
5260 Parameter_Associations =>
5261 New_List
5262 (Remove_Head (Parameter_Associations (N)))),
5263 Expressions => Parameter_Associations (N));
5264 end if;
fbf5a39b 5265
74e7891f
RD
5266 -- Preserve the parenthesis count of the node
5267
5268 Set_Paren_Count (Index_Node, Paren_Count (N));
5269
fbf5a39b
AC
5270 -- Since we are correcting a node classification error made
5271 -- by the parser, we call Replace rather than Rewrite.
5272
5273 Replace (N, Index_Node);
74e7891f 5274
fbf5a39b
AC
5275 Set_Etype (Prefix (N), Ret_Type);
5276 Set_Etype (N, Typ);
5277 Resolve_Indexed_Component (N, Typ);
5278 Check_Elab_Call (Prefix (N));
5279 end if;
996ae0b0
RK
5280 end if;
5281
5282 return;
5283 end;
5284
5285 else
5286 Set_Etype (N, Etype (Nam));
5287 end if;
5288
5289 -- In the case where the call is to an overloaded subprogram, Analyze
5290 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5291 -- such a case Normalize_Actuals needs to be called once more to order
5292 -- the actuals correctly. Otherwise the call will have the ordering
5293 -- given by the last overloaded subprogram whether this is the correct
5294 -- one being called or not.
5295
5296 if Is_Overloaded (Subp) then
5297 Normalize_Actuals (N, Nam, False, Norm_OK);
5298 pragma Assert (Norm_OK);
5299 end if;
5300
5301 -- In any case, call is fully resolved now. Reset Overload flag, to
5302 -- prevent subsequent overload resolution if node is analyzed again
5303
5304 Set_Is_Overloaded (Subp, False);
5305 Set_Is_Overloaded (N, False);
5306
758c442c
GD
5307 -- If we are calling the current subprogram from immediately within its
5308 -- body, then that is the case where we can sometimes detect cases of
5309 -- infinite recursion statically. Do not try this in case restriction
b7d1f17f 5310 -- No_Recursion is in effect anyway, and do it only for source calls.
996ae0b0 5311
b7d1f17f
HK
5312 if Comes_From_Source (N) then
5313 Scop := Current_Scope;
996ae0b0 5314
26570b21
RD
5315 -- Issue warning for possible infinite recursion in the absence
5316 -- of the No_Recursion restriction.
5317
ee81cbe9 5318 if Same_Or_Aliased_Subprograms (Nam, Scop)
b7d1f17f
HK
5319 and then not Restriction_Active (No_Recursion)
5320 and then Check_Infinite_Recursion (N)
5321 then
5322 -- Here we detected and flagged an infinite recursion, so we do
da20aa43
RD
5323 -- not need to test the case below for further warnings. Also we
5324 -- are all done if we now have a raise SE node.
996ae0b0 5325
26570b21
RD
5326 if Nkind (N) = N_Raise_Storage_Error then
5327 return;
5328 end if;
996ae0b0 5329
26570b21
RD
5330 -- If call is to immediately containing subprogram, then check for
5331 -- the case of a possible run-time detectable infinite recursion.
996ae0b0 5332
b7d1f17f
HK
5333 else
5334 Scope_Loop : while Scop /= Standard_Standard loop
ee81cbe9 5335 if Same_Or_Aliased_Subprograms (Nam, Scop) then
b7d1f17f
HK
5336
5337 -- Although in general case, recursion is not statically
5338 -- checkable, the case of calling an immediately containing
5339 -- subprogram is easy to catch.
5340
5341 Check_Restriction (No_Recursion, N);
5342
5343 -- If the recursive call is to a parameterless subprogram,
5344 -- then even if we can't statically detect infinite
5345 -- recursion, this is pretty suspicious, and we output a
5346 -- warning. Furthermore, we will try later to detect some
5347 -- cases here at run time by expanding checking code (see
5348 -- Detect_Infinite_Recursion in package Exp_Ch6).
5349
5350 -- If the recursive call is within a handler, do not emit a
5351 -- warning, because this is a common idiom: loop until input
5352 -- is correct, catch illegal input in handler and restart.
5353
5354 if No (First_Formal (Nam))
5355 and then Etype (Nam) = Standard_Void_Type
5356 and then not Error_Posted (N)
5357 and then Nkind (Parent (N)) /= N_Exception_Handler
aa180613 5358 then
b7d1f17f
HK
5359 -- For the case of a procedure call. We give the message
5360 -- only if the call is the first statement in a sequence
5361 -- of statements, or if all previous statements are
5362 -- simple assignments. This is simply a heuristic to
5363 -- decrease false positives, without losing too many good
5364 -- warnings. The idea is that these previous statements
5365 -- may affect global variables the procedure depends on.
5366
5367 if Nkind (N) = N_Procedure_Call_Statement
5368 and then Is_List_Member (N)
5369 then
5370 declare
5371 P : Node_Id;
5372 begin
5373 P := Prev (N);
5374 while Present (P) loop
5375 if Nkind (P) /= N_Assignment_Statement then
5376 exit Scope_Loop;
5377 end if;
5378
5379 Prev (P);
5380 end loop;
5381 end;
5382 end if;
5383
5384 -- Do not give warning if we are in a conditional context
5385
aa180613 5386 declare
b7d1f17f 5387 K : constant Node_Kind := Nkind (Parent (N));
aa180613 5388 begin
b7d1f17f 5389 if (K = N_Loop_Statement
b5c739f9 5390 and then Present (Iteration_Scheme (Parent (N))))
b7d1f17f
HK
5391 or else K = N_If_Statement
5392 or else K = N_Elsif_Part
5393 or else K = N_Case_Statement_Alternative
5394 then
5395 exit Scope_Loop;
5396 end if;
aa180613 5397 end;
aa180613 5398
b7d1f17f 5399 -- Here warning is to be issued
aa180613 5400
b7d1f17f
HK
5401 Set_Has_Recursive_Call (Nam);
5402 Error_Msg_N
aa5147f0 5403 ("?possible infinite recursion!", N);
b7d1f17f 5404 Error_Msg_N
aa5147f0 5405 ("\?Storage_Error may be raised at run time!", N);
b7d1f17f 5406 end if;
aa180613 5407
b7d1f17f 5408 exit Scope_Loop;
996ae0b0
RK
5409 end if;
5410
b7d1f17f
HK
5411 Scop := Scope (Scop);
5412 end loop Scope_Loop;
5413 end if;
996ae0b0
RK
5414 end if;
5415
b5c739f9
RD
5416 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5417
5418 Check_Obsolescent_2005_Entity (Nam, Subp);
5419
996ae0b0
RK
5420 -- If subprogram name is a predefined operator, it was given in
5421 -- functional notation. Replace call node with operator node, so
5422 -- that actuals can be resolved appropriately.
5423
5424 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5425 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5426 return;
5427
5428 elsif Present (Alias (Nam))
5429 and then Is_Predefined_Op (Alias (Nam))
5430 then
5431 Resolve_Actuals (N, Nam);
5432 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5433 return;
5434 end if;
5435
fbf5a39b
AC
5436 -- Create a transient scope if the resulting type requires it
5437
4017021b
AC
5438 -- There are several notable exceptions:
5439
4d2907fd 5440 -- a) In init procs, the transient scope overhead is not needed, and is
4017021b
AC
5441 -- even incorrect when the call is a nested initialization call for a
5442 -- component whose expansion may generate adjust calls. However, if the
5443 -- call is some other procedure call within an initialization procedure
5444 -- (for example a call to Create_Task in the init_proc of the task
5445 -- run-time record) a transient scope must be created around this call.
5446
4d2907fd 5447 -- b) Enumeration literal pseudo-calls need no transient scope
4017021b 5448
4d2907fd 5449 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
4017021b 5450 -- functions) do not use the secondary stack even though the return
4d2907fd 5451 -- type may be unconstrained.
4017021b 5452
4d2907fd 5453 -- d) Calls to a build-in-place function, since such functions may
4017021b
AC
5454 -- allocate their result directly in a target object, and cases where
5455 -- the result does get allocated in the secondary stack are checked for
5456 -- within the specialized Exp_Ch6 procedures for expanding those
5457 -- build-in-place calls.
5458
5459 -- e) If the subprogram is marked Inline_Always, then even if it returns
c8ef728f 5460 -- an unconstrained type the call does not require use of the secondary
45fc7ddb
HK
5461 -- stack. However, inlining will only take place if the body to inline
5462 -- is already present. It may not be available if e.g. the subprogram is
5463 -- declared in a child instance.
c8ef728f 5464
4017021b
AC
5465 -- If this is an initialization call for a type whose construction
5466 -- uses the secondary stack, and it is not a nested call to initialize
5467 -- a component, we do need to create a transient scope for it. We
5468 -- check for this by traversing the type in Check_Initialization_Call.
5469
c8ef728f 5470 if Is_Inlined (Nam)
45fc7ddb
HK
5471 and then Has_Pragma_Inline_Always (Nam)
5472 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5473 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
c8ef728f
ES
5474 then
5475 null;
5476
4017021b
AC
5477 elsif Ekind (Nam) = E_Enumeration_Literal
5478 or else Is_Build_In_Place_Function (Nam)
5479 or else Is_Intrinsic_Subprogram (Nam)
5480 then
5481 null;
5482
c8ef728f 5483 elsif Expander_Active
996ae0b0
RK
5484 and then Is_Type (Etype (Nam))
5485 and then Requires_Transient_Scope (Etype (Nam))
4017021b
AC
5486 and then
5487 (not Within_Init_Proc
5488 or else
5489 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
996ae0b0 5490 then
0669bebe 5491 Establish_Transient_Scope (N, Sec_Stack => True);
996ae0b0 5492
a9f4e3d2
AC
5493 -- If the call appears within the bounds of a loop, it will
5494 -- be rewritten and reanalyzed, nothing left to do here.
5495
5496 if Nkind (N) /= N_Function_Call then
5497 return;
5498 end if;
5499
fbf5a39b 5500 elsif Is_Init_Proc (Nam)
996ae0b0
RK
5501 and then not Within_Init_Proc
5502 then
5503 Check_Initialization_Call (N, Nam);
5504 end if;
5505
5506 -- A protected function cannot be called within the definition of the
5507 -- enclosing protected type.
5508
5509 if Is_Protected_Type (Scope (Nam))
5510 and then In_Open_Scopes (Scope (Nam))
5511 and then not Has_Completion (Scope (Nam))
5512 then
5513 Error_Msg_NE
5514 ("& cannot be called before end of protected definition", N, Nam);
5515 end if;
5516
5517 -- Propagate interpretation to actuals, and add default expressions
5518 -- where needed.
5519
5520 if Present (First_Formal (Nam)) then
5521 Resolve_Actuals (N, Nam);
5522
d81b4bfe
TQ
5523 -- Overloaded literals are rewritten as function calls, for purpose of
5524 -- resolution. After resolution, we can replace the call with the
5525 -- literal itself.
996ae0b0
RK
5526
5527 elsif Ekind (Nam) = E_Enumeration_Literal then
5528 Copy_Node (Subp, N);
5529 Resolve_Entity_Name (N, Typ);
5530
fbf5a39b 5531 -- Avoid validation, since it is a static function call
996ae0b0 5532
e65f50ec 5533 Generate_Reference (Nam, Subp);
996ae0b0
RK
5534 return;
5535 end if;
5536
b7d1f17f
HK
5537 -- If the subprogram is not global, then kill all saved values and
5538 -- checks. This is a bit conservative, since in many cases we could do
5539 -- better, but it is not worth the effort. Similarly, we kill constant
5540 -- values. However we do not need to do this for internal entities
5541 -- (unless they are inherited user-defined subprograms), since they
5542 -- are not in the business of molesting local values.
5543
5544 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5545 -- kill all checks and values for calls to global subprograms. This
5546 -- takes care of the case where an access to a local subprogram is
5547 -- taken, and could be passed directly or indirectly and then called
5548 -- from almost any context.
aa180613
RD
5549
5550 -- Note: we do not do this step till after resolving the actuals. That
5551 -- way we still take advantage of the current value information while
5552 -- scanning the actuals.
5553
45fc7ddb
HK
5554 -- We suppress killing values if we are processing the nodes associated
5555 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5556 -- type kills all the values as part of analyzing the code that
5557 -- initializes the dispatch tables.
5558
5559 if Inside_Freezing_Actions = 0
5560 and then (not Is_Library_Level_Entity (Nam)
24357840
RD
5561 or else Suppress_Value_Tracking_On_Call
5562 (Nearest_Dynamic_Scope (Current_Scope)))
aa180613
RD
5563 and then (Comes_From_Source (Nam)
5564 or else (Present (Alias (Nam))
5565 and then Comes_From_Source (Alias (Nam))))
5566 then
5567 Kill_Current_Values;
5568 end if;
5569
36fcf362
RD
5570 -- If we are warning about unread OUT parameters, this is the place to
5571 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5572 -- after the above call to Kill_Current_Values (since that call clears
5573 -- the Last_Assignment field of all local variables).
67ce0d7e 5574
36fcf362 5575 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
67ce0d7e
RD
5576 and then Comes_From_Source (N)
5577 and then In_Extended_Main_Source_Unit (N)
5578 then
5579 declare
5580 F : Entity_Id;
5581 A : Node_Id;
5582
5583 begin
5584 F := First_Formal (Nam);
5585 A := First_Actual (N);
5586 while Present (F) and then Present (A) loop
964f13da 5587 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
36fcf362 5588 and then Warn_On_Modified_As_Out_Parameter (F)
67ce0d7e
RD
5589 and then Is_Entity_Name (A)
5590 and then Present (Entity (A))
36fcf362 5591 and then Comes_From_Source (N)
67ce0d7e
RD
5592 and then Safe_To_Capture_Value (N, Entity (A))
5593 then
5594 Set_Last_Assignment (Entity (A), A);
5595 end if;
5596
5597 Next_Formal (F);
5598 Next_Actual (A);
5599 end loop;
5600 end;
5601 end if;
5602
996ae0b0
RK
5603 -- If the subprogram is a primitive operation, check whether or not
5604 -- it is a correct dispatching call.
5605
5606 if Is_Overloadable (Nam)
5607 and then Is_Dispatching_Operation (Nam)
5608 then
5609 Check_Dispatching_Call (N);
5610
0669bebe
GB
5611 elsif Ekind (Nam) /= E_Subprogram_Type
5612 and then Is_Abstract_Subprogram (Nam)
996ae0b0
RK
5613 and then not In_Instance
5614 then
5615 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5616 end if;
5617
e65f50ec
ES
5618 -- If this is a dispatching call, generate the appropriate reference,
5619 -- for better source navigation in GPS.
5620
5621 if Is_Overloadable (Nam)
5622 and then Present (Controlling_Argument (N))
5623 then
5624 Generate_Reference (Nam, Subp, 'R');
c5d91669 5625
9c870c90 5626 -- Normal case, not a dispatching call. Generate a call reference.
c5d91669 5627
e65f50ec 5628 else
9c870c90 5629 Generate_Reference (Nam, Subp, 's');
e65f50ec
ES
5630 end if;
5631
996ae0b0
RK
5632 if Is_Intrinsic_Subprogram (Nam) then
5633 Check_Intrinsic_Call (N);
5634 end if;
5635
5b2217f8 5636 -- Check for violation of restriction No_Specific_Termination_Handlers
dce86910 5637 -- and warn on a potentially blocking call to Abort_Task.
5b2217f8
RD
5638
5639 if Is_RTE (Nam, RE_Set_Specific_Handler)
5640 or else
5641 Is_RTE (Nam, RE_Specific_Handler)
5642 then
5643 Check_Restriction (No_Specific_Termination_Handlers, N);
dce86910
AC
5644
5645 elsif Is_RTE (Nam, RE_Abort_Task) then
5646 Check_Potentially_Blocking_Operation (N);
5b2217f8
RD
5647 end if;
5648
806f6d37
AC
5649 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5650 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5651 -- need to check the second argument to determine whether it is an
5652 -- absolute or relative timing event.
afbcdf5e 5653
806f6d37
AC
5654 if Is_RTE (Nam, RE_Set_Handler)
5655 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5656 then
afbcdf5e
AC
5657 Check_Restriction (No_Relative_Delay, N);
5658 end if;
5659
9cbfc269
AC
5660 -- Issue an error for a call to an eliminated subprogram. We skip this
5661 -- in a spec expression, e.g. a call in a default parameter value, since
5662 -- we are not really doing a call at this time. That's important because
5663 -- the spec expression may itself belong to an eliminated subprogram.
16212e89 5664
9cbfc269
AC
5665 if not In_Spec_Expression then
5666 Check_For_Eliminated_Subprogram (Subp, Nam);
5667 end if;
16212e89 5668
67ce0d7e
RD
5669 -- All done, evaluate call and deal with elaboration issues
5670
c01a9391 5671 Eval_Call (N);
996ae0b0 5672 Check_Elab_Call (N);
76b84bf0 5673 Warn_On_Overlapping_Actuals (Nam, N);
996ae0b0
RK
5674 end Resolve_Call;
5675
19d846a0
RD
5676 -----------------------------
5677 -- Resolve_Case_Expression --
5678 -----------------------------
5679
5680 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5681 Alt : Node_Id;
5682
5683 begin
5684 Alt := First (Alternatives (N));
5685 while Present (Alt) loop
5686 Resolve (Expression (Alt), Typ);
5687 Next (Alt);
5688 end loop;
5689
5690 Set_Etype (N, Typ);
5691 Eval_Case_Expression (N);
5692 end Resolve_Case_Expression;
5693
996ae0b0
RK
5694 -------------------------------
5695 -- Resolve_Character_Literal --
5696 -------------------------------
5697
5698 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5699 B_Typ : constant Entity_Id := Base_Type (Typ);
5700 C : Entity_Id;
5701
5702 begin
5703 -- Verify that the character does belong to the type of the context
5704
5705 Set_Etype (N, B_Typ);
5706 Eval_Character_Literal (N);
5707
82c80734
RD
5708 -- Wide_Wide_Character literals must always be defined, since the set
5709 -- of wide wide character literals is complete, i.e. if a character
5710 -- literal is accepted by the parser, then it is OK for wide wide
5711 -- character (out of range character literals are rejected).
996ae0b0 5712
82c80734 5713 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
996ae0b0
RK
5714 return;
5715
5716 -- Always accept character literal for type Any_Character, which
5717 -- occurs in error situations and in comparisons of literals, both
5718 -- of which should accept all literals.
5719
5720 elsif B_Typ = Any_Character then
5721 return;
5722
5723 -- For Standard.Character or a type derived from it, check that
5724 -- the literal is in range
5725
5726 elsif Root_Type (B_Typ) = Standard_Character then
82c80734
RD
5727 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5728 return;
5729 end if;
5730
5731 -- For Standard.Wide_Character or a type derived from it, check
5732 -- that the literal is in range
5733
5734 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5735 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
996ae0b0
RK
5736 return;
5737 end if;
5738
82c80734
RD
5739 -- For Standard.Wide_Wide_Character or a type derived from it, we
5740 -- know the literal is in range, since the parser checked!
5741
5742 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5743 return;
5744
d81b4bfe
TQ
5745 -- If the entity is already set, this has already been resolved in a
5746 -- generic context, or comes from expansion. Nothing else to do.
996ae0b0
RK
5747
5748 elsif Present (Entity (N)) then
5749 return;
5750
d81b4bfe
TQ
5751 -- Otherwise we have a user defined character type, and we can use the
5752 -- standard visibility mechanisms to locate the referenced entity.
996ae0b0
RK
5753
5754 else
5755 C := Current_Entity (N);
996ae0b0
RK
5756 while Present (C) loop
5757 if Etype (C) = B_Typ then
5758 Set_Entity_With_Style_Check (N, C);
5759 Generate_Reference (C, N);
5760 return;
5761 end if;
5762
5763 C := Homonym (C);
5764 end loop;
5765 end if;
5766
5767 -- If we fall through, then the literal does not match any of the
5768 -- entries of the enumeration type. This isn't just a constraint
5769 -- error situation, it is an illegality (see RM 4.2).
5770
5771 Error_Msg_NE
5772 ("character not defined for }", N, First_Subtype (B_Typ));
996ae0b0
RK
5773 end Resolve_Character_Literal;
5774
5775 ---------------------------
5776 -- Resolve_Comparison_Op --
5777 ---------------------------
5778
5779 -- Context requires a boolean type, and plays no role in resolution.
fbf5a39b
AC
5780 -- Processing identical to that for equality operators. The result
5781 -- type is the base type, which matters when pathological subtypes of
5782 -- booleans with limited ranges are used.
996ae0b0
RK
5783
5784 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5785 L : constant Node_Id := Left_Opnd (N);
5786 R : constant Node_Id := Right_Opnd (N);
5787 T : Entity_Id;
5788
5789 begin
d81b4bfe
TQ
5790 -- If this is an intrinsic operation which is not predefined, use the
5791 -- types of its declared arguments to resolve the possibly overloaded
5792 -- operands. Otherwise the operands are unambiguous and specify the
5793 -- expected type.
996ae0b0
RK
5794
5795 if Scope (Entity (N)) /= Standard_Standard then
5796 T := Etype (First_Entity (Entity (N)));
1420b484 5797
996ae0b0
RK
5798 else
5799 T := Find_Unique_Type (L, R);
5800
5801 if T = Any_Fixed then
5802 T := Unique_Fixed_Point_Type (L);
5803 end if;
5804 end if;
5805
fbf5a39b 5806 Set_Etype (N, Base_Type (Typ));
996ae0b0
RK
5807 Generate_Reference (T, N, ' ');
5808
bd29d519 5809 -- Skip remaining processing if already set to Any_Type
996ae0b0 5810
bd29d519
AC
5811 if T = Any_Type then
5812 return;
5813 end if;
5814
5815 -- Deal with other error cases
996ae0b0 5816
bd29d519
AC
5817 if T = Any_String or else
5818 T = Any_Composite or else
5819 T = Any_Character
5820 then
5821 if T = Any_Character then
5822 Ambiguous_Character (L);
996ae0b0 5823 else
bd29d519 5824 Error_Msg_N ("ambiguous operands for comparison", N);
996ae0b0 5825 end if;
bd29d519
AC
5826
5827 Set_Etype (N, Any_Type);
5828 return;
996ae0b0 5829 end if;
bd29d519
AC
5830
5831 -- Resolve the operands if types OK
5832
5833 Resolve (L, T);
5834 Resolve (R, T);
5835 Check_Unset_Reference (L);
5836 Check_Unset_Reference (R);
5837 Generate_Operator_Reference (N, T);
5838 Check_Low_Bound_Tested (N);
5839
b0186f71
AC
5840 -- In SPARK or ALFA, ordering operators <, <=, >, >= are not defined
5841 -- for Boolean types or array types except String.
5842
fe5d3068
YM
5843 if Is_Boolean_Type (T) then
5844 Check_Formal_Restriction
5845 ("comparison is not defined on Boolean type", N);
5846 elsif Is_Array_Type (T)
5847 and then Base_Type (T) /= Standard_String
b0186f71 5848 then
fe5d3068
YM
5849 Check_Formal_Restriction
5850 ("comparison is not defined on array types other than String", N);
5851 else
5852 null;
b0186f71
AC
5853 end if;
5854
bd29d519
AC
5855 -- Check comparison on unordered enumeration
5856
5857 if Comes_From_Source (N)
5858 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5859 then
5860 Error_Msg_N ("comparison on unordered enumeration type?", N);
5861 end if;
5862
5863 -- Evaluate the relation (note we do this after the above check
5864 -- since this Eval call may change N to True/False.
5865
5866 Eval_Relational_Op (N);
996ae0b0
RK
5867 end Resolve_Comparison_Op;
5868
5869 ------------------------------------
5870 -- Resolve_Conditional_Expression --
5871 ------------------------------------
5872
5873 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5874 Condition : constant Node_Id := First (Expressions (N));
5875 Then_Expr : constant Node_Id := Next (Condition);
b46be8a2
RD
5876 Else_Expr : Node_Id := Next (Then_Expr);
5877
996ae0b0 5878 begin
b46be8a2 5879 Resolve (Condition, Any_Boolean);
996ae0b0 5880 Resolve (Then_Expr, Typ);
b46be8a2
RD
5881
5882 -- If ELSE expression present, just resolve using the determined type
5883
5884 if Present (Else_Expr) then
5885 Resolve (Else_Expr, Typ);
5886
5887 -- If no ELSE expression is present, root type must be Standard.Boolean
5888 -- and we provide a Standard.True result converted to the appropriate
5889 -- Boolean type (in case it is a derived boolean type).
5890
5891 elsif Root_Type (Typ) = Standard_Boolean then
5892 Else_Expr :=
5893 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5894 Analyze_And_Resolve (Else_Expr, Typ);
5895 Append_To (Expressions (N), Else_Expr);
5896
5897 else
5898 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5899 Append_To (Expressions (N), Error);
5900 end if;
5901
996ae0b0
RK
5902 Set_Etype (N, Typ);
5903 Eval_Conditional_Expression (N);
5904 end Resolve_Conditional_Expression;
5905
5906 -----------------------------------------
5907 -- Resolve_Discrete_Subtype_Indication --
5908 -----------------------------------------
5909
5910 procedure Resolve_Discrete_Subtype_Indication
5911 (N : Node_Id;
5912 Typ : Entity_Id)
5913 is
5914 R : Node_Id;
5915 S : Entity_Id;
5916
5917 begin
5918 Analyze (Subtype_Mark (N));
5919 S := Entity (Subtype_Mark (N));
5920
5921 if Nkind (Constraint (N)) /= N_Range_Constraint then
5922 Error_Msg_N ("expect range constraint for discrete type", N);
5923 Set_Etype (N, Any_Type);
5924
5925 else
5926 R := Range_Expression (Constraint (N));
5c736541
RD
5927
5928 if R = Error then
5929 return;
5930 end if;
5931
996ae0b0
RK
5932 Analyze (R);
5933
5934 if Base_Type (S) /= Base_Type (Typ) then
5935 Error_Msg_NE
5936 ("expect subtype of }", N, First_Subtype (Typ));
5937
5938 -- Rewrite the constraint as a range of Typ
5939 -- to allow compilation to proceed further.
5940
5941 Set_Etype (N, Typ);
5942 Rewrite (Low_Bound (R),
5943 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5944 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5945 Attribute_Name => Name_First));
5946 Rewrite (High_Bound (R),
5947 Make_Attribute_Reference (Sloc (High_Bound (R)),
5948 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5949 Attribute_Name => Name_First));
5950
5951 else
5952 Resolve (R, Typ);
5953 Set_Etype (N, Etype (R));
5954
5955 -- Additionally, we must check that the bounds are compatible
5956 -- with the given subtype, which might be different from the
5957 -- type of the context.
5958
5959 Apply_Range_Check (R, S);
5960
5961 -- ??? If the above check statically detects a Constraint_Error
5962 -- it replaces the offending bound(s) of the range R with a
5963 -- Constraint_Error node. When the itype which uses these bounds
5964 -- is frozen the resulting call to Duplicate_Subexpr generates
5965 -- a new temporary for the bounds.
5966
5967 -- Unfortunately there are other itypes that are also made depend
5968 -- on these bounds, so when Duplicate_Subexpr is called they get
5969 -- a forward reference to the newly created temporaries and Gigi
5970 -- aborts on such forward references. This is probably sign of a
5971 -- more fundamental problem somewhere else in either the order of
5972 -- itype freezing or the way certain itypes are constructed.
5973
5974 -- To get around this problem we call Remove_Side_Effects right
5975 -- away if either bounds of R are a Constraint_Error.
5976
5977 declare
fbf5a39b
AC
5978 L : constant Node_Id := Low_Bound (R);
5979 H : constant Node_Id := High_Bound (R);
996ae0b0
RK
5980
5981 begin
5982 if Nkind (L) = N_Raise_Constraint_Error then
5983 Remove_Side_Effects (L);
5984 end if;
5985
5986 if Nkind (H) = N_Raise_Constraint_Error then
5987 Remove_Side_Effects (H);
5988 end if;
5989 end;
5990
5991 Check_Unset_Reference (Low_Bound (R));
5992 Check_Unset_Reference (High_Bound (R));
5993 end if;
5994 end if;
5995 end Resolve_Discrete_Subtype_Indication;
5996
5997 -------------------------
5998 -- Resolve_Entity_Name --
5999 -------------------------
6000
6001 -- Used to resolve identifiers and expanded names
6002
6003 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6004 E : constant Entity_Id := Entity (N);
6005
6006 begin
07fc65c4
GB
6007 -- If garbage from errors, set to Any_Type and return
6008
6009 if No (E) and then Total_Errors_Detected /= 0 then
6010 Set_Etype (N, Any_Type);
6011 return;
6012 end if;
6013
996ae0b0
RK
6014 -- Replace named numbers by corresponding literals. Note that this is
6015 -- the one case where Resolve_Entity_Name must reset the Etype, since
6016 -- it is currently marked as universal.
6017
6018 if Ekind (E) = E_Named_Integer then
6019 Set_Etype (N, Typ);
6020 Eval_Named_Integer (N);
6021
6022 elsif Ekind (E) = E_Named_Real then
6023 Set_Etype (N, Typ);
6024 Eval_Named_Real (N);
6025
6989bc1f
AC
6026 -- For enumeration literals, we need to make sure that a proper style
6027 -- check is done, since such literals are overloaded, and thus we did
6028 -- not do a style check during the first phase of analysis.
6029
6030 elsif Ekind (E) = E_Enumeration_Literal then
6031 Set_Entity_With_Style_Check (N, E);
6032 Eval_Entity_Name (N);
6033
e606088a 6034 -- Case of subtype name appearing as an operand in expression
996ae0b0
RK
6035
6036 elsif Is_Type (E) then
e606088a
AC
6037
6038 -- Allow use of subtype if it is a concurrent type where we are
6039 -- currently inside the body. This will eventually be expanded into a
6040 -- call to Self (for tasks) or _object (for protected objects). Any
6041 -- other use of a subtype is invalid.
6042
996ae0b0
RK
6043 if Is_Concurrent_Type (E)
6044 and then In_Open_Scopes (E)
6045 then
6046 null;
e606088a 6047
308e6f3a 6048 -- Any other use is an error
e606088a 6049
996ae0b0
RK
6050 else
6051 Error_Msg_N
758c442c 6052 ("invalid use of subtype mark in expression or call", N);
996ae0b0
RK
6053 end if;
6054
6055 -- Check discriminant use if entity is discriminant in current scope,
6056 -- i.e. discriminant of record or concurrent type currently being
6057 -- analyzed. Uses in corresponding body are unrestricted.
6058
6059 elsif Ekind (E) = E_Discriminant
6060 and then Scope (E) = Current_Scope
6061 and then not Has_Completion (Current_Scope)
6062 then
6063 Check_Discriminant_Use (N);
6064
6065 -- A parameterless generic function cannot appear in a context that
6066 -- requires resolution.
6067
6068 elsif Ekind (E) = E_Generic_Function then
6069 Error_Msg_N ("illegal use of generic function", N);
6070
6071 elsif Ekind (E) = E_Out_Parameter
0ab80019 6072 and then Ada_Version = Ada_83
996ae0b0
RK
6073 and then (Nkind (Parent (N)) in N_Op
6074 or else (Nkind (Parent (N)) = N_Assignment_Statement
6075 and then N = Expression (Parent (N)))
6076 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6077 then
6078 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6079
6080 -- In all other cases, just do the possible static evaluation
6081
6082 else
d81b4bfe
TQ
6083 -- A deferred constant that appears in an expression must have a
6084 -- completion, unless it has been removed by in-place expansion of
6085 -- an aggregate.
996ae0b0
RK
6086
6087 if Ekind (E) = E_Constant
6088 and then Comes_From_Source (E)
6089 and then No (Constant_Value (E))
6090 and then Is_Frozen (Etype (E))
45fc7ddb 6091 and then not In_Spec_Expression
996ae0b0
RK
6092 and then not Is_Imported (E)
6093 then
996ae0b0
RK
6094 if No_Initialization (Parent (E))
6095 or else (Present (Full_View (E))
6096 and then No_Initialization (Parent (Full_View (E))))
6097 then
6098 null;
6099 else
6100 Error_Msg_N (
6101 "deferred constant is frozen before completion", N);
6102 end if;
6103 end if;
6104
6105 Eval_Entity_Name (N);
6106 end if;
6107 end Resolve_Entity_Name;
6108
6109 -------------------
6110 -- Resolve_Entry --
6111 -------------------
6112
6113 procedure Resolve_Entry (Entry_Name : Node_Id) is
6114 Loc : constant Source_Ptr := Sloc (Entry_Name);
6115 Nam : Entity_Id;
6116 New_N : Node_Id;
6117 S : Entity_Id;
6118 Tsk : Entity_Id;
6119 E_Name : Node_Id;
6120 Index : Node_Id;
6121
6122 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6123 -- If the bounds of the entry family being called depend on task
6124 -- discriminants, build a new index subtype where a discriminant is
6125 -- replaced with the value of the discriminant of the target task.
6126 -- The target task is the prefix of the entry name in the call.
6127
6128 -----------------------
6129 -- Actual_Index_Type --
6130 -----------------------
6131
6132 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
fbf5a39b
AC
6133 Typ : constant Entity_Id := Entry_Index_Type (E);
6134 Tsk : constant Entity_Id := Scope (E);
6135 Lo : constant Node_Id := Type_Low_Bound (Typ);
6136 Hi : constant Node_Id := Type_High_Bound (Typ);
996ae0b0
RK
6137 New_T : Entity_Id;
6138
6139 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6140 -- If the bound is given by a discriminant, replace with a reference
d81b4bfe
TQ
6141 -- to the discriminant of the same name in the target task. If the
6142 -- entry name is the target of a requeue statement and the entry is
6143 -- in the current protected object, the bound to be used is the
008f6fd3 6144 -- discriminal of the object (see Apply_Range_Checks for details of
d81b4bfe 6145 -- the transformation).
996ae0b0
RK
6146
6147 -----------------------------
6148 -- Actual_Discriminant_Ref --
6149 -----------------------------
6150
6151 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
fbf5a39b 6152 Typ : constant Entity_Id := Etype (Bound);
996ae0b0
RK
6153 Ref : Node_Id;
6154
6155 begin
6156 Remove_Side_Effects (Bound);
6157
6158 if not Is_Entity_Name (Bound)
6159 or else Ekind (Entity (Bound)) /= E_Discriminant
6160 then
6161 return Bound;
6162
6163 elsif Is_Protected_Type (Tsk)
6164 and then In_Open_Scopes (Tsk)
6165 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6166 then
6ca9ec9c
AC
6167 -- Note: here Bound denotes a discriminant of the corresponding
6168 -- record type tskV, whose discriminal is a formal of the
6169 -- init-proc tskVIP. What we want is the body discriminal,
6170 -- which is associated to the discriminant of the original
6171 -- concurrent type tsk.
6172
5a153b27
AC
6173 return New_Occurrence_Of
6174 (Find_Body_Discriminal (Entity (Bound)), Loc);
996ae0b0
RK
6175
6176 else
6177 Ref :=
6178 Make_Selected_Component (Loc,
6179 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6180 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6181 Analyze (Ref);
6182 Resolve (Ref, Typ);
6183 return Ref;
6184 end if;
6185 end Actual_Discriminant_Ref;
6186
6187 -- Start of processing for Actual_Index_Type
6188
6189 begin
6190 if not Has_Discriminants (Tsk)
6191 or else (not Is_Entity_Name (Lo)
d81b4bfe
TQ
6192 and then
6193 not Is_Entity_Name (Hi))
996ae0b0
RK
6194 then
6195 return Entry_Index_Type (E);
6196
6197 else
6198 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6199 Set_Etype (New_T, Base_Type (Typ));
6200 Set_Size_Info (New_T, Typ);
6201 Set_RM_Size (New_T, RM_Size (Typ));
6202 Set_Scalar_Range (New_T,
6203 Make_Range (Sloc (Entry_Name),
6204 Low_Bound => Actual_Discriminant_Ref (Lo),
6205 High_Bound => Actual_Discriminant_Ref (Hi)));
6206
6207 return New_T;
6208 end if;
6209 end Actual_Index_Type;
6210
6211 -- Start of processing of Resolve_Entry
6212
6213 begin
6214 -- Find name of entry being called, and resolve prefix of name
6215 -- with its own type. The prefix can be overloaded, and the name
6216 -- and signature of the entry must be taken into account.
6217
6218 if Nkind (Entry_Name) = N_Indexed_Component then
6219
6220 -- Case of dealing with entry family within the current tasks
6221
6222 E_Name := Prefix (Entry_Name);
6223
6224 else
6225 E_Name := Entry_Name;
6226 end if;
6227
6228 if Is_Entity_Name (E_Name) then
996ae0b0 6229
d81b4bfe
TQ
6230 -- Entry call to an entry (or entry family) in the current task. This
6231 -- is legal even though the task will deadlock. Rewrite as call to
6232 -- current task.
996ae0b0 6233
d81b4bfe
TQ
6234 -- This can also be a call to an entry in an enclosing task. If this
6235 -- is a single task, we have to retrieve its name, because the scope
6236 -- of the entry is the task type, not the object. If the enclosing
6237 -- task is a task type, the identity of the task is given by its own
6238 -- self variable.
6239
6240 -- Finally this can be a requeue on an entry of the same task or
6241 -- protected object.
996ae0b0
RK
6242
6243 S := Scope (Entity (E_Name));
6244
6245 for J in reverse 0 .. Scope_Stack.Last loop
996ae0b0
RK
6246 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6247 and then not Comes_From_Source (S)
6248 then
6249 -- S is an enclosing task or protected object. The concurrent
6250 -- declaration has been converted into a type declaration, and
6251 -- the object itself has an object declaration that follows
6252 -- the type in the same declarative part.
6253
6254 Tsk := Next_Entity (S);
996ae0b0
RK
6255 while Etype (Tsk) /= S loop
6256 Next_Entity (Tsk);
6257 end loop;
6258
6259 S := Tsk;
6260 exit;
6261
6262 elsif S = Scope_Stack.Table (J).Entity then
6263
6264 -- Call to current task. Will be transformed into call to Self
6265
6266 exit;
6267
6268 end if;
6269 end loop;
6270
6271 New_N :=
6272 Make_Selected_Component (Loc,
6273 Prefix => New_Occurrence_Of (S, Loc),
6274 Selector_Name =>
6275 New_Occurrence_Of (Entity (E_Name), Loc));
6276 Rewrite (E_Name, New_N);
6277 Analyze (E_Name);
6278
6279 elsif Nkind (Entry_Name) = N_Selected_Component
6280 and then Is_Overloaded (Prefix (Entry_Name))
6281 then
d81b4bfe
TQ
6282 -- Use the entry name (which must be unique at this point) to find
6283 -- the prefix that returns the corresponding task type or protected
6284 -- type.
996ae0b0
RK
6285
6286 declare
fbf5a39b
AC
6287 Pref : constant Node_Id := Prefix (Entry_Name);
6288 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
996ae0b0
RK
6289 I : Interp_Index;
6290 It : Interp;
996ae0b0
RK
6291
6292 begin
6293 Get_First_Interp (Pref, I, It);
996ae0b0 6294 while Present (It.Typ) loop
996ae0b0
RK
6295 if Scope (Ent) = It.Typ then
6296 Set_Etype (Pref, It.Typ);
6297 exit;
6298 end if;
6299
6300 Get_Next_Interp (I, It);
6301 end loop;
6302 end;
6303 end if;
6304
6305 if Nkind (Entry_Name) = N_Selected_Component then
fbf5a39b 6306 Resolve (Prefix (Entry_Name));
996ae0b0
RK
6307
6308 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6309 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
fbf5a39b 6310 Resolve (Prefix (Prefix (Entry_Name)));
996ae0b0
RK
6311 Index := First (Expressions (Entry_Name));
6312 Resolve (Index, Entry_Index_Type (Nam));
6313
d81b4bfe
TQ
6314 -- Up to this point the expression could have been the actual in a
6315 -- simple entry call, and be given by a named association.
996ae0b0
RK
6316
6317 if Nkind (Index) = N_Parameter_Association then
6318 Error_Msg_N ("expect expression for entry index", Index);
6319 else
6320 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6321 end if;
6322 end if;
996ae0b0
RK
6323 end Resolve_Entry;
6324
6325 ------------------------
6326 -- Resolve_Entry_Call --
6327 ------------------------
6328
6329 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6330 Entry_Name : constant Node_Id := Name (N);
6331 Loc : constant Source_Ptr := Sloc (Entry_Name);
6332 Actuals : List_Id;
6333 First_Named : Node_Id;
6334 Nam : Entity_Id;
6335 Norm_OK : Boolean;
6336 Obj : Node_Id;
6337 Was_Over : Boolean;
6338
6339 begin
d81b4bfe
TQ
6340 -- We kill all checks here, because it does not seem worth the effort to
6341 -- do anything better, an entry call is a big operation.
fbf5a39b
AC
6342
6343 Kill_All_Checks;
6344
996ae0b0
RK
6345 -- Processing of the name is similar for entry calls and protected
6346 -- operation calls. Once the entity is determined, we can complete
6347 -- the resolution of the actuals.
6348
6349 -- The selector may be overloaded, in the case of a protected object
6350 -- with overloaded functions. The type of the context is used for
6351 -- resolution.
6352
6353 if Nkind (Entry_Name) = N_Selected_Component
6354 and then Is_Overloaded (Selector_Name (Entry_Name))
6355 and then Typ /= Standard_Void_Type
6356 then
6357 declare
6358 I : Interp_Index;
6359 It : Interp;
6360
6361 begin
6362 Get_First_Interp (Selector_Name (Entry_Name), I, It);
996ae0b0 6363 while Present (It.Typ) loop
996ae0b0
RK
6364 if Covers (Typ, It.Typ) then
6365 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6366 Set_Etype (Entry_Name, It.Typ);
6367
6368 Generate_Reference (It.Typ, N, ' ');
6369 end if;
6370
6371 Get_Next_Interp (I, It);
6372 end loop;
6373 end;
6374 end if;
6375
6376 Resolve_Entry (Entry_Name);
6377
6378 if Nkind (Entry_Name) = N_Selected_Component then
6379
a77842bd 6380 -- Simple entry call
996ae0b0
RK
6381
6382 Nam := Entity (Selector_Name (Entry_Name));
6383 Obj := Prefix (Entry_Name);
6384 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6385
6386 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6387
a77842bd 6388 -- Call to member of entry family
996ae0b0
RK
6389
6390 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6391 Obj := Prefix (Prefix (Entry_Name));
6392 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6393 end if;
6394
fbf5a39b
AC
6395 -- We cannot in general check the maximum depth of protected entry
6396 -- calls at compile time. But we can tell that any protected entry
6397 -- call at all violates a specified nesting depth of zero.
6398
6399 if Is_Protected_Type (Scope (Nam)) then
9f4fd324 6400 Check_Restriction (Max_Entry_Queue_Length, N);
fbf5a39b
AC
6401 end if;
6402
996ae0b0
RK
6403 -- Use context type to disambiguate a protected function that can be
6404 -- called without actuals and that returns an array type, and where
6405 -- the argument list may be an indexing of the returned value.
6406
6407 if Ekind (Nam) = E_Function
6408 and then Needs_No_Actuals (Nam)
6409 and then Present (Parameter_Associations (N))
6410 and then
6411 ((Is_Array_Type (Etype (Nam))
6412 and then Covers (Typ, Component_Type (Etype (Nam))))
6413
6414 or else (Is_Access_Type (Etype (Nam))
6415 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6416 and then Covers (Typ,
6417 Component_Type (Designated_Type (Etype (Nam))))))
6418 then
6419 declare
6420 Index_Node : Node_Id;
6421
6422 begin
6423 Index_Node :=
6424 Make_Indexed_Component (Loc,
6425 Prefix =>
6426 Make_Function_Call (Loc,
6427 Name => Relocate_Node (Entry_Name)),
6428 Expressions => Parameter_Associations (N));
6429
6430 -- Since we are correcting a node classification error made by
6431 -- the parser, we call Replace rather than Rewrite.
6432
6433 Replace (N, Index_Node);
6434 Set_Etype (Prefix (N), Etype (Nam));
6435 Set_Etype (N, Typ);
6436 Resolve_Indexed_Component (N, Typ);
6437 return;
6438 end;
6439 end if;
6440
b7f17b20
ES
6441 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6442 and then Present (PPC_Wrapper (Nam))
6443 and then Current_Scope /= PPC_Wrapper (Nam)
6444 then
468ee96a 6445 -- Rewrite as call to the precondition wrapper, adding the task
3fd9f17c
AC
6446 -- object to the list of actuals. If the call is to a member of
6447 -- an entry family, include the index as well.
b7f17b20
ES
6448
6449 declare
468ee96a 6450 New_Call : Node_Id;
b7f17b20
ES
6451 New_Actuals : List_Id;
6452 begin
6453 New_Actuals := New_List (Obj);
3fd9f17c
AC
6454
6455 if Nkind (Entry_Name) = N_Indexed_Component then
6456 Append_To (New_Actuals,
6457 New_Copy_Tree (First (Expressions (Entry_Name))));
6458 end if;
6459
b7f17b20 6460 Append_List (Parameter_Associations (N), New_Actuals);
468ee96a
AC
6461 New_Call :=
6462 Make_Procedure_Call_Statement (Loc,
6463 Name =>
6464 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6465 Parameter_Associations => New_Actuals);
b7f17b20
ES
6466 Rewrite (N, New_Call);
6467 Analyze_And_Resolve (N);
6468 return;
6469 end;
6470 end if;
6471
996ae0b0 6472 -- The operation name may have been overloaded. Order the actuals
fbf5a39b
AC
6473 -- according to the formals of the resolved entity, and set the
6474 -- return type to that of the operation.
996ae0b0
RK
6475
6476 if Was_Over then
6477 Normalize_Actuals (N, Nam, False, Norm_OK);
6478 pragma Assert (Norm_OK);
fbf5a39b 6479 Set_Etype (N, Etype (Nam));
996ae0b0
RK
6480 end if;
6481
6482 Resolve_Actuals (N, Nam);
ae6ede77
AC
6483
6484 -- Create a call reference to the entry
6485
6486 Generate_Reference (Nam, Entry_Name, 's');
996ae0b0 6487
8a95f4e8 6488 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
996ae0b0
RK
6489 Check_Potentially_Blocking_Operation (N);
6490 end if;
6491
6492 -- Verify that a procedure call cannot masquerade as an entry
6493 -- call where an entry call is expected.
6494
6495 if Ekind (Nam) = E_Procedure then
996ae0b0
RK
6496 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6497 and then N = Entry_Call_Statement (Parent (N))
6498 then
6499 Error_Msg_N ("entry call required in select statement", N);
6500
6501 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6502 and then N = Triggering_Statement (Parent (N))
6503 then
6504 Error_Msg_N ("triggering statement cannot be procedure call", N);
6505
6506 elsif Ekind (Scope (Nam)) = E_Task_Type
6507 and then not In_Open_Scopes (Scope (Nam))
6508 then
758c442c 6509 Error_Msg_N ("task has no entry with this name", Entry_Name);
996ae0b0
RK
6510 end if;
6511 end if;
6512
d81b4bfe
TQ
6513 -- After resolution, entry calls and protected procedure calls are
6514 -- changed into entry calls, for expansion. The structure of the node
6515 -- does not change, so it can safely be done in place. Protected
6516 -- function calls must keep their structure because they are
6517 -- subexpressions.
996ae0b0
RK
6518
6519 if Ekind (Nam) /= E_Function then
6520
6521 -- A protected operation that is not a function may modify the
d81b4bfe
TQ
6522 -- corresponding object, and cannot apply to a constant. If this
6523 -- is an internal call, the prefix is the type itself.
996ae0b0
RK
6524
6525 if Is_Protected_Type (Scope (Nam))
6526 and then not Is_Variable (Obj)
6527 and then (not Is_Entity_Name (Obj)
6528 or else not Is_Type (Entity (Obj)))
6529 then
6530 Error_Msg_N
6531 ("prefix of protected procedure or entry call must be variable",
6532 Entry_Name);
6533 end if;
6534
6535 Actuals := Parameter_Associations (N);
6536 First_Named := First_Named_Actual (N);
6537
6538 Rewrite (N,
6539 Make_Entry_Call_Statement (Loc,
6540 Name => Entry_Name,
6541 Parameter_Associations => Actuals));
6542
6543 Set_First_Named_Actual (N, First_Named);
6544 Set_Analyzed (N, True);
6545
6546 -- Protected functions can return on the secondary stack, in which
1420b484 6547 -- case we must trigger the transient scope mechanism.
996ae0b0
RK
6548
6549 elsif Expander_Active
6550 and then Requires_Transient_Scope (Etype (Nam))
6551 then
0669bebe 6552 Establish_Transient_Scope (N, Sec_Stack => True);
996ae0b0 6553 end if;
996ae0b0
RK
6554 end Resolve_Entry_Call;
6555
6556 -------------------------
6557 -- Resolve_Equality_Op --
6558 -------------------------
6559
d81b4bfe
TQ
6560 -- Both arguments must have the same type, and the boolean context does
6561 -- not participate in the resolution. The first pass verifies that the
6562 -- interpretation is not ambiguous, and the type of the left argument is
6563 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6564 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6565 -- though they carry a single (universal) type. Diagnose this case here.
996ae0b0
RK
6566
6567 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6568 L : constant Node_Id := Left_Opnd (N);
6569 R : constant Node_Id := Right_Opnd (N);
6570 T : Entity_Id := Find_Unique_Type (L, R);
6571
a8930b80
AC
6572 procedure Check_Conditional_Expression (Cond : Node_Id);
6573 -- The resolution rule for conditional expressions requires that each
6574 -- such must have a unique type. This means that if several dependent
6575 -- expressions are of a non-null anonymous access type, and the context
6576 -- does not impose an expected type (as can be the case in an equality
6577 -- operation) the expression must be rejected.
6578
996ae0b0
RK
6579 function Find_Unique_Access_Type return Entity_Id;
6580 -- In the case of allocators, make a last-ditch attempt to find a single
6581 -- access type with the right designated type. This is semantically
6582 -- dubious, and of no interest to any real code, but c48008a makes it
6583 -- all worthwhile.
6584
a8930b80
AC
6585 ----------------------------------
6586 -- Check_Conditional_Expression --
6587 ----------------------------------
6588
6589 procedure Check_Conditional_Expression (Cond : Node_Id) is
6590 Then_Expr : Node_Id;
6591 Else_Expr : Node_Id;
6592
6593 begin
6594 if Nkind (Cond) = N_Conditional_Expression then
6595 Then_Expr := Next (First (Expressions (Cond)));
6596 Else_Expr := Next (Then_Expr);
6597
6598 if Nkind (Then_Expr) /= N_Null
6599 and then Nkind (Else_Expr) /= N_Null
6600 then
6601 Error_Msg_N
6602 ("cannot determine type of conditional expression", Cond);
6603 end if;
6604 end if;
6605 end Check_Conditional_Expression;
6606
996ae0b0
RK
6607 -----------------------------
6608 -- Find_Unique_Access_Type --
6609 -----------------------------
6610
6611 function Find_Unique_Access_Type return Entity_Id is
6612 Acc : Entity_Id;
6613 E : Entity_Id;
1420b484 6614 S : Entity_Id;
996ae0b0
RK
6615
6616 begin
6617 if Ekind (Etype (R)) = E_Allocator_Type then
6618 Acc := Designated_Type (Etype (R));
996ae0b0
RK
6619 elsif Ekind (Etype (L)) = E_Allocator_Type then
6620 Acc := Designated_Type (Etype (L));
996ae0b0
RK
6621 else
6622 return Empty;
6623 end if;
6624
1420b484 6625 S := Current_Scope;
996ae0b0
RK
6626 while S /= Standard_Standard loop
6627 E := First_Entity (S);
996ae0b0 6628 while Present (E) loop
996ae0b0
RK
6629 if Is_Type (E)
6630 and then Is_Access_Type (E)
6631 and then Ekind (E) /= E_Allocator_Type
6632 and then Designated_Type (E) = Base_Type (Acc)
6633 then
6634 return E;
6635 end if;
6636
6637 Next_Entity (E);
6638 end loop;
6639
6640 S := Scope (S);
6641 end loop;
6642
6643 return Empty;
6644 end Find_Unique_Access_Type;
6645
6646 -- Start of processing for Resolve_Equality_Op
6647
6648 begin
6649 Set_Etype (N, Base_Type (Typ));
6650 Generate_Reference (T, N, ' ');
6651
6652 if T = Any_Fixed then
6653 T := Unique_Fixed_Point_Type (L);
6654 end if;
6655
6656 if T /= Any_Type then
996ae0b0
RK
6657 if T = Any_String
6658 or else T = Any_Composite
6659 or else T = Any_Character
6660 then
996ae0b0
RK
6661 if T = Any_Character then
6662 Ambiguous_Character (L);
6663 else
6664 Error_Msg_N ("ambiguous operands for equality", N);
6665 end if;
6666
6667 Set_Etype (N, Any_Type);
6668 return;
6669
6670 elsif T = Any_Access
964f13da 6671 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
996ae0b0
RK
6672 then
6673 T := Find_Unique_Access_Type;
6674
6675 if No (T) then
6676 Error_Msg_N ("ambiguous operands for equality", N);
6677 Set_Etype (N, Any_Type);
6678 return;
6679 end if;
a8930b80
AC
6680
6681 -- Conditional expressions must have a single type, and if the
6682 -- context does not impose one the dependent expressions cannot
6683 -- be anonymous access types.
6684
6685 elsif Ada_Version >= Ada_2012
ae2aa109
AC
6686 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6687 E_Anonymous_Access_Subprogram_Type)
6688 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6689 E_Anonymous_Access_Subprogram_Type)
a8930b80
AC
6690 then
6691 Check_Conditional_Expression (L);
6692 Check_Conditional_Expression (R);
996ae0b0
RK
6693 end if;
6694
996ae0b0
RK
6695 Resolve (L, T);
6696 Resolve (R, T);
fbf5a39b 6697
b0186f71
AC
6698 -- In SPARK or ALFA, equality operators = and /= for array types
6699 -- other than String are only defined when, for each index position,
6700 -- the operands have equal static bounds.
6701
fe5d3068 6702 if Is_Array_Type (T)
b0186f71
AC
6703 and then Base_Type (T) /= Standard_String
6704 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6705 then
fe5d3068
YM
6706 Check_Formal_Restriction
6707 ("array types should have matching static bounds", N);
b0186f71
AC
6708 end if;
6709
0669bebe
GB
6710 -- If the unique type is a class-wide type then it will be expanded
6711 -- into a dispatching call to the predefined primitive. Therefore we
6712 -- check here for potential violation of such restriction.
6713
6714 if Is_Class_Wide_Type (T) then
6715 Check_Restriction (No_Dispatching_Calls, N);
6716 end if;
6717
fbf5a39b
AC
6718 if Warn_On_Redundant_Constructs
6719 and then Comes_From_Source (N)
6720 and then Is_Entity_Name (R)
6721 and then Entity (R) = Standard_True
6722 and then Comes_From_Source (R)
6723 then
305caf42
AC
6724 Error_Msg_N -- CODEFIX
6725 ("?comparison with True is redundant!", R);
fbf5a39b
AC
6726 end if;
6727
996ae0b0
RK
6728 Check_Unset_Reference (L);
6729 Check_Unset_Reference (R);
fbf5a39b 6730 Generate_Operator_Reference (N, T);
fad0600d 6731 Check_Low_Bound_Tested (N);
996ae0b0
RK
6732
6733 -- If this is an inequality, it may be the implicit inequality
6734 -- created for a user-defined operation, in which case the corres-
6735 -- ponding equality operation is not intrinsic, and the operation
6736 -- cannot be constant-folded. Else fold.
6737
6738 if Nkind (N) = N_Op_Eq
6739 or else Comes_From_Source (Entity (N))
6740 or else Ekind (Entity (N)) = E_Operator
6741 or else Is_Intrinsic_Subprogram
6742 (Corresponding_Equality (Entity (N)))
6743 then
6744 Eval_Relational_Op (N);
45fc7ddb 6745
996ae0b0 6746 elsif Nkind (N) = N_Op_Ne
0669bebe 6747 and then Is_Abstract_Subprogram (Entity (N))
996ae0b0
RK
6748 then
6749 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6750 end if;
758c442c 6751
d81b4bfe
TQ
6752 -- Ada 2005: If one operand is an anonymous access type, convert the
6753 -- other operand to it, to ensure that the underlying types match in
6754 -- the back-end. Same for access_to_subprogram, and the conversion
6755 -- verifies that the types are subtype conformant.
b7d1f17f 6756
d81b4bfe
TQ
6757 -- We apply the same conversion in the case one of the operands is a
6758 -- private subtype of the type of the other.
c8ef728f 6759
b7d1f17f
HK
6760 -- Why the Expander_Active test here ???
6761
4197ae1e 6762 if Expander_Active
b7d1f17f 6763 and then
964f13da
RD
6764 (Ekind_In (T, E_Anonymous_Access_Type,
6765 E_Anonymous_Access_Subprogram_Type)
b7d1f17f 6766 or else Is_Private_Type (T))
c8ef728f
ES
6767 then
6768 if Etype (L) /= T then
6769 Rewrite (L,
6770 Make_Unchecked_Type_Conversion (Sloc (L),
6771 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6772 Expression => Relocate_Node (L)));
6773 Analyze_And_Resolve (L, T);
6774 end if;
6775
6776 if (Etype (R)) /= T then
6777 Rewrite (R,
6778 Make_Unchecked_Type_Conversion (Sloc (R),
6779 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6780 Expression => Relocate_Node (R)));
6781 Analyze_And_Resolve (R, T);
6782 end if;
6783 end if;
996ae0b0
RK
6784 end if;
6785 end Resolve_Equality_Op;
6786
6787 ----------------------------------
6788 -- Resolve_Explicit_Dereference --
6789 ----------------------------------
6790
6791 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
bc5f3720
RD
6792 Loc : constant Source_Ptr := Sloc (N);
6793 New_N : Node_Id;
6794 P : constant Node_Id := Prefix (N);
6795 I : Interp_Index;
6796 It : Interp;
996ae0b0
RK
6797
6798 begin
c8ef728f 6799 Check_Fully_Declared_Prefix (Typ, P);
996ae0b0
RK
6800
6801 if Is_Overloaded (P) then
6802
758c442c
GD
6803 -- Use the context type to select the prefix that has the correct
6804 -- designated type.
996ae0b0
RK
6805
6806 Get_First_Interp (P, I, It);
6807 while Present (It.Typ) loop
6808 exit when Is_Access_Type (It.Typ)
6809 and then Covers (Typ, Designated_Type (It.Typ));
996ae0b0
RK
6810 Get_Next_Interp (I, It);
6811 end loop;
6812
bc5f3720
RD
6813 if Present (It.Typ) then
6814 Resolve (P, It.Typ);
6815 else
758c442c
GD
6816 -- If no interpretation covers the designated type of the prefix,
6817 -- this is the pathological case where not all implementations of
6818 -- the prefix allow the interpretation of the node as a call. Now
6819 -- that the expected type is known, Remove other interpretations
6820 -- from prefix, rewrite it as a call, and resolve again, so that
6821 -- the proper call node is generated.
bc5f3720
RD
6822
6823 Get_First_Interp (P, I, It);
6824 while Present (It.Typ) loop
6825 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6826 Remove_Interp (I);
6827 end if;
6828
6829 Get_Next_Interp (I, It);
6830 end loop;
6831
6832 New_N :=
6833 Make_Function_Call (Loc,
6834 Name =>
6835 Make_Explicit_Dereference (Loc,
6836 Prefix => P),
6837 Parameter_Associations => New_List);
6838
6839 Save_Interps (N, New_N);
6840 Rewrite (N, New_N);
6841 Analyze_And_Resolve (N, Typ);
6842 return;
6843 end if;
6844
996ae0b0
RK
6845 Set_Etype (N, Designated_Type (It.Typ));
6846
6847 else
fbf5a39b 6848 Resolve (P);
996ae0b0
RK
6849 end if;
6850
6851 if Is_Access_Type (Etype (P)) then
6852 Apply_Access_Check (N);
6853 end if;
6854
758c442c
GD
6855 -- If the designated type is a packed unconstrained array type, and the
6856 -- explicit dereference is not in the context of an attribute reference,
6857 -- then we must compute and set the actual subtype, since it is needed
6858 -- by Gigi. The reason we exclude the attribute case is that this is
6859 -- handled fine by Gigi, and in fact we use such attributes to build the
6860 -- actual subtype. We also exclude generated code (which builds actual
6861 -- subtypes directly if they are needed).
996ae0b0
RK
6862
6863 if Is_Array_Type (Etype (N))
6864 and then Is_Packed (Etype (N))
6865 and then not Is_Constrained (Etype (N))
6866 and then Nkind (Parent (N)) /= N_Attribute_Reference
6867 and then Comes_From_Source (N)
6868 then
6869 Set_Etype (N, Get_Actual_Subtype (N));
6870 end if;
6871
09494c32
AC
6872 -- Note: No Eval processing is required for an explicit dereference,
6873 -- because such a name can never be static.
996ae0b0
RK
6874
6875 end Resolve_Explicit_Dereference;
6876
955871d3
AC
6877 -------------------------------------
6878 -- Resolve_Expression_With_Actions --
6879 -------------------------------------
6880
6881 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6882 begin
6883 Set_Etype (N, Typ);
6884 end Resolve_Expression_With_Actions;
6885
996ae0b0
RK
6886 -------------------------------
6887 -- Resolve_Indexed_Component --
6888 -------------------------------
6889
6890 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6891 Name : constant Node_Id := Prefix (N);
6892 Expr : Node_Id;
6893 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6894 Index : Node_Id;
6895
6896 begin
6897 if Is_Overloaded (Name) then
6898
758c442c
GD
6899 -- Use the context type to select the prefix that yields the correct
6900 -- component type.
996ae0b0
RK
6901
6902 declare
6903 I : Interp_Index;
6904 It : Interp;
6905 I1 : Interp_Index := 0;
6906 P : constant Node_Id := Prefix (N);
6907 Found : Boolean := False;
6908
6909 begin
6910 Get_First_Interp (P, I, It);
996ae0b0 6911 while Present (It.Typ) loop
996ae0b0
RK
6912 if (Is_Array_Type (It.Typ)
6913 and then Covers (Typ, Component_Type (It.Typ)))
6914 or else (Is_Access_Type (It.Typ)
6915 and then Is_Array_Type (Designated_Type (It.Typ))
6916 and then Covers
6917 (Typ, Component_Type (Designated_Type (It.Typ))))
6918 then
6919 if Found then
6920 It := Disambiguate (P, I1, I, Any_Type);
6921
6922 if It = No_Interp then
6923 Error_Msg_N ("ambiguous prefix for indexing", N);
6924 Set_Etype (N, Typ);
6925 return;
6926
6927 else
6928 Found := True;
6929 Array_Type := It.Typ;
6930 I1 := I;
6931 end if;
6932
6933 else
6934 Found := True;
6935 Array_Type := It.Typ;
6936 I1 := I;
6937 end if;
6938 end if;
6939
6940 Get_Next_Interp (I, It);
6941 end loop;
6942 end;
6943
6944 else
6945 Array_Type := Etype (Name);
6946 end if;
6947
6948 Resolve (Name, Array_Type);
6949 Array_Type := Get_Actual_Subtype_If_Available (Name);
6950
6951 -- If prefix is access type, dereference to get real array type.
6952 -- Note: we do not apply an access check because the expander always
6953 -- introduces an explicit dereference, and the check will happen there.
6954
6955 if Is_Access_Type (Array_Type) then
6956 Array_Type := Designated_Type (Array_Type);
6957 end if;
6958
a77842bd 6959 -- If name was overloaded, set component type correctly now
f3d57416 6960 -- If a misplaced call to an entry family (which has no index types)
b7d1f17f 6961 -- return. Error will be diagnosed from calling context.
996ae0b0 6962
b7d1f17f
HK
6963 if Is_Array_Type (Array_Type) then
6964 Set_Etype (N, Component_Type (Array_Type));
6965 else
6966 return;
6967 end if;
996ae0b0
RK
6968
6969 Index := First_Index (Array_Type);
6970 Expr := First (Expressions (N));
6971
758c442c
GD
6972 -- The prefix may have resolved to a string literal, in which case its
6973 -- etype has a special representation. This is only possible currently
6974 -- if the prefix is a static concatenation, written in functional
6975 -- notation.
996ae0b0
RK
6976
6977 if Ekind (Array_Type) = E_String_Literal_Subtype then
6978 Resolve (Expr, Standard_Positive);
6979
6980 else
6981 while Present (Index) and Present (Expr) loop
6982 Resolve (Expr, Etype (Index));
6983 Check_Unset_Reference (Expr);
6984
6985 if Is_Scalar_Type (Etype (Expr)) then
6986 Apply_Scalar_Range_Check (Expr, Etype (Index));
6987 else
6988 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6989 end if;
6990
6991 Next_Index (Index);
6992 Next (Expr);
6993 end loop;
6994 end if;
6995
0669bebe
GB
6996 -- Do not generate the warning on suspicious index if we are analyzing
6997 -- package Ada.Tags; otherwise we will report the warning with the
6998 -- Prims_Ptr field of the dispatch table.
6999
7000 if Scope (Etype (Prefix (N))) = Standard_Standard
7001 or else not
7002 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7003 Ada_Tags)
7004 then
7005 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7006 Eval_Indexed_Component (N);
7007 end if;
c28408b7
RD
7008
7009 -- If the array type is atomic, and is packed, and we are in a left side
7010 -- context, then this is worth a warning, since we have a situation
7011 -- where the access to the component may cause extra read/writes of
7012 -- the atomic array object, which could be considered unexpected.
7013
7014 if Nkind (N) = N_Indexed_Component
7015 and then (Is_Atomic (Array_Type)
7016 or else (Is_Entity_Name (Prefix (N))
7017 and then Is_Atomic (Entity (Prefix (N)))))
7018 and then Is_Bit_Packed_Array (Array_Type)
7019 and then Is_LHS (N)
7020 then
7021 Error_Msg_N ("?assignment to component of packed atomic array",
7022 Prefix (N));
7023 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7024 Prefix (N));
7025 end if;
996ae0b0
RK
7026 end Resolve_Indexed_Component;
7027
7028 -----------------------------
7029 -- Resolve_Integer_Literal --
7030 -----------------------------
7031
7032 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7033 begin
7034 Set_Etype (N, Typ);
7035 Eval_Integer_Literal (N);
7036 end Resolve_Integer_Literal;
7037
15ce9ca2
AC
7038 --------------------------------
7039 -- Resolve_Intrinsic_Operator --
7040 --------------------------------
996ae0b0
RK
7041
7042 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
bb481772
AC
7043 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7044 Op : Entity_Id;
7045 Orig_Op : constant Entity_Id := Entity (N);
7046 Arg1 : Node_Id;
7047 Arg2 : Node_Id;
996ae0b0
RK
7048
7049 begin
305caf42
AC
7050 -- We must preserve the original entity in a generic setting, so that
7051 -- the legality of the operation can be verified in an instance.
7052
7053 if not Expander_Active then
7054 return;
7055 end if;
7056
996ae0b0 7057 Op := Entity (N);
996ae0b0
RK
7058 while Scope (Op) /= Standard_Standard loop
7059 Op := Homonym (Op);
7060 pragma Assert (Present (Op));
7061 end loop;
7062
7063 Set_Entity (N, Op);
af152989 7064 Set_Is_Overloaded (N, False);
996ae0b0 7065
758c442c
GD
7066 -- If the operand type is private, rewrite with suitable conversions on
7067 -- the operands and the result, to expose the proper underlying numeric
7068 -- type.
996ae0b0 7069
fbf5a39b
AC
7070 if Is_Private_Type (Typ) then
7071 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
7072
7073 if Nkind (N) = N_Op_Expon then
7074 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7075 else
7076 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7077 end if;
7078
bb481772
AC
7079 if Nkind (Arg1) = N_Type_Conversion then
7080 Save_Interps (Left_Opnd (N), Expression (Arg1));
7081 end if;
7082
7083 if Nkind (Arg2) = N_Type_Conversion then
7084 Save_Interps (Right_Opnd (N), Expression (Arg2));
7085 end if;
996ae0b0 7086
fbf5a39b
AC
7087 Set_Left_Opnd (N, Arg1);
7088 Set_Right_Opnd (N, Arg2);
7089
7090 Set_Etype (N, Btyp);
7091 Rewrite (N, Unchecked_Convert_To (Typ, N));
7092 Resolve (N, Typ);
7093
7094 elsif Typ /= Etype (Left_Opnd (N))
7095 or else Typ /= Etype (Right_Opnd (N))
7096 then
d81b4bfe 7097 -- Add explicit conversion where needed, and save interpretations in
bb481772
AC
7098 -- case operands are overloaded. If the context is a VMS operation,
7099 -- assert that the conversion is legal (the operands have the proper
7100 -- types to select the VMS intrinsic). Note that in rare cases the
7101 -- VMS operators may be visible, but the default System is being used
7102 -- and Address is a private type.
fbf5a39b 7103
af152989 7104 Arg1 := Convert_To (Typ, Left_Opnd (N));
fbf5a39b
AC
7105 Arg2 := Convert_To (Typ, Right_Opnd (N));
7106
7107 if Nkind (Arg1) = N_Type_Conversion then
7108 Save_Interps (Left_Opnd (N), Expression (Arg1));
bb481772
AC
7109
7110 if Is_VMS_Operator (Orig_Op) then
7111 Set_Conversion_OK (Arg1);
7112 end if;
af152989
AC
7113 else
7114 Save_Interps (Left_Opnd (N), Arg1);
fbf5a39b
AC
7115 end if;
7116
7117 if Nkind (Arg2) = N_Type_Conversion then
7118 Save_Interps (Right_Opnd (N), Expression (Arg2));
bb481772
AC
7119
7120 if Is_VMS_Operator (Orig_Op) then
7121 Set_Conversion_OK (Arg2);
7122 end if;
af152989 7123 else
0ab80019 7124 Save_Interps (Right_Opnd (N), Arg2);
fbf5a39b
AC
7125 end if;
7126
7127 Rewrite (Left_Opnd (N), Arg1);
7128 Rewrite (Right_Opnd (N), Arg2);
7129 Analyze (Arg1);
7130 Analyze (Arg2);
7131 Resolve_Arithmetic_Op (N, Typ);
7132
7133 else
7134 Resolve_Arithmetic_Op (N, Typ);
7135 end if;
996ae0b0
RK
7136 end Resolve_Intrinsic_Operator;
7137
fbf5a39b
AC
7138 --------------------------------------
7139 -- Resolve_Intrinsic_Unary_Operator --
7140 --------------------------------------
7141
7142 procedure Resolve_Intrinsic_Unary_Operator
7143 (N : Node_Id;
7144 Typ : Entity_Id)
7145 is
7146 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7147 Op : Entity_Id;
7148 Arg2 : Node_Id;
7149
7150 begin
7151 Op := Entity (N);
fbf5a39b
AC
7152 while Scope (Op) /= Standard_Standard loop
7153 Op := Homonym (Op);
7154 pragma Assert (Present (Op));
7155 end loop;
7156
7157 Set_Entity (N, Op);
7158
7159 if Is_Private_Type (Typ) then
7160 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7161 Save_Interps (Right_Opnd (N), Expression (Arg2));
7162
7163 Set_Right_Opnd (N, Arg2);
7164
7165 Set_Etype (N, Btyp);
7166 Rewrite (N, Unchecked_Convert_To (Typ, N));
7167 Resolve (N, Typ);
7168
7169 else
7170 Resolve_Unary_Op (N, Typ);
7171 end if;
7172 end Resolve_Intrinsic_Unary_Operator;
7173
996ae0b0
RK
7174 ------------------------
7175 -- Resolve_Logical_Op --
7176 ------------------------
7177
7178 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7179 B_Typ : Entity_Id;
7180
7181 begin
f61580d4
AC
7182 Check_No_Direct_Boolean_Operators (N);
7183
758c442c
GD
7184 -- Predefined operations on scalar types yield the base type. On the
7185 -- other hand, logical operations on arrays yield the type of the
7186 -- arguments (and the context).
996ae0b0
RK
7187
7188 if Is_Array_Type (Typ) then
7189 B_Typ := Typ;
7190 else
7191 B_Typ := Base_Type (Typ);
7192 end if;
7193
001c7783
AC
7194 -- OK if this is a VMS-specific intrinsic operation
7195
7196 if Is_VMS_Operator (Entity (N)) then
7197 null;
7198
996ae0b0
RK
7199 -- The following test is required because the operands of the operation
7200 -- may be literals, in which case the resulting type appears to be
7201 -- compatible with a signed integer type, when in fact it is compatible
7202 -- only with modular types. If the context itself is universal, the
7203 -- operation is illegal.
7204
001c7783 7205 elsif not Valid_Boolean_Arg (Typ) then
996ae0b0
RK
7206 Error_Msg_N ("invalid context for logical operation", N);
7207 Set_Etype (N, Any_Type);
7208 return;
7209
7210 elsif Typ = Any_Modular then
7211 Error_Msg_N
7212 ("no modular type available in this context", N);
7213 Set_Etype (N, Any_Type);
7214 return;
07fc65c4
GB
7215 elsif Is_Modular_Integer_Type (Typ)
7216 and then Etype (Left_Opnd (N)) = Universal_Integer
7217 and then Etype (Right_Opnd (N)) = Universal_Integer
7218 then
7219 Check_For_Visible_Operator (N, B_Typ);
996ae0b0
RK
7220 end if;
7221
7222 Resolve (Left_Opnd (N), B_Typ);
7223 Resolve (Right_Opnd (N), B_Typ);
7224
7225 Check_Unset_Reference (Left_Opnd (N));
7226 Check_Unset_Reference (Right_Opnd (N));
7227
7228 Set_Etype (N, B_Typ);
fbf5a39b 7229 Generate_Operator_Reference (N, B_Typ);
996ae0b0 7230 Eval_Logical_Op (N);
9f90d123
AC
7231
7232 -- In SPARK or ALFA, logical operations AND, OR and XOR for arrays are
7233 -- defined only when both operands have same static lower and higher
7234 -- bounds.
7235
fe5d3068 7236 if Is_Array_Type (B_Typ)
b0186f71
AC
7237 and then not Matching_Static_Array_Bounds (Etype (Left_Opnd (N)),
7238 Etype (Right_Opnd (N)))
9f90d123 7239 then
fe5d3068
YM
7240 Check_Formal_Restriction
7241 ("array types should have matching static bounds", N);
9f90d123
AC
7242 end if;
7243
996ae0b0
RK
7244 end Resolve_Logical_Op;
7245
7246 ---------------------------
7247 -- Resolve_Membership_Op --
7248 ---------------------------
7249
7250 -- The context can only be a boolean type, and does not determine
7251 -- the arguments. Arguments should be unambiguous, but the preference
7252 -- rule for universal types applies.
7253
7254 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
07fc65c4
GB
7255 pragma Warnings (Off, Typ);
7256
197e4514 7257 L : constant Node_Id := Left_Opnd (N);
b1c11e0e 7258 R : constant Node_Id := Right_Opnd (N);
996ae0b0
RK
7259 T : Entity_Id;
7260
197e4514
AC
7261 procedure Resolve_Set_Membership;
7262 -- Analysis has determined a unique type for the left operand.
7263 -- Use it to resolve the disjuncts.
7264
7265 ----------------------------
7266 -- Resolve_Set_Membership --
7267 ----------------------------
7268
7269 procedure Resolve_Set_Membership is
7270 Alt : Node_Id;
7271
7272 begin
7273 Resolve (L, Etype (L));
7274
7275 Alt := First (Alternatives (N));
7276 while Present (Alt) loop
7277
7278 -- Alternative is an expression, a range
7279 -- or a subtype mark.
7280
7281 if not Is_Entity_Name (Alt)
7282 or else not Is_Type (Entity (Alt))
7283 then
7284 Resolve (Alt, Etype (L));
7285 end if;
7286
7287 Next (Alt);
7288 end loop;
7289 end Resolve_Set_Membership;
7290
442c0581 7291 -- Start of processing for Resolve_Membership_Op
197e4514 7292
996ae0b0
RK
7293 begin
7294 if L = Error or else R = Error then
7295 return;
7296 end if;
7297
197e4514
AC
7298 if Present (Alternatives (N)) then
7299 Resolve_Set_Membership;
7300 return;
7301
7302 elsif not Is_Overloaded (R)
996ae0b0
RK
7303 and then
7304 (Etype (R) = Universal_Integer or else
7305 Etype (R) = Universal_Real)
7306 and then Is_Overloaded (L)
7307 then
7308 T := Etype (R);
1420b484 7309
d81b4bfe 7310 -- Ada 2005 (AI-251): Support the following case:
1420b484
JM
7311
7312 -- type I is interface;
7313 -- type T is tagged ...
7314
c8ef728f 7315 -- function Test (O : I'Class) is
1420b484
JM
7316 -- begin
7317 -- return O in T'Class.
7318 -- end Test;
7319
d81b4bfe 7320 -- In this case we have nothing else to do. The membership test will be
e7c0dd39 7321 -- done at run time.
1420b484 7322
0791fbe9 7323 elsif Ada_Version >= Ada_2005
1420b484
JM
7324 and then Is_Class_Wide_Type (Etype (L))
7325 and then Is_Interface (Etype (L))
7326 and then Is_Class_Wide_Type (Etype (R))
7327 and then not Is_Interface (Etype (R))
7328 then
7329 return;
7330
996ae0b0
RK
7331 else
7332 T := Intersect_Types (L, R);
7333 end if;
7334
9a0ddeee
AC
7335 -- If mixed-mode operations are present and operands are all literal,
7336 -- the only interpretation involves Duration, which is probably not
7337 -- the intention of the programmer.
7338
7339 if T = Any_Fixed then
7340 T := Unique_Fixed_Point_Type (N);
7341
7342 if T = Any_Type then
7343 return;
7344 end if;
7345 end if;
7346
996ae0b0
RK
7347 Resolve (L, T);
7348 Check_Unset_Reference (L);
7349
7350 if Nkind (R) = N_Range
7351 and then not Is_Scalar_Type (T)
7352 then
7353 Error_Msg_N ("scalar type required for range", R);
7354 end if;
7355
7356 if Is_Entity_Name (R) then
7357 Freeze_Expression (R);
7358 else
7359 Resolve (R, T);
7360 Check_Unset_Reference (R);
7361 end if;
7362
7363 Eval_Membership_Op (N);
7364 end Resolve_Membership_Op;
7365
7366 ------------------
7367 -- Resolve_Null --
7368 ------------------
7369
7370 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
b1c11e0e
JM
7371 Loc : constant Source_Ptr := Sloc (N);
7372
996ae0b0 7373 begin
758c442c 7374 -- Handle restriction against anonymous null access values This
6ba6b1e3 7375 -- restriction can be turned off using -gnatdj.
996ae0b0 7376
0ab80019 7377 -- Ada 2005 (AI-231): Remove restriction
2820d220 7378
0791fbe9 7379 if Ada_Version < Ada_2005
2820d220 7380 and then not Debug_Flag_J
996ae0b0
RK
7381 and then Ekind (Typ) = E_Anonymous_Access_Type
7382 and then Comes_From_Source (N)
7383 then
d81b4bfe
TQ
7384 -- In the common case of a call which uses an explicitly null value
7385 -- for an access parameter, give specialized error message.
996ae0b0 7386
45fc7ddb
HK
7387 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7388 N_Function_Call)
996ae0b0
RK
7389 then
7390 Error_Msg_N
7391 ("null is not allowed as argument for an access parameter", N);
7392
7393 -- Standard message for all other cases (are there any?)
7394
7395 else
7396 Error_Msg_N
7397 ("null cannot be of an anonymous access type", N);
7398 end if;
7399 end if;
7400
b1c11e0e
JM
7401 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7402 -- assignment to a null-excluding object
7403
0791fbe9 7404 if Ada_Version >= Ada_2005
b1c11e0e
JM
7405 and then Can_Never_Be_Null (Typ)
7406 and then Nkind (Parent (N)) = N_Assignment_Statement
7407 then
7408 if not Inside_Init_Proc then
7409 Insert_Action
7410 (Compile_Time_Constraint_Error (N,
7411 "(Ada 2005) null not allowed in null-excluding objects?"),
7412 Make_Raise_Constraint_Error (Loc,
7413 Reason => CE_Access_Check_Failed));
7414 else
7415 Insert_Action (N,
7416 Make_Raise_Constraint_Error (Loc,
7417 Reason => CE_Access_Check_Failed));
7418 end if;
7419 end if;
7420
d81b4bfe
TQ
7421 -- In a distributed context, null for a remote access to subprogram may
7422 -- need to be replaced with a special record aggregate. In this case,
7423 -- return after having done the transformation.
996ae0b0
RK
7424
7425 if (Ekind (Typ) = E_Record_Type
7426 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7427 and then Remote_AST_Null_Value (N, Typ)
7428 then
7429 return;
7430 end if;
7431
a77842bd 7432 -- The null literal takes its type from the context
996ae0b0
RK
7433
7434 Set_Etype (N, Typ);
7435 end Resolve_Null;
7436
7437 -----------------------
7438 -- Resolve_Op_Concat --
7439 -----------------------
7440
7441 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
996ae0b0 7442
10303118
BD
7443 -- We wish to avoid deep recursion, because concatenations are often
7444 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7445 -- operands nonrecursively until we find something that is not a simple
7446 -- concatenation (A in this case). We resolve that, and then walk back
7447 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7448 -- to do the rest of the work at each level. The Parent pointers allow
7449 -- us to avoid recursion, and thus avoid running out of memory. See also
d81b4bfe 7450 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
996ae0b0 7451
10303118
BD
7452 NN : Node_Id := N;
7453 Op1 : Node_Id;
996ae0b0 7454
10303118
BD
7455 begin
7456 -- The following code is equivalent to:
996ae0b0 7457
10303118
BD
7458 -- Resolve_Op_Concat_First (NN, Typ);
7459 -- Resolve_Op_Concat_Arg (N, ...);
7460 -- Resolve_Op_Concat_Rest (N, Typ);
996ae0b0 7461
10303118
BD
7462 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7463 -- operand is a concatenation.
996ae0b0 7464
10303118 7465 -- Walk down left operands
996ae0b0 7466
10303118
BD
7467 loop
7468 Resolve_Op_Concat_First (NN, Typ);
7469 Op1 := Left_Opnd (NN);
7470 exit when not (Nkind (Op1) = N_Op_Concat
7471 and then not Is_Array_Type (Component_Type (Typ))
7472 and then Entity (Op1) = Entity (NN));
7473 NN := Op1;
7474 end loop;
996ae0b0 7475
10303118 7476 -- Now (given the above example) NN is A&B and Op1 is A
996ae0b0 7477
10303118 7478 -- First resolve Op1 ...
9ebe3743 7479
10303118 7480 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
9ebe3743 7481
10303118
BD
7482 -- ... then walk NN back up until we reach N (where we started), calling
7483 -- Resolve_Op_Concat_Rest along the way.
9ebe3743 7484
10303118
BD
7485 loop
7486 Resolve_Op_Concat_Rest (NN, Typ);
7487 exit when NN = N;
7488 NN := Parent (NN);
7489 end loop;
2933b16c 7490
fe5d3068
YM
7491 if Base_Type (Etype (N)) /= Standard_String then
7492 Check_Formal_Restriction
7493 ("result of concatenation should have type String", N);
2933b16c 7494 end if;
10303118 7495 end Resolve_Op_Concat;
9ebe3743 7496
10303118
BD
7497 ---------------------------
7498 -- Resolve_Op_Concat_Arg --
7499 ---------------------------
996ae0b0 7500
10303118
BD
7501 procedure Resolve_Op_Concat_Arg
7502 (N : Node_Id;
7503 Arg : Node_Id;
7504 Typ : Entity_Id;
7505 Is_Comp : Boolean)
7506 is
7507 Btyp : constant Entity_Id := Base_Type (Typ);
996ae0b0 7508
10303118
BD
7509 begin
7510 if In_Instance then
7511 if Is_Comp
7512 or else (not Is_Overloaded (Arg)
7513 and then Etype (Arg) /= Any_Composite
7514 and then Covers (Component_Type (Typ), Etype (Arg)))
7515 then
7516 Resolve (Arg, Component_Type (Typ));
7517 else
7518 Resolve (Arg, Btyp);
7519 end if;
fbf5a39b 7520
10303118
BD
7521 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7522 if Nkind (Arg) = N_Aggregate
7523 and then Is_Composite_Type (Component_Type (Typ))
7524 then
7525 if Is_Private_Type (Component_Type (Typ)) then
7526 Resolve (Arg, Btyp);
7527 else
7528 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7529 Set_Etype (Arg, Any_Type);
996ae0b0
RK
7530 end if;
7531
7532 else
10303118
BD
7533 if Is_Overloaded (Arg)
7534 and then Has_Compatible_Type (Arg, Typ)
7535 and then Etype (Arg) /= Any_Type
7536 then
7537 declare
7538 I : Interp_Index;
7539 It : Interp;
7540 Func : Entity_Id;
7541
7542 begin
7543 Get_First_Interp (Arg, I, It);
7544 Func := It.Nam;
7545 Get_Next_Interp (I, It);
7546
7547 -- Special-case the error message when the overloading is
7548 -- caused by a function that yields an array and can be
7549 -- called without parameters.
7550
7551 if It.Nam = Func then
7552 Error_Msg_Sloc := Sloc (Func);
7553 Error_Msg_N ("ambiguous call to function#", Arg);
7554 Error_Msg_NE
7555 ("\\interpretation as call yields&", Arg, Typ);
7556 Error_Msg_NE
7557 ("\\interpretation as indexing of call yields&",
7558 Arg, Component_Type (Typ));
7559
7560 else
7561 Error_Msg_N
7562 ("ambiguous operand for concatenation!", Arg);
7563 Get_First_Interp (Arg, I, It);
7564 while Present (It.Nam) loop
7565 Error_Msg_Sloc := Sloc (It.Nam);
7566
7567 if Base_Type (It.Typ) = Base_Type (Typ)
7568 or else Base_Type (It.Typ) =
7569 Base_Type (Component_Type (Typ))
7570 then
4e7a4f6e
AC
7571 Error_Msg_N -- CODEFIX
7572 ("\\possible interpretation#", Arg);
10303118
BD
7573 end if;
7574
7575 Get_Next_Interp (I, It);
7576 end loop;
7577 end if;
7578 end;
7579 end if;
7580
7581 Resolve (Arg, Component_Type (Typ));
7582
7583 if Nkind (Arg) = N_String_Literal then
7584 Set_Etype (Arg, Component_Type (Typ));
7585 end if;
7586
7587 if Arg = Left_Opnd (N) then
7588 Set_Is_Component_Left_Opnd (N);
7589 else
7590 Set_Is_Component_Right_Opnd (N);
7591 end if;
996ae0b0
RK
7592 end if;
7593
10303118
BD
7594 else
7595 Resolve (Arg, Btyp);
7596 end if;
7597
2933b16c
RD
7598 -- Concatenation is restricted in SPARK or ALFA: each operand must be
7599 -- either a string literal, a static character expression, or another
7600 -- concatenation. Arg cannot be a concatenation here as callers of
7601 -- Resolve_Op_Concat_Arg call it separately on each final operand, past
7602 -- concatenation operations.
7603
fe5d3068
YM
7604 if Is_Character_Type (Etype (Arg)) then
7605 if not Is_Static_Expression (Arg) then
7606 Check_Formal_Restriction
7607 ("character operand for concatenation should be static", N);
7608 end if;
2933b16c 7609
fe5d3068
YM
7610 elsif Is_String_Type (Etype (Arg)) then
7611 if Nkind (Arg) /= N_String_Literal then
7612 Check_Formal_Restriction
7613 ("string operand for concatenation should be a literal", N);
7614 end if;
2933b16c 7615
b9e48541
AC
7616 -- Do not issue error on an operand that is neither a character nor a
7617 -- string, as the error is issued in Resolve_Op_Concat.
2933b16c 7618
fe5d3068
YM
7619 else
7620 null;
2933b16c
RD
7621 end if;
7622
10303118
BD
7623 Check_Unset_Reference (Arg);
7624 end Resolve_Op_Concat_Arg;
996ae0b0 7625
10303118
BD
7626 -----------------------------
7627 -- Resolve_Op_Concat_First --
7628 -----------------------------
7629
7630 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7631 Btyp : constant Entity_Id := Base_Type (Typ);
7632 Op1 : constant Node_Id := Left_Opnd (N);
7633 Op2 : constant Node_Id := Right_Opnd (N);
996ae0b0
RK
7634
7635 begin
dae2b8ea
HK
7636 -- The parser folds an enormous sequence of concatenations of string
7637 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
4fc26524 7638 -- in the right operand. If the expression resolves to a predefined "&"
dae2b8ea
HK
7639 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7640 -- we give an error. See P_Simple_Expression in Par.Ch4.
7641
7642 if Nkind (Op2) = N_String_Literal
7643 and then Is_Folded_In_Parser (Op2)
7644 and then Ekind (Entity (N)) = E_Function
7645 then
7646 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7647 and then String_Length (Strval (Op1)) = 0);
7648 Error_Msg_N ("too many user-defined concatenations", N);
7649 return;
7650 end if;
7651
996ae0b0
RK
7652 Set_Etype (N, Btyp);
7653
7654 if Is_Limited_Composite (Btyp) then
7655 Error_Msg_N ("concatenation not available for limited array", N);
fbf5a39b 7656 Explain_Limited_Type (Btyp, N);
996ae0b0 7657 end if;
10303118 7658 end Resolve_Op_Concat_First;
996ae0b0 7659
10303118
BD
7660 ----------------------------
7661 -- Resolve_Op_Concat_Rest --
7662 ----------------------------
996ae0b0 7663
10303118
BD
7664 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7665 Op1 : constant Node_Id := Left_Opnd (N);
7666 Op2 : constant Node_Id := Right_Opnd (N);
996ae0b0 7667
10303118
BD
7668 begin
7669 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
996ae0b0 7670
fbf5a39b 7671 Generate_Operator_Reference (N, Typ);
996ae0b0
RK
7672
7673 if Is_String_Type (Typ) then
7674 Eval_Concatenation (N);
7675 end if;
7676
d81b4bfe
TQ
7677 -- If this is not a static concatenation, but the result is a string
7678 -- type (and not an array of strings) ensure that static string operands
7679 -- have their subtypes properly constructed.
996ae0b0
RK
7680
7681 if Nkind (N) /= N_String_Literal
7682 and then Is_Character_Type (Component_Type (Typ))
7683 then
7684 Set_String_Literal_Subtype (Op1, Typ);
7685 Set_String_Literal_Subtype (Op2, Typ);
7686 end if;
10303118 7687 end Resolve_Op_Concat_Rest;
996ae0b0
RK
7688
7689 ----------------------
7690 -- Resolve_Op_Expon --
7691 ----------------------
7692
7693 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7694 B_Typ : constant Entity_Id := Base_Type (Typ);
7695
7696 begin
f3d57416 7697 -- Catch attempts to do fixed-point exponentiation with universal
758c442c
GD
7698 -- operands, which is a case where the illegality is not caught during
7699 -- normal operator analysis.
996ae0b0
RK
7700
7701 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7702 Error_Msg_N ("exponentiation not available for fixed point", N);
7703 return;
7704 end if;
7705
fbf5a39b
AC
7706 if Comes_From_Source (N)
7707 and then Ekind (Entity (N)) = E_Function
7708 and then Is_Imported (Entity (N))
7709 and then Is_Intrinsic_Subprogram (Entity (N))
7710 then
7711 Resolve_Intrinsic_Operator (N, Typ);
7712 return;
7713 end if;
7714
996ae0b0
RK
7715 if Etype (Left_Opnd (N)) = Universal_Integer
7716 or else Etype (Left_Opnd (N)) = Universal_Real
7717 then
7718 Check_For_Visible_Operator (N, B_Typ);
7719 end if;
7720
7721 -- We do the resolution using the base type, because intermediate values
7722 -- in expressions always are of the base type, not a subtype of it.
7723
7724 Resolve (Left_Opnd (N), B_Typ);
7725 Resolve (Right_Opnd (N), Standard_Integer);
7726
7727 Check_Unset_Reference (Left_Opnd (N));
7728 Check_Unset_Reference (Right_Opnd (N));
7729
7730 Set_Etype (N, B_Typ);
fbf5a39b 7731 Generate_Operator_Reference (N, B_Typ);
996ae0b0
RK
7732 Eval_Op_Expon (N);
7733
7734 -- Set overflow checking bit. Much cleverer code needed here eventually
7735 -- and perhaps the Resolve routines should be separated for the various
7736 -- arithmetic operations, since they will need different processing. ???
7737
7738 if Nkind (N) in N_Op then
7739 if not Overflow_Checks_Suppressed (Etype (N)) then
fbf5a39b 7740 Enable_Overflow_Check (N);
996ae0b0
RK
7741 end if;
7742 end if;
996ae0b0
RK
7743 end Resolve_Op_Expon;
7744
7745 --------------------
7746 -- Resolve_Op_Not --
7747 --------------------
7748
7749 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7750 B_Typ : Entity_Id;
7751
7752 function Parent_Is_Boolean return Boolean;
7753 -- This function determines if the parent node is a boolean operator
7754 -- or operation (comparison op, membership test, or short circuit form)
7755 -- and the not in question is the left operand of this operation.
7756 -- Note that if the not is in parens, then false is returned.
7757
aa180613
RD
7758 -----------------------
7759 -- Parent_Is_Boolean --
7760 -----------------------
7761
996ae0b0
RK
7762 function Parent_Is_Boolean return Boolean is
7763 begin
7764 if Paren_Count (N) /= 0 then
7765 return False;
7766
7767 else
7768 case Nkind (Parent (N)) is
7769 when N_Op_And |
7770 N_Op_Eq |
7771 N_Op_Ge |
7772 N_Op_Gt |
7773 N_Op_Le |
7774 N_Op_Lt |
7775 N_Op_Ne |
7776 N_Op_Or |
7777 N_Op_Xor |
7778 N_In |
7779 N_Not_In |
7780 N_And_Then |
aa180613 7781 N_Or_Else =>
996ae0b0
RK
7782
7783 return Left_Opnd (Parent (N)) = N;
7784
7785 when others =>
7786 return False;
7787 end case;
7788 end if;
7789 end Parent_Is_Boolean;
7790
7791 -- Start of processing for Resolve_Op_Not
7792
7793 begin
758c442c
GD
7794 -- Predefined operations on scalar types yield the base type. On the
7795 -- other hand, logical operations on arrays yield the type of the
7796 -- arguments (and the context).
996ae0b0
RK
7797
7798 if Is_Array_Type (Typ) then
7799 B_Typ := Typ;
7800 else
7801 B_Typ := Base_Type (Typ);
7802 end if;
7803
001c7783
AC
7804 if Is_VMS_Operator (Entity (N)) then
7805 null;
7806
f3d57416 7807 -- Straightforward case of incorrect arguments
aa180613 7808
001c7783 7809 elsif not Valid_Boolean_Arg (Typ) then
996ae0b0
RK
7810 Error_Msg_N ("invalid operand type for operator&", N);
7811 Set_Etype (N, Any_Type);
7812 return;
7813
aa180613
RD
7814 -- Special case of probable missing parens
7815
fbf5a39b 7816 elsif Typ = Universal_Integer or else Typ = Any_Modular then
996ae0b0 7817 if Parent_Is_Boolean then
ed2233dc 7818 Error_Msg_N
996ae0b0
RK
7819 ("operand of not must be enclosed in parentheses",
7820 Right_Opnd (N));
7821 else
7822 Error_Msg_N
7823 ("no modular type available in this context", N);
7824 end if;
7825
7826 Set_Etype (N, Any_Type);
7827 return;
7828
aa180613
RD
7829 -- OK resolution of not
7830
996ae0b0 7831 else
aa180613
RD
7832 -- Warn if non-boolean types involved. This is a case like not a < b
7833 -- where a and b are modular, where we will get (not a) < b and most
7834 -- likely not (a < b) was intended.
7835
7836 if Warn_On_Questionable_Missing_Parens
7837 and then not Is_Boolean_Type (Typ)
996ae0b0
RK
7838 and then Parent_Is_Boolean
7839 then
ed2233dc 7840 Error_Msg_N ("?not expression should be parenthesized here!", N);
996ae0b0
RK
7841 end if;
7842
09bc9ab6
RD
7843 -- Warn on double negation if checking redundant constructs
7844
7845 if Warn_On_Redundant_Constructs
7846 and then Comes_From_Source (N)
7847 and then Comes_From_Source (Right_Opnd (N))
7848 and then Root_Type (Typ) = Standard_Boolean
7849 and then Nkind (Right_Opnd (N)) = N_Op_Not
7850 then
ed2233dc 7851 Error_Msg_N ("redundant double negation?", N);
09bc9ab6
RD
7852 end if;
7853
7854 -- Complete resolution and evaluation of NOT
7855
996ae0b0
RK
7856 Resolve (Right_Opnd (N), B_Typ);
7857 Check_Unset_Reference (Right_Opnd (N));
7858 Set_Etype (N, B_Typ);
fbf5a39b 7859 Generate_Operator_Reference (N, B_Typ);
996ae0b0
RK
7860 Eval_Op_Not (N);
7861 end if;
7862 end Resolve_Op_Not;
7863
7864 -----------------------------
7865 -- Resolve_Operator_Symbol --
7866 -----------------------------
7867
7868 -- Nothing to be done, all resolved already
7869
7870 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
07fc65c4
GB
7871 pragma Warnings (Off, N);
7872 pragma Warnings (Off, Typ);
7873
996ae0b0
RK
7874 begin
7875 null;
7876 end Resolve_Operator_Symbol;
7877
7878 ----------------------------------
7879 -- Resolve_Qualified_Expression --
7880 ----------------------------------
7881
7882 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
07fc65c4
GB
7883 pragma Warnings (Off, Typ);
7884
996ae0b0
RK
7885 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7886 Expr : constant Node_Id := Expression (N);
7887
7888 begin
7889 Resolve (Expr, Target_Typ);
7890
fe5d3068 7891 if Is_Array_Type (Target_Typ)
b0186f71
AC
7892 and then Is_Array_Type (Etype (Expr))
7893 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
7894 then
fe5d3068
YM
7895 Check_Formal_Restriction
7896 ("array types should have matching static bounds", N);
b0186f71
AC
7897 end if;
7898
996ae0b0 7899 -- A qualified expression requires an exact match of the type,
1420b484
JM
7900 -- class-wide matching is not allowed. However, if the qualifying
7901 -- type is specific and the expression has a class-wide type, it
7902 -- may still be okay, since it can be the result of the expansion
7903 -- of a call to a dispatching function, so we also have to check
7904 -- class-wideness of the type of the expression's original node.
7905
7906 if (Is_Class_Wide_Type (Target_Typ)
7907 or else
7908 (Is_Class_Wide_Type (Etype (Expr))
7909 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
996ae0b0
RK
7910 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7911 then
7912 Wrong_Type (Expr, Target_Typ);
7913 end if;
7914
90c63b09
AC
7915 -- If the target type is unconstrained, then we reset the type of the
7916 -- result from the type of the expression. For other cases, the actual
7917 -- subtype of the expression is the target type.
996ae0b0
RK
7918
7919 if Is_Composite_Type (Target_Typ)
7920 and then not Is_Constrained (Target_Typ)
7921 then
7922 Set_Etype (N, Etype (Expr));
7923 end if;
7924
7925 Eval_Qualified_Expression (N);
7926 end Resolve_Qualified_Expression;
7927
a961aa79
AC
7928 -----------------------------------
7929 -- Resolve_Quantified_Expression --
7930 -----------------------------------
7931
7932 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
7933 begin
7934 -- The loop structure is already resolved during its analysis, only the
0592046e
AC
7935 -- resolution of the condition needs to be done. Expansion is disabled
7936 -- so that checks and other generated code are inserted in the tree
7937 -- after expression has been rewritten as a loop.
a961aa79 7938
0592046e 7939 Expander_Mode_Save_And_Set (False);
a961aa79 7940 Resolve (Condition (N), Typ);
0592046e 7941 Expander_Mode_Restore;
a961aa79
AC
7942 end Resolve_Quantified_Expression;
7943
996ae0b0
RK
7944 -------------------
7945 -- Resolve_Range --
7946 -------------------
7947
7948 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7949 L : constant Node_Id := Low_Bound (N);
7950 H : constant Node_Id := High_Bound (N);
7951
bd29d519
AC
7952 function First_Last_Ref return Boolean;
7953 -- Returns True if N is of the form X'First .. X'Last where X is the
7954 -- same entity for both attributes.
7955
7956 --------------------
7957 -- First_Last_Ref --
7958 --------------------
7959
7960 function First_Last_Ref return Boolean is
7961 Lorig : constant Node_Id := Original_Node (L);
7962 Horig : constant Node_Id := Original_Node (H);
7963
7964 begin
7965 if Nkind (Lorig) = N_Attribute_Reference
7966 and then Nkind (Horig) = N_Attribute_Reference
7967 and then Attribute_Name (Lorig) = Name_First
7968 and then Attribute_Name (Horig) = Name_Last
7969 then
7970 declare
7971 PL : constant Node_Id := Prefix (Lorig);
7972 PH : constant Node_Id := Prefix (Horig);
7973 begin
7974 if Is_Entity_Name (PL)
7975 and then Is_Entity_Name (PH)
7976 and then Entity (PL) = Entity (PH)
7977 then
7978 return True;
7979 end if;
7980 end;
7981 end if;
7982
7983 return False;
7984 end First_Last_Ref;
7985
7986 -- Start of processing for Resolve_Range
7987
996ae0b0
RK
7988 begin
7989 Set_Etype (N, Typ);
7990 Resolve (L, Typ);
7991 Resolve (H, Typ);
7992
bd29d519
AC
7993 -- Check for inappropriate range on unordered enumeration type
7994
7995 if Bad_Unordered_Enumeration_Reference (N, Typ)
7996
7997 -- Exclude X'First .. X'Last if X is the same entity for both
7998
7999 and then not First_Last_Ref
8000 then
8001 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
498d1b80
AC
8002 end if;
8003
996ae0b0
RK
8004 Check_Unset_Reference (L);
8005 Check_Unset_Reference (H);
8006
8007 -- We have to check the bounds for being within the base range as
758c442c
GD
8008 -- required for a non-static context. Normally this is automatic and
8009 -- done as part of evaluating expressions, but the N_Range node is an
8010 -- exception, since in GNAT we consider this node to be a subexpression,
8011 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8012 -- this, but that would put the test on the main evaluation path for
8013 -- expressions.
996ae0b0
RK
8014
8015 Check_Non_Static_Context (L);
8016 Check_Non_Static_Context (H);
8017
b7d1f17f
HK
8018 -- Check for an ambiguous range over character literals. This will
8019 -- happen with a membership test involving only literals.
8020
8021 if Typ = Any_Character then
8022 Ambiguous_Character (L);
8023 Set_Etype (N, Any_Type);
8024 return;
8025 end if;
8026
fbf5a39b
AC
8027 -- If bounds are static, constant-fold them, so size computations
8028 -- are identical between front-end and back-end. Do not perform this
8029 -- transformation while analyzing generic units, as type information
8030 -- would then be lost when reanalyzing the constant node in the
8031 -- instance.
8032
8033 if Is_Discrete_Type (Typ) and then Expander_Active then
8034 if Is_OK_Static_Expression (L) then
8035 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8036 end if;
8037
8038 if Is_OK_Static_Expression (H) then
8039 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8040 end if;
8041 end if;
996ae0b0
RK
8042 end Resolve_Range;
8043
8044 --------------------------
8045 -- Resolve_Real_Literal --
8046 --------------------------
8047
8048 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8049 Actual_Typ : constant Entity_Id := Etype (N);
8050
8051 begin
8052 -- Special processing for fixed-point literals to make sure that the
8053 -- value is an exact multiple of small where this is required. We
8054 -- skip this for the universal real case, and also for generic types.
8055
8056 if Is_Fixed_Point_Type (Typ)
8057 and then Typ /= Universal_Fixed
8058 and then Typ /= Any_Fixed
8059 and then not Is_Generic_Type (Typ)
8060 then
8061 declare
8062 Val : constant Ureal := Realval (N);
8063 Cintr : constant Ureal := Val / Small_Value (Typ);
8064 Cint : constant Uint := UR_Trunc (Cintr);
8065 Den : constant Uint := Norm_Den (Cintr);
8066 Stat : Boolean;
8067
8068 begin
8069 -- Case of literal is not an exact multiple of the Small
8070
8071 if Den /= 1 then
8072
8073 -- For a source program literal for a decimal fixed-point
8074 -- type, this is statically illegal (RM 4.9(36)).
8075
8076 if Is_Decimal_Fixed_Point_Type (Typ)
8077 and then Actual_Typ = Universal_Real
8078 and then Comes_From_Source (N)
8079 then
8080 Error_Msg_N ("value has extraneous low order digits", N);
8081 end if;
8082
bc5f3720
RD
8083 -- Generate a warning if literal from source
8084
8085 if Is_Static_Expression (N)
8086 and then Warn_On_Bad_Fixed_Value
8087 then
8088 Error_Msg_N
aa5147f0 8089 ("?static fixed-point value is not a multiple of Small!",
bc5f3720
RD
8090 N);
8091 end if;
8092
996ae0b0
RK
8093 -- Replace literal by a value that is the exact representation
8094 -- of a value of the type, i.e. a multiple of the small value,
8095 -- by truncation, since Machine_Rounds is false for all GNAT
8096 -- fixed-point types (RM 4.9(38)).
8097
8098 Stat := Is_Static_Expression (N);
8099 Rewrite (N,
8100 Make_Real_Literal (Sloc (N),
8101 Realval => Small_Value (Typ) * Cint));
8102
8103 Set_Is_Static_Expression (N, Stat);
8104 end if;
8105
8106 -- In all cases, set the corresponding integer field
8107
8108 Set_Corresponding_Integer_Value (N, Cint);
8109 end;
8110 end if;
8111
8112 -- Now replace the actual type by the expected type as usual
8113
8114 Set_Etype (N, Typ);
8115 Eval_Real_Literal (N);
8116 end Resolve_Real_Literal;
8117
8118 -----------------------
8119 -- Resolve_Reference --
8120 -----------------------
8121
8122 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8123 P : constant Node_Id := Prefix (N);
8124
8125 begin
8126 -- Replace general access with specific type
8127
8128 if Ekind (Etype (N)) = E_Allocator_Type then
8129 Set_Etype (N, Base_Type (Typ));
8130 end if;
8131
8132 Resolve (P, Designated_Type (Etype (N)));
8133
8134 -- If we are taking the reference of a volatile entity, then treat
8135 -- it as a potential modification of this entity. This is much too
638e383e 8136 -- conservative, but is necessary because remove side effects can
996ae0b0
RK
8137 -- result in transformations of normal assignments into reference
8138 -- sequences that otherwise fail to notice the modification.
8139
fbf5a39b 8140 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
45fc7ddb 8141 Note_Possible_Modification (P, Sure => False);
996ae0b0
RK
8142 end if;
8143 end Resolve_Reference;
8144
8145 --------------------------------
8146 -- Resolve_Selected_Component --
8147 --------------------------------
8148
8149 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8150 Comp : Entity_Id;
8151 Comp1 : Entity_Id := Empty; -- prevent junk warning
8152 P : constant Node_Id := Prefix (N);
8153 S : constant Node_Id := Selector_Name (N);
8154 T : Entity_Id := Etype (P);
8155 I : Interp_Index;
8156 I1 : Interp_Index := 0; -- prevent junk warning
8157 It : Interp;
8158 It1 : Interp;
8159 Found : Boolean;
8160
6510f4c9
GB
8161 function Init_Component return Boolean;
8162 -- Check whether this is the initialization of a component within an
fbf5a39b 8163 -- init proc (by assignment or call to another init proc). If true,
6510f4c9
GB
8164 -- there is no need for a discriminant check.
8165
8166 --------------------
8167 -- Init_Component --
8168 --------------------
8169
8170 function Init_Component return Boolean is
8171 begin
8172 return Inside_Init_Proc
8173 and then Nkind (Prefix (N)) = N_Identifier
8174 and then Chars (Prefix (N)) = Name_uInit
8175 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8176 end Init_Component;
8177
8178 -- Start of processing for Resolve_Selected_Component
8179
996ae0b0
RK
8180 begin
8181 if Is_Overloaded (P) then
8182
8183 -- Use the context type to select the prefix that has a selector
8184 -- of the correct name and type.
8185
8186 Found := False;
8187 Get_First_Interp (P, I, It);
8188
8189 Search : while Present (It.Typ) loop
8190 if Is_Access_Type (It.Typ) then
8191 T := Designated_Type (It.Typ);
8192 else
8193 T := It.Typ;
8194 end if;
8195
8196 if Is_Record_Type (T) then
36fcf362
RD
8197
8198 -- The visible components of a class-wide type are those of
8199 -- the root type.
8200
8201 if Is_Class_Wide_Type (T) then
8202 T := Etype (T);
8203 end if;
8204
996ae0b0 8205 Comp := First_Entity (T);
996ae0b0 8206 while Present (Comp) loop
996ae0b0
RK
8207 if Chars (Comp) = Chars (S)
8208 and then Covers (Etype (Comp), Typ)
8209 then
8210 if not Found then
8211 Found := True;
8212 I1 := I;
8213 It1 := It;
8214 Comp1 := Comp;
8215
8216 else
8217 It := Disambiguate (P, I1, I, Any_Type);
8218
8219 if It = No_Interp then
8220 Error_Msg_N
8221 ("ambiguous prefix for selected component", N);
8222 Set_Etype (N, Typ);
8223 return;
8224
8225 else
8226 It1 := It;
8227
c8ef728f
ES
8228 -- There may be an implicit dereference. Retrieve
8229 -- designated record type.
8230
8231 if Is_Access_Type (It1.Typ) then
8232 T := Designated_Type (It1.Typ);
8233 else
8234 T := It1.Typ;
8235 end if;
8236
8237 if Scope (Comp1) /= T then
996ae0b0
RK
8238
8239 -- Resolution chooses the new interpretation.
8240 -- Find the component with the right name.
8241
c8ef728f 8242 Comp1 := First_Entity (T);
996ae0b0
RK
8243 while Present (Comp1)
8244 and then Chars (Comp1) /= Chars (S)
8245 loop
8246 Comp1 := Next_Entity (Comp1);
8247 end loop;
8248 end if;
8249
8250 exit Search;
8251 end if;
8252 end if;
8253 end if;
8254
8255 Comp := Next_Entity (Comp);
8256 end loop;
996ae0b0
RK
8257 end if;
8258
8259 Get_Next_Interp (I, It);
996ae0b0
RK
8260 end loop Search;
8261
8262 Resolve (P, It1.Typ);
8263 Set_Etype (N, Typ);
aa180613 8264 Set_Entity_With_Style_Check (S, Comp1);
996ae0b0
RK
8265
8266 else
fbf5a39b 8267 -- Resolve prefix with its type
996ae0b0
RK
8268
8269 Resolve (P, T);
8270 end if;
8271
aa180613
RD
8272 -- Generate cross-reference. We needed to wait until full overloading
8273 -- resolution was complete to do this, since otherwise we can't tell if
01e17342 8274 -- we are an lvalue or not.
aa180613
RD
8275
8276 if May_Be_Lvalue (N) then
8277 Generate_Reference (Entity (S), S, 'm');
8278 else
8279 Generate_Reference (Entity (S), S, 'r');
8280 end if;
8281
c8ef728f
ES
8282 -- If prefix is an access type, the node will be transformed into an
8283 -- explicit dereference during expansion. The type of the node is the
8284 -- designated type of that of the prefix.
996ae0b0
RK
8285
8286 if Is_Access_Type (Etype (P)) then
996ae0b0 8287 T := Designated_Type (Etype (P));
c8ef728f 8288 Check_Fully_Declared_Prefix (T, P);
996ae0b0
RK
8289 else
8290 T := Etype (P);
8291 end if;
8292
8293 if Has_Discriminants (T)
964f13da 8294 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
996ae0b0
RK
8295 and then Present (Original_Record_Component (Entity (S)))
8296 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8297 and then Present (Discriminant_Checking_Func
8298 (Original_Record_Component (Entity (S))))
8299 and then not Discriminant_Checks_Suppressed (T)
6510f4c9 8300 and then not Init_Component
996ae0b0
RK
8301 then
8302 Set_Do_Discriminant_Check (N);
8303 end if;
8304
8305 if Ekind (Entity (S)) = E_Void then
8306 Error_Msg_N ("premature use of component", S);
8307 end if;
8308
8309 -- If the prefix is a record conversion, this may be a renamed
8310 -- discriminant whose bounds differ from those of the original
8311 -- one, so we must ensure that a range check is performed.
8312
8313 if Nkind (P) = N_Type_Conversion
8314 and then Ekind (Entity (S)) = E_Discriminant
fbf5a39b 8315 and then Is_Discrete_Type (Typ)
996ae0b0
RK
8316 then
8317 Set_Etype (N, Base_Type (Typ));
8318 end if;
8319
8320 -- Note: No Eval processing is required, because the prefix is of a
8321 -- record type, or protected type, and neither can possibly be static.
8322
c28408b7
RD
8323 -- If the array type is atomic, and is packed, and we are in a left side
8324 -- context, then this is worth a warning, since we have a situation
8325 -- where the access to the component may cause extra read/writes of
8326 -- the atomic array object, which could be considered unexpected.
8327
8328 if Nkind (N) = N_Selected_Component
8329 and then (Is_Atomic (T)
8330 or else (Is_Entity_Name (Prefix (N))
8331 and then Is_Atomic (Entity (Prefix (N)))))
8332 and then Is_Packed (T)
8333 and then Is_LHS (N)
8334 then
8335 Error_Msg_N ("?assignment to component of packed atomic record",
8336 Prefix (N));
8337 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8338 Prefix (N));
8339 end if;
996ae0b0
RK
8340 end Resolve_Selected_Component;
8341
8342 -------------------
8343 -- Resolve_Shift --
8344 -------------------
8345
8346 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8347 B_Typ : constant Entity_Id := Base_Type (Typ);
8348 L : constant Node_Id := Left_Opnd (N);
8349 R : constant Node_Id := Right_Opnd (N);
8350
8351 begin
8352 -- We do the resolution using the base type, because intermediate values
8353 -- in expressions always are of the base type, not a subtype of it.
8354
8355 Resolve (L, B_Typ);
8356 Resolve (R, Standard_Natural);
8357
8358 Check_Unset_Reference (L);
8359 Check_Unset_Reference (R);
8360
8361 Set_Etype (N, B_Typ);
fbf5a39b 8362 Generate_Operator_Reference (N, B_Typ);
996ae0b0
RK
8363 Eval_Shift (N);
8364 end Resolve_Shift;
8365
8366 ---------------------------
8367 -- Resolve_Short_Circuit --
8368 ---------------------------
8369
8370 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8371 B_Typ : constant Entity_Id := Base_Type (Typ);
8372 L : constant Node_Id := Left_Opnd (N);
8373 R : constant Node_Id := Right_Opnd (N);
8374
8375 begin
8376 Resolve (L, B_Typ);
8377 Resolve (R, B_Typ);
8378
45fc7ddb
HK
8379 -- Check for issuing warning for always False assert/check, this happens
8380 -- when assertions are turned off, in which case the pragma Assert/Check
36fcf362
RD
8381 -- was transformed into:
8382
8383 -- if False and then <condition> then ...
8384
8385 -- and we detect this pattern
8386
8387 if Warn_On_Assertion_Failure
8388 and then Is_Entity_Name (R)
8389 and then Entity (R) = Standard_False
8390 and then Nkind (Parent (N)) = N_If_Statement
8391 and then Nkind (N) = N_And_Then
8392 and then Is_Entity_Name (L)
8393 and then Entity (L) = Standard_False
8394 then
8395 declare
8396 Orig : constant Node_Id := Original_Node (Parent (N));
45fc7ddb 8397
36fcf362
RD
8398 begin
8399 if Nkind (Orig) = N_Pragma
26570b21 8400 and then Pragma_Name (Orig) = Name_Assert
36fcf362
RD
8401 then
8402 -- Don't want to warn if original condition is explicit False
8403
8404 declare
8405 Expr : constant Node_Id :=
8406 Original_Node
8407 (Expression
8408 (First (Pragma_Argument_Associations (Orig))));
8409 begin
8410 if Is_Entity_Name (Expr)
8411 and then Entity (Expr) = Standard_False
8412 then
8413 null;
8414 else
51bf9bdf
AC
8415 -- Issue warning. We do not want the deletion of the
8416 -- IF/AND-THEN to take this message with it. We achieve
8417 -- this by making sure that the expanded code points to
8418 -- the Sloc of the expression, not the original pragma.
8419
8420 Error_Msg_N
e7c0dd39 8421 ("?assertion would fail at run time!",
51bf9bdf
AC
8422 Expression
8423 (First (Pragma_Argument_Associations (Orig))));
36fcf362
RD
8424 end if;
8425 end;
45fc7ddb
HK
8426
8427 -- Similar processing for Check pragma
8428
8429 elsif Nkind (Orig) = N_Pragma
8430 and then Pragma_Name (Orig) = Name_Check
8431 then
8432 -- Don't want to warn if original condition is explicit False
8433
8434 declare
8435 Expr : constant Node_Id :=
8436 Original_Node
8437 (Expression
8438 (Next (First
8439 (Pragma_Argument_Associations (Orig)))));
8440 begin
8441 if Is_Entity_Name (Expr)
8442 and then Entity (Expr) = Standard_False
8443 then
8444 null;
8445 else
51bf9bdf 8446 Error_Msg_N
e7c0dd39 8447 ("?check would fail at run time!",
51bf9bdf
AC
8448 Expression
8449 (Last (Pragma_Argument_Associations (Orig))));
45fc7ddb
HK
8450 end if;
8451 end;
36fcf362
RD
8452 end if;
8453 end;
8454 end if;
8455
8456 -- Continue with processing of short circuit
8457
996ae0b0
RK
8458 Check_Unset_Reference (L);
8459 Check_Unset_Reference (R);
8460
8461 Set_Etype (N, B_Typ);
8462 Eval_Short_Circuit (N);
8463 end Resolve_Short_Circuit;
8464
8465 -------------------
8466 -- Resolve_Slice --
8467 -------------------
8468
8469 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8470 Name : constant Node_Id := Prefix (N);
8471 Drange : constant Node_Id := Discrete_Range (N);
8472 Array_Type : Entity_Id := Empty;
8473 Index : Node_Id;
8474
8475 begin
8476 if Is_Overloaded (Name) then
8477
d81b4bfe
TQ
8478 -- Use the context type to select the prefix that yields the correct
8479 -- array type.
996ae0b0
RK
8480
8481 declare
8482 I : Interp_Index;
8483 I1 : Interp_Index := 0;
8484 It : Interp;
8485 P : constant Node_Id := Prefix (N);
8486 Found : Boolean := False;
8487
8488 begin
8489 Get_First_Interp (P, I, It);
996ae0b0 8490 while Present (It.Typ) loop
996ae0b0
RK
8491 if (Is_Array_Type (It.Typ)
8492 and then Covers (Typ, It.Typ))
8493 or else (Is_Access_Type (It.Typ)
8494 and then Is_Array_Type (Designated_Type (It.Typ))
8495 and then Covers (Typ, Designated_Type (It.Typ)))
8496 then
8497 if Found then
8498 It := Disambiguate (P, I1, I, Any_Type);
8499
8500 if It = No_Interp then
8501 Error_Msg_N ("ambiguous prefix for slicing", N);
8502 Set_Etype (N, Typ);
8503 return;
8504 else
8505 Found := True;
8506 Array_Type := It.Typ;
8507 I1 := I;
8508 end if;
8509 else
8510 Found := True;
8511 Array_Type := It.Typ;
8512 I1 := I;
8513 end if;
8514 end if;
8515
8516 Get_Next_Interp (I, It);
8517 end loop;
8518 end;
8519
8520 else
8521 Array_Type := Etype (Name);
8522 end if;
8523
8524 Resolve (Name, Array_Type);
8525
8526 if Is_Access_Type (Array_Type) then
8527 Apply_Access_Check (N);
8528 Array_Type := Designated_Type (Array_Type);
8529
c8ef728f
ES
8530 -- If the prefix is an access to an unconstrained array, we must use
8531 -- the actual subtype of the object to perform the index checks. The
8532 -- object denoted by the prefix is implicit in the node, so we build
8533 -- an explicit representation for it in order to compute the actual
8534 -- subtype.
82c80734
RD
8535
8536 if not Is_Constrained (Array_Type) then
8537 Remove_Side_Effects (Prefix (N));
8538
8539 declare
8540 Obj : constant Node_Id :=
8541 Make_Explicit_Dereference (Sloc (N),
8542 Prefix => New_Copy_Tree (Prefix (N)));
8543 begin
8544 Set_Etype (Obj, Array_Type);
8545 Set_Parent (Obj, Parent (N));
8546 Array_Type := Get_Actual_Subtype (Obj);
8547 end;
8548 end if;
8549
996ae0b0 8550 elsif Is_Entity_Name (Name)
6c994759 8551 or else Nkind (Name) = N_Explicit_Dereference
996ae0b0
RK
8552 or else (Nkind (Name) = N_Function_Call
8553 and then not Is_Constrained (Etype (Name)))
8554 then
8555 Array_Type := Get_Actual_Subtype (Name);
aa5147f0
ES
8556
8557 -- If the name is a selected component that depends on discriminants,
8558 -- build an actual subtype for it. This can happen only when the name
8559 -- itself is overloaded; otherwise the actual subtype is created when
8560 -- the selected component is analyzed.
8561
8562 elsif Nkind (Name) = N_Selected_Component
8563 and then Full_Analysis
8564 and then Depends_On_Discriminant (First_Index (Array_Type))
8565 then
8566 declare
8567 Act_Decl : constant Node_Id :=
8568 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8569 begin
8570 Insert_Action (N, Act_Decl);
8571 Array_Type := Defining_Identifier (Act_Decl);
8572 end;
d79e621a
GD
8573
8574 -- Maybe this should just be "else", instead of checking for the
8575 -- specific case of slice??? This is needed for the case where
8576 -- the prefix is an Image attribute, which gets expanded to a
8577 -- slice, and so has a constrained subtype which we want to use
8578 -- for the slice range check applied below (the range check won't
8579 -- get done if the unconstrained subtype of the 'Image is used).
8580
8581 elsif Nkind (Name) = N_Slice then
8582 Array_Type := Etype (Name);
996ae0b0
RK
8583 end if;
8584
8585 -- If name was overloaded, set slice type correctly now
8586
8587 Set_Etype (N, Array_Type);
8588
c8ef728f
ES
8589 -- If the range is specified by a subtype mark, no resolution is
8590 -- necessary. Else resolve the bounds, and apply needed checks.
996ae0b0
RK
8591
8592 if not Is_Entity_Name (Drange) then
8593 Index := First_Index (Array_Type);
8594 Resolve (Drange, Base_Type (Etype (Index)));
8595
dbe945f1
AC
8596 if Nkind (Drange) = N_Range then
8597
8598 -- Ensure that side effects in the bounds are properly handled
8599
8600 Remove_Side_Effects (Low_Bound (Drange), Variable_Ref => True);
8601 Remove_Side_Effects (High_Bound (Drange), Variable_Ref => True);
0669bebe
GB
8602
8603 -- Do not apply the range check to nodes associated with the
8604 -- frontend expansion of the dispatch table. We first check
dbe945f1 8605 -- if Ada.Tags is already loaded to avoid the addition of an
0669bebe
GB
8606 -- undesired dependence on such run-time unit.
8607
dbe945f1
AC
8608 if not Tagged_Type_Expansion
8609 or else not
8610 (RTU_Loaded (Ada_Tags)
cead616d
AC
8611 and then Nkind (Prefix (N)) = N_Selected_Component
8612 and then Present (Entity (Selector_Name (Prefix (N))))
8613 and then Entity (Selector_Name (Prefix (N))) =
dbe945f1
AC
8614 RTE_Record_Component (RE_Prims_Ptr))
8615 then
8616 Apply_Range_Check (Drange, Etype (Index));
8617 end if;
996ae0b0
RK
8618 end if;
8619 end if;
8620
8621 Set_Slice_Subtype (N);
aa180613 8622
ea034236
AC
8623 -- Check bad use of type with predicates
8624
8625 if Has_Predicates (Etype (Drange)) then
ed00f472 8626 Bad_Predicated_Subtype_Use
ea034236
AC
8627 ("subtype& has predicate, not allowed in slice",
8628 Drange, Etype (Drange));
8629
8630 -- Otherwise here is where we check suspicious indexes
8631
8632 elsif Nkind (Drange) = N_Range then
aa180613
RD
8633 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8634 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8635 end if;
8636
996ae0b0 8637 Eval_Slice (N);
996ae0b0
RK
8638 end Resolve_Slice;
8639
8640 ----------------------------
8641 -- Resolve_String_Literal --
8642 ----------------------------
8643
8644 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8645 C_Typ : constant Entity_Id := Component_Type (Typ);
8646 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8647 Loc : constant Source_Ptr := Sloc (N);
8648 Str : constant String_Id := Strval (N);
8649 Strlen : constant Nat := String_Length (Str);
8650 Subtype_Id : Entity_Id;
8651 Need_Check : Boolean;
8652
8653 begin
8654 -- For a string appearing in a concatenation, defer creation of the
8655 -- string_literal_subtype until the end of the resolution of the
c8ef728f
ES
8656 -- concatenation, because the literal may be constant-folded away. This
8657 -- is a useful optimization for long concatenation expressions.
996ae0b0 8658
c8ef728f 8659 -- If the string is an aggregate built for a single character (which
996ae0b0 8660 -- happens in a non-static context) or a is null string to which special
c8ef728f
ES
8661 -- checks may apply, we build the subtype. Wide strings must also get a
8662 -- string subtype if they come from a one character aggregate. Strings
996ae0b0
RK
8663 -- generated by attributes might be static, but it is often hard to
8664 -- determine whether the enclosing context is static, so we generate
8665 -- subtypes for them as well, thus losing some rarer optimizations ???
8666 -- Same for strings that come from a static conversion.
8667
8668 Need_Check :=
8669 (Strlen = 0 and then Typ /= Standard_String)
8670 or else Nkind (Parent (N)) /= N_Op_Concat
8671 or else (N /= Left_Opnd (Parent (N))
8672 and then N /= Right_Opnd (Parent (N)))
82c80734
RD
8673 or else ((Typ = Standard_Wide_String
8674 or else Typ = Standard_Wide_Wide_String)
996ae0b0
RK
8675 and then Nkind (Original_Node (N)) /= N_String_Literal);
8676
d81b4bfe
TQ
8677 -- If the resolving type is itself a string literal subtype, we can just
8678 -- reuse it, since there is no point in creating another.
996ae0b0
RK
8679
8680 if Ekind (Typ) = E_String_Literal_Subtype then
8681 Subtype_Id := Typ;
8682
8683 elsif Nkind (Parent (N)) = N_Op_Concat
8684 and then not Need_Check
45fc7ddb
HK
8685 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8686 N_Attribute_Reference,
8687 N_Qualified_Expression,
8688 N_Type_Conversion)
996ae0b0
RK
8689 then
8690 Subtype_Id := Typ;
8691
8692 -- Otherwise we must create a string literal subtype. Note that the
8693 -- whole idea of string literal subtypes is simply to avoid the need
8694 -- for building a full fledged array subtype for each literal.
45fc7ddb 8695
996ae0b0
RK
8696 else
8697 Set_String_Literal_Subtype (N, Typ);
8698 Subtype_Id := Etype (N);
8699 end if;
8700
8701 if Nkind (Parent (N)) /= N_Op_Concat
8702 or else Need_Check
8703 then
8704 Set_Etype (N, Subtype_Id);
8705 Eval_String_Literal (N);
8706 end if;
8707
8708 if Is_Limited_Composite (Typ)
8709 or else Is_Private_Composite (Typ)
8710 then
8711 Error_Msg_N ("string literal not available for private array", N);
8712 Set_Etype (N, Any_Type);
8713 return;
8714 end if;
8715
d81b4bfe
TQ
8716 -- The validity of a null string has been checked in the call to
8717 -- Eval_String_Literal.
996ae0b0
RK
8718
8719 if Strlen = 0 then
8720 return;
8721
c8ef728f
ES
8722 -- Always accept string literal with component type Any_Character, which
8723 -- occurs in error situations and in comparisons of literals, both of
8724 -- which should accept all literals.
996ae0b0
RK
8725
8726 elsif R_Typ = Any_Character then
8727 return;
8728
f3d57416
RW
8729 -- If the type is bit-packed, then we always transform the string
8730 -- literal into a full fledged aggregate.
996ae0b0
RK
8731
8732 elsif Is_Bit_Packed_Array (Typ) then
8733 null;
8734
82c80734 8735 -- Deal with cases of Wide_Wide_String, Wide_String, and String
996ae0b0
RK
8736
8737 else
82c80734
RD
8738 -- For Standard.Wide_Wide_String, or any other type whose component
8739 -- type is Standard.Wide_Wide_Character, we know that all the
996ae0b0
RK
8740 -- characters in the string must be acceptable, since the parser
8741 -- accepted the characters as valid character literals.
8742
82c80734 8743 if R_Typ = Standard_Wide_Wide_Character then
996ae0b0
RK
8744 null;
8745
c8ef728f
ES
8746 -- For the case of Standard.String, or any other type whose component
8747 -- type is Standard.Character, we must make sure that there are no
8748 -- wide characters in the string, i.e. that it is entirely composed
8749 -- of characters in range of type Character.
996ae0b0 8750
c8ef728f
ES
8751 -- If the string literal is the result of a static concatenation, the
8752 -- test has already been performed on the components, and need not be
8753 -- repeated.
996ae0b0
RK
8754
8755 elsif R_Typ = Standard_Character
8756 and then Nkind (Original_Node (N)) /= N_Op_Concat
8757 then
8758 for J in 1 .. Strlen loop
8759 if not In_Character_Range (Get_String_Char (Str, J)) then
8760
8761 -- If we are out of range, post error. This is one of the
8762 -- very few places that we place the flag in the middle of
d81b4bfe
TQ
8763 -- a token, right under the offending wide character. Not
8764 -- quite clear if this is right wrt wide character encoding
8765 -- sequences, but it's only an error message!
996ae0b0
RK
8766
8767 Error_Msg
82c80734
RD
8768 ("literal out of range of type Standard.Character",
8769 Source_Ptr (Int (Loc) + J));
8770 return;
8771 end if;
8772 end loop;
8773
8774 -- For the case of Standard.Wide_String, or any other type whose
8775 -- component type is Standard.Wide_Character, we must make sure that
8776 -- there are no wide characters in the string, i.e. that it is
8777 -- entirely composed of characters in range of type Wide_Character.
8778
8779 -- If the string literal is the result of a static concatenation,
8780 -- the test has already been performed on the components, and need
8781 -- not be repeated.
8782
8783 elsif R_Typ = Standard_Wide_Character
8784 and then Nkind (Original_Node (N)) /= N_Op_Concat
8785 then
8786 for J in 1 .. Strlen loop
8787 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8788
8789 -- If we are out of range, post error. This is one of the
8790 -- very few places that we place the flag in the middle of
8791 -- a token, right under the offending wide character.
8792
8793 -- This is not quite right, because characters in general
8794 -- will take more than one character position ???
8795
8796 Error_Msg
8797 ("literal out of range of type Standard.Wide_Character",
996ae0b0
RK
8798 Source_Ptr (Int (Loc) + J));
8799 return;
8800 end if;
8801 end loop;
8802
8803 -- If the root type is not a standard character, then we will convert
8804 -- the string into an aggregate and will let the aggregate code do
82c80734 8805 -- the checking. Standard Wide_Wide_Character is also OK here.
996ae0b0
RK
8806
8807 else
8808 null;
996ae0b0
RK
8809 end if;
8810
c8ef728f
ES
8811 -- See if the component type of the array corresponding to the string
8812 -- has compile time known bounds. If yes we can directly check
8813 -- whether the evaluation of the string will raise constraint error.
8814 -- Otherwise we need to transform the string literal into the
8815 -- corresponding character aggregate and let the aggregate
996ae0b0
RK
8816 -- code do the checking.
8817
45fc7ddb
HK
8818 if Is_Standard_Character_Type (R_Typ) then
8819
996ae0b0
RK
8820 -- Check for the case of full range, where we are definitely OK
8821
8822 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8823 return;
8824 end if;
8825
8826 -- Here the range is not the complete base type range, so check
8827
8828 declare
8829 Comp_Typ_Lo : constant Node_Id :=
8830 Type_Low_Bound (Component_Type (Typ));
8831 Comp_Typ_Hi : constant Node_Id :=
8832 Type_High_Bound (Component_Type (Typ));
8833
8834 Char_Val : Uint;
8835
8836 begin
8837 if Compile_Time_Known_Value (Comp_Typ_Lo)
8838 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8839 then
8840 for J in 1 .. Strlen loop
8841 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8842
8843 if Char_Val < Expr_Value (Comp_Typ_Lo)
8844 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8845 then
8846 Apply_Compile_Time_Constraint_Error
07fc65c4 8847 (N, "character out of range?", CE_Range_Check_Failed,
996ae0b0
RK
8848 Loc => Source_Ptr (Int (Loc) + J));
8849 end if;
8850 end loop;
8851
8852 return;
8853 end if;
8854 end;
8855 end if;
8856 end if;
8857
8858 -- If we got here we meed to transform the string literal into the
8859 -- equivalent qualified positional array aggregate. This is rather
8860 -- heavy artillery for this situation, but it is hard work to avoid.
8861
8862 declare
fbf5a39b 8863 Lits : constant List_Id := New_List;
996ae0b0
RK
8864 P : Source_Ptr := Loc + 1;
8865 C : Char_Code;
8866
8867 begin
c8ef728f
ES
8868 -- Build the character literals, we give them source locations that
8869 -- correspond to the string positions, which is a bit tricky given
8870 -- the possible presence of wide character escape sequences.
996ae0b0
RK
8871
8872 for J in 1 .. Strlen loop
8873 C := Get_String_Char (Str, J);
8874 Set_Character_Literal_Name (C);
8875
8876 Append_To (Lits,
82c80734
RD
8877 Make_Character_Literal (P,
8878 Chars => Name_Find,
8879 Char_Literal_Value => UI_From_CC (C)));
996ae0b0
RK
8880
8881 if In_Character_Range (C) then
8882 P := P + 1;
8883
8884 -- Should we have a call to Skip_Wide here ???
8885 -- ??? else
8886 -- Skip_Wide (P);
8887
8888 end if;
8889 end loop;
8890
8891 Rewrite (N,
8892 Make_Qualified_Expression (Loc,
8893 Subtype_Mark => New_Reference_To (Typ, Loc),
8894 Expression =>
8895 Make_Aggregate (Loc, Expressions => Lits)));
8896
8897 Analyze_And_Resolve (N, Typ);
8898 end;
8899 end Resolve_String_Literal;
8900
8901 -----------------------------
8902 -- Resolve_Subprogram_Info --
8903 -----------------------------
8904
8905 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8906 begin
8907 Set_Etype (N, Typ);
8908 end Resolve_Subprogram_Info;
8909
8910 -----------------------------
8911 -- Resolve_Type_Conversion --
8912 -----------------------------
8913
8914 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
4b2d2c13
AC
8915 Conv_OK : constant Boolean := Conversion_OK (N);
8916 Operand : constant Node_Id := Expression (N);
b7d1f17f
HK
8917 Operand_Typ : constant Entity_Id := Etype (Operand);
8918 Target_Typ : constant Entity_Id := Etype (N);
996ae0b0 8919 Rop : Node_Id;
fbf5a39b
AC
8920 Orig_N : Node_Id;
8921 Orig_T : Node_Id;
996ae0b0 8922
ae2aa109
AC
8923 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
8924 -- Set to False to suppress cases where we want to suppress the test
8925 -- for redundancy to avoid possible false positives on this warning.
8926
996ae0b0 8927 begin
996ae0b0 8928 if not Conv_OK
b7d1f17f 8929 and then not Valid_Conversion (N, Target_Typ, Operand)
996ae0b0
RK
8930 then
8931 return;
8932 end if;
8933
ae2aa109
AC
8934 -- If the Operand Etype is Universal_Fixed, then the conversion is
8935 -- never redundant. We need this check because by the time we have
8936 -- finished the rather complex transformation, the conversion looks
8937 -- redundant when it is not.
8938
8939 if Operand_Typ = Universal_Fixed then
8940 Test_Redundant := False;
8941
8942 -- If the operand is marked as Any_Fixed, then special processing is
8943 -- required. This is also a case where we suppress the test for a
8944 -- redundant conversion, since most certainly it is not redundant.
8945
8946 elsif Operand_Typ = Any_Fixed then
8947 Test_Redundant := False;
996ae0b0
RK
8948
8949 -- Mixed-mode operation involving a literal. Context must be a fixed
8950 -- type which is applied to the literal subsequently.
8951
8952 if Is_Fixed_Point_Type (Typ) then
8953 Set_Etype (Operand, Universal_Real);
8954
8955 elsif Is_Numeric_Type (Typ)
45fc7ddb 8956 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
996ae0b0 8957 and then (Etype (Right_Opnd (Operand)) = Universal_Real
45fc7ddb
HK
8958 or else
8959 Etype (Left_Opnd (Operand)) = Universal_Real)
996ae0b0 8960 then
a77842bd
TQ
8961 -- Return if expression is ambiguous
8962
996ae0b0 8963 if Unique_Fixed_Point_Type (N) = Any_Type then
a77842bd 8964 return;
82c80734 8965
a77842bd
TQ
8966 -- If nothing else, the available fixed type is Duration
8967
8968 else
996ae0b0
RK
8969 Set_Etype (Operand, Standard_Duration);
8970 end if;
8971
bc5f3720 8972 -- Resolve the real operand with largest available precision
9ebe3743 8973
996ae0b0
RK
8974 if Etype (Right_Opnd (Operand)) = Universal_Real then
8975 Rop := New_Copy_Tree (Right_Opnd (Operand));
8976 else
8977 Rop := New_Copy_Tree (Left_Opnd (Operand));
8978 end if;
8979
9ebe3743 8980 Resolve (Rop, Universal_Real);
996ae0b0 8981
82c80734
RD
8982 -- If the operand is a literal (it could be a non-static and
8983 -- illegal exponentiation) check whether the use of Duration
8984 -- is potentially inaccurate.
8985
8986 if Nkind (Rop) = N_Real_Literal
8987 and then Realval (Rop) /= Ureal_0
996ae0b0
RK
8988 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8989 then
aa180613 8990 Error_Msg_N
aa5147f0
ES
8991 ("?universal real operand can only " &
8992 "be interpreted as Duration!",
aa180613
RD
8993 Rop);
8994 Error_Msg_N
aa5147f0 8995 ("\?precision will be lost in the conversion!", Rop);
996ae0b0
RK
8996 end if;
8997
891a6e79
AC
8998 elsif Is_Numeric_Type (Typ)
8999 and then Nkind (Operand) in N_Op
9000 and then Unique_Fixed_Point_Type (N) /= Any_Type
9001 then
9002 Set_Etype (Operand, Standard_Duration);
9003
996ae0b0
RK
9004 else
9005 Error_Msg_N ("invalid context for mixed mode operation", N);
9006 Set_Etype (Operand, Any_Type);
9007 return;
9008 end if;
9009 end if;
9010
fbf5a39b 9011 Resolve (Operand);
996ae0b0 9012
b0186f71
AC
9013 -- In SPARK or ALFA, a type conversion between array types should be
9014 -- restricted to types which have matching static bounds.
9015
fe5d3068 9016 if Is_Array_Type (Target_Typ)
b0186f71
AC
9017 and then Is_Array_Type (Operand_Typ)
9018 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9019 then
fe5d3068
YM
9020 Check_Formal_Restriction
9021 ("array types should have matching static bounds", N);
b0186f71
AC
9022 end if;
9023
996ae0b0 9024 -- Note: we do the Eval_Type_Conversion call before applying the
d81b4bfe
TQ
9025 -- required checks for a subtype conversion. This is important, since
9026 -- both are prepared under certain circumstances to change the type
9027 -- conversion to a constraint error node, but in the case of
9028 -- Eval_Type_Conversion this may reflect an illegality in the static
9029 -- case, and we would miss the illegality (getting only a warning
9030 -- message), if we applied the type conversion checks first.
996ae0b0
RK
9031
9032 Eval_Type_Conversion (N);
9033
d81b4bfe
TQ
9034 -- Even when evaluation is not possible, we may be able to simplify the
9035 -- conversion or its expression. This needs to be done before applying
9036 -- checks, since otherwise the checks may use the original expression
9037 -- and defeat the simplifications. This is specifically the case for
9038 -- elimination of the floating-point Truncation attribute in
9039 -- float-to-int conversions.
0669bebe
GB
9040
9041 Simplify_Type_Conversion (N);
9042
d81b4bfe
TQ
9043 -- If after evaluation we still have a type conversion, then we may need
9044 -- to apply checks required for a subtype conversion.
996ae0b0
RK
9045
9046 -- Skip these type conversion checks if universal fixed operands
9047 -- operands involved, since range checks are handled separately for
9048 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9049
9050 if Nkind (N) = N_Type_Conversion
b7d1f17f
HK
9051 and then not Is_Generic_Type (Root_Type (Target_Typ))
9052 and then Target_Typ /= Universal_Fixed
9053 and then Operand_Typ /= Universal_Fixed
996ae0b0
RK
9054 then
9055 Apply_Type_Conversion_Checks (N);
9056 end if;
9057
d81b4bfe
TQ
9058 -- Issue warning for conversion of simple object to its own type. We
9059 -- have to test the original nodes, since they may have been rewritten
9060 -- by various optimizations.
fbf5a39b
AC
9061
9062 Orig_N := Original_Node (N);
996ae0b0 9063
ae2aa109
AC
9064 -- Here we test for a redundant conversion if the warning mode is
9065 -- active (and was not locally reset), and we have a type conversion
9066 -- from source not appearing in a generic instance.
9067
9068 if Test_Redundant
fbf5a39b 9069 and then Nkind (Orig_N) = N_Type_Conversion
ae2aa109 9070 and then Comes_From_Source (Orig_N)
5453d5bd 9071 and then not In_Instance
996ae0b0 9072 then
fbf5a39b 9073 Orig_N := Original_Node (Expression (Orig_N));
b7d1f17f 9074 Orig_T := Target_Typ;
fbf5a39b
AC
9075
9076 -- If the node is part of a larger expression, the Target_Type
9077 -- may not be the original type of the node if the context is a
9078 -- condition. Recover original type to see if conversion is needed.
9079
9080 if Is_Boolean_Type (Orig_T)
9081 and then Nkind (Parent (N)) in N_Op
9082 then
9083 Orig_T := Etype (Parent (N));
9084 end if;
9085
4adf3c50 9086 -- If we have an entity name, then give the warning if the entity
ae2aa109
AC
9087 -- is the right type, or if it is a loop parameter covered by the
9088 -- original type (that's needed because loop parameters have an
9089 -- odd subtype coming from the bounds).
9090
9091 if (Is_Entity_Name (Orig_N)
9092 and then
9093 (Etype (Entity (Orig_N)) = Orig_T
9094 or else
9095 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
477bd732 9096 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
ae2aa109 9097
477bd732 9098 -- If not an entity, then type of expression must match
ae2aa109
AC
9099
9100 or else Etype (Orig_N) = Orig_T
fbf5a39b 9101 then
4b2d2c13
AC
9102 -- One more check, do not give warning if the analyzed conversion
9103 -- has an expression with non-static bounds, and the bounds of the
9104 -- target are static. This avoids junk warnings in cases where the
9105 -- conversion is necessary to establish staticness, for example in
9106 -- a case statement.
9107
9108 if not Is_OK_Static_Subtype (Operand_Typ)
9109 and then Is_OK_Static_Subtype (Target_Typ)
9110 then
9111 null;
9112
9db0b232 9113 -- Finally, if this type conversion occurs in a context that
4adf3c50
AC
9114 -- requires a prefix, and the expression is a qualified expression
9115 -- then the type conversion is not redundant, because a qualified
9116 -- expression is not a prefix, whereas a type conversion is. For
9117 -- example, "X := T'(Funx(...)).Y;" is illegal because a selected
9118 -- component requires a prefix, but a type conversion makes it
9119 -- legal: "X := T(T'(Funx(...))).Y;"
9120
9db0b232
AC
9121 -- In Ada 2012, a qualified expression is a name, so this idiom is
9122 -- no longer needed, but we still suppress the warning because it
9123 -- seems unfriendly for warnings to pop up when you switch to the
9124 -- newer language version.
be257e99
AC
9125
9126 elsif Nkind (Orig_N) = N_Qualified_Expression
f5d96d00
AC
9127 and then Nkind_In (Parent (N), N_Attribute_Reference,
9128 N_Indexed_Component,
9129 N_Selected_Component,
9130 N_Slice,
9131 N_Explicit_Dereference)
be257e99
AC
9132 then
9133 null;
9134
ae2aa109
AC
9135 -- Here we give the redundant conversion warning. If it is an
9136 -- entity, give the name of the entity in the message. If not,
9137 -- just mention the expression.
4b2d2c13
AC
9138
9139 else
ae2aa109
AC
9140 if Is_Entity_Name (Orig_N) then
9141 Error_Msg_Node_2 := Orig_T;
9142 Error_Msg_NE -- CODEFIX
9143 ("?redundant conversion, & is of type &!",
9144 N, Entity (Orig_N));
9145 else
9146 Error_Msg_NE
9147 ("?redundant conversion, expression is of type&!",
9148 N, Orig_T);
9149 end if;
4b2d2c13 9150 end if;
fbf5a39b 9151 end if;
996ae0b0 9152 end if;
758c442c 9153
b7d1f17f 9154 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
0669bebe
GB
9155 -- No need to perform any interface conversion if the type of the
9156 -- expression coincides with the target type.
758c442c 9157
0791fbe9 9158 if Ada_Version >= Ada_2005
0669bebe 9159 and then Expander_Active
b7d1f17f 9160 and then Operand_Typ /= Target_Typ
0669bebe 9161 then
b7d1f17f
HK
9162 declare
9163 Opnd : Entity_Id := Operand_Typ;
9164 Target : Entity_Id := Target_Typ;
758c442c 9165
b7d1f17f
HK
9166 begin
9167 if Is_Access_Type (Opnd) then
841dd0f5 9168 Opnd := Designated_Type (Opnd);
1420b484
JM
9169 end if;
9170
b7d1f17f 9171 if Is_Access_Type (Target_Typ) then
841dd0f5 9172 Target := Designated_Type (Target);
4197ae1e 9173 end if;
c8ef728f 9174
b7d1f17f
HK
9175 if Opnd = Target then
9176 null;
c8ef728f 9177
b7d1f17f 9178 -- Conversion from interface type
ea985d95 9179
b7d1f17f 9180 elsif Is_Interface (Opnd) then
ea985d95 9181
b7d1f17f 9182 -- Ada 2005 (AI-217): Handle entities from limited views
aa180613 9183
b7d1f17f
HK
9184 if From_With_Type (Opnd) then
9185 Error_Msg_Qual_Level := 99;
305caf42
AC
9186 Error_Msg_NE -- CODEFIX
9187 ("missing WITH clause on package &", N,
b7d1f17f
HK
9188 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9189 Error_Msg_N
9190 ("type conversions require visibility of the full view",
9191 N);
aa180613 9192
aa5147f0
ES
9193 elsif From_With_Type (Target)
9194 and then not
9195 (Is_Access_Type (Target_Typ)
9196 and then Present (Non_Limited_View (Etype (Target))))
9197 then
b7d1f17f 9198 Error_Msg_Qual_Level := 99;
305caf42
AC
9199 Error_Msg_NE -- CODEFIX
9200 ("missing WITH clause on package &", N,
b7d1f17f
HK
9201 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9202 Error_Msg_N
9203 ("type conversions require visibility of the full view",
9204 N);
aa180613 9205
b7d1f17f
HK
9206 else
9207 Expand_Interface_Conversion (N, Is_Static => False);
9208 end if;
9209
9210 -- Conversion to interface type
9211
9212 elsif Is_Interface (Target) then
9213
9214 -- Handle subtypes
9215
8a95f4e8 9216 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
b7d1f17f
HK
9217 Opnd := Etype (Opnd);
9218 end if;
9219
9220 if not Interface_Present_In_Ancestor
9221 (Typ => Opnd,
9222 Iface => Target)
9223 then
9224 if Is_Class_Wide_Type (Opnd) then
9225
9226 -- The static analysis is not enough to know if the
9227 -- interface is implemented or not. Hence we must pass
9228 -- the work to the expander to generate code to evaluate
e7c0dd39 9229 -- the conversion at run time.
b7d1f17f
HK
9230
9231 Expand_Interface_Conversion (N, Is_Static => False);
9232
9233 else
9234 Error_Msg_Name_1 := Chars (Etype (Target));
9235 Error_Msg_Name_2 := Chars (Opnd);
9236 Error_Msg_N
9237 ("wrong interface conversion (% is not a progenitor " &
9238 "of %)", N);
9239 end if;
9240
9241 else
9242 Expand_Interface_Conversion (N);
9243 end if;
9244 end if;
9245 end;
758c442c 9246 end if;
996ae0b0
RK
9247 end Resolve_Type_Conversion;
9248
9249 ----------------------
9250 -- Resolve_Unary_Op --
9251 ----------------------
9252
9253 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
fbf5a39b
AC
9254 B_Typ : constant Entity_Id := Base_Type (Typ);
9255 R : constant Node_Id := Right_Opnd (N);
9256 OK : Boolean;
9257 Lo : Uint;
9258 Hi : Uint;
996ae0b0
RK
9259
9260 begin
b7d1f17f 9261 -- Deal with intrinsic unary operators
996ae0b0 9262
fbf5a39b
AC
9263 if Comes_From_Source (N)
9264 and then Ekind (Entity (N)) = E_Function
9265 and then Is_Imported (Entity (N))
9266 and then Is_Intrinsic_Subprogram (Entity (N))
9267 then
9268 Resolve_Intrinsic_Unary_Operator (N, Typ);
9269 return;
9270 end if;
9271
0669bebe
GB
9272 -- Deal with universal cases
9273
996ae0b0 9274 if Etype (R) = Universal_Integer
0669bebe
GB
9275 or else
9276 Etype (R) = Universal_Real
996ae0b0
RK
9277 then
9278 Check_For_Visible_Operator (N, B_Typ);
9279 end if;
9280
9281 Set_Etype (N, B_Typ);
9282 Resolve (R, B_Typ);
fbf5a39b 9283
9ebe3743
HK
9284 -- Generate warning for expressions like abs (x mod 2)
9285
9286 if Warn_On_Redundant_Constructs
9287 and then Nkind (N) = N_Op_Abs
9288 then
9289 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9290
9291 if OK and then Hi >= Lo and then Lo >= 0 then
305caf42 9292 Error_Msg_N -- CODEFIX
9ebe3743
HK
9293 ("?abs applied to known non-negative value has no effect", N);
9294 end if;
9295 end if;
9296
0669bebe
GB
9297 -- Deal with reference generation
9298
996ae0b0 9299 Check_Unset_Reference (R);
fbf5a39b 9300 Generate_Operator_Reference (N, B_Typ);
996ae0b0
RK
9301 Eval_Unary_Op (N);
9302
9303 -- Set overflow checking bit. Much cleverer code needed here eventually
9304 -- and perhaps the Resolve routines should be separated for the various
9305 -- arithmetic operations, since they will need different processing ???
9306
9307 if Nkind (N) in N_Op then
9308 if not Overflow_Checks_Suppressed (Etype (N)) then
fbf5a39b 9309 Enable_Overflow_Check (N);
996ae0b0
RK
9310 end if;
9311 end if;
0669bebe 9312
d81b4bfe
TQ
9313 -- Generate warning for expressions like -5 mod 3 for integers. No need
9314 -- to worry in the floating-point case, since parens do not affect the
9315 -- result so there is no point in giving in a warning.
0669bebe
GB
9316
9317 declare
9318 Norig : constant Node_Id := Original_Node (N);
9319 Rorig : Node_Id;
9320 Val : Uint;
9321 HB : Uint;
9322 LB : Uint;
9323 Lval : Uint;
9324 Opnd : Node_Id;
9325
9326 begin
9327 if Warn_On_Questionable_Missing_Parens
9328 and then Comes_From_Source (Norig)
9329 and then Is_Integer_Type (Typ)
9330 and then Nkind (Norig) = N_Op_Minus
9331 then
9332 Rorig := Original_Node (Right_Opnd (Norig));
9333
9334 -- We are looking for cases where the right operand is not
f3d57416 9335 -- parenthesized, and is a binary operator, multiply, divide, or
0669bebe
GB
9336 -- mod. These are the cases where the grouping can affect results.
9337
9338 if Paren_Count (Rorig) = 0
45fc7ddb 9339 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
0669bebe
GB
9340 then
9341 -- For mod, we always give the warning, since the value is
9342 -- affected by the parenthesization (e.g. (-5) mod 315 /=
d81b4bfe 9343 -- -(5 mod 315)). But for the other cases, the only concern is
0669bebe
GB
9344 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9345 -- overflows, but (-2) * 64 does not). So we try to give the
9346 -- message only when overflow is possible.
9347
9348 if Nkind (Rorig) /= N_Op_Mod
9349 and then Compile_Time_Known_Value (R)
9350 then
9351 Val := Expr_Value (R);
9352
9353 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9354 HB := Expr_Value (Type_High_Bound (Typ));
9355 else
9356 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9357 end if;
9358
9359 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9360 LB := Expr_Value (Type_Low_Bound (Typ));
9361 else
9362 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9363 end if;
9364
d81b4bfe
TQ
9365 -- Note that the test below is deliberately excluding the
9366 -- largest negative number, since that is a potentially
0669bebe
GB
9367 -- troublesome case (e.g. -2 * x, where the result is the
9368 -- largest negative integer has an overflow with 2 * x).
9369
9370 if Val > LB and then Val <= HB then
9371 return;
9372 end if;
9373 end if;
9374
9375 -- For the multiplication case, the only case we have to worry
9376 -- about is when (-a)*b is exactly the largest negative number
9377 -- so that -(a*b) can cause overflow. This can only happen if
9378 -- a is a power of 2, and more generally if any operand is a
9379 -- constant that is not a power of 2, then the parentheses
9380 -- cannot affect whether overflow occurs. We only bother to
9381 -- test the left most operand
9382
9383 -- Loop looking at left operands for one that has known value
9384
9385 Opnd := Rorig;
9386 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9387 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9388 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9389
9390 -- Operand value of 0 or 1 skips warning
9391
9392 if Lval <= 1 then
9393 return;
9394
9395 -- Otherwise check power of 2, if power of 2, warn, if
9396 -- anything else, skip warning.
9397
9398 else
9399 while Lval /= 2 loop
9400 if Lval mod 2 = 1 then
9401 return;
9402 else
9403 Lval := Lval / 2;
9404 end if;
9405 end loop;
9406
9407 exit Opnd_Loop;
9408 end if;
9409 end if;
9410
9411 -- Keep looking at left operands
9412
9413 Opnd := Left_Opnd (Opnd);
9414 end loop Opnd_Loop;
9415
9416 -- For rem or "/" we can only have a problematic situation
9417 -- if the divisor has a value of minus one or one. Otherwise
9418 -- overflow is impossible (divisor > 1) or we have a case of
9419 -- division by zero in any case.
9420
45fc7ddb 9421 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
0669bebe
GB
9422 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9423 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9424 then
9425 return;
9426 end if;
9427
9428 -- If we fall through warning should be issued
9429
ed2233dc 9430 Error_Msg_N
aa5147f0 9431 ("?unary minus expression should be parenthesized here!", N);
0669bebe
GB
9432 end if;
9433 end if;
9434 end;
996ae0b0
RK
9435 end Resolve_Unary_Op;
9436
9437 ----------------------------------
9438 -- Resolve_Unchecked_Expression --
9439 ----------------------------------
9440
9441 procedure Resolve_Unchecked_Expression
9442 (N : Node_Id;
9443 Typ : Entity_Id)
9444 is
9445 begin
9446 Resolve (Expression (N), Typ, Suppress => All_Checks);
9447 Set_Etype (N, Typ);
9448 end Resolve_Unchecked_Expression;
9449
9450 ---------------------------------------
9451 -- Resolve_Unchecked_Type_Conversion --
9452 ---------------------------------------
9453
9454 procedure Resolve_Unchecked_Type_Conversion
9455 (N : Node_Id;
9456 Typ : Entity_Id)
9457 is
07fc65c4
GB
9458 pragma Warnings (Off, Typ);
9459
996ae0b0
RK
9460 Operand : constant Node_Id := Expression (N);
9461 Opnd_Type : constant Entity_Id := Etype (Operand);
9462
9463 begin
a77842bd 9464 -- Resolve operand using its own type
996ae0b0
RK
9465
9466 Resolve (Operand, Opnd_Type);
9467 Eval_Unchecked_Conversion (N);
996ae0b0
RK
9468 end Resolve_Unchecked_Type_Conversion;
9469
9470 ------------------------------
9471 -- Rewrite_Operator_As_Call --
9472 ------------------------------
9473
9474 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
fbf5a39b
AC
9475 Loc : constant Source_Ptr := Sloc (N);
9476 Actuals : constant List_Id := New_List;
996ae0b0
RK
9477 New_N : Node_Id;
9478
9479 begin
9480 if Nkind (N) in N_Binary_Op then
9481 Append (Left_Opnd (N), Actuals);
9482 end if;
9483
9484 Append (Right_Opnd (N), Actuals);
9485
9486 New_N :=
9487 Make_Function_Call (Sloc => Loc,
9488 Name => New_Occurrence_Of (Nam, Loc),
9489 Parameter_Associations => Actuals);
9490
9491 Preserve_Comes_From_Source (New_N, N);
9492 Preserve_Comes_From_Source (Name (New_N), N);
9493 Rewrite (N, New_N);
9494 Set_Etype (N, Etype (Nam));
9495 end Rewrite_Operator_As_Call;
9496
9497 ------------------------------
9498 -- Rewrite_Renamed_Operator --
9499 ------------------------------
9500
0ab80019
AC
9501 procedure Rewrite_Renamed_Operator
9502 (N : Node_Id;
9503 Op : Entity_Id;
9504 Typ : Entity_Id)
9505 is
996ae0b0
RK
9506 Nam : constant Name_Id := Chars (Op);
9507 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9508 Op_Node : Node_Id;
9509
9510 begin
d81b4bfe
TQ
9511 -- Rewrite the operator node using the real operator, not its renaming.
9512 -- Exclude user-defined intrinsic operations of the same name, which are
9513 -- treated separately and rewritten as calls.
996ae0b0 9514
964f13da 9515 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
996ae0b0
RK
9516 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9517 Set_Chars (Op_Node, Nam);
9518 Set_Etype (Op_Node, Etype (N));
9519 Set_Entity (Op_Node, Op);
9520 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9521
b7d1f17f
HK
9522 -- Indicate that both the original entity and its renaming are
9523 -- referenced at this point.
fbf5a39b
AC
9524
9525 Generate_Reference (Entity (N), N);
996ae0b0
RK
9526 Generate_Reference (Op, N);
9527
9528 if Is_Binary then
9529 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9530 end if;
9531
9532 Rewrite (N, Op_Node);
0ab80019 9533
1366997b
AC
9534 -- If the context type is private, add the appropriate conversions so
9535 -- that the operator is applied to the full view. This is done in the
9536 -- routines that resolve intrinsic operators.
0ab80019
AC
9537
9538 if Is_Intrinsic_Subprogram (Op)
9539 and then Is_Private_Type (Typ)
9540 then
9541 case Nkind (N) is
9542 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9543 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9544 Resolve_Intrinsic_Operator (N, Typ);
9545
d81b4bfe 9546 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
0ab80019
AC
9547 Resolve_Intrinsic_Unary_Operator (N, Typ);
9548
9549 when others =>
9550 Resolve (N, Typ);
9551 end case;
9552 end if;
9553
964f13da
RD
9554 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9555
1366997b
AC
9556 -- Operator renames a user-defined operator of the same name. Use the
9557 -- original operator in the node, which is the one Gigi knows about.
0ab80019
AC
9558
9559 Set_Entity (N, Op);
9560 Set_Is_Overloaded (N, False);
996ae0b0
RK
9561 end if;
9562 end Rewrite_Renamed_Operator;
9563
9564 -----------------------
9565 -- Set_Slice_Subtype --
9566 -----------------------
9567
1366997b
AC
9568 -- Build an implicit subtype declaration to represent the type delivered by
9569 -- the slice. This is an abbreviated version of an array subtype. We define
9570 -- an index subtype for the slice, using either the subtype name or the
9571 -- discrete range of the slice. To be consistent with index usage elsewhere
9572 -- we create a list header to hold the single index. This list is not
9573 -- otherwise attached to the syntax tree.
996ae0b0
RK
9574
9575 procedure Set_Slice_Subtype (N : Node_Id) is
9576 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 9577 Index_List : constant List_Id := New_List;
996ae0b0 9578 Index : Node_Id;
996ae0b0
RK
9579 Index_Subtype : Entity_Id;
9580 Index_Type : Entity_Id;
9581 Slice_Subtype : Entity_Id;
9582 Drange : constant Node_Id := Discrete_Range (N);
9583
9584 begin
9585 if Is_Entity_Name (Drange) then
9586 Index_Subtype := Entity (Drange);
9587
9588 else
9589 -- We force the evaluation of a range. This is definitely needed in
9590 -- the renamed case, and seems safer to do unconditionally. Note in
9591 -- any case that since we will create and insert an Itype referring
9592 -- to this range, we must make sure any side effect removal actions
9593 -- are inserted before the Itype definition.
9594
9595 if Nkind (Drange) = N_Range then
9596 Force_Evaluation (Low_Bound (Drange));
9597 Force_Evaluation (High_Bound (Drange));
9598 end if;
9599
9600 Index_Type := Base_Type (Etype (Drange));
9601
9602 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9603
8a95f4e8 9604 -- Take a new copy of Drange (where bounds have been rewritten to
3c1ecd7e
AC
9605 -- reference side-effect-free names). Using a separate tree ensures
9606 -- that further expansion (e.g. while rewriting a slice assignment
8a95f4e8
RD
9607 -- into a FOR loop) does not attempt to remove side effects on the
9608 -- bounds again (which would cause the bounds in the index subtype
9609 -- definition to refer to temporaries before they are defined) (the
9610 -- reason is that some names are considered side effect free here
9611 -- for the subtype, but not in the context of a loop iteration
9612 -- scheme).
9613
9614 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
4230bdb7 9615 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
996ae0b0
RK
9616 Set_Etype (Index_Subtype, Index_Type);
9617 Set_Size_Info (Index_Subtype, Index_Type);
9618 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9619 end if;
9620
9621 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9622
9623 Index := New_Occurrence_Of (Index_Subtype, Loc);
9624 Set_Etype (Index, Index_Subtype);
9625 Append (Index, Index_List);
9626
996ae0b0
RK
9627 Set_First_Index (Slice_Subtype, Index);
9628 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9629 Set_Is_Constrained (Slice_Subtype, True);
996ae0b0 9630
8a95f4e8
RD
9631 Check_Compile_Time_Size (Slice_Subtype);
9632
b7d1f17f
HK
9633 -- The Etype of the existing Slice node is reset to this slice subtype.
9634 -- Its bounds are obtained from its first index.
996ae0b0
RK
9635
9636 Set_Etype (N, Slice_Subtype);
9637
cfab0c49
AC
9638 -- For packed slice subtypes, freeze immediately (except in the
9639 -- case of being in a "spec expression" where we never freeze
9640 -- when we first see the expression).
8a95f4e8
RD
9641
9642 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9643 Freeze_Itype (Slice_Subtype, N);
996ae0b0 9644
cfab0c49
AC
9645 -- For all other cases insert an itype reference in the slice's actions
9646 -- so that the itype is frozen at the proper place in the tree (i.e. at
9647 -- the point where actions for the slice are analyzed). Note that this
9648 -- is different from freezing the itype immediately, which might be
9649 -- premature (e.g. if the slice is within a transient scope).
9650
8a95f4e8
RD
9651 else
9652 Ensure_Defined (Typ => Slice_Subtype, N => N);
9653 end if;
996ae0b0
RK
9654 end Set_Slice_Subtype;
9655
9656 --------------------------------
9657 -- Set_String_Literal_Subtype --
9658 --------------------------------
9659
9660 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
c8ef728f
ES
9661 Loc : constant Source_Ptr := Sloc (N);
9662 Low_Bound : constant Node_Id :=
d81b4bfe 9663 Type_Low_Bound (Etype (First_Index (Typ)));
996ae0b0
RK
9664 Subtype_Id : Entity_Id;
9665
9666 begin
9667 if Nkind (N) /= N_String_Literal then
9668 return;
996ae0b0
RK
9669 end if;
9670
c8ef728f 9671 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
91b1417d
AC
9672 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9673 (String_Length (Strval (N))));
c8ef728f
ES
9674 Set_Etype (Subtype_Id, Base_Type (Typ));
9675 Set_Is_Constrained (Subtype_Id);
9676 Set_Etype (N, Subtype_Id);
9677
9678 if Is_OK_Static_Expression (Low_Bound) then
996ae0b0 9679
1366997b
AC
9680 -- The low bound is set from the low bound of the corresponding index
9681 -- type. Note that we do not store the high bound in the string literal
9682 -- subtype, but it can be deduced if necessary from the length and the
9683 -- low bound.
996ae0b0 9684
c8ef728f 9685 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
996ae0b0 9686
c8ef728f
ES
9687 else
9688 Set_String_Literal_Low_Bound
9689 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9690 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9691
b7d1f17f
HK
9692 -- Build bona fide subtype for the string, and wrap it in an
9693 -- unchecked conversion, because the backend expects the
c8ef728f
ES
9694 -- String_Literal_Subtype to have a static lower bound.
9695
9696 declare
9697 Index_List : constant List_Id := New_List;
9698 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9699 High_Bound : constant Node_Id :=
9700 Make_Op_Add (Loc,
9701 Left_Opnd => New_Copy_Tree (Low_Bound),
9702 Right_Opnd =>
9703 Make_Integer_Literal (Loc,
9704 String_Length (Strval (N)) - 1));
9705 Array_Subtype : Entity_Id;
9706 Index_Subtype : Entity_Id;
9707 Drange : Node_Id;
9708 Index : Node_Id;
9709
9710 begin
9711 Index_Subtype :=
9712 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
0669bebe 9713 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
c8ef728f
ES
9714 Set_Scalar_Range (Index_Subtype, Drange);
9715 Set_Parent (Drange, N);
9716 Analyze_And_Resolve (Drange, Index_Type);
9717
36fcf362
RD
9718 -- In the context, the Index_Type may already have a constraint,
9719 -- so use common base type on string subtype. The base type may
9720 -- be used when generating attributes of the string, for example
9721 -- in the context of a slice assignment.
9722
4adf3c50
AC
9723 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9724 Set_Size_Info (Index_Subtype, Index_Type);
9725 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
c8ef728f
ES
9726
9727 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9728
9729 Index := New_Occurrence_Of (Index_Subtype, Loc);
9730 Set_Etype (Index, Index_Subtype);
9731 Append (Index, Index_List);
9732
9733 Set_First_Index (Array_Subtype, Index);
9734 Set_Etype (Array_Subtype, Base_Type (Typ));
9735 Set_Is_Constrained (Array_Subtype, True);
c8ef728f
ES
9736
9737 Rewrite (N,
9738 Make_Unchecked_Type_Conversion (Loc,
9739 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9740 Expression => Relocate_Node (N)));
9741 Set_Etype (N, Array_Subtype);
9742 end;
9743 end if;
996ae0b0
RK
9744 end Set_String_Literal_Subtype;
9745
0669bebe
GB
9746 ------------------------------
9747 -- Simplify_Type_Conversion --
9748 ------------------------------
9749
9750 procedure Simplify_Type_Conversion (N : Node_Id) is
9751 begin
9752 if Nkind (N) = N_Type_Conversion then
9753 declare
9754 Operand : constant Node_Id := Expression (N);
9755 Target_Typ : constant Entity_Id := Etype (N);
9756 Opnd_Typ : constant Entity_Id := Etype (Operand);
9757
9758 begin
9759 if Is_Floating_Point_Type (Opnd_Typ)
9760 and then
9761 (Is_Integer_Type (Target_Typ)
9762 or else (Is_Fixed_Point_Type (Target_Typ)
9763 and then Conversion_OK (N)))
9764 and then Nkind (Operand) = N_Attribute_Reference
9765 and then Attribute_Name (Operand) = Name_Truncation
9766
9767 -- Special processing required if the conversion is the expression
9768 -- of a Truncation attribute reference. In this case we replace:
9769
9770 -- ityp (ftyp'Truncation (x))
9771
9772 -- by
9773
9774 -- ityp (x)
9775
4adf3c50 9776 -- with the Float_Truncate flag set, which is more efficient.
0669bebe
GB
9777
9778 then
9779 Rewrite (Operand,
9780 Relocate_Node (First (Expressions (Operand))));
9781 Set_Float_Truncate (N, True);
9782 end if;
9783 end;
9784 end if;
9785 end Simplify_Type_Conversion;
9786
996ae0b0
RK
9787 -----------------------------
9788 -- Unique_Fixed_Point_Type --
9789 -----------------------------
9790
9791 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9792 T1 : Entity_Id := Empty;
9793 T2 : Entity_Id;
9794 Item : Node_Id;
9795 Scop : Entity_Id;
9796
9797 procedure Fixed_Point_Error;
d81b4bfe
TQ
9798 -- Give error messages for true ambiguity. Messages are posted on node
9799 -- N, and entities T1, T2 are the possible interpretations.
a77842bd
TQ
9800
9801 -----------------------
9802 -- Fixed_Point_Error --
9803 -----------------------
996ae0b0
RK
9804
9805 procedure Fixed_Point_Error is
9806 begin
ed2233dc
AC
9807 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9808 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9809 Error_Msg_NE ("\\possible interpretation as}", N, T2);
996ae0b0
RK
9810 end Fixed_Point_Error;
9811
a77842bd
TQ
9812 -- Start of processing for Unique_Fixed_Point_Type
9813
996ae0b0
RK
9814 begin
9815 -- The operations on Duration are visible, so Duration is always a
9816 -- possible interpretation.
9817
9818 T1 := Standard_Duration;
9819
bc5f3720 9820 -- Look for fixed-point types in enclosing scopes
996ae0b0 9821
fbf5a39b 9822 Scop := Current_Scope;
996ae0b0
RK
9823 while Scop /= Standard_Standard loop
9824 T2 := First_Entity (Scop);
996ae0b0
RK
9825 while Present (T2) loop
9826 if Is_Fixed_Point_Type (T2)
9827 and then Current_Entity (T2) = T2
9828 and then Scope (Base_Type (T2)) = Scop
9829 then
9830 if Present (T1) then
9831 Fixed_Point_Error;
9832 return Any_Type;
9833 else
9834 T1 := T2;
9835 end if;
9836 end if;
9837
9838 Next_Entity (T2);
9839 end loop;
9840
9841 Scop := Scope (Scop);
9842 end loop;
9843
a77842bd 9844 -- Look for visible fixed type declarations in the context
996ae0b0
RK
9845
9846 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
996ae0b0 9847 while Present (Item) loop
996ae0b0
RK
9848 if Nkind (Item) = N_With_Clause then
9849 Scop := Entity (Name (Item));
9850 T2 := First_Entity (Scop);
996ae0b0
RK
9851 while Present (T2) loop
9852 if Is_Fixed_Point_Type (T2)
9853 and then Scope (Base_Type (T2)) = Scop
9854 and then (Is_Potentially_Use_Visible (T2)
9855 or else In_Use (T2))
9856 then
9857 if Present (T1) then
9858 Fixed_Point_Error;
9859 return Any_Type;
9860 else
9861 T1 := T2;
9862 end if;
9863 end if;
9864
9865 Next_Entity (T2);
9866 end loop;
9867 end if;
9868
9869 Next (Item);
9870 end loop;
9871
9872 if Nkind (N) = N_Real_Literal then
aa5147f0 9873 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
996ae0b0 9874 else
aa5147f0 9875 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
996ae0b0
RK
9876 end if;
9877
9878 return T1;
9879 end Unique_Fixed_Point_Type;
9880
9881 ----------------------
9882 -- Valid_Conversion --
9883 ----------------------
9884
9885 function Valid_Conversion
9886 (N : Node_Id;
9887 Target : Entity_Id;
0ab80019 9888 Operand : Node_Id) return Boolean
996ae0b0 9889 is
fbf5a39b 9890 Target_Type : constant Entity_Id := Base_Type (Target);
996ae0b0
RK
9891 Opnd_Type : Entity_Id := Etype (Operand);
9892
9893 function Conversion_Check
9894 (Valid : Boolean;
0ab80019 9895 Msg : String) return Boolean;
996ae0b0
RK
9896 -- Little routine to post Msg if Valid is False, returns Valid value
9897
9898 function Valid_Tagged_Conversion
9899 (Target_Type : Entity_Id;
0ab80019 9900 Opnd_Type : Entity_Id) return Boolean;
996ae0b0
RK
9901 -- Specifically test for validity of tagged conversions
9902
aa180613 9903 function Valid_Array_Conversion return Boolean;
4adf3c50
AC
9904 -- Check index and component conformance, and accessibility levels if
9905 -- the component types are anonymous access types (Ada 2005).
aa180613 9906
996ae0b0
RK
9907 ----------------------
9908 -- Conversion_Check --
9909 ----------------------
9910
9911 function Conversion_Check
9912 (Valid : Boolean;
0ab80019 9913 Msg : String) return Boolean
996ae0b0
RK
9914 is
9915 begin
9916 if not Valid then
9917 Error_Msg_N (Msg, Operand);
9918 end if;
9919
9920 return Valid;
9921 end Conversion_Check;
9922
aa180613
RD
9923 ----------------------------
9924 -- Valid_Array_Conversion --
9925 ----------------------------
9926
9927 function Valid_Array_Conversion return Boolean
9928 is
9929 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9930 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9931
9932 Opnd_Index : Node_Id;
9933 Opnd_Index_Type : Entity_Id;
9934
9935 Target_Comp_Type : constant Entity_Id :=
9936 Component_Type (Target_Type);
9937 Target_Comp_Base : constant Entity_Id :=
9938 Base_Type (Target_Comp_Type);
9939
9940 Target_Index : Node_Id;
9941 Target_Index_Type : Entity_Id;
9942
9943 begin
9944 -- Error if wrong number of dimensions
9945
9946 if
9947 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9948 then
9949 Error_Msg_N
9950 ("incompatible number of dimensions for conversion", Operand);
9951 return False;
9952
9953 -- Number of dimensions matches
9954
9955 else
9956 -- Loop through indexes of the two arrays
9957
9958 Target_Index := First_Index (Target_Type);
9959 Opnd_Index := First_Index (Opnd_Type);
9960 while Present (Target_Index) and then Present (Opnd_Index) loop
9961 Target_Index_Type := Etype (Target_Index);
9962 Opnd_Index_Type := Etype (Opnd_Index);
9963
9964 -- Error if index types are incompatible
9965
9966 if not (Is_Integer_Type (Target_Index_Type)
9967 and then Is_Integer_Type (Opnd_Index_Type))
9968 and then (Root_Type (Target_Index_Type)
9969 /= Root_Type (Opnd_Index_Type))
9970 then
9971 Error_Msg_N
9972 ("incompatible index types for array conversion",
9973 Operand);
9974 return False;
9975 end if;
9976
9977 Next_Index (Target_Index);
9978 Next_Index (Opnd_Index);
9979 end loop;
9980
9981 -- If component types have same base type, all set
9982
9983 if Target_Comp_Base = Opnd_Comp_Base then
9984 null;
9985
9986 -- Here if base types of components are not the same. The only
9987 -- time this is allowed is if we have anonymous access types.
9988
9989 -- The conversion of arrays of anonymous access types can lead
9990 -- to dangling pointers. AI-392 formalizes the accessibility
9991 -- checks that must be applied to such conversions to prevent
9992 -- out-of-scope references.
9993
9994 elsif
964f13da
RD
9995 Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type,
9996 E_Anonymous_Access_Subprogram_Type)
aa180613
RD
9997 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
9998 and then
9999 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10000 then
10001 if Type_Access_Level (Target_Type) <
10002 Type_Access_Level (Opnd_Type)
10003 then
10004 if In_Instance_Body then
10005 Error_Msg_N ("?source array type " &
10006 "has deeper accessibility level than target", Operand);
10007 Error_Msg_N ("\?Program_Error will be raised at run time",
10008 Operand);
10009 Rewrite (N,
10010 Make_Raise_Program_Error (Sloc (N),
10011 Reason => PE_Accessibility_Check_Failed));
10012 Set_Etype (N, Target_Type);
10013 return False;
10014
10015 -- Conversion not allowed because of accessibility levels
10016
10017 else
10018 Error_Msg_N ("source array type " &
10019 "has deeper accessibility level than target", Operand);
10020 return False;
10021 end if;
10022 else
10023 null;
10024 end if;
10025
10026 -- All other cases where component base types do not match
10027
10028 else
10029 Error_Msg_N
10030 ("incompatible component types for array conversion",
10031 Operand);
10032 return False;
10033 end if;
10034
45fc7ddb
HK
10035 -- Check that component subtypes statically match. For numeric
10036 -- types this means that both must be either constrained or
10037 -- unconstrained. For enumeration types the bounds must match.
10038 -- All of this is checked in Subtypes_Statically_Match.
aa180613 10039
45fc7ddb 10040 if not Subtypes_Statically_Match
aa180613
RD
10041 (Target_Comp_Type, Opnd_Comp_Type)
10042 then
10043 Error_Msg_N
10044 ("component subtypes must statically match", Operand);
10045 return False;
10046 end if;
10047 end if;
10048
10049 return True;
10050 end Valid_Array_Conversion;
10051
996ae0b0
RK
10052 -----------------------------
10053 -- Valid_Tagged_Conversion --
10054 -----------------------------
10055
10056 function Valid_Tagged_Conversion
10057 (Target_Type : Entity_Id;
0ab80019 10058 Opnd_Type : Entity_Id) return Boolean
996ae0b0
RK
10059 is
10060 begin
a77842bd 10061 -- Upward conversions are allowed (RM 4.6(22))
996ae0b0
RK
10062
10063 if Covers (Target_Type, Opnd_Type)
10064 or else Is_Ancestor (Target_Type, Opnd_Type)
10065 then
10066 return True;
10067
a77842bd
TQ
10068 -- Downward conversion are allowed if the operand is class-wide
10069 -- (RM 4.6(23)).
996ae0b0
RK
10070
10071 elsif Is_Class_Wide_Type (Opnd_Type)
b7d1f17f 10072 and then Covers (Opnd_Type, Target_Type)
996ae0b0
RK
10073 then
10074 return True;
10075
10076 elsif Covers (Opnd_Type, Target_Type)
10077 or else Is_Ancestor (Opnd_Type, Target_Type)
10078 then
10079 return
10080 Conversion_Check (False,
10081 "downward conversion of tagged objects not allowed");
758c442c 10082
0669bebe
GB
10083 -- Ada 2005 (AI-251): The conversion to/from interface types is
10084 -- always valid
758c442c 10085
0669bebe 10086 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
758c442c
GD
10087 return True;
10088
b7d1f17f
HK
10089 -- If the operand is a class-wide type obtained through a limited_
10090 -- with clause, and the context includes the non-limited view, use
10091 -- it to determine whether the conversion is legal.
10092
10093 elsif Is_Class_Wide_Type (Opnd_Type)
10094 and then From_With_Type (Opnd_Type)
10095 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10096 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10097 then
10098 return True;
10099
aa180613
RD
10100 elsif Is_Access_Type (Opnd_Type)
10101 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10102 then
10103 return True;
10104
996ae0b0
RK
10105 else
10106 Error_Msg_NE
10107 ("invalid tagged conversion, not compatible with}",
10108 N, First_Subtype (Opnd_Type));
10109 return False;
10110 end if;
10111 end Valid_Tagged_Conversion;
10112
10113 -- Start of processing for Valid_Conversion
10114
10115 begin
10116 Check_Parameterless_Call (Operand);
10117
10118 if Is_Overloaded (Operand) then
10119 declare
10120 I : Interp_Index;
10121 I1 : Interp_Index;
10122 It : Interp;
10123 It1 : Interp;
10124 N1 : Entity_Id;
f0d10385 10125 T1 : Entity_Id;
996ae0b0
RK
10126
10127 begin
d81b4bfe
TQ
10128 -- Remove procedure calls, which syntactically cannot appear in
10129 -- this context, but which cannot be removed by type checking,
996ae0b0
RK
10130 -- because the context does not impose a type.
10131
1420b484
JM
10132 -- When compiling for VMS, spurious ambiguities can be produced
10133 -- when arithmetic operations have a literal operand and return
10134 -- System.Address or a descendant of it. These ambiguities are
10135 -- otherwise resolved by the context, but for conversions there
10136 -- is no context type and the removal of the spurious operations
10137 -- must be done explicitly here.
10138
4adf3c50
AC
10139 -- The node may be labelled overloaded, but still contain only one
10140 -- interpretation because others were discarded earlier. If this
10141 -- is the case, retain the single interpretation if legal.
9ebe3743 10142
996ae0b0 10143 Get_First_Interp (Operand, I, It);
9ebe3743
HK
10144 Opnd_Type := It.Typ;
10145 Get_Next_Interp (I, It);
996ae0b0 10146
9ebe3743
HK
10147 if Present (It.Typ)
10148 and then Opnd_Type /= Standard_Void_Type
10149 then
10150 -- More than one candidate interpretation is available
996ae0b0 10151
9ebe3743
HK
10152 Get_First_Interp (Operand, I, It);
10153 while Present (It.Typ) loop
10154 if It.Typ = Standard_Void_Type then
10155 Remove_Interp (I);
10156 end if;
1420b484 10157
9ebe3743
HK
10158 if Present (System_Aux_Id)
10159 and then Is_Descendent_Of_Address (It.Typ)
10160 then
10161 Remove_Interp (I);
10162 end if;
10163
10164 Get_Next_Interp (I, It);
10165 end loop;
10166 end if;
996ae0b0
RK
10167
10168 Get_First_Interp (Operand, I, It);
10169 I1 := I;
10170 It1 := It;
10171
10172 if No (It.Typ) then
10173 Error_Msg_N ("illegal operand in conversion", Operand);
10174 return False;
10175 end if;
10176
10177 Get_Next_Interp (I, It);
10178
10179 if Present (It.Typ) then
10180 N1 := It1.Nam;
f0d10385 10181 T1 := It1.Typ;
996ae0b0
RK
10182 It1 := Disambiguate (Operand, I1, I, Any_Type);
10183
10184 if It1 = No_Interp then
10185 Error_Msg_N ("ambiguous operand in conversion", Operand);
10186
f0d10385
AC
10187 -- If the interpretation involves a standard operator, use
10188 -- the location of the type, which may be user-defined.
10189
10190 if Sloc (It.Nam) = Standard_Location then
10191 Error_Msg_Sloc := Sloc (It.Typ);
10192 else
10193 Error_Msg_Sloc := Sloc (It.Nam);
10194 end if;
10195
4e7a4f6e
AC
10196 Error_Msg_N -- CODEFIX
10197 ("\\possible interpretation#!", Operand);
996ae0b0 10198
f0d10385
AC
10199 if Sloc (N1) = Standard_Location then
10200 Error_Msg_Sloc := Sloc (T1);
10201 else
10202 Error_Msg_Sloc := Sloc (N1);
10203 end if;
10204
4e7a4f6e
AC
10205 Error_Msg_N -- CODEFIX
10206 ("\\possible interpretation#!", Operand);
996ae0b0
RK
10207
10208 return False;
10209 end if;
10210 end if;
10211
10212 Set_Etype (Operand, It1.Typ);
10213 Opnd_Type := It1.Typ;
10214 end;
10215 end if;
10216
aa180613 10217 -- Numeric types
996ae0b0 10218
aa180613 10219 if Is_Numeric_Type (Target_Type) then
996ae0b0 10220
aa180613 10221 -- A universal fixed expression can be converted to any numeric type
996ae0b0 10222
996ae0b0
RK
10223 if Opnd_Type = Universal_Fixed then
10224 return True;
7324bf49 10225
aa180613
RD
10226 -- Also no need to check when in an instance or inlined body, because
10227 -- the legality has been established when the template was analyzed.
10228 -- Furthermore, numeric conversions may occur where only a private
f3d57416 10229 -- view of the operand type is visible at the instantiation point.
aa180613
RD
10230 -- This results in a spurious error if we check that the operand type
10231 -- is a numeric type.
10232
10233 -- Note: in a previous version of this unit, the following tests were
10234 -- applied only for generated code (Comes_From_Source set to False),
10235 -- but in fact the test is required for source code as well, since
10236 -- this situation can arise in source code.
10237
10238 elsif In_Instance or else In_Inlined_Body then
d347f572 10239 return True;
aa180613
RD
10240
10241 -- Otherwise we need the conversion check
7324bf49 10242
996ae0b0 10243 else
aa180613
RD
10244 return Conversion_Check
10245 (Is_Numeric_Type (Opnd_Type),
10246 "illegal operand for numeric conversion");
996ae0b0
RK
10247 end if;
10248
aa180613
RD
10249 -- Array types
10250
996ae0b0
RK
10251 elsif Is_Array_Type (Target_Type) then
10252 if not Is_Array_Type (Opnd_Type)
10253 or else Opnd_Type = Any_Composite
10254 or else Opnd_Type = Any_String
10255 then
4adf3c50 10256 Error_Msg_N ("illegal operand for array conversion", Operand);
996ae0b0 10257 return False;
996ae0b0 10258 else
aa180613 10259 return Valid_Array_Conversion;
996ae0b0
RK
10260 end if;
10261
e65f50ec
ES
10262 -- Ada 2005 (AI-251): Anonymous access types where target references an
10263 -- interface type.
758c442c 10264
964f13da
RD
10265 elsif Ekind_In (Target_Type, E_General_Access_Type,
10266 E_Anonymous_Access_Type)
758c442c
GD
10267 and then Is_Interface (Directly_Designated_Type (Target_Type))
10268 then
10269 -- Check the static accessibility rule of 4.6(17). Note that the
d81b4bfe
TQ
10270 -- check is not enforced when within an instance body, since the
10271 -- RM requires such cases to be caught at run time.
758c442c
GD
10272
10273 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10274 if Type_Access_Level (Opnd_Type) >
10275 Type_Access_Level (Target_Type)
10276 then
10277 -- In an instance, this is a run-time check, but one we know
10278 -- will fail, so generate an appropriate warning. The raise
10279 -- will be generated by Expand_N_Type_Conversion.
10280
10281 if In_Instance_Body then
10282 Error_Msg_N
10283 ("?cannot convert local pointer to non-local access type",
10284 Operand);
10285 Error_Msg_N
c8ef728f 10286 ("\?Program_Error will be raised at run time", Operand);
758c442c
GD
10287 else
10288 Error_Msg_N
10289 ("cannot convert local pointer to non-local access type",
10290 Operand);
10291 return False;
10292 end if;
10293
10294 -- Special accessibility checks are needed in the case of access
10295 -- discriminants declared for a limited type.
10296
10297 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10298 and then not Is_Local_Anonymous_Access (Opnd_Type)
10299 then
10300 -- When the operand is a selected access discriminant the check
10301 -- needs to be made against the level of the object denoted by
d81b4bfe
TQ
10302 -- the prefix of the selected name (Object_Access_Level handles
10303 -- checking the prefix of the operand for this case).
758c442c
GD
10304
10305 if Nkind (Operand) = N_Selected_Component
c8ef728f 10306 and then Object_Access_Level (Operand) >
45fc7ddb 10307 Type_Access_Level (Target_Type)
758c442c 10308 then
d81b4bfe
TQ
10309 -- In an instance, this is a run-time check, but one we know
10310 -- will fail, so generate an appropriate warning. The raise
10311 -- will be generated by Expand_N_Type_Conversion.
758c442c
GD
10312
10313 if In_Instance_Body then
10314 Error_Msg_N
10315 ("?cannot convert access discriminant to non-local" &
10316 " access type", Operand);
10317 Error_Msg_N
c8ef728f 10318 ("\?Program_Error will be raised at run time", Operand);
758c442c
GD
10319 else
10320 Error_Msg_N
10321 ("cannot convert access discriminant to non-local" &
10322 " access type", Operand);
10323 return False;
10324 end if;
10325 end if;
10326
10327 -- The case of a reference to an access discriminant from
10328 -- within a limited type declaration (which will appear as
10329 -- a discriminal) is always illegal because the level of the
f3d57416 10330 -- discriminant is considered to be deeper than any (nameable)
758c442c
GD
10331 -- access type.
10332
10333 if Is_Entity_Name (Operand)
10334 and then not Is_Local_Anonymous_Access (Opnd_Type)
964f13da
RD
10335 and then
10336 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
758c442c
GD
10337 and then Present (Discriminal_Link (Entity (Operand)))
10338 then
10339 Error_Msg_N
10340 ("discriminant has deeper accessibility level than target",
10341 Operand);
10342 return False;
10343 end if;
10344 end if;
10345 end if;
10346
10347 return True;
10348
aa180613
RD
10349 -- General and anonymous access types
10350
964f13da
RD
10351 elsif Ekind_In (Target_Type, E_General_Access_Type,
10352 E_Anonymous_Access_Type)
996ae0b0
RK
10353 and then
10354 Conversion_Check
10355 (Is_Access_Type (Opnd_Type)
964f13da
RD
10356 and then not
10357 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10358 E_Access_Protected_Subprogram_Type),
996ae0b0
RK
10359 "must be an access-to-object type")
10360 then
10361 if Is_Access_Constant (Opnd_Type)
10362 and then not Is_Access_Constant (Target_Type)
10363 then
10364 Error_Msg_N
10365 ("access-to-constant operand type not allowed", Operand);
10366 return False;
10367 end if;
10368
758c442c
GD
10369 -- Check the static accessibility rule of 4.6(17). Note that the
10370 -- check is not enforced when within an instance body, since the RM
10371 -- requires such cases to be caught at run time.
996ae0b0 10372
758c442c
GD
10373 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10374 or else Is_Local_Anonymous_Access (Target_Type)
10375 then
996ae0b0
RK
10376 if Type_Access_Level (Opnd_Type)
10377 > Type_Access_Level (Target_Type)
10378 then
d81b4bfe
TQ
10379 -- In an instance, this is a run-time check, but one we know
10380 -- will fail, so generate an appropriate warning. The raise
10381 -- will be generated by Expand_N_Type_Conversion.
996ae0b0
RK
10382
10383 if In_Instance_Body then
10384 Error_Msg_N
10385 ("?cannot convert local pointer to non-local access type",
10386 Operand);
10387 Error_Msg_N
c8ef728f 10388 ("\?Program_Error will be raised at run time", Operand);
996ae0b0
RK
10389
10390 else
b90cfacd
HK
10391 -- Avoid generation of spurious error message
10392
10393 if not Error_Posted (N) then
10394 Error_Msg_N
10395 ("cannot convert local pointer to non-local access type",
10396 Operand);
10397 end if;
10398
996ae0b0
RK
10399 return False;
10400 end if;
10401
758c442c
GD
10402 -- Special accessibility checks are needed in the case of access
10403 -- discriminants declared for a limited type.
10404
10405 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10406 and then not Is_Local_Anonymous_Access (Opnd_Type)
10407 then
758c442c
GD
10408 -- When the operand is a selected access discriminant the check
10409 -- needs to be made against the level of the object denoted by
d81b4bfe
TQ
10410 -- the prefix of the selected name (Object_Access_Level handles
10411 -- checking the prefix of the operand for this case).
996ae0b0
RK
10412
10413 if Nkind (Operand) = N_Selected_Component
45fc7ddb
HK
10414 and then Object_Access_Level (Operand) >
10415 Type_Access_Level (Target_Type)
996ae0b0 10416 then
d81b4bfe
TQ
10417 -- In an instance, this is a run-time check, but one we know
10418 -- will fail, so generate an appropriate warning. The raise
10419 -- will be generated by Expand_N_Type_Conversion.
996ae0b0
RK
10420
10421 if In_Instance_Body then
10422 Error_Msg_N
10423 ("?cannot convert access discriminant to non-local" &
10424 " access type", Operand);
10425 Error_Msg_N
c8ef728f
ES
10426 ("\?Program_Error will be raised at run time",
10427 Operand);
996ae0b0
RK
10428
10429 else
10430 Error_Msg_N
10431 ("cannot convert access discriminant to non-local" &
10432 " access type", Operand);
10433 return False;
10434 end if;
10435 end if;
10436
758c442c
GD
10437 -- The case of a reference to an access discriminant from
10438 -- within a limited type declaration (which will appear as
10439 -- a discriminal) is always illegal because the level of the
f3d57416 10440 -- discriminant is considered to be deeper than any (nameable)
758c442c 10441 -- access type.
996ae0b0
RK
10442
10443 if Is_Entity_Name (Operand)
964f13da
RD
10444 and then
10445 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
996ae0b0
RK
10446 and then Present (Discriminal_Link (Entity (Operand)))
10447 then
10448 Error_Msg_N
10449 ("discriminant has deeper accessibility level than target",
10450 Operand);
10451 return False;
10452 end if;
10453 end if;
10454 end if;
10455
14e33999
AC
10456 -- In the presence of limited_with clauses we have to use non-limited
10457 -- views, if available.
d81b4bfe 10458
14e33999 10459 Check_Limited : declare
0669bebe
GB
10460 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10461 -- Helper function to handle limited views
10462
10463 --------------------------
10464 -- Full_Designated_Type --
10465 --------------------------
10466
10467 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
950d217a 10468 Desig : constant Entity_Id := Designated_Type (T);
c0985d4e 10469
0669bebe 10470 begin
950d217a
AC
10471 -- Handle the limited view of a type
10472
c0985d4e
HK
10473 if Is_Incomplete_Type (Desig)
10474 and then From_With_Type (Desig)
0669bebe
GB
10475 and then Present (Non_Limited_View (Desig))
10476 then
950d217a
AC
10477 return Available_View (Desig);
10478 else
10479 return Desig;
0669bebe
GB
10480 end if;
10481 end Full_Designated_Type;
10482
d81b4bfe
TQ
10483 -- Local Declarations
10484
0669bebe
GB
10485 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10486 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10487
10488 Same_Base : constant Boolean :=
10489 Base_Type (Target) = Base_Type (Opnd);
996ae0b0 10490
14e33999 10491 -- Start of processing for Check_Limited
d81b4bfe 10492
996ae0b0
RK
10493 begin
10494 if Is_Tagged_Type (Target) then
10495 return Valid_Tagged_Conversion (Target, Opnd);
10496
10497 else
0669bebe 10498 if not Same_Base then
996ae0b0
RK
10499 Error_Msg_NE
10500 ("target designated type not compatible with }",
10501 N, Base_Type (Opnd));
10502 return False;
10503
da709d08
AC
10504 -- Ada 2005 AI-384: legality rule is symmetric in both
10505 -- designated types. The conversion is legal (with possible
10506 -- constraint check) if either designated type is
10507 -- unconstrained.
10508
10509 elsif Subtypes_Statically_Match (Target, Opnd)
10510 or else
10511 (Has_Discriminants (Target)
10512 and then
10513 (not Is_Constrained (Opnd)
10514 or else not Is_Constrained (Target)))
996ae0b0 10515 then
9fa33291
RD
10516 -- Special case, if Value_Size has been used to make the
10517 -- sizes different, the conversion is not allowed even
10518 -- though the subtypes statically match.
10519
10520 if Known_Static_RM_Size (Target)
10521 and then Known_Static_RM_Size (Opnd)
10522 and then RM_Size (Target) /= RM_Size (Opnd)
10523 then
10524 Error_Msg_NE
10525 ("target designated subtype not compatible with }",
10526 N, Opnd);
10527 Error_Msg_NE
10528 ("\because sizes of the two designated subtypes differ",
10529 N, Opnd);
10530 return False;
10531
10532 -- Normal case where conversion is allowed
10533
10534 else
10535 return True;
10536 end if;
da709d08
AC
10537
10538 else
996ae0b0
RK
10539 Error_Msg_NE
10540 ("target designated subtype not compatible with }",
10541 N, Opnd);
10542 return False;
996ae0b0
RK
10543 end if;
10544 end if;
14e33999 10545 end Check_Limited;
996ae0b0 10546
cdbf04c0 10547 -- Access to subprogram types. If the operand is an access parameter,
4adf3c50
AC
10548 -- the type has a deeper accessibility that any master, and cannot be
10549 -- assigned. We must make an exception if the conversion is part of an
10550 -- assignment and the target is the return object of an extended return
10551 -- statement, because in that case the accessibility check takes place
10552 -- after the return.
aa180613 10553
dce86910 10554 elsif Is_Access_Subprogram_Type (Target_Type)
bc5f3720 10555 and then No (Corresponding_Remote_Type (Opnd_Type))
996ae0b0 10556 then
cdbf04c0
AC
10557 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10558 and then Is_Entity_Name (Operand)
10559 and then Ekind (Entity (Operand)) = E_In_Parameter
53cf4600
ES
10560 and then
10561 (Nkind (Parent (N)) /= N_Assignment_Statement
10562 or else not Is_Entity_Name (Name (Parent (N)))
10563 or else not Is_Return_Object (Entity (Name (Parent (N)))))
0669bebe
GB
10564 then
10565 Error_Msg_N
10566 ("illegal attempt to store anonymous access to subprogram",
10567 Operand);
10568 Error_Msg_N
10569 ("\value has deeper accessibility than any master " &
aa5147f0 10570 "(RM 3.10.2 (13))",
0669bebe
GB
10571 Operand);
10572
c147ac26
ES
10573 Error_Msg_NE
10574 ("\use named access type for& instead of access parameter",
10575 Operand, Entity (Operand));
0669bebe
GB
10576 end if;
10577
996ae0b0
RK
10578 -- Check that the designated types are subtype conformant
10579
bc5f3720
RD
10580 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10581 Old_Id => Designated_Type (Opnd_Type),
10582 Err_Loc => N);
996ae0b0
RK
10583
10584 -- Check the static accessibility rule of 4.6(20)
10585
10586 if Type_Access_Level (Opnd_Type) >
10587 Type_Access_Level (Target_Type)
10588 then
10589 Error_Msg_N
10590 ("operand type has deeper accessibility level than target",
10591 Operand);
10592
10593 -- Check that if the operand type is declared in a generic body,
10594 -- then the target type must be declared within that same body
10595 -- (enforces last sentence of 4.6(20)).
10596
10597 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10598 declare
10599 O_Gen : constant Node_Id :=
10600 Enclosing_Generic_Body (Opnd_Type);
10601
1420b484 10602 T_Gen : Node_Id;
996ae0b0
RK
10603
10604 begin
1420b484 10605 T_Gen := Enclosing_Generic_Body (Target_Type);
996ae0b0
RK
10606 while Present (T_Gen) and then T_Gen /= O_Gen loop
10607 T_Gen := Enclosing_Generic_Body (T_Gen);
10608 end loop;
10609
10610 if T_Gen /= O_Gen then
10611 Error_Msg_N
10612 ("target type must be declared in same generic body"
10613 & " as operand type", N);
10614 end if;
10615 end;
10616 end if;
10617
10618 return True;
10619
aa180613
RD
10620 -- Remote subprogram access types
10621
996ae0b0
RK
10622 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10623 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10624 then
10625 -- It is valid to convert from one RAS type to another provided
10626 -- that their specification statically match.
10627
10628 Check_Subtype_Conformant
10629 (New_Id =>
10630 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10631 Old_Id =>
10632 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10633 Err_Loc =>
10634 N);
10635 return True;
aa180613 10636
e65f50ec 10637 -- If both are tagged types, check legality of view conversions
996ae0b0 10638
e65f50ec 10639 elsif Is_Tagged_Type (Target_Type)
4adf3c50
AC
10640 and then
10641 Is_Tagged_Type (Opnd_Type)
e65f50ec 10642 then
996ae0b0
RK
10643 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10644
a77842bd 10645 -- Types derived from the same root type are convertible
996ae0b0
RK
10646
10647 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10648 return True;
10649
4adf3c50
AC
10650 -- In an instance or an inlined body, there may be inconsistent views of
10651 -- the same type, or of types derived from a common root.
996ae0b0 10652
aa5147f0
ES
10653 elsif (In_Instance or In_Inlined_Body)
10654 and then
d81b4bfe
TQ
10655 Root_Type (Underlying_Type (Target_Type)) =
10656 Root_Type (Underlying_Type (Opnd_Type))
996ae0b0
RK
10657 then
10658 return True;
10659
10660 -- Special check for common access type error case
10661
10662 elsif Ekind (Target_Type) = E_Access_Type
10663 and then Is_Access_Type (Opnd_Type)
10664 then
10665 Error_Msg_N ("target type must be general access type!", N);
305caf42
AC
10666 Error_Msg_NE -- CODEFIX
10667 ("add ALL to }!", N, Target_Type);
996ae0b0
RK
10668 return False;
10669
10670 else
10671 Error_Msg_NE ("invalid conversion, not compatible with }",
10672 N, Opnd_Type);
996ae0b0
RK
10673 return False;
10674 end if;
10675 end Valid_Conversion;
10676
10677end Sem_Res;
This page took 4.431897 seconds and 5 git commands to generate.