| 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/err.h" | 5 #include "tools/gn/err.h" |
| 6 #include "tools/gn/functions.h" | 6 #include "tools/gn/functions.h" |
| 7 #include "tools/gn/parse_tree.h" | 7 #include "tools/gn/parse_tree.h" |
| 8 #include "tools/gn/scope.h" | 8 #include "tools/gn/scope.h" |
| 9 | 9 |
| 10 namespace functions { | 10 namespace functions { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 "\n" | 40 "\n" |
| 41 " Prints:\n" | 41 " Prints:\n" |
| 42 " a\n" | 42 " a\n" |
| 43 " b\n" | 43 " b\n" |
| 44 " c\n"; | 44 " c\n"; |
| 45 | 45 |
| 46 Value RunForEach(Scope* scope, | 46 Value RunForEach(Scope* scope, |
| 47 const FunctionCallNode* function, | 47 const FunctionCallNode* function, |
| 48 const ListNode* args_list, | 48 const ListNode* args_list, |
| 49 Err* err) { | 49 Err* err) { |
| 50 const std::vector<const ParseNode*>& args_vector = args_list->contents(); | 50 const auto& args_vector = args_list->contents(); |
| 51 if (args_vector.size() != 2) { | 51 if (args_vector.size() != 2) { |
| 52 *err = Err(function, "Wrong number of arguments to foreach().", | 52 *err = Err(function, "Wrong number of arguments to foreach().", |
| 53 "Expecting exactly two."); | 53 "Expecting exactly two."); |
| 54 return Value(); | 54 return Value(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Extract the loop variable. | 57 // Extract the loop variable. |
| 58 const IdentifierNode* identifier = args_vector[0]->AsIdentifier(); | 58 const IdentifierNode* identifier = args_vector[0]->AsIdentifier(); |
| 59 if (!identifier) { | 59 if (!identifier) { |
| 60 *err = Err(args_vector[0], "Expected an identifier for the loop var."); | 60 *err = |
| 61 Err(args_vector[0].get(), "Expected an identifier for the loop var."); |
| 61 return Value(); | 62 return Value(); |
| 62 } | 63 } |
| 63 base::StringPiece loop_var(identifier->value().value()); | 64 base::StringPiece loop_var(identifier->value().value()); |
| 64 | 65 |
| 65 // Extract the list, avoid a copy if it's an identifier (common case). | 66 // Extract the list, avoid a copy if it's an identifier (common case). |
| 66 Value value_storage_for_exec; // Backing for list_value when we need to exec. | 67 Value value_storage_for_exec; // Backing for list_value when we need to exec. |
| 67 const Value* list_value = nullptr; | 68 const Value* list_value = nullptr; |
| 68 const IdentifierNode* list_identifier = args_vector[1]->AsIdentifier(); | 69 const IdentifierNode* list_identifier = args_vector[1]->AsIdentifier(); |
| 69 if (list_identifier) { | 70 if (list_identifier) { |
| 70 list_value = scope->GetValue(list_identifier->value().value(), true); | 71 list_value = scope->GetValue(list_identifier->value().value(), true); |
| 71 if (!list_value) { | 72 if (!list_value) { |
| 72 *err = Err(args_vector[1], "Undefined identifier."); | 73 *err = Err(args_vector[1].get(), "Undefined identifier."); |
| 73 return Value(); | 74 return Value(); |
| 74 } | 75 } |
| 75 } else { | 76 } else { |
| 76 // Not an identifier, evaluate the node to get the result. | 77 // Not an identifier, evaluate the node to get the result. |
| 77 Scope list_exec_scope(scope); | 78 Scope list_exec_scope(scope); |
| 78 value_storage_for_exec = args_vector[1]->Execute(scope, err); | 79 value_storage_for_exec = args_vector[1]->Execute(scope, err); |
| 79 if (err->has_error()) | 80 if (err->has_error()) |
| 80 return Value(); | 81 return Value(); |
| 81 list_value = &value_storage_for_exec; | 82 list_value = &value_storage_for_exec; |
| 82 } | 83 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 112 scope->SetValue(loop_var, old_loop_value, old_loop_value.origin()); | 113 scope->SetValue(loop_var, old_loop_value, old_loop_value.origin()); |
| 113 } else { | 114 } else { |
| 114 // Loop variable was undefined before loop, delete it. | 115 // Loop variable was undefined before loop, delete it. |
| 115 scope->RemoveIdentifier(loop_var); | 116 scope->RemoveIdentifier(loop_var); |
| 116 } | 117 } |
| 117 | 118 |
| 118 return Value(); | 119 return Value(); |
| 119 } | 120 } |
| 120 | 121 |
| 121 } // namespace functions | 122 } // namespace functions |
| OLD | NEW |