1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- E X P _ S P A R K --
9 -- Copyright (C) 1992-2017, 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 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Einfo
; use Einfo
;
30 with Exp_Ch5
; use Exp_Ch5
;
31 with Exp_Dbug
; use Exp_Dbug
;
32 with Exp_Util
; use Exp_Util
;
33 with Namet
; use Namet
;
34 with Nlists
; use Nlists
;
35 with Nmake
; use Nmake
;
36 with Rtsfind
; use Rtsfind
;
38 with Sem_Eval
; use Sem_Eval
;
39 with Sem_Res
; use Sem_Res
;
40 with Sem_Util
; use Sem_Util
;
41 with Sinfo
; use Sinfo
;
42 with Snames
; use Snames
;
43 with Stand
; use Stand
;
44 with Tbuild
; use Tbuild
;
45 with Uintp
; use Uintp
;
47 package body Exp_SPARK
is
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Expand_SPARK_N_Attribute_Reference
(N
: Node_Id
);
54 -- Replace occurrences of System'To_Address by calls to
55 -- System.Storage_Elements.To_Address
57 procedure Expand_SPARK_Freeze_Type
(E
: Entity_Id
);
58 -- Build the DIC procedure of a type when needed, if not already done
60 procedure Expand_SPARK_N_Object_Declaration
(N
: Node_Id
);
61 -- Perform object-declaration-specific expansion
63 procedure Expand_SPARK_N_Object_Renaming_Declaration
(N
: Node_Id
);
64 -- Perform name evaluation for a renamed object
66 procedure Expand_SPARK_Op_Ne
(N
: Node_Id
);
67 -- Rewrite operator /= based on operator = when defined explicitly
73 procedure Expand_SPARK
(N
: Node_Id
) is
77 -- Qualification of entity names in formal verification mode
78 -- is limited to the addition of a suffix for homonyms (see
79 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
80 -- as full expansion does, but this was removed as this prevents the
81 -- verification back-end from using a short name for debugging and
82 -- user interaction. The verification back-end already takes care
83 -- of qualifying names when needed.
85 when N_Block_Statement
88 | N_Package_Declaration
89 | N_Protected_Type_Declaration
91 | N_Task_Type_Declaration
93 Qualify_Entity_Names
(N
);
95 -- Replace occurrences of System'To_Address by calls to
96 -- System.Storage_Elements.To_Address.
98 when N_Attribute_Reference
=>
99 Expand_SPARK_N_Attribute_Reference
(N
);
104 Expand_SPARK_Potential_Renaming
(N
);
106 -- Loop iterations over arrays need to be expanded, to avoid getting
107 -- two names referring to the same object in memory (the array and
108 -- the iterator) in GNATprove, especially since both can be written
109 -- (thus possibly leading to interferences due to aliasing). No such
110 -- problem arises with quantified expressions over arrays, which are
111 -- dealt with specially in GNATprove.
113 when N_Loop_Statement
=>
115 Scheme
: constant Node_Id
:= Iteration_Scheme
(N
);
118 and then Present
(Iterator_Specification
(Scheme
))
120 Is_Iterator_Over_Array
(Iterator_Specification
(Scheme
))
122 Expand_Iterator_Loop_Over_Array
(N
);
126 when N_Object_Declaration
=>
127 Expand_SPARK_N_Object_Declaration
(N
);
129 when N_Object_Renaming_Declaration
=>
130 Expand_SPARK_N_Object_Renaming_Declaration
(N
);
133 Expand_SPARK_Op_Ne
(N
);
135 when N_Freeze_Entity
=>
136 if Is_Type
(Entity
(N
)) then
137 Expand_SPARK_Freeze_Type
(Entity
(N
));
140 -- In SPARK mode, no other constructs require expansion
147 ----------------------------------------
148 -- Expand_SPARK_N_Attribute_Reference --
149 ----------------------------------------
151 procedure Expand_SPARK_N_Attribute_Reference
(N
: Node_Id
) is
152 Aname
: constant Name_Id
:= Attribute_Name
(N
);
153 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
154 Loc
: constant Source_Ptr
:= Sloc
(N
);
155 Typ
: constant Entity_Id
:= Etype
(N
);
159 if Attr_Id
= Attribute_To_Address
then
161 -- Extract and convert argument to expected type for call
164 Make_Type_Conversion
(Loc
,
166 New_Occurrence_Of
(RTE
(RE_Integer_Address
), Loc
),
167 Expression
=> Relocate_Node
(First
(Expressions
(N
))));
169 -- Replace attribute reference with call
172 Make_Function_Call
(Loc
,
174 New_Occurrence_Of
(RTE
(RE_To_Address
), Loc
),
175 Parameter_Associations
=> New_List
(Expr
)));
176 Analyze_And_Resolve
(N
, Typ
);
178 -- For attributes which return Universal_Integer, introduce a conversion
179 -- to the expected type with the appropriate check flags set.
181 elsif Attr_Id
= Attribute_Alignment
182 or else Attr_Id
= Attribute_Bit
183 or else Attr_Id
= Attribute_Bit_Position
184 or else Attr_Id
= Attribute_Descriptor_Size
185 or else Attr_Id
= Attribute_First_Bit
186 or else Attr_Id
= Attribute_Last_Bit
187 or else Attr_Id
= Attribute_Length
188 or else Attr_Id
= Attribute_Max_Size_In_Storage_Elements
189 or else Attr_Id
= Attribute_Pos
190 or else Attr_Id
= Attribute_Position
191 or else Attr_Id
= Attribute_Range_Length
192 or else Attr_Id
= Attribute_Object_Size
193 or else Attr_Id
= Attribute_Size
194 or else Attr_Id
= Attribute_Value_Size
195 or else Attr_Id
= Attribute_VADS_Size
196 or else Attr_Id
= Attribute_Aft
197 or else Attr_Id
= Attribute_Max_Alignment_For_Allocation
199 -- If the expected type is Long_Long_Integer, there will be no check
200 -- flag as the compiler assumes attributes always fit in this type.
201 -- Since in SPARK_Mode we do not take Storage_Error into account, we
202 -- cannot make this assumption and need to produce a check.
203 -- ??? It should be enough to add this check for attributes 'Length
204 -- and 'Range_Length when the type is as big as Long_Long_Integer.
207 Typ
: Entity_Id
:= Empty
;
209 if Attr_Id
= Attribute_Range_Length
then
210 Typ
:= Etype
(Prefix
(N
));
212 elsif Attr_Id
= Attribute_Length
then
213 Typ
:= Etype
(Prefix
(N
));
220 if Is_Access_Type
(Typ
) then
221 Typ
:= Designated_Type
(Typ
);
224 if No
(Expressions
(N
)) then
227 J
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
230 Indx
:= First_Index
(Typ
);
240 Apply_Universal_Integer_Attribute_Checks
(N
);
243 and then RM_Size
(Typ
) = RM_Size
(Standard_Long_Long_Integer
)
245 Set_Do_Overflow_Check
(N
);
249 end Expand_SPARK_N_Attribute_Reference
;
251 ------------------------------
252 -- Expand_SPARK_Freeze_Type --
253 ------------------------------
255 procedure Expand_SPARK_Freeze_Type
(E
: Entity_Id
) is
257 -- When a DIC is inherited by a tagged type, it may need to be
258 -- specialized to the descendant type, hence build a separate DIC
259 -- procedure for it as done during regular expansion for compilation.
261 if Has_DIC
(E
) and then Is_Tagged_Type
(E
) then
262 Build_DIC_Procedure_Body
(E
, For_Freeze
=> True);
264 end Expand_SPARK_Freeze_Type
;
266 ---------------------------------------
267 -- Expand_SPARK_N_Object_Declaration --
268 ---------------------------------------
270 procedure Expand_SPARK_N_Object_Declaration
(N
: Node_Id
) is
271 Def_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
272 Loc
: constant Source_Ptr
:= Sloc
(N
);
273 Typ
: constant Entity_Id
:= Etype
(Def_Id
);
276 -- If the object declaration denotes a variable without initialization
277 -- whose type is subject to pragma Default_Initial_Condition, create
278 -- and analyze a dummy call to the DIC procedure of the type in order
279 -- to detect potential elaboration issues.
281 if Comes_From_Source
(Def_Id
)
282 and then Has_DIC
(Typ
)
283 and then Present
(DIC_Procedure
(Typ
))
284 and then not Has_Init_Expression
(N
)
286 Analyze
(Build_DIC_Call
(Loc
, Def_Id
, Typ
));
288 end Expand_SPARK_N_Object_Declaration
;
290 ------------------------------------------------
291 -- Expand_SPARK_N_Object_Renaming_Declaration --
292 ------------------------------------------------
294 procedure Expand_SPARK_N_Object_Renaming_Declaration
(N
: Node_Id
) is
296 -- Unconditionally remove all side effects from the name
298 Evaluate_Name
(Name
(N
));
299 end Expand_SPARK_N_Object_Renaming_Declaration
;
301 ------------------------
302 -- Expand_SPARK_Op_Ne --
303 ------------------------
305 procedure Expand_SPARK_Op_Ne
(N
: Node_Id
) is
306 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
309 -- Case of elementary type with standard operator
311 if Is_Elementary_Type
(Typ
)
312 and then Sloc
(Entity
(N
)) = Standard_Location
317 Exp_Ch4
.Expand_N_Op_Ne
(N
);
319 end Expand_SPARK_Op_Ne
;
321 -------------------------------------
322 -- Expand_SPARK_Potential_Renaming --
323 -------------------------------------
325 procedure Expand_SPARK_Potential_Renaming
(N
: Node_Id
) is
326 Loc
: constant Source_Ptr
:= Sloc
(N
);
327 Ren_Id
: constant Entity_Id
:= Entity
(N
);
328 Typ
: constant Entity_Id
:= Etype
(N
);
332 -- Replace a reference to a renaming with the actual renamed object
334 if Ekind
(Ren_Id
) in Object_Kind
then
335 Obj_Id
:= Renamed_Object
(Ren_Id
);
337 if Present
(Obj_Id
) then
339 -- The renamed object is an entity when instantiating generics
340 -- or inlining bodies. In this case the renaming is part of the
341 -- mapping "prologue" which links actuals to formals.
343 if Nkind
(Obj_Id
) in N_Entity
then
344 Rewrite
(N
, New_Occurrence_Of
(Obj_Id
, Loc
));
346 -- Otherwise the renamed object denotes a name
349 Rewrite
(N
, New_Copy_Tree
(Obj_Id
, New_Sloc
=> Loc
));
350 Reset_Analyzed_Flags
(N
);
353 Analyze_And_Resolve
(N
, Typ
);
356 end Expand_SPARK_Potential_Renaming
;