forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_string.h
80 lines (70 loc) · 2.98 KB
/
template_string.h
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
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_COMMON_TEMPLATE_STRING_H_
#define CARBON_COMMON_TEMPLATE_STRING_H_
#include "llvm/ADT/StringRef.h"
namespace Carbon {
// Represents a compile-time string in a form suitable for use as a non-type
// template argument.
//
// These arguments are required to be a "structural type", and so we copy the
// string contents into a public array of `char`s. For details, see:
// https://en.cppreference.com/w/cpp/language/template_parameters#Non-type_template_parameter
//
// Designed to support implicitly deduced construction from a string literal
// template argument. This type will implicitly convert to an `llvm::StringRef`
// for accessing the string contents, and also provides a dedicated `c_str()`
// method to access the string as a C string.
//
// Example usage:
// ```cpp
// template <TemplateString Str> auto F() -> void {
// llvm::cout() << Str;
// }
//
// auto Example() -> void {
// F<"string contents here">();
// }
// ```
template <int N>
struct TemplateString {
// Constructs the template string from a string literal.
//
// Intentionally allows implicit conversion from string literals for use as a
// non-type template parameter.
//
// The closest we can get to explicitly accepting a string literal is to
// accept an array of `const char`s, so we additionally use Clang's constexpr
// `enable_if` attribute to require the array to be usable as a C string with
// the expected length. This checks both for null-termination and no embedded
// `0` bytes.
//
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr TemplateString(const char (&str)[N + 1]) __attribute__((
enable_if(__builtin_strlen(str) == N,
"character array is not null-terminated valid C string"))) {
// Rely on Clang's constexpr `__builtin_memcpy` to minimize compile time
// overhead copying the string contents around.
__builtin_memcpy(storage_, str, N + 1);
}
// This type is designed to act as a `StringRef` implicitly while having the
// storage necessary to be used as a template parameter.
//
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr operator llvm::StringRef() const {
return llvm::StringRef(storage_, N);
}
// Accesses the string data directly as a compile-time C string.
constexpr auto c_str() const -> const char* { return storage_; }
// Note that this must be public for the type to be structural and available
// as a template argument, but this is not part of the public API.
char storage_[N + 1];
};
// Allow deducing `N` when implicitly constructing these so that we can directly
// use a string literal in a template argument. The array needs an extra char
// for the null terminator.
template <int M>
TemplateString(const char (&str)[M]) -> TemplateString<M - 1>;
} // namespace Carbon
#endif // CARBON_COMMON_TEMPLATE_STRING_H_