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