CodeGenTypes.h revision 207619
167754Smsmith//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// 267754Smsmith// 367754Smsmith// The LLVM Compiler Infrastructure 4114237Snjl// 567754Smsmith// This file is distributed under the University of Illinois Open Source 667754Smsmith// License. See LICENSE.TXT for details. 767754Smsmith// 867754Smsmith//===----------------------------------------------------------------------===// 967754Smsmith// 1067754Smsmith// This is the code that handles AST -> LLVM type lowering. 1167754Smsmith// 12114237Snjl//===----------------------------------------------------------------------===// 1370243Smsmith 1467754Smsmith#ifndef CLANG_CODEGEN_CODEGENTYPES_H 1567754Smsmith#define CLANG_CODEGEN_CODEGENTYPES_H 1667754Smsmith 1767754Smsmith#include "llvm/Module.h" 1867754Smsmith#include "llvm/ADT/DenseMap.h" 1967754Smsmith#include <vector> 2067754Smsmith 2167754Smsmith#include "CGCall.h" 2267754Smsmith#include "GlobalDecl.h" 2367754Smsmith 2467754Smsmithnamespace llvm { 2567754Smsmith class FunctionType; 2667754Smsmith class Module; 2767754Smsmith class OpaqueType; 2867754Smsmith class PATypeHolder; 2967754Smsmith class TargetData; 3067754Smsmith class Type; 3167754Smsmith class LLVMContext; 3267754Smsmith} 3367754Smsmith 3467754Smsmithnamespace clang { 3567754Smsmith class ABIInfo; 3667754Smsmith class ASTContext; 3767754Smsmith template <typename> class CanQual; 3867754Smsmith class CXXConstructorDecl; 3967754Smsmith class CXXDestructorDecl; 4067754Smsmith class CXXMethodDecl; 4167754Smsmith class FieldDecl; 4267754Smsmith class FunctionProtoType; 4367754Smsmith class ObjCInterfaceDecl; 4467754Smsmith class ObjCIvarDecl; 4567754Smsmith class PointerType; 4667754Smsmith class QualType; 4767754Smsmith class RecordDecl; 4867754Smsmith class TagDecl; 4967754Smsmith class TargetInfo; 5067754Smsmith class Type; 5167754Smsmith typedef CanQual<Type> CanQualType; 5267754Smsmith 5367754Smsmithnamespace CodeGen { 5467754Smsmith class CGRecordLayout; 5567754Smsmith 5667754Smsmith/// CodeGenTypes - This class organizes the cross-module state that is used 5767754Smsmith/// while lowering AST types to LLVM types. 5867754Smsmithclass CodeGenTypes { 5967754Smsmith ASTContext &Context; 6067754Smsmith const TargetInfo &Target; 6167754Smsmith llvm::Module& TheModule; 6267754Smsmith const llvm::TargetData& TheTargetData; 6367754Smsmith const ABIInfo& TheABIInfo; 6467754Smsmith 6567754Smsmith llvm::SmallVector<std::pair<QualType, 6667754Smsmith llvm::OpaqueType *>, 8> PointersToResolve; 6767754Smsmith 6867754Smsmith llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes; 6967754Smsmith 7067754Smsmith llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes; 7167754Smsmith 7267754Smsmith /// The opaque type map for Objective-C interfaces. All direct 7367754Smsmith /// manipulation is done by the runtime interfaces, which are 7467754Smsmith /// responsible for coercing to the appropriate type; these opaque 7567754Smsmith /// types are never refined. 7667754Smsmith llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes; 7767754Smsmith 7867754Smsmith /// CGRecordLayouts - This maps llvm struct type with corresponding 7967754Smsmith /// record layout info. 8067754Smsmith llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 8167754Smsmith 8267754Smsmith /// FunctionInfos - Hold memoized CGFunctionInfo results. 8367754Smsmith llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 8467754Smsmith 8567754Smsmithprivate: 8667754Smsmith /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) 8767754Smsmith /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is 8867754Smsmith /// used instead of llvm::Type because it allows us to bypass potential 8967754Smsmith /// dangling type pointers due to type refinement on llvm side. 9067754Smsmith llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache; 9167754Smsmith 9267754Smsmith /// ConvertNewType - Convert type T into a llvm::Type. Do not use this 9367754Smsmith /// method directly because it does not do any type caching. This method 9467754Smsmith /// is available only for ConvertType(). CovertType() is preferred 9567754Smsmith /// interface to convert type T into a llvm::Type. 9667754Smsmith const llvm::Type *ConvertNewType(QualType T); 9767754Smsmithpublic: 9867754Smsmith CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD, 9967754Smsmith const ABIInfo &Info); 10067754Smsmith ~CodeGenTypes(); 10167754Smsmith 10267754Smsmith const llvm::TargetData &getTargetData() const { return TheTargetData; } 10367754Smsmith const TargetInfo &getTarget() const { return Target; } 10467754Smsmith ASTContext &getContext() const { return Context; } 10567754Smsmith const ABIInfo &getABIInfo() const { return TheABIInfo; } 10667754Smsmith llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 10767754Smsmith 10867754Smsmith /// ConvertType - Convert type T into a llvm::Type. 10967754Smsmith const llvm::Type *ConvertType(QualType T); 11067754Smsmith const llvm::Type *ConvertTypeRecursive(QualType T); 11167754Smsmith 11267754Smsmith /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 11367754Smsmith /// ConvertType in that it is used to convert to the memory representation for 11467754Smsmith /// a type. For example, the scalar representation for _Bool is i1, but the 11567754Smsmith /// memory representation is usually i8 or i32, depending on the target. 11667754Smsmith const llvm::Type *ConvertTypeForMem(QualType T); 11767754Smsmith const llvm::Type *ConvertTypeForMemRecursive(QualType T); 11867754Smsmith 11967754Smsmith /// GetFunctionType - Get the LLVM function type for \arg Info. 12067754Smsmith const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 121102550Siwasaki bool IsVariadic); 12267754Smsmith 123102550Siwasaki const llvm::FunctionType *GetFunctionType(GlobalDecl GD); 12491116Smsmith 12567754Smsmith 12667754Smsmith /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 12767754Smsmith /// given a CXXMethodDecl. If the method to has an incomplete return type, 12867754Smsmith /// and/or incomplete argument types, this will return the opaque type. 12967754Smsmith const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD); 13067754Smsmith 13167754Smsmith const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const; 13267754Smsmith 13367754Smsmith /// UpdateCompletedType - When we find the full definition for a TagDecl, 134114237Snjl /// replace the 'opaque' type we previously made for it if applicable. 13567754Smsmith void UpdateCompletedType(const TagDecl *TD); 13667754Smsmith 13767754Smsmith /// getFunctionInfo - Get the function info for the specified function decl. 13867754Smsmith const CGFunctionInfo &getFunctionInfo(GlobalDecl GD); 13967754Smsmith 14099679Siwasaki const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 14199679Siwasaki const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 14299679Siwasaki const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 14399679Siwasaki const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 14499679Siwasaki CXXCtorType Type); 14567754Smsmith const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 14667754Smsmith CXXDtorType Type); 14767754Smsmith 14867754Smsmith const CGFunctionInfo &getFunctionInfo(const CallArgList &Args, 14967754Smsmith const FunctionType *Ty) { 15067754Smsmith return getFunctionInfo(Ty->getResultType(), Args, 15167754Smsmith Ty->getExtInfo()); 15267754Smsmith } 15367754Smsmith const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty); 15467754Smsmith const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty); 15567754Smsmith 15667754Smsmith // getFunctionInfo - Get the function info for a member function. 15767754Smsmith const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 15867754Smsmith const FunctionProtoType *FTP); 15967754Smsmith 16067754Smsmith /// getFunctionInfo - Get the function info for a function described by a 161114237Snjl /// return type and argument types. If the calling convention is not 16267754Smsmith /// specified, the "C" calling convention will be used. 16367754Smsmith const CGFunctionInfo &getFunctionInfo(QualType ResTy, 16467754Smsmith const CallArgList &Args, 16567754Smsmith const FunctionType::ExtInfo &Info); 16691116Smsmith const CGFunctionInfo &getFunctionInfo(QualType ResTy, 16767754Smsmith const FunctionArgList &Args, 16883174Smsmith const FunctionType::ExtInfo &Info); 16983174Smsmith 17067754Smsmith /// Retrieves the ABI information for the given function signature. 17167754Smsmith /// 17283174Smsmith /// \param ArgTys - must all actually be canonical as params 17383174Smsmith const CGFunctionInfo &getFunctionInfo(CanQualType RetTy, 17467754Smsmith const llvm::SmallVectorImpl<CanQualType> &ArgTys, 17583174Smsmith const FunctionType::ExtInfo &Info); 17683174Smsmith 17767754Smsmith /// \brief Compute a new LLVM record layout object for the given record. 17883174Smsmith CGRecordLayout *ComputeRecordLayout(const RecordDecl *D); 17967754Smsmith 18067754Smsmithpublic: // These are internal details of CGT that shouldn't be used externally. 18167754Smsmith /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 18283174Smsmith /// enum. 18383174Smsmith const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 18467754Smsmith 18583174Smsmith /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 18667754Smsmith /// argument types it would be passed as on the provided vector \arg 18767754Smsmith /// ArgTys. See ABIArgInfo::Expand. 18883174Smsmith void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); 18983174Smsmith}; 19067754Smsmith 19183174Smsmith} // end namespace CodeGen 19267754Smsmith} // end namespace clang 19367754Smsmith 19467754Smsmith#endif 19567754Smsmith