aboutsummaryrefslogtreecommitdiff
path: root/build2/variable
blob: a5f2ea562341f732f06c20abd6dc4c2011e5ee11 (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
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
// file      : build2/variable -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD2_VARIABLE
#define BUILD2_VARIABLE

#include <map>
#include <functional>    // hash
#include <type_traits>   // aligned_storage
#include <unordered_set>

#include <butl/prefix-map>

#include <build2/types>
#include <build2/utility>

#include <build2/target-type>

namespace build2
{
  class value;
  struct variable;
  struct lookup;

  struct value_type
  {
    const char* name;  // Type name for diagnostics.
    const size_t size; // Type size in value::data_ (only used for PODs).

    // Base type, if any. We have very limited support for inheritance: a
    // const value (but not non-const) can be cast to the base type. In
    // particular, a derived/base value cannot be assigned to base/derived.
    // If not NULL, then the cast function below is expected to return the
    // base pointer if its second argument points to the base's value_type.
    //
    const value_type* base;

    // Destroy the value. If it is NULL, then the type is assumed to be POD
    // with a trivial destructor.
    //
    void (*const dtor) (value&);

    // Copy/move constructor and copy/move assignment for data_. If NULL, then
    // assume the stored data is POD. If move is true then the second argument
    // can be const_cast and moved from. copy_assign() is only called with
    // non-NULL first argument.
    //
    void (*const copy_ctor) (value&, const value&, bool move);
    void (*const copy_assign) (value&, const value&, bool move);

    // While assign cannot be NULL, if append or prepend is NULL, then this
    // means this type doesn't support this operation. Variable is optional
    // and is provided only for diagnostics. Return true if the resulting
    // value is not empty.
    //
    bool (*const assign) (value&, names&&, const variable*);
    bool (*const append) (value&, names&&, const variable*);
    bool (*const prepend) (value&, names&&, const variable*);

    // Reverse the value back to a vector of names. Storage can be used by the
    // implementation if necessary. Cannot be NULL.
    //
    names_view (*const reverse) (const value&, names& storage);

    // Cast value::data_ storage to value type so that the result can be
    // static_cast to const T*. If it is NULL, then cast data_ directly. Note
    // that this function is used for both const and non-const values.
    //
    const void* (*const cast) (const value&, const value_type*);

    // If NULL, then the types are compared as PODs using memcmp().
    //
    int (*const compare) (const value&, const value&);
  };

  enum class variable_visibility
  {
    scope,    // This scope (no outer scopes).
    project,  // This project (no outer projects).
    normal    // All outer scopes.
  };

  // variable
  //
  // The two variables are considered the same if they have the same name.
  //
  // If the variable is overridden on the command line, then override is the
  // chain of the special override variables. Their names are derived from the
  // main variable name as <name>.{__override,__prefix,__suffix} and they are
  // not entered into the var_pool. The override variables only vary in their
  // names and visibility.
  //
  // Note also that we don't propagate the variable type to override variables
  // and we keep override values as untyped names. They get "typed" when they
  // are applied.
  //
  // We use the "modify original, override on query" model. Because of that, a
  // modified value does not necessarily represent the actual value so care
  // must be taken to re-query after (direct) modification. And because of
  // that, variables set by the C++ code are by default non-overridable.
  //
  // Initial processing including entering of global overrides happens in
  // reset() before any other variables. Project wide overrides are entered in
  // main(). Overriding happens in scope::find_override().
  //
  struct variable
  {
    string name;
    mutable const value_type* type;        // If NULL, then not (yet) typed.
    mutable unique_ptr<variable> override;
    variable_visibility visibility;
  };

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

  typedef reference_wrapper<const variable> variable_cref;

  // value
  //
  enum class value_state: uint8_t {null, empty, filled};

  class value
  {
  public:
    const value_type* type; // NULL means this value is not (yet) typed.
    value_state state;

    // Extra data that is associated with the value that can be used to store
    // flags, etc. It is initialized to 0 and copied (but not assigned) from
    // one value to another but is otherwise untouched (not even when the
    // value is reset to NULL).
    //
    uint16_t extra;

    bool null () const {return state == value_state::null;}
    bool empty () const {return state == value_state::empty;}

    explicit operator bool () const {return !null ();}
    bool operator== (nullptr_t) const {return null ();}
    bool operator!= (nullptr_t) const {return !null ();}

    // Creation. A default-initialzied value is NULL and can be reset back to
    // NULL by assigning nullptr. Values can be copied and copy-assigned. Note
    // that for assignment, the values' types should be the same or LHS should
    // be untyped.
    //
    //
  public:
    ~value () {*this = nullptr;}

    explicit
    value (const value_type* t = nullptr)
        : type (t), state (value_state::null), extra (0) {}

    explicit
    value (names&&); // Create untyped value.

    // Note: preserves type.
    //
    value&
    operator= (nullptr_t) {if (!null ()) reset (); return *this;}

    value (value&&);
    explicit value (const value&);
    value& operator= (value&&);
    value& operator= (const value&);
    value& operator= (reference_wrapper<value>);
    value& operator= (reference_wrapper<const value>);

    // Assign/Append/Prepend.
    //
  public:
    // Assign/append a typed value. For assign, LHS should be either of the
    // same type or untyped. For append, LHS should be either of the same type
    // or untyped and NULL.
    //
    template <typename T> value& operator= (T);
    template <typename T> value& operator+= (T);

    template <typename T> value& operator+= (T* v) {
      return v != nullptr ? *this += *v : *this;}

    value& operator= (const char* v) {return *this = string (v);}
    value& operator+= (const char* v) {return *this += string (v);}

    // Assign/append/prepend raw data. Variable is optional and is only used
    // for diagnostics.
    //
    void
    assign (names&&, const variable*);

    void
    append (names&&, const variable*);

    void
    prepend (names&&, const variable*);

    // Implementation details, don't use directly except in representation
    // type implementations.
    //
  public:
    // Fast, unchecked cast of data_ to T.
    //
    template<typename T> T& as () & {return reinterpret_cast<T&> (data_);}
    template<typename T> T&& as () && {return reinterpret_cast<T&&> (data_);}
    template<typename T> const T& as () const& {
      return reinterpret_cast<const T&> (data_);}

  public:
    // The maximum size we can store directly in the value is that of a name,
    // which is sufficient for the most commonly used types (string, vector,
    // map) on all the platforms that we support (each type should static
    // assert this in its value_traits specialization below). Types that don't
    // fit will have to be handled with an extra dynamic allocation.
    //
    std::aligned_storage<sizeof (name)>::type data_;
    static const size_t size_ = sizeof (data_);

    // Make sure we have sufficient storage for untyped values.
    //
    static_assert (sizeof (names) <= size_, "insufficient space");

  private:
    void
    reset ();
  };

  // The values should be of the same type (or both be untyped) except NULL
  // values can also be untyped. NULL values compare equal.
  //
  bool operator== (const value&, const value&);
  bool operator!= (const value&, const value&);

  // Value cast. The first three expect the value to be not NULL. The cast
  // from lookup expects the value to aslo be defined.
  //
  // Why are these non-members? The cast is easier on the eyes and is also
  // consistent with the cast operators. The other two are for symmetry.
  //
  //
  template <typename T> T& cast (value&);
  template <typename T> T&& cast (value&&);
  template <typename T> const T& cast (const value&);
  template <typename T> const T& cast (const lookup&);

  // As above but returns NULL if the value is NULL (or not defined, in
  // case of lookup).
  //
  template <typename T> T* cast_null (value&);
  template <typename T> const T* cast_null (const value&);
  template <typename T> const T* cast_null (const lookup&);

  // Assign value type to the value. In the second version the variable is
  // optional and is only used for diagnostics.
  //
  template <typename T>
  void typify (value&, const variable&);
  void typify (value&, const value_type&, const variable*);

  // Reverse the value back to names. The value should no be NULL and storage
  // should be empty.
  //
  names_view
  reverse (const value&, names& storage);

  vector_view<name>
  reverse (value&, names& storage);

  // lookup
  //
  // A variable can be undefined, NULL, or contain a (potentially empty)
  // value.
  //
  struct variable_map;

  struct lookup
  {
    using value_type = build2::value;

    const value_type* 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 ();}

    const value_type& operator*  () const {return *value;}
    const value_type* 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 (in
    // which case it won't belong to either).
    //
    template <typename T>
    bool
    belongs (const T& x) const {return vars == &x.vars;}

    lookup (): value (nullptr), vars (nullptr) {}

    template <typename T>
    lookup (const value_type& v, const T& x): lookup (&v, &x.vars) {}

    lookup (const value_type& v, const variable_map& vs)
        : value (&v), vars (&vs) {}

    lookup (const value_type* v, const variable_map* vs)
        : value (v), vars (v != nullptr ? vs : nullptr) {}
  };

  // Two lookups are equal if they point to the same variable.
  //
  inline bool
  operator== (const lookup& x, const lookup& y)
  {
    bool r (x.value == y.value);
    assert (!r || x.vars == y.vars);
    return r;
  }

  inline bool
  operator!= (const lookup& x, const lookup& y) {return !(x == y);}


  // Representation types.
  //
  // Potential optimizations:
  //
  // - Split value::operator=/+=() into const T and T&&, also overload
  //   value_traits functions that they call.
  //
  // - Specialization for vector<names> (if used and becomes critical).
  //
  //
  template <typename T>
  struct value_traits;
  // {
  //   static_assert (sizeof (T) <= value::size_, "insufficient space");
  //
  //   // Convert name to T. If rhs is not NULL, then it is the second half
  //   // of a pair. Only needs to be provided by simple types. Throw
  //   // invalid_argument (without a message) if the name is not a valid
  //   // representation of value (in which case the name should remain
  //   // unchanged for diagnostics).
  //   //
  //   static T convert (name&&, name* rhs);
  //
  //   // Assign/append/prepend T to value and return true if the result is
  //   // not empty. Value is already of type T but can be NULL.
  //   //
  //   static bool assign (value&, T&&);
  //   static bool append (value&, T&&);
  //   static bool prepend (value&, T&&);
  //
  //   // Reverse a value back to name. Only needs to be provided by simple
  //   // types.
  //   //
  //   static name reverse (const T&);
  //
  //   // Compare two values. Only needs to be provided by simple types.
  //   //
  //   static int compare (const T&, const T&);
  //
  //   static const build2::value_type value_type;
  // };

  // Convert name to a simple value. Throw invalid_argument (without any
  // message) if the name is not a valid representation of value (in which
  // case the name remains unchanged for diagnostics). The second version is
  // called for a pair.
  //
  template <typename T> T convert (name&&);
  template <typename T> T convert (name&&, name&&);

  // Default implementations of the dtor/copy_ctor/copy_assing callbacks for
  // types that are stored directly in value::data_ and the provide all the
  // necessary functions (copy/move ctor and assignment operator).
  //
  template <typename T>
  static void
  default_dtor (value&);

  template <typename T>
  static void
  default_copy_ctor (value&, const value&, bool);

  template <typename T>
  static void
  default_copy_assign (value&, const value&, bool);

  // Default implementations of the assign/append/prepend callbacks for simple
  // types. They call value_traits<T>::convert() and then pass the result to
  // value_traits<T>::assign()/append()/prepend(). As a result, it may not be
  // the most efficient way to do it. If the empty template parameter is true,
  // then an empty names sequence is converted to a default-constructed T. And
  // if false, then an empty value is not allowed.
  //
  template <typename T, bool empty>
  static bool
  simple_assign (value&, names&&, const variable*);

  template <typename T, bool empty>
  static bool
  simple_append (value&, names&&, const variable*);

  template <typename T, bool empty>
  static bool
  simple_prepend (value&, names&&, const variable*);

  // Default implementations of the reverse callback for simple types that
  // calls value_traits<T>::reverse() and adds the result to the vector. As a
  // result, it may not be the most efficient way to do it.
  //
  template <typename T>
  static names_view
  simple_reverse (const value&, names&);

  // Default implementations of the compare callback for simple types that
  // calls value_traits<T>::compare().
  //
  template <typename T>
  static int
  simple_compare (const value&, const value&);

  // bool
  //
  template <>
  struct value_traits<bool>
  {
    static_assert (sizeof (bool) <= value::size_, "insufficient space");

    static bool convert (name&&, name*);
    static bool assign (value&, bool);
    static bool append (value&, bool); // OR.
    static name reverse (bool x) {return name (x ? "true" : "false");}
    static int compare (bool, bool);

    static const build2::value_type value_type;
  };

  template <>
  struct value_traits<uint64_t>
  {
    static_assert (sizeof (uint64_t) <= value::size_, "insufficient space");

    static uint64_t convert (name&&, name*);
    static bool assign (value&, uint64_t);
    static bool append (value&, uint64_t); // ADD.
    static name reverse (uint64_t x) {return name (to_string (x));}
    static int compare (uint64_t, uint64_t);

    static const build2::value_type value_type;
  };

  // string
  //
  template <>
  struct value_traits<string>
  {
    static_assert (sizeof (string) <= value::size_, "insufficient space");

    static string convert (name&&, name*);
    static bool assign (value&, string&&);
    static bool append (value&, string&&);
    static bool prepend (value&, string&&);
    static name reverse (const string& x) {return name (x);}
    static int compare (const string&, const string&);

    static const build2::value_type value_type;
  };

  // path
  //
  template <>
  struct value_traits<path>
  {
    static_assert (sizeof (path) <= value::size_, "insufficient space");

    static path convert (name&&, name*);
    static bool assign (value&, path&&);
    static bool append (value&, path&&);  // operator/
    static bool prepend (value&, path&&); // operator/
    static name reverse (const path& x) {return name (x.string ());}
    static int compare (const path&, const path&);

    static const build2::value_type value_type;
  };

  // dir_path
  //
  template <>
  struct value_traits<dir_path>
  {
    static_assert (sizeof (dir_path) <= value::size_, "insufficient space");

    static dir_path convert (name&&, name*);
    static bool assign (value&, dir_path&&);
    static bool append (value&, dir_path&&);  // operator/
    static bool prepend (value&, dir_path&&); // operator/
    static name reverse (const dir_path& x) {return name (x);}
    static int compare (const dir_path&, const dir_path&);

    static const build2::value_type value_type;
  };

  // abs_dir_path
  //
  template <>
  struct value_traits<abs_dir_path>
  {
    static_assert (sizeof (abs_dir_path) <= value::size_,
                   "insufficient space");

    static abs_dir_path convert (name&&, name*);
    static bool assign (value&, abs_dir_path&&);
    static bool append (value&, abs_dir_path&&);  // operator/
    static name reverse (const abs_dir_path& x) {return name (x);}
    static int compare (const abs_dir_path&, const abs_dir_path&);

    static const build2::value_type value_type;
  };

  // name
  //
  template <>
  struct value_traits<name>
  {
    static_assert (sizeof (name) <= value::size_, "insufficient space");

    static name convert (name&&, name*);
    static bool assign (value&, name&&);
    static bool append (value&, name&&);
    static bool prepend (value&, name&&);
    static name reverse (const name& x) {return x;}
    static int compare (const name&, const name&);

    static const build2::value_type value_type;
  };

  // vector<T>
  //
  template <typename T>
  struct value_traits<vector<T>>
  {
    static_assert (sizeof (vector<T>) <= value::size_, "insufficient space");

    static bool assign (value&, vector<T>&&);
    static bool append (value&, vector<T>&&);
    static bool prepend (value&, vector<T>&&);

    static const string type_name;
    static const build2::value_type value_type;
  };

  // map<K, V>
  //
  template <typename K, typename V>
  struct value_traits<std::map<K, V>>
  {
    template <typename K1, typename V1> using map = std::map<K1, V1>;

    static_assert (sizeof (map<K, V>) <= value::size_, "insufficient space");

    static bool assign (value&, map<K, V>&&);
    static bool append (value&, map<K, V>&&);
    static bool prepend (value& v, map<K, V>&& x) {
      return append (v, move (x));}

    static const string type_name;
    static const build2::value_type value_type;
  };
}

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

namespace butl
{
  template <>
  struct compare_prefix<build2::variable_cref>: compare_prefix<std::string>
  {
    typedef compare_prefix<std::string> base;

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

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

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

namespace build2
{
  // variable_pool
  //
  using variable_pool_base = std::unordered_set<variable>;
  struct variable_pool: private variable_pool_base
  {
    const variable&
    insert (string name,
            bool overridable = false,
            variable_visibility v = variable_visibility::normal)
    {
      return insert (move (name), nullptr, v, overridable);
    }

    template <typename T>
    const variable&
    insert (string name,
            bool overridable = false,
            variable_visibility v = variable_visibility::normal)
    {
      return insert (
        move (name), &value_traits<T>::value_type, v, overridable);
    }

    const variable&
    find (const string& name);

    using variable_pool_base::clear;

  private:
    const variable&
    insert (string name,
            const build2::value_type*,
            variable_visibility,
            bool overridable);
  };

  extern variable_pool var_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>;

    // Lookup. Note that variable overrides will not be applied, even if
    // set in this map.
    //
    lookup
    operator[] (const variable& var) const
    {
      return lookup (find (var), this);
    }

    lookup
    operator[] (const string& name) const
    {
      return operator[] (var_pool.find (name));
    }

    const value*
    find (const variable&) const;

    value*
    find (const variable&);

    // The second member in the pair indicates whether the new value (which
    // will be NULL) was assigned.
    //
    pair<reference_wrapper<value>, bool>
    assign (const variable&);

    pair<reference_wrapper<value>, bool>
    assign (const string& name)
    {
      return assign (var_pool.find (name));
    }

    // Unlike the two above, assign a non-overridable variable with normal
    // visibility.
    //
    template <typename T>
    pair<reference_wrapper<value>, bool>
    assign (string name)
    {
      return assign (var_pool.insert<T> (move (name)));
    }

    pair<const_iterator, const_iterator>
    find_namespace (const string& ns) const
    {
      auto r (m_.find_prefix (var_pool.find (ns)));
      return 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<string, variable_map>;
  using variable_type_map_base = std::map<reference_wrapper<const target_type>,
                                          variable_pattern_map>;

  struct variable_type_map: variable_type_map_base
  {
    lookup
    find (const target_type&, const string& tname, const variable&) const;
  };

  // Override cache.
  //
  struct variable_override_value
  {
    build2::value value;
    const variable_map* stem_vars = nullptr; // NULL means there is no stem.
  };

  extern std::map<pair<const variable_map*, const variable*>,
                  variable_override_value> variable_override_cache;
}

#include <build2/variable.ixx>
#include <build2/variable.txx>

#endif // BUILD2_VARIABLE