Commit 84bbdc76 authored by adamk's avatar adamk Committed by Commit bot

[modules] Move MODULE variable back to Scopes, before resolution

Unlike other variable allocation logic, MODULE allocation does
not depend on resolution. So in order to give hole check elimination
(which runs during resolution) access to the information "is this
variable an import", simply allocate all modules variables prior
to resolution.

BUG=v8:1569

Review-Url: https://codereview.chromium.org/2458653002
Cr-Commit-Position: refs/heads/master@{#40621}
parent 2c38023b
...@@ -1080,6 +1080,10 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith( ...@@ -1080,6 +1080,10 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith(
} }
void DeclarationScope::AllocateVariables(ParseInfo* info, AnalyzeMode mode) { void DeclarationScope::AllocateVariables(ParseInfo* info, AnalyzeMode mode) {
// Module variables must be allocated before variable resolution
// to ensure that AccessNeedsHoleCheck() can detect import variables.
if (is_module_scope()) AsModuleScope()->AllocateModuleVariables();
ResolveVariablesRecursively(info); ResolveVariablesRecursively(info);
AllocateVariablesRecursively(); AllocateVariablesRecursively();
...@@ -1918,7 +1922,12 @@ void DeclarationScope::AllocateLocals() { ...@@ -1918,7 +1922,12 @@ void DeclarationScope::AllocateLocals() {
} }
} }
void ModuleScope::AllocateModuleExports() { void ModuleScope::AllocateModuleVariables() {
for (const auto& it : module()->regular_imports()) {
Variable* var = LookupLocal(it.first);
var->AllocateTo(VariableLocation::MODULE, Variable::kModuleImportIndex);
}
for (const auto& it : module()->regular_exports()) { for (const auto& it : module()->regular_exports()) {
Variable* var = LookupLocal(it.first); Variable* var = LookupLocal(it.first);
var->AllocateTo(VariableLocation::MODULE, Variable::kModuleExportIndex); var->AllocateTo(VariableLocation::MODULE, Variable::kModuleExportIndex);
...@@ -1944,9 +1953,7 @@ void Scope::AllocateVariablesRecursively() { ...@@ -1944,9 +1953,7 @@ void Scope::AllocateVariablesRecursively() {
// Allocate variables for this scope. // Allocate variables for this scope.
// Parameters must be allocated first, if any. // Parameters must be allocated first, if any.
if (is_declaration_scope()) { if (is_declaration_scope()) {
if (is_module_scope()) { if (is_function_scope()) {
AsModuleScope()->AllocateModuleExports();
} else if (is_function_scope()) {
AsDeclarationScope()->AllocateParameterLocals(); AsDeclarationScope()->AllocateParameterLocals();
} }
AsDeclarationScope()->AllocateReceiver(); AsDeclarationScope()->AllocateReceiver();
......
...@@ -862,9 +862,9 @@ class ModuleScope final : public DeclarationScope { ...@@ -862,9 +862,9 @@ class ModuleScope final : public DeclarationScope {
return module_descriptor_; return module_descriptor_;
} }
// Set MODULE as VariableLocation for all variables that will live in this // Set MODULE as VariableLocation for all variables that will live in a
// module's export table. Imports are allocated by the parser. // module's export table.
void AllocateModuleExports(); void AllocateModuleVariables();
private: private:
ModuleDescriptor* module_descriptor_; ModuleDescriptor* module_descriptor_;
......
...@@ -1154,7 +1154,8 @@ ZoneList<const Parser::NamedImport*>* Parser::ParseNamedImports( ...@@ -1154,7 +1154,8 @@ ZoneList<const Parser::NamedImport*>* Parser::ParseNamedImports(
return nullptr; return nullptr;
} }
DeclareModuleImport(local_name, position(), CHECK_OK); DeclareVariable(local_name, CONST, kNeedsInitialization, position(),
CHECK_OK);
NamedImport* import = NamedImport* import =
new (zone()) NamedImport(import_name, local_name, location); new (zone()) NamedImport(import_name, local_name, location);
...@@ -1204,7 +1205,8 @@ void Parser::ParseImportDeclaration(bool* ok) { ...@@ -1204,7 +1205,8 @@ void Parser::ParseImportDeclaration(bool* ok) {
import_default_binding = import_default_binding =
ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID);
import_default_binding_loc = scanner()->location(); import_default_binding_loc = scanner()->location();
DeclareModuleImport(import_default_binding, pos, CHECK_OK_VOID); DeclareVariable(import_default_binding, CONST, kNeedsInitialization, pos,
CHECK_OK_VOID);
} }
// Parse NameSpaceImport or NamedImports if present. // Parse NameSpaceImport or NamedImports if present.
...@@ -1481,21 +1483,6 @@ Declaration* Parser::DeclareVariable(const AstRawString* name, ...@@ -1481,21 +1483,6 @@ Declaration* Parser::DeclareVariable(const AstRawString* name,
return declaration; return declaration;
} }
Declaration* Parser::DeclareModuleImport(const AstRawString* name, int pos,
bool* ok) {
DCHECK_EQ(MODULE_SCOPE, scope()->scope_type());
Declaration* decl =
DeclareVariable(name, CONST, kNeedsInitialization, pos, CHECK_OK);
// Allocate imports eagerly as hole check elimination logic in scope
// analisys depends on identifying imports.
// TODO(adamk): It's weird to allocate imports long before everything
// else. We should find a different way of filtering out imports
// during hole check elimination.
decl->proxy()->var()->AllocateTo(VariableLocation::MODULE,
Variable::kModuleImportIndex);
return decl;
}
Variable* Parser::Declare(Declaration* declaration, Variable* Parser::Declare(Declaration* declaration,
DeclarationDescriptor::Kind declaration_kind, DeclarationDescriptor::Kind declaration_kind,
VariableMode mode, InitializationFlag init, bool* ok, VariableMode mode, InitializationFlag init, bool* ok,
......
...@@ -474,7 +474,6 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) { ...@@ -474,7 +474,6 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
int pos, bool* ok); int pos, bool* ok);
Declaration* DeclareVariable(const AstRawString* name, VariableMode mode, Declaration* DeclareVariable(const AstRawString* name, VariableMode mode,
InitializationFlag init, int pos, bool* ok); InitializationFlag init, int pos, bool* ok);
Declaration* DeclareModuleImport(const AstRawString* name, int pos, bool* ok);
bool TargetStackContainsLabel(const AstRawString* label); bool TargetStackContainsLabel(const AstRawString* label);
BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok); BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment