Debian Codegraph @

gcc/ada/exp_prag.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ P R A G --
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 Atree; use Atree;
27 with Casing; use Casing;
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 Errout; use Errout;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Util; use Exp_Util;
37 with Inline; use Inline;
38 with Lib; use Lib;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Prag; use Sem_Prag;
50 with Sem_Util; use Sem_Util;
51 with Sinfo; use Sinfo;
52 with Sinfo.Nodes; use Sinfo.Nodes;
53 with Sinfo.Utils; use Sinfo.Utils;
54 with Sinput; use Sinput;
55 with Snames; use Snames;
56 with Stringt; use Stringt;
57 with Stand; use Stand;
58 with Tbuild; use Tbuild;
59 with Uintp; use Uintp;
60 with Validsw; use Validsw;
61 with Warnsw; use Warnsw;
62
63 with System.Case_Util; use System.Case_Util;
64
65 package body Exp_Prag is
66
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
70
71 function Arg_N (N : Node_Id; Arg_Number : Positive) return Node_Id;
72 -- Obtain specified pragma argument expression
73
74 procedure Expand_Pragma_Abort_Defer (N : Node_Id);
75 procedure Expand_Pragma_Check (N : Node_Id);
76 procedure Expand_Pragma_Common_Object (N : Node_Id);
77 procedure Expand_Pragma_CUDA_Execute (N : Node_Id);
78 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id);
79 procedure Expand_Pragma_Inspection_Point (N : Node_Id);
80 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id);
81 procedure Expand_Pragma_Loop_Variant (N : Node_Id);
82 procedure Expand_Pragma_Psect_Object (N : Node_Id);
83 procedure Expand_Pragma_Relative_Deadline (N : Node_Id);
84 procedure Expand_Pragma_Suppress_Initialization (N : Node_Id);
85
86 procedure Undo_Initialization (Def_Id : Entity_Id; N : Node_Id);
87 -- This procedure is used to undo initialization already done for Def_Id,
88 -- which is always an E_Variable, in response to the occurrence of the
89 -- pragma N, a pragma Interface, Import, or Suppress_Initialization. In all
90 -- these cases we want no initialization to occur, but we have already done
91 -- the initialization by the time we see the pragma, so we have to undo it.
92
93 -----------
94 -- Arg_N --
95 -----------
96
97 function Arg_N (N : Node_Id; Arg_Number : Positive) return Node_Id is
98 Arg : Node_Id := First (Pragma_Argument_Associations (N));
99 begin
100 if No (Arg) then
101 return Empty;
102 end if;
103
104 for J in 2 .. Arg_Number loop
105 Next (Arg);
106 if No (Arg) then
107 return Empty;
108 end if;
109 end loop;
110
111 if Present (Arg) then
112 return Get_Pragma_Arg (Arg);
113 else
114 return Empty;
115 end if;
116 end Arg_N;
117
118 ---------------------
119 -- Expand_N_Pragma --
120 ---------------------
121
122 procedure Expand_N_Pragma (N : Node_Id) is
123 Pname : constant Name_Id := Pragma_Name (N);
124 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Pname);
125
126 begin
127 -- Suppress the expansion of an ignored assertion pragma. Such a pragma
128 -- should not be transformed into a null statement because:
129 --
130 -- * The pragma may be part of the rep item chain of a type, in which
131 -- case rewriting it will destroy the chain.
132 --
133 -- * The analysis of the pragma may involve two parts (see routines
134 -- Analyze_xxx_In_Decl_Part). The second part of the analysis will
135 -- not happen if the pragma is rewritten.
136
137 if Assertion_Expression_Pragma (Prag_Id) and then Is_Ignored (N) then
138 return;
139
140 -- Rewrite the pragma into a null statement when it is ignored using
141 -- pragma Ignore_Pragma, or denotes Default_Scalar_Storage_Order and
142 -- compilation switch -gnatI is in effect.
143
144 elsif Should_Ignore_Pragma_Sem (N)
145 or else (Prag_Id = Pragma_Default_Scalar_Storage_Order
146 and then Ignore_Rep_Clauses)
147 then
148 Rewrite (N, Make_Null_Statement (Sloc (N)));
149 return;
150 end if;
151
152 case Prag_Id is
153
154 -- Pragmas requiring special expander action
155
156 when Pragma_Abort_Defer =>
157 Expand_Pragma_Abort_Defer (N);
158
159 when Pragma_Check =>
160 Expand_Pragma_Check (N);
161
162 when Pragma_Common_Object =>
163 Expand_Pragma_Common_Object (N);
164
165 when Pragma_CUDA_Execute =>
166 Expand_Pragma_CUDA_Execute (N);
167
168 when Pragma_Import =>
169 Expand_Pragma_Import_Or_Interface (N);
170
171 when Pragma_Inspection_Point =>
172 Expand_Pragma_Inspection_Point (N);
173
174 when Pragma_Interface =>
175 Expand_Pragma_Import_Or_Interface (N);
176
177 when Pragma_Interrupt_Priority =>
178 Expand_Pragma_Interrupt_Priority (N);
179
180 when Pragma_Loop_Variant =>
181 Expand_Pragma_Loop_Variant (N);
182
183 when Pragma_Psect_Object =>
184 Expand_Pragma_Psect_Object (N);
185
186 when Pragma_Relative_Deadline =>
187 Expand_Pragma_Relative_Deadline (N);
188
189 when Pragma_Suppress_Initialization =>
190 Expand_Pragma_Suppress_Initialization (N);
191
192 -- All other pragmas need no expander action (includes
193 -- Unknown_Pragma).
194
195 when others => null;
196 end case;
197 end Expand_N_Pragma;
198
199 -------------------------------
200 -- Expand_Pragma_Abort_Defer --
201 -------------------------------
202
203 -- An Abort_Defer pragma appears as the first statement in a handled
204 -- statement sequence (right after the begin). It defers aborts for
205 -- the entire statement sequence, but not for any declarations or
206 -- handlers (if any) associated with this statement sequence.
207
208 -- The transformation is to transform
209
210 -- pragma Abort_Defer;
211 -- statements;
212
213 -- into
214
215 -- begin
216 -- Abort_Defer.all;
217 -- statements
218 -- exception
219 -- when all others =>
220 -- Abort_Undefer.all;
221 -- raise;
222 -- at end
223 -- Abort_Undefer_Direct;
224 -- end;
225
226 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
227 begin
228 -- Abort_Defer has no useful effect if Abort's are not allowed
229
230 if not Abort_Allowed then
231 return;
232 end if;
233
234 -- Normal case where abort is possible
235
236 declare
237 Loc : constant Source_Ptr := Sloc (N);
238 Stm : Node_Id;
239 Stms : List_Id;
240 HSS : Node_Id;
241 Blk : constant Entity_Id :=
242 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
243 AUD : constant Entity_Id := RTE (RE_Abort_Undefer_Direct);
244
245 begin
246 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
247 loop
248 Stm := Remove_Next (N);
249 exit when No (Stm);
250 Append (Stm, Stms);
251 end loop;
252
253 HSS :=
254 Make_Handled_Sequence_Of_Statements (Loc,
255 Statements => Stms,
256 At_End_Proc => New_Occurrence_Of (AUD, Loc));
257
258 -- Present the Abort_Undefer_Direct function to the backend so that
259 -- it can inline the call to the function.
260
261 Add_Inlined_Body (AUD, N);
262
263 Rewrite (N,
264 Make_Block_Statement (Loc, Handled_Statement_Sequence => HSS));
265
266 Set_Scope (Blk, Current_Scope);
267 Set_Etype (Blk, Standard_Void_Type);
268 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
269 Expand_At_End_Handler (HSS, Blk);
270 Analyze (N);
271 end;
272 end Expand_Pragma_Abort_Defer;
273
274 -------------------------------------
275 -- Expand_Pragma_Always_Terminates --
276 -------------------------------------
277
278 procedure Expand_Pragma_Always_Terminates (Prag : Node_Id) is
279 pragma Unreferenced (Prag);
280 begin
281 null;
282 end Expand_Pragma_Always_Terminates;
283
284 --------------------------
285 -- Expand_Pragma_Check --
286 --------------------------
287
288 procedure Expand_Pragma_Check (N : Node_Id) is
289 procedure Replace_Discriminals_Of_Protected_Op (Expr : Node_Id);
290 -- Discriminants of the enclosing protected object may be referenced
291 -- in the expression of a precondition of a protected operation.
292 -- In the body of the operation these references must be replaced by
293 -- the discriminal created for them, which are renamings of the
294 -- discriminants of the object that is the target of the operation.
295 -- This replacement is done by visibility when the references appear
296 -- in the subprogram body, but in the case of a condition which appears
297 -- on the specification of the subprogram it has be done separately
298 -- because the condition has been replaced by a Check pragma and
299 -- analyzed earlier, before the creation of the discriminal renaming
300 -- declarations that are added to the subprogram body.
301
302 function Make_Failure_Message
303 (Nam : Name_Id;
304 Cond : Node_Id)
305 return Node_Id;
306 -- Build node with a string literal of a message for check Nam with
307 -- condition Cond failing at runtime.
308
309 --------------------------
310 -- Make_Failure_Message --
311 --------------------------
312
313 function Make_Failure_Message
314 (Nam : Name_Id;
315 Cond : Node_Id)
316 return Node_Id
317 is
318 Loc : constant Source_Ptr := Sloc (First_Node (Cond));
319 Loc_Str : constant String := Build_Location_String (Loc);
320
321 begin
322 Name_Len := 0;
323
324 -- For Assert, we just use the location
325
326 if Nam = Name_Assert then
327 null;
328
329 -- For predicate, we generate the string "predicate failed at yyy".
330 -- We prefer all lower case for predicate.
331
332 elsif Nam = Name_Predicate then
333 Add_Str_To_Name_Buffer ("predicate failed at ");
334
335 -- For special case of Precondition/Postcondition the string is
336 -- "failed xx from yy" where xx is precondition/postcondition in all
337 -- lower case. The reason for this different wording is that the
338 -- failure is not at the point of occurrence of the pragma, unlike
339 -- the other Check cases.
340
341 elsif Nam in Name_Pre
342 | Name_Precondition
343 | Name_Post
344 | Name_Postcondition
345 then
346 Add_Str_To_Name_Buffer ("failed ");
347
348 -- Enhance information for inherited pragmas
349
350 if Comes_From_Inherited_Pragma (Loc) then
351 Add_Str_To_Name_Buffer ("inherited ");
352 end if;
353
354 if Nam = Name_Pre then
355 Get_Name_String_And_Append (Name_Precondition);
356 elsif Nam = Name_Post then
357 Get_Name_String_And_Append (Name_Postcondition);
358 else
359 Get_Name_String_And_Append (Nam);
360 end if;
361
362 Add_Str_To_Name_Buffer (" from ");
363
364 -- For special case of Invariant, the string is "failed invariant
365 -- from yy", to be consistent with the string that is generated for
366 -- the aspect case (the code later on checks for this specific string
367 -- to modify it in some cases, so this is functionally important).
368
369 elsif Nam = Name_Invariant then
370 Add_Str_To_Name_Buffer ("failed invariant from ");
371
372 -- For all other checks, the string is "xxx failed at yyy"
373 -- where xxx is the check name with appropriate casing.
374
375 else
376 Get_Name_String (Nam);
377 Set_Casing (Identifier_Casing (Source_Index (Current_Sem_Unit)));
378 Add_Str_To_Name_Buffer (" failed at ");
379 end if;
380
381 -- In all cases, add location string
382
383 Add_Str_To_Name_Buffer (Loc_Str);
384
385 -- Build the message
386
387 return Make_String_Literal (Loc, Name_Buffer (1 .. Name_Len));
388 end Make_Failure_Message;
389
390 ------------------------------------------
391 -- Replace_Discriminals_Of_Protected_Op --
392 ------------------------------------------
393
394 procedure Replace_Discriminals_Of_Protected_Op (Expr : Node_Id) is
395 function Find_Corresponding_Discriminal
396 (E : Entity_Id) return Entity_Id;
397 -- Find the local entity that renames a discriminant of the enclosing
398 -- protected type, and has a matching name.
399
400 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result;
401 -- Replace a reference to a discriminant of the original protected
402 -- type by the local renaming declaration of the discriminant of
403 -- the target object.
404
405 ------------------------------------
406 -- Find_Corresponding_Discriminal --
407 ------------------------------------
408
409 function Find_Corresponding_Discriminal
410 (E : Entity_Id) return Entity_Id
411 is
412 R : Entity_Id;
413
414 begin
415 R := First_Entity (Current_Scope);
416
417 while Present (R) loop
418 if Nkind (Parent (R)) = N_Object_Renaming_Declaration
419 and then Present (Discriminal_Link (R))
420 and then Chars (Discriminal_Link (R)) = Chars (E)
421 then
422 return R;
423 end if;
424
425 Next_Entity (R);
426 end loop;
427
428 return Empty;
429 end Find_Corresponding_Discriminal;
430
431 -----------------------
432 -- Replace_Discr_Ref --
433 -----------------------
434
435 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result is
436 R : Entity_Id;
437
438 begin
439 if Is_Entity_Name (N)
440 and then Present (Discriminal_Link (Entity (N)))
441 then
442 R := Find_Corresponding_Discriminal (Entity (N));
443 Rewrite (N, New_Occurrence_Of (R, Sloc (N)));
444 end if;
445
446 return OK;
447 end Replace_Discr_Ref;
448
449 procedure Replace_Discriminant_References is
450 new Traverse_Proc (Replace_Discr_Ref);
451
452 -- Start of processing for Replace_Discriminals_Of_Protected_Op
453
454 begin
455 Replace_Discriminant_References (Expr);
456 end Replace_Discriminals_Of_Protected_Op;
457
458 -- Local variables
459
460 Cond : constant Node_Id := Arg_N (N, 2);
461 Nam : constant Name_Id := Chars (Arg_N (N, 1));
462 Msg : Node_Id;
463
464 Loc : constant Source_Ptr := Sloc (First_Node (Cond));
465 -- Source location used in the case of a failed assertion: point to the
466 -- failing condition, not Loc. Note that the source location of the
467 -- expression is not usually the best choice here, because it points to
468 -- the location of the topmost tree node, which may be an operator in
469 -- the middle of the source text of the expression. For example, it gets
470 -- located on the last AND keyword in a chain of boolean expressions
471 -- AND'ed together. It is best to put the message on the first character
472 -- of the condition, which is the effect of the First_Node call here.
473 -- This source location is used to build the default exception message,
474 -- and also as the sloc of the call to the runtime subprogram raising
475 -- Assert_Failure, so that coverage analysis tools can relate the
476 -- call to the failed check.
477
478 -- Start of processing for Expand_Pragma_Check
479
480 begin
481 -- Nothing to do if pragma is ignored
482
483 if Is_Ignored (N) then
484 return;
485 end if;
486
487 -- Since this check is active, rewrite the pragma into a corresponding
488 -- if statement, and then analyze the statement.
489
490 -- The normal case expansion transforms:
491
492 -- pragma Check (name, condition [,message]);
493
494 -- into
495
496 -- if not condition then
497 -- System.Assertions.Raise_Assert_Failure (Str);
498 -- end if;
499
500 -- where Str is the message if one is present, or the default of
501 -- name failed at file:line if no message is given (the "name failed
502 -- at" is omitted for name = Assertion, since it is redundant, given
503 -- that the name of the exception is Assert_Failure.)
504
505 -- Also, instead of "XXX failed at", we generate slightly
506 -- different messages for some of the contract assertions (see
507 -- code below for details).
508
509 -- An alternative expansion is used when the No_Exception_Propagation
510 -- restriction is active and there is a local Assert_Failure handler.
511 -- This is not a common combination of circumstances, but it occurs in
512 -- the context of Aunit and the zero footprint profile. In this case we
513 -- generate:
514
515 -- if not condition then
516 -- raise Assert_Failure;
517 -- end if;
518
519 -- This will then be transformed into a goto, and the local handler will
520 -- be able to handle the assert error (which would not be the case if a
521 -- call is made to the Raise_Assert_Failure procedure).
522
523 -- We also generate the direct raise if the Suppress_Exception_Locations
524 -- is active, since we don't want to generate messages in this case.
525
526 -- Note that the reason we do not always generate a direct raise is that
527 -- the form in which the procedure is called allows for more efficient
528 -- breakpointing of assertion errors.
529
530 -- Generate the appropriate if statement. Note that we consider this to
531 -- be an explicit conditional in the source, not an implicit if, so we
532 -- do not call Make_Implicit_If_Statement. Note also that we wrap the
533 -- raise statement in a block statement so that, if the condition is
534 -- evaluated at compile time to False, then the rewriting of the if
535 -- statement will not involve the raise but the block statement, and
536 -- thus not leave a dangling reference to the raise statement in the
537 -- Local_Raise_Statements list of the handler.
538
539 -- Case where we generate a direct raise
540
541 if ((Debug_Flag_Dot_G
542 or else Restriction_Active (No_Exception_Propagation))
543 and then Present (Find_Local_Handler (RTE (RE_Assert_Failure), N)))
544 or else (Opt.Exception_Locations_Suppressed and then No (Arg_N (N, 3)))
545 then
546 Rewrite (N,
547 Make_If_Statement (Loc,
548 Condition => Make_Op_Not (Loc, Right_Opnd => Cond),
549 Then_Statements => New_List (
550 Make_Block_Statement (Loc,
551 Handled_Statement_Sequence =>
552 Make_Handled_Sequence_Of_Statements (Loc,
553 Statements => New_List (
554 Make_Raise_Statement (Loc,
555 Name =>
556 New_Occurrence_Of (RTE (RE_Assert_Failure),
557 Loc))))))));
558
559 Set_Comes_From_Check_Or_Contract (N);
560
561 -- Case where we call the procedure
562
563 else
564 -- For a precondition, replace references to discriminants of a
565 -- protected type with the local discriminals.
566
567 if Is_Protected_Type (Scope (Current_Scope))
568 and then Has_Discriminants (Scope (Current_Scope))
569 and then From_Aspect_Specification (N)
570 then
571 Replace_Discriminals_Of_Protected_Op (Cond);
572 end if;
573
574 -- Now rewrite as an if statement
575
576 declare
577 function Make_Elsif_Check (Conj : Node_Id) return Node_Id;
578 -- Create an elsif part that checks a conjunct expression Conj and
579 -- emits a message with the exact location when the check fails.
580
581 ----------------------
582 -- Make_Elsif_Check --
583 ----------------------
584
585 function Make_Elsif_Check (Conj : Node_Id) return Node_Id is
586 begin
587 return
588 Make_Elsif_Part (Loc,
589 Condition =>
590 Make_Op_Not (Loc,
591 Relocate_Node (Conj)),
592 Then_Statements =>
593 New_List (
594 Make_Procedure_Call_Statement (Loc,
595 Name =>
596 New_Occurrence_Of
597 (RTE (RE_Raise_Assert_Failure), Loc),
598 Parameter_Associations =>
599 New_List (
600 Make_Failure_Message (Nam, Conj)))));
601 end Make_Elsif_Check;
602
603 Conjunct : Node_Id := Cond;
604 Elsifs : List_Id := No_List;
605
606 begin
607 -- If we have a message given, use it
608
609 if Present (Arg_N (N, 3)) then
610 Msg := Get_Pragma_Arg (Arg_N (N, 3));
611
612 -- If check is for a Pre/Post expression of the form "A and then
613 -- B", then we split condition into separate conjuncts with
614 -- messages pointing to their exact locations, i.e.:
615 --
616 -- if not A then
617 -- Raise_Assert_Failure ("failed pre/post from [sloc of A]");
618 -- elsif not B then then
619 -- Raise_Assert_Failure ("failed pre/post from [sloc of B]");
620 -- end if;
621 --
622 -- This makes it easier to debug a failed complex contract.
623
624 else
625 if Nam in Name_Pre
626 | Name_Precondition
627 | Name_Post
628 | Name_Postcondition
629 then
630 while Nkind (Conjunct) = N_And_Then loop
631 Prepend_New_To (Elsifs,
632 Make_Elsif_Check (Right_Opnd (Conjunct)));
633 Conjunct := Left_Opnd (Conjunct);
634 end loop;
635 end if;
636
637 Msg := Make_Failure_Message (Nam, Conjunct);
638 end if;
639
640 Rewrite (N,
641 Make_If_Statement (Loc,
642 Condition =>
643 Make_Op_Not (Loc, Relocate_Node (Conjunct)),
644 Then_Statements =>
645 New_List (
646 Make_Procedure_Call_Statement (Loc,
647 Name =>
648 New_Occurrence_Of
649 (RTE (RE_Raise_Assert_Failure), Loc),
650 Parameter_Associations =>
651 New_List (Msg))),
652 Elsif_Parts => Elsifs));
653
654 Set_Comes_From_Check_Or_Contract (N);
655 end;
656 end if;
657
658 Analyze (N);
659
660 -- If new condition is always false, give a warning
661
662 if Warn_On_Assertion_Failure
663 and then Nkind (N) = N_Procedure_Call_Statement
664 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
665 then
666 -- If original condition was a Standard.False, we assume that this is
667 -- indeed intended to raise assert error and no warning is required.
668
669 if Is_Entity_Name (Original_Node (Cond))
670 and then Entity (Original_Node (Cond)) = Standard_False
671 then
672 null;
673
674 -- For Subprogram_Variant suppress the warning altogether, because
675 -- for mutually recursive subprograms with multiple variant clauses
676 -- some of the clauses might have expressions that are only meant for
677 -- verification and would always fail when executed.
678
679 elsif Nam = Name_Subprogram_Variant then
680 null;
681 elsif Nam = Name_Assert then
682 Error_Msg_N ("?.a?assertion will fail at run time", N);
683 else
684 Error_Msg_N ("?.a?check will fail at run time", N);
685 end if;
686 end if;
687 end Expand_Pragma_Check;
688
689 ---------------------------------
690 -- Expand_Pragma_Common_Object --
691 ---------------------------------
692
693 -- Use a machine attribute to replicate semantic effect in DEC Ada
694
695 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
696
697 -- For now we do nothing with the size attribute ???
698
699 -- Note: Psect_Object shares this processing
700
701 procedure Expand_Pragma_Common_Object (N : Node_Id) is
702 Loc : constant Source_Ptr := Sloc (N);
703
704 Internal : constant Node_Id := Arg_N (N, 1);
705 External : constant Node_Id := Arg_N (N, 2);
706
707 Psect : Node_Id;
708 -- Psect value upper cased as string literal
709
710 Iloc : constant Source_Ptr := Sloc (Internal);
711 Eloc : constant Source_Ptr := Sloc (External);
712 Ploc : Source_Ptr;
713
714 begin
715 -- Acquire Psect value and fold to upper case
716
717 if Present (External) then
718 if Nkind (External) = N_String_Literal then
719 String_To_Name_Buffer (Strval (External));
720 else
721 Get_Name_String (Chars (External));
722 end if;
723
724 Set_Casing (All_Upper_Case);
725
726 Psect :=
727 Make_String_Literal (Eloc, Strval => String_From_Name_Buffer);
728
729 else
730 Get_Name_String (Chars (Internal));
731 Set_Casing (All_Upper_Case);
732 Psect :=
733 Make_String_Literal (Iloc, Strval => String_From_Name_Buffer);
734 end if;
735
736 Ploc := Sloc (Psect);
737
738 -- Insert the pragma
739
740 Insert_After_And_Analyze (N,
741 Make_Pragma (Loc,
742 Chars => Name_Machine_Attribute,
743 Pragma_Argument_Associations => New_List (
744 Make_Pragma_Argument_Association (Iloc,
745 Expression => New_Copy_Tree (Internal)),
746 Make_Pragma_Argument_Association (Eloc,
747 Expression =>
748 Make_String_Literal (Sloc => Ploc, Strval => "common_object")),
749 Make_Pragma_Argument_Association (Ploc,
750 Expression => New_Copy_Tree (Psect)))));
751 end Expand_Pragma_Common_Object;
752
753 --------------------------------
754 -- Expand_Pragma_CUDA_Execute --
755 --------------------------------
756
757 -- Pragma CUDA_Execute is expanded in the following manner:
758
759 -- Original Code
760
761 -- pragma CUDA_Execute (My_Proc (X, Y), Blocks, Grids, Mem, Stream)
762
763 -- Expanded Code
764
765 -- declare
766 -- Blocks_Id : CUDA.Vector_Types.Dim3 := Blocks;
767 -- Grids_Id : CUDA.Vector_Types.Dim3 := Grids;
768 -- Mem_Id : Integer := <Mem or 0>;
769 -- Stream_Id : CUDA.Driver_Types.Stream_T := <Stream or null>;
770 -- X_Id : <Type of X> := X;
771 -- Y_Id : <Type of Y> := Y;
772 -- Arg_Id : Array (1..2) of System.Address :=
773 -- (X'Address,_Id Y'Address);_Id
774 -- begin
775 -- CUDA.Internal.Push_Call_Configuration (
776 -- Grids_Id,
777 -- Blocks_Id,
778 -- Mem_Id,
779 -- Stream_Id);
780 -- CUDA.Internal.Pop_Call_Configuration (
781 -- Grids_Id'address,
782 -- Blocks_Id'address,
783 -- Mem_Id'address,
784 -- Stream_Id'address),
785 -- CUDA.Internal.Launch_Kernel (
786 -- My_Proc'Address,
787 -- Blocks_Id,
788 -- Grids_Id,
789 -- Arg_Id'Address,
790 -- Mem_Id,
791 -- Stream_Id);
792 -- end;
793
794 procedure Expand_Pragma_CUDA_Execute (N : Node_Id) is
795
796 Loc : constant Source_Ptr := Sloc (N);
797
798 procedure Append_Copies
799 (Params : List_Id;
800 Decls : List_Id;
801 Copies : Elist_Id);
802 -- For each parameter in list Params, create an object declaration of
803 -- the following form:
804 --
805 -- Copy_Id : Param_Typ := Param_Val;
806 --
807 -- Param_Typ is the type of the parameter. Param_Val is the initial
808 -- value of the parameter. The declarations are stored in Decls, the
809 -- entities of the new objects are collected in list Copies.
810
811 function Build_Dim3_Declaration
812 (Decl_Id : Entity_Id;
813 Init_Val : Node_Id) return Node_Id;
814 -- Build an object declaration of the form
815 --
816 -- Decl_Id : CUDA.Internal.Dim3 := Val;
817 --
818 -- Val depends on the nature of Init_Val, as follows:
819 --
820 -- * If Init_Val is of type CUDA.Vector_Types.Dim3, then Val has the
821 -- following form:
822 --
823 -- (Interfaces.C.Unsigned (Val.X),
824 -- Interfaces.C.Unsigned (Val.Y),
825 -- Interfaces.C.Unsigned (Val.Z))
826 --
827 -- * If Init_Val is a single Integer, Val has the following form:
828 --
829 -- (Interfaces.C.Unsigned (Init_Val),
830 -- Interfaces.C.Unsigned (1),
831 -- Interfaces.C.Unsigned (1))
832 --
833 -- * If Init_Val is an aggregate of three values, Val has the
834 -- following form:
835 --
836 -- (Interfaces.C.Unsigned (Val_1),
837 -- Interfaces.C.Unsigned (Val_2),
838 -- Interfaces.C.Unsigned (Val_3))
839
840 function Build_Kernel_Args_Declaration
841 (Kernel_Arg : Entity_Id;
842 Var_Ids : Elist_Id) return Node_Id;
843 -- Given a list of variables, return an object declaration of the
844 -- following form:
845 --
846 -- Kernel_Arg : ... := (Var_1'Address, ..., Var_N'Address);
847
848 function Build_Launch_Kernel_Call
849 (Proc : Entity_Id;
850 Grid_Dims : Entity_Id;
851 Block_Dims : Entity_Id;
852 Kernel_Arg : Entity_Id;
853 Memory : Entity_Id;
854 Stream : Entity_Id) return Node_Id;
855 -- Builds and returns a call to CUDA.Internal.Launch_Kernel using the
856 -- given arguments. Proc is the entity of the procedure passed to the
857 -- CUDA_Execute pragma. Grid_Dims and Block_Dims are entities of the
858 -- generated declarations that hold the kernel's dimensions. Args is the
859 -- entity of the temporary array that holds the arguments of the kernel.
860 -- Memory and Stream are the entities of the temporaries that hold the
861 -- fourth and fith arguments of CUDA_Execute or their default values.
862
863 function Build_Shared_Memory_Declaration
864 (Decl_Id : Entity_Id;
865 Init_Val : Node_Id) return Node_Id;
866 -- Builds a declaration the Defining_Identifier of which is Decl_Id, the
867 -- type of which is inferred from CUDA.Internal.Launch_Kernel and the
868 -- value of which is Init_Val if present or null if not.
869
870 function Build_Simple_Declaration_With_Default
871 (Decl_Id : Entity_Id;
872 Init_Val : Node_Id;
873 Typ : Node_Id;
874 Default_Val : Node_Id) return Node_Id;
875 -- Build a declaration the Defining_Identifier of which is Decl_Id, the
876 -- Object_Definition of which is Typ, the value of which is Init_Val if
877 -- present or Default otherwise.
878
879 function Build_Stream_Declaration
880 (Decl_Id : Entity_Id;
881 Init_Val : Node_Id) return Node_Id;
882 -- Build a declaration the Defining_Identifier of which is Decl_Id, the
883 -- type of which is Integer, the value of which is Init_Val if present
884 -- and 0 otherwise.
885
886 function Etype_Or_Dim3 (N : Node_Id) return Node_Id;
887 -- If N is an aggregate whose type is unknown, return a new occurrence
888 -- of the public Dim3 type. Otherwise, return a new occurrence of N's
889 -- type.
890
891 function Get_Nth_Arg_Type
892 (Subprogram : Entity_Id;
893 N : Positive) return Entity_Id;
894 -- Returns the type of the Nth argument of Subprogram
895
896 function To_Addresses (Elmts : Elist_Id) return List_Id;
897 -- Returns a new list containing each element of Elmts wrapped in an
898 -- 'address attribute reference. When passed No_Elist, returns an empty
899 -- list.
900
901 -------------------
902 -- Append_Copies --
903 -------------------
904
905 procedure Append_Copies
906 (Params : List_Id;
907 Decls : List_Id;
908 Copies : Elist_Id)
909 is
910 Copy : Entity_Id;
911 Param : Node_Id;
912 Expr : Node_Id;
913 begin
914 Param := First (Params);
915 while Present (Param) loop
916 Copy := Make_Temporary (Loc, 'C');
917
918 if Nkind (Param) = N_Parameter_Association then
919 Expr := Explicit_Actual_Parameter (Param);
920 else
921 Expr := Param;
922 end if;
923
924 Append_To (Decls,
925 Make_Object_Declaration (Loc,
926 Defining_Identifier => Copy,
927 Object_Definition => New_Occurrence_Of (Etype (Expr), Loc),
928 Expression => New_Copy_Tree (Expr)));
929
930 Append_Elmt (Copy, Copies);
931 Next (Param);
932 end loop;
933 end Append_Copies;
934
935 ----------------------------
936 -- Build_Dim3_Declaration --
937 ----------------------------
938
939 function Build_Dim3_Declaration
940 (Decl_Id : Entity_Id;
941 Init_Val : Node_Id) return Node_Id
942 is
943 -- Expressions for each component of the returned Dim3
944 Dim_X : Node_Id;
945 Dim_Y : Node_Id;
946 Dim_Z : Node_Id;
947
948 -- Type of CUDA.Internal.Dim3 - inferred from
949 -- RE_Push_Call_Configuration to avoid needing changes in GNAT when
950 -- the CUDA bindings change (this happens frequently).
951 Internal_Dim3 : constant Entity_Id :=
952 Get_Nth_Arg_Type (RTE (RE_Push_Call_Configuration), 1);
953
954 -- Entities for each component of external and internal Dim3
955 First_Component : Entity_Id := First_Entity (RTE (RE_Dim3));
956 Second_Component : Entity_Id := Next_Entity (First_Component);
957 Third_Component : Entity_Id := Next_Entity (Second_Component);
958
959 begin
960 -- Sem_prag.adb ensured that Init_Val is either a Dim3, an aggregate
961 -- of three Any_Integers or Any_Integer.
962
963 -- If Init_Val is a Dim3, use each of its components
964
965 if Etype (Init_Val) = RTE (RE_Dim3) then
966 Dim_X := Make_Selected_Component (Loc,
967 Prefix => New_Occurrence_Of (Entity (Init_Val), Loc),
968 Selector_Name => New_Occurrence_Of (First_Component, Loc));
969
970 Dim_Y := Make_Selected_Component (Loc,
971 Prefix => New_Occurrence_Of (Entity (Init_Val), Loc),
972 Selector_Name => New_Occurrence_Of (Second_Component, Loc));
973
974 Dim_Z := Make_Selected_Component (Loc,
975 Prefix => New_Occurrence_Of (Entity (Init_Val), Loc),
976 Selector_Name => New_Occurrence_Of (Third_Component, Loc));
977 else
978 -- If Init_Val is an aggregate, use each of its arguments
979
980 if Nkind (Init_Val) = N_Aggregate then
981 Dim_X := First (Expressions (Init_Val));
982 Dim_Y := Next (Dim_X);
983 Dim_Z := Next (Dim_Y);
984
985 -- Otherwise, we know it is an integer and the rest defaults to 1
986
987 else
988 Dim_X := Init_Val;
989 Dim_Y := Make_Integer_Literal (Loc, 1);
990 Dim_Z := Make_Integer_Literal (Loc, 1);
991 end if;
992 end if;
993
994 First_Component := First_Entity (Internal_Dim3);
995 Second_Component := Next_Entity (First_Component);
996 Third_Component := Next_Entity (Second_Component);
997
998 -- Finally return the CUDA.Internal.Dim3 declaration with an
999 -- aggregate initialization expression.
1000
1001 return Make_Object_Declaration (Loc,
1002 Defining_Identifier => Decl_Id,
1003 Object_Definition => New_Occurrence_Of (Internal_Dim3, Loc),
1004 Expression => Make_Aggregate (Loc,
1005 Expressions => New_List (
1006 Make_Type_Conversion (Loc,
1007 Subtype_Mark =>
1008 New_Occurrence_Of (Etype (First_Component), Loc),
1009 Expression => New_Copy_Tree (Dim_X)),
1010 Make_Type_Conversion (Loc,
1011 Subtype_Mark =>
1012 New_Occurrence_Of (Etype (Second_Component), Loc),
1013 Expression => New_Copy_Tree (Dim_Y)),
1014 Make_Type_Conversion (Loc,
1015 Subtype_Mark =>
1016 New_Occurrence_Of (Etype (Third_Component), Loc),
1017 Expression => New_Copy_Tree (Dim_Z)))));
1018 end Build_Dim3_Declaration;
1019
1020 -----------------------------------
1021 -- Build_Kernel_Args_Declaration --
1022 -----------------------------------
1023
1024 function Build_Kernel_Args_Declaration
1025 (Kernel_Arg : Entity_Id;
1026 Var_Ids : Elist_Id) return Node_Id
1027 is
1028 Vals : constant List_Id := To_Addresses (Var_Ids);
1029 begin
1030 return
1031 Make_Object_Declaration (Loc,
1032 Defining_Identifier => Kernel_Arg,
1033 Object_Definition =>
1034 Make_Constrained_Array_Definition (Loc,
1035 Discrete_Subtype_Definitions => New_List (
1036 Make_Range (Loc,
1037 Low_Bound => Make_Integer_Literal (Loc, 1),
1038 High_Bound =>
1039 Make_Integer_Literal (Loc, List_Length (Vals)))),
1040 Component_Definition =>
1041 Make_Component_Definition (Loc,
1042 Subtype_Indication =>
1043 New_Occurrence_Of (Etype (RTE (RE_Address)), Loc))),
1044 Expression => Make_Aggregate (Loc, Vals));
1045 end Build_Kernel_Args_Declaration;
1046
1047 -------------------------------
1048 -- Build_Launch_Kernel_Call --
1049 -------------------------------
1050
1051 function Build_Launch_Kernel_Call
1052 (Proc : Entity_Id;
1053 Grid_Dims : Entity_Id;
1054 Block_Dims : Entity_Id;
1055 Kernel_Arg : Entity_Id;
1056 Memory : Entity_Id;
1057 Stream : Entity_Id) return Node_Id is
1058 begin
1059 return
1060 Make_Procedure_Call_Statement (Loc,
1061 Name =>
1062 New_Occurrence_Of (RTE (RE_Launch_Kernel), Loc),
1063 Parameter_Associations => New_List (
1064 Make_Attribute_Reference (Loc,
1065 Prefix => New_Occurrence_Of (Proc, Loc),
1066 Attribute_Name => Name_Address),
1067 New_Occurrence_Of (Grid_Dims, Loc),
1068 New_Occurrence_Of (Block_Dims, Loc),
1069 Make_Attribute_Reference (Loc,
1070 Prefix => New_Occurrence_Of (Kernel_Arg, Loc),
1071 Attribute_Name => Name_Address),
1072 New_Occurrence_Of (Memory, Loc),
1073 New_Occurrence_Of (Stream, Loc)));
1074 end Build_Launch_Kernel_Call;
1075
1076 -------------------------------------
1077 -- Build_Shared_Memory_Declaration --
1078 -------------------------------------
1079
1080 function Build_Shared_Memory_Declaration
1081 (Decl_Id : Entity_Id;
1082 Init_Val : Node_Id) return Node_Id
1083 is
1084 begin
1085 return Build_Simple_Declaration_With_Default
1086 (Decl_Id => Decl_Id,
1087 Init_Val => Init_Val,
1088 Typ =>
1089 New_Occurrence_Of
1090 (Get_Nth_Arg_Type (RTE (RE_Launch_Kernel), 5), Loc),
1091 Default_Val => Make_Integer_Literal (Loc, 0));
1092 end Build_Shared_Memory_Declaration;
1093
1094 -------------------------------------------
1095 -- Build_Simple_Declaration_With_Default --
1096 -------------------------------------------
1097
1098 function Build_Simple_Declaration_With_Default
1099 (Decl_Id : Entity_Id;
1100 Init_Val : Node_Id;
1101 Typ : Node_Id;
1102 Default_Val : Node_Id) return Node_Id
1103 is
1104 Value : Node_Id := Init_Val;
1105 begin
1106 if No (Value) then
1107 Value := Default_Val;
1108 end if;
1109
1110 return Make_Object_Declaration (Loc,
1111 Defining_Identifier => Decl_Id,
1112 Object_Definition => Typ,
1113 Expression => Value);
1114 end Build_Simple_Declaration_With_Default;
1115
1116 ------------------------------
1117 -- Build_Stream_Declaration --
1118 ------------------------------
1119
1120 function Build_Stream_Declaration
1121 (Decl_Id : Entity_Id;
1122 Init_Val : Node_Id) return Node_Id
1123 is
1124 begin
1125 return Build_Simple_Declaration_With_Default
1126 (Decl_Id => Decl_Id,
1127 Init_Val => Init_Val,
1128 Typ =>
1129 New_Occurrence_Of
1130 (Get_Nth_Arg_Type (RTE (RE_Launch_Kernel), 6), Loc),
1131 Default_Val => Make_Null (Loc));
1132 end Build_Stream_Declaration;
1133
1134 -------------------
1135 -- Etype_Or_Dim3 --
1136 -------------------
1137
1138 function Etype_Or_Dim3 (N : Node_Id) return Node_Id is
1139 begin
1140 if Nkind (N) = N_Aggregate and then Is_Composite_Type (Etype (N)) then
1141 return New_Occurrence_Of (RTE (RE_Dim3), Sloc (N));
1142 end if;
1143
1144 return New_Occurrence_Of (Etype (N), Loc);
1145 end Etype_Or_Dim3;
1146
1147 ----------------------
1148 -- Get_Nth_Arg_Type --
1149 ----------------------
1150
1151 function Get_Nth_Arg_Type
1152 (Subprogram : Entity_Id;
1153 N : Positive) return Entity_Id
1154 is
1155 Argument : Entity_Id := First_Entity (Subprogram);
1156 begin
1157 for J in 2 .. N loop
1158 Next_Entity (Argument);
1159 end loop;
1160
1161 return Etype (Argument);
1162 end Get_Nth_Arg_Type;
1163
1164 ------------------
1165 -- To_Addresses --
1166 ------------------
1167
1168 function To_Addresses (Elmts : Elist_Id) return List_Id is
1169 Result : constant List_Id := New_List;
1170 Elmt : Elmt_Id;
1171 begin
1172 if No (Elmts) then
1173 return Result;
1174 end if;
1175
1176 Elmt := First_Elmt (Elmts);
1177 while Present (Elmt) loop
1178 Append_To (Result,
1179 Make_Attribute_Reference (Loc,
1180 Prefix => New_Occurrence_Of (Node (Elmt), Loc),
1181 Attribute_Name => Name_Address));
1182 Next_Elmt (Elmt);
1183 end loop;
1184
1185 return Result;
1186 end To_Addresses;
1187
1188 -- Local variables
1189
1190 -- Pragma arguments
1191
1192 Procedure_Call : constant Node_Id := Get_Pragma_Arg (Arg_N (N, 1));
1193 Grid_Dimensions : constant Node_Id := Get_Pragma_Arg (Arg_N (N, 2));
1194 Block_Dimensions : constant Node_Id := Get_Pragma_Arg (Arg_N (N, 3));
1195 Shared_Memory : constant Node_Id := Get_Pragma_Arg (Arg_N (N, 4));
1196 CUDA_Stream : constant Node_Id := Get_Pragma_Arg (Arg_N (N, 5));
1197
1198 -- Entities of objects that will be overwritten by calls to cuda runtime
1199 Grids_Id : constant Entity_Id := Make_Temporary (Loc, 'C');
1200 Blocks_Id : constant Entity_Id := Make_Temporary (Loc, 'C');
1201 Memory_Id : constant Entity_Id := Make_Temporary (Loc, 'C');
1202 Stream_Id : constant Entity_Id := Make_Temporary (Loc, 'C');
1203
1204 -- Entities of objects that capture the value of pragma arguments
1205 Temp_Grid : constant Entity_Id := Make_Temporary (Loc, 'C');
1206 Temp_Block : constant Entity_Id := Make_Temporary (Loc, 'C');
1207
1208 -- Declarations for temporary block and grids. These needs to be stored
1209 -- in temporary declarations as the expressions will need to be
1210 -- referenced multiple times but could have side effects.
1211 Temp_Grid_Decl : constant Node_Id := Make_Object_Declaration (Loc,
1212 Defining_Identifier => Temp_Grid,
1213 Object_Definition => Etype_Or_Dim3 (Grid_Dimensions),
1214 Expression => Grid_Dimensions);
1215 Temp_Block_Decl : constant Node_Id := Make_Object_Declaration (Loc,
1216 Defining_Identifier => Temp_Block,
1217 Object_Definition => Etype_Or_Dim3 (Block_Dimensions),
1218 Expression => Block_Dimensions);
1219
1220 -- List holding the entities of the copies of Procedure_Call's arguments
1221
1222 Kernel_Arg_Copies : constant Elist_Id := New_Elmt_List;
1223
1224 -- Entity of the array that contains the address of each of the kernel's
1225 -- arguments.
1226
1227 Kernel_Args_Id : constant Entity_Id := Make_Temporary (Loc, 'C');
1228
1229 -- Calls to the CUDA runtime API.
1230
1231 Launch_Kernel_Call : Node_Id;
1232 Pop_Call : Node_Id;
1233 Push_Call : Node_Id;
1234
1235 -- Declaration of all temporaries required for CUDA API Calls
1236
1237 Blk_Decls : constant List_Id := New_List;
1238
1239 -- Start of processing for CUDA_Execute
1240
1241 begin
1242 -- Append temporary declarations
1243
1244 Append_To (Blk_Decls, Temp_Grid_Decl);
1245 Analyze (Temp_Grid_Decl);
1246
1247 Append_To (Blk_Decls, Temp_Block_Decl);
1248 Analyze (Temp_Block_Decl);
1249
1250 -- Build parameter declarations for CUDA API calls
1251
1252 Append_To
1253 (Blk_Decls,
1254 Build_Dim3_Declaration
1255 (Grids_Id, New_Occurrence_Of (Temp_Grid, Loc)));
1256
1257 Append_To
1258 (Blk_Decls,
1259 Build_Dim3_Declaration
1260 (Blocks_Id, New_Occurrence_Of (Temp_Block, Loc)));
1261
1262 Append_To
1263 (Blk_Decls,
1264 Build_Shared_Memory_Declaration (Memory_Id, Shared_Memory));
1265
1266 Append_To
1267 (Blk_Decls, Build_Stream_Declaration (Stream_Id, CUDA_Stream));
1268
1269 Append_Copies
1270 (Parameter_Associations (Procedure_Call),
1271 Blk_Decls,
1272 Kernel_Arg_Copies);
1273
1274 Append_To
1275 (Blk_Decls,
1276 Build_Kernel_Args_Declaration
1277 (Kernel_Args_Id, Kernel_Arg_Copies));
1278
1279 -- Build calls to the CUDA API
1280
1281 Push_Call :=
1282 Make_Procedure_Call_Statement (Loc,
1283 Name =>
1284 New_Occurrence_Of (RTE (RE_Push_Call_Configuration), Loc),
1285 Parameter_Associations => New_List (
1286 New_Occurrence_Of (Grids_Id, Loc),
1287 New_Occurrence_Of (Blocks_Id, Loc),
1288 New_Occurrence_Of (Memory_Id, Loc),
1289 New_Occurrence_Of (Stream_Id, Loc)));
1290
1291 Pop_Call :=
1292 Make_Procedure_Call_Statement (Loc,
1293 Name =>
1294 New_Occurrence_Of (RTE (RE_Pop_Call_Configuration), Loc),
1295 Parameter_Associations => To_Addresses
1296 (New_Elmt_List
1297 (Grids_Id,
1298 Blocks_Id,
1299 Memory_Id,
1300 Stream_Id)));
1301
1302 Launch_Kernel_Call := Build_Launch_Kernel_Call
1303 (Proc => Entity (Name (Procedure_Call)),
1304 Grid_Dims => Grids_Id,
1305 Block_Dims => Blocks_Id,
1306 Kernel_Arg => Kernel_Args_Id,
1307 Memory => Memory_Id,
1308 Stream => Stream_Id);
1309
1310 -- Finally make the block that holds declarations and calls
1311
1312 Rewrite (N,
1313 Make_Block_Statement (Loc,
1314 Declarations => Blk_Decls,
1315 Handled_Statement_Sequence =>
1316 Make_Handled_Sequence_Of_Statements (Loc,
1317 Statements => New_List (
1318 Push_Call,
1319 Pop_Call,
1320 Launch_Kernel_Call))));
1321 Analyze (N);
1322 end Expand_Pragma_CUDA_Execute;
1323
1324 ----------------------------------
1325 -- Expand_Pragma_Contract_Cases --
1326 ----------------------------------
1327
1328 -- Pragma Contract_Cases is expanded in the following manner:
1329
1330 -- subprogram S is
1331 -- Count : Natural := 0;
1332 -- Flag_1 : Boolean := False;
1333 -- . . .
1334 -- Flag_N : Boolean := False;
1335 -- Flag_N+1 : Boolean := False; -- when "others" present
1336 -- Pref_1 : ...;
1337 -- . . .
1338 -- Pref_M : ...;
1339
1340 -- <preconditions (if any)>
1341
1342 -- -- Evaluate all case guards
1343
1344 -- if Case_Guard_1 then
1345 -- Flag_1 := True;
1346 -- Count := Count + 1;
1347 -- end if;
1348 -- . . .
1349 -- if Case_Guard_N then
1350 -- Flag_N := True;
1351 -- Count := Count + 1;
1352 -- end if;
1353
1354 -- -- Emit errors depending on the number of case guards that
1355 -- -- evaluated to True.
1356
1357 -- if Count = 0 then
1358 -- raise Assertion_Error with "xxx contract cases incomplete";
1359 -- <or>
1360 -- Flag_N+1 := True; -- when "others" present
1361
1362 -- elsif Count > 1 then
1363 -- declare
1364 -- Str0 : constant String :=
1365 -- "contract cases overlap for subprogram ABC";
1366 -- Str1 : constant String :=
1367 -- (if Flag_1 then
1368 -- Str0 & "case guard at xxx evaluates to True"
1369 -- else Str0);
1370 -- StrN : constant String :=
1371 -- (if Flag_N then
1372 -- StrN-1 & "case guard at xxx evaluates to True"
1373 -- else StrN-1);
1374 -- begin
1375 -- raise Assertion_Error with StrN;
1376 -- end;
1377 -- end if;
1378
1379 -- -- Evaluate all attribute 'Old prefixes found in the selected
1380 -- -- consequence.
1381
1382 -- if Flag_1 then
1383 -- Pref_1 := <prefix of 'Old found in Consequence_1>
1384 -- . . .
1385 -- elsif Flag_N then
1386 -- Pref_M := <prefix of 'Old found in Consequence_N>
1387 -- end if;
1388
1389 -- procedure _Postconditions is
1390 -- begin
1391 -- <postconditions (if any)>
1392
1393 -- if Flag_1 and then not Consequence_1 then
1394 -- raise Assertion_Error with "failed contract case at xxx";
1395 -- end if;
1396 -- . . .
1397 -- if Flag_N[+1] and then not Consequence_N[+1] then
1398 -- raise Assertion_Error with "failed contract case at xxx";
1399 -- end if;
1400 -- end _Postconditions;
1401 -- begin
1402 -- . . .
1403 -- end S;
1404
1405 procedure Expand_Pragma_Contract_Cases
1406 (CCs : Node_Id;
1407 Subp_Id : Entity_Id;
1408 Decls : List_Id;
1409 Stmts : in out List_Id)
1410 is
1411 Loc : constant Source_Ptr := Sloc (CCs);
1412
1413 procedure Case_Guard_Error
1414 (Decls : List_Id;
1415 Flag : Entity_Id;
1416 Error_Loc : Source_Ptr;
1417 Msg : in out Entity_Id);
1418 -- Given a declarative list Decls, status flag Flag, the location of the
1419 -- error and a string Msg, construct the following check:
1420 -- Msg : constant String :=
1421 -- (if Flag then
1422 -- Msg & "case guard at Error_Loc evaluates to True"
1423 -- else Msg);
1424 -- The resulting code is added to Decls
1425
1426 procedure Consequence_Error
1427 (Checks : in out Node_Id;
1428 Flag : Entity_Id;
1429 Conseq : Node_Id);
1430 -- Given an if statement Checks, status flag Flag and a consequence
1431 -- Conseq, construct the following check:
1432 -- [els]if Flag and then not Conseq then
1433 -- raise Assertion_Error
1434 -- with "failed contract case at Sloc (Conseq)";
1435 -- [end if;]
1436 -- The resulting code is added to Checks
1437
1438 function Declaration_Of (Id : Entity_Id) return Node_Id;
1439 -- Given the entity Id of a boolean flag, generate:
1440 -- Id : Boolean := False;
1441
1442 procedure Expand_Attributes_In_Consequence
1443 (Decls : List_Id;
1444 Evals : in out Node_Id;
1445 Flag : Entity_Id;
1446 Conseq : Node_Id);
1447 -- Perform specialized expansion of all attribute 'Old references found
1448 -- in consequence Conseq such that at runtime only prefixes coming from
1449 -- the selected consequence are evaluated. Similarly expand attribute
1450 -- 'Result references by replacing them with identifier _result which
1451 -- resolves to the sole formal parameter of procedure _Postconditions.
1452 -- Any temporaries generated in the process are added to declarations
1453 -- Decls. Evals is a complex if statement tasked with the evaluation of
1454 -- all prefixes coming from a single selected consequence. Flag is the
1455 -- corresponding case guard flag. Conseq is the consequence expression.
1456
1457 function Increment (Id : Entity_Id) return Node_Id;
1458 -- Given the entity Id of a numerical variable, generate:
1459 -- Id := Id + 1;
1460
1461 function Set (Id : Entity_Id) return Node_Id;
1462 -- Given the entity Id of a boolean variable, generate:
1463 -- Id := True;
1464
1465 ----------------------
1466 -- Case_Guard_Error --
1467 ----------------------
1468
1469 procedure Case_Guard_Error
1470 (Decls : List_Id;
1471 Flag : Entity_Id;
1472 Error_Loc : Source_Ptr;
1473 Msg : in out Entity_Id)
1474 is
1475 New_Line : constant Character := Character'Val (10);
1476 New_Msg : constant Entity_Id := Make_Temporary (Loc, 'S');
1477
1478 begin
1479 Start_String;
1480 Store_String_Char (New_Line);
1481 Store_String_Chars (" case guard at ");
1482 Store_String_Chars (Build_Location_String (Error_Loc));
1483 Store_String_Chars (" evaluates to True");
1484
1485 -- Generate:
1486 -- New_Msg : constant String :=
1487 -- (if Flag then
1488 -- Msg & "case guard at Error_Loc evaluates to True"
1489 -- else Msg);
1490
1491 Append_To (Decls,
1492 Make_Object_Declaration (Loc,
1493 Defining_Identifier => New_Msg,
1494 Constant_Present => True,
1495 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1496 Expression =>
1497 Make_If_Expression (Loc,
1498 Expressions => New_List (
1499 New_Occurrence_Of (Flag, Loc),
1500
1501 Make_Op_Concat (Loc,
1502 Left_Opnd => New_Occurrence_Of (Msg, Loc),
1503 Right_Opnd => Make_String_Literal (Loc, End_String)),
1504
1505 New_Occurrence_Of (Msg, Loc)))));
1506
1507 Msg := New_Msg;
1508 end Case_Guard_Error;
1509
1510 -----------------------
1511 -- Consequence_Error --
1512 -----------------------
1513
1514 procedure Consequence_Error
1515 (Checks : in out Node_Id;
1516 Flag : Entity_Id;
1517 Conseq : Node_Id)
1518 is
1519 Cond : Node_Id;
1520 Error : Node_Id;
1521
1522 begin
1523 -- Generate:
1524 -- Flag and then not Conseq
1525
1526 Cond :=
1527 Make_And_Then (Loc,
1528 Left_Opnd => New_Occurrence_Of (Flag, Loc),
1529 Right_Opnd =>
1530 Make_Op_Not (Loc,
1531 Right_Opnd => Relocate_Node (Conseq)));
1532
1533 -- Generate:
1534 -- raise Assertion_Error
1535 -- with "failed contract case at Sloc (Conseq)";
1536
1537 Start_String;
1538 Store_String_Chars ("failed contract case at ");
1539 Store_String_Chars (Build_Location_String (Sloc (Conseq)));
1540
1541 Error :=
1542 Make_Procedure_Call_Statement (Loc,
1543 Name =>
1544 New_Occurrence_Of (RTE (RE_Raise_Assert_Failure), Loc),
1545 Parameter_Associations => New_List (
1546 Make_String_Literal (Loc, End_String)));
1547
1548 if No (Checks) then
1549 Checks :=
1550 Make_Implicit_If_Statement (CCs,
1551 Condition => Cond,
1552 Then_Statements => New_List (Error));
1553
1554 Set_Comes_From_Check_Or_Contract (Checks);
1555
1556 else
1557 if No (Elsif_Parts (Checks)) then
1558 Set_Elsif_Parts (Checks, New_List);
1559 end if;
1560
1561 Append_To (Elsif_Parts (Checks),
1562 Make_Elsif_Part (Loc,
1563 Condition => Cond,
1564 Then_Statements => New_List (Error)));
1565 end if;
1566 end Consequence_Error;
1567
1568 --------------------
1569 -- Declaration_Of --
1570 --------------------
1571
1572 function Declaration_Of (Id : Entity_Id) return Node_Id is
1573 begin
1574 return
1575 Make_Object_Declaration (Loc,
1576 Defining_Identifier => Id,
1577 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
1578 Expression => New_Occurrence_Of (Standard_False, Loc));
1579 end Declaration_Of;
1580
1581 --------------------------------------
1582 -- Expand_Attributes_In_Consequence --
1583 --------------------------------------
1584
1585 procedure Expand_Attributes_In_Consequence
1586 (Decls : List_Id;
1587 Evals : in out Node_Id;
1588 Flag : Entity_Id;
1589 Conseq : Node_Id)
1590 is
1591 Eval_Stmts : List_Id := No_List;
1592 -- The evaluation sequence expressed as assignment statements of all
1593 -- prefixes of attribute 'Old found in the current consequence.
1594
1595 function Expand_Attributes (N : Node_Id) return Traverse_Result;
1596 -- Determine whether an arbitrary node denotes attribute 'Old or
1597 -- 'Result and if it does, perform all expansion-related actions.
1598
1599 -----------------------
1600 -- Expand_Attributes --
1601 -----------------------
1602
1603 function Expand_Attributes (N : Node_Id) return Traverse_Result is
1604 Decl : Node_Id;
1605 Pref : Node_Id;
1606 Temp : Entity_Id;
1607 Indirect : Boolean := False;
1608
1609 use Sem_Util.Old_Attr_Util.Indirect_Temps;
1610
1611 procedure Append_For_Indirect_Temp
1612 (N : Node_Id; Is_Eval_Stmt : Boolean);
1613
1614 -- Append either a declaration (which is to be elaborated
1615 -- unconditionally) or an evaluation statement (which is
1616 -- to be executed conditionally).
1617
1618 -------------------------------
1619 -- Append_For_Indirect_Temp --
1620 -------------------------------
1621
1622 procedure Append_For_Indirect_Temp
1623 (N : Node_Id; Is_Eval_Stmt : Boolean)
1624 is
1625 begin
1626 if Is_Eval_Stmt then
1627 Append_To (Eval_Stmts, N);
1628 else
1629 Prepend_To (Decls, N);
1630 -- This use of Prepend (as opposed to Append) is why
1631 -- we have the Append_Decls_In_Reverse_Order parameter.
1632 end if;
1633 end Append_For_Indirect_Temp;
1634
1635 procedure Declare_Indirect_Temporary is new
1636 Declare_Indirect_Temp (
1637 Append_Item => Append_For_Indirect_Temp,
1638 Append_Decls_In_Reverse_Order => True);
1639
1640 -- Start of processing for Expand_Attributes
1641
1642 begin
1643 -- Attribute 'Old
1644
1645 if Is_Attribute_Old (N) then
1646 Pref := Prefix (N);
1647
1648 Indirect := Indirect_Temp_Needed (Etype (Pref));
1649
1650 if Indirect then
1651 if No (Eval_Stmts) then
1652 Eval_Stmts := New_List;
1653 end if;
1654
1655 Declare_Indirect_Temporary
1656 (Attr_Prefix => Pref,
1657 Indirect_Temp => Temp);
1658
1659 -- Declare a temporary of the prefix type with no explicit
1660 -- initial value. If the appropriate contract case is selected
1661 -- at run time, then the temporary will be initialized via an
1662 -- assignment statement.
1663
1664 else
1665 Temp := Make_Temporary (Loc, 'T', Pref);
1666 Set_Etype (Temp, Etype (Pref));
1667
1668 -- Generate a temporary to capture the value of the prefix:
1669 -- Temp : <Pref type>;
1670
1671 Decl :=
1672 Make_Object_Declaration (Loc,
1673 Defining_Identifier => Temp,
1674 Object_Definition =>
1675 New_Occurrence_Of (Etype (Pref), Loc));
1676
1677 -- Place that temporary at the beginning of declarations, to
1678 -- prevent anomalies in the GNATprove flow-analysis pass in
1679 -- the precondition procedure that follows.
1680
1681 Prepend_To (Decls, Decl);
1682
1683 -- Initially Temp is uninitialized (which is required for
1684 -- correctness if default initialization might have side
1685 -- effects). Assign prefix value to temp on Eval_Statement
1686 -- list, so assignment will be executed conditionally.
1687
1688 Mutate_Ekind (Temp, E_Variable);
1689 Set_Suppress_Initialization (Temp);
1690 Analyze (Decl);
1691
1692 if No (Eval_Stmts) then
1693 Eval_Stmts := New_List;
1694 end if;
1695
1696 Append_To (Eval_Stmts,
1697 Make_Assignment_Statement (Loc,
1698 Name => New_Occurrence_Of (Temp, Loc),
1699 Expression => Pref));
1700 end if;
1701
1702 -- Mark the temporary as coming from a 'Old reference
1703
1704 if Present (Temp) then
1705 Set_Stores_Attribute_Old_Prefix (Temp);
1706 end if;
1707
1708 -- Ensure that the prefix is valid
1709
1710 if Validity_Checks_On and then Validity_Check_Operands then
1711 Ensure_Valid (Pref);
1712 end if;
1713
1714 -- Replace the original attribute 'Old by a reference to the
1715 -- generated temporary.
1716
1717 if Indirect then
1718 Rewrite (N,
1719 Indirect_Temp_Value
1720 (Temp => Temp, Typ => Etype (Pref), Loc => Loc));
1721 else
1722 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1723 end if;
1724
1725 -- Attribute 'Result
1726
1727 elsif Is_Attribute_Result (N) then
1728 Rewrite (N, Make_Identifier (Loc, Name_uResult));
1729 end if;
1730
1731 return OK;
1732 end Expand_Attributes;
1733
1734 procedure Expand_Attributes_In is
1735 new Traverse_Proc (Expand_Attributes);
1736
1737 -- Start of processing for Expand_Attributes_In_Consequence
1738
1739 begin
1740 -- Inspect the consequence and expand any attribute 'Old and 'Result
1741 -- references found within.
1742
1743 Expand_Attributes_In (Conseq);
1744
1745 -- The consequence does not contain any attribute 'Old references
1746
1747 if No (Eval_Stmts) then
1748 return;
1749 end if;
1750
1751 -- Augment the machinery to trigger the evaluation of all prefixes
1752 -- found in the step above. If Eval is empty, then this is the first
1753 -- consequence to yield expansion of 'Old. Generate:
1754
1755 -- if Flag then
1756 -- <evaluation statements>
1757 -- end if;
1758
1759 if No (Evals) then
1760 Evals :=
1761 Make_Implicit_If_Statement (CCs,
1762 Condition => New_Occurrence_Of (Flag, Loc),
1763 Then_Statements => Eval_Stmts);
1764
1765 Set_Comes_From_Check_Or_Contract (Evals);
1766
1767 -- Otherwise generate:
1768 -- elsif Flag then
1769 -- <evaluation statements>
1770 -- end if;
1771
1772 else
1773 if No (Elsif_Parts (Evals)) then
1774 Set_Elsif_Parts (Evals, New_List);
1775 end if;
1776
1777 Append_To (Elsif_Parts (Evals),
1778 Make_Elsif_Part (Loc,
1779 Condition => New_Occurrence_Of (Flag, Loc),
1780 Then_Statements => Eval_Stmts));
1781 end if;
1782 end Expand_Attributes_In_Consequence;
1783
1784 ---------------
1785 -- Increment --
1786 ---------------
1787
1788 function Increment (Id : Entity_Id) return Node_Id is
1789 begin
1790 return
1791 Make_Assignment_Statement (Loc,
1792 Name => New_Occurrence_Of (Id, Loc),
1793 Expression =>
1794 Make_Op_Add (Loc,
1795 Left_Opnd => New_Occurrence_Of (Id, Loc),
1796 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1797 end Increment;
1798
1799 ---------
1800 -- Set --
1801 ---------
1802
1803 function Set (Id : Entity_Id) return Node_Id is
1804 begin
1805 return
1806 Make_Assignment_Statement (Loc,
1807 Name => New_Occurrence_Of (Id, Loc),
1808 Expression => New_Occurrence_Of (Standard_True, Loc));
1809 end Set;
1810
1811 -- Local variables
1812
1813 Aggr : constant Node_Id :=
1814 Expression (First (Pragma_Argument_Associations (CCs)));
1815
1816 Case_Guard : Node_Id;
1817 CG_Checks : Node_Id;
1818 CG_Stmts : List_Id;
1819 Conseq : Node_Id;
1820 Conseq_Checks : Node_Id := Empty;
1821 Count : Entity_Id;
1822 Count_Decl : Node_Id;
1823 Error_Decls : List_Id := No_List; -- init to avoid warning
1824 Flag : Entity_Id;
1825 Flag_Decl : Node_Id;
1826 If_Stmt : Node_Id;
1827 Msg_Str : Entity_Id := Empty;
1828 Multiple_PCs : Boolean;
1829 Old_Evals : Node_Id := Empty;
1830 Others_Decl : Node_Id;
1831 Others_Flag : Entity_Id := Empty;
1832 Post_Case : Node_Id;
1833
1834 -- Start of processing for Expand_Pragma_Contract_Cases
1835
1836 begin
1837 -- Do nothing if pragma is not enabled. If pragma is disabled, it has
1838 -- already been rewritten as a Null statement.
1839
1840 if Is_Ignored (CCs) then
1841 return;
1842
1843 -- Guard against malformed contract cases
1844
1845 elsif Nkind (Aggr) /= N_Aggregate then
1846 return;
1847 end if;
1848
1849 -- The expansion of contract cases is quite distributed as it produces
1850 -- various statements to evaluate the case guards and consequences. To
1851 -- preserve the original context, set the Is_Assertion_Expr flag. This
1852 -- aids the Ghost legality checks when verifying the placement of a
1853 -- reference to a Ghost entity.
1854
1855 In_Assertion_Expr := In_Assertion_Expr + 1;
1856
1857 Multiple_PCs := List_Length (Component_Associations (Aggr)) > 1;
1858
1859 -- Create the counter which tracks the number of case guards that
1860 -- evaluate to True.
1861
1862 -- Count : Natural := 0;
1863
1864 Count := Make_Temporary (Loc, 'C');
1865 Count_Decl :=
1866 Make_Object_Declaration (Loc,
1867 Defining_Identifier => Count,
1868 Object_Definition => New_Occurrence_Of (Standard_Natural, Loc),
1869 Expression => Make_Integer_Literal (Loc, 0));
1870
1871 Prepend_To (Decls, Count_Decl);
1872 Analyze (Count_Decl);
1873
1874 -- Create the base error message for multiple overlapping case guards
1875
1876 -- Msg_Str : constant String :=
1877 -- "contract cases overlap for subprogram Subp_Id";
1878
1879 if Multiple_PCs then
1880 Msg_Str := Make_Temporary (Loc, 'S');
1881
1882 Start_String;
1883 Store_String_Chars ("contract cases overlap for subprogram ");
1884 Store_String_Chars (Get_Name_String (Chars (Subp_Id)));
1885
1886 Error_Decls := New_List (
1887 Make_Object_Declaration (Loc,
1888 Defining_Identifier => Msg_Str,
1889 Constant_Present => True,
1890 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1891 Expression => Make_String_Literal (Loc, End_String)));
1892 end if;
1893
1894 -- Process individual post cases
1895
1896 Post_Case := First (Component_Associations (Aggr));
1897 while Present (Post_Case) loop
1898 Case_Guard := First (Choices (Post_Case));
1899 Conseq := Expression (Post_Case);
1900
1901 -- The "others" choice requires special processing
1902
1903 if Nkind (Case_Guard) = N_Others_Choice then
1904 Others_Flag := Make_Temporary (Loc, 'F');
1905 Others_Decl := Declaration_Of (Others_Flag);
1906
1907 Prepend_To (Decls, Others_Decl);
1908 Analyze (Others_Decl);
1909
1910 -- Check possible overlap between a case guard and "others"
1911
1912 if Multiple_PCs and Exception_Extra_Info then
1913 Case_Guard_Error
1914 (Decls => Error_Decls,
1915 Flag => Others_Flag,
1916 Error_Loc => Sloc (Case_Guard),
1917 Msg => Msg_Str);
1918 end if;
1919
1920 -- Inspect the consequence and perform special expansion of any
1921 -- attribute 'Old and 'Result references found within.
1922
1923 Expand_Attributes_In_Consequence
1924 (Decls => Decls,
1925 Evals => Old_Evals,
1926 Flag => Others_Flag,
1927 Conseq => Conseq);
1928
1929 -- Check the corresponding consequence of "others"
1930
1931 Consequence_Error
1932 (Checks => Conseq_Checks,
1933 Flag => Others_Flag,
1934 Conseq => Conseq);
1935
1936 -- Regular post case
1937
1938 else
1939 -- Create the flag which tracks the state of its associated case
1940 -- guard.
1941
1942 Flag := Make_Temporary (Loc, 'F');
1943 Flag_Decl := Declaration_Of (Flag);
1944
1945 Prepend_To (Decls, Flag_Decl);
1946 Analyze (Flag_Decl);
1947
1948 -- The flag is set when the case guard is evaluated to True
1949 -- if Case_Guard then
1950 -- Flag := True;
1951 -- Count := Count + 1;
1952 -- end if;
1953
1954 If_Stmt :=
1955 Make_Implicit_If_Statement (CCs,
1956 Condition => Relocate_Node (Case_Guard),
1957 Then_Statements => New_List (
1958 Set (Flag),
1959 Increment (Count)));
1960
1961 Set_Comes_From_Check_Or_Contract (If_Stmt);
1962
1963 Append_To (Decls, If_Stmt);
1964 Analyze (If_Stmt);
1965
1966 -- Check whether this case guard overlaps with another one
1967
1968 if Multiple_PCs and Exception_Extra_Info then
1969 Case_Guard_Error
1970 (Decls => Error_Decls,
1971 Flag => Flag,
1972 Error_Loc => Sloc (Case_Guard),
1973 Msg => Msg_Str);
1974 end if;
1975
1976 -- Inspect the consequence and perform special expansion of any
1977 -- attribute 'Old and 'Result references found within.
1978
1979 Expand_Attributes_In_Consequence
1980 (Decls => Decls,
1981 Evals => Old_Evals,
1982 Flag => Flag,
1983 Conseq => Conseq);
1984
1985 -- The corresponding consequence of the case guard which evaluated
1986 -- to True must hold on exit from the subprogram.
1987
1988 Consequence_Error
1989 (Checks => Conseq_Checks,
1990 Flag => Flag,
1991 Conseq => Conseq);
1992 end if;
1993
1994 Next (Post_Case);
1995 end loop;
1996
1997 -- Raise Assertion_Error when none of the case guards evaluate to True.
1998 -- The only exception is when we have "others", in which case there is
1999 -- no error because "others" acts as a default True.
2000
2001 -- Generate:
2002 -- Flag := True;
2003
2004 if Present (Others_Flag) then
2005 CG_Stmts := New_List (Set (Others_Flag));
2006
2007 -- Generate:
2008 -- raise Assertion_Error with "xxx contract cases incomplete";
2009
2010 else
2011 Start_String;
2012 Store_String_Chars (Build_Location_String (Loc));
2013 Store_String_Chars (" contract cases incomplete");
2014
2015 CG_Stmts := New_List (
2016 Make_Procedure_Call_Statement (Loc,
2017 Name =>
2018 New_Occurrence_Of (RTE (RE_Raise_Assert_Failure), Loc),
2019 Parameter_Associations => New_List (
2020 Make_String_Literal (Loc, End_String))));
2021 end if;
2022
2023 CG_Checks :=
2024 Make_Implicit_If_Statement (CCs,
2025 Condition =>
2026 Make_Op_Eq (Loc,
2027 Left_Opnd => New_Occurrence_Of (Count, Loc),
2028 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2029 Then_Statements => CG_Stmts);
2030
2031 Set_Comes_From_Check_Or_Contract (CG_Checks);
2032
2033 -- Detect a possible failure due to several case guards evaluating to
2034 -- True.
2035
2036 -- Generate:
2037 -- elsif Count > 0 then
2038 -- declare
2039 -- <Error_Decls>
2040 -- begin
2041 -- raise Assertion_Error with <Msg_Str>;
2042 -- end if;
2043
2044 if Multiple_PCs then
2045 Set_Elsif_Parts (CG_Checks, New_List (
2046 Make_Elsif_Part (Loc,
2047 Condition =>
2048 Make_Op_Gt (Loc,
2049 Left_Opnd => New_Occurrence_Of (Count, Loc),
2050 Right_Opnd => Make_Integer_Literal (Loc, 1)),
2051
2052 Then_Statements => New_List (
2053 Make_Block_Statement (Loc,
2054 Declarations => Error_Decls,
2055 Handled_Statement_Sequence =>
2056 Make_Handled_Sequence_Of_Statements (Loc,
2057 Statements => New_List (
2058 Make_Procedure_Call_Statement (Loc,
2059 Name =>
2060 New_Occurrence_Of
2061 (RTE (RE_Raise_Assert_Failure), Loc),
2062 Parameter_Associations => New_List (
2063 New_Occurrence_Of (Msg_Str, Loc))))))))));
2064 end if;
2065
2066 -- Append the checks, but do not analyze them at this point, because
2067 -- contracts get potentially expanded as part of a wrapper which gets
2068 -- fully analyzed once it is fully formed.
2069
2070 Append_To (Decls, CG_Checks);
2071
2072 -- Once all case guards are evaluated and checked, evaluate any prefixes
2073 -- of attribute 'Old founds in the selected consequence.
2074
2075 if Present (Old_Evals) then
2076 Append_To (Decls, Old_Evals);
2077 end if;
2078
2079 -- Raise Assertion_Error when the corresponding consequence of a case
2080 -- guard that evaluated to True fails.
2081
2082 Append_New_To (Stmts, Conseq_Checks);
2083
2084 In_Assertion_Expr := In_Assertion_Expr - 1;
2085 end Expand_Pragma_Contract_Cases;
2086
2087 -------------------------------------
2088 -- Expand_Pragma_Exceptional_Cases --
2089 -------------------------------------
2090
2091 -- Aspect Exceptional_Cases shoule be expanded in the following manner:
2092
2093 -- Original declaration
2094
2095 -- procedure P (...) with
2096 -- Exceptional_Cases =>
2097 -- (Exp_1 => True,
2098 -- Exp_2 => Post_4);
2099
2100 -- Expanded body
2101
2102 -- procedure P (...) is
2103 -- begin
2104 -- -- normal body of of P
2105 -- declare
2106 -- ...
2107 -- end;
2108 --
2109 -- exception
2110 -- when Exp1 =>
2111 -- pragma Assert (True);
2112 -- raise;
2113 -- when E : Exp2 =>
2114 -- pragma Assert (Post_4);
2115 -- raise;
2116 -- when others =>
2117 -- pragma Assert (False);
2118 -- raise;
2119 -- end P;
2120
2121 procedure Expand_Pragma_Exceptional_Cases (Prag : Node_Id) is
2122 begin
2123 -- Currently we don't expand this pragma
2124
2125 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2126 end Expand_Pragma_Exceptional_Cases;
2127
2128 ------------------------------
2129 -- Expand_Pragma_Exit_Cases --
2130 ------------------------------
2131
2132 -- Aspect Exit_Cases shoule be expanded in the following manner:
2133
2134 -- subprogram S is
2135 -- Count : Natural := 0;
2136 -- Flag_1 : Boolean := False;
2137 -- . . .
2138 -- Flag_N : Boolean := False;
2139 -- Flag_N[+1] : Boolean := False; -- if others present
2140
2141 -- <preconditions (if any)>
2142
2143 -- -- Evaluate all case guards
2144
2145 -- if Case_Guard_1 then
2146 -- Flag_1 := True;
2147 -- Count := Count + 1;
2148 -- end if;
2149 -- . . .
2150 -- if Case_Guard_N then
2151 -- Flag_N := True;
2152 -- Count := Count + 1;
2153 -- end if;
2154
2155 -- -- Emit errors depending on the number of case guards that
2156 -- -- evaluated to True.
2157
2158 -- if Count = 0 then
2159 -- Flag_N+1 := True; -- if others present
2160 -- elsif Count > 1 then
2161 -- declare
2162 -- Str_Base : constant String :=
2163 -- "exit cases overlap for subprogram S";
2164 -- Str_Normal_Return : constant String :=
2165 -- (if Flag_... then
2166 -- Str_Base & "case guard at xxx evaluates to True"
2167 -- else Str_Base);
2168 -- StrN : constant String :=
2169 -- (if Flag_... then
2170 -- StrN-1 & "case guard at xxx evaluates to True"
2171 -- else StrN-1);
2172 -- begin
2173 -- raise Assertion_Error with StrN;
2174 -- end;
2175 -- end if;
2176
2177 -- procedure _Postconditions is
2178 -- begin
2179 -- <postconditions (if any)>
2180
2181 -- if Flag_1 and then Exit_Kind_1 /= Normal_Return then
2182 -- raise Assertion_Error with
2183 -- "subprogram returned normally, failed exit case at xxx";
2184 -- end if;
2185 -- . . .
2186 -- if Flag_N[+1] and then Exit_Kind_N[+1] /= Normal_Return then
2187 -- raise Assertion_Error with
2188 -- "subprogram returned normally, failed exit case at xxx";
2189 -- end if;
2190 -- end _Postconditions;
2191 -- begin
2192 --
2193 -- -- normal body of of P
2194 -- declare
2195 -- ...
2196 -- end;
2197 --
2198 -- exception
2199 -- when E : Exp2 =>
2200 -- if Flag_1
2201 -- and then Exit_Kind_1 /= Exception_Raised
2202 -- and then (Nkind (Exit_Kind_1) /= N_Associated_Component
2203 -- or else Expression (Exit_Kind_1) /= E)
2204 -- then
2205 -- raise Assertion_Error with
2206 -- "subprogram raised " & E & ", failed exit case at xxx";
2207 -- end if;
2208 -- . . .
2209 -- raise;
2210 -- end S;
2211
2212 procedure Expand_Pragma_Exit_Cases (Prag : Node_Id) is
2213 begin
2214 -- Currently we don't expand this pragma
2215
2216 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2217 end Expand_Pragma_Exit_Cases;
2218
2219 ---------------------------------------
2220 -- Expand_Pragma_Import_Or_Interface --
2221 ---------------------------------------
2222
2223 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
2224 Def_Id : Entity_Id;
2225
2226 begin
2227 -- In Relaxed_RM_Semantics, support old Ada 83 style:
2228 -- pragma Import (Entity, "external name");
2229
2230 if Relaxed_RM_Semantics
2231 and then List_Length (Pragma_Argument_Associations (N)) = 2
2232 and then Pragma_Name (N) = Name_Import
2233 and then Nkind (Arg_N (N, 2)) = N_String_Literal
2234 then
2235 Def_Id := Entity (Arg_N (N, 1));
2236 else
2237 Def_Id := Entity (Arg_N (N, 2));
2238 end if;
2239
2240 -- Variable case (we have to undo any initialization already done)
2241
2242 if Ekind (Def_Id) = E_Variable then
2243 Undo_Initialization (Def_Id, N);
2244
2245 -- Case of exception with convention C++
2246
2247 elsif Ekind (Def_Id) = E_Exception
2248 and then Convention (Def_Id) = Convention_CPP
2249 then
2250 -- Import a C++ convention
2251
2252 declare
2253 procedure Check_Class_Suffix;
2254 -- Check whether the External Name string designated by
2255 -- Name, declared below, ends with "'class" and, if so,
2256 -- set Lang_Id accordingly, and drop the suffix from Name
2257 -- and from Rtti_Name.
2258
2259 Loc : constant Source_Ptr := Sloc (N);
2260 Dum : constant Entity_Id := Make_Temporary (Loc, 'D');
2261 Lang_Id : Character := 'C';
2262 Rtti_Name : Node_Id := Arg_N (N, 3);
2263 Name : String_Id := Strval (Get_Pragma_Arg (Rtti_Name));
2264 Exdata : List_Id;
2265 Lang_Char : Node_Id;
2266 Foreign_Data : Node_Id;
2267
2268 ------------------------
2269 -- Check_Class_Suffix --
2270 ------------------------
2271
2272 procedure Check_Class_Suffix is
2273 Attr : constant String := "'class";
2274 J : Nat := String_Length (Name);
2275 begin
2276 -- We can't end with "'class" if there's no room for it
2277
2278 if J < Attr'Length then
2279 return;
2280 end if;
2281
2282 -- Check that we end with "'class", ignoring case.
2283 -- Return if we don't.
2284
2285 for K in reverse 1 .. Attr'Length loop
2286 if To_Lower (Get_Character (Get_String_Char (Name, J)))
2287 /= Attr (K)
2288 then
2289 return;
2290 end if;
2291 J := J - 1;
2292 end loop;
2293
2294 -- Build a new string without the pseudo attribute.
2295 -- Change Lang_Id to use base-type matching.
2296
2297 Start_String;
2298 for I in 1 .. J loop
2299 Store_String_Char (Get_String_Char (Name, I));
2300 end loop;
2301 Name := End_String;
2302 Rtti_Name := Make_String_Literal (Sloc (Rtti_Name),
2303 Strval => Name);
2304 Lang_Id := 'B';
2305 end Check_Class_Suffix;
2306
2307 begin
2308 Check_Class_Suffix;
2309
2310 Exdata := Component_Associations (Expression (Parent (Def_Id)));
2311
2312 Lang_Char := Next (First (Exdata));
2313
2314 -- Change the one-character language designator to Lang_Id
2315
2316 Rewrite (Expression (Lang_Char),
2317 Make_Character_Literal (Loc,
2318 Chars => Name_uC,
2319 Char_Literal_Value => UI_From_CC (Get_Char_Code (Lang_Id))));
2320 Analyze (Expression (Lang_Char));
2321
2322 -- Change the value of Foreign_Data
2323
2324 Foreign_Data := Next (Next (Next (Next (Lang_Char))));
2325
2326 Insert_Actions (Def_Id, New_List (
2327 Make_Object_Declaration (Loc,
2328 Defining_Identifier => Dum,
2329 Object_Definition =>
2330 New_Occurrence_Of (Standard_Character, Loc)),
2331
2332 Make_Pragma (Loc,
2333 Chars => Name_Import,
2334 Pragma_Argument_Associations => New_List (
2335 Make_Pragma_Argument_Association (Loc,
2336 Expression => Make_Identifier (Loc, Name_Ada)),
2337
2338 Make_Pragma_Argument_Association (Loc,
2339 Expression => Make_Identifier (Loc, Chars (Dum))),
2340
2341 Make_Pragma_Argument_Association (Loc,
2342 Chars => Name_External_Name,
2343 Expression => Relocate_Node (Rtti_Name))))));
2344
2345 Rewrite (Expression (Foreign_Data),
2346 OK_Convert_To (Standard_Address,
2347 Make_Attribute_Reference (Loc,
2348 Prefix => Make_Identifier (Loc, Chars (Dum)),
2349 Attribute_Name => Name_Address)));
2350 Analyze (Expression (Foreign_Data));
2351 end;
2352
2353 -- No special expansion required for any other case
2354
2355 else
2356 null;
2357 end if;
2358 end Expand_Pragma_Import_Or_Interface;
2359
2360 -------------------------------------
2361 -- Expand_Pragma_Initial_Condition --
2362 -------------------------------------
2363
2364 procedure Expand_Pragma_Initial_Condition
2365 (Pack_Id : Entity_Id;
2366 N : Node_Id)
2367 is
2368 procedure Extract_Package_Body_Lists
2369 (Pack_Body : Node_Id;
2370 Body_List : out List_Id;
2371 Call_List : out List_Id;
2372 Spec_List : out List_Id);
2373 -- Obtain the various declarative and statement lists of package body
2374 -- Pack_Body needed to insert the initial condition procedure and the
2375 -- call to it. The lists are as follows:
2376 --
2377 -- * Body_List - used to insert the initial condition procedure body
2378 --
2379 -- * Call_List - used to insert the call to the initial condition
2380 -- procedure.
2381 --
2382 -- * Spec_List - used to insert the initial condition procedure spec
2383
2384 procedure Extract_Package_Declaration_Lists
2385 (Pack_Decl : Node_Id;
2386 Body_List : out List_Id;
2387 Call_List : out List_Id;
2388 Spec_List : out List_Id);
2389 -- Obtain the various declarative lists of package declaration Pack_Decl
2390 -- needed to insert the initial condition procedure and the call to it.
2391 -- The lists are as follows:
2392 --
2393 -- * Body_List - used to insert the initial condition procedure body
2394 --
2395 -- * Call_List - used to insert the call to the initial condition
2396 -- procedure.
2397 --
2398 -- * Spec_List - used to insert the initial condition procedure spec
2399
2400 --------------------------------
2401 -- Extract_Package_Body_Lists --
2402 --------------------------------
2403
2404 procedure Extract_Package_Body_Lists
2405 (Pack_Body : Node_Id;
2406 Body_List : out List_Id;
2407 Call_List : out List_Id;
2408 Spec_List : out List_Id)
2409 is
2410 Pack_Spec : constant Entity_Id := Corresponding_Spec (Pack_Body);
2411
2412 Dummy_1 : List_Id;
2413 Dummy_2 : List_Id;
2414 HSS : Node_Id;
2415
2416 begin
2417 pragma Assert (Present (Pack_Spec));
2418
2419 -- The different parts of the invariant procedure are inserted as
2420 -- follows:
2421
2422 -- package Pack is package body Pack is
2423 -- <IC spec> <IC body>
2424 -- private begin
2425 -- ... <IC call>
2426 -- end Pack; end Pack;
2427
2428 -- The initial condition procedure spec is inserted in the visible
2429 -- declaration of the corresponding package spec.
2430
2431 Extract_Package_Declaration_Lists
2432 (Pack_Decl => Unit_Declaration_Node (Pack_Spec),
2433 Body_List => Dummy_1,
2434 Call_List => Dummy_2,
2435 Spec_List => Spec_List);
2436
2437 -- The initial condition procedure body is added to the declarations
2438 -- of the package body.
2439
2440 Body_List := Declarations (Pack_Body);
2441
2442 if No (Body_List) then
2443 Body_List := New_List;
2444 Set_Declarations (Pack_Body, Body_List);
2445 end if;
2446
2447 -- The call to the initial condition procedure is inserted in the
2448 -- statements of the package body.
2449
2450 HSS := Handled_Statement_Sequence (Pack_Body);
2451
2452 if No (HSS) then
2453 HSS :=
2454 Make_Handled_Sequence_Of_Statements (Sloc (Pack_Body),
2455 Statements => New_List);
2456 Set_Handled_Statement_Sequence (Pack_Body, HSS);
2457 end if;
2458
2459 Call_List := Statements (HSS);
2460 end Extract_Package_Body_Lists;
2461
2462 ---------------------------------------
2463 -- Extract_Package_Declaration_Lists --
2464 ---------------------------------------
2465
2466 procedure Extract_Package_Declaration_Lists
2467 (Pack_Decl : Node_Id;
2468 Body_List : out List_Id;
2469 Call_List : out List_Id;
2470 Spec_List : out List_Id)
2471 is
2472 Pack_Spec : constant Node_Id := Specification (Pack_Decl);
2473
2474 begin
2475 -- The different parts of the invariant procedure are inserted as
2476 -- follows:
2477
2478 -- package Pack is
2479 -- <IC spec>
2480 -- <IC body>
2481 -- private
2482 -- <IC call>
2483 -- end Pack;
2484
2485 -- The initial condition procedure spec and body are inserted in the
2486 -- visible declarations of the package spec.
2487
2488 Body_List := Visible_Declarations (Pack_Spec);
2489
2490 if No (Body_List) then
2491 Body_List := New_List;
2492 Set_Visible_Declarations (Pack_Spec, Body_List);
2493 end if;
2494
2495 Spec_List := Body_List;
2496
2497 -- The call to the initial procedure is inserted in the private
2498 -- declarations of the package spec.
2499
2500 Call_List := Private_Declarations (Pack_Spec);
2501
2502 if No (Call_List) then
2503 Call_List := New_List;
2504 Set_Private_Declarations (Pack_Spec, Call_List);
2505 end if;
2506 end Extract_Package_Declaration_Lists;
2507
2508 -- Local variables
2509
2510 IC_Prag : constant Node_Id :=
2511 Get_Pragma (Pack_Id, Pragma_Initial_Condition);
2512
2513 Body_List : List_Id;
2514 Call : Node_Id;
2515 Call_List : List_Id;
2516 Call_Loc : Source_Ptr;
2517 Expr : Node_Id;
2518 Loc : Source_Ptr;
2519 Proc_Body : Node_Id;
2520 Proc_Body_Id : Entity_Id;
2521 Proc_Decl : Node_Id;
2522 Proc_Id : Entity_Id;
2523 Spec_List : List_Id;
2524
2525 -- Start of processing for Expand_Pragma_Initial_Condition
2526
2527 begin
2528 -- Nothing to do when the package is not subject to an Initial_Condition
2529 -- pragma.
2530
2531 if No (IC_Prag) then
2532 return;
2533 end if;
2534
2535 Expr := Get_Pragma_Arg (First (Pragma_Argument_Associations (IC_Prag)));
2536 Loc := Sloc (IC_Prag);
2537
2538 -- Nothing to do when the pragma is ignored because its semantics are
2539 -- suppressed.
2540
2541 if Is_Ignored (IC_Prag) then
2542 return;
2543
2544 -- Nothing to do when the pragma or its argument are illegal because
2545 -- there is no valid expression to check.
2546
2547 elsif Error_Posted (IC_Prag) or else Error_Posted (Expr) then
2548 return;
2549 end if;
2550
2551 -- Obtain the various lists of the context where the individual pieces
2552 -- of the initial condition procedure are to be inserted.
2553
2554 if Nkind (N) = N_Package_Body then
2555 Extract_Package_Body_Lists
2556 (Pack_Body => N,
2557 Body_List => Body_List,
2558 Call_List => Call_List,
2559 Spec_List => Spec_List);
2560
2561 elsif Nkind (N) = N_Package_Declaration then
2562 Extract_Package_Declaration_Lists
2563 (Pack_Decl => N,
2564 Body_List => Body_List,
2565 Call_List => Call_List,
2566 Spec_List => Spec_List);
2567
2568 -- This routine should not be used on anything other than packages
2569
2570 else
2571 pragma Assert (False);
2572 return;
2573 end if;
2574
2575 Proc_Id :=
2576 Make_Defining_Identifier (Loc,
2577 Chars => New_External_Name (Chars (Pack_Id), "Initial_Condition"));
2578
2579 Mutate_Ekind (Proc_Id, E_Procedure);
2580 Set_Is_Initial_Condition_Procedure (Proc_Id);
2581
2582 -- Generate:
2583 -- procedure <Pack_Id>Initial_Condition;
2584
2585 Proc_Decl :=
2586 Make_Subprogram_Declaration (Loc,
2587 Make_Procedure_Specification (Loc,
2588 Defining_Unit_Name => Proc_Id));
2589
2590 Append_To (Spec_List, Proc_Decl);
2591
2592 -- The initial condition procedure requires debug info when initial
2593 -- condition is subject to Source Coverage Obligations.
2594
2595 if Generate_SCO then
2596 Set_Debug_Info_Needed (Proc_Id);
2597 end if;
2598
2599 -- Generate:
2600 -- procedure <Pack_Id>Initial_Condition is
2601 -- begin
2602 -- pragma Check (Initial_Condition, <Expr>);
2603 -- end <Pack_Id>Initial_Condition;
2604
2605 Proc_Body :=
2606 Make_Subprogram_Body (Loc,
2607 Specification =>
2608 Copy_Subprogram_Spec (Specification (Proc_Decl)),
2609 Declarations => Empty_List,
2610 Handled_Statement_Sequence =>
2611 Make_Handled_Sequence_Of_Statements (Loc,
2612 Statements => New_List (
2613 Make_Pragma (Loc,
2614 Chars => Name_Check,
2615 Pragma_Argument_Associations => New_List (
2616 Make_Pragma_Argument_Association (Loc,
2617 Expression =>
2618 Make_Identifier (Loc, Name_Initial_Condition)),
2619 Make_Pragma_Argument_Association (Loc,
2620 Expression => New_Copy_Tree (Expr)))))));
2621
2622 Append_To (Body_List, Proc_Body);
2623
2624 -- The initial condition procedure requires debug info when initial
2625 -- condition is subject to Source Coverage Obligations.
2626
2627 Proc_Body_Id := Defining_Entity (Proc_Body);
2628
2629 if Generate_SCO then
2630 Set_Debug_Info_Needed (Proc_Body_Id);
2631 end if;
2632
2633 -- The location of the initial condition procedure call must be as close
2634 -- as possible to the intended semantic location of the check because
2635 -- the ABE mechanism relies heavily on accurate locations.
2636
2637 Call_Loc := End_Keyword_Location (N);
2638
2639 -- Generate:
2640 -- <Pack_Id>Initial_Condition;
2641
2642 Call :=
2643 Make_Procedure_Call_Statement (Call_Loc,
2644 Name => New_Occurrence_Of (Proc_Id, Call_Loc));
2645
2646 Append_To (Call_List, Call);
2647
2648 Analyze (Proc_Decl);
2649 Analyze (Proc_Body);
2650 Analyze (Call);
2651 end Expand_Pragma_Initial_Condition;
2652
2653 ------------------------------------
2654 -- Expand_Pragma_Inspection_Point --
2655 ------------------------------------
2656
2657 -- If no argument is given, then we supply a default argument list that
2658 -- includes all objects declared at the source level in all subprograms
2659 -- that enclose the inspection point pragma.
2660
2661 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
2662 Loc : constant Source_Ptr := Sloc (N);
2663
2664 A : List_Id;
2665 Assoc : Node_Id;
2666 Faulty_Arg : Node_Id := Empty;
2667 E : Entity_Id;
2668 S : Entity_Id;
2669
2670 begin
2671 if No (Pragma_Argument_Associations (N)) then
2672 A := New_List;
2673 S := Current_Scope;
2674
2675 while S /= Standard_Standard loop
2676 E := First_Entity (S);
2677 while Present (E) loop
2678 if Comes_From_Source (E)
2679 and then Is_Object (E)
2680 and then not Is_Entry_Formal (E)
2681 and then not Is_Formal_Object (E)
2682 and then Ekind (E) /= E_Component
2683 and then Ekind (E) /= E_Discriminant
2684 then
2685 Append_To (A,
2686 Make_Pragma_Argument_Association (Loc,
2687 Expression => New_Occurrence_Of (E, Loc)));
2688 end if;
2689
2690 Next_Entity (E);
2691 end loop;
2692
2693 S := Scope (S);
2694 end loop;
2695
2696 Set_Pragma_Argument_Associations (N, A);
2697 end if;
2698
2699 -- Process the arguments of the pragma
2700
2701 Assoc := First (Pragma_Argument_Associations (N));
2702 while Present (Assoc) loop
2703 -- The back end may need to take the address of the object
2704
2705 Set_Address_Taken (Entity (Expression (Assoc)));
2706
2707 -- If any of the objects have a freeze node, it must appear before
2708 -- pragma Inspection_Point, otherwise the entity won't be elaborated
2709 -- when Gigi processes the pragma.
2710
2711 if Has_Delayed_Freeze (Entity (Expression (Assoc)))
2712 and then not Is_Frozen (Entity (Expression (Assoc)))
2713 then
2714 Error_Msg_NE
2715 ("??inspection point references unfrozen object &",
2716 Assoc,
2717 Entity (Expression (Assoc)));
2718 Faulty_Arg := Assoc;
2719 end if;
2720
2721 Next (Assoc);
2722 end loop;
2723
2724 -- When the above requirement isn't met, turn the pragma into a no-op
2725
2726 if Present (Faulty_Arg) then
2727 Error_Msg_Sloc := Sloc (Faulty_Arg);
2728 Error_Msg_N ("\pragma Inspection_Point # will be ignored",
2729 Faulty_Arg);
2730
2731 -- We can't just remove the pragma from the tree as it might be
2732 -- iterated over by the caller. Turn it into a null statement
2733 -- instead.
2734
2735 Rewrite (N, Make_Null_Statement (Loc));
2736 end if;
2737 end Expand_Pragma_Inspection_Point;
2738
2739 --------------------------------------
2740 -- Expand_Pragma_Interrupt_Priority --
2741 --------------------------------------
2742
2743 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
2744
2745 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
2746 Loc : constant Source_Ptr := Sloc (N);
2747 begin
2748 if No (Pragma_Argument_Associations (N)) then
2749 Set_Pragma_Argument_Associations (N, New_List (
2750 Make_Pragma_Argument_Association (Loc,
2751 Expression =>
2752 Make_Attribute_Reference (Loc,
2753 Prefix =>
2754 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
2755 Attribute_Name => Name_Last))));
2756 end if;
2757 end Expand_Pragma_Interrupt_Priority;
2758
2759 --------------------------------
2760 -- Expand_Pragma_Loop_Variant --
2761 --------------------------------
2762
2763 -- Pragma Loop_Variant is expanded in the following manner:
2764
2765 -- Original code
2766
2767 -- for | while ... loop
2768 -- <preceding source statements>
2769 -- pragma Loop_Variant
2770 -- (Increases => Incr_Expr,
2771 -- Decreases => Decr_Expr);
2772 -- <succeeding source statements>
2773 -- end loop;
2774
2775 -- Expanded code
2776
2777 -- Curr_1 : <type of Incr_Expr>;
2778 -- Curr_2 : <type of Decr_Expr>;
2779 -- Old_1 : <type of Incr_Expr>;
2780 -- Old_2 : <type of Decr_Expr>;
2781 -- Flag : Boolean := False;
2782
2783 -- for | while ... loop
2784 -- <preceding source statements>
2785
2786 -- if Flag then
2787 -- Old_1 := Curr_1;
2788 -- Old_2 := Curr_2;
2789 -- end if;
2790
2791 -- Curr_1 := <Incr_Expr>;
2792 -- Curr_2 := <Decr_Expr>;
2793
2794 -- if Flag then
2795 -- if Curr_1 /= Old_1 then
2796 -- pragma Check (Loop_Variant, Curr_1 > Old_1);
2797 -- else
2798 -- pragma Check (Loop_Variant, Curr_2 < Old_2);
2799 -- end if;
2800 -- else
2801 -- Flag := True;
2802 -- end if;
2803
2804 -- <succeeding source statements>
2805 -- end loop;
2806
2807 procedure Expand_Pragma_Loop_Variant (N : Node_Id) is
2808 Loc : constant Source_Ptr := Sloc (N);
2809 Last_Var : constant Node_Id :=
2810 Last (Pragma_Argument_Associations (N));
2811
2812 Curr_Assign : List_Id := No_List;
2813 Flag_Id : Entity_Id := Empty;
2814 If_Stmt : Node_Id := Empty;
2815 Old_Assign : List_Id := No_List;
2816 Loop_Scop : Entity_Id;
2817 Loop_Stmt : Node_Id;
2818 Variant : Node_Id;
2819
2820 procedure Process_Variant (Variant : Node_Id; Is_Last : Boolean);
2821 -- Process a single increasing / decreasing termination variant. Flag
2822 -- Is_Last should be set when processing the last variant.
2823
2824 ---------------------
2825 -- Process_Variant --
2826 ---------------------
2827
2828 procedure Process_Variant (Variant : Node_Id; Is_Last : Boolean) is
2829 Expr : constant Node_Id := Expression (Variant);
2830 Expr_Typ : constant Entity_Id := Etype (Expr);
2831 Loc : constant Source_Ptr := Sloc (Expr);
2832 Loop_Loc : constant Source_Ptr := Sloc (Loop_Stmt);
2833 Curr_Id : Entity_Id;
2834 Old_Id : Entity_Id;
2835 Prag : Node_Id;
2836
2837 begin
2838 -- All temporaries generated in this routine must be inserted before
2839 -- the related loop statement. Ensure that the proper scope is on the
2840 -- stack when analyzing the temporaries. Note that we also use the
2841 -- Sloc of the related loop.
2842
2843 Push_Scope (Scope (Loop_Scop));
2844
2845 -- Step 1: Create the declaration of the flag which controls the
2846 -- behavior of the assertion on the first iteration of the loop.
2847
2848 if No (Flag_Id) then
2849
2850 -- Generate:
2851 -- Flag : Boolean := False;
2852
2853 Flag_Id := Make_Temporary (Loop_Loc, 'F');
2854
2855 Insert_Action (Loop_Stmt,
2856 Make_Object_Declaration (Loop_Loc,
2857 Defining_Identifier => Flag_Id,
2858 Object_Definition =>
2859 New_Occurrence_Of (Standard_Boolean, Loop_Loc),
2860 Expression =>
2861 New_Occurrence_Of (Standard_False, Loop_Loc)));
2862
2863 -- Prevent an unwanted optimization where the Current_Value of
2864 -- the flag eliminates the if statement which stores the variant
2865 -- values coming from the previous iteration.
2866
2867 -- Flag : Boolean := False;
2868 -- loop
2869 -- if Flag then -- condition rewritten to False
2870 -- Old_N := Curr_N; -- and if statement eliminated
2871 -- end if;
2872 -- . . .
2873 -- Flag := True;
2874 -- end loop;
2875
2876 Set_Current_Value (Flag_Id, Empty);
2877 end if;
2878
2879 -- Step 2: Create the temporaries which store the old and current
2880 -- values of the associated expression.
2881
2882 -- Generate:
2883 -- Curr : <type of Expr>;
2884
2885 Curr_Id := Make_Temporary (Loc, 'C');
2886
2887 Insert_Action (Loop_Stmt,
2888 Make_Object_Declaration (Loop_Loc,
2889 Defining_Identifier => Curr_Id,
2890 Object_Definition => New_Occurrence_Of (Expr_Typ, Loop_Loc)));
2891
2892 -- Generate:
2893 -- Old : <type of Expr>;
2894
2895 Old_Id := Make_Temporary (Loc, 'P');
2896
2897 Insert_Action (Loop_Stmt,
2898 Make_Object_Declaration (Loop_Loc,
2899 Defining_Identifier => Old_Id,
2900 Object_Definition => New_Occurrence_Of (Expr_Typ, Loop_Loc)));
2901
2902 -- Restore original scope after all temporaries have been analyzed
2903
2904 Pop_Scope;
2905
2906 -- Step 3: Store value of the expression from the previous iteration
2907
2908 -- Generate:
2909 -- Old := Curr;
2910
2911 Append_New_To (Old_Assign,
2912 Make_Assignment_Statement (Loc,
2913 Name => New_Occurrence_Of (Old_Id, Loc),
2914 Expression => New_Occurrence_Of (Curr_Id, Loc)));
2915
2916 -- Step 4: Store the current value of the expression
2917
2918 -- Generate:
2919 -- Curr := <Expr>;
2920
2921 Append_New_To (Curr_Assign,
2922 Make_Assignment_Statement (Loc,
2923 Name => New_Occurrence_Of (Curr_Id, Loc),
2924 Expression => Relocate_Node (Expr)));
2925
2926 -- Step 5: Create corresponding assertion to verify change of value
2927
2928 -- Generate:
2929 -- pragma Check (Loop_Variant, Curr <|> Old);
2930
2931 Prag :=
2932 Make_Pragma (Loc,
2933 Chars => Name_Check,
2934 Pragma_Argument_Associations => New_List (
2935 Make_Pragma_Argument_Association (Loc,
2936 Expression => Make_Identifier (Loc, Name_Loop_Variant)),
2937 Make_Pragma_Argument_Association (Loc,
2938 Expression =>
2939 Make_Variant_Comparison (Loc,
2940 Mode => Chars (Variant),
2941 Typ => Expr_Typ,
2942 Curr_Val => New_Occurrence_Of (Curr_Id, Loc),
2943 Old_Val => New_Occurrence_Of (Old_Id, Loc)))));
2944
2945 -- Generate:
2946 -- if Curr /= Old then
2947 -- <Prag>;
2948
2949 if No (If_Stmt) then
2950
2951 -- When there is just one termination variant, do not compare the
2952 -- old and current value for equality, just check the pragma.
2953
2954 if Is_Last then
2955 If_Stmt := Prag;
2956 else
2957 If_Stmt :=
2958 Make_If_Statement (Loc,
2959 Condition =>
2960 Make_Op_Ne (Loc,
2961 Left_Opnd => New_Occurrence_Of (Curr_Id, Loc),
2962 Right_Opnd => New_Occurrence_Of (Old_Id, Loc)),
2963 Then_Statements => New_List (Prag));
2964 end if;
2965
2966 -- Generate:
2967 -- else
2968 -- <Prag>;
2969 -- end if;
2970
2971 elsif Is_Last then
2972 Set_Else_Statements (If_Stmt, New_List (Prag));
2973
2974 -- Generate:
2975 -- elsif Curr /= Old then
2976 -- <Prag>;
2977
2978 else
2979 if Elsif_Parts (If_Stmt) = No_List then
2980 Set_Elsif_Parts (If_Stmt, New_List);
2981 end if;
2982
2983 Append_To (Elsif_Parts (If_Stmt),
2984 Make_Elsif_Part (Loc,
2985 Condition =>
2986 Make_Op_Ne (Loc,
2987 Left_Opnd => New_Occurrence_Of (Curr_Id, Loc),
2988 Right_Opnd => New_Occurrence_Of (Old_Id, Loc)),
2989 Then_Statements => New_List (Prag)));
2990 end if;
2991 end Process_Variant;
2992
2993 -- Start of processing for Expand_Pragma_Loop_Variant
2994
2995 begin
2996 -- If pragma is not enabled, rewrite as Null statement. If pragma is
2997 -- disabled, it has already been rewritten as a Null statement.
2998 --
2999 -- Likewise, ignore structural variants for execution.
3000 --
3001 -- Also do this in CodePeer mode, because the expanded code is too
3002 -- complicated for CodePeer to analyse.
3003
3004 if Is_Ignored (N)
3005 or else Chars (Last_Var) = Name_Structural
3006 or else CodePeer_Mode
3007 then
3008 Rewrite (N, Make_Null_Statement (Loc));
3009 Analyze (N);
3010 return;
3011 end if;
3012
3013 -- The expansion of Loop_Variant is quite distributed as it produces
3014 -- various statements to capture and compare the arguments. To preserve
3015 -- the original context, set the Is_Assertion_Expr flag. This aids the
3016 -- Ghost legality checks when verifying the placement of a reference to
3017 -- a Ghost entity.
3018
3019 In_Assertion_Expr := In_Assertion_Expr + 1;
3020
3021 -- Locate the enclosing loop for which this assertion applies. In the
3022 -- case of Ada 2012 array iteration, we might be dealing with nested
3023 -- loops. Only the outermost loop has an identifier.
3024
3025 Loop_Stmt := N;
3026 while Present (Loop_Stmt) loop
3027 if Nkind (Loop_Stmt) = N_Loop_Statement
3028 and then Present (Identifier (Loop_Stmt))
3029 then
3030 exit;
3031 end if;
3032
3033 Loop_Stmt := Parent (Loop_Stmt);
3034 end loop;
3035
3036 Loop_Scop := Entity (Identifier (Loop_Stmt));
3037
3038 -- Create the circuitry which verifies individual variants
3039
3040 Variant := First (Pragma_Argument_Associations (N));
3041 while Present (Variant) loop
3042 Process_Variant (Variant, Is_Last => Variant = Last_Var);
3043 Next (Variant);
3044 end loop;
3045
3046 -- Construct the segment which stores the old values of all expressions.
3047 -- Generate:
3048 -- if Flag then
3049 -- <Old_Assign>
3050 -- end if;
3051
3052 Insert_Action (N,
3053 Make_If_Statement (Loc,
3054 Condition => New_Occurrence_Of (Flag_Id, Loc),
3055 Then_Statements => Old_Assign));
3056
3057 -- Update the values of all expressions
3058
3059 Insert_Actions (N, Curr_Assign);
3060
3061 -- Add the assertion circuitry to test all changes in expressions.
3062 -- Generate:
3063 -- if Flag then
3064 -- <If_Stmt>
3065 -- else
3066 -- Flag := True;
3067 -- end if;
3068
3069 Insert_Action (N,
3070 Make_If_Statement (Loc,
3071 Condition => New_Occurrence_Of (Flag_Id, Loc),
3072 Then_Statements => New_List (If_Stmt),
3073 Else_Statements => New_List (
3074 Make_Assignment_Statement (Loc,
3075 Name => New_Occurrence_Of (Flag_Id, Loc),
3076 Expression => New_Occurrence_Of (Standard_True, Loc)))));
3077
3078 -- Note: the pragma has been completely transformed into a sequence of
3079 -- corresponding declarations and statements. We leave it in the tree
3080 -- for documentation purposes. It will be ignored by the backend.
3081
3082 In_Assertion_Expr := In_Assertion_Expr - 1;
3083 end Expand_Pragma_Loop_Variant;
3084
3085 --------------------------------
3086 -- Expand_Pragma_Psect_Object --
3087 --------------------------------
3088
3089 -- Convert to Common_Object, and expand the resulting pragma
3090
3091 procedure Expand_Pragma_Psect_Object (N : Node_Id)
3092 renames Expand_Pragma_Common_Object;
3093
3094 -------------------------------------
3095 -- Expand_Pragma_Relative_Deadline --
3096 -------------------------------------
3097
3098 procedure Expand_Pragma_Relative_Deadline (N : Node_Id) is
3099 P : constant Node_Id := Parent (N);
3100 Loc : constant Source_Ptr := Sloc (N);
3101
3102 begin
3103 -- Expand the pragma only in the case of the main subprogram. For tasks
3104 -- the expansion is done in exp_ch9. Generate a call to Set_Deadline
3105 -- at Clock plus the relative deadline specified in the pragma. Time
3106 -- values are translated into Duration to allow for non-private
3107 -- addition operation.
3108
3109 if Nkind (P) = N_Subprogram_Body then
3110 Rewrite
3111 (N,
3112 Make_Procedure_Call_Statement (Loc,
3113 Name => New_Occurrence_Of (RTE (RE_Set_Deadline), Loc),
3114 Parameter_Associations => New_List (
3115 Unchecked_Convert_To (RTE (RO_RT_Time),
3116 Make_Op_Add (Loc,
3117 Left_Opnd =>
3118 Make_Function_Call (Loc,
3119 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
3120 New_List
3121 (Make_Function_Call
3122 (Loc, New_Occurrence_Of (RTE (RE_Clock), Loc)))),
3123 Right_Opnd =>
3124 Unchecked_Convert_To (
3125 Standard_Duration,
3126 Arg_N (N, 1)))))));
3127
3128 Analyze (N);
3129 end if;
3130 end Expand_Pragma_Relative_Deadline;
3131
3132 --------------------------------------
3133 -- Expand_Pragma_Subprogram_Variant --
3134 --------------------------------------
3135
3136 -- Aspect Subprogram_Variant is expanded in the following manner:
3137
3138 -- Original code
3139
3140 -- procedure Proc (Param : T) with
3141 -- with Variant (Increases => Incr_Expr,
3142 -- Decreases => Decr_Expr)
3143 -- <declarations>
3144 -- is
3145 -- <source statements>
3146 -- Proc (New_Param_Value);
3147 -- end Proc;
3148
3149 -- Expanded code
3150
3151 -- procedure Proc (Param : T) is
3152 -- Old_Incr : constant <type of Incr_Expr> := <Incr_Expr>;
3153 -- Old_Decr : constant <type of Decr_Expr> := <Decr_Expr> ;
3154 --
3155 -- procedure Variants (Param : T);
3156 --
3157 -- procedure Variants (Param : T) is
3158 -- Curr_Incr : constant <type of Incr_Expr> := <Incr_Expr>;
3159 -- Curr_Decr : constant <type of Decr_Expr> := <Decr_Expr>;
3160 -- begin
3161 -- if Curr_Incr /= Old_Incr then
3162 -- pragma Check (Variant, Curr_Incr > Old_Incr);
3163 -- else
3164 -- pragma Check (Variant, Curr_Decr < Old_Decr);
3165 -- end if;
3166 -- end Variants;
3167 --
3168 -- <declarations>
3169 -- begin
3170 -- <source statements>
3171 -- Variants (New_Param_Value);
3172 -- Proc (New_Param_Value);
3173 -- end Proc;
3174
3175 procedure Expand_Pragma_Subprogram_Variant
3176 (Prag : Node_Id;
3177 Subp_Id : Entity_Id;
3178 Body_Decls : List_Id)
3179 is
3180 Curr_Decls : List_Id;
3181 If_Stmt : Node_Id := Empty;
3182
3183 function Formal_Param_Map
3184 (Old_Subp : Entity_Id;
3185 New_Subp : Entity_Id) return Elist_Id;
3186 -- Given two subprogram entities Old_Subp and New_Subp with the same
3187 -- number of formal parameters return a list of the form:
3188 --
3189 -- old formal 1
3190 -- new formal 1
3191 -- old formal 2
3192 -- new formal 2
3193 -- ...
3194 --
3195 -- as required by New_Copy_Tree to replace references to formal
3196 -- parameters of Old_Subp with references to formal parameters of
3197 -- New_Subp.
3198
3199 procedure Process_Variant
3200 (Variant : Node_Id;
3201 Formal_Map : Elist_Id;
3202 Prev_Decl : in out Node_Id;
3203 Is_Last : Boolean);
3204 -- Process a single increasing / decreasing termination variant given by
3205 -- a component association Variant. Formal_Map is a list of formal
3206 -- parameters of the annotated subprogram and of the internal procedure
3207 -- that verifies the variant in the format required by New_Copy_Tree.
3208 -- The Old_... object created by this routine will be appended after
3209 -- Prev_Decl and is stored in this parameter for a next call to this
3210 -- routine. Is_Last is True when there are no more variants to process.
3211
3212 ----------------------
3213 -- Formal_Param_Map --
3214 ----------------------
3215
3216 function Formal_Param_Map
3217 (Old_Subp : Entity_Id;
3218 New_Subp : Entity_Id) return Elist_Id
3219 is
3220 Old_Formal : Entity_Id := First_Formal (Old_Subp);
3221 New_Formal : Entity_Id := First_Formal (New_Subp);
3222
3223 Param_Map : Elist_Id;
3224 begin
3225 if Present (Old_Formal) then
3226 Param_Map := New_Elmt_List;
3227 while Present (Old_Formal) and then Present (New_Formal) loop
3228 Append_Elmt (Old_Formal, Param_Map);
3229 Append_Elmt (New_Formal, Param_Map);
3230
3231 Next_Formal (Old_Formal);
3232 Next_Formal (New_Formal);
3233 end loop;
3234
3235 return Param_Map;
3236 else
3237 return No_Elist;
3238 end if;
3239 end Formal_Param_Map;
3240
3241 ---------------------
3242 -- Process_Variant --
3243 ---------------------
3244
3245 procedure Process_Variant
3246 (Variant : Node_Id;
3247 Formal_Map : Elist_Id;
3248 Prev_Decl : in out Node_Id;
3249 Is_Last : Boolean)
3250 is
3251 Expr : constant Node_Id := Expression (Variant);
3252 Expr_Typ : constant Entity_Id := Etype (Expr);
3253 Loc : constant Source_Ptr := Sloc (Expr);
3254
3255 Old_Id : Entity_Id;
3256 Old_Decl : Node_Id;
3257 Curr_Id : Entity_Id;
3258 Curr_Decl : Node_Id;
3259 Prag : Node_Id;
3260
3261 begin
3262 -- Create temporaries that store the old values of the associated
3263 -- expression.
3264
3265 -- Generate:
3266 -- Old : constant <type of Expr> := <Expr>;
3267
3268 Old_Id := Make_Temporary (Loc, 'P');
3269
3270 Old_Decl :=
3271 Make_Object_Declaration (Loc,
3272 Defining_Identifier => Old_Id,
3273 Constant_Present => True,
3274 Object_Definition => New_Occurrence_Of (Expr_Typ, Loc),
3275 Expression => New_Copy_Tree (Expr));
3276
3277 Insert_After_And_Analyze (Prev_Decl, Old_Decl);
3278
3279 Prev_Decl := Old_Decl;
3280
3281 -- Generate:
3282 -- Curr : constant <type of Expr> := <Expr>;
3283
3284 Curr_Id := Make_Temporary (Loc, 'C');
3285
3286 Curr_Decl :=
3287 Make_Object_Declaration (Loc,
3288 Defining_Identifier => Curr_Id,
3289 Constant_Present => True,
3290 Object_Definition => New_Occurrence_Of (Expr_Typ, Loc),
3291 Expression =>
3292 New_Copy_Tree (Expr, Map => Formal_Map));
3293
3294 Append (Curr_Decl, Curr_Decls);
3295
3296 -- Generate:
3297 -- pragma Check (Variant, Curr <|> Old);
3298
3299 Prag :=
3300 Make_Pragma (Loc,
3301 Chars => Name_Check,
3302 Pragma_Argument_Associations => New_List (
3303 Make_Pragma_Argument_Association (Loc,
3304 Expression =>
3305 Make_Identifier (Loc,
3306 Name_Subprogram_Variant)),
3307 Make_Pragma_Argument_Association (Loc,
3308 Expression =>
3309 Make_Variant_Comparison (Loc,
3310 Mode => Chars (First (Choices (Variant))),
3311 Typ => Expr_Typ,
3312 Curr_Val => New_Occurrence_Of (Curr_Id, Loc),
3313 Old_Val => New_Occurrence_Of (Old_Id, Loc)))));
3314
3315 -- Generate:
3316 -- if Curr /= Old then
3317 -- <Prag>;
3318
3319 if No (If_Stmt) then
3320
3321 -- When there is just one termination variant, do not compare
3322 -- the old and current value for equality, just check the
3323 -- pragma.
3324
3325 if Is_Last then
3326 If_Stmt := Prag;
3327 else
3328 If_Stmt :=
3329 Make_If_Statement (Loc,
3330 Condition =>
3331 Make_Op_Ne (Loc,
3332 Left_Opnd => New_Occurrence_Of (Curr_Id, Loc),
3333 Right_Opnd => New_Occurrence_Of (Old_Id, Loc)),
3334 Then_Statements => New_List (Prag));
3335 end if;
3336
3337 -- Generate:
3338 -- else
3339 -- <Prag>;
3340 -- end if;
3341
3342 elsif Is_Last then
3343 Set_Else_Statements (If_Stmt, New_List (Prag));
3344
3345 -- Generate:
3346 -- elsif Curr /= Old then
3347 -- <Prag>;
3348
3349 else
3350 if Elsif_Parts (If_Stmt) = No_List then
3351 Set_Elsif_Parts (If_Stmt, New_List);
3352 end if;
3353
3354 Append_To (Elsif_Parts (If_Stmt),
3355 Make_Elsif_Part (Loc,
3356 Condition =>
3357 Make_Op_Ne (Loc,
3358 Left_Opnd => New_Occurrence_Of (Curr_Id, Loc),
3359 Right_Opnd => New_Occurrence_Of (Old_Id, Loc)),
3360 Then_Statements => New_List (Prag)));
3361 end if;
3362 end Process_Variant;
3363
3364 -- Local variables
3365
3366 Loc : constant Source_Ptr := Sloc (Prag);
3367
3368 Aggr : constant Node_Id :=
3369 Expression (First (Pragma_Argument_Associations (Prag)));
3370 Formal_Map : Elist_Id;
3371 Last : Node_Id;
3372 Last_Variant : constant Node_Id :=
3373 Nlists.Last (Component_Associations (Aggr));
3374 Proc_Bod : Node_Id;
3375 Proc_Decl : Node_Id;
3376 Proc_Id : Entity_Id;
3377 Proc_Spec : Node_Id;
3378 Variant : Node_Id;
3379
3380 begin
3381 -- Do nothing if pragma is not present or is disabled.
3382 -- Also ignore structural variants for execution.
3383
3384 if Is_Ignored (Prag)
3385 or else Chars (Nlists.Last (Choices (Last_Variant))) = Name_Structural
3386 then
3387 return;
3388 end if;
3389
3390 -- The expansion of Subprogram Variant is quite distributed as it
3391 -- produces various statements to capture and compare the arguments.
3392 -- To preserve the original context, set the Is_Assertion_Expr flag.
3393 -- This aids the Ghost legality checks when verifying the placement
3394 -- of a reference to a Ghost entity.
3395
3396 In_Assertion_Expr := In_Assertion_Expr + 1;
3397
3398 -- Create declaration of the procedure that compares values of the
3399 -- variant expressions captured at the start of subprogram with their
3400 -- values at the recursive call of the subprogram.
3401
3402 Proc_Id := Make_Defining_Identifier (Loc, Name_uVariants);
3403
3404 Proc_Spec :=
3405 Make_Procedure_Specification
3406 (Loc,
3407 Defining_Unit_Name => Proc_Id,
3408 Parameter_Specifications => Copy_Parameter_List (Subp_Id));
3409
3410 Proc_Decl :=
3411 Make_Subprogram_Declaration (Loc, Proc_Spec);
3412
3413 Insert_Before_First_Source_Declaration (Proc_Decl, Body_Decls);
3414 Analyze (Proc_Decl);
3415
3416 -- Create a mapping between formals of the annotated subprogram (which
3417 -- are used to compute values of the variant expression at the start of
3418 -- subprogram) and formals of the internal procedure (which are used to
3419 -- compute values of of the variant expression at the recursive call).
3420
3421 Formal_Map :=
3422 Formal_Param_Map (Old_Subp => Subp_Id, New_Subp => Proc_Id);
3423
3424 -- Process invidual increasing / decreasing variants
3425
3426 Last := Proc_Decl;
3427 Curr_Decls := New_List;
3428
3429 Variant := First (Component_Associations (Aggr));
3430 while Present (Variant) loop
3431 Process_Variant
3432 (Variant => Variant,
3433 Formal_Map => Formal_Map,
3434 Prev_Decl => Last,
3435 Is_Last => Variant = Last_Variant);
3436 Next (Variant);
3437 end loop;
3438
3439 -- Create a subprogram body with declarations of objects that capture
3440 -- the current values of variant expressions at a recursive call and an
3441 -- if-then-else statement that compares current with old values.
3442
3443 Proc_Bod :=
3444 Make_Subprogram_Body (Loc,
3445 Specification =>
3446 Copy_Subprogram_Spec (Proc_Spec),
3447 Declarations => Curr_Decls,
3448 Handled_Statement_Sequence =>
3449 Make_Handled_Sequence_Of_Statements (Loc,
3450 Statements => New_List (If_Stmt),
3451 End_Label => Make_Identifier (Loc, Chars (Proc_Id))));
3452
3453 Insert_After_And_Analyze (Last, Proc_Bod);
3454
3455 -- Restore assertion context
3456
3457 In_Assertion_Expr := In_Assertion_Expr - 1;
3458
3459 -- Rewrite the aspect expression, which is no longer needed, with
3460 -- a reference to the procedure that has just been created. We will
3461 -- generate a call to this procedure at each recursive call of the
3462 -- subprogram that has been annotated with Subprogram_Variant.
3463
3464 Rewrite (Aggr, New_Occurrence_Of (Proc_Id, Loc));
3465 end Expand_Pragma_Subprogram_Variant;
3466
3467 -------------------------------------------
3468 -- Expand_Pragma_Suppress_Initialization --
3469 -------------------------------------------
3470
3471 procedure Expand_Pragma_Suppress_Initialization (N : Node_Id) is
3472 Def_Id : constant Entity_Id := Entity (Arg_N (N, 1));
3473
3474 begin
3475 -- Variable case (we have to undo any initialization already done)
3476
3477 if Ekind (Def_Id) = E_Variable then
3478 Undo_Initialization (Def_Id, N);
3479 end if;
3480 end Expand_Pragma_Suppress_Initialization;
3481
3482 -------------------------
3483 -- Undo_Initialization --
3484 -------------------------
3485
3486 procedure Undo_Initialization (Def_Id : Entity_Id; N : Node_Id) is
3487 Init_Call : Node_Id;
3488
3489 begin
3490 -- When applied to a variable, the default initialization must not be
3491 -- done. As it is already done when the pragma is found, we just get rid
3492 -- of the call to the initialization procedure which followed the object
3493 -- declaration. The call is inserted after the declaration, but validity
3494 -- checks may also have been inserted and thus the initialization call
3495 -- does not necessarily appear immediately after the object declaration.
3496
3497 -- We can't use the freezing mechanism for this purpose, since we have
3498 -- to elaborate the initialization expression when it is first seen (so
3499 -- this elaboration cannot be deferred to the freeze point).
3500
3501 -- Find and remove generated initialization call for object, if any
3502
3503 Init_Call := Remove_Init_Call (Def_Id, Rep_Clause => N);
3504
3505 -- Any default initialization expression should be removed (e.g.
3506 -- null defaults for access objects, zero initialization of packed
3507 -- bit arrays). Imported objects aren't allowed to have explicit
3508 -- initialization, so the expression must have been generated by
3509 -- the compiler.
3510
3511 if No (Init_Call) and then Present (Expression (Parent (Def_Id))) then
3512 Set_Expression (Parent (Def_Id), Empty);
3513 end if;
3514 end Undo_Initialization;
3515
3516 end Exp_Prag;