xref: /llvm-project/clang/lib/CodeGen/CodeGenTypes.h (revision b92d290e48e908c083976ef3a807ed1fcc9bf209)
1 //===--- CodeGenTypes.h - Type translation for LLVM 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 -> LLVM type lowering.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15 
16 #include "CGCall.h"
17 #include "clang/Basic/ABI.h"
18 #include "clang/CodeGen/CGFunctionInfo.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/Module.h"
21 
22 namespace llvm {
23 class FunctionType;
24 class DataLayout;
25 class Type;
26 class LLVMContext;
27 class StructType;
28 }
29 
30 namespace clang {
31 class ASTContext;
32 template <typename> class CanQual;
33 class CXXConstructorDecl;
34 class CXXDestructorDecl;
35 class CXXMethodDecl;
36 class CodeGenOptions;
37 class FieldDecl;
38 class FunctionProtoType;
39 class ObjCInterfaceDecl;
40 class ObjCIvarDecl;
41 class PointerType;
42 class QualType;
43 class RecordDecl;
44 class TagDecl;
45 class TargetInfo;
46 class Type;
47 typedef CanQual<Type> CanQualType;
48 class GlobalDecl;
49 
50 namespace CodeGen {
51 class ABIInfo;
52 class CGCXXABI;
53 class CGRecordLayout;
54 class CodeGenModule;
55 class RequiredArgs;
56 
57 enum class StructorType {
58   Complete, // constructor or destructor
59   Base,     // constructor or destructor
60   Deleting  // destructor only
61 };
62 
63 inline CXXCtorType toCXXCtorType(StructorType T) {
64   switch (T) {
65   case StructorType::Complete:
66     return Ctor_Complete;
67   case StructorType::Base:
68     return Ctor_Base;
69   case StructorType::Deleting:
70     llvm_unreachable("cannot have a deleting ctor");
71   }
72   llvm_unreachable("not a StructorType");
73 }
74 
75 inline StructorType getFromCtorType(CXXCtorType T) {
76   switch (T) {
77   case Ctor_Complete:
78     return StructorType::Complete;
79   case Ctor_Base:
80     return StructorType::Base;
81   case Ctor_Comdat:
82     llvm_unreachable("not expecting a COMDAT");
83   case Ctor_CopyingClosure:
84   case Ctor_DefaultClosure:
85     llvm_unreachable("not expecting a closure");
86   }
87   llvm_unreachable("not a CXXCtorType");
88 }
89 
90 inline CXXDtorType toCXXDtorType(StructorType T) {
91   switch (T) {
92   case StructorType::Complete:
93     return Dtor_Complete;
94   case StructorType::Base:
95     return Dtor_Base;
96   case StructorType::Deleting:
97     return Dtor_Deleting;
98   }
99   llvm_unreachable("not a StructorType");
100 }
101 
102 inline StructorType getFromDtorType(CXXDtorType T) {
103   switch (T) {
104   case Dtor_Deleting:
105     return StructorType::Deleting;
106   case Dtor_Complete:
107     return StructorType::Complete;
108   case Dtor_Base:
109     return StructorType::Base;
110   case Dtor_Comdat:
111     llvm_unreachable("not expecting a COMDAT");
112   }
113   llvm_unreachable("not a CXXDtorType");
114 }
115 
116 /// This class organizes the cross-module state that is used while lowering
117 /// AST types to LLVM types.
118 class CodeGenTypes {
119   CodeGenModule &CGM;
120   // Some of this stuff should probably be left on the CGM.
121   ASTContext &Context;
122   llvm::Module &TheModule;
123   const TargetInfo &Target;
124   CGCXXABI &TheCXXABI;
125 
126   // This should not be moved earlier, since its initialization depends on some
127   // of the previous reference members being already initialized
128   const ABIInfo &TheABIInfo;
129 
130   /// The opaque type map for Objective-C interfaces. All direct
131   /// manipulation is done by the runtime interfaces, which are
132   /// responsible for coercing to the appropriate type; these opaque
133   /// types are never refined.
134   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
135 
136   /// Maps clang struct type with corresponding record layout info.
137   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
138 
139   /// Contains the LLVM IR type for any converted RecordDecl.
140   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
141 
142   /// Hold memoized CGFunctionInfo results.
143   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
144 
145   /// This set keeps track of records that we're currently converting
146   /// to an IR type.  For example, when converting:
147   /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
148   /// types will be in this set.
149   llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
150 
151   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
152 
153   /// True if we didn't layout a function due to a being inside
154   /// a recursive struct conversion, set this to true.
155   bool SkippedLayout;
156 
157   SmallVector<const RecordDecl *, 8> DeferredRecords;
158 
159   /// This map keeps cache of llvm::Types and maps clang::Type to
160   /// corresponding llvm::Type.
161   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
162 
163   llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
164 
165   /// Helper for ConvertType.
166   llvm::Type *ConvertFunctionTypeInternal(QualType FT);
167 
168 public:
169   CodeGenTypes(CodeGenModule &cgm);
170   ~CodeGenTypes();
171 
172   const llvm::DataLayout &getDataLayout() const {
173     return TheModule.getDataLayout();
174   }
175   ASTContext &getContext() const { return Context; }
176   const ABIInfo &getABIInfo() const { return TheABIInfo; }
177   const TargetInfo &getTarget() const { return Target; }
178   CGCXXABI &getCXXABI() const { return TheCXXABI; }
179   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
180   const CodeGenOptions &getCodeGenOpts() const;
181 
182   /// Convert clang calling convention to LLVM callilng convention.
183   unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
184 
185   /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
186   /// qualification.
187   CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD);
188 
189   /// ConvertType - Convert type T into a llvm::Type.
190   llvm::Type *ConvertType(QualType T);
191 
192   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
193   /// ConvertType in that it is used to convert to the memory representation for
194   /// a type.  For example, the scalar representation for _Bool is i1, but the
195   /// memory representation is usually i8 or i32, depending on the target.
196   llvm::Type *ConvertTypeForMem(QualType T);
197 
198   /// GetFunctionType - Get the LLVM function type for \arg Info.
199   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
200 
201   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
202 
203   /// isFuncTypeConvertible - Utility to check whether a function type can
204   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
205   /// type).
206   bool isFuncTypeConvertible(const FunctionType *FT);
207   bool isFuncParamTypeConvertible(QualType Ty);
208 
209   /// Determine if a C++ inheriting constructor should have parameters matching
210   /// those of its inherited constructor.
211   bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
212                                CXXCtorType Type);
213 
214   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
215   /// given a CXXMethodDecl. If the method to has an incomplete return type,
216   /// and/or incomplete argument types, this will return the opaque type.
217   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
218 
219   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
220 
221   /// UpdateCompletedType - When we find the full definition for a TagDecl,
222   /// replace the 'opaque' type we previously made for it if applicable.
223   void UpdateCompletedType(const TagDecl *TD);
224 
225   /// Remove stale types from the type cache when an inheritance model
226   /// gets assigned to a class.
227   void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
228 
229   // The arrangement methods are split into three families:
230   //   - those meant to drive the signature and prologue/epilogue
231   //     of a function declaration or definition,
232   //   - those meant for the computation of the LLVM type for an abstract
233   //     appearance of a function, and
234   //   - those meant for performing the IR-generation of a call.
235   // They differ mainly in how they deal with optional (i.e. variadic)
236   // arguments, as well as unprototyped functions.
237   //
238   // Key points:
239   // - The CGFunctionInfo for emitting a specific call site must include
240   //   entries for the optional arguments.
241   // - The function type used at the call site must reflect the formal
242   //   signature of the declaration being called, or else the call will
243   //   go awry.
244   // - For the most part, unprototyped functions are called by casting to
245   //   a formal signature inferred from the specific argument types used
246   //   at the call-site.  However, some targets (e.g. x86-64) screw with
247   //   this for compatibility reasons.
248 
249   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
250 
251   /// Given a function info for a declaration, return the function info
252   /// for a call with the given arguments.
253   ///
254   /// Often this will be able to simply return the declaration info.
255   const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
256                                     const CallArgList &args);
257 
258   /// Free functions are functions that are compatible with an ordinary
259   /// C function pointer type.
260   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
261   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
262                                                 const FunctionType *Ty,
263                                                 bool ChainCall);
264   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
265   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
266 
267   /// A nullary function is a freestanding function of type 'void ()'.
268   /// This method works for both calls and declarations.
269   const CGFunctionInfo &arrangeNullaryFunction();
270 
271   /// A builtin function is a freestanding function using the default
272   /// C conventions.
273   const CGFunctionInfo &
274   arrangeBuiltinFunctionDeclaration(QualType resultType,
275                                     const FunctionArgList &args);
276   const CGFunctionInfo &
277   arrangeBuiltinFunctionDeclaration(CanQualType resultType,
278                                     ArrayRef<CanQualType> argTypes);
279   const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
280                                                    const CallArgList &args);
281 
282   /// Objective-C methods are C functions with some implicit parameters.
283   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
284   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
285                                                         QualType receiverType);
286   const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
287                                                      QualType returnType,
288                                                      const CallArgList &args);
289 
290   /// Block invocation functions are C functions with an implicit parameter.
291   const CGFunctionInfo &arrangeBlockFunctionDeclaration(
292                                                  const FunctionProtoType *type,
293                                                  const FunctionArgList &args);
294   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
295                                                  const FunctionType *type);
296 
297   /// C++ methods have some special rules and also have implicit parameters.
298   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
299   const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
300                                                       StructorType Type);
301   const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
302                                                   const CXXConstructorDecl *D,
303                                                   CXXCtorType CtorKind,
304                                                   unsigned ExtraPrefixArgs,
305                                                   unsigned ExtraSuffixArgs,
306                                                   bool PassProtoArgs = true);
307 
308   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
309                                              const FunctionProtoType *type,
310                                              RequiredArgs required,
311                                              unsigned numPrefixArgs);
312   const CGFunctionInfo &
313   arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
314   const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
315                                                  CXXCtorType CT);
316   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
317                                              const FunctionProtoType *FTP,
318                                              const CXXMethodDecl *MD);
319 
320   /// "Arrange" the LLVM information for a call or type with the given
321   /// signature.  This is largely an internal method; other clients
322   /// should use one of the above routines, which ultimately defer to
323   /// this.
324   ///
325   /// \param argTypes - must all actually be canonical as params
326   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
327                                                 bool instanceMethod,
328                                                 bool chainCall,
329                                                 ArrayRef<CanQualType> argTypes,
330                                                 FunctionType::ExtInfo info,
331                     ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
332                                                 RequiredArgs args);
333 
334   /// Compute a new LLVM record layout object for the given record.
335   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
336                                       llvm::StructType *Ty);
337 
338   /// addRecordTypeName - Compute a name from the given record decl with an
339   /// optional suffix and name the given LLVM type using it.
340   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
341                          StringRef suffix);
342 
343 
344 public:  // These are internal details of CGT that shouldn't be used externally.
345   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
346   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
347 
348   /// getExpandedTypes - Expand the type \arg Ty into the LLVM
349   /// argument types it would be passed as. See ABIArgInfo::Expand.
350   void getExpandedTypes(QualType Ty,
351                         SmallVectorImpl<llvm::Type *>::iterator &TI);
352 
353   /// IsZeroInitializable - Return whether a type can be
354   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
355   bool isZeroInitializable(QualType T);
356 
357   /// Check if the pointer type can be zero-initialized (in the C++ sense)
358   /// with an LLVM zeroinitializer.
359   bool isPointerZeroInitializable(QualType T);
360 
361   /// IsZeroInitializable - Return whether a record type can be
362   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
363   bool isZeroInitializable(const RecordDecl *RD);
364 
365   bool isRecordLayoutComplete(const Type *Ty) const;
366   bool noRecordsBeingLaidOut() const {
367     return RecordsBeingLaidOut.empty();
368   }
369   bool isRecordBeingLaidOut(const Type *Ty) const {
370     return RecordsBeingLaidOut.count(Ty);
371   }
372 
373 };
374 
375 }  // end namespace CodeGen
376 }  // end namespace clang
377 
378 #endif
379