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 | |
| 34 | namespace v8 { namespace internal { |
| 35 | |
| 36 | |
| 37 | class ParserMessage : public Malloced { |
| 38 | public: |
| 39 | ParserMessage(Scanner::Location loc, const char* message, |
| 40 | Vector<const char*> args) |
| 41 | : loc_(loc), |
| 42 | message_(message), |
| 43 | args_(args) { } |
| 44 | ~ParserMessage(); |
| 45 | Scanner::Location location() { return loc_; } |
| 46 | const char* message() { return message_; } |
| 47 | Vector<const char*> args() { return args_; } |
| 48 | private: |
| 49 | Scanner::Location loc_; |
| 50 | const char* message_; |
| 51 | Vector<const char*> args_; |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | class FunctionEntry BASE_EMBEDDED { |
| 56 | public: |
| 57 | explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { } |
| 58 | FunctionEntry() : backing_(Vector<unsigned>::empty()) { } |
| 59 | |
| 60 | int start_pos() { return backing_[kStartPosOffset]; } |
| 61 | void set_start_pos(int value) { backing_[kStartPosOffset] = value; } |
| 62 | |
| 63 | int end_pos() { return backing_[kEndPosOffset]; } |
| 64 | void set_end_pos(int value) { backing_[kEndPosOffset] = value; } |
| 65 | |
| 66 | int literal_count() { return backing_[kLiteralCountOffset]; } |
| 67 | void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; } |
| 68 | |
| 69 | int property_count() { return backing_[kPropertyCountOffset]; } |
| 70 | void set_property_count(int value) { backing_[kPropertyCountOffset] = value; } |
| 71 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 72 | bool contains_array_literal() { |
| 73 | return backing_[kContainsArrayLiteralOffset] != 0; |
| 74 | } |
| 75 | void set_contains_array_literal(bool value) { |
| 76 | backing_[kContainsArrayLiteralOffset] = value ? 1 : 0; |
| 77 | } |
| 78 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | bool is_valid() { return backing_.length() > 0; } |
| 80 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 81 | static const int kSize = 5; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | Vector<unsigned> backing_; |
| 85 | static const int kStartPosOffset = 0; |
| 86 | static const int kEndPosOffset = 1; |
| 87 | static const int kLiteralCountOffset = 2; |
| 88 | static const int kPropertyCountOffset = 3; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 89 | static const int kContainsArrayLiteralOffset = 4; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | |
| 93 | class ScriptDataImpl : public ScriptData { |
| 94 | public: |
| 95 | explicit ScriptDataImpl(Vector<unsigned> store) |
| 96 | : store_(store), |
| 97 | last_entry_(0) { } |
| 98 | virtual ~ScriptDataImpl(); |
| 99 | virtual int Length(); |
| 100 | virtual unsigned* Data(); |
| 101 | FunctionEntry GetFunctionEnd(int start); |
| 102 | bool SanityCheck(); |
| 103 | |
| 104 | Scanner::Location MessageLocation(); |
| 105 | const char* BuildMessage(); |
| 106 | Vector<const char*> BuildArgs(); |
| 107 | |
| 108 | bool has_error() { return store_[kHasErrorOffset]; } |
| 109 | unsigned magic() { return store_[kMagicOffset]; } |
| 110 | unsigned version() { return store_[kVersionOffset]; } |
| 111 | |
| 112 | static const unsigned kMagicNumber = 0xBadDead; |
| 113 | static const unsigned kCurrentVersion = 1; |
| 114 | |
| 115 | static const unsigned kMagicOffset = 0; |
| 116 | static const unsigned kVersionOffset = 1; |
| 117 | static const unsigned kHasErrorOffset = 2; |
| 118 | static const unsigned kSizeOffset = 3; |
| 119 | static const unsigned kHeaderSize = 4; |
| 120 | |
| 121 | private: |
| 122 | unsigned Read(int position); |
| 123 | unsigned* ReadAddress(int position); |
| 124 | int EntryCount(); |
| 125 | FunctionEntry nth(int n); |
| 126 | |
| 127 | Vector<unsigned> store_; |
| 128 | |
| 129 | // The last entry returned. This is used to make lookup faster: |
| 130 | // the next entry to return is typically the next entry so lookup |
| 131 | // will usually be much faster if we start from the last entry. |
| 132 | int last_entry_; |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | // The parser: Takes a script and and context information, and builds a |
| 137 | // FunctionLiteral AST node. Returns NULL and deallocates any allocated |
| 138 | // AST nodes if parsing failed. |
| 139 | FunctionLiteral* MakeAST(bool compile_in_global_context, |
| 140 | Handle<Script> script, |
| 141 | v8::Extension* extension, |
| 142 | ScriptDataImpl* pre_data); |
| 143 | |
| 144 | |
| 145 | ScriptDataImpl* PreParse(unibrow::CharacterStream* stream, |
| 146 | v8::Extension* extension); |
| 147 | |
| 148 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 149 | bool ParseRegExp(FlatStringReader* input, |
| 150 | bool multiline, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 151 | RegExpCompileData* result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 152 | |
| 153 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 154 | // Support for doing lazy compilation. The script is the script containing full |
| 155 | // source of the script where the function is declared. The start_position and |
| 156 | // 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] | 157 | // for the function declaration in the form: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | // |
| 159 | // (<formal parameters>) { <function body> } |
| 160 | // |
| 161 | // without any function keyword or name. |
| 162 | // |
| 163 | FunctionLiteral* MakeLazyAST(Handle<Script> script, |
| 164 | Handle<String> name, |
| 165 | int start_position, |
| 166 | int end_position, |
| 167 | bool is_expression); |
| 168 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 169 | |
| 170 | // Support for handling complex values (array and object literals) that |
| 171 | // can be fully handled at compile time. |
| 172 | class CompileTimeValue: public AllStatic { |
| 173 | public: |
| 174 | enum Type { |
| 175 | OBJECT_LITERAL, |
| 176 | ARRAY_LITERAL |
| 177 | }; |
| 178 | |
| 179 | static bool IsCompileTimeValue(Expression* expression); |
| 180 | |
| 181 | // Get the value as a compile time value. |
| 182 | static Handle<FixedArray> GetValue(Expression* expression); |
| 183 | |
| 184 | // Get the type of a compile time value returned by GetValue(). |
| 185 | static Type GetType(Handle<FixedArray> value); |
| 186 | |
| 187 | // Get the elements array of a compile time value returned by GetValue(). |
| 188 | static Handle<FixedArray> GetElements(Handle<FixedArray> value); |
| 189 | |
| 190 | private: |
| 191 | static const int kTypeSlot = 0; |
| 192 | static const int kElementsSlot = 1; |
| 193 | |
| 194 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 195 | }; |
| 196 | |
| 197 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | } } // namespace v8::internal |
| 199 | |
| 200 | #endif // V8_PARSER_H_ |