blob: 56412a05a54e53f13a834f938c4999dfdad3e65c [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
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_PARSER_H_
29#define V8_PARSER_H_
30
31#include "scanner.h"
32#include "allocation.h"
33
34namespace v8 {
35namespace internal {
36
37
38class ParserMessage : public Malloced {
39 public:
40 ParserMessage(Scanner::Location loc, const char* message,
41 Vector<const char*> args)
42 : loc_(loc),
43 message_(message),
44 args_(args) { }
45 ~ParserMessage();
46 Scanner::Location location() { return loc_; }
47 const char* message() { return message_; }
48 Vector<const char*> args() { return args_; }
49 private:
50 Scanner::Location loc_;
51 const char* message_;
52 Vector<const char*> args_;
53};
54
55
56class FunctionEntry BASE_EMBEDDED {
57 public:
58 explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { }
59 FunctionEntry() : backing_(Vector<unsigned>::empty()) { }
60
61 int start_pos() { return backing_[kStartPosOffset]; }
62 void set_start_pos(int value) { backing_[kStartPosOffset] = value; }
63
64 int end_pos() { return backing_[kEndPosOffset]; }
65 void set_end_pos(int value) { backing_[kEndPosOffset] = value; }
66
67 int literal_count() { return backing_[kLiteralCountOffset]; }
68 void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; }
69
70 int property_count() { return backing_[kPropertyCountOffset]; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +010071 void set_property_count(int value) {
72 backing_[kPropertyCountOffset] = value;
73 }
74
Iain Merrick9ac36c92010-09-13 15:29:50 +010075 int predata_function_skip() { return backing_[kPredataFunctionSkipOffset]; }
76 void set_predata_function_skip(int value) {
77 backing_[kPredataFunctionSkipOffset] = value;
Kristian Monsen80d68ea2010-09-08 11:05:35 +010078 }
Steve Blocka7e24c12009-10-30 11:49:00 +000079
Iain Merrick9ac36c92010-09-13 15:29:50 +010080 int predata_symbol_skip() { return backing_[kPredataSymbolSkipOffset]; }
81 void set_predata_symbol_skip(int value) {
82 backing_[kPredataSymbolSkipOffset] = value;
83 }
84
85 int symbol_id_skip() { return backing_[kSymbolIdSkipOffset]; }
86 void set_symbol_id_skip(int value) {
87 backing_[kSymbolIdSkipOffset] = value;
88 }
89
90
Steve Blocka7e24c12009-10-30 11:49:00 +000091 bool is_valid() { return backing_.length() > 0; }
92
Iain Merrick9ac36c92010-09-13 15:29:50 +010093 static const int kSize = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +000094
95 private:
96 Vector<unsigned> backing_;
97 static const int kStartPosOffset = 0;
98 static const int kEndPosOffset = 1;
99 static const int kLiteralCountOffset = 2;
100 static const int kPropertyCountOffset = 3;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100101 static const int kPredataFunctionSkipOffset = 4;
102 static const int kPredataSymbolSkipOffset = 5;
103 static const int kSymbolIdSkipOffset = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000104};
105
106
107class ScriptDataImpl : public ScriptData {
108 public:
109 explicit ScriptDataImpl(Vector<unsigned> store)
110 : store_(store),
Iain Merrick9ac36c92010-09-13 15:29:50 +0100111 function_index_(kHeaderSize),
112 symbol_id_(0),
113 owns_store_(true) {
114 Initialize();
115 }
116
117 void Initialize() {
118 if (store_.length() >= kHeaderSize) {
119 // Otherwise we won't satisfy the SanityCheck.
120 symbol_index_ = kHeaderSize + store_[kFunctionsSizeOffset];
121 }
122 }
123
124 // Create an empty ScriptDataImpl that is guaranteed to not satisfy
125 // a SanityCheck.
126 ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { }
127
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 virtual ~ScriptDataImpl();
129 virtual int Length();
Leon Clarkef7060e22010-06-03 12:02:55 +0100130 virtual const char* Data();
Leon Clarkee46be812010-01-19 14:06:41 +0000131 virtual bool HasError();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100132
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100133 FunctionEntry GetFunctionEntry(int start);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100134 int GetSymbolIdentifier(int start);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100135 void SkipFunctionEntry(int start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 bool SanityCheck();
137
138 Scanner::Location MessageLocation();
139 const char* BuildMessage();
140 Vector<const char*> BuildArgs();
141
Iain Merrick9ac36c92010-09-13 15:29:50 +0100142 int symbol_count() {
143 return (store_.length() > kHeaderSize) ? store_[kSymbolCountOffset] : 0;
144 }
145 // The following functions should only be called if SanityCheck has
146 // returned true.
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 bool has_error() { return store_[kHasErrorOffset]; }
148 unsigned magic() { return store_[kMagicOffset]; }
149 unsigned version() { return store_[kVersionOffset]; }
Iain Merrick9ac36c92010-09-13 15:29:50 +0100150
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100151 // Skip forward in the preparser data by the given number
152 // of unsigned ints.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100153 virtual void Skip(int function_entries, int symbol_entries, int symbol_ids) {
154 ASSERT(function_entries >= 0);
155 ASSERT(function_entries
156 <= (static_cast<int>(store_[kFunctionsSizeOffset])
157 - (function_index_ - kHeaderSize)));
158 function_index_ += function_entries;
159 symbol_index_ += symbol_entries;
160 symbol_id_ += symbol_ids;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100161 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000162
163 static const unsigned kMagicNumber = 0xBadDead;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100164 static const unsigned kCurrentVersion = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100166 static const int kMagicOffset = 0;
167 static const int kVersionOffset = 1;
168 static const int kHasErrorOffset = 2;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100169 static const int kFunctionsSizeOffset = 3;
170 static const int kSymbolCountOffset = 4;
171 static const int kSizeOffset = 5;
172 static const int kHeaderSize = 6;
173
174 static const int kMessageStartPos = 0;
175 static const int kMessageEndPos = 1;
176 static const int kMessageArgCountPos = 2;
177 static const int kMessageTextPos = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100180 Vector<unsigned> store_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100181 int function_index_;
182 int symbol_index_;
183 int symbol_id_;
184 bool owns_store_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100185
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 unsigned Read(int position);
187 unsigned* ReadAddress(int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000188
Iain Merrick9ac36c92010-09-13 15:29:50 +0100189 ScriptDataImpl(const char* backing_store, int length)
190 : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
191 length / sizeof(unsigned)),
192 function_index_(kHeaderSize),
193 symbol_id_(0),
194 owns_store_(false) {
195 ASSERT_EQ(0, reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned));
196 Initialize();
197 }
198
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100199 // Read strings written by ParserRecorder::WriteString.
200 static const char* ReadString(unsigned* start, int* chars);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100201
202 friend class ScriptData;
Steve Blocka7e24c12009-10-30 11:49:00 +0000203};
204
205
206// The parser: Takes a script and and context information, and builds a
207// FunctionLiteral AST node. Returns NULL and deallocates any allocated
208// AST nodes if parsing failed.
209FunctionLiteral* MakeAST(bool compile_in_global_context,
210 Handle<Script> script,
211 v8::Extension* extension,
Leon Clarke4515c472010-02-03 11:58:03 +0000212 ScriptDataImpl* pre_data,
213 bool is_json = false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214
215
216ScriptDataImpl* PreParse(Handle<String> source,
217 unibrow::CharacterStream* stream,
218 v8::Extension* extension);
219
220
221bool ParseRegExp(FlatStringReader* input,
222 bool multiline,
223 RegExpCompileData* result);
224
225
226// Support for doing lazy compilation. The script is the script containing full
227// source of the script where the function is declared. The start_position and
228// end_position specifies the part of the script source which has the source
229// for the function declaration in the form:
230//
231// (<formal parameters>) { <function body> }
232//
233// without any function keyword or name.
234//
235FunctionLiteral* MakeLazyAST(Handle<Script> script,
236 Handle<String> name,
237 int start_position,
238 int end_position,
239 bool is_expression);
240
241
242// Support for handling complex values (array and object literals) that
243// can be fully handled at compile time.
244class CompileTimeValue: public AllStatic {
245 public:
246 enum Type {
Steve Block6ded16b2010-05-10 14:33:55 +0100247 OBJECT_LITERAL_FAST_ELEMENTS,
248 OBJECT_LITERAL_SLOW_ELEMENTS,
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 ARRAY_LITERAL
250 };
251
252 static bool IsCompileTimeValue(Expression* expression);
253
Iain Merrick75681382010-08-19 15:07:18 +0100254 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
255
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 // Get the value as a compile time value.
257 static Handle<FixedArray> GetValue(Expression* expression);
258
259 // Get the type of a compile time value returned by GetValue().
260 static Type GetType(Handle<FixedArray> value);
261
262 // Get the elements array of a compile time value returned by GetValue().
263 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
264
265 private:
266 static const int kTypeSlot = 0;
267 static const int kElementsSlot = 1;
268
269 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
270};
271
272
273} } // namespace v8::internal
274
275#endif // V8_PARSER_H_