Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Ast/include/Luau/Ast.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/Location.h"
5
6
#include <iterator>
7
#include <optional>
8
#include <functional>
9
#include <string>
10
11
#include <string.h>
12
#include <stdint.h>
13
14
namespace Luau
15
{
16
17
struct AstName
18
{
19
const char* value;
20
21
AstName()
22
: value(nullptr)
23
{
24
}
25
26
explicit AstName(const char* value)
27
: value(value)
28
{
29
}
30
31
bool operator==(const AstName& rhs) const
32
{
33
return value == rhs.value;
34
}
35
36
bool operator!=(const AstName& rhs) const
37
{
38
return value != rhs.value;
39
}
40
41
bool operator==(const char* rhs) const
42
{
43
return value && strcmp(value, rhs) == 0;
44
}
45
46
bool operator!=(const char* rhs) const
47
{
48
return !value || strcmp(value, rhs) != 0;
49
}
50
51
bool operator<(const AstName& rhs) const
52
{
53
return (value && rhs.value) ? strcmp(value, rhs.value) < 0 : value < rhs.value;
54
}
55
};
56
57
class AstType;
58
class AstVisitor;
59
class AstStat;
60
class AstStatBlock;
61
class AstExpr;
62
class AstTypePack;
63
class AstAttr;
64
class AstExprTable;
65
66
struct AstLocal
67
{
68
AstName name;
69
Location location;
70
AstLocal* shadow;
71
size_t functionDepth;
72
size_t loopDepth;
73
bool isConst;
74
75
AstType* annotation;
76
77
AstLocal(
78
const AstName& name,
79
const Location& location,
80
AstLocal* shadow,
81
size_t functionDepth,
82
size_t loopDepth,
83
AstType* annotation,
84
bool isConst = false
85
)
86
: name(name)
87
, location(location)
88
, shadow(shadow)
89
, functionDepth(functionDepth)
90
, loopDepth(loopDepth)
91
, isConst(isConst)
92
, annotation(annotation)
93
{
94
}
95
};
96
97
template<typename T>
98
struct AstArray
99
{
100
T* data = nullptr;
101
size_t size = 0;
102
103
const T* begin() const
104
{
105
return data;
106
}
107
108
const T* end() const
109
{
110
return data + size;
111
}
112
113
std::reverse_iterator<const T*> rbegin() const
114
{
115
return std::make_reverse_iterator(end());
116
}
117
118
std::reverse_iterator<const T*> rend() const
119
{
120
return std::make_reverse_iterator(begin());
121
}
122
};
123
124
struct AstTypeList
125
{
126
AstArray<AstType*> types;
127
// Null indicates no tail, not an untyped tail.
128
AstTypePack* tailType = nullptr;
129
};
130
131
// Don't have Luau::Variant available, it's a bit of an overhead, but a plain struct is nice to use
132
struct AstTypeOrPack
133
{
134
AstType* type = nullptr;
135
AstTypePack* typePack = nullptr;
136
};
137
138
using AstArgumentName = std::pair<AstName, Location>; // TODO: remove and replace when we get a common struct for this pair instead of AstName
139
140
extern int gAstRttiIndex;
141
142
template<typename T>
143
struct AstRtti
144
{
145
static const int value;
146
};
147
148
template<typename T>
149
const int AstRtti<T>::value = ++gAstRttiIndex;
150
151
#define LUAU_RTTI(Class) \
152
static int ClassIndex() \
153
{ \
154
return AstRtti<Class>::value; \
155
}
156
157
class AstNode
158
{
159
public:
160
explicit AstNode(int classIndex, const Location& location)
161
: classIndex(classIndex)
162
, location(location)
163
{
164
}
165
166
virtual void visit(AstVisitor* visitor) = 0;
167
168
virtual AstExpr* asExpr()
169
{
170
return nullptr;
171
}
172
virtual AstStat* asStat()
173
{
174
return nullptr;
175
}
176
virtual AstType* asType()
177
{
178
return nullptr;
179
}
180
virtual AstAttr* asAttr()
181
{
182
return nullptr;
183
}
184
185
template<typename T>
186
bool is() const
187
{
188
return classIndex == T::ClassIndex();
189
}
190
template<typename T>
191
T* as()
192
{
193
return classIndex == T::ClassIndex() ? static_cast<T*>(this) : nullptr;
194
}
195
template<typename T>
196
const T* as() const
197
{
198
return classIndex == T::ClassIndex() ? static_cast<const T*>(this) : nullptr;
199
}
200
201
const int classIndex;
202
Location location;
203
};
204
205
class AstAttr : public AstNode
206
{
207
public:
208
LUAU_RTTI(AstAttr)
209
210
enum Type
211
{
212
Checked,
213
Native,
214
Deprecated,
215
DebugNoinline,
216
Unknown
217
};
218
219
struct DeprecatedInfo
220
{
221
bool deprecated = false;
222
std::optional<std::string> use;
223
std::optional<std::string> reason;
224
};
225
226
AstAttr(const Location& location, Type type, AstArray<AstExpr*> args);
227
AstAttr(const Location& location, Type type, AstArray<AstExpr*> args, AstName name);
228
229
AstAttr* asAttr() override
230
{
231
return this;
232
}
233
234
void visit(AstVisitor* visitor) override;
235
236
DeprecatedInfo deprecatedInfo() const;
237
238
Type type;
239
AstArray<AstExpr*> args;
240
AstName name;
241
};
242
243
class AstExpr : public AstNode
244
{
245
public:
246
explicit AstExpr(int classIndex, const Location& location)
247
: AstNode(classIndex, location)
248
{
249
}
250
251
AstExpr* asExpr() override
252
{
253
return this;
254
}
255
};
256
257
class AstStat : public AstNode
258
{
259
public:
260
explicit AstStat(int classIndex, const Location& location)
261
: AstNode(classIndex, location)
262
, hasSemicolon(false)
263
{
264
}
265
266
AstStat* asStat() override
267
{
268
return this;
269
}
270
271
bool hasSemicolon;
272
};
273
274
class AstGenericType : public AstNode
275
{
276
public:
277
LUAU_RTTI(AstGenericType)
278
279
explicit AstGenericType(const Location& location, AstName name, AstType* defaultValue = nullptr);
280
281
void visit(AstVisitor* visitor) override;
282
283
AstName name;
284
AstType* defaultValue = nullptr;
285
};
286
287
class AstGenericTypePack : public AstNode
288
{
289
public:
290
LUAU_RTTI(AstGenericTypePack)
291
292
explicit AstGenericTypePack(const Location& location, AstName name, AstTypePack* defaultValue = nullptr);
293
294
void visit(AstVisitor* visitor) override;
295
296
AstName name;
297
AstTypePack* defaultValue = nullptr;
298
};
299
300
class AstExprGroup : public AstExpr
301
{
302
public:
303
LUAU_RTTI(AstExprGroup)
304
305
explicit AstExprGroup(const Location& location, AstExpr* expr);
306
307
void visit(AstVisitor* visitor) override;
308
309
AstExpr* expr;
310
};
311
312
class AstExprConstantNil : public AstExpr
313
{
314
public:
315
LUAU_RTTI(AstExprConstantNil)
316
317
explicit AstExprConstantNil(const Location& location);
318
319
void visit(AstVisitor* visitor) override;
320
};
321
322
class AstExprConstantBool : public AstExpr
323
{
324
public:
325
LUAU_RTTI(AstExprConstantBool)
326
327
AstExprConstantBool(const Location& location, bool value);
328
329
void visit(AstVisitor* visitor) override;
330
331
bool value;
332
};
333
334
enum class ConstantNumberParseResult
335
{
336
Ok,
337
Imprecise,
338
Malformed,
339
BinOverflow,
340
HexOverflow,
341
IntOverflow,
342
};
343
344
class AstExprConstantNumber : public AstExpr
345
{
346
public:
347
LUAU_RTTI(AstExprConstantNumber)
348
349
AstExprConstantNumber(const Location& location, double value, ConstantNumberParseResult parseResult = ConstantNumberParseResult::Ok);
350
351
void visit(AstVisitor* visitor) override;
352
353
double value;
354
ConstantNumberParseResult parseResult;
355
};
356
357
class AstExprConstantInteger : public AstExpr
358
{
359
public:
360
LUAU_RTTI(AstExprConstantInteger)
361
362
AstExprConstantInteger(const Location& location, int64_t value, ConstantNumberParseResult parseResult = ConstantNumberParseResult::Ok);
363
364
void visit(AstVisitor* visitor) override;
365
366
int64_t value;
367
ConstantNumberParseResult parseResult;
368
};
369
class AstExprConstantString : public AstExpr
370
{
371
public:
372
LUAU_RTTI(AstExprConstantString)
373
374
enum QuoteStyle
375
{
376
// A string created using double quotes or an interpolated string,
377
// as in:
378
//
379
// "foo", `My name is {protagonist}! / And I'm {antagonist}!`
380
//
381
QuotedSimple,
382
// A string created using single quotes, as in:
383
//
384
// 'bar'
385
//
386
QuotedSingle,
387
// A string created using `[[ ... ]]` as in:
388
//
389
// [[ Gee, this sure is a long string.
390
// it even has a new line in it! ]]
391
//
392
QuotedRaw,
393
// A "string" in the context of a table literal, as in:
394
//
395
// { foo = 42 } -- `foo` here is a "constant string"
396
//
397
Unquoted,
398
};
399
400
AstExprConstantString(const Location& location, const AstArray<char>& value, QuoteStyle quoteStyle);
401
402
void visit(AstVisitor* visitor) override;
403
bool isQuoted() const;
404
405
AstArray<char> value;
406
QuoteStyle quoteStyle;
407
};
408
409
class AstExprLocal : public AstExpr
410
{
411
public:
412
LUAU_RTTI(AstExprLocal)
413
414
AstExprLocal(const Location& location, AstLocal* local, bool upvalue);
415
416
void visit(AstVisitor* visitor) override;
417
418
AstLocal* local;
419
bool upvalue;
420
};
421
422
class AstExprGlobal : public AstExpr
423
{
424
public:
425
LUAU_RTTI(AstExprGlobal)
426
427
AstExprGlobal(const Location& location, const AstName& name);
428
429
void visit(AstVisitor* visitor) override;
430
431
AstName name;
432
};
433
434
class AstExprVarargs : public AstExpr
435
{
436
public:
437
LUAU_RTTI(AstExprVarargs)
438
439
AstExprVarargs(const Location& location);
440
441
void visit(AstVisitor* visitor) override;
442
};
443
444
class AstExprCall : public AstExpr
445
{
446
public:
447
LUAU_RTTI(AstExprCall)
448
449
AstExprCall(
450
const Location& location,
451
AstExpr* func,
452
const AstArray<AstExpr*>& args,
453
bool self,
454
const AstArray<AstTypeOrPack>& explicitTypes,
455
const Location& argLocation
456
);
457
458
void visit(AstVisitor* visitor) override;
459
460
AstExpr* func;
461
// These will only be filled in specifically `t:f<<A, B>>()`.
462
// In `f<<A, B>>()`, this is parsed as `f<<A, B>>` as an expression,
463
// which is then called.
464
AstArray<AstTypeOrPack> typeArguments;
465
AstArray<AstExpr*> args;
466
bool self;
467
Location argLocation;
468
};
469
470
class AstExprIndexName : public AstExpr
471
{
472
public:
473
LUAU_RTTI(AstExprIndexName)
474
475
AstExprIndexName(
476
const Location& location,
477
AstExpr* expr,
478
const AstName& index,
479
const Location& indexLocation,
480
const Position& opPosition,
481
char op
482
);
483
484
void visit(AstVisitor* visitor) override;
485
486
AstExpr* expr;
487
AstName index;
488
Location indexLocation;
489
Position opPosition;
490
char op = '.';
491
};
492
493
class AstExprIndexExpr : public AstExpr
494
{
495
public:
496
LUAU_RTTI(AstExprIndexExpr)
497
498
AstExprIndexExpr(const Location& location, AstExpr* expr, AstExpr* index);
499
500
void visit(AstVisitor* visitor) override;
501
502
AstExpr* expr;
503
AstExpr* index;
504
};
505
506
class AstExprFunction : public AstExpr
507
{
508
public:
509
LUAU_RTTI(AstExprFunction)
510
511
AstExprFunction(
512
const Location& location,
513
const AstArray<AstAttr*>& attributes,
514
const AstArray<AstGenericType*>& generics,
515
const AstArray<AstGenericTypePack*>& genericPacks,
516
AstLocal* self,
517
const AstArray<AstLocal*>& args,
518
bool vararg,
519
const Location& varargLocation,
520
AstStatBlock* body,
521
size_t functionDepth,
522
const AstName& debugname,
523
AstTypePack* returnAnnotation,
524
AstTypePack* varargAnnotation = nullptr,
525
const std::optional<Location>& argLocation = std::nullopt
526
);
527
528
void visit(AstVisitor* visitor) override;
529
530
bool hasNativeAttribute() const;
531
bool hasAttribute(AstAttr::Type attributeType) const;
532
AstAttr* getAttribute(AstAttr::Type attributeType) const;
533
534
AstArray<AstAttr*> attributes;
535
AstArray<AstGenericType*> generics;
536
AstArray<AstGenericTypePack*> genericPacks;
537
AstLocal* self;
538
AstArray<AstLocal*> args;
539
AstTypePack* returnAnnotation = nullptr;
540
bool vararg = false;
541
Location varargLocation;
542
AstTypePack* varargAnnotation;
543
544
AstStatBlock* body;
545
546
size_t functionDepth;
547
548
AstName debugname;
549
550
std::optional<Location> argLocation;
551
};
552
553
class AstExprTable : public AstExpr
554
{
555
public:
556
LUAU_RTTI(AstExprTable)
557
558
struct Item
559
{
560
enum Kind
561
{
562
List, // foo, in which case key is a nullptr
563
Record, // foo=bar, in which case key is a AstExprConstantString
564
General, // [foo]=bar
565
};
566
567
Kind kind;
568
569
AstExpr* key; // can be nullptr!
570
AstExpr* value;
571
};
572
573
AstExprTable(const Location& location, const AstArray<Item>& items);
574
575
void visit(AstVisitor* visitor) override;
576
577
std::optional<AstExpr*> getRecord(const char* key) const;
578
579
AstArray<Item> items;
580
};
581
582
class AstExprUnary : public AstExpr
583
{
584
public:
585
LUAU_RTTI(AstExprUnary)
586
587
enum Op
588
{
589
Not,
590
Minus,
591
Len
592
};
593
594
AstExprUnary(const Location& location, Op op, AstExpr* expr);
595
596
void visit(AstVisitor* visitor) override;
597
598
Op op;
599
AstExpr* expr;
600
};
601
602
std::string toString(AstExprUnary::Op op);
603
604
class AstExprBinary : public AstExpr
605
{
606
public:
607
LUAU_RTTI(AstExprBinary)
608
609
enum Op
610
{
611
Add,
612
Sub,
613
Mul,
614
Div,
615
FloorDiv,
616
Mod,
617
Pow,
618
Concat,
619
CompareNe,
620
CompareEq,
621
CompareLt,
622
CompareLe,
623
CompareGt,
624
CompareGe,
625
And,
626
Or,
627
628
Op__Count
629
};
630
631
AstExprBinary(const Location& location, Op op, AstExpr* left, AstExpr* right);
632
633
void visit(AstVisitor* visitor) override;
634
635
Op op;
636
AstExpr* left;
637
AstExpr* right;
638
};
639
640
std::string toString(AstExprBinary::Op op);
641
642
class AstExprTypeAssertion : public AstExpr
643
{
644
public:
645
LUAU_RTTI(AstExprTypeAssertion)
646
647
AstExprTypeAssertion(const Location& location, AstExpr* expr, AstType* annotation);
648
649
void visit(AstVisitor* visitor) override;
650
651
AstExpr* expr;
652
AstType* annotation;
653
};
654
655
class AstExprIfElse : public AstExpr
656
{
657
public:
658
LUAU_RTTI(AstExprIfElse)
659
660
AstExprIfElse(const Location& location, AstExpr* condition, bool hasThen, AstExpr* trueExpr, bool hasElse, AstExpr* falseExpr);
661
662
void visit(AstVisitor* visitor) override;
663
664
AstExpr* condition;
665
bool hasThen;
666
AstExpr* trueExpr;
667
bool hasElse;
668
AstExpr* falseExpr;
669
};
670
671
class AstExprInterpString : public AstExpr
672
{
673
public:
674
LUAU_RTTI(AstExprInterpString)
675
676
AstExprInterpString(const Location& location, const AstArray<AstArray<char>>& strings, const AstArray<AstExpr*>& expressions);
677
678
void visit(AstVisitor* visitor) override;
679
680
/// An interpolated string such as `foo{bar}baz` is represented as
681
/// an array of strings for "foo" and "bar", and an array of expressions for "baz".
682
/// `strings` will always have one more element than `expressions`.
683
AstArray<AstArray<char>> strings;
684
AstArray<AstExpr*> expressions;
685
};
686
687
// f<<T>>
688
class AstExprInstantiate : public AstExpr
689
{
690
public:
691
LUAU_RTTI(AstExprInstantiate)
692
693
AstExprInstantiate(const Location& location, AstExpr* expr, AstArray<AstTypeOrPack> typePack);
694
695
void visit(AstVisitor* visitor) override;
696
697
AstExpr* expr;
698
AstArray<AstTypeOrPack> typeArguments;
699
};
700
701
class AstStatBlock : public AstStat
702
{
703
public:
704
LUAU_RTTI(AstStatBlock)
705
706
AstStatBlock(const Location& location, const AstArray<AstStat*>& body, bool hasEnd = true);
707
708
void visit(AstVisitor* visitor) override;
709
710
AstArray<AstStat*> body;
711
712
/* Indicates whether or not this block has been terminated in a
713
* syntactically valid way.
714
*
715
* This is usually but not always done with the 'end' keyword. AstStatIf
716
* and AstStatRepeat are the two main exceptions to this.
717
*
718
* The 'then' clause of an if statement can properly be closed by the
719
* keywords 'else' or 'elseif'. A 'repeat' loop's body is closed with the
720
* 'until' keyword.
721
*/
722
bool hasEnd = false;
723
};
724
725
class AstStatIf : public AstStat
726
{
727
public:
728
LUAU_RTTI(AstStatIf)
729
730
AstStatIf(
731
const Location& location,
732
AstExpr* condition,
733
AstStatBlock* thenbody,
734
AstStat* elsebody,
735
const std::optional<Location>& thenLocation,
736
const std::optional<Location>& elseLocation
737
);
738
739
void visit(AstVisitor* visitor) override;
740
741
AstExpr* condition;
742
AstStatBlock* thenbody;
743
AstStat* elsebody;
744
745
std::optional<Location> thenLocation;
746
747
// Active for 'elseif' as well
748
std::optional<Location> elseLocation;
749
};
750
751
class AstStatWhile : public AstStat
752
{
753
public:
754
LUAU_RTTI(AstStatWhile)
755
756
AstStatWhile(const Location& location, AstExpr* condition, AstStatBlock* body, bool hasDo, const Location& doLocation);
757
758
void visit(AstVisitor* visitor) override;
759
760
AstExpr* condition;
761
AstStatBlock* body;
762
763
bool hasDo = false;
764
Location doLocation;
765
};
766
767
class AstStatRepeat : public AstStat
768
{
769
public:
770
LUAU_RTTI(AstStatRepeat)
771
772
AstStatRepeat(const Location& location, AstExpr* condition, AstStatBlock* body, bool DEPRECATED_hasUntil);
773
774
void visit(AstVisitor* visitor) override;
775
776
AstExpr* condition;
777
AstStatBlock* body;
778
779
bool DEPRECATED_hasUntil = false;
780
};
781
782
class AstStatBreak : public AstStat
783
{
784
public:
785
LUAU_RTTI(AstStatBreak)
786
787
AstStatBreak(const Location& location);
788
789
void visit(AstVisitor* visitor) override;
790
};
791
792
class AstStatContinue : public AstStat
793
{
794
public:
795
LUAU_RTTI(AstStatContinue)
796
797
AstStatContinue(const Location& location);
798
799
void visit(AstVisitor* visitor) override;
800
};
801
802
class AstStatReturn : public AstStat
803
{
804
public:
805
LUAU_RTTI(AstStatReturn)
806
807
AstStatReturn(const Location& location, const AstArray<AstExpr*>& list);
808
809
void visit(AstVisitor* visitor) override;
810
811
AstArray<AstExpr*> list;
812
};
813
814
class AstStatExpr : public AstStat
815
{
816
public:
817
LUAU_RTTI(AstStatExpr)
818
819
AstStatExpr(const Location& location, AstExpr* expr);
820
821
void visit(AstVisitor* visitor) override;
822
823
AstExpr* expr;
824
};
825
826
class AstStatLocal : public AstStat
827
{
828
public:
829
LUAU_RTTI(AstStatLocal)
830
831
AstStatLocal(
832
const Location& location,
833
const AstArray<AstLocal*>& vars,
834
const AstArray<AstExpr*>& values,
835
const std::optional<Location>& equalsSignLocation
836
);
837
838
void visit(AstVisitor* visitor) override;
839
840
AstArray<AstLocal*> vars;
841
AstArray<AstExpr*> values;
842
843
std::optional<Location> equalsSignLocation;
844
};
845
846
class AstStatFor : public AstStat
847
{
848
public:
849
LUAU_RTTI(AstStatFor)
850
851
AstStatFor(
852
const Location& location,
853
AstLocal* var,
854
AstExpr* from,
855
AstExpr* to,
856
AstExpr* step,
857
AstStatBlock* body,
858
bool hasDo,
859
const Location& doLocation
860
);
861
862
void visit(AstVisitor* visitor) override;
863
864
AstLocal* var;
865
AstExpr* from;
866
AstExpr* to;
867
AstExpr* step;
868
AstStatBlock* body;
869
870
bool hasDo = false;
871
Location doLocation;
872
};
873
874
class AstStatForIn : public AstStat
875
{
876
public:
877
LUAU_RTTI(AstStatForIn)
878
879
AstStatForIn(
880
const Location& location,
881
const AstArray<AstLocal*>& vars,
882
const AstArray<AstExpr*>& values,
883
AstStatBlock* body,
884
bool hasIn,
885
const Location& inLocation,
886
bool hasDo,
887
const Location& doLocation
888
);
889
890
void visit(AstVisitor* visitor) override;
891
892
AstArray<AstLocal*> vars;
893
AstArray<AstExpr*> values;
894
AstStatBlock* body;
895
896
bool hasIn = false;
897
Location inLocation;
898
899
bool hasDo = false;
900
Location doLocation;
901
};
902
903
class AstStatAssign : public AstStat
904
{
905
public:
906
LUAU_RTTI(AstStatAssign)
907
908
AstStatAssign(const Location& location, const AstArray<AstExpr*>& vars, const AstArray<AstExpr*>& values);
909
910
void visit(AstVisitor* visitor) override;
911
912
AstArray<AstExpr*> vars;
913
AstArray<AstExpr*> values;
914
};
915
916
class AstStatCompoundAssign : public AstStat
917
{
918
public:
919
LUAU_RTTI(AstStatCompoundAssign)
920
921
AstStatCompoundAssign(const Location& location, AstExprBinary::Op op, AstExpr* var, AstExpr* value);
922
923
void visit(AstVisitor* visitor) override;
924
925
AstExprBinary::Op op;
926
AstExpr* var;
927
AstExpr* value;
928
};
929
930
class AstStatFunction : public AstStat
931
{
932
public:
933
LUAU_RTTI(AstStatFunction)
934
935
AstStatFunction(const Location& location, AstExpr* name, AstExprFunction* func);
936
937
void visit(AstVisitor* visitor) override;
938
939
AstExpr* name;
940
AstExprFunction* func;
941
};
942
943
class AstStatLocalFunction : public AstStat
944
{
945
public:
946
LUAU_RTTI(AstStatLocalFunction)
947
948
AstStatLocalFunction(const Location& location, AstLocal* name, AstExprFunction* func);
949
950
void visit(AstVisitor* visitor) override;
951
952
AstLocal* name;
953
AstExprFunction* func;
954
};
955
956
class AstStatTypeAlias : public AstStat
957
{
958
public:
959
LUAU_RTTI(AstStatTypeAlias)
960
961
AstStatTypeAlias(
962
const Location& location,
963
const AstName& name,
964
const Location& nameLocation,
965
const AstArray<AstGenericType*>& generics,
966
const AstArray<AstGenericTypePack*>& genericPacks,
967
AstType* type,
968
bool exported
969
);
970
971
void visit(AstVisitor* visitor) override;
972
973
AstName name;
974
Location nameLocation;
975
AstArray<AstGenericType*> generics;
976
AstArray<AstGenericTypePack*> genericPacks;
977
AstType* type;
978
bool exported;
979
};
980
981
class AstStatTypeFunction : public AstStat
982
{
983
public:
984
LUAU_RTTI(AstStatTypeFunction);
985
986
AstStatTypeFunction(
987
const Location& location,
988
const AstName& name,
989
const Location& nameLocation,
990
AstExprFunction* body,
991
bool exported,
992
bool hasErrors
993
);
994
995
void visit(AstVisitor* visitor) override;
996
997
AstName name;
998
Location nameLocation;
999
AstExprFunction* body = nullptr;
1000
bool exported = false;
1001
bool hasErrors = false;
1002
};
1003
1004
class AstStatDeclareGlobal : public AstStat
1005
{
1006
public:
1007
LUAU_RTTI(AstStatDeclareGlobal)
1008
1009
AstStatDeclareGlobal(const Location& location, const AstName& name, const Location& nameLocation, AstType* type);
1010
1011
void visit(AstVisitor* visitor) override;
1012
1013
AstName name;
1014
Location nameLocation;
1015
AstType* type;
1016
};
1017
1018
class AstStatDeclareFunction : public AstStat
1019
{
1020
public:
1021
LUAU_RTTI(AstStatDeclareFunction)
1022
1023
AstStatDeclareFunction(
1024
const Location& location,
1025
const AstName& name,
1026
const Location& nameLocation,
1027
const AstArray<AstGenericType*>& generics,
1028
const AstArray<AstGenericTypePack*>& genericPacks,
1029
const AstTypeList& params,
1030
const AstArray<AstArgumentName>& paramNames,
1031
bool vararg,
1032
const Location& varargLocation,
1033
AstTypePack* retTypes
1034
);
1035
1036
AstStatDeclareFunction(
1037
const Location& location,
1038
const AstArray<AstAttr*>& attributes,
1039
const AstName& name,
1040
const Location& nameLocation,
1041
const AstArray<AstGenericType*>& generics,
1042
const AstArray<AstGenericTypePack*>& genericPacks,
1043
const AstTypeList& params,
1044
const AstArray<AstArgumentName>& paramNames,
1045
bool vararg,
1046
const Location& varargLocation,
1047
AstTypePack* retTypes
1048
);
1049
1050
void visit(AstVisitor* visitor) override;
1051
1052
bool isCheckedFunction() const;
1053
bool hasAttribute(AstAttr::Type attributeType) const;
1054
AstAttr* getAttribute(AstAttr::Type attributeType) const;
1055
1056
AstArray<AstAttr*> attributes;
1057
AstName name;
1058
Location nameLocation;
1059
AstArray<AstGenericType*> generics;
1060
AstArray<AstGenericTypePack*> genericPacks;
1061
AstTypeList params;
1062
AstArray<AstArgumentName> paramNames;
1063
bool vararg = false;
1064
Location varargLocation;
1065
AstTypePack* retTypes;
1066
};
1067
1068
enum class AstTableAccess
1069
{
1070
Read = 0b01,
1071
Write = 0b10,
1072
ReadWrite = 0b11,
1073
};
1074
1075
struct AstDeclaredExternTypeProperty
1076
{
1077
AstName name;
1078
Location nameLocation;
1079
AstType* ty = nullptr;
1080
bool isMethod = false;
1081
Location location;
1082
AstTableAccess access = AstTableAccess::ReadWrite;
1083
};
1084
1085
struct AstTableIndexer
1086
{
1087
AstType* indexType;
1088
AstType* resultType;
1089
Location location;
1090
1091
AstTableAccess access = AstTableAccess::ReadWrite;
1092
std::optional<Location> accessLocation;
1093
};
1094
1095
class AstStatDeclareExternType : public AstStat
1096
{
1097
public:
1098
LUAU_RTTI(AstStatDeclareExternType)
1099
1100
AstStatDeclareExternType(
1101
const Location& location,
1102
const AstName& name,
1103
std::optional<AstName> superName,
1104
const AstArray<AstDeclaredExternTypeProperty>& props,
1105
AstTableIndexer* indexer = nullptr
1106
);
1107
1108
void visit(AstVisitor* visitor) override;
1109
1110
AstName name;
1111
std::optional<AstName> superName;
1112
1113
AstArray<AstDeclaredExternTypeProperty> props;
1114
AstTableIndexer* indexer;
1115
};
1116
1117
class AstType : public AstNode
1118
{
1119
public:
1120
AstType(int classIndex, const Location& location)
1121
: AstNode(classIndex, location)
1122
{
1123
}
1124
1125
AstType* asType() override
1126
{
1127
return this;
1128
}
1129
};
1130
1131
class AstTypeReference : public AstType
1132
{
1133
public:
1134
LUAU_RTTI(AstTypeReference)
1135
1136
AstTypeReference(
1137
const Location& location,
1138
std::optional<AstName> prefix,
1139
AstName name,
1140
std::optional<Location> prefixLocation,
1141
const Location& nameLocation,
1142
bool hasParameterList = false,
1143
const AstArray<AstTypeOrPack>& parameters = {}
1144
);
1145
1146
void visit(AstVisitor* visitor) override;
1147
1148
bool hasParameterList;
1149
std::optional<AstName> prefix;
1150
std::optional<Location> prefixLocation;
1151
AstName name;
1152
Location nameLocation;
1153
AstArray<AstTypeOrPack> parameters;
1154
};
1155
1156
struct AstTableProp
1157
{
1158
AstName name;
1159
Location location;
1160
AstType* type;
1161
AstTableAccess access = AstTableAccess::ReadWrite;
1162
std::optional<Location> accessLocation;
1163
};
1164
1165
class AstTypeTable : public AstType
1166
{
1167
public:
1168
LUAU_RTTI(AstTypeTable)
1169
1170
AstTypeTable(const Location& location, const AstArray<AstTableProp>& props, AstTableIndexer* indexer = nullptr);
1171
1172
void visit(AstVisitor* visitor) override;
1173
1174
AstArray<AstTableProp> props;
1175
AstTableIndexer* indexer;
1176
};
1177
1178
class AstTypeFunction : public AstType
1179
{
1180
public:
1181
LUAU_RTTI(AstTypeFunction)
1182
1183
AstTypeFunction(
1184
const Location& location,
1185
const AstArray<AstGenericType*>& generics,
1186
const AstArray<AstGenericTypePack*>& genericPacks,
1187
const AstTypeList& argTypes,
1188
const AstArray<std::optional<AstArgumentName>>& argNames,
1189
AstTypePack* returnTypes
1190
);
1191
1192
AstTypeFunction(
1193
const Location& location,
1194
const AstArray<AstAttr*>& attributes,
1195
const AstArray<AstGenericType*>& generics,
1196
const AstArray<AstGenericTypePack*>& genericPacks,
1197
const AstTypeList& argTypes,
1198
const AstArray<std::optional<AstArgumentName>>& argNames,
1199
AstTypePack* returnTypes
1200
);
1201
1202
void visit(AstVisitor* visitor) override;
1203
1204
bool isCheckedFunction() const;
1205
bool hasAttribute(AstAttr::Type attributeType) const;
1206
AstAttr* getAttribute(AstAttr::Type attributeType) const;
1207
1208
AstArray<AstAttr*> attributes;
1209
AstArray<AstGenericType*> generics;
1210
AstArray<AstGenericTypePack*> genericPacks;
1211
AstTypeList argTypes;
1212
AstArray<std::optional<AstArgumentName>> argNames;
1213
AstTypePack* returnTypes;
1214
};
1215
1216
class AstTypeTypeof : public AstType
1217
{
1218
public:
1219
LUAU_RTTI(AstTypeTypeof)
1220
1221
AstTypeTypeof(const Location& location, AstExpr* expr);
1222
1223
void visit(AstVisitor* visitor) override;
1224
1225
AstExpr* expr;
1226
};
1227
1228
class AstTypeOptional : public AstType
1229
{
1230
public:
1231
LUAU_RTTI(AstTypeOptional)
1232
1233
AstTypeOptional(const Location& location);
1234
1235
void visit(AstVisitor* visitor) override;
1236
};
1237
1238
class AstTypeUnion : public AstType
1239
{
1240
public:
1241
LUAU_RTTI(AstTypeUnion)
1242
1243
AstTypeUnion(const Location& location, const AstArray<AstType*>& types);
1244
1245
void visit(AstVisitor* visitor) override;
1246
1247
AstArray<AstType*> types;
1248
};
1249
1250
class AstTypeIntersection : public AstType
1251
{
1252
public:
1253
LUAU_RTTI(AstTypeIntersection)
1254
1255
AstTypeIntersection(const Location& location, const AstArray<AstType*>& types);
1256
1257
void visit(AstVisitor* visitor) override;
1258
1259
AstArray<AstType*> types;
1260
};
1261
1262
class AstExprError : public AstExpr
1263
{
1264
public:
1265
LUAU_RTTI(AstExprError)
1266
1267
AstExprError(const Location& location, const AstArray<AstExpr*>& expressions, unsigned messageIndex);
1268
1269
void visit(AstVisitor* visitor) override;
1270
1271
AstArray<AstExpr*> expressions;
1272
unsigned messageIndex;
1273
};
1274
1275
class AstStatError : public AstStat
1276
{
1277
public:
1278
LUAU_RTTI(AstStatError)
1279
1280
AstStatError(const Location& location, const AstArray<AstExpr*>& expressions, const AstArray<AstStat*>& statements, unsigned messageIndex);
1281
1282
void visit(AstVisitor* visitor) override;
1283
1284
AstArray<AstExpr*> expressions;
1285
AstArray<AstStat*> statements;
1286
unsigned messageIndex;
1287
};
1288
1289
class AstTypeError : public AstType
1290
{
1291
public:
1292
LUAU_RTTI(AstTypeError)
1293
1294
AstTypeError(const Location& location, const AstArray<AstType*>& types, bool isMissing, unsigned messageIndex);
1295
1296
void visit(AstVisitor* visitor) override;
1297
1298
AstArray<AstType*> types;
1299
bool isMissing;
1300
unsigned messageIndex;
1301
};
1302
1303
class AstTypeSingletonBool : public AstType
1304
{
1305
public:
1306
LUAU_RTTI(AstTypeSingletonBool)
1307
1308
AstTypeSingletonBool(const Location& location, bool value);
1309
1310
void visit(AstVisitor* visitor) override;
1311
1312
bool value;
1313
};
1314
1315
class AstTypeSingletonString : public AstType
1316
{
1317
public:
1318
LUAU_RTTI(AstTypeSingletonString)
1319
1320
AstTypeSingletonString(const Location& location, const AstArray<char>& value);
1321
1322
void visit(AstVisitor* visitor) override;
1323
1324
const AstArray<char> value;
1325
};
1326
1327
class AstTypeGroup : public AstType
1328
{
1329
public:
1330
LUAU_RTTI(AstTypeGroup)
1331
1332
explicit AstTypeGroup(const Location& location, AstType* type);
1333
1334
void visit(AstVisitor* visitor) override;
1335
1336
AstType* type;
1337
};
1338
1339
class AstTypePack : public AstNode
1340
{
1341
public:
1342
AstTypePack(int classIndex, const Location& location)
1343
: AstNode(classIndex, location)
1344
{
1345
}
1346
};
1347
1348
class AstTypePackExplicit : public AstTypePack
1349
{
1350
public:
1351
LUAU_RTTI(AstTypePackExplicit)
1352
1353
AstTypePackExplicit(const Location& location, AstTypeList typeList);
1354
1355
void visit(AstVisitor* visitor) override;
1356
1357
AstTypeList typeList;
1358
};
1359
1360
class AstTypePackVariadic : public AstTypePack
1361
{
1362
public:
1363
LUAU_RTTI(AstTypePackVariadic)
1364
1365
AstTypePackVariadic(const Location& location, AstType* variadicType);
1366
1367
void visit(AstVisitor* visitor) override;
1368
1369
AstType* variadicType;
1370
};
1371
1372
class AstTypePackGeneric : public AstTypePack
1373
{
1374
public:
1375
LUAU_RTTI(AstTypePackGeneric)
1376
1377
AstTypePackGeneric(const Location& location, AstName name);
1378
1379
void visit(AstVisitor* visitor) override;
1380
1381
AstName genericName;
1382
};
1383
1384
class AstVisitor
1385
{
1386
public:
1387
virtual ~AstVisitor() {}
1388
1389
virtual bool visit(class AstNode*)
1390
{
1391
return true;
1392
}
1393
1394
virtual bool visit(class AstAttr* node)
1395
{
1396
return visit(static_cast<AstNode*>(node));
1397
}
1398
1399
virtual bool visit(class AstGenericType* node)
1400
{
1401
return visit(static_cast<AstNode*>(node));
1402
}
1403
1404
virtual bool visit(class AstGenericTypePack* node)
1405
{
1406
return visit(static_cast<AstNode*>(node));
1407
}
1408
1409
virtual bool visit(class AstExpr* node)
1410
{
1411
return visit(static_cast<AstNode*>(node));
1412
}
1413
1414
virtual bool visit(class AstExprGroup* node)
1415
{
1416
return visit(static_cast<AstExpr*>(node));
1417
}
1418
virtual bool visit(class AstExprConstantNil* node)
1419
{
1420
return visit(static_cast<AstExpr*>(node));
1421
}
1422
virtual bool visit(class AstExprConstantBool* node)
1423
{
1424
return visit(static_cast<AstExpr*>(node));
1425
}
1426
virtual bool visit(class AstExprConstantNumber* node)
1427
{
1428
return visit(static_cast<AstExpr*>(node));
1429
}
1430
virtual bool visit(class AstExprConstantInteger* node)
1431
{
1432
return visit(static_cast<AstExpr*>(node));
1433
}
1434
virtual bool visit(class AstExprConstantString* node)
1435
{
1436
return visit(static_cast<AstExpr*>(node));
1437
}
1438
virtual bool visit(class AstExprLocal* node)
1439
{
1440
return visit(static_cast<AstExpr*>(node));
1441
}
1442
virtual bool visit(class AstExprGlobal* node)
1443
{
1444
return visit(static_cast<AstExpr*>(node));
1445
}
1446
virtual bool visit(class AstExprVarargs* node)
1447
{
1448
return visit(static_cast<AstExpr*>(node));
1449
}
1450
virtual bool visit(class AstExprCall* node)
1451
{
1452
return visit(static_cast<AstExpr*>(node));
1453
}
1454
virtual bool visit(class AstExprIndexName* node)
1455
{
1456
return visit(static_cast<AstExpr*>(node));
1457
}
1458
virtual bool visit(class AstExprIndexExpr* node)
1459
{
1460
return visit(static_cast<AstExpr*>(node));
1461
}
1462
virtual bool visit(class AstExprFunction* node)
1463
{
1464
return visit(static_cast<AstExpr*>(node));
1465
}
1466
virtual bool visit(class AstExprTable* node)
1467
{
1468
return visit(static_cast<AstExpr*>(node));
1469
}
1470
virtual bool visit(class AstExprUnary* node)
1471
{
1472
return visit(static_cast<AstExpr*>(node));
1473
}
1474
virtual bool visit(class AstExprBinary* node)
1475
{
1476
return visit(static_cast<AstExpr*>(node));
1477
}
1478
virtual bool visit(class AstExprTypeAssertion* node)
1479
{
1480
return visit(static_cast<AstExpr*>(node));
1481
}
1482
virtual bool visit(class AstExprIfElse* node)
1483
{
1484
return visit(static_cast<AstExpr*>(node));
1485
}
1486
virtual bool visit(class AstExprInterpString* node)
1487
{
1488
return visit(static_cast<AstExpr*>(node));
1489
}
1490
virtual bool visit(class AstExprInstantiate* node)
1491
{
1492
return visit(static_cast<AstExpr*>(node));
1493
}
1494
virtual bool visit(class AstExprError* node)
1495
{
1496
return visit(static_cast<AstExpr*>(node));
1497
}
1498
1499
virtual bool visit(class AstStat* node)
1500
{
1501
return visit(static_cast<AstNode*>(node));
1502
}
1503
1504
virtual bool visit(class AstStatBlock* node)
1505
{
1506
return visit(static_cast<AstStat*>(node));
1507
}
1508
virtual bool visit(class AstStatIf* node)
1509
{
1510
return visit(static_cast<AstStat*>(node));
1511
}
1512
virtual bool visit(class AstStatWhile* node)
1513
{
1514
return visit(static_cast<AstStat*>(node));
1515
}
1516
virtual bool visit(class AstStatRepeat* node)
1517
{
1518
return visit(static_cast<AstStat*>(node));
1519
}
1520
virtual bool visit(class AstStatBreak* node)
1521
{
1522
return visit(static_cast<AstStat*>(node));
1523
}
1524
virtual bool visit(class AstStatContinue* node)
1525
{
1526
return visit(static_cast<AstStat*>(node));
1527
}
1528
virtual bool visit(class AstStatReturn* node)
1529
{
1530
return visit(static_cast<AstStat*>(node));
1531
}
1532
virtual bool visit(class AstStatExpr* node)
1533
{
1534
return visit(static_cast<AstStat*>(node));
1535
}
1536
virtual bool visit(class AstStatLocal* node)
1537
{
1538
return visit(static_cast<AstStat*>(node));
1539
}
1540
virtual bool visit(class AstStatFor* node)
1541
{
1542
return visit(static_cast<AstStat*>(node));
1543
}
1544
virtual bool visit(class AstStatForIn* node)
1545
{
1546
return visit(static_cast<AstStat*>(node));
1547
}
1548
virtual bool visit(class AstStatAssign* node)
1549
{
1550
return visit(static_cast<AstStat*>(node));
1551
}
1552
virtual bool visit(class AstStatCompoundAssign* node)
1553
{
1554
return visit(static_cast<AstStat*>(node));
1555
}
1556
virtual bool visit(class AstStatFunction* node)
1557
{
1558
return visit(static_cast<AstStat*>(node));
1559
}
1560
virtual bool visit(class AstStatLocalFunction* node)
1561
{
1562
return visit(static_cast<AstStat*>(node));
1563
}
1564
virtual bool visit(class AstStatTypeAlias* node)
1565
{
1566
return visit(static_cast<AstStat*>(node));
1567
}
1568
virtual bool visit(class AstStatTypeFunction* node)
1569
{
1570
return visit(static_cast<AstStat*>(node));
1571
}
1572
virtual bool visit(class AstStatDeclareFunction* node)
1573
{
1574
return visit(static_cast<AstStat*>(node));
1575
}
1576
virtual bool visit(class AstStatDeclareGlobal* node)
1577
{
1578
return visit(static_cast<AstStat*>(node));
1579
}
1580
virtual bool visit(class AstStatDeclareExternType* node)
1581
{
1582
return visit(static_cast<AstStat*>(node));
1583
}
1584
virtual bool visit(class AstStatError* node)
1585
{
1586
return visit(static_cast<AstStat*>(node));
1587
}
1588
1589
// By default visiting type annotations is disabled; override this in your visitor if you need to!
1590
virtual bool visit(class AstType* node)
1591
{
1592
return false;
1593
}
1594
1595
virtual bool visit(class AstTypeReference* node)
1596
{
1597
return visit(static_cast<AstType*>(node));
1598
}
1599
virtual bool visit(class AstTypeTable* node)
1600
{
1601
return visit(static_cast<AstType*>(node));
1602
}
1603
virtual bool visit(class AstTypeFunction* node)
1604
{
1605
return visit(static_cast<AstType*>(node));
1606
}
1607
virtual bool visit(class AstTypeTypeof* node)
1608
{
1609
return visit(static_cast<AstType*>(node));
1610
}
1611
virtual bool visit(class AstTypeOptional* node)
1612
{
1613
return visit(static_cast<AstType*>(node));
1614
}
1615
virtual bool visit(class AstTypeUnion* node)
1616
{
1617
return visit(static_cast<AstType*>(node));
1618
}
1619
virtual bool visit(class AstTypeIntersection* node)
1620
{
1621
return visit(static_cast<AstType*>(node));
1622
}
1623
virtual bool visit(class AstTypeSingletonBool* node)
1624
{
1625
return visit(static_cast<AstType*>(node));
1626
}
1627
virtual bool visit(class AstTypeSingletonString* node)
1628
{
1629
return visit(static_cast<AstType*>(node));
1630
}
1631
virtual bool visit(class AstTypeGroup* node)
1632
{
1633
return visit(static_cast<AstType*>(node));
1634
}
1635
virtual bool visit(class AstTypeError* node)
1636
{
1637
return visit(static_cast<AstType*>(node));
1638
}
1639
1640
virtual bool visit(class AstTypePack* node)
1641
{
1642
return false;
1643
}
1644
virtual bool visit(class AstTypePackExplicit* node)
1645
{
1646
return visit(static_cast<AstTypePack*>(node));
1647
}
1648
virtual bool visit(class AstTypePackVariadic* node)
1649
{
1650
return visit(static_cast<AstTypePack*>(node));
1651
}
1652
virtual bool visit(class AstTypePackGeneric* node)
1653
{
1654
return visit(static_cast<AstTypePack*>(node));
1655
}
1656
};
1657
1658
bool isLValue(const AstExpr*);
1659
bool isConstantLiteral(const AstExpr*);
1660
bool isLiteralTable(const AstExpr*);
1661
AstName getIdentifier(AstExpr*);
1662
Location getLocation(const AstTypeList& typeList);
1663
1664
template<typename T> // AstNode, AstExpr, AstLocal, etc
1665
Location getLocation(AstArray<T*> array)
1666
{
1667
if (0 == array.size)
1668
return {};
1669
1670
return Location{array.data[0]->location.begin, array.data[array.size - 1]->location.end};
1671
}
1672
1673
#undef LUAU_RTTI
1674
1675
} // namespace Luau
1676
1677
namespace std
1678
{
1679
1680
template<>
1681
struct hash<Luau::AstName>
1682
{
1683
size_t operator()(const Luau::AstName& value) const
1684
{
1685
// note: since operator== uses pointer identity, hashing function uses it as well
1686
// the hasher is the same as DenseHashPointer (DenseHash.h)
1687
return (uintptr_t(value.value) >> 4) ^ (uintptr_t(value.value) >> 9);
1688
}
1689
};
1690
1691
} // namespace std
1692
1693