1999-10-04 Mark Mitchell <mark@codesourcery.com>
[official-gcc.git] / gcc / cp / expr.c
blob9556caced88e213db14fdba98ebf3a710ec01599
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"
33 #if 0
34 static tree extract_aggr_init PROTO((tree, tree));
35 static tree extract_scalar_init PROTO((tree, tree));
36 #endif
37 static rtx cplus_expand_expr PROTO((tree, rtx, enum machine_mode,
38 enum expand_modifier));
40 /* Hook used by output_constant to expand language-specific
41 constants. */
43 tree
44 cplus_expand_constant (cst)
45 tree cst;
47 switch (TREE_CODE (cst))
49 case PTRMEM_CST:
51 tree type = TREE_TYPE (cst);
52 tree member;
53 tree offset;
55 /* Find the member. */
56 member = PTRMEM_CST_MEMBER (cst);
58 if (TREE_CODE (member) == FIELD_DECL)
60 /* Find the offset for the field. */
61 offset = convert (sizetype,
62 size_binop (EASY_DIV_EXPR,
63 DECL_FIELD_BITPOS (member),
64 size_int (BITS_PER_UNIT)));
66 /* We offset all pointer to data members by 1 so that we
67 can distinguish between a null pointer to data member
68 and the first data member of a structure. */
69 offset = size_binop (PLUS_EXPR, offset, size_int (1));
71 cst = cp_convert (type, offset);
73 else
75 tree delta;
76 tree idx;
77 tree pfn;
78 tree delta2;
80 expand_ptrmemfunc_cst (cst, &delta, &idx, &pfn, &delta2);
82 cst = build_ptrmemfunc1 (type, delta, idx,
83 pfn, delta2);
86 break;
88 default:
89 /* There's nothing to do. */
90 break;
93 return cst;
96 /* Hook used by expand_expr to expand language-specific tree codes. */
98 static rtx
99 cplus_expand_expr (exp, target, tmode, modifier)
100 tree exp;
101 rtx target;
102 enum machine_mode tmode;
103 enum expand_modifier modifier;
105 tree type = TREE_TYPE (exp);
106 register enum machine_mode mode = TYPE_MODE (type);
107 register enum tree_code code = TREE_CODE (exp);
108 int ignore = target == const0_rtx;
110 if (ignore)
111 target = 0;
113 /* No sense saving up arithmetic to be done
114 if it's all in the wrong mode to form part of an address.
115 And force_operand won't know whether to sign-extend or zero-extend. */
117 if (mode != Pmode && modifier == EXPAND_SUM)
118 modifier = EXPAND_NORMAL;
120 switch (code)
122 case AGGR_INIT_EXPR:
124 /* Something needs to be initialized, but we didn't know
125 where that thing was when building the tree. For example,
126 it could be the return value of a function, or a parameter
127 to a function which lays down in the stack, or a temporary
128 variable which must be passed by reference.
130 Cleanups are handled in a language-specific way: they
131 might be run by the called function (true in GNU C++
132 for parameters with cleanups), or they might be
133 run by the caller, after the call (true in GNU C++
134 for other cleanup needs). */
136 tree func = TREE_OPERAND (exp, 0);
137 tree args = TREE_OPERAND (exp, 1);
138 tree type = TREE_TYPE (exp), slot;
139 tree call_exp;
140 rtx call_target, return_target;
141 int pcc_struct_return = 0;
143 /* The expression `init' wants to initialize what
144 `target' represents. SLOT holds the slot for TARGET. */
145 slot = TREE_OPERAND (exp, 2);
147 /* Should always be called with a target. */
148 my_friendly_assert (target != NULL_RTX, 205);
150 /* The target the initializer will initialize (CALL_TARGET)
151 must now be directed to initialize the target we are
152 supposed to initialize (TARGET). The semantics for
153 choosing what CALL_TARGET is is language-specific,
154 as is building the call which will perform the
155 initialization. It is left here to show the choices that
156 exist for C++. */
158 if (AGGR_INIT_VIA_CTOR_P (exp))
160 type = build_pointer_type (type);
161 mark_addressable (slot);
162 args = tree_cons (NULL_TREE,
163 build1 (ADDR_EXPR, type, slot),
164 TREE_CHAIN (args));
165 call_target = 0;
167 else
169 call_target = target;
170 #ifdef PCC_STATIC_STRUCT_RETURN
171 if (aggregate_value_p (type))
173 pcc_struct_return = 1;
174 call_target = 0;
176 #endif
179 call_exp = build (CALL_EXPR, type, func, args, NULL_TREE);
180 TREE_SIDE_EFFECTS (call_exp) = 1;
181 return_target = expand_call (call_exp, call_target, ignore);
183 if (call_target)
184 /* Trust that the right thing has been done; it's too hard to
185 verify. */
186 return return_target;
188 /* If we're suffering under the ancient PCC_STATIC_STRUCT_RETURN
189 calling convention, we need to copy the return value out of
190 the static return buffer into slot. */
191 if (pcc_struct_return)
193 extern int flag_access_control;
194 int old_ac = flag_access_control;
196 tree init = build_decl (VAR_DECL, NULL_TREE,
197 build_reference_type (type));
198 DECL_RTL (init) = XEXP (return_target, 0);
199 init = convert_from_reference (init);
201 flag_access_control = 0;
202 expand_expr (build_aggr_init (slot, init,
203 LOOKUP_ONLYCONVERTING),
204 target, tmode, EXPAND_NORMAL);
205 flag_access_control = old_ac;
207 if (TYPE_NEEDS_DESTRUCTOR (type))
209 init = maybe_build_cleanup (init);
210 if (init != NULL_TREE)
211 expand_expr (init, const0_rtx, VOIDmode, 0);
215 return DECL_RTL (slot);
218 case PTRMEM_CST:
219 return expand_expr (cplus_expand_constant (exp),
220 target, tmode, modifier);
222 case OFFSET_REF:
224 return expand_expr (default_conversion (resolve_offset_ref (exp)),
225 target, tmode, EXPAND_NORMAL);
228 case THUNK_DECL:
229 return DECL_RTL (exp);
231 case THROW_EXPR:
232 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
233 expand_internal_throw ();
234 return NULL;
236 case STMT_EXPR:
238 tree rtl_expr = begin_stmt_expr ();
239 expand_stmt (STMT_EXPR_STMT (exp));
240 finish_stmt_expr (rtl_expr);
241 return expand_expr (rtl_expr, target, tmode, modifier);
243 break;
245 default:
246 break;
248 my_friendly_abort (40);
249 /* NOTREACHED */
250 return NULL;
253 void
254 init_cplus_expand ()
256 lang_expand_expr = cplus_expand_expr;
257 lang_expand_constant = cplus_expand_constant;
260 /* If DECL had its rtl moved from where callers expect it
261 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
262 which may be a pseudo instead of a hard register. */
264 void
265 fixup_result_decl (decl, result)
266 tree decl;
267 rtx result;
269 if (REG_P (result))
271 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
273 rtx real_decl_result;
275 #ifdef FUNCTION_OUTGOING_VALUE
276 real_decl_result
277 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
278 #else
279 real_decl_result
280 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
281 #endif
282 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
283 result = real_decl_result;
285 store_expr (decl, result, 0);
286 emit_insn (gen_rtx (USE, VOIDmode, result));
290 #if 0
291 /* Expand this initialization inline and see if it's simple enough that
292 it can be done at compile-time. */
294 static tree
295 extract_aggr_init (decl, init)
296 tree decl, init;
298 return 0;
301 static tree
302 extract_scalar_init (decl, init)
303 tree decl, init;
305 rtx value, insns, insn;
306 extern struct obstack temporary_obstack;
307 tree t = NULL_TREE;
309 push_obstacks (&temporary_obstack, &temporary_obstack);
310 start_sequence ();
311 value = expand_expr (init, NULL_RTX, VOIDmode, 0);
312 insns = get_insns ();
313 end_sequence ();
314 reg_scan (insns, max_reg_num (), 0);
315 jump_optimize (insns, 0, 0, 1);
316 pop_obstacks ();
318 for (insn = insns; insn; insn = NEXT_INSN (insn))
320 rtx r, to;
322 if (GET_CODE (insn) == NOTE)
323 continue;
324 else if (GET_CODE (insn) != INSN)
325 return 0;
327 r = PATTERN (insn);
328 if (GET_CODE (r) != SET)
329 return 0;
331 to = XEXP (r, 0);
333 if (! (to == value
334 || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
335 return 0;
337 r = XEXP (r, 1);
339 switch (GET_CODE (r))
341 case CONST_INT:
342 t = build_int_2 (XEXP (r, 0), 0);
343 break;
344 default:
345 return 0;
349 return t;
351 #endif
354 extract_init (decl, init)
355 tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
357 return 0;
359 #if 0
360 if (IS_AGGR_TYPE (TREE_TYPE (decl))
361 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
362 init = extract_aggr_init (decl, init);
363 else
364 init = extract_scalar_init (decl, init);
366 if (init == NULL_TREE)
367 return 0;
369 DECL_INITIAL (decl) = init;
370 return 1;
371 #endif
374 void
375 do_case (start, end)
376 tree start, end;
378 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
380 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
381 && POINTER_TYPE_P (TREE_TYPE (start)))
382 error ("pointers are not permitted as case values");
384 if (end && pedantic)
385 pedwarn ("ANSI C++ forbids range expressions in switch statement");
387 if (start)
388 value1 = check_cp_case_value (start);
389 if (end)
390 value2 = check_cp_case_value (end);
392 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
394 if (value1 != error_mark_node
395 && value2 != error_mark_node)
397 tree duplicate;
398 int success;
400 if (end)
401 success = pushcase_range (value1, value2, convert_and_check,
402 label, &duplicate);
403 else if (start)
404 success = pushcase (value1, convert_and_check, label, &duplicate);
405 else
406 success = pushcase (NULL_TREE, 0, label, &duplicate);
408 if (success == 1)
410 if (end)
411 error ("case label not within a switch statement");
412 else if (start)
413 cp_error ("case label `%E' not within a switch statement", start);
414 else
415 error ("default label not within a switch statement");
417 else if (success == 2)
419 if (end)
421 error ("duplicate (or overlapping) case value");
422 cp_error_at ("this is the first entry overlapping that value",
423 duplicate);
425 else if (start)
427 cp_error ("duplicate case value `%E'", start);
428 cp_error_at ("previously used here", duplicate);
430 else
432 error ("multiple default labels in one switch");
433 cp_error_at ("this is the first default label", duplicate);
436 else if (success == 3)
437 warning ("case value out of range");
438 else if (success == 4)
439 warning ("empty range specified");
440 else if (success == 5)
442 if (end)
443 error ("case label within scope of cleanup or variable array");
444 else if (! start)
445 error ("`default' label within scope of cleanup or variable array");
446 else
447 cp_error ("case label `%E' within scope of cleanup or variable array", start);
451 current_function_return_value = NULL_TREE;