-
Notifications
You must be signed in to change notification settings - Fork 4
/
LnAst.h
343 lines (300 loc) · 11.6 KB
/
LnAst.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#ifndef LUONAST_H
#define LUONAST_H
/*
** Copyright (C) 2024 Rochus Keller ([email protected])
**
** This file is part of the Luon language project.
**
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
*/
// adopted from Micron
#include <QByteArray>
#include <Luon/LnRowCol.h>
#include <QVariant>
namespace Ln
{
class Declaration;
struct BasicType
{
enum { SignedIntBitWidth = 52 };
enum Type {
Undefined,
NoType,
StrLit,
ByteArrayLit,
Nil,
BOOLEAN,
CHAR,
BYTE,
INTEGER, // max. representable signed int in LuaJIT, use MAX and MIN
REAL, // double
SET, // cut at uint32
STRING,
ANYREC,
Max
};
static QVariant getMax(quint8 form);
static QVariant getMin(quint8 form);
static const char* name[];
};
struct Builtin
{
enum Type {
// functions
ABS, CAP, BITAND, BITASR, BITNOT, BITOR, BITS, BITSHL, BITSHR,
BITXOR, CAST, CHR, CLIP, DEFAULT, FLOOR, FLT, GETENV, LEN, KEYS, MAX,
MIN, ODD, ORD, STRLEN,
// procedures
ASSERT, COPY, DEC, EXCL, HALT, INC,
INCL, NEW, PCALL, PRINT, PRINTLN, RAISE, SETENV,
// end
TRAP, TRAPIF, TOSTRING,
Max
// NOTE: CAST is only used to convert integers to enums
};
static const char* name[];
};
class Expression;
class Type
{
public:
enum Form { Record = BasicType::Max, Array, HashMap, Proc, ConstEnum, NameRef, Generic };
uint form : 8;
uint deferred : 1;
uint anonymous : 1;
uint validated : 1;
uint allocated : 1;
uint receiver : 1;
quint32 len; // array length
Type* base; // array/pointer base type, return type
QList<Declaration*> subs; // list of record fields or enum elements, or params for proc type
Declaration* decl; // if NameRef includes pos and name
Expression* expr; // array len, hashmap key
bool isNumber() const { return form == BasicType::INTEGER || form == BasicType::REAL || form == BasicType::BYTE; }
bool isReal() const { return form == BasicType::REAL; }
bool isInteger() const { return form == BasicType::INTEGER || form == BasicType::BYTE; }
bool isSet() const { return form == BasicType::SET; }
bool isBoolean() const { return form == BasicType::BOOLEAN; }
bool isSimple() const { return form >= BasicType::StrLit && form < BasicType::Max; }
bool isReference() const {return form >= Record && form <= Proc; }
bool isStructured() const { return form == Array || form == Record || form == HashMap; }
QPair<int,int> countAllocRecordMembers(bool recursive = false);
static bool isSubtype(Type* super, Type* sub);
bool isDerefCharArray() const;
bool isDerefByteArray() const;
Type* deref() const;
Declaration* find(const QByteArray& name, bool recursive = true) const;
QList<Declaration*> fieldList() const;
QList<Declaration*> methodList(bool recursive = false) const;
Type():form(0),len(0),base(0),decl(0),deferred(false),anonymous(false),
expr(0),validated(false),allocated(false),receiver(false){}
~Type();
};
class Statement;
class Declaration
{
public:
enum Kind { Invalid, Helper, Scope, Module, TypeDecl, Builtin, ConstDecl, Import, Field,
VarDecl, LocalDecl,
Procedure, ParamDecl,
Max };
enum { IdWidth = 16, NoSlot = (1 << IdWidth) - 1 };
Declaration* link; // member list or imported module decl
Declaration* outer; // the owning declaration to reconstruct the qualident
Declaration* super; // super class or overridden method
Statement* body; // procs
Type* type;
QByteArray name;
RowCol pos;
enum Visi { NA, Private, ReadOnly, ReadWrite };
uint visi : 2;
uint varParam : 1; // var param
enum Mode { Normal, Begin, Receiver, Inline, Invar, Extern, Meta, Enum };
uint mode : 3;
uint ownstype : 1;
uint inList : 1; // private
uint validated : 1;
uint hasErrors : 1;
uint hasSubs : 1; // class/method: has overrides; module: has clients
uint kind : 4;
uint id : IdWidth; // used for built-in code and local/param number
QVariant data; // value for Const and Enum, path for Import, name for Extern
Expression* expr; // const decl, enum, meta actuals
Declaration():next(0),link(0),type(0),body(0),id(NoSlot),kind(0),mode(0), visi(0),ownstype(false),expr(0),
outer(0),varParam(0),inList(0),validated(0), super(0), hasSubs(0), hasErrors(0){}
QList<Declaration*> getParams(bool skipReceiver = false) const;
int getIndexOf(Declaration*) const;
bool isLvalue() const { return kind == VarDecl || kind == LocalDecl || kind == ParamDecl; }
bool isPublic() const { return visi >= ReadOnly; }
Declaration* getNext() const { return next; }
Declaration* getLast() const;
Declaration* find(const QByteArray& name, bool recursive = true);
Declaration* getModule();
void appendMember(Declaration*);
RowCol getEndPos() const;
QByteArray scopedName(bool withModule = false, bool withPath = false) const;
QByteArray getModuleFullName(bool dots = false) const;
static void deleteAll(Declaration*);
private:
~Declaration();
Declaration* next; // list of all declarations in outer scope
friend class AstModel;
};
typedef QList<Declaration*> DeclList;
class Expression {
public:
enum Kind {
Invalid,
Plus, Minus, Not, // Unary
Eq, Neq, Lt, Leq, Gt, Geq, In, Is, // Relation
Add, Sub, Or, // AddOp
Mul, Fdiv, Div, Mod, And, // MulOp
DeclRef, // val is declaration
Select, // f.g, val is field declaration
Index, // a[i]
Cast,
Call,
Literal,
Constructor, Range, KeyValue,
NameRef, // temporary, will be resolved by validator to DeclRef and ConstVal
ConstVal,
Super, // ^ supercall
MAX
};
#ifdef _DEBUG
Kind kind;
#else
quint8 kind;
#endif
uint byVal : 1; // option for LocalVar, Param, ModuleVar, Select, Index
uint needsLval : 1;
uint visi : 2;
uint byName : 1; // true if lhs of KeyValue is not an index or key, but a name
RowCol pos;
Type* type;
QVariant val; // set elements and call args are ExpList embedded in val
Expression* lhs; // for unary and binary ops
Expression* rhs; // for binary ops
Expression* next; // for args, set elems, and caselabellist
bool isConst() const;
bool isLvalue() const; // true if result of expression is usually a ref to type; can be changed with byVal
void setByVal();
bool isCharLiteral();
qint64 getCaseValue(bool* ok = 0) const;
void appendRhs(Expression*);
static int getCount(const Expression* list);
static void append(Expression* list, Expression* elem);
static QList<Expression*> getList(Expression* exp);
static Expression* createFromToken(quint16,const RowCol&);
Expression(Kind k = Invalid, const RowCol& rc = RowCol()):
kind(k),type(0),lhs(0),rhs(0),next(0),needsLval(false),
pos(rc),byVal(false),visi(0),byName(0){}
~Expression();
};
typedef QList<Expression*> ExpList;
typedef QList<Expression*> MetaActualList;
struct Import {
QByteArrayList path; // full path incl. name
MetaActualList metaActuals; // ref to importDecl->expr, deleted there
Declaration* importer;
RowCol importedAt;
Declaration* resolved; // module
Import():resolved(0),importer(0){}
bool equals( const Import& other) const;
};
class Statement {
public:
enum Kind { Invalid,
Assig, Call, If, Elsif, Else, Case, TypeCase, CaseLabel,
Loop, While, Repeat, Exit, Return, ForAssig, ForToBy, End
};
quint8 kind;
RowCol pos;
Expression* lhs; // owns: proc, assig lhs
Expression* rhs; // owns: rhs, args, cond, case, label, return
Statement* body; // owns: then
Statement(Kind k = Invalid, const RowCol& pos = RowCol()):kind(k),pos(pos),lhs(0),rhs(0),
next(0),body(0),inList(false) {}
Statement* getLast() const;
Statement* getNext() const { return next; }
void append(Statement*s);
static void deleteAll(Statement*);
private:
~Statement();
Statement* next; // owns: list of statements
bool inList;
};
typedef QList<Declaration*> MetaParamList;
struct ModuleData {
QByteArrayList path;
QString source;
MetaParamList metaParams;
MetaActualList metaActuals;
QByteArray suffix;
QByteArray fullName; // path.join('/') + suffix as symbol
RowCol end;
};
typedef QPair<QByteArray,QByteArray> Qualident;
class Symbol
{
public:
enum Kind { Invalid, Module, Decl, DeclRef, Lval };
quint8 kind;
quint8 len;
RowCol pos;
Declaration* decl;
Symbol* next;
Symbol():kind(Invalid),len(0),decl(0),next(0){}
static void deleteAll(Symbol*);
};
typedef QList<Symbol*> SymList;
struct Xref {
Symbol* syms;
QHash<Declaration*,SymList> uses;
QHash<Declaration*,DeclList> subs;
Xref():syms(0){}
};
class AstModel
{
public:
AstModel();
~AstModel();
void openScope(Declaration* scope);
Declaration* closeScope(bool takeMembers = false);
Declaration* addDecl(const QByteArray&);
Declaration* addHelper();
Declaration* findDecl(const QByteArray&, bool recursive = true) const;
Declaration* findDecl(Declaration* import, const QByteArray&) const;
Declaration* getTopScope() const;
QByteArray getTempName();
Declaration* getTopModule() const;
Type* getType(quint8 basicType) const { return types[basicType]; }
static void cleanupGlobals();
static DeclList toList(Declaration* d);
protected:
Type* newType(int form, int size);
Type* addType(const QByteArray& name, int form, int size);
void addTypeAlias(const QByteArray& name, Type*);
void addBuiltin(const QByteArray& name, Builtin::Type);
private:
QList<Declaration*> scopes;
Declaration* helper;
quint32 helperId;
static Declaration* globalScope;
static Type* types[BasicType::Max];
};
}
Q_DECLARE_METATYPE(Ln::Import)
Q_DECLARE_METATYPE(Ln::Declaration*)
Q_DECLARE_METATYPE(Ln::Expression*)
Q_DECLARE_METATYPE(Ln::Symbol*)
Q_DECLARE_METATYPE(Ln::ModuleData)
#endif // MICAST_H