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 | 15 |
16 Template::Template(const Scope* scope, const FunctionCallNode* def) | 16 Template::Template(const Scope* scope, const FunctionCallNode* def) |
17 : closure_(scope->MakeClosure()), | 17 : closure_(scope->MakeClosure()), |
18 definition_(def) { | 18 definition_(def) { |
19 } | 19 } |
20 | 20 |
21 Template::Template(std::unique_ptr<Scope> scope, const FunctionCallNode* def) | 21 Template::Template(std::unique_ptr<Scope> scope, const FunctionCallNode* def) |
22 : closure_(std::move(scope)), definition_(def) {} | 22 : closure_(std::move(scope)), definition_(def) {} |
23 | 23 |
24 Template::~Template() { | 24 Template::~Template() { |
25 } | 25 } |
26 | 26 |
27 Value Template::Invoke(Scope* scope, | 27 Value Template::Invoke(Scope* scope, |
28 const FunctionCallNode* invocation, | 28 const FunctionCallNode* invocation, |
| 29 const std::string& template_name, |
29 const std::vector<Value>& args, | 30 const std::vector<Value>& args, |
30 BlockNode* block, | 31 BlockNode* block, |
31 Err* err) const { | 32 Err* err) const { |
32 // Don't allow templates to be executed from imported files. Imports are for | 33 // Don't allow templates to be executed from imported files. Imports are for |
33 // simple values only. | 34 // simple values only. |
34 if (!EnsureNotProcessingImport(invocation, scope, err)) | 35 if (!EnsureNotProcessingImport(invocation, scope, err)) |
35 return Value(); | 36 return Value(); |
36 | 37 |
37 // First run the invocation's block. Need to allocate the scope on the heap | 38 // First run the invocation's block. Need to allocate the scope on the heap |
38 // so we can pass ownership to the template. | 39 // so we can pass ownership to the template. |
39 std::unique_ptr<Scope> invocation_scope(new Scope(scope)); | 40 std::unique_ptr<Scope> invocation_scope(new Scope(scope)); |
40 if (!FillTargetBlockScope(scope, invocation, | 41 if (!FillTargetBlockScope(scope, invocation, template_name, |
41 invocation->function().value().as_string(), | |
42 block, args, invocation_scope.get(), err)) | 42 block, args, invocation_scope.get(), err)) |
43 return Value(); | 43 return Value(); |
44 | 44 |
45 { | 45 { |
46 // Don't allow the block of the template invocation to include other | 46 // Don't allow the block of the template invocation to include other |
47 // targets configs, or template invocations. This must only be applied | 47 // targets configs, or template invocations. This must only be applied |
48 // to the invoker's block rather than the whole function because the | 48 // to the invoker's block rather than the whole function because the |
49 // template execution itself must be able to define targets, etc. | 49 // template execution itself must be able to define targets, etc. |
50 NonNestableBlock non_nestable(scope, invocation, "template invocation"); | 50 NonNestableBlock non_nestable(scope, invocation, "template invocation"); |
51 if (!non_nestable.Enter(err)) | 51 if (!non_nestable.Enter(err)) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 // Check for unused variables in the template itself. | 116 // Check for unused variables in the template itself. |
117 if (!template_scope.CheckForUnusedVars(err)) | 117 if (!template_scope.CheckForUnusedVars(err)) |
118 return Value(); | 118 return Value(); |
119 | 119 |
120 return result; | 120 return result; |
121 } | 121 } |
122 | 122 |
123 LocationRange Template::GetDefinitionRange() const { | 123 LocationRange Template::GetDefinitionRange() const { |
124 return definition_->GetRange(); | 124 return definition_->GetRange(); |
125 } | 125 } |
OLD | NEW |