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