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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// file : build/variable.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <build/variable>
#include <iterator> // make_move_iterator()
#include <build/utility>
#include <build/diagnostics>
using namespace std;
namespace build
{
// value
//
void
assign (value& v, const value_type* t, const variable& var)
{
if (v.type == nullptr)
{
v.type = t;
if (v && t->assign != nullptr)
v.state_ = t->assign (v.data_, var)
? value::state_type::filled
: value::state_type::empty;
}
else
fail << "variable '" << var.name << "' type mismatch" <<
info << "value '" << v.data_ << "' is " << v.type->name <<
info << (t == var.type ? "variable" : "new type") << " is "
<< (var.type != nullptr ? var.type->name : "untyped");
}
value& value::
operator= (value&& v)
{
assert (type == nullptr || type == v.type);
// Since the types are the same, we don't need to call
// the callbacks.
//
type = v.type;
state_ = v.state_;
data_ = move (v.data_);
return *this;
}
value& value::
append (value v, const variable& var)
{
assert (type == v.type);
append (move (v.data_), var);
return *this;
}
void value::
append (names v, const variable& var)
{
// Treat append to NULL as assign.
//
if (!null () && type != nullptr && type->append != nullptr)
{
state_ = type->append (data_, move (v), var)
? state_type::filled
: state_type::empty;
return;
}
if (data_.empty ())
data_ = move (v);
else
data_.insert (data_.end (),
make_move_iterator (v.begin ()),
make_move_iterator (v.end ()));
state_ = (type != nullptr && type->assign != nullptr
? type->assign (data_, var)
: !data_.empty ())
? state_type::filled
: state_type::empty;
}
// bool value
//
bool value_traits<bool>::
assign (name& n)
{
if (n.simple ())
{
const string& s (n.value);
if (s == "true" || s == "false")
return true;
}
return false;
}
static bool
bool_assign (names& v, const variable& var)
{
// Verify the value is either "true" or "false".
//
if (v.size () == 1)
{
name& n (v.front ());
if (value_traits<bool>::assign (n))
return true;
}
fail << "invalid bool variable '" << var.name << "' value '" << v << "'";
return false;
}
static bool
bool_append (names& v, names a, const variable& var)
{
// Translate append to OR.
//
bool_assign (a, var); // Verify "true" or "false".
if (a.front ().value[0] == 't' && v.front ().value[0] == 'f')
v = move (a);
return true;
}
const value_type value_traits<bool>::value_type
{
"bool",
&bool_assign,
&bool_append
};
const value_type* bool_type = &value_traits<bool>::value_type;
// string value
//
bool value_traits<string>::
assign (name& n)
{
// The below code is quite convoluted because we don't want to
// modify the name until we know it good (if it is not, then it
// will most likely be printed by the caller in diagnostics).
// Suspend project qualification.
//
const string* p (n.proj);
n.proj = nullptr;
// Convert directory to string.
//
if (n.directory ())
{
n.value = std::move (n.dir).string (); // Move string out of path.
// Add / back to the end of the path unless it is already there.
// Note that the string cannot be empty (n.directory () would
// have been false).
//
if (!dir_path::traits::is_separator (n.value[n.value.size () - 1]))
n.value += '/';
}
if (!n.simple ())
{
n.proj = p; // Restore.
return false;
}
// Convert project qualification to its string representation.
//
if (p != nullptr)
{
string s (*p);
s += '%';
s += n.value;
s.swap (n.value);
}
return true;
}
static bool
string_assign (names& v, const variable& var)
{
// Verify/convert the value is/to a single simple name.
//
if (v.empty ())
{
v.emplace_back (name ()); // Canonical empty string representation.
return false;
}
else if (v.size () == 1)
{
name& n (v.front ());
if (value_traits<string>::assign (n))
return !n.value.empty ();
}
fail << "invalid string variable '" << var.name << "' value '" << v << "'";
return false;
}
static bool
string_append (names& v, names a, const variable& var)
{
// Translate append to string concatenation.
//
string_assign (a, var); // Verify/convert value is/to string.
if (v.front ().value.empty ())
v = move (a);
else
v.front ().value += a.front ().value;
return !v.front ().value.empty ();
}
const value_type value_traits<string>::value_type
{
"string",
&string_assign,
&string_append
};
const value_type* string_type = &value_traits<string>::value_type;
// dir_path value
//
bool value_traits<dir_path>::
assign (name& n)
{
if (n.directory ())
return true;
if (n.simple ())
{
try
{
n.dir = n.empty () ? dir_path () : dir_path (move (n.value));
n.value.clear ();
return true;
}
catch (const invalid_path&) {} // Fall through.
}
return false;
}
static bool
dir_path_assign (names& v, const variable& var)
{
// Verify/convert the value is/to a single directory name.
//
if (v.empty ())
{
v.emplace_back (dir_path ()); // Canonical empty path representation.
return false;
}
else if (v.size () == 1)
{
name& n (v.front ());
if (value_traits<dir_path>::assign (n))
return !n.dir.empty ();
}
fail << "invalid dir_path variable '" << var.name << "' "
<< "value '" << v << "'";
return false;
}
static bool
dir_path_append (names& v, names a, const variable& var)
{
// Translate append to path concatenation.
//
dir_path_assign (a, var); // Verify/convert value is/to dir_path.
dir_path& d (a.front ().dir);
if (d.relative ())
return !(v.front ().dir /= d).empty ();
else
fail << "append of absolute path '" << d << "' to dir_path variable "
<< var.name;
return false;
}
const value_type value_traits<dir_path>::value_type
{
"dir_path",
&dir_path_assign,
&dir_path_append
};
const value_type* dir_path_type = &value_traits<dir_path>::value_type;
// name value
//
static bool
name_assign (names& v, const variable& var)
{
// Verify the value is a single name.
//
if (v.size () == 1)
return v.front ().empty ();
fail << "invalid string variable '" << var.name << "' value '" << v << "'";
return false;
}
static bool
name_append (names&, names, const variable& var)
{
fail << "append to name variable '" << var.name << "'";
return false;
}
const value_type value_traits<name>::value_type
{
"name",
&name_assign,
&name_append
};
const value_type* name_type = &value_traits<name>::value_type;
// vector<T> value
//
const value_type* strings_type = &value_traits<strings>::value_type;
const value_type* dir_paths_type = &value_traits<dir_paths>::value_type;
const value_type* names_type = &value_traits<names>::value_type;
// variable_set
//
variable_set variable_pool;
// variable_type_map
//
lookup<const value> variable_type_map::
lookup (const target_type& type,
const string& name,
const variable& var) const
{
using result = build::lookup<const value>;
// Search across target type hierarchy.
//
for (auto tt (&type); tt != nullptr; tt = tt->base)
{
auto i (find (*tt));
if (i == end ())
continue;
// Try to match the pattern, starting from the longest values
// so that the more "specific" patterns (i.e., those that cover
// fewer characters with the wildcard) take precedence. See
// tests/variable/type-pattern.
//
const variable_pattern_map& m (i->second);
for (auto j (m.rbegin ()); j != m.rend (); ++j)
{
const string& p (j->first);
size_t nn (name.size ());
size_t pn (p.size ());
if (nn < pn - 1) // One for '*'.
continue;
size_t w (p.find ('*'));
assert (w != string::npos);
// Compare prefix.
//
if (w != 0 &&
name.compare (0, w, p, 0, w) != 0)
continue;
++w; // First suffix character.
pn -= w; // Suffix length.
// Compare suffix.
//
if (pn != 0 &&
name.compare (nn - pn, pn, p, w, pn) != 0)
continue;
// Ok, this pattern matches. But is there a variable?
//
if (const value* v = j->second.find (var))
{
//@@ TODO: should we detect ambiguity? 'foo-*' '*-foo' and
// 'foo-foo'? Right now the last defined will be used.
//
return result (v, &j->second);
}
}
}
return result ();
}
}
|