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 <vector>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13
14class BlockNode;
15class Err;
16class FunctionCallNode;
17class LocationRange;
18class Scope;
19class Value;
20
21// Represents the information associated with a template() call in GN, which
22// includes a closure and the code to run when the template is invoked.
23//
24// This class is immutable so we can reference it from multiple threads without
25// locking. Normally, this will be assocated with a .gni file and then a
26// reference will be taken by each .gn file that imports it. These files might
27// execute the template in parallel.
28class Template : public base::RefCountedThreadSafe<Template> {
29 public:
30  // Makes a new closure based on the given scope.
31  Template(const Scope* scope, const FunctionCallNode* def);
32
33  // Takes ownership of a previously-constructed closure.
34  Template(scoped_ptr<Scope> closure, const FunctionCallNode* def);
35
36  // Invoke the template. The values correspond to the state of the code
37  // invoking the template.
38  Value Invoke(Scope* scope,
39               const FunctionCallNode* invocation,
40               const std::vector<Value>& args,
41               BlockNode* block,
42               Err* err) const;
43
44  // Returns the location range where this template was defined.
45  LocationRange GetDefinitionRange() const;
46
47 private:
48  friend class base::RefCountedThreadSafe<Template>;
49
50  Template();
51  ~Template();
52
53  scoped_ptr<Scope> closure_;
54  const FunctionCallNode* definition_;
55};
56
57#endif  // TOOLS_GN_TEMPLATE_H_
58