template_string.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. explicit(false) constexpr TemplateString(const char (&str)[N + 1])
  43. __attribute__((
  44. enable_if(__builtin_strlen(str) == N,
  45. "character array is not null-terminated valid C string"))) {
  46. // Rely on Clang's constexpr `__builtin_memcpy` to minimize compile time
  47. // overhead copying the string contents around.
  48. __builtin_memcpy(storage_, str, N + 1);
  49. }
  50. // This type is designed to act as a `StringLiteral` implicitly while having
  51. // the storage necessary to be used as a template parameter.
  52. //
  53. // NOLINTNEXTLINE(google-explicit-constructor)
  54. explicit(false) constexpr operator llvm::StringLiteral() const {
  55. return llvm::StringLiteral::withInnerNUL(storage_);
  56. }
  57. // Accesses the string data directly as a compile-time C string.
  58. constexpr auto c_str() const -> const char* { return storage_; }
  59. // Note that this must be public for the type to be structural and available
  60. // as a template argument, but this is not part of the public API.
  61. char storage_[N + 1];
  62. };
  63. // Allow deducing `N` when implicitly constructing these so that we can directly
  64. // use a string literal in a template argument. The array needs an extra char
  65. // for the null terminator.
  66. template <int M>
  67. TemplateString(const char (&str)[M]) -> TemplateString<M - 1>;
  68. } // namespace Carbon
  69. #endif // CARBON_COMMON_TEMPLATE_STRING_H_