198b9484cSchristos /* Internal demangler interface for g++ V3 ABI. 2*7e120ff0Schristos Copyright (C) 2003-2024 Free Software Foundation, Inc. 398b9484cSchristos Written by Ian Lance Taylor <ian@wasabisystems.com>. 498b9484cSchristos 598b9484cSchristos This file is part of the libiberty library, which is part of GCC. 698b9484cSchristos 798b9484cSchristos This file is free software; you can redistribute it and/or modify 898b9484cSchristos it under the terms of the GNU General Public License as published by 998b9484cSchristos the Free Software Foundation; either version 2 of the License, or 1098b9484cSchristos (at your option) any later version. 1198b9484cSchristos 1298b9484cSchristos In addition to the permissions in the GNU General Public License, the 1398b9484cSchristos Free Software Foundation gives you unlimited permission to link the 1498b9484cSchristos compiled version of this file into combinations with other programs, 1598b9484cSchristos and to distribute those combinations without any restriction coming 1698b9484cSchristos from the use of this file. (The General Public License restrictions 1798b9484cSchristos do apply in other respects; for example, they cover modification of 1898b9484cSchristos the file, and distribution when not linked into a combined 1998b9484cSchristos executable.) 2098b9484cSchristos 2198b9484cSchristos This program is distributed in the hope that it will be useful, 2298b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 2398b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2498b9484cSchristos GNU General Public License for more details. 2598b9484cSchristos 2698b9484cSchristos You should have received a copy of the GNU General Public License 2798b9484cSchristos along with this program; if not, write to the Free Software 2898b9484cSchristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 2998b9484cSchristos */ 3098b9484cSchristos 3198b9484cSchristos /* This file provides some definitions shared by cp-demangle.c and 3298b9484cSchristos cp-demint.c. It should not be included by any other files. */ 3398b9484cSchristos 3498b9484cSchristos /* Information we keep for operators. */ 3598b9484cSchristos 3698b9484cSchristos struct demangle_operator_info 3798b9484cSchristos { 3898b9484cSchristos /* Mangled name. */ 3998b9484cSchristos const char *code; 4098b9484cSchristos /* Real name. */ 4198b9484cSchristos const char *name; 4298b9484cSchristos /* Length of real name. */ 4398b9484cSchristos int len; 4498b9484cSchristos /* Number of arguments. */ 4598b9484cSchristos int args; 4698b9484cSchristos }; 4798b9484cSchristos 4898b9484cSchristos /* How to print the value of a builtin type. */ 4998b9484cSchristos 5098b9484cSchristos enum d_builtin_type_print 5198b9484cSchristos { 5298b9484cSchristos /* Print as (type)val. */ 5398b9484cSchristos D_PRINT_DEFAULT, 5498b9484cSchristos /* Print as integer. */ 5598b9484cSchristos D_PRINT_INT, 5698b9484cSchristos /* Print as unsigned integer, with trailing "u". */ 5798b9484cSchristos D_PRINT_UNSIGNED, 5898b9484cSchristos /* Print as long, with trailing "l". */ 5998b9484cSchristos D_PRINT_LONG, 6098b9484cSchristos /* Print as unsigned long, with trailing "ul". */ 6198b9484cSchristos D_PRINT_UNSIGNED_LONG, 6298b9484cSchristos /* Print as long long, with trailing "ll". */ 6398b9484cSchristos D_PRINT_LONG_LONG, 6498b9484cSchristos /* Print as unsigned long long, with trailing "ull". */ 6598b9484cSchristos D_PRINT_UNSIGNED_LONG_LONG, 6698b9484cSchristos /* Print as bool. */ 6798b9484cSchristos D_PRINT_BOOL, 6898b9484cSchristos /* Print as float--put value in square brackets. */ 6998b9484cSchristos D_PRINT_FLOAT, 7098b9484cSchristos /* Print in usual way, but here to detect void. */ 7198b9484cSchristos D_PRINT_VOID 7298b9484cSchristos }; 7398b9484cSchristos 7498b9484cSchristos /* Information we keep for a builtin type. */ 7598b9484cSchristos 7698b9484cSchristos struct demangle_builtin_type_info 7798b9484cSchristos { 7898b9484cSchristos /* Type name. */ 7998b9484cSchristos const char *name; 8098b9484cSchristos /* Length of type name. */ 8198b9484cSchristos int len; 8298b9484cSchristos /* Type name when using Java. */ 8398b9484cSchristos const char *java_name; 8498b9484cSchristos /* Length of java name. */ 8598b9484cSchristos int java_len; 8698b9484cSchristos /* How to print a value of this type. */ 8798b9484cSchristos enum d_builtin_type_print print; 8898b9484cSchristos }; 8998b9484cSchristos 9098b9484cSchristos /* The information structure we pass around. */ 9198b9484cSchristos 9298b9484cSchristos struct d_info 9398b9484cSchristos { 9498b9484cSchristos /* The string we are demangling. */ 9598b9484cSchristos const char *s; 9698b9484cSchristos /* The end of the string we are demangling. */ 9798b9484cSchristos const char *send; 9898b9484cSchristos /* The options passed to the demangler. */ 9998b9484cSchristos int options; 10098b9484cSchristos /* The next character in the string to consider. */ 10198b9484cSchristos const char *n; 10298b9484cSchristos /* The array of components. */ 10398b9484cSchristos struct demangle_component *comps; 10498b9484cSchristos /* The index of the next available component. */ 10598b9484cSchristos int next_comp; 10698b9484cSchristos /* The number of available component structures. */ 10798b9484cSchristos int num_comps; 10898b9484cSchristos /* The array of substitutions. */ 10998b9484cSchristos struct demangle_component **subs; 11098b9484cSchristos /* The index of the next substitution. */ 11198b9484cSchristos int next_sub; 11298b9484cSchristos /* The number of available entries in the subs array. */ 11398b9484cSchristos int num_subs; 11498b9484cSchristos /* The last name we saw, for constructors and destructors. */ 11598b9484cSchristos struct demangle_component *last_name; 11698b9484cSchristos /* A running total of the length of large expansions from the 11798b9484cSchristos mangled name to the demangled name, such as standard 11898b9484cSchristos substitutions and builtin types. */ 11998b9484cSchristos int expansion; 12003467a24Schristos /* Non-zero if we are parsing an expression. */ 12103467a24Schristos int is_expression; 12203467a24Schristos /* Non-zero if we are parsing the type operand of a conversion 12303467a24Schristos operator, but not when in an expression. */ 12403467a24Schristos int is_conversion; 1254b169a6bSchristos /* 1: using new unresolved-name grammar. 1264b169a6bSchristos -1: using new unresolved-name grammar and saw an unresolved-name. 1274b169a6bSchristos 0: using old unresolved-name grammar. */ 1284b169a6bSchristos int unresolved_name_state; 1294559860eSchristos /* If DMGL_NO_RECURSE_LIMIT is not active then this is set to 1304559860eSchristos the current recursion level. */ 1314559860eSchristos unsigned int recursion_level; 13298b9484cSchristos }; 13398b9484cSchristos 13498b9484cSchristos /* To avoid running past the ending '\0', don't: 13598b9484cSchristos - call d_peek_next_char if d_peek_char returned '\0' 13698b9484cSchristos - call d_advance with an 'i' that is too large 13798b9484cSchristos - call d_check_char(di, '\0') 13898b9484cSchristos Everything else is safe. */ 13998b9484cSchristos #define d_peek_char(di) (*((di)->n)) 140212397c6Schristos #ifndef CHECK_DEMANGLER 14198b9484cSchristos # define d_peek_next_char(di) ((di)->n[1]) 14298b9484cSchristos # define d_advance(di, i) ((di)->n += (i)) 143212397c6Schristos #endif 14498b9484cSchristos #define d_check_char(di, c) (d_peek_char(di) == c ? ((di)->n++, 1) : 0) 14598b9484cSchristos #define d_next_char(di) (d_peek_char(di) == '\0' ? '\0' : *((di)->n++)) 14698b9484cSchristos #define d_str(di) ((di)->n) 14798b9484cSchristos 148212397c6Schristos #ifdef CHECK_DEMANGLER 149212397c6Schristos static inline char 150212397c6Schristos d_peek_next_char (const struct d_info *di) 151212397c6Schristos { 152212397c6Schristos if (!di->n[0]) 153212397c6Schristos abort (); 154212397c6Schristos return di->n[1]; 155212397c6Schristos } 156212397c6Schristos 157212397c6Schristos static inline void 158212397c6Schristos d_advance (struct d_info *di, int i) 159212397c6Schristos { 160212397c6Schristos if (i < 0) 161212397c6Schristos abort (); 162212397c6Schristos while (i--) 163212397c6Schristos { 164212397c6Schristos if (!di->n[0]) 165212397c6Schristos abort (); 166212397c6Schristos di->n++; 167212397c6Schristos } 168212397c6Schristos } 169212397c6Schristos #endif 170212397c6Schristos 17198b9484cSchristos /* Functions and arrays in cp-demangle.c which are referenced by 17298b9484cSchristos functions in cp-demint.c. */ 17398b9484cSchristos #ifdef IN_GLIBCPP_V3 17498b9484cSchristos #define CP_STATIC_IF_GLIBCPP_V3 static 17598b9484cSchristos #else 17698b9484cSchristos #define CP_STATIC_IF_GLIBCPP_V3 extern 17798b9484cSchristos #endif 17898b9484cSchristos 17998b9484cSchristos #ifndef IN_GLIBCPP_V3 18098b9484cSchristos extern const struct demangle_operator_info cplus_demangle_operators[]; 18198b9484cSchristos #endif 18298b9484cSchristos 183*7e120ff0Schristos #define D_BUILTIN_TYPE_COUNT (36) 18498b9484cSchristos 18598b9484cSchristos CP_STATIC_IF_GLIBCPP_V3 18698b9484cSchristos const struct demangle_builtin_type_info 18798b9484cSchristos cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT]; 18898b9484cSchristos 18998b9484cSchristos CP_STATIC_IF_GLIBCPP_V3 19098b9484cSchristos struct demangle_component * 19198b9484cSchristos cplus_demangle_mangled_name (struct d_info *, int); 19298b9484cSchristos 19398b9484cSchristos CP_STATIC_IF_GLIBCPP_V3 19498b9484cSchristos struct demangle_component * 19598b9484cSchristos cplus_demangle_type (struct d_info *); 19698b9484cSchristos 19798b9484cSchristos extern void 19898b9484cSchristos cplus_demangle_init_info (const char *, int, size_t, struct d_info *); 19998b9484cSchristos 20098b9484cSchristos /* cp-demangle.c needs to define this a little differently */ 20198b9484cSchristos #undef CP_STATIC_IF_GLIBCPP_V3 202