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