1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that handles AST -> LLVM type lowering.
12 //===----------------------------------------------------------------------===//
14 #ifndef CLANG_CODEGEN_CODEGENTYPES_H
15 #define CLANG_CODEGEN_CODEGENTYPES_H
18 #include "clang/AST/GlobalDecl.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/Module.h"
35 template <typename> class CanQual;
36 class CXXConstructorDecl;
37 class CXXDestructorDecl;
41 class FunctionProtoType;
42 class ObjCInterfaceDecl;
50 typedef CanQual<Type> CanQualType;
58 /// CodeGenTypes - This class organizes the cross-module state that is used
59 /// while lowering AST types to LLVM types.
62 // Some of this stuff should probably be left on the CGM.
65 llvm::Module &TheModule;
66 const llvm::DataLayout &TheDataLayout;
67 const TargetInfo &Target;
69 const CodeGenOptions &CodeGenOpts;
71 // This should not be moved earlier, since its initialization depends on some
72 // of the previous reference members being already initialized
73 const ABIInfo &TheABIInfo;
76 /// The opaque type map for Objective-C interfaces. All direct
77 /// manipulation is done by the runtime interfaces, which are
78 /// responsible for coercing to the appropriate type; these opaque
79 /// types are never refined.
80 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
82 /// CGRecordLayouts - This maps llvm struct type with corresponding
83 /// record layout info.
84 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
86 /// RecordDeclTypes - This contains the LLVM IR type for any converted
88 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
90 /// FunctionInfos - Hold memoized CGFunctionInfo results.
91 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
93 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
94 /// converting to an IR type. For example, when converting:
95 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
96 /// types will be in this set.
97 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
99 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
101 /// SkippedLayout - True if we didn't layout a function due to a being inside
102 /// a recursive struct conversion, set this to true.
105 SmallVector<const RecordDecl *, 8> DeferredRecords;
108 /// TypeCache - This map keeps cache of llvm::Types
109 /// and maps llvm::Types to corresponding clang::Type.
110 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
113 CodeGenTypes(CodeGenModule &cgm);
116 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
117 ASTContext &getContext() const { return Context; }
118 const ABIInfo &getABIInfo() const { return TheABIInfo; }
119 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
120 const TargetInfo &getTarget() const { return Target; }
121 CGCXXABI &getCXXABI() const { return TheCXXABI; }
122 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
124 /// ConvertType - Convert type T into a llvm::Type.
125 llvm::Type *ConvertType(QualType T);
127 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
128 /// ConvertType in that it is used to convert to the memory representation for
129 /// a type. For example, the scalar representation for _Bool is i1, but the
130 /// memory representation is usually i8 or i32, depending on the target.
131 llvm::Type *ConvertTypeForMem(QualType T);
133 /// GetFunctionType - Get the LLVM function type for \arg Info.
134 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
136 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
138 /// isFuncTypeConvertible - Utility to check whether a function type can
139 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
141 bool isFuncTypeConvertible(const FunctionType *FT);
142 bool isFuncTypeArgumentConvertible(QualType Ty);
144 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
145 /// given a CXXMethodDecl. If the method to has an incomplete return type,
146 /// and/or incomplete argument types, this will return the opaque type.
147 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
149 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
151 /// UpdateCompletedType - When we find the full definition for a TagDecl,
152 /// replace the 'opaque' type we previously made for it if applicable.
153 void UpdateCompletedType(const TagDecl *TD);
155 /// getNullaryFunctionInfo - Get the function info for a void()
156 /// function with standard CC.
157 const CGFunctionInfo &arrangeNullaryFunction();
159 // The arrangement methods are split into three families:
160 // - those meant to drive the signature and prologue/epilogue
161 // of a function declaration or definition,
162 // - those meant for the computation of the LLVM type for an abstract
163 // appearance of a function, and
164 // - those meant for performing the IR-generation of a call.
165 // They differ mainly in how they deal with optional (i.e. variadic)
166 // arguments, as well as unprototyped functions.
169 // - The CGFunctionInfo for emitting a specific call site must include
170 // entries for the optional arguments.
171 // - The function type used at the call site must reflect the formal
172 // signature of the declaration being called, or else the call will
174 // - For the most part, unprototyped functions are called by casting to
175 // a formal signature inferred from the specific argument types used
176 // at the call-site. However, some targets (e.g. x86-64) screw with
177 // this for compatibility reasons.
179 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
180 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
181 const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
182 const FunctionArgList &Args,
183 const FunctionType::ExtInfo &Info,
186 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
187 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
188 QualType receiverType);
190 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
191 const CGFunctionInfo &arrangeCXXConstructorDeclaration(
192 const CXXConstructorDecl *D,
194 const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
197 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
198 const FunctionType *Ty);
199 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
200 const CallArgList &args,
201 FunctionType::ExtInfo info,
202 RequiredArgs required);
203 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
204 const FunctionType *type);
206 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
207 const FunctionProtoType *type,
208 RequiredArgs required);
210 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
211 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
212 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
213 const FunctionProtoType *FTP);
215 /// "Arrange" the LLVM information for a call or type with the given
216 /// signature. This is largely an internal method; other clients
217 /// should use one of the above routines, which ultimately defer to
220 /// \param argTypes - must all actually be canonical as params
221 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
222 ArrayRef<CanQualType> argTypes,
223 FunctionType::ExtInfo info,
226 /// \brief Compute a new LLVM record layout object for the given record.
227 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
228 llvm::StructType *Ty);
230 /// addRecordTypeName - Compute a name from the given record decl with an
231 /// optional suffix and name the given LLVM type using it.
232 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
236 public: // These are internal details of CGT that shouldn't be used externally.
237 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
238 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
240 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
241 /// argument types it would be passed as on the provided vector \arg
242 /// ArgTys. See ABIArgInfo::Expand.
243 void GetExpandedTypes(QualType type,
244 SmallVectorImpl<llvm::Type*> &expanded);
246 /// IsZeroInitializable - Return whether a type can be
247 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
248 bool isZeroInitializable(QualType T);
250 /// IsZeroInitializable - Return whether a record type can be
251 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
252 bool isZeroInitializable(const CXXRecordDecl *RD);
254 bool isRecordLayoutComplete(const Type *Ty) const;
255 bool noRecordsBeingLaidOut() const {
256 return RecordsBeingLaidOut.empty();
258 bool isRecordBeingLaidOut(const Type *Ty) const {
259 return RecordsBeingLaidOut.count(Ty);
264 } // end namespace CodeGen
265 } // end namespace clang