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;
29 with Exp_Ch5; use Exp_Ch5;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Namet; use Namet;
33 with Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Rtsfind; use Rtsfind;
37 with Sem_Eval; use Sem_Eval;
38 with Sem_Res; use Sem_Res;
39 with Sem_Util; use Sem_Util;
40 with Sinfo; use Sinfo;
41 with Snames; use Snames;
42 with Stand; use Stand;
43 with Tbuild; use Tbuild;
44 with Uintp; use Uintp;
46 package body Exp_SPARK is
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
52 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id);
53 -- Replace occurrences of System'To_Address by calls to
54 -- System.Storage_Elements.To_Address
56 procedure Expand_SPARK_Freeze_Type (E : Entity_Id);
57 -- Build the DIC procedure of a type when needed, if not already done
59 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id);
60 -- Perform object-declaration-specific expansion
62 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
63 -- Perform name evaluation for a renamed object
69 procedure Expand_SPARK (N : Node_Id) is
73 -- Qualification of entity names in formal verification mode
74 -- is limited to the addition of a suffix for homonyms (see
75 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
76 -- as full expansion does, but this was removed as this prevents the
77 -- verification back-end from using a short name for debugging and
78 -- user interaction. The verification back-end already takes care
79 -- of qualifying names when needed.
81 when N_Block_Statement
84 | N_Package_Declaration
85 | N_Protected_Type_Declaration
87 | N_Task_Type_Declaration
89 Qualify_Entity_Names (N);
91 -- Replace occurrences of System'To_Address by calls to
92 -- System.Storage_Elements.To_Address.
94 when N_Attribute_Reference =>
95 Expand_SPARK_N_Attribute_Reference (N);
100 Expand_SPARK_Potential_Renaming (N);
102 -- Loop iterations over arrays need to be expanded, to avoid getting
103 -- two names referring to the same object in memory (the array and
104 -- the iterator) in GNATprove, especially since both can be written
105 -- (thus possibly leading to interferences due to aliasing). No such
106 -- problem arises with quantified expressions over arrays, which are
107 -- dealt with specially in GNATprove.
109 when N_Loop_Statement =>
111 Scheme : constant Node_Id := Iteration_Scheme (N);
114 and then Present (Iterator_Specification (Scheme))
116 Is_Iterator_Over_Array (Iterator_Specification (Scheme))
118 Expand_Iterator_Loop_Over_Array (N);
122 when N_Object_Declaration =>
123 Expand_SPARK_N_Object_Declaration (N);
125 when N_Object_Renaming_Declaration =>
126 Expand_SPARK_N_Object_Renaming_Declaration (N);
128 when N_Freeze_Entity =>
129 if Is_Type (Entity (N)) then
130 Expand_SPARK_Freeze_Type (Entity (N));
133 -- In SPARK mode, no other constructs require expansion
140 ----------------------------------------
141 -- Expand_SPARK_N_Attribute_Reference --
142 ----------------------------------------
144 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id) is
145 Aname : constant Name_Id := Attribute_Name (N);
146 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
147 Loc : constant Source_Ptr := Sloc (N);
148 Typ : constant Entity_Id := Etype (N);
152 if Attr_Id = Attribute_To_Address then
154 -- Extract and convert argument to expected type for call
157 Make_Type_Conversion (Loc,
159 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
160 Expression => Relocate_Node (First (Expressions (N))));
162 -- Replace attribute reference with call
165 Make_Function_Call (Loc,
167 New_Occurrence_Of (RTE (RE_To_Address), Loc),
168 Parameter_Associations => New_List (Expr)));
169 Analyze_And_Resolve (N, Typ);
171 -- For attributes which return Universal_Integer, introduce a conversion
172 -- to the expected type with the appropriate check flags set.
174 elsif Attr_Id = Attribute_Alignment
175 or else Attr_Id = Attribute_Bit
176 or else Attr_Id = Attribute_Bit_Position
177 or else Attr_Id = Attribute_Descriptor_Size
178 or else Attr_Id = Attribute_First_Bit
179 or else Attr_Id = Attribute_Last_Bit
180 or else Attr_Id = Attribute_Length
181 or else Attr_Id = Attribute_Max_Size_In_Storage_Elements
182 or else Attr_Id = Attribute_Pos
183 or else Attr_Id = Attribute_Position
184 or else Attr_Id = Attribute_Range_Length
185 or else Attr_Id = Attribute_Object_Size
186 or else Attr_Id = Attribute_Size
187 or else Attr_Id = Attribute_Value_Size
188 or else Attr_Id = Attribute_VADS_Size
189 or else Attr_Id = Attribute_Aft
190 or else Attr_Id = Attribute_Max_Alignment_For_Allocation
192 -- If the expected type is Long_Long_Integer, there will be no check
193 -- flag as the compiler assumes attributes always fit in this type.
194 -- Since in SPARK_Mode we do not take Storage_Error into account, we
195 -- cannot make this assumption and need to produce a check.
196 -- ??? It should be enough to add this check for attributes 'Length
197 -- and 'Range_Length when the type is as big as Long_Long_Integer.
200 Typ : Entity_Id := Empty;
202 if Attr_Id = Attribute_Range_Length then
203 Typ := Etype (Prefix (N));
205 elsif Attr_Id = Attribute_Length then
206 Typ := Etype (Prefix (N));
213 if Is_Access_Type (Typ) then
214 Typ := Designated_Type (Typ);
217 if No (Expressions (N)) then
220 J := UI_To_Int (Expr_Value (First (Expressions (N))));
223 Indx := First_Index (Typ);
233 Apply_Universal_Integer_Attribute_Checks (N);
236 and then RM_Size (Typ) = RM_Size (Standard_Long_Long_Integer)
238 Set_Do_Overflow_Check (N);
242 end Expand_SPARK_N_Attribute_Reference;
244 ------------------------------
245 -- Expand_SPARK_Freeze_Type --
246 ------------------------------
248 procedure Expand_SPARK_Freeze_Type (E : Entity_Id) is
250 -- When a DIC is inherited by a tagged type, it may need to be
251 -- specialized to the descendant type, hence build a separate DIC
252 -- procedure for it as done during regular expansion for compilation.
255 and then Is_Tagged_Type (E)
257 Build_DIC_Procedure_Body (E, For_Freeze => True);
259 end Expand_SPARK_Freeze_Type;
261 ---------------------------------------
262 -- Expand_SPARK_N_Object_Declaration --
263 ---------------------------------------
265 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id) is
266 Def_Id : constant Entity_Id := Defining_Identifier (N);
267 Loc : constant Source_Ptr := Sloc (N);
268 Typ : constant Entity_Id := Etype (Def_Id);
271 -- If the object declaration denotes a variable without initialization
272 -- whose type is subject to pragma Default_Initial_Condition, create
273 -- and analyze a dummy call to the DIC procedure of the type in order
274 -- to detect potential elaboration issues.
276 if Comes_From_Source (Def_Id)
277 and then Has_DIC (Typ)
278 and then Present (DIC_Procedure (Typ))
279 and then not Has_Init_Expression (N)
281 Analyze (Build_DIC_Call (Loc, Def_Id, Typ));
283 end Expand_SPARK_N_Object_Declaration;
285 ------------------------------------------------
286 -- Expand_SPARK_N_Object_Renaming_Declaration --
287 ------------------------------------------------
289 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
291 -- Unconditionally remove all side effects from the name
293 Evaluate_Name (Name (N));
294 end Expand_SPARK_N_Object_Renaming_Declaration;
296 -------------------------------------
297 -- Expand_SPARK_Potential_Renaming --
298 -------------------------------------
300 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
301 Loc : constant Source_Ptr := Sloc (N);
302 Ren_Id : constant Entity_Id := Entity (N);
303 Typ : constant Entity_Id := Etype (N);
307 -- Replace a reference to a renaming with the actual renamed object
309 if Ekind (Ren_Id) in Object_Kind then
310 Obj_Id := Renamed_Object (Ren_Id);
312 if Present (Obj_Id) then
314 -- The renamed object is an entity when instantiating generics
315 -- or inlining bodies. In this case the renaming is part of the
316 -- mapping "prologue" which links actuals to formals.
318 if Nkind (Obj_Id) in N_Entity then
319 Rewrite (N, New_Occurrence_Of (Obj_Id, Loc));
321 -- Otherwise the renamed object denotes a name
324 Rewrite (N, New_Copy_Tree (Obj_Id, New_Sloc => Loc));
325 Reset_Analyzed_Flags (N);
328 Analyze_And_Resolve (N, Typ);
331 end Expand_SPARK_Potential_Renaming;