Skip to content

Commit e353f09

Browse files
committed
Use UDA as replacement for macro Q_DECLARE_METATYPE
1 parent 17aedcc commit e353f09

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

src/cppconv/dwriter.d

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10039,6 +10039,92 @@ void writeAllDCode(string outputPath, bool outputIsDir, DCodeOptions options, Se
1003910039
}
1004010040
}
1004110041

10042+
void findQtMetaTypeId(Tree t)
10043+
{
10044+
if (!t.isValid)
10045+
return;
10046+
if (t.nodeType == NodeType.array)
10047+
{
10048+
foreach (c; t.childs)
10049+
findQtMetaTypeId(c);
10050+
}
10051+
else if (t.nodeType == NodeType.merged)
10052+
{
10053+
auto mdata = &mergedSemantic.mergedTreeData(t);
10054+
foreach (i, c; t.childs)
10055+
if (!mdata.conditions[i].isFalse)
10056+
findQtMetaTypeId(c);
10057+
}
10058+
else if (t.nodeType == NodeType.nonterminal
10059+
&& t.nonterminalID == CONDITION_TREE_NONTERMINAL_ID)
10060+
{
10061+
foreach (c; t.childs)
10062+
findQtMetaTypeId(c);
10063+
}
10064+
else if (t.nonterminalID == nonterminalIDFor!"ClassSpecifier")
10065+
{
10066+
findQtMetaTypeId(t.childs[0]);
10067+
}
10068+
else if (t.nonterminalID == nonterminalIDFor!"ClassHead" && t.hasChildWithName("name"))
10069+
{
10070+
findQtMetaTypeId(t.childByName("name"));
10071+
}
10072+
else if (t.nonterminalID == nonterminalIDFor!"ClassHeadName")
10073+
{
10074+
findQtMetaTypeId(t.childs[$ - 1]);
10075+
}
10076+
else if (t.nonterminalID == nonterminalIDFor!"SimpleTemplateId")
10077+
{
10078+
findQtMetaTypeId(t.childs[2]);
10079+
}
10080+
else if (t.nonterminalID == nonterminalIDFor!"TypeId")
10081+
{
10082+
auto type = mergedSemantic.extraInfo(t).type;
10083+
10084+
if (type.kind == TypeKind.record)
10085+
{
10086+
auto recordType = cast(RecordType) type.type;
10087+
foreach (e2; recordType.declarationSet.entries)
10088+
{
10089+
if (e2.data.type != DeclarationType.type)
10090+
continue;
10091+
if (e2.data.flags & DeclarationFlags.forward)
10092+
continue;
10093+
data.declarationData(e2.data)
10094+
.extraAttributes.addOnce("Q_DECLARE_METATYPE");
10095+
10096+
auto f = getDeclarationFilename(e2.data, data);
10097+
10098+
if (f !in data.importGraph)
10099+
data.importGraph[f] = null;
10100+
10101+
ImportInfo importInfo;
10102+
if ("qt.core.metatype" in data.importGraph[f])
10103+
{
10104+
importInfo = data.importGraph[f]["qt.core.metatype"];
10105+
importInfo.condition = mergedSemantic.logicSystem.or(importInfo.condition,
10106+
e2.condition);
10107+
importInfo.outsideFunction |= true;
10108+
}
10109+
else
10110+
{
10111+
importInfo = new ImportInfo;
10112+
data.importGraph[f]["qt.core.metatype"] = importInfo;
10113+
importInfo.condition = e2.condition;
10114+
importInfo.outsideFunction = true;
10115+
}
10116+
}
10117+
}
10118+
}
10119+
}
10120+
if ("QMetaTypeId" in data.semantic.rootScope.symbols)
10121+
foreach (e; data.semantic.rootScope.symbols["QMetaTypeId"].entries)
10122+
{
10123+
if (!(e.data.flags & DeclarationFlags.templateSpecialization))
10124+
continue;
10125+
findQtMetaTypeId(e.data.tree);
10126+
}
10127+
1004210128
foreach (filename, decls; data.declsByFile)
1004310129
{
1004410130
foreach (d; decls)

tests/single/test370.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
template <typename T>
3+
struct QMetaTypeId
4+
{
5+
};
6+
7+
#define Q_DECLARE_METATYPE(TYPE) Q_DECLARE_METATYPE_IMPL(TYPE)
8+
#define Q_DECLARE_METATYPE_IMPL(TYPE) \
9+
template <> \
10+
struct QMetaTypeId< TYPE > \
11+
{ \
12+
enum { Defined = 1 }; \
13+
static int qt_metatype_id() \
14+
{ \
15+
return 1; \
16+
} \
17+
};
18+
19+
20+
struct S
21+
{
22+
};
23+
Q_DECLARE_METATYPE(S);

tests/single/test370.d

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
module test370;
3+
4+
import config;
5+
import cppconvhelpers;
6+
import qt.core.metatype;
7+
8+
struct QMetaTypeId(T)
9+
{
10+
}
11+
12+
/+ #define Q_DECLARE_METATYPE(TYPE) Q_DECLARE_METATYPE_IMPL(TYPE)
13+
#define Q_DECLARE_METATYPE_IMPL(TYPE) \
14+
template <> \
15+
struct QMetaTypeId< TYPE > \
16+
{ \
17+
enum { Defined = 1 }; \
18+
static int qt_metatype_id() \
19+
{ \
20+
return 1; \
21+
} \
22+
}; +/
23+
24+
25+
@Q_DECLARE_METATYPE struct S
26+
{
27+
}
28+
/+ Q_DECLARE_METATYPE(S); +/
29+

0 commit comments

Comments
 (0)