danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 31 | #include "ast.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 33 | #include "char-predicates-inl.h" |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 34 | #include "codegen.h" |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 35 | #include "compiler.h" |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 36 | #include "func-name-inferrer.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 37 | #include "messages.h" |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 38 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include "platform.h" |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 40 | #include "preparser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #include "runtime.h" |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 42 | #include "scanner-character-streams.h" |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 43 | #include "scopeinfo.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 44 | #include "string-stream.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | namespace v8 { |
| 47 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 49 | // PositionStack is used for on-stack allocation of token positions for |
| 50 | // new expressions. Please look at ParseNewExpression. |
| 51 | |
| 52 | class PositionStack { |
| 53 | public: |
| 54 | explicit PositionStack(bool* ok) : top_(NULL), ok_(ok) {} |
| 55 | ~PositionStack() { ASSERT(!*ok_ || is_empty()); } |
| 56 | |
| 57 | class Element { |
| 58 | public: |
| 59 | Element(PositionStack* stack, int value) { |
| 60 | previous_ = stack->top(); |
| 61 | value_ = value; |
| 62 | stack->set_top(this); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | Element* previous() { return previous_; } |
| 67 | int value() { return value_; } |
| 68 | friend class PositionStack; |
| 69 | Element* previous_; |
| 70 | int value_; |
| 71 | }; |
| 72 | |
| 73 | bool is_empty() { return top_ == NULL; } |
| 74 | int pop() { |
| 75 | ASSERT(!is_empty()); |
| 76 | int result = top_->value(); |
| 77 | top_ = top_->previous(); |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | Element* top() { return top_; } |
| 83 | void set_top(Element* value) { top_ = value; } |
| 84 | Element* top_; |
| 85 | bool* ok_; |
| 86 | }; |
| 87 | |
| 88 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 89 | RegExpBuilder::RegExpBuilder() |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 90 | : zone_(Isolate::Current()->zone()), |
| 91 | pending_empty_(false), |
| 92 | characters_(NULL), |
| 93 | terms_(), |
| 94 | alternatives_() |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 95 | #ifdef DEBUG |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 96 | , last_added_(ADD_NONE) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 97 | #endif |
| 98 | {} |
| 99 | |
| 100 | |
| 101 | void RegExpBuilder::FlushCharacters() { |
| 102 | pending_empty_ = false; |
| 103 | if (characters_ != NULL) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 104 | RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 105 | characters_ = NULL; |
| 106 | text_.Add(atom); |
| 107 | LAST(ADD_ATOM); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void RegExpBuilder::FlushText() { |
| 113 | FlushCharacters(); |
| 114 | int num_text = text_.length(); |
| 115 | if (num_text == 0) { |
| 116 | return; |
| 117 | } else if (num_text == 1) { |
| 118 | terms_.Add(text_.last()); |
| 119 | } else { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 120 | RegExpText* text = new(zone()) RegExpText(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 121 | for (int i = 0; i < num_text; i++) |
| 122 | text_.Get(i)->AppendToText(text); |
| 123 | terms_.Add(text); |
| 124 | } |
| 125 | text_.Clear(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void RegExpBuilder::AddCharacter(uc16 c) { |
| 130 | pending_empty_ = false; |
| 131 | if (characters_ == NULL) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 132 | characters_ = new(zone()) ZoneList<uc16>(4); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 133 | } |
| 134 | characters_->Add(c); |
| 135 | LAST(ADD_CHAR); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void RegExpBuilder::AddEmpty() { |
| 140 | pending_empty_ = true; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void RegExpBuilder::AddAtom(RegExpTree* term) { |
| 145 | if (term->IsEmpty()) { |
| 146 | AddEmpty(); |
| 147 | return; |
| 148 | } |
| 149 | if (term->IsTextElement()) { |
| 150 | FlushCharacters(); |
| 151 | text_.Add(term); |
| 152 | } else { |
| 153 | FlushText(); |
| 154 | terms_.Add(term); |
| 155 | } |
| 156 | LAST(ADD_ATOM); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void RegExpBuilder::AddAssertion(RegExpTree* assert) { |
| 161 | FlushText(); |
| 162 | terms_.Add(assert); |
| 163 | LAST(ADD_ASSERT); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void RegExpBuilder::NewAlternative() { |
| 168 | FlushTerms(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | void RegExpBuilder::FlushTerms() { |
| 173 | FlushText(); |
| 174 | int num_terms = terms_.length(); |
| 175 | RegExpTree* alternative; |
| 176 | if (num_terms == 0) { |
| 177 | alternative = RegExpEmpty::GetInstance(); |
| 178 | } else if (num_terms == 1) { |
| 179 | alternative = terms_.last(); |
| 180 | } else { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 181 | alternative = new(zone()) RegExpAlternative(terms_.GetList()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 182 | } |
| 183 | alternatives_.Add(alternative); |
| 184 | terms_.Clear(); |
| 185 | LAST(ADD_NONE); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | RegExpTree* RegExpBuilder::ToRegExp() { |
| 190 | FlushTerms(); |
| 191 | int num_alternatives = alternatives_.length(); |
| 192 | if (num_alternatives == 0) { |
| 193 | return RegExpEmpty::GetInstance(); |
| 194 | } |
| 195 | if (num_alternatives == 1) { |
| 196 | return alternatives_.last(); |
| 197 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 198 | return new(zone()) RegExpDisjunction(alternatives_.GetList()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 202 | void RegExpBuilder::AddQuantifierToAtom(int min, |
| 203 | int max, |
| 204 | RegExpQuantifier::Type type) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 205 | if (pending_empty_) { |
| 206 | pending_empty_ = false; |
| 207 | return; |
| 208 | } |
| 209 | RegExpTree* atom; |
| 210 | if (characters_ != NULL) { |
| 211 | ASSERT(last_added_ == ADD_CHAR); |
| 212 | // Last atom was character. |
| 213 | Vector<const uc16> char_vector = characters_->ToConstVector(); |
| 214 | int num_chars = char_vector.length(); |
| 215 | if (num_chars > 1) { |
| 216 | Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 217 | text_.Add(new(zone()) RegExpAtom(prefix)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 218 | char_vector = char_vector.SubVector(num_chars - 1, num_chars); |
| 219 | } |
| 220 | characters_ = NULL; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 221 | atom = new(zone()) RegExpAtom(char_vector); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 222 | FlushText(); |
| 223 | } else if (text_.length() > 0) { |
| 224 | ASSERT(last_added_ == ADD_ATOM); |
| 225 | atom = text_.RemoveLast(); |
| 226 | FlushText(); |
| 227 | } else if (terms_.length() > 0) { |
| 228 | ASSERT(last_added_ == ADD_ATOM); |
| 229 | atom = terms_.RemoveLast(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 230 | if (atom->max_match() == 0) { |
| 231 | // Guaranteed to only match an empty string. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 232 | LAST(ADD_TERM); |
| 233 | if (min == 0) { |
| 234 | return; |
| 235 | } |
| 236 | terms_.Add(atom); |
| 237 | return; |
| 238 | } |
| 239 | } else { |
| 240 | // Only call immediately after adding an atom or character! |
| 241 | UNREACHABLE(); |
| 242 | return; |
| 243 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 244 | terms_.Add(new(zone()) RegExpQuantifier(min, max, type, atom)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 245 | LAST(ADD_TERM); |
| 246 | } |
| 247 | |
| 248 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 249 | Handle<String> Parser::LookupSymbol(int symbol_id) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 250 | // Length of symbol cache is the number of identified symbols. |
| 251 | // If we are larger than that, or negative, it's not a cached symbol. |
| 252 | // This might also happen if there is no preparser symbol data, even |
| 253 | // if there is some preparser data. |
| 254 | if (static_cast<unsigned>(symbol_id) |
| 255 | >= static_cast<unsigned>(symbol_cache_.length())) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 256 | if (scanner().is_literal_ascii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 257 | return isolate()->factory()->LookupAsciiSymbol( |
| 258 | scanner().literal_ascii_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 259 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 260 | return isolate()->factory()->LookupTwoByteSymbol( |
| 261 | scanner().literal_uc16_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 262 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 263 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 264 | return LookupCachedSymbol(symbol_id); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 268 | Handle<String> Parser::LookupCachedSymbol(int symbol_id) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 269 | // Make sure the cache is large enough to hold the symbol identifier. |
| 270 | if (symbol_cache_.length() <= symbol_id) { |
| 271 | // Increase length to index + 1. |
| 272 | symbol_cache_.AddBlock(Handle<String>::null(), |
| 273 | symbol_id + 1 - symbol_cache_.length()); |
| 274 | } |
| 275 | Handle<String> result = symbol_cache_.at(symbol_id); |
| 276 | if (result.is_null()) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 277 | if (scanner().is_literal_ascii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 278 | result = isolate()->factory()->LookupAsciiSymbol( |
| 279 | scanner().literal_ascii_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 280 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 281 | result = isolate()->factory()->LookupTwoByteSymbol( |
| 282 | scanner().literal_uc16_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 283 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 284 | symbol_cache_.at(symbol_id) = result; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 285 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 287 | isolate()->counters()->total_preparse_symbols_skipped()->Increment(); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 288 | return result; |
| 289 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 291 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 292 | FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) { |
| 293 | // The current pre-data entry must be a FunctionEntry with the given |
| 294 | // start position. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 295 | if ((function_index_ + FunctionEntry::kSize <= store_.length()) |
| 296 | && (static_cast<int>(store_[function_index_]) == start)) { |
| 297 | int index = function_index_; |
| 298 | function_index_ += FunctionEntry::kSize; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 299 | return FunctionEntry(store_.SubVector(index, |
| 300 | index + FunctionEntry::kSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | } |
| 302 | return FunctionEntry(); |
| 303 | } |
| 304 | |
| 305 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 306 | int ScriptDataImpl::GetSymbolIdentifier() { |
| 307 | return ReadNumber(&symbol_data_); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 311 | bool ScriptDataImpl::SanityCheck() { |
| 312 | // Check that the header data is valid and doesn't specify |
| 313 | // point to positions outside the store. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 314 | if (store_.length() < PreparseDataConstants::kHeaderSize) return false; |
| 315 | if (magic() != PreparseDataConstants::kMagicNumber) return false; |
| 316 | if (version() != PreparseDataConstants::kCurrentVersion) return false; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 317 | if (has_error()) { |
| 318 | // Extra sane sanity check for error message encoding. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 319 | if (store_.length() <= PreparseDataConstants::kHeaderSize |
| 320 | + PreparseDataConstants::kMessageTextPos) { |
| 321 | return false; |
| 322 | } |
| 323 | if (Read(PreparseDataConstants::kMessageStartPos) > |
| 324 | Read(PreparseDataConstants::kMessageEndPos)) { |
| 325 | return false; |
| 326 | } |
| 327 | unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos); |
| 328 | int pos = PreparseDataConstants::kMessageTextPos; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 329 | for (unsigned int i = 0; i <= arg_count; i++) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 330 | if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) { |
| 331 | return false; |
| 332 | } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 333 | int length = static_cast<int>(Read(pos)); |
| 334 | if (length < 0) return false; |
| 335 | pos += 1 + length; |
| 336 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 337 | if (store_.length() < PreparseDataConstants::kHeaderSize + pos) { |
| 338 | return false; |
| 339 | } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | // Check that the space allocated for function entries is sane. |
| 343 | int functions_size = |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 344 | static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 345 | if (functions_size < 0) return false; |
| 346 | if (functions_size % FunctionEntry::kSize != 0) return false; |
| 347 | // Check that the count of symbols is non-negative. |
| 348 | int symbol_count = |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 349 | static_cast<int>(store_[PreparseDataConstants::kSymbolCountOffset]); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 350 | if (symbol_count < 0) return false; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 351 | // Check that the total size has room for header and function entries. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 352 | int minimum_size = |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 353 | PreparseDataConstants::kHeaderSize + functions_size; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 354 | if (store_.length() < minimum_size) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | return true; |
| 356 | } |
| 357 | |
| 358 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 359 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 360 | const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | int length = start[0]; |
| 362 | char* result = NewArray<char>(length + 1); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 363 | for (int i = 0; i < length; i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | result[i] = start[i + 1]; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 365 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | result[length] = '\0'; |
| 367 | if (chars != NULL) *chars = length; |
| 368 | return result; |
| 369 | } |
| 370 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | Scanner::Location ScriptDataImpl::MessageLocation() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 372 | int beg_pos = Read(PreparseDataConstants::kMessageStartPos); |
| 373 | int end_pos = Read(PreparseDataConstants::kMessageEndPos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 374 | return Scanner::Location(beg_pos, end_pos); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | const char* ScriptDataImpl::BuildMessage() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 379 | unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 380 | return ReadString(start, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | Vector<const char*> ScriptDataImpl::BuildArgs() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 385 | int arg_count = Read(PreparseDataConstants::kMessageArgCountPos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | const char** array = NewArray<const char*>(arg_count); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 387 | // Position after text found by skipping past length field and |
| 388 | // length field content words. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 389 | int pos = PreparseDataConstants::kMessageTextPos + 1 |
| 390 | + Read(PreparseDataConstants::kMessageTextPos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | for (int i = 0; i < arg_count; i++) { |
| 392 | int count = 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 393 | array[i] = ReadString(ReadAddress(pos), &count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | pos += count + 1; |
| 395 | } |
| 396 | return Vector<const char*>(array, arg_count); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | unsigned ScriptDataImpl::Read(int position) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 401 | return store_[PreparseDataConstants::kHeaderSize + position]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
| 405 | unsigned* ScriptDataImpl::ReadAddress(int position) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 406 | return &store_[PreparseDataConstants::kHeaderSize + position]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 410 | Scope* Parser::NewScope(Scope* parent, ScopeType type) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 411 | Scope* result = new(zone()) Scope(parent, type); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 412 | result->Initialize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | return result; |
| 414 | } |
| 415 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 416 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 417 | // ---------------------------------------------------------------------------- |
| 418 | // Target is a support class to facilitate manipulation of the |
| 419 | // Parser's target_stack_ (the stack of potential 'break' and |
| 420 | // 'continue' statement targets). Upon construction, a new target is |
| 421 | // added; it is removed upon destruction. |
| 422 | |
| 423 | class Target BASE_EMBEDDED { |
| 424 | public: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 425 | Target(Target** variable, AstNode* node) |
| 426 | : variable_(variable), node_(node), previous_(*variable) { |
| 427 | *variable = this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | ~Target() { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 431 | *variable_ = previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | } |
| 433 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 434 | Target* previous() { return previous_; } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 435 | AstNode* node() { return node_; } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 436 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | private: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 438 | Target** variable_; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 439 | AstNode* node_; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 440 | Target* previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | |
| 444 | class TargetScope BASE_EMBEDDED { |
| 445 | public: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 446 | explicit TargetScope(Target** variable) |
| 447 | : variable_(variable), previous_(*variable) { |
| 448 | *variable = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | ~TargetScope() { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 452 | *variable_ = previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | private: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 456 | Target** variable_; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 457 | Target* previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | |
| 461 | // ---------------------------------------------------------------------------- |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 462 | // FunctionState and BlockState together implement the parser's scope stack. |
| 463 | // The parser's current scope is in top_scope_. The BlockState and |
| 464 | // FunctionState constructors push on the scope stack and the destructors |
| 465 | // pop. They are also used to hold the parser's per-function and per-block |
| 466 | // state. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 467 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 468 | class Parser::BlockState BASE_EMBEDDED { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 469 | public: |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 470 | BlockState(Parser* parser, Scope* scope) |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 471 | : parser_(parser), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 472 | outer_scope_(parser->top_scope_) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 473 | parser->top_scope_ = scope; |
| 474 | } |
| 475 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 476 | ~BlockState() { parser_->top_scope_ = outer_scope_; } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 477 | |
| 478 | private: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 479 | Parser* parser_; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 480 | Scope* outer_scope_; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 483 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 484 | Parser::FunctionState::FunctionState(Parser* parser, |
| 485 | Scope* scope, |
| 486 | Isolate* isolate) |
| 487 | : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 488 | next_handler_index_(0), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 489 | expected_property_count_(0), |
| 490 | only_simple_this_property_assignments_(false), |
| 491 | this_property_assignments_(isolate->factory()->empty_fixed_array()), |
| 492 | parser_(parser), |
| 493 | outer_function_state_(parser->current_function_state_), |
| 494 | outer_scope_(parser->top_scope_), |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 495 | saved_ast_node_id_(isolate->ast_node_id()), |
| 496 | factory_(isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 497 | parser->top_scope_ = scope; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 498 | parser->current_function_state_ = this; |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 499 | isolate->set_ast_node_id(AstNode::kDeclarationsId + 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 503 | Parser::FunctionState::~FunctionState() { |
| 504 | parser_->top_scope_ = outer_scope_; |
| 505 | parser_->current_function_state_ = outer_function_state_; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 506 | if (outer_function_state_ != NULL) { |
| 507 | parser_->isolate()->set_ast_node_id(saved_ast_node_id_); |
| 508 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | // ---------------------------------------------------------------------------- |
| 513 | // The CHECK_OK macro is a convenient macro to enforce error |
| 514 | // handling for functions that may fail (by returning !*ok). |
| 515 | // |
| 516 | // CAUTION: This macro appends extra statements after a call, |
| 517 | // thus it must never be used where only a single statement |
| 518 | // is correct (e.g. an if statement branch w/o braces)! |
| 519 | |
| 520 | #define CHECK_OK ok); \ |
| 521 | if (!*ok) return NULL; \ |
| 522 | ((void)0 |
| 523 | #define DUMMY ) // to make indentation work |
| 524 | #undef DUMMY |
| 525 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 526 | #define CHECK_FAILED /**/); \ |
| 527 | if (failed_) return NULL; \ |
| 528 | ((void)0 |
| 529 | #define DUMMY ) // to make indentation work |
| 530 | #undef DUMMY |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | |
| 532 | // ---------------------------------------------------------------------------- |
| 533 | // Implementation of Parser |
| 534 | |
| 535 | Parser::Parser(Handle<Script> script, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 536 | int parser_flags, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 537 | v8::Extension* extension, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 538 | ScriptDataImpl* pre_data) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 539 | : isolate_(script->GetIsolate()), |
| 540 | symbol_cache_(pre_data ? pre_data->symbol_count() : 0), |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 541 | script_(script), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 542 | scanner_(isolate_->unicode_cache()), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 543 | reusable_preparser_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 544 | top_scope_(NULL), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 545 | current_function_state_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | target_stack_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | extension_(extension), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 548 | pre_data_(pre_data), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 549 | fni_(NULL), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 550 | allow_natives_syntax_((parser_flags & kAllowNativesSyntax) != 0), |
| 551 | allow_lazy_((parser_flags & kAllowLazy) != 0), |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 552 | allow_modules_((parser_flags & kAllowModules) != 0), |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 553 | stack_overflow_(false), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 554 | parenthesized_function_(false) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 555 | isolate_->set_ast_node_id(0); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 556 | if ((parser_flags & kLanguageModeMask) == EXTENDED_MODE) { |
| 557 | scanner().SetHarmonyScoping(true); |
| 558 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 559 | if ((parser_flags & kAllowModules) != 0) { |
| 560 | scanner().SetHarmonyModules(true); |
| 561 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 565 | FunctionLiteral* Parser::ParseProgram(CompilationInfo* info) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 566 | ZoneScope zone_scope(isolate(), DONT_DELETE_ON_EXIT); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 567 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 568 | HistogramTimerScope timer(isolate()->counters()->parse()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 569 | Handle<String> source(String::cast(script_->source())); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 570 | isolate()->counters()->total_parse_size()->Increment(source->length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 571 | fni_ = new(zone()) FuncNameInferrer(isolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | |
| 573 | // Initialize parser state. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 574 | source->TryFlatten(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 575 | if (source->IsExternalTwoByteString()) { |
| 576 | // Notice that the stream is destroyed at the end of the branch block. |
| 577 | // The last line of the blocks can't be moved outside, even though they're |
| 578 | // identical calls. |
| 579 | ExternalTwoByteStringUC16CharacterStream stream( |
| 580 | Handle<ExternalTwoByteString>::cast(source), 0, source->length()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 581 | scanner_.Initialize(&stream); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 582 | return DoParseProgram(info, source, &zone_scope); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 583 | } else { |
| 584 | GenericStringUC16CharacterStream stream(source, 0, source->length()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 585 | scanner_.Initialize(&stream); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 586 | return DoParseProgram(info, source, &zone_scope); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 591 | FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info, |
| 592 | Handle<String> source, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 593 | ZoneScope* zone_scope) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 594 | ASSERT(top_scope_ == NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 595 | ASSERT(target_stack_ == NULL); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 596 | if (pre_data_ != NULL) pre_data_->Initialize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | |
| 598 | // Compute the parsing mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 599 | mode_ = (FLAG_lazy && allow_lazy_) ? PARSE_LAZILY : PARSE_EAGERLY; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 600 | if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; |
| 601 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 602 | Handle<String> no_name = isolate()->factory()->empty_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | |
| 604 | FunctionLiteral* result = NULL; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 605 | { Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); |
| 606 | info->SetGlobalScope(scope); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 607 | if (!info->is_global() && |
| 608 | (info->shared_info().is_null() || info->shared_info()->is_function())) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 609 | scope = Scope::DeserializeScopeChain(*info->calling_context(), scope); |
| 610 | scope = NewScope(scope, EVAL_SCOPE); |
| 611 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 612 | scope->set_start_position(0); |
| 613 | scope->set_end_position(source->length()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 614 | FunctionState function_state(this, scope, isolate()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 615 | top_scope_->SetLanguageMode(info->language_mode()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 616 | ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | bool ok = true; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 618 | int beg_loc = scanner().location().beg_pos; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 619 | ParseSourceElements(body, Token::EOS, &ok); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 620 | if (ok && !top_scope_->is_classic_mode()) { |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 621 | CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok); |
| 622 | } |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 623 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 624 | if (ok && is_extended_mode()) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 625 | CheckConflictingVarDeclarations(scope, &ok); |
| 626 | } |
| 627 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | if (ok) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 629 | result = factory()->NewFunctionLiteral( |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 630 | no_name, |
| 631 | top_scope_, |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 632 | body, |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 633 | function_state.materialized_literal_count(), |
| 634 | function_state.expected_property_count(), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 635 | function_state.handler_count(), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 636 | function_state.only_simple_this_property_assignments(), |
| 637 | function_state.this_property_assignments(), |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 638 | 0, |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 639 | FunctionLiteral::kNoDuplicateParameters, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 640 | FunctionLiteral::ANONYMOUS_EXPRESSION, |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 641 | FunctionLiteral::kGlobalOrEval); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 642 | result->set_ast_properties(factory()->visitor()->ast_properties()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 643 | } else if (stack_overflow_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 644 | isolate()->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | // Make sure the target stack is empty. |
| 649 | ASSERT(target_stack_ == NULL); |
| 650 | |
| 651 | // If there was a syntax error we have to get rid of the AST |
| 652 | // and it is not safe to do so before the scope has been deleted. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 653 | if (result == NULL) zone_scope->DeleteOnExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 654 | return result; |
| 655 | } |
| 656 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 657 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 658 | FunctionLiteral* Parser::ParseLazy(CompilationInfo* info) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 659 | ZoneScope zone_scope(isolate(), DONT_DELETE_ON_EXIT); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 660 | HistogramTimerScope timer(isolate()->counters()->parse_lazy()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 661 | Handle<String> source(String::cast(script_->source())); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 662 | isolate()->counters()->total_parse_size()->Increment(source->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 663 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 664 | Handle<SharedFunctionInfo> shared_info = info->shared_info(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 665 | // Initialize parser state. |
| 666 | source->TryFlatten(); |
| 667 | if (source->IsExternalTwoByteString()) { |
| 668 | ExternalTwoByteStringUC16CharacterStream stream( |
| 669 | Handle<ExternalTwoByteString>::cast(source), |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 670 | shared_info->start_position(), |
| 671 | shared_info->end_position()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 672 | FunctionLiteral* result = ParseLazy(info, &stream, &zone_scope); |
| 673 | return result; |
| 674 | } else { |
| 675 | GenericStringUC16CharacterStream stream(source, |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 676 | shared_info->start_position(), |
| 677 | shared_info->end_position()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 678 | FunctionLiteral* result = ParseLazy(info, &stream, &zone_scope); |
| 679 | return result; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 684 | FunctionLiteral* Parser::ParseLazy(CompilationInfo* info, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 685 | UC16CharacterStream* source, |
| 686 | ZoneScope* zone_scope) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 687 | Handle<SharedFunctionInfo> shared_info = info->shared_info(); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 688 | scanner_.Initialize(source); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 689 | ASSERT(top_scope_ == NULL); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 690 | ASSERT(target_stack_ == NULL); |
| 691 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 692 | Handle<String> name(String::cast(shared_info->name())); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 693 | fni_ = new(zone()) FuncNameInferrer(isolate()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 694 | fni_->PushEnclosingName(name); |
| 695 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 696 | mode_ = PARSE_EAGERLY; |
| 697 | |
| 698 | // Place holder for the result. |
| 699 | FunctionLiteral* result = NULL; |
| 700 | |
| 701 | { |
| 702 | // Parse the function literal. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 703 | Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 704 | info->SetGlobalScope(scope); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 705 | if (!info->closure().is_null()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 706 | scope = Scope::DeserializeScopeChain(info->closure()->context(), scope); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 707 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 708 | FunctionState function_state(this, scope, isolate()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 709 | ASSERT(scope->language_mode() != STRICT_MODE || !info->is_classic_mode()); |
| 710 | ASSERT(scope->language_mode() != EXTENDED_MODE || |
| 711 | info->is_extended_mode()); |
| 712 | ASSERT(info->language_mode() == shared_info->language_mode()); |
| 713 | scope->SetLanguageMode(shared_info->language_mode()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 714 | FunctionLiteral::Type type = shared_info->is_expression() |
| 715 | ? (shared_info->is_anonymous() |
| 716 | ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 717 | : FunctionLiteral::NAMED_EXPRESSION) |
| 718 | : FunctionLiteral::DECLARATION; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 719 | bool ok = true; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 720 | result = ParseFunctionLiteral(name, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 721 | false, // Strict mode name already checked. |
| 722 | RelocInfo::kNoPosition, |
| 723 | type, |
| 724 | &ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | // Make sure the results agree. |
| 726 | ASSERT(ok == (result != NULL)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // Make sure the target stack is empty. |
| 730 | ASSERT(target_stack_ == NULL); |
| 731 | |
| 732 | // If there was a stack overflow we have to get rid of AST and it is |
| 733 | // not safe to do before scope has been deleted. |
| 734 | if (result == NULL) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 735 | zone_scope->DeleteOnExit(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 736 | if (stack_overflow_) isolate()->StackOverflow(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 737 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 738 | Handle<String> inferred_name(shared_info->inferred_name()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 739 | result->set_inferred_name(inferred_name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 740 | } |
| 741 | return result; |
| 742 | } |
| 743 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 744 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 745 | Handle<String> Parser::GetSymbol(bool* ok) { |
| 746 | int symbol_id = -1; |
| 747 | if (pre_data() != NULL) { |
| 748 | symbol_id = pre_data()->GetSymbolIdentifier(); |
| 749 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 750 | return LookupSymbol(symbol_id); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 754 | void Parser::ReportMessage(const char* type, Vector<const char*> args) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 755 | Scanner::Location source_location = scanner().location(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 756 | ReportMessageAt(source_location, type, args); |
| 757 | } |
| 758 | |
| 759 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 760 | void Parser::ReportMessage(const char* type, Vector<Handle<String> > args) { |
| 761 | Scanner::Location source_location = scanner().location(); |
| 762 | ReportMessageAt(source_location, type, args); |
| 763 | } |
| 764 | |
| 765 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 766 | void Parser::ReportMessageAt(Scanner::Location source_location, |
| 767 | const char* type, |
| 768 | Vector<const char*> args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 769 | MessageLocation location(script_, |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 770 | source_location.beg_pos, |
| 771 | source_location.end_pos); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 772 | Factory* factory = isolate()->factory(); |
| 773 | Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 774 | for (int i = 0; i < args.length(); i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 775 | Handle<String> arg_string = factory->NewStringFromUtf8(CStrVector(args[i])); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 776 | elements->set(i, *arg_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 777 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 778 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
| 779 | Handle<Object> result = factory->NewSyntaxError(type, array); |
| 780 | isolate()->Throw(*result, &location); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 784 | void Parser::ReportMessageAt(Scanner::Location source_location, |
| 785 | const char* type, |
| 786 | Vector<Handle<String> > args) { |
| 787 | MessageLocation location(script_, |
| 788 | source_location.beg_pos, |
| 789 | source_location.end_pos); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 790 | Factory* factory = isolate()->factory(); |
| 791 | Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 792 | for (int i = 0; i < args.length(); i++) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 793 | elements->set(i, *args[i]); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 794 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 795 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
| 796 | Handle<Object> result = factory->NewSyntaxError(type, array); |
| 797 | isolate()->Throw(*result, &location); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 801 | // Base class containing common code for the different finder classes used by |
| 802 | // the parser. |
| 803 | class ParserFinder { |
| 804 | protected: |
| 805 | ParserFinder() {} |
| 806 | static Assignment* AsAssignment(Statement* stat) { |
| 807 | if (stat == NULL) return NULL; |
| 808 | ExpressionStatement* exp_stat = stat->AsExpressionStatement(); |
| 809 | if (exp_stat == NULL) return NULL; |
| 810 | return exp_stat->expression()->AsAssignment(); |
| 811 | } |
| 812 | }; |
| 813 | |
| 814 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 815 | // An InitializationBlockFinder finds and marks sequences of statements of the |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 816 | // form expr.a = ...; expr.b = ...; etc. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 817 | class InitializationBlockFinder : public ParserFinder { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 818 | public: |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 819 | // We find and mark the initialization blocks in top level |
| 820 | // non-looping code only. This is because the optimization prevents |
| 821 | // reuse of the map transitions, so it should be used only for code |
| 822 | // that will only be run once. |
| 823 | InitializationBlockFinder(Scope* top_scope, Target* target) |
| 824 | : enabled_(top_scope->DeclarationScope()->is_global_scope() && |
| 825 | !IsLoopTarget(target)), |
| 826 | first_in_block_(NULL), |
| 827 | last_in_block_(NULL), |
| 828 | block_size_(0) {} |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 829 | |
| 830 | ~InitializationBlockFinder() { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 831 | if (!enabled_) return; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 832 | if (InBlock()) EndBlock(); |
| 833 | } |
| 834 | |
| 835 | void Update(Statement* stat) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 836 | if (!enabled_) return; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 837 | Assignment* assignment = AsAssignment(stat); |
| 838 | if (InBlock()) { |
| 839 | if (BlockContinues(assignment)) { |
| 840 | UpdateBlock(assignment); |
| 841 | } else { |
| 842 | EndBlock(); |
| 843 | } |
| 844 | } |
| 845 | if (!InBlock() && (assignment != NULL) && |
| 846 | (assignment->op() == Token::ASSIGN)) { |
| 847 | StartBlock(assignment); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | private: |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 852 | // The minimum number of contiguous assignment that will |
| 853 | // be treated as an initialization block. Benchmarks show that |
| 854 | // the overhead exceeds the savings below this limit. |
| 855 | static const int kMinInitializationBlock = 3; |
| 856 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 857 | static bool IsLoopTarget(Target* target) { |
| 858 | while (target != NULL) { |
| 859 | if (target->node()->AsIterationStatement() != NULL) return true; |
| 860 | target = target->previous(); |
| 861 | } |
| 862 | return false; |
| 863 | } |
| 864 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 865 | // Returns true if the expressions appear to denote the same object. |
| 866 | // In the context of initialization blocks, we only consider expressions |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 867 | // of the form 'expr.x' or expr["x"]. |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 868 | static bool SameObject(Expression* e1, Expression* e2) { |
| 869 | VariableProxy* v1 = e1->AsVariableProxy(); |
| 870 | VariableProxy* v2 = e2->AsVariableProxy(); |
| 871 | if (v1 != NULL && v2 != NULL) { |
| 872 | return v1->name()->Equals(*v2->name()); |
| 873 | } |
| 874 | Property* p1 = e1->AsProperty(); |
| 875 | Property* p2 = e2->AsProperty(); |
| 876 | if ((p1 == NULL) || (p2 == NULL)) return false; |
| 877 | Literal* key1 = p1->key()->AsLiteral(); |
| 878 | Literal* key2 = p2->key()->AsLiteral(); |
| 879 | if ((key1 == NULL) || (key2 == NULL)) return false; |
| 880 | if (!key1->handle()->IsString() || !key2->handle()->IsString()) { |
| 881 | return false; |
| 882 | } |
| 883 | String* name1 = String::cast(*key1->handle()); |
| 884 | String* name2 = String::cast(*key2->handle()); |
| 885 | if (!name1->Equals(name2)) return false; |
| 886 | return SameObject(p1->obj(), p2->obj()); |
| 887 | } |
| 888 | |
| 889 | // Returns true if the expressions appear to denote different properties |
| 890 | // of the same object. |
| 891 | static bool PropertyOfSameObject(Expression* e1, Expression* e2) { |
| 892 | Property* p1 = e1->AsProperty(); |
| 893 | Property* p2 = e2->AsProperty(); |
| 894 | if ((p1 == NULL) || (p2 == NULL)) return false; |
| 895 | return SameObject(p1->obj(), p2->obj()); |
| 896 | } |
| 897 | |
| 898 | bool BlockContinues(Assignment* assignment) { |
| 899 | if ((assignment == NULL) || (first_in_block_ == NULL)) return false; |
| 900 | if (assignment->op() != Token::ASSIGN) return false; |
| 901 | return PropertyOfSameObject(first_in_block_->target(), |
| 902 | assignment->target()); |
| 903 | } |
| 904 | |
| 905 | void StartBlock(Assignment* assignment) { |
| 906 | first_in_block_ = assignment; |
| 907 | last_in_block_ = assignment; |
| 908 | block_size_ = 1; |
| 909 | } |
| 910 | |
| 911 | void UpdateBlock(Assignment* assignment) { |
| 912 | last_in_block_ = assignment; |
| 913 | ++block_size_; |
| 914 | } |
| 915 | |
| 916 | void EndBlock() { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 917 | if (block_size_ >= kMinInitializationBlock) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 918 | first_in_block_->mark_block_start(); |
| 919 | last_in_block_->mark_block_end(); |
| 920 | } |
| 921 | last_in_block_ = first_in_block_ = NULL; |
| 922 | block_size_ = 0; |
| 923 | } |
| 924 | |
| 925 | bool InBlock() { return first_in_block_ != NULL; } |
| 926 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 927 | const bool enabled_; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 928 | Assignment* first_in_block_; |
| 929 | Assignment* last_in_block_; |
| 930 | int block_size_; |
| 931 | |
| 932 | DISALLOW_COPY_AND_ASSIGN(InitializationBlockFinder); |
| 933 | }; |
| 934 | |
| 935 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 936 | // A ThisNamedPropertyAssignmentFinder finds and marks statements of the form |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 937 | // this.x = ...;, where x is a named property. It also determines whether a |
| 938 | // function contains only assignments of this type. |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 939 | class ThisNamedPropertyAssignmentFinder : public ParserFinder { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 940 | public: |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 941 | explicit ThisNamedPropertyAssignmentFinder(Isolate* isolate) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 942 | : isolate_(isolate), |
| 943 | only_simple_this_property_assignments_(true), |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 944 | names_(0), |
| 945 | assigned_arguments_(0), |
| 946 | assigned_constants_(0) { |
| 947 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 948 | |
| 949 | void Update(Scope* scope, Statement* stat) { |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 950 | // Bail out if function already has property assignment that are |
| 951 | // not simple this property assignments. |
| 952 | if (!only_simple_this_property_assignments_) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 953 | return; |
| 954 | } |
| 955 | |
| 956 | // Check whether this statement is of the form this.x = ...; |
| 957 | Assignment* assignment = AsAssignment(stat); |
| 958 | if (IsThisPropertyAssignment(assignment)) { |
| 959 | HandleThisPropertyAssignment(scope, assignment); |
| 960 | } else { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 961 | only_simple_this_property_assignments_ = false; |
| 962 | } |
| 963 | } |
| 964 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 965 | // Returns whether only statements of the form this.x = y; where y is either a |
| 966 | // constant or a function argument was encountered. |
| 967 | bool only_simple_this_property_assignments() { |
| 968 | return only_simple_this_property_assignments_; |
| 969 | } |
| 970 | |
| 971 | // Returns a fixed array containing three elements for each assignment of the |
| 972 | // form this.x = y; |
| 973 | Handle<FixedArray> GetThisPropertyAssignments() { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 974 | if (names_.is_empty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 975 | return isolate_->factory()->empty_fixed_array(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 976 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 977 | ASSERT_EQ(names_.length(), assigned_arguments_.length()); |
| 978 | ASSERT_EQ(names_.length(), assigned_constants_.length()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 979 | Handle<FixedArray> assignments = |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 980 | isolate_->factory()->NewFixedArray(names_.length() * 3); |
| 981 | for (int i = 0; i < names_.length(); ++i) { |
| 982 | assignments->set(i * 3, *names_[i]); |
| 983 | assignments->set(i * 3 + 1, Smi::FromInt(assigned_arguments_[i])); |
| 984 | assignments->set(i * 3 + 2, *assigned_constants_[i]); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 985 | } |
| 986 | return assignments; |
| 987 | } |
| 988 | |
| 989 | private: |
| 990 | bool IsThisPropertyAssignment(Assignment* assignment) { |
| 991 | if (assignment != NULL) { |
| 992 | Property* property = assignment->target()->AsProperty(); |
| 993 | return assignment->op() == Token::ASSIGN |
| 994 | && property != NULL |
| 995 | && property->obj()->AsVariableProxy() != NULL |
| 996 | && property->obj()->AsVariableProxy()->is_this(); |
| 997 | } |
| 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | void HandleThisPropertyAssignment(Scope* scope, Assignment* assignment) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 1002 | // Check that the property assigned to is a named property, which is not |
| 1003 | // __proto__. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1004 | Property* property = assignment->target()->AsProperty(); |
| 1005 | ASSERT(property != NULL); |
| 1006 | Literal* literal = property->key()->AsLiteral(); |
| 1007 | uint32_t dummy; |
| 1008 | if (literal != NULL && |
| 1009 | literal->handle()->IsString() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1010 | !String::cast(*(literal->handle()))->Equals( |
| 1011 | isolate_->heap()->Proto_symbol()) && |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1012 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
| 1013 | Handle<String> key = Handle<String>::cast(literal->handle()); |
| 1014 | |
| 1015 | // Check whether the value assigned is either a constant or matches the |
| 1016 | // name of one of the arguments to the function. |
| 1017 | if (assignment->value()->AsLiteral() != NULL) { |
| 1018 | // Constant assigned. |
| 1019 | Literal* literal = assignment->value()->AsLiteral(); |
| 1020 | AssignmentFromConstant(key, literal->handle()); |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1021 | return; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1022 | } else if (assignment->value()->AsVariableProxy() != NULL) { |
| 1023 | // Variable assigned. |
| 1024 | Handle<String> name = |
| 1025 | assignment->value()->AsVariableProxy()->name(); |
| 1026 | // Check whether the variable assigned matches an argument name. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1027 | for (int i = 0; i < scope->num_parameters(); i++) { |
| 1028 | if (*scope->parameter(i)->name() == *name) { |
| 1029 | // Assigned from function argument. |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1030 | AssignmentFromParameter(key, i); |
| 1031 | return; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1036 | // It is not a simple "this.x = value;" assignment with a constant |
| 1037 | // or parameter value. |
| 1038 | AssignmentFromSomethingElse(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1041 | |
| 1042 | |
| 1043 | |
| 1044 | // We will potentially reorder the property assignments, so they must be |
| 1045 | // simple enough that the ordering does not matter. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1046 | void AssignmentFromParameter(Handle<String> name, int index) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1047 | EnsureInitialized(); |
| 1048 | for (int i = 0; i < names_.length(); ++i) { |
| 1049 | if (name->Equals(*names_[i])) { |
| 1050 | assigned_arguments_[i] = index; |
| 1051 | assigned_constants_[i] = isolate_->factory()->undefined_value(); |
| 1052 | return; |
| 1053 | } |
| 1054 | } |
| 1055 | names_.Add(name); |
| 1056 | assigned_arguments_.Add(index); |
| 1057 | assigned_constants_.Add(isolate_->factory()->undefined_value()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void AssignmentFromConstant(Handle<String> name, Handle<Object> value) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1061 | EnsureInitialized(); |
| 1062 | for (int i = 0; i < names_.length(); ++i) { |
| 1063 | if (name->Equals(*names_[i])) { |
| 1064 | assigned_arguments_[i] = -1; |
| 1065 | assigned_constants_[i] = value; |
| 1066 | return; |
| 1067 | } |
| 1068 | } |
| 1069 | names_.Add(name); |
| 1070 | assigned_arguments_.Add(-1); |
| 1071 | assigned_constants_.Add(value); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1074 | void AssignmentFromSomethingElse() { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1075 | // The this assignment is not a simple one. |
| 1076 | only_simple_this_property_assignments_ = false; |
| 1077 | } |
| 1078 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1079 | void EnsureInitialized() { |
| 1080 | if (names_.capacity() == 0) { |
| 1081 | ASSERT(assigned_arguments_.capacity() == 0); |
| 1082 | ASSERT(assigned_constants_.capacity() == 0); |
| 1083 | names_.Initialize(4); |
| 1084 | assigned_arguments_.Initialize(4); |
| 1085 | assigned_constants_.Initialize(4); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1089 | Isolate* isolate_; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1090 | bool only_simple_this_property_assignments_; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1091 | ZoneStringList names_; |
| 1092 | ZoneList<int> assigned_arguments_; |
| 1093 | ZoneObjectList assigned_constants_; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 1097 | void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | int end_token, |
| 1099 | bool* ok) { |
| 1100 | // SourceElements :: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1101 | // (ModuleElement)* <end_token> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1102 | |
| 1103 | // Allocate a target stack to use for this set of source |
| 1104 | // elements. This way, all scripts and functions get their own |
| 1105 | // target stack thus avoiding illegal breaks and continues across |
| 1106 | // functions. |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1107 | TargetScope scope(&this->target_stack_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1108 | |
| 1109 | ASSERT(processor != NULL); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1110 | InitializationBlockFinder block_finder(top_scope_, target_stack_); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1111 | ThisNamedPropertyAssignmentFinder this_property_assignment_finder(isolate()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1112 | bool directive_prologue = true; // Parsing directive prologue. |
| 1113 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1114 | while (peek() != end_token) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1115 | if (directive_prologue && peek() != Token::STRING) { |
| 1116 | directive_prologue = false; |
| 1117 | } |
| 1118 | |
| 1119 | Scanner::Location token_loc = scanner().peek_location(); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1120 | Statement* stat = ParseModuleElement(NULL, CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1121 | if (stat == NULL || stat->IsEmpty()) { |
| 1122 | directive_prologue = false; // End of directive prologue. |
| 1123 | continue; |
| 1124 | } |
| 1125 | |
| 1126 | if (directive_prologue) { |
| 1127 | // A shot at a directive. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1128 | ExpressionStatement* e_stat; |
| 1129 | Literal* literal; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1130 | // Still processing directive prologue? |
| 1131 | if ((e_stat = stat->AsExpressionStatement()) != NULL && |
| 1132 | (literal = e_stat->expression()->AsLiteral()) != NULL && |
| 1133 | literal->handle()->IsString()) { |
| 1134 | Handle<String> directive = Handle<String>::cast(literal->handle()); |
| 1135 | |
| 1136 | // Check "use strict" directive (ES5 14.1). |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1137 | if (top_scope_->is_classic_mode() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1138 | directive->Equals(isolate()->heap()->use_strict()) && |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1139 | token_loc.end_pos - token_loc.beg_pos == |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1140 | isolate()->heap()->use_strict()->length() + 2) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1141 | // TODO(ES6): Fix entering extended mode, once it is specified. |
| 1142 | top_scope_->SetLanguageMode(FLAG_harmony_scoping |
| 1143 | ? EXTENDED_MODE : STRICT_MODE); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1144 | // "use strict" is the only directive for now. |
| 1145 | directive_prologue = false; |
| 1146 | } |
| 1147 | } else { |
| 1148 | // End of the directive prologue. |
| 1149 | directive_prologue = false; |
| 1150 | } |
| 1151 | } |
| 1152 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1153 | block_finder.Update(stat); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1154 | // Find and mark all assignments to named properties in this (this.x =) |
| 1155 | if (top_scope_->is_function_scope()) { |
| 1156 | this_property_assignment_finder.Update(top_scope_, stat); |
| 1157 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1158 | processor->Add(stat); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1159 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Propagate the collected information on this property assignments. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 1162 | if (top_scope_->is_function_scope()) { |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1163 | bool only_simple_this_property_assignments = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1164 | this_property_assignment_finder.only_simple_this_property_assignments() |
| 1165 | && top_scope_->declarations()->length() == 0; |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1166 | if (only_simple_this_property_assignments) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1167 | current_function_state_->SetThisPropertyAssignmentInfo( |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1168 | only_simple_this_property_assignments, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1169 | this_property_assignment_finder.GetThisPropertyAssignments()); |
| 1170 | } |
| 1171 | } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1172 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1177 | Statement* Parser::ParseModuleElement(ZoneStringList* labels, |
| 1178 | bool* ok) { |
| 1179 | // (Ecma 262 5th Edition, clause 14): |
| 1180 | // SourceElement: |
| 1181 | // Statement |
| 1182 | // FunctionDeclaration |
| 1183 | // |
| 1184 | // In harmony mode we allow additionally the following productions |
| 1185 | // ModuleElement: |
| 1186 | // LetDeclaration |
| 1187 | // ConstDeclaration |
| 1188 | // ModuleDeclaration |
| 1189 | // ImportDeclaration |
| 1190 | // ExportDeclaration |
| 1191 | |
| 1192 | switch (peek()) { |
| 1193 | case Token::FUNCTION: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1194 | return ParseFunctionDeclaration(NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1195 | case Token::LET: |
| 1196 | case Token::CONST: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1197 | return ParseVariableStatement(kModuleElement, NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1198 | case Token::IMPORT: |
| 1199 | return ParseImportDeclaration(ok); |
| 1200 | case Token::EXPORT: |
| 1201 | return ParseExportDeclaration(ok); |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 1202 | default: { |
| 1203 | Statement* stmt = ParseStatement(labels, CHECK_OK); |
| 1204 | // Handle 'module' as a context-sensitive keyword. |
| 1205 | if (FLAG_harmony_modules && |
| 1206 | peek() == Token::IDENTIFIER && |
| 1207 | !scanner().HasAnyLineTerminatorBeforeNext() && |
| 1208 | stmt != NULL) { |
| 1209 | ExpressionStatement* estmt = stmt->AsExpressionStatement(); |
| 1210 | if (estmt != NULL && |
| 1211 | estmt->expression()->AsVariableProxy() != NULL && |
| 1212 | estmt->expression()->AsVariableProxy()->name()->Equals( |
| 1213 | isolate()->heap()->module_symbol()) && |
| 1214 | !scanner().literal_contains_escapes()) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1215 | return ParseModuleDeclaration(NULL, ok); |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | return stmt; |
| 1219 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1224 | Block* Parser::ParseModuleDeclaration(ZoneStringList* names, bool* ok) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1225 | // ModuleDeclaration: |
| 1226 | // 'module' Identifier Module |
| 1227 | |
| 1228 | // Create new block with one expected declaration. |
| 1229 | Block* block = factory()->NewBlock(NULL, 1, true); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1230 | Handle<String> name = ParseIdentifier(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1231 | |
| 1232 | #ifdef DEBUG |
| 1233 | if (FLAG_print_interface_details) |
| 1234 | PrintF("# Module %s...\n", name->ToAsciiArray()); |
| 1235 | #endif |
| 1236 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1237 | Module* module = ParseModule(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1238 | VariableProxy* proxy = NewUnresolved(name, LET, module->interface()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1239 | Declaration* declaration = |
| 1240 | factory()->NewModuleDeclaration(proxy, module, top_scope_); |
| 1241 | Declare(declaration, true, CHECK_OK); |
| 1242 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1243 | #ifdef DEBUG |
| 1244 | if (FLAG_print_interface_details) |
| 1245 | PrintF("# Module %s.\n", name->ToAsciiArray()); |
| 1246 | |
| 1247 | if (FLAG_print_interfaces) { |
| 1248 | PrintF("module %s : ", name->ToAsciiArray()); |
| 1249 | module->interface()->Print(); |
| 1250 | } |
| 1251 | #endif |
| 1252 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1253 | // TODO(rossberg): Add initialization statement to block. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (names) names->Add(name); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1256 | return block; |
| 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | Module* Parser::ParseModule(bool* ok) { |
| 1261 | // Module: |
| 1262 | // '{' ModuleElement '}' |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1263 | // '=' ModulePath ';' |
| 1264 | // 'at' String ';' |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1265 | |
| 1266 | switch (peek()) { |
| 1267 | case Token::LBRACE: |
| 1268 | return ParseModuleLiteral(ok); |
| 1269 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1270 | case Token::ASSIGN: { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1271 | Expect(Token::ASSIGN, CHECK_OK); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1272 | Module* result = ParseModulePath(CHECK_OK); |
| 1273 | ExpectSemicolon(CHECK_OK); |
| 1274 | return result; |
| 1275 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1276 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1277 | default: { |
| 1278 | ExpectContextualKeyword("at", CHECK_OK); |
| 1279 | Module* result = ParseModuleUrl(CHECK_OK); |
| 1280 | ExpectSemicolon(CHECK_OK); |
| 1281 | return result; |
| 1282 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | Module* Parser::ParseModuleLiteral(bool* ok) { |
| 1288 | // Module: |
| 1289 | // '{' ModuleElement '}' |
| 1290 | |
| 1291 | // Construct block expecting 16 statements. |
| 1292 | Block* body = factory()->NewBlock(NULL, 16, false); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1293 | #ifdef DEBUG |
| 1294 | if (FLAG_print_interface_details) PrintF("# Literal "); |
| 1295 | #endif |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1296 | Scope* scope = NewScope(top_scope_, MODULE_SCOPE); |
| 1297 | |
| 1298 | Expect(Token::LBRACE, CHECK_OK); |
| 1299 | scope->set_start_position(scanner().location().beg_pos); |
| 1300 | scope->SetLanguageMode(EXTENDED_MODE); |
| 1301 | |
| 1302 | { |
| 1303 | BlockState block_state(this, scope); |
| 1304 | TargetCollector collector; |
| 1305 | Target target(&this->target_stack_, &collector); |
| 1306 | Target target_body(&this->target_stack_, body); |
| 1307 | InitializationBlockFinder block_finder(top_scope_, target_stack_); |
| 1308 | |
| 1309 | while (peek() != Token::RBRACE) { |
| 1310 | Statement* stat = ParseModuleElement(NULL, CHECK_OK); |
| 1311 | if (stat && !stat->IsEmpty()) { |
| 1312 | body->AddStatement(stat); |
| 1313 | block_finder.Update(stat); |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | Expect(Token::RBRACE, CHECK_OK); |
| 1319 | scope->set_end_position(scanner().location().end_pos); |
| 1320 | body->set_block_scope(scope); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1321 | |
| 1322 | scope->interface()->Freeze(ok); |
| 1323 | ASSERT(ok); |
| 1324 | return factory()->NewModuleLiteral(body, scope->interface()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | Module* Parser::ParseModulePath(bool* ok) { |
| 1329 | // ModulePath: |
| 1330 | // Identifier |
| 1331 | // ModulePath '.' Identifier |
| 1332 | |
| 1333 | Module* result = ParseModuleVariable(CHECK_OK); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1334 | while (Check(Token::PERIOD)) { |
| 1335 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1336 | #ifdef DEBUG |
| 1337 | if (FLAG_print_interface_details) |
| 1338 | PrintF("# Path .%s ", name->ToAsciiArray()); |
| 1339 | #endif |
| 1340 | Module* member = factory()->NewModulePath(result, name); |
| 1341 | result->interface()->Add(name, member->interface(), ok); |
| 1342 | if (!*ok) { |
| 1343 | #ifdef DEBUG |
| 1344 | if (FLAG_print_interfaces) { |
| 1345 | PrintF("PATH TYPE ERROR at '%s'\n", name->ToAsciiArray()); |
| 1346 | PrintF("result: "); |
| 1347 | result->interface()->Print(); |
| 1348 | PrintF("member: "); |
| 1349 | member->interface()->Print(); |
| 1350 | } |
| 1351 | #endif |
| 1352 | ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1)); |
| 1353 | return NULL; |
| 1354 | } |
| 1355 | result = member; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | return result; |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | Module* Parser::ParseModuleVariable(bool* ok) { |
| 1363 | // ModulePath: |
| 1364 | // Identifier |
| 1365 | |
| 1366 | Handle<String> name = ParseIdentifier(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1367 | #ifdef DEBUG |
| 1368 | if (FLAG_print_interface_details) |
| 1369 | PrintF("# Module variable %s ", name->ToAsciiArray()); |
| 1370 | #endif |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1371 | VariableProxy* proxy = top_scope_->NewUnresolved( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1372 | factory(), name, scanner().location().beg_pos, Interface::NewModule()); |
| 1373 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1374 | return factory()->NewModuleVariable(proxy); |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | Module* Parser::ParseModuleUrl(bool* ok) { |
| 1379 | // Module: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1380 | // String |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1381 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1382 | Expect(Token::STRING, CHECK_OK); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1383 | Handle<String> symbol = GetSymbol(CHECK_OK); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1384 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1385 | // TODO(ES6): Request JS resource from environment... |
| 1386 | |
| 1387 | #ifdef DEBUG |
| 1388 | if (FLAG_print_interface_details) PrintF("# Url "); |
| 1389 | #endif |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1390 | return factory()->NewModuleUrl(symbol); |
| 1391 | } |
| 1392 | |
| 1393 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1394 | Module* Parser::ParseModuleSpecifier(bool* ok) { |
| 1395 | // ModuleSpecifier: |
| 1396 | // String |
| 1397 | // ModulePath |
| 1398 | |
| 1399 | if (peek() == Token::STRING) { |
| 1400 | return ParseModuleUrl(ok); |
| 1401 | } else { |
| 1402 | return ParseModulePath(ok); |
| 1403 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1407 | Block* Parser::ParseImportDeclaration(bool* ok) { |
| 1408 | // ImportDeclaration: |
| 1409 | // 'import' IdentifierName (',' IdentifierName)* 'from' ModuleSpecifier ';' |
| 1410 | // |
| 1411 | // TODO(ES6): implement destructuring ImportSpecifiers |
| 1412 | |
| 1413 | Expect(Token::IMPORT, CHECK_OK); |
| 1414 | ZoneStringList names(1); |
| 1415 | |
| 1416 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1417 | names.Add(name); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1418 | while (peek() == Token::COMMA) { |
| 1419 | Consume(Token::COMMA); |
| 1420 | name = ParseIdentifierName(CHECK_OK); |
| 1421 | names.Add(name); |
| 1422 | } |
| 1423 | |
| 1424 | ExpectContextualKeyword("from", CHECK_OK); |
| 1425 | Module* module = ParseModuleSpecifier(CHECK_OK); |
| 1426 | ExpectSemicolon(CHECK_OK); |
| 1427 | |
| 1428 | // Generate a separate declaration for each identifier. |
| 1429 | // TODO(ES6): once we implement destructuring, make that one declaration. |
| 1430 | Block* block = factory()->NewBlock(NULL, 1, true); |
| 1431 | for (int i = 0; i < names.length(); ++i) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1432 | #ifdef DEBUG |
| 1433 | if (FLAG_print_interface_details) |
| 1434 | PrintF("# Import %s ", names[i]->ToAsciiArray()); |
| 1435 | #endif |
| 1436 | Interface* interface = Interface::NewUnknown(); |
| 1437 | module->interface()->Add(names[i], interface, ok); |
| 1438 | if (!*ok) { |
| 1439 | #ifdef DEBUG |
| 1440 | if (FLAG_print_interfaces) { |
| 1441 | PrintF("IMPORT TYPE ERROR at '%s'\n", names[i]->ToAsciiArray()); |
| 1442 | PrintF("module: "); |
| 1443 | module->interface()->Print(); |
| 1444 | } |
| 1445 | #endif |
| 1446 | ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1)); |
| 1447 | return NULL; |
| 1448 | } |
| 1449 | VariableProxy* proxy = NewUnresolved(names[i], LET, interface); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1450 | Declaration* declaration = |
| 1451 | factory()->NewImportDeclaration(proxy, module, top_scope_); |
| 1452 | Declare(declaration, true, CHECK_OK); |
| 1453 | // TODO(rossberg): Add initialization statement to block. |
| 1454 | } |
| 1455 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1456 | return block; |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | Statement* Parser::ParseExportDeclaration(bool* ok) { |
| 1461 | // ExportDeclaration: |
| 1462 | // 'export' Identifier (',' Identifier)* ';' |
| 1463 | // 'export' VariableDeclaration |
| 1464 | // 'export' FunctionDeclaration |
| 1465 | // 'export' ModuleDeclaration |
| 1466 | // |
| 1467 | // TODO(ES6): implement structuring ExportSpecifiers |
| 1468 | |
| 1469 | Expect(Token::EXPORT, CHECK_OK); |
| 1470 | |
| 1471 | Statement* result = NULL; |
| 1472 | ZoneStringList names(1); |
| 1473 | switch (peek()) { |
| 1474 | case Token::IDENTIFIER: { |
| 1475 | Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1476 | // Handle 'module' as a context-sensitive keyword. |
| 1477 | if (!name->IsEqualTo(CStrVector("module"))) { |
| 1478 | names.Add(name); |
| 1479 | while (peek() == Token::COMMA) { |
| 1480 | Consume(Token::COMMA); |
| 1481 | name = ParseIdentifier(CHECK_OK); |
| 1482 | names.Add(name); |
| 1483 | } |
| 1484 | ExpectSemicolon(CHECK_OK); |
| 1485 | result = factory()->NewEmptyStatement(); |
| 1486 | } else { |
| 1487 | result = ParseModuleDeclaration(&names, CHECK_OK); |
| 1488 | } |
| 1489 | break; |
| 1490 | } |
| 1491 | |
| 1492 | case Token::FUNCTION: |
| 1493 | result = ParseFunctionDeclaration(&names, CHECK_OK); |
| 1494 | break; |
| 1495 | |
| 1496 | case Token::VAR: |
| 1497 | case Token::LET: |
| 1498 | case Token::CONST: |
| 1499 | result = ParseVariableStatement(kModuleElement, &names, CHECK_OK); |
| 1500 | break; |
| 1501 | |
| 1502 | default: |
| 1503 | *ok = false; |
| 1504 | ReportUnexpectedToken(scanner().current_token()); |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1508 | // Extract declared names into export declarations and interface. |
| 1509 | Interface* interface = top_scope_->interface(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1510 | for (int i = 0; i < names.length(); ++i) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1511 | #ifdef DEBUG |
| 1512 | if (FLAG_print_interface_details) |
| 1513 | PrintF("# Export %s ", names[i]->ToAsciiArray()); |
| 1514 | #endif |
| 1515 | Interface* inner = Interface::NewUnknown(); |
| 1516 | interface->Add(names[i], inner, CHECK_OK); |
| 1517 | VariableProxy* proxy = NewUnresolved(names[i], LET, inner); |
| 1518 | USE(proxy); |
| 1519 | // TODO(rossberg): Rethink whether we actually need to store export |
| 1520 | // declarations (for compilation?). |
| 1521 | // ExportDeclaration* declaration = |
| 1522 | // factory()->NewExportDeclaration(proxy, top_scope_); |
| 1523 | // top_scope_->AddDeclaration(declaration); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | ASSERT(result != NULL); |
| 1527 | return result; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | |
| 1531 | Statement* Parser::ParseBlockElement(ZoneStringList* labels, |
| 1532 | bool* ok) { |
| 1533 | // (Ecma 262 5th Edition, clause 14): |
| 1534 | // SourceElement: |
| 1535 | // Statement |
| 1536 | // FunctionDeclaration |
| 1537 | // |
| 1538 | // In harmony mode we allow additionally the following productions |
| 1539 | // BlockElement (aka SourceElement): |
| 1540 | // LetDeclaration |
| 1541 | // ConstDeclaration |
| 1542 | |
| 1543 | switch (peek()) { |
| 1544 | case Token::FUNCTION: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1545 | return ParseFunctionDeclaration(NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1546 | case Token::LET: |
| 1547 | case Token::CONST: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1548 | return ParseVariableStatement(kModuleElement, NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1549 | default: |
| 1550 | return ParseStatement(labels, ok); |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1555 | Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) { |
| 1556 | // Statement :: |
| 1557 | // Block |
| 1558 | // VariableStatement |
| 1559 | // EmptyStatement |
| 1560 | // ExpressionStatement |
| 1561 | // IfStatement |
| 1562 | // IterationStatement |
| 1563 | // ContinueStatement |
| 1564 | // BreakStatement |
| 1565 | // ReturnStatement |
| 1566 | // WithStatement |
| 1567 | // LabelledStatement |
| 1568 | // SwitchStatement |
| 1569 | // ThrowStatement |
| 1570 | // TryStatement |
| 1571 | // DebuggerStatement |
| 1572 | |
| 1573 | // Note: Since labels can only be used by 'break' and 'continue' |
| 1574 | // statements, which themselves are only valid within blocks, |
| 1575 | // iterations or 'switch' statements (i.e., BreakableStatements), |
| 1576 | // labels can be simply ignored in all other cases; except for |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1577 | // trivial labeled break statements 'label: break label' which is |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1578 | // parsed into an empty statement. |
| 1579 | |
| 1580 | // Keep the source position of the statement |
| 1581 | int statement_pos = scanner().peek_location().beg_pos; |
| 1582 | Statement* stmt = NULL; |
| 1583 | switch (peek()) { |
| 1584 | case Token::LBRACE: |
| 1585 | return ParseBlock(labels, ok); |
| 1586 | |
| 1587 | case Token::CONST: // fall through |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1588 | case Token::LET: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1589 | case Token::VAR: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1590 | stmt = ParseVariableStatement(kStatement, NULL, ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1591 | break; |
| 1592 | |
| 1593 | case Token::SEMICOLON: |
| 1594 | Next(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1595 | return factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1596 | |
| 1597 | case Token::IF: |
| 1598 | stmt = ParseIfStatement(labels, ok); |
| 1599 | break; |
| 1600 | |
| 1601 | case Token::DO: |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1602 | stmt = ParseDoWhileStatement(labels, ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | |
| 1605 | case Token::WHILE: |
| 1606 | stmt = ParseWhileStatement(labels, ok); |
| 1607 | break; |
| 1608 | |
| 1609 | case Token::FOR: |
| 1610 | stmt = ParseForStatement(labels, ok); |
| 1611 | break; |
| 1612 | |
| 1613 | case Token::CONTINUE: |
| 1614 | stmt = ParseContinueStatement(ok); |
| 1615 | break; |
| 1616 | |
| 1617 | case Token::BREAK: |
| 1618 | stmt = ParseBreakStatement(labels, ok); |
| 1619 | break; |
| 1620 | |
| 1621 | case Token::RETURN: |
| 1622 | stmt = ParseReturnStatement(ok); |
| 1623 | break; |
| 1624 | |
| 1625 | case Token::WITH: |
| 1626 | stmt = ParseWithStatement(labels, ok); |
| 1627 | break; |
| 1628 | |
| 1629 | case Token::SWITCH: |
| 1630 | stmt = ParseSwitchStatement(labels, ok); |
| 1631 | break; |
| 1632 | |
| 1633 | case Token::THROW: |
| 1634 | stmt = ParseThrowStatement(ok); |
| 1635 | break; |
| 1636 | |
| 1637 | case Token::TRY: { |
| 1638 | // NOTE: It is somewhat complicated to have labels on |
| 1639 | // try-statements. When breaking out of a try-finally statement, |
| 1640 | // one must take great care not to treat it as a |
| 1641 | // fall-through. It is much easier just to wrap the entire |
| 1642 | // try-statement in a statement block and put the labels there |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1643 | Block* result = factory()->NewBlock(labels, 1, false); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1644 | Target target(&this->target_stack_, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1645 | TryStatement* statement = ParseTryStatement(CHECK_OK); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1646 | if (statement) { |
| 1647 | statement->set_statement_pos(statement_pos); |
| 1648 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1649 | if (result) result->AddStatement(statement); |
| 1650 | return result; |
| 1651 | } |
| 1652 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1653 | case Token::FUNCTION: { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1654 | // FunctionDeclaration is only allowed in the context of SourceElements |
| 1655 | // (Ecma 262 5th Edition, clause 14): |
| 1656 | // SourceElement: |
| 1657 | // Statement |
| 1658 | // FunctionDeclaration |
| 1659 | // Common language extension is to allow function declaration in place |
| 1660 | // of any statement. This language extension is disabled in strict mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1661 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1662 | ReportMessageAt(scanner().peek_location(), "strict_function", |
| 1663 | Vector<const char*>::empty()); |
| 1664 | *ok = false; |
| 1665 | return NULL; |
| 1666 | } |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1667 | return ParseFunctionDeclaration(NULL, ok); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1668 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1669 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1670 | case Token::DEBUGGER: |
| 1671 | stmt = ParseDebuggerStatement(ok); |
| 1672 | break; |
| 1673 | |
| 1674 | default: |
| 1675 | stmt = ParseExpressionOrLabelledStatement(labels, ok); |
| 1676 | } |
| 1677 | |
| 1678 | // Store the source position of the statement |
| 1679 | if (stmt != NULL) stmt->set_statement_pos(statement_pos); |
| 1680 | return stmt; |
| 1681 | } |
| 1682 | |
| 1683 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1684 | VariableProxy* Parser::NewUnresolved( |
| 1685 | Handle<String> name, VariableMode mode, Interface* interface) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1686 | // If we are inside a function, a declaration of a var/const variable is a |
| 1687 | // truly local variable, and the scope of the variable is always the function |
| 1688 | // scope. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1689 | // Let/const variables in harmony mode are always added to the immediately |
| 1690 | // enclosing scope. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1691 | return DeclarationScope(mode)->NewUnresolved( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1692 | factory(), name, scanner().location().beg_pos, interface); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | void Parser::Declare(Declaration* declaration, bool resolve, bool* ok) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1697 | VariableProxy* proxy = declaration->proxy(); |
| 1698 | Handle<String> name = proxy->name(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1699 | VariableMode mode = declaration->mode(); |
| 1700 | Scope* declaration_scope = DeclarationScope(mode); |
| 1701 | Variable* var = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1702 | |
| 1703 | // If a function scope exists, then we can statically declare this |
| 1704 | // variable and also set its mode. In any case, a Declaration node |
| 1705 | // will be added to the scope so that the declaration can be added |
| 1706 | // to the corresponding activation frame at runtime if necessary. |
| 1707 | // For instance declarations inside an eval scope need to be added |
| 1708 | // to the calling function context. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 1709 | // Similarly, strict mode eval scope does not leak variable declarations to |
| 1710 | // the caller's scope so we declare all locals, too. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1711 | // Also for block scoped let/const bindings the variable can be |
| 1712 | // statically declared. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1713 | if (declaration_scope->is_function_scope() || |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1714 | declaration_scope->is_strict_or_extended_eval_scope() || |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1715 | declaration_scope->is_block_scope() || |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1716 | declaration_scope->is_module_scope() || |
| 1717 | declaration->AsModuleDeclaration() != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1718 | // Declare the variable in the function scope. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1719 | var = declaration_scope->LocalLookup(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1720 | if (var == NULL) { |
| 1721 | // Declare the name. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1722 | var = declaration_scope->DeclareLocal( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1723 | name, mode, declaration->initialization(), proxy->interface()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1724 | } else { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1725 | // The name was declared in this scope before; check for conflicting |
| 1726 | // re-declarations. We have a conflict if either of the declarations is |
| 1727 | // not a var. There is similar code in runtime.cc in the Declare |
| 1728 | // functions. The function CheckNonConflictingScope checks for conflicting |
| 1729 | // var and let bindings from different scopes whereas this is a check for |
| 1730 | // conflicting declarations within the same scope. This check also covers |
| 1731 | // |
| 1732 | // function () { let x; { var x; } } |
| 1733 | // |
| 1734 | // because the var declaration is hoisted to the function scope where 'x' |
| 1735 | // is already bound. |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1736 | if ((mode != VAR) || (var->mode() != VAR)) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1737 | // We only have vars, consts and lets in declarations. |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1738 | ASSERT(var->mode() == VAR || |
| 1739 | var->mode() == CONST || |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1740 | var->mode() == CONST_HARMONY || |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1741 | var->mode() == LET); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1742 | if (is_extended_mode()) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1743 | // In harmony mode we treat re-declarations as early errors. See |
| 1744 | // ES5 16 for a definition of early errors. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1745 | SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1746 | const char* elms[2] = { "Variable", *c_string }; |
| 1747 | Vector<const char*> args(elms, 2); |
| 1748 | ReportMessage("redeclaration", args); |
| 1749 | *ok = false; |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1750 | return; |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1751 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1752 | const char* type = (var->mode() == VAR) |
| 1753 | ? "var" : var->is_const_mode() ? "const" : "let"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1754 | Handle<String> type_string = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1755 | isolate()->factory()->NewStringFromUtf8(CStrVector(type), TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1756 | Expression* expression = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1757 | NewThrowTypeError(isolate()->factory()->redeclaration_symbol(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1758 | type_string, name); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1759 | declaration_scope->SetIllegalRedeclaration(expression); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // We add a declaration node for every declaration. The compiler |
| 1765 | // will only generate code if necessary. In particular, declarations |
| 1766 | // for inner local variables that do not represent functions won't |
| 1767 | // result in any generated code. |
| 1768 | // |
| 1769 | // Note that we always add an unresolved proxy even if it's not |
| 1770 | // used, simply because we don't know in this method (w/o extra |
| 1771 | // parameters) if the proxy is needed or not. The proxy will be |
| 1772 | // bound during variable resolution time unless it was pre-bound |
| 1773 | // below. |
| 1774 | // |
| 1775 | // WARNING: This will lead to multiple declaration nodes for the |
| 1776 | // same variable if it is declared several times. This is not a |
| 1777 | // semantic issue as long as we keep the source order, but it may be |
| 1778 | // a performance issue since it may lead to repeated |
| 1779 | // Runtime::DeclareContextSlot() calls. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1780 | declaration_scope->AddDeclaration(declaration); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1781 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1782 | if ((mode == CONST || mode == CONST_HARMONY) && |
| 1783 | declaration_scope->is_global_scope()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1784 | // For global const variables we bind the proxy to a variable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1785 | ASSERT(resolve); // should be set by all callers |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1786 | Variable::Kind kind = Variable::NORMAL; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1787 | var = new(zone()) Variable(declaration_scope, |
| 1788 | name, |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1789 | mode, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1790 | true, |
| 1791 | kind, |
| 1792 | kNeedsInitialization); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1793 | } else if (declaration_scope->is_eval_scope() && |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1794 | declaration_scope->is_classic_mode()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1795 | // For variable declarations in a non-strict eval scope the proxy is bound |
| 1796 | // to a lookup variable to force a dynamic declaration using the |
| 1797 | // DeclareContextSlot runtime function. |
| 1798 | Variable::Kind kind = Variable::NORMAL; |
| 1799 | var = new(zone()) Variable(declaration_scope, |
| 1800 | name, |
| 1801 | mode, |
| 1802 | true, |
| 1803 | kind, |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1804 | declaration->initialization()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1805 | var->AllocateTo(Variable::LOOKUP, -1); |
| 1806 | resolve = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | // If requested and we have a local variable, bind the proxy to the variable |
| 1810 | // at parse-time. This is used for functions (and consts) declared inside |
| 1811 | // statements: the corresponding function (or const) variable must be in the |
| 1812 | // function scope and not a statement-local scope, e.g. as provided with a |
| 1813 | // 'with' statement: |
| 1814 | // |
| 1815 | // with (obj) { |
| 1816 | // function f() {} |
| 1817 | // } |
| 1818 | // |
| 1819 | // which is translated into: |
| 1820 | // |
| 1821 | // with (obj) { |
| 1822 | // // in this case this is not: 'var f; f = function () {};' |
| 1823 | // var f = function () {}; |
| 1824 | // } |
| 1825 | // |
| 1826 | // Note that if 'f' is accessed from inside the 'with' statement, it |
| 1827 | // will be allocated in the context (because we must be able to look |
| 1828 | // it up dynamically) but it will also be accessed statically, i.e., |
| 1829 | // with a context slot index and a context chain length for this |
| 1830 | // initialization code. Thus, inside the 'with' statement, we need |
| 1831 | // both access to the static and the dynamic context chain; the |
| 1832 | // runtime needs to provide both. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 1833 | if (resolve && var != NULL) { |
| 1834 | proxy->BindTo(var); |
| 1835 | |
| 1836 | if (FLAG_harmony_modules) { |
| 1837 | bool ok; |
| 1838 | #ifdef DEBUG |
| 1839 | if (FLAG_print_interface_details) |
| 1840 | PrintF("# Declare %s\n", var->name()->ToAsciiArray()); |
| 1841 | #endif |
| 1842 | proxy->interface()->Unify(var->interface(), &ok); |
| 1843 | if (!ok) { |
| 1844 | #ifdef DEBUG |
| 1845 | if (FLAG_print_interfaces) { |
| 1846 | PrintF("DECLARE TYPE ERROR\n"); |
| 1847 | PrintF("proxy: "); |
| 1848 | proxy->interface()->Print(); |
| 1849 | PrintF("var: "); |
| 1850 | var->interface()->Print(); |
| 1851 | } |
| 1852 | #endif |
| 1853 | ReportMessage("module_type_error", Vector<Handle<String> >(&name, 1)); |
| 1854 | } |
| 1855 | } |
| 1856 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | // Language extension which is only enabled for source files loaded |
| 1861 | // through the API's extension mechanism. A native function |
| 1862 | // declaration is resolved by looking up the function through a |
| 1863 | // callback provided by the extension. |
| 1864 | Statement* Parser::ParseNativeDeclaration(bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1865 | Expect(Token::FUNCTION, CHECK_OK); |
| 1866 | Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1867 | Expect(Token::LPAREN, CHECK_OK); |
| 1868 | bool done = (peek() == Token::RPAREN); |
| 1869 | while (!done) { |
| 1870 | ParseIdentifier(CHECK_OK); |
| 1871 | done = (peek() == Token::RPAREN); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 1872 | if (!done) { |
| 1873 | Expect(Token::COMMA, CHECK_OK); |
| 1874 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | } |
| 1876 | Expect(Token::RPAREN, CHECK_OK); |
| 1877 | Expect(Token::SEMICOLON, CHECK_OK); |
| 1878 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | // Make sure that the function containing the native declaration |
| 1880 | // isn't lazily compiled. The extension structures are only |
| 1881 | // accessible while parsing the first time not when reparsing |
| 1882 | // because of lazy compilation. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1883 | DeclarationScope(VAR)->ForceEagerCompilation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | |
| 1885 | // Compute the function template for the native function. |
| 1886 | v8::Handle<v8::FunctionTemplate> fun_template = |
| 1887 | extension_->GetNativeFunction(v8::Utils::ToLocal(name)); |
| 1888 | ASSERT(!fun_template.IsEmpty()); |
| 1889 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1890 | // Instantiate the function and create a shared function info from it. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); |
| 1892 | const int literals = fun->NumberOfLiterals(); |
| 1893 | Handle<Code> code = Handle<Code>(fun->shared()->code()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1894 | Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 1895 | Handle<SharedFunctionInfo> shared = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1896 | isolate()->factory()->NewSharedFunctionInfo(name, literals, code, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1897 | Handle<ScopeInfo>(fun->shared()->scope_info())); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1898 | shared->set_construct_stub(*construct_stub); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1899 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1900 | // Copy the function data to the shared function info. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1901 | shared->set_function_data(fun->shared()->function_data()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1902 | int parameters = fun->shared()->formal_parameter_count(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1903 | shared->set_formal_parameter_count(parameters); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1904 | |
| 1905 | // TODO(1240846): It's weird that native function declarations are |
| 1906 | // introduced dynamically when we meet their declarations, whereas |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1907 | // other functions are set up when entering the surrounding scope. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1908 | VariableProxy* proxy = NewUnresolved(name, VAR); |
| 1909 | Declaration* declaration = |
| 1910 | factory()->NewVariableDeclaration(proxy, VAR, top_scope_); |
| 1911 | Declare(declaration, true, CHECK_OK); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1912 | SharedFunctionInfoLiteral* lit = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1913 | factory()->NewSharedFunctionInfoLiteral(shared); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1914 | return factory()->NewExpressionStatement( |
| 1915 | factory()->NewAssignment( |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1916 | Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1920 | Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1921 | // FunctionDeclaration :: |
| 1922 | // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | Expect(Token::FUNCTION, CHECK_OK); |
| 1924 | int function_token_position = scanner().location().beg_pos; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1925 | bool is_strict_reserved = false; |
| 1926 | Handle<String> name = ParseIdentifierOrStrictReservedWord( |
| 1927 | &is_strict_reserved, CHECK_OK); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1928 | FunctionLiteral* fun = ParseFunctionLiteral(name, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1929 | is_strict_reserved, |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1930 | function_token_position, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1931 | FunctionLiteral::DECLARATION, |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1932 | CHECK_OK); |
| 1933 | // Even if we're not at the top-level of the global or a function |
| 1934 | // scope, we treat is as such and introduce the function with it's |
| 1935 | // initial value upon entering the corresponding scope. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1936 | VariableMode mode = is_extended_mode() ? LET : VAR; |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1937 | VariableProxy* proxy = NewUnresolved(name, mode); |
| 1938 | Declaration* declaration = |
| 1939 | factory()->NewFunctionDeclaration(proxy, mode, fun, top_scope_); |
| 1940 | Declare(declaration, true, CHECK_OK); |
| 1941 | if (names) names->Add(name); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1942 | return factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | |
| 1946 | Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1947 | if (top_scope_->is_extended_mode()) return ParseScopedBlock(labels, ok); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1948 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | // Block :: |
| 1950 | // '{' Statement* '}' |
| 1951 | |
| 1952 | // Note that a Block does not introduce a new execution scope! |
| 1953 | // (ECMA-262, 3rd, 12.2) |
| 1954 | // |
| 1955 | // Construct block expecting 16 statements. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1956 | Block* result = factory()->NewBlock(labels, 16, false); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1957 | Target target(&this->target_stack_, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1958 | Expect(Token::LBRACE, CHECK_OK); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1959 | InitializationBlockFinder block_finder(top_scope_, target_stack_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1960 | while (peek() != Token::RBRACE) { |
| 1961 | Statement* stat = ParseStatement(NULL, CHECK_OK); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1962 | if (stat && !stat->IsEmpty()) { |
| 1963 | result->AddStatement(stat); |
| 1964 | block_finder.Update(stat); |
| 1965 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1966 | } |
| 1967 | Expect(Token::RBRACE, CHECK_OK); |
| 1968 | return result; |
| 1969 | } |
| 1970 | |
| 1971 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1972 | Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1973 | // The harmony mode uses block elements instead of statements. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1974 | // |
| 1975 | // Block :: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1976 | // '{' BlockElement* '}' |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1977 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1978 | // Construct block expecting 16 statements. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1979 | Block* body = factory()->NewBlock(labels, 16, false); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1980 | Scope* block_scope = NewScope(top_scope_, BLOCK_SCOPE); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1981 | |
| 1982 | // Parse the statements and collect escaping labels. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1983 | Expect(Token::LBRACE, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1984 | block_scope->set_start_position(scanner().location().beg_pos); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1985 | { BlockState block_state(this, block_scope); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1986 | TargetCollector collector; |
| 1987 | Target target(&this->target_stack_, &collector); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1988 | Target target_body(&this->target_stack_, body); |
| 1989 | InitializationBlockFinder block_finder(top_scope_, target_stack_); |
| 1990 | |
| 1991 | while (peek() != Token::RBRACE) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1992 | Statement* stat = ParseBlockElement(NULL, CHECK_OK); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1993 | if (stat && !stat->IsEmpty()) { |
| 1994 | body->AddStatement(stat); |
| 1995 | block_finder.Update(stat); |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | Expect(Token::RBRACE, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2000 | block_scope->set_end_position(scanner().location().end_pos); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 2001 | block_scope = block_scope->FinalizeBlockScope(); |
| 2002 | body->set_block_scope(block_scope); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 2003 | return body; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2007 | Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context, |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2008 | ZoneStringList* names, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2009 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | // VariableStatement :: |
| 2011 | // VariableDeclarations ';' |
| 2012 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2013 | Handle<String> ignore; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2014 | Block* result = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2015 | ParseVariableDeclarations(var_context, NULL, names, &ignore, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2016 | ExpectSemicolon(CHECK_OK); |
| 2017 | return result; |
| 2018 | } |
| 2019 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2020 | |
| 2021 | bool Parser::IsEvalOrArguments(Handle<String> string) { |
| 2022 | return string.is_identical_to(isolate()->factory()->eval_symbol()) || |
| 2023 | string.is_identical_to(isolate()->factory()->arguments_symbol()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2024 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2025 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2026 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2027 | // If the variable declaration declares exactly one non-const |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 2028 | // variable, then *out is set to that variable. In all other cases, |
| 2029 | // *out is untouched; in particular, it is the caller's responsibility |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2030 | // to initialize it properly. This mechanism is used for the parsing |
| 2031 | // of 'for-in' loops. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2032 | Block* Parser::ParseVariableDeclarations( |
| 2033 | VariableDeclarationContext var_context, |
| 2034 | VariableDeclarationProperties* decl_props, |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2035 | ZoneStringList* names, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2036 | Handle<String>* out, |
| 2037 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2038 | // VariableDeclarations :: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2039 | // ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[','] |
| 2040 | // |
| 2041 | // The ES6 Draft Rev3 specifies the following grammar for const declarations |
| 2042 | // |
| 2043 | // ConstDeclaration :: |
| 2044 | // const ConstBinding (',' ConstBinding)* ';' |
| 2045 | // ConstBinding :: |
| 2046 | // Identifier '=' AssignmentExpression |
| 2047 | // |
| 2048 | // TODO(ES6): |
| 2049 | // ConstBinding :: |
| 2050 | // BindingPattern '=' AssignmentExpression |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2051 | VariableMode mode = VAR; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2052 | // True if the binding needs initialization. 'let' and 'const' declared |
| 2053 | // bindings are created uninitialized by their declaration nodes and |
| 2054 | // need initialization. 'var' declared bindings are always initialized |
| 2055 | // immediately by their declaration nodes. |
| 2056 | bool needs_init = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | bool is_const = false; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2058 | Token::Value init_op = Token::INIT_VAR; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2059 | if (peek() == Token::VAR) { |
| 2060 | Consume(Token::VAR); |
| 2061 | } else if (peek() == Token::CONST) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2062 | // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: |
| 2063 | // |
| 2064 | // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' |
| 2065 | // |
| 2066 | // * It is a Syntax Error if the code that matches this production is not |
| 2067 | // contained in extended code. |
| 2068 | // |
| 2069 | // However disallowing const in classic mode will break compatibility with |
| 2070 | // existing pages. Therefore we keep allowing const with the old |
| 2071 | // non-harmony semantics in classic mode. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2072 | Consume(Token::CONST); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2073 | switch (top_scope_->language_mode()) { |
| 2074 | case CLASSIC_MODE: |
| 2075 | mode = CONST; |
| 2076 | init_op = Token::INIT_CONST; |
| 2077 | break; |
| 2078 | case STRICT_MODE: |
| 2079 | ReportMessage("strict_const", Vector<const char*>::empty()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2080 | *ok = false; |
| 2081 | return NULL; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2082 | case EXTENDED_MODE: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2083 | if (var_context == kStatement) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2084 | // In extended mode 'const' declarations are only allowed in source |
| 2085 | // element positions. |
| 2086 | ReportMessage("unprotected_const", Vector<const char*>::empty()); |
| 2087 | *ok = false; |
| 2088 | return NULL; |
| 2089 | } |
| 2090 | mode = CONST_HARMONY; |
| 2091 | init_op = Token::INIT_CONST_HARMONY; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2092 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2093 | is_const = true; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2094 | needs_init = true; |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2095 | } else if (peek() == Token::LET) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2096 | // ES6 Draft Rev4 section 12.2.1: |
| 2097 | // |
| 2098 | // LetDeclaration : let LetBindingList ; |
| 2099 | // |
| 2100 | // * It is a Syntax Error if the code that matches this production is not |
| 2101 | // contained in extended code. |
| 2102 | if (!is_extended_mode()) { |
| 2103 | ReportMessage("illegal_let", Vector<const char*>::empty()); |
| 2104 | *ok = false; |
| 2105 | return NULL; |
| 2106 | } |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2107 | Consume(Token::LET); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2108 | if (var_context == kStatement) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2109 | // Let declarations are only allowed in source element positions. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2110 | ReportMessage("unprotected_let", Vector<const char*>::empty()); |
| 2111 | *ok = false; |
| 2112 | return NULL; |
| 2113 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2114 | mode = LET; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2115 | needs_init = true; |
| 2116 | init_op = Token::INIT_LET; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2117 | } else { |
| 2118 | UNREACHABLE(); // by current callers |
| 2119 | } |
| 2120 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2121 | Scope* declaration_scope = DeclarationScope(mode); |
| 2122 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2123 | // The scope of a var/const declared variable anywhere inside a function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2124 | // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2125 | // transform a source-level var/const declaration into a (Function) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | // Scope declaration, and rewrite the source-level initialization into an |
| 2127 | // assignment statement. We use a block to collect multiple assignments. |
| 2128 | // |
| 2129 | // We mark the block as initializer block because we don't want the |
| 2130 | // rewriter to add a '.result' assignment to such a block (to get compliant |
| 2131 | // behavior for code such as print(eval('var x = 7')), and for cosmetic |
| 2132 | // reasons when pretty-printing. Also, unless an assignment (initialization) |
| 2133 | // is inside an initializer block, it is ignored. |
| 2134 | // |
| 2135 | // Create new block with one expected declaration. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2136 | Block* block = factory()->NewBlock(NULL, 1, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2137 | int nvars = 0; // the number of variables declared |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2138 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2139 | do { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2140 | if (fni_ != NULL) fni_->Enter(); |
| 2141 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2142 | // Parse variable name. |
| 2143 | if (nvars > 0) Consume(Token::COMMA); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2144 | name = ParseIdentifier(CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2145 | if (fni_ != NULL) fni_->PushVariableName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2146 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2147 | // Strict mode variables may not be named eval or arguments |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2148 | if (!declaration_scope->is_classic_mode() && IsEvalOrArguments(name)) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2149 | ReportMessage("strict_var_name", Vector<const char*>::empty()); |
| 2150 | *ok = false; |
| 2151 | return NULL; |
| 2152 | } |
| 2153 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2154 | // Declare variable. |
| 2155 | // Note that we *always* must treat the initial value via a separate init |
| 2156 | // assignment for variables and constants because the value must be assigned |
| 2157 | // when the variable is encountered in the source. But the variable/constant |
| 2158 | // is declared (and set to 'undefined') upon entering the function within |
| 2159 | // which the variable or constant is declared. Only function variables have |
| 2160 | // an initial value in the declaration (because they are initialized upon |
| 2161 | // entering the function). |
| 2162 | // |
| 2163 | // If we have a const declaration, in an inner scope, the proxy is always |
| 2164 | // bound to the declared variable (independent of possibly surrounding with |
| 2165 | // statements). |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2166 | // For let/const declarations in harmony mode, we can also immediately |
| 2167 | // pre-resolve the proxy because it resides in the same scope as the |
| 2168 | // declaration. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2169 | VariableProxy* proxy = NewUnresolved(name, mode); |
| 2170 | Declaration* declaration = |
| 2171 | factory()->NewVariableDeclaration(proxy, mode, top_scope_); |
| 2172 | Declare(declaration, mode != VAR, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2173 | nvars++; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2174 | if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 2175 | ReportMessageAt(scanner().location(), "too_many_variables", |
| 2176 | Vector<const char*>::empty()); |
| 2177 | *ok = false; |
| 2178 | return NULL; |
| 2179 | } |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2180 | if (names) names->Add(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2181 | |
| 2182 | // Parse initialization expression if present and/or needed. A |
| 2183 | // declaration of the form: |
| 2184 | // |
| 2185 | // var v = x; |
| 2186 | // |
| 2187 | // is syntactic sugar for: |
| 2188 | // |
| 2189 | // var v; v = x; |
| 2190 | // |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2191 | // In particular, we need to re-lookup 'v' (in top_scope_, not |
| 2192 | // declaration_scope) as it may be a different 'v' than the 'v' in the |
| 2193 | // declaration (e.g., if we are inside a 'with' statement or 'catch' |
| 2194 | // block). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | // |
| 2196 | // However, note that const declarations are different! A const |
| 2197 | // declaration of the form: |
| 2198 | // |
| 2199 | // const c = x; |
| 2200 | // |
| 2201 | // is *not* syntactic sugar for: |
| 2202 | // |
| 2203 | // const c; c = x; |
| 2204 | // |
| 2205 | // The "variable" c initialized to x is the same as the declared |
| 2206 | // one - there is no re-lookup (see the last parameter of the |
| 2207 | // Declare() call above). |
| 2208 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2209 | Scope* initialization_scope = is_const ? declaration_scope : top_scope_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2210 | Expression* value = NULL; |
| 2211 | int position = -1; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2212 | // Harmony consts have non-optional initializers. |
| 2213 | if (peek() == Token::ASSIGN || mode == CONST_HARMONY) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2214 | Expect(Token::ASSIGN, CHECK_OK); |
| 2215 | position = scanner().location().beg_pos; |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2216 | value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2217 | // Don't infer if it is "a = function(){...}();"-like expression. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2218 | if (fni_ != NULL && |
| 2219 | value->AsCall() == NULL && |
| 2220 | value->AsCallNew() == NULL) { |
| 2221 | fni_->Infer(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2222 | } else { |
| 2223 | fni_->RemoveLastFunction(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2224 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2225 | if (decl_props != NULL) *decl_props = kHasInitializers; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2228 | // Record the end position of the initializer. |
| 2229 | if (proxy->var() != NULL) { |
| 2230 | proxy->var()->set_initializer_position(scanner().location().end_pos); |
| 2231 | } |
| 2232 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2233 | // Make sure that 'const x' and 'let x' initialize 'x' to undefined. |
| 2234 | if (value == NULL && needs_init) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2235 | value = GetLiteralUndefined(); |
| 2236 | } |
| 2237 | |
| 2238 | // Global variable declarations must be compiled in a specific |
| 2239 | // way. When the script containing the global variable declaration |
| 2240 | // is entered, the global variable must be declared, so that if it |
| 2241 | // doesn't exist (not even in a prototype of the global object) it |
| 2242 | // gets created with an initial undefined value. This is handled |
| 2243 | // by the declarations part of the function representing the |
| 2244 | // top-level global code; see Runtime::DeclareGlobalVariable. If |
| 2245 | // it already exists (in the object or in a prototype), it is |
| 2246 | // *not* touched until the variable declaration statement is |
| 2247 | // executed. |
| 2248 | // |
| 2249 | // Executing the variable declaration statement will always |
| 2250 | // guarantee to give the global object a "local" variable; a |
| 2251 | // variable defined in the global object and not in any |
| 2252 | // prototype. This way, global variable declarations can shadow |
| 2253 | // properties in the prototype chain, but only after the variable |
| 2254 | // declaration statement has been executed. This is important in |
| 2255 | // browsers where the global object (window) has lots of |
| 2256 | // properties defined in prototype objects. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2257 | if (initialization_scope->is_global_scope()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2258 | // Compute the arguments for the runtime call. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 2259 | ZoneList<Expression*>* arguments = new(zone()) ZoneList<Expression*>(3); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2260 | // We have at least 1 parameter. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2261 | arguments->Add(factory()->NewLiteral(name)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2262 | CallRuntime* initialize; |
| 2263 | |
| 2264 | if (is_const) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2265 | arguments->Add(value); |
| 2266 | value = NULL; // zap the value to avoid the unnecessary assignment |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2267 | |
| 2268 | // Construct the call to Runtime_InitializeConstGlobal |
| 2269 | // and add it to the initialization statement block. |
| 2270 | // Note that the function does different things depending on |
| 2271 | // the number of arguments (1 or 2). |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2272 | initialize = factory()->NewCallRuntime( |
| 2273 | isolate()->factory()->InitializeConstGlobal_symbol(), |
| 2274 | Runtime::FunctionForId(Runtime::kInitializeConstGlobal), |
| 2275 | arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | } else { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2277 | // Add strict mode. |
| 2278 | // We may want to pass singleton to avoid Literal allocations. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2279 | LanguageMode language_mode = initialization_scope->language_mode(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2280 | arguments->Add(factory()->NewNumberLiteral(language_mode)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2281 | |
| 2282 | // Be careful not to assign a value to the global variable if |
| 2283 | // we're in a with. The initialization value should not |
| 2284 | // necessarily be stored in the global object in that case, |
| 2285 | // which is why we need to generate a separate assignment node. |
| 2286 | if (value != NULL && !inside_with()) { |
| 2287 | arguments->Add(value); |
| 2288 | value = NULL; // zap the value to avoid the unnecessary assignment |
| 2289 | } |
| 2290 | |
| 2291 | // Construct the call to Runtime_InitializeVarGlobal |
| 2292 | // and add it to the initialization statement block. |
| 2293 | // Note that the function does different things depending on |
| 2294 | // the number of arguments (2 or 3). |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2295 | initialize = factory()->NewCallRuntime( |
| 2296 | isolate()->factory()->InitializeVarGlobal_symbol(), |
| 2297 | Runtime::FunctionForId(Runtime::kInitializeVarGlobal), |
| 2298 | arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2299 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2300 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2301 | block->AddStatement(factory()->NewExpressionStatement(initialize)); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2302 | } else if (needs_init) { |
| 2303 | // Constant initializations always assign to the declared constant which |
| 2304 | // is always at the function scope level. This is only relevant for |
| 2305 | // dynamically looked-up variables and constants (the start context for |
| 2306 | // constant lookups is always the function context, while it is the top |
| 2307 | // context for var declared variables). Sigh... |
| 2308 | // For 'let' and 'const' declared variables in harmony mode the |
| 2309 | // initialization also always assigns to the declared variable. |
| 2310 | ASSERT(proxy != NULL); |
| 2311 | ASSERT(proxy->var() != NULL); |
| 2312 | ASSERT(value != NULL); |
| 2313 | Assignment* assignment = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2314 | factory()->NewAssignment(init_op, proxy, value, position); |
| 2315 | block->AddStatement(factory()->NewExpressionStatement(assignment)); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2316 | value = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 2319 | // Add an assignment node to the initialization statement block if we still |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2320 | // have a pending initialization value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2321 | if (value != NULL) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2322 | ASSERT(mode == VAR); |
| 2323 | // 'var' initializations are simply assignments (with all the consequences |
| 2324 | // if they are inside a 'with' statement - they may change a 'with' object |
| 2325 | // property). |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2326 | VariableProxy* proxy = |
| 2327 | initialization_scope->NewUnresolved(factory(), name); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2328 | Assignment* assignment = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2329 | factory()->NewAssignment(init_op, proxy, value, position); |
| 2330 | block->AddStatement(factory()->NewExpressionStatement(assignment)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2331 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2332 | |
| 2333 | if (fni_ != NULL) fni_->Leave(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2334 | } while (peek() == Token::COMMA); |
| 2335 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2336 | // If there was a single non-const declaration, return it in the output |
| 2337 | // parameter for possible use by for/in. |
| 2338 | if (nvars == 1 && !is_const) { |
| 2339 | *out = name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | return block; |
| 2343 | } |
| 2344 | |
| 2345 | |
| 2346 | static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) { |
| 2347 | ASSERT(!label.is_null()); |
| 2348 | if (labels != NULL) |
| 2349 | for (int i = labels->length(); i-- > 0; ) |
| 2350 | if (labels->at(i).is_identical_to(label)) |
| 2351 | return true; |
| 2352 | |
| 2353 | return false; |
| 2354 | } |
| 2355 | |
| 2356 | |
| 2357 | Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels, |
| 2358 | bool* ok) { |
| 2359 | // ExpressionStatement | LabelledStatement :: |
| 2360 | // Expression ';' |
| 2361 | // Identifier ':' Statement |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2362 | bool starts_with_idenfifier = peek_any_identifier(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2363 | Expression* expr = ParseExpression(true, CHECK_OK); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2364 | if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2365 | expr->AsVariableProxy() != NULL && |
| 2366 | !expr->AsVariableProxy()->is_this()) { |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2367 | // Expression is a single identifier, and not, e.g., a parenthesized |
| 2368 | // identifier. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2369 | VariableProxy* var = expr->AsVariableProxy(); |
| 2370 | Handle<String> label = var->name(); |
| 2371 | // TODO(1240780): We don't check for redeclaration of labels |
| 2372 | // during preparsing since keeping track of the set of active |
| 2373 | // labels requires nontrivial changes to the way scopes are |
| 2374 | // structured. However, these are probably changes we want to |
| 2375 | // make later anyway so we should go back and fix this then. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2376 | if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2377 | SmartArrayPointer<char> c_string = label->ToCString(DISALLOW_NULLS); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2378 | const char* elms[2] = { "Label", *c_string }; |
| 2379 | Vector<const char*> args(elms, 2); |
| 2380 | ReportMessage("redeclaration", args); |
| 2381 | *ok = false; |
| 2382 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2383 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 2384 | if (labels == NULL) labels = new(zone()) ZoneStringList(4); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2385 | labels->Add(label); |
| 2386 | // Remove the "ghost" variable that turned out to be a label |
| 2387 | // from the top scope. This way, we don't try to resolve it |
| 2388 | // during the scope processing. |
| 2389 | top_scope_->RemoveUnresolved(var); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2390 | Expect(Token::COLON, CHECK_OK); |
| 2391 | return ParseStatement(labels, ok); |
| 2392 | } |
| 2393 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2394 | // If we have an extension, we allow a native function declaration. |
| 2395 | // A native function declaration starts with "native function" with |
| 2396 | // no line-terminator between the two words. |
| 2397 | if (extension_ != NULL && |
| 2398 | peek() == Token::FUNCTION && |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2399 | !scanner().HasAnyLineTerminatorBeforeNext() && |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2400 | expr != NULL && |
| 2401 | expr->AsVariableProxy() != NULL && |
| 2402 | expr->AsVariableProxy()->name()->Equals( |
| 2403 | isolate()->heap()->native_symbol()) && |
| 2404 | !scanner().literal_contains_escapes()) { |
| 2405 | return ParseNativeDeclaration(ok); |
| 2406 | } |
| 2407 | |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 2408 | // Parsed expression statement, or the context-sensitive 'module' keyword. |
| 2409 | // Only expect semicolon in the former case. |
| 2410 | if (!FLAG_harmony_modules || |
| 2411 | peek() != Token::IDENTIFIER || |
| 2412 | scanner().HasAnyLineTerminatorBeforeNext() || |
| 2413 | expr->AsVariableProxy() == NULL || |
| 2414 | !expr->AsVariableProxy()->name()->Equals( |
| 2415 | isolate()->heap()->module_symbol()) || |
| 2416 | scanner().literal_contains_escapes()) { |
| 2417 | ExpectSemicolon(CHECK_OK); |
| 2418 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2419 | return factory()->NewExpressionStatement(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | |
| 2423 | IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) { |
| 2424 | // IfStatement :: |
| 2425 | // 'if' '(' Expression ')' Statement ('else' Statement)? |
| 2426 | |
| 2427 | Expect(Token::IF, CHECK_OK); |
| 2428 | Expect(Token::LPAREN, CHECK_OK); |
| 2429 | Expression* condition = ParseExpression(true, CHECK_OK); |
| 2430 | Expect(Token::RPAREN, CHECK_OK); |
| 2431 | Statement* then_statement = ParseStatement(labels, CHECK_OK); |
| 2432 | Statement* else_statement = NULL; |
| 2433 | if (peek() == Token::ELSE) { |
| 2434 | Next(); |
| 2435 | else_statement = ParseStatement(labels, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2436 | } else { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2437 | else_statement = factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2439 | return factory()->NewIfStatement(condition, then_statement, else_statement); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | Statement* Parser::ParseContinueStatement(bool* ok) { |
| 2444 | // ContinueStatement :: |
| 2445 | // 'continue' Identifier? ';' |
| 2446 | |
| 2447 | Expect(Token::CONTINUE, CHECK_OK); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2448 | Handle<String> label = Handle<String>::null(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2449 | Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2450 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2451 | tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2452 | label = ParseIdentifier(CHECK_OK); |
| 2453 | } |
| 2454 | IterationStatement* target = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2455 | target = LookupContinueTarget(label, CHECK_OK); |
| 2456 | if (target == NULL) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2457 | // Illegal continue statement. |
| 2458 | const char* message = "illegal_continue"; |
| 2459 | Vector<Handle<String> > args; |
| 2460 | if (!label.is_null()) { |
| 2461 | message = "unknown_label"; |
| 2462 | args = Vector<Handle<String> >(&label, 1); |
| 2463 | } |
| 2464 | ReportMessageAt(scanner().location(), message, args); |
| 2465 | *ok = false; |
| 2466 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2467 | } |
| 2468 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2469 | return factory()->NewContinueStatement(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) { |
| 2474 | // BreakStatement :: |
| 2475 | // 'break' Identifier? ';' |
| 2476 | |
| 2477 | Expect(Token::BREAK, CHECK_OK); |
| 2478 | Handle<String> label; |
| 2479 | Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2480 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2481 | tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2482 | label = ParseIdentifier(CHECK_OK); |
| 2483 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2484 | // Parse labeled break statements that target themselves into |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2485 | // empty statements, e.g. 'l1: l2: l3: break l2;' |
| 2486 | if (!label.is_null() && ContainsLabel(labels, label)) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2487 | ExpectSemicolon(CHECK_OK); |
| 2488 | return factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2489 | } |
| 2490 | BreakableStatement* target = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2491 | target = LookupBreakTarget(label, CHECK_OK); |
| 2492 | if (target == NULL) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2493 | // Illegal break statement. |
| 2494 | const char* message = "illegal_break"; |
| 2495 | Vector<Handle<String> > args; |
| 2496 | if (!label.is_null()) { |
| 2497 | message = "unknown_label"; |
| 2498 | args = Vector<Handle<String> >(&label, 1); |
| 2499 | } |
| 2500 | ReportMessageAt(scanner().location(), message, args); |
| 2501 | *ok = false; |
| 2502 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2503 | } |
| 2504 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2505 | return factory()->NewBreakStatement(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | Statement* Parser::ParseReturnStatement(bool* ok) { |
| 2510 | // ReturnStatement :: |
| 2511 | // 'return' Expression? ';' |
| 2512 | |
| 2513 | // Consume the return token. It is necessary to do the before |
| 2514 | // reporting any errors on it, because of the way errors are |
| 2515 | // reported (underlining). |
| 2516 | Expect(Token::RETURN, CHECK_OK); |
| 2517 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2518 | Token::Value tok = peek(); |
| 2519 | Statement* result; |
| 2520 | if (scanner().HasAnyLineTerminatorBeforeNext() || |
| 2521 | tok == Token::SEMICOLON || |
| 2522 | tok == Token::RBRACE || |
| 2523 | tok == Token::EOS) { |
| 2524 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2525 | result = factory()->NewReturnStatement(GetLiteralUndefined()); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2526 | } else { |
| 2527 | Expression* expr = ParseExpression(true, CHECK_OK); |
| 2528 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2529 | result = factory()->NewReturnStatement(expr); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2532 | // An ECMAScript program is considered syntactically incorrect if it |
| 2533 | // contains a return statement that is not within the body of a |
| 2534 | // function. See ECMA-262, section 12.9, page 67. |
| 2535 | // |
| 2536 | // To be consistent with KJS we report the syntax error at runtime. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2537 | Scope* declaration_scope = top_scope_->DeclarationScope(); |
| 2538 | if (declaration_scope->is_global_scope() || |
| 2539 | declaration_scope->is_eval_scope()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2540 | Handle<String> type = isolate()->factory()->illegal_return_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2541 | Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2542 | return factory()->NewExpressionStatement(throw_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2543 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2544 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2548 | Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) { |
| 2549 | // WithStatement :: |
| 2550 | // 'with' '(' Expression ')' Statement |
| 2551 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2552 | Expect(Token::WITH, CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2553 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2554 | if (!top_scope_->is_classic_mode()) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2555 | ReportMessage("strict_mode_with", Vector<const char*>::empty()); |
| 2556 | *ok = false; |
| 2557 | return NULL; |
| 2558 | } |
| 2559 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2560 | Expect(Token::LPAREN, CHECK_OK); |
| 2561 | Expression* expr = ParseExpression(true, CHECK_OK); |
| 2562 | Expect(Token::RPAREN, CHECK_OK); |
| 2563 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2564 | top_scope_->DeclarationScope()->RecordWithStatement(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2565 | Scope* with_scope = NewScope(top_scope_, WITH_SCOPE); |
| 2566 | Statement* stmt; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2567 | { BlockState block_state(this, with_scope); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2568 | with_scope->set_start_position(scanner().peek_location().beg_pos); |
| 2569 | stmt = ParseStatement(labels, CHECK_OK); |
| 2570 | with_scope->set_end_position(scanner().location().end_pos); |
| 2571 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2572 | return factory()->NewWithStatement(expr, stmt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | |
| 2576 | CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) { |
| 2577 | // CaseClause :: |
| 2578 | // 'case' Expression ':' Statement* |
| 2579 | // 'default' ':' Statement* |
| 2580 | |
| 2581 | Expression* label = NULL; // NULL expression indicates default case |
| 2582 | if (peek() == Token::CASE) { |
| 2583 | Expect(Token::CASE, CHECK_OK); |
| 2584 | label = ParseExpression(true, CHECK_OK); |
| 2585 | } else { |
| 2586 | Expect(Token::DEFAULT, CHECK_OK); |
| 2587 | if (*default_seen_ptr) { |
| 2588 | ReportMessage("multiple_defaults_in_switch", |
| 2589 | Vector<const char*>::empty()); |
| 2590 | *ok = false; |
| 2591 | return NULL; |
| 2592 | } |
| 2593 | *default_seen_ptr = true; |
| 2594 | } |
| 2595 | Expect(Token::COLON, CHECK_OK); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2596 | int pos = scanner().location().beg_pos; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 2597 | ZoneList<Statement*>* statements = new(zone()) ZoneList<Statement*>(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2598 | while (peek() != Token::CASE && |
| 2599 | peek() != Token::DEFAULT && |
| 2600 | peek() != Token::RBRACE) { |
| 2601 | Statement* stat = ParseStatement(NULL, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2602 | statements->Add(stat); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 2605 | return new(zone()) CaseClause(isolate(), label, statements, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | |
| 2609 | SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels, |
| 2610 | bool* ok) { |
| 2611 | // SwitchStatement :: |
| 2612 | // 'switch' '(' Expression ')' '{' CaseClause* '}' |
| 2613 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2614 | SwitchStatement* statement = factory()->NewSwitchStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2615 | Target target(&this->target_stack_, statement); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2616 | |
| 2617 | Expect(Token::SWITCH, CHECK_OK); |
| 2618 | Expect(Token::LPAREN, CHECK_OK); |
| 2619 | Expression* tag = ParseExpression(true, CHECK_OK); |
| 2620 | Expect(Token::RPAREN, CHECK_OK); |
| 2621 | |
| 2622 | bool default_seen = false; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 2623 | ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2624 | Expect(Token::LBRACE, CHECK_OK); |
| 2625 | while (peek() != Token::RBRACE) { |
| 2626 | CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2627 | cases->Add(clause); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2628 | } |
| 2629 | Expect(Token::RBRACE, CHECK_OK); |
| 2630 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2631 | if (statement) statement->Initialize(tag, cases); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2632 | return statement; |
| 2633 | } |
| 2634 | |
| 2635 | |
| 2636 | Statement* Parser::ParseThrowStatement(bool* ok) { |
| 2637 | // ThrowStatement :: |
| 2638 | // 'throw' Expression ';' |
| 2639 | |
| 2640 | Expect(Token::THROW, CHECK_OK); |
| 2641 | int pos = scanner().location().beg_pos; |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2642 | if (scanner().HasAnyLineTerminatorBeforeNext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2643 | ReportMessage("newline_after_throw", Vector<const char*>::empty()); |
| 2644 | *ok = false; |
| 2645 | return NULL; |
| 2646 | } |
| 2647 | Expression* exception = ParseExpression(true, CHECK_OK); |
| 2648 | ExpectSemicolon(CHECK_OK); |
| 2649 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2650 | return factory()->NewExpressionStatement(factory()->NewThrow(exception, pos)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2654 | TryStatement* Parser::ParseTryStatement(bool* ok) { |
| 2655 | // TryStatement :: |
| 2656 | // 'try' Block Catch |
| 2657 | // 'try' Block Finally |
| 2658 | // 'try' Block Catch Finally |
| 2659 | // |
| 2660 | // Catch :: |
| 2661 | // 'catch' '(' Identifier ')' Block |
| 2662 | // |
| 2663 | // Finally :: |
| 2664 | // 'finally' Block |
| 2665 | |
| 2666 | Expect(Token::TRY, CHECK_OK); |
| 2667 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2668 | TargetCollector try_collector; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2669 | Block* try_block; |
| 2670 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2671 | { Target target(&this->target_stack_, &try_collector); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2672 | try_block = ParseBlock(NULL, CHECK_OK); |
| 2673 | } |
| 2674 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2675 | Token::Value tok = peek(); |
| 2676 | if (tok != Token::CATCH && tok != Token::FINALLY) { |
| 2677 | ReportMessage("no_catch_or_finally", Vector<const char*>::empty()); |
| 2678 | *ok = false; |
| 2679 | return NULL; |
| 2680 | } |
| 2681 | |
| 2682 | // If we can break out from the catch block and there is a finally block, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2683 | // then we will need to collect escaping targets from the catch |
| 2684 | // block. Since we don't know yet if there will be a finally block, we |
| 2685 | // always collect the targets. |
| 2686 | TargetCollector catch_collector; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2687 | Scope* catch_scope = NULL; |
| 2688 | Variable* catch_variable = NULL; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2689 | Block* catch_block = NULL; |
| 2690 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2691 | if (tok == Token::CATCH) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2692 | Consume(Token::CATCH); |
| 2693 | |
| 2694 | Expect(Token::LPAREN, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2695 | catch_scope = NewScope(top_scope_, CATCH_SCOPE); |
| 2696 | catch_scope->set_start_position(scanner().location().beg_pos); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2697 | name = ParseIdentifier(CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2698 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2699 | if (!top_scope_->is_classic_mode() && IsEvalOrArguments(name)) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2700 | ReportMessage("strict_catch_variable", Vector<const char*>::empty()); |
| 2701 | *ok = false; |
| 2702 | return NULL; |
| 2703 | } |
| 2704 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2705 | Expect(Token::RPAREN, CHECK_OK); |
| 2706 | |
| 2707 | if (peek() == Token::LBRACE) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2708 | Target target(&this->target_stack_, &catch_collector); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2709 | VariableMode mode = is_extended_mode() ? LET : VAR; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2710 | catch_variable = |
| 2711 | catch_scope->DeclareLocal(name, mode, kCreatedInitialized); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2712 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2713 | BlockState block_state(this, catch_scope); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2714 | catch_block = ParseBlock(NULL, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2715 | } else { |
| 2716 | Expect(Token::LBRACE, CHECK_OK); |
| 2717 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2718 | catch_scope->set_end_position(scanner().location().end_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2719 | tok = peek(); |
| 2720 | } |
| 2721 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2722 | Block* finally_block = NULL; |
| 2723 | if (tok == Token::FINALLY || catch_block == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2724 | Consume(Token::FINALLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2725 | finally_block = ParseBlock(NULL, CHECK_OK); |
| 2726 | } |
| 2727 | |
| 2728 | // Simplify the AST nodes by converting: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2729 | // 'try B0 catch B1 finally B2' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2730 | // to: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2731 | // 'try { try B0 catch B1 } finally B2' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2732 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2733 | if (catch_block != NULL && finally_block != NULL) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2734 | // If we have both, create an inner try/catch. |
| 2735 | ASSERT(catch_scope != NULL && catch_variable != NULL); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2736 | int index = current_function_state_->NextHandlerIndex(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2737 | TryCatchStatement* statement = factory()->NewTryCatchStatement( |
| 2738 | index, try_block, catch_scope, catch_variable, catch_block); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2739 | statement->set_escaping_targets(try_collector.targets()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2740 | try_block = factory()->NewBlock(NULL, 1, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | try_block->AddStatement(statement); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2742 | catch_block = NULL; // Clear to indicate it's been handled. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | TryStatement* result = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2746 | if (catch_block != NULL) { |
| 2747 | ASSERT(finally_block == NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2748 | ASSERT(catch_scope != NULL && catch_variable != NULL); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2749 | int index = current_function_state_->NextHandlerIndex(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2750 | result = factory()->NewTryCatchStatement( |
| 2751 | index, try_block, catch_scope, catch_variable, catch_block); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2752 | } else { |
| 2753 | ASSERT(finally_block != NULL); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2754 | int index = current_function_state_->NextHandlerIndex(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2755 | result = factory()->NewTryFinallyStatement(index, try_block, finally_block); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2756 | // Combine the jump targets of the try block and the possible catch block. |
| 2757 | try_collector.targets()->AddAll(*catch_collector.targets()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2760 | result->set_escaping_targets(try_collector.targets()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2761 | return result; |
| 2762 | } |
| 2763 | |
| 2764 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2765 | DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels, |
| 2766 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | // DoStatement :: |
| 2768 | // 'do' Statement 'while' '(' Expression ')' ';' |
| 2769 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2770 | DoWhileStatement* loop = factory()->NewDoWhileStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2771 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2772 | |
| 2773 | Expect(Token::DO, CHECK_OK); |
| 2774 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2775 | Expect(Token::WHILE, CHECK_OK); |
| 2776 | Expect(Token::LPAREN, CHECK_OK); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2777 | |
| 2778 | if (loop != NULL) { |
| 2779 | int position = scanner().location().beg_pos; |
| 2780 | loop->set_condition_position(position); |
| 2781 | } |
| 2782 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2783 | Expression* cond = ParseExpression(true, CHECK_OK); |
| 2784 | Expect(Token::RPAREN, CHECK_OK); |
| 2785 | |
| 2786 | // Allow do-statements to be terminated with and without |
| 2787 | // semi-colons. This allows code such as 'do;while(0)return' to |
| 2788 | // parse, which would not be the case if we had used the |
| 2789 | // ExpectSemicolon() functionality here. |
| 2790 | if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON); |
| 2791 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2792 | if (loop != NULL) loop->Initialize(cond, body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2793 | return loop; |
| 2794 | } |
| 2795 | |
| 2796 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2797 | WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2798 | // WhileStatement :: |
| 2799 | // 'while' '(' Expression ')' Statement |
| 2800 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2801 | WhileStatement* loop = factory()->NewWhileStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2802 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2803 | |
| 2804 | Expect(Token::WHILE, CHECK_OK); |
| 2805 | Expect(Token::LPAREN, CHECK_OK); |
| 2806 | Expression* cond = ParseExpression(true, CHECK_OK); |
| 2807 | Expect(Token::RPAREN, CHECK_OK); |
| 2808 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2809 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2810 | if (loop != NULL) loop->Initialize(cond, body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2811 | return loop; |
| 2812 | } |
| 2813 | |
| 2814 | |
| 2815 | Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { |
| 2816 | // ForStatement :: |
| 2817 | // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement |
| 2818 | |
| 2819 | Statement* init = NULL; |
| 2820 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2821 | // Create an in-between scope for let-bound iteration variables. |
| 2822 | Scope* saved_scope = top_scope_; |
| 2823 | Scope* for_scope = NewScope(top_scope_, BLOCK_SCOPE); |
| 2824 | top_scope_ = for_scope; |
| 2825 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2826 | Expect(Token::FOR, CHECK_OK); |
| 2827 | Expect(Token::LPAREN, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2828 | for_scope->set_start_position(scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2829 | if (peek() != Token::SEMICOLON) { |
| 2830 | if (peek() == Token::VAR || peek() == Token::CONST) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2831 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2832 | Block* variable_statement = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2833 | ParseVariableDeclarations(kForStatement, NULL, NULL, &name, CHECK_OK); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2834 | |
| 2835 | if (peek() == Token::IN && !name.is_null()) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2836 | VariableProxy* each = top_scope_->NewUnresolved(factory(), name); |
| 2837 | ForInStatement* loop = factory()->NewForInStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2838 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2839 | |
| 2840 | Expect(Token::IN, CHECK_OK); |
| 2841 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2842 | Expect(Token::RPAREN, CHECK_OK); |
| 2843 | |
| 2844 | Statement* body = ParseStatement(NULL, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2845 | loop->Initialize(each, enumerable, body); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2846 | Block* result = factory()->NewBlock(NULL, 2, false); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2847 | result->AddStatement(variable_statement); |
| 2848 | result->AddStatement(loop); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2849 | top_scope_ = saved_scope; |
| 2850 | for_scope->set_end_position(scanner().location().end_pos); |
| 2851 | for_scope = for_scope->FinalizeBlockScope(); |
| 2852 | ASSERT(for_scope == NULL); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2853 | // Parsed for-in loop w/ variable/const declaration. |
| 2854 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2855 | } else { |
| 2856 | init = variable_statement; |
| 2857 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2858 | } else if (peek() == Token::LET) { |
| 2859 | Handle<String> name; |
| 2860 | VariableDeclarationProperties decl_props = kHasNoInitializers; |
| 2861 | Block* variable_statement = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2862 | ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name, |
| 2863 | CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2864 | bool accept_IN = !name.is_null() && decl_props != kHasInitializers; |
| 2865 | if (peek() == Token::IN && accept_IN) { |
| 2866 | // Rewrite a for-in statement of the form |
| 2867 | // |
| 2868 | // for (let x in e) b |
| 2869 | // |
| 2870 | // into |
| 2871 | // |
| 2872 | // <let x' be a temporary variable> |
| 2873 | // for (x' in e) { |
| 2874 | // let x; |
| 2875 | // x = x'; |
| 2876 | // b; |
| 2877 | // } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2878 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2879 | // TODO(keuchel): Move the temporary variable to the block scope, after |
| 2880 | // implementing stack allocated block scoped variables. |
| 2881 | Variable* temp = top_scope_->DeclarationScope()->NewTemporary(name); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2882 | VariableProxy* temp_proxy = factory()->NewVariableProxy(temp); |
| 2883 | VariableProxy* each = top_scope_->NewUnresolved(factory(), name); |
| 2884 | ForInStatement* loop = factory()->NewForInStatement(labels); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2885 | Target target(&this->target_stack_, loop); |
| 2886 | |
| 2887 | Expect(Token::IN, CHECK_OK); |
| 2888 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2889 | Expect(Token::RPAREN, CHECK_OK); |
| 2890 | |
| 2891 | Statement* body = ParseStatement(NULL, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2892 | Block* body_block = factory()->NewBlock(NULL, 3, false); |
| 2893 | Assignment* assignment = factory()->NewAssignment( |
| 2894 | Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2895 | Statement* assignment_statement = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2896 | factory()->NewExpressionStatement(assignment); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2897 | body_block->AddStatement(variable_statement); |
| 2898 | body_block->AddStatement(assignment_statement); |
| 2899 | body_block->AddStatement(body); |
| 2900 | loop->Initialize(temp_proxy, enumerable, body_block); |
| 2901 | top_scope_ = saved_scope; |
| 2902 | for_scope->set_end_position(scanner().location().end_pos); |
| 2903 | for_scope = for_scope->FinalizeBlockScope(); |
| 2904 | body_block->set_block_scope(for_scope); |
| 2905 | // Parsed for-in loop w/ let declaration. |
| 2906 | return loop; |
| 2907 | |
| 2908 | } else { |
| 2909 | init = variable_statement; |
| 2910 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2911 | } else { |
| 2912 | Expression* expression = ParseExpression(false, CHECK_OK); |
| 2913 | if (peek() == Token::IN) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2914 | // Signal a reference error if the expression is an invalid |
| 2915 | // left-hand side expression. We could report this as a syntax |
| 2916 | // error here but for compatibility with JSC we choose to report |
| 2917 | // the error at runtime. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2918 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2919 | Handle<String> type = |
| 2920 | isolate()->factory()->invalid_lhs_in_for_in_symbol(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2921 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2923 | ForInStatement* loop = factory()->NewForInStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2924 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2925 | |
| 2926 | Expect(Token::IN, CHECK_OK); |
| 2927 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2928 | Expect(Token::RPAREN, CHECK_OK); |
| 2929 | |
| 2930 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2931 | if (loop) loop->Initialize(expression, enumerable, body); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2932 | top_scope_ = saved_scope; |
| 2933 | for_scope->set_end_position(scanner().location().end_pos); |
| 2934 | for_scope = for_scope->FinalizeBlockScope(); |
| 2935 | ASSERT(for_scope == NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2936 | // Parsed for-in loop. |
| 2937 | return loop; |
| 2938 | |
| 2939 | } else { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2940 | init = factory()->NewExpressionStatement(expression); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | // Standard 'for' loop |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2946 | ForStatement* loop = factory()->NewForStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2947 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2948 | |
| 2949 | // Parsed initializer at this point. |
| 2950 | Expect(Token::SEMICOLON, CHECK_OK); |
| 2951 | |
| 2952 | Expression* cond = NULL; |
| 2953 | if (peek() != Token::SEMICOLON) { |
| 2954 | cond = ParseExpression(true, CHECK_OK); |
| 2955 | } |
| 2956 | Expect(Token::SEMICOLON, CHECK_OK); |
| 2957 | |
| 2958 | Statement* next = NULL; |
| 2959 | if (peek() != Token::RPAREN) { |
| 2960 | Expression* exp = ParseExpression(true, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2961 | next = factory()->NewExpressionStatement(exp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2962 | } |
| 2963 | Expect(Token::RPAREN, CHECK_OK); |
| 2964 | |
| 2965 | Statement* body = ParseStatement(NULL, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2966 | top_scope_ = saved_scope; |
| 2967 | for_scope->set_end_position(scanner().location().end_pos); |
| 2968 | for_scope = for_scope->FinalizeBlockScope(); |
| 2969 | if (for_scope != NULL) { |
| 2970 | // Rewrite a for statement of the form |
| 2971 | // |
| 2972 | // for (let x = i; c; n) b |
| 2973 | // |
| 2974 | // into |
| 2975 | // |
| 2976 | // { |
| 2977 | // let x = i; |
| 2978 | // for (; c; n) b |
| 2979 | // } |
| 2980 | ASSERT(init != NULL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2981 | Block* result = factory()->NewBlock(NULL, 2, false); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2982 | result->AddStatement(init); |
| 2983 | result->AddStatement(loop); |
| 2984 | result->set_block_scope(for_scope); |
| 2985 | if (loop) loop->Initialize(NULL, cond, next, body); |
| 2986 | return result; |
| 2987 | } else { |
| 2988 | if (loop) loop->Initialize(init, cond, next, body); |
| 2989 | return loop; |
| 2990 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | // Precedence = 1 |
| 2995 | Expression* Parser::ParseExpression(bool accept_IN, bool* ok) { |
| 2996 | // Expression :: |
| 2997 | // AssignmentExpression |
| 2998 | // Expression ',' AssignmentExpression |
| 2999 | |
| 3000 | Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 3001 | while (peek() == Token::COMMA) { |
| 3002 | Expect(Token::COMMA, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3003 | int position = scanner().location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3004 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3005 | result = |
| 3006 | factory()->NewBinaryOperation(Token::COMMA, result, right, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3007 | } |
| 3008 | return result; |
| 3009 | } |
| 3010 | |
| 3011 | |
| 3012 | // Precedence = 2 |
| 3013 | Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) { |
| 3014 | // AssignmentExpression :: |
| 3015 | // ConditionalExpression |
| 3016 | // LeftHandSideExpression AssignmentOperator AssignmentExpression |
| 3017 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3018 | if (fni_ != NULL) fni_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3019 | Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK); |
| 3020 | |
| 3021 | if (!Token::IsAssignmentOp(peek())) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3022 | if (fni_ != NULL) fni_->Leave(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3023 | // Parsed conditional expression only (no assignment). |
| 3024 | return expression; |
| 3025 | } |
| 3026 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3027 | // Signal a reference error if the expression is an invalid left-hand |
| 3028 | // side expression. We could report this as a syntax error here but |
| 3029 | // for compatibility with JSC we choose to report the error at |
| 3030 | // runtime. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3031 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3032 | Handle<String> type = |
| 3033 | isolate()->factory()->invalid_lhs_in_assignment_symbol(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3034 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3037 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3038 | // Assignment to eval or arguments is disallowed in strict mode. |
| 3039 | CheckStrictModeLValue(expression, "strict_lhs_assignment", CHECK_OK); |
| 3040 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3041 | MarkAsLValue(expression); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3042 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3043 | Token::Value op = Next(); // Get assignment operator. |
| 3044 | int pos = scanner().location().beg_pos; |
| 3045 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 3046 | |
| 3047 | // TODO(1231235): We try to estimate the set of properties set by |
| 3048 | // constructors. We define a new property whenever there is an |
| 3049 | // assignment to a property of 'this'. We should probably only add |
| 3050 | // properties if we haven't seen them before. Otherwise we'll |
| 3051 | // probably overestimate the number of properties. |
| 3052 | Property* property = expression ? expression->AsProperty() : NULL; |
| 3053 | if (op == Token::ASSIGN && |
| 3054 | property != NULL && |
| 3055 | property->obj()->AsVariableProxy() != NULL && |
| 3056 | property->obj()->AsVariableProxy()->is_this()) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3057 | current_function_state_->AddProperty(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 3060 | // If we assign a function literal to a property we pretenure the |
| 3061 | // literal so it can be added as a constant function property. |
| 3062 | if (property != NULL && right->AsFunctionLiteral() != NULL) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3063 | right->AsFunctionLiteral()->set_pretenure(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3066 | if (fni_ != NULL) { |
| 3067 | // Check if the right hand side is a call to avoid inferring a |
| 3068 | // name if we're dealing with "a = function(){...}();"-like |
| 3069 | // expression. |
| 3070 | if ((op == Token::INIT_VAR |
| 3071 | || op == Token::INIT_CONST |
| 3072 | || op == Token::ASSIGN) |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3073 | && (right->AsCall() == NULL && right->AsCallNew() == NULL)) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3074 | fni_->Infer(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3075 | } else { |
| 3076 | fni_->RemoveLastFunction(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3077 | } |
| 3078 | fni_->Leave(); |
| 3079 | } |
| 3080 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3081 | return factory()->NewAssignment(op, expression, right, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | |
| 3085 | // Precedence = 3 |
| 3086 | Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { |
| 3087 | // ConditionalExpression :: |
| 3088 | // LogicalOrExpression |
| 3089 | // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression |
| 3090 | |
| 3091 | // We start using the binary expression parser for prec >= 4 only! |
| 3092 | Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK); |
| 3093 | if (peek() != Token::CONDITIONAL) return expression; |
| 3094 | Consume(Token::CONDITIONAL); |
| 3095 | // In parsing the first assignment expression in conditional |
| 3096 | // expressions we always accept the 'in' keyword; see ECMA-262, |
| 3097 | // section 11.12, page 58. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3098 | int left_position = scanner().peek_location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3099 | Expression* left = ParseAssignmentExpression(true, CHECK_OK); |
| 3100 | Expect(Token::COLON, CHECK_OK); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3101 | int right_position = scanner().peek_location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3102 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3103 | return factory()->NewConditional( |
| 3104 | expression, left, right, left_position, right_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | |
| 3108 | static int Precedence(Token::Value tok, bool accept_IN) { |
| 3109 | if (tok == Token::IN && !accept_IN) |
| 3110 | return 0; // 0 precedence will terminate binary expression parsing |
| 3111 | |
| 3112 | return Token::Precedence(tok); |
| 3113 | } |
| 3114 | |
| 3115 | |
| 3116 | // Precedence >= 4 |
| 3117 | Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) { |
| 3118 | ASSERT(prec >= 4); |
| 3119 | Expression* x = ParseUnaryExpression(CHECK_OK); |
| 3120 | for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) { |
| 3121 | // prec1 >= 4 |
| 3122 | while (Precedence(peek(), accept_IN) == prec1) { |
| 3123 | Token::Value op = Next(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3124 | int position = scanner().location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3125 | Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK); |
| 3126 | |
| 3127 | // Compute some expressions involving only number literals. |
| 3128 | if (x && x->AsLiteral() && x->AsLiteral()->handle()->IsNumber() && |
| 3129 | y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) { |
| 3130 | double x_val = x->AsLiteral()->handle()->Number(); |
| 3131 | double y_val = y->AsLiteral()->handle()->Number(); |
| 3132 | |
| 3133 | switch (op) { |
| 3134 | case Token::ADD: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3135 | x = factory()->NewNumberLiteral(x_val + y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3136 | continue; |
| 3137 | case Token::SUB: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3138 | x = factory()->NewNumberLiteral(x_val - y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3139 | continue; |
| 3140 | case Token::MUL: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3141 | x = factory()->NewNumberLiteral(x_val * y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3142 | continue; |
| 3143 | case Token::DIV: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3144 | x = factory()->NewNumberLiteral(x_val / y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3145 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3146 | case Token::BIT_OR: { |
| 3147 | int value = DoubleToInt32(x_val) | DoubleToInt32(y_val); |
| 3148 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3149 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3150 | } |
| 3151 | case Token::BIT_AND: { |
| 3152 | int value = DoubleToInt32(x_val) & DoubleToInt32(y_val); |
| 3153 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3154 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3155 | } |
| 3156 | case Token::BIT_XOR: { |
| 3157 | int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val); |
| 3158 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3159 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3160 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3161 | case Token::SHL: { |
| 3162 | int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3163 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3164 | continue; |
| 3165 | } |
| 3166 | case Token::SHR: { |
| 3167 | uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
| 3168 | uint32_t value = DoubleToUint32(x_val) >> shift; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3169 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3170 | continue; |
| 3171 | } |
| 3172 | case Token::SAR: { |
| 3173 | uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
| 3174 | int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3175 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3176 | continue; |
| 3177 | } |
| 3178 | default: |
| 3179 | break; |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | // For now we distinguish between comparisons and other binary |
| 3184 | // operations. (We could combine the two and get rid of this |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3185 | // code and AST node eventually.) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3186 | if (Token::IsCompareOp(op)) { |
| 3187 | // We have a comparison. |
| 3188 | Token::Value cmp = op; |
| 3189 | switch (op) { |
| 3190 | case Token::NE: cmp = Token::EQ; break; |
| 3191 | case Token::NE_STRICT: cmp = Token::EQ_STRICT; break; |
| 3192 | default: break; |
| 3193 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3194 | x = factory()->NewCompareOperation(cmp, x, y, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3195 | if (cmp != op) { |
| 3196 | // The comparison was negated - add a NOT. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3197 | x = factory()->NewUnaryOperation(Token::NOT, x, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | } else { |
| 3201 | // We have a "normal" binary operation. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3202 | x = factory()->NewBinaryOperation(op, x, y, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | return x; |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | Expression* Parser::ParseUnaryExpression(bool* ok) { |
| 3211 | // UnaryExpression :: |
| 3212 | // PostfixExpression |
| 3213 | // 'delete' UnaryExpression |
| 3214 | // 'void' UnaryExpression |
| 3215 | // 'typeof' UnaryExpression |
| 3216 | // '++' UnaryExpression |
| 3217 | // '--' UnaryExpression |
| 3218 | // '+' UnaryExpression |
| 3219 | // '-' UnaryExpression |
| 3220 | // '~' UnaryExpression |
| 3221 | // '!' UnaryExpression |
| 3222 | |
| 3223 | Token::Value op = peek(); |
| 3224 | if (Token::IsUnaryOp(op)) { |
| 3225 | op = Next(); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 3226 | int position = scanner().location().beg_pos; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3227 | Expression* expression = ParseUnaryExpression(CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3228 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3229 | if (expression != NULL && (expression->AsLiteral() != NULL)) { |
| 3230 | Handle<Object> literal = expression->AsLiteral()->handle(); |
| 3231 | if (op == Token::NOT) { |
| 3232 | // Convert the literal to a boolean condition and negate it. |
| 3233 | bool condition = literal->ToBoolean()->IsTrue(); |
| 3234 | Handle<Object> result(isolate()->heap()->ToBoolean(!condition)); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3235 | return factory()->NewLiteral(result); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3236 | } else if (literal->IsNumber()) { |
| 3237 | // Compute some expressions involving only number literals. |
| 3238 | double value = literal->Number(); |
| 3239 | switch (op) { |
| 3240 | case Token::ADD: |
| 3241 | return expression; |
| 3242 | case Token::SUB: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3243 | return factory()->NewNumberLiteral(-value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3244 | case Token::BIT_NOT: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3245 | return factory()->NewNumberLiteral(~DoubleToInt32(value)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3246 | default: |
| 3247 | break; |
| 3248 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3252 | // "delete identifier" is a syntax error in strict mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3253 | if (op == Token::DELETE && !top_scope_->is_classic_mode()) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3254 | VariableProxy* operand = expression->AsVariableProxy(); |
| 3255 | if (operand != NULL && !operand->is_this()) { |
| 3256 | ReportMessage("strict_delete", Vector<const char*>::empty()); |
| 3257 | *ok = false; |
| 3258 | return NULL; |
| 3259 | } |
| 3260 | } |
| 3261 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3262 | return factory()->NewUnaryOperation(op, expression, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3263 | |
| 3264 | } else if (Token::IsCountOp(op)) { |
| 3265 | op = Next(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3266 | Expression* expression = ParseUnaryExpression(CHECK_OK); |
| 3267 | // Signal a reference error if the expression is an invalid |
| 3268 | // left-hand side expression. We could report this as a syntax |
| 3269 | // error here but for compatibility with JSC we choose to report the |
| 3270 | // error at runtime. |
| 3271 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3272 | Handle<String> type = |
| 3273 | isolate()->factory()->invalid_lhs_in_prefix_op_symbol(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3274 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3275 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3276 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3277 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3278 | // Prefix expression operand in strict mode may not be eval or arguments. |
| 3279 | CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK); |
| 3280 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3281 | MarkAsLValue(expression); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3282 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3283 | int position = scanner().location().beg_pos; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3284 | return factory()->NewCountOperation(op, |
| 3285 | true /* prefix */, |
| 3286 | expression, |
| 3287 | position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3288 | |
| 3289 | } else { |
| 3290 | return ParsePostfixExpression(ok); |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | |
| 3295 | Expression* Parser::ParsePostfixExpression(bool* ok) { |
| 3296 | // PostfixExpression :: |
| 3297 | // LeftHandSideExpression ('++' | '--')? |
| 3298 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3299 | Expression* expression = ParseLeftHandSideExpression(CHECK_OK); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 3300 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3301 | Token::IsCountOp(peek())) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3302 | // Signal a reference error if the expression is an invalid |
| 3303 | // left-hand side expression. We could report this as a syntax |
| 3304 | // error here but for compatibility with JSC we choose to report the |
| 3305 | // error at runtime. |
| 3306 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3307 | Handle<String> type = |
| 3308 | isolate()->factory()->invalid_lhs_in_postfix_op_symbol(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3309 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3310 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3311 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3312 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3313 | // Postfix expression operand in strict mode may not be eval or arguments. |
| 3314 | CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK); |
| 3315 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3316 | MarkAsLValue(expression); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3317 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3318 | Token::Value next = Next(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3319 | int position = scanner().location().beg_pos; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3320 | expression = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3321 | factory()->NewCountOperation(next, |
| 3322 | false /* postfix */, |
| 3323 | expression, |
| 3324 | position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3325 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3326 | return expression; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | |
| 3330 | Expression* Parser::ParseLeftHandSideExpression(bool* ok) { |
| 3331 | // LeftHandSideExpression :: |
| 3332 | // (NewExpression | MemberExpression) ... |
| 3333 | |
| 3334 | Expression* result; |
| 3335 | if (peek() == Token::NEW) { |
| 3336 | result = ParseNewExpression(CHECK_OK); |
| 3337 | } else { |
| 3338 | result = ParseMemberExpression(CHECK_OK); |
| 3339 | } |
| 3340 | |
| 3341 | while (true) { |
| 3342 | switch (peek()) { |
| 3343 | case Token::LBRACK: { |
| 3344 | Consume(Token::LBRACK); |
| 3345 | int pos = scanner().location().beg_pos; |
| 3346 | Expression* index = ParseExpression(true, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3347 | result = factory()->NewProperty(result, index, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3348 | Expect(Token::RBRACK, CHECK_OK); |
| 3349 | break; |
| 3350 | } |
| 3351 | |
| 3352 | case Token::LPAREN: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3353 | int pos; |
| 3354 | if (scanner().current_token() == Token::IDENTIFIER) { |
| 3355 | // For call of an identifier we want to report position of |
| 3356 | // the identifier as position of the call in the stack trace. |
| 3357 | pos = scanner().location().beg_pos; |
| 3358 | } else { |
| 3359 | // For other kinds of calls we record position of the parenthesis as |
| 3360 | // position of the call. Note that this is extremely important for |
| 3361 | // expressions of the form function(){...}() for which call position |
| 3362 | // should not point to the closing brace otherwise it will intersect |
| 3363 | // with positions recorded for function literal and confuse debugger. |
| 3364 | pos = scanner().peek_location().beg_pos; |
| 3365 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3366 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
| 3367 | |
| 3368 | // Keep track of eval() calls since they disable all local variable |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3369 | // optimizations. |
| 3370 | // The calls that need special treatment are the |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3371 | // direct eval calls. These calls are all of the form eval(...), with |
| 3372 | // no explicit receiver. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3373 | // These calls are marked as potentially direct eval calls. Whether |
| 3374 | // they are actually direct calls to eval is determined at run time. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3375 | VariableProxy* callee = result->AsVariableProxy(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3376 | if (callee != NULL && |
| 3377 | callee->IsVariable(isolate()->factory()->eval_symbol())) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3378 | top_scope_->DeclarationScope()->RecordEvalCall(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3379 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3380 | result = factory()->NewCall(result, args, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3381 | break; |
| 3382 | } |
| 3383 | |
| 3384 | case Token::PERIOD: { |
| 3385 | Consume(Token::PERIOD); |
| 3386 | int pos = scanner().location().beg_pos; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 3387 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3388 | result = |
| 3389 | factory()->NewProperty(result, factory()->NewLiteral(name), pos); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3390 | if (fni_ != NULL) fni_->PushLiteralName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3391 | break; |
| 3392 | } |
| 3393 | |
| 3394 | default: |
| 3395 | return result; |
| 3396 | } |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3401 | Expression* Parser::ParseNewPrefix(PositionStack* stack, bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3402 | // NewExpression :: |
| 3403 | // ('new')+ MemberExpression |
| 3404 | |
| 3405 | // The grammar for new expressions is pretty warped. The keyword |
| 3406 | // 'new' can either be a part of the new expression (where it isn't |
| 3407 | // followed by an argument list) or a part of the member expression, |
| 3408 | // where it must be followed by an argument list. To accommodate |
| 3409 | // this, we parse the 'new' keywords greedily and keep track of how |
| 3410 | // many we have parsed. This information is then passed on to the |
| 3411 | // member expression parser, which is only allowed to match argument |
| 3412 | // lists as long as it has 'new' prefixes left |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3413 | Expect(Token::NEW, CHECK_OK); |
| 3414 | PositionStack::Element pos(stack, scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3415 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3416 | Expression* result; |
| 3417 | if (peek() == Token::NEW) { |
| 3418 | result = ParseNewPrefix(stack, CHECK_OK); |
| 3419 | } else { |
| 3420 | result = ParseMemberWithNewPrefixesExpression(stack, CHECK_OK); |
| 3421 | } |
| 3422 | |
| 3423 | if (!stack->is_empty()) { |
| 3424 | int last = stack->pop(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3425 | result = factory()->NewCallNew( |
| 3426 | result, new(zone()) ZoneList<Expression*>(0), last); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3427 | } |
| 3428 | return result; |
| 3429 | } |
| 3430 | |
| 3431 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3432 | Expression* Parser::ParseNewExpression(bool* ok) { |
| 3433 | PositionStack stack(ok); |
| 3434 | return ParseNewPrefix(&stack, ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3438 | Expression* Parser::ParseMemberExpression(bool* ok) { |
| 3439 | return ParseMemberWithNewPrefixesExpression(NULL, ok); |
| 3440 | } |
| 3441 | |
| 3442 | |
| 3443 | Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack, |
| 3444 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3445 | // MemberExpression :: |
| 3446 | // (PrimaryExpression | FunctionLiteral) |
| 3447 | // ('[' Expression ']' | '.' Identifier | Arguments)* |
| 3448 | |
| 3449 | // Parse the initial primary or function expression. |
| 3450 | Expression* result = NULL; |
| 3451 | if (peek() == Token::FUNCTION) { |
| 3452 | Expect(Token::FUNCTION, CHECK_OK); |
| 3453 | int function_token_position = scanner().location().beg_pos; |
| 3454 | Handle<String> name; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3455 | bool is_strict_reserved_name = false; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3456 | if (peek_any_identifier()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3457 | name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, |
| 3458 | CHECK_OK); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3459 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 3460 | FunctionLiteral::Type type = name.is_null() |
| 3461 | ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 3462 | : FunctionLiteral::NAMED_EXPRESSION; |
| 3463 | result = ParseFunctionLiteral(name, |
| 3464 | is_strict_reserved_name, |
| 3465 | function_token_position, |
| 3466 | type, |
| 3467 | CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3468 | } else { |
| 3469 | result = ParsePrimaryExpression(CHECK_OK); |
| 3470 | } |
| 3471 | |
| 3472 | while (true) { |
| 3473 | switch (peek()) { |
| 3474 | case Token::LBRACK: { |
| 3475 | Consume(Token::LBRACK); |
| 3476 | int pos = scanner().location().beg_pos; |
| 3477 | Expression* index = ParseExpression(true, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3478 | result = factory()->NewProperty(result, index, pos); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3479 | if (fni_ != NULL) { |
| 3480 | if (index->IsPropertyName()) { |
| 3481 | fni_->PushLiteralName(index->AsLiteral()->AsPropertyName()); |
| 3482 | } else { |
| 3483 | fni_->PushLiteralName( |
| 3484 | isolate()->factory()->anonymous_function_symbol()); |
| 3485 | } |
| 3486 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3487 | Expect(Token::RBRACK, CHECK_OK); |
| 3488 | break; |
| 3489 | } |
| 3490 | case Token::PERIOD: { |
| 3491 | Consume(Token::PERIOD); |
| 3492 | int pos = scanner().location().beg_pos; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 3493 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3494 | result = |
| 3495 | factory()->NewProperty(result, factory()->NewLiteral(name), pos); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3496 | if (fni_ != NULL) fni_->PushLiteralName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3497 | break; |
| 3498 | } |
| 3499 | case Token::LPAREN: { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3500 | if ((stack == NULL) || stack->is_empty()) return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3501 | // Consume one of the new prefixes (already parsed). |
| 3502 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3503 | int last = stack->pop(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3504 | result = factory()->NewCallNew(result, args, last); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3505 | break; |
| 3506 | } |
| 3507 | default: |
| 3508 | return result; |
| 3509 | } |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | |
| 3514 | DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) { |
| 3515 | // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser |
| 3516 | // contexts this is used as a statement which invokes the debugger as i a |
| 3517 | // break point is present. |
| 3518 | // DebuggerStatement :: |
| 3519 | // 'debugger' ';' |
| 3520 | |
| 3521 | Expect(Token::DEBUGGER, CHECK_OK); |
| 3522 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3523 | return factory()->NewDebuggerStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | |
| 3527 | void Parser::ReportUnexpectedToken(Token::Value token) { |
| 3528 | // We don't report stack overflows here, to avoid increasing the |
| 3529 | // stack depth even further. Instead we report it after parsing is |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3530 | // over, in ParseProgram/ParseJson. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3531 | if (token == Token::ILLEGAL && stack_overflow_) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3532 | // Four of the tokens are treated specially |
| 3533 | switch (token) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3534 | case Token::EOS: |
| 3535 | return ReportMessage("unexpected_eos", Vector<const char*>::empty()); |
| 3536 | case Token::NUMBER: |
| 3537 | return ReportMessage("unexpected_token_number", |
| 3538 | Vector<const char*>::empty()); |
| 3539 | case Token::STRING: |
| 3540 | return ReportMessage("unexpected_token_string", |
| 3541 | Vector<const char*>::empty()); |
| 3542 | case Token::IDENTIFIER: |
| 3543 | return ReportMessage("unexpected_token_identifier", |
| 3544 | Vector<const char*>::empty()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3545 | case Token::FUTURE_RESERVED_WORD: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3546 | return ReportMessage("unexpected_reserved", |
| 3547 | Vector<const char*>::empty()); |
| 3548 | case Token::FUTURE_STRICT_RESERVED_WORD: |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3549 | return ReportMessage(top_scope_->is_classic_mode() ? |
| 3550 | "unexpected_token_identifier" : |
| 3551 | "unexpected_strict_reserved", |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3552 | Vector<const char*>::empty()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3553 | default: |
| 3554 | const char* name = Token::String(token); |
| 3555 | ASSERT(name != NULL); |
| 3556 | ReportMessage("unexpected_token", Vector<const char*>(&name, 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3561 | void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 3562 | SmartArrayPointer<char> name_string = name->ToCString(DISALLOW_NULLS); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3563 | const char* element[1] = { *name_string }; |
| 3564 | ReportMessage("invalid_preparser_data", |
| 3565 | Vector<const char*>(element, 1)); |
| 3566 | *ok = false; |
| 3567 | } |
| 3568 | |
| 3569 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3570 | Expression* Parser::ParsePrimaryExpression(bool* ok) { |
| 3571 | // PrimaryExpression :: |
| 3572 | // 'this' |
| 3573 | // 'null' |
| 3574 | // 'true' |
| 3575 | // 'false' |
| 3576 | // Identifier |
| 3577 | // Number |
| 3578 | // String |
| 3579 | // ArrayLiteral |
| 3580 | // ObjectLiteral |
| 3581 | // RegExpLiteral |
| 3582 | // '(' Expression ')' |
| 3583 | |
| 3584 | Expression* result = NULL; |
| 3585 | switch (peek()) { |
| 3586 | case Token::THIS: { |
| 3587 | Consume(Token::THIS); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3588 | result = factory()->NewVariableProxy(top_scope_->receiver()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3589 | break; |
| 3590 | } |
| 3591 | |
| 3592 | case Token::NULL_LITERAL: |
| 3593 | Consume(Token::NULL_LITERAL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3594 | result = factory()->NewLiteral(isolate()->factory()->null_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3595 | break; |
| 3596 | |
| 3597 | case Token::TRUE_LITERAL: |
| 3598 | Consume(Token::TRUE_LITERAL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3599 | result = factory()->NewLiteral(isolate()->factory()->true_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3600 | break; |
| 3601 | |
| 3602 | case Token::FALSE_LITERAL: |
| 3603 | Consume(Token::FALSE_LITERAL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3604 | result = factory()->NewLiteral(isolate()->factory()->false_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3605 | break; |
| 3606 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3607 | case Token::IDENTIFIER: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3608 | case Token::FUTURE_STRICT_RESERVED_WORD: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3609 | Handle<String> name = ParseIdentifier(CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3610 | if (fni_ != NULL) fni_->PushVariableName(name); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 3611 | // The name may refer to a module instance object, so its type is unknown. |
| 3612 | #ifdef DEBUG |
| 3613 | if (FLAG_print_interface_details) |
| 3614 | PrintF("# Variable %s ", name->ToAsciiArray()); |
| 3615 | #endif |
| 3616 | Interface* interface = Interface::NewUnknown(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3617 | result = top_scope_->NewUnresolved( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 3618 | factory(), name, scanner().location().beg_pos, interface); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3619 | break; |
| 3620 | } |
| 3621 | |
| 3622 | case Token::NUMBER: { |
| 3623 | Consume(Token::NUMBER); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 3624 | ASSERT(scanner().is_literal_ascii()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 3625 | double value = StringToDouble(isolate()->unicode_cache(), |
| 3626 | scanner().literal_ascii_string(), |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 3627 | ALLOW_HEX | ALLOW_OCTALS); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3628 | result = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3629 | break; |
| 3630 | } |
| 3631 | |
| 3632 | case Token::STRING: { |
| 3633 | Consume(Token::STRING); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 3634 | Handle<String> symbol = GetSymbol(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3635 | result = factory()->NewLiteral(symbol); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3636 | if (fni_ != NULL) fni_->PushLiteralName(symbol); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3637 | break; |
| 3638 | } |
| 3639 | |
| 3640 | case Token::ASSIGN_DIV: |
| 3641 | result = ParseRegExpLiteral(true, CHECK_OK); |
| 3642 | break; |
| 3643 | |
| 3644 | case Token::DIV: |
| 3645 | result = ParseRegExpLiteral(false, CHECK_OK); |
| 3646 | break; |
| 3647 | |
| 3648 | case Token::LBRACK: |
| 3649 | result = ParseArrayLiteral(CHECK_OK); |
| 3650 | break; |
| 3651 | |
| 3652 | case Token::LBRACE: |
| 3653 | result = ParseObjectLiteral(CHECK_OK); |
| 3654 | break; |
| 3655 | |
| 3656 | case Token::LPAREN: |
| 3657 | Consume(Token::LPAREN); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3658 | // Heuristically try to detect immediately called functions before |
| 3659 | // seeing the call parentheses. |
| 3660 | parenthesized_function_ = (peek() == Token::FUNCTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3661 | result = ParseExpression(true, CHECK_OK); |
| 3662 | Expect(Token::RPAREN, CHECK_OK); |
| 3663 | break; |
| 3664 | |
| 3665 | case Token::MOD: |
| 3666 | if (allow_natives_syntax_ || extension_ != NULL) { |
| 3667 | result = ParseV8Intrinsic(CHECK_OK); |
| 3668 | break; |
| 3669 | } |
| 3670 | // If we're not allowing special syntax we fall-through to the |
| 3671 | // default case. |
| 3672 | |
| 3673 | default: { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3674 | Token::Value tok = Next(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3675 | ReportUnexpectedToken(tok); |
| 3676 | *ok = false; |
| 3677 | return NULL; |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | return result; |
| 3682 | } |
| 3683 | |
| 3684 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3685 | void Parser::BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* values, |
| 3686 | Handle<FixedArray> literals, |
| 3687 | bool* is_simple, |
| 3688 | int* depth) { |
| 3689 | // Fill in the literals. |
| 3690 | // Accumulate output values in local variables. |
| 3691 | bool is_simple_acc = true; |
| 3692 | int depth_acc = 1; |
| 3693 | for (int i = 0; i < values->length(); i++) { |
| 3694 | MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral(); |
| 3695 | if (m_literal != NULL && m_literal->depth() >= depth_acc) { |
| 3696 | depth_acc = m_literal->depth() + 1; |
| 3697 | } |
| 3698 | Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i)); |
| 3699 | if (boilerplate_value->IsUndefined()) { |
| 3700 | literals->set_the_hole(i); |
| 3701 | is_simple_acc = false; |
| 3702 | } else { |
| 3703 | literals->set(i, *boilerplate_value); |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | *is_simple = is_simple_acc; |
| 3708 | *depth = depth_acc; |
| 3709 | } |
| 3710 | |
| 3711 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3712 | Expression* Parser::ParseArrayLiteral(bool* ok) { |
| 3713 | // ArrayLiteral :: |
| 3714 | // '[' Expression? (',' Expression?)* ']' |
| 3715 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 3716 | ZoneList<Expression*>* values = new(zone()) ZoneList<Expression*>(4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3717 | Expect(Token::LBRACK, CHECK_OK); |
| 3718 | while (peek() != Token::RBRACK) { |
| 3719 | Expression* elem; |
| 3720 | if (peek() == Token::COMMA) { |
| 3721 | elem = GetLiteralTheHole(); |
| 3722 | } else { |
| 3723 | elem = ParseAssignmentExpression(true, CHECK_OK); |
| 3724 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3725 | values->Add(elem); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3726 | if (peek() != Token::RBRACK) { |
| 3727 | Expect(Token::COMMA, CHECK_OK); |
| 3728 | } |
| 3729 | } |
| 3730 | Expect(Token::RBRACK, CHECK_OK); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3731 | |
| 3732 | // Update the scope information before the pre-parsing bailout. |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3733 | int literal_index = current_function_state_->NextMaterializedLiteralIndex(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3734 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3735 | // Allocate a fixed array to hold all the object literals. |
| 3736 | Handle<FixedArray> object_literals = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3737 | isolate()->factory()->NewFixedArray(values->length(), TENURED); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3738 | Handle<FixedDoubleArray> double_literals; |
| 3739 | ElementsKind elements_kind = FAST_SMI_ONLY_ELEMENTS; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3740 | bool has_only_undefined_values = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3741 | |
| 3742 | // Fill in the literals. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3743 | bool is_simple = true; |
| 3744 | int depth = 1; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3745 | for (int i = 0, n = values->length(); i < n; i++) { |
| 3746 | MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3747 | if (m_literal != NULL && m_literal->depth() + 1 > depth) { |
| 3748 | depth = m_literal->depth() + 1; |
| 3749 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3750 | Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3751 | if (boilerplate_value->IsUndefined()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3752 | object_literals->set_the_hole(i); |
| 3753 | if (elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 3754 | double_literals->set_the_hole(i); |
| 3755 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3756 | is_simple = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3757 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3758 | // Examine each literal element, and adjust the ElementsKind if the |
| 3759 | // literal element is not of a type that can be stored in the current |
| 3760 | // ElementsKind. Start with FAST_SMI_ONLY_ELEMENTS, and transition to |
| 3761 | // FAST_DOUBLE_ELEMENTS and FAST_ELEMENTS as necessary. Always remember |
| 3762 | // the tagged value, no matter what the ElementsKind is in case we |
| 3763 | // ultimately end up in FAST_ELEMENTS. |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3764 | has_only_undefined_values = false; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3765 | object_literals->set(i, *boilerplate_value); |
| 3766 | if (elements_kind == FAST_SMI_ONLY_ELEMENTS) { |
| 3767 | // Smi only elements. Notice if a transition to FAST_DOUBLE_ELEMENTS or |
| 3768 | // FAST_ELEMENTS is required. |
| 3769 | if (!boilerplate_value->IsSmi()) { |
| 3770 | if (boilerplate_value->IsNumber() && FLAG_smi_only_arrays) { |
| 3771 | // Allocate a double array on the FAST_DOUBLE_ELEMENTS transition to |
| 3772 | // avoid over-allocating in TENURED space. |
| 3773 | double_literals = isolate()->factory()->NewFixedDoubleArray( |
| 3774 | values->length(), TENURED); |
| 3775 | // Copy the contents of the FAST_SMI_ONLY_ELEMENT array to the |
| 3776 | // FAST_DOUBLE_ELEMENTS array so that they are in sync. |
| 3777 | for (int j = 0; j < i; ++j) { |
| 3778 | Object* smi_value = object_literals->get(j); |
| 3779 | if (smi_value->IsTheHole()) { |
| 3780 | double_literals->set_the_hole(j); |
| 3781 | } else { |
| 3782 | double_literals->set(j, Smi::cast(smi_value)->value()); |
| 3783 | } |
| 3784 | } |
| 3785 | double_literals->set(i, boilerplate_value->Number()); |
| 3786 | elements_kind = FAST_DOUBLE_ELEMENTS; |
| 3787 | } else { |
| 3788 | elements_kind = FAST_ELEMENTS; |
| 3789 | } |
| 3790 | } |
| 3791 | } else if (elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 3792 | // Continue to store double values in to FAST_DOUBLE_ELEMENTS arrays |
| 3793 | // until the first value is seen that can't be stored as a double. |
| 3794 | if (boilerplate_value->IsNumber()) { |
| 3795 | double_literals->set(i, boilerplate_value->Number()); |
| 3796 | } else { |
| 3797 | elements_kind = FAST_ELEMENTS; |
| 3798 | } |
| 3799 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3800 | } |
| 3801 | } |
| 3802 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3803 | // Very small array literals that don't have a concrete hint about their type |
| 3804 | // from a constant value should default to the slow case to avoid lots of |
| 3805 | // elements transitions on really small objects. |
| 3806 | if (has_only_undefined_values && values->length() <= 2) { |
| 3807 | elements_kind = FAST_ELEMENTS; |
| 3808 | } |
| 3809 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 3810 | // Simple and shallow arrays can be lazily copied, we transform the |
| 3811 | // elements array to a copy-on-write array. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3812 | if (is_simple && depth == 1 && values->length() > 0 && |
| 3813 | elements_kind != FAST_DOUBLE_ELEMENTS) { |
| 3814 | object_literals->set_map(isolate()->heap()->fixed_cow_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3817 | Handle<FixedArrayBase> element_values = elements_kind == FAST_DOUBLE_ELEMENTS |
| 3818 | ? Handle<FixedArrayBase>(double_literals) |
| 3819 | : Handle<FixedArrayBase>(object_literals); |
| 3820 | |
| 3821 | // Remember both the literal's constant values as well as the ElementsKind |
| 3822 | // in a 2-element FixedArray. |
| 3823 | Handle<FixedArray> literals = |
| 3824 | isolate()->factory()->NewFixedArray(2, TENURED); |
| 3825 | |
| 3826 | literals->set(0, Smi::FromInt(elements_kind)); |
| 3827 | literals->set(1, *element_values); |
| 3828 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3829 | return factory()->NewArrayLiteral( |
| 3830 | literals, values, literal_index, is_simple, depth); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3834 | bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { |
| 3835 | return property != NULL && |
| 3836 | property->kind() != ObjectLiteral::Property::PROTOTYPE; |
| 3837 | } |
| 3838 | |
| 3839 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3840 | bool CompileTimeValue::IsCompileTimeValue(Expression* expression) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3841 | if (expression->AsLiteral() != NULL) return true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3842 | MaterializedLiteral* lit = expression->AsMaterializedLiteral(); |
| 3843 | return lit != NULL && lit->is_simple(); |
| 3844 | } |
| 3845 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 3846 | |
| 3847 | bool CompileTimeValue::ArrayLiteralElementNeedsInitialization( |
| 3848 | Expression* value) { |
| 3849 | // If value is a literal the property value is already set in the |
| 3850 | // boilerplate object. |
| 3851 | if (value->AsLiteral() != NULL) return false; |
| 3852 | // If value is a materialized literal the property value is already set |
| 3853 | // in the boilerplate object if it is simple. |
| 3854 | if (CompileTimeValue::IsCompileTimeValue(value)) return false; |
| 3855 | return true; |
| 3856 | } |
| 3857 | |
| 3858 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3859 | Handle<FixedArray> CompileTimeValue::GetValue(Expression* expression) { |
| 3860 | ASSERT(IsCompileTimeValue(expression)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3861 | Handle<FixedArray> result = FACTORY->NewFixedArray(2, TENURED); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3862 | ObjectLiteral* object_literal = expression->AsObjectLiteral(); |
| 3863 | if (object_literal != NULL) { |
| 3864 | ASSERT(object_literal->is_simple()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3865 | if (object_literal->fast_elements()) { |
| 3866 | result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS)); |
| 3867 | } else { |
| 3868 | result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS)); |
| 3869 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3870 | result->set(kElementsSlot, *object_literal->constant_properties()); |
| 3871 | } else { |
| 3872 | ArrayLiteral* array_literal = expression->AsArrayLiteral(); |
| 3873 | ASSERT(array_literal != NULL && array_literal->is_simple()); |
| 3874 | result->set(kTypeSlot, Smi::FromInt(ARRAY_LITERAL)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3875 | result->set(kElementsSlot, *array_literal->constant_elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3876 | } |
| 3877 | return result; |
| 3878 | } |
| 3879 | |
| 3880 | |
| 3881 | CompileTimeValue::Type CompileTimeValue::GetType(Handle<FixedArray> value) { |
| 3882 | Smi* type_value = Smi::cast(value->get(kTypeSlot)); |
| 3883 | return static_cast<Type>(type_value->value()); |
| 3884 | } |
| 3885 | |
| 3886 | |
| 3887 | Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
| 3888 | return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
| 3889 | } |
| 3890 | |
| 3891 | |
| 3892 | Handle<Object> Parser::GetBoilerplateValue(Expression* expression) { |
| 3893 | if (expression->AsLiteral() != NULL) { |
| 3894 | return expression->AsLiteral()->handle(); |
| 3895 | } |
| 3896 | if (CompileTimeValue::IsCompileTimeValue(expression)) { |
| 3897 | return CompileTimeValue::GetValue(expression); |
| 3898 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3899 | return isolate()->factory()->undefined_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3902 | // Validation per 11.1.5 Object Initialiser |
| 3903 | class ObjectLiteralPropertyChecker { |
| 3904 | public: |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3905 | ObjectLiteralPropertyChecker(Parser* parser, LanguageMode language_mode) : |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 3906 | props_(Literal::Match), |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3907 | parser_(parser), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3908 | language_mode_(language_mode) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | void CheckProperty( |
| 3912 | ObjectLiteral::Property* property, |
| 3913 | Scanner::Location loc, |
| 3914 | bool* ok); |
| 3915 | |
| 3916 | private: |
| 3917 | enum PropertyKind { |
| 3918 | kGetAccessor = 0x01, |
| 3919 | kSetAccessor = 0x02, |
| 3920 | kAccessor = kGetAccessor | kSetAccessor, |
| 3921 | kData = 0x04 |
| 3922 | }; |
| 3923 | |
| 3924 | static intptr_t GetPropertyKind(ObjectLiteral::Property* property) { |
| 3925 | switch (property->kind()) { |
| 3926 | case ObjectLiteral::Property::GETTER: |
| 3927 | return kGetAccessor; |
| 3928 | case ObjectLiteral::Property::SETTER: |
| 3929 | return kSetAccessor; |
| 3930 | default: |
| 3931 | return kData; |
| 3932 | } |
| 3933 | } |
| 3934 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 3935 | HashMap props_; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3936 | Parser* parser_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3937 | LanguageMode language_mode_; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3938 | }; |
| 3939 | |
| 3940 | |
| 3941 | void ObjectLiteralPropertyChecker::CheckProperty( |
| 3942 | ObjectLiteral::Property* property, |
| 3943 | Scanner::Location loc, |
| 3944 | bool* ok) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3945 | ASSERT(property != NULL); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 3946 | Literal* literal = property->key(); |
| 3947 | HashMap::Entry* entry = props_.Lookup(literal, literal->Hash(), true); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3948 | intptr_t prev = reinterpret_cast<intptr_t> (entry->value); |
| 3949 | intptr_t curr = GetPropertyKind(property); |
| 3950 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3951 | // Duplicate data properties are illegal in strict or extended mode. |
| 3952 | if (language_mode_ != CLASSIC_MODE && (curr & prev & kData) != 0) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3953 | parser_->ReportMessageAt(loc, "strict_duplicate_property", |
| 3954 | Vector<const char*>::empty()); |
| 3955 | *ok = false; |
| 3956 | return; |
| 3957 | } |
| 3958 | // Data property conflicting with an accessor. |
| 3959 | if (((curr & kData) && (prev & kAccessor)) || |
| 3960 | ((prev & kData) && (curr & kAccessor))) { |
| 3961 | parser_->ReportMessageAt(loc, "accessor_data_property", |
| 3962 | Vector<const char*>::empty()); |
| 3963 | *ok = false; |
| 3964 | return; |
| 3965 | } |
| 3966 | // Two accessors of the same type conflicting |
| 3967 | if ((curr & prev & kAccessor) != 0) { |
| 3968 | parser_->ReportMessageAt(loc, "accessor_get_set", |
| 3969 | Vector<const char*>::empty()); |
| 3970 | *ok = false; |
| 3971 | return; |
| 3972 | } |
| 3973 | |
| 3974 | // Update map |
| 3975 | entry->value = reinterpret_cast<void*> (prev | curr); |
| 3976 | *ok = true; |
| 3977 | } |
| 3978 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3979 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3980 | void Parser::BuildObjectLiteralConstantProperties( |
| 3981 | ZoneList<ObjectLiteral::Property*>* properties, |
| 3982 | Handle<FixedArray> constant_properties, |
| 3983 | bool* is_simple, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3984 | bool* fast_elements, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3985 | int* depth) { |
| 3986 | int position = 0; |
| 3987 | // Accumulate the value in local variables and store it at the end. |
| 3988 | bool is_simple_acc = true; |
| 3989 | int depth_acc = 1; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3990 | uint32_t max_element_index = 0; |
| 3991 | uint32_t elements = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3992 | for (int i = 0; i < properties->length(); i++) { |
| 3993 | ObjectLiteral::Property* property = properties->at(i); |
| 3994 | if (!IsBoilerplateProperty(property)) { |
| 3995 | is_simple_acc = false; |
| 3996 | continue; |
| 3997 | } |
| 3998 | MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral(); |
| 3999 | if (m_literal != NULL && m_literal->depth() >= depth_acc) { |
| 4000 | depth_acc = m_literal->depth() + 1; |
| 4001 | } |
| 4002 | |
| 4003 | // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined |
| 4004 | // value for COMPUTED properties, the real value is filled in at |
| 4005 | // runtime. The enumeration order is maintained. |
| 4006 | Handle<Object> key = property->key()->handle(); |
| 4007 | Handle<Object> value = GetBoilerplateValue(property->value()); |
| 4008 | is_simple_acc = is_simple_acc && !value->IsUndefined(); |
| 4009 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4010 | // Keep track of the number of elements in the object literal and |
| 4011 | // the largest element index. If the largest element index is |
| 4012 | // much larger than the number of elements, creating an object |
| 4013 | // literal with fast elements will be a waste of space. |
| 4014 | uint32_t element_index = 0; |
| 4015 | if (key->IsString() |
| 4016 | && Handle<String>::cast(key)->AsArrayIndex(&element_index) |
| 4017 | && element_index > max_element_index) { |
| 4018 | max_element_index = element_index; |
| 4019 | elements++; |
| 4020 | } else if (key->IsSmi()) { |
| 4021 | int key_value = Smi::cast(*key)->value(); |
| 4022 | if (key_value > 0 |
| 4023 | && static_cast<uint32_t>(key_value) > max_element_index) { |
| 4024 | max_element_index = key_value; |
| 4025 | } |
| 4026 | elements++; |
| 4027 | } |
| 4028 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4029 | // Add name, value pair to the fixed array. |
| 4030 | constant_properties->set(position++, *key); |
| 4031 | constant_properties->set(position++, *value); |
| 4032 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4033 | *fast_elements = |
| 4034 | (max_element_index <= 32) || ((2 * elements) >= max_element_index); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4035 | *is_simple = is_simple_acc; |
| 4036 | *depth = depth_acc; |
| 4037 | } |
| 4038 | |
| 4039 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4040 | ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter, |
| 4041 | bool* ok) { |
| 4042 | // Special handling of getter and setter syntax: |
| 4043 | // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... } |
| 4044 | // We have already read the "get" or "set" keyword. |
| 4045 | Token::Value next = Next(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 4046 | bool is_keyword = Token::IsKeyword(next); |
| 4047 | if (next == Token::IDENTIFIER || next == Token::NUMBER || |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4048 | next == Token::FUTURE_RESERVED_WORD || |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4049 | next == Token::FUTURE_STRICT_RESERVED_WORD || |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 4050 | next == Token::STRING || is_keyword) { |
| 4051 | Handle<String> name; |
| 4052 | if (is_keyword) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4053 | name = isolate_->factory()->LookupAsciiSymbol(Token::String(next)); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 4054 | } else { |
| 4055 | name = GetSymbol(CHECK_OK); |
| 4056 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4057 | FunctionLiteral* value = |
| 4058 | ParseFunctionLiteral(name, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4059 | false, // reserved words are allowed here |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4060 | RelocInfo::kNoPosition, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4061 | FunctionLiteral::ANONYMOUS_EXPRESSION, |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4062 | CHECK_OK); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4063 | // Allow any number of parameters for compatibilty with JSC. |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 4064 | // Specification only allows zero parameters for get and one for set. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4065 | return factory()->NewObjectLiteralProperty(is_getter, value); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4066 | } else { |
| 4067 | ReportUnexpectedToken(next); |
| 4068 | *ok = false; |
| 4069 | return NULL; |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4074 | Expression* Parser::ParseObjectLiteral(bool* ok) { |
| 4075 | // ObjectLiteral :: |
| 4076 | // '{' ( |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4077 | // ((IdentifierName | String | Number) ':' AssignmentExpression) |
| 4078 | // | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4079 | // )*[','] '}' |
| 4080 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4081 | ZoneList<ObjectLiteral::Property*>* properties = |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4082 | new(zone()) ZoneList<ObjectLiteral::Property*>(4); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4083 | int number_of_boilerplate_properties = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4084 | bool has_function = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4085 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4086 | ObjectLiteralPropertyChecker checker(this, top_scope_->language_mode()); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4087 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | Expect(Token::LBRACE, CHECK_OK); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4089 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4090 | while (peek() != Token::RBRACE) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4091 | if (fni_ != NULL) fni_->Enter(); |
| 4092 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4093 | Literal* key = NULL; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4094 | Token::Value next = peek(); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4095 | |
| 4096 | // Location of the property name token |
| 4097 | Scanner::Location loc = scanner().peek_location(); |
| 4098 | |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4099 | switch (next) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4100 | case Token::FUTURE_RESERVED_WORD: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4101 | case Token::FUTURE_STRICT_RESERVED_WORD: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4102 | case Token::IDENTIFIER: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4103 | bool is_getter = false; |
| 4104 | bool is_setter = false; |
| 4105 | Handle<String> id = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4106 | ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4107 | if (fni_ != NULL) fni_->PushLiteralName(id); |
| 4108 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4109 | if ((is_getter || is_setter) && peek() != Token::COLON) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4110 | // Update loc to point to the identifier |
| 4111 | loc = scanner().peek_location(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4112 | ObjectLiteral::Property* property = |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4113 | ParseObjectLiteralGetSet(is_getter, CHECK_OK); |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4114 | if (IsBoilerplateProperty(property)) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4115 | number_of_boilerplate_properties++; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4116 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4117 | // Validate the property. |
| 4118 | checker.CheckProperty(property, loc, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4119 | properties->Add(property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4120 | if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4121 | |
| 4122 | if (fni_ != NULL) { |
| 4123 | fni_->Infer(); |
| 4124 | fni_->Leave(); |
| 4125 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4126 | continue; // restart the while |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4127 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4128 | // Failed to parse as get/set property, so it's just a property |
| 4129 | // called "get" or "set". |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4130 | key = factory()->NewLiteral(id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4131 | break; |
| 4132 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4133 | case Token::STRING: { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4134 | Consume(Token::STRING); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4135 | Handle<String> string = GetSymbol(CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4136 | if (fni_ != NULL) fni_->PushLiteralName(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4137 | uint32_t index; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4138 | if (!string.is_null() && string->AsArrayIndex(&index)) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4139 | key = factory()->NewNumberLiteral(index); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4140 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4141 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4142 | key = factory()->NewLiteral(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4143 | break; |
| 4144 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4145 | case Token::NUMBER: { |
| 4146 | Consume(Token::NUMBER); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4147 | ASSERT(scanner().is_literal_ascii()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 4148 | double value = StringToDouble(isolate()->unicode_cache(), |
| 4149 | scanner().literal_ascii_string(), |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4150 | ALLOW_HEX | ALLOW_OCTALS); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4151 | key = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4152 | break; |
| 4153 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4154 | default: |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4155 | if (Token::IsKeyword(next)) { |
| 4156 | Consume(next); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4157 | Handle<String> string = GetSymbol(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4158 | key = factory()->NewLiteral(string); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4159 | } else { |
| 4160 | // Unexpected token. |
| 4161 | Token::Value next = Next(); |
| 4162 | ReportUnexpectedToken(next); |
| 4163 | *ok = false; |
| 4164 | return NULL; |
| 4165 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | Expect(Token::COLON, CHECK_OK); |
| 4169 | Expression* value = ParseAssignmentExpression(true, CHECK_OK); |
| 4170 | |
| 4171 | ObjectLiteral::Property* property = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 4172 | new(zone()) ObjectLiteral::Property(key, value, isolate()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4173 | |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 4174 | // Mark top-level object literals that contain function literals and |
| 4175 | // pretenure the literal so it can be added as a constant function |
| 4176 | // property. |
| 4177 | if (top_scope_->DeclarationScope()->is_global_scope() && |
| 4178 | value->AsFunctionLiteral() != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4179 | has_function = true; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4180 | value->AsFunctionLiteral()->set_pretenure(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4181 | } |
| 4182 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4183 | // Count CONSTANT or COMPUTED properties to maintain the enumeration order. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 4184 | if (IsBoilerplateProperty(property)) number_of_boilerplate_properties++; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4185 | // Validate the property |
| 4186 | checker.CheckProperty(property, loc, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4187 | properties->Add(property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4188 | |
| 4189 | // TODO(1240767): Consider allowing trailing comma. |
| 4190 | if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4191 | |
| 4192 | if (fni_ != NULL) { |
| 4193 | fni_->Infer(); |
| 4194 | fni_->Leave(); |
| 4195 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4196 | } |
| 4197 | Expect(Token::RBRACE, CHECK_OK); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4198 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4199 | // Computation of literal_index must happen before pre parse bailout. |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4200 | int literal_index = current_function_state_->NextMaterializedLiteralIndex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4201 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4202 | Handle<FixedArray> constant_properties = isolate()->factory()->NewFixedArray( |
| 4203 | number_of_boilerplate_properties * 2, TENURED); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4204 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4205 | bool is_simple = true; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4206 | bool fast_elements = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4207 | int depth = 1; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4208 | BuildObjectLiteralConstantProperties(properties, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4209 | constant_properties, |
| 4210 | &is_simple, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4211 | &fast_elements, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4212 | &depth); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4213 | return factory()->NewObjectLiteral(constant_properties, |
| 4214 | properties, |
| 4215 | literal_index, |
| 4216 | is_simple, |
| 4217 | fast_elements, |
| 4218 | depth, |
| 4219 | has_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | |
| 4223 | Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4224 | if (!scanner().ScanRegExpPattern(seen_equal)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4225 | Next(); |
| 4226 | ReportMessage("unterminated_regexp", Vector<const char*>::empty()); |
| 4227 | *ok = false; |
| 4228 | return NULL; |
| 4229 | } |
| 4230 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4231 | int literal_index = current_function_state_->NextMaterializedLiteralIndex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4232 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4233 | Handle<String> js_pattern = NextLiteralString(TENURED); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4234 | scanner().ScanRegExpFlags(); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4235 | Handle<String> js_flags = NextLiteralString(TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4236 | Next(); |
| 4237 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4238 | return factory()->NewRegExpLiteral(js_pattern, js_flags, literal_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | |
| 4242 | ZoneList<Expression*>* Parser::ParseArguments(bool* ok) { |
| 4243 | // Arguments :: |
| 4244 | // '(' (AssignmentExpression)*[','] ')' |
| 4245 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4246 | ZoneList<Expression*>* result = new(zone()) ZoneList<Expression*>(4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4247 | Expect(Token::LPAREN, CHECK_OK); |
| 4248 | bool done = (peek() == Token::RPAREN); |
| 4249 | while (!done) { |
| 4250 | Expression* argument = ParseAssignmentExpression(true, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4251 | result->Add(argument); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4252 | if (result->length() > kMaxNumFunctionParameters) { |
| 4253 | ReportMessageAt(scanner().location(), "too_many_arguments", |
| 4254 | Vector<const char*>::empty()); |
| 4255 | *ok = false; |
| 4256 | return NULL; |
| 4257 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4258 | done = (peek() == Token::RPAREN); |
| 4259 | if (!done) Expect(Token::COMMA, CHECK_OK); |
| 4260 | } |
| 4261 | Expect(Token::RPAREN, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4262 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4266 | class SingletonLogger : public ParserRecorder { |
| 4267 | public: |
| 4268 | SingletonLogger() : has_error_(false), start_(-1), end_(-1) { } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4269 | virtual ~SingletonLogger() { } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4270 | |
| 4271 | void Reset() { has_error_ = false; } |
| 4272 | |
| 4273 | virtual void LogFunction(int start, |
| 4274 | int end, |
| 4275 | int literals, |
| 4276 | int properties, |
| 4277 | LanguageMode mode) { |
| 4278 | ASSERT(!has_error_); |
| 4279 | start_ = start; |
| 4280 | end_ = end; |
| 4281 | literals_ = literals; |
| 4282 | properties_ = properties; |
| 4283 | mode_ = mode; |
| 4284 | }; |
| 4285 | |
| 4286 | // Logs a symbol creation of a literal or identifier. |
| 4287 | virtual void LogAsciiSymbol(int start, Vector<const char> literal) { } |
| 4288 | virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { } |
| 4289 | |
| 4290 | // Logs an error message and marks the log as containing an error. |
| 4291 | // Further logging will be ignored, and ExtractData will return a vector |
| 4292 | // representing the error only. |
| 4293 | virtual void LogMessage(int start, |
| 4294 | int end, |
| 4295 | const char* message, |
| 4296 | const char* argument_opt) { |
| 4297 | has_error_ = true; |
| 4298 | start_ = start; |
| 4299 | end_ = end; |
| 4300 | message_ = message; |
| 4301 | argument_opt_ = argument_opt; |
| 4302 | } |
| 4303 | |
| 4304 | virtual int function_position() { return 0; } |
| 4305 | |
| 4306 | virtual int symbol_position() { return 0; } |
| 4307 | |
| 4308 | virtual int symbol_ids() { return -1; } |
| 4309 | |
| 4310 | virtual Vector<unsigned> ExtractData() { |
| 4311 | UNREACHABLE(); |
| 4312 | return Vector<unsigned>(); |
| 4313 | } |
| 4314 | |
| 4315 | virtual void PauseRecording() { } |
| 4316 | |
| 4317 | virtual void ResumeRecording() { } |
| 4318 | |
| 4319 | bool has_error() { return has_error_; } |
| 4320 | |
| 4321 | int start() { return start_; } |
| 4322 | int end() { return end_; } |
| 4323 | int literals() { |
| 4324 | ASSERT(!has_error_); |
| 4325 | return literals_; |
| 4326 | } |
| 4327 | int properties() { |
| 4328 | ASSERT(!has_error_); |
| 4329 | return properties_; |
| 4330 | } |
| 4331 | LanguageMode language_mode() { |
| 4332 | ASSERT(!has_error_); |
| 4333 | return mode_; |
| 4334 | } |
| 4335 | const char* message() { |
| 4336 | ASSERT(has_error_); |
| 4337 | return message_; |
| 4338 | } |
| 4339 | const char* argument_opt() { |
| 4340 | ASSERT(has_error_); |
| 4341 | return argument_opt_; |
| 4342 | } |
| 4343 | |
| 4344 | private: |
| 4345 | bool has_error_; |
| 4346 | int start_; |
| 4347 | int end_; |
| 4348 | // For function entries. |
| 4349 | int literals_; |
| 4350 | int properties_; |
| 4351 | LanguageMode mode_; |
| 4352 | // For error messages. |
| 4353 | const char* message_; |
| 4354 | const char* argument_opt_; |
| 4355 | }; |
| 4356 | |
| 4357 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4358 | FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4359 | bool name_is_strict_reserved, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4360 | int function_token_position, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4361 | FunctionLiteral::Type type, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4362 | bool* ok) { |
| 4363 | // Function :: |
| 4364 | // '(' FormalParameterList? ')' '{' FunctionBody '}' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4365 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4366 | // Anonymous functions were passed either the empty symbol or a null |
| 4367 | // handle as the function name. Remember if we were passed a non-empty |
| 4368 | // handle to decide whether to invoke function name inference. |
| 4369 | bool should_infer_name = function_name.is_null(); |
| 4370 | |
| 4371 | // We want a non-null handle as the function name. |
| 4372 | if (should_infer_name) { |
| 4373 | function_name = isolate()->factory()->empty_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | int num_parameters = 0; |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 4377 | // Function declarations are function scoped in normal mode, so they are |
| 4378 | // hoisted. In harmony block scoping mode they are block scoped, so they |
| 4379 | // are not hoisted. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4380 | Scope* scope = (type == FunctionLiteral::DECLARATION && !is_extended_mode()) |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4381 | ? NewScope(top_scope_->DeclarationScope(), FUNCTION_SCOPE) |
| 4382 | : NewScope(top_scope_, FUNCTION_SCOPE); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4383 | ZoneList<Statement*>* body = NULL; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4384 | int materialized_literal_count = -1; |
| 4385 | int expected_property_count = -1; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4386 | int handler_count = 0; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4387 | bool only_simple_this_property_assignments; |
| 4388 | Handle<FixedArray> this_property_assignments; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4389 | FunctionLiteral::ParameterFlag duplicate_parameters = |
| 4390 | FunctionLiteral::kNoDuplicateParameters; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4391 | AstProperties ast_properties; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4392 | // Parse function body. |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4393 | { FunctionState function_state(this, scope, isolate()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4394 | top_scope_->SetScopeName(function_name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4395 | |
| 4396 | // FormalParameterList :: |
| 4397 | // '(' (Identifier)*[','] ')' |
| 4398 | Expect(Token::LPAREN, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4399 | scope->set_start_position(scanner().location().beg_pos); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4400 | Scanner::Location name_loc = Scanner::Location::invalid(); |
| 4401 | Scanner::Location dupe_loc = Scanner::Location::invalid(); |
| 4402 | Scanner::Location reserved_loc = Scanner::Location::invalid(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4403 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4404 | bool done = (peek() == Token::RPAREN); |
| 4405 | while (!done) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4406 | bool is_strict_reserved = false; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4407 | Handle<String> param_name = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4408 | ParseIdentifierOrStrictReservedWord(&is_strict_reserved, |
| 4409 | CHECK_OK); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4410 | |
| 4411 | // Store locations for possible future error reports. |
| 4412 | if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) { |
| 4413 | name_loc = scanner().location(); |
| 4414 | } |
| 4415 | if (!dupe_loc.IsValid() && top_scope_->IsDeclared(param_name)) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4416 | duplicate_parameters = FunctionLiteral::kHasDuplicateParameters; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4417 | dupe_loc = scanner().location(); |
| 4418 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4419 | if (!reserved_loc.IsValid() && is_strict_reserved) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4420 | reserved_loc = scanner().location(); |
| 4421 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4422 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4423 | top_scope_->DeclareParameter(param_name, VAR); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4424 | num_parameters++; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4425 | if (num_parameters > kMaxNumFunctionParameters) { |
| 4426 | ReportMessageAt(scanner().location(), "too_many_parameters", |
| 4427 | Vector<const char*>::empty()); |
| 4428 | *ok = false; |
| 4429 | return NULL; |
| 4430 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4431 | done = (peek() == Token::RPAREN); |
| 4432 | if (!done) Expect(Token::COMMA, CHECK_OK); |
| 4433 | } |
| 4434 | Expect(Token::RPAREN, CHECK_OK); |
| 4435 | |
| 4436 | Expect(Token::LBRACE, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4437 | |
| 4438 | // If we have a named function expression, we add a local variable |
| 4439 | // declaration to the body of the function with the name of the |
| 4440 | // function and let it refer to the function itself (closure). |
| 4441 | // NOTE: We create a proxy and resolve it here so that in the |
| 4442 | // future we can change the AST to only refer to VariableProxies |
| 4443 | // instead of Variables and Proxis as is the case now. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4444 | Variable* fvar = NULL; |
| 4445 | Token::Value fvar_init_op = Token::INIT_CONST; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4446 | if (type == FunctionLiteral::NAMED_EXPRESSION) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4447 | VariableMode fvar_mode; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4448 | if (is_extended_mode()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4449 | fvar_mode = CONST_HARMONY; |
| 4450 | fvar_init_op = Token::INIT_CONST_HARMONY; |
| 4451 | } else { |
| 4452 | fvar_mode = CONST; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4453 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4454 | fvar = |
| 4455 | top_scope_->DeclareFunctionVar(function_name, fvar_mode, factory()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4458 | // Determine whether the function will be lazily compiled. |
| 4459 | // The heuristics are: |
| 4460 | // - It must not have been prohibited by the caller to Parse (some callers |
| 4461 | // need a full AST). |
| 4462 | // - The outer scope must be trivial (only global variables in scope). |
| 4463 | // - The function mustn't be a function expression with an open parenthesis |
| 4464 | // before; we consider that a hint that the function will be called |
| 4465 | // immediately, and it would be a waste of time to make it lazily |
| 4466 | // compiled. |
| 4467 | // These are all things we can know at this point, without looking at the |
| 4468 | // function itself. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4469 | bool is_lazily_compiled = (mode() == PARSE_LAZILY && |
| 4470 | top_scope_->outer_scope()->is_global_scope() && |
| 4471 | top_scope_->HasTrivialOuterContext() && |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4472 | !parenthesized_function_); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4473 | parenthesized_function_ = false; // The bit was set for this function only. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4474 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4475 | if (is_lazily_compiled) { |
| 4476 | int function_block_pos = scanner().location().beg_pos; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4477 | FunctionEntry entry; |
| 4478 | if (pre_data_ != NULL) { |
| 4479 | // If we have pre_data_, we use it to skip parsing the function body. |
| 4480 | // the preparser data contains the information we need to construct the |
| 4481 | // lazy function. |
| 4482 | entry = pre_data()->GetFunctionEntry(function_block_pos); |
| 4483 | if (entry.is_valid()) { |
| 4484 | if (entry.end_pos() <= function_block_pos) { |
| 4485 | // End position greater than end of stream is safe, and hard |
| 4486 | // to check. |
| 4487 | ReportInvalidPreparseData(function_name, CHECK_OK); |
| 4488 | } |
| 4489 | scanner().SeekForward(entry.end_pos() - 1); |
| 4490 | |
| 4491 | scope->set_end_position(entry.end_pos()); |
| 4492 | Expect(Token::RBRACE, CHECK_OK); |
| 4493 | isolate()->counters()->total_preparse_skipped()->Increment( |
| 4494 | scope->end_position() - function_block_pos); |
| 4495 | materialized_literal_count = entry.literal_count(); |
| 4496 | expected_property_count = entry.property_count(); |
| 4497 | top_scope_->SetLanguageMode(entry.language_mode()); |
| 4498 | only_simple_this_property_assignments = false; |
| 4499 | this_property_assignments = isolate()->factory()->empty_fixed_array(); |
| 4500 | } else { |
| 4501 | is_lazily_compiled = false; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4502 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4503 | } else { |
| 4504 | // With no preparser data, we partially parse the function, without |
| 4505 | // building an AST. This gathers the data needed to build a lazy |
| 4506 | // function. |
| 4507 | SingletonLogger logger; |
| 4508 | preparser::PreParser::PreParseResult result = |
| 4509 | LazyParseFunctionLiteral(&logger); |
| 4510 | if (result == preparser::PreParser::kPreParseStackOverflow) { |
| 4511 | // Propagate stack overflow. |
| 4512 | stack_overflow_ = true; |
| 4513 | *ok = false; |
| 4514 | return NULL; |
| 4515 | } |
| 4516 | if (logger.has_error()) { |
| 4517 | const char* arg = logger.argument_opt(); |
| 4518 | Vector<const char*> args; |
| 4519 | if (arg != NULL) { |
| 4520 | args = Vector<const char*>(&arg, 1); |
| 4521 | } |
| 4522 | ReportMessageAt(Scanner::Location(logger.start(), logger.end()), |
| 4523 | logger.message(), args); |
| 4524 | *ok = false; |
| 4525 | return NULL; |
| 4526 | } |
| 4527 | scope->set_end_position(logger.end()); |
| 4528 | Expect(Token::RBRACE, CHECK_OK); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4529 | isolate()->counters()->total_preparse_skipped()->Increment( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4530 | scope->end_position() - function_block_pos); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4531 | materialized_literal_count = logger.literals(); |
| 4532 | expected_property_count = logger.properties(); |
| 4533 | top_scope_->SetLanguageMode(logger.language_mode()); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4534 | only_simple_this_property_assignments = false; |
| 4535 | this_property_assignments = isolate()->factory()->empty_fixed_array(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 4536 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | if (!is_lazily_compiled) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4540 | body = new(zone()) ZoneList<Statement*>(8); |
| 4541 | if (fvar != NULL) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4542 | VariableProxy* fproxy = |
| 4543 | top_scope_->NewUnresolved(factory(), function_name); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4544 | fproxy->BindTo(fvar); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4545 | body->Add(factory()->NewExpressionStatement( |
| 4546 | factory()->NewAssignment(fvar_init_op, |
| 4547 | fproxy, |
| 4548 | factory()->NewThisFunction(), |
| 4549 | RelocInfo::kNoPosition))); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4550 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4551 | ParseSourceElements(body, Token::RBRACE, CHECK_OK); |
| 4552 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4553 | materialized_literal_count = function_state.materialized_literal_count(); |
| 4554 | expected_property_count = function_state.expected_property_count(); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4555 | handler_count = function_state.handler_count(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 4556 | only_simple_this_property_assignments = |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4557 | function_state.only_simple_this_property_assignments(); |
| 4558 | this_property_assignments = function_state.this_property_assignments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4559 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4560 | Expect(Token::RBRACE, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4561 | scope->set_end_position(scanner().location().end_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4564 | // Validate strict mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4565 | if (!top_scope_->is_classic_mode()) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4566 | if (IsEvalOrArguments(function_name)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4567 | int start_pos = scope->start_position(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4568 | int position = function_token_position != RelocInfo::kNoPosition |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4569 | ? function_token_position |
| 4570 | : (start_pos > 0 ? start_pos - 1 : start_pos); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4571 | Scanner::Location location = Scanner::Location(position, start_pos); |
| 4572 | ReportMessageAt(location, |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4573 | "strict_function_name", Vector<const char*>::empty()); |
| 4574 | *ok = false; |
| 4575 | return NULL; |
| 4576 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4577 | if (name_loc.IsValid()) { |
| 4578 | ReportMessageAt(name_loc, "strict_param_name", |
| 4579 | Vector<const char*>::empty()); |
| 4580 | *ok = false; |
| 4581 | return NULL; |
| 4582 | } |
| 4583 | if (dupe_loc.IsValid()) { |
| 4584 | ReportMessageAt(dupe_loc, "strict_param_dupe", |
| 4585 | Vector<const char*>::empty()); |
| 4586 | *ok = false; |
| 4587 | return NULL; |
| 4588 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4589 | if (name_is_strict_reserved) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4590 | int start_pos = scope->start_position(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4591 | int position = function_token_position != RelocInfo::kNoPosition |
| 4592 | ? function_token_position |
| 4593 | : (start_pos > 0 ? start_pos - 1 : start_pos); |
| 4594 | Scanner::Location location = Scanner::Location(position, start_pos); |
| 4595 | ReportMessageAt(location, "strict_reserved_word", |
| 4596 | Vector<const char*>::empty()); |
| 4597 | *ok = false; |
| 4598 | return NULL; |
| 4599 | } |
| 4600 | if (reserved_loc.IsValid()) { |
| 4601 | ReportMessageAt(reserved_loc, "strict_reserved_word", |
| 4602 | Vector<const char*>::empty()); |
| 4603 | *ok = false; |
| 4604 | return NULL; |
| 4605 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4606 | CheckOctalLiteral(scope->start_position(), |
| 4607 | scope->end_position(), |
| 4608 | CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4609 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4610 | ast_properties = *factory()->visitor()->ast_properties(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4611 | } |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4612 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4613 | if (is_extended_mode()) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 4614 | CheckConflictingVarDeclarations(scope, CHECK_OK); |
| 4615 | } |
| 4616 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4617 | FunctionLiteral* function_literal = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4618 | factory()->NewFunctionLiteral(function_name, |
| 4619 | scope, |
| 4620 | body, |
| 4621 | materialized_literal_count, |
| 4622 | expected_property_count, |
| 4623 | handler_count, |
| 4624 | only_simple_this_property_assignments, |
| 4625 | this_property_assignments, |
| 4626 | num_parameters, |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4627 | duplicate_parameters, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4628 | type, |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4629 | FunctionLiteral::kIsFunction); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4630 | function_literal->set_function_token_position(function_token_position); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4631 | function_literal->set_ast_properties(&ast_properties); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4632 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4633 | if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4634 | return function_literal; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4638 | preparser::PreParser::PreParseResult Parser::LazyParseFunctionLiteral( |
| 4639 | SingletonLogger* logger) { |
| 4640 | HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse()); |
| 4641 | ASSERT_EQ(Token::LBRACE, scanner().current_token()); |
| 4642 | |
| 4643 | if (reusable_preparser_ == NULL) { |
| 4644 | intptr_t stack_limit = isolate()->stack_guard()->real_climit(); |
| 4645 | bool do_allow_lazy = true; |
| 4646 | reusable_preparser_ = new preparser::PreParser(&scanner_, |
| 4647 | NULL, |
| 4648 | stack_limit, |
| 4649 | do_allow_lazy, |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 4650 | allow_natives_syntax_, |
| 4651 | allow_modules_); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4652 | } |
| 4653 | preparser::PreParser::PreParseResult result = |
| 4654 | reusable_preparser_->PreParseLazyFunction(top_scope_->language_mode(), |
| 4655 | logger); |
| 4656 | return result; |
| 4657 | } |
| 4658 | |
| 4659 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4660 | Expression* Parser::ParseV8Intrinsic(bool* ok) { |
| 4661 | // CallRuntime :: |
| 4662 | // '%' Identifier Arguments |
| 4663 | |
| 4664 | Expect(Token::MOD, CHECK_OK); |
| 4665 | Handle<String> name = ParseIdentifier(CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4666 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4667 | |
| 4668 | if (extension_ != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4669 | // The extension structures are only accessible while parsing the |
| 4670 | // very first time not when reparsing because of lazy compilation. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4671 | top_scope_->DeclarationScope()->ForceEagerCompilation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4674 | const Runtime::Function* function = Runtime::FunctionForSymbol(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4675 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4676 | // Check for built-in IS_VAR macro. |
| 4677 | if (function != NULL && |
| 4678 | function->intrinsic_type == Runtime::RUNTIME && |
| 4679 | function->function_id == Runtime::kIS_VAR) { |
| 4680 | // %IS_VAR(x) evaluates to x if x is a variable, |
| 4681 | // leads to a parse error otherwise. Could be implemented as an |
| 4682 | // inline function %_IS_VAR(x) to eliminate this special case. |
| 4683 | if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) { |
| 4684 | return args->at(0); |
| 4685 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4686 | ReportMessage("unable_to_parse", Vector<const char*>::empty()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4687 | *ok = false; |
| 4688 | return NULL; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4689 | } |
| 4690 | } |
| 4691 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4692 | // Check that the expected number of arguments are being passed. |
| 4693 | if (function != NULL && |
| 4694 | function->nargs != -1 && |
| 4695 | function->nargs != args->length()) { |
| 4696 | ReportMessage("illegal_access", Vector<const char*>::empty()); |
| 4697 | *ok = false; |
| 4698 | return NULL; |
| 4699 | } |
| 4700 | |
| 4701 | // We have a valid intrinsics call or a call to a builtin. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4702 | return factory()->NewCallRuntime(name, function, args); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4706 | bool Parser::peek_any_identifier() { |
| 4707 | Token::Value next = peek(); |
| 4708 | return next == Token::IDENTIFIER || |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4709 | next == Token::FUTURE_RESERVED_WORD || |
| 4710 | next == Token::FUTURE_STRICT_RESERVED_WORD; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4714 | void Parser::Consume(Token::Value token) { |
| 4715 | Token::Value next = Next(); |
| 4716 | USE(next); |
| 4717 | USE(token); |
| 4718 | ASSERT(next == token); |
| 4719 | } |
| 4720 | |
| 4721 | |
| 4722 | void Parser::Expect(Token::Value token, bool* ok) { |
| 4723 | Token::Value next = Next(); |
| 4724 | if (next == token) return; |
| 4725 | ReportUnexpectedToken(next); |
| 4726 | *ok = false; |
| 4727 | } |
| 4728 | |
| 4729 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4730 | bool Parser::Check(Token::Value token) { |
| 4731 | Token::Value next = peek(); |
| 4732 | if (next == token) { |
| 4733 | Consume(next); |
| 4734 | return true; |
| 4735 | } |
| 4736 | return false; |
| 4737 | } |
| 4738 | |
| 4739 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4740 | void Parser::ExpectSemicolon(bool* ok) { |
| 4741 | // Check for automatic semicolon insertion according to |
| 4742 | // the rules given in ECMA-262, section 7.9, page 21. |
| 4743 | Token::Value tok = peek(); |
| 4744 | if (tok == Token::SEMICOLON) { |
| 4745 | Next(); |
| 4746 | return; |
| 4747 | } |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 4748 | if (scanner().HasAnyLineTerminatorBeforeNext() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4749 | tok == Token::RBRACE || |
| 4750 | tok == Token::EOS) { |
| 4751 | return; |
| 4752 | } |
| 4753 | Expect(Token::SEMICOLON, ok); |
| 4754 | } |
| 4755 | |
| 4756 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 4757 | void Parser::ExpectContextualKeyword(const char* keyword, bool* ok) { |
| 4758 | Expect(Token::IDENTIFIER, ok); |
| 4759 | if (!*ok) return; |
| 4760 | Handle<String> symbol = GetSymbol(ok); |
| 4761 | if (!*ok) return; |
| 4762 | if (!symbol->IsEqualTo(CStrVector(keyword))) { |
| 4763 | *ok = false; |
| 4764 | ReportUnexpectedToken(scanner().current_token()); |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4769 | Literal* Parser::GetLiteralUndefined() { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4770 | return factory()->NewLiteral(isolate()->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | |
| 4774 | Literal* Parser::GetLiteralTheHole() { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4775 | return factory()->NewLiteral(isolate()->factory()->the_hole_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 4779 | // Parses an identifier that is valid for the current scope, in particular it |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4780 | // fails on strict mode future reserved keywords in a strict scope. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4781 | Handle<String> Parser::ParseIdentifier(bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4782 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4783 | Expect(Token::IDENTIFIER, ok); |
| 4784 | } else if (!Check(Token::IDENTIFIER)) { |
| 4785 | Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); |
| 4786 | } |
| 4787 | if (!*ok) return Handle<String>(); |
| 4788 | return GetSymbol(ok); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4792 | // Parses and identifier or a strict mode future reserved word, and indicate |
| 4793 | // whether it is strict mode future reserved. |
| 4794 | Handle<String> Parser::ParseIdentifierOrStrictReservedWord( |
| 4795 | bool* is_strict_reserved, bool* ok) { |
| 4796 | *is_strict_reserved = false; |
| 4797 | if (!Check(Token::IDENTIFIER)) { |
| 4798 | Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); |
| 4799 | *is_strict_reserved = true; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4800 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4801 | if (!*ok) return Handle<String>(); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4802 | return GetSymbol(ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4805 | |
| 4806 | Handle<String> Parser::ParseIdentifierName(bool* ok) { |
| 4807 | Token::Value next = Next(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4808 | if (next != Token::IDENTIFIER && |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4809 | next != Token::FUTURE_RESERVED_WORD && |
| 4810 | next != Token::FUTURE_STRICT_RESERVED_WORD && |
| 4811 | !Token::IsKeyword(next)) { |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4812 | ReportUnexpectedToken(next); |
| 4813 | *ok = false; |
| 4814 | return Handle<String>(); |
| 4815 | } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4816 | return GetSymbol(ok); |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4819 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4820 | void Parser::MarkAsLValue(Expression* expression) { |
| 4821 | VariableProxy* proxy = expression != NULL |
| 4822 | ? expression->AsVariableProxy() |
| 4823 | : NULL; |
| 4824 | |
| 4825 | if (proxy != NULL) proxy->MarkAsLValue(); |
| 4826 | } |
| 4827 | |
| 4828 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4829 | // Checks LHS expression for assignment and prefix/postfix increment/decrement |
| 4830 | // in strict mode. |
| 4831 | void Parser::CheckStrictModeLValue(Expression* expression, |
| 4832 | const char* error, |
| 4833 | bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4834 | ASSERT(!top_scope_->is_classic_mode()); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4835 | VariableProxy* lhs = expression != NULL |
| 4836 | ? expression->AsVariableProxy() |
| 4837 | : NULL; |
| 4838 | |
| 4839 | if (lhs != NULL && !lhs->is_this() && IsEvalOrArguments(lhs->name())) { |
| 4840 | ReportMessage(error, Vector<const char*>::empty()); |
| 4841 | *ok = false; |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4846 | // Checks whether an octal literal was last seen between beg_pos and end_pos. |
| 4847 | // If so, reports an error. Only called for strict mode. |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 4848 | void Parser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4849 | Scanner::Location octal = scanner().octal_position(); |
| 4850 | if (octal.IsValid() && |
| 4851 | beg_pos <= octal.beg_pos && |
| 4852 | octal.end_pos <= end_pos) { |
| 4853 | ReportMessageAt(octal, "strict_octal_literal", |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 4854 | Vector<const char*>::empty()); |
| 4855 | scanner().clear_octal_position(); |
| 4856 | *ok = false; |
| 4857 | } |
| 4858 | } |
| 4859 | |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4860 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 4861 | void Parser::CheckConflictingVarDeclarations(Scope* scope, bool* ok) { |
| 4862 | Declaration* decl = scope->CheckConflictingVarDeclarations(); |
| 4863 | if (decl != NULL) { |
| 4864 | // In harmony mode we treat conflicting variable bindinds as early |
| 4865 | // errors. See ES5 16 for a definition of early errors. |
| 4866 | Handle<String> name = decl->proxy()->name(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4867 | SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 4868 | const char* elms[2] = { "Variable", *c_string }; |
| 4869 | Vector<const char*> args(elms, 2); |
| 4870 | int position = decl->proxy()->position(); |
| 4871 | Scanner::Location location = position == RelocInfo::kNoPosition |
| 4872 | ? Scanner::Location::invalid() |
| 4873 | : Scanner::Location(position, position + 1); |
| 4874 | ReportMessageAt(location, "redeclaration", args); |
| 4875 | *ok = false; |
| 4876 | } |
| 4877 | } |
| 4878 | |
| 4879 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4880 | // This function reads an identifier name and determines whether or not it |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4881 | // is 'get' or 'set'. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4882 | Handle<String> Parser::ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 4883 | bool* is_set, |
| 4884 | bool* ok) { |
| 4885 | Handle<String> result = ParseIdentifierName(ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4886 | if (!*ok) return Handle<String>(); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4887 | if (scanner().is_literal_ascii() && scanner().literal_length() == 3) { |
| 4888 | const char* token = scanner().literal_ascii_string().start(); |
| 4889 | *is_get = strncmp(token, "get", 3) == 0; |
| 4890 | *is_set = !*is_get && strncmp(token, "set", 3) == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4891 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4892 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | |
| 4896 | // ---------------------------------------------------------------------------- |
| 4897 | // Parser support |
| 4898 | |
| 4899 | |
| 4900 | bool Parser::TargetStackContainsLabel(Handle<String> label) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4901 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4902 | BreakableStatement* stat = t->node()->AsBreakableStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4903 | if (stat != NULL && ContainsLabel(stat->labels(), label)) |
| 4904 | return true; |
| 4905 | } |
| 4906 | return false; |
| 4907 | } |
| 4908 | |
| 4909 | |
| 4910 | BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) { |
| 4911 | bool anonymous = label.is_null(); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4912 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4913 | BreakableStatement* stat = t->node()->AsBreakableStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4914 | if (stat == NULL) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4915 | if ((anonymous && stat->is_target_for_anonymous()) || |
| 4916 | (!anonymous && ContainsLabel(stat->labels(), label))) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4917 | RegisterTargetUse(stat->break_target(), t->previous()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4918 | return stat; |
| 4919 | } |
| 4920 | } |
| 4921 | return NULL; |
| 4922 | } |
| 4923 | |
| 4924 | |
| 4925 | IterationStatement* Parser::LookupContinueTarget(Handle<String> label, |
| 4926 | bool* ok) { |
| 4927 | bool anonymous = label.is_null(); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4928 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4929 | IterationStatement* stat = t->node()->AsIterationStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4930 | if (stat == NULL) continue; |
| 4931 | |
| 4932 | ASSERT(stat->is_target_for_anonymous()); |
| 4933 | if (anonymous || ContainsLabel(stat->labels(), label)) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4934 | RegisterTargetUse(stat->continue_target(), t->previous()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4935 | return stat; |
| 4936 | } |
| 4937 | } |
| 4938 | return NULL; |
| 4939 | } |
| 4940 | |
| 4941 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4942 | void Parser::RegisterTargetUse(Label* target, Target* stop) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4943 | // Register that a break target found at the given stop in the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4944 | // target stack has been used from the top of the target stack. Add |
| 4945 | // the break target to any TargetCollectors passed on the stack. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4946 | for (Target* t = target_stack_; t != stop; t = t->previous()) { |
| 4947 | TargetCollector* collector = t->node()->AsTargetCollector(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4948 | if (collector != NULL) collector->AddTarget(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4953 | Expression* Parser::NewThrowReferenceError(Handle<String> type) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4954 | return NewThrowError(isolate()->factory()->MakeReferenceError_symbol(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4955 | type, HandleVector<Object>(NULL, 0)); |
| 4956 | } |
| 4957 | |
| 4958 | |
| 4959 | Expression* Parser::NewThrowSyntaxError(Handle<String> type, |
| 4960 | Handle<Object> first) { |
| 4961 | int argc = first.is_null() ? 0 : 1; |
| 4962 | Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4963 | return NewThrowError( |
| 4964 | isolate()->factory()->MakeSyntaxError_symbol(), type, arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | |
| 4968 | Expression* Parser::NewThrowTypeError(Handle<String> type, |
| 4969 | Handle<Object> first, |
| 4970 | Handle<Object> second) { |
| 4971 | ASSERT(!first.is_null() && !second.is_null()); |
| 4972 | Handle<Object> elements[] = { first, second }; |
| 4973 | Vector< Handle<Object> > arguments = |
| 4974 | HandleVector<Object>(elements, ARRAY_SIZE(elements)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4975 | return NewThrowError( |
| 4976 | isolate()->factory()->MakeTypeError_symbol(), type, arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | |
| 4980 | Expression* Parser::NewThrowError(Handle<String> constructor, |
| 4981 | Handle<String> type, |
| 4982 | Vector< Handle<Object> > arguments) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4983 | int argc = arguments.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4984 | Handle<FixedArray> elements = isolate()->factory()->NewFixedArray(argc, |
| 4985 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4986 | for (int i = 0; i < argc; i++) { |
| 4987 | Handle<Object> element = arguments[i]; |
| 4988 | if (!element.is_null()) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4989 | elements->set(i, *element); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4990 | } |
| 4991 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4992 | Handle<JSArray> array = isolate()->factory()->NewJSArrayWithElements( |
| 4993 | elements, FAST_ELEMENTS, TENURED); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4994 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4995 | ZoneList<Expression*>* args = new(zone()) ZoneList<Expression*>(2); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4996 | args->Add(factory()->NewLiteral(type)); |
| 4997 | args->Add(factory()->NewLiteral(array)); |
| 4998 | CallRuntime* call_constructor = |
| 4999 | factory()->NewCallRuntime(constructor, NULL, args); |
| 5000 | return factory()->NewThrow(call_constructor, scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5003 | // ---------------------------------------------------------------------------- |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5004 | // Regular expressions |
| 5005 | |
| 5006 | |
| 5007 | RegExpParser::RegExpParser(FlatStringReader* in, |
| 5008 | Handle<String>* error, |
| 5009 | bool multiline) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5010 | : isolate_(Isolate::Current()), |
| 5011 | error_(error), |
| 5012 | captures_(NULL), |
| 5013 | in_(in), |
| 5014 | current_(kEndMarker), |
| 5015 | next_pos_(0), |
| 5016 | capture_count_(0), |
| 5017 | has_more_(true), |
| 5018 | multiline_(multiline), |
| 5019 | simple_(false), |
| 5020 | contains_anchor_(false), |
| 5021 | is_scanned_for_captures_(false), |
| 5022 | failed_(false) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5023 | Advance(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | |
| 5027 | uc32 RegExpParser::Next() { |
| 5028 | if (has_next()) { |
| 5029 | return in()->Get(next_pos_); |
| 5030 | } else { |
| 5031 | return kEndMarker; |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | |
| 5036 | void RegExpParser::Advance() { |
| 5037 | if (next_pos_ < in()->length()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5038 | StackLimitCheck check(isolate()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5039 | if (check.HasOverflowed()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5040 | ReportError(CStrVector(Isolate::kStackOverflowMessage)); |
| 5041 | } else if (isolate()->zone()->excess_allocation()) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5042 | ReportError(CStrVector("Regular expression too large")); |
| 5043 | } else { |
| 5044 | current_ = in()->Get(next_pos_); |
| 5045 | next_pos_++; |
| 5046 | } |
| 5047 | } else { |
| 5048 | current_ = kEndMarker; |
| 5049 | has_more_ = false; |
| 5050 | } |
| 5051 | } |
| 5052 | |
| 5053 | |
| 5054 | void RegExpParser::Reset(int pos) { |
| 5055 | next_pos_ = pos; |
| 5056 | Advance(); |
| 5057 | } |
| 5058 | |
| 5059 | |
| 5060 | void RegExpParser::Advance(int dist) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5061 | next_pos_ += dist - 1; |
| 5062 | Advance(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5066 | bool RegExpParser::simple() { |
| 5067 | return simple_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | RegExpTree* RegExpParser::ReportError(Vector<const char> message) { |
| 5071 | failed_ = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5072 | *error_ = isolate()->factory()->NewStringFromAscii(message, NOT_TENURED); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5073 | // Zip to the end to make sure the no more input is read. |
| 5074 | current_ = kEndMarker; |
| 5075 | next_pos_ = in()->length(); |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | |
| 5079 | |
| 5080 | // Pattern :: |
| 5081 | // Disjunction |
| 5082 | RegExpTree* RegExpParser::ParsePattern() { |
| 5083 | RegExpTree* result = ParseDisjunction(CHECK_FAILED); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5084 | ASSERT(!has_more()); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5085 | // If the result of parsing is a literal string atom, and it has the |
| 5086 | // same length as the input, then the atom is identical to the input. |
| 5087 | if (result->IsAtom() && result->AsAtom()->length() == in()->length()) { |
| 5088 | simple_ = true; |
| 5089 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5090 | return result; |
| 5091 | } |
| 5092 | |
| 5093 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5094 | // Disjunction :: |
| 5095 | // Alternative |
| 5096 | // Alternative | Disjunction |
| 5097 | // Alternative :: |
| 5098 | // [empty] |
| 5099 | // Term Alternative |
| 5100 | // Term :: |
| 5101 | // Assertion |
| 5102 | // Atom |
| 5103 | // Atom Quantifier |
| 5104 | RegExpTree* RegExpParser::ParseDisjunction() { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5105 | // Used to store current state while parsing subexpressions. |
| 5106 | RegExpParserState initial_state(NULL, INITIAL, 0); |
| 5107 | RegExpParserState* stored_state = &initial_state; |
| 5108 | // Cache the builder in a local variable for quick access. |
| 5109 | RegExpBuilder* builder = initial_state.builder(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5110 | while (true) { |
| 5111 | switch (current()) { |
| 5112 | case kEndMarker: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5113 | if (stored_state->IsSubexpression()) { |
| 5114 | // Inside a parenthesized group when hitting end of input. |
| 5115 | ReportError(CStrVector("Unterminated group") CHECK_FAILED); |
| 5116 | } |
| 5117 | ASSERT_EQ(INITIAL, stored_state->group_type()); |
| 5118 | // Parsing completed successfully. |
| 5119 | return builder->ToRegExp(); |
| 5120 | case ')': { |
| 5121 | if (!stored_state->IsSubexpression()) { |
| 5122 | ReportError(CStrVector("Unmatched ')'") CHECK_FAILED); |
| 5123 | } |
| 5124 | ASSERT_NE(INITIAL, stored_state->group_type()); |
| 5125 | |
| 5126 | Advance(); |
| 5127 | // End disjunction parsing and convert builder content to new single |
| 5128 | // regexp atom. |
| 5129 | RegExpTree* body = builder->ToRegExp(); |
| 5130 | |
| 5131 | int end_capture_index = captures_started(); |
| 5132 | |
| 5133 | int capture_index = stored_state->capture_index(); |
| 5134 | SubexpressionType type = stored_state->group_type(); |
| 5135 | |
| 5136 | // Restore previous state. |
| 5137 | stored_state = stored_state->previous_state(); |
| 5138 | builder = stored_state->builder(); |
| 5139 | |
| 5140 | // Build result of subexpression. |
| 5141 | if (type == CAPTURE) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5142 | RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5143 | captures_->at(capture_index - 1) = capture; |
| 5144 | body = capture; |
| 5145 | } else if (type != GROUPING) { |
| 5146 | ASSERT(type == POSITIVE_LOOKAHEAD || type == NEGATIVE_LOOKAHEAD); |
| 5147 | bool is_positive = (type == POSITIVE_LOOKAHEAD); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5148 | body = new(zone()) RegExpLookahead(body, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5149 | is_positive, |
| 5150 | end_capture_index - capture_index, |
| 5151 | capture_index); |
| 5152 | } |
| 5153 | builder->AddAtom(body); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 5154 | // For compatability with JSC and ES3, we allow quantifiers after |
| 5155 | // lookaheads, and break in all cases. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5156 | break; |
| 5157 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5158 | case '|': { |
| 5159 | Advance(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5160 | builder->NewAlternative(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5161 | continue; |
| 5162 | } |
| 5163 | case '*': |
| 5164 | case '+': |
| 5165 | case '?': |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5166 | return ReportError(CStrVector("Nothing to repeat")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5167 | case '^': { |
| 5168 | Advance(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5169 | if (multiline_) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5170 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5171 | new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE)); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5172 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5173 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5174 | new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT)); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5175 | set_contains_anchor(); |
| 5176 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5177 | continue; |
| 5178 | } |
| 5179 | case '$': { |
| 5180 | Advance(); |
| 5181 | RegExpAssertion::Type type = |
| 5182 | multiline_ ? RegExpAssertion::END_OF_LINE : |
| 5183 | RegExpAssertion::END_OF_INPUT; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5184 | builder->AddAssertion(new(zone()) RegExpAssertion(type)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5185 | continue; |
| 5186 | } |
| 5187 | case '.': { |
| 5188 | Advance(); |
| 5189 | // everything except \x0a, \x0d, \u2028 and \u2029 |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5190 | ZoneList<CharacterRange>* ranges = |
| 5191 | new(zone()) ZoneList<CharacterRange>(2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5192 | CharacterRange::AddClassEscape('.', ranges); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5193 | RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5194 | builder->AddAtom(atom); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5195 | break; |
| 5196 | } |
| 5197 | case '(': { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5198 | SubexpressionType type = CAPTURE; |
| 5199 | Advance(); |
| 5200 | if (current() == '?') { |
| 5201 | switch (Next()) { |
| 5202 | case ':': |
| 5203 | type = GROUPING; |
| 5204 | break; |
| 5205 | case '=': |
| 5206 | type = POSITIVE_LOOKAHEAD; |
| 5207 | break; |
| 5208 | case '!': |
| 5209 | type = NEGATIVE_LOOKAHEAD; |
| 5210 | break; |
| 5211 | default: |
| 5212 | ReportError(CStrVector("Invalid group") CHECK_FAILED); |
| 5213 | break; |
| 5214 | } |
| 5215 | Advance(2); |
| 5216 | } else { |
| 5217 | if (captures_ == NULL) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5218 | captures_ = new(zone()) ZoneList<RegExpCapture*>(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5219 | } |
| 5220 | if (captures_started() >= kMaxCaptures) { |
| 5221 | ReportError(CStrVector("Too many captures") CHECK_FAILED); |
| 5222 | } |
| 5223 | captures_->Add(NULL); |
| 5224 | } |
| 5225 | // Store current state and begin new disjunction parsing. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5226 | stored_state = new(zone()) RegExpParserState(stored_state, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5227 | type, |
| 5228 | captures_started()); |
| 5229 | builder = stored_state->builder(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 5230 | continue; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5231 | } |
| 5232 | case '[': { |
| 5233 | RegExpTree* atom = ParseCharacterClass(CHECK_FAILED); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5234 | builder->AddAtom(atom); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5235 | break; |
| 5236 | } |
| 5237 | // Atom :: |
| 5238 | // \ AtomEscape |
| 5239 | case '\\': |
| 5240 | switch (Next()) { |
| 5241 | case kEndMarker: |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5242 | return ReportError(CStrVector("\\ at end of pattern")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5243 | case 'b': |
| 5244 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5245 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5246 | new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5247 | continue; |
| 5248 | case 'B': |
| 5249 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5250 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5251 | new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5252 | continue; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 5253 | // AtomEscape :: |
| 5254 | // CharacterClassEscape |
| 5255 | // |
| 5256 | // CharacterClassEscape :: one of |
| 5257 | // d D s S w W |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5258 | case 'd': case 'D': case 's': case 'S': case 'w': case 'W': { |
| 5259 | uc32 c = Next(); |
| 5260 | Advance(2); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5261 | ZoneList<CharacterRange>* ranges = |
| 5262 | new(zone()) ZoneList<CharacterRange>(2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5263 | CharacterRange::AddClassEscape(c, ranges); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5264 | RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5265 | builder->AddAtom(atom); |
| 5266 | break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5267 | } |
| 5268 | case '1': case '2': case '3': case '4': case '5': case '6': |
| 5269 | case '7': case '8': case '9': { |
| 5270 | int index = 0; |
| 5271 | if (ParseBackReferenceIndex(&index)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5272 | RegExpCapture* capture = NULL; |
| 5273 | if (captures_ != NULL && index <= captures_->length()) { |
| 5274 | capture = captures_->at(index - 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5275 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5276 | if (capture == NULL) { |
| 5277 | builder->AddEmpty(); |
| 5278 | break; |
| 5279 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5280 | RegExpTree* atom = new(zone()) RegExpBackReference(capture); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5281 | builder->AddAtom(atom); |
| 5282 | break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5283 | } |
| 5284 | uc32 first_digit = Next(); |
| 5285 | if (first_digit == '8' || first_digit == '9') { |
| 5286 | // Treat as identity escape |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5287 | builder->AddCharacter(first_digit); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5288 | Advance(2); |
| 5289 | break; |
| 5290 | } |
| 5291 | } |
| 5292 | // FALLTHROUGH |
| 5293 | case '0': { |
| 5294 | Advance(); |
| 5295 | uc32 octal = ParseOctalLiteral(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5296 | builder->AddCharacter(octal); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5297 | break; |
| 5298 | } |
| 5299 | // ControlEscape :: one of |
| 5300 | // f n r t v |
| 5301 | case 'f': |
| 5302 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5303 | builder->AddCharacter('\f'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5304 | break; |
| 5305 | case 'n': |
| 5306 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5307 | builder->AddCharacter('\n'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5308 | break; |
| 5309 | case 'r': |
| 5310 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5311 | builder->AddCharacter('\r'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5312 | break; |
| 5313 | case 't': |
| 5314 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5315 | builder->AddCharacter('\t'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5316 | break; |
| 5317 | case 'v': |
| 5318 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5319 | builder->AddCharacter('\v'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5320 | break; |
| 5321 | case 'c': { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 5322 | Advance(); |
| 5323 | uc32 controlLetter = Next(); |
| 5324 | // Special case if it is an ASCII letter. |
| 5325 | // Convert lower case letters to uppercase. |
| 5326 | uc32 letter = controlLetter & ~('a' ^ 'A'); |
| 5327 | if (letter < 'A' || 'Z' < letter) { |
| 5328 | // controlLetter is not in range 'A'-'Z' or 'a'-'z'. |
| 5329 | // This is outside the specification. We match JSC in |
| 5330 | // reading the backslash as a literal character instead |
| 5331 | // of as starting an escape. |
| 5332 | builder->AddCharacter('\\'); |
| 5333 | } else { |
| 5334 | Advance(2); |
| 5335 | builder->AddCharacter(controlLetter & 0x1f); |
| 5336 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5337 | break; |
| 5338 | } |
| 5339 | case 'x': { |
| 5340 | Advance(2); |
| 5341 | uc32 value; |
| 5342 | if (ParseHexEscape(2, &value)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5343 | builder->AddCharacter(value); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5344 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5345 | builder->AddCharacter('x'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5346 | } |
| 5347 | break; |
| 5348 | } |
| 5349 | case 'u': { |
| 5350 | Advance(2); |
| 5351 | uc32 value; |
| 5352 | if (ParseHexEscape(4, &value)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5353 | builder->AddCharacter(value); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5354 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5355 | builder->AddCharacter('u'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5356 | } |
| 5357 | break; |
| 5358 | } |
| 5359 | default: |
| 5360 | // Identity escape. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5361 | builder->AddCharacter(Next()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5362 | Advance(2); |
| 5363 | break; |
| 5364 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5365 | break; |
| 5366 | case '{': { |
| 5367 | int dummy; |
| 5368 | if (ParseIntervalQuantifier(&dummy, &dummy)) { |
| 5369 | ReportError(CStrVector("Nothing to repeat") CHECK_FAILED); |
| 5370 | } |
| 5371 | // fallthrough |
| 5372 | } |
| 5373 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5374 | builder->AddCharacter(current()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5375 | Advance(); |
| 5376 | break; |
| 5377 | } // end switch(current()) |
| 5378 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5379 | int min; |
| 5380 | int max; |
| 5381 | switch (current()) { |
| 5382 | // QuantifierPrefix :: |
| 5383 | // * |
| 5384 | // + |
| 5385 | // ? |
| 5386 | // { |
| 5387 | case '*': |
| 5388 | min = 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5389 | max = RegExpTree::kInfinity; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5390 | Advance(); |
| 5391 | break; |
| 5392 | case '+': |
| 5393 | min = 1; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5394 | max = RegExpTree::kInfinity; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5395 | Advance(); |
| 5396 | break; |
| 5397 | case '?': |
| 5398 | min = 0; |
| 5399 | max = 1; |
| 5400 | Advance(); |
| 5401 | break; |
| 5402 | case '{': |
| 5403 | if (ParseIntervalQuantifier(&min, &max)) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5404 | if (max < min) { |
| 5405 | ReportError(CStrVector("numbers out of order in {} quantifier.") |
| 5406 | CHECK_FAILED); |
| 5407 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5408 | break; |
| 5409 | } else { |
| 5410 | continue; |
| 5411 | } |
| 5412 | default: |
| 5413 | continue; |
| 5414 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5415 | RegExpQuantifier::Type type = RegExpQuantifier::GREEDY; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5416 | if (current() == '?') { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5417 | type = RegExpQuantifier::NON_GREEDY; |
| 5418 | Advance(); |
| 5419 | } else if (FLAG_regexp_possessive_quantifier && current() == '+') { |
| 5420 | // FLAG_regexp_possessive_quantifier is a debug-only flag. |
| 5421 | type = RegExpQuantifier::POSSESSIVE; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5422 | Advance(); |
| 5423 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5424 | builder->AddQuantifierToAtom(min, max, type); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5428 | |
| 5429 | #ifdef DEBUG |
| 5430 | // Currently only used in an ASSERT. |
| 5431 | static bool IsSpecialClassEscape(uc32 c) { |
| 5432 | switch (c) { |
| 5433 | case 'd': case 'D': |
| 5434 | case 's': case 'S': |
| 5435 | case 'w': case 'W': |
| 5436 | return true; |
| 5437 | default: |
| 5438 | return false; |
| 5439 | } |
| 5440 | } |
| 5441 | #endif |
| 5442 | |
| 5443 | |
| 5444 | // In order to know whether an escape is a backreference or not we have to scan |
| 5445 | // the entire regexp and find the number of capturing parentheses. However we |
| 5446 | // don't want to scan the regexp twice unless it is necessary. This mini-parser |
| 5447 | // is called when needed. It can see the difference between capturing and |
| 5448 | // noncapturing parentheses and can skip character classes and backslash-escaped |
| 5449 | // characters. |
| 5450 | void RegExpParser::ScanForCaptures() { |
| 5451 | // Start with captures started previous to current position |
| 5452 | int capture_count = captures_started(); |
| 5453 | // Add count of captures after this position. |
| 5454 | int n; |
| 5455 | while ((n = current()) != kEndMarker) { |
| 5456 | Advance(); |
| 5457 | switch (n) { |
| 5458 | case '\\': |
| 5459 | Advance(); |
| 5460 | break; |
| 5461 | case '[': { |
| 5462 | int c; |
| 5463 | while ((c = current()) != kEndMarker) { |
| 5464 | Advance(); |
| 5465 | if (c == '\\') { |
| 5466 | Advance(); |
| 5467 | } else { |
| 5468 | if (c == ']') break; |
| 5469 | } |
| 5470 | } |
| 5471 | break; |
| 5472 | } |
| 5473 | case '(': |
| 5474 | if (current() != '?') capture_count++; |
| 5475 | break; |
| 5476 | } |
| 5477 | } |
| 5478 | capture_count_ = capture_count; |
| 5479 | is_scanned_for_captures_ = true; |
| 5480 | } |
| 5481 | |
| 5482 | |
| 5483 | bool RegExpParser::ParseBackReferenceIndex(int* index_out) { |
| 5484 | ASSERT_EQ('\\', current()); |
| 5485 | ASSERT('1' <= Next() && Next() <= '9'); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5486 | // Try to parse a decimal literal that is no greater than the total number |
| 5487 | // of left capturing parentheses in the input. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5488 | int start = position(); |
| 5489 | int value = Next() - '0'; |
| 5490 | Advance(2); |
| 5491 | while (true) { |
| 5492 | uc32 c = current(); |
| 5493 | if (IsDecimalDigit(c)) { |
| 5494 | value = 10 * value + (c - '0'); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5495 | if (value > kMaxCaptures) { |
| 5496 | Reset(start); |
| 5497 | return false; |
| 5498 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5499 | Advance(); |
| 5500 | } else { |
| 5501 | break; |
| 5502 | } |
| 5503 | } |
| 5504 | if (value > captures_started()) { |
| 5505 | if (!is_scanned_for_captures_) { |
| 5506 | int saved_position = position(); |
| 5507 | ScanForCaptures(); |
| 5508 | Reset(saved_position); |
| 5509 | } |
| 5510 | if (value > capture_count_) { |
| 5511 | Reset(start); |
| 5512 | return false; |
| 5513 | } |
| 5514 | } |
| 5515 | *index_out = value; |
| 5516 | return true; |
| 5517 | } |
| 5518 | |
| 5519 | |
| 5520 | // QuantifierPrefix :: |
| 5521 | // { DecimalDigits } |
| 5522 | // { DecimalDigits , } |
| 5523 | // { DecimalDigits , DecimalDigits } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5524 | // |
| 5525 | // Returns true if parsing succeeds, and set the min_out and max_out |
| 5526 | // values. Values are truncated to RegExpTree::kInfinity if they overflow. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5527 | bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) { |
| 5528 | ASSERT_EQ(current(), '{'); |
| 5529 | int start = position(); |
| 5530 | Advance(); |
| 5531 | int min = 0; |
| 5532 | if (!IsDecimalDigit(current())) { |
| 5533 | Reset(start); |
| 5534 | return false; |
| 5535 | } |
| 5536 | while (IsDecimalDigit(current())) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5537 | int next = current() - '0'; |
| 5538 | if (min > (RegExpTree::kInfinity - next) / 10) { |
| 5539 | // Overflow. Skip past remaining decimal digits and return -1. |
| 5540 | do { |
| 5541 | Advance(); |
| 5542 | } while (IsDecimalDigit(current())); |
| 5543 | min = RegExpTree::kInfinity; |
| 5544 | break; |
| 5545 | } |
| 5546 | min = 10 * min + next; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5547 | Advance(); |
| 5548 | } |
| 5549 | int max = 0; |
| 5550 | if (current() == '}') { |
| 5551 | max = min; |
| 5552 | Advance(); |
| 5553 | } else if (current() == ',') { |
| 5554 | Advance(); |
| 5555 | if (current() == '}') { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5556 | max = RegExpTree::kInfinity; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5557 | Advance(); |
| 5558 | } else { |
| 5559 | while (IsDecimalDigit(current())) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5560 | int next = current() - '0'; |
| 5561 | if (max > (RegExpTree::kInfinity - next) / 10) { |
| 5562 | do { |
| 5563 | Advance(); |
| 5564 | } while (IsDecimalDigit(current())); |
| 5565 | max = RegExpTree::kInfinity; |
| 5566 | break; |
| 5567 | } |
| 5568 | max = 10 * max + next; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5569 | Advance(); |
| 5570 | } |
| 5571 | if (current() != '}') { |
| 5572 | Reset(start); |
| 5573 | return false; |
| 5574 | } |
| 5575 | Advance(); |
| 5576 | } |
| 5577 | } else { |
| 5578 | Reset(start); |
| 5579 | return false; |
| 5580 | } |
| 5581 | *min_out = min; |
| 5582 | *max_out = max; |
| 5583 | return true; |
| 5584 | } |
| 5585 | |
| 5586 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5587 | uc32 RegExpParser::ParseOctalLiteral() { |
| 5588 | ASSERT('0' <= current() && current() <= '7'); |
| 5589 | // For compatibility with some other browsers (not all), we parse |
| 5590 | // up to three octal digits with a value below 256. |
| 5591 | uc32 value = current() - '0'; |
| 5592 | Advance(); |
| 5593 | if ('0' <= current() && current() <= '7') { |
| 5594 | value = value * 8 + current() - '0'; |
| 5595 | Advance(); |
| 5596 | if (value < 32 && '0' <= current() && current() <= '7') { |
| 5597 | value = value * 8 + current() - '0'; |
| 5598 | Advance(); |
| 5599 | } |
| 5600 | } |
| 5601 | return value; |
| 5602 | } |
| 5603 | |
| 5604 | |
| 5605 | bool RegExpParser::ParseHexEscape(int length, uc32 *value) { |
| 5606 | int start = position(); |
| 5607 | uc32 val = 0; |
| 5608 | bool done = false; |
| 5609 | for (int i = 0; !done; i++) { |
| 5610 | uc32 c = current(); |
| 5611 | int d = HexValue(c); |
| 5612 | if (d < 0) { |
| 5613 | Reset(start); |
| 5614 | return false; |
| 5615 | } |
| 5616 | val = val * 16 + d; |
| 5617 | Advance(); |
| 5618 | if (i == length - 1) { |
| 5619 | done = true; |
| 5620 | } |
| 5621 | } |
| 5622 | *value = val; |
| 5623 | return true; |
| 5624 | } |
| 5625 | |
| 5626 | |
| 5627 | uc32 RegExpParser::ParseClassCharacterEscape() { |
| 5628 | ASSERT(current() == '\\'); |
| 5629 | ASSERT(has_next() && !IsSpecialClassEscape(Next())); |
| 5630 | Advance(); |
| 5631 | switch (current()) { |
| 5632 | case 'b': |
| 5633 | Advance(); |
| 5634 | return '\b'; |
| 5635 | // ControlEscape :: one of |
| 5636 | // f n r t v |
| 5637 | case 'f': |
| 5638 | Advance(); |
| 5639 | return '\f'; |
| 5640 | case 'n': |
| 5641 | Advance(); |
| 5642 | return '\n'; |
| 5643 | case 'r': |
| 5644 | Advance(); |
| 5645 | return '\r'; |
| 5646 | case 't': |
| 5647 | Advance(); |
| 5648 | return '\t'; |
| 5649 | case 'v': |
| 5650 | Advance(); |
| 5651 | return '\v'; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 5652 | case 'c': { |
| 5653 | uc32 controlLetter = Next(); |
| 5654 | uc32 letter = controlLetter & ~('A' ^ 'a'); |
| 5655 | // For compatibility with JSC, inside a character class |
| 5656 | // we also accept digits and underscore as control characters. |
| 5657 | if ((controlLetter >= '0' && controlLetter <= '9') || |
| 5658 | controlLetter == '_' || |
| 5659 | (letter >= 'A' && letter <= 'Z')) { |
| 5660 | Advance(2); |
| 5661 | // Control letters mapped to ASCII control characters in the range |
| 5662 | // 0x00-0x1f. |
| 5663 | return controlLetter & 0x1f; |
| 5664 | } |
| 5665 | // We match JSC in reading the backslash as a literal |
| 5666 | // character instead of as starting an escape. |
| 5667 | return '\\'; |
| 5668 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5669 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 5670 | case '6': case '7': |
| 5671 | // For compatibility, we interpret a decimal escape that isn't |
| 5672 | // a back reference (and therefore either \0 or not valid according |
| 5673 | // to the specification) as a 1..3 digit octal character code. |
| 5674 | return ParseOctalLiteral(); |
| 5675 | case 'x': { |
| 5676 | Advance(); |
| 5677 | uc32 value; |
| 5678 | if (ParseHexEscape(2, &value)) { |
| 5679 | return value; |
| 5680 | } |
| 5681 | // If \x is not followed by a two-digit hexadecimal, treat it |
| 5682 | // as an identity escape. |
| 5683 | return 'x'; |
| 5684 | } |
| 5685 | case 'u': { |
| 5686 | Advance(); |
| 5687 | uc32 value; |
| 5688 | if (ParseHexEscape(4, &value)) { |
| 5689 | return value; |
| 5690 | } |
| 5691 | // If \u is not followed by a four-digit hexadecimal, treat it |
| 5692 | // as an identity escape. |
| 5693 | return 'u'; |
| 5694 | } |
| 5695 | default: { |
| 5696 | // Extended identity escape. We accept any character that hasn't |
| 5697 | // been matched by a more specific case, not just the subset required |
| 5698 | // by the ECMAScript specification. |
| 5699 | uc32 result = current(); |
| 5700 | Advance(); |
| 5701 | return result; |
| 5702 | } |
| 5703 | } |
| 5704 | return 0; |
| 5705 | } |
| 5706 | |
| 5707 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5708 | CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) { |
| 5709 | ASSERT_EQ(0, *char_class); |
| 5710 | uc32 first = current(); |
| 5711 | if (first == '\\') { |
| 5712 | switch (Next()) { |
| 5713 | case 'w': case 'W': case 'd': case 'D': case 's': case 'S': { |
| 5714 | *char_class = Next(); |
| 5715 | Advance(2); |
| 5716 | return CharacterRange::Singleton(0); // Return dummy value. |
| 5717 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5718 | case kEndMarker: |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5719 | return ReportError(CStrVector("\\ at end of pattern")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5720 | default: |
| 5721 | uc32 c = ParseClassCharacterEscape(CHECK_FAILED); |
| 5722 | return CharacterRange::Singleton(c); |
| 5723 | } |
| 5724 | } else { |
| 5725 | Advance(); |
| 5726 | return CharacterRange::Singleton(first); |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5731 | static const uc16 kNoCharClass = 0; |
| 5732 | |
| 5733 | // Adds range or pre-defined character class to character ranges. |
| 5734 | // If char_class is not kInvalidClass, it's interpreted as a class |
| 5735 | // escape (i.e., 's' means whitespace, from '\s'). |
| 5736 | static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges, |
| 5737 | uc16 char_class, |
| 5738 | CharacterRange range) { |
| 5739 | if (char_class != kNoCharClass) { |
| 5740 | CharacterRange::AddClassEscape(char_class, ranges); |
| 5741 | } else { |
| 5742 | ranges->Add(range); |
| 5743 | } |
| 5744 | } |
| 5745 | |
| 5746 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5747 | RegExpTree* RegExpParser::ParseCharacterClass() { |
| 5748 | static const char* kUnterminated = "Unterminated character class"; |
| 5749 | static const char* kRangeOutOfOrder = "Range out of order in character class"; |
| 5750 | |
| 5751 | ASSERT_EQ(current(), '['); |
| 5752 | Advance(); |
| 5753 | bool is_negated = false; |
| 5754 | if (current() == '^') { |
| 5755 | is_negated = true; |
| 5756 | Advance(); |
| 5757 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5758 | ZoneList<CharacterRange>* ranges = new(zone()) ZoneList<CharacterRange>(2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5759 | while (has_more() && current() != ']') { |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5760 | uc16 char_class = kNoCharClass; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5761 | CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5762 | if (current() == '-') { |
| 5763 | Advance(); |
| 5764 | if (current() == kEndMarker) { |
| 5765 | // If we reach the end we break out of the loop and let the |
| 5766 | // following code report an error. |
| 5767 | break; |
| 5768 | } else if (current() == ']') { |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5769 | AddRangeOrEscape(ranges, char_class, first); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5770 | ranges->Add(CharacterRange::Singleton('-')); |
| 5771 | break; |
| 5772 | } |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5773 | uc16 char_class_2 = kNoCharClass; |
| 5774 | CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED); |
| 5775 | if (char_class != kNoCharClass || char_class_2 != kNoCharClass) { |
| 5776 | // Either end is an escaped character class. Treat the '-' verbatim. |
| 5777 | AddRangeOrEscape(ranges, char_class, first); |
| 5778 | ranges->Add(CharacterRange::Singleton('-')); |
| 5779 | AddRangeOrEscape(ranges, char_class_2, next); |
| 5780 | continue; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5781 | } |
| 5782 | if (first.from() > next.to()) { |
| 5783 | return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED); |
| 5784 | } |
| 5785 | ranges->Add(CharacterRange::Range(first.from(), next.to())); |
| 5786 | } else { |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5787 | AddRangeOrEscape(ranges, char_class, first); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5788 | } |
| 5789 | } |
| 5790 | if (!has_more()) { |
| 5791 | return ReportError(CStrVector(kUnterminated) CHECK_FAILED); |
| 5792 | } |
| 5793 | Advance(); |
| 5794 | if (ranges->length() == 0) { |
| 5795 | ranges->Add(CharacterRange::Everything()); |
| 5796 | is_negated = !is_negated; |
| 5797 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5798 | return new(zone()) RegExpCharacterClass(ranges, is_negated); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | |
| 5802 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5803 | // The Parser interface. |
| 5804 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5805 | ParserMessage::~ParserMessage() { |
| 5806 | for (int i = 0; i < args().length(); i++) |
| 5807 | DeleteArray(args()[i]); |
| 5808 | DeleteArray(args().start()); |
| 5809 | } |
| 5810 | |
| 5811 | |
| 5812 | ScriptDataImpl::~ScriptDataImpl() { |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 5813 | if (owns_store_) store_.Dispose(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | |
| 5817 | int ScriptDataImpl::Length() { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5818 | return store_.length() * sizeof(unsigned); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5819 | } |
| 5820 | |
| 5821 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5822 | const char* ScriptDataImpl::Data() { |
| 5823 | return reinterpret_cast<const char*>(store_.start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5827 | bool ScriptDataImpl::HasError() { |
| 5828 | return has_error(); |
| 5829 | } |
| 5830 | |
| 5831 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 5832 | void ScriptDataImpl::Initialize() { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 5833 | // Prepares state for use. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5834 | if (store_.length() >= PreparseDataConstants::kHeaderSize) { |
| 5835 | function_index_ = PreparseDataConstants::kHeaderSize; |
| 5836 | int symbol_data_offset = PreparseDataConstants::kHeaderSize |
| 5837 | + store_[PreparseDataConstants::kFunctionsSizeOffset]; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 5838 | if (store_.length() > symbol_data_offset) { |
| 5839 | symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]); |
| 5840 | } else { |
| 5841 | // Partial preparse causes no symbol information. |
| 5842 | symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length()); |
| 5843 | } |
| 5844 | symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length()); |
| 5845 | } |
| 5846 | } |
| 5847 | |
| 5848 | |
| 5849 | int ScriptDataImpl::ReadNumber(byte** source) { |
| 5850 | // Reads a number from symbol_data_ in base 128. The most significant |
| 5851 | // bit marks that there are more digits. |
| 5852 | // If the first byte is 0x80 (kNumberTerminator), it would normally |
| 5853 | // represent a leading zero. Since that is useless, and therefore won't |
| 5854 | // appear as the first digit of any actual value, it is used to |
| 5855 | // mark the end of the input stream. |
| 5856 | byte* data = *source; |
| 5857 | if (data >= symbol_data_end_) return -1; |
| 5858 | byte input = *data; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5859 | if (input == PreparseDataConstants::kNumberTerminator) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 5860 | // End of stream marker. |
| 5861 | return -1; |
| 5862 | } |
| 5863 | int result = input & 0x7f; |
| 5864 | data++; |
| 5865 | while ((input & 0x80u) != 0) { |
| 5866 | if (data >= symbol_data_end_) return -1; |
| 5867 | input = *data; |
| 5868 | result = (result << 7) | (input & 0x7f); |
| 5869 | data++; |
| 5870 | } |
| 5871 | *source = data; |
| 5872 | return result; |
| 5873 | } |
| 5874 | |
| 5875 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 5876 | // Create a Scanner for the preparser to use as input, and preparse the source. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5877 | static ScriptDataImpl* DoPreParse(UC16CharacterStream* source, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5878 | int flags, |
| 5879 | ParserRecorder* recorder) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5880 | Isolate* isolate = Isolate::Current(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5881 | HistogramTimerScope timer(isolate->counters()->pre_parse()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5882 | Scanner scanner(isolate->unicode_cache()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5883 | scanner.SetHarmonyScoping(FLAG_harmony_scoping); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 5884 | scanner.Initialize(source); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5885 | intptr_t stack_limit = isolate->stack_guard()->real_climit(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5886 | preparser::PreParser::PreParseResult result = |
| 5887 | preparser::PreParser::PreParseProgram(&scanner, |
| 5888 | recorder, |
| 5889 | flags, |
| 5890 | stack_limit); |
| 5891 | if (result == preparser::PreParser::kPreParseStackOverflow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5892 | isolate->StackOverflow(); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 5893 | return NULL; |
| 5894 | } |
| 5895 | |
| 5896 | // Extract the accumulated data from the recorder as a single |
| 5897 | // contiguous vector that we are responsible for disposing. |
| 5898 | Vector<unsigned> store = recorder->ExtractData(); |
| 5899 | return new ScriptDataImpl(store); |
| 5900 | } |
| 5901 | |
| 5902 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 5903 | // Preparse, but only collect data that is immediately useful, |
| 5904 | // even if the preparser data is only used once. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5905 | ScriptDataImpl* ParserApi::PartialPreParse(Handle<String> source, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 5906 | v8::Extension* extension, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5907 | int flags) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 5908 | bool allow_lazy = FLAG_lazy && (extension == NULL); |
| 5909 | if (!allow_lazy) { |
| 5910 | // Partial preparsing is only about lazily compiled functions. |
| 5911 | // If we don't allow lazy compilation, the log data will be empty. |
| 5912 | return NULL; |
| 5913 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5914 | flags |= kAllowLazy; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 5915 | PartialParserRecorder recorder; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5916 | int source_length = source->length(); |
| 5917 | if (source->IsExternalTwoByteString()) { |
| 5918 | ExternalTwoByteStringUC16CharacterStream stream( |
| 5919 | Handle<ExternalTwoByteString>::cast(source), 0, source_length); |
| 5920 | return DoPreParse(&stream, flags, &recorder); |
| 5921 | } else { |
| 5922 | GenericStringUC16CharacterStream stream(source, 0, source_length); |
| 5923 | return DoPreParse(&stream, flags, &recorder); |
| 5924 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5928 | ScriptDataImpl* ParserApi::PreParse(UC16CharacterStream* source, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 5929 | v8::Extension* extension, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5930 | int flags) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5931 | Handle<Script> no_script; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5932 | if (FLAG_lazy && (extension == NULL)) { |
| 5933 | flags |= kAllowLazy; |
| 5934 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 5935 | CompleteParserRecorder recorder; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5936 | return DoPreParse(source, flags, &recorder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
| 5939 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 5940 | bool RegExpParser::ParseRegExp(FlatStringReader* input, |
| 5941 | bool multiline, |
| 5942 | RegExpCompileData* result) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5943 | ASSERT(result != NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5944 | RegExpParser parser(input, &result->error, multiline); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5945 | RegExpTree* tree = parser.ParsePattern(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5946 | if (parser.failed()) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5947 | ASSERT(tree == NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5948 | ASSERT(!result->error.is_null()); |
| 5949 | } else { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5950 | ASSERT(tree != NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5951 | ASSERT(result->error.is_null()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5952 | result->tree = tree; |
| 5953 | int capture_count = parser.captures_started(); |
| 5954 | result->simple = tree->IsAtom() && parser.simple() && capture_count == 0; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5955 | result->contains_anchor = parser.contains_anchor(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5956 | result->capture_count = capture_count; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5957 | } |
| 5958 | return !parser.failed(); |
| 5959 | } |
| 5960 | |
| 5961 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5962 | bool ParserApi::Parse(CompilationInfo* info, int parsing_flags) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5963 | ASSERT(info->function() == NULL); |
| 5964 | FunctionLiteral* result = NULL; |
| 5965 | Handle<Script> script = info->script(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5966 | ASSERT((parsing_flags & kLanguageModeMask) == CLASSIC_MODE); |
| 5967 | if (!info->is_native() && FLAG_harmony_scoping) { |
| 5968 | // Harmony scoping is requested. |
| 5969 | parsing_flags |= EXTENDED_MODE; |
| 5970 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 5971 | if (!info->is_native() && FLAG_harmony_modules) { |
| 5972 | parsing_flags |= kAllowModules; |
| 5973 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5974 | if (FLAG_allow_natives_syntax || info->is_native()) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 5975 | // We require %identifier(..) syntax. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5976 | parsing_flags |= kAllowNativesSyntax; |
| 5977 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5978 | if (info->is_lazy()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 5979 | ASSERT(!info->is_eval()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5980 | Parser parser(script, parsing_flags, NULL, NULL); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 5981 | if (info->shared_info()->is_function()) { |
| 5982 | result = parser.ParseLazy(info); |
| 5983 | } else { |
| 5984 | result = parser.ParseProgram(info); |
| 5985 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5986 | } else { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5987 | ScriptDataImpl* pre_data = info->pre_parse_data(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5988 | Parser parser(script, parsing_flags, info->extension(), pre_data); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5989 | if (pre_data != NULL && pre_data->has_error()) { |
| 5990 | Scanner::Location loc = pre_data->MessageLocation(); |
| 5991 | const char* message = pre_data->BuildMessage(); |
| 5992 | Vector<const char*> args = pre_data->BuildArgs(); |
| 5993 | parser.ReportMessageAt(loc, message, args); |
| 5994 | DeleteArray(message); |
| 5995 | for (int i = 0; i < args.length(); i++) { |
| 5996 | DeleteArray(args[i]); |
| 5997 | } |
| 5998 | DeleteArray(args.start()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5999 | ASSERT(info->isolate()->has_pending_exception()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 6000 | } else { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 6001 | result = parser.ParseProgram(info); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 6002 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6003 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 6004 | info->SetFunction(result); |
| 6005 | return (result != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6006 | } |
| 6007 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6008 | } } // namespace v8::internal |