aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/function.hxx
blob: 51c17c01ae55f0fcf59598ac00e8e11f80a74a53 (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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// file      : libbuild2/function.hxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBUILD2_FUNCTION_HXX
#define LIBBUILD2_FUNCTION_HXX

#include <map>
#include <utility>       // index_sequence
#include <type_traits>   // aligned_storage

#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/variable.hxx>
#include <libbuild2/diagnostics.hxx>

#include <libbuild2/export.hxx>

namespace build2
{
  // Functions can be overloaded based on types of their arguments but
  // arguments can be untyped and a function can elect to accept an argument
  // of any type.
  //
  // Functions can be qualified (e.g, string.length(), path.directory()) and
  // unqualified (e.g., length(), directory()). Only functions overloaded on
  // static types can be unqualified plus they should also define a qualified
  // alias.
  //
  // Low-level function implementation would be called with a list of values
  // as arguments. There is also higher-level, more convenient support for
  // defining functions as pointers to functions (including capture-less
  // lambdas), pointers to member functions (e.g., string::size()), or
  // pointers to data members (e.g., name::type). In this case the build2
  // function types are automatically matched to C++ function types according
  // to these rules:
  //
  // T           - statically-typed (value_traits<T> must be defined)
  // names       - untyped
  // value       - any type
  // T*          - NULL-able argument (here T can be names)
  // value*      - NULL-able any type (never NULL itself, use value::null)
  // optional<T> - optional argument (here T can be T*, names, value)
  //
  // Optional arguments must be last. In case of a failure the function is
  // expected to issue diagnostics and throw failed. Note that the arguments
  // are conceptually "moved" and can be reused by the implementation.
  //
  // A function can also optionally receive the current scope by having the
  // first argument of the const scope* type. It may be NULL if the function
  // is called out of any scope (e.g., command line).
  //
  // Note also that we don't pass the location to the function instead
  // printing the info message pointing to the call site.
  //
  // A function can return value or anything that can be converted to value.
  // In particular, if a function returns optional<T>, then the result will be
  // either NULL or value of type T.
  //
  // Normally functions come in families that share a common qualification
  // (e.g., string. or path.). The function_family class is a "registrar"
  // that simplifies handling of function families. For example:
  //
  // function_family f ("string");
  //
  // // Register length() and string.length().
  // //
  // f["length"] = &string::size;
  //
  // // Register string.max_size().
  // //
  // f[".max_size"] = []() {return string ().max_size ();};
  //
  // For more examples/ideas, study the existing function families (reside
  // in the functions-*.cxx files).
  //
  // Note that normally there will be a function overload that has all the
  // parameters untyped with an implementation that falls back to one of the
  // overloads that have all the parameters typed, possibly inferring the type
  // from the argument value "syntax" (e.g., presence of a trailing slash for
  // a directory path).
  //
  struct function_overload;

  using function_impl = value (const scope*,
                               vector_view<value>,
                               const function_overload&);

  struct LIBBUILD2_SYMEXPORT function_overload
  {
    const char* name;     // Set to point to key by insert() below.
    const char* alt_name; // Alternative name, NULL if none. This is the
                          // qualified name for unqualified or vice verse.

    // Arguments.
    //
    // A function can have a number of optional arguments. Arguments can also
    // be typed. A non-existent entry in arg_types means a value of any type.
    // A NULL entry means an untyped value.
    //
    // If arg_max equals to arg_variadic, then the function takes an unlimited
    // number of arguments. In this case the semantics of arg_min and
    // arg_types is unchanged.
    //
    static const size_t arg_variadic = size_t (~0);

    using types = vector_view<const optional<const value_type*>>;

    const size_t arg_min;
    const size_t arg_max;
    const types  arg_types;

    // Function implementation.
    //
    function_impl* const impl;

    // Auxiliary data storage. Note that it is assumed to be POD (no
    // destructors, bitwise copy, etc).
    //
    std::aligned_storage<sizeof (void*) * 3>::type data;
    static const size_t data_size = sizeof (decltype (data));

    function_overload (const char* an,
                       size_t mi, size_t ma, types ts,
                       function_impl* im)
        : alt_name (an),
          arg_min (mi), arg_max (ma), arg_types (move (ts)),
          impl (im) {}

    template <typename D>
    function_overload (const char* an,
                       size_t mi, size_t ma, types ts,
                       function_impl* im,
                       D d)
        : function_overload (an, mi, ma, move (ts), im)
    {
      // std::is_pod appears to be broken in VC16 and also in GCC up to
      // 5 (pointers to members).
      //
#if !((defined(_MSC_VER) && _MSC_VER < 2000) || \
      (defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 5))
      static_assert (std::is_pod<D>::value, "type is not POD");
#endif
      static_assert (sizeof (D) <= data_size, "insufficient space");
      new (&data) D (move (d));
    }
  };

  LIBBUILD2_SYMEXPORT ostream&
  operator<< (ostream&, const function_overload&); // Print signature.

  class LIBBUILD2_SYMEXPORT function_map
  {
  public:
    using map_type = std::multimap<string, function_overload>;
    using iterator = map_type::iterator;
    using const_iterator = map_type::const_iterator;

    iterator
    insert (string name, function_overload);

    void
    erase (iterator i) {map_.erase (i);}

    value
    call (const scope* base,
          const string& name,
          vector_view<value> args,
          const location& l) const
    {
      return call (base, name, args, l, true).first;
    }

    // As above but do not fail if no match was found (but still do if the
    // match is ambiguous). Instead return an indication of whether the call
    // was made. Used to issue custom diagnostics when calling internal
    // functions.
    //
    pair<value, bool>
    try_call (const scope* base,
              const string& name,
              vector_view<value> args,
              const location& l) const
    {
      return call (base, name, args, l, false);
    }

    iterator
    begin () {return map_.begin ();}

    iterator
    end () {return map_.end ();}

    const_iterator
    begin () const {return map_.begin ();}

    const_iterator
    end () const {return map_.end ();}

    // Return true if the function with this name is already defined. If the
    // name ends with '.', then instead check if any function with this prefix
    // (which we call a family) is already defined.
    //
    bool
    defined (const string&) const;

  private:
    pair<value, bool>
    call (const scope*,
          const string&,
          vector_view<value>,
          const location&,
          bool fail) const;

    map_type map_;
  };

  LIBBUILD2_SYMEXPORT extern function_map functions;

  class LIBBUILD2_SYMEXPORT function_family
  {
  public:
    // The call() function above catches invalid_argument and issues
    // diagnostics by assuming it is related to function arguments and
    // contains useful description.
    //
    // In order to catch additional exceptions, you can implement a custom
    // thunk which would normally call this default implementation.
    //
    static value
    default_thunk (const scope*, vector_view<value>, const function_overload&);

    // A function family uses a common qualification (though you can pass
    // empty string to supress it). For an unqualified name (doesn't not
    // contain a dot) the qualified version is added automatically. A name
    // containing a leading dot is a shortcut notation for a qualified-only
    // name.
    //
    explicit
    function_family (string qual, function_impl* thunk = &default_thunk)
        : qual_ (qual), thunk_ (thunk) {}

    struct entry;

    entry
    operator[] (string name) const;

    static bool
    defined (string qual)
    {
      qual += '.';
      return functions.defined (qual);
    }

  private:
    const string qual_;
    function_impl* thunk_;
  };

  // Implementation details. If you can understand and explain all of this,
  // then you are hired ;-)!
  //

  template <typename T>
  struct function_arg
  {
    static const bool null = false;
    static const bool opt = false;

    static constexpr optional<const value_type*>
    type () {return &value_traits<T>::value_type;}

    static T&&
    cast (value* v)
    {
      if (v->null)
        throw invalid_argument ("null value");

      // Use fast but unchecked cast since the caller matched the types.
      //
      return move (v->as<T> ());
    }
  };

  template <>
  struct LIBBUILD2_SYMEXPORT function_arg<names> // Untyped.
  {
    static const bool null = false;
    static const bool opt = false;

    static constexpr optional<const value_type*>
    type () {return nullptr;}

    static names&&
    cast (value* v)
    {
      if (v->null)
        throw invalid_argument ("null value");

      return move (v->as<names> ());
    }
  };

  template <>
  struct LIBBUILD2_SYMEXPORT function_arg<value> // Anytyped.
  {
    static const bool null = false;
    static const bool opt = false;

    static constexpr optional<const value_type*>
    type () {return nullopt;}

    static value&&
    cast (value* v)
    {
      if (v->null)
        throw invalid_argument ("null value");

      return move (*v);
    }
  };

  template <typename T>
  struct function_arg<T*>: function_arg<T>
  {
    static const bool null = true;

    static T*
    cast (value* v)
    {
      if (v->null)
        return nullptr;

      // This looks bizarre but makes sense. The cast() that we are calling
      // returns an r-value reference to (what's inside) v. And it has to
      // return an r-value reference to that the value is moved into by-value
      // arguments.
      //
      T&& r (function_arg<T>::cast (v));
      return &r;
    }
  };

  template <>
  struct LIBBUILD2_SYMEXPORT function_arg<value*>: function_arg<value>
  {
    static const bool null = true;

    static value*
    cast (value* v) {return v;} // NULL indicator in value::null.
  };

  template <typename T>
  struct function_arg<optional<T>>: function_arg<T>
  {
    static const bool opt = true;

    static optional<T>
    cast (value* v)
    {
      return v != nullptr ? optional<T> (function_arg<T>::cast (v)) : nullopt;
    }
  };

  // Number of optional arguments. Note that we currently don't check that
  // they are all at the end.
  //
  template <typename A0, typename... A>
  struct function_args_opt
  {
    static const size_t count = (function_arg<A0>::opt ? 1 : 0) +
      function_args_opt<A...>::count;
  };

  template <typename A0>
  struct function_args_opt<A0>
  {
    static const size_t count = (function_arg<A0>::opt ? 1 : 0);
  };

  // Argument counts/types.
  //
  template <typename... A>
  struct function_args
  {
    static const size_t max = sizeof...(A);
    static const size_t min = max - function_args_opt<A...>::count;

    // VC15 doesn't realize that a pointer to static object (in our case it is
    // &value_trair<T>::value_type) is constexpr.
    //
    // Note that during the library split we discovered that the constexpr
    // variant causes compilation/linkage issues for both MinGW GCC and
    // VC. Thus we now only use it for POSIX systems.
    //
    // #if !defined(_MSC_VER) || _MSC_VER > 1910
    //
#if !defined(_WIN32)
    static constexpr const optional<const value_type*> types[max] = {
      function_arg<A>::type ()...};
#else
    static const optional<const value_type*> types[max];
#endif
  };

  template <typename... A>
#if !defined(_WIN32)
  constexpr const optional<const value_type*>
  function_args<A...>::types[function_args<A...>::max];
#else
  const optional<const value_type*>
  function_args<A...>::types[function_args<A...>::max] = {
    function_arg<A>::type ()...};
#endif

  // Specialization for no arguments.
  //
  template <>
  struct LIBBUILD2_SYMEXPORT function_args<>
  {
    static const size_t max = 0;
    static const size_t min = 0;

#if !defined(_WIN32)
    static constexpr const optional<const value_type*>* types = nullptr;
#else
    static const optional<const value_type*>* const types;
#endif
  };

  // Cast data/thunk for functions.
  //
  template <typename R, typename... A>
  struct function_cast_func
  {
    // A pointer to a standard layout struct is a pointer to its first data
    // member, which in our case is the cast thunk.
    //
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      R (*const impl) (A...);
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      return thunk (move (args),
                    static_cast<const data*> (d)->impl,
                    std::index_sequence_for<A...> ());
    }

    template <size_t... i>
    static value
    thunk (vector_view<value> args,
           R (*impl) (A...),
           std::index_sequence<i...>)
    {
      return value (
        impl (
          function_arg<A>::cast (
            i < args.size () ? &args[i] : nullptr)...));
    }
  };

  // Specialization for functions that expect the current scope as a first
  // argument.
  //
  template <typename R, typename... A>
  struct function_cast_func<R, const scope*, A...>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      R (*const impl) (const scope*, A...);
    };

    static value
    thunk (const scope* base, vector_view<value> args, const void* d)
    {
      return thunk (base, move (args),
                    static_cast<const data*> (d)->impl,
                    std::index_sequence_for<A...> ());
    }

    template <size_t... i>
    static value
    thunk (const scope* base, vector_view<value> args,
           R (*impl) (const scope*, A...),
           std::index_sequence<i...>)
    {
      return value (
        impl (base,
              function_arg<A>::cast (
                i < args.size () ? &args[i] : nullptr)...));
    }
  };

  // Specialization for void return type. In this case we return NULL value.
  //
  template <typename... A>
  struct function_cast_func<void, A...>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      void (*const impl) (A...);
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      thunk (move (args),
             static_cast<const data*> (d)->impl,
             std::index_sequence_for<A...> ());
      return value (nullptr);
    }

    template <size_t... i>
    static void
    thunk (vector_view<value> args,
           void (*impl) (A...),
           std::index_sequence<i...>)
    {
      impl (function_arg<A>::cast (i < args.size () ? &args[i] : nullptr)...);
    }
  };

  template <typename... A>
  struct function_cast_func<void, const scope*, A...>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      void (*const impl) (const scope*, A...);
    };

    static value
    thunk (const scope* base, vector_view<value> args, const void* d)
    {
      thunk (base, move (args),
             static_cast<const data*> (d)->impl,
             std::index_sequence_for<A...> ());
      return value (nullptr);
    }

    template <size_t... i>
    static void
    thunk (const scope* base, vector_view<value> args,
           void (*impl) (const scope*, A...),
           std::index_sequence<i...>)
    {
      impl (base,
            function_arg<A>::cast (i < args.size () ? &args[i] : nullptr)...);
    }
  };

  // Customization for coerced lambdas (see below).
  //
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 6
  template <typename L, typename R, typename... A>
  struct function_cast_lamb
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      R (L::*const impl) (A...) const;
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      return thunk (move (args),
                    static_cast<const data*> (d)->impl,
                    std::index_sequence_for<A...> ());
    }

    template <size_t... i>
    static value
    thunk (vector_view<value> args,
           R (L::*impl) (A...) const,
           std::index_sequence<i...>)
    {
      const L* l (nullptr); // Undefined behavior.

      return value (
        (l->*impl) (
          function_arg<A>::cast (
            i < args.size () ? &args[i] : nullptr)...));
    }
  };

  template <typename L, typename R, typename... A>
  struct function_cast_lamb<L, R, const scope*, A...>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      R (L::*const impl) (const scope*, A...) const;
    };

    static value
    thunk (const scope* base, vector_view<value> args, const void* d)
    {
      return thunk (base, move (args),
                    static_cast<const data*> (d)->impl,
                    std::index_sequence_for<A...> ());
    }

    template <size_t... i>
    static value
    thunk (const scope* base, vector_view<value> args,
           R (L::*impl) (const scope*, A...) const,
           std::index_sequence<i...>)
    {
      const L* l (nullptr); // Undefined behavior.

      return value (
        (l->*impl) (base,
                    function_arg<A>::cast (
                      i < args.size () ? &args[i] : nullptr)...));
    }
  };

  template <typename L, typename... A>
  struct function_cast_lamb<L, void, A...>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      void (L::*const impl) (A...) const;
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      thunk (move (args),
             static_cast<const data*> (d)->impl,
             std::index_sequence_for<A...> ());
      return value (nullptr);
    }

    template <size_t... i>
    static void
    thunk (vector_view<value> args,
           void (L::*impl) (A...) const,
           std::index_sequence<i...>)
    {
      const L* l (nullptr);
      (l->*impl) (
        function_arg<A>::cast (
          i < args.size () ? &args[i] : nullptr)...);
    }
  };

  template <typename L, typename... A>
  struct function_cast_lamb<L, void, const scope*, A...>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      void (L::*const impl) (const scope*, A...) const;
    };

    static value
    thunk (const scope* base, vector_view<value> args, const void* d)
    {
      thunk (base, move (args),
             static_cast<const data*> (d)->impl,
             std::index_sequence_for<A...> ());
      return value (nullptr);
    }

    template <size_t... i>
    static void
    thunk (const scope* base, vector_view<value> args,
           void (L::*impl) (const scope*, A...) const,
           std::index_sequence<i...>)
    {
      const L* l (nullptr);
      (l->*impl) (base,
                  function_arg<A>::cast (
                    i < args.size () ? &args[i] : nullptr)...);
    }
  };
#endif

  // Customization for member functions.
  //
  template <typename R, typename T>
  struct function_cast_memf
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      R (T::*const impl) () const;
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      auto mf (static_cast<const data*> (d)->impl);
      return value ((function_arg<T>::cast (&args[0]).*mf) ());
    }
  };

  template <typename T>
  struct function_cast_memf<void, T>
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      void (T::*const impl) () const;
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      auto mf (static_cast<const data*> (d)->impl);
      (function_arg<T>::cast (args[0]).*mf) ();
      return value (nullptr);
    }
  };

  // Customization for data members.
  //
  template <typename R, typename T>
  struct function_cast_memd
  {
    struct data
    {
      value (*const thunk) (const scope*, vector_view<value>, const void*);
      R T::*const impl;
    };

    static value
    thunk (const scope*, vector_view<value> args, const void* d)
    {
      auto dm (static_cast<const data*> (d)->impl);
      return value (move (function_arg<T>::cast (&args[0]).*dm));
    }
  };

  struct LIBBUILD2_SYMEXPORT function_family::entry
  {
    string name;
    const string& qual;
    function_impl* thunk;

    template <typename R, typename... A>
    void
    operator= (R (*impl) (A...)) &&
    {
      using args = function_args<A...>;
      using cast = function_cast_func<R, A...>;

      insert (move (name),
              function_overload (
                nullptr,
                args::min,
                args::max,
                function_overload::types (args::types, args::max),
                thunk,
                typename cast::data {&cast::thunk, impl}));
    }

    template <typename R, typename... A>
    void
    operator= (R (*impl) (const scope*, A...)) &&
    {
      using args = function_args<A...>;
      using cast = function_cast_func<R, const scope*, A...>;

      insert (move (name),
              function_overload (
                nullptr,
                args::min,
                args::max,
                function_overload::types (args::types, args::max),
                thunk,
                typename cast::data {&cast::thunk, impl}));
    }

    // Support for assigning a (capture-less) lambda.
    //
    // GCC up until version 6 has a bug (#62052) that is triggered by calling
    // a lambda that takes a by-value argument via its "decayed" function
    // pointer. To work around this we are not going to decay it and instead
    // will call its operator() on NULL pointer; yes, undefined behavior, but
    // better than a guaranteed crash.
    //
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 6
    template <typename L>
    void
    operator= (const L&) &&
    {
      move (*this).coerce_lambda (&L::operator());
    }

    template <typename L, typename R, typename... A>
    void
    coerce_lambda (R (L::*op) (A...) const) &&
    {
      using args = function_args<A...>;
      using cast = function_cast_lamb<L, R, A...>;

      insert (move (name),
              function_overload (
                nullptr,
                args::min,
                args::max,
                function_overload::types (args::types, args::max),
                thunk,
                typename cast::data {&cast::thunk, op}));
    }

    template <typename L, typename R, typename... A>
    void
    coerce_lambda (R (L::*op) (const scope*, A...) const) &&
    {
      using args = function_args<A...>;
      using cast = function_cast_lamb<L, R, const scope*, A...>;

      insert (move (name),
              function_overload (
                nullptr,
                args::min,
                args::max,
                function_overload::types (args::types, args::max),
                thunk,
                typename cast::data {&cast::thunk, op}));
    }
#else
    template <typename L>
    void
    operator= (const L& l) &&
    {
      move (*this).operator= (decay_lambda (&L::operator(), l));
    }

    template <typename L, typename R, typename... A>
    static auto
    decay_lambda (R (L::*) (A...) const, const L& l) -> R (*) (A...)
    {
      return static_cast<R (*) (A...)> (l);
    }
#endif

    // Support for assigning a pointer to member function (e.g. an accessor).
    //
    // For now we don't support passing additional (to this) arguments though
    // we could probably do that. The issues would be the argument passing
    // semantics (e.g., what if it's const&) and the optional/default argument
    // handling.
    //
    template <typename R, typename T>
    void
    operator= (R (T::*mf) () const) &&
    {
      using args = function_args<T>;
      using cast = function_cast_memf<R, T>;

      insert (move (name),
              function_overload (
                nullptr,
                args::min,
                args::max,
                function_overload::types (args::types, args::max),
                thunk,
                typename cast::data {&cast::thunk, mf}));
    }

    // Support for assigning a pointer to data member.
    //
    template <typename R, typename T>
    void
    operator= (R T::*dm) &&
    {
      using args = function_args<T>;
      using cast = function_cast_memd<R, T>;

      insert (move (name),
              function_overload (
                nullptr,
                args::min,
                args::max,
                function_overload::types (args::types, args::max),
                thunk,
                typename cast::data {&cast::thunk, dm}));
    }

  private:
    void
    insert (string, function_overload) const;
  };

  inline auto function_family::
  operator[] (string name) const -> entry
  {
    return entry {move (name), qual_, thunk_};
  }
}

#endif // LIBBUILD2_FUNCTION_HXX