OSDN Git Service

f83518117067f99386aba219ba66b1e9ab325e9e
[pf3gnuchains/gcc-fork.git] / gcc / cp / expr.c
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.
5
6 This file is part of GNU CC.
7
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.
12
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.
17
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.  */
22
23
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"
34
35 #if 0
36 static tree extract_aggr_init PARAMS ((tree, tree));
37 static tree extract_scalar_init PARAMS ((tree, tree));
38 #endif
39 static rtx cplus_expand_expr PARAMS ((tree, rtx, enum machine_mode,
40                                     enum expand_modifier));
41
42 /* Hook used by output_constant to expand language-specific
43    constants.  */
44
45 tree
46 cplus_expand_constant (cst)
47      tree cst;
48 {
49   switch (TREE_CODE (cst))
50     {
51     case PTRMEM_CST:
52       {
53         tree type = TREE_TYPE (cst);
54         tree member;
55         tree offset;
56       
57         /* Find the member.  */
58         member = PTRMEM_CST_MEMBER (cst);
59
60         if (TREE_CODE (member) == FIELD_DECL) 
61           {
62             /* Find the offset for the field.  */
63             offset = convert (sizetype,
64                               size_binop (EASY_DIV_EXPR,
65                                           DECL_FIELD_BITPOS (member),
66                                           bitsize_int (BITS_PER_UNIT)));
67
68             /* We offset all pointer to data members by 1 so that we
69                can distinguish between a null pointer to data member
70                and the first data member of a structure.  */
71             offset = size_binop (PLUS_EXPR, offset, size_one_node);
72             cst = cp_convert (type, offset);
73           }
74         else
75           {
76             tree delta;
77             tree idx;
78             tree pfn;
79             tree delta2;
80
81             expand_ptrmemfunc_cst (cst, &delta, &idx, &pfn, &delta2);
82
83             cst = build_ptrmemfunc1 (type, delta, idx,
84                                      pfn, delta2);
85           }
86       }
87       break;
88
89     default:
90       /* There's nothing to do.  */
91       break;
92     }
93
94   return cst;
95 }
96
97 /* Hook used by expand_expr to expand language-specific tree codes.  */
98
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;
105 {
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;
110
111   if (ignore)
112     target = 0;
113
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.  */
117
118   if (mode != Pmode && modifier == EXPAND_SUM)
119     modifier = EXPAND_NORMAL;
120
121   switch (code)
122     {
123     case PTRMEM_CST:
124       return expand_expr (cplus_expand_constant (exp),
125                           target, tmode, modifier);
126
127     case OFFSET_REF:
128       {
129         return expand_expr (default_conversion (resolve_offset_ref (exp)),
130                             target, tmode, EXPAND_NORMAL);
131       }
132
133     case THUNK_DECL:
134       my_friendly_assert (DECL_RTL (exp) != NULL_RTX, 20000115);
135       return DECL_RTL (exp);
136
137     case THROW_EXPR:
138       expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
139       expand_internal_throw ();
140       return NULL;
141
142     case EMPTY_CLASS_EXPR:
143       /* We don't need to generate any code for an empty class.  */
144       return const0_rtx;
145
146     case STMT_EXPR:
147       {
148         tree rtl_expr = expand_start_stmt_expr ();
149         expand_stmt (STMT_EXPR_STMT (exp));
150         expand_end_stmt_expr (rtl_expr);
151         RTL_EXPR_HAS_NO_SCOPE (rtl_expr) = 1;
152         return expand_expr (rtl_expr, target, tmode, modifier);
153       }
154       break;
155
156     default:
157       break;
158     }
159   my_friendly_abort (40);
160   /* NOTREACHED */
161   return NULL;
162 }
163
164 void
165 init_cplus_expand ()
166 {
167   lang_expand_expr = cplus_expand_expr;
168   lang_expand_constant = cplus_expand_constant;
169 }
170
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.  */
174
175 void
176 fixup_result_decl (decl, result)
177      tree decl;
178      rtx result;
179 {
180   if (REG_P (result))
181     {
182       if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
183         {
184           rtx real_decl_result;
185
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;
195         }
196       store_expr (decl, result, 0);
197       emit_insn (gen_rtx (USE, VOIDmode, result));
198     }
199 }
200
201 #if 0
202 /* Expand this initialization inline and see if it's simple enough that
203    it can be done at compile-time.  */
204
205 static tree
206 extract_aggr_init (decl, init)
207      tree decl, init;
208 {
209   return 0;
210 }
211
212 static tree
213 extract_scalar_init (decl, init)
214      tree decl, init;
215 {
216   rtx value, insns, insn;
217   extern struct obstack temporary_obstack;
218   tree t = NULL_TREE;
219
220   start_sequence ();
221   value = expand_expr (init, NULL_RTX, VOIDmode, 0);
222   insns = get_insns ();
223   end_sequence ();
224   reg_scan (insns, max_reg_num (), 0);
225   jump_optimize (insns, 0, 0, 1);
226
227   for (insn = insns; insn; insn = NEXT_INSN (insn))
228     {
229       rtx r, to;
230
231       if (GET_CODE (insn) == NOTE)
232         continue;
233       else if (GET_CODE (insn) != INSN)
234         return 0;
235
236       r = PATTERN (insn);
237       if (GET_CODE (r) != SET)
238         return 0;
239
240       to = XEXP (r, 0);
241
242       if (! (to == value
243              || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
244         return 0;
245
246       r = XEXP (r, 1);
247
248       switch (GET_CODE (r))
249         {
250         case CONST_INT:
251           t = build_int_2 (XEXP (r, 0), 0);
252           break;
253         default:
254           return 0;
255         }
256     }
257
258   return t; 
259 }
260 #endif
261
262 int
263 extract_init (decl, init)
264      tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
265 {
266   return 0;
267
268 #if 0
269   if (IS_AGGR_TYPE (TREE_TYPE (decl))
270       || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
271     init = extract_aggr_init (decl, init);
272   else
273     init = extract_scalar_init (decl, init);
274
275   if (init == NULL_TREE)
276     return 0;
277
278   DECL_INITIAL (decl) = init;
279   return 1;
280 #endif
281 }
282
283 void
284 do_case (start, end)
285      tree start, end;
286 {
287   tree value1 = NULL_TREE, value2 = NULL_TREE, label;
288
289   if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE 
290       && POINTER_TYPE_P (TREE_TYPE (start)))
291     error ("pointers are not permitted as case values");
292
293   if (end && pedantic)
294     pedwarn ("ISO C++ forbids range expressions in switch statement");
295
296   if (start)
297     value1 = check_cp_case_value (start);
298   if (end)
299     value2 = check_cp_case_value (end);
300   
301   label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
302
303   if (value1 != error_mark_node
304       && value2 != error_mark_node)
305     {
306       tree duplicate;
307       int success;
308
309       if (end)
310         success = pushcase_range (value1, value2, convert_and_check,
311                                   label, &duplicate);
312       else if (start)
313         success = pushcase (value1, convert_and_check, label, &duplicate);
314       else
315         success = pushcase (NULL_TREE, 0, label, &duplicate);
316
317       if (success == 1)
318         {
319           if (end)
320             error ("case label not within a switch statement");
321           else if (start)
322             cp_error ("case label `%E' not within a switch statement", start);
323           else
324             error ("default label not within a switch statement");
325         }
326       else if (success == 2)
327         {
328           if (end)
329             {
330               error ("duplicate (or overlapping) case value");
331               cp_error_at ("this is the first entry overlapping that value",
332                            duplicate);
333             }
334           else if (start)
335             {
336               cp_error ("duplicate case value `%E'", start);
337               cp_error_at ("previously used here", duplicate);
338             }
339           else
340             {
341               error ("multiple default labels in one switch");
342               cp_error_at ("this is the first default label", duplicate);
343             }
344         }
345       else if (success == 3)
346         warning ("case value out of range");
347       else if (success == 4)
348         warning ("empty range specified");
349       else if (success == 5)
350         {
351           if (end)
352             error ("case label within scope of cleanup or variable array");
353           else if (! start)
354             error ("`default' label within scope of cleanup or variable array");
355           else
356             cp_error ("case label `%E' within scope of cleanup or variable array", start);
357         }
358     }
359
360   current_function_return_value = NULL_TREE;
361 }