| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_AST_VARIABLES_H_ | 5 #ifndef V8_AST_VARIABLES_H_ |
| 6 #define V8_AST_VARIABLES_H_ | 6 #define V8_AST_VARIABLES_H_ |
| 7 | 7 |
| 8 #include "src/ast/ast-value-factory.h" | 8 #include "src/ast/ast-value-factory.h" |
| 9 #include "src/globals.h" |
| 9 #include "src/zone.h" | 10 #include "src/zone.h" |
| 10 | 11 |
| 11 namespace v8 { | 12 namespace v8 { |
| 12 namespace internal { | 13 namespace internal { |
| 13 | 14 |
| 14 // The AST refers to variables via VariableProxies - placeholders for the actual | 15 // The AST refers to variables via VariableProxies - placeholders for the actual |
| 15 // variables. Variables themselves are never directly referred to from the AST, | 16 // variables. Variables themselves are never directly referred to from the AST, |
| 16 // they are maintained by scopes, and referred to from VariableProxies and Slots | 17 // they are maintained by scopes, and referred to from VariableProxies and Slots |
| 17 // after binding and variable allocation. | 18 // after binding and variable allocation. |
| 18 class Variable final : public ZoneObject { | 19 class Variable final : public ZoneObject { |
| 19 public: | 20 public: |
| 20 enum Kind : uint8_t { | 21 Variable(Scope* scope, const AstRawString* name, VariableMode mode, |
| 21 NORMAL, | 22 VariableKind kind, InitializationFlag initialization_flag, |
| 22 FUNCTION, | |
| 23 THIS, | |
| 24 ARGUMENTS, | |
| 25 SLOPPY_FUNCTION_NAME, | |
| 26 kLastKind = SLOPPY_FUNCTION_NAME | |
| 27 }; | |
| 28 | |
| 29 Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind, | |
| 30 InitializationFlag initialization_flag, | |
| 31 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); | 23 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); |
| 32 | 24 |
| 33 // Printing support | |
| 34 static const char* Mode2String(VariableMode mode); | |
| 35 | |
| 36 // The source code for an eval() call may refer to a variable that is | 25 // The source code for an eval() call may refer to a variable that is |
| 37 // in an outer scope about which we don't know anything (it may not | 26 // in an outer scope about which we don't know anything (it may not |
| 38 // be the script scope). scope() is NULL in that case. Currently the | 27 // be the script scope). scope() is NULL in that case. Currently the |
| 39 // scope is only used to follow the context chain length. | 28 // scope is only used to follow the context chain length. |
| 40 Scope* scope() const { return scope_; } | 29 Scope* scope() const { return scope_; } |
| 41 | 30 |
| 42 // This is for adjusting the scope of temporaries used when desugaring | 31 // This is for adjusting the scope of temporaries used when desugaring |
| 43 // parameter initializers. | 32 // parameter initializers. |
| 44 void set_scope(Scope* scope) { scope_ = scope; } | 33 void set_scope(Scope* scope) { scope_ = scope; } |
| 45 | 34 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 bool IsGlobalObjectProperty() const; | 66 bool IsGlobalObjectProperty() const; |
| 78 bool IsStaticGlobalObjectProperty() const; | 67 bool IsStaticGlobalObjectProperty() const; |
| 79 | 68 |
| 80 bool is_dynamic() const { return IsDynamicVariableMode(mode()); } | 69 bool is_dynamic() const { return IsDynamicVariableMode(mode()); } |
| 81 bool binding_needs_init() const { | 70 bool binding_needs_init() const { |
| 82 DCHECK(initialization_flag() != kNeedsInitialization || | 71 DCHECK(initialization_flag() != kNeedsInitialization || |
| 83 IsLexicalVariableMode(mode())); | 72 IsLexicalVariableMode(mode())); |
| 84 return initialization_flag() == kNeedsInitialization; | 73 return initialization_flag() == kNeedsInitialization; |
| 85 } | 74 } |
| 86 bool throw_on_const_assignment(LanguageMode language_mode) const { | 75 bool throw_on_const_assignment(LanguageMode language_mode) const { |
| 87 return kind() != SLOPPY_FUNCTION_NAME || is_strict(language_mode); | 76 return kind() != SLOPPY_FUNCTION_NAME_VARIABLE || is_strict(language_mode); |
| 88 } | 77 } |
| 89 | 78 |
| 90 bool is_function() const { return kind() == FUNCTION; } | 79 bool is_function() const { return kind() == FUNCTION_VARIABLE; } |
| 91 bool is_this() const { return kind() == THIS; } | 80 bool is_this() const { return kind() == THIS_VARIABLE; } |
| 92 bool is_arguments() const { return kind() == ARGUMENTS; } | 81 bool is_arguments() const { return kind() == ARGUMENTS_VARIABLE; } |
| 93 bool is_sloppy_function_name() const { | 82 bool is_sloppy_function_name() const { |
| 94 return kind() == SLOPPY_FUNCTION_NAME; | 83 return kind() == SLOPPY_FUNCTION_NAME_VARIABLE; |
| 95 } | 84 } |
| 96 | 85 |
| 97 Variable* local_if_not_shadowed() const { | 86 Variable* local_if_not_shadowed() const { |
| 98 DCHECK(mode() == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); | 87 DCHECK(mode() == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
| 99 return local_if_not_shadowed_; | 88 return local_if_not_shadowed_; |
| 100 } | 89 } |
| 101 | 90 |
| 102 void set_local_if_not_shadowed(Variable* local) { | 91 void set_local_if_not_shadowed(Variable* local) { |
| 103 local_if_not_shadowed_ = local; | 92 local_if_not_shadowed_ = local; |
| 104 } | 93 } |
| 105 | 94 |
| 106 VariableLocation location() const { | 95 VariableLocation location() const { |
| 107 return LocationField::decode(bit_field_); | 96 return LocationField::decode(bit_field_); |
| 108 } | 97 } |
| 109 Kind kind() const { return KindField::decode(bit_field_); } | 98 VariableKind kind() const { return VariableKindField::decode(bit_field_); } |
| 110 InitializationFlag initialization_flag() const { | 99 InitializationFlag initialization_flag() const { |
| 111 return InitializationFlagField::decode(bit_field_); | 100 return InitializationFlagField::decode(bit_field_); |
| 112 } | 101 } |
| 113 | 102 |
| 114 int index() const { return index_; } | 103 int index() const { return index_; } |
| 115 | 104 |
| 116 void AllocateTo(VariableLocation location, int index) { | 105 void AllocateTo(VariableLocation location, int index) { |
| 117 DCHECK(IsUnallocated() || | 106 DCHECK(IsUnallocated() || |
| 118 (this->location() == location && this->index() == index)); | 107 (this->location() == location && this->index() == index)); |
| 119 bit_field_ = LocationField::update(bit_field_, location); | 108 bit_field_ = LocationField::update(bit_field_, location); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 133 // If this field is set, this variable references the stored locally bound | 122 // If this field is set, this variable references the stored locally bound |
| 134 // variable, but it might be shadowed by variable bindings introduced by | 123 // variable, but it might be shadowed by variable bindings introduced by |
| 135 // sloppy 'eval' calls between the reference scope (inclusive) and the | 124 // sloppy 'eval' calls between the reference scope (inclusive) and the |
| 136 // binding scope (exclusive). | 125 // binding scope (exclusive). |
| 137 Variable* local_if_not_shadowed_; | 126 Variable* local_if_not_shadowed_; |
| 138 int index_; | 127 int index_; |
| 139 int initializer_position_; | 128 int initializer_position_; |
| 140 uint16_t bit_field_; | 129 uint16_t bit_field_; |
| 141 | 130 |
| 142 class VariableModeField : public BitField16<VariableMode, 0, 3> {}; | 131 class VariableModeField : public BitField16<VariableMode, 0, 3> {}; |
| 143 class KindField : public BitField16<Kind, VariableModeField::kNext, 3> {}; | 132 class VariableKindField |
| 133 : public BitField16<VariableKind, VariableModeField::kNext, 3> {}; |
| 144 class LocationField | 134 class LocationField |
| 145 : public BitField16<VariableLocation, KindField::kNext, 3> {}; | 135 : public BitField16<VariableLocation, VariableKindField::kNext, 3> {}; |
| 146 class ForceContextAllocationField | 136 class ForceContextAllocationField |
| 147 : public BitField16<bool, LocationField::kNext, 1> {}; | 137 : public BitField16<bool, LocationField::kNext, 1> {}; |
| 148 class IsUsedField | 138 class IsUsedField |
| 149 : public BitField16<bool, ForceContextAllocationField::kNext, 1> {}; | 139 : public BitField16<bool, ForceContextAllocationField::kNext, 1> {}; |
| 150 class InitializationFlagField | 140 class InitializationFlagField |
| 151 : public BitField16<InitializationFlag, IsUsedField::kNext, 2> {}; | 141 : public BitField16<InitializationFlag, IsUsedField::kNext, 2> {}; |
| 152 class MaybeAssignedFlagField | 142 class MaybeAssignedFlagField |
| 153 : public BitField16<MaybeAssignedFlag, InitializationFlagField::kNext, | 143 : public BitField16<MaybeAssignedFlag, InitializationFlagField::kNext, |
| 154 2> {}; | 144 2> {}; |
| 155 }; | 145 }; |
| 156 } // namespace internal | 146 } // namespace internal |
| 157 } // namespace v8 | 147 } // namespace v8 |
| 158 | 148 |
| 159 #endif // V8_AST_VARIABLES_H_ | 149 #endif // V8_AST_VARIABLES_H_ |
| OLD | NEW |