Skip to content

Commit ffe5e23

Browse files
committed
deduplicate the 'isSomeFunction' equivalent
1 parent f9db662 commit ffe5e23

1 file changed

Lines changed: 48 additions & 60 deletions

File tree

src/ddmd/traits.d

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import ddmd.identifier;
3030
import ddmd.mtype;
3131
import ddmd.nogc;
3232
import ddmd.root.array;
33+
import ddmd.root.rootobject;
3334
import ddmd.root.speller;
3435
import ddmd.root.stringtable;
3536
import ddmd.tokens;
@@ -403,6 +404,37 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
403404
return new IntegerExp(e.loc, false, Type.tbool);
404405
}
405406

407+
static TypeFunction traitsFuncArg(RootObject o, FuncDeclaration* fdp = null)
408+
{
409+
auto s = getDsymbol(o);
410+
auto t = isType(o);
411+
TypeFunction tf = null;
412+
413+
if (s)
414+
{
415+
auto fd = s.isFuncDeclaration();
416+
if (fd)
417+
{
418+
t = fd.type;
419+
if (fdp)
420+
*fdp = fd;
421+
}
422+
else if (auto vd = s.isVarDeclaration())
423+
t = vd.type;
424+
}
425+
if (t)
426+
{
427+
if (t.ty == Tfunction)
428+
tf = cast(TypeFunction)t;
429+
else if (t.ty == Tdelegate)
430+
tf = cast(TypeFunction)t.nextOf();
431+
else if (t.ty == Tpointer && t.nextOf().ty == Tfunction)
432+
tf = cast(TypeFunction)t.nextOf();
433+
}
434+
435+
return tf;
436+
}
437+
406438
Expression isX(T)(bool function(T) fp)
407439
{
408440
if (!dim)
@@ -878,26 +910,8 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
878910
if (dim != 1)
879911
return dimError(1);
880912

881-
auto o = (*e.args)[0];
882-
auto s = getDsymbol(o);
883-
auto t = isType(o);
884-
TypeFunction tf = null;
885-
if (s)
886-
{
887-
if (auto fd = s.isFuncDeclaration())
888-
t = fd.type;
889-
else if (auto vd = s.isVarDeclaration())
890-
t = vd.type;
891-
}
892-
if (t)
893-
{
894-
if (t.ty == Tfunction)
895-
tf = cast(TypeFunction)t;
896-
else if (t.ty == Tdelegate)
897-
tf = cast(TypeFunction)t.nextOf();
898-
else if (t.ty == Tpointer && t.nextOf().ty == Tfunction)
899-
tf = cast(TypeFunction)t.nextOf();
900-
}
913+
TypeFunction tf = traitsFuncArg((*e.args)[0]);
914+
901915
if (!tf)
902916
{
903917
e.error("first argument is not a function");
@@ -928,27 +942,18 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
928942
LINK link;
929943
int varargs;
930944
auto o = (*e.args)[0];
931-
auto t = isType(o);
932-
TypeFunction tf = null;
933-
if (t)
934-
{
935-
if (t.ty == Tfunction)
936-
tf = cast(TypeFunction)t;
937-
else if (t.ty == Tdelegate)
938-
tf = cast(TypeFunction)t.nextOf();
939-
else if (t.ty == Tpointer && t.nextOf().ty == Tfunction)
940-
tf = cast(TypeFunction)t.nextOf();
941-
}
945+
946+
FuncDeclaration fd;
947+
TypeFunction tf = traitsFuncArg(o, &fd);
948+
942949
if (tf)
943950
{
944951
link = tf.linkage;
945952
varargs = tf.varargs;
946953
}
947954
else
948955
{
949-
auto s = getDsymbol(o);
950-
FuncDeclaration fd;
951-
if (!s || (fd = s.isFuncDeclaration()) is null)
956+
if (!fd)
952957
{
953958
e.error("argument to `__traits(getFunctionVariadicStyle, %s)` is not a function", o.toChars());
954959
return new ErrorExp();
@@ -978,29 +983,20 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
978983
if (dim != 2)
979984
return dimError(2);
980985

981-
auto o1 = (*e.args)[1];
982986
auto o = (*e.args)[0];
983-
auto t = isType(o);
984-
TypeFunction tf = null;
985-
if (t)
986-
{
987-
if (t.ty == Tfunction)
988-
tf = cast(TypeFunction)t;
989-
else if (t.ty == Tdelegate)
990-
tf = cast(TypeFunction)t.nextOf();
991-
else if (t.ty == Tpointer && t.nextOf().ty == Tfunction)
992-
tf = cast(TypeFunction)t.nextOf();
993-
}
987+
auto o1 = (*e.args)[1];
988+
989+
FuncDeclaration fd;
990+
TypeFunction tf = traitsFuncArg(o, &fd);
991+
994992
Parameters* fparams;
995993
if (tf)
996994
{
997995
fparams = tf.parameters;
998996
}
999997
else
1000998
{
1001-
auto s = getDsymbol(o);
1002-
FuncDeclaration fd;
1003-
if (!s || (fd = s.isFuncDeclaration()) is null)
999+
if (!fd)
10041000
{
10051001
e.error("first argument to `__traits(getParameterStorageClasses, %s, %s)` is not a function",
10061002
o.toChars(), o1.toChars());
@@ -1080,17 +1076,9 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
10801076

10811077
LINK link;
10821078
auto o = (*e.args)[0];
1083-
auto t = isType(o);
1084-
TypeFunction tf = null;
1085-
if (t)
1086-
{
1087-
if (t.ty == Tfunction)
1088-
tf = cast(TypeFunction)t;
1089-
else if (t.ty == Tdelegate)
1090-
tf = cast(TypeFunction)t.nextOf();
1091-
else if (t.ty == Tpointer && t.nextOf().ty == Tfunction)
1092-
tf = cast(TypeFunction)t.nextOf();
1093-
}
1079+
1080+
TypeFunction tf = traitsFuncArg(o);
1081+
10941082
if (tf)
10951083
link = tf.linkage;
10961084
else

0 commit comments

Comments
 (0)