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
3 // found in the LICENSE file.
5 #include "tools/gn/template.h"
7 #include "tools/gn/err.h"
8 #include "tools/gn/functions.h"
9 #include "tools/gn/parse_tree.h"
10 #include "tools/gn/scope.h"
11 #include "tools/gn/scope_per_file_provider.h"
12 #include "tools/gn/value.h"
14 Template::Template(const Scope
* scope
, const FunctionCallNode
* def
)
15 : closure_(scope
->MakeClosure()),
19 Template::Template(scoped_ptr
<Scope
> scope
, const FunctionCallNode
* def
)
20 : closure_(scope
.Pass()),
24 Template::~Template() {
27 scoped_ptr
<Template
> Template::Clone() const {
28 // We can make a new closure from our closure to copy it.
29 return scoped_ptr
<Template
>(
30 new Template(closure_
->MakeClosure(), definition_
));
33 Value
Template::Invoke(Scope
* scope
,
34 const FunctionCallNode
* invocation
,
35 const std::vector
<Value
>& args
,
38 // Don't allow templates to be executed from imported files. Imports are for
39 // simple values only.
40 if (!EnsureNotProcessingImport(invocation
, scope
, err
))
43 // First run the invocation's block. Need to allocate the scope on the heap
44 // so we can pass ownership to the template.
45 scoped_ptr
<Scope
> invocation_scope(new Scope(scope
));
46 if (!FillTargetBlockScope(scope
, invocation
,
47 invocation
->function().value().as_string(),
48 block
, args
, invocation_scope
.get(), err
))
50 block
->ExecuteBlockInScope(invocation_scope
.get(), err
);
54 // Set up the scope to run the template and set the current directory for the
55 // template (which ScopePerFileProvider uses to base the target-related
56 // variables target_gen_dir and target_out_dir on) to be that of the invoker.
57 // This way, files don't have to be rebased and target_*_dir works the way
58 // people expect (otherwise its to easy to be putting generated files in the
59 // gen dir corresponding to an imported file).
60 Scope
template_scope(closure_
.get());
61 template_scope
.set_source_dir(scope
->GetSourceDir());
63 ScopePerFileProvider
per_file_provider(&template_scope
, true);
65 // We jump through some hoops to avoid copying the invocation scope when
66 // setting it in the template scope (since the invocation scope may have
67 // large lists of source files in it and could be expensive to copy).
69 // Scope.SetValue will copy the value which will in turn copy the scope, but
70 // if we instead create a value and then set the scope on it, the copy can
72 const char kInvoker
[] = "invoker";
73 template_scope
.SetValue(kInvoker
, Value(NULL
, scoped_ptr
<Scope
>()),
75 Value
* invoker_value
= template_scope
.GetMutableValue(kInvoker
, false);
76 invoker_value
->SetScopeValue(invocation_scope
.Pass());
77 template_scope
.set_source_dir(scope
->GetSourceDir());
79 const base::StringPiece
target_name("target_name");
80 template_scope
.SetValue(target_name
,
81 Value(invocation
, args
[0].string_value()),
84 // Actually run the template code.
86 definition_
->block()->ExecuteBlockInScope(&template_scope
, err
);
90 // Check for unused variables in the invocation scope. This will find typos
91 // of things the caller meant to pass to the template but the template didn't
94 // This is a bit tricky because it's theoretically possible for the template
95 // to overwrite the value of "invoker" and free the Scope owned by the
96 // value. So we need to look it up again and don't do anything if it doesn't
98 invoker_value
= template_scope
.GetMutableValue(kInvoker
, false);
99 if (invoker_value
&& invoker_value
->type() == Value::SCOPE
) {
100 if (!invoker_value
->scope_value()->CheckForUnusedVars(err
))
104 // Check for unused variables in the template itself.
105 if (!template_scope
.CheckForUnusedVars(err
))
111 LocationRange
Template::GetDefinitionRange() const {
112 return definition_
->GetRange();