Debian Codegraph @

clang/lib/CIR/CodeGen/CIRGenTypes.h

raw
1 //===--- CIRGenTypes.h - Type translation for CIR CodeGen -------*- C++ -*-===//
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 // This is the code that handles AST -> CIR type lowering.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15
16 #include "ABIInfo.h"
17 #include "CIRGenFunctionInfo.h"
18 #include "CIRGenRecordLayout.h"
19
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/ABI.h"
23 #include "clang/CIR/Dialect/IR/CIRTypes.h"
24
25 #include "llvm/ADT/SmallPtrSet.h"
26
27 namespace clang {
28 class ASTContext;
29 class FunctionType;
30 class GlobalDecl;
31 class QualType;
32 class Type;
33 } // namespace clang
34
35 namespace mlir {
36 class Type;
37 }
38
39 namespace clang::CIRGen {
40
41 class CallArgList;
42 class CIRGenBuilderTy;
43 class CIRGenCXXABI;
44 class CIRGenModule;
45
46 /// This class organizes the cross-module state that is used while lowering
47 /// AST types to CIR types.
48 class CIRGenTypes {
49 CIRGenModule &cgm;
50 clang::ASTContext &astContext;
51 CIRGenBuilderTy &builder;
52 CIRGenCXXABI &theCXXABI;
53
54 const ABIInfo &theABIInfo;
55
56 /// Contains the CIR type for any converted RecordDecl.
57 llvm::DenseMap<const clang::Type *, std::unique_ptr<CIRGenRecordLayout>>
58 cirGenRecordLayouts;
59
60 /// Contains the CIR type for any converted RecordDecl
61 llvm::DenseMap<const clang::Type *, cir::RecordType> recordDeclTypes;
62
63 /// Hold memoized CIRGenFunctionInfo results
64 llvm::FoldingSet<CIRGenFunctionInfo> functionInfos;
65
66 /// This set keeps track of records that we're currently converting to a CIR
67 /// type. For example, when converting:
68 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
69 /// types will be in this set.
70 llvm::SmallPtrSet<const clang::Type *, 4> recordsBeingLaidOut;
71
72 llvm::SmallVector<const clang::RecordDecl *, 8> deferredRecords;
73
74 /// Heper for convertType.
75 mlir::Type convertFunctionTypeInternal(clang::QualType ft);
76
77 public:
78 CIRGenTypes(CIRGenModule &cgm);
79 ~CIRGenTypes();
80
81 CIRGenBuilderTy &getBuilder() const { return builder; }
82 CIRGenModule &getCGModule() const { return cgm; }
83
84 /// Utility to check whether a function type can be converted to a CIR type
85 /// (i.e. doesn't depend on an incomplete tag type).
86 bool isFuncTypeConvertible(const clang::FunctionType *ft);
87 bool isFuncParamTypeConvertible(clang::QualType type);
88
89 /// Derives the 'this' type for CIRGen purposes, i.e. ignoring method CVR
90 /// qualification.
91 clang::CanQualType deriveThisType(const clang::CXXRecordDecl *rd,
92 const clang::CXXMethodDecl *md);
93
94 /// This map of clang::Type to mlir::Type (which includes CIR type) is a
95 /// cache of types that have already been processed.
96 using TypeCacheTy = llvm::DenseMap<const clang::Type *, mlir::Type>;
97 TypeCacheTy typeCache;
98
99 mlir::MLIRContext &getMLIRContext() const;
100 clang::ASTContext &getASTContext() const { return astContext; }
101
102 bool isRecordLayoutComplete(const clang::Type *ty) const;
103 bool noRecordsBeingLaidOut() const { return recordsBeingLaidOut.empty(); }
104 bool isRecordBeingLaidOut(const clang::Type *ty) const {
105 return recordsBeingLaidOut.count(ty);
106 }
107
108 const ABIInfo &getABIInfo() const { return theABIInfo; }
109
110 /// Convert a Clang type into a mlir::Type.
111 mlir::Type convertType(clang::QualType type);
112
113 mlir::Type convertRecordDeclType(const clang::RecordDecl *recordDecl);
114
115 std::unique_ptr<CIRGenRecordLayout>
116 computeRecordLayout(const clang::RecordDecl *rd, cir::RecordType *ty);
117
118 std::string getRecordTypeName(const clang::RecordDecl *,
119 llvm::StringRef suffix);
120
121 const CIRGenRecordLayout &getCIRGenRecordLayout(const clang::RecordDecl *rd);
122
123 /// Convert type T into an mlir::Type. This differs from convertType in that
124 /// it is used to convert to the memory representation for a type. For
125 /// example, the scalar representation for bool is i1, but the memory
126 /// representation is usually i8 or i32, depending on the target.
127 // TODO: convert this comment to account for MLIR's equivalence
128 mlir::Type convertTypeForMem(clang::QualType, bool forBitField = false);
129
130 /// Get the CIR function type for \arg Info.
131 cir::FuncType getFunctionType(const CIRGenFunctionInfo &info);
132
133 cir::FuncType getFunctionType(clang::GlobalDecl gd);
134
135 /// Get the CIR function type for use in a vtable, given a CXXMethodDecl. If
136 /// the method has an incomplete return type, and/or incomplete argument
137 /// types, this will return the opaque type.
138 cir::FuncType getFunctionTypeForVTable(clang::GlobalDecl gd);
139
140 // The arrangement methods are split into three families:
141 // - those meant to drive the signature and prologue/epilogue
142 // of a function declaration or definition,
143 // - those meant for the computation of the CIR type for an abstract
144 // appearance of a function, and
145 // - those meant for performing the CIR-generation of a call.
146 // They differ mainly in how they deal with optional (i.e. variadic)
147 // arguments, as well as unprototyped functions.
148 //
149 // Key points:
150 // - The CIRGenFunctionInfo for emitting a specific call site must include
151 // entries for the optional arguments.
152 // - The function type used at the call site must reflect the formal
153 // signature
154 // of the declaration being called, or else the call will go away.
155 // - For the most part, unprototyped functions are called by casting to a
156 // formal signature inferred from the specific argument types used at the
157 // call-site. However, some targets (e.g. x86-64) screw with this for
158 // compatability reasons.
159
160 const CIRGenFunctionInfo &arrangeGlobalDeclaration(GlobalDecl gd);
161
162 /// UpdateCompletedType - when we find the full definition for a TagDecl,
163 /// replace the 'opaque' type we previously made for it if applicable.
164 void updateCompletedType(const clang::TagDecl *td);
165
166 /// Free functions are functions that are compatible with an ordinary C
167 /// function pointer type.
168 const CIRGenFunctionInfo &
169 arrangeFunctionDeclaration(const clang::FunctionDecl *fd);
170
171 /// Return whether a type can be zero-initialized (in the C++ sense) with an
172 /// LLVM zeroinitializer.
173 bool isZeroInitializable(clang::QualType ty);
174 bool isZeroInitializable(const RecordDecl *rd);
175
176 const CIRGenFunctionInfo &arrangeCXXConstructorCall(
177 const CallArgList &args, const clang::CXXConstructorDecl *d,
178 clang::CXXCtorType ctorKind, unsigned extraPrefixArgs,
179 unsigned extraSuffixArgs, bool passProtoArgs = true);
180
181 const CIRGenFunctionInfo &
182 arrangeCXXMethodCall(const CallArgList &args,
183 const clang::FunctionProtoType *type,
184 RequiredArgs required, unsigned numPrefixArgs);
185
186 /// C++ methods have some special rules and also have implicit parameters.
187 const CIRGenFunctionInfo &
188 arrangeCXXMethodDeclaration(const clang::CXXMethodDecl *md);
189 const CIRGenFunctionInfo &arrangeCXXStructorDeclaration(clang::GlobalDecl gd);
190
191 const CIRGenFunctionInfo &
192 arrangeCXXMethodType(const clang::CXXRecordDecl *rd,
193 const clang::FunctionProtoType *ftp,
194 const clang::CXXMethodDecl *md);
195
196 const CIRGenFunctionInfo &arrangeFreeFunctionCall(const CallArgList &args,
197 const FunctionType *fnType);
198
199 const CIRGenFunctionInfo &
200 arrangeCIRFunctionInfo(CanQualType returnType,
201 llvm::ArrayRef<CanQualType> argTypes,
202 RequiredArgs required);
203
204 const CIRGenFunctionInfo &
205 arrangeFreeFunctionType(CanQual<FunctionProtoType> fpt);
206 const CIRGenFunctionInfo &
207 arrangeFreeFunctionType(CanQual<FunctionNoProtoType> fnpt);
208 };
209
210 } // namespace clang::CIRGen
211
212 #endif