-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
asttypename.d
117 lines (108 loc) · 2.98 KB
/
asttypename.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Part of the Compiler implementation of the D programming language
* Copyright: Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
* Authors: Stefan Koch
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/asttypename.d, _asttypename.d)
* Documentation: https://dlang.org/phobos/dmd_asttypename.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/asttypename.d
*/
module dmd.asttypename;
import dmd.attrib;
import dmd.aliasthis;
import dmd.aggregate;
import dmd.complex;
import dmd.cond;
import dmd.ctfeexpr;
import dmd.dclass;
import dmd.declaration;
import dmd.denum;
import dmd.dimport;
import dmd.declaration;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.dversion;
import dmd.expression;
import dmd.func;
import dmd.denum;
import dmd.dimport;
import dmd.dmodule;
import dmd.mtype;
import dmd.typinf;
import dmd.identifier;
import dmd.init;
import dmd.doc;
import dmd.root.rootobject;
import dmd.statement;
import dmd.staticassert;
import dmd.nspace;
import dmd.visitor;
/// Returns: the typename of the dynamic ast-node-type
/// (this is a development tool, do not use in actual code)
string astTypeName(RootObject node)
{
final switch (node.dyncast())
{
case DYNCAST.object:
return "RootObject";
case DYNCAST.identifier:
return "Identifier";
case DYNCAST.expression:
return astTypeName(cast(Expression) node);
case DYNCAST.dsymbol:
return astTypeName(cast(Dsymbol) node);
case DYNCAST.type:
return astTypeName(cast(Type) node);
case DYNCAST.tuple:
return astTypeName(cast(Tuple) node);
case DYNCAST.parameter:
return astTypeName(cast(Parameter) node);
case DYNCAST.statement:
return astTypeName(cast(Statement) node);
case DYNCAST.condition:
return astTypeName(cast(Condition) node);
}
}
mixin
({
string astTypeNameFunctions;
string visitOverloads;
foreach (ov; __traits(getOverloads, Visitor, "visit"))
{
static if (is(typeof(ov) P == function))
{
static if (is(P[0] S == super) && is(S[0] == RootObject))
{
astTypeNameFunctions ~= `
string astTypeName(` ~ P[0].stringof ~ ` node)
{
scope tsv = new AstTypeNameVisitor;
node.accept(tsv);
return tsv.typeName;
}
`;
}
visitOverloads ~= `
override void visit (` ~ P[0].stringof ~ ` _)
{
typeName = "` ~ P[0].stringof ~ `";
}
`;
}
}
return astTypeNameFunctions ~ `
private extern(C++) final class AstTypeNameVisitor : Visitor
{
alias visit = Visitor.visit;
public :
string typeName;
` ~ visitOverloads ~ "}";
}());
///
unittest
{
import dmd.globals : Loc;
Expression e = new TypeidExp(Loc.init, null);
assert(e.astTypeName == "TypeidExp");
}