2012-08-15 Segher Boessenkool <segher@kernel.crashing.org>
[official-gcc.git] / gcc / ada / exp_alfa.adb
blob2a640fd542315c33ae6c099f8742dc7c4e3c746a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A L F A --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Exp_Attr; use Exp_Attr;
29 with Exp_Ch4; use Exp_Ch4;
30 with Exp_Ch6; use Exp_Ch6;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Nlists; use Nlists;
34 with Rtsfind; use Rtsfind;
35 with Sem_Aux; use Sem_Aux;
36 with Sem_Res; use Sem_Res;
37 with Sem_Util; use Sem_Util;
38 with Sinfo; use Sinfo;
39 with Snames; use Snames;
40 with Stand; use Stand;
41 with Tbuild; use Tbuild;
43 package body Exp_Alfa is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 procedure Expand_Alfa_Call (N : Node_Id);
50 -- This procedure contains common processing for function and procedure
51 -- calls:
52 -- * expansion of actuals to introduce necessary temporaries
53 -- * replacement of renaming by subprogram renamed
55 procedure Expand_Alfa_N_Attribute_Reference (N : Node_Id);
56 -- Expand attributes 'Old and 'Result only
58 procedure Expand_Alfa_N_In (N : Node_Id);
59 -- Expand set membership into individual ones
61 procedure Expand_Alfa_N_Object_Renaming_Declaration (N : Node_Id);
62 -- Perform name evaluation for a renamed object
64 procedure Expand_Alfa_N_Simple_Return_Statement (N : Node_Id);
65 -- Insert conversion on function return if necessary
67 procedure Expand_Alfa_Simple_Function_Return (N : Node_Id);
68 -- Expand simple return from function
70 procedure Expand_Potential_Renaming (N : Node_Id);
71 -- N denotes a N_Identifier or N_Expanded_Name. If N references a renaming,
72 -- replace N with the renamed object.
74 -----------------
75 -- Expand_Alfa --
76 -----------------
78 procedure Expand_Alfa (N : Node_Id) is
79 begin
80 case Nkind (N) is
81 when N_Attribute_Reference =>
82 Expand_Alfa_N_Attribute_Reference (N);
84 when N_Block_Statement |
85 N_Package_Body |
86 N_Package_Declaration |
87 N_Subprogram_Body =>
88 Qualify_Entity_Names (N);
90 when N_Subprogram_Call =>
91 Expand_Alfa_Call (N);
93 when N_Expanded_Name |
94 N_Identifier =>
95 Expand_Potential_Renaming (N);
97 when N_In =>
98 Expand_Alfa_N_In (N);
100 when N_Not_In =>
101 Expand_N_Not_In (N);
103 when N_Object_Renaming_Declaration =>
104 Expand_Alfa_N_Object_Renaming_Declaration (N);
106 when N_Simple_Return_Statement =>
107 Expand_Alfa_N_Simple_Return_Statement (N);
109 when others =>
110 null;
111 end case;
112 end Expand_Alfa;
114 ----------------------
115 -- Expand_Alfa_Call --
116 ----------------------
118 procedure Expand_Alfa_Call (N : Node_Id) is
119 Call_Node : constant Node_Id := N;
120 Parent_Subp : Entity_Id;
121 Subp : Entity_Id;
123 begin
124 -- Ignore if previous error
126 if Nkind (Call_Node) in N_Has_Etype
127 and then Etype (Call_Node) = Any_Type
128 then
129 return;
130 end if;
132 -- Call using access to subprogram with explicit dereference
134 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
135 Subp := Etype (Name (Call_Node));
136 Parent_Subp := Empty;
138 -- Case of call to simple entry, where the Name is a selected component
139 -- whose prefix is the task, and whose selector name is the entry name
141 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
142 Subp := Entity (Selector_Name (Name (Call_Node)));
143 Parent_Subp := Empty;
145 -- Case of call to member of entry family, where Name is an indexed
146 -- component, with the prefix being a selected component giving the
147 -- task and entry family name, and the index being the entry index.
149 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
150 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
151 Parent_Subp := Empty;
153 -- Normal case
155 else
156 Subp := Entity (Name (Call_Node));
157 Parent_Subp := Alias (Subp);
158 end if;
160 -- Various expansion activities for actuals are carried out
162 Expand_Actuals (N, Subp);
164 -- If the subprogram is a renaming, replace it in the call with the name
165 -- of the actual subprogram being called.
167 if Present (Parent_Subp) then
168 Parent_Subp := Ultimate_Alias (Parent_Subp);
170 -- The below setting of Entity is suspect, see F109-018 discussion???
172 Set_Entity (Name (Call_Node), Parent_Subp);
173 end if;
174 end Expand_Alfa_Call;
176 ---------------------------------------
177 -- Expand_Alfa_N_Attribute_Reference --
178 ---------------------------------------
180 procedure Expand_Alfa_N_Attribute_Reference (N : Node_Id) is
181 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
183 begin
184 case Id is
185 when Attribute_Old |
186 Attribute_Result =>
187 Expand_N_Attribute_Reference (N);
189 when others =>
190 null;
191 end case;
192 end Expand_Alfa_N_Attribute_Reference;
194 ----------------------
195 -- Expand_Alfa_N_In --
196 ----------------------
198 procedure Expand_Alfa_N_In (N : Node_Id) is
199 begin
200 if Present (Alternatives (N)) then
201 Expand_Set_Membership (N);
202 end if;
203 end Expand_Alfa_N_In;
205 -----------------------------------------------
206 -- Expand_Alfa_N_Object_Renaming_Declaration --
207 -----------------------------------------------
209 procedure Expand_Alfa_N_Object_Renaming_Declaration (N : Node_Id) is
210 begin
211 -- Unconditionally remove all side effects from the name
213 Evaluate_Name (Name (N));
214 end Expand_Alfa_N_Object_Renaming_Declaration;
216 -------------------------------------------
217 -- Expand_Alfa_N_Simple_Return_Statement --
218 -------------------------------------------
220 procedure Expand_Alfa_N_Simple_Return_Statement (N : Node_Id) is
221 begin
222 -- Defend against previous errors (i.e. the return statement calls a
223 -- function that is not available in configurable runtime).
225 if Present (Expression (N))
226 and then Nkind (Expression (N)) = N_Empty
227 then
228 return;
229 end if;
231 -- Distinguish the function and non-function cases:
233 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
235 when E_Function |
236 E_Generic_Function =>
237 Expand_Alfa_Simple_Function_Return (N);
239 when E_Procedure |
240 E_Generic_Procedure |
241 E_Entry |
242 E_Entry_Family |
243 E_Return_Statement =>
244 null;
246 when others =>
247 raise Program_Error;
248 end case;
250 exception
251 when RE_Not_Available =>
252 return;
253 end Expand_Alfa_N_Simple_Return_Statement;
255 ----------------------------------------
256 -- Expand_Alfa_Simple_Function_Return --
257 ----------------------------------------
259 procedure Expand_Alfa_Simple_Function_Return (N : Node_Id) is
260 Scope_Id : constant Entity_Id :=
261 Return_Applies_To (Return_Statement_Entity (N));
262 -- The function we are returning from
264 R_Type : constant Entity_Id := Etype (Scope_Id);
265 -- The result type of the function
267 Exp : constant Node_Id := Expression (N);
268 pragma Assert (Present (Exp));
270 Exptyp : constant Entity_Id := Etype (Exp);
271 -- The type of the expression (not necessarily the same as R_Type)
273 begin
274 -- Check the result expression of a scalar function against the subtype
275 -- of the function by inserting a conversion. This conversion must
276 -- eventually be performed for other classes of types, but for now it's
277 -- only done for scalars.
278 -- ???
280 if Is_Scalar_Type (Exptyp) then
281 Rewrite (Exp, Convert_To (R_Type, Exp));
283 -- The expression is resolved to ensure that the conversion gets
284 -- expanded to generate a possible constraint check.
286 Analyze_And_Resolve (Exp, R_Type);
287 end if;
288 end Expand_Alfa_Simple_Function_Return;
290 -------------------------------
291 -- Expand_Potential_Renaming --
292 -------------------------------
294 procedure Expand_Potential_Renaming (N : Node_Id) is
295 E : constant Entity_Id := Entity (N);
296 T : constant Entity_Id := Etype (N);
298 begin
299 -- Replace a reference to a renaming with the actual renamed object
301 if Ekind (E) in Object_Kind and then Present (Renamed_Object (E)) then
302 Rewrite (N, New_Copy_Tree (Renamed_Object (E)));
303 Reset_Analyzed_Flags (N);
304 Analyze_And_Resolve (N, T);
305 end if;
306 end Expand_Potential_Renaming;
308 end Exp_Alfa;