]>
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 | -- -- | |
36dccb2b | 9 | -- Copyright (C) 1992-2010, 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- -- | |
6bc9506f | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
ee6ba406 | 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 -- | |
3430bf31 | 16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
17 | -- for more details. You should have received a copy of the GNU General -- | |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- | |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
ee6ba406 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
ee6ba406 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
28 | with Einfo; use Einfo; | |
00f91aef | 29 | with Elists; use Elists; |
99f2248e | 30 | with Exp_Atag; use Exp_Atag; |
ee6ba406 | 31 | with Exp_Ch2; use Exp_Ch2; |
d55c93e0 | 32 | with Exp_Ch3; use Exp_Ch3; |
33 | with Exp_Ch6; use Exp_Ch6; | |
ee6ba406 | 34 | with Exp_Ch9; use Exp_Ch9; |
5690e662 | 35 | with Exp_Dist; use Exp_Dist; |
ee6ba406 | 36 | with Exp_Imgv; use Exp_Imgv; |
37 | with Exp_Pakd; use Exp_Pakd; | |
38 | with Exp_Strm; use Exp_Strm; | |
39 | with Exp_Tss; use Exp_Tss; | |
40 | with Exp_Util; use Exp_Util; | |
6e62b6c3 | 41 | with Exp_VFpt; use Exp_VFpt; |
d55c93e0 | 42 | with Fname; use Fname; |
db14252c | 43 | with Freeze; use Freeze; |
ee6ba406 | 44 | with Gnatvsn; use Gnatvsn; |
db14252c | 45 | with Itypes; use Itypes; |
ee6ba406 | 46 | with Lib; use Lib; |
47 | with Namet; use Namet; | |
48 | with Nmake; use Nmake; | |
49 | with Nlists; use Nlists; | |
50 | with Opt; use Opt; | |
51 | with Restrict; use Restrict; | |
1e16c51c | 52 | with Rident; use Rident; |
ee6ba406 | 53 | with Rtsfind; use Rtsfind; |
54 | with Sem; use Sem; | |
d60c9ff7 | 55 | with Sem_Aux; use Sem_Aux; |
4c06b9d2 | 56 | with Sem_Ch6; use Sem_Ch6; |
ee6ba406 | 57 | with Sem_Ch7; use Sem_Ch7; |
58 | with Sem_Ch8; use Sem_Ch8; | |
ee6ba406 | 59 | with Sem_Eval; use Sem_Eval; |
60 | with Sem_Res; use Sem_Res; | |
61 | with Sem_Util; use Sem_Util; | |
62 | with Sinfo; use Sinfo; | |
63 | with Snames; use Snames; | |
64 | with Stand; use Stand; | |
65 | with Stringt; use Stringt; | |
83aa52b6 | 66 | with Targparm; use Targparm; |
ee6ba406 | 67 | with Tbuild; use Tbuild; |
68 | with Ttypes; use Ttypes; | |
69 | with Uintp; use Uintp; | |
70 | with Uname; use Uname; | |
71 | with Validsw; use Validsw; | |
72 | ||
73 | package body Exp_Attr is | |
74 | ||
75 | ----------------------- | |
76 | -- Local Subprograms -- | |
77 | ----------------------- | |
78 | ||
79 | procedure Compile_Stream_Body_In_Scope | |
80 | (N : Node_Id; | |
81 | Decl : Node_Id; | |
82 | Arr : Entity_Id; | |
83 | Check : Boolean); | |
84 | -- The body for a stream subprogram may be generated outside of the scope | |
85 | -- of the type. If the type is fully private, it may depend on the full | |
1d00a8ce | 86 | -- view of other types (e.g. indexes) that are currently private as well. |
ee6ba406 | 87 | -- We install the declarations of the package in which the type is declared |
88 | -- before compiling the body in what is its proper environment. The Check | |
89 | -- parameter indicates if checks are to be suppressed for the stream body. | |
90 | -- We suppress checks for array/record reads, since the rule is that these | |
91 | -- are like assignments, out of range values due to uninitialized storage, | |
92 | -- or other invalid values do NOT cause a Constraint_Error to be raised. | |
93 | ||
7f8eb6ed | 94 | procedure Expand_Access_To_Protected_Op |
95 | (N : Node_Id; | |
96 | Pref : Node_Id; | |
97 | Typ : Entity_Id); | |
7f8eb6ed | 98 | -- An attribute reference to a protected subprogram is transformed into |
99 | -- a pair of pointers: one to the object, and one to the operations. | |
100 | -- This expansion is performed for 'Access and for 'Unrestricted_Access. | |
101 | ||
ee6ba406 | 102 | procedure Expand_Fpt_Attribute |
9dfe12ae | 103 | (N : Node_Id; |
1550b445 | 104 | Pkg : RE_Id; |
9dfe12ae | 105 | Nam : Name_Id; |
ee6ba406 | 106 | Args : List_Id); |
107 | -- This procedure expands a call to a floating-point attribute function. | |
108 | -- N is the attribute reference node, and Args is a list of arguments to | |
1550b445 | 109 | -- be passed to the function call. Pkg identifies the package containing |
110 | -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args | |
111 | -- have already been converted to the floating-point type for which Pkg was | |
112 | -- instantiated. The Nam argument is the relevant attribute processing | |
113 | -- routine to be called. This is the same as the attribute name, except in | |
114 | -- the Unaligned_Valid case. | |
ee6ba406 | 115 | |
116 | procedure Expand_Fpt_Attribute_R (N : Node_Id); | |
117 | -- This procedure expands a call to a floating-point attribute function | |
9dfe12ae | 118 | -- that takes a single floating-point argument. The function to be called |
119 | -- is always the same as the attribute name. | |
ee6ba406 | 120 | |
121 | procedure Expand_Fpt_Attribute_RI (N : Node_Id); | |
122 | -- This procedure expands a call to a floating-point attribute function | |
9dfe12ae | 123 | -- that takes one floating-point argument and one integer argument. The |
124 | -- function to be called is always the same as the attribute name. | |
ee6ba406 | 125 | |
126 | procedure Expand_Fpt_Attribute_RR (N : Node_Id); | |
127 | -- This procedure expands a call to a floating-point attribute function | |
9dfe12ae | 128 | -- that takes two floating-point arguments. The function to be called |
129 | -- is always the same as the attribute name. | |
ee6ba406 | 130 | |
131 | procedure Expand_Pred_Succ (N : Node_Id); | |
132 | -- Handles expansion of Pred or Succ attributes for case of non-real | |
133 | -- operand with overflow checking required. | |
134 | ||
135 | function Get_Index_Subtype (N : Node_Id) return Entity_Id; | |
f0bf2ff3 | 136 | -- Used for Last, Last, and Length, when the prefix is an array type. |
ee6ba406 | 137 | -- Obtains the corresponding index subtype. |
138 | ||
1550b445 | 139 | procedure Find_Fat_Info |
140 | (T : Entity_Id; | |
141 | Fat_Type : out Entity_Id; | |
142 | Fat_Pkg : out RE_Id); | |
143 | -- Given a floating-point type T, identifies the package containing the | |
144 | -- attributes for this type (returned in Fat_Pkg), and the corresponding | |
145 | -- type for which this package was instantiated from Fat_Gen. Error if T | |
146 | -- is not a floating-point type. | |
147 | ||
9dfe12ae | 148 | function Find_Stream_Subprogram |
149 | (Typ : Entity_Id; | |
150 | Nam : TSS_Name_Type) return Entity_Id; | |
151 | -- Returns the stream-oriented subprogram attribute for Typ. For tagged | |
152 | -- types, the corresponding primitive operation is looked up, else the | |
153 | -- appropriate TSS from the type itself, or from its closest ancestor | |
154 | -- defining it, is returned. In both cases, inheritance of representation | |
155 | -- aspects is thus taken into account. | |
ee6ba406 | 156 | |
7af38999 | 157 | function Full_Base (T : Entity_Id) return Entity_Id; |
158 | -- The stream functions need to examine the underlying representation of | |
159 | -- composite types. In some cases T may be non-private but its base type | |
160 | -- is, in which case the function returns the corresponding full view. | |
161 | ||
5245b786 | 162 | function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id; |
163 | -- Given a type, find a corresponding stream convert pragma that applies to | |
164 | -- the implementation base type of this type (Typ). If found, return the | |
165 | -- pragma node, otherwise return Empty if no pragma is found. | |
166 | ||
ee6ba406 | 167 | function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean; |
168 | -- Utility for array attributes, returns true on packed constrained | |
169 | -- arrays, and on access to same. | |
170 | ||
99f2248e | 171 | function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean; |
172 | -- Returns true iff the given node refers to an attribute call that | |
173 | -- can be expanded directly by the back end and does not need front end | |
174 | -- expansion. Typically used for rounding and truncation attributes that | |
175 | -- appear directly inside a conversion to integer. | |
176 | ||
ee6ba406 | 177 | ---------------------------------- |
178 | -- Compile_Stream_Body_In_Scope -- | |
179 | ---------------------------------- | |
180 | ||
181 | procedure Compile_Stream_Body_In_Scope | |
182 | (N : Node_Id; | |
183 | Decl : Node_Id; | |
184 | Arr : Entity_Id; | |
185 | Check : Boolean) | |
186 | is | |
187 | Installed : Boolean := False; | |
188 | Scop : constant Entity_Id := Scope (Arr); | |
189 | Curr : constant Entity_Id := Current_Scope; | |
190 | ||
191 | begin | |
192 | if Is_Hidden (Arr) | |
193 | and then not In_Open_Scopes (Scop) | |
194 | and then Ekind (Scop) = E_Package | |
195 | then | |
83aa52b6 | 196 | Push_Scope (Scop); |
ee6ba406 | 197 | Install_Visible_Declarations (Scop); |
198 | Install_Private_Declarations (Scop); | |
199 | Installed := True; | |
200 | ||
201 | -- The entities in the package are now visible, but the generated | |
202 | -- stream entity must appear in the current scope (usually an | |
203 | -- enclosing stream function) so that itypes all have their proper | |
204 | -- scopes. | |
205 | ||
83aa52b6 | 206 | Push_Scope (Curr); |
ee6ba406 | 207 | end if; |
208 | ||
209 | if Check then | |
210 | Insert_Action (N, Decl); | |
211 | else | |
1550b445 | 212 | Insert_Action (N, Decl, Suppress => All_Checks); |
ee6ba406 | 213 | end if; |
214 | ||
215 | if Installed then | |
216 | ||
217 | -- Remove extra copy of current scope, and package itself | |
218 | ||
219 | Pop_Scope; | |
220 | End_Package_Scope (Scop); | |
221 | end if; | |
222 | end Compile_Stream_Body_In_Scope; | |
223 | ||
7f8eb6ed | 224 | ----------------------------------- |
225 | -- Expand_Access_To_Protected_Op -- | |
226 | ----------------------------------- | |
227 | ||
228 | procedure Expand_Access_To_Protected_Op | |
229 | (N : Node_Id; | |
230 | Pref : Node_Id; | |
231 | Typ : Entity_Id) | |
232 | is | |
233 | -- The value of the attribute_reference is a record containing two | |
234 | -- fields: an access to the protected object, and an access to the | |
235 | -- subprogram itself. The prefix is a selected component. | |
236 | ||
237 | Loc : constant Source_Ptr := Sloc (N); | |
238 | Agg : Node_Id; | |
239 | Btyp : constant Entity_Id := Base_Type (Typ); | |
240 | Sub : Entity_Id; | |
a9b0586f | 241 | Sub_Ref : Node_Id; |
7f8eb6ed | 242 | E_T : constant Entity_Id := Equivalent_Type (Btyp); |
243 | Acc : constant Entity_Id := | |
244 | Etype (Next_Component (First_Component (E_T))); | |
245 | Obj_Ref : Node_Id; | |
246 | Curr : Entity_Id; | |
247 | ||
248 | function May_Be_External_Call return Boolean; | |
249 | -- If the 'Access is to a local operation, but appears in a context | |
250 | -- where it may lead to a call from outside the object, we must treat | |
251 | -- this as an external call. Clearly we cannot tell without full | |
252 | -- flow analysis, and a subsequent call that uses this 'Access may | |
253 | -- lead to a bounded error (trying to seize locks twice, e.g.). For | |
254 | -- now we treat 'Access as a potential external call if it is an actual | |
255 | -- in a call to an outside subprogram. | |
256 | ||
257 | -------------------------- | |
258 | -- May_Be_External_Call -- | |
259 | -------------------------- | |
260 | ||
261 | function May_Be_External_Call return Boolean is | |
262 | Subp : Entity_Id; | |
d55c93e0 | 263 | Par : Node_Id := Parent (N); |
264 | ||
7f8eb6ed | 265 | begin |
d55c93e0 | 266 | -- Account for the case where the Access attribute is part of a |
267 | -- named parameter association. | |
268 | ||
269 | if Nkind (Par) = N_Parameter_Association then | |
270 | Par := Parent (Par); | |
271 | end if; | |
272 | ||
273 | if Nkind_In (Par, N_Procedure_Call_Statement, N_Function_Call) | |
274 | and then Is_Entity_Name (Name (Par)) | |
7f8eb6ed | 275 | then |
d55c93e0 | 276 | Subp := Entity (Name (Par)); |
7f8eb6ed | 277 | return not In_Open_Scopes (Scope (Subp)); |
278 | else | |
279 | return False; | |
280 | end if; | |
281 | end May_Be_External_Call; | |
282 | ||
283 | -- Start of processing for Expand_Access_To_Protected_Op | |
284 | ||
285 | begin | |
d73d4db0 | 286 | -- Within the body of the protected type, the prefix designates a local |
287 | -- operation, and the object is the first parameter of the corresponding | |
288 | -- protected body of the current enclosing operation. | |
7f8eb6ed | 289 | |
290 | if Is_Entity_Name (Pref) then | |
7f8eb6ed | 291 | if May_Be_External_Call then |
292 | Sub := | |
d73d4db0 | 293 | New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc); |
7f8eb6ed | 294 | else |
295 | Sub := | |
296 | New_Occurrence_Of | |
297 | (Protected_Body_Subprogram (Entity (Pref)), Loc); | |
298 | end if; | |
299 | ||
d55c93e0 | 300 | -- Don't traverse the scopes when the attribute occurs within an init |
301 | -- proc, because we directly use the _init formal of the init proc in | |
302 | -- that case. | |
303 | ||
7f8eb6ed | 304 | Curr := Current_Scope; |
d55c93e0 | 305 | if not Is_Init_Proc (Curr) then |
306 | pragma Assert (In_Open_Scopes (Scope (Entity (Pref)))); | |
307 | ||
308 | while Scope (Curr) /= Scope (Entity (Pref)) loop | |
309 | Curr := Scope (Curr); | |
310 | end loop; | |
311 | end if; | |
7f8eb6ed | 312 | |
313 | -- In case of protected entries the first formal of its Protected_ | |
314 | -- Body_Subprogram is the address of the object. | |
315 | ||
316 | if Ekind (Curr) = E_Entry then | |
317 | Obj_Ref := | |
318 | New_Occurrence_Of | |
319 | (First_Formal | |
320 | (Protected_Body_Subprogram (Curr)), Loc); | |
321 | ||
d55c93e0 | 322 | -- If the current scope is an init proc, then use the address of the |
323 | -- _init formal as the object reference. | |
324 | ||
325 | elsif Is_Init_Proc (Curr) then | |
326 | Obj_Ref := | |
327 | Make_Attribute_Reference (Loc, | |
328 | Prefix => New_Occurrence_Of (First_Formal (Curr), Loc), | |
329 | Attribute_Name => Name_Address); | |
330 | ||
7f8eb6ed | 331 | -- In case of protected subprograms the first formal of its |
332 | -- Protected_Body_Subprogram is the object and we get its address. | |
333 | ||
334 | else | |
335 | Obj_Ref := | |
336 | Make_Attribute_Reference (Loc, | |
337 | Prefix => | |
338 | New_Occurrence_Of | |
339 | (First_Formal | |
340 | (Protected_Body_Subprogram (Curr)), Loc), | |
341 | Attribute_Name => Name_Address); | |
342 | end if; | |
343 | ||
344 | -- Case where the prefix is not an entity name. Find the | |
345 | -- version of the protected operation to be called from | |
346 | -- outside the protected object. | |
347 | ||
348 | else | |
349 | Sub := | |
350 | New_Occurrence_Of | |
351 | (External_Subprogram | |
352 | (Entity (Selector_Name (Pref))), Loc); | |
353 | ||
354 | Obj_Ref := | |
355 | Make_Attribute_Reference (Loc, | |
356 | Prefix => Relocate_Node (Prefix (Pref)), | |
357 | Attribute_Name => Name_Address); | |
358 | end if; | |
359 | ||
a9b0586f | 360 | Sub_Ref := |
361 | Make_Attribute_Reference (Loc, | |
82ae9906 | 362 | Prefix => Sub, |
a9b0586f | 363 | Attribute_Name => Name_Access); |
364 | ||
365 | -- We set the type of the access reference to the already generated | |
366 | -- access_to_subprogram type, and declare the reference analyzed, to | |
367 | -- prevent further expansion when the enclosing aggregate is analyzed. | |
368 | ||
369 | Set_Etype (Sub_Ref, Acc); | |
370 | Set_Analyzed (Sub_Ref); | |
371 | ||
7f8eb6ed | 372 | Agg := |
373 | Make_Aggregate (Loc, | |
82ae9906 | 374 | Expressions => New_List (Obj_Ref, Sub_Ref)); |
7f8eb6ed | 375 | |
6cba2d6a | 376 | -- Sub_Ref has been marked as analyzed, but we still need to make sure |
377 | -- Sub is correctly frozen. | |
378 | ||
d73d4db0 | 379 | Freeze_Before (N, Entity (Sub)); |
6cba2d6a | 380 | |
7f8eb6ed | 381 | Rewrite (N, Agg); |
7f8eb6ed | 382 | Analyze_And_Resolve (N, E_T); |
383 | ||
82ae9906 | 384 | -- For subsequent analysis, the node must retain its type. The backend |
385 | -- will replace it with the equivalent type where needed. | |
7f8eb6ed | 386 | |
387 | Set_Etype (N, Typ); | |
388 | end Expand_Access_To_Protected_Op; | |
389 | ||
ee6ba406 | 390 | -------------------------- |
391 | -- Expand_Fpt_Attribute -- | |
392 | -------------------------- | |
393 | ||
394 | procedure Expand_Fpt_Attribute | |
395 | (N : Node_Id; | |
1550b445 | 396 | Pkg : RE_Id; |
9dfe12ae | 397 | Nam : Name_Id; |
ee6ba406 | 398 | Args : List_Id) |
399 | is | |
400 | Loc : constant Source_Ptr := Sloc (N); | |
401 | Typ : constant Entity_Id := Etype (N); | |
ee6ba406 | 402 | Fnm : Node_Id; |
403 | ||
404 | begin | |
1550b445 | 405 | -- The function name is the selected component Attr_xxx.yyy where |
406 | -- Attr_xxx is the package name, and yyy is the argument Nam. | |
ee6ba406 | 407 | |
408 | -- Note: it would be more usual to have separate RE entries for each | |
409 | -- of the entities in the Fat packages, but first they have identical | |
410 | -- names (so we would have to have lots of renaming declarations to | |
411 | -- meet the normal RE rule of separate names for all runtime entities), | |
412 | -- and second there would be an awful lot of them! | |
413 | ||
ee6ba406 | 414 | Fnm := |
415 | Make_Selected_Component (Loc, | |
416 | Prefix => New_Reference_To (RTE (Pkg), Loc), | |
9dfe12ae | 417 | Selector_Name => Make_Identifier (Loc, Nam)); |
ee6ba406 | 418 | |
419 | -- The generated call is given the provided set of parameters, and then | |
420 | -- wrapped in a conversion which converts the result to the target type | |
5245b786 | 421 | -- We use the base type as the target because a range check may be |
422 | -- required. | |
ee6ba406 | 423 | |
424 | Rewrite (N, | |
5245b786 | 425 | Unchecked_Convert_To (Base_Type (Etype (N)), |
ee6ba406 | 426 | Make_Function_Call (Loc, |
1550b445 | 427 | Name => Fnm, |
ee6ba406 | 428 | Parameter_Associations => Args))); |
429 | ||
430 | Analyze_And_Resolve (N, Typ); | |
ee6ba406 | 431 | end Expand_Fpt_Attribute; |
432 | ||
433 | ---------------------------- | |
434 | -- Expand_Fpt_Attribute_R -- | |
435 | ---------------------------- | |
436 | ||
437 | -- The single argument is converted to its root type to call the | |
438 | -- appropriate runtime function, with the actual call being built | |
439 | -- by Expand_Fpt_Attribute | |
440 | ||
441 | procedure Expand_Fpt_Attribute_R (N : Node_Id) is | |
442 | E1 : constant Node_Id := First (Expressions (N)); | |
1550b445 | 443 | Ftp : Entity_Id; |
444 | Pkg : RE_Id; | |
ee6ba406 | 445 | begin |
1550b445 | 446 | Find_Fat_Info (Etype (E1), Ftp, Pkg); |
9dfe12ae | 447 | Expand_Fpt_Attribute |
1550b445 | 448 | (N, Pkg, Attribute_Name (N), |
449 | New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1)))); | |
ee6ba406 | 450 | end Expand_Fpt_Attribute_R; |
451 | ||
452 | ----------------------------- | |
453 | -- Expand_Fpt_Attribute_RI -- | |
454 | ----------------------------- | |
455 | ||
456 | -- The first argument is converted to its root type and the second | |
457 | -- argument is converted to standard long long integer to call the | |
458 | -- appropriate runtime function, with the actual call being built | |
459 | -- by Expand_Fpt_Attribute | |
460 | ||
461 | procedure Expand_Fpt_Attribute_RI (N : Node_Id) is | |
462 | E1 : constant Node_Id := First (Expressions (N)); | |
1550b445 | 463 | Ftp : Entity_Id; |
464 | Pkg : RE_Id; | |
ee6ba406 | 465 | E2 : constant Node_Id := Next (E1); |
ee6ba406 | 466 | begin |
1550b445 | 467 | Find_Fat_Info (Etype (E1), Ftp, Pkg); |
9dfe12ae | 468 | Expand_Fpt_Attribute |
1550b445 | 469 | (N, Pkg, Attribute_Name (N), |
9dfe12ae | 470 | New_List ( |
1550b445 | 471 | Unchecked_Convert_To (Ftp, Relocate_Node (E1)), |
9dfe12ae | 472 | Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2)))); |
ee6ba406 | 473 | end Expand_Fpt_Attribute_RI; |
474 | ||
475 | ----------------------------- | |
476 | -- Expand_Fpt_Attribute_RR -- | |
477 | ----------------------------- | |
478 | ||
99f2248e | 479 | -- The two arguments are converted to their root types to call the |
ee6ba406 | 480 | -- appropriate runtime function, with the actual call being built |
481 | -- by Expand_Fpt_Attribute | |
482 | ||
483 | procedure Expand_Fpt_Attribute_RR (N : Node_Id) is | |
484 | E1 : constant Node_Id := First (Expressions (N)); | |
1550b445 | 485 | Ftp : Entity_Id; |
486 | Pkg : RE_Id; | |
ee6ba406 | 487 | E2 : constant Node_Id := Next (E1); |
ee6ba406 | 488 | begin |
1550b445 | 489 | Find_Fat_Info (Etype (E1), Ftp, Pkg); |
9dfe12ae | 490 | Expand_Fpt_Attribute |
1550b445 | 491 | (N, Pkg, Attribute_Name (N), |
9dfe12ae | 492 | New_List ( |
1550b445 | 493 | Unchecked_Convert_To (Ftp, Relocate_Node (E1)), |
494 | Unchecked_Convert_To (Ftp, Relocate_Node (E2)))); | |
ee6ba406 | 495 | end Expand_Fpt_Attribute_RR; |
496 | ||
497 | ---------------------------------- | |
498 | -- Expand_N_Attribute_Reference -- | |
499 | ---------------------------------- | |
500 | ||
501 | procedure Expand_N_Attribute_Reference (N : Node_Id) is | |
502 | Loc : constant Source_Ptr := Sloc (N); | |
503 | Typ : constant Entity_Id := Etype (N); | |
504 | Btyp : constant Entity_Id := Base_Type (Typ); | |
505 | Pref : constant Node_Id := Prefix (N); | |
d55c93e0 | 506 | Ptyp : constant Entity_Id := Etype (Pref); |
ee6ba406 | 507 | Exprs : constant List_Id := Expressions (N); |
508 | Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N)); | |
509 | ||
510 | procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id); | |
511 | -- Rewrites a stream attribute for Read, Write or Output with the | |
512 | -- procedure call. Pname is the entity for the procedure to call. | |
513 | ||
514 | ------------------------------ | |
515 | -- Rewrite_Stream_Proc_Call -- | |
516 | ------------------------------ | |
517 | ||
518 | procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is | |
519 | Item : constant Node_Id := Next (First (Exprs)); | |
9dfe12ae | 520 | Formal : constant Entity_Id := Next_Formal (First_Formal (Pname)); |
521 | Formal_Typ : constant Entity_Id := Etype (Formal); | |
522 | Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter); | |
ee6ba406 | 523 | |
524 | begin | |
9dfe12ae | 525 | -- The expansion depends on Item, the second actual, which is |
526 | -- the object being streamed in or out. | |
527 | ||
528 | -- If the item is a component of a packed array type, and | |
529 | -- a conversion is needed on exit, we introduce a temporary to | |
530 | -- hold the value, because otherwise the packed reference will | |
531 | -- not be properly expanded. | |
532 | ||
533 | if Nkind (Item) = N_Indexed_Component | |
534 | and then Is_Packed (Base_Type (Etype (Prefix (Item)))) | |
535 | and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ) | |
536 | and then Is_Written | |
537 | then | |
538 | declare | |
46eb6933 | 539 | Temp : constant Entity_Id := Make_Temporary (Loc, 'V'); |
9dfe12ae | 540 | Decl : Node_Id; |
541 | Assn : Node_Id; | |
542 | ||
543 | begin | |
544 | Decl := | |
545 | Make_Object_Declaration (Loc, | |
546 | Defining_Identifier => Temp, | |
547 | Object_Definition => | |
548 | New_Occurrence_Of (Formal_Typ, Loc)); | |
549 | Set_Etype (Temp, Formal_Typ); | |
550 | ||
551 | Assn := | |
552 | Make_Assignment_Statement (Loc, | |
553 | Name => New_Copy_Tree (Item), | |
554 | Expression => | |
555 | Unchecked_Convert_To | |
556 | (Etype (Item), New_Occurrence_Of (Temp, Loc))); | |
557 | ||
558 | Rewrite (Item, New_Occurrence_Of (Temp, Loc)); | |
559 | Insert_Actions (N, | |
560 | New_List ( | |
561 | Decl, | |
562 | Make_Procedure_Call_Statement (Loc, | |
563 | Name => New_Occurrence_Of (Pname, Loc), | |
564 | Parameter_Associations => Exprs), | |
565 | Assn)); | |
566 | ||
567 | Rewrite (N, Make_Null_Statement (Loc)); | |
568 | return; | |
569 | end; | |
570 | end if; | |
ee6ba406 | 571 | |
572 | -- For the class-wide dispatching cases, and for cases in which | |
573 | -- the base type of the second argument matches the base type of | |
9dfe12ae | 574 | -- the corresponding formal parameter (that is to say the stream |
575 | -- operation is not inherited), we are all set, and can use the | |
576 | -- argument unchanged. | |
ee6ba406 | 577 | |
578 | -- For all other cases we do an unchecked conversion of the second | |
579 | -- parameter to the type of the formal of the procedure we are | |
580 | -- calling. This deals with the private type cases, and with going | |
581 | -- to the root type as required in elementary type case. | |
582 | ||
583 | if not Is_Class_Wide_Type (Entity (Pref)) | |
9dfe12ae | 584 | and then not Is_Class_Wide_Type (Etype (Item)) |
ee6ba406 | 585 | and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ) |
586 | then | |
587 | Rewrite (Item, | |
588 | Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item))); | |
589 | ||
590 | -- For untagged derived types set Assignment_OK, to prevent | |
591 | -- copies from being created when the unchecked conversion | |
592 | -- is expanded (which would happen in Remove_Side_Effects | |
593 | -- if Expand_N_Unchecked_Conversion were allowed to call | |
594 | -- Force_Evaluation). The copy could violate Ada semantics | |
595 | -- in cases such as an actual that is an out parameter. | |
596 | -- Note that this approach is also used in exp_ch7 for calls | |
597 | -- to controlled type operations to prevent problems with | |
598 | -- actuals wrapped in unchecked conversions. | |
599 | ||
600 | if Is_Untagged_Derivation (Etype (Expression (Item))) then | |
601 | Set_Assignment_OK (Item); | |
602 | end if; | |
603 | end if; | |
604 | ||
ad7d90bc | 605 | -- The stream operation to call maybe a renaming created by |
606 | -- an attribute definition clause, and may not be frozen yet. | |
607 | -- Ensure that it has the necessary extra formals. | |
608 | ||
609 | if not Is_Frozen (Pname) then | |
610 | Create_Extra_Formals (Pname); | |
611 | end if; | |
612 | ||
ee6ba406 | 613 | -- And now rewrite the call |
614 | ||
615 | Rewrite (N, | |
616 | Make_Procedure_Call_Statement (Loc, | |
617 | Name => New_Occurrence_Of (Pname, Loc), | |
618 | Parameter_Associations => Exprs)); | |
619 | ||
620 | Analyze (N); | |
621 | end Rewrite_Stream_Proc_Call; | |
622 | ||
623 | -- Start of processing for Expand_N_Attribute_Reference | |
624 | ||
625 | begin | |
7189d17f | 626 | -- Do required validity checking, if enabled. Do not apply check to |
627 | -- output parameters of an Asm instruction, since the value of this | |
372ac9de | 628 | -- is not set till after the attribute has been elaborated, and do |
629 | -- not apply the check to the arguments of a 'Read or 'Input attribute | |
630 | -- reference since the scalar argument is an OUT scalar. | |
ee6ba406 | 631 | |
7189d17f | 632 | if Validity_Checks_On and then Validity_Check_Operands |
633 | and then Id /= Attribute_Asm_Output | |
372ac9de | 634 | and then Id /= Attribute_Read |
635 | and then Id /= Attribute_Input | |
7189d17f | 636 | then |
ee6ba406 | 637 | declare |
638 | Expr : Node_Id; | |
ee6ba406 | 639 | begin |
640 | Expr := First (Expressions (N)); | |
641 | while Present (Expr) loop | |
642 | Ensure_Valid (Expr); | |
643 | Next (Expr); | |
644 | end loop; | |
645 | end; | |
646 | end if; | |
647 | ||
d55c93e0 | 648 | -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in- |
649 | -- place function, then a temporary return object needs to be created | |
650 | -- and access to it must be passed to the function. Currently we limit | |
651 | -- such functions to those with inherently limited result subtypes, but | |
652 | -- eventually we plan to expand the functions that are treated as | |
653 | -- build-in-place to include other composite result types. | |
654 | ||
de54c5ab | 655 | if Ada_Version >= Ada_2005 |
d55c93e0 | 656 | and then Is_Build_In_Place_Function_Call (Pref) |
657 | then | |
658 | Make_Build_In_Place_Call_In_Anonymous_Context (Pref); | |
659 | end if; | |
660 | ||
36e5d81f | 661 | -- If prefix is a protected type name, this is a reference to the |
662 | -- current instance of the type. For a component definition, nothing | |
663 | -- to do (expansion will occur in the init proc). In other contexts, | |
664 | -- rewrite into reference to current instance. | |
665 | ||
666 | if Is_Protected_Self_Reference (Pref) | |
667 | and then not | |
9e434a36 | 668 | (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint, |
669 | N_Discriminant_Association) | |
670 | and then Nkind (Parent (Parent (Parent (Parent (N))))) = | |
671 | N_Component_Definition) | |
36e5d81f | 672 | then |
98d58e33 | 673 | Rewrite (Pref, Concurrent_Ref (Pref)); |
674 | Analyze (Pref); | |
675 | end if; | |
676 | ||
ee6ba406 | 677 | -- Remaining processing depends on specific attribute |
678 | ||
679 | case Id is | |
680 | ||
681 | ------------ | |
682 | -- Access -- | |
683 | ------------ | |
684 | ||
f947f061 | 685 | when Attribute_Access | |
686 | Attribute_Unchecked_Access | | |
687 | Attribute_Unrestricted_Access => | |
ee6ba406 | 688 | |
5e82d8fe | 689 | Access_Cases : declare |
5e82d8fe | 690 | Ref_Object : constant Node_Id := Get_Referenced_Object (Pref); |
d251c56f | 691 | Btyp_DDT : Entity_Id; |
ee6ba406 | 692 | |
4094dca5 | 693 | function Enclosing_Object (N : Node_Id) return Node_Id; |
694 | -- If N denotes a compound name (selected component, indexed | |
9e434a36 | 695 | -- component, or slice), returns the name of the outermost such |
696 | -- enclosing object. Otherwise returns N. If the object is a | |
697 | -- renaming, then the renamed object is returned. | |
4094dca5 | 698 | |
699 | ---------------------- | |
700 | -- Enclosing_Object -- | |
701 | ---------------------- | |
702 | ||
703 | function Enclosing_Object (N : Node_Id) return Node_Id is | |
704 | Obj_Name : Node_Id; | |
705 | ||
706 | begin | |
707 | Obj_Name := N; | |
708 | while Nkind_In (Obj_Name, N_Selected_Component, | |
709 | N_Indexed_Component, | |
710 | N_Slice) | |
711 | loop | |
712 | Obj_Name := Prefix (Obj_Name); | |
713 | end loop; | |
714 | ||
715 | return Get_Referenced_Object (Obj_Name); | |
716 | end Enclosing_Object; | |
717 | ||
718 | -- Local declarations | |
719 | ||
720 | Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object); | |
721 | ||
722 | -- Start of processing for Access_Cases | |
723 | ||
5e82d8fe | 724 | begin |
d251c56f | 725 | Btyp_DDT := Designated_Type (Btyp); |
726 | ||
727 | -- Handle designated types that come from the limited view | |
728 | ||
729 | if Ekind (Btyp_DDT) = E_Incomplete_Type | |
730 | and then From_With_Type (Btyp_DDT) | |
731 | and then Present (Non_Limited_View (Btyp_DDT)) | |
732 | then | |
733 | Btyp_DDT := Non_Limited_View (Btyp_DDT); | |
734 | ||
735 | elsif Is_Class_Wide_Type (Btyp_DDT) | |
736 | and then Ekind (Etype (Btyp_DDT)) = E_Incomplete_Type | |
737 | and then From_With_Type (Etype (Btyp_DDT)) | |
738 | and then Present (Non_Limited_View (Etype (Btyp_DDT))) | |
739 | and then Present (Class_Wide_Type | |
740 | (Non_Limited_View (Etype (Btyp_DDT)))) | |
741 | then | |
742 | Btyp_DDT := | |
743 | Class_Wide_Type (Non_Limited_View (Etype (Btyp_DDT))); | |
744 | end if; | |
745 | ||
4c06b9d2 | 746 | -- In order to improve the text of error messages, the designated |
747 | -- type of access-to-subprogram itypes is set by the semantics as | |
748 | -- the associated subprogram entity (see sem_attr). Now we replace | |
749 | -- such node with the proper E_Subprogram_Type itype. | |
750 | ||
751 | if Id = Attribute_Unrestricted_Access | |
752 | and then Is_Subprogram (Directly_Designated_Type (Typ)) | |
753 | then | |
d55c93e0 | 754 | -- The following conditions ensure that this special management |
4c06b9d2 | 755 | -- is done only for "Address!(Prim'Unrestricted_Access)" nodes. |
756 | -- At this stage other cases in which the designated type is | |
757 | -- still a subprogram (instead of an E_Subprogram_Type) are | |
526aedbb | 758 | -- wrong because the semantics must have overridden the type of |
4c06b9d2 | 759 | -- the node with the type imposed by the context. |
760 | ||
d55c93e0 | 761 | if Nkind (Parent (N)) = N_Unchecked_Type_Conversion |
762 | and then Etype (Parent (N)) = RTE (RE_Prim_Ptr) | |
763 | then | |
764 | Set_Etype (N, RTE (RE_Prim_Ptr)); | |
4c06b9d2 | 765 | |
d55c93e0 | 766 | else |
767 | declare | |
768 | Subp : constant Entity_Id := | |
769 | Directly_Designated_Type (Typ); | |
770 | Etyp : Entity_Id; | |
771 | Extra : Entity_Id := Empty; | |
772 | New_Formal : Entity_Id; | |
773 | Old_Formal : Entity_Id := First_Formal (Subp); | |
774 | Subp_Typ : Entity_Id; | |
4c06b9d2 | 775 | |
d55c93e0 | 776 | begin |
777 | Subp_Typ := Create_Itype (E_Subprogram_Type, N); | |
778 | Set_Etype (Subp_Typ, Etype (Subp)); | |
779 | Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp)); | |
4c06b9d2 | 780 | |
d55c93e0 | 781 | if Present (Old_Formal) then |
782 | New_Formal := New_Copy (Old_Formal); | |
783 | Set_First_Entity (Subp_Typ, New_Formal); | |
4c06b9d2 | 784 | |
d55c93e0 | 785 | loop |
786 | Set_Scope (New_Formal, Subp_Typ); | |
787 | Etyp := Etype (New_Formal); | |
4c06b9d2 | 788 | |
d55c93e0 | 789 | -- Handle itypes. There is no need to duplicate |
790 | -- here the itypes associated with record types | |
791 | -- (i.e the implicit full view of private types). | |
4c06b9d2 | 792 | |
d55c93e0 | 793 | if Is_Itype (Etyp) |
794 | and then Ekind (Base_Type (Etyp)) /= E_Record_Type | |
4c06b9d2 | 795 | then |
d55c93e0 | 796 | Extra := New_Copy (Etyp); |
797 | Set_Parent (Extra, New_Formal); | |
798 | Set_Etype (New_Formal, Extra); | |
799 | Set_Scope (Extra, Subp_Typ); | |
4c06b9d2 | 800 | end if; |
801 | ||
d55c93e0 | 802 | Extra := New_Formal; |
803 | Next_Formal (Old_Formal); | |
804 | exit when No (Old_Formal); | |
4c06b9d2 | 805 | |
d55c93e0 | 806 | Set_Next_Entity (New_Formal, |
807 | New_Copy (Old_Formal)); | |
808 | Next_Entity (New_Formal); | |
809 | end loop; | |
4c06b9d2 | 810 | |
d55c93e0 | 811 | Set_Next_Entity (New_Formal, Empty); |
812 | Set_Last_Entity (Subp_Typ, Extra); | |
813 | end if; | |
4c06b9d2 | 814 | |
d55c93e0 | 815 | -- Now that the explicit formals have been duplicated, |
816 | -- any extra formals needed by the subprogram must be | |
817 | -- created. | |
4c06b9d2 | 818 | |
d55c93e0 | 819 | if Present (Extra) then |
820 | Set_Extra_Formal (Extra, Empty); | |
821 | end if; | |
4c06b9d2 | 822 | |
d55c93e0 | 823 | Create_Extra_Formals (Subp_Typ); |
824 | Set_Directly_Designated_Type (Typ, Subp_Typ); | |
825 | end; | |
826 | end if; | |
4c06b9d2 | 827 | end if; |
828 | ||
5e82d8fe | 829 | if Is_Access_Protected_Subprogram_Type (Btyp) then |
830 | Expand_Access_To_Protected_Op (N, Pref, Typ); | |
831 | ||
832 | -- If prefix is a type name, this is a reference to the current | |
833 | -- instance of the type, within its initialization procedure. | |
834 | ||
835 | elsif Is_Entity_Name (Pref) | |
836 | and then Is_Type (Entity (Pref)) | |
837 | then | |
838 | declare | |
839 | Par : Node_Id; | |
840 | Formal : Entity_Id; | |
841 | ||
842 | begin | |
843 | -- If the current instance name denotes a task type, then | |
844 | -- the access attribute is rewritten to be the name of the | |
845 | -- "_task" parameter associated with the task type's task | |
846 | -- procedure. An unchecked conversion is applied to ensure | |
847 | -- a type match in cases of expander-generated calls (e.g. | |
848 | -- init procs). | |
849 | ||
850 | if Is_Task_Type (Entity (Pref)) then | |
851 | Formal := | |
852 | First_Entity (Get_Task_Body_Procedure (Entity (Pref))); | |
853 | while Present (Formal) loop | |
854 | exit when Chars (Formal) = Name_uTask; | |
855 | Next_Entity (Formal); | |
856 | end loop; | |
857 | ||
858 | pragma Assert (Present (Formal)); | |
f947f061 | 859 | |
5e82d8fe | 860 | Rewrite (N, |
861 | Unchecked_Convert_To (Typ, | |
862 | New_Occurrence_Of (Formal, Loc))); | |
863 | Set_Etype (N, Typ); | |
f947f061 | 864 | |
5e82d8fe | 865 | -- The expression must appear in a default expression, |
866 | -- (which in the initialization procedure is the | |
867 | -- right-hand side of an assignment), and not in a | |
868 | -- discriminant constraint. | |
f947f061 | 869 | |
5e82d8fe | 870 | else |
871 | Par := Parent (N); | |
872 | while Present (Par) loop | |
873 | exit when Nkind (Par) = N_Assignment_Statement; | |
f947f061 | 874 | |
5e82d8fe | 875 | if Nkind (Par) = N_Component_Declaration then |
876 | return; | |
877 | end if; | |
f947f061 | 878 | |
5e82d8fe | 879 | Par := Parent (Par); |
880 | end loop; | |
f947f061 | 881 | |
5e82d8fe | 882 | if Present (Par) then |
883 | Rewrite (N, | |
884 | Make_Attribute_Reference (Loc, | |
885 | Prefix => Make_Identifier (Loc, Name_uInit), | |
886 | Attribute_Name => Attribute_Name (N))); | |
f947f061 | 887 | |
5e82d8fe | 888 | Analyze_And_Resolve (N, Typ); |
889 | end if; | |
f947f061 | 890 | end if; |
5e82d8fe | 891 | end; |
892 | ||
893 | -- If the prefix of an Access attribute is a dereference of an | |
4094dca5 | 894 | -- access parameter (or a renaming of such a dereference, or a |
895 | -- subcomponent of such a dereference) and the context is a | |
80e22f63 | 896 | -- general access type (including the type of an object or |
897 | -- component with an access_definition, but not the anonymous | |
898 | -- type of an access parameter or access discriminant), then | |
4094dca5 | 899 | -- apply an accessibility check to the access parameter. We used |
900 | -- to rewrite the access parameter as a type conversion, but that | |
901 | -- could only be done if the immediate prefix of the Access | |
902 | -- attribute was the dereference, and didn't handle cases where | |
903 | -- the attribute is applied to a subcomponent of the dereference, | |
904 | -- since there's generally no available, appropriate access type | |
55dc6dc2 | 905 | -- to convert to in that case. The attribute is passed as the |
906 | -- point to insert the check, because the access parameter may | |
907 | -- come from a renaming, possibly in a different scope, and the | |
908 | -- check must be associated with the attribute itself. | |
4094dca5 | 909 | |
910 | elsif Id = Attribute_Access | |
911 | and then Nkind (Enc_Object) = N_Explicit_Dereference | |
912 | and then Is_Entity_Name (Prefix (Enc_Object)) | |
80e22f63 | 913 | and then (Ekind (Btyp) = E_General_Access_Type |
914 | or else Is_Local_Anonymous_Access (Btyp)) | |
4094dca5 | 915 | and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind |
916 | and then Ekind (Etype (Entity (Prefix (Enc_Object)))) | |
5e82d8fe | 917 | = E_Anonymous_Access_Type |
918 | and then Present (Extra_Accessibility | |
4094dca5 | 919 | (Entity (Prefix (Enc_Object)))) |
5e82d8fe | 920 | then |
55dc6dc2 | 921 | Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N); |
5e82d8fe | 922 | |
923 | -- Ada 2005 (AI-251): If the designated type is an interface we | |
924 | -- add an implicit conversion to force the displacement of the | |
925 | -- pointer to reference the secondary dispatch table. | |
926 | ||
927 | elsif Is_Interface (Btyp_DDT) | |
928 | and then (Comes_From_Source (N) | |
929 | or else Comes_From_Source (Ref_Object) | |
930 | or else (Nkind (Ref_Object) in N_Has_Chars | |
931 | and then Chars (Ref_Object) = Name_uInit)) | |
932 | then | |
933 | if Nkind (Ref_Object) /= N_Explicit_Dereference then | |
934 | ||
d8d8b098 | 935 | -- No implicit conversion required if types match, or if |
936 | -- the prefix is the class_wide_type of the interface. In | |
937 | -- either case passing an object of the interface type has | |
938 | -- already set the pointer correctly. | |
939 | ||
940 | if Btyp_DDT = Etype (Ref_Object) | |
941 | or else (Is_Class_Wide_Type (Etype (Ref_Object)) | |
942 | and then | |
943 | Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object)) | |
944 | then | |
945 | null; | |
5e82d8fe | 946 | |
d8d8b098 | 947 | else |
5e82d8fe | 948 | Rewrite (Prefix (N), |
d251c56f | 949 | Convert_To (Btyp_DDT, |
5e82d8fe | 950 | New_Copy_Tree (Prefix (N)))); |
951 | ||
d251c56f | 952 | Analyze_And_Resolve (Prefix (N), Btyp_DDT); |
ee6ba406 | 953 | end if; |
aad6babd | 954 | |
5e82d8fe | 955 | -- When the object is an explicit dereference, convert the |
956 | -- dereference's prefix. | |
f947f061 | 957 | |
5e82d8fe | 958 | else |
959 | declare | |
960 | Obj_DDT : constant Entity_Id := | |
961 | Base_Type | |
962 | (Directly_Designated_Type | |
963 | (Etype (Prefix (Ref_Object)))); | |
964 | begin | |
965 | -- No implicit conversion required if designated types | |
966 | -- match. | |
967 | ||
968 | if Obj_DDT /= Btyp_DDT | |
969 | and then not (Is_Class_Wide_Type (Obj_DDT) | |
970 | and then Etype (Obj_DDT) = Btyp_DDT) | |
971 | then | |
972 | Rewrite (N, | |
973 | Convert_To (Typ, | |
974 | New_Copy_Tree (Prefix (Ref_Object)))); | |
975 | Analyze_And_Resolve (N, Typ); | |
976 | end if; | |
977 | end; | |
ee6ba406 | 978 | end if; |
5e82d8fe | 979 | end if; |
980 | end Access_Cases; | |
ee6ba406 | 981 | |
982 | -------------- | |
983 | -- Adjacent -- | |
984 | -------------- | |
985 | ||
986 | -- Transforms 'Adjacent into a call to the floating-point attribute | |
987 | -- function Adjacent in Fat_xxx (where xxx is the root type) | |
988 | ||
989 | when Attribute_Adjacent => | |
990 | Expand_Fpt_Attribute_RR (N); | |
991 | ||
992 | ------------- | |
993 | -- Address -- | |
994 | ------------- | |
995 | ||
996 | when Attribute_Address => Address : declare | |
997 | Task_Proc : Entity_Id; | |
998 | ||
999 | begin | |
f947f061 | 1000 | -- If the prefix is a task or a task type, the useful address is that |
1001 | -- of the procedure for the task body, i.e. the actual program unit. | |
1002 | -- We replace the original entity with that of the procedure. | |
ee6ba406 | 1003 | |
1004 | if Is_Entity_Name (Pref) | |
1005 | and then Is_Task_Type (Entity (Pref)) | |
1006 | then | |
d55c93e0 | 1007 | Task_Proc := Next_Entity (Root_Type (Ptyp)); |
ee6ba406 | 1008 | |
1009 | while Present (Task_Proc) loop | |
1010 | exit when Ekind (Task_Proc) = E_Procedure | |
1011 | and then Etype (First_Formal (Task_Proc)) = | |
d55c93e0 | 1012 | Corresponding_Record_Type (Ptyp); |
ee6ba406 | 1013 | Next_Entity (Task_Proc); |
1014 | end loop; | |
1015 | ||
1016 | if Present (Task_Proc) then | |
1017 | Set_Entity (Pref, Task_Proc); | |
1018 | Set_Etype (Pref, Etype (Task_Proc)); | |
1019 | end if; | |
1020 | ||
1021 | -- Similarly, the address of a protected operation is the address | |
1022 | -- of the corresponding protected body, regardless of the protected | |
1023 | -- object from which it is selected. | |
1024 | ||
1025 | elsif Nkind (Pref) = N_Selected_Component | |
1026 | and then Is_Subprogram (Entity (Selector_Name (Pref))) | |
1027 | and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref)))) | |
1028 | then | |
1029 | Rewrite (Pref, | |
1030 | New_Occurrence_Of ( | |
1031 | External_Subprogram (Entity (Selector_Name (Pref))), Loc)); | |
1032 | ||
1033 | elsif Nkind (Pref) = N_Explicit_Dereference | |
d55c93e0 | 1034 | and then Ekind (Ptyp) = E_Subprogram_Type |
1035 | and then Convention (Ptyp) = Convention_Protected | |
ee6ba406 | 1036 | then |
1037 | -- The prefix is be a dereference of an access_to_protected_ | |
1038 | -- subprogram. The desired address is the second component of | |
1039 | -- the record that represents the access. | |
1040 | ||
1041 | declare | |
1042 | Addr : constant Entity_Id := Etype (N); | |
1043 | Ptr : constant Node_Id := Prefix (Pref); | |
1044 | T : constant Entity_Id := | |
1045 | Equivalent_Type (Base_Type (Etype (Ptr))); | |
1046 | ||
1047 | begin | |
1048 | Rewrite (N, | |
1049 | Unchecked_Convert_To (Addr, | |
1050 | Make_Selected_Component (Loc, | |
1051 | Prefix => Unchecked_Convert_To (T, Ptr), | |
1052 | Selector_Name => New_Occurrence_Of ( | |
1053 | Next_Entity (First_Entity (T)), Loc)))); | |
1054 | ||
1055 | Analyze_And_Resolve (N, Addr); | |
1056 | end; | |
99f2248e | 1057 | |
1058 | -- Ada 2005 (AI-251): Class-wide interface objects are always | |
1059 | -- "displaced" to reference the tag associated with the interface | |
1060 | -- type. In order to obtain the real address of such objects we | |
1061 | -- generate a call to a run-time subprogram that returns the base | |
1062 | -- address of the object. | |
1063 | ||
f0bf2ff3 | 1064 | -- This processing is not needed in the VM case, where dispatching |
1065 | -- issues are taken care of by the virtual machine. | |
1066 | ||
d55c93e0 | 1067 | elsif Is_Class_Wide_Type (Ptyp) |
1068 | and then Is_Interface (Ptyp) | |
662256db | 1069 | and then Tagged_Type_Expansion |
83aa52b6 | 1070 | and then not (Nkind (Pref) in N_Has_Entity |
1071 | and then Is_Subprogram (Entity (Pref))) | |
99f2248e | 1072 | then |
1073 | Rewrite (N, | |
1074 | Make_Function_Call (Loc, | |
1075 | Name => New_Reference_To (RTE (RE_Base_Address), Loc), | |
1076 | Parameter_Associations => New_List ( | |
1077 | Relocate_Node (N)))); | |
1078 | Analyze (N); | |
1079 | return; | |
ee6ba406 | 1080 | end if; |
1081 | ||
d55c93e0 | 1082 | -- Deal with packed array reference, other cases are handled by |
1083 | -- the back end. | |
ee6ba406 | 1084 | |
1085 | if Involves_Packed_Array_Reference (Pref) then | |
1086 | Expand_Packed_Address_Reference (N); | |
1087 | end if; | |
1088 | end Address; | |
1089 | ||
9dfe12ae | 1090 | --------------- |
1091 | -- Alignment -- | |
1092 | --------------- | |
1093 | ||
1094 | when Attribute_Alignment => Alignment : declare | |
9dfe12ae | 1095 | New_Node : Node_Id; |
1096 | ||
1097 | begin | |
1098 | -- For class-wide types, X'Class'Alignment is transformed into a | |
1099 | -- direct reference to the Alignment of the class type, so that the | |
1100 | -- back end does not have to deal with the X'Class'Alignment | |
1101 | -- reference. | |
1102 | ||
1103 | if Is_Entity_Name (Pref) | |
1104 | and then Is_Class_Wide_Type (Entity (Pref)) | |
1105 | then | |
1106 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
1107 | return; | |
1108 | ||
1109 | -- For x'Alignment applied to an object of a class wide type, | |
1110 | -- transform X'Alignment into a call to the predefined primitive | |
1111 | -- operation _Alignment applied to X. | |
1112 | ||
1113 | elsif Is_Class_Wide_Type (Ptyp) then | |
99f2248e | 1114 | |
1115 | -- No need to do anything else compiling under restriction | |
1116 | -- No_Dispatching_Calls. During the semantic analysis we | |
1117 | -- already notified such violation. | |
1118 | ||
1119 | if Restriction_Active (No_Dispatching_Calls) then | |
1120 | return; | |
1121 | end if; | |
1122 | ||
9dfe12ae | 1123 | New_Node := |
1124 | Make_Function_Call (Loc, | |
1125 | Name => New_Reference_To | |
1126 | (Find_Prim_Op (Ptyp, Name_uAlignment), Loc), | |
1127 | Parameter_Associations => New_List (Pref)); | |
1128 | ||
1129 | if Typ /= Standard_Integer then | |
1130 | ||
1131 | -- The context is a specific integer type with which the | |
1132 | -- original attribute was compatible. The function has a | |
1133 | -- specific type as well, so to preserve the compatibility | |
1134 | -- we must convert explicitly. | |
1135 | ||
1136 | New_Node := Convert_To (Typ, New_Node); | |
1137 | end if; | |
1138 | ||
1139 | Rewrite (N, New_Node); | |
1140 | Analyze_And_Resolve (N, Typ); | |
1141 | return; | |
1142 | ||
1143 | -- For all other cases, we just have to deal with the case of | |
1144 | -- the fact that the result can be universal. | |
1145 | ||
1146 | else | |
1147 | Apply_Universal_Integer_Attribute_Checks (N); | |
1148 | end if; | |
1149 | end Alignment; | |
1150 | ||
ee6ba406 | 1151 | --------------- |
1152 | -- AST_Entry -- | |
1153 | --------------- | |
1154 | ||
1155 | when Attribute_AST_Entry => AST_Entry : declare | |
1156 | Ttyp : Entity_Id; | |
1157 | T_Id : Node_Id; | |
1158 | Eent : Entity_Id; | |
1159 | ||
1160 | Entry_Ref : Node_Id; | |
1161 | -- The reference to the entry or entry family | |
1162 | ||
1163 | Index : Node_Id; | |
1164 | -- The index expression for an entry family reference, or | |
1165 | -- the Empty if Entry_Ref references a simple entry. | |
1166 | ||
1167 | begin | |
1168 | if Nkind (Pref) = N_Indexed_Component then | |
1169 | Entry_Ref := Prefix (Pref); | |
1170 | Index := First (Expressions (Pref)); | |
1171 | else | |
1172 | Entry_Ref := Pref; | |
1173 | Index := Empty; | |
1174 | end if; | |
1175 | ||
1176 | -- Get expression for Task_Id and the entry entity | |
1177 | ||
1178 | if Nkind (Entry_Ref) = N_Selected_Component then | |
1179 | T_Id := | |
1180 | Make_Attribute_Reference (Loc, | |
1181 | Attribute_Name => Name_Identity, | |
1182 | Prefix => Prefix (Entry_Ref)); | |
1183 | ||
1184 | Ttyp := Etype (Prefix (Entry_Ref)); | |
1185 | Eent := Entity (Selector_Name (Entry_Ref)); | |
1186 | ||
1187 | else | |
1188 | T_Id := | |
1189 | Make_Function_Call (Loc, | |
1190 | Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc)); | |
1191 | ||
1192 | Eent := Entity (Entry_Ref); | |
1193 | ||
1194 | -- We have to find the enclosing task to get the task type | |
1195 | -- There must be one, since we already validated this earlier | |
1196 | ||
1197 | Ttyp := Current_Scope; | |
1198 | while not Is_Task_Type (Ttyp) loop | |
1199 | Ttyp := Scope (Ttyp); | |
1200 | end loop; | |
1201 | end if; | |
1202 | ||
1203 | -- Now rewrite the attribute with a call to Create_AST_Handler | |
1204 | ||
1205 | Rewrite (N, | |
1206 | Make_Function_Call (Loc, | |
1207 | Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc), | |
1208 | Parameter_Associations => New_List ( | |
1209 | T_Id, | |
1210 | Entry_Index_Expression (Loc, Eent, Index, Ttyp)))); | |
1211 | ||
1212 | Analyze_And_Resolve (N, RTE (RE_AST_Handler)); | |
1213 | end AST_Entry; | |
1214 | ||
5c182b3b | 1215 | --------- |
1216 | -- Bit -- | |
1217 | --------- | |
1218 | ||
1219 | -- We compute this if a packed array reference was present, otherwise we | |
1220 | -- leave the computation up to the back end. | |
1221 | ||
1222 | when Attribute_Bit => | |
1223 | if Involves_Packed_Array_Reference (Pref) then | |
1224 | Expand_Packed_Bit_Reference (N); | |
1225 | else | |
1226 | Apply_Universal_Integer_Attribute_Checks (N); | |
1227 | end if; | |
1228 | ||
ee6ba406 | 1229 | ------------------ |
1230 | -- Bit_Position -- | |
1231 | ------------------ | |
1232 | ||
d55c93e0 | 1233 | -- We compute this if a component clause was present, otherwise we leave |
1234 | -- the computation up to the back end, since we don't know what layout | |
1235 | -- will be chosen. | |
ee6ba406 | 1236 | |
1237 | -- Note that the attribute can apply to a naked record component | |
1238 | -- in generated code (i.e. the prefix is an identifier that | |
1239 | -- references the component or discriminant entity). | |
1240 | ||
5c182b3b | 1241 | when Attribute_Bit_Position => Bit_Position : declare |
ee6ba406 | 1242 | CE : Entity_Id; |
1243 | ||
1244 | begin | |
1245 | if Nkind (Pref) = N_Identifier then | |
1246 | CE := Entity (Pref); | |
1247 | else | |
1248 | CE := Entity (Selector_Name (Pref)); | |
1249 | end if; | |
1250 | ||
1251 | if Known_Static_Component_Bit_Offset (CE) then | |
1252 | Rewrite (N, | |
1253 | Make_Integer_Literal (Loc, | |
1254 | Intval => Component_Bit_Offset (CE))); | |
1255 | Analyze_And_Resolve (N, Typ); | |
1256 | ||
1257 | else | |
1258 | Apply_Universal_Integer_Attribute_Checks (N); | |
1259 | end if; | |
1260 | end Bit_Position; | |
1261 | ||
1262 | ------------------ | |
1263 | -- Body_Version -- | |
1264 | ------------------ | |
1265 | ||
1266 | -- A reference to P'Body_Version or P'Version is expanded to | |
1267 | ||
1268 | -- Vnn : Unsigned; | |
17953e50 | 1269 | -- pragma Import (C, Vnn, "uuuuT"); |
ee6ba406 | 1270 | -- ... |
1271 | -- Get_Version_String (Vnn) | |
1272 | ||
1273 | -- where uuuu is the unit name (dots replaced by double underscore) | |
1274 | -- and T is B for the cases of Body_Version, or Version applied to a | |
1275 | -- subprogram acting as its own spec, and S for Version applied to a | |
1276 | -- subprogram spec or package. This sequence of code references the | |
6fb3c314 | 1277 | -- unsigned constant created in the main program by the binder. |
ee6ba406 | 1278 | |
36dccb2b | 1279 | -- A special exception occurs for Standard, where the string returned |
1280 | -- is a copy of the library string in gnatvsn.ads. | |
ee6ba406 | 1281 | |
1282 | when Attribute_Body_Version | Attribute_Version => Version : declare | |
46eb6933 | 1283 | E : constant Entity_Id := Make_Temporary (Loc, 'V'); |
f947f061 | 1284 | Pent : Entity_Id; |
ee6ba406 | 1285 | S : String_Id; |
1286 | ||
1287 | begin | |
1288 | -- If not library unit, get to containing library unit | |
1289 | ||
f947f061 | 1290 | Pent := Entity (Pref); |
ee6ba406 | 1291 | while Pent /= Standard_Standard |
1292 | and then Scope (Pent) /= Standard_Standard | |
f947f061 | 1293 | and then not Is_Child_Unit (Pent) |
ee6ba406 | 1294 | loop |
1295 | Pent := Scope (Pent); | |
1296 | end loop; | |
1297 | ||
f947f061 | 1298 | -- Special case Standard and Standard.ASCII |
ee6ba406 | 1299 | |
f947f061 | 1300 | if Pent = Standard_Standard or else Pent = Standard_ASCII then |
ee6ba406 | 1301 | Rewrite (N, |
1302 | Make_String_Literal (Loc, | |
5245b786 | 1303 | Strval => Verbose_Library_Version)); |
ee6ba406 | 1304 | |
1305 | -- All other cases | |
1306 | ||
1307 | else | |
1308 | -- Build required string constant | |
1309 | ||
1310 | Get_Name_String (Get_Unit_Name (Pent)); | |
1311 | ||
1312 | Start_String; | |
1313 | for J in 1 .. Name_Len - 2 loop | |
1314 | if Name_Buffer (J) = '.' then | |
1315 | Store_String_Chars ("__"); | |
1316 | else | |
1317 | Store_String_Char (Get_Char_Code (Name_Buffer (J))); | |
1318 | end if; | |
1319 | end loop; | |
1320 | ||
1321 | -- Case of subprogram acting as its own spec, always use body | |
1322 | ||
1323 | if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification | |
1324 | and then Nkind (Parent (Declaration_Node (Pent))) = | |
1325 | N_Subprogram_Body | |
1326 | and then Acts_As_Spec (Parent (Declaration_Node (Pent))) | |
1327 | then | |
1328 | Store_String_Chars ("B"); | |
1329 | ||
1330 | -- Case of no body present, always use spec | |
1331 | ||
1332 | elsif not Unit_Requires_Body (Pent) then | |
1333 | Store_String_Chars ("S"); | |
1334 | ||
1335 | -- Otherwise use B for Body_Version, S for spec | |
1336 | ||
1337 | elsif Id = Attribute_Body_Version then | |
1338 | Store_String_Chars ("B"); | |
1339 | else | |
1340 | Store_String_Chars ("S"); | |
1341 | end if; | |
1342 | ||
1343 | S := End_String; | |
1344 | Lib.Version_Referenced (S); | |
1345 | ||
1346 | -- Insert the object declaration | |
1347 | ||
1348 | Insert_Actions (N, New_List ( | |
1349 | Make_Object_Declaration (Loc, | |
1350 | Defining_Identifier => E, | |
1351 | Object_Definition => | |
1352 | New_Occurrence_Of (RTE (RE_Unsigned), Loc)))); | |
1353 | ||
1354 | -- Set entity as imported with correct external name | |
1355 | ||
1356 | Set_Is_Imported (E); | |
1357 | Set_Interface_Name (E, Make_String_Literal (Loc, S)); | |
1358 | ||
f947f061 | 1359 | -- Set entity as internal to ensure proper Sprint output of its |
1360 | -- implicit importation. | |
1361 | ||
1362 | Set_Is_Internal (E); | |
1363 | ||
ee6ba406 | 1364 | -- And now rewrite original reference |
1365 | ||
1366 | Rewrite (N, | |
1367 | Make_Function_Call (Loc, | |
1368 | Name => New_Reference_To (RTE (RE_Get_Version_String), Loc), | |
1369 | Parameter_Associations => New_List ( | |
1370 | New_Occurrence_Of (E, Loc)))); | |
1371 | end if; | |
1372 | ||
1373 | Analyze_And_Resolve (N, RTE (RE_Version_String)); | |
1374 | end Version; | |
1375 | ||
1376 | ------------- | |
1377 | -- Ceiling -- | |
1378 | ------------- | |
1379 | ||
1380 | -- Transforms 'Ceiling into a call to the floating-point attribute | |
1381 | -- function Ceiling in Fat_xxx (where xxx is the root type) | |
1382 | ||
1383 | when Attribute_Ceiling => | |
1384 | Expand_Fpt_Attribute_R (N); | |
1385 | ||
1386 | -------------- | |
1387 | -- Callable -- | |
1388 | -------------- | |
1389 | ||
aad6babd | 1390 | -- Transforms 'Callable attribute into a call to the Callable function |
ee6ba406 | 1391 | |
1392 | when Attribute_Callable => Callable : | |
1393 | begin | |
1550b445 | 1394 | -- We have an object of a task interface class-wide type as a prefix |
1395 | -- to Callable. Generate: | |
83aa52b6 | 1396 | -- callable (Task_Id (Pref._disp_get_task_id)); |
1550b445 | 1397 | |
de54c5ab | 1398 | if Ada_Version >= Ada_2005 |
d55c93e0 | 1399 | and then Ekind (Ptyp) = E_Class_Wide_Type |
1400 | and then Is_Interface (Ptyp) | |
1401 | and then Is_Task_Interface (Ptyp) | |
1550b445 | 1402 | then |
1403 | Rewrite (N, | |
1404 | Make_Function_Call (Loc, | |
1405 | Name => | |
1406 | New_Reference_To (RTE (RE_Callable), Loc), | |
1407 | Parameter_Associations => New_List ( | |
83aa52b6 | 1408 | Make_Unchecked_Type_Conversion (Loc, |
1409 | Subtype_Mark => | |
1410 | New_Reference_To (RTE (RO_ST_Task_Id), Loc), | |
1411 | Expression => | |
1412 | Make_Selected_Component (Loc, | |
1413 | Prefix => | |
1414 | New_Copy_Tree (Pref), | |
1415 | Selector_Name => | |
1416 | Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))))); | |
1417 | ||
1550b445 | 1418 | else |
1419 | Rewrite (N, | |
1420 | Build_Call_With_Task (Pref, RTE (RE_Callable))); | |
1421 | end if; | |
1422 | ||
ee6ba406 | 1423 | Analyze_And_Resolve (N, Standard_Boolean); |
1424 | end Callable; | |
1425 | ||
1426 | ------------ | |
1427 | -- Caller -- | |
1428 | ------------ | |
1429 | ||
1430 | -- Transforms 'Caller attribute into a call to either the | |
1431 | -- Task_Entry_Caller or the Protected_Entry_Caller function. | |
1432 | ||
1433 | when Attribute_Caller => Caller : declare | |
7f9be362 | 1434 | Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id); |
9dfe12ae | 1435 | Ent : constant Entity_Id := Entity (Pref); |
1436 | Conctype : constant Entity_Id := Scope (Ent); | |
1437 | Nest_Depth : Integer := 0; | |
ee6ba406 | 1438 | Name : Node_Id; |
1439 | S : Entity_Id; | |
1440 | ||
1441 | begin | |
1442 | -- Protected case | |
1443 | ||
1444 | if Is_Protected_Type (Conctype) then | |
4c06b9d2 | 1445 | case Corresponding_Runtime_Package (Conctype) is |
1446 | when System_Tasking_Protected_Objects_Entries => | |
1447 | Name := | |
1448 | New_Reference_To | |
1449 | (RTE (RE_Protected_Entry_Caller), Loc); | |
1450 | ||
1451 | when System_Tasking_Protected_Objects_Single_Entry => | |
1452 | Name := | |
1453 | New_Reference_To | |
1454 | (RTE (RE_Protected_Single_Entry_Caller), Loc); | |
1455 | ||
1456 | when others => | |
1457 | raise Program_Error; | |
1458 | end case; | |
ee6ba406 | 1459 | |
1460 | Rewrite (N, | |
1461 | Unchecked_Convert_To (Id_Kind, | |
1462 | Make_Function_Call (Loc, | |
1463 | Name => Name, | |
d55c93e0 | 1464 | Parameter_Associations => New_List ( |
1465 | New_Reference_To | |
1466 | (Find_Protection_Object (Current_Scope), Loc))))); | |
ee6ba406 | 1467 | |
1468 | -- Task case | |
1469 | ||
1470 | else | |
1471 | -- Determine the nesting depth of the E'Caller attribute, that | |
1472 | -- is, how many accept statements are nested within the accept | |
1473 | -- statement for E at the point of E'Caller. The runtime uses | |
1474 | -- this depth to find the specified entry call. | |
1475 | ||
1476 | for J in reverse 0 .. Scope_Stack.Last loop | |
1477 | S := Scope_Stack.Table (J).Entity; | |
1478 | ||
1479 | -- We should not reach the scope of the entry, as it should | |
1480 | -- already have been checked in Sem_Attr that this attribute | |
1481 | -- reference is within a matching accept statement. | |
1482 | ||
1483 | pragma Assert (S /= Conctype); | |
1484 | ||
1485 | if S = Ent then | |
1486 | exit; | |
1487 | ||
1488 | elsif Is_Entry (S) then | |
1489 | Nest_Depth := Nest_Depth + 1; | |
1490 | end if; | |
1491 | end loop; | |
1492 | ||
1493 | Rewrite (N, | |
1494 | Unchecked_Convert_To (Id_Kind, | |
1495 | Make_Function_Call (Loc, | |
d55c93e0 | 1496 | Name => |
1497 | New_Reference_To (RTE (RE_Task_Entry_Caller), Loc), | |
ee6ba406 | 1498 | Parameter_Associations => New_List ( |
1499 | Make_Integer_Literal (Loc, | |
1500 | Intval => Int (Nest_Depth)))))); | |
1501 | end if; | |
1502 | ||
1503 | Analyze_And_Resolve (N, Id_Kind); | |
1504 | end Caller; | |
1505 | ||
1506 | ------------- | |
1507 | -- Compose -- | |
1508 | ------------- | |
1509 | ||
1510 | -- Transforms 'Compose into a call to the floating-point attribute | |
1511 | -- function Compose in Fat_xxx (where xxx is the root type) | |
1512 | ||
1513 | -- Note: we strictly should have special code here to deal with the | |
1514 | -- case of absurdly negative arguments (less than Integer'First) | |
1515 | -- which will return a (signed) zero value, but it hardly seems | |
1516 | -- worth the effort. Absurdly large positive arguments will raise | |
1517 | -- constraint error which is fine. | |
1518 | ||
1519 | when Attribute_Compose => | |
1520 | Expand_Fpt_Attribute_RI (N); | |
1521 | ||
1522 | ----------------- | |
1523 | -- Constrained -- | |
1524 | ----------------- | |
1525 | ||
1526 | when Attribute_Constrained => Constrained : declare | |
1527 | Formal_Ent : constant Entity_Id := Param_Entity (Pref); | |
1528 | ||
7f8eb6ed | 1529 | function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean; |
1530 | -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a | |
1531 | -- view of an aliased object whose subtype is constrained. | |
1532 | ||
1533 | --------------------------------- | |
1534 | -- Is_Constrained_Aliased_View -- | |
1535 | --------------------------------- | |
1536 | ||
1537 | function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is | |
1538 | E : Entity_Id; | |
1539 | ||
1540 | begin | |
1541 | if Is_Entity_Name (Obj) then | |
1542 | E := Entity (Obj); | |
1543 | ||
1544 | if Present (Renamed_Object (E)) then | |
1545 | return Is_Constrained_Aliased_View (Renamed_Object (E)); | |
7f8eb6ed | 1546 | else |
1547 | return Is_Aliased (E) and then Is_Constrained (Etype (E)); | |
1548 | end if; | |
1549 | ||
1550 | else | |
1551 | return Is_Aliased_View (Obj) | |
1552 | and then | |
1553 | (Is_Constrained (Etype (Obj)) | |
1554 | or else (Nkind (Obj) = N_Explicit_Dereference | |
1555 | and then | |
1556 | not Has_Constrained_Partial_View | |
1557 | (Base_Type (Etype (Obj))))); | |
1558 | end if; | |
1559 | end Is_Constrained_Aliased_View; | |
1560 | ||
1561 | -- Start of processing for Constrained | |
1562 | ||
ee6ba406 | 1563 | begin |
1564 | -- Reference to a parameter where the value is passed as an extra | |
1565 | -- actual, corresponding to the extra formal referenced by the | |
9dfe12ae | 1566 | -- Extra_Constrained field of the corresponding formal. If this |
1567 | -- is an entry in-parameter, it is replaced by a constant renaming | |
1568 | -- for which Extra_Constrained is never created. | |
ee6ba406 | 1569 | |
1570 | if Present (Formal_Ent) | |
9dfe12ae | 1571 | and then Ekind (Formal_Ent) /= E_Constant |
ee6ba406 | 1572 | and then Present (Extra_Constrained (Formal_Ent)) |
1573 | then | |
1574 | Rewrite (N, | |
1575 | New_Occurrence_Of | |
1576 | (Extra_Constrained (Formal_Ent), Sloc (N))); | |
1577 | ||
1578 | -- For variables with a Extra_Constrained field, we use the | |
1579 | -- corresponding entity. | |
1580 | ||
1581 | elsif Nkind (Pref) = N_Identifier | |
1582 | and then Ekind (Entity (Pref)) = E_Variable | |
1583 | and then Present (Extra_Constrained (Entity (Pref))) | |
1584 | then | |
1585 | Rewrite (N, | |
1586 | New_Occurrence_Of | |
1587 | (Extra_Constrained (Entity (Pref)), Sloc (N))); | |
1588 | ||
1589 | -- For all other entity names, we can tell at compile time | |
1590 | ||
1591 | elsif Is_Entity_Name (Pref) then | |
1592 | declare | |
1593 | Ent : constant Entity_Id := Entity (Pref); | |
1594 | Res : Boolean; | |
1595 | ||
1596 | begin | |
1597 | -- (RM J.4) obsolescent cases | |
1598 | ||
1599 | if Is_Type (Ent) then | |
1600 | ||
1601 | -- Private type | |
1602 | ||
1603 | if Is_Private_Type (Ent) then | |
1604 | Res := not Has_Discriminants (Ent) | |
1605 | or else Is_Constrained (Ent); | |
1606 | ||
1607 | -- It not a private type, must be a generic actual type | |
1608 | -- that corresponded to a private type. We know that this | |
1609 | -- correspondence holds, since otherwise the reference | |
1610 | -- within the generic template would have been illegal. | |
1611 | ||
1612 | else | |
9dfe12ae | 1613 | if Is_Composite_Type (Underlying_Type (Ent)) then |
1614 | Res := Is_Constrained (Ent); | |
1615 | else | |
1616 | Res := True; | |
1617 | end if; | |
ee6ba406 | 1618 | end if; |
1619 | ||
1620 | -- If the prefix is not a variable or is aliased, then | |
d55c93e0 | 1621 | -- definitely true; if it's a formal parameter without an |
1622 | -- associated extra formal, then treat it as constrained. | |
ee6ba406 | 1623 | |
7f8eb6ed | 1624 | -- Ada 2005 (AI-363): An aliased prefix must be known to be |
1625 | -- constrained in order to set the attribute to True. | |
1626 | ||
ee6ba406 | 1627 | elsif not Is_Variable (Pref) |
1628 | or else Present (Formal_Ent) | |
de54c5ab | 1629 | or else (Ada_Version < Ada_2005 |
7f8eb6ed | 1630 | and then Is_Aliased_View (Pref)) |
de54c5ab | 1631 | or else (Ada_Version >= Ada_2005 |
7f8eb6ed | 1632 | and then Is_Constrained_Aliased_View (Pref)) |
ee6ba406 | 1633 | then |
1634 | Res := True; | |
1635 | ||
d55c93e0 | 1636 | -- Variable case, look at type to see if it is constrained. |
1637 | -- Note that the one case where this is not accurate (the | |
1638 | -- procedure formal case), has been handled above. | |
ee6ba406 | 1639 | |
99f2248e | 1640 | -- We use the Underlying_Type here (and below) in case the |
1641 | -- type is private without discriminants, but the full type | |
1642 | -- has discriminants. This case is illegal, but we generate it | |
1643 | -- internally for passing to the Extra_Constrained parameter. | |
1644 | ||
ee6ba406 | 1645 | else |
e8b5ac67 | 1646 | -- In Ada 2012, test for case of a limited tagged type, in |
1647 | -- which case the attribute is always required to return | |
1648 | -- True. The underlying type is tested, to make sure we also | |
1649 | -- return True for cases where there is an unconstrained | |
1650 | -- object with an untagged limited partial view which has | |
1651 | -- defaulted discriminants (such objects always produce a | |
1652 | -- False in earlier versions of Ada). (Ada 2012: AI05-0214) | |
1653 | ||
1654 | Res := Is_Constrained (Underlying_Type (Etype (Ent))) | |
1655 | or else | |
1656 | (Ada_Version >= Ada_2012 | |
1657 | and then Is_Tagged_Type (Underlying_Type (Ptyp)) | |
1658 | and then Is_Limited_Type (Ptyp)); | |
ee6ba406 | 1659 | end if; |
1660 | ||
e8b5ac67 | 1661 | Rewrite (N, New_Reference_To (Boolean_Literals (Res), Loc)); |
ee6ba406 | 1662 | end; |
1663 | ||
d55c93e0 | 1664 | -- Prefix is not an entity name. These are also cases where we can |
1665 | -- always tell at compile time by looking at the form and type of the | |
1666 | -- prefix. If an explicit dereference of an object with constrained | |
e8b5ac67 | 1667 | -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the |
1668 | -- underlying type is a limited tagged type, then Constrained is | |
1669 | -- required to always return True (Ada 2012: AI05-0214). | |
ee6ba406 | 1670 | |
1671 | else | |
1bbc9831 | 1672 | Rewrite (N, |
1673 | New_Reference_To ( | |
1674 | Boolean_Literals ( | |
1675 | not Is_Variable (Pref) | |
aad6babd | 1676 | or else |
1677 | (Nkind (Pref) = N_Explicit_Dereference | |
e8b5ac67 | 1678 | and then |
1679 | not Has_Constrained_Partial_View (Base_Type (Ptyp))) | |
1680 | or else Is_Constrained (Underlying_Type (Ptyp)) | |
1681 | or else (Ada_Version >= Ada_2012 | |
1682 | and then Is_Tagged_Type (Underlying_Type (Ptyp)) | |
1683 | and then Is_Limited_Type (Ptyp))), | |
1bbc9831 | 1684 | Loc)); |
ee6ba406 | 1685 | end if; |
1686 | ||
1687 | Analyze_And_Resolve (N, Standard_Boolean); | |
1688 | end Constrained; | |
1689 | ||
1690 | --------------- | |
1691 | -- Copy_Sign -- | |
1692 | --------------- | |
1693 | ||
1694 | -- Transforms 'Copy_Sign into a call to the floating-point attribute | |
1695 | -- function Copy_Sign in Fat_xxx (where xxx is the root type) | |
1696 | ||
1697 | when Attribute_Copy_Sign => | |
1698 | Expand_Fpt_Attribute_RR (N); | |
1699 | ||
1700 | ----------- | |
1701 | -- Count -- | |
1702 | ----------- | |
1703 | ||
1704 | -- Transforms 'Count attribute into a call to the Count function | |
1705 | ||
d55c93e0 | 1706 | when Attribute_Count => Count : declare |
1707 | Call : Node_Id; | |
1708 | Conctyp : Entity_Id; | |
1709 | Entnam : Node_Id; | |
1710 | Entry_Id : Entity_Id; | |
1711 | Index : Node_Id; | |
1712 | Name : Node_Id; | |
ee6ba406 | 1713 | |
1714 | begin | |
1715 | -- If the prefix is a member of an entry family, retrieve both | |
1716 | -- entry name and index. For a simple entry there is no index. | |
1717 | ||
1718 | if Nkind (Pref) = N_Indexed_Component then | |
1719 | Entnam := Prefix (Pref); | |
1720 | Index := First (Expressions (Pref)); | |
1721 | else | |
1722 | Entnam := Pref; | |
1723 | Index := Empty; | |
1724 | end if; | |
1725 | ||
d55c93e0 | 1726 | Entry_Id := Entity (Entnam); |
1727 | ||
ee6ba406 | 1728 | -- Find the concurrent type in which this attribute is referenced |
1729 | -- (there had better be one). | |
1730 | ||
1731 | Conctyp := Current_Scope; | |
1732 | while not Is_Concurrent_Type (Conctyp) loop | |
1733 | Conctyp := Scope (Conctyp); | |
1734 | end loop; | |
1735 | ||
1736 | -- Protected case | |
1737 | ||
1738 | if Is_Protected_Type (Conctyp) then | |
4c06b9d2 | 1739 | case Corresponding_Runtime_Package (Conctyp) is |
1740 | when System_Tasking_Protected_Objects_Entries => | |
1741 | Name := New_Reference_To (RTE (RE_Protected_Count), Loc); | |
1742 | ||
1743 | Call := | |
1744 | Make_Function_Call (Loc, | |
1745 | Name => Name, | |
1746 | Parameter_Associations => New_List ( | |
d55c93e0 | 1747 | New_Reference_To |
1748 | (Find_Protection_Object (Current_Scope), Loc), | |
1749 | Entry_Index_Expression | |
1750 | (Loc, Entry_Id, Index, Scope (Entry_Id)))); | |
4c06b9d2 | 1751 | |
1752 | when System_Tasking_Protected_Objects_Single_Entry => | |
d55c93e0 | 1753 | Name := |
1754 | New_Reference_To (RTE (RE_Protected_Count_Entry), Loc); | |
4c06b9d2 | 1755 | |
1756 | Call := | |
1757 | Make_Function_Call (Loc, | |
1758 | Name => Name, | |
1759 | Parameter_Associations => New_List ( | |
d55c93e0 | 1760 | New_Reference_To |
1761 | (Find_Protection_Object (Current_Scope), Loc))); | |
1762 | ||
4c06b9d2 | 1763 | when others => |
1764 | raise Program_Error; | |
4c06b9d2 | 1765 | end case; |
ee6ba406 | 1766 | |
1767 | -- Task case | |
1768 | ||
1769 | else | |
1770 | Call := | |
1771 | Make_Function_Call (Loc, | |
1772 | Name => New_Reference_To (RTE (RE_Task_Count), Loc), | |
1773 | Parameter_Associations => New_List ( | |
d55c93e0 | 1774 | Entry_Index_Expression (Loc, |
1775 | Entry_Id, Index, Scope (Entry_Id)))); | |
ee6ba406 | 1776 | end if; |
1777 | ||
1778 | -- The call returns type Natural but the context is universal integer | |
1779 | -- so any integer type is allowed. The attribute was already resolved | |
1780 | -- so its Etype is the required result type. If the base type of the | |
1781 | -- context type is other than Standard.Integer we put in a conversion | |
1782 | -- to the required type. This can be a normal typed conversion since | |
1783 | -- both input and output types of the conversion are integer types | |
1784 | ||
1785 | if Base_Type (Typ) /= Base_Type (Standard_Integer) then | |
1786 | Rewrite (N, Convert_To (Typ, Call)); | |
1787 | else | |
1788 | Rewrite (N, Call); | |
1789 | end if; | |
1790 | ||
1791 | Analyze_And_Resolve (N, Typ); | |
1792 | end Count; | |
1793 | ||
1794 | --------------- | |
1795 | -- Elab_Body -- | |
1796 | --------------- | |
1797 | ||
1798 | -- This processing is shared by Elab_Spec | |
1799 | ||
1800 | -- What we do is to insert the following declarations | |
1801 | ||
1802 | -- procedure tnn; | |
1803 | -- pragma Import (C, enn, "name___elabb/s"); | |
1804 | ||
1805 | -- and then the Elab_Body/Spec attribute is replaced by a reference | |
1806 | -- to this defining identifier. | |
1807 | ||
1808 | when Attribute_Elab_Body | | |
1809 | Attribute_Elab_Spec => | |
1810 | ||
1811 | Elab_Body : declare | |
46eb6933 | 1812 | Ent : constant Entity_Id := Make_Temporary (Loc, 'E'); |
ee6ba406 | 1813 | Str : String_Id; |
1814 | Lang : Node_Id; | |
1815 | ||
1816 | procedure Make_Elab_String (Nod : Node_Id); | |
1817 | -- Given Nod, an identifier, or a selected component, put the | |
1818 | -- image into the current string literal, with double underline | |
1819 | -- between components. | |
1820 | ||
7f8eb6ed | 1821 | ---------------------- |
1822 | -- Make_Elab_String -- | |
1823 | ---------------------- | |
1824 | ||
ee6ba406 | 1825 | procedure Make_Elab_String (Nod : Node_Id) is |
1826 | begin | |
1827 | if Nkind (Nod) = N_Selected_Component then | |
1828 | Make_Elab_String (Prefix (Nod)); | |
7f8eb6ed | 1829 | |
83aa52b6 | 1830 | case VM_Target is |
1831 | when JVM_Target => | |
1832 | Store_String_Char ('$'); | |
1833 | when CLI_Target => | |
1834 | Store_String_Char ('.'); | |
1835 | when No_VM => | |
1836 | Store_String_Char ('_'); | |
1837 | Store_String_Char ('_'); | |
1838 | end case; | |
ee6ba406 | 1839 | |
1840 | Get_Name_String (Chars (Selector_Name (Nod))); | |
1841 | ||
1842 | else | |
1843 | pragma Assert (Nkind (Nod) = N_Identifier); | |
1844 | Get_Name_String (Chars (Nod)); | |
1845 | end if; | |
1846 | ||
1847 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
1848 | end Make_Elab_String; | |
1849 | ||
1850 | -- Start of processing for Elab_Body/Elab_Spec | |
1851 | ||
1852 | begin | |
1853 | -- First we need to prepare the string literal for the name of | |
1854 | -- the elaboration routine to be referenced. | |
1855 | ||
1856 | Start_String; | |
1857 | Make_Elab_String (Pref); | |
1858 | ||
83aa52b6 | 1859 | if VM_Target = No_VM then |
ee6ba406 | 1860 | Store_String_Chars ("___elab"); |
1861 | Lang := Make_Identifier (Loc, Name_C); | |
83aa52b6 | 1862 | else |
1863 | Store_String_Chars ("._elab"); | |
1864 | Lang := Make_Identifier (Loc, Name_Ada); | |
ee6ba406 | 1865 | end if; |
1866 | ||
1867 | if Id = Attribute_Elab_Body then | |
1868 | Store_String_Char ('b'); | |
1869 | else | |
1870 | Store_String_Char ('s'); | |
1871 | end if; | |
1872 | ||
1873 | Str := End_String; | |
1874 | ||
1875 | Insert_Actions (N, New_List ( | |
1876 | Make_Subprogram_Declaration (Loc, | |
1877 | Specification => | |
1878 | Make_Procedure_Specification (Loc, | |
1879 | Defining_Unit_Name => Ent)), | |
1880 | ||
1881 | Make_Pragma (Loc, | |
1882 | Chars => Name_Import, | |
1883 | Pragma_Argument_Associations => New_List ( | |
55868293 | 1884 | Make_Pragma_Argument_Association (Loc, Expression => Lang), |
ee6ba406 | 1885 | |
1886 | Make_Pragma_Argument_Association (Loc, | |
55868293 | 1887 | Expression => Make_Identifier (Loc, Chars (Ent))), |
ee6ba406 | 1888 | |
1889 | Make_Pragma_Argument_Association (Loc, | |
55868293 | 1890 | Expression => Make_String_Literal (Loc, Str)))))); |
ee6ba406 | 1891 | |
1892 | Set_Entity (N, Ent); | |
1893 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
1894 | end Elab_Body; | |
1895 | ||
1896 | ---------------- | |
1897 | -- Elaborated -- | |
1898 | ---------------- | |
1899 | ||
d55c93e0 | 1900 | -- Elaborated is always True for preelaborated units, predefined units, |
1901 | -- pure units and units which have Elaborate_Body pragmas. These units | |
1902 | -- have no elaboration entity. | |
ee6ba406 | 1903 | |
d55c93e0 | 1904 | -- Note: The Elaborated attribute is never passed to the back end |
ee6ba406 | 1905 | |
1906 | when Attribute_Elaborated => Elaborated : declare | |
1907 | Ent : constant Entity_Id := Entity (Pref); | |
1908 | ||
1909 | begin | |
1910 | if Present (Elaboration_Entity (Ent)) then | |
1911 | Rewrite (N, | |
1912 | New_Occurrence_Of (Elaboration_Entity (Ent), Loc)); | |
1913 | else | |
1914 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
1915 | end if; | |
1916 | end Elaborated; | |
1917 | ||
1918 | -------------- | |
1919 | -- Enum_Rep -- | |
1920 | -------------- | |
1921 | ||
1922 | when Attribute_Enum_Rep => Enum_Rep : | |
1923 | begin | |
1924 | -- X'Enum_Rep (Y) expands to | |
1925 | ||
1926 | -- target-type (Y) | |
1927 | ||
d55c93e0 | 1928 | -- This is simply a direct conversion from the enumeration type to |
1929 | -- the target integer type, which is treated by the back end as a | |
1930 | -- normal integer conversion, treating the enumeration type as an | |
1931 | -- integer, which is exactly what we want! We set Conversion_OK to | |
1932 | -- make sure that the analyzer does not complain about what otherwise | |
1933 | -- might be an illegal conversion. | |
ee6ba406 | 1934 | |
1935 | if Is_Non_Empty_List (Exprs) then | |
1936 | Rewrite (N, | |
1937 | OK_Convert_To (Typ, Relocate_Node (First (Exprs)))); | |
1938 | ||
1939 | -- X'Enum_Rep where X is an enumeration literal is replaced by | |
1940 | -- the literal value. | |
1941 | ||
1942 | elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then | |
1943 | Rewrite (N, | |
1944 | Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref)))); | |
1945 | ||
9dfe12ae | 1946 | -- If this is a renaming of a literal, recover the representation |
1947 | -- of the original. | |
1948 | ||
1949 | elsif Ekind (Entity (Pref)) = E_Constant | |
1950 | and then Present (Renamed_Object (Entity (Pref))) | |
1951 | and then | |
1952 | Ekind (Entity (Renamed_Object (Entity (Pref)))) | |
1953 | = E_Enumeration_Literal | |
1954 | then | |
1955 | Rewrite (N, | |
1956 | Make_Integer_Literal (Loc, | |
1957 | Enumeration_Rep (Entity (Renamed_Object (Entity (Pref)))))); | |
1958 | ||
ee6ba406 | 1959 | -- X'Enum_Rep where X is an object does a direct unchecked conversion |
1960 | -- of the object value, as described for the type case above. | |
1961 | ||
1962 | else | |
1963 | Rewrite (N, | |
1964 | OK_Convert_To (Typ, Relocate_Node (Pref))); | |
1965 | end if; | |
1966 | ||
1967 | Set_Etype (N, Typ); | |
1968 | Analyze_And_Resolve (N, Typ); | |
ee6ba406 | 1969 | end Enum_Rep; |
1970 | ||
d55c93e0 | 1971 | -------------- |
1972 | -- Enum_Val -- | |
1973 | -------------- | |
1974 | ||
1975 | when Attribute_Enum_Val => Enum_Val : declare | |
1976 | Expr : Node_Id; | |
1977 | Btyp : constant Entity_Id := Base_Type (Ptyp); | |
1978 | ||
1979 | begin | |
1980 | -- X'Enum_Val (Y) expands to | |
1981 | ||
1982 | -- [constraint_error when _rep_to_pos (Y, False) = -1, msg] | |
1983 | -- X!(Y); | |
1984 | ||
1985 | Expr := Unchecked_Convert_To (Ptyp, First (Exprs)); | |
1986 | ||
1987 | Insert_Action (N, | |
1988 | Make_Raise_Constraint_Error (Loc, | |
1989 | Condition => | |
1990 | Make_Op_Eq (Loc, | |
1991 | Left_Opnd => | |
1992 | Make_Function_Call (Loc, | |
1993 | Name => | |
1994 | New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc), | |
1995 | Parameter_Associations => New_List ( | |
1996 | Relocate_Node (Duplicate_Subexpr (Expr)), | |
1997 | New_Occurrence_Of (Standard_False, Loc))), | |
1998 | ||
1999 | Right_Opnd => Make_Integer_Literal (Loc, -1)), | |
2000 | Reason => CE_Range_Check_Failed)); | |
2001 | ||
2002 | Rewrite (N, Expr); | |
2003 | Analyze_And_Resolve (N, Ptyp); | |
2004 | end Enum_Val; | |
2005 | ||
ee6ba406 | 2006 | -------------- |
2007 | -- Exponent -- | |
2008 | -------------- | |
2009 | ||
2010 | -- Transforms 'Exponent into a call to the floating-point attribute | |
2011 | -- function Exponent in Fat_xxx (where xxx is the root type) | |
2012 | ||
2013 | when Attribute_Exponent => | |
2014 | Expand_Fpt_Attribute_R (N); | |
2015 | ||
2016 | ------------------ | |
2017 | -- External_Tag -- | |
2018 | ------------------ | |
2019 | ||
2020 | -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag) | |
2021 | ||
2022 | when Attribute_External_Tag => External_Tag : | |
2023 | begin | |
2024 | Rewrite (N, | |
2025 | Make_Function_Call (Loc, | |
2026 | Name => New_Reference_To (RTE (RE_External_Tag), Loc), | |
2027 | Parameter_Associations => New_List ( | |
2028 | Make_Attribute_Reference (Loc, | |
2029 | Attribute_Name => Name_Tag, | |
2030 | Prefix => Prefix (N))))); | |
2031 | ||
2032 | Analyze_And_Resolve (N, Standard_String); | |
2033 | end External_Tag; | |
2034 | ||
2035 | ----------- | |
2036 | -- First -- | |
2037 | ----------- | |
2038 | ||
d55c93e0 | 2039 | when Attribute_First => |
ee6ba406 | 2040 | |
ee6ba406 | 2041 | -- If the prefix type is a constrained packed array type which |
2042 | -- already has a Packed_Array_Type representation defined, then | |
2043 | -- replace this attribute with a direct reference to 'First of the | |
d55c93e0 | 2044 | -- appropriate index subtype (since otherwise the back end will try |
2045 | -- to give us the value of 'First for this implementation type). | |
ee6ba406 | 2046 | |
2047 | if Is_Constrained_Packed_Array (Ptyp) then | |
2048 | Rewrite (N, | |
2049 | Make_Attribute_Reference (Loc, | |
2050 | Attribute_Name => Name_First, | |
2051 | Prefix => New_Reference_To (Get_Index_Subtype (N), Loc))); | |
2052 | Analyze_And_Resolve (N, Typ); | |
2053 | ||
2054 | elsif Is_Access_Type (Ptyp) then | |
2055 | Apply_Access_Check (N); | |
2056 | end if; | |
ee6ba406 | 2057 | |
2058 | --------------- | |
2059 | -- First_Bit -- | |
2060 | --------------- | |
2061 | ||
d55c93e0 | 2062 | -- Compute this if component clause was present, otherwise we leave the |
2063 | -- computation to be completed in the back-end, since we don't know what | |
ee6ba406 | 2064 | -- layout will be chosen. |
2065 | ||
d55c93e0 | 2066 | when Attribute_First_Bit => First_Bit : declare |
ee6ba406 | 2067 | CE : constant Entity_Id := Entity (Selector_Name (Pref)); |
2068 | ||
2069 | begin | |
2070 | if Known_Static_Component_Bit_Offset (CE) then | |
2071 | Rewrite (N, | |
2072 | Make_Integer_Literal (Loc, | |
2073 | Component_Bit_Offset (CE) mod System_Storage_Unit)); | |
2074 | ||
2075 | Analyze_And_Resolve (N, Typ); | |
2076 | ||
2077 | else | |
2078 | Apply_Universal_Integer_Attribute_Checks (N); | |
2079 | end if; | |
2080 | end First_Bit; | |
2081 | ||
2082 | ----------------- | |
2083 | -- Fixed_Value -- | |
2084 | ----------------- | |
2085 | ||
2086 | -- We transform: | |
2087 | ||
2088 | -- fixtype'Fixed_Value (integer-value) | |
2089 | ||
2090 | -- into | |
2091 | ||
2092 | -- fixtype(integer-value) | |
2093 | ||
d55c93e0 | 2094 | -- We do all the required analysis of the conversion here, because we do |
2095 | -- not want this to go through the fixed-point conversion circuits. Note | |
2096 | -- that the back end always treats fixed-point as equivalent to the | |
2097 | -- corresponding integer type anyway. | |
ee6ba406 | 2098 | |
2099 | when Attribute_Fixed_Value => Fixed_Value : | |
2100 | begin | |
2101 | Rewrite (N, | |
2102 | Make_Type_Conversion (Loc, | |
2103 | Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc), | |
2104 | Expression => Relocate_Node (First (Exprs)))); | |
2105 | Set_Etype (N, Entity (Pref)); | |
2106 | Set_Analyzed (N); | |
9dfe12ae | 2107 | |
2108 | -- Note: it might appear that a properly analyzed unchecked conversion | |
2109 | -- would be just fine here, but that's not the case, since the full | |
2110 | -- range checks performed by the following call are critical! | |
2111 | ||
ee6ba406 | 2112 | Apply_Type_Conversion_Checks (N); |
2113 | end Fixed_Value; | |
2114 | ||
2115 | ----------- | |
2116 | -- Floor -- | |
2117 | ----------- | |
2118 | ||
2119 | -- Transforms 'Floor into a call to the floating-point attribute | |
2120 | -- function Floor in Fat_xxx (where xxx is the root type) | |
2121 | ||
2122 | when Attribute_Floor => | |
2123 | Expand_Fpt_Attribute_R (N); | |
2124 | ||
2125 | ---------- | |
2126 | -- Fore -- | |
2127 | ---------- | |
2128 | ||
2129 | -- For the fixed-point type Typ: | |
2130 | ||
2131 | -- Typ'Fore | |
2132 | ||
2133 | -- expands into | |
2134 | ||
1550b445 | 2135 | -- Result_Type (System.Fore (Universal_Real (Type'First)), |
2136 | -- Universal_Real (Type'Last)) | |
ee6ba406 | 2137 | |
2138 | -- Note that we know that the type is a non-static subtype, or Fore | |
2139 | -- would have itself been computed dynamically in Eval_Attribute. | |
2140 | ||
d55c93e0 | 2141 | when Attribute_Fore => Fore : begin |
ee6ba406 | 2142 | Rewrite (N, |
2143 | Convert_To (Typ, | |
2144 | Make_Function_Call (Loc, | |
2145 | Name => New_Reference_To (RTE (RE_Fore), Loc), | |
2146 | ||
2147 | Parameter_Associations => New_List ( | |
1550b445 | 2148 | Convert_To (Universal_Real, |
ee6ba406 | 2149 | Make_Attribute_Reference (Loc, |
2150 | Prefix => New_Reference_To (Ptyp, Loc), | |
2151 | Attribute_Name => Name_First)), | |
2152 | ||
1550b445 | 2153 | Convert_To (Universal_Real, |
ee6ba406 | 2154 | Make_Attribute_Reference (Loc, |
2155 | Prefix => New_Reference_To (Ptyp, Loc), | |
2156 | Attribute_Name => Name_Last)))))); | |
2157 | ||
2158 | Analyze_And_Resolve (N, Typ); | |
2159 | end Fore; | |
2160 | ||
2161 | -------------- | |
2162 | -- Fraction -- | |
2163 | -------------- | |
2164 | ||
2165 | -- Transforms 'Fraction into a call to the floating-point attribute | |
2166 | -- function Fraction in Fat_xxx (where xxx is the root type) | |
2167 | ||
2168 | when Attribute_Fraction => | |
2169 | Expand_Fpt_Attribute_R (N); | |
2170 | ||
5690e662 | 2171 | -------------- |
2172 | -- From_Any -- | |
2173 | -------------- | |
2174 | ||
2175 | when Attribute_From_Any => From_Any : declare | |
2176 | P_Type : constant Entity_Id := Etype (Pref); | |
2177 | Decls : constant List_Id := New_List; | |
2178 | begin | |
2179 | Rewrite (N, | |
2180 | Build_From_Any_Call (P_Type, | |
2181 | Relocate_Node (First (Exprs)), | |
2182 | Decls)); | |
2183 | Insert_Actions (N, Decls); | |
2184 | Analyze_And_Resolve (N, P_Type); | |
2185 | end From_Any; | |
2186 | ||
ee6ba406 | 2187 | -------------- |
2188 | -- Identity -- | |
2189 | -------------- | |
2190 | ||
2191 | -- For an exception returns a reference to the exception data: | |
2192 | -- Exception_Id!(Prefix'Reference) | |
2193 | ||
2194 | -- For a task it returns a reference to the _task_id component of | |
2195 | -- corresponding record: | |
2196 | ||
7f9be362 | 2197 | -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined |
ee6ba406 | 2198 | |
aad6babd | 2199 | -- in Ada.Task_Identification |
ee6ba406 | 2200 | |
2201 | when Attribute_Identity => Identity : declare | |
2202 | Id_Kind : Entity_Id; | |
2203 | ||
2204 | begin | |
d55c93e0 | 2205 | if Ptyp = Standard_Exception_Type then |
ee6ba406 | 2206 | Id_Kind := RTE (RE_Exception_Id); |
2207 | ||
2208 | if Present (Renamed_Object (Entity (Pref))) then | |
2209 | Set_Entity (Pref, Renamed_Object (Entity (Pref))); | |
2210 | end if; | |
2211 | ||
2212 | Rewrite (N, | |
2213 | Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref))); | |
2214 | else | |
7f9be362 | 2215 | Id_Kind := RTE (RO_AT_Task_Id); |
ee6ba406 | 2216 | |
f0bf2ff3 | 2217 | -- If the prefix is a task interface, the Task_Id is obtained |
2218 | -- dynamically through a dispatching call, as for other task | |
2219 | -- attributes applied to interfaces. | |
2220 | ||
de54c5ab | 2221 | if Ada_Version >= Ada_2005 |
d55c93e0 | 2222 | and then Ekind (Ptyp) = E_Class_Wide_Type |
2223 | and then Is_Interface (Ptyp) | |
2224 | and then Is_Task_Interface (Ptyp) | |
f0bf2ff3 | 2225 | then |
2226 | Rewrite (N, | |
2227 | Unchecked_Convert_To (Id_Kind, | |
2228 | Make_Selected_Component (Loc, | |
2229 | Prefix => | |
2230 | New_Copy_Tree (Pref), | |
2231 | Selector_Name => | |
2232 | Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))); | |
2233 | ||
2234 | else | |
2235 | Rewrite (N, | |
2236 | Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref))); | |
2237 | end if; | |
ee6ba406 | 2238 | end if; |
2239 | ||
2240 | Analyze_And_Resolve (N, Id_Kind); | |
2241 | end Identity; | |
2242 | ||
2243 | ----------- | |
2244 | -- Image -- | |
2245 | ----------- | |
2246 | ||
2247 | -- Image attribute is handled in separate unit Exp_Imgv | |
2248 | ||
2249 | when Attribute_Image => | |
2250 | Exp_Imgv.Expand_Image_Attribute (N); | |
2251 | ||
2252 | --------- | |
2253 | -- Img -- | |
2254 | --------- | |
2255 | ||
2256 | -- X'Img is expanded to typ'Image (X), where typ is the type of X | |
2257 | ||
2258 | when Attribute_Img => Img : | |
2259 | begin | |
2260 | Rewrite (N, | |
2261 | Make_Attribute_Reference (Loc, | |
d55c93e0 | 2262 | Prefix => New_Reference_To (Ptyp, Loc), |
ee6ba406 | 2263 | Attribute_Name => Name_Image, |
2264 | Expressions => New_List (Relocate_Node (Pref)))); | |
2265 | ||
2266 | Analyze_And_Resolve (N, Standard_String); | |
2267 | end Img; | |
2268 | ||
2269 | ----------- | |
2270 | -- Input -- | |
2271 | ----------- | |
2272 | ||
2273 | when Attribute_Input => Input : declare | |
2274 | P_Type : constant Entity_Id := Entity (Pref); | |
2275 | B_Type : constant Entity_Id := Base_Type (P_Type); | |
2276 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
2277 | Strm : constant Node_Id := First (Exprs); | |
2278 | Fname : Entity_Id; | |
2279 | Decl : Node_Id; | |
2280 | Call : Node_Id; | |
2281 | Prag : Node_Id; | |
2282 | Arg2 : Node_Id; | |
2283 | Rfunc : Node_Id; | |
2284 | ||
2285 | Cntrl : Node_Id := Empty; | |
2286 | -- Value for controlling argument in call. Always Empty except in | |
2287 | -- the dispatching (class-wide type) case, where it is a reference | |
2288 | -- to the dummy object initialized to the right internal tag. | |
2289 | ||
d53a018a | 2290 | procedure Freeze_Stream_Subprogram (F : Entity_Id); |
2291 | -- The expansion of the attribute reference may generate a call to | |
2292 | -- a user-defined stream subprogram that is frozen by the call. This | |
2293 | -- can lead to access-before-elaboration problem if the reference | |
2294 | -- appears in an object declaration and the subprogram body has not | |
2295 | -- been seen. The freezing of the subprogram requires special code | |
2296 | -- because it appears in an expanded context where expressions do | |
2297 | -- not freeze their constituents. | |
2298 | ||
2299 | ------------------------------ | |
2300 | -- Freeze_Stream_Subprogram -- | |
2301 | ------------------------------ | |
2302 | ||
2303 | procedure Freeze_Stream_Subprogram (F : Entity_Id) is | |
2304 | Decl : constant Node_Id := Unit_Declaration_Node (F); | |
2305 | Bod : Node_Id; | |
2306 | ||
2307 | begin | |
2308 | -- If this is user-defined subprogram, the corresponding | |
2309 | -- stream function appears as a renaming-as-body, and the | |
2310 | -- user subprogram must be retrieved by tree traversal. | |
2311 | ||
2312 | if Present (Decl) | |
2313 | and then Nkind (Decl) = N_Subprogram_Declaration | |
2314 | and then Present (Corresponding_Body (Decl)) | |
2315 | then | |
2316 | Bod := Corresponding_Body (Decl); | |
2317 | ||
2318 | if Nkind (Unit_Declaration_Node (Bod)) = | |
2319 | N_Subprogram_Renaming_Declaration | |
2320 | then | |
2321 | Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod)))); | |
2322 | end if; | |
2323 | end if; | |
2324 | end Freeze_Stream_Subprogram; | |
2325 | ||
2326 | -- Start of processing for Input | |
2327 | ||
ee6ba406 | 2328 | begin |
2329 | -- If no underlying type, we have an error that will be diagnosed | |
2330 | -- elsewhere, so here we just completely ignore the expansion. | |
2331 | ||
2332 | if No (U_Type) then | |
2333 | return; | |
2334 | end if; | |
2335 | ||
2336 | -- If there is a TSS for Input, just call it | |
2337 | ||
9dfe12ae | 2338 | Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input); |
ee6ba406 | 2339 | |
2340 | if Present (Fname) then | |
2341 | null; | |
2342 | ||
2343 | else | |
2344 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
2345 | ||
2346 | -- sourcetyp'Input (stream) | |
2347 | ||
2348 | -- as | |
2349 | ||
2350 | -- sourcetyp (streamread (strmtyp'Input (stream))); | |
2351 | ||
2c145f84 | 2352 | -- where streamread is the given Read function that converts an |
d55c93e0 | 2353 | -- argument of type strmtyp to type sourcetyp or a type from which |
2354 | -- it is derived (extra conversion required for the derived case). | |
ee6ba406 | 2355 | |
5245b786 | 2356 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 2357 | |
2358 | if Present (Prag) then | |
2359 | Arg2 := Next (First (Pragma_Argument_Associations (Prag))); | |
2360 | Rfunc := Entity (Expression (Arg2)); | |
2361 | ||
2362 | Rewrite (N, | |
2363 | Convert_To (B_Type, | |
2364 | Make_Function_Call (Loc, | |
2365 | Name => New_Occurrence_Of (Rfunc, Loc), | |
2366 | Parameter_Associations => New_List ( | |
2367 | Make_Attribute_Reference (Loc, | |
2368 | Prefix => | |
2369 | New_Occurrence_Of | |
2370 | (Etype (First_Formal (Rfunc)), Loc), | |
2371 | Attribute_Name => Name_Input, | |
2372 | Expressions => Exprs))))); | |
2373 | ||
2374 | Analyze_And_Resolve (N, B_Type); | |
2375 | return; | |
2376 | ||
2377 | -- Elementary types | |
2378 | ||
2379 | elsif Is_Elementary_Type (U_Type) then | |
2380 | ||
2381 | -- A special case arises if we have a defined _Read routine, | |
2382 | -- since in this case we are required to call this routine. | |
2383 | ||
9dfe12ae | 2384 | if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then |
ee6ba406 | 2385 | Build_Record_Or_Elementary_Input_Function |
2386 | (Loc, U_Type, Decl, Fname); | |
2387 | Insert_Action (N, Decl); | |
2388 | ||
2389 | -- For normal cases, we call the I_xxx routine directly | |
2390 | ||
2391 | else | |
2392 | Rewrite (N, Build_Elementary_Input_Call (N)); | |
2393 | Analyze_And_Resolve (N, P_Type); | |
2394 | return; | |
2395 | end if; | |
2396 | ||
2397 | -- Array type case | |
2398 | ||
2399 | elsif Is_Array_Type (U_Type) then | |
2400 | Build_Array_Input_Function (Loc, U_Type, Decl, Fname); | |
2401 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
2402 | ||
2403 | -- Dispatching case with class-wide type | |
2404 | ||
2405 | elsif Is_Class_Wide_Type (P_Type) then | |
2406 | ||
99f2248e | 2407 | -- No need to do anything else compiling under restriction |
2408 | -- No_Dispatching_Calls. During the semantic analysis we | |
2409 | -- already notified such violation. | |
2410 | ||
2411 | if Restriction_Active (No_Dispatching_Calls) then | |
2412 | return; | |
2413 | end if; | |
2414 | ||
ee6ba406 | 2415 | declare |
2416 | Rtyp : constant Entity_Id := Root_Type (P_Type); | |
2417 | Dnn : Entity_Id; | |
2418 | Decl : Node_Id; | |
46eb6933 | 2419 | Expr : Node_Id; |
ee6ba406 | 2420 | |
2421 | begin | |
2422 | -- Read the internal tag (RM 13.13.2(34)) and use it to | |
2423 | -- initialize a dummy tag object: | |
2424 | ||
46eb6933 | 2425 | -- Dnn : Ada.Tags.Tag := |
2426 | -- Descendant_Tag (String'Input (Strm), P_Type); | |
ee6ba406 | 2427 | |
2428 | -- This dummy object is used only to provide a controlling | |
aad6babd | 2429 | -- argument for the eventual _Input call. Descendant_Tag is |
2430 | -- called rather than Internal_Tag to ensure that we have a | |
2431 | -- tag for a type that is descended from the prefix type and | |
2432 | -- declared at the same accessibility level (the exception | |
2433 | -- Tag_Error will be raised otherwise). The level check is | |
2434 | -- required for Ada 2005 because tagged types can be | |
2435 | -- extended in nested scopes (AI-344). | |
ee6ba406 | 2436 | |
46eb6933 | 2437 | Expr := |
2438 | Make_Function_Call (Loc, | |
2439 | Name => | |
2440 | New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc), | |
2441 | Parameter_Associations => New_List ( | |
2442 | Make_Attribute_Reference (Loc, | |
2443 | Prefix => New_Occurrence_Of (Standard_String, Loc), | |
2444 | Attribute_Name => Name_Input, | |
2445 | Expressions => New_List ( | |
2446 | Relocate_Node (Duplicate_Subexpr (Strm)))), | |
2447 | Make_Attribute_Reference (Loc, | |
2448 | Prefix => New_Reference_To (P_Type, Loc), | |
2449 | Attribute_Name => Name_Tag))); | |
2450 | ||
2451 | Dnn := Make_Temporary (Loc, 'D', Expr); | |
ee6ba406 | 2452 | |
2453 | Decl := | |
2454 | Make_Object_Declaration (Loc, | |
2455 | Defining_Identifier => Dnn, | |
46eb6933 | 2456 | Object_Definition => |
ee6ba406 | 2457 | New_Occurrence_Of (RTE (RE_Tag), Loc), |
46eb6933 | 2458 | Expression => Expr); |
ee6ba406 | 2459 | |
2460 | Insert_Action (N, Decl); | |
2461 | ||
2462 | -- Now we need to get the entity for the call, and construct | |
2463 | -- a function call node, where we preset a reference to Dnn | |
aad6babd | 2464 | -- as the controlling argument (doing an unchecked convert |
2465 | -- to the class-wide tagged type to make it look like a real | |
2466 | -- tagged object). | |
ee6ba406 | 2467 | |
9dfe12ae | 2468 | Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input); |
46eb6933 | 2469 | Cntrl := |
2470 | Unchecked_Convert_To (P_Type, | |
2471 | New_Occurrence_Of (Dnn, Loc)); | |
9dfe12ae | 2472 | Set_Etype (Cntrl, P_Type); |
ee6ba406 | 2473 | Set_Parent (Cntrl, N); |
2474 | end; | |
2475 | ||
2476 | -- For tagged types, use the primitive Input function | |
2477 | ||
2478 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 2479 | Fname := Find_Prim_Op (U_Type, TSS_Stream_Input); |
ee6ba406 | 2480 | |
aad6babd | 2481 | -- All other record type cases, including protected records. The |
2482 | -- latter only arise for expander generated code for handling | |
2483 | -- shared passive partition access. | |
ee6ba406 | 2484 | |
2485 | else | |
2486 | pragma Assert | |
2487 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
2488 | ||
d55c93e0 | 2489 | -- Ada 2005 (AI-216): Program_Error is raised executing default |
2490 | -- implementation of the Input attribute of an unchecked union | |
2491 | -- type if the type lacks default discriminant values. | |
00f91aef | 2492 | |
2493 | if Is_Unchecked_Union (Base_Type (U_Type)) | |
18a40e97 | 2494 | and then No (Discriminant_Constraint (U_Type)) |
00f91aef | 2495 | then |
2496 | Insert_Action (N, | |
2497 | Make_Raise_Program_Error (Loc, | |
2498 | Reason => PE_Unchecked_Union_Restriction)); | |
2499 | ||
2500 | return; | |
2501 | end if; | |
2502 | ||
ee6ba406 | 2503 | Build_Record_Or_Elementary_Input_Function |
2504 | (Loc, Base_Type (U_Type), Decl, Fname); | |
2505 | Insert_Action (N, Decl); | |
d53a018a | 2506 | |
2507 | if Nkind (Parent (N)) = N_Object_Declaration | |
2508 | and then Is_Record_Type (U_Type) | |
2509 | then | |
2510 | -- The stream function may contain calls to user-defined | |
2511 | -- Read procedures for individual components. | |
2512 | ||
2513 | declare | |
2514 | Comp : Entity_Id; | |
2515 | Func : Entity_Id; | |
2516 | ||
2517 | begin | |
2518 | Comp := First_Component (U_Type); | |
2519 | while Present (Comp) loop | |
2520 | Func := | |
2521 | Find_Stream_Subprogram | |
2522 | (Etype (Comp), TSS_Stream_Read); | |
2523 | ||
2524 | if Present (Func) then | |
2525 | Freeze_Stream_Subprogram (Func); | |
2526 | end if; | |
2527 | ||
2528 | Next_Component (Comp); | |
2529 | end loop; | |
2530 | end; | |
2531 | end if; | |
ee6ba406 | 2532 | end if; |
2533 | end if; | |
2534 | ||
aad6babd | 2535 | -- If we fall through, Fname is the function to be called. The result |
2536 | -- is obtained by calling the appropriate function, then converting | |
2537 | -- the result. The conversion does a subtype check. | |
ee6ba406 | 2538 | |
2539 | Call := | |
2540 | Make_Function_Call (Loc, | |
2541 | Name => New_Occurrence_Of (Fname, Loc), | |
2542 | Parameter_Associations => New_List ( | |
2543 | Relocate_Node (Strm))); | |
2544 | ||
2545 | Set_Controlling_Argument (Call, Cntrl); | |
2546 | Rewrite (N, Unchecked_Convert_To (P_Type, Call)); | |
2547 | Analyze_And_Resolve (N, P_Type); | |
d53a018a | 2548 | |
2549 | if Nkind (Parent (N)) = N_Object_Declaration then | |
2550 | Freeze_Stream_Subprogram (Fname); | |
2551 | end if; | |
ee6ba406 | 2552 | end Input; |
2553 | ||
2554 | ------------------- | |
2555 | -- Integer_Value -- | |
2556 | ------------------- | |
2557 | ||
2558 | -- We transform | |
2559 | ||
2560 | -- inttype'Fixed_Value (fixed-value) | |
2561 | ||
2562 | -- into | |
2563 | ||
2564 | -- inttype(integer-value)) | |
2565 | ||
d55c93e0 | 2566 | -- we do all the required analysis of the conversion here, because we do |
2567 | -- not want this to go through the fixed-point conversion circuits. Note | |
2568 | -- that the back end always treats fixed-point as equivalent to the | |
2569 | -- corresponding integer type anyway. | |
ee6ba406 | 2570 | |
2571 | when Attribute_Integer_Value => Integer_Value : | |
2572 | begin | |
2573 | Rewrite (N, | |
2574 | Make_Type_Conversion (Loc, | |
2575 | Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc), | |
2576 | Expression => Relocate_Node (First (Exprs)))); | |
2577 | Set_Etype (N, Entity (Pref)); | |
2578 | Set_Analyzed (N); | |
9dfe12ae | 2579 | |
2580 | -- Note: it might appear that a properly analyzed unchecked conversion | |
2581 | -- would be just fine here, but that's not the case, since the full | |
2582 | -- range checks performed by the following call are critical! | |
2583 | ||
ee6ba406 | 2584 | Apply_Type_Conversion_Checks (N); |
2585 | end Integer_Value; | |
2586 | ||
d55c93e0 | 2587 | ------------------- |
2588 | -- Invalid_Value -- | |
2589 | ------------------- | |
2590 | ||
2591 | when Attribute_Invalid_Value => | |
2592 | Rewrite (N, Get_Simple_Init_Val (Ptyp, N)); | |
2593 | ||
ee6ba406 | 2594 | ---------- |
2595 | -- Last -- | |
2596 | ---------- | |
2597 | ||
d55c93e0 | 2598 | when Attribute_Last => |
ee6ba406 | 2599 | |
ee6ba406 | 2600 | -- If the prefix type is a constrained packed array type which |
2601 | -- already has a Packed_Array_Type representation defined, then | |
2602 | -- replace this attribute with a direct reference to 'Last of the | |
d55c93e0 | 2603 | -- appropriate index subtype (since otherwise the back end will try |
2604 | -- to give us the value of 'Last for this implementation type). | |
ee6ba406 | 2605 | |
2606 | if Is_Constrained_Packed_Array (Ptyp) then | |
2607 | Rewrite (N, | |
2608 | Make_Attribute_Reference (Loc, | |
2609 | Attribute_Name => Name_Last, | |
2610 | Prefix => New_Reference_To (Get_Index_Subtype (N), Loc))); | |
2611 | Analyze_And_Resolve (N, Typ); | |
2612 | ||
2613 | elsif Is_Access_Type (Ptyp) then | |
2614 | Apply_Access_Check (N); | |
2615 | end if; | |
ee6ba406 | 2616 | |
2617 | -------------- | |
2618 | -- Last_Bit -- | |
2619 | -------------- | |
2620 | ||
d55c93e0 | 2621 | -- We compute this if a component clause was present, otherwise we leave |
2622 | -- the computation up to the back end, since we don't know what layout | |
2623 | -- will be chosen. | |
ee6ba406 | 2624 | |
d55c93e0 | 2625 | when Attribute_Last_Bit => Last_Bit : declare |
ee6ba406 | 2626 | CE : constant Entity_Id := Entity (Selector_Name (Pref)); |
2627 | ||
2628 | begin | |
2629 | if Known_Static_Component_Bit_Offset (CE) | |
2630 | and then Known_Static_Esize (CE) | |
2631 | then | |
2632 | Rewrite (N, | |
2633 | Make_Integer_Literal (Loc, | |
2634 | Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit) | |
2635 | + Esize (CE) - 1)); | |
2636 | ||
2637 | Analyze_And_Resolve (N, Typ); | |
2638 | ||
2639 | else | |
2640 | Apply_Universal_Integer_Attribute_Checks (N); | |
2641 | end if; | |
2642 | end Last_Bit; | |
2643 | ||
2644 | ------------------ | |
2645 | -- Leading_Part -- | |
2646 | ------------------ | |
2647 | ||
2648 | -- Transforms 'Leading_Part into a call to the floating-point attribute | |
2649 | -- function Leading_Part in Fat_xxx (where xxx is the root type) | |
2650 | ||
d55c93e0 | 2651 | -- Note: strictly, we should generate special case code to deal with |
aad6babd | 2652 | -- absurdly large positive arguments (greater than Integer'Last), which |
2653 | -- result in returning the first argument unchanged, but it hardly seems | |
2654 | -- worth the effort. We raise constraint error for absurdly negative | |
2655 | -- arguments which is fine. | |
ee6ba406 | 2656 | |
2657 | when Attribute_Leading_Part => | |
2658 | Expand_Fpt_Attribute_RI (N); | |
2659 | ||
2660 | ------------ | |
2661 | -- Length -- | |
2662 | ------------ | |
2663 | ||
2664 | when Attribute_Length => declare | |
ee6ba406 | 2665 | Ityp : Entity_Id; |
2666 | Xnum : Uint; | |
2667 | ||
2668 | begin | |
2669 | -- Processing for packed array types | |
2670 | ||
2671 | if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then | |
2672 | Ityp := Get_Index_Subtype (N); | |
2673 | ||
d55c93e0 | 2674 | -- If the index type, Ityp, is an enumeration type with holes, |
2675 | -- then we calculate X'Length explicitly using | |
ee6ba406 | 2676 | |
2677 | -- Typ'Max | |
2678 | -- (0, Ityp'Pos (X'Last (N)) - | |
2679 | -- Ityp'Pos (X'First (N)) + 1); | |
2680 | ||
d55c93e0 | 2681 | -- Since the bounds in the template are the representation values |
2682 | -- and the back end would get the wrong value. | |
ee6ba406 | 2683 | |
2684 | if Is_Enumeration_Type (Ityp) | |
2685 | and then Present (Enum_Pos_To_Rep (Base_Type (Ityp))) | |
2686 | then | |
2687 | if No (Exprs) then | |
2688 | Xnum := Uint_1; | |
2689 | else | |
2690 | Xnum := Expr_Value (First (Expressions (N))); | |
2691 | end if; | |
2692 | ||
2693 | Rewrite (N, | |
2694 | Make_Attribute_Reference (Loc, | |
2695 | Prefix => New_Occurrence_Of (Typ, Loc), | |
2696 | Attribute_Name => Name_Max, | |
2697 | Expressions => New_List | |
2698 | (Make_Integer_Literal (Loc, 0), | |
2699 | ||
2700 | Make_Op_Add (Loc, | |
2701 | Left_Opnd => | |
2702 | Make_Op_Subtract (Loc, | |
2703 | Left_Opnd => | |
2704 | Make_Attribute_Reference (Loc, | |
2705 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2706 | Attribute_Name => Name_Pos, | |
2707 | ||
2708 | Expressions => New_List ( | |
2709 | Make_Attribute_Reference (Loc, | |
2710 | Prefix => Duplicate_Subexpr (Pref), | |
2711 | Attribute_Name => Name_Last, | |
2712 | Expressions => New_List ( | |
2713 | Make_Integer_Literal (Loc, Xnum))))), | |
2714 | ||
2715 | Right_Opnd => | |
2716 | Make_Attribute_Reference (Loc, | |
2717 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2718 | Attribute_Name => Name_Pos, | |
2719 | ||
2720 | Expressions => New_List ( | |
2721 | Make_Attribute_Reference (Loc, | |
9dfe12ae | 2722 | Prefix => |
2723 | Duplicate_Subexpr_No_Checks (Pref), | |
ee6ba406 | 2724 | Attribute_Name => Name_First, |
2725 | Expressions => New_List ( | |
2726 | Make_Integer_Literal (Loc, Xnum)))))), | |
2727 | ||
2728 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); | |
2729 | ||
2730 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
2731 | return; | |
2732 | ||
2733 | -- If the prefix type is a constrained packed array type which | |
2734 | -- already has a Packed_Array_Type representation defined, then | |
2735 | -- replace this attribute with a direct reference to 'Range_Length | |
d55c93e0 | 2736 | -- of the appropriate index subtype (since otherwise the back end |
2737 | -- will try to give us the value of 'Length for this | |
2738 | -- implementation type). | |
ee6ba406 | 2739 | |
2740 | elsif Is_Constrained (Ptyp) then | |
2741 | Rewrite (N, | |
2742 | Make_Attribute_Reference (Loc, | |
2743 | Attribute_Name => Name_Range_Length, | |
2744 | Prefix => New_Reference_To (Ityp, Loc))); | |
2745 | Analyze_And_Resolve (N, Typ); | |
2746 | end if; | |
2747 | ||
ee6ba406 | 2748 | -- Access type case |
2749 | ||
2750 | elsif Is_Access_Type (Ptyp) then | |
2751 | Apply_Access_Check (N); | |
2752 | ||
d55c93e0 | 2753 | -- If the designated type is a packed array type, then we convert |
2754 | -- the reference to: | |
ee6ba406 | 2755 | |
2756 | -- typ'Max (0, 1 + | |
2757 | -- xtyp'Pos (Pref'Last (Expr)) - | |
2758 | -- xtyp'Pos (Pref'First (Expr))); | |
2759 | ||
d55c93e0 | 2760 | -- This is a bit complex, but it is the easiest thing to do that |
2761 | -- works in all cases including enum types with holes xtyp here | |
2762 | -- is the appropriate index type. | |
ee6ba406 | 2763 | |
2764 | declare | |
2765 | Dtyp : constant Entity_Id := Designated_Type (Ptyp); | |
2766 | Xtyp : Entity_Id; | |
2767 | ||
2768 | begin | |
2769 | if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then | |
2770 | Xtyp := Get_Index_Subtype (N); | |
2771 | ||
2772 | Rewrite (N, | |
2773 | Make_Attribute_Reference (Loc, | |
2774 | Prefix => New_Occurrence_Of (Typ, Loc), | |
2775 | Attribute_Name => Name_Max, | |
2776 | Expressions => New_List ( | |
2777 | Make_Integer_Literal (Loc, 0), | |
2778 | ||
2779 | Make_Op_Add (Loc, | |
2780 | Make_Integer_Literal (Loc, 1), | |
2781 | Make_Op_Subtract (Loc, | |
2782 | Left_Opnd => | |
2783 | Make_Attribute_Reference (Loc, | |
2784 | Prefix => New_Occurrence_Of (Xtyp, Loc), | |
2785 | Attribute_Name => Name_Pos, | |
2786 | Expressions => New_List ( | |
2787 | Make_Attribute_Reference (Loc, | |
2788 | Prefix => Duplicate_Subexpr (Pref), | |
2789 | Attribute_Name => Name_Last, | |
2790 | Expressions => | |
2791 | New_Copy_List (Exprs)))), | |
2792 | ||
2793 | Right_Opnd => | |
2794 | Make_Attribute_Reference (Loc, | |
2795 | Prefix => New_Occurrence_Of (Xtyp, Loc), | |
2796 | Attribute_Name => Name_Pos, | |
2797 | Expressions => New_List ( | |
2798 | Make_Attribute_Reference (Loc, | |
9dfe12ae | 2799 | Prefix => |
2800 | Duplicate_Subexpr_No_Checks (Pref), | |
ee6ba406 | 2801 | Attribute_Name => Name_First, |
2802 | Expressions => | |
2803 | New_Copy_List (Exprs))))))))); | |
2804 | ||
2805 | Analyze_And_Resolve (N, Typ); | |
2806 | end if; | |
2807 | end; | |
2808 | ||
d55c93e0 | 2809 | -- Otherwise leave it to the back end |
ee6ba406 | 2810 | |
2811 | else | |
2812 | Apply_Universal_Integer_Attribute_Checks (N); | |
2813 | end if; | |
2814 | end; | |
2815 | ||
2816 | ------------- | |
2817 | -- Machine -- | |
2818 | ------------- | |
2819 | ||
2820 | -- Transforms 'Machine into a call to the floating-point attribute | |
2821 | -- function Machine in Fat_xxx (where xxx is the root type) | |
2822 | ||
2823 | when Attribute_Machine => | |
2824 | Expand_Fpt_Attribute_R (N); | |
2825 | ||
1550b445 | 2826 | ---------------------- |
2827 | -- Machine_Rounding -- | |
2828 | ---------------------- | |
2829 | ||
2830 | -- Transforms 'Machine_Rounding into a call to the floating-point | |
2831 | -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root | |
99f2248e | 2832 | -- type). Expansion is avoided for cases the back end can handle |
2833 | -- directly. | |
1550b445 | 2834 | |
2835 | when Attribute_Machine_Rounding => | |
99f2248e | 2836 | if not Is_Inline_Floating_Point_Attribute (N) then |
2837 | Expand_Fpt_Attribute_R (N); | |
2838 | end if; | |
1550b445 | 2839 | |
ee6ba406 | 2840 | ------------------ |
2841 | -- Machine_Size -- | |
2842 | ------------------ | |
2843 | ||
2844 | -- Machine_Size is equivalent to Object_Size, so transform it into | |
d55c93e0 | 2845 | -- Object_Size and that way the back end never sees Machine_Size. |
ee6ba406 | 2846 | |
2847 | when Attribute_Machine_Size => | |
2848 | Rewrite (N, | |
2849 | Make_Attribute_Reference (Loc, | |
2850 | Prefix => Prefix (N), | |
2851 | Attribute_Name => Name_Object_Size)); | |
2852 | ||
2853 | Analyze_And_Resolve (N, Typ); | |
2854 | ||
2855 | -------------- | |
2856 | -- Mantissa -- | |
2857 | -------------- | |
2858 | ||
aad6babd | 2859 | -- The only case that can get this far is the dynamic case of the old |
d55c93e0 | 2860 | -- Ada 83 Mantissa attribute for the fixed-point case. For this case, |
2861 | -- we expand: | |
ee6ba406 | 2862 | |
2863 | -- typ'Mantissa | |
2864 | ||
2865 | -- into | |
2866 | ||
2867 | -- ityp (System.Mantissa.Mantissa_Value | |
2868 | -- (Integer'Integer_Value (typ'First), | |
2869 | -- Integer'Integer_Value (typ'Last))); | |
2870 | ||
d55c93e0 | 2871 | when Attribute_Mantissa => Mantissa : begin |
ee6ba406 | 2872 | Rewrite (N, |
2873 | Convert_To (Typ, | |
2874 | Make_Function_Call (Loc, | |
2875 | Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc), | |
2876 | ||
2877 | Parameter_Associations => New_List ( | |
2878 | ||
2879 | Make_Attribute_Reference (Loc, | |
2880 | Prefix => New_Occurrence_Of (Standard_Integer, Loc), | |
2881 | Attribute_Name => Name_Integer_Value, | |
2882 | Expressions => New_List ( | |
2883 | ||
2884 | Make_Attribute_Reference (Loc, | |
2885 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
2886 | Attribute_Name => Name_First))), | |
2887 | ||
2888 | Make_Attribute_Reference (Loc, | |
2889 | Prefix => New_Occurrence_Of (Standard_Integer, Loc), | |
2890 | Attribute_Name => Name_Integer_Value, | |
2891 | Expressions => New_List ( | |
2892 | ||
2893 | Make_Attribute_Reference (Loc, | |
2894 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
2895 | Attribute_Name => Name_Last))))))); | |
2896 | ||
2897 | Analyze_And_Resolve (N, Typ); | |
2898 | end Mantissa; | |
2899 | ||
18a40e97 | 2900 | -------------------- |
2901 | -- Mechanism_Code -- | |
2902 | -------------------- | |
2903 | ||
2904 | when Attribute_Mechanism_Code => | |
2905 | ||
2906 | -- We must replace the prefix in the renamed case | |
2907 | ||
2908 | if Is_Entity_Name (Pref) | |
2909 | and then Present (Alias (Entity (Pref))) | |
2910 | then | |
2911 | Set_Renamed_Subprogram (Pref, Alias (Entity (Pref))); | |
2912 | end if; | |
2913 | ||
e0521a36 | 2914 | --------- |
2915 | -- Mod -- | |
2916 | --------- | |
2917 | ||
2918 | when Attribute_Mod => Mod_Case : declare | |
2919 | Arg : constant Node_Id := Relocate_Node (First (Exprs)); | |
2920 | Hi : constant Node_Id := Type_High_Bound (Etype (Arg)); | |
2921 | Modv : constant Uint := Modulus (Btyp); | |
2922 | ||
2923 | begin | |
2924 | ||
2925 | -- This is not so simple. The issue is what type to use for the | |
2926 | -- computation of the modular value. | |
2927 | ||
2928 | -- The easy case is when the modulus value is within the bounds | |
2929 | -- of the signed integer type of the argument. In this case we can | |
2930 | -- just do the computation in that signed integer type, and then | |
2931 | -- do an ordinary conversion to the target type. | |
2932 | ||
2933 | if Modv <= Expr_Value (Hi) then | |
2934 | Rewrite (N, | |
2935 | Convert_To (Btyp, | |
2936 | Make_Op_Mod (Loc, | |
2937 | Left_Opnd => Arg, | |
2938 | Right_Opnd => Make_Integer_Literal (Loc, Modv)))); | |
2939 | ||
2940 | -- Here we know that the modulus is larger than type'Last of the | |
9eb397d8 | 2941 | -- integer type. There are two cases to consider: |
e0521a36 | 2942 | |
2943 | -- a) The integer value is non-negative. In this case, it is | |
2944 | -- returned as the result (since it is less than the modulus). | |
2945 | ||
aad6babd | 2946 | -- b) The integer value is negative. In this case, we know that the |
2947 | -- result is modulus + value, where the value might be as small as | |
2948 | -- -modulus. The trouble is what type do we use to do the subtract. | |
2949 | -- No type will do, since modulus can be as big as 2**64, and no | |
2c145f84 | 2950 | -- integer type accommodates this value. Let's do bit of algebra |
e0521a36 | 2951 | |
2952 | -- modulus + value | |
2953 | -- = modulus - (-value) | |
2954 | -- = (modulus - 1) - (-value - 1) | |
2955 | ||
2956 | -- Now modulus - 1 is certainly in range of the modular type. | |
2957 | -- -value is in the range 1 .. modulus, so -value -1 is in the | |
2958 | -- range 0 .. modulus-1 which is in range of the modular type. | |
2959 | -- Furthermore, (-value - 1) can be expressed as -(value + 1) | |
2960 | -- which we can compute using the integer base type. | |
2961 | ||
9eb397d8 | 2962 | -- Once this is done we analyze the conditional expression without |
2963 | -- range checks, because we know everything is in range, and we | |
2964 | -- want to prevent spurious warnings on either branch. | |
2965 | ||
e0521a36 | 2966 | else |
2967 | Rewrite (N, | |
2968 | Make_Conditional_Expression (Loc, | |
2969 | Expressions => New_List ( | |
2970 | Make_Op_Ge (Loc, | |
2971 | Left_Opnd => Duplicate_Subexpr (Arg), | |
2972 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
2973 | ||
2974 | Convert_To (Btyp, | |
2975 | Duplicate_Subexpr_No_Checks (Arg)), | |
2976 | ||
2977 | Make_Op_Subtract (Loc, | |
2978 | Left_Opnd => | |
2979 | Make_Integer_Literal (Loc, | |
2980 | Intval => Modv - 1), | |
2981 | Right_Opnd => | |
2982 | Convert_To (Btyp, | |
2983 | Make_Op_Minus (Loc, | |
2984 | Right_Opnd => | |
2985 | Make_Op_Add (Loc, | |
2986 | Left_Opnd => Duplicate_Subexpr_No_Checks (Arg), | |
2987 | Right_Opnd => | |
2988 | Make_Integer_Literal (Loc, | |
2989 | Intval => 1)))))))); | |
2990 | ||
e0521a36 | 2991 | end if; |
2992 | ||
1550b445 | 2993 | Analyze_And_Resolve (N, Btyp, Suppress => All_Checks); |
e0521a36 | 2994 | end Mod_Case; |
2995 | ||
ee6ba406 | 2996 | ----------- |
2997 | -- Model -- | |
2998 | ----------- | |
2999 | ||
3000 | -- Transforms 'Model into a call to the floating-point attribute | |
3001 | -- function Model in Fat_xxx (where xxx is the root type) | |
3002 | ||
3003 | when Attribute_Model => | |
3004 | Expand_Fpt_Attribute_R (N); | |
3005 | ||
3006 | ----------------- | |
3007 | -- Object_Size -- | |
3008 | ----------------- | |
3009 | ||
3010 | -- The processing for Object_Size shares the processing for Size | |
3011 | ||
4c06b9d2 | 3012 | --------- |
3013 | -- Old -- | |
3014 | --------- | |
3015 | ||
3016 | when Attribute_Old => Old : declare | |
46eb6933 | 3017 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', Pref); |
4c06b9d2 | 3018 | Subp : Node_Id; |
3019 | Asn_Stm : Node_Id; | |
3020 | ||
3021 | begin | |
d55c93e0 | 3022 | -- Find the nearest subprogram body, ignoring _Preconditions |
3023 | ||
4c06b9d2 | 3024 | Subp := N; |
3025 | loop | |
3026 | Subp := Parent (Subp); | |
d55c93e0 | 3027 | exit when Nkind (Subp) = N_Subprogram_Body |
3028 | and then Chars (Defining_Entity (Subp)) /= Name_uPostconditions; | |
4c06b9d2 | 3029 | end loop; |
3030 | ||
03a47ff8 | 3031 | -- Insert the initialized object declaration at the start of the |
3032 | -- subprogram's declarations. | |
d55c93e0 | 3033 | |
4c06b9d2 | 3034 | Asn_Stm := |
3035 | Make_Object_Declaration (Loc, | |
3036 | Defining_Identifier => Tnn, | |
3037 | Constant_Present => True, | |
3038 | Object_Definition => New_Occurrence_Of (Etype (N), Loc), | |
3039 | Expression => Pref); | |
3040 | ||
03a47ff8 | 3041 | -- Push the subprogram's scope, so that the object will be analyzed |
3042 | -- in that context (rather than the context of the Precondition | |
3043 | -- subprogram) and will have its Scope set properly. | |
3044 | ||
3045 | if Present (Corresponding_Spec (Subp)) then | |
3046 | Push_Scope (Corresponding_Spec (Subp)); | |
3047 | else | |
3048 | Push_Scope (Defining_Entity (Subp)); | |
3049 | end if; | |
3050 | ||
4c06b9d2 | 3051 | if Is_Empty_List (Declarations (Subp)) then |
3052 | Set_Declarations (Subp, New_List (Asn_Stm)); | |
3053 | Analyze (Asn_Stm); | |
3054 | else | |
3055 | Insert_Action (First (Declarations (Subp)), Asn_Stm); | |
3056 | end if; | |
3057 | ||
03a47ff8 | 3058 | Pop_Scope; |
3059 | ||
4c06b9d2 | 3060 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); |
3061 | end Old; | |
3062 | ||
ee6ba406 | 3063 | ------------ |
3064 | -- Output -- | |
3065 | ------------ | |
3066 | ||
3067 | when Attribute_Output => Output : declare | |
3068 | P_Type : constant Entity_Id := Entity (Pref); | |
ee6ba406 | 3069 | U_Type : constant Entity_Id := Underlying_Type (P_Type); |
3070 | Pname : Entity_Id; | |
3071 | Decl : Node_Id; | |
3072 | Prag : Node_Id; | |
3073 | Arg3 : Node_Id; | |
3074 | Wfunc : Node_Id; | |
3075 | ||
3076 | begin | |
3077 | -- If no underlying type, we have an error that will be diagnosed | |
3078 | -- elsewhere, so here we just completely ignore the expansion. | |
3079 | ||
3080 | if No (U_Type) then | |
3081 | return; | |
3082 | end if; | |
3083 | ||
3084 | -- If TSS for Output is present, just call it | |
3085 | ||
9dfe12ae | 3086 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output); |
ee6ba406 | 3087 | |
3088 | if Present (Pname) then | |
3089 | null; | |
3090 | ||
3091 | else | |
3092 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
3093 | ||
3094 | -- sourcetyp'Output (stream, Item) | |
3095 | ||
3096 | -- as | |
3097 | ||
3098 | -- strmtyp'Output (Stream, strmwrite (acttyp (Item))); | |
3099 | ||
aad6babd | 3100 | -- where strmwrite is the given Write function that converts an |
3101 | -- argument of type sourcetyp or a type acctyp, from which it is | |
3102 | -- derived to type strmtyp. The conversion to acttyp is required | |
3103 | -- for the derived case. | |
ee6ba406 | 3104 | |
5245b786 | 3105 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 3106 | |
3107 | if Present (Prag) then | |
3108 | Arg3 := | |
3109 | Next (Next (First (Pragma_Argument_Associations (Prag)))); | |
3110 | Wfunc := Entity (Expression (Arg3)); | |
3111 | ||
3112 | Rewrite (N, | |
3113 | Make_Attribute_Reference (Loc, | |
3114 | Prefix => New_Occurrence_Of (Etype (Wfunc), Loc), | |
3115 | Attribute_Name => Name_Output, | |
3116 | Expressions => New_List ( | |
3117 | Relocate_Node (First (Exprs)), | |
3118 | Make_Function_Call (Loc, | |
3119 | Name => New_Occurrence_Of (Wfunc, Loc), | |
3120 | Parameter_Associations => New_List ( | |
83aa52b6 | 3121 | OK_Convert_To (Etype (First_Formal (Wfunc)), |
ee6ba406 | 3122 | Relocate_Node (Next (First (Exprs))))))))); |
3123 | ||
3124 | Analyze (N); | |
3125 | return; | |
3126 | ||
3127 | -- For elementary types, we call the W_xxx routine directly. | |
3128 | -- Note that the effect of Write and Output is identical for | |
3129 | -- the case of an elementary type, since there are no | |
3130 | -- discriminants or bounds. | |
3131 | ||
3132 | elsif Is_Elementary_Type (U_Type) then | |
3133 | ||
3134 | -- A special case arises if we have a defined _Write routine, | |
3135 | -- since in this case we are required to call this routine. | |
3136 | ||
9dfe12ae | 3137 | if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then |
ee6ba406 | 3138 | Build_Record_Or_Elementary_Output_Procedure |
3139 | (Loc, U_Type, Decl, Pname); | |
3140 | Insert_Action (N, Decl); | |
3141 | ||
3142 | -- For normal cases, we call the W_xxx routine directly | |
3143 | ||
3144 | else | |
3145 | Rewrite (N, Build_Elementary_Write_Call (N)); | |
3146 | Analyze (N); | |
3147 | return; | |
3148 | end if; | |
3149 | ||
3150 | -- Array type case | |
3151 | ||
3152 | elsif Is_Array_Type (U_Type) then | |
3153 | Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname); | |
3154 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
3155 | ||
3156 | -- Class-wide case, first output external tag, then dispatch | |
3157 | -- to the appropriate primitive Output function (RM 13.13.2(31)). | |
3158 | ||
3159 | elsif Is_Class_Wide_Type (P_Type) then | |
99f2248e | 3160 | |
3161 | -- No need to do anything else compiling under restriction | |
3162 | -- No_Dispatching_Calls. During the semantic analysis we | |
3163 | -- already notified such violation. | |
3164 | ||
3165 | if Restriction_Active (No_Dispatching_Calls) then | |
3166 | return; | |
3167 | end if; | |
3168 | ||
ee6ba406 | 3169 | Tag_Write : declare |
3170 | Strm : constant Node_Id := First (Exprs); | |
3171 | Item : constant Node_Id := Next (Strm); | |
3172 | ||
3173 | begin | |
83aa52b6 | 3174 | -- Ada 2005 (AI-344): Check that the accessibility level |
3175 | -- of the type of the output object is not deeper than | |
3176 | -- that of the attribute's prefix type. | |
3177 | ||
aad6babd | 3178 | -- if Get_Access_Level (Item'Tag) |
3179 | -- /= Get_Access_Level (P_Type'Tag) | |
3180 | -- then | |
3181 | -- raise Tag_Error; | |
3182 | -- end if; | |
83aa52b6 | 3183 | |
aad6babd | 3184 | -- String'Output (Strm, External_Tag (Item'Tag)); |
3185 | ||
83aa52b6 | 3186 | -- We cannot figure out a practical way to implement this |
3187 | -- accessibility check on virtual machines, so we omit it. | |
aad6babd | 3188 | |
de54c5ab | 3189 | if Ada_Version >= Ada_2005 |
662256db | 3190 | and then Tagged_Type_Expansion |
83aa52b6 | 3191 | then |
aad6babd | 3192 | Insert_Action (N, |
3193 | Make_Implicit_If_Statement (N, | |
3194 | Condition => | |
3195 | Make_Op_Ne (Loc, | |
3196 | Left_Opnd => | |
99f2248e | 3197 | Build_Get_Access_Level (Loc, |
3198 | Make_Attribute_Reference (Loc, | |
3199 | Prefix => | |
3200 | Relocate_Node ( | |
3201 | Duplicate_Subexpr (Item, | |
3202 | Name_Req => True)), | |
3203 | Attribute_Name => Name_Tag)), | |
3204 | ||
aad6babd | 3205 | Right_Opnd => |
99f2248e | 3206 | Make_Integer_Literal (Loc, |
3207 | Type_Access_Level (P_Type))), | |
3208 | ||
aad6babd | 3209 | Then_Statements => |
3210 | New_List (Make_Raise_Statement (Loc, | |
3211 | New_Occurrence_Of ( | |
3212 | RTE (RE_Tag_Error), Loc))))); | |
3213 | end if; | |
ee6ba406 | 3214 | |
3215 | Insert_Action (N, | |
3216 | Make_Attribute_Reference (Loc, | |
3217 | Prefix => New_Occurrence_Of (Standard_String, Loc), | |
3218 | Attribute_Name => Name_Output, | |
3219 | Expressions => New_List ( | |
3220 | Relocate_Node (Duplicate_Subexpr (Strm)), | |
3221 | Make_Function_Call (Loc, | |
3222 | Name => | |
3223 | New_Occurrence_Of (RTE (RE_External_Tag), Loc), | |
3224 | Parameter_Associations => New_List ( | |
3225 | Make_Attribute_Reference (Loc, | |
3226 | Prefix => | |
3227 | Relocate_Node | |
3228 | (Duplicate_Subexpr (Item, Name_Req => True)), | |
3229 | Attribute_Name => Name_Tag)))))); | |
3230 | end Tag_Write; | |
3231 | ||
9dfe12ae | 3232 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Output); |
ee6ba406 | 3233 | |
3234 | -- Tagged type case, use the primitive Output function | |
3235 | ||
3236 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 3237 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Output); |
ee6ba406 | 3238 | |
99f2248e | 3239 | -- All other record type cases, including protected records. |
3240 | -- The latter only arise for expander generated code for | |
3241 | -- handling shared passive partition access. | |
ee6ba406 | 3242 | |
3243 | else | |
3244 | pragma Assert | |
3245 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
3246 | ||
00f91aef | 3247 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
3248 | -- the default implementation of the Output attribute of an | |
3249 | -- unchecked union type if the type lacks default discriminant | |
3250 | -- values. | |
3251 | ||
3252 | if Is_Unchecked_Union (Base_Type (U_Type)) | |
18a40e97 | 3253 | and then No (Discriminant_Constraint (U_Type)) |
00f91aef | 3254 | then |
3255 | Insert_Action (N, | |
3256 | Make_Raise_Program_Error (Loc, | |
3257 | Reason => PE_Unchecked_Union_Restriction)); | |
3258 | ||
3259 | return; | |
3260 | end if; | |
3261 | ||
ee6ba406 | 3262 | Build_Record_Or_Elementary_Output_Procedure |
3263 | (Loc, Base_Type (U_Type), Decl, Pname); | |
3264 | Insert_Action (N, Decl); | |
3265 | end if; | |
3266 | end if; | |
3267 | ||
3268 | -- If we fall through, Pname is the name of the procedure to call | |
3269 | ||
3270 | Rewrite_Stream_Proc_Call (Pname); | |
3271 | end Output; | |
3272 | ||
3273 | --------- | |
3274 | -- Pos -- | |
3275 | --------- | |
3276 | ||
3277 | -- For enumeration types with a standard representation, Pos is | |
d55c93e0 | 3278 | -- handled by the back end. |
ee6ba406 | 3279 | |
5c182b3b | 3280 | -- For enumeration types, with a non-standard representation we generate |
3281 | -- a call to the _Rep_To_Pos function created when the type was frozen. | |
3282 | -- The call has the form | |
ee6ba406 | 3283 | |
9dfe12ae | 3284 | -- _rep_to_pos (expr, flag) |
ee6ba406 | 3285 | |
9dfe12ae | 3286 | -- The parameter flag is True if range checks are enabled, causing |
3287 | -- Program_Error to be raised if the expression has an invalid | |
3288 | -- representation, and False if range checks are suppressed. | |
ee6ba406 | 3289 | |
3290 | -- For integer types, Pos is equivalent to a simple integer | |
3291 | -- conversion and we rewrite it as such | |
3292 | ||
3293 | when Attribute_Pos => Pos : | |
3294 | declare | |
3295 | Etyp : Entity_Id := Base_Type (Entity (Pref)); | |
3296 | ||
3297 | begin | |
3298 | -- Deal with zero/non-zero boolean values | |
3299 | ||
3300 | if Is_Boolean_Type (Etyp) then | |
3301 | Adjust_Condition (First (Exprs)); | |
3302 | Etyp := Standard_Boolean; | |
3303 | Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc)); | |
3304 | end if; | |
3305 | ||
3306 | -- Case of enumeration type | |
3307 | ||
3308 | if Is_Enumeration_Type (Etyp) then | |
3309 | ||
3310 | -- Non-standard enumeration type (generate call) | |
3311 | ||
3312 | if Present (Enum_Pos_To_Rep (Etyp)) then | |
9dfe12ae | 3313 | Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc)); |
ee6ba406 | 3314 | Rewrite (N, |
3315 | Convert_To (Typ, | |
3316 | Make_Function_Call (Loc, | |
3317 | Name => | |
9dfe12ae | 3318 | New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
ee6ba406 | 3319 | Parameter_Associations => Exprs))); |
3320 | ||
3321 | Analyze_And_Resolve (N, Typ); | |
3322 | ||
3323 | -- Standard enumeration type (do universal integer check) | |
3324 | ||
3325 | else | |
3326 | Apply_Universal_Integer_Attribute_Checks (N); | |
3327 | end if; | |
3328 | ||
3329 | -- Deal with integer types (replace by conversion) | |
3330 | ||
3331 | elsif Is_Integer_Type (Etyp) then | |
3332 | Rewrite (N, Convert_To (Typ, First (Exprs))); | |
3333 | Analyze_And_Resolve (N, Typ); | |
3334 | end if; | |
3335 | ||
3336 | end Pos; | |
3337 | ||
3338 | -------------- | |
3339 | -- Position -- | |
3340 | -------------- | |
3341 | ||
d55c93e0 | 3342 | -- We compute this if a component clause was present, otherwise we leave |
3343 | -- the computation up to the back end, since we don't know what layout | |
3344 | -- will be chosen. | |
ee6ba406 | 3345 | |
3346 | when Attribute_Position => Position : | |
3347 | declare | |
3348 | CE : constant Entity_Id := Entity (Selector_Name (Pref)); | |
3349 | ||
3350 | begin | |
3351 | if Present (Component_Clause (CE)) then | |
3352 | Rewrite (N, | |
3353 | Make_Integer_Literal (Loc, | |
3354 | Intval => Component_Bit_Offset (CE) / System_Storage_Unit)); | |
3355 | Analyze_And_Resolve (N, Typ); | |
3356 | ||
3357 | else | |
3358 | Apply_Universal_Integer_Attribute_Checks (N); | |
3359 | end if; | |
3360 | end Position; | |
3361 | ||
3362 | ---------- | |
3363 | -- Pred -- | |
3364 | ---------- | |
3365 | ||
3366 | -- 1. Deal with enumeration types with holes | |
3367 | -- 2. For floating-point, generate call to attribute function | |
3368 | -- 3. For other cases, deal with constraint checking | |
3369 | ||
3370 | when Attribute_Pred => Pred : | |
3371 | declare | |
d55c93e0 | 3372 | Etyp : constant Entity_Id := Base_Type (Ptyp); |
ee6ba406 | 3373 | |
3374 | begin | |
d55c93e0 | 3375 | |
ee6ba406 | 3376 | -- For enumeration types with non-standard representations, we |
3377 | -- expand typ'Pred (x) into | |
3378 | ||
3379 | -- Pos_To_Rep (Rep_To_Pos (x) - 1) | |
3380 | ||
9dfe12ae | 3381 | -- If the representation is contiguous, we compute instead |
3382 | -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations. | |
d55c93e0 | 3383 | -- The conversion function Enum_Pos_To_Rep is defined on the |
3384 | -- base type, not the subtype, so we have to use the base type | |
3385 | -- explicitly for this and other enumeration attributes. | |
9dfe12ae | 3386 | |
ee6ba406 | 3387 | if Is_Enumeration_Type (Ptyp) |
d55c93e0 | 3388 | and then Present (Enum_Pos_To_Rep (Etyp)) |
ee6ba406 | 3389 | then |
d55c93e0 | 3390 | if Has_Contiguous_Rep (Etyp) then |
9dfe12ae | 3391 | Rewrite (N, |
3392 | Unchecked_Convert_To (Ptyp, | |
3393 | Make_Op_Add (Loc, | |
3394 | Left_Opnd => | |
3395 | Make_Integer_Literal (Loc, | |
3396 | Enumeration_Rep (First_Literal (Ptyp))), | |
3397 | Right_Opnd => | |
3398 | Make_Function_Call (Loc, | |
3399 | Name => | |
3400 | New_Reference_To | |
d55c93e0 | 3401 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
9dfe12ae | 3402 | |
3403 | Parameter_Associations => | |
3404 | New_List ( | |
3405 | Unchecked_Convert_To (Ptyp, | |
3406 | Make_Op_Subtract (Loc, | |
3407 | Left_Opnd => | |
3408 | Unchecked_Convert_To (Standard_Integer, | |
3409 | Relocate_Node (First (Exprs))), | |
3410 | Right_Opnd => | |
3411 | Make_Integer_Literal (Loc, 1))), | |
3412 | Rep_To_Pos_Flag (Ptyp, Loc)))))); | |
ee6ba406 | 3413 | |
9dfe12ae | 3414 | else |
3415 | -- Add Boolean parameter True, to request program errror if | |
3416 | -- we have a bad representation on our hands. If checks are | |
3417 | -- suppressed, then add False instead | |
ee6ba406 | 3418 | |
9dfe12ae | 3419 | Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc)); |
3420 | Rewrite (N, | |
3421 | Make_Indexed_Component (Loc, | |
d55c93e0 | 3422 | Prefix => |
3423 | New_Reference_To | |
3424 | (Enum_Pos_To_Rep (Etyp), Loc), | |
9dfe12ae | 3425 | Expressions => New_List ( |
3426 | Make_Op_Subtract (Loc, | |
ee6ba406 | 3427 | Left_Opnd => |
3428 | Make_Function_Call (Loc, | |
3429 | Name => | |
d55c93e0 | 3430 | New_Reference_To |
3431 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), | |
9dfe12ae | 3432 | Parameter_Associations => Exprs), |
ee6ba406 | 3433 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); |
9dfe12ae | 3434 | end if; |
ee6ba406 | 3435 | |
3436 | Analyze_And_Resolve (N, Typ); | |
3437 | ||
3438 | -- For floating-point, we transform 'Pred into a call to the Pred | |
3439 | -- floating-point attribute function in Fat_xxx (xxx is root type) | |
3440 | ||
3441 | elsif Is_Floating_Point_Type (Ptyp) then | |
3442 | Expand_Fpt_Attribute_R (N); | |
3443 | Analyze_And_Resolve (N, Typ); | |
3444 | ||
3445 | -- For modular types, nothing to do (no overflow, since wraps) | |
3446 | ||
3447 | elsif Is_Modular_Integer_Type (Ptyp) then | |
3448 | null; | |
3449 | ||
a9b57347 | 3450 | -- For other types, if argument is marked as needing a range check or |
3451 | -- overflow checking is enabled, we must generate a check. | |
ee6ba406 | 3452 | |
a9b57347 | 3453 | elsif not Overflow_Checks_Suppressed (Ptyp) |
3454 | or else Do_Range_Check (First (Exprs)) | |
3455 | then | |
3456 | Set_Do_Range_Check (First (Exprs), False); | |
ee6ba406 | 3457 | Expand_Pred_Succ (N); |
3458 | end if; | |
ee6ba406 | 3459 | end Pred; |
3460 | ||
7f8eb6ed | 3461 | -------------- |
3462 | -- Priority -- | |
3463 | -------------- | |
3464 | ||
3465 | -- Ada 2005 (AI-327): Dynamic ceiling priorities | |
3466 | ||
3467 | -- We rewrite X'Priority as the following run-time call: | |
3468 | ||
3469 | -- Get_Ceiling (X._Object) | |
3470 | ||
3471 | -- Note that although X'Priority is notionally an object, it is quite | |
3472 | -- deliberately not defined as an aliased object in the RM. This means | |
3473 | -- that it works fine to rewrite it as a call, without having to worry | |
3474 | -- about complications that would other arise from X'Priority'Access, | |
3475 | -- which is illegal, because of the lack of aliasing. | |
3476 | ||
3477 | when Attribute_Priority => | |
3478 | declare | |
3479 | Call : Node_Id; | |
3480 | Conctyp : Entity_Id; | |
3481 | Object_Parm : Node_Id; | |
3482 | Subprg : Entity_Id; | |
3483 | RT_Subprg_Name : Node_Id; | |
3484 | ||
3485 | begin | |
3486 | -- Look for the enclosing concurrent type | |
3487 | ||
3488 | Conctyp := Current_Scope; | |
3489 | while not Is_Concurrent_Type (Conctyp) loop | |
3490 | Conctyp := Scope (Conctyp); | |
3491 | end loop; | |
3492 | ||
3493 | pragma Assert (Is_Protected_Type (Conctyp)); | |
3494 | ||
3495 | -- Generate the actual of the call | |
3496 | ||
3497 | Subprg := Current_Scope; | |
3498 | while not Present (Protected_Body_Subprogram (Subprg)) loop | |
3499 | Subprg := Scope (Subprg); | |
3500 | end loop; | |
3501 | ||
db14252c | 3502 | -- Use of 'Priority inside protected entries and barriers (in |
3503 | -- both cases the type of the first formal of their expanded | |
3504 | -- subprogram is Address) | |
3505 | ||
3506 | if Etype (First_Entity (Protected_Body_Subprogram (Subprg))) | |
3507 | = RTE (RE_Address) | |
3508 | then | |
3509 | declare | |
3510 | New_Itype : Entity_Id; | |
3511 | ||
3512 | begin | |
3513 | -- In the expansion of protected entries the type of the | |
3514 | -- first formal of the Protected_Body_Subprogram is an | |
3515 | -- Address. In order to reference the _object component | |
3516 | -- we generate: | |
3517 | ||
3518 | -- type T is access p__ptTV; | |
3519 | -- freeze T [] | |
3520 | ||
3521 | New_Itype := Create_Itype (E_Access_Type, N); | |
3522 | Set_Etype (New_Itype, New_Itype); | |
db14252c | 3523 | Set_Directly_Designated_Type (New_Itype, |
3524 | Corresponding_Record_Type (Conctyp)); | |
3525 | Freeze_Itype (New_Itype, N); | |
3526 | ||
3527 | -- Generate: | |
3528 | -- T!(O)._object'unchecked_access | |
3529 | ||
3530 | Object_Parm := | |
3531 | Make_Attribute_Reference (Loc, | |
55868293 | 3532 | Prefix => |
3533 | Make_Selected_Component (Loc, | |
3534 | Prefix => | |
3535 | Unchecked_Convert_To (New_Itype, | |
3536 | New_Reference_To | |
3537 | (First_Entity | |
3538 | (Protected_Body_Subprogram (Subprg)), | |
3539 | Loc)), | |
3540 | Selector_Name => | |
3541 | Make_Identifier (Loc, Name_uObject)), | |
db14252c | 3542 | Attribute_Name => Name_Unchecked_Access); |
3543 | end; | |
3544 | ||
3545 | -- Use of 'Priority inside a protected subprogram | |
3546 | ||
3547 | else | |
3548 | Object_Parm := | |
3549 | Make_Attribute_Reference (Loc, | |
3550 | Prefix => | |
3551 | Make_Selected_Component (Loc, | |
3552 | Prefix => New_Reference_To | |
3553 | (First_Entity | |
3554 | (Protected_Body_Subprogram (Subprg)), | |
3555 | Loc), | |
55868293 | 3556 | Selector_Name => Make_Identifier (Loc, Name_uObject)), |
db14252c | 3557 | Attribute_Name => Name_Unchecked_Access); |
3558 | end if; | |
7f8eb6ed | 3559 | |
3560 | -- Select the appropriate run-time subprogram | |
3561 | ||
3562 | if Number_Entries (Conctyp) = 0 then | |
3563 | RT_Subprg_Name := | |
3564 | New_Reference_To (RTE (RE_Get_Ceiling), Loc); | |
3565 | else | |
3566 | RT_Subprg_Name := | |
3567 | New_Reference_To (RTE (RO_PE_Get_Ceiling), Loc); | |
3568 | end if; | |
3569 | ||
3570 | Call := | |
3571 | Make_Function_Call (Loc, | |
3572 | Name => RT_Subprg_Name, | |
3573 | Parameter_Associations => New_List (Object_Parm)); | |
3574 | ||
3575 | Rewrite (N, Call); | |
db14252c | 3576 | |
3577 | -- Avoid the generation of extra checks on the pointer to the | |
3578 | -- protected object. | |
3579 | ||
3580 | Analyze_And_Resolve (N, Typ, Suppress => Access_Check); | |
7f8eb6ed | 3581 | end; |
3582 | ||
ee6ba406 | 3583 | ------------------ |
3584 | -- Range_Length -- | |
3585 | ------------------ | |
3586 | ||
d55c93e0 | 3587 | when Attribute_Range_Length => Range_Length : begin |
5c182b3b | 3588 | |
ee6ba406 | 3589 | -- The only special processing required is for the case where |
3590 | -- Range_Length is applied to an enumeration type with holes. | |
3591 | -- In this case we transform | |
3592 | ||
3593 | -- X'Range_Length | |
3594 | ||
3595 | -- to | |
3596 | ||
3597 | -- X'Pos (X'Last) - X'Pos (X'First) + 1 | |
3598 | ||
3599 | -- So that the result reflects the proper Pos values instead | |
3600 | -- of the underlying representations. | |
3601 | ||
d55c93e0 | 3602 | if Is_Enumeration_Type (Ptyp) |
3603 | and then Has_Non_Standard_Rep (Ptyp) | |
ee6ba406 | 3604 | then |
3605 | Rewrite (N, | |
3606 | Make_Op_Add (Loc, | |
3607 | Left_Opnd => | |
3608 | Make_Op_Subtract (Loc, | |
3609 | Left_Opnd => | |
3610 | Make_Attribute_Reference (Loc, | |
3611 | Attribute_Name => Name_Pos, | |
d55c93e0 | 3612 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
ee6ba406 | 3613 | Expressions => New_List ( |
3614 | Make_Attribute_Reference (Loc, | |
3615 | Attribute_Name => Name_Last, | |
d55c93e0 | 3616 | Prefix => New_Occurrence_Of (Ptyp, Loc)))), |
ee6ba406 | 3617 | |
3618 | Right_Opnd => | |
3619 | Make_Attribute_Reference (Loc, | |
3620 | Attribute_Name => Name_Pos, | |
d55c93e0 | 3621 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
ee6ba406 | 3622 | Expressions => New_List ( |
3623 | Make_Attribute_Reference (Loc, | |
3624 | Attribute_Name => Name_First, | |
d55c93e0 | 3625 | Prefix => New_Occurrence_Of (Ptyp, Loc))))), |
ee6ba406 | 3626 | |
328a1570 | 3627 | Right_Opnd => Make_Integer_Literal (Loc, 1))); |
ee6ba406 | 3628 | |
3629 | Analyze_And_Resolve (N, Typ); | |
3630 | ||
d55c93e0 | 3631 | -- For all other cases, the attribute is handled by the back end, but |
3632 | -- we need to deal with the case of the range check on a universal | |
3633 | -- integer. | |
ee6ba406 | 3634 | |
3635 | else | |
3636 | Apply_Universal_Integer_Attribute_Checks (N); | |
3637 | end if; | |
ee6ba406 | 3638 | end Range_Length; |
3639 | ||
3640 | ---------- | |
3641 | -- Read -- | |
3642 | ---------- | |
3643 | ||
3644 | when Attribute_Read => Read : declare | |
3645 | P_Type : constant Entity_Id := Entity (Pref); | |
3646 | B_Type : constant Entity_Id := Base_Type (P_Type); | |
3647 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
3648 | Pname : Entity_Id; | |
3649 | Decl : Node_Id; | |
3650 | Prag : Node_Id; | |
3651 | Arg2 : Node_Id; | |
3652 | Rfunc : Node_Id; | |
3653 | Lhs : Node_Id; | |
3654 | Rhs : Node_Id; | |
3655 | ||
3656 | begin | |
3657 | -- If no underlying type, we have an error that will be diagnosed | |
3658 | -- elsewhere, so here we just completely ignore the expansion. | |
3659 | ||
3660 | if No (U_Type) then | |
3661 | return; | |
3662 | end if; | |
3663 | ||
3664 | -- The simple case, if there is a TSS for Read, just call it | |
3665 | ||
9dfe12ae | 3666 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read); |
ee6ba406 | 3667 | |
3668 | if Present (Pname) then | |
3669 | null; | |
3670 | ||
3671 | else | |
3672 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
3673 | ||
3674 | -- sourcetyp'Read (stream, Item) | |
3675 | ||
3676 | -- as | |
3677 | ||
3678 | -- Item := sourcetyp (strmread (strmtyp'Input (Stream))); | |
3679 | ||
aad6babd | 3680 | -- where strmread is the given Read function that converts an |
3681 | -- argument of type strmtyp to type sourcetyp or a type from which | |
3682 | -- it is derived. The conversion to sourcetyp is required in the | |
3683 | -- latter case. | |
ee6ba406 | 3684 | |
3685 | -- A special case arises if Item is a type conversion in which | |
3686 | -- case, we have to expand to: | |
3687 | ||
3688 | -- Itemx := typex (strmread (strmtyp'Input (Stream))); | |
3689 | ||
3690 | -- where Itemx is the expression of the type conversion (i.e. | |
3691 | -- the actual object), and typex is the type of Itemx. | |
3692 | ||
5245b786 | 3693 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 3694 | |
3695 | if Present (Prag) then | |
3696 | Arg2 := Next (First (Pragma_Argument_Associations (Prag))); | |
3697 | Rfunc := Entity (Expression (Arg2)); | |
3698 | Lhs := Relocate_Node (Next (First (Exprs))); | |
3699 | Rhs := | |
83aa52b6 | 3700 | OK_Convert_To (B_Type, |
ee6ba406 | 3701 | Make_Function_Call (Loc, |
3702 | Name => New_Occurrence_Of (Rfunc, Loc), | |
3703 | Parameter_Associations => New_List ( | |
3704 | Make_Attribute_Reference (Loc, | |
3705 | Prefix => | |
3706 | New_Occurrence_Of | |
3707 | (Etype (First_Formal (Rfunc)), Loc), | |
3708 | Attribute_Name => Name_Input, | |
3709 | Expressions => New_List ( | |
3710 | Relocate_Node (First (Exprs))))))); | |
3711 | ||
3712 | if Nkind (Lhs) = N_Type_Conversion then | |
3713 | Lhs := Expression (Lhs); | |
3714 | Rhs := Convert_To (Etype (Lhs), Rhs); | |
3715 | end if; | |
3716 | ||
3717 | Rewrite (N, | |
3718 | Make_Assignment_Statement (Loc, | |
9dfe12ae | 3719 | Name => Lhs, |
ee6ba406 | 3720 | Expression => Rhs)); |
3721 | Set_Assignment_OK (Lhs); | |
3722 | Analyze (N); | |
3723 | return; | |
3724 | ||
3725 | -- For elementary types, we call the I_xxx routine using the first | |
3726 | -- parameter and then assign the result into the second parameter. | |
3727 | -- We set Assignment_OK to deal with the conversion case. | |
3728 | ||
3729 | elsif Is_Elementary_Type (U_Type) then | |
3730 | declare | |
3731 | Lhs : Node_Id; | |
3732 | Rhs : Node_Id; | |
3733 | ||
3734 | begin | |
3735 | Lhs := Relocate_Node (Next (First (Exprs))); | |
3736 | Rhs := Build_Elementary_Input_Call (N); | |
3737 | ||
3738 | if Nkind (Lhs) = N_Type_Conversion then | |
3739 | Lhs := Expression (Lhs); | |
3740 | Rhs := Convert_To (Etype (Lhs), Rhs); | |
3741 | end if; | |
3742 | ||
3743 | Set_Assignment_OK (Lhs); | |
3744 | ||
3745 | Rewrite (N, | |
3746 | Make_Assignment_Statement (Loc, | |
328a1570 | 3747 | Name => Lhs, |
ee6ba406 | 3748 | Expression => Rhs)); |
3749 | ||
3750 | Analyze (N); | |
3751 | return; | |
3752 | end; | |
3753 | ||
3754 | -- Array type case | |
3755 | ||
3756 | elsif Is_Array_Type (U_Type) then | |
3757 | Build_Array_Read_Procedure (N, U_Type, Decl, Pname); | |
3758 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
3759 | ||
3760 | -- Tagged type case, use the primitive Read function. Note that | |
3761 | -- this will dispatch in the class-wide case which is what we want | |
3762 | ||
3763 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 3764 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Read); |
ee6ba406 | 3765 | |
aad6babd | 3766 | -- All other record type cases, including protected records. The |
3767 | -- latter only arise for expander generated code for handling | |
3768 | -- shared passive partition access. | |
ee6ba406 | 3769 | |
3770 | else | |
3771 | pragma Assert | |
3772 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
3773 | ||
00f91aef | 3774 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
3775 | -- the default implementation of the Read attribute of an | |
3776 | -- Unchecked_Union type. | |
3777 | ||
3778 | if Is_Unchecked_Union (Base_Type (U_Type)) then | |
3779 | Insert_Action (N, | |
3780 | Make_Raise_Program_Error (Loc, | |
3781 | Reason => PE_Unchecked_Union_Restriction)); | |
3782 | end if; | |
3783 | ||
ee6ba406 | 3784 | if Has_Discriminants (U_Type) |
3785 | and then Present | |
3786 | (Discriminant_Default_Value (First_Discriminant (U_Type))) | |
3787 | then | |
3788 | Build_Mutable_Record_Read_Procedure | |
7af38999 | 3789 | (Loc, Full_Base (U_Type), Decl, Pname); |
ee6ba406 | 3790 | else |
3791 | Build_Record_Read_Procedure | |
7af38999 | 3792 | (Loc, Full_Base (U_Type), Decl, Pname); |
ee6ba406 | 3793 | end if; |
3794 | ||
3795 | -- Suppress checks, uninitialized or otherwise invalid | |
3796 | -- data does not cause constraint errors to be raised for | |
3797 | -- a complete record read. | |
3798 | ||
3799 | Insert_Action (N, Decl, All_Checks); | |
3800 | end if; | |
3801 | end if; | |
3802 | ||
3803 | Rewrite_Stream_Proc_Call (Pname); | |
3804 | end Read; | |
3805 | ||
4f2ad752 | 3806 | --------- |
3807 | -- Ref -- | |
3808 | --------- | |
3809 | ||
3810 | -- Ref is identical to To_Address, see To_Address for processing | |
3811 | ||
ee6ba406 | 3812 | --------------- |
3813 | -- Remainder -- | |
3814 | --------------- | |
3815 | ||
3816 | -- Transforms 'Remainder into a call to the floating-point attribute | |
3817 | -- function Remainder in Fat_xxx (where xxx is the root type) | |
3818 | ||
3819 | when Attribute_Remainder => | |
3820 | Expand_Fpt_Attribute_RR (N); | |
3821 | ||
d55c93e0 | 3822 | ------------ |
3823 | -- Result -- | |
3824 | ------------ | |
3825 | ||
3826 | -- Transform 'Result into reference to _Result formal. At the point | |
3827 | -- where a legal 'Result attribute is expanded, we know that we are in | |
3828 | -- the context of a _Postcondition function with a _Result parameter. | |
3829 | ||
3830 | when Attribute_Result => | |
328a1570 | 3831 | Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult)); |
d55c93e0 | 3832 | Analyze_And_Resolve (N, Typ); |
3833 | ||
ee6ba406 | 3834 | ----------- |
3835 | -- Round -- | |
3836 | ----------- | |
3837 | ||
aad6babd | 3838 | -- The handling of the Round attribute is quite delicate. The processing |
3839 | -- in Sem_Attr introduced a conversion to universal real, reflecting the | |
3840 | -- semantics of Round, but we do not want anything to do with universal | |
3841 | -- real at runtime, since this corresponds to using floating-point | |
3842 | -- arithmetic. | |
3843 | ||
3844 | -- What we have now is that the Etype of the Round attribute correctly | |
3845 | -- indicates the final result type. The operand of the Round is the | |
3846 | -- conversion to universal real, described above, and the operand of | |
3847 | -- this conversion is the actual operand of Round, which may be the | |
3848 | -- special case of a fixed point multiplication or division (Etype = | |
3849 | -- universal fixed) | |
3850 | ||
3851 | -- The exapander will expand first the operand of the conversion, then | |
3852 | -- the conversion, and finally the round attribute itself, since we | |
3853 | -- always work inside out. But we cannot simply process naively in this | |
3854 | -- order. In the semantic world where universal fixed and real really | |
3855 | -- exist and have infinite precision, there is no problem, but in the | |
3856 | -- implementation world, where universal real is a floating-point type, | |
3857 | -- we would get the wrong result. | |
3858 | ||
3859 | -- So the approach is as follows. First, when expanding a multiply or | |
3860 | -- divide whose type is universal fixed, we do nothing at all, instead | |
3861 | -- deferring the operation till later. | |
ee6ba406 | 3862 | |
3863 | -- The actual processing is done in Expand_N_Type_Conversion which | |
aad6babd | 3864 | -- handles the special case of Round by looking at its parent to see if |
3865 | -- it is a Round attribute, and if it is, handling the conversion (or | |
3866 | -- its fixed multiply/divide child) in an appropriate manner. | |
ee6ba406 | 3867 | |
3868 | -- This means that by the time we get to expanding the Round attribute | |
3869 | -- itself, the Round is nothing more than a type conversion (and will | |
3870 | -- often be a null type conversion), so we just replace it with the | |
3871 | -- appropriate conversion operation. | |
3872 | ||
3873 | when Attribute_Round => | |
3874 | Rewrite (N, | |
3875 | Convert_To (Etype (N), Relocate_Node (First (Exprs)))); | |
3876 | Analyze_And_Resolve (N); | |
3877 | ||
3878 | -------------- | |
3879 | -- Rounding -- | |
3880 | -------------- | |
3881 | ||
3882 | -- Transforms 'Rounding into a call to the floating-point attribute | |
3883 | -- function Rounding in Fat_xxx (where xxx is the root type) | |
3884 | ||
3885 | when Attribute_Rounding => | |
3886 | Expand_Fpt_Attribute_R (N); | |
3887 | ||
3888 | ------------- | |
3889 | -- Scaling -- | |
3890 | ------------- | |
3891 | ||
3892 | -- Transforms 'Scaling into a call to the floating-point attribute | |
3893 | -- function Scaling in Fat_xxx (where xxx is the root type) | |
3894 | ||
3895 | when Attribute_Scaling => | |
3896 | Expand_Fpt_Attribute_RI (N); | |
3897 | ||
3898 | ---------- | |
3899 | -- Size -- | |
3900 | ---------- | |
3901 | ||
3902 | when Attribute_Size | | |
3903 | Attribute_Object_Size | | |
3904 | Attribute_Value_Size | | |
3905 | Attribute_VADS_Size => Size : | |
3906 | ||
3907 | declare | |
ee6ba406 | 3908 | Siz : Uint; |
9dfe12ae | 3909 | New_Node : Node_Id; |
ee6ba406 | 3910 | |
3911 | begin | |
3912 | -- Processing for VADS_Size case. Note that this processing removes | |
3913 | -- all traces of VADS_Size from the tree, and completes all required | |
3914 | -- processing for VADS_Size by translating the attribute reference | |
3915 | -- to an appropriate Size or Object_Size reference. | |
3916 | ||
3917 | if Id = Attribute_VADS_Size | |
3918 | or else (Use_VADS_Size and then Id = Attribute_Size) | |
3919 | then | |
3920 | -- If the size is specified, then we simply use the specified | |
3921 | -- size. This applies to both types and objects. The size of an | |
3922 | -- object can be specified in the following ways: | |
3923 | ||
3924 | -- An explicit size object is given for an object | |
3925 | -- A component size is specified for an indexed component | |
3926 | -- A component clause is specified for a selected component | |
3927 | -- The object is a component of a packed composite object | |
3928 | ||
3929 | -- If the size is specified, then VADS_Size of an object | |
3930 | ||
3931 | if (Is_Entity_Name (Pref) | |
3932 | and then Present (Size_Clause (Entity (Pref)))) | |
3933 | or else | |
3934 | (Nkind (Pref) = N_Component_Clause | |
3935 | and then (Present (Component_Clause | |
3936 | (Entity (Selector_Name (Pref)))) | |
3937 | or else Is_Packed (Etype (Prefix (Pref))))) | |
3938 | or else | |
3939 | (Nkind (Pref) = N_Indexed_Component | |
3940 | and then (Component_Size (Etype (Prefix (Pref))) /= 0 | |
3941 | or else Is_Packed (Etype (Prefix (Pref))))) | |
3942 | then | |
3943 | Set_Attribute_Name (N, Name_Size); | |
3944 | ||
3945 | -- Otherwise if we have an object rather than a type, then the | |
3946 | -- VADS_Size attribute applies to the type of the object, rather | |
3947 | -- than the object itself. This is one of the respects in which | |
3948 | -- VADS_Size differs from Size. | |
3949 | ||
3950 | else | |
3951 | if (not Is_Entity_Name (Pref) | |
3952 | or else not Is_Type (Entity (Pref))) | |
d55c93e0 | 3953 | and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp)) |
ee6ba406 | 3954 | then |
d55c93e0 | 3955 | Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc)); |
ee6ba406 | 3956 | end if; |
3957 | ||
aad6babd | 3958 | -- For a scalar type for which no size was explicitly given, |
3959 | -- VADS_Size means Object_Size. This is the other respect in | |
3960 | -- which VADS_Size differs from Size. | |
ee6ba406 | 3961 | |
d55c93e0 | 3962 | if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then |
ee6ba406 | 3963 | Set_Attribute_Name (N, Name_Object_Size); |
3964 | ||
3965 | -- In all other cases, Size and VADS_Size are the sane | |
3966 | ||
3967 | else | |
3968 | Set_Attribute_Name (N, Name_Size); | |
3969 | end if; | |
3970 | end if; | |
3971 | end if; | |
3972 | ||
d55c93e0 | 3973 | -- For class-wide types, X'Class'Size is transformed into a direct |
3974 | -- reference to the Size of the class type, so that the back end does | |
3975 | -- not have to deal with the X'Class'Size reference. | |
ee6ba406 | 3976 | |
9dfe12ae | 3977 | if Is_Entity_Name (Pref) |
3978 | and then Is_Class_Wide_Type (Entity (Pref)) | |
3979 | then | |
3980 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
3981 | return; | |
3982 | ||
1550b445 | 3983 | -- For X'Size applied to an object of a class-wide type, transform |
9dfe12ae | 3984 | -- X'Size into a call to the primitive operation _Size applied to X. |
3985 | ||
442049cc | 3986 | elsif Is_Class_Wide_Type (Ptyp) |
3987 | or else (Id = Attribute_Size | |
3988 | and then Is_Tagged_Type (Ptyp) | |
3989 | and then Has_Unknown_Discriminants (Ptyp)) | |
3990 | then | |
99f2248e | 3991 | -- No need to do anything else compiling under restriction |
3992 | -- No_Dispatching_Calls. During the semantic analysis we | |
3993 | -- already notified such violation. | |
3994 | ||
3995 | if Restriction_Active (No_Dispatching_Calls) then | |
3996 | return; | |
3997 | end if; | |
3998 | ||
ee6ba406 | 3999 | New_Node := |
4000 | Make_Function_Call (Loc, | |
4001 | Name => New_Reference_To | |
4002 | (Find_Prim_Op (Ptyp, Name_uSize), Loc), | |
4003 | Parameter_Associations => New_List (Pref)); | |
4004 | ||
4005 | if Typ /= Standard_Long_Long_Integer then | |
4006 | ||
4007 | -- The context is a specific integer type with which the | |
4008 | -- original attribute was compatible. The function has a | |
4009 | -- specific type as well, so to preserve the compatibility | |
4010 | -- we must convert explicitly. | |
4011 | ||
4012 | New_Node := Convert_To (Typ, New_Node); | |
4013 | end if; | |
4014 | ||
4015 | Rewrite (N, New_Node); | |
4016 | Analyze_And_Resolve (N, Typ); | |
442049cc | 4017 | return; |
83aa52b6 | 4018 | |
4019 | -- Case of known RM_Size of a type | |
4020 | ||
4021 | elsif (Id = Attribute_Size or else Id = Attribute_Value_Size) | |
4022 | and then Is_Entity_Name (Pref) | |
4023 | and then Is_Type (Entity (Pref)) | |
4024 | and then Known_Static_RM_Size (Entity (Pref)) | |
4025 | then | |
4026 | Siz := RM_Size (Entity (Pref)); | |
4027 | ||
4028 | -- Case of known Esize of a type | |
4029 | ||
4030 | elsif Id = Attribute_Object_Size | |
4031 | and then Is_Entity_Name (Pref) | |
4032 | and then Is_Type (Entity (Pref)) | |
4033 | and then Known_Static_Esize (Entity (Pref)) | |
4034 | then | |
4035 | Siz := Esize (Entity (Pref)); | |
4036 | ||
4037 | -- Case of known size of object | |
4038 | ||
4039 | elsif Id = Attribute_Size | |
4040 | and then Is_Entity_Name (Pref) | |
4041 | and then Is_Object (Entity (Pref)) | |
4042 | and then Known_Esize (Entity (Pref)) | |
4043 | and then Known_Static_Esize (Entity (Pref)) | |
4044 | then | |
4045 | Siz := Esize (Entity (Pref)); | |
ee6ba406 | 4046 | |
4047 | -- For an array component, we can do Size in the front end | |
4048 | -- if the component_size of the array is set. | |
4049 | ||
4050 | elsif Nkind (Pref) = N_Indexed_Component then | |
4051 | Siz := Component_Size (Etype (Prefix (Pref))); | |
4052 | ||
aad6babd | 4053 | -- For a record component, we can do Size in the front end if there |
4054 | -- is a component clause, or if the record is packed and the | |
4055 | -- component's size is known at compile time. | |
ee6ba406 | 4056 | |
4057 | elsif Nkind (Pref) = N_Selected_Component then | |
4058 | declare | |
4059 | Rec : constant Entity_Id := Etype (Prefix (Pref)); | |
4060 | Comp : constant Entity_Id := Entity (Selector_Name (Pref)); | |
4061 | ||
4062 | begin | |
4063 | if Present (Component_Clause (Comp)) then | |
4064 | Siz := Esize (Comp); | |
4065 | ||
4066 | elsif Is_Packed (Rec) then | |
4067 | Siz := RM_Size (Ptyp); | |
4068 | ||
4069 | else | |
4070 | Apply_Universal_Integer_Attribute_Checks (N); | |
4071 | return; | |
4072 | end if; | |
4073 | end; | |
4074 | ||
d55c93e0 | 4075 | -- All other cases are handled by the back end |
ee6ba406 | 4076 | |
4077 | else | |
4078 | Apply_Universal_Integer_Attribute_Checks (N); | |
4079 | ||
1550b445 | 4080 | -- If Size is applied to a formal parameter that is of a packed |
4081 | -- array subtype, then apply Size to the actual subtype. | |
ee6ba406 | 4082 | |
4083 | if Is_Entity_Name (Pref) | |
4084 | and then Is_Formal (Entity (Pref)) | |
d55c93e0 | 4085 | and then Is_Array_Type (Ptyp) |
4086 | and then Is_Packed (Ptyp) | |
ee6ba406 | 4087 | then |
4088 | Rewrite (N, | |
4089 | Make_Attribute_Reference (Loc, | |
4090 | Prefix => | |
4091 | New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc), | |
4092 | Attribute_Name => Name_Size)); | |
4093 | Analyze_And_Resolve (N, Typ); | |
4094 | end if; | |
4095 | ||
83aa52b6 | 4096 | -- If Size applies to a dereference of an access to unconstrained |
d55c93e0 | 4097 | -- packed array, the back end needs to see its unconstrained |
4098 | -- nominal type, but also a hint to the actual constrained type. | |
1550b445 | 4099 | |
4100 | if Nkind (Pref) = N_Explicit_Dereference | |
d55c93e0 | 4101 | and then Is_Array_Type (Ptyp) |
4102 | and then not Is_Constrained (Ptyp) | |
4103 | and then Is_Packed (Ptyp) | |
1550b445 | 4104 | then |
4105 | Set_Actual_Designated_Subtype (Pref, | |
4106 | Get_Actual_Subtype (Pref)); | |
4107 | end if; | |
4108 | ||
ee6ba406 | 4109 | return; |
4110 | end if; | |
4111 | ||
4112 | -- Common processing for record and array component case | |
4113 | ||
83aa52b6 | 4114 | if Siz /= No_Uint and then Siz /= 0 then |
6deeca1d | 4115 | declare |
4116 | CS : constant Boolean := Comes_From_Source (N); | |
ee6ba406 | 4117 | |
6deeca1d | 4118 | begin |
4119 | Rewrite (N, Make_Integer_Literal (Loc, Siz)); | |
4120 | ||
4121 | -- This integer literal is not a static expression. We do not | |
4122 | -- call Analyze_And_Resolve here, because this would activate | |
4123 | -- the circuit for deciding that a static value was out of | |
4124 | -- range, and we don't want that. | |
ee6ba406 | 4125 | |
6deeca1d | 4126 | -- So just manually set the type, mark the expression as non- |
4127 | -- static, and then ensure that the result is checked properly | |
4128 | -- if the attribute comes from source (if it was internally | |
4129 | -- generated, we never need a constraint check). | |
ee6ba406 | 4130 | |
6deeca1d | 4131 | Set_Etype (N, Typ); |
4132 | Set_Is_Static_Expression (N, False); | |
4133 | ||
4134 | if CS then | |
4135 | Apply_Constraint_Check (N, Typ); | |
4136 | end if; | |
4137 | end; | |
ee6ba406 | 4138 | end if; |
4139 | end Size; | |
4140 | ||
4141 | ------------------ | |
4142 | -- Storage_Pool -- | |
4143 | ------------------ | |
4144 | ||
4145 | when Attribute_Storage_Pool => | |
4146 | Rewrite (N, | |
4147 | Make_Type_Conversion (Loc, | |
4148 | Subtype_Mark => New_Reference_To (Etype (N), Loc), | |
4149 | Expression => New_Reference_To (Entity (N), Loc))); | |
4150 | Analyze_And_Resolve (N, Typ); | |
4151 | ||
4152 | ------------------ | |
4153 | -- Storage_Size -- | |
4154 | ------------------ | |
4155 | ||
d55c93e0 | 4156 | when Attribute_Storage_Size => Storage_Size : begin |
ee6ba406 | 4157 | |
ee6ba406 | 4158 | -- Access type case, always go to the root type |
4159 | ||
4160 | -- The case of access types results in a value of zero for the case | |
4161 | -- where no storage size attribute clause has been given. If a | |
4162 | -- storage size has been given, then the attribute is converted | |
4163 | -- to a reference to the variable used to hold this value. | |
4164 | ||
4165 | if Is_Access_Type (Ptyp) then | |
4166 | if Present (Storage_Size_Variable (Root_Type (Ptyp))) then | |
4167 | Rewrite (N, | |
4168 | Make_Attribute_Reference (Loc, | |
4169 | Prefix => New_Reference_To (Typ, Loc), | |
4170 | Attribute_Name => Name_Max, | |
4171 | Expressions => New_List ( | |
4172 | Make_Integer_Literal (Loc, 0), | |
4173 | Convert_To (Typ, | |
4174 | New_Reference_To | |
4175 | (Storage_Size_Variable (Root_Type (Ptyp)), Loc))))); | |
4176 | ||
4177 | elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then | |
4178 | Rewrite (N, | |
4179 | OK_Convert_To (Typ, | |
4180 | Make_Function_Call (Loc, | |
9dfe12ae | 4181 | Name => |
4182 | New_Reference_To | |
7f8eb6ed | 4183 | (Find_Prim_Op |
4184 | (Etype (Associated_Storage_Pool (Root_Type (Ptyp))), | |
4185 | Attribute_Name (N)), | |
4186 | Loc), | |
4187 | ||
4188 | Parameter_Associations => New_List ( | |
4189 | New_Reference_To | |
4190 | (Associated_Storage_Pool (Root_Type (Ptyp)), Loc))))); | |
ee6ba406 | 4191 | |
ee6ba406 | 4192 | else |
4193 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
4194 | end if; | |
4195 | ||
4196 | Analyze_And_Resolve (N, Typ); | |
4197 | ||
7f8eb6ed | 4198 | -- For tasks, we retrieve the size directly from the TCB. The |
4199 | -- size may depend on a discriminant of the type, and therefore | |
4200 | -- can be a per-object expression, so type-level information is | |
4201 | -- not sufficient in general. There are four cases to consider: | |
ee6ba406 | 4202 | |
7f8eb6ed | 4203 | -- a) If the attribute appears within a task body, the designated |
4204 | -- TCB is obtained by a call to Self. | |
ee6ba406 | 4205 | |
7f8eb6ed | 4206 | -- b) If the prefix of the attribute is the name of a task object, |
4207 | -- the designated TCB is the one stored in the corresponding record. | |
ee6ba406 | 4208 | |
7f8eb6ed | 4209 | -- c) If the prefix is a task type, the size is obtained from the |
4210 | -- size variable created for each task type | |
ee6ba406 | 4211 | |
7f8eb6ed | 4212 | -- d) If no storage_size was specified for the type , there is no |
4213 | -- size variable, and the value is a system-specific default. | |
ee6ba406 | 4214 | |
4215 | else | |
7f8eb6ed | 4216 | if In_Open_Scopes (Ptyp) then |
4217 | ||
4218 | -- Storage_Size (Self) | |
4219 | ||
ee6ba406 | 4220 | Rewrite (N, |
4221 | Convert_To (Typ, | |
4222 | Make_Function_Call (Loc, | |
4223 | Name => | |
7f8eb6ed | 4224 | New_Occurrence_Of (RTE (RE_Storage_Size), Loc), |
4225 | Parameter_Associations => | |
4226 | New_List ( | |
4227 | Make_Function_Call (Loc, | |
4228 | Name => | |
4229 | New_Reference_To (RTE (RE_Self), Loc)))))); | |
ee6ba406 | 4230 | |
7f8eb6ed | 4231 | elsif not Is_Entity_Name (Pref) |
4232 | or else not Is_Type (Entity (Pref)) | |
4233 | then | |
4234 | -- Storage_Size (Rec (Obj).Size) | |
4235 | ||
4236 | Rewrite (N, | |
4237 | Convert_To (Typ, | |
4238 | Make_Function_Call (Loc, | |
4239 | Name => | |
4240 | New_Occurrence_Of (RTE (RE_Storage_Size), Loc), | |
4241 | Parameter_Associations => | |
ee6ba406 | 4242 | New_List ( |
4243 | Make_Selected_Component (Loc, | |
4244 | Prefix => | |
4245 | Unchecked_Convert_To ( | |
4246 | Corresponding_Record_Type (Ptyp), | |
7f8eb6ed | 4247 | New_Copy_Tree (Pref)), |
ee6ba406 | 4248 | Selector_Name => |
7f8eb6ed | 4249 | Make_Identifier (Loc, Name_uTask_Id)))))); |
ee6ba406 | 4250 | |
7f8eb6ed | 4251 | elsif Present (Storage_Size_Variable (Ptyp)) then |
ee6ba406 | 4252 | |
7f8eb6ed | 4253 | -- Static storage size pragma given for type: retrieve value |
4254 | -- from its allocated storage variable. | |
ee6ba406 | 4255 | |
7f8eb6ed | 4256 | Rewrite (N, |
4257 | Convert_To (Typ, | |
4258 | Make_Function_Call (Loc, | |
4259 | Name => New_Occurrence_Of ( | |
4260 | RTE (RE_Adjust_Storage_Size), Loc), | |
4261 | Parameter_Associations => | |
4262 | New_List ( | |
4263 | New_Reference_To ( | |
4264 | Storage_Size_Variable (Ptyp), Loc))))); | |
4265 | else | |
4266 | -- Get system default | |
4267 | ||
4268 | Rewrite (N, | |
4269 | Convert_To (Typ, | |
4270 | Make_Function_Call (Loc, | |
4271 | Name => | |
4272 | New_Occurrence_Of ( | |
4273 | RTE (RE_Default_Stack_Size), Loc)))); | |
ee6ba406 | 4274 | end if; |
7f8eb6ed | 4275 | |
4276 | Analyze_And_Resolve (N, Typ); | |
ee6ba406 | 4277 | end if; |
4278 | end Storage_Size; | |
4279 | ||
7189d17f | 4280 | ----------------- |
4281 | -- Stream_Size -- | |
4282 | ----------------- | |
4283 | ||
3430bf31 | 4284 | when Attribute_Stream_Size => |
4285 | Rewrite (N, | |
4286 | Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp))); | |
7189d17f | 4287 | Analyze_And_Resolve (N, Typ); |
7189d17f | 4288 | |
ee6ba406 | 4289 | ---------- |
4290 | -- Succ -- | |
4291 | ---------- | |
4292 | ||
4293 | -- 1. Deal with enumeration types with holes | |
4294 | -- 2. For floating-point, generate call to attribute function | |
4295 | -- 3. For other cases, deal with constraint checking | |
4296 | ||
5c182b3b | 4297 | when Attribute_Succ => Succ : declare |
d55c93e0 | 4298 | Etyp : constant Entity_Id := Base_Type (Ptyp); |
ee6ba406 | 4299 | |
4300 | begin | |
d55c93e0 | 4301 | |
ee6ba406 | 4302 | -- For enumeration types with non-standard representations, we |
4303 | -- expand typ'Succ (x) into | |
4304 | ||
4305 | -- Pos_To_Rep (Rep_To_Pos (x) + 1) | |
4306 | ||
9dfe12ae | 4307 | -- If the representation is contiguous, we compute instead |
4308 | -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations. | |
4309 | ||
ee6ba406 | 4310 | if Is_Enumeration_Type (Ptyp) |
d55c93e0 | 4311 | and then Present (Enum_Pos_To_Rep (Etyp)) |
ee6ba406 | 4312 | then |
d55c93e0 | 4313 | if Has_Contiguous_Rep (Etyp) then |
9dfe12ae | 4314 | Rewrite (N, |
4315 | Unchecked_Convert_To (Ptyp, | |
4316 | Make_Op_Add (Loc, | |
4317 | Left_Opnd => | |
4318 | Make_Integer_Literal (Loc, | |
4319 | Enumeration_Rep (First_Literal (Ptyp))), | |
4320 | Right_Opnd => | |
4321 | Make_Function_Call (Loc, | |
4322 | Name => | |
4323 | New_Reference_To | |
d55c93e0 | 4324 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
9dfe12ae | 4325 | |
4326 | Parameter_Associations => | |
4327 | New_List ( | |
4328 | Unchecked_Convert_To (Ptyp, | |
4329 | Make_Op_Add (Loc, | |
4330 | Left_Opnd => | |
4331 | Unchecked_Convert_To (Standard_Integer, | |
4332 | Relocate_Node (First (Exprs))), | |
4333 | Right_Opnd => | |
4334 | Make_Integer_Literal (Loc, 1))), | |
4335 | Rep_To_Pos_Flag (Ptyp, Loc)))))); | |
4336 | else | |
4337 | -- Add Boolean parameter True, to request program errror if | |
4338 | -- we have a bad representation on our hands. Add False if | |
4339 | -- checks are suppressed. | |
ee6ba406 | 4340 | |
9dfe12ae | 4341 | Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc)); |
4342 | Rewrite (N, | |
4343 | Make_Indexed_Component (Loc, | |
d55c93e0 | 4344 | Prefix => |
4345 | New_Reference_To | |
4346 | (Enum_Pos_To_Rep (Etyp), Loc), | |
9dfe12ae | 4347 | Expressions => New_List ( |
4348 | Make_Op_Add (Loc, | |
4349 | Left_Opnd => | |
4350 | Make_Function_Call (Loc, | |
4351 | Name => | |
4352 | New_Reference_To | |
d55c93e0 | 4353 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
9dfe12ae | 4354 | Parameter_Associations => Exprs), |
4355 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); | |
4356 | end if; | |
ee6ba406 | 4357 | |
4358 | Analyze_And_Resolve (N, Typ); | |
4359 | ||
4360 | -- For floating-point, we transform 'Succ into a call to the Succ | |
4361 | -- floating-point attribute function in Fat_xxx (xxx is root type) | |
4362 | ||
4363 | elsif Is_Floating_Point_Type (Ptyp) then | |
4364 | Expand_Fpt_Attribute_R (N); | |
4365 | Analyze_And_Resolve (N, Typ); | |
4366 | ||
4367 | -- For modular types, nothing to do (no overflow, since wraps) | |
4368 | ||
4369 | elsif Is_Modular_Integer_Type (Ptyp) then | |
4370 | null; | |
4371 | ||
a9b57347 | 4372 | -- For other types, if argument is marked as needing a range check or |
4373 | -- overflow checking is enabled, we must generate a check. | |
ee6ba406 | 4374 | |
a9b57347 | 4375 | elsif not Overflow_Checks_Suppressed (Ptyp) |
4376 | or else Do_Range_Check (First (Exprs)) | |
4377 | then | |
4378 | Set_Do_Range_Check (First (Exprs), False); | |
ee6ba406 | 4379 | Expand_Pred_Succ (N); |
4380 | end if; | |
4381 | end Succ; | |
4382 | ||
4383 | --------- | |
4384 | -- Tag -- | |
4385 | --------- | |
4386 | ||
4387 | -- Transforms X'Tag into a direct reference to the tag of X | |
4388 | ||
5c182b3b | 4389 | when Attribute_Tag => Tag : declare |
ee6ba406 | 4390 | Ttyp : Entity_Id; |
4391 | Prefix_Is_Type : Boolean; | |
4392 | ||
4393 | begin | |
4394 | if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then | |
4395 | Ttyp := Entity (Pref); | |
4396 | Prefix_Is_Type := True; | |
4397 | else | |
d55c93e0 | 4398 | Ttyp := Ptyp; |
ee6ba406 | 4399 | Prefix_Is_Type := False; |
4400 | end if; | |
4401 | ||
4402 | if Is_Class_Wide_Type (Ttyp) then | |
4403 | Ttyp := Root_Type (Ttyp); | |
4404 | end if; | |
4405 | ||
4406 | Ttyp := Underlying_Type (Ttyp); | |
4407 | ||
8188864b | 4408 | -- Ada 2005: The type may be a synchronized tagged type, in which |
4409 | -- case the tag information is stored in the corresponding record. | |
4410 | ||
4411 | if Is_Concurrent_Type (Ttyp) then | |
4412 | Ttyp := Corresponding_Record_Type (Ttyp); | |
4413 | end if; | |
4414 | ||
ee6ba406 | 4415 | if Prefix_Is_Type then |
1d7e0b5b | 4416 | |
83aa52b6 | 4417 | -- For VMs we leave the type attribute unexpanded because |
1d7e0b5b | 4418 | -- there's not a dispatching table to reference. |
4419 | ||
662256db | 4420 | if Tagged_Type_Expansion then |
1d7e0b5b | 4421 | Rewrite (N, |
4422 | Unchecked_Convert_To (RTE (RE_Tag), | |
4660e715 | 4423 | New_Reference_To |
4424 | (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc))); | |
1d7e0b5b | 4425 | Analyze_And_Resolve (N, RTE (RE_Tag)); |
4426 | end if; | |
ee6ba406 | 4427 | |
b6dbc975 | 4428 | -- Ada 2005 (AI-251): The use of 'Tag in the sources always |
83aa52b6 | 4429 | -- references the primary tag of the actual object. If 'Tag is |
4430 | -- applied to class-wide interface objects we generate code that | |
4431 | -- displaces "this" to reference the base of the object. | |
4432 | ||
4433 | elsif Comes_From_Source (N) | |
4434 | and then Is_Class_Wide_Type (Etype (Prefix (N))) | |
4435 | and then Is_Interface (Etype (Prefix (N))) | |
4436 | then | |
4437 | -- Generate: | |
4438 | -- (To_Tag_Ptr (Prefix'Address)).all | |
4439 | ||
4440 | -- Note that Prefix'Address is recursively expanded into a call | |
4441 | -- to Base_Address (Obj.Tag) | |
4442 | ||
f0bf2ff3 | 4443 | -- Not needed for VM targets, since all handled by the VM |
4444 | ||
662256db | 4445 | if Tagged_Type_Expansion then |
f0bf2ff3 | 4446 | Rewrite (N, |
4447 | Make_Explicit_Dereference (Loc, | |
4448 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
4449 | Make_Attribute_Reference (Loc, | |
4450 | Prefix => Relocate_Node (Pref), | |
4451 | Attribute_Name => Name_Address)))); | |
4452 | Analyze_And_Resolve (N, RTE (RE_Tag)); | |
4453 | end if; | |
83aa52b6 | 4454 | |
ee6ba406 | 4455 | else |
4456 | Rewrite (N, | |
4457 | Make_Selected_Component (Loc, | |
4458 | Prefix => Relocate_Node (Pref), | |
4459 | Selector_Name => | |
4660e715 | 4460 | New_Reference_To (First_Tag_Component (Ttyp), Loc))); |
1d7e0b5b | 4461 | Analyze_And_Resolve (N, RTE (RE_Tag)); |
ee6ba406 | 4462 | end if; |
ee6ba406 | 4463 | end Tag; |
4464 | ||
4465 | ---------------- | |
4466 | -- Terminated -- | |
4467 | ---------------- | |
4468 | ||
aad6babd | 4469 | -- Transforms 'Terminated attribute into a call to Terminated function |
ee6ba406 | 4470 | |
4471 | when Attribute_Terminated => Terminated : | |
4472 | begin | |
1550b445 | 4473 | -- The prefix of Terminated is of a task interface class-wide type. |
4474 | -- Generate: | |
83aa52b6 | 4475 | -- terminated (Task_Id (Pref._disp_get_task_id)); |
1550b445 | 4476 | |
de54c5ab | 4477 | if Ada_Version >= Ada_2005 |
d55c93e0 | 4478 | and then Ekind (Ptyp) = E_Class_Wide_Type |
4479 | and then Is_Interface (Ptyp) | |
4480 | and then Is_Task_Interface (Ptyp) | |
1550b445 | 4481 | then |
4482 | Rewrite (N, | |
4483 | Make_Function_Call (Loc, | |
4484 | Name => | |
4485 | New_Reference_To (RTE (RE_Terminated), Loc), | |
4486 | Parameter_Associations => New_List ( | |
83aa52b6 | 4487 | Make_Unchecked_Type_Conversion (Loc, |
4488 | Subtype_Mark => | |
4489 | New_Reference_To (RTE (RO_ST_Task_Id), Loc), | |
4490 | Expression => | |
4491 | Make_Selected_Component (Loc, | |
4492 | Prefix => | |
4493 | New_Copy_Tree (Pref), | |
4494 | Selector_Name => | |
4495 | Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))))); | |
1550b445 | 4496 | |
4497 | elsif Restricted_Profile then | |
ee6ba406 | 4498 | Rewrite (N, |
4499 | Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated))); | |
4500 | ||
4501 | else | |
4502 | Rewrite (N, | |
4503 | Build_Call_With_Task (Pref, RTE (RE_Terminated))); | |
4504 | end if; | |
4505 | ||
4506 | Analyze_And_Resolve (N, Standard_Boolean); | |
4507 | end Terminated; | |
4508 | ||
4509 | ---------------- | |
4510 | -- To_Address -- | |
4511 | ---------------- | |
4512 | ||
4f2ad752 | 4513 | -- Transforms System'To_Address (X) and System.Address'Ref (X) into |
4514 | -- unchecked conversion from (integral) type of X to type address. | |
ee6ba406 | 4515 | |
4f2ad752 | 4516 | when Attribute_To_Address | Attribute_Ref => |
ee6ba406 | 4517 | Rewrite (N, |
4518 | Unchecked_Convert_To (RTE (RE_Address), | |
4519 | Relocate_Node (First (Exprs)))); | |
4520 | Analyze_And_Resolve (N, RTE (RE_Address)); | |
4521 | ||
5690e662 | 4522 | ------------ |
4523 | -- To_Any -- | |
4524 | ------------ | |
4525 | ||
4526 | when Attribute_To_Any => To_Any : declare | |
4527 | P_Type : constant Entity_Id := Etype (Pref); | |
4528 | Decls : constant List_Id := New_List; | |
4529 | begin | |
4530 | Rewrite (N, | |
4531 | Build_To_Any_Call | |
4532 | (Convert_To (P_Type, | |
4533 | Relocate_Node (First (Exprs))), Decls)); | |
4534 | Insert_Actions (N, Decls); | |
4535 | Analyze_And_Resolve (N, RTE (RE_Any)); | |
4536 | end To_Any; | |
4537 | ||
ee6ba406 | 4538 | ---------------- |
4539 | -- Truncation -- | |
4540 | ---------------- | |
4541 | ||
4542 | -- Transforms 'Truncation into a call to the floating-point attribute | |
99f2248e | 4543 | -- function Truncation in Fat_xxx (where xxx is the root type). |
4544 | -- Expansion is avoided for cases the back end can handle directly. | |
ee6ba406 | 4545 | |
4546 | when Attribute_Truncation => | |
99f2248e | 4547 | if not Is_Inline_Floating_Point_Attribute (N) then |
4548 | Expand_Fpt_Attribute_R (N); | |
4549 | end if; | |
ee6ba406 | 4550 | |
5690e662 | 4551 | -------------- |
4552 | -- TypeCode -- | |
4553 | -------------- | |
4554 | ||
4555 | when Attribute_TypeCode => TypeCode : declare | |
4556 | P_Type : constant Entity_Id := Etype (Pref); | |
4557 | Decls : constant List_Id := New_List; | |
4558 | begin | |
4559 | Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls)); | |
4560 | Insert_Actions (N, Decls); | |
4561 | Analyze_And_Resolve (N, RTE (RE_TypeCode)); | |
4562 | end TypeCode; | |
4563 | ||
ee6ba406 | 4564 | ----------------------- |
4565 | -- Unbiased_Rounding -- | |
4566 | ----------------------- | |
4567 | ||
4568 | -- Transforms 'Unbiased_Rounding into a call to the floating-point | |
4569 | -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the | |
99f2248e | 4570 | -- root type). Expansion is avoided for cases the back end can handle |
4571 | -- directly. | |
ee6ba406 | 4572 | |
4573 | when Attribute_Unbiased_Rounding => | |
99f2248e | 4574 | if not Is_Inline_Floating_Point_Attribute (N) then |
4575 | Expand_Fpt_Attribute_R (N); | |
4576 | end if; | |
ee6ba406 | 4577 | |
ee6ba406 | 4578 | ----------------- |
4579 | -- UET_Address -- | |
4580 | ----------------- | |
4581 | ||
4582 | when Attribute_UET_Address => UET_Address : declare | |
46eb6933 | 4583 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T'); |
ee6ba406 | 4584 | |
4585 | begin | |
4586 | Insert_Action (N, | |
4587 | Make_Object_Declaration (Loc, | |
4588 | Defining_Identifier => Ent, | |
4589 | Aliased_Present => True, | |
4590 | Object_Definition => | |
4591 | New_Occurrence_Of (RTE (RE_Address), Loc))); | |
4592 | ||
4593 | -- Construct name __gnat_xxx__SDP, where xxx is the unit name | |
4594 | -- in normal external form. | |
4595 | ||
4596 | Get_External_Unit_Name_String (Get_Unit_Name (Pref)); | |
4597 | Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len); | |
4598 | Name_Len := Name_Len + 7; | |
4599 | Name_Buffer (1 .. 7) := "__gnat_"; | |
4600 | Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP"; | |
4601 | Name_Len := Name_Len + 5; | |
4602 | ||
4603 | Set_Is_Imported (Ent); | |
4604 | Set_Interface_Name (Ent, | |
4605 | Make_String_Literal (Loc, | |
4606 | Strval => String_From_Name_Buffer)); | |
4607 | ||
f947f061 | 4608 | -- Set entity as internal to ensure proper Sprint output of its |
4609 | -- implicit importation. | |
4610 | ||
4611 | Set_Is_Internal (Ent); | |
4612 | ||
ee6ba406 | 4613 | Rewrite (N, |
4614 | Make_Attribute_Reference (Loc, | |
4615 | Prefix => New_Occurrence_Of (Ent, Loc), | |
4616 | Attribute_Name => Name_Address)); | |
4617 | ||
4618 | Analyze_And_Resolve (N, Typ); | |
4619 | end UET_Address; | |
4620 | ||
ee6ba406 | 4621 | --------------- |
4622 | -- VADS_Size -- | |
4623 | --------------- | |
4624 | ||
4625 | -- The processing for VADS_Size is shared with Size | |
4626 | ||
4627 | --------- | |
4628 | -- Val -- | |
4629 | --------- | |
4630 | ||
4631 | -- For enumeration types with a standard representation, and for all | |
d55c93e0 | 4632 | -- other types, Val is handled by the back end. For enumeration types |
4633 | -- with a non-standard representation we use the _Pos_To_Rep array that | |
ee6ba406 | 4634 | -- was created when the type was frozen. |
4635 | ||
5c182b3b | 4636 | when Attribute_Val => Val : declare |
ee6ba406 | 4637 | Etyp : constant Entity_Id := Base_Type (Entity (Pref)); |
4638 | ||
4639 | begin | |
4640 | if Is_Enumeration_Type (Etyp) | |
4641 | and then Present (Enum_Pos_To_Rep (Etyp)) | |
4642 | then | |
9dfe12ae | 4643 | if Has_Contiguous_Rep (Etyp) then |
4644 | declare | |
4645 | Rep_Node : constant Node_Id := | |
4646 | Unchecked_Convert_To (Etyp, | |
4647 | Make_Op_Add (Loc, | |
4648 | Left_Opnd => | |
4649 | Make_Integer_Literal (Loc, | |
4650 | Enumeration_Rep (First_Literal (Etyp))), | |
4651 | Right_Opnd => | |
4652 | (Convert_To (Standard_Integer, | |
4653 | Relocate_Node (First (Exprs)))))); | |
4654 | ||
4655 | begin | |
4656 | Rewrite (N, | |
4657 | Unchecked_Convert_To (Etyp, | |
4658 | Make_Op_Add (Loc, | |
4659 | Left_Opnd => | |
4660 | Make_Integer_Literal (Loc, | |
4661 | Enumeration_Rep (First_Literal (Etyp))), | |
4662 | Right_Opnd => | |
4663 | Make_Function_Call (Loc, | |
4664 | Name => | |
4665 | New_Reference_To | |
4666 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), | |
4667 | Parameter_Associations => New_List ( | |
4668 | Rep_Node, | |
4669 | Rep_To_Pos_Flag (Etyp, Loc)))))); | |
4670 | end; | |
4671 | ||
4672 | else | |
4673 | Rewrite (N, | |
4674 | Make_Indexed_Component (Loc, | |
4675 | Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc), | |
4676 | Expressions => New_List ( | |
4677 | Convert_To (Standard_Integer, | |
4678 | Relocate_Node (First (Exprs)))))); | |
4679 | end if; | |
ee6ba406 | 4680 | |
4681 | Analyze_And_Resolve (N, Typ); | |
a9b57347 | 4682 | |
4683 | -- If the argument is marked as requiring a range check then generate | |
4684 | -- it here. | |
4685 | ||
4686 | elsif Do_Range_Check (First (Exprs)) then | |
4687 | Set_Do_Range_Check (First (Exprs), False); | |
4688 | Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed); | |
ee6ba406 | 4689 | end if; |
4690 | end Val; | |
4691 | ||
4692 | ----------- | |
4693 | -- Valid -- | |
4694 | ----------- | |
4695 | ||
4696 | -- The code for valid is dependent on the particular types involved. | |
4697 | -- See separate sections below for the generated code in each case. | |
4698 | ||
5c182b3b | 4699 | when Attribute_Valid => Valid : declare |
d55c93e0 | 4700 | Btyp : Entity_Id := Base_Type (Ptyp); |
ee6ba406 | 4701 | Tst : Node_Id; |
4702 | ||
9dfe12ae | 4703 | Save_Validity_Checks_On : constant Boolean := Validity_Checks_On; |
4704 | -- Save the validity checking mode. We always turn off validity | |
4705 | -- checking during process of 'Valid since this is one place | |
4706 | -- where we do not want the implicit validity checks to intefere | |
4707 | -- with the explicit validity check that the programmer is doing. | |
4708 | ||
ee6ba406 | 4709 | function Make_Range_Test return Node_Id; |
4710 | -- Build the code for a range test of the form | |
72a8dd48 | 4711 | -- Btyp!(Pref) in Btyp!(Ptyp'First) .. Btyp!(Ptyp'Last) |
ee6ba406 | 4712 | |
9dfe12ae | 4713 | --------------------- |
4714 | -- Make_Range_Test -- | |
4715 | --------------------- | |
4716 | ||
ee6ba406 | 4717 | function Make_Range_Test return Node_Id is |
58f8748b | 4718 | Temp : constant Node_Id := Duplicate_Subexpr (Pref); |
4719 | ||
ee6ba406 | 4720 | begin |
58f8748b | 4721 | -- The value whose validity is being checked has been captured in |
4722 | -- an object declaration. We certainly don't want this object to | |
4723 | -- appear valid because the declaration initializes it! | |
4724 | ||
4725 | if Is_Entity_Name (Temp) then | |
4726 | Set_Is_Known_Valid (Entity (Temp), False); | |
4727 | end if; | |
4728 | ||
ee6ba406 | 4729 | return |
72a8dd48 | 4730 | Make_In (Loc, |
4731 | Left_Opnd => | |
4732 | Unchecked_Convert_To (Btyp, Temp), | |
4733 | Right_Opnd => | |
4734 | Make_Range (Loc, | |
4735 | Low_Bound => | |
ee6ba406 | 4736 | Unchecked_Convert_To (Btyp, |
4737 | Make_Attribute_Reference (Loc, | |
4738 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
72a8dd48 | 4739 | Attribute_Name => Name_First)), |
4740 | High_Bound => | |
ee6ba406 | 4741 | Unchecked_Convert_To (Btyp, |
4742 | Make_Attribute_Reference (Loc, | |
4743 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
4744 | Attribute_Name => Name_Last)))); | |
4745 | end Make_Range_Test; | |
4746 | ||
4747 | -- Start of processing for Attribute_Valid | |
4748 | ||
4749 | begin | |
01cb2726 | 4750 | -- Do not expand sourced code 'Valid reference in CodePeer mode, |
4751 | -- will be handled by the back-end directly. | |
4752 | ||
4753 | if CodePeer_Mode and then Comes_From_Source (N) then | |
4754 | return; | |
4755 | end if; | |
4756 | ||
9dfe12ae | 4757 | -- Turn off validity checks. We do not want any implicit validity |
4758 | -- checks to intefere with the explicit check from the attribute | |
4759 | ||
4760 | Validity_Checks_On := False; | |
4761 | ||
ee6ba406 | 4762 | -- Floating-point case. This case is handled by the Valid attribute |
4763 | -- code in the floating-point attribute run-time library. | |
4764 | ||
4765 | if Is_Floating_Point_Type (Ptyp) then | |
4766 | declare | |
1550b445 | 4767 | Pkg : RE_Id; |
4768 | Ftp : Entity_Id; | |
ee6ba406 | 4769 | |
4770 | begin | |
6e62b6c3 | 4771 | |
95b21580 | 4772 | case Float_Rep (Btyp) is |
c9d7c2c0 | 4773 | |
95b21580 | 4774 | -- For vax fpt types, call appropriate routine in special |
c9d7c2c0 | 4775 | -- vax floating point unit. No need to worry about loads in |
4776 | -- this case, since these types have no signalling NaN's. | |
6e62b6c3 | 4777 | |
95b21580 | 4778 | when VAX_Native => Expand_Vax_Valid (N); |
7f8eb6ed | 4779 | |
95b21580 | 4780 | -- The AAMP back end handles Valid for floating-point types |
7f8eb6ed | 4781 | |
95b21580 | 4782 | when AAMP => |
4783 | Analyze_And_Resolve (Pref, Ptyp); | |
4784 | Set_Etype (N, Standard_Boolean); | |
4785 | Set_Analyzed (N); | |
9dfe12ae | 4786 | |
95b21580 | 4787 | when IEEE_Binary => |
4788 | Find_Fat_Info (Ptyp, Ftp, Pkg); | |
9dfe12ae | 4789 | |
95b21580 | 4790 | -- If the floating-point object might be unaligned, we |
4791 | -- need to call the special routine Unaligned_Valid, | |
4792 | -- which makes the needed copy, being careful not to | |
4793 | -- load the value into any floating-point register. | |
4794 | -- The argument in this case is obj'Address (see | |
4795 | -- Unaligned_Valid routine in Fat_Gen). | |
9dfe12ae | 4796 | |
95b21580 | 4797 | if Is_Possibly_Unaligned_Object (Pref) then |
4798 | Expand_Fpt_Attribute | |
4799 | (N, Pkg, Name_Unaligned_Valid, | |
4800 | New_List ( | |
4801 | Make_Attribute_Reference (Loc, | |
4802 | Prefix => Relocate_Node (Pref), | |
4803 | Attribute_Name => Name_Address))); | |
4804 | ||
4805 | -- In the normal case where we are sure the object is | |
4806 | -- aligned, we generate a call to Valid, and the argument | |
4807 | -- in this case is obj'Unrestricted_Access (after | |
4808 | -- converting obj to the right floating-point type). | |
4809 | ||
4810 | else | |
4811 | Expand_Fpt_Attribute | |
4812 | (N, Pkg, Name_Valid, | |
4813 | New_List ( | |
4814 | Make_Attribute_Reference (Loc, | |
4815 | Prefix => Unchecked_Convert_To (Ftp, Pref), | |
4816 | Attribute_Name => Name_Unrestricted_Access))); | |
4817 | end if; | |
4818 | end case; | |
ee6ba406 | 4819 | |
4820 | -- One more task, we still need a range check. Required | |
4821 | -- only if we have a constraint, since the Valid routine | |
4822 | -- catches infinities properly (infinities are never valid). | |
4823 | ||
4824 | -- The way we do the range check is simply to create the | |
4825 | -- expression: Valid (N) and then Base_Type(Pref) in Typ. | |
4826 | ||
4827 | if not Subtypes_Statically_Match (Ptyp, Btyp) then | |
4828 | Rewrite (N, | |
4829 | Make_And_Then (Loc, | |
4830 | Left_Opnd => Relocate_Node (N), | |
4831 | Right_Opnd => | |
4832 | Make_In (Loc, | |
4833 | Left_Opnd => Convert_To (Btyp, Pref), | |
4834 | Right_Opnd => New_Occurrence_Of (Ptyp, Loc)))); | |
4835 | end if; | |
4836 | end; | |
4837 | ||
4838 | -- Enumeration type with holes | |
4839 | ||
4840 | -- For enumeration types with holes, the Pos value constructed by | |
4841 | -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a | |
4842 | -- second argument of False returns minus one for an invalid value, | |
4843 | -- and the non-negative pos value for a valid value, so the | |
4844 | -- expansion of X'Valid is simply: | |
4845 | ||
4846 | -- type(X)'Pos (X) >= 0 | |
4847 | ||
4848 | -- We can't quite generate it that way because of the requirement | |
5329ca64 | 4849 | -- for the non-standard second argument of False in the resulting |
4850 | -- rep_to_pos call, so we have to explicitly create: | |
ee6ba406 | 4851 | |
4852 | -- _rep_to_pos (X, False) >= 0 | |
4853 | ||
4854 | -- If we have an enumeration subtype, we also check that the | |
4855 | -- value is in range: | |
4856 | ||
4857 | -- _rep_to_pos (X, False) >= 0 | |
4858 | -- and then | |
5329ca64 | 4859 | -- (X >= type(X)'First and then type(X)'Last <= X) |
ee6ba406 | 4860 | |
4861 | elsif Is_Enumeration_Type (Ptyp) | |
4862 | and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp))) | |
4863 | then | |
4864 | Tst := | |
4865 | Make_Op_Ge (Loc, | |
4866 | Left_Opnd => | |
4867 | Make_Function_Call (Loc, | |
4868 | Name => | |
4869 | New_Reference_To | |
9dfe12ae | 4870 | (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc), |
ee6ba406 | 4871 | Parameter_Associations => New_List ( |
4872 | Pref, | |
4873 | New_Occurrence_Of (Standard_False, Loc))), | |
4874 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
4875 | ||
4876 | if Ptyp /= Btyp | |
4877 | and then | |
4878 | (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp) | |
4879 | or else | |
4880 | Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp)) | |
4881 | then | |
4882 | -- The call to Make_Range_Test will create declarations | |
4883 | -- that need a proper insertion point, but Pref is now | |
4884 | -- attached to a node with no ancestor. Attach to tree | |
4885 | -- even if it is to be rewritten below. | |
4886 | ||
4887 | Set_Parent (Tst, Parent (N)); | |
4888 | ||
4889 | Tst := | |
4890 | Make_And_Then (Loc, | |
4891 | Left_Opnd => Make_Range_Test, | |
4892 | Right_Opnd => Tst); | |
4893 | end if; | |
4894 | ||
4895 | Rewrite (N, Tst); | |
4896 | ||
4897 | -- Fortran convention booleans | |
4898 | ||
4899 | -- For the very special case of Fortran convention booleans, the | |
4900 | -- value is always valid, since it is an integer with the semantics | |
4901 | -- that non-zero is true, and any value is permissible. | |
4902 | ||
4903 | elsif Is_Boolean_Type (Ptyp) | |
4904 | and then Convention (Ptyp) = Convention_Fortran | |
4905 | then | |
4906 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
4907 | ||
4908 | -- For biased representations, we will be doing an unchecked | |
aad6babd | 4909 | -- conversion without unbiasing the result. That means that the range |
4910 | -- test has to take this into account, and the proper form of the | |
4911 | -- test is: | |
ee6ba406 | 4912 | |
4913 | -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length) | |
4914 | ||
4915 | elsif Has_Biased_Representation (Ptyp) then | |
4916 | Btyp := RTE (RE_Unsigned_32); | |
4917 | Rewrite (N, | |
4918 | Make_Op_Lt (Loc, | |
4919 | Left_Opnd => | |
4920 | Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)), | |
4921 | Right_Opnd => | |
4922 | Unchecked_Convert_To (Btyp, | |
4923 | Make_Attribute_Reference (Loc, | |
4924 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
4925 | Attribute_Name => Name_Range_Length)))); | |
4926 | ||
4927 | -- For all other scalar types, what we want logically is a | |
4928 | -- range test: | |
4929 | ||
4930 | -- X in type(X)'First .. type(X)'Last | |
4931 | ||
4932 | -- But that's precisely what won't work because of possible | |
4933 | -- unwanted optimization (and indeed the basic motivation for | |
5329ca64 | 4934 | -- the Valid attribute is exactly that this test does not work!) |
ee6ba406 | 4935 | -- What will work is: |
4936 | ||
4937 | -- Btyp!(X) >= Btyp!(type(X)'First) | |
4938 | -- and then | |
4939 | -- Btyp!(X) <= Btyp!(type(X)'Last) | |
4940 | ||
4941 | -- where Btyp is an integer type large enough to cover the full | |
4942 | -- range of possible stored values (i.e. it is chosen on the basis | |
4943 | -- of the size of the type, not the range of the values). We write | |
4944 | -- this as two tests, rather than a range check, so that static | |
4945 | -- evaluation will easily remove either or both of the checks if | |
4946 | -- they can be -statically determined to be true (this happens | |
4947 | -- when the type of X is static and the range extends to the full | |
4948 | -- range of stored values). | |
4949 | ||
4950 | -- Unsigned types. Note: it is safe to consider only whether the | |
4951 | -- subtype is unsigned, since we will in that case be doing all | |
aad6babd | 4952 | -- unsigned comparisons based on the subtype range. Since we use the |
4953 | -- actual subtype object size, this is appropriate. | |
ee6ba406 | 4954 | |
4955 | -- For example, if we have | |
4956 | ||
4957 | -- subtype x is integer range 1 .. 200; | |
4958 | -- for x'Object_Size use 8; | |
4959 | ||
aad6babd | 4960 | -- Now the base type is signed, but objects of this type are bits |
4961 | -- unsigned, and doing an unsigned test of the range 1 to 200 is | |
4962 | -- correct, even though a value greater than 127 looks signed to a | |
4963 | -- signed comparison. | |
ee6ba406 | 4964 | |
4965 | elsif Is_Unsigned_Type (Ptyp) then | |
4966 | if Esize (Ptyp) <= 32 then | |
4967 | Btyp := RTE (RE_Unsigned_32); | |
4968 | else | |
4969 | Btyp := RTE (RE_Unsigned_64); | |
4970 | end if; | |
4971 | ||
4972 | Rewrite (N, Make_Range_Test); | |
4973 | ||
4974 | -- Signed types | |
4975 | ||
4976 | else | |
4977 | if Esize (Ptyp) <= Esize (Standard_Integer) then | |
4978 | Btyp := Standard_Integer; | |
4979 | else | |
4980 | Btyp := Universal_Integer; | |
4981 | end if; | |
4982 | ||
4983 | Rewrite (N, Make_Range_Test); | |
4984 | end if; | |
4985 | ||
4986 | Analyze_And_Resolve (N, Standard_Boolean); | |
9dfe12ae | 4987 | Validity_Checks_On := Save_Validity_Checks_On; |
ee6ba406 | 4988 | end Valid; |
4989 | ||
4990 | ----------- | |
4991 | -- Value -- | |
4992 | ----------- | |
4993 | ||
4994 | -- Value attribute is handled in separate unti Exp_Imgv | |
4995 | ||
4996 | when Attribute_Value => | |
4997 | Exp_Imgv.Expand_Value_Attribute (N); | |
4998 | ||
4999 | ----------------- | |
5000 | -- Value_Size -- | |
5001 | ----------------- | |
5002 | ||
5003 | -- The processing for Value_Size shares the processing for Size | |
5004 | ||
5005 | ------------- | |
5006 | -- Version -- | |
5007 | ------------- | |
5008 | ||
5009 | -- The processing for Version shares the processing for Body_Version | |
5010 | ||
5011 | ---------------- | |
5012 | -- Wide_Image -- | |
5013 | ---------------- | |
5014 | ||
f0bf2ff3 | 5015 | -- Wide_Image attribute is handled in separate unit Exp_Imgv |
ee6ba406 | 5016 | |
f0bf2ff3 | 5017 | when Attribute_Wide_Image => |
5018 | Exp_Imgv.Expand_Wide_Image_Attribute (N); | |
ee6ba406 | 5019 | |
7189d17f | 5020 | --------------------- |
5021 | -- Wide_Wide_Image -- | |
5022 | --------------------- | |
5023 | ||
f0bf2ff3 | 5024 | -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv |
7189d17f | 5025 | |
f0bf2ff3 | 5026 | when Attribute_Wide_Wide_Image => |
5027 | Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N); | |
7189d17f | 5028 | |
ee6ba406 | 5029 | ---------------- |
5030 | -- Wide_Value -- | |
5031 | ---------------- | |
5032 | ||
5033 | -- We expand typ'Wide_Value (X) into | |
5034 | ||
5035 | -- typ'Value | |
5036 | -- (Wide_String_To_String (X, Wide_Character_Encoding_Method)) | |
5037 | ||
5038 | -- Wide_String_To_String is a runtime function that converts its wide | |
5039 | -- string argument to String, converting any non-translatable characters | |
5040 | -- into appropriate escape sequences. This preserves the required | |
5041 | -- semantics of Wide_Value in all cases, and results in a very simple | |
5042 | -- implementation approach. | |
5043 | ||
7f8eb6ed | 5044 | -- Note: for this approach to be fully standard compliant for the cases |
5045 | -- where typ is Wide_Character and Wide_Wide_Character, the encoding | |
5046 | -- method must cover the entire character range (e.g. UTF-8). But that | |
5047 | -- is a reasonable requirement when dealing with encoded character | |
5048 | -- sequences. Presumably if one of the restrictive encoding mechanisms | |
5049 | -- is in use such as Shift-JIS, then characters that cannot be | |
5050 | -- represented using this encoding will not appear in any case. | |
ee6ba406 | 5051 | |
5052 | when Attribute_Wide_Value => Wide_Value : | |
5053 | begin | |
5054 | Rewrite (N, | |
5055 | Make_Attribute_Reference (Loc, | |
5056 | Prefix => Pref, | |
5057 | Attribute_Name => Name_Value, | |
5058 | ||
5059 | Expressions => New_List ( | |
5060 | Make_Function_Call (Loc, | |
5061 | Name => | |
5062 | New_Reference_To (RTE (RE_Wide_String_To_String), Loc), | |
5063 | ||
5064 | Parameter_Associations => New_List ( | |
5065 | Relocate_Node (First (Exprs)), | |
5066 | Make_Integer_Literal (Loc, | |
5067 | Intval => Int (Wide_Character_Encoding_Method))))))); | |
5068 | ||
5069 | Analyze_And_Resolve (N, Typ); | |
5070 | end Wide_Value; | |
5071 | ||
7189d17f | 5072 | --------------------- |
5073 | -- Wide_Wide_Value -- | |
5074 | --------------------- | |
5075 | ||
5076 | -- We expand typ'Wide_Value_Value (X) into | |
5077 | ||
5078 | -- typ'Value | |
5079 | -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method)) | |
5080 | ||
5081 | -- Wide_Wide_String_To_String is a runtime function that converts its | |
5082 | -- wide string argument to String, converting any non-translatable | |
5083 | -- characters into appropriate escape sequences. This preserves the | |
5084 | -- required semantics of Wide_Wide_Value in all cases, and results in a | |
5085 | -- very simple implementation approach. | |
5086 | ||
5087 | -- It's not quite right where typ = Wide_Wide_Character, because the | |
5088 | -- encoding method may not cover the whole character type ??? | |
5089 | ||
5090 | when Attribute_Wide_Wide_Value => Wide_Wide_Value : | |
5091 | begin | |
5092 | Rewrite (N, | |
5093 | Make_Attribute_Reference (Loc, | |
5094 | Prefix => Pref, | |
5095 | Attribute_Name => Name_Value, | |
5096 | ||
5097 | Expressions => New_List ( | |
5098 | Make_Function_Call (Loc, | |
5099 | Name => | |
5100 | New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc), | |
5101 | ||
5102 | Parameter_Associations => New_List ( | |
5103 | Relocate_Node (First (Exprs)), | |
5104 | Make_Integer_Literal (Loc, | |
5105 | Intval => Int (Wide_Character_Encoding_Method))))))); | |
5106 | ||
5107 | Analyze_And_Resolve (N, Typ); | |
5108 | end Wide_Wide_Value; | |
5109 | ||
5110 | --------------------- | |
5111 | -- Wide_Wide_Width -- | |
5112 | --------------------- | |
5113 | ||
5114 | -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv | |
5115 | ||
5116 | when Attribute_Wide_Wide_Width => | |
5117 | Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide); | |
5118 | ||
ee6ba406 | 5119 | ---------------- |
5120 | -- Wide_Width -- | |
5121 | ---------------- | |
5122 | ||
5123 | -- Wide_Width attribute is handled in separate unit Exp_Imgv | |
5124 | ||
5125 | when Attribute_Wide_Width => | |
7189d17f | 5126 | Exp_Imgv.Expand_Width_Attribute (N, Wide); |
ee6ba406 | 5127 | |
5128 | ----------- | |
5129 | -- Width -- | |
5130 | ----------- | |
5131 | ||
5132 | -- Width attribute is handled in separate unit Exp_Imgv | |
5133 | ||
5134 | when Attribute_Width => | |
7189d17f | 5135 | Exp_Imgv.Expand_Width_Attribute (N, Normal); |
ee6ba406 | 5136 | |
5137 | ----------- | |
5138 | -- Write -- | |
5139 | ----------- | |
5140 | ||
5141 | when Attribute_Write => Write : declare | |
5142 | P_Type : constant Entity_Id := Entity (Pref); | |
5143 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
5144 | Pname : Entity_Id; | |
5145 | Decl : Node_Id; | |
5146 | Prag : Node_Id; | |
5147 | Arg3 : Node_Id; | |
5148 | Wfunc : Node_Id; | |
5149 | ||
5150 | begin | |
5151 | -- If no underlying type, we have an error that will be diagnosed | |
5152 | -- elsewhere, so here we just completely ignore the expansion. | |
5153 | ||
5154 | if No (U_Type) then | |
5155 | return; | |
5156 | end if; | |
5157 | ||
5158 | -- The simple case, if there is a TSS for Write, just call it | |
5159 | ||
9dfe12ae | 5160 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write); |
ee6ba406 | 5161 | |
5162 | if Present (Pname) then | |
5163 | null; | |
5164 | ||
5165 | else | |
5166 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
5167 | ||
5168 | -- sourcetyp'Output (stream, Item) | |
5169 | ||
5170 | -- as | |
5171 | ||
5172 | -- strmtyp'Output (Stream, strmwrite (acttyp (Item))); | |
5173 | ||
aad6babd | 5174 | -- where strmwrite is the given Write function that converts an |
5175 | -- argument of type sourcetyp or a type acctyp, from which it is | |
5176 | -- derived to type strmtyp. The conversion to acttyp is required | |
5177 | -- for the derived case. | |
ee6ba406 | 5178 | |
5245b786 | 5179 | Prag := Get_Stream_Convert_Pragma (P_Type); |
ee6ba406 | 5180 | |
5181 | if Present (Prag) then | |
5182 | Arg3 := | |
5183 | Next (Next (First (Pragma_Argument_Associations (Prag)))); | |
5184 | Wfunc := Entity (Expression (Arg3)); | |
5185 | ||
5186 | Rewrite (N, | |
5187 | Make_Attribute_Reference (Loc, | |
5188 | Prefix => New_Occurrence_Of (Etype (Wfunc), Loc), | |
5189 | Attribute_Name => Name_Output, | |
5190 | Expressions => New_List ( | |
5191 | Relocate_Node (First (Exprs)), | |
5192 | Make_Function_Call (Loc, | |
5193 | Name => New_Occurrence_Of (Wfunc, Loc), | |
5194 | Parameter_Associations => New_List ( | |
83aa52b6 | 5195 | OK_Convert_To (Etype (First_Formal (Wfunc)), |
ee6ba406 | 5196 | Relocate_Node (Next (First (Exprs))))))))); |
5197 | ||
5198 | Analyze (N); | |
5199 | return; | |
5200 | ||
5201 | -- For elementary types, we call the W_xxx routine directly | |
5202 | ||
5203 | elsif Is_Elementary_Type (U_Type) then | |
5204 | Rewrite (N, Build_Elementary_Write_Call (N)); | |
5205 | Analyze (N); | |
5206 | return; | |
5207 | ||
5208 | -- Array type case | |
5209 | ||
5210 | elsif Is_Array_Type (U_Type) then | |
5211 | Build_Array_Write_Procedure (N, U_Type, Decl, Pname); | |
5212 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
5213 | ||
5214 | -- Tagged type case, use the primitive Write function. Note that | |
5215 | -- this will dispatch in the class-wide case which is what we want | |
5216 | ||
5217 | elsif Is_Tagged_Type (U_Type) then | |
9dfe12ae | 5218 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Write); |
ee6ba406 | 5219 | |
5220 | -- All other record type cases, including protected records. | |
5221 | -- The latter only arise for expander generated code for | |
5222 | -- handling shared passive partition access. | |
5223 | ||
5224 | else | |
5225 | pragma Assert | |
5226 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
5227 | ||
00f91aef | 5228 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
5229 | -- the default implementation of the Write attribute of an | |
99f2248e | 5230 | -- Unchecked_Union type. However, if the 'Write reference is |
5231 | -- within the generated Output stream procedure, Write outputs | |
5232 | -- the components, and the default values of the discriminant | |
5233 | -- are streamed by the Output procedure itself. | |
00f91aef | 5234 | |
99f2248e | 5235 | if Is_Unchecked_Union (Base_Type (U_Type)) |
5236 | and not Is_TSS (Current_Scope, TSS_Stream_Output) | |
5237 | then | |
00f91aef | 5238 | Insert_Action (N, |
5239 | Make_Raise_Program_Error (Loc, | |
5240 | Reason => PE_Unchecked_Union_Restriction)); | |
5241 | end if; | |
5242 | ||
ee6ba406 | 5243 | if Has_Discriminants (U_Type) |
5244 | and then Present | |
5245 | (Discriminant_Default_Value (First_Discriminant (U_Type))) | |
5246 | then | |
5247 | Build_Mutable_Record_Write_Procedure | |
7af38999 | 5248 | (Loc, Full_Base (U_Type), Decl, Pname); |
ee6ba406 | 5249 | else |
5250 | Build_Record_Write_Procedure | |
7af38999 | 5251 | (Loc, Full_Base (U_Type), Decl, Pname); |
ee6ba406 | 5252 | end if; |
5253 | ||
5254 | Insert_Action (N, Decl); | |
5255 | end if; | |
5256 | end if; | |
5257 | ||
5258 | -- If we fall through, Pname is the procedure to be called | |
5259 | ||
5260 | Rewrite_Stream_Proc_Call (Pname); | |
5261 | end Write; | |
5262 | ||
d55c93e0 | 5263 | -- Component_Size is handled by the back end, unless the component size |
5264 | -- is known at compile time, which is always true in the packed array | |
5265 | -- case. It is important that the packed array case is handled in the | |
5266 | -- front end (see Eval_Attribute) since the back end would otherwise get | |
5267 | -- confused by the equivalent packed array type. | |
ee6ba406 | 5268 | |
5269 | when Attribute_Component_Size => | |
5270 | null; | |
5271 | ||
18a40e97 | 5272 | -- The following attributes are handled by the back end (except that |
5273 | -- static cases have already been evaluated during semantic processing, | |
5274 | -- but in any case the back end should not count on this). The one bit | |
5275 | -- of special processing required is that these attributes typically | |
5276 | -- generate conditionals in the code, so we need to check the relevant | |
5277 | -- restriction. | |
5278 | ||
5279 | when Attribute_Max | | |
5280 | Attribute_Min => | |
5281 | Check_Restriction (No_Implicit_Conditionals, N); | |
ee6ba406 | 5282 | |
18a40e97 | 5283 | -- The following attributes are handled by the back end (except that |
5284 | -- static cases have already been evaluated during semantic processing, | |
5285 | -- but in any case the back end should not count on this). | |
ee6ba406 | 5286 | |
d55c93e0 | 5287 | -- The back end also handles the non-class-wide cases of Size |
ee6ba406 | 5288 | |
5289 | when Attribute_Bit_Order | | |
5290 | Attribute_Code_Address | | |
5291 | Attribute_Definite | | |
ee6ba406 | 5292 | Attribute_Null_Parameter | |
9dfe12ae | 5293 | Attribute_Passed_By_Reference | |
5294 | Attribute_Pool_Address => | |
ee6ba406 | 5295 | null; |
5296 | ||
d55c93e0 | 5297 | -- The following attributes are also handled by the back end, but return |
5298 | -- a universal integer result, so may need a conversion for checking | |
ee6ba406 | 5299 | -- that the result is in range. |
5300 | ||
5301 | when Attribute_Aft | | |
aee37720 | 5302 | Attribute_Max_Alignment_For_Allocation | |
5303 | Attribute_Max_Size_In_Storage_Elements => | |
ee6ba406 | 5304 | Apply_Universal_Integer_Attribute_Checks (N); |
5305 | ||
5306 | -- The following attributes should not appear at this stage, since they | |
5307 | -- have already been handled by the analyzer (and properly rewritten | |
5308 | -- with corresponding values or entities to represent the right values) | |
5309 | ||
5310 | when Attribute_Abort_Signal | | |
5311 | Attribute_Address_Size | | |
5312 | Attribute_Base | | |
5313 | Attribute_Class | | |
a79db212 | 5314 | Attribute_Compiler_Version | |
ee6ba406 | 5315 | Attribute_Default_Bit_Order | |
5316 | Attribute_Delta | | |
5317 | Attribute_Denorm | | |
5318 | Attribute_Digits | | |
5319 | Attribute_Emax | | |
f947f061 | 5320 | Attribute_Enabled | |
ee6ba406 | 5321 | Attribute_Epsilon | |
f0bf2ff3 | 5322 | Attribute_Fast_Math | |
5c99c290 | 5323 | Attribute_Has_Access_Values | |
ee6ba406 | 5324 | Attribute_Has_Discriminants | |
d55c93e0 | 5325 | Attribute_Has_Tagged_Values | |
ee6ba406 | 5326 | Attribute_Large | |
5327 | Attribute_Machine_Emax | | |
5328 | Attribute_Machine_Emin | | |
5329 | Attribute_Machine_Mantissa | | |
5330 | Attribute_Machine_Overflows | | |
5331 | Attribute_Machine_Radix | | |
5332 | Attribute_Machine_Rounds | | |
ee6ba406 | 5333 | Attribute_Maximum_Alignment | |
5334 | Attribute_Model_Emin | | |
5335 | Attribute_Model_Epsilon | | |
5336 | Attribute_Model_Mantissa | | |
5337 | Attribute_Model_Small | | |
5338 | Attribute_Modulus | | |
5339 | Attribute_Partition_ID | | |
5340 | Attribute_Range | | |
5341 | Attribute_Safe_Emax | | |
5342 | Attribute_Safe_First | | |
5343 | Attribute_Safe_Large | | |
5344 | Attribute_Safe_Last | | |
5345 | Attribute_Safe_Small | | |
5346 | Attribute_Scale | | |
5347 | Attribute_Signed_Zeros | | |
5348 | Attribute_Small | | |
5349 | Attribute_Storage_Unit | | |
7f8eb6ed | 5350 | Attribute_Stub_Type | |
9dfe12ae | 5351 | Attribute_Target_Name | |
ee6ba406 | 5352 | Attribute_Type_Class | |
ec19aaaf | 5353 | Attribute_Type_Key | |
9dfe12ae | 5354 | Attribute_Unconstrained_Array | |
ee6ba406 | 5355 | Attribute_Universal_Literal_String | |
5356 | Attribute_Wchar_T_Size | | |
5357 | Attribute_Word_Size => | |
5358 | ||
5359 | raise Program_Error; | |
5360 | ||
5361 | -- The Asm_Input and Asm_Output attributes are not expanded at this | |
d55c93e0 | 5362 | -- stage, but will be eliminated in the expansion of the Asm call, see |
5363 | -- Exp_Intr for details. So the back end will never see these either. | |
ee6ba406 | 5364 | |
5365 | when Attribute_Asm_Input | | |
5366 | Attribute_Asm_Output => | |
5367 | ||
5368 | null; | |
5369 | ||
5370 | end case; | |
5371 | ||
9dfe12ae | 5372 | exception |
5373 | when RE_Not_Available => | |
5374 | return; | |
ee6ba406 | 5375 | end Expand_N_Attribute_Reference; |
5376 | ||
5377 | ---------------------- | |
5378 | -- Expand_Pred_Succ -- | |
5379 | ---------------------- | |
5380 | ||
5381 | -- For typ'Pred (exp), we generate the check | |
5382 | ||
5383 | -- [constraint_error when exp = typ'Base'First] | |
5384 | ||
5385 | -- Similarly, for typ'Succ (exp), we generate the check | |
5386 | ||
5387 | -- [constraint_error when exp = typ'Base'Last] | |
5388 | ||
5389 | -- These checks are not generated for modular types, since the proper | |
5390 | -- semantics for Succ and Pred on modular types is to wrap, not raise CE. | |
55e8372b | 5391 | -- We also suppress these checks if we are the right side of an assignment |
5392 | -- statement or the expression of an object declaration, where the flag | |
5393 | -- Suppress_Assignment_Checks is set for the assignment/declaration. | |
ee6ba406 | 5394 | |
5395 | procedure Expand_Pred_Succ (N : Node_Id) is | |
5396 | Loc : constant Source_Ptr := Sloc (N); | |
55e8372b | 5397 | P : constant Node_Id := Parent (N); |
ee6ba406 | 5398 | Cnam : Name_Id; |
5399 | ||
5400 | begin | |
5401 | if Attribute_Name (N) = Name_Pred then | |
5402 | Cnam := Name_First; | |
5403 | else | |
5404 | Cnam := Name_Last; | |
5405 | end if; | |
5406 | ||
55e8372b | 5407 | if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration) |
5408 | or else not Suppress_Assignment_Checks (P) | |
5409 | then | |
5410 | Insert_Action (N, | |
5411 | Make_Raise_Constraint_Error (Loc, | |
5412 | Condition => | |
5413 | Make_Op_Eq (Loc, | |
5414 | Left_Opnd => | |
5415 | Duplicate_Subexpr_Move_Checks (First (Expressions (N))), | |
5416 | Right_Opnd => | |
5417 | Make_Attribute_Reference (Loc, | |
5418 | Prefix => | |
5419 | New_Reference_To (Base_Type (Etype (Prefix (N))), Loc), | |
5420 | Attribute_Name => Cnam)), | |
5421 | Reason => CE_Overflow_Check_Failed)); | |
5422 | end if; | |
ee6ba406 | 5423 | end Expand_Pred_Succ; |
5424 | ||
1550b445 | 5425 | ------------------- |
5426 | -- Find_Fat_Info -- | |
5427 | ------------------- | |
5428 | ||
5429 | procedure Find_Fat_Info | |
5430 | (T : Entity_Id; | |
5431 | Fat_Type : out Entity_Id; | |
5432 | Fat_Pkg : out RE_Id) | |
5433 | is | |
5434 | Btyp : constant Entity_Id := Base_Type (T); | |
5435 | Rtyp : constant Entity_Id := Root_Type (T); | |
5436 | Digs : constant Nat := UI_To_Int (Digits_Value (Btyp)); | |
5437 | ||
5438 | begin | |
5439 | -- If the base type is VAX float, then get appropriate VAX float type | |
5440 | ||
5441 | if Vax_Float (Btyp) then | |
5442 | case Digs is | |
5443 | when 6 => | |
5444 | Fat_Type := RTE (RE_Fat_VAX_F); | |
5445 | Fat_Pkg := RE_Attr_VAX_F_Float; | |
5446 | ||
5447 | when 9 => | |
5448 | Fat_Type := RTE (RE_Fat_VAX_D); | |
5449 | Fat_Pkg := RE_Attr_VAX_D_Float; | |
5450 | ||
5451 | when 15 => | |
5452 | Fat_Type := RTE (RE_Fat_VAX_G); | |
5453 | Fat_Pkg := RE_Attr_VAX_G_Float; | |
5454 | ||
5455 | when others => | |
5456 | raise Program_Error; | |
5457 | end case; | |
5458 | ||
5459 | -- If root type is VAX float, this is the case where the library has | |
5460 | -- been recompiled in VAX float mode, and we have an IEEE float type. | |
5461 | -- This is when we use the special IEEE Fat packages. | |
5462 | ||
5463 | elsif Vax_Float (Rtyp) then | |
5464 | case Digs is | |
5465 | when 6 => | |
5466 | Fat_Type := RTE (RE_Fat_IEEE_Short); | |
5467 | Fat_Pkg := RE_Attr_IEEE_Short; | |
5468 | ||
5469 | when 15 => | |
5470 | Fat_Type := RTE (RE_Fat_IEEE_Long); | |
5471 | Fat_Pkg := RE_Attr_IEEE_Long; | |
5472 | ||
5473 | when others => | |
5474 | raise Program_Error; | |
5475 | end case; | |
5476 | ||
95b21580 | 5477 | -- If neither the base type nor the root type is VAX_Native then VAX |
1550b445 | 5478 | -- float is out of the picture, and we can just use the root type. |
5479 | ||
5480 | else | |
5481 | Fat_Type := Rtyp; | |
5482 | ||
5483 | if Fat_Type = Standard_Short_Float then | |
5484 | Fat_Pkg := RE_Attr_Short_Float; | |
7f8eb6ed | 5485 | |
1550b445 | 5486 | elsif Fat_Type = Standard_Float then |
5487 | Fat_Pkg := RE_Attr_Float; | |
7f8eb6ed | 5488 | |
1550b445 | 5489 | elsif Fat_Type = Standard_Long_Float then |
5490 | Fat_Pkg := RE_Attr_Long_Float; | |
7f8eb6ed | 5491 | |
1550b445 | 5492 | elsif Fat_Type = Standard_Long_Long_Float then |
5493 | Fat_Pkg := RE_Attr_Long_Long_Float; | |
7f8eb6ed | 5494 | |
5495 | -- Universal real (which is its own root type) is treated as being | |
5496 | -- equivalent to Standard.Long_Long_Float, since it is defined to | |
5497 | -- have the same precision as the longest Float type. | |
5498 | ||
5499 | elsif Fat_Type = Universal_Real then | |
5500 | Fat_Type := Standard_Long_Long_Float; | |
5501 | Fat_Pkg := RE_Attr_Long_Long_Float; | |
5502 | ||
1550b445 | 5503 | else |
5504 | raise Program_Error; | |
5505 | end if; | |
5506 | end if; | |
5507 | end Find_Fat_Info; | |
5508 | ||
9dfe12ae | 5509 | ---------------------------- |
5510 | -- Find_Stream_Subprogram -- | |
5511 | ---------------------------- | |
5512 | ||
5513 | function Find_Stream_Subprogram | |
5514 | (Typ : Entity_Id; | |
aad6babd | 5515 | Nam : TSS_Name_Type) return Entity_Id |
5516 | is | |
8667b0b2 | 5517 | Base_Typ : constant Entity_Id := Base_Type (Typ); |
5518 | Ent : constant Entity_Id := TSS (Typ, Nam); | |
d55c93e0 | 5519 | |
5236d9f4 | 5520 | function Is_Available (Entity : RE_Id) return Boolean; |
5521 | pragma Inline (Is_Available); | |
5522 | -- Function to check whether the specified run-time call is available | |
5523 | -- in the run time used. In the case of a configurable run time, it | |
5524 | -- is normal that some subprograms are not there. | |
5525 | ||
5526 | function Is_Available (Entity : RE_Id) return Boolean is | |
5527 | begin | |
5528 | -- Assume that the unit will always be available when using a | |
5529 | -- "normal" (not configurable) run time. | |
5530 | ||
5531 | return not Configurable_Run_Time_Mode | |
5532 | or else RTE_Available (Entity); | |
5533 | end Is_Available; | |
5534 | ||
9dfe12ae | 5535 | begin |
aad6babd | 5536 | if Present (Ent) then |
5537 | return Ent; | |
5538 | end if; | |
5539 | ||
d55c93e0 | 5540 | -- Stream attributes for strings are expanded into library calls. The |
5541 | -- following checks are disabled when the run-time is not available or | |
5542 | -- when compiling predefined types due to bootstrap issues. As a result, | |
5543 | -- the compiler will generate in-place stream routines for string types | |
5544 | -- that appear in GNAT's library, but will generate calls via rtsfind | |
5545 | -- to library routines for user code. | |
c2b89d6e | 5546 | |
d55c93e0 | 5547 | -- ??? For now, disable this code for JVM, since this generates a |
0c826ed4 | 5548 | -- VerifyError exception at run time on e.g. c330001. |
c2b89d6e | 5549 | |
5550 | -- This is disabled for AAMP, to avoid creating dependences on files not | |
d55c93e0 | 5551 | -- supported in the AAMP library (such as s-fileio.adb). |
5552 | ||
5236d9f4 | 5553 | -- In the case of using a configurable run time, it is very likely |
5554 | -- that stream routines for string types are not present (they require | |
5555 | -- file system support). In this case, the specific stream routines for | |
5556 | -- strings are not used, relying on the regular stream mechanism | |
5557 | -- instead. | |
5558 | ||
d55c93e0 | 5559 | if VM_Target /= JVM_Target |
5560 | and then not AAMP_On_Target | |
5561 | and then | |
5562 | not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit)) | |
5563 | then | |
d55c93e0 | 5564 | -- String as defined in package Ada |
5565 | ||
8667b0b2 | 5566 | if Base_Typ = Standard_String then |
b9f24e67 | 5567 | if Restriction_Active (No_Stream_Optimizations) then |
5236d9f4 | 5568 | if Nam = TSS_Stream_Input |
5569 | and then Is_Available (RE_String_Input) | |
5570 | then | |
b9f24e67 | 5571 | return RTE (RE_String_Input); |
5572 | ||
5236d9f4 | 5573 | elsif Nam = TSS_Stream_Output |
5574 | and then Is_Available (RE_String_Output) | |
5575 | then | |
b9f24e67 | 5576 | return RTE (RE_String_Output); |
5577 | ||
5236d9f4 | 5578 | elsif Nam = TSS_Stream_Read |
5579 | and then Is_Available (RE_String_Read) | |
5580 | then | |
b9f24e67 | 5581 | return RTE (RE_String_Read); |
d55c93e0 | 5582 | |
5236d9f4 | 5583 | elsif Nam = TSS_Stream_Write |
5584 | and then Is_Available (RE_String_Write) | |
5585 | then | |
b9f24e67 | 5586 | return RTE (RE_String_Write); |
5236d9f4 | 5587 | |
5588 | elsif Nam /= TSS_Stream_Input and then | |
5589 | Nam /= TSS_Stream_Output and then | |
5590 | Nam /= TSS_Stream_Read and then | |
5591 | Nam /= TSS_Stream_Write | |
5592 | then | |
5593 | raise Program_Error; | |
b9f24e67 | 5594 | end if; |
5595 | ||
5596 | else | |
5236d9f4 | 5597 | if Nam = TSS_Stream_Input |
5598 | and then Is_Available (RE_String_Input_Blk_IO) | |
5599 | then | |
b9f24e67 | 5600 | return RTE (RE_String_Input_Blk_IO); |
d55c93e0 | 5601 | |
5236d9f4 | 5602 | elsif Nam = TSS_Stream_Output |
5603 | and then Is_Available (RE_String_Output_Blk_IO) | |
5604 | then | |
b9f24e67 | 5605 | return RTE (RE_String_Output_Blk_IO); |
d55c93e0 | 5606 | |
5236d9f4 | 5607 | elsif Nam = TSS_Stream_Read |
5608 | and then Is_Available (RE_String_Read_Blk_IO) | |
5609 | then | |
b9f24e67 | 5610 | return RTE (RE_String_Read_Blk_IO); |
5611 | ||
5236d9f4 | 5612 | elsif Nam = TSS_Stream_Write |
5613 | and then Is_Available (RE_String_Write_Blk_IO) | |
5614 | then | |
b9f24e67 | 5615 | return RTE (RE_String_Write_Blk_IO); |
5236d9f4 | 5616 | |
5617 | elsif Nam /= TSS_Stream_Input and then | |
5618 | Nam /= TSS_Stream_Output and then | |
5619 | Nam /= TSS_Stream_Read and then | |
5620 | Nam /= TSS_Stream_Write | |
5621 | then | |
5622 | raise Program_Error; | |
b9f24e67 | 5623 | end if; |
d55c93e0 | 5624 | end if; |
5625 | ||
5626 | -- Wide_String as defined in package Ada | |
5627 | ||
8667b0b2 | 5628 | elsif Base_Typ = Standard_Wide_String then |
b9f24e67 | 5629 | if Restriction_Active (No_Stream_Optimizations) then |
5236d9f4 | 5630 | if Nam = TSS_Stream_Input |
5631 | and then Is_Available (RE_Wide_String_Input) | |
5632 | then | |
b9f24e67 | 5633 | return RTE (RE_Wide_String_Input); |
5634 | ||
5236d9f4 | 5635 | elsif Nam = TSS_Stream_Output |
5636 | and then Is_Available (RE_Wide_String_Output) | |
5637 | then | |
b9f24e67 | 5638 | return RTE (RE_Wide_String_Output); |
5639 | ||
5236d9f4 | 5640 | elsif Nam = TSS_Stream_Read |
5641 | and then Is_Available (RE_Wide_String_Read) | |
5642 | then | |
b9f24e67 | 5643 | return RTE (RE_Wide_String_Read); |
5644 | ||
5236d9f4 | 5645 | elsif Nam = TSS_Stream_Write |
5646 | and then Is_Available (RE_Wide_String_Write) | |
5647 | then | |
b9f24e67 | 5648 | return RTE (RE_Wide_String_Write); |
5236d9f4 | 5649 | |
5650 | elsif Nam /= TSS_Stream_Input and then | |
5651 | Nam /= TSS_Stream_Output and then | |
5652 | Nam /= TSS_Stream_Read and then | |
5653 | Nam /= TSS_Stream_Write | |
5654 | then | |
5655 | raise Program_Error; | |
b9f24e67 | 5656 | end if; |
5657 | ||
5658 | else | |
5236d9f4 | 5659 | if Nam = TSS_Stream_Input |
5660 | and then Is_Available (RE_Wide_String_Input_Blk_IO) | |
5661 | then | |
b9f24e67 | 5662 | return RTE (RE_Wide_String_Input_Blk_IO); |
d55c93e0 | 5663 | |
5236d9f4 | 5664 | elsif Nam = TSS_Stream_Output |
5665 | and then Is_Available (RE_Wide_String_Output_Blk_IO) | |
5666 | then | |
b9f24e67 | 5667 | return RTE (RE_Wide_String_Output_Blk_IO); |
d55c93e0 | 5668 | |
5236d9f4 | 5669 | elsif Nam = TSS_Stream_Read |
5670 | and then Is_Available (RE_Wide_String_Read_Blk_IO) | |
5671 | then | |
b9f24e67 | 5672 | return RTE (RE_Wide_String_Read_Blk_IO); |
d55c93e0 | 5673 | |
5236d9f4 | 5674 | elsif Nam = TSS_Stream_Write |
5675 | and then Is_Available (RE_Wide_String_Write_Blk_IO) | |
5676 | then | |
b9f24e67 | 5677 | return RTE (RE_Wide_String_Write_Blk_IO); |
5236d9f4 | 5678 | |
5679 | elsif Nam /= TSS_Stream_Input and then | |
5680 | Nam /= TSS_Stream_Output and then | |
5681 | Nam /= TSS_Stream_Read and then | |
5682 | Nam /= TSS_Stream_Write | |
5683 | then | |
5684 | raise Program_Error; | |
b9f24e67 | 5685 | end if; |
d55c93e0 | 5686 | end if; |
5687 | ||
5688 | -- Wide_Wide_String as defined in package Ada | |
5689 | ||
8667b0b2 | 5690 | elsif Base_Typ = Standard_Wide_Wide_String then |
b9f24e67 | 5691 | if Restriction_Active (No_Stream_Optimizations) then |
5236d9f4 | 5692 | if Nam = TSS_Stream_Input |
5693 | and then Is_Available (RE_Wide_Wide_String_Input) | |
5694 | then | |
b9f24e67 | 5695 | return RTE (RE_Wide_Wide_String_Input); |
5696 | ||
5236d9f4 | 5697 | elsif Nam = TSS_Stream_Output |
5698 | and then Is_Available (RE_Wide_Wide_String_Output) | |
5699 | then | |
b9f24e67 | 5700 | return RTE (RE_Wide_Wide_String_Output); |
d55c93e0 | 5701 | |
5236d9f4 | 5702 | elsif Nam = TSS_Stream_Read |
5703 | and then Is_Available (RE_Wide_Wide_String_Read) | |
5704 | then | |
b9f24e67 | 5705 | return RTE (RE_Wide_Wide_String_Read); |
d55c93e0 | 5706 | |
5236d9f4 | 5707 | elsif Nam = TSS_Stream_Write |
5708 | and then Is_Available (RE_Wide_Wide_String_Write) | |
5709 | then | |
b9f24e67 | 5710 | return RTE (RE_Wide_Wide_String_Write); |
5236d9f4 | 5711 | |
5712 | elsif Nam /= TSS_Stream_Input and then | |
5713 | Nam /= TSS_Stream_Output and then | |
5714 | Nam /= TSS_Stream_Read and then | |
5715 | Nam /= TSS_Stream_Write | |
5716 | then | |
5717 | raise Program_Error; | |
b9f24e67 | 5718 | end if; |
d55c93e0 | 5719 | |
b9f24e67 | 5720 | else |
5236d9f4 | 5721 | if Nam = TSS_Stream_Input |
5722 | and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO) | |
5723 | then | |
b9f24e67 | 5724 | return RTE (RE_Wide_Wide_String_Input_Blk_IO); |
5725 | ||
5236d9f4 | 5726 | elsif Nam = TSS_Stream_Output |
5727 | and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO) | |
5728 | then | |
b9f24e67 | 5729 | return RTE (RE_Wide_Wide_String_Output_Blk_IO); |
5730 | ||
5236d9f4 | 5731 | elsif Nam = TSS_Stream_Read |
5732 | and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO) | |
5733 | then | |
b9f24e67 | 5734 | return RTE (RE_Wide_Wide_String_Read_Blk_IO); |
5735 | ||
5236d9f4 | 5736 | elsif Nam = TSS_Stream_Write |
5737 | and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO) | |
5738 | then | |
b9f24e67 | 5739 | return RTE (RE_Wide_Wide_String_Write_Blk_IO); |
5236d9f4 | 5740 | |
5741 | elsif Nam /= TSS_Stream_Input and then | |
5742 | Nam /= TSS_Stream_Output and then | |
5743 | Nam /= TSS_Stream_Read and then | |
5744 | Nam /= TSS_Stream_Write | |
5745 | then | |
5746 | raise Program_Error; | |
b9f24e67 | 5747 | end if; |
d55c93e0 | 5748 | end if; |
5749 | end if; | |
5750 | end if; | |
5751 | ||
9dfe12ae | 5752 | if Is_Tagged_Type (Typ) |
5753 | and then Is_Derived_Type (Typ) | |
5754 | then | |
5755 | return Find_Prim_Op (Typ, Nam); | |
5756 | else | |
5757 | return Find_Inherited_TSS (Typ, Nam); | |
5758 | end if; | |
5759 | end Find_Stream_Subprogram; | |
5760 | ||
7af38999 | 5761 | --------------- |
5762 | -- Full_Base -- | |
5763 | --------------- | |
5764 | ||
5765 | function Full_Base (T : Entity_Id) return Entity_Id is | |
5766 | BT : Entity_Id; | |
5767 | ||
5768 | begin | |
5769 | BT := Base_Type (T); | |
5770 | ||
5771 | if Is_Private_Type (BT) | |
5772 | and then Present (Full_View (BT)) | |
5773 | then | |
5774 | BT := Full_View (BT); | |
5775 | end if; | |
5776 | ||
5777 | return BT; | |
5778 | end Full_Base; | |
5779 | ||
ee6ba406 | 5780 | ----------------------- |
5781 | -- Get_Index_Subtype -- | |
5782 | ----------------------- | |
5783 | ||
5784 | function Get_Index_Subtype (N : Node_Id) return Node_Id is | |
5785 | P_Type : Entity_Id := Etype (Prefix (N)); | |
5786 | Indx : Node_Id; | |
5787 | J : Int; | |
5788 | ||
5789 | begin | |
5790 | if Is_Access_Type (P_Type) then | |
5791 | P_Type := Designated_Type (P_Type); | |
5792 | end if; | |
5793 | ||
5794 | if No (Expressions (N)) then | |
5795 | J := 1; | |
5796 | else | |
5797 | J := UI_To_Int (Expr_Value (First (Expressions (N)))); | |
5798 | end if; | |
5799 | ||
5800 | Indx := First_Index (P_Type); | |
5801 | while J > 1 loop | |
5802 | Next_Index (Indx); | |
5803 | J := J - 1; | |
5804 | end loop; | |
5805 | ||
5806 | return Etype (Indx); | |
5807 | end Get_Index_Subtype; | |
5808 | ||
5245b786 | 5809 | ------------------------------- |
5810 | -- Get_Stream_Convert_Pragma -- | |
5811 | ------------------------------- | |
5812 | ||
5813 | function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is | |
5814 | Typ : Entity_Id; | |
5815 | N : Node_Id; | |
5816 | ||
5817 | begin | |
5818 | -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity | |
5819 | -- that a stream convert pragma for a tagged type is not inherited from | |
5820 | -- its parent. Probably what is wrong here is that it is basically | |
5821 | -- incorrect to consider a stream convert pragma to be a representation | |
5822 | -- pragma at all ??? | |
5823 | ||
5824 | N := First_Rep_Item (Implementation_Base_Type (T)); | |
5825 | while Present (N) loop | |
4c06b9d2 | 5826 | if Nkind (N) = N_Pragma |
5827 | and then Pragma_Name (N) = Name_Stream_Convert | |
5828 | then | |
5245b786 | 5829 | -- For tagged types this pragma is not inherited, so we |
5830 | -- must verify that it is defined for the given type and | |
5831 | -- not an ancestor. | |
5832 | ||
5833 | Typ := | |
5834 | Entity (Expression (First (Pragma_Argument_Associations (N)))); | |
5835 | ||
5836 | if not Is_Tagged_Type (T) | |
5837 | or else T = Typ | |
5838 | or else (Is_Private_Type (Typ) and then T = Full_View (Typ)) | |
5839 | then | |
5840 | return N; | |
5841 | end if; | |
5842 | end if; | |
5843 | ||
5844 | Next_Rep_Item (N); | |
5845 | end loop; | |
5846 | ||
5847 | return Empty; | |
5848 | end Get_Stream_Convert_Pragma; | |
5849 | ||
ee6ba406 | 5850 | --------------------------------- |
5851 | -- Is_Constrained_Packed_Array -- | |
5852 | --------------------------------- | |
5853 | ||
5854 | function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is | |
5855 | Arr : Entity_Id := Typ; | |
5856 | ||
5857 | begin | |
5858 | if Is_Access_Type (Arr) then | |
5859 | Arr := Designated_Type (Arr); | |
5860 | end if; | |
5861 | ||
5862 | return Is_Array_Type (Arr) | |
5863 | and then Is_Constrained (Arr) | |
5864 | and then Present (Packed_Array_Type (Arr)); | |
5865 | end Is_Constrained_Packed_Array; | |
5866 | ||
99f2248e | 5867 | ---------------------------------------- |
5868 | -- Is_Inline_Floating_Point_Attribute -- | |
5869 | ---------------------------------------- | |
5870 | ||
5871 | function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is | |
5872 | Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N)); | |
5873 | ||
5874 | begin | |
5875 | if Nkind (Parent (N)) /= N_Type_Conversion | |
5876 | or else not Is_Integer_Type (Etype (Parent (N))) | |
5877 | then | |
5878 | return False; | |
5879 | end if; | |
5880 | ||
5881 | -- Should also support 'Machine_Rounding and 'Unbiased_Rounding, but | |
5882 | -- required back end support has not been implemented yet ??? | |
5883 | ||
5884 | return Id = Attribute_Truncation; | |
5885 | end Is_Inline_Floating_Point_Attribute; | |
5886 | ||
ee6ba406 | 5887 | end Exp_Attr; |