Debian Codegraph @

gcc/ada/contracts.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- C O N T R A C T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2015-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Prag; use Exp_Prag;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Lib; use Lib;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Sem; use Sem;
43 with Sem_Aux; use Sem_Aux;
44 with Sem_Ch3; use Sem_Ch3;
45 with Sem_Ch6; use Sem_Ch6;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Ch12; use Sem_Ch12;
48 with Sem_Ch13; use Sem_Ch13;
49 with Sem_Disp; use Sem_Disp;
50 with Sem_Prag; use Sem_Prag;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Sinfo.Nodes; use Sinfo.Nodes;
55 with Sinfo.Utils; use Sinfo.Utils;
56 with Sinput; use Sinput;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Stringt; use Stringt;
60 with Tbuild; use Tbuild;
61 with Warnsw; use Warnsw;
62
63 package body Contracts is
64
65 Contract_Error : exception;
66 -- This exception is raised by Add_Contract_Item when it is invoked on an
67 -- invalid pragma. Note that clients of the package must filter them out
68 -- before invoking Add_Contract_Item, so it should not escape the package.
69
70 procedure Analyze_Package_Instantiation_Contract (Inst_Id : Entity_Id);
71 -- Analyze all delayed pragmas chained on the contract of package
72 -- instantiation Inst_Id as if they appear at the end of a declarative
73 -- region. The pragmas in question are:
74 --
75 -- Part_Of
76
77 procedure Build_Subprogram_Contract_Wrapper
78 (Body_Id : Entity_Id;
79 Stmts : List_Id;
80 Decls : List_Id;
81 Result : Entity_Id);
82 -- Generate a wrapper for a given subprogram body when the expansion of
83 -- postconditions require it by moving its declarations and statements
84 -- into a locally declared subprogram _Wrapped_Statements.
85
86 -- Postcondition and precondition checks then get inserted in place of
87 -- the original statements and declarations along with a call to
88 -- _Wrapped_Statements.
89
90 procedure Check_Class_Condition
91 (Cond : Node_Id;
92 Subp : Entity_Id;
93 Par_Subp : Entity_Id;
94 Is_Precondition : Boolean);
95 -- Perform checking of class-wide pre/postcondition Cond inherited by Subp
96 -- from Par_Subp. Is_Precondition enables check specific for preconditions.
97 -- In SPARK_Mode, an inherited operation that is not overridden but has
98 -- inherited modified conditions pre/postconditions is illegal.
99
100 function Is_Prologue_Renaming (Decl : Node_Id) return Boolean;
101 -- Determine whether arbitrary declaration Decl denotes a renaming of
102 -- a discriminant or protection field _object.
103
104 procedure Check_Type_Or_Object_External_Properties
105 (Type_Or_Obj_Id : Entity_Id);
106 -- Perform checking of external properties pragmas that is common to both
107 -- type declarations and object declarations.
108
109 procedure Expand_Subprogram_Contract (Body_Id : Entity_Id);
110 -- Expand the contracts of a subprogram body and its correspoding spec (if
111 -- any). This routine processes all [refined] pre- and postconditions as
112 -- well as Always_Terminates, Contract_Cases, Exceptional_Cases,
113 -- Subprogram_Variant, invariants and predicates. Body_Id denotes the
114 -- entity of the subprogram body.
115
116 procedure Preanalyze_Condition
117 (Subp : Entity_Id;
118 Expr : Node_Id);
119 -- Preanalyze the class-wide condition Expr of Subp
120
121 procedure Set_Class_Condition
122 (Kind : Condition_Kind;
123 Subp : Entity_Id;
124 Cond : Node_Id);
125 -- Set the class-wide Kind condition of Subp
126
127 -----------------------
128 -- Add_Contract_Item --
129 -----------------------
130
131 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
132 Items : Node_Id := Contract (Id);
133
134 procedure Add_Classification;
135 -- Prepend Prag to the list of classifications
136
137 procedure Add_Contract_Test_Case;
138 -- Prepend Prag to the list of contract and test cases
139
140 procedure Add_Pre_Post_Condition;
141 -- Prepend Prag to the list of pre- and postconditions
142
143 ------------------------
144 -- Add_Classification --
145 ------------------------
146
147 procedure Add_Classification is
148 begin
149 Set_Next_Pragma (Prag, Classifications (Items));
150 Set_Classifications (Items, Prag);
151 end Add_Classification;
152
153 ----------------------------
154 -- Add_Contract_Test_Case --
155 ----------------------------
156
157 procedure Add_Contract_Test_Case is
158 begin
159 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
160 Set_Contract_Test_Cases (Items, Prag);
161 end Add_Contract_Test_Case;
162
163 ----------------------------
164 -- Add_Pre_Post_Condition --
165 ----------------------------
166
167 procedure Add_Pre_Post_Condition is
168 begin
169 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
170 Set_Pre_Post_Conditions (Items, Prag);
171 end Add_Pre_Post_Condition;
172
173 -- Local variables
174
175 -- A contract must contain only pragmas
176
177 pragma Assert (Nkind (Prag) = N_Pragma);
178 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
179
180 -- Start of processing for Add_Contract_Item
181
182 begin
183 -- Create a new contract when adding the first item
184
185 if No (Items) then
186 Items := Make_Contract (Sloc (Id));
187 Set_Contract (Id, Items);
188 end if;
189
190 -- Constants, the applicable pragmas are:
191 -- Part_Of
192
193 if Ekind (Id) = E_Constant then
194 if Prag_Nam in Name_Async_Readers
195 | Name_Async_Writers
196 | Name_Effective_Reads
197 | Name_Effective_Writes
198 | Name_No_Caching
199 | Name_Part_Of
200 then
201 Add_Classification;
202
203 -- The pragma is not a proper contract item
204
205 else
206 raise Contract_Error;
207 end if;
208
209 -- Entry bodies, the applicable pragmas are:
210 -- Refined_Depends
211 -- Refined_Global
212 -- Refined_Post
213
214 elsif Is_Entry_Body (Id) then
215 if Prag_Nam in Name_Refined_Depends | Name_Refined_Global then
216 Add_Classification;
217
218 elsif Prag_Nam = Name_Refined_Post then
219 Add_Pre_Post_Condition;
220
221 -- The pragma is not a proper contract item
222
223 else
224 raise Contract_Error;
225 end if;
226
227 -- Entry or subprogram declarations, the applicable pragmas are:
228 -- Always_Terminates
229 -- Attach_Handler
230 -- Contract_Cases
231 -- Depends
232 -- Exceptional_Cases
233 -- Extensions_Visible
234 -- Global
235 -- Interrupt_Handler
236 -- Postcondition
237 -- Precondition
238 -- Side_Effects
239 -- Subprogram_Variant
240 -- Test_Case
241 -- Volatile_Function
242
243 elsif Is_Entry_Declaration (Id)
244 or else Ekind (Id) in E_Function
245 | E_Generic_Function
246 | E_Generic_Procedure
247 | E_Procedure
248 then
249 if Prag_Nam in Name_Attach_Handler | Name_Interrupt_Handler
250 and then Ekind (Id) in E_Generic_Procedure | E_Procedure
251 then
252 Add_Classification;
253
254 elsif Prag_Nam in Name_Depends
255 | Name_Extensions_Visible
256 | Name_Global
257 | Name_Side_Effects
258 then
259 Add_Classification;
260
261 elsif Prag_Nam = Name_Volatile_Function
262 and then Ekind (Id) in E_Function | E_Generic_Function
263 then
264 Add_Classification;
265
266 elsif Prag_Nam in Name_Always_Terminates
267 | Name_Contract_Cases
268 | Name_Exceptional_Cases
269 | Name_Exit_Cases
270 | Name_Subprogram_Variant
271 | Name_Test_Case
272 then
273 Add_Contract_Test_Case;
274
275 elsif Prag_Nam in Name_Postcondition | Name_Precondition then
276 Add_Pre_Post_Condition;
277
278 -- The pragma is not a proper contract item
279
280 else
281 raise Contract_Error;
282 end if;
283
284 -- Packages or instantiations, the applicable pragmas are:
285 -- Abstract_States
286 -- Initial_Condition
287 -- Initializes
288 -- Part_Of (instantiation only)
289
290 elsif Is_Package_Or_Generic_Package (Id) then
291 if Prag_Nam in Name_Abstract_State
292 | Name_Initial_Condition
293 | Name_Initializes
294 then
295 Add_Classification;
296
297 -- Indicator Part_Of must be associated with a package instantiation
298
299 elsif Prag_Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
300 Add_Classification;
301
302 elsif Prag_Nam = Name_Always_Terminates then
303 Add_Contract_Test_Case;
304
305 -- The pragma is not a proper contract item
306
307 else
308 raise Contract_Error;
309 end if;
310
311 -- Package bodies, the applicable pragmas are:
312 -- Refined_States
313
314 elsif Ekind (Id) = E_Package_Body then
315 if Prag_Nam = Name_Refined_State then
316 Add_Classification;
317
318 -- The pragma is not a proper contract item
319
320 else
321 raise Contract_Error;
322 end if;
323
324 -- The four volatility refinement pragmas are ok for all types.
325 -- Part_Of is ok for task types and protected types.
326 -- Depends and Global are ok for task types.
327 --
328 -- Precondition and Postcondition are added separately; they are allowed
329 -- for access-to-subprogram types.
330
331 elsif Is_Type (Id) then
332 declare
333 Is_OK_Classification : constant Boolean :=
334 Prag_Nam in Name_Async_Readers
335 | Name_Async_Writers
336 | Name_Effective_Reads
337 | Name_Effective_Writes
338 | Name_No_Caching
339 or else (Ekind (Id) = E_Task_Type
340 and Prag_Nam in Name_Part_Of
341 | Name_Depends
342 | Name_Global)
343 or else (Ekind (Id) = E_Protected_Type
344 and Prag_Nam = Name_Part_Of);
345
346 begin
347 if Is_OK_Classification then
348 Add_Classification;
349
350 elsif Ekind (Id) = E_Subprogram_Type
351 and then Prag_Nam in Name_Precondition
352 | Name_Postcondition
353 then
354 Add_Pre_Post_Condition;
355 else
356
357 -- The pragma is not a proper contract item
358
359 raise Contract_Error;
360 end if;
361 end;
362
363 -- Subprogram bodies, the applicable pragmas are:
364 -- Postcondition
365 -- Precondition
366 -- Refined_Depends
367 -- Refined_Global
368 -- Refined_Post
369
370 elsif Ekind (Id) = E_Subprogram_Body then
371 if Prag_Nam in Name_Refined_Depends | Name_Refined_Global then
372 Add_Classification;
373
374 elsif Prag_Nam in Name_Postcondition
375 | Name_Precondition
376 | Name_Refined_Post
377 then
378 Add_Pre_Post_Condition;
379
380 -- The pragma is not a proper contract item
381
382 else
383 raise Contract_Error;
384 end if;
385
386 -- Task bodies, the applicable pragmas are:
387 -- Refined_Depends
388 -- Refined_Global
389
390 elsif Ekind (Id) = E_Task_Body then
391 if Prag_Nam in Name_Refined_Depends | Name_Refined_Global then
392 Add_Classification;
393
394 -- The pragma is not a proper contract item
395
396 else
397 raise Contract_Error;
398 end if;
399
400 -- Task units, the applicable pragmas are:
401 -- Depends
402 -- Global
403 -- Part_Of
404
405 -- Variables, the applicable pragmas are:
406 -- Async_Readers
407 -- Async_Writers
408 -- Constant_After_Elaboration
409 -- Depends
410 -- Effective_Reads
411 -- Effective_Writes
412 -- Global
413 -- No_Caching
414 -- Part_Of
415
416 elsif Ekind (Id) = E_Variable then
417 if Prag_Nam in Name_Async_Readers
418 | Name_Async_Writers
419 | Name_Constant_After_Elaboration
420 | Name_Depends
421 | Name_Effective_Reads
422 | Name_Effective_Writes
423 | Name_Global
424 | Name_No_Caching
425 | Name_Part_Of
426 then
427 Add_Classification;
428
429 -- The pragma is not a proper contract item
430
431 else
432 raise Contract_Error;
433 end if;
434
435 else
436 raise Contract_Error;
437 end if;
438 end Add_Contract_Item;
439
440 -----------------------
441 -- Analyze_Contracts --
442 -----------------------
443
444 procedure Analyze_Contracts (L : List_Id) is
445 Decl : Node_Id;
446
447 begin
448 Decl := First (L);
449 while Present (Decl) loop
450
451 -- Entry or subprogram declarations
452
453 if Nkind (Decl) in N_Abstract_Subprogram_Declaration
454 | N_Entry_Declaration
455 | N_Generic_Subprogram_Declaration
456 | N_Subprogram_Declaration
457 then
458 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Decl));
459
460 -- Entry or subprogram bodies
461
462 elsif Nkind (Decl) in N_Entry_Body | N_Subprogram_Body then
463 Analyze_Entry_Or_Subprogram_Body_Contract (Defining_Entity (Decl));
464
465 -- Objects
466
467 elsif Nkind (Decl) = N_Object_Declaration then
468 Analyze_Object_Contract (Defining_Entity (Decl));
469
470 -- Package instantiation
471
472 elsif Nkind (Decl) = N_Package_Instantiation then
473 Analyze_Package_Instantiation_Contract (Defining_Entity (Decl));
474
475 -- Protected units
476
477 elsif Nkind (Decl) in N_Protected_Type_Declaration
478 | N_Single_Protected_Declaration
479 then
480 Analyze_Protected_Contract (Defining_Entity (Decl));
481
482 -- Subprogram body stubs
483
484 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
485 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
486
487 -- Task units
488
489 elsif Nkind (Decl) in N_Single_Task_Declaration
490 | N_Task_Type_Declaration
491 then
492 Analyze_Task_Contract (Defining_Entity (Decl));
493
494 -- For type declarations, we need to do the preanalysis of Iterable
495 -- and the 3 Xxx_Literal aspect specifications.
496
497 -- Other type aspects need to be resolved here???
498
499 elsif Nkind (Decl) = N_Private_Type_Declaration
500 and then Present (Aspect_Specifications (Decl))
501 then
502 declare
503 E : constant Entity_Id := Defining_Identifier (Decl);
504 It : constant Node_Id := Find_Aspect (E, Aspect_Iterable);
505 I_Lit : constant Node_Id :=
506 Find_Aspect (E, Aspect_Integer_Literal);
507 R_Lit : constant Node_Id :=
508 Find_Aspect (E, Aspect_Real_Literal);
509 S_Lit : constant Node_Id :=
510 Find_Aspect (E, Aspect_String_Literal);
511
512 begin
513 if Present (It) then
514 Validate_Iterable_Aspect (E, It);
515 end if;
516 if Present (I_Lit) then
517 Validate_Literal_Aspect (E, I_Lit);
518 end if;
519 if Present (R_Lit) then
520 Validate_Literal_Aspect (E, R_Lit);
521 end if;
522 if Present (S_Lit) then
523 Validate_Literal_Aspect (E, S_Lit);
524 end if;
525 end;
526 end if;
527
528 if Nkind (Decl) in N_Full_Type_Declaration
529 | N_Private_Type_Declaration
530 | N_Task_Type_Declaration
531 | N_Protected_Type_Declaration
532 | N_Formal_Type_Declaration
533 then
534 Analyze_Type_Contract (Defining_Identifier (Decl));
535 end if;
536
537 Next (Decl);
538 end loop;
539 end Analyze_Contracts;
540
541 -------------------------------------
542 -- Analyze_Pragmas_In_Declarations --
543 -------------------------------------
544
545 procedure Analyze_Pragmas_In_Declarations (Body_Id : Entity_Id) is
546 Curr_Decl : Node_Id;
547
548 begin
549 -- Move through the body's declarations analyzing all pragmas which
550 -- appear at the top of the declarations. Go over the list twice, so
551 -- that pragmas which should be analyzed first are analyzed in the
552 -- first pass.
553
554 for Pragmas_Analyzed_First in reverse False .. True loop
555
556 Curr_Decl := First (Declarations (Unit_Declaration_Node (Body_Id)));
557 while Present (Curr_Decl) loop
558
559 if Nkind (Curr_Decl) = N_Pragma then
560
561 if Pragma_Significant_To_Subprograms
562 (Get_Pragma_Id (Curr_Decl))
563 and then Pragmas_Analyzed_First =
564 Pragma_Significant_To_Subprograms_Analyzed_First
565 (Get_Pragma_Id (Curr_Decl))
566 then
567 Analyze (Curr_Decl);
568 end if;
569
570 -- Skip the renamings of discriminants and protection fields
571
572 elsif Is_Prologue_Renaming (Curr_Decl) then
573 null;
574
575 -- We have reached something which is not a pragma so we can be
576 -- sure there are no more contracts or pragmas which need to be
577 -- taken into account.
578
579 else
580 exit;
581 end if;
582
583 Next (Curr_Decl);
584 end loop;
585 end loop;
586 end Analyze_Pragmas_In_Declarations;
587
588 -----------------------------------------------
589 -- Analyze_Entry_Or_Subprogram_Body_Contract --
590 -----------------------------------------------
591
592 -- WARNING: This routine manages SPARK regions. Return statements must be
593 -- replaced by gotos which jump to the end of the routine and restore the
594 -- SPARK mode.
595
596 procedure Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id : Entity_Id) is
597 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
598 Items : constant Node_Id := Contract (Body_Id);
599 Spec_Id : constant Entity_Id := Unique_Defining_Entity (Body_Decl);
600
601 begin
602 -- When a subprogram body declaration is illegal, its defining entity is
603 -- left unanalyzed. There is nothing left to do in this case because the
604 -- body lacks a contract, or even a proper Ekind.
605
606 if Ekind (Body_Id) = E_Void then
607 return;
608
609 -- Do not analyze a contract multiple times
610
611 elsif Present (Items) then
612 if Analyzed (Items) then
613 return;
614 else
615 Set_Analyzed (Items);
616 end if;
617
618 -- When this is a subprogram body not coming from source, for example an
619 -- expression function, it does not cause freezing of previous contracts
620 -- (see Analyze_Subprogram_Body_Helper), in particular not of those on
621 -- its spec if it exists. In this case make sure they have been properly
622 -- analyzed before being expanded below, as we may be invoked during the
623 -- freezing of the subprogram in the middle of its enclosing declarative
624 -- part because the declarative part contains e.g. the declaration of a
625 -- variable initialized by means of a call to the subprogram.
626
627 elsif Nkind (Body_Decl) = N_Subprogram_Body
628 and then not Comes_From_Source (Original_Node (Body_Decl))
629 and then Present (Corresponding_Spec (Body_Decl))
630 and then Present (Contract (Corresponding_Spec (Body_Decl)))
631 then
632 Analyze_Entry_Or_Subprogram_Contract (Corresponding_Spec (Body_Decl));
633 end if;
634
635 -- Ensure that the contract cases or postconditions mention 'Result or
636 -- define a post-state.
637
638 Check_Result_And_Post_State (Body_Id);
639
640 -- Capture all global references in a generic subprogram body now that
641 -- the contract has been analyzed.
642
643 if Is_Generic_Declaration_Or_Body (Body_Decl) then
644 Save_Global_References_In_Contract
645 (Templ => Original_Node (Body_Decl),
646 Gen_Id => Spec_Id);
647 end if;
648
649 -- Deal with preconditions, [refined] postconditions, Always_Terminates,
650 -- Contract_Cases, Exceptional_Cases, Subprogram_Variant, invariants and
651 -- predicates associated with body and its spec. Do not expand the
652 -- contract of subprogram body stubs.
653
654 if Nkind (Body_Decl) = N_Subprogram_Body then
655 Expand_Subprogram_Contract (Body_Id);
656 end if;
657 end Analyze_Entry_Or_Subprogram_Body_Contract;
658
659 ------------------------------------------
660 -- Analyze_Entry_Or_Subprogram_Contract --
661 ------------------------------------------
662
663 -- WARNING: This routine manages SPARK regions. Return statements must be
664 -- replaced by gotos which jump to the end of the routine and restore the
665 -- SPARK mode.
666
667 procedure Analyze_Entry_Or_Subprogram_Contract
668 (Subp_Id : Entity_Id;
669 Freeze_Id : Entity_Id := Empty)
670 is
671 Items : constant Node_Id := Contract (Subp_Id);
672 Subp_Decl : constant Node_Id :=
673 (if Ekind (Subp_Id) = E_Subprogram_Type
674 then Associated_Node_For_Itype (Subp_Id)
675 else Unit_Declaration_Node (Subp_Id));
676
677 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
678 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
679 -- Save the SPARK_Mode-related data to restore on exit
680
681 Skip_Assert_Exprs : constant Boolean :=
682 Is_Entry (Subp_Id) and then not GNATprove_Mode;
683
684 Depends : Node_Id := Empty;
685 Global : Node_Id := Empty;
686 Prag : Node_Id;
687 Prag_Nam : Name_Id;
688
689 begin
690 -- Do not analyze a contract multiple times
691
692 if Present (Items) then
693 if Analyzed (Items) then
694 return;
695 else
696 Set_Analyzed (Items);
697 end if;
698 end if;
699
700 -- Due to the timing of contract analysis, delayed pragmas may be
701 -- subject to the wrong SPARK_Mode, usually that of the enclosing
702 -- context. To remedy this, restore the original SPARK_Mode of the
703 -- related subprogram body.
704
705 Set_SPARK_Mode (Subp_Id);
706
707 -- All subprograms carry a contract, but for some it is not significant
708 -- and should not be processed.
709
710 if not Has_Significant_Contract (Subp_Id) then
711 null;
712
713 elsif Present (Items) then
714
715 -- Do not analyze the pre/postconditions of an entry declaration
716 -- unless annotating the original tree for GNATprove. The
717 -- real analysis occurs when the pre/postconditons are relocated to
718 -- the contract wrapper procedure (see Build_Contract_Wrapper).
719
720 if Skip_Assert_Exprs then
721 null;
722
723 -- Otherwise analyze the pre/postconditions.
724 -- If these come from an aspect specification, their expressions
725 -- might include references to types that are not frozen yet, in the
726 -- case where the body is a rewritten expression function that is a
727 -- completion, so freeze all types within before constructing the
728 -- contract code.
729
730 else
731 declare
732 Bod : Node_Id := Empty;
733 Freeze_Types : Boolean := False;
734
735 begin
736 if Present (Freeze_Id) then
737 Bod := Unit_Declaration_Node (Freeze_Id);
738
739 if Nkind (Bod) = N_Subprogram_Body
740 and then Was_Expression_Function (Bod)
741 and then Ekind (Subp_Id) = E_Function
742 and then Chars (Subp_Id) = Chars (Freeze_Id)
743 and then Subp_Id /= Freeze_Id
744 then
745 Freeze_Types := True;
746 end if;
747 end if;
748
749 Prag := Pre_Post_Conditions (Items);
750 while Present (Prag) loop
751 if Freeze_Types
752 and then Present (Corresponding_Aspect (Prag))
753 then
754 Freeze_Expr_Types
755 (Def_Id => Subp_Id,
756 Typ => Standard_Boolean,
757 Expr =>
758 Expression
759 (First (Pragma_Argument_Associations (Prag))),
760 N => Bod);
761 end if;
762
763 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Freeze_Id);
764 Prag := Next_Pragma (Prag);
765 end loop;
766 end;
767 end if;
768
769 -- Analyze contract-cases, subprogram-variant and test-cases
770
771 Prag := Contract_Test_Cases (Items);
772 while Present (Prag) loop
773 Prag_Nam := Pragma_Name (Prag);
774
775 if Prag_Nam = Name_Always_Terminates then
776 Analyze_Always_Terminates_In_Decl_Part (Prag);
777
778 elsif Prag_Nam = Name_Contract_Cases then
779
780 -- Do not analyze the contract cases of an entry declaration
781 -- unless annotating the original tree for GNATprove.
782 -- The real analysis occurs when the contract cases are moved
783 -- to the contract wrapper procedure (Build_Contract_Wrapper).
784
785 if Skip_Assert_Exprs then
786 null;
787
788 -- Otherwise analyze the contract cases
789
790 else
791 Analyze_Contract_Cases_In_Decl_Part (Prag, Freeze_Id);
792 end if;
793
794 elsif Prag_Nam = Name_Exit_Cases then
795 Analyze_Exit_Cases_In_Decl_Part (Prag);
796
797 elsif Prag_Nam = Name_Exceptional_Cases then
798 Analyze_Exceptional_Cases_In_Decl_Part (Prag);
799
800 elsif Prag_Nam = Name_Subprogram_Variant then
801 Analyze_Subprogram_Variant_In_Decl_Part (Prag);
802
803 else
804 pragma Assert (Prag_Nam = Name_Test_Case);
805 Analyze_Test_Case_In_Decl_Part (Prag);
806 end if;
807
808 Prag := Next_Pragma (Prag);
809 end loop;
810
811 -- Analyze classification pragmas
812
813 Prag := Classifications (Items);
814 while Present (Prag) loop
815 Prag_Nam := Pragma_Name (Prag);
816
817 if Prag_Nam = Name_Depends then
818 Depends := Prag;
819
820 elsif Prag_Nam = Name_Global then
821 Global := Prag;
822 end if;
823
824 Prag := Next_Pragma (Prag);
825 end loop;
826
827 -- Analyze Global first, as Depends may mention items classified in
828 -- the global categorization.
829
830 if Present (Global) then
831 Analyze_Global_In_Decl_Part (Global);
832 end if;
833
834 -- Depends must be analyzed after Global in order to see the modes of
835 -- all global items.
836
837 if Present (Depends) then
838 Analyze_Depends_In_Decl_Part (Depends);
839 end if;
840
841 -- Ensure that the contract cases or postconditions mention 'Result
842 -- or define a post-state.
843
844 Check_Result_And_Post_State (Subp_Id);
845 end if;
846
847 -- Restore the SPARK_Mode of the enclosing context after all delayed
848 -- pragmas have been analyzed.
849
850 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
851
852 -- Capture all global references in a generic subprogram now that the
853 -- contract has been analyzed.
854
855 if Is_Generic_Declaration_Or_Body (Subp_Decl) then
856 Save_Global_References_In_Contract
857 (Templ => Original_Node (Subp_Decl),
858 Gen_Id => Subp_Id);
859 end if;
860 end Analyze_Entry_Or_Subprogram_Contract;
861
862 ----------------------------------------------
863 -- Check_Type_Or_Object_External_Properties --
864 ----------------------------------------------
865
866 procedure Check_Type_Or_Object_External_Properties
867 (Type_Or_Obj_Id : Entity_Id)
868 is
869 Is_Type_Id : constant Boolean := Is_Type (Type_Or_Obj_Id);
870
871 -- Local variables
872
873 AR_Val : Boolean := False;
874 AW_Val : Boolean := False;
875 ER_Val : Boolean := False;
876 EW_Val : Boolean := False;
877 NC_Val : Boolean;
878 Seen : Boolean := False;
879 Prag : Node_Id;
880
881 -- Start of processing for Check_Type_Or_Object_External_Properties
882
883 begin
884 -- Analyze all external properties
885
886 if Is_Type_Id then
887 -- If the parent type of a derived type is volatile
888 -- then the derived type inherits volatility-related flags.
889
890 if Is_Derived_Type (Type_Or_Obj_Id) then
891 declare
892 Parent_Type : constant Entity_Id :=
893 Etype (Base_Type (Type_Or_Obj_Id));
894 begin
895 if Is_Effectively_Volatile (Parent_Type) then
896 AR_Val := Async_Readers_Enabled (Parent_Type);
897 AW_Val := Async_Writers_Enabled (Parent_Type);
898 ER_Val := Effective_Reads_Enabled (Parent_Type);
899 EW_Val := Effective_Writes_Enabled (Parent_Type);
900 end if;
901 end;
902 end if;
903 end if;
904
905 Prag := Get_Pragma (Type_Or_Obj_Id, Pragma_Async_Readers);
906
907 if Present (Prag) then
908 declare
909 Saved_AR_Val : constant Boolean := AR_Val;
910 begin
911 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
912 Seen := True;
913 if Saved_AR_Val and not AR_Val then
914 Error_Msg_N
915 ("illegal non-confirming Async_Readers specification",
916 Prag);
917 end if;
918 end;
919 end if;
920
921 Prag := Get_Pragma (Type_Or_Obj_Id, Pragma_Async_Writers);
922
923 if Present (Prag) then
924 declare
925 Saved_AW_Val : constant Boolean := AW_Val;
926 begin
927 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
928 Seen := True;
929 if Saved_AW_Val and not AW_Val then
930 Error_Msg_N
931 ("illegal non-confirming Async_Writers specification",
932 Prag);
933 end if;
934 end;
935 end if;
936
937 Prag := Get_Pragma (Type_Or_Obj_Id, Pragma_Effective_Reads);
938
939 if Present (Prag) then
940 declare
941 Saved_ER_Val : constant Boolean := ER_Val;
942 begin
943 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
944 Seen := True;
945 if Saved_ER_Val and not ER_Val then
946 Error_Msg_N
947 ("illegal non-confirming Effective_Reads specification",
948 Prag);
949 end if;
950 end;
951 end if;
952
953 Prag := Get_Pragma (Type_Or_Obj_Id, Pragma_Effective_Writes);
954
955 if Present (Prag) then
956 declare
957 Saved_EW_Val : constant Boolean := EW_Val;
958 begin
959 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
960 Seen := True;
961 if Saved_EW_Val and not EW_Val then
962 Error_Msg_N
963 ("illegal non-confirming Effective_Writes specification",
964 Prag);
965 end if;
966 end;
967 end if;
968
969 -- Verify the mutual interaction of the various external properties.
970 -- For types and variables for which No_Caching is enabled, it has been
971 -- checked already that only False values for other external properties
972 -- are allowed.
973
974 if Seen
975 and then not No_Caching_Enabled (Type_Or_Obj_Id)
976 then
977 Check_External_Properties
978 (Type_Or_Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
979 end if;
980
981 -- Analyze the non-external volatility property No_Caching
982
983 Prag := Get_Pragma (Type_Or_Obj_Id, Pragma_No_Caching);
984
985 if Present (Prag) then
986 Analyze_External_Property_In_Decl_Part (Prag, NC_Val);
987 end if;
988 end Check_Type_Or_Object_External_Properties;
989
990 -----------------------------
991 -- Analyze_Object_Contract --
992 -----------------------------
993
994 -- WARNING: This routine manages SPARK regions. Return statements must be
995 -- replaced by gotos which jump to the end of the routine and restore the
996 -- SPARK mode.
997
998 procedure Analyze_Object_Contract
999 (Obj_Id : Entity_Id;
1000 Freeze_Id : Entity_Id := Empty)
1001 is
1002 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
1003
1004 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
1005 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
1006 -- Save the SPARK_Mode-related data to restore on exit
1007
1008 Items : Node_Id;
1009 Prag : Node_Id;
1010 Ref_Elmt : Elmt_Id;
1011
1012 begin
1013 -- The loop parameter in an element iterator over a formal container
1014 -- is declared with an object declaration, but no contracts apply.
1015
1016 if Ekind (Obj_Id) = E_Loop_Parameter then
1017 return;
1018 end if;
1019
1020 -- Do not analyze a contract multiple times
1021
1022 Items := Contract (Obj_Id);
1023
1024 if Present (Items) then
1025 if Analyzed (Items) then
1026 return;
1027 else
1028 Set_Analyzed (Items);
1029 end if;
1030 end if;
1031
1032 -- The anonymous object created for a single concurrent type inherits
1033 -- the SPARK_Mode from the type. Due to the timing of contract analysis,
1034 -- delayed pragmas may be subject to the wrong SPARK_Mode, usually that
1035 -- of the enclosing context. To remedy this, restore the original mode
1036 -- of the related anonymous object.
1037
1038 if Is_Single_Concurrent_Object (Obj_Id)
1039 and then Present (SPARK_Pragma (Obj_Id))
1040 then
1041 Set_SPARK_Mode (Obj_Id);
1042 end if;
1043
1044 -- Checks related to external properties, same for constants and
1045 -- variables.
1046
1047 Check_Type_Or_Object_External_Properties (Type_Or_Obj_Id => Obj_Id);
1048
1049 -- Constant-related checks
1050
1051 if Ekind (Obj_Id) = E_Constant then
1052
1053 -- Analyze indicator Part_Of
1054
1055 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
1056
1057 -- Check whether the lack of indicator Part_Of agrees with the
1058 -- placement of the constant with respect to the state space.
1059
1060 if No (Prag) then
1061 Check_Missing_Part_Of (Obj_Id);
1062 end if;
1063
1064 -- Variable-related checks
1065
1066 else pragma Assert (Ekind (Obj_Id) = E_Variable);
1067
1068 -- The anonymous object created for a single task type carries
1069 -- pragmas Depends and Global of the type.
1070
1071 if Is_Single_Task_Object (Obj_Id) then
1072
1073 -- Analyze Global first, as Depends may mention items classified
1074 -- in the global categorization.
1075
1076 Prag := Get_Pragma (Obj_Id, Pragma_Global);
1077
1078 if Present (Prag) then
1079 Analyze_Global_In_Decl_Part (Prag);
1080 end if;
1081
1082 -- Depends must be analyzed after Global in order to see the modes
1083 -- of all global items.
1084
1085 Prag := Get_Pragma (Obj_Id, Pragma_Depends);
1086
1087 if Present (Prag) then
1088 Analyze_Depends_In_Decl_Part (Prag);
1089 end if;
1090 end if;
1091
1092 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
1093
1094 -- Analyze indicator Part_Of
1095
1096 if Present (Prag) then
1097 Analyze_Part_Of_In_Decl_Part (Prag, Freeze_Id);
1098
1099 -- The variable is a constituent of a single protected/task type
1100 -- and behaves as a component of the type. Verify that references
1101 -- to the variable occur within the definition or body of the type
1102 -- (SPARK RM 9.3).
1103
1104 if Present (Encapsulating_State (Obj_Id))
1105 and then Is_Single_Concurrent_Object
1106 (Encapsulating_State (Obj_Id))
1107 and then Present (Part_Of_References (Obj_Id))
1108 then
1109 Ref_Elmt := First_Elmt (Part_Of_References (Obj_Id));
1110 while Present (Ref_Elmt) loop
1111 Check_Part_Of_Reference (Obj_Id, Node (Ref_Elmt));
1112 Next_Elmt (Ref_Elmt);
1113 end loop;
1114 end if;
1115
1116 -- Otherwise check whether the lack of indicator Part_Of agrees with
1117 -- the placement of the variable with respect to the state space.
1118
1119 else
1120 Check_Missing_Part_Of (Obj_Id);
1121 end if;
1122 end if;
1123
1124 -- Common checks
1125
1126 if Comes_From_Source (Obj_Id) and then Is_Ghost_Entity (Obj_Id) then
1127
1128 -- A Ghost object cannot be of a type that yields a synchronized
1129 -- object (SPARK RM 6.9(21)).
1130
1131 if Yields_Synchronized_Object (Obj_Typ) then
1132 Error_Msg_N ("ghost object & cannot be synchronized", Obj_Id);
1133
1134 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(7)).
1135 -- One exception to this is the object that represents the dispatch
1136 -- table of a Ghost tagged type, as the symbol needs to be exported.
1137
1138 elsif Is_Exported (Obj_Id) then
1139 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
1140
1141 elsif Is_Imported (Obj_Id) then
1142 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
1143 end if;
1144 end if;
1145
1146 -- Restore the SPARK_Mode of the enclosing context after all delayed
1147 -- pragmas have been analyzed.
1148
1149 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
1150 end Analyze_Object_Contract;
1151
1152 -----------------------------------
1153 -- Analyze_Package_Body_Contract --
1154 -----------------------------------
1155
1156 -- WARNING: This routine manages SPARK regions. Return statements must be
1157 -- replaced by gotos which jump to the end of the routine and restore the
1158 -- SPARK mode.
1159
1160 procedure Analyze_Package_Body_Contract
1161 (Body_Id : Entity_Id;
1162 Freeze_Id : Entity_Id := Empty)
1163 is
1164 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
1165 Items : constant Node_Id := Contract (Body_Id);
1166 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
1167
1168 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
1169 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
1170 -- Save the SPARK_Mode-related data to restore on exit
1171
1172 Ref_State : Node_Id;
1173
1174 begin
1175 -- Do not analyze a contract multiple times
1176
1177 if Present (Items) then
1178 if Analyzed (Items) then
1179 return;
1180 else
1181 Set_Analyzed (Items);
1182 end if;
1183 end if;
1184
1185 -- Due to the timing of contract analysis, delayed pragmas may be
1186 -- subject to the wrong SPARK_Mode, usually that of the enclosing
1187 -- context. To remedy this, restore the original SPARK_Mode of the
1188 -- related package body.
1189
1190 Set_SPARK_Mode (Body_Id);
1191
1192 Ref_State := Get_Pragma (Body_Id, Pragma_Refined_State);
1193
1194 -- The analysis of pragma Refined_State detects whether the spec has
1195 -- abstract states available for refinement.
1196
1197 if Present (Ref_State) then
1198 Analyze_Refined_State_In_Decl_Part (Ref_State, Freeze_Id);
1199 end if;
1200
1201 -- Restore the SPARK_Mode of the enclosing context after all delayed
1202 -- pragmas have been analyzed.
1203
1204 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
1205
1206 -- Capture all global references in a generic package body now that the
1207 -- contract has been analyzed.
1208
1209 if Is_Generic_Declaration_Or_Body (Body_Decl) then
1210 Save_Global_References_In_Contract
1211 (Templ => Original_Node (Body_Decl),
1212 Gen_Id => Spec_Id);
1213 end if;
1214 end Analyze_Package_Body_Contract;
1215
1216 ------------------------------
1217 -- Analyze_Package_Contract --
1218 ------------------------------
1219
1220 -- WARNING: This routine manages SPARK regions. Return statements must be
1221 -- replaced by gotos which jump to the end of the routine and restore the
1222 -- SPARK mode.
1223
1224 procedure Analyze_Package_Contract (Pack_Id : Entity_Id) is
1225 Items : constant Node_Id := Contract (Pack_Id);
1226 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack_Id);
1227
1228 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
1229 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
1230 -- Save the SPARK_Mode-related data to restore on exit
1231
1232 Init : Node_Id := Empty;
1233 Init_Cond : Node_Id := Empty;
1234 Prag : Node_Id;
1235 Prag_Nam : Name_Id;
1236
1237 begin
1238 -- Do not analyze a contract multiple times
1239
1240 if Present (Items) then
1241 if Analyzed (Items) then
1242 return;
1243
1244 -- Do not analyze the contract of the internal package
1245 -- created to check conformance of an actual package.
1246 -- Such an internal package is removed from the tree after
1247 -- legality checks are completed, and it does not contain
1248 -- the declarations of all local entities of the generic.
1249
1250 elsif Is_Internal (Pack_Id)
1251 and then Is_Generic_Instance (Pack_Id)
1252 then
1253 return;
1254
1255 else
1256 Set_Analyzed (Items);
1257 end if;
1258 end if;
1259
1260 -- Due to the timing of contract analysis, delayed pragmas may be
1261 -- subject to the wrong SPARK_Mode, usually that of the enclosing
1262 -- context. To remedy this, restore the original SPARK_Mode of the
1263 -- related package.
1264
1265 Set_SPARK_Mode (Pack_Id);
1266
1267 if Present (Items) then
1268
1269 -- Locate and store pragmas Initial_Condition and Initializes, since
1270 -- their order of analysis matters.
1271
1272 Prag := Classifications (Items);
1273 while Present (Prag) loop
1274 Prag_Nam := Pragma_Name (Prag);
1275
1276 if Prag_Nam = Name_Initial_Condition then
1277 Init_Cond := Prag;
1278
1279 elsif Prag_Nam = Name_Initializes then
1280 Init := Prag;
1281 end if;
1282
1283 Prag := Next_Pragma (Prag);
1284 end loop;
1285
1286 -- Analyze the initialization-related pragmas. Initializes must come
1287 -- before Initial_Condition due to item dependencies.
1288
1289 if Present (Init) then
1290 Analyze_Initializes_In_Decl_Part (Init);
1291 end if;
1292
1293 if Present (Init_Cond) then
1294 Analyze_Initial_Condition_In_Decl_Part (Init_Cond);
1295 end if;
1296 end if;
1297
1298 -- Restore the SPARK_Mode of the enclosing context after all delayed
1299 -- pragmas have been analyzed.
1300
1301 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
1302
1303 -- Capture all global references in a generic package now that the
1304 -- contract has been analyzed.
1305
1306 if Is_Generic_Declaration_Or_Body (Pack_Decl) then
1307 Save_Global_References_In_Contract
1308 (Templ => Original_Node (Pack_Decl),
1309 Gen_Id => Pack_Id);
1310 end if;
1311 end Analyze_Package_Contract;
1312
1313 --------------------------------------------
1314 -- Analyze_Package_Instantiation_Contract --
1315 --------------------------------------------
1316
1317 -- WARNING: This routine manages SPARK regions. Return statements must be
1318 -- replaced by gotos which jump to the end of the routine and restore the
1319 -- SPARK mode.
1320
1321 procedure Analyze_Package_Instantiation_Contract (Inst_Id : Entity_Id) is
1322 Inst_Spec : constant Node_Id :=
1323 Instance_Spec (Unit_Declaration_Node (Inst_Id));
1324
1325 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
1326 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
1327 -- Save the SPARK_Mode-related data to restore on exit
1328
1329 Pack_Id : Entity_Id;
1330 Prag : Node_Id;
1331
1332 begin
1333 -- Nothing to do when the package instantiation is erroneous or left
1334 -- partially decorated.
1335
1336 if No (Inst_Spec) then
1337 return;
1338 end if;
1339
1340 Pack_Id := Defining_Entity (Inst_Spec);
1341 Prag := Get_Pragma (Pack_Id, Pragma_Part_Of);
1342
1343 -- Due to the timing of contract analysis, delayed pragmas may be
1344 -- subject to the wrong SPARK_Mode, usually that of the enclosing
1345 -- context. To remedy this, restore the original SPARK_Mode of the
1346 -- related package.
1347
1348 Set_SPARK_Mode (Pack_Id);
1349
1350 -- Check whether the lack of indicator Part_Of agrees with the placement
1351 -- of the package instantiation with respect to the state space. Nested
1352 -- package instantiations do not need to be checked because they inherit
1353 -- Part_Of indicator of the outermost package instantiation (see routine
1354 -- Propagate_Part_Of in Sem_Prag).
1355
1356 if In_Instance then
1357 null;
1358
1359 elsif No (Prag) then
1360 Check_Missing_Part_Of (Pack_Id);
1361 end if;
1362
1363 -- Restore the SPARK_Mode of the enclosing context after all delayed
1364 -- pragmas have been analyzed.
1365
1366 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
1367 end Analyze_Package_Instantiation_Contract;
1368
1369 --------------------------------
1370 -- Analyze_Protected_Contract --
1371 --------------------------------
1372
1373 procedure Analyze_Protected_Contract (Prot_Id : Entity_Id) is
1374 Items : constant Node_Id := Contract (Prot_Id);
1375
1376 begin
1377 -- Do not analyze a contract multiple times
1378
1379 if Present (Items) then
1380 if Analyzed (Items) then
1381 return;
1382 else
1383 Set_Analyzed (Items);
1384 end if;
1385 end if;
1386 end Analyze_Protected_Contract;
1387
1388 -------------------------------------------
1389 -- Analyze_Subprogram_Body_Stub_Contract --
1390 -------------------------------------------
1391
1392 procedure Analyze_Subprogram_Body_Stub_Contract (Stub_Id : Entity_Id) is
1393 Stub_Decl : constant Node_Id := Parent (Parent (Stub_Id));
1394 Spec_Id : constant Entity_Id := Corresponding_Spec_Of_Stub (Stub_Decl);
1395
1396 begin
1397 -- A subprogram body stub may act as its own spec or as the completion
1398 -- of a previous declaration. Depending on the context, the contract of
1399 -- the stub may contain two sets of pragmas.
1400
1401 -- The stub is a completion, the applicable pragmas are:
1402 -- Refined_Depends
1403 -- Refined_Global
1404
1405 if Present (Spec_Id) then
1406 Analyze_Entry_Or_Subprogram_Body_Contract (Stub_Id);
1407
1408 -- The stub acts as its own spec, the applicable pragmas are:
1409 -- Always_Terminates
1410 -- Contract_Cases
1411 -- Depends
1412 -- Exceptional_Cases
1413 -- Global
1414 -- Postcondition
1415 -- Precondition
1416 -- Subprogram_Variant
1417 -- Test_Case
1418
1419 else
1420 Analyze_Entry_Or_Subprogram_Contract (Stub_Id);
1421 end if;
1422 end Analyze_Subprogram_Body_Stub_Contract;
1423
1424 ---------------------------
1425 -- Analyze_Task_Contract --
1426 ---------------------------
1427
1428 -- WARNING: This routine manages SPARK regions. Return statements must be
1429 -- replaced by gotos which jump to the end of the routine and restore the
1430 -- SPARK mode.
1431
1432 procedure Analyze_Task_Contract (Task_Id : Entity_Id) is
1433 Items : constant Node_Id := Contract (Task_Id);
1434
1435 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
1436 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
1437 -- Save the SPARK_Mode-related data to restore on exit
1438
1439 Prag : Node_Id;
1440
1441 begin
1442 -- Do not analyze a contract multiple times
1443
1444 if Present (Items) then
1445 if Analyzed (Items) then
1446 return;
1447 else
1448 Set_Analyzed (Items);
1449 end if;
1450 end if;
1451
1452 -- Due to the timing of contract analysis, delayed pragmas may be
1453 -- subject to the wrong SPARK_Mode, usually that of the enclosing
1454 -- context. To remedy this, restore the original SPARK_Mode of the
1455 -- related task unit.
1456
1457 Set_SPARK_Mode (Task_Id);
1458
1459 -- Analyze Global first, as Depends may mention items classified in the
1460 -- global categorization.
1461
1462 Prag := Get_Pragma (Task_Id, Pragma_Global);
1463
1464 if Present (Prag) then
1465 Analyze_Global_In_Decl_Part (Prag);
1466 end if;
1467
1468 -- Depends must be analyzed after Global in order to see the modes of
1469 -- all global items.
1470
1471 Prag := Get_Pragma (Task_Id, Pragma_Depends);
1472
1473 if Present (Prag) then
1474 Analyze_Depends_In_Decl_Part (Prag);
1475 end if;
1476
1477 -- Restore the SPARK_Mode of the enclosing context after all delayed
1478 -- pragmas have been analyzed.
1479
1480 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
1481 end Analyze_Task_Contract;
1482
1483 ---------------------------
1484 -- Analyze_Type_Contract --
1485 ---------------------------
1486
1487 procedure Analyze_Type_Contract (Type_Id : Entity_Id) is
1488 begin
1489 Check_Type_Or_Object_External_Properties
1490 (Type_Or_Obj_Id => Type_Id);
1491
1492 -- Analyze Pre/Post on access-to-subprogram type
1493
1494 if Ekind (Type_Id) in Access_Subprogram_Kind then
1495 Analyze_Entry_Or_Subprogram_Contract
1496 (Directly_Designated_Type (Type_Id));
1497 end if;
1498 end Analyze_Type_Contract;
1499
1500 ---------------------------------------
1501 -- Build_Subprogram_Contract_Wrapper --
1502 ---------------------------------------
1503
1504 procedure Build_Subprogram_Contract_Wrapper
1505 (Body_Id : Entity_Id;
1506 Stmts : List_Id;
1507 Decls : List_Id;
1508 Result : Entity_Id)
1509 is
1510 Body_Decl : constant Entity_Id := Unit_Declaration_Node (Body_Id);
1511 Loc : constant Source_Ptr := Sloc (Body_Decl);
1512 Spec_Id : constant Entity_Id := Corresponding_Spec (Body_Decl);
1513 Subp_Id : Entity_Id;
1514 Ret_Type : Entity_Id;
1515
1516 Wrapper_Id : Entity_Id;
1517 Wrapper_Body : Node_Id;
1518 Wrapper_Spec : Node_Id;
1519
1520 begin
1521 -- When there are no postcondition statements we do not need to
1522 -- generate a wrapper.
1523
1524 if No (Stmts) then
1525 return;
1526 end if;
1527
1528 -- Obtain the related subprogram id from the body id.
1529
1530 if Present (Spec_Id) then
1531 Subp_Id := Spec_Id;
1532 else
1533 Subp_Id := Body_Id;
1534 end if;
1535 Ret_Type := Etype (Subp_Id);
1536
1537 -- Generate the contracts wrapper by moving the original declarations
1538 -- and statements within a local subprogram, calling it and possibly
1539 -- preserving the result for the purpose of evaluating postconditions,
1540 -- contracts, type invariants, etc.
1541
1542 -- In the case of a function, generate:
1543 --
1544 -- function Original_Func (X : in out Integer) return Typ is
1545 -- <prologue renamings>
1546 -- <preconditions>
1547 --
1548 -- function _Wrapped_Statements return Typ is
1549 -- <original declarations>
1550 -- begin
1551 -- <original statements>
1552 -- end;
1553 --
1554 -- begin
1555 -- return
1556 -- Result_Obj : constant Typ := _Wrapped_Statements
1557 -- do
1558 -- <postconditions statements>
1559 -- end return;
1560 -- end;
1561
1562 -- Or else, in the case of a procedure, generate:
1563 --
1564 -- procedure Original_Proc (X : in out Integer) is
1565 -- <prologue renamings>
1566 -- <preconditions>
1567 --
1568 -- procedure _Wrapped_Statements is
1569 -- <original declarations>
1570 -- begin
1571 -- <original statements>
1572 -- end;
1573 --
1574 -- begin
1575 -- _Wrapped_Statements;
1576 -- <postconditions statements>
1577 -- end;
1578
1579 -- Create Identifier
1580
1581 Wrapper_Id := Make_Defining_Identifier (Loc, Name_uWrapped_Statements);
1582 Set_Debug_Info_Needed (Wrapper_Id);
1583 Set_Wrapped_Statements (Subp_Id, Wrapper_Id);
1584
1585 Set_Has_Pragma_Inline (Wrapper_Id, Has_Pragma_Inline (Subp_Id));
1586 Set_Has_Pragma_Inline_Always
1587 (Wrapper_Id, Has_Pragma_Inline_Always (Subp_Id));
1588
1589 -- Create specification and declaration for the wrapper
1590
1591 if No (Ret_Type) or else Ret_Type = Standard_Void_Type then
1592 Wrapper_Spec :=
1593 Make_Procedure_Specification (Loc,
1594 Defining_Unit_Name => Wrapper_Id);
1595 else
1596 Wrapper_Spec :=
1597 Make_Function_Specification (Loc,
1598 Defining_Unit_Name => Wrapper_Id,
1599 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
1600 end if;
1601
1602 -- Create the wrapper body using Body_Id's statements and declarations
1603
1604 Wrapper_Body :=
1605 Make_Subprogram_Body (Loc,
1606 Specification => Wrapper_Spec,
1607 Declarations => Declarations (Body_Decl),
1608 Handled_Statement_Sequence =>
1609 Relocate_Node (Handled_Statement_Sequence (Body_Decl)));
1610
1611 Append_To (Decls, Wrapper_Body);
1612 Set_Declarations (Body_Decl, Decls);
1613 Set_Handled_Statement_Sequence (Body_Decl,
1614 Make_Handled_Sequence_Of_Statements (Loc,
1615 End_Label => Make_Identifier (Loc, Chars (Wrapper_Id))));
1616
1617 -- Prepend a call to the wrapper when the subprogram is a procedure
1618
1619 if No (Ret_Type) or else Ret_Type = Standard_Void_Type then
1620 Prepend_To (Stmts,
1621 Make_Procedure_Call_Statement (Loc,
1622 Name => New_Occurrence_Of (Wrapper_Id, Loc)));
1623 Set_Statements
1624 (Handled_Statement_Sequence (Body_Decl), Stmts);
1625
1626 -- Generate the post-execution statements and the extended return
1627 -- when the subprogram being wrapped is a function.
1628
1629 else
1630 Set_Statements (Handled_Statement_Sequence (Body_Decl), New_List (
1631 Make_Extended_Return_Statement (Loc,
1632 Return_Object_Declarations => New_List (
1633 Make_Object_Declaration (Loc,
1634 Defining_Identifier => Result,
1635 Constant_Present => True,
1636 Object_Definition =>
1637 New_Occurrence_Of (Ret_Type, Loc),
1638 Expression =>
1639 Make_Function_Call (Loc,
1640 Name =>
1641 New_Occurrence_Of (Wrapper_Id, Loc)))),
1642 Handled_Statement_Sequence =>
1643 Make_Handled_Sequence_Of_Statements (Loc,
1644 Statements => Stmts))));
1645 end if;
1646 end Build_Subprogram_Contract_Wrapper;
1647
1648 ----------------------------------
1649 -- Build_Entry_Contract_Wrapper --
1650 ----------------------------------
1651
1652 procedure Build_Entry_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1653 Conc_Typ : constant Entity_Id := Scope (E);
1654 Loc : constant Source_Ptr := Sloc (E);
1655
1656 procedure Add_Discriminant_Renamings
1657 (Obj_Id : Entity_Id;
1658 Decls : List_Id);
1659 -- Add renaming declarations for all discriminants of concurrent type
1660 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1661 -- represents the concurrent object.
1662
1663 procedure Add_Matching_Formals
1664 (Formals : List_Id;
1665 Actuals : in out List_Id);
1666 -- Add formal parameters that match those of entry E to list Formals.
1667 -- The routine also adds matching actuals for the new formals to list
1668 -- Actuals.
1669
1670 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1671 -- Relocate pragma Prag to list To. The routine creates a new list if
1672 -- To does not exist.
1673
1674 --------------------------------
1675 -- Add_Discriminant_Renamings --
1676 --------------------------------
1677
1678 procedure Add_Discriminant_Renamings
1679 (Obj_Id : Entity_Id;
1680 Decls : List_Id)
1681 is
1682 Discr : Entity_Id;
1683 Renaming_Decl : Node_Id;
1684
1685 begin
1686 -- Inspect the discriminants of the concurrent type and generate a
1687 -- renaming for each one.
1688
1689 if Has_Discriminants (Conc_Typ) then
1690 Discr := First_Discriminant (Conc_Typ);
1691 while Present (Discr) loop
1692 Renaming_Decl :=
1693 Make_Object_Renaming_Declaration (Loc,
1694 Defining_Identifier =>
1695 Make_Defining_Identifier (Loc, Chars (Discr)),
1696 Subtype_Mark =>
1697 New_Occurrence_Of (Etype (Discr), Loc),
1698 Name =>
1699 Make_Selected_Component (Loc,
1700 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1701 Selector_Name =>
1702 Make_Identifier (Loc, Chars (Discr))));
1703
1704 Prepend_To (Decls, Renaming_Decl);
1705
1706 Next_Discriminant (Discr);
1707 end loop;
1708 end if;
1709 end Add_Discriminant_Renamings;
1710
1711 --------------------------
1712 -- Add_Matching_Formals --
1713 --------------------------
1714
1715 procedure Add_Matching_Formals
1716 (Formals : List_Id;
1717 Actuals : in out List_Id)
1718 is
1719 Formal : Entity_Id;
1720 New_Formal : Entity_Id;
1721
1722 begin
1723 -- Inspect the formal parameters of the entry and generate a new
1724 -- matching formal with the same name for the wrapper. A reference
1725 -- to the new formal becomes an actual in the entry call.
1726
1727 Formal := First_Formal (E);
1728 while Present (Formal) loop
1729 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1730 Append_To (Formals,
1731 Make_Parameter_Specification (Loc,
1732 Defining_Identifier => New_Formal,
1733 In_Present => In_Present (Parent (Formal)),
1734 Out_Present => Out_Present (Parent (Formal)),
1735 Parameter_Type =>
1736 New_Occurrence_Of (Etype (Formal), Loc)));
1737
1738 if No (Actuals) then
1739 Actuals := New_List;
1740 end if;
1741
1742 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1743 Next_Formal (Formal);
1744 end loop;
1745 end Add_Matching_Formals;
1746
1747 ---------------------
1748 -- Transfer_Pragma --
1749 ---------------------
1750
1751 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1752 New_Prag : Node_Id;
1753
1754 begin
1755 if No (To) then
1756 To := New_List;
1757 end if;
1758
1759 New_Prag := Relocate_Node (Prag);
1760
1761 Set_Analyzed (New_Prag, False);
1762 Append (New_Prag, To);
1763 end Transfer_Pragma;
1764
1765 -- Local variables
1766
1767 Items : constant Node_Id := Contract (E);
1768 Actuals : List_Id := No_List;
1769 Call : Node_Id;
1770 Call_Nam : Node_Id;
1771 Decls : List_Id := No_List;
1772 Formals : List_Id;
1773 Has_Pragma : Boolean := False;
1774 Index_Id : Entity_Id;
1775 Obj_Id : Entity_Id;
1776 Prag : Node_Id;
1777 Wrapper_Id : Entity_Id;
1778
1779 -- Start of processing for Build_Entry_Contract_Wrapper
1780
1781 begin
1782 -- This routine generates a specialized wrapper for a protected or task
1783 -- entry [family] which implements precondition/postcondition semantics.
1784 -- Preconditions and case guards of contract cases are checked before
1785 -- the protected action or rendezvous takes place.
1786
1787 -- procedure Wrapper
1788 -- (Obj_Id : Conc_Typ; -- concurrent object
1789 -- [Index : Index_Typ;] -- index of entry family
1790 -- [Formal_1 : ...; -- parameters of original entry
1791 -- Formal_N : ...])
1792 -- is
1793 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1794 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1795
1796 -- <contracts pragmas>
1797 -- <case guard checks>
1798
1799 -- begin
1800 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1801 -- end Wrapper;
1802
1803 -- Create the wrapper only when the entry has at least one executable
1804 -- contract item such as contract cases, precondition or postcondition.
1805
1806 if Present (Items) then
1807
1808 -- Inspect the list of pre/postconditions and transfer all available
1809 -- pragmas to the declarative list of the wrapper.
1810
1811 Prag := Pre_Post_Conditions (Items);
1812 while Present (Prag) loop
1813 if Pragma_Name_Unmapped (Prag) in Name_Postcondition
1814 | Name_Precondition
1815 and then Is_Checked (Prag)
1816 then
1817 Has_Pragma := True;
1818 Transfer_Pragma (Prag, To => Decls);
1819 end if;
1820
1821 Prag := Next_Pragma (Prag);
1822 end loop;
1823
1824 -- Inspect the list of test/contract cases and transfer only contract
1825 -- cases pragmas to the declarative part of the wrapper.
1826
1827 Prag := Contract_Test_Cases (Items);
1828 while Present (Prag) loop
1829 if Pragma_Name (Prag) = Name_Contract_Cases
1830 and then Is_Checked (Prag)
1831 then
1832 Has_Pragma := True;
1833 Transfer_Pragma (Prag, To => Decls);
1834 end if;
1835
1836 Prag := Next_Pragma (Prag);
1837 end loop;
1838 end if;
1839
1840 -- The entry lacks executable contract items and a wrapper is not needed
1841
1842 if not Has_Pragma then
1843 return;
1844 end if;
1845
1846 -- Create the profile of the wrapper. The first formal parameter is the
1847 -- concurrent object.
1848
1849 Obj_Id :=
1850 Make_Defining_Identifier (Loc,
1851 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1852
1853 Formals := New_List (
1854 Make_Parameter_Specification (Loc,
1855 Defining_Identifier => Obj_Id,
1856 Out_Present => True,
1857 In_Present => True,
1858 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1859
1860 -- Construct the call to the original entry. The call will be gradually
1861 -- augmented with an optional entry index and extra parameters.
1862
1863 Call_Nam :=
1864 Make_Selected_Component (Loc,
1865 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1866 Selector_Name => New_Occurrence_Of (E, Loc));
1867
1868 -- When creating a wrapper for an entry family, the second formal is the
1869 -- entry index.
1870
1871 if Ekind (E) = E_Entry_Family then
1872 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1873
1874 Append_To (Formals,
1875 Make_Parameter_Specification (Loc,
1876 Defining_Identifier => Index_Id,
1877 Parameter_Type =>
1878 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1879
1880 -- The call to the original entry becomes an indexed component to
1881 -- accommodate the entry index.
1882
1883 Call_Nam :=
1884 Make_Indexed_Component (Loc,
1885 Prefix => Call_Nam,
1886 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1887 end if;
1888
1889 -- Add formal parameters to match those of the entry and build actuals
1890 -- for the entry call.
1891
1892 Add_Matching_Formals (Formals, Actuals);
1893
1894 Call :=
1895 Make_Procedure_Call_Statement (Loc,
1896 Name => Call_Nam,
1897 Parameter_Associations => Actuals);
1898
1899 -- Add renaming declarations for the discriminants of the enclosing type
1900 -- as the various contract items may reference them.
1901
1902 Add_Discriminant_Renamings (Obj_Id, Decls);
1903
1904 Wrapper_Id :=
1905 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1906 Set_Contract_Wrapper (E, Wrapper_Id);
1907 Set_Is_Entry_Wrapper (Wrapper_Id);
1908
1909 -- The wrapper body is analyzed when the enclosing type is frozen
1910
1911 Append_Freeze_Action (Defining_Entity (Decl),
1912 Make_Subprogram_Body (Loc,
1913 Specification =>
1914 Make_Procedure_Specification (Loc,
1915 Defining_Unit_Name => Wrapper_Id,
1916 Parameter_Specifications => Formals),
1917 Declarations => Decls,
1918 Handled_Statement_Sequence =>
1919 Make_Handled_Sequence_Of_Statements (Loc,
1920 Statements => New_List (Call))));
1921 end Build_Entry_Contract_Wrapper;
1922
1923 ---------------------------
1924 -- Check_Class_Condition --
1925 ---------------------------
1926
1927 procedure Check_Class_Condition
1928 (Cond : Node_Id;
1929 Subp : Entity_Id;
1930 Par_Subp : Entity_Id;
1931 Is_Precondition : Boolean)
1932 is
1933 function Check_Entity (N : Node_Id) return Traverse_Result;
1934 -- Check reference to formal of inherited operation or to primitive
1935 -- operation of root type.
1936
1937 ------------------
1938 -- Check_Entity --
1939 ------------------
1940
1941 function Check_Entity (N : Node_Id) return Traverse_Result is
1942 New_E : Entity_Id;
1943 Orig_E : Entity_Id;
1944
1945 begin
1946 if Nkind (N) = N_Identifier
1947 and then Present (Entity (N))
1948 and then
1949 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1950 and then
1951 (Nkind (Parent (N)) /= N_Attribute_Reference
1952 or else Attribute_Name (Parent (N)) /= Name_Class)
1953 then
1954 -- These checks do not apply to dispatching calls within the
1955 -- condition, but only to calls whose static tag is that of
1956 -- the parent type.
1957
1958 if Is_Subprogram (Entity (N))
1959 and then Nkind (Parent (N)) = N_Function_Call
1960 and then Present (Controlling_Argument (Parent (N)))
1961 then
1962 return OK;
1963 end if;
1964
1965 -- Determine whether entity has a renaming
1966
1967 Orig_E := Entity (N);
1968 New_E := Get_Mapped_Entity (Orig_E);
1969
1970 if Present (New_E) then
1971
1972 -- AI12-0166: A precondition for a protected operation
1973 -- cannot include an internal call to a protected function
1974 -- of the type. In the case of an inherited condition for an
1975 -- overriding operation, both the operation and the function
1976 -- are given by primitive wrappers.
1977
1978 if Is_Precondition
1979 and then Ekind (New_E) = E_Function
1980 and then Is_Primitive_Wrapper (New_E)
1981 and then Is_Primitive_Wrapper (Subp)
1982 and then Scope (Subp) = Scope (New_E)
1983 then
1984 Error_Msg_Node_2 := Wrapped_Entity (Subp);
1985 Error_Msg_NE
1986 ("internal call to& cannot appear in inherited "
1987 & "precondition of protected operation&",
1988 Subp, Wrapped_Entity (New_E));
1989 end if;
1990 end if;
1991
1992 -- Check that there are no calls left to abstract operations if
1993 -- the current subprogram is not abstract.
1994
1995 if Present (New_E)
1996 and then Nkind (Parent (N)) = N_Function_Call
1997 and then N = Name (Parent (N))
1998 then
1999 if not Is_Abstract_Subprogram (Subp)
2000 and then Is_Abstract_Subprogram (New_E)
2001 then
2002 Error_Msg_Sloc := Sloc (Current_Scope);
2003 Error_Msg_Node_2 := Subp;
2004
2005 if Comes_From_Source (Subp) then
2006 Error_Msg_NE
2007 ("cannot call abstract subprogram & in inherited "
2008 & "condition for&#", Subp, New_E);
2009 else
2010 Error_Msg_NE
2011 ("cannot call abstract subprogram & in inherited "
2012 & "condition for inherited&#", Subp, New_E);
2013 end if;
2014
2015 -- In SPARK mode, report error on inherited condition for an
2016 -- inherited operation if it contains a call to an overriding
2017 -- operation, because this implies that the pre/postconditions
2018 -- of the inherited operation have changed silently.
2019
2020 elsif SPARK_Mode = On
2021 and then Warn_On_Suspicious_Contract
2022 and then Present (Alias (Subp))
2023 and then Present (New_E)
2024 and then Comes_From_Source (New_E)
2025 then
2026 Error_Msg_N
2027 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
2028 Parent (Subp));
2029 Error_Msg_Sloc := Sloc (New_E);
2030 Error_Msg_Node_2 := Subp;
2031 Error_Msg_NE
2032 ("\overriding of&# forces overriding of&",
2033 Parent (Subp), New_E);
2034 end if;
2035 end if;
2036 end if;
2037
2038 return OK;
2039 end Check_Entity;
2040
2041 procedure Check_Condition_Entities is
2042 new Traverse_Proc (Check_Entity);
2043
2044 -- Start of processing for Check_Class_Condition
2045
2046 begin
2047 -- No check required if the subprograms match
2048
2049 if Par_Subp = Subp then
2050 return;
2051 end if;
2052
2053 Update_Primitives_Mapping (Par_Subp, Subp);
2054 Map_Formals (Par_Subp, Subp);
2055 Check_Condition_Entities (Cond);
2056 end Check_Class_Condition;
2057
2058 -----------------------------
2059 -- Create_Generic_Contract --
2060 -----------------------------
2061
2062 procedure Create_Generic_Contract (Unit : Node_Id) is
2063 Templ : constant Node_Id := Original_Node (Unit);
2064 Templ_Id : constant Entity_Id := Defining_Entity (Templ);
2065
2066 procedure Add_Generic_Contract_Pragma (Prag : Node_Id);
2067 -- Add a single contract-related source pragma Prag to the contract of
2068 -- generic template Templ_Id.
2069
2070 ---------------------------------
2071 -- Add_Generic_Contract_Pragma --
2072 ---------------------------------
2073
2074 procedure Add_Generic_Contract_Pragma (Prag : Node_Id) is
2075 Prag_Templ : Node_Id;
2076
2077 begin
2078 -- Mark the pragma to prevent the premature capture of global
2079 -- references when capturing global references of the context
2080 -- (see Save_References_In_Pragma).
2081
2082 Set_Is_Generic_Contract_Pragma (Prag);
2083
2084 -- Pragmas that apply to a generic subprogram declaration are not
2085 -- part of the semantic structure of the generic template:
2086
2087 -- generic
2088 -- procedure Example (Formal : Integer);
2089 -- pragma Precondition (Formal > 0);
2090
2091 -- Create a generic template for such pragmas and link the template
2092 -- of the pragma with the generic template.
2093
2094 if Nkind (Templ) = N_Generic_Subprogram_Declaration then
2095 Rewrite
2096 (Prag, Copy_Generic_Node (Prag, Empty, Instantiating => False));
2097 Prag_Templ := Original_Node (Prag);
2098
2099 Set_Is_Generic_Contract_Pragma (Prag_Templ);
2100 Add_Contract_Item (Prag_Templ, Templ_Id);
2101
2102 -- Otherwise link the pragma with the generic template
2103
2104 else
2105 Add_Contract_Item (Prag, Templ_Id);
2106 end if;
2107
2108 exception
2109 -- We do not stop the compilation at this point in the case of an
2110 -- invalid pragma because it will be properly diagnosed afterward.
2111
2112 when Contract_Error => null;
2113 end Add_Generic_Contract_Pragma;
2114
2115 -- Local variables
2116
2117 Context : constant Node_Id := Parent (Unit);
2118 Decl : Node_Id := Empty;
2119
2120 -- Start of processing for Create_Generic_Contract
2121
2122 begin
2123 -- A generic package declaration carries contract-related source pragmas
2124 -- in its visible declarations.
2125
2126 if Nkind (Templ) = N_Generic_Package_Declaration then
2127 Mutate_Ekind (Templ_Id, E_Generic_Package);
2128
2129 Decl := First (Visible_Declarations (Specification (Templ)));
2130
2131 -- A generic package body carries contract-related source pragmas in its
2132 -- declarations.
2133
2134 elsif Nkind (Templ) = N_Package_Body then
2135 Mutate_Ekind (Templ_Id, E_Package_Body);
2136
2137 Decl := First (Declarations (Templ));
2138
2139 -- Generic subprogram declaration
2140
2141 elsif Nkind (Templ) = N_Generic_Subprogram_Declaration then
2142 if Nkind (Specification (Templ)) = N_Function_Specification then
2143 Mutate_Ekind (Templ_Id, E_Generic_Function);
2144 else
2145 Mutate_Ekind (Templ_Id, E_Generic_Procedure);
2146 end if;
2147
2148 -- When the generic subprogram acts as a compilation unit, inspect
2149 -- the Pragmas_After list for contract-related source pragmas.
2150
2151 if Nkind (Context) = N_Compilation_Unit then
2152 if Present (Aux_Decls_Node (Context)) then
2153 Decl := First (Pragmas_After (Aux_Decls_Node (Context)));
2154 end if;
2155
2156 -- Otherwise inspect the successive declarations for contract-related
2157 -- source pragmas.
2158
2159 else
2160 Decl := Next (Unit);
2161 end if;
2162
2163 -- A generic subprogram body carries contract-related source pragmas in
2164 -- its declarations.
2165
2166 elsif Nkind (Templ) = N_Subprogram_Body then
2167 Mutate_Ekind (Templ_Id, E_Subprogram_Body);
2168
2169 Decl := First (Declarations (Templ));
2170 end if;
2171
2172 -- Inspect the relevant declarations looking for contract-related source
2173 -- pragmas and add them to the contract of the generic unit.
2174
2175 while Present (Decl) loop
2176 if Comes_From_Source (Decl) then
2177 if Nkind (Decl) = N_Pragma then
2178
2179 -- The source pragma is a contract annotation
2180
2181 if Is_Contract_Annotation (Decl) then
2182 Add_Generic_Contract_Pragma (Decl);
2183 end if;
2184
2185 -- The region where a contract-related source pragma may appear
2186 -- ends with the first source non-pragma declaration or statement.
2187
2188 else
2189 exit;
2190 end if;
2191 end if;
2192
2193 Next (Decl);
2194 end loop;
2195 end Create_Generic_Contract;
2196
2197 --------------------------------
2198 -- Expand_Subprogram_Contract --
2199 --------------------------------
2200
2201 procedure Expand_Subprogram_Contract (Body_Id : Entity_Id) is
2202 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
2203 Spec_Id : constant Entity_Id := Corresponding_Spec (Body_Decl);
2204
2205 procedure Add_Invariant_And_Predicate_Checks
2206 (Subp_Id : Entity_Id;
2207 Stmts : in out List_Id;
2208 Result : out Node_Id);
2209 -- Process the result of function Subp_Id (if applicable) and all its
2210 -- formals. Add invariant and predicate checks where applicable. The
2211 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
2212 -- function, Result contains the entity of parameter _Result, to be
2213 -- used in the creation of procedure _Postconditions.
2214
2215 procedure Add_Stable_Property_Contracts
2216 (Subp_Id : Entity_Id; Class_Present : Boolean);
2217 -- Augment postcondition contracts to reflect Stable_Property aspect
2218 -- (if Class_Present = False) or Stable_Property'Class aspect (if
2219 -- Class_Present = True).
2220
2221 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
2222 -- Append a node to a list. If there is no list, create a new one. When
2223 -- the item denotes a pragma, it is added to the list only when it is
2224 -- enabled.
2225
2226 procedure Process_Contract_Cases
2227 (Stmts : in out List_Id;
2228 Decls : List_Id);
2229 -- Process pragma Contract_Cases. This routine prepends items to the
2230 -- body declarations and appends items to list Stmts.
2231
2232 procedure Process_Postconditions (Stmts : in out List_Id);
2233 -- Collect all [inherited] spec and body postconditions and accumulate
2234 -- their pragma Check equivalents in list Stmts.
2235
2236 procedure Process_Preconditions (Decls : in out List_Id);
2237 -- Collect all [inherited] spec and body preconditions and prepend their
2238 -- pragma Check equivalents to the declarations of the body.
2239
2240 ----------------------------------------
2241 -- Add_Invariant_And_Predicate_Checks --
2242 ----------------------------------------
2243
2244 procedure Add_Invariant_And_Predicate_Checks
2245 (Subp_Id : Entity_Id;
2246 Stmts : in out List_Id;
2247 Result : out Node_Id)
2248 is
2249 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
2250 -- Id denotes the return value of a function or a formal parameter.
2251 -- Add an invariant check if the type of Id is access to a type with
2252 -- invariants. The routine appends the generated code to Stmts.
2253
2254 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
2255 -- Determine whether type Typ can benefit from invariant checks. To
2256 -- qualify, the type must have a non-null invariant procedure and
2257 -- subprogram Subp_Id must appear visible from the point of view of
2258 -- the type.
2259
2260 ---------------------------------
2261 -- Add_Invariant_Access_Checks --
2262 ---------------------------------
2263
2264 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
2265 Loc : constant Source_Ptr := Sloc (Body_Decl);
2266 Ref : Node_Id;
2267 Typ : Entity_Id;
2268
2269 begin
2270 Typ := Etype (Id);
2271
2272 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
2273 Typ := Designated_Type (Typ);
2274
2275 if Invariant_Checks_OK (Typ) then
2276 Ref :=
2277 Make_Explicit_Dereference (Loc,
2278 Prefix => New_Occurrence_Of (Id, Loc));
2279 Set_Etype (Ref, Typ);
2280
2281 -- Generate:
2282 -- if <Id> /= null then
2283 -- <invariant_call (<Ref>)>
2284 -- end if;
2285
2286 Append_Enabled_Item
2287 (Item =>
2288 Make_If_Statement (Loc,
2289 Condition =>
2290 Make_Op_Ne (Loc,
2291 Left_Opnd => New_Occurrence_Of (Id, Loc),
2292 Right_Opnd => Make_Null (Loc)),
2293 Then_Statements => New_List (
2294 Make_Invariant_Call (Ref))),
2295 List => Stmts);
2296 end if;
2297 end if;
2298 end Add_Invariant_Access_Checks;
2299
2300 -------------------------
2301 -- Invariant_Checks_OK --
2302 -------------------------
2303
2304 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
2305 function Has_Public_Visibility_Of_Subprogram return Boolean;
2306 -- Determine whether type Typ has public visibility of subprogram
2307 -- Subp_Id.
2308
2309 -----------------------------------------
2310 -- Has_Public_Visibility_Of_Subprogram --
2311 -----------------------------------------
2312
2313 function Has_Public_Visibility_Of_Subprogram return Boolean is
2314 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
2315
2316 begin
2317 -- An Initialization procedure must be considered visible even
2318 -- though it is internally generated.
2319
2320 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
2321 return True;
2322
2323 elsif Ekind (Scope (Typ)) /= E_Package then
2324 return False;
2325
2326 -- Internally generated code is never publicly visible except
2327 -- for a subprogram that is the implementation of an expression
2328 -- function. In that case the visibility is determined by the
2329 -- last check.
2330
2331 elsif not Comes_From_Source (Subp_Decl)
2332 and then
2333 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
2334 or else not
2335 Comes_From_Source (Defining_Entity (Subp_Decl)))
2336 then
2337 return False;
2338
2339 -- Determine whether the subprogram is declared in the visible
2340 -- declarations of the package containing the type, or in the
2341 -- visible declaration of a child unit of that package.
2342
2343 elsif Is_List_Member (Subp_Decl) then
2344 declare
2345 Decls : constant List_Id :=
2346 List_Containing (Subp_Decl);
2347 Subp_Scope : constant Entity_Id :=
2348 Scope (Defining_Entity (Subp_Decl));
2349 Typ_Scope : constant Entity_Id := Scope (Typ);
2350
2351 begin
2352 return
2353 Decls = Visible_Declarations
2354 (Specification (Unit_Declaration_Node (Typ_Scope)))
2355
2356 or else
2357 (Ekind (Subp_Scope) = E_Package
2358 and then Typ_Scope /= Subp_Scope
2359 and then Is_Child_Unit (Subp_Scope)
2360 and then
2361 Is_Ancestor_Package (Typ_Scope, Subp_Scope)
2362 and then
2363 Decls = Visible_Declarations
2364 (Specification
2365 (Unit_Declaration_Node (Subp_Scope))));
2366 end;
2367
2368 -- Determine whether the subprogram is a child subprogram of
2369 -- of the package containing the type.
2370
2371 else
2372 pragma Assert
2373 (Nkind (Parent (Subp_Decl)) = N_Compilation_Unit);
2374
2375 declare
2376 Subp_Scope : constant Entity_Id :=
2377 Scope (Defining_Entity (Subp_Decl));
2378 Typ_Scope : constant Entity_Id := Scope (Typ);
2379
2380 begin
2381 return
2382 Ekind (Subp_Scope) = E_Package
2383 and then
2384 (Typ_Scope = Subp_Scope
2385 or else
2386 (Is_Child_Unit (Subp_Scope)
2387 and then Is_Ancestor_Package
2388 (Typ_Scope, Subp_Scope)));
2389 end;
2390 end if;
2391 end Has_Public_Visibility_Of_Subprogram;
2392
2393 -- Start of processing for Invariant_Checks_OK
2394
2395 begin
2396 return
2397 Has_Invariants (Typ)
2398 and then Present (Invariant_Procedure (Typ))
2399 and then not Has_Null_Body (Invariant_Procedure (Typ))
2400 and then Has_Public_Visibility_Of_Subprogram;
2401 end Invariant_Checks_OK;
2402
2403 -- Local variables
2404
2405 Loc : constant Source_Ptr := Sloc (Body_Decl);
2406 -- Source location of subprogram body contract
2407
2408 Formal : Entity_Id;
2409 Typ : Entity_Id;
2410
2411 -- Start of processing for Add_Invariant_And_Predicate_Checks
2412
2413 begin
2414 Result := Empty;
2415
2416 -- Process the result of a function
2417
2418 if Ekind (Subp_Id) = E_Function then
2419 Typ := Etype (Subp_Id);
2420
2421 -- Generate _Result which is used in procedure _Postconditions to
2422 -- verify the return value.
2423
2424 Result := Make_Defining_Identifier (Loc, Name_uResult);
2425 Set_Etype (Result, Typ);
2426
2427 -- Add an invariant check when the return type has invariants and
2428 -- the related function is visible to the outside.
2429
2430 if Invariant_Checks_OK (Typ) then
2431 Append_Enabled_Item
2432 (Item =>
2433 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
2434 List => Stmts);
2435 end if;
2436
2437 -- Add an invariant check when the return type is an access to a
2438 -- type with invariants.
2439
2440 Add_Invariant_Access_Checks (Result);
2441 end if;
2442
2443 -- Add invariant checks for all formals that qualify (see AI05-0289
2444 -- and AI12-0044).
2445
2446 Formal := First_Formal (Subp_Id);
2447 while Present (Formal) loop
2448 Typ := Etype (Formal);
2449
2450 if Ekind (Formal) /= E_In_Parameter
2451 or else Ekind (Subp_Id) = E_Procedure
2452 or else Is_Access_Type (Typ)
2453 then
2454 if Invariant_Checks_OK (Typ) then
2455 Append_Enabled_Item
2456 (Item =>
2457 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
2458 List => Stmts);
2459 end if;
2460
2461 Add_Invariant_Access_Checks (Formal);
2462
2463 -- Note: we used to add predicate checks for OUT and IN OUT
2464 -- formals here, but that was misguided, since such checks are
2465 -- performed on the caller side, based on the predicate of the
2466 -- actual, rather than the predicate of the formal.
2467
2468 end if;
2469
2470 Next_Formal (Formal);
2471 end loop;
2472 end Add_Invariant_And_Predicate_Checks;
2473
2474 -----------------------------------
2475 -- Add_Stable_Property_Contracts --
2476 -----------------------------------
2477
2478 procedure Add_Stable_Property_Contracts
2479 (Subp_Id : Entity_Id; Class_Present : Boolean)
2480 is
2481 Loc : constant Source_Ptr := Sloc (Subp_Id);
2482
2483 procedure Insert_Stable_Property_Check
2484 (Formal : Entity_Id; Property_Function : Entity_Id);
2485 -- Build the pragma for one check and insert it in the tree.
2486
2487 function Make_Stable_Property_Condition
2488 (Formal : Entity_Id; Property_Function : Entity_Id) return Node_Id;
2489 -- Builds tree for "Func (Formal) = Func (Formal)'Old" expression.
2490
2491 function Stable_Properties
2492 (Aspect_Bearer : Entity_Id; Negated : out Boolean)
2493 return Subprogram_List;
2494 -- If no aspect specified, then returns length-zero result.
2495 -- Negated indicates that reserved word NOT was specified.
2496
2497 ----------------------------------
2498 -- Insert_Stable_Property_Check --
2499 ----------------------------------
2500
2501 procedure Insert_Stable_Property_Check
2502 (Formal : Entity_Id; Property_Function : Entity_Id) is
2503
2504 Args : constant List_Id :=
2505 New_List
2506 (Make_Pragma_Argument_Association
2507 (Sloc => Loc,
2508 Expression =>
2509 Make_Stable_Property_Condition
2510 (Formal => Formal,
2511 Property_Function => Property_Function)),
2512 Make_Pragma_Argument_Association
2513 (Sloc => Loc,
2514 Expression =>
2515 Make_String_Literal
2516 (Sloc => Loc,
2517 Strval =>
2518 "failed stable property check at "
2519 & Build_Location_String (Loc)
2520 & " for parameter "
2521 & To_String (Fully_Qualified_Name_String
2522 (Formal, Append_NUL => False))
2523 & " and property function "
2524 & To_String (Fully_Qualified_Name_String
2525 (Property_Function, Append_NUL => False))
2526 )));
2527
2528 Prag : constant Node_Id :=
2529 Make_Pragma (Loc,
2530 Pragma_Identifier =>
2531 Make_Identifier (Loc, Name_Postcondition),
2532 Pragma_Argument_Associations => Args,
2533 Class_Present => Class_Present);
2534
2535 Subp_Decl : constant Node_Id := Enclosing_Declaration (Subp_Id);
2536 pragma Assert (Is_Declaration (Subp_Decl));
2537 begin
2538 Insert_After_And_Analyze (Subp_Decl, Prag);
2539 end Insert_Stable_Property_Check;
2540
2541 ------------------------------------
2542 -- Make_Stable_Property_Condition --
2543 ------------------------------------
2544
2545 function Make_Stable_Property_Condition
2546 (Formal : Entity_Id; Property_Function : Entity_Id) return Node_Id
2547 is
2548 function Call_Property_Function return Node_Id is
2549 (Make_Function_Call
2550 (Loc,
2551 Name =>
2552 New_Occurrence_Of (Property_Function, Loc),
2553 Parameter_Associations =>
2554 New_List (New_Occurrence_Of (Formal, Loc))));
2555 begin
2556 return Make_Op_Eq
2557 (Loc,
2558 Call_Property_Function,
2559 Make_Attribute_Reference
2560 (Loc,
2561 Prefix => Call_Property_Function,
2562 Attribute_Name => Name_Old));
2563 end Make_Stable_Property_Condition;
2564
2565 -----------------------
2566 -- Stable_Properties --
2567 -----------------------
2568
2569 function Stable_Properties
2570 (Aspect_Bearer : Entity_Id; Negated : out Boolean)
2571 return Subprogram_List
2572 is
2573 Aspect_Spec : Node_Id :=
2574 Find_Value_Of_Aspect
2575 (Aspect_Bearer, Aspect_Stable_Properties,
2576 Class_Present => Class_Present);
2577 begin
2578 -- ??? For a derived type, we wish Find_Value_Of_Aspect
2579 -- somehow knew that this aspect is not inherited.
2580 -- But it doesn't, so we cope with that here.
2581 --
2582 -- There are probably issues here with inheritance from
2583 -- interface types, where just looking for the one parent type
2584 -- isn't enough. But this is far from the only work needed for
2585 -- Stable_Properties'Class for interface types.
2586
2587 if Is_Derived_Type (Aspect_Bearer) then
2588 declare
2589 Parent_Type : constant Entity_Id :=
2590 Etype (Base_Type (Aspect_Bearer));
2591 begin
2592 if Aspect_Spec =
2593 Find_Value_Of_Aspect
2594 (Parent_Type, Aspect_Stable_Properties,
2595 Class_Present => Class_Present)
2596 then
2597 -- prevent inheritance
2598 Aspect_Spec := Empty;
2599 end if;
2600 end;
2601 end if;
2602
2603 if No (Aspect_Spec) then
2604 Negated := Aspect_Bearer = Subp_Id;
2605 -- This is a little bit subtle.
2606 -- We need to assign True in the Subp_Id case in order to
2607 -- distinguish between no aspect spec at all vs. an
2608 -- explicitly specified "with S_P => []" empty list.
2609 -- In both cases Stable_Properties will return a length-0
2610 -- array, but the two cases are not equivalent.
2611 -- Very roughly speaking the lack of an S_P aspect spec for
2612 -- a subprogram would be equivalent to something like
2613 -- "with S_P => [not]", where we apply the "not" modifier to
2614 -- an empty set of subprograms, if such a construct existed.
2615 -- We could just assign True here, but it seems untidy to
2616 -- return True in the case of an aspect spec for a type
2617 -- (since negation is only allowed for subp S_P aspects).
2618
2619 return (1 .. 0 => <>);
2620 else
2621 return Parse_Aspect_Stable_Properties
2622 (Aspect_Spec, Negated => Negated);
2623 end if;
2624 end Stable_Properties;
2625
2626 Formal : Entity_Id := First_Formal (Subp_Id);
2627 Type_Of_Formal : Entity_Id;
2628
2629 Subp_Properties_Negated : Boolean;
2630 Subp_Properties : constant Subprogram_List :=
2631 Stable_Properties (Subp_Id, Subp_Properties_Negated);
2632
2633 -- start of processing for Add_Stable_Property_Contracts
2634
2635 begin
2636 if not (Is_Primitive (Subp_Id) and then Comes_From_Source (Subp_Id))
2637 then
2638 return;
2639 end if;
2640
2641 while Present (Formal) loop
2642 Type_Of_Formal := Base_Type (Etype (Formal));
2643
2644 if not Subp_Properties_Negated then
2645
2646 for SPF_Id of Subp_Properties loop
2647 if Type_Of_Formal = Base_Type (Etype (First_Formal (SPF_Id)))
2648 and then Scope (Type_Of_Formal) = Scope (Subp_Id)
2649 then
2650 -- ??? Need to filter out checks for SPFs that are
2651 -- mentioned explicitly in the postcondition of
2652 -- Subp_Id.
2653
2654 Insert_Stable_Property_Check
2655 (Formal => Formal, Property_Function => SPF_Id);
2656 end if;
2657 end loop;
2658
2659 elsif Scope (Type_Of_Formal) = Scope (Subp_Id) then
2660 declare
2661 Ignored : Boolean range False .. False;
2662
2663 Typ_Property_Funcs : constant Subprogram_List :=
2664 Stable_Properties (Type_Of_Formal, Negated => Ignored);
2665
2666 function Excluded_By_Aspect_Spec_Of_Subp
2667 (SPF_Id : Entity_Id) return Boolean;
2668 -- Examine Subp_Properties to determine whether SPF should
2669 -- be excluded.
2670
2671 -------------------------------------
2672 -- Excluded_By_Aspect_Spec_Of_Subp --
2673 -------------------------------------
2674
2675 function Excluded_By_Aspect_Spec_Of_Subp
2676 (SPF_Id : Entity_Id) return Boolean is
2677 begin
2678 pragma Assert (Subp_Properties_Negated);
2679 -- Look through renames for equality test here ???
2680 return (for some F of Subp_Properties => F = SPF_Id);
2681 end Excluded_By_Aspect_Spec_Of_Subp;
2682
2683 -- Look through renames for equality test here ???
2684 Subp_Is_Stable_Property_Function : constant Boolean :=
2685 (for some F of Typ_Property_Funcs => F = Subp_Id);
2686 begin
2687 if not Subp_Is_Stable_Property_Function then
2688 for SPF_Id of Typ_Property_Funcs loop
2689 if not Excluded_By_Aspect_Spec_Of_Subp (SPF_Id) then
2690 -- ??? Need to filter out checks for SPFs that are
2691 -- mentioned explicitly in the postcondition of
2692 -- Subp_Id.
2693 Insert_Stable_Property_Check
2694 (Formal => Formal, Property_Function => SPF_Id);
2695 end if;
2696 end loop;
2697 end if;
2698 end;
2699 end if;
2700 Next_Formal (Formal);
2701 end loop;
2702 end Add_Stable_Property_Contracts;
2703
2704 -------------------------
2705 -- Append_Enabled_Item --
2706 -------------------------
2707
2708 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
2709 begin
2710 -- Do not chain ignored or disabled pragmas
2711
2712 if Nkind (Item) = N_Pragma
2713 and then (Is_Ignored (Item) or else Is_Disabled (Item))
2714 then
2715 null;
2716
2717 -- Otherwise, add the item
2718
2719 else
2720 Append_New (Item, List);
2721 end if;
2722 end Append_Enabled_Item;
2723
2724 ----------------------------
2725 -- Process_Contract_Cases --
2726 ----------------------------
2727
2728 procedure Process_Contract_Cases
2729 (Stmts : in out List_Id;
2730 Decls : List_Id)
2731 is
2732 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id);
2733 -- Process pragma Contract_Cases for subprogram Subp_Id
2734
2735 --------------------------------
2736 -- Process_Contract_Cases_For --
2737 --------------------------------
2738
2739 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id) is
2740 Items : constant Node_Id := Contract (Subp_Id);
2741 Prag : Node_Id;
2742
2743 begin
2744 if Present (Items) then
2745 Prag := Contract_Test_Cases (Items);
2746 while Present (Prag) loop
2747 if Is_Checked (Prag) then
2748 if Pragma_Name (Prag) = Name_Always_Terminates then
2749 Expand_Pragma_Always_Terminates (Prag);
2750
2751 elsif Pragma_Name (Prag) = Name_Contract_Cases then
2752 Expand_Pragma_Contract_Cases
2753 (CCs => Prag,
2754 Subp_Id => Subp_Id,
2755 Decls => Decls,
2756 Stmts => Stmts);
2757
2758 elsif Pragma_Name (Prag) = Name_Exceptional_Cases then
2759 Expand_Pragma_Exceptional_Cases (Prag);
2760
2761 elsif Pragma_Name (Prag) = Name_Exit_Cases then
2762 Expand_Pragma_Exit_Cases (Prag);
2763
2764 elsif Pragma_Name (Prag) = Name_Subprogram_Variant then
2765 Expand_Pragma_Subprogram_Variant
2766 (Prag => Prag,
2767 Subp_Id => Subp_Id,
2768 Body_Decls => Decls);
2769 end if;
2770 end if;
2771
2772 Prag := Next_Pragma (Prag);
2773 end loop;
2774 end if;
2775 end Process_Contract_Cases_For;
2776
2777 -- Start of processing for Process_Contract_Cases
2778
2779 begin
2780 Process_Contract_Cases_For (Body_Id);
2781
2782 if Present (Spec_Id) then
2783 Process_Contract_Cases_For (Spec_Id);
2784 end if;
2785 end Process_Contract_Cases;
2786
2787 ----------------------------
2788 -- Process_Postconditions --
2789 ----------------------------
2790
2791 procedure Process_Postconditions (Stmts : in out List_Id) is
2792 procedure Process_Body_Postconditions (Post_Nam : Name_Id);
2793 -- Collect all [refined] postconditions of a specific kind denoted
2794 -- by Post_Nam that belong to the body, and generate pragma Check
2795 -- equivalents in list Stmts.
2796
2797 procedure Process_Spec_Postconditions;
2798 -- Collect all [inherited] postconditions of the spec, and generate
2799 -- pragma Check equivalents in list Stmts.
2800
2801 ---------------------------------
2802 -- Process_Body_Postconditions --
2803 ---------------------------------
2804
2805 procedure Process_Body_Postconditions (Post_Nam : Name_Id) is
2806 Items : constant Node_Id := Contract (Body_Id);
2807 Unit_Decl : constant Node_Id := Parent (Body_Decl);
2808 Decl : Node_Id;
2809 Prag : Node_Id;
2810
2811 begin
2812 -- Process the contract
2813
2814 if Present (Items) then
2815 Prag := Pre_Post_Conditions (Items);
2816 while Present (Prag) loop
2817 if Pragma_Name (Prag) = Post_Nam
2818 and then Is_Checked (Prag)
2819 then
2820 Append_Enabled_Item
2821 (Item => Build_Pragma_Check_Equivalent (Prag),
2822 List => Stmts);
2823 end if;
2824
2825 Prag := Next_Pragma (Prag);
2826 end loop;
2827 end if;
2828
2829 -- The subprogram body being processed is actually the proper body
2830 -- of a stub with a corresponding spec. The subprogram stub may
2831 -- carry a postcondition pragma, in which case it must be taken
2832 -- into account. The pragma appears after the stub.
2833
2834 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
2835 Decl := Next (Corresponding_Stub (Unit_Decl));
2836 while Present (Decl) loop
2837
2838 -- Note that non-matching pragmas are skipped
2839
2840 if Nkind (Decl) = N_Pragma then
2841 if Pragma_Name (Decl) = Post_Nam
2842 and then Is_Checked (Decl)
2843 then
2844 Append_Enabled_Item
2845 (Item => Build_Pragma_Check_Equivalent (Decl),
2846 List => Stmts);
2847 end if;
2848
2849 -- Skip internally generated code
2850
2851 elsif not Comes_From_Source (Decl) then
2852 null;
2853
2854 -- Postcondition pragmas are usually grouped together. There
2855 -- is no need to inspect the whole declarative list.
2856
2857 else
2858 exit;
2859 end if;
2860
2861 Next (Decl);
2862 end loop;
2863 end if;
2864 end Process_Body_Postconditions;
2865
2866 ---------------------------------
2867 -- Process_Spec_Postconditions --
2868 ---------------------------------
2869
2870 procedure Process_Spec_Postconditions is
2871 Subps : constant Subprogram_List :=
2872 Inherited_Subprograms (Spec_Id);
2873 Seen : Subprogram_List (Subps'Range) := (others => Empty);
2874
2875 function Seen_Subp (Subp_Id : Entity_Id) return Boolean;
2876 -- Return True if the contract of subprogram Subp_Id has been
2877 -- processed.
2878
2879 ---------------
2880 -- Seen_Subp --
2881 ---------------
2882
2883 function Seen_Subp (Subp_Id : Entity_Id) return Boolean is
2884 begin
2885 for Index in Seen'Range loop
2886 if Seen (Index) = Subp_Id then
2887 return True;
2888 end if;
2889 end loop;
2890
2891 return False;
2892 end Seen_Subp;
2893
2894 -- Local variables
2895
2896 Item : Node_Id;
2897 Items : Node_Id;
2898 Prag : Node_Id;
2899 Subp_Id : Entity_Id;
2900
2901 -- Start of processing for Process_Spec_Postconditions
2902
2903 begin
2904 -- Process the contract
2905
2906 Items := Contract (Spec_Id);
2907
2908 if Present (Items) then
2909 Prag := Pre_Post_Conditions (Items);
2910 while Present (Prag) loop
2911 if Pragma_Name (Prag) = Name_Postcondition
2912 and then Is_Checked (Prag)
2913 then
2914 Append_Enabled_Item
2915 (Item => Build_Pragma_Check_Equivalent (Prag),
2916 List => Stmts);
2917 end if;
2918
2919 Prag := Next_Pragma (Prag);
2920 end loop;
2921 end if;
2922
2923 -- Process the contracts of all inherited subprograms, looking for
2924 -- class-wide postconditions.
2925
2926 for Index in Subps'Range loop
2927 Subp_Id := Subps (Index);
2928
2929 if Present (Alias (Subp_Id)) then
2930 Subp_Id := Ultimate_Alias (Subp_Id);
2931 end if;
2932
2933 -- Wrappers of class-wide pre/postconditions reference the
2934 -- parent primitive that has the inherited contract.
2935
2936 if Is_LSP_Wrapper (Subp_Id) then
2937 Subp_Id := LSP_Subprogram (Subp_Id);
2938 end if;
2939
2940 Items := Contract (Subp_Id);
2941
2942 if not Seen_Subp (Subp_Id) and then Present (Items) then
2943 Seen (Index) := Subp_Id;
2944
2945 Prag := Pre_Post_Conditions (Items);
2946 while Present (Prag) loop
2947 if Pragma_Name (Prag) = Name_Postcondition
2948 and then Class_Present (Prag)
2949 then
2950 Item :=
2951 Build_Pragma_Check_Equivalent
2952 (Prag => Prag,
2953 Subp_Id => Spec_Id,
2954 Inher_Id => Subp_Id);
2955
2956 -- The pragma Check equivalent of the class-wide
2957 -- postcondition is still created even though the
2958 -- pragma may be ignored because the equivalent
2959 -- performs semantic checks.
2960
2961 if Is_Checked (Prag) then
2962 Append_Enabled_Item (Item, Stmts);
2963 end if;
2964 end if;
2965
2966 Prag := Next_Pragma (Prag);
2967 end loop;
2968 end if;
2969 end loop;
2970 end Process_Spec_Postconditions;
2971
2972 pragma Unmodified (Stmts);
2973 -- Stmts is passed as IN OUT to signal that the list can be updated,
2974 -- even if the corresponding integer value representing the list does
2975 -- not change.
2976
2977 -- Start of processing for Process_Postconditions
2978
2979 begin
2980 -- The processing of postconditions is done in reverse order (body
2981 -- first) to ensure the following arrangement:
2982
2983 -- <refined postconditions from body>
2984 -- <postconditions from body>
2985 -- <postconditions from spec>
2986 -- <inherited postconditions>
2987
2988 Process_Body_Postconditions (Name_Refined_Post);
2989 Process_Body_Postconditions (Name_Postcondition);
2990
2991 if Present (Spec_Id) then
2992 Process_Spec_Postconditions;
2993 end if;
2994 end Process_Postconditions;
2995
2996 ---------------------------
2997 -- Process_Preconditions --
2998 ---------------------------
2999
3000 procedure Process_Preconditions (Decls : in out List_Id) is
3001 Insert_Node : Node_Id := Empty;
3002 -- The insertion node after which all pragma Check equivalents are
3003 -- inserted.
3004
3005 procedure Prepend_To_Decls (Item : Node_Id);
3006 -- Prepend a single item to the declarations of the subprogram body
3007
3008 procedure Prepend_Pragma_To_Decls (Prag : Node_Id);
3009 -- Prepend a normal precondition to the declarations of the body and
3010 -- analyze it.
3011
3012 procedure Process_Preconditions_For (Subp_Id : Entity_Id);
3013 -- Collect all preconditions of subprogram Subp_Id and prepend their
3014 -- pragma Check equivalents to the declarations of the body.
3015
3016 ----------------------
3017 -- Prepend_To_Decls --
3018 ----------------------
3019
3020 procedure Prepend_To_Decls (Item : Node_Id) is
3021 begin
3022 -- Ensure that the body has a declarative list
3023
3024 if No (Decls) then
3025 Decls := New_List;
3026 Set_Declarations (Body_Decl, Decls);
3027 end if;
3028
3029 Prepend_To (Decls, Item);
3030 end Prepend_To_Decls;
3031
3032 -----------------------------
3033 -- Prepend_Pragma_To_Decls --
3034 -----------------------------
3035
3036 procedure Prepend_Pragma_To_Decls (Prag : Node_Id) is
3037 Check_Prag : Node_Id;
3038
3039 begin
3040 -- Skip the sole class-wide precondition (if any) since it is
3041 -- processed by Merge_Class_Conditions.
3042
3043 if Class_Present (Prag) then
3044 null;
3045
3046 -- Accumulate the corresponding Check pragmas at the top of the
3047 -- declarations. Prepending the items ensures that they will be
3048 -- evaluated in their original order.
3049
3050 else
3051 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
3052 Prepend_To_Decls (Check_Prag);
3053
3054 end if;
3055 end Prepend_Pragma_To_Decls;
3056
3057 -------------------------------
3058 -- Process_Preconditions_For --
3059 -------------------------------
3060
3061 procedure Process_Preconditions_For (Subp_Id : Entity_Id) is
3062 Items : constant Node_Id := Contract (Subp_Id);
3063 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3064 Decl : Node_Id;
3065 Freeze_T : Boolean;
3066 Prag : Node_Id;
3067
3068 begin
3069 -- Process the contract. If the body is an expression function
3070 -- that is a completion, freeze types within, because this may
3071 -- not have been done yet, when the subprogram declaration and
3072 -- its completion by an expression function appear in distinct
3073 -- declarative lists of the same unit (visible and private).
3074
3075 Freeze_T :=
3076 Was_Expression_Function (Body_Decl)
3077 and then Sloc (Body_Id) /= Sloc (Subp_Id)
3078 and then In_Same_Source_Unit (Body_Id, Subp_Id)
3079 and then not In_Same_List (Body_Decl, Subp_Decl);
3080
3081 if Present (Items) then
3082 Prag := Pre_Post_Conditions (Items);
3083 while Present (Prag) loop
3084 if Pragma_Name (Prag) = Name_Precondition
3085 and then Is_Checked (Prag)
3086 then
3087 if Freeze_T
3088 and then Present (Corresponding_Aspect (Prag))
3089 then
3090 Freeze_Expr_Types
3091 (Def_Id => Subp_Id,
3092 Typ => Standard_Boolean,
3093 Expr =>
3094 Expression
3095 (First (Pragma_Argument_Associations (Prag))),
3096 N => Body_Decl);
3097 end if;
3098
3099 Prepend_Pragma_To_Decls (Prag);
3100 end if;
3101
3102 Prag := Next_Pragma (Prag);
3103 end loop;
3104 end if;
3105
3106 -- The subprogram declaration being processed is actually a body
3107 -- stub. The stub may carry a precondition pragma, in which case
3108 -- it must be taken into account. The pragma appears after the
3109 -- stub.
3110
3111 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
3112
3113 -- Inspect the declarations following the body stub
3114
3115 Decl := Next (Subp_Decl);
3116 while Present (Decl) loop
3117
3118 -- Note that non-matching pragmas are skipped
3119
3120 if Nkind (Decl) = N_Pragma then
3121 if Pragma_Name (Decl) = Name_Precondition
3122 and then Is_Checked (Decl)
3123 then
3124 Prepend_Pragma_To_Decls (Decl);
3125 end if;
3126
3127 -- Skip internally generated code
3128
3129 elsif not Comes_From_Source (Decl) then
3130 null;
3131
3132 -- Preconditions are usually grouped together. There is no
3133 -- need to inspect the whole declarative list.
3134
3135 else
3136 exit;
3137 end if;
3138
3139 Next (Decl);
3140 end loop;
3141 end if;
3142 end Process_Preconditions_For;
3143
3144 -- Local variables
3145
3146 Body_Decls : constant List_Id := Declarations (Body_Decl);
3147 Decl : Node_Id;
3148 Next_Decl : Node_Id;
3149
3150 -- Start of processing for Process_Preconditions
3151
3152 begin
3153 -- Find the proper insertion point for all pragma Check equivalents
3154
3155 if Present (Body_Decls) then
3156 Decl := First (Body_Decls);
3157 while Present (Decl) loop
3158
3159 -- First source declaration terminates the search, because all
3160 -- preconditions must be evaluated prior to it, by definition.
3161
3162 if Comes_From_Source (Decl) then
3163 exit;
3164
3165 -- Certain internally generated object renamings such as those
3166 -- for discriminants and protection fields must be elaborated
3167 -- before the preconditions are evaluated, as their expressions
3168 -- may mention the discriminants. The renamings include those
3169 -- for private components so we need to find the last such.
3170
3171 elsif Is_Prologue_Renaming (Decl) then
3172 while Present (Next (Decl))
3173 and then Is_Prologue_Renaming (Next (Decl))
3174 loop
3175 Next (Decl);
3176 end loop;
3177
3178 Insert_Node := Decl;
3179
3180 -- Otherwise the declaration does not come from source. This
3181 -- also terminates the search, because internal code may raise
3182 -- exceptions which should not preempt the preconditions.
3183
3184 else
3185 exit;
3186 end if;
3187
3188 Next (Decl);
3189 end loop;
3190
3191 -- The processing of preconditions is done in reverse order (body
3192 -- first), because each pragma Check equivalent is inserted at the
3193 -- top of the declarations. This ensures that the final order is
3194 -- consistent with following diagram:
3195
3196 -- <inherited preconditions>
3197 -- <preconditions from spec>
3198 -- <preconditions from body>
3199
3200 Process_Preconditions_For (Body_Id);
3201
3202 -- Move the generated entry-call prologue renamings into the
3203 -- outer declarations for use in the preconditions.
3204
3205 Decl := First (Body_Decls);
3206 while Present (Decl) and then Present (Insert_Node) loop
3207 Next_Decl := Next (Decl);
3208 Remove (Decl);
3209 Prepend_To_Decls (Decl);
3210
3211 exit when Decl = Insert_Node;
3212 Decl := Next_Decl;
3213 end loop;
3214 end if;
3215
3216 if Present (Spec_Id) then
3217 Process_Preconditions_For (Spec_Id);
3218 end if;
3219 end Process_Preconditions;
3220
3221 -- Local variables
3222
3223 Restore_Scope : Boolean := False;
3224 Result : Entity_Id;
3225 Stmts : List_Id := No_List;
3226 Decls : List_Id := New_List;
3227 Subp_Id : Entity_Id;
3228
3229 -- Start of processing for Expand_Subprogram_Contract
3230
3231 begin
3232 -- Obtain the entity of the initial declaration
3233
3234 if Present (Spec_Id) then
3235 Subp_Id := Spec_Id;
3236 else
3237 Subp_Id := Body_Id;
3238 end if;
3239
3240 -- Do not perform expansion activity when it is not needed
3241
3242 if not Expander_Active then
3243 return;
3244
3245 -- GNATprove does not need the executable semantics of a contract
3246
3247 elsif GNATprove_Mode then
3248 return;
3249
3250 -- The contract of a generic subprogram or one declared in a generic
3251 -- context is not expanded, as the corresponding instance will provide
3252 -- the executable semantics of the contract.
3253
3254 elsif Is_Generic_Subprogram (Subp_Id) or else Inside_A_Generic then
3255 return;
3256
3257 -- All subprograms carry a contract, but for some it is not significant
3258 -- and should not be processed. This is a small optimization.
3259
3260 elsif not Has_Significant_Contract (Subp_Id) then
3261 return;
3262
3263 -- The contract of an ignored Ghost subprogram does not need expansion,
3264 -- because the subprogram and all calls to it will be removed.
3265
3266 elsif Is_Ignored_Ghost_Entity (Subp_Id) then
3267 return;
3268
3269 -- No action needed for helpers and indirect-call wrapper built to
3270 -- support class-wide preconditions.
3271
3272 elsif Present (Class_Preconditions_Subprogram (Subp_Id)) then
3273 return;
3274
3275 -- Do not re-expand the same contract. This scenario occurs when a
3276 -- construct is rewritten into something else during its analysis
3277 -- (expression functions for instance).
3278
3279 elsif Has_Expanded_Contract (Subp_Id) then
3280 return;
3281 end if;
3282
3283 -- Prevent multiple expansion attempts of the same contract
3284
3285 Set_Has_Expanded_Contract (Subp_Id);
3286
3287 -- Ensure that the formal parameters are visible when expanding all
3288 -- contract items.
3289
3290 if not In_Open_Scopes (Subp_Id) then
3291 Restore_Scope := True;
3292 Push_Scope (Subp_Id);
3293
3294 if Is_Generic_Subprogram (Subp_Id) then
3295 Install_Generic_Formals (Subp_Id);
3296 else
3297 Install_Formals (Subp_Id);
3298 end if;
3299 end if;
3300
3301 -- The expansion of a subprogram contract involves the creation of Check
3302 -- pragmas to verify the contract assertions of the spec and body in a
3303 -- particular order. The order is as follows:
3304
3305 -- function Original_Code (...) return ... is
3306 -- <prologue renamings>
3307 -- <inherited preconditions>
3308 -- <preconditions from spec>
3309 -- <preconditions from body>
3310 -- <contract case conditions>
3311
3312 -- function _Wrapped_Statements (...) return ... is
3313 -- <source declarations>
3314 -- begin
3315 -- <source statements>
3316 -- end _Wrapped_Statements;
3317
3318 -- begin
3319 -- return Result : constant ... := _Wrapped_Statements do
3320 -- <refined postconditions from body>
3321 -- <postconditions from body>
3322 -- <postconditions from spec>
3323 -- <inherited postconditions>
3324 -- <contract case consequences>
3325 -- <invariant check of function result>
3326 -- <invariant and predicate checks of parameters
3327 -- end return;
3328 -- end Original_Code;
3329
3330 -- Step 1: augment contracts list with postconditions associated with
3331 -- Stable_Properties and Stable_Properties'Class aspects. This must
3332 -- precede Process_Postconditions.
3333
3334 for Class_Present in Boolean loop
3335 Add_Stable_Property_Contracts
3336 (Subp_Id, Class_Present => Class_Present);
3337 end loop;
3338
3339 -- Step 2: Handle all preconditions. This action must come before the
3340 -- processing of pragma Contract_Cases because the pragma prepends items
3341 -- to the body declarations.
3342
3343 Process_Preconditions (Decls);
3344
3345 -- Step 3: Handle all postconditions. This action must come before the
3346 -- processing of pragma Contract_Cases because the pragma appends items
3347 -- to list Stmts.
3348
3349 Process_Postconditions (Stmts);
3350
3351 -- Step 4: Handle pragma Contract_Cases. This action must come before
3352 -- the processing of invariants and predicates because those append
3353 -- items to list Stmts.
3354
3355 Process_Contract_Cases (Stmts, Decls);
3356
3357 -- Step 5: Apply invariant and predicate checks on a function result and
3358 -- all formals. The resulting checks are accumulated in list Stmts.
3359
3360 Add_Invariant_And_Predicate_Checks (Subp_Id, Stmts, Result);
3361
3362 -- Step 6: Construct subprogram _wrapped_statements
3363
3364 -- When no statements are present we still need to insert contract
3365 -- related declarations.
3366
3367 if No (Stmts) then
3368 Prepend_List_To (Declarations (Body_Decl), Decls);
3369
3370 -- Otherwise, we need a wrapper
3371
3372 else
3373 Build_Subprogram_Contract_Wrapper (Body_Id, Stmts, Decls, Result);
3374 end if;
3375
3376 if Restore_Scope then
3377 End_Scope;
3378 end if;
3379 end Expand_Subprogram_Contract;
3380
3381 -------------------------------
3382 -- Freeze_Previous_Contracts --
3383 -------------------------------
3384
3385 procedure Freeze_Previous_Contracts (Body_Decl : Node_Id) is
3386 function Causes_Contract_Freezing (N : Node_Id) return Boolean;
3387 pragma Inline (Causes_Contract_Freezing);
3388 -- Determine whether arbitrary node N causes contract freezing. This is
3389 -- used as an assertion for the current body declaration that caused
3390 -- contract freezing, and as a condition to detect body declaration that
3391 -- already caused contract freezing before.
3392
3393 procedure Freeze_Contracts;
3394 pragma Inline (Freeze_Contracts);
3395 -- Freeze the contracts of all eligible constructs which precede body
3396 -- Body_Decl.
3397
3398 procedure Freeze_Enclosing_Package_Body;
3399 pragma Inline (Freeze_Enclosing_Package_Body);
3400 -- Freeze the contract of the nearest package body (if any) which
3401 -- encloses body Body_Decl.
3402
3403 ------------------------------
3404 -- Causes_Contract_Freezing --
3405 ------------------------------
3406
3407 function Causes_Contract_Freezing (N : Node_Id) return Boolean is
3408 begin
3409 -- The following condition matches guards for calls to
3410 -- Freeze_Previous_Contracts from routines that analyze various body
3411 -- declarations. In particular, it detects expression functions, as
3412 -- described in the call from Analyze_Subprogram_Body_Helper.
3413
3414 return
3415 Comes_From_Source (Original_Node (N))
3416 and then
3417 Nkind (N) in
3418 N_Entry_Body | N_Package_Body | N_Protected_Body |
3419 N_Subprogram_Body | N_Subprogram_Body_Stub | N_Task_Body;
3420 end Causes_Contract_Freezing;
3421
3422 ----------------------
3423 -- Freeze_Contracts --
3424 ----------------------
3425
3426 procedure Freeze_Contracts is
3427 Body_Id : constant Entity_Id := Defining_Entity (Body_Decl);
3428 Decl : Node_Id;
3429
3430 begin
3431 -- Nothing to do when the body which causes freezing does not appear
3432 -- in a declarative list because there cannot possibly be constructs
3433 -- with contracts.
3434
3435 if not Is_List_Member (Body_Decl) then
3436 return;
3437 end if;
3438
3439 -- Inspect the declarations preceding the body, and freeze individual
3440 -- contracts of eligible constructs.
3441
3442 Decl := Prev (Body_Decl);
3443 while Present (Decl) loop
3444
3445 -- Stop the traversal when a preceding construct that causes
3446 -- freezing is encountered as there is no point in refreezing
3447 -- the already frozen constructs.
3448
3449 if Causes_Contract_Freezing (Decl) then
3450 exit;
3451
3452 -- Entry or subprogram declarations
3453
3454 elsif Nkind (Decl) in N_Abstract_Subprogram_Declaration
3455 | N_Entry_Declaration
3456 | N_Generic_Subprogram_Declaration
3457 | N_Subprogram_Declaration
3458 then
3459 Analyze_Entry_Or_Subprogram_Contract
3460 (Subp_Id => Defining_Entity (Decl),
3461 Freeze_Id => Body_Id);
3462
3463 -- Objects
3464
3465 elsif Nkind (Decl) = N_Object_Declaration then
3466 Analyze_Object_Contract
3467 (Obj_Id => Defining_Entity (Decl),
3468 Freeze_Id => Body_Id);
3469
3470 -- Protected units
3471
3472 elsif Nkind (Decl) in N_Protected_Type_Declaration
3473 | N_Single_Protected_Declaration
3474 then
3475 Analyze_Protected_Contract (Defining_Entity (Decl));
3476
3477 -- Subprogram body stubs
3478
3479 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
3480 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
3481
3482 -- Task units
3483
3484 elsif Nkind (Decl) in N_Single_Task_Declaration
3485 | N_Task_Type_Declaration
3486 then
3487 Analyze_Task_Contract (Defining_Entity (Decl));
3488 end if;
3489
3490 if Nkind (Decl) in N_Full_Type_Declaration
3491 | N_Private_Type_Declaration
3492 | N_Task_Type_Declaration
3493 | N_Protected_Type_Declaration
3494 | N_Formal_Type_Declaration
3495 then
3496 Analyze_Type_Contract (Defining_Identifier (Decl));
3497 end if;
3498
3499 Prev (Decl);
3500 end loop;
3501 end Freeze_Contracts;
3502
3503 -----------------------------------
3504 -- Freeze_Enclosing_Package_Body --
3505 -----------------------------------
3506
3507 procedure Freeze_Enclosing_Package_Body is
3508 Orig_Decl : constant Node_Id := Original_Node (Body_Decl);
3509 Par : Node_Id;
3510
3511 begin
3512 -- Climb the parent chain looking for an enclosing package body. Do
3513 -- not use the scope stack, because a body utilizes the entity of its
3514 -- corresponding spec.
3515
3516 Par := Parent (Body_Decl);
3517 while Present (Par) loop
3518 if Nkind (Par) = N_Package_Body then
3519 Analyze_Package_Body_Contract
3520 (Body_Id => Defining_Entity (Par),
3521 Freeze_Id => Defining_Entity (Body_Decl));
3522
3523 exit;
3524
3525 -- Do not look for an enclosing package body when the construct
3526 -- which causes freezing is a body generated for an expression
3527 -- function and it appears within a package spec. This ensures
3528 -- that the traversal will not reach too far up the parent chain
3529 -- and attempt to freeze a package body which must not be frozen.
3530
3531 -- package body Enclosing_Body
3532 -- with Refined_State => (State => Var)
3533 -- is
3534 -- package Nested is
3535 -- type Some_Type is ...;
3536 -- function Cause_Freezing return ...;
3537 -- private
3538 -- function Cause_Freezing is (...);
3539 -- end Nested;
3540 --
3541 -- Var : Nested.Some_Type;
3542
3543 elsif Nkind (Par) = N_Package_Declaration
3544 and then Nkind (Orig_Decl) = N_Expression_Function
3545 then
3546 exit;
3547
3548 -- Prevent the search from going too far
3549
3550 elsif Is_Body_Or_Package_Declaration (Par) then
3551 exit;
3552 end if;
3553
3554 Par := Parent (Par);
3555 end loop;
3556 end Freeze_Enclosing_Package_Body;
3557
3558 -- Local variables
3559
3560 Body_Id : constant Entity_Id := Defining_Entity (Body_Decl);
3561
3562 -- Start of processing for Freeze_Previous_Contracts
3563
3564 begin
3565 pragma Assert (Causes_Contract_Freezing (Body_Decl));
3566
3567 -- A body that is in the process of being inlined appears from source,
3568 -- but carries name _parent. Such a body does not cause freezing of
3569 -- contracts.
3570
3571 if Chars (Body_Id) = Name_uParent then
3572 return;
3573 end if;
3574
3575 Freeze_Enclosing_Package_Body;
3576 Freeze_Contracts;
3577 end Freeze_Previous_Contracts;
3578
3579 ---------------------------------
3580 -- Inherit_Subprogram_Contract --
3581 ---------------------------------
3582
3583 procedure Inherit_Subprogram_Contract
3584 (Subp : Entity_Id;
3585 From_Subp : Entity_Id)
3586 is
3587 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
3588 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
3589 -- Subp's contract.
3590
3591 --------------------
3592 -- Inherit_Pragma --
3593 --------------------
3594
3595 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
3596 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
3597
3598 begin
3599 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
3600 -- chains, therefore the node must be replicated. The new pragma is
3601 -- flagged as inherited for distinction purposes.
3602
3603 if Present (Prag) then
3604 Add_Contract_Item (New_Copy_Tree (Prag), Subp);
3605 end if;
3606 end Inherit_Pragma;
3607
3608 -- Start of processing for Inherit_Subprogram_Contract
3609
3610 begin
3611 -- Inheritance is carried out only when both entities are subprograms
3612 -- with contracts.
3613
3614 if Is_Subprogram_Or_Generic_Subprogram (Subp)
3615 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
3616 and then Present (Contract (From_Subp))
3617 then
3618 Inherit_Pragma (Pragma_Extensions_Visible);
3619 Inherit_Pragma (Pragma_Side_Effects);
3620 end if;
3621 end Inherit_Subprogram_Contract;
3622
3623 -------------------------------------
3624 -- Instantiate_Subprogram_Contract --
3625 -------------------------------------
3626
3627 procedure Instantiate_Subprogram_Contract (Templ : Node_Id; L : List_Id) is
3628 procedure Instantiate_Pragmas (First_Prag : Node_Id);
3629 -- Instantiate all contract-related source pragmas found in the list,
3630 -- starting with pragma First_Prag. Each instantiated pragma is added
3631 -- to list L.
3632
3633 -------------------------
3634 -- Instantiate_Pragmas --
3635 -------------------------
3636
3637 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
3638 Inst_Prag : Node_Id;
3639 Prag : Node_Id;
3640
3641 begin
3642 Prag := First_Prag;
3643 while Present (Prag) loop
3644 if Is_Generic_Contract_Pragma (Prag) then
3645 Inst_Prag :=
3646 Copy_Generic_Node (Prag, Empty, Instantiating => True);
3647
3648 Set_Analyzed (Inst_Prag, False);
3649 Append_To (L, Inst_Prag);
3650 end if;
3651
3652 Prag := Next_Pragma (Prag);
3653 end loop;
3654 end Instantiate_Pragmas;
3655
3656 -- Local variables
3657
3658 Items : constant Node_Id := Contract (Defining_Entity (Templ));
3659
3660 -- Start of processing for Instantiate_Subprogram_Contract
3661
3662 begin
3663 if Present (Items) then
3664 Instantiate_Pragmas (Pre_Post_Conditions (Items));
3665 Instantiate_Pragmas (Contract_Test_Cases (Items));
3666 Instantiate_Pragmas (Classifications (Items));
3667 end if;
3668 end Instantiate_Subprogram_Contract;
3669
3670 --------------------------
3671 -- Is_Prologue_Renaming --
3672 --------------------------
3673
3674 -- This should be turned into a flag and set during the expansion of
3675 -- task and protected types when the renamings get generated ???
3676
3677 function Is_Prologue_Renaming (Decl : Node_Id) return Boolean is
3678 Nam : Node_Id;
3679 Obj : Entity_Id;
3680 Pref : Node_Id;
3681 Sel : Node_Id;
3682
3683 begin
3684 if Nkind (Decl) = N_Object_Renaming_Declaration
3685 and then not Comes_From_Source (Decl)
3686 then
3687 Obj := Defining_Entity (Decl);
3688 Nam := Name (Decl);
3689
3690 if Nkind (Nam) = N_Selected_Component then
3691 -- Analyze the renaming declaration so we can further examine it
3692
3693 if not Analyzed (Decl) then
3694 Analyze (Decl);
3695 end if;
3696
3697 Pref := Prefix (Nam);
3698 Sel := Selector_Name (Nam);
3699
3700 -- A discriminant renaming appears as
3701 -- Discr : constant ... := Prefix.Discr;
3702
3703 if Ekind (Obj) = E_Constant
3704 and then Is_Entity_Name (Sel)
3705 and then Present (Entity (Sel))
3706 and then Ekind (Entity (Sel)) = E_Discriminant
3707 then
3708 return True;
3709
3710 -- A protection field renaming appears as
3711 -- Prot : ... := _object._object;
3712
3713 -- A renamed private component is just a component of
3714 -- _object, with an arbitrary name.
3715
3716 elsif Ekind (Obj) in E_Variable | E_Constant
3717 and then Nkind (Pref) = N_Identifier
3718 and then Chars (Pref) = Name_uObject
3719 and then Nkind (Sel) = N_Identifier
3720 then
3721 return True;
3722 end if;
3723 end if;
3724 end if;
3725
3726 return False;
3727 end Is_Prologue_Renaming;
3728
3729 -----------------------------------
3730 -- Make_Class_Precondition_Subps --
3731 -----------------------------------
3732
3733 procedure Make_Class_Precondition_Subps
3734 (Subp_Id : Entity_Id;
3735 Late_Overriding : Boolean := False)
3736 is
3737 Loc : constant Source_Ptr := Sloc (Subp_Id);
3738 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
3739
3740 procedure Add_Indirect_Call_Wrapper;
3741 -- Build the indirect-call wrapper and append it to the freezing actions
3742 -- of Tagged_Type.
3743
3744 procedure Add_Call_Helper
3745 (Helper_Id : Entity_Id;
3746 Is_Dynamic : Boolean);
3747 -- Factorizes code for building a call helper with the given identifier
3748 -- and append it to the freezing actions of Tagged_Type. Is_Dynamic
3749 -- controls building the static or dynamic version of the helper.
3750
3751 function Build_Unique_Name (Suffix : String) return Name_Id;
3752 -- Build an unique new name adding suffix to Subp_Id name (plus its
3753 -- homonym number for values bigger than 1).
3754
3755 -------------------------------
3756 -- Add_Indirect_Call_Wrapper --
3757 -------------------------------
3758
3759 procedure Add_Indirect_Call_Wrapper is
3760
3761 function Build_ICW_Body return Node_Id;
3762 -- Build the body of the indirect call wrapper
3763
3764 function Build_ICW_Decl return Node_Id;
3765 -- Build the declaration of the indirect call wrapper
3766
3767 --------------------
3768 -- Build_ICW_Body --
3769 --------------------
3770
3771 function Build_ICW_Body return Node_Id is
3772 ICW_Id : constant Entity_Id := Indirect_Call_Wrapper (Subp_Id);
3773 Spec : constant Node_Id := Parent (ICW_Id);
3774 Body_Spec : Node_Id;
3775 Call : Node_Id;
3776 ICW_Body : Node_Id;
3777
3778 begin
3779 Body_Spec := Copy_Subprogram_Spec (Spec);
3780
3781 -- Build call to wrapped subprogram
3782
3783 declare
3784 Actuals : constant List_Id := Empty_List;
3785 Formal_Spec : Entity_Id :=
3786 First (Parameter_Specifications (Spec));
3787 begin
3788 -- Build parameter association & call
3789
3790 while Present (Formal_Spec) loop
3791 Append_To (Actuals,
3792 New_Occurrence_Of
3793 (Defining_Identifier (Formal_Spec), Loc));
3794 Next (Formal_Spec);
3795 end loop;
3796
3797 if Ekind (ICW_Id) = E_Procedure then
3798 Call :=
3799 Make_Procedure_Call_Statement (Loc,
3800 Name => New_Occurrence_Of (Subp_Id, Loc),
3801 Parameter_Associations => Actuals);
3802 else
3803 Call :=
3804 Make_Simple_Return_Statement (Loc,
3805 Expression =>
3806 Make_Function_Call (Loc,
3807 Name => New_Occurrence_Of (Subp_Id, Loc),
3808 Parameter_Associations => Actuals));
3809 end if;
3810 end;
3811
3812 ICW_Body :=
3813 Make_Subprogram_Body (Loc,
3814 Specification => Body_Spec,
3815 Declarations => New_List,
3816 Handled_Statement_Sequence =>
3817 Make_Handled_Sequence_Of_Statements (Loc,
3818 Statements => New_List (Call)));
3819
3820 -- The new operation is internal and overriding indicators do not
3821 -- apply.
3822
3823 Set_Must_Override (Body_Spec, False);
3824
3825 return ICW_Body;
3826 end Build_ICW_Body;
3827
3828 --------------------
3829 -- Build_ICW_Decl --
3830 --------------------
3831
3832 function Build_ICW_Decl return Node_Id is
3833 ICW_Id : constant Entity_Id :=
3834 Make_Defining_Identifier (Loc,
3835 Build_Unique_Name (Suffix => "ICW"));
3836 Decl : Node_Id;
3837 Spec : Node_Id;
3838
3839 begin
3840 Spec := Copy_Subprogram_Spec (Parent (Subp_Id));
3841 Set_Must_Override (Spec, False);
3842 Set_Must_Not_Override (Spec, False);
3843 Set_Defining_Unit_Name (Spec, ICW_Id);
3844 Mutate_Ekind (ICW_Id, Ekind (Subp_Id));
3845 Set_Is_Public (ICW_Id);
3846
3847 -- The indirect call wrapper is commonly used for indirect calls
3848 -- but inlined for direct calls performed from the DTW.
3849
3850 Set_Is_Inlined (ICW_Id);
3851
3852 if Nkind (Spec) = N_Procedure_Specification then
3853 Set_Null_Present (Spec, False);
3854 end if;
3855
3856 Decl := Make_Subprogram_Declaration (Loc, Spec);
3857
3858 -- Link original subprogram to indirect wrapper and vice versa
3859
3860 Set_Indirect_Call_Wrapper (Subp_Id, ICW_Id);
3861 Set_Class_Preconditions_Subprogram (ICW_Id, Subp_Id);
3862
3863 -- Inherit debug info flag to allow debugging the wrapper
3864
3865 if Needs_Debug_Info (Subp_Id) then
3866 Set_Debug_Info_Needed (ICW_Id);
3867 end if;
3868
3869 return Decl;
3870 end Build_ICW_Decl;
3871
3872 -- Local Variables
3873
3874 ICW_Body : Node_Id;
3875 ICW_Decl : Node_Id;
3876
3877 -- Start of processing for Add_Indirect_Call_Wrapper
3878
3879 begin
3880 pragma Assert (No (Indirect_Call_Wrapper (Subp_Id)));
3881
3882 ICW_Decl := Build_ICW_Decl;
3883
3884 Append_Freeze_Action (Tagged_Type, ICW_Decl);
3885 Analyze (ICW_Decl);
3886
3887 ICW_Body := Build_ICW_Body;
3888 Append_Freeze_Action (Tagged_Type, ICW_Body);
3889
3890 -- We cannot defer the analysis of this ICW wrapper when it is
3891 -- built as a consequence of building its partner DTW wrapper
3892 -- at the freezing point of the tagged type.
3893
3894 if Is_Dispatch_Table_Wrapper (Subp_Id) then
3895 Analyze (ICW_Body);
3896 end if;
3897 end Add_Indirect_Call_Wrapper;
3898
3899 ---------------------
3900 -- Add_Call_Helper --
3901 ---------------------
3902
3903 procedure Add_Call_Helper
3904 (Helper_Id : Entity_Id;
3905 Is_Dynamic : Boolean)
3906 is
3907 function Build_Call_Helper_Body return Node_Id;
3908 -- Build the body of a call helper
3909
3910 function Build_Call_Helper_Decl return Node_Id;
3911 -- Build the declaration of a call helper
3912
3913 function Build_Call_Helper_Spec (Spec_Id : Entity_Id) return Node_Id;
3914 -- Build the specification of the helper
3915
3916 ----------------------------
3917 -- Build_Call_Helper_Body --
3918 ----------------------------
3919
3920 function Build_Call_Helper_Body return Node_Id is
3921
3922 function Copy_And_Update_References
3923 (Expr : Node_Id) return Node_Id;
3924 -- Copy Expr updating references to formals of Helper_Id; update
3925 -- also references to loop identifiers of quantified expressions.
3926
3927 --------------------------------
3928 -- Copy_And_Update_References --
3929 --------------------------------
3930
3931 function Copy_And_Update_References
3932 (Expr : Node_Id) return Node_Id
3933 is
3934 Assoc_List : constant Elist_Id := New_Elmt_List;
3935
3936 procedure Map_Quantified_Expression_Loop_Identifiers;
3937 -- Traverse Expr and append to Assoc_List the mapping of loop
3938 -- identifers of quantified expressions with its new copy.
3939
3940 ------------------------------------------------
3941 -- Map_Quantified_Expression_Loop_Identifiers --
3942 ------------------------------------------------
3943
3944 procedure Map_Quantified_Expression_Loop_Identifiers is
3945 function Map_Loop_Param (N : Node_Id) return Traverse_Result;
3946 -- Append to Assoc_List the mapping of loop identifers of
3947 -- quantified expressions with its new copy.
3948
3949 --------------------
3950 -- Map_Loop_Param --
3951 --------------------
3952
3953 function Map_Loop_Param (N : Node_Id) return Traverse_Result
3954 is
3955 begin
3956 if Nkind (N) = N_Loop_Parameter_Specification
3957 and then Nkind (Parent (N)) = N_Quantified_Expression
3958 then
3959 declare
3960 Def_Id : constant Entity_Id :=
3961 Defining_Identifier (N);
3962 begin
3963 Append_Elmt (Def_Id, Assoc_List);
3964 Append_Elmt (New_Copy (Def_Id), Assoc_List);
3965 end;
3966 end if;
3967
3968 return OK;
3969 end Map_Loop_Param;
3970
3971 procedure Map_Quantified_Expressions is
3972 new Traverse_Proc (Map_Loop_Param);
3973
3974 begin
3975 Map_Quantified_Expressions (Expr);
3976 end Map_Quantified_Expression_Loop_Identifiers;
3977
3978 -- Local variables
3979
3980 Subp_Formal_Id : Entity_Id := First_Formal (Subp_Id);
3981 Helper_Formal_Id : Entity_Id := First_Formal (Helper_Id);
3982
3983 -- Start of processing for Copy_And_Update_References
3984
3985 begin
3986 while Present (Subp_Formal_Id) loop
3987 Append_Elmt (Subp_Formal_Id, Assoc_List);
3988 Append_Elmt (Helper_Formal_Id, Assoc_List);
3989
3990 Next_Formal (Subp_Formal_Id);
3991 Next_Formal (Helper_Formal_Id);
3992 end loop;
3993
3994 Map_Quantified_Expression_Loop_Identifiers;
3995
3996 return New_Copy_Tree (Expr, Map => Assoc_List);
3997 end Copy_And_Update_References;
3998
3999 -- Local variables
4000
4001 Helper_Decl : constant Node_Id := Parent (Parent (Helper_Id));
4002 Body_Id : Entity_Id;
4003 Body_Spec : Node_Id;
4004 Body_Stmts : Node_Id;
4005 Helper_Body : Node_Id;
4006 Return_Expr : Node_Id;
4007
4008 -- Start of processing for Build_Call_Helper_Body
4009
4010 begin
4011 pragma Assert (Analyzed (Unit_Declaration_Node (Helper_Id)));
4012 pragma Assert (No (Corresponding_Body (Helper_Decl)));
4013
4014 Body_Id := Make_Defining_Identifier (Loc, Chars (Helper_Id));
4015 Body_Spec := Build_Call_Helper_Spec (Body_Id);
4016
4017 Set_Corresponding_Body (Helper_Decl, Body_Id);
4018 Set_Must_Override (Body_Spec, False);
4019
4020 if Present (Class_Preconditions (Subp_Id))
4021 -- Evaluate the expression if we are building a dynamic helper
4022 -- or we are building a static helper for a non-abstract tagged
4023 -- type; for abstract tagged types the helper just returns True
4024 -- since it is called by the indirect call wrapper (ICW).
4025 and then
4026 (Is_Dynamic
4027 or else
4028 not Is_Abstract_Type (Find_Dispatching_Type (Subp_Id)))
4029 then
4030 Return_Expr :=
4031 Copy_And_Update_References (Class_Preconditions (Subp_Id));
4032
4033 -- When the subprogram is compiled with assertions disabled the
4034 -- helper just returns True; done to avoid reporting errors at
4035 -- link time since a unit may be compiled with assertions disabled
4036 -- and another (which depends on it) compiled with assertions
4037 -- enabled.
4038
4039 else
4040 pragma Assert (Present (Ignored_Class_Preconditions (Subp_Id))
4041 or else Is_Abstract_Type (Find_Dispatching_Type (Subp_Id)));
4042 Return_Expr := New_Occurrence_Of (Standard_True, Loc);
4043 end if;
4044
4045 Body_Stmts :=
4046 Make_Handled_Sequence_Of_Statements (Loc,
4047 Statements => New_List (
4048 Make_Simple_Return_Statement (Loc, Return_Expr)));
4049
4050 Helper_Body :=
4051 Make_Subprogram_Body (Loc,
4052 Specification => Body_Spec,
4053 Declarations => New_List,
4054 Handled_Statement_Sequence => Body_Stmts);
4055
4056 return Helper_Body;
4057 end Build_Call_Helper_Body;
4058
4059 ----------------------------
4060 -- Build_Call_Helper_Decl --
4061 ----------------------------
4062
4063 function Build_Call_Helper_Decl return Node_Id is
4064 Decl : Node_Id;
4065 Spec : Node_Id;
4066
4067 begin
4068 Spec := Build_Call_Helper_Spec (Helper_Id);
4069 Set_Must_Override (Spec, False);
4070 Set_Must_Not_Override (Spec, False);
4071 Set_Is_Inlined (Helper_Id);
4072 Set_Is_Public (Helper_Id);
4073
4074 Decl := Make_Subprogram_Declaration (Loc, Spec);
4075
4076 -- Inherit debug info flag from Subp_Id to Helper_Id to allow
4077 -- debugging of the helper subprogram.
4078
4079 if Needs_Debug_Info (Subp_Id) then
4080 Set_Debug_Info_Needed (Helper_Id);
4081 end if;
4082
4083 return Decl;
4084 end Build_Call_Helper_Decl;
4085
4086 ----------------------------
4087 -- Build_Call_Helper_Spec --
4088 ----------------------------
4089
4090 function Build_Call_Helper_Spec (Spec_Id : Entity_Id) return Node_Id
4091 is
4092 Spec : constant Node_Id := Parent (Subp_Id);
4093 Def_Id : constant Node_Id := Defining_Unit_Name (Spec);
4094 Formal : Entity_Id;
4095 Func_Formals : constant List_Id := New_List;
4096 P_Spec : constant List_Id := Parameter_Specifications (Spec);
4097 Par_Formal : Node_Id;
4098 Param : Node_Id;
4099 Param_Type : Node_Id;
4100
4101 begin
4102 -- Create a list of formal parameters with the same types as the
4103 -- original subprogram but changing the controlling formal.
4104
4105 Param := First (P_Spec);
4106 Formal := First_Formal (Def_Id);
4107 while Present (Formal) loop
4108 Par_Formal := Parent (Formal);
4109
4110 if Is_Dynamic and then Is_Controlling_Formal (Formal) then
4111 if Nkind (Parameter_Type (Par_Formal))
4112 = N_Access_Definition
4113 then
4114 Param_Type :=
4115 Copy_Separate_Tree (Parameter_Type (Par_Formal));
4116 Rewrite (Subtype_Mark (Param_Type),
4117 Make_Attribute_Reference (Loc,
4118 Prefix => Relocate_Node (Subtype_Mark (Param_Type)),
4119 Attribute_Name => Name_Class));
4120
4121 else
4122 Param_Type :=
4123 Make_Attribute_Reference (Loc,
4124 Prefix => New_Occurrence_Of (Etype (Formal), Loc),
4125 Attribute_Name => Name_Class);
4126 end if;
4127 else
4128 Param_Type := New_Occurrence_Of (Etype (Formal), Loc);
4129 end if;
4130
4131 Append_To (Func_Formals,
4132 Make_Parameter_Specification (Loc,
4133 Defining_Identifier =>
4134 Make_Defining_Identifier (Loc, Chars (Formal)),
4135 In_Present => In_Present (Par_Formal),
4136 Out_Present => Out_Present (Par_Formal),
4137 Null_Exclusion_Present => Null_Exclusion_Present
4138 (Par_Formal),
4139 Parameter_Type => Param_Type));
4140
4141 Next (Param);
4142 Next_Formal (Formal);
4143 end loop;
4144
4145 return
4146 Make_Function_Specification (Loc,
4147 Defining_Unit_Name => Spec_Id,
4148 Parameter_Specifications => Func_Formals,
4149 Result_Definition =>
4150 New_Occurrence_Of (Standard_Boolean, Loc));
4151 end Build_Call_Helper_Spec;
4152
4153 -- Local variables
4154
4155 Helper_Body : Node_Id;
4156 Helper_Decl : Node_Id;
4157
4158 -- Start of processing for Add_Call_Helper
4159
4160 begin
4161 Helper_Decl := Build_Call_Helper_Decl;
4162 Mutate_Ekind (Helper_Id, Ekind (Subp_Id));
4163
4164 -- Add the helper to the freezing actions of the class-wide type
4165
4166 Append_Freeze_Action (Class_Wide_Type (Tagged_Type), Helper_Decl);
4167 Analyze (Helper_Decl);
4168
4169 Helper_Body := Build_Call_Helper_Body;
4170 Append_Freeze_Action (Class_Wide_Type (Tagged_Type), Helper_Body);
4171
4172 -- If this helper is built as part of building the DTW at the
4173 -- freezing point of its tagged type then we cannot defer
4174 -- its analysis.
4175
4176 if Late_Overriding then
4177 pragma Assert (Is_Dispatch_Table_Wrapper (Subp_Id));
4178 Analyze (Helper_Body);
4179 end if;
4180 end Add_Call_Helper;
4181
4182 -----------------------
4183 -- Build_Unique_Name --
4184 -----------------------
4185
4186 function Build_Unique_Name (Suffix : String) return Name_Id is
4187 begin
4188 -- Append the homonym number. Strip the leading space character in
4189 -- the image of natural numbers. Also do not add the homonym value
4190 -- of 1.
4191
4192 if Has_Homonym (Subp_Id) and then Homonym_Number (Subp_Id) > 1 then
4193 declare
4194 S : constant String := Homonym_Number (Subp_Id)'Img;
4195
4196 begin
4197 return New_External_Name (Chars (Subp_Id),
4198 Suffix => Suffix & "_" & S (2 .. S'Last));
4199 end;
4200 end if;
4201
4202 return New_External_Name (Chars (Subp_Id), Suffix);
4203 end Build_Unique_Name;
4204
4205 -- Local variables
4206
4207 Helper_Id : Entity_Id;
4208
4209 -- Start of processing for Make_Class_Precondition_Subps
4210
4211 begin
4212 if Present (Class_Preconditions (Subp_Id))
4213 or Present (Ignored_Class_Preconditions (Subp_Id))
4214 then
4215 pragma Assert
4216 (Comes_From_Source (Subp_Id)
4217 or else Is_Dispatch_Table_Wrapper (Subp_Id));
4218
4219 if No (Dynamic_Call_Helper (Subp_Id)) then
4220
4221 -- Build and add to the freezing actions of Tagged_Type its
4222 -- dynamic-call helper.
4223
4224 Helper_Id :=
4225 Make_Defining_Identifier (Loc,
4226 Build_Unique_Name (Suffix => "DP"));
4227 Add_Call_Helper (Helper_Id, Is_Dynamic => True);
4228
4229 -- Link original subprogram to helper and vice versa
4230
4231 Set_Dynamic_Call_Helper (Subp_Id, Helper_Id);
4232 Set_Class_Preconditions_Subprogram (Helper_Id, Subp_Id);
4233 end if;
4234
4235 if not Is_Abstract_Subprogram (Subp_Id)
4236 and then No (Static_Call_Helper (Subp_Id))
4237 then
4238 -- Build and add to the freezing actions of Tagged_Type its
4239 -- static-call helper.
4240
4241 Helper_Id :=
4242 Make_Defining_Identifier (Loc,
4243 Build_Unique_Name (Suffix => "SP"));
4244
4245 Add_Call_Helper (Helper_Id, Is_Dynamic => False);
4246
4247 -- Link original subprogram to helper and vice versa
4248
4249 Set_Static_Call_Helper (Subp_Id, Helper_Id);
4250 Set_Class_Preconditions_Subprogram (Helper_Id, Subp_Id);
4251
4252 -- Build and add to the freezing actions of Tagged_Type the
4253 -- indirect-call wrapper.
4254
4255 Add_Indirect_Call_Wrapper;
4256 end if;
4257 end if;
4258 end Make_Class_Precondition_Subps;
4259
4260 ----------------------------------------------
4261 -- Process_Class_Conditions_At_Freeze_Point --
4262 ----------------------------------------------
4263
4264 procedure Process_Class_Conditions_At_Freeze_Point (Typ : Entity_Id) is
4265
4266 procedure Check_Class_Conditions (Spec_Id : Entity_Id);
4267 -- Check class-wide pre/postconditions of Spec_Id
4268
4269 function Has_Class_Postconditions_Subprogram
4270 (Spec_Id : Entity_Id) return Boolean;
4271 -- Return True if Spec_Id has (or inherits) a postconditions subprogram.
4272
4273 function Has_Class_Preconditions_Subprogram
4274 (Spec_Id : Entity_Id) return Boolean;
4275 -- Return True if Spec_Id has (or inherits) a preconditions subprogram.
4276
4277 ----------------------------
4278 -- Check_Class_Conditions --
4279 ----------------------------
4280
4281 procedure Check_Class_Conditions (Spec_Id : Entity_Id) is
4282 Par_Subp : Entity_Id;
4283
4284 begin
4285 for Kind in Condition_Kind loop
4286 Par_Subp := Nearest_Class_Condition_Subprogram (Kind, Spec_Id);
4287
4288 if Present (Par_Subp) then
4289 Check_Class_Condition
4290 (Cond => Class_Condition (Kind, Par_Subp),
4291 Subp => Spec_Id,
4292 Par_Subp => Par_Subp,
4293 Is_Precondition => Kind in Ignored_Class_Precondition
4294 | Class_Precondition);
4295 end if;
4296 end loop;
4297 end Check_Class_Conditions;
4298
4299 -----------------------------------------
4300 -- Has_Class_Postconditions_Subprogram --
4301 -----------------------------------------
4302
4303 function Has_Class_Postconditions_Subprogram
4304 (Spec_Id : Entity_Id) return Boolean is
4305 begin
4306 return
4307 Present (Nearest_Class_Condition_Subprogram
4308 (Spec_Id => Spec_Id,
4309 Kind => Class_Postcondition))
4310 or else
4311 Present (Nearest_Class_Condition_Subprogram
4312 (Spec_Id => Spec_Id,
4313 Kind => Ignored_Class_Postcondition));
4314 end Has_Class_Postconditions_Subprogram;
4315
4316 ----------------------------------------
4317 -- Has_Class_Preconditions_Subprogram --
4318 ----------------------------------------
4319
4320 function Has_Class_Preconditions_Subprogram
4321 (Spec_Id : Entity_Id) return Boolean is
4322 begin
4323 return
4324 Present (Nearest_Class_Condition_Subprogram
4325 (Spec_Id => Spec_Id,
4326 Kind => Class_Precondition))
4327 or else
4328 Present (Nearest_Class_Condition_Subprogram
4329 (Spec_Id => Spec_Id,
4330 Kind => Ignored_Class_Precondition));
4331 end Has_Class_Preconditions_Subprogram;
4332
4333 -- Local variables
4334
4335 Prim_Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Typ));
4336 Prim : Entity_Id;
4337
4338 -- Start of processing for Process_Class_Conditions_At_Freeze_Point
4339
4340 begin
4341 while Present (Prim_Elmt) loop
4342 Prim := Node (Prim_Elmt);
4343
4344 if Has_Class_Preconditions_Subprogram (Prim)
4345 or else Has_Class_Postconditions_Subprogram (Prim)
4346 then
4347 if Comes_From_Source (Prim) then
4348 if Has_Significant_Contract (Prim) then
4349 Merge_Class_Conditions (Prim);
4350 end if;
4351
4352 -- Handle wrapper of protected operation
4353
4354 elsif Is_Primitive_Wrapper (Prim) then
4355 Merge_Class_Conditions (Prim);
4356
4357 -- Check inherited class-wide conditions, excluding internal
4358 -- entities built for mapping of interface primitives.
4359
4360 elsif Is_Derived_Type (Typ)
4361 and then Present (Alias (Prim))
4362 and then No (Interface_Alias (Prim))
4363 then
4364 Check_Class_Conditions (Prim);
4365 end if;
4366 end if;
4367
4368 Next_Elmt (Prim_Elmt);
4369 end loop;
4370 end Process_Class_Conditions_At_Freeze_Point;
4371
4372 ----------------------------
4373 -- Merge_Class_Conditions --
4374 ----------------------------
4375
4376 procedure Merge_Class_Conditions (Spec_Id : Entity_Id) is
4377
4378 procedure Process_Inherited_Conditions (Kind : Condition_Kind);
4379 -- Collect all inherited class-wide conditions of Spec_Id and merge
4380 -- them into one big condition.
4381
4382 ----------------------------------
4383 -- Process_Inherited_Conditions --
4384 ----------------------------------
4385
4386 procedure Process_Inherited_Conditions (Kind : Condition_Kind) is
4387 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (Spec_Id);
4388 Subps : constant Subprogram_List := Inherited_Subprograms (Spec_Id);
4389 Seen : Subprogram_List (Subps'Range) := (others => Empty);
4390
4391 function Inherit_Condition
4392 (Par_Subp : Entity_Id) return Node_Id;
4393 -- Inherit the class-wide condition from Par_Subp. Simply makes
4394 -- a copy of the condition in preparation for later mapping of
4395 -- referenced formals and functions by Build_Class_Wide_Expression.
4396
4397 procedure Merge_Conditions (From : Node_Id; Into : Node_Id);
4398 -- Merge two class-wide preconditions or postconditions (the former
4399 -- are merged using "or else", and the latter are merged using "and-
4400 -- then"). The changes are accumulated in parameter Into.
4401
4402 function Seen_Subp (Id : Entity_Id) return Boolean;
4403 -- Return True if the contract of subprogram Id has been processed
4404
4405 -----------------------
4406 -- Inherit_Condition --
4407 -----------------------
4408
4409 function Inherit_Condition
4410 (Par_Subp : Entity_Id) return Node_Id is
4411 begin
4412 return
4413 New_Copy_Tree
4414 (Source => Class_Condition (Kind, Par_Subp));
4415 end Inherit_Condition;
4416
4417 ----------------------
4418 -- Merge_Conditions --
4419 ----------------------
4420
4421 procedure Merge_Conditions (From : Node_Id; Into : Node_Id) is
4422 function Expression_Arg (Expr : Node_Id) return Node_Id;
4423 -- Return the boolean expression argument of a condition while
4424 -- updating its parentheses count for the subsequent merge.
4425
4426 --------------------
4427 -- Expression_Arg --
4428 --------------------
4429
4430 function Expression_Arg (Expr : Node_Id) return Node_Id is
4431 begin
4432 if Paren_Count (Expr) = 0 then
4433 Set_Paren_Count (Expr, 1);
4434 end if;
4435
4436 return Expr;
4437 end Expression_Arg;
4438
4439 -- Local variables
4440
4441 From_Expr : constant Node_Id := Expression_Arg (From);
4442 Into_Expr : constant Node_Id := Expression_Arg (Into);
4443 Loc : constant Source_Ptr := Sloc (Into);
4444
4445 -- Start of processing for Merge_Conditions
4446
4447 begin
4448 case Kind is
4449
4450 -- Merge the two preconditions by "or else"-ing them
4451
4452 when Ignored_Class_Precondition
4453 | Class_Precondition
4454 =>
4455 Rewrite (Into_Expr,
4456 Make_Or_Else (Loc,
4457 Right_Opnd => Relocate_Node (Into_Expr),
4458 Left_Opnd => From_Expr));
4459
4460 -- Merge the two postconditions by "and then"-ing them
4461
4462 when Ignored_Class_Postcondition
4463 | Class_Postcondition
4464 =>
4465 Rewrite (Into_Expr,
4466 Make_And_Then (Loc,
4467 Right_Opnd => Relocate_Node (Into_Expr),
4468 Left_Opnd => From_Expr));
4469 end case;
4470 end Merge_Conditions;
4471
4472 ---------------
4473 -- Seen_Subp --
4474 ---------------
4475
4476 function Seen_Subp (Id : Entity_Id) return Boolean is
4477 begin
4478 for Index in Seen'Range loop
4479 if Seen (Index) = Id then
4480 return True;
4481 end if;
4482 end loop;
4483
4484 return False;
4485 end Seen_Subp;
4486
4487 -- Local variables
4488
4489 Class_Cond : Node_Id;
4490 Cond : Node_Id;
4491 Subp_Id : Entity_Id;
4492 Par_Prim : Entity_Id := Empty;
4493 Par_Iface_Prims : Elist_Id := No_Elist;
4494
4495 -- Start of processing for Process_Inherited_Conditions
4496
4497 begin
4498 Class_Cond := Class_Condition (Kind, Spec_Id);
4499
4500 -- Process parent primitives looking for nearest ancestor with
4501 -- class-wide conditions.
4502
4503 for Index in Subps'Range loop
4504 Subp_Id := Subps (Index);
4505
4506 if No (Par_Prim)
4507 and then Is_Ancestor (Find_Dispatching_Type (Subp_Id), Tag_Typ)
4508 then
4509 if Present (Alias (Subp_Id)) then
4510 Subp_Id := Ultimate_Alias (Subp_Id);
4511 end if;
4512
4513 -- Wrappers of class-wide pre/postconditions reference the
4514 -- parent primitive that has the inherited contract and help
4515 -- us to climb fast.
4516
4517 if Is_LSP_Wrapper (Subp_Id) then
4518 Subp_Id := LSP_Subprogram (Subp_Id);
4519 end if;
4520
4521 if not Seen_Subp (Subp_Id)
4522 and then Present (Class_Condition (Kind, Subp_Id))
4523 then
4524 Seen (Index) := Subp_Id;
4525 Par_Prim := Subp_Id;
4526 Par_Iface_Prims := Covered_Interface_Primitives (Par_Prim);
4527
4528 Cond := Inherit_Condition (Par_Subp => Subp_Id);
4529
4530 if Present (Class_Cond) then
4531 Merge_Conditions (Cond, Class_Cond);
4532 else
4533 Class_Cond := Cond;
4534 end if;
4535
4536 Check_Class_Condition
4537 (Cond => Class_Cond,
4538 Subp => Spec_Id,
4539 Par_Subp => Subp_Id,
4540 Is_Precondition => Kind in Ignored_Class_Precondition
4541 | Class_Precondition);
4542 Build_Class_Wide_Expression
4543 (Pragma_Or_Expr => Class_Cond,
4544 Subp => Spec_Id,
4545 Par_Subp => Subp_Id,
4546 Adjust_Sloc => False);
4547
4548 -- We are done as soon as we process the nearest ancestor
4549
4550 exit;
4551 end if;
4552 end if;
4553 end loop;
4554
4555 -- Process the contract of interface primitives not covered by
4556 -- the nearest ancestor.
4557
4558 for Index in Subps'Range loop
4559 Subp_Id := Subps (Index);
4560
4561 if Is_Interface (Find_Dispatching_Type (Subp_Id)) then
4562 if Present (Alias (Subp_Id)) then
4563 Subp_Id := Ultimate_Alias (Subp_Id);
4564 end if;
4565
4566 if not Seen_Subp (Subp_Id)
4567 and then Present (Class_Condition (Kind, Subp_Id))
4568 and then not Contains (Par_Iface_Prims, Subp_Id)
4569 then
4570 Seen (Index) := Subp_Id;
4571
4572 Cond := Inherit_Condition (Par_Subp => Subp_Id);
4573
4574 Check_Class_Condition
4575 (Cond => Cond,
4576 Subp => Spec_Id,
4577 Par_Subp => Subp_Id,
4578 Is_Precondition => Kind in Ignored_Class_Precondition
4579 | Class_Precondition);
4580 Build_Class_Wide_Expression
4581 (Pragma_Or_Expr => Cond,
4582 Subp => Spec_Id,
4583 Par_Subp => Subp_Id,
4584 Adjust_Sloc => False);
4585
4586 if Present (Class_Cond) then
4587 Merge_Conditions (Cond, Class_Cond);
4588 else
4589 Class_Cond := Cond;
4590 end if;
4591 end if;
4592 end if;
4593 end loop;
4594
4595 Set_Class_Condition (Kind, Spec_Id, Class_Cond);
4596 end Process_Inherited_Conditions;
4597
4598 -- Local variables
4599
4600 Cond : Node_Id;
4601
4602 -- Start of processing for Merge_Class_Conditions
4603
4604 begin
4605 for Kind in Condition_Kind loop
4606 Cond := Class_Condition (Kind, Spec_Id);
4607
4608 -- If this subprogram has class-wide conditions then preanalyze
4609 -- them before processing inherited conditions since conditions
4610 -- are checked and merged from right to left.
4611
4612 if Present (Cond) then
4613 Preanalyze_Condition (Spec_Id, Cond);
4614 end if;
4615
4616 Process_Inherited_Conditions (Kind);
4617
4618 -- Preanalyze merged inherited conditions
4619
4620 if Cond /= Class_Condition (Kind, Spec_Id) then
4621 Preanalyze_Condition (Spec_Id,
4622 Class_Condition (Kind, Spec_Id));
4623 end if;
4624 end loop;
4625 end Merge_Class_Conditions;
4626
4627 ---------------------------------
4628 -- Preanalyze_Class_Conditions --
4629 ---------------------------------
4630
4631 procedure Preanalyze_Class_Conditions (Spec_Id : Entity_Id) is
4632 Cond : Node_Id;
4633
4634 begin
4635 for Kind in Condition_Kind loop
4636 Cond := Class_Condition (Kind, Spec_Id);
4637
4638 if Present (Cond) then
4639 Preanalyze_Condition (Spec_Id, Cond);
4640 end if;
4641 end loop;
4642 end Preanalyze_Class_Conditions;
4643
4644 --------------------------
4645 -- Preanalyze_Condition --
4646 --------------------------
4647
4648 procedure Preanalyze_Condition
4649 (Subp : Entity_Id;
4650 Expr : Node_Id)
4651 is
4652 procedure Clear_Unset_References;
4653 -- Clear unset references on formals of Subp since preanalysis
4654 -- occurs in a place unrelated to the actual code.
4655
4656 procedure Remove_Controlling_Arguments;
4657 -- Traverse Expr and clear the Controlling_Argument of calls to
4658 -- nonabstract functions.
4659
4660 procedure Restore_Original_Selected_Component;
4661 -- Traverse Expr searching for dispatching calls to functions whose
4662 -- original node was a selected component, and replace them with
4663 -- their original node.
4664
4665 ----------------------------
4666 -- Clear_Unset_References --
4667 ----------------------------
4668
4669 procedure Clear_Unset_References is
4670 F : Entity_Id := First_Formal (Subp);
4671
4672 begin
4673 while Present (F) loop
4674 Set_Unset_Reference (F, Empty);
4675 Next_Formal (F);
4676 end loop;
4677 end Clear_Unset_References;
4678
4679 ----------------------------------
4680 -- Remove_Controlling_Arguments --
4681 ----------------------------------
4682
4683 procedure Remove_Controlling_Arguments is
4684 function Remove_Ctrl_Arg (N : Node_Id) return Traverse_Result;
4685 -- Reset the Controlling_Argument of calls to nonabstract
4686 -- function calls.
4687
4688 ---------------------
4689 -- Remove_Ctrl_Arg --
4690 ---------------------
4691
4692 function Remove_Ctrl_Arg (N : Node_Id) return Traverse_Result is
4693 begin
4694 if Nkind (N) = N_Function_Call
4695 and then Present (Controlling_Argument (N))
4696 and then not Is_Abstract_Subprogram (Entity (Name (N)))
4697 then
4698 Set_Controlling_Argument (N, Empty);
4699 end if;
4700
4701 return OK;
4702 end Remove_Ctrl_Arg;
4703
4704 procedure Remove_Ctrl_Args is new Traverse_Proc (Remove_Ctrl_Arg);
4705 begin
4706 Remove_Ctrl_Args (Expr);
4707 end Remove_Controlling_Arguments;
4708
4709 -----------------------------------------
4710 -- Restore_Original_Selected_Component --
4711 -----------------------------------------
4712
4713 procedure Restore_Original_Selected_Component is
4714 Restored_Nodes_List : Elist_Id := No_Elist;
4715
4716 procedure Fix_Parents (N : Node_Id);
4717 -- Traverse the subtree of N fixing the Parent field of all the
4718 -- nodes.
4719
4720 function Restore_Node (N : Node_Id) return Traverse_Result;
4721 -- Process dispatching calls to functions whose original node was
4722 -- a selected component, and replace them with their original
4723 -- node. Restored nodes are stored in the Restored_Nodes_List
4724 -- to fix the parent fields of their subtrees in a separate
4725 -- tree traversal.
4726
4727 -----------------
4728 -- Fix_Parents --
4729 -----------------
4730
4731 procedure Fix_Parents (N : Node_Id) is
4732
4733 function Fix_Parent
4734 (Parent_Node : Node_Id;
4735 Node : Node_Id) return Traverse_Result;
4736 -- Process a single node
4737
4738 ----------------
4739 -- Fix_Parent --
4740 ----------------
4741
4742 function Fix_Parent
4743 (Parent_Node : Node_Id;
4744 Node : Node_Id) return Traverse_Result
4745 is
4746 Par : constant Node_Id := Parent (Node);
4747
4748 begin
4749 if Par /= Parent_Node then
4750 if Is_List_Member (Node) then
4751 Set_List_Parent (List_Containing (Node), Parent_Node);
4752 else
4753 Set_Parent (Node, Parent_Node);
4754 end if;
4755 end if;
4756
4757 return OK;
4758 end Fix_Parent;
4759
4760 procedure Fix_Parents is
4761 new Traverse_Proc_With_Parent (Fix_Parent);
4762
4763 begin
4764 Fix_Parents (N);
4765 end Fix_Parents;
4766
4767 ------------------
4768 -- Restore_Node --
4769 ------------------
4770
4771 function Restore_Node (N : Node_Id) return Traverse_Result is
4772 begin
4773 if Nkind (N) = N_Function_Call
4774 and then Nkind (Original_Node (N)) = N_Selected_Component
4775 and then Is_Dispatching_Operation (Entity (Name (N)))
4776 then
4777 Rewrite (N, Original_Node (N));
4778 Set_Original_Node (N, N);
4779
4780 -- Save the restored node in the Restored_Nodes_List to fix
4781 -- the parent fields of their subtrees in a separate tree
4782 -- traversal.
4783
4784 Append_New_Elmt (N, Restored_Nodes_List);
4785 end if;
4786
4787 return OK;
4788 end Restore_Node;
4789
4790 procedure Restore_Nodes is new Traverse_Proc (Restore_Node);
4791
4792 -- Start of processing for Restore_Original_Selected_Component
4793
4794 begin
4795 Restore_Nodes (Expr);
4796
4797 -- After restoring the original node we must fix the decoration
4798 -- of the Parent attribute to ensure tree consistency; required
4799 -- because when the class-wide condition is inherited, calls to
4800 -- New_Copy_Tree will perform copies of this subtree, and formal
4801 -- occurrences with wrong Parent field cannot be mapped to the
4802 -- new formals.
4803
4804 if Present (Restored_Nodes_List) then
4805 declare
4806 Elmt : Elmt_Id := First_Elmt (Restored_Nodes_List);
4807
4808 begin
4809 while Present (Elmt) loop
4810 Fix_Parents (Node (Elmt));
4811 Next_Elmt (Elmt);
4812 end loop;
4813 end;
4814 end if;
4815 end Restore_Original_Selected_Component;
4816
4817 -- Start of processing for Preanalyze_Condition
4818
4819 begin
4820 pragma Assert (Present (Expr));
4821 pragma Assert (Inside_Class_Condition_Preanalysis = False);
4822
4823 Push_Scope (Subp);
4824 Install_Formals (Subp);
4825 Inside_Class_Condition_Preanalysis := True;
4826
4827 Preanalyze_Spec_Expression (Expr, Standard_Boolean);
4828
4829 Inside_Class_Condition_Preanalysis := False;
4830 End_Scope;
4831
4832 -- If this preanalyzed condition has occurrences of dispatching calls
4833 -- using the Object.Operation notation, during preanalysis such calls
4834 -- are rewritten as dispatching function calls; if at later stages
4835 -- this condition is inherited we must have restored the original
4836 -- selected-component node to ensure that the preanalysis of the
4837 -- inherited condition rewrites these dispatching calls in the
4838 -- correct context to avoid reporting spurious errors.
4839
4840 Restore_Original_Selected_Component;
4841
4842 -- Traverse Expr and clear the Controlling_Argument of calls to
4843 -- nonabstract functions. Required since the preanalyzed condition
4844 -- is not yet installed on its definite context and will be cloned
4845 -- and extended in derivations with additional conditions.
4846
4847 Remove_Controlling_Arguments;
4848
4849 -- Clear also attribute Unset_Reference; again because preanalysis
4850 -- occurs in a place unrelated to the actual code.
4851
4852 Clear_Unset_References;
4853 end Preanalyze_Condition;
4854
4855 ----------------------------------------
4856 -- Save_Global_References_In_Contract --
4857 ----------------------------------------
4858
4859 procedure Save_Global_References_In_Contract
4860 (Templ : Node_Id;
4861 Gen_Id : Entity_Id)
4862 is
4863 procedure Save_Global_References_In_List (First_Prag : Node_Id);
4864 -- Save all global references in contract-related source pragmas found
4865 -- in the list, starting with pragma First_Prag.
4866
4867 ------------------------------------
4868 -- Save_Global_References_In_List --
4869 ------------------------------------
4870
4871 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
4872 Prag : Node_Id := First_Prag;
4873
4874 begin
4875 while Present (Prag) loop
4876 if Is_Generic_Contract_Pragma (Prag) then
4877 Save_Global_References (Prag);
4878 end if;
4879
4880 Prag := Next_Pragma (Prag);
4881 end loop;
4882 end Save_Global_References_In_List;
4883
4884 -- Local variables
4885
4886 Items : constant Node_Id := Contract (Defining_Entity (Templ));
4887
4888 -- Start of processing for Save_Global_References_In_Contract
4889
4890 begin
4891 -- The entity of the analyzed generic copy must be on the scope stack
4892 -- to ensure proper detection of global references.
4893
4894 Push_Scope (Gen_Id);
4895
4896 if Permits_Aspect_Specifications (Templ) then
4897 Save_Global_References_In_Aspects (Templ);
4898 end if;
4899
4900 if Present (Items) then
4901 Save_Global_References_In_List (Pre_Post_Conditions (Items));
4902 Save_Global_References_In_List (Contract_Test_Cases (Items));
4903 Save_Global_References_In_List (Classifications (Items));
4904 end if;
4905
4906 Pop_Scope;
4907 end Save_Global_References_In_Contract;
4908
4909 -------------------------
4910 -- Set_Class_Condition --
4911 -------------------------
4912
4913 procedure Set_Class_Condition
4914 (Kind : Condition_Kind;
4915 Subp : Entity_Id;
4916 Cond : Node_Id)
4917 is
4918 begin
4919 case Kind is
4920 when Class_Postcondition =>
4921 Set_Class_Postconditions (Subp, Cond);
4922
4923 when Class_Precondition =>
4924 Set_Class_Preconditions (Subp, Cond);
4925
4926 when Ignored_Class_Postcondition =>
4927 Set_Ignored_Class_Postconditions (Subp, Cond);
4928
4929 when Ignored_Class_Precondition =>
4930 Set_Ignored_Class_Preconditions (Subp, Cond);
4931 end case;
4932 end Set_Class_Condition;
4933
4934 end Contracts;