| 1 | //==---- QualTypeMapper.cpp - Maps Clang QualType to LLVMABI Types ---------==// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// Maps Clang QualType instances to corresponding LLVM ABI type |
| 11 | /// representations. This mapper translates high-level type information from the |
| 12 | /// AST into low-level ABI-specific types that encode size, alignment, and |
| 13 | /// layout details required for code generation and cross-language |
| 14 | /// interoperability. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "QualTypeMapper.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/ASTFwd.h" |
| 20 | #include "clang/AST/Attr.h" |
| 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/DeclCXX.h" |
| 23 | #include "clang/AST/RecordLayout.h" |
| 24 | #include "clang/AST/Type.h" |
| 25 | #include "clang/Basic/AddressSpaces.h" |
| 26 | #include "clang/Basic/LLVM.h" |
| 27 | #include "clang/Basic/TargetInfo.h" |
| 28 | #include "llvm/ABI/Types.h" |
| 29 | #include "llvm/Support/Alignment.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/TypeSize.h" |
| 32 | #include <cstdint> |
| 33 | |
| 34 | namespace clang { |
| 35 | namespace CodeGen { |
| 36 | |
| 37 | /// Main entry point for converting Clang QualType to LLVM ABI Type. |
| 38 | /// This method performs type canonicalization, caching, and dispatches |
| 39 | /// to specialized conversion methods based on the type kind. |
| 40 | /// |
| 41 | /// \param QT The Clang QualType to convert |
| 42 | /// \return Corresponding LLVM ABI Type representation |
| 43 | const llvm::abi::Type *QualTypeMapper::convertType(QualType QT) { |
| 44 | // Canonicalize type and strip qualifiers |
| 45 | // This ensures consistent type representation across different contexts |
| 46 | // |
| 47 | // TODO: AttributedType is NeverCanonical, so aligned typedef attributes |
| 48 | // for instance, __attribute__((aligned(N))) are lost here. Capture the |
| 49 | // effective alignment from the original QT and thread it through |
| 50 | // convertTypeImpl. |
| 51 | QT = QT.getCanonicalType().getUnqualifiedType(); |
| 52 | |
| 53 | // Results are cached since type conversion may be expensive. |
| 54 | auto It = TypeCache.find(Val: QT); |
| 55 | if (It != TypeCache.end()) |
| 56 | return It->second; |
| 57 | |
| 58 | const llvm::abi::Type *Result = convertTypeImpl(QT); |
| 59 | assert(Result && "convertTypeImpl returned nullptr" ); |
| 60 | TypeCache[QT] = Result; |
| 61 | return Result; |
| 62 | } |
| 63 | |
| 64 | /// Dispatches to specialized conversion methods based on the type kind. |
| 65 | const llvm::abi::Type *QualTypeMapper::convertTypeImpl(QualType QT) { |
| 66 | switch (QT->getTypeClass()) { |
| 67 | // Non-canonical and dependent types should have been stripped by |
| 68 | // getCanonicalType() above or cannot appear during code generation. |
| 69 | #define TYPE(Class, Base) |
| 70 | #define ABSTRACT_TYPE(Class, Base) |
| 71 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 72 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 73 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 74 | #include "clang/AST/TypeNodes.inc" |
| 75 | llvm::reportFatalInternalError( |
| 76 | reason: "Non-canonical or dependent types should not reach ABI lowering" ); |
| 77 | |
| 78 | case Type::Builtin: |
| 79 | return convertBuiltinType(BT: cast<BuiltinType>(Val&: QT)); |
| 80 | case Type::Pointer: |
| 81 | return createPointerTypeForPointee(PointeeType: cast<PointerType>(Val&: QT)->getPointeeType()); |
| 82 | case Type::LValueReference: |
| 83 | case Type::RValueReference: |
| 84 | return createPointerTypeForPointee( |
| 85 | PointeeType: cast<ReferenceType>(Val&: QT)->getPointeeType()); |
| 86 | case Type::ConstantArray: |
| 87 | case Type::ArrayParameter: |
| 88 | case Type::IncompleteArray: |
| 89 | case Type::VariableArray: |
| 90 | return convertArrayType(AT: cast<ArrayType>(Val&: QT)); |
| 91 | case Type::Vector: |
| 92 | case Type::ExtVector: |
| 93 | return convertVectorType(VT: cast<VectorType>(Val&: QT)); |
| 94 | case Type::Record: |
| 95 | return convertRecordType(RT: cast<RecordType>(Val&: QT)); |
| 96 | case Type::Enum: |
| 97 | return convertEnumType(ET: cast<EnumType>(Val&: QT)); |
| 98 | case Type::Complex: |
| 99 | return convertComplexType(CT: cast<ComplexType>(Val&: QT)); |
| 100 | case Type::Atomic: |
| 101 | return convertType(QT: cast<AtomicType>(Val&: QT)->getValueType()); |
| 102 | case Type::BlockPointer: |
| 103 | case Type::Pipe: |
| 104 | return createPointerTypeForPointee(PointeeType: ASTCtx.VoidPtrTy); |
| 105 | case Type::ConstantMatrix: { |
| 106 | const auto *MT = cast<ConstantMatrixType>(Val&: QT); |
| 107 | return Builder.getArrayType(ElementType: convertType(QT: MT->getElementType()), |
| 108 | NumElements: MT->getNumRows() * MT->getNumColumns(), |
| 109 | SizeInBits: ASTCtx.getTypeSize(T: QT), /*IsMatrixType=*/true); |
| 110 | } |
| 111 | case Type::MemberPointer: |
| 112 | return convertMemberPointerType(MPT: cast<MemberPointerType>(Val&: QT)); |
| 113 | case Type::BitInt: { |
| 114 | const auto *BIT = cast<BitIntType>(Val&: QT); |
| 115 | return Builder.getIntegerType(BitWidth: BIT->getNumBits(), Align: getTypeAlign(QT), |
| 116 | /*Signed=*/BIT->isSigned(), |
| 117 | /*IsBitInt=*/true); |
| 118 | } |
| 119 | case Type::ObjCObject: |
| 120 | case Type::ObjCInterface: |
| 121 | case Type::ObjCObjectPointer: |
| 122 | // Objective-C objects are represented as pointers in the ABI. |
| 123 | return Builder.getPointerType( |
| 124 | Size: ASTCtx.getTargetInfo().getPointerWidth(AddrSpace: QT.getAddressSpace()), |
| 125 | Align: llvm::Align( |
| 126 | ASTCtx.getTargetInfo().getPointerAlign(AddrSpace: QT.getAddressSpace()) / 8), |
| 127 | Addrspace: ASTCtx.getTargetInfo().getTargetAddressSpace(AS: QT.getAddressSpace())); |
| 128 | case Type::OverflowBehavior: |
| 129 | return convertType(QT: cast<OverflowBehaviorType>(Val&: QT)->getUnderlyingType()); |
| 130 | case Type::Auto: |
| 131 | case Type::DeducedTemplateSpecialization: |
| 132 | case Type::FunctionProto: |
| 133 | case Type::FunctionNoProto: |
| 134 | case Type::HLSLAttributedResource: |
| 135 | case Type::HLSLInlineSpirv: |
| 136 | llvm::reportFatalInternalError(reason: "Type not supported in ABI lowering" ); |
| 137 | } |
| 138 | llvm_unreachable("unhandled type class in convertTypeImpl" ); |
| 139 | } |
| 140 | |
| 141 | /// Converts C/C++ builtin types to LLVM ABI types. |
| 142 | /// This handles all fundamental scalar types including integers, floats, |
| 143 | /// and special types like void and bool. |
| 144 | const llvm::abi::Type * |
| 145 | QualTypeMapper::convertBuiltinType(const BuiltinType *BT) { |
| 146 | QualType QT(BT, 0); |
| 147 | |
| 148 | switch (BT->getKind()) { |
| 149 | case BuiltinType::Void: |
| 150 | return Builder.getVoidType(); |
| 151 | |
| 152 | case BuiltinType::NullPtr: |
| 153 | return createPointerTypeForPointee(PointeeType: QT); |
| 154 | |
| 155 | case BuiltinType::Bool: |
| 156 | return Builder.getIntegerType(BitWidth: 1, Align: getTypeAlign(QT), /*Signed=*/false, |
| 157 | /*IsBitInt=*/false); |
| 158 | |
| 159 | case BuiltinType::Char_S: |
| 160 | case BuiltinType::Char_U: |
| 161 | case BuiltinType::SChar: |
| 162 | case BuiltinType::UChar: |
| 163 | case BuiltinType::WChar_S: |
| 164 | case BuiltinType::WChar_U: |
| 165 | case BuiltinType::Char8: |
| 166 | case BuiltinType::Char16: |
| 167 | case BuiltinType::Char32: |
| 168 | case BuiltinType::Short: |
| 169 | case BuiltinType::UShort: |
| 170 | case BuiltinType::Int: |
| 171 | case BuiltinType::UInt: |
| 172 | case BuiltinType::Long: |
| 173 | case BuiltinType::ULong: |
| 174 | case BuiltinType::LongLong: |
| 175 | case BuiltinType::ULongLong: |
| 176 | case BuiltinType::Int128: |
| 177 | case BuiltinType::UInt128: |
| 178 | return Builder.getIntegerType(BitWidth: ASTCtx.getTypeSize(T: QT), Align: getTypeAlign(QT), |
| 179 | /*Signed=*/BT->isSignedInteger(), |
| 180 | /*IsBitInt=*/false); |
| 181 | |
| 182 | case BuiltinType::Half: |
| 183 | case BuiltinType::Float16: |
| 184 | case BuiltinType::BFloat16: |
| 185 | case BuiltinType::Float: |
| 186 | case BuiltinType::Double: |
| 187 | case BuiltinType::LongDouble: |
| 188 | case BuiltinType::Float128: |
| 189 | return Builder.getFloatType(Semantics: ASTCtx.getFloatTypeSemantics(T: QT), |
| 190 | Align: getTypeAlign(QT)); |
| 191 | |
| 192 | // TODO: IBM 128-bit extended double |
| 193 | case BuiltinType::Ibm128: |
| 194 | llvm::reportFatalInternalError( |
| 195 | reason: "IBM128 is not yet supported in the ABI lowering libary" ); |
| 196 | |
| 197 | // TODO: Fixed-point types |
| 198 | case BuiltinType::ShortAccum: |
| 199 | case BuiltinType::Accum: |
| 200 | case BuiltinType::LongAccum: |
| 201 | case BuiltinType::UShortAccum: |
| 202 | case BuiltinType::UAccum: |
| 203 | case BuiltinType::ULongAccum: |
| 204 | case BuiltinType::ShortFract: |
| 205 | case BuiltinType::Fract: |
| 206 | case BuiltinType::LongFract: |
| 207 | case BuiltinType::UShortFract: |
| 208 | case BuiltinType::UFract: |
| 209 | case BuiltinType::ULongFract: |
| 210 | case BuiltinType::SatShortAccum: |
| 211 | case BuiltinType::SatAccum: |
| 212 | case BuiltinType::SatLongAccum: |
| 213 | case BuiltinType::SatUShortAccum: |
| 214 | case BuiltinType::SatUAccum: |
| 215 | case BuiltinType::SatULongAccum: |
| 216 | case BuiltinType::SatShortFract: |
| 217 | case BuiltinType::SatFract: |
| 218 | case BuiltinType::SatLongFract: |
| 219 | case BuiltinType::SatUShortFract: |
| 220 | case BuiltinType::SatUFract: |
| 221 | case BuiltinType::SatULongFract: |
| 222 | llvm::reportFatalInternalError( |
| 223 | reason: "Fixed Point types not yet implemented in the ABI lowering library" ); |
| 224 | |
| 225 | // OpenCL image types are represented as opaque pointers. |
| 226 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 227 | case BuiltinType::Id: |
| 228 | #include "clang/Basic/OpenCLImageTypes.def" |
| 229 | // OpenCL extension types are represented as opaque pointers. |
| 230 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) case BuiltinType::Id: |
| 231 | #include "clang/Basic/OpenCLExtensionTypes.def" |
| 232 | case BuiltinType::OCLSampler: |
| 233 | case BuiltinType::OCLEvent: |
| 234 | case BuiltinType::OCLClkEvent: |
| 235 | case BuiltinType::OCLQueue: |
| 236 | case BuiltinType::OCLReserveID: |
| 237 | return createPointerTypeForPointee(PointeeType: QT); |
| 238 | |
| 239 | // Objective-C builtin types are represented as opaque pointers. |
| 240 | case BuiltinType::ObjCId: |
| 241 | case BuiltinType::ObjCClass: |
| 242 | case BuiltinType::ObjCSel: |
| 243 | return createPointerTypeForPointee(PointeeType: QT); |
| 244 | |
| 245 | // Target-specific vector/matrix types — not yet implemented. |
| 246 | #define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 247 | #include "clang/Basic/AArch64ACLETypes.def" |
| 248 | llvm::reportFatalInternalError( |
| 249 | reason: "AArch64 SVE types not yet supported in ABI lowering library" ); |
| 250 | #define PPC_VECTOR_TYPE(Name, Id, Size) case BuiltinType::Id: |
| 251 | #include "clang/Basic/PPCTypes.def" |
| 252 | llvm::reportFatalInternalError( |
| 253 | reason: "PPC MMA types not yet supported in ABI lowering library" ); |
| 254 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 255 | #include "clang/Basic/RISCVVTypes.def" |
| 256 | llvm::reportFatalInternalError( |
| 257 | reason: "RISC-V vector types not yet supported in ABI lowering library" ); |
| 258 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 259 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
| 260 | llvm::reportFatalInternalError(reason: "WebAssembly reference types not yet " |
| 261 | "supported in ABI lowering library" ); |
| 262 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: |
| 263 | #include "clang/Basic/AMDGPUTypes.def" |
| 264 | llvm::reportFatalInternalError( |
| 265 | reason: "AMDGPU types not yet supported in ABI lowering library" ); |
| 266 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 267 | #include "clang/Basic/HLSLIntangibleTypes.def" |
| 268 | llvm::reportFatalInternalError( |
| 269 | reason: "HLSL intangible types not yet Supported in ABI lowering library" ); |
| 270 | |
| 271 | // Placeholder types should never reach ABI lowering. |
| 272 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 273 | #define BUILTIN_TYPE(Id, SingletonId) |
| 274 | #include "clang/AST/BuiltinTypes.def" |
| 275 | llvm::reportFatalInternalError( |
| 276 | reason: "Placeholder type should not reach ABI lowering" ); |
| 277 | |
| 278 | case BuiltinType::Dependent: |
| 279 | llvm::reportFatalInternalError( |
| 280 | reason: "Dependent builtin type should not reach ABI lowering" ); |
| 281 | } |
| 282 | llvm_unreachable("unhandled builtin type kind in convertBuiltinType" ); |
| 283 | } |
| 284 | |
| 285 | /// Converts array types to LLVM ABI array representations. |
| 286 | /// Handles different array kinds: constant arrays, incomplete arrays, |
| 287 | /// and variable-length arrays. |
| 288 | /// |
| 289 | /// \param AT The ArrayType to convert |
| 290 | /// \return LLVM ABI ArrayType or PointerType |
| 291 | const llvm::abi::Type * |
| 292 | QualTypeMapper::convertArrayType(const clang::ArrayType *AT) { |
| 293 | const llvm::abi::Type *ElementType = convertType(QT: AT->getElementType()); |
| 294 | uint64_t Size = ASTCtx.getTypeSize(T: AT); |
| 295 | |
| 296 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) { |
| 297 | auto NumElements = CAT->getZExtSize(); |
| 298 | return Builder.getArrayType(ElementType, NumElements, SizeInBits: Size); |
| 299 | } |
| 300 | if (isa<IncompleteArrayType>(Val: AT)) |
| 301 | return Builder.getArrayType(ElementType, NumElements: 0, SizeInBits: 0); |
| 302 | if (const auto *VAT = dyn_cast<VariableArrayType>(Val: AT)) |
| 303 | return createPointerTypeForPointee(PointeeType: VAT->getPointeeType()); |
| 304 | llvm::reportFatalInternalError( |
| 305 | reason: "unexpected array type in ABI lowering (dependent array types should be " |
| 306 | "resolved before reaching this point)" ); |
| 307 | } |
| 308 | |
| 309 | const llvm::abi::Type *QualTypeMapper::convertVectorType(const VectorType *VT) { |
| 310 | const llvm::abi::Type *ElementType = convertType(QT: VT->getElementType()); |
| 311 | QualType VectorQualType(VT, 0); |
| 312 | |
| 313 | unsigned NElems = VT->getNumElements(); |
| 314 | llvm::ElementCount NumElements = llvm::ElementCount::getFixed(MinVal: NElems); |
| 315 | llvm::Align VectorAlign = getTypeAlign(QT: VectorQualType); |
| 316 | |
| 317 | return Builder.getVectorType(ElementType, NumElements, Align: VectorAlign); |
| 318 | } |
| 319 | |
| 320 | /// Converts complex types to LLVM ABI complex representations. |
| 321 | /// Complex types consist of two components of the element type |
| 322 | /// (real and imaginary parts). |
| 323 | /// |
| 324 | /// \param CT The ComplexType to convert |
| 325 | /// \return LLVM ABI ComplexType with element type and alignment |
| 326 | const llvm::abi::Type * |
| 327 | QualTypeMapper::convertComplexType(const ComplexType *CT) { |
| 328 | const llvm::abi::Type *ElementType = convertType(QT: CT->getElementType()); |
| 329 | llvm::Align ComplexAlign = getTypeAlign(QT: QualType(CT, 0)); |
| 330 | |
| 331 | return Builder.getComplexType(ElementType, Align: ComplexAlign); |
| 332 | } |
| 333 | |
| 334 | /// Converts member pointer types to LLVM ABI representations. |
| 335 | /// Member pointers have different layouts depending on whether they |
| 336 | /// point to functions or data members. |
| 337 | /// |
| 338 | /// \param MPT The MemberPointerType to convert |
| 339 | /// \return LLVM ABI MemberPointerType |
| 340 | const llvm::abi::Type * |
| 341 | QualTypeMapper::convertMemberPointerType(const clang::MemberPointerType *MPT) { |
| 342 | QualType QT(MPT, 0); |
| 343 | uint64_t Size = ASTCtx.getTypeSize(T: QT); |
| 344 | llvm::Align Align = getTypeAlign(QT); |
| 345 | |
| 346 | bool IsFunctionPointer = MPT->isMemberFunctionPointerType(); |
| 347 | |
| 348 | return Builder.getMemberPointerType(IsFunctionPointer, SizeInBits: Size, Align); |
| 349 | } |
| 350 | |
| 351 | /// Converts record types (struct/class/union) to LLVM ABI representations. |
| 352 | /// This is the main dispatch method that handles different record kinds |
| 353 | /// and delegates to specialized converters. |
| 354 | /// |
| 355 | /// \param RT The RecordType to convert |
| 356 | /// \return LLVM ABI RecordType |
| 357 | const llvm::abi::Type *QualTypeMapper::convertRecordType(const RecordType *RT) { |
| 358 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 359 | if (!RD) |
| 360 | return Builder.getRecordType(Fields: {}, Size: llvm::TypeSize::getFixed(ExactSize: 0), |
| 361 | Align: llvm::Align(1)); |
| 362 | |
| 363 | if (RD->isUnion()) |
| 364 | return convertUnionType(RD); |
| 365 | |
| 366 | // Handle C++ classes with base classes |
| 367 | auto *CXXRd = dyn_cast<CXXRecordDecl>(Val: RD); |
| 368 | if (CXXRd && (CXXRd->getNumBases() > 0 || CXXRd->getNumVBases() > 0)) |
| 369 | return convertCXXRecordType(RD: CXXRd); |
| 370 | return convertStructType(RD); |
| 371 | } |
| 372 | |
| 373 | /// Converts C++ classes with inheritance to LLVM ABI struct representations. |
| 374 | /// This method handles the complex layout of C++ objects including: |
| 375 | /// - Virtual table pointers for polymorphic classes |
| 376 | /// - Base class subobjects (both direct and virtual bases) |
| 377 | /// - Member field layout with proper offsets |
| 378 | /// |
| 379 | /// \param RD The C++ record declaration |
| 380 | /// \return LLVM ABI RecordType representing the complete object layout |
| 381 | const llvm::abi::RecordType * |
| 382 | QualTypeMapper::convertCXXRecordType(const CXXRecordDecl *RD) { |
| 383 | const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(D: RD); |
| 384 | SmallVector<llvm::abi::FieldInfo, 16> Fields; |
| 385 | SmallVector<llvm::abi::FieldInfo, 8> BaseClasses; |
| 386 | SmallVector<llvm::abi::FieldInfo, 8> VirtualBaseClasses; |
| 387 | |
| 388 | // Add vtable pointer for polymorphic classes |
| 389 | if (RD->isPolymorphic()) { |
| 390 | const llvm::abi::Type *VtablePointer = |
| 391 | createPointerTypeForPointee(PointeeType: ASTCtx.VoidPtrTy); |
| 392 | Fields.emplace_back(Args&: VtablePointer, Args: 0); |
| 393 | } |
| 394 | |
| 395 | for (const auto &Base : RD->bases()) { |
| 396 | if (Base.isVirtual()) |
| 397 | continue; |
| 398 | |
| 399 | const RecordType *BaseRT = Base.getType()->castAs<RecordType>(); |
| 400 | const llvm::abi::Type *BaseType = convertType(QT: Base.getType()); |
| 401 | uint64_t BaseOffset = |
| 402 | Layout.getBaseClassOffset(Base: BaseRT->getAsCXXRecordDecl()).getQuantity() * |
| 403 | 8; |
| 404 | |
| 405 | BaseClasses.emplace_back(Args&: BaseType, Args&: BaseOffset); |
| 406 | } |
| 407 | |
| 408 | for (const auto &VBase : RD->vbases()) { |
| 409 | const RecordType *VBaseRT = VBase.getType()->castAs<RecordType>(); |
| 410 | const llvm::abi::Type *VBaseType = convertType(QT: VBase.getType()); |
| 411 | uint64_t VBaseOffset = |
| 412 | Layout.getVBaseClassOffset(VBase: VBaseRT->getAsCXXRecordDecl()) |
| 413 | .getQuantity() * |
| 414 | 8; |
| 415 | |
| 416 | VirtualBaseClasses.emplace_back(Args&: VBaseType, Args&: VBaseOffset); |
| 417 | } |
| 418 | |
| 419 | computeFieldInfo(RD, Fields, Layout); |
| 420 | |
| 421 | llvm::sort(C&: Fields, |
| 422 | Comp: [](const llvm::abi::FieldInfo &A, const llvm::abi::FieldInfo &B) { |
| 423 | return A.OffsetInBits < B.OffsetInBits; |
| 424 | }); |
| 425 | |
| 426 | llvm::TypeSize Size = |
| 427 | llvm::TypeSize::getFixed(ExactSize: Layout.getSize().getQuantity() * 8); |
| 428 | llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity()); |
| 429 | |
| 430 | llvm::abi::RecordFlags RecFlags = llvm::abi::RecordFlags::IsCXXRecord; |
| 431 | if (RD->isPolymorphic()) |
| 432 | RecFlags |= llvm::abi::RecordFlags::IsPolymorphic; |
| 433 | if (RD->canPassInRegisters()) |
| 434 | RecFlags |= llvm::abi::RecordFlags::CanPassInRegisters; |
| 435 | if (RD->hasFlexibleArrayMember()) |
| 436 | RecFlags |= llvm::abi::RecordFlags::HasFlexibleArrayMember; |
| 437 | |
| 438 | return Builder.getRecordType(Fields, Size, Align: Alignment, |
| 439 | Pack: llvm::abi::StructPacking::Default, BaseClasses, |
| 440 | VirtualBaseClasses, RecFlags); |
| 441 | } |
| 442 | |
| 443 | /// Converts enumeration types to their underlying integer representations. |
| 444 | /// This method handles various enum states and falls back to safe defaults |
| 445 | /// when enum information is incomplete or invalid. |
| 446 | /// |
| 447 | /// \param ET The EnumType to convert |
| 448 | /// \return LLVM ABI IntegerType representing the enum's underlying type |
| 449 | const llvm::abi::Type * |
| 450 | QualTypeMapper::convertEnumType(const clang::EnumType *ET) { |
| 451 | const EnumDecl *ED = ET->getDecl(); |
| 452 | QualType UnderlyingType = ED->getIntegerType(); |
| 453 | |
| 454 | if (UnderlyingType.isNull()) |
| 455 | UnderlyingType = ASTCtx.IntTy; |
| 456 | |
| 457 | return convertType(QT: UnderlyingType); |
| 458 | } |
| 459 | |
| 460 | /// Converts plain C structs and C++ classes without inheritance. |
| 461 | /// This handles the simpler case where we only need to layout member fields |
| 462 | /// without considering base classes or virtual functions. |
| 463 | /// |
| 464 | /// \param RD The RecordDecl to convert |
| 465 | /// \return LLVM ABI RecordType |
| 466 | const llvm::abi::RecordType * |
| 467 | QualTypeMapper::convertStructType(const clang::RecordDecl *RD) { |
| 468 | const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(D: RD); |
| 469 | |
| 470 | bool IsCXXRecord = isa<CXXRecordDecl>(Val: RD); |
| 471 | SmallVector<llvm::abi::FieldInfo, 16> Fields; |
| 472 | computeFieldInfo(RD, Fields, Layout); |
| 473 | |
| 474 | llvm::TypeSize Size = |
| 475 | llvm::TypeSize::getFixed(ExactSize: Layout.getSize().getQuantity() * 8); |
| 476 | llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity()); |
| 477 | |
| 478 | llvm::abi::RecordFlags RecFlags = llvm::abi::RecordFlags::None; |
| 479 | if (IsCXXRecord) |
| 480 | RecFlags |= llvm::abi::RecordFlags::IsCXXRecord; |
| 481 | if (RD->canPassInRegisters()) |
| 482 | RecFlags |= llvm::abi::RecordFlags::CanPassInRegisters; |
| 483 | if (RD->hasFlexibleArrayMember()) |
| 484 | RecFlags |= llvm::abi::RecordFlags::HasFlexibleArrayMember; |
| 485 | |
| 486 | return Builder.getRecordType(Fields, Size, Align: Alignment, |
| 487 | Pack: llvm::abi::StructPacking::Default, BaseClasses: {}, VirtualBaseClasses: {}, |
| 488 | RecFlags); |
| 489 | } |
| 490 | |
| 491 | /// Converts C union types where all fields occupy the same memory location. |
| 492 | /// The union size is determined by its largest member, and all fields |
| 493 | /// start at offset 0. |
| 494 | /// |
| 495 | /// \param RD The RecordDecl representing the union |
| 496 | /// \return LLVM ABI UnionType |
| 497 | const llvm::abi::RecordType * |
| 498 | QualTypeMapper::convertUnionType(const clang::RecordDecl *RD) { |
| 499 | const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(D: RD); |
| 500 | |
| 501 | SmallVector<llvm::abi::FieldInfo, 16> AllFields; |
| 502 | computeFieldInfo(RD, Fields&: AllFields, Layout); |
| 503 | |
| 504 | llvm::TypeSize Size = |
| 505 | llvm::TypeSize::getFixed(ExactSize: Layout.getSize().getQuantity() * 8); |
| 506 | llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity()); |
| 507 | |
| 508 | llvm::abi::RecordFlags RecFlags = llvm::abi::RecordFlags::None; |
| 509 | if (RD->hasAttr<TransparentUnionAttr>()) |
| 510 | RecFlags |= llvm::abi::RecordFlags::IsTransparent; |
| 511 | if (RD->canPassInRegisters()) |
| 512 | RecFlags |= llvm::abi::RecordFlags::CanPassInRegisters; |
| 513 | if (isa<CXXRecordDecl>(Val: RD)) |
| 514 | RecFlags |= llvm::abi::RecordFlags::IsCXXRecord; |
| 515 | |
| 516 | return Builder.getUnionType(Fields: AllFields, Size, Align: Alignment, |
| 517 | Pack: llvm::abi::StructPacking::Default, RecFlags); |
| 518 | } |
| 519 | |
| 520 | llvm::Align QualTypeMapper::getTypeAlign(QualType QT) const { |
| 521 | |
| 522 | return llvm::Align(ASTCtx.getTypeAlignInChars(T: QT).getQuantity()); |
| 523 | } |
| 524 | |
| 525 | const llvm::abi::Type * |
| 526 | QualTypeMapper::createPointerTypeForPointee(QualType PointeeType) { |
| 527 | auto AddrSpace = PointeeType.getAddressSpace(); |
| 528 | auto PointerSize = ASTCtx.getTargetInfo().getPointerWidth(AddrSpace); |
| 529 | llvm::Align Alignment = |
| 530 | llvm::Align(ASTCtx.getTargetInfo().getPointerAlign(AddrSpace)); |
| 531 | // Function types without an explicit address space qualifier use the program |
| 532 | // address space, which may differ from the default data address space on |
| 533 | // targets like AMDGPU. |
| 534 | unsigned TargetAddrSpace = |
| 535 | PointeeType->isFunctionType() && !PointeeType.hasAddressSpace() |
| 536 | ? DL.getProgramAddressSpace() |
| 537 | : ASTCtx.getTargetInfo().getTargetAddressSpace(AS: AddrSpace); |
| 538 | return Builder.getPointerType(Size: PointerSize, Align: llvm::Align(Alignment.value() / 8), |
| 539 | Addrspace: TargetAddrSpace); |
| 540 | } |
| 541 | |
| 542 | /// Processes the fields of a record (struct/class/union) and populates |
| 543 | /// the Fields vector with FieldInfo objects containing type, offset, |
| 544 | /// and bitfield information. |
| 545 | /// |
| 546 | /// \param RD The RecordDecl whose fields to process |
| 547 | /// \param Fields Output vector to populate with field information |
| 548 | /// \param Layout The AST record layout containing field offset information |
| 549 | void QualTypeMapper::computeFieldInfo( |
| 550 | const RecordDecl *RD, SmallVectorImpl<llvm::abi::FieldInfo> &Fields, |
| 551 | const ASTRecordLayout &Layout) { |
| 552 | unsigned FieldIndex = 0; |
| 553 | |
| 554 | for (const auto *FD : RD->fields()) { |
| 555 | const llvm::abi::Type *FieldType = convertType(QT: FD->getType()); |
| 556 | uint64_t OffsetInBits = Layout.getFieldOffset(FieldNo: FieldIndex); |
| 557 | |
| 558 | bool IsBitField = FD->isBitField(); |
| 559 | uint64_t BitFieldWidth = 0; |
| 560 | bool IsUnnamedBitField = false; |
| 561 | |
| 562 | if (IsBitField) { |
| 563 | BitFieldWidth = FD->getBitWidthValue(); |
| 564 | IsUnnamedBitField = FD->isUnnamedBitField(); |
| 565 | } |
| 566 | |
| 567 | Fields.emplace_back(Args&: FieldType, Args&: OffsetInBits, Args&: IsBitField, Args&: BitFieldWidth, |
| 568 | Args&: IsUnnamedBitField); |
| 569 | ++FieldIndex; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | } // namespace CodeGen |
| 574 | } // namespace clang |
| 575 | |