// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details #pragma once #include "Luau/Location.h" #include namespace Luau { extern int gCstRttiIndex; template struct CstRtti { static const int value; }; template const int CstRtti::value = ++gCstRttiIndex; #define LUAU_CST_RTTI(Class) \ static int CstClassIndex() \ { \ return CstRtti::value; \ } class CstNode { public: explicit CstNode(int classIndex) : classIndex(classIndex) { } template bool is() const { return classIndex == T::CstClassIndex(); } template T* as() { return classIndex == T::CstClassIndex() ? static_cast(this) : nullptr; } template const T* as() const { return classIndex == T::CstClassIndex() ? static_cast(this) : nullptr; } const int classIndex; }; class CstExprConstantNumber : public CstNode { public: LUAU_CST_RTTI(CstExprConstantNumber) explicit CstExprConstantNumber(const AstArray& value); AstArray value; }; class CstExprConstantString : public CstNode { public: LUAU_CST_RTTI(CstExprConstantNumber) enum QuoteStyle { QuotedSingle, QuotedDouble, QuotedRaw, QuotedInterp, }; CstExprConstantString(AstArray sourceString, QuoteStyle quoteStyle, unsigned int blockDepth); AstArray sourceString; QuoteStyle quoteStyle; unsigned int blockDepth; }; class CstExprCall : public CstNode { public: LUAU_CST_RTTI(CstExprCall) CstExprCall(std::optional openParens, std::optional closeParens, AstArray commaPositions); std::optional openParens; std::optional closeParens; AstArray commaPositions; }; class CstExprIndexExpr : public CstNode { public: LUAU_CST_RTTI(CstExprIndexExpr) CstExprIndexExpr(Position openBracketPosition, Position closeBracketPosition); Position openBracketPosition; Position closeBracketPosition; }; class CstExprFunction : public CstNode { public: LUAU_CST_RTTI(CstExprFunction) CstExprFunction(); Position functionKeywordPosition{0, 0}; Position openGenericsPosition{0,0}; AstArray genericsCommaPositions; Position closeGenericsPosition{0,0}; AstArray argsCommaPositions; Position returnSpecifierPosition{0,0}; }; class CstExprTable : public CstNode { public: LUAU_CST_RTTI(CstExprTable) enum Separator { Comma, Semicolon, }; struct Item { std::optional indexerOpenPosition; // '[', only if Kind == General std::optional indexerClosePosition; // ']', only if Kind == General std::optional equalsPosition; // only if Kind != List std::optional separator; // may be missing for last Item std::optional separatorPosition; }; explicit CstExprTable(const AstArray& items); AstArray items; }; // TODO: Shared between unary and binary, should we split? class CstExprOp : public CstNode { public: LUAU_CST_RTTI(CstExprOp) explicit CstExprOp(Position opPosition); Position opPosition; }; class CstExprTypeAssertion : public CstNode { public: LUAU_CST_RTTI(CstExprTypeAssertion) explicit CstExprTypeAssertion(Position opPosition); Position opPosition; }; class CstExprIfElse : public CstNode { public: LUAU_CST_RTTI(CstExprIfElse) CstExprIfElse(Position thenPosition, Position elsePosition, bool isElseIf); Position thenPosition; Position elsePosition; bool isElseIf; }; class CstExprInterpString : public CstNode { public: LUAU_CST_RTTI(CstExprInterpString) explicit CstExprInterpString(AstArray> sourceStrings, AstArray stringPositions); AstArray> sourceStrings; AstArray stringPositions; }; class CstStatDo : public CstNode { public: LUAU_CST_RTTI(CstStatDo) explicit CstStatDo(Position endPosition); Position endPosition; }; class CstStatRepeat : public CstNode { public: LUAU_CST_RTTI(CstStatRepeat) explicit CstStatRepeat(Position untilPosition); Position untilPosition; }; class CstStatReturn : public CstNode { public: LUAU_CST_RTTI(CstStatReturn) explicit CstStatReturn(AstArray commaPositions); AstArray commaPositions; }; class CstStatLocal : public CstNode { public: LUAU_CST_RTTI(CstStatLocal) CstStatLocal(AstArray varsCommaPositions, AstArray valuesCommaPositions); AstArray varsCommaPositions; AstArray valuesCommaPositions; }; class CstStatFor : public CstNode { public: LUAU_CST_RTTI(CstStatFor) CstStatFor(Position equalsPosition, Position endCommaPosition, std::optional stepCommaPosition); Position equalsPosition; Position endCommaPosition; std::optional stepCommaPosition; }; class CstStatForIn : public CstNode { public: LUAU_CST_RTTI(CstStatForIn) CstStatForIn(AstArray varsCommaPositions, AstArray valuesCommaPositions); AstArray varsCommaPositions; AstArray valuesCommaPositions; }; class CstStatAssign : public CstNode { public: LUAU_CST_RTTI(CstStatAssign) CstStatAssign(AstArray varsCommaPositions, Position equalsPosition, AstArray valuesCommaPositions); AstArray varsCommaPositions; Position equalsPosition; AstArray valuesCommaPositions; }; class CstStatCompoundAssign : public CstNode { public: LUAU_CST_RTTI(CstStatCompoundAssign) explicit CstStatCompoundAssign(Position opPosition); Position opPosition; }; class CstStatFunction : public CstNode { public: LUAU_CST_RTTI(CstStatFunction) explicit CstStatFunction(Position functionKeywordPosition); Position functionKeywordPosition; }; class CstStatLocalFunction : public CstNode { public: LUAU_CST_RTTI(CstStatLocalFunction) explicit CstStatLocalFunction(Position localKeywordPosition, Position functionKeywordPosition); Position localKeywordPosition; Position functionKeywordPosition; }; class CstGenericType : public CstNode { public: LUAU_CST_RTTI(CstGenericType) CstGenericType(std::optional defaultEqualsPosition); std::optional defaultEqualsPosition; }; class CstGenericTypePack : public CstNode { public: LUAU_CST_RTTI(CstGenericTypePack) CstGenericTypePack(Position ellipsisPosition, std::optional defaultEqualsPosition); Position ellipsisPosition; std::optional defaultEqualsPosition; }; class CstStatTypeAlias : public CstNode { public: LUAU_CST_RTTI(CstStatTypeAlias) CstStatTypeAlias( Position typeKeywordPosition, Position genericsOpenPosition, AstArray genericsCommaPositions, Position genericsClosePosition, Position equalsPosition ); Position typeKeywordPosition; Position genericsOpenPosition; AstArray genericsCommaPositions; Position genericsClosePosition; Position equalsPosition; }; class CstStatTypeFunction : public CstNode { public: LUAU_CST_RTTI(CstStatTypeFunction) CstStatTypeFunction(Position typeKeywordPosition, Position functionKeywordPosition); Position typeKeywordPosition; Position functionKeywordPosition; }; class CstTypeReference : public CstNode { public: LUAU_CST_RTTI(CstTypeReference) CstTypeReference( std::optional prefixPointPosition, Position openParametersPosition, AstArray parametersCommaPositions, Position closeParametersPosition ); std::optional prefixPointPosition; Position openParametersPosition; AstArray parametersCommaPositions; Position closeParametersPosition; }; class CstTypeTable : public CstNode { public: LUAU_CST_RTTI(CstTypeTable) struct Item { enum struct Kind { Indexer, Property, StringProperty, }; Kind kind; Position indexerOpenPosition; // '[', only if Kind != Property Position indexerClosePosition; // ']' only if Kind != Property Position colonPosition; std::optional separator; // may be missing for last Item std::optional separatorPosition; CstExprConstantString* stringInfo = nullptr; // only if Kind == StringProperty }; CstTypeTable(AstArray items, bool isArray); AstArray items; bool isArray = false; }; class CstTypeFunction : public CstNode { public: LUAU_CST_RTTI(CstTypeFunction) CstTypeFunction( Position openGenericsPosition, AstArray genericsCommaPositions, Position closeGenericsPosition, Position openArgsPosition, AstArray> argumentNameColonPositions, AstArray argumentsCommaPositions, Position closeArgsPosition, Position returnArrowPosition ); Position openGenericsPosition; AstArray genericsCommaPositions; Position closeGenericsPosition; Position openArgsPosition; AstArray> argumentNameColonPositions; AstArray argumentsCommaPositions; Position closeArgsPosition; Position returnArrowPosition; }; class CstTypeTypeof : public CstNode { public: LUAU_CST_RTTI(CstTypeTypeof) CstTypeTypeof(Position openPosition, Position closePosition); Position openPosition; Position closePosition; }; class CstTypeUnion : public CstNode { public: LUAU_CST_RTTI(CstTypeUnion) CstTypeUnion(std::optional leadingPosition, AstArray separatorPositions); std::optional leadingPosition; AstArray separatorPositions; }; class CstTypeIntersection : public CstNode { public: LUAU_CST_RTTI(CstTypeIntersection) explicit CstTypeIntersection(std::optional leadingPosition, AstArray separatorPositions); std::optional leadingPosition; AstArray separatorPositions; }; class CstTypeSingletonString : public CstNode { public: LUAU_CST_RTTI(CstTypeSingletonString) CstTypeSingletonString(AstArray sourceString, CstExprConstantString::QuoteStyle quoteStyle, unsigned int blockDepth); AstArray sourceString; CstExprConstantString::QuoteStyle quoteStyle; unsigned int blockDepth; }; class CstTypePackExplicit : public CstNode { public: LUAU_CST_RTTI(CstTypePackExplicit) CstTypePackExplicit(Position openParenthesesPosition, Position closeParenthesesPosition, AstArray commaPositions); Position openParenthesesPosition; Position closeParenthesesPosition; AstArray commaPositions; }; class CstTypePackGeneric : public CstNode { public: LUAU_CST_RTTI(CstTypePackGeneric) explicit CstTypePackGeneric(Position ellipsisPosition); Position ellipsisPosition; }; } // namespace Luau