]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/expr.c
cp-tree.def (SUBOBJECT): New tree node.
[gcc.git] / gcc / cp / expr.c
1 /* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988, 92-97, 1998 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 "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
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));
38
39 /* Hook used by output_constant to expand language-specific
40 constants. */
41
42 tree
43 cplus_expand_constant (cst)
44 tree cst;
45 {
46 switch (TREE_CODE (cst))
47 {
48 case PTRMEM_CST:
49 {
50 tree type = TREE_TYPE (cst);
51 tree member;
52 tree offset;
53
54 /* Find the member. */
55 member = PTRMEM_CST_MEMBER (cst);
56
57 if (TREE_CODE (member) == FIELD_DECL)
58 {
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)));
64
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));
69
70 cst = cp_convert (type, offset);
71 }
72 else
73 {
74 tree delta;
75 tree idx;
76 tree pfn;
77 tree delta2;
78
79 expand_ptrmemfunc_cst (cst, &delta, &idx, &pfn, &delta2);
80
81 cst = build_ptrmemfunc1 (type, delta, idx,
82 pfn, delta2);
83 }
84 }
85 break;
86
87 default:
88 /* There's nothing to do. */
89 break;
90 }
91
92 return cst;
93 }
94
95 /* Hook used by expand_expr to expand language-specific tree codes. */
96
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;
103 {
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;
108
109 if (ignore)
110 target = 0;
111
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. */
115
116 if (mode != Pmode && modifier == EXPAND_SUM)
117 modifier = EXPAND_NORMAL;
118
119 switch (code)
120 {
121 case AGGR_INIT_EXPR:
122 {
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.
128
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). */
134
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;
141
142 /* The expression `init' wants to initialize what
143 `target' represents. SLOT holds the slot for TARGET. */
144 slot = TREE_OPERAND (exp, 2);
145
146 /* Should always be called with a target. */
147 my_friendly_assert (target != NULL_RTX, 205);
148
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++. */
156
157 if (TREE_CODE (func) == ADDR_EXPR
158 && TREE_CODE (TREE_OPERAND (func, 0)) == FUNCTION_DECL
159 && DECL_CONSTRUCTOR_P (TREE_OPERAND (func, 0)))
160 {
161 type = build_pointer_type (type);
162 /* Don't clobber a value that might be part of a default
163 parameter value. */
164 mark_addressable (slot);
165 if (TREE_PERMANENT (args))
166 args = expr_tree_cons (0, build1 (ADDR_EXPR, type, slot),
167 TREE_CHAIN (args));
168 else
169 TREE_VALUE (args) = build1 (ADDR_EXPR, type, slot);
170 call_target = 0;
171 }
172 else
173 {
174 call_target = target;
175 #ifdef PCC_STATIC_STRUCT_RETURN
176 if (aggregate_value_p (type))
177 {
178 pcc_struct_return = 1;
179 call_target = 0;
180 }
181 #endif
182 }
183
184 call_exp = build (CALL_EXPR, type, func, args, NULL_TREE);
185 TREE_SIDE_EFFECTS (call_exp) = 1;
186 return_target = expand_call (call_exp, call_target, ignore);
187
188 if (call_target)
189 /* Trust that the right thing has been done; it's too hard to
190 verify. */
191 return return_target;
192
193 /* If we're suffering under the ancient PCC_STATIC_STRUCT_RETURN
194 calling convention, we need to copy the return value out of
195 the static return buffer into slot. */
196 if (pcc_struct_return)
197 {
198 extern int flag_access_control;
199 int old_ac = flag_access_control;
200
201 tree init = build_decl (VAR_DECL, NULL_TREE,
202 build_reference_type (type));
203 DECL_RTL (init) = XEXP (return_target, 0);
204 init = convert_from_reference (init);
205
206 flag_access_control = 0;
207 expand_expr (build_aggr_init (slot, init,
208 LOOKUP_ONLYCONVERTING),
209 target, tmode, EXPAND_NORMAL);
210 flag_access_control = old_ac;
211
212 if (TYPE_NEEDS_DESTRUCTOR (type))
213 {
214 init = maybe_build_cleanup (init);
215 if (init != NULL_TREE)
216 expand_expr (init, const0_rtx, VOIDmode, 0);
217 }
218 }
219
220 return DECL_RTL (slot);
221 }
222
223 case PTRMEM_CST:
224 return expand_expr (cplus_expand_constant (exp),
225 target, tmode, modifier);
226
227 case OFFSET_REF:
228 {
229 return expand_expr (default_conversion (resolve_offset_ref (exp)),
230 target, tmode, EXPAND_NORMAL);
231 }
232
233 case THUNK_DECL:
234 return DECL_RTL (exp);
235
236 case THROW_EXPR:
237 expand_throw (TREE_OPERAND (exp, 0));
238 return NULL;
239
240 case VEC_INIT_EXPR:
241 return expand_expr
242 (build_vec_init
243 (NULL_TREE, TREE_OPERAND (exp, 0),
244 build_binary_op (MINUS_EXPR, TREE_OPERAND (exp, 2),
245 integer_one_node),
246 TREE_OPERAND (exp, 1), 0), target, tmode, modifier);
247
248 case NEW_EXPR:
249 return expand_expr (build_new_1 (exp), target, tmode, modifier);
250
251 case STMT_EXPR:
252 {
253 tree rtl_expr = begin_stmt_expr ();
254 tree block = expand_stmt (STMT_EXPR_STMT (exp));
255 finish_stmt_expr (rtl_expr, block);
256 return expand_expr (rtl_expr, target, tmode, modifier);
257 }
258 break;
259
260 default:
261 break;
262 }
263 my_friendly_abort (40);
264 /* NOTREACHED */
265 return NULL;
266 }
267
268 void
269 init_cplus_expand ()
270 {
271 lang_expand_expr = cplus_expand_expr;
272 lang_expand_constant = cplus_expand_constant;
273 }
274
275 /* If DECL had its rtl moved from where callers expect it
276 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
277 which may be a pseudo instead of a hard register. */
278
279 void
280 fixup_result_decl (decl, result)
281 tree decl;
282 rtx result;
283 {
284 if (REG_P (result))
285 {
286 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
287 {
288 rtx real_decl_result;
289
290 #ifdef FUNCTION_OUTGOING_VALUE
291 real_decl_result
292 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
293 #else
294 real_decl_result
295 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
296 #endif
297 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
298 result = real_decl_result;
299 }
300 store_expr (decl, result, 0);
301 emit_insn (gen_rtx (USE, VOIDmode, result));
302 }
303 }
304
305 #if 0
306 /* Expand this initialization inline and see if it's simple enough that
307 it can be done at compile-time. */
308
309 static tree
310 extract_aggr_init (decl, init)
311 tree decl, init;
312 {
313 return 0;
314 }
315
316 static tree
317 extract_scalar_init (decl, init)
318 tree decl, init;
319 {
320 rtx value, insns, insn;
321 extern struct obstack temporary_obstack;
322 tree t = NULL_TREE;
323
324 push_obstacks (&temporary_obstack, &temporary_obstack);
325 start_sequence ();
326 value = expand_expr (init, NULL_RTX, VOIDmode, 0);
327 insns = get_insns ();
328 end_sequence ();
329 reg_scan (insns, max_reg_num (), 0);
330 jump_optimize (insns, 0, 0, 1);
331 pop_obstacks ();
332
333 for (insn = insns; insn; insn = NEXT_INSN (insn))
334 {
335 rtx r, to;
336
337 if (GET_CODE (insn) == NOTE)
338 continue;
339 else if (GET_CODE (insn) != INSN)
340 return 0;
341
342 r = PATTERN (insn);
343 if (GET_CODE (r) != SET)
344 return 0;
345
346 to = XEXP (r, 0);
347
348 if (! (to == value
349 || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
350 return 0;
351
352 r = XEXP (r, 1);
353
354 switch (GET_CODE (r))
355 {
356 case CONST_INT:
357 t = build_int_2 (XEXP (r, 0), 0);
358 break;
359 default:
360 return 0;
361 }
362 }
363
364 return t;
365 }
366 #endif
367
368 int
369 extract_init (decl, init)
370 tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
371 {
372 return 0;
373
374 #if 0
375 if (IS_AGGR_TYPE (TREE_TYPE (decl))
376 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
377 init = extract_aggr_init (decl, init);
378 else
379 init = extract_scalar_init (decl, init);
380
381 if (init == NULL_TREE)
382 return 0;
383
384 DECL_INITIAL (decl) = init;
385 return 1;
386 #endif
387 }
388
389 void
390 do_case (start, end)
391 tree start, end;
392 {
393 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
394
395 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
396 && POINTER_TYPE_P (TREE_TYPE (start)))
397 error ("pointers are not permitted as case values");
398
399 if (end && pedantic)
400 pedwarn ("ANSI C++ forbids range expressions in switch statement");
401
402 if (start)
403 value1 = check_cp_case_value (start);
404 if (end)
405 value2 = check_cp_case_value (end);
406
407 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
408
409 if (value1 != error_mark_node
410 && value2 != error_mark_node)
411 {
412 tree duplicate;
413 int success;
414
415 if (end)
416 success = pushcase_range (value1, value2, convert_and_check,
417 label, &duplicate);
418 else if (start)
419 success = pushcase (value1, convert_and_check, label, &duplicate);
420 else
421 success = pushcase (NULL_TREE, 0, label, &duplicate);
422
423 if (success == 1)
424 {
425 if (end)
426 error ("case label not within a switch statement");
427 else if (start)
428 cp_error ("case label `%E' not within a switch statement", start);
429 else
430 error ("default label not within a switch statement");
431 }
432 else if (success == 2)
433 {
434 if (end)
435 {
436 error ("duplicate (or overlapping) case value");
437 cp_error_at ("this is the first entry overlapping that value",
438 duplicate);
439 }
440 else if (start)
441 {
442 cp_error ("duplicate case value `%E'", start);
443 cp_error_at ("previously used here", duplicate);
444 }
445 else
446 {
447 error ("multiple default labels in one switch");
448 cp_error_at ("this is the first default label", duplicate);
449 }
450 }
451 else if (success == 3)
452 warning ("case value out of range");
453 else if (success == 4)
454 warning ("empty range specified");
455 else if (success == 5)
456 {
457 if (end)
458 error ("case label within scope of cleanup or variable array");
459 else if (! start)
460 error ("`default' label within scope of cleanup or variable array");
461 else
462 cp_error ("case label `%E' within scope of cleanup or variable array", start);
463 }
464 }
465 define_case_label ();
466 }
This page took 0.062475 seconds and 6 git commands to generate.