5 #ifndef V8_AST_VARIABLES_H_ 6 #define V8_AST_VARIABLES_H_ 8 #include "src/ast/ast-value-factory.h" 9 #include "src/globals.h" 10 #include "src/zone/zone.h" 22 VariableKind kind, InitializationFlag initialization_flag,
23 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned)
26 local_if_not_shadowed_(
nullptr),
29 initializer_position_(kNoSourcePosition),
30 bit_field_(MaybeAssignedFlagField::encode(maybe_assigned_flag) |
31 InitializationFlagField::encode(initialization_flag) |
32 VariableModeField::encode(mode) |
33 IsUsedField::encode(
false) |
34 ForceContextAllocationField::encode(
false) |
35 ForceHoleInitializationField::encode(
false) |
36 LocationField::encode(VariableLocation::UNALLOCATED) |
37 VariableKindField::encode(kind)) {
39 DCHECK(!(mode == VariableMode::kVar &&
40 initialization_flag == kNeedsInitialization));
49 Scope* scope()
const {
return scope_; }
53 void set_scope(
Scope* scope) { scope_ = scope; }
57 VariableMode mode()
const {
return VariableModeField::decode(bit_field_); }
58 bool has_forced_context_allocation()
const {
59 return ForceContextAllocationField::decode(bit_field_);
61 void ForceContextAllocation() {
62 DCHECK(IsUnallocated() || IsContextSlot() ||
63 location() == VariableLocation::MODULE);
64 bit_field_ = ForceContextAllocationField::update(bit_field_,
true);
66 bool is_used() {
return IsUsedField::decode(bit_field_); }
67 void set_is_used() { bit_field_ = IsUsedField::update(bit_field_,
true); }
68 MaybeAssignedFlag maybe_assigned()
const {
69 return MaybeAssignedFlagField::decode(bit_field_);
71 void set_maybe_assigned() {
72 bit_field_ = MaybeAssignedFlagField::update(bit_field_, kMaybeAssigned);
75 int initializer_position() {
return initializer_position_; }
76 void set_initializer_position(
int pos) { initializer_position_ = pos; }
78 bool IsUnallocated()
const {
79 return location() == VariableLocation::UNALLOCATED;
81 bool IsParameter()
const {
return location() == VariableLocation::PARAMETER; }
82 bool IsStackLocal()
const {
return location() == VariableLocation::LOCAL; }
83 bool IsStackAllocated()
const {
return IsParameter() || IsStackLocal(); }
84 bool IsContextSlot()
const {
return location() == VariableLocation::CONTEXT; }
85 bool IsLookupSlot()
const {
return location() == VariableLocation::LOOKUP; }
86 bool IsGlobalObjectProperty()
const;
88 bool is_dynamic()
const {
return IsDynamicVariableMode(mode()); }
94 InitializationFlag initialization_flag()
const {
95 return InitializationFlagField::decode(bit_field_);
100 bool binding_needs_init()
const {
101 DCHECK_IMPLIES(initialization_flag() == kNeedsInitialization,
102 IsLexicalVariableMode(mode()));
103 DCHECK_IMPLIES(ForceHoleInitializationField::decode(bit_field_),
104 initialization_flag() == kNeedsInitialization);
108 if (ForceHoleInitializationField::decode(bit_field_))
return true;
115 if (IsStackAllocated())
return false;
118 return initialization_flag() == kNeedsInitialization;
124 void ForceHoleInitialization() {
125 DCHECK_EQ(kNeedsInitialization, initialization_flag());
126 DCHECK(IsLexicalVariableMode(mode()));
127 bit_field_ = ForceHoleInitializationField::update(bit_field_,
true);
130 bool throw_on_const_assignment(LanguageMode language_mode)
const {
131 return kind() != SLOPPY_FUNCTION_NAME_VARIABLE || is_strict(language_mode);
134 bool is_this()
const {
return kind() == THIS_VARIABLE; }
135 bool is_sloppy_function_name()
const {
136 return kind() == SLOPPY_FUNCTION_NAME_VARIABLE;
139 Variable* local_if_not_shadowed()
const {
140 DCHECK(mode() == VariableMode::kDynamicLocal &&
141 local_if_not_shadowed_ !=
nullptr);
142 return local_if_not_shadowed_;
145 void set_local_if_not_shadowed(
Variable* local) {
146 local_if_not_shadowed_ = local;
149 VariableLocation location()
const {
150 return LocationField::decode(bit_field_);
152 VariableKind kind()
const {
return VariableKindField::decode(bit_field_); }
154 int index()
const {
return index_; }
156 bool IsReceiver()
const {
157 DCHECK(IsParameter());
162 bool IsExport()
const {
163 DCHECK_EQ(location(), VariableLocation::MODULE);
164 DCHECK_NE(index(), 0);
168 void AllocateTo(VariableLocation location,
int index) {
169 DCHECK(IsUnallocated() ||
170 (this->location() == location && this->index() == index));
171 DCHECK_IMPLIES(location == VariableLocation::MODULE, index != 0);
172 bit_field_ = LocationField::update(bit_field_, location);
173 DCHECK_EQ(location, this->location());
177 static InitializationFlag DefaultInitializationFlag(VariableMode mode) {
178 DCHECK(IsDeclaredVariableMode(mode));
179 return mode == VariableMode::kVar ? kCreatedInitialized
180 : kNeedsInitialization;
196 int initializer_position_;
199 class VariableModeField :
public BitField16<VariableMode, 0, 3> {};
200 class VariableKindField
201 :
public BitField16<VariableKind, VariableModeField::kNext, 3> {};
203 :
public BitField16<VariableLocation, VariableKindField::kNext, 3> {};
204 class ForceContextAllocationField
205 :
public BitField16<bool, LocationField::kNext, 1> {};
207 :
public BitField16<bool, ForceContextAllocationField::kNext, 1> {};
208 class InitializationFlagField
209 :
public BitField16<InitializationFlag, IsUsedField::kNext, 1> {};
210 class ForceHoleInitializationField
211 :
public BitField16<bool, InitializationFlagField::kNext, 1> {};
212 class MaybeAssignedFlagField
214 ForceHoleInitializationField::kNext, 1> {};
215 Variable** next() {
return &next_; }
222 #endif // V8_AST_VARIABLES_H_