| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- C O N T R A C T S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 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 |
-- This package contains routines that perform analysis and expansion of |
| 27 |
-- various contracts. |
| 28 |
|
| 29 |
with Types; use Types; |
| 30 |
|
| 31 |
package Contracts is |
| 32 |
|
| 33 |
procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id); |
| 34 |
-- Add pragma Prag to the contract of a constant, entry, entry family, |
| 35 |
-- [generic] package, package body, protected unit, [generic] subprogram, |
| 36 |
-- subprogram body, variable, task unit, or type denoted by Id. |
| 37 |
-- The following are valid pragmas: |
| 38 |
-- |
| 39 |
-- Abstract_State |
| 40 |
-- Always_Terminates |
| 41 |
-- Async_Readers |
| 42 |
-- Async_Writers |
| 43 |
-- Attach_Handler |
| 44 |
-- Constant_After_Elaboration |
| 45 |
-- Contract_Cases |
| 46 |
-- Depends |
| 47 |
-- Effective_Reads |
| 48 |
-- Effective_Writes |
| 49 |
-- Exceptional_Cases |
| 50 |
-- Extensions_Visible |
| 51 |
-- Global |
| 52 |
-- Initial_Condition |
| 53 |
-- Initializes |
| 54 |
-- Interrupt_Handler |
| 55 |
-- No_Caching |
| 56 |
-- Part_Of |
| 57 |
-- Postcondition |
| 58 |
-- Precondition |
| 59 |
-- Refined_Depends |
| 60 |
-- Refined_Global |
| 61 |
-- Refined_Post |
| 62 |
-- Refined_States |
| 63 |
-- Side_Effects |
| 64 |
-- Subprogram_Variant |
| 65 |
-- Test_Case |
| 66 |
-- Volatile_Function |
| 67 |
|
| 68 |
procedure Analyze_Contracts (L : List_Id); |
| 69 |
-- Analyze the contracts of all eligible constructs found in list L |
| 70 |
|
| 71 |
procedure Analyze_Pragmas_In_Declarations (Body_Id : Entity_Id); |
| 72 |
-- Perform early analysis of pragmas at the top of a given subprogram's |
| 73 |
-- declarations. |
| 74 |
-- |
| 75 |
-- The purpose of this is to analyze contract-related pragmas for later |
| 76 |
-- processing, but also to handle other such pragmas before these |
| 77 |
-- declarations get moved to an internal wrapper as part of contract |
| 78 |
-- expansion. For example, pragmas Inline, Ghost, Volatile all need to |
| 79 |
-- apply directly to the subprogram and not be moved to a wrapper. |
| 80 |
|
| 81 |
procedure Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id : Entity_Id); |
| 82 |
-- Analyze all delayed pragmas chained on the contract of entry or |
| 83 |
-- subprogram body Body_Id as if they appeared at the end of a declarative |
| 84 |
-- region. Pragmas in question are: |
| 85 |
-- |
| 86 |
-- Always_Terminates (stand alone subprogram body) |
| 87 |
-- Contract_Cases (stand alone subprogram body) |
| 88 |
-- Depends (stand alone subprogram body) |
| 89 |
-- Exceptional_Cases (stand alone subprogram body) |
| 90 |
-- Global (stand alone subprogram body) |
| 91 |
-- Postcondition (stand alone subprogram body) |
| 92 |
-- Precondition (stand alone subprogram body) |
| 93 |
-- Refined_Depends |
| 94 |
-- Refined_Global |
| 95 |
-- Refined_Post |
| 96 |
-- Subprogram_Variant (stand alone subprogram body) |
| 97 |
-- Test_Case (stand alone subprogram body) |
| 98 |
|
| 99 |
procedure Analyze_Entry_Or_Subprogram_Contract |
| 100 |
(Subp_Id : Entity_Id; |
| 101 |
Freeze_Id : Entity_Id := Empty); |
| 102 |
-- Analyze all delayed pragmas chained on the contract of entry or |
| 103 |
-- subprogram Subp_Id as if they appeared at the end of a declarative |
| 104 |
-- region. The pragmas in question are: |
| 105 |
-- |
| 106 |
-- Always_Terminates |
| 107 |
-- Contract_Cases |
| 108 |
-- Depends |
| 109 |
-- Exceptional_Cases |
| 110 |
-- Global |
| 111 |
-- Postcondition |
| 112 |
-- Precondition |
| 113 |
-- Subprogram_Variant |
| 114 |
-- Test_Case |
| 115 |
-- |
| 116 |
-- Freeze_Id is the entity of a [generic] package body or a [generic] |
| 117 |
-- subprogram body which "freezes" the contract of Subp_Id. |
| 118 |
|
| 119 |
procedure Analyze_Object_Contract |
| 120 |
(Obj_Id : Entity_Id; |
| 121 |
Freeze_Id : Entity_Id := Empty); |
| 122 |
-- Analyze all delayed pragmas chained on the contract of object Obj_Id as |
| 123 |
-- if they appeared at the end of the declarative region. The pragmas to be |
| 124 |
-- considered are: |
| 125 |
-- |
| 126 |
-- Async_Readers |
| 127 |
-- Async_Writers |
| 128 |
-- Depends (single concurrent object) |
| 129 |
-- Effective_Reads |
| 130 |
-- Effective_Writes |
| 131 |
-- Global (single concurrent object) |
| 132 |
-- Part_Of |
| 133 |
-- |
| 134 |
-- Freeze_Id is the entity of a [generic] package body or a [generic] |
| 135 |
-- subprogram body which "freezes" the contract of Obj_Id. |
| 136 |
|
| 137 |
procedure Analyze_Type_Contract (Type_Id : Entity_Id); |
| 138 |
-- Analyze all delayed pragmas chained on the contract of object Obj_Id as |
| 139 |
-- if they appeared at the end of the declarative region. The pragmas to be |
| 140 |
-- considered are: |
| 141 |
-- |
| 142 |
-- Async_Readers |
| 143 |
-- Async_Writers |
| 144 |
-- Effective_Reads |
| 145 |
-- Effective_Writes |
| 146 |
-- Postcondition |
| 147 |
-- Precondition |
| 148 |
-- |
| 149 |
-- In the case of a protected or task type, there will also be |
| 150 |
-- a call to Analyze_Protected_Contract or Analyze_Task_Contract. |
| 151 |
|
| 152 |
procedure Analyze_Package_Body_Contract |
| 153 |
(Body_Id : Entity_Id; |
| 154 |
Freeze_Id : Entity_Id := Empty); |
| 155 |
-- Analyze all delayed pragmas chained on the contract of package body |
| 156 |
-- Body_Id as if they appeared at the end of a declarative region. The |
| 157 |
-- pragmas that are considered are: |
| 158 |
-- |
| 159 |
-- Refined_State |
| 160 |
-- |
| 161 |
-- Freeze_Id is the entity of a [generic] package body or a [generic] |
| 162 |
-- subprogram body which "freezes" the contract of Body_Id. |
| 163 |
|
| 164 |
procedure Analyze_Package_Contract (Pack_Id : Entity_Id); |
| 165 |
-- Analyze all delayed pragmas chained on the contract of package Pack_Id |
| 166 |
-- as if they appeared at the end of a declarative region. The pragmas |
| 167 |
-- that are considered are: |
| 168 |
-- |
| 169 |
-- Initial_Condition |
| 170 |
-- Initializes |
| 171 |
|
| 172 |
procedure Analyze_Protected_Contract (Prot_Id : Entity_Id); |
| 173 |
-- Analyze all delayed pragmas chained on the contract of protected unit |
| 174 |
-- Prot_Id if they appeared at the end of a declarative region. Currently |
| 175 |
-- there are no such pragmas. |
| 176 |
|
| 177 |
procedure Analyze_Subprogram_Body_Stub_Contract (Stub_Id : Entity_Id); |
| 178 |
-- Analyze all delayed pragmas chained on the contract of subprogram body |
| 179 |
-- stub Stub_Id as if they appeared at the end of a declarative region. The |
| 180 |
-- pragmas in question are: |
| 181 |
-- |
| 182 |
-- Always_Terminates |
| 183 |
-- Contract_Cases |
| 184 |
-- Depends |
| 185 |
-- Exceptional_Cases |
| 186 |
-- Global |
| 187 |
-- Postcondition |
| 188 |
-- Precondition |
| 189 |
-- Refined_Depends |
| 190 |
-- Refined_Global |
| 191 |
-- Refined_Post |
| 192 |
-- Subprogram_Variant |
| 193 |
-- Test_Case |
| 194 |
|
| 195 |
procedure Analyze_Task_Contract (Task_Id : Entity_Id); |
| 196 |
-- Analyze all delayed pragmas chained on the contract of task unit Task_Id |
| 197 |
-- as if they appeared at the end of a declarative region. The pragmas in |
| 198 |
-- question are: |
| 199 |
-- |
| 200 |
-- Depends |
| 201 |
-- Global |
| 202 |
|
| 203 |
procedure Build_Entry_Contract_Wrapper (E : Entity_Id; Decl : Node_Id); |
| 204 |
-- Build the body of a wrapper procedure for an entry or entry family that |
| 205 |
-- has contract cases, preconditions, or postconditions, and add it to the |
| 206 |
-- freeze actions of the related synchronized type. |
| 207 |
-- |
| 208 |
-- The body first verifies the preconditions and case guards of the |
| 209 |
-- contract cases, then invokes the entry [family], and finally verifies |
| 210 |
-- the postconditions and the consequences of the contract cases. E denotes |
| 211 |
-- the entry family. Decl denotes the declaration of the enclosing |
| 212 |
-- synchronized type. |
| 213 |
|
| 214 |
procedure Create_Generic_Contract (Unit : Node_Id); |
| 215 |
-- Create a contract node for a generic package, generic subprogram, or a |
| 216 |
-- generic body denoted by Unit by collecting all source contract-related |
| 217 |
-- pragmas in the contract of the unit. |
| 218 |
|
| 219 |
procedure Freeze_Previous_Contracts (Body_Decl : Node_Id); |
| 220 |
-- Freeze the contracts of all source constructs found in the declarative |
| 221 |
-- list which contains entry, package, protected, subprogram, or task body |
| 222 |
-- denoted by Body_Decl. In addition, freeze the contract of the nearest |
| 223 |
-- enclosing package body. |
| 224 |
|
| 225 |
procedure Inherit_Subprogram_Contract |
| 226 |
(Subp : Entity_Id; |
| 227 |
From_Subp : Entity_Id); |
| 228 |
-- Inherit relevant contract items from source subprogram From_Subp. Subp |
| 229 |
-- denotes the destination subprogram. The inherited items are: |
| 230 |
-- Extensions_Visible |
| 231 |
-- Side_Effects |
| 232 |
-- ??? it would be nice if this routine handles Pre'Class and Post'Class |
| 233 |
|
| 234 |
procedure Instantiate_Subprogram_Contract (Templ : Node_Id; L : List_Id); |
| 235 |
-- Instantiate all source pragmas found in the contract of the generic |
| 236 |
-- subprogram declaration template denoted by Templ. The instantiated |
| 237 |
-- pragmas are added to list L. |
| 238 |
|
| 239 |
procedure Make_Class_Precondition_Subps |
| 240 |
(Subp_Id : Entity_Id; |
| 241 |
Late_Overriding : Boolean := False); |
| 242 |
-- Build helpers that at run time evaluate statically and dynamically the |
| 243 |
-- class-wide preconditions of Subp_Id; build also the indirect-call |
| 244 |
-- wrapper (ICW) required to check class-wide preconditions when the |
| 245 |
-- subprogram is invoked through an access-to-subprogram, or when it |
| 246 |
-- overrides an inherited class-wide precondition (see AI12-0195-1). |
| 247 |
-- Late_Overriding enables special handling required for late-overriding |
| 248 |
-- subprograms. |
| 249 |
-- |
| 250 |
-- For example, if we have a subprogram with the following profile: |
| 251 |
-- |
| 252 |
-- procedure Prim (Obj : TagTyp; <additional formals>) |
| 253 |
-- with Pre'Class => F1 (Obj) and F2(Obj) |
| 254 |
-- |
| 255 |
-- We build the following helper that evaluates statically the class-wide |
| 256 |
-- precondition: |
| 257 |
-- |
| 258 |
-- function PrimSP (Obj : TagTyp) return Boolean is |
| 259 |
-- begin |
| 260 |
-- return F1 (Obj) and F2(Obj); |
| 261 |
-- end PrimSP; |
| 262 |
-- |
| 263 |
-- ... and the following helper that evaluates dynamically the class-wide |
| 264 |
-- precondition: |
| 265 |
-- |
| 266 |
-- function PrimDP (Obj : TagTyp'Class; ...) return Boolean is |
| 267 |
-- begin |
| 268 |
-- return F1 (Obj) and F2(Obj); |
| 269 |
-- end PrimSP; |
| 270 |
-- |
| 271 |
-- ... and the following indirect-call wrapper (ICW) that is used by the |
| 272 |
-- code generated by the compiler for indirect calls: |
| 273 |
-- |
| 274 |
-- procedure PrimICW (Obj : TagTyp; <additional formals> is |
| 275 |
-- begin |
| 276 |
-- if not PrimSP (Obj) then |
| 277 |
-- $raise_assert_failure ("failed precondition in call at ..."); |
| 278 |
-- end if; |
| 279 |
-- |
| 280 |
-- Prim (Obj, ...); |
| 281 |
-- end Prim; |
| 282 |
|
| 283 |
procedure Merge_Class_Conditions (Spec_Id : Entity_Id); |
| 284 |
-- Merge and preanalyze all class-wide conditions of Spec_Id (class-wide |
| 285 |
-- preconditions merged with operator or-else; class-wide postconditions |
| 286 |
-- merged with operator and-then). Ignored pre/postconditions are also |
| 287 |
-- merged since, although they are not required to generate code, their |
| 288 |
-- preanalysis is required to perform semantic checks. Resulting merged |
| 289 |
-- expressions are later installed by the expander in helper subprograms |
| 290 |
-- which are invoked from the caller side; they are also used to build |
| 291 |
-- the dispatch-table wrapper (DTW), if required. |
| 292 |
|
| 293 |
procedure Preanalyze_Class_Conditions (Spec_Id : Entity_Id); |
| 294 |
-- Preanalyze class-wide pre-/postconditions of the given subprogram |
| 295 |
-- specification. |
| 296 |
|
| 297 |
procedure Process_Class_Conditions_At_Freeze_Point (Typ : Entity_Id); |
| 298 |
-- Merge, preanalyze, and check class-wide pre/postconditions of Typ |
| 299 |
-- primitives. |
| 300 |
|
| 301 |
procedure Save_Global_References_In_Contract |
| 302 |
(Templ : Node_Id; |
| 303 |
Gen_Id : Entity_Id); |
| 304 |
-- Save all global references found within the aspect specifications and |
| 305 |
-- the contract-related source pragmas assocated with generic template |
| 306 |
-- Templ. Gen_Id denotes the entity of the analyzed generic copy. |
| 307 |
|
| 308 |
end Contracts; |