1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that handles AST -> LLVM type lowering.
12 //===----------------------------------------------------------------------===//
14 #ifndef CLANG_CODEGEN_CODEGENTYPES_H
15 #define CLANG_CODEGEN_CODEGENTYPES_H
18 #include "clang/AST/GlobalDecl.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/DenseMap.h"
35 template <typename> class CanQual;
36 class CXXConstructorDecl;
37 class CXXDestructorDecl;
41 class FunctionProtoType;
42 class ObjCInterfaceDecl;
50 typedef CanQual<Type> CanQualType;
56 /// CodeGenTypes - This class organizes the cross-module state that is used
57 /// while lowering AST types to LLVM types.
60 const TargetInfo &Target;
61 llvm::Module &TheModule;
62 const llvm::TargetData &TheTargetData;
63 const ABIInfo &TheABIInfo;
65 const CodeGenOptions &CodeGenOpts;
67 /// The opaque type map for Objective-C interfaces. All direct
68 /// manipulation is done by the runtime interfaces, which are
69 /// responsible for coercing to the appropriate type; these opaque
70 /// types are never refined.
71 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
73 /// CGRecordLayouts - This maps llvm struct type with corresponding
74 /// record layout info.
75 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
77 /// RecordDeclTypes - This contains the LLVM IR type for any converted
79 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
81 /// FunctionInfos - Hold memoized CGFunctionInfo results.
82 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
84 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
85 /// converting to an IR type. For example, when converting:
86 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
87 /// types will be in this set.
88 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
90 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
92 /// SkippedLayout - True if we didn't layout a function due to a being inside
93 /// a recursive struct conversion, set this to true.
96 llvm::SmallVector<const RecordDecl *, 8> DeferredRecords;
99 /// TypeCache - This map keeps cache of llvm::Types
100 /// and maps llvm::Types to corresponding clang::Type.
101 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
104 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
105 const ABIInfo &Info, CGCXXABI &CXXABI,
106 const CodeGenOptions &Opts);
109 const llvm::TargetData &getTargetData() const { return TheTargetData; }
110 const TargetInfo &getTarget() const { return Target; }
111 ASTContext &getContext() const { return Context; }
112 const ABIInfo &getABIInfo() const { return TheABIInfo; }
113 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
114 CGCXXABI &getCXXABI() const { return TheCXXABI; }
115 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
117 /// ConvertType - Convert type T into a llvm::Type.
118 llvm::Type *ConvertType(QualType T);
120 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
121 /// ConvertType in that it is used to convert to the memory representation for
122 /// a type. For example, the scalar representation for _Bool is i1, but the
123 /// memory representation is usually i8 or i32, depending on the target.
124 llvm::Type *ConvertTypeForMem(QualType T);
126 /// GetFunctionType - Get the LLVM function type for \arg Info.
127 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
130 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
132 /// isFuncTypeConvertible - Utility to check whether a function type can
133 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
135 bool isFuncTypeConvertible(const FunctionType *FT);
136 bool isFuncTypeArgumentConvertible(QualType Ty);
138 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
139 /// given a CXXMethodDecl. If the method to has an incomplete return type,
140 /// and/or incomplete argument types, this will return the opaque type.
141 const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
143 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
145 /// UpdateCompletedType - When we find the full definition for a TagDecl,
146 /// replace the 'opaque' type we previously made for it if applicable.
147 void UpdateCompletedType(const TagDecl *TD);
149 /// getNullaryFunctionInfo - Get the function info for a void()
150 /// function with standard CC.
151 const CGFunctionInfo &getNullaryFunctionInfo();
153 /// getFunctionInfo - Get the function info for the specified function decl.
154 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
156 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
157 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
158 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
159 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
161 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
164 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
165 const FunctionType *Ty) {
166 return getFunctionInfo(Ty->getResultType(), Args,
170 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
171 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
173 /// getFunctionInfo - Get the function info for a member function of
174 /// the given type. This is used for calls through member function
176 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
177 const FunctionProtoType *FTP);
179 /// getFunctionInfo - Get the function info for a function described by a
180 /// return type and argument types. If the calling convention is not
181 /// specified, the "C" calling convention will be used.
182 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
183 const CallArgList &Args,
184 const FunctionType::ExtInfo &Info);
185 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
186 const FunctionArgList &Args,
187 const FunctionType::ExtInfo &Info);
189 /// Retrieves the ABI information for the given function signature.
191 /// \param ArgTys - must all actually be canonical as params
192 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
193 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
194 const FunctionType::ExtInfo &Info);
196 /// \brief Compute a new LLVM record layout object for the given record.
197 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
198 llvm::StructType *Ty);
200 /// addRecordTypeName - Compute a name from the given record decl with an
201 /// optional suffix and name the given LLVM type using it.
202 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
203 llvm::StringRef suffix);
206 public: // These are internal details of CGT that shouldn't be used externally.
207 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
208 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
210 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
211 /// argument types it would be passed as on the provided vector \arg
212 /// ArgTys. See ABIArgInfo::Expand.
213 void GetExpandedTypes(QualType type,
214 llvm::SmallVectorImpl<llvm::Type*> &expanded);
216 /// IsZeroInitializable - Return whether a type can be
217 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
218 bool isZeroInitializable(QualType T);
220 /// IsZeroInitializable - Return whether a record type can be
221 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
222 bool isZeroInitializable(const CXXRecordDecl *RD);
224 bool isRecordLayoutComplete(const Type *Ty) const;
225 bool noRecordsBeingLaidOut() const {
226 return RecordsBeingLaidOut.empty();
228 bool isRecordBeingLaidOut(const Type *Ty) const {
229 return RecordsBeingLaidOut.count(Ty);
234 } // end namespace CodeGen
235 } // end namespace clang