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