1 //===- FormatVariadicDetails.h - Helpers for FormatVariadic.h ----*- 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_SUPPORT_FORMATVARIADIC_DETAILS_H
11 #define LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/raw_ostream.h"
15
16 #include <type_traits>
17
18 namespace llvm {
19 template <typename T, typename Enable = void> struct format_provider {};
20 class Error;
21
22 namespace detail {
23 class format_adapter {
24 protected:
~format_adapter()25 virtual ~format_adapter() {}
26
27 public:
28 virtual void format(raw_ostream &S, StringRef Options) = 0;
29 };
30
31 template <typename T> class provider_format_adapter : public format_adapter {
32 T Item;
33
34 public:
provider_format_adapter(T && Item)35 explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
36
format(llvm::raw_ostream & S,StringRef Options)37 void format(llvm::raw_ostream &S, StringRef Options) override {
38 format_provider<typename std::decay<T>::type>::format(Item, S, Options);
39 }
40 };
41
42 template <typename T>
43 class stream_operator_format_adapter : public format_adapter {
44 T Item;
45
46 public:
stream_operator_format_adapter(T && Item)47 explicit stream_operator_format_adapter(T &&Item)
48 : Item(std::forward<T>(Item)) {}
49
format(llvm::raw_ostream & S,StringRef Options)50 void format(llvm::raw_ostream &S, StringRef Options) override { S << Item; }
51 };
52
53 template <typename T> class missing_format_adapter;
54
55 // Test if format_provider<T> is defined on T and contains a member function
56 // with the signature:
57 // static void format(const T&, raw_stream &, StringRef);
58 //
59 template <class T> class has_FormatProvider {
60 public:
61 using Decayed = typename std::decay<T>::type;
62 typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
63 StringRef);
64
65 template <typename U>
66 static char test(SameType<Signature_format, &U::format> *);
67
68 template <typename U> static double test(...);
69
70 static bool const value =
71 (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
72 };
73
74 // Test if raw_ostream& << T -> raw_ostream& is findable via ADL.
75 template <class T> class has_StreamOperator {
76 public:
77 using ConstRefT = const typename std::decay<T>::type &;
78
79 template <typename U>
80 static char test(typename std::enable_if<
81 std::is_same<decltype(std::declval<llvm::raw_ostream &>()
82 << std::declval<U>()),
83 llvm::raw_ostream &>::value,
84 int *>::type);
85
86 template <typename U> static double test(...);
87
88 static bool const value = (sizeof(test<ConstRefT>(nullptr)) == 1);
89 };
90
91 // Simple template that decides whether a type T should use the member-function
92 // based format() invocation.
93 template <typename T>
94 struct uses_format_member
95 : public std::integral_constant<
96 bool,
97 std::is_base_of<format_adapter,
98 typename std::remove_reference<T>::type>::value> {};
99
100 // Simple template that decides whether a type T should use the format_provider
101 // based format() invocation. The member function takes priority, so this test
102 // will only be true if there is not ALSO a format member.
103 template <typename T>
104 struct uses_format_provider
105 : public std::integral_constant<
106 bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
107 };
108
109 // Simple template that decides whether a type T should use the operator<<
110 // based format() invocation. This takes last priority.
111 template <typename T>
112 struct uses_stream_operator
113 : public std::integral_constant<bool, !uses_format_member<T>::value &&
114 !uses_format_provider<T>::value &&
115 has_StreamOperator<T>::value> {};
116
117 // Simple template that decides whether a type T has neither a member-function
118 // nor format_provider based implementation that it can use. Mostly used so
119 // that the compiler spits out a nice diagnostic when a type with no format
120 // implementation can be located.
121 template <typename T>
122 struct uses_missing_provider
123 : public std::integral_constant<bool, !uses_format_member<T>::value &&
124 !uses_format_provider<T>::value &&
125 !uses_stream_operator<T>::value> {
126 };
127
128 template <typename T>
129 typename std::enable_if<uses_format_member<T>::value, T>::type
build_format_adapter(T && Item)130 build_format_adapter(T &&Item) {
131 return std::forward<T>(Item);
132 }
133
134 template <typename T>
135 typename std::enable_if<uses_format_provider<T>::value,
136 provider_format_adapter<T>>::type
build_format_adapter(T && Item)137 build_format_adapter(T &&Item) {
138 return provider_format_adapter<T>(std::forward<T>(Item));
139 }
140
141 template <typename T>
142 typename std::enable_if<uses_stream_operator<T>::value,
143 stream_operator_format_adapter<T>>::type
build_format_adapter(T && Item)144 build_format_adapter(T &&Item) {
145 // If the caller passed an Error by value, then stream_operator_format_adapter
146 // would be responsible for consuming it.
147 // Make the caller opt into this by calling fmt_consume().
148 static_assert(
149 !std::is_same<llvm::Error, typename std::remove_cv<T>::type>::value,
150 "llvm::Error-by-value must be wrapped in fmt_consume() for formatv");
151 return stream_operator_format_adapter<T>(std::forward<T>(Item));
152 }
153
154 template <typename T>
155 typename std::enable_if<uses_missing_provider<T>::value,
156 missing_format_adapter<T>>::type
build_format_adapter(T && Item)157 build_format_adapter(T &&Item) {
158 return missing_format_adapter<T>();
159 }
160 }
161 }
162
163 #endif
164