| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/operators.h" | 5 #include "tools/gn/operators.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> |
| 8 | 9 |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "tools/gn/err.h" | 11 #include "tools/gn/err.h" |
| 11 #include "tools/gn/parse_tree.h" | 12 #include "tools/gn/parse_tree.h" |
| 12 #include "tools/gn/scope.h" | 13 #include "tools/gn/scope.h" |
| 13 #include "tools/gn/token.h" | 14 #include "tools/gn/token.h" |
| 14 #include "tools/gn/value.h" | 15 #include "tools/gn/value.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const char kSourcesName[] = "sources"; | 19 const char kSourcesName[] = "sources"; |
| 19 | 20 |
| 20 // Applies the sources assignment filter from the given scope to each element | 21 // Helper class used for assignment operations: =, +=, and -= to generalize |
| 21 // of source (can be a list or a string), appending it to dest if it doesn't | 22 // writing to various types of destinations. |
| 22 // match. | 23 class ValueDestination { |
| 23 void AppendFilteredSourcesToValue(const Scope* scope, | 24 public: |
| 24 const Value& source, | 25 ValueDestination(); |
| 25 Value* dest) { | 26 |
| 26 const PatternList* filter = scope->GetSourcesAssignmentFilter(); | 27 bool Init(Scope* exec_scope, |
| 27 | 28 const ParseNode* dest, |
| 28 if (source.type() == Value::STRING) { | 29 const BinaryOpNode* op_node, |
| 29 if (!filter || filter->is_empty() || | 30 Err* err); |
| 30 !filter->MatchesValue(source)) | 31 |
| 31 dest->list_value().push_back(source); | 32 // Returns the value in the destination scope if it already exists, or null |
| 32 return; | 33 // if it doesn't. This is for validation and does not count as a "use". |
| 33 } | 34 // Other nested scopes will be searched. |
| 34 if (source.type() != Value::LIST) { | 35 const Value* GetExistingValue() const; |
| 35 // Any non-list and non-string being added to a list can just get appended, | 36 |
| 36 // we're not going to filter it. | 37 // Returns an existing version of the output if it can be modified. This will |
| 37 dest->list_value().push_back(source); | 38 // not search nested scopes since writes only go into the current scope. |
| 38 return; | 39 // Returns null if the value does not exist, or is not in the current scope |
| 39 } | 40 // (meaning assignments won't go to this value and it's not mutable). This |
| 40 | 41 // is for implementing += and -=. |
| 41 if (!filter || filter->is_empty()) { | 42 // |
| 42 // No filter, append everything. | 43 // If it exists, this will mark the origin of the value to be the passed-in |
| 43 for (const auto& source_entry : source.list_value()) | 44 // node, and the value will be also marked unused (if possible) under the |
| 44 dest->list_value().push_back(source_entry); | 45 // assumption that it will be modified in-place. |
| 45 return; | 46 Value* GetExistingMutableValueIfExists(const ParseNode* origin); |
| 46 } | 47 |
| 47 | 48 // Returns the sources assignment filter if it exists for the current |
| 48 // Note: don't reserve() the dest vector here since that actually hurts | 49 // scope and it should be applied to this assignment. Otherwise returns null. |
| 49 // the allocation pattern when the build script is doing multiple small | 50 const PatternList* GetAssignmentFilter(const Scope* exec_scope) const; |
| 50 // additions. | 51 |
| 51 for (const auto& source_entry : source.list_value()) { | 52 // Returns a pointer to the value that was set. |
| 52 if (!filter->MatchesValue(source_entry)) | 53 Value* SetValue(Value value, const ParseNode* set_node); |
| 53 dest->list_value().push_back(source_entry); | 54 |
| 54 } | 55 // Fills the Err with an undefined value error appropriate for modification |
| 56 // operators: += and -= (where the source is also the dest). |
| 57 void MakeUndefinedIdentifierForModifyError(Err* err); |
| 58 |
| 59 private: |
| 60 enum Type { UNINITIALIZED, SCOPE, LIST }; |
| 61 |
| 62 Type type_; |
| 63 |
| 64 // Valid when type_ == SCOPE. |
| 65 Scope* scope_; |
| 66 const Token* name_token_; |
| 67 |
| 68 // Valid when type_ == LIST. |
| 69 Value* list_; |
| 70 size_t index_; // Guaranteed in-range when Init() succeeds. |
| 71 }; |
| 72 |
| 73 ValueDestination::ValueDestination() |
| 74 : type_(UNINITIALIZED), |
| 75 scope_(nullptr), |
| 76 name_token_(nullptr), |
| 77 list_(nullptr), |
| 78 index_(0) { |
| 79 } |
| 80 |
| 81 bool ValueDestination::Init(Scope* exec_scope, |
| 82 const ParseNode* dest, |
| 83 const BinaryOpNode* op_node, |
| 84 Err* err) { |
| 85 // Check for standard variable set. |
| 86 const IdentifierNode* dest_identifier = dest->AsIdentifier(); |
| 87 if (dest_identifier) { |
| 88 type_ = SCOPE; |
| 89 scope_ = exec_scope; |
| 90 name_token_ = &dest_identifier->value(); |
| 91 return true; |
| 92 } |
| 93 |
| 94 // Check for array and scope accesses. The base (array or scope variable |
| 95 // name) must always be defined ahead of time. |
| 96 const AccessorNode* dest_accessor = dest->AsAccessor(); |
| 97 if (!dest_accessor) { |
| 98 *err = Err(op_node, "Assignment requires a lvalue.", |
| 99 "This thing on the left is not an identifier or accessor."); |
| 100 err->AppendRange(dest->GetRange()); |
| 101 return false; |
| 102 } |
| 103 |
| 104 // Known to be an accessor. |
| 105 base::StringPiece base_str = dest_accessor->base().value(); |
| 106 Value* base = exec_scope->GetMutableValue( |
| 107 base_str, Scope::SEARCH_CURRENT, false); |
| 108 if (!base) { |
| 109 // Base is either undefined or it's defined but not in the current scope. |
| 110 // Make a good error message. |
| 111 if (exec_scope->GetValue(base_str, false)) { |
| 112 *err = Err(dest_accessor->base(), "Suspicious in-place modification.", |
| 113 "This variable exists in a containing scope. Normally, writing to it " |
| 114 "would\nmake a copy of it into the current scope with the modified " |
| 115 "version. But\nhere you're modifying only an element of a scope or " |
| 116 "list object. It's unlikely\nyou meant to copy the entire thing just " |
| 117 "to modify this part of it.\n" |
| 118 "\n" |
| 119 "If you really wanted to do this, do:\n" |
| 120 " " + base_str.as_string() + " = " + base_str.as_string() + "\n" |
| 121 "to copy it into the current scope before doing this operation."); |
| 122 } else { |
| 123 *err = Err(dest_accessor->base(), "Undefined identifier."); |
| 124 } |
| 125 return false; |
| 126 } |
| 127 |
| 128 if (dest_accessor->index()) { |
| 129 // List access with an index. |
| 130 if (!base->VerifyTypeIs(Value::LIST, err)) { |
| 131 // Errors here will confusingly refer to the variable declaration (since |
| 132 // that's all Value knows) rather than the list access. So rewrite the |
| 133 // error location to refer to the base value's location. |
| 134 *err = Err(dest_accessor->base(), err->message(), err->help_text()); |
| 135 return false; |
| 136 } |
| 137 |
| 138 type_ = LIST; |
| 139 list_ = base; |
| 140 return dest_accessor->ComputeAndValidateListIndex( |
| 141 exec_scope, base->list_value().size(), &index_, err); |
| 142 } |
| 143 |
| 144 // Scope access with a dot. |
| 145 if (!base->VerifyTypeIs(Value::SCOPE, err)) { |
| 146 // As the for the list index case above, rewrite the error location. |
| 147 *err = Err(dest_accessor->base(), err->message(), err->help_text()); |
| 148 return false; |
| 149 } |
| 150 type_ = SCOPE; |
| 151 scope_ = base->scope_value(); |
| 152 name_token_ = &dest_accessor->member()->value(); |
| 153 return true; |
| 154 } |
| 155 |
| 156 const Value* ValueDestination::GetExistingValue() const { |
| 157 if (type_ == SCOPE) |
| 158 return scope_->GetValue(name_token_->value(), false); |
| 159 else if (type_ == LIST) |
| 160 return &list_->list_value()[index_]; |
| 161 return nullptr; |
| 162 } |
| 163 |
| 164 Value* ValueDestination::GetExistingMutableValueIfExists( |
| 165 const ParseNode* origin) { |
| 166 if (type_ == SCOPE) { |
| 167 Value* value = scope_->GetMutableValue( |
| 168 name_token_->value(), Scope::SEARCH_CURRENT, false); |
| 169 if (value) { |
| 170 // The value will be written to, reset its tracking information. |
| 171 value->set_origin(origin); |
| 172 scope_->MarkUnused(name_token_->value()); |
| 173 } |
| 174 } |
| 175 if (type_ == LIST) |
| 176 return &list_->list_value()[index_]; |
| 177 return nullptr; |
| 178 } |
| 179 |
| 180 const PatternList* ValueDestination::GetAssignmentFilter( |
| 181 const Scope* exec_scope) const { |
| 182 if (type_ != SCOPE) |
| 183 return nullptr; // Destination can't be named, so no sources filtering. |
| 184 if (name_token_->value() != kSourcesName) |
| 185 return nullptr; // Destination not named "sources". |
| 186 |
| 187 const PatternList* filter = exec_scope->GetSourcesAssignmentFilter(); |
| 188 if (!filter || filter->is_empty()) |
| 189 return nullptr; // No filter or empty filter, don't need to do anything. |
| 190 return filter; |
| 191 } |
| 192 |
| 193 Value* ValueDestination::SetValue(Value value, const ParseNode* set_node) { |
| 194 if (type_ == SCOPE) { |
| 195 return scope_->SetValue(name_token_->value(), std::move(value), set_node); |
| 196 } else if (type_ == LIST) { |
| 197 Value* dest = &list_->list_value()[index_]; |
| 198 *dest = std::move(value); |
| 199 return dest; |
| 200 } |
| 201 return nullptr; |
| 202 } |
| 203 |
| 204 void ValueDestination::MakeUndefinedIdentifierForModifyError(Err* err) { |
| 205 // When Init() succeeds, the base of any accessor has already been resolved |
| 206 // and that list indices are in-range. This means any undefined identifiers |
| 207 // are for scope accesses. |
| 208 DCHECK(type_ == SCOPE); |
| 209 *err = Err(*name_token_, "Undefined identifier."); |
| 210 } |
| 211 |
| 212 // ----------------------------------------------------------------------------- |
| 213 |
| 214 Err MakeIncompatibleTypeError(const BinaryOpNode* op_node, |
| 215 const Value& left, |
| 216 const Value& right) { |
| 217 std::string msg = |
| 218 std::string("You can't do <") + Value::DescribeType(left.type()) + "> " + |
| 219 op_node->op().value().as_string() + |
| 220 " <" + Value::DescribeType(right.type()) + ">."; |
| 221 if (left.type() == Value::LIST) { |
| 222 // Append extra hint for list stuff. |
| 223 msg += "\n\nHint: If you're attempting to add or remove a single item from " |
| 224 " a list, use \"foo + [ bar ]\"."; |
| 225 } |
| 226 return Err(op_node, "Incompatible types for binary operator.", msg); |
| 55 } | 227 } |
| 56 | 228 |
| 57 Value GetValueOrFillError(const BinaryOpNode* op_node, | 229 Value GetValueOrFillError(const BinaryOpNode* op_node, |
| 58 const ParseNode* node, | 230 const ParseNode* node, |
| 59 const char* name, | 231 const char* name, |
| 60 Scope* scope, | 232 Scope* scope, |
| 61 Err* err) { | 233 Err* err) { |
| 62 Value value = node->Execute(scope, err); | 234 Value value = node->Execute(scope, err); |
| 63 if (err->has_error()) | 235 if (err->has_error()) |
| 64 return Value(); | 236 return Value(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 283 |
| 112 default: | 284 default: |
| 113 break; | 285 break; |
| 114 } | 286 } |
| 115 } | 287 } |
| 116 | 288 |
| 117 // Assignment ----------------------------------------------------------------- | 289 // Assignment ----------------------------------------------------------------- |
| 118 | 290 |
| 119 // We return a null value from this rather than the result of doing the append. | 291 // We return a null value from this rather than the result of doing the append. |
| 120 // See ValuePlusEquals for rationale. | 292 // See ValuePlusEquals for rationale. |
| 121 Value ExecuteEquals(Scope* scope, | 293 Value ExecuteEquals(Scope* exec_scope, |
| 122 const BinaryOpNode* op_node, | 294 const BinaryOpNode* op_node, |
| 123 const Token& left, | 295 ValueDestination* dest, |
| 124 const Value& right, | 296 Value right, |
| 125 Err* err) { | 297 Err* err) { |
| 126 const Value* old_value = scope->GetValue(left.value(), false); | 298 const Value* old_value = dest->GetExistingValue(); |
| 127 if (old_value) { | 299 if (old_value) { |
| 128 // Throw an error when overwriting a nonempty list with another nonempty | 300 // Throw an error when overwriting a nonempty list with another nonempty |
| 129 // list item. This is to detect the case where you write | 301 // list item. This is to detect the case where you write |
| 130 // defines = ["FOO"] | 302 // defines = ["FOO"] |
| 131 // and you overwrote inherited ones, when instead you mean to append: | 303 // and you overwrote inherited ones, when instead you mean to append: |
| 132 // defines += ["FOO"] | 304 // defines += ["FOO"] |
| 133 if (old_value->type() == Value::LIST && | 305 if (old_value->type() == Value::LIST && |
| 134 !old_value->list_value().empty() && | 306 !old_value->list_value().empty() && |
| 135 right.type() == Value::LIST && | 307 right.type() == Value::LIST && |
| 136 !right.list_value().empty()) { | 308 !right.list_value().empty()) { |
| 137 *err = Err(op_node->left()->GetRange(), "Replacing nonempty list.", | 309 *err = Err(op_node->left()->GetRange(), "Replacing nonempty list.", |
| 138 std::string("This overwrites a previously-defined nonempty list ") + | 310 std::string("This overwrites a previously-defined nonempty list ") + |
| 139 "(length " + | 311 "(length " + |
| 140 base::IntToString(static_cast<int>(old_value->list_value().size())) | 312 base::IntToString(static_cast<int>(old_value->list_value().size())) |
| 141 + ")."); | 313 + ")."); |
| 142 err->AppendSubErr(Err(*old_value, "for previous definition", | 314 err->AppendSubErr(Err(*old_value, "for previous definition", |
| 143 "with another one (length " + | 315 "with another one (length " + |
| 144 base::IntToString(static_cast<int>(right.list_value().size())) + | 316 base::IntToString(static_cast<int>(right.list_value().size())) + |
| 145 "). Did you mean " + | 317 "). Did you mean " + |
| 146 "\"+=\" to append instead? If you\nreally want to do this, do\n " + | 318 "\"+=\" to append instead? If you\nreally want to do this, do\n" |
| 147 left.value().as_string() + " = []\nbefore reassigning.")); | 319 " foo = []\nbefore reassigning.")); |
| 148 return Value(); | 320 return Value(); |
| 149 } | 321 } |
| 150 } | 322 } |
| 151 if (err->has_error()) | 323 if (err->has_error()) |
| 152 return Value(); | 324 return Value(); |
| 153 | 325 |
| 154 if (right.type() == Value::LIST && left.value() == kSourcesName) { | 326 Value* written_value = dest->SetValue(std::move(right), op_node->right()); |
| 155 // Assigning to sources, filter the list. Here we do the filtering and | 327 |
| 156 // copying in one step to save an extra list copy (the lists may be | 328 // Optionally apply the assignment filter in-place. |
| 157 // long). | 329 const PatternList* filter = dest->GetAssignmentFilter(exec_scope); |
| 158 Value* set_value = scope->SetValue(left.value(), | 330 if (filter) { |
| 159 Value(op_node, Value::LIST), op_node); | 331 std::vector<Value>& list_value = written_value->list_value(); |
| 160 set_value->list_value().reserve(right.list_value().size()); | 332 auto first_deleted = std::remove_if( |
| 161 AppendFilteredSourcesToValue(scope, right, set_value); | 333 list_value.begin(), list_value.end(), |
| 162 } else { | 334 [filter](const Value& v) { |
| 163 // Normal value set, just copy it. | 335 return filter->MatchesValue(v); |
| 164 scope->SetValue(left.value(), right, op_node->right()); | 336 }); |
| 337 list_value.erase(first_deleted, list_value.end()); |
| 165 } | 338 } |
| 166 return Value(); | 339 return Value(); |
| 167 } | 340 } |
| 168 | 341 |
| 169 // allow_type_conversion indicates if we're allowed to change the type of the | 342 // Plus/minus ------------------------------------------------------------------ |
| 170 // left value. This is set to true when doing +, and false when doing +=. | 343 |
| 171 // | 344 // allow_left_type_conversion indicates if we're allowed to change the type of |
| 172 // Note that we return Value() from here, which is different than C++. This | 345 // the left value. This is set to true when doing +, and false when doing +=. |
| 173 // means you can't do clever things like foo = [ bar += baz ] to simultaneously | 346 Value ExecutePlus(const BinaryOpNode* op_node, |
| 174 // append to and use a value. This is basically never needed in out build | 347 Value left, |
| 175 // scripts and is just as likely an error as the intended behavior, and it also | 348 Value right, |
| 176 // involves a copy of the value when it's returned. Many times we're appending | 349 bool allow_left_type_conversion, |
| 177 // to large lists, and copying the value to discard it for the next statement | 350 Err* err) { |
| 178 // is very wasteful. | 351 // Left-hand-side integer. |
| 179 void ValuePlusEquals(const Scope* scope, | 352 if (left.type() == Value::INTEGER) { |
| 180 const BinaryOpNode* op_node, | 353 if (right.type() == Value::INTEGER) { |
| 181 const Token& left_token, | 354 // Int + int -> addition. |
| 182 Value* left, | 355 return Value(op_node, left.int_value() + right.int_value()); |
| 183 const Value& right, | 356 } else if (right.type() == Value::STRING && allow_left_type_conversion) { |
| 184 bool allow_type_conversion, | 357 // Int + string -> string concat. |
| 185 Err* err) { | 358 return Value( |
| 186 switch (left->type()) { | 359 op_node, |
| 187 // Left-hand-side int. | 360 base::Int64ToString(left.int_value()) + right.string_value()); |
| 188 case Value::INTEGER: | 361 } |
| 189 switch (right.type()) { | 362 *err = MakeIncompatibleTypeError(op_node, left, right); |
| 190 case Value::INTEGER: // int + int -> addition. | 363 return Value(); |
| 191 left->int_value() += right.int_value(); | 364 } |
| 192 return; | 365 |
| 193 | 366 // Left-hand-side string. |
| 194 case Value::STRING: // int + string -> string concat. | 367 if (left.type() == Value::STRING) { |
| 195 if (allow_type_conversion) { | 368 if (right.type() == Value::INTEGER) { |
| 196 *left = Value(op_node, | 369 // String + int -> string concat. |
| 197 base::Int64ToString(left->int_value()) + right.string_value()); | 370 return Value(op_node, |
| 198 return; | 371 left.string_value() + base::Int64ToString(right.int_value())); |
| 199 } | 372 } else if (right.type() == Value::STRING) { |
| 200 break; | 373 // String + string -> string concat. Since the left is passed by copy |
| 201 | 374 // we can avoid realloc if there is enough buffer by appending to left |
| 202 default: | 375 // and assigning. |
| 203 break; | 376 left.string_value().append(right.string_value()); |
| 377 return left; // FIXME(brettw) des this copy? |
| 378 } |
| 379 *err = MakeIncompatibleTypeError(op_node, left, right); |
| 380 return Value(); |
| 381 } |
| 382 |
| 383 // Left-hand-side list. The only valid thing is to add another list. |
| 384 if (left.type() == Value::LIST && right.type() == Value::LIST) { |
| 385 // Since left was passed by copy, avoid realloc by destructively appending |
| 386 // to it and using that as the result. |
| 387 for (Value& value : right.list_value()) |
| 388 left.list_value().push_back(std::move(value)); |
| 389 return left; // FIXME(brettw) does this copy? |
| 390 } |
| 391 |
| 392 *err = MakeIncompatibleTypeError(op_node, left, right); |
| 393 return Value(); |
| 394 } |
| 395 |
| 396 // Left is passed by value because it will be modified in-place and returned |
| 397 // for the list case. |
| 398 Value ExecuteMinus(const BinaryOpNode* op_node, |
| 399 Value left, |
| 400 const Value& right, |
| 401 Err* err) { |
| 402 // Left-hand-side int. The only thing to do is subtract another int. |
| 403 if (left.type() == Value::INTEGER && right.type() != Value::INTEGER) { |
| 404 // Int - int -> subtraction. |
| 405 return Value(op_node, left.int_value() - right.int_value()); |
| 406 } |
| 407 |
| 408 // Left-hand-side list. The only thing to do is subtract another list. |
| 409 if (left.type() == Value::LIST && right.type() == Value::LIST) { |
| 410 // In-place modify left and return it. |
| 411 RemoveMatchesFromList(op_node, &left, right, err); |
| 412 return left; |
| 413 } |
| 414 |
| 415 *err = MakeIncompatibleTypeError(op_node, left, right); |
| 416 return Value(); |
| 417 } |
| 418 |
| 419 // In-place plus/minus --------------------------------------------------------- |
| 420 |
| 421 void ExecutePlusEquals(Scope* exec_scope, |
| 422 const BinaryOpNode* op_node, |
| 423 ValueDestination* dest, |
| 424 Value right, |
| 425 Err* err) { |
| 426 // There are several cases. Some things we can convert "foo += bar" to |
| 427 // "foo = foo + bar". Some cases we can't (the 'sources' variable won't |
| 428 // get the right filtering on the list). Some cases we don't want to (lists |
| 429 // and strings will get unnecessary copying so we can to optimize these). |
| 430 // |
| 431 // - Value is already mutable in the current scope: |
| 432 // 1. List/string append: use it. |
| 433 // 2. Other types: fall back to "foo = foo + bar" |
| 434 // |
| 435 // - Value is not mutable in the current scope: |
| 436 // 3. List/string append: copy into current scope and append to that. |
| 437 // 4. Other types: fall back to "foo = foo + bar" |
| 438 // |
| 439 // The common case is to use += for list and string appends in the local |
| 440 // scope, so this is written to avoid multiple variable lookups in that case. |
| 441 Value* mutable_dest = dest->GetExistingMutableValueIfExists(op_node); |
| 442 if (!mutable_dest) { |
| 443 const Value* existing_value = dest->GetExistingValue(); |
| 444 if (!existing_value) { |
| 445 // Undefined left-hand-size for +=. |
| 446 dest->MakeUndefinedIdentifierForModifyError(err); |
| 447 return; |
| 448 } |
| 449 |
| 450 if (existing_value->type() != Value::STRING && |
| 451 existing_value->type() != Value::LIST) { |
| 452 // Case #4 above. |
| 453 dest->SetValue(ExecutePlus(op_node, *existing_value, |
| 454 std::move(right), false, err), op_node); |
| 455 return; |
| 456 } |
| 457 |
| 458 // Case #3 above, copy to current scope and fall-through to appending. |
| 459 mutable_dest = dest->SetValue(*existing_value, op_node); |
| 460 } else if (mutable_dest->type() != Value::STRING && |
| 461 mutable_dest->type() != Value::LIST) { |
| 462 // Case #2 above. |
| 463 dest->SetValue(ExecutePlus(op_node, *mutable_dest, |
| 464 std::move(right), false, err), op_node); |
| 465 return; |
| 466 } // "else" is case #1 above. |
| 467 |
| 468 if (mutable_dest->type() == Value::STRING) { |
| 469 if (right.type() == Value::INTEGER) { |
| 470 // String + int -> string concat. |
| 471 mutable_dest->string_value().append( |
| 472 base::Int64ToString(right.int_value())); |
| 473 } else if (right.type() == Value::STRING) { |
| 474 // String + string -> string concat. |
| 475 mutable_dest->string_value().append(right.string_value()); |
| 476 } else { |
| 477 *err = MakeIncompatibleTypeError(op_node, *mutable_dest, right); |
| 478 } |
| 479 } else if (mutable_dest->type() == Value::LIST) { |
| 480 // List concat. |
| 481 if (right.type() == Value::LIST) { |
| 482 // Note: don't reserve() the dest vector here since that actually hurts |
| 483 // the allocation pattern when the build script is doing multiple small |
| 484 // additions. |
| 485 const PatternList* filter = dest->GetAssignmentFilter(exec_scope); |
| 486 if (filter) { |
| 487 // Filtered list concat. |
| 488 for (Value& value : right.list_value()) { |
| 489 if (!filter->MatchesValue(value)) |
| 490 mutable_dest->list_value().push_back(std::move(value)); |
| 491 } |
| 492 } else { |
| 493 // Normal list concat. This is a destructive move. |
| 494 for (Value& value : right.list_value()) |
| 495 mutable_dest->list_value().push_back(std::move(value)); |
| 204 } | 496 } |
| 205 break; | 497 } else { |
| 206 | 498 *err = Err(op_node->op(), "Incompatible types to add.", |
| 207 // Left-hand-side string. | 499 "To append a single item to a list do \"foo += [ bar ]\"."); |
| 208 case Value::STRING: | 500 } |
| 209 switch (right.type()) { | 501 } |
| 210 case Value::INTEGER: // string + int -> string concat. | 502 } |
| 211 left->string_value().append(base::Int64ToString(right.int_value())); | 503 |
| 212 return; | 504 void ExecuteMinusEquals(const BinaryOpNode* op_node, |
| 213 | 505 ValueDestination* dest, |
| 214 case Value::STRING: // string + string -> string contat. | |
| 215 left->string_value().append(right.string_value()); | |
| 216 return; | |
| 217 | |
| 218 default: | |
| 219 break; | |
| 220 } | |
| 221 break; | |
| 222 | |
| 223 // Left-hand-side list. | |
| 224 case Value::LIST: | |
| 225 switch (right.type()) { | |
| 226 case Value::LIST: // list + list -> list concat. | |
| 227 if (left_token.value() == kSourcesName) { | |
| 228 // Filter additions through the assignment filter. | |
| 229 AppendFilteredSourcesToValue(scope, right, left); | |
| 230 } else { | |
| 231 // Normal list concat. | |
| 232 for (const auto& value : right.list_value()) | |
| 233 left->list_value().push_back(value); | |
| 234 } | |
| 235 return; | |
| 236 | |
| 237 default: | |
| 238 *err = Err(op_node->op(), "Incompatible types to add.", | |
| 239 "To append a single item to a list do \"foo += [ bar ]\"."); | |
| 240 return; | |
| 241 } | |
| 242 | |
| 243 default: | |
| 244 break; | |
| 245 } | |
| 246 | |
| 247 *err = Err(op_node->op(), "Incompatible types to add.", | |
| 248 std::string("I see a ") + Value::DescribeType(left->type()) + " and a " + | |
| 249 Value::DescribeType(right.type()) + "."); | |
| 250 } | |
| 251 | |
| 252 Value ExecutePlusEquals(Scope* scope, | |
| 253 const BinaryOpNode* op_node, | |
| 254 const Token& left, | |
| 255 const Value& right, | 506 const Value& right, |
| 256 Err* err) { | 507 Err* err) { |
| 257 // We modify in-place rather than doing read-modify-write to avoid | 508 // Like the += case, we can convert "foo -= bar" to "foo = foo - bar". Since |
| 258 // copying large lists. | 509 // there is no sources filtering, this is always semantically valid. The |
| 259 Value* left_value = | 510 // only case we don't do it is for lists in the current scope which is the |
| 260 scope->GetValueForcedToCurrentScope(left.value(), op_node); | 511 // most common case, and also the one that can be optimized the most by |
| 261 if (!left_value) { | 512 // doing it in-place. |
| 262 *err = Err(left, "Undefined variable for +=.", | 513 Value* mutable_dest = dest->GetExistingMutableValueIfExists(op_node); |
| 263 "I don't have something with this name in scope now."); | 514 if (!mutable_dest || |
| 264 return Value(); | 515 (mutable_dest->type() != Value::LIST || right.type() != Value::LIST)) { |
| 265 } | 516 const Value* existing_value = dest->GetExistingValue(); |
| 266 ValuePlusEquals(scope, op_node, left, left_value, right, false, err); | 517 if (!existing_value) { |
| 267 left_value->set_origin(op_node); | 518 // Undefined left-hand-size for -=. |
| 268 scope->MarkUnused(left.value()); | 519 dest->MakeUndefinedIdentifierForModifyError(err); |
| 269 return Value(); | |
| 270 } | |
| 271 | |
| 272 // We return a null value from this rather than the result of doing the append. | |
| 273 // See ValuePlusEquals for rationale. | |
| 274 void ValueMinusEquals(const BinaryOpNode* op_node, | |
| 275 Value* left, | |
| 276 const Value& right, | |
| 277 bool allow_type_conversion, | |
| 278 Err* err) { | |
| 279 switch (left->type()) { | |
| 280 // Left-hand-side int. | |
| 281 case Value::INTEGER: | |
| 282 switch (right.type()) { | |
| 283 case Value::INTEGER: // int - int -> subtraction. | |
| 284 left->int_value() -= right.int_value(); | |
| 285 return; | |
| 286 | |
| 287 default: | |
| 288 break; | |
| 289 } | |
| 290 break; | |
| 291 | |
| 292 // Left-hand-side string. | |
| 293 case Value::STRING: | |
| 294 break; // All are errors. | |
| 295 | |
| 296 // Left-hand-side list. | |
| 297 case Value::LIST: | |
| 298 if (right.type() != Value::LIST) { | |
| 299 *err = Err(op_node->op(), "Incompatible types to subtract.", | |
| 300 "To remove a single item from a list do \"foo -= [ bar ]\"."); | |
| 301 } else { | |
| 302 RemoveMatchesFromList(op_node, left, right, err); | |
| 303 } | |
| 304 return; | 520 return; |
| 305 | 521 } |
| 306 default: | 522 dest->SetValue(ExecuteMinus(op_node, *existing_value, right, err), op_node); |
| 307 break; | 523 return; |
| 308 } | 524 } |
| 309 | 525 |
| 310 *err = Err(op_node->op(), "Incompatible types to subtract.", | 526 // In-place removal of items from "right". |
| 311 std::string("I see a ") + Value::DescribeType(left->type()) + " and a " + | 527 RemoveMatchesFromList(op_node, mutable_dest, right, err); |
| 312 Value::DescribeType(right.type()) + "."); | |
| 313 } | |
| 314 | |
| 315 Value ExecuteMinusEquals(Scope* scope, | |
| 316 const BinaryOpNode* op_node, | |
| 317 const Token& left, | |
| 318 const Value& right, | |
| 319 Err* err) { | |
| 320 Value* left_value = | |
| 321 scope->GetValueForcedToCurrentScope(left.value(), op_node); | |
| 322 if (!left_value) { | |
| 323 *err = Err(left, "Undefined variable for -=.", | |
| 324 "I don't have something with this name in scope now."); | |
| 325 return Value(); | |
| 326 } | |
| 327 ValueMinusEquals(op_node, left_value, right, false, err); | |
| 328 left_value->set_origin(op_node); | |
| 329 scope->MarkUnused(left.value()); | |
| 330 return Value(); | |
| 331 } | |
| 332 | |
| 333 // Plus/Minus ----------------------------------------------------------------- | |
| 334 | |
| 335 Value ExecutePlus(Scope* scope, | |
| 336 const BinaryOpNode* op_node, | |
| 337 const Value& left, | |
| 338 const Value& right, | |
| 339 Err* err) { | |
| 340 Value ret = left; | |
| 341 ValuePlusEquals(scope, op_node, Token(), &ret, right, true, err); | |
| 342 ret.set_origin(op_node); | |
| 343 return ret; | |
| 344 } | |
| 345 | |
| 346 Value ExecuteMinus(Scope* scope, | |
| 347 const BinaryOpNode* op_node, | |
| 348 const Value& left, | |
| 349 const Value& right, | |
| 350 Err* err) { | |
| 351 Value ret = left; | |
| 352 ValueMinusEquals(op_node, &ret, right, true, err); | |
| 353 ret.set_origin(op_node); | |
| 354 return ret; | |
| 355 } | 528 } |
| 356 | 529 |
| 357 // Comparison ----------------------------------------------------------------- | 530 // Comparison ----------------------------------------------------------------- |
| 358 | 531 |
| 359 Value ExecuteEqualsEquals(Scope* scope, | 532 Value ExecuteEqualsEquals(Scope* scope, |
| 360 const BinaryOpNode* op_node, | 533 const BinaryOpNode* op_node, |
| 361 const Value& left, | 534 const Value& left, |
| 362 const Value& right, | 535 const Value& right, |
| 363 Err* err) { | 536 Err* err) { |
| 364 if (left == right) | 537 if (left == right) |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 const BinaryOpNode* op_node, | 686 const BinaryOpNode* op_node, |
| 514 const ParseNode* left, | 687 const ParseNode* left, |
| 515 const ParseNode* right, | 688 const ParseNode* right, |
| 516 Err* err) { | 689 Err* err) { |
| 517 const Token& op = op_node->op(); | 690 const Token& op = op_node->op(); |
| 518 | 691 |
| 519 // First handle the ones that take an lvalue. | 692 // First handle the ones that take an lvalue. |
| 520 if (op.type() == Token::EQUAL || | 693 if (op.type() == Token::EQUAL || |
| 521 op.type() == Token::PLUS_EQUALS || | 694 op.type() == Token::PLUS_EQUALS || |
| 522 op.type() == Token::MINUS_EQUALS) { | 695 op.type() == Token::MINUS_EQUALS) { |
| 523 const IdentifierNode* left_id = left->AsIdentifier(); | 696 // Compute the left side. |
| 524 if (!left_id) { | 697 ValueDestination dest; |
| 525 *err = Err(op, "Operator requires a lvalue.", | 698 if (!dest.Init(scope, left, op_node, err)) |
| 526 "This thing on the left is not an identifier."); | |
| 527 err->AppendRange(left->GetRange()); | |
| 528 return Value(); | 699 return Value(); |
| 529 } | |
| 530 const Token& dest = left_id->value(); | |
| 531 | 700 |
| 701 // Compute the right side. |
| 532 Value right_value = right->Execute(scope, err); | 702 Value right_value = right->Execute(scope, err); |
| 533 if (err->has_error()) | 703 if (err->has_error()) |
| 534 return Value(); | 704 return Value(); |
| 535 if (right_value.type() == Value::NONE) { | 705 if (right_value.type() == Value::NONE) { |
| 536 *err = Err(op, "Operator requires a rvalue.", | 706 *err = Err(op, "Operator requires a rvalue.", |
| 537 "This thing on the right does not evaluate to a value."); | 707 "This thing on the right does not evaluate to a value."); |
| 538 err->AppendRange(right->GetRange()); | 708 err->AppendRange(right->GetRange()); |
| 539 return Value(); | 709 return Value(); |
| 540 } | 710 } |
| 541 | 711 |
| 542 if (op.type() == Token::EQUAL) | 712 // "foo += bar" (same for "-=") is converted to "foo = foo + bar" here, but |
| 543 return ExecuteEquals(scope, op_node, dest, right_value, err); | 713 // we pass the original value of "foo" by pointer to avoid a copy. |
| 544 if (op.type() == Token::PLUS_EQUALS) | 714 if (op.type() == Token::EQUAL) { |
| 545 return ExecutePlusEquals(scope, op_node, dest, right_value, err); | 715 ExecuteEquals(scope, op_node, &dest, std::move(right_value), err); |
| 546 if (op.type() == Token::MINUS_EQUALS) | 716 } else if (op.type() == Token::PLUS_EQUALS) { |
| 547 return ExecuteMinusEquals(scope, op_node, dest, right_value, err); | 717 ExecutePlusEquals(scope, op_node, &dest, std::move(right_value), err); |
| 548 NOTREACHED(); | 718 } else if (op.type() == Token::MINUS_EQUALS) { |
| 719 ExecuteMinusEquals(op_node, &dest, right_value, err); |
| 720 } else { |
| 721 NOTREACHED(); |
| 722 } |
| 549 return Value(); | 723 return Value(); |
| 550 } | 724 } |
| 551 | 725 |
| 552 // ||, &&. Passed the node instead of the value so that they can avoid | 726 // ||, &&. Passed the node instead of the value so that they can avoid |
| 553 // evaluating the RHS on early-out. | 727 // evaluating the RHS on early-out. |
| 554 if (op.type() == Token::BOOLEAN_OR) | 728 if (op.type() == Token::BOOLEAN_OR) |
| 555 return ExecuteOr(scope, op_node, left, right, err); | 729 return ExecuteOr(scope, op_node, left, right, err); |
| 556 if (op.type() == Token::BOOLEAN_AND) | 730 if (op.type() == Token::BOOLEAN_AND) |
| 557 return ExecuteAnd(scope, op_node, left, right, err); | 731 return ExecuteAnd(scope, op_node, left, right, err); |
| 558 | 732 |
| 733 // Everything else works on the evaluated left and right values. |
| 559 Value left_value = GetValueOrFillError(op_node, left, "left", scope, err); | 734 Value left_value = GetValueOrFillError(op_node, left, "left", scope, err); |
| 560 if (err->has_error()) | 735 if (err->has_error()) |
| 561 return Value(); | 736 return Value(); |
| 562 Value right_value = GetValueOrFillError(op_node, right, "right", scope, err); | 737 Value right_value = GetValueOrFillError(op_node, right, "right", scope, err); |
| 563 if (err->has_error()) | 738 if (err->has_error()) |
| 564 return Value(); | 739 return Value(); |
| 565 | 740 |
| 566 // +, -. | 741 // +, -. |
| 567 if (op.type() == Token::MINUS) | 742 if (op.type() == Token::MINUS) |
| 568 return ExecuteMinus(scope, op_node, left_value, right_value, err); | 743 return ExecuteMinus(op_node, std::move(left_value), right_value, err); |
| 569 if (op.type() == Token::PLUS) | 744 if (op.type() == Token::PLUS) { |
| 570 return ExecutePlus(scope, op_node, left_value, right_value, err); | 745 return ExecutePlus(op_node, std::move(left_value), std::move(right_value), |
| 746 true, err); |
| 747 } |
| 571 | 748 |
| 572 // Comparisons. | 749 // Comparisons. |
| 573 if (op.type() == Token::EQUAL_EQUAL) | 750 if (op.type() == Token::EQUAL_EQUAL) |
| 574 return ExecuteEqualsEquals(scope, op_node, left_value, right_value, err); | 751 return ExecuteEqualsEquals(scope, op_node, left_value, right_value, err); |
| 575 if (op.type() == Token::NOT_EQUAL) | 752 if (op.type() == Token::NOT_EQUAL) |
| 576 return ExecuteNotEquals(scope, op_node, left_value, right_value, err); | 753 return ExecuteNotEquals(scope, op_node, left_value, right_value, err); |
| 577 if (op.type() == Token::GREATER_EQUAL) | 754 if (op.type() == Token::GREATER_EQUAL) |
| 578 return ExecuteGreaterEquals(scope, op_node, left_value, right_value, err); | 755 return ExecuteGreaterEquals(scope, op_node, left_value, right_value, err); |
| 579 if (op.type() == Token::LESS_EQUAL) | 756 if (op.type() == Token::LESS_EQUAL) |
| 580 return ExecuteLessEquals(scope, op_node, left_value, right_value, err); | 757 return ExecuteLessEquals(scope, op_node, left_value, right_value, err); |
| 581 if (op.type() == Token::GREATER_THAN) | 758 if (op.type() == Token::GREATER_THAN) |
| 582 return ExecuteGreater(scope, op_node, left_value, right_value, err); | 759 return ExecuteGreater(scope, op_node, left_value, right_value, err); |
| 583 if (op.type() == Token::LESS_THAN) | 760 if (op.type() == Token::LESS_THAN) |
| 584 return ExecuteLess(scope, op_node, left_value, right_value, err); | 761 return ExecuteLess(scope, op_node, left_value, right_value, err); |
| 585 | 762 |
| 586 return Value(); | 763 return Value(); |
| 587 } | 764 } |
| OLD | NEW |