1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -verify %s -fms-extensions -triple x86_64-apple-darwin9.0.0
2f4a2713aSLionel Sambuc
3f4a2713aSLionel Sambuc using size_t = decltype(sizeof(int));
4f4a2713aSLionel Sambuc enum class LitKind {
5f4a2713aSLionel Sambuc Char, WideChar, Char16, Char32,
6f4a2713aSLionel Sambuc CharStr, WideStr, Char16Str, Char32Str,
7f4a2713aSLionel Sambuc Integer, Floating, Raw, Template
8f4a2713aSLionel Sambuc };
operator ""_kind(char p)9f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(char p) { return LitKind::Char; }
operator ""_kind(wchar_t p)10f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(wchar_t p) { return LitKind::WideChar; }
operator ""_kind(char16_t p)11f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(char16_t p) { return LitKind::Char16; }
operator ""_kind(char32_t p)12f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(char32_t p) { return LitKind::Char32; }
operator ""_kind(const char * p,size_t n)13f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const char *p, size_t n) { return LitKind::CharStr; }
operator ""_kind(const wchar_t * p,size_t n)14f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const wchar_t *p, size_t n) { return LitKind::WideStr; }
operator ""_kind(const char16_t * p,size_t n)15f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const char16_t *p, size_t n) { return LitKind::Char16Str; }
operator ""_kind(const char32_t * p,size_t n)16f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const char32_t *p, size_t n) { return LitKind::Char32Str; }
operator ""_kind(unsigned long long n)17f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(unsigned long long n) { return LitKind::Integer; }
operator ""_kind(long double n)18f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(long double n) { return LitKind::Floating; }
operator ""_kind2(const char * p)19f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind2(const char *p) { return LitKind::Raw; }
operator ""_kind3()20f4a2713aSLionel Sambuc template<char ...Cs> constexpr LitKind operator"" _kind3() { return LitKind::Template; }
21f4a2713aSLionel Sambuc
22f4a2713aSLionel Sambuc static_assert('x'_kind == LitKind::Char, "");
23f4a2713aSLionel Sambuc static_assert(L'x'_kind == LitKind::WideChar, "");
24f4a2713aSLionel Sambuc static_assert(u'x'_kind == LitKind::Char16, "");
25f4a2713aSLionel Sambuc static_assert(U'x'_kind == LitKind::Char32, "");
26f4a2713aSLionel Sambuc static_assert("foo"_kind == LitKind::CharStr, "");
27f4a2713aSLionel Sambuc static_assert(u8"foo"_kind == LitKind::CharStr, "");
28f4a2713aSLionel Sambuc static_assert(L"foo"_kind == LitKind::WideStr, "");
29f4a2713aSLionel Sambuc static_assert(u"foo"_kind == LitKind::Char16Str, "");
30f4a2713aSLionel Sambuc static_assert(U"foo"_kind == LitKind::Char32Str, "");
31f4a2713aSLionel Sambuc static_assert(194_kind == LitKind::Integer, "");
32f4a2713aSLionel Sambuc static_assert(0377_kind == LitKind::Integer, "");
33f4a2713aSLionel Sambuc static_assert(0x5ffc_kind == LitKind::Integer, "");
34f4a2713aSLionel Sambuc static_assert(.5954_kind == LitKind::Floating, "");
35f4a2713aSLionel Sambuc static_assert(1._kind == LitKind::Floating, "");
36f4a2713aSLionel Sambuc static_assert(1.e-2_kind == LitKind::Floating, "");
37f4a2713aSLionel Sambuc static_assert(4e6_kind == LitKind::Floating, "");
38f4a2713aSLionel Sambuc static_assert(4e6_kind2 == LitKind::Raw, "");
39f4a2713aSLionel Sambuc static_assert(4e6_kind3 == LitKind::Template, "");
40f4a2713aSLionel Sambuc
fractional_digits_impl(const char * p)41f4a2713aSLionel Sambuc constexpr const char *fractional_digits_impl(const char *p) {
42f4a2713aSLionel Sambuc return *p == '.' ? p + 1 : *p ? fractional_digits_impl(p + 1) : 0;
43f4a2713aSLionel Sambuc }
operator ""_fractional_digits(const char * p)44f4a2713aSLionel Sambuc constexpr const char *operator"" _fractional_digits(const char *p) {
45f4a2713aSLionel Sambuc return fractional_digits_impl(p) ?: p;
46f4a2713aSLionel Sambuc }
streq(const char * p,const char * q)47f4a2713aSLionel Sambuc constexpr bool streq(const char *p, const char *q) {
48f4a2713aSLionel Sambuc return *p == *q && (!*p || streq(p+1, q+1));
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc static_assert(streq(143.97_fractional_digits, "97"), "");
52f4a2713aSLionel Sambuc static_assert(streq(0x786_fractional_digits, "0x786"), "");
53f4a2713aSLionel Sambuc static_assert(streq(.4_fractional_digits, "4"), "");
54f4a2713aSLionel Sambuc static_assert(streq(4._fractional_digits, ""), "");
55f4a2713aSLionel Sambuc static_assert(streq(1e+97_fractional_digits, "1e+97"), "");
56f4a2713aSLionel Sambuc static_assert(streq(0377_fractional_digits, "0377"), "");
57f4a2713aSLionel Sambuc static_assert(streq(0377.5_fractional_digits, "5"), "");
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc int operator"" _ambiguous(char); // expected-note {{candidate}}
60f4a2713aSLionel Sambuc namespace N {
61f4a2713aSLionel Sambuc void *operator"" _ambiguous(char); // expected-note {{candidate}}
62f4a2713aSLionel Sambuc }
63f4a2713aSLionel Sambuc using namespace N;
64f4a2713aSLionel Sambuc int k = 'x'_ambiguous; // expected-error {{ambiguous}}
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc int operator"" _deleted(unsigned long long) = delete; // expected-note {{here}}
67f4a2713aSLionel Sambuc int m = 42_deleted; // expected-error {{attempt to use a deleted}}
68f4a2713aSLionel Sambuc
69f4a2713aSLionel Sambuc namespace Using {
70f4a2713aSLionel Sambuc namespace M {
71f4a2713aSLionel Sambuc int operator"" _using(char);
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc int k1 = 'x'_using; // expected-error {{no matching literal operator for call to 'operator "" _using'}}
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc using M::operator "" _using;
76f4a2713aSLionel Sambuc int k2 = 'x'_using;
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc
79f4a2713aSLionel Sambuc namespace AmbiguousRawTemplate {
80f4a2713aSLionel Sambuc int operator"" _ambig1(const char *); // expected-note {{candidate}}
81f4a2713aSLionel Sambuc template<char...> int operator"" _ambig1(); // expected-note {{candidate}}
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc int k1 = 123_ambig1; // expected-error {{call to 'operator "" _ambig1' is ambiguous}}
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc namespace Inner {
86f4a2713aSLionel Sambuc template<char...> int operator"" _ambig2(); // expected-note 3{{candidate}}
87f4a2713aSLionel Sambuc }
88f4a2713aSLionel Sambuc int operator"" _ambig2(const char *); // expected-note 3{{candidate}}
89f4a2713aSLionel Sambuc using Inner::operator"" _ambig2;
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc int k2 = 123_ambig2; // expected-error {{call to 'operator "" _ambig2' is ambiguous}}
92f4a2713aSLionel Sambuc
93f4a2713aSLionel Sambuc namespace N {
94f4a2713aSLionel Sambuc using Inner::operator"" _ambig2;
95f4a2713aSLionel Sambuc
96f4a2713aSLionel Sambuc int k3 = 123_ambig2; // ok
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc using AmbiguousRawTemplate::operator"" _ambig2;
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc int k4 = 123_ambig2; // expected-error {{ambiguous}}
101f4a2713aSLionel Sambuc
102f4a2713aSLionel Sambuc namespace M {
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc template<char...> int operator"" _ambig2();
105f4a2713aSLionel Sambuc
106f4a2713aSLionel Sambuc int k5 = 123_ambig2; // ok
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc int operator"" _ambig2(unsigned long long);
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc int k6 = 123_ambig2; // ok
112f4a2713aSLionel Sambuc int k7 = 123._ambig2; // expected-error {{ambiguous}}
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc
mash(unsigned a)116f4a2713aSLionel Sambuc constexpr unsigned mash(unsigned a) {
117f4a2713aSLionel Sambuc return 0x93ae27b5 * ((a >> 13) | a << 19);
118f4a2713aSLionel Sambuc }
hash(unsigned a)119f4a2713aSLionel Sambuc template<typename=void> constexpr unsigned hash(unsigned a) { return a; }
hash(unsigned a)120f4a2713aSLionel Sambuc template<char C, char...Cs> constexpr unsigned hash(unsigned a) {
121f4a2713aSLionel Sambuc return hash<Cs...>(mash(a ^ mash(C)));
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc template<typename T, T v> struct constant { constexpr static T value = v; };
operator ""_hash()124f4a2713aSLionel Sambuc template<char...Cs> constexpr unsigned operator"" _hash() {
125f4a2713aSLionel Sambuc return constant<unsigned, hash<Cs...>(0)>::value;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc static_assert(0x1234_hash == 0x103eff5e, "");
128f4a2713aSLionel Sambuc static_assert(hash<'0', 'x', '1', '2', '3', '4'>(0) == 0x103eff5e, "");
129f4a2713aSLionel Sambuc
130f4a2713aSLionel Sambuc // Functions and literal suffixes go in separate namespaces.
131f4a2713aSLionel Sambuc namespace Namespace {
132f4a2713aSLionel Sambuc template<char...> int operator"" _x();
133f4a2713aSLionel Sambuc int k = _x(); // expected-error {{undeclared identifier '_x'}}
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc int _y(unsigned long long);
136f4a2713aSLionel Sambuc int k2 = 123_y; // expected-error {{no matching literal operator for call to 'operator "" _y'}}
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc
139f4a2713aSLionel Sambuc namespace PR14950 {
140f4a2713aSLionel Sambuc template<...> // expected-error {{expected template parameter}}
141f4a2713aSLionel Sambuc int operator"" _b(); // expected-error {{no function template matches function template specialization}}
main()142f4a2713aSLionel Sambuc int main() { return 0_b; } // expected-error {{no matching literal operator for call to 'operator "" _b'}}
143f4a2713aSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc
145*0a6a1f1dSLionel Sambuc namespace bad_names {
146*0a6a1f1dSLionel Sambuc template<char...> int operator""_x();
147*0a6a1f1dSLionel Sambuc
f()148*0a6a1f1dSLionel Sambuc template<typename T> void f() {
149*0a6a1f1dSLionel Sambuc class T:: // expected-error {{anonymous class}} expected-warning {{does not declare anything}}
150*0a6a1f1dSLionel Sambuc operator // expected-error {{expected identifier}}
151*0a6a1f1dSLionel Sambuc ""_q<'a'>;
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc T::template operator""_q<'a'>(); // expected-error {{non-namespace scope 'T::' cannot have a literal operator member}} expected-error +{{}}
154*0a6a1f1dSLionel Sambuc T::template operator""_q<'a'>::X; // expected-error {{non-namespace scope 'T::' cannot have a literal operator member}} expected-error +{{}}
155*0a6a1f1dSLionel Sambuc T::operator""_q<'a'>(); // expected-error {{non-namespace scope 'T::' cannot have a literal operator member}} expected-error +{{}}
156*0a6a1f1dSLionel Sambuc typename T::template operator""_q<'a'> a; // expected-error {{non-namespace scope 'T::' cannot have a literal operator member}} expected-error +{{}}
157*0a6a1f1dSLionel Sambuc typename T::operator""_q(""); // expected-error +{{}} expected-note {{to match}}
158*0a6a1f1dSLionel Sambuc T::operator""_q(""); // expected-error {{non-namespace scope 'T::' cannot have a literal operator member}}
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc bad_names::operator""_x<'a', 'b', 'c'>();
161*0a6a1f1dSLionel Sambuc };
162*0a6a1f1dSLionel Sambuc
163*0a6a1f1dSLionel Sambuc struct S {};
g()164*0a6a1f1dSLionel Sambuc void g() {
165*0a6a1f1dSLionel Sambuc S::operator""_q(); // expected-error {{non-namespace scope 'S::' cannot have a literal operator member}}
166*0a6a1f1dSLionel Sambuc }
167*0a6a1f1dSLionel Sambuc }
168