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