| 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/template.h" | 5 #include "tools/gn/template.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "tools/gn/err.h" | 9 #include "tools/gn/err.h" |
| 10 #include "tools/gn/functions.h" | 10 #include "tools/gn/functions.h" |
| 11 #include "tools/gn/parse_tree.h" | 11 #include "tools/gn/parse_tree.h" |
| 12 #include "tools/gn/scope.h" | 12 #include "tools/gn/scope.h" |
| 13 #include "tools/gn/scope_per_file_provider.h" | 13 #include "tools/gn/scope_per_file_provider.h" |
| 14 #include "tools/gn/value.h" | 14 #include "tools/gn/value.h" |
| 15 #include "tools/gn/variables.h" |
| 15 | 16 |
| 16 Template::Template(const Scope* scope, const FunctionCallNode* def) | 17 Template::Template(const Scope* scope, const FunctionCallNode* def) |
| 17 : closure_(scope->MakeClosure()), | 18 : closure_(scope->MakeClosure()), |
| 18 definition_(def) { | 19 definition_(def) { |
| 19 } | 20 } |
| 20 | 21 |
| 21 Template::Template(std::unique_ptr<Scope> scope, const FunctionCallNode* def) | 22 Template::Template(std::unique_ptr<Scope> scope, const FunctionCallNode* def) |
| 22 : closure_(std::move(scope)), definition_(def) {} | 23 : closure_(std::move(scope)), definition_(def) {} |
| 23 | 24 |
| 24 Template::~Template() { | 25 Template::~Template() { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Targets defined in the template go in the collector for the invoking file. | 71 // Targets defined in the template go in the collector for the invoking file. |
| 71 template_scope.set_item_collector(scope->GetItemCollector()); | 72 template_scope.set_item_collector(scope->GetItemCollector()); |
| 72 | 73 |
| 73 // We jump through some hoops to avoid copying the invocation scope when | 74 // We jump through some hoops to avoid copying the invocation scope when |
| 74 // setting it in the template scope (since the invocation scope may have | 75 // setting it in the template scope (since the invocation scope may have |
| 75 // large lists of source files in it and could be expensive to copy). | 76 // large lists of source files in it and could be expensive to copy). |
| 76 // | 77 // |
| 77 // Scope.SetValue will copy the value which will in turn copy the scope, but | 78 // Scope.SetValue will copy the value which will in turn copy the scope, but |
| 78 // if we instead create a value and then set the scope on it, the copy can | 79 // if we instead create a value and then set the scope on it, the copy can |
| 79 // be avoided. | 80 // be avoided. |
| 80 const char kInvoker[] = "invoker"; | 81 template_scope.SetValue(variables::kInvoker, |
| 81 template_scope.SetValue(kInvoker, Value(nullptr, std::unique_ptr<Scope>()), | 82 Value(nullptr, std::unique_ptr<Scope>()), invocation); |
| 82 invocation); | 83 Value* invoker_value = |
| 83 Value* invoker_value = template_scope.GetMutableValue(kInvoker, false); | 84 template_scope.GetMutableValue(variables::kInvoker, false); |
| 84 invoker_value->SetScopeValue(std::move(invocation_scope)); | 85 invoker_value->SetScopeValue(std::move(invocation_scope)); |
| 85 template_scope.set_source_dir(scope->GetSourceDir()); | 86 template_scope.set_source_dir(scope->GetSourceDir()); |
| 86 | 87 |
| 87 const base::StringPiece target_name("target_name"); | 88 const base::StringPiece target_name(variables::kTargetName); |
| 88 template_scope.SetValue(target_name, | 89 template_scope.SetValue(target_name, |
| 89 Value(invocation, args[0].string_value()), | 90 Value(invocation, args[0].string_value()), |
| 90 invocation); | 91 invocation); |
| 91 | 92 |
| 92 // Actually run the template code. | 93 // Actually run the template code. |
| 93 Value result = | 94 Value result = |
| 94 definition_->block()->Execute(&template_scope, err); | 95 definition_->block()->Execute(&template_scope, err); |
| 95 if (err->has_error()) { | 96 if (err->has_error()) { |
| 96 // If there was an error, append the caller location so the error message | 97 // If there was an error, append the caller location so the error message |
| 97 // displays a stack trace of how it got here. | 98 // displays a stack trace of how it got here. |
| 98 err->AppendSubErr(Err(invocation, "whence it was called.")); | 99 err->AppendSubErr(Err(invocation, "whence it was called.")); |
| 99 return Value(); | 100 return Value(); |
| 100 } | 101 } |
| 101 | 102 |
| 102 // Check for unused variables in the invocation scope. This will find typos | 103 // Check for unused variables in the invocation scope. This will find typos |
| 103 // of things the caller meant to pass to the template but the template didn't | 104 // of things the caller meant to pass to the template but the template didn't |
| 104 // read out. | 105 // read out. |
| 105 // | 106 // |
| 106 // This is a bit tricky because it's theoretically possible for the template | 107 // This is a bit tricky because it's theoretically possible for the template |
| 107 // to overwrite the value of "invoker" and free the Scope owned by the | 108 // to overwrite the value of "invoker" and free the Scope owned by the |
| 108 // value. So we need to look it up again and don't do anything if it doesn't | 109 // value. So we need to look it up again and don't do anything if it doesn't |
| 109 // exist. | 110 // exist. |
| 110 invoker_value = template_scope.GetMutableValue(kInvoker, false); | 111 invoker_value = template_scope.GetMutableValue(variables::kInvoker, false); |
| 111 if (invoker_value && invoker_value->type() == Value::SCOPE) { | 112 if (invoker_value && invoker_value->type() == Value::SCOPE) { |
| 112 if (!invoker_value->scope_value()->CheckForUnusedVars(err)) | 113 if (!invoker_value->scope_value()->CheckForUnusedVars(err)) |
| 113 return Value(); | 114 return Value(); |
| 114 } | 115 } |
| 115 | 116 |
| 116 // Check for unused variables in the template itself. | 117 // Check for unused variables in the template itself. |
| 117 if (!template_scope.CheckForUnusedVars(err)) | 118 if (!template_scope.CheckForUnusedVars(err)) |
| 118 return Value(); | 119 return Value(); |
| 119 | 120 |
| 120 return result; | 121 return result; |
| 121 } | 122 } |
| 122 | 123 |
| 123 LocationRange Template::GetDefinitionRange() const { | 124 LocationRange Template::GetDefinitionRange() const { |
| 124 return definition_->GetRange(); | 125 return definition_->GetRange(); |
| 125 } | 126 } |
| OLD | NEW |