template_string.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_COMMON_TEMPLATE_STRING_H_
  5. #define CARBON_COMMON_TEMPLATE_STRING_H_
  6. #include "llvm/ADT/StringRef.h"
  7. namespace Carbon {
  8. // Represents a compile-time string in a form suitable for use as a non-type
  9. // template argument.
  10. //
  11. // These arguments are required to be a "structural type", and so we copy the
  12. // string contents into a public array of `char`s. For details, see:
  13. // https://en.cppreference.com/w/cpp/language/template_parameters#Non-type_template_parameter
  14. //
  15. // Designed to support implicitly deduced construction from a string literal
  16. // template argument. This type will implicitly convert to an `llvm::StringRef`
  17. // for accessing the string contents, and also provides a dedicated `c_str()`
  18. // method to access the string as a C string.
  19. //
  20. // Example usage:
  21. // ```cpp
  22. // template <TemplateString Str> auto F() -> void {
  23. // llvm::cout() << Str;
  24. // }
  25. //
  26. // auto Example() -> void {
  27. // F<"string contents here">();
  28. // }
  29. // ```
  30. template <int N>
  31. struct TemplateString {
  32. // Constructs the template string from a string literal.
  33. //
  34. // Intentionally allows implicit conversion from string literals for use as a
  35. // non-type template parameter.
  36. //
  37. // The closest we can get to explicitly accepting a string literal is to
  38. // accept an array of `const char`s, so we additionally use Clang's constexpr
  39. // `enable_if` attribute to require the array to be usable as a C string with
  40. // the expected length. This checks both for null-termination and no embedded
  41. // `0` bytes.
  42. //
  43. // NOLINTNEXTLINE(google-explicit-constructor)
  44. constexpr TemplateString(const char (&str)[N + 1]) __attribute__((
  45. enable_if(__builtin_strlen(str) == N,
  46. "character array is not null-terminated valid C string"))) {
  47. // Rely on Clang's constexpr `__builtin_memcpy` to minimize compile time
  48. // overhead copying the string contents around.
  49. __builtin_memcpy(storage_, str, N + 1);
  50. }
  51. // This type is designed to act as a `StringRef` implicitly while having the
  52. // storage necessary to be used as a template parameter.
  53. //
  54. // NOLINTNEXTLINE(google-explicit-constructor)
  55. constexpr operator llvm::StringRef() const {
  56. return llvm::StringRef(storage_, N);
  57. }
  58. // Accesses the string data directly as a compile-time C string.
  59. constexpr auto c_str() const -> const char* { return storage_; }
  60. // Note that this must be public for the type to be structural and available
  61. // as a template argument, but this is not part of the public API.
  62. char storage_[N + 1];
  63. };
  64. // Allow deducing `N` when implicitly constructing these so that we can directly
  65. // use a string literal in a template argument. The array needs an extra char
  66. // for the null terminator.
  67. template <int M>
  68. TemplateString(const char (&str)[M]) -> TemplateString<M - 1>;
  69. } // namespace Carbon
  70. #endif // CARBON_COMMON_TEMPLATE_STRING_H_