================
@@ -0,0 +1,586 @@
+//==---- QualTypeMapper.cpp - Maps Clang QualType to LLVMABI Types 
---------==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Maps Clang QualType instances to corresponding LLVM ABI type
+/// representations. This mapper translates high-level type information from 
the
+/// AST into low-level ABI-specific types that encode size, alignment, and
+/// layout details required for code generation and cross-language
+/// interoperability.
+///
+//===----------------------------------------------------------------------===//
+#include "QualTypeMapper.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTFwd.h"
+#include "clang/AST/Attr.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/RecordLayout.h"
+#include "clang/AST/Type.h"
+#include "clang/Basic/AddressSpaces.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/TargetInfo.h"
+#include "llvm/ABI/Types.h"
+#include "llvm/Support/Alignment.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/TypeSize.h"
+#include <cstdint>
+
+namespace clang {
+namespace CodeGen {
+
+/// Main entry point for converting Clang QualType to LLVM ABI Type.
+/// This method performs type canonicalization, caching, and dispatches
+/// to specialized conversion methods based on the type kind.
+///
+/// \param QT The Clang QualType to convert
+/// \return Corresponding LLVM ABI Type representation, or nullptr on error
+const llvm::abi::Type *QualTypeMapper::convertType(QualType QT) {
+  // Canonicalize type and strip qualifiers
+  // This ensures consistent type representation across different contexts
+  QT = QT.getCanonicalType().getUnqualifiedType();
+
+  // Results are cached since type conversion may be expensive
+  auto It = TypeCache.find(QT);
+  if (It != TypeCache.end())
+    return It->second;
+
+  const llvm::abi::Type *Result = nullptr;
+  switch (QT->getTypeClass()) {
+    // Non-canonical and dependent types should have been stripped by
+    // getCanonicalType() above or cannot appear during code generation.
+#define TYPE(Class, Base)
+#define ABSTRACT_TYPE(Class, Base)
+#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
+#define DEPENDENT_TYPE(Class, Base) case Type::Class:
+#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
+#include "clang/AST/TypeNodes.inc"
+    llvm::reportFatalInternalError(
+        "Non-canonical or dependent types should not reach ABI lowering");
+
+  case Type::Builtin:
+    Result = convertBuiltinType(cast<BuiltinType>(QT));
+    break;
+  case Type::Pointer:
+    Result =
+        createPointerTypeForPointee(cast<PointerType>(QT)->getPointeeType());
+    break;
+  case Type::LValueReference:
+  case Type::RValueReference:
+    Result =
+        createPointerTypeForPointee(cast<ReferenceType>(QT)->getPointeeType());
+    break;
+  case Type::ConstantArray:
+  case Type::ArrayParameter:
+  case Type::IncompleteArray:
+  case Type::VariableArray:
+    Result = convertArrayType(cast<ArrayType>(QT));
+    break;
+  case Type::Vector:
+  case Type::ExtVector:
+    Result = convertVectorType(cast<VectorType>(QT));
+    break;
+  case Type::Record:
+    Result = convertRecordType(cast<RecordType>(QT));
+    break;
+  case Type::Enum:
+    Result = convertEnumType(cast<EnumType>(QT));
+    break;
+  case Type::Complex:
+    Result = convertComplexType(cast<ComplexType>(QT));
+    break;
+  case Type::Atomic:
+    return convertType(cast<AtomicType>(QT)->getValueType());
+  case Type::BlockPointer:
+    return createPointerTypeForPointee(ASTCtx.VoidPtrTy);
+  case Type::Pipe:
+    Result = createPointerTypeForPointee(ASTCtx.VoidPtrTy);
+    break;
+  case Type::ConstantMatrix: {
+    const auto *MT = cast<ConstantMatrixType>(QT);
+    return Builder.getArrayType(convertType(MT->getElementType()),
+                                MT->getNumRows() * MT->getNumColumns(),
+                                ASTCtx.getTypeSize(QT), /*IsMatrixType=*/true);
+  }
+  case Type::MemberPointer:
+    Result = convertMemberPointerType(cast<MemberPointerType>(QT));
+    break;
+  case Type::BitInt: {
+    const auto *BIT = cast<BitIntType>(QT);
+    return Builder.getIntegerType(BIT->getNumBits(), getTypeAlign(QT),
+                                  /*Signed=*/BIT->isSigned(),
+                                  /*IsBitInt=*/true);
+  }
+  case Type::ObjCObject:
+  case Type::ObjCInterface:
+  case Type::ObjCObjectPointer:
+    // Objective-C objects are represented as pointers in the ABI.
+    return Builder.getPointerType(
+        ASTCtx.getTargetInfo().getPointerWidth(QT.getAddressSpace()),
+        llvm::Align(ASTCtx.getTargetInfo().getPointerAlign(LangAS::Default) /
+                    8),
+        ASTCtx.getTargetInfo().getTargetAddressSpace(QT.getAddressSpace()));
+  case Type::Auto:
+  case Type::DeducedTemplateSpecialization:
+  case Type::FunctionProto:
+  case Type::FunctionNoProto:
+  case Type::HLSLAttributedResource:
+    llvm::reportFatalInternalError("Type not supported in ABI lowering");
+  }
+
+  assert(Result && "convertType returned nullptr");
+  TypeCache[QT] = Result;
+  return Result;
+}
+
+/// Converts C/C++ builtin types to LLVM ABI types.
+/// This handles all fundamental scalar types including integers, floats,
+/// and special types like void and bool.
+///
+/// \param BT The BuiltinType to convert
+/// \return Corresponding LLVM ABI integer, float, or void type
----------------
nikic wrote:

I'd drop these `\param` and `\return` (here and on other methods in this file). 
They're kind of obvious and don't really add anything. (Based on this 
discussion: https://discourse.llvm.org/t/rfc-policy-for-doxygen-comments/89675)

https://github.com/llvm/llvm-project/pull/174634
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to