blob: 2952581af92c7313c203e4fb67178045d644c963 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000032#include "allocation.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
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]; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000071 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 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
80 bool is_valid() { return backing_.length() > 0; }
81
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000082 static const int kSize = 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000090 static const int kPredataSkipOffset = 4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091};
92
93
94class ScriptDataImpl : public ScriptData {
95 public:
96 explicit ScriptDataImpl(Vector<unsigned> store)
97 : store_(store),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000098 index_(kHeaderSize) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 virtual ~ScriptDataImpl();
100 virtual int Length();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000101 virtual const char* Data();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000102 virtual bool HasError();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000103 FunctionEntry GetFunctionEntry(int start);
104 void SkipFunctionEntry(int start);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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]; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000114 // 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 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121
122 static const unsigned kMagicNumber = 0xBadDead;
123 static const unsigned kCurrentVersion = 1;
124
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000125 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;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130
131 private:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000132 Vector<unsigned> store_;
133 int index_;
134
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 unsigned Read(int position);
136 unsigned* ReadAddress(int position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000138 void FindStart(int position);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000139 // Read strings written by ParserRecorder::WriteString.
140 static const char* ReadString(unsigned* start, int* chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000150 ScriptDataImpl* pre_data,
151 bool is_json = false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152
153
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000154ScriptDataImpl* PreParse(Handle<String> source,
155 unibrow::CharacterStream* stream,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 v8::Extension* extension);
157
158
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000159bool ParseRegExp(FlatStringReader* input,
160 bool multiline,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000161 RegExpCompileData* result);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000162
163
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164// 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
ager@chromium.org32912102009-01-16 10:38:43 +0000167// for the function declaration in the form:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168//
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
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000179
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 {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000185 OBJECT_LITERAL_FAST_ELEMENTS,
186 OBJECT_LITERAL_SLOW_ELEMENTS,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000187 ARRAY_LITERAL
188 };
189
190 static bool IsCompileTimeValue(Expression* expression);
191
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000192 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
193
ager@chromium.orgbb29dc92009-03-24 13:25:23 +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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211} } // namespace v8::internal
212
213#endif // V8_PARSER_H_