ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 32 | #include "allocation.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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]; } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 71 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | |
| 80 | bool is_valid() { return backing_.length() > 0; } |
| 81 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 82 | static const int kSize = 5; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | |
| 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.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 90 | static const int kPredataSkipOffset = 4; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | |
| 94 | class ScriptDataImpl : public ScriptData { |
| 95 | public: |
| 96 | explicit ScriptDataImpl(Vector<unsigned> store) |
| 97 | : store_(store), |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 98 | index_(kHeaderSize) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | virtual ~ScriptDataImpl(); |
| 100 | virtual int Length(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 101 | virtual const char* Data(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 102 | virtual bool HasError(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 103 | FunctionEntry GetFunctionEntry(int start); |
| 104 | void SkipFunctionEntry(int start); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | 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.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 114 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | |
| 122 | static const unsigned kMagicNumber = 0xBadDead; |
| 123 | static const unsigned kCurrentVersion = 1; |
| 124 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 125 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | |
| 131 | private: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 132 | Vector<unsigned> store_; |
| 133 | int index_; |
| 134 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | unsigned Read(int position); |
| 136 | unsigned* ReadAddress(int position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame^] | 138 | void FindStart(int position); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 139 | // Read strings written by ParserRecorder::WriteString. |
| 140 | static const char* ReadString(unsigned* start, int* chars); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | }; |
| 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. |
| 147 | FunctionLiteral* MakeAST(bool compile_in_global_context, |
| 148 | Handle<Script> script, |
| 149 | v8::Extension* extension, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 150 | ScriptDataImpl* pre_data, |
| 151 | bool is_json = false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | |
| 153 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 154 | ScriptDataImpl* PreParse(Handle<String> source, |
| 155 | unibrow::CharacterStream* stream, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | v8::Extension* extension); |
| 157 | |
| 158 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 159 | bool ParseRegExp(FlatStringReader* input, |
| 160 | bool multiline, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 161 | RegExpCompileData* result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 162 | |
| 163 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 167 | // for the function declaration in the form: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | // |
| 169 | // (<formal parameters>) { <function body> } |
| 170 | // |
| 171 | // without any function keyword or name. |
| 172 | // |
| 173 | FunctionLiteral* MakeLazyAST(Handle<Script> script, |
| 174 | Handle<String> name, |
| 175 | int start_position, |
| 176 | int end_position, |
| 177 | bool is_expression); |
| 178 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 179 | |
| 180 | // Support for handling complex values (array and object literals) that |
| 181 | // can be fully handled at compile time. |
| 182 | class CompileTimeValue: public AllStatic { |
| 183 | public: |
| 184 | enum Type { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 185 | OBJECT_LITERAL_FAST_ELEMENTS, |
| 186 | OBJECT_LITERAL_SLOW_ELEMENTS, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 187 | ARRAY_LITERAL |
| 188 | }; |
| 189 | |
| 190 | static bool IsCompileTimeValue(Expression* expression); |
| 191 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 192 | static bool ArrayLiteralElementNeedsInitialization(Expression* value); |
| 193 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 194 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | } } // namespace v8::internal |
| 212 | |
| 213 | #endif // V8_PARSER_H_ |