Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | |
| 38 | class 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 | |
| 56 | class 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 Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 71 | void set_property_count(int value) { |
| 72 | backing_[kPropertyCountOffset] = value; |
| 73 | } |
| 74 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 75 | int predata_function_skip() { return backing_[kPredataFunctionSkipOffset]; } |
| 76 | void set_predata_function_skip(int value) { |
| 77 | backing_[kPredataFunctionSkipOffset] = value; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 78 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 80 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | bool is_valid() { return backing_.length() > 0; } |
| 92 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 93 | static const int kSize = 7; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | |
| 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 Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 101 | static const int kPredataFunctionSkipOffset = 4; |
| 102 | static const int kPredataSymbolSkipOffset = 5; |
| 103 | static const int kSymbolIdSkipOffset = 6; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | |
| 107 | class ScriptDataImpl : public ScriptData { |
| 108 | public: |
| 109 | explicit ScriptDataImpl(Vector<unsigned> store) |
| 110 | : store_(store), |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 111 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | virtual ~ScriptDataImpl(); |
| 129 | virtual int Length(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 130 | virtual const char* Data(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 131 | virtual bool HasError(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 132 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 133 | FunctionEntry GetFunctionEntry(int start); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 134 | int GetSymbolIdentifier(int start); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 135 | void SkipFunctionEntry(int start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | bool SanityCheck(); |
| 137 | |
| 138 | Scanner::Location MessageLocation(); |
| 139 | const char* BuildMessage(); |
| 140 | Vector<const char*> BuildArgs(); |
| 141 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 142 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | bool has_error() { return store_[kHasErrorOffset]; } |
| 148 | unsigned magic() { return store_[kMagicOffset]; } |
| 149 | unsigned version() { return store_[kVersionOffset]; } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 150 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 151 | // Skip forward in the preparser data by the given number |
| 152 | // of unsigned ints. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 153 | 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 Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 161 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | |
| 163 | static const unsigned kMagicNumber = 0xBadDead; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 164 | static const unsigned kCurrentVersion = 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 166 | static const int kMagicOffset = 0; |
| 167 | static const int kVersionOffset = 1; |
| 168 | static const int kHasErrorOffset = 2; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 169 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
| 179 | private: |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 180 | Vector<unsigned> store_; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 181 | int function_index_; |
| 182 | int symbol_index_; |
| 183 | int symbol_id_; |
| 184 | bool owns_store_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 185 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | unsigned Read(int position); |
| 187 | unsigned* ReadAddress(int position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 189 | 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 Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 199 | // Read strings written by ParserRecorder::WriteString. |
| 200 | static const char* ReadString(unsigned* start, int* chars); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 201 | |
| 202 | friend class ScriptData; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | }; |
| 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. |
| 209 | FunctionLiteral* MakeAST(bool compile_in_global_context, |
| 210 | Handle<Script> script, |
| 211 | v8::Extension* extension, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 212 | ScriptDataImpl* pre_data, |
| 213 | bool is_json = false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 214 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame^] | 215 | // Generic preparser generating full preparse data. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | ScriptDataImpl* PreParse(Handle<String> source, |
| 217 | unibrow::CharacterStream* stream, |
| 218 | v8::Extension* extension); |
| 219 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame^] | 220 | // Preparser that only does preprocessing that makes sense if only used |
| 221 | // immediately after. |
| 222 | ScriptDataImpl* PartialPreParse(Handle<String> source, |
| 223 | unibrow::CharacterStream* stream, |
| 224 | v8::Extension* extension); |
| 225 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | |
| 227 | bool ParseRegExp(FlatStringReader* input, |
| 228 | bool multiline, |
| 229 | RegExpCompileData* result); |
| 230 | |
| 231 | |
| 232 | // Support for doing lazy compilation. The script is the script containing full |
| 233 | // source of the script where the function is declared. The start_position and |
| 234 | // end_position specifies the part of the script source which has the source |
| 235 | // for the function declaration in the form: |
| 236 | // |
| 237 | // (<formal parameters>) { <function body> } |
| 238 | // |
| 239 | // without any function keyword or name. |
| 240 | // |
| 241 | FunctionLiteral* MakeLazyAST(Handle<Script> script, |
| 242 | Handle<String> name, |
| 243 | int start_position, |
| 244 | int end_position, |
| 245 | bool is_expression); |
| 246 | |
| 247 | |
| 248 | // Support for handling complex values (array and object literals) that |
| 249 | // can be fully handled at compile time. |
| 250 | class CompileTimeValue: public AllStatic { |
| 251 | public: |
| 252 | enum Type { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 253 | OBJECT_LITERAL_FAST_ELEMENTS, |
| 254 | OBJECT_LITERAL_SLOW_ELEMENTS, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | ARRAY_LITERAL |
| 256 | }; |
| 257 | |
| 258 | static bool IsCompileTimeValue(Expression* expression); |
| 259 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 260 | static bool ArrayLiteralElementNeedsInitialization(Expression* value); |
| 261 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | // Get the value as a compile time value. |
| 263 | static Handle<FixedArray> GetValue(Expression* expression); |
| 264 | |
| 265 | // Get the type of a compile time value returned by GetValue(). |
| 266 | static Type GetType(Handle<FixedArray> value); |
| 267 | |
| 268 | // Get the elements array of a compile time value returned by GetValue(). |
| 269 | static Handle<FixedArray> GetElements(Handle<FixedArray> value); |
| 270 | |
| 271 | private: |
| 272 | static const int kTypeSlot = 0; |
| 273 | static const int kElementsSlot = 1; |
| 274 | |
| 275 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 276 | }; |
| 277 | |
| 278 | |
| 279 | } } // namespace v8::internal |
| 280 | |
| 281 | #endif // V8_PARSER_H_ |