📄 template.c
字号:
static short *replace2(char *buf, int *max, int *len, LIST *to, LIST *tn)
{
short *buf2;
global_flag++;
buf2 = xalloc(*max * sizeof(short));
memcpy(buf2, buf, *max * sizeof(short));
buf = buf2;
while (to && tn)
{
if (((TYP*)to->data)->sp->istemplatevar)
buf = llreplace(buf, max, len, to->data, tn->data);
to = to->link;
tn = tn->link;
}
global_flag--;
return buf;
}
//-------------------------------------------------------------------------
static short *replace(struct _templatelist *tl, struct template *tm)
{
int max = tm->linemax;
int len = tm->linelen;
short *buf;
LIST *to = tl->oldtypes;
LIST *tn = tl->newtypes;
return replace2(tm->lines, &max, &len, to, tn);
} void setunaltered(void)
{
short *p = interjectptr;
short *q = unalteredline;
while (*p && *p != '\n')
*q++ = *p++;
*q++ = '\n';
*q++ = 0;
}
//-------------------------------------------------------------------------
int tlookup(TYP *t, struct template *tpl)
{
SYM *s1;
SYM *s2 = tpl->classes.head;
int i = 0;
if (t->type == bt_ref)
t = t->btp;
if (!isstructured(t))
return - 1;
s1 = t->sp;
for (i = 0; s2; i++, s2 = s2->next)
{
if (s2->tp->type != bt_class)
continue;
if (exactype(s1, s2, FALSE))
return i;
} return - 1;
}
//-------------------------------------------------------------------------
int tlookuptp(TYP *f, TYP *t, struct template *tpl, TYP **rv)
{
if (t->type == bt_ref)
t = t->btp;
if (t->type == bt_templateplaceholder)
{
if (f->sp->value.classdata.templateparent && f->sp
->value.classdata.templateparent->mainsym == t->lst.head->mainsym)
{
LIST *tn = t->lst.tail;
LIST *fn = f->sp->value.classdata.templateargs;
while (tn && fn)
{
int index = tlookup(tn->data, tpl);
if (index != - 1)
{
*rv = fn->data;
return index;
} if (!isstructured(tn->data))
{
SYM *s2 = tpl->classes.head;
int i = 0;
while (s2)
{
if (!isstructured(s2->tp) && !strcmp(s2->name, ((TYP*)
tn->data)->sp->name))
{
*rv = fn->data;
return i;
}
i++;
s2 = s2->next;
}
}
index = tlookuptp(fn, tn, tpl, rv);
if (index >= 0)
return index;
fn = fn->link;
tn = tn->link;
}
}
}
return - 1;
}
//-------------------------------------------------------------------------
static struct _templatelist *conjoint(SYM *fi, SYM *ti, SYM *sp, char *name)
{
char mname[512], mname2[512], *p;
struct _templatelist *rv;
struct template *tpl = sp->value.classdata.templatedata;
TYP *tp[100];
LIST **t1, **x1;
SYM *f = fi;
SYM *t = ti;
int i, partial = 0;
int max = 0;
SYM *s = tpl->classes.head;
#ifdef XXXXX
while (s)
{
if (s->tp->type != bt_class)
return 0;
s = s->next;
}
#endif
memset(tp, 0, sizeof(tp));
while (f && t)
{
int index;
index = tlookup(t->tp, tpl);
if (index < 0)
{
if (!exactype(f->tp, t->tp, FALSE))
{
TYP *x;
index = tlookuptp(f->tp, t->tp, tpl, &x);
if (index < 0)
return 0;
if (index >= max)
max = index + 1;
if (tp[index])
{
if (!exactype(x, tp[index], FALSE))
return 0;
}
else
tp[index] = x;
}
else
partial++;
}
else
{
if (index >= max)
max = index + 1;
if (tp[index])
{
if (!exactype(f->tp, tp[index], FALSE))
return 0;
}
else
tp[index] = f->tp;
}
f = f->next;
t = t->next;
}
if (f || t)
return 0;
t = tpl->classes.head;
for (i = 0; t; i++, t = t->next)
{
if (!tp[i])
{
if (t->defalt)
tp[i] = t->defalt;
else
return 0;
}
}
if (prm_cmangle && name[0] == '_')
name++;
sprintf(mname, "@#%s$t", name);
p = mname + strlen(mname);
manglenamecount = 0;
for (i = 0; i < max; i++)
{
p = cpponearg(p, tp[i]);
}
*p++ = '#';
*p = 0;
manglenamecount = 0;
strcpy(mname2, mname);
p = mname2 + strlen(mname2);
*p++ = '$';
*p++ = 'q';
p = cppargs(p, fi);
*p = 0;
manglenamecount = - 1;
rv = (struct _templatelist*)search(mname2, (TABLE*)templateFuncs);
if (rv)
return rv;
t = tpl->classes.head;
global_flag++;
rv = xalloc(sizeof(struct _templatelist));
rv->partialcount = partial;
rv->oldtypes = 0;
rv->newtypes = 0;
t1 = &rv->oldtypes;
x1 = &rv->newtypes;
for (i = 0; t; i++, t = t->next)
{
LIST *l = xalloc(sizeof(LIST));
l->data = t->tp;
*t1 = l;
t1 = &(*t1)->link;
l = xalloc(sizeof(LIST));
l->data = tp[i];
*x1 = l;
x1 = &(*x1)->link;
} rv->sp = sp;
rv->partialcount = partial;
rv->name = litlate(mname2);
rv->prefixname = litlate(mname);
global_flag--;
return rv;
}
//-------------------------------------------------------------------------
static struct _templatelist *search_template_func(SYM *tbsym, TYP *tp, char
*name)
{
SYM *sp1 = tbsym->tp->lst.head;
int found1 = FALSE;
struct _templatelist *hold = 0, *hold1 = 0;
char intrnm[256], *p = intrnm, *nm = tbsym->name;
cpp_unmang_intrins(&p, &nm, tbsym->parentclass ? tbsym->parentclass->name:
0);
while (sp1)
{
if (sp1->value.classdata.templatedata)
{
struct _templatelist *s;
found1 = TRUE;
// not sure templateFuncs is used...
s = conjoint(tp->lst.head, sp1->tp->lst.head, sp1, tbsym->name);
if (s)
{
if (!hold)
hold = s;
else if (s->sp->value.classdata.templatedata->partials > hold
->sp->value.classdata.templatedata->partials)
{
hold = s;
hold1 = 0;
}
else if (!hold1)
hold1 = s;
else if (s->sp->value.classdata.templatedata->partials > hold1
->sp->value.classdata.templatedata->partials)
{
hold1 = s;
}
}
}
sp1 = sp1->next;
}
if (hold == 0)
if (found1)
gensymerror(ERR_AMBIGTEMPLATE, intrnm);
else
gensymerror(ERR_NOFUNCMATCH, intrnm);
else if (hold1 == 0)
return hold;
else if (hold1->sp->value.classdata.templatedata->partials > hold->sp
->value.classdata.templatedata->partials)
return hold1;
else if (hold->sp->value.classdata.templatedata->partials > hold1->sp
->value.classdata.templatedata->partials)
return hold;
else
gensymerror(ERR_AMBIGTEMPLATE, intrnm);
return 0;
}
//-------------------------------------------------------------------------
static TYP *tplconst(void)
{
ENODE *node;
TYP *tp;
if (lastst == sconst)
{
tp = maketype(bt_pointer, laststrlen);
tp->btp = maketype(bt_char, laststrlen);
tp->sp = makesym(sc_tconst);
tp->sp->value.s = litlate(laststr);
getsym();
}
else if (lastst == lsconst)
{
tp = maketype(bt_pointer, laststrlen);
tp->btp = maketype(bt_short, laststrlen *2);
tp->sp = makesym(sc_tconst);
tp->sp->value.s = xalloc(laststrlen *2+2);
memcpy(tp->btp->sp->value.s, laststr, laststrlen *2+2);
getsym();
}
else
{
intemplateargs = TRUE;
tp = exprnc(&node);
opt4(&node);
intemplateargs = FALSE;
if (!tp)
{
tp = maketype(bt_int, 4);
tp->sp = makesym(sc_tconst);
generror(ERR_NEEDCONST, 0, 0);
}
else if (!node)
{
tp->sp = makesym(sc_tconst);
generror(ERR_NEEDCONST, 0, 0);
}
else if (isintconst(node->nodetype))
{
tp = copytype(tp, 0);
tp->sp = makesym(sc_tconst);
tp->sp->value.i = node->v.i;
tp->cflags &= ~DF_CONST;
}
else if (isfloatconst(node->nodetype))
{
tp = copytype(tp, 0);
tp->sp = makesym(sc_tconst);
tp->sp->value.f = node->v.f;
tp->cflags &= ~DF_CONST;
}
else
{
tp->sp = makesym(sc_tconst);
generror(ERR_NEEDCONST, 0, 0);
}
}
return tp;
}
//-------------------------------------------------------------------------
static void matchandmangle(char *mangname, char *nm, LIST **tail, SYM *tplargs)
{
char *p;
int err = 0;
matching++;
if (nm)
{
manglenamecount = 0;
if (prm_cmangle && *nm == '_')
nm++;
sprintf(mangname, "@#%s$t", nm);
p = mangname + strlen(mangname);
}
if (lastst != gt)
do
{
if (castbegin(lastst))
{
if (typequal && typequal->istemplate)
{
needpunc(gt, 0);
head = typequal->tp;
typequal = 0;
}
else
{
decl(0, 0);
headptr = &head;
decl1(sc_type, 0);
}
if (tail)
{
*tail = xalloc(sizeof(LIST));
(*tail)->data = head;
tail = &(*tail)->link;
}
}
else
{
head = tplconst();
if (tplargs != (SYM*) - 1)
head->type = tplargs->tp->type;
if (tail)
{
*tail = xalloc(sizeof(LIST));
(*tail)->data = head;
tail = &(*tail)->link;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -