Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Ast/src/Ast.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/Ast.h"
3
4
#include "Luau/Common.h"
5
6
7
namespace Luau
8
{
9
10
static AstAttr* findAttributeInArray(const AstArray<AstAttr*> attributes, AstAttr::Type attributeType)
11
{
12
for (const auto attribute : attributes)
13
{
14
if (attribute->type == attributeType)
15
return attribute;
16
}
17
18
return nullptr;
19
}
20
21
static bool hasAttributeInArray(const AstArray<AstAttr*> attributes, AstAttr::Type attributeType)
22
{
23
return findAttributeInArray(attributes, attributeType) != nullptr;
24
}
25
26
static void visitTypeList(AstVisitor* visitor, const AstTypeList& list)
27
{
28
for (AstType* ty : list.types)
29
ty->visit(visitor);
30
31
if (list.tailType)
32
list.tailType->visit(visitor);
33
}
34
35
static void visitTypeOrPackArray(AstVisitor* visitor, const AstArray<AstTypeOrPack>& arrayOfTypeOrPack)
36
{
37
for (const AstTypeOrPack& param : arrayOfTypeOrPack)
38
{
39
if (param.type)
40
param.type->visit(visitor);
41
else
42
param.typePack->visit(visitor);
43
}
44
}
45
46
AstAttr::AstAttr(const Location& location, Type type, AstArray<AstExpr*> args)
47
: AstNode(ClassIndex(), location)
48
, type(type)
49
, args(args)
50
{
51
}
52
53
AstAttr::AstAttr(const Location& location, Type type, AstArray<AstExpr*> args, AstName name)
54
: AstNode(ClassIndex(), location)
55
, type(type)
56
, args(args)
57
, name(name)
58
{
59
}
60
61
void AstAttr::visit(AstVisitor* visitor)
62
{
63
visitor->visit(this);
64
}
65
66
AstAttr::DeprecatedInfo AstAttr::deprecatedInfo() const
67
{
68
AstAttr::DeprecatedInfo info;
69
info.deprecated = type == AstAttr::Type::Deprecated;
70
71
if (info.deprecated && args.size > 0 && args.data[0]->is<AstExprTable>())
72
{
73
AstExprTable* table = args.data[0]->as<AstExprTable>();
74
if (auto useValue = table->getRecord("use"))
75
{
76
if ((*useValue)->is<AstExprConstantString>())
77
{
78
AstArray<char> use = (*useValue)->as<AstExprConstantString>()->value;
79
info.use = {{use.data, use.size}};
80
}
81
}
82
if (auto reasonValue = table->getRecord("reason"))
83
{
84
if ((*reasonValue)->is<AstExprConstantString>())
85
{
86
AstArray<char> reason = (*reasonValue)->as<AstExprConstantString>()->value;
87
info.reason = {{reason.data, reason.size}};
88
}
89
}
90
}
91
92
return info;
93
}
94
95
int gAstRttiIndex = 0;
96
97
AstGenericType::AstGenericType(const Location& location, AstName name, AstType* defaultValue)
98
: AstNode(ClassIndex(), location)
99
, name(name)
100
, defaultValue(defaultValue)
101
{
102
}
103
104
void AstGenericType::visit(AstVisitor* visitor)
105
{
106
if (visitor->visit(this))
107
{
108
if (defaultValue)
109
defaultValue->visit(visitor);
110
}
111
}
112
113
AstGenericTypePack::AstGenericTypePack(const Location& location, AstName name, AstTypePack* defaultValue)
114
: AstNode(ClassIndex(), location)
115
, name(name)
116
, defaultValue(defaultValue)
117
{
118
}
119
120
void AstGenericTypePack::visit(AstVisitor* visitor)
121
{
122
if (visitor->visit(this))
123
{
124
if (defaultValue)
125
defaultValue->visit(visitor);
126
}
127
}
128
129
AstExprGroup::AstExprGroup(const Location& location, AstExpr* expr)
130
: AstExpr(ClassIndex(), location)
131
, expr(expr)
132
{
133
}
134
135
void AstExprGroup::visit(AstVisitor* visitor)
136
{
137
if (visitor->visit(this))
138
expr->visit(visitor);
139
}
140
141
AstExprConstantNil::AstExprConstantNil(const Location& location)
142
: AstExpr(ClassIndex(), location)
143
{
144
}
145
146
void AstExprConstantNil::visit(AstVisitor* visitor)
147
{
148
visitor->visit(this);
149
}
150
151
AstExprConstantBool::AstExprConstantBool(const Location& location, bool value)
152
: AstExpr(ClassIndex(), location)
153
, value(value)
154
{
155
}
156
157
void AstExprConstantBool::visit(AstVisitor* visitor)
158
{
159
visitor->visit(this);
160
}
161
162
AstExprConstantNumber::AstExprConstantNumber(const Location& location, double value, ConstantNumberParseResult parseResult)
163
: AstExpr(ClassIndex(), location)
164
, value(value)
165
, parseResult(parseResult)
166
{
167
}
168
169
void AstExprConstantNumber::visit(AstVisitor* visitor)
170
{
171
visitor->visit(this);
172
}
173
174
AstExprConstantInteger::AstExprConstantInteger(const Location& location, int64_t value, ConstantNumberParseResult parseResult)
175
: AstExpr(ClassIndex(), location)
176
, value(value)
177
, parseResult(parseResult)
178
{
179
}
180
181
void AstExprConstantInteger::visit(AstVisitor* visitor)
182
{
183
visitor->visit(this);
184
}
185
186
AstExprConstantString::AstExprConstantString(const Location& location, const AstArray<char>& value, QuoteStyle quoteStyle)
187
: AstExpr(ClassIndex(), location)
188
, value(value)
189
, quoteStyle(quoteStyle)
190
{
191
}
192
193
void AstExprConstantString::visit(AstVisitor* visitor)
194
{
195
visitor->visit(this);
196
}
197
198
bool AstExprConstantString::isQuoted() const
199
{
200
return quoteStyle == QuoteStyle::QuotedSimple || quoteStyle == QuoteStyle::QuotedRaw;
201
}
202
203
AstExprLocal::AstExprLocal(const Location& location, AstLocal* local, bool upvalue)
204
: AstExpr(ClassIndex(), location)
205
, local(local)
206
, upvalue(upvalue)
207
{
208
}
209
210
void AstExprLocal::visit(AstVisitor* visitor)
211
{
212
visitor->visit(this);
213
}
214
215
AstExprGlobal::AstExprGlobal(const Location& location, const AstName& name)
216
: AstExpr(ClassIndex(), location)
217
, name(name)
218
{
219
}
220
221
void AstExprGlobal::visit(AstVisitor* visitor)
222
{
223
visitor->visit(this);
224
}
225
226
AstExprVarargs::AstExprVarargs(const Location& location)
227
: AstExpr(ClassIndex(), location)
228
{
229
}
230
231
void AstExprVarargs::visit(AstVisitor* visitor)
232
{
233
visitor->visit(this);
234
}
235
236
AstExprCall::AstExprCall(
237
const Location& location,
238
AstExpr* func,
239
const AstArray<AstExpr*>& args,
240
bool self,
241
const AstArray<AstTypeOrPack>& explicitTypes,
242
const Location& argLocation
243
)
244
: AstExpr(ClassIndex(), location)
245
, func(func)
246
, typeArguments(explicitTypes)
247
, args(args)
248
, self(self)
249
, argLocation(argLocation)
250
{
251
}
252
253
void AstExprCall::visit(AstVisitor* visitor)
254
{
255
if (visitor->visit(this))
256
{
257
func->visit(visitor);
258
259
for (AstExpr* arg : args)
260
arg->visit(visitor);
261
}
262
}
263
264
AstExprIndexName::AstExprIndexName(
265
const Location& location,
266
AstExpr* expr,
267
const AstName& index,
268
const Location& indexLocation,
269
const Position& opPosition,
270
char op
271
)
272
: AstExpr(ClassIndex(), location)
273
, expr(expr)
274
, index(index)
275
, indexLocation(indexLocation)
276
, opPosition(opPosition)
277
, op(op)
278
{
279
}
280
281
void AstExprIndexName::visit(AstVisitor* visitor)
282
{
283
if (visitor->visit(this))
284
expr->visit(visitor);
285
}
286
287
AstExprIndexExpr::AstExprIndexExpr(const Location& location, AstExpr* expr, AstExpr* index)
288
: AstExpr(ClassIndex(), location)
289
, expr(expr)
290
, index(index)
291
{
292
}
293
294
void AstExprIndexExpr::visit(AstVisitor* visitor)
295
{
296
if (visitor->visit(this))
297
{
298
expr->visit(visitor);
299
index->visit(visitor);
300
}
301
}
302
303
AstExprFunction::AstExprFunction(
304
const Location& location,
305
const AstArray<AstAttr*>& attributes,
306
const AstArray<AstGenericType*>& generics,
307
const AstArray<AstGenericTypePack*>& genericPacks,
308
AstLocal* self,
309
const AstArray<AstLocal*>& args,
310
bool vararg,
311
const Location& varargLocation,
312
AstStatBlock* body,
313
size_t functionDepth,
314
const AstName& debugname,
315
AstTypePack* returnAnnotation,
316
AstTypePack* varargAnnotation,
317
const std::optional<Location>& argLocation
318
)
319
: AstExpr(ClassIndex(), location)
320
, attributes(attributes)
321
, generics(generics)
322
, genericPacks(genericPacks)
323
, self(self)
324
, args(args)
325
, returnAnnotation(returnAnnotation)
326
, vararg(vararg)
327
, varargLocation(varargLocation)
328
, varargAnnotation(varargAnnotation)
329
, body(body)
330
, functionDepth(functionDepth)
331
, debugname(debugname)
332
, argLocation(argLocation)
333
{
334
}
335
336
void AstExprFunction::visit(AstVisitor* visitor)
337
{
338
if (visitor->visit(this))
339
{
340
for (AstLocal* arg : args)
341
{
342
if (arg->annotation)
343
arg->annotation->visit(visitor);
344
}
345
346
if (varargAnnotation)
347
varargAnnotation->visit(visitor);
348
349
if (returnAnnotation)
350
returnAnnotation->visit(visitor);
351
352
body->visit(visitor);
353
}
354
}
355
356
bool AstExprFunction::hasNativeAttribute() const
357
{
358
for (const auto attribute : attributes)
359
{
360
if (attribute->type == AstAttr::Type::Native)
361
return true;
362
}
363
return false;
364
}
365
366
bool AstExprFunction::hasAttribute(const AstAttr::Type attributeType) const
367
{
368
return hasAttributeInArray(attributes, attributeType);
369
}
370
371
AstAttr* AstExprFunction::getAttribute(const AstAttr::Type attributeType) const
372
{
373
return findAttributeInArray(attributes, attributeType);
374
}
375
376
AstExprTable::AstExprTable(const Location& location, const AstArray<Item>& items)
377
: AstExpr(ClassIndex(), location)
378
, items(items)
379
{
380
}
381
382
void AstExprTable::visit(AstVisitor* visitor)
383
{
384
if (visitor->visit(this))
385
{
386
for (const Item& item : items)
387
{
388
if (item.key)
389
item.key->visit(visitor);
390
391
item.value->visit(visitor);
392
}
393
}
394
}
395
396
std::optional<AstExpr*> AstExprTable::getRecord(const char* key) const
397
{
398
for (const AstExprTable::Item& item : items)
399
{
400
if (item.kind == AstExprTable::Item::Kind::Record)
401
{
402
if (strcmp(item.key->as<AstExprConstantString>()->value.data, key) == 0)
403
return item.value;
404
}
405
}
406
return {};
407
}
408
409
AstExprUnary::AstExprUnary(const Location& location, Op op, AstExpr* expr)
410
: AstExpr(ClassIndex(), location)
411
, op(op)
412
, expr(expr)
413
{
414
}
415
416
void AstExprUnary::visit(AstVisitor* visitor)
417
{
418
if (visitor->visit(this))
419
expr->visit(visitor);
420
}
421
422
std::string toString(AstExprUnary::Op op)
423
{
424
switch (op)
425
{
426
case AstExprUnary::Minus:
427
return "-";
428
case AstExprUnary::Not:
429
return "not";
430
case AstExprUnary::Len:
431
return "#";
432
default:
433
LUAU_ASSERT(false);
434
return ""; // MSVC requires this even though the switch/case is exhaustive
435
}
436
}
437
438
AstExprBinary::AstExprBinary(const Location& location, Op op, AstExpr* left, AstExpr* right)
439
: AstExpr(ClassIndex(), location)
440
, op(op)
441
, left(left)
442
, right(right)
443
{
444
}
445
446
void AstExprBinary::visit(AstVisitor* visitor)
447
{
448
if (visitor->visit(this))
449
{
450
left->visit(visitor);
451
right->visit(visitor);
452
}
453
}
454
455
std::string toString(AstExprBinary::Op op)
456
{
457
switch (op)
458
{
459
case AstExprBinary::Add:
460
return "+";
461
case AstExprBinary::Sub:
462
return "-";
463
case AstExprBinary::Mul:
464
return "*";
465
case AstExprBinary::Div:
466
return "/";
467
case AstExprBinary::FloorDiv:
468
return "//";
469
case AstExprBinary::Mod:
470
return "%";
471
case AstExprBinary::Pow:
472
return "^";
473
case AstExprBinary::Concat:
474
return "..";
475
case AstExprBinary::CompareNe:
476
return "~=";
477
case AstExprBinary::CompareEq:
478
return "==";
479
case AstExprBinary::CompareLt:
480
return "<";
481
case AstExprBinary::CompareLe:
482
return "<=";
483
case AstExprBinary::CompareGt:
484
return ">";
485
case AstExprBinary::CompareGe:
486
return ">=";
487
case AstExprBinary::And:
488
return "and";
489
case AstExprBinary::Or:
490
return "or";
491
default:
492
LUAU_ASSERT(false);
493
return ""; // MSVC requires this even though the switch/case is exhaustive
494
}
495
}
496
497
AstExprTypeAssertion::AstExprTypeAssertion(const Location& location, AstExpr* expr, AstType* annotation)
498
: AstExpr(ClassIndex(), location)
499
, expr(expr)
500
, annotation(annotation)
501
{
502
}
503
504
void AstExprTypeAssertion::visit(AstVisitor* visitor)
505
{
506
if (visitor->visit(this))
507
{
508
expr->visit(visitor);
509
annotation->visit(visitor);
510
}
511
}
512
513
AstExprIfElse::AstExprIfElse(const Location& location, AstExpr* condition, bool hasThen, AstExpr* trueExpr, bool hasElse, AstExpr* falseExpr)
514
: AstExpr(ClassIndex(), location)
515
, condition(condition)
516
, hasThen(hasThen)
517
, trueExpr(trueExpr)
518
, hasElse(hasElse)
519
, falseExpr(falseExpr)
520
{
521
}
522
523
void AstExprIfElse::visit(AstVisitor* visitor)
524
{
525
if (visitor->visit(this))
526
{
527
condition->visit(visitor);
528
trueExpr->visit(visitor);
529
falseExpr->visit(visitor);
530
}
531
}
532
533
AstExprError::AstExprError(const Location& location, const AstArray<AstExpr*>& expressions, unsigned messageIndex)
534
: AstExpr(ClassIndex(), location)
535
, expressions(expressions)
536
, messageIndex(messageIndex)
537
{
538
}
539
540
AstExprInterpString::AstExprInterpString(const Location& location, const AstArray<AstArray<char>>& strings, const AstArray<AstExpr*>& expressions)
541
: AstExpr(ClassIndex(), location)
542
, strings(strings)
543
, expressions(expressions)
544
{
545
}
546
547
void AstExprInterpString::visit(AstVisitor* visitor)
548
{
549
if (visitor->visit(this))
550
{
551
for (AstExpr* expr : expressions)
552
expr->visit(visitor);
553
}
554
}
555
556
AstExprInstantiate::AstExprInstantiate(const Location& location, AstExpr* expr, AstArray<AstTypeOrPack> types)
557
: AstExpr(ClassIndex(), location)
558
, expr(expr)
559
, typeArguments(types)
560
{
561
}
562
563
void AstExprInstantiate::visit(AstVisitor* visitor)
564
{
565
if (visitor->visit(this))
566
{
567
expr->visit(visitor);
568
visitTypeOrPackArray(visitor, typeArguments);
569
}
570
}
571
572
573
void AstExprError::visit(AstVisitor* visitor)
574
{
575
if (visitor->visit(this))
576
{
577
for (AstExpr* expression : expressions)
578
expression->visit(visitor);
579
}
580
}
581
582
AstStatBlock::AstStatBlock(const Location& location, const AstArray<AstStat*>& body, bool hasEnd)
583
: AstStat(ClassIndex(), location)
584
, body(body)
585
, hasEnd(hasEnd)
586
{
587
}
588
589
void AstStatBlock::visit(AstVisitor* visitor)
590
{
591
if (visitor->visit(this))
592
{
593
for (AstStat* stat : body)
594
stat->visit(visitor);
595
}
596
}
597
598
AstStatIf::AstStatIf(
599
const Location& location,
600
AstExpr* condition,
601
AstStatBlock* thenbody,
602
AstStat* elsebody,
603
const std::optional<Location>& thenLocation,
604
const std::optional<Location>& elseLocation
605
)
606
: AstStat(ClassIndex(), location)
607
, condition(condition)
608
, thenbody(thenbody)
609
, elsebody(elsebody)
610
, thenLocation(thenLocation)
611
, elseLocation(elseLocation)
612
{
613
}
614
615
void AstStatIf::visit(AstVisitor* visitor)
616
{
617
if (visitor->visit(this))
618
{
619
condition->visit(visitor);
620
thenbody->visit(visitor);
621
622
if (elsebody)
623
elsebody->visit(visitor);
624
}
625
}
626
627
AstStatWhile::AstStatWhile(const Location& location, AstExpr* condition, AstStatBlock* body, bool hasDo, const Location& doLocation)
628
: AstStat(ClassIndex(), location)
629
, condition(condition)
630
, body(body)
631
, hasDo(hasDo)
632
, doLocation(doLocation)
633
{
634
}
635
636
void AstStatWhile::visit(AstVisitor* visitor)
637
{
638
if (visitor->visit(this))
639
{
640
condition->visit(visitor);
641
body->visit(visitor);
642
}
643
}
644
645
AstStatRepeat::AstStatRepeat(const Location& location, AstExpr* condition, AstStatBlock* body, bool DEPRECATED_hasUntil)
646
: AstStat(ClassIndex(), location)
647
, condition(condition)
648
, body(body)
649
, DEPRECATED_hasUntil(DEPRECATED_hasUntil)
650
{
651
}
652
653
void AstStatRepeat::visit(AstVisitor* visitor)
654
{
655
if (visitor->visit(this))
656
{
657
body->visit(visitor);
658
condition->visit(visitor);
659
}
660
}
661
662
AstStatBreak::AstStatBreak(const Location& location)
663
: AstStat(ClassIndex(), location)
664
{
665
}
666
667
void AstStatBreak::visit(AstVisitor* visitor)
668
{
669
visitor->visit(this);
670
}
671
672
AstStatContinue::AstStatContinue(const Location& location)
673
: AstStat(ClassIndex(), location)
674
{
675
}
676
677
void AstStatContinue::visit(AstVisitor* visitor)
678
{
679
visitor->visit(this);
680
}
681
682
AstStatReturn::AstStatReturn(const Location& location, const AstArray<AstExpr*>& list)
683
: AstStat(ClassIndex(), location)
684
, list(list)
685
{
686
}
687
688
void AstStatReturn::visit(AstVisitor* visitor)
689
{
690
if (visitor->visit(this))
691
{
692
for (AstExpr* expr : list)
693
expr->visit(visitor);
694
}
695
}
696
697
AstStatExpr::AstStatExpr(const Location& location, AstExpr* expr)
698
: AstStat(ClassIndex(), location)
699
, expr(expr)
700
{
701
}
702
703
void AstStatExpr::visit(AstVisitor* visitor)
704
{
705
if (visitor->visit(this))
706
expr->visit(visitor);
707
}
708
709
AstStatLocal::AstStatLocal(
710
const Location& location,
711
const AstArray<AstLocal*>& vars,
712
const AstArray<AstExpr*>& values,
713
const std::optional<Location>& equalsSignLocation
714
)
715
: AstStat(ClassIndex(), location)
716
, vars(vars)
717
, values(values)
718
, equalsSignLocation(equalsSignLocation)
719
{
720
}
721
722
void AstStatLocal::visit(AstVisitor* visitor)
723
{
724
if (visitor->visit(this))
725
{
726
for (AstLocal* var : vars)
727
{
728
if (var->annotation)
729
var->annotation->visit(visitor);
730
}
731
732
for (AstExpr* expr : values)
733
expr->visit(visitor);
734
}
735
}
736
737
AstStatFor::AstStatFor(
738
const Location& location,
739
AstLocal* var,
740
AstExpr* from,
741
AstExpr* to,
742
AstExpr* step,
743
AstStatBlock* body,
744
bool hasDo,
745
const Location& doLocation
746
)
747
: AstStat(ClassIndex(), location)
748
, var(var)
749
, from(from)
750
, to(to)
751
, step(step)
752
, body(body)
753
, hasDo(hasDo)
754
, doLocation(doLocation)
755
{
756
}
757
758
void AstStatFor::visit(AstVisitor* visitor)
759
{
760
if (visitor->visit(this))
761
{
762
if (var->annotation)
763
var->annotation->visit(visitor);
764
765
from->visit(visitor);
766
to->visit(visitor);
767
768
if (step)
769
step->visit(visitor);
770
771
body->visit(visitor);
772
}
773
}
774
775
AstStatForIn::AstStatForIn(
776
const Location& location,
777
const AstArray<AstLocal*>& vars,
778
const AstArray<AstExpr*>& values,
779
AstStatBlock* body,
780
bool hasIn,
781
const Location& inLocation,
782
bool hasDo,
783
const Location& doLocation
784
)
785
: AstStat(ClassIndex(), location)
786
, vars(vars)
787
, values(values)
788
, body(body)
789
, hasIn(hasIn)
790
, inLocation(inLocation)
791
, hasDo(hasDo)
792
, doLocation(doLocation)
793
{
794
}
795
796
void AstStatForIn::visit(AstVisitor* visitor)
797
{
798
if (visitor->visit(this))
799
{
800
for (AstLocal* var : vars)
801
{
802
if (var->annotation)
803
var->annotation->visit(visitor);
804
}
805
806
for (AstExpr* expr : values)
807
expr->visit(visitor);
808
809
body->visit(visitor);
810
}
811
}
812
813
AstStatAssign::AstStatAssign(const Location& location, const AstArray<AstExpr*>& vars, const AstArray<AstExpr*>& values)
814
: AstStat(ClassIndex(), location)
815
, vars(vars)
816
, values(values)
817
{
818
}
819
820
void AstStatAssign::visit(AstVisitor* visitor)
821
{
822
if (visitor->visit(this))
823
{
824
for (AstExpr* lvalue : vars)
825
lvalue->visit(visitor);
826
827
for (AstExpr* expr : values)
828
expr->visit(visitor);
829
}
830
}
831
832
AstStatCompoundAssign::AstStatCompoundAssign(const Location& location, AstExprBinary::Op op, AstExpr* var, AstExpr* value)
833
: AstStat(ClassIndex(), location)
834
, op(op)
835
, var(var)
836
, value(value)
837
{
838
}
839
840
void AstStatCompoundAssign::visit(AstVisitor* visitor)
841
{
842
if (visitor->visit(this))
843
{
844
var->visit(visitor);
845
value->visit(visitor);
846
}
847
}
848
849
AstStatFunction::AstStatFunction(const Location& location, AstExpr* name, AstExprFunction* func)
850
: AstStat(ClassIndex(), location)
851
, name(name)
852
, func(func)
853
{
854
}
855
856
void AstStatFunction::visit(AstVisitor* visitor)
857
{
858
if (visitor->visit(this))
859
{
860
name->visit(visitor);
861
func->visit(visitor);
862
}
863
}
864
865
AstStatLocalFunction::AstStatLocalFunction(const Location& location, AstLocal* name, AstExprFunction* func)
866
: AstStat(ClassIndex(), location)
867
, name(name)
868
, func(func)
869
{
870
}
871
872
void AstStatLocalFunction::visit(AstVisitor* visitor)
873
{
874
if (visitor->visit(this))
875
func->visit(visitor);
876
}
877
878
AstStatTypeAlias::AstStatTypeAlias(
879
const Location& location,
880
const AstName& name,
881
const Location& nameLocation,
882
const AstArray<AstGenericType*>& generics,
883
const AstArray<AstGenericTypePack*>& genericPacks,
884
AstType* type,
885
bool exported
886
)
887
: AstStat(ClassIndex(), location)
888
, name(name)
889
, nameLocation(nameLocation)
890
, generics(generics)
891
, genericPacks(genericPacks)
892
, type(type)
893
, exported(exported)
894
{
895
}
896
897
void AstStatTypeAlias::visit(AstVisitor* visitor)
898
{
899
if (visitor->visit(this))
900
{
901
for (AstGenericType* el : generics)
902
{
903
el->visit(visitor);
904
}
905
906
for (AstGenericTypePack* el : genericPacks)
907
{
908
el->visit(visitor);
909
}
910
911
type->visit(visitor);
912
}
913
}
914
915
AstStatTypeFunction::AstStatTypeFunction(
916
const Location& location,
917
const AstName& name,
918
const Location& nameLocation,
919
AstExprFunction* body,
920
bool exported,
921
bool hasErrors
922
)
923
: AstStat(ClassIndex(), location)
924
, name(name)
925
, nameLocation(nameLocation)
926
, body(body)
927
, exported(exported)
928
, hasErrors(hasErrors)
929
{
930
}
931
932
void AstStatTypeFunction::visit(AstVisitor* visitor)
933
{
934
if (visitor->visit(this))
935
body->visit(visitor);
936
}
937
938
AstStatDeclareGlobal::AstStatDeclareGlobal(const Location& location, const AstName& name, const Location& nameLocation, AstType* type)
939
: AstStat(ClassIndex(), location)
940
, name(name)
941
, nameLocation(nameLocation)
942
, type(type)
943
{
944
}
945
946
void AstStatDeclareGlobal::visit(AstVisitor* visitor)
947
{
948
if (visitor->visit(this))
949
type->visit(visitor);
950
}
951
952
AstStatDeclareFunction::AstStatDeclareFunction(
953
const Location& location,
954
const AstName& name,
955
const Location& nameLocation,
956
const AstArray<AstGenericType*>& generics,
957
const AstArray<AstGenericTypePack*>& genericPacks,
958
const AstTypeList& params,
959
const AstArray<AstArgumentName>& paramNames,
960
bool vararg,
961
const Location& varargLocation,
962
AstTypePack* retTypes
963
)
964
: AstStat(ClassIndex(), location)
965
, attributes()
966
, name(name)
967
, nameLocation(nameLocation)
968
, generics(generics)
969
, genericPacks(genericPacks)
970
, params(params)
971
, paramNames(paramNames)
972
, vararg(vararg)
973
, varargLocation(varargLocation)
974
, retTypes(retTypes)
975
{
976
}
977
978
AstStatDeclareFunction::AstStatDeclareFunction(
979
const Location& location,
980
const AstArray<AstAttr*>& attributes,
981
const AstName& name,
982
const Location& nameLocation,
983
const AstArray<AstGenericType*>& generics,
984
const AstArray<AstGenericTypePack*>& genericPacks,
985
const AstTypeList& params,
986
const AstArray<AstArgumentName>& paramNames,
987
bool vararg,
988
const Location& varargLocation,
989
AstTypePack* retTypes
990
)
991
: AstStat(ClassIndex(), location)
992
, attributes(attributes)
993
, name(name)
994
, nameLocation(nameLocation)
995
, generics(generics)
996
, genericPacks(genericPacks)
997
, params(params)
998
, paramNames(paramNames)
999
, vararg(vararg)
1000
, varargLocation(varargLocation)
1001
, retTypes(retTypes)
1002
{
1003
}
1004
1005
void AstStatDeclareFunction::visit(AstVisitor* visitor)
1006
{
1007
if (visitor->visit(this))
1008
{
1009
visitTypeList(visitor, params);
1010
retTypes->visit(visitor);
1011
}
1012
}
1013
1014
bool AstStatDeclareFunction::isCheckedFunction() const
1015
{
1016
for (const AstAttr* attr : attributes)
1017
{
1018
if (attr->type == AstAttr::Type::Checked)
1019
return true;
1020
}
1021
1022
return false;
1023
}
1024
1025
bool AstStatDeclareFunction::hasAttribute(AstAttr::Type attributeType) const
1026
{
1027
return hasAttributeInArray(attributes, attributeType);
1028
}
1029
1030
AstAttr* AstStatDeclareFunction::getAttribute(const AstAttr::Type attributeType) const
1031
{
1032
return findAttributeInArray(attributes, attributeType);
1033
}
1034
1035
AstStatDeclareExternType::AstStatDeclareExternType(
1036
const Location& location,
1037
const AstName& name,
1038
std::optional<AstName> superName,
1039
const AstArray<AstDeclaredExternTypeProperty>& props,
1040
AstTableIndexer* indexer
1041
)
1042
: AstStat(ClassIndex(), location)
1043
, name(name)
1044
, superName(superName)
1045
, props(props)
1046
, indexer(indexer)
1047
{
1048
}
1049
1050
void AstStatDeclareExternType::visit(AstVisitor* visitor)
1051
{
1052
if (visitor->visit(this))
1053
{
1054
for (const AstDeclaredExternTypeProperty& prop : props)
1055
prop.ty->visit(visitor);
1056
}
1057
}
1058
1059
AstStatError::AstStatError(
1060
const Location& location,
1061
const AstArray<AstExpr*>& expressions,
1062
const AstArray<AstStat*>& statements,
1063
unsigned messageIndex
1064
)
1065
: AstStat(ClassIndex(), location)
1066
, expressions(expressions)
1067
, statements(statements)
1068
, messageIndex(messageIndex)
1069
{
1070
}
1071
1072
void AstStatError::visit(AstVisitor* visitor)
1073
{
1074
if (visitor->visit(this))
1075
{
1076
for (AstNode* expression : expressions)
1077
expression->visit(visitor);
1078
1079
for (AstNode* statement : statements)
1080
statement->visit(visitor);
1081
}
1082
}
1083
1084
AstTypeReference::AstTypeReference(
1085
const Location& location,
1086
std::optional<AstName> prefix,
1087
AstName name,
1088
std::optional<Location> prefixLocation,
1089
const Location& nameLocation,
1090
bool hasParameterList,
1091
const AstArray<AstTypeOrPack>& parameters
1092
)
1093
: AstType(ClassIndex(), location)
1094
, hasParameterList(hasParameterList)
1095
, prefix(prefix)
1096
, prefixLocation(prefixLocation)
1097
, name(name)
1098
, nameLocation(nameLocation)
1099
, parameters(parameters)
1100
{
1101
}
1102
1103
void AstTypeReference::visit(AstVisitor* visitor)
1104
{
1105
if (visitor->visit(this))
1106
{
1107
visitTypeOrPackArray(visitor, parameters);
1108
}
1109
}
1110
1111
AstTypeTable::AstTypeTable(const Location& location, const AstArray<AstTableProp>& props, AstTableIndexer* indexer)
1112
: AstType(ClassIndex(), location)
1113
, props(props)
1114
, indexer(indexer)
1115
{
1116
}
1117
1118
void AstTypeTable::visit(AstVisitor* visitor)
1119
{
1120
if (visitor->visit(this))
1121
{
1122
for (const AstTableProp& prop : props)
1123
prop.type->visit(visitor);
1124
1125
if (indexer)
1126
{
1127
indexer->indexType->visit(visitor);
1128
indexer->resultType->visit(visitor);
1129
}
1130
}
1131
}
1132
1133
AstTypeFunction::AstTypeFunction(
1134
const Location& location,
1135
const AstArray<AstGenericType*>& generics,
1136
const AstArray<AstGenericTypePack*>& genericPacks,
1137
const AstTypeList& argTypes,
1138
const AstArray<std::optional<AstArgumentName>>& argNames,
1139
AstTypePack* returnTypes
1140
)
1141
: AstType(ClassIndex(), location)
1142
, attributes()
1143
, generics(generics)
1144
, genericPacks(genericPacks)
1145
, argTypes(argTypes)
1146
, argNames(argNames)
1147
, returnTypes(returnTypes)
1148
{
1149
LUAU_ASSERT(argNames.size == 0 || argNames.size == argTypes.types.size);
1150
}
1151
1152
AstTypeFunction::AstTypeFunction(
1153
const Location& location,
1154
const AstArray<AstAttr*>& attributes,
1155
const AstArray<AstGenericType*>& generics,
1156
const AstArray<AstGenericTypePack*>& genericPacks,
1157
const AstTypeList& argTypes,
1158
const AstArray<std::optional<AstArgumentName>>& argNames,
1159
AstTypePack* returnTypes
1160
)
1161
: AstType(ClassIndex(), location)
1162
, attributes(attributes)
1163
, generics(generics)
1164
, genericPacks(genericPacks)
1165
, argTypes(argTypes)
1166
, argNames(argNames)
1167
, returnTypes(returnTypes)
1168
{
1169
LUAU_ASSERT(argNames.size == 0 || argNames.size == argTypes.types.size);
1170
}
1171
1172
void AstTypeFunction::visit(AstVisitor* visitor)
1173
{
1174
if (visitor->visit(this))
1175
{
1176
visitTypeList(visitor, argTypes);
1177
returnTypes->visit(visitor);
1178
}
1179
}
1180
1181
bool AstTypeFunction::isCheckedFunction() const
1182
{
1183
for (const AstAttr* attr : attributes)
1184
{
1185
if (attr->type == AstAttr::Type::Checked)
1186
return true;
1187
}
1188
1189
return false;
1190
}
1191
1192
bool AstTypeFunction::hasAttribute(AstAttr::Type attributeType) const
1193
{
1194
return hasAttributeInArray(attributes, attributeType);
1195
}
1196
1197
AstAttr* AstTypeFunction::getAttribute(AstAttr::Type attributeType) const
1198
{
1199
return findAttributeInArray(attributes, attributeType);
1200
}
1201
1202
AstTypeTypeof::AstTypeTypeof(const Location& location, AstExpr* expr)
1203
: AstType(ClassIndex(), location)
1204
, expr(expr)
1205
{
1206
}
1207
1208
void AstTypeTypeof::visit(AstVisitor* visitor)
1209
{
1210
if (visitor->visit(this))
1211
expr->visit(visitor);
1212
}
1213
1214
AstTypeOptional::AstTypeOptional(const Location& location)
1215
: AstType(ClassIndex(), location)
1216
{
1217
}
1218
1219
void AstTypeOptional::visit(AstVisitor* visitor)
1220
{
1221
visitor->visit(this);
1222
}
1223
1224
AstTypeUnion::AstTypeUnion(const Location& location, const AstArray<AstType*>& types)
1225
: AstType(ClassIndex(), location)
1226
, types(types)
1227
{
1228
}
1229
1230
void AstTypeUnion::visit(AstVisitor* visitor)
1231
{
1232
if (visitor->visit(this))
1233
{
1234
for (AstType* type : types)
1235
type->visit(visitor);
1236
}
1237
}
1238
1239
AstTypeIntersection::AstTypeIntersection(const Location& location, const AstArray<AstType*>& types)
1240
: AstType(ClassIndex(), location)
1241
, types(types)
1242
{
1243
}
1244
1245
void AstTypeIntersection::visit(AstVisitor* visitor)
1246
{
1247
if (visitor->visit(this))
1248
{
1249
for (AstType* type : types)
1250
type->visit(visitor);
1251
}
1252
}
1253
1254
AstTypeSingletonBool::AstTypeSingletonBool(const Location& location, bool value)
1255
: AstType(ClassIndex(), location)
1256
, value(value)
1257
{
1258
}
1259
1260
void AstTypeSingletonBool::visit(AstVisitor* visitor)
1261
{
1262
visitor->visit(this);
1263
}
1264
1265
AstTypeSingletonString::AstTypeSingletonString(const Location& location, const AstArray<char>& value)
1266
: AstType(ClassIndex(), location)
1267
, value(value)
1268
{
1269
}
1270
1271
void AstTypeSingletonString::visit(AstVisitor* visitor)
1272
{
1273
visitor->visit(this);
1274
}
1275
1276
AstTypeGroup::AstTypeGroup(const Location& location, AstType* type)
1277
: AstType(ClassIndex(), location)
1278
, type(type)
1279
{
1280
}
1281
1282
void AstTypeGroup::visit(AstVisitor* visitor)
1283
{
1284
if (visitor->visit(this))
1285
type->visit(visitor);
1286
}
1287
1288
AstTypeError::AstTypeError(const Location& location, const AstArray<AstType*>& types, bool isMissing, unsigned messageIndex)
1289
: AstType(ClassIndex(), location)
1290
, types(types)
1291
, isMissing(isMissing)
1292
, messageIndex(messageIndex)
1293
{
1294
}
1295
1296
void AstTypeError::visit(AstVisitor* visitor)
1297
{
1298
if (visitor->visit(this))
1299
{
1300
for (AstType* type : types)
1301
type->visit(visitor);
1302
}
1303
}
1304
1305
AstTypePackExplicit::AstTypePackExplicit(const Location& location, AstTypeList typeList)
1306
: AstTypePack(ClassIndex(), location)
1307
, typeList(typeList)
1308
{
1309
}
1310
1311
void AstTypePackExplicit::visit(AstVisitor* visitor)
1312
{
1313
if (visitor->visit(this))
1314
{
1315
for (AstType* type : typeList.types)
1316
type->visit(visitor);
1317
1318
if (typeList.tailType)
1319
typeList.tailType->visit(visitor);
1320
}
1321
}
1322
1323
AstTypePackVariadic::AstTypePackVariadic(const Location& location, AstType* variadicType)
1324
: AstTypePack(ClassIndex(), location)
1325
, variadicType(variadicType)
1326
{
1327
}
1328
1329
void AstTypePackVariadic::visit(AstVisitor* visitor)
1330
{
1331
if (visitor->visit(this))
1332
variadicType->visit(visitor);
1333
}
1334
1335
AstTypePackGeneric::AstTypePackGeneric(const Location& location, AstName name)
1336
: AstTypePack(ClassIndex(), location)
1337
, genericName(name)
1338
{
1339
}
1340
1341
void AstTypePackGeneric::visit(AstVisitor* visitor)
1342
{
1343
visitor->visit(this);
1344
}
1345
1346
bool isLValue(const AstExpr* expr)
1347
{
1348
return expr->is<AstExprLocal>() || expr->is<AstExprGlobal>() || expr->is<AstExprIndexName>() || expr->is<AstExprIndexExpr>();
1349
}
1350
1351
bool isConstantLiteral(const AstExpr* expr)
1352
{
1353
return expr->is<AstExprConstantNil>() || expr->is<AstExprConstantBool>() || expr->is<AstExprConstantNumber>() ||
1354
expr->is<AstExprConstantString>();
1355
}
1356
1357
bool isLiteralTable(const AstExpr* expr)
1358
{
1359
if (!expr->is<AstExprTable>())
1360
return false;
1361
1362
for (const AstExprTable::Item& item : expr->as<AstExprTable>()->items)
1363
{
1364
switch (item.kind)
1365
{
1366
case AstExprTable::Item::Kind::General:
1367
return false;
1368
break;
1369
case AstExprTable::Item::Kind::Record:
1370
case AstExprTable::Item::Kind::List:
1371
if (!isConstantLiteral(item.value) && !isLiteralTable(item.value))
1372
return false;
1373
break;
1374
}
1375
}
1376
return true;
1377
}
1378
1379
AstName getIdentifier(AstExpr* node)
1380
{
1381
if (AstExprGlobal* expr = node->as<AstExprGlobal>())
1382
return expr->name;
1383
1384
if (AstExprLocal* expr = node->as<AstExprLocal>())
1385
return expr->local->name;
1386
1387
return AstName();
1388
}
1389
1390
Location getLocation(const AstTypeList& typeList)
1391
{
1392
Location result;
1393
if (typeList.types.size)
1394
{
1395
result = Location{typeList.types.data[0]->location, typeList.types.data[typeList.types.size - 1]->location};
1396
}
1397
if (typeList.tailType)
1398
result.end = typeList.tailType->location.end;
1399
1400
return result;
1401
}
1402
1403
} // namespace Luau
1404
1405