alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
DemangleTypeNames.hpp
Go to the documentation of this file.
1/* Copyright 2025 Andrea Bocci, Simeon Ehrig
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5/*
6 * C++20 compile-time demangled type name, based on
7 * https://www.reddit.com/r/cpp/comments/lfi6jt/finally_a_possibly_portable_way_to_convert_types/
8 * https://rodusek.com/posts/2021/03/09/getting-an-unmangled-type-name-at-compile-time/
9 */
10
11#pragma once
12
13#include <array>
14/** The following compilers does not use std::source_location therefore we do not include the header.
15 * Including the header source_location would lead into compiler issues under the following conditions.
16 * - nvcc is used with clang 15 as host compiler and MDSpan is used too.
17 * error: `include/c++/11/source_location(53): error: identifier "__builtin_source_location" is undefined`
18 */
19#if !(defined(__GNUC__) || defined(__clang__) || defined(__PGI) || defined(__NVCOMPILER) || defined(_MSC_VER))
20# if defined(__cpp_lib_source_location)
21# include <source_location>
22# endif
23#endif
24#include <string_view>
25#include <utility>
26
27namespace alpaka::core
28{
29
30 namespace detail
31 {
32
33 // Generate a function name with full type signature that includes the user-provided type name.
34 // Use the compiler-specific extensions for known compilers because nvc++ does not include the full type
35 // signature in std::source_location::current().function_name() .
36 template<typename T>
37 consteval std::string_view embedTypeName()
38 {
39#if defined(__GNUC__) || defined(__clang__) || defined(__PGI) || defined(__NVCOMPILER)
40 // gcc, clang, PGI, nvc++
42#elif defined(_MSC_VER)
43 // MSVC
44 return __FUNCSIG__;
45#elif defined(__cpp_lib_source_location)
46 // unknown compiler, use the c++20 standard function_name()
47 return std::source_location::current().function_name();
48#else
49 // std::source_location is not supported, fail
50# error \
51 "This compiler is not recognised and it does not support std::source_location. Please add explicit support to \"alpaka/core/DemangleTypeNames.hpp\"."
52#endif
53 }
54
55 // Use a fundamental type to detect the prefix and suffix, because MSVC includes the class, struct, or union
56 // keyword in the type signature.
58
59 constexpr std::string_view testPatternName("double");
60
61 // Embed the demangled type name into a function signature, then extract it and return it as a null-terminated
62 // array of bytes.
63 template<typename T>
64 consteval auto demangleAsArray()
65 {
66 // Embed a known type, and use it to extract the prefix and suffix generated by the compiler.
67 constexpr std::string_view embeddedPattern = embedTypeName<TestPatternType>();
68 constexpr size_t prefixSize = embeddedPattern.find(testPatternName);
69 constexpr size_t suffixSize = embeddedPattern.size() - prefixSize - testPatternName.size();
70
71 // Embed T, and use the known prefix and suffix length to determien the type name's position and size.
72 constexpr std::string_view embeddedType = embedTypeName<T>();
73 constexpr size_t start = prefixSize;
74 constexpr size_t end = embeddedType.size() - suffixSize;
75 static_assert(start < end);
76 constexpr size_t length = end - start;
77
78 // Copy the demangled type name into an array, and add a null terminaton character
79 std::array<char, length + 1> storage{};
80 std::copy(embeddedType.data() + start, embeddedType.data() + end, storage.data());
81 storage[length] = '\0';
82
83 return storage;
84 }
85
86 // Store the demangled type name as a null-terminated array of bytes.
87 // Note: this could be a function-static constexpr variable in c++23.
88 template<typename T>
89 inline constexpr auto storage = demangleAsArray<T>();
90
91 // Return the demangled type name.
92 template<typename T>
93 consteval std::string_view demangle()
94 {
95 return std::string_view{storage<T>.data(), storage<T>.size()};
96 }
97
98 } // namespace detail
99
100 // constexpr template string_view that contains the demangled type name.
101 template<typename T>
102 inline constexpr std::string_view demangled = detail::demangle<T>();
103
104} // namespace alpaka::core
consteval std::string_view demangle()
consteval auto demangleAsArray()
consteval std::string_view embedTypeName()
constexpr std::string_view testPatternName("double")
constexpr std::string_view demangled
auto clipCast(V const &val) -> T
Definition ClipCast.hpp:16