luau/Analysis/src/Anyification.cpp

91 lines
2.3 KiB
C++
Raw Normal View History

2022-08-18 22:04:33 +01:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/Anyification.h"
#include "Luau/Common.h"
#include "Luau/Normalize.h"
#include "Luau/TxnLog.h"
LUAU_FASTFLAG(LuauClassTypeVarsInSubstitution)
namespace Luau
{
2023-01-03 17:33:19 +00:00
Anyification::Anyification(TypeArena* arena, NotNull<Scope> scope, NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter* iceHandler,
2022-09-08 22:44:50 +01:00
TypeId anyType, TypePackId anyTypePack)
2022-08-18 22:04:33 +01:00
: Substitution(TxnLog::empty(), arena)
2022-09-02 00:00:14 +01:00
, scope(scope)
2023-01-03 17:33:19 +00:00
, builtinTypes(builtinTypes)
2022-08-18 22:04:33 +01:00
, iceHandler(iceHandler)
, anyType(anyType)
, anyTypePack(anyTypePack)
{
}
2023-01-03 17:33:19 +00:00
Anyification::Anyification(TypeArena* arena, const ScopePtr& scope, NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter* iceHandler,
2022-09-08 22:44:50 +01:00
TypeId anyType, TypePackId anyTypePack)
2023-01-03 17:33:19 +00:00
: Anyification(arena, NotNull{scope.get()}, builtinTypes, iceHandler, anyType, anyTypePack)
2022-09-02 00:00:14 +01:00
{
}
2022-08-18 22:04:33 +01:00
bool Anyification::isDirty(TypeId ty)
{
if (ty->persistent)
return false;
2023-01-03 17:33:19 +00:00
if (const TableType* ttv = log->getMutable<TableType>(ty))
2022-08-18 22:04:33 +01:00
return (ttv->state == TableState::Free || ttv->state == TableState::Unsealed);
2023-01-03 17:33:19 +00:00
else if (log->getMutable<FreeType>(ty))
2022-08-18 22:04:33 +01:00
return true;
else
return false;
}
bool Anyification::isDirty(TypePackId tp)
{
if (tp->persistent)
return false;
if (log->getMutable<FreeTypePack>(tp))
return true;
else
return false;
}
TypeId Anyification::clean(TypeId ty)
{
LUAU_ASSERT(isDirty(ty));
2023-01-03 17:33:19 +00:00
if (const TableType* ttv = log->getMutable<TableType>(ty))
2022-08-18 22:04:33 +01:00
{
2023-01-03 17:33:19 +00:00
TableType clone = TableType{ttv->props, ttv->indexer, ttv->level, TableState::Sealed};
2022-08-18 22:04:33 +01:00
clone.definitionModuleName = ttv->definitionModuleName;
clone.name = ttv->name;
clone.syntheticName = ttv->syntheticName;
clone.tags = ttv->tags;
TypeId res = addType(std::move(clone));
return res;
}
else
return anyType;
}
TypePackId Anyification::clean(TypePackId tp)
{
LUAU_ASSERT(isDirty(tp));
return anyTypePack;
}
bool Anyification::ignoreChildren(TypeId ty)
{
2023-01-03 17:33:19 +00:00
if (FFlag::LuauClassTypeVarsInSubstitution && get<ClassType>(ty))
2022-08-18 22:04:33 +01:00
return true;
return ty->persistent;
}
bool Anyification::ignoreChildren(TypePackId ty)
{
return ty->persistent;
}
2022-09-02 00:00:14 +01:00
} // namespace Luau