This repository was archived by the owner on Sep 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctypeparser.h
More file actions
243 lines (188 loc) · 7.06 KB
/
ctypeparser.h
File metadata and controls
243 lines (188 loc) · 7.06 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*==============================================================================
** Copyright (C) 2025-2028 WingSummer
**
** This program is free software: you can redistribute it and/or modify it under
** the terms of the GNU Affero General Public License as published by the Free
** Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
** FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
** details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <https://www.gnu.org/licenses/>.
** =============================================================================
*/
#ifndef CTYPE_PARSER_H
#define CTYPE_PARSER_H
#include "define.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QHash>
#include <QList>
#include <QObject>
#include <QPair>
#include <QRegularExpression>
#include <QStringList>
#include <QTextStream>
class CStructVisitorParser;
enum class PointerMode { X86, X64 };
enum class LongMode { LLP64, LP64 };
class CTypeParser {
Q_GADGET
Q_DECLARE_TR_FUNCTIONS(CTypeParser)
public:
// Result codes for manager operations
enum class StructResult {
Ok,
NameConflict,
DuplicateDefinition,
IncompleteReference,
OutofMemory,
CountOfLimit,
};
enum class CType {
Unknown,
ConstVar,
Enum,
Struct,
Union,
TypeDef,
BasicType
};
Q_ENUM(CType);
using INT_TYPE = std::variant<qint64, quint64>;
public:
explicit CTypeParser(const std::function<void(const MsgInfo &)> &msgcb);
virtual ~CTypeParser();
public:
bool parse(const QString &file);
public:
quint64 generateAnomyID();
// Forward declarations
StructResult addForwardStruct(const QString &name);
StructResult addForwardUnion(const QString &name);
StructResult addForwardEnum(const QString &name);
// Definitions (only register names)
StructResult defineStruct(const QString &name,
const QVector<VariableDeclaration> &members,
int alignment);
StructResult defineUnion(const QString &name,
const QVector<VariableDeclaration> &members,
int alignment);
StructResult
defineStructOrUnion(bool isStruct, const QString &name,
const QVector<VariableDeclaration> &members,
int alignment);
StructResult defineEnum(const QString &name,
const QHash<QString, INT_TYPE> &values);
StructResult defineTypedef(const QString &alias, const QString &origin,
bool isPointer);
StructResult defineConstVar(const QString &name, const INT_TYPE &var);
public:
qsizetype padAlignment() const;
///
/// \brief set the struct alignment
/// \param newKAlignment alignment value (only for 1, 2, 4, 8)
/// \warning it takes no effect for structs/union that
/// already have been added
///
void setPadAlignment(qsizetype newKAlignment);
PointerMode pointerMode() const;
///
/// \brief set the pointer size mode
/// \param newPmode
/// \warning it takes no effect for structs/union that
/// already have been added
///
void setPointerMode(PointerMode newPmode);
LongMode longMode() const;
///
/// \brief set the specification rule for long type
/// \param newLmode
/// \warning it takes no effect for structs/union that
/// already have been added
///
void setLongmode(LongMode newLmode);
public:
bool containsType(const QString &name) const;
bool isBasicType(const QString &name) const;
bool isUnsignedBasicType(const QString &name) const;
bool containsEnum(const QString &name) const;
bool containsStruct(const QString &name) const;
bool containsUnion(const QString &name) const;
bool containsTypeDef(const QString &name) const;
bool containsConstVar(const QString &name) const;
bool isCompletedType(const QString &name) const;
INT_TYPE constVarValue(const QString &name) const;
bool isCompletedStruct(const QString &name) const;
bool isCompletedUnion(const QString &name) const;
QStringList getMissingDependencise(const QString &name) const;
public:
QMetaType::Type metaType(const QString &name) const;
CType type(const QString &name) const;
std::optional<quint64> getTypeSize(const QString &data_type) const;
public:
void dumpAllTypeDefines(QTextStream &output) const;
void clear();
private:
QStringList
getMissingDependencise(const QVector<VariableDeclaration> &members);
std::optional<quint64> padStruct(QVector<VariableDeclaration> &members,
int alignment);
quint64 calcUnionSize(const QVector<VariableDeclaration> &members,
int alignment) const;
Q_REQUIRED_RESULT bool
storeStructUnionDef(const bool is_struct, const QString &type_name,
QVector<VariableDeclaration> &members,
qsizetype alignment);
void restoreIncompleteType(const QString &name);
private:
enum class IncompleteType { Struct, Union, Typedef, Enum };
private:
/// read in basic data such as keywords/qualifiers, and basic data type
/// sizes
void initialize();
void findHeaderFiles(const QString &path);
QString getFile(QString &filename) const;
/// class members
private:
qsizetype kAlignment_ = 1; ///< alignment
private:
CStructVisitorParser *_visitor = nullptr;
PointerMode _pmode;
LongMode _lmode;
/// Size of C data types and also user-defined struct/union types
/// @note All enum types have fixed size, so they're not stored
QHash<QString, QPair<QMetaType::Type, quint64>> type_maps_;
/// names awaiting definition
QHash<QString, IncompleteType> referencedIncomplete_;
QHash<QString, QStringList> incompleteDeps_;
/// basic types
QStringList base_types_;
/// Parsing result - extracted type definitions
/// for below 3 maps:
/// key - type name
/// value - type members
/// struct definitions
QHash<QString, QList<VariableDeclaration>> struct_defs_;
/// union definitions
QHash<QString, QList<VariableDeclaration>> union_defs_;
QHash<QString, int> struct_union_alignments_;
/// typedef definitions
/// <typedefName, <originalName, isPointer>>
QHash<QString, QPair<QString, bool>> type_defs_;
/// enum definitions
QHash<QString, QHash<QString, INT_TYPE>> enum_defs_;
/// constants and macros that have integer values
/// key - constant/macro name
/// value - an integer (all types of number are cast to long type for
/// convenience)
QHash<QString, INT_TYPE> const_defs_;
std::function<void(const MsgInfo &)> _msgcb;
private:
quint64 _anomyIndex = 0;
};
#endif // _TYPE_PARSER_H_