CodeGenTypes.h revision 04a67a6aa3dfdc92d57f7f8d93ba397348c868a4
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 "llvm/Module.h" 18#include "llvm/ADT/DenseMap.h" 19#include "llvm/ADT/SmallSet.h" 20#include <vector> 21 22#include "CGCall.h" 23#include "CGCXX.h" 24 25namespace llvm { 26 class FunctionType; 27 class Module; 28 class OpaqueType; 29 class PATypeHolder; 30 class TargetData; 31 class Type; 32 class LLVMContext; 33} 34 35namespace clang { 36 class ABIInfo; 37 class ASTContext; 38 class CXXConstructorDecl; 39 class CXXDestructorDecl; 40 class CXXMethodDecl; 41 class FieldDecl; 42 class FunctionProtoType; 43 class ObjCInterfaceDecl; 44 class ObjCIvarDecl; 45 class PointerType; 46 class QualType; 47 class RecordDecl; 48 class TagDecl; 49 class TargetInfo; 50 class Type; 51 52namespace CodeGen { 53 class CodeGenTypes; 54 55 /// CGRecordLayout - This class handles struct and union layout info while 56 /// lowering AST types to LLVM types. 57 class CGRecordLayout { 58 CGRecordLayout(); // DO NOT IMPLEMENT 59 60 /// LLVMType - The LLVMType corresponding to this record layout. 61 const llvm::Type *LLVMType; 62 63 /// ContainsPointerToDataMember - Whether one of the fields in this record 64 /// layout is a pointer to data member, or a struct that contains pointer to 65 /// data member. 66 bool ContainsPointerToDataMember; 67 68 public: 69 CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember) 70 : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) { } 71 72 /// getLLVMType - Return llvm type associated with this record. 73 const llvm::Type *getLLVMType() const { 74 return LLVMType; 75 } 76 77 /// containsPointerToDataMember - Whether this struct contains pointers to 78 /// data members. 79 bool containsPointerToDataMember() const { 80 return ContainsPointerToDataMember; 81 } 82 }; 83 84/// CodeGenTypes - This class organizes the cross-module state that is used 85/// while lowering AST types to LLVM types. 86class CodeGenTypes { 87 ASTContext &Context; 88 const TargetInfo &Target; 89 llvm::Module& TheModule; 90 const llvm::TargetData& TheTargetData; 91 const ABIInfo& TheABIInfo; 92 93 llvm::SmallVector<std::pair<QualType, 94 llvm::OpaqueType *>, 8> PointersToResolve; 95 96 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes; 97 98 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes; 99 100 /// The opaque type map for Objective-C interfaces. All direct 101 /// manipulation is done by the runtime interfaces, which are 102 /// responsible for coercing to the appropriate type; these opaque 103 /// types are never refined. 104 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes; 105 106 /// CGRecordLayouts - This maps llvm struct type with corresponding 107 /// record layout info. 108 /// FIXME : If CGRecordLayout is less than 16 bytes then use 109 /// inline it in the map. 110 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 111 112 /// FieldInfo - This maps struct field with corresponding llvm struct type 113 /// field no. This info is populated by record organizer. 114 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; 115 116 /// FunctionInfos - Hold memoized CGFunctionInfo results. 117 llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 118 119public: 120 struct BitFieldInfo { 121 BitFieldInfo(unsigned FieldNo, 122 unsigned Start, 123 unsigned Size) 124 : FieldNo(FieldNo), Start(Start), Size(Size) {} 125 126 unsigned FieldNo; 127 unsigned Start; 128 unsigned Size; 129 }; 130 131private: 132 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields; 133 134 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) 135 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is 136 /// used instead of llvm::Type because it allows us to bypass potential 137 /// dangling type pointers due to type refinement on llvm side. 138 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache; 139 140 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this 141 /// method directly because it does not do any type caching. This method 142 /// is available only for ConvertType(). CovertType() is preferred 143 /// interface to convert type T into a llvm::Type. 144 const llvm::Type *ConvertNewType(QualType T); 145public: 146 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD, 147 const ABIInfo &Info); 148 ~CodeGenTypes(); 149 150 const llvm::TargetData &getTargetData() const { return TheTargetData; } 151 const TargetInfo &getTarget() const { return Target; } 152 ASTContext &getContext() const { return Context; } 153 const ABIInfo &getABIInfo() const { return TheABIInfo; } 154 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 155 156 /// ConvertType - Convert type T into a llvm::Type. 157 const llvm::Type *ConvertType(QualType T); 158 const llvm::Type *ConvertTypeRecursive(QualType T); 159 160 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 161 /// ConvertType in that it is used to convert to the memory representation for 162 /// a type. For example, the scalar representation for _Bool is i1, but the 163 /// memory representation is usually i8 or i32, depending on the target. 164 const llvm::Type *ConvertTypeForMem(QualType T); 165 const llvm::Type *ConvertTypeForMemRecursive(QualType T); 166 167 /// GetFunctionType - Get the LLVM function type for \arg Info. 168 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 169 bool IsVariadic); 170 171 172 /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable, 173 /// given a CXXMethodDecl. If the method to has an incomplete return type, 174 /// and/or incomplete argument types, this will return the opaque type. 175 const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD); 176 177 const CGRecordLayout &getCGRecordLayout(const TagDecl*) const; 178 179 /// getLLVMFieldNo - Return llvm::StructType element number 180 /// that corresponds to the field FD. 181 unsigned getLLVMFieldNo(const FieldDecl *FD); 182 183 /// UpdateCompletedType - When we find the full definition for a TagDecl, 184 /// replace the 'opaque' type we previously made for it if applicable. 185 void UpdateCompletedType(const TagDecl *TD); 186 187private: 188 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP); 189 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP); 190 191public: 192 /// getFunctionInfo - Get the function info for the specified function decl. 193 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 194 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 195 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 196 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 197 CXXCtorType Type); 198 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 199 CXXDtorType Type); 200 201 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args, 202 const FunctionType *Ty) { 203 return getFunctionInfo(Ty->getResultType(), Args, 204 Ty->getCallConv(), Ty->getNoReturnAttr()); 205 } 206 207 // getFunctionInfo - Get the function info for a member function. 208 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 209 const FunctionProtoType *FTP); 210 211 /// getFunctionInfo - Get the function info for a function described by a 212 /// return type and argument types. If the calling convention is not 213 /// specified, the "C" calling convention will be used. 214 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 215 const CallArgList &Args, 216 CallingConv CC, 217 bool NoReturn); 218 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 219 const FunctionArgList &Args, 220 CallingConv CC, 221 bool NoReturn); 222 const CGFunctionInfo &getFunctionInfo(QualType RetTy, 223 const llvm::SmallVector<QualType, 16> &ArgTys, 224 CallingConv CC, 225 bool NoReturn); 226 227public: // These are internal details of CGT that shouldn't be used externally. 228 /// addFieldInfo - Assign field number to field FD. 229 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo); 230 231 /// addBitFieldInfo - Assign a start bit and a size to field FD. 232 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, 233 unsigned Start, unsigned Size); 234 235 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field 236 /// FD. 237 BitFieldInfo getBitFieldInfo(const FieldDecl *FD); 238 239 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 240 /// enum. 241 const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 242 243 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 244 /// argument types it would be passed as on the provided vector \arg 245 /// ArgTys. See ABIArgInfo::Expand. 246 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); 247}; 248 249} // end namespace CodeGen 250} // end namespace clang 251 252#endif 253