]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/expr.c
expr.c (cplus_expand_expr, [...]): Call convert_from_reference sooner.
[gcc.git] / gcc / cp / expr.c
1 /* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include <stdio.h>
25 #include "rtl.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "expr.h"
29 #include "cp-tree.h"
30
31 static tree extract_aggr_init PROTO((tree, tree));
32 static tree extract_scalar_init PROTO((tree, tree));
33 static rtx cplus_expand_expr PROTO((tree, rtx, enum machine_mode,
34 enum expand_modifier));
35
36 /* Hook used by expand_expr to expand language-specific tree codes. */
37
38 static rtx
39 cplus_expand_expr (exp, target, tmode, modifier)
40 tree exp;
41 rtx target;
42 enum machine_mode tmode;
43 enum expand_modifier modifier;
44 {
45 tree type = TREE_TYPE (exp);
46 register enum machine_mode mode = TYPE_MODE (type);
47 register enum tree_code code = TREE_CODE (exp);
48 int ignore = target == const0_rtx;
49
50 if (ignore)
51 target = 0;
52
53 /* No sense saving up arithmetic to be done
54 if it's all in the wrong mode to form part of an address.
55 And force_operand won't know whether to sign-extend or zero-extend. */
56
57 if (mode != Pmode && modifier == EXPAND_SUM)
58 modifier = EXPAND_NORMAL;
59
60 switch (code)
61 {
62 case AGGR_INIT_EXPR:
63 {
64 /* Something needs to be initialized, but we didn't know
65 where that thing was when building the tree. For example,
66 it could be the return value of a function, or a parameter
67 to a function which lays down in the stack, or a temporary
68 variable which must be passed by reference.
69
70 Cleanups are handled in a language-specific way: they
71 might be run by the called function (true in GNU C++
72 for parameters with cleanups), or they might be
73 run by the caller, after the call (true in GNU C++
74 for other cleanup needs). */
75
76 tree func = TREE_OPERAND (exp, 0);
77 tree args = TREE_OPERAND (exp, 1);
78 tree type = TREE_TYPE (exp), slot;
79 tree call_exp;
80 rtx call_target, return_target;
81 int pcc_struct_return = 0;
82
83 /* The expression `init' wants to initialize what
84 `target' represents. SLOT holds the slot for TARGET. */
85 slot = TREE_OPERAND (exp, 2);
86
87 /* Should always be called with a target. */
88 my_friendly_assert (target != NULL_RTX, 205);
89
90 /* The target the initializer will initialize (CALL_TARGET)
91 must now be directed to initialize the target we are
92 supposed to initialize (TARGET). The semantics for
93 choosing what CALL_TARGET is is language-specific,
94 as is building the call which will perform the
95 initialization. It is left here to show the choices that
96 exist for C++. */
97
98 if (TREE_CODE (func) == ADDR_EXPR
99 && TREE_CODE (TREE_OPERAND (func, 0)) == FUNCTION_DECL
100 && DECL_CONSTRUCTOR_P (TREE_OPERAND (func, 0)))
101 {
102 type = build_pointer_type (type);
103 /* Don't clobber a value that might be part of a default
104 parameter value. */
105 mark_addressable (slot);
106 if (TREE_PERMANENT (args))
107 args = expr_tree_cons (0, build1 (ADDR_EXPR, type, slot),
108 TREE_CHAIN (args));
109 else
110 TREE_VALUE (args) = build1 (ADDR_EXPR, type, slot);
111 call_target = 0;
112 }
113 else
114 {
115 #ifdef PCC_STATIC_STRUCT_RETURN
116 pcc_struct_return = 1;
117 call_target = 0;
118 #else
119 call_target = target;
120 #endif
121 }
122
123 call_exp = build (CALL_EXPR, type, func, args, NULL_TREE);
124 TREE_SIDE_EFFECTS (call_exp) = 1;
125 return_target = expand_call (call_exp, call_target, ignore);
126
127 if (call_target)
128 /* Trust that the right thing has been done; it's too hard to
129 verify. */
130 return return_target;
131
132 /* If we're suffering under the ancient PCC_STATIC_STRUCT_RETURN
133 calling convention, we need to copy the return value out of
134 the static return buffer into slot. */
135 if (pcc_struct_return)
136 {
137 extern int flag_access_control;
138 int old_ac = flag_access_control;
139
140 tree init = build_decl (VAR_DECL, NULL_TREE,
141 build_reference_type (type));
142 DECL_RTL (init) = XEXP (return_target, 0);
143 init = convert_from_reference (init);
144
145 flag_access_control = 0;
146 expand_aggr_init (slot, init, 0, LOOKUP_ONLYCONVERTING);
147 flag_access_control = old_ac;
148
149 if (TYPE_NEEDS_DESTRUCTOR (type))
150 {
151 init = maybe_build_cleanup (init);
152 if (init != NULL_TREE)
153 expand_expr (init, const0_rtx, VOIDmode, 0);
154 }
155 }
156
157 return DECL_RTL (slot);
158 }
159
160 case OFFSET_REF:
161 {
162 #if 1
163 return expand_expr (default_conversion (resolve_offset_ref (exp)),
164 target, tmode, EXPAND_NORMAL);
165 #else
166 /* This is old crusty code, and does not handle all that the
167 resolve_offset_ref function does. (mrs) */
168 tree base = build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 0), 0);
169 tree offset = build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
170 return expand_expr (build (PLUS_EXPR, TREE_TYPE (exp), base, offset),
171 target, tmode, EXPAND_NORMAL);
172 #endif
173 }
174
175 case THUNK_DECL:
176 return DECL_RTL (exp);
177
178 case THROW_EXPR:
179 expand_throw (TREE_OPERAND (exp, 0));
180 return NULL;
181
182 case VEC_INIT_EXPR:
183 return expand_expr
184 (expand_vec_init
185 (NULL_TREE, TREE_OPERAND (exp, 0),
186 build_binary_op (MINUS_EXPR, TREE_OPERAND (exp, 2),
187 integer_one_node, 1),
188 TREE_OPERAND (exp, 1), 0), target, tmode, modifier);
189
190 case NEW_EXPR:
191 return expand_expr (build_new_1 (exp), target, tmode, modifier);
192
193 default:
194 break;
195 }
196 my_friendly_abort (40);
197 /* NOTREACHED */
198 return NULL;
199 }
200
201 void
202 init_cplus_expand ()
203 {
204 lang_expand_expr = cplus_expand_expr;
205 }
206
207 /* If DECL had its rtl moved from where callers expect it
208 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
209 which may be a pseudo instead of a hard register. */
210
211 void
212 fixup_result_decl (decl, result)
213 tree decl;
214 rtx result;
215 {
216 if (REG_P (result))
217 {
218 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
219 {
220 rtx real_decl_result;
221
222 #ifdef FUNCTION_OUTGOING_VALUE
223 real_decl_result
224 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
225 #else
226 real_decl_result
227 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
228 #endif
229 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
230 result = real_decl_result;
231 }
232 store_expr (decl, result, 0);
233 emit_insn (gen_rtx (USE, VOIDmode, result));
234 }
235 }
236
237 /* Expand this initialization inline and see if it's simple enough that
238 it can be done at compile-time. */
239
240 static tree
241 extract_aggr_init (decl, init)
242 tree decl, init;
243 {
244 return 0;
245 }
246
247 static tree
248 extract_scalar_init (decl, init)
249 tree decl, init;
250 {
251 rtx value, insns, insn;
252 extern struct obstack temporary_obstack;
253 tree t = NULL_TREE;
254
255 push_obstacks (&temporary_obstack, &temporary_obstack);
256 start_sequence ();
257 value = expand_expr (init, NULL_RTX, VOIDmode, 0);
258 insns = get_insns ();
259 end_sequence ();
260 reg_scan (insns, max_reg_num (), 0);
261 jump_optimize (insns, 0, 0, 1);
262 pop_obstacks ();
263
264 for (insn = insns; insn; insn = NEXT_INSN (insn))
265 {
266 rtx r, to;
267
268 if (GET_CODE (insn) == NOTE)
269 continue;
270 else if (GET_CODE (insn) != INSN)
271 return 0;
272
273 r = PATTERN (insn);
274 if (GET_CODE (r) != SET)
275 return 0;
276
277 to = XEXP (r, 0);
278
279 if (! (to == value
280 || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
281 return 0;
282
283 r = XEXP (r, 1);
284
285 switch (GET_CODE (r))
286 {
287 case CONST_INT:
288 t = build_int_2 (XEXP (r, 0), 0);
289 break;
290 default:
291 return 0;
292 }
293 }
294
295 return t;
296 }
297
298 int
299 extract_init (decl, init)
300 tree decl, init;
301 {
302 return 0;
303
304 #if 0
305 if (IS_AGGR_TYPE (TREE_TYPE (decl))
306 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
307 init = extract_aggr_init (decl, init);
308 else
309 init = extract_scalar_init (decl, init);
310
311 if (init == NULL_TREE)
312 return 0;
313
314 DECL_INITIAL (decl) = init;
315 return 1;
316 #endif
317 }
318
319 void
320 do_case (start, end)
321 tree start, end;
322 {
323 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
324
325 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
326 && POINTER_TYPE_P (TREE_TYPE (start)))
327 error ("pointers are not permitted as case values");
328
329 if (end && pedantic)
330 pedwarn ("ANSI C++ forbids range expressions in switch statement");
331
332 if (processing_template_decl)
333 {
334 add_tree (build_min_nt (CASE_LABEL, start, end));
335 return;
336 }
337
338 if (start)
339 value1 = check_cp_case_value (start);
340 if (end)
341 value2 = check_cp_case_value (end);
342
343 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
344
345 if (value1 != error_mark_node
346 && value2 != error_mark_node)
347 {
348 tree duplicate;
349 int success;
350
351 if (end)
352 success = pushcase_range (value1, value2, convert_and_check,
353 label, &duplicate);
354 else if (start)
355 success = pushcase (value1, convert_and_check, label, &duplicate);
356 else
357 success = pushcase (NULL_TREE, 0, label, &duplicate);
358
359 if (success == 1)
360 {
361 if (end)
362 error ("case label not within a switch statement");
363 else if (start)
364 cp_error ("case label `%E' not within a switch statement", start);
365 else
366 error ("default label not within a switch statement");
367 }
368 else if (success == 2)
369 {
370 if (end)
371 {
372 error ("duplicate (or overlapping) case value");
373 cp_error_at ("this is the first entry overlapping that value",
374 duplicate);
375 }
376 else if (start)
377 {
378 cp_error ("duplicate case value `%E'", start);
379 cp_error_at ("previously used here", duplicate);
380 }
381 else
382 {
383 error ("multiple default labels in one switch");
384 cp_error_at ("this is the first default label", duplicate);
385 }
386 }
387 else if (success == 3)
388 warning ("case value out of range");
389 else if (success == 4)
390 warning ("empty range specified");
391 else if (success == 5)
392 {
393 if (end)
394 error ("case label within scope of cleanup or variable array");
395 else if (! start)
396 error ("`default' label within scope of cleanup or variable array");
397 else
398 cp_error ("case label `%E' within scope of cleanup or variable array", start);
399 }
400 }
401 if (start)
402 define_case_label (label);
403 else
404 define_case_label (NULL_TREE);
405 }
This page took 0.06202 seconds and 6 git commands to generate.