| 1 | //==---- QualTypeMapper.h - 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 | #ifndef CLANG_LIB_CODEGEN_QUALTYPE_MAPPER_H |
| 18 | #define CLANG_LIB_CODEGEN_QUALTYPE_MAPPER_H |
| 19 | |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/Type.h" |
| 23 | #include "clang/AST/TypeOrdering.h" |
| 24 | #include "llvm/ABI/Types.h" |
| 25 | #include "llvm/ADT/DenseMap.h" |
| 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/Support/Allocator.h" |
| 28 | |
| 29 | namespace clang { |
| 30 | namespace CodeGen { |
| 31 | |
| 32 | class QualTypeMapper { |
| 33 | private: |
| 34 | clang::ASTContext &ASTCtx; |
| 35 | const llvm::DataLayout &DL; |
| 36 | llvm::abi::TypeBuilder Builder; |
| 37 | |
| 38 | llvm::DenseMap<QualType, const llvm::abi::Type *> TypeCache; |
| 39 | |
| 40 | const llvm::abi::Type *convertTypeImpl(QualType QT); |
| 41 | const llvm::abi::Type *convertBuiltinType(const clang::BuiltinType *BT); |
| 42 | const llvm::abi::Type *convertArrayType(const clang::ArrayType *AT); |
| 43 | const llvm::abi::Type *convertVectorType(const clang::VectorType *VT); |
| 44 | const llvm::abi::Type *convertRecordType(const clang::RecordType *RT); |
| 45 | const llvm::abi::Type *convertEnumType(const clang::EnumType *ET); |
| 46 | const llvm::abi::Type *convertComplexType(const ComplexType *CT); |
| 47 | const llvm::abi::Type * |
| 48 | convertMemberPointerType(const clang::MemberPointerType *MPT); |
| 49 | const llvm::abi::Type *convertMatrixType(const ConstantMatrixType *MT); |
| 50 | |
| 51 | const llvm::abi::RecordType *convertStructType(const clang::RecordDecl *RD); |
| 52 | const llvm::abi::RecordType *convertUnionType(const clang::RecordDecl *RD); |
| 53 | const llvm::abi::Type *createPointerTypeForPointee(QualType PointeeType); |
| 54 | const llvm::abi::RecordType *convertCXXRecordType(const CXXRecordDecl *RD); |
| 55 | |
| 56 | void computeFieldInfo(const clang::RecordDecl *RD, |
| 57 | SmallVectorImpl<llvm::abi::FieldInfo> &Fields, |
| 58 | const clang::ASTRecordLayout &Layout); |
| 59 | |
| 60 | llvm::TypeSize getTypeSize(clang::QualType QT) const; |
| 61 | llvm::Align getTypeAlign(clang::QualType QT) const; |
| 62 | uint64_t getPointerSize() const; |
| 63 | uint64_t getPointerAlign() const; |
| 64 | |
| 65 | public: |
| 66 | explicit QualTypeMapper(clang::ASTContext &Ctx, const llvm::DataLayout &DL, |
| 67 | llvm::BumpPtrAllocator &Alloc) |
| 68 | : ASTCtx(Ctx), DL(DL), Builder(Alloc) {} |
| 69 | |
| 70 | const llvm::abi::Type *convertType(clang::QualType QT); |
| 71 | |
| 72 | void clearCache() { TypeCache.clear(); } |
| 73 | |
| 74 | llvm::abi::TypeBuilder getTypeBuilder() { return Builder; } |
| 75 | }; |
| 76 | |
| 77 | } // namespace CodeGen |
| 78 | } // namespace clang |
| 79 | |
| 80 | #endif // CLANG_LIB_CODEGEN_QUALTYPE_MAPPER_H |
| 81 | |