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 3 // found in the LICENSE file. 4 5 #ifndef V8_AST_VARIABLES_H_ 6 #define V8_AST_VARIABLES_H_ 7 8 #include "src/ast/ast-value-factory.h" 9 #include "src/zone.h" 10 11 namespace v8 { 12 namespace internal { 13 14 // The AST refers to variables via VariableProxies - placeholders for the actual 15 // 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 // after binding and variable allocation. 18 19 class ClassVariable; 20 21 class Variable: public ZoneObject { 22 public: 23 enum Kind { NORMAL, FUNCTION, CLASS, THIS, ARGUMENTS }; 24 25 Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind, 26 InitializationFlag initialization_flag, 27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); 28 ~Variable()29 virtual ~Variable() {} 30 31 // Printing support 32 static const char* Mode2String(VariableMode mode); 33 34 // The source code for an eval() call may refer to a variable that is 35 // in an outer scope about which we don't know anything (it may not 36 // be the script scope). scope() is NULL in that case. Currently the 37 // scope is only used to follow the context chain length. scope()38 Scope* scope() const { return scope_; } 39 40 // This is for adjusting the scope of temporaries used when desugaring 41 // parameter initializers. set_scope(Scope * scope)42 void set_scope(Scope* scope) { scope_ = scope; } 43 name()44 Handle<String> name() const { return name_->string(); } raw_name()45 const AstRawString* raw_name() const { return name_; } mode()46 VariableMode mode() const { return mode_; } has_forced_context_allocation()47 bool has_forced_context_allocation() const { 48 return force_context_allocation_; 49 } ForceContextAllocation()50 void ForceContextAllocation() { 51 force_context_allocation_ = true; 52 } is_used()53 bool is_used() { return is_used_; } set_is_used()54 void set_is_used() { is_used_ = true; } maybe_assigned()55 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; } set_maybe_assigned()56 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; } 57 initializer_position()58 int initializer_position() { return initializer_position_; } set_initializer_position(int pos)59 void set_initializer_position(int pos) { initializer_position_ = pos; } 60 IsVariable(Handle<String> n)61 bool IsVariable(Handle<String> n) const { 62 return !is_this() && name().is_identical_to(n); 63 } 64 IsUnallocated()65 bool IsUnallocated() const { 66 return location_ == VariableLocation::UNALLOCATED; 67 } IsParameter()68 bool IsParameter() const { return location_ == VariableLocation::PARAMETER; } IsStackLocal()69 bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; } IsStackAllocated()70 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } IsContextSlot()71 bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; } IsGlobalSlot()72 bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; } IsUnallocatedOrGlobalSlot()73 bool IsUnallocatedOrGlobalSlot() const { 74 return IsUnallocated() || IsGlobalSlot(); 75 } IsLookupSlot()76 bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; } 77 bool IsGlobalObjectProperty() const; 78 bool IsStaticGlobalObjectProperty() const; 79 is_dynamic()80 bool is_dynamic() const { return IsDynamicVariableMode(mode_); } is_const_mode()81 bool is_const_mode() const { return IsImmutableVariableMode(mode_); } binding_needs_init()82 bool binding_needs_init() const { 83 return initialization_flag_ == kNeedsInitialization; 84 } 85 is_function()86 bool is_function() const { return kind_ == FUNCTION; } is_class()87 bool is_class() const { return kind_ == CLASS; } is_this()88 bool is_this() const { return kind_ == THIS; } is_arguments()89 bool is_arguments() const { return kind_ == ARGUMENTS; } 90 91 // For script scopes, the "this" binding is provided by a ScriptContext added 92 // to the global's ScriptContextTable. This binding might not statically 93 // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL. However 94 // any variable named "this" does indeed refer to a Variable::THIS binding; 95 // the grammar ensures this to be the case. So wherever a "this" binding 96 // might be provided by the global, use HasThisName instead of is_this(). HasThisName(Isolate * isolate)97 bool HasThisName(Isolate* isolate) const { 98 return is_this() || *name() == *isolate->factory()->this_string(); 99 } 100 AsClassVariable()101 ClassVariable* AsClassVariable() { 102 DCHECK(is_class()); 103 return reinterpret_cast<ClassVariable*>(this); 104 } 105 106 // True if the variable is named eval and not known to be shadowed. is_possibly_eval(Isolate * isolate)107 bool is_possibly_eval(Isolate* isolate) const { 108 return IsVariable(isolate->factory()->eval_string()); 109 } 110 local_if_not_shadowed()111 Variable* local_if_not_shadowed() const { 112 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); 113 return local_if_not_shadowed_; 114 } 115 set_local_if_not_shadowed(Variable * local)116 void set_local_if_not_shadowed(Variable* local) { 117 local_if_not_shadowed_ = local; 118 } 119 location()120 VariableLocation location() const { return location_; } index()121 int index() const { return index_; } initialization_flag()122 InitializationFlag initialization_flag() const { 123 return initialization_flag_; 124 } 125 AllocateTo(VariableLocation location,int index)126 void AllocateTo(VariableLocation location, int index) { 127 location_ = location; 128 index_ = index; 129 } 130 SetFromEval()131 void SetFromEval() { is_from_eval_ = true; } 132 133 static int CompareIndex(Variable* const* v, Variable* const* w); 134 RecordStrongModeReference(int start_position,int end_position)135 void RecordStrongModeReference(int start_position, int end_position) { 136 // Record the earliest reference to the variable. Used in error messages for 137 // strong mode references to undeclared variables. 138 if (has_strong_mode_reference_ && 139 strong_mode_reference_start_position_ < start_position) 140 return; 141 has_strong_mode_reference_ = true; 142 strong_mode_reference_start_position_ = start_position; 143 strong_mode_reference_end_position_ = end_position; 144 } 145 has_strong_mode_reference()146 bool has_strong_mode_reference() const { return has_strong_mode_reference_; } strong_mode_reference_start_position()147 int strong_mode_reference_start_position() const { 148 return strong_mode_reference_start_position_; 149 } strong_mode_reference_end_position()150 int strong_mode_reference_end_position() const { 151 return strong_mode_reference_end_position_; 152 } DeclarationPropertyAttributes()153 PropertyAttributes DeclarationPropertyAttributes() const { 154 int property_attributes = NONE; 155 if (IsImmutableVariableMode(mode_)) { 156 property_attributes |= READ_ONLY; 157 } 158 if (is_from_eval_) { 159 property_attributes |= EVAL_DECLARED; 160 } 161 return static_cast<PropertyAttributes>(property_attributes); 162 } 163 164 private: 165 Scope* scope_; 166 const AstRawString* name_; 167 VariableMode mode_; 168 Kind kind_; 169 VariableLocation location_; 170 int index_; 171 int initializer_position_; 172 // Tracks whether the variable is bound to a VariableProxy which is in strong 173 // mode, and if yes, the source location of the reference. 174 bool has_strong_mode_reference_; 175 int strong_mode_reference_start_position_; 176 int strong_mode_reference_end_position_; 177 178 // If this field is set, this variable references the stored locally bound 179 // variable, but it might be shadowed by variable bindings introduced by 180 // sloppy 'eval' calls between the reference scope (inclusive) and the 181 // binding scope (exclusive). 182 Variable* local_if_not_shadowed_; 183 184 // True if this variable is introduced by a sloppy eval 185 bool is_from_eval_; 186 187 // Usage info. 188 bool force_context_allocation_; // set by variable resolver 189 bool is_used_; 190 InitializationFlag initialization_flag_; 191 MaybeAssignedFlag maybe_assigned_; 192 }; 193 194 class ClassVariable : public Variable { 195 public: 196 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode, 197 InitializationFlag initialization_flag, 198 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, 199 int declaration_group_start = -1) Variable(scope,name,mode,Variable::CLASS,initialization_flag,maybe_assigned_flag)200 : Variable(scope, name, mode, Variable::CLASS, initialization_flag, 201 maybe_assigned_flag), 202 declaration_group_start_(declaration_group_start) {} 203 declaration_group_start()204 int declaration_group_start() const { return declaration_group_start_; } set_declaration_group_start(int declaration_group_start)205 void set_declaration_group_start(int declaration_group_start) { 206 declaration_group_start_ = declaration_group_start; 207 } 208 209 private: 210 // For classes we keep track of consecutive groups of delcarations. They are 211 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement 212 // checks for functions too. 213 int declaration_group_start_; 214 }; 215 } // namespace internal 216 } // namespace v8 217 218 #endif // V8_AST_VARIABLES_H_ 219