04c8484cb0ccc59cc2075c76642c36d8383e2056
[gcc.git] / gcc / ada / exp_alfa.adb
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-2011, 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_Attr; use Exp_Attr;
29 with Exp_Ch6; use Exp_Ch6;
30 with Exp_Dbug; use Exp_Dbug;
31 with Rtsfind; use Rtsfind;
32 with Sem_Aux; use Sem_Aux;
33 with Sem_Res; use Sem_Res;
34 with Sinfo; use Sinfo;
35 with Snames; use Snames;
36 with Stand; use Stand;
37 with Tbuild; use Tbuild;
38
39 package body Exp_Alfa is
40
41 -----------------------
42 -- Local Subprograms --
43 -----------------------
44
45 procedure Expand_Alfa_Call (N : Node_Id);
46 -- This procedure contains common processing for function and procedure
47 -- calls:
48 -- * expansion of actuals to introduce necessary temporaries
49 -- * replacement of renaming by subprogram renamed
50
51 procedure Expand_Alfa_N_Attribute_Reference (N : Node_Id);
52 -- Expand attributes 'Old and 'Result only
53
54 procedure Expand_Alfa_N_Simple_Return_Statement (N : Node_Id);
55 -- Insert conversion on function return if necessary
56
57 procedure Expand_Alfa_Simple_Function_Return (N : Node_Id);
58 -- Expand simple return from function
59
60 -----------------
61 -- Expand_Alfa --
62 -----------------
63
64 procedure Expand_Alfa (N : Node_Id) is
65 begin
66 case Nkind (N) is
67
68 when N_Package_Body |
69 N_Package_Declaration |
70 N_Subprogram_Body |
71 N_Block_Statement =>
72 Qualify_Entity_Names (N);
73
74 when N_Simple_Return_Statement =>
75 Expand_Alfa_N_Simple_Return_Statement (N);
76
77 when N_Function_Call |
78 N_Procedure_Call_Statement =>
79 Expand_Alfa_Call (N);
80
81 when N_Attribute_Reference =>
82 Expand_Alfa_N_Attribute_Reference (N);
83
84 when others =>
85 null;
86 end case;
87 end Expand_Alfa;
88
89 ----------------------
90 -- Expand_Alfa_Call --
91 ----------------------
92
93 procedure Expand_Alfa_Call (N : Node_Id) is
94 Call_Node : constant Node_Id := N;
95 Parent_Subp : Entity_Id;
96 Subp : Entity_Id;
97
98 begin
99 -- Ignore if previous error
100
101 if Nkind (Call_Node) in N_Has_Etype
102 and then Etype (Call_Node) = Any_Type
103 then
104 return;
105 end if;
106
107 -- Call using access to subprogram with explicit dereference
108
109 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
110 Subp := Etype (Name (Call_Node));
111 Parent_Subp := Empty;
112
113 -- Case of call to simple entry, where the Name is a selected component
114 -- whose prefix is the task, and whose selector name is the entry name
115
116 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
117 Subp := Entity (Selector_Name (Name (Call_Node)));
118 Parent_Subp := Empty;
119
120 -- Case of call to member of entry family, where Name is an indexed
121 -- component, with the prefix being a selected component giving the
122 -- task and entry family name, and the index being the entry index.
123
124 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
125 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
126 Parent_Subp := Empty;
127
128 -- Normal case
129
130 else
131 Subp := Entity (Name (Call_Node));
132 Parent_Subp := Alias (Subp);
133 end if;
134
135 -- Various expansion activities for actuals are carried out
136
137 Expand_Actuals (N, Subp);
138
139 -- If the subprogram is a renaming, replace it in the call with the name
140 -- of the actual subprogram being called.
141
142 if Present (Parent_Subp) then
143 Parent_Subp := Ultimate_Alias (Parent_Subp);
144
145 -- The below setting of Entity is suspect, see F109-018 discussion???
146
147 Set_Entity (Name (Call_Node), Parent_Subp);
148 end if;
149
150 end Expand_Alfa_Call;
151
152 ---------------------------------------
153 -- Expand_Alfa_N_Attribute_Reference --
154 ---------------------------------------
155
156 procedure Expand_Alfa_N_Attribute_Reference (N : Node_Id) is
157 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
158
159 begin
160 case Id is
161 when Attribute_Old |
162 Attribute_Result =>
163 Expand_N_Attribute_Reference (N);
164
165 when others =>
166 null;
167 end case;
168 end Expand_Alfa_N_Attribute_Reference;
169
170 -------------------------------------------
171 -- Expand_Alfa_N_Simple_Return_Statement --
172 -------------------------------------------
173
174 procedure Expand_Alfa_N_Simple_Return_Statement (N : Node_Id) is
175 begin
176 -- Defend against previous errors (i.e. the return statement calls a
177 -- function that is not available in configurable runtime).
178
179 if Present (Expression (N))
180 and then Nkind (Expression (N)) = N_Empty
181 then
182 return;
183 end if;
184
185 -- Distinguish the function and non-function cases:
186
187 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
188
189 when E_Function |
190 E_Generic_Function =>
191 Expand_Alfa_Simple_Function_Return (N);
192
193 when E_Procedure |
194 E_Generic_Procedure |
195 E_Entry |
196 E_Entry_Family |
197 E_Return_Statement =>
198 -- Expand_Non_Function_Return (N);
199 null;
200
201 when others =>
202 raise Program_Error;
203 end case;
204
205 exception
206 when RE_Not_Available =>
207 return;
208 end Expand_Alfa_N_Simple_Return_Statement;
209
210 ----------------------------------------
211 -- Expand_Alfa_Simple_Function_Return --
212 ----------------------------------------
213
214 procedure Expand_Alfa_Simple_Function_Return (N : Node_Id) is
215 Scope_Id : constant Entity_Id :=
216 Return_Applies_To (Return_Statement_Entity (N));
217 -- The function we are returning from
218
219 R_Type : constant Entity_Id := Etype (Scope_Id);
220 -- The result type of the function
221
222 Exp : constant Node_Id := Expression (N);
223 pragma Assert (Present (Exp));
224
225 Exptyp : constant Entity_Id := Etype (Exp);
226 -- The type of the expression (not necessarily the same as R_Type)
227
228 begin
229 -- Check the result expression of a scalar function against the subtype
230 -- of the function by inserting a conversion. This conversion must
231 -- eventually be performed for other classes of types, but for now it's
232 -- only done for scalars.
233 -- ???
234
235 if Is_Scalar_Type (Exptyp) then
236 Rewrite (Exp, Convert_To (R_Type, Exp));
237
238 -- The expression is resolved to ensure that the conversion gets
239 -- expanded to generate a possible constraint check.
240
241 Analyze_And_Resolve (Exp, R_Type);
242 end if;
243 end Expand_Alfa_Simple_Function_Return;
244
245 end Exp_Alfa;