cp-demangle.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,161 行 · 第 1/5 页
C
2,161 行
PARAMS ((demangling_t, int *, int, int));static status_t demangle_number_literally PARAMS ((demangling_t, dyn_string_t, int, int));static status_t demangle_identifier PARAMS ((demangling_t, int, dyn_string_t));static status_t demangle_operator_name PARAMS ((demangling_t, int, int *));static status_t demangle_nv_offset PARAMS ((demangling_t));static status_t demangle_v_offset PARAMS ((demangling_t));static status_t demangle_call_offset PARAMS ((demangling_t));static status_t demangle_special_name PARAMS ((demangling_t));static status_t demangle_ctor_dtor_name PARAMS ((demangling_t));static status_t demangle_type_ptr PARAMS ((demangling_t, int *, int));static status_t demangle_type PARAMS ((demangling_t));static status_t demangle_CV_qualifiers PARAMS ((demangling_t, dyn_string_t));static status_t demangle_builtin_type PARAMS ((demangling_t));static status_t demangle_function_type PARAMS ((demangling_t, int *));static status_t demangle_bare_function_type PARAMS ((demangling_t, int *));static status_t demangle_class_enum_type PARAMS ((demangling_t, int *));static status_t demangle_array_type PARAMS ((demangling_t, int *));static status_t demangle_template_param PARAMS ((demangling_t));static status_t demangle_template_args PARAMS ((demangling_t));static status_t demangle_literal PARAMS ((demangling_t));static status_t demangle_template_arg PARAMS ((demangling_t));static status_t demangle_expression PARAMS ((demangling_t));static status_t demangle_scope_expression PARAMS ((demangling_t));static status_t demangle_expr_primary PARAMS ((demangling_t));static status_t demangle_substitution PARAMS ((demangling_t, int *));static status_t demangle_local_name PARAMS ((demangling_t));static status_t demangle_discriminator PARAMS ((demangling_t, int));static status_t cp_demangle PARAMS ((const char *, dyn_string_t));#ifdef IN_LIBGCC2static status_t cp_demangle_type PARAMS ((const char*, dyn_string_t));#endif/* When passed to demangle_bare_function_type, indicates that the function's return type is not encoded before its parameter types. */#define BFT_NO_RETURN_TYPE NULL/* Check that the next character is C. If so, consume it. If not, return an error. */static status_tdemangle_char (dm, c) demangling_t dm; int c;{ static char *error_message = NULL; if (peek_char (dm) == c) { advance_char (dm); return STATUS_OK; } else { if (error_message == NULL) error_message = strdup ("Expected ?"); error_message[9] = c; return error_message; }}/* Demangles and emits a <mangled-name>. <mangled-name> ::= _Z <encoding> */static status_tdemangle_mangled_name (dm) demangling_t dm;{ DEMANGLE_TRACE ("mangled-name", dm); RETURN_IF_ERROR (demangle_char (dm, '_')); RETURN_IF_ERROR (demangle_char (dm, 'Z')); RETURN_IF_ERROR (demangle_encoding (dm)); return STATUS_OK;}/* Demangles and emits an <encoding>. <encoding> ::= <function name> <bare-function-type> ::= <data name> ::= <special-name> */static status_tdemangle_encoding (dm) demangling_t dm;{ int encode_return_type; int start_position; template_arg_list_t old_arg_list = current_template_arg_list (dm); char peek = peek_char (dm); DEMANGLE_TRACE ("encoding", dm); /* Remember where the name starts. If it turns out to be a template function, we'll have to insert the return type here. */ start_position = result_caret_pos (dm); if (peek == 'G' || peek == 'T') RETURN_IF_ERROR (demangle_special_name (dm)); else { /* Now demangle the name. */ RETURN_IF_ERROR (demangle_name (dm, &encode_return_type)); /* If there's anything left, the name was a function name, with maybe its return type, and its parameter types, following. */ if (!end_of_name_p (dm) && peek_char (dm) != 'E') { if (encode_return_type) /* Template functions have their return type encoded. The return type should be inserted at start_position. */ RETURN_IF_ERROR (demangle_bare_function_type (dm, &start_position)); else /* Non-template functions don't have their return type encoded. */ RETURN_IF_ERROR (demangle_bare_function_type (dm, BFT_NO_RETURN_TYPE)); } } /* Pop off template argument lists that were built during the mangling of this name, to restore the old template context. */ pop_to_template_arg_list (dm, old_arg_list); return STATUS_OK;}/* Demangles and emits a <name>. <name> ::= <unscoped-name> ::= <unscoped-template-name> <template-args> ::= <nested-name> ::= <local-name> <unscoped-name> ::= <unqualified-name> ::= St <unqualified-name> # ::std:: <unscoped-template-name> ::= <unscoped-name> ::= <substitution> */static status_tdemangle_name (dm, encode_return_type) demangling_t dm; int *encode_return_type;{ int start = substitution_start (dm); char peek = peek_char (dm); int is_std_substitution = 0; /* Generally, the return type is encoded if the function is a template-id, and suppressed otherwise. There are a few cases, though, in which the return type is not encoded even for a templated function. In these cases, this flag is set. */ int suppress_return_type = 0; DEMANGLE_TRACE ("name", dm); switch (peek) { case 'N': /* This is a <nested-name>. */ RETURN_IF_ERROR (demangle_nested_name (dm, encode_return_type)); break; case 'Z': RETURN_IF_ERROR (demangle_local_name (dm)); *encode_return_type = 0; break; case 'S': /* The `St' substitution allows a name nested in std:: to appear without being enclosed in a nested name. */ if (peek_char_next (dm) == 't') { (void) next_char (dm); (void) next_char (dm); RETURN_IF_ERROR (result_add (dm, "std::")); RETURN_IF_ERROR (demangle_unqualified_name (dm, &suppress_return_type)); is_std_substitution = 1; } else RETURN_IF_ERROR (demangle_substitution (dm, encode_return_type)); /* Check if a template argument list immediately follows. If so, then we just demangled an <unqualified-template-name>. */ if (peek_char (dm) == 'I') { /* A template name of the form std::<unqualified-name> is a substitution candidate. */ if (is_std_substitution) RETURN_IF_ERROR (substitution_add (dm, start, 0)); /* Demangle the <template-args> here. */ RETURN_IF_ERROR (demangle_template_args (dm)); *encode_return_type = !suppress_return_type; } else *encode_return_type = 0; break; default: /* This is an <unscoped-name> or <unscoped-template-name>. */ RETURN_IF_ERROR (demangle_unqualified_name (dm, &suppress_return_type)); /* If the <unqualified-name> is followed by template args, this is an <unscoped-template-name>. */ if (peek_char (dm) == 'I') { /* Add a substitution for the unqualified template name. */ RETURN_IF_ERROR (substitution_add (dm, start, 0)); RETURN_IF_ERROR (demangle_template_args (dm)); *encode_return_type = !suppress_return_type; } else *encode_return_type = 0; break; } return STATUS_OK;}/* Demangles and emits a <nested-name>. <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqulified-name> E */static status_tdemangle_nested_name (dm, encode_return_type) demangling_t dm; int *encode_return_type;{ char peek; DEMANGLE_TRACE ("nested-name", dm); RETURN_IF_ERROR (demangle_char (dm, 'N')); peek = peek_char (dm); if (peek == 'r' || peek == 'V' || peek == 'K') { dyn_string_t cv_qualifiers; status_t status; /* Snarf up CV qualifiers. */ cv_qualifiers = dyn_string_new (24); if (cv_qualifiers == NULL) return STATUS_ALLOCATION_FAILED; demangle_CV_qualifiers (dm, cv_qualifiers); /* Emit them, preceded by a space. */ status = result_add_char (dm, ' '); if (STATUS_NO_ERROR (status)) status = result_add_string (dm, cv_qualifiers); /* The CV qualifiers that occur in a <nested-name> will be qualifiers for member functions. These are placed at the end of the function. Therefore, shift the caret to the left by the length of the qualifiers, so other text is inserted before them and they stay at the end. */ result_shift_caret (dm, -dyn_string_length (cv_qualifiers) - 1); /* Clean up. */ dyn_string_delete (cv_qualifiers); RETURN_IF_ERROR (status); } RETURN_IF_ERROR (demangle_prefix (dm, encode_return_type)); /* No need to demangle the final <unqualified-name>; demangle_prefix will handle it. */ RETURN_IF_ERROR (demangle_char (dm, 'E')); return STATUS_OK;}/* Demangles and emits a <prefix>. <prefix> ::= <prefix> <unqualified-name> ::= <template-prefix> <template-args> ::= # empty ::= <substitution> <template-prefix> ::= <prefix> ::= <substitution> */static status_tdemangle_prefix (dm, encode_return_type) demangling_t dm; int *encode_return_type;{ int start = substitution_start (dm); int nested = 0; /* ENCODE_RETURN_TYPE is updated as we decend the nesting chain. After <template-args>, it is set to non-zero; after everything else it is set to zero. */ /* Generally, the return type is encoded if the function is a template-id, and suppressed otherwise. There are a few cases, though, in which the return type is not encoded even for a templated function. In these cases, this flag is set. */ int suppress_return_type = 0; DEMANGLE_TRACE ("prefix", dm); while (1) { char peek; if (end_of_name_p (dm)) return "Unexpected end of name in <compound-name>."; peek = peek_char (dm); /* We'll initialize suppress_return_type to false, and set it to true if we end up demangling a constructor name. However, make sure we're not actually about to demangle template arguments -- if so, this is the <template-args> following a <template-prefix>, so we'll want the previous flag value around. */ if (peek != 'I') suppress_return_type = 0; if (IS_DIGIT ((unsigned char) peek) || (peek >= 'a' && peek <= 'z') || peek == 'C' || peek == 'D' || peek == 'S') { /* We have another level of scope qualification. */ if (nested) RETURN_IF_ERROR (result_add (dm, "::")); else nested = 1; if (peek == 'S') /* The substitution determines whether this is a template-id. */ RETURN_IF_ERROR (demangle_substitution (dm, encode_return_type)); else { /* It's just a name. */ RETURN_IF_ERROR (demangle_unqualified_name (dm, &suppress_return_type)); *encode_return_type = 0; } } else if (peek == 'Z') RETURN_IF_ERROR (demangle_local_name (dm)); else if (peek == 'I') { RETURN_IF_ERROR (demangle_template_args (dm)); /* Now we want to indicate to the caller that we've demangled template arguments, thus the prefix was a <template-prefix>. That's so that the caller knows to demangle the function's return type, if this turns out to be a function name. But, if it's a member template constructor or a templated conversion operator, report it as untemplated. Those never get encoded return types. */ *encode_return_type = !suppress_return_type; } else if (peek == 'E') /* All done. */ return STATUS_OK; else return "Unexpected character in <compound-name>."; if (peek != 'S' && peek_char (dm) != 'E') /* Add a new substitution for the prefix thus far. */ RETURN_IF_ERROR (substitution_add (dm, start, *encode_return_type)); }}/* Demangles and emits an <unqualified-name>. If this <unqualified-name> is for a special function type that should never have its return type encoded (particularly, a constructor or conversion operator), *SUPPRESS_RETURN_TYPE is set to 1; otherwise, it is set to zero. <unqualified-name> ::= <operator-name> ::= <special-name> ::= <source-name> */static status_tdemangle_unqualified_name (dm, suppress_return_type) demangling_t dm; int *suppress_return_type;{ char peek = peek_char (dm); DEMANGLE_TRACE ("unqualified-name", dm); /* By default, don't force suppression of the return type (though non-template functions still don't get a return type encoded). */ *suppress_return_type = 0; if (IS_DIGIT ((unsigned char) peek)) RETURN_IF_ERROR (demangle_source_name (dm)); else if (peek >= 'a' && peek <= 'z') { int num_args; /* Conversion operators never have a return type encoded. */ if (peek == 'c' && peek_char_next (dm) == 'v')
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?