📄 cplus-dem.c
字号:
switch (**mangled) { case '\0': case '_': break; case 'v': (*mangled)++; APPEND_BLANK (result); string_append (result, "void"); break; case 'x': (*mangled)++; APPEND_BLANK (result); string_append (result, "long long"); break; case 'l': (*mangled)++; APPEND_BLANK (result); string_append (result, "long"); break; case 'i': (*mangled)++; APPEND_BLANK (result); string_append (result, "int"); break; case 's': (*mangled)++; APPEND_BLANK (result); string_append (result, "short"); break; case 'b': (*mangled)++; APPEND_BLANK (result); string_append (result, "bool"); break; case 'c': (*mangled)++; APPEND_BLANK (result); string_append (result, "char"); break; case 'w': (*mangled)++; APPEND_BLANK (result); string_append (result, "wchar_t"); break; case 'r': (*mangled)++; APPEND_BLANK (result); string_append (result, "long double"); break; case 'd': (*mangled)++; APPEND_BLANK (result); string_append (result, "double"); break; case 'f': (*mangled)++; APPEND_BLANK (result); string_append (result, "float"); break; case 'G': (*mangled)++; if (!isdigit (**mangled)) { success = 0; break; } /* fall through */ /* An explicit type, such as "6mytype" or "7integer" */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': APPEND_BLANK (result); if (!demangle_class_name (work, mangled, result)) { --result->p; success = 0; } break; case 't': success = demangle_template(work,mangled, result, 0); break; default: success = 0; break; } return (success);}/* `result' will be initialized in do_type; it will be freed on failure */static intdo_arg (work, mangled, result) struct work_stuff *work; const char **mangled; string *result;{ const char *start = *mangled; if (!do_type (work, mangled, result)) { return (0); } else { remember_type (work, start, *mangled - start); return (1); }}static voidremember_type (work, start, len) struct work_stuff *work; const char *start; int len;{ char *tem; if (work -> ntypes >= work -> typevec_size) { if (work -> typevec_size == 0) { work -> typevec_size = 3; work -> typevec = (char **) xmalloc (sizeof (char *) * work -> typevec_size); } else { work -> typevec_size *= 2; work -> typevec = (char **) xrealloc ((char *)work -> typevec, sizeof (char *) * work -> typevec_size); } } tem = xmalloc (len + 1); memcpy (tem, start, len); tem[len] = '\0'; work -> typevec[work -> ntypes++] = tem;}/* Forget the remembered types, but not the type vector itself. */static voidforget_types (work) struct work_stuff *work;{ int i; while (work -> ntypes > 0) { i = --(work -> ntypes); if (work -> typevec[i] != NULL) { free (work -> typevec[i]); work -> typevec[i] = NULL; } }}/* Process the argument list part of the signature, after any class spec has been consumed, as well as the first 'F' character (if any). For example: "__als__3fooRT0" => process "RT0" "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i" DECLP must be already initialised, usually non-empty. It won't be freed on failure. Note that g++ differs significantly from ARM and lucid style mangling with regards to references to previously seen types. For example, given the source fragment: class foo { public: foo::foo (int, foo &ia, int, foo &ib, int, foo &ic); }; foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; } void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; } g++ produces the names: __3fooiRT0iT2iT2 foo__FiR3fooiT1iT1 while lcc (and presumably other ARM style compilers as well) produces: foo__FiR3fooT1T2T1T2 __ct__3fooFiR3fooT1T2T1T2 Note that g++ bases it's type numbers starting at zero and counts all previously seen types, while lucid/ARM bases it's type numbers starting at one and only considers types after it has seen the 'F' character indicating the start of the function args. For lucid/ARM style, we account for this difference by discarding any previously seen types when we see the 'F' character, and subtracting one from the type number reference. */static intdemangle_args (work, mangled, declp) struct work_stuff *work; const char **mangled; string *declp;{ string arg; int need_comma = 0; int r; int t; const char *tem; char temptype; if (PRINT_ARG_TYPES) { string_append (declp, "("); if (**mangled == '\0') { string_append (declp, "void"); } } while (**mangled != '_' && **mangled != '\0' && **mangled != 'e') { if ((**mangled == 'N') || (**mangled == 'T')) { temptype = *(*mangled)++; if (temptype == 'N') { if (!get_count (mangled, &r)) { return (0); } } else { r = 1; } if (ARM_DEMANGLING && work -> ntypes >= 10) { /* If we have 10 or more types we might have more than a 1 digit index so we'll have to consume the whole count here. This will lose if the next thing is a type name preceded by a count but it's impossible to demangle that case properly anyway. Eg if we already have 12 types is T12Pc "(..., type1, Pc, ...)" or "(..., type12, char *, ...)" */ if ((t = consume_count(mangled)) == 0) { return (0); } } else { if (!get_count (mangled, &t)) { return (0); } } if (LUCID_DEMANGLING || ARM_DEMANGLING) { t--; } /* Validate the type index. Protect against illegal indices from malformed type strings. */ if ((t < 0) || (t >= work -> ntypes)) { return (0); } while (--r >= 0) { tem = work -> typevec[t]; if (need_comma && PRINT_ARG_TYPES) { string_append (declp, ", "); } if (!do_arg (work, &tem, &arg)) { return (0); } if (PRINT_ARG_TYPES) { string_appends (declp, &arg); } string_delete (&arg); need_comma = 1; } } else { if (need_comma & PRINT_ARG_TYPES) { string_append (declp, ", "); } if (!do_arg (work, mangled, &arg)) { return (0); } if (PRINT_ARG_TYPES) { string_appends (declp, &arg); } string_delete (&arg); need_comma = 1; } } if (**mangled == 'e') { (*mangled)++; if (PRINT_ARG_TYPES) { if (need_comma) { string_append (declp, ","); } string_append (declp, "..."); } } if (PRINT_ARG_TYPES) { string_append (declp, ")"); } return (1);}static voiddemangle_function_name (work, mangled, declp, scan) struct work_stuff *work; const char **mangled; string *declp; const char *scan;{ int i; int len; string type; const char *tem; string_appendn (declp, (*mangled), scan - (*mangled)); string_need (declp, 1); *(declp -> p) = '\0'; /* Consume the function name, including the "__" separating the name from the signature. We are guaranteed that SCAN points to the separator. */ (*mangled) = scan + 2; if (LUCID_DEMANGLING || ARM_DEMANGLING) { /* See if we have an ARM style constructor or destructor operator. If so, then just record it, clear the decl, and return. We can't build the actual constructor/destructor decl until later, when we recover the class name from the signature. */ if (strcmp (declp -> b, "__ct") == 0) { work -> constructor += 1; string_clear (declp); return; } else if (strcmp (declp -> b, "__dt") == 0) { work -> destructor += 1; string_clear (declp); return; } } if (declp->p - declp->b >= 3 && declp->b[0] == 'o' && declp->b[1] == 'p' && strchr (cplus_markers, declp->b[2]) != NULL) { /* see if it's an assignment expression */ if (declp->p - declp->b >= 10 /* op$assign_ */ && memcmp (declp->b + 3, "assign_", 7) == 0) { for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++) { len = declp->p - declp->b - 10; if (strlen (optable[i].in) == len && memcmp (optable[i].in, declp->b + 10, len) == 0) { string_clear (declp); string_append (declp, "operator"); string_append (declp, optable[i].out); string_append (declp, "="); break; } } } else { for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++) { int len = declp->p - declp->b - 3; if (strlen (optable[i].in) == len && memcmp (optable[i].in, declp->b + 3, len) == 0) { string_clear (declp); string_append (declp, "operator"); string_append (declp, optable[i].out); break; } } } } else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0 && strchr (cplus_markers, declp->b[4]) != NULL) { /* type conversion operator */ tem = declp->b + 5; if (do_type (work, &tem, &type)) { string_clear (declp); string_append (declp, "operator "); string_appends (declp, &type); string_delete (&type); } } else if (declp->b[0] == '_' && declp->b[1] == '_' && declp->b[2] == 'o' && declp->b[3] == 'p') { /* ANSI. */ /* type conversion operator. */ tem = declp->b + 4; if (do_type (work, &tem, &type)) { string_clear (declp); string_append (declp, "operator "); string_appends (declp, &type); string_delete (&type); } } else if (declp->b[0] == '_' && declp->b[1] == '_' && declp->b[2] >= 'a' && declp->b[2] <= 'z' && declp->b[3] >= 'a' && declp->b[3] <= 'z') { if (declp->b[4] == '\0') { /* Operator. */ for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++) { if (strlen (optable[i].in) == 2 && memcmp (optable[i].in, declp->b + 2, 2) == 0) { string_clear (declp); string_append (declp, "operator"); string_append (declp, optable[i].out); break; } } } else { if (declp->b[2] == 'a' && declp->b[5] == '\0') { /* Assignment. */ for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++) { if (strlen (optable[i].in) == 3 && memcmp (optable[i].in, declp->b + 2, 3) == 0) { string_clear (declp); string_append (declp, "operator"); string_append (declp, optable[i].out); break; } } } } }}/* a mini string-handling package */static voidstring_need (s, n) string *s; int n;{ int tem; if (s->b == NULL) { if (n < 32) { n = 32; } s->p = s->b = xmalloc (n); s->e = s->b + n; } else if (s->e - s->p < n) { tem = s->p - s->b; n += tem; n *= 2; s->b = xrealloc (s->b, n); s->p = s->b + tem; s->e = s->b + n; }}static voidstring_delete (s) string *s;{ if (s->b != NULL) { free (s->b); s->b = s->e = s->p = NULL; }}static voidstring_init (s) string *s;{ s->b = s->p = s->e = NULL;}static void string_clear (s) string *s;{ s->p = s->b;}#if 0static intstring_empty (s) string *s;{ return (s->b == s->p);}#endifstatic voidstring_append (p, s) string *p; const char *s;{ int n; if (s == NULL || *s == '\0') return; n = strle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -