Move *-*-gnu* pattern below *-*-linux*.
[official-gcc.git] / gcc / cp / expr.c
blob373afe62dbc25d16e97a11df8432652af55a3c0e
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"
32 #if 0
33 static tree extract_aggr_init PROTO((tree, tree));
34 static tree extract_scalar_init PROTO((tree, tree));
35 #endif
36 static rtx cplus_expand_expr PROTO((tree, rtx, enum machine_mode,
37 enum expand_modifier));
39 /* Hook used by output_constant to expand language-specific
40 constants. */
42 tree
43 cplus_expand_constant (cst)
44 tree cst;
46 switch (TREE_CODE (cst))
48 case PTRMEM_CST:
50 tree type = TREE_TYPE (cst);
51 tree member;
52 tree offset;
54 /* Find the member. */
55 member = PTRMEM_CST_MEMBER (cst);
57 if (TREE_CODE (member) == FIELD_DECL)
59 /* Find the offset for the field. */
60 offset = convert (sizetype,
61 size_binop (EASY_DIV_EXPR,
62 DECL_FIELD_BITPOS (member),
63 size_int (BITS_PER_UNIT)));
65 /* We offset all pointer to data members by 1 so that we
66 can distinguish between a null pointer to data member
67 and the first data member of a structure. */
68 offset = size_binop (PLUS_EXPR, offset, size_int (1));
70 cst = cp_convert (type, offset);
72 else
74 tree delta;
75 tree idx;
76 tree pfn;
77 tree delta2;
79 expand_ptrmemfunc_cst (cst, &delta, &idx, &pfn, &delta2);
81 cst = build_ptrmemfunc1 (type, delta, idx,
82 pfn, delta2);
85 break;
87 default:
88 /* There's nothing to do. */
89 break;
92 return cst;
95 /* Hook used by expand_expr to expand language-specific tree codes. */
97 static rtx
98 cplus_expand_expr (exp, target, tmode, modifier)
99 tree exp;
100 rtx target;
101 enum machine_mode tmode;
102 enum expand_modifier modifier;
104 tree type = TREE_TYPE (exp);
105 register enum machine_mode mode = TYPE_MODE (type);
106 register enum tree_code code = TREE_CODE (exp);
107 int ignore = target == const0_rtx;
109 if (ignore)
110 target = 0;
112 /* No sense saving up arithmetic to be done
113 if it's all in the wrong mode to form part of an address.
114 And force_operand won't know whether to sign-extend or zero-extend. */
116 if (mode != Pmode && modifier == EXPAND_SUM)
117 modifier = EXPAND_NORMAL;
119 switch (code)
121 case AGGR_INIT_EXPR:
123 /* Something needs to be initialized, but we didn't know
124 where that thing was when building the tree. For example,
125 it could be the return value of a function, or a parameter
126 to a function which lays down in the stack, or a temporary
127 variable which must be passed by reference.
129 Cleanups are handled in a language-specific way: they
130 might be run by the called function (true in GNU C++
131 for parameters with cleanups), or they might be
132 run by the caller, after the call (true in GNU C++
133 for other cleanup needs). */
135 tree func = TREE_OPERAND (exp, 0);
136 tree args = TREE_OPERAND (exp, 1);
137 tree type = TREE_TYPE (exp), slot;
138 tree call_exp;
139 rtx call_target, return_target;
140 int pcc_struct_return = 0;
142 /* The expression `init' wants to initialize what
143 `target' represents. SLOT holds the slot for TARGET. */
144 slot = TREE_OPERAND (exp, 2);
146 /* Should always be called with a target. */
147 my_friendly_assert (target != NULL_RTX, 205);
149 /* The target the initializer will initialize (CALL_TARGET)
150 must now be directed to initialize the target we are
151 supposed to initialize (TARGET). The semantics for
152 choosing what CALL_TARGET is is language-specific,
153 as is building the call which will perform the
154 initialization. It is left here to show the choices that
155 exist for C++. */
157 if (AGGR_INIT_VIA_CTOR_P (exp))
159 type = build_pointer_type (type);
160 /* Don't clobber a value that might be part of a default
161 parameter value. */
162 mark_addressable (slot);
163 if (TREE_PERMANENT (args))
164 args = expr_tree_cons (0, build1 (ADDR_EXPR, type, slot),
165 TREE_CHAIN (args));
166 else
167 TREE_VALUE (args) = build1 (ADDR_EXPR, type, slot);
168 call_target = 0;
170 else
172 call_target = target;
173 #ifdef PCC_STATIC_STRUCT_RETURN
174 if (aggregate_value_p (type))
176 pcc_struct_return = 1;
177 call_target = 0;
179 #endif
182 call_exp = build (CALL_EXPR, type, func, args, NULL_TREE);
183 TREE_SIDE_EFFECTS (call_exp) = 1;
184 return_target = expand_call (call_exp, call_target, ignore);
186 if (call_target)
187 /* Trust that the right thing has been done; it's too hard to
188 verify. */
189 return return_target;
191 /* If we're suffering under the ancient PCC_STATIC_STRUCT_RETURN
192 calling convention, we need to copy the return value out of
193 the static return buffer into slot. */
194 if (pcc_struct_return)
196 extern int flag_access_control;
197 int old_ac = flag_access_control;
199 tree init = build_decl (VAR_DECL, NULL_TREE,
200 build_reference_type (type));
201 DECL_RTL (init) = XEXP (return_target, 0);
202 init = convert_from_reference (init);
204 flag_access_control = 0;
205 expand_expr (build_aggr_init (slot, init,
206 LOOKUP_ONLYCONVERTING),
207 target, tmode, EXPAND_NORMAL);
208 flag_access_control = old_ac;
210 if (TYPE_NEEDS_DESTRUCTOR (type))
212 init = maybe_build_cleanup (init);
213 if (init != NULL_TREE)
214 expand_expr (init, const0_rtx, VOIDmode, 0);
218 return DECL_RTL (slot);
221 case PTRMEM_CST:
222 return expand_expr (cplus_expand_constant (exp),
223 target, tmode, modifier);
225 case OFFSET_REF:
227 return expand_expr (default_conversion (resolve_offset_ref (exp)),
228 target, tmode, EXPAND_NORMAL);
231 case THUNK_DECL:
232 return DECL_RTL (exp);
234 case THROW_EXPR:
235 expand_throw (TREE_OPERAND (exp, 0));
236 return NULL;
238 case VEC_INIT_EXPR:
239 return expand_expr
240 (build_vec_init
241 (NULL_TREE, TREE_OPERAND (exp, 0),
242 build_binary_op (MINUS_EXPR, TREE_OPERAND (exp, 2),
243 integer_one_node),
244 TREE_OPERAND (exp, 1), 0), target, tmode, modifier);
246 case NEW_EXPR:
247 return expand_expr (build_new_1 (exp), target, tmode, modifier);
249 case STMT_EXPR:
251 tree rtl_expr = begin_stmt_expr ();
252 tree block = expand_stmt (STMT_EXPR_STMT (exp));
253 finish_stmt_expr (rtl_expr, block);
254 return expand_expr (rtl_expr, target, tmode, modifier);
256 break;
258 default:
259 break;
261 my_friendly_abort (40);
262 /* NOTREACHED */
263 return NULL;
266 void
267 init_cplus_expand ()
269 lang_expand_expr = cplus_expand_expr;
270 lang_expand_constant = cplus_expand_constant;
273 /* If DECL had its rtl moved from where callers expect it
274 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
275 which may be a pseudo instead of a hard register. */
277 void
278 fixup_result_decl (decl, result)
279 tree decl;
280 rtx result;
282 if (REG_P (result))
284 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
286 rtx real_decl_result;
288 #ifdef FUNCTION_OUTGOING_VALUE
289 real_decl_result
290 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
291 #else
292 real_decl_result
293 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
294 #endif
295 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
296 result = real_decl_result;
298 store_expr (decl, result, 0);
299 emit_insn (gen_rtx (USE, VOIDmode, result));
303 #if 0
304 /* Expand this initialization inline and see if it's simple enough that
305 it can be done at compile-time. */
307 static tree
308 extract_aggr_init (decl, init)
309 tree decl, init;
311 return 0;
314 static tree
315 extract_scalar_init (decl, init)
316 tree decl, init;
318 rtx value, insns, insn;
319 extern struct obstack temporary_obstack;
320 tree t = NULL_TREE;
322 push_obstacks (&temporary_obstack, &temporary_obstack);
323 start_sequence ();
324 value = expand_expr (init, NULL_RTX, VOIDmode, 0);
325 insns = get_insns ();
326 end_sequence ();
327 reg_scan (insns, max_reg_num (), 0);
328 jump_optimize (insns, 0, 0, 1);
329 pop_obstacks ();
331 for (insn = insns; insn; insn = NEXT_INSN (insn))
333 rtx r, to;
335 if (GET_CODE (insn) == NOTE)
336 continue;
337 else if (GET_CODE (insn) != INSN)
338 return 0;
340 r = PATTERN (insn);
341 if (GET_CODE (r) != SET)
342 return 0;
344 to = XEXP (r, 0);
346 if (! (to == value
347 || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
348 return 0;
350 r = XEXP (r, 1);
352 switch (GET_CODE (r))
354 case CONST_INT:
355 t = build_int_2 (XEXP (r, 0), 0);
356 break;
357 default:
358 return 0;
362 return t;
364 #endif
367 extract_init (decl, init)
368 tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
370 return 0;
372 #if 0
373 if (IS_AGGR_TYPE (TREE_TYPE (decl))
374 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
375 init = extract_aggr_init (decl, init);
376 else
377 init = extract_scalar_init (decl, init);
379 if (init == NULL_TREE)
380 return 0;
382 DECL_INITIAL (decl) = init;
383 return 1;
384 #endif
387 void
388 do_case (start, end)
389 tree start, end;
391 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
393 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
394 && POINTER_TYPE_P (TREE_TYPE (start)))
395 error ("pointers are not permitted as case values");
397 if (end && pedantic)
398 pedwarn ("ANSI C++ forbids range expressions in switch statement");
400 if (start)
401 value1 = check_cp_case_value (start);
402 if (end)
403 value2 = check_cp_case_value (end);
405 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
407 if (value1 != error_mark_node
408 && value2 != error_mark_node)
410 tree duplicate;
411 int success;
413 if (end)
414 success = pushcase_range (value1, value2, convert_and_check,
415 label, &duplicate);
416 else if (start)
417 success = pushcase (value1, convert_and_check, label, &duplicate);
418 else
419 success = pushcase (NULL_TREE, 0, label, &duplicate);
421 if (success == 1)
423 if (end)
424 error ("case label not within a switch statement");
425 else if (start)
426 cp_error ("case label `%E' not within a switch statement", start);
427 else
428 error ("default label not within a switch statement");
430 else if (success == 2)
432 if (end)
434 error ("duplicate (or overlapping) case value");
435 cp_error_at ("this is the first entry overlapping that value",
436 duplicate);
438 else if (start)
440 cp_error ("duplicate case value `%E'", start);
441 cp_error_at ("previously used here", duplicate);
443 else
445 error ("multiple default labels in one switch");
446 cp_error_at ("this is the first default label", duplicate);
449 else if (success == 3)
450 warning ("case value out of range");
451 else if (success == 4)
452 warning ("empty range specified");
453 else if (success == 5)
455 if (end)
456 error ("case label within scope of cleanup or variable array");
457 else if (! start)
458 error ("`default' label within scope of cleanup or variable array");
459 else
460 cp_error ("case label `%E' within scope of cleanup or variable array", start);
463 define_case_label ();