1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- C O N T R A C T S --
9 -- Copyright (C) 2015-2023, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This package contains routines that perform analysis and expansion of
29 with Types
; use Types
;
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:
44 -- Constant_After_Elaboration
68 procedure Analyze_Contracts
(L
: List_Id
);
69 -- Analyze the contracts of all eligible constructs found in list L
71 procedure Analyze_Pragmas_In_Declarations
(Body_Id
: Entity_Id
);
72 -- Perform early analysis of pragmas at the top of a given subprogram's
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.
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:
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)
96 -- Subprogram_Variant (stand alone subprogram body)
97 -- Test_Case (stand alone subprogram body)
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:
113 -- Subprogram_Variant
116 -- Freeze_Id is the entity of a [generic] package body or a [generic]
117 -- subprogram body which "freezes" the contract of Subp_Id.
119 procedure Analyze_Object_Contract
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
128 -- Depends (single concurrent object)
131 -- Global (single concurrent object)
134 -- Freeze_Id is the entity of a [generic] package body or a [generic]
135 -- subprogram body which "freezes" the contract of Obj_Id.
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
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.
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:
161 -- Freeze_Id is the entity of a [generic] package body or a [generic]
162 -- subprogram body which "freezes" the contract of Body_Id.
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:
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.
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:
192 -- Subprogram_Variant
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
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.
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.
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.
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.
225 procedure Inherit_Subprogram_Contract
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
232 -- ??? it would be nice if this routine handles Pre'Class and Post'Class
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.
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
250 -- For example, if we have a subprogram with the following profile:
252 -- procedure Prim (Obj : TagTyp; <additional formals>)
253 -- with Pre'Class => F1 (Obj) and F2(Obj)
255 -- We build the following helper that evaluates statically the class-wide
258 -- function PrimSP (Obj : TagTyp) return Boolean is
260 -- return F1 (Obj) and F2(Obj);
263 -- ... and the following helper that evaluates dynamically the class-wide
266 -- function PrimDP (Obj : TagTyp'Class; ...) return Boolean is
268 -- return F1 (Obj) and F2(Obj);
271 -- ... and the following indirect-call wrapper (ICW) that is used by the
272 -- code generated by the compiler for indirect calls:
274 -- procedure PrimICW (Obj : TagTyp; <additional formals> is
276 -- if not PrimSP (Obj) then
277 -- $raise_assert_failure ("failed precondition in call at ...");
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.
293 procedure Preanalyze_Class_Conditions
(Spec_Id
: Entity_Id
);
294 -- Preanalyze class-wide pre-/postconditions of the given subprogram
297 procedure Process_Class_Conditions_At_Freeze_Point
(Typ
: Entity_Id
);
298 -- Merge, preanalyze, and check class-wide pre/postconditions of Typ
301 procedure Save_Global_References_In_Contract
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.