blob: 2952581af92c7313c203e4fb67178045d644c963 [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
75 int predata_skip() { return backing_[kPredataSkipOffset]; }
76 void set_predata_skip(int value) {
77 backing_[kPredataSkipOffset] = value;
78 }
Steve Blocka7e24c12009-10-30 11:49:00 +000079
Steve Blocka7e24c12009-10-30 11:49:00 +000080 bool is_valid() { return backing_.length() > 0; }
81
Kristian Monsen80d68ea2010-09-08 11:05:35 +010082 static const int kSize = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +000083
84 private:
85 Vector<unsigned> backing_;
86 static const int kStartPosOffset = 0;
87 static const int kEndPosOffset = 1;
88 static const int kLiteralCountOffset = 2;
89 static const int kPropertyCountOffset = 3;
Kristian Monsen80d68ea2010-09-08 11:05:35 +010090 static const int kPredataSkipOffset = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +000091};
92
93
94class ScriptDataImpl : public ScriptData {
95 public:
96 explicit ScriptDataImpl(Vector<unsigned> store)
97 : store_(store),
Kristian Monsen80d68ea2010-09-08 11:05:35 +010098 index_(kHeaderSize) { }
Steve Blocka7e24c12009-10-30 11:49:00 +000099 virtual ~ScriptDataImpl();
100 virtual int Length();
Leon Clarkef7060e22010-06-03 12:02:55 +0100101 virtual const char* Data();
Leon Clarkee46be812010-01-19 14:06:41 +0000102 virtual bool HasError();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100103 FunctionEntry GetFunctionEntry(int start);
104 void SkipFunctionEntry(int start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 bool SanityCheck();
106
107 Scanner::Location MessageLocation();
108 const char* BuildMessage();
109 Vector<const char*> BuildArgs();
110
111 bool has_error() { return store_[kHasErrorOffset]; }
112 unsigned magic() { return store_[kMagicOffset]; }
113 unsigned version() { return store_[kVersionOffset]; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100114 // Skip forward in the preparser data by the given number
115 // of unsigned ints.
116 virtual void Skip(int entries) {
117 ASSERT(entries >= 0);
118 ASSERT(entries <= store_.length() - index_);
119 index_ += entries;
120 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000121
122 static const unsigned kMagicNumber = 0xBadDead;
123 static const unsigned kCurrentVersion = 1;
124
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100125 static const int kMagicOffset = 0;
126 static const int kVersionOffset = 1;
127 static const int kHasErrorOffset = 2;
128 static const int kSizeOffset = 3;
129 static const int kHeaderSize = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100132 Vector<unsigned> store_;
133 int index_;
134
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 unsigned Read(int position);
136 unsigned* ReadAddress(int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100138 void FindStart(int position);
139 // Read strings written by ParserRecorder::WriteString.
140 static const char* ReadString(unsigned* start, int* chars);
Steve Blocka7e24c12009-10-30 11:49:00 +0000141};
142
143
144// The parser: Takes a script and and context information, and builds a
145// FunctionLiteral AST node. Returns NULL and deallocates any allocated
146// AST nodes if parsing failed.
147FunctionLiteral* MakeAST(bool compile_in_global_context,
148 Handle<Script> script,
149 v8::Extension* extension,
Leon Clarke4515c472010-02-03 11:58:03 +0000150 ScriptDataImpl* pre_data,
151 bool is_json = false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153
154ScriptDataImpl* PreParse(Handle<String> source,
155 unibrow::CharacterStream* stream,
156 v8::Extension* extension);
157
158
159bool ParseRegExp(FlatStringReader* input,
160 bool multiline,
161 RegExpCompileData* result);
162
163
164// Support for doing lazy compilation. The script is the script containing full
165// source of the script where the function is declared. The start_position and
166// end_position specifies the part of the script source which has the source
167// for the function declaration in the form:
168//
169// (<formal parameters>) { <function body> }
170//
171// without any function keyword or name.
172//
173FunctionLiteral* MakeLazyAST(Handle<Script> script,
174 Handle<String> name,
175 int start_position,
176 int end_position,
177 bool is_expression);
178
179
180// Support for handling complex values (array and object literals) that
181// can be fully handled at compile time.
182class CompileTimeValue: public AllStatic {
183 public:
184 enum Type {
Steve Block6ded16b2010-05-10 14:33:55 +0100185 OBJECT_LITERAL_FAST_ELEMENTS,
186 OBJECT_LITERAL_SLOW_ELEMENTS,
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 ARRAY_LITERAL
188 };
189
190 static bool IsCompileTimeValue(Expression* expression);
191
Iain Merrick75681382010-08-19 15:07:18 +0100192 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
193
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 // Get the value as a compile time value.
195 static Handle<FixedArray> GetValue(Expression* expression);
196
197 // Get the type of a compile time value returned by GetValue().
198 static Type GetType(Handle<FixedArray> value);
199
200 // Get the elements array of a compile time value returned by GetValue().
201 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
202
203 private:
204 static const int kTypeSlot = 0;
205 static const int kElementsSlot = 1;
206
207 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
208};
209
210
211} } // namespace v8::internal
212
213#endif // V8_PARSER_H_