Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:34

0001 //===- TypeName.h -----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_TYPENAME_H
0010 #define LLVM_SUPPORT_TYPENAME_H
0011 
0012 #include "llvm/ADT/StringRef.h"
0013 
0014 namespace llvm {
0015 
0016 namespace detail {
0017 template <typename DesiredTypeName> inline StringRef getTypeNameImpl() {
0018 #if defined(__clang__) || defined(__GNUC__)
0019   StringRef Name = __PRETTY_FUNCTION__;
0020 
0021   StringRef Key = "DesiredTypeName = ";
0022   Name = Name.substr(Name.find(Key));
0023   assert(!Name.empty() && "Unable to find the template parameter!");
0024   Name = Name.drop_front(Key.size());
0025 
0026   assert(Name.ends_with("]") && "Name doesn't end in the substitution key!");
0027   return Name.drop_back(1);
0028 #elif defined(_MSC_VER)
0029   StringRef Name = __FUNCSIG__;
0030 
0031   StringRef Key = "getTypeNameImpl<";
0032   Name = Name.substr(Name.find(Key));
0033   assert(!Name.empty() && "Unable to find the function name!");
0034   Name = Name.drop_front(Key.size());
0035 
0036   for (StringRef Prefix : {"class ", "struct ", "union ", "enum "})
0037     if (Name.starts_with(Prefix)) {
0038       Name = Name.drop_front(Prefix.size());
0039       break;
0040     }
0041 
0042   auto AnglePos = Name.rfind('>');
0043   assert(AnglePos != StringRef::npos && "Unable to find the closing '>'!");
0044   return Name.substr(0, AnglePos);
0045 #else
0046   // No known technique for statically extracting a type name on this compiler.
0047   // We return a string that is unlikely to look like any type in LLVM.
0048   return "UNKNOWN_TYPE";
0049 #endif
0050 }
0051 } // namespace detail
0052 
0053 /// We provide a function which tries to compute the (demangled) name of a type
0054 /// statically.
0055 ///
0056 /// This routine may fail on some platforms or for particularly unusual types.
0057 /// Do not use it for anything other than logging and debugging aids. It isn't
0058 /// portable or dependendable in any real sense.
0059 ///
0060 /// The returned StringRef will point into a static storage duration string.
0061 /// However, it may not be null terminated and may be some strangely aligned
0062 /// inner substring of a larger string.
0063 template <typename DesiredTypeName> inline StringRef getTypeName() {
0064   static StringRef Name = detail::getTypeNameImpl<DesiredTypeName>();
0065   return Name;
0066 }
0067 
0068 } // namespace llvm
0069 
0070 #endif