sem_aggr.adb, [...]: Update all eligible case statements to reflect the new style...
[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-2016, 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 Einfo; use Einfo;
28 with Exp_Ch5; use Exp_Ch5;
29 with Exp_Dbug; use Exp_Dbug;
30 with Exp_Util; use Exp_Util;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Rtsfind; use Rtsfind;
35 with Sem_Res; use Sem_Res;
36 with Sem_Util; use Sem_Util;
37 with Sinfo; use Sinfo;
38 with Snames; use Snames;
39 with Tbuild; use Tbuild;
40
41 package body Exp_SPARK is
42
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
46
47 procedure Expand_SPARK_Attribute_Reference (N : Node_Id);
48 -- Replace occurrences of System'To_Address by calls to
49 -- System.Storage_Elements.To_Address
50
51 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
52 -- Perform name evaluation for a renamed object
53
54 ------------------
55 -- Expand_SPARK --
56 ------------------
57
58 procedure Expand_SPARK (N : Node_Id) is
59 begin
60 case Nkind (N) is
61
62 -- Qualification of entity names in formal verification mode
63 -- is limited to the addition of a suffix for homonyms (see
64 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
65 -- as full expansion does, but this was removed as this prevents the
66 -- verification back-end from using a short name for debugging and
67 -- user interaction. The verification back-end already takes care
68 -- of qualifying names when needed.
69
70 when N_Block_Statement
71 | N_Entry_Declaration
72 | N_Package_Body
73 | N_Package_Declaration
74 | N_Protected_Type_Declaration
75 | N_Subprogram_Body
76 | N_Task_Type_Declaration
77 =>
78 Qualify_Entity_Names (N);
79
80 when N_Expanded_Name
81 | N_Identifier
82 =>
83 Expand_SPARK_Potential_Renaming (N);
84
85 when N_Object_Renaming_Declaration =>
86 Expand_SPARK_N_Object_Renaming_Declaration (N);
87
88 -- Replace occurrences of System'To_Address by calls to
89 -- System.Storage_Elements.To_Address
90
91 when N_Attribute_Reference =>
92 Expand_SPARK_Attribute_Reference (N);
93
94 -- Loop iterations over arrays need to be expanded, to avoid getting
95 -- two names referring to the same object in memory (the array and
96 -- the iterator) in GNATprove, especially since both can be written
97 -- (thus possibly leading to interferences due to aliasing). No such
98 -- problem arises with quantified expressions over arrays, which are
99 -- dealt with specially in GNATprove.
100
101 when N_Loop_Statement =>
102 declare
103 Scheme : constant Node_Id := Iteration_Scheme (N);
104 begin
105 if Present (Scheme)
106 and then Present (Iterator_Specification (Scheme))
107 and then
108 Is_Iterator_Over_Array (Iterator_Specification (Scheme))
109 then
110 Expand_Iterator_Loop_Over_Array (N);
111 end if;
112 end;
113
114 -- In SPARK mode, no other constructs require expansion
115
116 when others =>
117 null;
118 end case;
119 end Expand_SPARK;
120
121 --------------------------------------
122 -- Expand_SPARK_Attribute_Reference --
123 --------------------------------------
124
125 procedure Expand_SPARK_Attribute_Reference (N : Node_Id) is
126 Aname : constant Name_Id := Attribute_Name (N);
127 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
128 Loc : constant Source_Ptr := Sloc (N);
129 Typ : constant Entity_Id := Etype (N);
130 Expr : Node_Id;
131
132 begin
133 if Attr_Id = Attribute_To_Address then
134
135 -- Extract and convert argument to expected type for call
136
137 Expr :=
138 Make_Type_Conversion (Loc,
139 Subtype_Mark =>
140 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
141 Expression => Relocate_Node (First (Expressions (N))));
142
143 -- Replace attribute reference with call
144
145 Rewrite (N,
146 Make_Function_Call (Loc,
147 Name =>
148 New_Occurrence_Of (RTE (RE_To_Address), Loc),
149 Parameter_Associations => New_List (Expr)));
150 Analyze_And_Resolve (N, Typ);
151 end if;
152 end Expand_SPARK_Attribute_Reference;
153
154 ------------------------------------------------
155 -- Expand_SPARK_N_Object_Renaming_Declaration --
156 ------------------------------------------------
157
158 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
159 begin
160 -- Unconditionally remove all side effects from the name
161
162 Evaluate_Name (Name (N));
163 end Expand_SPARK_N_Object_Renaming_Declaration;
164
165 -------------------------------------
166 -- Expand_SPARK_Potential_Renaming --
167 -------------------------------------
168
169 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
170 Loc : constant Source_Ptr := Sloc (N);
171 Ren_Id : constant Entity_Id := Entity (N);
172 Typ : constant Entity_Id := Etype (N);
173 Obj_Id : Node_Id;
174
175 begin
176 -- Replace a reference to a renaming with the actual renamed object
177
178 if Ekind (Ren_Id) in Object_Kind then
179 Obj_Id := Renamed_Object (Ren_Id);
180
181 if Present (Obj_Id) then
182
183 -- The renamed object is an entity when instantiating generics
184 -- or inlining bodies. In this case the renaming is part of the
185 -- mapping "prologue" which links actuals to formals.
186
187 if Nkind (Obj_Id) in N_Entity then
188 Rewrite (N, New_Occurrence_Of (Obj_Id, Loc));
189
190 -- Otherwise the renamed object denotes a name
191
192 else
193 Rewrite (N, New_Copy_Tree (Obj_Id, New_Sloc => Loc));
194 Reset_Analyzed_Flags (N);
195 end if;
196
197 Analyze_And_Resolve (N, Typ);
198 end if;
199 end if;
200 end Expand_SPARK_Potential_Renaming;
201
202 end Exp_SPARK;