blob: a189f173b56fa2e2bf0b0a412e301441e1348721 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 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
4// met:
5//
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.
15//
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.
27
28#ifndef V8_AST_VALUE_FACTORY_H_
29#define V8_AST_VALUE_FACTORY_H_
30
31#include "src/api.h"
32#include "src/hashmap.h"
33#include "src/utils.h"
34
35// AstString, AstValue and AstValueFactory are for storing strings and values
36// independent of the V8 heap and internalizing them later. During parsing,
37// AstStrings and AstValues are created and stored outside the heap, in
38// AstValueFactory. After parsing, the strings and values are internalized
39// (moved into the V8 heap).
40namespace v8 {
41namespace internal {
42
43class AstString : public ZoneObject {
44 public:
45 virtual ~AstString() {}
46
47 virtual int length() const = 0;
48 bool IsEmpty() const { return length() == 0; }
49
50 // Puts the string into the V8 heap.
51 virtual void Internalize(Isolate* isolate) = 0;
52
53 // This function can be called after internalizing.
54 V8_INLINE Handle<String> string() const {
55 DCHECK(!string_.is_null());
56 return string_;
57 }
58
59 protected:
60 // This is null until the string is internalized.
61 Handle<String> string_;
62};
63
64
65class AstRawString : public AstString {
66 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040067 int length() const OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 if (is_one_byte_)
69 return literal_bytes_.length();
70 return literal_bytes_.length() / 2;
71 }
72
Emily Bernierd0a1eb72015-03-24 16:35:39 -040073 void Internalize(Isolate* isolate) OVERRIDE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074
75 bool AsArrayIndex(uint32_t* index) const;
76
77 // The string is not null-terminated, use length() to find out the length.
78 const unsigned char* raw_data() const {
79 return literal_bytes_.start();
80 }
81 bool is_one_byte() const { return is_one_byte_; }
82 bool IsOneByteEqualTo(const char* data) const;
83 uint16_t FirstCharacter() const {
84 if (is_one_byte_)
85 return literal_bytes_[0];
86 const uint16_t* c =
87 reinterpret_cast<const uint16_t*>(literal_bytes_.start());
88 return *c;
89 }
90
Emily Bernierd0a1eb72015-03-24 16:35:39 -040091 V8_INLINE bool IsArguments(AstValueFactory* ast_value_factory) const;
92
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 // For storing AstRawStrings in a hash map.
94 uint32_t hash() const {
95 return hash_;
96 }
97 static bool Compare(void* a, void* b);
98
Emily Bernierd0a1eb72015-03-24 16:35:39 -040099 bool operator==(const AstRawString& rhs) const;
100
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 private:
102 friend class AstValueFactory;
103 friend class AstRawStringInternalizationKey;
104
105 AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes,
106 uint32_t hash)
107 : is_one_byte_(is_one_byte), literal_bytes_(literal_bytes), hash_(hash) {}
108
109 AstRawString()
110 : is_one_byte_(true),
111 hash_(0) {}
112
113 bool is_one_byte_;
114
115 // Points to memory owned by Zone.
116 Vector<const byte> literal_bytes_;
117 uint32_t hash_;
118};
119
120
121class AstConsString : public AstString {
122 public:
123 AstConsString(const AstString* left, const AstString* right)
124 : left_(left),
125 right_(right) {}
126
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400127 int length() const OVERRIDE { return left_->length() + right_->length(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400129 void Internalize(Isolate* isolate) OVERRIDE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130
131 private:
132 friend class AstValueFactory;
133
134 const AstString* left_;
135 const AstString* right_;
136};
137
138
139// AstValue is either a string, a number, a string array, a boolean, or a
140// special value (null, undefined, the hole).
141class AstValue : public ZoneObject {
142 public:
143 bool IsString() const {
144 return type_ == STRING;
145 }
146
147 bool IsNumber() const {
148 return type_ == NUMBER || type_ == SMI;
149 }
150
151 const AstRawString* AsString() const {
152 if (type_ == STRING)
153 return string_;
154 UNREACHABLE();
155 return 0;
156 }
157
158 double AsNumber() const {
159 if (type_ == NUMBER)
160 return number_;
161 if (type_ == SMI)
162 return smi_;
163 UNREACHABLE();
164 return 0;
165 }
166
167 bool EqualsString(const AstRawString* string) const {
168 return type_ == STRING && string_ == string;
169 }
170
171 bool IsPropertyName() const;
172
173 bool BooleanValue() const;
174
175 void Internalize(Isolate* isolate);
176
177 // Can be called after Internalize has been called.
178 V8_INLINE Handle<Object> value() const {
179 if (type_ == STRING) {
180 return string_->string();
181 }
182 DCHECK(!value_.is_null());
183 return value_;
184 }
185
186 private:
187 friend class AstValueFactory;
188
189 enum Type {
190 STRING,
191 SYMBOL,
192 NUMBER,
193 SMI,
194 BOOLEAN,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 NULL_TYPE,
196 UNDEFINED,
197 THE_HOLE
198 };
199
200 explicit AstValue(const AstRawString* s) : type_(STRING) { string_ = s; }
201
202 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; }
203
204 explicit AstValue(double n) : type_(NUMBER) { number_ = n; }
205
206 AstValue(Type t, int i) : type_(t) {
207 DCHECK(type_ == SMI);
208 smi_ = i;
209 }
210
211 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; }
212
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 explicit AstValue(Type t) : type_(t) {
214 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE);
215 }
216
217 Type type_;
218
219 // Uninternalized value.
220 union {
221 const AstRawString* string_;
222 double number_;
223 int smi_;
224 bool bool_;
225 ZoneList<const AstRawString*>* strings_;
226 const char* symbol_name_;
227 };
228
229 // Internalized value (empty before internalized).
230 Handle<Object> value_;
231};
232
233
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400234// For generating constants.
235#define STRING_CONSTANTS(F) \
236 F(anonymous_function, "(anonymous function)") \
237 F(arguments, "arguments") \
238 F(constructor, "constructor") \
239 F(done, "done") \
240 F(dot, ".") \
241 F(dot_for, ".for") \
242 F(dot_generator, ".generator") \
243 F(dot_generator_object, ".generator_object") \
244 F(dot_iterator, ".iterator") \
245 F(dot_module, ".module") \
246 F(dot_result, ".result") \
247 F(empty, "") \
248 F(eval, "eval") \
249 F(get_template_callsite, "GetTemplateCallSite") \
250 F(initialize_const_global, "initializeConstGlobal") \
251 F(initialize_var_global, "initializeVarGlobal") \
252 F(let, "let") \
253 F(make_reference_error, "MakeReferenceErrorEmbedded") \
254 F(make_syntax_error, "MakeSyntaxErrorEmbedded") \
255 F(make_type_error, "MakeTypeErrorEmbedded") \
256 F(module, "module") \
257 F(native, "native") \
258 F(next, "next") \
259 F(proto, "__proto__") \
260 F(prototype, "prototype") \
261 F(this, "this") \
262 F(use_asm, "use asm") \
263 F(use_strict, "use strict") \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 F(value, "value")
265
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400266#define OTHER_CONSTANTS(F) \
267 F(true_value) \
268 F(false_value) \
269 F(null_value) \
270 F(undefined_value) \
271 F(the_hole_value)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272
273class AstValueFactory {
274 public:
275 AstValueFactory(Zone* zone, uint32_t hash_seed)
276 : string_table_(AstRawString::Compare),
277 zone_(zone),
278 isolate_(NULL),
279 hash_seed_(hash_seed) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400280#define F(name, str) name##_string_ = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 STRING_CONSTANTS(F)
282#undef F
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400283#define F(name) name##_ = NULL;
284 OTHER_CONSTANTS(F)
285#undef F
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 }
287
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400288 Zone* zone() const { return zone_; }
289
290 const AstRawString* GetOneByteString(Vector<const uint8_t> literal) {
291 return GetOneByteStringInternal(literal);
292 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 const AstRawString* GetOneByteString(const char* string) {
294 return GetOneByteString(Vector<const uint8_t>(
295 reinterpret_cast<const uint8_t*>(string), StrLength(string)));
296 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400297 const AstRawString* GetTwoByteString(Vector<const uint16_t> literal) {
298 return GetTwoByteStringInternal(literal);
299 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 const AstRawString* GetString(Handle<String> literal);
301 const AstConsString* NewConsString(const AstString* left,
302 const AstString* right);
303
304 void Internalize(Isolate* isolate);
305 bool IsInternalized() {
306 return isolate_ != NULL;
307 }
308
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400309#define F(name, str) \
310 const AstRawString* name##_string() { \
311 if (name##_string_ == NULL) { \
312 const char* data = str; \
313 name##_string_ = GetOneByteString( \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400315 static_cast<int>(strlen(data)))); \
316 } \
317 return name##_string_; \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 }
319 STRING_CONSTANTS(F)
320#undef F
321
322 const AstValue* NewString(const AstRawString* string);
323 // A JavaScript symbol (ECMA-262 edition 6).
324 const AstValue* NewSymbol(const char* name);
325 const AstValue* NewNumber(double number);
326 const AstValue* NewSmi(int number);
327 const AstValue* NewBoolean(bool b);
328 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings);
329 const AstValue* NewNull();
330 const AstValue* NewUndefined();
331 const AstValue* NewTheHole();
332
333 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400334 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal);
335 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal);
336 AstRawString* GetString(uint32_t hash, bool is_one_byte,
337 Vector<const byte> literal_bytes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338
339 // All strings are copied here, one after another (no NULLs inbetween).
340 HashMap string_table_;
341 // For keeping track of all AstValues and AstRawStrings we've created (so that
342 // they can be internalized later).
343 List<AstValue*> values_;
344 List<AstString*> strings_;
345 Zone* zone_;
346 Isolate* isolate_;
347
348 uint32_t hash_seed_;
349
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400350#define F(name, str) const AstRawString* name##_string_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351 STRING_CONSTANTS(F)
352#undef F
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400353
354#define F(name) AstValue* name##_;
355 OTHER_CONSTANTS(F)
356#undef F
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357};
358
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400359
360bool AstRawString::IsArguments(AstValueFactory* ast_value_factory) const {
361 return ast_value_factory->arguments_string() == this;
362}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363} } // namespace v8::internal
364
365#undef STRING_CONSTANTS
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400366#undef OTHER_CONSTANTS
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367
368#endif // V8_AST_VALUE_FACTORY_H_