1 /* Helper routines for C++ support in GDB.
2    Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 
4    Contributed by MontaVista Software.
5    Namespace support contributed by David Carlton.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 #ifndef CP_SUPPORT_H
23 #define CP_SUPPORT_H
24 
25 #include "symtab.h"
26 #include "gdbsupport/gdb_vecs.h"
27 #include "gdbsupport/gdb_obstack.h"
28 #include "gdbsupport/array-view.h"
29 #include <vector>
30 
31 /* Opaque declarations.  */
32 
33 struct symbol;
34 struct block;
35 struct buildsym_compunit;
36 struct objfile;
37 struct type;
38 struct demangle_component;
39 struct using_direct;
40 
41 /* A string representing the name of the anonymous namespace used in GDB.  */
42 
43 #define CP_ANONYMOUS_NAMESPACE_STR "(anonymous namespace)"
44 
45 /* The length of the string representing the anonymous namespace.  */
46 
47 #define CP_ANONYMOUS_NAMESPACE_LEN 21
48 
49 /* A string representing the start of an operator name.  */
50 
51 #define CP_OPERATOR_STR "operator"
52 
53 /* The length of CP_OPERATOR_STR.  */
54 
55 #define CP_OPERATOR_LEN 8
56 
57 /* The result of parsing a name.  */
58 
59 struct demangle_parse_info
60 {
61   /* The result of the parse.  */
62   struct demangle_component *tree = nullptr;
63 
64   /* Any memory used during processing.  */
65   auto_obstack obstack;
66 
67   /* Any other objects referred to by this object, and whose storage
68      lifetime must be linked.  */
69   std::vector<std::unique_ptr<demangle_parse_info>> infos;
70 };
71 
72 
73 /* Functions from cp-support.c.  */
74 
75 extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string
76   (const char *string);
77 
78 extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string_no_typedefs
79   (const char *string);
80 
81 typedef const char *(canonicalization_ftype) (struct type *, void *);
82 
83 extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string_full
84   (const char *string, canonicalization_ftype *finder, void *data);
85 
86 extern char *cp_class_name_from_physname (const char *physname);
87 
88 extern char *method_name_from_physname (const char *physname);
89 
90 extern unsigned int cp_find_first_component (const char *name);
91 
92 extern unsigned int cp_entire_prefix_len (const char *name);
93 
94 extern gdb::unique_xmalloc_ptr<char> cp_func_name (const char *full_name);
95 
96 extern gdb::unique_xmalloc_ptr<char> cp_remove_params
97   (const char *demangled_name);
98 
99 /* DEMANGLED_NAME is the name of a function, (optionally) including
100    parameters and (optionally) a return type.  Return the name of the
101    function without parameters or return type, or NULL if we can not
102    parse the name.  If COMPLETION_MODE is true, then tolerate a
103    non-existing or unbalanced parameter list.  */
104 extern gdb::unique_xmalloc_ptr<char> cp_remove_params_if_any
105   (const char *demangled_name, bool completion_mode);
106 
107 extern std::vector<symbol *> make_symbol_overload_list (const char *,
108                                                                       const char *);
109 
110 extern void add_symbol_overload_list_adl
111   (gdb::array_view<type *> arg_types,
112    const char *func_name,
113    std::vector<symbol *> *overload_list);
114 
115 extern struct type *cp_lookup_rtti_type (const char *name,
116                                                    const struct block *block);
117 
118 /* Produce an unsigned hash value from SEARCH_NAME that is compatible
119    with cp_symbol_name_matches.  Only the last component in
120    "foo::bar::function()" is considered for hashing purposes (i.e.,
121    the entire prefix is skipped), so that later on looking up for
122    "function" or "bar::function" in all namespaces is possible.  */
123 extern unsigned int cp_search_name_hash (const char *search_name);
124 
125 /* Implement the "get_symbol_name_matcher" language_defn method for C++.  */
126 extern symbol_name_matcher_ftype *cp_get_symbol_name_matcher
127   (const lookup_name_info &lookup_name);
128 
129 /* Functions/variables from cp-namespace.c.  */
130 
131 extern int cp_is_in_anonymous (const char *symbol_name);
132 
133 extern void cp_scan_for_anonymous_namespaces (struct buildsym_compunit *,
134                                                         const struct symbol *symbol,
135                                                         struct objfile *objfile);
136 
137 extern struct block_symbol cp_lookup_symbol_nonlocal
138      (const struct language_defn *langdef,
139       const char *name,
140       const struct block *block,
141       const domain_search_flags domain);
142 
143 extern struct block_symbol
144   cp_lookup_symbol_namespace (const char *the_namespace,
145                                     const char *name,
146                                     const struct block *block,
147                                     const domain_search_flags domain);
148 
149 extern struct block_symbol cp_lookup_symbol_imports_or_template
150      (const char *scope,
151       const char *name,
152       const struct block *block,
153       const domain_search_flags domain);
154 
155 extern struct block_symbol
156   cp_lookup_nested_symbol (struct type *parent_type,
157                                  const char *nested_name,
158                                  const struct block *block,
159                                  const domain_search_flags domain);
160 
161 struct type *cp_lookup_transparent_type (const char *name,
162                                                    domain_search_flags flags);
163 
164 /* See description in cp-namespace.c.  */
165 
166 struct type *cp_find_type_baseclass_by_name (struct type *parent_type,
167                                                        const char *name);
168 
169 /* Functions from cp-name-parser.y.  */
170 
171 extern std::unique_ptr<demangle_parse_info> cp_demangled_name_to_comp
172      (const char *demangled_name, std::string *errmsg);
173 
174 /* Convert RESULT to a string.  ESTIMATED_LEN is used only as a guide
175    to the length of the result.  */
176 
177 extern gdb::unique_xmalloc_ptr<char> cp_comp_to_string
178   (struct demangle_component *result, int estimated_len);
179 
180 extern void cp_merge_demangle_parse_infos (struct demangle_parse_info *,
181                                                      struct demangle_component *,
182                                                      std::unique_ptr<demangle_parse_info>);
183 
184 /* The list of "maint cplus" commands.  */
185 
186 extern struct cmd_list_element *maint_cplus_cmd_list;
187 
188 /* Wrappers for bfd and libiberty demangling entry points.  Note they
189    all force DMGL_VERBOSE so that callers don't need to.  This is so
190    that GDB consistently uses DMGL_VERBOSE throughout -- we want
191    libiberty's demangler to expand standard substitutions to their
192    full template name.  */
193 
194 /* A wrapper for bfd_demangle.  */
195 
196 gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options);
197 
198 /* A wrapper for cplus_demangle_print.  */
199 
200 extern char *gdb_cplus_demangle_print (int options,
201                                                struct demangle_component *tree,
202                                                int estimated_length,
203                                                size_t *p_allocated_size);
204 
205 /* Find an instance of the character C in the string S that is outside
206    of all parenthesis pairs, single-quoted strings, and double-quoted
207    strings.  Also, ignore the char within a template name, like a ','
208    within foo<int, int>.  */
209 
210 extern const char *find_toplevel_char (const char *s, char c);
211 
212 #endif /* CP_SUPPORT_H */
213