* cp-tree.def (EMPTY_CLASS_EXPR): New tree node.
[official-gcc.git] / gcc / cp / expr.c
blobe588efbf8d1ce4e9eb73b9ffb5335e47b1a7e6a7
1 /* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988, 92-97, 1998 Free Software Foundation, Inc.
5 This file is part of GNU CC.
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.
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.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "expr.h"
29 #include "cp-tree.h"
30 #include "toplev.h"
31 #include "except.h"
32 #include "tm_p.h"
34 #if 0
35 static tree extract_aggr_init PROTO((tree, tree));
36 static tree extract_scalar_init PROTO((tree, tree));
37 #endif
38 static rtx cplus_expand_expr PROTO((tree, rtx, enum machine_mode,
39 enum expand_modifier));
41 /* Hook used by output_constant to expand language-specific
42 constants. */
44 tree
45 cplus_expand_constant (cst)
46 tree cst;
48 switch (TREE_CODE (cst))
50 case PTRMEM_CST:
52 tree type = TREE_TYPE (cst);
53 tree member;
54 tree offset;
56 /* Find the member. */
57 member = PTRMEM_CST_MEMBER (cst);
59 if (TREE_CODE (member) == FIELD_DECL)
61 /* Find the offset for the field. */
62 offset = convert (sizetype,
63 size_binop (EASY_DIV_EXPR,
64 DECL_FIELD_BITPOS (member),
65 size_int (BITS_PER_UNIT)));
67 /* We offset all pointer to data members by 1 so that we
68 can distinguish between a null pointer to data member
69 and the first data member of a structure. */
70 offset = size_binop (PLUS_EXPR, offset, size_int (1));
72 cst = cp_convert (type, offset);
74 else
76 tree delta;
77 tree idx;
78 tree pfn;
79 tree delta2;
81 expand_ptrmemfunc_cst (cst, &delta, &idx, &pfn, &delta2);
83 cst = build_ptrmemfunc1 (type, delta, idx,
84 pfn, delta2);
87 break;
89 default:
90 /* There's nothing to do. */
91 break;
94 return cst;
97 /* Hook used by expand_expr to expand language-specific tree codes. */
99 static rtx
100 cplus_expand_expr (exp, target, tmode, modifier)
101 tree exp;
102 rtx target;
103 enum machine_mode tmode;
104 enum expand_modifier modifier;
106 tree type = TREE_TYPE (exp);
107 register enum machine_mode mode = TYPE_MODE (type);
108 register enum tree_code code = TREE_CODE (exp);
109 int ignore = target == const0_rtx;
111 if (ignore)
112 target = 0;
114 /* No sense saving up arithmetic to be done
115 if it's all in the wrong mode to form part of an address.
116 And force_operand won't know whether to sign-extend or zero-extend. */
118 if (mode != Pmode && modifier == EXPAND_SUM)
119 modifier = EXPAND_NORMAL;
121 switch (code)
123 case AGGR_INIT_EXPR:
125 /* Something needs to be initialized, but we didn't know
126 where that thing was when building the tree. For example,
127 it could be the return value of a function, or a parameter
128 to a function which lays down in the stack, or a temporary
129 variable which must be passed by reference.
131 Cleanups are handled in a language-specific way: they
132 might be run by the called function (true in GNU C++
133 for parameters with cleanups), or they might be
134 run by the caller, after the call (true in GNU C++
135 for other cleanup needs). */
137 tree func = TREE_OPERAND (exp, 0);
138 tree args = TREE_OPERAND (exp, 1);
139 tree type = TREE_TYPE (exp), slot;
140 tree call_exp;
141 rtx call_target, return_target;
142 int pcc_struct_return = 0;
144 /* The expression `init' wants to initialize what
145 `target' represents. SLOT holds the slot for TARGET. */
146 slot = TREE_OPERAND (exp, 2);
148 /* Should always be called with a target. */
149 my_friendly_assert (target != NULL_RTX, 205);
151 /* The target the initializer will initialize (CALL_TARGET)
152 must now be directed to initialize the target we are
153 supposed to initialize (TARGET). The semantics for
154 choosing what CALL_TARGET is is language-specific,
155 as is building the call which will perform the
156 initialization. It is left here to show the choices that
157 exist for C++. */
159 if (AGGR_INIT_VIA_CTOR_P (exp))
161 type = build_pointer_type (type);
162 mark_addressable (slot);
163 args = tree_cons (NULL_TREE,
164 build1 (ADDR_EXPR, type, slot),
165 TREE_CHAIN (args));
166 call_target = 0;
168 else
170 call_target = target;
171 #ifdef PCC_STATIC_STRUCT_RETURN
172 if (aggregate_value_p (type))
174 pcc_struct_return = 1;
175 call_target = 0;
177 #endif
180 call_exp = build (CALL_EXPR, type, func, args, NULL_TREE);
181 TREE_SIDE_EFFECTS (call_exp) = 1;
182 return_target = expand_call (call_exp, call_target, ignore);
184 if (call_target)
185 /* Trust that the right thing has been done; it's too hard to
186 verify. */
187 return return_target;
189 /* If we're suffering under the ancient PCC_STATIC_STRUCT_RETURN
190 calling convention, we need to copy the return value out of
191 the static return buffer into slot. */
192 if (pcc_struct_return)
194 extern int flag_access_control;
195 int old_ac = flag_access_control;
197 tree init = build_decl (VAR_DECL, NULL_TREE,
198 build_reference_type (type));
199 DECL_RTL (init) = XEXP (return_target, 0);
200 init = convert_from_reference (init);
202 flag_access_control = 0;
203 expand_expr (build_aggr_init (slot, init,
204 LOOKUP_ONLYCONVERTING),
205 target, tmode, EXPAND_NORMAL);
206 flag_access_control = old_ac;
208 if (TYPE_NEEDS_DESTRUCTOR (type))
210 init = maybe_build_cleanup (init);
211 if (init != NULL_TREE)
212 expand_expr (init, const0_rtx, VOIDmode, 0);
216 return DECL_RTL (slot);
219 case PTRMEM_CST:
220 return expand_expr (cplus_expand_constant (exp),
221 target, tmode, modifier);
223 case OFFSET_REF:
225 return expand_expr (default_conversion (resolve_offset_ref (exp)),
226 target, tmode, EXPAND_NORMAL);
229 case THUNK_DECL:
230 return DECL_RTL (exp);
232 case THROW_EXPR:
233 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
234 expand_internal_throw ();
235 return NULL;
237 case EMPTY_CLASS_EXPR:
238 /* We don't need to generate any code for an empty class. */
239 return const0_rtx;
241 case STMT_EXPR:
243 tree rtl_expr = begin_stmt_expr ();
244 expand_stmt (STMT_EXPR_STMT (exp));
245 finish_stmt_expr (rtl_expr);
246 return expand_expr (rtl_expr, target, tmode, modifier);
248 break;
250 default:
251 break;
253 my_friendly_abort (40);
254 /* NOTREACHED */
255 return NULL;
258 void
259 init_cplus_expand ()
261 lang_expand_expr = cplus_expand_expr;
262 lang_expand_constant = cplus_expand_constant;
265 /* If DECL had its rtl moved from where callers expect it
266 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
267 which may be a pseudo instead of a hard register. */
269 void
270 fixup_result_decl (decl, result)
271 tree decl;
272 rtx result;
274 if (REG_P (result))
276 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
278 rtx real_decl_result;
280 #ifdef FUNCTION_OUTGOING_VALUE
281 real_decl_result
282 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
283 #else
284 real_decl_result
285 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
286 #endif
287 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
288 result = real_decl_result;
290 store_expr (decl, result, 0);
291 emit_insn (gen_rtx (USE, VOIDmode, result));
295 #if 0
296 /* Expand this initialization inline and see if it's simple enough that
297 it can be done at compile-time. */
299 static tree
300 extract_aggr_init (decl, init)
301 tree decl, init;
303 return 0;
306 static tree
307 extract_scalar_init (decl, init)
308 tree decl, init;
310 rtx value, insns, insn;
311 extern struct obstack temporary_obstack;
312 tree t = NULL_TREE;
314 push_obstacks (&temporary_obstack, &temporary_obstack);
315 start_sequence ();
316 value = expand_expr (init, NULL_RTX, VOIDmode, 0);
317 insns = get_insns ();
318 end_sequence ();
319 reg_scan (insns, max_reg_num (), 0);
320 jump_optimize (insns, 0, 0, 1);
321 pop_obstacks ();
323 for (insn = insns; insn; insn = NEXT_INSN (insn))
325 rtx r, to;
327 if (GET_CODE (insn) == NOTE)
328 continue;
329 else if (GET_CODE (insn) != INSN)
330 return 0;
332 r = PATTERN (insn);
333 if (GET_CODE (r) != SET)
334 return 0;
336 to = XEXP (r, 0);
338 if (! (to == value
339 || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
340 return 0;
342 r = XEXP (r, 1);
344 switch (GET_CODE (r))
346 case CONST_INT:
347 t = build_int_2 (XEXP (r, 0), 0);
348 break;
349 default:
350 return 0;
354 return t;
356 #endif
359 extract_init (decl, init)
360 tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
362 return 0;
364 #if 0
365 if (IS_AGGR_TYPE (TREE_TYPE (decl))
366 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
367 init = extract_aggr_init (decl, init);
368 else
369 init = extract_scalar_init (decl, init);
371 if (init == NULL_TREE)
372 return 0;
374 DECL_INITIAL (decl) = init;
375 return 1;
376 #endif
379 void
380 do_case (start, end)
381 tree start, end;
383 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
385 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
386 && POINTER_TYPE_P (TREE_TYPE (start)))
387 error ("pointers are not permitted as case values");
389 if (end && pedantic)
390 pedwarn ("ANSI C++ forbids range expressions in switch statement");
392 if (start)
393 value1 = check_cp_case_value (start);
394 if (end)
395 value2 = check_cp_case_value (end);
397 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
399 if (value1 != error_mark_node
400 && value2 != error_mark_node)
402 tree duplicate;
403 int success;
405 if (end)
406 success = pushcase_range (value1, value2, convert_and_check,
407 label, &duplicate);
408 else if (start)
409 success = pushcase (value1, convert_and_check, label, &duplicate);
410 else
411 success = pushcase (NULL_TREE, 0, label, &duplicate);
413 if (success == 1)
415 if (end)
416 error ("case label not within a switch statement");
417 else if (start)
418 cp_error ("case label `%E' not within a switch statement", start);
419 else
420 error ("default label not within a switch statement");
422 else if (success == 2)
424 if (end)
426 error ("duplicate (or overlapping) case value");
427 cp_error_at ("this is the first entry overlapping that value",
428 duplicate);
430 else if (start)
432 cp_error ("duplicate case value `%E'", start);
433 cp_error_at ("previously used here", duplicate);
435 else
437 error ("multiple default labels in one switch");
438 cp_error_at ("this is the first default label", duplicate);
441 else if (success == 3)
442 warning ("case value out of range");
443 else if (success == 4)
444 warning ("empty range specified");
445 else if (success == 5)
447 if (end)
448 error ("case label within scope of cleanup or variable array");
449 else if (! start)
450 error ("`default' label within scope of cleanup or variable array");
451 else
452 cp_error ("case label `%E' within scope of cleanup or variable array", start);
456 current_function_return_value = NULL_TREE;