2022-10-21 18:54:01 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/NotNull.h"
|
|
|
|
#include "Luau/TypedAllocator.h"
|
|
|
|
#include "Luau/Variant.h"
|
|
|
|
|
2022-12-02 18:09:59 +00:00
|
|
|
#include <string>
|
|
|
|
#include <optional>
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-12-02 18:09:59 +00:00
|
|
|
struct Def;
|
2022-10-21 18:54:01 +01:00
|
|
|
using DefId = NotNull<const Def>;
|
|
|
|
|
|
|
|
/**
|
2022-12-02 18:09:59 +00:00
|
|
|
* A cell is a "single-object" value.
|
2022-10-21 18:54:01 +01:00
|
|
|
*
|
|
|
|
* Leaky implementation note: sometimes "multiple-object" values, but none of which were interesting enough to warrant creating a phi node instead.
|
|
|
|
* That can happen because there's no point in creating a phi node that points to either resultant in `if math.random() > 0.5 then 5 else "hello"`.
|
|
|
|
* This might become of utmost importance if we wanted to do some backward reasoning, e.g. if `5` is taken, then `cond` must be `truthy`.
|
|
|
|
*/
|
2022-12-02 18:09:59 +00:00
|
|
|
struct Cell
|
2022-10-21 18:54:01 +01:00
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
bool subscripted = false;
|
2022-10-21 18:54:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-12-02 18:09:59 +00:00
|
|
|
* A phi node is a union of cells.
|
2022-10-21 18:54:01 +01:00
|
|
|
*
|
|
|
|
* We need this because we're statically evaluating a program, and sometimes a place may be assigned with
|
2022-12-02 18:09:59 +00:00
|
|
|
* different cells, and when that happens, we need a special data type that merges in all the cells
|
2022-10-21 18:54:01 +01:00
|
|
|
* that will flow into that specific place. For example, consider this simple program:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* x-1
|
|
|
|
* if cond() then
|
|
|
|
* x-2 = 5
|
|
|
|
* else
|
|
|
|
* x-3 = "hello"
|
|
|
|
* end
|
|
|
|
* x-4 : {x-2, x-3}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* At x-4, we know for a fact statically that either `5` or `"hello"` can flow into the variable `x` after the branch, but
|
|
|
|
* we cannot make any definitive decisions about which one, so we just take in both.
|
|
|
|
*/
|
|
|
|
struct Phi
|
|
|
|
{
|
|
|
|
std::vector<DefId> operands;
|
|
|
|
};
|
|
|
|
|
2022-12-02 18:09:59 +00:00
|
|
|
/**
|
|
|
|
* We statically approximate a value at runtime using a symbolic value, which we call a Def.
|
|
|
|
*
|
|
|
|
* DataFlowGraphBuilder will allocate these defs as a stand-in for some Luau values, and bind them to places that
|
|
|
|
* can hold a Luau value, and then observes how those defs will commute as it statically evaluate the program.
|
|
|
|
*
|
|
|
|
* It must also be noted that defs are a cyclic graph, so it is not safe to recursively traverse into it expecting it to terminate.
|
|
|
|
*/
|
|
|
|
struct Def
|
2022-10-21 18:54:01 +01:00
|
|
|
{
|
2022-12-02 18:09:59 +00:00
|
|
|
using V = Variant<struct Cell, struct Phi>;
|
|
|
|
|
|
|
|
V v;
|
|
|
|
};
|
2022-10-21 18:54:01 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T* get(DefId def)
|
|
|
|
{
|
2022-12-02 18:09:59 +00:00
|
|
|
return get_if<T>(&def->v);
|
2022-10-21 18:54:01 +01:00
|
|
|
}
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
bool containsSubscriptedDefinition(DefId def);
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
struct DefArena
|
|
|
|
{
|
|
|
|
TypedAllocator<Def> allocator;
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
DefId freshCell(bool subscripted = false);
|
2023-11-10 21:10:07 +00:00
|
|
|
DefId phi(DefId a, DefId b);
|
2023-11-17 18:46:18 +00:00
|
|
|
DefId phi(const std::vector<DefId>& defs);
|
2022-10-21 18:54:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|