OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 const FunctionCallNode* function, | 165 const FunctionCallNode* function, |
166 const ListNode* args_list, | 166 const ListNode* args_list, |
167 Err* err) { | 167 Err* err) { |
168 const auto& args_vector = args_list->contents(); | 168 const auto& args_vector = args_list->contents(); |
169 if (args_vector.size() != 2 && args_vector.size() != 3) { | 169 if (args_vector.size() != 2 && args_vector.size() != 3) { |
170 *err = Err(function, "Wrong number of arguments.", | 170 *err = Err(function, "Wrong number of arguments.", |
171 "Expecting two or three arguments."); | 171 "Expecting two or three arguments."); |
172 return Value(); | 172 return Value(); |
173 } | 173 } |
174 | 174 |
175 // Extract the scope identifier. This assumes the first parameter is an | 175 Value* value = nullptr; // Value to use, may point to result_value. |
176 // identifier. It is difficult to write code where this is not the case, and | 176 Value result_value; // Storage for the "evaluate" case. |
177 // this saves an expensive scope copy. If necessary, this could be expanded | |
178 // to execute the ParseNode and get the value out if it's not an identifer. | |
179 const IdentifierNode* identifier = args_vector[0]->AsIdentifier(); | 177 const IdentifierNode* identifier = args_vector[0]->AsIdentifier(); |
180 if (!identifier) { | 178 if (identifier) { |
181 *err = Err(args_vector[0].get(), "Expected an identifier for the scope."); | 179 // Optimize the common case where the input scope is an identifier. This |
182 return Value(); | 180 // prevents a copy of a potentially large Scope object. |
| 181 value = scope->GetMutableValue(identifier->value().value(), |
| 182 Scope::SEARCH_NESTED, true); |
| 183 if (!value) { |
| 184 *err = Err(identifier, "Undefined identifier."); |
| 185 return Value(); |
| 186 } |
| 187 } else { |
| 188 // Non-optimized case, just evaluate the argument. |
| 189 result_value = args_vector[0]->Execute(scope, err); |
| 190 if (err->has_error()) |
| 191 return Value(); |
| 192 value = &result_value; |
183 } | 193 } |
184 | 194 |
185 // Extract the source scope. | 195 // Extract the source scope. |
186 Value* value = scope->GetMutableValue( | |
187 identifier->value().value(), Scope::SEARCH_NESTED, true); | |
188 if (!value) { | |
189 *err = Err(identifier, "Undefined identifier."); | |
190 return Value(); | |
191 } | |
192 if (!value->VerifyTypeIs(Value::SCOPE, err)) | 196 if (!value->VerifyTypeIs(Value::SCOPE, err)) |
193 return Value(); | 197 return Value(); |
194 Scope* source = value->scope_value(); | 198 Scope* source = value->scope_value(); |
195 | 199 |
196 // Extract the exclusion list if defined. | 200 // Extract the exclusion list if defined. |
197 std::set<std::string> exclusion_set; | 201 std::set<std::string> exclusion_set; |
198 if (args_vector.size() == 3) { | 202 if (args_vector.size() == 3) { |
199 Value exclusion_value = args_vector[2]->Execute(scope, err); | 203 Value exclusion_value = args_vector[2]->Execute(scope, err); |
200 if (err->has_error()) | 204 if (err->has_error()) |
201 return Value(); | 205 return Value(); |
(...skipping 29 matching lines...) Expand all Loading... |
231 } | 235 } |
232 } | 236 } |
233 | 237 |
234 // Not the right type of argument. | 238 // Not the right type of argument. |
235 *err = Err(what_value, "Not a valid list of variables to copy.", | 239 *err = Err(what_value, "Not a valid list of variables to copy.", |
236 "Expecting either the string \"*\" or a list of strings."); | 240 "Expecting either the string \"*\" or a list of strings."); |
237 return Value(); | 241 return Value(); |
238 } | 242 } |
239 | 243 |
240 } // namespace functions | 244 } // namespace functions |
OLD | NEW |