mirror of
https://github.com/luau-lang/luau.git
synced 2025-01-07 11:59:11 +00:00
49b0c59eec
Changes: - Support for time tracing for analysis/compiler (not currently exposed through CLI) - Support for type pack arguments in type aliases (#83) - Basic support for require(path) in luau-analyze - Add a lint warning for table.move with 0 index as part of TableOperation lint - Remove last STL dependency from Luau.VM - Minor VS2022 performance tuning Co-authored-by: Rodactor <rodactor@roblox.com>
123 lines
2.7 KiB
C++
123 lines
2.7 KiB
C++
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/Scope.h"
|
|
|
|
namespace Luau
|
|
{
|
|
|
|
Scope::Scope(TypePackId returnType)
|
|
: parent(nullptr)
|
|
, returnType(returnType)
|
|
, level(TypeLevel())
|
|
{
|
|
}
|
|
|
|
Scope::Scope(const ScopePtr& parent, int subLevel)
|
|
: parent(parent)
|
|
, returnType(parent->returnType)
|
|
, level(parent->level.incr())
|
|
{
|
|
level.subLevel = subLevel;
|
|
}
|
|
|
|
std::optional<TypeId> Scope::lookup(const Symbol& name)
|
|
{
|
|
Scope* scope = this;
|
|
|
|
while (scope)
|
|
{
|
|
auto it = scope->bindings.find(name);
|
|
if (it != scope->bindings.end())
|
|
return it->second.typeId;
|
|
|
|
scope = scope->parent.get();
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<TypeFun> Scope::lookupType(const Name& name)
|
|
{
|
|
const Scope* scope = this;
|
|
while (true)
|
|
{
|
|
auto it = scope->exportedTypeBindings.find(name);
|
|
if (it != scope->exportedTypeBindings.end())
|
|
return it->second;
|
|
|
|
it = scope->privateTypeBindings.find(name);
|
|
if (it != scope->privateTypeBindings.end())
|
|
return it->second;
|
|
|
|
if (scope->parent)
|
|
scope = scope->parent.get();
|
|
else
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
std::optional<TypeFun> Scope::lookupImportedType(const Name& moduleAlias, const Name& name)
|
|
{
|
|
const Scope* scope = this;
|
|
while (scope)
|
|
{
|
|
auto it = scope->importedTypeBindings.find(moduleAlias);
|
|
if (it == scope->importedTypeBindings.end())
|
|
{
|
|
scope = scope->parent.get();
|
|
continue;
|
|
}
|
|
|
|
auto it2 = it->second.find(name);
|
|
if (it2 == it->second.end())
|
|
{
|
|
scope = scope->parent.get();
|
|
continue;
|
|
}
|
|
|
|
return it2->second;
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<TypePackId> Scope::lookupPack(const Name& name)
|
|
{
|
|
const Scope* scope = this;
|
|
while (true)
|
|
{
|
|
auto it = scope->privateTypePackBindings.find(name);
|
|
if (it != scope->privateTypePackBindings.end())
|
|
return it->second;
|
|
|
|
if (scope->parent)
|
|
scope = scope->parent.get();
|
|
else
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
std::optional<Binding> Scope::linearSearchForBinding(const std::string& name, bool traverseScopeChain)
|
|
{
|
|
Scope* scope = this;
|
|
|
|
while (scope)
|
|
{
|
|
for (const auto& [n, binding] : scope->bindings)
|
|
{
|
|
if (n.local && n.local->name == name.c_str())
|
|
return binding;
|
|
else if (n.global.value && n.global == name.c_str())
|
|
return binding;
|
|
}
|
|
|
|
scope = scope->parent.get();
|
|
|
|
if (!traverseScopeChain)
|
|
break;
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
} // namespace Luau
|