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/Module.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include <vector> 22 23 namespace llvm { 24 class FunctionType; 25 class Module; 26 class TargetData; 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 // Some of this stuff should probably be left on the CGM. 62 ASTContext &Context; 63 const TargetInfo &Target; 64 llvm::Module &TheModule; 65 const llvm::TargetData &TheTargetData; 66 const ABIInfo &TheABIInfo; 67 CGCXXABI &TheCXXABI; 68 const CodeGenOptions &CodeGenOpts; 69 CodeGenModule &CGM; 70 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; 76 77 /// CGRecordLayouts - This maps llvm struct type with corresponding 78 /// record layout info. 79 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 80 81 /// RecordDeclTypes - This contains the LLVM IR type for any converted 82 /// RecordDecl. 83 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes; 84 85 /// FunctionInfos - Hold memoized CGFunctionInfo results. 86 llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 87 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; 93 94 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed; 95 96 /// SkippedLayout - True if we didn't layout a function due to a being inside 97 /// a recursive struct conversion, set this to true. 98 bool SkippedLayout; 99 100 SmallVector<const RecordDecl *, 8> DeferredRecords; 101 102 private: 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; 106 107 public: 108 CodeGenTypes(CodeGenModule &CGM); 109 ~CodeGenTypes(); 110 getTargetData()111 const llvm::TargetData &getTargetData() const { return TheTargetData; } getTarget()112 const TargetInfo &getTarget() const { return Target; } getContext()113 ASTContext &getContext() const { return Context; } getABIInfo()114 const ABIInfo &getABIInfo() const { return TheABIInfo; } getCodeGenOpts()115 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } getCXXABI()116 CGCXXABI &getCXXABI() const { return TheCXXABI; } getLLVMContext()117 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 118 119 /// ConvertType - Convert type T into a llvm::Type. 120 llvm::Type *ConvertType(QualType T); 121 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); 127 128 /// GetFunctionType - Get the LLVM function type for \arg Info. 129 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info); 130 131 llvm::FunctionType *GetFunctionType(GlobalDecl GD); 132 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 135 /// type). 136 bool isFuncTypeConvertible(const FunctionType *FT); 137 bool isFuncTypeArgumentConvertible(QualType Ty); 138 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); 143 144 const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 145 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); 149 150 /// getNullaryFunctionInfo - Get the function info for a void() 151 /// function with standard CC. 152 const CGFunctionInfo &arrangeNullaryFunction(); 153 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. 162 // 163 // Key points: 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 168 // go awry. 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. 173 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, 179 bool isVariadic); 180 181 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD); 182 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 183 QualType receiverType); 184 185 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD); 186 const CGFunctionInfo &arrangeCXXConstructorDeclaration( 187 const CXXConstructorDecl *D, 188 CXXCtorType Type); 189 const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D, 190 CXXDtorType Type); 191 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); 198 199 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args, 200 const FunctionProtoType *type, 201 RequiredArgs required); 202 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); 207 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 211 /// this. 212 /// 213 /// \param argTypes - must all actually be canonical as params 214 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType, 215 ArrayRef<CanQualType> argTypes, 216 FunctionType::ExtInfo info, 217 RequiredArgs args); 218 219 /// \brief Compute a new LLVM record layout object for the given record. 220 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D, 221 llvm::StructType *Ty); 222 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, 226 StringRef suffix); 227 228 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); 232 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); 238 239 /// IsZeroInitializable - Return whether a type can be 240 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 241 bool isZeroInitializable(QualType T); 242 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); 246 247 bool isRecordLayoutComplete(const Type *Ty) const; noRecordsBeingLaidOut()248 bool noRecordsBeingLaidOut() const { 249 return RecordsBeingLaidOut.empty(); 250 } isRecordBeingLaidOut(const Type * Ty)251 bool isRecordBeingLaidOut(const Type *Ty) const { 252 return RecordsBeingLaidOut.count(Ty); 253 } 254 255 }; 256 257 } // end namespace CodeGen 258 } // end namespace clang 259 260 #endif 261