luau/Compiler/src/ConstantFolding.h

50 lines
1.1 KiB
C
Raw Normal View History

2022-01-14 16:06:31 +00:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include "ValueTracking.h"
namespace Luau
{
namespace Compile
{
struct Constant
{
enum Type
{
Type_Unknown,
Type_Nil,
Type_Boolean,
Type_Number,
Type_String,
};
Type type = Type_Unknown;
unsigned int stringLength = 0;
union
{
bool valueBoolean;
double valueNumber;
2022-07-14 23:39:35 +01:00
const char* valueString = nullptr; // length stored in stringLength
2022-01-14 16:06:31 +00:00
};
bool isTruthful() const
{
LUAU_ASSERT(type != Type_Unknown);
return type != Type_Nil && !(type == Type_Boolean && valueBoolean == false);
}
2022-07-14 23:39:35 +01:00
AstArray<const char> getString() const
2022-01-14 16:06:31 +00:00
{
LUAU_ASSERT(type == Type_String);
return {valueString, stringLength};
}
};
2022-04-21 22:04:22 +01:00
void foldConstants(DenseHashMap<AstExpr*, Constant>& constants, DenseHashMap<AstLocal*, Variable>& variables,
2022-07-14 23:39:35 +01:00
DenseHashMap<AstLocal*, Constant>& locals, const DenseHashMap<AstExprCall*, int>* builtins, AstNode* root);
2022-01-14 16:06:31 +00:00
} // namespace Compile
} // namespace Luau