aboutsummaryrefslogtreecommitdiff
path: root/build/variable
blob: ae4c7b310dc58df1099b1956ca3183fcb86c30ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// file      : build/variable -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_VARIABLE
#define BUILD_VARIABLE

#include <string>
#include <memory>        // unique_ptr
#include <utility>       // move()
#include <cassert>
#include <typeindex>
#include <unordered_set>

#include <build/path>
#include <build/name>
#include <build/prefix-map>

namespace build
{
  class scope;
  struct value;

  struct value_type
  {
    std::type_index id;
    value* (*const factory) ();
  };

  // variable
  //
  // The two variables are considered the same if they have the same name.
  //
  struct variable
  {
    explicit
    variable (std::string n): name (std::move (n)), type (nullptr) {}

    std::string name;
    const value_type* type; // If NULL, then this variable has no fixed type.
  };

  inline bool
  operator== (const variable& x, const variable& y) {return x.name == y.name;}

  typedef std::reference_wrapper<const variable> variable_cref;

  // value
  //
  struct value
  {
    typedef build::scope scope_type;

    virtual
    ~value () = default;

    value (scope_type& s): scope (s) {}

    scope_type& scope; // Scope to which this value belongs.
  };
  typedef std::unique_ptr<value> value_ptr;

  struct list_value: value
  {
    list_value (scope_type& s, names d): value (s), data (std::move (d)) {}

    list_value (scope_type& s, std::string d)
        : value (s)
    {
      data.emplace_back (std::move (d));
    }

    // Note: stored in name as a directory.
    //
    list_value (scope_type& s, path d)
        : value (s)
    {
      data.emplace_back (std::move (d));
    }

    names data;
  };
  typedef std::unique_ptr<list_value> list_value_ptr;

  // value_proxy
  //
  struct value_proxy
  {
    explicit operator bool () const {return p != nullptr && *p != nullptr;}
    explicit operator value_ptr& () const {return *p;}

    // Get interface. See available specializations below.
    //
    template <typename T>
    T
    as () const = delete;

    // Set interface.
    //
    const value_proxy&
    operator= (value_ptr v) const
    {
      assert (v == nullptr || &v->scope == s);
      *p = std::move (v);
      return *this;
    }

    const value_proxy&
    operator= (std::string v) const
    {
      // In most cases this is used to initialize a new variable, so
      // don't bother trying to optimize for the case where p is not
      // NULL.
      //
      p->reset (new list_value (*s, std::move (v)));
      return *this;
    }

    // Note: stored in name as a directory.
    //
    const value_proxy&
    operator= (path v) const
    {
      p->reset (new list_value (*s, std::move (v)));
      return *this;
    }

    // Implementation details.
    //
    explicit
    value_proxy (value_ptr* p, scope* s): p (p), s (s) {}

  protected:
    value_ptr* p;
    scope* s;
  };

  template <>
  list_value& value_proxy::
  as<list_value&> () const;

  template <>
  const std::string& value_proxy::
  as<const std::string&> () const;

  // Note: get the name's directory.
  //
  template <>
  const path& value_proxy::
  as<const path&> () const;
}

namespace std
{
  template <>
  struct hash<build::variable>: hash<string>
  {
    size_t
    operator() (const build::variable& v) const noexcept
    {
      return hash<string>::operator() (v.name);
    }
  };
}

namespace build
{
  // variable_pool
  //
  struct variable_set: std::unordered_set<variable>
  {
    // @@ Need to check/set type?
    //
    const variable&
    find (std::string name) {return *emplace (std::move (name)).first;}
  };

  extern variable_set variable_pool;

  // variable_map
  //
  template <>
  struct compare_prefix<variable_cref>: compare_prefix<std::string>
  {
    typedef compare_prefix<std::string> base;

    explicit
    compare_prefix (char d): base (d) {}

    bool
    operator() (const variable& x, const variable& y) const
    {
      return base::operator() (x.name, y.name);
    }

    bool
    prefix (const variable& p, const variable& k) const
    {
      return base::prefix (p.name, k.name);
    }
  };

  struct variable_map: prefix_map<variable_cref, value_ptr, '.'>
  {
    typedef prefix_map<variable_cref, value_ptr, '.'> base;

    value_proxy
    operator[] (const std::string& v)
    {
      return operator[] (variable_pool.find (v));
    }

    value_proxy
    operator[] (const variable& v)
    {
      return value_proxy (&base::operator[] (v), &scope_);
    }

    explicit
    variable_map (scope& s): scope_ (s) {}

  private:
    scope& scope_; // Scope to which this map belongs (and all its value).
  };
}

#endif // BUILD_VARIABLE