OSDN Git Service

e5c1c0911df6fc928b4297971501593fe05c08d1
[pf3gnuchains/gcc-fork.git] / gcc / cp / cp-lang.c
1 /* Language-dependent hooks for C++.
2    Copyright 2001, 2002, 2004, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Alexandre Oliva  <aoliva@redhat.com>
5
6 This file is part of GCC.
7
8 GCC 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 3, or (at your option)
11 any later version.
12
13 GCC 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 GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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-family/c-common.h"
29 #include "langhooks.h"
30 #include "langhooks-def.h"
31 #include "debug.h"
32 #include "cp-objcp-common.h"
33 #include "hashtab.h"
34 #include "target.h"
35
36 enum c_language_kind c_language = clk_cxx;
37 static void cp_init_ts (void);
38 static const char * cxx_dwarf_name (tree t, int verbosity);
39 static enum classify_record cp_classify_record (tree type);
40 static tree cp_eh_personality (void);
41 static tree get_template_innermost_arguments_folded (const_tree);
42 static tree get_template_argument_pack_elems_folded (const_tree);
43
44 /* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
45    consequently, there should be very few hooks below.  */
46
47 #undef LANG_HOOKS_NAME
48 #define LANG_HOOKS_NAME "GNU C++"
49 #undef LANG_HOOKS_INIT
50 #define LANG_HOOKS_INIT cxx_init
51 #undef LANG_HOOKS_CLASSIFY_RECORD
52 #define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
53 #undef LANG_HOOKS_GENERIC_TYPE_P
54 #define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
55
56 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
57 #define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
58         get_primary_template_innermost_parameters
59 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
60 #define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
61         get_template_innermost_arguments_folded
62 #undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
63 #define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
64         function_parameter_pack_p
65 #undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
66 #define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
67         get_template_argument_pack_elems_folded
68 #undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
69 #define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
70         template_template_parameter_p
71 #undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
72 #define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
73         function_parameter_expanded_from_pack_p
74 #undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
75 #define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
76 #undef LANG_HOOKS_DWARF_NAME
77 #define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
78 #undef LANG_HOOKS_INIT_TS
79 #define LANG_HOOKS_INIT_TS cp_init_ts
80 #undef LANG_HOOKS_EH_PERSONALITY
81 #define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
82 #undef LANG_HOOKS_EH_RUNTIME_TYPE
83 #define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
84
85 /* Each front end provides its own lang hook initializer.  */
86 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
87
88 /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.c;
89    there should be very few routines below.  */
90
91 /* The following function does something real, but only in Objective-C++.  */
92
93 tree
94 objcp_tsubst_copy_and_build (tree t ATTRIBUTE_UNUSED,
95                              tree args ATTRIBUTE_UNUSED,
96                              tsubst_flags_t complain ATTRIBUTE_UNUSED,
97                              tree in_decl ATTRIBUTE_UNUSED,
98                              bool function_p ATTRIBUTE_UNUSED)
99 {
100   return NULL_TREE;
101 }
102
103
104 static void
105 cp_init_ts (void)
106 {
107   tree_contains_struct[NAMESPACE_DECL][TS_DECL_NON_COMMON] = 1;
108   tree_contains_struct[USING_DECL][TS_DECL_NON_COMMON] = 1;
109   tree_contains_struct[TEMPLATE_DECL][TS_DECL_NON_COMMON] = 1;
110
111   tree_contains_struct[NAMESPACE_DECL][TS_DECL_WITH_VIS] = 1;
112   tree_contains_struct[USING_DECL][TS_DECL_WITH_VIS] = 1;
113   tree_contains_struct[TEMPLATE_DECL][TS_DECL_WITH_VIS] = 1;
114
115   tree_contains_struct[NAMESPACE_DECL][TS_DECL_WRTL] = 1;
116   tree_contains_struct[USING_DECL][TS_DECL_WRTL] = 1;
117   tree_contains_struct[TEMPLATE_DECL][TS_DECL_WRTL] = 1;
118
119   tree_contains_struct[NAMESPACE_DECL][TS_DECL_COMMON] = 1;
120   tree_contains_struct[USING_DECL][TS_DECL_COMMON] = 1;
121   tree_contains_struct[TEMPLATE_DECL][TS_DECL_COMMON] = 1;
122
123   tree_contains_struct[NAMESPACE_DECL][TS_DECL_MINIMAL] = 1;
124   tree_contains_struct[USING_DECL][TS_DECL_MINIMAL] = 1;
125   tree_contains_struct[TEMPLATE_DECL][TS_DECL_MINIMAL] = 1;
126
127   init_shadowed_var_for_decl ();
128
129 }
130
131 static const char *
132 cxx_dwarf_name (tree t, int verbosity)
133 {
134   gcc_assert (DECL_P (t));
135
136   if (DECL_NAME (t)
137       && (ANON_AGGRNAME_P (DECL_NAME (t)) || LAMBDANAME_P (DECL_NAME (t))))
138     return NULL;
139   if (verbosity >= 2)
140     return decl_as_string (t,
141                            TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
142                            | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
143
144   return cxx_printable_name (t, verbosity);
145 }
146
147 static enum classify_record
148 cp_classify_record (tree type)
149 {
150   if (CLASSTYPE_DECLARED_CLASS (type))
151     return RECORD_IS_CLASS;
152
153   return RECORD_IS_STRUCT;
154 }
155
156 static GTY(()) tree cp_eh_personality_decl;
157
158 static tree
159 cp_eh_personality (void)
160 {
161   if (!cp_eh_personality_decl)
162     {
163       const char *lang = (pragma_java_exceptions ? "gcj" : "gxx");
164       cp_eh_personality_decl = build_personality_function (lang);
165     }
166
167   return cp_eh_personality_decl;
168 }
169
170 /* This is a subroutine of fold_cplus_constants.  It returns TRUE if T
171    is a C++ specific constant that needs to be folded further before
172    being passed to the debug info emitter.  */
173
174 static bool
175 template_arg_needs_folding (const_tree t)
176 {
177   /* For now only PTRMEM_CST nodes are to be folded further.  */
178   if (TREE_CODE (t) == PTRMEM_CST)
179     return true;
180   return false;
181 }
182
183 /* Fold the elements of the TREE_VEC C which are C++ specific nodes
184    that would need folding so that they can be processed by the debug
185    info emitter. This is a subroutine of
186    get_template_innermost_arguments_folded and
187    get_template_argument_pack_elems_folded.  */
188
189 static tree
190 fold_cplus_constants (const_tree c)
191 {
192   tree folded_elems, elems = CONST_CAST_TREE (c);
193   int vec_len, i;
194
195   if (elems == NULL_TREE || elems == error_mark_node)
196     return elems;
197
198   vec_len = TREE_VEC_LENGTH (elems);
199
200   /* First check if there is at least one element that needs
201      folding. If there is none, we just return ELEMS. Otherwise create
202      and return a new tree vector that contains the folded versions of
203      ELEMS. This is to avoid allocating memory if we don't need
204      to.  */
205   for (i = 0; i < vec_len; ++i)
206     {
207       if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
208         break;
209     }
210   if (i == vec_len)
211     return elems;
212
213   folded_elems = make_tree_vec (vec_len);
214   for (i = 0; i < vec_len; ++i)
215     {
216       tree elem = TREE_VEC_ELT (elems, i);
217       TREE_VEC_ELT (folded_elems, i) =  
218         (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
219
220     }
221   return folded_elems;
222 }
223
224 /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
225    hook. It returns the innermost template arguments of type T, and
226    makes sure those arguments are folded enough for the debug info
227    emitter.  */
228
229 static tree
230 get_template_innermost_arguments_folded (const_tree t)
231 {
232   return fold_cplus_constants (get_template_innermost_arguments (t));
233 }
234
235 static tree
236 get_template_argument_pack_elems_folded (const_tree t)
237 {
238   return fold_cplus_constants (get_template_argument_pack_elems (t));
239 }
240
241 #include "gt-cp-cp-lang.h"
242 #include "gtype-cp.h"