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#include <iterator>
25#include <algorithm>
26#include <set>
27#include <unordered_map>
28#include <clang/Basic/SourceLocation.h>
29#include "qbjs.h"
30#include "clangversionabstraction.h"
31
32class MocPPCallbacks;
33namespace clang {
34class CXXMethodDecl;
35class CXXRecordDecl;
36class CXXConstructorDecl;
37class NamespaceDecl;
38class EnumDecl;
39class Preprocessor;
40class Sema;
41class TypeDecl;
42class Type;
43class QualType;
44}
45
46struct NotifyDef {
47 std::string Str;
48 clang::SourceLocation Loc;
49 clang::CXXMethodDecl *MD = nullptr;
50 int notifyId = -1;
51};
52
53struct PrivateSlotDef {
54 std::string ReturnType;
55 std::string Name;
56 std::vector<std::string> Args;
57 int NumDefault = 0;
58 std::string InPrivateClass;
59};
60
61struct PropertyDef {
62 std::string name, type, member, read, write, reset, designable = "true", scriptable = "true", editable, stored = "true",
63 user = "false", inPrivateClass;
64 NotifyDef notify;
65
66 bool constant = false;
67 bool final = false;
68
69 bool isEnum = false;
70
71 int revision = 0;
72 bool PointerHack = false; // If the READ method returns a pointer to the type
73 bool PossiblyForwardDeclared = false; //if the type is only forward declared
74};
75
76struct PluginData {
77 std::string IID;
78 QBJS::Value MetaData;
79};
80
81struct BaseDef {
82 std::vector<std::tuple<clang::EnumDecl*, std::string, bool>> Enums;
83
84 void addEnum(clang::EnumDecl *E, std::string Alias, bool IsFlag) {
85 for (auto I : Enums)
86 if (std::get<1>(I) == Alias)
87 return;
88
89 Enums.emplace_back(E, std::move(Alias), IsFlag);
90 }
91
92 std::vector<clang::CXXRecordDecl *> Extra;
93 void addExtra(clang::CXXRecordDecl *E) {
94 if (!E)
95 return;
96 if (std::find(Extra.begin(), Extra.end(), E) != Extra.end())
97 return;
98 Extra.push_back(E);
99 }
100
101 std::vector<std::pair<std::string, std::string>> ClassInfo;
102};
103
104struct ClassDef : BaseDef {
105
106 clang::CXXRecordDecl *Record = nullptr;
107
108 // This list only includes the things registered with the keywords
109 std::vector<clang::CXXMethodDecl*> Signals;
110 std::vector<clang::CXXMethodDecl*> Slots;
111 std::vector<PrivateSlotDef> PrivateSlots;
112 std::vector<clang::CXXMethodDecl*> Methods;
113 std::vector<clang::CXXConstructorDecl*> Constructors;
114
115
116 std::vector<std::string> Interfaces;
117 PluginData Plugin;
118
119 std::vector<PropertyDef> Properties;
120
121 bool HasQObject = false;
122 bool HasQGadget = false;
123
124 int NotifyCount = 0;
125 int PrivateSlotCount = 0;
126 int RevisionPropertyCount = 0;
127 int RevisionMethodCount = 0;
128};
129
130struct NamespaceDef : BaseDef {
131 clang::NamespaceDecl *Namespace = nullptr;
132 bool hasQNamespace = false;
133};
134
135class MocNg {
136public:
137
138 typedef std::set<const clang::Type*> MetaTypeSet;
139 MetaTypeSet registered_meta_type;
140
141 typedef std::unordered_map<std::string, const clang::CXXRecordDecl*> InterfaceMap;
142 InterfaceMap interfaces;
143
144 ClassDef parseClass (clang::CXXRecordDecl* RD, clang::Sema& Sema);
145 NamespaceDef parseNamespace(clang::NamespaceDecl* ND, clang::Sema& Sema);
146
147 bool HasPlugin = false;
148
149 std::map<clang::SourceLocation, std::string> Tags;
150 std::string GetTag(clang::SourceLocation DeclLoc, const clang::SourceManager& SM);
151 bool ShouldRegisterMetaType(clang::QualType T);
152};
153