Merge from trunk rev 172662.
[official-gcc.git] / gcc / cp / cp-lang.c
blobc47fafd4b80cecb7825fb38e751afa6b8224f88c
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>
6 This file is part of GCC.
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.
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.
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/>. */
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 #include "parser.h"
36 #include "pph.h"
38 enum c_language_kind c_language = clk_cxx;
39 static void cp_init_ts (void);
40 static const char * cxx_dwarf_name (tree t, int verbosity);
41 static enum classify_record cp_classify_record (tree type);
42 static tree cp_eh_personality (void);
43 static tree get_template_innermost_arguments_folded (const_tree);
44 static tree get_template_argument_pack_elems_folded (const_tree);
46 /* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
47 consequently, there should be very few hooks below. */
49 #undef LANG_HOOKS_NAME
50 #define LANG_HOOKS_NAME "GNU C++"
51 #undef LANG_HOOKS_INIT
52 #define LANG_HOOKS_INIT cxx_init
53 #undef LANG_HOOKS_CLASSIFY_RECORD
54 #define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
55 #undef LANG_HOOKS_GENERIC_TYPE_P
56 #define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
58 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
59 #define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
60 get_primary_template_innermost_parameters
61 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
62 #define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
63 get_template_innermost_arguments_folded
64 #undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
65 #define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
66 function_parameter_pack_p
67 #undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
68 #define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
69 get_template_argument_pack_elems_folded
70 #undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
71 #define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
72 template_template_parameter_p
73 #undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
74 #define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
75 function_parameter_expanded_from_pack_p
76 #undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
77 #define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
78 #undef LANG_HOOKS_DWARF_NAME
79 #define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
80 #undef LANG_HOOKS_INIT_TS
81 #define LANG_HOOKS_INIT_TS cp_init_ts
82 #undef LANG_HOOKS_EH_PERSONALITY
83 #define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
84 #undef LANG_HOOKS_EH_RUNTIME_TYPE
85 #define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
87 /* Each front end provides its own lang hook initializer. */
88 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
90 /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.c;
91 there should be very few routines below. */
93 /* The following function does something real, but only in Objective-C++. */
95 tree
96 objcp_tsubst_copy_and_build (tree t ATTRIBUTE_UNUSED,
97 tree args ATTRIBUTE_UNUSED,
98 tsubst_flags_t complain ATTRIBUTE_UNUSED,
99 tree in_decl ATTRIBUTE_UNUSED,
100 bool function_p ATTRIBUTE_UNUSED)
102 return NULL_TREE;
105 static void
106 cp_init_ts (void)
108 cp_common_init_ts ();
110 init_shadowed_var_for_decl ();
113 static const char *
114 cxx_dwarf_name (tree t, int verbosity)
116 gcc_assert (DECL_P (t));
118 if (DECL_NAME (t)
119 && (ANON_AGGRNAME_P (DECL_NAME (t)) || LAMBDANAME_P (DECL_NAME (t))))
120 return NULL;
121 if (verbosity >= 2)
122 return decl_as_string (t,
123 TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
124 | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
126 return cxx_printable_name (t, verbosity);
129 static enum classify_record
130 cp_classify_record (tree type)
132 if (CLASSTYPE_DECLARED_CLASS (type))
133 return RECORD_IS_CLASS;
135 return RECORD_IS_STRUCT;
138 static GTY(()) tree cp_eh_personality_decl;
140 static tree
141 cp_eh_personality (void)
143 if (!cp_eh_personality_decl)
145 const char *lang = (pragma_java_exceptions ? "gcj" : "gxx");
146 cp_eh_personality_decl = build_personality_function (lang);
149 return cp_eh_personality_decl;
152 /* This is a subroutine of fold_cplus_constants. It returns TRUE if T
153 is a C++ specific constant that needs to be folded further before
154 being passed to the debug info emitter. */
156 static bool
157 template_arg_needs_folding (const_tree t)
159 /* For now only PTRMEM_CST nodes are to be folded further. */
160 if (TREE_CODE (t) == PTRMEM_CST)
161 return true;
162 return false;
165 /* Fold the elements of the TREE_VEC C which are C++ specific nodes
166 that would need folding so that they can be processed by the debug
167 info emitter. This is a subroutine of
168 get_template_innermost_arguments_folded and
169 get_template_argument_pack_elems_folded. */
171 static tree
172 fold_cplus_constants (const_tree c)
174 tree folded_elems, elems = CONST_CAST_TREE (c);
175 int vec_len, i;
177 if (elems == NULL_TREE || elems == error_mark_node)
178 return elems;
180 vec_len = TREE_VEC_LENGTH (elems);
182 /* First check if there is at least one element that needs
183 folding. If there is none, we just return ELEMS. Otherwise create
184 and return a new tree vector that contains the folded versions of
185 ELEMS. This is to avoid allocating memory if we don't need
186 to. */
187 for (i = 0; i < vec_len; ++i)
189 if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
190 break;
192 if (i == vec_len)
193 return elems;
195 folded_elems = make_tree_vec (vec_len);
196 for (i = 0; i < vec_len; ++i)
198 tree elem = TREE_VEC_ELT (elems, i);
199 TREE_VEC_ELT (folded_elems, i) =
200 (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
203 return folded_elems;
206 /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
207 hook. It returns the innermost template arguments of type T, and
208 makes sure those arguments are folded enough for the debug info
209 emitter. */
211 static tree
212 get_template_innermost_arguments_folded (const_tree t)
214 return fold_cplus_constants (get_template_innermost_arguments (t));
217 static tree
218 get_template_argument_pack_elems_folded (const_tree t)
220 return fold_cplus_constants (get_template_argument_pack_elems (t));
223 #include "gt-cp-cp-lang.h"
224 #include "gtype-cp.h"