📄 mangle.c
字号:
//-------------------------------------------------------------------------
SYM *castmatch(SYM *sp, TYP *tp, TABLE *tb)
{
SYM *sp1, *sp2 = 0;
char name[256], *nm1;
int temp = tp->cflags;
tp->cflags = 0;
name[0] = '$';
name[1] = 'o';
cpponearg(name + 2, tp);
nm1 = fullcppmangle(sp, name, &stdvoidfunc);
sp1 = search(nm1, tb);
tp->cflags = temp;
return sp1;
}
//-------------------------------------------------------------------------
/* Mangle one C++ argument */
static void mname(char *buf, SYM *sp)
{
char *v;
char *bufin = buf;
char ibuf[512];
if (!sp)
return ;
buf[0] = 0;
if (sp->parentclass)
{
mname(buf, sp->parentclass);
strcat(buf, "@");
buf = buf + strlen(buf);
}
v = sp->name;
if (!v)
v = "$$bad";
if (prm_cmangle && *v == '_')
v++;
strcat(buf, v);
if (sp->value.classdata.parentns)
{
v = sp->value.classdata.parentns->sp->name;
if (prm_cmangle && *v == '_')
v++;
strcpy(ibuf, v);
strcat(ibuf, "@");
strcat(ibuf, bufin);
strcpy(bufin, ibuf);
}
}
int lookupname(char *name)
{
int i;
if (manglenamecount < 0)
return 0;
for (i = 0; i < manglenamecount; i++)
if (!strcmp(manglenames[i], name))
{
if (i >= 10)
i += 7;
return i + '0';
}
if (manglenamecount >= MAX_MANGLE_NAME_COUNT)
return 0;
strcpy(manglenames[manglenamecount++], name);
return 0;
}
static char *putvc(char *buf, TYP *tp)
{
if (tp->cflags &DF_CONST)
*buf++ = 'x';
if (tp->cflags &DF_VOL)
*buf++ = 'y';
return buf;
}
char *cppval(char *buf, TYP *tp)
{
int i;
if (tp->sp && tp->sp->storage_class == sc_tconst)
{
*buf++ = '$';
switch (tp->type)
{
case bt_pointer:
if (tp->btp->type == bt_char)
{
sprintf(buf, "s%s", tp->sp->value.s);
}
else if (tp->btp->type == bt_short)
{
*buf++ = 'w';
for (i = 0; i < tp->size; i++)
{
sprintf(buf, "%04X", ((short*)tp->sp->value.s)[i]);
buf += 4;
}
break;
}
break;
case bt_bool:
if (tp->sp->value.i)
strcpy(buf, "btrue");
else
strcpy(buf, "bfalse");
break;
case bt_char:
case bt_short:
case bt_enum:
case bt_unsignedchar:
case bt_unsignedshort:
case bt_int:
case bt_long:
case bt_longlong:
case bt_unsignedlonglong:
case bt_unsigned:
case bt_unsignedlong:
//#if sizeof(ULLONG_TYPE) == 4
sprintf(buf, "i%d", tp->sp->value.i);
//#else
// sprintf(buf,"i%lld",tp->sp->value.i) ;
//#endif
case bt_fcomplex:
case bt_rcomplex:
case bt_lrcomplex:
break;
case bt_float:
case bt_fimaginary:
case bt_rimaginary:
case bt_lrimaginary:
case bt_double:
case bt_longdouble:
sprintf(buf, "f%f", tp->sp->value.f);
break;
}
buf += strlen(buf);
*buf++ = '$';
*buf = 0;
}
return buf;
}
void tplPlaceholder(char *buf, char *nm, LIST *tn)
{
*buf++ = '#';
nm += prm_cmangle && nm[0] == '_';
strcpy(buf, nm);
buf += strlen(buf);
*buf++ = '$';
*buf++ = 't';
while (tn)
{
buf = cpponearg(buf, tn->data);
tn = tn->link;
}
*buf++ = '#';
*buf = 0;
}
char *cpponearg(char *buf, TYP *tp)
{
char nm[512];
int i;
start: if (tp->type != bt_pointer || tp->btp->type != bt_func)
buf = putvc(buf, tp);
switch (tp->type)
{
case bt_templateplaceholder:
tplPlaceholder(nm, tp->lst.head->name, tp->lst.tail);
sprintf(buf, "%d%s", strlen(nm), nm);
buf += strlen(buf);
break;
case bt_memberptr:
*buf++ = 'M';
mname(nm, tp->sp);
if (!(i = lookupname(nm)))
sprintf(buf, "%d%s", strlen(nm), nm);
else
sprintf(buf, "n%c", i);
buf = buf + strlen(buf);
if (tp->btp->type == bt_func)
{
buf = putvc(buf, tp->btp);
*buf++ = 'q';
buf = cppargs(buf, tp->btp->lst.head);
*buf++ = '$';
buf = cpponearg(buf, tp->btp->btp);
}
else
{
buf = cpponearg(buf, tp->btp);
}
break;
case bt_enum:
mname(nm, tp->btp);
if (!(i = lookupname(nm)))
sprintf(buf, "%d%s", strlen(nm), nm);
else
sprintf(buf, "n%c", i);
buf = buf + strlen(buf);
break;
case bt_struct:
case bt_union:
case bt_class:
mname(nm, tp->sp);
if (!(i = lookupname(nm)))
sprintf(buf, "%d%s", strlen(nm), nm);
else
sprintf(buf, "n%c", i);
buf = buf + strlen(buf);
break;
case bt_bool:
strcpy(buf, "4bool");
buf += 5;
break;
case bt_unsignedshort:
*buf++ = 'u';
case bt_short:
*buf++ = 's';
buf = cppval(buf, tp);
break;
case bt_unsigned:
*buf++ = 'u';
case bt_int:
*buf++ = 'i';
buf = cppval(buf, tp);
break;
case bt_unsignedlong:
*buf++ = 'u';
case bt_long:
*buf++ = 'l';
buf = cppval(buf, tp);
break;
case bt_unsignedlonglong:
*buf++ = 'u';
case bt_longlong:
*buf++ = 'L';
buf = cppval(buf, tp);
break;
case bt_unsignedchar:
*buf++ = 'u';
case bt_char:
*buf++ = 'c';
buf = cppval(buf, tp);
break;
case bt_fcomplex:
*buf++ = 'F';
buf = cppval(buf, tp);
break;
case bt_rcomplex:
*buf++ = 'R';
buf = cppval(buf, tp);
break;
case bt_lrcomplex:
*buf++ = 'G';
buf = cppval(buf, tp);
break;
case bt_float:
*buf++ = 'f';
buf = cppval(buf, tp);
break;
case bt_double:
*buf++ = 'd';
buf = cppval(buf, tp);
break;
case bt_longdouble:
*buf++ = 'g';
buf = cppval(buf, tp);
break;
case bt_pointer:
if (tp->btp->type == bt_func)
{
buf = putvc(buf, tp->btp);
*buf++ = 'p';
*buf++ = 'q';
buf = cppargs(buf, tp->btp->lst.head);
*buf++ = '$';
tp = tp->btp->btp;
}
else
{
*buf++ = 'p';
buf = cpponearg(buf, tp->btp);
buf = cppval(buf, tp);
break;
}
goto start;
case bt_farpointer:
if (tp->btp->type == bt_func)
{
buf = putvc(buf, tp->btp);
*buf++ = 'Q';
buf = cppargs(buf, tp->lst.head);
*buf++ = '$';
tp = tp->btp->btp;
}
else
{
*buf++ = 'P';
tp = tp->btp;
}
goto start;
case bt_ref:
*buf++ = 'r';
tp = tp->btp;
goto start;
case bt_ellipse:
*buf++ = 'e';
break;
case bt_void:
*buf++ = 'v';
break;
}
*buf = 0;
return buf;
}
/* Mangle an entire C++ function */
char *cppargs(char *buf, SYM *sp)
{
if (sp == (SYM*) - 1)
{
*buf++ = 'v';
}
else
while (sp)
{
buf = cpponearg(buf, sp->tp);
sp = sp->next;
}
*buf = 0;
return buf;
}
/* Wrapper for function name mangling */
char *cppmangle(char *name, TYP *tp)
{
char buf[100], *p = buf;
if (*name == 0)
return 0;
if (prm_cmangle && *name == '_')
name++;
if (tp)
{
sprintf(p, "@%s$", name);
p = p + strlen(p);
p = putvc(p, tp);
*p++ = 'q';
cppargs(p, tp->lst.head);
}
else
sprintf(p, "@%s", name);
return (litlate(buf));
}
static char *manglens(char *buf, NAMESPACE *ns)
{
if (!ns)
return buf;
buf = manglens(buf, ns->next);
buf = buf + strlen(buf);
*buf++ = '@';
strcpy(buf, ns->sp->name + prm_cmangle);
return buf + strlen(buf);
}
void mangleclass(char *buf, SYM *sl)
{
char *p;
if (!sl)
return ;
manglens(buf, sl->value.classdata.parentns);
if (!isstructured(sl->tp))
return ;
mangleclass(buf, sl->parentclass);
p = buf + strlen(buf);
if (sl->storage_class == sc_type)
{
*p++ = '@';
strcpy(p, sl->name + (sl->name[0] == '_' && prm_cmangle));
}
}
/* Wrapper for function name mangling */
char *fullcppmangle(SYM *class , char *name, TYP *tp)
{
char buf[200], *p = buf;
*p = 0;
if (*name == 0)
return 0;
if (prm_cmangle && *name == '_')
name++;
if (class )
mangleclass(buf, class );
else if (typequal)
mangleclass(buf, typequal);
else if (declclass)
mangleclass(buf, declclass);
p = buf + strlen(buf);
manglenamecount = 0;
if (templatePrefix)
{
strcpy(p, templatePrefix);
p = p + strlen(p);
if (tp)
{
*p++ = '$';
p = putvc(p, tp);
*p++ = 'q';
cppargs(p, tp->lst.head);
}
}
else if (tp && (tp->type == bt_func || tp->type == bt_ifunc))
{
sprintf(p, "@%s$", name);
p = p + strlen(p);
p = putvc(p, tp);
*p++ = 'q';
cppargs(p, tp->lst.head);
}
else
sprintf(p, "@%s", name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -