Fix cut and paste error in last change
[official-gcc.git] / gcc / cp / expr.c
blob42a53903ee315dbd84115874500e271706a559a9
1 /* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 2000 Free Software Foundation, Inc.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "cp-tree.h"
31 #include "toplev.h"
32 #include "except.h"
33 #include "tm_p.h"
35 static rtx cplus_expand_expr PARAMS ((tree, rtx, enum machine_mode,
36 enum expand_modifier));
38 /* Hook used by output_constant to expand language-specific
39 constants. */
41 tree
42 cplus_expand_constant (cst)
43 tree cst;
45 switch (TREE_CODE (cst))
47 case PTRMEM_CST:
49 tree type = TREE_TYPE (cst);
50 tree member;
52 /* Find the member. */
53 member = PTRMEM_CST_MEMBER (cst);
55 if (TREE_CODE (member) == FIELD_DECL)
57 /* Find the offset for the field. */
58 tree offset = byte_position (member);
60 if (flag_new_abi)
61 /* Under the new ABI, we use -1 to represent the NULL
62 pointer; non-NULL values simply contain the offset of
63 the data member. */
65 else
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_one_node);
71 cst = fold (build1 (NOP_EXPR, type, offset));
73 else
75 tree delta, idx, pfn, delta2;
77 expand_ptrmemfunc_cst (cst, &delta, &idx, &pfn, &delta2);
78 cst = build_ptrmemfunc1 (type, delta, idx, pfn, delta2);
81 break;
83 default:
84 /* There's nothing to do. */
85 break;
88 return cst;
91 /* Hook used by expand_expr to expand language-specific tree codes. */
93 static rtx
94 cplus_expand_expr (exp, target, tmode, modifier)
95 tree exp;
96 rtx target;
97 enum machine_mode tmode;
98 enum expand_modifier modifier;
100 tree type = TREE_TYPE (exp);
101 register enum machine_mode mode = TYPE_MODE (type);
102 register enum tree_code code = TREE_CODE (exp);
103 int ignore = target == const0_rtx;
105 if (ignore)
106 target = 0;
108 /* No sense saving up arithmetic to be done
109 if it's all in the wrong mode to form part of an address.
110 And force_operand won't know whether to sign-extend or zero-extend. */
112 if (mode != Pmode && modifier == EXPAND_SUM)
113 modifier = EXPAND_NORMAL;
115 switch (code)
117 case PTRMEM_CST:
118 return expand_expr (cplus_expand_constant (exp),
119 target, tmode, modifier);
121 case OFFSET_REF:
123 return expand_expr (default_conversion (resolve_offset_ref (exp)),
124 target, tmode, EXPAND_NORMAL);
127 case THROW_EXPR:
128 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
129 expand_internal_throw ();
130 return NULL;
132 case EMPTY_CLASS_EXPR:
133 /* We don't need to generate any code for an empty class. */
134 return const0_rtx;
136 case STMT_EXPR:
138 tree rtl_expr;
139 rtx result;
141 /* Since expand_expr_stmt calls free_temp_slots after every
142 expression statement, we must call push_temp_slots here.
143 Otherwise, any temporaries in use now would be considered
144 out-of-scope after the first EXPR_STMT from within the
145 STMT_EXPR. */
146 push_temp_slots ();
147 rtl_expr = expand_start_stmt_expr ();
148 expand_stmt (STMT_EXPR_STMT (exp));
149 expand_end_stmt_expr (rtl_expr);
150 result = expand_expr (rtl_expr, target, tmode, modifier);
151 pop_temp_slots ();
152 return result;
154 break;
156 default:
157 break;
159 my_friendly_abort (40);
160 /* NOTREACHED */
161 return NULL;
164 void
165 init_cplus_expand ()
167 lang_expand_expr = cplus_expand_expr;
168 lang_expand_constant = cplus_expand_constant;
171 /* If DECL had its rtl moved from where callers expect it
172 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
173 which may be a pseudo instead of a hard register. */
175 void
176 fixup_result_decl (decl, result)
177 tree decl;
178 rtx result;
180 if (REG_P (result))
182 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
184 rtx real_decl_result;
186 #ifdef FUNCTION_OUTGOING_VALUE
187 real_decl_result
188 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
189 #else
190 real_decl_result
191 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
192 #endif
193 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
194 result = real_decl_result;
196 store_expr (decl, result, 0);
197 emit_insn (gen_rtx (USE, VOIDmode, result));
202 extract_init (decl, init)
203 tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
205 return 0;
208 void
209 do_case (start, end)
210 tree start, end;
212 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
214 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
215 && POINTER_TYPE_P (TREE_TYPE (start)))
216 error ("pointers are not permitted as case values");
218 if (end && pedantic)
219 pedwarn ("ISO C++ forbids range expressions in switch statement");
221 if (start)
222 value1 = check_cp_case_value (start);
223 if (end)
224 value2 = check_cp_case_value (end);
226 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
228 if (value1 != error_mark_node
229 && value2 != error_mark_node)
231 tree duplicate;
232 int success;
234 if (end)
235 success = pushcase_range (value1, value2, convert_and_check,
236 label, &duplicate);
237 else if (start)
238 success = pushcase (value1, convert_and_check, label, &duplicate);
239 else
240 success = pushcase (NULL_TREE, 0, label, &duplicate);
242 if (success == 1)
244 if (end)
245 error ("case label not within a switch statement");
246 else if (start)
247 cp_error ("case label `%E' not within a switch statement", start);
248 else
249 error ("default label not within a switch statement");
251 else if (success == 2)
253 if (end)
255 error ("duplicate (or overlapping) case value");
256 cp_error_at ("this is the first entry overlapping that value",
257 duplicate);
259 else if (start)
261 cp_error ("duplicate case value `%E'", start);
262 cp_error_at ("previously used here", duplicate);
264 else
266 error ("multiple default labels in one switch");
267 cp_error_at ("this is the first default label", duplicate);
270 else if (success == 3)
271 warning ("case value out of range");
272 else if (success == 4)
273 warning ("empty range specified");
274 else if (success == 5)
276 if (end)
277 error ("case label within scope of cleanup or variable array");
278 else if (! start)
279 error ("`default' label within scope of cleanup or variable array");
280 else
281 cp_error ("case label `%E' within scope of cleanup or variable array", start);
285 current_function_return_value = NULL_TREE;