]>
Commit | Line | Data |
---|---|---|
996ae0b0 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ R E S -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
a52fefe6 | 9 | -- Copyright (C) 1992-2009, 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 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
28 | with Debug; use Debug; | |
29 | with Debug_A; use Debug_A; | |
30 | with Einfo; use Einfo; | |
b7d1f17f | 31 | with Elists; use Elists; |
996ae0b0 RK |
32 | with Errout; use Errout; |
33 | with Expander; use Expander; | |
758c442c | 34 | with Exp_Disp; use Exp_Disp; |
0669bebe | 35 | with Exp_Ch6; use Exp_Ch6; |
996ae0b0 | 36 | with Exp_Ch7; use Exp_Ch7; |
fbf5a39b | 37 | with Exp_Tss; use Exp_Tss; |
996ae0b0 | 38 | with Exp_Util; use Exp_Util; |
dae2b8ea | 39 | with Fname; use Fname; |
996ae0b0 RK |
40 | with Freeze; use Freeze; |
41 | with Itypes; use Itypes; | |
42 | with Lib; use Lib; | |
43 | with Lib.Xref; use Lib.Xref; | |
44 | with Namet; use Namet; | |
45 | with Nmake; use Nmake; | |
46 | with Nlists; use Nlists; | |
47 | with Opt; use Opt; | |
48 | with Output; use Output; | |
49 | with Restrict; use Restrict; | |
6e937c1c | 50 | with Rident; use Rident; |
996ae0b0 RK |
51 | with Rtsfind; use Rtsfind; |
52 | with Sem; use Sem; | |
a4100e55 | 53 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
54 | with Sem_Aggr; use Sem_Aggr; |
55 | with Sem_Attr; use Sem_Attr; | |
56 | with Sem_Cat; use Sem_Cat; | |
57 | with Sem_Ch4; use Sem_Ch4; | |
58 | with Sem_Ch6; use Sem_Ch6; | |
59 | with Sem_Ch8; use Sem_Ch8; | |
4b92fd3c | 60 | with Sem_Ch13; use Sem_Ch13; |
996ae0b0 RK |
61 | with Sem_Disp; use Sem_Disp; |
62 | with Sem_Dist; use Sem_Dist; | |
16212e89 | 63 | with Sem_Elim; use Sem_Elim; |
996ae0b0 RK |
64 | with Sem_Elab; use Sem_Elab; |
65 | with Sem_Eval; use Sem_Eval; | |
66 | with Sem_Intr; use Sem_Intr; | |
67 | with Sem_Util; use Sem_Util; | |
68 | with Sem_Type; use Sem_Type; | |
69 | with Sem_Warn; use Sem_Warn; | |
70 | with Sinfo; use Sinfo; | |
fbf5a39b | 71 | with Snames; use Snames; |
996ae0b0 RK |
72 | with Stand; use Stand; |
73 | with Stringt; use Stringt; | |
45fc7ddb | 74 | with Style; use Style; |
996ae0b0 RK |
75 | with Tbuild; use Tbuild; |
76 | with Uintp; use Uintp; | |
77 | with Urealp; use Urealp; | |
78 | ||
79 | package body Sem_Res is | |
80 | ||
81 | ----------------------- | |
82 | -- Local Subprograms -- | |
83 | ----------------------- | |
84 | ||
85 | -- Second pass (top-down) type checking and overload resolution procedures | |
86 | -- Typ is the type required by context. These procedures propagate the | |
87 | -- type information recursively to the descendants of N. If the node | |
88 | -- is not overloaded, its Etype is established in the first pass. If | |
89 | -- overloaded, the Resolve routines set the correct type. For arith. | |
90 | -- operators, the Etype is the base type of the context. | |
91 | ||
92 | -- Note that Resolve_Attribute is separated off in Sem_Attr | |
93 | ||
996ae0b0 RK |
94 | procedure Check_Discriminant_Use (N : Node_Id); |
95 | -- Enforce the restrictions on the use of discriminants when constraining | |
96 | -- a component of a discriminated type (record or concurrent type). | |
97 | ||
98 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id); | |
99 | -- Given a node for an operator associated with type T, check that | |
100 | -- the operator is visible. Operators all of whose operands are | |
101 | -- universal must be checked for visibility during resolution | |
102 | -- because their type is not determinable based on their operands. | |
103 | ||
c8ef728f ES |
104 | procedure Check_Fully_Declared_Prefix |
105 | (Typ : Entity_Id; | |
106 | Pref : Node_Id); | |
107 | -- Check that the type of the prefix of a dereference is not incomplete | |
108 | ||
996ae0b0 RK |
109 | function Check_Infinite_Recursion (N : Node_Id) return Boolean; |
110 | -- Given a call node, N, which is known to occur immediately within the | |
111 | -- subprogram being called, determines whether it is a detectable case of | |
112 | -- an infinite recursion, and if so, outputs appropriate messages. Returns | |
113 | -- True if an infinite recursion is detected, and False otherwise. | |
114 | ||
115 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id); | |
116 | -- If the type of the object being initialized uses the secondary stack | |
117 | -- directly or indirectly, create a transient scope for the call to the | |
fbf5a39b AC |
118 | -- init proc. This is because we do not create transient scopes for the |
119 | -- initialization of individual components within the init proc itself. | |
996ae0b0 RK |
120 | -- Could be optimized away perhaps? |
121 | ||
f61580d4 AC |
122 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id); |
123 | -- N is the node for a comparison or logical operator. If the operator | |
124 | -- is predefined, and the root type of the operands is Standard.Boolean, | |
125 | -- then a check is made for restriction No_Direct_Boolean_Operators. | |
126 | ||
67ce0d7e RD |
127 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean; |
128 | -- Determine whether E is an access type declared by an access | |
129 | -- declaration, and not an (anonymous) allocator type. | |
130 | ||
996ae0b0 RK |
131 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean; |
132 | -- Utility to check whether the name in the call is a predefined | |
133 | -- operator, in which case the call is made into an operator node. | |
134 | -- An instance of an intrinsic conversion operation may be given | |
135 | -- an operator name, but is not treated like an operator. | |
136 | ||
137 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id); | |
138 | -- If a default expression in entry call N depends on the discriminants | |
139 | -- of the task, it must be replaced with a reference to the discriminant | |
140 | -- of the task being called. | |
141 | ||
10303118 BD |
142 | procedure Resolve_Op_Concat_Arg |
143 | (N : Node_Id; | |
144 | Arg : Node_Id; | |
145 | Typ : Entity_Id; | |
146 | Is_Comp : Boolean); | |
147 | -- Internal procedure for Resolve_Op_Concat to resolve one operand of | |
148 | -- concatenation operator. The operand is either of the array type or of | |
149 | -- the component type. If the operand is an aggregate, and the component | |
150 | -- type is composite, this is ambiguous if component type has aggregates. | |
151 | ||
152 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id); | |
153 | -- Does the first part of the work of Resolve_Op_Concat | |
154 | ||
155 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id); | |
156 | -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand | |
157 | -- has been resolved. See Resolve_Op_Concat for details. | |
158 | ||
996ae0b0 RK |
159 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id); |
160 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id); | |
161 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id); | |
162 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id); | |
163 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id); | |
164 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id); | |
165 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id); | |
166 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id); | |
167 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id); | |
168 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id); | |
169 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id); | |
170 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id); | |
171 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id); | |
172 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id); | |
173 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id); | |
174 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id); | |
175 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id); | |
176 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id); | |
177 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id); | |
178 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id); | |
179 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id); | |
180 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id); | |
181 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id); | |
182 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id); | |
183 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id); | |
184 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id); | |
185 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id); | |
186 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id); | |
187 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id); | |
188 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id); | |
189 | procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id); | |
190 | procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id); | |
191 | ||
192 | function Operator_Kind | |
193 | (Op_Name : Name_Id; | |
0ab80019 | 194 | Is_Binary : Boolean) return Node_Kind; |
996ae0b0 RK |
195 | -- Utility to map the name of an operator into the corresponding Node. Used |
196 | -- by other node rewriting procedures. | |
197 | ||
198 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id); | |
bc5f3720 RD |
199 | -- Resolve actuals of call, and add default expressions for missing ones. |
200 | -- N is the Node_Id for the subprogram call, and Nam is the entity of the | |
201 | -- called subprogram. | |
996ae0b0 RK |
202 | |
203 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id); | |
204 | -- Called from Resolve_Call, when the prefix denotes an entry or element | |
205 | -- of entry family. Actuals are resolved as for subprograms, and the node | |
206 | -- is rebuilt as an entry call. Also called for protected operations. Typ | |
207 | -- is the context type, which is used when the operation is a protected | |
208 | -- function with no arguments, and the return value is indexed. | |
209 | ||
210 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id); | |
211 | -- A call to a user-defined intrinsic operator is rewritten as a call | |
212 | -- to the corresponding predefined operator, with suitable conversions. | |
213 | ||
fbf5a39b | 214 | procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id); |
a77842bd | 215 | -- Ditto, for unary operators (only arithmetic ones) |
fbf5a39b | 216 | |
996ae0b0 RK |
217 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id); |
218 | -- If an operator node resolves to a call to a user-defined operator, | |
219 | -- rewrite the node as a function call. | |
220 | ||
221 | procedure Make_Call_Into_Operator | |
222 | (N : Node_Id; | |
223 | Typ : Entity_Id; | |
224 | Op_Id : Entity_Id); | |
225 | -- Inverse transformation: if an operator is given in functional notation, | |
226 | -- then after resolving the node, transform into an operator node, so | |
227 | -- that operands are resolved properly. Recall that predefined operators | |
228 | -- do not have a full signature and special resolution rules apply. | |
229 | ||
0ab80019 AC |
230 | procedure Rewrite_Renamed_Operator |
231 | (N : Node_Id; | |
232 | Op : Entity_Id; | |
233 | Typ : Entity_Id); | |
996ae0b0 | 234 | -- An operator can rename another, e.g. in an instantiation. In that |
0ab80019 | 235 | -- case, the proper operator node must be constructed and resolved. |
996ae0b0 RK |
236 | |
237 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id); | |
238 | -- The String_Literal_Subtype is built for all strings that are not | |
07fc65c4 GB |
239 | -- operands of a static concatenation operation. If the argument is |
240 | -- not a N_String_Literal node, then the call has no effect. | |
996ae0b0 RK |
241 | |
242 | procedure Set_Slice_Subtype (N : Node_Id); | |
fbf5a39b | 243 | -- Build subtype of array type, with the range specified by the slice |
996ae0b0 | 244 | |
0669bebe GB |
245 | procedure Simplify_Type_Conversion (N : Node_Id); |
246 | -- Called after N has been resolved and evaluated, but before range checks | |
247 | -- have been applied. Currently simplifies a combination of floating-point | |
248 | -- to integer conversion and Truncation attribute. | |
249 | ||
996ae0b0 | 250 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id; |
07fc65c4 GB |
251 | -- A universal_fixed expression in an universal context is unambiguous |
252 | -- if there is only one applicable fixed point type. Determining whether | |
996ae0b0 RK |
253 | -- there is only one requires a search over all visible entities, and |
254 | -- happens only in very pathological cases (see 6115-006). | |
255 | ||
256 | function Valid_Conversion | |
257 | (N : Node_Id; | |
258 | Target : Entity_Id; | |
0ab80019 | 259 | Operand : Node_Id) return Boolean; |
996ae0b0 RK |
260 | -- Verify legality rules given in 4.6 (8-23). Target is the target |
261 | -- type of the conversion, which may be an implicit conversion of | |
262 | -- an actual parameter to an anonymous access type (in which case | |
263 | -- N denotes the actual parameter and N = Operand). | |
264 | ||
265 | ------------------------- | |
266 | -- Ambiguous_Character -- | |
267 | ------------------------- | |
268 | ||
269 | procedure Ambiguous_Character (C : Node_Id) is | |
270 | E : Entity_Id; | |
271 | ||
272 | begin | |
273 | if Nkind (C) = N_Character_Literal then | |
274 | Error_Msg_N ("ambiguous character literal", C); | |
b7d1f17f HK |
275 | |
276 | -- First the ones in Standard | |
277 | ||
996ae0b0 | 278 | Error_Msg_N |
b7d1f17f HK |
279 | ("\\possible interpretation: Character!", C); |
280 | Error_Msg_N | |
281 | ("\\possible interpretation: Wide_Character!", C); | |
282 | ||
283 | -- Include Wide_Wide_Character in Ada 2005 mode | |
284 | ||
285 | if Ada_Version >= Ada_05 then | |
286 | Error_Msg_N | |
287 | ("\\possible interpretation: Wide_Wide_Character!", C); | |
288 | end if; | |
289 | ||
290 | -- Now any other types that match | |
996ae0b0 RK |
291 | |
292 | E := Current_Entity (C); | |
1420b484 | 293 | while Present (E) loop |
aa180613 | 294 | Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E)); |
1420b484 JM |
295 | E := Homonym (E); |
296 | end loop; | |
996ae0b0 RK |
297 | end if; |
298 | end Ambiguous_Character; | |
299 | ||
300 | ------------------------- | |
301 | -- Analyze_And_Resolve -- | |
302 | ------------------------- | |
303 | ||
304 | procedure Analyze_And_Resolve (N : Node_Id) is | |
305 | begin | |
306 | Analyze (N); | |
fbf5a39b | 307 | Resolve (N); |
996ae0b0 RK |
308 | end Analyze_And_Resolve; |
309 | ||
310 | procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is | |
311 | begin | |
312 | Analyze (N); | |
313 | Resolve (N, Typ); | |
314 | end Analyze_And_Resolve; | |
315 | ||
316 | -- Version withs check(s) suppressed | |
317 | ||
318 | procedure Analyze_And_Resolve | |
319 | (N : Node_Id; | |
320 | Typ : Entity_Id; | |
321 | Suppress : Check_Id) | |
322 | is | |
fbf5a39b | 323 | Scop : constant Entity_Id := Current_Scope; |
996ae0b0 RK |
324 | |
325 | begin | |
326 | if Suppress = All_Checks then | |
327 | declare | |
fbf5a39b | 328 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
329 | begin |
330 | Scope_Suppress := (others => True); | |
331 | Analyze_And_Resolve (N, Typ); | |
332 | Scope_Suppress := Svg; | |
333 | end; | |
334 | ||
335 | else | |
336 | declare | |
fbf5a39b | 337 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 RK |
338 | |
339 | begin | |
fbf5a39b | 340 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 341 | Analyze_And_Resolve (N, Typ); |
fbf5a39b | 342 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
343 | end; |
344 | end if; | |
345 | ||
346 | if Current_Scope /= Scop | |
347 | and then Scope_Is_Transient | |
348 | then | |
349 | -- This can only happen if a transient scope was created | |
350 | -- for an inner expression, which will be removed upon | |
351 | -- completion of the analysis of an enclosing construct. | |
352 | -- The transient scope must have the suppress status of | |
353 | -- the enclosing environment, not of this Analyze call. | |
354 | ||
355 | Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress := | |
356 | Scope_Suppress; | |
357 | end if; | |
358 | end Analyze_And_Resolve; | |
359 | ||
360 | procedure Analyze_And_Resolve | |
361 | (N : Node_Id; | |
362 | Suppress : Check_Id) | |
363 | is | |
fbf5a39b | 364 | Scop : constant Entity_Id := Current_Scope; |
996ae0b0 RK |
365 | |
366 | begin | |
367 | if Suppress = All_Checks then | |
368 | declare | |
fbf5a39b | 369 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
370 | begin |
371 | Scope_Suppress := (others => True); | |
372 | Analyze_And_Resolve (N); | |
373 | Scope_Suppress := Svg; | |
374 | end; | |
375 | ||
376 | else | |
377 | declare | |
fbf5a39b | 378 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 RK |
379 | |
380 | begin | |
fbf5a39b | 381 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 382 | Analyze_And_Resolve (N); |
fbf5a39b | 383 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
384 | end; |
385 | end if; | |
386 | ||
387 | if Current_Scope /= Scop | |
388 | and then Scope_Is_Transient | |
389 | then | |
390 | Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress := | |
391 | Scope_Suppress; | |
392 | end if; | |
393 | end Analyze_And_Resolve; | |
394 | ||
395 | ---------------------------- | |
396 | -- Check_Discriminant_Use -- | |
397 | ---------------------------- | |
398 | ||
399 | procedure Check_Discriminant_Use (N : Node_Id) is | |
400 | PN : constant Node_Id := Parent (N); | |
401 | Disc : constant Entity_Id := Entity (N); | |
402 | P : Node_Id; | |
403 | D : Node_Id; | |
404 | ||
405 | begin | |
f3d0f304 | 406 | -- Any use in a spec-expression is legal |
996ae0b0 | 407 | |
45fc7ddb | 408 | if In_Spec_Expression then |
996ae0b0 RK |
409 | null; |
410 | ||
411 | elsif Nkind (PN) = N_Range then | |
412 | ||
a77842bd | 413 | -- Discriminant cannot be used to constrain a scalar type |
996ae0b0 RK |
414 | |
415 | P := Parent (PN); | |
416 | ||
417 | if Nkind (P) = N_Range_Constraint | |
418 | and then Nkind (Parent (P)) = N_Subtype_Indication | |
a397db96 | 419 | and then Nkind (Parent (Parent (P))) = N_Component_Definition |
996ae0b0 RK |
420 | then |
421 | Error_Msg_N ("discriminant cannot constrain scalar type", N); | |
422 | ||
423 | elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then | |
424 | ||
425 | -- The following check catches the unusual case where | |
426 | -- a discriminant appears within an index constraint | |
427 | -- that is part of a larger expression within a constraint | |
428 | -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))". | |
429 | -- For now we only check case of record components, and | |
430 | -- note that a similar check should also apply in the | |
431 | -- case of discriminant constraints below. ??? | |
432 | ||
433 | -- Note that the check for N_Subtype_Declaration below is to | |
434 | -- detect the valid use of discriminants in the constraints of a | |
435 | -- subtype declaration when this subtype declaration appears | |
436 | -- inside the scope of a record type (which is syntactically | |
437 | -- illegal, but which may be created as part of derived type | |
438 | -- processing for records). See Sem_Ch3.Build_Derived_Record_Type | |
439 | -- for more info. | |
440 | ||
441 | if Ekind (Current_Scope) = E_Record_Type | |
442 | and then Scope (Disc) = Current_Scope | |
443 | and then not | |
444 | (Nkind (Parent (P)) = N_Subtype_Indication | |
45fc7ddb HK |
445 | and then |
446 | Nkind_In (Parent (Parent (P)), N_Component_Definition, | |
447 | N_Subtype_Declaration) | |
996ae0b0 RK |
448 | and then Paren_Count (N) = 0) |
449 | then | |
450 | Error_Msg_N | |
451 | ("discriminant must appear alone in component constraint", N); | |
452 | return; | |
453 | end if; | |
454 | ||
a0ac3932 | 455 | -- Detect a common error: |
9bc43c53 | 456 | |
996ae0b0 | 457 | -- type R (D : Positive := 100) is record |
9bc43c53 | 458 | -- Name : String (1 .. D); |
996ae0b0 RK |
459 | -- end record; |
460 | ||
a0ac3932 RD |
461 | -- The default value causes an object of type R to be allocated |
462 | -- with room for Positive'Last characters. The RM does not mandate | |
463 | -- the allocation of the maximum size, but that is what GNAT does | |
464 | -- so we should warn the programmer that there is a problem. | |
996ae0b0 | 465 | |
a0ac3932 | 466 | Check_Large : declare |
996ae0b0 RK |
467 | SI : Node_Id; |
468 | T : Entity_Id; | |
469 | TB : Node_Id; | |
470 | CB : Entity_Id; | |
471 | ||
472 | function Large_Storage_Type (T : Entity_Id) return Boolean; | |
473 | -- Return True if type T has a large enough range that | |
474 | -- any array whose index type covered the whole range of | |
475 | -- the type would likely raise Storage_Error. | |
476 | ||
fbf5a39b AC |
477 | ------------------------ |
478 | -- Large_Storage_Type -- | |
479 | ------------------------ | |
480 | ||
996ae0b0 RK |
481 | function Large_Storage_Type (T : Entity_Id) return Boolean is |
482 | begin | |
4b92fd3c ST |
483 | -- The type is considered large if its bounds are known at |
484 | -- compile time and if it requires at least as many bits as | |
485 | -- a Positive to store the possible values. | |
486 | ||
487 | return Compile_Time_Known_Value (Type_Low_Bound (T)) | |
488 | and then Compile_Time_Known_Value (Type_High_Bound (T)) | |
489 | and then | |
490 | Minimum_Size (T, Biased => True) >= | |
a0ac3932 | 491 | RM_Size (Standard_Positive); |
996ae0b0 RK |
492 | end Large_Storage_Type; |
493 | ||
a0ac3932 RD |
494 | -- Start of processing for Check_Large |
495 | ||
996ae0b0 RK |
496 | begin |
497 | -- Check that the Disc has a large range | |
498 | ||
499 | if not Large_Storage_Type (Etype (Disc)) then | |
500 | goto No_Danger; | |
501 | end if; | |
502 | ||
503 | -- If the enclosing type is limited, we allocate only the | |
504 | -- default value, not the maximum, and there is no need for | |
505 | -- a warning. | |
506 | ||
507 | if Is_Limited_Type (Scope (Disc)) then | |
508 | goto No_Danger; | |
509 | end if; | |
510 | ||
511 | -- Check that it is the high bound | |
512 | ||
513 | if N /= High_Bound (PN) | |
c8ef728f | 514 | or else No (Discriminant_Default_Value (Disc)) |
996ae0b0 RK |
515 | then |
516 | goto No_Danger; | |
517 | end if; | |
518 | ||
519 | -- Check the array allows a large range at this bound. | |
520 | -- First find the array | |
521 | ||
522 | SI := Parent (P); | |
523 | ||
524 | if Nkind (SI) /= N_Subtype_Indication then | |
525 | goto No_Danger; | |
526 | end if; | |
527 | ||
528 | T := Entity (Subtype_Mark (SI)); | |
529 | ||
530 | if not Is_Array_Type (T) then | |
531 | goto No_Danger; | |
532 | end if; | |
533 | ||
534 | -- Next, find the dimension | |
535 | ||
536 | TB := First_Index (T); | |
537 | CB := First (Constraints (P)); | |
538 | while True | |
539 | and then Present (TB) | |
540 | and then Present (CB) | |
541 | and then CB /= PN | |
542 | loop | |
543 | Next_Index (TB); | |
544 | Next (CB); | |
545 | end loop; | |
546 | ||
547 | if CB /= PN then | |
548 | goto No_Danger; | |
549 | end if; | |
550 | ||
551 | -- Now, check the dimension has a large range | |
552 | ||
553 | if not Large_Storage_Type (Etype (TB)) then | |
554 | goto No_Danger; | |
555 | end if; | |
556 | ||
557 | -- Warn about the danger | |
558 | ||
559 | Error_Msg_N | |
aa5147f0 | 560 | ("?creation of & object may raise Storage_Error!", |
fbf5a39b | 561 | Scope (Disc)); |
996ae0b0 RK |
562 | |
563 | <<No_Danger>> | |
564 | null; | |
565 | ||
a0ac3932 | 566 | end Check_Large; |
996ae0b0 RK |
567 | end if; |
568 | ||
569 | -- Legal case is in index or discriminant constraint | |
570 | ||
45fc7ddb HK |
571 | elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint, |
572 | N_Discriminant_Association) | |
996ae0b0 RK |
573 | then |
574 | if Paren_Count (N) > 0 then | |
575 | Error_Msg_N | |
576 | ("discriminant in constraint must appear alone", N); | |
758c442c GD |
577 | |
578 | elsif Nkind (N) = N_Expanded_Name | |
579 | and then Comes_From_Source (N) | |
580 | then | |
581 | Error_Msg_N | |
582 | ("discriminant must appear alone as a direct name", N); | |
996ae0b0 RK |
583 | end if; |
584 | ||
585 | return; | |
586 | ||
587 | -- Otherwise, context is an expression. It should not be within | |
588 | -- (i.e. a subexpression of) a constraint for a component. | |
589 | ||
590 | else | |
591 | D := PN; | |
592 | P := Parent (PN); | |
45fc7ddb HK |
593 | while not Nkind_In (P, N_Component_Declaration, |
594 | N_Subtype_Indication, | |
595 | N_Entry_Declaration) | |
996ae0b0 RK |
596 | loop |
597 | D := P; | |
598 | P := Parent (P); | |
599 | exit when No (P); | |
600 | end loop; | |
601 | ||
602 | -- If the discriminant is used in an expression that is a bound | |
603 | -- of a scalar type, an Itype is created and the bounds are attached | |
604 | -- to its range, not to the original subtype indication. Such use | |
605 | -- is of course a double fault. | |
606 | ||
607 | if (Nkind (P) = N_Subtype_Indication | |
45fc7ddb HK |
608 | and then Nkind_In (Parent (P), N_Component_Definition, |
609 | N_Derived_Type_Definition) | |
996ae0b0 RK |
610 | and then D = Constraint (P)) |
611 | ||
612 | -- The constraint itself may be given by a subtype indication, | |
613 | -- rather than by a more common discrete range. | |
614 | ||
615 | or else (Nkind (P) = N_Subtype_Indication | |
fbf5a39b AC |
616 | and then |
617 | Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint) | |
996ae0b0 RK |
618 | or else Nkind (P) = N_Entry_Declaration |
619 | or else Nkind (D) = N_Defining_Identifier | |
620 | then | |
621 | Error_Msg_N | |
622 | ("discriminant in constraint must appear alone", N); | |
623 | end if; | |
624 | end if; | |
625 | end Check_Discriminant_Use; | |
626 | ||
627 | -------------------------------- | |
628 | -- Check_For_Visible_Operator -- | |
629 | -------------------------------- | |
630 | ||
631 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is | |
996ae0b0 | 632 | begin |
fbf5a39b | 633 | if Is_Invisible_Operator (N, T) then |
996ae0b0 RK |
634 | Error_Msg_NE |
635 | ("operator for} is not directly visible!", N, First_Subtype (T)); | |
636 | Error_Msg_N ("use clause would make operation legal!", N); | |
637 | end if; | |
638 | end Check_For_Visible_Operator; | |
639 | ||
c8ef728f ES |
640 | ---------------------------------- |
641 | -- Check_Fully_Declared_Prefix -- | |
642 | ---------------------------------- | |
643 | ||
644 | procedure Check_Fully_Declared_Prefix | |
645 | (Typ : Entity_Id; | |
646 | Pref : Node_Id) | |
647 | is | |
648 | begin | |
649 | -- Check that the designated type of the prefix of a dereference is | |
650 | -- not an incomplete type. This cannot be done unconditionally, because | |
651 | -- dereferences of private types are legal in default expressions. This | |
652 | -- case is taken care of in Check_Fully_Declared, called below. There | |
653 | -- are also 2005 cases where it is legal for the prefix to be unfrozen. | |
654 | ||
655 | -- This consideration also applies to similar checks for allocators, | |
656 | -- qualified expressions, and type conversions. | |
657 | ||
658 | -- An additional exception concerns other per-object expressions that | |
659 | -- are not directly related to component declarations, in particular | |
660 | -- representation pragmas for tasks. These will be per-object | |
661 | -- expressions if they depend on discriminants or some global entity. | |
662 | -- If the task has access discriminants, the designated type may be | |
663 | -- incomplete at the point the expression is resolved. This resolution | |
664 | -- takes place within the body of the initialization procedure, where | |
665 | -- the discriminant is replaced by its discriminal. | |
666 | ||
667 | if Is_Entity_Name (Pref) | |
668 | and then Ekind (Entity (Pref)) = E_In_Parameter | |
669 | then | |
670 | null; | |
671 | ||
672 | -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages | |
673 | -- are handled by Analyze_Access_Attribute, Analyze_Assignment, | |
674 | -- Analyze_Object_Renaming, and Freeze_Entity. | |
675 | ||
676 | elsif Ada_Version >= Ada_05 | |
677 | and then Is_Entity_Name (Pref) | |
811c6a85 | 678 | and then Is_Access_Type (Etype (Pref)) |
c8ef728f ES |
679 | and then Ekind (Directly_Designated_Type (Etype (Pref))) = |
680 | E_Incomplete_Type | |
681 | and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref))) | |
682 | then | |
683 | null; | |
684 | else | |
685 | Check_Fully_Declared (Typ, Parent (Pref)); | |
686 | end if; | |
687 | end Check_Fully_Declared_Prefix; | |
688 | ||
996ae0b0 RK |
689 | ------------------------------ |
690 | -- Check_Infinite_Recursion -- | |
691 | ------------------------------ | |
692 | ||
693 | function Check_Infinite_Recursion (N : Node_Id) return Boolean is | |
694 | P : Node_Id; | |
695 | C : Node_Id; | |
696 | ||
07fc65c4 GB |
697 | function Same_Argument_List return Boolean; |
698 | -- Check whether list of actuals is identical to list of formals | |
699 | -- of called function (which is also the enclosing scope). | |
700 | ||
701 | ------------------------ | |
702 | -- Same_Argument_List -- | |
703 | ------------------------ | |
704 | ||
705 | function Same_Argument_List return Boolean is | |
706 | A : Node_Id; | |
707 | F : Entity_Id; | |
708 | Subp : Entity_Id; | |
709 | ||
710 | begin | |
711 | if not Is_Entity_Name (Name (N)) then | |
712 | return False; | |
713 | else | |
714 | Subp := Entity (Name (N)); | |
715 | end if; | |
716 | ||
717 | F := First_Formal (Subp); | |
718 | A := First_Actual (N); | |
07fc65c4 GB |
719 | while Present (F) and then Present (A) loop |
720 | if not Is_Entity_Name (A) | |
721 | or else Entity (A) /= F | |
722 | then | |
723 | return False; | |
724 | end if; | |
725 | ||
726 | Next_Actual (A); | |
727 | Next_Formal (F); | |
728 | end loop; | |
729 | ||
730 | return True; | |
731 | end Same_Argument_List; | |
732 | ||
733 | -- Start of processing for Check_Infinite_Recursion | |
734 | ||
996ae0b0 | 735 | begin |
26570b21 RD |
736 | -- Special case, if this is a procedure call and is a call to the |
737 | -- current procedure with the same argument list, then this is for | |
738 | -- sure an infinite recursion and we insert a call to raise SE. | |
739 | ||
740 | if Is_List_Member (N) | |
741 | and then List_Length (List_Containing (N)) = 1 | |
742 | and then Same_Argument_List | |
743 | then | |
744 | declare | |
745 | P : constant Node_Id := Parent (N); | |
746 | begin | |
747 | if Nkind (P) = N_Handled_Sequence_Of_Statements | |
748 | and then Nkind (Parent (P)) = N_Subprogram_Body | |
749 | and then Is_Empty_List (Declarations (Parent (P))) | |
750 | then | |
751 | Error_Msg_N ("!?infinite recursion", N); | |
752 | Error_Msg_N ("\!?Storage_Error will be raised at run time", N); | |
753 | Insert_Action (N, | |
754 | Make_Raise_Storage_Error (Sloc (N), | |
755 | Reason => SE_Infinite_Recursion)); | |
756 | return True; | |
757 | end if; | |
758 | end; | |
759 | end if; | |
760 | ||
761 | -- If not that special case, search up tree, quitting if we reach a | |
762 | -- construct (e.g. a conditional) that tells us that this is not a | |
763 | -- case for an infinite recursion warning. | |
996ae0b0 RK |
764 | |
765 | C := N; | |
766 | loop | |
767 | P := Parent (C); | |
9a7da240 RD |
768 | |
769 | -- If no parent, then we were not inside a subprogram, this can for | |
770 | -- example happen when processing certain pragmas in a spec. Just | |
771 | -- return False in this case. | |
772 | ||
773 | if No (P) then | |
774 | return False; | |
775 | end if; | |
776 | ||
777 | -- Done if we get to subprogram body, this is definitely an infinite | |
778 | -- recursion case if we did not find anything to stop us. | |
779 | ||
996ae0b0 | 780 | exit when Nkind (P) = N_Subprogram_Body; |
9a7da240 RD |
781 | |
782 | -- If appearing in conditional, result is false | |
783 | ||
45fc7ddb HK |
784 | if Nkind_In (P, N_Or_Else, |
785 | N_And_Then, | |
786 | N_If_Statement, | |
787 | N_Case_Statement) | |
996ae0b0 RK |
788 | then |
789 | return False; | |
790 | ||
791 | elsif Nkind (P) = N_Handled_Sequence_Of_Statements | |
792 | and then C /= First (Statements (P)) | |
793 | then | |
26570b21 RD |
794 | -- If the call is the expression of a return statement and the |
795 | -- actuals are identical to the formals, it's worth a warning. | |
796 | -- However, we skip this if there is an immediately preceding | |
797 | -- raise statement, since the call is never executed. | |
07fc65c4 GB |
798 | |
799 | -- Furthermore, this corresponds to a common idiom: | |
800 | ||
801 | -- function F (L : Thing) return Boolean is | |
802 | -- begin | |
803 | -- raise Program_Error; | |
804 | -- return F (L); | |
805 | -- end F; | |
806 | ||
807 | -- for generating a stub function | |
808 | ||
aa5147f0 | 809 | if Nkind (Parent (N)) = N_Simple_Return_Statement |
07fc65c4 GB |
810 | and then Same_Argument_List |
811 | then | |
9ebe3743 HK |
812 | exit when not Is_List_Member (Parent (N)); |
813 | ||
814 | -- OK, return statement is in a statement list, look for raise | |
815 | ||
816 | declare | |
817 | Nod : Node_Id; | |
818 | ||
819 | begin | |
820 | -- Skip past N_Freeze_Entity nodes generated by expansion | |
821 | ||
822 | Nod := Prev (Parent (N)); | |
823 | while Present (Nod) | |
824 | and then Nkind (Nod) = N_Freeze_Entity | |
825 | loop | |
826 | Prev (Nod); | |
827 | end loop; | |
828 | ||
829 | -- If no raise statement, give warning | |
830 | ||
831 | exit when Nkind (Nod) /= N_Raise_Statement | |
832 | and then | |
833 | (Nkind (Nod) not in N_Raise_xxx_Error | |
834 | or else Present (Condition (Nod))); | |
835 | end; | |
07fc65c4 GB |
836 | end if; |
837 | ||
996ae0b0 RK |
838 | return False; |
839 | ||
840 | else | |
841 | C := P; | |
842 | end if; | |
843 | end loop; | |
844 | ||
aa5147f0 ES |
845 | Error_Msg_N ("!?possible infinite recursion", N); |
846 | Error_Msg_N ("\!?Storage_Error may be raised at run time", N); | |
996ae0b0 RK |
847 | |
848 | return True; | |
849 | end Check_Infinite_Recursion; | |
850 | ||
851 | ------------------------------- | |
852 | -- Check_Initialization_Call -- | |
853 | ------------------------------- | |
854 | ||
855 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is | |
fbf5a39b | 856 | Typ : constant Entity_Id := Etype (First_Formal (Nam)); |
996ae0b0 RK |
857 | |
858 | function Uses_SS (T : Entity_Id) return Boolean; | |
07fc65c4 GB |
859 | -- Check whether the creation of an object of the type will involve |
860 | -- use of the secondary stack. If T is a record type, this is true | |
f3d57416 | 861 | -- if the expression for some component uses the secondary stack, e.g. |
07fc65c4 GB |
862 | -- through a call to a function that returns an unconstrained value. |
863 | -- False if T is controlled, because cleanups occur elsewhere. | |
864 | ||
865 | ------------- | |
866 | -- Uses_SS -- | |
867 | ------------- | |
996ae0b0 RK |
868 | |
869 | function Uses_SS (T : Entity_Id) return Boolean is | |
aa5147f0 ES |
870 | Comp : Entity_Id; |
871 | Expr : Node_Id; | |
872 | Full_Type : Entity_Id := Underlying_Type (T); | |
996ae0b0 RK |
873 | |
874 | begin | |
aa5147f0 ES |
875 | -- Normally we want to use the underlying type, but if it's not set |
876 | -- then continue with T. | |
877 | ||
878 | if not Present (Full_Type) then | |
879 | Full_Type := T; | |
880 | end if; | |
881 | ||
882 | if Is_Controlled (Full_Type) then | |
996ae0b0 RK |
883 | return False; |
884 | ||
aa5147f0 ES |
885 | elsif Is_Array_Type (Full_Type) then |
886 | return Uses_SS (Component_Type (Full_Type)); | |
996ae0b0 | 887 | |
aa5147f0 ES |
888 | elsif Is_Record_Type (Full_Type) then |
889 | Comp := First_Component (Full_Type); | |
996ae0b0 | 890 | while Present (Comp) loop |
996ae0b0 RK |
891 | if Ekind (Comp) = E_Component |
892 | and then Nkind (Parent (Comp)) = N_Component_Declaration | |
893 | then | |
aa5147f0 ES |
894 | -- The expression for a dynamic component may be rewritten |
895 | -- as a dereference, so retrieve original node. | |
896 | ||
897 | Expr := Original_Node (Expression (Parent (Comp))); | |
996ae0b0 | 898 | |
aa5147f0 ES |
899 | -- Return True if the expression is a call to a function |
900 | -- (including an attribute function such as Image) with | |
901 | -- a result that requires a transient scope. | |
fbf5a39b | 902 | |
aa5147f0 ES |
903 | if (Nkind (Expr) = N_Function_Call |
904 | or else (Nkind (Expr) = N_Attribute_Reference | |
905 | and then Present (Expressions (Expr)))) | |
996ae0b0 RK |
906 | and then Requires_Transient_Scope (Etype (Expr)) |
907 | then | |
908 | return True; | |
909 | ||
910 | elsif Uses_SS (Etype (Comp)) then | |
911 | return True; | |
912 | end if; | |
913 | end if; | |
914 | ||
915 | Next_Component (Comp); | |
916 | end loop; | |
917 | ||
918 | return False; | |
919 | ||
920 | else | |
921 | return False; | |
922 | end if; | |
923 | end Uses_SS; | |
924 | ||
07fc65c4 GB |
925 | -- Start of processing for Check_Initialization_Call |
926 | ||
996ae0b0 | 927 | begin |
0669bebe | 928 | -- Establish a transient scope if the type needs it |
07fc65c4 | 929 | |
0669bebe | 930 | if Uses_SS (Typ) then |
996ae0b0 RK |
931 | Establish_Transient_Scope (First_Actual (N), Sec_Stack => True); |
932 | end if; | |
933 | end Check_Initialization_Call; | |
934 | ||
f61580d4 AC |
935 | --------------------------------------- |
936 | -- Check_No_Direct_Boolean_Operators -- | |
937 | --------------------------------------- | |
938 | ||
939 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is | |
940 | begin | |
941 | if Scope (Entity (N)) = Standard_Standard | |
942 | and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean | |
943 | then | |
944 | -- Restriction does not apply to generated code | |
945 | ||
946 | if not Comes_From_Source (N) then | |
947 | null; | |
948 | ||
949 | -- Restriction does not apply for A=False, A=True | |
950 | ||
951 | elsif Nkind (N) = N_Op_Eq | |
952 | and then (Is_Entity_Name (Right_Opnd (N)) | |
953 | and then (Entity (Right_Opnd (N)) = Standard_True | |
954 | or else | |
955 | Entity (Right_Opnd (N)) = Standard_False)) | |
956 | then | |
957 | null; | |
958 | ||
959 | -- Otherwise restriction applies | |
960 | ||
961 | else | |
962 | Check_Restriction (No_Direct_Boolean_Operators, N); | |
963 | end if; | |
964 | end if; | |
965 | end Check_No_Direct_Boolean_Operators; | |
966 | ||
996ae0b0 RK |
967 | ------------------------------ |
968 | -- Check_Parameterless_Call -- | |
969 | ------------------------------ | |
970 | ||
971 | procedure Check_Parameterless_Call (N : Node_Id) is | |
972 | Nam : Node_Id; | |
973 | ||
bc5f3720 RD |
974 | function Prefix_Is_Access_Subp return Boolean; |
975 | -- If the prefix is of an access_to_subprogram type, the node must be | |
976 | -- rewritten as a call. Ditto if the prefix is overloaded and all its | |
977 | -- interpretations are access to subprograms. | |
978 | ||
979 | --------------------------- | |
980 | -- Prefix_Is_Access_Subp -- | |
981 | --------------------------- | |
982 | ||
983 | function Prefix_Is_Access_Subp return Boolean is | |
984 | I : Interp_Index; | |
985 | It : Interp; | |
986 | ||
987 | begin | |
988 | if not Is_Overloaded (N) then | |
989 | return | |
990 | Ekind (Etype (N)) = E_Subprogram_Type | |
991 | and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type; | |
992 | else | |
993 | Get_First_Interp (N, I, It); | |
994 | while Present (It.Typ) loop | |
995 | if Ekind (It.Typ) /= E_Subprogram_Type | |
996 | or else Base_Type (Etype (It.Typ)) = Standard_Void_Type | |
997 | then | |
998 | return False; | |
999 | end if; | |
1000 | ||
1001 | Get_Next_Interp (I, It); | |
1002 | end loop; | |
1003 | ||
1004 | return True; | |
1005 | end if; | |
1006 | end Prefix_Is_Access_Subp; | |
1007 | ||
1008 | -- Start of processing for Check_Parameterless_Call | |
1009 | ||
996ae0b0 | 1010 | begin |
07fc65c4 GB |
1011 | -- Defend against junk stuff if errors already detected |
1012 | ||
1013 | if Total_Errors_Detected /= 0 then | |
1014 | if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then | |
1015 | return; | |
1016 | elsif Nkind (N) in N_Has_Chars | |
1017 | and then Chars (N) in Error_Name_Or_No_Name | |
1018 | then | |
1019 | return; | |
1020 | end if; | |
fbf5a39b AC |
1021 | |
1022 | Require_Entity (N); | |
996ae0b0 RK |
1023 | end if; |
1024 | ||
45fc7ddb HK |
1025 | -- If the context expects a value, and the name is a procedure, this is |
1026 | -- most likely a missing 'Access. Don't try to resolve the parameterless | |
1027 | -- call, error will be caught when the outer call is analyzed. | |
18c0ecbe AC |
1028 | |
1029 | if Is_Entity_Name (N) | |
1030 | and then Ekind (Entity (N)) = E_Procedure | |
1031 | and then not Is_Overloaded (N) | |
1032 | and then | |
45fc7ddb HK |
1033 | Nkind_In (Parent (N), N_Parameter_Association, |
1034 | N_Function_Call, | |
1035 | N_Procedure_Call_Statement) | |
18c0ecbe AC |
1036 | then |
1037 | return; | |
1038 | end if; | |
1039 | ||
45fc7ddb HK |
1040 | -- Rewrite as call if overloadable entity that is (or could be, in the |
1041 | -- overloaded case) a function call. If we know for sure that the entity | |
1042 | -- is an enumeration literal, we do not rewrite it. | |
996ae0b0 RK |
1043 | |
1044 | if (Is_Entity_Name (N) | |
1045 | and then Is_Overloadable (Entity (N)) | |
1046 | and then (Ekind (Entity (N)) /= E_Enumeration_Literal | |
1047 | or else Is_Overloaded (N))) | |
1048 | ||
1049 | -- Rewrite as call if it is an explicit deference of an expression of | |
f3d57416 | 1050 | -- a subprogram access type, and the subprogram type is not that of a |
996ae0b0 RK |
1051 | -- procedure or entry. |
1052 | ||
1053 | or else | |
bc5f3720 | 1054 | (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp) |
996ae0b0 RK |
1055 | |
1056 | -- Rewrite as call if it is a selected component which is a function, | |
1057 | -- this is the case of a call to a protected function (which may be | |
1058 | -- overloaded with other protected operations). | |
1059 | ||
1060 | or else | |
1061 | (Nkind (N) = N_Selected_Component | |
1062 | and then (Ekind (Entity (Selector_Name (N))) = E_Function | |
fbf5a39b AC |
1063 | or else |
1064 | ((Ekind (Entity (Selector_Name (N))) = E_Entry | |
1065 | or else | |
1066 | Ekind (Entity (Selector_Name (N))) = E_Procedure) | |
1067 | and then Is_Overloaded (Selector_Name (N))))) | |
996ae0b0 RK |
1068 | |
1069 | -- If one of the above three conditions is met, rewrite as call. | |
1070 | -- Apply the rewriting only once. | |
1071 | ||
1072 | then | |
1073 | if Nkind (Parent (N)) /= N_Function_Call | |
1074 | or else N /= Name (Parent (N)) | |
1075 | then | |
1076 | Nam := New_Copy (N); | |
1077 | ||
bc5f3720 | 1078 | -- If overloaded, overload set belongs to new copy |
996ae0b0 RK |
1079 | |
1080 | Save_Interps (N, Nam); | |
1081 | ||
1082 | -- Change node to parameterless function call (note that the | |
1083 | -- Parameter_Associations associations field is left set to Empty, | |
1084 | -- its normal default value since there are no parameters) | |
1085 | ||
1086 | Change_Node (N, N_Function_Call); | |
1087 | Set_Name (N, Nam); | |
1088 | Set_Sloc (N, Sloc (Nam)); | |
1089 | Analyze_Call (N); | |
1090 | end if; | |
1091 | ||
1092 | elsif Nkind (N) = N_Parameter_Association then | |
1093 | Check_Parameterless_Call (Explicit_Actual_Parameter (N)); | |
1094 | end if; | |
1095 | end Check_Parameterless_Call; | |
1096 | ||
67ce0d7e RD |
1097 | ----------------------------- |
1098 | -- Is_Definite_Access_Type -- | |
1099 | ----------------------------- | |
1100 | ||
1101 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean is | |
1102 | Btyp : constant Entity_Id := Base_Type (E); | |
1103 | begin | |
1104 | return Ekind (Btyp) = E_Access_Type | |
1105 | or else (Ekind (Btyp) = E_Access_Subprogram_Type | |
1106 | and then Comes_From_Source (Btyp)); | |
1107 | end Is_Definite_Access_Type; | |
1108 | ||
996ae0b0 RK |
1109 | ---------------------- |
1110 | -- Is_Predefined_Op -- | |
1111 | ---------------------- | |
1112 | ||
1113 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean is | |
1114 | begin | |
1115 | return Is_Intrinsic_Subprogram (Nam) | |
1116 | and then not Is_Generic_Instance (Nam) | |
1117 | and then Chars (Nam) in Any_Operator_Name | |
1118 | and then (No (Alias (Nam)) | |
1119 | or else Is_Predefined_Op (Alias (Nam))); | |
1120 | end Is_Predefined_Op; | |
1121 | ||
1122 | ----------------------------- | |
1123 | -- Make_Call_Into_Operator -- | |
1124 | ----------------------------- | |
1125 | ||
1126 | procedure Make_Call_Into_Operator | |
1127 | (N : Node_Id; | |
1128 | Typ : Entity_Id; | |
1129 | Op_Id : Entity_Id) | |
1130 | is | |
1131 | Op_Name : constant Name_Id := Chars (Op_Id); | |
1132 | Act1 : Node_Id := First_Actual (N); | |
1133 | Act2 : Node_Id := Next_Actual (Act1); | |
1134 | Error : Boolean := False; | |
2820d220 AC |
1135 | Func : constant Entity_Id := Entity (Name (N)); |
1136 | Is_Binary : constant Boolean := Present (Act2); | |
996ae0b0 RK |
1137 | Op_Node : Node_Id; |
1138 | Opnd_Type : Entity_Id; | |
1139 | Orig_Type : Entity_Id := Empty; | |
1140 | Pack : Entity_Id; | |
1141 | ||
1142 | type Kind_Test is access function (E : Entity_Id) return Boolean; | |
1143 | ||
996ae0b0 RK |
1144 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean; |
1145 | -- If the operand is not universal, and the operator is given by a | |
1146 | -- expanded name, verify that the operand has an interpretation with | |
1147 | -- a type defined in the given scope of the operator. | |
1148 | ||
1149 | function Type_In_P (Test : Kind_Test) return Entity_Id; | |
1150 | -- Find a type of the given class in the package Pack that contains | |
1151 | -- the operator. | |
1152 | ||
996ae0b0 RK |
1153 | --------------------------- |
1154 | -- Operand_Type_In_Scope -- | |
1155 | --------------------------- | |
1156 | ||
1157 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean is | |
1158 | Nod : constant Node_Id := Right_Opnd (Op_Node); | |
1159 | I : Interp_Index; | |
1160 | It : Interp; | |
1161 | ||
1162 | begin | |
1163 | if not Is_Overloaded (Nod) then | |
1164 | return Scope (Base_Type (Etype (Nod))) = S; | |
1165 | ||
1166 | else | |
1167 | Get_First_Interp (Nod, I, It); | |
996ae0b0 | 1168 | while Present (It.Typ) loop |
996ae0b0 RK |
1169 | if Scope (Base_Type (It.Typ)) = S then |
1170 | return True; | |
1171 | end if; | |
1172 | ||
1173 | Get_Next_Interp (I, It); | |
1174 | end loop; | |
1175 | ||
1176 | return False; | |
1177 | end if; | |
1178 | end Operand_Type_In_Scope; | |
1179 | ||
1180 | --------------- | |
1181 | -- Type_In_P -- | |
1182 | --------------- | |
1183 | ||
1184 | function Type_In_P (Test : Kind_Test) return Entity_Id is | |
1185 | E : Entity_Id; | |
1186 | ||
1187 | function In_Decl return Boolean; | |
1188 | -- Verify that node is not part of the type declaration for the | |
1189 | -- candidate type, which would otherwise be invisible. | |
1190 | ||
1191 | ------------- | |
1192 | -- In_Decl -- | |
1193 | ------------- | |
1194 | ||
1195 | function In_Decl return Boolean is | |
1196 | Decl_Node : constant Node_Id := Parent (E); | |
1197 | N2 : Node_Id; | |
1198 | ||
1199 | begin | |
1200 | N2 := N; | |
1201 | ||
1202 | if Etype (E) = Any_Type then | |
1203 | return True; | |
1204 | ||
1205 | elsif No (Decl_Node) then | |
1206 | return False; | |
1207 | ||
1208 | else | |
1209 | while Present (N2) | |
1210 | and then Nkind (N2) /= N_Compilation_Unit | |
1211 | loop | |
1212 | if N2 = Decl_Node then | |
1213 | return True; | |
1214 | else | |
1215 | N2 := Parent (N2); | |
1216 | end if; | |
1217 | end loop; | |
1218 | ||
1219 | return False; | |
1220 | end if; | |
1221 | end In_Decl; | |
1222 | ||
1223 | -- Start of processing for Type_In_P | |
1224 | ||
1225 | begin | |
1226 | -- If the context type is declared in the prefix package, this | |
1227 | -- is the desired base type. | |
1228 | ||
1229 | if Scope (Base_Type (Typ)) = Pack | |
1230 | and then Test (Typ) | |
1231 | then | |
1232 | return Base_Type (Typ); | |
1233 | ||
1234 | else | |
1235 | E := First_Entity (Pack); | |
996ae0b0 | 1236 | while Present (E) loop |
996ae0b0 RK |
1237 | if Test (E) |
1238 | and then not In_Decl | |
1239 | then | |
1240 | return E; | |
1241 | end if; | |
1242 | ||
1243 | Next_Entity (E); | |
1244 | end loop; | |
1245 | ||
1246 | return Empty; | |
1247 | end if; | |
1248 | end Type_In_P; | |
1249 | ||
996ae0b0 RK |
1250 | -- Start of processing for Make_Call_Into_Operator |
1251 | ||
1252 | begin | |
1253 | Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N)); | |
1254 | ||
1255 | -- Binary operator | |
1256 | ||
1257 | if Is_Binary then | |
1258 | Set_Left_Opnd (Op_Node, Relocate_Node (Act1)); | |
1259 | Set_Right_Opnd (Op_Node, Relocate_Node (Act2)); | |
1260 | Save_Interps (Act1, Left_Opnd (Op_Node)); | |
1261 | Save_Interps (Act2, Right_Opnd (Op_Node)); | |
1262 | Act1 := Left_Opnd (Op_Node); | |
1263 | Act2 := Right_Opnd (Op_Node); | |
1264 | ||
1265 | -- Unary operator | |
1266 | ||
1267 | else | |
1268 | Set_Right_Opnd (Op_Node, Relocate_Node (Act1)); | |
1269 | Save_Interps (Act1, Right_Opnd (Op_Node)); | |
1270 | Act1 := Right_Opnd (Op_Node); | |
1271 | end if; | |
1272 | ||
1273 | -- If the operator is denoted by an expanded name, and the prefix is | |
1274 | -- not Standard, but the operator is a predefined one whose scope is | |
1275 | -- Standard, then this is an implicit_operator, inserted as an | |
1276 | -- interpretation by the procedure of the same name. This procedure | |
1277 | -- overestimates the presence of implicit operators, because it does | |
1278 | -- not examine the type of the operands. Verify now that the operand | |
1279 | -- type appears in the given scope. If right operand is universal, | |
1280 | -- check the other operand. In the case of concatenation, either | |
1281 | -- argument can be the component type, so check the type of the result. | |
1282 | -- If both arguments are literals, look for a type of the right kind | |
1283 | -- defined in the given scope. This elaborate nonsense is brought to | |
1284 | -- you courtesy of b33302a. The type itself must be frozen, so we must | |
1285 | -- find the type of the proper class in the given scope. | |
1286 | ||
1287 | -- A final wrinkle is the multiplication operator for fixed point | |
1288 | -- types, which is defined in Standard only, and not in the scope of | |
1289 | -- the fixed_point type itself. | |
1290 | ||
1291 | if Nkind (Name (N)) = N_Expanded_Name then | |
1292 | Pack := Entity (Prefix (Name (N))); | |
1293 | ||
1294 | -- If the entity being called is defined in the given package, | |
1295 | -- it is a renaming of a predefined operator, and known to be | |
1296 | -- legal. | |
1297 | ||
1298 | if Scope (Entity (Name (N))) = Pack | |
1299 | and then Pack /= Standard_Standard | |
1300 | then | |
1301 | null; | |
1302 | ||
9ebe3743 HK |
1303 | -- Visibility does not need to be checked in an instance: if the |
1304 | -- operator was not visible in the generic it has been diagnosed | |
1305 | -- already, else there is an implicit copy of it in the instance. | |
1306 | ||
1307 | elsif In_Instance then | |
1308 | null; | |
1309 | ||
996ae0b0 RK |
1310 | elsif (Op_Name = Name_Op_Multiply |
1311 | or else Op_Name = Name_Op_Divide) | |
1312 | and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node))) | |
1313 | and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node))) | |
1314 | then | |
1315 | if Pack /= Standard_Standard then | |
1316 | Error := True; | |
1317 | end if; | |
1318 | ||
c8ef728f ES |
1319 | -- Ada 2005, AI-420: Predefined equality on Universal_Access |
1320 | -- is available. | |
1321 | ||
1322 | elsif Ada_Version >= Ada_05 | |
1323 | and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) | |
1324 | and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type | |
1325 | then | |
1326 | null; | |
1327 | ||
996ae0b0 RK |
1328 | else |
1329 | Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node))); | |
1330 | ||
1331 | if Op_Name = Name_Op_Concat then | |
1332 | Opnd_Type := Base_Type (Typ); | |
1333 | ||
1334 | elsif (Scope (Opnd_Type) = Standard_Standard | |
1335 | and then Is_Binary) | |
1336 | or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference | |
1337 | and then Is_Binary | |
1338 | and then not Comes_From_Source (Opnd_Type)) | |
1339 | then | |
1340 | Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node))); | |
1341 | end if; | |
1342 | ||
1343 | if Scope (Opnd_Type) = Standard_Standard then | |
1344 | ||
1345 | -- Verify that the scope contains a type that corresponds to | |
1346 | -- the given literal. Optimize the case where Pack is Standard. | |
1347 | ||
1348 | if Pack /= Standard_Standard then | |
1349 | ||
1350 | if Opnd_Type = Universal_Integer then | |
1351 | Orig_Type := Type_In_P (Is_Integer_Type'Access); | |
1352 | ||
1353 | elsif Opnd_Type = Universal_Real then | |
1354 | Orig_Type := Type_In_P (Is_Real_Type'Access); | |
1355 | ||
1356 | elsif Opnd_Type = Any_String then | |
1357 | Orig_Type := Type_In_P (Is_String_Type'Access); | |
1358 | ||
1359 | elsif Opnd_Type = Any_Access then | |
1360 | Orig_Type := Type_In_P (Is_Definite_Access_Type'Access); | |
1361 | ||
1362 | elsif Opnd_Type = Any_Composite then | |
1363 | Orig_Type := Type_In_P (Is_Composite_Type'Access); | |
1364 | ||
1365 | if Present (Orig_Type) then | |
1366 | if Has_Private_Component (Orig_Type) then | |
1367 | Orig_Type := Empty; | |
1368 | else | |
1369 | Set_Etype (Act1, Orig_Type); | |
1370 | ||
1371 | if Is_Binary then | |
1372 | Set_Etype (Act2, Orig_Type); | |
1373 | end if; | |
1374 | end if; | |
1375 | end if; | |
1376 | ||
1377 | else | |
1378 | Orig_Type := Empty; | |
1379 | end if; | |
1380 | ||
1381 | Error := No (Orig_Type); | |
1382 | end if; | |
1383 | ||
1384 | elsif Ekind (Opnd_Type) = E_Allocator_Type | |
1385 | and then No (Type_In_P (Is_Definite_Access_Type'Access)) | |
1386 | then | |
1387 | Error := True; | |
1388 | ||
1389 | -- If the type is defined elsewhere, and the operator is not | |
1390 | -- defined in the given scope (by a renaming declaration, e.g.) | |
1391 | -- then this is an error as well. If an extension of System is | |
1392 | -- present, and the type may be defined there, Pack must be | |
1393 | -- System itself. | |
1394 | ||
1395 | elsif Scope (Opnd_Type) /= Pack | |
1396 | and then Scope (Op_Id) /= Pack | |
1397 | and then (No (System_Aux_Id) | |
1398 | or else Scope (Opnd_Type) /= System_Aux_Id | |
1399 | or else Pack /= Scope (System_Aux_Id)) | |
1400 | then | |
244e5a2c AC |
1401 | if not Is_Overloaded (Right_Opnd (Op_Node)) then |
1402 | Error := True; | |
1403 | else | |
1404 | Error := not Operand_Type_In_Scope (Pack); | |
1405 | end if; | |
996ae0b0 RK |
1406 | |
1407 | elsif Pack = Standard_Standard | |
1408 | and then not Operand_Type_In_Scope (Standard_Standard) | |
1409 | then | |
1410 | Error := True; | |
1411 | end if; | |
1412 | end if; | |
1413 | ||
1414 | if Error then | |
1415 | Error_Msg_Node_2 := Pack; | |
1416 | Error_Msg_NE | |
1417 | ("& not declared in&", N, Selector_Name (Name (N))); | |
1418 | Set_Etype (N, Any_Type); | |
1419 | return; | |
1420 | end if; | |
1421 | end if; | |
1422 | ||
1423 | Set_Chars (Op_Node, Op_Name); | |
fbf5a39b AC |
1424 | |
1425 | if not Is_Private_Type (Etype (N)) then | |
1426 | Set_Etype (Op_Node, Base_Type (Etype (N))); | |
1427 | else | |
1428 | Set_Etype (Op_Node, Etype (N)); | |
1429 | end if; | |
1430 | ||
2820d220 AC |
1431 | -- If this is a call to a function that renames a predefined equality, |
1432 | -- the renaming declaration provides a type that must be used to | |
1433 | -- resolve the operands. This must be done now because resolution of | |
1434 | -- the equality node will not resolve any remaining ambiguity, and it | |
1435 | -- assumes that the first operand is not overloaded. | |
1436 | ||
1437 | if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) | |
1438 | and then Ekind (Func) = E_Function | |
1439 | and then Is_Overloaded (Act1) | |
1440 | then | |
1441 | Resolve (Act1, Base_Type (Etype (First_Formal (Func)))); | |
1442 | Resolve (Act2, Base_Type (Etype (First_Formal (Func)))); | |
1443 | end if; | |
1444 | ||
996ae0b0 RK |
1445 | Set_Entity (Op_Node, Op_Id); |
1446 | Generate_Reference (Op_Id, N, ' '); | |
45fc7ddb HK |
1447 | |
1448 | -- Do rewrite setting Comes_From_Source on the result if the original | |
1449 | -- call came from source. Although it is not strictly the case that the | |
1450 | -- operator as such comes from the source, logically it corresponds | |
1451 | -- exactly to the function call in the source, so it should be marked | |
1452 | -- this way (e.g. to make sure that validity checks work fine). | |
1453 | ||
1454 | declare | |
1455 | CS : constant Boolean := Comes_From_Source (N); | |
1456 | begin | |
1457 | Rewrite (N, Op_Node); | |
1458 | Set_Comes_From_Source (N, CS); | |
1459 | end; | |
fbf5a39b AC |
1460 | |
1461 | -- If this is an arithmetic operator and the result type is private, | |
1462 | -- the operands and the result must be wrapped in conversion to | |
1463 | -- expose the underlying numeric type and expand the proper checks, | |
1464 | -- e.g. on division. | |
1465 | ||
1466 | if Is_Private_Type (Typ) then | |
1467 | case Nkind (N) is | |
1468 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
1469 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
1470 | Resolve_Intrinsic_Operator (N, Typ); | |
1471 | ||
1472 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => | |
1473 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
1474 | ||
1475 | when others => | |
1476 | Resolve (N, Typ); | |
1477 | end case; | |
1478 | else | |
1479 | Resolve (N, Typ); | |
1480 | end if; | |
996ae0b0 RK |
1481 | |
1482 | -- For predefined operators on literals, the operation freezes | |
1483 | -- their type. | |
1484 | ||
1485 | if Present (Orig_Type) then | |
1486 | Set_Etype (Act1, Orig_Type); | |
1487 | Freeze_Expression (Act1); | |
1488 | end if; | |
1489 | end Make_Call_Into_Operator; | |
1490 | ||
1491 | ------------------- | |
1492 | -- Operator_Kind -- | |
1493 | ------------------- | |
1494 | ||
1495 | function Operator_Kind | |
1496 | (Op_Name : Name_Id; | |
0ab80019 | 1497 | Is_Binary : Boolean) return Node_Kind |
996ae0b0 RK |
1498 | is |
1499 | Kind : Node_Kind; | |
1500 | ||
1501 | begin | |
1502 | if Is_Binary then | |
aa5147f0 ES |
1503 | if Op_Name = Name_Op_And then |
1504 | Kind := N_Op_And; | |
1505 | elsif Op_Name = Name_Op_Or then | |
1506 | Kind := N_Op_Or; | |
1507 | elsif Op_Name = Name_Op_Xor then | |
1508 | Kind := N_Op_Xor; | |
1509 | elsif Op_Name = Name_Op_Eq then | |
1510 | Kind := N_Op_Eq; | |
1511 | elsif Op_Name = Name_Op_Ne then | |
1512 | Kind := N_Op_Ne; | |
1513 | elsif Op_Name = Name_Op_Lt then | |
1514 | Kind := N_Op_Lt; | |
1515 | elsif Op_Name = Name_Op_Le then | |
1516 | Kind := N_Op_Le; | |
1517 | elsif Op_Name = Name_Op_Gt then | |
1518 | Kind := N_Op_Gt; | |
1519 | elsif Op_Name = Name_Op_Ge then | |
1520 | Kind := N_Op_Ge; | |
1521 | elsif Op_Name = Name_Op_Add then | |
1522 | Kind := N_Op_Add; | |
1523 | elsif Op_Name = Name_Op_Subtract then | |
1524 | Kind := N_Op_Subtract; | |
1525 | elsif Op_Name = Name_Op_Concat then | |
1526 | Kind := N_Op_Concat; | |
1527 | elsif Op_Name = Name_Op_Multiply then | |
1528 | Kind := N_Op_Multiply; | |
1529 | elsif Op_Name = Name_Op_Divide then | |
1530 | Kind := N_Op_Divide; | |
1531 | elsif Op_Name = Name_Op_Mod then | |
1532 | Kind := N_Op_Mod; | |
1533 | elsif Op_Name = Name_Op_Rem then | |
1534 | Kind := N_Op_Rem; | |
1535 | elsif Op_Name = Name_Op_Expon then | |
1536 | Kind := N_Op_Expon; | |
996ae0b0 RK |
1537 | else |
1538 | raise Program_Error; | |
1539 | end if; | |
1540 | ||
1541 | -- Unary operators | |
1542 | ||
1543 | else | |
aa5147f0 ES |
1544 | if Op_Name = Name_Op_Add then |
1545 | Kind := N_Op_Plus; | |
1546 | elsif Op_Name = Name_Op_Subtract then | |
1547 | Kind := N_Op_Minus; | |
1548 | elsif Op_Name = Name_Op_Abs then | |
1549 | Kind := N_Op_Abs; | |
1550 | elsif Op_Name = Name_Op_Not then | |
1551 | Kind := N_Op_Not; | |
996ae0b0 RK |
1552 | else |
1553 | raise Program_Error; | |
1554 | end if; | |
1555 | end if; | |
1556 | ||
1557 | return Kind; | |
1558 | end Operator_Kind; | |
1559 | ||
45fc7ddb HK |
1560 | ---------------------------- |
1561 | -- Preanalyze_And_Resolve -- | |
1562 | ---------------------------- | |
996ae0b0 | 1563 | |
45fc7ddb | 1564 | procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is |
996ae0b0 RK |
1565 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1566 | ||
1567 | begin | |
1568 | Full_Analysis := False; | |
1569 | Expander_Mode_Save_And_Set (False); | |
1570 | ||
1571 | -- We suppress all checks for this analysis, since the checks will | |
1572 | -- be applied properly, and in the right location, when the default | |
1573 | -- expression is reanalyzed and reexpanded later on. | |
1574 | ||
1575 | Analyze_And_Resolve (N, T, Suppress => All_Checks); | |
1576 | ||
1577 | Expander_Mode_Restore; | |
1578 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1579 | end Preanalyze_And_Resolve; |
996ae0b0 | 1580 | |
a77842bd | 1581 | -- Version without context type |
996ae0b0 | 1582 | |
45fc7ddb | 1583 | procedure Preanalyze_And_Resolve (N : Node_Id) is |
996ae0b0 RK |
1584 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1585 | ||
1586 | begin | |
1587 | Full_Analysis := False; | |
1588 | Expander_Mode_Save_And_Set (False); | |
1589 | ||
1590 | Analyze (N); | |
1591 | Resolve (N, Etype (N), Suppress => All_Checks); | |
1592 | ||
1593 | Expander_Mode_Restore; | |
1594 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1595 | end Preanalyze_And_Resolve; |
996ae0b0 RK |
1596 | |
1597 | ---------------------------------- | |
1598 | -- Replace_Actual_Discriminants -- | |
1599 | ---------------------------------- | |
1600 | ||
1601 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is | |
1602 | Loc : constant Source_Ptr := Sloc (N); | |
1603 | Tsk : Node_Id := Empty; | |
1604 | ||
1605 | function Process_Discr (Nod : Node_Id) return Traverse_Result; | |
1606 | ||
1607 | ------------------- | |
1608 | -- Process_Discr -- | |
1609 | ------------------- | |
1610 | ||
1611 | function Process_Discr (Nod : Node_Id) return Traverse_Result is | |
1612 | Ent : Entity_Id; | |
1613 | ||
1614 | begin | |
1615 | if Nkind (Nod) = N_Identifier then | |
1616 | Ent := Entity (Nod); | |
1617 | ||
1618 | if Present (Ent) | |
1619 | and then Ekind (Ent) = E_Discriminant | |
1620 | then | |
1621 | Rewrite (Nod, | |
1622 | Make_Selected_Component (Loc, | |
1623 | Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc), | |
1624 | Selector_Name => Make_Identifier (Loc, Chars (Ent)))); | |
1625 | ||
1626 | Set_Etype (Nod, Etype (Ent)); | |
1627 | end if; | |
1628 | ||
1629 | end if; | |
1630 | ||
1631 | return OK; | |
1632 | end Process_Discr; | |
1633 | ||
1634 | procedure Replace_Discrs is new Traverse_Proc (Process_Discr); | |
1635 | ||
1636 | -- Start of processing for Replace_Actual_Discriminants | |
1637 | ||
1638 | begin | |
1639 | if not Expander_Active then | |
1640 | return; | |
1641 | end if; | |
1642 | ||
1643 | if Nkind (Name (N)) = N_Selected_Component then | |
1644 | Tsk := Prefix (Name (N)); | |
1645 | ||
1646 | elsif Nkind (Name (N)) = N_Indexed_Component then | |
1647 | Tsk := Prefix (Prefix (Name (N))); | |
1648 | end if; | |
1649 | ||
1650 | if No (Tsk) then | |
1651 | return; | |
1652 | else | |
1653 | Replace_Discrs (Default); | |
1654 | end if; | |
1655 | end Replace_Actual_Discriminants; | |
1656 | ||
1657 | ------------- | |
1658 | -- Resolve -- | |
1659 | ------------- | |
1660 | ||
1661 | procedure Resolve (N : Node_Id; Typ : Entity_Id) is | |
dae2b8ea HK |
1662 | Ambiguous : Boolean := False; |
1663 | Ctx_Type : Entity_Id := Typ; | |
1664 | Expr_Type : Entity_Id := Empty; -- prevent junk warning | |
1665 | Err_Type : Entity_Id := Empty; | |
1666 | Found : Boolean := False; | |
1667 | From_Lib : Boolean; | |
996ae0b0 | 1668 | I : Interp_Index; |
dae2b8ea | 1669 | I1 : Interp_Index := 0; -- prevent junk warning |
996ae0b0 RK |
1670 | It : Interp; |
1671 | It1 : Interp; | |
996ae0b0 | 1672 | Seen : Entity_Id := Empty; -- prevent junk warning |
dae2b8ea HK |
1673 | |
1674 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean; | |
1675 | -- Determine whether a node comes from a predefined library unit or | |
1676 | -- Standard. | |
996ae0b0 RK |
1677 | |
1678 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id); | |
1679 | -- Try and fix up a literal so that it matches its expected type. New | |
1680 | -- literals are manufactured if necessary to avoid cascaded errors. | |
1681 | ||
1682 | procedure Resolution_Failed; | |
1683 | -- Called when attempt at resolving current expression fails | |
1684 | ||
dae2b8ea HK |
1685 | ------------------------------------ |
1686 | -- Comes_From_Predefined_Lib_Unit -- | |
1687 | ------------------------------------- | |
1688 | ||
1689 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is | |
1690 | begin | |
1691 | return | |
1692 | Sloc (Nod) = Standard_Location | |
1693 | or else Is_Predefined_File_Name (Unit_File_Name ( | |
1694 | Get_Source_Unit (Sloc (Nod)))); | |
1695 | end Comes_From_Predefined_Lib_Unit; | |
1696 | ||
996ae0b0 RK |
1697 | -------------------- |
1698 | -- Patch_Up_Value -- | |
1699 | -------------------- | |
1700 | ||
1701 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is | |
1702 | begin | |
1703 | if Nkind (N) = N_Integer_Literal | |
1704 | and then Is_Real_Type (Typ) | |
1705 | then | |
1706 | Rewrite (N, | |
1707 | Make_Real_Literal (Sloc (N), | |
1708 | Realval => UR_From_Uint (Intval (N)))); | |
1709 | Set_Etype (N, Universal_Real); | |
1710 | Set_Is_Static_Expression (N); | |
1711 | ||
1712 | elsif Nkind (N) = N_Real_Literal | |
1713 | and then Is_Integer_Type (Typ) | |
1714 | then | |
1715 | Rewrite (N, | |
1716 | Make_Integer_Literal (Sloc (N), | |
1717 | Intval => UR_To_Uint (Realval (N)))); | |
1718 | Set_Etype (N, Universal_Integer); | |
1719 | Set_Is_Static_Expression (N); | |
45fc7ddb | 1720 | |
996ae0b0 RK |
1721 | elsif Nkind (N) = N_String_Literal |
1722 | and then Is_Character_Type (Typ) | |
1723 | then | |
1724 | Set_Character_Literal_Name (Char_Code (Character'Pos ('A'))); | |
1725 | Rewrite (N, | |
1726 | Make_Character_Literal (Sloc (N), | |
1727 | Chars => Name_Find, | |
82c80734 RD |
1728 | Char_Literal_Value => |
1729 | UI_From_Int (Character'Pos ('A')))); | |
996ae0b0 RK |
1730 | Set_Etype (N, Any_Character); |
1731 | Set_Is_Static_Expression (N); | |
1732 | ||
1733 | elsif Nkind (N) /= N_String_Literal | |
1734 | and then Is_String_Type (Typ) | |
1735 | then | |
1736 | Rewrite (N, | |
1737 | Make_String_Literal (Sloc (N), | |
1738 | Strval => End_String)); | |
1739 | ||
1740 | elsif Nkind (N) = N_Range then | |
1741 | Patch_Up_Value (Low_Bound (N), Typ); | |
1742 | Patch_Up_Value (High_Bound (N), Typ); | |
1743 | end if; | |
1744 | end Patch_Up_Value; | |
1745 | ||
1746 | ----------------------- | |
1747 | -- Resolution_Failed -- | |
1748 | ----------------------- | |
1749 | ||
1750 | procedure Resolution_Failed is | |
1751 | begin | |
1752 | Patch_Up_Value (N, Typ); | |
1753 | Set_Etype (N, Typ); | |
1754 | Debug_A_Exit ("resolving ", N, " (done, resolution failed)"); | |
1755 | Set_Is_Overloaded (N, False); | |
1756 | ||
1757 | -- The caller will return without calling the expander, so we need | |
1758 | -- to set the analyzed flag. Note that it is fine to set Analyzed | |
1759 | -- to True even if we are in the middle of a shallow analysis, | |
1760 | -- (see the spec of sem for more details) since this is an error | |
1761 | -- situation anyway, and there is no point in repeating the | |
1762 | -- analysis later (indeed it won't work to repeat it later, since | |
1763 | -- we haven't got a clear resolution of which entity is being | |
1764 | -- referenced.) | |
1765 | ||
1766 | Set_Analyzed (N, True); | |
1767 | return; | |
1768 | end Resolution_Failed; | |
1769 | ||
1770 | -- Start of processing for Resolve | |
1771 | ||
1772 | begin | |
5c736541 RD |
1773 | if N = Error then |
1774 | return; | |
1775 | end if; | |
1776 | ||
996ae0b0 RK |
1777 | -- Access attribute on remote subprogram cannot be used for |
1778 | -- a non-remote access-to-subprogram type. | |
1779 | ||
1780 | if Nkind (N) = N_Attribute_Reference | |
1781 | and then (Attribute_Name (N) = Name_Access | |
ea985d95 RD |
1782 | or else Attribute_Name (N) = Name_Unrestricted_Access |
1783 | or else Attribute_Name (N) = Name_Unchecked_Access) | |
996ae0b0 RK |
1784 | and then Comes_From_Source (N) |
1785 | and then Is_Entity_Name (Prefix (N)) | |
1786 | and then Is_Subprogram (Entity (Prefix (N))) | |
1787 | and then Is_Remote_Call_Interface (Entity (Prefix (N))) | |
1788 | and then not Is_Remote_Access_To_Subprogram_Type (Typ) | |
1789 | then | |
1790 | Error_Msg_N | |
1791 | ("prefix must statically denote a non-remote subprogram", N); | |
1792 | end if; | |
1793 | ||
dae2b8ea HK |
1794 | From_Lib := Comes_From_Predefined_Lib_Unit (N); |
1795 | ||
996ae0b0 RK |
1796 | -- If the context is a Remote_Access_To_Subprogram, access attributes |
1797 | -- must be resolved with the corresponding fat pointer. There is no need | |
1798 | -- to check for the attribute name since the return type of an | |
1799 | -- attribute is never a remote type. | |
1800 | ||
1801 | if Nkind (N) = N_Attribute_Reference | |
1802 | and then Comes_From_Source (N) | |
1803 | and then (Is_Remote_Call_Interface (Typ) | |
1804 | or else Is_Remote_Types (Typ)) | |
1805 | then | |
1806 | declare | |
1807 | Attr : constant Attribute_Id := | |
1808 | Get_Attribute_Id (Attribute_Name (N)); | |
1809 | Pref : constant Node_Id := Prefix (N); | |
1810 | Decl : Node_Id; | |
1811 | Spec : Node_Id; | |
1812 | Is_Remote : Boolean := True; | |
1813 | ||
1814 | begin | |
a77842bd | 1815 | -- Check that Typ is a remote access-to-subprogram type |
996ae0b0 | 1816 | |
a77842bd | 1817 | if Is_Remote_Access_To_Subprogram_Type (Typ) then |
996ae0b0 RK |
1818 | -- Prefix (N) must statically denote a remote subprogram |
1819 | -- declared in a package specification. | |
1820 | ||
1821 | if Attr = Attribute_Access then | |
1822 | Decl := Unit_Declaration_Node (Entity (Pref)); | |
1823 | ||
1824 | if Nkind (Decl) = N_Subprogram_Body then | |
1825 | Spec := Corresponding_Spec (Decl); | |
1826 | ||
1827 | if not No (Spec) then | |
1828 | Decl := Unit_Declaration_Node (Spec); | |
1829 | end if; | |
1830 | end if; | |
1831 | ||
1832 | Spec := Parent (Decl); | |
1833 | ||
1834 | if not Is_Entity_Name (Prefix (N)) | |
1835 | or else Nkind (Spec) /= N_Package_Specification | |
1836 | or else | |
1837 | not Is_Remote_Call_Interface (Defining_Entity (Spec)) | |
1838 | then | |
1839 | Is_Remote := False; | |
1840 | Error_Msg_N | |
1841 | ("prefix must statically denote a remote subprogram ", | |
1842 | N); | |
1843 | end if; | |
1844 | end if; | |
1845 | ||
fbf5a39b AC |
1846 | -- If we are generating code for a distributed program. |
1847 | -- perform semantic checks against the corresponding | |
1848 | -- remote entities. | |
1849 | ||
1850 | if (Attr = Attribute_Access | |
1851 | or else Attr = Attribute_Unchecked_Access | |
1852 | or else Attr = Attribute_Unrestricted_Access) | |
1853 | and then Expander_Active | |
a77842bd | 1854 | and then Get_PCS_Name /= Name_No_DSA |
996ae0b0 RK |
1855 | then |
1856 | Check_Subtype_Conformant | |
1857 | (New_Id => Entity (Prefix (N)), | |
1858 | Old_Id => Designated_Type | |
1859 | (Corresponding_Remote_Type (Typ)), | |
1860 | Err_Loc => N); | |
b7d1f17f | 1861 | |
996ae0b0 RK |
1862 | if Is_Remote then |
1863 | Process_Remote_AST_Attribute (N, Typ); | |
1864 | end if; | |
1865 | end if; | |
1866 | end if; | |
1867 | end; | |
1868 | end if; | |
1869 | ||
1870 | Debug_A_Entry ("resolving ", N); | |
1871 | ||
07fc65c4 GB |
1872 | if Comes_From_Source (N) then |
1873 | if Is_Fixed_Point_Type (Typ) then | |
1874 | Check_Restriction (No_Fixed_Point, N); | |
996ae0b0 | 1875 | |
07fc65c4 GB |
1876 | elsif Is_Floating_Point_Type (Typ) |
1877 | and then Typ /= Universal_Real | |
1878 | and then Typ /= Any_Real | |
1879 | then | |
1880 | Check_Restriction (No_Floating_Point, N); | |
1881 | end if; | |
996ae0b0 RK |
1882 | end if; |
1883 | ||
1884 | -- Return if already analyzed | |
1885 | ||
1886 | if Analyzed (N) then | |
1887 | Debug_A_Exit ("resolving ", N, " (done, already analyzed)"); | |
1888 | return; | |
1889 | ||
1890 | -- Return if type = Any_Type (previous error encountered) | |
1891 | ||
1892 | elsif Etype (N) = Any_Type then | |
1893 | Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)"); | |
1894 | return; | |
1895 | end if; | |
1896 | ||
1897 | Check_Parameterless_Call (N); | |
1898 | ||
1899 | -- If not overloaded, then we know the type, and all that needs doing | |
1900 | -- is to check that this type is compatible with the context. | |
1901 | ||
1902 | if not Is_Overloaded (N) then | |
1903 | Found := Covers (Typ, Etype (N)); | |
1904 | Expr_Type := Etype (N); | |
1905 | ||
1906 | -- In the overloaded case, we must select the interpretation that | |
1907 | -- is compatible with the context (i.e. the type passed to Resolve) | |
1908 | ||
1909 | else | |
996ae0b0 RK |
1910 | -- Loop through possible interpretations |
1911 | ||
1420b484 | 1912 | Get_First_Interp (N, I, It); |
996ae0b0 RK |
1913 | Interp_Loop : while Present (It.Typ) loop |
1914 | ||
1915 | -- We are only interested in interpretations that are compatible | |
aa5147f0 | 1916 | -- with the expected type, any other interpretations are ignored. |
996ae0b0 | 1917 | |
fbf5a39b AC |
1918 | if not Covers (Typ, It.Typ) then |
1919 | if Debug_Flag_V then | |
1920 | Write_Str (" interpretation incompatible with context"); | |
1921 | Write_Eol; | |
1922 | end if; | |
996ae0b0 | 1923 | |
fbf5a39b | 1924 | else |
aa5147f0 ES |
1925 | -- Skip the current interpretation if it is disabled by an |
1926 | -- abstract operator. This action is performed only when the | |
1927 | -- type against which we are resolving is the same as the | |
1928 | -- type of the interpretation. | |
1929 | ||
1930 | if Ada_Version >= Ada_05 | |
1931 | and then It.Typ = Typ | |
1932 | and then Typ /= Universal_Integer | |
1933 | and then Typ /= Universal_Real | |
1934 | and then Present (It.Abstract_Op) | |
1935 | then | |
1936 | goto Continue; | |
1937 | end if; | |
1938 | ||
996ae0b0 RK |
1939 | -- First matching interpretation |
1940 | ||
1941 | if not Found then | |
1942 | Found := True; | |
1943 | I1 := I; | |
1944 | Seen := It.Nam; | |
1945 | Expr_Type := It.Typ; | |
1946 | ||
fbf5a39b | 1947 | -- Matching interpretation that is not the first, maybe an |
996ae0b0 RK |
1948 | -- error, but there are some cases where preference rules are |
1949 | -- used to choose between the two possibilities. These and | |
1950 | -- some more obscure cases are handled in Disambiguate. | |
1951 | ||
1952 | else | |
dae2b8ea HK |
1953 | -- If the current statement is part of a predefined library |
1954 | -- unit, then all interpretations which come from user level | |
1955 | -- packages should not be considered. | |
1956 | ||
1957 | if From_Lib | |
1958 | and then not Comes_From_Predefined_Lib_Unit (It.Nam) | |
1959 | then | |
1960 | goto Continue; | |
1961 | end if; | |
1962 | ||
996ae0b0 RK |
1963 | Error_Msg_Sloc := Sloc (Seen); |
1964 | It1 := Disambiguate (N, I1, I, Typ); | |
1965 | ||
fbf5a39b AC |
1966 | -- Disambiguation has succeeded. Skip the remaining |
1967 | -- interpretations. | |
996ae0b0 | 1968 | |
fbf5a39b AC |
1969 | if It1 /= No_Interp then |
1970 | Seen := It1.Nam; | |
1971 | Expr_Type := It1.Typ; | |
1972 | ||
1973 | while Present (It.Typ) loop | |
1974 | Get_Next_Interp (I, It); | |
1975 | end loop; | |
1976 | ||
1977 | else | |
996ae0b0 RK |
1978 | -- Before we issue an ambiguity complaint, check for |
1979 | -- the case of a subprogram call where at least one | |
1980 | -- of the arguments is Any_Type, and if so, suppress | |
1981 | -- the message, since it is a cascaded error. | |
1982 | ||
45fc7ddb HK |
1983 | if Nkind_In (N, N_Function_Call, |
1984 | N_Procedure_Call_Statement) | |
996ae0b0 RK |
1985 | then |
1986 | declare | |
1420b484 | 1987 | A : Node_Id; |
996ae0b0 RK |
1988 | E : Node_Id; |
1989 | ||
1990 | begin | |
1420b484 | 1991 | A := First_Actual (N); |
996ae0b0 RK |
1992 | while Present (A) loop |
1993 | E := A; | |
1994 | ||
1995 | if Nkind (E) = N_Parameter_Association then | |
1996 | E := Explicit_Actual_Parameter (E); | |
1997 | end if; | |
1998 | ||
1999 | if Etype (E) = Any_Type then | |
2000 | if Debug_Flag_V then | |
2001 | Write_Str ("Any_Type in call"); | |
2002 | Write_Eol; | |
2003 | end if; | |
2004 | ||
2005 | exit Interp_Loop; | |
2006 | end if; | |
2007 | ||
2008 | Next_Actual (A); | |
2009 | end loop; | |
2010 | end; | |
2011 | ||
aa5147f0 | 2012 | elsif Nkind (N) in N_Binary_Op |
996ae0b0 RK |
2013 | and then (Etype (Left_Opnd (N)) = Any_Type |
2014 | or else Etype (Right_Opnd (N)) = Any_Type) | |
2015 | then | |
2016 | exit Interp_Loop; | |
2017 | ||
2018 | elsif Nkind (N) in N_Unary_Op | |
2019 | and then Etype (Right_Opnd (N)) = Any_Type | |
2020 | then | |
2021 | exit Interp_Loop; | |
2022 | end if; | |
2023 | ||
2024 | -- Not that special case, so issue message using the | |
2025 | -- flag Ambiguous to control printing of the header | |
2026 | -- message only at the start of an ambiguous set. | |
2027 | ||
2028 | if not Ambiguous then | |
aa180613 RD |
2029 | if Nkind (N) = N_Function_Call |
2030 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2031 | then | |
2032 | Error_Msg_N | |
2033 | ("ambiguous expression " | |
2034 | & "(cannot resolve indirect call)!", N); | |
2035 | else | |
483c78cb | 2036 | Error_Msg_NE -- CODEFIX |
aa180613 RD |
2037 | ("ambiguous expression (cannot resolve&)!", |
2038 | N, It.Nam); | |
2039 | end if; | |
fbf5a39b | 2040 | |
996ae0b0 | 2041 | Ambiguous := True; |
0669bebe GB |
2042 | |
2043 | if Nkind (Parent (Seen)) = N_Full_Type_Declaration then | |
2044 | Error_Msg_N | |
2045 | ("\\possible interpretation (inherited)#!", N); | |
2046 | else | |
4e7a4f6e AC |
2047 | Error_Msg_N -- CODEFIX |
2048 | ("\\possible interpretation#!", N); | |
0669bebe | 2049 | end if; |
996ae0b0 RK |
2050 | end if; |
2051 | ||
2052 | Error_Msg_Sloc := Sloc (It.Nam); | |
996ae0b0 | 2053 | |
fbf5a39b | 2054 | -- By default, the error message refers to the candidate |
0669bebe GB |
2055 | -- interpretation. But if it is a predefined operator, it |
2056 | -- is implicitly declared at the declaration of the type | |
2057 | -- of the operand. Recover the sloc of that declaration | |
2058 | -- for the error message. | |
fbf5a39b AC |
2059 | |
2060 | if Nkind (N) in N_Op | |
2061 | and then Scope (It.Nam) = Standard_Standard | |
2062 | and then not Is_Overloaded (Right_Opnd (N)) | |
0669bebe GB |
2063 | and then Scope (Base_Type (Etype (Right_Opnd (N)))) /= |
2064 | Standard_Standard | |
fbf5a39b AC |
2065 | then |
2066 | Err_Type := First_Subtype (Etype (Right_Opnd (N))); | |
2067 | ||
2068 | if Comes_From_Source (Err_Type) | |
2069 | and then Present (Parent (Err_Type)) | |
2070 | then | |
2071 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2072 | end if; | |
2073 | ||
2074 | elsif Nkind (N) in N_Binary_Op | |
2075 | and then Scope (It.Nam) = Standard_Standard | |
2076 | and then not Is_Overloaded (Left_Opnd (N)) | |
0669bebe GB |
2077 | and then Scope (Base_Type (Etype (Left_Opnd (N)))) /= |
2078 | Standard_Standard | |
fbf5a39b AC |
2079 | then |
2080 | Err_Type := First_Subtype (Etype (Left_Opnd (N))); | |
2081 | ||
2082 | if Comes_From_Source (Err_Type) | |
2083 | and then Present (Parent (Err_Type)) | |
2084 | then | |
2085 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2086 | end if; | |
aa180613 RD |
2087 | |
2088 | -- If this is an indirect call, use the subprogram_type | |
2089 | -- in the message, to have a meaningful location. | |
2090 | -- Indicate as well if this is an inherited operation, | |
2091 | -- created by a type declaration. | |
2092 | ||
2093 | elsif Nkind (N) = N_Function_Call | |
2094 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2095 | and then Is_Type (It.Nam) | |
2096 | then | |
2097 | Err_Type := It.Nam; | |
2098 | Error_Msg_Sloc := | |
2099 | Sloc (Associated_Node_For_Itype (Err_Type)); | |
fbf5a39b AC |
2100 | else |
2101 | Err_Type := Empty; | |
2102 | end if; | |
2103 | ||
2104 | if Nkind (N) in N_Op | |
2105 | and then Scope (It.Nam) = Standard_Standard | |
2106 | and then Present (Err_Type) | |
2107 | then | |
aa5147f0 ES |
2108 | -- Special-case the message for universal_fixed |
2109 | -- operators, which are not declared with the type | |
2110 | -- of the operand, but appear forever in Standard. | |
2111 | ||
2112 | if It.Typ = Universal_Fixed | |
2113 | and then Scope (It.Nam) = Standard_Standard | |
2114 | then | |
2115 | Error_Msg_N | |
2116 | ("\\possible interpretation as " & | |
2117 | "universal_fixed operation " & | |
2118 | "(RM 4.5.5 (19))", N); | |
2119 | else | |
2120 | Error_Msg_N | |
2121 | ("\\possible interpretation (predefined)#!", N); | |
2122 | end if; | |
aa180613 RD |
2123 | |
2124 | elsif | |
2125 | Nkind (Parent (It.Nam)) = N_Full_Type_Declaration | |
2126 | then | |
2127 | Error_Msg_N | |
2128 | ("\\possible interpretation (inherited)#!", N); | |
fbf5a39b | 2129 | else |
4e7a4f6e AC |
2130 | Error_Msg_N -- CODEFIX |
2131 | ("\\possible interpretation#!", N); | |
fbf5a39b | 2132 | end if; |
996ae0b0 | 2133 | |
996ae0b0 RK |
2134 | end if; |
2135 | end if; | |
2136 | ||
0669bebe GB |
2137 | -- We have a matching interpretation, Expr_Type is the type |
2138 | -- from this interpretation, and Seen is the entity. | |
996ae0b0 | 2139 | |
0669bebe GB |
2140 | -- For an operator, just set the entity name. The type will be |
2141 | -- set by the specific operator resolution routine. | |
996ae0b0 RK |
2142 | |
2143 | if Nkind (N) in N_Op then | |
2144 | Set_Entity (N, Seen); | |
2145 | Generate_Reference (Seen, N); | |
2146 | ||
2147 | elsif Nkind (N) = N_Character_Literal then | |
2148 | Set_Etype (N, Expr_Type); | |
2149 | ||
e0ba1bfd ES |
2150 | elsif Nkind (N) = N_Conditional_Expression then |
2151 | Set_Etype (N, Expr_Type); | |
2152 | ||
996ae0b0 | 2153 | -- For an explicit dereference, attribute reference, range, |
0669bebe GB |
2154 | -- short-circuit form (which is not an operator node), or call |
2155 | -- with a name that is an explicit dereference, there is | |
2156 | -- nothing to be done at this point. | |
996ae0b0 | 2157 | |
45fc7ddb HK |
2158 | elsif Nkind_In (N, N_Explicit_Dereference, |
2159 | N_Attribute_Reference, | |
2160 | N_And_Then, | |
2161 | N_Indexed_Component, | |
2162 | N_Or_Else, | |
2163 | N_Range, | |
2164 | N_Selected_Component, | |
2165 | N_Slice) | |
996ae0b0 RK |
2166 | or else Nkind (Name (N)) = N_Explicit_Dereference |
2167 | then | |
2168 | null; | |
2169 | ||
0669bebe GB |
2170 | -- For procedure or function calls, set the type of the name, |
2171 | -- and also the entity pointer for the prefix | |
996ae0b0 | 2172 | |
45fc7ddb | 2173 | elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call) |
996ae0b0 RK |
2174 | and then (Is_Entity_Name (Name (N)) |
2175 | or else Nkind (Name (N)) = N_Operator_Symbol) | |
2176 | then | |
2177 | Set_Etype (Name (N), Expr_Type); | |
2178 | Set_Entity (Name (N), Seen); | |
2179 | Generate_Reference (Seen, Name (N)); | |
2180 | ||
2181 | elsif Nkind (N) = N_Function_Call | |
2182 | and then Nkind (Name (N)) = N_Selected_Component | |
2183 | then | |
2184 | Set_Etype (Name (N), Expr_Type); | |
2185 | Set_Entity (Selector_Name (Name (N)), Seen); | |
2186 | Generate_Reference (Seen, Selector_Name (Name (N))); | |
2187 | ||
2188 | -- For all other cases, just set the type of the Name | |
2189 | ||
2190 | else | |
2191 | Set_Etype (Name (N), Expr_Type); | |
2192 | end if; | |
2193 | ||
996ae0b0 RK |
2194 | end if; |
2195 | ||
aa5147f0 ES |
2196 | <<Continue>> |
2197 | ||
996ae0b0 RK |
2198 | -- Move to next interpretation |
2199 | ||
c8ef728f | 2200 | exit Interp_Loop when No (It.Typ); |
996ae0b0 RK |
2201 | |
2202 | Get_Next_Interp (I, It); | |
2203 | end loop Interp_Loop; | |
2204 | end if; | |
2205 | ||
2206 | -- At this stage Found indicates whether or not an acceptable | |
2207 | -- interpretation exists. If not, then we have an error, except | |
2208 | -- that if the context is Any_Type as a result of some other error, | |
2209 | -- then we suppress the error report. | |
2210 | ||
2211 | if not Found then | |
2212 | if Typ /= Any_Type then | |
2213 | ||
0669bebe GB |
2214 | -- If type we are looking for is Void, then this is the procedure |
2215 | -- call case, and the error is simply that what we gave is not a | |
2216 | -- procedure name (we think of procedure calls as expressions with | |
2217 | -- types internally, but the user doesn't think of them this way!) | |
996ae0b0 RK |
2218 | |
2219 | if Typ = Standard_Void_Type then | |
91b1417d AC |
2220 | |
2221 | -- Special case message if function used as a procedure | |
2222 | ||
2223 | if Nkind (N) = N_Procedure_Call_Statement | |
2224 | and then Is_Entity_Name (Name (N)) | |
2225 | and then Ekind (Entity (Name (N))) = E_Function | |
2226 | then | |
2227 | Error_Msg_NE | |
2228 | ("cannot use function & in a procedure call", | |
2229 | Name (N), Entity (Name (N))); | |
2230 | ||
0669bebe GB |
2231 | -- Otherwise give general message (not clear what cases this |
2232 | -- covers, but no harm in providing for them!) | |
91b1417d AC |
2233 | |
2234 | else | |
2235 | Error_Msg_N ("expect procedure name in procedure call", N); | |
2236 | end if; | |
2237 | ||
996ae0b0 RK |
2238 | Found := True; |
2239 | ||
2240 | -- Otherwise we do have a subexpression with the wrong type | |
2241 | ||
0669bebe GB |
2242 | -- Check for the case of an allocator which uses an access type |
2243 | -- instead of the designated type. This is a common error and we | |
2244 | -- specialize the message, posting an error on the operand of the | |
2245 | -- allocator, complaining that we expected the designated type of | |
2246 | -- the allocator. | |
996ae0b0 RK |
2247 | |
2248 | elsif Nkind (N) = N_Allocator | |
2249 | and then Ekind (Typ) in Access_Kind | |
2250 | and then Ekind (Etype (N)) in Access_Kind | |
2251 | and then Designated_Type (Etype (N)) = Typ | |
2252 | then | |
2253 | Wrong_Type (Expression (N), Designated_Type (Typ)); | |
2254 | Found := True; | |
2255 | ||
0669bebe GB |
2256 | -- Check for view mismatch on Null in instances, for which the |
2257 | -- view-swapping mechanism has no identifier. | |
17be0cdf ES |
2258 | |
2259 | elsif (In_Instance or else In_Inlined_Body) | |
2260 | and then (Nkind (N) = N_Null) | |
2261 | and then Is_Private_Type (Typ) | |
2262 | and then Is_Access_Type (Full_View (Typ)) | |
2263 | then | |
2264 | Resolve (N, Full_View (Typ)); | |
2265 | Set_Etype (N, Typ); | |
2266 | return; | |
2267 | ||
aa180613 RD |
2268 | -- Check for an aggregate. Sometimes we can get bogus aggregates |
2269 | -- from misuse of parentheses, and we are about to complain about | |
2270 | -- the aggregate without even looking inside it. | |
996ae0b0 | 2271 | |
aa180613 RD |
2272 | -- Instead, if we have an aggregate of type Any_Composite, then |
2273 | -- analyze and resolve the component fields, and then only issue | |
2274 | -- another message if we get no errors doing this (otherwise | |
2275 | -- assume that the errors in the aggregate caused the problem). | |
996ae0b0 RK |
2276 | |
2277 | elsif Nkind (N) = N_Aggregate | |
2278 | and then Etype (N) = Any_Composite | |
2279 | then | |
996ae0b0 RK |
2280 | -- Disable expansion in any case. If there is a type mismatch |
2281 | -- it may be fatal to try to expand the aggregate. The flag | |
2282 | -- would otherwise be set to false when the error is posted. | |
2283 | ||
2284 | Expander_Active := False; | |
2285 | ||
2286 | declare | |
2287 | procedure Check_Aggr (Aggr : Node_Id); | |
aa180613 RD |
2288 | -- Check one aggregate, and set Found to True if we have a |
2289 | -- definite error in any of its elements | |
996ae0b0 RK |
2290 | |
2291 | procedure Check_Elmt (Aelmt : Node_Id); | |
aa180613 RD |
2292 | -- Check one element of aggregate and set Found to True if |
2293 | -- we definitely have an error in the element. | |
2294 | ||
2295 | ---------------- | |
2296 | -- Check_Aggr -- | |
2297 | ---------------- | |
996ae0b0 RK |
2298 | |
2299 | procedure Check_Aggr (Aggr : Node_Id) is | |
2300 | Elmt : Node_Id; | |
2301 | ||
2302 | begin | |
2303 | if Present (Expressions (Aggr)) then | |
2304 | Elmt := First (Expressions (Aggr)); | |
2305 | while Present (Elmt) loop | |
2306 | Check_Elmt (Elmt); | |
2307 | Next (Elmt); | |
2308 | end loop; | |
2309 | end if; | |
2310 | ||
2311 | if Present (Component_Associations (Aggr)) then | |
2312 | Elmt := First (Component_Associations (Aggr)); | |
2313 | while Present (Elmt) loop | |
aa180613 | 2314 | |
0669bebe GB |
2315 | -- If this is a default-initialized component, then |
2316 | -- there is nothing to check. The box will be | |
2317 | -- replaced by the appropriate call during late | |
2318 | -- expansion. | |
aa180613 RD |
2319 | |
2320 | if not Box_Present (Elmt) then | |
2321 | Check_Elmt (Expression (Elmt)); | |
2322 | end if; | |
2323 | ||
996ae0b0 RK |
2324 | Next (Elmt); |
2325 | end loop; | |
2326 | end if; | |
2327 | end Check_Aggr; | |
2328 | ||
fbf5a39b AC |
2329 | ---------------- |
2330 | -- Check_Elmt -- | |
2331 | ---------------- | |
2332 | ||
996ae0b0 RK |
2333 | procedure Check_Elmt (Aelmt : Node_Id) is |
2334 | begin | |
2335 | -- If we have a nested aggregate, go inside it (to | |
2336 | -- attempt a naked analyze-resolve of the aggregate | |
2337 | -- can cause undesirable cascaded errors). Do not | |
2338 | -- resolve expression if it needs a type from context, | |
2339 | -- as for integer * fixed expression. | |
2340 | ||
2341 | if Nkind (Aelmt) = N_Aggregate then | |
2342 | Check_Aggr (Aelmt); | |
2343 | ||
2344 | else | |
2345 | Analyze (Aelmt); | |
2346 | ||
2347 | if not Is_Overloaded (Aelmt) | |
2348 | and then Etype (Aelmt) /= Any_Fixed | |
2349 | then | |
fbf5a39b | 2350 | Resolve (Aelmt); |
996ae0b0 RK |
2351 | end if; |
2352 | ||
2353 | if Etype (Aelmt) = Any_Type then | |
2354 | Found := True; | |
2355 | end if; | |
2356 | end if; | |
2357 | end Check_Elmt; | |
2358 | ||
2359 | begin | |
2360 | Check_Aggr (N); | |
2361 | end; | |
2362 | end if; | |
2363 | ||
2364 | -- If an error message was issued already, Found got reset | |
2365 | -- to True, so if it is still False, issue the standard | |
2366 | -- Wrong_Type message. | |
2367 | ||
2368 | if not Found then | |
2369 | if Is_Overloaded (N) | |
2370 | and then Nkind (N) = N_Function_Call | |
2371 | then | |
65356e64 AC |
2372 | declare |
2373 | Subp_Name : Node_Id; | |
2374 | begin | |
2375 | if Is_Entity_Name (Name (N)) then | |
2376 | Subp_Name := Name (N); | |
2377 | ||
2378 | elsif Nkind (Name (N)) = N_Selected_Component then | |
2379 | ||
a77842bd | 2380 | -- Protected operation: retrieve operation name |
65356e64 AC |
2381 | |
2382 | Subp_Name := Selector_Name (Name (N)); | |
2383 | else | |
2384 | raise Program_Error; | |
2385 | end if; | |
2386 | ||
2387 | Error_Msg_Node_2 := Typ; | |
2388 | Error_Msg_NE ("no visible interpretation of&" & | |
2389 | " matches expected type&", N, Subp_Name); | |
2390 | end; | |
996ae0b0 RK |
2391 | |
2392 | if All_Errors_Mode then | |
2393 | declare | |
2394 | Index : Interp_Index; | |
2395 | It : Interp; | |
2396 | ||
2397 | begin | |
aa180613 | 2398 | Error_Msg_N ("\\possible interpretations:", N); |
996ae0b0 | 2399 | |
1420b484 | 2400 | Get_First_Interp (Name (N), Index, It); |
996ae0b0 | 2401 | while Present (It.Nam) loop |
ea985d95 | 2402 | Error_Msg_Sloc := Sloc (It.Nam); |
aa5147f0 ES |
2403 | Error_Msg_Node_2 := It.Nam; |
2404 | Error_Msg_NE | |
2405 | ("\\ type& for & declared#", N, It.Typ); | |
996ae0b0 RK |
2406 | Get_Next_Interp (Index, It); |
2407 | end loop; | |
2408 | end; | |
aa5147f0 | 2409 | |
996ae0b0 RK |
2410 | else |
2411 | Error_Msg_N ("\use -gnatf for details", N); | |
2412 | end if; | |
2413 | else | |
2414 | Wrong_Type (N, Typ); | |
2415 | end if; | |
2416 | end if; | |
2417 | end if; | |
2418 | ||
2419 | Resolution_Failed; | |
2420 | return; | |
2421 | ||
2422 | -- Test if we have more than one interpretation for the context | |
2423 | ||
2424 | elsif Ambiguous then | |
2425 | Resolution_Failed; | |
2426 | return; | |
2427 | ||
2428 | -- Here we have an acceptable interpretation for the context | |
2429 | ||
2430 | else | |
996ae0b0 RK |
2431 | -- Propagate type information and normalize tree for various |
2432 | -- predefined operations. If the context only imposes a class of | |
2433 | -- types, rather than a specific type, propagate the actual type | |
2434 | -- downward. | |
2435 | ||
2436 | if Typ = Any_Integer | |
2437 | or else Typ = Any_Boolean | |
2438 | or else Typ = Any_Modular | |
2439 | or else Typ = Any_Real | |
2440 | or else Typ = Any_Discrete | |
2441 | then | |
2442 | Ctx_Type := Expr_Type; | |
2443 | ||
2444 | -- Any_Fixed is legal in a real context only if a specific | |
2445 | -- fixed point type is imposed. If Norman Cohen can be | |
2446 | -- confused by this, it deserves a separate message. | |
2447 | ||
2448 | if Typ = Any_Real | |
2449 | and then Expr_Type = Any_Fixed | |
2450 | then | |
758c442c | 2451 | Error_Msg_N ("illegal context for mixed mode operation", N); |
996ae0b0 RK |
2452 | Set_Etype (N, Universal_Real); |
2453 | Ctx_Type := Universal_Real; | |
2454 | end if; | |
2455 | end if; | |
2456 | ||
f3d57416 | 2457 | -- A user-defined operator is transformed into a function call at |
0ab80019 AC |
2458 | -- this point, so that further processing knows that operators are |
2459 | -- really operators (i.e. are predefined operators). User-defined | |
2460 | -- operators that are intrinsic are just renamings of the predefined | |
2461 | -- ones, and need not be turned into calls either, but if they rename | |
2462 | -- a different operator, we must transform the node accordingly. | |
2463 | -- Instantiations of Unchecked_Conversion are intrinsic but are | |
2464 | -- treated as functions, even if given an operator designator. | |
2465 | ||
2466 | if Nkind (N) in N_Op | |
2467 | and then Present (Entity (N)) | |
2468 | and then Ekind (Entity (N)) /= E_Operator | |
2469 | then | |
2470 | ||
2471 | if not Is_Predefined_Op (Entity (N)) then | |
2472 | Rewrite_Operator_As_Call (N, Entity (N)); | |
2473 | ||
615cbd95 AC |
2474 | elsif Present (Alias (Entity (N))) |
2475 | and then | |
45fc7ddb HK |
2476 | Nkind (Parent (Parent (Entity (N)))) = |
2477 | N_Subprogram_Renaming_Declaration | |
615cbd95 | 2478 | then |
0ab80019 AC |
2479 | Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ); |
2480 | ||
2481 | -- If the node is rewritten, it will be fully resolved in | |
2482 | -- Rewrite_Renamed_Operator. | |
2483 | ||
2484 | if Analyzed (N) then | |
2485 | return; | |
2486 | end if; | |
2487 | end if; | |
2488 | end if; | |
2489 | ||
996ae0b0 RK |
2490 | case N_Subexpr'(Nkind (N)) is |
2491 | ||
2492 | when N_Aggregate => Resolve_Aggregate (N, Ctx_Type); | |
2493 | ||
2494 | when N_Allocator => Resolve_Allocator (N, Ctx_Type); | |
2495 | ||
514d0fc5 | 2496 | when N_Short_Circuit |
996ae0b0 RK |
2497 | => Resolve_Short_Circuit (N, Ctx_Type); |
2498 | ||
2499 | when N_Attribute_Reference | |
2500 | => Resolve_Attribute (N, Ctx_Type); | |
2501 | ||
2502 | when N_Character_Literal | |
2503 | => Resolve_Character_Literal (N, Ctx_Type); | |
2504 | ||
2505 | when N_Conditional_Expression | |
2506 | => Resolve_Conditional_Expression (N, Ctx_Type); | |
2507 | ||
2508 | when N_Expanded_Name | |
2509 | => Resolve_Entity_Name (N, Ctx_Type); | |
2510 | ||
2511 | when N_Extension_Aggregate | |
2512 | => Resolve_Extension_Aggregate (N, Ctx_Type); | |
2513 | ||
2514 | when N_Explicit_Dereference | |
2515 | => Resolve_Explicit_Dereference (N, Ctx_Type); | |
2516 | ||
2517 | when N_Function_Call | |
2518 | => Resolve_Call (N, Ctx_Type); | |
2519 | ||
2520 | when N_Identifier | |
2521 | => Resolve_Entity_Name (N, Ctx_Type); | |
2522 | ||
996ae0b0 RK |
2523 | when N_Indexed_Component |
2524 | => Resolve_Indexed_Component (N, Ctx_Type); | |
2525 | ||
2526 | when N_Integer_Literal | |
2527 | => Resolve_Integer_Literal (N, Ctx_Type); | |
2528 | ||
0669bebe GB |
2529 | when N_Membership_Test |
2530 | => Resolve_Membership_Op (N, Ctx_Type); | |
2531 | ||
996ae0b0 RK |
2532 | when N_Null => Resolve_Null (N, Ctx_Type); |
2533 | ||
2534 | when N_Op_And | N_Op_Or | N_Op_Xor | |
2535 | => Resolve_Logical_Op (N, Ctx_Type); | |
2536 | ||
2537 | when N_Op_Eq | N_Op_Ne | |
2538 | => Resolve_Equality_Op (N, Ctx_Type); | |
2539 | ||
2540 | when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge | |
2541 | => Resolve_Comparison_Op (N, Ctx_Type); | |
2542 | ||
2543 | when N_Op_Not => Resolve_Op_Not (N, Ctx_Type); | |
2544 | ||
2545 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | | |
2546 | N_Op_Divide | N_Op_Mod | N_Op_Rem | |
2547 | ||
2548 | => Resolve_Arithmetic_Op (N, Ctx_Type); | |
2549 | ||
2550 | when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type); | |
2551 | ||
2552 | when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type); | |
2553 | ||
2554 | when N_Op_Plus | N_Op_Minus | N_Op_Abs | |
2555 | => Resolve_Unary_Op (N, Ctx_Type); | |
2556 | ||
2557 | when N_Op_Shift => Resolve_Shift (N, Ctx_Type); | |
2558 | ||
2559 | when N_Procedure_Call_Statement | |
2560 | => Resolve_Call (N, Ctx_Type); | |
2561 | ||
2562 | when N_Operator_Symbol | |
2563 | => Resolve_Operator_Symbol (N, Ctx_Type); | |
2564 | ||
2565 | when N_Qualified_Expression | |
2566 | => Resolve_Qualified_Expression (N, Ctx_Type); | |
2567 | ||
2568 | when N_Raise_xxx_Error | |
2569 | => Set_Etype (N, Ctx_Type); | |
2570 | ||
2571 | when N_Range => Resolve_Range (N, Ctx_Type); | |
2572 | ||
2573 | when N_Real_Literal | |
2574 | => Resolve_Real_Literal (N, Ctx_Type); | |
2575 | ||
2576 | when N_Reference => Resolve_Reference (N, Ctx_Type); | |
2577 | ||
2578 | when N_Selected_Component | |
2579 | => Resolve_Selected_Component (N, Ctx_Type); | |
2580 | ||
2581 | when N_Slice => Resolve_Slice (N, Ctx_Type); | |
2582 | ||
2583 | when N_String_Literal | |
2584 | => Resolve_String_Literal (N, Ctx_Type); | |
2585 | ||
2586 | when N_Subprogram_Info | |
2587 | => Resolve_Subprogram_Info (N, Ctx_Type); | |
2588 | ||
2589 | when N_Type_Conversion | |
2590 | => Resolve_Type_Conversion (N, Ctx_Type); | |
2591 | ||
2592 | when N_Unchecked_Expression => | |
2593 | Resolve_Unchecked_Expression (N, Ctx_Type); | |
2594 | ||
2595 | when N_Unchecked_Type_Conversion => | |
2596 | Resolve_Unchecked_Type_Conversion (N, Ctx_Type); | |
2597 | ||
2598 | end case; | |
2599 | ||
2600 | -- If the subexpression was replaced by a non-subexpression, then | |
2601 | -- all we do is to expand it. The only legitimate case we know of | |
2602 | -- is converting procedure call statement to entry call statements, | |
2603 | -- but there may be others, so we are making this test general. | |
2604 | ||
2605 | if Nkind (N) not in N_Subexpr then | |
2606 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2607 | Expand (N); | |
2608 | return; | |
2609 | end if; | |
2610 | ||
2611 | -- The expression is definitely NOT overloaded at this point, so | |
2612 | -- we reset the Is_Overloaded flag to avoid any confusion when | |
2613 | -- reanalyzing the node. | |
2614 | ||
2615 | Set_Is_Overloaded (N, False); | |
2616 | ||
2617 | -- Freeze expression type, entity if it is a name, and designated | |
fbf5a39b | 2618 | -- type if it is an allocator (RM 13.14(10,11,13)). |
996ae0b0 RK |
2619 | |
2620 | -- Now that the resolution of the type of the node is complete, | |
2621 | -- and we did not detect an error, we can expand this node. We | |
2622 | -- skip the expand call if we are in a default expression, see | |
2623 | -- section "Handling of Default Expressions" in Sem spec. | |
2624 | ||
2625 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2626 | ||
2627 | -- We unconditionally freeze the expression, even if we are in | |
2628 | -- default expression mode (the Freeze_Expression routine tests | |
2629 | -- this flag and only freezes static types if it is set). | |
2630 | ||
2631 | Freeze_Expression (N); | |
2632 | ||
2633 | -- Now we can do the expansion | |
2634 | ||
2635 | Expand (N); | |
2636 | end if; | |
996ae0b0 RK |
2637 | end Resolve; |
2638 | ||
fbf5a39b AC |
2639 | ------------- |
2640 | -- Resolve -- | |
2641 | ------------- | |
2642 | ||
996ae0b0 RK |
2643 | -- Version with check(s) suppressed |
2644 | ||
2645 | procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is | |
2646 | begin | |
2647 | if Suppress = All_Checks then | |
2648 | declare | |
fbf5a39b | 2649 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
2650 | begin |
2651 | Scope_Suppress := (others => True); | |
2652 | Resolve (N, Typ); | |
2653 | Scope_Suppress := Svg; | |
2654 | end; | |
2655 | ||
2656 | else | |
2657 | declare | |
fbf5a39b | 2658 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 | 2659 | begin |
fbf5a39b | 2660 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 2661 | Resolve (N, Typ); |
fbf5a39b | 2662 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
2663 | end; |
2664 | end if; | |
2665 | end Resolve; | |
2666 | ||
fbf5a39b AC |
2667 | ------------- |
2668 | -- Resolve -- | |
2669 | ------------- | |
2670 | ||
2671 | -- Version with implicit type | |
2672 | ||
2673 | procedure Resolve (N : Node_Id) is | |
2674 | begin | |
2675 | Resolve (N, Etype (N)); | |
2676 | end Resolve; | |
2677 | ||
996ae0b0 RK |
2678 | --------------------- |
2679 | -- Resolve_Actuals -- | |
2680 | --------------------- | |
2681 | ||
2682 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is | |
2683 | Loc : constant Source_Ptr := Sloc (N); | |
2684 | A : Node_Id; | |
2685 | F : Entity_Id; | |
2686 | A_Typ : Entity_Id; | |
2687 | F_Typ : Entity_Id; | |
2688 | Prev : Node_Id := Empty; | |
67ce0d7e | 2689 | Orig_A : Node_Id; |
996ae0b0 | 2690 | |
45fc7ddb HK |
2691 | procedure Check_Argument_Order; |
2692 | -- Performs a check for the case where the actuals are all simple | |
2693 | -- identifiers that correspond to the formal names, but in the wrong | |
2694 | -- order, which is considered suspicious and cause for a warning. | |
2695 | ||
b7d1f17f HK |
2696 | procedure Check_Prefixed_Call; |
2697 | -- If the original node is an overloaded call in prefix notation, | |
2698 | -- insert an 'Access or a dereference as needed over the first actual. | |
2699 | -- Try_Object_Operation has already verified that there is a valid | |
2700 | -- interpretation, but the form of the actual can only be determined | |
2701 | -- once the primitive operation is identified. | |
2702 | ||
996ae0b0 RK |
2703 | procedure Insert_Default; |
2704 | -- If the actual is missing in a call, insert in the actuals list | |
2705 | -- an instance of the default expression. The insertion is always | |
2706 | -- a named association. | |
2707 | ||
fbf5a39b AC |
2708 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean; |
2709 | -- Check whether T1 and T2, or their full views, are derived from a | |
2710 | -- common type. Used to enforce the restrictions on array conversions | |
2711 | -- of AI95-00246. | |
2712 | ||
a7a3cf5c AC |
2713 | function Static_Concatenation (N : Node_Id) return Boolean; |
2714 | -- Predicate to determine whether an actual that is a concatenation | |
2715 | -- will be evaluated statically and does not need a transient scope. | |
2716 | -- This must be determined before the actual is resolved and expanded | |
2717 | -- because if needed the transient scope must be introduced earlier. | |
2718 | ||
45fc7ddb HK |
2719 | -------------------------- |
2720 | -- Check_Argument_Order -- | |
2721 | -------------------------- | |
2722 | ||
2723 | procedure Check_Argument_Order is | |
2724 | begin | |
2725 | -- Nothing to do if no parameters, or original node is neither a | |
2726 | -- function call nor a procedure call statement (happens in the | |
2727 | -- operator-transformed-to-function call case), or the call does | |
2728 | -- not come from source, or this warning is off. | |
2729 | ||
2730 | if not Warn_On_Parameter_Order | |
2731 | or else | |
2732 | No (Parameter_Associations (N)) | |
2733 | or else | |
2734 | not Nkind_In (Original_Node (N), N_Procedure_Call_Statement, | |
2735 | N_Function_Call) | |
2736 | or else | |
2737 | not Comes_From_Source (N) | |
2738 | then | |
2739 | return; | |
2740 | end if; | |
2741 | ||
2742 | declare | |
2743 | Nargs : constant Nat := List_Length (Parameter_Associations (N)); | |
2744 | ||
2745 | begin | |
2746 | -- Nothing to do if only one parameter | |
2747 | ||
2748 | if Nargs < 2 then | |
2749 | return; | |
2750 | end if; | |
2751 | ||
2752 | -- Here if at least two arguments | |
2753 | ||
2754 | declare | |
2755 | Actuals : array (1 .. Nargs) of Node_Id; | |
2756 | Actual : Node_Id; | |
2757 | Formal : Node_Id; | |
2758 | ||
2759 | Wrong_Order : Boolean := False; | |
2760 | -- Set True if an out of order case is found | |
2761 | ||
2762 | begin | |
2763 | -- Collect identifier names of actuals, fail if any actual is | |
2764 | -- not a simple identifier, and record max length of name. | |
2765 | ||
2766 | Actual := First (Parameter_Associations (N)); | |
2767 | for J in Actuals'Range loop | |
2768 | if Nkind (Actual) /= N_Identifier then | |
2769 | return; | |
2770 | else | |
2771 | Actuals (J) := Actual; | |
2772 | Next (Actual); | |
2773 | end if; | |
2774 | end loop; | |
2775 | ||
2776 | -- If we got this far, all actuals are identifiers and the list | |
2777 | -- of their names is stored in the Actuals array. | |
2778 | ||
2779 | Formal := First_Formal (Nam); | |
2780 | for J in Actuals'Range loop | |
2781 | ||
2782 | -- If we ran out of formals, that's odd, probably an error | |
2783 | -- which will be detected elsewhere, but abandon the search. | |
2784 | ||
2785 | if No (Formal) then | |
2786 | return; | |
2787 | end if; | |
2788 | ||
2789 | -- If name matches and is in order OK | |
2790 | ||
2791 | if Chars (Formal) = Chars (Actuals (J)) then | |
2792 | null; | |
2793 | ||
2794 | else | |
2795 | -- If no match, see if it is elsewhere in list and if so | |
2796 | -- flag potential wrong order if type is compatible. | |
2797 | ||
2798 | for K in Actuals'Range loop | |
2799 | if Chars (Formal) = Chars (Actuals (K)) | |
2800 | and then | |
2801 | Has_Compatible_Type (Actuals (K), Etype (Formal)) | |
2802 | then | |
2803 | Wrong_Order := True; | |
2804 | goto Continue; | |
2805 | end if; | |
2806 | end loop; | |
2807 | ||
2808 | -- No match | |
2809 | ||
2810 | return; | |
2811 | end if; | |
2812 | ||
2813 | <<Continue>> Next_Formal (Formal); | |
2814 | end loop; | |
2815 | ||
2816 | -- If Formals left over, also probably an error, skip warning | |
2817 | ||
2818 | if Present (Formal) then | |
2819 | return; | |
2820 | end if; | |
2821 | ||
2822 | -- Here we give the warning if something was out of order | |
2823 | ||
2824 | if Wrong_Order then | |
2825 | Error_Msg_N | |
2826 | ("actuals for this call may be in wrong order?", N); | |
2827 | end if; | |
2828 | end; | |
2829 | end; | |
2830 | end Check_Argument_Order; | |
2831 | ||
b7d1f17f HK |
2832 | ------------------------- |
2833 | -- Check_Prefixed_Call -- | |
2834 | ------------------------- | |
2835 | ||
2836 | procedure Check_Prefixed_Call is | |
2837 | Act : constant Node_Id := First_Actual (N); | |
2838 | A_Type : constant Entity_Id := Etype (Act); | |
2839 | F_Type : constant Entity_Id := Etype (First_Formal (Nam)); | |
2840 | Orig : constant Node_Id := Original_Node (N); | |
2841 | New_A : Node_Id; | |
2842 | ||
2843 | begin | |
2844 | -- Check whether the call is a prefixed call, with or without | |
2845 | -- additional actuals. | |
2846 | ||
2847 | if Nkind (Orig) = N_Selected_Component | |
2848 | or else | |
2849 | (Nkind (Orig) = N_Indexed_Component | |
2850 | and then Nkind (Prefix (Orig)) = N_Selected_Component | |
2851 | and then Is_Entity_Name (Prefix (Prefix (Orig))) | |
2852 | and then Is_Entity_Name (Act) | |
2853 | and then Chars (Act) = Chars (Prefix (Prefix (Orig)))) | |
2854 | then | |
2855 | if Is_Access_Type (A_Type) | |
2856 | and then not Is_Access_Type (F_Type) | |
2857 | then | |
2858 | -- Introduce dereference on object in prefix | |
2859 | ||
2860 | New_A := | |
2861 | Make_Explicit_Dereference (Sloc (Act), | |
2862 | Prefix => Relocate_Node (Act)); | |
2863 | Rewrite (Act, New_A); | |
2864 | Analyze (Act); | |
2865 | ||
2866 | elsif Is_Access_Type (F_Type) | |
2867 | and then not Is_Access_Type (A_Type) | |
2868 | then | |
2869 | -- Introduce an implicit 'Access in prefix | |
2870 | ||
2871 | if not Is_Aliased_View (Act) then | |
2872 | Error_Msg_NE | |
2873 | ("object in prefixed call to& must be aliased" | |
aa5147f0 | 2874 | & " (RM-2005 4.3.1 (13))", |
b7d1f17f HK |
2875 | Prefix (Act), Nam); |
2876 | end if; | |
2877 | ||
2878 | Rewrite (Act, | |
2879 | Make_Attribute_Reference (Loc, | |
2880 | Attribute_Name => Name_Access, | |
2881 | Prefix => Relocate_Node (Act))); | |
2882 | end if; | |
2883 | ||
2884 | Analyze (Act); | |
2885 | end if; | |
2886 | end Check_Prefixed_Call; | |
2887 | ||
996ae0b0 RK |
2888 | -------------------- |
2889 | -- Insert_Default -- | |
2890 | -------------------- | |
2891 | ||
2892 | procedure Insert_Default is | |
2893 | Actval : Node_Id; | |
2894 | Assoc : Node_Id; | |
2895 | ||
2896 | begin | |
fbf5a39b | 2897 | -- Missing argument in call, nothing to insert |
996ae0b0 | 2898 | |
fbf5a39b AC |
2899 | if No (Default_Value (F)) then |
2900 | return; | |
2901 | ||
2902 | else | |
2903 | -- Note that we do a full New_Copy_Tree, so that any associated | |
2904 | -- Itypes are properly copied. This may not be needed any more, | |
2905 | -- but it does no harm as a safety measure! Defaults of a generic | |
2906 | -- formal may be out of bounds of the corresponding actual (see | |
2907 | -- cc1311b) and an additional check may be required. | |
996ae0b0 | 2908 | |
b7d1f17f HK |
2909 | Actval := |
2910 | New_Copy_Tree | |
2911 | (Default_Value (F), | |
2912 | New_Scope => Current_Scope, | |
2913 | New_Sloc => Loc); | |
996ae0b0 RK |
2914 | |
2915 | if Is_Concurrent_Type (Scope (Nam)) | |
2916 | and then Has_Discriminants (Scope (Nam)) | |
2917 | then | |
2918 | Replace_Actual_Discriminants (N, Actval); | |
2919 | end if; | |
2920 | ||
2921 | if Is_Overloadable (Nam) | |
2922 | and then Present (Alias (Nam)) | |
2923 | then | |
2924 | if Base_Type (Etype (F)) /= Base_Type (Etype (Actval)) | |
2925 | and then not Is_Tagged_Type (Etype (F)) | |
2926 | then | |
2927 | -- If default is a real literal, do not introduce a | |
2928 | -- conversion whose effect may depend on the run-time | |
2929 | -- size of universal real. | |
2930 | ||
2931 | if Nkind (Actval) = N_Real_Literal then | |
2932 | Set_Etype (Actval, Base_Type (Etype (F))); | |
2933 | else | |
2934 | Actval := Unchecked_Convert_To (Etype (F), Actval); | |
2935 | end if; | |
2936 | end if; | |
2937 | ||
2938 | if Is_Scalar_Type (Etype (F)) then | |
2939 | Enable_Range_Check (Actval); | |
2940 | end if; | |
2941 | ||
996ae0b0 RK |
2942 | Set_Parent (Actval, N); |
2943 | ||
2944 | -- Resolve aggregates with their base type, to avoid scope | |
f3d57416 | 2945 | -- anomalies: the subtype was first built in the subprogram |
996ae0b0 RK |
2946 | -- declaration, and the current call may be nested. |
2947 | ||
2948 | if Nkind (Actval) = N_Aggregate | |
2949 | and then Has_Discriminants (Etype (Actval)) | |
2950 | then | |
2951 | Analyze_And_Resolve (Actval, Base_Type (Etype (Actval))); | |
2952 | else | |
2953 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
2954 | end if; | |
fbf5a39b AC |
2955 | |
2956 | else | |
2957 | Set_Parent (Actval, N); | |
2958 | ||
a77842bd | 2959 | -- See note above concerning aggregates |
fbf5a39b AC |
2960 | |
2961 | if Nkind (Actval) = N_Aggregate | |
2962 | and then Has_Discriminants (Etype (Actval)) | |
2963 | then | |
2964 | Analyze_And_Resolve (Actval, Base_Type (Etype (Actval))); | |
2965 | ||
2966 | -- Resolve entities with their own type, which may differ | |
2967 | -- from the type of a reference in a generic context (the | |
2968 | -- view swapping mechanism did not anticipate the re-analysis | |
2969 | -- of default values in calls). | |
2970 | ||
2971 | elsif Is_Entity_Name (Actval) then | |
2972 | Analyze_And_Resolve (Actval, Etype (Entity (Actval))); | |
2973 | ||
2974 | else | |
2975 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
2976 | end if; | |
996ae0b0 RK |
2977 | end if; |
2978 | ||
2979 | -- If default is a tag indeterminate function call, propagate | |
2980 | -- tag to obtain proper dispatching. | |
2981 | ||
2982 | if Is_Controlling_Formal (F) | |
2983 | and then Nkind (Default_Value (F)) = N_Function_Call | |
2984 | then | |
2985 | Set_Is_Controlling_Actual (Actval); | |
2986 | end if; | |
2987 | ||
996ae0b0 RK |
2988 | end if; |
2989 | ||
2990 | -- If the default expression raises constraint error, then just | |
2991 | -- silently replace it with an N_Raise_Constraint_Error node, | |
2992 | -- since we already gave the warning on the subprogram spec. | |
2993 | ||
2994 | if Raises_Constraint_Error (Actval) then | |
2995 | Rewrite (Actval, | |
07fc65c4 GB |
2996 | Make_Raise_Constraint_Error (Loc, |
2997 | Reason => CE_Range_Check_Failed)); | |
996ae0b0 RK |
2998 | Set_Raises_Constraint_Error (Actval); |
2999 | Set_Etype (Actval, Etype (F)); | |
3000 | end if; | |
3001 | ||
3002 | Assoc := | |
3003 | Make_Parameter_Association (Loc, | |
3004 | Explicit_Actual_Parameter => Actval, | |
3005 | Selector_Name => Make_Identifier (Loc, Chars (F))); | |
3006 | ||
3007 | -- Case of insertion is first named actual | |
3008 | ||
3009 | if No (Prev) or else | |
3010 | Nkind (Parent (Prev)) /= N_Parameter_Association | |
3011 | then | |
3012 | Set_Next_Named_Actual (Assoc, First_Named_Actual (N)); | |
3013 | Set_First_Named_Actual (N, Actval); | |
3014 | ||
3015 | if No (Prev) then | |
c8ef728f | 3016 | if No (Parameter_Associations (N)) then |
996ae0b0 RK |
3017 | Set_Parameter_Associations (N, New_List (Assoc)); |
3018 | else | |
3019 | Append (Assoc, Parameter_Associations (N)); | |
3020 | end if; | |
3021 | ||
3022 | else | |
3023 | Insert_After (Prev, Assoc); | |
3024 | end if; | |
3025 | ||
3026 | -- Case of insertion is not first named actual | |
3027 | ||
3028 | else | |
3029 | Set_Next_Named_Actual | |
3030 | (Assoc, Next_Named_Actual (Parent (Prev))); | |
3031 | Set_Next_Named_Actual (Parent (Prev), Actval); | |
3032 | Append (Assoc, Parameter_Associations (N)); | |
3033 | end if; | |
3034 | ||
3035 | Mark_Rewrite_Insertion (Assoc); | |
3036 | Mark_Rewrite_Insertion (Actval); | |
3037 | ||
3038 | Prev := Actval; | |
3039 | end Insert_Default; | |
3040 | ||
fbf5a39b AC |
3041 | ------------------- |
3042 | -- Same_Ancestor -- | |
3043 | ------------------- | |
3044 | ||
3045 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is | |
3046 | FT1 : Entity_Id := T1; | |
3047 | FT2 : Entity_Id := T2; | |
3048 | ||
3049 | begin | |
3050 | if Is_Private_Type (T1) | |
3051 | and then Present (Full_View (T1)) | |
3052 | then | |
3053 | FT1 := Full_View (T1); | |
3054 | end if; | |
3055 | ||
3056 | if Is_Private_Type (T2) | |
3057 | and then Present (Full_View (T2)) | |
3058 | then | |
3059 | FT2 := Full_View (T2); | |
3060 | end if; | |
3061 | ||
3062 | return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2)); | |
3063 | end Same_Ancestor; | |
3064 | ||
a7a3cf5c AC |
3065 | -------------------------- |
3066 | -- Static_Concatenation -- | |
3067 | -------------------------- | |
3068 | ||
3069 | function Static_Concatenation (N : Node_Id) return Boolean is | |
3070 | begin | |
c72a85f2 TQ |
3071 | case Nkind (N) is |
3072 | when N_String_Literal => | |
3073 | return True; | |
a7a3cf5c | 3074 | |
d81b4bfe TQ |
3075 | when N_Op_Concat => |
3076 | ||
4342eda9 TQ |
3077 | -- Concatenation is static when both operands are static |
3078 | -- and the concatenation operator is a predefined one. | |
3079 | ||
3080 | return Scope (Entity (N)) = Standard_Standard | |
3081 | and then | |
3082 | Static_Concatenation (Left_Opnd (N)) | |
c72a85f2 TQ |
3083 | and then |
3084 | Static_Concatenation (Right_Opnd (N)); | |
3085 | ||
3086 | when others => | |
3087 | if Is_Entity_Name (N) then | |
3088 | declare | |
3089 | Ent : constant Entity_Id := Entity (N); | |
3090 | begin | |
3091 | return Ekind (Ent) = E_Constant | |
3092 | and then Present (Constant_Value (Ent)) | |
d81b4bfe TQ |
3093 | and then |
3094 | Is_Static_Expression (Constant_Value (Ent)); | |
c72a85f2 | 3095 | end; |
a7a3cf5c | 3096 | |
a7a3cf5c AC |
3097 | else |
3098 | return False; | |
3099 | end if; | |
c72a85f2 | 3100 | end case; |
a7a3cf5c AC |
3101 | end Static_Concatenation; |
3102 | ||
996ae0b0 RK |
3103 | -- Start of processing for Resolve_Actuals |
3104 | ||
3105 | begin | |
45fc7ddb HK |
3106 | Check_Argument_Order; |
3107 | ||
b7d1f17f HK |
3108 | if Present (First_Actual (N)) then |
3109 | Check_Prefixed_Call; | |
3110 | end if; | |
3111 | ||
996ae0b0 RK |
3112 | A := First_Actual (N); |
3113 | F := First_Formal (Nam); | |
996ae0b0 | 3114 | while Present (F) loop |
fbf5a39b AC |
3115 | if No (A) and then Needs_No_Actuals (Nam) then |
3116 | null; | |
996ae0b0 | 3117 | |
d81b4bfe TQ |
3118 | -- If we have an error in any actual or formal, indicated by a type |
3119 | -- of Any_Type, then abandon resolution attempt, and set result type | |
3120 | -- to Any_Type. | |
07fc65c4 | 3121 | |
fbf5a39b AC |
3122 | elsif (Present (A) and then Etype (A) = Any_Type) |
3123 | or else Etype (F) = Any_Type | |
07fc65c4 GB |
3124 | then |
3125 | Set_Etype (N, Any_Type); | |
3126 | return; | |
3127 | end if; | |
3128 | ||
e65f50ec ES |
3129 | -- Case where actual is present |
3130 | ||
45fc7ddb | 3131 | -- If the actual is an entity, generate a reference to it now. We |
36fcf362 RD |
3132 | -- do this before the actual is resolved, because a formal of some |
3133 | -- protected subprogram, or a task discriminant, will be rewritten | |
3134 | -- during expansion, and the reference to the source entity may | |
3135 | -- be lost. | |
3136 | ||
3137 | if Present (A) | |
3138 | and then Is_Entity_Name (A) | |
3139 | and then Comes_From_Source (N) | |
3140 | then | |
3141 | Orig_A := Entity (A); | |
3142 | ||
3143 | if Present (Orig_A) then | |
3144 | if Is_Formal (Orig_A) | |
3145 | and then Ekind (F) /= E_In_Parameter | |
3146 | then | |
3147 | Generate_Reference (Orig_A, A, 'm'); | |
36fcf362 RD |
3148 | elsif not Is_Overloaded (A) then |
3149 | Generate_Reference (Orig_A, A); | |
3150 | end if; | |
3151 | end if; | |
3152 | end if; | |
3153 | ||
996ae0b0 RK |
3154 | if Present (A) |
3155 | and then (Nkind (Parent (A)) /= N_Parameter_Association | |
3156 | or else | |
3157 | Chars (Selector_Name (Parent (A))) = Chars (F)) | |
3158 | then | |
45fc7ddb HK |
3159 | -- If style checking mode on, check match of formal name |
3160 | ||
3161 | if Style_Check then | |
3162 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3163 | Check_Identifier (Selector_Name (Parent (A)), F); | |
3164 | end if; | |
3165 | end if; | |
3166 | ||
996ae0b0 RK |
3167 | -- If the formal is Out or In_Out, do not resolve and expand the |
3168 | -- conversion, because it is subsequently expanded into explicit | |
3169 | -- temporaries and assignments. However, the object of the | |
ea985d95 RD |
3170 | -- conversion can be resolved. An exception is the case of tagged |
3171 | -- type conversion with a class-wide actual. In that case we want | |
3172 | -- the tag check to occur and no temporary will be needed (no | |
3173 | -- representation change can occur) and the parameter is passed by | |
3174 | -- reference, so we go ahead and resolve the type conversion. | |
c8ef728f | 3175 | -- Another exception is the case of reference to component or |
ea985d95 RD |
3176 | -- subcomponent of a bit-packed array, in which case we want to |
3177 | -- defer expansion to the point the in and out assignments are | |
3178 | -- performed. | |
996ae0b0 RK |
3179 | |
3180 | if Ekind (F) /= E_In_Parameter | |
3181 | and then Nkind (A) = N_Type_Conversion | |
3182 | and then not Is_Class_Wide_Type (Etype (Expression (A))) | |
3183 | then | |
07fc65c4 GB |
3184 | if Ekind (F) = E_In_Out_Parameter |
3185 | and then Is_Array_Type (Etype (F)) | |
07fc65c4 | 3186 | then |
fbf5a39b AC |
3187 | if Has_Aliased_Components (Etype (Expression (A))) |
3188 | /= Has_Aliased_Components (Etype (F)) | |
3189 | then | |
758c442c | 3190 | |
45fc7ddb HK |
3191 | -- In a view conversion, the conversion must be legal in |
3192 | -- both directions, and thus both component types must be | |
3193 | -- aliased, or neither (4.6 (8)). | |
758c442c | 3194 | |
45fc7ddb | 3195 | -- The additional rule 4.6 (24.9.2) seems unduly |
d81b4bfe TQ |
3196 | -- restrictive: the privacy requirement should not apply |
3197 | -- to generic types, and should be checked in an | |
3198 | -- instance. ARG query is in order ??? | |
45fc7ddb HK |
3199 | |
3200 | Error_Msg_N | |
3201 | ("both component types in a view conversion must be" | |
3202 | & " aliased, or neither", A); | |
3203 | ||
3204 | elsif | |
3205 | not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3206 | then | |
3207 | if Is_By_Reference_Type (Etype (F)) | |
3208 | or else Is_By_Reference_Type (Etype (Expression (A))) | |
758c442c GD |
3209 | then |
3210 | Error_Msg_N | |
45fc7ddb HK |
3211 | ("view conversion between unrelated by reference " & |
3212 | "array types not allowed (\'A'I-00246)", A); | |
3213 | else | |
3214 | declare | |
3215 | Comp_Type : constant Entity_Id := | |
3216 | Component_Type | |
3217 | (Etype (Expression (A))); | |
3218 | begin | |
3219 | if Comes_From_Source (A) | |
3220 | and then Ada_Version >= Ada_05 | |
3221 | and then | |
3222 | ((Is_Private_Type (Comp_Type) | |
3223 | and then not Is_Generic_Type (Comp_Type)) | |
3224 | or else Is_Tagged_Type (Comp_Type) | |
3225 | or else Is_Volatile (Comp_Type)) | |
3226 | then | |
3227 | Error_Msg_N | |
3228 | ("component type of a view conversion cannot" | |
3229 | & " be private, tagged, or volatile" | |
3230 | & " (RM 4.6 (24))", | |
3231 | Expression (A)); | |
3232 | end if; | |
3233 | end; | |
758c442c | 3234 | end if; |
fbf5a39b | 3235 | end if; |
07fc65c4 GB |
3236 | end if; |
3237 | ||
16397eff TQ |
3238 | if (Conversion_OK (A) |
3239 | or else Valid_Conversion (A, Etype (A), Expression (A))) | |
3240 | and then not Is_Ref_To_Bit_Packed_Array (Expression (A)) | |
996ae0b0 | 3241 | then |
fbf5a39b | 3242 | Resolve (Expression (A)); |
996ae0b0 RK |
3243 | end if; |
3244 | ||
b7d1f17f HK |
3245 | -- If the actual is a function call that returns a limited |
3246 | -- unconstrained object that needs finalization, create a | |
3247 | -- transient scope for it, so that it can receive the proper | |
3248 | -- finalization list. | |
3249 | ||
3250 | elsif Nkind (A) = N_Function_Call | |
3251 | and then Is_Limited_Record (Etype (F)) | |
3252 | and then not Is_Constrained (Etype (F)) | |
3253 | and then Expander_Active | |
3254 | and then | |
3255 | (Is_Controlled (Etype (F)) or else Has_Task (Etype (F))) | |
3256 | then | |
3257 | Establish_Transient_Scope (A, False); | |
3258 | ||
a52fefe6 AC |
3259 | -- A small optimization: if one of the actuals is a concatenation |
3260 | -- create a block around a procedure call to recover stack space. | |
3261 | -- This alleviates stack usage when several procedure calls in | |
76e776e5 AC |
3262 | -- the same statement list use concatenation. We do not perform |
3263 | -- this wrapping for code statements, where the argument is a | |
3264 | -- static string, and we want to preserve warnings involving | |
3265 | -- sequences of such statements. | |
a52fefe6 AC |
3266 | |
3267 | elsif Nkind (A) = N_Op_Concat | |
3268 | and then Nkind (N) = N_Procedure_Call_Statement | |
3269 | and then Expander_Active | |
76e776e5 AC |
3270 | and then |
3271 | not (Is_Intrinsic_Subprogram (Nam) | |
3272 | and then Chars (Nam) = Name_Asm) | |
a7a3cf5c | 3273 | and then not Static_Concatenation (A) |
a52fefe6 AC |
3274 | then |
3275 | Establish_Transient_Scope (A, False); | |
3276 | Resolve (A, Etype (F)); | |
3277 | ||
996ae0b0 | 3278 | else |
fbf5a39b AC |
3279 | if Nkind (A) = N_Type_Conversion |
3280 | and then Is_Array_Type (Etype (F)) | |
3281 | and then not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3282 | and then | |
3283 | (Is_Limited_Type (Etype (F)) | |
3284 | or else Is_Limited_Type (Etype (Expression (A)))) | |
3285 | then | |
3286 | Error_Msg_N | |
758c442c GD |
3287 | ("conversion between unrelated limited array types " & |
3288 | "not allowed (\A\I-00246)", A); | |
fbf5a39b | 3289 | |
758c442c GD |
3290 | if Is_Limited_Type (Etype (F)) then |
3291 | Explain_Limited_Type (Etype (F), A); | |
3292 | end if; | |
fbf5a39b | 3293 | |
758c442c GD |
3294 | if Is_Limited_Type (Etype (Expression (A))) then |
3295 | Explain_Limited_Type (Etype (Expression (A)), A); | |
3296 | end if; | |
fbf5a39b AC |
3297 | end if; |
3298 | ||
c8ef728f ES |
3299 | -- (Ada 2005: AI-251): If the actual is an allocator whose |
3300 | -- directly designated type is a class-wide interface, we build | |
3301 | -- an anonymous access type to use it as the type of the | |
3302 | -- allocator. Later, when the subprogram call is expanded, if | |
3303 | -- the interface has a secondary dispatch table the expander | |
3304 | -- will add a type conversion to force the correct displacement | |
3305 | -- of the pointer. | |
3306 | ||
3307 | if Nkind (A) = N_Allocator then | |
3308 | declare | |
3309 | DDT : constant Entity_Id := | |
3310 | Directly_Designated_Type (Base_Type (Etype (F))); | |
45fc7ddb | 3311 | |
c8ef728f | 3312 | New_Itype : Entity_Id; |
45fc7ddb | 3313 | |
c8ef728f ES |
3314 | begin |
3315 | if Is_Class_Wide_Type (DDT) | |
3316 | and then Is_Interface (DDT) | |
3317 | then | |
3318 | New_Itype := Create_Itype (E_Anonymous_Access_Type, A); | |
45fc7ddb | 3319 | Set_Etype (New_Itype, Etype (A)); |
c8ef728f ES |
3320 | Set_Directly_Designated_Type (New_Itype, |
3321 | Directly_Designated_Type (Etype (A))); | |
3322 | Set_Etype (A, New_Itype); | |
3323 | end if; | |
0669bebe GB |
3324 | |
3325 | -- Ada 2005, AI-162:If the actual is an allocator, the | |
3326 | -- innermost enclosing statement is the master of the | |
b7d1f17f HK |
3327 | -- created object. This needs to be done with expansion |
3328 | -- enabled only, otherwise the transient scope will not | |
3329 | -- be removed in the expansion of the wrapped construct. | |
0669bebe | 3330 | |
45fc7ddb | 3331 | if (Is_Controlled (DDT) or else Has_Task (DDT)) |
b7d1f17f | 3332 | and then Expander_Active |
0669bebe GB |
3333 | then |
3334 | Establish_Transient_Scope (A, False); | |
3335 | end if; | |
c8ef728f ES |
3336 | end; |
3337 | end if; | |
3338 | ||
b7d1f17f HK |
3339 | -- (Ada 2005): The call may be to a primitive operation of |
3340 | -- a tagged synchronized type, declared outside of the type. | |
3341 | -- In this case the controlling actual must be converted to | |
3342 | -- its corresponding record type, which is the formal type. | |
45fc7ddb HK |
3343 | -- The actual may be a subtype, either because of a constraint |
3344 | -- or because it is a generic actual, so use base type to | |
3345 | -- locate concurrent type. | |
b7d1f17f | 3346 | |
15e4986c JM |
3347 | A_Typ := Base_Type (Etype (A)); |
3348 | F_Typ := Base_Type (Etype (F)); | |
3349 | ||
3350 | declare | |
3351 | Full_A_Typ : Entity_Id; | |
3352 | ||
3353 | begin | |
3354 | if Present (Full_View (A_Typ)) then | |
3355 | Full_A_Typ := Base_Type (Full_View (A_Typ)); | |
3356 | else | |
3357 | Full_A_Typ := A_Typ; | |
3358 | end if; | |
b7d1f17f | 3359 | |
15e4986c JM |
3360 | -- Tagged synchronized type (case 1): the actual is a |
3361 | -- concurrent type | |
3362 | ||
3363 | if Is_Concurrent_Type (A_Typ) | |
3364 | and then Corresponding_Record_Type (A_Typ) = F_Typ | |
3365 | then | |
3366 | Rewrite (A, | |
3367 | Unchecked_Convert_To | |
3368 | (Corresponding_Record_Type (A_Typ), A)); | |
3369 | Resolve (A, Etype (F)); | |
3370 | ||
3371 | -- Tagged synchronized type (case 2): the formal is a | |
3372 | -- concurrent type | |
3373 | ||
3374 | elsif Ekind (Full_A_Typ) = E_Record_Type | |
3375 | and then Present | |
3376 | (Corresponding_Concurrent_Type (Full_A_Typ)) | |
3377 | and then Is_Concurrent_Type (F_Typ) | |
3378 | and then Present (Corresponding_Record_Type (F_Typ)) | |
3379 | and then Full_A_Typ = Corresponding_Record_Type (F_Typ) | |
3380 | then | |
3381 | Resolve (A, Corresponding_Record_Type (F_Typ)); | |
3382 | ||
3383 | -- Common case | |
3384 | ||
3385 | else | |
3386 | Resolve (A, Etype (F)); | |
3387 | end if; | |
3388 | end; | |
996ae0b0 RK |
3389 | end if; |
3390 | ||
3391 | A_Typ := Etype (A); | |
3392 | F_Typ := Etype (F); | |
3393 | ||
26570b21 RD |
3394 | -- For mode IN, if actual is an entity, and the type of the formal |
3395 | -- has warnings suppressed, then we reset Never_Set_In_Source for | |
3396 | -- the calling entity. The reason for this is to catch cases like | |
3397 | -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram | |
3398 | -- uses trickery to modify an IN parameter. | |
3399 | ||
3400 | if Ekind (F) = E_In_Parameter | |
3401 | and then Is_Entity_Name (A) | |
3402 | and then Present (Entity (A)) | |
3403 | and then Ekind (Entity (A)) = E_Variable | |
3404 | and then Has_Warnings_Off (F_Typ) | |
3405 | then | |
3406 | Set_Never_Set_In_Source (Entity (A), False); | |
3407 | end if; | |
3408 | ||
fbf5a39b AC |
3409 | -- Perform error checks for IN and IN OUT parameters |
3410 | ||
3411 | if Ekind (F) /= E_Out_Parameter then | |
3412 | ||
3413 | -- Check unset reference. For scalar parameters, it is clearly | |
3414 | -- wrong to pass an uninitialized value as either an IN or | |
3415 | -- IN-OUT parameter. For composites, it is also clearly an | |
3416 | -- error to pass a completely uninitialized value as an IN | |
3417 | -- parameter, but the case of IN OUT is trickier. We prefer | |
3418 | -- not to give a warning here. For example, suppose there is | |
3419 | -- a routine that sets some component of a record to False. | |
3420 | -- It is perfectly reasonable to make this IN-OUT and allow | |
3421 | -- either initialized or uninitialized records to be passed | |
3422 | -- in this case. | |
3423 | ||
3424 | -- For partially initialized composite values, we also avoid | |
3425 | -- warnings, since it is quite likely that we are passing a | |
3426 | -- partially initialized value and only the initialized fields | |
3427 | -- will in fact be read in the subprogram. | |
3428 | ||
3429 | if Is_Scalar_Type (A_Typ) | |
3430 | or else (Ekind (F) = E_In_Parameter | |
3431 | and then not Is_Partially_Initialized_Type (A_Typ)) | |
996ae0b0 | 3432 | then |
fbf5a39b | 3433 | Check_Unset_Reference (A); |
996ae0b0 | 3434 | end if; |
996ae0b0 | 3435 | |
758c442c GD |
3436 | -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT |
3437 | -- actual to a nested call, since this is case of reading an | |
3438 | -- out parameter, which is not allowed. | |
996ae0b0 | 3439 | |
0ab80019 | 3440 | if Ada_Version = Ada_83 |
996ae0b0 RK |
3441 | and then Is_Entity_Name (A) |
3442 | and then Ekind (Entity (A)) = E_Out_Parameter | |
3443 | then | |
3444 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", A); | |
3445 | end if; | |
3446 | end if; | |
3447 | ||
67ce0d7e RD |
3448 | -- Case of OUT or IN OUT parameter |
3449 | ||
36fcf362 | 3450 | if Ekind (F) /= E_In_Parameter then |
67ce0d7e RD |
3451 | |
3452 | -- For an Out parameter, check for useless assignment. Note | |
45fc7ddb HK |
3453 | -- that we can't set Last_Assignment this early, because we may |
3454 | -- kill current values in Resolve_Call, and that call would | |
3455 | -- clobber the Last_Assignment field. | |
67ce0d7e | 3456 | |
45fc7ddb HK |
3457 | -- Note: call Warn_On_Useless_Assignment before doing the check |
3458 | -- below for Is_OK_Variable_For_Out_Formal so that the setting | |
3459 | -- of Referenced_As_LHS/Referenced_As_Out_Formal properly | |
3460 | -- reflects the last assignment, not this one! | |
36fcf362 | 3461 | |
67ce0d7e | 3462 | if Ekind (F) = E_Out_Parameter then |
36fcf362 | 3463 | if Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
3464 | and then Is_Entity_Name (A) |
3465 | and then Present (Entity (A)) | |
36fcf362 | 3466 | and then Comes_From_Source (N) |
67ce0d7e | 3467 | then |
36fcf362 | 3468 | Warn_On_Useless_Assignment (Entity (A), A); |
67ce0d7e RD |
3469 | end if; |
3470 | end if; | |
3471 | ||
36fcf362 RD |
3472 | -- Validate the form of the actual. Note that the call to |
3473 | -- Is_OK_Variable_For_Out_Formal generates the required | |
3474 | -- reference in this case. | |
3475 | ||
3476 | if not Is_OK_Variable_For_Out_Formal (A) then | |
3477 | Error_Msg_NE ("actual for& must be a variable", A, F); | |
3478 | end if; | |
3479 | ||
67ce0d7e | 3480 | -- What's the following about??? |
fbf5a39b AC |
3481 | |
3482 | if Is_Entity_Name (A) then | |
3483 | Kill_Checks (Entity (A)); | |
3484 | else | |
3485 | Kill_All_Checks; | |
3486 | end if; | |
3487 | end if; | |
3488 | ||
3489 | if Etype (A) = Any_Type then | |
3490 | Set_Etype (N, Any_Type); | |
3491 | return; | |
3492 | end if; | |
3493 | ||
996ae0b0 RK |
3494 | -- Apply appropriate range checks for in, out, and in-out |
3495 | -- parameters. Out and in-out parameters also need a separate | |
3496 | -- check, if there is a type conversion, to make sure the return | |
3497 | -- value meets the constraints of the variable before the | |
3498 | -- conversion. | |
3499 | ||
3500 | -- Gigi looks at the check flag and uses the appropriate types. | |
3501 | -- For now since one flag is used there is an optimization which | |
3502 | -- might not be done in the In Out case since Gigi does not do | |
3503 | -- any analysis. More thought required about this ??? | |
3504 | ||
3505 | if Ekind (F) = E_In_Parameter | |
3506 | or else Ekind (F) = E_In_Out_Parameter | |
3507 | then | |
3508 | if Is_Scalar_Type (Etype (A)) then | |
3509 | Apply_Scalar_Range_Check (A, F_Typ); | |
3510 | ||
3511 | elsif Is_Array_Type (Etype (A)) then | |
3512 | Apply_Length_Check (A, F_Typ); | |
3513 | ||
3514 | elsif Is_Record_Type (F_Typ) | |
3515 | and then Has_Discriminants (F_Typ) | |
3516 | and then Is_Constrained (F_Typ) | |
3517 | and then (not Is_Derived_Type (F_Typ) | |
3518 | or else Comes_From_Source (Nam)) | |
3519 | then | |
3520 | Apply_Discriminant_Check (A, F_Typ); | |
3521 | ||
3522 | elsif Is_Access_Type (F_Typ) | |
3523 | and then Is_Array_Type (Designated_Type (F_Typ)) | |
3524 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3525 | then | |
3526 | Apply_Length_Check (A, F_Typ); | |
3527 | ||
3528 | elsif Is_Access_Type (F_Typ) | |
3529 | and then Has_Discriminants (Designated_Type (F_Typ)) | |
3530 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3531 | then | |
3532 | Apply_Discriminant_Check (A, F_Typ); | |
3533 | ||
3534 | else | |
3535 | Apply_Range_Check (A, F_Typ); | |
3536 | end if; | |
2820d220 | 3537 | |
0ab80019 | 3538 | -- Ada 2005 (AI-231) |
2820d220 | 3539 | |
0ab80019 | 3540 | if Ada_Version >= Ada_05 |
2820d220 | 3541 | and then Is_Access_Type (F_Typ) |
1420b484 | 3542 | and then Can_Never_Be_Null (F_Typ) |
aa5147f0 | 3543 | and then Known_Null (A) |
2820d220 | 3544 | then |
1420b484 JM |
3545 | Apply_Compile_Time_Constraint_Error |
3546 | (N => A, | |
aa5147f0 | 3547 | Msg => "(Ada 2005) null not allowed in " |
1420b484 JM |
3548 | & "null-excluding formal?", |
3549 | Reason => CE_Null_Not_Allowed); | |
2820d220 | 3550 | end if; |
996ae0b0 RK |
3551 | end if; |
3552 | ||
3553 | if Ekind (F) = E_Out_Parameter | |
3554 | or else Ekind (F) = E_In_Out_Parameter | |
3555 | then | |
996ae0b0 RK |
3556 | if Nkind (A) = N_Type_Conversion then |
3557 | if Is_Scalar_Type (A_Typ) then | |
3558 | Apply_Scalar_Range_Check | |
3559 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3560 | else | |
3561 | Apply_Range_Check | |
3562 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3563 | end if; | |
3564 | ||
3565 | else | |
3566 | if Is_Scalar_Type (F_Typ) then | |
3567 | Apply_Scalar_Range_Check (A, A_Typ, F_Typ); | |
3568 | ||
3569 | elsif Is_Array_Type (F_Typ) | |
3570 | and then Ekind (F) = E_Out_Parameter | |
3571 | then | |
3572 | Apply_Length_Check (A, F_Typ); | |
3573 | ||
3574 | else | |
3575 | Apply_Range_Check (A, A_Typ, F_Typ); | |
3576 | end if; | |
3577 | end if; | |
3578 | end if; | |
3579 | ||
3580 | -- An actual associated with an access parameter is implicitly | |
45fc7ddb HK |
3581 | -- converted to the anonymous access type of the formal and must |
3582 | -- satisfy the legality checks for access conversions. | |
996ae0b0 RK |
3583 | |
3584 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
3585 | if not Valid_Conversion (A, F_Typ, A) then | |
3586 | Error_Msg_N | |
3587 | ("invalid implicit conversion for access parameter", A); | |
3588 | end if; | |
3589 | end if; | |
3590 | ||
3591 | -- Check bad case of atomic/volatile argument (RM C.6(12)) | |
3592 | ||
3593 | if Is_By_Reference_Type (Etype (F)) | |
3594 | and then Comes_From_Source (N) | |
3595 | then | |
3596 | if Is_Atomic_Object (A) | |
3597 | and then not Is_Atomic (Etype (F)) | |
3598 | then | |
3599 | Error_Msg_N | |
3600 | ("cannot pass atomic argument to non-atomic formal", | |
3601 | N); | |
3602 | ||
3603 | elsif Is_Volatile_Object (A) | |
3604 | and then not Is_Volatile (Etype (F)) | |
3605 | then | |
3606 | Error_Msg_N | |
3607 | ("cannot pass volatile argument to non-volatile formal", | |
3608 | N); | |
3609 | end if; | |
3610 | end if; | |
3611 | ||
3612 | -- Check that subprograms don't have improper controlling | |
d81b4bfe | 3613 | -- arguments (RM 3.9.2 (9)). |
996ae0b0 | 3614 | |
0669bebe GB |
3615 | -- A primitive operation may have an access parameter of an |
3616 | -- incomplete tagged type, but a dispatching call is illegal | |
3617 | -- if the type is still incomplete. | |
3618 | ||
996ae0b0 RK |
3619 | if Is_Controlling_Formal (F) then |
3620 | Set_Is_Controlling_Actual (A); | |
0669bebe GB |
3621 | |
3622 | if Ekind (Etype (F)) = E_Anonymous_Access_Type then | |
3623 | declare | |
3624 | Desig : constant Entity_Id := Designated_Type (Etype (F)); | |
3625 | begin | |
3626 | if Ekind (Desig) = E_Incomplete_Type | |
3627 | and then No (Full_View (Desig)) | |
3628 | and then No (Non_Limited_View (Desig)) | |
3629 | then | |
3630 | Error_Msg_NE | |
3631 | ("premature use of incomplete type& " & | |
3632 | "in dispatching call", A, Desig); | |
3633 | end if; | |
3634 | end; | |
3635 | end if; | |
3636 | ||
996ae0b0 RK |
3637 | elsif Nkind (A) = N_Explicit_Dereference then |
3638 | Validate_Remote_Access_To_Class_Wide_Type (A); | |
3639 | end if; | |
3640 | ||
3641 | if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A)) | |
3642 | and then not Is_Class_Wide_Type (F_Typ) | |
3643 | and then not Is_Controlling_Formal (F) | |
3644 | then | |
3645 | Error_Msg_N ("class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3646 | |
3647 | if Is_Subprogram (Nam) | |
3648 | and then Comes_From_Source (Nam) | |
3649 | then | |
996ae0b0 RK |
3650 | Error_Msg_Node_2 := F_Typ; |
3651 | Error_Msg_NE | |
82c80734 | 3652 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3653 | end if; |
3654 | ||
3655 | elsif Is_Access_Type (A_Typ) | |
3656 | and then Is_Access_Type (F_Typ) | |
3657 | and then Ekind (F_Typ) /= E_Access_Subprogram_Type | |
aa5147f0 | 3658 | and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type |
996ae0b0 | 3659 | and then (Is_Class_Wide_Type (Designated_Type (A_Typ)) |
07fc65c4 GB |
3660 | or else (Nkind (A) = N_Attribute_Reference |
3661 | and then | |
3662 | Is_Class_Wide_Type (Etype (Prefix (A))))) | |
996ae0b0 RK |
3663 | and then not Is_Class_Wide_Type (Designated_Type (F_Typ)) |
3664 | and then not Is_Controlling_Formal (F) | |
ae65d635 AC |
3665 | |
3666 | -- Disable these checks in imported C++ subprograms | |
3667 | ||
3668 | and then not (Is_Imported (Entity (Name (N))) | |
3669 | and then Convention (Entity (Name (N))) | |
3670 | = Convention_CPP) | |
996ae0b0 RK |
3671 | then |
3672 | Error_Msg_N | |
3673 | ("access to class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3674 | |
3675 | if Is_Subprogram (Nam) | |
3676 | and then Comes_From_Source (Nam) | |
3677 | then | |
996ae0b0 RK |
3678 | Error_Msg_Node_2 := Designated_Type (F_Typ); |
3679 | Error_Msg_NE | |
82c80734 | 3680 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3681 | end if; |
3682 | end if; | |
3683 | ||
3684 | Eval_Actual (A); | |
3685 | ||
3686 | -- If it is a named association, treat the selector_name as | |
3687 | -- a proper identifier, and mark the corresponding entity. | |
3688 | ||
3689 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3690 | Set_Entity (Selector_Name (Parent (A)), F); | |
3691 | Generate_Reference (F, Selector_Name (Parent (A))); | |
3692 | Set_Etype (Selector_Name (Parent (A)), F_Typ); | |
3693 | Generate_Reference (F_Typ, N, ' '); | |
3694 | end if; | |
3695 | ||
3696 | Prev := A; | |
fbf5a39b AC |
3697 | |
3698 | if Ekind (F) /= E_Out_Parameter then | |
3699 | Check_Unset_Reference (A); | |
3700 | end if; | |
3701 | ||
996ae0b0 RK |
3702 | Next_Actual (A); |
3703 | ||
fbf5a39b AC |
3704 | -- Case where actual is not present |
3705 | ||
996ae0b0 RK |
3706 | else |
3707 | Insert_Default; | |
3708 | end if; | |
3709 | ||
3710 | Next_Formal (F); | |
3711 | end loop; | |
996ae0b0 RK |
3712 | end Resolve_Actuals; |
3713 | ||
3714 | ----------------------- | |
3715 | -- Resolve_Allocator -- | |
3716 | ----------------------- | |
3717 | ||
3718 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is | |
3719 | E : constant Node_Id := Expression (N); | |
3720 | Subtyp : Entity_Id; | |
3721 | Discrim : Entity_Id; | |
3722 | Constr : Node_Id; | |
b7d1f17f HK |
3723 | Aggr : Node_Id; |
3724 | Assoc : Node_Id := Empty; | |
996ae0b0 RK |
3725 | Disc_Exp : Node_Id; |
3726 | ||
b7d1f17f HK |
3727 | procedure Check_Allocator_Discrim_Accessibility |
3728 | (Disc_Exp : Node_Id; | |
3729 | Alloc_Typ : Entity_Id); | |
3730 | -- Check that accessibility level associated with an access discriminant | |
3731 | -- initialized in an allocator by the expression Disc_Exp is not deeper | |
3732 | -- than the level of the allocator type Alloc_Typ. An error message is | |
3733 | -- issued if this condition is violated. Specialized checks are done for | |
3734 | -- the cases of a constraint expression which is an access attribute or | |
3735 | -- an access discriminant. | |
3736 | ||
07fc65c4 | 3737 | function In_Dispatching_Context return Boolean; |
b7d1f17f HK |
3738 | -- If the allocator is an actual in a call, it is allowed to be class- |
3739 | -- wide when the context is not because it is a controlling actual. | |
3740 | ||
3741 | procedure Propagate_Coextensions (Root : Node_Id); | |
3742 | -- Propagate all nested coextensions which are located one nesting | |
3743 | -- level down the tree to the node Root. Example: | |
3744 | -- | |
3745 | -- Top_Record | |
3746 | -- Level_1_Coextension | |
3747 | -- Level_2_Coextension | |
3748 | -- | |
3749 | -- The algorithm is paired with delay actions done by the Expander. In | |
3750 | -- the above example, assume all coextensions are controlled types. | |
3751 | -- The cycle of analysis, resolution and expansion will yield: | |
3752 | -- | |
3753 | -- 1) Analyze Top_Record | |
3754 | -- 2) Analyze Level_1_Coextension | |
3755 | -- 3) Analyze Level_2_Coextension | |
f3d57416 | 3756 | -- 4) Resolve Level_2_Coextension. The allocator is marked as a |
b7d1f17f HK |
3757 | -- coextension. |
3758 | -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is | |
3759 | -- generated to capture the allocated object. Temp_1 is attached | |
3760 | -- to the coextension chain of Level_2_Coextension. | |
3761 | -- 6) Resolve Level_1_Coextension. The allocator is marked as a | |
3762 | -- coextension. A forward tree traversal is performed which finds | |
3763 | -- Level_2_Coextension's list and copies its contents into its | |
3764 | -- own list. | |
3765 | -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is | |
3766 | -- generated to capture the allocated object. Temp_2 is attached | |
3767 | -- to the coextension chain of Level_1_Coextension. Currently, the | |
3768 | -- contents of the list are [Temp_2, Temp_1]. | |
3769 | -- 8) Resolve Top_Record. A forward tree traversal is performed which | |
3770 | -- finds Level_1_Coextension's list and copies its contents into | |
3771 | -- its own list. | |
3772 | -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and | |
3773 | -- Temp_2 and attach them to Top_Record's finalization list. | |
3774 | ||
3775 | ------------------------------------------- | |
3776 | -- Check_Allocator_Discrim_Accessibility -- | |
3777 | ------------------------------------------- | |
3778 | ||
3779 | procedure Check_Allocator_Discrim_Accessibility | |
3780 | (Disc_Exp : Node_Id; | |
3781 | Alloc_Typ : Entity_Id) | |
3782 | is | |
3783 | begin | |
3784 | if Type_Access_Level (Etype (Disc_Exp)) > | |
3785 | Type_Access_Level (Alloc_Typ) | |
3786 | then | |
3787 | Error_Msg_N | |
3788 | ("operand type has deeper level than allocator type", Disc_Exp); | |
3789 | ||
3790 | -- When the expression is an Access attribute the level of the prefix | |
3791 | -- object must not be deeper than that of the allocator's type. | |
3792 | ||
3793 | elsif Nkind (Disc_Exp) = N_Attribute_Reference | |
3794 | and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) | |
3795 | = Attribute_Access | |
3796 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
3797 | > Type_Access_Level (Alloc_Typ) | |
3798 | then | |
3799 | Error_Msg_N | |
3800 | ("prefix of attribute has deeper level than allocator type", | |
3801 | Disc_Exp); | |
3802 | ||
3803 | -- When the expression is an access discriminant the check is against | |
3804 | -- the level of the prefix object. | |
3805 | ||
3806 | elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type | |
3807 | and then Nkind (Disc_Exp) = N_Selected_Component | |
3808 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
3809 | > Type_Access_Level (Alloc_Typ) | |
3810 | then | |
3811 | Error_Msg_N | |
3812 | ("access discriminant has deeper level than allocator type", | |
3813 | Disc_Exp); | |
3814 | ||
3815 | -- All other cases are legal | |
3816 | ||
3817 | else | |
3818 | null; | |
3819 | end if; | |
3820 | end Check_Allocator_Discrim_Accessibility; | |
07fc65c4 GB |
3821 | |
3822 | ---------------------------- | |
3823 | -- In_Dispatching_Context -- | |
3824 | ---------------------------- | |
3825 | ||
3826 | function In_Dispatching_Context return Boolean is | |
3827 | Par : constant Node_Id := Parent (N); | |
07fc65c4 | 3828 | begin |
45fc7ddb | 3829 | return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement) |
07fc65c4 GB |
3830 | and then Is_Entity_Name (Name (Par)) |
3831 | and then Is_Dispatching_Operation (Entity (Name (Par))); | |
3832 | end In_Dispatching_Context; | |
3833 | ||
b7d1f17f HK |
3834 | ---------------------------- |
3835 | -- Propagate_Coextensions -- | |
3836 | ---------------------------- | |
3837 | ||
3838 | procedure Propagate_Coextensions (Root : Node_Id) is | |
3839 | ||
3840 | procedure Copy_List (From : Elist_Id; To : Elist_Id); | |
3841 | -- Copy the contents of list From into list To, preserving the | |
3842 | -- order of elements. | |
3843 | ||
3844 | function Process_Allocator (Nod : Node_Id) return Traverse_Result; | |
3845 | -- Recognize an allocator or a rewritten allocator node and add it | |
f3d57416 | 3846 | -- along with its nested coextensions to the list of Root. |
b7d1f17f HK |
3847 | |
3848 | --------------- | |
3849 | -- Copy_List -- | |
3850 | --------------- | |
3851 | ||
3852 | procedure Copy_List (From : Elist_Id; To : Elist_Id) is | |
3853 | From_Elmt : Elmt_Id; | |
3854 | begin | |
3855 | From_Elmt := First_Elmt (From); | |
3856 | while Present (From_Elmt) loop | |
3857 | Append_Elmt (Node (From_Elmt), To); | |
3858 | Next_Elmt (From_Elmt); | |
3859 | end loop; | |
3860 | end Copy_List; | |
3861 | ||
3862 | ----------------------- | |
3863 | -- Process_Allocator -- | |
3864 | ----------------------- | |
3865 | ||
3866 | function Process_Allocator (Nod : Node_Id) return Traverse_Result is | |
3867 | Orig_Nod : Node_Id := Nod; | |
3868 | ||
3869 | begin | |
3870 | -- This is a possible rewritten subtype indication allocator. Any | |
3871 | -- nested coextensions will appear as discriminant constraints. | |
3872 | ||
3873 | if Nkind (Nod) = N_Identifier | |
3874 | and then Present (Original_Node (Nod)) | |
3875 | and then Nkind (Original_Node (Nod)) = N_Subtype_Indication | |
3876 | then | |
3877 | declare | |
3878 | Discr : Node_Id; | |
3879 | Discr_Elmt : Elmt_Id; | |
3880 | ||
3881 | begin | |
3882 | if Is_Record_Type (Entity (Nod)) then | |
3883 | Discr_Elmt := | |
3884 | First_Elmt (Discriminant_Constraint (Entity (Nod))); | |
3885 | while Present (Discr_Elmt) loop | |
3886 | Discr := Node (Discr_Elmt); | |
3887 | ||
3888 | if Nkind (Discr) = N_Identifier | |
3889 | and then Present (Original_Node (Discr)) | |
3890 | and then Nkind (Original_Node (Discr)) = N_Allocator | |
3891 | and then Present (Coextensions ( | |
3892 | Original_Node (Discr))) | |
3893 | then | |
3894 | if No (Coextensions (Root)) then | |
3895 | Set_Coextensions (Root, New_Elmt_List); | |
3896 | end if; | |
3897 | ||
3898 | Copy_List | |
3899 | (From => Coextensions (Original_Node (Discr)), | |
3900 | To => Coextensions (Root)); | |
3901 | end if; | |
3902 | ||
3903 | Next_Elmt (Discr_Elmt); | |
3904 | end loop; | |
3905 | ||
3906 | -- There is no need to continue the traversal of this | |
3907 | -- subtree since all the information has already been | |
3908 | -- propagated. | |
3909 | ||
3910 | return Skip; | |
3911 | end if; | |
3912 | end; | |
3913 | ||
3914 | -- Case of either a stand alone allocator or a rewritten allocator | |
3915 | -- with an aggregate. | |
3916 | ||
3917 | else | |
3918 | if Present (Original_Node (Nod)) then | |
3919 | Orig_Nod := Original_Node (Nod); | |
3920 | end if; | |
3921 | ||
3922 | if Nkind (Orig_Nod) = N_Allocator then | |
3923 | ||
3924 | -- Propagate the list of nested coextensions to the Root | |
3925 | -- allocator. This is done through list copy since a single | |
3926 | -- allocator may have multiple coextensions. Do not touch | |
3927 | -- coextensions roots. | |
3928 | ||
3929 | if not Is_Coextension_Root (Orig_Nod) | |
3930 | and then Present (Coextensions (Orig_Nod)) | |
3931 | then | |
3932 | if No (Coextensions (Root)) then | |
3933 | Set_Coextensions (Root, New_Elmt_List); | |
3934 | end if; | |
3935 | ||
3936 | Copy_List | |
3937 | (From => Coextensions (Orig_Nod), | |
3938 | To => Coextensions (Root)); | |
3939 | end if; | |
3940 | ||
3941 | -- There is no need to continue the traversal of this | |
3942 | -- subtree since all the information has already been | |
3943 | -- propagated. | |
3944 | ||
3945 | return Skip; | |
3946 | end if; | |
3947 | end if; | |
3948 | ||
3949 | -- Keep on traversing, looking for the next allocator | |
3950 | ||
3951 | return OK; | |
3952 | end Process_Allocator; | |
3953 | ||
3954 | procedure Process_Allocators is | |
3955 | new Traverse_Proc (Process_Allocator); | |
3956 | ||
3957 | -- Start of processing for Propagate_Coextensions | |
3958 | ||
3959 | begin | |
3960 | Process_Allocators (Expression (Root)); | |
3961 | end Propagate_Coextensions; | |
3962 | ||
07fc65c4 GB |
3963 | -- Start of processing for Resolve_Allocator |
3964 | ||
996ae0b0 RK |
3965 | begin |
3966 | -- Replace general access with specific type | |
3967 | ||
3968 | if Ekind (Etype (N)) = E_Allocator_Type then | |
3969 | Set_Etype (N, Base_Type (Typ)); | |
3970 | end if; | |
3971 | ||
0669bebe | 3972 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
3973 | Error_Msg_N ("type of allocator cannot be abstract", N); |
3974 | end if; | |
3975 | ||
3976 | -- For qualified expression, resolve the expression using the | |
3977 | -- given subtype (nothing to do for type mark, subtype indication) | |
3978 | ||
3979 | if Nkind (E) = N_Qualified_Expression then | |
3980 | if Is_Class_Wide_Type (Etype (E)) | |
3981 | and then not Is_Class_Wide_Type (Designated_Type (Typ)) | |
07fc65c4 | 3982 | and then not In_Dispatching_Context |
996ae0b0 RK |
3983 | then |
3984 | Error_Msg_N | |
3985 | ("class-wide allocator not allowed for this access type", N); | |
3986 | end if; | |
3987 | ||
3988 | Resolve (Expression (E), Etype (E)); | |
3989 | Check_Unset_Reference (Expression (E)); | |
3990 | ||
fbf5a39b | 3991 | -- A qualified expression requires an exact match of the type, |
7b4db06c | 3992 | -- class-wide matching is not allowed. |
fbf5a39b | 3993 | |
7b4db06c | 3994 | if (Is_Class_Wide_Type (Etype (Expression (E))) |
b46be8a2 | 3995 | or else Is_Class_Wide_Type (Etype (E))) |
fbf5a39b AC |
3996 | and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E)) |
3997 | then | |
3998 | Wrong_Type (Expression (E), Etype (E)); | |
3999 | end if; | |
4000 | ||
b7d1f17f HK |
4001 | -- A special accessibility check is needed for allocators that |
4002 | -- constrain access discriminants. The level of the type of the | |
4003 | -- expression used to constrain an access discriminant cannot be | |
f3d57416 | 4004 | -- deeper than the type of the allocator (in contrast to access |
b7d1f17f HK |
4005 | -- parameters, where the level of the actual can be arbitrary). |
4006 | ||
4007 | -- We can't use Valid_Conversion to perform this check because | |
4008 | -- in general the type of the allocator is unrelated to the type | |
4009 | -- of the access discriminant. | |
4010 | ||
4011 | if Ekind (Typ) /= E_Anonymous_Access_Type | |
4012 | or else Is_Local_Anonymous_Access (Typ) | |
4013 | then | |
4014 | Subtyp := Entity (Subtype_Mark (E)); | |
4015 | ||
4016 | Aggr := Original_Node (Expression (E)); | |
4017 | ||
4018 | if Has_Discriminants (Subtyp) | |
45fc7ddb | 4019 | and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate) |
b7d1f17f HK |
4020 | then |
4021 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4022 | ||
4023 | -- Get the first component expression of the aggregate | |
4024 | ||
4025 | if Present (Expressions (Aggr)) then | |
4026 | Disc_Exp := First (Expressions (Aggr)); | |
4027 | ||
4028 | elsif Present (Component_Associations (Aggr)) then | |
4029 | Assoc := First (Component_Associations (Aggr)); | |
4030 | ||
4031 | if Present (Assoc) then | |
4032 | Disc_Exp := Expression (Assoc); | |
4033 | else | |
4034 | Disc_Exp := Empty; | |
4035 | end if; | |
4036 | ||
4037 | else | |
4038 | Disc_Exp := Empty; | |
4039 | end if; | |
4040 | ||
4041 | while Present (Discrim) and then Present (Disc_Exp) loop | |
4042 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4043 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); | |
4044 | end if; | |
4045 | ||
4046 | Next_Discriminant (Discrim); | |
4047 | ||
4048 | if Present (Discrim) then | |
4049 | if Present (Assoc) then | |
4050 | Next (Assoc); | |
4051 | Disc_Exp := Expression (Assoc); | |
4052 | ||
4053 | elsif Present (Next (Disc_Exp)) then | |
4054 | Next (Disc_Exp); | |
4055 | ||
4056 | else | |
4057 | Assoc := First (Component_Associations (Aggr)); | |
4058 | ||
4059 | if Present (Assoc) then | |
4060 | Disc_Exp := Expression (Assoc); | |
4061 | else | |
4062 | Disc_Exp := Empty; | |
4063 | end if; | |
4064 | end if; | |
4065 | end if; | |
4066 | end loop; | |
4067 | end if; | |
4068 | end if; | |
4069 | ||
996ae0b0 RK |
4070 | -- For a subtype mark or subtype indication, freeze the subtype |
4071 | ||
4072 | else | |
4073 | Freeze_Expression (E); | |
4074 | ||
4075 | if Is_Access_Constant (Typ) and then not No_Initialization (N) then | |
4076 | Error_Msg_N | |
4077 | ("initialization required for access-to-constant allocator", N); | |
4078 | end if; | |
4079 | ||
4080 | -- A special accessibility check is needed for allocators that | |
4081 | -- constrain access discriminants. The level of the type of the | |
b7d1f17f | 4082 | -- expression used to constrain an access discriminant cannot be |
f3d57416 | 4083 | -- deeper than the type of the allocator (in contrast to access |
996ae0b0 RK |
4084 | -- parameters, where the level of the actual can be arbitrary). |
4085 | -- We can't use Valid_Conversion to perform this check because | |
4086 | -- in general the type of the allocator is unrelated to the type | |
b7d1f17f | 4087 | -- of the access discriminant. |
996ae0b0 RK |
4088 | |
4089 | if Nkind (Original_Node (E)) = N_Subtype_Indication | |
b7d1f17f HK |
4090 | and then (Ekind (Typ) /= E_Anonymous_Access_Type |
4091 | or else Is_Local_Anonymous_Access (Typ)) | |
996ae0b0 RK |
4092 | then |
4093 | Subtyp := Entity (Subtype_Mark (Original_Node (E))); | |
4094 | ||
4095 | if Has_Discriminants (Subtyp) then | |
4096 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4097 | Constr := First (Constraints (Constraint (Original_Node (E)))); | |
996ae0b0 RK |
4098 | while Present (Discrim) and then Present (Constr) loop |
4099 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4100 | if Nkind (Constr) = N_Discriminant_Association then | |
4101 | Disc_Exp := Original_Node (Expression (Constr)); | |
4102 | else | |
4103 | Disc_Exp := Original_Node (Constr); | |
4104 | end if; | |
4105 | ||
b7d1f17f | 4106 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); |
996ae0b0 | 4107 | end if; |
b7d1f17f | 4108 | |
996ae0b0 RK |
4109 | Next_Discriminant (Discrim); |
4110 | Next (Constr); | |
4111 | end loop; | |
4112 | end if; | |
4113 | end if; | |
4114 | end if; | |
4115 | ||
758c442c GD |
4116 | -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility |
4117 | -- check that the level of the type of the created object is not deeper | |
4118 | -- than the level of the allocator's access type, since extensions can | |
4119 | -- now occur at deeper levels than their ancestor types. This is a | |
4120 | -- static accessibility level check; a run-time check is also needed in | |
4121 | -- the case of an initialized allocator with a class-wide argument (see | |
4122 | -- Expand_Allocator_Expression). | |
4123 | ||
4124 | if Ada_Version >= Ada_05 | |
4125 | and then Is_Class_Wide_Type (Designated_Type (Typ)) | |
4126 | then | |
4127 | declare | |
b7d1f17f | 4128 | Exp_Typ : Entity_Id; |
758c442c GD |
4129 | |
4130 | begin | |
4131 | if Nkind (E) = N_Qualified_Expression then | |
4132 | Exp_Typ := Etype (E); | |
4133 | elsif Nkind (E) = N_Subtype_Indication then | |
4134 | Exp_Typ := Entity (Subtype_Mark (Original_Node (E))); | |
4135 | else | |
4136 | Exp_Typ := Entity (E); | |
4137 | end if; | |
4138 | ||
4139 | if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then | |
4140 | if In_Instance_Body then | |
4141 | Error_Msg_N ("?type in allocator has deeper level than" & | |
4142 | " designated class-wide type", E); | |
c8ef728f ES |
4143 | Error_Msg_N ("\?Program_Error will be raised at run time", |
4144 | E); | |
758c442c GD |
4145 | Rewrite (N, |
4146 | Make_Raise_Program_Error (Sloc (N), | |
4147 | Reason => PE_Accessibility_Check_Failed)); | |
4148 | Set_Etype (N, Typ); | |
aa180613 RD |
4149 | |
4150 | -- Do not apply Ada 2005 accessibility checks on a class-wide | |
4151 | -- allocator if the type given in the allocator is a formal | |
4152 | -- type. A run-time check will be performed in the instance. | |
4153 | ||
4154 | elsif not Is_Generic_Type (Exp_Typ) then | |
758c442c GD |
4155 | Error_Msg_N ("type in allocator has deeper level than" & |
4156 | " designated class-wide type", E); | |
4157 | end if; | |
4158 | end if; | |
4159 | end; | |
4160 | end if; | |
4161 | ||
996ae0b0 RK |
4162 | -- Check for allocation from an empty storage pool |
4163 | ||
4164 | if No_Pool_Assigned (Typ) then | |
4165 | declare | |
4166 | Loc : constant Source_Ptr := Sloc (N); | |
996ae0b0 | 4167 | begin |
aa5147f0 ES |
4168 | Error_Msg_N ("?allocation from empty storage pool!", N); |
4169 | Error_Msg_N ("\?Storage_Error will be raised at run time!", N); | |
996ae0b0 | 4170 | Insert_Action (N, |
07fc65c4 GB |
4171 | Make_Raise_Storage_Error (Loc, |
4172 | Reason => SE_Empty_Storage_Pool)); | |
996ae0b0 | 4173 | end; |
1420b484 JM |
4174 | |
4175 | -- If the context is an unchecked conversion, as may happen within | |
4176 | -- an inlined subprogram, the allocator is being resolved with its | |
4177 | -- own anonymous type. In that case, if the target type has a specific | |
4178 | -- storage pool, it must be inherited explicitly by the allocator type. | |
4179 | ||
4180 | elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion | |
4181 | and then No (Associated_Storage_Pool (Typ)) | |
4182 | then | |
4183 | Set_Associated_Storage_Pool | |
4184 | (Typ, Associated_Storage_Pool (Etype (Parent (N)))); | |
996ae0b0 | 4185 | end if; |
b7d1f17f HK |
4186 | |
4187 | -- An erroneous allocator may be rewritten as a raise Program_Error | |
4188 | -- statement. | |
4189 | ||
4190 | if Nkind (N) = N_Allocator then | |
4191 | ||
4192 | -- An anonymous access discriminant is the definition of a | |
aa5147f0 | 4193 | -- coextension. |
b7d1f17f HK |
4194 | |
4195 | if Ekind (Typ) = E_Anonymous_Access_Type | |
4196 | and then Nkind (Associated_Node_For_Itype (Typ)) = | |
4197 | N_Discriminant_Specification | |
4198 | then | |
4199 | -- Avoid marking an allocator as a dynamic coextension if it is | |
aa5147f0 | 4200 | -- within a static construct. |
b7d1f17f HK |
4201 | |
4202 | if not Is_Static_Coextension (N) then | |
aa5147f0 | 4203 | Set_Is_Dynamic_Coextension (N); |
b7d1f17f HK |
4204 | end if; |
4205 | ||
4206 | -- Cleanup for potential static coextensions | |
4207 | ||
4208 | else | |
aa5147f0 ES |
4209 | Set_Is_Dynamic_Coextension (N, False); |
4210 | Set_Is_Static_Coextension (N, False); | |
b7d1f17f HK |
4211 | end if; |
4212 | ||
aa5147f0 ES |
4213 | -- There is no need to propagate any nested coextensions if they |
4214 | -- are marked as static since they will be rewritten on the spot. | |
4215 | ||
4216 | if not Is_Static_Coextension (N) then | |
4217 | Propagate_Coextensions (N); | |
4218 | end if; | |
b7d1f17f | 4219 | end if; |
996ae0b0 RK |
4220 | end Resolve_Allocator; |
4221 | ||
4222 | --------------------------- | |
4223 | -- Resolve_Arithmetic_Op -- | |
4224 | --------------------------- | |
4225 | ||
4226 | -- Used for resolving all arithmetic operators except exponentiation | |
4227 | ||
4228 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
4229 | L : constant Node_Id := Left_Opnd (N); |
4230 | R : constant Node_Id := Right_Opnd (N); | |
4231 | TL : constant Entity_Id := Base_Type (Etype (L)); | |
4232 | TR : constant Entity_Id := Base_Type (Etype (R)); | |
4233 | T : Entity_Id; | |
4234 | Rop : Node_Id; | |
996ae0b0 RK |
4235 | |
4236 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
4237 | -- We do the resolution using the base type, because intermediate values | |
4238 | -- in expressions always are of the base type, not a subtype of it. | |
4239 | ||
aa180613 RD |
4240 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean; |
4241 | -- Returns True if N is in a context that expects "any real type" | |
4242 | ||
996ae0b0 RK |
4243 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean; |
4244 | -- Return True iff given type is Integer or universal real/integer | |
4245 | ||
4246 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id); | |
4247 | -- Choose type of integer literal in fixed-point operation to conform | |
4248 | -- to available fixed-point type. T is the type of the other operand, | |
4249 | -- which is needed to determine the expected type of N. | |
4250 | ||
4251 | procedure Set_Operand_Type (N : Node_Id); | |
4252 | -- Set operand type to T if universal | |
4253 | ||
aa180613 RD |
4254 | ------------------------------- |
4255 | -- Expected_Type_Is_Any_Real -- | |
4256 | ------------------------------- | |
4257 | ||
4258 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is | |
4259 | begin | |
4260 | -- N is the expression after "delta" in a fixed_point_definition; | |
4261 | -- see RM-3.5.9(6): | |
4262 | ||
45fc7ddb HK |
4263 | return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition, |
4264 | N_Decimal_Fixed_Point_Definition, | |
aa180613 RD |
4265 | |
4266 | -- N is one of the bounds in a real_range_specification; | |
4267 | -- see RM-3.5.7(5): | |
4268 | ||
45fc7ddb | 4269 | N_Real_Range_Specification, |
aa180613 RD |
4270 | |
4271 | -- N is the expression of a delta_constraint; | |
4272 | -- see RM-J.3(3): | |
4273 | ||
45fc7ddb | 4274 | N_Delta_Constraint); |
aa180613 RD |
4275 | end Expected_Type_Is_Any_Real; |
4276 | ||
996ae0b0 RK |
4277 | ----------------------------- |
4278 | -- Is_Integer_Or_Universal -- | |
4279 | ----------------------------- | |
4280 | ||
4281 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean is | |
4282 | T : Entity_Id; | |
4283 | Index : Interp_Index; | |
4284 | It : Interp; | |
4285 | ||
4286 | begin | |
4287 | if not Is_Overloaded (N) then | |
4288 | T := Etype (N); | |
4289 | return Base_Type (T) = Base_Type (Standard_Integer) | |
4290 | or else T = Universal_Integer | |
4291 | or else T = Universal_Real; | |
4292 | else | |
4293 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4294 | while Present (It.Typ) loop |
996ae0b0 RK |
4295 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) |
4296 | or else It.Typ = Universal_Integer | |
4297 | or else It.Typ = Universal_Real | |
4298 | then | |
4299 | return True; | |
4300 | end if; | |
4301 | ||
4302 | Get_Next_Interp (Index, It); | |
4303 | end loop; | |
4304 | end if; | |
4305 | ||
4306 | return False; | |
4307 | end Is_Integer_Or_Universal; | |
4308 | ||
4309 | ---------------------------- | |
4310 | -- Set_Mixed_Mode_Operand -- | |
4311 | ---------------------------- | |
4312 | ||
4313 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is | |
4314 | Index : Interp_Index; | |
4315 | It : Interp; | |
4316 | ||
4317 | begin | |
4318 | if Universal_Interpretation (N) = Universal_Integer then | |
4319 | ||
4320 | -- A universal integer literal is resolved as standard integer | |
758c442c GD |
4321 | -- except in the case of a fixed-point result, where we leave it |
4322 | -- as universal (to be handled by Exp_Fixd later on) | |
996ae0b0 RK |
4323 | |
4324 | if Is_Fixed_Point_Type (T) then | |
4325 | Resolve (N, Universal_Integer); | |
4326 | else | |
4327 | Resolve (N, Standard_Integer); | |
4328 | end if; | |
4329 | ||
4330 | elsif Universal_Interpretation (N) = Universal_Real | |
4331 | and then (T = Base_Type (Standard_Integer) | |
4332 | or else T = Universal_Integer | |
4333 | or else T = Universal_Real) | |
4334 | then | |
4335 | -- A universal real can appear in a fixed-type context. We resolve | |
4336 | -- the literal with that context, even though this might raise an | |
4337 | -- exception prematurely (the other operand may be zero). | |
4338 | ||
4339 | Resolve (N, B_Typ); | |
4340 | ||
4341 | elsif Etype (N) = Base_Type (Standard_Integer) | |
4342 | and then T = Universal_Real | |
4343 | and then Is_Overloaded (N) | |
4344 | then | |
4345 | -- Integer arg in mixed-mode operation. Resolve with universal | |
4346 | -- type, in case preference rule must be applied. | |
4347 | ||
4348 | Resolve (N, Universal_Integer); | |
4349 | ||
4350 | elsif Etype (N) = T | |
4351 | and then B_Typ /= Universal_Fixed | |
4352 | then | |
a77842bd | 4353 | -- Not a mixed-mode operation, resolve with context |
996ae0b0 RK |
4354 | |
4355 | Resolve (N, B_Typ); | |
4356 | ||
4357 | elsif Etype (N) = Any_Fixed then | |
4358 | ||
a77842bd | 4359 | -- N may itself be a mixed-mode operation, so use context type |
996ae0b0 RK |
4360 | |
4361 | Resolve (N, B_Typ); | |
4362 | ||
4363 | elsif Is_Fixed_Point_Type (T) | |
4364 | and then B_Typ = Universal_Fixed | |
4365 | and then Is_Overloaded (N) | |
4366 | then | |
4367 | -- Must be (fixed * fixed) operation, operand must have one | |
4368 | -- compatible interpretation. | |
4369 | ||
4370 | Resolve (N, Any_Fixed); | |
4371 | ||
4372 | elsif Is_Fixed_Point_Type (B_Typ) | |
4373 | and then (T = Universal_Real | |
4374 | or else Is_Fixed_Point_Type (T)) | |
4375 | and then Is_Overloaded (N) | |
4376 | then | |
4377 | -- C * F(X) in a fixed context, where C is a real literal or a | |
4378 | -- fixed-point expression. F must have either a fixed type | |
4379 | -- interpretation or an integer interpretation, but not both. | |
4380 | ||
4381 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4382 | while Present (It.Typ) loop |
996ae0b0 RK |
4383 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) then |
4384 | ||
4385 | if Analyzed (N) then | |
4386 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4387 | else | |
4388 | Resolve (N, Standard_Integer); | |
4389 | end if; | |
4390 | ||
4391 | elsif Is_Fixed_Point_Type (It.Typ) then | |
4392 | ||
4393 | if Analyzed (N) then | |
4394 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4395 | else | |
4396 | Resolve (N, It.Typ); | |
4397 | end if; | |
4398 | end if; | |
4399 | ||
4400 | Get_Next_Interp (Index, It); | |
4401 | end loop; | |
4402 | ||
758c442c GD |
4403 | -- Reanalyze the literal with the fixed type of the context. If |
4404 | -- context is Universal_Fixed, we are within a conversion, leave | |
4405 | -- the literal as a universal real because there is no usable | |
4406 | -- fixed type, and the target of the conversion plays no role in | |
4407 | -- the resolution. | |
996ae0b0 | 4408 | |
0ab80019 AC |
4409 | declare |
4410 | Op2 : Node_Id; | |
4411 | T2 : Entity_Id; | |
4412 | ||
4413 | begin | |
4414 | if N = L then | |
4415 | Op2 := R; | |
4416 | else | |
4417 | Op2 := L; | |
4418 | end if; | |
4419 | ||
4420 | if B_Typ = Universal_Fixed | |
4421 | and then Nkind (Op2) = N_Real_Literal | |
4422 | then | |
4423 | T2 := Universal_Real; | |
4424 | else | |
4425 | T2 := B_Typ; | |
4426 | end if; | |
4427 | ||
4428 | Set_Analyzed (Op2, False); | |
4429 | Resolve (Op2, T2); | |
4430 | end; | |
996ae0b0 RK |
4431 | |
4432 | else | |
fbf5a39b | 4433 | Resolve (N); |
996ae0b0 RK |
4434 | end if; |
4435 | end Set_Mixed_Mode_Operand; | |
4436 | ||
4437 | ---------------------- | |
4438 | -- Set_Operand_Type -- | |
4439 | ---------------------- | |
4440 | ||
4441 | procedure Set_Operand_Type (N : Node_Id) is | |
4442 | begin | |
4443 | if Etype (N) = Universal_Integer | |
4444 | or else Etype (N) = Universal_Real | |
4445 | then | |
4446 | Set_Etype (N, T); | |
4447 | end if; | |
4448 | end Set_Operand_Type; | |
4449 | ||
996ae0b0 RK |
4450 | -- Start of processing for Resolve_Arithmetic_Op |
4451 | ||
4452 | begin | |
4453 | if Comes_From_Source (N) | |
4454 | and then Ekind (Entity (N)) = E_Function | |
4455 | and then Is_Imported (Entity (N)) | |
fbf5a39b | 4456 | and then Is_Intrinsic_Subprogram (Entity (N)) |
996ae0b0 RK |
4457 | then |
4458 | Resolve_Intrinsic_Operator (N, Typ); | |
4459 | return; | |
4460 | ||
4461 | -- Special-case for mixed-mode universal expressions or fixed point | |
4462 | -- type operation: each argument is resolved separately. The same | |
4463 | -- treatment is required if one of the operands of a fixed point | |
4464 | -- operation is universal real, since in this case we don't do a | |
4465 | -- conversion to a specific fixed-point type (instead the expander | |
4466 | -- takes care of the case). | |
4467 | ||
45fc7ddb | 4468 | elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real) |
996ae0b0 RK |
4469 | and then Present (Universal_Interpretation (L)) |
4470 | and then Present (Universal_Interpretation (R)) | |
4471 | then | |
4472 | Resolve (L, Universal_Interpretation (L)); | |
4473 | Resolve (R, Universal_Interpretation (R)); | |
4474 | Set_Etype (N, B_Typ); | |
4475 | ||
4476 | elsif (B_Typ = Universal_Real | |
45fc7ddb HK |
4477 | or else Etype (N) = Universal_Fixed |
4478 | or else (Etype (N) = Any_Fixed | |
4479 | and then Is_Fixed_Point_Type (B_Typ)) | |
4480 | or else (Is_Fixed_Point_Type (B_Typ) | |
4481 | and then (Is_Integer_Or_Universal (L) | |
4482 | or else | |
4483 | Is_Integer_Or_Universal (R)))) | |
4484 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) | |
996ae0b0 RK |
4485 | then |
4486 | if TL = Universal_Integer or else TR = Universal_Integer then | |
4487 | Check_For_Visible_Operator (N, B_Typ); | |
4488 | end if; | |
4489 | ||
4490 | -- If context is a fixed type and one operand is integer, the | |
4491 | -- other is resolved with the type of the context. | |
4492 | ||
4493 | if Is_Fixed_Point_Type (B_Typ) | |
4494 | and then (Base_Type (TL) = Base_Type (Standard_Integer) | |
4495 | or else TL = Universal_Integer) | |
4496 | then | |
4497 | Resolve (R, B_Typ); | |
4498 | Resolve (L, TL); | |
4499 | ||
4500 | elsif Is_Fixed_Point_Type (B_Typ) | |
4501 | and then (Base_Type (TR) = Base_Type (Standard_Integer) | |
4502 | or else TR = Universal_Integer) | |
4503 | then | |
4504 | Resolve (L, B_Typ); | |
4505 | Resolve (R, TR); | |
4506 | ||
4507 | else | |
4508 | Set_Mixed_Mode_Operand (L, TR); | |
4509 | Set_Mixed_Mode_Operand (R, TL); | |
4510 | end if; | |
4511 | ||
45fc7ddb HK |
4512 | -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed |
4513 | -- multiplying operators from being used when the expected type is | |
4514 | -- also universal_fixed. Note that B_Typ will be Universal_Fixed in | |
4515 | -- some cases where the expected type is actually Any_Real; | |
4516 | -- Expected_Type_Is_Any_Real takes care of that case. | |
aa180613 | 4517 | |
996ae0b0 RK |
4518 | if Etype (N) = Universal_Fixed |
4519 | or else Etype (N) = Any_Fixed | |
4520 | then | |
4521 | if B_Typ = Universal_Fixed | |
aa180613 | 4522 | and then not Expected_Type_Is_Any_Real (N) |
45fc7ddb HK |
4523 | and then not Nkind_In (Parent (N), N_Type_Conversion, |
4524 | N_Unchecked_Type_Conversion) | |
996ae0b0 | 4525 | then |
45fc7ddb HK |
4526 | Error_Msg_N ("type cannot be determined from context!", N); |
4527 | Error_Msg_N ("\explicit conversion to result type required", N); | |
996ae0b0 RK |
4528 | |
4529 | Set_Etype (L, Any_Type); | |
4530 | Set_Etype (R, Any_Type); | |
4531 | ||
4532 | else | |
0ab80019 | 4533 | if Ada_Version = Ada_83 |
45fc7ddb HK |
4534 | and then Etype (N) = Universal_Fixed |
4535 | and then not | |
4536 | Nkind_In (Parent (N), N_Type_Conversion, | |
4537 | N_Unchecked_Type_Conversion) | |
996ae0b0 RK |
4538 | then |
4539 | Error_Msg_N | |
45fc7ddb HK |
4540 | ("(Ada 83) fixed-point operation " |
4541 | & "needs explicit conversion", N); | |
996ae0b0 RK |
4542 | end if; |
4543 | ||
aa180613 RD |
4544 | -- The expected type is "any real type" in contexts like |
4545 | -- type T is delta <universal_fixed-expression> ... | |
4546 | -- in which case we need to set the type to Universal_Real | |
4547 | -- so that static expression evaluation will work properly. | |
4548 | ||
4549 | if Expected_Type_Is_Any_Real (N) then | |
4550 | Set_Etype (N, Universal_Real); | |
4551 | else | |
4552 | Set_Etype (N, B_Typ); | |
4553 | end if; | |
996ae0b0 RK |
4554 | end if; |
4555 | ||
4556 | elsif Is_Fixed_Point_Type (B_Typ) | |
4557 | and then (Is_Integer_Or_Universal (L) | |
4558 | or else Nkind (L) = N_Real_Literal | |
4559 | or else Nkind (R) = N_Real_Literal | |
45fc7ddb | 4560 | or else Is_Integer_Or_Universal (R)) |
996ae0b0 RK |
4561 | then |
4562 | Set_Etype (N, B_Typ); | |
4563 | ||
4564 | elsif Etype (N) = Any_Fixed then | |
4565 | ||
4566 | -- If no previous errors, this is only possible if one operand | |
4567 | -- is overloaded and the context is universal. Resolve as such. | |
4568 | ||
4569 | Set_Etype (N, B_Typ); | |
4570 | end if; | |
4571 | ||
4572 | else | |
4573 | if (TL = Universal_Integer or else TL = Universal_Real) | |
45fc7ddb HK |
4574 | and then |
4575 | (TR = Universal_Integer or else TR = Universal_Real) | |
996ae0b0 RK |
4576 | then |
4577 | Check_For_Visible_Operator (N, B_Typ); | |
4578 | end if; | |
4579 | ||
4580 | -- If the context is Universal_Fixed and the operands are also | |
4581 | -- universal fixed, this is an error, unless there is only one | |
4582 | -- applicable fixed_point type (usually duration). | |
4583 | ||
45fc7ddb | 4584 | if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then |
996ae0b0 RK |
4585 | T := Unique_Fixed_Point_Type (N); |
4586 | ||
4587 | if T = Any_Type then | |
4588 | Set_Etype (N, T); | |
4589 | return; | |
4590 | else | |
4591 | Resolve (L, T); | |
4592 | Resolve (R, T); | |
4593 | end if; | |
4594 | ||
4595 | else | |
4596 | Resolve (L, B_Typ); | |
4597 | Resolve (R, B_Typ); | |
4598 | end if; | |
4599 | ||
4600 | -- If one of the arguments was resolved to a non-universal type. | |
4601 | -- label the result of the operation itself with the same type. | |
4602 | -- Do the same for the universal argument, if any. | |
4603 | ||
4604 | T := Intersect_Types (L, R); | |
4605 | Set_Etype (N, Base_Type (T)); | |
4606 | Set_Operand_Type (L); | |
4607 | Set_Operand_Type (R); | |
4608 | end if; | |
4609 | ||
fbf5a39b | 4610 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
4611 | Eval_Arithmetic_Op (N); |
4612 | ||
4613 | -- Set overflow and division checking bit. Much cleverer code needed | |
4614 | -- here eventually and perhaps the Resolve routines should be separated | |
4615 | -- for the various arithmetic operations, since they will need | |
4616 | -- different processing. ??? | |
4617 | ||
4618 | if Nkind (N) in N_Op then | |
4619 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 4620 | Enable_Overflow_Check (N); |
996ae0b0 RK |
4621 | end if; |
4622 | ||
fbf5a39b AC |
4623 | -- Give warning if explicit division by zero |
4624 | ||
45fc7ddb | 4625 | if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod) |
996ae0b0 RK |
4626 | and then not Division_Checks_Suppressed (Etype (N)) |
4627 | then | |
fbf5a39b AC |
4628 | Rop := Right_Opnd (N); |
4629 | ||
4630 | if Compile_Time_Known_Value (Rop) | |
4631 | and then ((Is_Integer_Type (Etype (Rop)) | |
45fc7ddb | 4632 | and then Expr_Value (Rop) = Uint_0) |
fbf5a39b AC |
4633 | or else |
4634 | (Is_Real_Type (Etype (Rop)) | |
45fc7ddb | 4635 | and then Expr_Value_R (Rop) = Ureal_0)) |
fbf5a39b | 4636 | then |
aa180613 RD |
4637 | -- Specialize the warning message according to the operation |
4638 | ||
4639 | case Nkind (N) is | |
4640 | when N_Op_Divide => | |
4641 | Apply_Compile_Time_Constraint_Error | |
4642 | (N, "division by zero?", CE_Divide_By_Zero, | |
4643 | Loc => Sloc (Right_Opnd (N))); | |
4644 | ||
4645 | when N_Op_Rem => | |
4646 | Apply_Compile_Time_Constraint_Error | |
4647 | (N, "rem with zero divisor?", CE_Divide_By_Zero, | |
4648 | Loc => Sloc (Right_Opnd (N))); | |
4649 | ||
4650 | when N_Op_Mod => | |
4651 | Apply_Compile_Time_Constraint_Error | |
4652 | (N, "mod with zero divisor?", CE_Divide_By_Zero, | |
4653 | Loc => Sloc (Right_Opnd (N))); | |
4654 | ||
4655 | -- Division by zero can only happen with division, rem, | |
4656 | -- and mod operations. | |
4657 | ||
4658 | when others => | |
4659 | raise Program_Error; | |
4660 | end case; | |
fbf5a39b AC |
4661 | |
4662 | -- Otherwise just set the flag to check at run time | |
4663 | ||
4664 | else | |
b7d1f17f | 4665 | Activate_Division_Check (N); |
fbf5a39b | 4666 | end if; |
996ae0b0 | 4667 | end if; |
45fc7ddb HK |
4668 | |
4669 | -- If Restriction No_Implicit_Conditionals is active, then it is | |
4670 | -- violated if either operand can be negative for mod, or for rem | |
4671 | -- if both operands can be negative. | |
4672 | ||
4673 | if Restrictions.Set (No_Implicit_Conditionals) | |
4674 | and then Nkind_In (N, N_Op_Rem, N_Op_Mod) | |
4675 | then | |
4676 | declare | |
4677 | Lo : Uint; | |
4678 | Hi : Uint; | |
4679 | OK : Boolean; | |
4680 | ||
4681 | LNeg : Boolean; | |
4682 | RNeg : Boolean; | |
4683 | -- Set if corresponding operand might be negative | |
4684 | ||
4685 | begin | |
4686 | Determine_Range (Left_Opnd (N), OK, Lo, Hi); | |
4687 | LNeg := (not OK) or else Lo < 0; | |
4688 | ||
4689 | Determine_Range (Right_Opnd (N), OK, Lo, Hi); | |
4690 | RNeg := (not OK) or else Lo < 0; | |
4691 | ||
4692 | if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg)) | |
4693 | or else | |
4694 | (Nkind (N) = N_Op_Mod and then (LNeg or RNeg)) | |
4695 | then | |
4696 | Check_Restriction (No_Implicit_Conditionals, N); | |
4697 | end if; | |
4698 | end; | |
4699 | end if; | |
996ae0b0 RK |
4700 | end if; |
4701 | ||
4702 | Check_Unset_Reference (L); | |
4703 | Check_Unset_Reference (R); | |
996ae0b0 RK |
4704 | end Resolve_Arithmetic_Op; |
4705 | ||
4706 | ------------------ | |
4707 | -- Resolve_Call -- | |
4708 | ------------------ | |
4709 | ||
4710 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is | |
4711 | Loc : constant Source_Ptr := Sloc (N); | |
4712 | Subp : constant Node_Id := Name (N); | |
4713 | Nam : Entity_Id; | |
4714 | I : Interp_Index; | |
4715 | It : Interp; | |
4716 | Norm_OK : Boolean; | |
4717 | Scop : Entity_Id; | |
aa180613 | 4718 | Rtype : Entity_Id; |
996ae0b0 RK |
4719 | |
4720 | begin | |
758c442c GD |
4721 | -- The context imposes a unique interpretation with type Typ on a |
4722 | -- procedure or function call. Find the entity of the subprogram that | |
4723 | -- yields the expected type, and propagate the corresponding formal | |
4724 | -- constraints on the actuals. The caller has established that an | |
4725 | -- interpretation exists, and emitted an error if not unique. | |
996ae0b0 RK |
4726 | |
4727 | -- First deal with the case of a call to an access-to-subprogram, | |
4728 | -- dereference made explicit in Analyze_Call. | |
4729 | ||
4730 | if Ekind (Etype (Subp)) = E_Subprogram_Type then | |
996ae0b0 RK |
4731 | if not Is_Overloaded (Subp) then |
4732 | Nam := Etype (Subp); | |
4733 | ||
4734 | else | |
758c442c GD |
4735 | -- Find the interpretation whose type (a subprogram type) has a |
4736 | -- return type that is compatible with the context. Analysis of | |
4737 | -- the node has established that one exists. | |
996ae0b0 | 4738 | |
996ae0b0 RK |
4739 | Nam := Empty; |
4740 | ||
1420b484 | 4741 | Get_First_Interp (Subp, I, It); |
996ae0b0 | 4742 | while Present (It.Typ) loop |
996ae0b0 RK |
4743 | if Covers (Typ, Etype (It.Typ)) then |
4744 | Nam := It.Typ; | |
4745 | exit; | |
4746 | end if; | |
4747 | ||
4748 | Get_Next_Interp (I, It); | |
4749 | end loop; | |
4750 | ||
4751 | if No (Nam) then | |
4752 | raise Program_Error; | |
4753 | end if; | |
4754 | end if; | |
4755 | ||
4756 | -- If the prefix is not an entity, then resolve it | |
4757 | ||
4758 | if not Is_Entity_Name (Subp) then | |
4759 | Resolve (Subp, Nam); | |
4760 | end if; | |
4761 | ||
758c442c GD |
4762 | -- For an indirect call, we always invalidate checks, since we do not |
4763 | -- know whether the subprogram is local or global. Yes we could do | |
4764 | -- better here, e.g. by knowing that there are no local subprograms, | |
aa180613 | 4765 | -- but it does not seem worth the effort. Similarly, we kill all |
758c442c | 4766 | -- knowledge of current constant values. |
fbf5a39b AC |
4767 | |
4768 | Kill_Current_Values; | |
4769 | ||
b7d1f17f HK |
4770 | -- If this is a procedure call which is really an entry call, do |
4771 | -- the conversion of the procedure call to an entry call. Protected | |
4772 | -- operations use the same circuitry because the name in the call | |
4773 | -- can be an arbitrary expression with special resolution rules. | |
996ae0b0 | 4774 | |
45fc7ddb | 4775 | elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component) |
996ae0b0 RK |
4776 | or else (Is_Entity_Name (Subp) |
4777 | and then Ekind (Entity (Subp)) = E_Entry) | |
4778 | then | |
4779 | Resolve_Entry_Call (N, Typ); | |
4780 | Check_Elab_Call (N); | |
fbf5a39b AC |
4781 | |
4782 | -- Kill checks and constant values, as above for indirect case | |
4783 | -- Who knows what happens when another task is activated? | |
4784 | ||
4785 | Kill_Current_Values; | |
996ae0b0 RK |
4786 | return; |
4787 | ||
4788 | -- Normal subprogram call with name established in Resolve | |
4789 | ||
4790 | elsif not (Is_Type (Entity (Subp))) then | |
4791 | Nam := Entity (Subp); | |
4792 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
4793 | |
4794 | -- Otherwise we must have the case of an overloaded call | |
4795 | ||
4796 | else | |
4797 | pragma Assert (Is_Overloaded (Subp)); | |
d81b4bfe TQ |
4798 | |
4799 | -- Initialize Nam to prevent warning (we know it will be assigned | |
4800 | -- in the loop below, but the compiler does not know that). | |
4801 | ||
4802 | Nam := Empty; | |
996ae0b0 RK |
4803 | |
4804 | Get_First_Interp (Subp, I, It); | |
996ae0b0 RK |
4805 | while Present (It.Typ) loop |
4806 | if Covers (Typ, It.Typ) then | |
4807 | Nam := It.Nam; | |
4808 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
4809 | exit; |
4810 | end if; | |
4811 | ||
4812 | Get_Next_Interp (I, It); | |
4813 | end loop; | |
4814 | end if; | |
4815 | ||
c9b99571 ES |
4816 | if Is_Access_Subprogram_Type (Base_Type (Etype (Nam))) |
4817 | and then not Is_Access_Subprogram_Type (Base_Type (Typ)) | |
53cf4600 ES |
4818 | and then Nkind (Subp) /= N_Explicit_Dereference |
4819 | and then Present (Parameter_Associations (N)) | |
4820 | then | |
66aa7643 TQ |
4821 | -- The prefix is a parameterless function call that returns an access |
4822 | -- to subprogram. If parameters are present in the current call, add | |
4823 | -- add an explicit dereference. We use the base type here because | |
4824 | -- within an instance these may be subtypes. | |
53cf4600 ES |
4825 | |
4826 | -- The dereference is added either in Analyze_Call or here. Should | |
4827 | -- be consolidated ??? | |
4828 | ||
4829 | Set_Is_Overloaded (Subp, False); | |
4830 | Set_Etype (Subp, Etype (Nam)); | |
4831 | Insert_Explicit_Dereference (Subp); | |
4832 | Nam := Designated_Type (Etype (Nam)); | |
4833 | Resolve (Subp, Nam); | |
4834 | end if; | |
4835 | ||
996ae0b0 RK |
4836 | -- Check that a call to Current_Task does not occur in an entry body |
4837 | ||
4838 | if Is_RTE (Nam, RE_Current_Task) then | |
4839 | declare | |
4840 | P : Node_Id; | |
4841 | ||
4842 | begin | |
4843 | P := N; | |
4844 | loop | |
4845 | P := Parent (P); | |
45fc7ddb HK |
4846 | |
4847 | -- Exclude calls that occur within the default of a formal | |
4848 | -- parameter of the entry, since those are evaluated outside | |
4849 | -- of the body. | |
4850 | ||
4851 | exit when No (P) or else Nkind (P) = N_Parameter_Specification; | |
996ae0b0 | 4852 | |
aa180613 RD |
4853 | if Nkind (P) = N_Entry_Body |
4854 | or else (Nkind (P) = N_Subprogram_Body | |
45fc7ddb | 4855 | and then Is_Entry_Barrier_Function (P)) |
aa180613 RD |
4856 | then |
4857 | Rtype := Etype (N); | |
996ae0b0 | 4858 | Error_Msg_NE |
aa5147f0 | 4859 | ("?& should not be used in entry body (RM C.7(17))", |
996ae0b0 | 4860 | N, Nam); |
aa180613 RD |
4861 | Error_Msg_NE |
4862 | ("\Program_Error will be raised at run time?", N, Nam); | |
4863 | Rewrite (N, | |
4864 | Make_Raise_Program_Error (Loc, | |
4865 | Reason => PE_Current_Task_In_Entry_Body)); | |
4866 | Set_Etype (N, Rtype); | |
e65f50ec | 4867 | return; |
996ae0b0 RK |
4868 | end if; |
4869 | end loop; | |
4870 | end; | |
4871 | end if; | |
4872 | ||
758c442c GD |
4873 | -- Check that a procedure call does not occur in the context of the |
4874 | -- entry call statement of a conditional or timed entry call. Note that | |
4875 | -- the case of a call to a subprogram renaming of an entry will also be | |
4876 | -- rejected. The test for N not being an N_Entry_Call_Statement is | |
4877 | -- defensive, covering the possibility that the processing of entry | |
4878 | -- calls might reach this point due to later modifications of the code | |
4879 | -- above. | |
996ae0b0 RK |
4880 | |
4881 | if Nkind (Parent (N)) = N_Entry_Call_Alternative | |
4882 | and then Nkind (N) /= N_Entry_Call_Statement | |
4883 | and then Entry_Call_Statement (Parent (N)) = N | |
4884 | then | |
1420b484 JM |
4885 | if Ada_Version < Ada_05 then |
4886 | Error_Msg_N ("entry call required in select statement", N); | |
4887 | ||
4888 | -- Ada 2005 (AI-345): If a procedure_call_statement is used | |
66aa7643 TQ |
4889 | -- for a procedure_or_entry_call, the procedure_name or |
4890 | -- procedure_prefix of the procedure_call_statement shall denote | |
1420b484 JM |
4891 | -- an entry renamed by a procedure, or (a view of) a primitive |
4892 | -- subprogram of a limited interface whose first parameter is | |
4893 | -- a controlling parameter. | |
4894 | ||
4895 | elsif Nkind (N) = N_Procedure_Call_Statement | |
4896 | and then not Is_Renamed_Entry (Nam) | |
4897 | and then not Is_Controlling_Limited_Procedure (Nam) | |
4898 | then | |
4899 | Error_Msg_N | |
c8ef728f | 4900 | ("entry call or dispatching primitive of interface required", N); |
1420b484 | 4901 | end if; |
996ae0b0 RK |
4902 | end if; |
4903 | ||
66aa7643 TQ |
4904 | -- Check that this is not a call to a protected procedure or entry from |
4905 | -- within a protected function. | |
fbf5a39b AC |
4906 | |
4907 | if Ekind (Current_Scope) = E_Function | |
4908 | and then Ekind (Scope (Current_Scope)) = E_Protected_Type | |
4909 | and then Ekind (Nam) /= E_Function | |
4910 | and then Scope (Nam) = Scope (Current_Scope) | |
4911 | then | |
4912 | Error_Msg_N ("within protected function, protected " & | |
4913 | "object is constant", N); | |
4914 | Error_Msg_N ("\cannot call operation that may modify it", N); | |
4915 | end if; | |
4916 | ||
45fc7ddb | 4917 | -- Freeze the subprogram name if not in a spec-expression. Note that we |
758c442c GD |
4918 | -- freeze procedure calls as well as function calls. Procedure calls are |
4919 | -- not frozen according to the rules (RM 13.14(14)) because it is | |
4920 | -- impossible to have a procedure call to a non-frozen procedure in pure | |
4921 | -- Ada, but in the code that we generate in the expander, this rule | |
4922 | -- needs extending because we can generate procedure calls that need | |
4923 | -- freezing. | |
996ae0b0 | 4924 | |
45fc7ddb | 4925 | if Is_Entity_Name (Subp) and then not In_Spec_Expression then |
996ae0b0 RK |
4926 | Freeze_Expression (Subp); |
4927 | end if; | |
4928 | ||
758c442c GD |
4929 | -- For a predefined operator, the type of the result is the type imposed |
4930 | -- by context, except for a predefined operation on universal fixed. | |
4931 | -- Otherwise The type of the call is the type returned by the subprogram | |
4932 | -- being called. | |
996ae0b0 RK |
4933 | |
4934 | if Is_Predefined_Op (Nam) then | |
996ae0b0 RK |
4935 | if Etype (N) /= Universal_Fixed then |
4936 | Set_Etype (N, Typ); | |
4937 | end if; | |
4938 | ||
758c442c GD |
4939 | -- If the subprogram returns an array type, and the context requires the |
4940 | -- component type of that array type, the node is really an indexing of | |
4941 | -- the parameterless call. Resolve as such. A pathological case occurs | |
4942 | -- when the type of the component is an access to the array type. In | |
4943 | -- this case the call is truly ambiguous. | |
996ae0b0 | 4944 | |
0669bebe | 4945 | elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam)) |
996ae0b0 RK |
4946 | and then |
4947 | ((Is_Array_Type (Etype (Nam)) | |
4948 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
4949 | or else (Is_Access_Type (Etype (Nam)) | |
4950 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
4951 | and then | |
4952 | Covers (Typ, | |
4953 | Component_Type (Designated_Type (Etype (Nam)))))) | |
4954 | then | |
4955 | declare | |
4956 | Index_Node : Node_Id; | |
fbf5a39b AC |
4957 | New_Subp : Node_Id; |
4958 | Ret_Type : constant Entity_Id := Etype (Nam); | |
996ae0b0 RK |
4959 | |
4960 | begin | |
fbf5a39b AC |
4961 | if Is_Access_Type (Ret_Type) |
4962 | and then Ret_Type = Component_Type (Designated_Type (Ret_Type)) | |
4963 | then | |
4964 | Error_Msg_N | |
4965 | ("cannot disambiguate function call and indexing", N); | |
4966 | else | |
4967 | New_Subp := Relocate_Node (Subp); | |
4968 | Set_Entity (Subp, Nam); | |
4969 | ||
4970 | if Component_Type (Ret_Type) /= Any_Type then | |
0669bebe GB |
4971 | if Needs_No_Actuals (Nam) then |
4972 | ||
4973 | -- Indexed call to a parameterless function | |
4974 | ||
4975 | Index_Node := | |
4976 | Make_Indexed_Component (Loc, | |
4977 | Prefix => | |
4978 | Make_Function_Call (Loc, | |
4979 | Name => New_Subp), | |
4980 | Expressions => Parameter_Associations (N)); | |
4981 | else | |
4982 | -- An Ada 2005 prefixed call to a primitive operation | |
4983 | -- whose first parameter is the prefix. This prefix was | |
4984 | -- prepended to the parameter list, which is actually a | |
4985 | -- list of indices. Remove the prefix in order to build | |
4986 | -- the proper indexed component. | |
4987 | ||
4988 | Index_Node := | |
4989 | Make_Indexed_Component (Loc, | |
4990 | Prefix => | |
4991 | Make_Function_Call (Loc, | |
4992 | Name => New_Subp, | |
4993 | Parameter_Associations => | |
4994 | New_List | |
4995 | (Remove_Head (Parameter_Associations (N)))), | |
4996 | Expressions => Parameter_Associations (N)); | |
4997 | end if; | |
fbf5a39b AC |
4998 | |
4999 | -- Since we are correcting a node classification error made | |
5000 | -- by the parser, we call Replace rather than Rewrite. | |
5001 | ||
5002 | Replace (N, Index_Node); | |
5003 | Set_Etype (Prefix (N), Ret_Type); | |
5004 | Set_Etype (N, Typ); | |
5005 | Resolve_Indexed_Component (N, Typ); | |
5006 | Check_Elab_Call (Prefix (N)); | |
5007 | end if; | |
996ae0b0 RK |
5008 | end if; |
5009 | ||
5010 | return; | |
5011 | end; | |
5012 | ||
5013 | else | |
5014 | Set_Etype (N, Etype (Nam)); | |
5015 | end if; | |
5016 | ||
5017 | -- In the case where the call is to an overloaded subprogram, Analyze | |
5018 | -- calls Normalize_Actuals once per overloaded subprogram. Therefore in | |
5019 | -- such a case Normalize_Actuals needs to be called once more to order | |
5020 | -- the actuals correctly. Otherwise the call will have the ordering | |
5021 | -- given by the last overloaded subprogram whether this is the correct | |
5022 | -- one being called or not. | |
5023 | ||
5024 | if Is_Overloaded (Subp) then | |
5025 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
5026 | pragma Assert (Norm_OK); | |
5027 | end if; | |
5028 | ||
5029 | -- In any case, call is fully resolved now. Reset Overload flag, to | |
5030 | -- prevent subsequent overload resolution if node is analyzed again | |
5031 | ||
5032 | Set_Is_Overloaded (Subp, False); | |
5033 | Set_Is_Overloaded (N, False); | |
5034 | ||
758c442c GD |
5035 | -- If we are calling the current subprogram from immediately within its |
5036 | -- body, then that is the case where we can sometimes detect cases of | |
5037 | -- infinite recursion statically. Do not try this in case restriction | |
b7d1f17f | 5038 | -- No_Recursion is in effect anyway, and do it only for source calls. |
996ae0b0 | 5039 | |
b7d1f17f HK |
5040 | if Comes_From_Source (N) then |
5041 | Scop := Current_Scope; | |
996ae0b0 | 5042 | |
26570b21 RD |
5043 | -- Issue warning for possible infinite recursion in the absence |
5044 | -- of the No_Recursion restriction. | |
5045 | ||
b7d1f17f HK |
5046 | if Nam = Scop |
5047 | and then not Restriction_Active (No_Recursion) | |
5048 | and then Check_Infinite_Recursion (N) | |
5049 | then | |
5050 | -- Here we detected and flagged an infinite recursion, so we do | |
26570b21 RD |
5051 | -- not need to test the case below for further warnings. Also if |
5052 | -- we now have a raise SE node, we are all done. | |
996ae0b0 | 5053 | |
26570b21 RD |
5054 | if Nkind (N) = N_Raise_Storage_Error then |
5055 | return; | |
5056 | end if; | |
996ae0b0 | 5057 | |
26570b21 RD |
5058 | -- If call is to immediately containing subprogram, then check for |
5059 | -- the case of a possible run-time detectable infinite recursion. | |
996ae0b0 | 5060 | |
b7d1f17f HK |
5061 | else |
5062 | Scope_Loop : while Scop /= Standard_Standard loop | |
5063 | if Nam = Scop then | |
5064 | ||
5065 | -- Although in general case, recursion is not statically | |
5066 | -- checkable, the case of calling an immediately containing | |
5067 | -- subprogram is easy to catch. | |
5068 | ||
5069 | Check_Restriction (No_Recursion, N); | |
5070 | ||
5071 | -- If the recursive call is to a parameterless subprogram, | |
5072 | -- then even if we can't statically detect infinite | |
5073 | -- recursion, this is pretty suspicious, and we output a | |
5074 | -- warning. Furthermore, we will try later to detect some | |
5075 | -- cases here at run time by expanding checking code (see | |
5076 | -- Detect_Infinite_Recursion in package Exp_Ch6). | |
5077 | ||
5078 | -- If the recursive call is within a handler, do not emit a | |
5079 | -- warning, because this is a common idiom: loop until input | |
5080 | -- is correct, catch illegal input in handler and restart. | |
5081 | ||
5082 | if No (First_Formal (Nam)) | |
5083 | and then Etype (Nam) = Standard_Void_Type | |
5084 | and then not Error_Posted (N) | |
5085 | and then Nkind (Parent (N)) /= N_Exception_Handler | |
aa180613 | 5086 | then |
b7d1f17f HK |
5087 | -- For the case of a procedure call. We give the message |
5088 | -- only if the call is the first statement in a sequence | |
5089 | -- of statements, or if all previous statements are | |
5090 | -- simple assignments. This is simply a heuristic to | |
5091 | -- decrease false positives, without losing too many good | |
5092 | -- warnings. The idea is that these previous statements | |
5093 | -- may affect global variables the procedure depends on. | |
5094 | ||
5095 | if Nkind (N) = N_Procedure_Call_Statement | |
5096 | and then Is_List_Member (N) | |
5097 | then | |
5098 | declare | |
5099 | P : Node_Id; | |
5100 | begin | |
5101 | P := Prev (N); | |
5102 | while Present (P) loop | |
5103 | if Nkind (P) /= N_Assignment_Statement then | |
5104 | exit Scope_Loop; | |
5105 | end if; | |
5106 | ||
5107 | Prev (P); | |
5108 | end loop; | |
5109 | end; | |
5110 | end if; | |
5111 | ||
5112 | -- Do not give warning if we are in a conditional context | |
5113 | ||
aa180613 | 5114 | declare |
b7d1f17f | 5115 | K : constant Node_Kind := Nkind (Parent (N)); |
aa180613 | 5116 | begin |
b7d1f17f HK |
5117 | if (K = N_Loop_Statement |
5118 | and then Present (Iteration_Scheme (Parent (N)))) | |
5119 | or else K = N_If_Statement | |
5120 | or else K = N_Elsif_Part | |
5121 | or else K = N_Case_Statement_Alternative | |
5122 | then | |
5123 | exit Scope_Loop; | |
5124 | end if; | |
aa180613 | 5125 | end; |
aa180613 | 5126 | |
b7d1f17f | 5127 | -- Here warning is to be issued |
aa180613 | 5128 | |
b7d1f17f HK |
5129 | Set_Has_Recursive_Call (Nam); |
5130 | Error_Msg_N | |
aa5147f0 | 5131 | ("?possible infinite recursion!", N); |
b7d1f17f | 5132 | Error_Msg_N |
aa5147f0 | 5133 | ("\?Storage_Error may be raised at run time!", N); |
b7d1f17f | 5134 | end if; |
aa180613 | 5135 | |
b7d1f17f | 5136 | exit Scope_Loop; |
996ae0b0 RK |
5137 | end if; |
5138 | ||
b7d1f17f HK |
5139 | Scop := Scope (Scop); |
5140 | end loop Scope_Loop; | |
5141 | end if; | |
996ae0b0 RK |
5142 | end if; |
5143 | ||
5144 | -- If subprogram name is a predefined operator, it was given in | |
5145 | -- functional notation. Replace call node with operator node, so | |
5146 | -- that actuals can be resolved appropriately. | |
5147 | ||
5148 | if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then | |
5149 | Make_Call_Into_Operator (N, Typ, Entity (Name (N))); | |
5150 | return; | |
5151 | ||
5152 | elsif Present (Alias (Nam)) | |
5153 | and then Is_Predefined_Op (Alias (Nam)) | |
5154 | then | |
5155 | Resolve_Actuals (N, Nam); | |
5156 | Make_Call_Into_Operator (N, Typ, Alias (Nam)); | |
5157 | return; | |
5158 | end if; | |
5159 | ||
fbf5a39b AC |
5160 | -- Create a transient scope if the resulting type requires it |
5161 | ||
4017021b AC |
5162 | -- There are several notable exceptions: |
5163 | ||
4d2907fd | 5164 | -- a) In init procs, the transient scope overhead is not needed, and is |
4017021b AC |
5165 | -- even incorrect when the call is a nested initialization call for a |
5166 | -- component whose expansion may generate adjust calls. However, if the | |
5167 | -- call is some other procedure call within an initialization procedure | |
5168 | -- (for example a call to Create_Task in the init_proc of the task | |
5169 | -- run-time record) a transient scope must be created around this call. | |
5170 | ||
4d2907fd | 5171 | -- b) Enumeration literal pseudo-calls need no transient scope |
4017021b | 5172 | |
4d2907fd | 5173 | -- c) Intrinsic subprograms (Unchecked_Conversion and source info |
4017021b | 5174 | -- functions) do not use the secondary stack even though the return |
4d2907fd | 5175 | -- type may be unconstrained. |
4017021b | 5176 | |
4d2907fd | 5177 | -- d) Calls to a build-in-place function, since such functions may |
4017021b AC |
5178 | -- allocate their result directly in a target object, and cases where |
5179 | -- the result does get allocated in the secondary stack are checked for | |
5180 | -- within the specialized Exp_Ch6 procedures for expanding those | |
5181 | -- build-in-place calls. | |
5182 | ||
5183 | -- e) If the subprogram is marked Inline_Always, then even if it returns | |
c8ef728f | 5184 | -- an unconstrained type the call does not require use of the secondary |
45fc7ddb HK |
5185 | -- stack. However, inlining will only take place if the body to inline |
5186 | -- is already present. It may not be available if e.g. the subprogram is | |
5187 | -- declared in a child instance. | |
c8ef728f | 5188 | |
4017021b AC |
5189 | -- If this is an initialization call for a type whose construction |
5190 | -- uses the secondary stack, and it is not a nested call to initialize | |
5191 | -- a component, we do need to create a transient scope for it. We | |
5192 | -- check for this by traversing the type in Check_Initialization_Call. | |
5193 | ||
c8ef728f | 5194 | if Is_Inlined (Nam) |
45fc7ddb HK |
5195 | and then Has_Pragma_Inline_Always (Nam) |
5196 | and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration | |
5197 | and then Present (Body_To_Inline (Unit_Declaration_Node (Nam))) | |
c8ef728f ES |
5198 | then |
5199 | null; | |
5200 | ||
4017021b AC |
5201 | elsif Ekind (Nam) = E_Enumeration_Literal |
5202 | or else Is_Build_In_Place_Function (Nam) | |
5203 | or else Is_Intrinsic_Subprogram (Nam) | |
5204 | then | |
5205 | null; | |
5206 | ||
c8ef728f | 5207 | elsif Expander_Active |
996ae0b0 RK |
5208 | and then Is_Type (Etype (Nam)) |
5209 | and then Requires_Transient_Scope (Etype (Nam)) | |
4017021b AC |
5210 | and then |
5211 | (not Within_Init_Proc | |
5212 | or else | |
5213 | (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function)) | |
996ae0b0 | 5214 | then |
0669bebe | 5215 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 5216 | |
a9f4e3d2 AC |
5217 | -- If the call appears within the bounds of a loop, it will |
5218 | -- be rewritten and reanalyzed, nothing left to do here. | |
5219 | ||
5220 | if Nkind (N) /= N_Function_Call then | |
5221 | return; | |
5222 | end if; | |
5223 | ||
fbf5a39b | 5224 | elsif Is_Init_Proc (Nam) |
996ae0b0 RK |
5225 | and then not Within_Init_Proc |
5226 | then | |
5227 | Check_Initialization_Call (N, Nam); | |
5228 | end if; | |
5229 | ||
5230 | -- A protected function cannot be called within the definition of the | |
5231 | -- enclosing protected type. | |
5232 | ||
5233 | if Is_Protected_Type (Scope (Nam)) | |
5234 | and then In_Open_Scopes (Scope (Nam)) | |
5235 | and then not Has_Completion (Scope (Nam)) | |
5236 | then | |
5237 | Error_Msg_NE | |
5238 | ("& cannot be called before end of protected definition", N, Nam); | |
5239 | end if; | |
5240 | ||
5241 | -- Propagate interpretation to actuals, and add default expressions | |
5242 | -- where needed. | |
5243 | ||
5244 | if Present (First_Formal (Nam)) then | |
5245 | Resolve_Actuals (N, Nam); | |
5246 | ||
d81b4bfe TQ |
5247 | -- Overloaded literals are rewritten as function calls, for purpose of |
5248 | -- resolution. After resolution, we can replace the call with the | |
5249 | -- literal itself. | |
996ae0b0 RK |
5250 | |
5251 | elsif Ekind (Nam) = E_Enumeration_Literal then | |
5252 | Copy_Node (Subp, N); | |
5253 | Resolve_Entity_Name (N, Typ); | |
5254 | ||
fbf5a39b | 5255 | -- Avoid validation, since it is a static function call |
996ae0b0 | 5256 | |
e65f50ec | 5257 | Generate_Reference (Nam, Subp); |
996ae0b0 RK |
5258 | return; |
5259 | end if; | |
5260 | ||
b7d1f17f HK |
5261 | -- If the subprogram is not global, then kill all saved values and |
5262 | -- checks. This is a bit conservative, since in many cases we could do | |
5263 | -- better, but it is not worth the effort. Similarly, we kill constant | |
5264 | -- values. However we do not need to do this for internal entities | |
5265 | -- (unless they are inherited user-defined subprograms), since they | |
5266 | -- are not in the business of molesting local values. | |
5267 | ||
5268 | -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also | |
5269 | -- kill all checks and values for calls to global subprograms. This | |
5270 | -- takes care of the case where an access to a local subprogram is | |
5271 | -- taken, and could be passed directly or indirectly and then called | |
5272 | -- from almost any context. | |
aa180613 RD |
5273 | |
5274 | -- Note: we do not do this step till after resolving the actuals. That | |
5275 | -- way we still take advantage of the current value information while | |
5276 | -- scanning the actuals. | |
5277 | ||
45fc7ddb HK |
5278 | -- We suppress killing values if we are processing the nodes associated |
5279 | -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged | |
5280 | -- type kills all the values as part of analyzing the code that | |
5281 | -- initializes the dispatch tables. | |
5282 | ||
5283 | if Inside_Freezing_Actions = 0 | |
5284 | and then (not Is_Library_Level_Entity (Nam) | |
24357840 RD |
5285 | or else Suppress_Value_Tracking_On_Call |
5286 | (Nearest_Dynamic_Scope (Current_Scope))) | |
aa180613 RD |
5287 | and then (Comes_From_Source (Nam) |
5288 | or else (Present (Alias (Nam)) | |
5289 | and then Comes_From_Source (Alias (Nam)))) | |
5290 | then | |
5291 | Kill_Current_Values; | |
5292 | end if; | |
5293 | ||
36fcf362 RD |
5294 | -- If we are warning about unread OUT parameters, this is the place to |
5295 | -- set Last_Assignment for OUT and IN OUT parameters. We have to do this | |
5296 | -- after the above call to Kill_Current_Values (since that call clears | |
5297 | -- the Last_Assignment field of all local variables). | |
67ce0d7e | 5298 | |
36fcf362 | 5299 | if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters) |
67ce0d7e RD |
5300 | and then Comes_From_Source (N) |
5301 | and then In_Extended_Main_Source_Unit (N) | |
5302 | then | |
5303 | declare | |
5304 | F : Entity_Id; | |
5305 | A : Node_Id; | |
5306 | ||
5307 | begin | |
5308 | F := First_Formal (Nam); | |
5309 | A := First_Actual (N); | |
5310 | while Present (F) and then Present (A) loop | |
36fcf362 | 5311 | if (Ekind (F) = E_Out_Parameter |
d81b4bfe TQ |
5312 | or else |
5313 | Ekind (F) = E_In_Out_Parameter) | |
36fcf362 | 5314 | and then Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
5315 | and then Is_Entity_Name (A) |
5316 | and then Present (Entity (A)) | |
36fcf362 | 5317 | and then Comes_From_Source (N) |
67ce0d7e RD |
5318 | and then Safe_To_Capture_Value (N, Entity (A)) |
5319 | then | |
5320 | Set_Last_Assignment (Entity (A), A); | |
5321 | end if; | |
5322 | ||
5323 | Next_Formal (F); | |
5324 | Next_Actual (A); | |
5325 | end loop; | |
5326 | end; | |
5327 | end if; | |
5328 | ||
996ae0b0 RK |
5329 | -- If the subprogram is a primitive operation, check whether or not |
5330 | -- it is a correct dispatching call. | |
5331 | ||
5332 | if Is_Overloadable (Nam) | |
5333 | and then Is_Dispatching_Operation (Nam) | |
5334 | then | |
5335 | Check_Dispatching_Call (N); | |
5336 | ||
0669bebe GB |
5337 | elsif Ekind (Nam) /= E_Subprogram_Type |
5338 | and then Is_Abstract_Subprogram (Nam) | |
996ae0b0 RK |
5339 | and then not In_Instance |
5340 | then | |
5341 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam); | |
5342 | end if; | |
5343 | ||
e65f50ec ES |
5344 | -- If this is a dispatching call, generate the appropriate reference, |
5345 | -- for better source navigation in GPS. | |
5346 | ||
5347 | if Is_Overloadable (Nam) | |
5348 | and then Present (Controlling_Argument (N)) | |
5349 | then | |
5350 | Generate_Reference (Nam, Subp, 'R'); | |
c5d91669 AC |
5351 | |
5352 | -- Normal case, not a dispatching call | |
5353 | ||
e65f50ec ES |
5354 | else |
5355 | Generate_Reference (Nam, Subp); | |
5356 | end if; | |
5357 | ||
996ae0b0 RK |
5358 | if Is_Intrinsic_Subprogram (Nam) then |
5359 | Check_Intrinsic_Call (N); | |
5360 | end if; | |
5361 | ||
5b2217f8 | 5362 | -- Check for violation of restriction No_Specific_Termination_Handlers |
dce86910 | 5363 | -- and warn on a potentially blocking call to Abort_Task. |
5b2217f8 RD |
5364 | |
5365 | if Is_RTE (Nam, RE_Set_Specific_Handler) | |
5366 | or else | |
5367 | Is_RTE (Nam, RE_Specific_Handler) | |
5368 | then | |
5369 | Check_Restriction (No_Specific_Termination_Handlers, N); | |
dce86910 AC |
5370 | |
5371 | elsif Is_RTE (Nam, RE_Abort_Task) then | |
5372 | Check_Potentially_Blocking_Operation (N); | |
5b2217f8 RD |
5373 | end if; |
5374 | ||
16212e89 GD |
5375 | -- Issue an error for a call to an eliminated subprogram |
5376 | ||
5377 | Check_For_Eliminated_Subprogram (Subp, Nam); | |
5378 | ||
67ce0d7e RD |
5379 | -- All done, evaluate call and deal with elaboration issues |
5380 | ||
c01a9391 | 5381 | Eval_Call (N); |
996ae0b0 | 5382 | Check_Elab_Call (N); |
996ae0b0 RK |
5383 | end Resolve_Call; |
5384 | ||
5385 | ------------------------------- | |
5386 | -- Resolve_Character_Literal -- | |
5387 | ------------------------------- | |
5388 | ||
5389 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is | |
5390 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
5391 | C : Entity_Id; | |
5392 | ||
5393 | begin | |
5394 | -- Verify that the character does belong to the type of the context | |
5395 | ||
5396 | Set_Etype (N, B_Typ); | |
5397 | Eval_Character_Literal (N); | |
5398 | ||
82c80734 RD |
5399 | -- Wide_Wide_Character literals must always be defined, since the set |
5400 | -- of wide wide character literals is complete, i.e. if a character | |
5401 | -- literal is accepted by the parser, then it is OK for wide wide | |
5402 | -- character (out of range character literals are rejected). | |
996ae0b0 | 5403 | |
82c80734 | 5404 | if Root_Type (B_Typ) = Standard_Wide_Wide_Character then |
996ae0b0 RK |
5405 | return; |
5406 | ||
5407 | -- Always accept character literal for type Any_Character, which | |
5408 | -- occurs in error situations and in comparisons of literals, both | |
5409 | -- of which should accept all literals. | |
5410 | ||
5411 | elsif B_Typ = Any_Character then | |
5412 | return; | |
5413 | ||
5414 | -- For Standard.Character or a type derived from it, check that | |
5415 | -- the literal is in range | |
5416 | ||
5417 | elsif Root_Type (B_Typ) = Standard_Character then | |
82c80734 RD |
5418 | if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then |
5419 | return; | |
5420 | end if; | |
5421 | ||
5422 | -- For Standard.Wide_Character or a type derived from it, check | |
5423 | -- that the literal is in range | |
5424 | ||
5425 | elsif Root_Type (B_Typ) = Standard_Wide_Character then | |
5426 | if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then | |
996ae0b0 RK |
5427 | return; |
5428 | end if; | |
5429 | ||
82c80734 RD |
5430 | -- For Standard.Wide_Wide_Character or a type derived from it, we |
5431 | -- know the literal is in range, since the parser checked! | |
5432 | ||
5433 | elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then | |
5434 | return; | |
5435 | ||
d81b4bfe TQ |
5436 | -- If the entity is already set, this has already been resolved in a |
5437 | -- generic context, or comes from expansion. Nothing else to do. | |
996ae0b0 RK |
5438 | |
5439 | elsif Present (Entity (N)) then | |
5440 | return; | |
5441 | ||
d81b4bfe TQ |
5442 | -- Otherwise we have a user defined character type, and we can use the |
5443 | -- standard visibility mechanisms to locate the referenced entity. | |
996ae0b0 RK |
5444 | |
5445 | else | |
5446 | C := Current_Entity (N); | |
996ae0b0 RK |
5447 | while Present (C) loop |
5448 | if Etype (C) = B_Typ then | |
5449 | Set_Entity_With_Style_Check (N, C); | |
5450 | Generate_Reference (C, N); | |
5451 | return; | |
5452 | end if; | |
5453 | ||
5454 | C := Homonym (C); | |
5455 | end loop; | |
5456 | end if; | |
5457 | ||
5458 | -- If we fall through, then the literal does not match any of the | |
5459 | -- entries of the enumeration type. This isn't just a constraint | |
5460 | -- error situation, it is an illegality (see RM 4.2). | |
5461 | ||
5462 | Error_Msg_NE | |
5463 | ("character not defined for }", N, First_Subtype (B_Typ)); | |
996ae0b0 RK |
5464 | end Resolve_Character_Literal; |
5465 | ||
5466 | --------------------------- | |
5467 | -- Resolve_Comparison_Op -- | |
5468 | --------------------------- | |
5469 | ||
5470 | -- Context requires a boolean type, and plays no role in resolution. | |
fbf5a39b AC |
5471 | -- Processing identical to that for equality operators. The result |
5472 | -- type is the base type, which matters when pathological subtypes of | |
5473 | -- booleans with limited ranges are used. | |
996ae0b0 RK |
5474 | |
5475 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is | |
5476 | L : constant Node_Id := Left_Opnd (N); | |
5477 | R : constant Node_Id := Right_Opnd (N); | |
5478 | T : Entity_Id; | |
5479 | ||
5480 | begin | |
f61580d4 AC |
5481 | Check_No_Direct_Boolean_Operators (N); |
5482 | ||
d81b4bfe TQ |
5483 | -- If this is an intrinsic operation which is not predefined, use the |
5484 | -- types of its declared arguments to resolve the possibly overloaded | |
5485 | -- operands. Otherwise the operands are unambiguous and specify the | |
5486 | -- expected type. | |
996ae0b0 RK |
5487 | |
5488 | if Scope (Entity (N)) /= Standard_Standard then | |
5489 | T := Etype (First_Entity (Entity (N))); | |
1420b484 | 5490 | |
996ae0b0 RK |
5491 | else |
5492 | T := Find_Unique_Type (L, R); | |
5493 | ||
5494 | if T = Any_Fixed then | |
5495 | T := Unique_Fixed_Point_Type (L); | |
5496 | end if; | |
5497 | end if; | |
5498 | ||
fbf5a39b | 5499 | Set_Etype (N, Base_Type (Typ)); |
996ae0b0 RK |
5500 | Generate_Reference (T, N, ' '); |
5501 | ||
5502 | if T /= Any_Type then | |
d81b4bfe TQ |
5503 | if T = Any_String or else |
5504 | T = Any_Composite or else | |
5505 | T = Any_Character | |
996ae0b0 RK |
5506 | then |
5507 | if T = Any_Character then | |
5508 | Ambiguous_Character (L); | |
5509 | else | |
5510 | Error_Msg_N ("ambiguous operands for comparison", N); | |
5511 | end if; | |
5512 | ||
5513 | Set_Etype (N, Any_Type); | |
5514 | return; | |
5515 | ||
5516 | else | |
996ae0b0 RK |
5517 | Resolve (L, T); |
5518 | Resolve (R, T); | |
5519 | Check_Unset_Reference (L); | |
5520 | Check_Unset_Reference (R); | |
fbf5a39b | 5521 | Generate_Operator_Reference (N, T); |
fad0600d | 5522 | Check_Low_Bound_Tested (N); |
996ae0b0 RK |
5523 | Eval_Relational_Op (N); |
5524 | end if; | |
5525 | end if; | |
996ae0b0 RK |
5526 | end Resolve_Comparison_Op; |
5527 | ||
5528 | ------------------------------------ | |
5529 | -- Resolve_Conditional_Expression -- | |
5530 | ------------------------------------ | |
5531 | ||
5532 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is | |
5533 | Condition : constant Node_Id := First (Expressions (N)); | |
5534 | Then_Expr : constant Node_Id := Next (Condition); | |
b46be8a2 RD |
5535 | Else_Expr : Node_Id := Next (Then_Expr); |
5536 | ||
996ae0b0 | 5537 | begin |
b46be8a2 | 5538 | Resolve (Condition, Any_Boolean); |
996ae0b0 | 5539 | Resolve (Then_Expr, Typ); |
b46be8a2 RD |
5540 | |
5541 | -- If ELSE expression present, just resolve using the determined type | |
5542 | ||
5543 | if Present (Else_Expr) then | |
5544 | Resolve (Else_Expr, Typ); | |
5545 | ||
5546 | -- If no ELSE expression is present, root type must be Standard.Boolean | |
5547 | -- and we provide a Standard.True result converted to the appropriate | |
5548 | -- Boolean type (in case it is a derived boolean type). | |
5549 | ||
5550 | elsif Root_Type (Typ) = Standard_Boolean then | |
5551 | Else_Expr := | |
5552 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))); | |
5553 | Analyze_And_Resolve (Else_Expr, Typ); | |
5554 | Append_To (Expressions (N), Else_Expr); | |
5555 | ||
5556 | else | |
5557 | Error_Msg_N ("can only omit ELSE expression in Boolean case", N); | |
5558 | Append_To (Expressions (N), Error); | |
5559 | end if; | |
5560 | ||
996ae0b0 RK |
5561 | Set_Etype (N, Typ); |
5562 | Eval_Conditional_Expression (N); | |
5563 | end Resolve_Conditional_Expression; | |
5564 | ||
5565 | ----------------------------------------- | |
5566 | -- Resolve_Discrete_Subtype_Indication -- | |
5567 | ----------------------------------------- | |
5568 | ||
5569 | procedure Resolve_Discrete_Subtype_Indication | |
5570 | (N : Node_Id; | |
5571 | Typ : Entity_Id) | |
5572 | is | |
5573 | R : Node_Id; | |
5574 | S : Entity_Id; | |
5575 | ||
5576 | begin | |
5577 | Analyze (Subtype_Mark (N)); | |
5578 | S := Entity (Subtype_Mark (N)); | |
5579 | ||
5580 | if Nkind (Constraint (N)) /= N_Range_Constraint then | |
5581 | Error_Msg_N ("expect range constraint for discrete type", N); | |
5582 | Set_Etype (N, Any_Type); | |
5583 | ||
5584 | else | |
5585 | R := Range_Expression (Constraint (N)); | |
5c736541 RD |
5586 | |
5587 | if R = Error then | |
5588 | return; | |
5589 | end if; | |
5590 | ||
996ae0b0 RK |
5591 | Analyze (R); |
5592 | ||
5593 | if Base_Type (S) /= Base_Type (Typ) then | |
5594 | Error_Msg_NE | |
5595 | ("expect subtype of }", N, First_Subtype (Typ)); | |
5596 | ||
5597 | -- Rewrite the constraint as a range of Typ | |
5598 | -- to allow compilation to proceed further. | |
5599 | ||
5600 | Set_Etype (N, Typ); | |
5601 | Rewrite (Low_Bound (R), | |
5602 | Make_Attribute_Reference (Sloc (Low_Bound (R)), | |
5603 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), | |
5604 | Attribute_Name => Name_First)); | |
5605 | Rewrite (High_Bound (R), | |
5606 | Make_Attribute_Reference (Sloc (High_Bound (R)), | |
5607 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), | |
5608 | Attribute_Name => Name_First)); | |
5609 | ||
5610 | else | |
5611 | Resolve (R, Typ); | |
5612 | Set_Etype (N, Etype (R)); | |
5613 | ||
5614 | -- Additionally, we must check that the bounds are compatible | |
5615 | -- with the given subtype, which might be different from the | |
5616 | -- type of the context. | |
5617 | ||
5618 | Apply_Range_Check (R, S); | |
5619 | ||
5620 | -- ??? If the above check statically detects a Constraint_Error | |
5621 | -- it replaces the offending bound(s) of the range R with a | |
5622 | -- Constraint_Error node. When the itype which uses these bounds | |
5623 | -- is frozen the resulting call to Duplicate_Subexpr generates | |
5624 | -- a new temporary for the bounds. | |
5625 | ||
5626 | -- Unfortunately there are other itypes that are also made depend | |
5627 | -- on these bounds, so when Duplicate_Subexpr is called they get | |
5628 | -- a forward reference to the newly created temporaries and Gigi | |
5629 | -- aborts on such forward references. This is probably sign of a | |
5630 | -- more fundamental problem somewhere else in either the order of | |
5631 | -- itype freezing or the way certain itypes are constructed. | |
5632 | ||
5633 | -- To get around this problem we call Remove_Side_Effects right | |
5634 | -- away if either bounds of R are a Constraint_Error. | |
5635 | ||
5636 | declare | |
fbf5a39b AC |
5637 | L : constant Node_Id := Low_Bound (R); |
5638 | H : constant Node_Id := High_Bound (R); | |
996ae0b0 RK |
5639 | |
5640 | begin | |
5641 | if Nkind (L) = N_Raise_Constraint_Error then | |
5642 | Remove_Side_Effects (L); | |
5643 | end if; | |
5644 | ||
5645 | if Nkind (H) = N_Raise_Constraint_Error then | |
5646 | Remove_Side_Effects (H); | |
5647 | end if; | |
5648 | end; | |
5649 | ||
5650 | Check_Unset_Reference (Low_Bound (R)); | |
5651 | Check_Unset_Reference (High_Bound (R)); | |
5652 | end if; | |
5653 | end if; | |
5654 | end Resolve_Discrete_Subtype_Indication; | |
5655 | ||
5656 | ------------------------- | |
5657 | -- Resolve_Entity_Name -- | |
5658 | ------------------------- | |
5659 | ||
5660 | -- Used to resolve identifiers and expanded names | |
5661 | ||
5662 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is | |
5663 | E : constant Entity_Id := Entity (N); | |
5664 | ||
5665 | begin | |
07fc65c4 GB |
5666 | -- If garbage from errors, set to Any_Type and return |
5667 | ||
5668 | if No (E) and then Total_Errors_Detected /= 0 then | |
5669 | Set_Etype (N, Any_Type); | |
5670 | return; | |
5671 | end if; | |
5672 | ||
996ae0b0 RK |
5673 | -- Replace named numbers by corresponding literals. Note that this is |
5674 | -- the one case where Resolve_Entity_Name must reset the Etype, since | |
5675 | -- it is currently marked as universal. | |
5676 | ||
5677 | if Ekind (E) = E_Named_Integer then | |
5678 | Set_Etype (N, Typ); | |
5679 | Eval_Named_Integer (N); | |
5680 | ||
5681 | elsif Ekind (E) = E_Named_Real then | |
5682 | Set_Etype (N, Typ); | |
5683 | Eval_Named_Real (N); | |
5684 | ||
5685 | -- Allow use of subtype only if it is a concurrent type where we are | |
d81b4bfe TQ |
5686 | -- currently inside the body. This will eventually be expanded into a |
5687 | -- call to Self (for tasks) or _object (for protected objects). Any | |
5688 | -- other use of a subtype is invalid. | |
996ae0b0 RK |
5689 | |
5690 | elsif Is_Type (E) then | |
5691 | if Is_Concurrent_Type (E) | |
5692 | and then In_Open_Scopes (E) | |
5693 | then | |
5694 | null; | |
5695 | else | |
5696 | Error_Msg_N | |
758c442c | 5697 | ("invalid use of subtype mark in expression or call", N); |
996ae0b0 RK |
5698 | end if; |
5699 | ||
5700 | -- Check discriminant use if entity is discriminant in current scope, | |
5701 | -- i.e. discriminant of record or concurrent type currently being | |
5702 | -- analyzed. Uses in corresponding body are unrestricted. | |
5703 | ||
5704 | elsif Ekind (E) = E_Discriminant | |
5705 | and then Scope (E) = Current_Scope | |
5706 | and then not Has_Completion (Current_Scope) | |
5707 | then | |
5708 | Check_Discriminant_Use (N); | |
5709 | ||
5710 | -- A parameterless generic function cannot appear in a context that | |
5711 | -- requires resolution. | |
5712 | ||
5713 | elsif Ekind (E) = E_Generic_Function then | |
5714 | Error_Msg_N ("illegal use of generic function", N); | |
5715 | ||
5716 | elsif Ekind (E) = E_Out_Parameter | |
0ab80019 | 5717 | and then Ada_Version = Ada_83 |
996ae0b0 RK |
5718 | and then (Nkind (Parent (N)) in N_Op |
5719 | or else (Nkind (Parent (N)) = N_Assignment_Statement | |
5720 | and then N = Expression (Parent (N))) | |
5721 | or else Nkind (Parent (N)) = N_Explicit_Dereference) | |
5722 | then | |
5723 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", N); | |
5724 | ||
5725 | -- In all other cases, just do the possible static evaluation | |
5726 | ||
5727 | else | |
d81b4bfe TQ |
5728 | -- A deferred constant that appears in an expression must have a |
5729 | -- completion, unless it has been removed by in-place expansion of | |
5730 | -- an aggregate. | |
996ae0b0 RK |
5731 | |
5732 | if Ekind (E) = E_Constant | |
5733 | and then Comes_From_Source (E) | |
5734 | and then No (Constant_Value (E)) | |
5735 | and then Is_Frozen (Etype (E)) | |
45fc7ddb | 5736 | and then not In_Spec_Expression |
996ae0b0 RK |
5737 | and then not Is_Imported (E) |
5738 | then | |
5739 | ||
5740 | if No_Initialization (Parent (E)) | |
5741 | or else (Present (Full_View (E)) | |
5742 | and then No_Initialization (Parent (Full_View (E)))) | |
5743 | then | |
5744 | null; | |
5745 | else | |
5746 | Error_Msg_N ( | |
5747 | "deferred constant is frozen before completion", N); | |
5748 | end if; | |
5749 | end if; | |
5750 | ||
5751 | Eval_Entity_Name (N); | |
5752 | end if; | |
5753 | end Resolve_Entity_Name; | |
5754 | ||
5755 | ------------------- | |
5756 | -- Resolve_Entry -- | |
5757 | ------------------- | |
5758 | ||
5759 | procedure Resolve_Entry (Entry_Name : Node_Id) is | |
5760 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
5761 | Nam : Entity_Id; | |
5762 | New_N : Node_Id; | |
5763 | S : Entity_Id; | |
5764 | Tsk : Entity_Id; | |
5765 | E_Name : Node_Id; | |
5766 | Index : Node_Id; | |
5767 | ||
5768 | function Actual_Index_Type (E : Entity_Id) return Entity_Id; | |
5769 | -- If the bounds of the entry family being called depend on task | |
5770 | -- discriminants, build a new index subtype where a discriminant is | |
5771 | -- replaced with the value of the discriminant of the target task. | |
5772 | -- The target task is the prefix of the entry name in the call. | |
5773 | ||
5774 | ----------------------- | |
5775 | -- Actual_Index_Type -- | |
5776 | ----------------------- | |
5777 | ||
5778 | function Actual_Index_Type (E : Entity_Id) return Entity_Id is | |
fbf5a39b AC |
5779 | Typ : constant Entity_Id := Entry_Index_Type (E); |
5780 | Tsk : constant Entity_Id := Scope (E); | |
5781 | Lo : constant Node_Id := Type_Low_Bound (Typ); | |
5782 | Hi : constant Node_Id := Type_High_Bound (Typ); | |
996ae0b0 RK |
5783 | New_T : Entity_Id; |
5784 | ||
5785 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id; | |
5786 | -- If the bound is given by a discriminant, replace with a reference | |
d81b4bfe TQ |
5787 | -- to the discriminant of the same name in the target task. If the |
5788 | -- entry name is the target of a requeue statement and the entry is | |
5789 | -- in the current protected object, the bound to be used is the | |
5790 | -- discriminal of the object (see apply_range_checks for details of | |
5791 | -- the transformation). | |
996ae0b0 RK |
5792 | |
5793 | ----------------------------- | |
5794 | -- Actual_Discriminant_Ref -- | |
5795 | ----------------------------- | |
5796 | ||
5797 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is | |
fbf5a39b | 5798 | Typ : constant Entity_Id := Etype (Bound); |
996ae0b0 RK |
5799 | Ref : Node_Id; |
5800 | ||
5801 | begin | |
5802 | Remove_Side_Effects (Bound); | |
5803 | ||
5804 | if not Is_Entity_Name (Bound) | |
5805 | or else Ekind (Entity (Bound)) /= E_Discriminant | |
5806 | then | |
5807 | return Bound; | |
5808 | ||
5809 | elsif Is_Protected_Type (Tsk) | |
5810 | and then In_Open_Scopes (Tsk) | |
5811 | and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement | |
5812 | then | |
5813 | return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc); | |
5814 | ||
5815 | else | |
5816 | Ref := | |
5817 | Make_Selected_Component (Loc, | |
5818 | Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))), | |
5819 | Selector_Name => New_Occurrence_Of (Entity (Bound), Loc)); | |
5820 | Analyze (Ref); | |
5821 | Resolve (Ref, Typ); | |
5822 | return Ref; | |
5823 | end if; | |
5824 | end Actual_Discriminant_Ref; | |
5825 | ||
5826 | -- Start of processing for Actual_Index_Type | |
5827 | ||
5828 | begin | |
5829 | if not Has_Discriminants (Tsk) | |
5830 | or else (not Is_Entity_Name (Lo) | |
d81b4bfe TQ |
5831 | and then |
5832 | not Is_Entity_Name (Hi)) | |
996ae0b0 RK |
5833 | then |
5834 | return Entry_Index_Type (E); | |
5835 | ||
5836 | else | |
5837 | New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name)); | |
5838 | Set_Etype (New_T, Base_Type (Typ)); | |
5839 | Set_Size_Info (New_T, Typ); | |
5840 | Set_RM_Size (New_T, RM_Size (Typ)); | |
5841 | Set_Scalar_Range (New_T, | |
5842 | Make_Range (Sloc (Entry_Name), | |
5843 | Low_Bound => Actual_Discriminant_Ref (Lo), | |
5844 | High_Bound => Actual_Discriminant_Ref (Hi))); | |
5845 | ||
5846 | return New_T; | |
5847 | end if; | |
5848 | end Actual_Index_Type; | |
5849 | ||
5850 | -- Start of processing of Resolve_Entry | |
5851 | ||
5852 | begin | |
5853 | -- Find name of entry being called, and resolve prefix of name | |
5854 | -- with its own type. The prefix can be overloaded, and the name | |
5855 | -- and signature of the entry must be taken into account. | |
5856 | ||
5857 | if Nkind (Entry_Name) = N_Indexed_Component then | |
5858 | ||
5859 | -- Case of dealing with entry family within the current tasks | |
5860 | ||
5861 | E_Name := Prefix (Entry_Name); | |
5862 | ||
5863 | else | |
5864 | E_Name := Entry_Name; | |
5865 | end if; | |
5866 | ||
5867 | if Is_Entity_Name (E_Name) then | |
996ae0b0 | 5868 | |
d81b4bfe TQ |
5869 | -- Entry call to an entry (or entry family) in the current task. This |
5870 | -- is legal even though the task will deadlock. Rewrite as call to | |
5871 | -- current task. | |
996ae0b0 | 5872 | |
d81b4bfe TQ |
5873 | -- This can also be a call to an entry in an enclosing task. If this |
5874 | -- is a single task, we have to retrieve its name, because the scope | |
5875 | -- of the entry is the task type, not the object. If the enclosing | |
5876 | -- task is a task type, the identity of the task is given by its own | |
5877 | -- self variable. | |
5878 | ||
5879 | -- Finally this can be a requeue on an entry of the same task or | |
5880 | -- protected object. | |
996ae0b0 RK |
5881 | |
5882 | S := Scope (Entity (E_Name)); | |
5883 | ||
5884 | for J in reverse 0 .. Scope_Stack.Last loop | |
996ae0b0 RK |
5885 | if Is_Task_Type (Scope_Stack.Table (J).Entity) |
5886 | and then not Comes_From_Source (S) | |
5887 | then | |
5888 | -- S is an enclosing task or protected object. The concurrent | |
5889 | -- declaration has been converted into a type declaration, and | |
5890 | -- the object itself has an object declaration that follows | |
5891 | -- the type in the same declarative part. | |
5892 | ||
5893 | Tsk := Next_Entity (S); | |
996ae0b0 RK |
5894 | while Etype (Tsk) /= S loop |
5895 | Next_Entity (Tsk); | |
5896 | end loop; | |
5897 | ||
5898 | S := Tsk; | |
5899 | exit; | |
5900 | ||
5901 | elsif S = Scope_Stack.Table (J).Entity then | |
5902 | ||
5903 | -- Call to current task. Will be transformed into call to Self | |
5904 | ||
5905 | exit; | |
5906 | ||
5907 | end if; | |
5908 | end loop; | |
5909 | ||
5910 | New_N := | |
5911 | Make_Selected_Component (Loc, | |
5912 | Prefix => New_Occurrence_Of (S, Loc), | |
5913 | Selector_Name => | |
5914 | New_Occurrence_Of (Entity (E_Name), Loc)); | |
5915 | Rewrite (E_Name, New_N); | |
5916 | Analyze (E_Name); | |
5917 | ||
5918 | elsif Nkind (Entry_Name) = N_Selected_Component | |
5919 | and then Is_Overloaded (Prefix (Entry_Name)) | |
5920 | then | |
d81b4bfe TQ |
5921 | -- Use the entry name (which must be unique at this point) to find |
5922 | -- the prefix that returns the corresponding task type or protected | |
5923 | -- type. | |
996ae0b0 RK |
5924 | |
5925 | declare | |
fbf5a39b AC |
5926 | Pref : constant Node_Id := Prefix (Entry_Name); |
5927 | Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name)); | |
996ae0b0 RK |
5928 | I : Interp_Index; |
5929 | It : Interp; | |
996ae0b0 RK |
5930 | |
5931 | begin | |
5932 | Get_First_Interp (Pref, I, It); | |
996ae0b0 | 5933 | while Present (It.Typ) loop |
996ae0b0 RK |
5934 | if Scope (Ent) = It.Typ then |
5935 | Set_Etype (Pref, It.Typ); | |
5936 | exit; | |
5937 | end if; | |
5938 | ||
5939 | Get_Next_Interp (I, It); | |
5940 | end loop; | |
5941 | end; | |
5942 | end if; | |
5943 | ||
5944 | if Nkind (Entry_Name) = N_Selected_Component then | |
fbf5a39b | 5945 | Resolve (Prefix (Entry_Name)); |
996ae0b0 RK |
5946 | |
5947 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
5948 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
fbf5a39b | 5949 | Resolve (Prefix (Prefix (Entry_Name))); |
996ae0b0 RK |
5950 | Index := First (Expressions (Entry_Name)); |
5951 | Resolve (Index, Entry_Index_Type (Nam)); | |
5952 | ||
d81b4bfe TQ |
5953 | -- Up to this point the expression could have been the actual in a |
5954 | -- simple entry call, and be given by a named association. | |
996ae0b0 RK |
5955 | |
5956 | if Nkind (Index) = N_Parameter_Association then | |
5957 | Error_Msg_N ("expect expression for entry index", Index); | |
5958 | else | |
5959 | Apply_Range_Check (Index, Actual_Index_Type (Nam)); | |
5960 | end if; | |
5961 | end if; | |
996ae0b0 RK |
5962 | end Resolve_Entry; |
5963 | ||
5964 | ------------------------ | |
5965 | -- Resolve_Entry_Call -- | |
5966 | ------------------------ | |
5967 | ||
5968 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is | |
5969 | Entry_Name : constant Node_Id := Name (N); | |
5970 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
5971 | Actuals : List_Id; | |
5972 | First_Named : Node_Id; | |
5973 | Nam : Entity_Id; | |
5974 | Norm_OK : Boolean; | |
5975 | Obj : Node_Id; | |
5976 | Was_Over : Boolean; | |
5977 | ||
5978 | begin | |
d81b4bfe TQ |
5979 | -- We kill all checks here, because it does not seem worth the effort to |
5980 | -- do anything better, an entry call is a big operation. | |
fbf5a39b AC |
5981 | |
5982 | Kill_All_Checks; | |
5983 | ||
996ae0b0 RK |
5984 | -- Processing of the name is similar for entry calls and protected |
5985 | -- operation calls. Once the entity is determined, we can complete | |
5986 | -- the resolution of the actuals. | |
5987 | ||
5988 | -- The selector may be overloaded, in the case of a protected object | |
5989 | -- with overloaded functions. The type of the context is used for | |
5990 | -- resolution. | |
5991 | ||
5992 | if Nkind (Entry_Name) = N_Selected_Component | |
5993 | and then Is_Overloaded (Selector_Name (Entry_Name)) | |
5994 | and then Typ /= Standard_Void_Type | |
5995 | then | |
5996 | declare | |
5997 | I : Interp_Index; | |
5998 | It : Interp; | |
5999 | ||
6000 | begin | |
6001 | Get_First_Interp (Selector_Name (Entry_Name), I, It); | |
996ae0b0 | 6002 | while Present (It.Typ) loop |
996ae0b0 RK |
6003 | if Covers (Typ, It.Typ) then |
6004 | Set_Entity (Selector_Name (Entry_Name), It.Nam); | |
6005 | Set_Etype (Entry_Name, It.Typ); | |
6006 | ||
6007 | Generate_Reference (It.Typ, N, ' '); | |
6008 | end if; | |
6009 | ||
6010 | Get_Next_Interp (I, It); | |
6011 | end loop; | |
6012 | end; | |
6013 | end if; | |
6014 | ||
6015 | Resolve_Entry (Entry_Name); | |
6016 | ||
6017 | if Nkind (Entry_Name) = N_Selected_Component then | |
6018 | ||
a77842bd | 6019 | -- Simple entry call |
996ae0b0 RK |
6020 | |
6021 | Nam := Entity (Selector_Name (Entry_Name)); | |
6022 | Obj := Prefix (Entry_Name); | |
6023 | Was_Over := Is_Overloaded (Selector_Name (Entry_Name)); | |
6024 | ||
6025 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6026 | ||
a77842bd | 6027 | -- Call to member of entry family |
996ae0b0 RK |
6028 | |
6029 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
6030 | Obj := Prefix (Prefix (Entry_Name)); | |
6031 | Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name))); | |
6032 | end if; | |
6033 | ||
fbf5a39b AC |
6034 | -- We cannot in general check the maximum depth of protected entry |
6035 | -- calls at compile time. But we can tell that any protected entry | |
6036 | -- call at all violates a specified nesting depth of zero. | |
6037 | ||
6038 | if Is_Protected_Type (Scope (Nam)) then | |
9f4fd324 | 6039 | Check_Restriction (Max_Entry_Queue_Length, N); |
fbf5a39b AC |
6040 | end if; |
6041 | ||
996ae0b0 RK |
6042 | -- Use context type to disambiguate a protected function that can be |
6043 | -- called without actuals and that returns an array type, and where | |
6044 | -- the argument list may be an indexing of the returned value. | |
6045 | ||
6046 | if Ekind (Nam) = E_Function | |
6047 | and then Needs_No_Actuals (Nam) | |
6048 | and then Present (Parameter_Associations (N)) | |
6049 | and then | |
6050 | ((Is_Array_Type (Etype (Nam)) | |
6051 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
6052 | ||
6053 | or else (Is_Access_Type (Etype (Nam)) | |
6054 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
6055 | and then Covers (Typ, | |
6056 | Component_Type (Designated_Type (Etype (Nam)))))) | |
6057 | then | |
6058 | declare | |
6059 | Index_Node : Node_Id; | |
6060 | ||
6061 | begin | |
6062 | Index_Node := | |
6063 | Make_Indexed_Component (Loc, | |
6064 | Prefix => | |
6065 | Make_Function_Call (Loc, | |
6066 | Name => Relocate_Node (Entry_Name)), | |
6067 | Expressions => Parameter_Associations (N)); | |
6068 | ||
6069 | -- Since we are correcting a node classification error made by | |
6070 | -- the parser, we call Replace rather than Rewrite. | |
6071 | ||
6072 | Replace (N, Index_Node); | |
6073 | Set_Etype (Prefix (N), Etype (Nam)); | |
6074 | Set_Etype (N, Typ); | |
6075 | Resolve_Indexed_Component (N, Typ); | |
6076 | return; | |
6077 | end; | |
6078 | end if; | |
6079 | ||
6080 | -- The operation name may have been overloaded. Order the actuals | |
fbf5a39b AC |
6081 | -- according to the formals of the resolved entity, and set the |
6082 | -- return type to that of the operation. | |
996ae0b0 RK |
6083 | |
6084 | if Was_Over then | |
6085 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
6086 | pragma Assert (Norm_OK); | |
fbf5a39b | 6087 | Set_Etype (N, Etype (Nam)); |
996ae0b0 RK |
6088 | end if; |
6089 | ||
6090 | Resolve_Actuals (N, Nam); | |
6091 | Generate_Reference (Nam, Entry_Name); | |
6092 | ||
6093 | if Ekind (Nam) = E_Entry | |
6094 | or else Ekind (Nam) = E_Entry_Family | |
6095 | then | |
6096 | Check_Potentially_Blocking_Operation (N); | |
6097 | end if; | |
6098 | ||
6099 | -- Verify that a procedure call cannot masquerade as an entry | |
6100 | -- call where an entry call is expected. | |
6101 | ||
6102 | if Ekind (Nam) = E_Procedure then | |
996ae0b0 RK |
6103 | if Nkind (Parent (N)) = N_Entry_Call_Alternative |
6104 | and then N = Entry_Call_Statement (Parent (N)) | |
6105 | then | |
6106 | Error_Msg_N ("entry call required in select statement", N); | |
6107 | ||
6108 | elsif Nkind (Parent (N)) = N_Triggering_Alternative | |
6109 | and then N = Triggering_Statement (Parent (N)) | |
6110 | then | |
6111 | Error_Msg_N ("triggering statement cannot be procedure call", N); | |
6112 | ||
6113 | elsif Ekind (Scope (Nam)) = E_Task_Type | |
6114 | and then not In_Open_Scopes (Scope (Nam)) | |
6115 | then | |
758c442c | 6116 | Error_Msg_N ("task has no entry with this name", Entry_Name); |
996ae0b0 RK |
6117 | end if; |
6118 | end if; | |
6119 | ||
d81b4bfe TQ |
6120 | -- After resolution, entry calls and protected procedure calls are |
6121 | -- changed into entry calls, for expansion. The structure of the node | |
6122 | -- does not change, so it can safely be done in place. Protected | |
6123 | -- function calls must keep their structure because they are | |
6124 | -- subexpressions. | |
996ae0b0 RK |
6125 | |
6126 | if Ekind (Nam) /= E_Function then | |
6127 | ||
6128 | -- A protected operation that is not a function may modify the | |
d81b4bfe TQ |
6129 | -- corresponding object, and cannot apply to a constant. If this |
6130 | -- is an internal call, the prefix is the type itself. | |
996ae0b0 RK |
6131 | |
6132 | if Is_Protected_Type (Scope (Nam)) | |
6133 | and then not Is_Variable (Obj) | |
6134 | and then (not Is_Entity_Name (Obj) | |
6135 | or else not Is_Type (Entity (Obj))) | |
6136 | then | |
6137 | Error_Msg_N | |
6138 | ("prefix of protected procedure or entry call must be variable", | |
6139 | Entry_Name); | |
6140 | end if; | |
6141 | ||
6142 | Actuals := Parameter_Associations (N); | |
6143 | First_Named := First_Named_Actual (N); | |
6144 | ||
6145 | Rewrite (N, | |
6146 | Make_Entry_Call_Statement (Loc, | |
6147 | Name => Entry_Name, | |
6148 | Parameter_Associations => Actuals)); | |
6149 | ||
6150 | Set_First_Named_Actual (N, First_Named); | |
6151 | Set_Analyzed (N, True); | |
6152 | ||
6153 | -- Protected functions can return on the secondary stack, in which | |
1420b484 | 6154 | -- case we must trigger the transient scope mechanism. |
996ae0b0 RK |
6155 | |
6156 | elsif Expander_Active | |
6157 | and then Requires_Transient_Scope (Etype (Nam)) | |
6158 | then | |
0669bebe | 6159 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 6160 | end if; |
996ae0b0 RK |
6161 | end Resolve_Entry_Call; |
6162 | ||
6163 | ------------------------- | |
6164 | -- Resolve_Equality_Op -- | |
6165 | ------------------------- | |
6166 | ||
d81b4bfe TQ |
6167 | -- Both arguments must have the same type, and the boolean context does |
6168 | -- not participate in the resolution. The first pass verifies that the | |
6169 | -- interpretation is not ambiguous, and the type of the left argument is | |
6170 | -- correctly set, or is Any_Type in case of ambiguity. If both arguments | |
6171 | -- are strings or aggregates, allocators, or Null, they are ambiguous even | |
6172 | -- though they carry a single (universal) type. Diagnose this case here. | |
996ae0b0 RK |
6173 | |
6174 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is | |
6175 | L : constant Node_Id := Left_Opnd (N); | |
6176 | R : constant Node_Id := Right_Opnd (N); | |
6177 | T : Entity_Id := Find_Unique_Type (L, R); | |
6178 | ||
6179 | function Find_Unique_Access_Type return Entity_Id; | |
6180 | -- In the case of allocators, make a last-ditch attempt to find a single | |
6181 | -- access type with the right designated type. This is semantically | |
6182 | -- dubious, and of no interest to any real code, but c48008a makes it | |
6183 | -- all worthwhile. | |
6184 | ||
6185 | ----------------------------- | |
6186 | -- Find_Unique_Access_Type -- | |
6187 | ----------------------------- | |
6188 | ||
6189 | function Find_Unique_Access_Type return Entity_Id is | |
6190 | Acc : Entity_Id; | |
6191 | E : Entity_Id; | |
1420b484 | 6192 | S : Entity_Id; |
996ae0b0 RK |
6193 | |
6194 | begin | |
6195 | if Ekind (Etype (R)) = E_Allocator_Type then | |
6196 | Acc := Designated_Type (Etype (R)); | |
996ae0b0 RK |
6197 | elsif Ekind (Etype (L)) = E_Allocator_Type then |
6198 | Acc := Designated_Type (Etype (L)); | |
996ae0b0 RK |
6199 | else |
6200 | return Empty; | |
6201 | end if; | |
6202 | ||
1420b484 | 6203 | S := Current_Scope; |
996ae0b0 RK |
6204 | while S /= Standard_Standard loop |
6205 | E := First_Entity (S); | |
996ae0b0 | 6206 | while Present (E) loop |
996ae0b0 RK |
6207 | if Is_Type (E) |
6208 | and then Is_Access_Type (E) | |
6209 | and then Ekind (E) /= E_Allocator_Type | |
6210 | and then Designated_Type (E) = Base_Type (Acc) | |
6211 | then | |
6212 | return E; | |
6213 | end if; | |
6214 | ||
6215 | Next_Entity (E); | |
6216 | end loop; | |
6217 | ||
6218 | S := Scope (S); | |
6219 | end loop; | |
6220 | ||
6221 | return Empty; | |
6222 | end Find_Unique_Access_Type; | |
6223 | ||
6224 | -- Start of processing for Resolve_Equality_Op | |
6225 | ||
6226 | begin | |
f61580d4 AC |
6227 | Check_No_Direct_Boolean_Operators (N); |
6228 | ||
996ae0b0 RK |
6229 | Set_Etype (N, Base_Type (Typ)); |
6230 | Generate_Reference (T, N, ' '); | |
6231 | ||
6232 | if T = Any_Fixed then | |
6233 | T := Unique_Fixed_Point_Type (L); | |
6234 | end if; | |
6235 | ||
6236 | if T /= Any_Type then | |
996ae0b0 RK |
6237 | if T = Any_String |
6238 | or else T = Any_Composite | |
6239 | or else T = Any_Character | |
6240 | then | |
996ae0b0 RK |
6241 | if T = Any_Character then |
6242 | Ambiguous_Character (L); | |
6243 | else | |
6244 | Error_Msg_N ("ambiguous operands for equality", N); | |
6245 | end if; | |
6246 | ||
6247 | Set_Etype (N, Any_Type); | |
6248 | return; | |
6249 | ||
6250 | elsif T = Any_Access | |
6251 | or else Ekind (T) = E_Allocator_Type | |
0669bebe | 6252 | or else Ekind (T) = E_Access_Attribute_Type |
996ae0b0 RK |
6253 | then |
6254 | T := Find_Unique_Access_Type; | |
6255 | ||
6256 | if No (T) then | |
6257 | Error_Msg_N ("ambiguous operands for equality", N); | |
6258 | Set_Etype (N, Any_Type); | |
6259 | return; | |
6260 | end if; | |
6261 | end if; | |
6262 | ||
996ae0b0 RK |
6263 | Resolve (L, T); |
6264 | Resolve (R, T); | |
fbf5a39b | 6265 | |
0669bebe GB |
6266 | -- If the unique type is a class-wide type then it will be expanded |
6267 | -- into a dispatching call to the predefined primitive. Therefore we | |
6268 | -- check here for potential violation of such restriction. | |
6269 | ||
6270 | if Is_Class_Wide_Type (T) then | |
6271 | Check_Restriction (No_Dispatching_Calls, N); | |
6272 | end if; | |
6273 | ||
fbf5a39b AC |
6274 | if Warn_On_Redundant_Constructs |
6275 | and then Comes_From_Source (N) | |
6276 | and then Is_Entity_Name (R) | |
6277 | and then Entity (R) = Standard_True | |
6278 | and then Comes_From_Source (R) | |
6279 | then | |
aa5147f0 | 6280 | Error_Msg_N ("?comparison with True is redundant!", R); |
fbf5a39b AC |
6281 | end if; |
6282 | ||
996ae0b0 RK |
6283 | Check_Unset_Reference (L); |
6284 | Check_Unset_Reference (R); | |
fbf5a39b | 6285 | Generate_Operator_Reference (N, T); |
fad0600d | 6286 | Check_Low_Bound_Tested (N); |
996ae0b0 RK |
6287 | |
6288 | -- If this is an inequality, it may be the implicit inequality | |
6289 | -- created for a user-defined operation, in which case the corres- | |
6290 | -- ponding equality operation is not intrinsic, and the operation | |
6291 | -- cannot be constant-folded. Else fold. | |
6292 | ||
6293 | if Nkind (N) = N_Op_Eq | |
6294 | or else Comes_From_Source (Entity (N)) | |
6295 | or else Ekind (Entity (N)) = E_Operator | |
6296 | or else Is_Intrinsic_Subprogram | |
6297 | (Corresponding_Equality (Entity (N))) | |
6298 | then | |
6299 | Eval_Relational_Op (N); | |
45fc7ddb | 6300 | |
996ae0b0 | 6301 | elsif Nkind (N) = N_Op_Ne |
0669bebe | 6302 | and then Is_Abstract_Subprogram (Entity (N)) |
996ae0b0 RK |
6303 | then |
6304 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N)); | |
6305 | end if; | |
758c442c | 6306 | |
d81b4bfe TQ |
6307 | -- Ada 2005: If one operand is an anonymous access type, convert the |
6308 | -- other operand to it, to ensure that the underlying types match in | |
6309 | -- the back-end. Same for access_to_subprogram, and the conversion | |
6310 | -- verifies that the types are subtype conformant. | |
b7d1f17f | 6311 | |
d81b4bfe TQ |
6312 | -- We apply the same conversion in the case one of the operands is a |
6313 | -- private subtype of the type of the other. | |
c8ef728f | 6314 | |
b7d1f17f HK |
6315 | -- Why the Expander_Active test here ??? |
6316 | ||
4197ae1e | 6317 | if Expander_Active |
b7d1f17f HK |
6318 | and then |
6319 | (Ekind (T) = E_Anonymous_Access_Type | |
6320 | or else Ekind (T) = E_Anonymous_Access_Subprogram_Type | |
6321 | or else Is_Private_Type (T)) | |
c8ef728f ES |
6322 | then |
6323 | if Etype (L) /= T then | |
6324 | Rewrite (L, | |
6325 | Make_Unchecked_Type_Conversion (Sloc (L), | |
6326 | Subtype_Mark => New_Occurrence_Of (T, Sloc (L)), | |
6327 | Expression => Relocate_Node (L))); | |
6328 | Analyze_And_Resolve (L, T); | |
6329 | end if; | |
6330 | ||
6331 | if (Etype (R)) /= T then | |
6332 | Rewrite (R, | |
6333 | Make_Unchecked_Type_Conversion (Sloc (R), | |
6334 | Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)), | |
6335 | Expression => Relocate_Node (R))); | |
6336 | Analyze_And_Resolve (R, T); | |
6337 | end if; | |
6338 | end if; | |
996ae0b0 RK |
6339 | end if; |
6340 | end Resolve_Equality_Op; | |
6341 | ||
6342 | ---------------------------------- | |
6343 | -- Resolve_Explicit_Dereference -- | |
6344 | ---------------------------------- | |
6345 | ||
6346 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is | |
bc5f3720 RD |
6347 | Loc : constant Source_Ptr := Sloc (N); |
6348 | New_N : Node_Id; | |
6349 | P : constant Node_Id := Prefix (N); | |
6350 | I : Interp_Index; | |
6351 | It : Interp; | |
996ae0b0 RK |
6352 | |
6353 | begin | |
c8ef728f | 6354 | Check_Fully_Declared_Prefix (Typ, P); |
996ae0b0 RK |
6355 | |
6356 | if Is_Overloaded (P) then | |
6357 | ||
758c442c GD |
6358 | -- Use the context type to select the prefix that has the correct |
6359 | -- designated type. | |
996ae0b0 RK |
6360 | |
6361 | Get_First_Interp (P, I, It); | |
6362 | while Present (It.Typ) loop | |
6363 | exit when Is_Access_Type (It.Typ) | |
6364 | and then Covers (Typ, Designated_Type (It.Typ)); | |
996ae0b0 RK |
6365 | Get_Next_Interp (I, It); |
6366 | end loop; | |
6367 | ||
bc5f3720 RD |
6368 | if Present (It.Typ) then |
6369 | Resolve (P, It.Typ); | |
6370 | else | |
758c442c GD |
6371 | -- If no interpretation covers the designated type of the prefix, |
6372 | -- this is the pathological case where not all implementations of | |
6373 | -- the prefix allow the interpretation of the node as a call. Now | |
6374 | -- that the expected type is known, Remove other interpretations | |
6375 | -- from prefix, rewrite it as a call, and resolve again, so that | |
6376 | -- the proper call node is generated. | |
bc5f3720 RD |
6377 | |
6378 | Get_First_Interp (P, I, It); | |
6379 | while Present (It.Typ) loop | |
6380 | if Ekind (It.Typ) /= E_Access_Subprogram_Type then | |
6381 | Remove_Interp (I); | |
6382 | end if; | |
6383 | ||
6384 | Get_Next_Interp (I, It); | |
6385 | end loop; | |
6386 | ||
6387 | New_N := | |
6388 | Make_Function_Call (Loc, | |
6389 | Name => | |
6390 | Make_Explicit_Dereference (Loc, | |
6391 | Prefix => P), | |
6392 | Parameter_Associations => New_List); | |
6393 | ||
6394 | Save_Interps (N, New_N); | |
6395 | Rewrite (N, New_N); | |
6396 | Analyze_And_Resolve (N, Typ); | |
6397 | return; | |
6398 | end if; | |
6399 | ||
996ae0b0 RK |
6400 | Set_Etype (N, Designated_Type (It.Typ)); |
6401 | ||
6402 | else | |
fbf5a39b | 6403 | Resolve (P); |
996ae0b0 RK |
6404 | end if; |
6405 | ||
6406 | if Is_Access_Type (Etype (P)) then | |
6407 | Apply_Access_Check (N); | |
6408 | end if; | |
6409 | ||
758c442c GD |
6410 | -- If the designated type is a packed unconstrained array type, and the |
6411 | -- explicit dereference is not in the context of an attribute reference, | |
6412 | -- then we must compute and set the actual subtype, since it is needed | |
6413 | -- by Gigi. The reason we exclude the attribute case is that this is | |
6414 | -- handled fine by Gigi, and in fact we use such attributes to build the | |
6415 | -- actual subtype. We also exclude generated code (which builds actual | |
6416 | -- subtypes directly if they are needed). | |
996ae0b0 RK |
6417 | |
6418 | if Is_Array_Type (Etype (N)) | |
6419 | and then Is_Packed (Etype (N)) | |
6420 | and then not Is_Constrained (Etype (N)) | |
6421 | and then Nkind (Parent (N)) /= N_Attribute_Reference | |
6422 | and then Comes_From_Source (N) | |
6423 | then | |
6424 | Set_Etype (N, Get_Actual_Subtype (N)); | |
6425 | end if; | |
6426 | ||
758c442c GD |
6427 | -- Note: there is no Eval processing required for an explicit deference, |
6428 | -- because the type is known to be an allocators, and allocator | |
6429 | -- expressions can never be static. | |
996ae0b0 RK |
6430 | |
6431 | end Resolve_Explicit_Dereference; | |
6432 | ||
6433 | ------------------------------- | |
6434 | -- Resolve_Indexed_Component -- | |
6435 | ------------------------------- | |
6436 | ||
6437 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is | |
6438 | Name : constant Node_Id := Prefix (N); | |
6439 | Expr : Node_Id; | |
6440 | Array_Type : Entity_Id := Empty; -- to prevent junk warning | |
6441 | Index : Node_Id; | |
6442 | ||
6443 | begin | |
6444 | if Is_Overloaded (Name) then | |
6445 | ||
758c442c GD |
6446 | -- Use the context type to select the prefix that yields the correct |
6447 | -- component type. | |
996ae0b0 RK |
6448 | |
6449 | declare | |
6450 | I : Interp_Index; | |
6451 | It : Interp; | |
6452 | I1 : Interp_Index := 0; | |
6453 | P : constant Node_Id := Prefix (N); | |
6454 | Found : Boolean := False; | |
6455 | ||
6456 | begin | |
6457 | Get_First_Interp (P, I, It); | |
996ae0b0 | 6458 | while Present (It.Typ) loop |
996ae0b0 RK |
6459 | if (Is_Array_Type (It.Typ) |
6460 | and then Covers (Typ, Component_Type (It.Typ))) | |
6461 | or else (Is_Access_Type (It.Typ) | |
6462 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
6463 | and then Covers | |
6464 | (Typ, Component_Type (Designated_Type (It.Typ)))) | |
6465 | then | |
6466 | if Found then | |
6467 | It := Disambiguate (P, I1, I, Any_Type); | |
6468 | ||
6469 | if It = No_Interp then | |
6470 | Error_Msg_N ("ambiguous prefix for indexing", N); | |
6471 | Set_Etype (N, Typ); | |
6472 | return; | |
6473 | ||
6474 | else | |
6475 | Found := True; | |
6476 | Array_Type := It.Typ; | |
6477 | I1 := I; | |
6478 | end if; | |
6479 | ||
6480 | else | |
6481 | Found := True; | |
6482 | Array_Type := It.Typ; | |
6483 | I1 := I; | |
6484 | end if; | |
6485 | end if; | |
6486 | ||
6487 | Get_Next_Interp (I, It); | |
6488 | end loop; | |
6489 | end; | |
6490 | ||
6491 | else | |
6492 | Array_Type := Etype (Name); | |
6493 | end if; | |
6494 | ||
6495 | Resolve (Name, Array_Type); | |
6496 | Array_Type := Get_Actual_Subtype_If_Available (Name); | |
6497 | ||
6498 | -- If prefix is access type, dereference to get real array type. | |
6499 | -- Note: we do not apply an access check because the expander always | |
6500 | -- introduces an explicit dereference, and the check will happen there. | |
6501 | ||
6502 | if Is_Access_Type (Array_Type) then | |
6503 | Array_Type := Designated_Type (Array_Type); | |
6504 | end if; | |
6505 | ||
a77842bd | 6506 | -- If name was overloaded, set component type correctly now |
f3d57416 | 6507 | -- If a misplaced call to an entry family (which has no index types) |
b7d1f17f | 6508 | -- return. Error will be diagnosed from calling context. |
996ae0b0 | 6509 | |
b7d1f17f HK |
6510 | if Is_Array_Type (Array_Type) then |
6511 | Set_Etype (N, Component_Type (Array_Type)); | |
6512 | else | |
6513 | return; | |
6514 | end if; | |
996ae0b0 RK |
6515 | |
6516 | Index := First_Index (Array_Type); | |
6517 | Expr := First (Expressions (N)); | |
6518 | ||
758c442c GD |
6519 | -- The prefix may have resolved to a string literal, in which case its |
6520 | -- etype has a special representation. This is only possible currently | |
6521 | -- if the prefix is a static concatenation, written in functional | |
6522 | -- notation. | |
996ae0b0 RK |
6523 | |
6524 | if Ekind (Array_Type) = E_String_Literal_Subtype then | |
6525 | Resolve (Expr, Standard_Positive); | |
6526 | ||
6527 | else | |
6528 | while Present (Index) and Present (Expr) loop | |
6529 | Resolve (Expr, Etype (Index)); | |
6530 | Check_Unset_Reference (Expr); | |
6531 | ||
6532 | if Is_Scalar_Type (Etype (Expr)) then | |
6533 | Apply_Scalar_Range_Check (Expr, Etype (Index)); | |
6534 | else | |
6535 | Apply_Range_Check (Expr, Get_Actual_Subtype (Index)); | |
6536 | end if; | |
6537 | ||
6538 | Next_Index (Index); | |
6539 | Next (Expr); | |
6540 | end loop; | |
6541 | end if; | |
6542 | ||
0669bebe GB |
6543 | -- Do not generate the warning on suspicious index if we are analyzing |
6544 | -- package Ada.Tags; otherwise we will report the warning with the | |
6545 | -- Prims_Ptr field of the dispatch table. | |
6546 | ||
6547 | if Scope (Etype (Prefix (N))) = Standard_Standard | |
6548 | or else not | |
6549 | Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))), | |
6550 | Ada_Tags) | |
6551 | then | |
6552 | Warn_On_Suspicious_Index (Name, First (Expressions (N))); | |
6553 | Eval_Indexed_Component (N); | |
6554 | end if; | |
996ae0b0 RK |
6555 | end Resolve_Indexed_Component; |
6556 | ||
6557 | ----------------------------- | |
6558 | -- Resolve_Integer_Literal -- | |
6559 | ----------------------------- | |
6560 | ||
6561 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is | |
6562 | begin | |
6563 | Set_Etype (N, Typ); | |
6564 | Eval_Integer_Literal (N); | |
6565 | end Resolve_Integer_Literal; | |
6566 | ||
15ce9ca2 AC |
6567 | -------------------------------- |
6568 | -- Resolve_Intrinsic_Operator -- | |
6569 | -------------------------------- | |
996ae0b0 RK |
6570 | |
6571 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
6572 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); |
6573 | Op : Entity_Id; | |
6574 | Arg1 : Node_Id; | |
6575 | Arg2 : Node_Id; | |
996ae0b0 RK |
6576 | |
6577 | begin | |
6578 | Op := Entity (N); | |
996ae0b0 RK |
6579 | while Scope (Op) /= Standard_Standard loop |
6580 | Op := Homonym (Op); | |
6581 | pragma Assert (Present (Op)); | |
6582 | end loop; | |
6583 | ||
6584 | Set_Entity (N, Op); | |
af152989 | 6585 | Set_Is_Overloaded (N, False); |
996ae0b0 | 6586 | |
758c442c GD |
6587 | -- If the operand type is private, rewrite with suitable conversions on |
6588 | -- the operands and the result, to expose the proper underlying numeric | |
6589 | -- type. | |
996ae0b0 | 6590 | |
fbf5a39b AC |
6591 | if Is_Private_Type (Typ) then |
6592 | Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N)); | |
6593 | ||
6594 | if Nkind (N) = N_Op_Expon then | |
6595 | Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N)); | |
6596 | else | |
6597 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
6598 | end if; | |
6599 | ||
6600 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
6601 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
996ae0b0 | 6602 | |
fbf5a39b AC |
6603 | Set_Left_Opnd (N, Arg1); |
6604 | Set_Right_Opnd (N, Arg2); | |
6605 | ||
6606 | Set_Etype (N, Btyp); | |
6607 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
6608 | Resolve (N, Typ); | |
6609 | ||
6610 | elsif Typ /= Etype (Left_Opnd (N)) | |
6611 | or else Typ /= Etype (Right_Opnd (N)) | |
6612 | then | |
d81b4bfe TQ |
6613 | -- Add explicit conversion where needed, and save interpretations in |
6614 | -- case operands are overloaded. | |
fbf5a39b | 6615 | |
af152989 | 6616 | Arg1 := Convert_To (Typ, Left_Opnd (N)); |
fbf5a39b AC |
6617 | Arg2 := Convert_To (Typ, Right_Opnd (N)); |
6618 | ||
6619 | if Nkind (Arg1) = N_Type_Conversion then | |
6620 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
af152989 AC |
6621 | else |
6622 | Save_Interps (Left_Opnd (N), Arg1); | |
fbf5a39b AC |
6623 | end if; |
6624 | ||
6625 | if Nkind (Arg2) = N_Type_Conversion then | |
6626 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
af152989 | 6627 | else |
0ab80019 | 6628 | Save_Interps (Right_Opnd (N), Arg2); |
fbf5a39b AC |
6629 | end if; |
6630 | ||
6631 | Rewrite (Left_Opnd (N), Arg1); | |
6632 | Rewrite (Right_Opnd (N), Arg2); | |
6633 | Analyze (Arg1); | |
6634 | Analyze (Arg2); | |
6635 | Resolve_Arithmetic_Op (N, Typ); | |
6636 | ||
6637 | else | |
6638 | Resolve_Arithmetic_Op (N, Typ); | |
6639 | end if; | |
996ae0b0 RK |
6640 | end Resolve_Intrinsic_Operator; |
6641 | ||
fbf5a39b AC |
6642 | -------------------------------------- |
6643 | -- Resolve_Intrinsic_Unary_Operator -- | |
6644 | -------------------------------------- | |
6645 | ||
6646 | procedure Resolve_Intrinsic_Unary_Operator | |
6647 | (N : Node_Id; | |
6648 | Typ : Entity_Id) | |
6649 | is | |
6650 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); | |
6651 | Op : Entity_Id; | |
6652 | Arg2 : Node_Id; | |
6653 | ||
6654 | begin | |
6655 | Op := Entity (N); | |
fbf5a39b AC |
6656 | while Scope (Op) /= Standard_Standard loop |
6657 | Op := Homonym (Op); | |
6658 | pragma Assert (Present (Op)); | |
6659 | end loop; | |
6660 | ||
6661 | Set_Entity (N, Op); | |
6662 | ||
6663 | if Is_Private_Type (Typ) then | |
6664 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
6665 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
6666 | ||
6667 | Set_Right_Opnd (N, Arg2); | |
6668 | ||
6669 | Set_Etype (N, Btyp); | |
6670 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
6671 | Resolve (N, Typ); | |
6672 | ||
6673 | else | |
6674 | Resolve_Unary_Op (N, Typ); | |
6675 | end if; | |
6676 | end Resolve_Intrinsic_Unary_Operator; | |
6677 | ||
996ae0b0 RK |
6678 | ------------------------ |
6679 | -- Resolve_Logical_Op -- | |
6680 | ------------------------ | |
6681 | ||
6682 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is | |
6683 | B_Typ : Entity_Id; | |
6684 | ||
6685 | begin | |
f61580d4 AC |
6686 | Check_No_Direct_Boolean_Operators (N); |
6687 | ||
758c442c GD |
6688 | -- Predefined operations on scalar types yield the base type. On the |
6689 | -- other hand, logical operations on arrays yield the type of the | |
6690 | -- arguments (and the context). | |
996ae0b0 RK |
6691 | |
6692 | if Is_Array_Type (Typ) then | |
6693 | B_Typ := Typ; | |
6694 | else | |
6695 | B_Typ := Base_Type (Typ); | |
6696 | end if; | |
6697 | ||
6698 | -- The following test is required because the operands of the operation | |
6699 | -- may be literals, in which case the resulting type appears to be | |
6700 | -- compatible with a signed integer type, when in fact it is compatible | |
6701 | -- only with modular types. If the context itself is universal, the | |
6702 | -- operation is illegal. | |
6703 | ||
6704 | if not Valid_Boolean_Arg (Typ) then | |
6705 | Error_Msg_N ("invalid context for logical operation", N); | |
6706 | Set_Etype (N, Any_Type); | |
6707 | return; | |
6708 | ||
6709 | elsif Typ = Any_Modular then | |
6710 | Error_Msg_N | |
6711 | ("no modular type available in this context", N); | |
6712 | Set_Etype (N, Any_Type); | |
6713 | return; | |
07fc65c4 GB |
6714 | elsif Is_Modular_Integer_Type (Typ) |
6715 | and then Etype (Left_Opnd (N)) = Universal_Integer | |
6716 | and then Etype (Right_Opnd (N)) = Universal_Integer | |
6717 | then | |
6718 | Check_For_Visible_Operator (N, B_Typ); | |
996ae0b0 RK |
6719 | end if; |
6720 | ||
6721 | Resolve (Left_Opnd (N), B_Typ); | |
6722 | Resolve (Right_Opnd (N), B_Typ); | |
6723 | ||
6724 | Check_Unset_Reference (Left_Opnd (N)); | |
6725 | Check_Unset_Reference (Right_Opnd (N)); | |
6726 | ||
6727 | Set_Etype (N, B_Typ); | |
fbf5a39b | 6728 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
6729 | Eval_Logical_Op (N); |
6730 | end Resolve_Logical_Op; | |
6731 | ||
6732 | --------------------------- | |
6733 | -- Resolve_Membership_Op -- | |
6734 | --------------------------- | |
6735 | ||
6736 | -- The context can only be a boolean type, and does not determine | |
6737 | -- the arguments. Arguments should be unambiguous, but the preference | |
6738 | -- rule for universal types applies. | |
6739 | ||
6740 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
6741 | pragma Warnings (Off, Typ); |
6742 | ||
197e4514 | 6743 | L : constant Node_Id := Left_Opnd (N); |
b1c11e0e | 6744 | R : constant Node_Id := Right_Opnd (N); |
996ae0b0 RK |
6745 | T : Entity_Id; |
6746 | ||
197e4514 AC |
6747 | procedure Resolve_Set_Membership; |
6748 | -- Analysis has determined a unique type for the left operand. | |
6749 | -- Use it to resolve the disjuncts. | |
6750 | ||
6751 | ---------------------------- | |
6752 | -- Resolve_Set_Membership -- | |
6753 | ---------------------------- | |
6754 | ||
6755 | procedure Resolve_Set_Membership is | |
6756 | Alt : Node_Id; | |
6757 | ||
6758 | begin | |
6759 | Resolve (L, Etype (L)); | |
6760 | ||
6761 | Alt := First (Alternatives (N)); | |
6762 | while Present (Alt) loop | |
6763 | ||
6764 | -- Alternative is an expression, a range | |
6765 | -- or a subtype mark. | |
6766 | ||
6767 | if not Is_Entity_Name (Alt) | |
6768 | or else not Is_Type (Entity (Alt)) | |
6769 | then | |
6770 | Resolve (Alt, Etype (L)); | |
6771 | end if; | |
6772 | ||
6773 | Next (Alt); | |
6774 | end loop; | |
6775 | end Resolve_Set_Membership; | |
6776 | ||
442c0581 | 6777 | -- Start of processing for Resolve_Membership_Op |
197e4514 | 6778 | |
996ae0b0 RK |
6779 | begin |
6780 | if L = Error or else R = Error then | |
6781 | return; | |
6782 | end if; | |
6783 | ||
197e4514 AC |
6784 | if Present (Alternatives (N)) then |
6785 | Resolve_Set_Membership; | |
6786 | return; | |
6787 | ||
6788 | elsif not Is_Overloaded (R) | |
996ae0b0 RK |
6789 | and then |
6790 | (Etype (R) = Universal_Integer or else | |
6791 | Etype (R) = Universal_Real) | |
6792 | and then Is_Overloaded (L) | |
6793 | then | |
6794 | T := Etype (R); | |
1420b484 | 6795 | |
d81b4bfe | 6796 | -- Ada 2005 (AI-251): Support the following case: |
1420b484 JM |
6797 | |
6798 | -- type I is interface; | |
6799 | -- type T is tagged ... | |
6800 | ||
c8ef728f | 6801 | -- function Test (O : I'Class) is |
1420b484 JM |
6802 | -- begin |
6803 | -- return O in T'Class. | |
6804 | -- end Test; | |
6805 | ||
d81b4bfe | 6806 | -- In this case we have nothing else to do. The membership test will be |
1420b484 JM |
6807 | -- done at run-time. |
6808 | ||
6809 | elsif Ada_Version >= Ada_05 | |
6810 | and then Is_Class_Wide_Type (Etype (L)) | |
6811 | and then Is_Interface (Etype (L)) | |
6812 | and then Is_Class_Wide_Type (Etype (R)) | |
6813 | and then not Is_Interface (Etype (R)) | |
6814 | then | |
6815 | return; | |
6816 | ||
996ae0b0 RK |
6817 | else |
6818 | T := Intersect_Types (L, R); | |
6819 | end if; | |
6820 | ||
6821 | Resolve (L, T); | |
6822 | Check_Unset_Reference (L); | |
6823 | ||
6824 | if Nkind (R) = N_Range | |
6825 | and then not Is_Scalar_Type (T) | |
6826 | then | |
6827 | Error_Msg_N ("scalar type required for range", R); | |
6828 | end if; | |
6829 | ||
6830 | if Is_Entity_Name (R) then | |
6831 | Freeze_Expression (R); | |
6832 | else | |
6833 | Resolve (R, T); | |
6834 | Check_Unset_Reference (R); | |
6835 | end if; | |
6836 | ||
6837 | Eval_Membership_Op (N); | |
6838 | end Resolve_Membership_Op; | |
6839 | ||
6840 | ------------------ | |
6841 | -- Resolve_Null -- | |
6842 | ------------------ | |
6843 | ||
6844 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is | |
b1c11e0e JM |
6845 | Loc : constant Source_Ptr := Sloc (N); |
6846 | ||
996ae0b0 | 6847 | begin |
758c442c | 6848 | -- Handle restriction against anonymous null access values This |
6ba6b1e3 | 6849 | -- restriction can be turned off using -gnatdj. |
996ae0b0 | 6850 | |
0ab80019 | 6851 | -- Ada 2005 (AI-231): Remove restriction |
2820d220 | 6852 | |
0ab80019 | 6853 | if Ada_Version < Ada_05 |
2820d220 | 6854 | and then not Debug_Flag_J |
996ae0b0 RK |
6855 | and then Ekind (Typ) = E_Anonymous_Access_Type |
6856 | and then Comes_From_Source (N) | |
6857 | then | |
d81b4bfe TQ |
6858 | -- In the common case of a call which uses an explicitly null value |
6859 | -- for an access parameter, give specialized error message. | |
996ae0b0 | 6860 | |
45fc7ddb HK |
6861 | if Nkind_In (Parent (N), N_Procedure_Call_Statement, |
6862 | N_Function_Call) | |
996ae0b0 RK |
6863 | then |
6864 | Error_Msg_N | |
6865 | ("null is not allowed as argument for an access parameter", N); | |
6866 | ||
6867 | -- Standard message for all other cases (are there any?) | |
6868 | ||
6869 | else | |
6870 | Error_Msg_N | |
6871 | ("null cannot be of an anonymous access type", N); | |
6872 | end if; | |
6873 | end if; | |
6874 | ||
b1c11e0e JM |
6875 | -- Ada 2005 (AI-231): Generate the null-excluding check in case of |
6876 | -- assignment to a null-excluding object | |
6877 | ||
6878 | if Ada_Version >= Ada_05 | |
6879 | and then Can_Never_Be_Null (Typ) | |
6880 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
6881 | then | |
6882 | if not Inside_Init_Proc then | |
6883 | Insert_Action | |
6884 | (Compile_Time_Constraint_Error (N, | |
6885 | "(Ada 2005) null not allowed in null-excluding objects?"), | |
6886 | Make_Raise_Constraint_Error (Loc, | |
6887 | Reason => CE_Access_Check_Failed)); | |
6888 | else | |
6889 | Insert_Action (N, | |
6890 | Make_Raise_Constraint_Error (Loc, | |
6891 | Reason => CE_Access_Check_Failed)); | |
6892 | end if; | |
6893 | end if; | |
6894 | ||
d81b4bfe TQ |
6895 | -- In a distributed context, null for a remote access to subprogram may |
6896 | -- need to be replaced with a special record aggregate. In this case, | |
6897 | -- return after having done the transformation. | |
996ae0b0 RK |
6898 | |
6899 | if (Ekind (Typ) = E_Record_Type | |
6900 | or else Is_Remote_Access_To_Subprogram_Type (Typ)) | |
6901 | and then Remote_AST_Null_Value (N, Typ) | |
6902 | then | |
6903 | return; | |
6904 | end if; | |
6905 | ||
a77842bd | 6906 | -- The null literal takes its type from the context |
996ae0b0 RK |
6907 | |
6908 | Set_Etype (N, Typ); | |
6909 | end Resolve_Null; | |
6910 | ||
6911 | ----------------------- | |
6912 | -- Resolve_Op_Concat -- | |
6913 | ----------------------- | |
6914 | ||
6915 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is | |
996ae0b0 | 6916 | |
10303118 BD |
6917 | -- We wish to avoid deep recursion, because concatenations are often |
6918 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
6919 | -- operands nonrecursively until we find something that is not a simple | |
6920 | -- concatenation (A in this case). We resolve that, and then walk back | |
6921 | -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest | |
6922 | -- to do the rest of the work at each level. The Parent pointers allow | |
6923 | -- us to avoid recursion, and thus avoid running out of memory. See also | |
d81b4bfe | 6924 | -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used. |
996ae0b0 | 6925 | |
10303118 BD |
6926 | NN : Node_Id := N; |
6927 | Op1 : Node_Id; | |
996ae0b0 | 6928 | |
10303118 BD |
6929 | begin |
6930 | -- The following code is equivalent to: | |
996ae0b0 | 6931 | |
10303118 BD |
6932 | -- Resolve_Op_Concat_First (NN, Typ); |
6933 | -- Resolve_Op_Concat_Arg (N, ...); | |
6934 | -- Resolve_Op_Concat_Rest (N, Typ); | |
996ae0b0 | 6935 | |
10303118 BD |
6936 | -- where the Resolve_Op_Concat_Arg call recurses back here if the left |
6937 | -- operand is a concatenation. | |
996ae0b0 | 6938 | |
10303118 | 6939 | -- Walk down left operands |
996ae0b0 | 6940 | |
10303118 BD |
6941 | loop |
6942 | Resolve_Op_Concat_First (NN, Typ); | |
6943 | Op1 := Left_Opnd (NN); | |
6944 | exit when not (Nkind (Op1) = N_Op_Concat | |
6945 | and then not Is_Array_Type (Component_Type (Typ)) | |
6946 | and then Entity (Op1) = Entity (NN)); | |
6947 | NN := Op1; | |
6948 | end loop; | |
996ae0b0 | 6949 | |
10303118 | 6950 | -- Now (given the above example) NN is A&B and Op1 is A |
996ae0b0 | 6951 | |
10303118 | 6952 | -- First resolve Op1 ... |
9ebe3743 | 6953 | |
10303118 | 6954 | Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN)); |
9ebe3743 | 6955 | |
10303118 BD |
6956 | -- ... then walk NN back up until we reach N (where we started), calling |
6957 | -- Resolve_Op_Concat_Rest along the way. | |
9ebe3743 | 6958 | |
10303118 BD |
6959 | loop |
6960 | Resolve_Op_Concat_Rest (NN, Typ); | |
6961 | exit when NN = N; | |
6962 | NN := Parent (NN); | |
6963 | end loop; | |
6964 | end Resolve_Op_Concat; | |
9ebe3743 | 6965 | |
10303118 BD |
6966 | --------------------------- |
6967 | -- Resolve_Op_Concat_Arg -- | |
6968 | --------------------------- | |
996ae0b0 | 6969 | |
10303118 BD |
6970 | procedure Resolve_Op_Concat_Arg |
6971 | (N : Node_Id; | |
6972 | Arg : Node_Id; | |
6973 | Typ : Entity_Id; | |
6974 | Is_Comp : Boolean) | |
6975 | is | |
6976 | Btyp : constant Entity_Id := Base_Type (Typ); | |
996ae0b0 | 6977 | |
10303118 BD |
6978 | begin |
6979 | if In_Instance then | |
6980 | if Is_Comp | |
6981 | or else (not Is_Overloaded (Arg) | |
6982 | and then Etype (Arg) /= Any_Composite | |
6983 | and then Covers (Component_Type (Typ), Etype (Arg))) | |
6984 | then | |
6985 | Resolve (Arg, Component_Type (Typ)); | |
6986 | else | |
6987 | Resolve (Arg, Btyp); | |
6988 | end if; | |
fbf5a39b | 6989 | |
10303118 BD |
6990 | elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then |
6991 | if Nkind (Arg) = N_Aggregate | |
6992 | and then Is_Composite_Type (Component_Type (Typ)) | |
6993 | then | |
6994 | if Is_Private_Type (Component_Type (Typ)) then | |
6995 | Resolve (Arg, Btyp); | |
6996 | else | |
6997 | Error_Msg_N ("ambiguous aggregate must be qualified", Arg); | |
6998 | Set_Etype (Arg, Any_Type); | |
996ae0b0 RK |
6999 | end if; |
7000 | ||
7001 | else | |
10303118 BD |
7002 | if Is_Overloaded (Arg) |
7003 | and then Has_Compatible_Type (Arg, Typ) | |
7004 | and then Etype (Arg) /= Any_Type | |
7005 | then | |
7006 | declare | |
7007 | I : Interp_Index; | |
7008 | It : Interp; | |
7009 | Func : Entity_Id; | |
7010 | ||
7011 | begin | |
7012 | Get_First_Interp (Arg, I, It); | |
7013 | Func := It.Nam; | |
7014 | Get_Next_Interp (I, It); | |
7015 | ||
7016 | -- Special-case the error message when the overloading is | |
7017 | -- caused by a function that yields an array and can be | |
7018 | -- called without parameters. | |
7019 | ||
7020 | if It.Nam = Func then | |
7021 | Error_Msg_Sloc := Sloc (Func); | |
7022 | Error_Msg_N ("ambiguous call to function#", Arg); | |
7023 | Error_Msg_NE | |
7024 | ("\\interpretation as call yields&", Arg, Typ); | |
7025 | Error_Msg_NE | |
7026 | ("\\interpretation as indexing of call yields&", | |
7027 | Arg, Component_Type (Typ)); | |
7028 | ||
7029 | else | |
7030 | Error_Msg_N | |
7031 | ("ambiguous operand for concatenation!", Arg); | |
7032 | Get_First_Interp (Arg, I, It); | |
7033 | while Present (It.Nam) loop | |
7034 | Error_Msg_Sloc := Sloc (It.Nam); | |
7035 | ||
7036 | if Base_Type (It.Typ) = Base_Type (Typ) | |
7037 | or else Base_Type (It.Typ) = | |
7038 | Base_Type (Component_Type (Typ)) | |
7039 | then | |
4e7a4f6e AC |
7040 | Error_Msg_N -- CODEFIX |
7041 | ("\\possible interpretation#", Arg); | |
10303118 BD |
7042 | end if; |
7043 | ||
7044 | Get_Next_Interp (I, It); | |
7045 | end loop; | |
7046 | end if; | |
7047 | end; | |
7048 | end if; | |
7049 | ||
7050 | Resolve (Arg, Component_Type (Typ)); | |
7051 | ||
7052 | if Nkind (Arg) = N_String_Literal then | |
7053 | Set_Etype (Arg, Component_Type (Typ)); | |
7054 | end if; | |
7055 | ||
7056 | if Arg = Left_Opnd (N) then | |
7057 | Set_Is_Component_Left_Opnd (N); | |
7058 | else | |
7059 | Set_Is_Component_Right_Opnd (N); | |
7060 | end if; | |
996ae0b0 RK |
7061 | end if; |
7062 | ||
10303118 BD |
7063 | else |
7064 | Resolve (Arg, Btyp); | |
7065 | end if; | |
7066 | ||
7067 | Check_Unset_Reference (Arg); | |
7068 | end Resolve_Op_Concat_Arg; | |
996ae0b0 | 7069 | |
10303118 BD |
7070 | ----------------------------- |
7071 | -- Resolve_Op_Concat_First -- | |
7072 | ----------------------------- | |
7073 | ||
7074 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is | |
7075 | Btyp : constant Entity_Id := Base_Type (Typ); | |
7076 | Op1 : constant Node_Id := Left_Opnd (N); | |
7077 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
7078 | |
7079 | begin | |
dae2b8ea HK |
7080 | -- The parser folds an enormous sequence of concatenations of string |
7081 | -- literals into "" & "...", where the Is_Folded_In_Parser flag is set | |
4fc26524 | 7082 | -- in the right operand. If the expression resolves to a predefined "&" |
dae2b8ea HK |
7083 | -- operator, all is well. Otherwise, the parser's folding is wrong, so |
7084 | -- we give an error. See P_Simple_Expression in Par.Ch4. | |
7085 | ||
7086 | if Nkind (Op2) = N_String_Literal | |
7087 | and then Is_Folded_In_Parser (Op2) | |
7088 | and then Ekind (Entity (N)) = E_Function | |
7089 | then | |
7090 | pragma Assert (Nkind (Op1) = N_String_Literal -- should be "" | |
7091 | and then String_Length (Strval (Op1)) = 0); | |
7092 | Error_Msg_N ("too many user-defined concatenations", N); | |
7093 | return; | |
7094 | end if; | |
7095 | ||
996ae0b0 RK |
7096 | Set_Etype (N, Btyp); |
7097 | ||
7098 | if Is_Limited_Composite (Btyp) then | |
7099 | Error_Msg_N ("concatenation not available for limited array", N); | |
fbf5a39b | 7100 | Explain_Limited_Type (Btyp, N); |
996ae0b0 | 7101 | end if; |
10303118 | 7102 | end Resolve_Op_Concat_First; |
996ae0b0 | 7103 | |
10303118 BD |
7104 | ---------------------------- |
7105 | -- Resolve_Op_Concat_Rest -- | |
7106 | ---------------------------- | |
996ae0b0 | 7107 | |
10303118 BD |
7108 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is |
7109 | Op1 : constant Node_Id := Left_Opnd (N); | |
7110 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 | 7111 | |
10303118 BD |
7112 | begin |
7113 | Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N)); | |
996ae0b0 | 7114 | |
fbf5a39b | 7115 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
7116 | |
7117 | if Is_String_Type (Typ) then | |
7118 | Eval_Concatenation (N); | |
7119 | end if; | |
7120 | ||
d81b4bfe TQ |
7121 | -- If this is not a static concatenation, but the result is a string |
7122 | -- type (and not an array of strings) ensure that static string operands | |
7123 | -- have their subtypes properly constructed. | |
996ae0b0 RK |
7124 | |
7125 | if Nkind (N) /= N_String_Literal | |
7126 | and then Is_Character_Type (Component_Type (Typ)) | |
7127 | then | |
7128 | Set_String_Literal_Subtype (Op1, Typ); | |
7129 | Set_String_Literal_Subtype (Op2, Typ); | |
7130 | end if; | |
10303118 | 7131 | end Resolve_Op_Concat_Rest; |
996ae0b0 RK |
7132 | |
7133 | ---------------------- | |
7134 | -- Resolve_Op_Expon -- | |
7135 | ---------------------- | |
7136 | ||
7137 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is | |
7138 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
7139 | ||
7140 | begin | |
f3d57416 | 7141 | -- Catch attempts to do fixed-point exponentiation with universal |
758c442c GD |
7142 | -- operands, which is a case where the illegality is not caught during |
7143 | -- normal operator analysis. | |
996ae0b0 RK |
7144 | |
7145 | if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then | |
7146 | Error_Msg_N ("exponentiation not available for fixed point", N); | |
7147 | return; | |
7148 | end if; | |
7149 | ||
fbf5a39b AC |
7150 | if Comes_From_Source (N) |
7151 | and then Ekind (Entity (N)) = E_Function | |
7152 | and then Is_Imported (Entity (N)) | |
7153 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
7154 | then | |
7155 | Resolve_Intrinsic_Operator (N, Typ); | |
7156 | return; | |
7157 | end if; | |
7158 | ||
996ae0b0 RK |
7159 | if Etype (Left_Opnd (N)) = Universal_Integer |
7160 | or else Etype (Left_Opnd (N)) = Universal_Real | |
7161 | then | |
7162 | Check_For_Visible_Operator (N, B_Typ); | |
7163 | end if; | |
7164 | ||
7165 | -- We do the resolution using the base type, because intermediate values | |
7166 | -- in expressions always are of the base type, not a subtype of it. | |
7167 | ||
7168 | Resolve (Left_Opnd (N), B_Typ); | |
7169 | Resolve (Right_Opnd (N), Standard_Integer); | |
7170 | ||
7171 | Check_Unset_Reference (Left_Opnd (N)); | |
7172 | Check_Unset_Reference (Right_Opnd (N)); | |
7173 | ||
7174 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7175 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7176 | Eval_Op_Expon (N); |
7177 | ||
7178 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
7179 | -- and perhaps the Resolve routines should be separated for the various | |
7180 | -- arithmetic operations, since they will need different processing. ??? | |
7181 | ||
7182 | if Nkind (N) in N_Op then | |
7183 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 7184 | Enable_Overflow_Check (N); |
996ae0b0 RK |
7185 | end if; |
7186 | end if; | |
996ae0b0 RK |
7187 | end Resolve_Op_Expon; |
7188 | ||
7189 | -------------------- | |
7190 | -- Resolve_Op_Not -- | |
7191 | -------------------- | |
7192 | ||
7193 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is | |
7194 | B_Typ : Entity_Id; | |
7195 | ||
7196 | function Parent_Is_Boolean return Boolean; | |
7197 | -- This function determines if the parent node is a boolean operator | |
7198 | -- or operation (comparison op, membership test, or short circuit form) | |
7199 | -- and the not in question is the left operand of this operation. | |
7200 | -- Note that if the not is in parens, then false is returned. | |
7201 | ||
aa180613 RD |
7202 | ----------------------- |
7203 | -- Parent_Is_Boolean -- | |
7204 | ----------------------- | |
7205 | ||
996ae0b0 RK |
7206 | function Parent_Is_Boolean return Boolean is |
7207 | begin | |
7208 | if Paren_Count (N) /= 0 then | |
7209 | return False; | |
7210 | ||
7211 | else | |
7212 | case Nkind (Parent (N)) is | |
7213 | when N_Op_And | | |
7214 | N_Op_Eq | | |
7215 | N_Op_Ge | | |
7216 | N_Op_Gt | | |
7217 | N_Op_Le | | |
7218 | N_Op_Lt | | |
7219 | N_Op_Ne | | |
7220 | N_Op_Or | | |
7221 | N_Op_Xor | | |
7222 | N_In | | |
7223 | N_Not_In | | |
7224 | N_And_Then | | |
aa180613 | 7225 | N_Or_Else => |
996ae0b0 RK |
7226 | |
7227 | return Left_Opnd (Parent (N)) = N; | |
7228 | ||
7229 | when others => | |
7230 | return False; | |
7231 | end case; | |
7232 | end if; | |
7233 | end Parent_Is_Boolean; | |
7234 | ||
7235 | -- Start of processing for Resolve_Op_Not | |
7236 | ||
7237 | begin | |
758c442c GD |
7238 | -- Predefined operations on scalar types yield the base type. On the |
7239 | -- other hand, logical operations on arrays yield the type of the | |
7240 | -- arguments (and the context). | |
996ae0b0 RK |
7241 | |
7242 | if Is_Array_Type (Typ) then | |
7243 | B_Typ := Typ; | |
7244 | else | |
7245 | B_Typ := Base_Type (Typ); | |
7246 | end if; | |
7247 | ||
f3d57416 | 7248 | -- Straightforward case of incorrect arguments |
aa180613 | 7249 | |
996ae0b0 RK |
7250 | if not Valid_Boolean_Arg (Typ) then |
7251 | Error_Msg_N ("invalid operand type for operator&", N); | |
7252 | Set_Etype (N, Any_Type); | |
7253 | return; | |
7254 | ||
aa180613 RD |
7255 | -- Special case of probable missing parens |
7256 | ||
fbf5a39b | 7257 | elsif Typ = Universal_Integer or else Typ = Any_Modular then |
996ae0b0 RK |
7258 | if Parent_Is_Boolean then |
7259 | Error_Msg_N | |
7260 | ("operand of not must be enclosed in parentheses", | |
7261 | Right_Opnd (N)); | |
7262 | else | |
7263 | Error_Msg_N | |
7264 | ("no modular type available in this context", N); | |
7265 | end if; | |
7266 | ||
7267 | Set_Etype (N, Any_Type); | |
7268 | return; | |
7269 | ||
aa180613 RD |
7270 | -- OK resolution of not |
7271 | ||
996ae0b0 | 7272 | else |
aa180613 RD |
7273 | -- Warn if non-boolean types involved. This is a case like not a < b |
7274 | -- where a and b are modular, where we will get (not a) < b and most | |
7275 | -- likely not (a < b) was intended. | |
7276 | ||
7277 | if Warn_On_Questionable_Missing_Parens | |
7278 | and then not Is_Boolean_Type (Typ) | |
996ae0b0 RK |
7279 | and then Parent_Is_Boolean |
7280 | then | |
aa5147f0 | 7281 | Error_Msg_N ("?not expression should be parenthesized here!", N); |
996ae0b0 RK |
7282 | end if; |
7283 | ||
09bc9ab6 RD |
7284 | -- Warn on double negation if checking redundant constructs |
7285 | ||
7286 | if Warn_On_Redundant_Constructs | |
7287 | and then Comes_From_Source (N) | |
7288 | and then Comes_From_Source (Right_Opnd (N)) | |
7289 | and then Root_Type (Typ) = Standard_Boolean | |
7290 | and then Nkind (Right_Opnd (N)) = N_Op_Not | |
7291 | then | |
7292 | Error_Msg_N ("redundant double negation?", N); | |
7293 | end if; | |
7294 | ||
7295 | -- Complete resolution and evaluation of NOT | |
7296 | ||
996ae0b0 RK |
7297 | Resolve (Right_Opnd (N), B_Typ); |
7298 | Check_Unset_Reference (Right_Opnd (N)); | |
7299 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7300 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7301 | Eval_Op_Not (N); |
7302 | end if; | |
7303 | end Resolve_Op_Not; | |
7304 | ||
7305 | ----------------------------- | |
7306 | -- Resolve_Operator_Symbol -- | |
7307 | ----------------------------- | |
7308 | ||
7309 | -- Nothing to be done, all resolved already | |
7310 | ||
7311 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7312 | pragma Warnings (Off, N); |
7313 | pragma Warnings (Off, Typ); | |
7314 | ||
996ae0b0 RK |
7315 | begin |
7316 | null; | |
7317 | end Resolve_Operator_Symbol; | |
7318 | ||
7319 | ---------------------------------- | |
7320 | -- Resolve_Qualified_Expression -- | |
7321 | ---------------------------------- | |
7322 | ||
7323 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7324 | pragma Warnings (Off, Typ); |
7325 | ||
996ae0b0 RK |
7326 | Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N)); |
7327 | Expr : constant Node_Id := Expression (N); | |
7328 | ||
7329 | begin | |
7330 | Resolve (Expr, Target_Typ); | |
7331 | ||
7332 | -- A qualified expression requires an exact match of the type, | |
1420b484 JM |
7333 | -- class-wide matching is not allowed. However, if the qualifying |
7334 | -- type is specific and the expression has a class-wide type, it | |
7335 | -- may still be okay, since it can be the result of the expansion | |
7336 | -- of a call to a dispatching function, so we also have to check | |
7337 | -- class-wideness of the type of the expression's original node. | |
7338 | ||
7339 | if (Is_Class_Wide_Type (Target_Typ) | |
7340 | or else | |
7341 | (Is_Class_Wide_Type (Etype (Expr)) | |
7342 | and then Is_Class_Wide_Type (Etype (Original_Node (Expr))))) | |
996ae0b0 RK |
7343 | and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ) |
7344 | then | |
7345 | Wrong_Type (Expr, Target_Typ); | |
7346 | end if; | |
7347 | ||
7348 | -- If the target type is unconstrained, then we reset the type of | |
7349 | -- the result from the type of the expression. For other cases, the | |
7350 | -- actual subtype of the expression is the target type. | |
7351 | ||
7352 | if Is_Composite_Type (Target_Typ) | |
7353 | and then not Is_Constrained (Target_Typ) | |
7354 | then | |
7355 | Set_Etype (N, Etype (Expr)); | |
7356 | end if; | |
7357 | ||
7358 | Eval_Qualified_Expression (N); | |
7359 | end Resolve_Qualified_Expression; | |
7360 | ||
7361 | ------------------- | |
7362 | -- Resolve_Range -- | |
7363 | ------------------- | |
7364 | ||
7365 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is | |
7366 | L : constant Node_Id := Low_Bound (N); | |
7367 | H : constant Node_Id := High_Bound (N); | |
7368 | ||
7369 | begin | |
7370 | Set_Etype (N, Typ); | |
7371 | Resolve (L, Typ); | |
7372 | Resolve (H, Typ); | |
7373 | ||
7374 | Check_Unset_Reference (L); | |
7375 | Check_Unset_Reference (H); | |
7376 | ||
7377 | -- We have to check the bounds for being within the base range as | |
758c442c GD |
7378 | -- required for a non-static context. Normally this is automatic and |
7379 | -- done as part of evaluating expressions, but the N_Range node is an | |
7380 | -- exception, since in GNAT we consider this node to be a subexpression, | |
7381 | -- even though in Ada it is not. The circuit in Sem_Eval could check for | |
7382 | -- this, but that would put the test on the main evaluation path for | |
7383 | -- expressions. | |
996ae0b0 RK |
7384 | |
7385 | Check_Non_Static_Context (L); | |
7386 | Check_Non_Static_Context (H); | |
7387 | ||
b7d1f17f HK |
7388 | -- Check for an ambiguous range over character literals. This will |
7389 | -- happen with a membership test involving only literals. | |
7390 | ||
7391 | if Typ = Any_Character then | |
7392 | Ambiguous_Character (L); | |
7393 | Set_Etype (N, Any_Type); | |
7394 | return; | |
7395 | end if; | |
7396 | ||
fbf5a39b AC |
7397 | -- If bounds are static, constant-fold them, so size computations |
7398 | -- are identical between front-end and back-end. Do not perform this | |
7399 | -- transformation while analyzing generic units, as type information | |
7400 | -- would then be lost when reanalyzing the constant node in the | |
7401 | -- instance. | |
7402 | ||
7403 | if Is_Discrete_Type (Typ) and then Expander_Active then | |
7404 | if Is_OK_Static_Expression (L) then | |
7405 | Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L)); | |
7406 | end if; | |
7407 | ||
7408 | if Is_OK_Static_Expression (H) then | |
7409 | Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H)); | |
7410 | end if; | |
7411 | end if; | |
996ae0b0 RK |
7412 | end Resolve_Range; |
7413 | ||
7414 | -------------------------- | |
7415 | -- Resolve_Real_Literal -- | |
7416 | -------------------------- | |
7417 | ||
7418 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is | |
7419 | Actual_Typ : constant Entity_Id := Etype (N); | |
7420 | ||
7421 | begin | |
7422 | -- Special processing for fixed-point literals to make sure that the | |
7423 | -- value is an exact multiple of small where this is required. We | |
7424 | -- skip this for the universal real case, and also for generic types. | |
7425 | ||
7426 | if Is_Fixed_Point_Type (Typ) | |
7427 | and then Typ /= Universal_Fixed | |
7428 | and then Typ /= Any_Fixed | |
7429 | and then not Is_Generic_Type (Typ) | |
7430 | then | |
7431 | declare | |
7432 | Val : constant Ureal := Realval (N); | |
7433 | Cintr : constant Ureal := Val / Small_Value (Typ); | |
7434 | Cint : constant Uint := UR_Trunc (Cintr); | |
7435 | Den : constant Uint := Norm_Den (Cintr); | |
7436 | Stat : Boolean; | |
7437 | ||
7438 | begin | |
7439 | -- Case of literal is not an exact multiple of the Small | |
7440 | ||
7441 | if Den /= 1 then | |
7442 | ||
7443 | -- For a source program literal for a decimal fixed-point | |
7444 | -- type, this is statically illegal (RM 4.9(36)). | |
7445 | ||
7446 | if Is_Decimal_Fixed_Point_Type (Typ) | |
7447 | and then Actual_Typ = Universal_Real | |
7448 | and then Comes_From_Source (N) | |
7449 | then | |
7450 | Error_Msg_N ("value has extraneous low order digits", N); | |
7451 | end if; | |
7452 | ||
bc5f3720 RD |
7453 | -- Generate a warning if literal from source |
7454 | ||
7455 | if Is_Static_Expression (N) | |
7456 | and then Warn_On_Bad_Fixed_Value | |
7457 | then | |
7458 | Error_Msg_N | |
aa5147f0 | 7459 | ("?static fixed-point value is not a multiple of Small!", |
bc5f3720 RD |
7460 | N); |
7461 | end if; | |
7462 | ||
996ae0b0 RK |
7463 | -- Replace literal by a value that is the exact representation |
7464 | -- of a value of the type, i.e. a multiple of the small value, | |
7465 | -- by truncation, since Machine_Rounds is false for all GNAT | |
7466 | -- fixed-point types (RM 4.9(38)). | |
7467 | ||
7468 | Stat := Is_Static_Expression (N); | |
7469 | Rewrite (N, | |
7470 | Make_Real_Literal (Sloc (N), | |
7471 | Realval => Small_Value (Typ) * Cint)); | |
7472 | ||
7473 | Set_Is_Static_Expression (N, Stat); | |
7474 | end if; | |
7475 | ||
7476 | -- In all cases, set the corresponding integer field | |
7477 | ||
7478 | Set_Corresponding_Integer_Value (N, Cint); | |
7479 | end; | |
7480 | end if; | |
7481 | ||
7482 | -- Now replace the actual type by the expected type as usual | |
7483 | ||
7484 | Set_Etype (N, Typ); | |
7485 | Eval_Real_Literal (N); | |
7486 | end Resolve_Real_Literal; | |
7487 | ||
7488 | ----------------------- | |
7489 | -- Resolve_Reference -- | |
7490 | ----------------------- | |
7491 | ||
7492 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is | |
7493 | P : constant Node_Id := Prefix (N); | |
7494 | ||
7495 | begin | |
7496 | -- Replace general access with specific type | |
7497 | ||
7498 | if Ekind (Etype (N)) = E_Allocator_Type then | |
7499 | Set_Etype (N, Base_Type (Typ)); | |
7500 | end if; | |
7501 | ||
7502 | Resolve (P, Designated_Type (Etype (N))); | |
7503 | ||
7504 | -- If we are taking the reference of a volatile entity, then treat | |
7505 | -- it as a potential modification of this entity. This is much too | |
638e383e | 7506 | -- conservative, but is necessary because remove side effects can |
996ae0b0 RK |
7507 | -- result in transformations of normal assignments into reference |
7508 | -- sequences that otherwise fail to notice the modification. | |
7509 | ||
fbf5a39b | 7510 | if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then |
45fc7ddb | 7511 | Note_Possible_Modification (P, Sure => False); |
996ae0b0 RK |
7512 | end if; |
7513 | end Resolve_Reference; | |
7514 | ||
7515 | -------------------------------- | |
7516 | -- Resolve_Selected_Component -- | |
7517 | -------------------------------- | |
7518 | ||
7519 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is | |
7520 | Comp : Entity_Id; | |
7521 | Comp1 : Entity_Id := Empty; -- prevent junk warning | |
7522 | P : constant Node_Id := Prefix (N); | |
7523 | S : constant Node_Id := Selector_Name (N); | |
7524 | T : Entity_Id := Etype (P); | |
7525 | I : Interp_Index; | |
7526 | I1 : Interp_Index := 0; -- prevent junk warning | |
7527 | It : Interp; | |
7528 | It1 : Interp; | |
7529 | Found : Boolean; | |
7530 | ||
6510f4c9 GB |
7531 | function Init_Component return Boolean; |
7532 | -- Check whether this is the initialization of a component within an | |
fbf5a39b | 7533 | -- init proc (by assignment or call to another init proc). If true, |
6510f4c9 GB |
7534 | -- there is no need for a discriminant check. |
7535 | ||
7536 | -------------------- | |
7537 | -- Init_Component -- | |
7538 | -------------------- | |
7539 | ||
7540 | function Init_Component return Boolean is | |
7541 | begin | |
7542 | return Inside_Init_Proc | |
7543 | and then Nkind (Prefix (N)) = N_Identifier | |
7544 | and then Chars (Prefix (N)) = Name_uInit | |
7545 | and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative; | |
7546 | end Init_Component; | |
7547 | ||
7548 | -- Start of processing for Resolve_Selected_Component | |
7549 | ||
996ae0b0 RK |
7550 | begin |
7551 | if Is_Overloaded (P) then | |
7552 | ||
7553 | -- Use the context type to select the prefix that has a selector | |
7554 | -- of the correct name and type. | |
7555 | ||
7556 | Found := False; | |
7557 | Get_First_Interp (P, I, It); | |
7558 | ||
7559 | Search : while Present (It.Typ) loop | |
7560 | if Is_Access_Type (It.Typ) then | |
7561 | T := Designated_Type (It.Typ); | |
7562 | else | |
7563 | T := It.Typ; | |
7564 | end if; | |
7565 | ||
7566 | if Is_Record_Type (T) then | |
36fcf362 RD |
7567 | |
7568 | -- The visible components of a class-wide type are those of | |
7569 | -- the root type. | |
7570 | ||
7571 | if Is_Class_Wide_Type (T) then | |
7572 | T := Etype (T); | |
7573 | end if; | |
7574 | ||
996ae0b0 | 7575 | Comp := First_Entity (T); |
996ae0b0 | 7576 | while Present (Comp) loop |
996ae0b0 RK |
7577 | if Chars (Comp) = Chars (S) |
7578 | and then Covers (Etype (Comp), Typ) | |
7579 | then | |
7580 | if not Found then | |
7581 | Found := True; | |
7582 | I1 := I; | |
7583 | It1 := It; | |
7584 | Comp1 := Comp; | |
7585 | ||
7586 | else | |
7587 | It := Disambiguate (P, I1, I, Any_Type); | |
7588 | ||
7589 | if It = No_Interp then | |
7590 | Error_Msg_N | |
7591 | ("ambiguous prefix for selected component", N); | |
7592 | Set_Etype (N, Typ); | |
7593 | return; | |
7594 | ||
7595 | else | |
7596 | It1 := It; | |
7597 | ||
c8ef728f ES |
7598 | -- There may be an implicit dereference. Retrieve |
7599 | -- designated record type. | |
7600 | ||
7601 | if Is_Access_Type (It1.Typ) then | |
7602 | T := Designated_Type (It1.Typ); | |
7603 | else | |
7604 | T := It1.Typ; | |
7605 | end if; | |
7606 | ||
7607 | if Scope (Comp1) /= T then | |
996ae0b0 RK |
7608 | |
7609 | -- Resolution chooses the new interpretation. | |
7610 | -- Find the component with the right name. | |
7611 | ||
c8ef728f | 7612 | Comp1 := First_Entity (T); |
996ae0b0 RK |
7613 | while Present (Comp1) |
7614 | and then Chars (Comp1) /= Chars (S) | |
7615 | loop | |
7616 | Comp1 := Next_Entity (Comp1); | |
7617 | end loop; | |
7618 | end if; | |
7619 | ||
7620 | exit Search; | |
7621 | end if; | |
7622 | end if; | |
7623 | end if; | |
7624 | ||
7625 | Comp := Next_Entity (Comp); | |
7626 | end loop; | |
7627 | ||
7628 | end if; | |
7629 | ||
7630 | Get_Next_Interp (I, It); | |
996ae0b0 RK |
7631 | end loop Search; |
7632 | ||
7633 | Resolve (P, It1.Typ); | |
7634 | Set_Etype (N, Typ); | |
aa180613 | 7635 | Set_Entity_With_Style_Check (S, Comp1); |
996ae0b0 RK |
7636 | |
7637 | else | |
fbf5a39b | 7638 | -- Resolve prefix with its type |
996ae0b0 RK |
7639 | |
7640 | Resolve (P, T); | |
7641 | end if; | |
7642 | ||
aa180613 RD |
7643 | -- Generate cross-reference. We needed to wait until full overloading |
7644 | -- resolution was complete to do this, since otherwise we can't tell if | |
01e17342 | 7645 | -- we are an lvalue or not. |
aa180613 RD |
7646 | |
7647 | if May_Be_Lvalue (N) then | |
7648 | Generate_Reference (Entity (S), S, 'm'); | |
7649 | else | |
7650 | Generate_Reference (Entity (S), S, 'r'); | |
7651 | end if; | |
7652 | ||
c8ef728f ES |
7653 | -- If prefix is an access type, the node will be transformed into an |
7654 | -- explicit dereference during expansion. The type of the node is the | |
7655 | -- designated type of that of the prefix. | |
996ae0b0 RK |
7656 | |
7657 | if Is_Access_Type (Etype (P)) then | |
996ae0b0 | 7658 | T := Designated_Type (Etype (P)); |
c8ef728f | 7659 | Check_Fully_Declared_Prefix (T, P); |
996ae0b0 RK |
7660 | else |
7661 | T := Etype (P); | |
7662 | end if; | |
7663 | ||
7664 | if Has_Discriminants (T) | |
fbf5a39b AC |
7665 | and then (Ekind (Entity (S)) = E_Component |
7666 | or else | |
7667 | Ekind (Entity (S)) = E_Discriminant) | |
996ae0b0 RK |
7668 | and then Present (Original_Record_Component (Entity (S))) |
7669 | and then Ekind (Original_Record_Component (Entity (S))) = E_Component | |
7670 | and then Present (Discriminant_Checking_Func | |
7671 | (Original_Record_Component (Entity (S)))) | |
7672 | and then not Discriminant_Checks_Suppressed (T) | |
6510f4c9 | 7673 | and then not Init_Component |
996ae0b0 RK |
7674 | then |
7675 | Set_Do_Discriminant_Check (N); | |
7676 | end if; | |
7677 | ||
7678 | if Ekind (Entity (S)) = E_Void then | |
7679 | Error_Msg_N ("premature use of component", S); | |
7680 | end if; | |
7681 | ||
7682 | -- If the prefix is a record conversion, this may be a renamed | |
7683 | -- discriminant whose bounds differ from those of the original | |
7684 | -- one, so we must ensure that a range check is performed. | |
7685 | ||
7686 | if Nkind (P) = N_Type_Conversion | |
7687 | and then Ekind (Entity (S)) = E_Discriminant | |
fbf5a39b | 7688 | and then Is_Discrete_Type (Typ) |
996ae0b0 RK |
7689 | then |
7690 | Set_Etype (N, Base_Type (Typ)); | |
7691 | end if; | |
7692 | ||
7693 | -- Note: No Eval processing is required, because the prefix is of a | |
7694 | -- record type, or protected type, and neither can possibly be static. | |
7695 | ||
7696 | end Resolve_Selected_Component; | |
7697 | ||
7698 | ------------------- | |
7699 | -- Resolve_Shift -- | |
7700 | ------------------- | |
7701 | ||
7702 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is | |
7703 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
7704 | L : constant Node_Id := Left_Opnd (N); | |
7705 | R : constant Node_Id := Right_Opnd (N); | |
7706 | ||
7707 | begin | |
7708 | -- We do the resolution using the base type, because intermediate values | |
7709 | -- in expressions always are of the base type, not a subtype of it. | |
7710 | ||
7711 | Resolve (L, B_Typ); | |
7712 | Resolve (R, Standard_Natural); | |
7713 | ||
7714 | Check_Unset_Reference (L); | |
7715 | Check_Unset_Reference (R); | |
7716 | ||
7717 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7718 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7719 | Eval_Shift (N); |
7720 | end Resolve_Shift; | |
7721 | ||
7722 | --------------------------- | |
7723 | -- Resolve_Short_Circuit -- | |
7724 | --------------------------- | |
7725 | ||
7726 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is | |
7727 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
7728 | L : constant Node_Id := Left_Opnd (N); | |
7729 | R : constant Node_Id := Right_Opnd (N); | |
7730 | ||
7731 | begin | |
7732 | Resolve (L, B_Typ); | |
7733 | Resolve (R, B_Typ); | |
7734 | ||
45fc7ddb HK |
7735 | -- Check for issuing warning for always False assert/check, this happens |
7736 | -- when assertions are turned off, in which case the pragma Assert/Check | |
36fcf362 RD |
7737 | -- was transformed into: |
7738 | ||
7739 | -- if False and then <condition> then ... | |
7740 | ||
7741 | -- and we detect this pattern | |
7742 | ||
7743 | if Warn_On_Assertion_Failure | |
7744 | and then Is_Entity_Name (R) | |
7745 | and then Entity (R) = Standard_False | |
7746 | and then Nkind (Parent (N)) = N_If_Statement | |
7747 | and then Nkind (N) = N_And_Then | |
7748 | and then Is_Entity_Name (L) | |
7749 | and then Entity (L) = Standard_False | |
7750 | then | |
7751 | declare | |
7752 | Orig : constant Node_Id := Original_Node (Parent (N)); | |
45fc7ddb | 7753 | |
36fcf362 RD |
7754 | begin |
7755 | if Nkind (Orig) = N_Pragma | |
26570b21 | 7756 | and then Pragma_Name (Orig) = Name_Assert |
36fcf362 RD |
7757 | then |
7758 | -- Don't want to warn if original condition is explicit False | |
7759 | ||
7760 | declare | |
7761 | Expr : constant Node_Id := | |
7762 | Original_Node | |
7763 | (Expression | |
7764 | (First (Pragma_Argument_Associations (Orig)))); | |
7765 | begin | |
7766 | if Is_Entity_Name (Expr) | |
7767 | and then Entity (Expr) = Standard_False | |
7768 | then | |
7769 | null; | |
7770 | else | |
7771 | -- Issue warning. Note that we don't want to make this | |
7772 | -- an unconditional warning, because if the assert is | |
7773 | -- within deleted code we do not want the warning. But | |
7774 | -- we do not want the deletion of the IF/AND-THEN to | |
7775 | -- take this message with it. We achieve this by making | |
7776 | -- sure that the expanded code points to the Sloc of | |
7777 | -- the expression, not the original pragma. | |
7778 | ||
7779 | Error_Msg_N ("?assertion would fail at run-time", Orig); | |
7780 | end if; | |
7781 | end; | |
45fc7ddb HK |
7782 | |
7783 | -- Similar processing for Check pragma | |
7784 | ||
7785 | elsif Nkind (Orig) = N_Pragma | |
7786 | and then Pragma_Name (Orig) = Name_Check | |
7787 | then | |
7788 | -- Don't want to warn if original condition is explicit False | |
7789 | ||
7790 | declare | |
7791 | Expr : constant Node_Id := | |
7792 | Original_Node | |
7793 | (Expression | |
7794 | (Next (First | |
7795 | (Pragma_Argument_Associations (Orig))))); | |
7796 | begin | |
7797 | if Is_Entity_Name (Expr) | |
7798 | and then Entity (Expr) = Standard_False | |
7799 | then | |
7800 | null; | |
7801 | else | |
7802 | Error_Msg_N ("?check would fail at run-time", Orig); | |
7803 | end if; | |
7804 | end; | |
36fcf362 RD |
7805 | end if; |
7806 | end; | |
7807 | end if; | |
7808 | ||
7809 | -- Continue with processing of short circuit | |
7810 | ||
996ae0b0 RK |
7811 | Check_Unset_Reference (L); |
7812 | Check_Unset_Reference (R); | |
7813 | ||
7814 | Set_Etype (N, B_Typ); | |
7815 | Eval_Short_Circuit (N); | |
7816 | end Resolve_Short_Circuit; | |
7817 | ||
7818 | ------------------- | |
7819 | -- Resolve_Slice -- | |
7820 | ------------------- | |
7821 | ||
7822 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is | |
7823 | Name : constant Node_Id := Prefix (N); | |
7824 | Drange : constant Node_Id := Discrete_Range (N); | |
7825 | Array_Type : Entity_Id := Empty; | |
7826 | Index : Node_Id; | |
7827 | ||
7828 | begin | |
7829 | if Is_Overloaded (Name) then | |
7830 | ||
d81b4bfe TQ |
7831 | -- Use the context type to select the prefix that yields the correct |
7832 | -- array type. | |
996ae0b0 RK |
7833 | |
7834 | declare | |
7835 | I : Interp_Index; | |
7836 | I1 : Interp_Index := 0; | |
7837 | It : Interp; | |
7838 | P : constant Node_Id := Prefix (N); | |
7839 | Found : Boolean := False; | |
7840 | ||
7841 | begin | |
7842 | Get_First_Interp (P, I, It); | |
996ae0b0 | 7843 | while Present (It.Typ) loop |
996ae0b0 RK |
7844 | if (Is_Array_Type (It.Typ) |
7845 | and then Covers (Typ, It.Typ)) | |
7846 | or else (Is_Access_Type (It.Typ) | |
7847 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
7848 | and then Covers (Typ, Designated_Type (It.Typ))) | |
7849 | then | |
7850 | if Found then | |
7851 | It := Disambiguate (P, I1, I, Any_Type); | |
7852 | ||
7853 | if It = No_Interp then | |
7854 | Error_Msg_N ("ambiguous prefix for slicing", N); | |
7855 | Set_Etype (N, Typ); | |
7856 | return; | |
7857 | else | |
7858 | Found := True; | |
7859 | Array_Type := It.Typ; | |
7860 | I1 := I; | |
7861 | end if; | |
7862 | else | |
7863 | Found := True; | |
7864 | Array_Type := It.Typ; | |
7865 | I1 := I; | |
7866 | end if; | |
7867 | end if; | |
7868 | ||
7869 | Get_Next_Interp (I, It); | |
7870 | end loop; | |
7871 | end; | |
7872 | ||
7873 | else | |
7874 | Array_Type := Etype (Name); | |
7875 | end if; | |
7876 | ||
7877 | Resolve (Name, Array_Type); | |
7878 | ||
7879 | if Is_Access_Type (Array_Type) then | |
7880 | Apply_Access_Check (N); | |
7881 | Array_Type := Designated_Type (Array_Type); | |
7882 | ||
c8ef728f ES |
7883 | -- If the prefix is an access to an unconstrained array, we must use |
7884 | -- the actual subtype of the object to perform the index checks. The | |
7885 | -- object denoted by the prefix is implicit in the node, so we build | |
7886 | -- an explicit representation for it in order to compute the actual | |
7887 | -- subtype. | |
82c80734 RD |
7888 | |
7889 | if not Is_Constrained (Array_Type) then | |
7890 | Remove_Side_Effects (Prefix (N)); | |
7891 | ||
7892 | declare | |
7893 | Obj : constant Node_Id := | |
7894 | Make_Explicit_Dereference (Sloc (N), | |
7895 | Prefix => New_Copy_Tree (Prefix (N))); | |
7896 | begin | |
7897 | Set_Etype (Obj, Array_Type); | |
7898 | Set_Parent (Obj, Parent (N)); | |
7899 | Array_Type := Get_Actual_Subtype (Obj); | |
7900 | end; | |
7901 | end if; | |
7902 | ||
996ae0b0 RK |
7903 | elsif Is_Entity_Name (Name) |
7904 | or else (Nkind (Name) = N_Function_Call | |
7905 | and then not Is_Constrained (Etype (Name))) | |
7906 | then | |
7907 | Array_Type := Get_Actual_Subtype (Name); | |
aa5147f0 ES |
7908 | |
7909 | -- If the name is a selected component that depends on discriminants, | |
7910 | -- build an actual subtype for it. This can happen only when the name | |
7911 | -- itself is overloaded; otherwise the actual subtype is created when | |
7912 | -- the selected component is analyzed. | |
7913 | ||
7914 | elsif Nkind (Name) = N_Selected_Component | |
7915 | and then Full_Analysis | |
7916 | and then Depends_On_Discriminant (First_Index (Array_Type)) | |
7917 | then | |
7918 | declare | |
7919 | Act_Decl : constant Node_Id := | |
7920 | Build_Actual_Subtype_Of_Component (Array_Type, Name); | |
7921 | begin | |
7922 | Insert_Action (N, Act_Decl); | |
7923 | Array_Type := Defining_Identifier (Act_Decl); | |
7924 | end; | |
d79e621a GD |
7925 | |
7926 | -- Maybe this should just be "else", instead of checking for the | |
7927 | -- specific case of slice??? This is needed for the case where | |
7928 | -- the prefix is an Image attribute, which gets expanded to a | |
7929 | -- slice, and so has a constrained subtype which we want to use | |
7930 | -- for the slice range check applied below (the range check won't | |
7931 | -- get done if the unconstrained subtype of the 'Image is used). | |
7932 | ||
7933 | elsif Nkind (Name) = N_Slice then | |
7934 | Array_Type := Etype (Name); | |
996ae0b0 RK |
7935 | end if; |
7936 | ||
7937 | -- If name was overloaded, set slice type correctly now | |
7938 | ||
7939 | Set_Etype (N, Array_Type); | |
7940 | ||
c8ef728f ES |
7941 | -- If the range is specified by a subtype mark, no resolution is |
7942 | -- necessary. Else resolve the bounds, and apply needed checks. | |
996ae0b0 RK |
7943 | |
7944 | if not Is_Entity_Name (Drange) then | |
7945 | Index := First_Index (Array_Type); | |
7946 | Resolve (Drange, Base_Type (Etype (Index))); | |
7947 | ||
0669bebe GB |
7948 | if Nkind (Drange) = N_Range |
7949 | ||
7950 | -- Do not apply the range check to nodes associated with the | |
7951 | -- frontend expansion of the dispatch table. We first check | |
7952 | -- if Ada.Tags is already loaded to void the addition of an | |
7953 | -- undesired dependence on such run-time unit. | |
7954 | ||
b7d1f17f | 7955 | and then |
1f110335 AC |
7956 | (not Tagged_Type_Expansion |
7957 | or else not | |
7958 | (RTU_Loaded (Ada_Tags) | |
7959 | and then Nkind (Prefix (N)) = N_Selected_Component | |
7960 | and then Present (Entity (Selector_Name (Prefix (N)))) | |
7961 | and then Entity (Selector_Name (Prefix (N))) = | |
7962 | RTE_Record_Component (RE_Prims_Ptr))) | |
0669bebe | 7963 | then |
996ae0b0 RK |
7964 | Apply_Range_Check (Drange, Etype (Index)); |
7965 | end if; | |
7966 | end if; | |
7967 | ||
7968 | Set_Slice_Subtype (N); | |
aa180613 RD |
7969 | |
7970 | if Nkind (Drange) = N_Range then | |
7971 | Warn_On_Suspicious_Index (Name, Low_Bound (Drange)); | |
7972 | Warn_On_Suspicious_Index (Name, High_Bound (Drange)); | |
7973 | end if; | |
7974 | ||
996ae0b0 | 7975 | Eval_Slice (N); |
996ae0b0 RK |
7976 | end Resolve_Slice; |
7977 | ||
7978 | ---------------------------- | |
7979 | -- Resolve_String_Literal -- | |
7980 | ---------------------------- | |
7981 | ||
7982 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is | |
7983 | C_Typ : constant Entity_Id := Component_Type (Typ); | |
7984 | R_Typ : constant Entity_Id := Root_Type (C_Typ); | |
7985 | Loc : constant Source_Ptr := Sloc (N); | |
7986 | Str : constant String_Id := Strval (N); | |
7987 | Strlen : constant Nat := String_Length (Str); | |
7988 | Subtype_Id : Entity_Id; | |
7989 | Need_Check : Boolean; | |
7990 | ||
7991 | begin | |
7992 | -- For a string appearing in a concatenation, defer creation of the | |
7993 | -- string_literal_subtype until the end of the resolution of the | |
c8ef728f ES |
7994 | -- concatenation, because the literal may be constant-folded away. This |
7995 | -- is a useful optimization for long concatenation expressions. | |
996ae0b0 | 7996 | |
c8ef728f | 7997 | -- If the string is an aggregate built for a single character (which |
996ae0b0 | 7998 | -- happens in a non-static context) or a is null string to which special |
c8ef728f ES |
7999 | -- checks may apply, we build the subtype. Wide strings must also get a |
8000 | -- string subtype if they come from a one character aggregate. Strings | |
996ae0b0 RK |
8001 | -- generated by attributes might be static, but it is often hard to |
8002 | -- determine whether the enclosing context is static, so we generate | |
8003 | -- subtypes for them as well, thus losing some rarer optimizations ??? | |
8004 | -- Same for strings that come from a static conversion. | |
8005 | ||
8006 | Need_Check := | |
8007 | (Strlen = 0 and then Typ /= Standard_String) | |
8008 | or else Nkind (Parent (N)) /= N_Op_Concat | |
8009 | or else (N /= Left_Opnd (Parent (N)) | |
8010 | and then N /= Right_Opnd (Parent (N))) | |
82c80734 RD |
8011 | or else ((Typ = Standard_Wide_String |
8012 | or else Typ = Standard_Wide_Wide_String) | |
996ae0b0 RK |
8013 | and then Nkind (Original_Node (N)) /= N_String_Literal); |
8014 | ||
d81b4bfe TQ |
8015 | -- If the resolving type is itself a string literal subtype, we can just |
8016 | -- reuse it, since there is no point in creating another. | |
996ae0b0 RK |
8017 | |
8018 | if Ekind (Typ) = E_String_Literal_Subtype then | |
8019 | Subtype_Id := Typ; | |
8020 | ||
8021 | elsif Nkind (Parent (N)) = N_Op_Concat | |
8022 | and then not Need_Check | |
45fc7ddb HK |
8023 | and then not Nkind_In (Original_Node (N), N_Character_Literal, |
8024 | N_Attribute_Reference, | |
8025 | N_Qualified_Expression, | |
8026 | N_Type_Conversion) | |
996ae0b0 RK |
8027 | then |
8028 | Subtype_Id := Typ; | |
8029 | ||
8030 | -- Otherwise we must create a string literal subtype. Note that the | |
8031 | -- whole idea of string literal subtypes is simply to avoid the need | |
8032 | -- for building a full fledged array subtype for each literal. | |
45fc7ddb | 8033 | |
996ae0b0 RK |
8034 | else |
8035 | Set_String_Literal_Subtype (N, Typ); | |
8036 | Subtype_Id := Etype (N); | |
8037 | end if; | |
8038 | ||
8039 | if Nkind (Parent (N)) /= N_Op_Concat | |
8040 | or else Need_Check | |
8041 | then | |
8042 | Set_Etype (N, Subtype_Id); | |
8043 | Eval_String_Literal (N); | |
8044 | end if; | |
8045 | ||
8046 | if Is_Limited_Composite (Typ) | |
8047 | or else Is_Private_Composite (Typ) | |
8048 | then | |
8049 | Error_Msg_N ("string literal not available for private array", N); | |
8050 | Set_Etype (N, Any_Type); | |
8051 | return; | |
8052 | end if; | |
8053 | ||
d81b4bfe TQ |
8054 | -- The validity of a null string has been checked in the call to |
8055 | -- Eval_String_Literal. | |
996ae0b0 RK |
8056 | |
8057 | if Strlen = 0 then | |
8058 | return; | |
8059 | ||
c8ef728f ES |
8060 | -- Always accept string literal with component type Any_Character, which |
8061 | -- occurs in error situations and in comparisons of literals, both of | |
8062 | -- which should accept all literals. | |
996ae0b0 RK |
8063 | |
8064 | elsif R_Typ = Any_Character then | |
8065 | return; | |
8066 | ||
f3d57416 RW |
8067 | -- If the type is bit-packed, then we always transform the string |
8068 | -- literal into a full fledged aggregate. | |
996ae0b0 RK |
8069 | |
8070 | elsif Is_Bit_Packed_Array (Typ) then | |
8071 | null; | |
8072 | ||
82c80734 | 8073 | -- Deal with cases of Wide_Wide_String, Wide_String, and String |
996ae0b0 RK |
8074 | |
8075 | else | |
82c80734 RD |
8076 | -- For Standard.Wide_Wide_String, or any other type whose component |
8077 | -- type is Standard.Wide_Wide_Character, we know that all the | |
996ae0b0 RK |
8078 | -- characters in the string must be acceptable, since the parser |
8079 | -- accepted the characters as valid character literals. | |
8080 | ||
82c80734 | 8081 | if R_Typ = Standard_Wide_Wide_Character then |
996ae0b0 RK |
8082 | null; |
8083 | ||
c8ef728f ES |
8084 | -- For the case of Standard.String, or any other type whose component |
8085 | -- type is Standard.Character, we must make sure that there are no | |
8086 | -- wide characters in the string, i.e. that it is entirely composed | |
8087 | -- of characters in range of type Character. | |
996ae0b0 | 8088 | |
c8ef728f ES |
8089 | -- If the string literal is the result of a static concatenation, the |
8090 | -- test has already been performed on the components, and need not be | |
8091 | -- repeated. | |
996ae0b0 RK |
8092 | |
8093 | elsif R_Typ = Standard_Character | |
8094 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8095 | then | |
8096 | for J in 1 .. Strlen loop | |
8097 | if not In_Character_Range (Get_String_Char (Str, J)) then | |
8098 | ||
8099 | -- If we are out of range, post error. This is one of the | |
8100 | -- very few places that we place the flag in the middle of | |
d81b4bfe TQ |
8101 | -- a token, right under the offending wide character. Not |
8102 | -- quite clear if this is right wrt wide character encoding | |
8103 | -- sequences, but it's only an error message! | |
996ae0b0 RK |
8104 | |
8105 | Error_Msg | |
82c80734 RD |
8106 | ("literal out of range of type Standard.Character", |
8107 | Source_Ptr (Int (Loc) + J)); | |
8108 | return; | |
8109 | end if; | |
8110 | end loop; | |
8111 | ||
8112 | -- For the case of Standard.Wide_String, or any other type whose | |
8113 | -- component type is Standard.Wide_Character, we must make sure that | |
8114 | -- there are no wide characters in the string, i.e. that it is | |
8115 | -- entirely composed of characters in range of type Wide_Character. | |
8116 | ||
8117 | -- If the string literal is the result of a static concatenation, | |
8118 | -- the test has already been performed on the components, and need | |
8119 | -- not be repeated. | |
8120 | ||
8121 | elsif R_Typ = Standard_Wide_Character | |
8122 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8123 | then | |
8124 | for J in 1 .. Strlen loop | |
8125 | if not In_Wide_Character_Range (Get_String_Char (Str, J)) then | |
8126 | ||
8127 | -- If we are out of range, post error. This is one of the | |
8128 | -- very few places that we place the flag in the middle of | |
8129 | -- a token, right under the offending wide character. | |
8130 | ||
8131 | -- This is not quite right, because characters in general | |
8132 | -- will take more than one character position ??? | |
8133 | ||
8134 | Error_Msg | |
8135 | ("literal out of range of type Standard.Wide_Character", | |
996ae0b0 RK |
8136 | Source_Ptr (Int (Loc) + J)); |
8137 | return; | |
8138 | end if; | |
8139 | end loop; | |
8140 | ||
8141 | -- If the root type is not a standard character, then we will convert | |
8142 | -- the string into an aggregate and will let the aggregate code do | |
82c80734 | 8143 | -- the checking. Standard Wide_Wide_Character is also OK here. |
996ae0b0 RK |
8144 | |
8145 | else | |
8146 | null; | |
996ae0b0 RK |
8147 | end if; |
8148 | ||
c8ef728f ES |
8149 | -- See if the component type of the array corresponding to the string |
8150 | -- has compile time known bounds. If yes we can directly check | |
8151 | -- whether the evaluation of the string will raise constraint error. | |
8152 | -- Otherwise we need to transform the string literal into the | |
8153 | -- corresponding character aggregate and let the aggregate | |
996ae0b0 RK |
8154 | -- code do the checking. |
8155 | ||
45fc7ddb HK |
8156 | if Is_Standard_Character_Type (R_Typ) then |
8157 | ||
996ae0b0 RK |
8158 | -- Check for the case of full range, where we are definitely OK |
8159 | ||
8160 | if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then | |
8161 | return; | |
8162 | end if; | |
8163 | ||
8164 | -- Here the range is not the complete base type range, so check | |
8165 | ||
8166 | declare | |
8167 | Comp_Typ_Lo : constant Node_Id := | |
8168 | Type_Low_Bound (Component_Type (Typ)); | |
8169 | Comp_Typ_Hi : constant Node_Id := | |
8170 | Type_High_Bound (Component_Type (Typ)); | |
8171 | ||
8172 | Char_Val : Uint; | |
8173 | ||
8174 | begin | |
8175 | if Compile_Time_Known_Value (Comp_Typ_Lo) | |
8176 | and then Compile_Time_Known_Value (Comp_Typ_Hi) | |
8177 | then | |
8178 | for J in 1 .. Strlen loop | |
8179 | Char_Val := UI_From_Int (Int (Get_String_Char (Str, J))); | |
8180 | ||
8181 | if Char_Val < Expr_Value (Comp_Typ_Lo) | |
8182 | or else Char_Val > Expr_Value (Comp_Typ_Hi) | |
8183 | then | |
8184 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 8185 | (N, "character out of range?", CE_Range_Check_Failed, |
996ae0b0 RK |
8186 | Loc => Source_Ptr (Int (Loc) + J)); |
8187 | end if; | |
8188 | end loop; | |
8189 | ||
8190 | return; | |
8191 | end if; | |
8192 | end; | |
8193 | end if; | |
8194 | end if; | |
8195 | ||
8196 | -- If we got here we meed to transform the string literal into the | |
8197 | -- equivalent qualified positional array aggregate. This is rather | |
8198 | -- heavy artillery for this situation, but it is hard work to avoid. | |
8199 | ||
8200 | declare | |
fbf5a39b | 8201 | Lits : constant List_Id := New_List; |
996ae0b0 RK |
8202 | P : Source_Ptr := Loc + 1; |
8203 | C : Char_Code; | |
8204 | ||
8205 | begin | |
c8ef728f ES |
8206 | -- Build the character literals, we give them source locations that |
8207 | -- correspond to the string positions, which is a bit tricky given | |
8208 | -- the possible presence of wide character escape sequences. | |
996ae0b0 RK |
8209 | |
8210 | for J in 1 .. Strlen loop | |
8211 | C := Get_String_Char (Str, J); | |
8212 | Set_Character_Literal_Name (C); | |
8213 | ||
8214 | Append_To (Lits, | |
82c80734 RD |
8215 | Make_Character_Literal (P, |
8216 | Chars => Name_Find, | |
8217 | Char_Literal_Value => UI_From_CC (C))); | |
996ae0b0 RK |
8218 | |
8219 | if In_Character_Range (C) then | |
8220 | P := P + 1; | |
8221 | ||
8222 | -- Should we have a call to Skip_Wide here ??? | |
8223 | -- ??? else | |
8224 | -- Skip_Wide (P); | |
8225 | ||
8226 | end if; | |
8227 | end loop; | |
8228 | ||
8229 | Rewrite (N, | |
8230 | Make_Qualified_Expression (Loc, | |
8231 | Subtype_Mark => New_Reference_To (Typ, Loc), | |
8232 | Expression => | |
8233 | Make_Aggregate (Loc, Expressions => Lits))); | |
8234 | ||
8235 | Analyze_And_Resolve (N, Typ); | |
8236 | end; | |
8237 | end Resolve_String_Literal; | |
8238 | ||
8239 | ----------------------------- | |
8240 | -- Resolve_Subprogram_Info -- | |
8241 | ----------------------------- | |
8242 | ||
8243 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is | |
8244 | begin | |
8245 | Set_Etype (N, Typ); | |
8246 | end Resolve_Subprogram_Info; | |
8247 | ||
8248 | ----------------------------- | |
8249 | -- Resolve_Type_Conversion -- | |
8250 | ----------------------------- | |
8251 | ||
8252 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is | |
758c442c | 8253 | Conv_OK : constant Boolean := Conversion_OK (N); |
b7d1f17f HK |
8254 | Operand : constant Node_Id := Expression (N); |
8255 | Operand_Typ : constant Entity_Id := Etype (Operand); | |
8256 | Target_Typ : constant Entity_Id := Etype (N); | |
996ae0b0 | 8257 | Rop : Node_Id; |
fbf5a39b AC |
8258 | Orig_N : Node_Id; |
8259 | Orig_T : Node_Id; | |
996ae0b0 RK |
8260 | |
8261 | begin | |
996ae0b0 | 8262 | if not Conv_OK |
b7d1f17f | 8263 | and then not Valid_Conversion (N, Target_Typ, Operand) |
996ae0b0 RK |
8264 | then |
8265 | return; | |
8266 | end if; | |
8267 | ||
8268 | if Etype (Operand) = Any_Fixed then | |
8269 | ||
8270 | -- Mixed-mode operation involving a literal. Context must be a fixed | |
8271 | -- type which is applied to the literal subsequently. | |
8272 | ||
8273 | if Is_Fixed_Point_Type (Typ) then | |
8274 | Set_Etype (Operand, Universal_Real); | |
8275 | ||
8276 | elsif Is_Numeric_Type (Typ) | |
45fc7ddb | 8277 | and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide) |
996ae0b0 | 8278 | and then (Etype (Right_Opnd (Operand)) = Universal_Real |
45fc7ddb HK |
8279 | or else |
8280 | Etype (Left_Opnd (Operand)) = Universal_Real) | |
996ae0b0 | 8281 | then |
a77842bd TQ |
8282 | -- Return if expression is ambiguous |
8283 | ||
996ae0b0 | 8284 | if Unique_Fixed_Point_Type (N) = Any_Type then |
a77842bd | 8285 | return; |
82c80734 | 8286 | |
a77842bd TQ |
8287 | -- If nothing else, the available fixed type is Duration |
8288 | ||
8289 | else | |
996ae0b0 RK |
8290 | Set_Etype (Operand, Standard_Duration); |
8291 | end if; | |
8292 | ||
bc5f3720 | 8293 | -- Resolve the real operand with largest available precision |
9ebe3743 | 8294 | |
996ae0b0 RK |
8295 | if Etype (Right_Opnd (Operand)) = Universal_Real then |
8296 | Rop := New_Copy_Tree (Right_Opnd (Operand)); | |
8297 | else | |
8298 | Rop := New_Copy_Tree (Left_Opnd (Operand)); | |
8299 | end if; | |
8300 | ||
9ebe3743 | 8301 | Resolve (Rop, Universal_Real); |
996ae0b0 | 8302 | |
82c80734 RD |
8303 | -- If the operand is a literal (it could be a non-static and |
8304 | -- illegal exponentiation) check whether the use of Duration | |
8305 | -- is potentially inaccurate. | |
8306 | ||
8307 | if Nkind (Rop) = N_Real_Literal | |
8308 | and then Realval (Rop) /= Ureal_0 | |
996ae0b0 RK |
8309 | and then abs (Realval (Rop)) < Delta_Value (Standard_Duration) |
8310 | then | |
aa180613 | 8311 | Error_Msg_N |
aa5147f0 ES |
8312 | ("?universal real operand can only " & |
8313 | "be interpreted as Duration!", | |
aa180613 RD |
8314 | Rop); |
8315 | Error_Msg_N | |
aa5147f0 | 8316 | ("\?precision will be lost in the conversion!", Rop); |
996ae0b0 RK |
8317 | end if; |
8318 | ||
891a6e79 AC |
8319 | elsif Is_Numeric_Type (Typ) |
8320 | and then Nkind (Operand) in N_Op | |
8321 | and then Unique_Fixed_Point_Type (N) /= Any_Type | |
8322 | then | |
8323 | Set_Etype (Operand, Standard_Duration); | |
8324 | ||
996ae0b0 RK |
8325 | else |
8326 | Error_Msg_N ("invalid context for mixed mode operation", N); | |
8327 | Set_Etype (Operand, Any_Type); | |
8328 | return; | |
8329 | end if; | |
8330 | end if; | |
8331 | ||
fbf5a39b | 8332 | Resolve (Operand); |
996ae0b0 RK |
8333 | |
8334 | -- Note: we do the Eval_Type_Conversion call before applying the | |
d81b4bfe TQ |
8335 | -- required checks for a subtype conversion. This is important, since |
8336 | -- both are prepared under certain circumstances to change the type | |
8337 | -- conversion to a constraint error node, but in the case of | |
8338 | -- Eval_Type_Conversion this may reflect an illegality in the static | |
8339 | -- case, and we would miss the illegality (getting only a warning | |
8340 | -- message), if we applied the type conversion checks first. | |
996ae0b0 RK |
8341 | |
8342 | Eval_Type_Conversion (N); | |
8343 | ||
d81b4bfe TQ |
8344 | -- Even when evaluation is not possible, we may be able to simplify the |
8345 | -- conversion or its expression. This needs to be done before applying | |
8346 | -- checks, since otherwise the checks may use the original expression | |
8347 | -- and defeat the simplifications. This is specifically the case for | |
8348 | -- elimination of the floating-point Truncation attribute in | |
8349 | -- float-to-int conversions. | |
0669bebe GB |
8350 | |
8351 | Simplify_Type_Conversion (N); | |
8352 | ||
d81b4bfe TQ |
8353 | -- If after evaluation we still have a type conversion, then we may need |
8354 | -- to apply checks required for a subtype conversion. | |
996ae0b0 RK |
8355 | |
8356 | -- Skip these type conversion checks if universal fixed operands | |
8357 | -- operands involved, since range checks are handled separately for | |
8358 | -- these cases (in the appropriate Expand routines in unit Exp_Fixd). | |
8359 | ||
8360 | if Nkind (N) = N_Type_Conversion | |
b7d1f17f HK |
8361 | and then not Is_Generic_Type (Root_Type (Target_Typ)) |
8362 | and then Target_Typ /= Universal_Fixed | |
8363 | and then Operand_Typ /= Universal_Fixed | |
996ae0b0 RK |
8364 | then |
8365 | Apply_Type_Conversion_Checks (N); | |
8366 | end if; | |
8367 | ||
d81b4bfe TQ |
8368 | -- Issue warning for conversion of simple object to its own type. We |
8369 | -- have to test the original nodes, since they may have been rewritten | |
8370 | -- by various optimizations. | |
fbf5a39b AC |
8371 | |
8372 | Orig_N := Original_Node (N); | |
996ae0b0 RK |
8373 | |
8374 | if Warn_On_Redundant_Constructs | |
fbf5a39b AC |
8375 | and then Comes_From_Source (Orig_N) |
8376 | and then Nkind (Orig_N) = N_Type_Conversion | |
5453d5bd | 8377 | and then not In_Instance |
996ae0b0 | 8378 | then |
fbf5a39b | 8379 | Orig_N := Original_Node (Expression (Orig_N)); |
b7d1f17f | 8380 | Orig_T := Target_Typ; |
fbf5a39b AC |
8381 | |
8382 | -- If the node is part of a larger expression, the Target_Type | |
8383 | -- may not be the original type of the node if the context is a | |
8384 | -- condition. Recover original type to see if conversion is needed. | |
8385 | ||
8386 | if Is_Boolean_Type (Orig_T) | |
8387 | and then Nkind (Parent (N)) in N_Op | |
8388 | then | |
8389 | Orig_T := Etype (Parent (N)); | |
8390 | end if; | |
8391 | ||
8392 | if Is_Entity_Name (Orig_N) | |
b90cfacd HK |
8393 | and then |
8394 | (Etype (Entity (Orig_N)) = Orig_T | |
8395 | or else | |
8396 | (Ekind (Entity (Orig_N)) = E_Loop_Parameter | |
8397 | and then Covers (Orig_T, Etype (Entity (Orig_N))))) | |
fbf5a39b | 8398 | then |
b90cfacd | 8399 | Error_Msg_Node_2 := Orig_T; |
483c78cb | 8400 | Error_Msg_NE -- CODEFIX |
b90cfacd | 8401 | ("?redundant conversion, & is of type &!", N, Entity (Orig_N)); |
fbf5a39b | 8402 | end if; |
996ae0b0 | 8403 | end if; |
758c442c | 8404 | |
b7d1f17f | 8405 | -- Ada 2005 (AI-251): Handle class-wide interface type conversions. |
0669bebe GB |
8406 | -- No need to perform any interface conversion if the type of the |
8407 | -- expression coincides with the target type. | |
758c442c | 8408 | |
0669bebe GB |
8409 | if Ada_Version >= Ada_05 |
8410 | and then Expander_Active | |
b7d1f17f | 8411 | and then Operand_Typ /= Target_Typ |
0669bebe | 8412 | then |
b7d1f17f HK |
8413 | declare |
8414 | Opnd : Entity_Id := Operand_Typ; | |
8415 | Target : Entity_Id := Target_Typ; | |
758c442c | 8416 | |
b7d1f17f HK |
8417 | begin |
8418 | if Is_Access_Type (Opnd) then | |
8419 | Opnd := Directly_Designated_Type (Opnd); | |
1420b484 JM |
8420 | end if; |
8421 | ||
b7d1f17f HK |
8422 | if Is_Access_Type (Target_Typ) then |
8423 | Target := Directly_Designated_Type (Target); | |
4197ae1e | 8424 | end if; |
c8ef728f | 8425 | |
b7d1f17f HK |
8426 | if Opnd = Target then |
8427 | null; | |
c8ef728f | 8428 | |
b7d1f17f | 8429 | -- Conversion from interface type |
ea985d95 | 8430 | |
b7d1f17f | 8431 | elsif Is_Interface (Opnd) then |
ea985d95 | 8432 | |
b7d1f17f | 8433 | -- Ada 2005 (AI-217): Handle entities from limited views |
aa180613 | 8434 | |
b7d1f17f HK |
8435 | if From_With_Type (Opnd) then |
8436 | Error_Msg_Qual_Level := 99; | |
c72a85f2 | 8437 | Error_Msg_NE ("missing WITH clause on package &", N, |
b7d1f17f HK |
8438 | Cunit_Entity (Get_Source_Unit (Base_Type (Opnd)))); |
8439 | Error_Msg_N | |
8440 | ("type conversions require visibility of the full view", | |
8441 | N); | |
aa180613 | 8442 | |
aa5147f0 ES |
8443 | elsif From_With_Type (Target) |
8444 | and then not | |
8445 | (Is_Access_Type (Target_Typ) | |
8446 | and then Present (Non_Limited_View (Etype (Target)))) | |
8447 | then | |
b7d1f17f | 8448 | Error_Msg_Qual_Level := 99; |
c72a85f2 | 8449 | Error_Msg_NE ("missing WITH clause on package &", N, |
b7d1f17f HK |
8450 | Cunit_Entity (Get_Source_Unit (Base_Type (Target)))); |
8451 | Error_Msg_N | |
8452 | ("type conversions require visibility of the full view", | |
8453 | N); | |
aa180613 | 8454 | |
b7d1f17f HK |
8455 | else |
8456 | Expand_Interface_Conversion (N, Is_Static => False); | |
8457 | end if; | |
8458 | ||
8459 | -- Conversion to interface type | |
8460 | ||
8461 | elsif Is_Interface (Target) then | |
8462 | ||
8463 | -- Handle subtypes | |
8464 | ||
8465 | if Ekind (Opnd) = E_Protected_Subtype | |
8466 | or else Ekind (Opnd) = E_Task_Subtype | |
8467 | then | |
8468 | Opnd := Etype (Opnd); | |
8469 | end if; | |
8470 | ||
8471 | if not Interface_Present_In_Ancestor | |
8472 | (Typ => Opnd, | |
8473 | Iface => Target) | |
8474 | then | |
8475 | if Is_Class_Wide_Type (Opnd) then | |
8476 | ||
8477 | -- The static analysis is not enough to know if the | |
8478 | -- interface is implemented or not. Hence we must pass | |
8479 | -- the work to the expander to generate code to evaluate | |
8480 | -- the conversion at run-time. | |
8481 | ||
8482 | Expand_Interface_Conversion (N, Is_Static => False); | |
8483 | ||
8484 | else | |
8485 | Error_Msg_Name_1 := Chars (Etype (Target)); | |
8486 | Error_Msg_Name_2 := Chars (Opnd); | |
8487 | Error_Msg_N | |
8488 | ("wrong interface conversion (% is not a progenitor " & | |
8489 | "of %)", N); | |
8490 | end if; | |
8491 | ||
8492 | else | |
8493 | Expand_Interface_Conversion (N); | |
8494 | end if; | |
8495 | end if; | |
8496 | end; | |
758c442c | 8497 | end if; |
996ae0b0 RK |
8498 | end Resolve_Type_Conversion; |
8499 | ||
8500 | ---------------------- | |
8501 | -- Resolve_Unary_Op -- | |
8502 | ---------------------- | |
8503 | ||
8504 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
8505 | B_Typ : constant Entity_Id := Base_Type (Typ); |
8506 | R : constant Node_Id := Right_Opnd (N); | |
8507 | OK : Boolean; | |
8508 | Lo : Uint; | |
8509 | Hi : Uint; | |
996ae0b0 RK |
8510 | |
8511 | begin | |
b7d1f17f | 8512 | -- Deal with intrinsic unary operators |
996ae0b0 | 8513 | |
fbf5a39b AC |
8514 | if Comes_From_Source (N) |
8515 | and then Ekind (Entity (N)) = E_Function | |
8516 | and then Is_Imported (Entity (N)) | |
8517 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
8518 | then | |
8519 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
8520 | return; | |
8521 | end if; | |
8522 | ||
0669bebe GB |
8523 | -- Deal with universal cases |
8524 | ||
996ae0b0 | 8525 | if Etype (R) = Universal_Integer |
0669bebe GB |
8526 | or else |
8527 | Etype (R) = Universal_Real | |
996ae0b0 RK |
8528 | then |
8529 | Check_For_Visible_Operator (N, B_Typ); | |
8530 | end if; | |
8531 | ||
8532 | Set_Etype (N, B_Typ); | |
8533 | Resolve (R, B_Typ); | |
fbf5a39b | 8534 | |
9ebe3743 HK |
8535 | -- Generate warning for expressions like abs (x mod 2) |
8536 | ||
8537 | if Warn_On_Redundant_Constructs | |
8538 | and then Nkind (N) = N_Op_Abs | |
8539 | then | |
8540 | Determine_Range (Right_Opnd (N), OK, Lo, Hi); | |
8541 | ||
8542 | if OK and then Hi >= Lo and then Lo >= 0 then | |
8543 | Error_Msg_N | |
8544 | ("?abs applied to known non-negative value has no effect", N); | |
8545 | end if; | |
8546 | end if; | |
8547 | ||
0669bebe GB |
8548 | -- Deal with reference generation |
8549 | ||
996ae0b0 | 8550 | Check_Unset_Reference (R); |
fbf5a39b | 8551 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
8552 | Eval_Unary_Op (N); |
8553 | ||
8554 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
8555 | -- and perhaps the Resolve routines should be separated for the various | |
8556 | -- arithmetic operations, since they will need different processing ??? | |
8557 | ||
8558 | if Nkind (N) in N_Op then | |
8559 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 8560 | Enable_Overflow_Check (N); |
996ae0b0 RK |
8561 | end if; |
8562 | end if; | |
0669bebe | 8563 | |
d81b4bfe TQ |
8564 | -- Generate warning for expressions like -5 mod 3 for integers. No need |
8565 | -- to worry in the floating-point case, since parens do not affect the | |
8566 | -- result so there is no point in giving in a warning. | |
0669bebe GB |
8567 | |
8568 | declare | |
8569 | Norig : constant Node_Id := Original_Node (N); | |
8570 | Rorig : Node_Id; | |
8571 | Val : Uint; | |
8572 | HB : Uint; | |
8573 | LB : Uint; | |
8574 | Lval : Uint; | |
8575 | Opnd : Node_Id; | |
8576 | ||
8577 | begin | |
8578 | if Warn_On_Questionable_Missing_Parens | |
8579 | and then Comes_From_Source (Norig) | |
8580 | and then Is_Integer_Type (Typ) | |
8581 | and then Nkind (Norig) = N_Op_Minus | |
8582 | then | |
8583 | Rorig := Original_Node (Right_Opnd (Norig)); | |
8584 | ||
8585 | -- We are looking for cases where the right operand is not | |
f3d57416 | 8586 | -- parenthesized, and is a binary operator, multiply, divide, or |
0669bebe GB |
8587 | -- mod. These are the cases where the grouping can affect results. |
8588 | ||
8589 | if Paren_Count (Rorig) = 0 | |
45fc7ddb | 8590 | and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide) |
0669bebe GB |
8591 | then |
8592 | -- For mod, we always give the warning, since the value is | |
8593 | -- affected by the parenthesization (e.g. (-5) mod 315 /= | |
d81b4bfe | 8594 | -- -(5 mod 315)). But for the other cases, the only concern is |
0669bebe GB |
8595 | -- overflow, e.g. for the case of 8 big signed (-(2 * 64) |
8596 | -- overflows, but (-2) * 64 does not). So we try to give the | |
8597 | -- message only when overflow is possible. | |
8598 | ||
8599 | if Nkind (Rorig) /= N_Op_Mod | |
8600 | and then Compile_Time_Known_Value (R) | |
8601 | then | |
8602 | Val := Expr_Value (R); | |
8603 | ||
8604 | if Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
8605 | HB := Expr_Value (Type_High_Bound (Typ)); | |
8606 | else | |
8607 | HB := Expr_Value (Type_High_Bound (Base_Type (Typ))); | |
8608 | end if; | |
8609 | ||
8610 | if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then | |
8611 | LB := Expr_Value (Type_Low_Bound (Typ)); | |
8612 | else | |
8613 | LB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); | |
8614 | end if; | |
8615 | ||
d81b4bfe TQ |
8616 | -- Note that the test below is deliberately excluding the |
8617 | -- largest negative number, since that is a potentially | |
0669bebe GB |
8618 | -- troublesome case (e.g. -2 * x, where the result is the |
8619 | -- largest negative integer has an overflow with 2 * x). | |
8620 | ||
8621 | if Val > LB and then Val <= HB then | |
8622 | return; | |
8623 | end if; | |
8624 | end if; | |
8625 | ||
8626 | -- For the multiplication case, the only case we have to worry | |
8627 | -- about is when (-a)*b is exactly the largest negative number | |
8628 | -- so that -(a*b) can cause overflow. This can only happen if | |
8629 | -- a is a power of 2, and more generally if any operand is a | |
8630 | -- constant that is not a power of 2, then the parentheses | |
8631 | -- cannot affect whether overflow occurs. We only bother to | |
8632 | -- test the left most operand | |
8633 | ||
8634 | -- Loop looking at left operands for one that has known value | |
8635 | ||
8636 | Opnd := Rorig; | |
8637 | Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop | |
8638 | if Compile_Time_Known_Value (Left_Opnd (Opnd)) then | |
8639 | Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd))); | |
8640 | ||
8641 | -- Operand value of 0 or 1 skips warning | |
8642 | ||
8643 | if Lval <= 1 then | |
8644 | return; | |
8645 | ||
8646 | -- Otherwise check power of 2, if power of 2, warn, if | |
8647 | -- anything else, skip warning. | |
8648 | ||
8649 | else | |
8650 | while Lval /= 2 loop | |
8651 | if Lval mod 2 = 1 then | |
8652 | return; | |
8653 | else | |
8654 | Lval := Lval / 2; | |
8655 | end if; | |
8656 | end loop; | |
8657 | ||
8658 | exit Opnd_Loop; | |
8659 | end if; | |
8660 | end if; | |
8661 | ||
8662 | -- Keep looking at left operands | |
8663 | ||
8664 | Opnd := Left_Opnd (Opnd); | |
8665 | end loop Opnd_Loop; | |
8666 | ||
8667 | -- For rem or "/" we can only have a problematic situation | |
8668 | -- if the divisor has a value of minus one or one. Otherwise | |
8669 | -- overflow is impossible (divisor > 1) or we have a case of | |
8670 | -- division by zero in any case. | |
8671 | ||
45fc7ddb | 8672 | if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem) |
0669bebe GB |
8673 | and then Compile_Time_Known_Value (Right_Opnd (Rorig)) |
8674 | and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1 | |
8675 | then | |
8676 | return; | |
8677 | end if; | |
8678 | ||
8679 | -- If we fall through warning should be issued | |
8680 | ||
8681 | Error_Msg_N | |
aa5147f0 | 8682 | ("?unary minus expression should be parenthesized here!", N); |
0669bebe GB |
8683 | end if; |
8684 | end if; | |
8685 | end; | |
996ae0b0 RK |
8686 | end Resolve_Unary_Op; |
8687 | ||
8688 | ---------------------------------- | |
8689 | -- Resolve_Unchecked_Expression -- | |
8690 | ---------------------------------- | |
8691 | ||
8692 | procedure Resolve_Unchecked_Expression | |
8693 | (N : Node_Id; | |
8694 | Typ : Entity_Id) | |
8695 | is | |
8696 | begin | |
8697 | Resolve (Expression (N), Typ, Suppress => All_Checks); | |
8698 | Set_Etype (N, Typ); | |
8699 | end Resolve_Unchecked_Expression; | |
8700 | ||
8701 | --------------------------------------- | |
8702 | -- Resolve_Unchecked_Type_Conversion -- | |
8703 | --------------------------------------- | |
8704 | ||
8705 | procedure Resolve_Unchecked_Type_Conversion | |
8706 | (N : Node_Id; | |
8707 | Typ : Entity_Id) | |
8708 | is | |
07fc65c4 GB |
8709 | pragma Warnings (Off, Typ); |
8710 | ||
996ae0b0 RK |
8711 | Operand : constant Node_Id := Expression (N); |
8712 | Opnd_Type : constant Entity_Id := Etype (Operand); | |
8713 | ||
8714 | begin | |
a77842bd | 8715 | -- Resolve operand using its own type |
996ae0b0 RK |
8716 | |
8717 | Resolve (Operand, Opnd_Type); | |
8718 | Eval_Unchecked_Conversion (N); | |
8719 | ||
8720 | end Resolve_Unchecked_Type_Conversion; | |
8721 | ||
8722 | ------------------------------ | |
8723 | -- Rewrite_Operator_As_Call -- | |
8724 | ------------------------------ | |
8725 | ||
8726 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is | |
fbf5a39b AC |
8727 | Loc : constant Source_Ptr := Sloc (N); |
8728 | Actuals : constant List_Id := New_List; | |
996ae0b0 RK |
8729 | New_N : Node_Id; |
8730 | ||
8731 | begin | |
8732 | if Nkind (N) in N_Binary_Op then | |
8733 | Append (Left_Opnd (N), Actuals); | |
8734 | end if; | |
8735 | ||
8736 | Append (Right_Opnd (N), Actuals); | |
8737 | ||
8738 | New_N := | |
8739 | Make_Function_Call (Sloc => Loc, | |
8740 | Name => New_Occurrence_Of (Nam, Loc), | |
8741 | Parameter_Associations => Actuals); | |
8742 | ||
8743 | Preserve_Comes_From_Source (New_N, N); | |
8744 | Preserve_Comes_From_Source (Name (New_N), N); | |
8745 | Rewrite (N, New_N); | |
8746 | Set_Etype (N, Etype (Nam)); | |
8747 | end Rewrite_Operator_As_Call; | |
8748 | ||
8749 | ------------------------------ | |
8750 | -- Rewrite_Renamed_Operator -- | |
8751 | ------------------------------ | |
8752 | ||
0ab80019 AC |
8753 | procedure Rewrite_Renamed_Operator |
8754 | (N : Node_Id; | |
8755 | Op : Entity_Id; | |
8756 | Typ : Entity_Id) | |
8757 | is | |
996ae0b0 RK |
8758 | Nam : constant Name_Id := Chars (Op); |
8759 | Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
8760 | Op_Node : Node_Id; | |
8761 | ||
8762 | begin | |
d81b4bfe TQ |
8763 | -- Rewrite the operator node using the real operator, not its renaming. |
8764 | -- Exclude user-defined intrinsic operations of the same name, which are | |
8765 | -- treated separately and rewritten as calls. | |
996ae0b0 | 8766 | |
0ab80019 AC |
8767 | if Ekind (Op) /= E_Function |
8768 | or else Chars (N) /= Nam | |
8769 | then | |
996ae0b0 RK |
8770 | Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N)); |
8771 | Set_Chars (Op_Node, Nam); | |
8772 | Set_Etype (Op_Node, Etype (N)); | |
8773 | Set_Entity (Op_Node, Op); | |
8774 | Set_Right_Opnd (Op_Node, Right_Opnd (N)); | |
8775 | ||
b7d1f17f HK |
8776 | -- Indicate that both the original entity and its renaming are |
8777 | -- referenced at this point. | |
fbf5a39b AC |
8778 | |
8779 | Generate_Reference (Entity (N), N); | |
996ae0b0 RK |
8780 | Generate_Reference (Op, N); |
8781 | ||
8782 | if Is_Binary then | |
8783 | Set_Left_Opnd (Op_Node, Left_Opnd (N)); | |
8784 | end if; | |
8785 | ||
8786 | Rewrite (N, Op_Node); | |
0ab80019 AC |
8787 | |
8788 | -- If the context type is private, add the appropriate conversions | |
8789 | -- so that the operator is applied to the full view. This is done | |
8790 | -- in the routines that resolve intrinsic operators, | |
8791 | ||
8792 | if Is_Intrinsic_Subprogram (Op) | |
8793 | and then Is_Private_Type (Typ) | |
8794 | then | |
8795 | case Nkind (N) is | |
8796 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
8797 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
8798 | Resolve_Intrinsic_Operator (N, Typ); | |
8799 | ||
d81b4bfe | 8800 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
0ab80019 AC |
8801 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
8802 | ||
8803 | when others => | |
8804 | Resolve (N, Typ); | |
8805 | end case; | |
8806 | end if; | |
8807 | ||
8808 | elsif Ekind (Op) = E_Function | |
8809 | and then Is_Intrinsic_Subprogram (Op) | |
8810 | then | |
8811 | -- Operator renames a user-defined operator of the same name. Use | |
b7d1f17f | 8812 | -- the original operator in the node, which is the one that Gigi |
0ab80019 AC |
8813 | -- knows about. |
8814 | ||
8815 | Set_Entity (N, Op); | |
8816 | Set_Is_Overloaded (N, False); | |
996ae0b0 RK |
8817 | end if; |
8818 | end Rewrite_Renamed_Operator; | |
8819 | ||
8820 | ----------------------- | |
8821 | -- Set_Slice_Subtype -- | |
8822 | ----------------------- | |
8823 | ||
8824 | -- Build an implicit subtype declaration to represent the type delivered | |
8825 | -- by the slice. This is an abbreviated version of an array subtype. We | |
b7d1f17f | 8826 | -- define an index subtype for the slice, using either the subtype name |
996ae0b0 RK |
8827 | -- or the discrete range of the slice. To be consistent with index usage |
8828 | -- elsewhere, we create a list header to hold the single index. This list | |
8829 | -- is not otherwise attached to the syntax tree. | |
8830 | ||
8831 | procedure Set_Slice_Subtype (N : Node_Id) is | |
8832 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 8833 | Index_List : constant List_Id := New_List; |
996ae0b0 | 8834 | Index : Node_Id; |
996ae0b0 RK |
8835 | Index_Subtype : Entity_Id; |
8836 | Index_Type : Entity_Id; | |
8837 | Slice_Subtype : Entity_Id; | |
8838 | Drange : constant Node_Id := Discrete_Range (N); | |
8839 | ||
8840 | begin | |
8841 | if Is_Entity_Name (Drange) then | |
8842 | Index_Subtype := Entity (Drange); | |
8843 | ||
8844 | else | |
8845 | -- We force the evaluation of a range. This is definitely needed in | |
8846 | -- the renamed case, and seems safer to do unconditionally. Note in | |
8847 | -- any case that since we will create and insert an Itype referring | |
8848 | -- to this range, we must make sure any side effect removal actions | |
8849 | -- are inserted before the Itype definition. | |
8850 | ||
8851 | if Nkind (Drange) = N_Range then | |
8852 | Force_Evaluation (Low_Bound (Drange)); | |
8853 | Force_Evaluation (High_Bound (Drange)); | |
8854 | end if; | |
8855 | ||
8856 | Index_Type := Base_Type (Etype (Drange)); | |
8857 | ||
8858 | Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
8859 | ||
8860 | Set_Scalar_Range (Index_Subtype, Drange); | |
8861 | Set_Etype (Index_Subtype, Index_Type); | |
8862 | Set_Size_Info (Index_Subtype, Index_Type); | |
8863 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
8864 | end if; | |
8865 | ||
8866 | Slice_Subtype := Create_Itype (E_Array_Subtype, N); | |
8867 | ||
8868 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
8869 | Set_Etype (Index, Index_Subtype); | |
8870 | Append (Index, Index_List); | |
8871 | ||
996ae0b0 RK |
8872 | Set_First_Index (Slice_Subtype, Index); |
8873 | Set_Etype (Slice_Subtype, Base_Type (Etype (N))); | |
8874 | Set_Is_Constrained (Slice_Subtype, True); | |
996ae0b0 RK |
8875 | |
8876 | Check_Compile_Time_Size (Slice_Subtype); | |
8877 | ||
b7d1f17f HK |
8878 | -- The Etype of the existing Slice node is reset to this slice subtype. |
8879 | -- Its bounds are obtained from its first index. | |
996ae0b0 RK |
8880 | |
8881 | Set_Etype (N, Slice_Subtype); | |
8882 | ||
8883 | -- In the packed case, this must be immediately frozen | |
8884 | ||
8885 | -- Couldn't we always freeze here??? and if we did, then the above | |
8886 | -- call to Check_Compile_Time_Size could be eliminated, which would | |
8887 | -- be nice, because then that routine could be made private to Freeze. | |
8888 | ||
45fc7ddb HK |
8889 | -- Why the test for In_Spec_Expression here ??? |
8890 | ||
8891 | if Is_Packed (Slice_Subtype) and not In_Spec_Expression then | |
996ae0b0 RK |
8892 | Freeze_Itype (Slice_Subtype, N); |
8893 | end if; | |
8894 | ||
8895 | end Set_Slice_Subtype; | |
8896 | ||
8897 | -------------------------------- | |
8898 | -- Set_String_Literal_Subtype -- | |
8899 | -------------------------------- | |
8900 | ||
8901 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is | |
c8ef728f ES |
8902 | Loc : constant Source_Ptr := Sloc (N); |
8903 | Low_Bound : constant Node_Id := | |
d81b4bfe | 8904 | Type_Low_Bound (Etype (First_Index (Typ))); |
996ae0b0 RK |
8905 | Subtype_Id : Entity_Id; |
8906 | ||
8907 | begin | |
8908 | if Nkind (N) /= N_String_Literal then | |
8909 | return; | |
996ae0b0 RK |
8910 | end if; |
8911 | ||
c8ef728f | 8912 | Subtype_Id := Create_Itype (E_String_Literal_Subtype, N); |
91b1417d AC |
8913 | Set_String_Literal_Length (Subtype_Id, UI_From_Int |
8914 | (String_Length (Strval (N)))); | |
c8ef728f ES |
8915 | Set_Etype (Subtype_Id, Base_Type (Typ)); |
8916 | Set_Is_Constrained (Subtype_Id); | |
8917 | Set_Etype (N, Subtype_Id); | |
8918 | ||
8919 | if Is_OK_Static_Expression (Low_Bound) then | |
996ae0b0 RK |
8920 | |
8921 | -- The low bound is set from the low bound of the corresponding | |
8922 | -- index type. Note that we do not store the high bound in the | |
c8ef728f | 8923 | -- string literal subtype, but it can be deduced if necessary |
996ae0b0 RK |
8924 | -- from the length and the low bound. |
8925 | ||
c8ef728f | 8926 | Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound); |
996ae0b0 | 8927 | |
c8ef728f ES |
8928 | else |
8929 | Set_String_Literal_Low_Bound | |
8930 | (Subtype_Id, Make_Integer_Literal (Loc, 1)); | |
8931 | Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive); | |
8932 | ||
b7d1f17f HK |
8933 | -- Build bona fide subtype for the string, and wrap it in an |
8934 | -- unchecked conversion, because the backend expects the | |
c8ef728f ES |
8935 | -- String_Literal_Subtype to have a static lower bound. |
8936 | ||
8937 | declare | |
8938 | Index_List : constant List_Id := New_List; | |
8939 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
8940 | High_Bound : constant Node_Id := | |
8941 | Make_Op_Add (Loc, | |
8942 | Left_Opnd => New_Copy_Tree (Low_Bound), | |
8943 | Right_Opnd => | |
8944 | Make_Integer_Literal (Loc, | |
8945 | String_Length (Strval (N)) - 1)); | |
8946 | Array_Subtype : Entity_Id; | |
8947 | Index_Subtype : Entity_Id; | |
8948 | Drange : Node_Id; | |
8949 | Index : Node_Id; | |
8950 | ||
8951 | begin | |
8952 | Index_Subtype := | |
8953 | Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
0669bebe | 8954 | Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound); |
c8ef728f ES |
8955 | Set_Scalar_Range (Index_Subtype, Drange); |
8956 | Set_Parent (Drange, N); | |
8957 | Analyze_And_Resolve (Drange, Index_Type); | |
8958 | ||
36fcf362 RD |
8959 | -- In the context, the Index_Type may already have a constraint, |
8960 | -- so use common base type on string subtype. The base type may | |
8961 | -- be used when generating attributes of the string, for example | |
8962 | -- in the context of a slice assignment. | |
8963 | ||
8964 | Set_Etype (Index_Subtype, Base_Type (Index_Type)); | |
c8ef728f ES |
8965 | Set_Size_Info (Index_Subtype, Index_Type); |
8966 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
8967 | ||
8968 | Array_Subtype := Create_Itype (E_Array_Subtype, N); | |
8969 | ||
8970 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
8971 | Set_Etype (Index, Index_Subtype); | |
8972 | Append (Index, Index_List); | |
8973 | ||
8974 | Set_First_Index (Array_Subtype, Index); | |
8975 | Set_Etype (Array_Subtype, Base_Type (Typ)); | |
8976 | Set_Is_Constrained (Array_Subtype, True); | |
c8ef728f ES |
8977 | |
8978 | Rewrite (N, | |
8979 | Make_Unchecked_Type_Conversion (Loc, | |
8980 | Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc), | |
8981 | Expression => Relocate_Node (N))); | |
8982 | Set_Etype (N, Array_Subtype); | |
8983 | end; | |
8984 | end if; | |
996ae0b0 RK |
8985 | end Set_String_Literal_Subtype; |
8986 | ||
0669bebe GB |
8987 | ------------------------------ |
8988 | -- Simplify_Type_Conversion -- | |
8989 | ------------------------------ | |
8990 | ||
8991 | procedure Simplify_Type_Conversion (N : Node_Id) is | |
8992 | begin | |
8993 | if Nkind (N) = N_Type_Conversion then | |
8994 | declare | |
8995 | Operand : constant Node_Id := Expression (N); | |
8996 | Target_Typ : constant Entity_Id := Etype (N); | |
8997 | Opnd_Typ : constant Entity_Id := Etype (Operand); | |
8998 | ||
8999 | begin | |
9000 | if Is_Floating_Point_Type (Opnd_Typ) | |
9001 | and then | |
9002 | (Is_Integer_Type (Target_Typ) | |
9003 | or else (Is_Fixed_Point_Type (Target_Typ) | |
9004 | and then Conversion_OK (N))) | |
9005 | and then Nkind (Operand) = N_Attribute_Reference | |
9006 | and then Attribute_Name (Operand) = Name_Truncation | |
9007 | ||
9008 | -- Special processing required if the conversion is the expression | |
9009 | -- of a Truncation attribute reference. In this case we replace: | |
9010 | ||
9011 | -- ityp (ftyp'Truncation (x)) | |
9012 | ||
9013 | -- by | |
9014 | ||
9015 | -- ityp (x) | |
9016 | ||
9017 | -- with the Float_Truncate flag set, which is more efficient | |
9018 | ||
9019 | then | |
9020 | Rewrite (Operand, | |
9021 | Relocate_Node (First (Expressions (Operand)))); | |
9022 | Set_Float_Truncate (N, True); | |
9023 | end if; | |
9024 | end; | |
9025 | end if; | |
9026 | end Simplify_Type_Conversion; | |
9027 | ||
996ae0b0 RK |
9028 | ----------------------------- |
9029 | -- Unique_Fixed_Point_Type -- | |
9030 | ----------------------------- | |
9031 | ||
9032 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is | |
9033 | T1 : Entity_Id := Empty; | |
9034 | T2 : Entity_Id; | |
9035 | Item : Node_Id; | |
9036 | Scop : Entity_Id; | |
9037 | ||
9038 | procedure Fixed_Point_Error; | |
d81b4bfe TQ |
9039 | -- Give error messages for true ambiguity. Messages are posted on node |
9040 | -- N, and entities T1, T2 are the possible interpretations. | |
a77842bd TQ |
9041 | |
9042 | ----------------------- | |
9043 | -- Fixed_Point_Error -- | |
9044 | ----------------------- | |
996ae0b0 RK |
9045 | |
9046 | procedure Fixed_Point_Error is | |
9047 | begin | |
9048 | Error_Msg_N ("ambiguous universal_fixed_expression", N); | |
aa180613 RD |
9049 | Error_Msg_NE ("\\possible interpretation as}", N, T1); |
9050 | Error_Msg_NE ("\\possible interpretation as}", N, T2); | |
996ae0b0 RK |
9051 | end Fixed_Point_Error; |
9052 | ||
a77842bd TQ |
9053 | -- Start of processing for Unique_Fixed_Point_Type |
9054 | ||
996ae0b0 RK |
9055 | begin |
9056 | -- The operations on Duration are visible, so Duration is always a | |
9057 | -- possible interpretation. | |
9058 | ||
9059 | T1 := Standard_Duration; | |
9060 | ||
bc5f3720 | 9061 | -- Look for fixed-point types in enclosing scopes |
996ae0b0 | 9062 | |
fbf5a39b | 9063 | Scop := Current_Scope; |
996ae0b0 RK |
9064 | while Scop /= Standard_Standard loop |
9065 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9066 | while Present (T2) loop |
9067 | if Is_Fixed_Point_Type (T2) | |
9068 | and then Current_Entity (T2) = T2 | |
9069 | and then Scope (Base_Type (T2)) = Scop | |
9070 | then | |
9071 | if Present (T1) then | |
9072 | Fixed_Point_Error; | |
9073 | return Any_Type; | |
9074 | else | |
9075 | T1 := T2; | |
9076 | end if; | |
9077 | end if; | |
9078 | ||
9079 | Next_Entity (T2); | |
9080 | end loop; | |
9081 | ||
9082 | Scop := Scope (Scop); | |
9083 | end loop; | |
9084 | ||
a77842bd | 9085 | -- Look for visible fixed type declarations in the context |
996ae0b0 RK |
9086 | |
9087 | Item := First (Context_Items (Cunit (Current_Sem_Unit))); | |
996ae0b0 | 9088 | while Present (Item) loop |
996ae0b0 RK |
9089 | if Nkind (Item) = N_With_Clause then |
9090 | Scop := Entity (Name (Item)); | |
9091 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9092 | while Present (T2) loop |
9093 | if Is_Fixed_Point_Type (T2) | |
9094 | and then Scope (Base_Type (T2)) = Scop | |
9095 | and then (Is_Potentially_Use_Visible (T2) | |
9096 | or else In_Use (T2)) | |
9097 | then | |
9098 | if Present (T1) then | |
9099 | Fixed_Point_Error; | |
9100 | return Any_Type; | |
9101 | else | |
9102 | T1 := T2; | |
9103 | end if; | |
9104 | end if; | |
9105 | ||
9106 | Next_Entity (T2); | |
9107 | end loop; | |
9108 | end if; | |
9109 | ||
9110 | Next (Item); | |
9111 | end loop; | |
9112 | ||
9113 | if Nkind (N) = N_Real_Literal then | |
aa5147f0 | 9114 | Error_Msg_NE ("?real literal interpreted as }!", N, T1); |
996ae0b0 | 9115 | else |
aa5147f0 | 9116 | Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1); |
996ae0b0 RK |
9117 | end if; |
9118 | ||
9119 | return T1; | |
9120 | end Unique_Fixed_Point_Type; | |
9121 | ||
9122 | ---------------------- | |
9123 | -- Valid_Conversion -- | |
9124 | ---------------------- | |
9125 | ||
9126 | function Valid_Conversion | |
9127 | (N : Node_Id; | |
9128 | Target : Entity_Id; | |
0ab80019 | 9129 | Operand : Node_Id) return Boolean |
996ae0b0 | 9130 | is |
fbf5a39b | 9131 | Target_Type : constant Entity_Id := Base_Type (Target); |
996ae0b0 RK |
9132 | Opnd_Type : Entity_Id := Etype (Operand); |
9133 | ||
9134 | function Conversion_Check | |
9135 | (Valid : Boolean; | |
0ab80019 | 9136 | Msg : String) return Boolean; |
996ae0b0 RK |
9137 | -- Little routine to post Msg if Valid is False, returns Valid value |
9138 | ||
9139 | function Valid_Tagged_Conversion | |
9140 | (Target_Type : Entity_Id; | |
0ab80019 | 9141 | Opnd_Type : Entity_Id) return Boolean; |
996ae0b0 RK |
9142 | -- Specifically test for validity of tagged conversions |
9143 | ||
aa180613 RD |
9144 | function Valid_Array_Conversion return Boolean; |
9145 | -- Check index and component conformance, and accessibility levels | |
9146 | -- if the component types are anonymous access types (Ada 2005) | |
9147 | ||
996ae0b0 RK |
9148 | ---------------------- |
9149 | -- Conversion_Check -- | |
9150 | ---------------------- | |
9151 | ||
9152 | function Conversion_Check | |
9153 | (Valid : Boolean; | |
0ab80019 | 9154 | Msg : String) return Boolean |
996ae0b0 RK |
9155 | is |
9156 | begin | |
9157 | if not Valid then | |
9158 | Error_Msg_N (Msg, Operand); | |
9159 | end if; | |
9160 | ||
9161 | return Valid; | |
9162 | end Conversion_Check; | |
9163 | ||
aa180613 RD |
9164 | ---------------------------- |
9165 | -- Valid_Array_Conversion -- | |
9166 | ---------------------------- | |
9167 | ||
9168 | function Valid_Array_Conversion return Boolean | |
9169 | is | |
9170 | Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type); | |
9171 | Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type); | |
9172 | ||
9173 | Opnd_Index : Node_Id; | |
9174 | Opnd_Index_Type : Entity_Id; | |
9175 | ||
9176 | Target_Comp_Type : constant Entity_Id := | |
9177 | Component_Type (Target_Type); | |
9178 | Target_Comp_Base : constant Entity_Id := | |
9179 | Base_Type (Target_Comp_Type); | |
9180 | ||
9181 | Target_Index : Node_Id; | |
9182 | Target_Index_Type : Entity_Id; | |
9183 | ||
9184 | begin | |
9185 | -- Error if wrong number of dimensions | |
9186 | ||
9187 | if | |
9188 | Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type) | |
9189 | then | |
9190 | Error_Msg_N | |
9191 | ("incompatible number of dimensions for conversion", Operand); | |
9192 | return False; | |
9193 | ||
9194 | -- Number of dimensions matches | |
9195 | ||
9196 | else | |
9197 | -- Loop through indexes of the two arrays | |
9198 | ||
9199 | Target_Index := First_Index (Target_Type); | |
9200 | Opnd_Index := First_Index (Opnd_Type); | |
9201 | while Present (Target_Index) and then Present (Opnd_Index) loop | |
9202 | Target_Index_Type := Etype (Target_Index); | |
9203 | Opnd_Index_Type := Etype (Opnd_Index); | |
9204 | ||
9205 | -- Error if index types are incompatible | |
9206 | ||
9207 | if not (Is_Integer_Type (Target_Index_Type) | |
9208 | and then Is_Integer_Type (Opnd_Index_Type)) | |
9209 | and then (Root_Type (Target_Index_Type) | |
9210 | /= Root_Type (Opnd_Index_Type)) | |
9211 | then | |
9212 | Error_Msg_N | |
9213 | ("incompatible index types for array conversion", | |
9214 | Operand); | |
9215 | return False; | |
9216 | end if; | |
9217 | ||
9218 | Next_Index (Target_Index); | |
9219 | Next_Index (Opnd_Index); | |
9220 | end loop; | |
9221 | ||
9222 | -- If component types have same base type, all set | |
9223 | ||
9224 | if Target_Comp_Base = Opnd_Comp_Base then | |
9225 | null; | |
9226 | ||
9227 | -- Here if base types of components are not the same. The only | |
9228 | -- time this is allowed is if we have anonymous access types. | |
9229 | ||
9230 | -- The conversion of arrays of anonymous access types can lead | |
9231 | -- to dangling pointers. AI-392 formalizes the accessibility | |
9232 | -- checks that must be applied to such conversions to prevent | |
9233 | -- out-of-scope references. | |
9234 | ||
9235 | elsif | |
9236 | (Ekind (Target_Comp_Base) = E_Anonymous_Access_Type | |
9237 | or else | |
9238 | Ekind (Target_Comp_Base) = E_Anonymous_Access_Subprogram_Type) | |
9239 | and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base) | |
9240 | and then | |
9241 | Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type) | |
9242 | then | |
9243 | if Type_Access_Level (Target_Type) < | |
9244 | Type_Access_Level (Opnd_Type) | |
9245 | then | |
9246 | if In_Instance_Body then | |
9247 | Error_Msg_N ("?source array type " & | |
9248 | "has deeper accessibility level than target", Operand); | |
9249 | Error_Msg_N ("\?Program_Error will be raised at run time", | |
9250 | Operand); | |
9251 | Rewrite (N, | |
9252 | Make_Raise_Program_Error (Sloc (N), | |
9253 | Reason => PE_Accessibility_Check_Failed)); | |
9254 | Set_Etype (N, Target_Type); | |
9255 | return False; | |
9256 | ||
9257 | -- Conversion not allowed because of accessibility levels | |
9258 | ||
9259 | else | |
9260 | Error_Msg_N ("source array type " & | |
9261 | "has deeper accessibility level than target", Operand); | |
9262 | return False; | |
9263 | end if; | |
9264 | else | |
9265 | null; | |
9266 | end if; | |
9267 | ||
9268 | -- All other cases where component base types do not match | |
9269 | ||
9270 | else | |
9271 | Error_Msg_N | |
9272 | ("incompatible component types for array conversion", | |
9273 | Operand); | |
9274 | return False; | |
9275 | end if; | |
9276 | ||
45fc7ddb HK |
9277 | -- Check that component subtypes statically match. For numeric |
9278 | -- types this means that both must be either constrained or | |
9279 | -- unconstrained. For enumeration types the bounds must match. | |
9280 | -- All of this is checked in Subtypes_Statically_Match. | |
aa180613 | 9281 | |
45fc7ddb | 9282 | if not Subtypes_Statically_Match |
aa180613 RD |
9283 | (Target_Comp_Type, Opnd_Comp_Type) |
9284 | then | |
9285 | Error_Msg_N | |
9286 | ("component subtypes must statically match", Operand); | |
9287 | return False; | |
9288 | end if; | |
9289 | end if; | |
9290 | ||
9291 | return True; | |
9292 | end Valid_Array_Conversion; | |
9293 | ||
996ae0b0 RK |
9294 | ----------------------------- |
9295 | -- Valid_Tagged_Conversion -- | |
9296 | ----------------------------- | |
9297 | ||
9298 | function Valid_Tagged_Conversion | |
9299 | (Target_Type : Entity_Id; | |
0ab80019 | 9300 | Opnd_Type : Entity_Id) return Boolean |
996ae0b0 RK |
9301 | is |
9302 | begin | |
a77842bd | 9303 | -- Upward conversions are allowed (RM 4.6(22)) |
996ae0b0 RK |
9304 | |
9305 | if Covers (Target_Type, Opnd_Type) | |
9306 | or else Is_Ancestor (Target_Type, Opnd_Type) | |
9307 | then | |
9308 | return True; | |
9309 | ||
a77842bd TQ |
9310 | -- Downward conversion are allowed if the operand is class-wide |
9311 | -- (RM 4.6(23)). | |
996ae0b0 RK |
9312 | |
9313 | elsif Is_Class_Wide_Type (Opnd_Type) | |
b7d1f17f | 9314 | and then Covers (Opnd_Type, Target_Type) |
996ae0b0 RK |
9315 | then |
9316 | return True; | |
9317 | ||
9318 | elsif Covers (Opnd_Type, Target_Type) | |
9319 | or else Is_Ancestor (Opnd_Type, Target_Type) | |
9320 | then | |
9321 | return | |
9322 | Conversion_Check (False, | |
9323 | "downward conversion of tagged objects not allowed"); | |
758c442c | 9324 | |
0669bebe GB |
9325 | -- Ada 2005 (AI-251): The conversion to/from interface types is |
9326 | -- always valid | |
758c442c | 9327 | |
0669bebe | 9328 | elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then |
758c442c GD |
9329 | return True; |
9330 | ||
b7d1f17f HK |
9331 | -- If the operand is a class-wide type obtained through a limited_ |
9332 | -- with clause, and the context includes the non-limited view, use | |
9333 | -- it to determine whether the conversion is legal. | |
9334 | ||
9335 | elsif Is_Class_Wide_Type (Opnd_Type) | |
9336 | and then From_With_Type (Opnd_Type) | |
9337 | and then Present (Non_Limited_View (Etype (Opnd_Type))) | |
9338 | and then Is_Interface (Non_Limited_View (Etype (Opnd_Type))) | |
9339 | then | |
9340 | return True; | |
9341 | ||
aa180613 RD |
9342 | elsif Is_Access_Type (Opnd_Type) |
9343 | and then Is_Interface (Directly_Designated_Type (Opnd_Type)) | |
9344 | then | |
9345 | return True; | |
9346 | ||
996ae0b0 RK |
9347 | else |
9348 | Error_Msg_NE | |
9349 | ("invalid tagged conversion, not compatible with}", | |
9350 | N, First_Subtype (Opnd_Type)); | |
9351 | return False; | |
9352 | end if; | |
9353 | end Valid_Tagged_Conversion; | |
9354 | ||
9355 | -- Start of processing for Valid_Conversion | |
9356 | ||
9357 | begin | |
9358 | Check_Parameterless_Call (Operand); | |
9359 | ||
9360 | if Is_Overloaded (Operand) then | |
9361 | declare | |
9362 | I : Interp_Index; | |
9363 | I1 : Interp_Index; | |
9364 | It : Interp; | |
9365 | It1 : Interp; | |
9366 | N1 : Entity_Id; | |
9367 | ||
9368 | begin | |
d81b4bfe TQ |
9369 | -- Remove procedure calls, which syntactically cannot appear in |
9370 | -- this context, but which cannot be removed by type checking, | |
996ae0b0 RK |
9371 | -- because the context does not impose a type. |
9372 | ||
1420b484 JM |
9373 | -- When compiling for VMS, spurious ambiguities can be produced |
9374 | -- when arithmetic operations have a literal operand and return | |
9375 | -- System.Address or a descendant of it. These ambiguities are | |
9376 | -- otherwise resolved by the context, but for conversions there | |
9377 | -- is no context type and the removal of the spurious operations | |
9378 | -- must be done explicitly here. | |
9379 | ||
9ebe3743 HK |
9380 | -- The node may be labelled overloaded, but still contain only |
9381 | -- one interpretation because others were discarded in previous | |
9382 | -- filters. If this is the case, retain the single interpretation | |
9383 | -- if legal. | |
9384 | ||
996ae0b0 | 9385 | Get_First_Interp (Operand, I, It); |
9ebe3743 HK |
9386 | Opnd_Type := It.Typ; |
9387 | Get_Next_Interp (I, It); | |
996ae0b0 | 9388 | |
9ebe3743 HK |
9389 | if Present (It.Typ) |
9390 | and then Opnd_Type /= Standard_Void_Type | |
9391 | then | |
9392 | -- More than one candidate interpretation is available | |
996ae0b0 | 9393 | |
9ebe3743 HK |
9394 | Get_First_Interp (Operand, I, It); |
9395 | while Present (It.Typ) loop | |
9396 | if It.Typ = Standard_Void_Type then | |
9397 | Remove_Interp (I); | |
9398 | end if; | |
1420b484 | 9399 | |
9ebe3743 HK |
9400 | if Present (System_Aux_Id) |
9401 | and then Is_Descendent_Of_Address (It.Typ) | |
9402 | then | |
9403 | Remove_Interp (I); | |
9404 | end if; | |
9405 | ||
9406 | Get_Next_Interp (I, It); | |
9407 | end loop; | |
9408 | end if; | |
996ae0b0 RK |
9409 | |
9410 | Get_First_Interp (Operand, I, It); | |
9411 | I1 := I; | |
9412 | It1 := It; | |
9413 | ||
9414 | if No (It.Typ) then | |
9415 | Error_Msg_N ("illegal operand in conversion", Operand); | |
9416 | return False; | |
9417 | end if; | |
9418 | ||
9419 | Get_Next_Interp (I, It); | |
9420 | ||
9421 | if Present (It.Typ) then | |
9422 | N1 := It1.Nam; | |
9423 | It1 := Disambiguate (Operand, I1, I, Any_Type); | |
9424 | ||
9425 | if It1 = No_Interp then | |
9426 | Error_Msg_N ("ambiguous operand in conversion", Operand); | |
9427 | ||
9428 | Error_Msg_Sloc := Sloc (It.Nam); | |
4e7a4f6e AC |
9429 | Error_Msg_N -- CODEFIX |
9430 | ("\\possible interpretation#!", Operand); | |
996ae0b0 RK |
9431 | |
9432 | Error_Msg_Sloc := Sloc (N1); | |
4e7a4f6e AC |
9433 | Error_Msg_N -- CODEFIX |
9434 | ("\\possible interpretation#!", Operand); | |
996ae0b0 RK |
9435 | |
9436 | return False; | |
9437 | end if; | |
9438 | end if; | |
9439 | ||
9440 | Set_Etype (Operand, It1.Typ); | |
9441 | Opnd_Type := It1.Typ; | |
9442 | end; | |
9443 | end if; | |
9444 | ||
aa180613 | 9445 | -- Numeric types |
996ae0b0 | 9446 | |
aa180613 | 9447 | if Is_Numeric_Type (Target_Type) then |
996ae0b0 | 9448 | |
aa180613 | 9449 | -- A universal fixed expression can be converted to any numeric type |
996ae0b0 | 9450 | |
996ae0b0 RK |
9451 | if Opnd_Type = Universal_Fixed then |
9452 | return True; | |
7324bf49 | 9453 | |
aa180613 RD |
9454 | -- Also no need to check when in an instance or inlined body, because |
9455 | -- the legality has been established when the template was analyzed. | |
9456 | -- Furthermore, numeric conversions may occur where only a private | |
f3d57416 | 9457 | -- view of the operand type is visible at the instantiation point. |
aa180613 RD |
9458 | -- This results in a spurious error if we check that the operand type |
9459 | -- is a numeric type. | |
9460 | ||
9461 | -- Note: in a previous version of this unit, the following tests were | |
9462 | -- applied only for generated code (Comes_From_Source set to False), | |
9463 | -- but in fact the test is required for source code as well, since | |
9464 | -- this situation can arise in source code. | |
9465 | ||
9466 | elsif In_Instance or else In_Inlined_Body then | |
9467 | return True; | |
9468 | ||
9469 | -- Otherwise we need the conversion check | |
7324bf49 | 9470 | |
996ae0b0 | 9471 | else |
aa180613 RD |
9472 | return Conversion_Check |
9473 | (Is_Numeric_Type (Opnd_Type), | |
9474 | "illegal operand for numeric conversion"); | |
996ae0b0 RK |
9475 | end if; |
9476 | ||
aa180613 RD |
9477 | -- Array types |
9478 | ||
996ae0b0 RK |
9479 | elsif Is_Array_Type (Target_Type) then |
9480 | if not Is_Array_Type (Opnd_Type) | |
9481 | or else Opnd_Type = Any_Composite | |
9482 | or else Opnd_Type = Any_String | |
9483 | then | |
9484 | Error_Msg_N | |
9485 | ("illegal operand for array conversion", Operand); | |
9486 | return False; | |
996ae0b0 | 9487 | else |
aa180613 | 9488 | return Valid_Array_Conversion; |
996ae0b0 RK |
9489 | end if; |
9490 | ||
e65f50ec ES |
9491 | -- Ada 2005 (AI-251): Anonymous access types where target references an |
9492 | -- interface type. | |
758c442c GD |
9493 | |
9494 | elsif (Ekind (Target_Type) = E_General_Access_Type | |
aa180613 RD |
9495 | or else |
9496 | Ekind (Target_Type) = E_Anonymous_Access_Type) | |
758c442c GD |
9497 | and then Is_Interface (Directly_Designated_Type (Target_Type)) |
9498 | then | |
9499 | -- Check the static accessibility rule of 4.6(17). Note that the | |
d81b4bfe TQ |
9500 | -- check is not enforced when within an instance body, since the |
9501 | -- RM requires such cases to be caught at run time. | |
758c442c GD |
9502 | |
9503 | if Ekind (Target_Type) /= E_Anonymous_Access_Type then | |
9504 | if Type_Access_Level (Opnd_Type) > | |
9505 | Type_Access_Level (Target_Type) | |
9506 | then | |
9507 | -- In an instance, this is a run-time check, but one we know | |
9508 | -- will fail, so generate an appropriate warning. The raise | |
9509 | -- will be generated by Expand_N_Type_Conversion. | |
9510 | ||
9511 | if In_Instance_Body then | |
9512 | Error_Msg_N | |
9513 | ("?cannot convert local pointer to non-local access type", | |
9514 | Operand); | |
9515 | Error_Msg_N | |
c8ef728f | 9516 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
9517 | else |
9518 | Error_Msg_N | |
9519 | ("cannot convert local pointer to non-local access type", | |
9520 | Operand); | |
9521 | return False; | |
9522 | end if; | |
9523 | ||
9524 | -- Special accessibility checks are needed in the case of access | |
9525 | -- discriminants declared for a limited type. | |
9526 | ||
9527 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
9528 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
9529 | then | |
9530 | -- When the operand is a selected access discriminant the check | |
9531 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
9532 | -- the prefix of the selected name (Object_Access_Level handles |
9533 | -- checking the prefix of the operand for this case). | |
758c442c GD |
9534 | |
9535 | if Nkind (Operand) = N_Selected_Component | |
c8ef728f | 9536 | and then Object_Access_Level (Operand) > |
45fc7ddb | 9537 | Type_Access_Level (Target_Type) |
758c442c | 9538 | then |
d81b4bfe TQ |
9539 | -- In an instance, this is a run-time check, but one we know |
9540 | -- will fail, so generate an appropriate warning. The raise | |
9541 | -- will be generated by Expand_N_Type_Conversion. | |
758c442c GD |
9542 | |
9543 | if In_Instance_Body then | |
9544 | Error_Msg_N | |
9545 | ("?cannot convert access discriminant to non-local" & | |
9546 | " access type", Operand); | |
9547 | Error_Msg_N | |
c8ef728f | 9548 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
9549 | else |
9550 | Error_Msg_N | |
9551 | ("cannot convert access discriminant to non-local" & | |
9552 | " access type", Operand); | |
9553 | return False; | |
9554 | end if; | |
9555 | end if; | |
9556 | ||
9557 | -- The case of a reference to an access discriminant from | |
9558 | -- within a limited type declaration (which will appear as | |
9559 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 9560 | -- discriminant is considered to be deeper than any (nameable) |
758c442c GD |
9561 | -- access type. |
9562 | ||
9563 | if Is_Entity_Name (Operand) | |
9564 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
9565 | and then (Ekind (Entity (Operand)) = E_In_Parameter | |
9566 | or else Ekind (Entity (Operand)) = E_Constant) | |
9567 | and then Present (Discriminal_Link (Entity (Operand))) | |
9568 | then | |
9569 | Error_Msg_N | |
9570 | ("discriminant has deeper accessibility level than target", | |
9571 | Operand); | |
9572 | return False; | |
9573 | end if; | |
9574 | end if; | |
9575 | end if; | |
9576 | ||
9577 | return True; | |
9578 | ||
aa180613 RD |
9579 | -- General and anonymous access types |
9580 | ||
996ae0b0 RK |
9581 | elsif (Ekind (Target_Type) = E_General_Access_Type |
9582 | or else Ekind (Target_Type) = E_Anonymous_Access_Type) | |
9583 | and then | |
9584 | Conversion_Check | |
9585 | (Is_Access_Type (Opnd_Type) | |
9586 | and then Ekind (Opnd_Type) /= | |
9587 | E_Access_Subprogram_Type | |
9588 | and then Ekind (Opnd_Type) /= | |
9589 | E_Access_Protected_Subprogram_Type, | |
9590 | "must be an access-to-object type") | |
9591 | then | |
9592 | if Is_Access_Constant (Opnd_Type) | |
9593 | and then not Is_Access_Constant (Target_Type) | |
9594 | then | |
9595 | Error_Msg_N | |
9596 | ("access-to-constant operand type not allowed", Operand); | |
9597 | return False; | |
9598 | end if; | |
9599 | ||
758c442c GD |
9600 | -- Check the static accessibility rule of 4.6(17). Note that the |
9601 | -- check is not enforced when within an instance body, since the RM | |
9602 | -- requires such cases to be caught at run time. | |
996ae0b0 | 9603 | |
758c442c GD |
9604 | if Ekind (Target_Type) /= E_Anonymous_Access_Type |
9605 | or else Is_Local_Anonymous_Access (Target_Type) | |
9606 | then | |
996ae0b0 RK |
9607 | if Type_Access_Level (Opnd_Type) |
9608 | > Type_Access_Level (Target_Type) | |
9609 | then | |
d81b4bfe TQ |
9610 | -- In an instance, this is a run-time check, but one we know |
9611 | -- will fail, so generate an appropriate warning. The raise | |
9612 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
9613 | |
9614 | if In_Instance_Body then | |
9615 | Error_Msg_N | |
9616 | ("?cannot convert local pointer to non-local access type", | |
9617 | Operand); | |
9618 | Error_Msg_N | |
c8ef728f | 9619 | ("\?Program_Error will be raised at run time", Operand); |
996ae0b0 RK |
9620 | |
9621 | else | |
b90cfacd HK |
9622 | -- Avoid generation of spurious error message |
9623 | ||
9624 | if not Error_Posted (N) then | |
9625 | Error_Msg_N | |
9626 | ("cannot convert local pointer to non-local access type", | |
9627 | Operand); | |
9628 | end if; | |
9629 | ||
996ae0b0 RK |
9630 | return False; |
9631 | end if; | |
9632 | ||
758c442c GD |
9633 | -- Special accessibility checks are needed in the case of access |
9634 | -- discriminants declared for a limited type. | |
9635 | ||
9636 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
9637 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
9638 | then | |
996ae0b0 | 9639 | |
758c442c GD |
9640 | -- When the operand is a selected access discriminant the check |
9641 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
9642 | -- the prefix of the selected name (Object_Access_Level handles |
9643 | -- checking the prefix of the operand for this case). | |
996ae0b0 RK |
9644 | |
9645 | if Nkind (Operand) = N_Selected_Component | |
45fc7ddb HK |
9646 | and then Object_Access_Level (Operand) > |
9647 | Type_Access_Level (Target_Type) | |
996ae0b0 | 9648 | then |
d81b4bfe TQ |
9649 | -- In an instance, this is a run-time check, but one we know |
9650 | -- will fail, so generate an appropriate warning. The raise | |
9651 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
9652 | |
9653 | if In_Instance_Body then | |
9654 | Error_Msg_N | |
9655 | ("?cannot convert access discriminant to non-local" & | |
9656 | " access type", Operand); | |
9657 | Error_Msg_N | |
c8ef728f ES |
9658 | ("\?Program_Error will be raised at run time", |
9659 | Operand); | |
996ae0b0 RK |
9660 | |
9661 | else | |
9662 | Error_Msg_N | |
9663 | ("cannot convert access discriminant to non-local" & | |
9664 | " access type", Operand); | |
9665 | return False; | |
9666 | end if; | |
9667 | end if; | |
9668 | ||
758c442c GD |
9669 | -- The case of a reference to an access discriminant from |
9670 | -- within a limited type declaration (which will appear as | |
9671 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 9672 | -- discriminant is considered to be deeper than any (nameable) |
758c442c | 9673 | -- access type. |
996ae0b0 RK |
9674 | |
9675 | if Is_Entity_Name (Operand) | |
9676 | and then (Ekind (Entity (Operand)) = E_In_Parameter | |
9677 | or else Ekind (Entity (Operand)) = E_Constant) | |
9678 | and then Present (Discriminal_Link (Entity (Operand))) | |
9679 | then | |
9680 | Error_Msg_N | |
9681 | ("discriminant has deeper accessibility level than target", | |
9682 | Operand); | |
9683 | return False; | |
9684 | end if; | |
9685 | end if; | |
9686 | end if; | |
9687 | ||
14e33999 AC |
9688 | -- In the presence of limited_with clauses we have to use non-limited |
9689 | -- views, if available. | |
d81b4bfe | 9690 | |
14e33999 | 9691 | Check_Limited : declare |
0669bebe GB |
9692 | function Full_Designated_Type (T : Entity_Id) return Entity_Id; |
9693 | -- Helper function to handle limited views | |
9694 | ||
9695 | -------------------------- | |
9696 | -- Full_Designated_Type -- | |
9697 | -------------------------- | |
9698 | ||
9699 | function Full_Designated_Type (T : Entity_Id) return Entity_Id is | |
950d217a | 9700 | Desig : constant Entity_Id := Designated_Type (T); |
c0985d4e | 9701 | |
0669bebe | 9702 | begin |
950d217a AC |
9703 | -- Handle the limited view of a type |
9704 | ||
c0985d4e HK |
9705 | if Is_Incomplete_Type (Desig) |
9706 | and then From_With_Type (Desig) | |
0669bebe GB |
9707 | and then Present (Non_Limited_View (Desig)) |
9708 | then | |
950d217a AC |
9709 | return Available_View (Desig); |
9710 | else | |
9711 | return Desig; | |
0669bebe GB |
9712 | end if; |
9713 | end Full_Designated_Type; | |
9714 | ||
d81b4bfe TQ |
9715 | -- Local Declarations |
9716 | ||
0669bebe GB |
9717 | Target : constant Entity_Id := Full_Designated_Type (Target_Type); |
9718 | Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type); | |
9719 | ||
9720 | Same_Base : constant Boolean := | |
9721 | Base_Type (Target) = Base_Type (Opnd); | |
996ae0b0 | 9722 | |
14e33999 | 9723 | -- Start of processing for Check_Limited |
d81b4bfe | 9724 | |
996ae0b0 RK |
9725 | begin |
9726 | if Is_Tagged_Type (Target) then | |
9727 | return Valid_Tagged_Conversion (Target, Opnd); | |
9728 | ||
9729 | else | |
0669bebe | 9730 | if not Same_Base then |
996ae0b0 RK |
9731 | Error_Msg_NE |
9732 | ("target designated type not compatible with }", | |
9733 | N, Base_Type (Opnd)); | |
9734 | return False; | |
9735 | ||
da709d08 AC |
9736 | -- Ada 2005 AI-384: legality rule is symmetric in both |
9737 | -- designated types. The conversion is legal (with possible | |
9738 | -- constraint check) if either designated type is | |
9739 | -- unconstrained. | |
9740 | ||
9741 | elsif Subtypes_Statically_Match (Target, Opnd) | |
9742 | or else | |
9743 | (Has_Discriminants (Target) | |
9744 | and then | |
9745 | (not Is_Constrained (Opnd) | |
9746 | or else not Is_Constrained (Target))) | |
996ae0b0 | 9747 | then |
9fa33291 RD |
9748 | -- Special case, if Value_Size has been used to make the |
9749 | -- sizes different, the conversion is not allowed even | |
9750 | -- though the subtypes statically match. | |
9751 | ||
9752 | if Known_Static_RM_Size (Target) | |
9753 | and then Known_Static_RM_Size (Opnd) | |
9754 | and then RM_Size (Target) /= RM_Size (Opnd) | |
9755 | then | |
9756 | Error_Msg_NE | |
9757 | ("target designated subtype not compatible with }", | |
9758 | N, Opnd); | |
9759 | Error_Msg_NE | |
9760 | ("\because sizes of the two designated subtypes differ", | |
9761 | N, Opnd); | |
9762 | return False; | |
9763 | ||
9764 | -- Normal case where conversion is allowed | |
9765 | ||
9766 | else | |
9767 | return True; | |
9768 | end if; | |
da709d08 AC |
9769 | |
9770 | else | |
996ae0b0 RK |
9771 | Error_Msg_NE |
9772 | ("target designated subtype not compatible with }", | |
9773 | N, Opnd); | |
9774 | return False; | |
996ae0b0 RK |
9775 | end if; |
9776 | end if; | |
14e33999 | 9777 | end Check_Limited; |
996ae0b0 | 9778 | |
cdbf04c0 | 9779 | -- Access to subprogram types. If the operand is an access parameter, |
c147ac26 | 9780 | -- the type has a deeper accessibility that any master, and cannot |
53cf4600 ES |
9781 | -- be assigned. We must make an exception if the conversion is part |
9782 | -- of an assignment and the target is the return object of an extended | |
9783 | -- return statement, because in that case the accessibility check | |
9784 | -- takes place after the return. | |
aa180613 | 9785 | |
dce86910 | 9786 | elsif Is_Access_Subprogram_Type (Target_Type) |
bc5f3720 | 9787 | and then No (Corresponding_Remote_Type (Opnd_Type)) |
996ae0b0 | 9788 | then |
cdbf04c0 AC |
9789 | if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type |
9790 | and then Is_Entity_Name (Operand) | |
9791 | and then Ekind (Entity (Operand)) = E_In_Parameter | |
53cf4600 ES |
9792 | and then |
9793 | (Nkind (Parent (N)) /= N_Assignment_Statement | |
9794 | or else not Is_Entity_Name (Name (Parent (N))) | |
9795 | or else not Is_Return_Object (Entity (Name (Parent (N))))) | |
0669bebe GB |
9796 | then |
9797 | Error_Msg_N | |
9798 | ("illegal attempt to store anonymous access to subprogram", | |
9799 | Operand); | |
9800 | Error_Msg_N | |
9801 | ("\value has deeper accessibility than any master " & | |
aa5147f0 | 9802 | "(RM 3.10.2 (13))", |
0669bebe GB |
9803 | Operand); |
9804 | ||
c147ac26 ES |
9805 | Error_Msg_NE |
9806 | ("\use named access type for& instead of access parameter", | |
9807 | Operand, Entity (Operand)); | |
0669bebe GB |
9808 | end if; |
9809 | ||
996ae0b0 RK |
9810 | -- Check that the designated types are subtype conformant |
9811 | ||
bc5f3720 RD |
9812 | Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type), |
9813 | Old_Id => Designated_Type (Opnd_Type), | |
9814 | Err_Loc => N); | |
996ae0b0 RK |
9815 | |
9816 | -- Check the static accessibility rule of 4.6(20) | |
9817 | ||
9818 | if Type_Access_Level (Opnd_Type) > | |
9819 | Type_Access_Level (Target_Type) | |
9820 | then | |
9821 | Error_Msg_N | |
9822 | ("operand type has deeper accessibility level than target", | |
9823 | Operand); | |
9824 | ||
9825 | -- Check that if the operand type is declared in a generic body, | |
9826 | -- then the target type must be declared within that same body | |
9827 | -- (enforces last sentence of 4.6(20)). | |
9828 | ||
9829 | elsif Present (Enclosing_Generic_Body (Opnd_Type)) then | |
9830 | declare | |
9831 | O_Gen : constant Node_Id := | |
9832 | Enclosing_Generic_Body (Opnd_Type); | |
9833 | ||
1420b484 | 9834 | T_Gen : Node_Id; |
996ae0b0 RK |
9835 | |
9836 | begin | |
1420b484 | 9837 | T_Gen := Enclosing_Generic_Body (Target_Type); |
996ae0b0 RK |
9838 | while Present (T_Gen) and then T_Gen /= O_Gen loop |
9839 | T_Gen := Enclosing_Generic_Body (T_Gen); | |
9840 | end loop; | |
9841 | ||
9842 | if T_Gen /= O_Gen then | |
9843 | Error_Msg_N | |
9844 | ("target type must be declared in same generic body" | |
9845 | & " as operand type", N); | |
9846 | end if; | |
9847 | end; | |
9848 | end if; | |
9849 | ||
9850 | return True; | |
9851 | ||
aa180613 RD |
9852 | -- Remote subprogram access types |
9853 | ||
996ae0b0 RK |
9854 | elsif Is_Remote_Access_To_Subprogram_Type (Target_Type) |
9855 | and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type) | |
9856 | then | |
9857 | -- It is valid to convert from one RAS type to another provided | |
9858 | -- that their specification statically match. | |
9859 | ||
9860 | Check_Subtype_Conformant | |
9861 | (New_Id => | |
9862 | Designated_Type (Corresponding_Remote_Type (Target_Type)), | |
9863 | Old_Id => | |
9864 | Designated_Type (Corresponding_Remote_Type (Opnd_Type)), | |
9865 | Err_Loc => | |
9866 | N); | |
9867 | return True; | |
aa180613 | 9868 | |
e65f50ec | 9869 | -- If both are tagged types, check legality of view conversions |
996ae0b0 | 9870 | |
e65f50ec ES |
9871 | elsif Is_Tagged_Type (Target_Type) |
9872 | and then Is_Tagged_Type (Opnd_Type) | |
9873 | then | |
996ae0b0 RK |
9874 | return Valid_Tagged_Conversion (Target_Type, Opnd_Type); |
9875 | ||
a77842bd | 9876 | -- Types derived from the same root type are convertible |
996ae0b0 RK |
9877 | |
9878 | elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then | |
9879 | return True; | |
9880 | ||
aa5147f0 ES |
9881 | -- In an instance or an inlined body, there may be inconsistent |
9882 | -- views of the same type, or of types derived from a common root. | |
996ae0b0 | 9883 | |
aa5147f0 ES |
9884 | elsif (In_Instance or In_Inlined_Body) |
9885 | and then | |
d81b4bfe TQ |
9886 | Root_Type (Underlying_Type (Target_Type)) = |
9887 | Root_Type (Underlying_Type (Opnd_Type)) | |
996ae0b0 RK |
9888 | then |
9889 | return True; | |
9890 | ||
9891 | -- Special check for common access type error case | |
9892 | ||
9893 | elsif Ekind (Target_Type) = E_Access_Type | |
9894 | and then Is_Access_Type (Opnd_Type) | |
9895 | then | |
9896 | Error_Msg_N ("target type must be general access type!", N); | |
9897 | Error_Msg_NE ("add ALL to }!", N, Target_Type); | |
996ae0b0 RK |
9898 | return False; |
9899 | ||
9900 | else | |
9901 | Error_Msg_NE ("invalid conversion, not compatible with }", | |
9902 | N, Opnd_Type); | |
996ae0b0 RK |
9903 | return False; |
9904 | end if; | |
9905 | end Valid_Conversion; | |
9906 | ||
9907 | end Sem_Res; |