OSDN Git Service

2007-03-09 Douglas Gregor <doug.gregor@gmail.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / cp-objcp-common.c
1 /* Some code common to C++ and ObjC++ front ends.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Contributed by Ziemowit Laski  <zlaski@apple.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "c-common.h"
29 #include "toplev.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
32 #include "diagnostic.h"
33 #include "debug.h"
34 #include "cxx-pretty-print.h"
35 #include "cp-objcp-common.h"
36
37 /* Special routine to get the alias set for C++.  */
38
39 HOST_WIDE_INT
40 cxx_get_alias_set (tree t)
41 {
42   if (IS_FAKE_BASE_TYPE (t))
43     /* The base variant of a type must be in the same alias set as the
44        complete type.  */
45     return get_alias_set (TYPE_CONTEXT (t));
46
47   /* Punt on PMFs until we canonicalize functions properly.  */
48   if (TYPE_PTRMEMFUNC_P (t))
49     return 0;
50
51   return c_common_get_alias_set (t);
52 }
53
54 /* Called from check_global_declarations.  */
55
56 bool
57 cxx_warn_unused_global_decl (tree decl)
58 {
59   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
60     return false;
61   if (DECL_IN_SYSTEM_HEADER (decl))
62     return false;
63
64   /* Const variables take the place of #defines in C++.  */
65   if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
66     return false;
67
68   return true;
69 }
70
71 /* Langhook for expr_size: Tell the back end that the value of an expression
72    of non-POD class type does not include any tail padding; a derived class
73    might have allocated something there.  */
74
75 tree
76 cp_expr_size (tree exp)
77 {
78   tree type = TREE_TYPE (exp);
79
80   if (CLASS_TYPE_P (type))
81     {
82       /* The back end should not be interested in the size of an expression
83          of a type with both of these set; all copies of such types must go
84          through a constructor or assignment op.  */
85       if (!TYPE_HAS_COMPLEX_INIT_REF (type)
86           || !TYPE_HAS_COMPLEX_ASSIGN_REF (type)
87           /* But storing a CONSTRUCTOR isn't a copy.  */
88           || TREE_CODE (exp) == CONSTRUCTOR
89           /* And, the gimplifier will sometimes make a copy of
90              an aggregate.  In particular, for a case like:
91
92                 struct S { S(); };
93                 struct X { int a; S s; };
94                 X x = { 0 };
95
96              the gimplifier will create a temporary with
97              static storage duration, perform static
98              initialization of the temporary, and then copy
99              the result.  Since the "s" subobject is never
100              constructed, this is a valid transformation.  */
101           || CP_AGGREGATE_TYPE_P (type))
102         /* This would be wrong for a type with virtual bases.  */
103         return (is_empty_class (type)
104                 ? size_zero_node
105                 : CLASSTYPE_SIZE_UNIT (type));
106       else
107         return NULL_TREE;
108     }
109   else
110     /* Use the default code.  */
111     return lhd_expr_size (exp);
112 }
113
114 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes.  */
115 size_t
116 cp_tree_size (enum tree_code code)
117 {
118   switch (code)
119     {
120     case TINST_LEVEL:           return sizeof (struct tinst_level_s);
121     case PTRMEM_CST:            return sizeof (struct ptrmem_cst);
122     case BASELINK:              return sizeof (struct tree_baselink);
123     case TEMPLATE_PARM_INDEX:   return sizeof (template_parm_index);
124     case DEFAULT_ARG:           return sizeof (struct tree_default_arg);
125     case OVERLOAD:              return sizeof (struct tree_overload);
126     case STATIC_ASSERT:         return sizeof (struct tree_static_assert);
127     case TYPE_ARGUMENT_PACK:
128     case TYPE_PACK_EXPANSION:
129       return sizeof (struct tree_common);
130
131     case NONTYPE_ARGUMENT_PACK:
132     case EXPR_PACK_EXPANSION:
133       return sizeof (struct tree_exp);
134
135     case ARGUMENT_PACK_SELECT:
136       return sizeof (struct tree_argument_pack_select);
137
138     default:
139       gcc_unreachable ();
140     }
141   /* NOTREACHED */
142 }
143
144 /* Returns true if T is a variably modified type, in the sense of C99.
145    FN is as passed to variably_modified_p.
146    This routine needs only check cases that cannot be handled by the
147    language-independent logic in tree.c.  */
148
149 bool
150 cp_var_mod_type_p (tree type, tree fn)
151 {
152   /* If TYPE is a pointer-to-member, it is variably modified if either
153      the class or the member are variably modified.  */
154   if (TYPE_PTR_TO_MEMBER_P (type))
155     return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type), fn)
156             || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type),
157                                          fn));
158
159   /* All other types are not variably modified.  */
160   return false;
161 }
162
163 /* Construct a C++-aware pretty-printer for CONTEXT.  It is assumed
164    that CONTEXT->printer is an already constructed basic pretty_printer.  */
165 void
166 cxx_initialize_diagnostics (diagnostic_context *context)
167 {
168   pretty_printer *base = context->printer;
169   cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
170   memcpy (pp_base (pp), base, sizeof (pretty_printer));
171   pp_cxx_pretty_printer_init (pp);
172   context->printer = (pretty_printer *) pp;
173
174   /* It is safe to free this object because it was previously malloc()'d.  */
175   free (base);
176 }
177
178 /* This compares two types for equivalence ("compatible" in C-based languages).
179    This routine should only return 1 if it is sure.  It should not be used
180    in contexts where erroneously returning 0 causes problems.  */
181
182 int
183 cxx_types_compatible_p (tree x, tree y)
184 {
185   if (same_type_ignoring_top_level_qualifiers_p (x, y))
186     return 1;
187
188   /* Once we get to the middle-end, references and pointers are
189      interchangeable.  FIXME should we try to replace all references with
190      pointers?  */
191   if (POINTER_TYPE_P (x) && POINTER_TYPE_P (y)
192       && TYPE_MODE (x) == TYPE_MODE (y)
193       && TYPE_REF_CAN_ALIAS_ALL (x) == TYPE_REF_CAN_ALIAS_ALL (y)
194       && same_type_p (TREE_TYPE (x), TREE_TYPE (y)))
195     return 1;
196
197   return 0;
198 }
199
200 tree
201 cxx_staticp (tree arg)
202 {
203   switch (TREE_CODE (arg))
204     {
205     case BASELINK:
206       return staticp (BASELINK_FUNCTIONS (arg));
207
208     default:
209       break;
210     }
211   
212   return NULL_TREE;
213 }
214
215 /* Stubs to keep c-opts.c happy.  */
216 void
217 push_file_scope (void)
218 {
219 }
220
221 void
222 pop_file_scope (void)
223 {
224 }
225
226 /* c-pragma.c needs to query whether a decl has extern "C" linkage.  */
227 bool
228 has_c_linkage (tree decl)
229 {
230   return DECL_EXTERN_C_P (decl);
231 }
232
233 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
234      htab_t shadowed_var_for_decl;
235
236 /* Lookup a shadowed var for FROM, and return it if we find one.  */
237
238 tree
239 decl_shadowed_for_var_lookup (tree from)
240 {
241   struct tree_map *h, in;
242   in.base.from = from;
243
244   h = (struct tree_map *) htab_find_with_hash (shadowed_var_for_decl, &in,
245                                                htab_hash_pointer (from));
246   if (h)
247     return h->to;
248   return NULL_TREE;
249 }
250
251 /* Insert a mapping FROM->TO in the shadowed var hashtable.  */
252
253 void
254 decl_shadowed_for_var_insert (tree from, tree to)
255 {
256   struct tree_map *h;
257   void **loc;
258
259   h = GGC_NEW (struct tree_map);
260   h->hash = htab_hash_pointer (from);
261   h->base.from = from;
262   h->to = to;
263   loc = htab_find_slot_with_hash (shadowed_var_for_decl, h, h->hash, INSERT);
264   *(struct tree_map **) loc = h;
265 }
266
267 void
268 init_shadowed_var_for_decl (void)
269 {
270   shadowed_var_for_decl = htab_create_ggc (512, tree_map_hash,
271                                            tree_map_eq, 0);
272 }
273
274
275 #include "gt-cp-cp-objcp-common.h"