blob: 8c00857276388d3640e2e217d5c99f18f51f6f2d [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
Steve Blocka7e24c12009-10-30 11:49:00 +000085 bool is_valid() { return backing_.length() > 0; }
86
Kristian Monsen0d5e1162010-09-30 15:31:59 +010087 static const int kSize = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +000088
89 private:
90 Vector<unsigned> backing_;
91 static const int kStartPosOffset = 0;
92 static const int kEndPosOffset = 1;
93 static const int kLiteralCountOffset = 2;
94 static const int kPropertyCountOffset = 3;
Iain Merrick9ac36c92010-09-13 15:29:50 +010095 static const int kPredataFunctionSkipOffset = 4;
96 static const int kPredataSymbolSkipOffset = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +000097};
98
99
100class ScriptDataImpl : public ScriptData {
101 public:
102 explicit ScriptDataImpl(Vector<unsigned> store)
103 : store_(store),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100104 owns_store_(true) { }
Iain Merrick9ac36c92010-09-13 15:29:50 +0100105
106 // Create an empty ScriptDataImpl that is guaranteed to not satisfy
107 // a SanityCheck.
108 ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { }
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 virtual ~ScriptDataImpl();
111 virtual int Length();
Leon Clarkef7060e22010-06-03 12:02:55 +0100112 virtual const char* Data();
Leon Clarkee46be812010-01-19 14:06:41 +0000113 virtual bool HasError();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100114
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100115 void Initialize();
116 void ReadNextSymbolPosition();
117
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100118 FunctionEntry GetFunctionEntry(int start);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100119 int GetSymbolIdentifier();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100120 void SkipFunctionEntry(int start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 bool SanityCheck();
122
123 Scanner::Location MessageLocation();
124 const char* BuildMessage();
125 Vector<const char*> BuildArgs();
126
Iain Merrick9ac36c92010-09-13 15:29:50 +0100127 int symbol_count() {
128 return (store_.length() > kHeaderSize) ? store_[kSymbolCountOffset] : 0;
129 }
130 // The following functions should only be called if SanityCheck has
131 // returned true.
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 bool has_error() { return store_[kHasErrorOffset]; }
133 unsigned magic() { return store_[kMagicOffset]; }
134 unsigned version() { return store_[kVersionOffset]; }
Iain Merrick9ac36c92010-09-13 15:29:50 +0100135
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100136 // Skip forward in the preparser data by the given number
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100137 // of unsigned ints of function entries and the given number of bytes of
138 // symbol id encoding.
139 void Skip(int function_entries, int symbol_entries) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100140 ASSERT(function_entries >= 0);
141 ASSERT(function_entries
142 <= (static_cast<int>(store_[kFunctionsSizeOffset])
143 - (function_index_ - kHeaderSize)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100144 ASSERT(symbol_entries >= 0);
145 ASSERT(symbol_entries <= symbol_data_end_ - symbol_data_);
146
147 unsigned max_function_skip = store_[kFunctionsSizeOffset] -
148 static_cast<unsigned>(function_index_ - kHeaderSize);
149 function_index_ +=
150 Min(static_cast<unsigned>(function_entries), max_function_skip);
151 symbol_data_ +=
152 Min(static_cast<unsigned>(symbol_entries),
153 static_cast<unsigned>(symbol_data_end_ - symbol_data_));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100154 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000155
156 static const unsigned kMagicNumber = 0xBadDead;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100157 static const unsigned kCurrentVersion = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100159 static const int kMagicOffset = 0;
160 static const int kVersionOffset = 1;
161 static const int kHasErrorOffset = 2;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100162 static const int kFunctionsSizeOffset = 3;
163 static const int kSymbolCountOffset = 4;
164 static const int kSizeOffset = 5;
165 static const int kHeaderSize = 6;
166
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100167 // If encoding a message, the following positions are fixed.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100168 static const int kMessageStartPos = 0;
169 static const int kMessageEndPos = 1;
170 static const int kMessageArgCountPos = 2;
171 static const int kMessageTextPos = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +0000172
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100173 static const byte kNumberTerminator = 0x80u;
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100176 Vector<unsigned> store_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100177 unsigned char* symbol_data_;
178 unsigned char* symbol_data_end_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100179 int function_index_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100180 bool owns_store_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100181
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 unsigned Read(int position);
183 unsigned* ReadAddress(int position);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100184 // Reads a number from the current symbols
185 int ReadNumber(byte** source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000186
Iain Merrick9ac36c92010-09-13 15:29:50 +0100187 ScriptDataImpl(const char* backing_store, int length)
188 : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
189 length / sizeof(unsigned)),
Iain Merrick9ac36c92010-09-13 15:29:50 +0100190 owns_store_(false) {
191 ASSERT_EQ(0, reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100192 }
193
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100194 // Read strings written by ParserRecorder::WriteString.
195 static const char* ReadString(unsigned* start, int* chars);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100196
197 friend class ScriptData;
Steve Blocka7e24c12009-10-30 11:49:00 +0000198};
199
200
201// The parser: Takes a script and and context information, and builds a
202// FunctionLiteral AST node. Returns NULL and deallocates any allocated
203// AST nodes if parsing failed.
204FunctionLiteral* MakeAST(bool compile_in_global_context,
205 Handle<Script> script,
206 v8::Extension* extension,
Leon Clarke4515c472010-02-03 11:58:03 +0000207 ScriptDataImpl* pre_data,
208 bool is_json = false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000209
Steve Block59151502010-09-22 15:07:15 +0100210// Generic preparser generating full preparse data.
Steve Blocka7e24c12009-10-30 11:49:00 +0000211ScriptDataImpl* PreParse(Handle<String> source,
212 unibrow::CharacterStream* stream,
213 v8::Extension* extension);
214
Steve Block59151502010-09-22 15:07:15 +0100215// Preparser that only does preprocessing that makes sense if only used
216// immediately after.
217ScriptDataImpl* PartialPreParse(Handle<String> source,
218 unibrow::CharacterStream* stream,
219 v8::Extension* extension);
220
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
222bool ParseRegExp(FlatStringReader* input,
223 bool multiline,
224 RegExpCompileData* result);
225
226
227// Support for doing lazy compilation. The script is the script containing full
228// source of the script where the function is declared. The start_position and
229// end_position specifies the part of the script source which has the source
230// for the function declaration in the form:
231//
232// (<formal parameters>) { <function body> }
233//
234// without any function keyword or name.
235//
236FunctionLiteral* MakeLazyAST(Handle<Script> script,
237 Handle<String> name,
238 int start_position,
239 int end_position,
240 bool is_expression);
241
242
243// Support for handling complex values (array and object literals) that
244// can be fully handled at compile time.
245class CompileTimeValue: public AllStatic {
246 public:
247 enum Type {
Steve Block6ded16b2010-05-10 14:33:55 +0100248 OBJECT_LITERAL_FAST_ELEMENTS,
249 OBJECT_LITERAL_SLOW_ELEMENTS,
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 ARRAY_LITERAL
251 };
252
253 static bool IsCompileTimeValue(Expression* expression);
254
Iain Merrick75681382010-08-19 15:07:18 +0100255 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
256
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 // Get the value as a compile time value.
258 static Handle<FixedArray> GetValue(Expression* expression);
259
260 // Get the type of a compile time value returned by GetValue().
261 static Type GetType(Handle<FixedArray> value);
262
263 // Get the elements array of a compile time value returned by GetValue().
264 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
265
266 private:
267 static const int kTypeSlot = 0;
268 static const int kElementsSlot = 1;
269
270 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
271};
272
273
274} } // namespace v8::internal
275
276#endif // V8_PARSER_H_