clang API Documentation
00001 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This is the code that handles AST -> LLVM type lowering. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef CLANG_CODEGEN_CODEGENTYPES_H 00015 #define CLANG_CODEGEN_CODEGENTYPES_H 00016 00017 #include "CGCall.h" 00018 #include "clang/AST/GlobalDecl.h" 00019 #include "llvm/Module.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 #include <vector> 00022 00023 namespace llvm { 00024 class FunctionType; 00025 class Module; 00026 class TargetData; 00027 class Type; 00028 class LLVMContext; 00029 class StructType; 00030 } 00031 00032 namespace clang { 00033 class ABIInfo; 00034 class ASTContext; 00035 template <typename> class CanQual; 00036 class CXXConstructorDecl; 00037 class CXXDestructorDecl; 00038 class CXXMethodDecl; 00039 class CodeGenOptions; 00040 class FieldDecl; 00041 class FunctionProtoType; 00042 class ObjCInterfaceDecl; 00043 class ObjCIvarDecl; 00044 class PointerType; 00045 class QualType; 00046 class RecordDecl; 00047 class TagDecl; 00048 class TargetInfo; 00049 class Type; 00050 typedef CanQual<Type> CanQualType; 00051 00052 namespace CodeGen { 00053 class CGCXXABI; 00054 class CGRecordLayout; 00055 class CodeGenModule; 00056 class RequiredArgs; 00057 00058 /// CodeGenTypes - This class organizes the cross-module state that is used 00059 /// while lowering AST types to LLVM types. 00060 class CodeGenTypes { 00061 // Some of this stuff should probably be left on the CGM. 00062 ASTContext &Context; 00063 const TargetInfo &Target; 00064 llvm::Module &TheModule; 00065 const llvm::TargetData &TheTargetData; 00066 const ABIInfo &TheABIInfo; 00067 CGCXXABI &TheCXXABI; 00068 const CodeGenOptions &CodeGenOpts; 00069 CodeGenModule &CGM; 00070 00071 /// The opaque type map for Objective-C interfaces. All direct 00072 /// manipulation is done by the runtime interfaces, which are 00073 /// responsible for coercing to the appropriate type; these opaque 00074 /// types are never refined. 00075 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes; 00076 00077 /// CGRecordLayouts - This maps llvm struct type with corresponding 00078 /// record layout info. 00079 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 00080 00081 /// RecordDeclTypes - This contains the LLVM IR type for any converted 00082 /// RecordDecl. 00083 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes; 00084 00085 /// FunctionInfos - Hold memoized CGFunctionInfo results. 00086 llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 00087 00088 /// RecordsBeingLaidOut - This set keeps track of records that we're currently 00089 /// converting to an IR type. For example, when converting: 00090 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B' 00091 /// types will be in this set. 00092 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut; 00093 00094 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed; 00095 00096 /// SkippedLayout - True if we didn't layout a function due to a being inside 00097 /// a recursive struct conversion, set this to true. 00098 bool SkippedLayout; 00099 00100 SmallVector<const RecordDecl *, 8> DeferredRecords; 00101 00102 private: 00103 /// TypeCache - This map keeps cache of llvm::Types 00104 /// and maps llvm::Types to corresponding clang::Type. 00105 llvm::DenseMap<const Type *, llvm::Type *> TypeCache; 00106 00107 public: 00108 CodeGenTypes(CodeGenModule &CGM); 00109 ~CodeGenTypes(); 00110 00111 const llvm::TargetData &getTargetData() const { return TheTargetData; } 00112 const TargetInfo &getTarget() const { return Target; } 00113 ASTContext &getContext() const { return Context; } 00114 const ABIInfo &getABIInfo() const { return TheABIInfo; } 00115 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } 00116 CGCXXABI &getCXXABI() const { return TheCXXABI; } 00117 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 00118 00119 /// ConvertType - Convert type T into a llvm::Type. 00120 llvm::Type *ConvertType(QualType T); 00121 00122 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 00123 /// ConvertType in that it is used to convert to the memory representation for 00124 /// a type. For example, the scalar representation for _Bool is i1, but the 00125 /// memory representation is usually i8 or i32, depending on the target. 00126 llvm::Type *ConvertTypeForMem(QualType T); 00127 00128 /// GetFunctionType - Get the LLVM function type for \arg Info. 00129 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info); 00130 00131 llvm::FunctionType *GetFunctionType(GlobalDecl GD); 00132 00133 /// isFuncTypeConvertible - Utility to check whether a function type can 00134 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 00135 /// type). 00136 bool isFuncTypeConvertible(const FunctionType *FT); 00137 bool isFuncTypeArgumentConvertible(QualType Ty); 00138 00139 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 00140 /// given a CXXMethodDecl. If the method to has an incomplete return type, 00141 /// and/or incomplete argument types, this will return the opaque type. 00142 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 00143 00144 const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 00145 00146 /// UpdateCompletedType - When we find the full definition for a TagDecl, 00147 /// replace the 'opaque' type we previously made for it if applicable. 00148 void UpdateCompletedType(const TagDecl *TD); 00149 00150 /// getNullaryFunctionInfo - Get the function info for a void() 00151 /// function with standard CC. 00152 const CGFunctionInfo &arrangeNullaryFunction(); 00153 00154 // The arrangement methods are split into three families: 00155 // - those meant to drive the signature and prologue/epilogue 00156 // of a function declaration or definition, 00157 // - those meant for the computation of the LLVM type for an abstract 00158 // appearance of a function, and 00159 // - those meant for performing the IR-generation of a call. 00160 // They differ mainly in how they deal with optional (i.e. variadic) 00161 // arguments, as well as unprototyped functions. 00162 // 00163 // Key points: 00164 // - The CGFunctionInfo for emitting a specific call site must include 00165 // entries for the optional arguments. 00166 // - The function type used at the call site must reflect the formal 00167 // signature of the declaration being called, or else the call will 00168 // go awry. 00169 // - For the most part, unprototyped functions are called by casting to 00170 // a formal signature inferred from the specific argument types used 00171 // at the call-site. However, some targets (e.g. x86-64) screw with 00172 // this for compatibility reasons. 00173 00174 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD); 00175 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD); 00176 const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy, 00177 const FunctionArgList &Args, 00178 const FunctionType::ExtInfo &Info, 00179 bool isVariadic); 00180 00181 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD); 00182 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 00183 QualType receiverType); 00184 00185 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD); 00186 const CGFunctionInfo &arrangeCXXConstructorDeclaration( 00187 const CXXConstructorDecl *D, 00188 CXXCtorType Type); 00189 const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D, 00190 CXXDtorType Type); 00191 00192 const CGFunctionInfo &arrangeFunctionCall(const CallArgList &Args, 00193 const FunctionType *Ty); 00194 const CGFunctionInfo &arrangeFunctionCall(QualType ResTy, 00195 const CallArgList &args, 00196 const FunctionType::ExtInfo &info, 00197 RequiredArgs required); 00198 00199 const CGFunctionInfo &arrangeFunctionType(CanQual<FunctionProtoType> Ty); 00200 const CGFunctionInfo &arrangeFunctionType(CanQual<FunctionNoProtoType> Ty); 00201 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD, 00202 const FunctionProtoType *FTP); 00203 00204 /// Retrieves the ABI information for the given function signature. 00205 /// This is the "core" routine to which all the others defer. 00206 /// 00207 /// \param argTypes - must all actually be canonical as params 00208 const CGFunctionInfo &arrangeFunctionType(CanQualType returnType, 00209 ArrayRef<CanQualType> argTypes, 00210 const FunctionType::ExtInfo &info, 00211 RequiredArgs args); 00212 00213 /// \brief Compute a new LLVM record layout object for the given record. 00214 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D, 00215 llvm::StructType *Ty); 00216 00217 /// addRecordTypeName - Compute a name from the given record decl with an 00218 /// optional suffix and name the given LLVM type using it. 00219 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, 00220 StringRef suffix); 00221 00222 00223 public: // These are internal details of CGT that shouldn't be used externally. 00224 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 00225 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD); 00226 00227 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 00228 /// argument types it would be passed as on the provided vector \arg 00229 /// ArgTys. See ABIArgInfo::Expand. 00230 void GetExpandedTypes(QualType type, 00231 SmallVectorImpl<llvm::Type*> &expanded); 00232 00233 /// IsZeroInitializable - Return whether a type can be 00234 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 00235 bool isZeroInitializable(QualType T); 00236 00237 /// IsZeroInitializable - Return whether a record type can be 00238 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 00239 bool isZeroInitializable(const CXXRecordDecl *RD); 00240 00241 bool isRecordLayoutComplete(const Type *Ty) const; 00242 bool noRecordsBeingLaidOut() const { 00243 return RecordsBeingLaidOut.empty(); 00244 } 00245 bool isRecordBeingLaidOut(const Type *Ty) const { 00246 return RecordsBeingLaidOut.count(Ty); 00247 } 00248 00249 }; 00250 00251 } // end namespace CodeGen 00252 } // end namespace clang 00253 00254 #endif