Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(467)

Side by Side Diff: tools/gn/function_foreach.cc

Issue 1884503003: GN: Replace vector<ParseNode*> with vector<unique_ptr<ParseNode>> in parse_tree.h. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/command_format.cc ('k') | tools/gn/function_forward_variables_from.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
OLDNEW
« no previous file with comments | « tools/gn/command_format.cc ('k') | tools/gn/function_forward_variables_from.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698