| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_SCOPES_H_ | 5 #ifndef VM_SCOPES_H_ |
| 6 #define VM_SCOPES_H_ | 6 #define VM_SCOPES_H_ |
| 7 | 7 |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "platform/globals.h" | 9 #include "platform/globals.h" |
| 10 #include "vm/allocation.h" | 10 #include "vm/allocation.h" |
| 11 #include "vm/growable_array.h" | 11 #include "vm/growable_array.h" |
| 12 #include "vm/object.h" | 12 #include "vm/object.h" |
| 13 #include "vm/raw_object.h" | 13 #include "vm/raw_object.h" |
| 14 #include "vm/symbols.h" | 14 #include "vm/symbols.h" |
| 15 #include "vm/token.h" | 15 #include "vm/token.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 class LocalScope; | 19 class LocalScope; |
| 20 | 20 |
| 21 | 21 |
| 22 class LocalVariable : public ZoneAllocated { | 22 class LocalVariable : public ZoneAllocated { |
| 23 public: | 23 public: |
| 24 LocalVariable(intptr_t token_pos, | 24 LocalVariable(TokenDescriptor token_pos, |
| 25 const String& name, | 25 const String& name, |
| 26 const AbstractType& type) | 26 const AbstractType& type) |
| 27 : token_pos_(token_pos), | 27 : token_pos_(token_pos), |
| 28 name_(name), | 28 name_(name), |
| 29 owner_(NULL), | 29 owner_(NULL), |
| 30 type_(type), | 30 type_(type), |
| 31 const_value_(NULL), | 31 const_value_(NULL), |
| 32 is_final_(false), | 32 is_final_(false), |
| 33 is_captured_(false), | 33 is_captured_(false), |
| 34 is_invisible_(false), | 34 is_invisible_(false), |
| 35 is_captured_parameter_(false), | 35 is_captured_parameter_(false), |
| 36 index_(LocalVariable::kUninitializedIndex) { | 36 index_(LocalVariable::kUninitializedIndex) { |
| 37 ASSERT(type.IsZoneHandle() || type.IsReadOnlyHandle()); | 37 ASSERT(type.IsZoneHandle() || type.IsReadOnlyHandle()); |
| 38 ASSERT(type.IsFinalized()); | 38 ASSERT(type.IsFinalized()); |
| 39 ASSERT(name.IsSymbol()); | 39 ASSERT(name.IsSymbol()); |
| 40 } | 40 } |
| 41 | 41 |
| 42 intptr_t token_pos() const { return token_pos_; } | 42 TokenDescriptor token_pos() const { return token_pos_; } |
| 43 const String& name() const { return name_; } | 43 const String& name() const { return name_; } |
| 44 LocalScope* owner() const { return owner_; } | 44 LocalScope* owner() const { return owner_; } |
| 45 void set_owner(LocalScope* owner) { | 45 void set_owner(LocalScope* owner) { |
| 46 ASSERT(owner_ == NULL); | 46 ASSERT(owner_ == NULL); |
| 47 owner_ = owner; | 47 owner_ = owner; |
| 48 } | 48 } |
| 49 | 49 |
| 50 const AbstractType& type() const { return type_; } | 50 const AbstractType& type() const { return type_; } |
| 51 | 51 |
| 52 bool is_final() const { return is_final_; } | 52 bool is_final() const { return is_final_; } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 // Map the frame index to a bit-vector index. Assumes the variable is | 103 // Map the frame index to a bit-vector index. Assumes the variable is |
| 104 // allocated to the frame. | 104 // allocated to the frame. |
| 105 // var_count is the total number of stack-allocated variables including | 105 // var_count is the total number of stack-allocated variables including |
| 106 // all parameters. | 106 // all parameters. |
| 107 int BitIndexIn(intptr_t var_count) const; | 107 int BitIndexIn(intptr_t var_count) const; |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 static const int kUninitializedIndex = INT_MIN; | 110 static const int kUninitializedIndex = INT_MIN; |
| 111 | 111 |
| 112 const intptr_t token_pos_; | 112 const TokenDescriptor token_pos_; |
| 113 const String& name_; | 113 const String& name_; |
| 114 LocalScope* owner_; // Local scope declaring this variable. | 114 LocalScope* owner_; // Local scope declaring this variable. |
| 115 | 115 |
| 116 const AbstractType& type_; // Declaration type of local variable. | 116 const AbstractType& type_; // Declaration type of local variable. |
| 117 | 117 |
| 118 const Instance* const_value_; // NULL or compile-time const value. | 118 const Instance* const_value_; // NULL or compile-time const value. |
| 119 | 119 |
| 120 bool is_final_; // If true, this variable is readonly. | 120 bool is_final_; // If true, this variable is readonly. |
| 121 bool is_captured_; // If true, this variable lives in the context, otherwise | 121 bool is_captured_; // If true, this variable lives in the context, otherwise |
| 122 // in the stack frame. | 122 // in the stack frame. |
| 123 bool is_invisible_; | 123 bool is_invisible_; |
| 124 bool is_captured_parameter_; | 124 bool is_captured_parameter_; |
| 125 int index_; // Allocation index in words relative to frame pointer (if not | 125 int index_; // Allocation index in words relative to frame pointer (if not |
| 126 // captured), or relative to the context pointer (if captured). | 126 // captured), or relative to the context pointer (if captured). |
| 127 | 127 |
| 128 friend class LocalScope; | 128 friend class LocalScope; |
| 129 DISALLOW_COPY_AND_ASSIGN(LocalVariable); | 129 DISALLOW_COPY_AND_ASSIGN(LocalVariable); |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 | 132 |
| 133 class NameReference : public ZoneAllocated { | 133 class NameReference : public ZoneAllocated { |
| 134 public: | 134 public: |
| 135 NameReference(intptr_t token_pos, const String& name) | 135 NameReference(TokenDescriptor token_pos, const String& name) |
| 136 : token_pos_(token_pos), | 136 : token_pos_(token_pos), |
| 137 name_(name) { | 137 name_(name) { |
| 138 ASSERT(name.IsSymbol()); | 138 ASSERT(name.IsSymbol()); |
| 139 } | 139 } |
| 140 const String& name() const { return name_; } | 140 const String& name() const { return name_; } |
| 141 intptr_t token_pos() const { return token_pos_; } | 141 TokenDescriptor token_pos() const { return token_pos_; } |
| 142 void set_token_pos(intptr_t value) { token_pos_ = value; } | 142 void set_token_pos(TokenDescriptor value) { token_pos_ = value; } |
| 143 private: | 143 private: |
| 144 intptr_t token_pos_; | 144 TokenDescriptor token_pos_; |
| 145 const String& name_; | 145 const String& name_; |
| 146 }; | 146 }; |
| 147 | 147 |
| 148 | 148 |
| 149 class SourceLabel : public ZoneAllocated { | 149 class SourceLabel : public ZoneAllocated { |
| 150 public: | 150 public: |
| 151 enum Kind { | 151 enum Kind { |
| 152 kFor, | 152 kFor, |
| 153 kWhile, | 153 kWhile, |
| 154 kDoWhile, | 154 kDoWhile, |
| 155 kSwitch, | 155 kSwitch, |
| 156 kCase, | 156 kCase, |
| 157 kTry, | 157 kTry, |
| 158 kCatch, | 158 kCatch, |
| 159 kForward, | 159 kForward, |
| 160 kStatement // Any statement other than the above | 160 kStatement // Any statement other than the above |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 SourceLabel(intptr_t token_pos, const String& name, Kind kind) | 163 SourceLabel(TokenDescriptor token_pos, const String& name, Kind kind) |
| 164 : token_pos_(token_pos), | 164 : token_pos_(token_pos), |
| 165 name_(name), | 165 name_(name), |
| 166 owner_(NULL), | 166 owner_(NULL), |
| 167 kind_(kind) { | 167 kind_(kind) { |
| 168 ASSERT(name.IsSymbol()); | 168 ASSERT(name.IsSymbol()); |
| 169 } | 169 } |
| 170 | 170 |
| 171 static SourceLabel* New(intptr_t token_pos, String* name, Kind kind) { | 171 static SourceLabel* New(TokenDescriptor token_pos, String* name, Kind kind) { |
| 172 if (name != NULL) { | 172 if (name != NULL) { |
| 173 return new SourceLabel(token_pos, *name, kind); | 173 return new SourceLabel(token_pos, *name, kind); |
| 174 } else { | 174 } else { |
| 175 return new SourceLabel(token_pos, | 175 return new SourceLabel(token_pos, |
| 176 Symbols::DefaultLabel(), | 176 Symbols::DefaultLabel(), |
| 177 kind); | 177 kind); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 intptr_t token_pos() const { return token_pos_; } | 181 TokenDescriptor token_pos() const { return token_pos_; } |
| 182 const String& name() const { return name_; } | 182 const String& name() const { return name_; } |
| 183 LocalScope* owner() const { return owner_; } | 183 LocalScope* owner() const { return owner_; } |
| 184 void set_owner(LocalScope* owner) { | 184 void set_owner(LocalScope* owner) { |
| 185 ASSERT(owner_ == NULL); | 185 ASSERT(owner_ == NULL); |
| 186 owner_ = owner; | 186 owner_ = owner; |
| 187 } | 187 } |
| 188 | 188 |
| 189 Kind kind() const { return kind_; } | 189 Kind kind() const { return kind_; } |
| 190 | 190 |
| 191 // Returns the function level of the scope in which the label is defined. | 191 // Returns the function level of the scope in which the label is defined. |
| 192 int FunctionLevel() const; | 192 int FunctionLevel() const; |
| 193 | 193 |
| 194 void ResolveForwardReference() { kind_ = kCase; } | 194 void ResolveForwardReference() { kind_ = kCase; } |
| 195 | 195 |
| 196 private: | 196 private: |
| 197 const intptr_t token_pos_; | 197 const TokenDescriptor token_pos_; |
| 198 const String& name_; | 198 const String& name_; |
| 199 LocalScope* owner_; // Local scope declaring this label. | 199 LocalScope* owner_; // Local scope declaring this label. |
| 200 | 200 |
| 201 Kind kind_; | 201 Kind kind_; |
| 202 | 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(SourceLabel); | 203 DISALLOW_COPY_AND_ASSIGN(SourceLabel); |
| 204 }; | 204 }; |
| 205 | 205 |
| 206 | 206 |
| 207 class LocalScope : public ZoneAllocated { | 207 class LocalScope : public ZoneAllocated { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 225 int context_level() const { | 225 int context_level() const { |
| 226 ASSERT(HasContextLevel()); | 226 ASSERT(HasContextLevel()); |
| 227 return context_level_; | 227 return context_level_; |
| 228 } | 228 } |
| 229 void set_context_level(int context_level) { | 229 void set_context_level(int context_level) { |
| 230 ASSERT(!HasContextLevel()); | 230 ASSERT(!HasContextLevel()); |
| 231 ASSERT(context_level != kUnitializedContextLevel); | 231 ASSERT(context_level != kUnitializedContextLevel); |
| 232 context_level_ = context_level; | 232 context_level_ = context_level; |
| 233 } | 233 } |
| 234 | 234 |
| 235 intptr_t begin_token_pos() const { return begin_token_pos_; } | 235 TokenDescriptor begin_token_pos() const { return begin_token_pos_; } |
| 236 void set_begin_token_pos(intptr_t value) { begin_token_pos_ = value; } | 236 void set_begin_token_pos(TokenDescriptor value) { begin_token_pos_ = value; } |
| 237 | 237 |
| 238 intptr_t end_token_pos() const { return end_token_pos_; } | 238 TokenDescriptor end_token_pos() const { return end_token_pos_; } |
| 239 void set_end_token_pos(intptr_t value) { end_token_pos_ = value; } | 239 void set_end_token_pos(TokenDescriptor value) { end_token_pos_ = value; } |
| 240 | 240 |
| 241 // The number of variables allocated in the context and belonging to this | 241 // The number of variables allocated in the context and belonging to this |
| 242 // scope and to its children at the same loop level. | 242 // scope and to its children at the same loop level. |
| 243 int num_context_variables() const { return num_context_variables_; } | 243 int num_context_variables() const { return num_context_variables_; } |
| 244 | 244 |
| 245 // Add a variable to the scope. Returns false if a variable with the | 245 // Add a variable to the scope. Returns false if a variable with the |
| 246 // same name is already present. | 246 // same name is already present. |
| 247 bool AddVariable(LocalVariable* variable); | 247 bool AddVariable(LocalVariable* variable); |
| 248 | 248 |
| 249 // Insert a formal parameter variable to the scope at the given position, | 249 // Insert a formal parameter variable to the scope at the given position, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 return variables_[index]; | 295 return variables_[index]; |
| 296 } | 296 } |
| 297 | 297 |
| 298 // Count the captured variables belonging to outer scopes and referenced in | 298 // Count the captured variables belonging to outer scopes and referenced in |
| 299 // this local scope. | 299 // this local scope. |
| 300 int NumCapturedVariables() const; | 300 int NumCapturedVariables() const; |
| 301 | 301 |
| 302 // Add a reference to the given name into this scope and the enclosing | 302 // Add a reference to the given name into this scope and the enclosing |
| 303 // scopes that do not have a local variable declaration for this name | 303 // scopes that do not have a local variable declaration for this name |
| 304 // already. | 304 // already. |
| 305 void AddReferencedName(intptr_t token_pos, const String& name); | 305 void AddReferencedName(TokenDescriptor token_pos, const String& name); |
| 306 intptr_t PreviousReferencePos(const String& name) const; | 306 TokenDescriptor PreviousReferencePos(const String& name) const; |
| 307 | 307 |
| 308 // Allocate both captured and non-captured variables declared in this scope | 308 // Allocate both captured and non-captured variables declared in this scope |
| 309 // and in its children scopes of the same function level. Allocating means | 309 // and in its children scopes of the same function level. Allocating means |
| 310 // assigning a frame slot index or a context slot index. | 310 // assigning a frame slot index or a context slot index. |
| 311 // Parameters to be allocated in the frame must all appear in the top scope | 311 // Parameters to be allocated in the frame must all appear in the top scope |
| 312 // and not in its children (we do not yet handle register parameters). | 312 // and not in its children (we do not yet handle register parameters). |
| 313 // Locals must be listed after parameters in top scope and in its children. | 313 // Locals must be listed after parameters in top scope and in its children. |
| 314 // Two locals in different sibling scopes may share the same frame slot. | 314 // Two locals in different sibling scopes may share the same frame slot. |
| 315 // Return the index of the next available frame slot. | 315 // Return the index of the next available frame slot. |
| 316 int AllocateVariables(int first_parameter_index, | 316 int AllocateVariables(int first_parameter_index, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 NameReference* FindReference(const String& name) const; | 360 NameReference* FindReference(const String& name) const; |
| 361 | 361 |
| 362 static const int kUnitializedContextLevel = INT_MIN; | 362 static const int kUnitializedContextLevel = INT_MIN; |
| 363 LocalScope* parent_; | 363 LocalScope* parent_; |
| 364 LocalScope* child_; | 364 LocalScope* child_; |
| 365 LocalScope* sibling_; | 365 LocalScope* sibling_; |
| 366 int function_level_; // Reflects the nesting level of local functions. | 366 int function_level_; // Reflects the nesting level of local functions. |
| 367 int loop_level_; // Reflects the loop nesting level. | 367 int loop_level_; // Reflects the loop nesting level. |
| 368 int context_level_; // Reflects the level of the runtime context. | 368 int context_level_; // Reflects the level of the runtime context. |
| 369 int num_context_variables_; // Only set if this scope is a context owner. | 369 int num_context_variables_; // Only set if this scope is a context owner. |
| 370 intptr_t begin_token_pos_; // Token index of beginning of scope. | 370 TokenDescriptor begin_token_pos_; // Token index of beginning of scope. |
| 371 intptr_t end_token_pos_; // Token index of end of scope. | 371 TokenDescriptor end_token_pos_; // Token index of end of scope. |
| 372 GrowableArray<LocalVariable*> variables_; | 372 GrowableArray<LocalVariable*> variables_; |
| 373 GrowableArray<SourceLabel*> labels_; | 373 GrowableArray<SourceLabel*> labels_; |
| 374 | 374 |
| 375 // List of names referenced in this scope and its children that | 375 // List of names referenced in this scope and its children that |
| 376 // are not resolved to local variables. | 376 // are not resolved to local variables. |
| 377 GrowableArray<NameReference*> referenced_; | 377 GrowableArray<NameReference*> referenced_; |
| 378 | 378 |
| 379 DISALLOW_COPY_AND_ASSIGN(LocalScope); | 379 DISALLOW_COPY_AND_ASSIGN(LocalScope); |
| 380 }; | 380 }; |
| 381 | 381 |
| 382 } // namespace dart | 382 } // namespace dart |
| 383 | 383 |
| 384 #endif // VM_SCOPES_H_ | 384 #endif // VM_SCOPES_H_ |
| OLD | NEW |