OSDN Git Service

* decl.c (init_decl_processing): Reenable inlining on trees.
[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, 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 #include "except.h"
32 #include "tm_p.h"
33
34 #if 0
35 static tree extract_aggr_init PROTO((tree, tree));
36 static tree extract_scalar_init PROTO((tree, tree));
37 #endif
38 static rtx cplus_expand_expr PROTO((tree, rtx, enum machine_mode,
39                                     enum expand_modifier));
40
41 /* Hook used by output_constant to expand language-specific
42    constants.  */
43
44 tree
45 cplus_expand_constant (cst)
46      tree cst;
47 {
48   switch (TREE_CODE (cst))
49     {
50     case PTRMEM_CST:
51       {
52         tree type = TREE_TYPE (cst);
53         tree member;
54         tree offset;
55       
56         /* Find the member.  */
57         member = PTRMEM_CST_MEMBER (cst);
58
59         if (TREE_CODE (member) == FIELD_DECL) 
60           {
61             /* Find the offset for the field.  */
62             offset = convert (sizetype,
63                               size_binop (EASY_DIV_EXPR,
64                                           DECL_FIELD_BITPOS (member),
65                                           size_int (BITS_PER_UNIT)));
66
67             /* We offset all pointer to data members by 1 so that we
68                can distinguish between a null pointer to data member
69                and the first data member of a structure.  */
70             offset = size_binop (PLUS_EXPR, offset, size_int (1));
71         
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       return DECL_RTL (exp);
135
136     case THROW_EXPR:
137       expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
138       expand_internal_throw ();
139       return NULL;
140
141     case EMPTY_CLASS_EXPR:
142       /* We don't need to generate any code for an empty class.  */
143       return const0_rtx;
144
145     case STMT_EXPR:
146       {
147         tree rtl_expr = begin_stmt_expr ();
148         expand_stmt (STMT_EXPR_STMT (exp));
149         finish_stmt_expr (rtl_expr);
150         return expand_expr (rtl_expr, target, tmode, modifier);
151       }
152       break;
153
154     default:
155       break;
156     }
157   my_friendly_abort (40);
158   /* NOTREACHED */
159   return NULL;
160 }
161
162 void
163 init_cplus_expand ()
164 {
165   lang_expand_expr = cplus_expand_expr;
166   lang_expand_constant = cplus_expand_constant;
167 }
168
169 /* If DECL had its rtl moved from where callers expect it
170    to be, fix it up.  RESULT is the nominal rtl for the RESULT_DECL,
171    which may be a pseudo instead of a hard register.  */
172
173 void
174 fixup_result_decl (decl, result)
175      tree decl;
176      rtx result;
177 {
178   if (REG_P (result))
179     {
180       if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
181         {
182           rtx real_decl_result;
183
184 #ifdef FUNCTION_OUTGOING_VALUE
185           real_decl_result
186             = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
187 #else
188           real_decl_result
189             = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
190 #endif
191           REG_FUNCTION_VALUE_P (real_decl_result) = 1;
192           result = real_decl_result;
193         }
194       store_expr (decl, result, 0);
195       emit_insn (gen_rtx (USE, VOIDmode, result));
196     }
197 }
198
199 #if 0
200 /* Expand this initialization inline and see if it's simple enough that
201    it can be done at compile-time.  */
202
203 static tree
204 extract_aggr_init (decl, init)
205      tree decl, init;
206 {
207   return 0;
208 }
209
210 static tree
211 extract_scalar_init (decl, init)
212      tree decl, init;
213 {
214   rtx value, insns, insn;
215   extern struct obstack temporary_obstack;
216   tree t = NULL_TREE;
217
218   start_sequence ();
219   value = expand_expr (init, NULL_RTX, VOIDmode, 0);
220   insns = get_insns ();
221   end_sequence ();
222   reg_scan (insns, max_reg_num (), 0);
223   jump_optimize (insns, 0, 0, 1);
224
225   for (insn = insns; insn; insn = NEXT_INSN (insn))
226     {
227       rtx r, to;
228
229       if (GET_CODE (insn) == NOTE)
230         continue;
231       else if (GET_CODE (insn) != INSN)
232         return 0;
233
234       r = PATTERN (insn);
235       if (GET_CODE (r) != SET)
236         return 0;
237
238       to = XEXP (r, 0);
239
240       if (! (to == value
241              || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
242         return 0;
243
244       r = XEXP (r, 1);
245
246       switch (GET_CODE (r))
247         {
248         case CONST_INT:
249           t = build_int_2 (XEXP (r, 0), 0);
250           break;
251         default:
252           return 0;
253         }
254     }
255
256   return t; 
257 }
258 #endif
259
260 int
261 extract_init (decl, init)
262      tree decl ATTRIBUTE_UNUSED, init ATTRIBUTE_UNUSED;
263 {
264   return 0;
265
266 #if 0
267   if (IS_AGGR_TYPE (TREE_TYPE (decl))
268       || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
269     init = extract_aggr_init (decl, init);
270   else
271     init = extract_scalar_init (decl, init);
272
273   if (init == NULL_TREE)
274     return 0;
275
276   DECL_INITIAL (decl) = init;
277   return 1;
278 #endif
279 }
280
281 void
282 do_case (start, end)
283      tree start, end;
284 {
285   tree value1 = NULL_TREE, value2 = NULL_TREE, label;
286
287   if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE 
288       && POINTER_TYPE_P (TREE_TYPE (start)))
289     error ("pointers are not permitted as case values");
290
291   if (end && pedantic)
292     pedwarn ("ANSI C++ forbids range expressions in switch statement");
293
294   if (start)
295     value1 = check_cp_case_value (start);
296   if (end)
297     value2 = check_cp_case_value (end);
298   
299   label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
300
301   if (value1 != error_mark_node
302       && value2 != error_mark_node)
303     {
304       tree duplicate;
305       int success;
306
307       if (end)
308         success = pushcase_range (value1, value2, convert_and_check,
309                                   label, &duplicate);
310       else if (start)
311         success = pushcase (value1, convert_and_check, label, &duplicate);
312       else
313         success = pushcase (NULL_TREE, 0, label, &duplicate);
314
315       if (success == 1)
316         {
317           if (end)
318             error ("case label not within a switch statement");
319           else if (start)
320             cp_error ("case label `%E' not within a switch statement", start);
321           else
322             error ("default label not within a switch statement");
323         }
324       else if (success == 2)
325         {
326           if (end)
327             {
328               error ("duplicate (or overlapping) case value");
329               cp_error_at ("this is the first entry overlapping that value",
330                            duplicate);
331             }
332           else if (start)
333             {
334               cp_error ("duplicate case value `%E'", start);
335               cp_error_at ("previously used here", duplicate);
336             }
337           else
338             {
339               error ("multiple default labels in one switch");
340               cp_error_at ("this is the first default label", duplicate);
341             }
342         }
343       else if (success == 3)
344         warning ("case value out of range");
345       else if (success == 4)
346         warning ("empty range specified");
347       else if (success == 5)
348         {
349           if (end)
350             error ("case label within scope of cleanup or variable array");
351           else if (! start)
352             error ("`default' label within scope of cleanup or variable array");
353           else
354             cp_error ("case label `%E' within scope of cleanup or variable array", start);
355         }
356     }
357
358   current_function_return_value = NULL_TREE;
359 }