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