Skip to content

Commit

Permalink
Merge pull request #3109 from yebblies/toctype2
Browse files Browse the repository at this point in the history
[refactor] We don't need a result variable as t->ctype caches the value
  • Loading branch information
yebblies committed Jan 18, 2014
2 parents 015f85e + a545225 commit 7991b7f
Showing 1 changed file with 31 additions and 72 deletions.
103 changes: 31 additions & 72 deletions src/toctype.c
Expand Up @@ -38,95 +38,67 @@ type *Type_toCtype(Type *t);
class ToCtypeVisitor : public Visitor
{
public:
type *result;

ToCtypeVisitor() {}

void visit(Type *t)
{
if (!t->ctype)
{
t->ctype = type_fake(t->totym());
t->ctype->Tcount++;
}
result = t->ctype;
t->ctype = type_fake(t->totym());
t->ctype->Tcount++;
}

void visit(TypeSArray *t)
{
if (!t->ctype)
t->ctype = type_static_array(t->dim->toInteger(), Type_toCtype(t->next));
result = t->ctype;
t->ctype = type_static_array(t->dim->toInteger(), Type_toCtype(t->next));
}

void visit(TypeDArray *t)
{
if (!t->ctype)
{
t->ctype = type_dyn_array(Type_toCtype(t->next));
t->ctype->Tident = t->toChars(); // needed to generate sensible debug info for cv8
}
result = t->ctype;
t->ctype = type_dyn_array(Type_toCtype(t->next));
t->ctype->Tident = t->toChars(); // needed to generate sensible debug info for cv8
}

void visit(TypeAArray *t)
{
if (!t->ctype)
t->ctype = type_assoc_array(Type_toCtype(t->index), Type_toCtype(t->next));
result = t->ctype;
t->ctype = type_assoc_array(Type_toCtype(t->index), Type_toCtype(t->next));
}

void visit(TypePointer *t)
{
//printf("TypePointer::toCtype() %s\n", t->toChars());
if (!t->ctype)
t->ctype = type_pointer(Type_toCtype(t->next));
result = t->ctype;
t->ctype = type_pointer(Type_toCtype(t->next));
}

void visit(TypeFunction *t)
{
if (!t->ctype)
{
size_t nparams = Parameter::dim(t->parameters);
size_t nparams = Parameter::dim(t->parameters);

type *tmp[10];
type **ptypes = tmp;
if (nparams > 10)
ptypes = (type **)malloc(sizeof(type*) * nparams);
type *tmp[10];
type **ptypes = tmp;
if (nparams > 10)
ptypes = (type **)malloc(sizeof(type*) * nparams);

for (size_t i = 0; i < nparams; i++)
{
Parameter *arg = Parameter::getNth(t->parameters, i);
type *tp = Type_toCtype(arg->type);
if (arg->storageClass & (STCout | STCref))
tp = type_allocn(TYref, tp);
ptypes[i] = tp;
}
for (size_t i = 0; i < nparams; i++)
{
Parameter *arg = Parameter::getNth(t->parameters, i);
type *tp = Type_toCtype(arg->type);
if (arg->storageClass & (STCout | STCref))
tp = type_allocn(TYref, tp);
ptypes[i] = tp;
}

t->ctype = type_function(t->totym(), ptypes, nparams, t->varargs == 1, Type_toCtype(t->next));
t->ctype = type_function(t->totym(), ptypes, nparams, t->varargs == 1, Type_toCtype(t->next));

if (nparams > 10)
free(ptypes);
}
result = t->ctype;
if (nparams > 10)
free(ptypes);
}

void visit(TypeDelegate *t)
{
if (!t->ctype)
t->ctype = type_delegate(Type_toCtype(t->next));
result = t->ctype;
t->ctype = type_delegate(Type_toCtype(t->next));
}

void visit(TypeStruct *t)
{
if (t->ctype)
{
result = t->ctype;
return;
}

//printf("TypeStruct::toCtype() '%s'\n", t->sym->toChars());
Type *tm = t->mutableOf();
if (tm->ctype)
Expand Down Expand Up @@ -187,17 +159,10 @@ class ToCtypeVisitor : public Visitor
}

//printf("t = %p, Tflags = x%x\n", ctype, ctype->Tflags);
result = t->ctype;
}

void visit(TypeEnum *t)
{
if (t->ctype)
{
result = t->ctype;
return;
}

//printf("TypeEnum::toCtype() '%s'\n", t->sym->toChars());
Type *tm = t->mutableOf();
if (tm->ctype && tybasic(tm->ctype->Tty) == TYenum)
Expand Down Expand Up @@ -246,23 +211,16 @@ class ToCtypeVisitor : public Visitor
}

//printf("t = %p, Tflags = x%x\n", t, t->Tflags);
result = t->ctype;
}

void visit(TypeTypedef *t)
{
result = Type_toCtype(t->sym->basetype);
t->ctype = Type_toCtype(t->sym->basetype);
}

void visit(TypeClass *t)
{
//printf("TypeClass::toCtype() %s\n", toChars());
if (t->ctype)
{
result = t->ctype;
return;
}

type *tc = type_struct_class(t->sym->toPrettyChars(), t->sym->alignsize, t->sym->structsize,
NULL,
NULL,
Expand All @@ -283,14 +241,15 @@ class ToCtypeVisitor : public Visitor
symbol_struct_addField(tc->Ttag, v->ident->toChars(), Type_toCtype(v->type), v->offset);
}
}

result = t->ctype;
}
};

type *Type_toCtype(Type *t)
{
ToCtypeVisitor v;
t->accept(&v);
return v.result;
if (!t->ctype)
{
ToCtypeVisitor v;
t->accept(&v);
}
return t->ctype;
}

0 comments on commit 7991b7f

Please sign in to comment.