1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_VARIABLES_H_
29 #define V8_VARIABLES_H_
33 namespace v8 { namespace internal {
35 class UseCount BASE_EMBEDDED {
39 // Inform the node of a "use". The weight can be used to indicate
40 // heavier use, for instance if the variable is accessed inside a loop.
41 void RecordRead(int weight);
42 void RecordWrite(int weight);
43 void RecordAccess(int weight); // records a read & write
44 void RecordUses(UseCount* uses);
46 int nreads() const { return nreads_; }
47 int nwrites() const { return nwrites_; }
48 int nuses() const { return nreads_ + nwrites_; }
50 bool is_read() const { return nreads() > 0; }
51 bool is_written() const { return nwrites() > 0; }
52 bool is_used() const { return nuses() > 0; }
64 // Variables and AST expression nodes can track their "type" to enable
65 // optimizations and removal of redundant checks when generating code.
67 class StaticType BASE_EMBEDDED {
74 StaticType() : kind_(UNKNOWN) {}
76 bool Is(Kind kind) const { return kind_ == kind; }
78 bool IsKnown() const { return !Is(UNKNOWN); }
79 bool IsUnknown() const { return Is(UNKNOWN); }
80 bool IsLikelySmi() const { return Is(LIKELY_SMI); }
82 void CopyFrom(StaticType* other) {
86 static const char* Type2String(StaticType* type);
88 // LIKELY_SMI accessors
89 void SetAsLikelySmi() {
93 void SetAsLikelySmiIfUnknown() {
102 DISALLOW_COPY_AND_ASSIGN(StaticType);
106 // The AST refers to variables via VariableProxies - placeholders for the actual
107 // variables. Variables themselves are never directly referred to from the AST,
108 // they are maintained by scopes, and referred to from VariableProxies and Slots
109 // after binding and variable allocation.
111 class Variable: public ZoneObject {
114 // User declared variables:
115 VAR, // declared via 'var', and 'function' declarations
116 CONST, // declared via 'const' declarations
118 // Variables introduced by the compiler:
119 DYNAMIC, // always require dynamic lookup (we don't know the declaration)
120 INTERNAL, // like VAR, but not user-visible (may or may not be in a
122 TEMPORARY // temporary variables (not user-visible), never in a context
126 static const char* Mode2String(Mode mode);
128 // Type testing & conversion
129 Property* AsProperty();
130 Variable* AsVariable();
131 bool IsValidLeftHandSide() { return is_valid_LHS_; }
133 // The source code for an eval() call may refer to a variable that is
134 // in an outer scope about which we don't know anything (it may not
135 // be the global scope). scope() is NULL in that case. Currently the
136 // scope is only used to follow the context chain length.
137 Scope* scope() const { return scope_; }
138 // If this assertion fails it means that some code has tried to
139 // treat the special this variable as an ordinary variable with
141 Handle<String> name() const { return name_; }
142 Mode mode() const { return mode_; }
143 bool is_accessed_from_inner_scope() const {
144 return is_accessed_from_inner_scope_;
146 UseCount* var_uses() { return &var_uses_; }
147 UseCount* obj_uses() { return &obj_uses_; }
149 bool IsVariable(Handle<String> n) {
150 return !is_this() && name().is_identical_to(n);
153 bool is_global() const;
154 bool is_this() const { return is_this_; }
156 Expression* rewrite() const { return rewrite_; }
159 StaticType* type() { return &type_; }
162 Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS,
166 Handle<String> name_;
172 bool is_accessed_from_inner_scope_; // set by variable resolver
173 UseCount var_uses_; // uses of the variable value
174 UseCount obj_uses_; // uses of the object the variable points to
176 // Static type information
180 // rewrite_ is usually a Slot or a Property, but maybe any expression.
181 Expression* rewrite_;
183 friend class VariableProxy;
185 friend class LocalsMap;
186 friend class AstBuildingParser;
190 } } // namespace v8::internal
192 #endif // V8_VARIABLES_H_