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