[Ada] SPARK: disable expansion of Enum_Rep
[gcc.git] / gcc / ada / exp_spark.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ S P A R K --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2019, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Exp_Attr;
30 with Exp_Ch4;
31 with Exp_Ch5; use Exp_Ch5;
32 with Exp_Dbug; use Exp_Dbug;
33 with Exp_Util; use Exp_Util;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
37 with Rtsfind; use Rtsfind;
38 with Sem; use Sem;
39 with Sem_Eval; use Sem_Eval;
40 with Sem_Prag; use Sem_Prag;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Tbuild; use Tbuild;
47 with Uintp; use Uintp;
48
49 package body Exp_SPARK is
50
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
54
55 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id);
56 -- Replace occurrences of System'To_Address by calls to
57 -- System.Storage_Elements.To_Address
58
59 procedure Expand_SPARK_N_Freeze_Type (E : Entity_Id);
60 -- Build the DIC procedure of a type when needed, if not already done
61
62 procedure Expand_SPARK_N_Indexed_Component (N : Node_Id);
63 -- Insert explicit dereference if required
64
65 procedure Expand_SPARK_N_Loop_Statement (N : Node_Id);
66 -- Perform loop statement-specific expansion
67
68 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id);
69 -- Perform object-declaration-specific expansion
70
71 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
72 -- Perform name evaluation for a renamed object
73
74 procedure Expand_SPARK_N_Op_Ne (N : Node_Id);
75 -- Rewrite operator /= based on operator = when defined explicitly
76
77 procedure Expand_SPARK_N_Selected_Component (N : Node_Id);
78 -- Insert explicit dereference if required
79
80 ------------------
81 -- Expand_SPARK --
82 ------------------
83
84 procedure Expand_SPARK (N : Node_Id) is
85 begin
86 case Nkind (N) is
87
88 -- Qualification of entity names in formal verification mode
89 -- is limited to the addition of a suffix for homonyms (see
90 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
91 -- as full expansion does, but this was removed as this prevents the
92 -- verification back-end from using a short name for debugging and
93 -- user interaction. The verification back-end already takes care
94 -- of qualifying names when needed.
95
96 when N_Block_Statement
97 | N_Entry_Declaration
98 | N_Package_Body
99 | N_Package_Declaration
100 | N_Protected_Type_Declaration
101 | N_Subprogram_Body
102 | N_Task_Type_Declaration
103 =>
104 Qualify_Entity_Names (N);
105
106 -- Replace occurrences of System'To_Address by calls to
107 -- System.Storage_Elements.To_Address.
108
109 when N_Attribute_Reference =>
110 Expand_SPARK_N_Attribute_Reference (N);
111
112 when N_Expanded_Name
113 | N_Identifier
114 =>
115 Expand_SPARK_Potential_Renaming (N);
116
117 -- Loop iterations over arrays need to be expanded, to avoid getting
118 -- two names referring to the same object in memory (the array and
119 -- the iterator) in GNATprove, especially since both can be written
120 -- (thus possibly leading to interferences due to aliasing). No such
121 -- problem arises with quantified expressions over arrays, which are
122 -- dealt with specially in GNATprove.
123
124 when N_Loop_Statement =>
125 Expand_SPARK_N_Loop_Statement (N);
126
127 when N_Object_Declaration =>
128 Expand_SPARK_N_Object_Declaration (N);
129
130 when N_Object_Renaming_Declaration =>
131 Expand_SPARK_N_Object_Renaming_Declaration (N);
132
133 when N_Op_Ne =>
134 Expand_SPARK_N_Op_Ne (N);
135
136 when N_Freeze_Entity =>
137 if Is_Type (Entity (N)) then
138 Expand_SPARK_N_Freeze_Type (Entity (N));
139 end if;
140
141 when N_Indexed_Component =>
142 Expand_SPARK_N_Indexed_Component (N);
143
144 when N_Selected_Component =>
145 Expand_SPARK_N_Selected_Component (N);
146
147 -- In SPARK mode, no other constructs require expansion
148
149 when others =>
150 null;
151 end case;
152 end Expand_SPARK;
153
154 --------------------------------
155 -- Expand_SPARK_N_Freeze_Type --
156 --------------------------------
157
158 procedure Expand_SPARK_N_Freeze_Type (E : Entity_Id) is
159 begin
160 -- When a DIC is inherited by a tagged type, it may need to be
161 -- specialized to the descendant type, hence build a separate DIC
162 -- procedure for it as done during regular expansion for compilation.
163
164 if Has_DIC (E) and then Is_Tagged_Type (E) then
165 Build_DIC_Procedure_Body (E, For_Freeze => True);
166 end if;
167 end Expand_SPARK_N_Freeze_Type;
168
169 ----------------------------------------
170 -- Expand_SPARK_N_Attribute_Reference --
171 ----------------------------------------
172
173 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id) is
174 Aname : constant Name_Id := Attribute_Name (N);
175 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
176 Loc : constant Source_Ptr := Sloc (N);
177 Typ : constant Entity_Id := Etype (N);
178 Expr : Node_Id;
179
180 begin
181 if Attr_Id = Attribute_To_Address then
182
183 -- Extract and convert argument to expected type for call
184
185 Expr :=
186 Make_Type_Conversion (Loc,
187 Subtype_Mark =>
188 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
189 Expression => Relocate_Node (First (Expressions (N))));
190
191 -- Replace attribute reference with call
192
193 Rewrite (N,
194 Make_Function_Call (Loc,
195 Name =>
196 New_Occurrence_Of (RTE (RE_To_Address), Loc),
197 Parameter_Associations => New_List (Expr)));
198 Analyze_And_Resolve (N, Typ);
199
200 -- Whenever possible, replace a prefix which is an enumeration literal
201 -- by the corresponding literal value.
202
203 elsif Attr_Id = Attribute_Enum_Rep then
204 declare
205 Exprs : constant List_Id := Expressions (N);
206 begin
207 if Is_Non_Empty_List (Exprs) then
208 Expr := First (Exprs);
209 else
210 Expr := Prefix (N);
211 end if;
212
213 -- If the argument is a literal, expand it
214
215 if Nkind (Expr) in N_Has_Entity
216 and then
217 (Ekind (Entity (Expr)) = E_Enumeration_Literal
218 or else
219 (Nkind (Expr) in N_Has_Entity
220 and then Ekind (Entity (Expr)) = E_Constant
221 and then Present (Renamed_Object (Entity (Expr)))
222 and then Is_Entity_Name (Renamed_Object (Entity (Expr)))
223 and then Ekind (Entity (Renamed_Object (Entity (Expr)))) =
224 E_Enumeration_Literal))
225 then
226 Exp_Attr.Expand_N_Attribute_Reference (N);
227 end if;
228 end;
229
230 -- For attributes which return Universal_Integer, introduce a conversion
231 -- to the expected type with the appropriate check flags set.
232
233 elsif Attr_Id = Attribute_Alignment
234 or else Attr_Id = Attribute_Bit
235 or else Attr_Id = Attribute_Bit_Position
236 or else Attr_Id = Attribute_Descriptor_Size
237 or else Attr_Id = Attribute_First_Bit
238 or else Attr_Id = Attribute_Last_Bit
239 or else Attr_Id = Attribute_Length
240 or else Attr_Id = Attribute_Max_Size_In_Storage_Elements
241 or else Attr_Id = Attribute_Pos
242 or else Attr_Id = Attribute_Position
243 or else Attr_Id = Attribute_Range_Length
244 or else Attr_Id = Attribute_Object_Size
245 or else Attr_Id = Attribute_Size
246 or else Attr_Id = Attribute_Value_Size
247 or else Attr_Id = Attribute_VADS_Size
248 or else Attr_Id = Attribute_Aft
249 or else Attr_Id = Attribute_Max_Alignment_For_Allocation
250 then
251 -- If the expected type is Long_Long_Integer, there will be no check
252 -- flag as the compiler assumes attributes always fit in this type.
253 -- Since in SPARK_Mode we do not take Storage_Error into account, we
254 -- cannot make this assumption and need to produce a check.
255 -- ??? It should be enough to add this check for attributes 'Length
256 -- and 'Range_Length when the type is as big as Long_Long_Integer.
257
258 declare
259 Typ : Entity_Id := Empty;
260 begin
261 if Attr_Id = Attribute_Range_Length then
262 Typ := Etype (Prefix (N));
263
264 elsif Attr_Id = Attribute_Length then
265 Typ := Etype (Prefix (N));
266
267 declare
268 Indx : Node_Id;
269 J : Int;
270
271 begin
272 if Is_Access_Type (Typ) then
273 Typ := Designated_Type (Typ);
274 end if;
275
276 if No (Expressions (N)) then
277 J := 1;
278 else
279 J := UI_To_Int (Expr_Value (First (Expressions (N))));
280 end if;
281
282 Indx := First_Index (Typ);
283 while J > 1 loop
284 Next_Index (Indx);
285 J := J - 1;
286 end loop;
287
288 Typ := Etype (Indx);
289 end;
290 end if;
291
292 Apply_Universal_Integer_Attribute_Checks (N);
293
294 if Present (Typ)
295 and then RM_Size (Typ) = RM_Size (Standard_Long_Long_Integer)
296 then
297 Set_Do_Overflow_Check (N);
298 end if;
299 end;
300 end if;
301 end Expand_SPARK_N_Attribute_Reference;
302
303 -----------------------------------
304 -- Expand_SPARK_N_Loop_Statement --
305 -----------------------------------
306
307 procedure Expand_SPARK_N_Loop_Statement (N : Node_Id) is
308 Scheme : constant Node_Id := Iteration_Scheme (N);
309
310 begin
311 -- Loop iterations over arrays need to be expanded, to avoid getting
312 -- two names referring to the same object in memory (the array and the
313 -- iterator) in GNATprove, especially since both can be written (thus
314 -- possibly leading to interferences due to aliasing). No such problem
315 -- arises with quantified expressions over arrays, which are dealt with
316 -- specially in GNATprove.
317
318 if Present (Scheme)
319 and then Present (Iterator_Specification (Scheme))
320 and then Is_Iterator_Over_Array (Iterator_Specification (Scheme))
321 then
322 Expand_Iterator_Loop_Over_Array (N);
323 end if;
324 end Expand_SPARK_N_Loop_Statement;
325
326 --------------------------------------
327 -- Expand_SPARK_N_Indexed_Component --
328 --------------------------------------
329
330 procedure Expand_SPARK_N_Indexed_Component (N : Node_Id) is
331 Pref : constant Node_Id := Prefix (N);
332 Typ : constant Entity_Id := Etype (Pref);
333
334 begin
335 if Is_Access_Type (Typ) then
336 Insert_Explicit_Dereference (Pref);
337 Analyze_And_Resolve (Pref, Designated_Type (Typ));
338 end if;
339 end Expand_SPARK_N_Indexed_Component;
340
341 ---------------------------------------
342 -- Expand_SPARK_N_Object_Declaration --
343 ---------------------------------------
344
345 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id) is
346 Loc : constant Source_Ptr := Sloc (N);
347 Obj_Id : constant Entity_Id := Defining_Identifier (N);
348 Typ : constant Entity_Id := Etype (Obj_Id);
349
350 Call : Node_Id;
351
352 begin
353 -- If the object declaration denotes a variable without initialization
354 -- whose type is subject to pragma Default_Initial_Condition, create
355 -- and analyze a dummy call to the DIC procedure of the type in order
356 -- to detect potential elaboration issues.
357
358 if Comes_From_Source (Obj_Id)
359 and then Ekind (Obj_Id) = E_Variable
360 and then Has_DIC (Typ)
361 and then Present (DIC_Procedure (Typ))
362 and then not Has_Init_Expression (N)
363 then
364 Call := Build_DIC_Call (Loc, Obj_Id, Typ);
365
366 -- Partially insert the call into the tree by setting its parent
367 -- pointer.
368
369 Set_Parent (Call, N);
370 Analyze (Call);
371 end if;
372 end Expand_SPARK_N_Object_Declaration;
373
374 ------------------------------------------------
375 -- Expand_SPARK_N_Object_Renaming_Declaration --
376 ------------------------------------------------
377
378 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
379 CFS : constant Boolean := Comes_From_Source (N);
380 Loc : constant Source_Ptr := Sloc (N);
381 Obj_Id : constant Entity_Id := Defining_Entity (N);
382 Nam : constant Node_Id := Name (N);
383 Typ : constant Entity_Id := Etype (Obj_Id);
384
385 begin
386 -- Transform a renaming of the form
387
388 -- Obj_Id : <subtype mark> renames <function call>;
389
390 -- into
391
392 -- Obj_Id : constant <subtype mark> := <function call>;
393
394 -- Invoking Evaluate_Name and ultimately Remove_Side_Effects introduces
395 -- a temporary to capture the function result. Once potential renamings
396 -- are rewritten for SPARK, the temporary may be leaked out into source
397 -- constructs and lead to confusing error diagnostics. Using an object
398 -- declaration prevents this unwanted side effect.
399
400 if Nkind (Nam) = N_Function_Call then
401 Rewrite (N,
402 Make_Object_Declaration (Loc,
403 Defining_Identifier => Obj_Id,
404 Constant_Present => True,
405 Object_Definition => New_Occurrence_Of (Typ, Loc),
406 Expression => Nam));
407
408 -- Inherit the original Comes_From_Source status of the renaming
409
410 Set_Comes_From_Source (N, CFS);
411
412 -- Sever the link to the renamed function result because the entity
413 -- will no longer alias anything.
414
415 Set_Renamed_Object (Obj_Id, Empty);
416
417 -- Remove the entity of the renaming declaration from visibility as
418 -- the analysis of the object declaration will reintroduce it again.
419
420 Remove_Entity_And_Homonym (Obj_Id);
421 Analyze (N);
422
423 -- Otherwise unconditionally remove all side effects from the name
424
425 else
426 Evaluate_Name (Nam);
427 end if;
428 end Expand_SPARK_N_Object_Renaming_Declaration;
429
430 --------------------------
431 -- Expand_SPARK_N_Op_Ne --
432 --------------------------
433
434 procedure Expand_SPARK_N_Op_Ne (N : Node_Id) is
435 Typ : constant Entity_Id := Etype (Left_Opnd (N));
436
437 begin
438 -- Case of elementary type with standard operator
439
440 if Is_Elementary_Type (Typ)
441 and then Sloc (Entity (N)) = Standard_Location
442 then
443 null;
444
445 else
446 Exp_Ch4.Expand_N_Op_Ne (N);
447 end if;
448 end Expand_SPARK_N_Op_Ne;
449
450 -------------------------------------
451 -- Expand_SPARK_Potential_Renaming --
452 -------------------------------------
453
454 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
455 function In_Insignificant_Pragma (Nod : Node_Id) return Boolean;
456 -- Determine whether arbitrary node Nod appears within a significant
457 -- pragma for SPARK.
458
459 -----------------------------
460 -- In_Insignificant_Pragma --
461 -----------------------------
462
463 function In_Insignificant_Pragma (Nod : Node_Id) return Boolean is
464 Par : Node_Id;
465
466 begin
467 -- Climb the parent chain looking for an enclosing pragma
468
469 Par := Nod;
470 while Present (Par) loop
471 if Nkind (Par) = N_Pragma then
472 return not Pragma_Significant_In_SPARK (Get_Pragma_Id (Par));
473
474 -- Prevent the search from going too far
475
476 elsif Is_Body_Or_Package_Declaration (Par) then
477 exit;
478 end if;
479
480 Par := Parent (Par);
481 end loop;
482
483 return False;
484 end In_Insignificant_Pragma;
485
486 -- Local variables
487
488 Loc : constant Source_Ptr := Sloc (N);
489 Obj_Id : constant Entity_Id := Entity (N);
490 Typ : constant Entity_Id := Etype (N);
491 Ren : Node_Id;
492
493 -- Start of processing for Expand_SPARK_Potential_Renaming
494
495 begin
496 -- Replace a reference to a renaming with the actual renamed object
497
498 if Ekind (Obj_Id) in Object_Kind then
499 Ren := Renamed_Object (Obj_Id);
500
501 if Present (Ren) then
502
503 -- Do not process a reference when it appears within a pragma of
504 -- no significance to SPARK. It is assumed that the replacement
505 -- will violate the semantics of the pragma and cause a spurious
506 -- error.
507
508 if In_Insignificant_Pragma (N) then
509 return;
510
511 -- Instantiations and inlining of subprograms employ "prologues"
512 -- which map actual to formal parameters by means of renamings.
513 -- Replace a reference to a formal by the corresponding actual
514 -- parameter.
515
516 elsif Nkind (Ren) in N_Entity then
517 Rewrite (N, New_Occurrence_Of (Ren, Loc));
518
519 -- Otherwise the renamed object denotes a name
520
521 else
522 Rewrite (N, New_Copy_Tree (Ren, New_Sloc => Loc));
523 Reset_Analyzed_Flags (N);
524 end if;
525
526 Analyze_And_Resolve (N, Typ);
527 end if;
528 end if;
529 end Expand_SPARK_Potential_Renaming;
530
531 ---------------------------------------
532 -- Expand_SPARK_N_Selected_Component --
533 ---------------------------------------
534
535 procedure Expand_SPARK_N_Selected_Component (N : Node_Id) is
536 Pref : constant Node_Id := Prefix (N);
537 Typ : constant Entity_Id := Underlying_Type (Etype (Pref));
538
539 begin
540 if Present (Typ) and then Is_Access_Type (Typ) then
541
542 -- First set prefix type to proper access type, in case it currently
543 -- has a private (non-access) view of this type.
544
545 Set_Etype (Pref, Typ);
546
547 Insert_Explicit_Dereference (Pref);
548 Analyze_And_Resolve (Pref, Designated_Type (Typ));
549 end if;
550 end Expand_SPARK_N_Selected_Component;
551
552 end Exp_SPARK;