Debian Codegraph @

gcc/ada/exp_imgv.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ I M G V --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Casing; use Casing;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Debug; use Debug;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Put_Image;
35 with Exp_Util; use Exp_Util;
36 with Lib; use Lib;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
40 with Opt; use Opt;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Rtsfind; use Rtsfind;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Res; use Sem_Res;
46 with Sem_Util; use Sem_Util;
47 with Sinfo; use Sinfo;
48 with Sinfo.Nodes; use Sinfo.Nodes;
49 with Sinfo.Utils; use Sinfo.Utils;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Stringt; use Stringt;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Ttypes; use Ttypes;
56 with Uintp; use Uintp;
57 with Urealp; use Urealp;
58
59 with System.Perfect_Hash_Generators;
60
61 package body Exp_Imgv is
62
63 procedure Rewrite_Object_Image
64 (N : Node_Id;
65 Pref : Node_Id;
66 Attr_Name : Name_Id;
67 Str_Typ : Entity_Id);
68 -- AI12-0124: Rewrite attribute 'Image when it is applied to an object
69 -- reference as an attribute applied to a type. N denotes the node to be
70 -- rewritten, Pref denotes the prefix of the 'Image attribute, and Name
71 -- and Str_Typ specify which specific string type and 'Image attribute to
72 -- apply (e.g. Name_Wide_Image and Standard_Wide_String).
73
74 ------------------------------------
75 -- Build_Enumeration_Image_Tables --
76 ------------------------------------
77
78 procedure Build_Enumeration_Image_Tables (E : Entity_Id; N : Node_Id) is
79 Loc : constant Source_Ptr := Sloc (E);
80 In_Main_Unit : constant Boolean := In_Extended_Main_Code_Unit (Loc);
81
82 Act : List_Id;
83 Eind : Entity_Id;
84 Estr : Entity_Id;
85 H_Id : Entity_Id;
86 H_OK : Boolean;
87 H_Sp : Node_Id;
88 Ind : List_Id;
89 Ityp : Node_Id;
90 Len : Nat;
91 Lit : Entity_Id;
92 Nlit : Nat;
93 S_Id : Entity_Id;
94 S_N : Nat := 0;
95 Str : String_Id;
96
97 package SPHG renames System.Perfect_Hash_Generators;
98
99 Saved_SSO : constant Character := Opt.Default_SSO;
100 -- Used to save the current scalar storage order during the generation
101 -- of the literal lookup table.
102
103 Serial_Number_Budget : constant := 50;
104 -- We may want to compute a perfect hash function for use by the Value
105 -- attribute. However computing this function is costly and, therefore,
106 -- cannot be done when compiling every unit where the enumeration type
107 -- is referenced, so we do it only when compiling the unit where it is
108 -- declared. This means that we may need to control the internal serial
109 -- numbers of this unit, or else we would risk generating public symbols
110 -- with mismatched names later on. The strategy for this is to allocate
111 -- a fixed budget of serial numbers to be spent from a specified point
112 -- until the end of the processing and to make sure that it is always
113 -- exactly spent on all possible paths from this point.
114
115 Threshold : constant Nat :=
116 (if Is_Library_Level_Entity (E)
117 or else not Always_Compatible_Rep_On_Target
118 then 3
119 else Nat'Last);
120 -- Threshold above which we want to generate the hash function in the
121 -- default case. We avoid doing it if this would cause a trampoline to
122 -- be generated because the type is local and descriptors are not used.
123
124 Threshold_For_Size : constant Nat := Nat'Max (Threshold, 9);
125 -- But the function and its tables take a bit of space so the threshold
126 -- is raised when compiling for size.
127
128 procedure Append_Table_To
129 (L : List_Id;
130 E : Entity_Id;
131 UB : Nat;
132 Ctyp : Entity_Id;
133 V : List_Id);
134 -- Append to L the declaration of E as a constant array of range 0 .. UB
135 -- and component type Ctyp with initial value V.
136
137 ---------------------
138 -- Append_Table_To --
139 ---------------------
140
141 procedure Append_Table_To
142 (L : List_Id;
143 E : Entity_Id;
144 UB : Nat;
145 Ctyp : Entity_Id;
146 V : List_Id)
147 is
148 begin
149 Append_To (L,
150 Make_Object_Declaration (Loc,
151 Defining_Identifier => E,
152 Constant_Present => True,
153 Object_Definition =>
154 Make_Constrained_Array_Definition (Loc,
155 Discrete_Subtype_Definitions => New_List (
156 Make_Range (Loc,
157 Low_Bound => Make_Integer_Literal (Loc, 0),
158 High_Bound => Make_Integer_Literal (Loc, UB))),
159 Component_Definition =>
160 Make_Component_Definition (Loc,
161 Subtype_Indication => New_Occurrence_Of (Ctyp, Loc))),
162 Expression => Make_Aggregate (Loc, Expressions => V)));
163 end Append_Table_To;
164
165 -- Start of Build_Enumeration_Image_Tables
166
167 begin
168 -- Nothing to do for types other than a root enumeration type
169
170 if E /= Root_Type (E) then
171 return;
172
173 -- Nothing to do if pragma Discard_Names applies
174
175 elsif Discard_Names (E) then
176 return;
177 end if;
178
179 -- Otherwise tables need constructing
180
181 Start_String;
182 Ind := New_List;
183 Lit := First_Literal (E);
184 Len := 1;
185 Nlit := 0;
186 H_OK := False;
187
188 loop
189 Append_To (Ind, Make_Integer_Literal (Loc, UI_From_Int (Len)));
190
191 exit when No (Lit);
192 Nlit := Nlit + 1;
193
194 Get_Unqualified_Decoded_Name_String (Chars (Lit));
195
196 if Name_Buffer (1) /= ''' then
197 Set_Casing (All_Upper_Case);
198 end if;
199
200 Store_String_Chars (Name_Buffer (1 .. Name_Len));
201 if In_Main_Unit then
202 SPHG.Insert (Name_Buffer (1 .. Name_Len));
203 end if;
204 Len := Len + Int (Name_Len);
205 Next_Literal (Lit);
206 end loop;
207
208 if Len < Int (2 ** (8 - 1)) then
209 Ityp := Standard_Integer_8;
210 elsif Len < Int (2 ** (16 - 1)) then
211 Ityp := Standard_Integer_16;
212 else
213 Ityp := Standard_Integer_32;
214 end if;
215
216 Str := End_String;
217
218 Estr :=
219 Make_Defining_Identifier (Loc,
220 Chars => New_External_Name (Chars (E), 'S'));
221
222 Eind :=
223 Make_Defining_Identifier (Loc,
224 Chars => New_External_Name (Chars (E), 'N'));
225
226 Set_Lit_Strings (E, Estr);
227 Set_Lit_Indexes (E, Eind);
228
229 -- Temporarily set the current scalar storage order to the default
230 -- during the generation of the literals table, since both the Image and
231 -- Value attributes rely on runtime routines for interpreting table
232 -- values.
233
234 Opt.Default_SSO := ' ';
235
236 -- Generate literal table
237
238 Act :=
239 New_List (
240 Make_Object_Declaration (Loc,
241 Defining_Identifier => Estr,
242 Constant_Present => True,
243 Object_Definition =>
244 New_Occurrence_Of (Standard_String, Loc),
245 Expression =>
246 Make_String_Literal (Loc,
247 Strval => Str)));
248
249 -- Generate index table
250
251 Append_Table_To (Act, Eind, Nlit, Ityp, Ind);
252
253 -- If the number of literals is not greater than Threshold, then we are
254 -- done. Otherwise we generate a (perfect) hash function for use by the
255 -- Value attribute.
256
257 if Nlit > Threshold then
258 -- We start to count serial numbers from here
259
260 S_N := Increment_Serial_Number;
261
262 -- Generate specification of hash function
263
264 H_Id :=
265 Make_Defining_Identifier (Loc,
266 Chars => New_External_Name (Chars (E), 'H'));
267 Mutate_Ekind (H_Id, E_Function);
268 Set_Is_Internal (H_Id);
269
270 if not Debug_Generated_Code then
271 Set_Debug_Info_Off (H_Id);
272 end if;
273
274 Set_Lit_Hash (E, H_Id);
275
276 S_Id := Make_Temporary (Loc, 'S');
277
278 H_Sp := Make_Function_Specification (Loc,
279 Defining_Unit_Name => H_Id,
280 Parameter_Specifications => New_List (
281 Make_Parameter_Specification (Loc,
282 Defining_Identifier => S_Id,
283 Parameter_Type =>
284 New_Occurrence_Of (Standard_String, Loc))),
285 Result_Definition =>
286 New_Occurrence_Of (Standard_Natural, Loc));
287
288 -- If the unit where the type is declared is the main unit, and the
289 -- number of literals is greater than Threshold_For_Size when we are
290 -- optimizing for size, and the restriction No_Implicit_Loops is not
291 -- active, and -gnatd_h is not specified, and not GNAT_Mode, generate
292 -- the hash function.
293
294 if In_Main_Unit
295 and then (Optimize_Size = 0 or else Nlit > Threshold_For_Size)
296 and then not Restriction_Active (No_Implicit_Loops)
297 and then not Debug_Flag_Underscore_H
298 and then not GNAT_Mode
299 then
300 declare
301 LB : constant Positive := 2 * Positive (Nlit) + 1;
302 UB : constant Positive := LB + 24;
303
304 begin
305 -- Try at most 25 * 4 times to compute the hash function before
306 -- giving up and using a linear search for the Value attribute.
307
308 for V in LB .. UB loop
309 begin
310 SPHG.Initialize (4321, V, SPHG.Memory_Space, Tries => 4);
311 SPHG.Compute ("");
312 H_OK := True;
313 exit;
314 exception
315 when SPHG.Too_Many_Tries => null;
316 end;
317 end loop;
318 end;
319 end if;
320
321 -- If the hash function has been successfully computed, 4 more tables
322 -- named P, T1, T2 and G are needed. The hash function is of the form
323
324 -- function Hash (S : String) return Natural is
325 -- xxxP : constant array (0 .. X) of Natural = [...];
326 -- xxxT1 : constant array (0 .. Y) of Index_Type = [...];
327 -- xxxT2 : constant array (0 .. Y) of Index_Type = [...];
328 -- xxxG : constant array (0 .. Z) of Index_Type = [...];
329
330 -- F : constant Natural := S'First - 1;
331 -- L : constant Natural := S'Length;
332 -- A, B : Natural := 0;
333 -- J : Natural;
334
335 -- begin
336 -- for K in P'Range loop
337 -- exit when L < P (K);
338 -- J := Character'Pos (S (P (K) + F));
339 -- A := (A + Natural (T1 (K) * J)) mod N;
340 -- B := (B + Natural (T2 (K) * J)) mod N;
341 -- end loop;
342
343 -- return (Natural (G (A)) + Natural (G (B))) mod M;
344 -- end Hash;
345
346 -- where N is the length of G and M the number of literals. Note that
347 -- we declare the tables inside the function for two reasons: first,
348 -- their analysis creates array subtypes and thus their concatenation
349 -- operators which are homonyms of the concatenation operator and may
350 -- change the homonym number of user operators declared in the scope;
351 -- second, the code generator can fold the values in the tables when
352 -- they are small and avoid emitting them in the final object code.
353
354 if H_OK then
355 declare
356 Siz, L1, L2 : Natural;
357 I : Int;
358
359 Pos, T1, T2, G : List_Id;
360 EPos, ET1, ET2, EG : Entity_Id;
361
362 F, L, A, B, J, K : Entity_Id;
363 Body_Decls : List_Id;
364 Body_Stmts : List_Id;
365 Loop_Stmts : List_Id;
366
367 begin
368 Body_Decls := New_List;
369
370 -- Generate position table
371
372 SPHG.Define (SPHG.Character_Position, Siz, L1, L2);
373 Pos := New_List;
374 for J in 0 .. L1 - 1 loop
375 I := Int (SPHG.Value (SPHG.Character_Position, J));
376 Append_To (Pos, Make_Integer_Literal (Loc, UI_From_Int (I)));
377 end loop;
378
379 EPos :=
380 Make_Defining_Identifier (Loc,
381 Chars => New_External_Name (Chars (E), 'P'));
382
383 Append_Table_To
384 (Body_Decls, EPos, Nat (L1 - 1), Standard_Natural, Pos);
385
386 -- Generate function table 1
387
388 SPHG.Define (SPHG.Function_Table_1, Siz, L1, L2);
389 T1 := New_List;
390 for J in 0 .. L1 - 1 loop
391 I := Int (SPHG.Value (SPHG.Function_Table_1, J));
392 Append_To (T1, Make_Integer_Literal (Loc, UI_From_Int (I)));
393 end loop;
394
395 ET1 :=
396 Make_Defining_Identifier (Loc,
397 Chars => New_External_Name (Chars (E), "T1"));
398
399 Ityp :=
400 Small_Integer_Type_For (UI_From_Int (Int (Siz)), Uns => True);
401 Append_Table_To (Body_Decls, ET1, Nat (L1 - 1), Ityp, T1);
402
403 -- Generate function table 2
404
405 SPHG.Define (SPHG.Function_Table_2, Siz, L1, L2);
406 T2 := New_List;
407 for J in 0 .. L1 - 1 loop
408 I := Int (SPHG.Value (SPHG.Function_Table_2, J));
409 Append_To (T2, Make_Integer_Literal (Loc, UI_From_Int (I)));
410 end loop;
411
412 ET2 :=
413 Make_Defining_Identifier (Loc,
414 Chars => New_External_Name (Chars (E), "T2"));
415
416 Ityp :=
417 Small_Integer_Type_For (UI_From_Int (Int (Siz)), Uns => True);
418 Append_Table_To (Body_Decls, ET2, Nat (L1 - 1), Ityp, T2);
419
420 -- Generate graph table
421
422 SPHG.Define (SPHG.Graph_Table, Siz, L1, L2);
423 G := New_List;
424 for J in 0 .. L1 - 1 loop
425 I := Int (SPHG.Value (SPHG.Graph_Table, J));
426 Append_To (G, Make_Integer_Literal (Loc, UI_From_Int (I)));
427 end loop;
428
429 EG :=
430 Make_Defining_Identifier (Loc,
431 Chars => New_External_Name (Chars (E), 'G'));
432
433 Ityp :=
434 Small_Integer_Type_For (UI_From_Int (Int (Siz)), Uns => True);
435 Append_Table_To (Body_Decls, EG, Nat (L1 - 1), Ityp, G);
436
437 F := Make_Temporary (Loc, 'F');
438
439 Append_To (Body_Decls,
440 Make_Object_Declaration (Loc,
441 Defining_Identifier => F,
442 Object_Definition =>
443 New_Occurrence_Of (Standard_Natural, Loc),
444 Expression =>
445 Make_Op_Subtract (Loc,
446 Left_Opnd =>
447 Make_Attribute_Reference (Loc,
448 Prefix => New_Occurrence_Of (S_Id, Loc),
449 Attribute_Name => Name_First),
450 Right_Opnd =>
451 Make_Integer_Literal (Loc, 1))));
452
453 L := Make_Temporary (Loc, 'L');
454
455 Append_To (Body_Decls,
456 Make_Object_Declaration (Loc,
457 Defining_Identifier => L,
458 Object_Definition =>
459 New_Occurrence_Of (Standard_Natural, Loc),
460 Expression =>
461 Make_Attribute_Reference (Loc,
462 Prefix => New_Occurrence_Of (S_Id, Loc),
463 Attribute_Name => Name_Length)));
464
465 A := Make_Temporary (Loc, 'A');
466
467 Append_To (Body_Decls,
468 Make_Object_Declaration (Loc,
469 Defining_Identifier => A,
470 Object_Definition =>
471 New_Occurrence_Of (Standard_Natural, Loc),
472 Expression => Make_Integer_Literal (Loc, 0)));
473
474 B := Make_Temporary (Loc, 'B');
475
476 Append_To (Body_Decls,
477 Make_Object_Declaration (Loc,
478 Defining_Identifier => B,
479 Object_Definition =>
480 New_Occurrence_Of (Standard_Natural, Loc),
481 Expression => Make_Integer_Literal (Loc, 0)));
482
483 J := Make_Temporary (Loc, 'J');
484
485 Append_To (Body_Decls,
486 Make_Object_Declaration (Loc,
487 Defining_Identifier => J,
488 Object_Definition =>
489 New_Occurrence_Of (Standard_Natural, Loc)));
490
491 K := Make_Temporary (Loc, 'K');
492
493 -- Generate exit when L < P (K);
494
495 Loop_Stmts := New_List (
496 Make_Exit_Statement (Loc,
497 Condition =>
498 Make_Op_Lt (Loc,
499 Left_Opnd => New_Occurrence_Of (L, Loc),
500 Right_Opnd =>
501 Make_Indexed_Component (Loc,
502 Prefix => New_Occurrence_Of (EPos, Loc),
503 Expressions => New_List (
504 New_Occurrence_Of (K, Loc))))));
505
506 -- Generate J := Character'Pos (S (P (K) + F));
507
508 Append_To (Loop_Stmts,
509 Make_Assignment_Statement (Loc,
510 Name => New_Occurrence_Of (J, Loc),
511 Expression =>
512 Make_Attribute_Reference (Loc,
513 Prefix =>
514 New_Occurrence_Of (Standard_Character, Loc),
515 Attribute_Name => Name_Pos,
516 Expressions => New_List (
517 Make_Indexed_Component (Loc,
518 Prefix => New_Occurrence_Of (S_Id, Loc),
519 Expressions => New_List (
520 Make_Op_Add (Loc,
521 Left_Opnd =>
522 Make_Indexed_Component (Loc,
523 Prefix =>
524 New_Occurrence_Of (EPos, Loc),
525 Expressions => New_List (
526 New_Occurrence_Of (K, Loc))),
527 Right_Opnd =>
528 New_Occurrence_Of (F, Loc))))))));
529
530 -- Generate A := (A + Natural (T1 (K) * J)) mod N;
531
532 Append_To (Loop_Stmts,
533 Make_Assignment_Statement (Loc,
534 Name => New_Occurrence_Of (A, Loc),
535 Expression =>
536 Make_Op_Mod (Loc,
537 Left_Opnd =>
538 Make_Op_Add (Loc,
539 Left_Opnd => New_Occurrence_Of (A, Loc),
540 Right_Opnd =>
541 Make_Op_Multiply (Loc,
542 Left_Opnd =>
543 Convert_To (Standard_Natural,
544 Make_Indexed_Component (Loc,
545 Prefix =>
546 New_Occurrence_Of (ET1, Loc),
547 Expressions => New_List (
548 New_Occurrence_Of (K, Loc)))),
549 Right_Opnd => New_Occurrence_Of (J, Loc))),
550 Right_Opnd => Make_Integer_Literal (Loc, Int (L1)))));
551
552 -- Generate B := (B + Natural (T2 (K) * J)) mod N;
553
554 Append_To (Loop_Stmts,
555 Make_Assignment_Statement (Loc,
556 Name => New_Occurrence_Of (B, Loc),
557 Expression =>
558 Make_Op_Mod (Loc,
559 Left_Opnd =>
560 Make_Op_Add (Loc,
561 Left_Opnd => New_Occurrence_Of (B, Loc),
562 Right_Opnd =>
563 Make_Op_Multiply (Loc,
564 Left_Opnd =>
565 Convert_To (Standard_Natural,
566 Make_Indexed_Component (Loc,
567 Prefix =>
568 New_Occurrence_Of (ET2, Loc),
569 Expressions => New_List (
570 New_Occurrence_Of (K, Loc)))),
571 Right_Opnd => New_Occurrence_Of (J, Loc))),
572 Right_Opnd => Make_Integer_Literal (Loc, Int (L1)))));
573
574 -- Generate loop
575
576 Body_Stmts := New_List (
577 Make_Implicit_Loop_Statement (N,
578 Iteration_Scheme =>
579 Make_Iteration_Scheme (Loc,
580 Loop_Parameter_Specification =>
581 Make_Loop_Parameter_Specification (Loc,
582 Defining_Identifier => K,
583 Discrete_Subtype_Definition =>
584 Make_Attribute_Reference (Loc,
585 Prefix =>
586 New_Occurrence_Of (EPos, Loc),
587 Attribute_Name => Name_Range))),
588 Statements => Loop_Stmts));
589
590 -- Generate return (Natural (G (A)) + Natural (G (B))) mod M;
591
592 Append_To (Body_Stmts,
593 Make_Simple_Return_Statement (Loc,
594 Expression =>
595 Make_Op_Mod (Loc,
596 Left_Opnd =>
597 Make_Op_Add (Loc,
598 Left_Opnd =>
599 Convert_To (Standard_Natural,
600 Make_Indexed_Component (Loc,
601 Prefix =>
602 New_Occurrence_Of (EG, Loc),
603 Expressions => New_List (
604 New_Occurrence_Of (A, Loc)))),
605 Right_Opnd =>
606 Convert_To (Standard_Natural,
607 Make_Indexed_Component (Loc,
608 Prefix =>
609 New_Occurrence_Of (EG, Loc),
610 Expressions => New_List (
611 New_Occurrence_Of (B, Loc))))),
612 Right_Opnd => Make_Integer_Literal (Loc, Nlit))));
613
614 -- Generate final body
615
616 Append_To (Act,
617 Make_Subprogram_Body (Loc,
618 Specification => H_Sp,
619 Declarations => Body_Decls,
620 Handled_Statement_Sequence =>
621 Make_Handled_Sequence_Of_Statements (Loc, Body_Stmts)));
622 end;
623
624 -- If we chose not to or did not manage to compute the hash function,
625 -- we need to build a dummy function always returning Natural'Last
626 -- because other units reference it if they use the Value attribute.
627
628 elsif In_Main_Unit then
629 declare
630 Body_Stmts : List_Id;
631
632 begin
633 -- Generate return Natural'Last
634
635 Body_Stmts := New_List (
636 Make_Simple_Return_Statement (Loc,
637 Expression =>
638 Make_Attribute_Reference (Loc,
639 Prefix =>
640 New_Occurrence_Of (Standard_Natural, Loc),
641 Attribute_Name => Name_Last)));
642
643 -- Generate body
644
645 Append_To (Act,
646 Make_Subprogram_Body (Loc,
647 Specification => H_Sp,
648 Declarations => Empty_List,
649 Handled_Statement_Sequence =>
650 Make_Handled_Sequence_Of_Statements (Loc, Body_Stmts)));
651 end;
652
653 -- For the other units, just declare the function
654
655 else
656 Append_To (Act,
657 Make_Subprogram_Declaration (Loc, Specification => H_Sp));
658 end if;
659
660 else
661 Set_Lit_Hash (E, Empty);
662 end if;
663
664 if In_Main_Unit then
665 System.Perfect_Hash_Generators.Finalize;
666 end if;
667
668 Insert_Actions (N, Act, Suppress => All_Checks);
669
670 -- This is where we check that our budget of serial numbers has been
671 -- entirely spent, see the declaration of Serial_Number_Budget above.
672
673 if Nlit > Threshold then
674 Synchronize_Serial_Number (S_N + Serial_Number_Budget);
675 end if;
676
677 -- Reset the scalar storage order to the saved value
678
679 Opt.Default_SSO := Saved_SSO;
680 end Build_Enumeration_Image_Tables;
681
682 ----------------------------
683 -- Expand_Image_Attribute --
684 ----------------------------
685
686 -- For all cases other than user-defined enumeration types, the scheme
687 -- is as follows. First we insert the following code:
688
689 -- Snn : String (1 .. rt'Width);
690 -- Pnn : Natural;
691 -- Image_xx (tv, Snn, Pnn [,pm]);
692 --
693 -- and then Expr is replaced by Snn (1 .. Pnn)
694
695 -- In the above expansion:
696
697 -- rt is the root type of the expression
698 -- tv is the expression with the value, usually a type conversion
699 -- pm is an extra parameter present in some cases
700
701 -- The following table shows tv, xx, and (if used) pm for the various
702 -- possible types of the argument:
703
704 -- For types whose root type is Character
705 -- xx = Character
706 -- tv = Character (Expr)
707
708 -- For types whose root type is Boolean
709 -- xx = Boolean
710 -- tv = Boolean (Expr)
711
712 -- For signed integer types
713 -- xx = [Long_Long_[Long_]]Integer
714 -- tv = [Long_Long_[Long_]]Integer (Expr)
715
716 -- For modular types
717 -- xx = [Long_Long_[Long_]]Unsigned
718 -- tv = System.Unsigned_Types.[Long_Long_[Long_]]Unsigned (Expr)
719
720 -- For types whose root type is Wide_Character
721 -- xx = Wide_Character
722 -- tv = Wide_Character (Expr)
723 -- pm = Boolean, true if Ada 2005 mode, False otherwise
724
725 -- For types whose root type is Wide_Wide_Character
726 -- xx = Wide_Wide_Character
727 -- tv = Wide_Wide_Character (Expr)
728
729 -- For floating-point types
730 -- xx = Floating_Point
731 -- tv = [Long_[Long_]]Float (Expr)
732 -- pm = typ'Digits (typ = subtype of expression)
733
734 -- For decimal fixed-point types
735 -- xx = Decimal{32,64,128}
736 -- tv = Integer_{32,64,128} (Expr)? [convert with no scaling]
737 -- pm = typ'Scale (typ = subtype of expression)
738
739 -- For the most common ordinary fixed-point types
740 -- xx = Fixed{32,64,128}
741 -- tv = Integer_{32,64,128} (Expr) [convert with no scaling]
742 -- pm = numerator of typ'Small (typ = subtype of expression)
743 -- denominator of typ'Small
744 -- (Integer_{32,64,128} x typ'Small)'Fore
745 -- typ'Aft
746
747 -- For other ordinary fixed-point types
748 -- xx = Fixed
749 -- tv = Long_Float (Expr)
750 -- pm = typ'Aft (typ = subtype of expression)
751
752 -- For enumeration types other than those declared in package Standard
753 -- or System, Snn, Pnn, are expanded as above, but the call looks like:
754
755 -- Image_Enumeration_NN (rt'Pos (X), Snn, Pnn, typS, typI'Address)
756
757 -- where rt is the root type of the expression, and typS and typI are
758 -- the entities constructed as described in the spec for the procedure
759 -- Build_Enumeration_Image_Tables and NN is 32/16/8 depending on the
760 -- element type of Lit_Indexes. The rewriting of the expression to
761 -- Snn (1 .. Pnn) then occurs as in the other cases. A special case is
762 -- when pragma Discard_Names applies, in which case we replace expr by:
763
764 -- (rt'Pos (expr))'Image
765
766 -- So that the result is a space followed by the decimal value for the
767 -- position of the enumeration value in the enumeration type.
768
769 procedure Expand_Image_Attribute (N : Node_Id) is
770 Loc : constant Source_Ptr := Sloc (N);
771 Exprs : constant List_Id := Expressions (N);
772 Expr : constant Node_Id := Relocate_Node (First (Exprs));
773 Pref : constant Node_Id := Prefix (N);
774
775 procedure Expand_Standard_Boolean_Image;
776 -- Expand attribute 'Image in Standard.Boolean, avoiding string copy
777
778 procedure Expand_User_Defined_Enumeration_Image (Typ : Entity_Id);
779 -- Expand attribute 'Image in user-defined enumeration types, avoiding
780 -- string copy.
781
782 -----------------------------------
783 -- Expand_Standard_Boolean_Image --
784 -----------------------------------
785
786 procedure Expand_Standard_Boolean_Image is
787 Ins_List : constant List_Id := New_List;
788 S1_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
789 T_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
790 F_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
791 V_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
792
793 begin
794 -- We use a single 5-character string subtype throughout so that the
795 -- subtype of the string if-expression is constrained and, therefore,
796 -- does not force the creation of a temporary during analysis.
797
798 -- Generate:
799 -- subtype S1 is String (1 .. 5);
800
801 Append_To (Ins_List,
802 Make_Subtype_Declaration (Loc,
803 Defining_Identifier => S1_Id,
804 Subtype_Indication =>
805 Make_Subtype_Indication (Loc,
806 Subtype_Mark =>
807 New_Occurrence_Of (Standard_String, Loc),
808 Constraint =>
809 Make_Index_Or_Discriminant_Constraint (Loc,
810 Constraints => New_List (
811 Make_Range (Loc,
812 Low_Bound => Make_Integer_Literal (Loc, 1),
813 High_Bound => Make_Integer_Literal (Loc, 5)))))));
814
815 -- Generate:
816 -- T : constant String (1 .. 5) := "TRUE ";
817
818 Start_String;
819 Store_String_Chars ("TRUE ");
820
821 Append_To (Ins_List,
822 Make_Object_Declaration (Loc,
823 Defining_Identifier => T_Id,
824 Object_Definition =>
825 New_Occurrence_Of (S1_Id, Loc),
826 Constant_Present => True,
827 Expression => Make_String_Literal (Loc, End_String)));
828
829 -- Generate:
830 -- F : constant String (1 .. 5) := "FALSE";
831
832 Start_String;
833 Store_String_Chars ("FALSE");
834
835 Append_To (Ins_List,
836 Make_Object_Declaration (Loc,
837 Defining_Identifier => F_Id,
838 Object_Definition =>
839 New_Occurrence_Of (S1_Id, Loc),
840 Constant_Present => True,
841 Expression => Make_String_Literal (Loc, End_String)));
842
843 -- Generate:
844 -- V : String (1 .. 5) renames (if Expr then T else F);
845
846 Append_To (Ins_List,
847 Make_Object_Renaming_Declaration (Loc,
848 Defining_Identifier => V_Id,
849 Subtype_Mark =>
850 New_Occurrence_Of (S1_Id, Loc),
851 Name =>
852 Make_If_Expression (Loc,
853 Expressions => New_List (
854 Duplicate_Subexpr (Expr),
855 New_Occurrence_Of (T_Id, Loc),
856 New_Occurrence_Of (F_Id, Loc)))));
857
858 -- Insert all the above declarations before N. We suppress checks
859 -- because everything is in range at this stage.
860
861 Insert_Actions (N, Ins_List, Suppress => All_Checks);
862
863 -- Final step is to rewrite the expression as a slice:
864 -- V (1 .. (if Expr then 4 else 5)) and analyze, again with no
865 -- checks, since we are sure that everything is OK.
866
867 Rewrite (N,
868 Make_Slice (Loc,
869 Prefix => New_Occurrence_Of (V_Id, Loc),
870 Discrete_Range =>
871 Make_Range (Loc,
872 Low_Bound => Make_Integer_Literal (Loc, 1),
873 High_Bound =>
874 Make_If_Expression (Loc,
875 Expressions => New_List (
876 Duplicate_Subexpr (Expr),
877 Make_Integer_Literal (Loc, 4),
878 Make_Integer_Literal (Loc, 5))))));
879
880 Analyze_And_Resolve (N, Standard_String, Suppress => All_Checks);
881 end Expand_Standard_Boolean_Image;
882
883 -------------------------------------------
884 -- Expand_User_Defined_Enumeration_Image --
885 -------------------------------------------
886
887 procedure Expand_User_Defined_Enumeration_Image (Typ : Entity_Id) is
888 Ins_List : constant List_Id := New_List;
889 P1_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
890 P2_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
891 P3_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
892 P4_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
893 S1_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
894
895 begin
896 -- Apply a validity check, since it is a bit drastic to get a
897 -- completely junk image value for an invalid value.
898
899 Insert_Valid_Check (Expr);
900
901 -- Generate:
902 -- P1 : constant Natural := Typ'Pos (Typ?(Expr));
903
904 Append_To (Ins_List,
905 Make_Object_Declaration (Loc,
906 Defining_Identifier => P1_Id,
907 Object_Definition =>
908 New_Occurrence_Of (Standard_Natural, Loc),
909 Constant_Present => True,
910 Expression =>
911 Convert_To (Standard_Natural,
912 Make_Attribute_Reference (Loc,
913 Attribute_Name => Name_Pos,
914 Prefix => New_Occurrence_Of (Typ, Loc),
915 Expressions => New_List (OK_Convert_To (Typ, Expr))))));
916
917 -- Compute the index of the string start, generating:
918 -- P2 : constant Natural := call_put_enumN (P1);
919
920 Append_To (Ins_List,
921 Make_Object_Declaration (Loc,
922 Defining_Identifier => P2_Id,
923 Object_Definition =>
924 New_Occurrence_Of (Standard_Natural, Loc),
925 Constant_Present => True,
926 Expression =>
927 Convert_To (Standard_Natural,
928 Make_Indexed_Component (Loc,
929 Prefix =>
930 New_Occurrence_Of (Lit_Indexes (Typ), Loc),
931 Expressions =>
932 New_List (New_Occurrence_Of (P1_Id, Loc))))));
933
934 -- Compute the index of the next value, generating:
935 -- P3 : constant Natural := call_put_enumN (P1 + 1);
936
937 declare
938 Add_Node : constant Node_Id :=
939 Make_Op_Add (Loc,
940 Left_Opnd => New_Occurrence_Of (P1_Id, Loc),
941 Right_Opnd => Make_Integer_Literal (Loc, Uint_1));
942
943 begin
944 Append_To (Ins_List,
945 Make_Object_Declaration (Loc,
946 Defining_Identifier => P3_Id,
947 Object_Definition =>
948 New_Occurrence_Of (Standard_Natural, Loc),
949 Constant_Present => True,
950 Expression =>
951 Convert_To (Standard_Natural,
952 Make_Indexed_Component (Loc,
953 Prefix =>
954 New_Occurrence_Of (Lit_Indexes (Typ), Loc),
955 Expressions =>
956 New_List (Add_Node)))));
957 end;
958
959 -- Generate:
960 -- P4 : String renames call_put_enumS (P2 .. P3 - 1);
961
962 declare
963 Sub_Node : constant Node_Id :=
964 Make_Op_Subtract (Loc,
965 Left_Opnd => New_Occurrence_Of (P3_Id, Loc),
966 Right_Opnd => Make_Integer_Literal (Loc, Uint_1));
967
968 begin
969 Append_To (Ins_List,
970 Make_Object_Renaming_Declaration (Loc,
971 Defining_Identifier => P4_Id,
972 Subtype_Mark =>
973 New_Occurrence_Of (Standard_String, Loc),
974 Name =>
975 Make_Slice (Loc,
976 Prefix =>
977 New_Occurrence_Of (Lit_Strings (Typ), Loc),
978 Discrete_Range =>
979 Make_Range (Loc,
980 Low_Bound => New_Occurrence_Of (P2_Id, Loc),
981 High_Bound => Sub_Node))));
982 end;
983
984 -- Generate:
985 -- subtype S1 is String (1 .. P3 - P2);
986
987 declare
988 HB : constant Node_Id :=
989 Make_Op_Subtract (Loc,
990 Left_Opnd => New_Occurrence_Of (P3_Id, Loc),
991 Right_Opnd => New_Occurrence_Of (P2_Id, Loc));
992
993 begin
994 Append_To (Ins_List,
995 Make_Subtype_Declaration (Loc,
996 Defining_Identifier => S1_Id,
997 Subtype_Indication =>
998 Make_Subtype_Indication (Loc,
999 Subtype_Mark =>
1000 New_Occurrence_Of (Standard_String, Loc),
1001 Constraint =>
1002 Make_Index_Or_Discriminant_Constraint (Loc,
1003 Constraints => New_List (
1004 Make_Range (Loc,
1005 Low_Bound => Make_Integer_Literal (Loc, 1),
1006 High_Bound => HB))))));
1007 end;
1008
1009 -- Insert all the above declarations before N. We suppress checks
1010 -- because everything is in range at this stage.
1011
1012 Insert_Actions (N, Ins_List, Suppress => All_Checks);
1013
1014 Rewrite (N,
1015 Unchecked_Convert_To (S1_Id, New_Occurrence_Of (P4_Id, Loc)));
1016
1017 Analyze_And_Resolve (N, Standard_String);
1018 end Expand_User_Defined_Enumeration_Image;
1019
1020 -- Local variables
1021
1022 Enum_Case : Boolean;
1023 Imid : RE_Id;
1024 Proc_Ent : Entity_Id;
1025 Ptyp : Entity_Id;
1026 Rtyp : Entity_Id;
1027 Tent : Entity_Id := Empty;
1028 Ttyp : Entity_Id;
1029
1030 Arg_List : List_Id;
1031 -- List of arguments for run-time procedure call
1032
1033 Ins_List : List_Id;
1034 -- List of actions to be inserted
1035
1036 Snn : constant Entity_Id := Make_Temporary (Loc, 'S');
1037 Pnn : constant Entity_Id := Make_Temporary (Loc, 'P');
1038
1039 -- Start of processing for Expand_Image_Attribute
1040
1041 begin
1042 if Is_Object_Image (Pref) then
1043 Rewrite_Object_Image (N, Pref, Name_Image, Standard_String);
1044 return;
1045 end if;
1046
1047 -- If Image should be transformed using Put_Image, then do so. See
1048 -- Exp_Put_Image for details.
1049
1050 if Exp_Put_Image.Image_Should_Call_Put_Image (N) then
1051 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
1052 Rewrite (N, Exp_Put_Image.Build_Image_Call (N));
1053 Analyze_And_Resolve (N, Standard_String, Suppress => All_Checks);
1054 return;
1055 end if;
1056
1057 Ptyp := Underlying_Type (Entity (Pref));
1058
1059 -- Ada 2022 allows 'Image on private types, so fetch the underlying
1060 -- type to obtain the structure of the type. We use the base type,
1061 -- not the root type, for discrete types in order to handle derived
1062 -- types, except for character types for which this is not needed.
1063
1064 if Is_Real_Type (Ptyp) or else Is_Character_Type (Ptyp) then
1065 Rtyp := Underlying_Type (Root_Type (Ptyp));
1066 else
1067 Rtyp := Underlying_Type (Base_Type (Ptyp));
1068 end if;
1069
1070 -- Set Imid (RE_Id of procedure to call), and Tent, target for the
1071 -- type conversion of the first argument for all possibilities.
1072
1073 Enum_Case := False;
1074
1075 if Is_Boolean_Type (Rtyp) then
1076 -- Use inline expansion if the -gnatd_x switch is not passed to the
1077 -- compiler. Otherwise expand into a call to the runtime.
1078
1079 if not Debug_Flag_Underscore_X then
1080 Expand_Standard_Boolean_Image;
1081 return;
1082
1083 else
1084 Imid := RE_Image_Boolean;
1085 Tent := Rtyp;
1086 end if;
1087
1088 -- For standard character, we have to select the version which handles
1089 -- soft hyphen correctly, based on the version of Ada in use (this is
1090 -- ugly, but we have no choice).
1091
1092 elsif Rtyp = Standard_Character then
1093 if Ada_Version < Ada_2005 then
1094 Imid := RE_Image_Character;
1095 else
1096 Imid := RE_Image_Character_05;
1097 end if;
1098
1099 Tent := Rtyp;
1100
1101 elsif Rtyp = Standard_Wide_Character then
1102 Imid := RE_Image_Wide_Character;
1103 Tent := Rtyp;
1104
1105 elsif Rtyp = Standard_Wide_Wide_Character then
1106 Imid := RE_Image_Wide_Wide_Character;
1107 Tent := Rtyp;
1108
1109 elsif Is_Signed_Integer_Type (Rtyp) then
1110 if Esize (Rtyp) <= Standard_Integer_Size then
1111 Imid := RE_Image_Integer;
1112 Tent := Standard_Integer;
1113 elsif Esize (Rtyp) <= Standard_Long_Long_Integer_Size then
1114 Imid := RE_Image_Long_Long_Integer;
1115 Tent := Standard_Long_Long_Integer;
1116 else
1117 Imid := RE_Image_Long_Long_Long_Integer;
1118 Tent := Standard_Long_Long_Long_Integer;
1119 end if;
1120
1121 elsif Is_Modular_Integer_Type (Rtyp) then
1122 if Modulus (Rtyp) <= Modulus (RTE (RE_Unsigned)) then
1123 Imid := RE_Image_Unsigned;
1124 Tent := RTE (RE_Unsigned);
1125 elsif Modulus (Rtyp) <= Modulus (RTE (RE_Long_Long_Unsigned)) then
1126 Imid := RE_Image_Long_Long_Unsigned;
1127 Tent := RTE (RE_Long_Long_Unsigned);
1128 else
1129 Imid := RE_Image_Long_Long_Long_Unsigned;
1130 Tent := RTE (RE_Long_Long_Long_Unsigned);
1131 end if;
1132
1133 elsif Is_Decimal_Fixed_Point_Type (Rtyp) then
1134 if Esize (Rtyp) <= 32 then
1135 Imid := RE_Image_Decimal32;
1136 Tent := RTE (RE_Integer_32);
1137 elsif Esize (Rtyp) <= 64 then
1138 Imid := RE_Image_Decimal64;
1139 Tent := RTE (RE_Integer_64);
1140 else
1141 Imid := RE_Image_Decimal128;
1142 Tent := RTE (RE_Integer_128);
1143 end if;
1144
1145 elsif Is_Ordinary_Fixed_Point_Type (Rtyp) then
1146 declare
1147 Num : constant Uint := Norm_Num (Small_Value (Rtyp));
1148 Den : constant Uint := Norm_Den (Small_Value (Rtyp));
1149 Max : constant Uint := UI_Max (Num, Den);
1150 Min : constant Uint := UI_Min (Num, Den);
1151 Siz : constant Uint := Esize (Rtyp);
1152
1153 begin
1154 -- Note that we do not use sharp bounds to speed things up
1155
1156 if Siz <= 32
1157 and then Max <= Uint_2 ** 31
1158 and then (Min = Uint_1
1159 or else (Num < Den and then Den <= Uint_2 ** 27)
1160 or else (Den < Num and then Num <= Uint_2 ** 25))
1161 then
1162 Imid := RE_Image_Fixed32;
1163 Tent := RTE (RE_Integer_32);
1164 elsif Siz <= 64
1165 and then Max <= Uint_2 ** 63
1166 and then (Min = Uint_1
1167 or else (Num < Den and then Den <= Uint_2 ** 59)
1168 or else (Den < Num and then Num <= Uint_2 ** 53))
1169 then
1170 Imid := RE_Image_Fixed64;
1171 Tent := RTE (RE_Integer_64);
1172 elsif System_Max_Integer_Size = 128
1173 and then Max <= Uint_2 ** 127
1174 and then (Min = Uint_1
1175 or else (Num < Den and then Den <= Uint_2 ** 123)
1176 or else (Den < Num and then Num <= Uint_2 ** 122))
1177 then
1178 Imid := RE_Image_Fixed128;
1179 Tent := RTE (RE_Integer_128);
1180 else
1181 Imid := RE_Image_Fixed;
1182 Tent := Standard_Long_Float;
1183 end if;
1184 end;
1185
1186 elsif Is_Floating_Point_Type (Rtyp) then
1187 -- Short_Float and Float are the same type for GNAT
1188
1189 if Rtyp = Standard_Short_Float or else Rtyp = Standard_Float then
1190 Imid := RE_Image_Float;
1191 Tent := Standard_Float;
1192
1193 elsif Rtyp = Standard_Long_Float then
1194 Imid := RE_Image_Long_Float;
1195 Tent := Standard_Long_Float;
1196
1197 else
1198 Imid := RE_Image_Long_Long_Float;
1199 Tent := Standard_Long_Long_Float;
1200 end if;
1201
1202 -- Only other possibility is user-defined enumeration type
1203
1204 else
1205 pragma Assert (Is_Enumeration_Type (Rtyp));
1206
1207 if Discard_Names (First_Subtype (Ptyp))
1208 or else No (Lit_Strings (Rtyp))
1209 then
1210 -- When pragma Discard_Names applies to the first subtype, build
1211 -- (Long_Long_Integer (Pref'Pos (Expr)))'Image. The conversion is
1212 -- there to avoid applying 'Image directly in Universal_Integer,
1213 -- which can be a very large type. See also the handling of 'Val.
1214
1215 Rewrite (N,
1216 Make_Attribute_Reference (Loc,
1217 Prefix =>
1218 Convert_To (Standard_Long_Long_Integer,
1219 Make_Attribute_Reference (Loc,
1220 Prefix => Pref,
1221 Attribute_Name => Name_Pos,
1222 Expressions => New_List (Expr))),
1223 Attribute_Name => Name_Image));
1224 Analyze_And_Resolve (N, Standard_String);
1225 return;
1226
1227 -- Use inline expansion if the -gnatd_x switch is not passed to the
1228 -- compiler. Otherwise expand into a call to the runtime.
1229
1230 elsif not Debug_Flag_Underscore_X then
1231 Expand_User_Defined_Enumeration_Image (Rtyp);
1232 return;
1233
1234 else
1235 Ttyp := Component_Type (Etype (Lit_Indexes (Rtyp)));
1236
1237 if Ttyp = Standard_Integer_8 then
1238 Imid := RE_Image_Enumeration_8;
1239
1240 elsif Ttyp = Standard_Integer_16 then
1241 Imid := RE_Image_Enumeration_16;
1242
1243 else
1244 Imid := RE_Image_Enumeration_32;
1245 end if;
1246
1247 -- Apply a validity check, since it is a bit drastic to get a
1248 -- completely junk image value for an invalid value.
1249
1250 Insert_Valid_Check (Expr);
1251
1252 Enum_Case := True;
1253 end if;
1254 end if;
1255
1256 -- Build first argument for call
1257
1258 if Enum_Case then
1259 Arg_List := New_List (
1260 Make_Attribute_Reference (Loc,
1261 Attribute_Name => Name_Pos,
1262 Prefix => New_Occurrence_Of (Ptyp, Loc),
1263 Expressions => New_List (Expr)));
1264
1265 -- AI12-0020: Ada 2022 allows 'Image for all types, including private
1266 -- types. If the full type is not a fixed-point type, then it is enough
1267 -- to set the Conversion_OK flag. However, that would not work for
1268 -- fixed-point types, because that flag changes the run-time semantics
1269 -- of fixed-point type conversions; therefore, we must first convert to
1270 -- Rtyp, and then to Tent.
1271
1272 else
1273 declare
1274 Conv : Node_Id;
1275
1276 begin
1277 if Is_Private_Type (Etype (Expr)) then
1278 if Is_Fixed_Point_Type (Rtyp) then
1279 Conv := Convert_To (Tent, OK_Convert_To (Rtyp, Expr));
1280 else
1281 Conv := OK_Convert_To (Tent, Expr);
1282 end if;
1283 else
1284 Conv := Convert_To (Tent, Expr);
1285 end if;
1286
1287 Arg_List := New_List (Conv);
1288 end;
1289 end if;
1290
1291 -- Build declarations of Snn and Pnn to be inserted
1292
1293 Ins_List := New_List (
1294
1295 -- Snn : String (1 .. typ'Width);
1296
1297 Make_Object_Declaration (Loc,
1298 Defining_Identifier => Snn,
1299 Object_Definition =>
1300 Make_Subtype_Indication (Loc,
1301 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1302 Constraint =>
1303 Make_Index_Or_Discriminant_Constraint (Loc,
1304 Constraints => New_List (
1305 Make_Range (Loc,
1306 Low_Bound => Make_Integer_Literal (Loc, 1),
1307 High_Bound =>
1308 Make_Attribute_Reference (Loc,
1309 Prefix => New_Occurrence_Of (Rtyp, Loc),
1310 Attribute_Name => Name_Width)))))),
1311
1312 -- Pnn : Natural;
1313
1314 Make_Object_Declaration (Loc,
1315 Defining_Identifier => Pnn,
1316 Object_Definition => New_Occurrence_Of (Standard_Natural, Loc)));
1317
1318 -- Append Snn, Pnn arguments
1319
1320 Append_To (Arg_List, New_Occurrence_Of (Snn, Loc));
1321 Append_To (Arg_List, New_Occurrence_Of (Pnn, Loc));
1322
1323 -- Get entity of procedure to call
1324
1325 Proc_Ent := RTE (Imid);
1326
1327 -- If the procedure entity is empty, that means we have a case in
1328 -- no run time mode where the operation is not allowed, and an
1329 -- appropriate diagnostic has already been issued.
1330
1331 if No (Proc_Ent) then
1332 return;
1333 end if;
1334
1335 -- Otherwise complete preparation of arguments for run-time call
1336
1337 -- Add extra arguments for Enumeration case
1338
1339 if Enum_Case then
1340 Append_To (Arg_List, New_Occurrence_Of (Lit_Strings (Rtyp), Loc));
1341 Append_To (Arg_List,
1342 Make_Attribute_Reference (Loc,
1343 Prefix => New_Occurrence_Of (Lit_Indexes (Rtyp), Loc),
1344 Attribute_Name => Name_Address));
1345
1346 -- For floating-point types, append Digits argument
1347
1348 elsif Is_Floating_Point_Type (Rtyp) then
1349 Append_To (Arg_List,
1350 Make_Attribute_Reference (Loc,
1351 Prefix => New_Occurrence_Of (Ptyp, Loc),
1352 Attribute_Name => Name_Digits));
1353
1354 -- For decimal, append Scale and also set to do literal conversion
1355
1356 elsif Is_Decimal_Fixed_Point_Type (Rtyp) then
1357 Set_Conversion_OK (First (Arg_List));
1358
1359 Append_To (Arg_List, Make_Integer_Literal (Loc, Scale_Value (Ptyp)));
1360
1361 -- For ordinary fixed-point types, append Num, Den, Fore, Aft parameters
1362 -- and also set to do literal conversion.
1363
1364 elsif Is_Ordinary_Fixed_Point_Type (Rtyp) then
1365 if Imid /= RE_Image_Fixed then
1366 Set_Conversion_OK (First (Arg_List));
1367
1368 Append_To (Arg_List,
1369 Make_Integer_Literal (Loc, -Norm_Num (Small_Value (Ptyp))));
1370
1371 Append_To (Arg_List,
1372 Make_Integer_Literal (Loc, -Norm_Den (Small_Value (Ptyp))));
1373
1374 -- We want to compute the Fore value for the fixed point type
1375 -- whose mantissa type is Tent and whose small is typ'Small.
1376
1377 declare
1378 T : Ureal := Uint_2 ** (Esize (Tent) - 1) * Small_Value (Ptyp);
1379 F : Nat := 2;
1380
1381 begin
1382 while T >= Ureal_10 loop
1383 F := F + 1;
1384 T := T / Ureal_10;
1385 end loop;
1386
1387 Append_To (Arg_List,
1388 Make_Integer_Literal (Loc, UI_From_Int (F)));
1389 end;
1390 end if;
1391
1392 Append_To (Arg_List, Make_Integer_Literal (Loc, Aft_Value (Ptyp)));
1393
1394 -- For Wide_Character, append Ada 2005 indication
1395
1396 elsif Rtyp = Standard_Wide_Character then
1397 Append_To (Arg_List,
1398 New_Occurrence_Of
1399 (Boolean_Literals (Ada_Version >= Ada_2005), Loc));
1400 end if;
1401
1402 -- Now append the procedure call to the insert list
1403
1404 Append_To (Ins_List,
1405 Make_Procedure_Call_Statement (Loc,
1406 Name => New_Occurrence_Of (Proc_Ent, Loc),
1407 Parameter_Associations => Arg_List));
1408
1409 -- Insert declarations of Snn, Pnn, and the procedure call. We suppress
1410 -- checks because we are sure that everything is in range at this stage.
1411
1412 Insert_Actions (N, Ins_List, Suppress => All_Checks);
1413
1414 -- Final step is to rewrite the expression as a slice and analyze,
1415 -- again with no checks, since we are sure that everything is OK.
1416
1417 Rewrite (N,
1418 Make_Slice (Loc,
1419 Prefix => New_Occurrence_Of (Snn, Loc),
1420 Discrete_Range =>
1421 Make_Range (Loc,
1422 Low_Bound => Make_Integer_Literal (Loc, 1),
1423 High_Bound => New_Occurrence_Of (Pnn, Loc))));
1424
1425 Analyze_And_Resolve (N, Standard_String, Suppress => All_Checks);
1426 end Expand_Image_Attribute;
1427
1428 ----------------------------------
1429 -- Expand_Valid_Value_Attribute --
1430 ----------------------------------
1431
1432 procedure Expand_Valid_Value_Attribute (N : Node_Id) is
1433 Loc : constant Source_Ptr := Sloc (N);
1434 Args : constant List_Id := Expressions (N);
1435 Btyp : constant Entity_Id := Base_Type (Entity (Prefix (N)));
1436 Rtyp : constant Entity_Id := Root_Type (Btyp);
1437 pragma Assert (Is_Enumeration_Type (Rtyp));
1438
1439 Func : RE_Id;
1440 Ttyp : Entity_Id;
1441
1442 begin
1443 -- Generate:
1444
1445 -- Valid_Value_Enumeration_NN
1446 -- (typS, typN'Address, typH'Unrestricted_Access, Num, Is_Wide, X)
1447
1448 Ttyp := Component_Type (Etype (Lit_Indexes (Rtyp)));
1449
1450 if Ttyp = Standard_Integer_8 then
1451 Func := RE_Valid_Value_Enumeration_8;
1452 elsif Ttyp = Standard_Integer_16 then
1453 Func := RE_Valid_Value_Enumeration_16;
1454 else
1455 Func := RE_Valid_Value_Enumeration_32;
1456 end if;
1457
1458 -- The Valid_[Wide_]Wide_Value attribute does not exist
1459
1460 Prepend_To (Args, New_Occurrence_Of (Standard_False, Loc));
1461
1462 Prepend_To (Args,
1463 Make_Attribute_Reference (Loc,
1464 Prefix => New_Occurrence_Of (Rtyp, Loc),
1465 Attribute_Name => Name_Pos,
1466 Expressions => New_List (
1467 Make_Attribute_Reference (Loc,
1468 Prefix => New_Occurrence_Of (Rtyp, Loc),
1469 Attribute_Name => Name_Last))));
1470
1471 if Present (Lit_Hash (Rtyp)) then
1472 Prepend_To (Args,
1473 Make_Attribute_Reference (Loc,
1474 Prefix => New_Occurrence_Of (Lit_Hash (Rtyp), Loc),
1475 Attribute_Name => Name_Unrestricted_Access));
1476 else
1477 Prepend_To (Args, Make_Null (Loc));
1478 end if;
1479
1480 Prepend_To (Args,
1481 Make_Attribute_Reference (Loc,
1482 Prefix => New_Occurrence_Of (Lit_Indexes (Rtyp), Loc),
1483 Attribute_Name => Name_Address));
1484
1485 Prepend_To (Args,
1486 New_Occurrence_Of (Lit_Strings (Rtyp), Loc));
1487
1488 Rewrite (N,
1489 Make_Function_Call (Loc,
1490 Name =>
1491 New_Occurrence_Of (RTE (Func), Loc),
1492 Parameter_Associations => Args));
1493
1494 Analyze_And_Resolve (N, Standard_Boolean);
1495 end Expand_Valid_Value_Attribute;
1496
1497 ----------------------------
1498 -- Expand_Value_Attribute --
1499 ----------------------------
1500
1501 -- For scalar types derived from Boolean, Character and integer types
1502 -- in package Standard, typ'Value (X) expands into:
1503
1504 -- btyp (Value_xx (X))
1505
1506 -- where btyp is the base type of the prefix
1507
1508 -- For types whose root type is Character
1509 -- xx = Character
1510
1511 -- For types whose root type is Wide_Character
1512 -- xx = Wide_Character
1513
1514 -- For types whose root type is Wide_Wide_Character
1515 -- xx = Wide_Wide_Character
1516
1517 -- For types whose root type is Boolean
1518 -- xx = Boolean
1519
1520 -- For signed integer types
1521 -- xx = [Long_Long_[Long_]]Integer
1522
1523 -- For modular types
1524 -- xx = [Long_Long_[Long_]]Unsigned
1525
1526 -- For floating-point types
1527 -- xx = [Long_[Long_]]Float
1528
1529 -- For decimal fixed-point types, typ'Value (X) expands into
1530
1531 -- btyp?(Value_Decimal{32,64,128} (X, typ'Scale));
1532
1533 -- For the most common ordinary fixed-point types, it expands into
1534
1535 -- btyp?(Value_Fixed{32,64,128} (X, numerator of S, denominator of S));
1536 -- where S = typ'Small
1537
1538 -- For other ordinary fixed-point types, it expands into
1539
1540 -- btyp (Value_Long_Float (X))
1541
1542 -- For Wide_[Wide_]Character types, typ'Value (X) expands into
1543
1544 -- btyp (Value_xx (X, EM))
1545
1546 -- where btyp is the base type of the prefix, and EM is the encoding method
1547
1548 -- For enumeration types other than those derived from types Boolean,
1549 -- Character, Wide_[Wide_]Character in Standard, typ'Value (X) expands to:
1550
1551 -- Enum'Val
1552 -- (Value_Enumeration_NN
1553 -- (typS, typN'Address, typH'Unrestricted_Access, Num, Is_Wide, X))
1554
1555 -- where typS, typN and typH are the Lit_Strings, Lit_Indexes and Lit_Hash
1556 -- entities from T's root type entity, and Num is Enum'Pos (Enum'Last).
1557 -- The Value_Enumeration_NN function will search the tables looking for
1558 -- X and return the position number in the table if found which is
1559 -- used to provide the result of 'Value (using Enum'Val). If the
1560 -- value is not found Constraint_Error is raised. The suffix _NN
1561 -- depends on the element type of typN.
1562
1563 procedure Expand_Value_Attribute (N : Node_Id) is
1564 Loc : constant Source_Ptr := Sloc (N);
1565 Args : constant List_Id := Expressions (N);
1566 Btyp : constant Entity_Id := Etype (N);
1567 pragma Assert (Is_Base_Type (Btyp));
1568 pragma Assert (Btyp = Base_Type (Entity (Prefix (N))));
1569 Rtyp : constant Entity_Id := Root_Type (Btyp);
1570
1571 Is_Wide : Boolean;
1572 Ttyp : Entity_Id;
1573 Vid : RE_Id;
1574
1575 begin
1576 -- Fall through for all cases except user-defined enumeration type
1577 -- and decimal types, with Vid set to the Id of the entity for the
1578 -- Value routine and Args set to the list of parameters for the call.
1579
1580 if Rtyp = Standard_Boolean then
1581 Vid := RE_Value_Boolean;
1582
1583 elsif Rtyp = Standard_Character then
1584 Vid := RE_Value_Character;
1585
1586 elsif Rtyp = Standard_Wide_Character then
1587 Vid := RE_Value_Wide_Character;
1588
1589 Append_To (Args,
1590 Make_Integer_Literal (Loc,
1591 Intval => Int (Wide_Character_Encoding_Method)));
1592
1593 elsif Rtyp = Standard_Wide_Wide_Character then
1594 Vid := RE_Value_Wide_Wide_Character;
1595
1596 Append_To (Args,
1597 Make_Integer_Literal (Loc,
1598 Intval => Int (Wide_Character_Encoding_Method)));
1599
1600 elsif Is_Signed_Integer_Type (Rtyp) then
1601 if Esize (Rtyp) <= Standard_Integer_Size then
1602 Vid := RE_Value_Integer;
1603 elsif Esize (Rtyp) <= Standard_Long_Long_Integer_Size then
1604 Vid := RE_Value_Long_Long_Integer;
1605 else
1606 Vid := RE_Value_Long_Long_Long_Integer;
1607 end if;
1608
1609 elsif Is_Modular_Integer_Type (Rtyp) then
1610 if Modulus (Rtyp) <= Modulus (RTE (RE_Unsigned)) then
1611 Vid := RE_Value_Unsigned;
1612 elsif Modulus (Rtyp) <= Modulus (RTE (RE_Long_Long_Unsigned)) then
1613 Vid := RE_Value_Long_Long_Unsigned;
1614 else
1615 Vid := RE_Value_Long_Long_Long_Unsigned;
1616 end if;
1617
1618 elsif Is_Decimal_Fixed_Point_Type (Rtyp) then
1619 if Esize (Rtyp) <= 32 and then abs Scale_Value (Rtyp) <= 9 then
1620 Vid := RE_Value_Decimal32;
1621 elsif Esize (Rtyp) <= 64 and then abs Scale_Value (Rtyp) <= 18 then
1622 Vid := RE_Value_Decimal64;
1623 else
1624 Vid := RE_Value_Decimal128;
1625 end if;
1626
1627 Append_To (Args, Make_Integer_Literal (Loc, Scale_Value (Rtyp)));
1628
1629 Rewrite (N,
1630 OK_Convert_To (Btyp,
1631 Make_Function_Call (Loc,
1632 Name => New_Occurrence_Of (RTE (Vid), Loc),
1633 Parameter_Associations => Args)));
1634
1635 Set_Etype (N, Btyp);
1636 Analyze_And_Resolve (N, Btyp);
1637 return;
1638
1639 elsif Is_Ordinary_Fixed_Point_Type (Rtyp) then
1640 declare
1641 Num : constant Uint := Norm_Num (Small_Value (Rtyp));
1642 Den : constant Uint := Norm_Den (Small_Value (Rtyp));
1643 Max : constant Uint := UI_Max (Num, Den);
1644 Min : constant Uint := UI_Min (Num, Den);
1645 Siz : constant Uint := Esize (Rtyp);
1646
1647 begin
1648 if Siz <= 32
1649 and then Max <= Uint_2 ** 31
1650 and then (Min = Uint_1 or else Max <= Uint_2 ** 27)
1651 then
1652 Vid := RE_Value_Fixed32;
1653 elsif Siz <= 64
1654 and then Max <= Uint_2 ** 63
1655 and then (Min = Uint_1 or else Max <= Uint_2 ** 59)
1656 then
1657 Vid := RE_Value_Fixed64;
1658 elsif System_Max_Integer_Size = 128
1659 and then Max <= Uint_2 ** 127
1660 and then (Min = Uint_1 or else Max <= Uint_2 ** 123)
1661 then
1662 Vid := RE_Value_Fixed128;
1663 else
1664 Vid := RE_Value_Long_Float;
1665 end if;
1666
1667 if Vid /= RE_Value_Long_Float then
1668 Append_To (Args,
1669 Make_Integer_Literal (Loc, -Norm_Num (Small_Value (Rtyp))));
1670
1671 Append_To (Args,
1672 Make_Integer_Literal (Loc, -Norm_Den (Small_Value (Rtyp))));
1673
1674 Rewrite (N,
1675 OK_Convert_To (Btyp,
1676 Make_Function_Call (Loc,
1677 Name => New_Occurrence_Of (RTE (Vid), Loc),
1678 Parameter_Associations => Args)));
1679
1680 Set_Etype (N, Btyp);
1681 Analyze_And_Resolve (N, Btyp);
1682 return;
1683 end if;
1684 end;
1685
1686 elsif Is_Floating_Point_Type (Rtyp) then
1687 -- Short_Float and Float are the same type for GNAT
1688
1689 if Rtyp = Standard_Short_Float or else Rtyp = Standard_Float then
1690 Vid := RE_Value_Float;
1691
1692 elsif Rtyp = Standard_Long_Float then
1693 Vid := RE_Value_Long_Float;
1694
1695 else
1696 Vid := RE_Value_Long_Long_Float;
1697 end if;
1698
1699 -- Only other possibility is user-defined enumeration type
1700
1701 else
1702 pragma Assert (Is_Enumeration_Type (Rtyp));
1703
1704 -- Case of pragma Discard_Names, transform the Value
1705 -- attribute to Btyp'Val (Long_Long_Integer'Value (Args))
1706
1707 if Discard_Names (First_Subtype (Btyp))
1708 or else No (Lit_Strings (Rtyp))
1709 then
1710 Rewrite (N,
1711 Make_Attribute_Reference (Loc,
1712 Prefix => New_Occurrence_Of (Btyp, Loc),
1713 Attribute_Name => Name_Val,
1714 Expressions => New_List (
1715 Make_Attribute_Reference (Loc,
1716 Prefix =>
1717 New_Occurrence_Of (Standard_Long_Long_Integer, Loc),
1718 Attribute_Name => Name_Value,
1719 Expressions => Args))));
1720
1721 Analyze_And_Resolve (N, Btyp);
1722
1723 -- Normal case where we have enumeration tables, build
1724
1725 -- T'Val
1726 -- (Value_Enumeration_NN
1727 -- (typS, typN'Address, typH'Unrestricted_Access, Num, Is_Wide, X))
1728
1729 else
1730 Ttyp := Component_Type (Etype (Lit_Indexes (Rtyp)));
1731
1732 if Ttyp = Standard_Integer_8 then
1733 Vid := RE_Value_Enumeration_8;
1734 elsif Ttyp = Standard_Integer_16 then
1735 Vid := RE_Value_Enumeration_16;
1736 else
1737 Vid := RE_Value_Enumeration_32;
1738 end if;
1739
1740 if Nkind (First (Args)) = N_Function_Call
1741 and then Is_Entity_Name (Name (First (Args)))
1742 then
1743 declare
1744 E : constant Entity_Id := Entity (Name (First (Args)));
1745
1746 begin
1747 Is_Wide := Is_RTE (E, RE_Enum_Wide_String_To_String)
1748 or else
1749 Is_RTE (E, RE_Enum_Wide_Wide_String_To_String);
1750 end;
1751
1752 else
1753 Is_Wide := False;
1754 end if;
1755
1756 Prepend_To (Args,
1757 New_Occurrence_Of (Boolean_Literals (Is_Wide), Loc));
1758
1759 Prepend_To (Args,
1760 Make_Attribute_Reference (Loc,
1761 Prefix => New_Occurrence_Of (Rtyp, Loc),
1762 Attribute_Name => Name_Pos,
1763 Expressions => New_List (
1764 Make_Attribute_Reference (Loc,
1765 Prefix => New_Occurrence_Of (Rtyp, Loc),
1766 Attribute_Name => Name_Last))));
1767
1768 if Present (Lit_Hash (Rtyp)) then
1769 Prepend_To (Args,
1770 Make_Attribute_Reference (Loc,
1771 Prefix => New_Occurrence_Of (Lit_Hash (Rtyp), Loc),
1772 Attribute_Name => Name_Unrestricted_Access));
1773 else
1774 Prepend_To (Args, Make_Null (Loc));
1775 end if;
1776
1777 Prepend_To (Args,
1778 Make_Attribute_Reference (Loc,
1779 Prefix => New_Occurrence_Of (Lit_Indexes (Rtyp), Loc),
1780 Attribute_Name => Name_Address));
1781
1782 Prepend_To (Args,
1783 New_Occurrence_Of (Lit_Strings (Rtyp), Loc));
1784
1785 Rewrite (N,
1786 Make_Attribute_Reference (Loc,
1787 Prefix => New_Occurrence_Of (Btyp, Loc),
1788 Attribute_Name => Name_Val,
1789 Expressions => New_List (
1790 Make_Function_Call (Loc,
1791 Name =>
1792 New_Occurrence_Of (RTE (Vid), Loc),
1793 Parameter_Associations => Args))));
1794
1795 Analyze_And_Resolve (N, Btyp);
1796 end if;
1797
1798 return;
1799 end if;
1800
1801 -- Compiling package Ada.Tags under No_Run_Time_Mode we disable the
1802 -- expansion of the attribute into the function call statement to avoid
1803 -- generating spurious errors caused by the use of Integer_Address'Value
1804 -- in our implementation of Ada.Tags.Internal_Tag.
1805
1806 if No_Run_Time_Mode
1807 and then Is_RTE (Rtyp, RE_Integer_Address)
1808 and then RTU_Loaded (Ada_Tags)
1809 and then Cunit_Entity (Current_Sem_Unit)
1810 = Body_Entity (RTU_Entity (Ada_Tags))
1811 then
1812 Rewrite (N,
1813 Unchecked_Convert_To (Rtyp,
1814 Make_Integer_Literal (Loc, Uint_0)));
1815
1816 else
1817 Rewrite (N,
1818 Convert_To (Btyp,
1819 Make_Function_Call (Loc,
1820 Name => New_Occurrence_Of (RTE (Vid), Loc),
1821 Parameter_Associations => Args)));
1822 end if;
1823
1824 Analyze_And_Resolve (N, Btyp);
1825 end Expand_Value_Attribute;
1826
1827 ---------------------------------
1828 -- Expand_Wide_Image_Attribute --
1829 ---------------------------------
1830
1831 -- We expand typ'Wide_Image (X) as follows. First we insert this code:
1832
1833 -- Rnn : Wide_String (1 .. rt'Wide_Width);
1834 -- Lnn : Natural;
1835 -- String_To_Wide_String
1836 -- (typ'Image (Expr), Rnn, Lnn, Wide_Character_Encoding_Method);
1837
1838 -- where rt is the root type of the prefix type
1839
1840 -- Now we replace the Wide_Image reference by
1841
1842 -- Rnn (1 .. Lnn)
1843
1844 -- This works in all cases because String_To_Wide_String converts any
1845 -- wide character escape sequences resulting from the Image call to the
1846 -- proper Wide_Character equivalent
1847
1848 -- not quite right for typ = Wide_Character ???
1849
1850 procedure Expand_Wide_Image_Attribute (N : Node_Id) is
1851 Loc : constant Source_Ptr := Sloc (N);
1852 Pref : constant Node_Id := Prefix (N);
1853 Rnn : constant Entity_Id := Make_Temporary (Loc, 'S');
1854 Lnn : constant Entity_Id := Make_Temporary (Loc, 'P');
1855 Rtyp : Entity_Id;
1856
1857 begin
1858 if Is_Object_Image (Pref) then
1859 Rewrite_Object_Image (N, Pref, Name_Wide_Image, Standard_Wide_String);
1860 return;
1861 end if;
1862
1863 -- If Image should be transformed using Put_Image, then do so. See
1864 -- Exp_Put_Image for details.
1865
1866 if Exp_Put_Image.Image_Should_Call_Put_Image (N) then
1867 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
1868 Rewrite (N, Exp_Put_Image.Build_Image_Call (N));
1869 Analyze_And_Resolve (N, Standard_Wide_String, Suppress => All_Checks);
1870 return;
1871 end if;
1872
1873 Rtyp := Root_Type (Entity (Pref));
1874
1875 Insert_Actions (N, New_List (
1876
1877 -- Rnn : Wide_String (1 .. base_typ'Width);
1878
1879 Make_Object_Declaration (Loc,
1880 Defining_Identifier => Rnn,
1881 Object_Definition =>
1882 Make_Subtype_Indication (Loc,
1883 Subtype_Mark =>
1884 New_Occurrence_Of (Standard_Wide_String, Loc),
1885 Constraint =>
1886 Make_Index_Or_Discriminant_Constraint (Loc,
1887 Constraints => New_List (
1888 Make_Range (Loc,
1889 Low_Bound => Make_Integer_Literal (Loc, 1),
1890 High_Bound =>
1891 Make_Attribute_Reference (Loc,
1892 Prefix => New_Occurrence_Of (Rtyp, Loc),
1893 Attribute_Name => Name_Wide_Width)))))),
1894
1895 -- Lnn : Natural;
1896
1897 Make_Object_Declaration (Loc,
1898 Defining_Identifier => Lnn,
1899 Object_Definition => New_Occurrence_Of (Standard_Natural, Loc)),
1900
1901 -- String_To_Wide_String
1902 -- (typ'Image (X), Rnn, Lnn, Wide_Character_Encoding_Method);
1903
1904 Make_Procedure_Call_Statement (Loc,
1905 Name =>
1906 New_Occurrence_Of (RTE (RE_String_To_Wide_String), Loc),
1907
1908 Parameter_Associations => New_List (
1909 Make_Attribute_Reference (Loc,
1910 Prefix => Prefix (N),
1911 Attribute_Name => Name_Image,
1912 Expressions => Expressions (N)),
1913 New_Occurrence_Of (Rnn, Loc),
1914 New_Occurrence_Of (Lnn, Loc),
1915 Make_Integer_Literal (Loc,
1916 Intval => Int (Wide_Character_Encoding_Method))))),
1917
1918 -- Suppress checks because we know everything is properly in range
1919
1920 Suppress => All_Checks);
1921
1922 -- Final step is to rewrite the expression as a slice and analyze,
1923 -- again with no checks, since we are sure that everything is OK.
1924
1925 Rewrite (N,
1926 Make_Slice (Loc,
1927 Prefix => New_Occurrence_Of (Rnn, Loc),
1928 Discrete_Range =>
1929 Make_Range (Loc,
1930 Low_Bound => Make_Integer_Literal (Loc, 1),
1931 High_Bound => New_Occurrence_Of (Lnn, Loc))));
1932
1933 Analyze_And_Resolve (N, Standard_Wide_String, Suppress => All_Checks);
1934 end Expand_Wide_Image_Attribute;
1935
1936 --------------------------------------
1937 -- Expand_Wide_Wide_Image_Attribute --
1938 --------------------------------------
1939
1940 -- We expand typ'Wide_Wide_Image (X) as follows. First we insert this code:
1941
1942 -- Rnn : Wide_Wide_String (1 .. rt'Wide_Wide_Width);
1943 -- Lnn : Natural;
1944 -- String_To_Wide_Wide_String
1945 -- (typ'Image (Expr), Rnn, Lnn, Wide_Character_Encoding_Method);
1946
1947 -- where rt is the root type of the prefix type
1948
1949 -- Now we replace the Wide_Wide_Image reference by
1950
1951 -- Rnn (1 .. Lnn)
1952
1953 -- This works in all cases because String_To_Wide_Wide_String converts any
1954 -- wide character escape sequences resulting from the Image call to the
1955 -- proper Wide_Wide_Character equivalent
1956
1957 -- not quite right for typ = Wide_Wide_Character ???
1958
1959 procedure Expand_Wide_Wide_Image_Attribute (N : Node_Id) is
1960 Loc : constant Source_Ptr := Sloc (N);
1961 Pref : constant Node_Id := Prefix (N);
1962 Rnn : constant Entity_Id := Make_Temporary (Loc, 'S');
1963 Lnn : constant Entity_Id := Make_Temporary (Loc, 'P');
1964 Rtyp : Entity_Id;
1965
1966 begin
1967 if Is_Object_Image (Pref) then
1968 Rewrite_Object_Image
1969 (N, Pref, Name_Wide_Wide_Image, Standard_Wide_Wide_String);
1970 return;
1971 end if;
1972
1973 -- If Image should be transformed using Put_Image, then do so. See
1974 -- Exp_Put_Image for details.
1975
1976 if Exp_Put_Image.Image_Should_Call_Put_Image (N) then
1977 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
1978 Rewrite (N, Exp_Put_Image.Build_Image_Call (N));
1979 Analyze_And_Resolve
1980 (N, Standard_Wide_Wide_String, Suppress => All_Checks);
1981 return;
1982 end if;
1983
1984 Rtyp := Root_Type (Entity (Pref));
1985
1986 Insert_Actions (N, New_List (
1987
1988 -- Rnn : Wide_Wide_String (1 .. rt'Wide_Wide_Width);
1989
1990 Make_Object_Declaration (Loc,
1991 Defining_Identifier => Rnn,
1992 Object_Definition =>
1993 Make_Subtype_Indication (Loc,
1994 Subtype_Mark =>
1995 New_Occurrence_Of (Standard_Wide_Wide_String, Loc),
1996 Constraint =>
1997 Make_Index_Or_Discriminant_Constraint (Loc,
1998 Constraints => New_List (
1999 Make_Range (Loc,
2000 Low_Bound => Make_Integer_Literal (Loc, 1),
2001 High_Bound =>
2002 Make_Attribute_Reference (Loc,
2003 Prefix => New_Occurrence_Of (Rtyp, Loc),
2004 Attribute_Name => Name_Wide_Wide_Width)))))),
2005
2006 -- Lnn : Natural;
2007
2008 Make_Object_Declaration (Loc,
2009 Defining_Identifier => Lnn,
2010 Object_Definition => New_Occurrence_Of (Standard_Natural, Loc)),
2011
2012 -- String_To_Wide_Wide_String
2013 -- (typ'Image (X), Rnn, Lnn, Wide_Character_Encoding_Method);
2014
2015 Make_Procedure_Call_Statement (Loc,
2016 Name =>
2017 New_Occurrence_Of (RTE (RE_String_To_Wide_Wide_String), Loc),
2018
2019 Parameter_Associations => New_List (
2020 Make_Attribute_Reference (Loc,
2021 Prefix => Prefix (N),
2022 Attribute_Name => Name_Image,
2023 Expressions => Expressions (N)),
2024 New_Occurrence_Of (Rnn, Loc),
2025 New_Occurrence_Of (Lnn, Loc),
2026 Make_Integer_Literal (Loc,
2027 Intval => Int (Wide_Character_Encoding_Method))))),
2028
2029 -- Suppress checks because we know everything is properly in range
2030
2031 Suppress => All_Checks);
2032
2033 -- Final step is to rewrite the expression as a slice and analyze,
2034 -- again with no checks, since we are sure that everything is OK.
2035
2036 Rewrite (N,
2037 Make_Slice (Loc,
2038 Prefix => New_Occurrence_Of (Rnn, Loc),
2039 Discrete_Range =>
2040 Make_Range (Loc,
2041 Low_Bound => Make_Integer_Literal (Loc, 1),
2042 High_Bound => New_Occurrence_Of (Lnn, Loc))));
2043
2044 Analyze_And_Resolve
2045 (N, Standard_Wide_Wide_String, Suppress => All_Checks);
2046 end Expand_Wide_Wide_Image_Attribute;
2047
2048 ----------------------------
2049 -- Expand_Width_Attribute --
2050 ----------------------------
2051
2052 -- The processing here also handles the case of Wide_[Wide_]Width. With the
2053 -- exceptions noted, the processing is identical
2054
2055 -- For scalar types derived from Boolean, character and integer types
2056 -- in package Standard. Note that the Width attribute is computed at
2057 -- compile time for all cases except those involving non-static sub-
2058 -- types. For such subtypes, typ'[Wide_[Wide_]]Width expands into:
2059
2060 -- Result_Type (xx (yy (Ptyp'First), yy (Ptyp'Last)))
2061
2062 -- where
2063
2064 -- For types whose root type is Character
2065 -- xx = Width_Character
2066 -- yy = Character
2067
2068 -- For types whose root type is Wide_Character
2069 -- xx = Wide_Width_Character
2070 -- yy = Character
2071
2072 -- For types whose root type is Wide_Wide_Character
2073 -- xx = Wide_Wide_Width_Character
2074 -- yy = Character
2075
2076 -- For types whose root type is Boolean
2077 -- xx = Width_Boolean
2078 -- yy = Boolean
2079
2080 -- For signed integer types
2081 -- xx = Width_[Long_Long_[Long_]]Integer
2082 -- yy = [Long_Long_[Long_]]Integer
2083
2084 -- For modular integer types
2085 -- xx = Width_[Long_Long_[Long_]]Unsigned
2086 -- yy = [Long_Long_[Long_]]Unsigned
2087
2088 -- For types derived from Wide_Character, typ'Width expands into
2089
2090 -- Result_Type (Width_Wide_Character (
2091 -- Wide_Character (typ'First),
2092 -- Wide_Character (typ'Last),
2093
2094 -- and typ'Wide_Width expands into:
2095
2096 -- Result_Type (Wide_Width_Wide_Character (
2097 -- Wide_Character (typ'First),
2098 -- Wide_Character (typ'Last));
2099
2100 -- and typ'Wide_Wide_Width expands into
2101
2102 -- Result_Type (Wide_Wide_Width_Wide_Character (
2103 -- Wide_Character (typ'First),
2104 -- Wide_Character (typ'Last));
2105
2106 -- For types derived from Wide_Wide_Character, typ'Width expands into
2107
2108 -- Result_Type (Width_Wide_Wide_Character (
2109 -- Wide_Wide_Character (typ'First),
2110 -- Wide_Wide_Character (typ'Last),
2111
2112 -- and typ'Wide_Width expands into:
2113
2114 -- Result_Type (Wide_Width_Wide_Wide_Character (
2115 -- Wide_Wide_Character (typ'First),
2116 -- Wide_Wide_Character (typ'Last));
2117
2118 -- and typ'Wide_Wide_Width expands into
2119
2120 -- Result_Type (Wide_Wide_Width_Wide_Wide_Char (
2121 -- Wide_Wide_Character (typ'First),
2122 -- Wide_Wide_Character (typ'Last));
2123
2124 -- For fixed point types, typ'Width and typ'Wide_[Wide_]Width expand into
2125
2126 -- if Ptyp'First > Ptyp'Last then 0 else Ptyp'Fore + 1 + Ptyp'Aft end if
2127
2128 -- and for floating point types, they expand into
2129
2130 -- if Ptyp'First > Ptyp'Last then 0 else btyp'Width end if
2131
2132 -- where btyp is the base type. This looks recursive but it isn't
2133 -- because the base type is always static, and hence the expression
2134 -- in the else is reduced to an integer literal.
2135
2136 -- For user-defined enumeration types, typ'Width expands into
2137
2138 -- Result_Type (Width_Enumeration_NN
2139 -- (typS,
2140 -- typI'Address,
2141 -- typ'Pos (typ'First),
2142 -- typ'Pos (Typ'Last)));
2143
2144 -- and typ'Wide_Width expands into:
2145
2146 -- Result_Type (Wide_Width_Enumeration_NN
2147 -- (typS,
2148 -- typI,
2149 -- typ'Pos (typ'First),
2150 -- typ'Pos (Typ'Last))
2151 -- Wide_Character_Encoding_Method);
2152
2153 -- and typ'Wide_Wide_Width expands into:
2154
2155 -- Result_Type (Wide_Wide_Width_Enumeration_NN
2156 -- (typS,
2157 -- typI,
2158 -- typ'Pos (typ'First),
2159 -- typ'Pos (Typ'Last))
2160 -- Wide_Character_Encoding_Method);
2161
2162 -- where typS and typI are the enumeration image strings and indexes
2163 -- table, as described in Build_Enumeration_Image_Tables. NN is 8/16/32
2164 -- for depending on the element type for typI.
2165
2166 -- Finally if Discard_Names is in effect for an enumeration type, then
2167 -- a special if expression is built that yields the space needed for the
2168 -- decimal representation of the largest pos value in the subtype. See
2169 -- code below for details.
2170
2171 procedure Expand_Width_Attribute (N : Node_Id; Attr : Atype := Normal) is
2172 Loc : constant Source_Ptr := Sloc (N);
2173 Typ : constant Entity_Id := Etype (N);
2174 Pref : constant Node_Id := Prefix (N);
2175 Ptyp : constant Entity_Id := Etype (Pref);
2176 Rtyp : constant Entity_Id := Root_Type (Ptyp);
2177 Arglist : List_Id;
2178 Ttyp : Entity_Id;
2179 XX : RE_Id;
2180 YY : Entity_Id;
2181
2182 begin
2183 -- Types derived from Standard.Boolean
2184
2185 if Rtyp = Standard_Boolean then
2186 XX := RE_Width_Boolean;
2187 YY := Rtyp;
2188
2189 -- Types derived from Standard.Character
2190
2191 elsif Rtyp = Standard_Character then
2192 case Attr is
2193 when Normal => XX := RE_Width_Character;
2194 when Wide => XX := RE_Wide_Width_Character;
2195 when Wide_Wide => XX := RE_Wide_Wide_Width_Character;
2196 end case;
2197
2198 YY := Rtyp;
2199
2200 -- Types derived from Standard.Wide_Character
2201
2202 elsif Rtyp = Standard_Wide_Character then
2203 case Attr is
2204 when Normal => XX := RE_Width_Wide_Character;
2205 when Wide => XX := RE_Wide_Width_Wide_Character;
2206 when Wide_Wide => XX := RE_Wide_Wide_Width_Wide_Character;
2207 end case;
2208
2209 YY := Rtyp;
2210
2211 -- Types derived from Standard.Wide_Wide_Character
2212
2213 elsif Rtyp = Standard_Wide_Wide_Character then
2214 case Attr is
2215 when Normal => XX := RE_Width_Wide_Wide_Character;
2216 when Wide => XX := RE_Wide_Width_Wide_Wide_Character;
2217 when Wide_Wide => XX := RE_Wide_Wide_Width_Wide_Wide_Char;
2218 end case;
2219
2220 YY := Rtyp;
2221
2222 -- Signed integer types
2223
2224 elsif Is_Signed_Integer_Type (Rtyp) then
2225 if Esize (Rtyp) <= Standard_Integer_Size then
2226 XX := RE_Width_Integer;
2227 YY := Standard_Integer;
2228 elsif Esize (Rtyp) <= Standard_Long_Long_Integer_Size then
2229 XX := RE_Width_Long_Long_Integer;
2230 YY := Standard_Long_Long_Integer;
2231 else
2232 XX := RE_Width_Long_Long_Long_Integer;
2233 YY := Standard_Long_Long_Long_Integer;
2234 end if;
2235
2236 -- Modular integer types
2237
2238 elsif Is_Modular_Integer_Type (Rtyp) then
2239 if Modulus (Rtyp) <= Modulus (RTE (RE_Unsigned)) then
2240 XX := RE_Width_Unsigned;
2241 YY := RTE (RE_Unsigned);
2242 elsif Modulus (Rtyp) <= Modulus (RTE (RE_Long_Long_Unsigned)) then
2243 XX := RE_Width_Long_Long_Unsigned;
2244 YY := RTE (RE_Long_Long_Unsigned);
2245 else
2246 XX := RE_Width_Long_Long_Long_Unsigned;
2247 YY := RTE (RE_Long_Long_Long_Unsigned);
2248 end if;
2249
2250 -- Fixed point types
2251
2252 elsif Is_Fixed_Point_Type (Rtyp) then
2253 Rewrite (N,
2254 Make_If_Expression (Loc,
2255 Expressions => New_List (
2256
2257 Make_Op_Gt (Loc,
2258 Left_Opnd =>
2259 Make_Attribute_Reference (Loc,
2260 Prefix => New_Occurrence_Of (Ptyp, Loc),
2261 Attribute_Name => Name_First),
2262
2263 Right_Opnd =>
2264 Make_Attribute_Reference (Loc,
2265 Prefix => New_Occurrence_Of (Ptyp, Loc),
2266 Attribute_Name => Name_Last)),
2267
2268 Make_Integer_Literal (Loc, 0),
2269
2270 Make_Op_Add (Loc,
2271 Make_Attribute_Reference (Loc,
2272 Prefix => New_Occurrence_Of (Ptyp, Loc),
2273 Attribute_Name => Name_Fore),
2274
2275 Make_Op_Add (Loc,
2276 Make_Integer_Literal (Loc, 1),
2277 Make_Integer_Literal (Loc, Aft_Value (Ptyp)))))));
2278
2279 Analyze_And_Resolve (N, Typ);
2280 return;
2281
2282 -- Floating point types
2283
2284 elsif Is_Floating_Point_Type (Rtyp) then
2285 Rewrite (N,
2286 Make_If_Expression (Loc,
2287 Expressions => New_List (
2288
2289 Make_Op_Gt (Loc,
2290 Left_Opnd =>
2291 Make_Attribute_Reference (Loc,
2292 Prefix => New_Occurrence_Of (Ptyp, Loc),
2293 Attribute_Name => Name_First),
2294
2295 Right_Opnd =>
2296 Make_Attribute_Reference (Loc,
2297 Prefix => New_Occurrence_Of (Ptyp, Loc),
2298 Attribute_Name => Name_Last)),
2299
2300 Make_Integer_Literal (Loc, 0),
2301
2302 Make_Attribute_Reference (Loc,
2303 Prefix => New_Occurrence_Of (Base_Type (Ptyp), Loc),
2304 Attribute_Name => Name_Width))));
2305
2306 Analyze_And_Resolve (N, Typ);
2307 return;
2308
2309 -- User-defined enumeration types
2310
2311 else
2312 pragma Assert (Is_Enumeration_Type (Rtyp));
2313
2314 -- Whenever pragma Discard_Names is in effect, the value we need
2315 -- is the value needed to accommodate the largest integer pos value
2316 -- in the range of the subtype + 1 for the space at the start. We
2317 -- build:
2318
2319 -- Tnn : constant Integer := Rtyp'Pos (Ptyp'Last);
2320
2321 -- and replace the expression by
2322
2323 -- (if Ptyp'Range_Length = 0 then 0
2324 -- else (if Tnn < 10 then 2
2325 -- else (if Tnn < 100 then 3
2326 -- ...
2327 -- else n)))...
2328
2329 -- where n is equal to Rtyp'Pos (Ptyp'Last) + 1
2330
2331 -- Note: The above processing is in accordance with the intent of
2332 -- the RM, which is that Width should be related to the impl-defined
2333 -- behavior of Image. It is not clear what this means if Image is
2334 -- not defined (as in the configurable run-time case for GNAT) and
2335 -- gives an error at compile time.
2336
2337 -- We choose in this case to just go ahead and implement Width the
2338 -- same way, returning what Image would have returned if it has been
2339 -- available in the configurable run-time library.
2340
2341 if Discard_Names (Rtyp) then
2342 declare
2343 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2344 Cexpr : Node_Id;
2345
2346 P : constant Nat :=
2347 UI_To_Int (Enumeration_Pos (Entity (Type_High_Bound (Rtyp))));
2348 -- The largest value that might need to be represented
2349
2350 K : Pos;
2351 M : Pos;
2352 -- K is the number of chars that will fit the image of 0..M-1;
2353 -- M is the smallest number that won't fit in K chars.
2354
2355 begin
2356 Insert_Action (N,
2357 Make_Object_Declaration (Loc,
2358 Defining_Identifier => Tnn,
2359 Constant_Present => True,
2360 Object_Definition =>
2361 New_Occurrence_Of (Standard_Integer, Loc),
2362 Expression =>
2363 Make_Attribute_Reference (Loc,
2364 Prefix => New_Occurrence_Of (Rtyp, Loc),
2365 Attribute_Name => Name_Pos,
2366 Expressions => New_List (
2367 Convert_To (Rtyp,
2368 Make_Attribute_Reference (Loc,
2369 Prefix => New_Occurrence_Of (Ptyp, Loc),
2370 Attribute_Name => Name_Last))))));
2371
2372 -- OK, now we need to build the if expression. First get the
2373 -- values of K and M for the largest possible value P.
2374
2375 K := 2;
2376 M := 10;
2377 -- With 2 characters we can represent values in 0..9
2378
2379 while P >= M loop
2380 M := M * 10;
2381 K := K + 1;
2382 end loop;
2383
2384 -- Build inner else
2385
2386 Cexpr := Make_Integer_Literal (Loc, K);
2387
2388 -- Wrap in inner if's until counted down to 2
2389
2390 while K > 2 loop
2391 M := M / 10;
2392 K := K - 1;
2393
2394 Cexpr :=
2395 Make_If_Expression (Loc,
2396 Expressions => New_List (
2397 Make_Op_Lt (Loc,
2398 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
2399 Right_Opnd => Make_Integer_Literal (Loc, M)),
2400 Make_Integer_Literal (Loc, K),
2401 Cexpr));
2402 end loop;
2403
2404 -- Add initial comparison for null range and we are done, so
2405 -- rewrite the attribute occurrence with this expression.
2406
2407 Rewrite (N,
2408 Convert_To (Typ,
2409 Make_If_Expression (Loc,
2410 Expressions => New_List (
2411 Make_Op_Eq (Loc,
2412 Left_Opnd =>
2413 Make_Attribute_Reference (Loc,
2414 Prefix => New_Occurrence_Of (Ptyp, Loc),
2415 Attribute_Name => Name_Range_Length),
2416 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2417 Make_Integer_Literal (Loc, 0),
2418 Cexpr))));
2419
2420 Analyze_And_Resolve (N, Typ);
2421 return;
2422 end;
2423 end if;
2424
2425 -- Normal case, not Discard_Names
2426
2427 Ttyp := Component_Type (Etype (Lit_Indexes (Rtyp)));
2428
2429 case Attr is
2430 when Normal =>
2431 if Ttyp = Standard_Integer_8 then
2432 XX := RE_Width_Enumeration_8;
2433 elsif Ttyp = Standard_Integer_16 then
2434 XX := RE_Width_Enumeration_16;
2435 else
2436 XX := RE_Width_Enumeration_32;
2437 end if;
2438
2439 when Wide =>
2440 if Ttyp = Standard_Integer_8 then
2441 XX := RE_Wide_Width_Enumeration_8;
2442 elsif Ttyp = Standard_Integer_16 then
2443 XX := RE_Wide_Width_Enumeration_16;
2444 else
2445 XX := RE_Wide_Width_Enumeration_32;
2446 end if;
2447
2448 when Wide_Wide =>
2449 if Ttyp = Standard_Integer_8 then
2450 XX := RE_Wide_Wide_Width_Enumeration_8;
2451 elsif Ttyp = Standard_Integer_16 then
2452 XX := RE_Wide_Wide_Width_Enumeration_16;
2453 else
2454 XX := RE_Wide_Wide_Width_Enumeration_32;
2455 end if;
2456 end case;
2457
2458 Arglist :=
2459 New_List (
2460 New_Occurrence_Of (Lit_Strings (Rtyp), Loc),
2461
2462 Make_Attribute_Reference (Loc,
2463 Prefix => New_Occurrence_Of (Lit_Indexes (Rtyp), Loc),
2464 Attribute_Name => Name_Address),
2465
2466 Make_Attribute_Reference (Loc,
2467 Prefix => New_Occurrence_Of (Ptyp, Loc),
2468 Attribute_Name => Name_Pos,
2469
2470 Expressions => New_List (
2471 Make_Attribute_Reference (Loc,
2472 Prefix => New_Occurrence_Of (Ptyp, Loc),
2473 Attribute_Name => Name_First))),
2474
2475 Make_Attribute_Reference (Loc,
2476 Prefix => New_Occurrence_Of (Ptyp, Loc),
2477 Attribute_Name => Name_Pos,
2478
2479 Expressions => New_List (
2480 Make_Attribute_Reference (Loc,
2481 Prefix => New_Occurrence_Of (Ptyp, Loc),
2482 Attribute_Name => Name_Last))));
2483
2484 Rewrite (N,
2485 Convert_To (Typ,
2486 Make_Function_Call (Loc,
2487 Name => New_Occurrence_Of (RTE (XX), Loc),
2488 Parameter_Associations => Arglist)));
2489
2490 Analyze_And_Resolve (N, Typ);
2491 return;
2492 end if;
2493
2494 -- If we fall through XX and YY are set
2495
2496 Arglist := New_List (
2497 Convert_To (YY,
2498 Make_Attribute_Reference (Loc,
2499 Prefix => New_Occurrence_Of (Ptyp, Loc),
2500 Attribute_Name => Name_First)),
2501
2502 Convert_To (YY,
2503 Make_Attribute_Reference (Loc,
2504 Prefix => New_Occurrence_Of (Ptyp, Loc),
2505 Attribute_Name => Name_Last)));
2506
2507 Rewrite (N,
2508 Convert_To (Typ,
2509 Make_Function_Call (Loc,
2510 Name => New_Occurrence_Of (RTE (XX), Loc),
2511 Parameter_Associations => Arglist)));
2512
2513 Analyze_And_Resolve (N, Typ);
2514 end Expand_Width_Attribute;
2515
2516 --------------------------
2517 -- Rewrite_Object_Image --
2518 --------------------------
2519
2520 procedure Rewrite_Object_Image
2521 (N : Node_Id;
2522 Pref : Node_Id;
2523 Attr_Name : Name_Id;
2524 Str_Typ : Entity_Id)
2525 is
2526 P : Node_Id;
2527 Ptyp : Entity_Id;
2528
2529 begin
2530 P := Pref;
2531 Ptyp := Etype (P);
2532
2533 -- If the type of the prefix is universal integer, which is a very large
2534 -- type, try to compute a narrower type. This may happen when the prefix
2535 -- itself is an attribute returning universal integer or a named number.
2536
2537 if Ptyp = Universal_Integer then
2538 if Nkind (P) in N_Type_Conversion | N_Unchecked_Type_Conversion then
2539 P := Expression (P);
2540 Ptyp := Etype (P);
2541
2542 elsif Nkind (P) = N_Integer_Literal then
2543 declare
2544 Val : constant Uint := Intval (P);
2545 Siz : constant Nat := Type_Size_For (Val);
2546
2547 begin
2548 if Siz <= System_Max_Integer_Size then
2549 Ptyp := Integer_Type_For (UI_From_Int (Siz), Val >= Uint_0);
2550 end if;
2551 end;
2552 end if;
2553 end if;
2554
2555 -- If the prefix is a component that depends on a discriminant, then
2556 -- create an actual subtype for it.
2557
2558 if Nkind (P) = N_Selected_Component then
2559 declare
2560 Decl : constant Node_Id :=
2561 Build_Actual_Subtype_Of_Component (Ptyp, P);
2562 begin
2563 if Present (Decl) then
2564 Insert_Action (N, Decl);
2565 Ptyp := Defining_Identifier (Decl);
2566 end if;
2567 end;
2568 end if;
2569
2570 Rewrite (N,
2571 Make_Attribute_Reference (Sloc (N),
2572 Prefix => New_Occurrence_Of (Ptyp, Sloc (N)),
2573 Attribute_Name => Attr_Name,
2574 Expressions => New_List (Unchecked_Convert_To (Ptyp, P))));
2575
2576 Analyze_And_Resolve (N, Str_Typ);
2577 end Rewrite_Object_Image;
2578 end Exp_Imgv;