2021-10-30 04:25:12 +08: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/Common.h"
|
|
|
|
#include "Luau/TypeVar.h"
|
2022-06-04 04:32:20 +08:00
|
|
|
#include "Luau/ConstraintGraphBuilder.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <optional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
LUAU_FASTINT(LuauTableTypeMaximumStringifierLength)
|
|
|
|
LUAU_FASTINT(LuauTypeMaximumStringifierLength)
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct ToStringNameMap
|
|
|
|
{
|
|
|
|
std::unordered_map<TypeId, std::string> typeVars;
|
|
|
|
std::unordered_map<TypePackId, std::string> typePacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ToStringOptions
|
|
|
|
{
|
2021-11-19 06:21:07 +08:00
|
|
|
bool exhaustive = false; // If true, we produce complete output rather than comprehensible output
|
|
|
|
bool useLineBreaks = false; // If true, we insert new lines to separate long results such as table entries/metatable.
|
|
|
|
bool functionTypeArguments = false; // If true, output function type argument names when they are available
|
|
|
|
bool hideTableKind = false; // If true, all tables will be surrounded with plain '{}'
|
|
|
|
bool hideNamedFunctionTypeParameters = false; // If true, type parameters of functions will be hidden at top-level.
|
2022-05-20 07:46:52 +08:00
|
|
|
bool hideFunctionSelfArgument = false; // If true, `self: X` will be omitted from the function signature if the function has self
|
2022-04-15 05:57:15 +08:00
|
|
|
bool indent = false;
|
2021-10-30 04:25:12 +08:00
|
|
|
size_t maxTableLength = size_t(FInt::LuauTableTypeMaximumStringifierLength); // Only applied to TableTypeVars
|
|
|
|
size_t maxTypeLength = size_t(FInt::LuauTypeMaximumStringifierLength);
|
|
|
|
std::optional<ToStringNameMap> nameMap;
|
|
|
|
std::shared_ptr<Scope> scope; // If present, module names will be added and types that are not available in scope will be marked as 'invalid'
|
2022-03-25 05:49:08 +08:00
|
|
|
std::vector<std::string> namedFunctionOverrideArgNames; // If present, named function argument names will be overridden
|
2021-10-30 04:25:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ToStringResult
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
ToStringNameMap nameMap;
|
|
|
|
|
|
|
|
bool invalid = false;
|
|
|
|
bool error = false;
|
|
|
|
bool cycle = false;
|
|
|
|
bool truncated = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ToStringResult toStringDetailed(TypeId ty, const ToStringOptions& opts = {});
|
|
|
|
ToStringResult toStringDetailed(TypePackId ty, const ToStringOptions& opts = {});
|
|
|
|
|
|
|
|
std::string toString(TypeId ty, const ToStringOptions& opts);
|
|
|
|
std::string toString(TypePackId ty, const ToStringOptions& opts);
|
2022-06-04 04:32:20 +08:00
|
|
|
std::string toString(const Constraint& c, ToStringOptions& opts);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
// These are offered as overloads rather than a default parameter so that they can be easily invoked from within the MSVC debugger.
|
|
|
|
// You can use them in watch expressions!
|
|
|
|
inline std::string toString(TypeId ty)
|
|
|
|
{
|
|
|
|
return toString(ty, ToStringOptions{});
|
|
|
|
}
|
|
|
|
inline std::string toString(TypePackId ty)
|
|
|
|
{
|
|
|
|
return toString(ty, ToStringOptions{});
|
|
|
|
}
|
2022-06-04 04:32:20 +08:00
|
|
|
inline std::string toString(const Constraint& c)
|
|
|
|
{
|
|
|
|
ToStringOptions opts;
|
|
|
|
return toString(c, opts);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::string toString(const TypeVar& tv, const ToStringOptions& opts = {});
|
|
|
|
std::string toString(const TypePackVar& tp, const ToStringOptions& opts = {});
|
|
|
|
|
2022-03-25 05:49:08 +08:00
|
|
|
std::string toStringNamedFunction(const std::string& funcName, const FunctionTypeVar& ftv, const ToStringOptions& opts = {});
|
2021-11-19 06:21:07 +08:00
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
// It could be useful to see the text representation of a type during a debugging session instead of exploring the content of the class
|
|
|
|
// These functions will dump the type to stdout and can be evaluated in Watch/Immediate windows or as gdb/lldb expression
|
2021-12-11 05:17:10 +08:00
|
|
|
std::string dump(TypeId ty);
|
|
|
|
std::string dump(TypePackId ty);
|
2022-06-04 04:32:20 +08:00
|
|
|
std::string dump(const Constraint& c);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-04-15 05:57:15 +08:00
|
|
|
std::string dump(const std::shared_ptr<Scope>& scope, const char* name);
|
|
|
|
|
2021-11-05 10:42:00 +08:00
|
|
|
std::string generateName(size_t n);
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
} // namespace Luau
|