]>
Commit | Line | Data |
---|---|---|
ee6ba406 | 1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ A T T R -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
7189d17f | 9 | -- Copyright (C) 1992-2005, Free Software Foundation, Inc. -- |
ee6ba406 | 10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
13 | -- ware Foundation; either version 2, or (at your option) any later ver- -- | |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- | |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
18 | -- Public License distributed with GNAT; see file COPYING. If not, write -- | |
19 | -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- | |
20 | -- MA 02111-1307, USA. -- | |
21 | -- -- | |
22 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 23 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
ee6ba406 | 24 | -- -- |
25 | ------------------------------------------------------------------------------ | |
26 | ||
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Einfo; use Einfo; | |
00f91aef | 30 | with Elists; use Elists; |
ee6ba406 | 31 | with Exp_Ch2; use Exp_Ch2; |
32 | with Exp_Ch9; use Exp_Ch9; | |
33 | with Exp_Imgv; use Exp_Imgv; | |
34 | with Exp_Pakd; use Exp_Pakd; | |
35 | with Exp_Strm; use Exp_Strm; | |
36 | with Exp_Tss; use Exp_Tss; | |
37 | with Exp_Util; use Exp_Util; | |
38 | with Gnatvsn; use Gnatvsn; | |
39 | with Hostparm; use Hostparm; | |
40 | with Lib; use Lib; | |
41 | with Namet; use Namet; | |
42 | with Nmake; use Nmake; | |
43 | with Nlists; use Nlists; | |
44 | with Opt; use Opt; | |
45 | with Restrict; use Restrict; | |
1e16c51c | 46 | with Rident; use Rident; |
ee6ba406 | 47 | with Rtsfind; use Rtsfind; |
48 | with Sem; use Sem; | |
49 | with Sem_Ch7; use Sem_Ch7; | |
50 | with Sem_Ch8; use Sem_Ch8; | |
ee6ba406 | 51 | with Sem_Eval; use Sem_Eval; |
52 | with Sem_Res; use Sem_Res; | |
53 | with Sem_Util; use Sem_Util; | |
54 | with Sinfo; use Sinfo; | |
55 | with Snames; use Snames; | |
56 | with Stand; use Stand; | |
57 | with Stringt; use Stringt; | |
58 | with Tbuild; use Tbuild; | |
59 | with Ttypes; use Ttypes; | |
60 | with Uintp; use Uintp; | |
61 | with Uname; use Uname; | |
62 | with Validsw; use Validsw; | |
63 | ||
64 | package body Exp_Attr is | |
65 | ||
66 | ----------------------- | |
67 | -- Local Subprograms -- | |
68 | ----------------------- | |
69 | ||
70 | procedure Compile_Stream_Body_In_Scope | |
71 | (N : Node_Id; | |
72 | Decl : Node_Id; | |
73 | Arr : Entity_Id; | |
74 | Check : Boolean); | |
75 | -- The body for a stream subprogram may be generated outside of the scope | |
76 | -- of the type. If the type is fully private, it may depend on the full | |
77 | -- view of other types (e.g. indices) that are currently private as well. | |
78 | -- We install the declarations of the package in which the type is declared | |
79 | -- before compiling the body in what is its proper environment. The Check | |
80 | -- parameter indicates if checks are to be suppressed for the stream body. | |
81 | -- We suppress checks for array/record reads, since the rule is that these | |
82 | -- are like assignments, out of range values due to uninitialized storage, | |
83 | -- or other invalid values do NOT cause a Constraint_Error to be raised. | |
84 | ||
85 | procedure Expand_Fpt_Attribute | |
9dfe12ae | 86 | (N : Node_Id; |
87 | Rtp : Entity_Id; | |
88 | Nam : Name_Id; | |
ee6ba406 | 89 | Args : List_Id); |
90 | -- This procedure expands a call to a floating-point attribute function. | |
91 | -- N is the attribute reference node, and Args is a list of arguments to | |
92 | -- be passed to the function call. Rtp is the root type of the floating | |
93 | -- point type involved (used to select the proper generic instantiation | |
9dfe12ae | 94 | -- of the package containing the attribute routines). The Nam argument |
95 | -- is the attribute processing routine to be called. This is normally | |
96 | -- the same as the attribute name, except in the Unaligned_Valid case. | |
ee6ba406 | 97 | |
98 | procedure Expand_Fpt_Attribute_R (N : Node_Id); | |
99 | -- This procedure expands a call to a floating-point attribute function | |
9dfe12ae | 100 | -- that takes a single floating-point argument. The function to be called |
101 | -- is always the same as the attribute name. | |
ee6ba406 | 102 | |
103 | procedure Expand_Fpt_Attribute_RI (N : Node_Id); | |
104 | -- This procedure expands a call to a floating-point attribute function | |
9dfe12ae | 105 | -- that takes one floating-point argument and one integer argument. The |
106 | -- function to be called is always the same as the attribute name. | |
ee6ba406 | 107 | |
108 | procedure Expand_Fpt_Attribute_RR (N : Node_Id); | |
109 | -- This procedure expands a call to a floating-point attribute function | |
9dfe12ae | 110 | -- that takes two floating-point arguments. The function to be called |
111 | -- is always the same as the attribute name. | |
ee6ba406 | 112 | |
113 | procedure Expand_Pred_Succ (N : Node_Id); | |
114 | -- Handles expansion of Pred or Succ attributes for case of non-real | |
115 | -- operand with overflow checking required. | |
116 | ||
117 | function Get_Index_Subtype (N : Node_Id) return Entity_Id; | |
118 | -- Used for Last, Last, and Length, when the prefix is an array type, | |
119 | -- Obtains the corresponding index subtype. | |
120 | ||
121 | procedure Expand_Access_To_Type (N : Node_Id); | |
122 | -- A reference to a type within its own scope is resolved to a reference | |
123 | -- to the current instance of the type in its initialization procedure. | |
124 | ||
125 | function Find_Inherited_TSS | |
126 | (Typ : Entity_Id; | |
9dfe12ae | 127 | Nam : TSS_Name_Type) return Entity_Id; |
128 | -- Returns the TSS of name Nam of Typ, or of its closest ancestor defining | |
129 | -- such a TSS. Empty is returned is neither Typ nor any of its ancestors | |
130 | -- have such a TSS. | |
131 | ||
132 | function Find_Stream_Subprogram | |
133 | (Typ : Entity_Id; | |
134 | Nam : TSS_Name_Type) return Entity_Id; | |
135 | -- Returns the stream-oriented subprogram attribute for Typ. For tagged | |
136 | -- types, the corresponding primitive operation is looked up, else the | |
137 | -- appropriate TSS from the type itself, or from its closest ancestor | |
138 | -- defining it, is returned. In both cases, inheritance of representation | |
139 | -- aspects is thus taken into account. | |
ee6ba406 | 140 | |
5245b786 | 141 | function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id; |
142 | -- Given a type, find a corresponding stream convert pragma that applies to | |
143 | -- the implementation base type of this type (Typ). If found, return the | |
144 | -- pragma node, otherwise return Empty if no pragma is found. | |
145 | ||
ee6ba406 | 146 | function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean; |
147 | -- Utility for array attributes, returns true on packed constrained | |
148 | -- arrays, and on access to same. | |
149 | ||
150 | ---------------------------------- | |
151 | -- Compile_Stream_Body_In_Scope -- | |
152 | ---------------------------------- | |
153 | ||
154 | procedure Compile_Stream_Body_In_Scope | |
155 | (N : Node_Id; | |
156 | Decl : Node_Id; | |
157 | Arr : Entity_Id; | |
158 | Check : Boolean) | |
159 | is | |
160 | Installed : Boolean := False; | |
161 | Scop : constant Entity_Id := Scope (Arr); | |
162 | Curr : constant Entity_Id := Current_Scope; | |
163 | ||
164 | begin | |
165 | if Is_Hidden (Arr) | |
166 | and then not In_Open_Scopes (Scop) | |
167 | and then Ekind (Scop) = E_Package | |
168 | then | |
169 | New_Scope (Scop); | |
170 | Install_Visible_Declarations (Scop); | |
171 | Install_Private_Declarations (Scop); | |
172 | Installed := True; | |
173 | ||
174 | -- The entities in the package are now visible, but the generated | |
175 | -- stream entity must appear in the current scope (usually an | |
176 | -- enclosing stream function) so that itypes all have their proper | |
177 | -- scopes. | |
178 | ||
179 | New_Scope (Curr); | |
180 | end if; | |
181 | ||
182 | if Check then | |
183 | Insert_Action (N, Decl); | |
184 | else | |
185 | Insert_Action (N, Decl, All_Checks); | |
186 | end if; | |
187 | ||
188 | if Installed then | |
189 | ||
190 | -- Remove extra copy of current scope, and package itself | |
191 | ||
192 | Pop_Scope; | |
193 | End_Package_Scope (Scop); | |
194 | end if; | |
195 | end Compile_Stream_Body_In_Scope; | |
196 | ||
197 | --------------------------- | |
198 | -- Expand_Access_To_Type -- | |
199 | --------------------------- | |
200 | ||
201 | procedure Expand_Access_To_Type (N : Node_Id) is | |
202 | Loc : constant Source_Ptr := Sloc (N); | |
203 | Typ : constant Entity_Id := Etype (N); | |
204 | Pref : constant Node_Id := Prefix (N); | |
205 | Par : Node_Id; | |
206 | Formal : Entity_Id; | |
207 | ||
208 | begin | |
209 | if Is_Entity_Name (Pref) | |
210 | and then Is_Type (Entity (Pref)) | |
211 | then | |
212 | -- If the current instance name denotes a task type, | |
213 | -- then the access attribute is rewritten to be the | |
214 | -- name of the "_task" parameter associated with the | |
215 | -- task type's task body procedure. An unchecked | |
216 | -- conversion is applied to ensure a type match in | |
217 | -- cases of expander-generated calls (e.g., init procs). | |
218 | ||
219 | if Is_Task_Type (Entity (Pref)) then | |
220 | Formal := | |
221 | First_Entity (Get_Task_Body_Procedure (Entity (Pref))); | |
222 | ||
223 | while Present (Formal) loop | |
224 | exit when Chars (Formal) = Name_uTask; | |
225 | Next_Entity (Formal); | |
226 | end loop; | |
227 | ||
228 | pragma Assert (Present (Formal)); | |
229 | ||
230 | Rewrite (N, | |
231 | Unchecked_Convert_To (Typ, New_Occurrence_Of (Formal, Loc))); | |
232 | Set_Etype (N, Typ); | |
233 | ||
234 | -- The expression must appear in a default expression, | |
235 | -- (which in the initialization procedure is the rhs of | |
236 | -- an assignment), and not in a discriminant constraint. | |
237 | ||
238 | else | |
239 | Par := Parent (N); | |
240 | ||
241 | while Present (Par) loop | |
242 | exit when Nkind (Par) = N_Assignment_Statement; | |
243 | ||
244 | if Nkind (Par) = N_Component_Declaration then | |
245 | return; | |
246 | end if; | |
247 | ||
248 | Par := Parent (Par); | |
249 | end loop; | |
250 | ||
251 | if Present (Par) then | |
252 | Rewrite (N, | |
253 | Make_Attribute_Reference (Loc, | |
254 | Prefix => Make_Identifier (Loc, Name_uInit), | |
255 | Attribute_Name => Attribute_Name (N))); | |
256 | ||
257 | Analyze_And_Resolve (N, Typ); | |
258 | end if; | |
259 | end if; | |
260 | end if; | |
261 | end Expand_Access_To_Type; | |
262 | ||
263 | -------------------------- | |
264 | -- Expand_Fpt_Attribute -- | |
265 | -------------------------- | |
266 | ||
267 | procedure Expand_Fpt_Attribute | |
268 | (N : Node_Id; | |
269 | Rtp : Entity_Id; | |
9dfe12ae | 270 | Nam : Name_Id; |
ee6ba406 | 271 | Args : List_Id) |
272 | is | |
273 | Loc : constant Source_Ptr := Sloc (N); | |
274 | Typ : constant Entity_Id := Etype (N); | |
275 | Pkg : RE_Id; | |
276 | Fnm : Node_Id; | |
277 | ||
278 | begin | |
279 | -- The function name is the selected component Fat_xxx.yyy where xxx | |
9dfe12ae | 280 | -- is the floating-point root type, and yyy is the argument Nam. |
ee6ba406 | 281 | |
282 | -- Note: it would be more usual to have separate RE entries for each | |
283 | -- of the entities in the Fat packages, but first they have identical | |
284 | -- names (so we would have to have lots of renaming declarations to | |
285 | -- meet the normal RE rule of separate names for all runtime entities), | |
286 | -- and second there would be an awful lot of them! | |
287 | ||
288 | if Rtp = Standard_Short_Float then | |
289 | Pkg := RE_Fat_Short_Float; | |
290 | elsif Rtp = Standard_Float then | |
291 | Pkg := RE_Fat_Float; | |
292 | elsif Rtp = Standard_Long_Float then | |
293 | Pkg := RE_Fat_Long_Float; | |
294 | else | |
295 | Pkg := RE_Fat_Long_Long_Float; | |
296 | end if; | |
297 | ||
298 | Fnm := | |
299 | Make_Selected_Component (Loc, | |
300 | Prefix => New_Reference_To (RTE (Pkg), Loc), | |
9dfe12ae | 301 | Selector_Name => Make_Identifier (Loc, Nam)); |
ee6ba406 | 302 | |
303 | -- The generated call is given the provided set of parameters, and then | |
304 | -- wrapped in a conversion which converts the result to the target type | |
5245b786 | 305 | -- We use the base type as the target because a range check may be |
306 | -- required. | |
ee6ba406 | 307 | |
308 | Rewrite (N, | |
5245b786 | 309 | Unchecked_Convert_To (Base_Type (Etype (N)), |
ee6ba406 | 310 | Make_Function_Call (Loc, |
311 | Name => Fnm, | |
312 | Parameter_Associations => Args))); | |
313 | ||
314 | Analyze_And_Resolve (N, Typ); | |
ee6ba406 | 315 | end Expand_Fpt_Attribute; |
316 | ||
317 | ---------------------------- | |
318 | -- Expand_Fpt_Attribute_R -- | |
319 | ---------------------------- | |
320 | ||
321 | -- The single argument is converted to its root type to call the | |
322 | -- appropriate runtime function, with the actual call being built | |
323 | -- by Expand_Fpt_Attribute | |
324 | ||
325 | procedure Expand_Fpt_Attribute_R (N : Node_Id) is | |
326 | E1 : constant Node_Id := First (Expressions (N)); | |
327 | Rtp : constant Entity_Id := Root_Type (Etype (E1)); | |
328 | ||
329 | begin | |
9dfe12ae | 330 | Expand_Fpt_Attribute |
331 | (N, Rtp, Attribute_Name (N), | |
332 | New_List (Unchecked_Convert_To (Rtp, Relocate_Node (E1)))); | |
ee6ba406 | 333 | end Expand_Fpt_Attribute_R; |
334 | ||
335 | ----------------------------- | |
336 | -- Expand_Fpt_Attribute_RI -- | |
337 | ----------------------------- | |
338 | ||
339 | -- The first argument is converted to its root type and the second | |
340 | -- argument is converted to standard long long integer to call the | |
341 | -- appropriate runtime function, with the actual call being built | |
342 | -- by Expand_Fpt_Attribute | |
343 | ||
344 | procedure Expand_Fpt_Attribute_RI (N : Node_Id) is | |
345 | E1 : constant Node_Id := First (Expressions (N)); | |
346 | Rtp : constant Entity_Id := Root_Type (Etype (E1)); | |
347 | E2 : constant Node_Id := Next (E1); | |
348 | ||
349 | begin | |
9dfe12ae | 350 | Expand_Fpt_Attribute |
351 | (N, Rtp, Attribute_Name (N), | |
352 | New_List ( | |
353 | Unchecked_Convert_To (Rtp, Relocate_Node (E1)), | |
354 | Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2)))); | |
ee6ba406 | 355 | end Expand_Fpt_Attribute_RI; |
356 | ||
357 | ----------------------------- | |
358 | -- Expand_Fpt_Attribute_RR -- | |
359 | ----------------------------- | |
360 | ||
361 | -- The two arguments is converted to their root types to call the | |
362 | -- appropriate runtime function, with the actual call being built | |
363 | -- by Expand_Fpt_Attribute | |
364 | ||
365 | procedure Expand_Fpt_Attribute_RR (N : Node_Id) is | |
366 | E1 : constant Node_Id := First (Expressions (N)); | |
367 | Rtp : constant Entity_Id := Root_Type (Etype (E1)); | |
368 | E2 : constant Node_Id := Next (E1); | |
369 | ||
370 | begin | |
9dfe12ae | 371 | Expand_Fpt_Attribute |
372 | (N, Rtp, Attribute_Name (N), | |
373 | New_List ( | |
374 | Unchecked_Convert_To (Rtp, Relocate_Node (E1)), | |
375 | Unchecked_Convert_To (Rtp, Relocate_Node (E2)))); | |
ee6ba406 | 376 | end Expand_Fpt_Attribute_RR; |
377 | ||
378 | ---------------------------------- | |
379 | -- Expand_N_Attribute_Reference -- | |
380 | ---------------------------------- | |
381 | ||
382 | procedure Expand_N_Attribute_Reference (N : Node_Id) is | |
383 | Loc : constant Source_Ptr := Sloc (N); | |
384 | Typ : constant Entity_Id := Etype (N); | |
385 | Btyp : constant Entity_Id := Base_Type (Typ); | |
386 | Pref : constant Node_Id := Prefix (N); | |
387 | Exprs : constant List_Id := Expressions (N); | |
388 | Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N)); | |
389 | ||
390 | procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id); | |
391 | -- Rewrites a stream attribute for Read, Write or Output with the | |
392 | -- procedure call. Pname is the entity for the procedure to call. | |
393 | ||
394 | ------------------------------ | |
395 | -- Rewrite_Stream_Proc_Call -- | |
396 | ------------------------------ | |
397 | ||
398 | procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is | |
399 | Item : constant Node_Id := Next (First (Exprs)); | |
9dfe12ae | 400 | Formal : constant Entity_Id := Next_Formal (First_Formal (Pname)); |
401 | Formal_Typ : constant Entity_Id := Etype (Formal); | |
402 | Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter); | |
ee6ba406 | 403 | |
404 | begin | |
9dfe12ae | 405 | -- The expansion depends on Item, the second actual, which is |
406 | -- the object being streamed in or out. | |
407 | ||
408 | -- If the item is a component of a packed array type, and | |
409 | -- a conversion is needed on exit, we introduce a temporary to | |
410 | -- hold the value, because otherwise the packed reference will | |
411 | -- not be properly expanded. | |
412 | ||
413 | if Nkind (Item) = N_Indexed_Component | |
414 | and then Is_Packed (Base_Type (Etype (Prefix (Item)))) | |
415 | and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ) | |
416 | and then Is_Written | |
417 | then | |
418 | declare | |
419 | Temp : constant Entity_Id := | |
420 | Make_Defining_Identifier | |
421 | (Loc, New_Internal_Name ('V')); | |
422 | Decl : Node_Id; | |
423 | Assn : Node_Id; | |
424 | ||
425 | begin | |
426 | Decl := | |
427 | Make_Object_Declaration (Loc, | |
428 | Defining_Identifier => Temp, | |
429 | Object_Definition => | |
430 | New_Occurrence_Of (Formal_Typ, Loc)); | |
431 | Set_Etype (Temp, Formal_Typ); | |
432 | ||
433 | Assn := | |
434 | Make_Assignment_Statement (Loc, | |
435 | Name => New_Copy_Tree (Item), | |
436 | Expression => | |
437 | Unchecked_Convert_To | |
438 | (Etype (Item), New_Occurrence_Of (Temp, Loc))); | |
439 | ||
440 | Rewrite (Item, New_Occurrence_Of (Temp, Loc)); | |
441 | Insert_Actions (N, | |
442 | New_List ( | |
443 | Decl, | |
444 | Make_Procedure_Call_Statement (Loc, | |
445 | Name => New_Occurrence_Of (Pname, Loc), | |
446 | Parameter_Associations => Exprs), | |
447 | Assn)); | |
448 | ||
449 | Rewrite (N, Make_Null_Statement (Loc)); | |
450 | return; | |
451 | end; | |
452 | end if; | |
ee6ba406 | 453 | |
454 | -- For the class-wide dispatching cases, and for cases in which | |
455 | -- the base type of the second argument matches the base type of | |
9dfe12ae | 456 | -- the corresponding formal parameter (that is to say the stream |
457 | -- operation is not inherited), we are all set, and can use the | |
458 | -- argument unchanged. | |
ee6ba406 | 459 | |
460 | -- For all other cases we do an unchecked conversion of the second | |
461 | -- parameter to the type of the formal of the procedure we are | |
462 | -- calling. This deals with the private type cases, and with going | |
463 | -- to the root type as required in elementary type case. | |
464 | ||
465 | if not Is_Class_Wide_Type (Entity (Pref)) | |
9dfe12ae | 466 | and then not Is_Class_Wide_Type (Etype (Item)) |
ee6ba406 | 467 | and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ) |
468 | then | |
469 | Rewrite (Item, | |
470 | Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item))); | |
471 | ||
472 | -- For untagged derived types set Assignment_OK, to prevent | |
473 | -- copies from being created when the unchecked conversion | |
474 | -- is expanded (which would happen in Remove_Side_Effects | |
475 | -- if Expand_N_Unchecked_Conversion were allowed to call | |
476 | -- Force_Evaluation). The copy could violate Ada semantics | |
477 | -- in cases such as an actual that is an out parameter. | |
478 | -- Note that this approach is also used in exp_ch7 for calls | |
479 | -- to controlled type operations to prevent problems with | |
480 | -- actuals wrapped in unchecked conversions. | |
481 | ||
482 | if Is_Untagged_Derivation (Etype (Expression (Item))) then | |
483 | Set_Assignment_OK (Item); | |
484 | end if; | |
485 | end if; | |
486 | ||
487 | -- And now rewrite the call | |
488 | ||
489 | Rewrite (N, | |
490 | Make_Procedure_Call_Statement (Loc, | |
491 | Name => New_Occurrence_Of (Pname, Loc), | |
492 | Parameter_Associations => Exprs)); | |
493 | ||
494 | Analyze (N); | |
495 | end Rewrite_Stream_Proc_Call; | |
496 | ||
497 | -- Start of processing for Expand_N_Attribute_Reference | |
498 | ||
499 | begin | |
7189d17f | 500 | -- Do required validity checking, if enabled. Do not apply check to |
501 | -- output parameters of an Asm instruction, since the value of this | |
502 | -- is not set till after the attribute has been elaborated. | |
ee6ba406 | 503 | |
7189d17f | 504 | if Validity_Checks_On and then Validity_Check_Operands |
505 | and then Id /= Attribute_Asm_Output | |
506 | then | |
ee6ba406 | 507 | declare |
508 | Expr : Node_Id; | |
ee6ba406 | 509 | begin |
510 | Expr := First (Expressions (N)); | |
511 | while Present (Expr) loop | |
512 | Ensure_Valid (Expr); | |
513 | Next (Expr); | |
514 | end loop; | |
515 | end; | |
516 | end if; | |
517 | ||
518 | -- Remaining processing depends on specific attribute | |
519 | ||
520 | case Id is | |
521 | ||
522 | ------------ | |
523 | -- Access -- | |
524 | ------------ | |
525 | ||
526 | when Attribute_Access => | |
527 | ||
528 | if Ekind (Btyp) = E_Access_Protected_Subprogram_Type then | |
529 | ||
530 | -- The value of the attribute_reference is a record containing | |
531 | -- two fields: an access to the protected object, and an access | |
532 | -- to the subprogram itself. The prefix is a selected component. | |
533 | ||
534 | declare | |
535 | Agg : Node_Id; | |
536 | Sub : Entity_Id; | |
f15731c4 | 537 | E_T : constant Entity_Id := Equivalent_Type (Btyp); |
ee6ba406 | 538 | Acc : constant Entity_Id := |
539 | Etype (Next_Component (First_Component (E_T))); | |
540 | Obj_Ref : Node_Id; | |
541 | Curr : Entity_Id; | |
542 | ||
543 | begin | |
544 | -- Within the body of the protected type, the prefix | |
545 | -- designates a local operation, and the object is the first | |
546 | -- parameter of the corresponding protected body of the | |
547 | -- current enclosing operation. | |
548 | ||
549 | if Is_Entity_Name (Pref) then | |
550 | pragma Assert (In_Open_Scopes (Scope (Entity (Pref)))); | |
551 | Sub := | |
552 | New_Occurrence_Of | |
553 | (Protected_Body_Subprogram (Entity (Pref)), Loc); | |
554 | Curr := Current_Scope; | |
555 | ||
556 | while Scope (Curr) /= Scope (Entity (Pref)) loop | |
557 | Curr := Scope (Curr); | |
558 | end loop; | |
559 | ||
560 | Obj_Ref := | |
561 | Make_Attribute_Reference (Loc, | |
562 | Prefix => | |
563 | New_Occurrence_Of | |
564 | (First_Formal | |
565 | (Protected_Body_Subprogram (Curr)), Loc), | |
566 | Attribute_Name => Name_Address); | |
567 | ||
568 | -- Case where the prefix is not an entity name. Find the | |
569 | -- version of the protected operation to be called from | |
570 | -- outside the protected object. | |
571 | ||
572 | else | |
573 | Sub := | |
574 | New_Occurrence_Of | |
575 | (External_Subprogram | |
576 | (Entity (Selector_Name (Pref))), Loc); | |
577 | ||
578 | Obj_Ref := | |
579 | Make_Attribute_Reference (Loc, | |
580 | Prefix => Relocate_Node (Prefix (Pref)), | |
581 | Attribute_Name => Name_Address); | |
582 | end if; | |
583 | ||
584 | Agg := | |
585 | Make_Aggregate (Loc, | |
586 | Expressions => | |
587 | New_List ( | |
588 | Obj_Ref, | |
589 | Unchecked_Convert_To (Acc, | |
590 | Make_Attribute_Reference (Loc, | |
591 | Prefix => Sub, | |
592 | Attribute_Name => Name_Address)))); | |
593 | ||
594 | Rewrite (N, Agg); | |
595 | ||
f15731c4 | 596 | Analyze_And_Resolve (N, E_T); |
ee6ba406 | 597 | |
598 | -- For subsequent analysis, the node must retain its type. | |
599 | -- The backend will replace it with the equivalent type where | |
600 | -- needed. | |
601 | ||
602 | Set_Etype (N, Typ); | |
603 | end; | |
604 | ||
605 | elsif Ekind (Btyp) = E_General_Access_Type then | |
606 | declare | |
607 | Ref_Object : constant Node_Id := Get_Referenced_Object (Pref); | |
608 | Parm_Ent : Entity_Id; | |
609 | Conversion : Node_Id; | |
610 | ||
611 | begin | |
612 | -- If the prefix of an Access attribute is a dereference of an | |
613 | -- access parameter (or a renaming of such a dereference) and | |
614 | -- the context is a general access type (but not an anonymous | |
615 | -- access type), then rewrite the attribute as a conversion of | |
616 | -- the access parameter to the context access type. This will | |
617 | -- result in an accessibility check being performed, if needed. | |
618 | ||
619 | -- (X.all'Access => Acc_Type (X)) | |
620 | ||
621 | if Nkind (Ref_Object) = N_Explicit_Dereference | |
622 | and then Is_Entity_Name (Prefix (Ref_Object)) | |
623 | then | |
624 | Parm_Ent := Entity (Prefix (Ref_Object)); | |
625 | ||
626 | if Ekind (Parm_Ent) in Formal_Kind | |
627 | and then Ekind (Etype (Parm_Ent)) = E_Anonymous_Access_Type | |
628 | and then Present (Extra_Accessibility (Parm_Ent)) | |
629 | then | |
630 | Conversion := | |
631 | Convert_To (Typ, New_Copy_Tree (Prefix (Ref_Object))); | |
632 | ||
633 | Rewrite (N, Conversion); | |
634 | Analyze_And_Resolve (N, Typ); | |
635 | end if; | |
636 | end if; | |
637 | end; | |
638 | ||
639 | -- If the prefix is a type name, this is a reference to the current | |
640 | -- instance of the type, within its initialization procedure. | |
641 | ||
642 | else | |
643 | Expand_Access_To_Type (N); | |
644 | end if; | |
645 | ||
646 | -------------- | |
647 | -- Adjacent -- | |
648 | -------------- | |
649 | ||
650 | -- Transforms 'Adjacent into a call to the floating-point attribute | |
651 | -- function Adjacent in Fat_xxx (where xxx is the root type) | |
652 | ||
653 | when Attribute_Adjacent => | |
654 | Expand_Fpt_Attribute_RR (N); | |
655 | ||
656 | ------------- | |
657 | -- Address -- | |
658 | ------------- | |
659 | ||
660 | when Attribute_Address => Address : declare | |
661 | Task_Proc : Entity_Id; | |
662 | ||
663 | begin | |
664 | -- If the prefix is a task or a task type, the useful address | |
665 | -- is that of the procedure for the task body, i.e. the actual | |
666 | -- program unit. We replace the original entity with that of | |
667 | -- the procedure. | |
668 | ||
669 | if Is_Entity_Name (Pref) | |
670 | and then Is_Task_Type (Entity (Pref)) | |
671 | then | |
672 | Task_Proc := Next_Entity (Root_Type (Etype (Pref))); | |
673 | ||
674 | while Present (Task_Proc) loop | |
675 | exit when Ekind (Task_Proc) = E_Procedure | |
676 | and then Etype (First_Formal (Task_Proc)) = | |
677 | Corresponding_Record_Type (Etype (Pref)); | |
678 | Next_Entity (Task_Proc); | |
679 | end loop; | |
680 | ||
681 | if Present (Task_Proc) then | |
682 | Set_Entity (Pref, Task_Proc); | |
683 | Set_Etype (Pref, Etype (Task_Proc)); | |
684 | end if; | |
685 | ||
686 | -- Similarly, the address of a protected operation is the address | |
687 | -- of the corresponding protected body, regardless of the protected | |
688 | -- object from which it is selected. | |
689 | ||
690 | elsif Nkind (Pref) = N_Selected_Component | |
691 | and then Is_Subprogram (Entity (Selector_Name (Pref))) | |
692 | and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref)))) | |
693 | then | |
694 | Rewrite (Pref, | |
695 | New_Occurrence_Of ( | |
696 | External_Subprogram (Entity (Selector_Name (Pref))), Loc)); | |
697 | ||
698 | elsif Nkind (Pref) = N_Explicit_Dereference | |
699 | and then Ekind (Etype (Pref)) = E_Subprogram_Type | |
700 | and then Convention (Etype (Pref)) = Convention_Protected | |
701 | then | |
702 | -- The prefix is be a dereference of an access_to_protected_ | |
703 | -- subprogram. The desired address is the second component of | |
704 | -- the record that represents the access. | |
705 | ||
706 | declare | |
707 | Addr : constant Entity_Id := Etype (N); | |
708 | Ptr : constant Node_Id := Prefix (Pref); | |
709 | T : constant Entity_Id := | |
710 | Equivalent_Type (Base_Type (Etype (Ptr))); | |
711 | ||
712 | begin | |
713 | Rewrite (N, | |
714 | Unchecked_Convert_To (Addr, | |
715 | Make_Selected_Component (Loc, | |
716 | Prefix => Unchecked_Convert_To (T, Ptr), | |
717 | Selector_Name => New_Occurrence_Of ( | |
718 | Next_Entity (First_Entity (T)), Loc)))); | |
719 | ||
720 | Analyze_And_Resolve (N, Addr); | |
721 | end; | |
722 | end if; | |
723 | ||
724 | -- Deal with packed array reference, other cases are handled by gigi | |
725 | ||
726 | if Involves_Packed_Array_Reference (Pref) then | |
727 | Expand_Packed_Address_Reference (N); | |
728 | end if; | |
729 | end Address; | |
730 | ||
9dfe12ae | 731 | --------------- |
732 | -- Alignment -- | |
733 | --------------- | |
734 | ||
735 | when Attribute_Alignment => Alignment : declare | |
736 | Ptyp : constant Entity_Id := Etype (Pref); | |
737 | New_Node : Node_Id; | |
738 | ||
739 | begin | |
740 | -- For class-wide types, X'Class'Alignment is transformed into a | |
741 | -- direct reference to the Alignment of the class type, so that the | |
742 | -- back end does not have to deal with the X'Class'Alignment | |
743 | -- reference. | |
744 | ||
745 | if Is_Entity_Name (Pref) | |
746 | and then Is_Class_Wide_Type (Entity (Pref)) | |
747 | then | |
748 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
749 | return; | |
750 | ||
751 | -- For x'Alignment applied to an object of a class wide type, | |
752 | -- transform X'Alignment into a call to the predefined primitive | |
753 | -- operation _Alignment applied to X. | |
754 | ||
755 | elsif Is_Class_Wide_Type (Ptyp) then | |
756 | New_Node := | |
757 | Make_Function_Call (Loc, | |
758 | Name => New_Reference_To | |
759 | (Find_Prim_Op (Ptyp, Name_uAlignment), Loc), | |
760 | Parameter_Associations => New_List (Pref)); | |
761 | ||
762 | if Typ /= Standard_Integer then | |
763 | ||
764 | -- The context is a specific integer type with which the | |
765 | -- original attribute was compatible. The function has a | |
766 | -- specific type as well, so to preserve the compatibility | |
767 | -- we must convert explicitly. | |
768 | ||
769 | New_Node := Convert_To (Typ, New_Node); | |
770 | end if; | |
771 | ||
772 | Rewrite (N, New_Node); | |
773 | Analyze_And_Resolve (N, Typ); | |
774 | return; | |
775 | ||
776 | -- For all other cases, we just have to deal with the case of | |
777 | -- the fact that the result can be universal. | |
778 | ||
779 | else | |
780 | Apply_Universal_Integer_Attribute_Checks (N); | |
781 | end if; | |
782 | end Alignment; | |
783 | ||
ee6ba406 | 784 | --------------- |
785 | -- AST_Entry -- | |
786 | --------------- | |
787 | ||
788 | when Attribute_AST_Entry => AST_Entry : declare | |
789 | Ttyp : Entity_Id; | |
790 | T_Id : Node_Id; | |
791 | Eent : Entity_Id; | |
792 | ||
793 | Entry_Ref : Node_Id; | |
794 | -- The reference to the entry or entry family | |
795 | ||
796 | Index : Node_Id; | |
797 | -- The index expression for an entry family reference, or | |
798 | -- the Empty if Entry_Ref references a simple entry. | |
799 | ||
800 | begin | |
801 | if Nkind (Pref) = N_Indexed_Component then | |
802 | Entry_Ref := Prefix (Pref); | |
803 | Index := First (Expressions (Pref)); | |
804 | else | |
805 | Entry_Ref := Pref; | |
806 | Index := Empty; | |
807 | end if; | |
808 | ||
809 | -- Get expression for Task_Id and the entry entity | |
810 | ||
811 | if Nkind (Entry_Ref) = N_Selected_Component then | |
812 | T_Id := | |
813 | Make_Attribute_Reference (Loc, | |
814 | Attribute_Name => Name_Identity, | |
815 | Prefix => Prefix (Entry_Ref)); | |
816 | ||
817 | Ttyp := Etype (Prefix (Entry_Ref)); | |
818 | Eent := Entity (Selector_Name (Entry_Ref)); | |
819 | ||
820 | else | |
821 | T_Id := | |
822 | Make_Function_Call (Loc, | |
823 | Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc)); | |
824 | ||
825 | Eent := Entity (Entry_Ref); | |
826 | ||
827 | -- We have to find the enclosing task to get the task type | |
828 | -- There must be one, since we already validated this earlier | |
829 | ||
830 | Ttyp := Current_Scope; | |
831 | while not Is_Task_Type (Ttyp) loop | |
832 | Ttyp := Scope (Ttyp); | |
833 | end loop; | |
834 | end if; | |
835 | ||
836 | -- Now rewrite the attribute with a call to Create_AST_Handler | |
837 | ||
838 | Rewrite (N, | |
839 | Make_Function_Call (Loc, | |
840 | Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc), | |
841 | Parameter_Associations => New_List ( | |
842 | T_Id, | |
843 | Entry_Index_Expression (Loc, Eent, Index, Ttyp)))); | |
844 | ||
845 | Analyze_And_Resolve (N, RTE (RE_AST_Handler)); | |
846 | end AST_Entry; | |
847 | ||
848 | ------------------ | |
849 | -- Bit_Position -- | |
850 | ------------------ | |
851 | ||
852 | -- We compute this if a component clause was present, otherwise | |
853 | -- we leave the computation up to Gigi, since we don't know what | |
854 | -- layout will be chosen. | |
855 | ||
856 | -- Note that the attribute can apply to a naked record component | |
857 | -- in generated code (i.e. the prefix is an identifier that | |
858 | -- references the component or discriminant entity). | |
859 | ||
860 | when Attribute_Bit_Position => Bit_Position : | |
861 | declare | |
862 | CE : Entity_Id; | |
863 | ||
864 | begin | |
865 | if Nkind (Pref) = N_Identifier then | |
866 | CE := Entity (Pref); | |
867 | else | |
868 | CE := Entity (Selector_Name (Pref)); | |
869 | end if; | |
870 | ||
871 | if Known_Static_Component_Bit_Offset (CE) then | |
872 | Rewrite (N, | |
873 | Make_Integer_Literal (Loc, | |
874 | Intval => Component_Bit_Offset (CE))); | |
875 | Analyze_And_Resolve (N, Typ); | |
876 | ||
877 | else | |
878 | Apply_Universal_Integer_Attribute_Checks (N); | |
879 | end if; | |
880 | end Bit_Position; | |
881 | ||
882 | ------------------ | |
883 | -- Body_Version -- | |
884 | ------------------ | |
885 | ||
886 | -- A reference to P'Body_Version or P'Version is expanded to | |
887 | ||
888 | -- Vnn : Unsigned; | |
889 | -- pragma Import (C, Vnn, "uuuuT"; | |
890 | -- ... | |
891 | -- Get_Version_String (Vnn) | |
892 | ||
893 | -- where uuuu is the unit name (dots replaced by double underscore) | |
894 | -- and T is B for the cases of Body_Version, or Version applied to a | |
895 | -- subprogram acting as its own spec, and S for Version applied to a | |
896 | -- subprogram spec or package. This sequence of code references the | |
897 | -- the unsigned constant created in the main program by the binder. | |
898 | ||
899 | -- A special exception occurs for Standard, where the string | |
900 | -- returned is a copy of the library string in gnatvsn.ads. | |
901 | ||
902 | when Attribute_Body_Version | Attribute_Version => Version : declare | |
903 | E : constant Entity_Id := | |
904 | Make_Defining_Identifier (Loc, New_Internal_Name ('V')); | |
905 | Pent : Entity_Id := Entity (Pref); | |
906 | S : String_Id; | |
907 | ||
908 | begin | |
909 | -- If not library unit, get to containing library unit | |
910 | ||
911 | while Pent /= Standard_Standard | |
912 | and then Scope (Pent) /= Standard_Standard | |
913 | loop | |
914 | Pent := Scope (Pent); | |
915 | end loop; | |
916 | ||
917 | -- Special case Standard | |
918 | ||
919 | if Pent = Standard_Standard | |
920 | or else Pent = Standard_ASCII | |
921 | then | |
ee6ba406 | 922 | Rewrite (N, |
923 | Make_String_Literal (Loc, | |
5245b786 | 924 | Strval => Verbose_Library_Version)); |
ee6ba406 | 925 | |
926 | -- All other cases | |
927 | ||
928 | else | |
929 | -- Build required string constant | |
930 | ||
931 | Get_Name_String (Get_Unit_Name (Pent)); | |
932 | ||
933 | Start_String; | |
934 | for J in 1 .. Name_Len - 2 loop | |
935 | if Name_Buffer (J) = '.' then | |
936 | Store_String_Chars ("__"); | |
937 | else | |
938 | Store_String_Char (Get_Char_Code (Name_Buffer (J))); | |
939 | end if; | |
940 | end loop; | |
941 | ||
942 | -- Case of subprogram acting as its own spec, always use body | |
943 | ||
944 | if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification | |
945 | and then Nkind (Parent (Declaration_Node (Pent))) = | |
946 | N_Subprogram_Body | |
947 | and then Acts_As_Spec (Parent (Declaration_Node (Pent))) | |
948 | then | |
949 | Store_String_Chars ("B"); | |
950 | ||
951 | -- Case of no body present, always use spec | |
952 | ||
953 | elsif not Unit_Requires_Body (Pent) then | |
954 | Store_String_Chars ("S"); | |
955 | ||
956 | -- Otherwise use B for Body_Version, S for spec | |
957 | ||
958 | elsif Id = Attribute_Body_Version then | |
959 | Store_String_Chars ("B"); | |
960 | else | |
961 | Store_String_Chars ("S"); | |
962 | end if; | |
963 | ||
964 | S := End_String; | |
965 | Lib.Version_Referenced (S); | |
966 | ||
967 | -- Insert the object declaration | |
968 | ||
969 | Insert_Actions (N, New_List ( | |
970 | Make_Object_Declaration (Loc, | |
971 | Defining_Identifier => E, | |
972 | Object_Definition => | |
973 | New_Occurrence_Of (RTE (RE_Unsigned), Loc)))); | |
974 | ||
975 | -- Set entity as imported with correct external name | |
976 | ||
977 | Set_Is_Imported (E); | |
978 | Set_Interface_Name (E, Make_String_Literal (Loc, S)); | |
979 | ||
980 | -- And now rewrite original reference | |
981 | ||
982 | Rewrite (N, | |
983 | Make_Function_Call (Loc, | |
984 | Name => New_Reference_To (RTE (RE_Get_Version_String), Loc), | |
985 | Parameter_Associations => New_List ( | |
986 | New_Occurrence_Of (E, Loc)))); | |
987 | end if; | |
988 | ||
989 | Analyze_And_Resolve (N, RTE (RE_Version_String)); | |
990 | end Version; | |
991 | ||
992 | ------------- | |
993 | -- Ceiling -- | |
994 | ------------- | |
995 | ||
996 | -- Transforms 'Ceiling into a call to the floating-point attribute | |
997 | -- function Ceiling in Fat_xxx (where xxx is the root type) | |
998 | ||
999 | when Attribute_Ceiling => | |
1000 | Expand_Fpt_Attribute_R (N); | |
1001 | ||
1002 | -------------- | |
1003 | -- Callable -- | |
1004 | -------------- | |
1005 | ||
1006 | -- Transforms 'Callable attribute into a call to the Callable function. | |
1007 | ||
1008 | when Attribute_Callable => Callable : | |
1009 | begin | |
1010 | Rewrite (N, | |
1011 | Build_Call_With_Task (Pref, RTE (RE_Callable))); | |
1012 | Analyze_And_Resolve (N, Standard_Boolean); | |
1013 | end Callable; | |
1014 | ||
1015 | ------------ | |
1016 | -- Caller -- | |
1017 | ------------ | |
1018 | ||
1019 | -- Transforms 'Caller attribute into a call to either the | |
1020 | -- Task_Entry_Caller or the Protected_Entry_Caller function. | |
1021 | ||
1022 | when Attribute_Caller => Caller : declare | |
7f9be362 | 1023 | Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id); |
9dfe12ae | 1024 | Ent : constant Entity_Id := Entity (Pref); |
1025 | Conctype : constant Entity_Id := Scope (Ent); | |
1026 | Nest_Depth : Integer := 0; | |
ee6ba406 | 1027 | Name : Node_Id; |
1028 | S : Entity_Id; | |
1029 | ||
1030 | begin | |
1031 | -- Protected case | |
1032 | ||
1033 | if Is_Protected_Type (Conctype) then | |
1034 | if Abort_Allowed | |
1e16c51c | 1035 | or else Restriction_Active (No_Entry_Queue) = False |
ee6ba406 | 1036 | or else Number_Entries (Conctype) > 1 |
1037 | then | |
1038 | Name := | |
1039 | New_Reference_To | |
1040 | (RTE (RE_Protected_Entry_Caller), Loc); | |
1041 | else | |
1042 | Name := | |
1043 | New_Reference_To | |
1044 | (RTE (RE_Protected_Single_Entry_Caller), Loc); | |
1045 | end if; | |
1046 | ||
1047 | Rewrite (N, | |
1048 | Unchecked_Convert_To (Id_Kind, | |
1049 | Make_Function_Call (Loc, | |
1050 | Name => Name, | |
1051 | Parameter_Associations => New_List | |
1052 | (New_Reference_To ( | |
1053 | Object_Ref | |
1054 | (Corresponding_Body (Parent (Conctype))), Loc))))); | |
1055 | ||
1056 | -- Task case | |
1057 | ||
1058 | else | |
1059 | -- Determine the nesting depth of the E'Caller attribute, that | |
1060 | -- is, how many accept statements are nested within the accept | |
1061 | -- statement for E at the point of E'Caller. The runtime uses | |
1062 | -- this depth to find the specified entry call. | |
1063 | ||
1064 | for J in reverse 0 .. Scope_Stack.Last loop | |
1065 | S := Scope_Stack.Table (J).Entity; | |
1066 | ||
1067 | -- We should not reach the scope of the entry, as it should | |
1068 | -- already have been checked in Sem_Attr that this attribute | |
1069 | -- reference is within a matching accept statement. | |
1070 | ||
1071 | pragma Assert (S /= Conctype); | |
1072 | ||
1073 | if S = Ent then | |
1074 | exit; | |
1075 | ||
1076 | elsif Is_Entry (S) then | |
1077 | Nest_Depth := Nest_Depth + 1; | |
1078 | end if; | |
1079 | end loop; | |
1080 | ||
1081 | Rewrite (N, | |
1082 | Unchecked_Convert_To (Id_Kind, | |
1083 | Make_Function_Call (Loc, | |
1084 | Name => New_Reference_To ( | |
1085 | RTE (RE_Task_Entry_Caller), Loc), | |
1086 | Parameter_Associations => New_List ( | |
1087 | Make_Integer_Literal (Loc, | |
1088 | Intval => Int (Nest_Depth)))))); | |
1089 | end if; | |
1090 | ||
1091 | Analyze_And_Resolve (N, Id_Kind); | |
1092 | end Caller; | |
1093 | ||
1094 | ------------- | |
1095 | -- Compose -- | |
1096 | ------------- | |
1097 | ||
1098 | -- Transforms 'Compose into a call to the floating-point attribute | |
1099 | -- function Compose in Fat_xxx (where xxx is the root type) | |
1100 | ||
1101 | -- Note: we strictly should have special code here to deal with the | |
1102 | -- case of absurdly negative arguments (less than Integer'First) | |
1103 | -- which will return a (signed) zero value, but it hardly seems | |
1104 | -- worth the effort. Absurdly large positive arguments will raise | |
1105 | -- constraint error which is fine. | |
1106 | ||
1107 | when Attribute_Compose => | |
1108 | Expand_Fpt_Attribute_RI (N); | |
1109 | ||
1110 | ----------------- | |
1111 | -- Constrained -- | |
1112 | ----------------- | |
1113 | ||
1114 | when Attribute_Constrained => Constrained : declare | |
1115 | Formal_Ent : constant Entity_Id := Param_Entity (Pref); | |
1116 | ||
1117 | begin | |
1118 | -- Reference to a parameter where the value is passed as an extra | |
1119 | -- actual, corresponding to the extra formal referenced by the | |
9dfe12ae | 1120 | -- Extra_Constrained field of the corresponding formal. If this |
1121 | -- is an entry in-parameter, it is replaced by a constant renaming | |
1122 | -- for which Extra_Constrained is never created. | |
ee6ba406 | 1123 | |
1124 | if Present (Formal_Ent) | |
9dfe12ae | 1125 | and then Ekind (Formal_Ent) /= E_Constant |
ee6ba406 | 1126 | and then Present (Extra_Constrained (Formal_Ent)) |
1127 | then | |
1128 | Rewrite (N, | |
1129 | New_Occurrence_Of | |
1130 | (Extra_Constrained (Formal_Ent), Sloc (N))); | |
1131 | ||
1132 | -- For variables with a Extra_Constrained field, we use the | |
1133 | -- corresponding entity. | |
1134 | ||
1135 | elsif Nkind (Pref) = N_Identifier | |
1136 | and then Ekind (Entity (Pref)) = E_Variable | |
1137 | and then Present (Extra_Constrained (Entity (Pref))) | |
1138 | then | |
1139 | Rewrite (N, | |
1140 | New_Occurrence_Of | |
1141 | (Extra_Constrained (Entity (Pref)), Sloc (N))); | |
1142 | ||
1143 | -- For all other entity names, we can tell at compile time | |
1144 | ||
1145 | elsif Is_Entity_Name (Pref) then | |
1146 | declare | |
1147 | Ent : constant Entity_Id := Entity (Pref); | |
1148 | Res : Boolean; | |
1149 | ||
1150 | begin | |
1151 | -- (RM J.4) obsolescent cases | |
1152 | ||
1153 | if Is_Type (Ent) then | |
1154 | ||
1155 | -- Private type | |
1156 | ||
1157 | if Is_Private_Type (Ent) then | |
1158 | Res := not Has_Discriminants (Ent) | |
1159 | or else Is_Constrained (Ent); | |
1160 | ||
1161 | -- It not a private type, must be a generic actual type | |
1162 | -- that corresponded to a private type. We know that this | |
1163 | -- correspondence holds, since otherwise the reference | |
1164 | -- within the generic template would have been illegal. | |
1165 | ||
1166 | else | |
9dfe12ae | 1167 | if Is_Composite_Type (Underlying_Type (Ent)) then |
1168 | Res := Is_Constrained (Ent); | |
1169 | else | |
1170 | Res := True; | |
1171 | end if; | |
ee6ba406 | 1172 | end if; |
1173 | ||
1174 | -- If the prefix is not a variable or is aliased, then | |
1175 | -- definitely true; if it's a formal parameter without | |
1176 | -- an associated extra formal, then treat it as constrained. | |
1177 | ||
1178 | elsif not Is_Variable (Pref) | |
1179 | or else Present (Formal_Ent) | |
1180 | or else Is_Aliased_View (Pref) | |
1181 | then | |
1182 | Res := True; | |
1183 | ||
1184 | -- Variable case, just look at type to see if it is | |
1185 | -- constrained. Note that the one case where this is | |
1186 | -- not accurate (the procedure formal case), has been | |
1187 | -- handled above. | |
1188 | ||
1189 | else | |
1190 | Res := Is_Constrained (Etype (Ent)); | |
1191 | end if; | |
1192 | ||
1bbc9831 | 1193 | Rewrite (N, |
1194 | New_Reference_To (Boolean_Literals (Res), Loc)); | |
ee6ba406 | 1195 | end; |
1196 | ||
1197 | -- Prefix is not an entity name. These are also cases where | |
1198 | -- we can always tell at compile time by looking at the form | |
1199 | -- and type of the prefix. | |
1200 | ||
1201 | else | |
1bbc9831 | 1202 | Rewrite (N, |
1203 | New_Reference_To ( | |
1204 | Boolean_Literals ( | |
1205 | not Is_Variable (Pref) | |
1206 | or else Nkind (Pref) = N_Explicit_Dereference | |
1207 | or else Is_Constrained (Etype (Pref))), | |
1208 | Loc)); | |
ee6ba406 | 1209 | end if; |
1210 | ||
1211 | Analyze_And_Resolve (N, Standard_Boolean); | |
1212 | end Constrained; | |
1213 | ||
1214 | --------------- | |
1215 | -- Copy_Sign -- | |
1216 | --------------- | |
1217 | ||
1218 | -- Transforms 'Copy_Sign into a call to the floating-point attribute | |
1219 | -- function Copy_Sign in Fat_xxx (where xxx is the root type) | |
1220 | ||
1221 | when Attribute_Copy_Sign => | |
1222 | Expand_Fpt_Attribute_RR (N); | |
1223 | ||
1224 | ----------- | |
1225 | -- Count -- | |
1226 | ----------- | |
1227 | ||
1228 | -- Transforms 'Count attribute into a call to the Count function | |
1229 | ||
1230 | when Attribute_Count => Count : | |
1231 | declare | |
1232 | Entnam : Node_Id; | |
1233 | Index : Node_Id; | |
1234 | Name : Node_Id; | |
1235 | Call : Node_Id; | |
1236 | Conctyp : Entity_Id; | |
1237 | ||
1238 | begin | |
1239 | -- If the prefix is a member of an entry family, retrieve both | |
1240 | -- entry name and index. For a simple entry there is no index. | |
1241 | ||
1242 | if Nkind (Pref) = N_Indexed_Component then | |
1243 | Entnam := Prefix (Pref); | |
1244 | Index := First (Expressions (Pref)); | |
1245 | else | |
1246 | Entnam := Pref; | |
1247 | Index := Empty; | |
1248 | end if; | |
1249 | ||
1250 | -- Find the concurrent type in which this attribute is referenced | |
1251 | -- (there had better be one). | |
1252 | ||
1253 | Conctyp := Current_Scope; | |
1254 | while not Is_Concurrent_Type (Conctyp) loop | |
1255 | Conctyp := Scope (Conctyp); | |
1256 | end loop; | |
1257 | ||
1258 | -- Protected case | |
1259 | ||
1260 | if Is_Protected_Type (Conctyp) then | |
1261 | ||
1262 | if Abort_Allowed | |
1e16c51c | 1263 | or else Restriction_Active (No_Entry_Queue) = False |
ee6ba406 | 1264 | or else Number_Entries (Conctyp) > 1 |
1265 | then | |
1266 | Name := New_Reference_To (RTE (RE_Protected_Count), Loc); | |
1267 | ||
1268 | Call := | |
1269 | Make_Function_Call (Loc, | |
1270 | Name => Name, | |
1271 | Parameter_Associations => New_List ( | |
1272 | New_Reference_To ( | |
1273 | Object_Ref ( | |
1274 | Corresponding_Body (Parent (Conctyp))), Loc), | |
1275 | Entry_Index_Expression ( | |
1276 | Loc, Entity (Entnam), Index, Scope (Entity (Entnam))))); | |
1277 | else | |
1278 | Name := New_Reference_To (RTE (RE_Protected_Count_Entry), Loc); | |
1279 | ||
1280 | Call := Make_Function_Call (Loc, | |
1281 | Name => Name, | |
1282 | Parameter_Associations => New_List ( | |
1283 | New_Reference_To ( | |
1284 | Object_Ref ( | |
1285 | Corresponding_Body (Parent (Conctyp))), Loc))); | |
1286 | end if; | |
1287 | ||
1288 | -- Task case | |
1289 | ||
1290 | else | |
1291 | Call := | |
1292 | Make_Function_Call (Loc, | |
1293 | Name => New_Reference_To (RTE (RE_Task_Count), Loc), | |
1294 | Parameter_Associations => New_List ( | |
1295 | Entry_Index_Expression | |
1296 | (Loc, Entity (Entnam), Index, Scope (Entity (Entnam))))); | |
1297 | end if; | |
1298 | ||
1299 | -- The call returns type Natural but the context is universal integer | |
1300 | -- so any integer type is allowed. The attribute was already resolved | |
1301 | -- so its Etype is the required result type. If the base type of the | |
1302 | -- context type is other than Standard.Integer we put in a conversion | |
1303 | -- to the required type. This can be a normal typed conversion since | |
1304 | -- both input and output types of the conversion are integer types | |
1305 | ||
1306 | if Base_Type (Typ) /= Base_Type (Standard_Integer) then | |
1307 | Rewrite (N, Convert_To (Typ, Call)); | |
1308 | else | |
1309 | Rewrite (N, Call); | |
1310 | end if; | |
1311 | ||
1312 | Analyze_And_Resolve (N, Typ); | |
1313 | end Count; | |
1314 | ||
1315 | --------------- | |
1316 | -- Elab_Body -- | |
1317 | --------------- | |
1318 | ||
1319 | -- This processing is shared by Elab_Spec | |
1320 | ||
1321 | -- What we do is to insert the following declarations | |
1322 | ||
1323 | -- procedure tnn; | |
1324 | -- pragma Import (C, enn, "name___elabb/s"); | |
1325 | ||
1326 | -- and then the Elab_Body/Spec attribute is replaced by a reference | |
1327 | -- to this defining identifier. | |
1328 | ||
1329 | when Attribute_Elab_Body | | |
1330 | Attribute_Elab_Spec => | |
1331 | ||
1332 | Elab_Body : declare | |
1333 | Ent : constant Entity_Id := | |
1334 | Make_Defining_Identifier (Loc, | |
1335 | New_Internal_Name ('E')); | |
1336 | Str : String_Id; | |
1337 | Lang : Node_Id; | |
1338 | ||
1339 | procedure Make_Elab_String (Nod : Node_Id); | |
1340 | -- Given Nod, an identifier, or a selected component, put the | |
1341 | -- image into the current string literal, with double underline | |
1342 | -- between components. | |
1343 | ||
1344 | procedure Make_Elab_String (Nod : Node_Id) is | |
1345 | begin | |
1346 | if Nkind (Nod) = N_Selected_Component then | |
1347 | Make_Elab_String (Prefix (Nod)); | |
1348 | if Java_VM then | |
1349 | Store_String_Char ('$'); | |
1350 | else | |
1351 | Store_String_Char ('_'); | |
1352 | Store_String_Char ('_'); | |
1353 | end if; | |
1354 | ||
1355 | Get_Name_String (Chars (Selector_Name (Nod))); | |
1356 | ||
1357 | else | |
1358 | pragma Assert (Nkind (Nod) = N_Identifier); | |
1359 | Get_Name_String (Chars (Nod)); | |
1360 | end if; | |
1361 | ||
1362 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
1363 | end Make_Elab_String; | |
1364 | ||
1365 | -- Start of processing for Elab_Body/Elab_Spec | |
1366 | ||
1367 | begin | |
1368 | -- First we need to prepare the string literal for the name of | |
1369 | -- the elaboration routine to be referenced. | |
1370 | ||
1371 | Start_String; | |
1372 | Make_Elab_String (Pref); | |
1373 | ||
1374 | if Java_VM then | |
1375 | Store_String_Chars ("._elab"); | |
1376 | Lang := Make_Identifier (Loc, Name_Ada); | |
1377 | else | |
1378 | Store_String_Chars ("___elab"); | |
1379 | Lang := Make_Identifier (Loc, Name_C); | |
1380 | end if; | |
1381 | ||
1382 | if Id = Attribute_Elab_Body then | |
1383 | Store_String_Char ('b'); | |
1384 | else | |
1385 | Store_String_Char ('s'); | |
1386 | end if; | |
1387 | ||
1388 | Str := End_String; | |
1389 | ||
1390 | Insert_Actions (N, New_List ( | |
1391 | Make_Subprogram_Declaration (Loc, | |
1392 | Specification => | |
1393 | Make_Procedure_Specification (Loc, | |
1394 | Defining_Unit_Name => Ent)), | |
1395 | ||
1396 | Make_Pragma (Loc, | |
1397 | Chars => Name_Import, | |
1398 | Pragma_Argument_Associations => New_List ( | |
1399 | Make_Pragma_Argument_Association (Loc, | |
1400 | Expression => Lang), | |
1401 | ||
1402 | Make_Pragma_Argument_Association (Loc, | |
1403 | Expression => | |
1404 | Make_Identifier (Loc, Chars (Ent))), | |
1405 | ||
1406 | Make_Pragma_Argument_Association (Loc, | |
1407 | Expression => | |
1408 | Make_String_Literal (Loc, Str)))))); | |
1409 | ||
1410 | Set_Entity (N, Ent); | |
1411 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
1412 | end Elab_Body; | |
1413 | ||
1414 | ---------------- | |
1415 | -- Elaborated -- | |
1416 | ---------------- | |
1417 | ||
1418 | -- Elaborated is always True for preelaborated units, predefined | |
1419 | -- units, pure units and units which have Elaborate_Body pragmas. | |
1420 | -- These units have no elaboration entity. | |
1421 | ||
1422 | -- Note: The Elaborated attribute is never passed through to Gigi | |
1423 | ||
1424 | when Attribute_Elaborated => Elaborated : declare | |
1425 | Ent : constant Entity_Id := Entity (Pref); | |
1426 | ||
1427 | begin | |
1428 | if Present (Elaboration_Entity (Ent)) then | |
1429 | Rewrite (N, | |
1430 | New_Occurrence_Of (Elaboration_Entity (Ent), Loc)); | |
1431 | else | |
1432 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
1433 | end if; | |
1434 | end Elaborated; | |
1435 | ||
1436 | -------------- | |
1437 | -- Enum_Rep -- | |
1438 | -------------- | |
1439 | ||
1440 | when Attribute_Enum_Rep => Enum_Rep : | |
1441 | begin | |
1442 | -- X'Enum_Rep (Y) expands to | |
1443 | ||
1444 | -- target-type (Y) | |
1445 | ||
1446 | -- This is simply a direct conversion from the enumeration type | |
1447 | -- to the target integer type, which is treated by Gigi as a normal | |
1448 | -- integer conversion, treating the enumeration type as an integer, | |
1449 | -- which is exactly what we want! We set Conversion_OK to make sure | |
1450 | -- that the analyzer does not complain about what otherwise might | |
1451 | -- be an illegal conversion. | |
1452 | ||
1453 | if Is_Non_Empty_List (Exprs) then | |
1454 | Rewrite (N, | |
1455 | OK_Convert_To (Typ, Relocate_Node (First (Exprs)))); | |
1456 | ||
1457 | -- X'Enum_Rep where X is an enumeration literal is replaced by | |
1458 | -- the literal value. | |
1459 | ||
1460 | elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then | |
1461 | Rewrite (N, | |
1462 | Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref)))); | |
1463 | ||
9dfe12ae | 1464 | -- If this is a renaming of a literal, recover the representation |
1465 | -- of the original. | |
1466 | ||
1467 | elsif Ekind (Entity (Pref)) = E_Constant | |
1468 | and then Present (Renamed_Object (Entity (Pref))) | |
1469 | and then | |
1470 | Ekind (Entity (Renamed_Object (Entity (Pref)))) | |
1471 | = E_Enumeration_Literal | |
1472 | then | |
1473 | Rewrite (N, | |
1474 | Make_Integer_Literal (Loc, | |
1475 | Enumeration_Rep (Entity (Renamed_Object (Entity (Pref)))))); | |
1476 | ||
ee6ba406 | 1477 | -- X'Enum_Rep where X is an object does a direct unchecked conversion |
1478 | -- of the object value, as described for the type case above. | |
1479 | ||
1480 | else | |
1481 | Rewrite (N, | |
1482 | OK_Convert_To (Typ, Relocate_Node (Pref))); | |
1483 | end if; | |
1484 | ||
1485 | Set_Etype (N, Typ); | |
1486 | Analyze_And_Resolve (N, Typ); | |
1487 | ||
1488 | end Enum_Rep; | |
1489 | ||
1490 | -------------- | |
1491 | -- Exponent -- | |
1492 | -------------- | |
1493 | ||
1494 | -- Transforms 'Exponent into a call to the floating-point attribute | |
1495 | -- function Exponent in Fat_xxx (where xxx is the root type) | |
1496 | ||
1497 | when Attribute_Exponent => | |
1498 | Expand_Fpt_Attribute_R (N); | |
1499 | ||
1500 | ------------------ | |
1501 | -- External_Tag -- | |
1502 | ------------------ | |
1503 | ||
1504 | -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag) | |
1505 | ||
1506 | when Attribute_External_Tag => External_Tag : | |
1507 | begin | |
1508 | Rewrite (N, | |
1509 | Make_Function_Call (Loc, | |
1510 | Name => New_Reference_To (RTE (RE_External_Tag), Loc), | |
1511 | Parameter_Associations => New_List ( | |
1512 | Make_Attribute_Reference (Loc, | |
1513 | Attribute_Name => Name_Tag, | |
1514 | Prefix => Prefix (N))))); | |
1515 | ||
1516 | Analyze_And_Resolve (N, Standard_String); | |
1517 | end External_Tag; | |
1518 | ||
1519 | ----------- | |
1520 | -- First -- | |
1521 | ----------- | |
1522 | ||
1523 | when Attribute_First => declare | |
1524 | Ptyp : constant Entity_Id := Etype (Pref); | |
1525 | ||
1526 | begin | |
1527 | -- If the prefix type is a constrained packed array type which | |
1528 | -- already has a Packed_Array_Type representation defined, then | |
1529 | -- replace this attribute with a direct reference to 'First of the | |
1530 | -- appropriate index subtype (since otherwise Gigi will try to give | |
1531 | -- us the value of 'First for this implementation type). | |
1532 | ||
1533 | if Is_Constrained_Packed_Array (Ptyp) then | |
1534 | Rewrite (N, | |
1535 | Make_Attribute_Reference (Loc, | |
1536 | Attribute_Name => Name_First, | |
1537 | Prefix => New_Reference_To (Get_Index_Subtype (N), Loc))); | |
1538 | Analyze_And_Resolve (N, Typ); | |
1539 | ||
1540 | elsif Is_Access_Type (Ptyp) then | |
1541 | Apply_Access_Check (N); | |
1542 | end if; | |
1543 | end; | |
1544 | ||
1545 | --------------- | |
1546 | -- First_Bit -- | |
1547 | --------------- | |
1548 | ||
1549 | -- We compute this if a component clause was present, otherwise | |
1550 | -- we leave the computation up to Gigi, since we don't know what | |
1551 | -- layout will be chosen. | |
1552 | ||
1553 | when Attribute_First_Bit => First_Bit : | |
1554 | declare | |
1555 | CE : constant Entity_Id := Entity (Selector_Name (Pref)); | |
1556 | ||
1557 | begin | |
1558 | if Known_Static_Component_Bit_Offset (CE) then | |
1559 | Rewrite (N, | |
1560 | Make_Integer_Literal (Loc, | |
1561 | Component_Bit_Offset (CE) mod System_Storage_Unit)); | |
1562 | ||
1563 | Analyze_And_Resolve (N, Typ); | |
1564 | ||
1565 | else | |
1566 | Apply_Universal_Integer_Attribute_Checks (N); | |
1567 | end if; | |
1568 | end First_Bit; | |
1569 | ||
1570 | ----------------- | |
1571 | -- Fixed_Value -- | |
1572 | ----------------- | |
1573 | ||
1574 | -- We transform: | |
1575 | ||
1576 | -- fixtype'Fixed_Value (integer-value) | |
1577 | ||
1578 | -- into | |
1579 | ||
1580 | -- fixtype(integer-value) | |
1581 | ||
1582 | -- we do all the required analysis of the conversion here, because | |
1583 | -- we do not want this to go through the fixed-point conversion | |
1584 | -- circuits. Note that gigi always treats fixed-point as equivalent | |
1585 | -- to the corresponding integer type anyway. | |
1586 | ||
1587 | when Attribute_Fixed_Value => Fixed_Value : | |
1588 | begin | |
1589 | Rewrite (N, | |
1590 | Make_Type_Conversion (Loc, | |
1591 | Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc), | |
1592 | Expression => Relocate_Node (First (Exprs)))); | |
1593 | Set_Etype (N, Entity (Pref)); | |
1594 | Set_Analyzed (N); | |
9dfe12ae | 1595 | |
1596 | -- Note: it might appear that a properly analyzed unchecked conversion | |
1597 | -- would be just fine here, but that's not the case, since the full | |
1598 | -- range checks performed by the following call are critical! | |
1599 | ||
ee6ba406 | 1600 | Apply_Type_Conversion_Checks (N); |
1601 | end Fixed_Value; | |
1602 | ||
1603 | ----------- | |
1604 | -- Floor -- | |
1605 | ----------- | |
1606 | ||
1607 | -- Transforms 'Floor into a call to the floating-point attribute | |
1608 | -- function Floor in Fat_xxx (where xxx is the root type) | |
1609 | ||
1610 | when Attribute_Floor => | |
1611 | Expand_Fpt_Attribute_R (N); | |
1612 | ||
1613 | ---------- | |
1614 | -- Fore -- | |
1615 | ---------- | |
1616 | ||
1617 | -- For the fixed-point type Typ: | |
1618 | ||
1619 | -- Typ'Fore | |
1620 | ||
1621 | -- expands into | |
1622 | ||
1623 | -- Result_Type (System.Fore (Long_Long_Float (Type'First)), | |
1624 | -- Long_Long_Float (Type'Last)) | |
1625 | ||
1626 | -- Note that we know that the type is a non-static subtype, or Fore | |
1627 | -- would have itself been computed dynamically in Eval_Attribute. | |
1628 | ||
1629 | when Attribute_Fore => Fore : | |
1630 | declare | |
1631 | Ptyp : constant Entity_Id := Etype (Pref); | |
1632 | ||
1633 | begin | |
1634 | Rewrite (N, | |
1635 | Convert_To (Typ, | |
1636 | Make_Function_Call (Loc, | |
1637 | Name => New_Reference_To (RTE (RE_Fore), Loc), | |
1638 | ||
1639 | Parameter_Associations => New_List ( | |
1640 | Convert_To (Standard_Long_Long_Float, | |
1641 | Make_Attribute_Reference (Loc, | |
1642 | Prefix => New_Reference_To (Ptyp, Loc), | |
1643 | Attribute_Name => Name_First)), | |
1644 | ||
1645 | Convert_To (Standard_Long_Long_Float, | |
1646 | Make_Attribute_Reference (Loc, | |
1647 | Prefix => New_Reference_To (Ptyp, Loc), | |
1648 | Attribute_Name => Name_Last)))))); | |
1649 | ||
1650 | Analyze_And_Resolve (N, Typ); | |
1651 | end Fore; | |
1652 | ||
1653 | -------------- | |
1654 | -- Fraction -- | |
1655 | -------------- | |
1656 | ||
1657 | -- Transforms 'Fraction into a call to the floating-point attribute | |
1658 | -- function Fraction in Fat_xxx (where xxx is the root type) | |
1659 | ||
1660 | when Attribute_Fraction => | |
1661 | Expand_Fpt_Attribute_R (N); | |
1662 | ||
1663 | -------------- | |
1664 | -- Identity -- | |
1665 | -------------- | |
1666 | ||
1667 | -- For an exception returns a reference to the exception data: | |
1668 | -- Exception_Id!(Prefix'Reference) | |
1669 | ||
1670 | -- For a task it returns a reference to the _task_id component of | |
1671 | -- corresponding record: | |
1672 | ||
7f9be362 | 1673 | -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined |
ee6ba406 | 1674 | |
1675 | -- in Ada.Task_Identification. | |
1676 | ||
1677 | when Attribute_Identity => Identity : declare | |
1678 | Id_Kind : Entity_Id; | |
1679 | ||
1680 | begin | |
1681 | if Etype (Pref) = Standard_Exception_Type then | |
1682 | Id_Kind := RTE (RE_Exception_Id); | |
1683 | ||
1684 | if Present (Renamed_Object (Entity (Pref))) then | |
1685 | Set_Entity (Pref, Renamed_Object (Entity (Pref))); | |
1686 | end if; | |
1687 | ||
1688 | Rewrite (N, | |
1689 | Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref))); | |
1690 | else | |
7f9be362 | 1691 | Id_Kind := RTE (RO_AT_Task_Id); |
ee6ba406 | 1692 | |
1693 | Rewrite (N, | |
1694 | Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref))); | |
1695 | end if; | |
1696 | ||
1697 | Analyze_And_Resolve (N, Id_Kind); | |
1698 | end Identity; | |
1699 | ||
1700 | ----------- | |
1701 | -- Image -- | |
1702 | ----------- | |
1703 | ||
1704 | -- Image attribute is handled in separate unit Exp_Imgv | |
1705 | ||
1706 | when Attribute_Image => | |
1707 | Exp_Imgv.Expand_Image_Attribute (N); | |
1708 | ||
1709 | --------- | |
1710 | -- Img -- | |
1711 | --------- | |
1712 | ||
1713 | -- X'Img is expanded to typ'Image (X), where typ is the type of X | |
1714 | ||
1715 | when Attribute_Img => Img : | |
1716 | begin | |
1717 | Rewrite (N, | |
1718 | Make_Attribute_Reference (Loc, | |
1719 | Prefix => New_Reference_To (Etype (Pref), Loc), | |
1720 | Attribute_Name => Name_Image, | |
1721 | Expressions => New_List (Relocate_Node (Pref)))); | |
1722 | ||
1723 | Analyze_And_Resolve (N, Standard_String); | |
1724 | end Img; | |
1725 | ||
1726 | ----------- | |
1727 | -- Input -- | |
1728 | ----------- | |
1729 | ||
1730 | when Attribute_Input => Input : declare | |
1731 | P_Type : constant Entity_Id := Entity (Pref); | |
1732 | B_Type : constant Entity_Id := Base_Type (P_Type); | |
1733 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
1734 | Strm : constant Node_Id := First (Exprs); | |
1735 | Fname : Entity_Id; | |
1736 | Decl : Node_Id; | |
1737 | Call : Node_Id; | |
1738 | Prag : Node_Id; | |
1739 | Arg2 : Node_Id; | |
1740 | Rfunc : Node_Id; | |
1741 | ||
1742 | Cntrl : Node_Id := Empty; | |
1743 | -- Value for controlling argument in call. Always Empty except in | |
1744 | -- the dispatching (class-wide type) case, where it is a reference | |
1745 | -- to the dummy object initialized to the right internal tag. | |
1746 | ||
d53a018a | 1747 | procedure Freeze_Stream_Subprogram (F : Entity_Id); |
1748 | -- The expansion of the attribute reference may generate a call to | |
1749 | -- a user-defined stream subprogram that is frozen by the call. This | |
1750 | -- can lead to access-before-elaboration problem if the reference | |
1751 | -- appears in an object declaration and the subprogram body has not | |
1752 | -- been seen. The freezing of the subprogram requires special code | |
1753 | -- because it appears in an expanded context where expressions do | |
1754 | -- not freeze their constituents. | |
1755 | ||
1756 | ------------------------------ | |
1757 | -- Freeze_Stream_Subprogram -- | |
1758 | ------------------------------ | |
1759 | ||
1760 | procedure Freeze_Stream_Subprogram (F : Entity_Id) is | |
1761 | Decl : constant Node_Id := Unit_Declaration_Node (F); | |
1762 | Bod : Node_Id; | |
1763 | ||
1764 | begin | |
1765 | -- If this is user-defined subprogram, the corresponding | |
1766 | -- stream function appears as a renaming-as-body, and the | |
1767 | -- user subprogram must be retrieved by tree traversal. | |
1768 | ||
1769 | if Present (Decl) | |
1770 | and then Nkind (Decl) = N_Subprogram_Declaration | |
1771 | and then Present (Corresponding_Body (Decl)) | |
1772 | then | |
1773 | Bod := Corresponding_Body (Decl); | |
1774 | ||
1775 | if Nkind (Unit_Declaration_Node (Bod)) = | |
1776 | N_Subprogram_Renaming_Declaration | |
1777 | then | |
1778 | Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod)))); | |
1779 | end if; | |
1780 | end if; | |
1781 | end Freeze_Stream_Subprogram; | |
1782 | ||
1783 | -- Start of processing for Input | |
1784 | ||
ee6ba406 | 1785 | begin |
1786 | -- If no underlying type, we have an error that will be diagnosed | |
1787 | -- elsewhere, so here we just completely ignore the expansion. | |
1788 | ||
1789 | if No (U_Type) then | |
1790 | return; | |
1791 | end if; | |
1792 | ||
1793 | -- If there is a TSS for Input, just call it | |
1794 | ||
9dfe12ae | 1795 | Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input); |
ee6ba406 | 1796 | |
1797 | if Present (Fname) then | |
1798 | null; | |
1799 | ||
1800 | else | |
1801 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
1802 | ||
1803 | -- sourcetyp'Input (stream) | |
1804 | ||
1805 | -- as | |
1806 | ||
1807 | -- sourcetyp (streamread (strmtyp'Input (stream))); | |
1808 | ||
1809 | -- where stmrearead is the given Read function that converts | |
1810 | -- an argument of type strmtyp to type sourcetyp or a type | |
1811 | -- from which it is derived. The extra conversion is required | |
1812 | -- for the derived case. | |
1813 | ||
5245b786 | 1814 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 1815 | |
1816 | if Present (Prag) then | |
1817 | Arg2 := Next (First (Pragma_Argument_Associations (Prag))); | |
1818 | Rfunc := Entity (Expression (Arg2)); | |
1819 | ||
1820 | Rewrite (N, | |
1821 | Convert_To (B_Type, | |
1822 | Make_Function_Call (Loc, | |
1823 | Name => New_Occurrence_Of (Rfunc, Loc), | |
1824 | Parameter_Associations => New_List ( | |
1825 | Make_Attribute_Reference (Loc, | |
1826 | Prefix => | |
1827 | New_Occurrence_Of | |
1828 | (Etype (First_Formal (Rfunc)), Loc), | |
1829 | Attribute_Name => Name_Input, | |
1830 | Expressions => Exprs))))); | |
1831 | ||
1832 | Analyze_And_Resolve (N, B_Type); | |
1833 | return; | |
1834 | ||
1835 | -- Elementary types | |
1836 | ||
1837 | elsif Is_Elementary_Type (U_Type) then | |
1838 | ||
1839 | -- A special case arises if we have a defined _Read routine, | |
1840 | -- since in this case we are required to call this routine. | |
1841 | ||
9dfe12ae | 1842 | if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then |
ee6ba406 | 1843 | Build_Record_Or_Elementary_Input_Function |
1844 | (Loc, U_Type, Decl, Fname); | |
1845 | Insert_Action (N, Decl); | |
1846 | ||
1847 | -- For normal cases, we call the I_xxx routine directly | |
1848 | ||
1849 | else | |
1850 | Rewrite (N, Build_Elementary_Input_Call (N)); | |
1851 | Analyze_And_Resolve (N, P_Type); | |
1852 | return; | |
1853 | end if; | |
1854 | ||
1855 | -- Array type case | |
1856 | ||
1857 | elsif Is_Array_Type (U_Type) then | |
1858 | Build_Array_Input_Function (Loc, U_Type, Decl, Fname); | |
1859 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
1860 | ||
1861 | -- Dispatching case with class-wide type | |
1862 | ||
1863 | elsif Is_Class_Wide_Type (P_Type) then | |
1864 | ||
1865 | declare | |
1866 | Rtyp : constant Entity_Id := Root_Type (P_Type); | |
1867 | Dnn : Entity_Id; | |
1868 | Decl : Node_Id; | |
1869 | ||
1870 | begin | |
1871 | -- Read the internal tag (RM 13.13.2(34)) and use it to | |
1872 | -- initialize a dummy tag object: | |
1873 | ||
1874 | -- Dnn : Ada.Tags.Tag | |
1875 | -- := Internal_Tag (String'Input (Strm)); | |
1876 | ||
1877 | -- This dummy object is used only to provide a controlling | |
1878 | -- argument for the eventual _Input call. | |
1879 | ||
1880 | Dnn := | |
1881 | Make_Defining_Identifier (Loc, | |
1882 | Chars => New_Internal_Name ('D')); | |
1883 | ||
1884 | Decl := | |
1885 | Make_Object_Declaration (Loc, | |
1886 | Defining_Identifier => Dnn, | |
1887 | Object_Definition => | |
1888 | New_Occurrence_Of (RTE (RE_Tag), Loc), | |
1889 | Expression => | |
1890 | Make_Function_Call (Loc, | |
1891 | Name => | |
1892 | New_Occurrence_Of (RTE (RE_Internal_Tag), Loc), | |
1893 | Parameter_Associations => New_List ( | |
1894 | Make_Attribute_Reference (Loc, | |
1895 | Prefix => | |
1896 | New_Occurrence_Of (Standard_String, Loc), | |
1897 | Attribute_Name => Name_Input, | |
1898 | Expressions => New_List ( | |
1899 | Relocate_Node | |
1900 | (Duplicate_Subexpr (Strm))))))); | |
1901 | ||
1902 | Insert_Action (N, Decl); | |
1903 | ||
1904 | -- Now we need to get the entity for the call, and construct | |
1905 | -- a function call node, where we preset a reference to Dnn | |
1906 | -- as the controlling argument (doing an unchecked | |
7189d17f | 1907 | -- conversion to the class-wide tagged type to make it |
9dfe12ae | 1908 | -- look like a real tagged object). |
ee6ba406 | 1909 | |
9dfe12ae | 1910 | Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input); |
1911 | Cntrl := Unchecked_Convert_To (P_Type, | |
ee6ba406 | 1912 | New_Occurrence_Of (Dnn, Loc)); |
9dfe12ae | 1913 | Set_Etype (Cntrl, P_Type); |
ee6ba406 | 1914 | Set_Parent (Cntrl, N); |
1915 | end; | |
1916 | ||
1917 | -- For tagged types, use the primitive Input function | |
1918 | ||
1919 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 1920 | Fname := Find_Prim_Op (U_Type, TSS_Stream_Input); |
ee6ba406 | 1921 | |
1922 | -- All other record type cases, including protected records. | |
1923 | -- The latter only arise for expander generated code for | |
1924 | -- handling shared passive partition access. | |
1925 | ||
1926 | else | |
1927 | pragma Assert | |
1928 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
1929 | ||
00f91aef | 1930 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
1931 | -- the default implementation of the Input attribute of an | |
1932 | -- unchecked union type if the type lacks default discriminant | |
1933 | -- values. | |
1934 | ||
1935 | if Is_Unchecked_Union (Base_Type (U_Type)) | |
1936 | and then not Present (Discriminant_Constraint (U_Type)) | |
1937 | then | |
1938 | Insert_Action (N, | |
1939 | Make_Raise_Program_Error (Loc, | |
1940 | Reason => PE_Unchecked_Union_Restriction)); | |
1941 | ||
1942 | return; | |
1943 | end if; | |
1944 | ||
ee6ba406 | 1945 | Build_Record_Or_Elementary_Input_Function |
1946 | (Loc, Base_Type (U_Type), Decl, Fname); | |
1947 | Insert_Action (N, Decl); | |
d53a018a | 1948 | |
1949 | if Nkind (Parent (N)) = N_Object_Declaration | |
1950 | and then Is_Record_Type (U_Type) | |
1951 | then | |
1952 | -- The stream function may contain calls to user-defined | |
1953 | -- Read procedures for individual components. | |
1954 | ||
1955 | declare | |
1956 | Comp : Entity_Id; | |
1957 | Func : Entity_Id; | |
1958 | ||
1959 | begin | |
1960 | Comp := First_Component (U_Type); | |
1961 | while Present (Comp) loop | |
1962 | Func := | |
1963 | Find_Stream_Subprogram | |
1964 | (Etype (Comp), TSS_Stream_Read); | |
1965 | ||
1966 | if Present (Func) then | |
1967 | Freeze_Stream_Subprogram (Func); | |
1968 | end if; | |
1969 | ||
1970 | Next_Component (Comp); | |
1971 | end loop; | |
1972 | end; | |
1973 | end if; | |
ee6ba406 | 1974 | end if; |
1975 | end if; | |
1976 | ||
1977 | -- If we fall through, Fname is the function to be called. The | |
1978 | -- result is obtained by calling the appropriate function, then | |
1979 | -- converting the result. The conversion does a subtype check. | |
1980 | ||
1981 | Call := | |
1982 | Make_Function_Call (Loc, | |
1983 | Name => New_Occurrence_Of (Fname, Loc), | |
1984 | Parameter_Associations => New_List ( | |
1985 | Relocate_Node (Strm))); | |
1986 | ||
1987 | Set_Controlling_Argument (Call, Cntrl); | |
1988 | Rewrite (N, Unchecked_Convert_To (P_Type, Call)); | |
1989 | Analyze_And_Resolve (N, P_Type); | |
d53a018a | 1990 | |
1991 | if Nkind (Parent (N)) = N_Object_Declaration then | |
1992 | Freeze_Stream_Subprogram (Fname); | |
1993 | end if; | |
ee6ba406 | 1994 | end Input; |
1995 | ||
1996 | ------------------- | |
1997 | -- Integer_Value -- | |
1998 | ------------------- | |
1999 | ||
2000 | -- We transform | |
2001 | ||
2002 | -- inttype'Fixed_Value (fixed-value) | |
2003 | ||
2004 | -- into | |
2005 | ||
2006 | -- inttype(integer-value)) | |
2007 | ||
2008 | -- we do all the required analysis of the conversion here, because | |
2009 | -- we do not want this to go through the fixed-point conversion | |
2010 | -- circuits. Note that gigi always treats fixed-point as equivalent | |
2011 | -- to the corresponding integer type anyway. | |
2012 | ||
2013 | when Attribute_Integer_Value => Integer_Value : | |
2014 | begin | |
2015 | Rewrite (N, | |
2016 | Make_Type_Conversion (Loc, | |
2017 | Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc), | |
2018 | Expression => Relocate_Node (First (Exprs)))); | |
2019 | Set_Etype (N, Entity (Pref)); | |
2020 | Set_Analyzed (N); | |
9dfe12ae | 2021 | |
2022 | -- Note: it might appear that a properly analyzed unchecked conversion | |
2023 | -- would be just fine here, but that's not the case, since the full | |
2024 | -- range checks performed by the following call are critical! | |
2025 | ||
ee6ba406 | 2026 | Apply_Type_Conversion_Checks (N); |
2027 | end Integer_Value; | |
2028 | ||
2029 | ---------- | |
2030 | -- Last -- | |
2031 | ---------- | |
2032 | ||
2033 | when Attribute_Last => declare | |
2034 | Ptyp : constant Entity_Id := Etype (Pref); | |
2035 | ||
2036 | begin | |
2037 | -- If the prefix type is a constrained packed array type which | |
2038 | -- already has a Packed_Array_Type representation defined, then | |
2039 | -- replace this attribute with a direct reference to 'Last of the | |
2040 | -- appropriate index subtype (since otherwise Gigi will try to give | |
2041 | -- us the value of 'Last for this implementation type). | |
2042 | ||
2043 | if Is_Constrained_Packed_Array (Ptyp) then | |
2044 | Rewrite (N, | |
2045 | Make_Attribute_Reference (Loc, | |
2046 | Attribute_Name => Name_Last, | |
2047 | Prefix => New_Reference_To (Get_Index_Subtype (N), Loc))); | |
2048 | Analyze_And_Resolve (N, Typ); | |
2049 | ||
2050 | elsif Is_Access_Type (Ptyp) then | |
2051 | Apply_Access_Check (N); | |
2052 | end if; | |
2053 | end; | |
2054 | ||
2055 | -------------- | |
2056 | -- Last_Bit -- | |
2057 | -------------- | |
2058 | ||
2059 | -- We compute this if a component clause was present, otherwise | |
2060 | -- we leave the computation up to Gigi, since we don't know what | |
2061 | -- layout will be chosen. | |
2062 | ||
2063 | when Attribute_Last_Bit => Last_Bit : | |
2064 | declare | |
2065 | CE : constant Entity_Id := Entity (Selector_Name (Pref)); | |
2066 | ||
2067 | begin | |
2068 | if Known_Static_Component_Bit_Offset (CE) | |
2069 | and then Known_Static_Esize (CE) | |
2070 | then | |
2071 | Rewrite (N, | |
2072 | Make_Integer_Literal (Loc, | |
2073 | Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit) | |
2074 | + Esize (CE) - 1)); | |
2075 | ||
2076 | Analyze_And_Resolve (N, Typ); | |
2077 | ||
2078 | else | |
2079 | Apply_Universal_Integer_Attribute_Checks (N); | |
2080 | end if; | |
2081 | end Last_Bit; | |
2082 | ||
2083 | ------------------ | |
2084 | -- Leading_Part -- | |
2085 | ------------------ | |
2086 | ||
2087 | -- Transforms 'Leading_Part into a call to the floating-point attribute | |
2088 | -- function Leading_Part in Fat_xxx (where xxx is the root type) | |
2089 | ||
2090 | -- Note: strictly, we should have special case code to deal with | |
2091 | -- absurdly large positive arguments (greater than Integer'Last), | |
2092 | -- which result in returning the first argument unchanged, but it | |
2093 | -- hardly seems worth the effort. We raise constraint error for | |
2094 | -- absurdly negative arguments which is fine. | |
2095 | ||
2096 | when Attribute_Leading_Part => | |
2097 | Expand_Fpt_Attribute_RI (N); | |
2098 | ||
2099 | ------------ | |
2100 | -- Length -- | |
2101 | ------------ | |
2102 | ||
2103 | when Attribute_Length => declare | |
2104 | Ptyp : constant Entity_Id := Etype (Pref); | |
2105 | Ityp : Entity_Id; | |
2106 | Xnum : Uint; | |
2107 | ||
2108 | begin | |
2109 | -- Processing for packed array types | |
2110 | ||
2111 | if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then | |
2112 | Ityp := Get_Index_Subtype (N); | |
2113 | ||
2114 | -- If the index type, Ityp, is an enumeration type with | |
2115 | -- holes, then we calculate X'Length explicitly using | |
2116 | ||
2117 | -- Typ'Max | |
2118 | -- (0, Ityp'Pos (X'Last (N)) - | |
2119 | -- Ityp'Pos (X'First (N)) + 1); | |
2120 | ||
2121 | -- Since the bounds in the template are the representation | |
2122 | -- values and gigi would get the wrong value. | |
2123 | ||
2124 | if Is_Enumeration_Type (Ityp) | |
2125 | and then Present (Enum_Pos_To_Rep (Base_Type (Ityp))) | |
2126 | then | |
2127 | if No (Exprs) then | |
2128 | Xnum := Uint_1; | |
2129 | else | |
2130 | Xnum := Expr_Value (First (Expressions (N))); | |
2131 | end if; | |
2132 | ||
2133 | Rewrite (N, | |
2134 | Make_Attribute_Reference (Loc, | |
2135 | Prefix => New_Occurrence_Of (Typ, Loc), | |
2136 | Attribute_Name => Name_Max, | |
2137 | Expressions => New_List | |
2138 | (Make_Integer_Literal (Loc, 0), | |
2139 | ||
2140 | Make_Op_Add (Loc, | |
2141 | Left_Opnd => | |
2142 | Make_Op_Subtract (Loc, | |
2143 | Left_Opnd => | |
2144 | Make_Attribute_Reference (Loc, | |
2145 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2146 | Attribute_Name => Name_Pos, | |
2147 | ||
2148 | Expressions => New_List ( | |
2149 | Make_Attribute_Reference (Loc, | |
2150 | Prefix => Duplicate_Subexpr (Pref), | |
2151 | Attribute_Name => Name_Last, | |
2152 | Expressions => New_List ( | |
2153 | Make_Integer_Literal (Loc, Xnum))))), | |
2154 | ||
2155 | Right_Opnd => | |
2156 | Make_Attribute_Reference (Loc, | |
2157 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2158 | Attribute_Name => Name_Pos, | |
2159 | ||
2160 | Expressions => New_List ( | |
2161 | Make_Attribute_Reference (Loc, | |
9dfe12ae | 2162 | Prefix => |
2163 | Duplicate_Subexpr_No_Checks (Pref), | |
ee6ba406 | 2164 | Attribute_Name => Name_First, |
2165 | Expressions => New_List ( | |
2166 | Make_Integer_Literal (Loc, Xnum)))))), | |
2167 | ||
2168 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); | |
2169 | ||
2170 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
2171 | return; | |
2172 | ||
2173 | -- If the prefix type is a constrained packed array type which | |
2174 | -- already has a Packed_Array_Type representation defined, then | |
2175 | -- replace this attribute with a direct reference to 'Range_Length | |
2176 | -- of the appropriate index subtype (since otherwise Gigi will try | |
2177 | -- to give us the value of 'Length for this implementation type). | |
2178 | ||
2179 | elsif Is_Constrained (Ptyp) then | |
2180 | Rewrite (N, | |
2181 | Make_Attribute_Reference (Loc, | |
2182 | Attribute_Name => Name_Range_Length, | |
2183 | Prefix => New_Reference_To (Ityp, Loc))); | |
2184 | Analyze_And_Resolve (N, Typ); | |
2185 | end if; | |
2186 | ||
2187 | -- If we have a packed array that is not bit packed, which was | |
2188 | ||
2189 | -- Access type case | |
2190 | ||
2191 | elsif Is_Access_Type (Ptyp) then | |
2192 | Apply_Access_Check (N); | |
2193 | ||
2194 | -- If the designated type is a packed array type, then we | |
2195 | -- convert the reference to: | |
2196 | ||
2197 | -- typ'Max (0, 1 + | |
2198 | -- xtyp'Pos (Pref'Last (Expr)) - | |
2199 | -- xtyp'Pos (Pref'First (Expr))); | |
2200 | ||
2201 | -- This is a bit complex, but it is the easiest thing to do | |
2202 | -- that works in all cases including enum types with holes | |
2203 | -- xtyp here is the appropriate index type. | |
2204 | ||
2205 | declare | |
2206 | Dtyp : constant Entity_Id := Designated_Type (Ptyp); | |
2207 | Xtyp : Entity_Id; | |
2208 | ||
2209 | begin | |
2210 | if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then | |
2211 | Xtyp := Get_Index_Subtype (N); | |
2212 | ||
2213 | Rewrite (N, | |
2214 | Make_Attribute_Reference (Loc, | |
2215 | Prefix => New_Occurrence_Of (Typ, Loc), | |
2216 | Attribute_Name => Name_Max, | |
2217 | Expressions => New_List ( | |
2218 | Make_Integer_Literal (Loc, 0), | |
2219 | ||
2220 | Make_Op_Add (Loc, | |
2221 | Make_Integer_Literal (Loc, 1), | |
2222 | Make_Op_Subtract (Loc, | |
2223 | Left_Opnd => | |
2224 | Make_Attribute_Reference (Loc, | |
2225 | Prefix => New_Occurrence_Of (Xtyp, Loc), | |
2226 | Attribute_Name => Name_Pos, | |
2227 | Expressions => New_List ( | |
2228 | Make_Attribute_Reference (Loc, | |
2229 | Prefix => Duplicate_Subexpr (Pref), | |
2230 | Attribute_Name => Name_Last, | |
2231 | Expressions => | |
2232 | New_Copy_List (Exprs)))), | |
2233 | ||
2234 | Right_Opnd => | |
2235 | Make_Attribute_Reference (Loc, | |
2236 | Prefix => New_Occurrence_Of (Xtyp, Loc), | |
2237 | Attribute_Name => Name_Pos, | |
2238 | Expressions => New_List ( | |
2239 | Make_Attribute_Reference (Loc, | |
9dfe12ae | 2240 | Prefix => |
2241 | Duplicate_Subexpr_No_Checks (Pref), | |
ee6ba406 | 2242 | Attribute_Name => Name_First, |
2243 | Expressions => | |
2244 | New_Copy_List (Exprs))))))))); | |
2245 | ||
2246 | Analyze_And_Resolve (N, Typ); | |
2247 | end if; | |
2248 | end; | |
2249 | ||
2250 | -- Otherwise leave it to gigi | |
2251 | ||
2252 | else | |
2253 | Apply_Universal_Integer_Attribute_Checks (N); | |
2254 | end if; | |
2255 | end; | |
2256 | ||
2257 | ------------- | |
2258 | -- Machine -- | |
2259 | ------------- | |
2260 | ||
2261 | -- Transforms 'Machine into a call to the floating-point attribute | |
2262 | -- function Machine in Fat_xxx (where xxx is the root type) | |
2263 | ||
2264 | when Attribute_Machine => | |
2265 | Expand_Fpt_Attribute_R (N); | |
2266 | ||
2267 | ------------------ | |
2268 | -- Machine_Size -- | |
2269 | ------------------ | |
2270 | ||
2271 | -- Machine_Size is equivalent to Object_Size, so transform it into | |
2272 | -- Object_Size and that way Gigi never sees Machine_Size. | |
2273 | ||
2274 | when Attribute_Machine_Size => | |
2275 | Rewrite (N, | |
2276 | Make_Attribute_Reference (Loc, | |
2277 | Prefix => Prefix (N), | |
2278 | Attribute_Name => Name_Object_Size)); | |
2279 | ||
2280 | Analyze_And_Resolve (N, Typ); | |
2281 | ||
2282 | -------------- | |
2283 | -- Mantissa -- | |
2284 | -------------- | |
2285 | ||
2286 | -- The only case that can get this far is the dynamic case of the | |
2287 | -- old Ada 83 Mantissa attribute for the fixed-point case. For this | |
2288 | -- case, we expand: | |
2289 | ||
2290 | -- typ'Mantissa | |
2291 | ||
2292 | -- into | |
2293 | ||
2294 | -- ityp (System.Mantissa.Mantissa_Value | |
2295 | -- (Integer'Integer_Value (typ'First), | |
2296 | -- Integer'Integer_Value (typ'Last))); | |
2297 | ||
2298 | when Attribute_Mantissa => Mantissa : declare | |
2299 | Ptyp : constant Entity_Id := Etype (Pref); | |
2300 | ||
2301 | begin | |
2302 | Rewrite (N, | |
2303 | Convert_To (Typ, | |
2304 | Make_Function_Call (Loc, | |
2305 | Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc), | |
2306 | ||
2307 | Parameter_Associations => New_List ( | |
2308 | ||
2309 | Make_Attribute_Reference (Loc, | |
2310 | Prefix => New_Occurrence_Of (Standard_Integer, Loc), | |
2311 | Attribute_Name => Name_Integer_Value, | |
2312 | Expressions => New_List ( | |
2313 | ||
2314 | Make_Attribute_Reference (Loc, | |
2315 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
2316 | Attribute_Name => Name_First))), | |
2317 | ||
2318 | Make_Attribute_Reference (Loc, | |
2319 | Prefix => New_Occurrence_Of (Standard_Integer, Loc), | |
2320 | Attribute_Name => Name_Integer_Value, | |
2321 | Expressions => New_List ( | |
2322 | ||
2323 | Make_Attribute_Reference (Loc, | |
2324 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
2325 | Attribute_Name => Name_Last))))))); | |
2326 | ||
2327 | Analyze_And_Resolve (N, Typ); | |
2328 | end Mantissa; | |
2329 | ||
e0521a36 | 2330 | --------- |
2331 | -- Mod -- | |
2332 | --------- | |
2333 | ||
2334 | when Attribute_Mod => Mod_Case : declare | |
2335 | Arg : constant Node_Id := Relocate_Node (First (Exprs)); | |
2336 | Hi : constant Node_Id := Type_High_Bound (Etype (Arg)); | |
2337 | Modv : constant Uint := Modulus (Btyp); | |
2338 | ||
2339 | begin | |
2340 | ||
2341 | -- This is not so simple. The issue is what type to use for the | |
2342 | -- computation of the modular value. | |
2343 | ||
2344 | -- The easy case is when the modulus value is within the bounds | |
2345 | -- of the signed integer type of the argument. In this case we can | |
2346 | -- just do the computation in that signed integer type, and then | |
2347 | -- do an ordinary conversion to the target type. | |
2348 | ||
2349 | if Modv <= Expr_Value (Hi) then | |
2350 | Rewrite (N, | |
2351 | Convert_To (Btyp, | |
2352 | Make_Op_Mod (Loc, | |
2353 | Left_Opnd => Arg, | |
2354 | Right_Opnd => Make_Integer_Literal (Loc, Modv)))); | |
2355 | ||
2356 | -- Here we know that the modulus is larger than type'Last of the | |
2357 | -- integer type. There are three possible cases to consider: | |
2358 | ||
2359 | -- a) The integer value is non-negative. In this case, it is | |
2360 | -- returned as the result (since it is less than the modulus). | |
2361 | ||
2362 | -- b) The integer value is negative. In this case, we know that | |
2363 | -- the result is modulus + value, where the value might be as | |
2364 | -- small as -modulus. The trouble is what type do we use to do | |
2365 | -- this subtraction. No type will do, since modulus can be as | |
2366 | -- big as 2**64, and no integer type accomodates this value. | |
2367 | -- Let's do a bit of algebra | |
2368 | ||
2369 | -- modulus + value | |
2370 | -- = modulus - (-value) | |
2371 | -- = (modulus - 1) - (-value - 1) | |
2372 | ||
2373 | -- Now modulus - 1 is certainly in range of the modular type. | |
2374 | -- -value is in the range 1 .. modulus, so -value -1 is in the | |
2375 | -- range 0 .. modulus-1 which is in range of the modular type. | |
2376 | -- Furthermore, (-value - 1) can be expressed as -(value + 1) | |
2377 | -- which we can compute using the integer base type. | |
2378 | ||
2379 | else | |
2380 | Rewrite (N, | |
2381 | Make_Conditional_Expression (Loc, | |
2382 | Expressions => New_List ( | |
2383 | Make_Op_Ge (Loc, | |
2384 | Left_Opnd => Duplicate_Subexpr (Arg), | |
2385 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
2386 | ||
2387 | Convert_To (Btyp, | |
2388 | Duplicate_Subexpr_No_Checks (Arg)), | |
2389 | ||
2390 | Make_Op_Subtract (Loc, | |
2391 | Left_Opnd => | |
2392 | Make_Integer_Literal (Loc, | |
2393 | Intval => Modv - 1), | |
2394 | Right_Opnd => | |
2395 | Convert_To (Btyp, | |
2396 | Make_Op_Minus (Loc, | |
2397 | Right_Opnd => | |
2398 | Make_Op_Add (Loc, | |
2399 | Left_Opnd => Duplicate_Subexpr_No_Checks (Arg), | |
2400 | Right_Opnd => | |
2401 | Make_Integer_Literal (Loc, | |
2402 | Intval => 1)))))))); | |
2403 | ||
e0521a36 | 2404 | end if; |
2405 | ||
2406 | Analyze_And_Resolve (N, Btyp); | |
2407 | end Mod_Case; | |
2408 | ||
ee6ba406 | 2409 | ----------- |
2410 | -- Model -- | |
2411 | ----------- | |
2412 | ||
2413 | -- Transforms 'Model into a call to the floating-point attribute | |
2414 | -- function Model in Fat_xxx (where xxx is the root type) | |
2415 | ||
2416 | when Attribute_Model => | |
2417 | Expand_Fpt_Attribute_R (N); | |
2418 | ||
2419 | ----------------- | |
2420 | -- Object_Size -- | |
2421 | ----------------- | |
2422 | ||
2423 | -- The processing for Object_Size shares the processing for Size | |
2424 | ||
2425 | ------------ | |
2426 | -- Output -- | |
2427 | ------------ | |
2428 | ||
2429 | when Attribute_Output => Output : declare | |
2430 | P_Type : constant Entity_Id := Entity (Pref); | |
ee6ba406 | 2431 | U_Type : constant Entity_Id := Underlying_Type (P_Type); |
2432 | Pname : Entity_Id; | |
2433 | Decl : Node_Id; | |
2434 | Prag : Node_Id; | |
2435 | Arg3 : Node_Id; | |
2436 | Wfunc : Node_Id; | |
2437 | ||
2438 | begin | |
2439 | -- If no underlying type, we have an error that will be diagnosed | |
2440 | -- elsewhere, so here we just completely ignore the expansion. | |
2441 | ||
2442 | if No (U_Type) then | |
2443 | return; | |
2444 | end if; | |
2445 | ||
2446 | -- If TSS for Output is present, just call it | |
2447 | ||
9dfe12ae | 2448 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output); |
ee6ba406 | 2449 | |
2450 | if Present (Pname) then | |
2451 | null; | |
2452 | ||
2453 | else | |
2454 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
2455 | ||
2456 | -- sourcetyp'Output (stream, Item) | |
2457 | ||
2458 | -- as | |
2459 | ||
2460 | -- strmtyp'Output (Stream, strmwrite (acttyp (Item))); | |
2461 | ||
2462 | -- where strmwrite is the given Write function that converts | |
2463 | -- an argument of type sourcetyp or a type acctyp, from which | |
2464 | -- it is derived to type strmtyp. The conversion to acttyp is | |
2465 | -- required for the derived case. | |
2466 | ||
5245b786 | 2467 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 2468 | |
2469 | if Present (Prag) then | |
2470 | Arg3 := | |
2471 | Next (Next (First (Pragma_Argument_Associations (Prag)))); | |
2472 | Wfunc := Entity (Expression (Arg3)); | |
2473 | ||
2474 | Rewrite (N, | |
2475 | Make_Attribute_Reference (Loc, | |
2476 | Prefix => New_Occurrence_Of (Etype (Wfunc), Loc), | |
2477 | Attribute_Name => Name_Output, | |
2478 | Expressions => New_List ( | |
2479 | Relocate_Node (First (Exprs)), | |
2480 | Make_Function_Call (Loc, | |
2481 | Name => New_Occurrence_Of (Wfunc, Loc), | |
2482 | Parameter_Associations => New_List ( | |
2483 | Convert_To (Etype (First_Formal (Wfunc)), | |
2484 | Relocate_Node (Next (First (Exprs))))))))); | |
2485 | ||
2486 | Analyze (N); | |
2487 | return; | |
2488 | ||
2489 | -- For elementary types, we call the W_xxx routine directly. | |
2490 | -- Note that the effect of Write and Output is identical for | |
2491 | -- the case of an elementary type, since there are no | |
2492 | -- discriminants or bounds. | |
2493 | ||
2494 | elsif Is_Elementary_Type (U_Type) then | |
2495 | ||
2496 | -- A special case arises if we have a defined _Write routine, | |
2497 | -- since in this case we are required to call this routine. | |
2498 | ||
9dfe12ae | 2499 | if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then |
ee6ba406 | 2500 | Build_Record_Or_Elementary_Output_Procedure |
2501 | (Loc, U_Type, Decl, Pname); | |
2502 | Insert_Action (N, Decl); | |
2503 | ||
2504 | -- For normal cases, we call the W_xxx routine directly | |
2505 | ||
2506 | else | |
2507 | Rewrite (N, Build_Elementary_Write_Call (N)); | |
2508 | Analyze (N); | |
2509 | return; | |
2510 | end if; | |
2511 | ||
2512 | -- Array type case | |
2513 | ||
2514 | elsif Is_Array_Type (U_Type) then | |
2515 | Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname); | |
2516 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
2517 | ||
2518 | -- Class-wide case, first output external tag, then dispatch | |
2519 | -- to the appropriate primitive Output function (RM 13.13.2(31)). | |
2520 | ||
2521 | elsif Is_Class_Wide_Type (P_Type) then | |
2522 | Tag_Write : declare | |
2523 | Strm : constant Node_Id := First (Exprs); | |
2524 | Item : constant Node_Id := Next (Strm); | |
2525 | ||
2526 | begin | |
2527 | -- The code is: | |
2528 | -- String'Output (Strm, External_Tag (Item'Tag)) | |
2529 | ||
2530 | Insert_Action (N, | |
2531 | Make_Attribute_Reference (Loc, | |
2532 | Prefix => New_Occurrence_Of (Standard_String, Loc), | |
2533 | Attribute_Name => Name_Output, | |
2534 | Expressions => New_List ( | |
2535 | Relocate_Node (Duplicate_Subexpr (Strm)), | |
2536 | Make_Function_Call (Loc, | |
2537 | Name => | |
2538 | New_Occurrence_Of (RTE (RE_External_Tag), Loc), | |
2539 | Parameter_Associations => New_List ( | |
2540 | Make_Attribute_Reference (Loc, | |
2541 | Prefix => | |
2542 | Relocate_Node | |
2543 | (Duplicate_Subexpr (Item, Name_Req => True)), | |
2544 | Attribute_Name => Name_Tag)))))); | |
2545 | end Tag_Write; | |
2546 | ||
9dfe12ae | 2547 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Output); |
ee6ba406 | 2548 | |
2549 | -- Tagged type case, use the primitive Output function | |
2550 | ||
2551 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 2552 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Output); |
ee6ba406 | 2553 | |
2554 | -- All other record type cases, including protected records. | |
2555 | -- The latter only arise for expander generated code for | |
2556 | -- handling shared passive partition access. | |
2557 | ||
2558 | else | |
2559 | pragma Assert | |
2560 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
2561 | ||
00f91aef | 2562 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
2563 | -- the default implementation of the Output attribute of an | |
2564 | -- unchecked union type if the type lacks default discriminant | |
2565 | -- values. | |
2566 | ||
2567 | if Is_Unchecked_Union (Base_Type (U_Type)) | |
2568 | and then not Present (Discriminant_Constraint (U_Type)) | |
2569 | then | |
2570 | Insert_Action (N, | |
2571 | Make_Raise_Program_Error (Loc, | |
2572 | Reason => PE_Unchecked_Union_Restriction)); | |
2573 | ||
2574 | return; | |
2575 | end if; | |
2576 | ||
ee6ba406 | 2577 | Build_Record_Or_Elementary_Output_Procedure |
2578 | (Loc, Base_Type (U_Type), Decl, Pname); | |
2579 | Insert_Action (N, Decl); | |
2580 | end if; | |
2581 | end if; | |
2582 | ||
2583 | -- If we fall through, Pname is the name of the procedure to call | |
2584 | ||
2585 | Rewrite_Stream_Proc_Call (Pname); | |
2586 | end Output; | |
2587 | ||
2588 | --------- | |
2589 | -- Pos -- | |
2590 | --------- | |
2591 | ||
2592 | -- For enumeration types with a standard representation, Pos is | |
2593 | -- handled by Gigi. | |
2594 | ||
2595 | -- For enumeration types, with a non-standard representation we | |
2596 | -- generate a call to the _Rep_To_Pos function created when the | |
2597 | -- type was frozen. The call has the form | |
2598 | ||
9dfe12ae | 2599 | -- _rep_to_pos (expr, flag) |
ee6ba406 | 2600 | |
9dfe12ae | 2601 | -- The parameter flag is True if range checks are enabled, causing |
2602 | -- Program_Error to be raised if the expression has an invalid | |
2603 | -- representation, and False if range checks are suppressed. | |
ee6ba406 | 2604 | |
2605 | -- For integer types, Pos is equivalent to a simple integer | |
2606 | -- conversion and we rewrite it as such | |
2607 | ||
2608 | when Attribute_Pos => Pos : | |
2609 | declare | |
2610 | Etyp : Entity_Id := Base_Type (Entity (Pref)); | |
2611 | ||
2612 | begin | |
2613 | -- Deal with zero/non-zero boolean values | |
2614 | ||
2615 | if Is_Boolean_Type (Etyp) then | |
2616 | Adjust_Condition (First (Exprs)); | |
2617 | Etyp := Standard_Boolean; | |
2618 | Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc)); | |
2619 | end if; | |
2620 | ||
2621 | -- Case of enumeration type | |
2622 | ||
2623 | if Is_Enumeration_Type (Etyp) then | |
2624 | ||
2625 | -- Non-standard enumeration type (generate call) | |
2626 | ||
2627 | if Present (Enum_Pos_To_Rep (Etyp)) then | |
9dfe12ae | 2628 | Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc)); |
ee6ba406 | 2629 | Rewrite (N, |
2630 | Convert_To (Typ, | |
2631 | Make_Function_Call (Loc, | |
2632 | Name => | |
9dfe12ae | 2633 | New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
ee6ba406 | 2634 | Parameter_Associations => Exprs))); |
2635 | ||
2636 | Analyze_And_Resolve (N, Typ); | |
2637 | ||
2638 | -- Standard enumeration type (do universal integer check) | |
2639 | ||
2640 | else | |
2641 | Apply_Universal_Integer_Attribute_Checks (N); | |
2642 | end if; | |
2643 | ||
2644 | -- Deal with integer types (replace by conversion) | |
2645 | ||
2646 | elsif Is_Integer_Type (Etyp) then | |
2647 | Rewrite (N, Convert_To (Typ, First (Exprs))); | |
2648 | Analyze_And_Resolve (N, Typ); | |
2649 | end if; | |
2650 | ||
2651 | end Pos; | |
2652 | ||
2653 | -------------- | |
2654 | -- Position -- | |
2655 | -------------- | |
2656 | ||
2657 | -- We compute this if a component clause was present, otherwise | |
2658 | -- we leave the computation up to Gigi, since we don't know what | |
2659 | -- layout will be chosen. | |
2660 | ||
2661 | when Attribute_Position => Position : | |
2662 | declare | |
2663 | CE : constant Entity_Id := Entity (Selector_Name (Pref)); | |
2664 | ||
2665 | begin | |
2666 | if Present (Component_Clause (CE)) then | |
2667 | Rewrite (N, | |
2668 | Make_Integer_Literal (Loc, | |
2669 | Intval => Component_Bit_Offset (CE) / System_Storage_Unit)); | |
2670 | Analyze_And_Resolve (N, Typ); | |
2671 | ||
2672 | else | |
2673 | Apply_Universal_Integer_Attribute_Checks (N); | |
2674 | end if; | |
2675 | end Position; | |
2676 | ||
2677 | ---------- | |
2678 | -- Pred -- | |
2679 | ---------- | |
2680 | ||
2681 | -- 1. Deal with enumeration types with holes | |
2682 | -- 2. For floating-point, generate call to attribute function | |
2683 | -- 3. For other cases, deal with constraint checking | |
2684 | ||
2685 | when Attribute_Pred => Pred : | |
2686 | declare | |
2687 | Ptyp : constant Entity_Id := Base_Type (Etype (Pref)); | |
2688 | ||
2689 | begin | |
2690 | -- For enumeration types with non-standard representations, we | |
2691 | -- expand typ'Pred (x) into | |
2692 | ||
2693 | -- Pos_To_Rep (Rep_To_Pos (x) - 1) | |
2694 | ||
9dfe12ae | 2695 | -- If the representation is contiguous, we compute instead |
2696 | -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations. | |
2697 | ||
ee6ba406 | 2698 | if Is_Enumeration_Type (Ptyp) |
2699 | and then Present (Enum_Pos_To_Rep (Ptyp)) | |
2700 | then | |
9dfe12ae | 2701 | if Has_Contiguous_Rep (Ptyp) then |
2702 | Rewrite (N, | |
2703 | Unchecked_Convert_To (Ptyp, | |
2704 | Make_Op_Add (Loc, | |
2705 | Left_Opnd => | |
2706 | Make_Integer_Literal (Loc, | |
2707 | Enumeration_Rep (First_Literal (Ptyp))), | |
2708 | Right_Opnd => | |
2709 | Make_Function_Call (Loc, | |
2710 | Name => | |
2711 | New_Reference_To | |
2712 | (TSS (Ptyp, TSS_Rep_To_Pos), Loc), | |
2713 | ||
2714 | Parameter_Associations => | |
2715 | New_List ( | |
2716 | Unchecked_Convert_To (Ptyp, | |
2717 | Make_Op_Subtract (Loc, | |
2718 | Left_Opnd => | |
2719 | Unchecked_Convert_To (Standard_Integer, | |
2720 | Relocate_Node (First (Exprs))), | |
2721 | Right_Opnd => | |
2722 | Make_Integer_Literal (Loc, 1))), | |
2723 | Rep_To_Pos_Flag (Ptyp, Loc)))))); | |
ee6ba406 | 2724 | |
9dfe12ae | 2725 | else |
2726 | -- Add Boolean parameter True, to request program errror if | |
2727 | -- we have a bad representation on our hands. If checks are | |
2728 | -- suppressed, then add False instead | |
ee6ba406 | 2729 | |
9dfe12ae | 2730 | Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc)); |
2731 | Rewrite (N, | |
2732 | Make_Indexed_Component (Loc, | |
2733 | Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc), | |
2734 | Expressions => New_List ( | |
2735 | Make_Op_Subtract (Loc, | |
ee6ba406 | 2736 | Left_Opnd => |
2737 | Make_Function_Call (Loc, | |
2738 | Name => | |
9dfe12ae | 2739 | New_Reference_To (TSS (Ptyp, TSS_Rep_To_Pos), Loc), |
2740 | Parameter_Associations => Exprs), | |
ee6ba406 | 2741 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); |
9dfe12ae | 2742 | end if; |
ee6ba406 | 2743 | |
2744 | Analyze_And_Resolve (N, Typ); | |
2745 | ||
2746 | -- For floating-point, we transform 'Pred into a call to the Pred | |
2747 | -- floating-point attribute function in Fat_xxx (xxx is root type) | |
2748 | ||
2749 | elsif Is_Floating_Point_Type (Ptyp) then | |
2750 | Expand_Fpt_Attribute_R (N); | |
2751 | Analyze_And_Resolve (N, Typ); | |
2752 | ||
2753 | -- For modular types, nothing to do (no overflow, since wraps) | |
2754 | ||
2755 | elsif Is_Modular_Integer_Type (Ptyp) then | |
2756 | null; | |
2757 | ||
2758 | -- For other types, if range checking is enabled, we must generate | |
2759 | -- a check if overflow checking is enabled. | |
2760 | ||
2761 | elsif not Overflow_Checks_Suppressed (Ptyp) then | |
2762 | Expand_Pred_Succ (N); | |
2763 | end if; | |
2764 | ||
2765 | end Pred; | |
2766 | ||
2767 | ------------------ | |
2768 | -- Range_Length -- | |
2769 | ------------------ | |
2770 | ||
2771 | when Attribute_Range_Length => Range_Length : declare | |
2772 | P_Type : constant Entity_Id := Etype (Pref); | |
2773 | ||
2774 | begin | |
2775 | -- The only special processing required is for the case where | |
2776 | -- Range_Length is applied to an enumeration type with holes. | |
2777 | -- In this case we transform | |
2778 | ||
2779 | -- X'Range_Length | |
2780 | ||
2781 | -- to | |
2782 | ||
2783 | -- X'Pos (X'Last) - X'Pos (X'First) + 1 | |
2784 | ||
2785 | -- So that the result reflects the proper Pos values instead | |
2786 | -- of the underlying representations. | |
2787 | ||
2788 | if Is_Enumeration_Type (P_Type) | |
2789 | and then Has_Non_Standard_Rep (P_Type) | |
2790 | then | |
2791 | Rewrite (N, | |
2792 | Make_Op_Add (Loc, | |
2793 | Left_Opnd => | |
2794 | Make_Op_Subtract (Loc, | |
2795 | Left_Opnd => | |
2796 | Make_Attribute_Reference (Loc, | |
2797 | Attribute_Name => Name_Pos, | |
2798 | Prefix => New_Occurrence_Of (P_Type, Loc), | |
2799 | Expressions => New_List ( | |
2800 | Make_Attribute_Reference (Loc, | |
2801 | Attribute_Name => Name_Last, | |
2802 | Prefix => New_Occurrence_Of (P_Type, Loc)))), | |
2803 | ||
2804 | Right_Opnd => | |
2805 | Make_Attribute_Reference (Loc, | |
2806 | Attribute_Name => Name_Pos, | |
2807 | Prefix => New_Occurrence_Of (P_Type, Loc), | |
2808 | Expressions => New_List ( | |
2809 | Make_Attribute_Reference (Loc, | |
2810 | Attribute_Name => Name_First, | |
2811 | Prefix => New_Occurrence_Of (P_Type, Loc))))), | |
2812 | ||
2813 | Right_Opnd => | |
2814 | Make_Integer_Literal (Loc, 1))); | |
2815 | ||
2816 | Analyze_And_Resolve (N, Typ); | |
2817 | ||
2818 | -- For all other cases, attribute is handled by Gigi, but we need | |
2819 | -- to deal with the case of the range check on a universal integer. | |
2820 | ||
2821 | else | |
2822 | Apply_Universal_Integer_Attribute_Checks (N); | |
2823 | end if; | |
2824 | ||
2825 | end Range_Length; | |
2826 | ||
2827 | ---------- | |
2828 | -- Read -- | |
2829 | ---------- | |
2830 | ||
2831 | when Attribute_Read => Read : declare | |
2832 | P_Type : constant Entity_Id := Entity (Pref); | |
2833 | B_Type : constant Entity_Id := Base_Type (P_Type); | |
2834 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
2835 | Pname : Entity_Id; | |
2836 | Decl : Node_Id; | |
2837 | Prag : Node_Id; | |
2838 | Arg2 : Node_Id; | |
2839 | Rfunc : Node_Id; | |
2840 | Lhs : Node_Id; | |
2841 | Rhs : Node_Id; | |
2842 | ||
2843 | begin | |
2844 | -- If no underlying type, we have an error that will be diagnosed | |
2845 | -- elsewhere, so here we just completely ignore the expansion. | |
2846 | ||
2847 | if No (U_Type) then | |
2848 | return; | |
2849 | end if; | |
2850 | ||
2851 | -- The simple case, if there is a TSS for Read, just call it | |
2852 | ||
9dfe12ae | 2853 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read); |
ee6ba406 | 2854 | |
2855 | if Present (Pname) then | |
2856 | null; | |
2857 | ||
2858 | else | |
2859 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
2860 | ||
2861 | -- sourcetyp'Read (stream, Item) | |
2862 | ||
2863 | -- as | |
2864 | ||
2865 | -- Item := sourcetyp (strmread (strmtyp'Input (Stream))); | |
2866 | ||
2867 | -- where strmread is the given Read function that converts | |
2868 | -- an argument of type strmtyp to type sourcetyp or a type | |
2869 | -- from which it is derived. The conversion to sourcetyp | |
2870 | -- is required in the latter case. | |
2871 | ||
2872 | -- A special case arises if Item is a type conversion in which | |
2873 | -- case, we have to expand to: | |
2874 | ||
2875 | -- Itemx := typex (strmread (strmtyp'Input (Stream))); | |
2876 | ||
2877 | -- where Itemx is the expression of the type conversion (i.e. | |
2878 | -- the actual object), and typex is the type of Itemx. | |
2879 | ||
5245b786 | 2880 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 2881 | |
2882 | if Present (Prag) then | |
2883 | Arg2 := Next (First (Pragma_Argument_Associations (Prag))); | |
2884 | Rfunc := Entity (Expression (Arg2)); | |
2885 | Lhs := Relocate_Node (Next (First (Exprs))); | |
2886 | Rhs := | |
2887 | Convert_To (B_Type, | |
2888 | Make_Function_Call (Loc, | |
2889 | Name => New_Occurrence_Of (Rfunc, Loc), | |
2890 | Parameter_Associations => New_List ( | |
2891 | Make_Attribute_Reference (Loc, | |
2892 | Prefix => | |
2893 | New_Occurrence_Of | |
2894 | (Etype (First_Formal (Rfunc)), Loc), | |
2895 | Attribute_Name => Name_Input, | |
2896 | Expressions => New_List ( | |
2897 | Relocate_Node (First (Exprs))))))); | |
2898 | ||
2899 | if Nkind (Lhs) = N_Type_Conversion then | |
2900 | Lhs := Expression (Lhs); | |
2901 | Rhs := Convert_To (Etype (Lhs), Rhs); | |
2902 | end if; | |
2903 | ||
2904 | Rewrite (N, | |
2905 | Make_Assignment_Statement (Loc, | |
9dfe12ae | 2906 | Name => Lhs, |
ee6ba406 | 2907 | Expression => Rhs)); |
2908 | Set_Assignment_OK (Lhs); | |
2909 | Analyze (N); | |
2910 | return; | |
2911 | ||
2912 | -- For elementary types, we call the I_xxx routine using the first | |
2913 | -- parameter and then assign the result into the second parameter. | |
2914 | -- We set Assignment_OK to deal with the conversion case. | |
2915 | ||
2916 | elsif Is_Elementary_Type (U_Type) then | |
2917 | declare | |
2918 | Lhs : Node_Id; | |
2919 | Rhs : Node_Id; | |
2920 | ||
2921 | begin | |
2922 | Lhs := Relocate_Node (Next (First (Exprs))); | |
2923 | Rhs := Build_Elementary_Input_Call (N); | |
2924 | ||
2925 | if Nkind (Lhs) = N_Type_Conversion then | |
2926 | Lhs := Expression (Lhs); | |
2927 | Rhs := Convert_To (Etype (Lhs), Rhs); | |
2928 | end if; | |
2929 | ||
2930 | Set_Assignment_OK (Lhs); | |
2931 | ||
2932 | Rewrite (N, | |
2933 | Make_Assignment_Statement (Loc, | |
2934 | Name => Lhs, | |
2935 | Expression => Rhs)); | |
2936 | ||
2937 | Analyze (N); | |
2938 | return; | |
2939 | end; | |
2940 | ||
2941 | -- Array type case | |
2942 | ||
2943 | elsif Is_Array_Type (U_Type) then | |
2944 | Build_Array_Read_Procedure (N, U_Type, Decl, Pname); | |
2945 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
2946 | ||
2947 | -- Tagged type case, use the primitive Read function. Note that | |
2948 | -- this will dispatch in the class-wide case which is what we want | |
2949 | ||
2950 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 2951 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Read); |
ee6ba406 | 2952 | |
2953 | -- All other record type cases, including protected records. | |
2954 | -- The latter only arise for expander generated code for | |
2955 | -- handling shared passive partition access. | |
2956 | ||
2957 | else | |
2958 | pragma Assert | |
2959 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
2960 | ||
00f91aef | 2961 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
2962 | -- the default implementation of the Read attribute of an | |
2963 | -- Unchecked_Union type. | |
2964 | ||
2965 | if Is_Unchecked_Union (Base_Type (U_Type)) then | |
2966 | Insert_Action (N, | |
2967 | Make_Raise_Program_Error (Loc, | |
2968 | Reason => PE_Unchecked_Union_Restriction)); | |
2969 | end if; | |
2970 | ||
ee6ba406 | 2971 | if Has_Discriminants (U_Type) |
2972 | and then Present | |
2973 | (Discriminant_Default_Value (First_Discriminant (U_Type))) | |
2974 | then | |
2975 | Build_Mutable_Record_Read_Procedure | |
2976 | (Loc, Base_Type (U_Type), Decl, Pname); | |
ee6ba406 | 2977 | else |
2978 | Build_Record_Read_Procedure | |
2979 | (Loc, Base_Type (U_Type), Decl, Pname); | |
2980 | end if; | |
2981 | ||
2982 | -- Suppress checks, uninitialized or otherwise invalid | |
2983 | -- data does not cause constraint errors to be raised for | |
2984 | -- a complete record read. | |
2985 | ||
2986 | Insert_Action (N, Decl, All_Checks); | |
2987 | end if; | |
2988 | end if; | |
2989 | ||
2990 | Rewrite_Stream_Proc_Call (Pname); | |
2991 | end Read; | |
2992 | ||
2993 | --------------- | |
2994 | -- Remainder -- | |
2995 | --------------- | |
2996 | ||
2997 | -- Transforms 'Remainder into a call to the floating-point attribute | |
2998 | -- function Remainder in Fat_xxx (where xxx is the root type) | |
2999 | ||
3000 | when Attribute_Remainder => | |
3001 | Expand_Fpt_Attribute_RR (N); | |
3002 | ||
3003 | ----------- | |
3004 | -- Round -- | |
3005 | ----------- | |
3006 | ||
3007 | -- The handling of the Round attribute is quite delicate. The | |
3008 | -- processing in Sem_Attr introduced a conversion to universal | |
3009 | -- real, reflecting the semantics of Round, but we do not want | |
3010 | -- anything to do with universal real at runtime, since this | |
3011 | -- corresponds to using floating-point arithmetic. | |
3012 | ||
3013 | -- What we have now is that the Etype of the Round attribute | |
3014 | -- correctly indicates the final result type. The operand of | |
3015 | -- the Round is the conversion to universal real, described | |
3016 | -- above, and the operand of this conversion is the actual | |
3017 | -- operand of Round, which may be the special case of a fixed | |
3018 | -- point multiplication or division (Etype = universal fixed) | |
3019 | ||
3020 | -- The exapander will expand first the operand of the conversion, | |
3021 | -- then the conversion, and finally the round attribute itself, | |
3022 | -- since we always work inside out. But we cannot simply process | |
3023 | -- naively in this order. In the semantic world where universal | |
3024 | -- fixed and real really exist and have infinite precision, there | |
3025 | -- is no problem, but in the implementation world, where universal | |
3026 | -- real is a floating-point type, we would get the wrong result. | |
3027 | ||
3028 | -- So the approach is as follows. First, when expanding a multiply | |
3029 | -- or divide whose type is universal fixed, we do nothing at all, | |
3030 | -- instead deferring the operation till later. | |
3031 | ||
3032 | -- The actual processing is done in Expand_N_Type_Conversion which | |
3033 | -- handles the special case of Round by looking at its parent to | |
3034 | -- see if it is a Round attribute, and if it is, handling the | |
3035 | -- conversion (or its fixed multiply/divide child) in an appropriate | |
3036 | -- manner. | |
3037 | ||
3038 | -- This means that by the time we get to expanding the Round attribute | |
3039 | -- itself, the Round is nothing more than a type conversion (and will | |
3040 | -- often be a null type conversion), so we just replace it with the | |
3041 | -- appropriate conversion operation. | |
3042 | ||
3043 | when Attribute_Round => | |
3044 | Rewrite (N, | |
3045 | Convert_To (Etype (N), Relocate_Node (First (Exprs)))); | |
3046 | Analyze_And_Resolve (N); | |
3047 | ||
3048 | -------------- | |
3049 | -- Rounding -- | |
3050 | -------------- | |
3051 | ||
3052 | -- Transforms 'Rounding into a call to the floating-point attribute | |
3053 | -- function Rounding in Fat_xxx (where xxx is the root type) | |
3054 | ||
3055 | when Attribute_Rounding => | |
3056 | Expand_Fpt_Attribute_R (N); | |
3057 | ||
3058 | ------------- | |
3059 | -- Scaling -- | |
3060 | ------------- | |
3061 | ||
3062 | -- Transforms 'Scaling into a call to the floating-point attribute | |
3063 | -- function Scaling in Fat_xxx (where xxx is the root type) | |
3064 | ||
3065 | when Attribute_Scaling => | |
3066 | Expand_Fpt_Attribute_RI (N); | |
3067 | ||
3068 | ---------- | |
3069 | -- Size -- | |
3070 | ---------- | |
3071 | ||
3072 | when Attribute_Size | | |
3073 | Attribute_Object_Size | | |
3074 | Attribute_Value_Size | | |
3075 | Attribute_VADS_Size => Size : | |
3076 | ||
3077 | declare | |
3078 | Ptyp : constant Entity_Id := Etype (Pref); | |
ee6ba406 | 3079 | Siz : Uint; |
9dfe12ae | 3080 | New_Node : Node_Id; |
ee6ba406 | 3081 | |
3082 | begin | |
3083 | -- Processing for VADS_Size case. Note that this processing removes | |
3084 | -- all traces of VADS_Size from the tree, and completes all required | |
3085 | -- processing for VADS_Size by translating the attribute reference | |
3086 | -- to an appropriate Size or Object_Size reference. | |
3087 | ||
3088 | if Id = Attribute_VADS_Size | |
3089 | or else (Use_VADS_Size and then Id = Attribute_Size) | |
3090 | then | |
3091 | -- If the size is specified, then we simply use the specified | |
3092 | -- size. This applies to both types and objects. The size of an | |
3093 | -- object can be specified in the following ways: | |
3094 | ||
3095 | -- An explicit size object is given for an object | |
3096 | -- A component size is specified for an indexed component | |
3097 | -- A component clause is specified for a selected component | |
3098 | -- The object is a component of a packed composite object | |
3099 | ||
3100 | -- If the size is specified, then VADS_Size of an object | |
3101 | ||
3102 | if (Is_Entity_Name (Pref) | |
3103 | and then Present (Size_Clause (Entity (Pref)))) | |
3104 | or else | |
3105 | (Nkind (Pref) = N_Component_Clause | |
3106 | and then (Present (Component_Clause | |
3107 | (Entity (Selector_Name (Pref)))) | |
3108 | or else Is_Packed (Etype (Prefix (Pref))))) | |
3109 | or else | |
3110 | (Nkind (Pref) = N_Indexed_Component | |
3111 | and then (Component_Size (Etype (Prefix (Pref))) /= 0 | |
3112 | or else Is_Packed (Etype (Prefix (Pref))))) | |
3113 | then | |
3114 | Set_Attribute_Name (N, Name_Size); | |
3115 | ||
3116 | -- Otherwise if we have an object rather than a type, then the | |
3117 | -- VADS_Size attribute applies to the type of the object, rather | |
3118 | -- than the object itself. This is one of the respects in which | |
3119 | -- VADS_Size differs from Size. | |
3120 | ||
3121 | else | |
3122 | if (not Is_Entity_Name (Pref) | |
3123 | or else not Is_Type (Entity (Pref))) | |
3124 | and then (Is_Scalar_Type (Etype (Pref)) | |
3125 | or else Is_Constrained (Etype (Pref))) | |
3126 | then | |
3127 | Rewrite (Pref, New_Occurrence_Of (Etype (Pref), Loc)); | |
3128 | end if; | |
3129 | ||
3130 | -- For a scalar type for which no size was | |
3131 | -- explicitly given, VADS_Size means Object_Size. This is the | |
3132 | -- other respect in which VADS_Size differs from Size. | |
3133 | ||
3134 | if Is_Scalar_Type (Etype (Pref)) | |
3135 | and then No (Size_Clause (Etype (Pref))) | |
3136 | then | |
3137 | Set_Attribute_Name (N, Name_Object_Size); | |
3138 | ||
3139 | -- In all other cases, Size and VADS_Size are the sane | |
3140 | ||
3141 | else | |
3142 | Set_Attribute_Name (N, Name_Size); | |
3143 | end if; | |
3144 | end if; | |
3145 | end if; | |
3146 | ||
9dfe12ae | 3147 | -- For class-wide types, X'Class'Size is transformed into a |
3148 | -- direct reference to the Size of the class type, so that gigi | |
3149 | -- does not have to deal with the X'Class'Size reference. | |
ee6ba406 | 3150 | |
9dfe12ae | 3151 | if Is_Entity_Name (Pref) |
3152 | and then Is_Class_Wide_Type (Entity (Pref)) | |
3153 | then | |
3154 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
3155 | return; | |
3156 | ||
7189d17f | 3157 | -- For x'Size applied to an object of a class-wide type, transform |
9dfe12ae | 3158 | -- X'Size into a call to the primitive operation _Size applied to X. |
3159 | ||
3160 | elsif Is_Class_Wide_Type (Ptyp) then | |
ee6ba406 | 3161 | New_Node := |
3162 | Make_Function_Call (Loc, | |
3163 | Name => New_Reference_To | |
3164 | (Find_Prim_Op (Ptyp, Name_uSize), Loc), | |
3165 | Parameter_Associations => New_List (Pref)); | |
3166 | ||
3167 | if Typ /= Standard_Long_Long_Integer then | |
3168 | ||
3169 | -- The context is a specific integer type with which the | |
3170 | -- original attribute was compatible. The function has a | |
3171 | -- specific type as well, so to preserve the compatibility | |
3172 | -- we must convert explicitly. | |
3173 | ||
3174 | New_Node := Convert_To (Typ, New_Node); | |
3175 | end if; | |
3176 | ||
3177 | Rewrite (N, New_Node); | |
3178 | Analyze_And_Resolve (N, Typ); | |
3179 | return; | |
3180 | ||
3181 | -- For an array component, we can do Size in the front end | |
3182 | -- if the component_size of the array is set. | |
3183 | ||
3184 | elsif Nkind (Pref) = N_Indexed_Component then | |
3185 | Siz := Component_Size (Etype (Prefix (Pref))); | |
3186 | ||
3187 | -- For a record component, we can do Size in the front end | |
3188 | -- if there is a component clause, or if the record is packed | |
3189 | -- and the component's size is known at compile time. | |
3190 | ||
3191 | elsif Nkind (Pref) = N_Selected_Component then | |
3192 | declare | |
3193 | Rec : constant Entity_Id := Etype (Prefix (Pref)); | |
3194 | Comp : constant Entity_Id := Entity (Selector_Name (Pref)); | |
3195 | ||
3196 | begin | |
3197 | if Present (Component_Clause (Comp)) then | |
3198 | Siz := Esize (Comp); | |
3199 | ||
3200 | elsif Is_Packed (Rec) then | |
3201 | Siz := RM_Size (Ptyp); | |
3202 | ||
3203 | else | |
3204 | Apply_Universal_Integer_Attribute_Checks (N); | |
3205 | return; | |
3206 | end if; | |
3207 | end; | |
3208 | ||
3209 | -- All other cases are handled by Gigi | |
3210 | ||
3211 | else | |
3212 | Apply_Universal_Integer_Attribute_Checks (N); | |
3213 | ||
3214 | -- If we have Size applied to a formal parameter, that is a | |
3215 | -- packed array subtype, then apply size to the actual subtype. | |
3216 | ||
3217 | if Is_Entity_Name (Pref) | |
3218 | and then Is_Formal (Entity (Pref)) | |
3219 | and then Is_Array_Type (Etype (Pref)) | |
3220 | and then Is_Packed (Etype (Pref)) | |
3221 | then | |
3222 | Rewrite (N, | |
3223 | Make_Attribute_Reference (Loc, | |
3224 | Prefix => | |
3225 | New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc), | |
3226 | Attribute_Name => Name_Size)); | |
3227 | Analyze_And_Resolve (N, Typ); | |
3228 | end if; | |
3229 | ||
3230 | return; | |
3231 | end if; | |
3232 | ||
3233 | -- Common processing for record and array component case | |
3234 | ||
3235 | if Siz /= 0 then | |
7189d17f | 3236 | Rewrite (N, Make_Integer_Literal (Loc, Siz)); |
ee6ba406 | 3237 | |
3238 | Analyze_And_Resolve (N, Typ); | |
3239 | ||
3240 | -- The result is not a static expression | |
3241 | ||
3242 | Set_Is_Static_Expression (N, False); | |
3243 | end if; | |
3244 | end Size; | |
3245 | ||
3246 | ------------------ | |
3247 | -- Storage_Pool -- | |
3248 | ------------------ | |
3249 | ||
3250 | when Attribute_Storage_Pool => | |
3251 | Rewrite (N, | |
3252 | Make_Type_Conversion (Loc, | |
3253 | Subtype_Mark => New_Reference_To (Etype (N), Loc), | |
3254 | Expression => New_Reference_To (Entity (N), Loc))); | |
3255 | Analyze_And_Resolve (N, Typ); | |
3256 | ||
3257 | ------------------ | |
3258 | -- Storage_Size -- | |
3259 | ------------------ | |
3260 | ||
3261 | when Attribute_Storage_Size => Storage_Size : | |
3262 | declare | |
3263 | Ptyp : constant Entity_Id := Etype (Pref); | |
3264 | ||
3265 | begin | |
3266 | -- Access type case, always go to the root type | |
3267 | ||
3268 | -- The case of access types results in a value of zero for the case | |
3269 | -- where no storage size attribute clause has been given. If a | |
3270 | -- storage size has been given, then the attribute is converted | |
3271 | -- to a reference to the variable used to hold this value. | |
3272 | ||
3273 | if Is_Access_Type (Ptyp) then | |
3274 | if Present (Storage_Size_Variable (Root_Type (Ptyp))) then | |
3275 | Rewrite (N, | |
3276 | Make_Attribute_Reference (Loc, | |
3277 | Prefix => New_Reference_To (Typ, Loc), | |
3278 | Attribute_Name => Name_Max, | |
3279 | Expressions => New_List ( | |
3280 | Make_Integer_Literal (Loc, 0), | |
3281 | Convert_To (Typ, | |
3282 | New_Reference_To | |
3283 | (Storage_Size_Variable (Root_Type (Ptyp)), Loc))))); | |
3284 | ||
3285 | elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then | |
3286 | Rewrite (N, | |
3287 | OK_Convert_To (Typ, | |
3288 | Make_Function_Call (Loc, | |
9dfe12ae | 3289 | Name => |
3290 | New_Reference_To | |
3291 | (Find_Prim_Op | |
3292 | (Etype (Associated_Storage_Pool (Root_Type (Ptyp))), | |
3293 | Attribute_Name (N)), | |
3294 | Loc), | |
ee6ba406 | 3295 | |
3296 | Parameter_Associations => New_List (New_Reference_To ( | |
3297 | Associated_Storage_Pool (Root_Type (Ptyp)), Loc))))); | |
3298 | else | |
3299 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
3300 | end if; | |
3301 | ||
3302 | Analyze_And_Resolve (N, Typ); | |
3303 | ||
3304 | -- The case of a task type (an obsolescent feature) is handled the | |
3305 | -- same way, seems as reasonable as anything, and it is what the | |
3306 | -- ACVC tests (e.g. CD1009K) seem to expect. | |
3307 | ||
3308 | -- If there is no Storage_Size variable, then we return the default | |
3309 | -- task stack size, otherwise, expand a Storage_Size attribute as | |
3310 | -- follows: | |
3311 | ||
3312 | -- Typ (Adjust_Storage_Size (taskZ)) | |
3313 | ||
3314 | -- except for the case of a task object which has a Storage_Size | |
3315 | -- pragma: | |
3316 | ||
3317 | -- Typ (Adjust_Storage_Size (taskV!(name)._Size)) | |
3318 | ||
3319 | else | |
3320 | if not Present (Storage_Size_Variable (Ptyp)) then | |
3321 | Rewrite (N, | |
3322 | Convert_To (Typ, | |
3323 | Make_Function_Call (Loc, | |
3324 | Name => | |
3325 | New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc)))); | |
3326 | ||
3327 | else | |
3328 | if not (Is_Entity_Name (Pref) and then | |
3329 | Is_Task_Type (Entity (Pref))) and then | |
3330 | Chars (Last_Entity (Corresponding_Record_Type (Ptyp))) = | |
3331 | Name_uSize | |
3332 | then | |
3333 | Rewrite (N, | |
3334 | Convert_To (Typ, | |
3335 | Make_Function_Call (Loc, | |
3336 | Name => New_Occurrence_Of ( | |
3337 | RTE (RE_Adjust_Storage_Size), Loc), | |
3338 | Parameter_Associations => | |
3339 | New_List ( | |
3340 | Make_Selected_Component (Loc, | |
3341 | Prefix => | |
3342 | Unchecked_Convert_To ( | |
3343 | Corresponding_Record_Type (Ptyp), | |
3344 | New_Copy_Tree (Pref)), | |
3345 | Selector_Name => | |
3346 | Make_Identifier (Loc, Name_uSize)))))); | |
3347 | ||
3348 | -- Task not having Storage_Size pragma | |
3349 | ||
3350 | else | |
3351 | Rewrite (N, | |
3352 | Convert_To (Typ, | |
3353 | Make_Function_Call (Loc, | |
3354 | Name => New_Occurrence_Of ( | |
3355 | RTE (RE_Adjust_Storage_Size), Loc), | |
3356 | Parameter_Associations => | |
3357 | New_List ( | |
3358 | New_Reference_To ( | |
3359 | Storage_Size_Variable (Ptyp), Loc))))); | |
3360 | end if; | |
3361 | ||
3362 | Analyze_And_Resolve (N, Typ); | |
3363 | end if; | |
3364 | end if; | |
3365 | end Storage_Size; | |
3366 | ||
7189d17f | 3367 | ----------------- |
3368 | -- Stream_Size -- | |
3369 | ----------------- | |
3370 | ||
3371 | when Attribute_Stream_Size => Stream_Size : declare | |
3372 | Ptyp : constant Entity_Id := Etype (Pref); | |
3373 | Size : Int; | |
3374 | ||
3375 | begin | |
3376 | -- If we have a Stream_Size clause for this type use it, otherwise | |
3377 | -- the Stream_Size if the size of the type. | |
3378 | ||
3379 | if Has_Stream_Size_Clause (Ptyp) then | |
3380 | Size := UI_To_Int | |
3381 | (Static_Integer (Expression (Stream_Size_Clause (Ptyp)))); | |
3382 | else | |
3383 | Size := UI_To_Int (Esize (Ptyp)); | |
3384 | end if; | |
3385 | ||
3386 | Rewrite (N, Make_Integer_Literal (Loc, Intval => Size)); | |
3387 | Analyze_And_Resolve (N, Typ); | |
3388 | end Stream_Size; | |
3389 | ||
ee6ba406 | 3390 | ---------- |
3391 | -- Succ -- | |
3392 | ---------- | |
3393 | ||
3394 | -- 1. Deal with enumeration types with holes | |
3395 | -- 2. For floating-point, generate call to attribute function | |
3396 | -- 3. For other cases, deal with constraint checking | |
3397 | ||
3398 | when Attribute_Succ => Succ : | |
3399 | declare | |
3400 | Ptyp : constant Entity_Id := Base_Type (Etype (Pref)); | |
3401 | ||
3402 | begin | |
3403 | -- For enumeration types with non-standard representations, we | |
3404 | -- expand typ'Succ (x) into | |
3405 | ||
3406 | -- Pos_To_Rep (Rep_To_Pos (x) + 1) | |
3407 | ||
9dfe12ae | 3408 | -- If the representation is contiguous, we compute instead |
3409 | -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations. | |
3410 | ||
ee6ba406 | 3411 | if Is_Enumeration_Type (Ptyp) |
3412 | and then Present (Enum_Pos_To_Rep (Ptyp)) | |
3413 | then | |
9dfe12ae | 3414 | if Has_Contiguous_Rep (Ptyp) then |
3415 | Rewrite (N, | |
3416 | Unchecked_Convert_To (Ptyp, | |
3417 | Make_Op_Add (Loc, | |
3418 | Left_Opnd => | |
3419 | Make_Integer_Literal (Loc, | |
3420 | Enumeration_Rep (First_Literal (Ptyp))), | |
3421 | Right_Opnd => | |
3422 | Make_Function_Call (Loc, | |
3423 | Name => | |
3424 | New_Reference_To | |
3425 | (TSS (Ptyp, TSS_Rep_To_Pos), Loc), | |
3426 | ||
3427 | Parameter_Associations => | |
3428 | New_List ( | |
3429 | Unchecked_Convert_To (Ptyp, | |
3430 | Make_Op_Add (Loc, | |
3431 | Left_Opnd => | |
3432 | Unchecked_Convert_To (Standard_Integer, | |
3433 | Relocate_Node (First (Exprs))), | |
3434 | Right_Opnd => | |
3435 | Make_Integer_Literal (Loc, 1))), | |
3436 | Rep_To_Pos_Flag (Ptyp, Loc)))))); | |
3437 | else | |
3438 | -- Add Boolean parameter True, to request program errror if | |
3439 | -- we have a bad representation on our hands. Add False if | |
3440 | -- checks are suppressed. | |
ee6ba406 | 3441 | |
9dfe12ae | 3442 | Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc)); |
3443 | Rewrite (N, | |
3444 | Make_Indexed_Component (Loc, | |
3445 | Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc), | |
3446 | Expressions => New_List ( | |
3447 | Make_Op_Add (Loc, | |
3448 | Left_Opnd => | |
3449 | Make_Function_Call (Loc, | |
3450 | Name => | |
3451 | New_Reference_To | |
3452 | (TSS (Ptyp, TSS_Rep_To_Pos), Loc), | |
3453 | Parameter_Associations => Exprs), | |
3454 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); | |
3455 | end if; | |
ee6ba406 | 3456 | |
3457 | Analyze_And_Resolve (N, Typ); | |
3458 | ||
3459 | -- For floating-point, we transform 'Succ into a call to the Succ | |
3460 | -- floating-point attribute function in Fat_xxx (xxx is root type) | |
3461 | ||
3462 | elsif Is_Floating_Point_Type (Ptyp) then | |
3463 | Expand_Fpt_Attribute_R (N); | |
3464 | Analyze_And_Resolve (N, Typ); | |
3465 | ||
3466 | -- For modular types, nothing to do (no overflow, since wraps) | |
3467 | ||
3468 | elsif Is_Modular_Integer_Type (Ptyp) then | |
3469 | null; | |
3470 | ||
3471 | -- For other types, if range checking is enabled, we must generate | |
3472 | -- a check if overflow checking is enabled. | |
3473 | ||
3474 | elsif not Overflow_Checks_Suppressed (Ptyp) then | |
3475 | Expand_Pred_Succ (N); | |
3476 | end if; | |
3477 | end Succ; | |
3478 | ||
3479 | --------- | |
3480 | -- Tag -- | |
3481 | --------- | |
3482 | ||
3483 | -- Transforms X'Tag into a direct reference to the tag of X | |
3484 | ||
3485 | when Attribute_Tag => Tag : | |
3486 | declare | |
3487 | Ttyp : Entity_Id; | |
3488 | Prefix_Is_Type : Boolean; | |
3489 | ||
3490 | begin | |
3491 | if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then | |
3492 | Ttyp := Entity (Pref); | |
3493 | Prefix_Is_Type := True; | |
3494 | else | |
3495 | Ttyp := Etype (Pref); | |
3496 | Prefix_Is_Type := False; | |
3497 | end if; | |
3498 | ||
3499 | if Is_Class_Wide_Type (Ttyp) then | |
3500 | Ttyp := Root_Type (Ttyp); | |
3501 | end if; | |
3502 | ||
3503 | Ttyp := Underlying_Type (Ttyp); | |
3504 | ||
3505 | if Prefix_Is_Type then | |
1d7e0b5b | 3506 | |
3507 | -- For JGNAT we leave the type attribute unexpanded because | |
3508 | -- there's not a dispatching table to reference. | |
3509 | ||
3510 | if not Java_VM then | |
3511 | Rewrite (N, | |
3512 | Unchecked_Convert_To (RTE (RE_Tag), | |
3513 | New_Reference_To (Access_Disp_Table (Ttyp), Loc))); | |
3514 | Analyze_And_Resolve (N, RTE (RE_Tag)); | |
3515 | end if; | |
ee6ba406 | 3516 | |
3517 | else | |
3518 | Rewrite (N, | |
3519 | Make_Selected_Component (Loc, | |
3520 | Prefix => Relocate_Node (Pref), | |
3521 | Selector_Name => | |
3522 | New_Reference_To (Tag_Component (Ttyp), Loc))); | |
1d7e0b5b | 3523 | Analyze_And_Resolve (N, RTE (RE_Tag)); |
ee6ba406 | 3524 | end if; |
ee6ba406 | 3525 | end Tag; |
3526 | ||
3527 | ---------------- | |
3528 | -- Terminated -- | |
3529 | ---------------- | |
3530 | ||
3531 | -- Transforms 'Terminated attribute into a call to Terminated function. | |
3532 | ||
3533 | when Attribute_Terminated => Terminated : | |
3534 | begin | |
3535 | if Restricted_Profile then | |
3536 | Rewrite (N, | |
3537 | Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated))); | |
3538 | ||
3539 | else | |
3540 | Rewrite (N, | |
3541 | Build_Call_With_Task (Pref, RTE (RE_Terminated))); | |
3542 | end if; | |
3543 | ||
3544 | Analyze_And_Resolve (N, Standard_Boolean); | |
3545 | end Terminated; | |
3546 | ||
3547 | ---------------- | |
3548 | -- To_Address -- | |
3549 | ---------------- | |
3550 | ||
3551 | -- Transforms System'To_Address (X) into unchecked conversion | |
3552 | -- from (integral) type of X to type address. | |
3553 | ||
3554 | when Attribute_To_Address => | |
3555 | Rewrite (N, | |
3556 | Unchecked_Convert_To (RTE (RE_Address), | |
3557 | Relocate_Node (First (Exprs)))); | |
3558 | Analyze_And_Resolve (N, RTE (RE_Address)); | |
3559 | ||
3560 | ---------------- | |
3561 | -- Truncation -- | |
3562 | ---------------- | |
3563 | ||
3564 | -- Transforms 'Truncation into a call to the floating-point attribute | |
3565 | -- function Truncation in Fat_xxx (where xxx is the root type) | |
3566 | ||
3567 | when Attribute_Truncation => | |
3568 | Expand_Fpt_Attribute_R (N); | |
3569 | ||
3570 | ----------------------- | |
3571 | -- Unbiased_Rounding -- | |
3572 | ----------------------- | |
3573 | ||
3574 | -- Transforms 'Unbiased_Rounding into a call to the floating-point | |
3575 | -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the | |
3576 | -- root type) | |
3577 | ||
3578 | when Attribute_Unbiased_Rounding => | |
3579 | Expand_Fpt_Attribute_R (N); | |
3580 | ||
3581 | ---------------------- | |
3582 | -- Unchecked_Access -- | |
3583 | ---------------------- | |
3584 | ||
3585 | when Attribute_Unchecked_Access => | |
3586 | Expand_Access_To_Type (N); | |
3587 | ||
3588 | ----------------- | |
3589 | -- UET_Address -- | |
3590 | ----------------- | |
3591 | ||
3592 | when Attribute_UET_Address => UET_Address : declare | |
3593 | Ent : constant Entity_Id := | |
3594 | Make_Defining_Identifier (Loc, New_Internal_Name ('T')); | |
3595 | ||
3596 | begin | |
3597 | Insert_Action (N, | |
3598 | Make_Object_Declaration (Loc, | |
3599 | Defining_Identifier => Ent, | |
3600 | Aliased_Present => True, | |
3601 | Object_Definition => | |
3602 | New_Occurrence_Of (RTE (RE_Address), Loc))); | |
3603 | ||
3604 | -- Construct name __gnat_xxx__SDP, where xxx is the unit name | |
3605 | -- in normal external form. | |
3606 | ||
3607 | Get_External_Unit_Name_String (Get_Unit_Name (Pref)); | |
3608 | Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len); | |
3609 | Name_Len := Name_Len + 7; | |
3610 | Name_Buffer (1 .. 7) := "__gnat_"; | |
3611 | Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP"; | |
3612 | Name_Len := Name_Len + 5; | |
3613 | ||
3614 | Set_Is_Imported (Ent); | |
3615 | Set_Interface_Name (Ent, | |
3616 | Make_String_Literal (Loc, | |
3617 | Strval => String_From_Name_Buffer)); | |
3618 | ||
3619 | Rewrite (N, | |
3620 | Make_Attribute_Reference (Loc, | |
3621 | Prefix => New_Occurrence_Of (Ent, Loc), | |
3622 | Attribute_Name => Name_Address)); | |
3623 | ||
3624 | Analyze_And_Resolve (N, Typ); | |
3625 | end UET_Address; | |
3626 | ||
3627 | ------------------------- | |
3628 | -- Unrestricted_Access -- | |
3629 | ------------------------- | |
3630 | ||
3631 | when Attribute_Unrestricted_Access => | |
3632 | Expand_Access_To_Type (N); | |
3633 | ||
3634 | --------------- | |
3635 | -- VADS_Size -- | |
3636 | --------------- | |
3637 | ||
3638 | -- The processing for VADS_Size is shared with Size | |
3639 | ||
3640 | --------- | |
3641 | -- Val -- | |
3642 | --------- | |
3643 | ||
3644 | -- For enumeration types with a standard representation, and for all | |
3645 | -- other types, Val is handled by Gigi. For enumeration types with | |
3646 | -- a non-standard representation we use the _Pos_To_Rep array that | |
3647 | -- was created when the type was frozen. | |
3648 | ||
3649 | when Attribute_Val => Val : | |
3650 | declare | |
3651 | Etyp : constant Entity_Id := Base_Type (Entity (Pref)); | |
3652 | ||
3653 | begin | |
3654 | if Is_Enumeration_Type (Etyp) | |
3655 | and then Present (Enum_Pos_To_Rep (Etyp)) | |
3656 | then | |
9dfe12ae | 3657 | if Has_Contiguous_Rep (Etyp) then |
3658 | declare | |
3659 | Rep_Node : constant Node_Id := | |
3660 | Unchecked_Convert_To (Etyp, | |
3661 | Make_Op_Add (Loc, | |
3662 | Left_Opnd => | |
3663 | Make_Integer_Literal (Loc, | |
3664 | Enumeration_Rep (First_Literal (Etyp))), | |
3665 | Right_Opnd => | |
3666 | (Convert_To (Standard_Integer, | |
3667 | Relocate_Node (First (Exprs)))))); | |
3668 | ||
3669 | begin | |
3670 | Rewrite (N, | |
3671 | Unchecked_Convert_To (Etyp, | |
3672 | Make_Op_Add (Loc, | |
3673 | Left_Opnd => | |
3674 | Make_Integer_Literal (Loc, | |
3675 | Enumeration_Rep (First_Literal (Etyp))), | |
3676 | Right_Opnd => | |
3677 | Make_Function_Call (Loc, | |
3678 | Name => | |
3679 | New_Reference_To | |
3680 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), | |
3681 | Parameter_Associations => New_List ( | |
3682 | Rep_Node, | |
3683 | Rep_To_Pos_Flag (Etyp, Loc)))))); | |
3684 | end; | |
3685 | ||
3686 | else | |
3687 | Rewrite (N, | |
3688 | Make_Indexed_Component (Loc, | |
3689 | Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc), | |
3690 | Expressions => New_List ( | |
3691 | Convert_To (Standard_Integer, | |
3692 | Relocate_Node (First (Exprs)))))); | |
3693 | end if; | |
ee6ba406 | 3694 | |
3695 | Analyze_And_Resolve (N, Typ); | |
3696 | end if; | |
3697 | end Val; | |
3698 | ||
3699 | ----------- | |
3700 | -- Valid -- | |
3701 | ----------- | |
3702 | ||
3703 | -- The code for valid is dependent on the particular types involved. | |
3704 | -- See separate sections below for the generated code in each case. | |
3705 | ||
3706 | when Attribute_Valid => Valid : | |
3707 | declare | |
3708 | Ptyp : constant Entity_Id := Etype (Pref); | |
9dfe12ae | 3709 | Btyp : Entity_Id := Base_Type (Ptyp); |
ee6ba406 | 3710 | Tst : Node_Id; |
3711 | ||
9dfe12ae | 3712 | Save_Validity_Checks_On : constant Boolean := Validity_Checks_On; |
3713 | -- Save the validity checking mode. We always turn off validity | |
3714 | -- checking during process of 'Valid since this is one place | |
3715 | -- where we do not want the implicit validity checks to intefere | |
3716 | -- with the explicit validity check that the programmer is doing. | |
3717 | ||
ee6ba406 | 3718 | function Make_Range_Test return Node_Id; |
3719 | -- Build the code for a range test of the form | |
3720 | -- Btyp!(Pref) >= Btyp!(Ptyp'First) | |
3721 | -- and then | |
3722 | -- Btyp!(Pref) <= Btyp!(Ptyp'Last) | |
3723 | ||
9dfe12ae | 3724 | --------------------- |
3725 | -- Make_Range_Test -- | |
3726 | --------------------- | |
3727 | ||
ee6ba406 | 3728 | function Make_Range_Test return Node_Id is |
3729 | begin | |
3730 | return | |
3731 | Make_And_Then (Loc, | |
3732 | Left_Opnd => | |
3733 | Make_Op_Ge (Loc, | |
3734 | Left_Opnd => | |
3735 | Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)), | |
3736 | ||
3737 | Right_Opnd => | |
3738 | Unchecked_Convert_To (Btyp, | |
3739 | Make_Attribute_Reference (Loc, | |
3740 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
3741 | Attribute_Name => Name_First))), | |
3742 | ||
3743 | Right_Opnd => | |
3744 | Make_Op_Le (Loc, | |
3745 | Left_Opnd => | |
9dfe12ae | 3746 | Unchecked_Convert_To (Btyp, |
3747 | Duplicate_Subexpr_No_Checks (Pref)), | |
ee6ba406 | 3748 | |
3749 | Right_Opnd => | |
3750 | Unchecked_Convert_To (Btyp, | |
3751 | Make_Attribute_Reference (Loc, | |
3752 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
3753 | Attribute_Name => Name_Last)))); | |
3754 | end Make_Range_Test; | |
3755 | ||
3756 | -- Start of processing for Attribute_Valid | |
3757 | ||
3758 | begin | |
9dfe12ae | 3759 | -- Turn off validity checks. We do not want any implicit validity |
3760 | -- checks to intefere with the explicit check from the attribute | |
3761 | ||
3762 | Validity_Checks_On := False; | |
3763 | ||
ee6ba406 | 3764 | -- Floating-point case. This case is handled by the Valid attribute |
3765 | -- code in the floating-point attribute run-time library. | |
3766 | ||
3767 | if Is_Floating_Point_Type (Ptyp) then | |
3768 | declare | |
3769 | Rtp : constant Entity_Id := Root_Type (Etype (Pref)); | |
3770 | ||
3771 | begin | |
9dfe12ae | 3772 | -- If the floating-point object might be unaligned, we need |
3773 | -- to call the special routine Unaligned_Valid, which makes | |
3774 | -- the needed copy, being careful not to load the value into | |
3775 | -- any floating-point register. The argument in this case is | |
3776 | -- obj'Address (see Unchecked_Valid routine in s-fatgen.ads). | |
3777 | ||
3778 | if Is_Possibly_Unaligned_Object (Pref) then | |
3779 | Set_Attribute_Name (N, Name_Unaligned_Valid); | |
3780 | Expand_Fpt_Attribute | |
3781 | (N, Rtp, Name_Unaligned_Valid, | |
3782 | New_List ( | |
3783 | Make_Attribute_Reference (Loc, | |
3784 | Prefix => Relocate_Node (Pref), | |
3785 | Attribute_Name => Name_Address))); | |
3786 | ||
3787 | -- In the normal case where we are sure the object is aligned, | |
3788 | -- we generate a caqll to Valid, and the argument in this case | |
3789 | -- is obj'Unrestricted_Access (after converting obj to the | |
3790 | -- right floating-point type). | |
3791 | ||
3792 | else | |
3793 | Expand_Fpt_Attribute | |
3794 | (N, Rtp, Name_Valid, | |
3795 | New_List ( | |
3796 | Make_Attribute_Reference (Loc, | |
3797 | Prefix => Unchecked_Convert_To (Rtp, Pref), | |
3798 | Attribute_Name => Name_Unrestricted_Access))); | |
3799 | end if; | |
ee6ba406 | 3800 | |
3801 | -- One more task, we still need a range check. Required | |
3802 | -- only if we have a constraint, since the Valid routine | |
3803 | -- catches infinities properly (infinities are never valid). | |
3804 | ||
3805 | -- The way we do the range check is simply to create the | |
3806 | -- expression: Valid (N) and then Base_Type(Pref) in Typ. | |
3807 | ||
3808 | if not Subtypes_Statically_Match (Ptyp, Btyp) then | |
3809 | Rewrite (N, | |
3810 | Make_And_Then (Loc, | |
3811 | Left_Opnd => Relocate_Node (N), | |
3812 | Right_Opnd => | |
3813 | Make_In (Loc, | |
3814 | Left_Opnd => Convert_To (Btyp, Pref), | |
3815 | Right_Opnd => New_Occurrence_Of (Ptyp, Loc)))); | |
3816 | end if; | |
3817 | end; | |
3818 | ||
3819 | -- Enumeration type with holes | |
3820 | ||
3821 | -- For enumeration types with holes, the Pos value constructed by | |
3822 | -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a | |
3823 | -- second argument of False returns minus one for an invalid value, | |
3824 | -- and the non-negative pos value for a valid value, so the | |
3825 | -- expansion of X'Valid is simply: | |
3826 | ||
3827 | -- type(X)'Pos (X) >= 0 | |
3828 | ||
3829 | -- We can't quite generate it that way because of the requirement | |
5329ca64 | 3830 | -- for the non-standard second argument of False in the resulting |
3831 | -- rep_to_pos call, so we have to explicitly create: | |
ee6ba406 | 3832 | |
3833 | -- _rep_to_pos (X, False) >= 0 | |
3834 | ||
3835 | -- If we have an enumeration subtype, we also check that the | |
3836 | -- value is in range: | |
3837 | ||
3838 | -- _rep_to_pos (X, False) >= 0 | |
3839 | -- and then | |
5329ca64 | 3840 | -- (X >= type(X)'First and then type(X)'Last <= X) |
ee6ba406 | 3841 | |
3842 | elsif Is_Enumeration_Type (Ptyp) | |
3843 | and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp))) | |
3844 | then | |
3845 | Tst := | |
3846 | Make_Op_Ge (Loc, | |
3847 | Left_Opnd => | |
3848 | Make_Function_Call (Loc, | |
3849 | Name => | |
3850 | New_Reference_To | |
9dfe12ae | 3851 | (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc), |
ee6ba406 | 3852 | Parameter_Associations => New_List ( |
3853 | Pref, | |
3854 | New_Occurrence_Of (Standard_False, Loc))), | |
3855 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
3856 | ||
3857 | if Ptyp /= Btyp | |
3858 | and then | |
3859 | (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp) | |
3860 | or else | |
3861 | Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp)) | |
3862 | then | |
3863 | -- The call to Make_Range_Test will create declarations | |
3864 | -- that need a proper insertion point, but Pref is now | |
3865 | -- attached to a node with no ancestor. Attach to tree | |
3866 | -- even if it is to be rewritten below. | |
3867 | ||
3868 | Set_Parent (Tst, Parent (N)); | |
3869 | ||
3870 | Tst := | |
3871 | Make_And_Then (Loc, | |
3872 | Left_Opnd => Make_Range_Test, | |
3873 | Right_Opnd => Tst); | |
3874 | end if; | |
3875 | ||
3876 | Rewrite (N, Tst); | |
3877 | ||
3878 | -- Fortran convention booleans | |
3879 | ||
3880 | -- For the very special case of Fortran convention booleans, the | |
3881 | -- value is always valid, since it is an integer with the semantics | |
3882 | -- that non-zero is true, and any value is permissible. | |
3883 | ||
3884 | elsif Is_Boolean_Type (Ptyp) | |
3885 | and then Convention (Ptyp) = Convention_Fortran | |
3886 | then | |
3887 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
3888 | ||
3889 | -- For biased representations, we will be doing an unchecked | |
3890 | -- conversion without unbiasing the result. That means that | |
3891 | -- the range test has to take this into account, and the | |
3892 | -- proper form of the test is: | |
3893 | ||
3894 | -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length) | |
3895 | ||
3896 | elsif Has_Biased_Representation (Ptyp) then | |
3897 | Btyp := RTE (RE_Unsigned_32); | |
3898 | Rewrite (N, | |
3899 | Make_Op_Lt (Loc, | |
3900 | Left_Opnd => | |
3901 | Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)), | |
3902 | Right_Opnd => | |
3903 | Unchecked_Convert_To (Btyp, | |
3904 | Make_Attribute_Reference (Loc, | |
3905 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
3906 | Attribute_Name => Name_Range_Length)))); | |
3907 | ||
3908 | -- For all other scalar types, what we want logically is a | |
3909 | -- range test: | |
3910 | ||
3911 | -- X in type(X)'First .. type(X)'Last | |
3912 | ||
3913 | -- But that's precisely what won't work because of possible | |
3914 | -- unwanted optimization (and indeed the basic motivation for | |
5329ca64 | 3915 | -- the Valid attribute is exactly that this test does not work!) |
ee6ba406 | 3916 | -- What will work is: |
3917 | ||
3918 | -- Btyp!(X) >= Btyp!(type(X)'First) | |
3919 | -- and then | |
3920 | -- Btyp!(X) <= Btyp!(type(X)'Last) | |
3921 | ||
3922 | -- where Btyp is an integer type large enough to cover the full | |
3923 | -- range of possible stored values (i.e. it is chosen on the basis | |
3924 | -- of the size of the type, not the range of the values). We write | |
3925 | -- this as two tests, rather than a range check, so that static | |
3926 | -- evaluation will easily remove either or both of the checks if | |
3927 | -- they can be -statically determined to be true (this happens | |
3928 | -- when the type of X is static and the range extends to the full | |
3929 | -- range of stored values). | |
3930 | ||
3931 | -- Unsigned types. Note: it is safe to consider only whether the | |
3932 | -- subtype is unsigned, since we will in that case be doing all | |
3933 | -- unsigned comparisons based on the subtype range. Since we use | |
3934 | -- the actual subtype object size, this is appropriate. | |
3935 | ||
3936 | -- For example, if we have | |
3937 | ||
3938 | -- subtype x is integer range 1 .. 200; | |
3939 | -- for x'Object_Size use 8; | |
3940 | ||
3941 | -- Now the base type is signed, but objects of this type are 8 | |
3942 | -- bits unsigned, and doing an unsigned test of the range 1 to | |
3943 | -- 200 is correct, even though a value greater than 127 looks | |
3944 | -- signed to a signed comparison. | |
3945 | ||
3946 | elsif Is_Unsigned_Type (Ptyp) then | |
3947 | if Esize (Ptyp) <= 32 then | |
3948 | Btyp := RTE (RE_Unsigned_32); | |
3949 | else | |
3950 | Btyp := RTE (RE_Unsigned_64); | |
3951 | end if; | |
3952 | ||
3953 | Rewrite (N, Make_Range_Test); | |
3954 | ||
3955 | -- Signed types | |
3956 | ||
3957 | else | |
3958 | if Esize (Ptyp) <= Esize (Standard_Integer) then | |
3959 | Btyp := Standard_Integer; | |
3960 | else | |
3961 | Btyp := Universal_Integer; | |
3962 | end if; | |
3963 | ||
3964 | Rewrite (N, Make_Range_Test); | |
3965 | end if; | |
3966 | ||
3967 | Analyze_And_Resolve (N, Standard_Boolean); | |
9dfe12ae | 3968 | Validity_Checks_On := Save_Validity_Checks_On; |
ee6ba406 | 3969 | end Valid; |
3970 | ||
3971 | ----------- | |
3972 | -- Value -- | |
3973 | ----------- | |
3974 | ||
3975 | -- Value attribute is handled in separate unti Exp_Imgv | |
3976 | ||
3977 | when Attribute_Value => | |
3978 | Exp_Imgv.Expand_Value_Attribute (N); | |
3979 | ||
3980 | ----------------- | |
3981 | -- Value_Size -- | |
3982 | ----------------- | |
3983 | ||
3984 | -- The processing for Value_Size shares the processing for Size | |
3985 | ||
3986 | ------------- | |
3987 | -- Version -- | |
3988 | ------------- | |
3989 | ||
3990 | -- The processing for Version shares the processing for Body_Version | |
3991 | ||
3992 | ---------------- | |
3993 | -- Wide_Image -- | |
3994 | ---------------- | |
3995 | ||
3996 | -- We expand typ'Wide_Image (X) into | |
3997 | ||
3998 | -- String_To_Wide_String | |
3999 | -- (typ'Image (X), Wide_Character_Encoding_Method) | |
4000 | ||
4001 | -- This works in all cases because String_To_Wide_String converts any | |
4002 | -- wide character escape sequences resulting from the Image call to the | |
4003 | -- proper Wide_Character equivalent | |
4004 | ||
4005 | -- not quite right for typ = Wide_Character ??? | |
4006 | ||
4007 | when Attribute_Wide_Image => Wide_Image : | |
4008 | begin | |
4009 | Rewrite (N, | |
4010 | Make_Function_Call (Loc, | |
4011 | Name => New_Reference_To (RTE (RE_String_To_Wide_String), Loc), | |
4012 | Parameter_Associations => New_List ( | |
4013 | Make_Attribute_Reference (Loc, | |
4014 | Prefix => Pref, | |
4015 | Attribute_Name => Name_Image, | |
4016 | Expressions => Exprs), | |
4017 | ||
4018 | Make_Integer_Literal (Loc, | |
4019 | Intval => Int (Wide_Character_Encoding_Method))))); | |
4020 | ||
4021 | Analyze_And_Resolve (N, Standard_Wide_String); | |
4022 | end Wide_Image; | |
4023 | ||
7189d17f | 4024 | --------------------- |
4025 | -- Wide_Wide_Image -- | |
4026 | --------------------- | |
4027 | ||
4028 | -- We expand typ'Wide_Wide_Image (X) into | |
4029 | ||
4030 | -- String_To_Wide_Wide_String | |
4031 | -- (typ'Image (X), Wide_Character_Encoding_Method) | |
4032 | ||
4033 | -- This works in all cases because String_To_Wide_Wide_String converts | |
4034 | -- any wide character escape sequences resulting from the Image call to | |
4035 | -- the proper Wide_Character equivalent | |
4036 | ||
4037 | -- not quite right for typ = Wide_Wide_Character ??? | |
4038 | ||
4039 | when Attribute_Wide_Wide_Image => Wide_Wide_Image : | |
4040 | begin | |
4041 | Rewrite (N, | |
4042 | Make_Function_Call (Loc, | |
4043 | Name => New_Reference_To | |
4044 | (RTE (RE_String_To_Wide_Wide_String), Loc), | |
4045 | Parameter_Associations => New_List ( | |
4046 | Make_Attribute_Reference (Loc, | |
4047 | Prefix => Pref, | |
4048 | Attribute_Name => Name_Image, | |
4049 | Expressions => Exprs), | |
4050 | ||
4051 | Make_Integer_Literal (Loc, | |
4052 | Intval => Int (Wide_Character_Encoding_Method))))); | |
4053 | ||
4054 | Analyze_And_Resolve (N, Standard_Wide_Wide_String); | |
4055 | end Wide_Wide_Image; | |
4056 | ||
ee6ba406 | 4057 | ---------------- |
4058 | -- Wide_Value -- | |
4059 | ---------------- | |
4060 | ||
4061 | -- We expand typ'Wide_Value (X) into | |
4062 | ||
4063 | -- typ'Value | |
4064 | -- (Wide_String_To_String (X, Wide_Character_Encoding_Method)) | |
4065 | ||
4066 | -- Wide_String_To_String is a runtime function that converts its wide | |
4067 | -- string argument to String, converting any non-translatable characters | |
4068 | -- into appropriate escape sequences. This preserves the required | |
4069 | -- semantics of Wide_Value in all cases, and results in a very simple | |
4070 | -- implementation approach. | |
4071 | ||
4072 | -- It's not quite right where typ = Wide_Character, because the encoding | |
4073 | -- method may not cover the whole character type ??? | |
4074 | ||
4075 | when Attribute_Wide_Value => Wide_Value : | |
4076 | begin | |
4077 | Rewrite (N, | |
4078 | Make_Attribute_Reference (Loc, | |
4079 | Prefix => Pref, | |
4080 | Attribute_Name => Name_Value, | |
4081 | ||
4082 | Expressions => New_List ( | |
4083 | Make_Function_Call (Loc, | |
4084 | Name => | |
4085 | New_Reference_To (RTE (RE_Wide_String_To_String), Loc), | |
4086 | ||
4087 | Parameter_Associations => New_List ( | |
4088 | Relocate_Node (First (Exprs)), | |
4089 | Make_Integer_Literal (Loc, | |
4090 | Intval => Int (Wide_Character_Encoding_Method))))))); | |
4091 | ||
4092 | Analyze_And_Resolve (N, Typ); | |
4093 | end Wide_Value; | |
4094 | ||
7189d17f | 4095 | --------------------- |
4096 | -- Wide_Wide_Value -- | |
4097 | --------------------- | |
4098 | ||
4099 | -- We expand typ'Wide_Value_Value (X) into | |
4100 | ||
4101 | -- typ'Value | |
4102 | -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method)) | |
4103 | ||
4104 | -- Wide_Wide_String_To_String is a runtime function that converts its | |
4105 | -- wide string argument to String, converting any non-translatable | |
4106 | -- characters into appropriate escape sequences. This preserves the | |
4107 | -- required semantics of Wide_Wide_Value in all cases, and results in a | |
4108 | -- very simple implementation approach. | |
4109 | ||
4110 | -- It's not quite right where typ = Wide_Wide_Character, because the | |
4111 | -- encoding method may not cover the whole character type ??? | |
4112 | ||
4113 | when Attribute_Wide_Wide_Value => Wide_Wide_Value : | |
4114 | begin | |
4115 | Rewrite (N, | |
4116 | Make_Attribute_Reference (Loc, | |
4117 | Prefix => Pref, | |
4118 | Attribute_Name => Name_Value, | |
4119 | ||
4120 | Expressions => New_List ( | |
4121 | Make_Function_Call (Loc, | |
4122 | Name => | |
4123 | New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc), | |
4124 | ||
4125 | Parameter_Associations => New_List ( | |
4126 | Relocate_Node (First (Exprs)), | |
4127 | Make_Integer_Literal (Loc, | |
4128 | Intval => Int (Wide_Character_Encoding_Method))))))); | |
4129 | ||
4130 | Analyze_And_Resolve (N, Typ); | |
4131 | end Wide_Wide_Value; | |
4132 | ||
4133 | --------------------- | |
4134 | -- Wide_Wide_Width -- | |
4135 | --------------------- | |
4136 | ||
4137 | -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv | |
4138 | ||
4139 | when Attribute_Wide_Wide_Width => | |
4140 | Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide); | |
4141 | ||
ee6ba406 | 4142 | ---------------- |
4143 | -- Wide_Width -- | |
4144 | ---------------- | |
4145 | ||
4146 | -- Wide_Width attribute is handled in separate unit Exp_Imgv | |
4147 | ||
4148 | when Attribute_Wide_Width => | |
7189d17f | 4149 | Exp_Imgv.Expand_Width_Attribute (N, Wide); |
ee6ba406 | 4150 | |
4151 | ----------- | |
4152 | -- Width -- | |
4153 | ----------- | |
4154 | ||
4155 | -- Width attribute is handled in separate unit Exp_Imgv | |
4156 | ||
4157 | when Attribute_Width => | |
7189d17f | 4158 | Exp_Imgv.Expand_Width_Attribute (N, Normal); |
ee6ba406 | 4159 | |
4160 | ----------- | |
4161 | -- Write -- | |
4162 | ----------- | |
4163 | ||
4164 | when Attribute_Write => Write : declare | |
4165 | P_Type : constant Entity_Id := Entity (Pref); | |
4166 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
4167 | Pname : Entity_Id; | |
4168 | Decl : Node_Id; | |
4169 | Prag : Node_Id; | |
4170 | Arg3 : Node_Id; | |
4171 | Wfunc : Node_Id; | |
4172 | ||
4173 | begin | |
4174 | -- If no underlying type, we have an error that will be diagnosed | |
4175 | -- elsewhere, so here we just completely ignore the expansion. | |
4176 | ||
4177 | if No (U_Type) then | |
4178 | return; | |
4179 | end if; | |
4180 | ||
4181 | -- The simple case, if there is a TSS for Write, just call it | |
4182 | ||
9dfe12ae | 4183 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write); |
ee6ba406 | 4184 | |
4185 | if Present (Pname) then | |
4186 | null; | |
4187 | ||
4188 | else | |
4189 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
4190 | ||
4191 | -- sourcetyp'Output (stream, Item) | |
4192 | ||
4193 | -- as | |
4194 | ||
4195 | -- strmtyp'Output (Stream, strmwrite (acttyp (Item))); | |
4196 | ||
4197 | -- where strmwrite is the given Write function that converts | |
4198 | -- an argument of type sourcetyp or a type acctyp, from which | |
4199 | -- it is derived to type strmtyp. The conversion to acttyp is | |
4200 | -- required for the derived case. | |
4201 | ||
5245b786 | 4202 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 4203 | |
4204 | if Present (Prag) then | |
4205 | Arg3 := | |
4206 | Next (Next (First (Pragma_Argument_Associations (Prag)))); | |
4207 | Wfunc := Entity (Expression (Arg3)); | |
4208 | ||
4209 | Rewrite (N, | |
4210 | Make_Attribute_Reference (Loc, | |
4211 | Prefix => New_Occurrence_Of (Etype (Wfunc), Loc), | |
4212 | Attribute_Name => Name_Output, | |
4213 | Expressions => New_List ( | |
4214 | Relocate_Node (First (Exprs)), | |
4215 | Make_Function_Call (Loc, | |
4216 | Name => New_Occurrence_Of (Wfunc, Loc), | |
4217 | Parameter_Associations => New_List ( | |
4218 | Convert_To (Etype (First_Formal (Wfunc)), | |
4219 | Relocate_Node (Next (First (Exprs))))))))); | |
4220 | ||
4221 | Analyze (N); | |
4222 | return; | |
4223 | ||
4224 | -- For elementary types, we call the W_xxx routine directly | |
4225 | ||
4226 | elsif Is_Elementary_Type (U_Type) then | |
4227 | Rewrite (N, Build_Elementary_Write_Call (N)); | |
4228 | Analyze (N); | |
4229 | return; | |
4230 | ||
4231 | -- Array type case | |
4232 | ||
4233 | elsif Is_Array_Type (U_Type) then | |
4234 | Build_Array_Write_Procedure (N, U_Type, Decl, Pname); | |
4235 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
4236 | ||
4237 | -- Tagged type case, use the primitive Write function. Note that | |
4238 | -- this will dispatch in the class-wide case which is what we want | |
4239 | ||
4240 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 4241 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Write); |
ee6ba406 | 4242 | |
4243 | -- All other record type cases, including protected records. | |
4244 | -- The latter only arise for expander generated code for | |
4245 | -- handling shared passive partition access. | |
4246 | ||
4247 | else | |
4248 | pragma Assert | |
4249 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
4250 | ||
00f91aef | 4251 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
4252 | -- the default implementation of the Write attribute of an | |
4253 | -- Unchecked_Union type. | |
4254 | ||
4255 | if Is_Unchecked_Union (Base_Type (U_Type)) then | |
4256 | Insert_Action (N, | |
4257 | Make_Raise_Program_Error (Loc, | |
4258 | Reason => PE_Unchecked_Union_Restriction)); | |
4259 | end if; | |
4260 | ||
ee6ba406 | 4261 | if Has_Discriminants (U_Type) |
4262 | and then Present | |
4263 | (Discriminant_Default_Value (First_Discriminant (U_Type))) | |
4264 | then | |
4265 | Build_Mutable_Record_Write_Procedure | |
4266 | (Loc, Base_Type (U_Type), Decl, Pname); | |
ee6ba406 | 4267 | else |
4268 | Build_Record_Write_Procedure | |
4269 | (Loc, Base_Type (U_Type), Decl, Pname); | |
4270 | end if; | |
4271 | ||
4272 | Insert_Action (N, Decl); | |
4273 | end if; | |
4274 | end if; | |
4275 | ||
4276 | -- If we fall through, Pname is the procedure to be called | |
4277 | ||
4278 | Rewrite_Stream_Proc_Call (Pname); | |
4279 | end Write; | |
4280 | ||
4281 | -- Component_Size is handled by Gigi, unless the component size is | |
4282 | -- known at compile time, which is always true in the packed array | |
4283 | -- case. It is important that the packed array case is handled in | |
4284 | -- the front end (see Eval_Attribute) since Gigi would otherwise | |
4285 | -- get confused by the equivalent packed array type. | |
4286 | ||
4287 | when Attribute_Component_Size => | |
4288 | null; | |
4289 | ||
4290 | -- The following attributes are handled by Gigi (except that static | |
4291 | -- cases have already been evaluated by the semantics, but in any | |
4292 | -- case Gigi should not count on that). | |
4293 | ||
4294 | -- In addition Gigi handles the non-floating-point cases of Pred | |
4295 | -- and Succ (including the fixed-point cases, which can just be | |
4296 | -- treated as integer increment/decrement operations) | |
4297 | ||
4298 | -- Gigi also handles the non-class-wide cases of Size | |
4299 | ||
4300 | when Attribute_Bit_Order | | |
4301 | Attribute_Code_Address | | |
4302 | Attribute_Definite | | |
4303 | Attribute_Max | | |
4304 | Attribute_Mechanism_Code | | |
4305 | Attribute_Min | | |
4306 | Attribute_Null_Parameter | | |
9dfe12ae | 4307 | Attribute_Passed_By_Reference | |
4308 | Attribute_Pool_Address => | |
ee6ba406 | 4309 | null; |
4310 | ||
4311 | -- The following attributes are also handled by Gigi, but return a | |
4312 | -- universal integer result, so may need a conversion for checking | |
4313 | -- that the result is in range. | |
4314 | ||
4315 | when Attribute_Aft | | |
ee6ba406 | 4316 | Attribute_Bit | |
4317 | Attribute_Max_Size_In_Storage_Elements | |
4318 | => | |
4319 | Apply_Universal_Integer_Attribute_Checks (N); | |
4320 | ||
4321 | -- The following attributes should not appear at this stage, since they | |
4322 | -- have already been handled by the analyzer (and properly rewritten | |
4323 | -- with corresponding values or entities to represent the right values) | |
4324 | ||
4325 | when Attribute_Abort_Signal | | |
4326 | Attribute_Address_Size | | |
4327 | Attribute_Base | | |
4328 | Attribute_Class | | |
4329 | Attribute_Default_Bit_Order | | |
4330 | Attribute_Delta | | |
4331 | Attribute_Denorm | | |
4332 | Attribute_Digits | | |
4333 | Attribute_Emax | | |
4334 | Attribute_Epsilon | | |
5c99c290 | 4335 | Attribute_Has_Access_Values | |
ee6ba406 | 4336 | Attribute_Has_Discriminants | |
4337 | Attribute_Large | | |
4338 | Attribute_Machine_Emax | | |
4339 | Attribute_Machine_Emin | | |
4340 | Attribute_Machine_Mantissa | | |
4341 | Attribute_Machine_Overflows | | |
4342 | Attribute_Machine_Radix | | |
4343 | Attribute_Machine_Rounds | | |
ee6ba406 | 4344 | Attribute_Maximum_Alignment | |
4345 | Attribute_Model_Emin | | |
4346 | Attribute_Model_Epsilon | | |
4347 | Attribute_Model_Mantissa | | |
4348 | Attribute_Model_Small | | |
4349 | Attribute_Modulus | | |
4350 | Attribute_Partition_ID | | |
4351 | Attribute_Range | | |
4352 | Attribute_Safe_Emax | | |
4353 | Attribute_Safe_First | | |
4354 | Attribute_Safe_Large | | |
4355 | Attribute_Safe_Last | | |
4356 | Attribute_Safe_Small | | |
4357 | Attribute_Scale | | |
4358 | Attribute_Signed_Zeros | | |
4359 | Attribute_Small | | |
4360 | Attribute_Storage_Unit | | |
9dfe12ae | 4361 | Attribute_Target_Name | |
ee6ba406 | 4362 | Attribute_Type_Class | |
9dfe12ae | 4363 | Attribute_Unconstrained_Array | |
ee6ba406 | 4364 | Attribute_Universal_Literal_String | |
4365 | Attribute_Wchar_T_Size | | |
4366 | Attribute_Word_Size => | |
4367 | ||
4368 | raise Program_Error; | |
4369 | ||
4370 | -- The Asm_Input and Asm_Output attributes are not expanded at this | |
4371 | -- stage, but will be eliminated in the expansion of the Asm call, | |
4372 | -- see Exp_Intr for details. So Gigi will never see these either. | |
4373 | ||
4374 | when Attribute_Asm_Input | | |
4375 | Attribute_Asm_Output => | |
4376 | ||
4377 | null; | |
4378 | ||
4379 | end case; | |
4380 | ||
9dfe12ae | 4381 | exception |
4382 | when RE_Not_Available => | |
4383 | return; | |
ee6ba406 | 4384 | end Expand_N_Attribute_Reference; |
4385 | ||
4386 | ---------------------- | |
4387 | -- Expand_Pred_Succ -- | |
4388 | ---------------------- | |
4389 | ||
4390 | -- For typ'Pred (exp), we generate the check | |
4391 | ||
4392 | -- [constraint_error when exp = typ'Base'First] | |
4393 | ||
4394 | -- Similarly, for typ'Succ (exp), we generate the check | |
4395 | ||
4396 | -- [constraint_error when exp = typ'Base'Last] | |
4397 | ||
4398 | -- These checks are not generated for modular types, since the proper | |
4399 | -- semantics for Succ and Pred on modular types is to wrap, not raise CE. | |
4400 | ||
4401 | procedure Expand_Pred_Succ (N : Node_Id) is | |
4402 | Loc : constant Source_Ptr := Sloc (N); | |
4403 | Cnam : Name_Id; | |
4404 | ||
4405 | begin | |
4406 | if Attribute_Name (N) = Name_Pred then | |
4407 | Cnam := Name_First; | |
4408 | else | |
4409 | Cnam := Name_Last; | |
4410 | end if; | |
4411 | ||
4412 | Insert_Action (N, | |
4413 | Make_Raise_Constraint_Error (Loc, | |
4414 | Condition => | |
4415 | Make_Op_Eq (Loc, | |
9dfe12ae | 4416 | Left_Opnd => |
4417 | Duplicate_Subexpr_Move_Checks (First (Expressions (N))), | |
ee6ba406 | 4418 | Right_Opnd => |
4419 | Make_Attribute_Reference (Loc, | |
4420 | Prefix => | |
4421 | New_Reference_To (Base_Type (Etype (Prefix (N))), Loc), | |
f15731c4 | 4422 | Attribute_Name => Cnam)), |
4423 | Reason => CE_Overflow_Check_Failed)); | |
ee6ba406 | 4424 | end Expand_Pred_Succ; |
4425 | ||
4426 | ------------------------ | |
4427 | -- Find_Inherited_TSS -- | |
4428 | ------------------------ | |
4429 | ||
4430 | function Find_Inherited_TSS | |
4431 | (Typ : Entity_Id; | |
9dfe12ae | 4432 | Nam : TSS_Name_Type) return Entity_Id |
ee6ba406 | 4433 | is |
9dfe12ae | 4434 | Btyp : Entity_Id := Typ; |
4435 | Proc : Entity_Id; | |
ee6ba406 | 4436 | |
4437 | begin | |
9dfe12ae | 4438 | loop |
4439 | Btyp := Base_Type (Btyp); | |
4440 | Proc := TSS (Btyp, Nam); | |
ee6ba406 | 4441 | |
9dfe12ae | 4442 | exit when Present (Proc) |
4443 | or else not Is_Derived_Type (Btyp); | |
ee6ba406 | 4444 | |
9dfe12ae | 4445 | -- If Typ is a derived type, it may inherit attributes from |
4446 | -- some ancestor. | |
ee6ba406 | 4447 | |
9dfe12ae | 4448 | Btyp := Etype (Btyp); |
4449 | end loop; | |
ee6ba406 | 4450 | |
9dfe12ae | 4451 | if No (Proc) then |
ee6ba406 | 4452 | |
9dfe12ae | 4453 | -- If nothing else, use the TSS of the root type |
4454 | ||
4455 | Proc := TSS (Base_Type (Underlying_Type (Typ)), Nam); | |
ee6ba406 | 4456 | end if; |
4457 | ||
9dfe12ae | 4458 | return Proc; |
ee6ba406 | 4459 | end Find_Inherited_TSS; |
4460 | ||
9dfe12ae | 4461 | ---------------------------- |
4462 | -- Find_Stream_Subprogram -- | |
4463 | ---------------------------- | |
4464 | ||
4465 | function Find_Stream_Subprogram | |
4466 | (Typ : Entity_Id; | |
4467 | Nam : TSS_Name_Type) return Entity_Id is | |
4468 | begin | |
4469 | if Is_Tagged_Type (Typ) | |
4470 | and then Is_Derived_Type (Typ) | |
4471 | then | |
4472 | return Find_Prim_Op (Typ, Nam); | |
4473 | else | |
4474 | return Find_Inherited_TSS (Typ, Nam); | |
4475 | end if; | |
4476 | end Find_Stream_Subprogram; | |
4477 | ||
ee6ba406 | 4478 | ----------------------- |
4479 | -- Get_Index_Subtype -- | |
4480 | ----------------------- | |
4481 | ||
4482 | function Get_Index_Subtype (N : Node_Id) return Node_Id is | |
4483 | P_Type : Entity_Id := Etype (Prefix (N)); | |
4484 | Indx : Node_Id; | |
4485 | J : Int; | |
4486 | ||
4487 | begin | |
4488 | if Is_Access_Type (P_Type) then | |
4489 | P_Type := Designated_Type (P_Type); | |
4490 | end if; | |
4491 | ||
4492 | if No (Expressions (N)) then | |
4493 | J := 1; | |
4494 | else | |
4495 | J := UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4496 | end if; | |
4497 | ||
4498 | Indx := First_Index (P_Type); | |
4499 | while J > 1 loop | |
4500 | Next_Index (Indx); | |
4501 | J := J - 1; | |
4502 | end loop; | |
4503 | ||
4504 | return Etype (Indx); | |
4505 | end Get_Index_Subtype; | |
4506 | ||
5245b786 | 4507 | ------------------------------- |
4508 | -- Get_Stream_Convert_Pragma -- | |
4509 | ------------------------------- | |
4510 | ||
4511 | function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is | |
4512 | Typ : Entity_Id; | |
4513 | N : Node_Id; | |
4514 | ||
4515 | begin | |
4516 | -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity | |
4517 | -- that a stream convert pragma for a tagged type is not inherited from | |
4518 | -- its parent. Probably what is wrong here is that it is basically | |
4519 | -- incorrect to consider a stream convert pragma to be a representation | |
4520 | -- pragma at all ??? | |
4521 | ||
4522 | N := First_Rep_Item (Implementation_Base_Type (T)); | |
4523 | while Present (N) loop | |
4524 | if Nkind (N) = N_Pragma and then Chars (N) = Name_Stream_Convert then | |
4525 | ||
4526 | -- For tagged types this pragma is not inherited, so we | |
4527 | -- must verify that it is defined for the given type and | |
4528 | -- not an ancestor. | |
4529 | ||
4530 | Typ := | |
4531 | Entity (Expression (First (Pragma_Argument_Associations (N)))); | |
4532 | ||
4533 | if not Is_Tagged_Type (T) | |
4534 | or else T = Typ | |
4535 | or else (Is_Private_Type (Typ) and then T = Full_View (Typ)) | |
4536 | then | |
4537 | return N; | |
4538 | end if; | |
4539 | end if; | |
4540 | ||
4541 | Next_Rep_Item (N); | |
4542 | end loop; | |
4543 | ||
4544 | return Empty; | |
4545 | end Get_Stream_Convert_Pragma; | |
4546 | ||
ee6ba406 | 4547 | --------------------------------- |
4548 | -- Is_Constrained_Packed_Array -- | |
4549 | --------------------------------- | |
4550 | ||
4551 | function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is | |
4552 | Arr : Entity_Id := Typ; | |
4553 | ||
4554 | begin | |
4555 | if Is_Access_Type (Arr) then | |
4556 | Arr := Designated_Type (Arr); | |
4557 | end if; | |
4558 | ||
4559 | return Is_Array_Type (Arr) | |
4560 | and then Is_Constrained (Arr) | |
4561 | and then Present (Packed_Array_Type (Arr)); | |
4562 | end Is_Constrained_Packed_Array; | |
4563 | ||
4564 | end Exp_Attr; |