blob: da2d8387f5117c541ea056685e3e4701053c84f4 [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/ast/variables.h"
6
Ben Murdoch014dc512016-03-22 12:00:34 +00007#include "src/ast/scopes.h"
Rubin Xu7bc1b612021-02-16 09:38:50 +00008#include "src/common/globals.h"
9#include "src/objects/objects-inl.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000010
11namespace v8 {
12namespace internal {
13
14// ----------------------------------------------------------------------------
15// Implementation Variable.
16
Rubin Xu7bc1b612021-02-16 09:38:50 +000017Variable::Variable(Variable* other)
18 : scope_(other->scope_),
19 name_(other->name_),
Ben Murdochf3b273f2017-01-17 12:11:28 +000020 local_if_not_shadowed_(nullptr),
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000021 next_(nullptr),
Rubin Xu7bc1b612021-02-16 09:38:50 +000022 index_(other->index_),
23 initializer_position_(other->initializer_position_),
24 bit_field_(other->bit_field_) {}
Ben Murdoch014dc512016-03-22 12:00:34 +000025
26bool Variable::IsGlobalObjectProperty() const {
27 // Temporaries are never global, they must always be allocated in the
28 // activation frame.
Rubin Xu7bc1b612021-02-16 09:38:50 +000029 return (IsDynamicVariableMode(mode()) || mode() == VariableMode::kVar) &&
Ben Murdoch62ed6312017-06-06 11:06:27 +010030 scope_ != nullptr && scope_->is_script_scope();
Ben Murdoch014dc512016-03-22 12:00:34 +000031}
32
Rubin Xu7bc1b612021-02-16 09:38:50 +000033bool Variable::IsReplGlobalLet() const {
34 return scope()->is_repl_mode_scope() && mode() == VariableMode::kLet;
35}
36
37void Variable::RewriteLocationForRepl() {
38 DCHECK(scope_->is_repl_mode_scope());
39
40 if (mode() == VariableMode::kLet) {
41 DCHECK_EQ(location(), VariableLocation::CONTEXT);
42 bit_field_ =
43 LocationField::update(bit_field_, VariableLocation::REPL_GLOBAL);
44 }
45}
46
Ben Murdoch014dc512016-03-22 12:00:34 +000047} // namespace internal
48} // namespace v8