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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
// file : build/variable -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#ifndef BUILD_VARIABLE
#define BUILD_VARIABLE
#include <map>
#include <vector>
#include <string>
#include <cstddef> // nullptr_t
#include <utility> // move(), pair, make_pair()
#include <cassert>
#include <iterator>
#include <functional> // hash, reference_wrapper
#include <type_traits> // conditional, is_reference, remove_reference, etc.
#include <unordered_set>
#include <butl/prefix-map>
#include <build/types>
#include <build/target-type>
namespace build
{
struct variable;
// If assign is NULL, then the value is assigned as is. If append
// is NULL, then the names are appended to the end of the value
// and assign is called, if not NULL. Variable is provided primarily
// for diagnostics. Return true if the resulting value is not empty.
//
struct value_type
{
const char* name;
bool (*const assign) (names&, const variable&);
bool (*const append) (names&, names, const variable&);
};
// variable
//
// The two variables are considered the same if they have the same name.
//
struct variable
{
explicit
variable (std::string n, const value_type* t = nullptr, char p = '\0')
: name (std::move (n)), pairs (p), type (t) {}
std::string name;
char pairs;
const value_type* type; // If NULL, then not (yet) typed.
};
inline bool
operator== (const variable& x, const variable& y) {return x.name == y.name;}
typedef std::reference_wrapper<const variable> variable_cref;
// value
//
class value
{
public:
// By default we create NULL value.
//
explicit value (const value_type* t = nullptr)
: type (t), state_ (state_type::null) {}
value (value&&) = default;
value&
operator= (std::nullptr_t)
{
data_.clear ();
state_ = state_type::null;
return *this;
}
value&
operator= (value&&);
value&
operator= (const value& v)
{
if (&v != this)
*this = value (v);
return *this;
}
value&
operator= (std::reference_wrapper<const value> v)
{
return *this = v.get ();
}
value&
append (value, const variable&); // Aka operator+=().
// Forwarded to the representation type's assign()/append() (see below).
//
template <typename T> value& operator= (T);
value& operator= (const char* v) {return *this = std::string (v);}
template <typename T> value& operator+= (T);
value& operator+= (const char* v) {return *this += std::string (v);}
private:
explicit value (const value&) = default;
public:
const value_type* type; // NULL means this value is not (yet) typed.
bool null () const {return state_ == state_type::null;}
bool empty () const {return state_ == state_type::empty;}
explicit operator bool () const {return !null ();}
// Raw data read interface.
//
using const_iterator = names::const_iterator;
const_iterator begin () const {return data_.begin ();}
const_iterator end () const {return data_.end ();}
// Raw data write interface. Note that it triggers callbacks for
// typed values. Variable is passed for diagnostics.
//
void
assign (names, const variable&);
void
append (names, const variable&);
public:
// Don't use directly except in the implementation of representation
// types, in which case take care to update state.
//
enum class state_type {null, empty, filled} state_;
names data_;
};
//@@ Right now we assume that for each value type each value has a
// unique representation. This is currently not the case for map.
//
inline bool
operator== (const value& x, const value& y)
{
return x.state_ == y.state_ && x.data_ == y.data_;
}
inline bool
operator!= (const value& x, const value& y) {return !(x == y);}
// lookup
//
// A variable can be undefined, NULL, or contain a (potentially
// empty) value.
//
struct variable_map;
template <typename V>
struct lookup
{
V* value;
const variable_map* vars;
bool
defined () const {return value != nullptr;}
// Note: returns true if defined and not NULL.
//
explicit operator bool () const {return defined () && !value->null ();}
V& operator* () const {return *value;}
V* operator-> () const {return value;}
// Return true if this value belongs to the specified scope or target.
// Note that it can also be a target type/pattern-specific value.
//
template <typename T>
bool
belongs (const T& x) const {return vars == &x.vars;}
lookup (): value (nullptr), vars (nullptr) {}
lookup (V* v, const variable_map* vs)
: value (v), vars (v != nullptr ? vs : nullptr) {}
template <typename T>
lookup (V& v, const T& x): lookup (&v, &x.vars) {}
};
// Representation types.
//
template <typename T> struct value_traits;
// Assign value type to the value.
//
template <typename T>
void assign (value&, const variable&);
void assign (value&, const value_type*, const variable&);
template <typename T> typename value_traits<T>::type as (value&);
template <typename T> typename value_traits<T>::const_type as (const value&);
// "Assign" simple value type to the value stored in name. Return false
// if the value is not valid for this type.
//
template <typename T> bool assign (name&);
template <typename T> typename value_traits<T>::type as (name&);
template <typename T> typename value_traits<T>::const_type as (const name&);
// bool
//
template <typename D>
struct bool_value
{
explicit
operator bool () const {return d->value[0] == 't';}
bool_value&
operator= (bool v)
{
d->value = v ? "true" : "false";
return *this;
}
bool_value&
operator+= (bool v)
{
if (!*this && v)
d->value = "true";
return *this;
}
// Implementation details.
//
public:
explicit bool_value (D& d): d (&d) {}
bool_value (const bool_value&) = delete;
bool_value& operator= (const bool_value&) = delete; // Rebind or deep?
bool_value (bool_value&&) = default;
bool_value& operator= (bool_value&&) = delete;
D* d; // name
};
template <>
struct value_traits<bool>
{
using type = bool_value<name>;
using const_type = bool_value<const name>;
static type as (name& n) {return type (n);}
static const_type as (const name& n) {return const_type (n);}
static type as (value&);
static const_type as (const value&);
static bool assign (name&);
static void assign (value&, bool);
static void append (value&, bool);
static const build::value_type value_type;
};
extern const value_type* bool_type;
// string
//
template <>
struct value_traits<std::string>
{
using type = std::string&;
using const_type = const std::string&;
static type as (name& n) {return n.value;}
static const_type as (const name& n) {return n.value;}
static type as (value&);
static const_type as (const value&);
static bool assign (name&);
static void assign (value&, std::string);
static void append (value&, std::string);
static const build::value_type value_type;
};
extern const value_type* string_type;
// dir_path
//
template <>
struct value_traits<dir_path>
{
using type = dir_path&;
using const_type = const dir_path&;
static type as (name& n) {return n.dir;}
static const_type as (const name& n) {return n.dir;}
static type as (value&);
static const_type as (const value&);
static bool assign (name&);
static void assign (value&, dir_path);
static void append (value&, dir_path);
static const build::value_type value_type;
};
extern const value_type* dir_path_type;
// name
//
template <>
struct value_traits<name>
{
using type = name&;
using const_type = const name&;
static type as (name& n) {return n;}
static const_type as (const name& n) {return n;}
static type as (value&);
static const_type as (const value&);
static bool assign (name&) {return true;}
static void assign (value&, name);
static void append (value&, name) = delete;
static const build::value_type value_type;
};
extern const value_type* name_type;
// vector<T>
//
template <typename T, typename D>
struct vector_value
{
using size_type = typename D::size_type;
using value_type = typename value_traits<T>::type;
using const_value_type = typename value_traits<T>::const_type;
template <typename I, typename V, typename R>
struct iterator_impl: I
{
using value_type = V;
using pointer = value_type*;
using reference = R;
using difference_type = typename I::difference_type;
iterator_impl () = default;
iterator_impl (const I& i): I (i) {}
// Note: operator->() is unavailable if R is a value.
//
reference operator* () const {return as<T> (I::operator* ());}
pointer operator-> () const {return &as<T> (I::operator* ());}
reference operator[] (difference_type n) const
{
return as<T> (I::operator[] (n));
}
};
// Make iterator the same as const_iterator if our data type is const.
//
using const_iterator =
iterator_impl<names::const_iterator, const T, const_value_type>;
using iterator = typename std::conditional<
std::is_const<D>::value,
const_iterator,
iterator_impl<names::iterator, T, value_type>>::type;
public:
vector_value&
operator= (std::vector<T> v) {assign (std::move (v)); return *this;}
vector_value&
assign (std::vector<T>);
template <typename D1>
vector_value&
assign (const vector_value<T, D1>&);
template <typename D1>
vector_value&
append (const vector_value<T, D1>&);
public:
bool empty () const {return d->empty ();}
size_type size () const {return d->size ();}
value_type operator[] (size_type i) {return as<T> ((*d)[i]);}
const_value_type operator[] (size_type i) const {return as<T> ((*d)[i]);}
iterator begin () {return iterator (d->begin ());}
iterator end () {return iterator (d->end ());}
const_iterator begin () const {return const_iterator (d->begin ());}
const_iterator end () const {return const_iterator (d->end ());}
const_iterator cbegin () const {return const_iterator (d->begin ());}
const_iterator cend () const {return const_iterator (d->end ());}
// Implementation details.
//
public:
explicit vector_value (D& d): d (&d) {}
vector_value (const vector_value&) = delete;
vector_value& operator= (const vector_value&) = delete; // Rebind or deep?
vector_value (vector_value&&) = default;
vector_value& operator= (vector_value&&) = default; //@@ TMP
explicit vector_value (std::nullptr_t): d (nullptr) {} //@@ TMP
D* d; // names
};
template <typename T>
struct value_traits<std::vector<T>>
{
using type = vector_value<T, names>;
using const_type = vector_value<T, const names>;
static type as (value&);
static const_type as (const value&);
template <typename V> static void assign (value&, V);
template <typename V> static void append (value&, V);
static const std::string type_name;
static const build::value_type value_type;
};
template <typename T, typename D>
struct value_traits<vector_value<T, D>>: value_traits<std::vector<T>> {};
using strings_value = vector_value<std::string, names>;
using const_strings_value = vector_value<std::string, const names>;
extern const value_type* strings_type; // vector<string> aka strings
extern const value_type* dir_paths_type; // vector<dir_path> aka dir_paths
extern const value_type* names_type; // vector<name> aka names
// map<K, V>
//
template <typename K, typename V, typename D>
struct map_value
{
template <typename F, typename S>
struct pair
{
using first_type = typename std::conditional<
std::is_reference<F>::value,
std::reference_wrapper<typename std::remove_reference<F>::type>,
F>::type;
using second_type = typename std::conditional<
std::is_reference<S>::value,
std::reference_wrapper<typename std::remove_reference<S>::type>,
S>::type;
first_type first;
second_type second;
};
template <typename I, typename T>
struct iterator_impl
{
using value_type = T;
using pointer = value_type*;
using reference = value_type; // Note: value.
using difference_type = typename I::difference_type;
using iterator_category = std::bidirectional_iterator_tag;
iterator_impl () = default;
iterator_impl (const I& i): i_ (i) {}
pointer operator-> () const = delete;
reference operator* () const
{
return value_type {as<K> (*i_), as<V> (*(i_ + 1))};
}
iterator_impl& operator++ () {i_ += 2; return *this;}
iterator_impl operator++ (int) {auto r (*this); operator++ (); return r;}
iterator_impl& operator-- () {i_ -= 2; return *this;}
iterator_impl operator-- (int) {auto r (*this); operator-- (); return r;}
bool operator== (const iterator_impl& y) const {return i_ == y.i_;}
bool operator!= (const iterator_impl& y) const {return i_ != y.i_;}
private:
I i_;
};
using size_type = typename D::size_type;
using value_type = pair<typename value_traits<K>::const_type,
typename value_traits<V>::type>;
using const_value_type = pair<typename value_traits<K>::const_type,
typename value_traits<V>::const_type>;
// Make iterator the same as const_iterator if our data type is const.
//
using const_iterator =
iterator_impl<names::const_iterator, const_value_type>;
using iterator = typename std::conditional<
std::is_const<D>::value,
const_iterator,
iterator_impl<names::iterator, value_type>>::type;
public:
map_value&
operator= (std::map<K, V> m) {assign (std::move (m)); return *this;}
map_value&
assign (std::map<K, V>);
bool empty () const {return d->empty ();}
size_type size () const {return d->size ();}
iterator find (const K&);
const_iterator find (const K&) const;
iterator begin () {return iterator (d->begin ());}
iterator end () {return iterator (d->end ());}
const_iterator begin () const {return const_iterator (d->begin ());}
const_iterator end () const {return const_iterator (d->end ());}
// Implementation details.
//
public:
explicit map_value (D& d): d (&d) {}
map_value (const map_value&) = delete;
map_value& operator= (const map_value&) = delete; // Rebind or deep?
map_value (map_value&&) = default;
map_value& operator= (map_value&&) = delete;
D* d; // names
};
template <typename K, typename V>
struct value_traits<std::map<K, V>>
{
using type = map_value<K, V, names>;
using const_type = map_value<K, V, const names>;
static type as (value&);
static const_type as (const value&);
template <typename M> static void assign (value&, M);
template <typename M> static void append (value&, M);
static const std::string type_name;
static const build::value_type value_type;
};
template <typename K, typename V, typename D>
struct value_traits<map_value<K, V, D>>: value_traits<std::map<K, V>> {};
}
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 butl
{
template <>
struct compare_prefix<build::variable_cref>: compare_prefix<std::string>
{
typedef compare_prefix<std::string> base;
explicit
compare_prefix (char d): base (d) {}
bool
operator() (const build::variable& x, const build::variable& y) const
{
return base::operator() (x.name, y.name);
}
bool
prefix (const build::variable& p, const build::variable& k) const
{
return base::prefix (p.name, k.name);
}
};
}
namespace build
{
// variable_pool
//
using variable_set_base = std::unordered_set<variable>;
struct variable_set: private variable_set_base
{
const variable&
find (std::string name, const build::value_type* t = nullptr, char p = '\0')
{
auto r (emplace (std::move (name), t, p));
const variable& v (*r.first);
// Update type?
//
if (!r.second && t != nullptr && v.type != t)
{
assert (v.type == nullptr);
const_cast<variable&> (v).type = t; // Ok, not changing the key.
}
return v;
}
using variable_set_base::clear;
};
extern variable_set variable_pool;
// variable_map
//
struct variable_map
{
using map_type = butl::prefix_map<variable_cref, value, '.'>;
using size_type = map_type::size_type;
template <typename I>
struct iterator_adapter: I
{
iterator_adapter () = default;
iterator_adapter (const I& i): I (i) {}
typename I::reference operator* () const;
typename I::pointer operator-> () const;
};
using const_iterator = iterator_adapter<map_type::const_iterator>;
const value*
find (const variable& var) const
{
auto i (m_.find (var));
const value* r (i != m_.end () ? &i->second : nullptr);
// First access after being assigned a type?
//
if (r != nullptr && var.type != nullptr && r->type != var.type)
build::assign (const_cast<value&> (*r), var.type, var);
return r;
}
value*
find (const variable& var)
{
auto i (m_.find (var));
value* r (i != m_.end () ? &i->second : nullptr);
// First access after being assigned a type?
//
if (r != nullptr && var.type != nullptr && r->type != var.type)
build::assign (*r, var.type, var);
return r;
}
lookup<const value>
operator[] (const variable& var) const
{
return lookup<const value> (find (var), this);
}
lookup<const value>
operator[] (const std::string& name) const
{
return operator[] (variable_pool.find (name));
}
// Non-const lookup. Only exposed on the map directly.
//
lookup<value>
operator[] (const variable& var)
{
return lookup<value> (find (var), this);
}
lookup<value>
operator[] (const std::string& name)
{
return operator[] (variable_pool.find (name));
}
// The second member in the pair indicates whether the new
// value (which will be NULL) was assigned.
//
std::pair<std::reference_wrapper<value>, bool>
assign (const variable& var)
{
auto r (m_.emplace (var, value (var.type)));
value& v (r.first->second);
// First access after being assigned a type?
//
if (!r.second && var.type != nullptr && v.type != var.type)
build::assign (v, var.type, var);
return std::make_pair (std::reference_wrapper<value> (v), r.second);
}
std::pair<std::reference_wrapper<value>, bool>
assign (const std::string& name)
{
return assign (variable_pool.find (name));
}
std::pair<const_iterator, const_iterator>
find_namespace (const std::string& ns) const
{
auto r (m_.find_prefix (variable_pool.find (ns)));
return std::make_pair (const_iterator (r.first),
const_iterator (r.second));
}
const_iterator
begin () const {return m_.begin ();}
const_iterator
end () const {return m_.end ();}
bool
empty () const {return m_.empty ();}
size_type
size () const {return m_.size ();}
private:
map_type m_;
};
// Target type/pattern-specific variables.
//
// @@ In quite a few places we assume that we can store a reference
// to the returned value (e.g., install::lookup_install()). If
// we "instantiate" the value on the fly, then we will need to
// consider its lifetime.
//
using variable_pattern_map = std::map<std::string, variable_map>;
struct variable_type_map: std::map<std::reference_wrapper<const target_type>,
variable_pattern_map>
{
build::lookup<const value>
lookup (const target_type&, const string& name, const variable&) const;
};
}
#include <build/variable.ixx>
#include <build/variable.txx>
#endif // BUILD_VARIABLE
|