Debian Codegraph @

gcc/ada/exp_ch2.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Exp_Smem; use Exp_Smem;
35 with Exp_Tss; use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Output; use Output;
42 with Rtsfind; use Rtsfind;
43 with Sem; use Sem;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Res; use Sem_Res;
46 with Sem_Util; use Sem_Util;
47 with Sem_Warn; use Sem_Warn;
48 with Sinfo; use Sinfo;
49 with Sinfo.Nodes; use Sinfo.Nodes;
50 with Sinfo.Utils; use Sinfo.Utils;
51 with Sinput; use Sinput;
52 with Snames; use Snames;
53 with Stand;
54 with Stringt; use Stringt;
55 with Tbuild; use Tbuild;
56 with Uintp; use Uintp;
57
58 package body Exp_Ch2 is
59
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
63
64 procedure Expand_Current_Value (N : Node_Id);
65 -- N is a node for a variable whose Current_Value field is set. If N is
66 -- node is for a discrete type, replaces node with a copy of the referenced
67 -- value. This provides a limited form of value propagation for variables
68 -- which are initialized or assigned not been further modified at the time
69 -- of reference. The call has no effect if the Current_Value refers to a
70 -- conditional with condition other than equality.
71
72 procedure Expand_Discriminant (N : Node_Id);
73 -- An occurrence of a discriminant within a discriminated type is replaced
74 -- with the corresponding discriminal, that is to say the formal parameter
75 -- of the initialization procedure for the type that is associated with
76 -- that particular discriminant. This replacement is not performed for
77 -- discriminants of records that appear in constraints of component of the
78 -- record, because Gigi uses the discriminant name to retrieve its value.
79 -- In the other hand, it has to be performed for default expressions of
80 -- components because they are used in the record init procedure. See Einfo
81 -- for more details, and Exp_Ch3, Exp_Ch9 for examples of use. For
82 -- discriminants of tasks and protected types, the transformation is more
83 -- complex when it occurs within a default expression for an entry or
84 -- protected operation. The corresponding default_expression_function has
85 -- an additional parameter which is the target of an entry call, and the
86 -- discriminant of the task must be replaced with a reference to the
87 -- discriminant of that formal parameter.
88
89 procedure Expand_Entity_Reference (N : Node_Id);
90 -- Common processing for expansion of identifiers and expanded names
91 -- Dispatches to specific expansion procedures.
92
93 procedure Expand_Entry_Index_Parameter (N : Node_Id);
94 -- A reference to the identifier in the entry index specification of an
95 -- entry body is modified to a reference to a constant definition equal to
96 -- the index of the entry family member being called. This constant is
97 -- calculated as part of the elaboration of the expanded code for the body,
98 -- and is calculated from the object-wide entry index returned by Next_
99 -- Entry_Call.
100
101 procedure Expand_Entry_Parameter (N : Node_Id);
102 -- A reference to an entry parameter is modified to be a reference to the
103 -- corresponding component of the entry parameter record that is passed by
104 -- the runtime to the accept body procedure.
105
106 procedure Expand_Formal (N : Node_Id);
107 -- A reference to a formal parameter of a protected subprogram is expanded
108 -- into the corresponding formal of the unprotected procedure used to
109 -- represent the operation within the protected object. In other cases
110 -- Expand_Formal is a no-op.
111
112 procedure Expand_Protected_Component (N : Node_Id);
113 -- A reference to a private component of a protected type is expanded into
114 -- a reference to the corresponding prival in the current protected entry
115 -- or subprogram.
116
117 procedure Expand_Renaming (N : Node_Id);
118 -- For renamings, just replace the identifier by the corresponding
119 -- named expression. Note that this has been evaluated (see routine
120 -- Exp_Ch8.Expand_N_Object_Renaming.Evaluate_Name) so this gives
121 -- the correct renaming semantics.
122
123 --------------------------
124 -- Expand_Current_Value --
125 --------------------------
126
127 procedure Expand_Current_Value (N : Node_Id) is
128 Loc : constant Source_Ptr := Sloc (N);
129 E : constant Entity_Id := Entity (N);
130 CV : constant Node_Id := Current_Value (E);
131 T : constant Entity_Id := Etype (N);
132 Val : Node_Id;
133 Op : Node_Kind;
134
135 begin
136 if True
137
138 -- No replacement if value raises constraint error
139
140 and then Nkind (CV) /= N_Raise_Constraint_Error
141
142 -- Do this only for discrete types
143
144 and then Is_Discrete_Type (T)
145
146 -- Do not replace biased types, since it is problematic to
147 -- consistently generate a sensible constant value in this case.
148
149 and then not Has_Biased_Representation (T)
150
151 -- Do not replace lvalues
152
153 and then not Known_To_Be_Assigned (N)
154
155 -- Check that entity is suitable for replacement
156
157 and then OK_To_Do_Constant_Replacement (E)
158
159 -- Do not replace the prefixes of attribute references, since this
160 -- causes trouble with cases like 4'Size. Also for Name_Asm_Input and
161 -- Name_Asm_Output, don't do replacement anywhere, since we can have
162 -- lvalue references in the arguments.
163
164 and then not (Nkind (Parent (N)) = N_Attribute_Reference
165 and then
166 (Attribute_Name (Parent (N)) in Name_Asm_Input
167 | Name_Asm_Output
168 or else Prefix (Parent (N)) = N))
169 then
170 -- Case of Current_Value is a compile time known value
171
172 if Nkind (CV) in N_Subexpr then
173 Val := CV;
174
175 -- Case of Current_Value is an if expression reference
176
177 else
178 Get_Current_Value_Condition (N, Op, Val);
179
180 if Op /= N_Op_Eq then
181 return;
182 end if;
183 end if;
184
185 -- If constant value is an occurrence of an enumeration literal,
186 -- then we just make another occurrence of the same literal.
187
188 if Is_Entity_Name (Val)
189 and then Ekind (Entity (Val)) = E_Enumeration_Literal
190 then
191 Rewrite (N,
192 Unchecked_Convert_To (T,
193 New_Occurrence_Of (Entity (Val), Loc)));
194
195 -- If constant is of a character type, just make an appropriate
196 -- character literal, which will get the proper type.
197
198 elsif Is_Character_Type (T) then
199 Rewrite (N,
200 Make_Character_Literal (Loc,
201 Chars => Chars (Val),
202 Char_Literal_Value => Expr_Rep_Value (Val)));
203
204 -- If constant is of an integer type, just make an appropriate
205 -- integer literal, which will get the proper type.
206
207 elsif Is_Integer_Type (T) then
208 Rewrite (N,
209 Make_Integer_Literal (Loc,
210 Intval => Expr_Rep_Value (Val)));
211
212 -- Otherwise do unchecked conversion of value to right type
213
214 else
215 Rewrite (N,
216 Unchecked_Convert_To (T,
217 Make_Integer_Literal (Loc,
218 Intval => Expr_Rep_Value (Val))));
219 end if;
220
221 Analyze_And_Resolve (N, T);
222 Set_Is_Static_Expression (N, False);
223 end if;
224 end Expand_Current_Value;
225
226 -------------------------
227 -- Expand_Discriminant --
228 -------------------------
229
230 procedure Expand_Discriminant (N : Node_Id) is
231 Scop : constant Entity_Id := Scope (Entity (N));
232 P : Node_Id := N;
233 Parent_P : Node_Id := Parent (P);
234 In_Entry : Boolean := False;
235
236 begin
237 -- The Incomplete_Or_Private_Kind happens while resolving the
238 -- discriminant constraint involved in a derived full type,
239 -- such as:
240
241 -- type D is private;
242 -- type D(C : ...) is new T(C);
243
244 if Ekind (Scop) = E_Record_Type
245 or Ekind (Scop) in Incomplete_Or_Private_Kind
246 then
247 -- Find the origin by walking up the tree till the component
248 -- declaration
249
250 while Present (Parent_P)
251 and then Nkind (Parent_P) /= N_Component_Declaration
252 loop
253 P := Parent_P;
254 Parent_P := Parent (P);
255 end loop;
256
257 -- If the discriminant reference was part of the default expression
258 -- it has to be "discriminalized"
259
260 if Present (Parent_P) and then P = Expression (Parent_P) then
261 Set_Entity (N, Discriminal (Entity (N)));
262 end if;
263
264 elsif Is_Concurrent_Type (Scop) then
265 while Present (Parent_P)
266 and then Nkind (Parent_P) /= N_Subprogram_Body
267 loop
268 P := Parent_P;
269
270 if Nkind (P) = N_Entry_Declaration then
271 In_Entry := True;
272 end if;
273
274 Parent_P := Parent (Parent_P);
275 end loop;
276
277 -- If the discriminant occurs within the default expression for a
278 -- formal of an entry or protected operation, replace it with a
279 -- reference to the discriminant of the formal of the enclosing
280 -- operation.
281
282 if Present (Parent_P)
283 and then Present (Corresponding_Spec (Parent_P))
284 then
285 declare
286 Loc : constant Source_Ptr := Sloc (N);
287 D_Fun : constant Entity_Id := Corresponding_Spec (Parent_P);
288 Formal : constant Entity_Id := First_Formal (D_Fun);
289 New_N : Node_Id;
290 Disc : Entity_Id;
291
292 begin
293 -- Verify that we are within the body of an entry or protected
294 -- operation. Its first formal parameter is the synchronized
295 -- type itself.
296
297 if Present (Formal)
298 and then Etype (Formal) = Scope (Entity (N))
299 then
300 Disc := CR_Discriminant (Entity (N));
301
302 New_N :=
303 Make_Selected_Component (Loc,
304 Prefix => New_Occurrence_Of (Formal, Loc),
305 Selector_Name => New_Occurrence_Of (Disc, Loc));
306
307 Set_Etype (New_N, Etype (N));
308 Rewrite (N, New_N);
309
310 else
311 Set_Entity (N, Discriminal (Entity (N)));
312 end if;
313 end;
314
315 elsif Nkind (Parent (N)) = N_Range
316 and then In_Entry
317 then
318 Set_Entity (N, CR_Discriminant (Entity (N)));
319
320 -- Finally, if the entity is the discriminant of the original
321 -- type declaration, and we are within the initialization
322 -- procedure for a task, the designated entity is the
323 -- discriminal of the task body. This can happen when the
324 -- argument of pragma Task_Name mentions a discriminant,
325 -- because the pragma is analyzed in the task declaration
326 -- but is expanded in the call to Create_Task in the init_proc.
327
328 elsif Within_Init_Proc then
329 Set_Entity (N, Discriminal (CR_Discriminant (Entity (N))));
330 else
331 Set_Entity (N, Discriminal (Entity (N)));
332 end if;
333
334 else
335 Set_Entity (N, Discriminal (Entity (N)));
336 end if;
337 end Expand_Discriminant;
338
339 -----------------------------
340 -- Expand_Entity_Reference --
341 -----------------------------
342
343 procedure Expand_Entity_Reference (N : Node_Id) is
344
345 function Is_Object_Renaming_Name (N : Node_Id) return Boolean;
346 -- Indicates that N occurs (after accounting for qualified expressions
347 -- and type conversions) as the name of an object renaming declaration.
348 -- We don't want to fold values in that case.
349
350 -----------------------------
351 -- Is_Object_Renaming_Name --
352 -----------------------------
353
354 function Is_Object_Renaming_Name (N : Node_Id) return Boolean is
355 Trailer : Node_Id := N;
356 Rover : Node_Id;
357 begin
358 loop
359 Rover := Parent (Trailer);
360 case Nkind (Rover) is
361 when N_Qualified_Expression | N_Type_Conversion =>
362 -- Conservative for type conversions; only necessary if
363 -- conversion does not introduce a new object (as opposed
364 -- to a new view of an existing object).
365 null;
366 when N_Object_Renaming_Declaration =>
367 return Trailer = Name (Rover);
368 when others =>
369 return False; -- the usual case
370 end case;
371 Trailer := Rover;
372 end loop;
373 end Is_Object_Renaming_Name;
374
375 -- Local variables
376
377 E : constant Entity_Id := Entity (N);
378
379 -- Start of processing for Expand_Entity_Reference
380
381 begin
382 -- Defend against errors
383
384 if No (E) then
385 Check_Error_Detected;
386 return;
387 end if;
388
389 if Ekind (E) = E_Discriminant then
390 Expand_Discriminant (N);
391
392 elsif Is_Entry_Formal (E) then
393 Expand_Entry_Parameter (N);
394
395 elsif Is_Protected_Component (E) then
396 if No_Run_Time_Mode then
397 return;
398 else
399 Expand_Protected_Component (N);
400 end if;
401
402 elsif Ekind (E) = E_Entry_Index_Parameter then
403 Expand_Entry_Index_Parameter (N);
404
405 elsif Is_Formal (E) then
406 Expand_Formal (N);
407
408 elsif Is_Renaming_Of_Object (E) then
409 Expand_Renaming (N);
410
411 elsif Ekind (E) = E_Variable
412 and then Is_Shared_Passive (E)
413 then
414 Expand_Shared_Passive_Variable (N);
415 end if;
416
417 -- Test code for implementing the pragma Reviewable requirement of
418 -- classifying reads of scalars as referencing potentially uninitialized
419 -- objects or not.
420
421 if Debug_Flag_XX
422 and then Is_Scalar_Type (Etype (N))
423 and then (Is_Assignable (E) or else Is_Constant_Object (E))
424 and then Comes_From_Source (N)
425 and then not Known_To_Be_Assigned (N)
426 and then not Is_Actual_Out_Parameter (N)
427 and then (Nkind (Parent (N)) /= N_Attribute_Reference
428 or else Attribute_Name (Parent (N)) /= Name_Valid)
429 then
430 Write_Location (Sloc (N));
431 Write_Str (": Read from scalar """);
432 Write_Name (Chars (N));
433 Write_Str ("""");
434
435 if Is_Known_Valid (E) then
436 Write_Str (", Is_Known_Valid");
437 end if;
438
439 Write_Eol;
440 end if;
441
442 -- Set Atomic_Sync_Required if necessary for atomic variable. Note that
443 -- this processing does NOT apply to Volatile_Full_Access variables.
444
445 if Nkind (N) in N_Identifier | N_Expanded_Name
446 and then Ekind (E) = E_Variable
447 and then (Is_Atomic (E) or else Is_Atomic (Etype (E)))
448 then
449 declare
450 Set : Boolean;
451
452 begin
453 -- If variable is atomic, but type is not, setting depends on
454 -- disable/enable state for the variable.
455
456 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
457 Set := not Atomic_Synchronization_Disabled (E);
458
459 -- If variable is not atomic, but its type is atomic, setting
460 -- depends on disable/enable state for the type.
461
462 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
463 Set := not Atomic_Synchronization_Disabled (Etype (E));
464
465 -- Else both variable and type are atomic (see outer if), and we
466 -- disable if either variable or its type have sync disabled.
467
468 else
469 Set := not Atomic_Synchronization_Disabled (E)
470 and then
471 not Atomic_Synchronization_Disabled (Etype (E));
472 end if;
473
474 -- Set flag if required
475
476 if Set then
477 Activate_Atomic_Synchronization (N);
478 end if;
479 end;
480 end if;
481
482 -- Interpret possible Current_Value for variable case. The
483 -- Is_Object_Renaming_Name test is needed for cases such as
484 -- X : Integer := 1;
485 -- Y : Integer renames Integer'(X);
486 -- where the value of Y is changed by any subsequent assignments to X.
487 -- In cases like this, we do not want to use Current_Value even though
488 -- it is available.
489
490 if Is_Assignable (E)
491 and then Present (Current_Value (E))
492 and then not Is_Object_Renaming_Name (N)
493 then
494 Expand_Current_Value (N);
495
496 -- We do want to warn for the case of a boolean variable (not a
497 -- boolean constant) whose value is known at compile time.
498
499 if Is_Boolean_Type (Etype (N)) then
500 Warn_On_Known_Condition (N);
501 end if;
502
503 -- Don't mess with Current_Value for compile time known values. Not
504 -- only is it unnecessary, but we could disturb an indication of a
505 -- static value, which could cause semantic trouble.
506
507 elsif Compile_Time_Known_Value (N) then
508 null;
509
510 -- Interpret possible Current_Value for constant case
511
512 elsif Is_Constant_Object (E)
513 and then Present (Current_Value (E))
514 then
515 Expand_Current_Value (N);
516 end if;
517 end Expand_Entity_Reference;
518
519 ----------------------------------
520 -- Expand_Entry_Index_Parameter --
521 ----------------------------------
522
523 procedure Expand_Entry_Index_Parameter (N : Node_Id) is
524 Index_Con : constant Entity_Id := Entry_Index_Constant (Entity (N));
525 begin
526 Set_Entity (N, Index_Con);
527 Set_Etype (N, Etype (Index_Con));
528 end Expand_Entry_Index_Parameter;
529
530 ----------------------------
531 -- Expand_Entry_Parameter --
532 ----------------------------
533
534 procedure Expand_Entry_Parameter (N : Node_Id) is
535 Loc : constant Source_Ptr := Sloc (N);
536 Ent_Formal : constant Entity_Id := Entity (N);
537 Ent_Spec : constant Entity_Id := Scope (Ent_Formal);
538 Parm_Type : constant Entity_Id := Entry_Parameters_Type (Ent_Spec);
539 Acc_Stack : constant Elist_Id := Accept_Address (Ent_Spec);
540 Addr_Ent : constant Entity_Id := Node (Last_Elmt (Acc_Stack));
541 P_Comp_Ref : Entity_Id;
542
543 -- Start of processing for Expand_Entry_Parameter
544
545 begin
546 if Is_Task_Type (Scope (Ent_Spec))
547 and then Comes_From_Source (Ent_Formal)
548 then
549 -- Before replacing the formal with the local renaming that is used
550 -- in the accept block, note if this is an assignment context, and
551 -- note the modification to avoid spurious warnings, because the
552 -- original entity is not used further. If formal is unconstrained,
553 -- we also generate an extra parameter to hold the Constrained
554 -- attribute of the actual. No renaming is generated for this flag.
555
556 -- Calling Note_Possible_Modification in the expander is dubious,
557 -- because this generates a cross-reference entry, and should be
558 -- done during semantic processing so it is called in -gnatc mode???
559
560 if Ekind (Entity (N)) /= E_In_Parameter
561 and then Known_To_Be_Assigned (N)
562 then
563 Note_Possible_Modification (N, Sure => True);
564 end if;
565 end if;
566
567 -- What we need is a reference to the corresponding component of the
568 -- parameter record object. The Accept_Address field of the entry entity
569 -- references the address variable that contains the address of the
570 -- accept parameters record. We first have to do an unchecked conversion
571 -- to turn this into a pointer to the parameter record and then we
572 -- select the required parameter field.
573
574 -- The same processing applies to protected entries, where the Accept_
575 -- Address is also the address of the Parameters record.
576
577 P_Comp_Ref :=
578 Make_Selected_Component (Loc,
579 Prefix =>
580 Make_Explicit_Dereference (Loc,
581 Unchecked_Convert_To (Parm_Type,
582 New_Occurrence_Of (Addr_Ent, Loc))),
583 Selector_Name =>
584 New_Occurrence_Of (Entry_Component (Ent_Formal), Loc));
585
586 -- For all types of parameters, the constructed parameter record object
587 -- contains a pointer to the parameter. Thus we must dereference them to
588 -- access them (this will often be redundant, since the dereference is
589 -- implicit, but no harm is done by making it explicit).
590
591 Rewrite (N,
592 Make_Explicit_Dereference (Loc, P_Comp_Ref));
593
594 Analyze (N);
595 end Expand_Entry_Parameter;
596
597 -------------------
598 -- Expand_Formal --
599 -------------------
600
601 procedure Expand_Formal (N : Node_Id) is
602 E : constant Entity_Id := Entity (N);
603 Scop : constant Entity_Id := Scope (E);
604
605 begin
606 -- Check whether the subprogram of which this is a formal is
607 -- a protected operation. The initialization procedure for
608 -- the corresponding record type is not itself a protected operation.
609
610 if Is_Protected_Type (Scope (Scop))
611 and then not Is_Init_Proc (Scop)
612 and then Present (Protected_Formal (E))
613 then
614 Set_Entity (N, Protected_Formal (E));
615 end if;
616 end Expand_Formal;
617
618 ----------------------------
619 -- Expand_N_Expanded_Name --
620 ----------------------------
621
622 procedure Expand_N_Expanded_Name (N : Node_Id) is
623 begin
624 Expand_Entity_Reference (N);
625 end Expand_N_Expanded_Name;
626
627 -------------------------
628 -- Expand_N_Identifier --
629 -------------------------
630
631 procedure Expand_N_Identifier (N : Node_Id) is
632 begin
633 Expand_Entity_Reference (N);
634 end Expand_N_Identifier;
635
636 ---------------------------
637 -- Expand_N_Real_Literal --
638 ---------------------------
639
640 procedure Expand_N_Real_Literal (N : Node_Id) is
641 pragma Unreferenced (N);
642
643 begin
644 -- Historically, this routine existed because there were expansion
645 -- requirements for Vax real literals, but now Vax real literals
646 -- are now handled by gigi, so this routine no longer does anything.
647
648 null;
649 end Expand_N_Real_Literal;
650
651 --------------------------------
652 -- Expand_Protected_Component --
653 --------------------------------
654
655 procedure Expand_Protected_Component (N : Node_Id) is
656
657 function Inside_Eliminated_Body return Boolean;
658 -- Determine whether the current entity is inside a subprogram or an
659 -- entry which has been marked as eliminated.
660
661 ----------------------------
662 -- Inside_Eliminated_Body --
663 ----------------------------
664
665 function Inside_Eliminated_Body return Boolean is
666 S : Entity_Id := Current_Scope;
667
668 begin
669 while Present (S) loop
670 if (Ekind (S) = E_Entry
671 or else Ekind (S) = E_Entry_Family
672 or else Ekind (S) = E_Function
673 or else Ekind (S) = E_Procedure)
674 and then Is_Eliminated (S)
675 then
676 return True;
677 end if;
678
679 S := Scope (S);
680 end loop;
681
682 return False;
683 end Inside_Eliminated_Body;
684
685 -- Start of processing for Expand_Protected_Component
686
687 begin
688 -- Eliminated bodies are not expanded and thus do not need privals
689
690 if not Inside_Eliminated_Body then
691 declare
692 Priv : constant Entity_Id := Prival (Entity (N));
693 begin
694 Set_Entity (N, Priv);
695 Set_Etype (N, Etype (Priv));
696 end;
697 end if;
698 end Expand_Protected_Component;
699
700 ---------------------
701 -- Expand_Renaming --
702 ---------------------
703
704 procedure Expand_Renaming (N : Node_Id) is
705 E : constant Entity_Id := Entity (N);
706 T : constant Entity_Id := Etype (N);
707
708 begin
709 -- Mark the entity as referenced since this reference is going away
710
711 Set_Referenced (E);
712
713 -- Now rewrite the reference as a copy of the renamed object
714
715 Rewrite (N, New_Copy_Tree (Renamed_Object (E)));
716
717 -- Mark the copy as unanalyzed to make sure that it is reanalyzed
718 -- at the top level. This is needed in the packed case since we
719 -- specifically avoided expanding packed array references when the
720 -- renaming declaration was analyzed.
721
722 Reset_Analyzed_Flags (N);
723 Analyze_And_Resolve (N, T);
724 end Expand_Renaming;
725
726 ------------------------------------------
727 -- Expand_N_Interpolated_String_Literal --
728 ------------------------------------------
729
730 procedure Expand_N_Interpolated_String_Literal (N : Node_Id) is
731
732 procedure Apply_Static_Length_Check (Typ : Entity_Id);
733 -- Tries to determine statically whether the length of the interpolated
734 -- string N exceeds the length of the target subtype Typ. If it can be
735 -- determined at compile time then an N_Raise_Constraint_Error node
736 -- replaces the interpolated string N, and a warning message is issued.
737
738 function Build_Interpolated_String_Image (N : Node_Id) return Node_Id;
739 -- Build the following Expression_With_Actions node:
740 -- do
741 -- Sink : Buffer;
742 -- [ Set_Trim_Leading_Spaces (Sink); ]
743 -- Type'Put_Image (Sink, X);
744 -- { [ Set_Trim_Leading_Spaces (Sink); ]
745 -- Type'Put_Image (Sink, X); }
746 -- Result : constant String := Get (Sink);
747 -- Destroy (Sink);
748 -- in Result end
749
750 -------------------------------
751 -- Apply_Static_Length_Check --
752 -------------------------------
753
754 procedure Apply_Static_Length_Check (Typ : Entity_Id) is
755 HB : constant Node_Id := High_Bound (First_Index (Typ));
756 LB : constant Node_Id := Low_Bound (First_Index (Typ));
757 Str_Elem : Node_Id;
758 Str_Length : Nat;
759 Typ_Length : Nat;
760
761 begin
762 if Compile_Time_Known_Value (LB)
763 and then Compile_Time_Known_Value (HB)
764 then
765 Typ_Length := UI_To_Int (Expr_Value (HB) - Expr_Value (LB) + 1);
766
767 -- Compute the minimum length of the interpolated string: the
768 -- length of the concatenation of the string literals composing
769 -- the interpolated string.
770
771 Str_Length := 0;
772 Str_Elem := First (Expressions (N));
773 while Present (Str_Elem) loop
774 if Nkind (Str_Elem) = N_String_Literal then
775 Str_Length := Str_Length + String_Length (Strval (Str_Elem));
776 end if;
777
778 Next (Str_Elem);
779 end loop;
780
781 if Str_Length > Typ_Length then
782 Apply_Compile_Time_Constraint_Error
783 (N, "wrong length for interpolated string of}??",
784 CE_Length_Check_Failed,
785 Ent => Typ,
786 Typ => Typ);
787 end if;
788 end if;
789 end Apply_Static_Length_Check;
790
791 -------------------------------------
792 -- Build_Interpolated_String_Image --
793 -------------------------------------
794
795 function Build_Interpolated_String_Image (N : Node_Id) return Node_Id
796 is
797 Loc : constant Source_Ptr := Sloc (N);
798 Sink_Entity : constant Entity_Id := Make_Temporary (Loc, 'S');
799 Sink_Decl : constant Node_Id :=
800 Make_Object_Declaration (Loc,
801 Defining_Identifier => Sink_Entity,
802 Object_Definition =>
803 New_Occurrence_Of (RTE (RE_Buffer_Type), Loc));
804
805 B_Type : constant Entity_Id := Base_Type (Etype (N));
806 Get_Id : constant RE_Id :=
807 (if B_Type = Stand.Standard_String then
808 RE_Get
809 elsif B_Type = Stand.Standard_Wide_String then
810 RE_Wide_Get
811 else
812 RE_Wide_Wide_Get);
813
814 Result_Entity : constant Entity_Id := Make_Temporary (Loc, 'R');
815 Result_Decl : constant Node_Id :=
816 Make_Object_Declaration (Loc,
817 Defining_Identifier => Result_Entity,
818 Object_Definition =>
819 New_Occurrence_Of (B_Type, Loc),
820 Expression =>
821 Make_Function_Call (Loc,
822 Name => New_Occurrence_Of (RTE (Get_Id), Loc),
823 Parameter_Associations => New_List (
824 New_Occurrence_Of (Sink_Entity, Loc))));
825
826 Actions : constant List_Id := New_List;
827 U_Type : constant Entity_Id := Underlying_Type (Etype (N));
828 Elem_Typ : Entity_Id;
829 Str_Elem : Node_Id;
830
831 begin
832 pragma Assert (Etype (N) /= Stand.Any_String);
833
834 Append_To (Actions, Sink_Decl);
835
836 Str_Elem := First (Expressions (N));
837 while Present (Str_Elem) loop
838 Elem_Typ := Etype (Str_Elem);
839
840 -- If the type is numeric or has a specified Integer_Literal or
841 -- Real_Literal aspect, then prior to invoking Put_Image, the
842 -- Trim_Leading_Spaces flag is set on the text buffer.
843
844 if Is_Numeric_Type (Underlying_Type (Elem_Typ))
845 or else Has_Aspect (Elem_Typ, Aspect_Integer_Literal)
846 or else Has_Aspect (Elem_Typ, Aspect_Real_Literal)
847 then
848 Append_To (Actions,
849 Make_Procedure_Call_Statement (Loc,
850 Name =>
851 New_Occurrence_Of
852 (RTE (RE_Set_Trim_Leading_Spaces), Loc),
853 Parameter_Associations => New_List (
854 Convert_To (RTE (RE_Root_Buffer_Type),
855 New_Occurrence_Of (Sink_Entity, Loc)),
856 New_Occurrence_Of (Stand.Standard_True, Loc))));
857 end if;
858
859 Append_To (Actions,
860 Make_Attribute_Reference (Loc,
861 Prefix => New_Occurrence_Of (Elem_Typ, Loc),
862 Attribute_Name => Name_Put_Image,
863 Expressions => New_List (
864 New_Occurrence_Of (Sink_Entity, Loc),
865 Duplicate_Subexpr (Str_Elem))));
866
867 Next (Str_Elem);
868 end loop;
869
870 -- Add a type conversion to the result object declaration of custom
871 -- string types.
872
873 if not Is_Standard_String_Type (U_Type)
874 and then (not RTU_Loaded (Interfaces_C)
875 or else Enclosing_Lib_Unit_Entity (U_Type)
876 /= RTU_Entity (Interfaces_C))
877 then
878 Set_Expression (Result_Decl,
879 Convert_To (Etype (N),
880 Relocate_Node (Expression (Result_Decl))));
881 end if;
882
883 Append_To (Actions, Result_Decl);
884
885 return Make_Expression_With_Actions (Loc,
886 Actions => Actions,
887 Expression => New_Occurrence_Of (Result_Entity, Loc));
888 end Build_Interpolated_String_Image;
889
890 -- Local variables
891
892 Typ : constant Entity_Id := Etype (N);
893
894 -- Start of processing for Expand_N_Interpolated_String_Literal
895
896 begin
897 -- If the type imposed by the context is constrained then check that
898 -- the statically known length of the interpolated string does not
899 -- exceed the length of its type.
900
901 if Is_Constrained (Typ) then
902 Apply_Static_Length_Check (Typ);
903
904 if Nkind (N) = N_Raise_Constraint_Error then
905 return;
906 end if;
907 end if;
908
909 Rewrite (N, Build_Interpolated_String_Image (N));
910 Analyze_And_Resolve (N, Typ);
911
912 if Is_Constrained (Typ) then
913 Apply_Length_Check (Expression (N), Typ);
914 end if;
915 end Expand_N_Interpolated_String_Literal;
916
917 end Exp_Ch2;