1 //===- llvm/ADT/ilist_node_options.h - ilist_node Options -------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_ADT_ILIST_NODE_OPTIONS_H 11 #define LLVM_ADT_ILIST_NODE_OPTIONS_H 12 13 #include "llvm/Config/abi-breaking.h" 14 15 #include <type_traits> 16 17 namespace llvm { 18 19 template <bool EnableSentinelTracking> class ilist_node_base; 20 template <bool EnableSentinelTracking> class ilist_base; 21 22 /// Option to choose whether to track sentinels. 23 /// 24 /// This option affects the ABI for the nodes. When not specified explicitly, 25 /// the ABI depends on LLVM_ENABLE_ABI_BREAKING_CHECKS. Specify explicitly to 26 /// enable \a ilist_node::isSentinel(). 27 template <bool EnableSentinelTracking> struct ilist_sentinel_tracking {}; 28 29 /// Option to specify a tag for the node type. 30 /// 31 /// This option allows a single value type to be inserted in multiple lists 32 /// simultaneously. See \a ilist_node for usage examples. 33 template <class Tag> struct ilist_tag {}; 34 35 namespace ilist_detail { 36 37 /// Helper trait for recording whether an option is specified explicitly. 38 template <bool IsExplicit> struct explicitness { 39 static const bool is_explicit = IsExplicit; 40 }; 41 typedef explicitness<true> is_explicit; 42 typedef explicitness<false> is_implicit; 43 44 /// Check whether an option is valid. 45 /// 46 /// The steps for adding and enabling a new ilist option include: 47 /// \li define the option, ilist_foo<Bar>, above; 48 /// \li add new parameters for Bar to \a ilist_detail::node_options; 49 /// \li add an extraction meta-function, ilist_detail::extract_foo; 50 /// \li call extract_foo from \a ilist_detail::compute_node_options and pass it 51 /// into \a ilist_detail::node_options; and 52 /// \li specialize \c is_valid_option<ilist_foo<Bar>> to inherit from \c 53 /// std::true_type to get static assertions passing in \a simple_ilist and \a 54 /// ilist_node. 55 template <class Option> struct is_valid_option : std::false_type {}; 56 57 /// Extract sentinel tracking option. 58 /// 59 /// Look through \p Options for the \a ilist_sentinel_tracking option, with the 60 /// default depending on LLVM_ENABLE_ABI_BREAKING_CHECKS. 61 template <class... Options> struct extract_sentinel_tracking; 62 template <bool EnableSentinelTracking, class... Options> 63 struct extract_sentinel_tracking< 64 ilist_sentinel_tracking<EnableSentinelTracking>, Options...> 65 : std::integral_constant<bool, EnableSentinelTracking>, is_explicit {}; 66 template <class Option1, class... Options> 67 struct extract_sentinel_tracking<Option1, Options...> 68 : extract_sentinel_tracking<Options...> {}; 69 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 70 template <> struct extract_sentinel_tracking<> : std::true_type, is_implicit {}; 71 #else 72 template <> 73 struct extract_sentinel_tracking<> : std::false_type, is_implicit {}; 74 #endif 75 template <bool EnableSentinelTracking> 76 struct is_valid_option<ilist_sentinel_tracking<EnableSentinelTracking>> 77 : std::true_type {}; 78 79 /// Extract custom tag option. 80 /// 81 /// Look through \p Options for the \a ilist_tag option, pulling out the 82 /// custom tag type, using void as a default. 83 template <class... Options> struct extract_tag; 84 template <class Tag, class... Options> 85 struct extract_tag<ilist_tag<Tag>, Options...> { 86 typedef Tag type; 87 }; 88 template <class Option1, class... Options> 89 struct extract_tag<Option1, Options...> : extract_tag<Options...> {}; 90 template <> struct extract_tag<> { typedef void type; }; 91 template <class Tag> struct is_valid_option<ilist_tag<Tag>> : std::true_type {}; 92 93 /// Check whether options are valid. 94 /// 95 /// The conjunction of \a is_valid_option on each individual option. 96 template <class... Options> struct check_options; 97 template <> struct check_options<> : std::true_type {}; 98 template <class Option1, class... Options> 99 struct check_options<Option1, Options...> 100 : std::integral_constant<bool, is_valid_option<Option1>::value && 101 check_options<Options...>::value> {}; 102 103 /// Traits for options for \a ilist_node. 104 /// 105 /// This is usually computed via \a compute_node_options. 106 template <class T, bool EnableSentinelTracking, bool IsSentinelTrackingExplicit, 107 class TagT> 108 struct node_options { 109 typedef T value_type; 110 typedef T *pointer; 111 typedef T &reference; 112 typedef const T *const_pointer; 113 typedef const T &const_reference; 114 115 static const bool enable_sentinel_tracking = EnableSentinelTracking; 116 static const bool is_sentinel_tracking_explicit = IsSentinelTrackingExplicit; 117 typedef TagT tag; 118 typedef ilist_node_base<enable_sentinel_tracking> node_base_type; 119 typedef ilist_base<enable_sentinel_tracking> list_base_type; 120 }; 121 122 template <class T, class... Options> struct compute_node_options { 123 typedef node_options<T, extract_sentinel_tracking<Options...>::value, 124 extract_sentinel_tracking<Options...>::is_explicit, 125 typename extract_tag<Options...>::type> 126 type; 127 }; 128 129 } // end namespace ilist_detail 130 } // end namespace llvm 131 132 #endif // LLVM_ADT_ILIST_NODE_OPTIONS_H 133