1/****************************************************************************
2 * Copyright (C) 2013-2016 Woboq GmbH
3 * Olivier Goffart <contact at woboq.com>
4 * https://woboq.com/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <string>
23#include <vector>
24
25namespace clang {
26class ASTContext;
27class CXXMethodDecl;
28class SourceManager;
29class QualType;
30}
31
32#include <clang/AST/PrettyPrinter.h>
33#include "mocng.h"
34
35struct ClassDef;
36
37
38
39// From qmetaobject_p.h
40enum PropertyFlags {
41 Invalid = 0x00000000,
42 Readable = 0x00000001,
43 Writable = 0x00000002,
44 Resettable = 0x00000004,
45 EnumOrFlag = 0x00000008,
46 StdCppSet = 0x00000100,
47// Override = 0x00000200,
48 Constant = 0x00000400,
49 Final = 0x00000800,
50 Designable = 0x00001000,
51 ResolveDesignable = 0x00002000,
52 Scriptable = 0x00004000,
53 ResolveScriptable = 0x00008000,
54 Stored = 0x00010000,
55 ResolveStored = 0x00020000,
56 Editable = 0x00040000,
57 ResolveEditable = 0x00080000,
58 User = 0x00100000,
59 ResolveUser = 0x00200000,
60 Notify = 0x00400000,
61 Revisioned = 0x00800000
62};
63enum MethodFlags {
64 AccessPrivate = 0x00,
65 AccessProtected = 0x01,
66 AccessPublic = 0x02,
67 AccessMask = 0x03, //mask
68 MethodMethod = 0x00,
69 MethodSignal = 0x04,
70 MethodSlot = 0x08,
71 MethodConstructor = 0x0c,
72 MethodTypeMask = 0x0c,
73 MethodCompatibility = 0x10,
74 MethodCloned = 0x20,
75 MethodScriptable = 0x40,
76 MethodRevisioned = 0x80
77};
78enum MetaObjectFlags {
79 DynamicMetaObject = 0x01,
80 RequiresVariantMetaObject = 0x02,
81 PropertyAccessInStaticMetaCall = 0x04
82};
83enum MetaDataFlags {
84 IsUnresolvedType = 0x80000000,
85 TypeNameIndexMask = 0x7FFFFFFF
86};
87
88
89enum { OutputRevision = 7,
90 MetaObjectPrivateFieldCount = 14, // = sizeof(QMetaObjectPrivate) / sizeof(int)
91 mocOutputRevision = 67,
92 QT_VERSION = 0x050100
93};
94
95#define MOCNG_VERSION_STR "alpha 1"
96
97class Generator {
98 const BaseDef *Def;
99 const ClassDef *CDef;
100 llvm::raw_ostream& OS;
101 llvm::raw_ostream& OS_TemplateHeader;
102
103 std::vector<std::string> Strings;
104
105 std::string QualName;
106 std::string BaseName;
107 std::string TemplatePrefix; // what is in front of the template declaration ("template<typename t>")
108 bool BaseHasStaticMetaObject = false;
109 bool HasTemplateHeader;
110 int MethodCount;
111
112 clang::ASTContext &Ctx;
113 clang::PrintingPolicy PrintPolicy;
114
115 MocNg *Moc;
116
117public:
118 explicit Generator(const ClassDef *CDef, llvm::raw_ostream& OS, clang::ASTContext & Ctx, MocNg *Moc,
119 llvm::raw_ostream *OS_TemplateHeader = nullptr);
120 // For namespaces
121 explicit Generator(const NamespaceDef *NDef, llvm::raw_ostream& OS, clang::ASTContext & Ctx, MocNg *Moc);
122
123 bool IsQtNamespace = false;
124
125 // plugin metadata from -M command line argument (to be put in the JSON)
126 std::vector<std::pair<llvm::StringRef, llvm::StringRef>> MetaData;
127
128 void GenerateCode();
129private:
130
131 int StrIdx(llvm::StringRef);
132 template <typename T>
133 void GenerateFunctions(const std::vector<T> &V, const char *TypeName, MethodFlags Type, int &ParamIndex);
134 template <typename T>
135 void GenerateFunctionParameters(const std::vector<T*> &V, const char *TypeName);
136
137 void GenerateProperties();
138 void GenerateMetaCall();
139 void GenerateStaticMetaCall();
140 void GenerateSignal(const clang::CXXMethodDecl *MD, int Idx);
141
142 void GenerateTypeInfo(clang::QualType Type);
143 void GenerateEnums(int EnumIndex);
144 void GeneratePluginMetaData(bool Debug);
145
146 // Called when emiting the code to generate the invokation of a method.
147 // Return true if the code was already emitted (include the break;)
148 // defined in workaroundtest.cpp
149 static bool WorkaroundTests(llvm::StringRef ClassName, const clang::CXXMethodDecl* MD, llvm::raw_ostream &OS);
150};
151