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/Module.h"
20 #include "llvm/ADT/DenseMap.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.
61 // Some of this stuff should probably be left on the CGM.
63 const TargetInfo &Target;
64 llvm::Module &TheModule;
65 const llvm::DataLayout &TheDataLayout;
66 const ABIInfo &TheABIInfo;
68 const CodeGenOptions &CodeGenOpts;
71 /// The opaque type map for Objective-C interfaces. All direct
72 /// manipulation is done by the runtime interfaces, which are
73 /// responsible for coercing to the appropriate type; these opaque
74 /// types are never refined.
75 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
77 /// CGRecordLayouts - This maps llvm struct type with corresponding
78 /// record layout info.
79 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
81 /// RecordDeclTypes - This contains the LLVM IR type for any converted
83 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
85 /// FunctionInfos - Hold memoized CGFunctionInfo results.
86 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
88 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
89 /// converting to an IR type. For example, when converting:
90 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
91 /// types will be in this set.
92 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
94 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
96 /// SkippedLayout - True if we didn't layout a function due to a being inside
97 /// a recursive struct conversion, set this to true.
100 SmallVector<const RecordDecl *, 8> DeferredRecords;
103 /// TypeCache - This map keeps cache of llvm::Types
104 /// and maps llvm::Types to corresponding clang::Type.
105 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
108 CodeGenTypes(CodeGenModule &CGM);
111 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
112 const TargetInfo &getTarget() const { return Target; }
113 ASTContext &getContext() const { return Context; }
114 const ABIInfo &getABIInfo() const { return TheABIInfo; }
115 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
116 CGCXXABI &getCXXABI() const { return TheCXXABI; }
117 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
119 /// ConvertType - Convert type T into a llvm::Type.
120 llvm::Type *ConvertType(QualType T);
122 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
123 /// ConvertType in that it is used to convert to the memory representation for
124 /// a type. For example, the scalar representation for _Bool is i1, but the
125 /// memory representation is usually i8 or i32, depending on the target.
126 llvm::Type *ConvertTypeForMem(QualType T);
128 /// GetFunctionType - Get the LLVM function type for \arg Info.
129 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
131 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
133 /// isFuncTypeConvertible - Utility to check whether a function type can
134 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
136 bool isFuncTypeConvertible(const FunctionType *FT);
137 bool isFuncTypeArgumentConvertible(QualType Ty);
139 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
140 /// given a CXXMethodDecl. If the method to has an incomplete return type,
141 /// and/or incomplete argument types, this will return the opaque type.
142 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
144 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
146 /// UpdateCompletedType - When we find the full definition for a TagDecl,
147 /// replace the 'opaque' type we previously made for it if applicable.
148 void UpdateCompletedType(const TagDecl *TD);
150 /// getNullaryFunctionInfo - Get the function info for a void()
151 /// function with standard CC.
152 const CGFunctionInfo &arrangeNullaryFunction();
154 // The arrangement methods are split into three families:
155 // - those meant to drive the signature and prologue/epilogue
156 // of a function declaration or definition,
157 // - those meant for the computation of the LLVM type for an abstract
158 // appearance of a function, and
159 // - those meant for performing the IR-generation of a call.
160 // They differ mainly in how they deal with optional (i.e. variadic)
161 // arguments, as well as unprototyped functions.
164 // - The CGFunctionInfo for emitting a specific call site must include
165 // entries for the optional arguments.
166 // - The function type used at the call site must reflect the formal
167 // signature of the declaration being called, or else the call will
169 // - For the most part, unprototyped functions are called by casting to
170 // a formal signature inferred from the specific argument types used
171 // at the call-site. However, some targets (e.g. x86-64) screw with
172 // this for compatibility reasons.
174 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
175 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
176 const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
177 const FunctionArgList &Args,
178 const FunctionType::ExtInfo &Info,
181 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
182 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
183 QualType receiverType);
185 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
186 const CGFunctionInfo &arrangeCXXConstructorDeclaration(
187 const CXXConstructorDecl *D,
189 const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
192 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
193 const FunctionType *Ty);
194 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
195 const CallArgList &args,
196 FunctionType::ExtInfo info,
197 RequiredArgs required);
199 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
200 const FunctionProtoType *type,
201 RequiredArgs required);
203 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
204 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
205 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
206 const FunctionProtoType *FTP);
208 /// "Arrange" the LLVM information for a call or type with the given
209 /// signature. This is largely an internal method; other clients
210 /// should use one of the above routines, which ultimately defer to
213 /// \param argTypes - must all actually be canonical as params
214 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
215 ArrayRef<CanQualType> argTypes,
216 FunctionType::ExtInfo info,
219 /// \brief Compute a new LLVM record layout object for the given record.
220 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
221 llvm::StructType *Ty);
223 /// addRecordTypeName - Compute a name from the given record decl with an
224 /// optional suffix and name the given LLVM type using it.
225 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
229 public: // These are internal details of CGT that shouldn't be used externally.
230 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
231 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
233 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
234 /// argument types it would be passed as on the provided vector \arg
235 /// ArgTys. See ABIArgInfo::Expand.
236 void GetExpandedTypes(QualType type,
237 SmallVectorImpl<llvm::Type*> &expanded);
239 /// IsZeroInitializable - Return whether a type can be
240 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
241 bool isZeroInitializable(QualType T);
243 /// IsZeroInitializable - Return whether a record type can be
244 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
245 bool isZeroInitializable(const CXXRecordDecl *RD);
247 bool isRecordLayoutComplete(const Type *Ty) const;
248 bool noRecordsBeingLaidOut() const {
249 return RecordsBeingLaidOut.empty();
251 bool isRecordBeingLaidOut(const Type *Ty) const {
252 return RecordsBeingLaidOut.count(Ty);
257 } // end namespace CodeGen
258 } // end namespace clang