• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef TOOLS_GN_TEMPLATE_H_
6 #define TOOLS_GN_TEMPLATE_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/memory/ref_counted.h"
12 
13 class BlockNode;
14 class Err;
15 class FunctionCallNode;
16 class LocationRange;
17 class Scope;
18 class Value;
19 
20 // Represents the information associated with a template() call in GN, which
21 // includes a closure and the code to run when the template is invoked.
22 //
23 // This class is immutable so we can reference it from multiple threads without
24 // locking. Normally, this will be associated with a .gni file and then a
25 // reference will be taken by each .gn file that imports it. These files might
26 // execute the template in parallel.
27 class Template : public base::RefCountedThreadSafe<Template> {
28  public:
29   // Makes a new closure based on the given scope.
30   Template(const Scope* scope, const FunctionCallNode* def);
31 
32   // Takes ownership of a previously-constructed closure.
33   Template(std::unique_ptr<Scope> closure, const FunctionCallNode* def);
34 
35   // Invoke the template. The values correspond to the state of the code
36   // invoking the template. The template name needs to be supplied since the
37   // template object itself doesn't know what name the calling code is using
38   // to refer to it (this is used to set defaults).
39   Value Invoke(Scope* scope,
40                const FunctionCallNode* invocation,
41                const std::string& template_name,
42                const std::vector<Value>& args,
43                BlockNode* block,
44                Err* err) const;
45 
46   // Returns the location range where this template was defined.
47   LocationRange GetDefinitionRange() const;
48 
49  private:
50   friend class base::RefCountedThreadSafe<Template>;
51 
52   Template();
53   ~Template();
54 
55   // It's important that this Scope is const. A template can be referenced by
56   // the root BUILDCONFIG file and then duplicated to all threads. Therefore,
57   // this scope must be usable from multiple threads at the same time.
58   //
59   // When executing a template, a new scope will be created as a child of this
60   // one, which will reference it as mutable or not according to the mutability
61   // of this value.
62   std::unique_ptr<const Scope> closure_;
63 
64   const FunctionCallNode* definition_;
65 };
66 
67 #endif  // TOOLS_GN_TEMPLATE_H_
68