// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details1#pragma once23#include "Luau/NotNull.h"4#include "Luau/TypedAllocator.h"5#include "Luau/Variant.h"6#include "Luau/Location.h"7#include "Luau/Symbol.h"8#include <string>9#include <optional>1011namespace Luau12{1314struct Def;15using DefId = NotNull<const Def>;16struct AstLocal;1718/**19* A cell is a "single-object" value.20*21* Leaky implementation note: sometimes "multiple-object" values, but none of which were interesting enough to warrant creating a phi node instead.22* 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"`.23* 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`.24*/25struct Cell26{27bool subscripted = false;28};2930/**31* A phi node is a union of cells.32*33* We need this because we're statically evaluating a program, and sometimes a place may be assigned with34* different cells, and when that happens, we need a special data type that merges in all the cells35* that will flow into that specific place. For example, consider this simple program:36*37* ```38* x-139* if cond() then40* x-2 = 541* else42* x-3 = "hello"43* end44* x-4 : {x-2, x-3}45* ```46*47* At x-4, we know for a fact statically that either `5` or `"hello"` can flow into the variable `x` after the branch, but48* we cannot make any definitive decisions about which one, so we just take in both.49*/50struct Phi51{52std::vector<DefId> operands;53};5455/**56* We statically approximate a value at runtime using a symbolic value, which we call a Def.57*58* DataFlowGraphBuilder will allocate these defs as a stand-in for some Luau values, and bind them to places that59* can hold a Luau value, and then observes how those defs will commute as it statically evaluate the program.60*61* 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.62*/63struct Def64{65using V = Variant<struct Cell, struct Phi>;6667V v;68Symbol name;69Location location;70};7172template<typename T>73const T* get(DefId def)74{75return get_if<T>(&def->v);76}7778bool containsSubscriptedDefinition(DefId def);79void collectOperands(DefId def, std::vector<DefId>* operands);8081struct DefArena82{83TypedAllocator<Def> allocator;8485DefId freshCell(Symbol sym, Location location, bool subscripted = false);86DefId phi(DefId a, DefId b);87DefId phi(const std::vector<DefId>& defs);88};8990} // namespace Luau919293